-
Notifications
You must be signed in to change notification settings - Fork 13
/
builtins.lisp
1487 lines (1285 loc) · 55.4 KB
/
builtins.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; builtins.lisp
;; MathMap
;; Copyright (C) 2002-2009 Mark Probst
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
(load "lisp-utils/utils.lisp")
(load "lisp-utils/let-match.lisp")
(defpackage "MATHMAP"
(:use "CL" #+clisp "EXT" "UTILS" "LET-MATCH"))
(in-package :mathmap)
(load "ops.lisp")
(load "simplify.lisp")
(defparameter *only-ansi* nil)
(defun my-macroexpand (sexp macros)
(labels ((expand (sexp)
(if (consp sexp)
(if (symbolp (car sexp))
(let ((macro (cdr (assoc (car sexp) macros))))
(if macro
(mapcar #'expand (apply macro (cdr sexp)))
(cons (car sexp) (mapcar #'expand (cdr sexp)))))
(mapcar #'expand sexp))
sexp)))
(expand sexp)))
(defparameter *ops* '((+v 2 "OP_ADD" "ADD")
(-v 2 "OP_SUB" "SUB")
(-v 1 "OP_NEG" "NEG")
(*v 2 "OP_MUL" "MUL")
(/v 2 "OP_DIV" "DIV")
(%v 2 "OP_MOD" "MOD")
(abs-v 1 "OP_ABS" "fabs")))
(defparameter *primops* (mapcar #'(lambda (op)
(list (op-name op) (op-arity op) (op-c-define op) (op-c-name op) (op-type op)))
*operators*))
(defparameter *condops* '((= 2 "OP_EQ" "EQ")
(< 2 "OP_LESS" "LESS")
(<= 2 "OP_LEQ" "LEQ")))
(defstruct builtin
overloaded-name name type args body docstring)
(defparameter *builtins* nil)
(defmacro defbuiltin (overloaded-name name type args &rest body)
(let ((docstring (if (stringp (first body))
(first body)
nil))
(body (if (stringp (first body))
(rest body)
body)))
`(push (make-builtin :overloaded-name ,overloaded-name
:name ',name
:type ',type
:args ',args
:body ',body
:docstring ,docstring)
*builtins*)))
(defmacro def-simple-builtin (overloaded-name name op-name arg-names &optional docstring)
(let* ((args-decl (mapcar #'(lambda (arg-name) `(,arg-name (?T 1))) arg-names))
(actual-args (mapcar #'(lambda (arg-name) `(nth 0 ,arg-name)) arg-names)))
`(defbuiltin ,overloaded-name ,name (?T 1) ,args-decl
,docstring
(set result (make (?T 1) (,op-name ,@actual-args))))))
(defun c-type (type)
(second (assoc type '((nil "float") (float "float") (int "int")
(color "color_t") (complex "complex float")))))
(defun gen-builtin (overloaded-name name type args body)
(labels ((length-of-arg-pos (pos)
(format nil "arglengths[~A]" pos))
(number-of-arg-pos (pos)
(format nil "argnumbers[~A]" pos))
(lookup-length (l)
(if (var-symbol-p l)
(let ((position (position-if #'(lambda (a) (eq l (cadadr a))) args)))
(length-of-arg-pos position))
l))
(arg-pos (name)
(position-if #'(lambda (a) (eq name (car a))) args))
(lookup-arg (name)
(let ((pos (arg-pos name)))
(if (null pos)
nil
(values #'(lambda (n)
(format nil "args[~A][~A]" pos n))
'tuple
(lookup-length (cadadr (nth pos args)))))))
(lookup-op (op num-args table)
(find-if #'(lambda (o) (and (eq op (first o)) (= num-args (second o)))) table)))
(let ((body (my-macroexpand body `((eval . eval)
(+ . ,#'(lambda (&rest args) (reduce #'(lambda (a b) (list '+ a b)) args)))
(* . ,#'(lambda (&rest args) (reduce #'(lambda (a b) (list '* a b)) args))))))
(result-length (lookup-length (cadr type))))
;; bindings are of the form (?name ?c-name primary nil ?c-type) or
;; (?name ?func tuple ?length ?c-type) or (?name ?c-name counter nil ?c-type)
(labels ((gen (stmt bindings)
(labels ((lookup-var (name)
(let ((var (assoc name bindings)))
(if (null var)
(lookup-arg name)
(values (second var) (third var) (fourth var) (fifth var)))))
(make-allocated (name allocatedp &key type)
(let ((type (if (null type) (rt-type-with-name 'int) type)))
(if allocatedp
""
(format nil "~A = compiler_make_temporary(~A);~%" name
(rt-type-c-define type)))))
;; returns type, length, c-type
(expr-type (expr)
(case-match expr
((nth ?n ?expr)
(multiple-value-bind (type length c-type)
(expr-type expr)
(values 'primary nil c-type)))
((make ?type . ?args)
(values 'tuple (format nil "~A" (length args))))
((splat ?type ?primary)
(multiple-value-bind (prim-type prim-length c-type)
(expr-type primary)
(values 'tuple (lookup-length (cadr type)) c-type)))
((sum ?expr)
'primary)
((internal ?name)
(values 'primary nil 'float))
((make-tuple . ?args)
(values 'primary nil (rt-type-with-name 'tuple)))
((?op . ?args)
(cond ((member op '(and or not))
'primary)
((not (null (lookup-op op (length args) *ops*)))
(expr-type (first args)))
((not (null (lookup-op op (length args) *primops*)))
(let ((op-entry (lookup-op op (length args) *primops*)))
(assert (not (null op-entry)))
(values 'primary nil (fifth op-entry))))
(t
(error "cannot determine type of expr ~A" expr))))
(?val
(cond ((integerp val)
'primary)
((floatp val)
'primary)
((symbolp val)
(multiple-value-bind (name type length c-type)
(lookup-var val)
(if (null name)
(error "variable ~A not bound" val)
(values type length c-type))))
(t
(error "cannot determine type of expr ~A" expr))))))
(expr-decl (expr name)
(multiple-value-bind (type length c-type)
(expr-type expr)
(case type
(primary
(format nil "compvar_t *~A;" name))
(tuple
(format nil "compvar_t *~A[~A];" name length))
(t
(error "cannot declare expr ~A of type ~A" expr type)))))
(gen-op (op-name args type lval allocatedp gen-sub)
(let ((arg-names (mapcar #'(lambda (x) (make-tmp-name)) args)))
(format nil "~A{~%~@[compvar_t ~{*~A~^, ~};~%~]~{~A~}emit_assign(make_lhs(~A), make_op_rhs(~A~{, make_compvar_primary(~A)~}));~%}~%"
(make-allocated lval allocatedp :type type)
arg-names
(mapcar #'(lambda (arg name) (funcall gen-sub arg name nil)) args arg-names)
lval op-name arg-names)))
(gen-primary (expr lval allocatedp)
(case-match expr
((nth ?n ?expr)
(if (symbolp n)
(multiple-value-bind (name type length)
(lookup-var n)
(assert (eq type 'counter))
(gen-expr-nth expr lval allocatedp name))
(progn
(assert (integerp n))
(gen-expr-nth expr lval allocatedp n))))
((sum ?expr)
(multiple-value-bind (type length c-type)
(expr-type expr)
(assert (eq type 'tuple))
(let ((t1 (make-tmp-name))
(t2 (make-tmp-name))
(ctr (make-tmp-name)))
(format nil "if (~A == 1)~%{~%~A~%}~%else~%{~%compvar_t *~A, *~A;~%int ~A;~%~A~A~Aemit_assign(make_lhs(~A), make_op_rhs(OP_ADD, make_compvar_primary(~A), make_compvar_primary(~A)));~%for (~A = 2; ~A < ~A; ++~A)~%{~Aemit_assign(make_lhs(~A), make_op_rhs(OP_ADD, make_compvar_primary(~A), make_compvar_primary(~A)));~%}~%}~%"
length (gen-expr-nth expr lval allocatedp "0")
t1 t2 ctr ;declare t1, t2, ctr
(make-allocated lval allocatedp)
(gen-expr-nth expr t1 nil "0") ;t1 = expr(0)
(gen-expr-nth expr t2 nil "1") ;t2 = expr(1)
lval t1 t2 ;lval = t1 + t2
ctr ctr length ctr ;for (ctr = 0; ctr < length; ++ctr)
(gen-expr-nth expr t1 nil ctr) ;t1 = expr(ctr)
lval lval t1)))) ;lval = lval + t1
((argtag ?val)
(let ((number (number-of-arg-pos (arg-pos val))))
(format nil "~Aemit_assign(make_lhs(~A), make_int_const_rhs(~A));~%"
(make-allocated lval allocatedp) lval number)))
((internal ?name)
(format nil "~Aemit_assign(make_lhs(~A), make_value_rhs(get_internal_value(filter, \"~A\", TRUE)));~%"
(make-allocated lval allocatedp) lval name))
((make-tuple . ?args)
(let ((arg-names (mapcar #'(lambda (x) (make-tmp-name)) args)))
(format nil "~A{~%compvar_t ~{*~A~^, ~};~%~{~A~}emit_assign(make_lhs(~A), make_tuple_rhs(~A~{, make_compvar_primary(~A)~}));~%}~%"
(make-allocated lval allocatedp :type (rt-type-with-name 'tuple))
arg-names
(mapcar #'(lambda (arg name) (gen-primary arg name nil)) args arg-names)
lval (length args) arg-names)))
((?op . ?args)
(let ((op-entry (lookup-op op (length args) *primops*)))
(if (not (null op-entry))
(values (gen-op (third op-entry) args (fifth op-entry) lval allocatedp #'gen-primary)
(fifth op-entry))
(error "unknown primop ~A/~A" op (length args)))))
(pi
(format nil "~Aemit_assign(make_lhs(~A), make_float_const_rhs(M_PI));~%"
(make-allocated lval allocatedp) lval))
(?val
(cond ((integerp val)
(format nil "~Aemit_assign(make_lhs(~A), make_int_const_rhs(~A));~%"
(make-allocated lval allocatedp)
lval val))
((floatp val)
(format nil "~Aemit_assign(make_lhs(~A), make_float_const_rhs(~A));~%"
(make-allocated lval allocatedp)
lval val))
((symbolp val)
(multiple-value-bind (name type length c-type)
(lookup-var val)
(case type
(primary
(values
(if allocatedp
(format nil "emit_assign(make_lhs(~A), make_compvar_rhs(~A));~%"
lval name)
(format nil "~A = ~A;~%"
lval name))
c-type))
(counter
(values
(format nil "~Aemit_assign(make_lhs(~A), make_int_const_rhs(~A));~%"
(make-allocated lval allocatedp)
lval name)
c-type))
(t
(error "expr ~A is not a primary but a ~A" expr type)))))
(t
(error "unknown primary ~A" expr))))
(t
(error "unknown primary ~A" expr))))
(gen-expr-nth (expr lval allocatedp n)
(case-match expr
((make ?type . ?args)
(format nil "switch (~A)~%{~%~{case ~A :~%~Abreak;~%~}default :~%assert(0);~%}~%"
n
(mappend #'(lambda (i p)
(list i (gen-primary p lval allocatedp)))
(integers-upto (length args))
args)))
((splat ?type ?primary)
(gen-primary primary lval allocatedp))
((?op . ?args)
(let ((op-entry (lookup-op op (length args) *ops*)))
(if (not (null op-entry))
(gen-op (third op-entry)
args (fifth op-entry) lval allocatedp
#'(lambda (arg name allocatedp) (gen-expr-nth arg name allocatedp n)))
(error "unknown op ~A" op))))
(?var
(if (symbolp var)
(multiple-value-bind (c-name-or-func type length c-type)
(lookup-var var)
(if (not (null c-name-or-func))
(let ((code (if (stringp c-name-or-func)
c-name-or-func
(funcall c-name-or-func n))))
(values
(if allocatedp
(format nil "emit_assign(make_lhs(~A), make_compvar_rhs(~A));~%" lval code)
(format nil "~A = ~A;~%" lval code))
c-type))
(error "unknown var ~A" var)))
(error "unknown expr ~A" expr)))
(t
(error "unknown expr ~A" expr))))
(gen-condition (expr lval allocatedp)
(case-match expr
((and ?a ?b)
(format nil "~A~Astart_if_cond(make_compvar_rhs(~A));~%~Aswitch_if_branch();~%end_if_cond();~%"
(make-allocated lval allocatedp)
(gen-condition a lval t)
lval
(gen-condition b lval t)))
((or ?a ?b)
(format nil "~A~Astart_if_cond(make_compvar_rhs(~A));~%switch_if_branch();~%~Aend_if_cond();~%"
(make-allocated lval allocatedp)
(gen-condition a lval t)
lval
(gen-condition b lval t)))
((not ?x)
(format nil "~Aemit_assign(make_lhs(~A), make_op_rhs(OP_NOT, make_compvar_primary(~A)));~%"
(gen-condition x lval allocatedp)
lval lval))
((?op . ?args)
(let ((op-entry (lookup-op op (length args) *condops*)))
(if (not (null op-entry))
(gen-op (third op-entry) args (fifth op-entry) lval allocatedp #'gen-primary)
(error "unknown condition op ~A" op))))
(t
(error "unknown condition ~A" expr))))
(gen-expr (expr lval allocatedp length)
(let ((ctr (make-tmp-name)))
(format nil "{~%int ~A;~%for (~A = 0; ~A < ~A; ++~A)~%{~%~A}~%}~%"
ctr ctr ctr length ctr
(gen-expr-nth expr (funcall lval ctr) allocatedp ctr))))
;; returns a list which for each let
;; contains: (?decl ?code ?c-name primary
;; nil ?c-type) for primaries and (?decl
;; ?code ?func tuple ?length ?c-type) for
;; tuples
(gen-let (name val)
(let ((c-name (make-tmp-name)))
(multiple-value-bind (type length c-type)
(expr-type val)
(if (eq type 'primary)
(list (expr-decl val c-name)
(gen-primary val c-name nil)
c-name
'primary
nil
c-type)
(let ((func #'(lambda (n) (format nil "~A[~A]" c-name n))))
(list (expr-decl val c-name)
(gen-expr val func nil length)
func
'tuple
length
c-type)))))))
(case-match stmt
((set result ?rhs)
(gen-expr rhs #'(lambda (n) (format nil "result_tmps[~A]" n)) t result-length))
((set (nth ?n result) ?rhs)
(if (symbolp n)
(multiple-value-bind (name type length c-type)
(lookup-var n)
(assert (eq type 'counter))
(gen-primary rhs (format nil "result_tmps[~A]" name) t))
(progn
(assert (integerp n))
(gen-primary rhs (format nil "result_tmps[~A]" n) t))))
((set ?var ?rhs)
(multiple-value-bind (name type length c-type)
(lookup-var var)
(assert (eq type 'primary))
(gen-primary rhs name t)))
((forarglength ?val ?ctr . ?body)
(let ((c-ctr (make-tmp-name)))
(assert (not (null (arg-pos val))))
(format nil "{~%int ~A;~%for (~A = 0; ~A < ~A; ++~A)~%{~%~{~A~}}~%}~%"
c-ctr
c-ctr c-ctr (length-of-arg-pos (arg-pos val)) c-ctr
(let ((bindings (cons (list ctr c-ctr 'counter nil) bindings)))
(mapcar #'(lambda (s) (gen s bindings)) body)))))
((forget ?primary)
(let ((dummy (make-tmp-name)))
(format nil "{~%~A~%~A}~%"
(expr-decl primary dummy)
(gen-primary primary dummy nil))))
((progn . ?body)
(reduce #'string-concat (mapcar #'(lambda (s) (gen s bindings)) body)))
((if ?condition ?consequent ?alternative)
(let ((condition-name (make-tmp-name)))
(format nil "{~%~A~%~Astart_if_cond(make_compvar_rhs(~A));~%~Aswitch_if_branch();~Aend_if_cond();~%}~%"
(expr-decl condition condition-name) (gen-condition condition condition-name nil)
condition-name
(gen consequent bindings)
(gen alternative bindings))))
((let ?lets . ?body)
(let ((lets (mapcar #'(lambda (l) (cons (car l) (gen-let (car l) (cadr l)))) lets)))
(format nil "{~%~{~A~%~}~%~{~A~%~}~{~A~}}~%"
(mapcar #'second lets)
(mapcar #'third lets)
(let ((bindings (append (mapcar #'(lambda (l) (list (first l) (fourth l) (fifth l) (sixth l) (seventh l))) lets)
bindings)))
(mapcar #'(lambda (s) (gen s bindings)) body)))))
(?
(error "unknown statement ~A" stmt))))))
(format t "static void~%gen_~A (filter_t *filter, compvar_t ***args, int *arglengths, int *argnumbers, compvar_t **result)~%{~%"
(dcs name))
(format t "compvar_t *result_tmps[~A];~%int i;~%for (i = 0; i < ~A; ++i) result_tmps[i] = compiler_make_temporary(result[i]->type);~%"
result-length result-length)
(dolist (stmt body)
(princ (gen stmt nil)))
(format t "for (i = 0; i < ~A; ++i) emit_assign(make_lhs(result[i]), make_compvar_rhs(result_tmps[i]));~%" result-length)
(format t "}~%~%")))))
#|
(defbuiltin "merd" merd (?T ?L) ((val (?T ?L)))
(let ((dummy1 (start-debug-tuple (argtag val))))
(forarglength val i
(let ((dummy2 (set-debug-tuple-data i (nth i val))))
(set (nth i result) (nth i val))))))
|#
(defbuiltin "print" print (nil 1) ((val (? ?)))
"Print a tuple to standard output. Useful for debugging a script."
(forarglength val i
(forget (print (nth i val))))
(forget (newline))
(set result (make (nil 1) 0)))
(defbuiltin "__add" add_ri (ri 2) ((a (ri 2)) (b (ri 2)))
"Addition. Works on real numbers, complex numbers and tuples.
Tuples can be added element-wise or the same real number can be added
to each element of a tuples."
(set result (+v a b)))
(defbuiltin "__add" add_ri_1 (ri 2) ((a (ri 2)) (b (? 1)))
(set result (+v a (make (ri 2) (nth 0 b) 0))))
(defbuiltin "__add" add_1_ri (ri 2) ((a (? 1)) (b (ri 2)))
(set result (+v b (make (ri 2) (nth 0 a) 0))))
(defbuiltin "__add" add_1 (?T 1) ((a (?T 1)) (b (?T 1)))
(set result (+v a b)))
(defbuiltin "__add" add_s (?T ?L) ((a (?T ?L)) (b (? 1)))
(set result (+v a (splat (T L) (nth 0 b)))))
(defbuiltin "__add" add_n (?T ?L) ((a (?T ?L)) (b (?T ?L)))
(set result (+v a b)))
(defbuiltin "__sub" sub_ri (ri 2) ((a (ri 2)) (b (ri 2)))
(set result (-v a b)))
(defbuiltin "__sub" sub_ri_1 (ri 2) ((a (ri 2)) (b (? 1)))
"Subtraction. Works on real numbers, complex numbers and tuples.
One tuple can be subtracted from another element-wise or the same real
number can be subtracted from each element of a tuple."
(set result (-v a (make (ri 2) (nth 0 b) 0))))
(defbuiltin "__sub" sub_1_ri (ri 2) ((a (? 1)) (b (ri 2)))
(set result (-v (make (ri 2) (nth 0 a) 0) b)))
(defbuiltin "__sub" sub_1 (?T 1) ((a (?T 1)) (b (?T 1)))
(set result (-v a b)))
(defbuiltin "__sub" sub_s (?T ?L) ((a (?T ?L)) (b (? 1)))
(set result (-v a (splat (?T ?L) (nth 0 b)))))
(defbuiltin "__sub" sub_n (?T ?L) ((a (?T ?L)) (b (?T ?L)))
(set result (-v a b)))
(defbuiltin "__neg" neg (?T ?L) ((x (?T ?L)))
"Negation."
(set result (-v x)))
(defbuiltin "__mul" mul_ri (ri 2) ((a (ri 2)) (b (ri 2)))
"Multiplication. Works on real numbers, complex numbers,
quaternions, hypercomplex numbers, tuples, vectors and matrices. Two
tuples can be multiplied element-wise or a tuple can be multipled by a
single number for each element. Vectors and matrices can be multipled
in both directions and two matrices can be multipled as well."
(set result (make (ri 2)
(- (* (nth 0 a) (nth 0 b)) (* (nth 1 a) (nth 1 b)))
(+ (* (nth 0 a) (nth 1 b)) (* (nth 0 b) (nth 1 a))))))
(defbuiltin "__mul" mul_1_ri (ri 2) ((a (? 1)) (b (ri 2)))
(set result (*v (splat (ri 2) (nth 0 a)) b)))
(defun matmul (n)
(mappend #'(lambda (i)
(map-times n #'(lambda (j)
`(+ ,@(map-times n #'(lambda (k)
`(* (nth ,(+ (* i n) k) a) (nth ,(+ (* k n) j) b))))))))
(integers-upto n)))
(defbuiltin "__mul" mul_m2x2 (m2x2 4) ((a (m2x2 4)) (b (m2x2 4)))
(set result (eval `(make (m2x2 4) ,@(matmul 2)))))
(defbuiltin "__mul" mul_m3x3 (m3x3 9) ((a (m3x3 9)) (b (m3x3 9)))
(set result (eval `(make (m3x3 9) ,@(matmul 3)))))
(defun vecmatmul (n)
(map-times n #'(lambda (i)
`(+ ,@(map-times n #'(lambda (j)
`(* (nth ,j a) (nth ,(+ i (* n j)) b))))))))
(defbuiltin "__mul" mul_v2m2x2 (v2 2) ((a (v2 2)) (b (m2x2 4)))
(set result (eval `(make (v2 2) ,@(vecmatmul 2)))))
(defbuiltin "__mul" mul_v3m3x3 (v3 3) ((a (v3 3)) (b (m3x3 9)))
(set result (eval `(make (v3 3) ,@(vecmatmul 3)))))
(defun matvecmul (n)
(map-times n #'(lambda (i)
`(+ ,@(map-times n #'(lambda (j)
`(* (nth ,(+ j (* n i)) a) (nth ,j b))))))))
(defbuiltin "__mul" mul_m2x2v2 (v2 2) ((a (m2x2 4)) (b (v2 2)))
(set result (eval `(make (v2 2) ,@(matvecmul 2)))))
(defbuiltin "__mul" mul_m3x3v3 (v3 3) ((a (m3x3 9)) (b (v3 3)))
(set result (eval `(make (v3 3) ,@(matvecmul 3)))))
(defbuiltin "__mul" mul_quat (quat 4) ((a (quat 4)) (b (quat 4)))
(set result (make (quat 4)
(+ (* (nth 0 a) (nth 0 b))
(- (* (nth 1 a) (nth 1 b)))
(- (* (nth 2 a) (nth 2 b)))
(- (* (nth 3 a) (nth 3 b))))
(+ (* (nth 0 a) (nth 1 b))
(* (nth 1 a) (nth 0 b))
(* (nth 2 a) (nth 3 b))
(- (* (nth 3 a) (nth 2 b))))
(+ (* (nth 0 a) (nth 2 b))
(* (nth 2 a) (nth 0 b))
(- (* (nth 1 a) (nth 3 b)))
(* (nth 3 a) (nth 1 b)))
(+ (* (nth 0 a) (nth 3 b))
(* (nth 3 a) (nth 0 b))
(* (nth 1 a) (nth 2 b))
(- (* (nth 2 a) (nth 1 b)))))))
(defbuiltin "__mul" mul_cquat (cquat 4) ((a (cquat 4)) (b (cquat 4)))
(set result (make (cquat 4)
(+ (* (nth 0 a) (nth 0 b))
(- (* (nth 1 a) (nth 1 b)))
(* (nth 2 a) (nth 2 b))
(* (nth 3 a) (nth 3 b)))
(+ (* (nth 0 a) (nth 1 b))
(* (nth 1 a) (nth 0 b))
(* (nth 2 a) (nth 3 b))
(* (nth 3 a) (nth 2 b)))
(+ (* (nth 0 a) (nth 2 b))
(* (nth 2 a) (nth 0 b))
(- (* (nth 1 a) (nth 3 b)))
(- (* (nth 3 a) (nth 1 b))))
(+ (* (nth 0 a) (nth 3 b))
(* (nth 3 a) (nth 0 b))
(- (* (nth 1 a) (nth 2 b)))
(- (* (nth 2 a) (nth 1 b)))))))
(defbuiltin "__mul" mul_hyper (hyper 4) ((a (hyper 4)) (b (hyper 4)))
(set result (make (hyper 4)
(+ (* (nth 0 a) (nth 0 b))
(- (* (nth 1 a) (nth 1 b)))
(- (* (nth 2 a) (nth 2 b)))
(* (nth 3 a) (nth 3 b)))
(+ (* (nth 0 a) (nth 1 b))
(* (nth 1 a) (nth 0 b))
(- (* (nth 2 a) (nth 3 b)))
(- (* (nth 3 a) (nth 2 b))))
(+ (* (nth 0 a) (nth 2 b))
(* (nth 2 a) (nth 0 b))
(- (* (nth 1 a) (nth 3 b)))
(- (* (nth 3 a) (nth 1 b))))
(+ (* (nth 0 a) (nth 3 b))
(* (nth 3 a) (nth 0 b))
(* (nth 1 a) (nth 2 b))
(* (nth 2 a) (nth 1 b))))))
(defbuiltin "__mul" mul_1 (?T 1) ((a (?T 1)) (b (?T 1)))
(set result (make (?T 1) (* (nth 0 a) (nth 0 b)))))
(defbuiltin "__mul" mul_s (?T ?L) ((a (?T ?L)) (b (? 1)))
(set result (*v a (splat (?T ?L) (nth 0 b)))))
(defbuiltin "__mul" mul_n (?T ?L) ((a (?T ?L)) (b (?T ?L)))
(set result (*v a b)))
(defbuiltin "__div" div_ri (ri 2) ((a (ri 2)) (b (ri 2)))
"Division. Works on real numbers, complex numbers, tuples, vectors
and matrices. A tuple can be divided by another element-wise or by
the same number for each element. Vectors can be divided by
matrices."
(if (and (= (nth 0 b) 0) (= (nth 1 b) 0))
(set result (make (ri 2) 0 0))
(let ((c (sum (*v b b))))
(set result (make (ri 2)
(/ (sum (*v a b)) c)
(/ (+ (* (- (nth 0 a)) (nth 1 b))
(* (nth 0 b) (nth 1 a)))
c))))))
(defbuiltin "__div" div_1_ri (ri 2) ((a (?T 1)) (b (ri 2)))
(let ((tmp (sum (*v b b))))
(if (= tmp 0)
(set result (make (ri 2) 0 0))
(set result (make (ri 2)
(/ (* (nth 0 a) (nth 0 b)) tmp)
(- (/ (* (nth 0 a) (nth 1 b)) tmp)))))))
(defbuiltin "__div" div_v2m2x2 (v2 2) ((a (? 2)) (b (m2x2 4)))
(let ((m (make-tuple (nth 0 b) (nth 1 b) (nth 2 b) (nth 3 b)))
(v (make-tuple (nth 0 a) (nth 1 a))))
(let ((r (solve-linear-2 m v)))
(set result (make (v2 2) (tuple-nth r 0) (tuple-nth r 1))))))
(defbuiltin "__div" div_v3m3x3 (v3 3) ((a (? 3)) (b (m3x3 9)))
(let ((m (make-tuple (nth 0 b) (nth 1 b) (nth 2 b)
(nth 3 b) (nth 4 b) (nth 5 b)
(nth 6 b) (nth 7 b) (nth 8 b)))
(v (make-tuple (nth 0 a) (nth 1 a) (nth 2 a))))
(let ((r (solve-linear-3 m v)))
(set result (make (v3 3) (tuple-nth r 0) (tuple-nth r 1) (tuple-nth r 2))))))
(defbuiltin "__div" div_1 (?T 1) ((a (?T 1)) (b (?T 1)))
(if (= (nth 0 b) 0)
(set result (make (?T 1) 0))
(set result (/v a b))))
(defbuiltin "__div" div_s (?T ?L) ((a (?T ?L)) (b (? 1)))
(if (= (nth 0 b) 0)
(set result (splat (?T ?L) 0))
(set result (/v a (splat (?T ?L) (nth 0 b))))))
(defbuiltin "__div" div_n (?T ?L) ((a (?T ?L)) (b (?T ?L)))
(forarglength b i
(if (= (nth i b) 0)
(set (nth i result) 0)
(set (nth i result) (/ (nth i a) (nth i b))))))
(defbuiltin "__mod" mod_1 (?T 1) ((a (?T 1)) (b (?T 1)))
"Remainder. Calculates the remainder of a division. Works on real
numbers and tuples. The remainder can be calculated for two tuples
element-wise or for one tuple and the same number for each element of
the tuple."
(if (= (nth 0 b) 0)
(set result (make (?T 1) 0))
(set result (%v a b))))
(defbuiltin "__mod" mod_s (?T ?L) ((a (?T ?L)) (b (? 1)))
(if (= (nth 0 b) 0)
(set result (splat (?T ?L) 0))
(set result (%v a (splat (?T ?L) (nth 0 b))))))
(defbuiltin "__mod" mod_n (?T ?L) ((a (?T ?L)) (b (?T ?L)))
(forarglength b i
(if (= (nth i b) 0)
(set (nth i result) 0)
(set (nth i result) (% (nth i a) (nth i b))))))
(defbuiltin "pmod" pmod (?T 1) ((a (?T 1)) (b (?T 1)))
"The remainder of a division, made positive if the dividend is
negative by adding the divisor."
(let ((mod (% (nth 0 a) (nth 0 b))))
(if (< (nth 0 a) 0)
(set result (make (?T 1) (+ mod (nth 0 b))))
(set result (make (?T 1) mod)))))
(defbuiltin "sqrt" sqrt_ri (ri 2) ((a (ri 2)))
"The square root of a complex or real number. A real argument must
be positive, otherwise the result will not be definied."
(let ((c (c-sqrt (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "sqrt" sqrt_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (sqrt (nth 0 a)))))
(defbuiltin "sum" sum (nil 1) ((a (?T ?L)))
"The sum of all elements of a tuple."
(set result (make (?T 1) (sum a))))
;;; vector functions
(defbuiltin "dotp" dotp (nil 1) ((a (?T ?L)) (b (?T ?L)))
"Dot product of two tuples/vectors."
(set result (make (nil 1) (sum (*v a b)))))
(defbuiltin "crossp" crossp (?T 3) ((a (?T 3)) (b (?T 3)))
"Cross product of two tuples/vectors with three elements."
(set result (make (?T 3)
(- (* (nth 1 a) (nth 2 b))
(* (nth 2 a) (nth 1 b)))
(- (* (nth 2 a) (nth 0 b))
(* (nth 0 a) (nth 2 b)))
(- (* (nth 0 a) (nth 1 b))
(* (nth 1 a) (nth 0 b))))))
(defbuiltin "det" det_m2x2 (nil 1) ((a (m2x2 4)))
"Determinant of a matrix."
(set result (make (nil 1) (- (* (nth 0 a) (nth 3 a)) (* (nth 1 a) (nth 2 a))))))
(defbuiltin "det" det_m3x3 (nil 1) ((a (m3x3 9)))
(set result (make (nil 1) (- (+ (* (nth 0 a) (nth 4 a) (nth 8 a))
(* (nth 1 a) (nth 5 a) (nth 6 a))
(* (nth 2 a) (nth 3 a) (nth 7 a)))
(+ (* (nth 2 a) (nth 4 a) (nth 6 a))
(* (nth 0 a) (nth 5 a) (nth 7 a))
(* (nth 1 a) (nth 3 a) (nth 8 a)))))))
(defbuiltin "normalize" normalize (?T ?L) ((a (?T ?L)))
"Normalize a vector to Euclidian length 1."
(let ((l (sum (*v a a))))
(if (= l 0)
(set result (splat (?T ?L) 0))
(set result (/v a (splat (?T ?L) (sqrt l)))))))
(defbuiltin "abs" abs_ri (nil 1) ((a (ri 2)))
"Absolute value of real numbers, complex numbers (magnitude),
quaternions, hypercomplex numbers and vectors (Euclidian norm)."
(set result (make (nil 1) (hypot (nth 0 a) (nth 1 a)))))
(defbuiltin "abs" abs_quat (nil 1) ((a (quat 4)))
(set result (make (nil 1) (sqrt (+ (* (nth 0 a) (nth 0 a))
(* (nth 1 a) (nth 1 a))
(* (nth 2 a) (nth 2 a))
(* (nth 3 a) (nth 3 a)))))))
(defbuiltin "abs" abs_cquat (nil 1) ((a (cquat 4)))
(set result (make (nil 1) (sqrt (+ (* (nth 0 a) (nth 0 a))
(* (nth 1 a) (nth 1 a))
(* (nth 2 a) (nth 2 a))
(* (nth 3 a) (nth 3 a)))))))
(defbuiltin "abs" abs_hyper (nil 1) ((a (hyper 4)))
(set result (make (nil 1) (sqrt (+ (* (nth 0 a) (nth 0 a))
(* (nth 1 a) (nth 1 a))
(* (nth 2 a) (nth 2 a))
(* (nth 3 a) (nth 3 a)))))))
(defbuiltin "abs" abs_v2 (nil 1) ((a (v2 2)))
(set result (make (nil 1) (sqrt (+ (* (nth 0 a) (nth 0 a))
(* (nth 1 a) (nth 1 a)))))))
(defbuiltin "abs" abs_v3 (nil 1) ((a (v3 3)))
(set result (make (nil 1) (sqrt (+ (* (nth 0 a) (nth 0 a))
(* (nth 1 a) (nth 1 a))
(* (nth 2 a) (nth 2 a)))))))
(defbuiltin "abs" abs_1 (?T 1) ((a (?T 1)))
(set result (abs-v a)))
(defbuiltin "abs" abs_n (?T ?L) ((a (?T ?L)))
(set result (abs-v a)))
;;; trigonometry
(defbuiltin "deg2rad" deg2rad (nil 1) ((a (? 1)))
"Convert degrees to radians."
(set result (make (nil 1) (* (nth 0 a) 0.017453292519943295722))))
(defbuiltin "rad2deg" rad2deg (deg 1) ((a (? 1)))
"Convert radians to degrees."
(set result (make (deg 1) (* (nth 0 a) 57.2957795130823208768))))
(defbuiltin "sin" sin_ri (ri 2) ((a (ri 2)))
"Sine of real and complex numbers."
(let ((c (c-sin (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "sin" sin (?T 1) ((a (?T 1)))
(set result (make (?T 1) (sin (nth 0 a)))))
(defbuiltin "cos" cos_ri (ri 2) ((a (ri 2)))
"Cosine of real and complex numbers."
(let ((c (c-cos (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "cos" cos (?T 1) ((a (?T 1)))
(set result (make (?T 1) (cos (nth 0 a)))))
(defbuiltin "tan" tan_ri (ri 2) ((a (ri 2)))
"Tangent of real and complex numbers."
(let ((c (c-tan (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "tan" tan (?T 1) ((a (?T 1)))
(set result (make (?T 1) (tan (nth 0 a)))))
(defbuiltin "asin" asin_ri (ri 2) ((a (ri 2)))
"Arcsine of real and complex numbers."
(let ((c (c-asin (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "asin" asin (?T 1) ((a (?T 1)))
(if (or (< (nth 0 a) -1) (< 1 (nth 0 a)))
(set result (make (?T 1) 0))
(set result (make (?T 1) (asin (nth 0 a))))))
(defbuiltin "acos" acos_ri (ri 2) ((a (ri 2)))
"Arccosine of real and complex numbers."
(let ((c (c-acos (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "acos" acos (?T 1) ((a (?T 1)))
(if (or (< (nth 0 a) -1) (< 1 (nth 0 a)))
(set result (make (?T 1) 0))
(set result (make (?T 1) (acos (nth 0 a))))))
(defbuiltin "atan" atan_ri (ri 2) ((a (ri 2)))
"Arctangent of real and complex numbers."
(let ((c (c-atan (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "atan" atan (?T 1) ((a (?T 1)))
(set result (make (?T 1) (atan (nth 0 a)))))
(defbuiltin "atan" atan2 (?T 1) ((y (?T 1)) (x (?T 1)))
"Arctangent of <tt>y/x</tt>, with the signs of the arguments taken
into account to determine the correct quadrant of the result."
(set result (make (?T 1) (atan2 (nth 0 y) (nth 0 x)))))
;;; exp and friends
(defbuiltin "__pow" pow_ri_1 (ri 2) ((a (ri 2)) (b (?T 1)))
"Exponentiation of real and complex numbers and tuples. A tuple can
be exponentiated for each element by a single number."
(let ((c (c-pow (complex (nth 0 a) (nth 1 a)) (complex (nth 0 b) 0.0))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "__pow" pow_ri (ri 2) ((a (ri 2)) (b (ri 2)))
(let ((c (c-pow (complex (nth 0 a) (nth 1 a)) (complex (nth 0 b) (nth 1 b)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "__pow" pow_1_ri (ri 2) ((a (?T 1)) (b (ri 2)))
(let ((c (c-pow (complex (nth 0 a) 0.0) (complex (nth 0 b) (nth 1 b)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "__pow" pow_1 (?T 1) ((a (?T 1)) (b (?T 1)))
(if (and (<= (nth 0 b) 0) (= (nth 0 a) 0))
(set result (make (?T 1) 0))
(set result (make (?T 1) (pow (nth 0 a) (nth 0 b))))))
(defbuiltin "__pow" pow_s (?T ?L) ((a (?T ?L)) (b (? 1)))
(forarglength a i
(if (and (<= (nth 0 b) 0) (= (nth i a) 0))
(set (nth i result) 0)
(set (nth i result) (pow (nth i a) (nth 0 b))))))
(defbuiltin "exp" exp_ri (ri 2) ((a (ri 2)))
"The natural exponential function <b>e^x</b> for real and complex
numbers."
(let ((c (c-exp (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "exp" exp_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (exp (nth 0 a)))))
(defbuiltin "log" log_ri (ri 2) ((a (ri 2)))
"The natural logarithm for real and complex numbers."
(let ((c (c-log (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "log" log_1 (?T 1) ((a (?T 1)))
(if (<= (nth 0 a) 0)
(set result (make (?T 1) 0))
(set result (make (?T 1) (log (nth 0 a))))))
;;; complex
(defbuiltin "arg" arg_ri (nil 1) ((a (ri 2)))
"The argument of a complex number."
(set result (make (nil 1) (c-arg (complex (nth 0 a) (nth 1 a))))))
(defbuiltin "conj" conj_ri (ri 2) ((a (ri 2)))
"The complex conjugate."
(set result (make (ri 2) (nth 0 a) (- (nth 1 a)))))
;;; hyperbolic
(defbuiltin "sinh" sinh_ri (ri 2) ((a (ri 2)))
"Hyperbolic sine of real and complex numbers."
(let ((c (c-sinh (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "sinh" sinh_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (sinh (nth 0 a)))))
(defbuiltin "cosh" cosh_ri (ri 2) ((a (ri 2)))
"Hyperbolic cosine of real and complex numbers."
(let ((c (c-cosh (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "cosh" cosh_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (cosh (nth 0 a)))))
(defbuiltin "tanh" tanh_ri (ri 2) ((a (ri 2)))
"Hyperbolic tangent of real and complex numbers."
(let ((c (c-tanh (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "tanh" tanh_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (tanh (nth 0 a)))))
(defbuiltin "asinh" asinh_ri (ri 2) ((a (ri 2)))
"Hyperbolic arcsine of real and complex numbers."
(let ((c (c-asinh (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "asinh" asinh_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (asinh (nth 0 a)))))
(defbuiltin "acosh" acosh_ri (ri 2) ((a (ri 2)))
"Hyperbolic arccosine of real and complex numbers."
(let ((c (c-acosh (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "acosh" acosh_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (acosh (nth 0 a)))))
(defbuiltin "atanh" atanh_ri (ri 2) ((a (ri 2)))
"Hyperbolic arctangent of real and complex numbers."
(let ((c (c-atanh (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "atanh" atanh_1 (?T 1) ((a (?T 1)))
(set result (make (?T 1) (atanh (nth 0 a)))))
(defbuiltin "gamma" gamma_ri (ri 2) ((a (ri 2)))
"The (logarithm of the) gamma function for real and complex numbers."
(let ((c (c-gamma (complex (nth 0 a) (nth 1 a)))))
(set result (make (ri 2) (c-real c) (c-imag c)))))
(defbuiltin "gamma" gamma_1 (?T 1) ((a (?T 1)))
(if (< (nth 0 a) 0)
(set result (make (?T 1) 0))
(set result (make (?T 1) (gamma (nth 0 a))))))
(defbuiltin "beta" beta_1 (?T 1) ((a (?T 1)) (b (?T 1)))
"The complete beta function for positive real arguments."
(if (or (< (nth 0 a) 0)
(< (nth 0 b) 0))
(set result (make (?T 1) 0))
(set result (make (?T 1) (beta (nth 0 a) (nth 0 b))))))
;;; elliptic
(def-simple-builtin "ell_int_Kcomp" ell_int_Kcomp ell-int-k-comp (k)
"Complete elliptic integral K in Legendre form.")
(def-simple-builtin "ell_int_Ecomp" ell_int_Ecomp ell-int-e-comp (k)
"Complete elliptic integral E in Legendre form.")
(def-simple-builtin "ell_int_F" ell_int_F ell-int-f (phi k)
"Incomplete elliptic integral F in Legendre form.")
(def-simple-builtin "ell_int_E" ell_int_E ell-int-e (phi k)
"Incomplete elliptic integral E in Legendre form.")
(def-simple-builtin "ell_int_P" ell_int_P ell-int-p (phi k n)
"Incomplete elliptic integral P in Legendre form.")
(def-simple-builtin "ell_int_D" ell_int_D ell-int-d (phi k n)
"Incomplete elliptic integral D in Legendre form.")
(def-simple-builtin "ell_int_RC" ell_int_RC ell-int-rc (x y)
"Incomplete elliptic integral RC in Carlson form.")
(def-simple-builtin "ell_int_RD" ell_int_RD ell-int-rd (x y z)
"Incomplete elliptic integral RD in Carlson form.")
(def-simple-builtin "ell_int_RF" ell_int_RF ell-int-rf (x y z)
"Incomplete elliptic integral RF in Carlson form.")
(def-simple-builtin "ell_int_RJ" ell_int_RJ ell-int-rj (x y z p)
"Incomplete elliptic integral RJ in Carlson form.")
(defbuiltin "ell_jac_sn" ell_jac_sn_1 (?T 1) ((u (?T 1)) (m (?T 1)))
"Jacobian elliptic function sn for real and complex arguments."
(let ((v (ell-jac (nth 0 u) (nth 0 m))))
(set result (make (?T 1) (tuple-nth v 0)))))
(defbuiltin "ell_jac_cn" ell_jac_cn_1 (?T 1) ((u (?T 1)) (m (?T 1)))
"Jacobian elliptic function cn for real and complex arguments."
(let ((v (ell-jac (nth 0 u) (nth 0 m))))
(set result (make (?T 1) (tuple-nth v 1)))))
(defbuiltin "ell_jac_dn" ell_jac_dn_1 (?T 1) ((u (?T 1)) (m (?T 1)))
"Jacobian elliptic function dn for real and complex arguments."
(let ((v (ell-jac (nth 0 u) (nth 0 m))))
(set result (make (?T 1) (tuple-nth v 2)))))
(defmacro def-complex-ell-jac (overloaded-name name r-nom i-nom)
`(defbuiltin ,overloaded-name ,name (ri 2) ((u (ri 2)) (m (? 1)))
(let ((v (ell-jac (nth 0 u) (nth 0 m)))
(v1 (ell-jac (nth 1 u) (- 1 (nth 0 m)))))
(let ((s (tuple-nth v 0))
(c (tuple-nth v 1))