Fix bug #3996: parse_string fails to parse string which contains semicolon
[maxima.git] / src / sin.lisp
blob33ca59dc69164c1066896b842e1db6a176e067ea
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 sin)
15 ;;; Reference: J. Moses, Symbolic Integration, MIT-LCS-TR-047, 12-1-1967.
16 ;;; http://www.lcs.mit.edu/publications/pubs/pdf/MIT-LCS-TR-047.pdf.
17 ;;;;
18 ;;;; Unfortunately, some important pages in the scan are all black.
19 ;;;;
20 ;;;; A version with the missing pages is available (2008-12-14) from
21 ;;;; http://www.softwarepreservation.org/projects/LISP/MIT
23 (declare-top (special $radexpand $%e_to_numlog ans arcpart coef
24 aa powerlist *a* *b* k stack e w y expres arg var
25 *powerl* *c* *d* exp varlist genvar $liflag $opsubst))
27 (defvar *debug-integrate* nil
28 "Enable debugging for the integrator routines.")
30 ;; When T do not call the risch integrator. This flag can be set by the risch
31 ;; integrator to avoid endless loops when calling the integrator from risch.
32 (defvar *in-risch-p* nil)
34 (defmacro op (frob)
35 `(get ,frob 'operators))
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;;; Predicate functions
41 (declaim (inline varp))
42 (defun varp (x)
43 (alike1 x var))
45 (defun integerp1 (x)
46 "Returns 2*x if 2*x is an integer, else nil"
47 (integerp2 (mul2* 2 x)))
49 (defun integerp2 (x)
50 "Returns x if x is an integer, else false"
51 (let (u)
52 (cond ((not (numberp x)) nil)
53 ((not (floatp x)) x)
54 ((prog2 (setq u (maxima-rationalize x))
55 (equal (cdr u) 1)) (car u)))))
57 ;; This predicate is used with m2 pattern matcher.
58 ;; A rational expression in var.
59 (defun rat8 (ex)
60 (cond ((or (varp ex) (freevar ex))
62 ((member (caar ex) '(mplus mtimes) :test #'eq)
63 (do ((u (cdr ex) (cdr u)))
64 ((null u) t)
65 (if (not (rat8 (car u)))
66 (return nil))))
67 ((not (eq (caar ex) 'mexpt))
68 nil)
69 ((integerp (caddr ex))
70 (rat8 (cadr ex)))))
72 (defun rat8prime (c)
73 (and (rat8 c)
74 (or (not (mnump c))
75 (not (zerop1 c)))))
77 (defun elem (a)
78 (cond ((freevar a) t)
79 ((atom a) nil)
80 ((m2 a expres) t)
81 (t (every #'elem (cdr a)))))
83 (defun freevar (a)
84 (cond ((atom a) (not (eq a var)))
85 ((varp a) nil)
86 ((and (not (atom (car a)))
87 (member 'array (cdar a) :test #'eq))
88 (cond ((freevar (cdr a)) t)
89 (t (merror "~&FREEVAR: variable of integration appeared in subscript."))))
90 (t (and (freevar (car a)) (freevar (cdr a))))))
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
94 ;; possibly a bug: For var = x and *d* =3, we have expand(?subst10(x^9 * (x+x^6))) --> x^5+x^4, but
95 ;; ?subst10(expand(x^9 * (x+x^6))) --> x^5+x^3. (Barton Willis)
97 (defun subst10 (ex)
98 (cond ((atom ex) ex)
99 ((and (eq (caar ex) 'mexpt) (eq (cadr ex) var))
100 (list '(mexpt) var (integerp2 (quotient (caddr ex) *d*))))
101 (t (cons (remove 'simp (car ex))
102 (mapcar #'(lambda (c) (subst10 c)) (cdr ex))))))
104 (defun rationalizer (x)
105 (let ((ex (simplify ($factor x))))
106 (if (not (alike1 ex x)) ex)))
108 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;;; Stage II of the Integrator
112 ;;; Check if the problem can be transformed or solved by special methods.
113 ;;; 11 Methods are implemented by Moses, some more have been added.
115 (defun intform (expres &aux w)
116 (cond ((freevar expres) nil)
117 ((atom expres) nil)
119 ;; Map the function intform over the arguments of a sum or a product
120 ((member (caar expres) '(mplus mtimes) :test #'eq)
121 (some #'intform (cdr expres)))
123 ((or (eq (caar expres) '%log)
124 (arcp (caar expres)))
125 (cond
126 ;; Method 9: Rational function times a log or arctric function
127 ((setq arg (m2 exp
128 `((mtimes) ((,(caar expres)) (b rat8))
129 ((coefftt) (c rat8prime)))))
130 ;; Integrand is of the form R(x)*F(S(x)) where F is a log, or
131 ;; arctric function and R(x) and S(x) are rational functions.
132 (ratlog exp var (cons (cons 'a expres) arg)))
134 (prog (y z)
135 (cond
136 ((setq y (intform (cadr expres))) (return y))
138 ;; Method 10: Rational function times log(b*x+a)
139 ((and (eq (caar expres) '%log)
140 (setq z (m2-b*x+a (cadr expres)))
141 (setq y (m2 exp
142 '((mtimes)
143 ((coefftt) (c rat8))
144 ((coefftt) (d elem))))))
145 (return
146 (let ((a (cdr (assoc 'a z :test #'eq)))
147 (b (cdr (assoc 'b z :test #'eq)))
148 (c (cdr (assoc 'c y :test #'eq)))
149 (d (cdr (assoc 'd y :test #'eq)))
150 (newvar (gensym "intform")))
151 ;; keep var from appearing in questions to user
152 (putprop newvar t 'internal)
153 ;; Substitute y = log(b*x+a) and integrate again
154 (substint
155 expres
156 newvar
157 (integrator
158 (muln
159 (list (maxima-substitute
160 `((mquotient) ((mplus) ((mexpt) $%e ,newvar)
161 ((mtimes) -1 ,a))
165 `((mquotient) ((mexpt) $%e ,newvar) ,b)
166 (maxima-substitute newvar expres d))
167 nil)
168 newvar)))))
169 (t (return nil)))))))
171 ;; We have a special function with an integral on the property list.
172 ;; After the integral property was defined for the trig functions,
173 ;; in rev 1.52, need to exclude trig functions here.
174 ((and (not (atom (car expres)))
175 (not (optrig (caar expres)))
176 (not (eq (caar expres) 'mexpt))
177 (get (caar expres) 'integral))
178 (when *debug-integrate*
179 (format t "~&INTFORM: found 'INTEGRAL on property list~%"))
180 (cond
181 ((setq arg
182 (m2 exp `((mtimes) ((,(caar expres)) (b rat8)) ((coefftt) (c rat8prime)))))
183 ;; A rational function times the special function.
184 ;; Integrate with the method integration-by-parts.
185 (partial-integration (cons (cons 'a expres) arg) var))
186 ;; The method of integration-by-parts can not be applied.
187 ;; Maxima tries to get a clue for the argument of the function which
188 ;; allows a substitution for the argument.
189 ((intform (cadr expres)))
190 (t nil)))
192 ;; Method 6: Elementary function of trigonometric functions
193 ((optrig (caar expres))
194 (cond ((not (setq w (m2-b*x+a (cadr expres))))
195 (intform (cadr expres)))
197 (prog2
198 (setq *powerl* t)
199 (monstertrig exp var (cadr expres))))))
201 ((and (eq (caar expres) '%derivative)
202 (eq (caar exp) (caar expres))
203 (checkderiv exp)))
205 ;; Stop intform if we have not a power function.
206 ((not (eq (caar expres) 'mexpt)) nil)
208 ;; Method 2: Substitution for an integral power
209 ((integerp (caddr expres)) (intform (cadr expres)))
211 ;; Method 1: Elementary function of exponentials
212 ((freevar (cadr expres))
213 (cond ((setq w (m2-b*x+a (caddr expres)))
214 (superexpt exp var (cadr expres) w))
215 ((intform (caddr expres)))
216 ((and (eq '$%e (cadr expres))
217 (isinop (caddr expres) '%log))
218 ;; Found something like exp(r*log(x))
219 (let* (($%e_to_numlog t)
220 ($radexpand nil) ; do not simplify sqrt(x^2) -> abs(x)
221 (nexp (resimplify exp)))
222 (cond ((alike1 exp nexp) nil)
223 (t (integrator (setq exp nexp) var)))))
224 (t nil)))
226 ;; The base is not a rational function. Try to get a clue for the base.
227 ((not (rat8 (cadr expres)))
228 (intform (cadr expres)))
230 ;; Method 3: Substitution for a rational root
231 ((and (setq w (m2-ratrootform (cadr expres))) ; e*(a*x+b) / (c*x+d)
232 (denomfind (caddr expres))) ; expon is ratnum
233 (or (progn
234 (setq *powerl* t)
235 (ratroot exp var (cadr expres) w))
236 (inte exp var)))
238 ;; Method 4: Binomial - Chebyschev
239 ((not (integerp1 (caddr expres))) ; 2*exponent not integer
240 (cond ((m2-chebyform exp)
241 (chebyf exp var))
242 (t (intform (cadr expres)))))
244 ;; Method 5: Arctrigonometric substitution
245 ((setq w (m2-c*x^2+b*x+a (cadr expres))) ; sqrt(c*x^2+b*x+a)
246 #+nil
247 (format t "expres = sqrt(c*x^2+b*x+a)~%")
248 ;; I think this is method 5, arctrigonometric substitutions.
249 ;; (Moses, pg 80.) The integrand is of the form
250 ;; R(x,sqrt(c*x^2+b*x+a)). This method first eliminates the b
251 ;; term of the quadratic, and then uses an arctrig substitution.
252 (inte exp var))
254 ;; Method 4: Binomial - Chebyschev
255 ((m2-chebyform exp )
256 (chebyf exp var))
258 ;; Expand expres.
259 ;; Substitute the expanded factor into the integrand and try again.
260 ((not (m2 (setq w ($expand (cadr expres)))
261 (cadr expres)))
262 (prog2
263 (setq exp (maxima-substitute w (cadr expres) exp))
264 (intform (simplify (list '(mexpt) w (caddr expres))))))
266 ;; Factor expres.
267 ;; Substitute the factored factor into the integrand and try again.
268 ((setq w (rationalizer (cadr expres)))
269 ;; The forms below used to have $radexpand set to $all. But I
270 ;; don't think we really want to do that here because that makes
271 ;; sqrt(x^2) become x, which might be totally wrong. This is one
272 ;; reason why we returned -4/3 for the
273 ;; integrate(sqrt(x+1/x-2),x,0,1). We were replacing
274 ;; sqrt((x-1)^2) with x - 1, which is totally wrong since 0 <= x
275 ;; <= 1.
276 (setq exp (let (($radexpand $radexpand))
277 (maxima-substitute w (cadr expres) exp)))
278 (intform (let (($radexpand '$all))
279 (simplify (list '(mexpt) w (caddr expres))))))))
281 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
283 (defun separc (ex)
284 (cond ((arcfuncp ex) (setq arcpart ex coef 1))
285 ((and (consp ex) (eq (caar ex) 'mtimes))
286 (arclist (cdr ex))
287 (setq coef (cond ((null (cdr coef)) (car coef))
288 (t (setq coef (cons (car ex) coef))))))))
290 (defun arclist (list)
291 (cond ((null list) nil)
292 ((and (arcfuncp (car list)) (null arcpart))
293 (setq arcpart (car list)) (arclist (cdr list)))
294 (t (setq coef (cons (car list) coef))
295 (arclist (cdr list)))))
297 (defun arcfuncp (ex)
298 (and (not (atom ex))
299 (or (arcp (caar ex))
300 (eq (caar ex) '%log) ; Experimentally treat logs also.
301 (and (eq (caar ex) 'mexpt)
302 (integerp2 (caddr ex))
303 (> (integerp2 (caddr ex)) 0)
304 (arcfuncp (cadr ex))))))
306 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
308 ;;; Five pattern for the Integrator and other routines.
310 ;; This is matching the pattern e*(a*x+b)/(c*x+d), where
311 ;; a, b, c, d, and e are free of x, and x is the variable of integration.
312 (defun m2-ratrootform (expr)
313 (m2 expr
314 `((mtimes)
315 ((coefftt) (e freevar))
316 ((mplus)
317 ((coeffpt) (a freevar) (var varp))
318 ((coeffpt) (b freevar)))
319 ((mexpt)
320 ((mplus)
321 ((coeffpt) (c freevar) (var varp))
322 ((coeffpt) (d freevar)))
323 -1))))
325 ;; This is for matching the pattern a*x^r1*(c1+c2*x^q)^r2.
326 (defun m2-chebyform (expr)
327 (m2 expr
328 `((mtimes)
329 ((mexpt) (var varp) (r1 numberp))
330 ((mexpt)
331 ((mplus)
332 ((mtimes)
333 ((coefftt) (c2 freevar))
334 ((mexpt) (var varp) (q free1)))
335 ((coeffpp) (c1 freevar)))
336 (r2 numberp))
337 ((coefftt) (a freevar)))))
339 ;; Pattern to match b*x + a
340 (defun m2-b*x+a (expr)
341 (m2 expr
342 `((mplus)
343 ((coeffpt) (b freevar) (x varp))
344 ((coeffpt) (a freevar)))))
346 ;; This is the pattern c*x^2 + b * x + a.
347 (defun m2-c*x^2+b*x+a (expr)
348 (m2 expr
349 `((mplus)
350 ((coeffpt) (c freevar) ((mexpt) (x varp) 2))
351 ((coeffpt) (b freevar) (x varp))
352 ((coeffpt) (a freevar)))))
354 ;; This is the pattern (a*x+b)*(c*x+d)
355 (defun m2-a*x+b/c*x+d (expr)
356 (m2 expr
357 `((mtimes)
358 ((mplus)
359 ((coeffpt) (a freevar) (var varp))
360 ((coeffpt) (b freevar)))
361 ((mplus)
362 ((coeffpt) (c freevar) (var varp))
363 ((coeffpt) (d freevar))))))
365 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
367 ;;; This is the main integration routine. It is called from sinint.
369 (defun integrator (exp var)
370 (prog (y arg *powerl* const *b* w arcpart coef integrand result)
371 (declare (special *integrator-level*))
372 ;; Increment recursion counter
373 (incf *integrator-level*)
375 ;; Trivial case. exp is not a function of var.
376 (if (freevar exp) (return (mul2* exp var)))
378 ;; Remove constant factors
379 (setq w (partition exp var 1))
380 (setq const (car w))
381 (setq exp (cdr w))
382 #+nil
383 (progn
384 (format t "w = ~A~%" w)
385 (format t "const = ~A~%" const)
386 (format t "exp = ~A~%" exp))
388 (cond ;; First stage, Method I: Integrate a sum.
389 ((mplusp exp)
390 (return (mul2* const (integrate1 (cdr exp)))))
392 ;; Convert atan2(a,b) to atan(a/b) and try again.
393 ((setq w (isinop exp '$atan2))
394 (setq exp
395 (maxima-substitute (take '(%atan) (div (cadr w) (caddr w)))
397 exp))
398 (return (mul* const
399 (integrator exp var))))
401 ;; First stage, Method II: Integrate sums.
402 ((and (not (atom exp))
403 (eq (caar exp) '%sum))
404 (return (mul2* const (intsum exp var))))
406 ;; First stage, Method III: Try derivative-divides method.
407 ;; This is the workhorse that solves many integrals.
408 ((setq y (diffdiv exp var))
409 (return (mul2* const y))))
411 ;; At this point, we have EXP as a product of terms. Make Y a
412 ;; list of the terms of the product.
413 (setq y (cond ((mtimesp exp)
414 (cdr exp))
416 (list exp))))
418 ;; Second stage:
419 ;; We're looking at each term of the product and check if we can
420 ;; apply one of the special methods.
421 loop
422 #+nil
423 (progn
424 (format t "car y =~%")
425 (maxima-display (car y)))
426 (cond ((rat8 (car y))
427 #+nil
428 (format t "In loop, go skip~%")
429 (go skip))
430 ((and (setq w (intform (car y)))
431 ;; Do not return a noun form as result at this point, because
432 ;; we would like to check for further special integrals.
433 ;; We store the result for later use.
434 (setq result w)
435 (not (isinop w '%integrate)))
436 #+nil
437 (format t "In loop, case intform~%")
438 (return (mul2* const w)))
440 #+nil
441 (format t "In loop, go special~%")
442 ;; Store a possible partial result
443 (setq result w)
444 (go special)))
445 skip
446 (setq y (cdr y))
447 (cond ((null y)
448 ;; Method 8: Rational functions
449 (return (mul2* const (cond ((setq y (powerlist exp var)) y)
450 (t (ratint exp var)))))))
451 (go loop)
453 special
454 ;; Third stage: Try more general methods
456 ;; SEPARC SETQS ARCPART AND COEF SUCH THAT
457 ;; COEF*ARCEXP=EXP WHERE ARCEXP IS OF THE FORM
458 ;; ARCFUNC^N AND COEF IS ITS ALGEBRAIC COEFFICIENT
459 (separc exp)
461 #+nil
462 (progn
463 (format t "arcpart = ~A~%" arcpart)
464 (format t "coef =~%")
465 (maxima-display coef))
466 (cond ((and (not (null arcpart))
467 (do ((stacklist stack (cdr stacklist)))
468 ((null stacklist) t)
469 (cond ((alike1 (car stacklist) coef)
470 (return nil))))
471 (not (isinop (setq w (let ((stack (cons coef stack)))
472 (integrator coef var)))
473 '%integrate))
474 (setq integrand (mul2 w (sdiff arcpart var)))
475 (do ((stacklist stack (cdr stacklist)))
476 ((null stacklist) t)
477 (cond ((alike1 (car stacklist) integrand)
478 (return nil))))
479 (not (isinop
480 (setq y (let ((stack (cons integrand stack))
481 (integ integrand))
482 (integrator integ var)))
483 '%integrate)))
484 (return (add* (list '(mtimes) const w arcpart)
485 (list '(mtimes) -1 const y))))
487 (return
488 (mul* const
489 (cond ((setq y (scep exp var))
490 (cond ((cddr y)
491 #+nil
492 (progn
493 (format t "cddr y =~%")
494 (maxima-display (cddr y)))
495 (integrator ($trigreduce exp) var))
496 (t (sce-int (car y) (cadr y) var))))
497 ;; I don't understand why we do this. This
498 ;; causes the stack overflow in Bug
499 ;; 1487703, because we keep expanding exp
500 ;; into a form that matches the original
501 ;; and therefore we loop forever. To
502 ;; break this we keep track how how many
503 ;; times we've tried this and give up
504 ;; after 4 (arbitrarily selected) times.
505 ((and (< *integrator-level* 4)
506 (not (alike1 exp (setq y ($expand exp)))))
507 #+nil
508 (progn
509 (format t "exp = ~A~%" exp)
510 (maxima-display exp)
511 (format t "y = ~A~%" y)
512 (maxima-display y)
513 (break))
514 (integrator y var))
515 ((and (not *powerl*)
516 (setq y (powerlist exp var)))
518 ((and (not *in-risch-p*) ; Not called from rischint
519 (setq y (rischint exp var))
520 ;; rischint has not found an integral but
521 ;; returns a noun form. Do not return that
522 ;; noun form as result at this point, but
523 ;; store it for later use.
524 (setq result y)
525 (not (isinop y '%integrate)))
527 ((setq y (integrate-exp-special exp var))
528 ;; Maxima found an integral for a power function
531 ;; Integrate-exp-special has not found an integral
532 ;; We look for a previous result obtained by
533 ;; intform or rischint.
534 (if result
535 result
536 (list '(%integrate) exp var))))))))))
538 (defun optrig (x)
539 (member x '(%sin %cos %sec %tan %csc %cot) :test #'eq))
541 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
543 ;;; Stage I
544 ;;; Implementation of Method 1: Integrate a sum
546 ;;after finding a non-integrable summand usually better to pass rest to risch
547 (defun integrate1 (exp)
548 (do ((terms exp (cdr terms)) (ans))
549 ((null terms) (addn ans nil))
550 (let ($liflag) ; don't gen li's for
551 (push (integrator (car terms) var) ans)) ; parts of integrand
552 (when (and (not *in-risch-p*) ; Not called from rischint
553 (not (free (car ans) '%integrate))
554 (cdr terms))
555 (return (addn (cons (rischint (cons '(mplus) terms) var) (cdr ans))
556 nil)))))
558 (defun scep (expr var &aux trigl exp) ; Product of SIN, COS, EXP
559 (and (mtimesp expr) ; of linear args.
560 (loop for fac in (cdr expr) do
561 (cond ((atom fac) (return nil))
562 ((trig1 (car fac))
563 (if (linearp (cadr fac) var) (push fac trigl)
564 (return nil)))
565 ((and (mexptp fac)
566 (eq (cadr fac) '$%e)
567 (linearp (caddr fac) var))
568 ;; should be only one exponential factor
569 (setq exp fac))
570 (t (return nil)))
571 finally (return (cons exp trigl)))))
573 ;; Integrates exponential * sin or cos, all with linear args.
575 (defun sce-int (exp s-c var) ; EXP is non-trivial
576 (let* ((e-coef (car (islinear (caddr exp) var)))
577 (sc-coef (car (islinear (cadr s-c) var)))
578 (sc-arg (cadr s-c))
579 (abs-val (add (power e-coef 2) (power sc-coef 2))))
580 (if (zerop1 abs-val)
581 ;; The numerator is zero. Exponentialize the integrand and try again.
582 (integrator ($exponentialize (mul exp s-c)) var)
583 (mul (div exp abs-val)
584 (add (mul e-coef s-c)
585 (if (eq (caar s-c) '%sin)
586 (mul* (neg sc-coef) `((%cos) ,sc-arg))
587 (mul* sc-coef `((%sin) ,sc-arg))))))))
589 (defun checkderiv (expr)
590 (checkderiv1 (cadr expr) (cddr expr) () ))
592 ;; CHECKDERIV1 gets called on the expression being differentiated,
593 ;; an alternating list of variables being differentiated with
594 ;; respect to and powers thereof, and a reversed list of the latter
595 ;; that have already been examined. It returns either the antiderivative
596 ;; or (), saying this derivative isn't wrt the variable of integration.
598 (defun checkderiv1 (expr wrt old-wrt)
599 (cond ((varp (car wrt))
600 (if (equal (cadr wrt) 1) ;Power = 1?
601 (if (null (cddr wrt)) ;single or partial
602 (if (null old-wrt)
603 expr ;single
604 `((%derivative), expr ;partial in old-wrt
605 ,.(nreverse old-wrt)))
606 `((%derivative) ,expr ;Partial, return rest
607 ,.(nreverse old-wrt)
608 ,@(cddr wrt)))
609 `((%derivative) ,expr ;Higher order, reduce order
610 ,.(nreverse old-wrt)
611 ,(car wrt) ,(add* (cadr wrt) -1)
612 ,@ (cddr wrt))))
613 ((null (cddr wrt)) () ) ;Say it doesn't apply here
614 (t (checkderiv1 expr (cddr wrt) ;Else we check later terms
615 (list* (cadr wrt) (car wrt) old-wrt)))))
617 (defun integrallookups (exp)
618 (let (form dummy-args real-args)
619 (cond
620 ((eq (caar exp) 'mqapply)
621 ;; Transform to functional form and try again.
622 ;; For example:
623 ;; ((MQAPPLY SIMP) (($PSI SIMP ARRAY) 1) $X)
624 ;; => (($PSI) 1 $X)
625 (integrallookups `((,(caaadr exp)) ,@(cdadr exp) ,@(cddr exp))))
627 ;; Lookup algorithm for integral of a special function.
628 ;; The integral form is put on the property list, and can be a
629 ;; lisp function of the args. If the form is nil, or evaluates
630 ;; to nil, then return noun form unevaluated.
631 ((and (not (atom (car exp)))
632 (setq form (get (caar exp) 'integral))
633 (setq dummy-args (car form))
634 (setq real-args (cdr exp))
635 ;; search through the args of exp and find the arg containing var
636 ;; look up the integral wrt this arg from form
637 (setq form
638 (do ((x real-args (cdr x))
639 (y (cdr form) (cdr y)))
640 ((or (null x) (null y)) nil)
641 (if (not (freevar (car x))) (return (car y)))))
642 ;; If form is a function then evaluate it with actual args
643 (or (not (functionp form))
644 (setq form (apply form real-args))))
645 (when *debug-integrate*
646 (format t "~&INTEGRALLOOKUPS: Found integral ~A.~%" (caar exp)))
647 (substitutel real-args dummy-args form))
649 ((eq (caar exp) 'mplus)
650 (muln (list '((rat simp) 1 2) exp exp) nil))
652 (t nil))))
654 ;; Define the antiderivatives of some elementary special functions.
655 ;; This may not be the best place for this definition, but it is close
656 ;; to the original code.
657 ;; Antiderivatives that depend on the logabs flag are defined further below.
658 (defprop %log ((x) ((mplus) ((mtimes) x ((%log) x)) ((mtimes) -1 x))) integral)
659 (defprop %sin ((x) ((mtimes) -1 ((%cos) x))) integral)
660 (defprop %cos ((x) ((%sin) x)) integral)
661 (defprop %sinh ((x) ((%cosh) x)) integral)
662 (defprop %cosh ((x) ((%sinh) x)) integral)
663 ;; No need to take logabs into account for tanh(x), because cosh(x) is positive.
664 (defprop %tanh ((x) ((%log) ((%cosh) x))) integral)
665 (defprop %sech ((x) ((%atan) ((%sinh) x))) integral)
666 (defprop %asin ((x) ((mplus) ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2))) ((rat) 1 2)) ((mtimes) x ((%asin) x)))) integral)
667 (defprop %acos ((x) ((mplus) ((mtimes) -1 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2))) ((rat) 1 2))) ((mtimes) x ((%acos) x)))) integral)
668 (defprop %atan ((x) ((mplus) ((mtimes) x ((%atan) x)) ((mtimes) ((rat) -1 2) ((%log) ((mplus) 1 ((mexpt) x 2)))))) integral)
669 (defprop %acsc ((x) ((mplus) ((mtimes) ((rat) -1 2) ((%log) ((mplus) 1 ((mtimes) -1 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x -2))) ((rat) 1 2)))))) ((mtimes) ((rat) 1 2) ((%log) ((mplus) 1 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x -2))) ((rat) 1 2))))) ((mtimes) x ((%acsc) x)))) integral)
670 (defprop %asec ((x) ((mplus) ((mtimes) ((rat) 1 2) ((%log) ((mplus) 1 ((mtimes) -1 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x -2))) ((rat) 1 2)))))) ((mtimes) ((rat) -1 2) ((%log) ((mplus) 1 ((mexpt) ((mplus) 1 ((mtimes) -1 ((mexpt) x -2))) ((rat) 1 2))))) ((mtimes) x ((%asec) x)))) integral)
671 (defprop %acot ((x) ((mplus) ((mtimes) x ((%acot) x)) ((mtimes) ((rat) 1 2) ((%log) ((mplus) 1 ((mexpt) x 2)))))) integral)
672 (defprop %asinh ((x) ((mplus) ((mtimes) -1 ((mexpt) ((mplus) 1 ((mexpt) x 2)) ((rat) 1 2))) ((mtimes) x ((%asinh) x)))) integral)
673 (defprop %acosh ((x) ((mplus) ((mtimes) -1 ((mexpt) ((mplus) -1 ((mexpt) x 2)) ((rat) 1 2))) ((mtimes) x ((%acosh) x)))) integral)
674 (defprop %atanh ((x) ((mplus) ((mtimes) x ((%atanh) x)) ((mtimes) ((rat) 1 2) ((%log) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2))))))) integral)
675 (defprop %acsch ((x) ((mplus) ((mtimes) ((rat) -1 2) ((%log) ((mplus) -1 ((mexpt) ((mplus) 1 ((mexpt) x -2)) ((rat) 1 2))))) ((mtimes) ((rat) 1 2) ((%log) ((mplus) 1 ((mexpt) ((mplus) 1 ((mexpt) x -2)) ((rat) 1 2))))) ((mtimes) x ((%acsch) x)))) integral)
676 (defprop %asech ((x) ((mplus) ((mtimes) -1 ((%atan) ((mexpt) ((mplus) -1 ((mexpt) x -2)) ((rat) 1 2)))) ((mtimes) x ((%asech) x)))) integral)
677 (defprop %acoth ((x) ((mplus) ((mtimes) x ((%acoth) x)) ((mtimes) ((rat) 1 2) ((%log) ((mplus) 1 ((mtimes) -1 ((mexpt) x 2))))))) integral)
679 ;; Define a little helper function to be used in antiderivatives.
680 ;; Depending on the logabs flag, it either returns log(x) or log(abs(x)).
681 (defun log-or-logabs (x)
682 (take '(%log) (if $logabs (take '(mabs) x) x)))
684 ;; Define the antiderivative of tan(x), taking logabs into account.
685 (defun integrate-tan (x)
686 (log-or-logabs (take '(%sec) x)))
687 (putprop '%tan `((x) ,#'integrate-tan) 'integral)
689 ;; ... the same for csc(x) ...
690 (defun integrate-csc (x)
691 (mul -1 (log-or-logabs (add (take '(%csc) x) (take '(%cot) x)))))
692 (putprop '%csc `((x) ,#'integrate-csc) 'integral)
694 ;; ... the same for sec(x) ...
695 (defun integrate-sec (x)
696 (log-or-logabs (add (take '(%sec) x) (take '(%tan) x))))
697 (putprop '%sec `((x) ,#'integrate-sec) 'integral)
699 ;; ... the same for cot(x) ...
700 (defun integrate-cot (x)
701 (log-or-logabs (take '(%sin) x)))
702 (putprop '%cot `((x) ,#'integrate-cot) 'integral)
704 ;; ... the same for coth(x) ...
705 (defun integrate-coth (x)
706 (log-or-logabs (take '(%sinh) x)))
707 (putprop '%coth `((x) ,#'integrate-coth) 'integral)
709 ;; ... the same for csch(x) ...
710 (defun integrate-csch (x)
711 (log-or-logabs (take '(%tanh) (mul '((rat simp) 1 2) x))))
712 (putprop '%csch `((x) ,#'integrate-csch) 'integral)
714 ;; integrate(x^n,x) = if n # -1 then x^(n+1)/(n+1) else log-or-logabs(x).
715 (defun integrate-mexpt-1 (x n)
716 (let ((n-is-minus-one ($askequal n -1)))
717 (cond ((eq '$yes n-is-minus-one)
718 (log-or-logabs x))
720 (setq n (add n 1))
721 (div (take '(mexpt) x n) n)))))
723 ;; integrate(a^x,x) = a^x/log(a).
724 (defun integrate-mexpt-2 (a x)
725 (div (take '(mexpt) a x) (take '(%log) a)))
727 (putprop 'mexpt `((x n) ,#'integrate-mexpt-1 ,#'integrate-mexpt-2) 'integral)
729 (defun rat10 (ex)
730 (cond ((freevar ex) t)
731 ((varp ex) nil)
732 ((eq (caar ex) 'mexpt)
733 (if (varp (cadr ex))
734 (if (integerp2 (caddr ex))
735 (setq powerlist (cons (caddr ex) powerlist)))
736 (and (rat10 (cadr ex)) (rat10 (caddr ex)))))
737 ((member (caar ex) '(mplus mtimes) :test #'eq)
738 (do ((u (cdr ex) (cdr u))) ((null u) t)
739 (if (not (rat10 (car u))) (return nil))))))
741 (defun integrate5 (ex var)
742 (if (rat8 ex)
743 (ratint ex var)
744 (integrator ex var)))
746 (defun denomfind (x)
747 (cond ((ratnump x) (caddr x))
748 ((not (numberp x)) nil)
749 ((not (floatp x)) 1)
750 (t (cdr (maxima-rationalize x)))))
752 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
754 ;;; Stage II
755 ;;; Implementation of Method 1: Elementary function of exponentials
757 ;;; The following examples are integrated with this method:
759 ;;; integrate(exp(x)/(2+3*exp(2*x)),x)
760 ;;; integrate(exp(x+1)/(1+exp(x)),x)
761 ;;; integrate(10^x*exp(x),x)
763 (let ((base nil) ; The common base.
764 (pow nil) ; The common power of the form b*x+a. The values are
765 ; stored in a list which is returned from m2.
766 (exptflag nil)) ; When T, the substitution is not possible.
768 (defun superexpt (exp var bas1 pow1)
769 (prog (y ($logabs nil) (new-var (gensym "NEW-VAR-")))
770 (putprop new-var t 'internal)
771 (setq base bas1
772 pow pow1
773 exptflag nil)
774 ;; Transform the integrand. At this point resimplify, because it is not
775 ;; guaranteed, that a correct simplified expression is returned.
776 ;; Use a new variable to prevent facts on the old variable to be wrongly used.
777 (setq y (resimplify (maxima-substitute new-var var (elemxpt exp))))
778 (when exptflag (return nil))
779 ;; Integrate the transformed integrand and substitute back.
780 (return
781 ($multthru
782 (substint (list '(mexpt) base
783 (list '(mplus) (cdras 'a pow)
784 (list '(mtimes) (cdras 'b pow) var)))
785 new-var
786 (integrator (div y
787 (mul new-var
788 (cdras 'b pow)
789 (take '(%log) base))) new-var))))))
791 ;; Transform expressions like g^(b*x+a) to the common base base and
792 ;; do the substitution y = base^(b*x+a) in the expr.
793 (defun elemxpt (expr &aux w)
794 (cond ((freevar expr) expr)
795 ;; var is the base of a subexpression. The transformation fails.
796 ((atom expr) (setq exptflag t))
797 ((not (eq (caar expr) 'mexpt))
798 (cons (car expr)
799 (mapcar #'(lambda (c) (elemxpt c)) (cdr expr))))
800 ((not (freevar (cadr expr)))
801 (list '(mexpt)
802 (elemxpt (cadr expr))
803 (elemxpt (caddr expr))))
804 ;; Transform the expression to the common base.
805 ((not (eq (cadr expr) base))
806 (elemxpt (list '(mexpt)
807 base
808 (mul (power (take '(%log) base) -1)
809 (take '(%log) (cadr expr))
810 (caddr expr)))))
811 ;; The exponent must be linear in the variable of integration.
812 ((not (setq w (m2-b*x+a (caddr expr))))
813 (list (car expr) base (elemxpt (caddr expr))))
814 ;; Do the substitution y = g^(b*x+a).
816 (setq w (cons (cons 'bb (cdras 'b pow)) w))
817 (setq w (cons (cons 'aa (cdras 'a pow)) w))
818 (setq w (cons (cons 'base base) w))
819 (subliss w '((mtimes)
820 ((mexpt) base a)
821 ((mexpt)
822 base
823 ((mquotient)
824 ((mtimes) -1 aa b) bb))
825 ((mexpt)
827 ((mquotient) b bb)))))))
828 ) ; End of let
830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
832 ;;; Stage II
833 ;;; Implementation of Method 3:
834 ;;; Substitution for a rational root of a linear fraction of x
836 ;;; This method is applicable when the integrand is of the form:
838 ;;; /
839 ;;; [ a x + b n1/m1 a x + b n1/m2
840 ;;; I R(x, (-------) , (-------) , ...) dx
841 ;;; ] c x + d c x + d
842 ;;; /
844 ;;; Substitute
846 ;;; (1) t = ((a*x+b)/(c*x+d))^(1/k), or
848 ;;; (2) x = (b-d*t^k)/(c*t^k-a)
850 ;;; where k is the least common multiplier of m1, m2, ... and
852 ;;; (3) dx = k*(a*d-b*c)*t^(k-1)/(a-c*t^k)^2 * dt
854 ;;; First, the algorithm calls the routine RAT3 to collect the roots of the
855 ;;; form ((a*x+b)/(c*x+d))^(n/m) in the list *ROOTLIST*.
856 ;;; search for the least common multiplier of m1, m2, ... then the
857 ;;; substitutions (2) and (3) are done and the new problem is integrated.
858 ;;; As always, W is an alist which associates to the coefficients
859 ;;; a, b... (and to VAR) their values.
861 (defvar *ratroot* nil) ; Expression of the form (a*x+b)/(c*x+d)
862 (defvar *rootlist* nil) ; List of powers of the expression *ratroot*.
864 (defun ratroot (exp var *ratroot* w)
865 (prog (*rootlist* k y w1)
866 ;; Check if the integrand has a chebyform, if so return the result.
867 (when (setq y (chebyf exp var)) (return y))
868 ;; Check if the integrand has a suitably form and collect the roots
869 ;; in the global special variable *ROOTLIST*.
870 (unless (rat3 exp t) (return nil))
871 ;; Get the least common multiplier of m1, m2, ...
872 (setq k (apply #'lcm *rootlist*))
873 (setq w1 (cons (cons 'k k) w))
874 ;; Substitute for the roots.
875 (setq y
876 (subst41 exp
877 (subliss w1
878 '((mquotient)
879 ((mplus) ((mtimes) b e)
880 ((mtimes) -1 d ((mexpt) var k)))
881 ((mplus) ((mtimes) c ((mexpt) var k))
882 ((mtimes) -1 e a))))
883 var))
884 ;; Integrate the new problem.
885 (setq y
886 (integrator
887 (mul y
888 (subliss w1
889 '((mquotient)
890 ((mtimes) e
891 ((mplus)
892 ((mtimes) a d k
893 ((mexpt) var ((mplus) -1 k)))
894 ((mtimes) -1
895 ((mtimes) b c k
896 ((mexpt) var ((mplus) -1 k))))))
897 ((mexpt) ((mplus)
898 ((mtimes) c ((mexpt) var k))
899 ((mtimes) -1 a e))
900 2))))
901 var))
902 ;; Substitute back and return the result.
903 (return (substint (power *ratroot* (power k -1)) var y))))
905 (defun rat3 (ex ind)
906 (cond ((freevar ex) t)
907 ((atom ex) ind)
908 ((member (caar ex) '(mtimes mplus) :test #'eq)
909 (do ((u (cdr ex) (cdr u)))
910 ((null u) t)
911 (if (not (rat3 (car u) ind))
912 (return nil))))
913 ((not (eq (caar ex) 'mexpt))
914 (rat3 (car (margs ex)) t))
915 ((freevar (cadr ex))
916 (rat3 (caddr ex) t))
917 ((integerp (caddr ex))
918 (rat3 (cadr ex) ind))
919 ((and (m2 (cadr ex) *ratroot*)
920 (denomfind (caddr ex)))
921 (setq *rootlist* (cons (denomfind (caddr ex)) *rootlist*)))
922 (t (rat3 (cadr ex) nil))))
924 (let ((rootform nil) ; Expression of the form x = (b*e-d*t^k)/(c*t^k-e*a).
925 (rootvar nil)) ; The variable we substitute for the root.
927 (defun subst4 (ex)
928 (cond ((freevar ex) ex)
929 ((atom ex) rootform)
930 ((not (eq (caar ex) 'mexpt))
931 (mapcar #'(lambda (u) (subst4 u)) ex))
932 ((m2 (cadr ex) *ratroot*)
933 (list (car ex) rootvar (integerp2 (timesk k (caddr ex)))))
934 (t (list (car ex) (subst4 (cadr ex)) (subst4 (caddr ex))))))
936 (defun subst41 (exp a b)
937 (setq rootform a
938 rootvar b)
939 ;; At this point resimplify, because it is not guaranteed, that a correct
940 ;; simplified expression is returned.
941 (resimplify (subst4 exp)))
942 ) ; End of let
944 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
946 ;;; Stage II
947 ;;; Implementation of Method 4: Binomial Chebyschev
949 ;; exp = a*t^r1*(c1+c2*t^q)^r2, where var = t.
951 ;; G&S 2.202 has says this integral can be expressed by elementary
952 ;; functions ii:
954 ;; 1. q is an integer
955 ;; 2. (r1+1)/q is an integer
956 ;; 3. (r1+1)/q+r2 is an integer.
958 ;; I (rtoy) think that for this code to work, r1, r2, and q must be numbers.
959 (defun chebyf (exp var)
960 (prog (r1 r2 d1 d2 n1 n2 w q)
961 ;; Return NIL if the expression doesn't match.
962 (when (not (setq w (m2-chebyform exp)))
963 (return nil))
964 #+nil
965 (format t "w = ~A~%" w)
966 (when (zerop1 (cdr (assoc 'c1 w :test #'eq)))
967 ;; rtoy: Is it really possible to be in this routine with c1 =
968 ;; 0?
969 (return
970 (mul*
971 ;; This factor is locally constant as long as t and
972 ;; c2*t^q avoid log's branch cut.
973 (subliss w '((mtimes) a ((mexpt) var ((mtimes) -1 q r2))
974 ((mexpt) ((mtimes) c2 ((mexpt) var q)) r2)))
975 (integrator
976 (subliss w '((mexpt) var ((mplus) r1 ((mtimes) q r2)))) var))))
977 (setq q (cdr (assoc 'q w :test #'eq)))
978 ;; Reset parameters. a = a/q, r1 = (1 - q + r1)/q
979 (setq w
980 (list* (cons 'a (div* (cdr (assoc 'a w :test #'eq)) q))
981 (cons
983 (div* (addn (list 1 (neg (simplify q)) (cdr (assoc 'r1 w :test #'eq))) nil) q))
985 #+nil
986 (format t "new w = ~A~%" w)
987 (setq r1 (cdr (assoc 'r1 w :test #'eq))
988 r2 (cdr (assoc 'r2 w :test #'eq)))
989 #+nil
990 (progn
991 (format t "new r1 = ~A~%" r1)
992 (format t "r2 = ~A~%" r2))
993 ;; Write r1 = d1/n1, r2 = d2/n2, if possible. Update w with
994 ;; these values, if so. If we can't, give up. I (rtoy) think
995 ;; this only happens if r1 or r2 can't be expressed as rational
996 ;; numbers. Hence, r1 and r2 have to be numbers, not variables.
997 (cond
998 ((not (and (setq d1 (denomfind r1))
999 (setq d2 (denomfind r2))
1000 (setq n1 (integerp2 (timesk r1 d1)))
1001 (setq n2 (integerp2 (timesk r2 d2)))
1002 (setq w (list* (cons 'd1 d1) (cons 'd2 d2)
1003 (cons 'n1 n1) (cons 'n2 n2)
1004 w))))
1005 #+nil
1006 (progn
1007 (format t "cheby can't find one of d1,d2,n1,n2:~%")
1008 (format t " d1 = ~A~%" d1)
1009 (format t " d2 = ~A~%" d2)
1010 (format t " n1 = ~A~%" n1)
1011 (format t " n2 = ~A~%" n2))
1012 (return nil))
1013 ((and (integerp2 r1) (> r1 0))
1014 #+nil (format t "integer r1 > 0~%")
1015 ;; (r1+q-1)/q is positive integer.
1017 ;; I (rtoy) think we are using the substitution z=(c1+c2*t^q).
1018 ;; Maxima thinks the resulting integral should then be
1020 ;; a/q*c2^(-r1/q-1/q)*integrate(z^r2*(z-c1)^(r1/q+1/q-1),z)
1022 (return
1023 (substint
1024 (subliss w '((mplus) c1 ((mtimes) c2 ((mexpt) var q))))
1026 (integrator
1027 (expands (list (subliss w
1028 ;; a*t^r2*c2^(-r1-1)
1029 '((mtimes)
1031 ((mexpt) var r2)
1032 ((mexpt)
1034 ((mtimes)
1036 ((mplus) r1 1))))))
1037 (cdr
1038 ;; (t-c1)^r1
1039 (expandexpt (subliss w
1040 '((mplus)
1042 ((mtimes) -1 c1)))
1043 r1)))
1044 var))))
1045 ((integerp2 r2)
1046 #+nil (format t "integer r2~%")
1047 ;; I (rtoy) think this is using the substitution z = t^(q/d1).
1049 ;; The integral (as maxima will tell us) becomes
1051 ;; a*d1/q*integrate(z^(n1/q+d1/q-1)*(c1+c2*z^d1)^r2,z)
1053 ;; But be careful because the variable A in the code is
1054 ;; actually a/q.
1055 (return
1056 (substint (subliss w '((mexpt) var ((mquotient) q d1)))
1058 (ratint (simplify (subliss w
1059 '((mtimes)
1060 d1 a
1061 ((mexpt)
1063 ((mplus)
1064 n1 d1 -1))
1065 ((mexpt)
1066 ((mplus)
1067 ((mtimes)
1069 ((mexpt)
1070 var d1))
1072 r2))))
1073 var))))
1074 ((and (integerp2 r1) (< r1 0))
1075 #+nil (format t "integer r1 < 0~%")
1076 ;; I (rtoy) think this is using the substitution
1078 ;; z = (c1+c2*t^q)^(1/d2)
1080 ;; With this substitution, maxima says the resulting integral
1081 ;; is
1083 ;; a/q*c2^(-r1/q-1/q)*d2*
1084 ;; integrate(z^(n2+d2-1)*(z^d2-c1)^(r1/q+1/q-1),z)
1085 (return
1086 (substint (subliss w
1087 ;; (c1+c2*t^q)^(1/d2)
1088 '((mexpt)
1089 ((mplus)
1091 ((mtimes) c2 ((mexpt) var q)))
1092 ((mquotient) 1 d2)))
1094 (ratint (simplify (subliss w
1095 ;; This is essentially
1096 ;; the integrand above,
1097 ;; except A and R1 here
1098 ;; are not the same as
1099 ;; derived above.
1100 '((mtimes)
1101 a d2
1102 ((mexpt)
1104 ((mtimes)
1106 ((mplus)
1107 r1 1)))
1108 ((mexpt)
1110 ((mplus)
1111 n2 d2 -1))
1112 ((mexpt)
1113 ((mplus)
1114 ((mexpt)
1115 var d2)
1116 ((mtimes) -1 c1))
1117 r1))))
1118 var))))
1119 ((integerp2 (add* r1 r2))
1120 #+nil (format t "integer r1+r2~%")
1121 ;; If we're here, (r1-q+1)/q+r2 is an integer.
1123 ;; I (rtoy) think this is using the substitution
1125 ;; z = ((c1+c2*t^q)/t^q)^(1/d1)
1127 ;; With this substitution, maxima says the resulting integral
1128 ;; is
1130 ;; a*d2/q*c1^(r2+r1/q+1/q)*
1131 ;; integrate(z^(d2*r2+d2-1)*(z^d2-c2)^(-r2-r1/q-1/q-1),z)
1132 (return
1133 (substint (let (($radexpand '$all))
1134 ;; Setting $radexpand to $all here gets rid of
1135 ;; ABS in the subtitution. I think that's ok in
1136 ;; this case. See Bug 1654183.
1137 (subliss w
1138 '((mexpt)
1139 ((mquotient)
1140 ((mplus)
1142 ((mtimes) c2 ((mexpt) var q)))
1143 ((mexpt) var q))
1144 ((mquotient) 1 d1))))
1146 (ratint (simplify (subliss w
1147 '((mtimes)
1148 -1 a d1
1149 ((mexpt)
1151 ((mplus)
1152 r1 r2 1))
1153 ((mexpt)
1155 ((mplus)
1156 n2 d1 -1))
1157 ((mexpt)
1158 ((mplus)
1159 ((mexpt)
1160 var d1)
1161 ((mtimes)
1162 -1 c2))
1163 ((mtimes)
1165 ((mplus)
1166 r1 r2
1167 2))))))
1168 var))))
1169 (t (return (list '(%integrate) exp var))))))
1171 (defun greaterratp (x1 x2)
1172 (cond ((and (numberp x1) (numberp x2))
1173 (> x1 x2))
1174 ((ratnump x1)
1175 (greaterratp (quotient (float (cadr x1))
1176 (caddr x1))
1177 x2))
1178 ((ratnump x2)
1179 (greaterratp x1
1180 (quotient (float (cadr x2))
1181 (caddr x2))))))
1183 (defun trig1 (x)
1184 (member (car x) '(%sin %cos) :test #'eq))
1186 (defun supertrig (exp)
1187 (declare (special *notsame* *trigarg*))
1188 (cond ((freevar exp) t)
1189 ((atom exp) nil)
1190 ((member (caar exp) '(mplus mtimes) :test #'eq)
1191 (and (supertrig (cadr exp))
1192 (or (null (cddr exp))
1193 (supertrig (cons (car exp)
1194 (cddr exp))))))
1195 ((eq (caar exp) 'mexpt)
1196 (and (supertrig (cadr exp))
1197 (supertrig (caddr exp))))
1198 ((eq (caar exp) '%log)
1199 (supertrig (cadr exp)))
1200 ((member (caar exp)
1201 '(%sin %cos %tan %sec %cot %csc) :test #'eq)
1202 (cond ((m2 (cadr exp) *trigarg*) t)
1203 ((m2-b*x+a (cadr exp))
1204 (and (setq *notsame* t) nil))
1205 (t (supertrig (cadr exp)))))
1206 (t (supertrig (cadr exp)))))
1208 (defun subst2s (ex pat)
1209 (cond ((null ex) nil)
1210 ((m2 ex pat) var)
1211 ((atom ex) ex)
1212 (t (cons (subst2s (car ex) pat)
1213 (subst2s (cdr ex) pat)))))
1215 ;; Match (c*x+b), where c and b are free of x
1216 (defun simple-trig-arg (exp)
1217 (m2 exp '((mplus) ((mtimes)
1218 ((coefftt) (c freevar))
1219 ((coefftt) (v varp)))
1220 ((coeffpp) (b freevar)))))
1222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1224 ;;; Stage II
1225 ;;; Implementation of Method 6: Elementary function of trigonometric functions
1227 (defun monstertrig (exp var *trigarg*)
1228 (declare (special *trigarg*))
1229 (when (and (not (atom *trigarg*))
1230 ;; Do not exute the following code when called from rischint.
1231 (not *in-risch-p*))
1232 (let ((arg (simple-trig-arg *trigarg*)))
1233 (cond (arg
1234 ;; We have trig(c*x+b). Use the substitution y=c*x+b to
1235 ;; try to compute the integral. Why? Because x*sin(n*x)
1236 ;; takes longer and longer as n gets larger and larger.
1237 ;; This is caused by the Risch integrator. This is a
1238 ;; work-around for this issue.
1239 (let* ((c (cdras 'c arg))
1240 (b (cdras 'b arg))
1241 (new-var (gensym "NEW-VAR-"))
1242 (new-exp (maxima-substitute (div (sub new-var b) c)
1243 var exp)))
1244 (putprop new-var t 'internal)
1245 (if (every-trigarg-alike new-exp new-var)
1246 ;; avoid endless recursion when more than one
1247 ;; trigarg exists or c is a float
1248 (return-from monstertrig
1249 (maxima-substitute
1250 *trigarg*
1251 new-var
1252 (div (integrator new-exp new-var) c))))))
1254 (return-from monstertrig (rischint exp var))))))
1255 (prog (*notsame* w a b y d)
1256 (declare (special *notsame*))
1257 (cond
1258 ((supertrig exp) (go a))
1259 ((null *notsame*) (return nil))
1260 ;; Check for an expression like a*trig1(m*x)*trig2(n*x),
1261 ;; where trig1 and trig2 are sin or cos.
1262 ((not (setq y (m2 exp
1263 '((mtimes)
1264 ((coefftt) (a freevar))
1265 (((b trig1))
1266 ((mtimes)
1267 (x varp)
1268 ((coefftt) (m freevar))))
1269 (((d trig1))
1270 ((mtimes)
1271 (x varp)
1272 ((coefftt) (n freevar))))))))
1273 (go b))
1274 ; This check has been done with the pattern match.
1275 ; ((not (and (member (car (setq b (cdr (assoc 'b y :test #'eq)))) '(%sin %cos) :test #'eq)
1276 ; (member (car (setq d (cdr (assoc 'd y :test #'eq)))) '(%sin %cos) :test #'eq)))
1277 ; (return nil))
1278 ((progn
1279 ;; The tests after this depend on values of b and d being
1280 ;; set. Set them here unconditionally, before doing the
1281 ;; tests.
1282 (setq b (cdras 'b y))
1283 (setq d (cdras 'd y))
1284 (and (eq (car b) '%sin)
1285 (eq (car d) '%sin)))
1286 ;; We have a*sin(m*x)*sin(n*x).
1287 ;; The integral is: a*(sin((m-n)*x)/(2*(m-n))-sin((m+n)*x)/(2*(m+n))
1288 (return (subliss y
1289 '((mtimes) a
1290 ((mplus)
1291 ((mquotient)
1292 ((%sin) ((mtimes) ((mplus) m ((mtimes) -1 n)) x))
1293 ((mtimes) 2 ((mplus) m ((mtimes) -1 n))))
1294 ((mtimes) -1
1295 ((mquotient)
1296 ((%sin) ((mtimes) ((mplus) m n) x))
1297 ((mtimes) 2 ((mplus) m n)))))))))
1298 ((and (eq (car b) '%cos) (eq (car d) '%cos))
1299 ;; We have a*cos(m*x)*cos(n*x).
1300 ;; The integral is: a*(sin((m-n)*x)/(2*(m-n))+sin((m+n)*x)/(2*(m+n))
1301 (return (subliss y
1302 '((mtimes) a
1303 ((mplus)
1304 ((mquotient)
1305 ((%sin) ((mtimes) ((mplus) m ((mtimes) -1 n)) x))
1306 ((mtimes) 2
1307 ((mplus) m ((mtimes) -1 n))))
1308 ((mquotient)
1309 ((%sin) ((mtimes) ((mplus) m n) x))
1310 ((mtimes) 2 ((mplus) m n))))))))
1311 ((or (and (eq (car b) '%cos)
1312 ;; The following (destructively!) swaps the values of
1313 ;; m and n if first trig term is sin. I (rtoy) don't
1314 ;; understand why this is needed. The formula
1315 ;; doesn't depend on that.
1316 (setq w (cdras 'm y ))
1317 (rplacd (assoc 'm y) (cdras 'n y))
1318 (rplacd (assoc 'n y) w))
1320 ;; We have a*cos(n*x)*sin(m*x).
1321 ;; The integral is: -a*(cos((m-n)*x)/(2*(m-n))+cos((m+n)*x)/(2*(m+n))
1322 (return (subliss y
1323 '((mtimes) -1 a
1324 ((mplus)
1325 ((mquotient)
1326 ((%cos) ((mtimes) ((mplus) m ((mtimes) -1 n)) x))
1327 ((mtimes) 2 ((mplus) m ((mtimes) -1 n))))
1328 ((mquotient)
1329 ((%cos) ((mtimes) ((mplus) m n) x))
1330 ((mtimes) 2 ((mplus) m n)))))))))
1331 b ;; At this point we have trig functions with different arguments,
1332 ;; but not a product of sin and cos.
1333 (cond ((not (setq y (prog2
1334 (setq *trigarg* var)
1335 (m2 exp
1336 '((mtimes)
1337 ((coefftt) (a freevar))
1338 (((b trig1))
1339 ((mtimes)
1340 (x varp)
1341 ((coefftt) (n integerp2))))
1342 ((coefftt) (c supertrig)))))))
1343 (return nil)))
1344 ;; We have a product of trig functions: trig1(n*x)*trig2(y).
1345 ;; trig1 is sin or cos, where n is a numerical integer. trig2 is not a sin
1346 ;; or cos. The cos or sin function is expanded.
1347 (return
1348 (integrator
1349 ($expand
1350 (list '(mtimes)
1351 (cdras 'a y) ; constant factor
1352 (cdras 'c y) ; trig functions
1353 (cond ((eq (car (cdras 'b y)) '%cos) ; expand cos(n*x)
1354 (maxima-substitute var
1356 (supercosnx (cdras 'n y))))
1357 (t ; expand sin(x*x)
1358 (maxima-substitute var
1360 (supersinx (cdras 'n y)))))))
1361 var))
1362 a ;; A product of trig functions and all trig functions have the same
1363 ;; argument *trigarg*. Maxima substitutes *trigarg* with the variable var
1364 ;; of integration and calls trigint to integrate the new problem.
1365 (setq w (subst2s exp *trigarg*))
1366 (setq b (cdras 'b (m2-b*x+a *trigarg*)))
1367 (setq a (substint *trigarg* var (trigint (div* w b) var)))
1368 (return (if (isinop a '%integrate)
1369 (list '(%integrate) exp var)
1370 a))))
1372 (defun trig2 (x)
1373 (member (car x) '(%sin %cos %tan %cot %sec %csc) :test #'eq))
1375 (defun supersinx (n)
1376 (let ((i (if (< n 0) -1 1)))
1377 ($expand (list '(mtimes) i (sinnx (timesk i n))))))
1379 (defun supercosnx (n)
1380 ($expand (cosnx (timesk (if (< n 0) -1 1) n))))
1382 (defun sinnx (n)
1383 (if (equal n 1)
1384 '((%sin) x)
1385 (list '(mplus)
1386 (list '(mtimes) '((%sin) x) (cosnx (1- n)))
1387 (list '(mtimes) '((%cos) x) (sinnx (1- n))))))
1389 (defun cosnx (n)
1390 (if (equal n 1)
1391 '((%cos) x)
1392 (list '(mplus)
1393 (list '(mtimes) '((%cos) x) (cosnx (1- n)))
1394 (list '(mtimes) -1 '((%sin) x) (sinnx (1- n))))))
1396 (defun poseven (x)
1397 (and (even x) (> x -1)))
1399 (defun trigfree (x)
1400 (if (atom x)
1401 (not (member x '(sin* cos* sec* tan*) :test #'eq))
1402 (and (trigfree (car x)) (trigfree (cdr x)))))
1404 (defun rat1 (exp)
1405 (prog (*b1* *notsame*)
1406 (declare (special *yy* *b1* *notsame*))
1407 (when (and (numberp exp) (zerop exp))
1408 (return nil))
1409 (setq *b1* (subst *b* 'b '((mexpt) b (n even))))
1410 (return (prog2
1411 (setq *yy* (rats exp))
1412 (cond ((not *notsame*) *yy*))))))
1414 (defun rats (exp)
1415 (prog (y)
1416 (declare (special *notsame* *b1*))
1417 (return
1418 (cond ((eq exp *a*) 'x)
1419 ((atom exp)
1420 (cond ((member exp '(sin* cos* sec* tan*) :test #'eq)
1421 (setq *notsame* t))
1422 (t exp)))
1423 ((setq y (m2 exp *b1*))
1424 (f3 y))
1425 (t (cons (car exp) (mapcar #'(lambda (g) (rats g)) (cdr exp))))))))
1427 (defun f3 (y)
1428 (maxima-substitute *c*
1430 (maxima-substitute (quotient (cdr (assoc 'n y :test #'eq)) 2)
1432 '((mexpt)
1433 ((mplus)
1435 ((mtimes)
1437 ((mexpt) x 2)))
1438 n))))
1440 (defun odd1 (n)
1441 (declare (special *yz*))
1442 (cond ((not (numberp n)) nil)
1443 ((not (equal (rem n 2) 0))
1444 (setq *yz*
1445 (maxima-substitute *c*
1447 (list '(mexpt)
1448 '((mplus) 1 ((mtimes) c ((mexpt) x 2)))
1449 (quotient (1- n) 2)))))
1450 (t nil)))
1452 (defun subvar (x)
1453 (maxima-substitute var 'x x))
1455 (defun subvardlg (x)
1456 (mapcar #'(lambda (m)
1457 (cons (maxima-substitute var 'x (car m)) (cdr m)))
1460 ;; This appears to be the implementation of Method 6, pp.82 in Moses' thesis.
1462 (defun trigint (exp var)
1463 (prog (y repl y1 y2 *yy* z m n *c* *yz* *a* *b* )
1464 (declare (special *yy* *yz*))
1465 ;; Transform trig(x) into trig* (for simplicity?) Convert cot to
1466 ;; tan and csc to sin.
1467 (setq y2
1468 (subliss (subvardlg '((((%sin) x) . sin*)
1469 (((%cos) x) . cos*)
1470 (((%tan) x) . tan*)
1471 (((%cot) x) . ((mexpt) tan* -1))
1472 (((%sec) x) . sec*)
1473 (((%csc) x) . ((mexpt) sin* -1))))
1474 exp))
1476 (when *debug-integrate*
1477 (format t "~& in TRIGINT:~%")
1478 (format t "~& : y2 = ~A~%" y2))
1480 ;; Now transform tan to sin/cos and sec to 1/cos.
1481 (setq y1 (setq y (subliss '((tan* . ((mtimes) sin*
1482 ((mexpt) cos* -1)))
1483 (sec* . ((mexpt) cos* -1)))
1484 y2)))
1486 (when *debug-integrate* (format t "~& : y = ~A~%" y))
1488 (when (null (setq z
1489 (m2 y
1490 '((mtimes)
1491 ((coefftt) (b trigfree))
1492 ((mexpt) sin* (m poseven))
1493 ((mexpt) cos* (n poseven))))))
1494 ;; Go if y is not of the form sin^m*cos^n for positive even m and n.
1495 (go l1))
1497 ;; Case III:
1498 ;; Handle the case of sin^m*cos^n, m, n both non-negative and even.
1500 (setq m (cdras 'm z))
1501 (setq n (cdras 'n z))
1502 (setq *a* (integerp2 (* 0.5 (if (< m n) 1 -1) (+ n (* -1 m)))))
1503 (setq z (cons (cons 'a *a*) z))
1504 (setq z (cons (cons 'x var) z))
1506 (when *debug-integrate*
1507 (format t "~& CASE III:~%")
1508 (format t "~& : m, n = ~A ~A~%" m n)
1509 (format t "~& : a = ~A~%" *a*)
1510 (format t "~& : z = ~A~%" z))
1512 ;; integrate(sin(y)^m*cos(y)^n,y) is transformed to the following form:
1514 ;; m < n: integrate((sin(2*y)/2)^n*(1/2+1/2*cos(2*y)^((n-m)/2),y)
1515 ;; m >= n: integrate((sin(2*y)/2)^n*(1/2-1/2*cos(2*y)^((m-n)/2),y)
1516 (return
1517 (mul (cdras 'b z)
1518 (div 1 2)
1519 (substint
1520 (mul 2 var)
1522 (integrator
1523 (cond ((< m n)
1524 (subliss z
1525 '((mtimes)
1526 ((mexpt)
1527 ((mtimes) ((rat simp) 1 2) ((%sin) x))
1529 ((mexpt)
1530 ((mplus)
1531 ((rat simp) 1 2)
1532 ((mtimes)
1533 ((rat simp) 1 2) ((%cos) x))) a))))
1535 (subliss z
1536 '((mtimes)
1537 ((mexpt)
1538 ((mtimes) ((rat simp) 1 2) ((%sin) x))
1540 ((mexpt)
1541 ((mplus)
1542 ((rat simp) 1 2)
1543 ((mtimes)
1544 ((rat simp) -1 2)
1545 ((%cos) x))) a)))))
1546 var))))
1548 ;; Case IV:
1549 ;; I think this is case IV, working on the expression in terms of
1550 ;; sin and cos.
1552 ;; Elem(x) means constants, x, trig functions of x, log and
1553 ;; inverse trig functions of x, and which are closed under
1554 ;; addition, multiplication, exponentiation, and substitution.
1556 ;; Elem(f(x)) is the same as Elem(x), but f(x) replaces x in the
1557 ;; definition.
1559 (when *debug-integrate* (format t "~& Case IV:~%"))
1561 (setq *c* -1)
1562 (setq *a* 'sin*)
1563 (setq *b* 'cos*)
1564 (when (and (m2 y '((coeffpt) (c rat1) ((mexpt) cos* (n odd1))))
1565 (setq repl (list '(%sin) var)))
1566 ;; The case cos^(2*n+1)*Elem(cos^2,sin). Use the substitution z = sin.
1567 (go getout))
1568 (setq *a* *b*)
1569 (setq *b* 'sin*)
1570 (when (and (m2 y '((coeffpt) (c rat1) ((mexpt) sin* (n odd1))))
1571 (setq repl (list '(%cos) var)))
1572 ;; The case sin^(2*n+1)*Elem(sin^2,cos). Use the substitution z = cos.
1573 (go get3))
1575 ;; Case V:
1576 ;; Transform sin and cos to tan and sec to see if the integral is
1577 ;; of the form Elem(tan, sec^2). If so, use the substitution z = tan.
1579 (when *debug-integrate* (format t "~& Case V:~%"))
1581 (setq y (subliss '((sin* (mtimes) tan* ((mexpt) sec* -1))
1582 (cos* (mexpt) sec* -1))
1583 y2))
1584 (setq *c* 1)
1585 (setq *a* 'tan*)
1586 (setq *b* 'sec*)
1587 (when (and (rat1 y) (setq repl (list '(%tan) var)))
1588 (go get1))
1589 (setq *a* *b*)
1590 (setq *b* 'tan*)
1591 (when (and (m2 y '((coeffpt) (c rat1) ((mexpt) tan* (n odd1))))
1592 (setq repl (list '(%sec) var)))
1593 (go getout))
1594 (when (not (alike1 (setq repl ($expand exp)) exp))
1595 (return (integrator repl var)))
1596 (setq y (subliss '((sin* (mtimes) 2 x
1597 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1))
1598 (cos* (mtimes)
1599 ((mplus) 1 ((mtimes) -1 ((mexpt) x 2)))
1600 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1)))
1601 y1))
1602 (setq y (list '(mtimes)
1604 '((mtimes) 2 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1))))
1605 (setq repl (subvar '((mquotient) ((%sin) x) ((mplus) 1 ((%cos) x)))))
1606 (go get2)
1607 get3
1608 (setq y (list '(mtimes) -1 *yy* *yz*))
1609 (go get2)
1610 get1
1611 (setq y (list '(mtimes) '((mexpt) ((mplus) 1 ((mexpt) x 2)) -1) *yy*))
1612 (go get2)
1613 getout
1614 (setq y (list '(mtimes) *yy* *yz*))
1615 get2
1616 (when *debug-integrate*
1617 (format t "~& Call the INTEGRATOR with:~%")
1618 (format t "~& : y = ~A~%" y)
1619 (format t "~& : repl = ~A~%" repl))
1620 ;; See Bug 2880797. We want atan(tan(x)) to simplify to x, so
1621 ;; set $triginverses to '$all.
1622 (return
1623 ;; Do not integrate for the global variable VAR, but substitute it.
1624 ;; This way possible assumptions on VAR are no longer present. The
1625 ;; algorithm of DEFINT depends on this behavior. See Bug 3085498.
1626 (let (($triginverses '$all) (newvar (gensym)))
1627 (substint repl
1628 newvar
1629 (integrator (maxima-substitute newvar 'x y) newvar))))))
1631 (defmvar $integration_constant_counter 0)
1632 (defmvar $integration_constant '$%c)
1634 ;; This is the top level of the integrator
1635 (defun sinint (exp var)
1636 ;; *integrator-level* is a recursion counter for INTEGRATOR. See
1637 ;; INTEGRATOR for more details. Initialize it here.
1638 (let ((*integrator-level* 0))
1639 (declare (special *integrator-level*))
1641 ;; Sanity checks for variables
1642 (when (mnump var)
1643 (merror (intl:gettext "integrate: variable must not be a number; found: ~:M") var))
1644 (when ($ratp var) (setf var (ratdisrep var)))
1645 (when ($ratp exp) (setf exp (ratdisrep exp)))
1647 (cond
1648 ;; Distribute over lists and matrices
1649 ((mxorlistp exp)
1650 (cons (car exp)
1651 (mapcar #'(lambda (y) (sinint y var)) (cdr exp))))
1653 ;; The symbolic integration code doesn't really deal very well with
1654 ;; subscripted variables, so if we have one then replace occurrences of var
1655 ;; with an atomic gensym and recurse.
1656 ((and (not (atom var))
1657 (member 'array (cdar var)))
1658 (let ((dummy-var (gensym)))
1659 (maxima-substitute var dummy-var
1660 (sinint (maxima-substitute dummy-var var exp) dummy-var))))
1662 ;; If exp is an equality, integrate both sides and add an integration
1663 ;; constant
1664 ((mequalp exp)
1665 (list (car exp) (sinint (cadr exp) var)
1666 (add (sinint (caddr exp) var)
1667 ($concat $integration_constant (incf $integration_constant_counter)))))
1669 ;; If var is an atom which occurs as an operator in exp, then return a noun form.
1670 ((and (atom var)
1671 (isinop exp var))
1672 (list '(%integrate) exp var))
1674 ((zerop1 exp) ;; special case because 0 will not pass sum-of-intsp test
1677 ((let ((ans (simplify
1678 (let ($opsubst varlist genvar stack)
1679 (integrator exp var)))))
1680 (if (sum-of-intsp ans)
1681 (list '(%integrate) exp var)
1682 ans))))))
1684 ;; SUM-OF-INTSP
1686 ;; This is a heuristic that SININT uses to work out whether the result from
1687 ;; INTEGRATOR is worth returning or whether just to return a noun form. If this
1688 ;; function returns T, then SININT will return a noun form.
1690 ;; The logic, as I understand it (Rupert 01/2014):
1692 ;; (1) If I integrate f(x) wrt x and get an atom other than x or 0, either
1693 ;; something's gone horribly wrong, or this is part of a larger
1694 ;; expression. In the latter case, we can return T here because hopefully
1695 ;; something else interesting will make the top-level return NIL.
1697 ;; (2) If I get a sum, return a noun form if every one of the summands is no
1698 ;; better than what I started with. (Presumably this is where the name
1699 ;; came from)
1701 ;; (3) If this is a noun form, it doesn't convey much information on its own,
1702 ;; so return T.
1704 ;; (4) If this is a product, something interesting has probably happened. But
1705 ;; I still want a noun form if the result is like 2*'integrate(f(x),x), so
1706 ;; I'm only interested in terms in the product that are not free of
1707 ;; VAR. If one of those terms is worthy of report, that's great: return
1708 ;; NIL. Otherwise, return T if we saw at least two things (eg splitting an
1709 ;; integral into a product of two integrals)
1711 ;; (5) If the result is free of VAR, we're in a similar position to (1).
1713 ;; (6) Otherwise something interesting (and hopefully useful) has
1714 ;; happened. Return NIL to tell SININT to report it.
1715 (defun sum-of-intsp (ans)
1716 (cond ((atom ans)
1717 ;; Result of integration should never be a constant other than zero.
1718 ;; If the result of integration is zero, it is either because:
1719 ;; 1) a subroutine inside integration failed and returned nil,
1720 ;; and (mul 0 nil) yielded 0, meaning that the result is wrong, or
1721 ;; 2) the original integrand was actually zero - this is handled
1722 ;; with a separate special case in sinint
1723 (not (eq ans var)))
1724 ((mplusp ans) (every #'sum-of-intsp (cdr ans)))
1725 ((eq (caar ans) '%integrate) t)
1726 ((mtimesp ans)
1727 (let ((int-factors 0))
1728 (not (or (dolist (factor (cdr ans))
1729 (unless (freeof var factor)
1730 (if (sum-of-intsp factor)
1731 (incf int-factors)
1732 (return t))))
1733 (<= 2 int-factors)))))
1734 ((freeof var ans) t)
1735 (t nil)))
1737 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1739 ;;; Stage I
1740 ;;; Implementation of Method 2: Integrate a summation
1742 (defun intsum (form var)
1743 (prog (exp idx ll ul pair val)
1744 (setq exp (cadr form)
1745 idx (caddr form)
1746 ll (cadddr form)
1747 ul (car (cddddr form)))
1748 (if (or (not (atom var))
1749 (not (free idx var))
1750 (not (free ll var))
1751 (not (free ul var)))
1752 (return (list '(%integrate) form var)))
1753 (setq pair (partition exp var 1))
1754 (when (and (mexptp (cdr pair))
1755 (eq (caddr pair) var))
1756 (setq val (maxima-substitute ll idx (cadddr pair)))
1757 (cond ((equal val -1)
1758 (return (add (integrator (maxima-substitute ll idx exp) var)
1759 (intsum1 exp idx (add 1 ll) ul var))))
1760 ((mlsp val -1)
1761 (return (list '(%integrate) form var)))))
1762 (return (intsum1 exp idx ll ul var))))
1764 (defun intsum1 (exp idx ll ul var)
1765 (assume (list '(mgeqp) idx ll))
1766 (if (not (eq ul '$inf))
1767 (assume (list '(mgeqp) ul idx)))
1768 (simplifya (list '(%sum) (integrator exp var) idx ll ul) t))
1770 (defun finds (x)
1771 (if (atom x)
1772 (member x '(%log %integrate %atan) :test #'eq)
1773 (or (finds (car x)) (finds (cdr x)))))
1775 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1777 ;;; Stage II
1778 ;;; Implementation of Method 9:
1779 ;;; Rational function times a log or arctric function
1781 ;;; ratlog is called for an expression containing a log or arctrig function
1782 ;;; The integrand is like log(x)*f'(x). To obtain the result the technique of
1783 ;;; partial integration is applied: log(x)*f(x)-integrate(1/x*f(x),x)
1785 (defun ratlog (exp var form)
1786 (prog (b c d y z w)
1787 (setq y form)
1788 (setq b (cdr (assoc 'b y :test #'eq)))
1789 (setq c (cdr (assoc 'c y :test #'eq)))
1790 (setq y (integrator c var))
1791 (when (finds y) (return nil))
1792 (setq d (sdiff (cdr (assoc 'a form :test #'eq)) var))
1794 (setq z (integrator (mul2* y d) var))
1795 (setq d (cdr (assoc 'a form :test #'eq)))
1796 (return (simplify (list '(mplus)
1797 (list '(mtimes) y d)
1798 (list '(mtimes) -1 z))))))
1800 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1802 ;;; partial-integration is an extension of the algorithm of ratlog to support
1803 ;;; the technique of partial integration for more cases. The integrand
1804 ;;; is like g(x)*f'(x) and the result is g(x)*f(x)-integrate(g'(x)*f(x),x).
1806 ;;; Adding integrals properties for elementary functions led to infinite recursion
1807 ;;; with integrate(z*expintegral_shi(z),z). This was resolved by limiting the
1808 ;;; recursion depth. *integrator-level* needs to be at least 3 to solve
1809 ;;; o integrate(expintegral_ei(1/sqrt(x)),x)
1810 ;;; o integrate(sqrt(z)*expintegral_li(z),z)
1811 ;;; while a value of 4 causes testsuite regressions with
1812 ;;; o integrate(z*expintegral_shi(z),z)
1813 (defun partial-integration (form var)
1814 (declare (special *integrator-level*))
1815 (let ((g (cdr (assoc 'a form))) ; part g(x)
1816 (df (cdr (assoc 'c form))) ; part f'(x)
1817 (f nil))
1818 (setq f (integrator df var)) ; integrate f'(x) wrt var
1819 (cond
1820 ((or (isinop f '%integrate) ; no result or
1821 (isinop f (caar g)) ; g in result
1822 (> *integrator-level* 3))
1823 nil) ; we return nil
1825 ;; Build the result: g(x)*f(x)-integrate(g'(x)*f(x))
1826 (add (mul f g)
1827 (mul -1 (integrator (mul f (sdiff g var)) var)))))))
1829 ;; returns t if argument of every trig operation in y matches arg
1830 (defun every-trigarg-alike (y arg)
1831 (cond ((atom y) t)
1832 ((optrig (caar y)) (alike1 arg (cadr y)))
1833 (t (every (lambda (exp)
1834 (every-trigarg-alike exp arg))
1835 (cdr y)))))
1837 ;; return argument of first trig operation encountered in y
1838 (defun find-first-trigarg (y)
1839 (cond ((atom y) nil)
1840 ((optrig (caar y)) (cadr y))
1841 (t (some (lambda (exp)
1842 (find-first-trigarg exp))
1843 (cdr y)))))
1845 ;; return constant factor that makes elements of alist match elements of blist
1846 ;; or nil if no match found
1847 ;; (we could replace this using rat package to divide alist and blist)
1848 (defun matchsum (alist blist)
1849 (prog (r s *c* *d*)
1850 (setq s (m2 (car alist) ;; find coeff for first term of alist
1851 '((mtimes)
1852 ((coefftt) (a freevar))
1853 ((coefftt) (c true)))))
1854 (setq *c* (cdr (assoc 'c s :test #'eq)))
1855 (cond ((not (setq r ;; find coeff for first term of blist
1856 (m2 (car blist)
1857 (cons '(mtimes)
1858 (cons '((coefftt) (b free1))
1859 (cond ((mtimesp *c*)
1860 (cdr *c*))
1861 (t (list *c*))))))))
1862 (return nil)))
1863 (setq *d* (simplify (list '(mtimes)
1864 (subliss s 'a)
1865 (list '(mexpt)
1866 (subliss r 'b)
1867 -1))))
1868 (cond ((m2 (cons '(mplus) alist) ;; check that all terms match
1869 (timesloop *d* blist))
1870 (return *d*))
1871 (t (return nil)))))
1873 (defun timesloop (a b)
1874 (cons '(mplus) (mapcar #'(lambda (c) (mul2* a c)) b)))
1876 (defun expands (aa b)
1877 (addn (mapcar #'(lambda (c) (timesloop c aa)) b) nil))
1879 (defun powerlist (exp var)
1880 (prog (y *c* *d* powerlist *b*)
1881 (setq y (m2 exp
1882 '((mtimes)
1883 ((mexpt) (var varp) (c integerp2))
1884 ((coefftt) (a freevar))
1885 ((coefftt) (b true)))))
1886 (setq *b* (cdr (assoc 'b y :test #'eq)))
1887 (setq *c* (cdr (assoc 'c y :test #'eq)))
1888 (unless (rat10 *b*) (return nil))
1889 (setq *d* (apply #'gcd (cons (1+ *c*) powerlist)))
1890 (when (or (eql 1 *d*) (zerop *d*)) (return nil))
1891 (return
1892 (substint
1893 (list '(mexpt) var *d*)
1895 (integrate5 (simplify (list '(mtimes)
1896 (power* *d* -1)
1897 (cdr (assoc 'a y :test #'eq))
1898 (list '(mexpt) var (1- (quotient (1+ *c*) *d*)))
1899 (subst10 *b*)))
1900 var)))))
1902 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1904 ;;; Stage I
1905 ;;; Implementation of Method 3: Derivative-divides algorithm
1907 ;; This is the derivative-divides algorithm of Moses.
1909 ;; /
1910 ;; [
1911 ;; Look for form I c * op(u(x)) * u'(x) dx
1912 ;; ]
1913 ;; /
1915 ;; where: c is a constant
1916 ;; u(x) is an elementary expression in x
1917 ;; u'(x) is its derivative
1918 ;; op is an elementary operator:
1919 ;; - the indentity, or
1920 ;; - any function that can be integrated by INTEGRALLOOKUPS
1922 ;; The method of solution, once the problem has been determined to
1923 ;; posses the form above, is to look up OP in a table and substitute
1924 ;; u(x) for each occurrence of x in the expression given in the table.
1925 ;; In other words, the method performs an implicit substitution y = u(x),
1926 ;; and obtains the integral of op(y)dy by a table look up.
1928 (defun diffdiv (exp var)
1929 (prog (y *a* x v *d* z w r)
1930 (cond ((and (mexptp exp)
1931 (mplusp (cadr exp))
1932 (integerp (caddr exp))
1933 (< (caddr exp) 6)
1934 (> (caddr exp) 0))
1935 (return (integrator (expandexpt (cadr exp) (caddr exp)) var))))
1937 ;; If not a product, transform to a product with one term
1938 (setq exp (cond ((mtimesp exp) exp) (t (list '(mtimes) exp))))
1940 ;; Loop over the terms in exp
1941 (setq z (cdr exp))
1942 a (setq y (car z))
1944 ;; This m2 pattern matches const*(exp/y)
1945 (setq r (list '(mplus)
1946 (cons '(coeffpt)
1947 (cons '(c free1)
1948 (remove y (cdr exp) :count 1)))))
1949 (cond
1950 ;; Case u(var) is the identity function. y is a term in exp.
1951 ;; Match if diff(y,var) == c*(exp/y).
1952 ;; This even works when y is a function with multiple args.
1953 ((setq w (m2 (sdiff y var) r))
1954 (return (muln (list y y (power* (mul2* 2 (cdr (assoc 'c w :test #'eq))) -1)) nil))))
1956 ;; w is the arg in y.
1957 (let ((arg-freevar))
1958 (setq w
1959 (cond
1960 ((or (atom y) (member (caar y) '(mplus mtimes) :test #'eq)) y)
1961 ;; Take the argument of a function with one value.
1962 ((= (length (cdr y)) 1) (cadr y))
1963 ;; A function has multiple args, and exactly one arg depends on var
1964 ((= (count-if #'null (setq arg-freevar (mapcar #'freevar (cdr y)))) 1)
1965 (do ((args (cdr y) (cdr args))
1966 (argf arg-freevar (cdr argf)))
1967 ((if (not (car argf)) (return (car args))))))
1968 (t 0))))
1970 (cond
1971 ((setq w (cond ((and (setq x (sdiff w var))
1972 (mplusp x)
1973 (setq *d* (remove y (cdr exp) :count 1))
1974 (setq v (car *d*))
1975 (mplusp v)
1976 (not (cdr *d*)))
1977 (cond ((setq *d* (matchsum (cdr x) (cdr v)))
1978 (list (cons 'c *d*)))
1979 (t nil)))
1980 (t (m2 x r))))
1981 (return (cond ((null (setq x (integrallookups y))) nil)
1982 ((eq w t) x)
1983 (t (mul2* x (power* (cdr (assoc 'c w :test #'eq)) -1)))))))
1984 (setq z (cdr z))
1985 (when (null z) (return nil))
1986 (go a)))
1988 (defun subliss (alist expr)
1989 "Alist is an alist consisting of a variable (symbol) and its value. expr is
1990 an expression. For each entry in alist, substitute the corresponding
1991 value into expr."
1992 (let ((x expr))
1993 (dolist (a alist x)
1994 (setq x (maxima-substitute (cdr a) (car a) x)))))
1996 (defun substint (x y expres)
1997 (if (and (not (atom expres)) (eq (caar expres) '%integrate))
1998 (list (car expres) exp var)
1999 (substint1 (maxima-substitute x y expres))))
2001 (defun substint1 (exp)
2002 (cond ((atom exp) exp)
2003 ((and (eq (caar exp) '%integrate)
2004 (null (cdddr exp))
2005 (not (symbolp (caddr exp)))
2006 (not (free (caddr exp) var)))
2007 (simplify (list '(%integrate)
2008 (mul2 (cadr exp) (sdiff (caddr exp) var))
2009 var)))
2010 (t (recur-apply #'substint1 exp))))
2012 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2014 ;:; Extension of the integrator for more integrals with power functions
2016 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2018 ;;; Recognize (a^(c*(z^r)^p+d)^v
2020 (defun m2-exp-type-1a (expr)
2021 (m2 expr
2022 '((mexpt)
2023 ((mexpt)
2024 (a freevar0)
2025 ((mplus)
2026 ;; The order of the pattern is critical. If we change it,
2027 ;; we do not get the expected match.
2028 ((coeffpp) (d freevar))
2029 ((coefft) (c freevar0)
2030 ((mexpt)
2031 ((mexpt) (z varp) (r freevar0))
2032 (p freevar)))))
2033 (v freevar))))
2035 ;;; Recognize z^v*a^(b*z^r+d)
2037 (defun m2-exp-type-2 (expr)
2038 (m2 expr
2039 '((mtimes)
2040 ((mexpt) (z varp) (v freevar0))
2041 ((mexpt)
2042 (a freevar0)
2043 ((mplus)
2044 ((coeffpp) (d freevar))
2045 ((coefft) (b freevar0) ((mexpt) (z varp) (r freevar0))))))))
2047 ;;; Recognize z^v*%e^(a*z^r+b)^u
2049 (defun m2-exp-type-2-1 (expr)
2050 (m2 expr
2051 '((mtimes)
2052 ((mexpt) (z varp) (v freevar0))
2053 ((mexpt)
2054 ((mexpt)
2056 ((mplus)
2057 ((coeffpp) (b freevar))
2058 ((coefft) (a freevar0) ((mexpt) (z varp) (r freevar0)))))
2059 (u freevar)))))
2061 ;;; Recognize (a*z+b)^p*%e^(c*z+d)
2063 (defun m2-exp-type-3 (expr)
2064 (m2 expr
2065 '((mtimes)
2066 ((mexpt)
2067 ((mplus)
2068 ((coefft) (a freevar0) (z varp))
2069 ((coeffpp) (b freevar)))
2070 (p freevar0))
2071 ((mexpt)
2073 ((mplus)
2074 ((coefft) (c freevar0) (z varp))
2075 ((coeffpp) (d freevar)))))))
2077 ;;; Recognize d^(a*z^2+b/z^2+c)
2079 (defun m2-exp-type-4 (expr)
2080 (m2 expr
2081 '((mexpt)
2082 (d freevar0)
2083 ((mplus)
2084 ((coefft) (a freevar0) ((mexpt) (z varp) 2))
2085 ((coefft) (b freevar0) ((mexpt) (z varp) -2))
2086 ((coeffpp) (c freevar))))))
2088 ;;; Recognize z^(2*n)*d^(a*z^2+b/z^2+c)
2090 (defun m2-exp-type-4-1 (expr)
2091 (m2 expr
2092 '((mtimes)
2093 ((mexpt) (z varp) (n freevar0))
2094 ((mexpt)
2095 (d freevar0)
2096 ((mplus)
2097 ((coefft) (a freevar0) ((mexpt) (z varp) 2))
2098 ((coefft) (b freevar0) ((mexpt) (z varp) -2))
2099 ((coeffpp) (c freevar)))))))
2101 ;;; Recognize z^n*d^(a*z^2+b*z+c)
2103 (defun m2-exp-type-5 (expr)
2104 (m2 expr
2105 '((mtimes)
2106 ((mexpt) (z varp) (n freevar))
2107 ((mexpt)
2108 (d freevar0)
2109 ((mplus)
2110 ((coeffpt) (a freevar) ((mexpt) (z varp) 2))
2111 ((coeffpt) (b freevar) (z varp))
2112 ((coeffpp) (c freevar)))))))
2114 ;;; Recognize z^n*(%e^(a*z^2+b*z+c))^u
2116 (defun m2-exp-type-5-1 (expr)
2117 (m2 expr
2118 '((mtimes)
2119 ((mexpt) (z varp) (n freevar0))
2120 ((mexpt)
2121 ((mexpt)
2123 ((mplus)
2124 ((coeffpp) (c freevar))
2125 ((coefft) (a freevar0) ((mexpt) (z varp) 2))
2126 ((coefft) (b freevar0) (z varp))))
2127 (u freevar)))))
2129 ;;; Recognize z^n*d^(a*sqrt(z)+b*z+c)
2131 (defun m2-exp-type-6 (expr)
2132 (m2 expr
2133 '((mtimes)
2134 ((mexpt) (z varp) (n freevar0))
2135 ((mexpt)
2136 (d freevar0)
2137 ((mplus)
2138 ((coefft) (a freevar0) ((mexpt) (z varp) ((rat) 1 2)))
2139 ((coefft) (b freevar0) (z varp))
2140 ((coeffpp) (c freevar)))))))
2142 ;;; Recognize z^n*(%e^(a*sqrt(z)+b*z+c))^u
2144 (defun m2-exp-type-6-1 (expr)
2145 (m2 expr
2146 '((mtimes)
2147 ((mexpt) (z varp) (n freevar0))
2148 ((mexpt)
2149 ((mexpt)
2151 ((mplus)
2152 ((coeffpp) (c freevar))
2153 ((coefft) (a freevar0) ((mexpt) (z varp) ((rat) 1 2)))
2154 ((coefft) (b freevar0) (z varp))))
2155 (u freevar)))))
2157 ;;; Recognize z^n*a^(b*z^r+e)*h^(c*z^r+g)
2159 (defun m2-exp-type-7 (expr)
2160 (m2 expr
2161 '((mtimes)
2162 ((mexpt) (z varp) (n freevar))
2163 ((mexpt)
2164 (a freevar0)
2165 ((mplus)
2166 ((coefft)
2167 (b freevar0)
2168 ((mexpt) (z varp) (r freevar0)))
2169 ((coeffpp) (e freevar))))
2170 ((mexpt)
2171 (h freevar0)
2172 ((mplus)
2173 ((coefft)
2174 (c freevar0)
2175 ((mexpt) (z varp) (r1 freevar0)))
2176 ((coeffpp) (g freevar)))))))
2178 ;;; Recognize z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u
2180 (defun m2-exp-type-7-1 (expr)
2181 (m2 expr
2182 '((mtimes)
2183 ((mexpt) (z varp) (v freevar))
2184 ((mexpt)
2185 ((mexpt)
2187 ((mplus)
2188 ((coeffpp) (e freevar))
2189 ((coefft) (b freevar0) ((mexpt) (z varp) (r freevar0)))))
2190 (q freevar))
2191 ((mexpt)
2192 ((mexpt)
2194 ((mplus)
2195 ((coeffpp) (g freevar))
2196 ((coefft) (c freevar0) ((mexpt) (z varp) (r1 freevar0)))))
2197 (u freevar)))))
2199 ;;; Recognize a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)
2201 (defun m2-exp-type-8 (expr)
2202 (m2 expr
2203 '((mtimes)
2204 ((mexpt)
2205 (a freevar0)
2206 ((mplus)
2207 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2208 ((coeffpt) (d freevar) (z varp))
2209 ((coeffpp) (e freevar))))
2210 ((mexpt)
2211 (h freevar0)
2212 ((mplus)
2213 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2214 ((coeffpt) (f freevar) (z varp))
2215 ((coeffpp) (g freevar)))))))
2217 ;;; Recognize (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v
2219 (defun m2-exp-type-8-1 (expr)
2220 (m2 expr
2221 '((mtimes)
2222 ((mexpt)
2223 ((mexpt)
2225 ((mplus)
2226 ((coeffpp) (e freevar))
2227 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2228 ((coeffpt) (d freevar) (z varp))))
2229 (u freevar))
2230 ((mexpt)
2231 ((mexpt)
2233 ((mplus)
2234 ((coeffpp) (g freevar))
2235 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2236 ((coeffpt) (f freevar) (z varp))))
2237 (v freevar)))))
2239 ;;; Recognize (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v
2241 (defun m2-exp-type-8-2 (expr)
2242 (m2 expr
2243 '((mtimes)
2244 ((mexpt)
2245 ((mexpt)
2247 ((mplus)
2248 ((coeffpp) (e freevar))
2249 ((coefft) (b freevar) ((mexpt) (z varp) (r freevar0)))))
2250 (u freevar))
2251 ((mexpt)
2252 ((mexpt)
2254 ((mplus)
2255 ((coeffpp) (g freevar))
2256 ((coefft) (c freevar) ((mexpt) (z varp) (r1 freevar0)))))
2257 (v freevar)))))
2259 ;;; Recognize z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)
2261 (defun m2-exp-type-9 (expr)
2262 (m2 expr
2263 '((mtimes)
2264 ((mexpt) (z varp) (n freevar))
2265 ((mexpt)
2266 (a freevar0)
2267 ((mplus)
2268 ((coeffpt) (b freevar) ((mexpt) (z varp) 2))
2269 ((coeffpt) (d freevar) (z varp))
2270 ((coeffpp) (e freevar))))
2271 ((mexpt)
2272 (h freevar0)
2273 ((mplus)
2274 ((coeffpt) (c freevar) ((mexpt) (z varp) 2))
2275 ((coeffpt) (f freevar) (z varp))
2276 ((coeffpp) (g freevar)))))))
2278 ;;; Recognize z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u
2280 (defun m2-exp-type-9-1 (expr)
2281 (m2 expr
2282 '((mtimes)
2283 ((mexpt) (z varp) (n freevar))
2284 ((mexpt)
2285 ((mexpt)
2287 ((mplus)
2288 ((coeffpp) (e freevar))
2289 ((coeffpt) (b freevar) ((mexpt) (z varp) 2))
2290 ((coeffpt) (d freevar) (z varp))))
2291 (q freevar))
2292 ((mexpt)
2293 ((mexpt)
2295 ((mplus)
2296 ((coeffpp) (g freevar))
2297 ((coeffpt) (c freevar) ((mexpt) (z varp) 2))
2298 ((coeffpt) (f freevar) (z varp))))
2299 (u freevar)))))
2301 ;;; Recognize z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z+)f*z+g)
2303 (defun m2-exp-type-10 (expr)
2304 (m2 expr
2305 '((mtimes)
2306 ((mexpt) (z varp) (n freevar))
2307 ((mexpt)
2308 (a freevar0)
2309 ((mplus)
2310 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2311 ((coeffpt) (d freevar) (z varp))
2312 ((coeffpp) (e freevar))))
2313 ((mexpt)
2314 (h freevar0)
2315 ((mplus)
2316 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2317 ((coeffpt) (f freevar) (z varp))
2318 ((coeffpp) (g freevar)))))))
2320 ;;; Recognize z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u
2322 (defun m2-exp-type-10-1 (expr)
2323 (m2 expr
2324 '((mtimes)
2325 ((mexpt) (z varp) (n freevar))
2326 ((mexpt)
2327 ((mexpt)
2329 ((mplus)
2330 ((coeffpp) (e freevar))
2331 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2332 ((coeffpt) (d freevar) (z varp))))
2333 (q freevar))
2334 ((mexpt)
2335 ((mexpt)
2337 ((mplus)
2338 ((coeffpp) (g freevar))
2339 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2340 ((coeffpt) (f freevar) (z varp))))
2341 (u freevar)))))
2343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2345 (defun integrate-exp-special (expr var &aux w const)
2347 ;; First factor the expression.
2348 (setq expr ($factor expr))
2350 ;; Remove constant factors.
2351 (setq w (partition expr var 1))
2352 (setq const (car w))
2353 (setq expr (cdr w))
2355 (schatchen-cond w
2356 ((m2-exp-type-1a (facsum-exponent expr))
2357 (a c d r p v)
2358 (when *debug-integrate*
2359 (format t "~&Type 1a: (a^(c*(z^r)^p+d)^v : w = ~A~%" w))
2361 (mul -1
2362 const
2363 ;; 1/(p*r*(a^(c*v*(var^r)^p)))
2364 (inv (mul p r (power a (mul c v (power (power var r) p)))))
2366 ;; (a^(d+c*(var^r)^p))^v
2367 (power (power a (add d (mul c (power (power var r) p)))) v)
2368 ;; gamma_incomplete(1/(p*r), -c*v*(var^r)^p*log(a))
2369 (take '(%gamma_incomplete)
2370 (inv (mul p r))
2371 (mul -1 c v (power (power var r) p) (take '(%log) a)))
2372 ;; (-c*v*(var^r)^p*log(a))^(-1/(p*r))
2373 (power (mul -1 c v (power (power var r) p) (take '(%log) a))
2374 (div -1 (mul p r)))))
2376 ((m2-exp-type-2 (facsum-exponent expr))
2377 (a b d v r)
2379 (when *debug-integrate*
2380 (format t "~&Type 2: z^v*a^(b*z^r+d) : w = ~A~%" w))
2382 (mul
2383 const
2384 (div -1 r)
2385 (power a d)
2386 (power var (add v 1))
2387 ($gamma_incomplete
2388 (div (add v 1) r)
2389 (mul -1 b (power var r) ($log a)))
2390 (power
2391 (mul -1 b (power var r) ($log a))
2392 (mul -1 (div (add v 1) r)))))
2394 ((m2-exp-type-2-1 (facsum-exponent expr))
2395 (a b v r u)
2396 (when *debug-integrate*
2397 (format t "~&Type 2-1: z^v*(%e^(a*z^r+b))^u : w = ~A~%" w))
2399 (mul const
2401 (inv r)
2402 (power '$%e (mul -1 a u (power var r)))
2403 (power (power '$%e (add (mul a (power var r)) b)) u)
2404 (power var (add v 1))
2405 (power (mul -1 a u (power var r)) (div (mul -1 (add v 1)) r))
2406 (take '(%gamma_incomplete)
2407 (div (add v 1) r)
2408 (mul -1 a u (power var r)))))
2410 ((m2-exp-type-3 (facsum-exponent expr))
2411 (a b c d p)
2412 (when *debug-integrate*
2413 (format t "~&Type 3: (a*z+b)^p*%e^(c*z+d) : w = ~A~%" w))
2414 (mul
2415 const
2416 (div -1 a)
2417 (power '$%e (sub d (div (mul b c) a)))
2418 (power (add b (mul a var)) (add p 1))
2419 (ftake '%expintegral_e (mul -1 p) (mul (div -1 a) c (add b (mul a var))))))
2421 ((m2-exp-type-4 expr)
2422 (a b c d)
2423 (let (($trigsign nil)) ; Do not simplify erfc(-x) !
2424 (when *debug-integrate*
2425 (format t "~&Type 4: d^(a*z^2+b/z^2+c) : w = ~A~%" w))
2427 (mul
2428 const
2429 (div 1 (mul 4 (power (mul -1 a ($log d)) (div 1 2))))
2430 (mul
2431 (power d c)
2432 (power '$%pi (div 1 2))
2433 (power '$%e
2434 (mul -2
2435 (power (mul -1 a ($log d)) (div 1 2))
2436 (power (mul -1 b ($log d)) (div 1 2))))
2437 (add
2438 ($erfc
2439 (add
2440 (div (power (mul -1 b ($log d)) (div 1 2)) var)
2441 (mul -1 var (power (mul -1 a ($log d)) (div 1 2)))))
2442 (mul -1
2443 (power '$%e
2444 (mul 4
2445 (power (mul -1 a ($log d)) (div 1 2))
2446 (power (mul -1 b ($log d)) (div 1 2))))
2447 ($erfc
2448 (add
2449 (mul var (power (mul -1 a ($log d)) (div 1 2)))
2450 (div (power (mul -1 b ($log d)) (div 1 2)) var)))))))))
2452 ((and (m2-exp-type-4-1 expr)
2453 (poseven (cdras 'n w)) ; only for n a positive, even integer
2454 (symbolp (cdras 'a w))) ; a has to be a symbol
2455 (a b c d n)
2456 (let (($trigsign nil)) ; Do not simplify erfc(-x) !
2458 (when *debug-integrate*
2459 (format t "~&Type 4-1: z^(2*n)*d^(a*z^2+b/z^2+c) : w = ~A~%" w))
2461 (setq n (div n 2))
2463 (mul const
2464 (div 1 4)
2465 (power d c)
2466 (power '$%pi (div 1 2))
2467 (simplify (list '(%derivative)
2468 (div
2469 (sub
2470 (mul
2471 (power ($log d) (mul -1 n))
2472 (add
2473 (mul
2474 (power
2475 '$%e
2476 (mul -2
2477 (power (mul -1 a ($log d)) (div 1 2))
2478 (power (mul -1 b ($log d)) (div 1 2))))
2479 ($erfc
2480 (sub
2481 (div
2482 (power (mul -1 b ($log d)) (div 1 2))
2483 var)
2484 (mul var (power (mul -1 ($log d)) (div 1 2))))))))
2485 (mul
2486 (power
2487 '$%e
2488 (mul 2
2489 (power (mul -1 a ($log d)) (div 1 2))
2490 (power (mul -1 b ($log d)) (div 1 2))))
2491 ($erfc
2492 (add
2493 (power (mul -1 a ($log d)) (div 1 2))
2494 (div (power (mul -1 b ($log d)) (div 1 2)) var)))))
2495 (power (mul -1 a ($log d)) (div 1 2)))
2496 a n)))))
2498 ((and (m2-exp-type-5 (facsum-exponent expr))
2499 (maxima-integerp (cdras 'n w))
2500 (eq ($sign (cdras 'n w)) '$pos))
2501 (a b c d n)
2503 (when *debug-integrate*
2504 (format t "~&Type 5: z^n*d^(a*z^2+b*z+c) : w = ~A~%" w))
2506 (mul
2507 const
2508 (div -1 (mul 2 (power (mul a ($log d)) (div 1 2))))
2509 (mul
2510 (power d (sub c (div (mul b b) (mul 4 a))))
2511 (let ((index (gensumindex))
2512 ($simpsum t))
2513 (mfuncall '$sum
2514 (mul
2515 (power 2 (sub index n))
2516 ($binomial n index)
2517 ($gamma_incomplete
2518 (div (add index 1) 2)
2519 (mul
2520 (div -1 (mul 4 a))
2521 (power (add b (mul 2 a var)) 2)
2522 ($log d)))
2523 (power (mul a ($log d)) (mul -1 (add n (div 1 2))))
2524 (power (mul -1 b ($log d)) (sub n index))
2525 (power (mul (add b (mul 2 a var)) ($log d)) (add index 1))
2526 (power
2527 (mul (div -1 a) (power (add b (mul 2 a var)) 2) ($log d))
2528 (mul (div -1 2) (add index 1))))
2529 index 0 n)))))
2531 ((and (m2-exp-type-5-1 (facsum-exponent expr))
2532 (maxima-integerp (cdras 'n w))
2533 (eq ($sign (cdras 'n w)) '$pos))
2534 (a b c u n)
2535 (when *debug-integrate*
2536 (format t "~&Type 5-1: z^n*(%e^(a*z^2+b*z+c))^u : w = ~A~%" w))
2538 (mul const
2539 (div -1 2)
2540 (power '$%e
2541 (add (mul -1 (div (mul b b u) (mul 4 a)))
2542 (mul -1 u (add (mul a var var) (mul b var)))))
2543 (power a (mul -1 (add n 1)))
2544 (power (power '$%e
2545 (add (mul a var var) (mul b var) c))
2547 (let ((index (gensumindex))
2548 ($simpsum t))
2549 (dosum
2550 (mul (power 2 (sub index n))
2551 (power (mul -1 b) (sub n index))
2552 (power (add b (mul 2 a var)) (add index 1))
2553 (power (div (mul -1 u (power (add b (mul 2 a var)) 2)) a)
2554 (mul (div -1 2) (add index 1)))
2555 (take '(%binomial) n index)
2556 (take '(%gamma_incomplete)
2557 (div (add index 1) 2)
2558 (div (mul -1 u (power (add b (mul 2 a var)) 2))
2559 (mul 4 a))))
2560 index 0 n t))))
2562 ((and (m2-exp-type-6 (facsum-exponent expr))
2563 (maxima-integerp (cdras 'n w))
2564 (eq ($sign (cdras 'n w)) '$pos))
2565 (a b c d n)
2566 (when *debug-integrate*
2567 (format t "~&Type 6: z^n*d^(a*sqrt(z)+b*z+c) : w = ~A~%" w))
2569 (mul
2570 const
2571 (power 2 (mul -1 (add n 1)))
2572 (power d (sub c (div (mul a a) (mul 4 b))))
2573 (power (mul b ($log d)) (mul -2 (add n 1)))
2574 (let ((index1 (gensumindex))
2575 (index2 (gensumindex))
2576 ($simpsum t))
2577 (mfuncall '$sum
2578 (mfuncall '$sum
2579 (mul
2580 (power -1 (sub index1 index2))
2581 (power 4 index1)
2582 ($binomial index1 index2)
2583 ($binomial n index1)
2584 ($log d)
2585 (power (mul a ($log d)) (sub (mul 2 n) (add index1 index2)))
2586 (power
2587 (mul (add a (mul 2 b (power var (div 1 2)))) ($log d))
2588 (add index1 index2))
2589 (power
2590 (mul
2591 (div -1 b)
2592 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2593 ($log d))
2594 (mul (div -1 2) (add index1 index2 1)))
2595 (add
2596 (mul 2 b
2597 (power
2598 (mul
2599 (div -1 b)
2600 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2601 ($log d))
2602 (div 1 2))
2603 ($gamma_incomplete
2604 (div (add index1 index2 2) 2)
2605 (mul
2606 (div -1 (mul 4 b))
2607 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2608 ($log d))))
2609 (mul a
2610 (add a (mul 2 b (power var (div 1 2))))
2611 ($log d)
2612 ($gamma_incomplete
2613 (div (add index1 index2 1) 2)
2614 (mul
2615 (div -1 (mul 4 b))
2616 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2617 ($log d))))))
2618 index2 0 index1)
2619 index1 0 n))))
2621 ((and (m2-exp-type-6-1 (facsum-exponent expr))
2622 (maxima-integerp (cdras 'n w))
2623 (eq ($sign (cdras 'n w)) '$pos))
2624 (a b c u n)
2625 (when *debug-integrate*
2626 (format t "~&Type 6-1: z^n*(%e^(a*sqrt(z)+b*z+c))^u : w = ~A~%" w))
2628 (mul const
2629 (power 2 (mul -1 (add (mul 2 n) 1)))
2630 (power '$%e
2631 (add (div (mul -1 u a a) (mul 4 b))
2632 (mul u (add (mul a (power var (div 1 2)))
2633 (mul b var)
2634 c))))
2635 (power b (mul -2 (add n 1)))
2636 (power (power '$%e
2637 (add (mul a (power var (div 1 2)))
2638 (mul b var)))
2640 (let ((index1 (gensumindex))
2641 (index2 (gensumindex))
2642 ($simpsum t))
2643 (dosum
2644 (dosum
2645 (mul (power -1 (sub index1 index2))
2646 (power 4 index1)
2647 (power a (add (neg index2) (neg index1) (mul 2 n)))
2648 (power (add a (mul 2 b (power var (div 1 2))))
2649 (add index1 index2))
2650 (power (div (mul -1 u
2651 (power (add a
2652 (mul 2
2654 (power var (div 1 2))))
2657 (mul (div -1 2) (add index1 index2 1)))
2658 (take '(%binomial) index1 index2)
2659 (take '(%binomial) n index1)
2660 (add (mul a
2661 (add a (mul 2 b (power var (div 1 2))))
2662 (take '(%gamma_incomplete)
2663 (div (add index1 index2 1) 2)
2664 (div (mul -1 u
2665 (power (add a
2666 (mul 2 b
2667 (power var
2668 (div 1 2))))
2670 (mul 4 b))))
2671 (mul (inv u)
2672 (power (div (mul -1 u
2673 (power (add a
2674 (mul 2 b
2675 (power var
2676 (div 1 2))))
2679 (div 1 2))
2680 (mul 2 b)
2681 (take '(%gamma_incomplete)
2682 (div (add index1 index2 2) 2)
2683 (div (mul -1 u
2684 (power (add a
2685 (mul 2 b
2686 (power var (div 1 2))))
2688 (mul 4 b))))))
2689 index2 0 index1 t)
2690 index1 0 n t))))
2692 ((and (m2-exp-type-7 (facsum-exponent expr))
2693 (eq ($sign (sub (cdras 'r w) (cdras 'r1 w))) '$zero))
2694 (a b c e g h r n)
2695 (when *debug-integrate*
2696 (format t "~&Type 7: z^n*a^(b*z^r+e)*h^(c*z^r+g) : w = ~A~%" w))
2698 (setq n (add n 1))
2700 (mul
2701 const
2702 (power var n)
2703 (div -1 r)
2704 (power a e)
2705 (power h g)
2706 (power
2707 (mul -1
2708 (power var r)
2709 (add (mul b ($log a)) (mul c ($log h))))
2710 (div (mul -1 n) r))
2711 ($gamma_incomplete
2712 (div n r)
2713 (mul -1 (power var r) (add (mul b ($log a)) (mul c ($log h)))))))
2715 ((and (m2-exp-type-7-1 (facsum-exponent expr))
2716 (eq ($sign (sub (cdras 'r w) (cdras 'r1 w))) '$zero))
2717 (b c e g r v q u)
2718 (when *debug-integrate*
2719 (format t "~&Type 7-1: z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u : w = ~A~%" w))
2721 (mul const
2722 (div -1 r)
2723 (power '$%e (mul -1 (power var r) (add (mul b q) (mul c u))))
2724 (power (power '$%e (add e (mul b (power var r)))) q)
2725 (power (power '$%e (add g (mul c (power var r)))) u)
2726 (power var (add v 1))
2727 (power (mul -1 (power var r) (add (mul b q) (mul c u)))
2728 (div (mul -1 (add v 1)) r))
2729 (take '(%gamma_incomplete)
2730 (div (add v 1) r)
2731 (mul -1 (power var r) (add (mul b q) (mul c u))))))
2733 ((m2-exp-type-8 (facsum-exponent expr))
2734 (a b c d e f g h)
2735 (when *debug-integrate*
2736 (format t "~&Type 8: a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2737 (format t "~& : w = ~A~%" w))
2739 (mul
2740 const
2741 (div 1 2)
2742 (power a e)
2743 (power h g)
2744 (add
2745 (mul 2
2746 (power a (add (mul b (power var (div 1 2))) (mul d var)))
2747 (power h (add (mul c (power var (div 1 2))) (mul f var)))
2748 (div 1 (add (mul d ($log a)) (mul f ($log h)))))
2749 (mul -1
2750 (power '$%pi (div 1 2))
2751 (power '$%e
2752 (mul -1
2753 (div
2754 (power (add (mul b ($log a)) (mul c ($log h))) 2)
2755 (mul 4 (add (mul d ($log a)) (mul f ($log h)))))))
2756 ($erfi
2757 (div
2758 (add
2759 (mul b ($log a))
2760 (mul c ($log h))
2761 (mul 2
2762 (power var (div 1 2))
2763 (add (mul d ($log a)) (mul f ($log h)))))
2764 (mul 2
2765 (power (add (mul d ($log a)) (mul f ($log h))) (div 1 2)))))
2766 (add (mul b ($log a)) (mul c ($log h)))
2767 (power (add (mul d ($log a)) (mul f ($log h))) (div -3 2))))))
2769 ((m2-exp-type-8-1 (facsum-exponent expr))
2770 (b c d e f g u v)
2771 (when *debug-integrate*
2772 (format t "~&Type 8-1: (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v")
2773 (format t "~& : w = ~A~%" w))
2775 (mul const
2776 (div 1 2)
2777 (power (add (mul d u) (mul f v)) (div -3 2))
2778 (mul (power '$%e
2779 (mul -1
2780 (power (add (mul b u)
2781 (mul 2 d u (power var (div 1 2)))
2782 (mul v (add c (mul 2 f (power var (div 1 2))))))
2784 (inv (mul 4 (add (mul d u) (mul f v))))))
2785 (power (power '$%e
2786 (add (mul b (power var (div 1 2)))
2788 (mul d var)))
2790 (power (power '$%e
2791 (add (mul c (power var (div 1 2)))
2793 (mul f var)))
2795 (add (mul 2
2796 (power '$%e
2797 (mul (power (add (mul b u)
2798 (mul 2 d u (power var (div 1 2)))
2799 (mul v (add c (mul 2 f (power var (div 1 2))))))
2801 (inv (mul 4 (add (mul d u) (mul f v))))))
2802 (power (add (mul d u) (mul f v)) (div 1 2)))
2803 (mul -1
2804 (power '$%pi (div 1 2))
2805 (add (mul b u) (mul c v))
2806 (take '(%erfi)
2807 (div (add (mul b u)
2808 (mul 2 d u (power var (div 1 2)))
2809 (mul c v)
2810 (mul 2 f v (power var (div 1 2))))
2811 (mul 2
2812 (power (add (mul d u) (mul f v))
2813 (div 1 2))))))))))
2815 ((and (m2-exp-type-8-2 (facsum-exponent expr))
2816 (eq ($sign (sub (cdras 'r w) (cdras 'r1 w))) '$zero))
2817 (b c e g r u v)
2818 (when *debug-integrate*
2819 (format t "~&Type 8-2: (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v")
2820 (format t "~& : w = ~A~%" w))
2822 (mul const
2824 (inv r)
2825 (power '$%e
2826 (mul -1
2827 (power var r)
2828 (add (mul b u) (mul c v))))
2829 (power (power '$%e
2830 (add (power var r) e))
2832 (power (power '$%e
2833 (add (power var r) g))
2836 (power (mul -1
2837 (power var r)
2838 (add (mul b u) (mul c v)))
2839 (div -1 r))
2840 (take '(%gamma_incomplete)
2841 (div 1 r)
2842 (mul -1 (power var r) (add (mul b u) (mul c v))))))
2844 ((and (m2-exp-type-9 (facsum-exponent expr))
2845 (maxima-integerp (cdras 'n w))
2846 (eq ($sign (cdras 'n w)) '$pos)
2847 (or (not (eq ($sign (cdras 'b w)) '$zero))
2848 (not (eq ($sign (cdras 'c w)) '$zero))))
2849 (a b c d e f g h n)
2850 (when *debug-integrate*
2851 (format t "~&Type 9: z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)")
2852 (format t "~& : w = ~A~%" w))
2854 (mul
2855 const
2856 (div -1 2)
2857 (power a e)
2858 (power h g)
2859 (power '$%e
2860 (div
2861 (power (add (mul d ($log a)) (mul f ($log h))) 2)
2862 (mul -4 (add (mul b ($log a)) (mul c ($log h))))))
2863 (power (add (mul b ($log a)) (mul c ($log h))) (mul -1 (add n 1)))
2864 (let ((index (gensumindex))
2865 ($simpsum t))
2866 (mfuncall '$sum
2867 (mul
2868 (power 2 (sub index n))
2869 ($binomial n index)
2870 (power
2871 (add (mul -1 d ($log a)) (mul -1 f ($log h)))
2872 (sub n index))
2873 (power
2874 (add
2875 (mul (add d (mul 2 b var)) ($log a))
2876 (mul (add f (mul 2 c var)) ($log h)))
2877 (add index 1))
2878 (power
2879 (mul -1
2880 (div
2881 (power
2882 (add
2883 (mul (add d (mul 2 b var)) ($log a))
2884 (mul (add f (mul 2 c var)) ($log h)))
2886 (add (mul b ($log a)) (mul c ($log h)))))
2887 (div (add index 1) -2))
2888 ($gamma_incomplete
2889 (div (add index 1) 2)
2890 (mul -1
2891 (div
2892 (power
2893 (add
2894 (mul (add d (mul 2 b var)) ($log a))
2895 (mul (add f (mul 2 c var)) ($log h)))
2897 (mul 4 (add (mul b ($log a)) (mul c ($log h))))))))
2898 index 0 n))))
2900 ((and (m2-exp-type-9-1 (facsum-exponent expr))
2901 (maxima-integerp (cdras 'n w))
2902 (eq ($sign (cdras 'n w)) '$pos)
2903 (or (not (eq ($sign (cdras 'b w)) '$zero))
2904 (not (eq ($sign (cdras 'c w)) '$zero))))
2905 (b c d e f g q u n)
2906 (when *debug-integrate*
2907 (format t "~&Type 9-1: z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u")
2908 (format t "~& : w = ~A~%" w))
2910 (mul const
2911 (div -1 2)
2912 (power (add (mul b q) (mul c u)) (div -1 2))
2913 (power '$%e
2914 (add (div (power (add (mul d q) (mul f u)) 2)
2915 (mul -4 (add (mul b q) (mul c u))))
2916 (mul -1 var
2917 (add (mul d q)
2918 (mul b q var)
2919 (mul f u)
2920 (mul c u var)))))
2921 (power (power '$%e
2922 (add e
2923 (mul var (add d (mul b var)))))
2925 (power (power '$%e
2926 (add g
2927 (mul var (add f (mul c var)))))
2929 (let ((index (gensumindex))
2930 ($simpsum t))
2931 (dosum
2932 (mul (power 2 (sub index n))
2933 (power (add (mul b q) (mul c u)) (neg (add n (div 1 2))))
2934 (power (add (neg (mul d q)) (neg (mul f u)))
2935 (sub n index))
2936 (power (add (mul d q)
2937 (mul f u)
2938 (mul 2 var (add (mul b q) (mul c u))))
2939 (add index 1))
2940 (power (div (power (add (mul d q)
2941 (mul f u)
2942 (mul 2
2943 (add (mul b q)
2944 (mul c u))
2945 var))
2947 (neg (add (mul b q) (mul c u))))
2948 (mul (div -1 2) (add index 1)))
2949 (take '(%binomial) n index)
2950 (take '(%gamma_incomplete)
2951 (div (add index 1) 2)
2952 (div (power (add (mul d q)
2953 (mul f u)
2954 (mul 2
2955 (add (mul b q)
2956 (mul c u))
2957 var))
2959 (mul -4 (add (mul b q) (mul c u))))))
2960 index 0 n t))))
2962 ((and (m2-exp-type-10 (facsum-exponent expr))
2963 (maxima-integerp (cdras 'n w))
2964 (eq ($sign (cdras 'n w)) '$pos)
2965 (or (not (eq ($sign (cdras 'b w)) '$zero))
2966 (not (eq ($sign (cdras 'c w)) '$zero))))
2967 (a b c d e f g h n)
2968 (when *debug-integrate*
2969 (format t "~&Type 10: z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2970 (format t "~& : w = ~A~%" w))
2972 (mul const
2973 (power 2 (add (mul -2 n) -1))
2974 (power a e)
2975 (power h g)
2976 (power '$%e
2977 (div (power (add (mul b ($log a)) (mul c ($log h))) 2)
2978 (mul -4 (add (mul d ($log a)) (mul f ($log h))))))
2979 (power (add (mul d ($log a)) (mul f ($log h))) (mul -2 (add n 1)))
2980 (let ((index1 (gensumindex))
2981 (index2 (gensumindex))
2982 ($simpsum t))
2983 (dosum
2984 (dosum
2985 (mul (power -1 (sub index1 index2))
2986 (power 4 index1)
2987 ($binomial index1 index2)
2988 ($binomial n index1)
2989 (power (add (mul b ($log a)) (mul c ($log h)))
2990 (sub (mul 2 n) (add index1 index2)))
2991 (power (add (mul b ($log a))
2992 (mul c ($log h))
2993 (mul 2
2994 (power var (div 1 2))
2995 (add (mul d ($log a)) (mul f ($log h)))))
2996 (add index1 index2))
2997 (power (mul -1
2998 (div (power (add (mul b ($log a))
2999 (mul c ($log h))
3000 (mul 2
3001 (power var (div 1 2))
3002 (add (mul d ($log a))
3003 (mul f ($log h)))))
3005 (add (mul d ($log a)) (mul f ($log h)))))
3006 (mul (div -1 2) (add index1 index2 1)))
3007 (add (mul ($gamma_incomplete (mul (div 1 2)
3008 (add index1 index2 1))
3009 (mul (div -1 4)
3010 (div (power (add (mul b ($log a))
3011 (mul c ($log h))
3012 (mul 2
3013 (power var (div 1 2))
3014 (add (mul d ($log a)) (mul f ($log h)))))
3016 (add (mul d ($log a)) (mul f ($log h))))))
3017 (add (mul b ($log a)) (mul c ($log h)))
3018 (add (mul b ($log a))
3019 (mul c ($log h))
3020 (mul 2
3021 (power var (div 1 2))
3022 (add (mul d ($log a)) (mul f ($log h))))))
3023 (mul 2
3024 ($gamma_incomplete (mul (div 1 2)
3025 (add index1 index2 2))
3026 (mul (div -1 4)
3027 (div (power (add (mul b ($log a))
3028 (mul c ($log h))
3029 (mul 2
3030 (power var (div 1 2))
3031 (add (mul d ($log a))
3032 (mul f ($log h)))))
3034 (add (mul d ($log a))
3035 (mul f ($log h))))))
3036 (add (mul d ($log a)) (mul f ($log h)))
3037 (power (mul -1
3038 (div (power (add (mul b ($log a))
3039 (mul c ($log h))
3040 (mul 2
3041 (power var (div 1 2))
3042 (add (mul d ($log a))
3043 (mul f ($log h)))))
3045 (add (mul d ($log a))
3046 (mul f ($log h)))))
3047 (div 1 2)))))
3048 index2 0 index1 t)
3049 index1 0 n t))))
3051 ((and (m2-exp-type-10-1 (facsum-exponent expr))
3052 (maxima-integerp (cdras 'n w))
3053 (eq ($sign (cdras 'n w)) '$pos)
3054 (or (not (eq ($sign (cdras 'b w)) '$zero))
3055 (not (eq ($sign (cdras 'c w)) '$zero))))
3056 (b c d e f g q u n)
3057 (let ((bq+cu (add (mul b q) (mul c u)))
3058 (dq+fu (add (mul d q) (mul f u))))
3059 (when *debug-integrate*
3060 (format t "~&Type 10-1: z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u")
3061 (format t "~& : w = ~A~%" w))
3063 (mul const
3064 (power 2 (mul -1 (add (mul 2 n) 1)))
3065 (power '$%e
3066 (add (div (mul -1 (power bq+cu 2)) (mul 4 dq+fu))
3067 (mul -1 d var q)
3068 (mul -1 b (power var (div 1 2)) q)
3069 (mul -1 f var u)
3070 (mul -1 c (power var (div 1 2)) u)))
3071 (power (power '$%e
3072 (add (mul b (power var (div 1 2)))
3073 (mul d var)
3076 (power (power '$%e
3077 (add (mul c (power var (div 1 2)))
3078 (mul f var)
3081 (power dq+fu (mul -2 (add n 1)))
3082 (let ((index1 (gensumindex))
3083 (index2 (gensumindex))
3084 ($simpsum t))
3085 (dosum
3086 (dosum
3087 (mul (power -1 (sub index1 index2))
3088 (power 4 index1)
3089 (power bq+cu
3090 (add (neg index1) (neg index2) (mul 2 n)))
3091 (power (add bq+cu
3092 (mul 2 (power var (div 1 2)) dq+fu))
3093 (add index1 index2))
3094 (power (div (power (add bq+cu
3095 (mul 2
3096 (power var (div 1 2))
3097 dq+fu))
3099 (mul -1 dq+fu))
3100 (mul (div -1 2)
3101 (add index1 index2 1)))
3102 (take '(%binomial) index1 index2)
3103 (take '(%binomial) n index1)
3104 (add (mul bq+cu
3105 (add bq+cu
3106 (mul 2
3107 (power var (div 1 2))
3108 dq+fu))
3109 (take '(%gamma_incomplete)
3110 (mul (div 1 2)
3111 (add index1 index2 1))
3112 (div (power (add (mul b q)
3113 (mul c u)
3114 (mul 2
3115 (power var (div 1 2))
3116 dq+fu))
3118 (mul -4
3119 dq+fu))))
3120 (mul 2
3121 (power (div (power (add bq+cu
3122 (mul 2
3123 (power var (div 1 2))
3124 dq+fu))
3126 (mul 1 dq+fu))
3127 (div 1 2))
3128 dq+fu
3129 (take '(%gamma_incomplete)
3130 (mul (div 1 2)
3131 (add index1 index2 2))
3132 (div (power (add bq+cu
3133 (mul 2
3134 (power var (div 1 2))
3135 dq+fu))
3137 (mul -4
3138 dq+fu))))))
3139 index2 0 index1 t)
3140 index1 0 n t)))))))
3142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3144 ;;; Do a facsum for the exponent of power functions.
3145 ;;; This is necessary to integrate all general forms. The pattern matcher is
3146 ;;; not powerful enough to do the job.
3148 (defun facsum-exponent (expr)
3149 ;; Make sure that expr has the form ((mtimes) factor1 factor2 ...)
3150 (when (not (mtimesp expr)) (setq expr (list '(mtimes) expr)))
3151 (do ((result nil)
3152 (l (cdr expr) (cdr l)))
3153 ((null l) (cons (list 'mtimes) result))
3154 (cond
3155 ((mexptp (car l))
3156 ;; Found an power function. Factor the exponent with facsum.
3157 (let* ((fac (mfuncall '$facsum (caddr (car l)) var))
3158 (num ($num fac))
3159 (den ($denom fac)))
3160 (setq result
3161 (cons (cons (list 'mexpt)
3162 (cons (cadr (car l))
3163 (if (equal 1 den)
3164 (list num)
3165 (list ($multthru (inv den) num)))))
3166 result))))
3168 ;; Nothing to do.
3169 (setq result (cons (car l) result))))))
3171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;