1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module risch
)
15 (load-macsyma-macros rzmac ratmac
)
17 (declare-top (special parnumer pardenom logptdx wholepart
18 $ratalgdenom expexpflag $logsimp switch1 degree cary
19 $ratfac $logexpand ratform genvar
*var var rootfactor
20 expint $keepfloat trigint operator $exponentialize $gcd
21 $logarc changevp klth r s beta gamma b mainvar expflag
22 expstuff liflag intvar switch varlist nogood genvar
23 $erfflag $liflag rischp $factorflag alphar m
24 genpairs hypertrigint
*mosesflag
*exp y $algebraic
25 implicit-real $%e_to_numlog generate-atan2
26 context rp-polylogp
*in-risch-p
*))
28 (defmvar $liflag t
"Controls whether `risch' generates polylogs")
30 (defmvar $erfflag t
"Controls whether `risch' generates `erfs'")
32 (defvar changevp t
"When nil prevents changevar hack")
34 (defmacro pair
(al bl
) `(mapcar #'cons
,al
,bl
))
36 ;; internal representation of risch expressions: list with canonical rational
37 ;; expression (CRE) as first element, standard maxima expressions as remaining
38 ;; elements. risch expression is sum of CRE and remaining elements.
39 (defmacro rischzero
() ''((0 .
1) 0))
41 (defun rischnoun (exp1 &optional
(exp2 exp1 exp2p
))
42 (unless exp2p
(setq exp1
(rzero)))
43 `(,exp1
((%integrate
) ,(disrep exp2
) ,intvar
)))
46 (do ((vl varlist
(cdr vl
))
48 ((null (cdr vl
)) (car gl
))))
50 ;; test whether CRE p is constant with respect to variable of integration.
51 ;; requires variables in varlist and genvar
52 ;; to be ordered as by intsetup, with var of integration ordered before
53 ;; any other expressions that contain it.
54 (defun risch-pconstp (p)
55 (or (pcoefp p
) (pointergp mainvar
(car p
))))
57 (defun risch-constp (r)
59 (and (risch-pconstp (car r
)) (risch-pconstp (cdr r
))))
61 ;; adds two risch expressions (defined above).
63 (destructuring-let (((a . b
) x
) ((c . d
) y
))
64 (cons (r+ a c
) (append b d
))))
66 (defmfun $risch
(exp var
)
67 (let ((*integrator-level
* 0))
68 (declare (special *integrator-level
*))
69 (with-new-context (context)
72 (defun spderivative (p var
)
73 (cond ((pcoefp p
) '(0 .
1))
74 ((null (cdr p
)) '(0 .
1))
75 ((or (not (atom (car p
))) (numberp (car p
))) ;P IS A RATFORM
76 (let ((denprime (spderivative (cdr p
) var
)))
77 (cond ((rzerop denprime
)
78 (ratqu (spderivative (car p
) var
) (cdr p
)))
79 (t (ratqu (r- (r* (spderivative (car p
) var
)
81 (r* (car p
) denprime
))
82 (r* (cdr p
) (cdr p
)))))))
83 (t (r+ (spderivative1 (car p
)
87 (spderivative (cons (car p
) (cdddr p
))
90 (defun spderivative1 (var1 deg coeff var
)
92 (r* (ratexpt (cons (list var
1 1) 1) (1- deg
))
94 ((pointergp var var1
) '(0 .
1))
95 ((equal deg
0) (spderivative coeff var
))
96 (t (r+ (r* (ratexpt (cons (list var1
1 1) 1) deg
)
97 (spderivative coeff var
))
98 (r* (cond ((equal deg
1) coeff
)
101 (ratexpt (cons (list var1
1 1) 1)
103 (get var1
'rischdiff
) )))))
105 (defun polylogp (exp &optional sub
)
106 (and (mqapplyp exp
) (eq (subfunname exp
) '$li
)
107 (or (null sub
) (equal sub
(car (subfunsubs exp
))))))
109 (defun rischint (exp intvar
&aux
($logarc nil
) ($exponentialize nil
)
110 ($gcd
'$algebraic
) ($algebraic t
) (implicit-real t
)
111 ($float nil
) ($numer nil
)
112 ;; The risch integrator expects $logexpand T. Otherwise,
113 ;; the integrator hangs for special types of integrals
114 ;; (See bug report ID:3039452)
116 (prog ($%e_to_numlog $logsimp trigint operator y z var ratform liflag
117 mainvar varlist genvar hypertrigint $ratfac $ratalgdenom
)
118 (if (specrepp exp
) (setq exp
(specdisrep exp
)))
119 (if (specrepp intvar
) (setq intvar
(specdisrep intvar
)))
121 (merror (intl:gettext
"risch: attempt to integrate wrt a number: ~:M") intvar
))
122 (if (and (atom intvar
) (isinop exp intvar
)) (go noun
))
124 (cond (trigint (return (trigin1 exp intvar
)))
125 (hypertrigint (return (hypertrigint1 exp intvar t
)))
126 (operator (go noun
)))
127 (setq y
(intsetup exp intvar
))
128 (if operator
(go noun
))
129 (setq ratform
(car y
))
130 (setq varlist
(caddr ratform
))
131 (setq mainvar
(caadr (ratf intvar
)))
132 (setq genvar
(cadddr ratform
))
133 (unless (some #'algpget varlist
)
134 (setq $algebraic nil
)
135 (setq $gcd
(car *gcdl
*)))
136 (setq var
(getrischvar))
137 (setq z
(tryrisch (cdr y
) mainvar
))
138 (setf (caddr ratform
) varlist
)
139 (setf (cadddr ratform
) genvar
)
140 (return (cond ((atom (cdr z
)) (disrep (car z
)))
141 (t (let (($logsimp t
) ($%e_to_numlog t
))
142 (simplify (list* '(mplus)
145 noun
(return (list '(%integrate
) exp intvar
))))
148 (cond ((or (atom l
) (alike1 intvar l
) (freeof intvar l
)) nil
)
150 (if (and (integerp (car (subfunsubs l
)))
151 (signp g
(car (subfunsubs l
))))
152 (rischform (car (subfunargs l
)))
156 ((%sin %cos %tan %cot %sec %csc
)
157 (setq trigint t $exponentialize t
)
158 (rischform (cadr l
)))
159 ((%asin %acos %atan %acot %asec %acsc
)
160 (setq trigint t $logarc t
)
161 (rischform (cadr l
)))
162 ((%sinh %cosh %tanh %coth %sech %csch
)
163 (setq hypertrigint t $exponentialize t
)
164 (rischform (cadr l
)))
165 ((%asinh %acosh %atanh %acoth %asech %acsch
)
166 (setq hypertrigint t $logarc t
)
167 (rischform (cadr l
)))
168 ((mtimes mplus mexpt rat %erf %log
)
169 (mapc #'rischform
(cdr l
)))
170 (t (setq operator
(caar l
)))))
171 (t (setq operator
(caar l
)))))
173 (defun hypertrigint1 (exp var hyperfunc
)
174 (let ((result (if hyperfunc
175 (sinint (resimplify exp
) var
)
176 (rischint (resimplify exp
) var
))))
177 ;; The result can contain solveable integrals. Look for this case.
178 (if (isinop result
'%integrate
)
179 ;; Found an integral. Evaluate the result again.
180 ;; Set the flag *in-risch-p* to make sure that we do not call
181 ;; rischint again from the integrator. This avoids endless loops.
182 (let ((*in-risch-p
* t
))
183 (meval (list '($ev
) result
'$nouns
)))
186 (defun trigin1 (*exp var
)
187 (let ((yyy (hypertrigint1 *exp var nil
)))
188 (setq yyy
(div ($expand
($num yyy
))
189 ($expand
($denom yyy
))))
190 (let ((rischp var
) (rp-polylogp t
) $logarc $exponentialize result
)
191 (setq result
(sratsimp (if (and (freeof '$%i
*exp
) (freeof '$li yyy
))
194 ;; The result can contain solveable integrals. Look for this case.
195 (if (isinop result
'%integrate
)
196 ;; Found an integral. Evaluate the result again.
197 ;; Set the flag *in-risch-p* to make sure that we do not call
198 ;; rischint again from the integrator. This avoids endless loops.
199 (let ((*in-risch-p
* t
))
200 (meval (list '($ev
) result
'$nouns
)))
203 (defun tryrisch (exp mainvar
)
204 (prog (wholepart rootfactor parnumer pardenom
205 switch1 logptdx expflag expstuff expint y
)
206 (setq expstuff
'(0 .
1))
207 (cond ((eq mainvar var
)
208 (return (rischfprog exp
)))
209 ((eq (get var
'leadop
)
212 (setq y
(rischlogdprog exp
))
213 (dolist (rat logptdx
)
214 (setq y
(rischadd (rischlogeprog rat
) y
)))
215 (if varlist
(setq y
(rischadd (tryrisch1 expstuff mainvar
) y
)))
216 (return (if expint
(rischadd (rischexppoly expint var
) y
)
219 (defun tryrisch1 (exp mainvar
)
220 (let* ((varlist (reverse (cdr (reverse varlist
))))
222 (tryrisch exp mainvar
)))
224 (defun rischfprog (rat)
225 (let (rootfactor pardenom parnumer logptdx wholepart switch1
)
226 (cons (cdr (ratrep* (dprog rat
)))
227 (let ((varlist varlist
)
228 (genvar (subseq genvar
0 (length varlist
))))
229 (mapcar #'eprog logptdx
)))))
231 (defun rischlogdprog (ratarg)
232 (prog (klth arootf deriv thebpg thetop thebot prod1 prod2 ans
)
234 (cond ((or (pcoefp (cdr ratarg
))
235 (pointergp var
(cadr ratarg
)))
236 (return (rischlogpoly ratarg
))))
237 (aprog (ratdenominator ratarg
))
238 (cprog (ratnumerator ratarg
) (ratdenominator ratarg
))
239 (do ((rootfactor (reverse rootfactor
) (cdr rootfactor
))
240 (parnumer (reverse parnumer
) (cdr parnumer
))
241 (klth (length rootfactor
) (1- klth
)))
243 (setq arootf
(car rootfactor
))
246 ((and (eq (get (car arootf
) 'leadop
) 'mexpt
)
247 (null (cdddr arootf
)))
251 (cond ((and (not (atom (car parnumer
)))
252 (not (atom (caar parnumer
)))
253 (eq (caaar parnumer
) (car arootf
)))
254 (gennegs arootf
(cdaar parnumer
) (cdar parnumer
)))
256 (list 'neg
(car parnumer
)
257 (car arootf
) klth
(cadr arootf
)))))
259 ((not (zerop (pdegree arootf var
)))
260 (setq deriv
(spderivative arootf mainvar
))
261 (setq thebpg
(bprog arootf
(ratnumerator deriv
)))
262 (setq thetop
(car parnumer
))
263 (do ((kx (1- klth
) (1- kx
))) ((= kx
0))
264 (setq prod1
(r* thetop
(car thebpg
)))
265 (setq prod2
(r* thetop
(cdr thebpg
) (ratdenominator deriv
)))
266 (setq thebot
(pexpt arootf kx
))
267 (setq ans
(r+ ans
(ratqu (r- prod2
) (r* kx thebot
))))
269 (r+ prod1
(ratqu (spderivative prod2 mainvar
) kx
)))
270 (setq thetop
(cdr (ratdivide thetop thebot
))))
271 (push (ratqu thetop arootf
) logptdx
))))
272 (push (ratqu (car parnumer
) (car rootfactor
)) logptdx
)
273 (cond ((or (pzerop ans
) (pzerop (car ans
)))
274 (return (rischlogpoly wholepart
))))
275 (setq thetop
(cadr (pdivide (ratnumerator ans
)
276 (ratdenominator ans
))))
277 (return (rischadd (ncons (ratqu thetop
(ratdenominator ans
)))
278 (rischlogpoly wholepart
)))))
280 (defun gennegs (denom num numdenom
)
281 (cond ((null num
) nil
)
282 (t (cons (list 'neg
(cadr num
)
285 (r* numdenom
(caddr denom
) ))
286 (gennegs denom
(cddr num
) numdenom
)))))
288 (defun rischlogeprog (p)
289 (prog (p1e p2e p2deriv logcoef ncc dcc allcc expcoef my-divisor
)
290 (if (or (pzerop p
) (pzerop (car p
))) (return (rischzero)))
291 (setq p1e
(ratnumerator p
))
292 (desetq (dcc p2e
) (oldcontent (ratdenominator p
)))
293 (cond ((and (not switch1
)
294 (cdr (setq pardenom
(intfactor p2e
))))
297 (desetq (ncc p1e
) (oldcontent p1e
))
299 (setq allcc
(ratqu ncc dcc
))
300 (return (do ((pnum parnumer
(cdr pnum
))
301 (pden pardenom
(cdr pden
))
303 ((or (null pnum
) (null pden
))
304 (setq switch1 nil
) ans
)
307 (r* allcc
(ratqu (car pnum
) (car pden
))))
309 (when (and expflag
(null (p-red p2e
)))
310 (push (cons 'neg p
) expint
)
311 (return (rischzero)))
312 (if expflag
(setq expcoef
(r* (p-le p2e
) (ratqu (get var
'rischdiff
)
314 (setq p1e
(ratqu p1e
(ptimes dcc
(p-lc p2e
)))
315 p2e
(ratqu p2e
(p-lc p2e
))) ;MAKE DENOM MONIC
316 (setq p2deriv
(spderivative p2e mainvar
))
317 (setq my-divisor
(if expflag
(r- p2deriv
(r* p2e expcoef
)) p2deriv
))
318 (when (equal my-divisor
'(0 .
1))
319 ;; (format t "HEY RISCHLOGEPROG, FOUND ZERO DIVISOR; GIVE UP.~%")
320 (return (rischnoun p
)))
321 (setq logcoef
(ratqu p1e my-divisor
))
322 (when (risch-constp logcoef
)
324 (setq expstuff
(r- expstuff
(r* expcoef logcoef
))))
330 (logmabs (disrep p2e
))))))
331 (if (and expflag $liflag changevp
)
332 (let* ((newvar (gensym))
334 `((%integrate
) ,(simplify (disrep p
)) ,intvar
)
335 (sub newvar
(get var
'rischexpr
))
337 (changevp nil
)) ;prevents recursive changevar
338 (if (and (freeof intvar new-int
)
340 (setq new-int
(rischint (sdiff new-int newvar
)
344 (maxima-substitute (get var
'rischexpr
) newvar new-int
))))))
345 (return (rischnoun p
))))
349 (cond ((atom exp
) nil
)
350 ((atom (car exp
)) (findint (cdr exp
)))
351 ((eq (caaar exp
) '%integrate
) t
)
352 (t (findint (cdr exp
)))))
354 (defun logequiv (fn1 fn2
)
355 (freeof intvar
($ratsimp
(div* (remabs (leadarg fn1
))
356 (remabs (leadarg fn2
))))))
359 (cond ((atom exp
) exp
)
360 ((eq (caar exp
) 'mabs
) (cadr exp
))
363 (declare-top (special vlist lians degree
))
365 (defun getfnsplit (l)
367 (dolist (x l
(values (muln coef nil
) (muln fn nil
)))
372 (defun getfncoeff (a form
)
374 ((equal (car a
) 0) (getfncoeff (cdr a
) form
))
375 ((and (listp (car a
))
376 (eq (caaar a
) 'mplus
) (ratpl (getfncoeff (cdar a
) form
)
377 (getfncoeff (cdr a
) form
))))
378 ((and (listp (car a
))
379 (eq (caaar a
) 'mtimes
))
380 (multiple-value-bind (coef newfn
)
381 (getfnsplit (cdar a
))
382 ;; (car a) is a mtimes expression. We insert coef and newfn as the
383 ;; new arguments to the mtimes expression. This causes problems if
384 ;; (1) coef is a mtimes expression too and
385 ;; (2) (car a) has already a simp flag
386 ;; We get a nested mtimes expression, which does not simplify.
387 ;; We comment out the following code (DK 09/2009):
388 ;; (setf (cdar a) (list coef newfn))
390 ;; Insert a complete mtimes expression without simpflag.
391 ;; Nested mtimes expressions simplify further.
392 (setf (car a
) (list '(mtimes) coef newfn
))
394 (setf (cdar a
) (list coef newfn
))
395 (cond ((zerop1 coef
) (getfncoeff (cdr a
) form
))
396 ((and (matanp newfn
) (member '$%i varlist
:test
#'eq
))
397 (let (($logarc t
) ($logexpand
'$all
))
398 (rplaca a
($expand
(resimplify (car a
)))))
400 ((and (alike1 (leadop newfn
) (leadop form
))
401 (or (alike1 (leadarg newfn
) (leadarg form
))
403 (logequiv form newfn
))))
406 (getfncoeff (cdr a
) form
))))
407 ((do ((vl varlist
(cdr vl
))) ((null vl
))
408 (and (not (atom (car vl
)))
409 (alike1 (leadop (car vl
)) (leadop newfn
))
411 (logequiv (car vl
) newfn
)
412 (alike1 (car vl
) newfn
))
413 (rplaca (cddar a
) (car vl
))
415 ((let (vlist) (newvar1 (car a
)) (null vlist
))
417 (ratpl (cdr (ratrep* (car a
)))
420 (getfncoeff (cdr a
) form
))
424 (push (dilog (cons (car a
) form
)) lians
)
426 (getfncoeff (cdr a
) form
))
430 (logequiv form newfn
))
431 (push (mul* (cadar a
) (make-li (1+ (car (subfunsubs form
)))
435 (getfncoeff (cdr a
) form
))
436 (t (setq nogood t
) 0))))
437 (t (rplaca a
(list '(mtimes) 1 (car a
)))
438 (getfncoeff a form
))))
441 (defun rischlogpoly (exp)
442 (cond ((equal exp
'(0 .
1)) (rischzero))
443 (expflag (push (cons 'poly exp
) expint
)
445 ((not (among var exp
)) (tryrisch1 exp mainvar
))
446 (t (do ((degree (pdegree (car exp
) var
) (1- degree
))
452 (y) (z) (ak) (nogood) (lbkpl1))
453 ((minusp degree
) (cons sum
(append lians
(cdr y
))))
454 (setq ak
(r- (ratqu (polcoef p degree
) den
)
455 (r* (cons (1+ degree
) 1)
457 (get var
'rischdiff
))))
458 (if (not (pzerop (polcoef p degree
)))
459 (setq p
(if (pcoefp p
) (pzero) (psimp var
(p-red p
)))))
460 (setq y
(tryrisch1 ak mainvar
))
462 (and (> degree
0) (setq liflag $liflag
))
463 (setq z
(getfncoeff (cdr y
) (get var
'rischexpr
)))
465 (cond ((and (> degree
0)
466 (or nogood
(findint (cdr y
))))
467 (return (rischnoun sum
(r+ (r* ak
468 (make-poly var degree
1))
470 (setq lbkpl1
(ratqu z
(cons (1+ degree
) 1)))
471 (setq sum
(r+ (r* lbkpl1
(make-poly var
(1+ degree
) 1))
472 (r* cary
(if (zerop degree
) 1
473 (make-poly var degree
1)))
476 (defun make-li (sub arg
)
477 (subfunmake '$li
(ncons sub
) (ncons arg
)))
479 ;;integrates log(ro)^degree*log(rn)' in terms of polylogs
480 ;;finds constants c,d and integers j,k such that
481 ;;c*ro^j+d=rn^k If ro and rn are poly's then can assume either j=1 or k=1
483 (destructuring-let* ((((nil coef nlog
) . olog
) l
)
484 (narg (remabs (cadr nlog
)))
487 (rn (rform narg
)) ;; can add new vars to varlist
488 (ro (rform (cadr olog
)))
490 ((j . k
) (ratreduce (pdegree (car rn
) var
) (pdegree (car ro
) var
)))
493 (cond ((and (= j
1) (> k
1))
494 (setq rn
(ratexpt rn k
)
497 ((and (= k
1) (> j
1))
498 (setq ro
(ratexpt ro j
)
499 coef
(div coef
(f* j degree
))
501 (desetq (rc . rd
) (ratdivide rn ro
))
502 (cond ((and (freeof intvar
(rdis rc
)) ;; can't use risch-constp because varlist
503 (freeof intvar
(rdis rd
))) ;; is not set up with vars in correct order.
504 (setq narg
($ratsimp
(sub 1 (div narg
(rdis rd
)))))
505 (mul* coef
(power -
1 (1+ degree
))
506 `((mfactorial) ,degree
)
507 (dosum (mul* (power -
1 idx
)
508 (div* (power olog idx
)
509 `((mfactorial) ,idx
))
510 (make-li (add degree
(neg idx
) 1) narg
))
512 (t (setq nogood t
) 0))))
514 (defun exppolycontrol (flag f a expg n
)
515 (let (y l var
(varlist varlist
) (genvar genvar
))
516 (setq varlist
(reverse (cdr (reverse varlist
))))
517 (setq var
(getrischvar))
518 (setq y
(get var
'leadop
))
519 (cond ((and (not (pzerop (ratnumerator f
)))
520 (risch-constp (setq l
(ratqu a f
))))
521 (cond (flag ;; multiply in expg^n - n may be negative
522 (list (r* l
(ratexpt (cons (list expg
1 1) 1) n
))
526 (rischexpvar nil flag
(list f a expg n
)))
527 (t (rischexplog (eq y
'mexpt
) flag f a
528 (list expg n
(get var
'rischarg
)
529 var
(get var
'rischdiff
)))))))
531 (defun rischexppoly (expint var
)
532 (let (y w num denom type
(ans (rischzero))
533 (expdiff (ratqu (get var
'rischdiff
) (list var
1 1))))
534 (do ((expint expint
(cdr expint
)))
536 (desetq (type . y
) (car expint
))
537 (desetq (num . denom
) (ratfix y
))
538 (cond ((eq type
'neg
)
539 (setq w
(exppolycontrol t
542 (ratqu num
(caddr denom
))
545 ((or (numberp num
) (not (eq (car num
) var
)))
546 (setq w
(tryrisch1 y mainvar
)))
547 (t (setq w
(rischzero))
548 (do ((num (cdr num
) (cddr num
))) ((null num
))
549 (cond ((equal (car num
) 0)
551 (tryrisch1 (ratqu (cadr num
) denom
) mainvar
)
553 (t (setq w
(rischadd (exppolycontrol
555 (r* (car num
) expdiff
)
556 (ratqu (cadr num
) denom
)
560 (setq ans
(rischadd w ans
)))))
562 (defun rischexpvar (expexpflag flag l
)
563 (prog (lcm y m p alphar beta gamma delta r s
564 tt denom k wl wv i ytemp ttemp yalpha f a expg n yn yd
)
565 (desetq (f a expg n
) l
)
566 (cond ((or (pzerop a
) (pzerop (car a
)))
567 (return (cond ((null flag
) (rzero))
569 (setq denom
(ratdenominator f
))
570 (setq p
(findpr (cdr (partfrac a mainvar
))
571 (cdr (partfrac f mainvar
))))
572 (setq lcm
(plcm (ratdenominator a
) p
))
573 (setq y
(ratpl (spderivative (cons 1 p
) mainvar
)
575 (setq lcm
(plcm lcm
(ratdenominator y
)))
576 (setq r
(car (ratqu lcm p
)))
577 (setq s
(car (r* lcm y
)))
578 (setq tt
(car (r* a lcm
)))
579 (setq beta
(pdegree r mainvar
))
580 (setq gamma
(pdegree s mainvar
))
581 (setq delta
(pdegree tt mainvar
))
582 (setq alphar
(max (- (1+ delta
) beta
)
585 (cond ((equal (1- beta
) gamma
)
587 (ratqu (polcoef s gamma
)
589 (and (equal (cdr y
) 1)
592 (setq alphar
(max alphar m
))
594 (return (if flag
(cxerfarg (rzero) expg n a
) nil
)))
595 (cond ((not (and (equal alphar m
) (not (zerop m
))))
597 (setq k
(+ alphar beta -
2))
599 l2
(setq wv
(list (cons (polcoef tt k
) 1)))
602 (cons (r+ (r* (cons i
1)
603 (polcoef r
(+ k
1 (- i
))))
604 (cons (polcoef s
(+ k
(- i
))) 1))
607 (cond ((> i -
1) (go l1
)))
608 (setq wl
(cons wv wl
))
610 (cond ((> k -
1) (go l2
)))
612 (if (or (eq y
'singular
) (eq y
'inconsistent
))
613 (cond ((null flag
) (return nil
))
614 (t (return (cxerfarg (rzero) expg n a
)))))
619 (r+ (r* (car y
) (pexpt (list mainvar
1 1) k
))
624 (return (cond ((null flag
) (ratqu lcm p
))
625 (t (list (r* (ratqu lcm p
)
626 (cons (list expg n
1) 1))
629 down2
(cond ((> (1- beta
) gamma
)
630 (setq k
(+ alphar
(1- beta
)))
631 (setq denom
'(ratti alphar
(polcoef r beta
) t
)))
633 (setq k
(+ alphar gamma
))
634 (setq denom
'(polcoef s gamma
)))
635 (t (setq k
(+ alphar gamma
))
637 '(ratpl (ratti alphar
(polcoef r beta
) t
)
638 (polcoef s gamma
)))))
640 loop
(setq yn
(polcoef (ratnumerator tt
) k
)
641 yd
(r* (ratdenominator tt
) ;DENOM MAY BE 0
642 (cond ((zerop alphar
) (polcoef s gamma
))
645 (cond ((pzerop yn
) (setq k
(1- k
) alphar
(1- alphar
))
646 (go loop
)) ;need more constraints?
648 ((null flag
) (return nil
))
649 (t (return (cxerfarg (rzero) expg n a
)))))))
650 (t (setq yalpha
(ratqu yn yd
))))
651 (setq ytemp
(r+ y
(r* yalpha
652 (cons (list mainvar alphar
1) 1) )))
653 (setq ttemp
(r- tt
(r* yalpha
654 (r+ (r* s
(cons (list mainvar alphar
1) 1))
656 (list mainvar
(1- alphar
) 1))))))
663 ((null flag
) (return (ratqu ytemp p
)))
664 (t (return (list (ratqu (r* ytemp
(cons (list expg n
1) 1))
667 ((null flag
) (return nil
))
668 ((and (risch-constp (setq ttemp
(ratqu ttemp lcm
)))
670 (equal (pdegree (car (get expg
'rischarg
)) mainvar
) 2)
671 (equal (pdegree (cdr (get expg
'rischarg
)) mainvar
) 0))
672 (return (list (ratqu (r* ytemp
(cons (list expg n
1) 1)) p
)
673 (erfarg2 (r* n
(get expg
'rischarg
)) ttemp
))))
676 (ratqu (r* y
(cons (list expg n
1) 1)) p
)
685 ;; *JM should be declared as an array, although it is not created
686 ;; by this file. -- cwh
689 (prog (d *mosesflag m m2
)
690 (setq d
(length (car mm
)))
691 ;; MTOA stands for MATRIX-TO-ARRAY. An array is created and
692 ;; associated functionally with the symbol *JM. The elements
693 ;; of the array are initialized from the matrix MM.
694 (mtoa '*jm
* (length mm
) d mm
)
695 (setq m
(tfgeli '*jm
* (length mm
) d
))
696 (cond ((or (and (null (car m
)) (null (cadr m
)))
698 (> (length (car m
)) (- (length mm
) (1- d
)))))
700 ((cadr m
) (return 'inconsistent
)))
702 (ptorat '*jm
* (1- d
) d
)
703 (setq m2
(xrutout '*jm
* (1- d
) d nil nil
))
704 (setq m2
(lsafix (cdr m2
) (caddr m
)))
708 (declare (special *jm
*))
712 (setf (aref *jm
* 1 (car n
)) (car l
)))
713 (do ((s (length l
) (1- s
))
715 ((= s
0) (cons '(list) ans
))
716 (setq ans
(cons (aref *jm
* 1 s
) ans
))))
719 (defun findpr (alist flist
&aux
(p 1) alphar fterm
)
720 (do ((alist alist
(cdr alist
))) ((null alist
))
721 (setq fterm
(findflist (cadar alist
) flist
))
722 (if fterm
(setq flist
(remove y flist
:count
1 :test
#'eq
)))
724 (cond ((null fterm
) (caddar alist
))
725 ((equal (caddr fterm
) 1)
726 (fpr-dif (car flist
) (caddar alist
)))
727 (t (max (- (caddar alist
) (caddr fterm
)) 0))))
728 (if (not (zerop alphar
))
729 (setq p
(ptimes p
(pexpt (cadar alist
) alphar
)))))
730 (do ((flist flist
(cdr flist
)))
732 (when (equal (caddar flist
) 1)
733 (setq alphar
(fpr-dif (car flist
) 0))
734 (setq p
(ptimes p
(pexpt (cadar flist
) alphar
)))))
737 (defun fpr-dif (fterm alpha
)
738 (destructuring-let* (((num den mult
) fterm
)
739 (m (spderivative den mainvar
))
741 (cond ((rzerop m
) alpha
)
742 (t (setq n
(ratqu (cdr (ratdivide num den
))
744 (if (and (equal (cdr n
) 1) (numberp (car n
)))
748 (defun findflist (a llist
)
749 (cond ((null llist
) nil
)
750 ((equal (cadar llist
) a
) (car llist
))
751 (t (findflist a
(cdr llist
)))))
754 (defun rischexplog (expexpflag flag f a l
)
755 (declare (special var
))
756 (prog (lcm y yy m p alphar beta gamma delta
757 mu r s tt denom ymu rbeta expg n eta logeta logdiff
758 temp cary nogood vector aarray rmu rrmu rarray
)
759 (desetq (expg n eta logeta logdiff
) l
)
760 (cond ((or (pzerop a
) (pzerop (car a
)))
761 (return (cond ((null flag
) (rzero))
763 (setq p
(findpr (cdr (partfrac a var
)) (cdr (partfrac f var
))))
764 (setq lcm
(plcm (ratdenominator a
) p
))
765 (setq y
(ratpl (spderivative (cons 1 p
) mainvar
)
767 (setq lcm
(plcm lcm
(ratdenominator y
)))
768 (setq r
(car (ratqu lcm p
)))
769 (setq s
(car (r* lcm y
)))
770 (setq tt
(car (r* a lcm
)))
771 (setq beta
(pdegree r var
))
772 (setq gamma
(pdegree s var
))
773 (setq delta
(pdegree tt var
))
774 (cond (expexpflag (setq mu
(max (- delta beta
)
777 (setq mu
(max (- (1+ delta
) beta
)
778 (- (1+ delta
) gamma
)))
779 (cond ((< beta gamma
) (go back
))
780 ((= (1- beta
) gamma
) (go down1
)))
781 (setq y
(tryrisch1 (ratqu (r- (r* (polcoef r
(1- beta
))
784 (polcoef s
(1- gamma
))))
789 (setq yy
(getfncoeff (cdr y
) (get var
'rischexpr
)))
790 (cond ((and (not (findint (cdr y
)))
799 (cond ((not (equal beta gamma
)) (go back
)))
800 (setq y
(tryrisch1 (ratqu (polcoef s gamma
) (polcoef r beta
))
802 (cond ((findint (cdr y
)) (go back
)))
803 (setq yy
(ratqu (r* -
1 (car y
)) eta
))
804 (cond ((and (equal (cdr yy
) 1)
809 down1
(setq y
(tryrisch1 (ratqu (polcoef s gamma
) (polcoef r beta
))
812 (setq yy
(getfncoeff (cdr y
) (get var
'rischexpr
)))
813 (cond ((and (not (findint (cdr y
)))
818 (setq mu
(- (car yy
)))))
820 (return (if flag
(cxerfarg (rzero) expg n a
) nil
)))
821 (cond ((> beta gamma
)(go lsacall
))
824 (setq denom
(polcoef s gamma
))
827 (setq ymu
(ratqu (polcoef (ratnumerator tt
) (+ mu gamma
))
828 (r* (ratdenominator tt
) denom
)))
829 (setq y
(r+ y
(setq ymu
(r* ymu
(pexpt (list logeta
1 1) mu
) ))))
832 (r* r
(spderivative ymu mainvar
))))
834 (cond ((not (< mu
0)) (go linearloop
))
835 ((not flag
) (return (if (rzerop tt
) (ratqu y p
) nil
)))
837 (return (cons (ratqu (r* y
(cons (list expg n
1) 1)) p
) '(0))))
838 (t (return (cxerfarg (ratqu (r* y
(cons (list expg n
1) 1)) p
)
843 (setq rbeta
(polcoef r beta
))
846 (setq f
(r+ (ratqu (polcoef s gamma
) rbeta
)
848 (r* mu
(spderivative eta mainvar
))
850 (setq ymu
(exppolycontrol nil
852 (ratqu (polcoef (ratnumerator tt
)
854 (r* (ratdenominator tt
) rbeta
))
857 (return (cond ((null flag
) nil
)
858 (t (return (cxerfarg (ratqu (r* y
(cons (list expg n
1) 1)) p
)
859 expg n
(ratqu tt lcm
)))))))
860 (setq y
(r+ y
(setq ymu
(r* ymu
(pexpt (list logeta
1 1) mu
)))))
863 (r* r
(spderivative ymu mainvar
))))
866 ((not (< mu
0)) (go recurseloop
))
868 (return (cond ((rzerop tt
) (ratqu y p
)) (t nil
))))
870 (return (cons (ratqu (r* y
(cons (list expg n
1) 1)) p
) '(0))))
871 (t (return (cxerfarg (ratqu (r* y
(cons (list expg n
1) 1)) p
)
878 (setq temp
(r* (ratexpt (cons (list logeta
1 1) 1) (1- mu
))
879 (r+ (r* s
(cons (list logeta
1 1) 1))
880 (r* mu r logdiff
))))
881 mu1
(setq vector nil
)
882 (setq rmu
(+ rrmu beta
))
884 (setq vector
(cons (ratqu (polcoef (ratnumerator temp
) rmu
)
885 (ratdenominator temp
)) vector
))
887 (unless (< rmu
0) (go rmuloop
))
889 (setq aarray
(append aarray
(list (reverse vector
))))
890 (cond ((not (< mu
0)) (go muloop
))
891 ((equal mu -
2) (go skipmu
)))
898 (setq vector
(mapcar 'car aarray
))
899 (setq aarray
(mapcar 'cdr aarray
))
900 (setq rarray
(append rarray
(list vector
)))
901 (unless (null (car aarray
)) (go arrayloop
))
905 (setq vector
(cons '(0 .
1) vector
))
907 (unless (< rmu
0) (go array1loop
))
910 (cond ((equal (car rarray
) vector
) nil
)
911 (t (setq aarray
(cons (car rarray
) aarray
))))
912 (setq rarray
(cdr rarray
))
913 (when rarray
(go array2loop
))
914 (setq rarray
(reverse aarray
))
915 (setq temp
(lsa rarray
))
916 (when (or (eq temp
'singular
) (eq temp
'inconsistent
))
917 (return (if (null flag
) nil
(cxerfarg (rzero) expg n a
))))
918 (setq temp
(reverse (cdr temp
)))
921 l3
(setq y
(r+ y
(r* (car temp
) (pexpt (list logeta
1 1) rmu
))))
922 (setq temp
(cdr temp
))
924 (unless (> rmu rrmu
) (go l3
))
925 (return (if (null flag
)
927 (cons (r* (list expg n
1) (ratqu y p
)) '(0))))))
930 (defun erfarg (exparg coef
)
931 (prog (num denom erfarg
)
932 (setq exparg
(r- exparg
))
933 (unless (and (setq num
(pnthrootp (ratnumerator exparg
) 2))
934 (setq denom
(pnthrootp (ratdenominator exparg
) 2)))
936 (setq erfarg
(cons num denom
))
938 (setq coef
(ratqu coef
(spderivative erfarg mainvar
))))
939 (return (simplify `((mtimes) ((rat) 1 2)
940 ((mexpt) $%pi
((rat) 1 2))
942 ((%erf
) ,(disrep erfarg
))))))))
944 (defun erfarg2 (exparg coeff
&aux
(var mainvar
) a b c d
)
945 (when (and (= (pdegree (car exparg
) var
) 2)
946 (eq (caar exparg
) var
)
947 (risch-pconstp (cdr exparg
))
948 (risch-constp coeff
))
949 (setq a
(ratqu (r* -
1 (caddar exparg
))
951 (setq b
(disrep (ratqu (r* -
1 (polcoef (car exparg
) 1))
953 (setq c
(disrep (ratqu (r* (polcoef (car exparg
) 0))
959 ((mexpt) $%e
((mplus) ,c
960 ((mquotient) ((mexpt) ,b
2)
965 ((mexpt) $%pi
((rat) 1 2)))
967 ((mtimes) ,d
,intvar
)
968 ((mtimes) ,b
((rat) 1 2) ((mexpt) ,d -
1))))))))
971 (defun cxerfarg (ans expg n numdenom
&aux
(arg (r* n
(get expg
'rischarg
)))
973 (prog (denom erfans num nerf
)
974 (desetq (num . denom
) numdenom
)
975 (unless $erfflag
(setq fails num
) (go lose
))
976 (if (setq erfans
(erfarg arg numdenom
))
977 (return (list ans erfans
)))
978 again
(when (and (not (pcoefp denom
))
980 (eq (get (car denom
) 'leadop
) 'mexpt
))
981 (setq arg
(r+ arg
(r* (- (p-le denom
))
982 (get (p-var denom
) 'rischarg
)))
985 (loop for
(coef exparg exppoly
) in
(explist num arg
1)
986 do
(setq coef
(ratqu coef denom
)
987 nerf
(or (erfarg2 exparg coef
) (erfarg exparg coef
)))
988 (if nerf
(push nerf erfans
) (setq fails
989 (pplus fails exppoly
))))
991 (if (pzerop fails
) (cons ans erfans
)
992 (rischadd (cons ans erfans
)
993 (rischnoun (r* (ratexpt (cons (make-poly expg
) 1) n
)
994 (ratqu fails
(cdr numdenom
)))))))))
996 (defun explist (p oarg exps
)
997 (cond ((or (pcoefp p
) (not (eq 'mexpt
(get (p-var p
) 'leadop
))))
998 (list (list p oarg
(ptimes p exps
))))
999 (t (loop with narg
= (get (p-var p
) 'rischarg
)
1000 for
(exp coef
) on
(p-terms p
) by
#'cddr
1002 (r+ oarg
(r* exp narg
))
1004 (make-poly (p-var p
) exp
1)))))))
1007 (declare-top (special *fnewvarsw
))
1009 (defun intsetup (exp *var
)
1010 (prog (varlist clist $factorflag dlist genpairs old y z $ratfac $keepfloat
1012 y
(setq exp
(radcan1 exp
))
1019 (cond ((freeof *var y
) (push y clist
))
1022 (not (eq (cadr y
) '$%e
)))
1023 (cond ((not (freeof *var
(caddr y
)))
1024 (setq dlist
`((mexpt simp
)
1027 `((%log
) ,(cadr y
)))))
1028 (setq exp
(maxima-substitute dlist y exp
))
1029 (setq varlist nil
) (go y
))
1031 (cond ((numberp (caddr y
)) (push y dlist
))
1032 (t (setq operator t
)(return nil
))))
1033 (t (push y dlist
))))
1036 (if (member '$%i clist
:test
#'eq
) (setq clist
(cons '$%i
(delete '$%i clist
:test
#'equal
))))
1037 (setq varlist
(append clist
1039 (nreverse (sort (append dlist nil
) #'intgreat
)))))
1040 (orderpointer varlist
)
1042 (mapc #'intset1
(cons *var dlist
))
1043 (cond ((alike old varlist
) (return (ratrep* exp
)))
1047 (cond ((atom exp
) exp
)
1048 ((mqapplyp exp
) (cadr exp
))
1051 (defun leadarg (exp)
1052 (cond ((atom exp
) 0)
1053 ((and (mexptp exp
) (eq (cadr exp
) '$%e
)) (caddr exp
))
1054 ((mqapplyp exp
) (car (subfunargs exp
)))
1060 (setq d
(if (mexptp b
) ;needed for radicals
1063 ,(radcan1 (sdiff (simplify (caddr b
)) *var
)))
1064 (radcan1 (sdiff (simplify b
) *var
)))))
1065 (setq d
(ratrep* d
))
1066 (setq c
(ratrep* (leadarg b
)))
1067 (setq e
(cdr (assoc b
(pair varlist genvar
) :test
#'equal
)))
1068 (putprop e
(leadop b
) 'leadop
)
1069 (putprop e b
'rischexpr
)
1070 (putprop e
(cdr d
) 'rischdiff
)
1071 (putprop e
(cdr c
) 'rischarg
)))
1073 ;; order of expressions for risch.
1074 ;; expressions containing erf and li last.
1075 ;; then order by size of expression to guarantee that
1076 ;; any subexpressions are considered smaller.
1077 ;; this relation should be transitive, since it is called by sort.
1078 (defun intgreat (a b
)
1079 (cond ((and (not (atom a
)) (not (atom b
)))
1080 (cond ((and (not (freeof '%erf a
)) (freeof '%erf b
)) t
)
1081 ((and (not (freeof '$li a
)) (freeof '$li b
)) t
)
1082 ((and (freeof '$li a
) (not (freeof '$li b
))) nil
)
1083 ((and (freeof '%erf a
) (not (freeof '%erf b
))) nil
)
1084 ((> (conssize a
) (conssize b
)) t
)
1085 ((< (conssize a
) (conssize b
)) nil
)
1086 (t (great (resimplify (fixintgreat a
))
1087 (resimplify (fixintgreat b
))))))
1088 (t (great (resimplify (fixintgreat a
))
1089 (resimplify (fixintgreat b
))))))
1091 (defun fixintgreat (a)
1092 (subst '/_101x
*var a
))
1094 (declare-top (unspecial b beta cary context
*exp degree gamma
1095 klth liflag m nogood operator
1096 r s switch switch1
*var var y
))