-
Notifications
You must be signed in to change notification settings - Fork 0
/
risch.lisp
1230 lines (1159 loc) · 52 KB
/
risch.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
;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; The data in this file contains enhancments. ;;;;;
;;; ;;;;;
;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
;;; All rights reserved ;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(in-package :maxima)
(macsyma-module risch)
(load-macsyma-macros rzmac ratmac)
(declare-top (special prob rootfac parnumer pardenom logptdx wholepart
$ratalgdenom expexpflag $logsimp switch1 degree cary
$ratfac $logexpand ratform genvar *var var rootfactor
expint $keepfloat trigint operator $exponentialize $gcd
$logarc changevp klth r s beta gamma b mainvar expflag
expstuff liflag intvar switch varlist nogood genvar
$erfflag $liflag rischp $factorflag alphar m simp
genpairs hypertrigint *mosesflag yyy *exp y $algebraic
implicit-real errrjfflag $%e_to_numlog generate-atan2
context bigfloatzero rp-polylogp *in-risch-p* distint))
(defmvar $liflag t "Controls whether `risch' generates polylogs")
(defmvar $erfflag t "Controls whether `risch' generates `erfs'")
(defvar changevp t "When nil prevents changevar hack")
(defmacro pair (al bl) `(mapcar #'cons ,al ,bl))
;; internal representation of risch expressions: list with canonical rational
;; expression (CRE) as first element, standard maxima expressions as remaining
;; elements. risch expression is sum of CRE and remaining elements.
(defmacro rischzero () ''((0 . 1) 0))
(defun rischnoun (exp1 &optional (exp2 exp1 exp2p))
(unless exp2p (setq exp1 (rzero)))
`(,exp1 ((%integrate) ,(disrep exp2) ,intvar)))
(defun getrischvar ()
(do ((vl varlist (cdr vl))
(gl genvar (cdr gl)))
((null (cdr vl)) (car gl))))
;; test whether CRE p is constant with respect to variable of integration.
;; requires variables in varlist and genvar
;; to be ordered as by intsetup, with var of integration ordered before
;; any other expressions that contain it.
(defun risch-pconstp (p)
(or (pcoefp p) (pointergp mainvar (car p))))
(defun risch-constp (r)
(setq r (ratfix r))
(and (risch-pconstp (car r)) (risch-pconstp (cdr r))))
;; adds two risch expressions (defined above).
(defun rischadd (x y)
(destructuring-let (((a . b) x) ((c . d) y))
(cons (r+ a c) (append b d))))
(defmfun $risch (exp var)
(let ((*integrator-level* 0))
(declare (special *integrator-level*))
(with-new-context (context)
(rischint exp var))))
(defun spderivative (p var)
(cond ((pcoefp p) '(0 . 1))
((null (cdr p)) '(0 . 1))
((or (not (atom (car p))) (numberp (car p))) ;P IS A RATFORM
(let ((denprime (spderivative (cdr p) var)))
(cond ((rzerop denprime)
(ratqu (spderivative (car p) var) (cdr p)))
(t (ratqu (r- (r* (spderivative (car p) var)
(cdr p))
(r* (car p) denprime))
(r* (cdr p) (cdr p)))))))
(t (r+ (spderivative1 (car p)
(cadr p)
(caddr p)
var)
(spderivative (cons (car p) (cdddr p))
var)))))
(defun spderivative1 (var1 deg coeff var)
(cond ((eq var1 var)
(r* (ratexpt (cons (list var 1 1) 1) (1- deg))
(pctimes deg coeff)))
((pointergp var var1) '(0 . 1))
((equal deg 0) (spderivative coeff var))
(t (r+ (r* (ratexpt (cons (list var1 1 1) 1) deg)
(spderivative coeff var))
(r* (cond ((equal deg 1) coeff)
(t (r* deg
coeff
(ratexpt (cons (list var1 1 1) 1)
(1- deg)))))
(get var1 'rischdiff) )))))
(defun polylogp (exp &optional sub)
(and (mqapplyp exp) (eq (subfunname exp) '$li)
(or (null sub) (equal sub (car (subfunsubs exp))))))
(defun rischint (exp intvar &aux ($logarc nil) ($exponentialize nil)
($gcd '$algebraic) ($algebraic t) (implicit-real t)
;; The risch integrator expects $logexpand T. Otherwise,
;; the integrator hangs for special types of integrals
;; (See bug report ID:3039452)
($logexpand t))
(prog ($%e_to_numlog $logsimp trigint operator y z var ratform liflag
mainvar varlist genvar hypertrigint $ratfac $ratalgdenom )
(if (specrepp exp) (setq exp (specdisrep exp)))
(if (specrepp intvar) (setq intvar (specdisrep intvar)))
(if (mnump intvar)
(merror (intl:gettext "risch: attempt to integrate wrt a number: ~:M") intvar))
(if (and (atom intvar) (isinop exp intvar)) (go noun))
(rischform exp)
(cond (distint (return (distribint exp intvar)))
(trigint (return (trigin1 exp intvar)))
(hypertrigint (return (hypertrigint1 exp intvar t)))
(operator (go noun)))
(setq y (intsetup exp intvar))
(if operator (go noun))
(setq ratform (car y))
(setq varlist (caddr ratform))
(setq mainvar (caadr (ratf intvar)))
(setq genvar (cadddr ratform))
(unless (some #'algpget varlist)
(setq $algebraic nil)
(setq $gcd (car *gcdl*)))
(setq var (getrischvar))
(setq z (tryrisch (cdr y) mainvar))
(setf (caddr ratform) varlist)
(setf (cadddr ratform) genvar)
(return (cond ((atom (cdr z)) (disrep (car z)))
(t (let (($logsimp t) ($%e_to_numlog t))
(simplify (list* '(mplus)
(disrep (car z))
(cdr z)))))))
noun (return (list '(%integrate) exp intvar))))
(defun rischform (l)
(cond ((or (atom l) (alike1 intvar l) (freeof intvar l)) nil)
((polylogp l)
(if (and (integerp (car (subfunsubs l)))
(signp g (car (subfunsubs l))))
(rischform (car (subfunargs l)))
(setq operator t)))
((atom (caar l))
(case (caar l)
((%sin %cos %tan %cot %sec %csc)
(setq trigint t $exponentialize t)
(rischform (cadr l)))
((%asin %acos %atan %acot %asec %acsc)
(setq trigint t $logarc t)
(rischform (cadr l)))
((%sinh %cosh %tanh %coth %sech %csch)
(setq hypertrigint t $exponentialize t)
(rischform (cadr l)))
((%asinh %acosh %atanh %acoth %asech %acsch)
(setq hypertrigint t $logarc t)
(rischform (cadr l)))
((mtimes mplus mexpt rat %erf %log)
(mapc #'rischform (cdr l)))
((%heaviside %delta %doublet)
(setq distint t)
(rischform (cadr l)))
(t (setq operator (caar l)))))
(t (setq operator (caar l)))))
(defun remult (begins ends)
;;;Support function to distribint for remultiplying
(let ((factors (append begins ends)))
(cond ((null factors) 1)
((cdr factors) (cons '(mtimes simp) factors))
(t (car factors)))))
(defun distribint (exp var)
;;;Hack to handle exclusively int(distrib*f(x),x), where distrib can
;;;be heaviside, delta, or doublet with a linear argument.
;;;No other cases handled, except distributing over addition.
(setq distint nil)
(let (ab firstfactors lastfactors thisfactor offset op antideriv sign hresult)
(cond ((eq (caar exp) 'mtimes)
(setq lastfactors (cdr exp))
(loop
(cond (lastfactors
(setq thisfactor (car lastfactors))
(setq lastfactors (cdr lastfactors))
(setq firstfactors (append (list thisfactor) firstfactors))
(cond ((or (atom thisfactor) (alike1 var exp)
(freeof var exp)) nil)
((member (caar thisfactor)
'(%heaviside %delta %doublet))
(setq thisfactor (simplifya thisfactor nil))
(setq ab (islinear (cadr thisfactor) var))
(cond (ab
(setq offset (div (cdr ab) (car ab)))
(setq op (caar thisfactor))
(setq exp (remult lastfactors
(cdr firstfactors)))
(return-from distribint (simplifya
(cond ((eq op '%heaviside)
(setq antideriv (rischint exp var))
(setq sign (asksign (car ab)))
(setq hresult
(list '(mtimes)
(list '(%heaviside)
(list '(mplus)
var
offset))
(list '(mplus)
antideriv
(list '(mtimes)
(maxima-substitute (neg offset) var antideriv) -1))))
(cond ((eq sign '$positive)
hresult)
(t (list '(mplus)
antideriv
(list '(mtimes)
hresult
-1)))))
((eq op '%delta)
(list '(mtimes)
(list '(mexpt)
(list '(mabs)
(car ab))
-1)
(list '(%heaviside)
(list '(mplus)
var
offset))
(maxima-substitute (neg offset) var exp)))
((eq op '%doublet)
(list '(mtimes)
-1
(list '(mexpt)
(list '(mabs)
(car ab))
-1)
(list '(mexpt)
(car ab)
-1)
(list '(mplus)
(list '(mtimes)
(list '(%heaviside)
(list '(mplus)
var
offset))
($at (sdiff exp var)
(list '(mequal)
var
(neg offset))))
(list '(mtimes)
(list '(%delta)
(list '(mplus)
var
offset))
(maxima-substitute (neg offset) var exp)))))) nil)))
(t nil)))
(t nil)))
(t (return-from distribint (list '(%integrate) exp var))))))
((eq (caar exp) 'mplus)
(simplus (cons '(mplus) (mapcar #'(lambda (x) (rischint x var))
(cdr exp))) 1 t))
((eq (caar exp) '%heaviside)
(setq ab (islinear (cadr exp) var))
(cond (ab
(setq offset (div (cdr ab) (car ab)))
(setq sign (asksign (car ab)))
(setq hresult
(list '(mtimes)
(list '(mplus) var offset)
(list '(%heaviside)
(list '(mplus) var offset))))
(cond ((eq sign '$positive) hresult)
(t
(list '(mplus)
var
(list '(mtimes) hresult -1)))))
(t
(list '(%integrate) exp var))))
((eq (caar exp) '%delta)
(setq ab (islinear (cadr exp) var))
(cond (ab
(setq offset (div (cdr ab) (car ab)))
(simplifya (list '(mtimes)
(list '(mexpt)
(list '(mabs) (car ab))
-1)
(list '(%heaviside)
(list '(mplus)
var offset))) nil))
(t
(list '(%integrate) exp var))))
((eq (caar exp) '%doublet)
(setq ab (islinear (cadr exp) var))
(cond (ab
(setq offset (div (cdr ab) (car ab)))
(simplifya (list '(mtimes)
(list '(mexpt)
(list '(mtimes)
(list '(mabs) (car ab))
(car ab))
-1)
(list '(%delta)
(list '(mplus) var offset))) nil))
(t
(list '(%integrate) exp var))))
(t
(list '(%integrate) exp var)))))
(defun hypertrigint1 (exp var hyperfunc)
(let ((result (if hyperfunc
(integrator (resimplify exp) var)
(rischint (resimplify exp) var))))
;; The result can contain solveable integrals. Look for this case.
(if (isinop result '%integrate)
;; Found an integral. Evaluate the result again.
;; Set the flag *in-risch-p* to make sure that we do not call
;; rischint again from the integrator. This avoids endless loops.
(let ((*in-risch-p* t))
(meval (list '($ev) result '$nouns)))
result)))
(defun trigin1 (*exp var)
(let ((yyy (hypertrigint1 *exp var nil)))
(setq yyy (div ($expand ($num yyy))
($expand ($denom yyy))))
(let ((rischp var) (rp-polylogp t) $logarc $exponentialize result)
(setq result (sratsimp (if (and (freeof '$%i *exp) (freeof '$li yyy))
($realpart yyy)
($rectform yyy))))
;; The result can contain solveable integrals. Look for this case.
(if (isinop result '%integrate)
;; Found an integral. Evaluate the result again.
;; Set the flag *in-risch-p* to make sure that we do not call
;; rischint again from the integrator. This avoids endless loops.
(let ((*in-risch-p* t))
(meval (list '($ev) result '$nouns)))
result))))
(defun tryrisch (exp mainvar)
(prog (wholepart rootfactor parnumer pardenom
switch1 logptdx expflag expstuff expint y)
(setq expstuff '(0 . 1))
(cond ((eq mainvar var)
(return (rischfprog exp)))
((eq (get var 'leadop)
'mexpt)
(setq expflag t)))
(setq y (rischlogdprog exp))
(dolist (rat logptdx)
(setq y (rischadd (rischlogeprog rat) y)))
(setq y (rischadd (tryrisch1 expstuff mainvar) y))
(return (if expint (rischadd (rischexppoly expint var) y)
y))))
(defun tryrisch1 (exp mainvar)
(let* ((varlist (reverse (cdr (reverse varlist))))
(var (getrischvar)))
(tryrisch exp mainvar)))
(defun rischfprog (rat)
(let (rootfactor pardenom parnumer logptdx wholepart switch1)
(cons (cdr (ratrep* (dprog rat)))
(let ((varlist varlist)
(genvar (subseq genvar 0 (length varlist))))
(mapcar #'eprog logptdx)))))
(defun rischlogdprog (ratarg)
(prog (klth arootf deriv thebpg thetop thebot prod1 prod2 ans)
(setq ans '(0 . 1))
(cond ((or (pcoefp (cdr ratarg))
(pointergp var (cadr ratarg)))
(return (rischlogpoly ratarg))))
(aprog (ratdenominator ratarg))
(cprog (ratnumerator ratarg) (ratdenominator ratarg))
(do ((rootfactor (reverse rootfactor) (cdr rootfactor))
(parnumer (reverse parnumer) (cdr parnumer))
(klth (length rootfactor) (1- klth)))
((= klth 1))
(setq arootf (car rootfactor))
(cond
((pcoefp arootf))
((and (eq (get (car arootf) 'leadop) 'mexpt)
(null (cdddr arootf)))
(setq
expint
(append
(cond ((and (not (atom (car parnumer)))
(not (atom (caar parnumer)))
(eq (caaar parnumer) (car arootf)))
(gennegs arootf (cdaar parnumer) (cdar parnumer)))
(t (list
(list 'neg (car parnumer)
(car arootf) klth (cadr arootf)))))
expint)))
((not (zerop (pdegree arootf var)))
(setq deriv (spderivative arootf mainvar))
(setq thebpg (bprog arootf (ratnumerator deriv)))
(setq thetop (car parnumer))
(do ((kx (1- klth) (1- kx))) ((= kx 0))
(setq prod1 (r* thetop (car thebpg)))
(setq prod2 (r* thetop (cdr thebpg) (ratdenominator deriv)))
(setq thebot (pexpt arootf kx))
(setq ans (r+ ans (ratqu (r- prod2) (r* kx thebot))))
(setq thetop
(r+ prod1 (ratqu (spderivative prod2 mainvar) kx)))
(setq thetop (cdr (ratdivide thetop thebot))))
(push (ratqu thetop arootf) logptdx))))
(push (ratqu (car parnumer) (car rootfactor)) logptdx)
(cond ((or (pzerop ans) (pzerop (car ans)))
(return (rischlogpoly wholepart))))
(setq thetop (cadr (pdivide (ratnumerator ans)
(ratdenominator ans))))
(return (rischadd (ncons (ratqu thetop (ratdenominator ans)))
(rischlogpoly wholepart)))))
(defun gennegs (denom num numdenom)
(cond ((null num) nil)
(t (cons (list 'neg (cadr num)
(car denom)
(- klth (car num))
(r* numdenom (caddr denom) ))
(gennegs denom (cddr num) numdenom)))))
(defun rischlogeprog (p)
(prog (p1e p2e p2deriv logcoef ncc dcc allcc expcoef)
(if (or (pzerop p) (pzerop (car p))) (return (rischzero)))
(setq p1e (ratnumerator p))
(desetq (dcc p2e) (oldcontent (ratdenominator p)))
(cond ((and (not switch1)
(cdr (setq pardenom (intfactor p2e))))
(setq parnumer nil)
(setq switch1 t)
(desetq (ncc p1e) (oldcontent p1e))
(cprog p1e p2e)
(setq allcc (ratqu ncc dcc))
(return (do ((pnum parnumer (cdr pnum))
(pden pardenom (cdr pden))
(ans (rischzero)))
((or (null pnum) (null pden))
(setq switch1 nil) ans)
(setq ans (rischadd
(rischlogeprog
(r* allcc (ratqu (car pnum) (car pden))))
ans))))))
(when (and expflag (null (p-red p2e)))
(push (cons 'neg p) expint)
(return (rischzero)))
(if expflag (setq expcoef (r* (p-le p2e) (ratqu (get var 'rischdiff)
(make-poly var)))))
(setq p1e (ratqu p1e (ptimes dcc (p-lc p2e)))
p2e (ratqu p2e (p-lc p2e))) ;MAKE DENOM MONIC
(setq p2deriv (spderivative p2e mainvar))
(setq logcoef (ratqu p1e
(if expflag (r- p2deriv (r* p2e expcoef))
p2deriv)))
(when (risch-constp logcoef)
(if expflag
(setq expstuff (r- expstuff (r* expcoef logcoef))))
(return
(list
'(0 . 1)
(list '(mtimes)
(disrep logcoef)
(logmabs (disrep p2e))))))
(if (and expflag $liflag changevp)
(let* ((newvar (gensym))
(new-int ($changevar
`((%integrate) ,(simplify (disrep p)) ,intvar)
(sub newvar (get var 'rischexpr))
newvar intvar))
(changevp nil)) ;prevents recursive changevar
(if (and (freeof intvar new-int)
(freeof '%integrate
(setq new-int (rischint (sdiff new-int newvar)
newvar))))
(return
(list (rzero)
(maxima-substitute (get var 'rischexpr) newvar new-int))))))
(return (rischnoun p))))
(defun findint (exp)
(cond ((atom exp) nil)
((atom (car exp)) (findint (cdr exp)))
((eq (caaar exp) '%integrate) t)
(t (findint (cdr exp)))))
(defun logequiv (fn1 fn2)
(freeof intvar ($ratsimp (div* (remabs (leadarg fn1))
(remabs (leadarg fn2))))))
(defun remabs (exp)
(cond ((atom exp) exp)
((eq (caar exp) 'mabs) (cadr exp))
(t exp)))
(declare-top (special vlist lians degree))
(defun getfnsplit (l &aux coef fn)
(mapc #'(lambda (x) (if (free x intvar) (push x coef) (push x fn))) l)
(cons (muln coef nil) (muln fn nil)))
(defun getfncoeff (a form)
(cond ((null a) 0)
((equal (car a) 0) (getfncoeff (cdr a) form))
((and (listp (car a))
(eq (caaar a) 'mplus) (ratpl (getfncoeff (cdar a) form)
(getfncoeff (cdr a) form))))
((and (listp (car a))
(eq (caaar a) 'mtimes))
(destructuring-let (((coef . newfn) (getfnsplit (cdar a))))
;; (car a) is a mtimes expression. We insert coef and newfn as the
;; new arguments to the mtimes expression. This causes problems if
;; (1) coef is a mtimes expression too and
;; (2) (car a) has already a simp flag
;; We get a nested mtimes expression, which does not simplify.
;; We comment out the following code (DK 09/2009):
;; (setf (cdar a) (list coef newfn))
;; Insert a complete mtimes expression without simpflag.
;; Nested mtimes expressions simplify further.
(setf (car a) (list '(mtimes) coef newfn))
(setf (cdar a) (list coef newfn))
(cond ((zerop1 coef) (getfncoeff (cdr a) form))
((and (matanp newfn) (member '$%i varlist :test #'eq))
(let (($logarc t) ($logexpand '$all))
(rplaca a ($expand (resimplify (car a)))))
(getfncoeff a form))
((and (alike1 (leadop newfn) (leadop form))
(or (alike1 (leadarg newfn) (leadarg form))
(and (mlogp newfn)
(logequiv form newfn))))
(ratpl (rform coef)
(prog2 (rplaca a 0)
(getfncoeff (cdr a) form))))
((do ((vl varlist (cdr vl))) ((null vl))
(and (not (atom (car vl)))
(alike1 (leadop (car vl)) (leadop newfn))
(if (mlogp newfn)
(logequiv (car vl) newfn)
(alike1 (car vl) newfn))
(rplaca (cddar a) (car vl))
(return nil))))
((let (vlist) (newvar1 (car a)) (null vlist))
(setq cary
(ratpl (cdr (ratrep* (car a)))
cary))
(rplaca a 0)
(getfncoeff (cdr a) form))
((and liflag
(mlogp form)
(mlogp newfn))
(push (dilog (cons (car a) form)) lians)
(rplaca a 0)
(getfncoeff (cdr a) form))
((and liflag
(polylogp form)
(mlogp newfn)
(logequiv form newfn))
(push (mul* (cadar a) (make-li (1+ (car (subfunsubs form)))
(leadarg form)))
lians)
(rplaca a 0)
(getfncoeff (cdr a) form))
(t (setq nogood t) 0))))
(t (rplaca a (list '(mtimes) 1 (car a)))
(getfncoeff a form))))
(defun rischlogpoly (exp)
(cond ((equal exp '(0 . 1)) (rischzero))
(expflag (push (cons 'poly exp) expint)
(rischzero))
((not (among var exp)) (tryrisch1 exp mainvar))
(t (do ((degree (pdegree (car exp) var) (1- degree))
(p (car exp))
(den (cdr exp))
(lians ())
(sum (rzero))
(cary (rzero))
(y) (z) (ak) (nogood) (lbkpl1))
((minusp degree) (cons sum (append lians (cdr y))))
(setq ak (r- (ratqu (polcoef p degree) den)
(r* (cons (1+ degree) 1)
cary
(get var 'rischdiff))))
(if (not (pzerop (polcoef p degree)))
(setq p (if (pcoefp p) (pzero) (psimp var (p-red p)))))
(setq y (tryrisch1 ak mainvar))
(setq cary (car y))
(and (> degree 0) (setq liflag $liflag))
(setq z (getfncoeff (cdr y) (get var 'rischexpr)))
(setq liflag nil)
(cond ((and (> degree 0)
(or nogood (findint (cdr y))))
(return (rischnoun sum (r+ (r* ak
(make-poly var degree 1))
(ratqu p den))))))
(setq lbkpl1 (ratqu z (cons (1+ degree) 1)))
(setq sum (r+ (r* lbkpl1 (make-poly var (1+ degree) 1))
(r* cary (if (zerop degree) 1
(make-poly var degree 1)))
sum))))))
(defun make-li (sub arg)
(subfunmake '$li (ncons sub) (ncons arg)))
;;integrates log(ro)^degree*log(rn)' in terms of polylogs
;;finds constants c,d and integers j,k such that
;;c*ro^j+d=rn^k If ro and rn are poly's then can assume either j=1 or k=1
(defun dilog (l)
(destructuring-let* ((((nil coef nlog) . olog) l)
(narg (remabs (cadr nlog)))
(varlist varlist)
(genvar genvar)
(rn (rform narg)) ;; can add new vars to varlist
(ro (rform (cadr olog)))
(var (caar ro))
((j . k) (ratreduce (pdegree (car rn) var) (pdegree (car ro) var)))
(idx (gensym))
(rc) (rd))
(cond ((and (= j 1) (> k 1))
(setq rn (ratexpt rn k)
coef (div coef k)
narg (rdis rn)))
((and (= k 1) (> j 1))
(setq ro (ratexpt ro j)
coef (div coef (f* j degree))
olog (mul j olog))))
(desetq (rc . rd) (ratdivide rn ro))
(cond ((and (freeof intvar (rdis rc)) ;; can't use risch-constp because varlist
(freeof intvar (rdis rd))) ;; is not set up with vars in correct order.
(setq narg ($ratsimp (sub 1 (div narg (rdis rd)))))
(mul* coef (power -1 (1+ degree))
`((mfactorial) ,degree)
(dosum (mul* (power -1 idx)
(div* (power olog idx)
`((mfactorial) ,idx))
(make-li (add degree (neg idx) 1) narg))
idx 0 degree t)))
(t (setq nogood t) 0))))
(defun exppolycontrol (flag f a expg n)
(let (y l var (varlist varlist) (genvar genvar))
(setq varlist (reverse (cdr (reverse varlist))))
(setq var (getrischvar))
(setq y (get var 'leadop))
(cond ((and (not (pzerop (ratnumerator f)))
(risch-constp (setq l (ratqu a f))))
(cond (flag ;; multiply in expg^n - n may be negative
(list (r* l (ratexpt (cons (list expg 1 1) 1) n))
0))
(t l)))
((eq y intvar)
(rischexpvar nil flag (list f a expg n)))
(t (rischexplog (eq y 'mexpt) flag f a
(list expg n (get var 'rischarg)
var (get var 'rischdiff)))))))
(defun rischexppoly (expint var)
(let (y w num denom type (ans (rischzero))
(expdiff (ratqu (get var 'rischdiff) (list var 1 1))))
(do ((expint expint (cdr expint)))
((null expint) ans)
(desetq (type . y) (car expint))
(desetq (num . denom) (ratfix y))
(cond ((eq type 'neg)
(setq w (exppolycontrol t
(r* (- (cadr denom))
expdiff)
(ratqu num (caddr denom))
var
(- (cadr denom)))))
((or (numberp num) (not (eq (car num) var)))
(setq w (tryrisch1 y mainvar)))
(t (setq w (rischzero))
(do ((num (cdr num) (cddr num))) ((null num))
(cond ((equal (car num) 0)
(setq w (rischadd
(tryrisch1 (ratqu (cadr num) denom) mainvar)
w)))
(t (setq w (rischadd (exppolycontrol
t
(r* (car num) expdiff)
(ratqu (cadr num) denom)
var
(car num))
w)))))))
(setq ans (rischadd w ans)))))
(defun rischexpvar (expexpflag flag l)
(prog (lcm y m p alphar beta gamma delta r s
tt denom k wl wv i ytemp ttemp yalpha f a expg n yn yd)
(desetq (f a expg n) l)
(cond ((or (pzerop a) (pzerop (car a)))
(return (cond ((null flag) (rzero))
(t (rischzero))))))
(setq denom (ratdenominator f))
(setq p (findpr (cdr (partfrac a mainvar))
(cdr (partfrac f mainvar))))
(setq lcm (plcm (ratdenominator a) p))
(setq y (ratpl (spderivative (cons 1 p) mainvar)
(ratqu f p)))
(setq lcm (plcm lcm (ratdenominator y)))
(setq r (car (ratqu lcm p)))
(setq s (car (r* lcm y)))
(setq tt (car (r* a lcm)))
(setq beta (pdegree r mainvar))
(setq gamma (pdegree s mainvar))
(setq delta (pdegree tt mainvar))
(setq alphar (max (- (1+ delta) beta)
(- delta gamma)))
(setq m 0)
(cond ((equal (1- beta) gamma)
(setq y (r* -1
(ratqu (polcoef s gamma)
(polcoef r beta))))
(and (equal (cdr y) 1)
(numberp (car y))
(setq m (car y)))))
(setq alphar (max alphar m))
(if (minusp alphar)
(return (if flag (cxerfarg (rzero) expg n a) nil)))
(cond ((not (and (equal alphar m) (not (zerop m))))
(go down2)))
(setq k (+ alphar beta -2))
(setq wl nil)
l2 (setq wv (list (cons (polcoef tt k) 1)))
(setq i alphar)
l1 (setq wv
(cons (r+ (r* (cons i 1)
(polcoef r (+ k 1 (- i))))
(cons (polcoef s (+ k (- i))) 1))
wv))
(decf i)
(cond ((> i -1) (go l1)))
(setq wl (cons wv wl))
(decf k)
(cond ((> k -1) (go l2)))
(setq y (lsa wl))
(if (or (eq y 'singular) (eq y 'inconsistent))
(cond ((null flag) (return nil))
(t (return (cxerfarg (rzero) expg n a)))))
(setq k 0)
(setq lcm 0)
(setq y (cdr y))
l3 (setq lcm
(r+ (r* (car y) (pexpt (list mainvar 1 1) k))
lcm))
(incf k)
(setq y (cdr y))
(cond ((null y)
(return (cond ((null flag) (ratqu lcm p))
(t (list (r* (ratqu lcm p)
(cons (list expg n 1) 1))
0))))))
(go l3)
down2 (cond ((> (1- beta) gamma)
(setq k (+ alphar (1- beta)))
(setq denom '(ratti alphar (polcoef r beta) t)))
((< (1- beta) gamma)
(setq k (+ alphar gamma))
(setq denom '(polcoef s gamma)))
(t (setq k (+ alphar gamma))
(setq denom
'(ratpl (ratti alphar (polcoef r beta) t)
(polcoef s gamma)))))
(setq y 0)
loop (setq yn (polcoef (ratnumerator tt) k)
yd (r* (ratdenominator tt) ;DENOM MAY BE 0
(cond ((zerop alphar) (polcoef s gamma))
(t (eval denom))) ))
(cond ((rzerop yd)
(cond ((pzerop yn) (setq k (1- k) alphar (1- alphar))
(go loop)) ;need more constraints?
(t (cond
((null flag) (return nil))
(t (return (cxerfarg (rzero) expg n a)))))))
(t (setq yalpha (ratqu yn yd))))
(setq ytemp (r+ y (r* yalpha
(cons (list mainvar alphar 1) 1) )))
(setq ttemp (r- tt (r* yalpha
(r+ (r* s (cons (list mainvar alphar 1) 1))
(r* r alphar
(list mainvar (1- alphar) 1))))))
(decf k)
(decf alphar)
(cond ((< alphar 0)
(cond
((rzerop ttemp)
(cond
((null flag) (return (ratqu ytemp p)))
(t (return (list (ratqu (r* ytemp (cons (list expg n 1) 1))
p)
0)))))
((null flag) (return nil))
((and (risch-constp (setq ttemp (ratqu ttemp lcm)))
$erfflag
(equal (pdegree (car (get expg 'rischarg)) mainvar) 2)
(equal (pdegree (cdr (get expg 'rischarg)) mainvar) 0))
(return (list (ratqu (r* ytemp (cons (list expg n 1) 1)) p)
(erfarg2 (r* n (get expg 'rischarg)) ttemp))))
(t (return
(cxerfarg
(ratqu (r* y (cons (list expg n 1) 1)) p)
expg
n
(ratqu tt lcm)))))))
(setq y ytemp)
(setq tt ttemp)
(go loop)))
;; *JM should be declared as an array, although it is not created
;; by this file. -- cwh
(defun lsa (mm)
(prog (d *mosesflag m m2)
(setq d (length (car mm)))
;; MTOA stands for MATRIX-TO-ARRAY. An array is created and
;; associated functionally with the symbol *JM. The elements
;; of the array are initialized from the matrix MM.
(mtoa '*jm* (length mm) d mm)
(setq m (tfgeli '*jm* (length mm) d))
(cond ((or (and (null (car m)) (null (cadr m)))
(and (car m)
(> (length (car m)) (- (length mm) (1- d)))))
(return 'singular))
((cadr m) (return 'inconsistent)))
(setq *mosesflag t)
(ptorat '*jm* (1- d) d)
(setq m2 (xrutout '*jm* (1- d) d nil nil))
(setq m2 (lsafix (cdr m2) (caddr m)))
(return m2)))
(defun lsafix (l n)
(declare (special *jm*))
(do ((n n (cdr n))
(l l (cdr l)))
((null l))
(setf (aref *jm* 1 (car n)) (car l)))
(do ((s (length l) (1- s))
(ans))
((= s 0) (cons '(list) ans))
(setq ans (cons (aref *jm* 1 s) ans))))
(defun findpr (alist flist &aux (p 1) alphar fterm)
(do ((alist alist (cdr alist))) ((null alist))
(setq fterm (findflist (cadar alist) flist))
(if fterm (setq flist (remove y flist :count 1 :test #'eq)))
(setq alphar
(cond ((null fterm) (caddar alist))
((equal (caddr fterm) 1)
(fpr-dif (car flist) (caddar alist)))
(t (max (- (caddar alist) (caddr fterm)) 0))))
(if (not (zerop alphar))
(setq p (ptimes p (pexpt (cadar alist) alphar)))))
(do ((flist flist (cdr flist)))
((null flist))
(when (equal (caddar flist) 1)
(setq alphar (fpr-dif (car flist) 0))
(setq p (ptimes p (pexpt (cadar flist) alphar)))))
p)
(defun fpr-dif (fterm alpha)
(destructuring-let* (((num den mult) fterm)
(m (spderivative den mainvar))
(n))
(cond ((rzerop m) alpha)
(t (setq n (ratqu (cdr (ratdivide num den))
m))
(if (and (equal (cdr n) 1) (numberp (car n)))
(max (car n) alpha)
alpha)))))
(defun findflist (a llist)
(cond ((null llist) nil)
((equal (cadar llist) a) (car llist))
(t (findflist a (cdr llist)))))
(defun rischexplog (expexpflag flag f a l)
(declare (special var))
(prog (lcm y yy m p alphar beta gamma delta
mu r s tt denom ymu rbeta expg n eta logeta logdiff
temp cary nogood vector aarray rmu rrmu rarray)
(desetq (expg n eta logeta logdiff) l)
(cond ((or (pzerop a) (pzerop (car a)))
(return (cond ((null flag) (rzero))
(t (rischzero))))))
(setq p (findpr (cdr (partfrac a var)) (cdr (partfrac f var))))
(setq lcm (plcm (ratdenominator a) p))
(setq y (ratpl (spderivative (cons 1 p) mainvar)
(ratqu f p)))
(setq lcm (plcm lcm (ratdenominator y)))
(setq r (car (ratqu lcm p)))
(setq s (car (r* lcm y)))
(setq tt (car (r* a lcm)))
(setq beta (pdegree r var))
(setq gamma (pdegree s var))
(setq delta (pdegree tt var))
(cond (expexpflag (setq mu (max (- delta beta)
(- delta gamma)))
(go expcase)))
(setq mu (max (- (1+ delta) beta)
(- (1+ delta) gamma)))
(cond ((< beta gamma) (go back))
((= (1- beta) gamma) (go down1)))
(setq y (tryrisch1 (ratqu (r- (r* (polcoef r (1- beta))
(polcoef s gamma))
(r* (polcoef r beta)
(polcoef s (1- gamma))))
(r* (polcoef r beta)
(polcoef r beta) ))
mainvar))
(setq cary (car y))
(setq yy (getfncoeff (cdr y) (get var 'rischexpr)))
(cond ((and (not (findint (cdr y)))
(not nogood)
(not (atom yy))
(equal (cdr yy) 1)
(numberp (car yy))
(> (car yy) mu))
(setq mu (car yy))))
(go back)
expcase
(cond ((not (equal beta gamma)) (go back)))
(setq y (tryrisch1 (ratqu (polcoef s gamma) (polcoef r beta))
mainvar))
(cond ((findint (cdr y)) (go back)))
(setq yy (ratqu (r* -1 (car y)) eta))
(cond ((and (equal (cdr yy) 1)
(numberp (car yy))
(> (car yy) mu))
(setq mu (car yy))))
(go back)
down1(setq y (tryrisch1 (ratqu (polcoef s gamma) (polcoef r beta))
mainvar))
(setq cary (car y))
(setq yy (getfncoeff (cdr y) (get var 'rischexpr)))
(cond ((and (not (findint (cdr y)))
(not nogood)
(equal (cdr yy) 1)
(numberp (car yy))
(> (- (car yy)) mu))
(setq mu (- (car yy)))))
back (if (minusp mu)
(return (if flag (cxerfarg (rzero) expg n a) nil)))
(cond ((> beta gamma)(go lsacall))
((= beta gamma)
(go recurse)))
(setq denom (polcoef s gamma))
(setq y '(0 . 1))
linearloop
(setq ymu (ratqu (polcoef (ratnumerator tt) (+ mu gamma))
(r* (ratdenominator tt) denom)))
(setq y (r+ y (setq ymu (r* ymu (pexpt (list logeta 1 1) mu) ))))
(setq tt (r- tt
(r* s ymu)
(r* r (spderivative ymu mainvar))))
(decf mu)
(cond ((not (< mu 0)) (go linearloop))
((not flag) (return (if (rzerop tt) (ratqu y p) nil)))
((rzerop tt)
(return (cons (ratqu (r* y (cons (list expg n 1) 1)) p) '(0))))
(t (return (cxerfarg (ratqu (r* y (cons (list expg n 1) 1)) p)
expg
n
(ratqu tt lcm)))))
recurse
(setq rbeta (polcoef r beta))
(setq y '(0 . 1))
recurseloop
(setq f (r+ (ratqu (polcoef s gamma) rbeta)
(if expexpflag
(r* mu (spderivative eta mainvar))
0)))
(setq ymu (exppolycontrol nil
f
(ratqu (polcoef (ratnumerator tt)
(+ beta mu))
(r* (ratdenominator tt) rbeta))
expg n))
(when (null ymu)
(return (cond ((null flag) nil)
(t (return (cxerfarg (ratqu (r* y (cons (list expg n 1) 1)) p)
expg n (ratqu tt lcm)))))))
(setq y (r+ y (setq ymu (r* ymu (pexpt (list logeta 1 1) mu)))))
(setq tt (r- tt