-
Notifications
You must be signed in to change notification settings - Fork 0
/
symath.lisp
1176 lines (1106 loc) · 50.9 KB
/
symath.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
(defpackage :symath
(:use cl)
(:export simplify array-multiply extract-subexpr get-polynome-cfs replace-subexpr split-to-subexprs with-var-cnt-reset))
(in-package :symath)
(defun mequal (a b)
(or (equal a b)
(and (numberp a)
(numberp b)
(= a b))))
(defun isfunc (f e) (and (listp e) (equal (car e) f)))
(defun filter-func (f l &optional invrs)
(labels ((flt (e) (isfunc f e)))
(if invrs
(remove-if #'flt l)
(remove-if-not #'flt l))))
(defun all-arraysp (l)
"Check if all elements in l are arrays"
(and (arrayp (car l))
(or (not (cdr l))
(all-arraysp (cdr l)))))
(defun equal-dimsp (l)
"Check if all arrays in l has the same dimensions"
(and (all-arraysp l)
(apply #'= (mapcar #'array-rank l))
(notany #'null
(apply #'mapcar
(cons #'=
(mapcar #'array-dimensions l))))))
(defun map-array (lam &rest arrs)
"Like #'mapcar, but for arrays"
(if (and (all-arraysp arrs)
(equal-dimsp arrs))
(let ((arr (make-array (array-dimensions (car arrs)))))
(loop for i below (apply #'* (array-dimensions (car arrs)))
do (setf (row-major-aref arr i)
(apply lam
(mapcar
(lambda (a)
(row-major-aref a i))
arrs))))
arr)
(error "Cannot map arrays with different dimensions")))
(defmethod array-multiply (x y)
(cond ((and (not (arrayp x))
(not (arrayp y)))
`(* ,x ,y))
((not (arrayp y))
(array-multiply y x))
((mequal x 0) 0)
((mequal x 1) y)
((not (arrayp x))
(map-array
(lambda (e)
(cond ((or (mequal e 0) (mequal x 0)) 0)
((mequal e 1) x)
((mequal x 1) e)
((and (isfunc '* e) (isfunc '* x)) `(* ,@(cdr e) ,@(cdr x)))
((isfunc '* e) `(* ,@(cdr e) ,x))
((isfunc '* x) `(* ,@(cdr x) ,e))
(t `(* ,x ,e))))
y))
((or (> (array-rank x) 2)
(> (array-rank y) 2))
(error "Array multiplication for non-2D matrices is not implemented. Overload #'array-multiple to implement."))
((and (equal (array-rank x) 2)
(equal (array-rank y) 2)
(not (cdr (remove-duplicates (append (array-dimensions x) (array-dimensions y))))))
(let ((dim (array-dimension x 0)))
(make-array `(,dim ,dim)
:initial-contents
(loop for j below dim collect
(loop for i below dim collect
`(+ ,@(loop for k below dim
collect (let ((v1 (aref x j k))
(v2 (aref y k i)))
(cond ((and (isfunc '* v1) (isfunc '* v2)) `(* ,@(cdr v1) ,@(cdr v2)))
((isfunc '* v1) `(* ,@(cdr v1) ,v2))
((isfunc '* v2) `(* ,v1 ,@(cdr v2)))
(t `(* ,v1 ,v2)))))))))))
(t (error "The '*' operator can be used only for multiplication of vector/matrix to constant or to multiply square matrices. Overload #'array-multiply to extend it."))))
(defmacro if-let (varexpr r1 &optional r2)
`(let ((,(car varexpr) ,(cadr varexpr)))
(if ,(car varexpr)
,r1
,r2)))
(defun tick ())
(defun boolp (e) (typep e 'boolean))
(defun copy-expr (e)
(if (listp e)
(copy-list e)
e))
(defun mintegerp (x)
(or (integerp x)
(and (numberp x)
(= (floor x) x))))
(defun all-bins (n) ;; 3 => ((0 0 0) (1 0 0) (0 1 0) (1 1 0) (0 0 1) (1 0 1) (0 1 1) (1 1 1))
(if (> n 1)
(mapcan
(lambda (b)
(list (cons 0 b)
(cons 1 b)))
(all-bins (- n 1)))
`((0) (1))))
(defun all-list-decs (l &key (min 1) (max (length l)))
(let ((ll (length l)))
(sort
(mapcar
(lambda (b)
(mapcar #'cdr
(remove-if (lambda (x) (= (car x) 0))
(mapcar #'cons b l))))
(mapcar #'cdr
(remove-if-not
(lambda (x) (and (>= (car x) min)
(<= (car x) max)))
(mapcar (lambda (x) (cons (count 1 x) x))
(all-bins ll)))))
#'>
:key #'length)))
(defun sqr (x)
"Multiple argument by itself. Works for vectors too!"
(if (arrayp x)
(if (cdr (array-dimensions x))
(array-multiply x x)
`(+ ,@(map 'list (lambda (x) `(expt ,x 2)) x)))
`(expt ,x 2)))
(defun math-rec-funcall (fn arg &optional deep)
"Call the function fn recursively"
(labels ((rfn (x)
(if (equal deep 0)
x
(math-rec-funcall fn x (if deep (- deep 1))))))
(multiple-value-call fn (cond
((listp arg)
(cons (car arg) (mapcar #'rfn (cdr arg))))
((arrayp arg)
(map-array #'rfn arg))
(t arg)))))
(defparameter *return-best-variant* nil) ;; If T, defun-stable-expr functions will return less weighted result. USE WITH CAUTION!
(defmacro defun-stable-expr (name args &rest cod)
(let ((h (gensym))
(r (gensym))
(arg (gensym)))
`(defun ,name (,arg &optional ,h)
(let* ((,r (funcall (lambda ,args ,@cod) ,arg)))
(if (position ,r ,h :test #'equal-expr)
(if *return-best-variant*
(caar (stable-sort (mapcar #'cons ,h (mapcar #'expr-weight ,h)) #'< :key #'cdr))
,r)
(,name ,r (cons ,r ,h)))))))
(defmacro chain-func-rec (fl arg)
"Recursively call function chain"
(let ((rfl (reverse fl)))
(reduce (lambda (r x)
(if (listp x)
`(multiple-value-call #'math-rec-funcall #',(cadr x) ,r ,(car x))
`(multiple-value-call #'math-rec-funcall #',x ,r)))
(cdr rfl)
:initial-value (if (listp (car rfl))
`(multiple-value-call #'math-rec-funcall #',(cadr (car rfl)) ,arg ,(car (car rfl)))
`(multiple-value-call #'math-rec-funcall #',(car rfl) ,arg)))))
(defmacro def-expr-cond (&rest margs)
"(expr-cond func-name expr-name :numberp cod :boolp cod :op (op cod) ... :defop cod)"
(let* ((func-name (car margs))
(e1 (cadr margs))
(expr-name e1)
(pars (loop for x on (cddr margs) by #'cddr collect (cons (car x) (cadr x))))
(ops (mapcar #'cdr (remove-if-not (lambda (x) (equal (car x) :op)) pars)))
(defop (cdar (remove-if-not (lambda (x) (equal (car x) :defop)) pars)))
(non-ops (remove-if-not (lambda (x)
(not (or (equal (car x) :op)
(equal (car x) :defop)))) pars)))
`(defun ,func-name ,(if (listp e1) e1 (list e1))
(tick)
(cond ,@(mapcar (lambda (x)
`((,(intern (symbol-name (car x))) ,expr-name) ,(cdr x)))
non-ops)
,@(if (not (or (position :arrayp non-ops)
(position :vectorp non-ops)))
`(((arrayp ,expr-name)
(map-array #',func-name ,expr-name))))
((listp ,expr-name)
(cond
,@(mapcar (lambda (x)
(let ((op (car x))
(cod (cadr x)))
`(,(if (listp op)
`(or ,@(mapcar (lambda (x)
`(equal (car ,expr-name) ',x)) op))
`(equal (car ,expr-name) ',op))
,cod)))
ops)
,@(if defop `((t ,defop)) `((t ,expr-name)))))
(t ,expr-name)))))
; (defun equal-expr (e1 e2) ;; Just to suppress warnings about undefined function
; (declare (ignore e1 e2))
; (error "STUB"))
(defun equal-args (l1 l2)
"Check if l1 and l2 are lists of the same expressions"
(and (equal (length l1) (length l2))
(labels ((e-a (l1 l2)
(or (and (not l1) (not l2))
(and (equal-expr (car l1) (car l2))
(e-a (cdr l1) (cdr l2))))))
(e-a l1 l2))))
(defparameter *equarg-uniq-sym* (gensym))
(defun equal-arg-sets (l1 l2)
"Check if l1 and l2 are lists of the same sets of expressions"
(and (equal (length l1) (length l2))
(let* ((tl2 (copy-list l2)))
(map nil
(lambda (e)
(let ((pos (position e tl2 :test #'equal-expr)))
(if pos
(setf (elt tl2 pos) *equarg-uniq-sym*)
(return-from equal-arg-sets nil))))
l1)
(equal *equarg-uniq-sym* (car (remove-duplicates tl2))))))
;; equal-expr and equal-expr-1 are not checks for true equality in math sense,
;; expressions must have similar forms to be treated as equal.
;; For example, exprs (+ a b c) and (+ a (+ b c)) will not be found as equal,
;; but (+ a b c) and (+ b c a) will. The equal-expr-1 a bit more tricky, but
;; also don't trust it too much.
;; A better (but much slower) way to check exprs equality is:
;; (or (equal 1 (symplify '(/ e1 e2))) (equal 0 (symplify '(- e1 e2))))
(defun equal-expr (e1 e2)
"Check if e1 and e2 a the same exprs"
(or (tree-equal e1 e2) ;; Quick result if we are lucky
(mequal e1 e2)
(and (equal (type-of e1) (type-of e2))
(cond ((symbolp e1) (and (equal (symbol-name e1) (symbol-name e2)) ;; It is possible to have different symbols like with same names!
(equal (symbol-package e1) (symbol-package e2))))
((listp e1) (and (equal (car e1) (car e2))
(and (= (length e1) (length e2))
(if (or (equal (car e1) '*) (equal (car e1) '+))
(equal-arg-sets (cdr e1) (cdr e2))
(if (or (equal (car e1) '/) (equal (car e1) '-))
(and (equal-expr (cadr e1) (cadr e2))
(equal-arg-sets (cddr e1) (cddr e2)))
(equal-args (cdr e1) (cdr e2)))))))
((arrayp e1)
(and (equal (array-dimensions e1) (array-dimensions e2))
(loop for i below (apply #'* (array-dimensions e1))
when (not (equal-expr (row-major-aref e1 i) (row-major-aref e2 i))) return nil
finally (return t))))
(t nil)))))
(defun equal-expr-1 (e1 e2)
"Check if e1 is -e2"
(or (and (numberp e1)
(numberp e2)
(= e1 (* -1 e2)))
(or (and (listp e1) (listp e2)
(equal (car e1) (car e2))
(equal (length e1) (length e2))
(equal (car e1) '+)
(labels ((eq-sum (l1 l2)
(or (not l1)
(let ((pos (position (car l1) l2 :test #'equal-expr-1)))
(and pos
(eq-sum (cdr l1)
(append (subseq l2 0 pos)
(subseq l2 (+ pos 1)))))))))
(eq-sum (cdr e1) (cdr e2))))
(and (or (isfunc '- e1) (isfunc '- e2))
(destructuring-bind (me oe) (if (isfunc '- e1)
(list e1 e2)
(list e2 e1))
(or (and (isfunc '- oe) ;; a-b = b-a ; a-b-c-d = (b+c+d)-a
(= 3 (length oe))
(equal-expr (caddr oe) (cadr me))
(or (and (= 3 (length me))
(equal-expr (cadr oe) (caddr me)))
(equal-expr (cons '+ (cddr me))
(cadr oe))))
(and (isfunc '+ oe) ;; a-b-c-d=b+c+d-a
(let ((pos (position-if (lambda (e)
(equal-expr-1 e (cadr me)))
oe)))
(and pos
(equal-arg-sets (cddr me)
`(,@(subseq oe 1 pos)
,@(subseq oe (+ pos 1))))))))))
(and (or (isfunc '* e1) (isfunc '* e2) (isfunc '/ e1) (isfunc '/ e2))
(multiple-value-bind (me oe) (if (or (isfunc '* e1) (isfunc '/ e1))
(values e1 e2)
(values e2 e1))
(labels ((get-n-nn (e)
(values (apply #'* (remove-if-not #'numberp (cdr e)))
(remove-if #'numberp (cdr e))))
(count-pm (l1 l2 &key (nm 0))
(if (and l1 l2)
(if-let (pos (position (car l1) l2 :test #'equal-expr))
(count-pm (cdr l1)
(append (subseq l2 0 pos) (subseq l2 (+ pos 1)))
:nm nm)
(if-let (pos (position (car l1) l2 :test #'equal-expr-1))
(count-pm (cdr l1)
(append (subseq l2 0 pos) (subseq l2 (+ pos 1)))
:nm (+ nm 1))
0))
(if (or (and l1 (mequal (car l1) -1))
(and l2 (mequal (car l2) -1)))
(+ nm 1)
(if (and (not l1) (not l2))
nm
0)))))
(multiple-value-bind (mn mnn) (get-n-nn me)
(or (and (or (isfunc '* oe) (isfunc '/ oe))
(multiple-value-bind (on onn) (get-n-nn oe)
(oddp (count-pm (cons mn mnn) (cons on onn)))))
(and (not (cdr mnn))
(or (and (= mn -1)
(equal-expr (car mnn) oe))
(and (= mn 1)
(equal-expr-1 (car mnn) oe))))))))))))
(defun expr-weight (e &key (weight 0) max-weight)
"Calculate a difficulty to calculate the expression e. If the weight exceeds max-weight, don't calculate more."
(if (and max-weight (> weight max-weight))
(return-from expr-weight weight))
(if (listp e)
(+ (apply #'+ (mapcar #'expr-weight (cdr e)))
(* (length (cdr e))
(case (car e)
((+ -) 1)
(abs 2)
(cast 3) ;; cast type, like integer -> float, etc
(* 4)
(exp 50)
(/ 30)
((sqrt expt) 100)
((sin cos tan) 60)
((asin acos atan) 80)
(otherwise 200))))
(if (arrayp e)
(loop for i below (apply #'* (array-dimensions e))
sum (row-major-aref e i) into s
when (> (+ s weight) max-weight) return s
finally (return s))
0.1)))
; (defmacro defun-stable-expr (name args &rest cod)
; "Define the function of one argument, which will call itself with its result as a argument, until already a seen result will be returned"
; (let ((h (gensym))
; (r (gensym))
; (arg (gensym)))
; `(defun ,name (,arg &optional ,h)
; (let* ((,r (funcall (lambda ,args ,@cod) ,arg)))
; (if (position ,r ,h :test #'equal-expr)
; ,r ; (caar (stable-sort (mapcar #'cons ,h (mapcar #'expr-eqight ,h)) #'< :key #'cdr))
; (,name ,r (cons ,r ,h)))))))
(defun collect-exprs (e)
"Collecting * and + expressions: (+ a (+ b c)) => (+ a b c)"
(if (listp e)
(if (position (car e) '(+ *))
(cons (car e)
(mapcan
(lambda (e1)
(if (listp e1)
(if (equal (car e) (car e1))
(cdr e1)
(list e1))
(list e1)))
(copy-list (cdr e))))
e)
e))
(def-expr-cond extract-nums e ;; Compute everything where it is possible
:op ((+ *)
(let* ((op (car e))
(args (cdr e))
(num (apply (symbol-function op) (remove-if-not #'numberp args)))
(not-nums (remove-if #'numberp args)))
(if not-nums
(if (equal op '*)
(cond ((mequal num 0)
0)
((mequal num 1)
(if (cdr not-nums)
(cons op not-nums)
(car not-nums)))
(t `(,op ,num ,@not-nums)))
(if (mequal num 0)
(if (cdr not-nums)
(cons op not-nums)
(car not-nums))
`(,op ,num ,@not-nums)))
num)))
:op (expt (if (or (mequal 0 (cadr e))
(mequal 1 (cadr e)))
(cadr e)
(if (numberp (caddr e))
(cond ((= (caddr e) 0) 1)
((= (caddr e) 1) (cadr e))
((numberp (cadr e)) (expt (cadr e) (caddr e)))
((equal '&math-e (cadr e)) (exp (caddr e)))
(t e))
e)))
:op (cast (if (numberp (caddr e))
(cond
((equal 'real*8 (cadr e)) (float (caddr e)))
((equal 'real*4 (cadr e)) (float (caddr e)))
((equal 'integer (cadr e))
(multiple-value-bind (f r) (floor (caddr e))
(if (= 0.0 r) f e)))
(t e))
e))
:defop (if (and (fboundp (car e))
(every #'numberp (cdr e)))
(apply (car e) (cdr e))
e))
(def-expr-cond norm-expr e ;; Convert every / and - to * and +, sqrt to (expt x 1/2)
:op (- (if (= (length e) 2)
`(* -1 ,(cadr e))
`(+ ,(cadr e)
(* -1 ,(if (= (length e) 3)
(caddr e)
`(+ ,@(cddr e)))))))
:op (/ `(* ,(cadr e) (expt ,(if (= (length e) 3)
(caddr e)
`(* ,@(cddr e)))
-1)))
:op (sqrt `(expt ,(cadr e) 1/2))
:op (exp `(expt &math-e ,(cadr e)))
:op (expt (if (and (arrayp (cadr e))
(numberp (caddr e))
(evenp (caddr e))
(equal 1 (array-rank (cadr e))))
(cond ((equal (caddr e) 0)
1)
(t `(expt ,(sqr (cadr e)) (/ (caddr e) 2))))
e)))
(def-expr-cond denorm-expr-expt e ;; The reverse of norm-expr for / and expt
:op (* (labels ((dvp (x)
(and (listp x)
(equal (length x) 3)
(equal (car x) '/)
(mequal (cadr x) 1))))
(let* ((divs (remove-if-not #'dvp (cdr e)))
(ndivs (remove-if #'dvp (cdr e)))
(dvop (if divs
(if (cdr divs)
`(/ 1 (* ,@(mapcar #'caddr divs)))
(car divs))))
(ndvop (if ndivs
(if (cdr ndivs)
`(* ,@ndivs)
(car ndivs)))))
(if divs
(if ndivs
`(/ ,ndvop ,(caddr dvop))
dvop)
e))))
:op (expt (if (numberp (caddr e))
(cond ((= (caddr e) 1/2)
`(sqrt ,(cadr e)))
((= (caddr e) 1)
(cadr e))
((= (caddr e) 0)
1)
((< (caddr e) 0)
`(/ 1 ,(if (mequal (caddr e) -1)
(cadr e)
`(expt ,(cadr e) ,(* -1 (caddr e))))))
(t e))
(if (equal '&math-e (cadr e))
`(exp ,(caddr e))
e))))
(def-expr-cond denorm-expr-zop e ;; Replace every (* x) and (+ x) to x
:op ((+ *) (if (cddr e)
e
(cadr e))))
(def-expr-cond denorm-expr-minus e ;; Convert (+ ... (* -1 ....) ...) to (- ...)
:op (+ (labels ((isneg (x)
(and (isfunc '* x)
(cddr x)
(find-if #'minusp (remove-if-not #'numberp (cdr x)))))
(deneg (l)
(if l
(if (minusp (car l))
(if (mequal (car l) -1)
(cdr l)
(cons (* -1 (car l)) (cdr l)))
(cons (car l) (deneg (cdr l)))))))
(let* ((negs (remove-if-not #'isneg (cdr e)))
(poss (remove-if #'isneg (cdr e)))
(pnegs (mapcar
(lambda (x)
(let ((dn (deneg (cdr x))))
(if (cdr dn)
`(* ,@dn)
(car dn))))
negs)))
(if negs
(if poss
`(- ,(if (cdr poss)
`(+ ,@poss)
(car poss))
,@pnegs)
`(* -1 (+ ,@pnegs)))
e)))))
(def-expr-cond denorm-expr-minus1 e ;; (+ -5 x) => (- x 5)
:op (+ (if (and (numberp (cadr e))
(< (cadr e) 0))
`(- ,(if (cdddr e)
`(+ ,@(cddr e))
(caddr e))
,(* -1 (cadr e)))
e)))
(def-expr-cond denorm-expr-plus-n e ;; Move numbers to the end of (+ ...)
:op (+ (if (and (numberp (cadr e))
(cddr e))
`(+ ,@(cddr e) ,(cadr e))
e)))
(defun denorm-expr (e)
(chain-func-rec
(denorm-expr-expt
denorm-expr-plus-n
denorm-expr-zop
denorm-expr-minus1
denorm-expr-minus)
e))
(def-expr-cond expand-expt1 e ;; (expt (* a ...) n) => (* (expt a n) ...)
:op (expt (if (isfunc '* (cadr e))
(cons '* (mapcar
(lambda (x)
`(expt ,x ,(caddr e)))
(cdr (cadr e))))
e)))
(def-expr-cond expand-expt2 e ;; (expt x (+ n1 ...)) => (* (expt x n) ...)
:op (expt (if (isfunc '+ (caddr e))
(cons '* (mapcar
(lambda (x)
`(expt ,(cadr e) ,x))
(cdr (caddr e))))
e)))
(def-expr-cond collect-expt e ;; Reverse of expand-expt*
:op (* (if (cddr e)
(let ((args (list))
(cf 1))
(labels ((process-arg (e)
(let ((e1 (if (isfunc 'expt e) (cadr e) e))
(d (if (isfunc 'expt e) (caddr e) 1)))
(if-let (r (assoc e1 args :test #'equal-expr))
(setf (cdr r) `(+ ,(cdr r) ,d))
(if-let (r (assoc e1 args :test #'equal-expr-1))
(progn
(setf (cdr r) `(+ ,(cdr r) ,d))
(setf cf (* cf -1)))
(push (cons e1 d) args))))))
(map nil #'process-arg (cdr e))
(let ((arg2 `(,@(if (= cf 1) nil (list cf))
,@(mapcar
(lambda (x)
(if (mequal (cdr x) 1) (car x) `(expt ,(car x) ,(cdr x))))
args))))
(if (cdr arg2)
`(* ,@arg2)
(car arg2)))))
e))
:op (expt (cond ((isfunc 'expt (cadr e))
`(expt ,(cadadr e) (* ,(caddr e) ,(car (cddadr e)))))
((isfunc '* (cadr e))
`(* ,@(mapcar (lambda (x)
(if (isfunc 'expt x)
`(expt ,(cadr x) (* ,(caddr e) ,(caddr x)))
`(expt ,x ,(caddr e))))
(cdadr e))))
(t e))))
(defparameter *max-degree-expansion* 5)
(def-expr-cond expand-int-expt e ;; (expt x 3) => (* x x x)
:op (expt (if (and (mintegerp (caddr e))
(listp (cadr e))
(> (abs (caddr e)) 1)
(<= (abs (caddr e)) *max-degree-expansion*))
(cons '* (loop for i below (abs (caddr e)) collect
(if (< (caddr e) 0)
`(expt ,(cadr e) -1)
(cadr e))))
e)))
(def-expr-cond expand-mul e ;; (* (+ ...) (+ ...)) => (+ (* ...) (* ...))
:op (* (let* ((args (cdr e))
(plus (mapcar #'cdr (mapcar #'collect-exprs (filter-func '+ args))))
(notplus (filter-func '+ args t)))
(if plus
(cons '+
(labels ((comb (l1 l2)
(loop for x in l1 append
(loop for y in l2 collect (cons y x))))
(comb-all (l)
(if (cdr l)
(comb-all (copy-tree (cons (comb (car l) (cadr l)) (cddr l))))
(car l))))
(loop for x in (comb-all (cons (list nil) plus)) collect
(collect-exprs (cons '* (concatenate 'list notplus x))))))
e))))
(def-expr-cond collect-common-nums e ;; (+ (* 2 x) (* 4 y)) => (* 2 (+ x (* 2 y)))
:op (+ (let* ((nums (mapcar
(lambda (x)
(if (numberp x)
x
(if (and (isfunc '* x)
(numberp (cadr x)))
(cadr x)
1)))
(cdr e))))
(if (not (every #'mintegerp nums))
e
(let* ((mcnt (length (remove-if-not #'minusp nums)))
(pcnt (- (length nums) mcnt))
(g (apply #'gcd (mapcar #'truncate nums)))
(g1 (* g (if (> mcnt pcnt) -1 1))))
(if (> g 1)
`(* ,g1 (+ ,@(mapcar
(lambda (x)
(if (numberp x)
(/ x g1)
(if (and (isfunc '* x)
(numberp (cadr x)))
(if (mequal (cadr x) g1)
(if (cdddr x)
`(* ,@(cddr x))
(caddr x))
`(* ,(/ (cadr x) g1) ,@(cddr x)))
(error "Internal error!!!"))))
(cdr e))))
e))))))
(defun is-int (e)
(or (rationalp e)
(and (floatp e) (mequal (floor e) e))))
(defun is-int-expt (e)
(and (isfunc 'expt e)
(is-int (caddr e))))
(defun eqf (e1 e2 &key int-expt) ;; helper function for extract-subexpr and collect-one-common
(let ((expt-fn (if int-expt
#'is-int-expt
(lambda (x) (isfunc 'expt x)))))
(or (equal-expr e1 e2)
(equal-expr-1 e1 e2)
(and (funcall expt-fn e2)
(or (equal-expr e1 (cadr e2))
(equal-expr-1 e1 (cadr e2))))
(and (funcall expt-fn e1)
(or (equal-expr e2 (cadr e1))
(equal-expr-1 e2 (cadr e1))))
(and (funcall expt-fn e1)
(funcall expt-fn e2)
(or (equal-expr (cadr e1) (cadr e2))
(equal-expr-1 (cadr e1) (cadr e2)))))))
(defun extract-subexpr-norm (e subex) ;; convert e (must be normalized) to the form: (subex^n)*e1+e2, return (values n e1 e2)
(labels
((equal-expr-2 (e1 e2)
(or (equal-expr e1 e2)
(equal-expr-1 e1 e2)))
(get-deg (x &key in-mul)
(cond ((equal-expr subex x)
(list 1 1))
((equal-expr-1 subex x)
(list 1 -1))
((and (isfunc 'expt x)
(equal-expr subex (cadr x)))
(list (caddr x) 1))
((and (isfunc 'expt x)
(equal-expr-1 (cadr x) subex))
(list (caddr x) `(expt -1 ,(caddr x))))
((and (not in-mul)
(isfunc '* x))
(map nil
(lambda (x1)
(if-let (res (get-deg x1 :in-mul t))
(return-from get-deg res)))
(cdr x))))))
(let* ((args (if (isfunc '+ e)
(if (isfunc '+ subex)
(labels ((rmse (le ls &optional res m)
(if (and le ls)
(let ((pos (position (car le) ls :test (if m #'equal-expr-1 #'equal-expr))))
(multiple-value-call #'rmse
(cdr le)
(if pos
`(,@(subseq ls 0 pos) ,@(subseq ls (1+ pos)))
ls)
(if pos
res
(cons (car le) res))
m))
(if ls
`(,@(reverse res) ,@le)
(values `(,@(reverse res) ,(if m `(* -1 ,subex) subex) ,@le) t)))))
(multiple-value-bind (args fnd) (rmse (cdr e) (cdr subex))
(if fnd
args
(rmse (cdr e) (cdr subex) nil t))))
(cdr e))
(list e)))
(dgsl (mapcar #'cons
(mapcar #'get-deg args)
args))
(ee-args (remove-if #'null dgsl :key #'car))
(nee-args (mapcar #'cdr (remove-if-not #'null dgsl :key #'car)))
(degs (remove-duplicates (mapcar #'caar ee-args) :test #'equal-expr))
(ee-deg (if (and degs (every #'is-int degs))
(apply #'min degs)
(if (cdr degs)
0
(if degs
(car degs)
0))))
(eargs (mapcar
(lambda (de)
(let ((dg (caar de))
(ml (cadr (car de)))
(ex (cdr de)))
(cond
((and (mequal dg ee-deg)
(equal-expr-2 ex subex))
ml)
((and (isfunc 'expt ex)
(equal-expr-2 (cadr ex) subex))
`(* ,ml (expt ,(cadr ex) (+ ,(caddr ex) (* -1 ,ee-deg)))))
((isfunc '* ex)
(labels ((rpe (exl)
(if exl
(let ((ex2 (car exl)))
(if (eqf ex2 subex)
(cond ((mequal dg ee-deg)
(cons ml (cdr exl)))
((isfunc 'expt ex2)
(cons
`(* ,ml (expt ,(cadr ex2) (+ ,(caddr ex2) (* -1 ,ee-deg))))
(cdr exl)))
(t (cons
`(* ,ml (expt ,ex2 (+ ,dg (* -1 ,ee-deg))))
(cdr exl))))
(cons ex2 (rpe (cdr exl)))))
(error (format nil "Internal error: common term not found!")))))
(cons '* (rpe (cdr ex)))))
(t (error "Internal error: cannot find expr to collect")))))
ee-args)))
(values ee-deg
(if (cdr eargs)
(cons '+ eargs)
(if eargs
(car eargs)
0))
(if (cdr nee-args)
(cons '+ nee-args)
(if nee-args
(car nee-args)
0))))))
(defun extract-subexpr (e subex &key expand) ;; normailze -> extract-subexpr-norm -> denormalize, if expant is T, expand all muls
(let* ((en (math-rec-funcall #'norm-expr e))
(en (if expand
(math-rec-funcall #'extract-nums
(math-rec-funcall #'collect-exprs
(math-rec-funcall #'expand-mul
(math-rec-funcall #'expand-int-expt
(math-rec-funcall #'extract-nums
(math-rec-funcall #'collect-exprs
(math-rec-funcall #'extract-nums
(math-rec-funcall #'collect-expt
(math-rec-funcall #'expand-expt2
(math-rec-funcall #'expand-expt1
(math-rec-funcall #'collect-exprs en)))))))))))
en))
(en (if expand
(math-rec-funcall #'extract-nums (cons (car en) (mapcar #'collect-expt (cdr en))))
en))
(sn (math-rec-funcall #'norm-expr subex))
(sn (if expand
(math-rec-funcall #'extract-nums
(math-rec-funcall #'collect-exprs
(math-rec-funcall #'expand-mul
(math-rec-funcall #'expand-int-expt
(math-rec-funcall #'extract-nums
(math-rec-funcall #'collect-exprs
(math-rec-funcall #'extract-nums
(math-rec-funcall #'collect-expt
(math-rec-funcall #'expand-expt2
(math-rec-funcall #'expand-expt1
(math-rec-funcall #'collect-exprs sn)))))))))))
sn))
(sn (if (and expand (listp sn))
(math-rec-funcall #'extract-nums (cons (car en) (mapcar #'collect-expt (cdr sn))))
sn)))
(multiple-value-bind (n e1 e2) (extract-subexpr-norm en sn)
(if (mequal n 0)
(values 0 0 e)
(labels ((den (e)
(funcall (if expand #'simplify #'identity)
(chain-func-rec (extract-nums denorm-expr) e))))
(values (den n)
(den e1)
(den e2)))))))
(defun get-polynome-cfs (e v &key expand) ;; Convert e to polynome against v and return alist of coefficients, like ((0 . zero-cf) (1 . 1-cf) (2 . 2-cf) ...)
(multiple-value-bind (n e1 e2) (extract-subexpr e v :expand expand)
(cond ((> n 0) (cons (cons 0 e2)
(mapcar (lambda (cf) (cons (+ (car cf) n) (cdr cf))) (get-polynome-cfs e1 v :expand expand))))
((= n 0) (list (cons 0 e2)))
(t (error (format nil "Strange polynomial coefficient found: ~A" n))))))
(defun collect-one-common (e) ;; extract one most common expression from e. Don't ever try to improve, if not understand it completely.
(if (isfunc '+ e)
(let ((e (math-rec-funcall #'collect-exprs e))
(ecnt-l nil)
(eqf (lambda (e1 e2) (eqf e1 e2 :int-expt t))))
(labels
((count-expr (e1 nt)
(if (not (numberp e1))
(labels ((inc-hash-test (e)
(if (not (numberp e))
(if-let (vk (rassoc e ecnt-l :test eqf))
(pushnew nt (car vk))
(push (cons (list nt) e) ecnt-l)))))
(map nil
(lambda (e2)
(if (is-int-expt e2)
(inc-hash-test (cadr e2))
(inc-hash-test e2)))
(if (isfunc '* e1)
(cons e1 (cdr e1))
(list e1)))))))
(map nil
#'count-expr
(cdr e)
(loop for i below (length (cdr e)) collect i))
(let* ((mxc (apply #'max (mapcar (lambda (x) (length (car x))) ecnt-l)))
(subexs (if (= mxc 1)
(mapcar
(lambda (x)
(cons '+ x))
(all-list-decs (cdr e) :min 2 :max (length (cddr e)))))))
(if (= mxc 1)
(map nil
#'count-expr
subexs
(loop for i below (length subexs) collect (+ i (length (cdr e))))))
(if (> (apply #'max (mapcar (lambda (x) (length (car x))) ecnt-l)) 1)
(let* ((ee (cdr (car (stable-sort (reverse ecnt-l) #'> :key (lambda (x) (length (car x)))))))
(ee (if (is-int-expt ee)
(cadr ee)
ee)))
(multiple-value-bind (dg e1 e2) (extract-subexpr-norm e ee)
(if (not (mequal dg 0))
(values (let* ((eedg (if (mequal dg 1)
ee
`(expt ,ee ,dg)))
(ee1 (if (mequal e1 1)
eedg
`(* ,eedg ,e1))))
(if (mequal e2 0)
ee1
`(+ ,ee1 ,e2)))
t)
(values e nil))))
(values e nil)))))
(values e nil)))
(defun-stable-expr collect-common (e) ;; collect all comvon subexprs
(labels ((lp (e &optional (deep 0))
(if (> deep 1000) (error "Too deep recursion in collect-common"))
(let* ((e (math-rec-funcall #'collect-exprs
(math-rec-funcall #'collect-common-nums
(math-rec-funcall #'extract-nums e)))))
(if (listp e)
(let ((e (cons (car e) (mapcar (lambda (x) (lp x (1+ deep))) (cdr e)))))
(if (isfunc '+ e)
(multiple-value-bind (e1 r) (collect-one-common e)
(if r
(lp e1 (1+ deep))
e))
e))
e))))
(lp e)))
(def-expr-cond calc-arrays e
:op ((+ -) (if (some #'arrayp (cdr e))
(let* ((zar (make-array (array-dimensions (find-if #'arrayp (cdr e)))
:initial-element 0))
(me (mapcar
(lambda (x)
(if (mequal x 0)
zar
x))
(cdr e))))
(if (equal-dimsp me)
(apply #'map-array
(cons (lambda (&rest els)
`(,(car e) ,@els))
me))
(error "Cannot mix elements with different ranks in +/-")))
e))
:op (* (let ((arrs (remove-if-not #'arrayp (cdr e)))
(not-arrs (remove-if #'arrayp (cdr e))))
(if arrs
(reduce
#'array-multiply
(cdr arrs)
:initial-value (if not-arrs
(array-multiply (if (cdr not-arrs)
`(* ,@not-arrs)
(car not-arrs))
(car arrs))
1))
e)))
:op (/ (calc-arrays `(* ,(cadr e) (expt ,(if (cdddr e)
`(* ,@(cddr e))
(caddr e))
-1))))
:op (expt (if (arrayp (caddr e))
(error "Array cannot be an exponent, sorry")
(if (arrayp (cadr e))
(let ((ar (cadr e))
(ex (caddr e)))
(if (and (numberp ex)
(integerp ex)
(>= ex 0))
(cond ((mequal ex 0) 1)
((mequal ex 1) ar)
(t (if (cdr (array-dimensions ar))
(reduce
#'array-multiply
(loop repeat (- ex 1) collect ar)
:initial-value ar)
(if (evenp ex)
`(+ ,@(loop for i across ar collect `(expt ,i ,ex)))
(array-multiply `(+ ,@(loop for i across ar collect `(expt ,i ,(- ex 1)))) ar)))))
(error "(expt array e): e must be integer >= 0")))
e)))
:op (vector (make-array (list (length (cdr e)))
:initial-contents (cdr e)))
:op (aref (apply #'aref (cdr e)))
:op (sqr (sqr (cadr e))))
(def-expr-cond collect-same-expts e ;; (* (expt x 2) (expt y 2)) => (expt (* x y) 2)
:op (* (labels ((is-expt (x) (isfunc 'expt x))
(is-the-expt (ex) (lambda (e) (equal-expr (caddr e) ex))))
(let* ((expts (remove-if-not #'is-expt (cdr e)))
(non-expts (remove-if #'is-expt (cdr e)))
(expt-exs (remove-duplicates (mapcar #'caddr expts) :test #'equal-expr)))
(if (and (cdr expts)
(< (length expt-exs) (length expts)))
(let ((expt-terms (mapcar
(lambda (ex)
(let ((expts-ex (remove-if-not (is-the-expt ex) expts)))
`(expt ,(if (cdr expts-ex)
`(* ,@(mapcar #'cadr expts-ex))
(cadr (car expts-ex)))