-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfinity.lisp
More file actions
2241 lines (2045 loc) · 86.1 KB
/
infinity.lisp
File metadata and controls
2241 lines (2045 loc) · 86.1 KB
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
(in-package :maxima)
(declare-top (special errorsw $radexpandr %pi-val expandflag exptrlsw rulesw timesp $ratinf))
;; ;some changes eg to timesin happened in Jan 2024. This was not updated..
;;; This file over-writes a bunch of programs from various
;;; files to implement 1/0 =oo, -1/0= -oo, 0/0 =indef, 0/-1 = -0,
;;; 0/1 would be +0 except we are just using the integer 0.
;;; see paper c:/papers/infinity.tex
(defun $rl()(load "d:/lisp/infinity.lisp")) ; temporarily, easier to reload
(defvar $ratinf t) ;; if t or true, then allow 1/0 etc etc
;; the header for rat objects, share it
(defvar *ratsimpind '(rat simp)) ;should be (get 'rat 'msimpind) but isn't
;; special forms.
;; First, infinity oo is 1/0
(defvar *ratinf `(,*ratsimpind 1 0))
;; negative infinity is -1/0
(defvar *ratminf `(,*ratsimpind -1 0))
;; indefinite is 0/0 but printed as indef
(defvar *ratind `(,*ratsimpind 0 0)) ;; indeterminate, unknown, ???
;; 1/(negative infinity) is minus 0, encoded as 0/-1,
;; the only rational number with a negative denominator.
(defvar *ratminuszero `(,*ratsimpind 0 -1))
;; test predicates, just in case we change the representation:
;; user callable.
(defun $ratinf_p (x)(alike1 x *ratinf)) ;; test for oo
(defun $ratminf_p (x)(alike1 x *ratminf)) ;; test for -oo
(defun $ratind_p (x)(alike1 x *ratind)) ;; test for 0/0
(defun $ratminuszero_p (x)(alike1 x *ratminuszero)) ;; test for 0/-1
;; what about +0? that's just the integer 0.
;; test for one of the extended numbers ... inf or indef or -0.
(defun $ratextended_p (x)
(and (consp x)(eq (caar x) 'rat)
(or
(eq (caddr x) 0) ;; a zero denominator means oo or -00 or indef
(eq (cadr x) 0)))) ;;alternatively 0/-1. Only way numerator is 0.
;; The remainder of this file is mostly
;; modified or re-written code from existing maxima source
;;
;; from simp.lisp
(defun zerop1 (x)
(or (and (integerp x) (= 0 x))
(and (floatp x) (= 0.0 x))
(and ($bfloatp x) (= 0 (second x)))
($ratminuszero_p x) ;; changed 3/1/2022 RJF for -0 as ((rat) 0 -1)
))
(defmfun simpexpt (x y z);; rewritten gr-> Base, pot ->Power from simp.lisp..
;; the major framework of this program dates from 1963!
(prog (Base Power check res rulesw w mlpBase mlpPower)
;; save the input;
;; if unchanged, return it to save memory/ share structure
(setq check x)
;; if z=t, then the parts of x are already simplified
(cond (z (setq Base (cadr x) Power (caddr x)) (go cont)))
;; x must look like ((mexpt Base Power). 2 arguments only.
(twoargcheck x)
(setq Base (simplifya (cadr x) nil)) ;; the BASE
(setq Power ;; the POWER
(let (($%enumer $numer))
;; Switch $%enumer on, when $numer is TRUE to allow
;; simplification of $%e to its numerical value.
(simplifya (if $ratsimpexpons ($ratsimp (caddr x)) (caddr x))
nil)))
;; This is the head of the main loop
;; we come back here after making some small changes to
;; continue processing
cont
;;
(cond (($ratp Power) ;; is Power an mrat (CRE, canonical rational expr.)
(setq Power (ratdisrep Power))
(go cont))
(($ratp Base) ;; if Base canonical rational expression or taylor
(cond ((member 'trunc (car Base) :test #'eq) ;truncated taylor series?
(return (srf (list '(mexpt) Base Power))))
((integerp Power)
(let ((varlist (caddar Base)) (genvar (cadddr (car Base))))
(return (ratrep* (list '(mexpt) Base Power)))))
(t ;not integer power, so disrep it, try again.
(setq Base (ratdisrep Base))
(go cont))))
;; maybe we have A^B where A or B
;; is a matrix or list (i.e. vector)
((or (setq mlpBase (mxorlistp Base))
(setq mlpPower (mxorlistp Power)))
(go matrix))
((onep1 Power) (go atBase));; a^1
;
((or (zerop1 Power) (onep1 Base))
;;a^0 a^0.0 or a^0.0b0 OR 1^a or 1.0^a ...
(go retno))
;; This next section of code tries to handle 0^a more completely
;; If a is explicit real and positive, 0^a is 0, 0.0, 0.0b0,
;; If a is explicit real and negative, 0^a is indef.
;; If a is explicit and complex, look at sign of realpart(a).
;; If a is not known enough to find its sign,
;; return an unchanged 0^a.
;; The handling of the flag *zexptsimp? is not changed.
;;; uh, dunno about DK and old comment below RJF 3/8/22
;; Reverting the return of an unsimplified 0^a, because timesin
;; can not handle such expressions. (DK 02/2010)
;; Note that if we got here, power is not zero.
((zerop1 Base)
;;;*****
;; 0 to a non-zero power. [else we use previous clause]
;; Base could be ((rat) 0 -1) i.e. -0. or just 0, 0.0, 0.0b0
(cond ((or (member (setq z ($csign Power)) '($neg $nz)) ;Power<=0
(and *zexptsimp? (eq ($asksign Power) '$neg)))
;; A negative exponent. Maxima error.?
(return (cond
($ratinf ;; are we using rat nums for oo, -oo,?
;; (format t "~%Power=~s Base=~s ratinf=~s ~%" Power Base $ratinf)
(cond
(($ratminuszero_p Base) ; it is zero, but -0
;; (-0)^(-1) to -1 power is minus inf..
(cond ((zerop1 Power) 1) ;;(-0)^0 is 1
(($evenp Power) 0) ;e.g. (-0)^2
(($oddp Power) *ratminuszero) ; e.g.(-0)^3
(t
(list (get 'mexpt 'msimpind) ;; don't know
;;Base is 0/-1
(list '($signum) Power)))))
;; zero but not -0
(t(list (get 'mexpt 'msimpind) ;; don't know
;;Base is 0
(list '($signum) Power)))))
;; $ratinf not t, return symbolic 0^a
;;
;;reminder.. Base is zero.
;; Power is NOT zero, also not a number
(t (list (get 'mexpt 'msimpind) ;; don't know
Base
(list '($signum) Power)))))))
(defun newvarmexpt (x e flag)
;; WHEN FLAG IS T, CALL RETURNS RATFORM
(prog (topexp)
(when (and (integerp e) (not flag))
(return (newvar1 (cadr x))))
;; only change is the line below.
(when (equal '(0 . 0) (cdadr x))(return '(0 . 0))) ;; any power of indef
(setq topexp 1)
top (cond
;; X=B^N FOR N A NUMBER
((integerp e)
(setq topexp (* topexp e))
(setq x (cadr x)))
((atom e) nil)
;; X=B^(P/Q) FOR P AND Q INTEGERS
((eq (caar e) 'rat)
(cond ((or (minusp (cadr e)) (> (cadr e) 1))
(setq topexp (* topexp (cadr e)))
(setq x (list '(mexpt)
(cadr x)
(list '(rat) 1 (caddr e))))))
(cond ((or flag (numberp (cadr x)) ))
(*ratsimp*
(cond ((memalike x radlist) (return nil))
(t (setq radlist (cons x radlist))
(return (newvar1 (cadr x))))) )
($algebraic (newvar1 (cadr x)))))
;; X=B^(A*C)
((eq (caar e) 'mtimes)
(cond
((or
;; X=B^(N *C)
(and (atom (cadr e))
(integerp (cadr e))
(setq topexp (* topexp (cadr e)))
(setq e (cddr e)))
;; X=B^(P/Q *C)
(and (not (atom (cadr e)))
(eq (caaadr e) 'rat)
(not (equal 1 (cadadr e)))
(setq topexp (* topexp (cadadr e)))
(setq e (cons (list '(rat)
1
(caddr (cadr e)))
(cddr e)))))
(setq x
(list '(mexpt)
(cadr x)
(setq e (simplify (cons '(mtimes)
e)))))
(go top))))
;; X=B^(A+C)
((and (eq (caar e) 'mplus) expsumsplit) ;SWITCH CONTROLS
(setq ;SPLITTING EXPONENT
x ;SUMS
(cons
'(mtimes)
(mapcar
#'(lambda (ll)
(list '(mexpt)
(cadr x)
(simplify (list '(mtimes)
topexp
ll))))
(cdr e))))
(cond (flag (return (prep1 x)))
(t (return (newvar1 x))))))
(cond (flag nil)
((equal 1 topexp)
(cond ((or (atom x)
(not (eq (caar x) 'mexpt)))
(newvar1 x))
((or (memalike x varlist) (memalike x vlist))
nil)
(t (cond ((or (atom x) (null *fnewvarsw))
(putonvlist x))
(t (setq x (littlefr1 x))
(mapc #'newvar1 (cdr x))
(or (memalike x vlist)
(memalike x varlist)
(putonvlist x)))))))
(t (newvar1 x)))
(return
(cond
((null flag) nil)
((equal 1 topexp)
(cond
((and (not (atom x)) (eq (caar x) 'mexpt))
(cond ((assolike x genpairs))
;; *** SHOULD ONLY GET HERE IF CALLED FROM FR1. *FNEWVARSW=NIL
(t (setq x (littlefr1 x))
(cond ((assolike x genpairs))
(t (newsym x))))))
(t (prep1 x))))
(t (ratexpt (prep1 x) topexp))))))
;;; the rest of this, uh dunno..
;; base is 0, just give error
((not errorsw) (merror (intl:gettext "expt: undefined: 0 to a negative exponent.")))
(t (throw 'errorsw t))
;; base is still zero.
((and (member z '($complex $imaginary))
;; A complex exponent. Look at the sign of the realpart.
(member (setq z ($sign ($realpart Power)))
'($neg $nz $zero)))
(cond ((not errorsw)
(merror (intl:gettext "expt: undefined: 0 to a complex exponent.")))
(t (throw 'errorsw t))))
((and *zexptsimp? (eq ($asksign Power) '$zero))
(cond ((not errorsw)
(merror (intl:gettext "expt: undefined: 0^0")))
(t (throw 'errorsw t))))
((not (member z '($pos $pz)))
;; The sign of realpart(Power) is not known. We can not return
;; an unsimplified 0^a expression, because timesin can not
;; handle it. We return ZERO. That is the old behavior.
;; Look for the imaginary symbol to be consistent with
;; old code.Huh/RJF
(cond ((not (free Power '$%i))
(cond ((not errorsw)
(merror (intl:gettext "expt: undefined: 0 to a complex exponent.")))
(t (throw 'errorsw t))))
(t
;; Return ZERO and not an unsimplified expression.
(return (zerores Base Power)))
))
(t (return (zerores Base Power))))
;;; BASE is NOT zero
((and (mnump Base)
(mnump Power)
(or (not (ratnump Base)) (not (ratnump Power))))
(return (eqtest (exptrl Base Power) check)))
;; Check for numerical evaluation of the sqrt.
((and (alike1 Power '((rat) 1 2))
(or (setq res (flonum-eval '%sqrt Base))
(and (not (member 'simp (car x) :test #'eq))
(setq res (big-float-eval '%sqrt Base)))))
(return res))
((eq Base '$%i)
(return (%itopot Power)))
((and (realp Base) (minusp Base) (mevenp Power))
(setq Base (- Base))
(go cont))
((and (realp Base) (minusp Base) (moddp Power))
(return (mul2 -1 (power (- Base) Power))))
((and (equal Base -1) (maxima-integerp Power) (mminusp Power))
(setq Power (neg Power))
(go cont))
((and (equal Base -1)
(maxima-integerp Power)
(mtimesp Power)
(= (length Power) 3)
(fixnump (cadr Power))
(oddp (cadr Power))
(maxima-integerp (caddr Power)))
(setq Power (caddr Power))
(go cont))
((atom Base) (go atBase))
((and (eq (caar Base) 'mabs)
(evnump Power)
(or (and (eq $domain '$real) (not (decl-complexp (cadr Base))))
(and (eq $domain '$complex) (decl-realp (cadr Base)))))
(return (power (cadr Base) Power)))
((and (eq (caar Base) 'mabs)
(integerp Power)
(oddp Power)
(not (equal Power -1))
(or (and (eq $domain '$real) (not (decl-complexp (cadr Base))))
(and (eq $domain '$complex) (decl-realp (cadr Base)))))
;; abs(x)^(2*n+1) -> abs(x)*x^(2*n), n an integer number
(if (plusp Power)
(return (mul (power (cadr Base) (add Power -1))
Base))
(return (mul (power (cadr Base) (add Power 1))
(inv Base)))))
((eq (caar Base) 'mequal)
(return (eqtest (list (ncons (caar Base))
(power (cadr Base) Power)
(power (caddr Base) Power))
Base)))
((symbolp Power) (go opp))
((eq (caar Base) 'mexpt) (go e1))
((and (eq (caar Base) '%sum)
$sumexpand
(integerp Power)
(signp g Power)
(< Power $maxposex))
(return (do ((i (1- Power) (1- i))
(an Base (simptimes (list '(mtimes) an Base) 1 t)))
((signp e i) an))))
((equal Power -1)
(return (eqtest (testt (tms Base Power nil)) check)))
((fixnump Power)
(return (eqtest (cond ((and (mplusp Base)
(not (or (> Power $expop)
(> (- Power) $expon))))
(expandexpt Base Power))
(t (simplifya (tms Base Power nil) t)))
check))))
opp
(cond ((eq (caar Base) 'mexpt) (go e1))
((eq (caar Base) 'rat)
(return (mul2 (power (cadr Base) Power)
(power (caddr Base) (mul2 -1 Power)))))
((not (eq (caar Base) 'mtimes)) (go up))
((or (eq $radexpand '$all) (and $radexpand (simplexpon Power)))
(setq res (list 1))
(go start))
((and (or (not (numberp (cadr Base)))
(equal (cadr Base) -1))
(equal -1 ($num Base)) ; only for -1
;; Do not simplify for a complex base.
(not (member ($csign Base) '($complex $imaginary)))
(and (eq $domain '$real) $radexpand))
;; (-1/x)^a -> 1/(-x)^a for x negative
;; For all other cases (-1)^a/x^a
(if (eq ($csign (setq w ($denom Base))) '$neg)
(return (inv (power (neg w) Power)))
(return (div (power -1 Power)
(power w Power)))))
((or (eq $domain '$complex) (not $radexpand)) (go up)))
(return (do ((l (cdr Base) (cdr l)) (res (ncons 1)) (rad))
((null l)
(cond ((equal res '(1))
(eqtest (list '(mexpt) Base Power) check))
((null rad)
(testt (cons '(mtimes simp) res)))
(t
(setq rad (power* ; RADEXPAND=()?
(cons '(mtimes) (nreverse rad)) Power))
(cond ((not (onep1 rad))
(setq rad
(testt (tms rad 1 (cons '(mtimes) res))))
(cond (rulesw
(setq rulesw nil res (cdr rad))))))
(eqtest (testt (cons '(mtimes) res)) check))))
;; Check with $csign to be more complete. This prevents wrong
;; simplifications like sqrt(-z^2)->%i*sqrt(z^2) for z complex.
(setq z ($csign (car l)))
(if (member z '($complex $imaginary))
(setq z '$pnz)) ; if appears complex, unknown sign
(setq w (cond ((member z '($neg $nz) :test #'eq)
(setq rad (cons -1 rad))
(mult -1 (car l)))
(t (car l))))
(cond ((onep1 w))
((alike1 w Base) (return (list '(mexpt simp) Base Power)))
((member z '($pn $pnz) :test #'eq)
(setq rad (cons w rad)))
(t
(setq w (testt (tms (simplifya (list '(mexpt) w Power) t)
1 (cons '(mtimes) res))))))
(cond (rulesw (setq rulesw nil res (cdr w))))))
start
(cond ((and (cdr res) (onep1 (car res)) (ratnump (cadr res)))
(setq res (cdr res))))
(cond ((null (setq Base (cdr Base)))
(return (eqtest (testt (cons '(mtimes) res)) check)))
((mexptp (car Base))
(setq y (list (caar Base) (cadar Base) (mult (caddar Base) Power))))
((eq (car Base) '$%i)
(setq y (%itopot Power)))
((mnump (car Base))
(setq y (list '(mexpt) (car Base) Power)))
(t (setq y (list '(mexpt simp) (car Base) Power))))
(setq w (testt (tms (simplifya y t) 1 (cons '(mtimes) res))))
(cond (rulesw (setq rulesw nil res (cdr w))))
(go start)
retno ;; might be 0^0
(return (exptrl Base Power))
atBase
(cond ((zerop1 Power) (go retno)) ; 1^0 , 1.0^0.0 etc
((onep1 Power)
(let ((y (mget Base '$numer)))
(if (and y (floatp y) (or $numer (not (equal Power 1))))
;; A numeric constant like %e, %pi, ... and
;; exponent is a float or bigfloat value.
(return (if (and (member Base *builtin-numeric-constants*)
(equal Power bigfloatone))
;; Return a bigfloat value.
($bfloat Base)
;; Return a float value.
y))
;; In all other cases exptrl simplifies accordingly.
(return (exptrl Base Power)))))
((eq Base '$%e)
(when (and $ratinf (ratnump Power)(= 0 (caddr Power))) ;; exp(0/0), 1/0, -1/0 RJF
(return (case (cadr Power) (0 *ratind)(-1 0)(1 *ratinf))))
;; Numerically evaluate if the power is a flonum.
(when $%emode
(let ((val (flonum-eval '%exp Power)))
(when val
(return val)))
;; Numerically evaluate if the power is a (complex)
;; big-float. (This is basically the guts of
;; big-float-eval, but we can't use big-float-eval.)
(when (and (not (member 'simp (car x) :test #'eq))
(complex-number-p Power 'bigfloat-or-number-p))
(let ((x ($realpart Power))
(y ($imagpart Power)))
(cond ((and ($bfloatp x) (like 0 y))
(return ($bfloat `((mexpt simp) $%e ,Power))))
((or ($bfloatp x) ($bfloatp y))
(let ((z (add ($bfloat x) (mul '$%i ($bfloat y)))))
(setq z ($rectform `((mexpt simp) $%e ,z)))
(return ($bfloat z))))))))
(cond ((and $logsimp (among '%log Power)) (return (%etolog Power)))
((and $demoivre (setq z (demoivre Power))) (return z))
((and $%emode
(among '$%i Power)
(among '$%pi Power)
;; Exponent contains %i and %pi and %emode is TRUE:
;; Check simplification of exp(%i*%pi*p/q*x)
(setq z (%especial Power)))
(return z))
(($taylorp (third x))
;; taylorize %e^taylor(...)
(return ($taylor x)))))
(t
(let ((y (mget Base '$numer)))
;; Check for a numeric constant.
(and y
(floatp y)
(or (floatp Power)
;; The exponent is a bigfloat. Convert base to bigfloat.
(and ($bfloatp Power)
(member Base *builtin-numeric-constants*)
(setq y ($bfloat Base)))
(and $numer (integerp Power)))
(return (exptrl y Power))))))
up
(return (eqtest (list '(mexpt) Base Power) check))
matrix
(cond ((zerop1 Power)
(cond ((mxorlistp1 Base) (return (constmx (addk 1 Power) Base)))
(t (go retno))))
((onep1 Power) (return Base))
((or $doallmxops $doscmxops $domxexpt)
(cond ((or (and mlpBase
(or (not ($listp Base)) $listarith)
(scalar-or-constant-p Power $assumescalar))
(and $domxexpt
mlpPower
(or (not ($listp Power)) $listarith)
(scalar-or-constant-p Base $assumescalar)))
(return (simplifya (outermap1 'mexpt Base Power) t)))
(t (go up))))
((and $domxmxops (member Power '(-1 -1.0) :test #'equal))
(return (simplifya (outermap1 'mexpt Base Power) t)))
(t (go up)))
e1
;; At this point we have an expression: (z^a)^b with Base = z^a and Power = b
(cond ((or (eq $radexpand '$all)
;; b is an integer or an odd rational
(simplexpon Power)
(and (eq $domain '$complex)
(not (member ($csign (caddr Base)) '($complex $imaginary)))
;; z >= 0 and a not a complex
(or (member ($csign (cadr Base)) '($pos $pz $zero))
;; -1 < a <= 1
(and (mnump (caddr Base))
(eq ($sign (sub 1 (take '(mabs) (caddr Base))))
'$pos))))
(and (eq $domain '$real)
(member ($csign (cadr Base)) '($pos $pz $zero)))
;; (1/z)^a -> 1/z^a when z a constant complex
(and (eql (caddr Base) -1)
(or (and $radexpand
(eq $domain '$real))
(and (eq ($csign (cadr Base)) '$complex)
($constantp (cadr Base)))))
;; This does (1/z)^a -> 1/z^a. This is in general wrong.
;; We switch this type of simplification on, when
;; $ratsimpexpons is T. E.g. radcan sets this flag to T.
;; radcan hangs for expressions like sqrt(1/(1+x)) without
;; this simplification.
(and $ratsimpexpons
(equal (caddr Base) -1))
(and $radexpandr
(eq $domain '$real)
(odnump (caddr Base))))
;; Simplify (z^a)^b -> z^(a*b)
(setq Power (mul Power (caddr Base))
Base (cadr Base)))
((and (eq $domain '$real)
(free Base '$%i)
$radexpand
(not (decl-complexp (cadr Base)))
(evnump (caddr Base)))
;; Simplify (x^a)^b -> abs(x)^(a*b)
(setq Power (mul Power (caddr Base))
Base (radmabs (cadr Base))))
((and $radexpand
(eq $domain '$real)
(mminusp (caddr Base)))
;; Simplify (1/z^a)^b -> 1/(z^a)^b
(setq Power (neg Power)
Base (power (cadr Base) (neg (caddr Base)))))
(t (go up)))
(go cont)))
(defun addk (x y)
(cond ((eql x 0) y)
((eql y 0) x)
((and (numberp x) (numberp y))
#+ignore (cl-or-bfloat-binary-op 'mplus '+ x y)
(+ x y)) ;just add them
((or ($bfloatp x) ($bfloatp y)) ($bfloat (list '(mplus) x y)))
(t (prog (g a b)
(cond ((numberp x)
(cond ((floatp x) (return (+ x (fpcofrat y))))
(t (setq x (list '(rat) x 1)))))
((numberp y)
(cond ((floatp y) (return (+ y (fpcofrat x))))
(t (setq y (list '(rat) y 1))))))
(setq g (gcd (caddr x) (caddr y)))
(cond ((= (caddr x) 0)
(cond((= (cadr x) 0)(return x))
((= (caddr y) 0)
(return (if (equal x y) x ; oo+oo or -oo-oo
*ratind))))) ;oo-oo is und
((= (caddr y) 0) ; return oo or -oo or und
(return y)))
;; above clause only change 12/2015 rjf
(setq a (truncate (caddr x) g)
b (truncate (caddr y) g))
(return (timeskl (list '(rat) 1 g)
(list '(rat)
(+ (* (cadr x) b)
(* (cadr y) a))
(* a b))))))))
(defun partition_ex_lisp ;; hacked, simplified for this purpose
(pred e)
(let ((yes nil)(no nil))
(map nil
#'(lambda(r)
(if (funcall pred r)
(setf yes (cons r yes))
; r fails predicate
(setf no (cons r no))))
e)
(cons yes no)))
(defun simptimes_ratinf (x)
;; here we combine floats, integers, ((rat) n d), bfloats, ((rat) n 0)
;; maybe common lisp rats, complexes, intervals.
;; into one item. e.g. ((mtimes) 0 ((rat) 1 0) $r $s)
;; becomes ((mtimes)((rat) 0 0) $r $s)
;; phew
;;first pass, look for ANY ((rat *) * 0)
(if (notany #'$ratextended_p
(cdr x))
x ;; no rational infinities or 0/0. quickest check, usually comes out here
(let ((h (partition_ex_lisp #'(lambda(r)(or (mnump r);; 1, 1.0, 1/2, 0/0, 1/0 also
(and (consp r)
(eq (caar r)'mrat))))
(cdr x))))
;; h is a list: (yes . no ) ; yes is a list of pieces like 1/0, 0/0. no is rest
(format t"~% h is ~s. ~%yes= ~s,~% no= ~s~%" h (car h)(cdr h) )
(if (null(cdar h))
#+ignore (mul (caar h)($signum (cons '(mtimes) (cdr h))))
; zero or only one item in yes part
; just return same value
;; above line breaks.
x
(infprod_hackit (car h)(cdr h))))))
;; nums is a list of 2 or more items.
;; look for at least one 0/0 --> 0.
;; look for at least one n/0 * 0 --> 0/0.
;; (-1/0)^n is 1/0 if n is even. but -3* (1/0) is -1/0 also.
;; need to check sign of all numbers.
(defun ratinfp(s)(and (consp s)
(eq (caar s) 'rat)
(equal (caddr s) 0)))
(defun infprod_hackit (nums rest)
(let ((poscount 0)(indefcount 0) (zerocount 0) (sign 0) (mrat nil))
;;counts of +oo -oo 0/0 0
;; mrat = t if the item encountered is mrat encoded not rat.
;; need to consider encoding -0, as one bonus.
;;sign
(loop for s in nums do
(cond((ratinfp s) ;denom is zero, look at numerator
(case (cadr s) ; numerator of ((rat) n d) is cadr
(1 (incf poscount))
(-1 (incf sign))
(0 (incf indefcount))))
;; the mrat case
((and (consp s)
(eq (caar s) 'mrat)
(equal (cddr s) 0)) ;denom is zero, look at numerator
(setq mrat t)
(case (cadr s) ;; the numerator of ((mrat) . ( n . d))
(1 (incf poscount))
(-1 (incf sign))
(0 (incf indefcount))))
;; not an infinity or indefinite
((zerop1 s) ; 0 or 0.0 or 0.0b0
(incf zerocount))
;leftover numbers, possibly bigfloat
((and (mnump s)(mgrp 0 s)) (incf sign))))
;; (format t "~% pos=~s indef=~s zer=~s sign=~s" poscount indefcount zerocount sign)
;; here we make it an mrat if we encountered an mrat inf or 0/0
(if mrat
(list '(mtimes) (cons '(mrat simp nil nil) ; return mrat of oo -oo or 0/0
(cons
; new numerator is..
(if (or (> indefcount 0)(> zerocount 0)) ;0/0 or 0* infinity
0
(expt -1 sign))
; (-1/0)^negcount
0)))
(cons '(mtimes) ; return product: (rat of oo -oo or 0/0)* rest
(cons
(cond ((or (> indefcount 0)(> zerocount 0)) ;0/0 or 0* infinity
*ratind)
(t (list *ratsimpind (expt -1 sign) 0))) ; (-1/0)^negcount
rest)))))
(defmfun simptimes (x w z) ; W must be 1
(prog (res check eqnflag matrixflag sumflag)
(if (null (cdr x)) (return 1))
(setq check x)
(if $ratinf (setf x (simptimes_ratinf x)))
start
(setq x (cdr x))
(cond ;;added 1/2016 .
#+ignore
((and(consp res)
; (prog nil (format t "~%start res=~s x=~s" res x) t)
(member 0 (cdr res) :test #'(lambda(r s) ; car of res is (mtimes)
(and (consp s)
(eq (caar s) 'rat)
(= (caddr s) 0) ; denom is zero
)))
(member 0 x))
; (format t "~%DONE! res=~s x=~s" res x)
(if (member 0 x) (return *ratind) ;; 0*(1/0). ;;; not there yet
))
((zerop1 res)
;;; added 1/2016 RJF
#+ignore
(if (member 0 x :test #'(lambda(r s)
(and (consp s)
(eq (caar s) 'rat)
(= (caddr s) 0) ; denom is zero
))) (return *ratind))
(cond ($mx0simp
(cond ((and matrixflag (mxorlistp1 matrixflag))
(return (constmx res matrixflag)))
(eqnflag (return (list '(mequal simp)
(mul2 res (cadr eqnflag))
(mul2 res (caddr eqnflag)))))
(t
(dolist (u x)
(cond ((mxorlistp u)
(return (setq res (constmx res u))))
((and (mexptp u)
(mxorlistp1 (cadr u))
($numberp (caddr u)))
(return (setq res (constmx res (cadr u)))))
((mequalp u)
(return
(setq res
(list '(mequal simp)
(mul2 res (cadr u))
(mul2 res (caddr u))))))))))))
(return res))
((null x) (go end)))
(setq w (if z (car x) (simplifya (car x) nil)))
st1
(cond ((atom w) nil)
((eq (caar w) 'mrat)
(cond ((or eqnflag matrixflag
(and sumflag
(not (member 'trunc (cdar w) :test #'eq)))
(spsimpcases (cdr x) w))
(setq w (ratdisrep w))
(go st1))
(t
(return
(ratf (cons '(mtimes)
(nconc (mapcar #'simplify (cons w (cdr x)))
(cdr res))))))))
((eq (caar w) 'mequal)
(setq eqnflag
(if (not eqnflag)
w
(list (car eqnflag)
(mul2 (cadr eqnflag) (cadr w))
(mul2 (caddr eqnflag) (caddr w)))))
(go start))
((member (caar w) '(mlist $matrix) :test #'eq)
(setq matrixflag
(cond ((not matrixflag) w)
((and (or $doallmxops $domxmxops $domxtimes)
(or (not (eq (caar w) 'mlist)) $listarith)
(not (eq *inv* '$detout)))
(stimex matrixflag w))
(t (setq res (tms w 1 res)) matrixflag)))
(go start))
((and (eq (caar w) '%sum) $sumexpand)
(setq sumflag (sumtimes sumflag w))
(go start)))
(setq res (tms w 1 res))
(go start)
end
(cond ((mtimesp res) (setq res (testt res))))
(cond (sumflag (setq res (cond ((or (null res) (equal res 1)) sumflag)
((not (mtimesp res))
(list '(mtimes) res sumflag))
(t (nconc res (list sumflag)))))))
(cond ((or (atom res)
(not (member (caar res) '(mexpt mtimes) :test #'eq))
(and (zerop $expop) (zerop $expon))
expandflag))
((eq (caar res) 'mtimes) (setq res (expandtimes res)))
((and (mplusp (cadr res))
(fixnump (caddr res))
(not (or (> (caddr res) $expop)
(> (- (caddr res)) $expon))))
(setq res (expandexpt (cadr res) (caddr res)))))
(cond (matrixflag
(setq res
(cond ((null res) matrixflag)
((and (or ($listp matrixflag)
$doallmxops
(and $doscmxops
(not (member res '(-1 -1.0) :test #'equal)))
;; RES should only be -1 here (not = 1)
(and $domxmxops
(member res '(-1 -1.0) :test #'equal)))
(or (not ($listp matrixflag)) $listarith))
(mxtimesc res matrixflag))
(t (testt (tms matrixflag 1 (tms res 1 nil))))))))
(if res (setq res (eqtest res check)))
(return (cond (eqnflag
(if (null res) (setq res 1))
(list (car eqnflag)
(mul2 (cadr eqnflag) res)
(mul2 (caddr eqnflag) res)))
(t res)))))
;; modifies product to include factor^power
#+ignore
(defun tms (factor power product &aux tem)
(prog ((rulesw nil)
(z nil))
(when (mplusp product) (setq product (list '(mtimes simp) product)))
(cond ((zerop1 factor) ;; 0^power
(cond ((mnegp power) ;; 0^negative_power)
(cond ($ratinf (return *ratind)) ;indefinite
(t(if errorsw
(throw 'errorsw t)
(merror (intl:gettext "Division by 0"))))))
(t factor)))
((and $ratinf (alike1 factor *ratind))(return *ratind))
((and $ratinf (alike1 factor *ratminf)) ;;(-oo)^power
(return(cond (($evenp power) *ratinf)
(($oddp power) *ratminf)
(t (list (get 'mexpt 'msimpind)
factor power))))
;;;
)
((and $ratinf (alike1 factor *ratinf)) ;; oo^power
(return *ratinf))
((and $ratinf (alike1 factor *ratminuszero))
(return
(cond ((zerop1 power) 1) ;;(-0)^0 is 1
(($evenp power) 0)
(($oddp power) *ratminuszero)
(t (list (get 'mexpt 'msimpind)
factor power)))
)
)
((and (null product)
(or (and (mtimesp factor) (equal power 1))
(and (setq product (list '(mtimes) 1)) nil)))
(setq tem (append '((mtimes)) (if (mnump (cadr factor)) nil '(1))
(cdr factor) nil))
(if (= (length tem) 1)
(setq tem (copy-list tem))
tem))
((mtimesp factor)
(do ((factor-list (cdr factor) (cdr factor-list)))
((or (null factor-list) (zerop1 product)) product)
(setq z (timesin (car factor-list) (cdr product) power))
(when rulesw
(setq rulesw nil)
(setq product (tms-format-product z)))))
(t
(setq z (timesin factor (cdr product) power))
(if rulesw
(tms-format-product z)
product)))))
;;original tms
(defun tms (factor power product &aux tem)
(let ((rulesw nil)
(z nil))
(when (mplusp product) (setq product (list '(mtimes simp) product)))
(cond ((zerop1 factor)
(cond ((mnegp power)
(if errorsw
(throw 'errorsw t)
(merror (intl:gettext "Division by 0"))))
(t factor)))
((and (null product)
(or (and (mtimesp factor) (equal power 1))
(and (setq product (list '(mtimes) 1)) nil)))
(setq tem (append '((mtimes)) (if (mnump (cadr factor)) nil '(1))
(cdr factor) nil))
(if (= (length tem) 1)
(setq tem (copy-list tem))
tem))
((mtimesp factor)
(do ((factor-list (cdr factor) (cdr factor-list)))
((or (null factor-list) (zerop1 product)) product)
(setq z (timesin (car factor-list) (cdr product) power))
(when rulesw
(setq rulesw nil)
(setq product (tms-format-product z)))))
(t
(setq z (timesin factor (cdr product) power))
(if rulesw
(tms-format-product z)
product)))))
;; we do need this change.??
(defun *red (n d)
(cond ((zerop n)(if $ratinf (list *ratsimpind
0 (if (numberp d)(cond ((< d 0) -1)((= d 0) 0)(t 1))
0)) 0)) ; 0/x is 0. 0/-5 is -0 1/2015 rjf
((equal d 1) n)
((equal d 0) (list *ratsimpind (signum n) d)) ;; changed 12/2015 rjf
(t (let ((u (gcd n d)))
(setq n (truncate n u)
d (truncate d u))
(if (minusp d) (setq n (- n) d (- d)))
(cond ((equal d 1) n)
($float (fpcofrat1 n d))
(t (list *ratsimpind n d)))))))
(defun simpmin (x vestigial z)
(declare (ignore vestigial))
(cond ((null (cdr x)) 0)
((equal 0 (cadr x)) `(,*ratsimpind 0 -1)) ;rjf 1/2016 -0
((null (cddr x))
(mul -1 (simplifya (cadr x) z)))
(t
;; ((mminus) a b ...) -> ((mplus) a ((mtimes) -1 b) ...)
(sub (simplifya (cadr x) z) (addn (cddr x) z)))))
(defmfun simpquot (x y z)
(twoargcheck x)
(cond ((and (integerp (cadr x)) (integerp (caddr x)) (not (zerop (caddr x))))
(*red (cadr x) (caddr x)))
((and (numberp (cadr x)) (numberp (caddr x)) (not (zerop (caddr x))))
(/ (cadr x)(caddr x))
#+ignore (cl-or-bfloat-binary-op 'mquotient '/ (cadr x) (caddr x))
)
(t (setq y (simplifya (cadr x) z))
(setq x (simplifya (list '(mexpt) (caddr x) -1) z))
(if (equal y 1) x (simplifya (list '(mtimes) y x) t)))))
(defmfun exptrl (r1 r2) ;compute r1^r2, both numbers of some sort
(cond ((equal r2 1) r1)
((equal r2 1.0)
(cond ((mnump r1) (addk 0.0 r1))
;; Do not simplify the type of the number away.
(t (list '(mexpt simp) r1 1.0))))
((equal r2 bigfloatone)
(cond ((mnump r1) ($bfloat r1))
;; Do not simplify the type of the number away.
(t (list '(mexpt simp) r1 bigfloatone))))
((zerop1 r1)
(cond
;; changed 1/2016 RJF
((or (zerop1 r2) (mnegp r2))
(cond #+ignore($ratinf *ratind) ;; this is RJF possible change.
;; for instance, mathematica says Indeterminate.
($ratinf 1) ;; or this? Knuth says 1 2/22/22
((not errorsw)
(merror (intl:gettext "expt: undefined: ~M") (list '(mexpt) r1 r2)))
(t (throw 'errorsw t))))