New version of the Plotting section of the manual reflecting recent changes
[maxima.git] / src / sin.lisp
blob427a66154652f8ad7a075b590e61e37fe2b7454c
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 (new-var (gensym "NEW-VAR-")))
770 (setq base bas1
771 pow pow1
772 exptflag nil)
773 ;; Transform the integrand. At this point resimplify, because it is not
774 ;; guaranteed, that a correct simplified expression is returned.
775 ;; Use a new variable to prevent facts on the old variable to be wrongly used.
776 (setq y (resimplify (maxima-substitute new-var var (elemxpt exp))))
777 (when exptflag (return nil))
778 ;; Integrate the transformed integrand and substitute back.
779 (return
780 ($multthru
781 (substint (list '(mexpt) base
782 (list '(mplus) (cdras 'a pow)
783 (list '(mtimes) (cdras 'b pow) var)))
784 new-var
785 (integrator (div y
786 (mul new-var
787 (cdras 'b pow)
788 (take '(%log) base))) new-var))))))
790 ;; Transform expressions like g^(b*x+a) to the common base base and
791 ;; do the substitution y = base^(b*x+a) in the expr.
792 (defun elemxpt (expr &aux w)
793 (cond ((freevar expr) expr)
794 ;; var is the base of a subexpression. The transformation fails.
795 ((atom expr) (setq exptflag t))
796 ((not (eq (caar expr) 'mexpt))
797 (cons (car expr)
798 (mapcar #'(lambda (c) (elemxpt c)) (cdr expr))))
799 ((not (freevar (cadr expr)))
800 (list '(mexpt)
801 (elemxpt (cadr expr))
802 (elemxpt (caddr expr))))
803 ;; Transform the expression to the common base.
804 ((not (eq (cadr expr) base))
805 (elemxpt (list '(mexpt)
806 base
807 (mul (power (take '(%log) base) -1)
808 (take '(%log) (cadr expr))
809 (caddr expr)))))
810 ;; The exponent must be linear in the variable of integration.
811 ((not (setq w (m2-b*x+a (caddr expr))))
812 (list (car expr) base (elemxpt (caddr expr))))
813 ;; Do the substitution y = g^(b*x+a).
815 (setq w (cons (cons 'bb (cdras 'b pow)) w))
816 (setq w (cons (cons 'aa (cdras 'a pow)) w))
817 (setq w (cons (cons 'base base) w))
818 (subliss w '((mtimes)
819 ((mexpt) base a)
820 ((mexpt)
821 base
822 ((mquotient)
823 ((mtimes) -1 aa b) bb))
824 ((mexpt)
826 ((mquotient) b bb)))))))
827 ) ; End of let
829 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
831 ;;; Stage II
832 ;;; Implementation of Method 3:
833 ;;; Substitution for a rational root of a linear fraction of x
835 ;;; This method is applicable when the integrand is of the form:
837 ;;; /
838 ;;; [ a x + b n1/m1 a x + b n1/m2
839 ;;; I R(x, (-------) , (-------) , ...) dx
840 ;;; ] c x + d c x + d
841 ;;; /
843 ;;; Substitute
845 ;;; (1) t = ((a*x+b)/(c*x+d))^(1/k), or
847 ;;; (2) x = (b-d*t^k)/(c*t^k-a)
849 ;;; where k is the least common multiplier of m1, m2, ... and
851 ;;; (3) dx = k*(a*d-b*c)*t^(k-1)/(a-c*t^k)^2 * dt
853 ;;; First, the algorithm calls the routine RAT3 to collect the roots of the
854 ;;; form ((a*x+b)/(c*x+d))^(n/m) in the list *ROOTLIST*.
855 ;;; search for the least common multiplier of m1, m2, ... then the
856 ;;; substitutions (2) and (3) are done and the new problem is integrated.
857 ;;; As always, W is an alist which associates to the coefficients
858 ;;; a, b... (and to VAR) their values.
860 (defvar *ratroot* nil) ; Expression of the form (a*x+b)/(c*x+d)
861 (defvar *rootlist* nil) ; List of powers of the expression *ratroot*.
863 (defun ratroot (exp var *ratroot* w)
864 (prog (*rootlist* k y w1)
865 ;; Check if the integrand has a chebyform, if so return the result.
866 (when (setq y (chebyf exp var)) (return y))
867 ;; Check if the integrand has a suitably form and collect the roots
868 ;; in the global special variable *ROOTLIST*.
869 (unless (rat3 exp t) (return nil))
870 ;; Get the least common multiplier of m1, m2, ...
871 (setq k (apply #'lcm *rootlist*))
872 (setq w1 (cons (cons 'k k) w))
873 ;; Substitute for the roots.
874 (setq y
875 (subst41 exp
876 (subliss w1
877 '((mquotient)
878 ((mplus) ((mtimes) b e)
879 ((mtimes) -1 d ((mexpt) var k)))
880 ((mplus) ((mtimes) c ((mexpt) var k))
881 ((mtimes) -1 e a))))
882 var))
883 ;; Integrate the new problem.
884 (setq y
885 (integrator
886 (mul y
887 (subliss w1
888 '((mquotient)
889 ((mtimes) e
890 ((mplus)
891 ((mtimes) a d k
892 ((mexpt) var ((mplus) -1 k)))
893 ((mtimes) -1
894 ((mtimes) b c k
895 ((mexpt) var ((mplus) -1 k))))))
896 ((mexpt) ((mplus)
897 ((mtimes) c ((mexpt) var k))
898 ((mtimes) -1 a e))
899 2))))
900 var))
901 ;; Substitute back and return the result.
902 (return (substint (power *ratroot* (power k -1)) var y))))
904 (defun rat3 (ex ind)
905 (cond ((freevar ex) t)
906 ((atom ex) ind)
907 ((member (caar ex) '(mtimes mplus) :test #'eq)
908 (do ((u (cdr ex) (cdr u)))
909 ((null u) t)
910 (if (not (rat3 (car u) ind))
911 (return nil))))
912 ((not (eq (caar ex) 'mexpt))
913 (rat3 (car (margs ex)) t))
914 ((freevar (cadr ex))
915 (rat3 (caddr ex) t))
916 ((integerp (caddr ex))
917 (rat3 (cadr ex) ind))
918 ((and (m2 (cadr ex) *ratroot*)
919 (denomfind (caddr ex)))
920 (setq *rootlist* (cons (denomfind (caddr ex)) *rootlist*)))
921 (t (rat3 (cadr ex) nil))))
923 (let ((rootform nil) ; Expression of the form x = (b*e-d*t^k)/(c*t^k-e*a).
924 (rootvar nil)) ; The variable we substitute for the root.
926 (defun subst4 (ex)
927 (cond ((freevar ex) ex)
928 ((atom ex) rootform)
929 ((not (eq (caar ex) 'mexpt))
930 (mapcar #'(lambda (u) (subst4 u)) ex))
931 ((m2 (cadr ex) *ratroot*)
932 (list (car ex) rootvar (integerp2 (timesk k (caddr ex)))))
933 (t (list (car ex) (subst4 (cadr ex)) (subst4 (caddr ex))))))
935 (defun subst41 (exp a b)
936 (setq rootform a
937 rootvar b)
938 ;; At this point resimplify, because it is not guaranteed, that a correct
939 ;; simplified expression is returned.
940 (resimplify (subst4 exp)))
941 ) ; End of let
943 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
945 ;;; Stage II
946 ;;; Implementation of Method 4: Binomial Chebyschev
948 ;; exp = a*t^r1*(c1+c2*t^q)^r2, where var = t.
950 ;; G&S 2.202 has says this integral can be expressed by elementary
951 ;; functions ii:
953 ;; 1. q is an integer
954 ;; 2. (r1+1)/q is an integer
955 ;; 3. (r1+1)/q+r2 is an integer.
957 ;; I (rtoy) think that for this code to work, r1, r2, and q must be numbers.
958 (defun chebyf (exp var)
959 (prog (r1 r2 d1 d2 n1 n2 w q)
960 ;; Return NIL if the expression doesn't match.
961 (when (not (setq w (m2-chebyform exp)))
962 (return nil))
963 #+nil
964 (format t "w = ~A~%" w)
965 (when (zerop1 (cdr (assoc 'c1 w :test #'eq)))
966 ;; rtoy: Is it really possible to be in this routine with c1 =
967 ;; 0?
968 (return
969 (mul*
970 ;; This factor is locally constant as long as t and
971 ;; c2*t^q avoid log's branch cut.
972 (subliss w '((mtimes) a ((mexpt) var ((mtimes) -1 q r2))
973 ((mexpt) ((mtimes) c2 ((mexpt) var q)) r2)))
974 (integrator
975 (subliss w '((mexpt) var ((mplus) r1 ((mtimes) q r2)))) var))))
976 (setq q (cdr (assoc 'q w :test #'eq)))
977 ;; Reset parameters. a = a/q, r1 = (1 - q + r1)/q
978 (setq w
979 (list* (cons 'a (div* (cdr (assoc 'a w :test #'eq)) q))
980 (cons
982 (div* (addn (list 1 (neg (simplify q)) (cdr (assoc 'r1 w :test #'eq))) nil) q))
984 #+nil
985 (format t "new w = ~A~%" w)
986 (setq r1 (cdr (assoc 'r1 w :test #'eq))
987 r2 (cdr (assoc 'r2 w :test #'eq)))
988 #+nil
989 (progn
990 (format t "new r1 = ~A~%" r1)
991 (format t "r2 = ~A~%" r2))
992 ;; Write r1 = d1/n1, r2 = d2/n2, if possible. Update w with
993 ;; these values, if so. If we can't, give up. I (rtoy) think
994 ;; this only happens if r1 or r2 can't be expressed as rational
995 ;; numbers. Hence, r1 and r2 have to be numbers, not variables.
996 (cond
997 ((not (and (setq d1 (denomfind r1))
998 (setq d2 (denomfind r2))
999 (setq n1 (integerp2 (timesk r1 d1)))
1000 (setq n2 (integerp2 (timesk r2 d2)))
1001 (setq w (list* (cons 'd1 d1) (cons 'd2 d2)
1002 (cons 'n1 n1) (cons 'n2 n2)
1003 w))))
1004 #+nil
1005 (progn
1006 (format t "cheby can't find one of d1,d2,n1,n2:~%")
1007 (format t " d1 = ~A~%" d1)
1008 (format t " d2 = ~A~%" d2)
1009 (format t " n1 = ~A~%" n1)
1010 (format t " n2 = ~A~%" n2))
1011 (return nil))
1012 ((and (integerp2 r1) (> r1 0))
1013 #+nil (format t "integer r1 > 0~%")
1014 ;; (r1+q-1)/q is positive integer.
1016 ;; I (rtoy) think we are using the substitution z=(c1+c2*t^q).
1017 ;; Maxima thinks the resulting integral should then be
1019 ;; a/q*c2^(-r1/q-1/q)*integrate(z^r2*(z-c1)^(r1/q+1/q-1),z)
1021 (return
1022 (substint
1023 (subliss w '((mplus) c1 ((mtimes) c2 ((mexpt) var q))))
1025 (integrator
1026 (expands (list (subliss w
1027 ;; a*t^r2*c2^(-r1-1)
1028 '((mtimes)
1030 ((mexpt) var r2)
1031 ((mexpt)
1033 ((mtimes)
1035 ((mplus) r1 1))))))
1036 (cdr
1037 ;; (t-c1)^r1
1038 (expandexpt (subliss w
1039 '((mplus)
1041 ((mtimes) -1 c1)))
1042 r1)))
1043 var))))
1044 ((integerp2 r2)
1045 #+nil (format t "integer r2~%")
1046 ;; I (rtoy) think this is using the substitution z = t^(q/d1).
1048 ;; The integral (as maxima will tell us) becomes
1050 ;; a*d1/q*integrate(z^(n1/q+d1/q-1)*(c1+c2*z^d1)^r2,z)
1052 ;; But be careful because the variable A in the code is
1053 ;; actually a/q.
1054 (return
1055 (substint (subliss w '((mexpt) var ((mquotient) q d1)))
1057 (ratint (simplify (subliss w
1058 '((mtimes)
1059 d1 a
1060 ((mexpt)
1062 ((mplus)
1063 n1 d1 -1))
1064 ((mexpt)
1065 ((mplus)
1066 ((mtimes)
1068 ((mexpt)
1069 var d1))
1071 r2))))
1072 var))))
1073 ((and (integerp2 r1) (< r1 0))
1074 #+nil (format t "integer r1 < 0~%")
1075 ;; I (rtoy) think this is using the substitution
1077 ;; z = (c1+c2*t^q)^(1/d2)
1079 ;; With this substitution, maxima says the resulting integral
1080 ;; is
1082 ;; a/q*c2^(-r1/q-1/q)*d2*
1083 ;; integrate(z^(n2+d2-1)*(z^d2-c1)^(r1/q+1/q-1),z)
1084 (return
1085 (substint (subliss w
1086 ;; (c1+c2*t^q)^(1/d2)
1087 '((mexpt)
1088 ((mplus)
1090 ((mtimes) c2 ((mexpt) var q)))
1091 ((mquotient) 1 d2)))
1093 (ratint (simplify (subliss w
1094 ;; This is essentially
1095 ;; the integrand above,
1096 ;; except A and R1 here
1097 ;; are not the same as
1098 ;; derived above.
1099 '((mtimes)
1100 a d2
1101 ((mexpt)
1103 ((mtimes)
1105 ((mplus)
1106 r1 1)))
1107 ((mexpt)
1109 ((mplus)
1110 n2 d2 -1))
1111 ((mexpt)
1112 ((mplus)
1113 ((mexpt)
1114 var d2)
1115 ((mtimes) -1 c1))
1116 r1))))
1117 var))))
1118 ((integerp2 (add* r1 r2))
1119 #+nil (format t "integer r1+r2~%")
1120 ;; If we're here, (r1-q+1)/q+r2 is an integer.
1122 ;; I (rtoy) think this is using the substitution
1124 ;; z = ((c1+c2*t^q)/t^q)^(1/d1)
1126 ;; With this substitution, maxima says the resulting integral
1127 ;; is
1129 ;; a*d2/q*c1^(r2+r1/q+1/q)*
1130 ;; integrate(z^(d2*r2+d2-1)*(z^d2-c2)^(-r2-r1/q-1/q-1),z)
1131 (return
1132 (substint (let (($radexpand '$all))
1133 ;; Setting $radexpand to $all here gets rid of
1134 ;; ABS in the subtitution. I think that's ok in
1135 ;; this case. See Bug 1654183.
1136 (subliss w
1137 '((mexpt)
1138 ((mquotient)
1139 ((mplus)
1141 ((mtimes) c2 ((mexpt) var q)))
1142 ((mexpt) var q))
1143 ((mquotient) 1 d1))))
1145 (ratint (simplify (subliss w
1146 '((mtimes)
1147 -1 a d1
1148 ((mexpt)
1150 ((mplus)
1151 r1 r2 1))
1152 ((mexpt)
1154 ((mplus)
1155 n2 d1 -1))
1156 ((mexpt)
1157 ((mplus)
1158 ((mexpt)
1159 var d1)
1160 ((mtimes)
1161 -1 c2))
1162 ((mtimes)
1164 ((mplus)
1165 r1 r2
1166 2))))))
1167 var))))
1168 (t (return (list '(%integrate) exp var))))))
1170 (defun greaterratp (x1 x2)
1171 (cond ((and (numberp x1) (numberp x2))
1172 (> x1 x2))
1173 ((ratnump x1)
1174 (greaterratp (quotient (float (cadr x1))
1175 (caddr x1))
1176 x2))
1177 ((ratnump x2)
1178 (greaterratp x1
1179 (quotient (float (cadr x2))
1180 (caddr x2))))))
1182 (defun trig1 (x)
1183 (member (car x) '(%sin %cos) :test #'eq))
1185 (defun supertrig (exp)
1186 (declare (special *notsame* *trigarg*))
1187 (cond ((freevar exp) t)
1188 ((atom exp) nil)
1189 ((member (caar exp) '(mplus mtimes) :test #'eq)
1190 (and (supertrig (cadr exp))
1191 (or (null (cddr exp))
1192 (supertrig (cons (car exp)
1193 (cddr exp))))))
1194 ((eq (caar exp) 'mexpt)
1195 (and (supertrig (cadr exp))
1196 (supertrig (caddr exp))))
1197 ((eq (caar exp) '%log)
1198 (supertrig (cadr exp)))
1199 ((member (caar exp)
1200 '(%sin %cos %tan %sec %cot %csc) :test #'eq)
1201 (cond ((m2 (cadr exp) *trigarg*) t)
1202 ((m2-b*x+a (cadr exp))
1203 (and (setq *notsame* t) nil))
1204 (t (supertrig (cadr exp)))))
1205 (t (supertrig (cadr exp)))))
1207 (defun subst2s (ex pat)
1208 (cond ((null ex) nil)
1209 ((m2 ex pat) var)
1210 ((atom ex) ex)
1211 (t (cons (subst2s (car ex) pat)
1212 (subst2s (cdr ex) pat)))))
1214 ;; Match (c*x+b), where c and b are free of x
1215 (defun simple-trig-arg (exp)
1216 (m2 exp '((mplus) ((mtimes)
1217 ((coefftt) (c freevar))
1218 ((coefftt) (v varp)))
1219 ((coeffpp) (b freevar)))))
1221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1223 ;;; Stage II
1224 ;;; Implementation of Method 6: Elementary function of trigonometric functions
1226 (defun monstertrig (exp var *trigarg*)
1227 (declare (special *trigarg*))
1228 (when (and (not (atom *trigarg*))
1229 ;; Do not exute the following code when called from rischint.
1230 (not *in-risch-p*))
1231 (let ((arg (simple-trig-arg *trigarg*)))
1232 (cond (arg
1233 ;; We have trig(c*x+b). Use the substitution y=c*x+b to
1234 ;; try to compute the integral. Why? Because x*sin(n*x)
1235 ;; takes longer and longer as n gets larger and larger.
1236 ;; This is caused by the Risch integrator. This is a
1237 ;; work-around for this issue.
1238 (let* ((c (cdras 'c arg))
1239 (b (cdras 'b arg))
1240 (new-var (gensym "NEW-VAR-"))
1241 (new-exp (maxima-substitute (div (sub new-var b) c)
1242 var exp)))
1243 (if (every-trigarg-alike new-exp new-var)
1244 ;; avoid endless recursion when more than one
1245 ;; trigarg exists or c is a float
1246 (return-from monstertrig
1247 (maxima-substitute
1248 *trigarg*
1249 new-var
1250 (div (integrator new-exp new-var) c))))))
1252 (return-from monstertrig (rischint exp var))))))
1253 (prog (*notsame* w a b y d)
1254 (declare (special *notsame*))
1255 (cond
1256 ((supertrig exp) (go a))
1257 ((null *notsame*) (return nil))
1258 ;; Check for an expression like a*trig1(m*x)*trig2(n*x),
1259 ;; where trig1 and trig2 are sin or cos.
1260 ((not (setq y (m2 exp
1261 '((mtimes)
1262 ((coefftt) (a freevar))
1263 (((b trig1))
1264 ((mtimes)
1265 (x varp)
1266 ((coefftt) (m freevar))))
1267 (((d trig1))
1268 ((mtimes)
1269 (x varp)
1270 ((coefftt) (n freevar))))))))
1271 (go b))
1272 ; This check has been done with the pattern match.
1273 ; ((not (and (member (car (setq b (cdr (assoc 'b y :test #'eq)))) '(%sin %cos) :test #'eq)
1274 ; (member (car (setq d (cdr (assoc 'd y :test #'eq)))) '(%sin %cos) :test #'eq)))
1275 ; (return nil))
1276 ((progn
1277 ;; The tests after this depend on values of b and d being
1278 ;; set. Set them here unconditionally, before doing the
1279 ;; tests.
1280 (setq b (cdras 'b y))
1281 (setq d (cdras 'd y))
1282 (and (eq (car b) '%sin)
1283 (eq (car d) '%sin)))
1284 ;; We have a*sin(m*x)*sin(n*x).
1285 ;; The integral is: a*(sin((m-n)*x)/(2*(m-n))-sin((m+n)*x)/(2*(m+n))
1286 (return (subliss y
1287 '((mtimes) a
1288 ((mplus)
1289 ((mquotient)
1290 ((%sin) ((mtimes) ((mplus) m ((mtimes) -1 n)) x))
1291 ((mtimes) 2 ((mplus) m ((mtimes) -1 n))))
1292 ((mtimes) -1
1293 ((mquotient)
1294 ((%sin) ((mtimes) ((mplus) m n) x))
1295 ((mtimes) 2 ((mplus) m n)))))))))
1296 ((and (eq (car b) '%cos) (eq (car d) '%cos))
1297 ;; We have a*cos(m*x)*cos(n*x).
1298 ;; The integral is: a*(sin((m-n)*x)/(2*(m-n))+sin((m+n)*x)/(2*(m+n))
1299 (return (subliss y
1300 '((mtimes) a
1301 ((mplus)
1302 ((mquotient)
1303 ((%sin) ((mtimes) ((mplus) m ((mtimes) -1 n)) x))
1304 ((mtimes) 2
1305 ((mplus) m ((mtimes) -1 n))))
1306 ((mquotient)
1307 ((%sin) ((mtimes) ((mplus) m n) x))
1308 ((mtimes) 2 ((mplus) m n))))))))
1309 ((or (and (eq (car b) '%cos)
1310 ;; The following (destructively!) swaps the values of
1311 ;; m and n if first trig term is sin. I (rtoy) don't
1312 ;; understand why this is needed. The formula
1313 ;; doesn't depend on that.
1314 (setq w (cdras 'm y ))
1315 (rplacd (assoc 'm y) (cdras 'n y))
1316 (rplacd (assoc 'n y) w))
1318 ;; We have a*cos(n*x)*sin(m*x).
1319 ;; The integral is: -a*(cos((m-n)*x)/(2*(m-n))+cos((m+n)*x)/(2*(m+n))
1320 (return (subliss y
1321 '((mtimes) -1 a
1322 ((mplus)
1323 ((mquotient)
1324 ((%cos) ((mtimes) ((mplus) m ((mtimes) -1 n)) x))
1325 ((mtimes) 2 ((mplus) m ((mtimes) -1 n))))
1326 ((mquotient)
1327 ((%cos) ((mtimes) ((mplus) m n) x))
1328 ((mtimes) 2 ((mplus) m n)))))))))
1329 b ;; At this point we have trig functions with different arguments,
1330 ;; but not a product of sin and cos.
1331 (cond ((not (setq y (prog2
1332 (setq *trigarg* var)
1333 (m2 exp
1334 '((mtimes)
1335 ((coefftt) (a freevar))
1336 (((b trig1))
1337 ((mtimes)
1338 (x varp)
1339 ((coefftt) (n integerp2))))
1340 ((coefftt) (c supertrig)))))))
1341 (return nil)))
1342 ;; We have a product of trig functions: trig1(n*x)*trig2(y).
1343 ;; trig1 is sin or cos, where n is a numerical integer. trig2 is not a sin
1344 ;; or cos. The cos or sin function is expanded.
1345 (return
1346 (integrator
1347 ($expand
1348 (list '(mtimes)
1349 (cdras 'a y) ; constant factor
1350 (cdras 'c y) ; trig functions
1351 (cond ((eq (car (cdras 'b y)) '%cos) ; expand cos(n*x)
1352 (maxima-substitute var
1354 (supercosnx (cdras 'n y))))
1355 (t ; expand sin(x*x)
1356 (maxima-substitute var
1358 (supersinx (cdras 'n y)))))))
1359 var))
1360 a ;; A product of trig functions and all trig functions have the same
1361 ;; argument *trigarg*. Maxima substitutes *trigarg* with the variable var
1362 ;; of integration and calls trigint to integrate the new problem.
1363 (setq w (subst2s exp *trigarg*))
1364 (setq b (cdras 'b (m2-b*x+a *trigarg*)))
1365 (setq a (substint *trigarg* var (trigint (div* w b) var)))
1366 (return (if (isinop a '%integrate)
1367 (list '(%integrate) exp var)
1368 a))))
1370 (defun trig2 (x)
1371 (member (car x) '(%sin %cos %tan %cot %sec %csc) :test #'eq))
1373 (defun supersinx (n)
1374 (let ((i (if (< n 0) -1 1)))
1375 ($expand (list '(mtimes) i (sinnx (timesk i n))))))
1377 (defun supercosnx (n)
1378 ($expand (cosnx (timesk (if (< n 0) -1 1) n))))
1380 (defun sinnx (n)
1381 (if (equal n 1)
1382 '((%sin) x)
1383 (list '(mplus)
1384 (list '(mtimes) '((%sin) x) (cosnx (1- n)))
1385 (list '(mtimes) '((%cos) x) (sinnx (1- n))))))
1387 (defun cosnx (n)
1388 (if (equal n 1)
1389 '((%cos) x)
1390 (list '(mplus)
1391 (list '(mtimes) '((%cos) x) (cosnx (1- n)))
1392 (list '(mtimes) -1 '((%sin) x) (sinnx (1- n))))))
1394 (defun poseven (x)
1395 (and (even x) (> x -1)))
1397 (defun trigfree (x)
1398 (if (atom x)
1399 (not (member x '(sin* cos* sec* tan*) :test #'eq))
1400 (and (trigfree (car x)) (trigfree (cdr x)))))
1402 (defun rat1 (exp)
1403 (prog (*b1* *notsame*)
1404 (declare (special *yy* *b1* *notsame*))
1405 (when (and (numberp exp) (zerop exp))
1406 (return nil))
1407 (setq *b1* (subst *b* 'b '((mexpt) b (n even))))
1408 (return (prog2
1409 (setq *yy* (rats exp))
1410 (cond ((not *notsame*) *yy*))))))
1412 (defun rats (exp)
1413 (prog (y)
1414 (declare (special *notsame* *b1*))
1415 (return
1416 (cond ((eq exp *a*) 'x)
1417 ((atom exp)
1418 (cond ((member exp '(sin* cos* sec* tan*) :test #'eq)
1419 (setq *notsame* t))
1420 (t exp)))
1421 ((setq y (m2 exp *b1*))
1422 (f3 y))
1423 (t (cons (car exp) (mapcar #'(lambda (g) (rats g)) (cdr exp))))))))
1425 (defun f3 (y)
1426 (maxima-substitute *c*
1428 (maxima-substitute (quotient (cdr (assoc 'n y :test #'eq)) 2)
1430 '((mexpt)
1431 ((mplus)
1433 ((mtimes)
1435 ((mexpt) x 2)))
1436 n))))
1438 (defun odd1 (n)
1439 (declare (special *yz*))
1440 (cond ((not (numberp n)) nil)
1441 ((not (equal (rem n 2) 0))
1442 (setq *yz*
1443 (maxima-substitute *c*
1445 (list '(mexpt)
1446 '((mplus) 1 ((mtimes) c ((mexpt) x 2)))
1447 (quotient (1- n) 2)))))
1448 (t nil)))
1450 (defun subvar (x)
1451 (maxima-substitute var 'x x))
1453 (defun subvardlg (x)
1454 (mapcar #'(lambda (m)
1455 (cons (maxima-substitute var 'x (car m)) (cdr m)))
1458 ;; This appears to be the implementation of Method 6, pp.82 in Moses' thesis.
1460 (defun trigint (exp var)
1461 (prog (y repl y1 y2 *yy* z m n *c* *yz* *a* *b* )
1462 (declare (special *yy* *yz*))
1463 ;; Transform trig(x) into trig* (for simplicity?) Convert cot to
1464 ;; tan and csc to sin.
1465 (setq y2
1466 (subliss (subvardlg '((((%sin) x) . sin*)
1467 (((%cos) x) . cos*)
1468 (((%tan) x) . tan*)
1469 (((%cot) x) . ((mexpt) tan* -1))
1470 (((%sec) x) . sec*)
1471 (((%csc) x) . ((mexpt) sin* -1))))
1472 exp))
1474 (when *debug-integrate*
1475 (format t "~& in TRIGINT:~%")
1476 (format t "~& : y2 = ~A~%" y2))
1478 ;; Now transform tan to sin/cos and sec to 1/cos.
1479 (setq y1 (setq y (subliss '((tan* . ((mtimes) sin*
1480 ((mexpt) cos* -1)))
1481 (sec* . ((mexpt) cos* -1)))
1482 y2)))
1484 (when *debug-integrate* (format t "~& : y = ~A~%" y))
1486 (when (null (setq z
1487 (m2 y
1488 '((mtimes)
1489 ((coefftt) (b trigfree))
1490 ((mexpt) sin* (m poseven))
1491 ((mexpt) cos* (n poseven))))))
1492 ;; Go if y is not of the form sin^m*cos^n for positive even m and n.
1493 (go l1))
1495 ;; Case III:
1496 ;; Handle the case of sin^m*cos^n, m, n both non-negative and even.
1498 (setq m (cdras 'm z))
1499 (setq n (cdras 'n z))
1500 (setq *a* (integerp2 (* 0.5 (if (< m n) 1 -1) (+ n (* -1 m)))))
1501 (setq z (cons (cons 'a *a*) z))
1502 (setq z (cons (cons 'x var) z))
1504 (when *debug-integrate*
1505 (format t "~& CASE III:~%")
1506 (format t "~& : m, n = ~A ~A~%" m n)
1507 (format t "~& : a = ~A~%" *a*)
1508 (format t "~& : z = ~A~%" z))
1510 ;; integrate(sin(y)^m*cos(y)^n,y) is transformed to the following form:
1512 ;; m < n: integrate((sin(2*y)/2)^n*(1/2+1/2*cos(2*y)^((n-m)/2),y)
1513 ;; m >= n: integrate((sin(2*y)/2)^n*(1/2-1/2*cos(2*y)^((m-n)/2),y)
1514 (return
1515 (mul (cdras 'b z)
1516 (div 1 2)
1517 (substint
1518 (mul 2 var)
1520 (integrator
1521 (cond ((< m n)
1522 (subliss z
1523 '((mtimes)
1524 ((mexpt)
1525 ((mtimes) ((rat simp) 1 2) ((%sin) x))
1527 ((mexpt)
1528 ((mplus)
1529 ((rat simp) 1 2)
1530 ((mtimes)
1531 ((rat simp) 1 2) ((%cos) x))) a))))
1533 (subliss z
1534 '((mtimes)
1535 ((mexpt)
1536 ((mtimes) ((rat simp) 1 2) ((%sin) x))
1538 ((mexpt)
1539 ((mplus)
1540 ((rat simp) 1 2)
1541 ((mtimes)
1542 ((rat simp) -1 2)
1543 ((%cos) x))) a)))))
1544 var))))
1546 ;; Case IV:
1547 ;; I think this is case IV, working on the expression in terms of
1548 ;; sin and cos.
1550 ;; Elem(x) means constants, x, trig functions of x, log and
1551 ;; inverse trig functions of x, and which are closed under
1552 ;; addition, multiplication, exponentiation, and substitution.
1554 ;; Elem(f(x)) is the same as Elem(x), but f(x) replaces x in the
1555 ;; definition.
1557 (when *debug-integrate* (format t "~& Case IV:~%"))
1559 (setq *c* -1)
1560 (setq *a* 'sin*)
1561 (setq *b* 'cos*)
1562 (when (and (m2 y '((coeffpt) (c rat1) ((mexpt) cos* (n odd1))))
1563 (setq repl (list '(%sin) var)))
1564 ;; The case cos^(2*n+1)*Elem(cos^2,sin). Use the substitution z = sin.
1565 (go getout))
1566 (setq *a* *b*)
1567 (setq *b* 'sin*)
1568 (when (and (m2 y '((coeffpt) (c rat1) ((mexpt) sin* (n odd1))))
1569 (setq repl (list '(%cos) var)))
1570 ;; The case sin^(2*n+1)*Elem(sin^2,cos). Use the substitution z = cos.
1571 (go get3))
1573 ;; Case V:
1574 ;; Transform sin and cos to tan and sec to see if the integral is
1575 ;; of the form Elem(tan, sec^2). If so, use the substitution z = tan.
1577 (when *debug-integrate* (format t "~& Case V:~%"))
1579 (setq y (subliss '((sin* (mtimes) tan* ((mexpt) sec* -1))
1580 (cos* (mexpt) sec* -1))
1581 y2))
1582 (setq *c* 1)
1583 (setq *a* 'tan*)
1584 (setq *b* 'sec*)
1585 (when (and (rat1 y) (setq repl (list '(%tan) var)))
1586 (go get1))
1587 (setq *a* *b*)
1588 (setq *b* 'tan*)
1589 (when (and (m2 y '((coeffpt) (c rat1) ((mexpt) tan* (n odd1))))
1590 (setq repl (list '(%sec) var)))
1591 (go getout))
1592 (when (not (alike1 (setq repl ($expand exp)) exp))
1593 (return (integrator repl var)))
1594 (setq y (subliss '((sin* (mtimes) 2 x
1595 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1))
1596 (cos* (mtimes)
1597 ((mplus) 1 ((mtimes) -1 ((mexpt) x 2)))
1598 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1)))
1599 y1))
1600 (setq y (list '(mtimes)
1602 '((mtimes) 2 ((mexpt) ((mplus) 1 ((mexpt) x 2)) -1))))
1603 (setq repl (subvar '((mquotient) ((%sin) x) ((mplus) 1 ((%cos) x)))))
1604 (go get2)
1605 get3
1606 (setq y (list '(mtimes) -1 *yy* *yz*))
1607 (go get2)
1608 get1
1609 (setq y (list '(mtimes) '((mexpt) ((mplus) 1 ((mexpt) x 2)) -1) *yy*))
1610 (go get2)
1611 getout
1612 (setq y (list '(mtimes) *yy* *yz*))
1613 get2
1614 (when *debug-integrate*
1615 (format t "~& Call the INTEGRATOR with:~%")
1616 (format t "~& : y = ~A~%" y)
1617 (format t "~& : repl = ~A~%" repl))
1618 ;; See Bug 2880797. We want atan(tan(x)) to simplify to x, so
1619 ;; set $triginverses to '$all.
1620 (return
1621 ;; Do not integrate for the global variable VAR, but substitute it.
1622 ;; This way possible assumptions on VAR are no longer present. The
1623 ;; algorithm of DEFINT depends on this behavior. See Bug 3085498.
1624 (let (($triginverses '$all) (newvar (gensym)))
1625 (substint repl
1626 newvar
1627 (integrator (maxima-substitute newvar 'x y) newvar))))))
1629 (defmvar $integration_constant_counter 0)
1630 (defmvar $integration_constant '$%c)
1632 ;; This is the top level of the integrator
1633 (defun sinint (exp var)
1634 ;; *integrator-level* is a recursion counter for INTEGRATOR. See
1635 ;; INTEGRATOR for more details. Initialize it here.
1636 (let ((*integrator-level* 0))
1637 (declare (special *integrator-level*))
1639 ;; Sanity checks for variables
1640 (when (mnump var)
1641 (merror (intl:gettext "integrate: variable must not be a number; found: ~:M") var))
1642 (when ($ratp var) (setf var (ratdisrep var)))
1643 (when ($ratp exp) (setf exp (ratdisrep exp)))
1645 (cond
1646 ;; Distribute over lists and matrices
1647 ((mxorlistp exp)
1648 (cons (car exp)
1649 (mapcar #'(lambda (y) (sinint y var)) (cdr exp))))
1651 ;; The symbolic integration code doesn't really deal very well with
1652 ;; subscripted variables, so if we have one then replace occurrences of var
1653 ;; with an atomic gensym and recurse.
1654 ((and (not (atom var))
1655 (member 'array (cdar var)))
1656 (let ((dummy-var (gensym)))
1657 (maxima-substitute var dummy-var
1658 (sinint (maxima-substitute dummy-var var exp) dummy-var))))
1660 ;; If exp is an equality, integrate both sides and add an integration
1661 ;; constant
1662 ((mequalp exp)
1663 (list (car exp) (sinint (cadr exp) var)
1664 (add (sinint (caddr exp) var)
1665 ($concat $integration_constant (incf $integration_constant_counter)))))
1667 ;; If var is an atom which occurs as an operator in exp, then return a noun form.
1668 ((and (atom var)
1669 (isinop exp var))
1670 (list '(%integrate) exp var))
1672 ((zerop1 exp) ;; special case because 0 will not pass sum-of-intsp test
1675 ((let ((ans (simplify
1676 (let ($opsubst varlist genvar stack)
1677 (integrator exp var)))))
1678 (if (sum-of-intsp ans)
1679 (list '(%integrate) exp var)
1680 ans))))))
1682 ;; SUM-OF-INTSP
1684 ;; This is a heuristic that SININT uses to work out whether the result from
1685 ;; INTEGRATOR is worth returning or whether just to return a noun form. If this
1686 ;; function returns T, then SININT will return a noun form.
1688 ;; The logic, as I understand it (Rupert 01/2014):
1690 ;; (1) If I integrate f(x) wrt x and get an atom other than x or 0, either
1691 ;; something's gone horribly wrong, or this is part of a larger
1692 ;; expression. In the latter case, we can return T here because hopefully
1693 ;; something else interesting will make the top-level return NIL.
1695 ;; (2) If I get a sum, return a noun form if every one of the summands is no
1696 ;; better than what I started with. (Presumably this is where the name
1697 ;; came from)
1699 ;; (3) If this is a noun form, it doesn't convey much information on its own,
1700 ;; so return T.
1702 ;; (4) If this is a product, something interesting has probably happened. But
1703 ;; I still want a noun form if the result is like 2*'integrate(f(x),x), so
1704 ;; I'm only interested in terms in the product that are not free of
1705 ;; VAR. If one of those terms is worthy of report, that's great: return
1706 ;; NIL. Otherwise, return T if we saw at least two things (eg splitting an
1707 ;; integral into a product of two integrals)
1709 ;; (5) If the result is free of VAR, we're in a similar position to (1).
1711 ;; (6) Otherwise something interesting (and hopefully useful) has
1712 ;; happened. Return NIL to tell SININT to report it.
1713 (defun sum-of-intsp (ans)
1714 (cond ((atom ans)
1715 ;; Result of integration should never be a constant other than zero.
1716 ;; If the result of integration is zero, it is either because:
1717 ;; 1) a subroutine inside integration failed and returned nil,
1718 ;; and (mul 0 nil) yielded 0, meaning that the result is wrong, or
1719 ;; 2) the original integrand was actually zero - this is handled
1720 ;; with a separate special case in sinint
1721 (not (eq ans var)))
1722 ((mplusp ans) (every #'sum-of-intsp (cdr ans)))
1723 ((eq (caar ans) '%integrate) t)
1724 ((mtimesp ans)
1725 (let ((int-factors 0))
1726 (not (or (dolist (factor (cdr ans))
1727 (unless (freeof var factor)
1728 (if (sum-of-intsp factor)
1729 (incf int-factors)
1730 (return t))))
1731 (<= 2 int-factors)))))
1732 ((freeof var ans) t)
1733 (t nil)))
1735 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1737 ;;; Stage I
1738 ;;; Implementation of Method 2: Integrate a summation
1740 (defun intsum (form var)
1741 (prog (exp idx ll ul pair val)
1742 (setq exp (cadr form)
1743 idx (caddr form)
1744 ll (cadddr form)
1745 ul (car (cddddr form)))
1746 (if (or (not (atom var))
1747 (not (free idx var))
1748 (not (free ll var))
1749 (not (free ul var)))
1750 (return (list '(%integrate) form var)))
1751 (setq pair (partition exp var 1))
1752 (when (and (mexptp (cdr pair))
1753 (eq (caddr pair) var))
1754 (setq val (maxima-substitute ll idx (cadddr pair)))
1755 (cond ((equal val -1)
1756 (return (add (integrator (maxima-substitute ll idx exp) var)
1757 (intsum1 exp idx (add 1 ll) ul var))))
1758 ((mlsp val -1)
1759 (return (list '(%integrate) form var)))))
1760 (return (intsum1 exp idx ll ul var))))
1762 (defun intsum1 (exp idx ll ul var)
1763 (assume (list '(mgeqp) idx ll))
1764 (if (not (eq ul '$inf))
1765 (assume (list '(mgeqp) ul idx)))
1766 (simplifya (list '(%sum) (integrator exp var) idx ll ul) t))
1768 (defun finds (x)
1769 (if (atom x)
1770 (member x '(%log %integrate %atan) :test #'eq)
1771 (or (finds (car x)) (finds (cdr x)))))
1773 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1775 ;;; Stage II
1776 ;;; Implementation of Method 9:
1777 ;;; Rational function times a log or arctric function
1779 ;;; ratlog is called for an expression containing a log or arctrig function
1780 ;;; The integrand is like log(x)*f'(x). To obtain the result the technique of
1781 ;;; partial integration is applied: log(x)*f(x)-integrate(1/x*f(x),x)
1783 (defun ratlog (exp var form)
1784 (prog (b c d y z w)
1785 (setq y form)
1786 (setq b (cdr (assoc 'b y :test #'eq)))
1787 (setq c (cdr (assoc 'c y :test #'eq)))
1788 (setq y (integrator c var))
1789 (when (finds y) (return nil))
1790 (setq d (sdiff (cdr (assoc 'a form :test #'eq)) var))
1792 (setq z (integrator (mul2* y d) var))
1793 (setq d (cdr (assoc 'a form :test #'eq)))
1794 (return (simplify (list '(mplus)
1795 (list '(mtimes) y d)
1796 (list '(mtimes) -1 z))))))
1798 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1800 ;;; partial-integration is an extension of the algorithm of ratlog to support
1801 ;;; the technique of partial integration for more cases. The integrand
1802 ;;; is like g(x)*f'(x) and the result is g(x)*f(x)-integrate(g'(x)*f(x),x).
1804 ;;; Adding integrals properties for elementary functions led to infinite recursion
1805 ;;; with integrate(z*expintegral_shi(z),z). This was resolved by limiting the
1806 ;;; recursion depth. *integrator-level* needs to be at least 3 to solve
1807 ;;; o integrate(expintegral_ei(1/sqrt(x)),x)
1808 ;;; o integrate(sqrt(z)*expintegral_li(z),z)
1809 ;;; while a value of 4 causes testsuite regressions with
1810 ;;; o integrate(z*expintegral_shi(z),z)
1811 (defun partial-integration (form var)
1812 (declare (special *integrator-level*))
1813 (let ((g (cdr (assoc 'a form))) ; part g(x)
1814 (df (cdr (assoc 'c form))) ; part f'(x)
1815 (f nil))
1816 (setq f (integrator df var)) ; integrate f'(x) wrt var
1817 (cond
1818 ((or (isinop f '%integrate) ; no result or
1819 (isinop f (caar g)) ; g in result
1820 (> *integrator-level* 3))
1821 nil) ; we return nil
1823 ;; Build the result: g(x)*f(x)-integrate(g'(x)*f(x))
1824 (add (mul f g)
1825 (mul -1 (integrator (mul f (sdiff g var)) var)))))))
1827 ;; returns t if argument of every trig operation in y matches arg
1828 (defun every-trigarg-alike (y arg)
1829 (cond ((atom y) t)
1830 ((optrig (caar y)) (alike1 arg (cadr y)))
1831 (t (every (lambda (exp)
1832 (every-trigarg-alike exp arg))
1833 (cdr y)))))
1835 ;; return argument of first trig operation encountered in y
1836 (defun find-first-trigarg (y)
1837 (cond ((atom y) nil)
1838 ((optrig (caar y)) (cadr y))
1839 (t (some (lambda (exp)
1840 (find-first-trigarg exp))
1841 (cdr y)))))
1843 ;; return constant factor that makes elements of alist match elements of blist
1844 ;; or nil if no match found
1845 ;; (we could replace this using rat package to divide alist and blist)
1846 (defun matchsum (alist blist)
1847 (prog (r s *c* *d*)
1848 (setq s (m2 (car alist) ;; find coeff for first term of alist
1849 '((mtimes)
1850 ((coefftt) (a freevar))
1851 ((coefftt) (c true)))))
1852 (setq *c* (cdr (assoc 'c s :test #'eq)))
1853 (cond ((not (setq r ;; find coeff for first term of blist
1854 (m2 (car blist)
1855 (cons '(mtimes)
1856 (cons '((coefftt) (b free1))
1857 (cond ((mtimesp *c*)
1858 (cdr *c*))
1859 (t (list *c*))))))))
1860 (return nil)))
1861 (setq *d* (simplify (list '(mtimes)
1862 (subliss s 'a)
1863 (list '(mexpt)
1864 (subliss r 'b)
1865 -1))))
1866 (cond ((m2 (cons '(mplus) alist) ;; check that all terms match
1867 (timesloop *d* blist))
1868 (return *d*))
1869 (t (return nil)))))
1871 (defun timesloop (a b)
1872 (cons '(mplus) (mapcar #'(lambda (c) (mul2* a c)) b)))
1874 (defun expands (aa b)
1875 (addn (mapcar #'(lambda (c) (timesloop c aa)) b) nil))
1877 (defun powerlist (exp var)
1878 (prog (y *c* *d* powerlist *b*)
1879 (setq y (m2 exp
1880 '((mtimes)
1881 ((mexpt) (var varp) (c integerp2))
1882 ((coefftt) (a freevar))
1883 ((coefftt) (b true)))))
1884 (setq *b* (cdr (assoc 'b y :test #'eq)))
1885 (setq *c* (cdr (assoc 'c y :test #'eq)))
1886 (unless (rat10 *b*) (return nil))
1887 (setq *d* (apply #'gcd (cons (1+ *c*) powerlist)))
1888 (when (or (eql 1 *d*) (zerop *d*)) (return nil))
1889 (return
1890 (substint
1891 (list '(mexpt) var *d*)
1893 (integrate5 (simplify (list '(mtimes)
1894 (power* *d* -1)
1895 (cdr (assoc 'a y :test #'eq))
1896 (list '(mexpt) var (1- (quotient (1+ *c*) *d*)))
1897 (subst10 *b*)))
1898 var)))))
1900 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1902 ;;; Stage I
1903 ;;; Implementation of Method 3: Derivative-divides algorithm
1905 ;; This is the derivative-divides algorithm of Moses.
1907 ;; /
1908 ;; [
1909 ;; Look for form I c * op(u(x)) * u'(x) dx
1910 ;; ]
1911 ;; /
1913 ;; where: c is a constant
1914 ;; u(x) is an elementary expression in x
1915 ;; u'(x) is its derivative
1916 ;; op is an elementary operator:
1917 ;; - the indentity, or
1918 ;; - any function that can be integrated by INTEGRALLOOKUPS
1920 ;; The method of solution, once the problem has been determined to
1921 ;; posses the form above, is to look up OP in a table and substitute
1922 ;; u(x) for each occurrence of x in the expression given in the table.
1923 ;; In other words, the method performs an implicit substitution y = u(x),
1924 ;; and obtains the integral of op(y)dy by a table look up.
1926 (defun diffdiv (exp var)
1927 (prog (y *a* x v *d* z w r)
1928 (cond ((and (mexptp exp)
1929 (mplusp (cadr exp))
1930 (integerp (caddr exp))
1931 (< (caddr exp) 6)
1932 (> (caddr exp) 0))
1933 (return (integrator (expandexpt (cadr exp) (caddr exp)) var))))
1935 ;; If not a product, transform to a product with one term
1936 (setq exp (cond ((mtimesp exp) exp) (t (list '(mtimes) exp))))
1938 ;; Loop over the terms in exp
1939 (setq z (cdr exp))
1940 a (setq y (car z))
1942 ;; This m2 pattern matches const*(exp/y)
1943 (setq r (list '(mplus)
1944 (cons '(coeffpt)
1945 (cons '(c free1)
1946 (remove y (cdr exp) :count 1)))))
1947 (cond
1948 ;; Case u(var) is the identity function. y is a term in exp.
1949 ;; Match if diff(y,var) == c*(exp/y).
1950 ;; This even works when y is a function with multiple args.
1951 ((setq w (m2 (sdiff y var) r))
1952 (return (muln (list y y (power* (mul2* 2 (cdr (assoc 'c w :test #'eq))) -1)) nil))))
1954 ;; w is the arg in y.
1955 (let ((arg-freevar))
1956 (setq w
1957 (cond
1958 ((or (atom y) (member (caar y) '(mplus mtimes) :test #'eq)) y)
1959 ;; Take the argument of a function with one value.
1960 ((= (length (cdr y)) 1) (cadr y))
1961 ;; A function has multiple args, and exactly one arg depends on var
1962 ((= (count-if #'null (setq arg-freevar (mapcar #'freevar (cdr y)))) 1)
1963 (do ((args (cdr y) (cdr args))
1964 (argf arg-freevar (cdr argf)))
1965 ((if (not (car argf)) (return (car args))))))
1966 (t 0))))
1968 (cond
1969 ((setq w (cond ((and (setq x (sdiff w var))
1970 (mplusp x)
1971 (setq *d* (remove y (cdr exp) :count 1))
1972 (setq v (car *d*))
1973 (mplusp v)
1974 (not (cdr *d*)))
1975 (cond ((setq *d* (matchsum (cdr x) (cdr v)))
1976 (list (cons 'c *d*)))
1977 (t nil)))
1978 (t (m2 x r))))
1979 (return (cond ((null (setq x (integrallookups y))) nil)
1980 ((eq w t) x)
1981 (t (mul2* x (power* (cdr (assoc 'c w :test #'eq)) -1)))))))
1982 (setq z (cdr z))
1983 (when (null z) (return nil))
1984 (go a)))
1986 (defun subliss (alist expr)
1987 "Alist is an alist consisting of a variable (symbol) and its value. expr is
1988 an expression. For each entry in alist, substitute the corresponding
1989 value into expr."
1990 (let ((x expr))
1991 (dolist (a alist x)
1992 (setq x (maxima-substitute (cdr a) (car a) x)))))
1994 (defun substint (x y expres)
1995 (if (and (not (atom expres)) (eq (caar expres) '%integrate))
1996 (list (car expres) exp var)
1997 (substint1 (maxima-substitute x y expres))))
1999 (defun substint1 (exp)
2000 (cond ((atom exp) exp)
2001 ((and (eq (caar exp) '%integrate)
2002 (null (cdddr exp))
2003 (not (symbolp (caddr exp)))
2004 (not (free (caddr exp) var)))
2005 (simplify (list '(%integrate)
2006 (mul2 (cadr exp) (sdiff (caddr exp) var))
2007 var)))
2008 (t (recur-apply #'substint1 exp))))
2010 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2012 ;:; Extension of the integrator for more integrals with power functions
2014 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2016 ;;; Recognize (a^(c*(z^r)^p+d)^v
2018 (defun m2-exp-type-1a (expr)
2019 (m2 expr
2020 '((mexpt)
2021 ((mexpt)
2022 (a freevar0)
2023 ((mplus)
2024 ;; The order of the pattern is critical. If we change it,
2025 ;; we do not get the expected match.
2026 ((coeffpp) (d freevar))
2027 ((coefft) (c freevar0)
2028 ((mexpt)
2029 ((mexpt) (z varp) (r freevar0))
2030 (p freevar)))))
2031 (v freevar))))
2033 ;;; Recognize z^v*a^(b*z^r+d)
2035 (defun m2-exp-type-2 (expr)
2036 (m2 expr
2037 '((mtimes)
2038 ((mexpt) (z varp) (v freevar0))
2039 ((mexpt)
2040 (a freevar0)
2041 ((mplus)
2042 ((coeffpp) (d freevar))
2043 ((coefft) (b freevar0) ((mexpt) (z varp) (r freevar0))))))))
2045 ;;; Recognize z^v*%e^(a*z^r+b)^u
2047 (defun m2-exp-type-2-1 (expr)
2048 (m2 expr
2049 '((mtimes)
2050 ((mexpt) (z varp) (v freevar0))
2051 ((mexpt)
2052 ((mexpt)
2054 ((mplus)
2055 ((coeffpp) (b freevar))
2056 ((coefft) (a freevar0) ((mexpt) (z varp) (r freevar0)))))
2057 (u freevar)))))
2059 ;;; Recognize (a*z+b)^p*%e^(c*z+d)
2061 (defun m2-exp-type-3 (expr)
2062 (m2 expr
2063 '((mtimes)
2064 ((mexpt)
2065 ((mplus)
2066 ((coefft) (a freevar0) (z varp))
2067 ((coeffpp) (b freevar)))
2068 (p freevar0))
2069 ((mexpt)
2071 ((mplus)
2072 ((coefft) (c freevar0) (z varp))
2073 ((coeffpp) (d freevar)))))))
2075 ;;; Recognize d^(a*z^2+b/z^2+c)
2077 (defun m2-exp-type-4 (expr)
2078 (m2 expr
2079 '((mexpt)
2080 (d freevar0)
2081 ((mplus)
2082 ((coefft) (a freevar0) ((mexpt) (z varp) 2))
2083 ((coefft) (b freevar0) ((mexpt) (z varp) -2))
2084 ((coeffpp) (c freevar))))))
2086 ;;; Recognize z^(2*n)*d^(a*z^2+b/z^2+c)
2088 (defun m2-exp-type-4-1 (expr)
2089 (m2 expr
2090 '((mtimes)
2091 ((mexpt) (z varp) (n freevar0))
2092 ((mexpt)
2093 (d freevar0)
2094 ((mplus)
2095 ((coefft) (a freevar0) ((mexpt) (z varp) 2))
2096 ((coefft) (b freevar0) ((mexpt) (z varp) -2))
2097 ((coeffpp) (c freevar)))))))
2099 ;;; Recognize z^n*d^(a*z^2+b*z+c)
2101 (defun m2-exp-type-5 (expr)
2102 (m2 expr
2103 '((mtimes)
2104 ((mexpt) (z varp) (n freevar))
2105 ((mexpt)
2106 (d freevar0)
2107 ((mplus)
2108 ((coeffpt) (a freevar) ((mexpt) (z varp) 2))
2109 ((coeffpt) (b freevar) (z varp))
2110 ((coeffpp) (c freevar)))))))
2112 ;;; Recognize z^n*(%e^(a*z^2+b*z+c))^u
2114 (defun m2-exp-type-5-1 (expr)
2115 (m2 expr
2116 '((mtimes)
2117 ((mexpt) (z varp) (n freevar0))
2118 ((mexpt)
2119 ((mexpt)
2121 ((mplus)
2122 ((coeffpp) (c freevar))
2123 ((coefft) (a freevar0) ((mexpt) (z varp) 2))
2124 ((coefft) (b freevar0) (z varp))))
2125 (u freevar)))))
2127 ;;; Recognize z^n*d^(a*sqrt(z)+b*z+c)
2129 (defun m2-exp-type-6 (expr)
2130 (m2 expr
2131 '((mtimes)
2132 ((mexpt) (z varp) (n freevar0))
2133 ((mexpt)
2134 (d freevar0)
2135 ((mplus)
2136 ((coefft) (a freevar0) ((mexpt) (z varp) ((rat) 1 2)))
2137 ((coefft) (b freevar0) (z varp))
2138 ((coeffpp) (c freevar)))))))
2140 ;;; Recognize z^n*(%e^(a*sqrt(z)+b*z+c))^u
2142 (defun m2-exp-type-6-1 (expr)
2143 (m2 expr
2144 '((mtimes)
2145 ((mexpt) (z varp) (n freevar0))
2146 ((mexpt)
2147 ((mexpt)
2149 ((mplus)
2150 ((coeffpp) (c freevar))
2151 ((coefft) (a freevar0) ((mexpt) (z varp) ((rat) 1 2)))
2152 ((coefft) (b freevar0) (z varp))))
2153 (u freevar)))))
2155 ;;; Recognize z^n*a^(b*z^r+e)*h^(c*z^r+g)
2157 (defun m2-exp-type-7 (expr)
2158 (m2 expr
2159 '((mtimes)
2160 ((mexpt) (z varp) (n freevar))
2161 ((mexpt)
2162 (a freevar0)
2163 ((mplus)
2164 ((coefft)
2165 (b freevar0)
2166 ((mexpt) (z varp) (r freevar0)))
2167 ((coeffpp) (e freevar))))
2168 ((mexpt)
2169 (h freevar0)
2170 ((mplus)
2171 ((coefft)
2172 (c freevar0)
2173 ((mexpt) (z varp) (r1 freevar0)))
2174 ((coeffpp) (g freevar)))))))
2176 ;;; Recognize z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u
2178 (defun m2-exp-type-7-1 (expr)
2179 (m2 expr
2180 '((mtimes)
2181 ((mexpt) (z varp) (v freevar))
2182 ((mexpt)
2183 ((mexpt)
2185 ((mplus)
2186 ((coeffpp) (e freevar))
2187 ((coefft) (b freevar0) ((mexpt) (z varp) (r freevar0)))))
2188 (q freevar))
2189 ((mexpt)
2190 ((mexpt)
2192 ((mplus)
2193 ((coeffpp) (g freevar))
2194 ((coefft) (c freevar0) ((mexpt) (z varp) (r1 freevar0)))))
2195 (u freevar)))))
2197 ;;; Recognize a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)
2199 (defun m2-exp-type-8 (expr)
2200 (m2 expr
2201 '((mtimes)
2202 ((mexpt)
2203 (a freevar0)
2204 ((mplus)
2205 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2206 ((coeffpt) (d freevar) (z varp))
2207 ((coeffpp) (e freevar))))
2208 ((mexpt)
2209 (h freevar0)
2210 ((mplus)
2211 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2212 ((coeffpt) (f freevar) (z varp))
2213 ((coeffpp) (g freevar)))))))
2215 ;;; Recognize (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v
2217 (defun m2-exp-type-8-1 (expr)
2218 (m2 expr
2219 '((mtimes)
2220 ((mexpt)
2221 ((mexpt)
2223 ((mplus)
2224 ((coeffpp) (e freevar))
2225 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2226 ((coeffpt) (d freevar) (z varp))))
2227 (u freevar))
2228 ((mexpt)
2229 ((mexpt)
2231 ((mplus)
2232 ((coeffpp) (g freevar))
2233 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2234 ((coeffpt) (f freevar) (z varp))))
2235 (v freevar)))))
2237 ;;; Recognize (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v
2239 (defun m2-exp-type-8-2 (expr)
2240 (m2 expr
2241 '((mtimes)
2242 ((mexpt)
2243 ((mexpt)
2245 ((mplus)
2246 ((coeffpp) (e freevar))
2247 ((coefft) (b freevar) ((mexpt) (z varp) (r freevar0)))))
2248 (u freevar))
2249 ((mexpt)
2250 ((mexpt)
2252 ((mplus)
2253 ((coeffpp) (g freevar))
2254 ((coefft) (c freevar) ((mexpt) (z varp) (r1 freevar0)))))
2255 (v freevar)))))
2257 ;;; Recognize z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)
2259 (defun m2-exp-type-9 (expr)
2260 (m2 expr
2261 '((mtimes)
2262 ((mexpt) (z varp) (n freevar))
2263 ((mexpt)
2264 (a freevar0)
2265 ((mplus)
2266 ((coeffpt) (b freevar) ((mexpt) (z varp) 2))
2267 ((coeffpt) (d freevar) (z varp))
2268 ((coeffpp) (e freevar))))
2269 ((mexpt)
2270 (h freevar0)
2271 ((mplus)
2272 ((coeffpt) (c freevar) ((mexpt) (z varp) 2))
2273 ((coeffpt) (f freevar) (z varp))
2274 ((coeffpp) (g freevar)))))))
2276 ;;; Recognize z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u
2278 (defun m2-exp-type-9-1 (expr)
2279 (m2 expr
2280 '((mtimes)
2281 ((mexpt) (z varp) (n freevar))
2282 ((mexpt)
2283 ((mexpt)
2285 ((mplus)
2286 ((coeffpp) (e freevar))
2287 ((coeffpt) (b freevar) ((mexpt) (z varp) 2))
2288 ((coeffpt) (d freevar) (z varp))))
2289 (q freevar))
2290 ((mexpt)
2291 ((mexpt)
2293 ((mplus)
2294 ((coeffpp) (g freevar))
2295 ((coeffpt) (c freevar) ((mexpt) (z varp) 2))
2296 ((coeffpt) (f freevar) (z varp))))
2297 (u freevar)))))
2299 ;;; Recognize z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z+)f*z+g)
2301 (defun m2-exp-type-10 (expr)
2302 (m2 expr
2303 '((mtimes)
2304 ((mexpt) (z varp) (n freevar))
2305 ((mexpt)
2306 (a freevar0)
2307 ((mplus)
2308 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2309 ((coeffpt) (d freevar) (z varp))
2310 ((coeffpp) (e freevar))))
2311 ((mexpt)
2312 (h freevar0)
2313 ((mplus)
2314 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2315 ((coeffpt) (f freevar) (z varp))
2316 ((coeffpp) (g freevar)))))))
2318 ;;; Recognize z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u
2320 (defun m2-exp-type-10-1 (expr)
2321 (m2 expr
2322 '((mtimes)
2323 ((mexpt) (z varp) (n freevar))
2324 ((mexpt)
2325 ((mexpt)
2327 ((mplus)
2328 ((coeffpp) (e freevar))
2329 ((coeffpt) (b freevar) ((mexpt) (z varp) ((rat) 1 2)))
2330 ((coeffpt) (d freevar) (z varp))))
2331 (q freevar))
2332 ((mexpt)
2333 ((mexpt)
2335 ((mplus)
2336 ((coeffpp) (g freevar))
2337 ((coeffpt) (c freevar) ((mexpt) (z varp) ((rat) 1 2)))
2338 ((coeffpt) (f freevar) (z varp))))
2339 (u freevar)))))
2341 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2343 (defun integrate-exp-special (expr var &aux w const)
2345 ;; First factor the expression.
2346 (setq expr ($factor expr))
2348 ;; Remove constant factors.
2349 (setq w (partition expr var 1))
2350 (setq const (car w))
2351 (setq expr (cdr w))
2353 (schatchen-cond w
2354 ((m2-exp-type-1a (facsum-exponent expr))
2355 (a c d r p v)
2356 (when *debug-integrate*
2357 (format t "~&Type 1a: (a^(c*(z^r)^p+d)^v : w = ~A~%" w))
2359 (mul -1
2360 const
2361 ;; 1/(p*r*(a^(c*v*(var^r)^p)))
2362 (inv (mul p r (power a (mul c v (power (power var r) p)))))
2364 ;; (a^(d+c*(var^r)^p))^v
2365 (power (power a (add d (mul c (power (power var r) p)))) v)
2366 ;; gamma_incomplete(1/(p*r), -c*v*(var^r)^p*log(a))
2367 (take '(%gamma_incomplete)
2368 (inv (mul p r))
2369 (mul -1 c v (power (power var r) p) (take '(%log) a)))
2370 ;; (-c*v*(var^r)^p*log(a))^(-1/(p*r))
2371 (power (mul -1 c v (power (power var r) p) (take '(%log) a))
2372 (div -1 (mul p r)))))
2374 ((m2-exp-type-2 (facsum-exponent expr))
2375 (a b d v r)
2377 (when *debug-integrate*
2378 (format t "~&Type 2: z^v*a^(b*z^r+d) : w = ~A~%" w))
2380 (mul
2381 const
2382 (div -1 r)
2383 (power a d)
2384 (power var (add v 1))
2385 ($gamma_incomplete
2386 (div (add v 1) r)
2387 (mul -1 b (power var r) ($log a)))
2388 (power
2389 (mul -1 b (power var r) ($log a))
2390 (mul -1 (div (add v 1) r)))))
2392 ((m2-exp-type-2-1 (facsum-exponent expr))
2393 (a b v r u)
2394 (when *debug-integrate*
2395 (format t "~&Type 2-1: z^v*(%e^(a*z^r+b))^u : w = ~A~%" w))
2397 (mul const
2399 (inv r)
2400 (power '$%e (mul -1 a u (power var r)))
2401 (power (power '$%e (add (mul a (power var r)) b)) u)
2402 (power var (add v 1))
2403 (power (mul -1 a u (power var r)) (div (mul -1 (add v 1)) r))
2404 (take '(%gamma_incomplete)
2405 (div (add v 1) r)
2406 (mul -1 a u (power var r)))))
2408 ((m2-exp-type-3 (facsum-exponent expr))
2409 (a b c d p)
2410 (when *debug-integrate*
2411 (format t "~&Type 3: (a*z+b)^p*%e^(c*z+d) : w = ~A~%" w))
2412 (mul
2413 const
2414 (div -1 a)
2415 (power '$%e (sub d (div (mul b c) a)))
2416 (power (add b (mul a var)) (add p 1))
2417 ($expintegral_e (mul -1 p) (mul (div -1 a) c (add b (mul a var))))))
2419 ((m2-exp-type-4 expr)
2420 (a b c d)
2421 (let (($trigsign nil)) ; Do not simplify erfc(-x) !
2422 (when *debug-integrate*
2423 (format t "~&Type 4: d^(a*z^2+b/z^2+c) : w = ~A~%" w))
2425 (mul
2426 const
2427 (div 1 (mul 4 (power (mul -1 a ($log d)) (div 1 2))))
2428 (mul
2429 (power d c)
2430 (power '$%pi (div 1 2))
2431 (power '$%e
2432 (mul -2
2433 (power (mul -1 a ($log d)) (div 1 2))
2434 (power (mul -1 b ($log d)) (div 1 2))))
2435 (add
2436 ($erfc
2437 (add
2438 (div (power (mul -1 b ($log d)) (div 1 2)) var)
2439 (mul -1 var (power (mul -1 a ($log d)) (div 1 2)))))
2440 (mul -1
2441 (power '$%e
2442 (mul 4
2443 (power (mul -1 a ($log d)) (div 1 2))
2444 (power (mul -1 b ($log d)) (div 1 2))))
2445 ($erfc
2446 (add
2447 (mul var (power (mul -1 a ($log d)) (div 1 2)))
2448 (div (power (mul -1 b ($log d)) (div 1 2)) var)))))))))
2450 ((and (m2-exp-type-4-1 expr)
2451 (poseven (cdras 'n w)) ; only for n a positive, even integer
2452 (symbolp (cdras 'a w))) ; a has to be a symbol
2453 (a b c d n)
2454 (let (($trigsign nil)) ; Do not simplify erfc(-x) !
2456 (when *debug-integrate*
2457 (format t "~&Type 4-1: z^(2*n)*d^(a*z^2+b/z^2+c) : w = ~A~%" w))
2459 (setq n (div n 2))
2461 (mul const
2462 (div 1 4)
2463 (power d c)
2464 (power '$%pi (div 1 2))
2465 (simplify (list '(%derivative)
2466 (div
2467 (sub
2468 (mul
2469 (power ($log d) (mul -1 n))
2470 (add
2471 (mul
2472 (power
2473 '$%e
2474 (mul -2
2475 (power (mul -1 a ($log d)) (div 1 2))
2476 (power (mul -1 b ($log d)) (div 1 2))))
2477 ($erfc
2478 (sub
2479 (div
2480 (power (mul -1 b ($log d)) (div 1 2))
2481 var)
2482 (mul var (power (mul -1 ($log d)) (div 1 2))))))))
2483 (mul
2484 (power
2485 '$%e
2486 (mul 2
2487 (power (mul -1 a ($log d)) (div 1 2))
2488 (power (mul -1 b ($log d)) (div 1 2))))
2489 ($erfc
2490 (add
2491 (power (mul -1 a ($log d)) (div 1 2))
2492 (div (power (mul -1 b ($log d)) (div 1 2)) var)))))
2493 (power (mul -1 a ($log d)) (div 1 2)))
2494 a n)))))
2496 ((and (m2-exp-type-5 (facsum-exponent expr))
2497 (maxima-integerp (cdras 'n w))
2498 (eq ($sign (cdras 'n w)) '$pos))
2499 (a b c d n)
2501 (when *debug-integrate*
2502 (format t "~&Type 5: z^n*d^(a*z^2+b*z+c) : w = ~A~%" w))
2504 (mul
2505 const
2506 (div -1 (mul 2 (power (mul a ($log d)) (div 1 2))))
2507 (mul
2508 (power d (sub c (div (mul b b) (mul 4 a))))
2509 (let ((index (gensumindex))
2510 ($simpsum t))
2511 (mfuncall '$sum
2512 (mul
2513 (power 2 (sub index n))
2514 ($binomial n index)
2515 ($gamma_incomplete
2516 (div (add index 1) 2)
2517 (mul
2518 (div -1 (mul 4 a))
2519 (power (add b (mul 2 a var)) 2)
2520 ($log d)))
2521 (power (mul a ($log d)) (mul -1 (add n (div 1 2))))
2522 (power (mul -1 b ($log d)) (sub n index))
2523 (power (mul (add b (mul 2 a var)) ($log d)) (add index 1))
2524 (power
2525 (mul (div -1 a) (power (add b (mul 2 a var)) 2) ($log d))
2526 (mul (div -1 2) (add index 1))))
2527 index 0 n)))))
2529 ((and (m2-exp-type-5-1 (facsum-exponent expr))
2530 (maxima-integerp (cdras 'n w))
2531 (eq ($sign (cdras 'n w)) '$pos))
2532 (a b c u n)
2533 (when *debug-integrate*
2534 (format t "~&Type 5-1: z^n*(%e^(a*z^2+b*z+c))^u : w = ~A~%" w))
2536 (mul const
2537 (div -1 2)
2538 (power '$%e
2539 (add (mul -1 (div (mul b b u) (mul 4 a)))
2540 (mul -1 u (add (mul a var var) (mul b var)))))
2541 (power a (mul -1 (add n 1)))
2542 (power (power '$%e
2543 (add (mul a var var) (mul b var) c))
2545 (let ((index (gensumindex))
2546 ($simpsum t))
2547 (dosum
2548 (mul (power 2 (sub index n))
2549 (power (mul -1 b) (sub n index))
2550 (power (add b (mul 2 a var)) (add index 1))
2551 (power (div (mul -1 u (power (add b (mul 2 a var)) 2)) a)
2552 (mul (div -1 2) (add index 1)))
2553 (take '(%binomial) n index)
2554 (take '(%gamma_incomplete)
2555 (div (add index 1) 2)
2556 (div (mul -1 u (power (add b (mul 2 a var)) 2))
2557 (mul 4 a))))
2558 index 0 n t))))
2560 ((and (m2-exp-type-6 (facsum-exponent expr))
2561 (maxima-integerp (cdras 'n w))
2562 (eq ($sign (cdras 'n w)) '$pos))
2563 (a b c d n)
2564 (when *debug-integrate*
2565 (format t "~&Type 6: z^n*d^(a*sqrt(z)+b*z+c) : w = ~A~%" w))
2567 (mul
2568 const
2569 (power 2 (mul -1 (add n 1)))
2570 (power d (sub c (div (mul a a) (mul 4 b))))
2571 (power (mul b ($log d)) (mul -2 (add n 1)))
2572 (let ((index1 (gensumindex))
2573 (index2 (gensumindex))
2574 ($simpsum t))
2575 (mfuncall '$sum
2576 (mfuncall '$sum
2577 (mul
2578 (power -1 (sub index1 index2))
2579 (power 4 index1)
2580 ($binomial index1 index2)
2581 ($binomial n index1)
2582 ($log d)
2583 (power (mul a ($log d)) (sub (mul 2 n) (add index1 index2)))
2584 (power
2585 (mul (add a (mul 2 b (power var (div 1 2)))) ($log d))
2586 (add index1 index2))
2587 (power
2588 (mul
2589 (div -1 b)
2590 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2591 ($log d))
2592 (mul (div -1 2) (add index1 index2 1)))
2593 (add
2594 (mul 2 b
2595 (power
2596 (mul
2597 (div -1 b)
2598 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2599 ($log d))
2600 (div 1 2))
2601 ($gamma_incomplete
2602 (div (add index1 index2 2) 2)
2603 (mul
2604 (div -1 (mul 4 b))
2605 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2606 ($log d))))
2607 (mul a
2608 (add a (mul 2 b (power var (div 1 2))))
2609 ($log d)
2610 ($gamma_incomplete
2611 (div (add index1 index2 1) 2)
2612 (mul
2613 (div -1 (mul 4 b))
2614 (power (add a (mul 2 b (power var (div 1 2)))) 2)
2615 ($log d))))))
2616 index2 0 index1)
2617 index1 0 n))))
2619 ((and (m2-exp-type-6-1 (facsum-exponent expr))
2620 (maxima-integerp (cdras 'n w))
2621 (eq ($sign (cdras 'n w)) '$pos))
2622 (a b c u n)
2623 (when *debug-integrate*
2624 (format t "~&Type 6-1: z^n*(%e^(a*sqrt(z)+b*z+c))^u : w = ~A~%" w))
2626 (mul const
2627 (power 2 (mul -1 (add (mul 2 n) 1)))
2628 (power '$%e
2629 (add (div (mul -1 u a a) (mul 4 b))
2630 (mul u (add (mul a (power var (div 1 2)))
2631 (mul b var)
2632 c))))
2633 (power b (mul -2 (add n 1)))
2634 (power (power '$%e
2635 (add (mul a (power var (div 1 2)))
2636 (mul b var)))
2638 (let ((index1 (gensumindex))
2639 (index2 (gensumindex))
2640 ($simpsum t))
2641 (dosum
2642 (dosum
2643 (mul (power -1 (sub index1 index2))
2644 (power 4 index1)
2645 (power a (add (neg index2) (neg index1) (mul 2 n)))
2646 (power (add a (mul 2 b (power var (div 1 2))))
2647 (add index1 index2))
2648 (power (div (mul -1 u
2649 (power (add a
2650 (mul 2
2652 (power var (div 1 2))))
2655 (mul (div -1 2) (add index1 index2 1)))
2656 (take '(%binomial) index1 index2)
2657 (take '(%binomial) n index1)
2658 (add (mul a
2659 (add a (mul 2 b (power var (div 1 2))))
2660 (take '(%gamma_incomplete)
2661 (div (add index1 index2 1) 2)
2662 (div (mul -1 u
2663 (power (add a
2664 (mul 2 b
2665 (power var
2666 (div 1 2))))
2668 (mul 4 b))))
2669 (mul (inv u)
2670 (power (div (mul -1 u
2671 (power (add a
2672 (mul 2 b
2673 (power var
2674 (div 1 2))))
2677 (div 1 2))
2678 (mul 2 b)
2679 (take '(%gamma_incomplete)
2680 (div (add index1 index2 2) 2)
2681 (div (mul -1 u
2682 (power (add a
2683 (mul 2 b
2684 (power var (div 1 2))))
2686 (mul 4 b))))))
2687 index2 0 index1 t)
2688 index1 0 n t))))
2690 ((and (m2-exp-type-7 (facsum-exponent expr))
2691 (eq ($sign (sub (cdras 'r w) (cdras 'r1 w))) '$zero))
2692 (a b c e g h r n)
2693 (when *debug-integrate*
2694 (format t "~&Type 7: z^n*a^(b*z^r+e)*h^(c*z^r+g) : w = ~A~%" w))
2696 (setq n (add n 1))
2698 (mul
2699 const
2700 (power var n)
2701 (div -1 r)
2702 (power a e)
2703 (power h g)
2704 (power
2705 (mul -1
2706 (power var r)
2707 (add (mul b ($log a)) (mul c ($log h))))
2708 (div (mul -1 n) r))
2709 ($gamma_incomplete
2710 (div n r)
2711 (mul -1 (power var r) (add (mul b ($log a)) (mul c ($log h)))))))
2713 ((and (m2-exp-type-7-1 (facsum-exponent expr))
2714 (eq ($sign (sub (cdras 'r w) (cdras 'r1 w))) '$zero))
2715 (b c e g r v q u)
2716 (when *debug-integrate*
2717 (format t "~&Type 7-1: z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u : w = ~A~%" w))
2719 (mul const
2720 (div -1 r)
2721 (power '$%e (mul -1 (power var r) (add (mul b q) (mul c u))))
2722 (power (power '$%e (add e (mul b (power var r)))) q)
2723 (power (power '$%e (add g (mul c (power var r)))) u)
2724 (power var (add v 1))
2725 (power (mul -1 (power var r) (add (mul b q) (mul c u)))
2726 (div (mul -1 (add v 1)) r))
2727 (take '(%gamma_incomplete)
2728 (div (add v 1) r)
2729 (mul -1 (power var r) (add (mul b q) (mul c u))))))
2731 ((m2-exp-type-8 (facsum-exponent expr))
2732 (a b c d e f g h)
2733 (when *debug-integrate*
2734 (format t "~&Type 8: a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2735 (format t "~& : w = ~A~%" w))
2737 (mul
2738 const
2739 (div 1 2)
2740 (power a e)
2741 (power h g)
2742 (add
2743 (mul 2
2744 (power a (add (mul b (power var (div 1 2))) (mul d var)))
2745 (power h (add (mul c (power var (div 1 2))) (mul f var)))
2746 (div 1 (add (mul d ($log a)) (mul f ($log h)))))
2747 (mul -1
2748 (power '$%pi (div 1 2))
2749 (power '$%e
2750 (mul -1
2751 (div
2752 (power (add (mul b ($log a)) (mul c ($log h))) 2)
2753 (mul 4 (add (mul d ($log a)) (mul f ($log h)))))))
2754 ($erfi
2755 (div
2756 (add
2757 (mul b ($log a))
2758 (mul c ($log h))
2759 (mul 2
2760 (power var (div 1 2))
2761 (add (mul d ($log a)) (mul f ($log h)))))
2762 (mul 2
2763 (power (add (mul d ($log a)) (mul f ($log h))) (div 1 2)))))
2764 (add (mul b ($log a)) (mul c ($log h)))
2765 (power (add (mul d ($log a)) (mul f ($log h))) (div -3 2))))))
2767 ((m2-exp-type-8-1 (facsum-exponent expr))
2768 (b c d e f g u v)
2769 (when *debug-integrate*
2770 (format t "~&Type 8-1: (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v")
2771 (format t "~& : w = ~A~%" w))
2773 (mul const
2774 (div 1 2)
2775 (power (add (mul d u) (mul f v)) (div -3 2))
2776 (mul (power '$%e
2777 (mul -1
2778 (power (add (mul b u)
2779 (mul 2 d u (power var (div 1 2)))
2780 (mul v (add c (mul 2 f (power var (div 1 2))))))
2782 (inv (mul 4 (add (mul d u) (mul f v))))))
2783 (power (power '$%e
2784 (add (mul b (power var (div 1 2)))
2786 (mul d var)))
2788 (power (power '$%e
2789 (add (mul c (power var (div 1 2)))
2791 (mul f var)))
2793 (add (mul 2
2794 (power '$%e
2795 (mul (power (add (mul b u)
2796 (mul 2 d u (power var (div 1 2)))
2797 (mul v (add c (mul 2 f (power var (div 1 2))))))
2799 (inv (mul 4 (add (mul d u) (mul f v))))))
2800 (power (add (mul d u) (mul f v)) (div 1 2)))
2801 (mul -1
2802 (power '$%pi (div 1 2))
2803 (add (mul b u) (mul c v))
2804 (take '(%erfi)
2805 (div (add (mul b u)
2806 (mul 2 d u (power var (div 1 2)))
2807 (mul c v)
2808 (mul 2 f v (power var (div 1 2))))
2809 (mul 2
2810 (power (add (mul d u) (mul f v))
2811 (div 1 2))))))))))
2813 ((and (m2-exp-type-8-2 (facsum-exponent expr))
2814 (eq ($sign (sub (cdras 'r w) (cdras 'r1 w))) '$zero))
2815 (b c e g r u v)
2816 (when *debug-integrate*
2817 (format t "~&Type 8-2: (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v")
2818 (format t "~& : w = ~A~%" w))
2820 (mul const
2822 (inv r)
2823 (power '$%e
2824 (mul -1
2825 (power var r)
2826 (add (mul b u) (mul c v))))
2827 (power (power '$%e
2828 (add (power var r) e))
2830 (power (power '$%e
2831 (add (power var r) g))
2834 (power (mul -1
2835 (power var r)
2836 (add (mul b u) (mul c v)))
2837 (div -1 r))
2838 (take '(%gamma_incomplete)
2839 (div 1 r)
2840 (mul -1 (power var r) (add (mul b u) (mul c v))))))
2842 ((and (m2-exp-type-9 (facsum-exponent expr))
2843 (maxima-integerp (cdras 'n w))
2844 (eq ($sign (cdras 'n w)) '$pos)
2845 (or (not (eq ($sign (cdras 'b w)) '$zero))
2846 (not (eq ($sign (cdras 'c w)) '$zero))))
2847 (a b c d e f g h n)
2848 (when *debug-integrate*
2849 (format t "~&Type 9: z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)")
2850 (format t "~& : w = ~A~%" w))
2852 (mul
2853 const
2854 (div -1 2)
2855 (power a e)
2856 (power h g)
2857 (power '$%e
2858 (div
2859 (power (add (mul d ($log a)) (mul f ($log h))) 2)
2860 (mul -4 (add (mul b ($log a)) (mul c ($log h))))))
2861 (power (add (mul b ($log a)) (mul c ($log h))) (mul -1 (add n 1)))
2862 (let ((index (gensumindex))
2863 ($simpsum t))
2864 (mfuncall '$sum
2865 (mul
2866 (power 2 (sub index n))
2867 ($binomial n index)
2868 (power
2869 (add (mul -1 d ($log a)) (mul -1 f ($log h)))
2870 (sub n index))
2871 (power
2872 (add
2873 (mul (add d (mul 2 b var)) ($log a))
2874 (mul (add f (mul 2 c var)) ($log h)))
2875 (add index 1))
2876 (power
2877 (mul -1
2878 (div
2879 (power
2880 (add
2881 (mul (add d (mul 2 b var)) ($log a))
2882 (mul (add f (mul 2 c var)) ($log h)))
2884 (add (mul b ($log a)) (mul c ($log h)))))
2885 (div (add index 1) -2))
2886 ($gamma_incomplete
2887 (div (add index 1) 2)
2888 (mul -1
2889 (div
2890 (power
2891 (add
2892 (mul (add d (mul 2 b var)) ($log a))
2893 (mul (add f (mul 2 c var)) ($log h)))
2895 (mul 4 (add (mul b ($log a)) (mul c ($log h))))))))
2896 index 0 n))))
2898 ((and (m2-exp-type-9-1 (facsum-exponent expr))
2899 (maxima-integerp (cdras 'n w))
2900 (eq ($sign (cdras 'n w)) '$pos)
2901 (or (not (eq ($sign (cdras 'b w)) '$zero))
2902 (not (eq ($sign (cdras 'c w)) '$zero))))
2903 (b c d e f g q u n)
2904 (when *debug-integrate*
2905 (format t "~&Type 9-1: z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u")
2906 (format t "~& : w = ~A~%" w))
2908 (mul const
2909 (div -1 2)
2910 (power (add (mul b q) (mul c u)) (div -1 2))
2911 (power '$%e
2912 (add (div (power (add (mul d q) (mul f u)) 2)
2913 (mul -4 (add (mul b q) (mul c u))))
2914 (mul -1 var
2915 (add (mul d q)
2916 (mul b q var)
2917 (mul f u)
2918 (mul c u var)))))
2919 (power (power '$%e
2920 (add e
2921 (mul var (add d (mul b var)))))
2923 (power (power '$%e
2924 (add g
2925 (mul var (add f (mul c var)))))
2927 (let ((index (gensumindex))
2928 ($simpsum t))
2929 (dosum
2930 (mul (power 2 (sub index n))
2931 (power (add (mul b q) (mul c u)) (neg (add n (div 1 2))))
2932 (power (add (neg (mul d q)) (neg (mul f u)))
2933 (sub n index))
2934 (power (add (mul d q)
2935 (mul f u)
2936 (mul 2 var (add (mul b q) (mul c u))))
2937 (add index 1))
2938 (power (div (power (add (mul d q)
2939 (mul f u)
2940 (mul 2
2941 (add (mul b q)
2942 (mul c u))
2943 var))
2945 (neg (add (mul b q) (mul c u))))
2946 (mul (div -1 2) (add index 1)))
2947 (take '(%binomial) n index)
2948 (take '(%gamma_incomplete)
2949 (div (add index 1) 2)
2950 (div (power (add (mul d q)
2951 (mul f u)
2952 (mul 2
2953 (add (mul b q)
2954 (mul c u))
2955 var))
2957 (mul -4 (add (mul b q) (mul c u))))))
2958 index 0 n t))))
2960 ((and (m2-exp-type-10 (facsum-exponent expr))
2961 (maxima-integerp (cdras 'n w))
2962 (eq ($sign (cdras 'n w)) '$pos)
2963 (or (not (eq ($sign (cdras 'b w)) '$zero))
2964 (not (eq ($sign (cdras 'c w)) '$zero))))
2965 (a b c d e f g h n)
2966 (when *debug-integrate*
2967 (format t "~&Type 10: z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2968 (format t "~& : w = ~A~%" w))
2970 (mul const
2971 (power 2 (add (mul -2 n) -1))
2972 (power a e)
2973 (power h g)
2974 (power '$%e
2975 (div (power (add (mul b ($log a)) (mul c ($log h))) 2)
2976 (mul -4 (add (mul d ($log a)) (mul f ($log h))))))
2977 (power (add (mul d ($log a)) (mul f ($log h))) (mul -2 (add n 1)))
2978 (let ((index1 (gensumindex))
2979 (index2 (gensumindex))
2980 ($simpsum t))
2981 (dosum
2982 (dosum
2983 (mul (power -1 (sub index1 index2))
2984 (power 4 index1)
2985 ($binomial index1 index2)
2986 ($binomial n index1)
2987 (power (add (mul b ($log a)) (mul c ($log h)))
2988 (sub (mul 2 n) (add index1 index2)))
2989 (power (add (mul b ($log a))
2990 (mul c ($log h))
2991 (mul 2
2992 (power var (div 1 2))
2993 (add (mul d ($log a)) (mul f ($log h)))))
2994 (add index1 index2))
2995 (power (mul -1
2996 (div (power (add (mul b ($log a))
2997 (mul c ($log h))
2998 (mul 2
2999 (power var (div 1 2))
3000 (add (mul d ($log a))
3001 (mul f ($log h)))))
3003 (add (mul d ($log a)) (mul f ($log h)))))
3004 (mul (div -1 2) (add index1 index2 1)))
3005 (add (mul ($gamma_incomplete (mul (div 1 2)
3006 (add index1 index2 1))
3007 (mul (div -1 4)
3008 (div (power (add (mul b ($log a))
3009 (mul c ($log h))
3010 (mul 2
3011 (power var (div 1 2))
3012 (add (mul d ($log a)) (mul f ($log h)))))
3014 (add (mul d ($log a)) (mul f ($log h))))))
3015 (add (mul b ($log a)) (mul c ($log h)))
3016 (add (mul b ($log a))
3017 (mul c ($log h))
3018 (mul 2
3019 (power var (div 1 2))
3020 (add (mul d ($log a)) (mul f ($log h))))))
3021 (mul 2
3022 ($gamma_incomplete (mul (div 1 2)
3023 (add index1 index2 2))
3024 (mul (div -1 4)
3025 (div (power (add (mul b ($log a))
3026 (mul c ($log h))
3027 (mul 2
3028 (power var (div 1 2))
3029 (add (mul d ($log a))
3030 (mul f ($log h)))))
3032 (add (mul d ($log a))
3033 (mul f ($log h))))))
3034 (add (mul d ($log a)) (mul f ($log h)))
3035 (power (mul -1
3036 (div (power (add (mul b ($log a))
3037 (mul c ($log h))
3038 (mul 2
3039 (power var (div 1 2))
3040 (add (mul d ($log a))
3041 (mul f ($log h)))))
3043 (add (mul d ($log a))
3044 (mul f ($log h)))))
3045 (div 1 2)))))
3046 index2 0 index1 t)
3047 index1 0 n t))))
3049 ((and (m2-exp-type-10-1 (facsum-exponent expr))
3050 (maxima-integerp (cdras 'n w))
3051 (eq ($sign (cdras 'n w)) '$pos)
3052 (or (not (eq ($sign (cdras 'b w)) '$zero))
3053 (not (eq ($sign (cdras 'c w)) '$zero))))
3054 (b c d e f g q u n)
3055 (let ((bq+cu (add (mul b q) (mul c u)))
3056 (dq+fu (add (mul d q) (mul f u))))
3057 (when *debug-integrate*
3058 (format t "~&Type 10-1: z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u")
3059 (format t "~& : w = ~A~%" w))
3061 (mul const
3062 (power 2 (mul -1 (add (mul 2 n) 1)))
3063 (power '$%e
3064 (add (div (mul -1 (power bq+cu 2)) (mul 4 dq+fu))
3065 (mul -1 d var q)
3066 (mul -1 b (power var (div 1 2)) q)
3067 (mul -1 f var u)
3068 (mul -1 c (power var (div 1 2)) u)))
3069 (power (power '$%e
3070 (add (mul b (power var (div 1 2)))
3071 (mul d var)
3074 (power (power '$%e
3075 (add (mul c (power var (div 1 2)))
3076 (mul f var)
3079 (power dq+fu (mul -2 (add n 1)))
3080 (let ((index1 (gensumindex))
3081 (index2 (gensumindex))
3082 ($simpsum t))
3083 (dosum
3084 (dosum
3085 (mul (power -1 (sub index1 index2))
3086 (power 4 index1)
3087 (power bq+cu
3088 (add (neg index1) (neg index2) (mul 2 n)))
3089 (power (add bq+cu
3090 (mul 2 (power var (div 1 2)) dq+fu))
3091 (add index1 index2))
3092 (power (div (power (add bq+cu
3093 (mul 2
3094 (power var (div 1 2))
3095 dq+fu))
3097 (mul -1 dq+fu))
3098 (mul (div -1 2)
3099 (add index1 index2 1)))
3100 (take '(%binomial) index1 index2)
3101 (take '(%binomial) n index1)
3102 (add (mul bq+cu
3103 (add bq+cu
3104 (mul 2
3105 (power var (div 1 2))
3106 dq+fu))
3107 (take '(%gamma_incomplete)
3108 (mul (div 1 2)
3109 (add index1 index2 1))
3110 (div (power (add (mul b q)
3111 (mul c u)
3112 (mul 2
3113 (power var (div 1 2))
3114 dq+fu))
3116 (mul -4
3117 dq+fu))))
3118 (mul 2
3119 (power (div (power (add bq+cu
3120 (mul 2
3121 (power var (div 1 2))
3122 dq+fu))
3124 (mul 1 dq+fu))
3125 (div 1 2))
3126 dq+fu
3127 (take '(%gamma_incomplete)
3128 (mul (div 1 2)
3129 (add index1 index2 2))
3130 (div (power (add bq+cu
3131 (mul 2
3132 (power var (div 1 2))
3133 dq+fu))
3135 (mul -4
3136 dq+fu))))))
3137 index2 0 index1 t)
3138 index1 0 n t)))))))
3140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3142 ;;; Do a facsum for the exponent of power functions.
3143 ;;; This is necessary to integrate all general forms. The pattern matcher is
3144 ;;; not powerful enough to do the job.
3146 (defun facsum-exponent (expr)
3147 ;; Make sure that expr has the form ((mtimes) factor1 factor2 ...)
3148 (when (not (mtimesp expr)) (setq expr (list '(mtimes) expr)))
3149 (do ((result nil)
3150 (l (cdr expr) (cdr l)))
3151 ((null l) (cons (list 'mtimes) result))
3152 (cond
3153 ((mexptp (car l))
3154 ;; Found an power function. Factor the exponent with facsum.
3155 (let* ((fac (mfuncall '$facsum (caddr (car l)) var))
3156 (num ($num fac))
3157 (den ($denom fac)))
3158 (setq result
3159 (cons (cons (list 'mexpt)
3160 (cons (cadr (car l))
3161 (if (equal 1 den)
3162 (list num)
3163 (list ($multthru (inv den) num)))))
3164 result))))
3166 ;; Nothing to do.
3167 (setq result (cons (car l) result))))))
3169 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;