-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy patheigenmath.c
18270 lines (15291 loc) · 307 KB
/
eigenmath.c
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
/*
BSD 2-Clause License
Copyright (c) 2024, George Weigt
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <setjmp.h>
#include <math.h>
#include <errno.h>
#define STACKSIZE 100000 // evaluation stack
#define BLOCKSIZE 10000
#define MAXBLOCKS 2000
#define BUCKETSIZE 100
#define STRBUFLEN 1000
#define MAXDIM 24
// MAXBLOCKS * BLOCKSIZE = 20,000,000 atoms
// MAXBLOCKS * BLOCKSIZE * sizeof (struct atom) = 480,000,000 bytes
// Symbolic expressions are built by linking structs of type "atom".
//
// For example, the expression "a b + c" is built like this:
//
// _______ _______ _______ _______
// |CONS | |CONS | |CONS | |SYM |
// |car cdr|--->|car cdr|----------------------------->|car cdr|--->|"nil" |
// |_|_____| |_|_____| |_|_____| |_______|
// | | |
// | | _v_____
// | | |SYM |
// | | |"c" |
// | | |_______|
// | |
// _v_____ _v_____ _______ _______ _______
// |SYM | |CONS | |CONS | |CONS | |SYM |
// |"add" | |car cdr|--->|car cdr|--->|car cdr|--->|"nil" |
// |_______| |_|_____| |_|_____| |_|_____| |_______|
// | | |
// _v_____ _v_____ _v_____
// |SYM | |SYM | |SYM |
// |"mul" | |"a" | |"b" |
// |_______| |_______| |_______|
struct atom {
union {
struct {
struct atom *car;
struct atom *cdr;
} cons;
struct {
char *name;
void (*func)(struct atom *);
} ksym;
struct {
char *name;
uint32_t index;
} usym;
struct {
uint32_t *a; // rational number a over b
uint32_t *b;
} q;
double d;
char *str;
struct tensor *tensor;
struct atom *next;
} u;
uint8_t atomtype, tag, sign;
};
struct tensor {
int ndim;
int dim[MAXDIM];
int nelem;
struct atom *elem[1];
};
// atom types
#define FREEATOM 0
#define CONS 1
#define KSYM 2
#define USYM 3
#define RATIONAL 4
#define DOUBLE 5
#define STR 6
#define TENSOR 7
// symbol table
#define ABS (0 * BUCKETSIZE + 0)
#define ADJ (0 * BUCKETSIZE + 1)
#define AND (0 * BUCKETSIZE + 2)
#define ARCCOS (0 * BUCKETSIZE + 3)
#define ARCCOSH (0 * BUCKETSIZE + 4)
#define ARCSIN (0 * BUCKETSIZE + 5)
#define ARCSINH (0 * BUCKETSIZE + 6)
#define ARCTAN (0 * BUCKETSIZE + 7)
#define ARCTANH (0 * BUCKETSIZE + 8)
#define ARG (0 * BUCKETSIZE + 9)
#define BINDING (1 * BUCKETSIZE + 0)
#define C_UPPER (2 * BUCKETSIZE + 0)
#define C_LOWER (2 * BUCKETSIZE + 1)
#define CEILING (2 * BUCKETSIZE + 2)
#define CHECK (2 * BUCKETSIZE + 3)
#define CIRCEXP (2 * BUCKETSIZE + 4)
#define CLEAR (2 * BUCKETSIZE + 5)
#define CLOCK (2 * BUCKETSIZE + 6)
#define COFACTOR (2 * BUCKETSIZE + 7)
#define CONJ (2 * BUCKETSIZE + 8)
#define CONTRACT (2 * BUCKETSIZE + 9)
#define COS (2 * BUCKETSIZE + 10)
#define COSH (2 * BUCKETSIZE + 11)
#define D_UPPER (3 * BUCKETSIZE + 0)
#define D_LOWER (3 * BUCKETSIZE + 1)
#define DEFINT (3 * BUCKETSIZE + 2)
#define DENOMINATOR (3 * BUCKETSIZE + 3)
#define DERIVATIVE (3 * BUCKETSIZE + 4)
#define DET (3 * BUCKETSIZE + 5)
#define DIM (3 * BUCKETSIZE + 6)
#define DO (3 * BUCKETSIZE + 7)
#define DOT (3 * BUCKETSIZE + 8)
#define DRAW (3 * BUCKETSIZE + 9)
#define EIGENVEC (4 * BUCKETSIZE + 0)
#define ERF (4 * BUCKETSIZE + 1)
#define ERFC (4 * BUCKETSIZE + 2)
#define EVAL (4 * BUCKETSIZE + 3)
#define EXIT (4 * BUCKETSIZE + 4)
#define EXP (4 * BUCKETSIZE + 5)
#define EXPCOS (4 * BUCKETSIZE + 6)
#define EXPCOSH (4 * BUCKETSIZE + 7)
#define EXPFORM (4 * BUCKETSIZE + 8)
#define EXPSIN (4 * BUCKETSIZE + 9)
#define EXPSINH (4 * BUCKETSIZE + 10)
#define EXPTAN (4 * BUCKETSIZE + 11)
#define EXPTANH (4 * BUCKETSIZE + 12)
#define FACTORIAL (5 * BUCKETSIZE + 0)
#define FLOATF (5 * BUCKETSIZE + 1)
#define FLOOR (5 * BUCKETSIZE + 2)
#define FOR (5 * BUCKETSIZE + 3)
#define H_UPPER (7 * BUCKETSIZE + 0)
#define H_LOWER (7 * BUCKETSIZE + 1)
#define HADAMARD (7 * BUCKETSIZE + 2)
#define I_UPPER (8 * BUCKETSIZE + 0)
#define I_LOWER (8 * BUCKETSIZE + 1)
#define IMAG (8 * BUCKETSIZE + 2)
#define INFIXFORM (8 * BUCKETSIZE + 3)
#define INNER (8 * BUCKETSIZE + 4)
#define INTEGRAL (8 * BUCKETSIZE + 5)
#define INV (8 * BUCKETSIZE + 6)
#define J_UPPER (9 * BUCKETSIZE + 0)
#define J_LOWER (9 * BUCKETSIZE + 1)
#define KRONECKER (10 * BUCKETSIZE + 0)
#define LAST (11 * BUCKETSIZE + 0)
#define LOG (11 * BUCKETSIZE + 1)
#define MAG (12 * BUCKETSIZE + 0)
#define MINOR (12 * BUCKETSIZE + 1)
#define MINORMATRIX (12 * BUCKETSIZE + 2)
#define MOD (12 * BUCKETSIZE + 3)
#define NIL (13 * BUCKETSIZE + 0)
#define NOEXPAND (13 * BUCKETSIZE + 1)
#define NOT (13 * BUCKETSIZE + 2)
#define NROOTS (13 * BUCKETSIZE + 3)
#define NUMBER (13 * BUCKETSIZE + 4)
#define NUMERATOR (13 * BUCKETSIZE + 5)
#define OR (14 * BUCKETSIZE + 0)
#define OUTER (14 * BUCKETSIZE + 1)
#define P_UPPER (15 * BUCKETSIZE + 0)
#define P_LOWER (15 * BUCKETSIZE + 1)
#define PI (15 * BUCKETSIZE + 2)
#define POLAR (15 * BUCKETSIZE + 3)
#define PREFIXFORM (15 * BUCKETSIZE + 4)
#define PRINT (15 * BUCKETSIZE + 5)
#define PRODUCT (15 * BUCKETSIZE + 6)
#define Q_UPPER (16 * BUCKETSIZE + 0)
#define Q_LOWER (16 * BUCKETSIZE + 1)
#define QUOTE (16 * BUCKETSIZE + 2)
#define R_UPPER (17 * BUCKETSIZE + 0)
#define R_LOWER (17 * BUCKETSIZE + 1)
#define RANK (17 * BUCKETSIZE + 2)
#define RATIONALIZE (17 * BUCKETSIZE + 3)
#define REAL (17 * BUCKETSIZE + 4)
#define RECTF (17 * BUCKETSIZE + 5)
#define ROOTS (17 * BUCKETSIZE + 6)
#define ROTATE (17 * BUCKETSIZE + 7)
#define RUN (17 * BUCKETSIZE + 8)
#define S_UPPER (18 * BUCKETSIZE + 0)
#define S_LOWER (18 * BUCKETSIZE + 1)
#define SGN (18 * BUCKETSIZE + 2)
#define SIMPLIFY (18 * BUCKETSIZE + 3)
#define SIN (18 * BUCKETSIZE + 4)
#define SINH (18 * BUCKETSIZE + 5)
#define SQRT (18 * BUCKETSIZE + 6)
#define STATUS (18 * BUCKETSIZE + 7)
#define STOP (18 * BUCKETSIZE + 8)
#define SUBST (18 * BUCKETSIZE + 9)
#define SUM (18 * BUCKETSIZE + 10)
#define T_UPPER (19 * BUCKETSIZE + 0)
#define T_LOWER (19 * BUCKETSIZE + 1)
#define TAN (19 * BUCKETSIZE + 2)
#define TANH (19 * BUCKETSIZE + 3)
#define TAYLOR (19 * BUCKETSIZE + 4)
#define TEST (19 * BUCKETSIZE + 5)
#define TESTEQ (19 * BUCKETSIZE + 6)
#define TESTGE (19 * BUCKETSIZE + 7)
#define TESTGT (19 * BUCKETSIZE + 8)
#define TESTLE (19 * BUCKETSIZE + 9)
#define TESTLT (19 * BUCKETSIZE + 10)
#define TRACE (19 * BUCKETSIZE + 11)
#define TRANSPOSE (19 * BUCKETSIZE + 12)
#define TTY (19 * BUCKETSIZE + 13)
#define U_UPPER (20 * BUCKETSIZE + 0)
#define U_LOWER (20 * BUCKETSIZE + 1)
#define UNIT (20 * BUCKETSIZE + 2)
#define V_UPPER (21 * BUCKETSIZE + 0)
#define V_LOWER (21 * BUCKETSIZE + 1)
#define W_UPPER (22 * BUCKETSIZE + 0)
#define W_LOWER (22 * BUCKETSIZE + 1)
#define X_UPPER (23 * BUCKETSIZE + 0)
#define X_LOWER (23 * BUCKETSIZE + 1)
#define Y_UPPER (24 * BUCKETSIZE + 0)
#define Y_LOWER (24 * BUCKETSIZE + 1)
#define Z_UPPER (25 * BUCKETSIZE + 0)
#define Z_LOWER (25 * BUCKETSIZE + 1)
#define ZERO (25 * BUCKETSIZE + 2)
#define ADD (26 * BUCKETSIZE + 0)
#define MULTIPLY (26 * BUCKETSIZE + 1)
#define POWER (26 * BUCKETSIZE + 2)
#define INDEX (26 * BUCKETSIZE + 3)
#define SETQ (26 * BUCKETSIZE + 4)
#define EXP1 (26 * BUCKETSIZE + 5)
#define SA (26 * BUCKETSIZE + 6)
#define SB (26 * BUCKETSIZE + 7)
#define SX (26 * BUCKETSIZE + 8)
#define ARG1 (26 * BUCKETSIZE + 9)
#define ARG2 (26 * BUCKETSIZE + 10)
#define ARG3 (26 * BUCKETSIZE + 11)
#define ARG4 (26 * BUCKETSIZE + 12)
#define ARG5 (26 * BUCKETSIZE + 13)
#define ARG6 (26 * BUCKETSIZE + 14)
#define ARG7 (26 * BUCKETSIZE + 15)
#define ARG8 (26 * BUCKETSIZE + 16)
#define ARG9 (26 * BUCKETSIZE + 17)
#define symbol(x) symtab[x]
#define push_symbol(x) push(symbol(x))
#define iscons(p) ((p)->atomtype == CONS)
#define isrational(p) ((p)->atomtype == RATIONAL)
#define isdouble(p) ((p)->atomtype == DOUBLE)
#define isnum(p) (isrational(p) || isdouble(p))
#define isstr(p) ((p)->atomtype == STR)
#define istensor(p) ((p)->atomtype == TENSOR)
#define iskeyword(p) ((p)->atomtype == KSYM)
#define isusersymbol(p) ((p)->atomtype == USYM)
#define issymbol(p) (iskeyword(p) || isusersymbol(p))
#define equal(p1, p2) (cmp(p1, p2) == 0)
#define lessp(p1, p2) (cmp(p1, p2) < 0)
#define car(p) (iscons(p) ? (p)->u.cons.car : symbol(NIL))
#define cdr(p) (iscons(p) ? (p)->u.cons.cdr : symbol(NIL))
#define caar(p) car(car(p))
#define cadr(p) car(cdr(p))
#define cdar(p) cdr(car(p))
#define cddr(p) cdr(cdr(p))
#define caadr(p) car(car(cdr(p)))
#define caddr(p) car(cdr(cdr(p)))
#define cadar(p) car(cdr(car(p)))
#define cdadr(p) cdr(car(cdr(p)))
#define cddar(p) cdr(cdr(car(p)))
#define cdddr(p) cdr(cdr(cdr(p)))
#define caaddr(p) car(car(cdr(cdr(p))))
#define cadadr(p) car(cdr(car(cdr(p))))
#define caddar(p) car(cdr(cdr(car(p))))
#define cadddr(p) car(cdr(cdr(cdr(p))))
#define cdaddr(p) cdr(car(cdr(cdr(p))))
#define cddadr(p) cdr(cdr(car(cdr(p))))
#define cddddr(p) cdr(cdr(cdr(cdr(p))))
#define caddddr(p) car(cdr(cdr(cdr(cdr(p)))))
#define cadaddr(p) car(cdr(car(cdr(cdr(p)))))
#define cddaddr(p) cdr(cdr(car(cdr(cdr(p)))))
#define caddadr(p) car(cdr(cdr(car(cdr(p)))))
#define cdddaddr(p) cdr(cdr(cdr(car(cdr(cdr(p))))))
#define caddaddr(p) car(cdr(cdr(car(cdr(cdr(p))))))
#define MPLUS 0
#define MMINUS 1
#define MLENGTH(p) (((int *) (p))[-1])
#define MZERO(p) (MLENGTH(p) == 1 && (p)[0] == 0)
#define MEQUAL(p, n) (MLENGTH(p) == 1 && (p)[0] == (n))
#define BLACK 0
#define BLUE 1
#define RED 2
#define Trace fprintf(stderr, "%s %d\n", __func__, __LINE__);
extern struct atom *mem[MAXBLOCKS]; // an array of pointers
extern struct atom *free_list;
extern int tos; // top of stack
extern struct atom *stack[STACKSIZE];
extern struct atom *symtab[27 * BUCKETSIZE];
extern struct atom *binding[27 * BUCKETSIZE];
extern struct atom *usrfunc[27 * BUCKETSIZE];
extern struct atom *zero;
extern struct atom *one;
extern struct atom *minusone;
extern struct atom *imaginaryunit;
extern int eval_level;
extern int gc_level;
extern int expanding;
extern int drawing;
extern int nonstop;
extern int interrupt;
extern jmp_buf jmpbuf0;
extern jmp_buf jmpbuf1;
extern char *trace1;
extern char *trace2;
extern int alloc_count;
extern int block_count;
extern int free_count;
extern int gc_count;
extern int bignum_count;
extern int ksym_count;
extern int usym_count;
extern int string_count;
extern int tensor_count;
extern int max_eval_level;
extern int max_tos;
extern int max_tof;
extern char strbuf[];
extern char *outbuf;
extern int outbuf_index;
extern int outbuf_length;
struct atom * alloc_atom(void);
void alloc_block(void);
struct atom * alloc_vector(int nrow);
struct atom * alloc_matrix(int nrow, int ncol);
struct atom * alloc_tensor(int nelem);
struct atom * alloc_str(void);
void * alloc_mem(int n);
double mfloat(uint32_t *p);
void msetbit(uint32_t *x, uint32_t k);
void mclrbit(uint32_t *x, uint32_t k);
uint32_t * mscan(char *s);
char * mstr(uint32_t *u);
int mdivby1billion(uint32_t *u);
uint32_t * madd(uint32_t *u, uint32_t *v);
uint32_t * msub(uint32_t *u, uint32_t *v);
uint32_t * mmul(uint32_t *u, uint32_t *v);
uint32_t * mdiv(uint32_t *u, uint32_t *v);
uint32_t * mmod(uint32_t *u, uint32_t *v);
uint32_t * mpow(uint32_t *u, uint32_t *v);
void mshr(uint32_t *u);
int mcmp(uint32_t *u, uint32_t *v);
int meq(uint32_t *u, uint32_t *v);
uint32_t * mint(uint32_t n);
uint32_t * mnew(int n);
void mfree(uint32_t *u);
uint32_t * mcopy(uint32_t *u);
void mnorm(uint32_t *u);
uint32_t * mgcd(uint32_t *u, uint32_t *v);
uint32_t * mroot(uint32_t *a, uint32_t *n);
void list(int n);
void cons(void);
int lengthf(struct atom *p);
int findf(struct atom *p, struct atom *q);
void sort(int n);
int sort_func(const void *p1, const void *p2);
int cmp(struct atom *p1, struct atom *p2);
int cmp_numbers(struct atom *p1, struct atom *p2);
int cmp_rationals(struct atom *a, struct atom *b);
int cmp_tensors(struct atom *p1, struct atom *p2);
int find_denominator(struct atom *p);
int count_denominators(struct atom *p);
int count_numerators(struct atom *p);
void evalg(void);
void evalf(void);
void evalf_nib(struct atom *p1);
void evalp(void);
void eval_abs(struct atom *p1);
void absfunc(void);
void eval_add(struct atom *p1);
void add(void);
void add_terms(int n);
void flatten_terms(int h);
struct atom * combine_tensors(int h);
void add_tensors(void);
void combine_terms(int h);
int combine_terms_nib(int i);
void sort_terms(int h);
int sort_terms_func(const void *q1, const void *q2);
int cmp_terms(struct atom *p1, struct atom *p2);
void normalize_terms(int h);
void add_numbers(struct atom *p1, struct atom *p2);
void add_rationals(struct atom *p1, struct atom *p2);
void add_integers(struct atom *p1, struct atom *p2);
void subtract(void);
void eval_adj(struct atom *p1);
void adj(void);
void eval_and(struct atom *p1);
void eval_arccos(struct atom *p1);
void arccos(void);
void eval_arccosh(struct atom *p1);
void arccosh(void);
void eval_arcsin(struct atom *p1);
void arcsin(void);
void eval_arcsinh(struct atom *p1);
void arcsinh(void);
void eval_arctan(struct atom *p1);
void arctan(void);
void arctan_numbers(struct atom *X, struct atom *Y);
void eval_arctanh(struct atom *p1);
void arctanh(void);
void eval_arg(struct atom *p1);
void argfunc(void);
void arg_nib(void);
void eval_binding(struct atom *p1);
void eval_ceiling(struct atom *p1);
void ceilingfunc(void);
void eval_check(struct atom *p1);
void eval_clear(struct atom *p1);
void eval_clock(struct atom *p1);
void clockfunc(void);
void eval_cofactor(struct atom *p1);
void eval_conj(struct atom *p1);
void conjfunc(void);
void conjfunc_subst(void);
void eval_contract(struct atom *p1);
void contract(void);
void eval_cos(struct atom *p1);
void cosfunc(void);
void cosfunc_sum(struct atom *p1);
void eval_cosh(struct atom *p1);
void coshfunc(void);
void eval_defint(struct atom *p1);
void eval_denominator(struct atom *p1);
void denominator(void);
void eval_derivative(struct atom *p1);
void derivative(void);
void d_scalar_scalar(struct atom *F, struct atom *X);
void dsum(struct atom *p1, struct atom *p2);
void dproduct(struct atom *p1, struct atom *p2);
void dpower(struct atom *F, struct atom *X);
void dlog(struct atom *p1, struct atom *p2);
void dd(struct atom *p1, struct atom *p2);
void dfunction(struct atom *p1, struct atom *p2);
void dsin(struct atom *p1, struct atom *p2);
void dcos(struct atom *p1, struct atom *p2);
void dtan(struct atom *p1, struct atom *p2);
void darcsin(struct atom *p1, struct atom *p2);
void darccos(struct atom *p1, struct atom *p2);
void darctan(struct atom *p1, struct atom *p2);
void dsinh(struct atom *p1, struct atom *p2);
void dcosh(struct atom *p1, struct atom *p2);
void dtanh(struct atom *p1, struct atom *p2);
void darcsinh(struct atom *p1, struct atom *p2);
void darccosh(struct atom *p1, struct atom *p2);
void darctanh(struct atom *p1, struct atom *p2);
void derf(struct atom *p1, struct atom *p2);
void derfc(struct atom *p1, struct atom *p2);
void d_tensor_tensor(struct atom *p1, struct atom *p2);
void d_scalar_tensor(struct atom *p1, struct atom *p2);
void d_tensor_scalar(struct atom *p1, struct atom *p2);
void eval_det(struct atom *p1);
void det(void);
void eval_dim(struct atom *p1);
void eval_do(struct atom *p1);
void eval_eigenvec(struct atom *p1);
void eigenvec(double *D, double *Q, int n);
int eigenvec_step(double *D, double *Q, int n);
void eigenvec_step_nib(double *D, double *Q, int n, int p, int q);
void eval_erf(struct atom *p1);
void erffunc(void);
void eval_erfc(struct atom *p1);
void erfcfunc(void);
void eval_eval(struct atom *p1);
void asubst(void);
int addcmp(struct atom *p1, struct atom *p2);
int mulcmp(struct atom *p1, struct atom *p2);
int expcmp(struct atom *p1, struct atom *p2);
void eval_exp(struct atom *p1);
void expfunc(void);
void eval_expcos(struct atom *p1);
void expcos(void);
void eval_expcosh(struct atom *p1);
void expcosh(void);
void eval_expform(struct atom *p1);
void expform(void);
void eval_expsin(struct atom *p1);
void expsin(void);
void eval_expsinh(struct atom *p1);
void expsinh(void);
void eval_exptan(struct atom *p1);
void exptan(void);
void eval_exptanh(struct atom *p1);
void exptanh(void);
void eval_factorial(struct atom *p1);
void factorial(void);
void eval_float(struct atom *p1);
void floatfunc(void);
void floatfunc_subst(void);
void eval_floor(struct atom *p1);
void floorfunc(void);
void eval_for(struct atom *p1);
void eval_hadamard(struct atom *p1);
void hadamard(void);
void eval_imag(struct atom *p1);
void imag(void);
void eval_index(struct atom *p1);
void indexfunc(struct atom *T, int h);
void eval_infixform(struct atom *p1);
void print_infixform(struct atom *p);
void infixform_subexpr(struct atom *p);
void infixform_expr(struct atom *p);
void infixform_expr_nib(struct atom *p);
void infixform_term(struct atom *p);
void infixform_term_nib(struct atom *p);
void infixform_numerators(struct atom *p);
void infixform_denominators(struct atom *p);
void infixform_factor(struct atom *p);
void infixform_power(struct atom *p);
void infixform_reciprocal(struct atom *p);
void infixform_factorial(struct atom *p);
void infixform_index(struct atom *p);
void infixform_arglist(struct atom *p);
void infixform_rational(struct atom *p);
void infixform_double(struct atom *p);
void infixform_base(struct atom *p);
void infixform_numeric_base(struct atom *p);
void infixform_numeric_exponent(struct atom *p);
void infixform_tensor(struct atom *p);
void infixform_tensor_nib(struct atom *p, int d, int k);
void eval_inner(struct atom *p1);
void inner(void);
void eval_integral(struct atom *p1);
void integral(void);
void integral_nib(struct atom *F, struct atom *X);
void integral_lookup(int h, struct atom *F);
int integral_classify(struct atom *p);
int integral_search(int h, struct atom *F, char **table, int n);
int integral_search_nib(int h, struct atom *F, struct atom *I, struct atom *C);
void decomp(void);
void decomp_sum(struct atom *F, struct atom *X);
void decomp_product(struct atom *F, struct atom *X);
void partition_term(void);
void eval_inv(struct atom *p1);
void inv(void);
void eval_kronecker(struct atom *p1);
void kronecker(void);
void eval_log(struct atom *p1);
void logfunc(void);
void eval_mag(struct atom *p1);
void magfunc(void);
void magfunc_nib(void);
void eval_minor(struct atom *p1);
void eval_minormatrix(struct atom *p1);
void minormatrix(int row, int col);
void eval_mod(struct atom *p1);
void modfunc(void);
void mod_rationals(struct atom *p1, struct atom *p2);
void mod_integers(struct atom *p1, struct atom *p2);
void eval_multiply(struct atom *p1);
void multiply(void);
void multiply_factors(int n);
void flatten_factors(int h);
struct atom * multiply_tensor_factors(int h);
void multiply_scalar_factors(int h);
struct atom * combine_numerical_factors(int h, struct atom *COEF);
void combine_factors(int h);
int combine_factors_nib(int i, int j);
void sort_factors_provisional(int h);
int sort_factors_provisional_func(const void *q1, const void *q2);
int cmp_factors_provisional(struct atom *p1, struct atom *p2);
void normalize_power_factors(int h);
void expand_sum_factors(int h);
void sort_factors(int n);
int sort_factors_func(const void *q1, const void *q2);
int cmp_factors(struct atom *p1, struct atom *p2);
int order_factor(struct atom *p);
void multiply_numbers(struct atom *p1, struct atom *p2);
void multiply_rationals(struct atom *p1, struct atom *p2);
struct atom * reduce_radical_factors(int h, struct atom *COEF);
int any_radical_factors(int h);
struct atom * reduce_radical_double(int h, struct atom *COEF);
struct atom * reduce_radical_rational(int h, struct atom *COEF);
void multiply_expand(void);
void multiply_noexpand(void);
void negate(void);
void reciprocate(void);
void divide(void);
void eval_nil(struct atom *p1);
void eval_noexpand(struct atom *p1);
void eval_not(struct atom *p1);
void eval_nroots(struct atom *p1);
void nroots(void);
void nfindroot(double cr[], double ci[], int n, double *par, double *pai);
void fata(double cr[], double ci[], int n, double ar, double ai, double *far, double *fai);
void nreduce(double cr[], double ci[], int n, double ar, double ai);
double zabs(double r, double i);
double urandom(void);
void eval_number(struct atom *p1);
void eval_numerator(struct atom *p1);
void numerator(void);
void eval_or(struct atom *p1);
void eval_outer(struct atom *p1);
void outer(void);
void eval_polar(struct atom *p1);
void polar(void);
void eval_power(struct atom *p1);
void power(void);
void power_numbers(struct atom *BASE, struct atom *EXPO);
void power_numbers_factor(struct atom *BASE, struct atom *EXPO);
void power_double(struct atom *BASE, struct atom *EXPO);
void power_minusone(struct atom *EXPO);
void normalize_clock_rational(struct atom *EXPO);
void normalize_clock_double(struct atom *EXPO);
void power_natural_number(struct atom *EXPO);
void normalize_polar(struct atom *EXPO);
void normalize_polar_term(struct atom *EXPO);
void normalize_polar_term_rational(struct atom *R);
void normalize_polar_term_double(struct atom *R);
void power_sum(struct atom *BASE, struct atom *EXPO);
void power_complex_number(struct atom *BASE, struct atom *EXPO);
void power_complex_plus(struct atom *X, struct atom *Y, int n);
void power_complex_minus(struct atom *X, struct atom *Y, int n);
void power_complex_double(struct atom *X, struct atom *Y, struct atom *EXPO);
void power_complex_rational(struct atom *X, struct atom *Y, struct atom *EXPO);
void eval_prefixform(struct atom *p1);
void print_prefixform(struct atom *p);
void prefixform(struct atom *p);
void eval_print(struct atom *p1);
void print_result(void);
int annotate_result(struct atom *p1, struct atom *p2);
void eval_product(struct atom *p1);
void eval_quote(struct atom *p1);
void eval_rank(struct atom *p1);
void eval_rationalize(struct atom *p1);
void rationalize(void);
void eval_real(struct atom *p1);
void real(void);
void eval_rect(struct atom *p1);
void rect(void);
void eval_roots(struct atom *p1);
void roots(void);
int findroot(int h, int n);
void horner(int h, int n, struct atom *A);
void divisors(int n);
void divisors_nib(int h, int k);
void reduce(int h, int n, struct atom *A);
void coeffs(struct atom *P, struct atom *X);
void eval_rotate(struct atom *p1);
void rotate_h(struct atom *PSI, uint32_t c, int n);
void rotate_p(struct atom *PSI, struct atom *PHASE, uint32_t c, int n);
void rotate_w(struct atom *PSI, uint32_t c, int m, int n);
void rotate_x(struct atom *PSI, uint32_t c, int n);
void rotate_y(struct atom *PSI, uint32_t c, int n);
void rotate_z(struct atom *PSI, uint32_t c, int n);
void rotate_q(struct atom *PSI, int n);
void rotate_v(struct atom *PSI, int n);
void eval_run(struct atom *p1);
char * read_file(char *filename);
void eval_setq(struct atom *p1);
void setq_indexed(struct atom *p1);
void set_component(struct atom *LVAL, struct atom *RVAL, int h);
void setq_usrfunc(struct atom *p1);
void convert_body(struct atom *A);
void eval_sgn(struct atom *p1);
void sgn(void);
void eval_simplify(struct atom *p1);
void simplify(void);
void simplify_nib(void);
void simplify_trig(void);
int simpler(struct atom *p1, struct atom *p2);
int diameter(struct atom *p);
int mass(struct atom *p);
void eval_sin(struct atom *p1);
void sinfunc(void);
void sinfunc_sum(struct atom *p1);
void eval_sinh(struct atom *p1);
void sinhfunc(void);
void eval_sqrt(struct atom *p1);
void sqrtfunc(void);
void eval_status(struct atom *p1);
void eval_stop(struct atom *p1);
void eval_subst(struct atom *p1);
void subst(void);
void eval_sum(struct atom *p1);
void eval_tan(struct atom *p1);
void tanfunc(void);
void tanfunc_sum(struct atom *p1);
void eval_tanh(struct atom *p1);
void tanhfunc(void);
void eval_taylor(struct atom *p1);
void eval_tensor(struct atom *p1);
void promote_tensor(void);
int compatible_dimensions(struct atom *p, struct atom *q);
struct atom * copy_tensor(struct atom *p1);
void eval_test(struct atom *p1);
void eval_testeq(struct atom *p1);
void eval_testge(struct atom *p1);
void eval_testgt(struct atom *p1);
void eval_testle(struct atom *p1);
void eval_testlt(struct atom *p1);
int cmp_args(struct atom *p1);
void eval_transpose(struct atom *p1);
void transpose(int n, int m);
void eval_unit(struct atom *p1);
void eval_user_function(struct atom *p1);
void eval_user_symbol(struct atom *p1);
void eval_zero(struct atom *p1);
void factor_factor(void);
void factor_bignum(uint32_t *N, struct atom *M);
void factor_int(int n);
void fmt(void);
void fmt_args(struct atom *p);
void fmt_base(struct atom *p);
void fmt_denominators(struct atom *p);
void fmt_double(struct atom *p);
void fmt_exponent(struct atom *p);
void fmt_expr(struct atom *p);
void fmt_expr_nib(struct atom *p);
void fmt_factor(struct atom *p);
void fmt_frac(struct atom *p);
void fmt_function(struct atom *p);
void fmt_indices(struct atom *p);
void fmt_infix_operator(int c);
void fmt_list(struct atom *p);
void fmt_matrix(struct atom *p, int d, int k);
void fmt_numerators(struct atom *p);
void fmt_numeric_exponent(struct atom *p);
void fmt_power(struct atom *p);
void fmt_rational(struct atom *p);
void fmt_reciprocal(struct atom *p);
void fmt_roman_char(int c);
void fmt_roman_string(char *s);
void fmt_space(void);
void fmt_string(struct atom *p);
void fmt_subexpr(struct atom *p);
void fmt_symbol(struct atom *p);
int fmt_symbol_fragment(char *s, int k);
void fmt_table(int x, int y, struct atom *p);
void fmt_tensor(struct atom *p);
void fmt_term(struct atom *p);
void fmt_term_nib(struct atom *p);
void fmt_update_fraction(void);
void fmt_update_list(int t);
void fmt_update_subexpr(void);
void fmt_update_subscript(void);
void fmt_update_superscript(void);
void fmt_update_table(int n, int m);
void fmt_vector(struct atom *p);
void fmt_draw(int x, int y, struct atom *p);
void fmt_draw_char(int x, int y, int c);
void fmt_draw_delims(int x, int y, int h, int d, int w);
void fmt_draw_ldelim(int x, int y, int h, int d);
void fmt_draw_rdelim(int x, int y, int h, int d);
void fmt_draw_table(int x, int y, struct atom *p);
void fmt_putw(uint32_t w);
void gc(void);
void untag(struct atom *p);
int main(int argc, char *argv[]);
void run_infile(char *infile);
void run_stdin(void);
void display(void);
void printbuf(char *s, int color);
void eval_draw(struct atom *p1);
void eval_exit(struct atom *p1);
void numden(void);
int numden_find_divisor(struct atom *p);
int numden_find_divisor_term(struct atom *p);
int numden_find_divisor_factor(struct atom *p);
void numden_cancel_factor(void);
void outbuf_init(void);
void outbuf_puts(char *s);
void outbuf_putc(int c);
int iszero(struct atom *p);
int isequaln(struct atom *p, int n);
int isequalq(struct atom *p, int a, int b);
int isplusone(struct atom *p);
int isminusone(struct atom *p);
int isinteger(struct atom *p);
int isfraction(struct atom *p);
int isposint(struct atom *p);
int isexponential(struct atom *p);
int isradicalterm(struct atom *p);
int isradical(struct atom *p);
int isnegativeterm(struct atom *p);
int isnegativenumber(struct atom *p);
int isimaginaryterm(struct atom *p);
int isimaginaryfactor(struct atom *p);
int iscomplexnumber(struct atom *p);
int isimaginarynumber(struct atom *p);
int isimaginaryunit(struct atom *p);
int isoneoversqrttwo(struct atom *p);
int isminusoneoversqrttwo(struct atom *p);
int isdoublez(struct atom *p);
int isdenominator(struct atom *p);
int isnumerator(struct atom *p);
int hasdouble(struct atom *p);
int isdenormalpolar(struct atom *p);
int isdenormalpolarterm(struct atom *p);
int issquarematrix(struct atom *p);
int issmallinteger(struct atom *p);
void run(char *buf);
void run_buf(char *buf);
char * scan_input(char *s);
void print_trace(int color);
void run_init_script(void);
void stopf(char *s);
void exitf(char *s);
char * scan(char *s);
char * scan1(char *s);
char * scan_nib(char *s);
void scan_stmt(void);
void scan_comparison(void);
void scan_expression(void);
int another_factor_pending(void);
void scan_term(void);
void scan_power(void);
void scan_factor(void);
void scan_symbol(void);
void scan_string(void);
void scan_function_call(void);
void scan_integer(void);
void scan_subexpr(void);
void get_token_skip_newlines(void);
void get_token(void);
void get_token_nib(void);
void update_token_buf(char *a, char *b);
void scan_error(char *errmsg);
void static_negate(void);
void static_reciprocate(void);
void push(struct atom *p);
struct atom * pop(void);
void save_symbol(struct atom *p);
void restore_symbol(void);
void dupl(void);
void swap(void);
void push_integer(int n);
void push_rational(int a, int b);
void push_bignum(int sign, uint32_t *a, uint32_t *b);
int pop_integer(void);
void push_double(double d);
double pop_double(void);
void push_string(char *s);
void slice(int h, int n);
struct atom * lookup(char *s);
char * printname(struct atom *p);
void set_symbol(struct atom *p1, struct atom *p2, struct atom *p3);
struct atom * get_binding(struct atom *p1);
struct atom * get_usrfunc(struct atom *p);
void init_symbol_table(void);
struct atom *
alloc_atom(void)
{
struct atom *p;
if (free_count == 0)
alloc_block();
p = free_list;
free_list = p->u.next;
free_count--;
alloc_count++;
return p;
}
void
alloc_block(void)
{
int i;
struct atom *p;
if (block_count == MAXBLOCKS) {
tos = 0;
gc(); // prep for next run
exitf("out of memory");
}
p = alloc_mem(BLOCKSIZE * sizeof (struct atom));
mem[block_count++] = p;
for (i = 0; i < BLOCKSIZE - 1; i++) {
p[i].atomtype = FREEATOM;
p[i].u.next = p + i + 1;
}
p[i].atomtype = FREEATOM;
p[i].u.next = NULL;
free_list = p;
free_count = BLOCKSIZE;
}
struct atom *
alloc_vector(int nrow)
{
struct atom *p = alloc_tensor(nrow);
p->u.tensor->ndim = 1;
p->u.tensor->dim[0] = nrow;
return p;
}
struct atom *
alloc_matrix(int nrow, int ncol)
{
struct atom *p = alloc_tensor(nrow * ncol);
p->u.tensor->ndim = 2;
p->u.tensor->dim[0] = nrow;
p->u.tensor->dim[1] = ncol;
return p;
}
struct atom *
alloc_tensor(int nelem)
{
int i;
struct atom *p;
struct tensor *t;
p = alloc_atom();
t = alloc_mem(sizeof (struct tensor) + nelem * sizeof (struct atom *));
p->atomtype = TENSOR;
p->u.tensor = t;
t->nelem = nelem;
for (i = 0; i < nelem; i++)
t->elem[i] = zero;
tensor_count++;
return p;
}
struct atom *
alloc_str(void)
{
struct atom *p;
p = alloc_atom();
p->atomtype = STR;
p->u.str = NULL;
string_count++;
return p;
}
void *
alloc_mem(int n)