-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
rules.cljc
1622 lines (1346 loc) · 45.5 KB
/
rules.cljc
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
;;
;; Copyright © 2021 Sam Ritchie.
;; This work is based on the Scmutils system of MIT/GNU Scheme:
;; Copyright © 2002 Massachusetts Institute of Technology
;;
;; This 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 3 of the License, or (at
;; your option) any later version.
;;
;; This software 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 code; if not, see <http://www.gnu.org/licenses/>.
;;
(ns sicmutils.simplify.rules
"This namespace contains many sets of algebraic simplification rules you can use
to build simplifiers for algebraic structures.
[[rules]] currently holds a mix of:
- rulesets ported from the scmutils library
- more fine-grained, tuneable rulesets like [[associative]]
and [[constant-elimination]] designed to help you build lightweight custom
term-rewriting simplifiers.
NOTE: Expect this namespace to be broken out into more fine-grained
namespaces. There are TODO entries throughout the docstrings left as tips for
how to proceed here."
(:refer-clojure :exclude [even? odd?])
(:require [clojure.set :as cs]
[pattern.match :as pm]
[pattern.rule :as r :refer [=> ruleset rule-simplifier]
#?@(:cljs [:include-macros true])]
[sicmutils.complex :as c]
[sicmutils.expression :as x]
[sicmutils.generic :as g]
[sicmutils.numsymb :as sym]
[sicmutils.util.logic :as ul]
[sicmutils.value :as v]))
;; ## Simplifier Configuration Variables
;;
;; SICMUtils uses a number of dynamic variables to tune the behavior of the
;; simplifier without having to thread explicit maps of options through the
;; rulesets. This design is not optimal; in a future version, we'll move any
;; rules that depend on these out to their own rulesets, and move the
;; configuration variables over to [[sicmutils.simplify]].
(def ^{:dynamic true
:doc "If true, allows the following simplification to proceed:
```clojure
(log (exp x)) => x.
```
Because `exp(i*x) == exp(i*(x+n*2pi))` for all integral `n`, this setting can
confuse `x` with `x+n*2pi`."}
*log-exp-simplify?*
true)
(def ^{:dynamic true
:doc "Allows `(x^a)^b => x^(a*b)`.
This is dangerous, because can lose or gain a root:
```
x = (x^(1/2))^2 != ((x^2)^1/2)=+-x
```
"}
*exponent-product-simplify?*
true)
(def ^{:dynamic true
:doc " Traditionally, `sqrt(x)` is the positive square root, but
`x^(1/2)` is both positive and negative roots.
Setting [[*expt-half->sqrt?*]] to `true` maps `x^(1/2)` to `sqrt(x)`,
potentially losing a root."}
*expt-half->sqrt?*
true)
(def ^{:dynamic true
:doc "If x is real, then `(sqrt (square x)) = (abs x)`.
Setting [[*sqrt-expt-simplify?*]] to `true` allows `(sqrt (square x)) = x`,
potentially causing a problem if `x` is in fact negative."}
*sqrt-expt-simplify?*
true)
(def ^{:dynamic true
:doc "If `x` and `y` are real and non-negative, then
```
(* (sqrt x) (sqrt y)) = (sqrt (* x y))
```
This is not true for negative factors. Setting [[*sqrt-factor-simplify?*]] to
true enables this simplification, causing a problem if `x` or `y` are in fact
negative."}
*sqrt-factor-simplify?*
true)
(def ^{:dynamic true
:doc "When `true`, allows:
```
(atan y x) => (atan (/ y d) (/ x d))
```
where `d=(gcd x y)`.
This is fine if `d` is a number (Numeric `gcd` is always positive), but may lose
quadrant information if `d` is a symbolic expression that can be negative for
some values of its variables."}
*aggressive-atan-simplify?*
true)
(def ^{:dynamic true
:doc "When `true`, allows trigonometric inverse functions to simplify:
```
(asin (sin x)) => x
```
Because trigonometric functions like `sin` and `cos` are cyclic, this can lose
multi-value info (as with [[*log-exp-simplify*]])."}
*inverse-simplify?*
true)
(def ^{:dynamic true
:doc "When `true`, allows arguments of `sin`, `cos` and `tan` that are
rational multiples of `'pi` to be reduced. See [[trig:special]] for these
rules."}
*sin-cos-simplify?*
true)
(def ^{:dynamic true
:doc "When `true`, enables the half-angle reductions described in [[half-angle]].
Note from GJS: 'Sign of result is hairy!'"}
*half-angle-simplify?*
true)
(def ^{:dynamic true
:doc "When true, allows commutation of partial derivatives so that partial derivatives appear in order.
For example:
```clojure
(((* (partial 2 1) (partial 1 1)) FF) (up t (up x y) (down p_x p_y)))
```
Since the partial indices in the outer derivative are lexically greater than
those of the inner, we canonicalize by swapping the order:
```clojure
(((* (partial 1 1) (partial 2 1)) FF) (up t (up x y) (down p_x p_y)))
```
When the components selected by the partials are unstructured (e.g. real), this
is okay due to the 'equality of mixed partials'."}
*commute-partials?*
true)
(def ^{:dynamic true
:doc "When `true`, allows division through the numerator by numbers in
the denominator:
```
(/ (+ (* 4 x) 5) 3) => (+ (* 4/3 x) 5/3)
```
This setting is `false` by default."}
*divide-numbers-through-simplify?*
true)
(def ^{:dynamic true
:doc "Transforms products of trig functions into functions of sums of
angles.
For example:
```
(* (sin x) (cos y))
;;=>
(+ (* 1/2 (sin (+ x y)))
(* 1/2 (sin (+ x (* -1 y)))) )
```"}
*trig-product-to-sum-simplify?*
false)
;; ## Binding Predicates
;;
;; The following predicates are used to restrict bindings in the rules that
;; follow. Bindings can take multiple predicates, but it reads a bit better to
;; have them tightened up into a single predicate.
(defn- negative-number? [x]
(and (v/number? x)
(g/negative? x)))
(defn- imaginary-number?
"Returns true if `z` is a complex number with nonzero imaginary part and zero
real part, false otherwise."
[z]
(and (c/complex? z)
(not (v/zero? z))
(v/zero? (g/real-part z))))
(defn- complex-number?
"Returns true if `z` is a complex number with nonzero real AND imaginary parts,
false otherwise."
[z]
(and (c/complex? z)
(not (v/zero? (g/real-part z)))
(not (v/zero? (g/imag-part z)))))
(defn- imaginary-integer?
"Returns true if `z` is an imaginary number with an integral (or VERY close to
integral) imaginary part, false otherwise."
[z]
(and (imaginary-number? z)
(v/almost-integral?
(g/imag-part z))))
(defn not-integral? [x]
(not (v/integral? x)))
(defn even? [x]
(v/zero? (g/modulo x 2)))
(defn odd? [x]
(v/one? (g/modulo x 2)))
(defn- even-integer? [x]
(and (v/integral? x)
(even? x)))
(defn odd-integer? [x]
(and (v/integral? x)
(odd? x)))
(defn odd-positive-integer? [x]
(and (odd-integer? x)
(> x 2)))
(defn- more-than-two? [x]
(and (v/number? x) (> x 2)))
(defn- at-least-two? [x]
(and (v/number? x) (>= x 2)))
;; ## Rule Sets
(defn unary-elimination
"Takes a sequence `ops` of operator symbols like `'+`, `'*` and returns a rule
that strips these operations off of unary applications.
```clojure
(let [rule (unary-elimination '+ '*)
f (rule-simplifier rule)]
(f '(+ x y (* z) (+ a))))
;;=> (+ x y z a)
```"
[& ops]
(let [op-set (into #{} ops)]
(ruleset
((? _ op-set) ?x) => ?x)))
(defn constant-elimination
"Takes an operator symbol `op` and an identity element `constant` and returns a
rule that eliminates instances of `constant` inside any-arity forms like
```clojure
(<op> ,,,args,,,)
```"
[op constant]
(ruleset
(~op ??xs)
=> (~op (?? (fn [{xs '??xs}]
(remove #{constant} xs))))))
(defn constant-promotion
"Takes an operator symbol `op` and an identity element `constant` and returns a
rule that turns binary forms with `constant` on either side into `constant`.
This rule is useful for commutative annihilators like:
```clojure
(* 0 <anything>) => 0
(and false <anything>) => false
(or true <anything>) => true
```"
[op constant]
(ruleset
(~op _ ~constant) => ~constant
(~op ~constant _) => ~constant))
(defn associative
"Takes any number of operator symbols `ops` like `'+`, `'*` and returns a rule
that collapses nested applications of each operation into a single
sequence. (The associative property lets us strip parentheses.)
```clojure
(let [rule (associative '+ '*)
f (rule-simplifier rule)]
(= (+ x y z a (* b c d) cake face)
(f '(+ x (+ y (+ z a) (* b (* c d))
(+ cake face))))))
```"
[& ops]
(let [op-set (into #{} ops)
flatten (fn [op]
(r/ruleset
(~op ??xs) => [??xs]
?x => [?x]))]
(ruleset
((? ?op op-set) ??a (?op ??b) ??c)
=>
(?op ??a (?? (fn [{op '?op, b '??b, c '??c}]
(mapcat (flatten op)
(concat b c))))))))
(defn commutative
"Takes any number of operator symbols `ops` like `'+`, `'*` and returns a rule
that sorts the argument list of any multiple-arity call to any of the supplied
operators. Sorting is accomplished with [[sicmutils.expression/sort]].
For example:
```clojure
(let [rule (commutative '* '+)]
(= '(* 2 3 a b c (+ c a b))
(rule '(* c a b (+ c a b) 3 2))))
```"
[& ops]
(let [op-set (into #{} ops)]
(ruleset
((? ?op op-set) ??xs)
=> (?op (?? #(x/sort (% '??xs)))))))
(defn idempotent
"Returns a simplifier that will remove consecutive duplicate arguments to any
of the operations supplied as `ops`. Acts as identity otherwise.
```clojure
(let [rule (idempotent 'and)]
(= '(and a b c d)
(rule '(and a b b c c c d))))
```"
[& ops]
(let [op-set (into #{} ops)]
(ruleset
((? ?op op-set) ??pre ?x ?x ??post)
=>
(?op (?? (fn [m]
(dedupe
(r/template
m (??pre ?x ??post)))))))))
(def ^{:doc "Set of rules that collect adjacent products, exponents and nested
exponents into exponent terms."}
exponent-contract
(ruleset
;; nested exponent case.
(expt (expt ?op (? ?n v/integral?))
(? ?m v/integral?))
=> (expt ?op (? #(g/+ (% '?n) (% '?m))))
;; adjacent pairs of exponents
(* ??pre
(expt ?op (? ?n v/integral?))
(expt ?op (? ?m v/integral?))
??post)
=> (* ??pre
(expt ?op (? #(g/+ (% '?n) (% '?m))))
??post)
;; exponent on right, non-expt on left
(* ??pre
?op (expt ?op (? ?n v/integral?))
??post)
=> (* ??pre
(expt ?op (? #(g/+ (% '?n) 1)))
??post)
;; exponent on left, non-expt on right
(* ??pre
(expt ?op (? ?n v/integral?)) ?op
??post)
=> (* ??pre
(expt ?op (? #(g/+ (% '?n) 1)))
??post)
;; non-exponent pairs
(* ??pre ?op ?op ??post)
=> (* ??pre (expt ?op 2) ??post)))
(defn logexp
"Returns a rule simplifier that attempts to simplify nested exp and log forms.
You can tune the behavior of this simplifier with [[*log-exp-simplify?*]]
and [[*sqrt-expt-simplify?*]].
NOTE: [[logexp]] returns a `rule-simplifier`, which memoizes its traversal
through the supplied expression. This means that if you try to
customize [[logexp]] with dynamic binding variables AFTER passing an
expression into it, you may get a memoized result which used the previous
dynamic binding.
This is a problem we should address!"
[simplify]
(rule-simplifier
(r/ruleset*
(r/rule
(exp (* (? ?n v/integral?) (log ?x)))
=> (expt ?x ?n))
(r/rule
(exp (log ?x)) => ?x)
(r/guard
(fn [_] *log-exp-simplify?*)
(r/rule
(log (exp ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(and (ul/assume!
(r/template
(= (log (exp ~xs)) ~xs))
'logexp1)
x)))))
(r/guard
(fn [_] *sqrt-expt-simplify?*)
(r/rule (sqrt (exp ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(and (ul/assume!
(r/template
(= (sqrt (exp ~xs))
(exp (/ ~xs 2))))
'logexp2)
(r/template (exp (/ ~x 2))))))))
(r/rule
(log (sqrt ?x)) => (* (/ 1 2) (log ?x))))))
(def ^{:doc "Rule simplifier for forms that contain `magnitude` entries."}
magnitude
(rule-simplifier
(ruleset
(magnitude (? ?n v/real?))
=> (? (comp g/magnitude '?n))
(magnitude (* ??xs))
=> (* (?? (fn [{xs '??xs}]
(map #(list 'magnitude %)
xs))))
(magnitude (expt ?x 1))
=> (magnitude ?x)
(magnitude (expt ?x (? ?n even-integer?)))
=> (expt ?x ?n)
(magnitude (expt ?x (? ?n v/integral?)))
=> (* (magnitude ?x) (expt ?x (? #(g/- (% '?n) 1)))))))
(defn miscsimp
"Simplifications for various exponent forms (assuming commutative multiplication).
NOTE that we have some similarities to [[exponent-contract]] above - that
function works for non-commutative multiplication - AND that this needs a new
name."
[simplify]
(let [sym:* (sym/symbolic-operator '*)]
(rule-simplifier
(ruleset
(expt _ 0) => 1
(expt ?x 1) => ?x
;; e^{ni} == 0,i,-1 or -i.
(expt (complex 0.0 1.0) (? ?n v/integral?))
=> (? #([1 '(complex 0.0 1.0) -1 '(complex 0.0 -1.0)]
(mod (% '?n) 4)))
(expt (expt ?x ?a) ?b)
(fn [{a '?a b '?b x '?x}]
(let [as (simplify a)
bs (simplify b)]
(when (or (and (v/integral? as)
(v/integral? bs))
(and (even-integer? bs)
(v/integral?
(simplify (sym:* as bs))))
(and *exponent-product-simplify?*
(ul/assume!
(r/template
(= (expt (expt ~x ~as) ~bs)
(expt ~x ~(sym:* as bs))))
'exponent-product)))
{'?ab (g/* a b)})))
(expt ?x ?ab)
(expt ?x (/ 1 2))
(fn [_] *expt-half->sqrt?*)
(sqrt ?x)
;; Collect duplicate terms into exponents.
;;
;; TODO this is missing the case where non-exponent duplicates get
;; collected into exponents. At least note this; in the current library,
;; like terms are collected by the polynomial simplifier.
;;
;; GJS notes: "a rare, expensive luxury."
(* ??fs1* ?x ??fs2 (expt ?x ?y) ??fs3)
=> (* ??fs1 ??fs2 (expt ?x (+ 1 ?y)) ??fs3)
(* ??fs1 (expt ?x ?y) ??fs2 ?x ??fs3)
=> (* ??fs1 (expt ?x (+ 1 ?y)) ??fs2 ??fs3)
(* ??fs1 (expt ?x ?y1) ??fs2 (expt ?x ?y2) ??fs3)
=> (* ??fs1 ??fs2 (expt ?x (+ ?y1 ?y2)) ??fs3)))))
;; ## Square Root Simplification
(defn simplify-square-roots [simplify]
(rule-simplifier
(r/rule
(expt (sqrt ?x) (? ?n even-integer?))
=> (expt ?x (? #(g// (% '?n) 2))))
;; TODO verify that these trigger and that the simplifier passes them
;; correctly.
(r/ruleset*
(r/guard
(fn [_] *sqrt-expt-simplify?*)
(r/ruleset*
(r/rule
(sqrt (expt ?x (? ?n even-integer?)))
(fn [{x '?x n '?n}]
(let [xs (simplify x)
half-n (g// n 2)]
(when (ul/assume!
(r/template
(= (sqrt (expt ~xs ~n))
(expt ~xs ~half-n)))
'simsqrt1)
{'?new-n half-n})))
(expt ?x ?new-n))
(r/rule
(sqrt (expt ?x (? ?n odd-positive-integer?)))
(fn [{x '?x n '?n}]
(let [xs (simplify x)
half-dec-n (g// (g/- n 1) 2)]
(when (ul/assume!
(r/template
(= (sqrt (expt ~xs ~n))
(expt ~xs ~half-dec-n)))
'simsqrt2)
{'?new-n half-dec-n})))
(* (sqrt ?x) (expt ?x ?new-n))))))
(ruleset
(expt (sqrt ?x) (? ?n odd-integer?))
=> (* (sqrt ?x)
(expt ?x (? #(g// (g/- (% '?n) 1) 2))))
(/ ?x (sqrt ?x)) => (sqrt ?x)
(/ (sqrt ?x) ?x) => (/ 1 (sqrt ?x))
(/ (* ??u ?x ??v) (sqrt ?x))
=>
(* ??u (sqrt ?x) ??v)
(/ (* ??u (sqrt ?x) ??v) ?x)
=>
(/ (* ??u ??v) (sqrt ?x))
(/ ?x (* ??u (sqrt ?x) ??v))
=>
(/ (sqrt ?x) (* ??u ??v))
(/ (sqrt ?x) (* ??u ?x ??v))
=>
(/ 1 (* ??u (sqrt ?x) ??v))
(/ (* ??p ?x ??q)
(* ??u (sqrt ?x) ??v))
=>
(/ (* ??p (sqrt ?x) ??q)
(* ??u ??v))
(/ (* ??p (sqrt ?x) ??q)
(* ??u ?x ??v))
=>
(/ (* ??p ??q)
(* ??u (sqrt ?x) ??v)))))
(defn non-negative-factors!
"Takes a `simplify` function, two simplified expressions `x` and `y` and a symbolic
identifier `id` and registers an assumption that both sides are
non-negative (just one side if they end up equal after simplification).
Returns the conjuction of both assumptions."
([simplify x id]
(ul/assume! `(~'non-negative? ~x) id (fn [] false)))
([simplify x y id]
(and (ul/assume! `(~'non-negative? ~x) id (fn [] false))
(ul/assume! `(~'non-negative? ~y) id (fn [] false)))))
(defn sqrt-expand
"Returns a rule simplifier that distributes the radical sign across products and
quotients. The companion rule [[sqrt-contract]] reassembles what remains.
NOTE that doing this may allow equal subexpressions within the radicals to
cancel in various ways.
Turn this simplifier on and off with [[*sqrt-factor-simplify?*]]."
[simplify]
(letfn [(pred [label]
(fn [{x '?x y '?y}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= xs ys)
(non-negative-factors! simplify xs label)
(non-negative-factors! simplify xs ys label)))))]
(r/attempt
(r/guard
(fn [_] *sqrt-factor-simplify?*)
(rule-simplifier
(ruleset
(sqrt (* ?x ?y))
(pred 'e1)
(* (sqrt ?x) (sqrt ?y))
(sqrt (* ?x ?y ??ys))
(pred 'e2)
(* (sqrt ?x) (sqrt (* ?y ??ys)))
(sqrt (/ ?x ?y))
(pred 'e3)
(/ (sqrt ?x) (sqrt ?y))
(sqrt (/ ?x ?y ??ys))
(pred 'e4)
(/ (sqrt ?x) (sqrt (* ?y ??ys)))))))))
(defn sqrt-contract [simplify]
(let [non-negative! (partial non-negative-factors! simplify)]
(rule-simplifier
(r/ruleset*
(r/rule
(* ??a (sqrt ?x) ??b (sqrt ?y) ??c)
(fn [{x '?x y '?y :as m}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= xs ys)
(and (non-negative! xs 'c1)
(r/template
m (* ??a ~xs ??b ??c)))
(and (non-negative! xs ys 'c1)
(r/template
m (* ??a (sqrt (* ~xs ~ys)) ??b ??c)))))))
(r/rule
(/ (sqrt ?x) (sqrt ?y))
(fn [{x '?x y '?y}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= xs ys)
(and (non-negative! xs 'c2)
1)
(and (non-negative! xs ys 'c2)
(r/template (sqrt (/ ~xs ~ys))))))))
(r/rule
(/ (* ??a (sqrt ?x) ??b) (sqrt ?y))
(fn [{x '?x y '?y :as m}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= xs ys)
(and (non-negative! xs 'c3)
(r/template m (* ??a ??b)))
(and (non-negative! xs ys 'c3)
(r/template
m (* ??a (sqrt (/ ~xs ~ys)) ??b)))))))
(r/rule
(/ (sqrt ?x) (* ??a (sqrt ?y) ??b))
(fn [{x '?x y '?y :as m}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= xs ys)
(and (non-negative! xs 'c4)
(r/template m (/ 1 (* ??a ??b))))
(and (non-negative! xs ys 'c4)
(r/template
m (/ (sqrt (/ ~xs ~ys))
(* ??a ??b))))))))
(r/rule
(/ (* ??a (sqrt ?x) ??b)
(* ??c (sqrt ?y) ??d))
(fn [{x '?x y '?y :as m}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= xs ys)
(and (non-negative! xs 'c5)
(r/template
m (/ (* ??a ??b)
(* ??c ??d))))
(and (non-negative! xs ys 'c5)
(r/template
m (/ (* ??a (sqrt (/ ~xs ~ys)) ??b)
(* ??c ??d))))))))))))
;; ## Log / Exp
(def specfun->logexp
(rule-simplifier
(ruleset
(sqrt ?x) => (exp (* (/ 1 2) (log ?x)))
(atan ?z)
=> (/ (- (log (+ 1 (* (complex 0.0 1.0) ?z)))
(log (- 1 (* (complex 0.0 1.0) ?z))))
(complex 0.0 2.0))
(asin ?z)
=> (* (complex 0.0 -1.0)
(log (+ (* (complex 0.0 1.0) ?z)
(sqrt (- 1 (expt ?z 2))))))
(acos ?z)
=> (* (complex 0.0 -1.0)
(log (+ ?z (* (complex 0.0 1.0)
(sqrt (- 1 (expt ?z 2)))))))
(sinh ?u) => (/ (- (exp ?u) (exp (* -1 ?u))) 2)
(cosh ?u) => (/ (+ (exp ?u) (exp (* -1 ?u))) 2)
(expt ?x (? ?y not-integral?)) => (exp (* ?y (log ?x))))))
(def logexp->specfun
(rule-simplifier
(ruleset
(exp (* -1 (log ?x))) => (expt ?x -1)
(exp (* (/ 1 2) (log ?x))) => (sqrt ?x)
(exp (* (/ -1 2) (log ?x))) => (/ 1 (sqrt ?x))
(exp (* (/ 3 2) (log ?x))) => (expt (sqrt ?x) 3)
(exp (* (/ -3 2) (log ?x))) => (expt (sqrt ?x) -3)
(exp (* ??n1 (log ?x) ??n2))
=> (expt ?x (* ??n1 ??n2)))))
(defn log-contract [simplify]
(rule-simplifier
(ruleset
(+ ??x1 (log ?x2) ??x3 (log ?x4) ??x5)
=> (+ ??x1 ??x3 ??x5 (log (* ?x2 ?x4)))
(- (log ?x) (log ?y))
=> (log (/ ?x ?y))
(+ ??x1
(* ??f1 (log ?x) ??f2)
??x2
(* ??f3 (log ?y) ??f4)
??x3)
(fn [m]
(let [s1 (simplify (r/template m (* ??f1 ??f2)))
s2 (simplify (r/template m (* ??f3 ??f4)))]
(when (v/exact-zero?
(simplify (list '- s1 s2)))
{'??s1 s1})))
(+ (* (log (* ?x ?y)) ??s1)
??x1 ??x2 ??x3))))
(def log-expand
(rule-simplifier
(ruleset
(log (* ?x1 ?x2 ??xs))
=> (+ (log ?x1) (log (* ?x2 ??xs)))
(log (/ ?x1 ?x2))
=> (- (log ?x1) (log ?x2))
(log (expt ?x ?e))
=> (* ?e (log ?x)))))
(def log-extra
(rule-simplifier
(ruleset
(* (? ?n v/integral?) ??f1 (log ?x) ??f2)
=> (* ??f1 (log (expt ?x ?n)) ??f2))))
;; ## Partials
(def canonicalize-partials
(rule-simplifier
(ruleset
;; Convert nests into products.
((partial ??i) ((partial ??j) ?f))
=> ((* (partial ??i) (partial ??j)) ?f)
((partial ??i) ((* (partial ??j) ??more) ?f))
=> ((* (partial ??i) (partial ??j) ??more) ?f)
;; Gather exponentiated partials into products
((expt (partial ??i) ?n) ((partial ??j) ?f))
=> ((* (expt (partial ??i) ?n) (partial ??j)) ?f)
((partial ??i) ((expt (partial ??j) ?n) ?f))
=> ((* (partial ??i) (expt (partial ??j) ?n)) ?f)
((expt (partial ??i) ?n) ((expt (partial ??j) ?m) ?f))
=> ((* (expt (partial ??i) ?n) (expt (partial ??j) ?m)) ?f)
;; Same idea, trickier when some accumulation has already occurred.
((expt (partial ??i) ?n) ((* (partial ??j) ??more) ?f))
=> ((* (expt (partial ??i) ?n) (partial ??j) ??more) ?f)
((partial ??i) ((* (expt (partial ??j) ?m) ??more) ?f))
=> ((* (partial ??i) (expt (partial ??j) ?m) ??more) ?f)
((expt (partial ??i) ?n) ((* (expt (partial ??j) ?m) ??more) ?f))
=> ((* (expt (partial ??i) ?n) (expt (partial ??j) ?m) ??more) ?f))
(r/guard
(fn [_] *commute-partials?*)
;; See [[*commute-partials?*]] above for a description of this
;; transformation.
;;
;; TODO in the predicate, implement `symb:elementary-access?` and make sure
;; that this only sorts if we can prove that we go all the way to the botom.
(r/rule
(((* ??xs (partial ??i) ??ys (partial ??j) ??zs) ?f) ??args)
(fn [{i '??i j '??j}]
(pos?
(compare (vec i) (vec j))))
(((* ??xs (partial ??j) ??ys (partial ??i) ??zs) ?f) ??args)))))
;; ## Trigonometric Rules
;;
;; the following rules are used to convert all trig expressions to ones
;; involving only sin and cos functions, and to make 1-arg atan into 2-arg atan.
(def trig->sincos
(rule-simplifier
(ruleset
(tan ?x) => (/ (sin ?x) (cos ?x))
(cot ?x) => (/ (cos ?x) (sin ?x))
(sec ?x) => (/ 1 (cos ?x))
(csc ?x) => (/ 1 (sin ?x))
(atan (/ ?y ?x)) => (atan ?y ?x)
(atan ?y) => (atan ?y 1))))
(def sincos->trig
(rule-simplifier
(ruleset
(/ (sin ?x) (cos ?x)) => (tan ?x)
(/ (* ??n1 (sin ?x) ??n2) (cos ?x))
=> (* ??n1 (tan ?x) ??n2)
(/ (sin ?x) (* ??d1 (cos ?x) ??d2))
=> (/ (tan ?x) (* ??d1 ??d2))
(/ (* ??n1 (sin ?x) ??n2)
(* ??d1 (cos ?x) ??d2))
=> (/ (* ??n1 (tan ?x) ??n2)
(* ??d1 ??d2)))))
(defn triginv [simplify]
(r/rule-simplifier
(let [sym:atan (sym/symbolic-operator 'atan)]
(r/guard
(fn [_] *aggressive-atan-simplify?*)
(r/rule
(atan ?y ?x)
(fn [{x '?x y '?y}]
(let [xs (simplify x)
ys (simplify y)]
(if (v/= ys xs)
(if (v/number? ys)
(if (g/negative? ys)
'(- (/ (* 3 pi) 4))
'(/ pi 4))
(and (ul/assume!
(list 'positive? xs)
'aggressive-atan-1)
'(/ pi 4)))
(if (and (v/number? ys)
(v/number? xs))
(sym:atan ys xs)
(let [s (simplify (list 'gcd ys xs))]
(when-not (v/one? s)
(and (ul/assume!
(list 'positive? s)
'aggressive-atan-2)
(let [yv (simplify (list '/ ys s))
xv (simplify (list '/ xs s))]
(r/template
(atan ~yv ~xv)))))))))))))
(ruleset
(sin (asin ?x)) => ?x
(cos (acos ?x)) => ?x
(tan (atan ?x)) => ?x
(sin (acos ?x)) => (sqrt (- 1 (expt ?x 2)))
(cos (asin ?y)) => (sqrt (- 1 (expt ?y 2)))
(tan (asin ?y)) => (/ ?y (sqrt (- 1 (expt ?y 2))))
(tan (acos ?x)) => (/ (sqrt (- 1 (expt ?x 2))) ?x)
(sin (atan ?a ?b))
=> (/ ?a (sqrt (+ (expt ?a 2) (expt ?b 2))))
(cos (atan ?a ?b))
=> (/ ?b (sqrt (+ (expt ?a 2) (expt ?b 2)))))
(r/guard
(fn [_] *inverse-simplify?*)
(ruleset
(asin (sin ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(ul/assume!
(r/template
(= (asin (sin ~xs)) ~xs))
'asin-sin)))
?x
(acos (cos ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(ul/assume!
(r/template
(= (acos (cos ~xs)) ~xs))
'acos-cos)))
?x
(atan (tan ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(ul/assume!
(r/template
(= (atan (tan ~xs)) ~xs))
'atan-tan)))
?x
(atan (sin ?x) (cos ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(ul/assume!
(r/template
(= (atan (sin ~xs) (cos ~xs)) ~xs))
'atan-sin-cos)))
?x
(asin (cos ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(ul/assume!
(r/template
(= (asin (cos ~xs))
(- (* (/ 1 2) pi) ~xs)))
'asin-cos)))
(- (* (/ 1 2) pi) ?x)
(acos (sin ?x))
(fn [{x '?x}]
(let [xs (simplify x)]
(ul/assume!
(r/template
(= (acos (sin ~xs))
(- (* (/ 1 2) pi) ~xs)))
'acos-sin)))
(- (* (/ 1 2) pi) ?x)))))