1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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.
18 ;;;; Unfortunately, some important pages in the scan are all black.
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
)
35 `(get ,frob
'operators
))
37 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;;; Predicate functions
41 (declaim (inline varp
))
46 "Returns 2*x if 2*x is an integer, else nil"
47 (integerp2 (mul2* 2 x
)))
50 "Returns x if x is an integer, else false"
52 (cond ((not (numberp x
)) nil
)
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.
60 (cond ((or (varp ex
) (freevar ex
))
62 ((member (caar ex
) '(mplus mtimes
) :test
#'eq
)
63 (do ((u (cdr ex
) (cdr u
)))
65 (if (not (rat8 (car u
)))
67 ((not (eq (caar ex
) 'mexpt
))
69 ((integerp (caddr ex
))
81 (t (every #'elem
(cdr a
)))))
84 (cond ((atom a
) (not (eq a var
)))
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)
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
)
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
)))
126 ;; Method 9: Rational function times a log or arctric function
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
)))
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
)))
144 ((coefftt) (d elem
))))))
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
159 (list (maxima-substitute
160 `((mquotient) ((mplus) ((mexpt) $%e
,newvar
)
165 `((mquotient) ((mexpt) $%e
,newvar
) ,b
)
166 (maxima-substitute newvar expres d
))
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~%"))
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
)))
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
)))
199 (monstertrig exp var
(cadr expres
))))))
201 ((and (eq (caar expres
) '%derivative
)
202 (eq (caar exp
) (caar expres
))
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
)))))
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
235 (ratroot exp var
(cadr expres
) w
))
238 ;; Method 4: Binomial - Chebyschev
239 ((not (integerp1 (caddr expres
))) ; 2*exponent not integer
240 (cond ((m2-chebyform exp
)
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)
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.
254 ;; Method 4: Binomial - Chebyschev
259 ;; Substitute the expanded factor into the integrand and try again.
260 ((not (m2 (setq w
($expand
(cadr expres
)))
263 (setq exp
(maxima-substitute w
(cadr expres
) exp
))
264 (intform (simplify (list '(mexpt) w
(caddr 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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
284 (cond ((arcfuncp ex
) (setq arcpart ex coef
1))
285 ((and (consp ex
) (eq (caar ex
) 'mtimes
))
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
)))))
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)
315 ((coefftt) (e freevar
))
317 ((coeffpt) (a freevar
) (var varp
))
318 ((coeffpt) (b freevar
)))
321 ((coeffpt) (c freevar
) (var varp
))
322 ((coeffpt) (d freevar
)))
325 ;; This is for matching the pattern a*x^r1*(c1+c2*x^q)^r2.
326 (defun m2-chebyform (expr)
329 ((mexpt) (var varp
) (r1 numberp
))
333 ((coefftt) (c2 freevar
))
334 ((mexpt) (var varp
) (q free1
)))
335 ((coeffpp) (c1 freevar
)))
337 ((coefftt) (a freevar
)))))
339 ;; Pattern to match b*x + a
340 (defun m2-b*x
+a
(expr)
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)
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)
359 ((coeffpt) (a freevar
) (var varp
))
360 ((coeffpt) (b freevar
)))
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))
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.
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
))
395 (maxima-substitute (take '(%atan
) (div (cadr w
) (caddr w
)))
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
)
419 ;; We're looking at each term of the product and check if we can
420 ;; apply one of the special methods.
424 (format t
"car y =~%")
425 (maxima-display (car y
)))
426 (cond ((rat8 (car y
))
428 (format t
"In loop, 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.
435 (not (isinop w
'%integrate
)))
437 (format t
"In loop, case intform~%")
438 (return (mul2* const w
)))
441 (format t
"In loop, go special~%")
442 ;; Store a possible partial result
448 ;; Method 8: Rational functions
449 (return (mul2* const
(cond ((setq y
(powerlist exp var
)) y
)
450 (t (ratint exp var
)))))))
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
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
)))
469 (cond ((alike1 (car stacklist
) coef
)
471 (not (isinop (setq w
(let ((stack (cons coef stack
)))
472 (integrator coef var
)))
474 (setq integrand
(mul2 w
(sdiff arcpart var
)))
475 (do ((stacklist stack
(cdr stacklist
)))
477 (cond ((alike1 (car stacklist
) integrand
)
480 (setq y
(let ((stack (cons integrand stack
))
482 (integrator integ var
)))
484 (return (add* (list '(mtimes) const w arcpart
)
485 (list '(mtimes) -
1 const y
))))
489 (cond ((setq y
(scep exp var
))
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
)))))
509 (format t
"exp = ~A~%" exp
)
511 (format t
"y = ~A~%" y
)
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.
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.
536 (list '(%integrate
) exp var
))))))))))
539 (member x
'(%sin %cos %sec %tan %csc %cot
) :test
#'eq
))
541 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
))
555 (return (addn (cons (rischint (cons '(mplus) terms
) var
) (cdr ans
))
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
))
563 (if (linearp (cadr fac
) var
) (push fac trigl
)
567 (linearp (caddr fac
) var
))
568 ;; should be only one exponential factor
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
)))
579 (abs-val (add (power e-coef
2) (power sc-coef
2))))
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
604 `((%derivative
), expr
;partial in old-wrt
605 ,.
(nreverse old-wrt
)))
606 `((%derivative
) ,expr
;Partial, return rest
609 `((%derivative
) ,expr
;Higher order, reduce order
611 ,(car wrt
) ,(add* (cadr wrt
) -
1)
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
)
620 ((eq (caar exp
) 'mqapply
)
621 ;; Transform to functional form and try again.
623 ;; ((MQAPPLY SIMP) (($PSI SIMP ARRAY) 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
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
))
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
)
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
)
730 (cond ((freevar ex
) t
)
732 ((eq (caar ex
) 'mexpt
)
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
)
744 (integrator ex var
)))
747 (cond ((ratnump x
) (caddr x
))
748 ((not (numberp x
)) nil
)
750 (t (cdr (maxima-rationalize x
)))))
752 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
755 ;;; Implementation of Method 1: Elementary function of exponentials
757 ;;; The following examples are integrated with this method:
759 ;;; integrate(exp(x)/(2+3*exp(2*x)),x)
760 ;;; integrate(exp(x+1)/(1+exp(x)),x)
761 ;;; integrate(10^x*exp(x),x)
763 (let ((base nil
) ; The common base.
764 (pow nil
) ; The common power of the form b*x+a. The values are
765 ; stored in a list which is returned from m2.
766 (exptflag nil
)) ; When T, the substitution is not possible.
768 (defun superexpt (exp var bas1 pow1
)
769 (prog (y ($logabs nil
) (new-var (gensym "NEW-VAR-")))
770 (putprop new-var t
'internal
)
774 ;; Transform the integrand. At this point resimplify, because it is not
775 ;; guaranteed, that a correct simplified expression is returned.
776 ;; Use a new variable to prevent facts on the old variable to be wrongly used.
777 (setq y
(resimplify (maxima-substitute new-var var
(elemxpt exp
))))
778 (when exptflag
(return nil
))
779 ;; Integrate the transformed integrand and substitute back.
782 (substint (list '(mexpt) base
783 (list '(mplus) (cdras 'a pow
)
784 (list '(mtimes) (cdras 'b pow
) var
)))
789 (take '(%log
) base
))) new-var
))))))
791 ;; Transform expressions like g^(b*x+a) to the common base base and
792 ;; do the substitution y = base^(b*x+a) in the expr.
793 (defun elemxpt (expr &aux w
)
794 (cond ((freevar expr
) expr
)
795 ;; var is the base of a subexpression. The transformation fails.
796 ((atom expr
) (setq exptflag t
))
797 ((not (eq (caar expr
) 'mexpt
))
799 (mapcar #'(lambda (c) (elemxpt c
)) (cdr expr
))))
800 ((not (freevar (cadr expr
)))
802 (elemxpt (cadr expr
))
803 (elemxpt (caddr expr
))))
804 ;; Transform the expression to the common base.
805 ((not (eq (cadr expr
) base
))
806 (elemxpt (list '(mexpt)
808 (mul (power (take '(%log
) base
) -
1)
809 (take '(%log
) (cadr expr
))
811 ;; The exponent must be linear in the variable of integration.
812 ((not (setq w
(m2-b*x
+a
(caddr expr
))))
813 (list (car expr
) base
(elemxpt (caddr expr
))))
814 ;; Do the substitution y = g^(b*x+a).
816 (setq w
(cons (cons 'bb
(cdras 'b pow
)) w
))
817 (setq w
(cons (cons 'aa
(cdras 'a pow
)) w
))
818 (setq w
(cons (cons 'base base
) w
))
819 (subliss w
'((mtimes)
824 ((mtimes) -
1 aa b
) bb
))
827 ((mquotient) b bb
)))))))
830 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
833 ;;; Implementation of Method 3:
834 ;;; Substitution for a rational root of a linear fraction of x
836 ;;; This method is applicable when the integrand is of the form:
839 ;;; [ a x + b n1/m1 a x + b n1/m2
840 ;;; I R(x, (-------) , (-------) , ...) dx
841 ;;; ] c x + d c x + d
846 ;;; (1) t = ((a*x+b)/(c*x+d))^(1/k), or
848 ;;; (2) x = (b-d*t^k)/(c*t^k-a)
850 ;;; where k is the least common multiplier of m1, m2, ... and
852 ;;; (3) dx = k*(a*d-b*c)*t^(k-1)/(a-c*t^k)^2 * dt
854 ;;; First, the algorithm calls the routine RAT3 to collect the roots of the
855 ;;; form ((a*x+b)/(c*x+d))^(n/m) in the list *ROOTLIST*.
856 ;;; search for the least common multiplier of m1, m2, ... then the
857 ;;; substitutions (2) and (3) are done and the new problem is integrated.
858 ;;; As always, W is an alist which associates to the coefficients
859 ;;; a, b... (and to VAR) their values.
861 (defvar *ratroot
* nil
) ; Expression of the form (a*x+b)/(c*x+d)
862 (defvar *rootlist
* nil
) ; List of powers of the expression *ratroot*.
864 (defun ratroot (exp var
*ratroot
* w
)
865 (prog (*rootlist
* k y w1
)
866 ;; Check if the integrand has a chebyform, if so return the result.
867 (when (setq y
(chebyf exp var
)) (return y
))
868 ;; Check if the integrand has a suitably form and collect the roots
869 ;; in the global special variable *ROOTLIST*.
870 (unless (rat3 exp t
) (return nil
))
871 ;; Get the least common multiplier of m1, m2, ...
872 (setq k
(apply #'lcm
*rootlist
*))
873 (setq w1
(cons (cons 'k k
) w
))
874 ;; Substitute for the roots.
879 ((mplus) ((mtimes) b e
)
880 ((mtimes) -
1 d
((mexpt) var k
)))
881 ((mplus) ((mtimes) c
((mexpt) var k
))
884 ;; Integrate the new problem.
893 ((mexpt) var
((mplus) -
1 k
)))
896 ((mexpt) var
((mplus) -
1 k
))))))
898 ((mtimes) c
((mexpt) var k
))
902 ;; Substitute back and return the result.
903 (return (substint (power *ratroot
* (power k -
1)) var y
))))
906 (cond ((freevar ex
) t
)
908 ((member (caar ex
) '(mtimes mplus
) :test
#'eq
)
909 (do ((u (cdr ex
) (cdr u
)))
911 (if (not (rat3 (car u
) ind
))
913 ((not (eq (caar ex
) 'mexpt
))
914 (rat3 (car (margs ex
)) t
))
917 ((integerp (caddr ex
))
918 (rat3 (cadr ex
) ind
))
919 ((and (m2 (cadr ex
) *ratroot
*)
920 (denomfind (caddr ex
)))
921 (setq *rootlist
* (cons (denomfind (caddr ex
)) *rootlist
*)))
922 (t (rat3 (cadr ex
) nil
))))
924 (let ((rootform nil
) ; Expression of the form x = (b*e-d*t^k)/(c*t^k-e*a).
925 (rootvar nil
)) ; The variable we substitute for the root.
928 (cond ((freevar ex
) ex
)
930 ((not (eq (caar ex
) 'mexpt
))
931 (mapcar #'(lambda (u) (subst4 u
)) ex
))
932 ((m2 (cadr ex
) *ratroot
*)
933 (list (car ex
) rootvar
(integerp2 (timesk k
(caddr ex
)))))
934 (t (list (car ex
) (subst4 (cadr ex
)) (subst4 (caddr ex
))))))
936 (defun subst41 (exp a b
)
939 ;; At this point resimplify, because it is not guaranteed, that a correct
940 ;; simplified expression is returned.
941 (resimplify (subst4 exp
)))
944 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
947 ;;; Implementation of Method 4: Binomial Chebyschev
949 ;; exp = a*t^r1*(c1+c2*t^q)^r2, where var = t.
951 ;; G&S 2.202 has says this integral can be expressed by elementary
954 ;; 1. q is an integer
955 ;; 2. (r1+1)/q is an integer
956 ;; 3. (r1+1)/q+r2 is an integer.
958 ;; I (rtoy) think that for this code to work, r1, r2, and q must be numbers.
959 (defun chebyf (exp var
)
960 (prog (r1 r2 d1 d2 n1 n2 w q
)
961 ;; Return NIL if the expression doesn't match.
962 (when (not (setq w
(m2-chebyform exp
)))
965 (format t
"w = ~A~%" w
)
966 (when (zerop1 (cdr (assoc 'c1 w
:test
#'eq
)))
967 ;; rtoy: Is it really possible to be in this routine with c1 =
971 ;; This factor is locally constant as long as t and
972 ;; c2*t^q avoid log's branch cut.
973 (subliss w
'((mtimes) a
((mexpt) var
((mtimes) -
1 q r2
))
974 ((mexpt) ((mtimes) c2
((mexpt) var q
)) r2
)))
976 (subliss w
'((mexpt) var
((mplus) r1
((mtimes) q r2
)))) var
))))
977 (setq q
(cdr (assoc 'q w
:test
#'eq
)))
978 ;; Reset parameters. a = a/q, r1 = (1 - q + r1)/q
980 (list* (cons 'a
(div* (cdr (assoc 'a w
:test
#'eq
)) q
))
983 (div* (addn (list 1 (neg (simplify q
)) (cdr (assoc 'r1 w
:test
#'eq
))) nil
) q
))
986 (format t
"new w = ~A~%" w
)
987 (setq r1
(cdr (assoc 'r1 w
:test
#'eq
))
988 r2
(cdr (assoc 'r2 w
:test
#'eq
)))
991 (format t
"new r1 = ~A~%" r1
)
992 (format t
"r2 = ~A~%" r2
))
993 ;; Write r1 = d1/n1, r2 = d2/n2, if possible. Update w with
994 ;; these values, if so. If we can't, give up. I (rtoy) think
995 ;; this only happens if r1 or r2 can't be expressed as rational
996 ;; numbers. Hence, r1 and r2 have to be numbers, not variables.
998 ((not (and (setq d1
(denomfind r1
))
999 (setq d2
(denomfind r2
))
1000 (setq n1
(integerp2 (timesk r1 d1
)))
1001 (setq n2
(integerp2 (timesk r2 d2
)))
1002 (setq w
(list* (cons 'd1 d1
) (cons 'd2 d2
)
1003 (cons 'n1 n1
) (cons 'n2 n2
)
1007 (format t
"cheby can't find one of d1,d2,n1,n2:~%")
1008 (format t
" d1 = ~A~%" d1
)
1009 (format t
" d2 = ~A~%" d2
)
1010 (format t
" n1 = ~A~%" n1
)
1011 (format t
" n2 = ~A~%" n2
))
1013 ((and (integerp2 r1
) (> r1
0))
1014 #+nil
(format t
"integer r1 > 0~%")
1015 ;; (r1+q-1)/q is positive integer.
1017 ;; I (rtoy) think we are using the substitution z=(c1+c2*t^q).
1018 ;; Maxima thinks the resulting integral should then be
1020 ;; a/q*c2^(-r1/q-1/q)*integrate(z^r2*(z-c1)^(r1/q+1/q-1),z)
1024 (subliss w
'((mplus) c1
((mtimes) c2
((mexpt) var q
))))
1027 (expands (list (subliss w
1028 ;; a*t^r2*c2^(-r1-1)
1039 (expandexpt (subliss w
1046 #+nil
(format t
"integer r2~%")
1047 ;; I (rtoy) think this is using the substitution z = t^(q/d1).
1049 ;; The integral (as maxima will tell us) becomes
1051 ;; a*d1/q*integrate(z^(n1/q+d1/q-1)*(c1+c2*z^d1)^r2,z)
1053 ;; But be careful because the variable A in the code is
1056 (substint (subliss w
'((mexpt) var
((mquotient) q d1
)))
1058 (ratint (simplify (subliss w
1074 ((and (integerp2 r1
) (< r1
0))
1075 #+nil
(format t
"integer r1 < 0~%")
1076 ;; I (rtoy) think this is using the substitution
1078 ;; z = (c1+c2*t^q)^(1/d2)
1080 ;; With this substitution, maxima says the resulting integral
1083 ;; a/q*c2^(-r1/q-1/q)*d2*
1084 ;; integrate(z^(n2+d2-1)*(z^d2-c1)^(r1/q+1/q-1),z)
1086 (substint (subliss w
1087 ;; (c1+c2*t^q)^(1/d2)
1091 ((mtimes) c2
((mexpt) var q
)))
1092 ((mquotient) 1 d2
)))
1094 (ratint (simplify (subliss w
1095 ;; This is essentially
1096 ;; the integrand above,
1097 ;; except A and R1 here
1098 ;; are not the same as
1119 ((integerp2 (add* r1 r2
))
1120 #+nil
(format t
"integer r1+r2~%")
1121 ;; If we're here, (r1-q+1)/q+r2 is an integer.
1123 ;; I (rtoy) think this is using the substitution
1125 ;; z = ((c1+c2*t^q)/t^q)^(1/d1)
1127 ;; With this substitution, maxima says the resulting integral
1130 ;; a*d2/q*c1^(r2+r1/q+1/q)*
1131 ;; integrate(z^(d2*r2+d2-1)*(z^d2-c2)^(-r2-r1/q-1/q-1),z)
1133 (substint (let (($radexpand
'$all
))
1134 ;; Setting $radexpand to $all here gets rid of
1135 ;; ABS in the subtitution. I think that's ok in
1136 ;; this case. See Bug 1654183.
1142 ((mtimes) c2
((mexpt) var q
)))
1144 ((mquotient) 1 d1
))))
1146 (ratint (simplify (subliss w
1169 (t (return (list '(%integrate
) exp var
))))))
1171 (defun greaterratp (x1 x2
)
1172 (cond ((and (numberp x1
) (numberp x2
))
1175 (greaterratp (quotient (float (cadr x1
))
1180 (quotient (float (cadr x2
))
1184 (member (car x
) '(%sin %cos
) :test
#'eq
))
1186 (defun supertrig (exp)
1187 (declare (special *notsame
* *trigarg
*))
1188 (cond ((freevar exp
) t
)
1190 ((member (caar exp
) '(mplus mtimes
) :test
#'eq
)
1191 (and (supertrig (cadr exp
))
1192 (or (null (cddr exp
))
1193 (supertrig (cons (car exp
)
1195 ((eq (caar exp
) 'mexpt
)
1196 (and (supertrig (cadr exp
))
1197 (supertrig (caddr exp
))))
1198 ((eq (caar exp
) '%log
)
1199 (supertrig (cadr exp
)))
1201 '(%sin %cos %tan %sec %cot %csc
) :test
#'eq
)
1202 (cond ((m2 (cadr exp
) *trigarg
*) t
)
1203 ((m2-b*x
+a
(cadr exp
))
1204 (and (setq *notsame
* t
) nil
))
1205 (t (supertrig (cadr exp
)))))
1206 (t (supertrig (cadr exp
)))))
1208 (defun subst2s (ex pat
)
1209 (cond ((null ex
) nil
)
1212 (t (cons (subst2s (car ex
) pat
)
1213 (subst2s (cdr ex
) pat
)))))
1215 ;; Match (c*x+b), where c and b are free of x
1216 (defun simple-trig-arg (exp)
1217 (m2 exp
'((mplus) ((mtimes)
1218 ((coefftt) (c freevar
))
1219 ((coefftt) (v varp
)))
1220 ((coeffpp) (b freevar
)))))
1222 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1225 ;;; Implementation of Method 6: Elementary function of trigonometric functions
1227 (defun monstertrig (exp var
*trigarg
*)
1228 (declare (special *trigarg
*))
1229 (when (and (not (atom *trigarg
*))
1230 ;; Do not exute the following code when called from rischint.
1232 (let ((arg (simple-trig-arg *trigarg
*)))
1234 ;; We have trig(c*x+b). Use the substitution y=c*x+b to
1235 ;; try to compute the integral. Why? Because x*sin(n*x)
1236 ;; takes longer and longer as n gets larger and larger.
1237 ;; This is caused by the Risch integrator. This is a
1238 ;; work-around for this issue.
1239 (let* ((c (cdras 'c arg
))
1241 (new-var (gensym "NEW-VAR-"))
1242 (new-exp (maxima-substitute (div (sub new-var b
) c
)
1244 (putprop new-var t
'internal
)
1245 (if (every-trigarg-alike new-exp new-var
)
1246 ;; avoid endless recursion when more than one
1247 ;; trigarg exists or c is a float
1248 (return-from monstertrig
1252 (div (integrator new-exp new-var
) c
))))))
1254 (return-from monstertrig
(rischint exp var
))))))
1255 (prog (*notsame
* w a b y d
)
1256 (declare (special *notsame
*))
1258 ((supertrig exp
) (go a
))
1259 ((null *notsame
*) (return nil
))
1260 ;; Check for an expression like a*trig1(m*x)*trig2(n*x),
1261 ;; where trig1 and trig2 are sin or cos.
1262 ((not (setq y
(m2 exp
1264 ((coefftt) (a freevar
))
1268 ((coefftt) (m freevar
))))
1272 ((coefftt) (n freevar
))))))))
1274 ; This check has been done with the pattern match.
1275 ; ((not (and (member (car (setq b (cdr (assoc 'b y :test #'eq)))) '(%sin %cos) :test #'eq)
1276 ; (member (car (setq d (cdr (assoc 'd y :test #'eq)))) '(%sin %cos) :test #'eq)))
1279 ;; The tests after this depend on values of b and d being
1280 ;; set. Set them here unconditionally, before doing the
1282 (setq b
(cdras 'b y
))
1283 (setq d
(cdras 'd y
))
1284 (and (eq (car b
) '%sin
)
1285 (eq (car d
) '%sin
)))
1286 ;; We have a*sin(m*x)*sin(n*x).
1287 ;; The integral is: a*(sin((m-n)*x)/(2*(m-n))-sin((m+n)*x)/(2*(m+n))
1292 ((%sin
) ((mtimes) ((mplus) m
((mtimes) -
1 n
)) x
))
1293 ((mtimes) 2 ((mplus) m
((mtimes) -
1 n
))))
1296 ((%sin
) ((mtimes) ((mplus) m n
) x
))
1297 ((mtimes) 2 ((mplus) m n
)))))))))
1298 ((and (eq (car b
) '%cos
) (eq (car d
) '%cos
))
1299 ;; We have a*cos(m*x)*cos(n*x).
1300 ;; The integral is: a*(sin((m-n)*x)/(2*(m-n))+sin((m+n)*x)/(2*(m+n))
1305 ((%sin
) ((mtimes) ((mplus) m
((mtimes) -
1 n
)) x
))
1307 ((mplus) m
((mtimes) -
1 n
))))
1309 ((%sin
) ((mtimes) ((mplus) m n
) x
))
1310 ((mtimes) 2 ((mplus) m n
))))))))
1311 ((or (and (eq (car b
) '%cos
)
1312 ;; The following (destructively!) swaps the values of
1313 ;; m and n if first trig term is sin. I (rtoy) don't
1314 ;; understand why this is needed. The formula
1315 ;; doesn't depend on that.
1316 (setq w
(cdras 'm y
))
1317 (rplacd (assoc 'm y
) (cdras 'n y
))
1318 (rplacd (assoc 'n y
) w
))
1320 ;; We have a*cos(n*x)*sin(m*x).
1321 ;; The integral is: -a*(cos((m-n)*x)/(2*(m-n))+cos((m+n)*x)/(2*(m+n))
1326 ((%cos
) ((mtimes) ((mplus) m
((mtimes) -
1 n
)) x
))
1327 ((mtimes) 2 ((mplus) m
((mtimes) -
1 n
))))
1329 ((%cos
) ((mtimes) ((mplus) m n
) x
))
1330 ((mtimes) 2 ((mplus) m n
)))))))))
1331 b
;; At this point we have trig functions with different arguments,
1332 ;; but not a product of sin and cos.
1333 (cond ((not (setq y
(prog2
1334 (setq *trigarg
* var
)
1337 ((coefftt) (a freevar
))
1341 ((coefftt) (n integerp2
))))
1342 ((coefftt) (c supertrig
)))))))
1344 ;; We have a product of trig functions: trig1(n*x)*trig2(y).
1345 ;; trig1 is sin or cos, where n is a numerical integer. trig2 is not a sin
1346 ;; or cos. The cos or sin function is expanded.
1351 (cdras 'a y
) ; constant factor
1352 (cdras 'c y
) ; trig functions
1353 (cond ((eq (car (cdras 'b y
)) '%cos
) ; expand cos(n*x)
1354 (maxima-substitute var
1356 (supercosnx (cdras 'n y
))))
1357 (t ; expand sin(x*x)
1358 (maxima-substitute var
1360 (supersinx (cdras 'n y
)))))))
1362 a
;; A product of trig functions and all trig functions have the same
1363 ;; argument *trigarg*. Maxima substitutes *trigarg* with the variable var
1364 ;; of integration and calls trigint to integrate the new problem.
1365 (setq w
(subst2s exp
*trigarg
*))
1366 (setq b
(cdras 'b
(m2-b*x
+a
*trigarg
*)))
1367 (setq a
(substint *trigarg
* var
(trigint (div* w b
) var
)))
1368 (return (if (isinop a
'%integrate
)
1369 (list '(%integrate
) exp var
)
1373 (member (car x
) '(%sin %cos %tan %cot %sec %csc
) :test
#'eq
))
1375 (defun supersinx (n)
1376 (let ((i (if (< n
0) -
1 1)))
1377 ($expand
(list '(mtimes) i
(sinnx (timesk i n
))))))
1379 (defun supercosnx (n)
1380 ($expand
(cosnx (timesk (if (< n
0) -
1 1) n
))))
1386 (list '(mtimes) '((%sin
) x
) (cosnx (1- n
)))
1387 (list '(mtimes) '((%cos
) x
) (sinnx (1- n
))))))
1393 (list '(mtimes) '((%cos
) x
) (cosnx (1- n
)))
1394 (list '(mtimes) -
1 '((%sin
) x
) (sinnx (1- n
))))))
1397 (and (even x
) (> x -
1)))
1401 (not (member x
'(sin* cos
* sec
* tan
*) :test
#'eq
))
1402 (and (trigfree (car x
)) (trigfree (cdr x
)))))
1405 (prog (*b1
* *notsame
*)
1406 (declare (special *yy
* *b1
* *notsame
*))
1407 (when (and (numberp exp
) (zerop exp
))
1409 (setq *b1
* (subst *b
* 'b
'((mexpt) b
(n even
))))
1411 (setq *yy
* (rats exp
))
1412 (cond ((not *notsame
*) *yy
*))))))
1416 (declare (special *notsame
* *b1
*))
1418 (cond ((eq exp
*a
*) 'x
)
1420 (cond ((member exp
'(sin* cos
* sec
* tan
*) :test
#'eq
)
1423 ((setq y
(m2 exp
*b1
*))
1425 (t (cons (car exp
) (mapcar #'(lambda (g) (rats g
)) (cdr exp
))))))))
1428 (maxima-substitute *c
*
1430 (maxima-substitute (quotient (cdr (assoc 'n y
:test
#'eq
)) 2)
1441 (declare (special *yz
*))
1442 (cond ((not (numberp n
)) nil
)
1443 ((not (equal (rem n
2) 0))
1445 (maxima-substitute *c
*
1448 '((mplus) 1 ((mtimes) c
((mexpt) x
2)))
1449 (quotient (1- n
) 2)))))
1453 (maxima-substitute var
'x x
))
1455 (defun subvardlg (x)
1456 (mapcar #'(lambda (m)
1457 (cons (maxima-substitute var
'x
(car m
)) (cdr m
)))
1460 ;; This appears to be the implementation of Method 6, pp.82 in Moses' thesis.
1462 (defun trigint (exp var
)
1463 (prog (y repl y1 y2
*yy
* z m n
*c
* *yz
* *a
* *b
* )
1464 (declare (special *yy
* *yz
*))
1465 ;; Transform trig(x) into trig* (for simplicity?) Convert cot to
1466 ;; tan and csc to sin.
1468 (subliss (subvardlg '((((%sin
) x
) . sin
*)
1471 (((%cot
) x
) .
((mexpt) tan
* -
1))
1473 (((%csc
) x
) .
((mexpt) sin
* -
1))))
1476 (when *debug-integrate
*
1477 (format t
"~& in TRIGINT:~%")
1478 (format t
"~& : y2 = ~A~%" y2
))
1480 ;; Now transform tan to sin/cos and sec to 1/cos.
1481 (setq y1
(setq y
(subliss '((tan* .
((mtimes) sin
*
1483 (sec* .
((mexpt) cos
* -
1)))
1486 (when *debug-integrate
* (format t
"~& : y = ~A~%" y
))
1491 ((coefftt) (b trigfree
))
1492 ((mexpt) sin
* (m poseven
))
1493 ((mexpt) cos
* (n poseven
))))))
1494 ;; Go if y is not of the form sin^m*cos^n for positive even m and n.
1498 ;; Handle the case of sin^m*cos^n, m, n both non-negative and even.
1500 (setq m
(cdras 'm z
))
1501 (setq n
(cdras 'n z
))
1502 (setq *a
* (integerp2 (* 0.5 (if (< m n
) 1 -
1) (+ n
(* -
1 m
)))))
1503 (setq z
(cons (cons 'a
*a
*) z
))
1504 (setq z
(cons (cons 'x var
) z
))
1506 (when *debug-integrate
*
1507 (format t
"~& CASE III:~%")
1508 (format t
"~& : m, n = ~A ~A~%" m n
)
1509 (format t
"~& : a = ~A~%" *a
*)
1510 (format t
"~& : z = ~A~%" z
))
1512 ;; integrate(sin(y)^m*cos(y)^n,y) is transformed to the following form:
1514 ;; m < n: integrate((sin(2*y)/2)^n*(1/2+1/2*cos(2*y)^((n-m)/2),y)
1515 ;; m >= n: integrate((sin(2*y)/2)^n*(1/2-1/2*cos(2*y)^((m-n)/2),y)
1527 ((mtimes) ((rat simp
) 1 2) ((%sin
) x
))
1533 ((rat simp
) 1 2) ((%cos
) x
))) a
))))
1538 ((mtimes) ((rat simp
) 1 2) ((%sin
) x
))
1549 ;; I think this is case IV, working on the expression in terms of
1552 ;; Elem(x) means constants, x, trig functions of x, log and
1553 ;; inverse trig functions of x, and which are closed under
1554 ;; addition, multiplication, exponentiation, and substitution.
1556 ;; Elem(f(x)) is the same as Elem(x), but f(x) replaces x in the
1559 (when *debug-integrate
* (format t
"~& Case IV:~%"))
1564 (when (and (m2 y
'((coeffpt) (c rat1
) ((mexpt) cos
* (n odd1
))))
1565 (setq repl
(list '(%sin
) var
)))
1566 ;; The case cos^(2*n+1)*Elem(cos^2,sin). Use the substitution z = sin.
1570 (when (and (m2 y
'((coeffpt) (c rat1
) ((mexpt) sin
* (n odd1
))))
1571 (setq repl
(list '(%cos
) var
)))
1572 ;; The case sin^(2*n+1)*Elem(sin^2,cos). Use the substitution z = cos.
1576 ;; Transform sin and cos to tan and sec to see if the integral is
1577 ;; of the form Elem(tan, sec^2). If so, use the substitution z = tan.
1579 (when *debug-integrate
* (format t
"~& Case V:~%"))
1581 (setq y
(subliss '((sin* (mtimes) tan
* ((mexpt) sec
* -
1))
1582 (cos* (mexpt) sec
* -
1))
1587 (when (and (rat1 y
) (setq repl
(list '(%tan
) var
)))
1591 (when (and (m2 y
'((coeffpt) (c rat1
) ((mexpt) tan
* (n odd1
))))
1592 (setq repl
(list '(%sec
) var
)))
1594 (when (not (alike1 (setq repl
($expand exp
)) exp
))
1595 (return (integrator repl var
)))
1596 (setq y
(subliss '((sin* (mtimes) 2 x
1597 ((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1))
1599 ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2)))
1600 ((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1)))
1602 (setq y
(list '(mtimes)
1604 '((mtimes) 2 ((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1))))
1605 (setq repl
(subvar '((mquotient) ((%sin
) x
) ((mplus) 1 ((%cos
) x
)))))
1608 (setq y
(list '(mtimes) -
1 *yy
* *yz
*))
1611 (setq y
(list '(mtimes) '((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1) *yy
*))
1614 (setq y
(list '(mtimes) *yy
* *yz
*))
1616 (when *debug-integrate
*
1617 (format t
"~& Call the INTEGRATOR with:~%")
1618 (format t
"~& : y = ~A~%" y
)
1619 (format t
"~& : repl = ~A~%" repl
))
1620 ;; See Bug 2880797. We want atan(tan(x)) to simplify to x, so
1621 ;; set $triginverses to '$all.
1623 ;; Do not integrate for the global variable VAR, but substitute it.
1624 ;; This way possible assumptions on VAR are no longer present. The
1625 ;; algorithm of DEFINT depends on this behavior. See Bug 3085498.
1626 (let (($triginverses
'$all
) (newvar (gensym)))
1629 (integrator (maxima-substitute newvar
'x y
) newvar
))))))
1631 (defmvar $integration_constant_counter
0)
1632 (defmvar $integration_constant
'$%c
)
1634 ;; This is the top level of the integrator
1635 (defun sinint (exp var
)
1636 ;; *integrator-level* is a recursion counter for INTEGRATOR. See
1637 ;; INTEGRATOR for more details. Initialize it here.
1638 (let ((*integrator-level
* 0))
1639 (declare (special *integrator-level
*))
1641 ;; Sanity checks for variables
1643 (merror (intl:gettext
"integrate: variable must not be a number; found: ~:M") var
))
1644 (when ($ratp var
) (setf var
(ratdisrep var
)))
1645 (when ($ratp exp
) (setf exp
(ratdisrep exp
)))
1648 ;; Distribute over lists and matrices
1651 (mapcar #'(lambda (y) (sinint y var
)) (cdr exp
))))
1653 ;; The symbolic integration code doesn't really deal very well with
1654 ;; subscripted variables, so if we have one then replace occurrences of var
1655 ;; with an atomic gensym and recurse.
1656 ((and (not (atom var
))
1657 (member 'array
(cdar var
)))
1658 (let ((dummy-var (gensym)))
1659 (maxima-substitute var dummy-var
1660 (sinint (maxima-substitute dummy-var var exp
) dummy-var
))))
1662 ;; If exp is an equality, integrate both sides and add an integration
1665 (list (car exp
) (sinint (cadr exp
) var
)
1666 (add (sinint (caddr exp
) var
)
1667 ($concat $integration_constant
(incf $integration_constant_counter
)))))
1669 ;; If var is an atom which occurs as an operator in exp, then return a noun form.
1672 (list '(%integrate
) exp var
))
1674 ((zerop1 exp
) ;; special case because 0 will not pass sum-of-intsp test
1677 ((let ((ans (simplify
1678 (let ($opsubst varlist genvar stack
)
1679 (integrator exp var
)))))
1680 (if (sum-of-intsp ans
)
1681 (list '(%integrate
) exp var
)
1686 ;; This is a heuristic that SININT uses to work out whether the result from
1687 ;; INTEGRATOR is worth returning or whether just to return a noun form. If this
1688 ;; function returns T, then SININT will return a noun form.
1690 ;; The logic, as I understand it (Rupert 01/2014):
1692 ;; (1) If I integrate f(x) wrt x and get an atom other than x or 0, either
1693 ;; something's gone horribly wrong, or this is part of a larger
1694 ;; expression. In the latter case, we can return T here because hopefully
1695 ;; something else interesting will make the top-level return NIL.
1697 ;; (2) If I get a sum, return a noun form if every one of the summands is no
1698 ;; better than what I started with. (Presumably this is where the name
1701 ;; (3) If this is a noun form, it doesn't convey much information on its own,
1704 ;; (4) If this is a product, something interesting has probably happened. But
1705 ;; I still want a noun form if the result is like 2*'integrate(f(x),x), so
1706 ;; I'm only interested in terms in the product that are not free of
1707 ;; VAR. If one of those terms is worthy of report, that's great: return
1708 ;; NIL. Otherwise, return T if we saw at least two things (eg splitting an
1709 ;; integral into a product of two integrals)
1711 ;; (5) If the result is free of VAR, we're in a similar position to (1).
1713 ;; (6) Otherwise something interesting (and hopefully useful) has
1714 ;; happened. Return NIL to tell SININT to report it.
1715 (defun sum-of-intsp (ans)
1717 ;; Result of integration should never be a constant other than zero.
1718 ;; If the result of integration is zero, it is either because:
1719 ;; 1) a subroutine inside integration failed and returned nil,
1720 ;; and (mul 0 nil) yielded 0, meaning that the result is wrong, or
1721 ;; 2) the original integrand was actually zero - this is handled
1722 ;; with a separate special case in sinint
1724 ((mplusp ans
) (every #'sum-of-intsp
(cdr ans
)))
1725 ((eq (caar ans
) '%integrate
) t
)
1727 (let ((int-factors 0))
1728 (not (or (dolist (factor (cdr ans
))
1729 (unless (freeof var factor
)
1730 (if (sum-of-intsp factor
)
1733 (<= 2 int-factors
)))))
1734 ((freeof var ans
) t
)
1737 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1740 ;;; Implementation of Method 2: Integrate a summation
1742 (defun intsum (form var
)
1743 (prog (exp idx ll ul pair val
)
1744 (setq exp
(cadr form
)
1747 ul
(car (cddddr form
)))
1748 (if (or (not (atom var
))
1749 (not (free idx var
))
1751 (not (free ul var
)))
1752 (return (list '(%integrate
) form var
)))
1753 (setq pair
(partition exp var
1))
1754 (when (and (mexptp (cdr pair
))
1755 (eq (caddr pair
) var
))
1756 (setq val
(maxima-substitute ll idx
(cadddr pair
)))
1757 (cond ((equal val -
1)
1758 (return (add (integrator (maxima-substitute ll idx exp
) var
)
1759 (intsum1 exp idx
(add 1 ll
) ul var
))))
1761 (return (list '(%integrate
) form var
)))))
1762 (return (intsum1 exp idx ll ul var
))))
1764 (defun intsum1 (exp idx ll ul var
)
1765 (assume (list '(mgeqp) idx ll
))
1766 (if (not (eq ul
'$inf
))
1767 (assume (list '(mgeqp) ul idx
)))
1768 (simplifya (list '(%sum
) (integrator exp var
) idx ll ul
) t
))
1772 (member x
'(%log %integrate %atan
) :test
#'eq
)
1773 (or (finds (car x
)) (finds (cdr x
)))))
1775 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1778 ;;; Implementation of Method 9:
1779 ;;; Rational function times a log or arctric function
1781 ;;; ratlog is called for an expression containing a log or arctrig function
1782 ;;; The integrand is like log(x)*f'(x). To obtain the result the technique of
1783 ;;; partial integration is applied: log(x)*f(x)-integrate(1/x*f(x),x)
1785 (defun ratlog (exp var form
)
1788 (setq b
(cdr (assoc 'b y
:test
#'eq
)))
1789 (setq c
(cdr (assoc 'c y
:test
#'eq
)))
1790 (setq y
(integrator c var
))
1791 (when (finds y
) (return nil
))
1792 (setq d
(sdiff (cdr (assoc 'a form
:test
#'eq
)) var
))
1794 (setq z
(integrator (mul2* y d
) var
))
1795 (setq d
(cdr (assoc 'a form
:test
#'eq
)))
1796 (return (simplify (list '(mplus)
1797 (list '(mtimes) y d
)
1798 (list '(mtimes) -
1 z
))))))
1800 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1802 ;;; partial-integration is an extension of the algorithm of ratlog to support
1803 ;;; the technique of partial integration for more cases. The integrand
1804 ;;; is like g(x)*f'(x) and the result is g(x)*f(x)-integrate(g'(x)*f(x),x).
1806 ;;; Adding integrals properties for elementary functions led to infinite recursion
1807 ;;; with integrate(z*expintegral_shi(z),z). This was resolved by limiting the
1808 ;;; recursion depth. *integrator-level* needs to be at least 3 to solve
1809 ;;; o integrate(expintegral_ei(1/sqrt(x)),x)
1810 ;;; o integrate(sqrt(z)*expintegral_li(z),z)
1811 ;;; while a value of 4 causes testsuite regressions with
1812 ;;; o integrate(z*expintegral_shi(z),z)
1813 (defun partial-integration (form var
)
1814 (declare (special *integrator-level
*))
1815 (let ((g (cdr (assoc 'a form
))) ; part g(x)
1816 (df (cdr (assoc 'c form
))) ; part f'(x)
1818 (setq f
(integrator df var
)) ; integrate f'(x) wrt var
1820 ((or (isinop f
'%integrate
) ; no result or
1821 (isinop f
(caar g
)) ; g in result
1822 (> *integrator-level
* 3))
1823 nil
) ; we return nil
1825 ;; Build the result: g(x)*f(x)-integrate(g'(x)*f(x))
1827 (mul -
1 (integrator (mul f
(sdiff g var
)) var
)))))))
1829 ;; returns t if argument of every trig operation in y matches arg
1830 (defun every-trigarg-alike (y arg
)
1832 ((optrig (caar y
)) (alike1 arg
(cadr y
)))
1833 (t (every (lambda (exp)
1834 (every-trigarg-alike exp arg
))
1837 ;; return argument of first trig operation encountered in y
1838 (defun find-first-trigarg (y)
1839 (cond ((atom y
) nil
)
1840 ((optrig (caar y
)) (cadr y
))
1841 (t (some (lambda (exp)
1842 (find-first-trigarg exp
))
1845 ;; return constant factor that makes elements of alist match elements of blist
1846 ;; or nil if no match found
1847 ;; (we could replace this using rat package to divide alist and blist)
1848 (defun matchsum (alist blist
)
1850 (setq s
(m2 (car alist
) ;; find coeff for first term of alist
1852 ((coefftt) (a freevar
))
1853 ((coefftt) (c true
)))))
1854 (setq *c
* (cdr (assoc 'c s
:test
#'eq
)))
1855 (cond ((not (setq r
;; find coeff for first term of blist
1858 (cons '((coefftt) (b free1
))
1859 (cond ((mtimesp *c
*)
1861 (t (list *c
*))))))))
1863 (setq *d
* (simplify (list '(mtimes)
1868 (cond ((m2 (cons '(mplus) alist
) ;; check that all terms match
1869 (timesloop *d
* blist
))
1873 (defun timesloop (a b
)
1874 (cons '(mplus) (mapcar #'(lambda (c) (mul2* a c
)) b
)))
1876 (defun expands (aa b
)
1877 (addn (mapcar #'(lambda (c) (timesloop c aa
)) b
) nil
))
1879 (defun powerlist (exp var
)
1880 (prog (y *c
* *d
* powerlist
*b
*)
1883 ((mexpt) (var varp
) (c integerp2
))
1884 ((coefftt) (a freevar
))
1885 ((coefftt) (b true
)))))
1886 (setq *b
* (cdr (assoc 'b y
:test
#'eq
)))
1887 (setq *c
* (cdr (assoc 'c y
:test
#'eq
)))
1888 (unless (rat10 *b
*) (return nil
))
1889 (setq *d
* (apply #'gcd
(cons (1+ *c
*) powerlist
)))
1890 (when (or (eql 1 *d
*) (zerop *d
*)) (return nil
))
1893 (list '(mexpt) var
*d
*)
1895 (integrate5 (simplify (list '(mtimes)
1897 (cdr (assoc 'a y
:test
#'eq
))
1898 (list '(mexpt) var
(1- (quotient (1+ *c
*) *d
*)))
1902 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1905 ;;; Implementation of Method 3: Derivative-divides algorithm
1907 ;; This is the derivative-divides algorithm of Moses.
1911 ;; Look for form I c * op(u(x)) * u'(x) dx
1915 ;; where: c is a constant
1916 ;; u(x) is an elementary expression in x
1917 ;; u'(x) is its derivative
1918 ;; op is an elementary operator:
1919 ;; - the indentity, or
1920 ;; - any function that can be integrated by INTEGRALLOOKUPS
1922 ;; The method of solution, once the problem has been determined to
1923 ;; posses the form above, is to look up OP in a table and substitute
1924 ;; u(x) for each occurrence of x in the expression given in the table.
1925 ;; In other words, the method performs an implicit substitution y = u(x),
1926 ;; and obtains the integral of op(y)dy by a table look up.
1928 (defun diffdiv (exp var
)
1929 (prog (y *a
* x v
*d
* z w r
)
1930 (cond ((and (mexptp exp
)
1932 (integerp (caddr exp
))
1935 (return (integrator (expandexpt (cadr exp
) (caddr exp
)) var
))))
1937 ;; If not a product, transform to a product with one term
1938 (setq exp
(cond ((mtimesp exp
) exp
) (t (list '(mtimes) exp
))))
1940 ;; Loop over the terms in exp
1944 ;; This m2 pattern matches const*(exp/y)
1945 (setq r
(list '(mplus)
1948 (remove y
(cdr exp
) :count
1)))))
1950 ;; Case u(var) is the identity function. y is a term in exp.
1951 ;; Match if diff(y,var) == c*(exp/y).
1952 ;; This even works when y is a function with multiple args.
1953 ((setq w
(m2 (sdiff y var
) r
))
1954 (return (muln (list y y
(power* (mul2* 2 (cdr (assoc 'c w
:test
#'eq
))) -
1)) nil
))))
1956 ;; w is the arg in y.
1957 (let ((arg-freevar))
1960 ((or (atom y
) (member (caar y
) '(mplus mtimes
) :test
#'eq
)) y
)
1961 ;; Take the argument of a function with one value.
1962 ((= (length (cdr y
)) 1) (cadr y
))
1963 ;; A function has multiple args, and exactly one arg depends on var
1964 ((= (count-if #'null
(setq arg-freevar
(mapcar #'freevar
(cdr y
)))) 1)
1965 (do ((args (cdr y
) (cdr args
))
1966 (argf arg-freevar
(cdr argf
)))
1967 ((if (not (car argf
)) (return (car args
))))))
1971 ((setq w
(cond ((and (setq x
(sdiff w var
))
1973 (setq *d
* (remove y
(cdr exp
) :count
1))
1977 (cond ((setq *d
* (matchsum (cdr x
) (cdr v
)))
1978 (list (cons 'c
*d
*)))
1981 (return (cond ((null (setq x
(integrallookups y
))) nil
)
1983 (t (mul2* x
(power* (cdr (assoc 'c w
:test
#'eq
)) -
1)))))))
1985 (when (null z
) (return nil
))
1988 (defun subliss (alist expr
)
1989 "Alist is an alist consisting of a variable (symbol) and its value. expr is
1990 an expression. For each entry in alist, substitute the corresponding
1994 (setq x
(maxima-substitute (cdr a
) (car a
) x
)))))
1996 (defun substint (x y expres
)
1997 (if (and (not (atom expres
)) (eq (caar expres
) '%integrate
))
1998 (list (car expres
) exp var
)
1999 (substint1 (maxima-substitute x y expres
))))
2001 (defun substint1 (exp)
2002 (cond ((atom exp
) exp
)
2003 ((and (eq (caar exp
) '%integrate
)
2005 (not (symbolp (caddr exp
)))
2006 (not (free (caddr exp
) var
)))
2007 (simplify (list '(%integrate
)
2008 (mul2 (cadr exp
) (sdiff (caddr exp
) var
))
2010 (t (recur-apply #'substint1 exp
))))
2012 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2014 ;:; Extension of the integrator for more integrals with power functions
2016 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2018 ;;; Recognize (a^(c*(z^r)^p+d)^v
2020 (defun m2-exp-type-1a (expr)
2026 ;; The order of the pattern is critical. If we change it,
2027 ;; we do not get the expected match.
2028 ((coeffpp) (d freevar
))
2029 ((coefft) (c freevar0
)
2031 ((mexpt) (z varp
) (r freevar0
))
2035 ;;; Recognize z^v*a^(b*z^r+d)
2037 (defun m2-exp-type-2 (expr)
2040 ((mexpt) (z varp
) (v freevar0
))
2044 ((coeffpp) (d freevar
))
2045 ((coefft) (b freevar0
) ((mexpt) (z varp
) (r freevar0
))))))))
2047 ;;; Recognize z^v*%e^(a*z^r+b)^u
2049 (defun m2-exp-type-2-1 (expr)
2052 ((mexpt) (z varp
) (v freevar0
))
2057 ((coeffpp) (b freevar
))
2058 ((coefft) (a freevar0
) ((mexpt) (z varp
) (r freevar0
)))))
2061 ;;; Recognize (a*z+b)^p*%e^(c*z+d)
2063 (defun m2-exp-type-3 (expr)
2068 ((coefft) (a freevar0
) (z varp
))
2069 ((coeffpp) (b freevar
)))
2074 ((coefft) (c freevar0
) (z varp
))
2075 ((coeffpp) (d freevar
)))))))
2077 ;;; Recognize d^(a*z^2+b/z^2+c)
2079 (defun m2-exp-type-4 (expr)
2084 ((coefft) (a freevar0
) ((mexpt) (z varp
) 2))
2085 ((coefft) (b freevar0
) ((mexpt) (z varp
) -
2))
2086 ((coeffpp) (c freevar
))))))
2088 ;;; Recognize z^(2*n)*d^(a*z^2+b/z^2+c)
2090 (defun m2-exp-type-4-1 (expr)
2093 ((mexpt) (z varp
) (n freevar0
))
2097 ((coefft) (a freevar0
) ((mexpt) (z varp
) 2))
2098 ((coefft) (b freevar0
) ((mexpt) (z varp
) -
2))
2099 ((coeffpp) (c freevar
)))))))
2101 ;;; Recognize z^n*d^(a*z^2+b*z+c)
2103 (defun m2-exp-type-5 (expr)
2106 ((mexpt) (z varp
) (n freevar
))
2110 ((coeffpt) (a freevar
) ((mexpt) (z varp
) 2))
2111 ((coeffpt) (b freevar
) (z varp
))
2112 ((coeffpp) (c freevar
)))))))
2114 ;;; Recognize z^n*(%e^(a*z^2+b*z+c))^u
2116 (defun m2-exp-type-5-1 (expr)
2119 ((mexpt) (z varp
) (n freevar0
))
2124 ((coeffpp) (c freevar
))
2125 ((coefft) (a freevar0
) ((mexpt) (z varp
) 2))
2126 ((coefft) (b freevar0
) (z varp
))))
2129 ;;; Recognize z^n*d^(a*sqrt(z)+b*z+c)
2131 (defun m2-exp-type-6 (expr)
2134 ((mexpt) (z varp
) (n freevar0
))
2138 ((coefft) (a freevar0
) ((mexpt) (z varp
) ((rat) 1 2)))
2139 ((coefft) (b freevar0
) (z varp
))
2140 ((coeffpp) (c freevar
)))))))
2142 ;;; Recognize z^n*(%e^(a*sqrt(z)+b*z+c))^u
2144 (defun m2-exp-type-6-1 (expr)
2147 ((mexpt) (z varp
) (n freevar0
))
2152 ((coeffpp) (c freevar
))
2153 ((coefft) (a freevar0
) ((mexpt) (z varp
) ((rat) 1 2)))
2154 ((coefft) (b freevar0
) (z varp
))))
2157 ;;; Recognize z^n*a^(b*z^r+e)*h^(c*z^r+g)
2159 (defun m2-exp-type-7 (expr)
2162 ((mexpt) (z varp
) (n freevar
))
2168 ((mexpt) (z varp
) (r freevar0
)))
2169 ((coeffpp) (e freevar
))))
2175 ((mexpt) (z varp
) (r1 freevar0
)))
2176 ((coeffpp) (g freevar
)))))))
2178 ;;; Recognize z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u
2180 (defun m2-exp-type-7-1 (expr)
2183 ((mexpt) (z varp
) (v freevar
))
2188 ((coeffpp) (e freevar
))
2189 ((coefft) (b freevar0
) ((mexpt) (z varp
) (r freevar0
)))))
2195 ((coeffpp) (g freevar
))
2196 ((coefft) (c freevar0
) ((mexpt) (z varp
) (r1 freevar0
)))))
2199 ;;; Recognize a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)
2201 (defun m2-exp-type-8 (expr)
2207 ((coeffpt) (b freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2208 ((coeffpt) (d freevar
) (z varp
))
2209 ((coeffpp) (e freevar
))))
2213 ((coeffpt) (c freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2214 ((coeffpt) (f freevar
) (z varp
))
2215 ((coeffpp) (g freevar
)))))))
2217 ;;; Recognize (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v
2219 (defun m2-exp-type-8-1 (expr)
2226 ((coeffpp) (e freevar
))
2227 ((coeffpt) (b freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2228 ((coeffpt) (d freevar
) (z varp
))))
2234 ((coeffpp) (g freevar
))
2235 ((coeffpt) (c freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2236 ((coeffpt) (f freevar
) (z varp
))))
2239 ;;; Recognize (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v
2241 (defun m2-exp-type-8-2 (expr)
2248 ((coeffpp) (e freevar
))
2249 ((coefft) (b freevar
) ((mexpt) (z varp
) (r freevar0
)))))
2255 ((coeffpp) (g freevar
))
2256 ((coefft) (c freevar
) ((mexpt) (z varp
) (r1 freevar0
)))))
2259 ;;; Recognize z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)
2261 (defun m2-exp-type-9 (expr)
2264 ((mexpt) (z varp
) (n freevar
))
2268 ((coeffpt) (b freevar
) ((mexpt) (z varp
) 2))
2269 ((coeffpt) (d freevar
) (z varp
))
2270 ((coeffpp) (e freevar
))))
2274 ((coeffpt) (c freevar
) ((mexpt) (z varp
) 2))
2275 ((coeffpt) (f freevar
) (z varp
))
2276 ((coeffpp) (g freevar
)))))))
2278 ;;; Recognize z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u
2280 (defun m2-exp-type-9-1 (expr)
2283 ((mexpt) (z varp
) (n freevar
))
2288 ((coeffpp) (e freevar
))
2289 ((coeffpt) (b freevar
) ((mexpt) (z varp
) 2))
2290 ((coeffpt) (d freevar
) (z varp
))))
2296 ((coeffpp) (g freevar
))
2297 ((coeffpt) (c freevar
) ((mexpt) (z varp
) 2))
2298 ((coeffpt) (f freevar
) (z varp
))))
2301 ;;; Recognize z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z+)f*z+g)
2303 (defun m2-exp-type-10 (expr)
2306 ((mexpt) (z varp
) (n freevar
))
2310 ((coeffpt) (b freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2311 ((coeffpt) (d freevar
) (z varp
))
2312 ((coeffpp) (e freevar
))))
2316 ((coeffpt) (c freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2317 ((coeffpt) (f freevar
) (z varp
))
2318 ((coeffpp) (g freevar
)))))))
2320 ;;; Recognize z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u
2322 (defun m2-exp-type-10-1 (expr)
2325 ((mexpt) (z varp
) (n freevar
))
2330 ((coeffpp) (e freevar
))
2331 ((coeffpt) (b freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2332 ((coeffpt) (d freevar
) (z varp
))))
2338 ((coeffpp) (g freevar
))
2339 ((coeffpt) (c freevar
) ((mexpt) (z varp
) ((rat) 1 2)))
2340 ((coeffpt) (f freevar
) (z varp
))))
2343 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2345 (defun integrate-exp-special (expr var
&aux w const
)
2347 ;; First factor the expression.
2348 (setq expr
($factor expr
))
2350 ;; Remove constant factors.
2351 (setq w
(partition expr var
1))
2352 (setq const
(car w
))
2356 ((m2-exp-type-1a (facsum-exponent expr
))
2358 (when *debug-integrate
*
2359 (format t
"~&Type 1a: (a^(c*(z^r)^p+d)^v : w = ~A~%" w
))
2363 ;; 1/(p*r*(a^(c*v*(var^r)^p)))
2364 (inv (mul p r
(power a
(mul c v
(power (power var r
) p
)))))
2366 ;; (a^(d+c*(var^r)^p))^v
2367 (power (power a
(add d
(mul c
(power (power var r
) p
)))) v
)
2368 ;; gamma_incomplete(1/(p*r), -c*v*(var^r)^p*log(a))
2369 (take '(%gamma_incomplete
)
2371 (mul -
1 c v
(power (power var r
) p
) (take '(%log
) a
)))
2372 ;; (-c*v*(var^r)^p*log(a))^(-1/(p*r))
2373 (power (mul -
1 c v
(power (power var r
) p
) (take '(%log
) a
))
2374 (div -
1 (mul p r
)))))
2376 ((m2-exp-type-2 (facsum-exponent expr
))
2379 (when *debug-integrate
*
2380 (format t
"~&Type 2: z^v*a^(b*z^r+d) : w = ~A~%" w
))
2386 (power var
(add v
1))
2389 (mul -
1 b
(power var r
) ($log a
)))
2391 (mul -
1 b
(power var r
) ($log a
))
2392 (mul -
1 (div (add v
1) r
)))))
2394 ((m2-exp-type-2-1 (facsum-exponent expr
))
2396 (when *debug-integrate
*
2397 (format t
"~&Type 2-1: z^v*(%e^(a*z^r+b))^u : w = ~A~%" w
))
2402 (power '$%e
(mul -
1 a u
(power var r
)))
2403 (power (power '$%e
(add (mul a
(power var r
)) b
)) u
)
2404 (power var
(add v
1))
2405 (power (mul -
1 a u
(power var r
)) (div (mul -
1 (add v
1)) r
))
2406 (take '(%gamma_incomplete
)
2408 (mul -
1 a u
(power var r
)))))
2410 ((m2-exp-type-3 (facsum-exponent expr
))
2412 (when *debug-integrate
*
2413 (format t
"~&Type 3: (a*z+b)^p*%e^(c*z+d) : w = ~A~%" w
))
2417 (power '$%e
(sub d
(div (mul b c
) a
)))
2418 (power (add b
(mul a var
)) (add p
1))
2419 (ftake '%expintegral_e
(mul -
1 p
) (mul (div -
1 a
) c
(add b
(mul a var
))))))
2421 ((m2-exp-type-4 expr
)
2423 (let (($trigsign nil
)) ; Do not simplify erfc(-x) !
2424 (when *debug-integrate
*
2425 (format t
"~&Type 4: d^(a*z^2+b/z^2+c) : w = ~A~%" w
))
2429 (div 1 (mul 4 (power (mul -
1 a
($log d
)) (div 1 2))))
2432 (power '$%pi
(div 1 2))
2435 (power (mul -
1 a
($log d
)) (div 1 2))
2436 (power (mul -
1 b
($log d
)) (div 1 2))))
2440 (div (power (mul -
1 b
($log d
)) (div 1 2)) var
)
2441 (mul -
1 var
(power (mul -
1 a
($log d
)) (div 1 2)))))
2445 (power (mul -
1 a
($log d
)) (div 1 2))
2446 (power (mul -
1 b
($log d
)) (div 1 2))))
2449 (mul var
(power (mul -
1 a
($log d
)) (div 1 2)))
2450 (div (power (mul -
1 b
($log d
)) (div 1 2)) var
)))))))))
2452 ((and (m2-exp-type-4-1 expr
)
2453 (poseven (cdras 'n w
)) ; only for n a positive, even integer
2454 (symbolp (cdras 'a w
))) ; a has to be a symbol
2456 (let (($trigsign nil
)) ; Do not simplify erfc(-x) !
2458 (when *debug-integrate
*
2459 (format t
"~&Type 4-1: z^(2*n)*d^(a*z^2+b/z^2+c) : w = ~A~%" w
))
2466 (power '$%pi
(div 1 2))
2467 (simplify (list '(%derivative
)
2471 (power ($log d
) (mul -
1 n
))
2477 (power (mul -
1 a
($log d
)) (div 1 2))
2478 (power (mul -
1 b
($log d
)) (div 1 2))))
2482 (power (mul -
1 b
($log d
)) (div 1 2))
2484 (mul var
(power (mul -
1 ($log d
)) (div 1 2))))))))
2489 (power (mul -
1 a
($log d
)) (div 1 2))
2490 (power (mul -
1 b
($log d
)) (div 1 2))))
2493 (power (mul -
1 a
($log d
)) (div 1 2))
2494 (div (power (mul -
1 b
($log d
)) (div 1 2)) var
)))))
2495 (power (mul -
1 a
($log d
)) (div 1 2)))
2498 ((and (m2-exp-type-5 (facsum-exponent expr
))
2499 (maxima-integerp (cdras 'n w
))
2500 (eq ($sign
(cdras 'n w
)) '$pos
))
2503 (when *debug-integrate
*
2504 (format t
"~&Type 5: z^n*d^(a*z^2+b*z+c) : w = ~A~%" w
))
2508 (div -
1 (mul 2 (power (mul a
($log d
)) (div 1 2))))
2510 (power d
(sub c
(div (mul b b
) (mul 4 a
))))
2511 (let ((index (gensumindex))
2515 (power 2 (sub index n
))
2518 (div (add index
1) 2)
2521 (power (add b
(mul 2 a var
)) 2)
2523 (power (mul a
($log d
)) (mul -
1 (add n
(div 1 2))))
2524 (power (mul -
1 b
($log d
)) (sub n index
))
2525 (power (mul (add b
(mul 2 a var
)) ($log d
)) (add index
1))
2527 (mul (div -
1 a
) (power (add b
(mul 2 a var
)) 2) ($log d
))
2528 (mul (div -
1 2) (add index
1))))
2531 ((and (m2-exp-type-5-1 (facsum-exponent expr
))
2532 (maxima-integerp (cdras 'n w
))
2533 (eq ($sign
(cdras 'n w
)) '$pos
))
2535 (when *debug-integrate
*
2536 (format t
"~&Type 5-1: z^n*(%e^(a*z^2+b*z+c))^u : w = ~A~%" w
))
2541 (add (mul -
1 (div (mul b b u
) (mul 4 a
)))
2542 (mul -
1 u
(add (mul a var var
) (mul b var
)))))
2543 (power a
(mul -
1 (add n
1)))
2545 (add (mul a var var
) (mul b var
) c
))
2547 (let ((index (gensumindex))
2550 (mul (power 2 (sub index n
))
2551 (power (mul -
1 b
) (sub n index
))
2552 (power (add b
(mul 2 a var
)) (add index
1))
2553 (power (div (mul -
1 u
(power (add b
(mul 2 a var
)) 2)) a
)
2554 (mul (div -
1 2) (add index
1)))
2555 (take '(%binomial
) n index
)
2556 (take '(%gamma_incomplete
)
2557 (div (add index
1) 2)
2558 (div (mul -
1 u
(power (add b
(mul 2 a var
)) 2))
2562 ((and (m2-exp-type-6 (facsum-exponent expr
))
2563 (maxima-integerp (cdras 'n w
))
2564 (eq ($sign
(cdras 'n w
)) '$pos
))
2566 (when *debug-integrate
*
2567 (format t
"~&Type 6: z^n*d^(a*sqrt(z)+b*z+c) : w = ~A~%" w
))
2571 (power 2 (mul -
1 (add n
1)))
2572 (power d
(sub c
(div (mul a a
) (mul 4 b
))))
2573 (power (mul b
($log d
)) (mul -
2 (add n
1)))
2574 (let ((index1 (gensumindex))
2575 (index2 (gensumindex))
2580 (power -
1 (sub index1 index2
))
2582 ($binomial index1 index2
)
2583 ($binomial n index1
)
2585 (power (mul a
($log d
)) (sub (mul 2 n
) (add index1 index2
)))
2587 (mul (add a
(mul 2 b
(power var
(div 1 2)))) ($log d
))
2588 (add index1 index2
))
2592 (power (add a
(mul 2 b
(power var
(div 1 2)))) 2)
2594 (mul (div -
1 2) (add index1 index2
1)))
2600 (power (add a
(mul 2 b
(power var
(div 1 2)))) 2)
2604 (div (add index1 index2
2) 2)
2607 (power (add a
(mul 2 b
(power var
(div 1 2)))) 2)
2610 (add a
(mul 2 b
(power var
(div 1 2))))
2613 (div (add index1 index2
1) 2)
2616 (power (add a
(mul 2 b
(power var
(div 1 2)))) 2)
2621 ((and (m2-exp-type-6-1 (facsum-exponent expr
))
2622 (maxima-integerp (cdras 'n w
))
2623 (eq ($sign
(cdras 'n w
)) '$pos
))
2625 (when *debug-integrate
*
2626 (format t
"~&Type 6-1: z^n*(%e^(a*sqrt(z)+b*z+c))^u : w = ~A~%" w
))
2629 (power 2 (mul -
1 (add (mul 2 n
) 1)))
2631 (add (div (mul -
1 u a a
) (mul 4 b
))
2632 (mul u
(add (mul a
(power var
(div 1 2)))
2635 (power b
(mul -
2 (add n
1)))
2637 (add (mul a
(power var
(div 1 2)))
2640 (let ((index1 (gensumindex))
2641 (index2 (gensumindex))
2645 (mul (power -
1 (sub index1 index2
))
2647 (power a
(add (neg index2
) (neg index1
) (mul 2 n
)))
2648 (power (add a
(mul 2 b
(power var
(div 1 2))))
2649 (add index1 index2
))
2650 (power (div (mul -
1 u
2654 (power var
(div 1 2))))
2657 (mul (div -
1 2) (add index1 index2
1)))
2658 (take '(%binomial
) index1 index2
)
2659 (take '(%binomial
) n index1
)
2661 (add a
(mul 2 b
(power var
(div 1 2))))
2662 (take '(%gamma_incomplete
)
2663 (div (add index1 index2
1) 2)
2672 (power (div (mul -
1 u
2681 (take '(%gamma_incomplete
)
2682 (div (add index1 index2
2) 2)
2686 (power var
(div 1 2))))
2692 ((and (m2-exp-type-7 (facsum-exponent expr
))
2693 (eq ($sign
(sub (cdras 'r w
) (cdras 'r1 w
))) '$zero
))
2695 (when *debug-integrate
*
2696 (format t
"~&Type 7: z^n*a^(b*z^r+e)*h^(c*z^r+g) : w = ~A~%" w
))
2709 (add (mul b
($log a
)) (mul c
($log h
))))
2713 (mul -
1 (power var r
) (add (mul b
($log a
)) (mul c
($log h
)))))))
2715 ((and (m2-exp-type-7-1 (facsum-exponent expr
))
2716 (eq ($sign
(sub (cdras 'r w
) (cdras 'r1 w
))) '$zero
))
2718 (when *debug-integrate
*
2719 (format t
"~&Type 7-1: z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u : w = ~A~%" w
))
2723 (power '$%e
(mul -
1 (power var r
) (add (mul b q
) (mul c u
))))
2724 (power (power '$%e
(add e
(mul b
(power var r
)))) q
)
2725 (power (power '$%e
(add g
(mul c
(power var r
)))) u
)
2726 (power var
(add v
1))
2727 (power (mul -
1 (power var r
) (add (mul b q
) (mul c u
)))
2728 (div (mul -
1 (add v
1)) r
))
2729 (take '(%gamma_incomplete
)
2731 (mul -
1 (power var r
) (add (mul b q
) (mul c u
))))))
2733 ((m2-exp-type-8 (facsum-exponent expr
))
2735 (when *debug-integrate
*
2736 (format t
"~&Type 8: a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2737 (format t
"~& : w = ~A~%" w
))
2746 (power a
(add (mul b
(power var
(div 1 2))) (mul d var
)))
2747 (power h
(add (mul c
(power var
(div 1 2))) (mul f var
)))
2748 (div 1 (add (mul d
($log a
)) (mul f
($log h
)))))
2750 (power '$%pi
(div 1 2))
2754 (power (add (mul b
($log a
)) (mul c
($log h
))) 2)
2755 (mul 4 (add (mul d
($log a
)) (mul f
($log h
)))))))
2762 (power var
(div 1 2))
2763 (add (mul d
($log a
)) (mul f
($log h
)))))
2765 (power (add (mul d
($log a
)) (mul f
($log h
))) (div 1 2)))))
2766 (add (mul b
($log a
)) (mul c
($log h
)))
2767 (power (add (mul d
($log a
)) (mul f
($log h
))) (div -
3 2))))))
2769 ((m2-exp-type-8-1 (facsum-exponent expr
))
2771 (when *debug-integrate
*
2772 (format t
"~&Type 8-1: (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v")
2773 (format t
"~& : w = ~A~%" w
))
2777 (power (add (mul d u
) (mul f v
)) (div -
3 2))
2780 (power (add (mul b u
)
2781 (mul 2 d u
(power var
(div 1 2)))
2782 (mul v
(add c
(mul 2 f
(power var
(div 1 2))))))
2784 (inv (mul 4 (add (mul d u
) (mul f v
))))))
2786 (add (mul b
(power var
(div 1 2)))
2791 (add (mul c
(power var
(div 1 2)))
2797 (mul (power (add (mul b u
)
2798 (mul 2 d u
(power var
(div 1 2)))
2799 (mul v
(add c
(mul 2 f
(power var
(div 1 2))))))
2801 (inv (mul 4 (add (mul d u
) (mul f v
))))))
2802 (power (add (mul d u
) (mul f v
)) (div 1 2)))
2804 (power '$%pi
(div 1 2))
2805 (add (mul b u
) (mul c v
))
2808 (mul 2 d u
(power var
(div 1 2)))
2810 (mul 2 f v
(power var
(div 1 2))))
2812 (power (add (mul d u
) (mul f v
))
2815 ((and (m2-exp-type-8-2 (facsum-exponent expr
))
2816 (eq ($sign
(sub (cdras 'r w
) (cdras 'r1 w
))) '$zero
))
2818 (when *debug-integrate
*
2819 (format t
"~&Type 8-2: (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v")
2820 (format t
"~& : w = ~A~%" w
))
2828 (add (mul b u
) (mul c v
))))
2830 (add (power var r
) e
))
2833 (add (power var r
) g
))
2838 (add (mul b u
) (mul c v
)))
2840 (take '(%gamma_incomplete
)
2842 (mul -
1 (power var r
) (add (mul b u
) (mul c v
))))))
2844 ((and (m2-exp-type-9 (facsum-exponent expr
))
2845 (maxima-integerp (cdras 'n w
))
2846 (eq ($sign
(cdras 'n w
)) '$pos
)
2847 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
2848 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
2850 (when *debug-integrate
*
2851 (format t
"~&Type 9: z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)")
2852 (format t
"~& : w = ~A~%" w
))
2861 (power (add (mul d
($log a
)) (mul f
($log h
))) 2)
2862 (mul -
4 (add (mul b
($log a
)) (mul c
($log h
))))))
2863 (power (add (mul b
($log a
)) (mul c
($log h
))) (mul -
1 (add n
1)))
2864 (let ((index (gensumindex))
2868 (power 2 (sub index n
))
2871 (add (mul -
1 d
($log a
)) (mul -
1 f
($log h
)))
2875 (mul (add d
(mul 2 b var
)) ($log a
))
2876 (mul (add f
(mul 2 c var
)) ($log h
)))
2883 (mul (add d
(mul 2 b var
)) ($log a
))
2884 (mul (add f
(mul 2 c var
)) ($log h
)))
2886 (add (mul b
($log a
)) (mul c
($log h
)))))
2887 (div (add index
1) -
2))
2889 (div (add index
1) 2)
2894 (mul (add d
(mul 2 b var
)) ($log a
))
2895 (mul (add f
(mul 2 c var
)) ($log h
)))
2897 (mul 4 (add (mul b
($log a
)) (mul c
($log h
))))))))
2900 ((and (m2-exp-type-9-1 (facsum-exponent expr
))
2901 (maxima-integerp (cdras 'n w
))
2902 (eq ($sign
(cdras 'n w
)) '$pos
)
2903 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
2904 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
2906 (when *debug-integrate
*
2907 (format t
"~&Type 9-1: z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u")
2908 (format t
"~& : w = ~A~%" w
))
2912 (power (add (mul b q
) (mul c u
)) (div -
1 2))
2914 (add (div (power (add (mul d q
) (mul f u
)) 2)
2915 (mul -
4 (add (mul b q
) (mul c u
))))
2923 (mul var
(add d
(mul b var
)))))
2927 (mul var
(add f
(mul c var
)))))
2929 (let ((index (gensumindex))
2932 (mul (power 2 (sub index n
))
2933 (power (add (mul b q
) (mul c u
)) (neg (add n
(div 1 2))))
2934 (power (add (neg (mul d q
)) (neg (mul f u
)))
2936 (power (add (mul d q
)
2938 (mul 2 var
(add (mul b q
) (mul c u
))))
2940 (power (div (power (add (mul d q
)
2947 (neg (add (mul b q
) (mul c u
))))
2948 (mul (div -
1 2) (add index
1)))
2949 (take '(%binomial
) n index
)
2950 (take '(%gamma_incomplete
)
2951 (div (add index
1) 2)
2952 (div (power (add (mul d q
)
2959 (mul -
4 (add (mul b q
) (mul c u
))))))
2962 ((and (m2-exp-type-10 (facsum-exponent expr
))
2963 (maxima-integerp (cdras 'n w
))
2964 (eq ($sign
(cdras 'n w
)) '$pos
)
2965 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
2966 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
2968 (when *debug-integrate
*
2969 (format t
"~&Type 10: z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2970 (format t
"~& : w = ~A~%" w
))
2973 (power 2 (add (mul -
2 n
) -
1))
2977 (div (power (add (mul b
($log a
)) (mul c
($log h
))) 2)
2978 (mul -
4 (add (mul d
($log a
)) (mul f
($log h
))))))
2979 (power (add (mul d
($log a
)) (mul f
($log h
))) (mul -
2 (add n
1)))
2980 (let ((index1 (gensumindex))
2981 (index2 (gensumindex))
2985 (mul (power -
1 (sub index1 index2
))
2987 ($binomial index1 index2
)
2988 ($binomial n index1
)
2989 (power (add (mul b
($log a
)) (mul c
($log h
)))
2990 (sub (mul 2 n
) (add index1 index2
)))
2991 (power (add (mul b
($log a
))
2994 (power var
(div 1 2))
2995 (add (mul d
($log a
)) (mul f
($log h
)))))
2996 (add index1 index2
))
2998 (div (power (add (mul b
($log a
))
3001 (power var
(div 1 2))
3002 (add (mul d
($log a
))
3005 (add (mul d
($log a
)) (mul f
($log h
)))))
3006 (mul (div -
1 2) (add index1 index2
1)))
3007 (add (mul ($gamma_incomplete
(mul (div 1 2)
3008 (add index1 index2
1))
3010 (div (power (add (mul b
($log a
))
3013 (power var
(div 1 2))
3014 (add (mul d
($log a
)) (mul f
($log h
)))))
3016 (add (mul d
($log a
)) (mul f
($log h
))))))
3017 (add (mul b
($log a
)) (mul c
($log h
)))
3018 (add (mul b
($log a
))
3021 (power var
(div 1 2))
3022 (add (mul d
($log a
)) (mul f
($log h
))))))
3024 ($gamma_incomplete
(mul (div 1 2)
3025 (add index1 index2
2))
3027 (div (power (add (mul b
($log a
))
3030 (power var
(div 1 2))
3031 (add (mul d
($log a
))
3034 (add (mul d
($log a
))
3035 (mul f
($log h
))))))
3036 (add (mul d
($log a
)) (mul f
($log h
)))
3038 (div (power (add (mul b
($log a
))
3041 (power var
(div 1 2))
3042 (add (mul d
($log a
))
3045 (add (mul d
($log a
))
3051 ((and (m2-exp-type-10-1 (facsum-exponent expr
))
3052 (maxima-integerp (cdras 'n w
))
3053 (eq ($sign
(cdras 'n w
)) '$pos
)
3054 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
3055 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
3057 (let ((bq+cu
(add (mul b q
) (mul c u
)))
3058 (dq+fu
(add (mul d q
) (mul f u
))))
3059 (when *debug-integrate
*
3060 (format t
"~&Type 10-1: z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u")
3061 (format t
"~& : w = ~A~%" w
))
3064 (power 2 (mul -
1 (add (mul 2 n
) 1)))
3066 (add (div (mul -
1 (power bq
+cu
2)) (mul 4 dq
+fu
))
3068 (mul -
1 b
(power var
(div 1 2)) q
)
3070 (mul -
1 c
(power var
(div 1 2)) u
)))
3072 (add (mul b
(power var
(div 1 2)))
3077 (add (mul c
(power var
(div 1 2)))
3081 (power dq
+fu
(mul -
2 (add n
1)))
3082 (let ((index1 (gensumindex))
3083 (index2 (gensumindex))
3087 (mul (power -
1 (sub index1 index2
))
3090 (add (neg index1
) (neg index2
) (mul 2 n
)))
3092 (mul 2 (power var
(div 1 2)) dq
+fu
))
3093 (add index1 index2
))
3094 (power (div (power (add bq
+cu
3096 (power var
(div 1 2))
3101 (add index1 index2
1)))
3102 (take '(%binomial
) index1 index2
)
3103 (take '(%binomial
) n index1
)
3107 (power var
(div 1 2))
3109 (take '(%gamma_incomplete
)
3111 (add index1 index2
1))
3112 (div (power (add (mul b q
)
3115 (power var
(div 1 2))
3121 (power (div (power (add bq
+cu
3123 (power var
(div 1 2))
3129 (take '(%gamma_incomplete
)
3131 (add index1 index2
2))
3132 (div (power (add bq
+cu
3134 (power var
(div 1 2))
3142 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3144 ;;; Do a facsum for the exponent of power functions.
3145 ;;; This is necessary to integrate all general forms. The pattern matcher is
3146 ;;; not powerful enough to do the job.
3148 (defun facsum-exponent (expr)
3149 ;; Make sure that expr has the form ((mtimes) factor1 factor2 ...)
3150 (when (not (mtimesp expr
)) (setq expr
(list '(mtimes) expr
)))
3152 (l (cdr expr
) (cdr l
)))
3153 ((null l
) (cons (list 'mtimes
) result
))
3156 ;; Found an power function. Factor the exponent with facsum.
3157 (let* ((fac (mfuncall '$facsum
(caddr (car l
)) var
))
3161 (cons (cons (list 'mexpt
)
3162 (cons (cadr (car l
))
3165 (list ($multthru
(inv den
) num
)))))
3169 (setq result
(cons (car l
) result
))))))
3171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;