Updating configure.ac with new version number
[maxima/cygwin.git] / src / risch.lisp
blob29821a6d18b2e9e2ebf1527edde1045bd4d9eadf
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
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)))
45 (defun getrischvar ()
46 (do ((vl varlist (cdr vl))
47 (gl genvar (cdr gl)))
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)
58 (setq r (ratfix r))
59 (and (risch-pconstp (car r)) (risch-pconstp (cdr r))))
61 ;; adds two risch expressions (defined above).
62 (defun rischadd (x y)
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)
70 (rischint exp var))))
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)
80 (cdr p))
81 (r* (car p) denprime))
82 (r* (cdr p) (cdr p)))))))
83 (t (r+ (spderivative1 (car p)
84 (cadr p)
85 (caddr p)
86 var)
87 (spderivative (cons (car p) (cdddr p))
88 var)))))
90 (defun spderivative1 (var1 deg coeff var)
91 (cond ((eq var1 var)
92 (r* (ratexpt (cons (list var 1 1) 1) (1- deg))
93 (pctimes deg coeff)))
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)
99 (t (r* deg
100 coeff
101 (ratexpt (cons (list var1 1 1) 1)
102 (1- deg)))))
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)
115 ($logexpand t))
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)))
120 (if (mnump intvar)
121 (merror (intl:gettext "risch: attempt to integrate wrt a number: ~:M") intvar))
122 (if (and (atom intvar) (isinop exp intvar)) (go noun))
123 (rischform exp)
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)
143 (disrep (car z))
144 (cdr z)))))))
145 noun (return (list '(%integrate) exp intvar))))
147 (defun rischform (l)
148 (cond ((or (atom l) (alike1 intvar l) (freeof intvar l)) nil)
149 ((polylogp l)
150 (if (and (integerp (car (subfunsubs l)))
151 (signp g (car (subfunsubs l))))
152 (rischform (car (subfunargs l)))
153 (setq operator t)))
154 ((atom (caar l))
155 (case (caar 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)))
184 result)))
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))
192 ($realpart yyy)
193 ($rectform 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)))
201 result))))
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)
210 'mexpt)
211 (setq expflag t)))
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)
217 y))))
219 (defun tryrisch1 (exp mainvar)
220 (let* ((varlist (reverse (cdr (reverse varlist))))
221 (var (getrischvar)))
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)
233 (setq ans '(0 . 1))
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)))
242 ((= klth 1))
243 (setq arootf (car rootfactor))
244 (cond
245 ((pcoefp arootf))
246 ((and (eq (get (car arootf) 'leadop) 'mexpt)
247 (null (cdddr arootf)))
248 (setq
249 expint
250 (append
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)))
255 (t (list
256 (list 'neg (car parnumer)
257 (car arootf) klth (cadr arootf)))))
258 expint)))
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))))
268 (setq thetop
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)
283 (car denom)
284 (- klth (car 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))))
295 (setq parnumer nil)
296 (setq switch1 t)
297 (desetq (ncc p1e) (oldcontent p1e))
298 (cprog p1e p2e)
299 (setq allcc (ratqu ncc dcc))
300 (return (do ((pnum parnumer (cdr pnum))
301 (pden pardenom (cdr pden))
302 (ans (rischzero)))
303 ((or (null pnum) (null pden))
304 (setq switch1 nil) ans)
305 (setq ans (rischadd
306 (rischlogeprog
307 (r* allcc (ratqu (car pnum) (car pden))))
308 ans))))))
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)
313 (make-poly var)))))
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)
323 (if expflag
324 (setq expstuff (r- expstuff (r* expcoef logcoef))))
325 (return
326 (list
327 '(0 . 1)
328 (list '(mtimes)
329 (disrep logcoef)
330 (logmabs (disrep p2e))))))
331 (if (and expflag $liflag changevp)
332 (let* ((newvar (gensym))
333 (new-int ($changevar
334 `((%integrate) ,(simplify (disrep p)) ,intvar)
335 (sub newvar (get var 'rischexpr))
336 newvar intvar))
337 (changevp nil)) ;prevents recursive changevar
338 (if (and (freeof intvar new-int)
339 (freeof '%integrate
340 (setq new-int (rischint (sdiff new-int newvar)
341 newvar))))
342 (return
343 (list (rzero)
344 (maxima-substitute (get var 'rischexpr) newvar new-int))))))
345 (return (rischnoun p))))
348 (defun findint (exp)
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))))))
358 (defun remabs (exp)
359 (cond ((atom exp) exp)
360 ((eq (caar exp) 'mabs) (cadr exp))
361 (t exp)))
363 (declare-top (special vlist lians degree))
365 (defun getfnsplit (l)
366 (let (coef fn)
367 (dolist (x l (values (muln coef nil) (muln fn nil)))
368 (if (free x intvar)
369 (push x coef)
370 (push x fn)))))
372 (defun getfncoeff (a form)
373 (cond ((null a) 0)
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)))))
399 (getfncoeff a form))
400 ((and (alike1 (leadop newfn) (leadop form))
401 (or (alike1 (leadarg newfn) (leadarg form))
402 (and (mlogp newfn)
403 (logequiv form newfn))))
404 (ratpl (rform coef)
405 (prog2 (rplaca a 0)
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))
410 (if (mlogp newfn)
411 (logequiv (car vl) newfn)
412 (alike1 (car vl) newfn))
413 (rplaca (cddar a) (car vl))
414 (return nil))))
415 ((let (vlist) (newvar1 (car a)) (null vlist))
416 (setq cary
417 (ratpl (cdr (ratrep* (car a)))
418 cary))
419 (rplaca a 0)
420 (getfncoeff (cdr a) form))
421 ((and liflag
422 (mlogp form)
423 (mlogp newfn))
424 (push (dilog (cons (car a) form)) lians)
425 (rplaca a 0)
426 (getfncoeff (cdr a) form))
427 ((and liflag
428 (polylogp form)
429 (mlogp newfn)
430 (logequiv form newfn))
431 (push (mul* (cadar a) (make-li (1+ (car (subfunsubs form)))
432 (leadarg form)))
433 lians)
434 (rplaca a 0)
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)
444 (rischzero))
445 ((not (among var exp)) (tryrisch1 exp mainvar))
446 (t (do ((degree (pdegree (car exp) var) (1- degree))
447 (p (car exp))
448 (den (cdr exp))
449 (lians ())
450 (sum (rzero))
451 (cary (rzero))
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)
456 cary
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))
461 (setq cary (car y))
462 (and (> degree 0) (setq liflag $liflag))
463 (setq z (getfncoeff (cdr y) (get var 'rischexpr)))
464 (setq liflag nil)
465 (cond ((and (> degree 0)
466 (or nogood (findint (cdr y))))
467 (return (rischnoun sum (r+ (r* ak
468 (make-poly var degree 1))
469 (ratqu p den))))))
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)))
474 sum))))))
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
482 (defun dilog (l)
483 (destructuring-let* ((((nil coef nlog) . olog) l)
484 (narg (remabs (cadr nlog)))
485 (varlist varlist)
486 (genvar genvar)
487 (rn (rform narg)) ;; can add new vars to varlist
488 (ro (rform (cadr olog)))
489 (var (caar ro))
490 ((j . k) (ratreduce (pdegree (car rn) var) (pdegree (car ro) var)))
491 (idx (gensym))
492 (rc) (rd))
493 (cond ((and (= j 1) (> k 1))
494 (setq rn (ratexpt rn k)
495 coef (div coef k)
496 narg (rdis rn)))
497 ((and (= k 1) (> j 1))
498 (setq ro (ratexpt ro j)
499 coef (div coef (f* j degree))
500 olog (mul j olog))))
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))
511 idx 0 degree t)))
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))
524 (t l)))
525 ((eq y intvar)
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)))
535 ((null expint) ans)
536 (desetq (type . y) (car expint))
537 (desetq (num . denom) (ratfix y))
538 (cond ((eq type 'neg)
539 (setq w (exppolycontrol t
540 (r* (- (cadr denom))
541 expdiff)
542 (ratqu num (caddr denom))
544 (- (cadr 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)
550 (setq w (rischadd
551 (tryrisch1 (ratqu (cadr num) denom) mainvar)
552 w)))
553 (t (setq w (rischadd (exppolycontrol
555 (r* (car num) expdiff)
556 (ratqu (cadr num) denom)
558 (car num))
559 w)))))))
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))
568 (t (rischzero))))))
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)
574 (ratqu f p)))
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)
583 (- delta gamma)))
584 (setq m 0)
585 (cond ((equal (1- beta) gamma)
586 (setq y (r* -1
587 (ratqu (polcoef s gamma)
588 (polcoef r beta))))
589 (and (equal (cdr y) 1)
590 (numberp (car y))
591 (setq m (car y)))))
592 (setq alphar (max alphar m))
593 (if (minusp alphar)
594 (return (if flag (cxerfarg (rzero) expg n a) nil)))
595 (cond ((not (and (equal alphar m) (not (zerop m))))
596 (go down2)))
597 (setq k (+ alphar beta -2))
598 (setq wl nil)
599 l2 (setq wv (list (cons (polcoef tt k) 1)))
600 (setq i alphar)
601 l1 (setq wv
602 (cons (r+ (r* (cons i 1)
603 (polcoef r (+ k 1 (- i))))
604 (cons (polcoef s (+ k (- i))) 1))
605 wv))
606 (decf i)
607 (cond ((> i -1) (go l1)))
608 (setq wl (cons wv wl))
609 (decf k)
610 (cond ((> k -1) (go l2)))
611 (setq y (lsa wl))
612 (if (or (eq y 'singular) (eq y 'inconsistent))
613 (cond ((null flag) (return nil))
614 (t (return (cxerfarg (rzero) expg n a)))))
615 (setq k 0)
616 (setq lcm 0)
617 (setq y (cdr y))
618 l3 (setq lcm
619 (r+ (r* (car y) (pexpt (list mainvar 1 1) k))
620 lcm))
621 (incf k)
622 (setq y (cdr y))
623 (cond ((null y)
624 (return (cond ((null flag) (ratqu lcm p))
625 (t (list (r* (ratqu lcm p)
626 (cons (list expg n 1) 1))
627 0))))))
628 (go l3)
629 down2 (cond ((> (1- beta) gamma)
630 (setq k (+ alphar (1- beta)))
631 (setq denom '(ratti alphar (polcoef r beta) t)))
632 ((< (1- beta) gamma)
633 (setq k (+ alphar gamma))
634 (setq denom '(polcoef s gamma)))
635 (t (setq k (+ alphar gamma))
636 (setq denom
637 '(ratpl (ratti alphar (polcoef r beta) t)
638 (polcoef s gamma)))))
639 (setq y 0)
640 loop (setq yn (polcoef (ratnumerator tt) k)
641 yd (r* (ratdenominator tt) ;DENOM MAY BE 0
642 (cond ((zerop alphar) (polcoef s gamma))
643 (t (eval denom))) ))
644 (cond ((rzerop yd)
645 (cond ((pzerop yn) (setq k (1- k) alphar (1- alphar))
646 (go loop)) ;need more constraints?
647 (t (cond
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))
655 (r* r alphar
656 (list mainvar (1- alphar) 1))))))
657 (decf k)
658 (decf alphar)
659 (cond ((< alphar 0)
660 (cond
661 ((rzerop ttemp)
662 (cond
663 ((null flag) (return (ratqu ytemp p)))
664 (t (return (list (ratqu (r* ytemp (cons (list expg n 1) 1))
666 0)))))
667 ((null flag) (return nil))
668 ((and (risch-constp (setq ttemp (ratqu ttemp lcm)))
669 $erfflag
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))))
674 (t (return
675 (cxerfarg
676 (ratqu (r* y (cons (list expg n 1) 1)) p)
677 expg
679 (ratqu tt lcm)))))))
680 (setq y ytemp)
681 (setq tt ttemp)
682 (go loop)))
685 ;; *JM should be declared as an array, although it is not created
686 ;; by this file. -- cwh
688 (defun lsa (mm)
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)))
697 (and (car m)
698 (> (length (car m)) (- (length mm) (1- d)))))
699 (return 'singular))
700 ((cadr m) (return 'inconsistent)))
701 (setq *mosesflag t)
702 (ptorat '*jm* (1- d) d)
703 (setq m2 (xrutout '*jm* (1- d) d nil nil))
704 (setq m2 (lsafix (cdr m2) (caddr m)))
705 (return m2)))
707 (defun lsafix (l n)
708 (declare (special *jm*))
709 (do ((n n (cdr n))
710 (l l (cdr l)))
711 ((null l))
712 (setf (aref *jm* 1 (car n)) (car l)))
713 (do ((s (length l) (1- s))
714 (ans))
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)))
723 (setq alphar
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)))
731 ((null 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))
740 (n))
741 (cond ((rzerop m) alpha)
742 (t (setq n (ratqu (cdr (ratdivide num den))
744 (if (and (equal (cdr n) 1) (numberp (car n)))
745 (max (car n) alpha)
746 alpha)))))
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))
762 (t (rischzero))))))
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)
766 (ratqu f p)))
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)
775 (- delta gamma)))
776 (go expcase)))
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))
782 (polcoef s gamma))
783 (r* (polcoef r beta)
784 (polcoef s (1- gamma))))
785 (r* (polcoef r beta)
786 (polcoef r beta) ))
787 mainvar))
788 (setq cary (car y))
789 (setq yy (getfncoeff (cdr y) (get var 'rischexpr)))
790 (cond ((and (not (findint (cdr y)))
791 (not nogood)
792 (not (atom yy))
793 (equal (cdr yy) 1)
794 (numberp (car yy))
795 (> (car yy) mu))
796 (setq mu (car yy))))
797 (go back)
798 expcase
799 (cond ((not (equal beta gamma)) (go back)))
800 (setq y (tryrisch1 (ratqu (polcoef s gamma) (polcoef r beta))
801 mainvar))
802 (cond ((findint (cdr y)) (go back)))
803 (setq yy (ratqu (r* -1 (car y)) eta))
804 (cond ((and (equal (cdr yy) 1)
805 (numberp (car yy))
806 (> (car yy) mu))
807 (setq mu (car yy))))
808 (go back)
809 down1(setq y (tryrisch1 (ratqu (polcoef s gamma) (polcoef r beta))
810 mainvar))
811 (setq cary (car y))
812 (setq yy (getfncoeff (cdr y) (get var 'rischexpr)))
813 (cond ((and (not (findint (cdr y)))
814 (not nogood)
815 (equal (cdr yy) 1)
816 (numberp (car yy))
817 (> (- (car yy)) mu))
818 (setq mu (- (car yy)))))
819 back (if (minusp mu)
820 (return (if flag (cxerfarg (rzero) expg n a) nil)))
821 (cond ((> beta gamma)(go lsacall))
822 ((= beta gamma)
823 (go recurse)))
824 (setq denom (polcoef s gamma))
825 (setq y '(0 . 1))
826 linearloop
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) ))))
830 (setq tt (r- tt
831 (r* s ymu)
832 (r* r (spderivative ymu mainvar))))
833 (decf mu)
834 (cond ((not (< mu 0)) (go linearloop))
835 ((not flag) (return (if (rzerop tt) (ratqu y p) nil)))
836 ((rzerop tt)
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)
839 expg
841 (ratqu tt lcm)))))
842 recurse
843 (setq rbeta (polcoef r beta))
844 (setq y '(0 . 1))
845 recurseloop
846 (setq f (r+ (ratqu (polcoef s gamma) rbeta)
847 (if expexpflag
848 (r* mu (spderivative eta mainvar))
849 0)))
850 (setq ymu (exppolycontrol nil
852 (ratqu (polcoef (ratnumerator tt)
853 (+ beta mu))
854 (r* (ratdenominator tt) rbeta))
855 expg n))
856 (when (null ymu)
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)))))
861 (setq tt (r- tt
862 (r* s ymu)
863 (r* r (spderivative ymu mainvar))))
864 (decf mu)
865 (cond
866 ((not (< mu 0)) (go recurseloop))
867 ((not flag)
868 (return (cond ((rzerop tt) (ratqu y p)) (t nil))))
869 ((rzerop tt)
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)
872 expg
874 (ratqu tt lcm)))))
875 lsacall
876 (setq rrmu mu)
877 muloop
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))
883 rmuloop
884 (setq vector (cons (ratqu (polcoef (ratnumerator temp) rmu)
885 (ratdenominator temp)) vector))
886 (decf rmu)
887 (unless (< rmu 0) (go rmuloop))
888 (decf mu)
889 (setq aarray (append aarray (list (reverse vector))))
890 (cond ((not (< mu 0)) (go muloop))
891 ((equal mu -2) (go skipmu)))
892 (setq temp tt)
893 (go mu1)
894 skipmu
895 (setq rarray nil)
896 arrayloop
897 (setq vector nil)
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))
902 (setq rmu (1+ rrmu))
903 (setq vector nil)
904 array1loop
905 (setq vector (cons '(0 . 1) vector))
906 (decf rmu)
907 (unless (< rmu 0) (go array1loop))
908 (setq aarray nil)
909 array2loop
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)))
919 (setq rmu 0)
920 (setq y 0)
921 l3 (setq y (r+ y (r* (car temp) (pexpt (list logeta 1 1) rmu))))
922 (setq temp (cdr temp))
923 (incf rmu)
924 (unless (> rmu rrmu) (go l3))
925 (return (if (null flag)
926 (ratqu y p)
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)))
935 (return nil))
936 (setq erfarg (cons num denom))
937 (if (risch-constp
938 (setq coef (ratqu coef (spderivative erfarg mainvar))))
939 (return (simplify `((mtimes) ((rat) 1 2)
940 ((mexpt) $%pi ((rat) 1 2))
941 ,(disrep coef)
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))
950 (cdr exparg)))
951 (setq b (disrep (ratqu (r* -1 (polcoef (car exparg) 1))
952 (cdr exparg))))
953 (setq c (disrep (ratqu (r* (polcoef (car exparg) 0))
954 (cdr exparg))))
955 (setq d (ratsqrt a))
956 (setq a (disrep a))
957 (simplify `((mtimes)
958 ((mtimes)
959 ((mexpt) $%e ((mplus) ,c
960 ((mquotient) ((mexpt) ,b 2)
961 ((mtimes) 4 ,a))))
962 ((rat) 1 2)
963 ,(disrep coeff)
964 ((mexpt) ,d -1)
965 ((mexpt) $%pi ((rat) 1 2)))
966 ((%erf) ((mplus)
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)))
972 (fails 0))
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))
979 (null (p-red denom))
980 (eq (get (car denom) 'leadop) 'mexpt))
981 (setq arg (r+ arg (r* (- (p-le denom))
982 (get (p-var denom) 'rischarg)))
983 denom (p-lc denom))
984 (go again))
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))))
990 lose (return
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
1001 nconc (explist coef
1002 (r+ oarg (r* exp narg))
1003 (ptimes exps
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
1011 *fnewvarsw)
1012 y (setq exp (radcan1 exp))
1013 (fnewvar exp)
1014 (setq *fnewvarsw t)
1015 a (setq clist nil)
1016 (setq dlist nil)
1017 (setq z varlist)
1018 up (setq y (pop z))
1019 (cond ((freeof *var y) (push y clist))
1020 ((eq y *var) nil)
1021 ((and (mexptp y)
1022 (not (eq (cadr y) '$%e)))
1023 (cond ((not (freeof *var (caddr y)))
1024 (setq dlist `((mexpt simp)
1026 ,(mul2* (caddr y)
1027 `((%log) ,(cadr y)))))
1028 (setq exp (maxima-substitute dlist y exp))
1029 (setq varlist nil) (go y))
1030 ((atom (caddr y))
1031 (cond ((numberp (caddr y)) (push y dlist))
1032 (t (setq operator t)(return nil))))
1033 (t (push y dlist))))
1034 (t (push y dlist)))
1035 (if z (go up))
1036 (if (member '$%i clist :test #'eq) (setq clist (cons '$%i (delete '$%i clist :test #'equal))))
1037 (setq varlist (append clist
1038 (cons *var
1039 (nreverse (sort (append dlist nil) #'intgreat)))))
1040 (orderpointer varlist)
1041 (setq old varlist)
1042 (mapc #'intset1 (cons *var dlist))
1043 (cond ((alike old varlist) (return (ratrep* exp)))
1044 (t (go a)))))
1046 (defun leadop (exp)
1047 (cond ((atom exp) exp)
1048 ((mqapplyp exp) (cadr exp))
1049 (t (caar 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)))
1055 (t (cadr exp))))
1057 (defun intset1 (b)
1058 (let (e c d)
1059 (fnewvar
1060 (setq d (if (mexptp b) ;needed for radicals
1061 `((mtimes simp)
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))