1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 (defvar *debug-integrate
* nil
24 "Enable debugging for the integrator routines.")
26 ;; When T do not call the risch integrator. This flag can be set by the risch
27 ;; integrator to avoid endless loops when calling the integrator from risch.
28 (defvar *in-risch-p
* nil
)
30 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
32 ;;; Predicate functions
34 ;; Note: varp and freevarp are not used in this file anymore. But
35 ;; they are used in other files. Someday, varp and freevarp should be
37 (declaim (inline varp
))
39 (declare (special var
))
43 (declare (special var
))
44 (cond ((atom a
) (not (eq a var
)))
46 ((and (not (atom (car a
)))
47 (member 'array
(cdar a
) :test
#'eq
))
48 (cond ((freevar (cdr a
)) t
)
49 (t (merror "~&FREEVAR: variable of integration appeared in subscript."))))
50 (t (and (freevar (car a
)) (freevar (cdr a
))))))
52 ;; Same as varp, but the second arg specifiies the variable to be
53 ;; tested instead of using the special variable VAR.
57 ;; Like freevar bug the second arg specifies the variable to be tested
58 ;; instead of using the special variable VAR.
59 (defun freevar2 (a var2
)
60 (cond ((atom a
) (not (eq a var2
)))
62 ((and (not (atom (car a
)))
63 (member 'array
(cdar a
) :test
#'eq
))
64 (cond ((freevar2 (cdr a
) var2
) t
)
65 (t (merror "~&FREEVAR: variable of integration appeared in subscript."))))
66 (t (and (freevar2 (car a
) var2
) (freevar2 (cdr a
) var2
)))))
69 "Returns 2*x if 2*x is an integer, else nil"
70 (integerp2 (mul2* 2 x
)))
73 "Returns x if x is an integer, else false"
75 (cond ((not (numberp x
)) nil
)
77 ((prog2 (setq u
(maxima-rationalize x
))
81 ;; This predicate is used with m2 pattern matcher.
82 ;; A rational expression in var2.
84 (cond ((or (varp2 ex var2
) (freevar2 ex var2
))
86 ((member (caar ex
) '(mplus mtimes
) :test
#'eq
)
87 (do ((u (cdr ex
) (cdr u
)))
89 (if (not (rat8 (car u
) var2
))
91 ((not (eq (caar ex
) 'mexpt
))
93 ((integerp (caddr ex
))
94 (rat8 (cadr ex
) var2
))))
96 ;; Predicate for m2 pattern matcher
97 (defun rat8prime (c var2
)
102 ;; Predicate for m2 patter matcher
103 (defun elem (a expres var2
)
104 (cond ((freevar2 a var2
) t
)
107 (t (every #'(lambda (f)
108 (elem f expres var2
))
111 ;; Like freevar0 (in hypgeo.lisp), but we take a second arg to specify
112 ;; the variable instead of implicitly using VAR.
113 (defun freevar02 (m var2
)
114 (cond ((equal m
0) nil
)
115 (t (freevar2 m var2
))))
117 ;; Like free1 in schatc, but takes an extra arg for the variable.
118 (defun free12 (a var2
)
119 (and (null (pzerop a
))
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
126 (defun rationalizer (x)
127 (let ((ex (simplify ($factor x
))))
128 (if (not (alike1 ex x
)) ex
)))
130 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
132 ;;; Stage II of the Integrator
134 ;;; Check if the problem can be transformed or solved by special methods.
135 ;;; 11 Methods are implemented by Moses, some more have been added.
138 ;; POWERL is initialized in INTEGRATOR to NIL and can be modified in
139 ;; INTFORM in certain cases and is read by INTEGRATOR in some cases.
140 ;; Instead of a global special variable, use a closure.
142 ;; It would be really good to get rid of the special variable *EXP*
143 ;; used only in INTFORM and INTEGRATOR. I (rtoy) haven't been able
144 ;; to figure out exactly how to do that.
146 (defun intform (expres var2
&aux w arg
)
147 (declare (special *exp
*))
148 (cond ((freevar2 expres var2
) nil
)
151 ;; Map the function intform over the arguments of a sum or a product
152 ((member (caar expres
) '(mplus mtimes
) :test
#'eq
)
157 ((or (eq (caar expres
) '%log
)
158 (arcp (caar expres
)))
160 ;; Method 9: Rational function times a log or arctric function
162 `((mtimes) ((,(caar expres
)) (b rat8
,var2
))
163 ((coefftt) (c rat8prime
,var2
)))))
164 ;; Integrand is of the form R(x)*F(S(x)) where F is a log, or
165 ;; arctric function and R(x) and S(x) are rational functions.
166 (ratlog var2
(cons (cons 'a expres
) arg
)))
170 ((setq y
(intform (cadr expres
) var2
)) (return y
))
172 ;; Method 10: Rational function times log(b*x+a)
173 ((and (eq (caar expres
) '%log
)
174 (setq z
(m2-b*x
+a
(cadr expres
) var2
))
177 ((coefftt) (c rat8
,var2
))
178 ((coefftt) (d elem
,expres
,var2
))))))
180 (let ((a (cdr (assoc 'a z
:test
#'eq
)))
181 (b (cdr (assoc 'b z
:test
#'eq
)))
182 (c (cdr (assoc 'c y
:test
#'eq
)))
183 (d (cdr (assoc 'd y
:test
#'eq
)))
184 (newvar (gensym "intform")))
185 ;; keep var from appearing in questions to user
186 (putprop newvar t
'internal
)
187 ;; Substitute y = log(b*x+a) and integrate again
193 (list (maxima-substitute
194 `((mquotient) ((mplus) ((mexpt) $%e
,newvar
)
199 `((mquotient) ((mexpt) $%e
,newvar
) ,b
)
200 (maxima-substitute newvar expres d
))
205 (t (return nil
)))))))
207 ;; We have a special function with an integral on the property list.
208 ;; After the integral property was defined for the trig functions,
209 ;; in rev 1.52, need to exclude trig functions here.
210 ((and (not (atom (car expres
)))
211 (not (optrig (caar expres
)))
212 (not (eq (caar expres
) 'mexpt
))
213 (get (caar expres
) 'integral
))
214 (when *debug-integrate
*
215 (format t
"~&INTFORM: found 'INTEGRAL on property list~%"))
218 (m2 *exp
* `((mtimes) ((,(caar expres
)) (b rat8
,var2
)) ((coefftt) (c rat8prime
,var2
)))))
219 ;; A rational function times the special function.
220 ;; Integrate with the method integration-by-parts.
221 (partial-integration (cons (cons 'a expres
) arg
) var2
))
222 ;; The method of integration-by-parts can not be applied.
223 ;; Maxima tries to get a clue for the argument of the function which
224 ;; allows a substitution for the argument.
225 ((intform (cadr expres
) var2
))
228 ;; Method 6: Elementary function of trigonometric functions
229 ((optrig (caar expres
))
230 (cond ((not (setq w
(m2-b*x
+a
(cadr expres
) var2
)))
231 (intform (cadr expres
) var2
))
235 (monstertrig *exp
* var2
(cadr expres
))))))
237 ((and (eq (caar expres
) '%derivative
)
238 (eq (caar *exp
*) (caar expres
))
239 (checkderiv *exp
* var2
)))
241 ;; Stop intform if we have not a power function.
242 ((not (eq (caar expres
) 'mexpt
)) nil
)
244 ;; Method 2: Substitution for an integral power
245 ((integerp (caddr expres
)) (intform (cadr expres
) var2
))
247 ;; Method 1: Elementary function of exponentials
248 ((freevar2 (cadr expres
) var2
)
249 (cond ((setq w
(m2-b*x
+a
(caddr expres
) var2
))
250 (superexpt *exp
* var2
(cadr expres
) w
))
251 ((intform (caddr expres
) var2
))
252 ((and (eq '$%e
(cadr expres
))
253 (isinop (caddr expres
) '%log
))
254 ;; Found something like exp(r*log(x))
255 (let* (($%e_to_numlog t
)
256 ($radexpand nil
) ; do not simplify sqrt(x^2) -> abs(x)
257 (nexp (resimplify *exp
*)))
258 (cond ((alike1 *exp
* nexp
) nil
)
259 (t (integrator (setq *exp
* nexp
) var2
)))))
262 ;; The base is not a rational function. Try to get a clue for the base.
263 ((not (rat8 (cadr expres
) var2
))
264 (intform (cadr expres
) var2
))
266 ;; Method 3: Substitution for a rational root
267 ((and (setq w
(m2-ratrootform (cadr expres
) var2
)) ; e*(a*x+b) / (c*x+d)
268 (denomfind (caddr expres
))) ; expon is ratnum
271 (ratroot *exp
* var2
(cadr expres
) w
))
274 ;; Method 4: Binomial - Chebyschev
275 ((not (integerp1 (caddr expres
))) ; 2*exponent not integer
276 (cond ((m2-chebyform *exp
* var2
)
278 (t (intform (cadr expres
) var2
))))
280 ;; Method 5: Arctrigonometric substitution
281 ((setq w
(m2-c*x^
2+b
*x
+a
(cadr expres
) var2
)) ; sqrt(c*x^2+b*x+a)
283 (format t
"expres = sqrt(c*x^2+b*x+a)~%")
284 ;; I think this is method 5, arctrigonometric substitutions.
285 ;; (Moses, pg 80.) The integrand is of the form
286 ;; R(x,sqrt(c*x^2+b*x+a)). This method first eliminates the b
287 ;; term of the quadratic, and then uses an arctrig substitution.
290 ;; Method 4: Binomial - Chebyschev
291 ((m2-chebyform *exp
* var2
)
295 ;; Substitute the expanded factor into the integrand and try again.
296 ((not (m2 (setq w
($expand
(cadr expres
)))
299 (setq *exp
* (maxima-substitute w
(cadr expres
) *exp
*))
300 (intform (simplify (list '(mexpt) w
(caddr expres
)))
304 ;; Substitute the factored factor into the integrand and try again.
305 ((setq w
(rationalizer (cadr expres
)))
306 ;; The forms below used to have $radexpand set to $all. But I
307 ;; don't think we really want to do that here because that makes
308 ;; sqrt(x^2) become x, which might be totally wrong. This is one
309 ;; reason why we returned -4/3 for the
310 ;; integrate(sqrt(x+1/x-2),x,0,1). We were replacing
311 ;; sqrt((x-1)^2) with x - 1, which is totally wrong since 0 <= x
313 (setq *exp
* (let (($radexpand $radexpand
))
314 (maxima-substitute w
(cadr expres
) *exp
*)))
315 (intform (let (($radexpand
'$all
))
316 (simplify (list '(mexpt) w
(caddr expres
))))
318 ;;------------------------------------------------------------------------------
320 ;; This is the main integration routine. It is called from sinint.
322 (defun integrator (*exp
* var2
&optional stack
)
323 (declare (special *exp
*))
324 (prog (y const w arcpart coef integrand result
)
325 (declare (special *integrator-level
*))
327 ;; Increment recursion counter
328 (incf *integrator-level
*)
330 ;; Trivial case. exp is not a function of var2.
331 (if (freevar2 *exp
* var2
) (return (mul2* *exp
* var2
)))
333 ;; Remove constant factors
334 (setq w
(partition *exp
* var2
1))
339 (format t
"w = ~A~%" w
)
340 (format t
"const = ~A~%" const
)
341 (format t
"exp = ~A~%" *exp
*))
343 (cond ;; First stage, Method I: Integrate a sum.
345 (return (mul2* const
(integrate1 (cdr *exp
*) var2
))))
347 ;; Convert atan2(a,b) to atan(a/b) and try again.
348 ((setq w
(isinop *exp
* '%atan2
))
350 (maxima-substitute (take '(%atan
) (div (cadr w
) (caddr w
)))
354 (integrator *exp
* var2 stack
))))
356 ;; First stage, Method II: Integrate sums.
357 ((and (not (atom *exp
*))
358 (eq (caar *exp
*) '%sum
))
359 (return (mul2* const
(intsum *exp
* var2
))))
361 ;; First stage, Method III: Try derivative-divides method.
362 ;; This is the workhorse that solves many integrals.
363 ((setq y
(diffdiv *exp
* var2
))
364 (return (mul2* const y
))))
366 ;; At this point, we have EXP as a product of terms. Make Y a
367 ;; list of the terms of the product.
368 (setq y
(cond ((mtimesp *exp
*)
374 ;; We're looking at each term of the product and check if we can
375 ;; apply one of the special methods.
379 (format t
"car y =~%")
380 (maxima-display (car y
)))
381 (cond ((rat8 (car y
) var2
)
383 (format t
"In loop, go skip~%")
385 ((and (setq w
(intform (car y
) var2
))
386 ;; Do not return a noun form as result at this point, because
387 ;; we would like to check for further special integrals.
388 ;; We store the result for later use.
390 (not (isinop w
'%integrate
)))
392 (format t
"In loop, case intform~%")
393 (return (mul2* const w
)))
396 (format t
"In loop, go special~%")
397 ;; Store a possible partial result
403 ;; Method 8: Rational functions
404 (return (mul2* const
(cond ((setq y
(powerlist *exp
* var2
)) y
)
405 (t (ratint *exp
* var2
)))))))
409 ;; Third stage: Try more general methods
411 ;; SEPARC SETQS ARCPART AND COEF SUCH THAT
412 ;; COEF*ARCEXP=EXP WHERE ARCEXP IS OF THE FORM
413 ;; ARCFUNC^N AND COEF IS ITS ALGEBRAIC COEFFICIENT
420 (format t
"arcpart = ~A~%" arcpart
)
421 (format t
"coef =~%")
422 (maxima-display coef
))
423 (cond ((and (not (null arcpart
))
424 (do ((stacklist stack
(cdr stacklist
)))
426 (cond ((alike1 (car stacklist
) coef
)
428 (not (isinop (setq w
(let ((stack (cons coef stack
)))
429 (integrator coef var2 stack
)))
431 (setq integrand
(mul2 w
(sdiff arcpart var2
)))
432 (do ((stacklist stack
(cdr stacklist
)))
434 (cond ((alike1 (car stacklist
) integrand
)
437 (setq y
(let ((stack (cons integrand stack
))
439 (integrator integ var2 stack
)))
441 (return (add* (list '(mtimes) const w arcpart
)
442 (list '(mtimes) -
1 const y
))))
446 (cond ((setq y
(scep *exp
* var2
))
450 (format t
"cddr y =~%")
451 (maxima-display (cddr y
)))
452 (integrator ($trigreduce
*exp
*) var2 stack
))
453 (t (sce-int (car y
) (cadr y
) var2
))))
454 ;; I don't understand why we do this. This
455 ;; causes the stack overflow in Bug
456 ;; 1487703, because we keep expanding *exp*
457 ;; into a form that matches the original
458 ;; and therefore we loop forever. To
459 ;; break this we keep track how how many
460 ;; times we've tried this and give up
461 ;; after 4 (arbitrarily selected) times.
462 ((and (< *integrator-level
* 4)
463 (not (alike1 *exp
* (setq y
($expand
*exp
*)))))
466 (format t
"*exp* = ~A~%" *exp
*)
467 (maxima-display *exp
*)
468 (format t
"y = ~A~%" y
)
471 (integrator y var2 stack
))
473 (setq y
(powerlist *exp
* var2
)))
475 ((and (not *in-risch-p
*) ; Not called from rischint
476 (setq y
(rischint *exp
* var2
))
477 ;; rischint has not found an integral but
478 ;; returns a noun form. Do not return that
479 ;; noun form as result at this point, but
480 ;; store it for later use.
482 (not (isinop y
'%integrate
)))
484 ((setq y
(integrate-exp-special *exp
* var2
))
485 ;; Maxima found an integral for a power function
488 ;; Integrate-exp-special has not found an integral
489 ;; We look for a previous result obtained by
490 ;; intform or rischint.
493 (list '(%integrate
) *exp
* var2
)))))))))))
495 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
503 (eq (caar ex
) '%log
) ; Experimentally treat logs also.
504 (and (eq (caar ex
) 'mexpt
)
505 (integerp2 (caddr ex
))
506 (> (integerp2 (caddr ex
)) 0)
507 (arcfuncp (cadr ex
))))))
511 ((and (arcfuncp (car list
))
513 (setq arcpart
(car list
))
514 (arclist (cdr list
)))
516 (setq coef
(cons (car list
) coef
))
517 (arclist (cdr list
))))))
522 (eq (caar ex
) 'mtimes
))
524 (setq coef
(cond ((null (cdr coef
))
527 (setq coef
(cons (car ex
) coef
)))))))
528 (values arcpart coef
))))
530 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
532 ;;; Five pattern for the Integrator and other routines.
534 ;; This is matching the pattern e*(a*x+b)/(c*x+d), where
535 ;; a, b, c, d, and e are free of x, and x is the variable of integration.
536 (defun m2-ratrootform (expr var2
)
539 ((coefftt) (e freevar2
,var2
))
541 ((coeffpt) (a freevar2
,var2
) (var varp2
,var2
))
542 ((coeffpt) (b freevar2
,var2
)))
545 ((coeffpt) (c freevar2
,var2
) (var varp2
,var2
))
546 ((coeffpt) (d freevar2
,var2
)))
549 ;; This is for matching the pattern a*x^r1*(c1+c2*x^q)^r2.
550 (defun m2-chebyform (expr var2
)
553 ((mexpt) (var varp2
,var2
) (r1 numberp
))
557 ((coefftt) (c2 freevar2
,var2
))
558 ((mexpt) (var varp2
,var2
) (q free12
,var2
)))
559 ((coeffpp) (c1 freevar2
,var2
)))
561 ((coefftt) (a freevar2
,var2
)))))
563 ;; Pattern to match b*x + a
564 (defun m2-b*x
+a
(expr var2
)
567 ((coeffpt) (b freevar2
,var2
) (x varp2
,var2
))
568 ((coeffpt) (a freevar2
,var2
)))))
570 ;; This is the pattern c*x^2 + b * x + a.
571 (defun m2-c*x^
2+b
*x
+a
(expr var2
)
574 ((coeffpt) (c freevar2
,var2
) ((mexpt) (x varp2
,var2
) 2))
575 ((coeffpt) (b freevar2
,var2
) (x varp2
,var2
))
576 ((coeffpt) (a freevar2
,var2
)))))
578 ;; This is the pattern (a*x+b)*(c*x+d)
579 ;; NOTE: This doesn't seem to be used anywhere in Maxima.
580 (defun m2-a*x
+b
/c
*x
+d
(expr)
584 ((coeffpt) (a freevar
) (var varp
))
585 ((coeffpt) (b freevar
)))
587 ((coeffpt) (c freevar
) (var varp
))
588 ((coeffpt) (d freevar
))))))
591 (member x
'(%sin %cos %sec %tan %csc %cot
) :test
#'eq
))
593 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
596 ;;; Implementation of Method 1: Integrate a sum
598 ;;after finding a non-integrable summand usually better to pass rest to risch
599 (defun integrate1 (expr var2
)
600 (do ((terms expr
(cdr terms
)) (ans))
601 ((null terms
) (addn ans nil
))
602 (let ($liflag
) ; don't gen li's for
603 (push (integrator (car terms
) var2
) ans
)) ; parts of integrand
604 (when (and (not *in-risch-p
*) ; Not called from rischint
605 (not (free (car ans
) '%integrate
))
607 (return (addn (cons (rischint (cons '(mplus) terms
) var2
) (cdr ans
))
610 (defun scep (expr var2
&aux trigl ex
) ; Product of SIN, COS, EXP
611 (and (mtimesp expr
) ; of linear args.
612 (loop for fac in
(cdr expr
) do
613 (cond ((atom fac
) (return nil
))
615 (if (linearp (cadr fac
) var2
) (push fac trigl
)
619 (linearp (caddr fac
) var2
))
620 ;; should be only one exponential factor
623 finally
(return (cons ex trigl
)))))
625 ;; Integrates exponential * sin or cos, all with linear args.
627 (defun sce-int (expr s-c var2
) ; EXP is non-trivial
628 (let* ((e-coef (car (islinear (caddr expr
) var2
)))
629 (sc-coef (car (islinear (cadr s-c
) var2
)))
631 (abs-val (add (power e-coef
2) (power sc-coef
2))))
633 ;; The numerator is zero. Exponentialize the integrand and try again.
634 (integrator ($exponentialize
(mul expr s-c
)) var2
)
635 (mul (div expr abs-val
)
636 (add (mul e-coef s-c
)
637 (if (eq (caar s-c
) '%sin
)
638 (mul* (neg sc-coef
) `((%cos
) ,sc-arg
))
639 (mul* sc-coef
`((%sin
) ,sc-arg
))))))))
641 (defun checkderiv (expr var2
)
642 (checkderiv1 (cadr expr
) (cddr expr
) () var2
))
644 ;; CHECKDERIV1 gets called on the expression being differentiated,
645 ;; an alternating list of variables being differentiated with
646 ;; respect to and powers thereof, and a reversed list of the latter
647 ;; that have already been examined. It returns either the antiderivative
648 ;; or (), saying this derivative isn't wrt the variable of integration.
650 (defun checkderiv1 (expr wrt old-wrt var2
)
651 (cond ((varp2 (car wrt
) var2
)
652 (if (equal (cadr wrt
) 1) ;Power = 1?
653 (if (null (cddr wrt
)) ;single or partial
656 `((%derivative
), expr
;partial in old-wrt
657 ,.
(nreverse old-wrt
)))
658 `((%derivative
) ,expr
;Partial, return rest
661 `((%derivative
) ,expr
;Higher order, reduce order
663 ,(car wrt
) ,(add* (cadr wrt
) -
1)
665 ((null (cddr wrt
)) () ) ;Say it doesn't apply here
666 (t (checkderiv1 expr
(cddr wrt
) ;Else we check later terms
667 (list* (cadr wrt
) (car wrt
) old-wrt
)
670 (defun integrallookups (expr var2
)
671 (let (form dummy-args real-args
)
673 ((eq (caar expr
) 'mqapply
)
674 ;; Transform to functional form and try again.
676 ;; ((MQAPPLY SIMP) (($PSI SIMP ARRAY) 1) $X)
678 (integrallookups `((,(caaadr expr
)) ,@(cdadr expr
) ,@(cddr expr
))
681 ;; Lookup algorithm for integral of a special function.
682 ;; The integral form is put on the property list, and can be a
683 ;; lisp function of the args. If the form is nil, or evaluates
684 ;; to nil, then return noun form unevaluated.
685 ((and (not (atom (car expr
)))
686 (setq form
(get (caar expr
) 'integral
))
687 (setq dummy-args
(car form
))
688 (setq real-args
(cdr expr
))
689 ;; search through the args of expr and find the arg containing var2
690 ;; look up the integral wrt this arg from form
692 (do ((x real-args
(cdr x
))
693 (y (cdr form
) (cdr y
)))
694 ((or (null x
) (null y
)) nil
)
695 (if (not (freevar2 (car x
) var2
)) (return (car y
)))))
696 ;; If form is a function then evaluate it with actual args
697 (or (not (functionp form
))
698 (setq form
(apply form real-args
))))
699 (when *debug-integrate
*
700 (format t
"~&INTEGRALLOOKUPS: Found integral ~A.~%" (caar expr
)))
701 (substitutel real-args dummy-args form
))
703 ((eq (caar expr
) 'mplus
)
704 (muln (list '((rat simp
) 1 2) expr expr
) nil
))
708 ;; Define the antiderivatives of some elementary special functions.
709 ;; This may not be the best place for this definition, but it is close
710 ;; to the original code.
711 ;; Antiderivatives that depend on the logabs flag are defined further below.
712 (defprop %log
((x) ((mplus) ((mtimes) x
((%log
) x
)) ((mtimes) -
1 x
))) integral
)
713 (defprop %sin
((x) ((mtimes) -
1 ((%cos
) x
))) integral
)
714 (defprop %cos
((x) ((%sin
) x
)) integral
)
715 (defprop %sinh
((x) ((%cosh
) x
)) integral
)
716 (defprop %cosh
((x) ((%sinh
) x
)) integral
)
717 ;; No need to take logabs into account for tanh(x), because cosh(x) is positive.
718 (defprop %tanh
((x) ((%log
) ((%cosh
) x
))) integral
)
719 (defprop %sech
((x) ((%atan
) ((%sinh
) x
))) integral
)
720 (defprop %asin
((x) ((mplus) ((mexpt) ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2))) ((rat) 1 2)) ((mtimes) x
((%asin
) x
)))) integral
)
721 (defprop %acos
((x) ((mplus) ((mtimes) -
1 ((mexpt) ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2))) ((rat) 1 2))) ((mtimes) x
((%acos
) x
)))) integral
)
722 (defprop %atan
((x) ((mplus) ((mtimes) x
((%atan
) x
)) ((mtimes) ((rat) -
1 2) ((%log
) ((mplus) 1 ((mexpt) x
2)))))) integral
)
723 (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
)
724 (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
)
725 (defprop %acot
((x) ((mplus) ((mtimes) x
((%acot
) x
)) ((mtimes) ((rat) 1 2) ((%log
) ((mplus) 1 ((mexpt) x
2)))))) integral
)
726 (defprop %asinh
((x) ((mplus) ((mtimes) -
1 ((mexpt) ((mplus) 1 ((mexpt) x
2)) ((rat) 1 2))) ((mtimes) x
((%asinh
) x
)))) integral
)
727 (defprop %acosh
((x) ((mplus) ((mtimes) -
1 ((mexpt) ((mplus) -
1 ((mexpt) x
2)) ((rat) 1 2))) ((mtimes) x
((%acosh
) x
)))) integral
)
728 (defprop %atanh
((x) ((mplus) ((mtimes) x
((%atanh
) x
)) ((mtimes) ((rat) 1 2) ((%log
) ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2))))))) integral
)
729 (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
)
730 (defprop %asech
((x) ((mplus) ((mtimes) -
1 ((%atan
) ((mexpt) ((mplus) -
1 ((mexpt) x -
2)) ((rat) 1 2)))) ((mtimes) x
((%asech
) x
)))) integral
)
731 (defprop %acoth
((x) ((mplus) ((mtimes) x
((%acoth
) x
)) ((mtimes) ((rat) 1 2) ((%log
) ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2))))))) integral
)
733 ;; Define a little helper function to be used in antiderivatives.
734 ;; Depending on the logabs flag, it either returns log(x) or log(abs(x)).
735 (defun log-or-logabs (x)
736 (take '(%log
) (if $logabs
(take '(mabs) x
) x
)))
738 ;; Define the antiderivative of tan(x), taking logabs into account.
739 (defun integrate-tan (x)
740 (log-or-logabs (take '(%sec
) x
)))
741 (putprop '%tan
`((x) ,'integrate-tan
) 'integral
)
743 ;; ... the same for csc(x) ...
744 (defun integrate-csc (x)
745 (mul -
1 (log-or-logabs (add (take '(%csc
) x
) (take '(%cot
) x
)))))
746 (putprop '%csc
`((x) ,'integrate-csc
) 'integral
)
748 ;; ... the same for sec(x) ...
749 (defun integrate-sec (x)
750 (log-or-logabs (add (take '(%sec
) x
) (take '(%tan
) x
))))
751 (putprop '%sec
`((x) ,'integrate-sec
) 'integral
)
753 ;; ... the same for cot(x) ...
754 (defun integrate-cot (x)
755 (log-or-logabs (take '(%sin
) x
)))
756 (putprop '%cot
`((x) ,'integrate-cot
) 'integral
)
758 ;; ... the same for coth(x) ...
759 (defun integrate-coth (x)
760 (log-or-logabs (take '(%sinh
) x
)))
761 (putprop '%coth
`((x) ,'integrate-coth
) 'integral
)
763 ;; ... the same for csch(x) ...
764 (defun integrate-csch (x)
765 (log-or-logabs (take '(%tanh
) (mul '((rat simp
) 1 2) x
))))
766 (putprop '%csch
`((x) ,'integrate-csch
) 'integral
)
768 ;; integrate(x^n,x) = if n # -1 then x^(n+1)/(n+1) else log-or-logabs(x).
769 (defun integrate-mexpt-1 (x n
)
770 (let ((n-is-minus-one ($askequal n -
1)))
771 (cond ((eq '$yes n-is-minus-one
)
775 (div (take '(mexpt) x n
) n
)))))
777 ;; integrate(a^x,x) = a^x/log(a).
778 (defun integrate-mexpt-2 (a x
)
779 (div (take '(mexpt) a x
) (take '(%log
) a
)))
781 (putprop 'mexpt
`((x n
) ,'integrate-mexpt-1
,'integrate-mexpt-2
) 'integral
)
783 (defun integrate5 (ex var2
)
786 (integrator ex var2
)))
789 (cond ((ratnump x
) (caddr x
))
790 ((not (numberp x
)) nil
)
792 (t (cdr (maxima-rationalize x
)))))
794 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
797 ;;; Implementation of Method 1: Elementary function of exponentials
799 ;;; The following examples are integrated with this method:
801 ;;; integrate(exp(x)/(2+3*exp(2*x)),x)
802 ;;; integrate(exp(x+1)/(1+exp(x)),x)
803 ;;; integrate(10^x*exp(x),x)
805 (let ((base nil
) ; The common base.
806 (pow nil
) ; The common power of the form b*x+a. The values are
807 ; stored in a list which is returned from m2.
808 (exptflag nil
)) ; When T, the substitution is not possible.
810 (defun superexpt (expr var2 bas1 pow1
)
811 (prog (y ($logabs nil
) (new-var (gensym "NEW-VAR-")))
812 (putprop new-var t
'internal
)
816 ;; Transform the integrand. At this point resimplify, because it is not
817 ;; guaranteed, that a correct simplified expression is returned.
818 ;; Use a new variable to prevent facts on the old variable to be wrongly used.
819 (setq y
(resimplify (maxima-substitute new-var var2
(elemxpt expr var2
))))
820 (when exptflag
(return nil
))
821 ;; Integrate the transformed integrand and substitute back.
824 (substint (list '(mexpt) base
825 (list '(mplus) (cdras 'a pow
)
826 (list '(mtimes) (cdras 'b pow
) var2
)))
831 (take '(%log
) base
)))
836 ;; Transform expressions like g^(b*x+a) to the common base base and
837 ;; do the substitution y = base^(b*x+a) in the expr.
838 (defun elemxpt (expr var2
&aux w
)
839 (cond ((freevar2 expr var2
) expr
)
840 ;; var2 is the base of a subexpression. The transformation fails.
841 ((atom expr
) (setq exptflag t
))
842 ((not (eq (caar expr
) 'mexpt
))
844 (mapcar #'(lambda (c)
847 ((not (freevar2 (cadr expr
) var2
))
849 (elemxpt (cadr expr
) var2
)
850 (elemxpt (caddr expr
) var2
)))
851 ;; Transform the expression to the common base.
852 ((not (eq (cadr expr
) base
))
853 (elemxpt (list '(mexpt)
855 (mul (power (take '(%log
) base
) -
1)
856 (take '(%log
) (cadr expr
))
859 ;; The exponent must be linear in the variable of integration.
860 ((not (setq w
(m2-b*x
+a
(caddr expr
) var2
)))
861 (list (car expr
) base
(elemxpt (caddr expr
) var2
)))
862 ;; Do the substitution y = g^(b*x+a).
864 (setq w
(cons (cons 'bb
(cdras 'b pow
)) w
))
865 (setq w
(cons (cons 'aa
(cdras 'a pow
)) w
))
866 (setq w
(cons (cons 'base base
) w
))
867 (subliss w
'((mtimes)
872 ((mtimes) -
1 aa b
) bb
))
875 ((mquotient) b bb
)))))))
878 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
881 ;;; Implementation of Method 3:
882 ;;; Substitution for a rational root of a linear fraction of x
884 ;;; This method is applicable when the integrand is of the form:
887 ;;; [ a x + b n1/m1 a x + b n1/m2
888 ;;; I R(x, (-------) , (-------) , ...) dx
889 ;;; ] c x + d c x + d
894 ;;; (1) t = ((a*x+b)/(c*x+d))^(1/k), or
896 ;;; (2) x = (b-d*t^k)/(c*t^k-a)
898 ;;; where k is the least common multiplier of m1, m2, ... and
900 ;;; (3) dx = k*(a*d-b*c)*t^(k-1)/(a-c*t^k)^2 * dt
902 ;;; First, the algorithm calls the routine RAT3 to collect the roots of the
903 ;;; form ((a*x+b)/(c*x+d))^(n/m) in the list ROOTLIST.
904 ;;; search for the least common multiplier of m1, m2, ... then the
905 ;;; substitutions (2) and (3) are done and the new problem is integrated.
906 ;;; As always, W is an alist which associates to the coefficients
907 ;;; a, b... (and to VAR) their values.
909 ;; ratroot2 is an expression of the form (a*x+b)/(c*x+d)
910 (defun ratroot (expr var2 ratroot2 w
)
911 (prog (rootlist k y w1
)
912 ;; List of powers of the expression ratroot2.
914 ;; Check if the integrand has a chebyform, if so return the result.
915 (when (setq y
(chebyf expr var2
)) (return y
))
916 ;; Check if the integrand has a suitably form and collect the roots
917 ;; in the global special variable *ROOTLIST*.
919 ((rat3 (ex ind var2 ratroot2
)
920 (cond ((freevar2 ex var2
) t
)
922 ((member (caar ex
) '(mtimes mplus
) :test
#'eq
)
923 (do ((u (cdr ex
) (cdr u
)))
925 (if (not (rat3 (car u
) ind var2 ratroot2
))
927 ((not (eq (caar ex
) 'mexpt
))
928 (rat3 (car (margs ex
)) t var2 ratroot2
))
929 ((freevar2 (cadr ex
) var2
)
930 (rat3 (caddr ex
) t var2 ratroot2
))
931 ((integerp (caddr ex
))
932 (rat3 (cadr ex
) ind var2 ratroot2
))
933 ((and (m2 (cadr ex
) ratroot2
)
934 (denomfind (caddr ex
)))
935 (setq rootlist
(cons (denomfind (caddr ex
)) rootlist
)))
936 (t (rat3 (cadr ex
) nil var2 ratroot2
)))))
937 (unless (rat3 expr t var2 ratroot2
) (return nil
)))
938 ;; Get the least common multiplier of m1, m2, ...
939 (setq k
(apply #'lcm rootlist
))
940 (setq w1
(cons (cons 'k k
) w
))
941 ;; Substitute for the roots.
946 ((mplus) ((mtimes) b e
)
947 ((mtimes) -
1 d
((mexpt) ,var2 k
)))
948 ((mplus) ((mtimes) c
((mexpt) ,var2 k
))
953 ;; Integrate the new problem.
962 ((mexpt) ,var2
((mplus) -
1 k
)))
965 ((mexpt) ,var2
((mplus) -
1 k
))))))
967 ((mtimes) c
((mexpt) ,var2 k
))
971 ;; Substitute back and return the result.
972 (return (substint (power ratroot2
(power k -
1)) var2 y var2 expr
))))
974 (let ((rootform nil
) ; Expression of the form x = (b*e-d*t^k)/(c*t^k-e*a).
975 (rootvar nil
)) ; The variable we substitute for the root.
977 (defun subst4 (ex k ratroot2
)
978 (cond ((freevar2 ex rootvar
)
979 ;; SUBST4 is called from SUBST41 with ROOTVAR equal to VAR
980 ;; (from RATROOT). Hence we can use FREEVAR2 to see if EX
981 ;; is free of ROOTVAR instead of using FREEVAR.
984 ((not (eq (caar ex
) 'mexpt
))
985 (mapcar #'(lambda (u) (subst4 u k ratroot2
)) ex
))
986 ((m2 (cadr ex
) ratroot2
)
987 (list (car ex
) rootvar
(integerp2 (timesk k
(caddr ex
)))))
988 (t (list (car ex
) (subst4 (cadr ex
) k ratroot2
) (subst4 (caddr ex
) k ratroot2
)))))
990 (defun subst41 (expr a b k ratroot2
)
991 ;; Note: SUBST41 is only called from RATROOT, and the arg B is VAR.
994 ;; At this point resimplify, because it is not guaranteed, that a correct
995 ;; simplified expression is returned.
996 (resimplify (subst4 expr k ratroot2
)))
999 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1002 ;;; Implementation of Method 4: Binomial Chebyschev
1004 ;; exp = a*t^r1*(c1+c2*t^q)^r2, where var2 = t.
1006 ;; G&S 2.202 has says this integral can be expressed by elementary
1009 ;; 1. q is an integer
1010 ;; 2. (r1+1)/q is an integer
1011 ;; 3. (r1+1)/q+r2 is an integer.
1013 ;; I (rtoy) think that for this code to work, r1, r2, and q must be numbers.
1014 (defun chebyf (expr var2
)
1015 (prog (r1 r2 d1 d2 n1 n2 w q
)
1016 ;; Return NIL if the expression doesn't match.
1017 (when (not (setq w
(m2-chebyform expr var2
)))
1020 (format t
"w = ~A~%" w
)
1021 (when (zerop1 (cdr (assoc 'c1 w
:test
#'eq
)))
1022 ;; rtoy: Is it really possible to be in this routine with c1 =
1026 ;; This factor is locally constant as long as t and
1027 ;; c2*t^q avoid log's branch cut.
1028 (subliss w
`((mtimes) a
((mexpt) ,var2
((mtimes) -
1 q r2
))
1029 ((mexpt) ((mtimes) c2
((mexpt) ,var2 q
)) r2
)))
1031 (subliss w
`((mexpt) ,var2
((mplus) r1
((mtimes) q r2
))))
1033 (setq q
(cdr (assoc 'q w
:test
#'eq
)))
1034 ;; Reset parameters. a = a/q, r1 = (1 - q + r1)/q
1036 (list* (cons 'a
(div* (cdr (assoc 'a w
:test
#'eq
)) q
))
1039 (div* (addn (list 1 (neg (simplify q
)) (cdr (assoc 'r1 w
:test
#'eq
))) nil
) q
))
1042 (format t
"new w = ~A~%" w
)
1043 (setq r1
(cdr (assoc 'r1 w
:test
#'eq
))
1044 r2
(cdr (assoc 'r2 w
:test
#'eq
)))
1047 (format t
"new r1 = ~A~%" r1
)
1048 (format t
"r2 = ~A~%" r2
))
1049 ;; Write r1 = d1/n1, r2 = d2/n2, if possible. Update w with
1050 ;; these values, if so. If we can't, give up. I (rtoy) think
1051 ;; this only happens if r1 or r2 can't be expressed as rational
1052 ;; numbers. Hence, r1 and r2 have to be numbers, not variables.
1054 ((not (and (setq d1
(denomfind r1
))
1055 (setq d2
(denomfind r2
))
1056 (setq n1
(integerp2 (timesk r1 d1
)))
1057 (setq n2
(integerp2 (timesk r2 d2
)))
1058 (setq w
(list* (cons 'd1 d1
) (cons 'd2 d2
)
1059 (cons 'n1 n1
) (cons 'n2 n2
)
1063 (format t
"cheby can't find one of d1,d2,n1,n2:~%")
1064 (format t
" d1 = ~A~%" d1
)
1065 (format t
" d2 = ~A~%" d2
)
1066 (format t
" n1 = ~A~%" n1
)
1067 (format t
" n2 = ~A~%" n2
))
1069 ((and (integerp2 r1
) (> r1
0))
1070 #+nil
(format t
"integer r1 > 0~%")
1071 ;; (r1+q-1)/q is positive integer.
1073 ;; I (rtoy) think we are using the substitution z=(c1+c2*t^q).
1074 ;; Maxima thinks the resulting integral should then be
1076 ;; a/q*c2^(-r1/q-1/q)*integrate(z^r2*(z-c1)^(r1/q+1/q-1),z)
1080 (subliss w
`((mplus) c1
((mtimes) c2
((mexpt) ,var2 q
))))
1083 (expands (list (subliss w
1084 ;; a*t^r2*c2^(-r1-1)
1095 (expandexpt (subliss w
1104 #+nil
(format t
"integer r2~%")
1105 ;; I (rtoy) think this is using the substitution z = t^(q/d1).
1107 ;; The integral (as maxima will tell us) becomes
1109 ;; a*d1/q*integrate(z^(n1/q+d1/q-1)*(c1+c2*z^d1)^r2,z)
1111 ;; But be careful because the variable A in the code is
1114 (substint (subliss w
`((mexpt) ,var2
((mquotient) q d1
)))
1116 (ratint (simplify (subliss w
1134 ((and (integerp2 r1
) (< r1
0))
1135 #+nil
(format t
"integer r1 < 0~%")
1136 ;; I (rtoy) think this is using the substitution
1138 ;; z = (c1+c2*t^q)^(1/d2)
1140 ;; With this substitution, maxima says the resulting integral
1143 ;; a/q*c2^(-r1/q-1/q)*d2*
1144 ;; integrate(z^(n2+d2-1)*(z^d2-c1)^(r1/q+1/q-1),z)
1146 (substint (subliss w
1147 ;; (c1+c2*t^q)^(1/d2)
1151 ((mtimes) c2
((mexpt) ,var2 q
)))
1152 ((mquotient) 1 d2
)))
1154 (ratint (simplify (subliss w
1155 ;; This is essentially
1156 ;; the integrand above,
1157 ;; except A and R1 here
1158 ;; are not the same as
1181 ((integerp2 (add* r1 r2
))
1182 #+nil
(format t
"integer r1+r2~%")
1183 ;; If we're here, (r1-q+1)/q+r2 is an integer.
1185 ;; I (rtoy) think this is using the substitution
1187 ;; z = ((c1+c2*t^q)/t^q)^(1/d1)
1189 ;; With this substitution, maxima says the resulting integral
1192 ;; a*d2/q*c1^(r2+r1/q+1/q)*
1193 ;; integrate(z^(d2*r2+d2-1)*(z^d2-c2)^(-r2-r1/q-1/q-1),z)
1195 (substint (let (($radexpand
'$all
))
1196 ;; Setting $radexpand to $all here gets rid of
1197 ;; ABS in the substitution. I think that's ok in
1198 ;; this case. See Bug 1654183.
1204 ((mtimes) c2
((mexpt) ,var2 q
)))
1206 ((mquotient) 1 d1
))))
1208 (ratint (simplify (subliss w
1233 (t (return (list '(%integrate
) expr var2
))))))
1235 (defun greaterratp (x1 x2
)
1236 (cond ((and (numberp x1
) (numberp x2
))
1239 (greaterratp (quotient (float (cadr x1
))
1244 (quotient (float (cadr x2
))
1248 (member (car x
) '(%sin %cos
) :test
#'eq
))
1250 (defun supertrig (expr var2 trigarg
)
1251 (declare (special *notsame
*))
1252 (cond ((freevar2 expr var2
) t
)
1254 ((member (caar expr
) '(mplus mtimes
) :test
#'eq
)
1255 (and (supertrig (cadr expr
) var2 trigarg
)
1256 (or (null (cddr expr
))
1257 (supertrig (cons (car expr
)
1260 ((eq (caar expr
) 'mexpt
)
1261 (and (supertrig (cadr expr
) var2 trigarg
)
1262 (supertrig (caddr expr
) var2 trigarg
)))
1263 ((eq (caar expr
) '%log
)
1264 (supertrig (cadr expr
) var2 trigarg
))
1265 ((member (caar expr
)
1266 '(%sin %cos %tan %sec %cot %csc
) :test
#'eq
)
1267 (cond ((m2 (cadr expr
) trigarg
) t
)
1268 ((m2-b*x
+a
(cadr expr
) var2
)
1269 (and (setq *notsame
* t
) nil
))
1270 (t (supertrig (cadr expr
) var2 trigarg
))))
1271 (t (supertrig (cadr expr
) var2 trigarg
))))
1273 (defun subst2s (ex pat var2
)
1274 (cond ((null ex
) nil
)
1277 (t (cons (subst2s (car ex
) pat var2
)
1278 (subst2s (cdr ex
) pat var2
)))))
1280 ;; Match (c*x+b), where c and b are free of x
1281 (defun simple-trig-arg (expr var2
)
1282 (m2 expr
`((mplus) ((mtimes)
1283 ((coefftt) (c freevar2
,var2
))
1284 ((coefftt) (v varp2
,var2
)))
1285 ((coeffpp) (b freevar2
,var2
)))))
1287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1290 ;;; Implementation of Method 6: Elementary function of trigonometric functions
1292 (defun monstertrig (expr var2 trigarg
)
1293 (prog (*notsame
* w a b y d
)
1294 (declare (special *notsame
*))
1296 ((supertrig expr var2 trigarg
)
1298 ((null *notsame
*) (return nil
))
1299 ;; Check for an expression like a*trig1(m*x)*trig2(n*x),
1300 ;; where trig1 and trig2 are sin or cos.
1301 ((not (setq y
(m2 expr
1303 ((coefftt) (a freevar2
,var2
))
1307 ((coefftt) (m freevar2
,var2
))))
1311 ((coefftt) (n freevar2
,var2
))))))))
1313 ; This check has been done with the pattern match.
1314 ; ((not (and (member (car (setq b (cdr (assoc 'b y :test #'eq)))) '(%sin %cos) :test #'eq)
1315 ; (member (car (setq d (cdr (assoc 'd y :test #'eq)))) '(%sin %cos) :test #'eq)))
1318 ;; The tests after this depend on values of b and d being
1319 ;; set. Set them here unconditionally, before doing the
1321 (setq b
(cdras 'b y
))
1322 (setq d
(cdras 'd y
))
1323 (and (eq (car b
) '%sin
)
1324 (eq (car d
) '%sin
)))
1325 ;; We have a*sin(m*x)*sin(n*x).
1328 ;; a*(sin((m-n)*x)/(2*(m-n))-sin((m+n)*x)/(2*(m+n)). But if n
1329 ;; = m, the integral is x/2-sin(2*n*x)/(4*n).
1330 (let ((n (cdras 'n y
))
1333 ((eq ($askequal n m
) '$yes
)
1334 ;; n=m, so we have the integral of a*sin(n*x)^2 which is
1341 ((%sin
) ((mtimes) 2 n x
))
1342 ((mtimes) 4 n
))))))))
1348 ((%sin
) ((mtimes) ((mplus) m
((mtimes) -
1 n
)) x
))
1349 ((mtimes) 2 ((mplus) m
((mtimes) -
1 n
))))
1352 ((%sin
) ((mtimes) ((mplus) m n
) x
))
1353 ((mtimes) 2 ((mplus) m n
))))))))))))
1354 ((and (eq (car b
) '%cos
) (eq (car d
) '%cos
))
1355 ;; We have a*cos(m*x)*cos(n*x).
1358 ;; a*(sin((m-n)*x)/(2*(m-n))+sin((m+n)*x)/(2*(m+n)). But when
1359 ;; n = m, the integral is sin(2*m*x)/(4*m)+x/2.
1360 (let ((n (cdras 'n y
))
1363 ((eq ($askequal n m
) '$yes
)
1368 ((%sin
) ((mtimes) 2 n x
))
1377 ((%sin
) ((mtimes) ((mplus) m
((mtimes) -
1 n
)) x
))
1379 ((mplus) m
((mtimes) -
1 n
))))
1381 ((%sin
) ((mtimes) ((mplus) m n
) x
))
1382 ((mtimes) 2 ((mplus) m n
)))))))))))
1383 ((or (and (eq (car b
) '%cos
)
1384 ;; The following (destructively!) swaps the values of
1385 ;; m and n if first trig term is sin. I (rtoy) don't
1386 ;; understand why this is needed. The formula
1387 ;; doesn't depend on that.
1388 (setq w
(cdras 'm y
))
1389 (rplacd (assoc 'm y
) (cdras 'n y
))
1390 (rplacd (assoc 'n y
) w
))
1392 ;; We have a*cos(n*x)*sin(m*x).
1395 ;; -a*(cos((m-n)*x)/(2*(m-n))+cos((m+n)*x)/(2*(m+n)). But
1396 ;; if n = m, the integral is -cos(n*x)^2/(2*n).
1397 (let ((n (cdras 'n y
))
1400 ((eq ($askequal n m
) '$yes
)
1401 ;; This needs work. For example
1402 ;; integrate(cos(m*x)*sin(2*m*x),x). We ask if 2*m = m.
1403 ;; If the answer is yes, we return -cos(m*x)^2/(2*m).
1404 ;; But if 2*m=m, then m=0 and the integral must be 0.
1409 ((%cos
) ((mtimes) n x
))
1417 ((%cos
) ((mtimes) ((mplus) m
((mtimes) -
1 n
)) x
))
1418 ((mtimes) 2 ((mplus) m
((mtimes) -
1 n
))))
1420 ((%cos
) ((mtimes) ((mplus) m n
) x
))
1421 ((mtimes) 2 ((mplus) m n
))))))))))))
1422 b
;; At this point we have trig functions with different arguments,
1423 ;; but not a product of sin and cos.
1424 (cond ((not (setq y
(prog2
1428 ((coefftt) (a freevar2
,var2
))
1432 ((coefftt) (n integerp2
))))
1433 ((coefftt) (c supertrig
,var2
,trigarg
)))))))
1435 ;; We have a product of trig functions: trig1(n*x)*trig2(y).
1436 ;; trig1 is sin or cos, where n is a numerical integer. trig2 is not a sin
1437 ;; or cos. The cos or sin function is expanded.
1442 (cdras 'a y
) ; constant factor
1443 (cdras 'c y
) ; trig functions
1444 (cond ((eq (car (cdras 'b y
)) '%cos
) ; expand cos(n*x)
1445 (maxima-substitute var2
1447 (supercosnx (cdras 'n y
))))
1448 (t ; expand sin(x*x)
1449 (maxima-substitute var2
1451 (supersinx (cdras 'n y
)))))))
1453 a
;; A product of trig functions and all trig functions have the same
1454 ;; argument trigarg. Maxima substitutes trigarg with the variable var2
1455 ;; of integration and calls trigint to integrate the new problem.
1456 (setq w
(subst2s expr trigarg var2
))
1457 (setq b
(cdras 'b
(m2-b*x
+a trigarg var2
)))
1458 (setq a
(substint trigarg var2
(trigint (div* w b
) var2
) var2 expr
))
1459 (return (if (isinop a
'%integrate
)
1460 (list '(%integrate
) expr var2
)
1464 (member (car x
) '(%sin %cos %tan %cot %sec %csc
) :test
#'eq
))
1466 ;; sin(n*x) for integer n /= 0. Result not simplified.
1467 (defun supersinx (n)
1468 (let ((i (if (< n
0) -
1 1)))
1469 ($expand
(list '(mtimes) i
(sinnx (timesk i n
))))))
1471 ;; cos(n*x) for integer n /= 0. Result not simplified.
1472 (defun supercosnx (n)
1473 ($expand
(cosnx (timesk (if (< n
0) -
1 1) n
))))
1475 ;; sin(n*x) for integer n >= 1. Result is not simplified.
1480 (list '(mtimes) '((%sin
) x
) (cosnx (1- n
)))
1481 (list '(mtimes) '((%cos
) x
) (sinnx (1- n
))))))
1483 ;; cos(n*x) for integer n >= 1. Result is not simplified.
1488 (list '(mtimes) '((%cos
) x
) (cosnx (1- n
)))
1489 (list '(mtimes) -
1 '((%sin
) x
) (sinnx (1- n
))))))
1492 (and (even x
) (> x -
1)))
1496 (not (member x
'(sin* cos
* sec
* tan
*) :test
#'eq
))
1497 (and (trigfree (car x
)) (trigfree (cdr x
)))))
1499 (defun rat1 (expr aa bb cc
)
1500 (prog (b1 *notsame
*)
1501 (declare (special *yy
* *notsame
*))
1502 (when (and (numberp expr
) (zerop expr
))
1504 (setq b1
(subst bb
'b
'((mexpt) b
(n even
))))
1506 (setq *yy
* (rats expr aa b1 cc
))
1507 (cond ((not *notsame
*) *yy
*))))))
1509 (defun rats (expr aa b1 cc
)
1511 (declare (special *notsame
*))
1513 (cond ((eq expr aa
) 'x
)
1515 (cond ((member expr
'(sin* cos
* sec
* tan
*) :test
#'eq
)
1518 ((setq y
(m2 expr b1
))
1520 (t (cons (car expr
) (mapcar #'(lambda (g) (rats g aa b1 cc
))
1524 (maxima-substitute cc
1526 (maxima-substitute (quotient (cdr (assoc 'n y
:test
#'eq
)) 2)
1537 (declare (special *yz
*))
1538 (cond ((not (numberp n
)) nil
)
1539 ((not (equal (rem n
2) 0))
1541 (maxima-substitute cc
1544 '((mplus) 1 ((mtimes) c
((mexpt) x
2)))
1545 (quotient (1- n
) 2)))))
1548 (defun subvar (x var2
)
1549 (maxima-substitute var2
'x x
))
1551 (defun subvardlg (x var2
)
1552 (mapcar #'(lambda (m)
1553 (cons (maxima-substitute var2
'x
(car m
)) (cdr m
)))
1556 ;; This appears to be the implementation of Method 6, pp.82 in Moses' thesis.
1558 (defun trigint (expr var2
)
1559 (prog (y repl y1 y2
*yy
* z m n
*yz
*)
1560 (declare (special *yy
* *yz
*))
1561 ;; Transform trig(x) into trig* (for simplicity?) Convert cot to
1562 ;; tan and csc to sin.
1564 (subliss (subvardlg '((((%sin
) x
) . sin
*)
1567 (((%cot
) x
) .
((mexpt) tan
* -
1))
1569 (((%csc
) x
) .
((mexpt) sin
* -
1)))
1573 (when *debug-integrate
*
1574 (format t
"~& in TRIGINT:~%")
1575 (format t
"~& : y2 = ~A~%" y2
))
1577 ;; Now transform tan to sin/cos and sec to 1/cos.
1578 (setq y1
(setq y
(subliss '((tan* .
((mtimes) sin
*
1580 (sec* .
((mexpt) cos
* -
1)))
1583 (when *debug-integrate
* (format t
"~& : y = ~A~%" y
))
1588 ((coefftt) (b trigfree
))
1589 ((mexpt) sin
* (m poseven
))
1590 ((mexpt) cos
* (n poseven
))))))
1591 ;; Go if y is not of the form sin^m*cos^n for positive even m and n.
1595 ;; Handle the case of sin^m*cos^n, m, n both non-negative and even.
1597 (setq m
(cdras 'm z
))
1598 (setq n
(cdras 'n z
))
1599 (let ((aa (integerp2 (* 0.5 (if (< m n
) 1 -
1) (+ n
(* -
1 m
))))))
1600 (setq z
(cons (cons 'a aa
) z
))
1601 (setq z
(cons (cons 'x var2
) z
))
1603 (when *debug-integrate
*
1604 (format t
"~& CASE III:~%")
1605 (format t
"~& : m, n = ~A ~A~%" m n
)
1606 (format t
"~& : a = ~A~%" aa
)
1607 (format t
"~& : z = ~A~%" z
)))
1609 ;; integrate(sin(y)^m*cos(y)^n,y) is transformed to the following form:
1611 ;; m < n: integrate((sin(2*y)/2)^n*(1/2+1/2*cos(2*y)^((n-m)/2),y)
1612 ;; m >= n: integrate((sin(2*y)/2)^n*(1/2-1/2*cos(2*y)^((m-n)/2),y)
1624 ((mtimes) ((rat simp
) 1 2) ((%sin
) x
))
1630 ((rat simp
) 1 2) ((%cos
) x
))) a
))))
1635 ((mtimes) ((rat simp
) 1 2) ((%sin
) x
))
1648 ;; I think this is case IV, working on the expression in terms of
1651 ;; Elem(x) means constants, x, trig functions of x, log and
1652 ;; inverse trig functions of x, and which are closed under
1653 ;; addition, multiplication, exponentiation, and substitution.
1655 ;; Elem(f(x)) is the same as Elem(x), but f(x) replaces x in the
1658 (when *debug-integrate
* (format t
"~& Case IV:~%"))
1660 (when (and (m2 y
'((coeffpt) (c rat1 sin
* cos
* -
1) ((mexpt) cos
* (n odd1 -
1))))
1661 (setq repl
(list '(%sin
) var2
)))
1662 ;; The case cos^(2*n+1)*Elem(cos^2,sin). Use the substitution z = sin.
1665 (when (and (m2 y
'((coeffpt) (c rat1 cos
* sin
* -
1) ((mexpt) sin
* (n odd1 -
1))))
1666 (setq repl
(list '(%cos
) var2
)))
1667 ;; The case sin^(2*n+1)*Elem(sin^2,cos). Use the substitution z = cos.
1671 ;; Transform sin and cos to tan and sec to see if the integral is
1672 ;; of the form Elem(tan, sec^2). If so, use the substitution z = tan.
1674 (when *debug-integrate
* (format t
"~& Case V:~%"))
1676 (setq y
(subliss '((sin* (mtimes) tan
* ((mexpt) sec
* -
1))
1677 (cos* (mexpt) sec
* -
1))
1679 (when (and (rat1 y
'tan
* 'sec
* 1) (setq repl
(list '(%tan
) var2
)))
1682 (when (and (m2 y
'((coeffpt) (c rat1 sec
* tan
* 1) ((mexpt) tan
* (n odd1
1))))
1683 (setq repl
(list '(%sec
) var2
)))
1685 (when (not (alike1 (setq repl
($expand expr
)) expr
))
1686 (return (integrator repl var2
)))
1687 (setq y
(subliss '((sin* (mtimes) 2 x
1688 ((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1))
1690 ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2)))
1691 ((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1)))
1693 (setq y
(list '(mtimes)
1695 '((mtimes) 2 ((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1))))
1696 (setq repl
(subvar '((mquotient) ((%sin
) x
) ((mplus) 1 ((%cos
) x
)))
1700 (setq y
(list '(mtimes) -
1 *yy
* *yz
*))
1703 (setq y
(list '(mtimes) '((mexpt) ((mplus) 1 ((mexpt) x
2)) -
1) *yy
*))
1706 (setq y
(list '(mtimes) *yy
* *yz
*))
1708 (when *debug-integrate
*
1709 (format t
"~& Call the INTEGRATOR with:~%")
1710 (format t
"~& : y = ~A~%" y
)
1711 (format t
"~& : repl = ~A~%" repl
))
1712 ;; See Bug 2880797. We want atan(tan(x)) to simplify to x, so
1713 ;; set $triginverses to '$all.
1715 ;; Do not integrate for the global variable VAR, but substitute it.
1716 ;; This way possible assumptions on VAR are no longer present. The
1717 ;; algorithm of DEFINT depends on this behavior. See Bug 3085498.
1718 (let (($triginverses
'$all
) (newvar (gensym)))
1721 (integrator (maxima-substitute newvar
'x y
) newvar
)
1725 (defmvar $integration_constant_counter
0)
1726 (defmvar $integration_constant
'$%c
)
1728 ;; This is the top level of the integrator
1729 (defun sinint (expr var2
)
1730 ;; *integrator-level* is a recursion counter for INTEGRATOR. See
1731 ;; INTEGRATOR for more details. Initialize it here.
1732 (let ((*integrator-level
* 0))
1733 (declare (special *integrator-level
*))
1735 ;; Sanity checks for variables
1737 (merror (intl:gettext
"integrate: variable must not be a number; found: ~:M") var2
))
1738 (when ($ratp var2
) (setf var2
(ratdisrep var2
)))
1739 (when ($ratp expr
) (setf expr
(ratdisrep expr
)))
1742 ;; Distribute over lists and matrices
1745 (mapcar #'(lambda (y) (sinint y var2
)) (cdr expr
))))
1747 ;; The symbolic integration code doesn't really deal very well with
1748 ;; subscripted variables, so if we have one then replace occurrences of var2
1749 ;; with an atomic gensym and recurse.
1750 ((and (not (atom var2
))
1751 (member 'array
(cdar var2
)))
1752 (let ((dummy-var2 (gensym)))
1753 (maxima-substitute var2 dummy-var2
1754 (sinint (maxima-substitute dummy-var2 var2 expr
) dummy-var2
))))
1756 ;; If expr is an equality, integrate both sides and add an integration
1759 (list (car expr
) (sinint (cadr expr
) var2
)
1760 (add (sinint (caddr expr
) var2
)
1761 ($concat $integration_constant
(incf $integration_constant_counter
)))))
1763 ;; If var2 is an atom which occurs as an operator in expr, then return a noun form.
1766 (list '(%integrate
) expr var2
))
1768 ((zerop1 expr
) ;; special case because 0 will not pass sum-of-intsp test
1771 ((let ((ans (simplify
1772 (let ($opsubst varlist genvar
)
1773 (integrator expr var2 nil
)))))
1774 (if (sum-of-intsp ans var2
)
1775 (list '(%integrate
) expr var2
)
1780 ;; This is a heuristic that SININT uses to work out whether the result from
1781 ;; INTEGRATOR is worth returning or whether just to return a noun form. If this
1782 ;; function returns T, then SININT will return a noun form.
1784 ;; The logic, as I understand it (Rupert 01/2014):
1786 ;; (1) If I integrate f(x) wrt x and get an atom other than x or 0, either
1787 ;; something's gone horribly wrong, or this is part of a larger
1788 ;; expression. In the latter case, we can return T here because hopefully
1789 ;; something else interesting will make the top-level return NIL.
1791 ;; (2) If I get a sum, return a noun form if every one of the summands is no
1792 ;; better than what I started with. (Presumably this is where the name
1795 ;; (3) If this is a noun form, it doesn't convey much information on its own,
1798 ;; (4) If this is a product, something interesting has probably happened. But
1799 ;; I still want a noun form if the result is like 2*'integrate(f(x),x), so
1800 ;; I'm only interested in terms in the product that are not free of
1801 ;; VAR. If one of those terms is worthy of report, that's great: return
1802 ;; NIL. Otherwise, return T if we saw at least two things (eg splitting an
1803 ;; integral into a product of two integrals)
1805 ;; (5) If the result is free of VAR, we're in a similar position to (1).
1807 ;; (6) Otherwise something interesting (and hopefully useful) has
1808 ;; happened. Return NIL to tell SININT to report it.
1809 (defun sum-of-intsp (ans var2
)
1811 ;; Result of integration should never be a constant other than zero.
1812 ;; If the result of integration is zero, it is either because:
1813 ;; 1) a subroutine inside integration failed and returned nil,
1814 ;; and (mul 0 nil) yielded 0, meaning that the result is wrong, or
1815 ;; 2) the original integrand was actually zero - this is handled
1816 ;; with a separate special case in sinint
1817 (not (eq ans var2
)))
1818 ((mplusp ans
) (every #'(lambda (e)
1819 (sum-of-intsp e var2
))
1821 ((eq (caar ans
) '%integrate
) t
)
1823 (let ((int-factors 0))
1824 (not (or (dolist (factor (cdr ans
))
1825 (unless (freeof var2 factor
)
1826 (if (sum-of-intsp factor var2
)
1829 (<= 2 int-factors
)))))
1830 ((freeof var2 ans
) t
)
1833 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1836 ;;; Implementation of Method 2: Integrate a summation
1838 (defun intsum (form var2
)
1839 (prog (expr idx ll ul pair val
)
1840 (setq expr
(cadr form
)
1843 ul
(car (cddddr form
)))
1844 (if (or (not (atom var2
))
1845 (not (free idx var2
))
1846 (not (free ll var2
))
1847 (not (free ul var2
)))
1848 (return (list '(%integrate
) form var2
)))
1849 (setq pair
(partition expr var2
1))
1850 (when (and (mexptp (cdr pair
))
1851 (eq (caddr pair
) var2
))
1852 (setq val
(maxima-substitute ll idx
(cadddr pair
)))
1853 (cond ((equal val -
1)
1854 (return (add (integrator (maxima-substitute ll idx expr
) var2
)
1855 (intsum1 expr idx
(add 1 ll
) ul var2
))))
1857 (return (list '(%integrate
) form var2
)))))
1858 (return (intsum1 expr idx ll ul var2
))))
1860 (defun intsum1 (expr idx ll ul var2
)
1861 (assume (list '(mgeqp) idx ll
))
1862 (if (not (eq ul
'$inf
))
1863 (assume (list '(mgeqp) ul idx
)))
1864 (simplifya (list '(%sum
) (integrator expr var2
) idx ll ul
) t
))
1868 (member x
'(%log %integrate %atan
) :test
#'eq
)
1869 (or (finds (car x
)) (finds (cdr x
)))))
1871 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1874 ;;; Implementation of Method 9:
1875 ;;; Rational function times a log or arctric function
1877 ;;; ratlog is called for an expression containing a log or arctrig function
1878 ;;; The integrand is like log(x)*f'(x). To obtain the result the technique of
1879 ;;; partial integration is applied: log(x)*f(x)-integrate(1/x*f(x),x)
1881 ;;; Only called by intform.
1882 (defun ratlog (var2 form
)
1885 (setq b
(cdr (assoc 'b y
:test
#'eq
)))
1886 (setq c
(cdr (assoc 'c y
:test
#'eq
)))
1887 (setq y
(integrator c var2
))
1888 (when (finds y
) (return nil
))
1889 (setq d
(sdiff (cdr (assoc 'a form
:test
#'eq
)) var2
))
1891 (setq z
(integrator (mul2* y d
) var2
))
1892 (setq d
(cdr (assoc 'a form
:test
#'eq
)))
1893 (return (simplify (list '(mplus)
1894 (list '(mtimes) y d
)
1895 (list '(mtimes) -
1 z
))))))
1897 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1899 ;;; partial-integration is an extension of the algorithm of ratlog to support
1900 ;;; the technique of partial integration for more cases. The integrand
1901 ;;; is like g(x)*f'(x) and the result is g(x)*f(x)-integrate(g'(x)*f(x),x).
1903 ;;; Adding integrals properties for elementary functions led to infinite recursion
1904 ;;; with integrate(z*expintegral_shi(z),z). This was resolved by limiting the
1905 ;;; recursion depth. *integrator-level* needs to be at least 3 to solve
1906 ;;; o integrate(expintegral_ei(1/sqrt(x)),x)
1907 ;;; o integrate(sqrt(z)*expintegral_li(z),z)
1908 ;;; while a value of 4 causes testsuite regressions with
1909 ;;; o integrate(z*expintegral_shi(z),z)
1910 (defun partial-integration (form var2
)
1911 (declare (special *integrator-level
*))
1912 (let ((g (cdr (assoc 'a form
))) ; part g(x)
1913 (df (cdr (assoc 'c form
))) ; part f'(x)
1915 (setq f
(integrator df var2
)) ; integrate f'(x) wrt var2
1917 ((or (isinop f
'%integrate
) ; no result or
1918 (isinop f
(caar g
)) ; g in result
1919 (> *integrator-level
* 3))
1920 nil
) ; we return nil
1922 ;; Build the result: g(x)*f(x)-integrate(g'(x)*f(x))
1924 (mul -
1 (integrator (mul f
(sdiff g var2
)) var2
)))))))
1926 ;; returns t if argument of every trig operation in y matches arg
1927 (defun every-trigarg-alike (y arg
)
1929 ((optrig (caar y
)) (alike1 arg
(cadr y
)))
1930 (t (every (lambda (expr)
1931 (every-trigarg-alike expr arg
))
1934 ;; return argument of first trig operation encountered in y
1935 (defun find-first-trigarg (y)
1936 (cond ((atom y
) nil
)
1937 ((optrig (caar y
)) (cadr y
))
1938 (t (some (lambda (expr)
1939 (find-first-trigarg expr
))
1942 ;; return constant factor that makes elements of alist match elements of blist
1943 ;; or nil if no match found
1944 ;; (we could replace this using rat package to divide alist and blist)
1945 (defun matchsum (alist blist var2
)
1947 (setq s
(m2 (car alist
) ;; find coeff for first term of alist
1949 ((coefftt) (a freevar2
,var2
))
1950 ((coefftt) (c true
)))))
1951 (setq cc
(cdr (assoc 'c s
:test
#'eq
)))
1952 (cond ((not (setq r
;; find coeff for first term of blist
1955 (cons `((coefftt) (b free12
,var2
))
1960 (setq dd
(simplify (list '(mtimes)
1965 (cond ((m2 (cons '(mplus) alist
) ;; check that all terms match
1966 (timesloop dd blist
))
1970 (defun timesloop (a b
)
1971 (cons '(mplus) (mapcar #'(lambda (c) (mul2* a c
)) b
)))
1973 (defun expands (arg1 arg2
)
1974 (addn (mapcar #'(lambda (c) (timesloop c arg1
)) arg2
) nil
))
1976 ;; possibly a bug: For var2 = x and dd =3, we have expand(?subst10(x^9 * (x+x^6))) --> x^5+x^4, but
1977 ;; ?subst10(expand(x^9 * (x+x^6))) --> x^5+x^3. (Barton Willis)
1979 (defun subst10 (ex var2 dd
)
1980 (cond ((atom ex
) ex
)
1981 ((and (eq (caar ex
) 'mexpt
) (eq (cadr ex
) var2
))
1982 (list '(mexpt) var2
(integerp2 (quotient (caddr ex
) dd
))))
1983 (t (cons (remove 'simp
(car ex
))
1984 (mapcar #'(lambda (c)
1985 (subst10 c var2 dd
))
1988 (defun powerlist (expr var2
)
1989 (prog (y cc dd power-list bb
)
1992 ((mexpt) (var varp2
,var2
) (c integerp2
))
1993 ((coefftt) (a freevar2
,var2
))
1994 ((coefftt) (b true
)))))
1995 (setq bb
(cdr (assoc 'b y
:test
#'eq
)))
1996 (setq cc
(cdr (assoc 'c y
:test
#'eq
)))
1999 (cond ((freevar2 ex var2
)
2003 ((eq (caar ex
) 'mexpt
)
2004 (if (varp2 (cadr ex
) var2
)
2005 (if (integerp2 (caddr ex
))
2006 (setq power-list
(cons (caddr ex
) power-list
)))
2007 (and (rat10 (cadr ex
))
2008 (rat10 (caddr ex
)))))
2009 ((member (caar ex
) '(mplus mtimes
) :test
#'eq
)
2010 (do ((u (cdr ex
) (cdr u
)))
2012 (if (not (rat10 (car u
)))
2014 (unless (rat10 bb
) (return nil
))
2015 (setq dd
(apply #'gcd
(cons (1+ cc
) power-list
))))
2016 (when (or (eql 1 dd
) (zerop dd
)) (return nil
))
2019 (list '(mexpt) var2 dd
)
2021 (integrate5 (simplify (list '(mtimes)
2023 (cdr (assoc 'a y
:test
#'eq
))
2024 (list '(mexpt) var2
(1- (quotient (1+ cc
) dd
)))
2025 (subst10 bb var2 dd
)))
2030 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2033 ;;; Implementation of Method 3: Derivative-divides algorithm
2035 ;; This is the derivative-divides algorithm of Moses.
2039 ;; Look for form I c * op(u(x)) * u'(x) dx
2043 ;; where: c is a constant
2044 ;; u(x) is an elementary expression in x
2045 ;; u'(x) is its derivative
2046 ;; op is an elementary operator:
2047 ;; - the identity, or
2048 ;; - any function that can be integrated by INTEGRALLOOKUPS
2050 ;; The method of solution, once the problem has been determined to
2051 ;; posses the form above, is to look up OP in a table and substitute
2052 ;; u(x) for each occurrence of x in the expression given in the table.
2053 ;; In other words, the method performs an implicit substitution y = u(x),
2054 ;; and obtains the integral of op(y)dy by a table look up.
2056 (defun diffdiv (expr var2
)
2057 (prog (y x v dd z w r
)
2058 (cond ((and (mexptp expr
)
2059 (mplusp (cadr expr
))
2060 (integerp (caddr expr
))
2063 (return (integrator (expandexpt (cadr expr
) (caddr expr
)) var2
))))
2065 ;; If not a product, transform to a product with one term
2066 (setq expr
(cond ((mtimesp expr
) expr
) (t (list '(mtimes) expr
))))
2068 ;; Loop over the terms in expr
2072 ;; This m2 pattern matches const*(exp/y)
2073 (setq r
(list '(mplus)
2075 (cons `(c free12
,var2
)
2076 (remove y
(cdr expr
) :count
1)))))
2078 ;; Case u(var2) is the identity function. y is a term in exp.
2079 ;; Match if diff(y,var2) == c*(exp/y).
2080 ;; This even works when y is a function with multiple args.
2081 ((setq w
(m2 (sdiff y var2
) r
))
2082 (return (muln (list y y
(power* (mul2* 2 (cdr (assoc 'c w
:test
#'eq
))) -
1)) nil
))))
2084 ;; w is the arg in y.
2085 (let ((arg-freevar))
2088 ((or (atom y
) (member (caar y
) '(mplus mtimes
) :test
#'eq
)) y
)
2089 ;; Take the argument of a function with one value.
2090 ((= (length (cdr y
)) 1) (cadr y
))
2091 ;; A function has multiple args, and exactly one arg depends on var2
2092 ((= (count-if #'null
(setq arg-freevar
(mapcar #'(lambda (v)
2096 (do ((args (cdr y
) (cdr args
))
2097 (argf arg-freevar
(cdr argf
)))
2098 ((if (not (car argf
)) (return (car args
))))))
2102 ((setq w
(cond ((and (setq x
(sdiff w var2
))
2104 (setq dd
(remove y
(cdr expr
) :count
1))
2108 (cond ((setq dd
(matchsum (cdr x
) (cdr v
) var2
))
2109 (list (cons 'c dd
)))
2112 (return (cond ((null (setq x
(integrallookups y var2
))) nil
)
2114 (t (mul2* x
(power* (cdr (assoc 'c w
:test
#'eq
)) -
1)))))))
2116 (when (null z
) (return nil
))
2119 (defun subliss (alist expr
)
2120 "Alist is an alist consisting of a variable (symbol) and its value. expr is
2121 an expression. For each entry in alist, substitute the corresponding
2125 (setq x
(maxima-substitute (cdr a
) (car a
) x
)))))
2127 (defun substint (x y expres var2 expr
)
2128 (if (and (not (atom expres
)) (eq (caar expres
) '%integrate
))
2129 (list (car expres
) expr var2
)
2130 (substint1 (maxima-substitute x y expres
) var2
)))
2132 (defun substint1 (expr var2
)
2133 (cond ((atom expr
) expr
)
2134 ((and (eq (caar expr
) '%integrate
)
2136 (not (symbolp (caddr expr
)))
2137 (not (free (caddr expr
) var2
)))
2138 (simplify (list '(%integrate
)
2139 (mul2 (cadr expr
) (sdiff (caddr expr
) var2
))
2141 (t (recur-apply #'(lambda (e)
2145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2147 ;:; Extension of the integrator for more integrals with power functions
2149 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2151 ;;; Recognize (a^(c*(z^r)^p+d)^v
2153 (defun m2-exp-type-1a (expr var2
)
2159 ;; The order of the pattern is critical. If we change it,
2160 ;; we do not get the expected match.
2161 ((coeffpp) (d freevar2
,var2
))
2162 ((coefft) (c freevar02
,var2
)
2164 ((mexpt) (z varp2
,var2
) (r freevar02
,var2
))
2165 (p freevar2
,var2
)))))
2166 (v freevar2
,var2
))))
2168 ;;; Recognize z^v*a^(b*z^r+d)
2170 (defun m2-exp-type-2 (expr var2
)
2173 ((mexpt) (z varp2
,var2
) (v freevar02
,var2
))
2177 ((coeffpp) (d freevar2
,var2
))
2178 ((coefft) (b freevar02
,var2
) ((mexpt) (z varp2
,var2
) (r freevar02
,var2
))))))))
2180 ;;; Recognize z^v*%e^(a*z^r+b)^u
2182 (defun m2-exp-type-2-1 (expr var2
)
2185 ((mexpt) (z varp2
,var2
) (v freevar02
,var2
))
2190 ((coeffpp) (b freevar2
,var2
))
2191 ((coefft) (a freevar02
,var2
) ((mexpt) (z varp2
,var2
) (r freevar02
,var2
)))))
2192 (u freevar2
,var2
)))))
2194 ;;; Recognize (a*z+b)^p*%e^(c*z+d)
2196 (defun m2-exp-type-3 (expr var2
)
2201 ((coefft) (a freevar02
,var2
) (z varp2
,var2
))
2202 ((coeffpp) (b freevar2
,var2
)))
2203 (p freevar02
,var2
))
2207 ((coefft) (c freevar02
,var2
) (z varp2
,var2
))
2208 ((coeffpp) (d freevar2
,var2
)))))))
2210 ;;; Recognize d^(a*z^2+b/z^2+c)
2212 (defun m2-exp-type-4 (expr var2
)
2217 ((coefft) (a freevar02
,var2
) ((mexpt) (z varp2
,var2
) 2))
2218 ((coefft) (b freevar02
,var2
) ((mexpt) (z varp2
,var2
) -
2))
2219 ((coeffpp) (c freevar2
,var2
))))))
2221 ;;; Recognize z^(2*n)*d^(a*z^2+b/z^2+c)
2223 (defun m2-exp-type-4-1 (expr var2
)
2226 ((mexpt) (z varp2
,var2
) (n freevar02
,var2
))
2230 ((coefft) (a freevar02
,var2
) ((mexpt) (z varp2
,var2
) 2))
2231 ((coefft) (b freevar02
,var2
) ((mexpt) (z varp2
,var2
) -
2))
2232 ((coeffpp) (c freevar2
,var2
)))))))
2234 ;;; Recognize z^n*d^(a*z^2+b*z+c)
2236 (defun m2-exp-type-5 (expr var2
)
2239 ((mexpt) (z varp2
,var2
) (n freevar2
,var2
))
2243 ((coeffpt) (a freevar2
,var2
) ((mexpt) (z varp2
,var2
) 2))
2244 ((coeffpt) (b freevar2
,var2
) (z varp2
,var2
))
2245 ((coeffpp) (c freevar2
,var2
)))))))
2247 ;;; Recognize z^n*(%e^(a*z^2+b*z+c))^u
2249 (defun m2-exp-type-5-1 (expr var2
)
2252 ((mexpt) (z varp2
,var2
) (n freevar02
,var2
))
2257 ((coeffpp) (c freevar2
,var2
))
2258 ((coefft) (a freevar02
,var2
) ((mexpt) (z varp2
,var2
) 2))
2259 ((coefft) (b freevar02
,var2
) (z varp2
,var2
))))
2260 (u freevar2
,var2
)))))
2262 ;;; Recognize z^n*d^(a*sqrt(z)+b*z+c)
2264 (defun m2-exp-type-6 (expr var2
)
2267 ((mexpt) (z varp2
,var2
) (n freevar02
,var2
))
2271 ((coefft) (a freevar02
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2272 ((coefft) (b freevar02
,var2
) (z varp2
,var2
))
2273 ((coeffpp) (c freevar2
,var2
)))))))
2275 ;;; Recognize z^n*(%e^(a*sqrt(z)+b*z+c))^u
2277 (defun m2-exp-type-6-1 (expr var2
)
2280 ((mexpt) (z varp2
,var2
) (n freevar02
,var2
))
2285 ((coeffpp) (c freevar2
,var2
))
2286 ((coefft) (a freevar02
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2287 ((coefft) (b freevar02
,var2
) (z varp2
,var2
))))
2288 (u freevar2
,var2
)))))
2290 ;;; Recognize z^n*a^(b*z^r+e)*h^(c*z^r+g)
2292 (defun m2-exp-type-7 (expr var2
)
2295 ((mexpt) (z varp2
,var2
) (n freevar2
,var2
))
2301 ((mexpt) (z varp2
,var2
) (r freevar02
,var2
)))
2302 ((coeffpp) (e freevar2
,var2
))))
2308 ((mexpt) (z varp2
,var2
) (r1 freevar02
,var2
)))
2309 ((coeffpp) (g freevar2
,var2
)))))))
2311 ;;; Recognize z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u
2313 (defun m2-exp-type-7-1 (expr var2
)
2316 ((mexpt) (z varp2
,var2
) (v freevar2
,var2
))
2321 ((coeffpp) (e freevar2
,var2
))
2322 ((coefft) (b freevar02
,var2
) ((mexpt) (z varp2
,var2
) (r freevar02
,var2
)))))
2328 ((coeffpp) (g freevar2
,var2
))
2329 ((coefft) (c freevar02
,var2
) ((mexpt) (z varp2
,var2
) (r1 freevar02
,var2
)))))
2330 (u freevar2
,var2
)))))
2332 ;;; Recognize a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)
2334 (defun m2-exp-type-8 (expr var2
)
2340 ((coeffpt) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2341 ((coeffpt) (d freevar2
,var2
) (z varp2
,var2
))
2342 ((coeffpp) (e freevar2
,var2
))))
2346 ((coeffpt) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2347 ((coeffpt) (f freevar2
,var2
) (z varp2
,var2
))
2348 ((coeffpp) (g freevar2
,var2
)))))))
2350 ;;; Recognize (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v
2352 (defun m2-exp-type-8-1 (expr var2
)
2359 ((coeffpp) (e freevar2
,var2
))
2360 ((coeffpt) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2361 ((coeffpt) (d freevar2
,var2
) (z varp2
,var2
))))
2367 ((coeffpp) (g freevar2
,var2
))
2368 ((coeffpt) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2369 ((coeffpt) (f freevar2
,var2
) (z varp2
,var2
))))
2370 (v freevar2
,var2
)))))
2372 ;;; Recognize (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v
2374 (defun m2-exp-type-8-2 (expr var2
)
2381 ((coeffpp) (e freevar2
,var2
))
2382 ((coefft) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) (r freevar02
,var2
)))))
2388 ((coeffpp) (g freevar2
,var2
))
2389 ((coefft) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) (r1 freevar02
,var2
)))))
2390 (v freevar2
,var2
)))))
2392 ;;; Recognize z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)
2394 (defun m2-exp-type-9 (expr var2
)
2397 ((mexpt) (z varp2
,var2
) (n freevar2
,var2
))
2401 ((coeffpt) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) 2))
2402 ((coeffpt) (d freevar2
,var2
) (z varp2
,var2
))
2403 ((coeffpp) (e freevar2
,var2
))))
2407 ((coeffpt) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) 2))
2408 ((coeffpt) (f freevar2
,var2
) (z varp2
,var2
))
2409 ((coeffpp) (g freevar2
,var2
)))))))
2411 ;;; Recognize z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u
2413 (defun m2-exp-type-9-1 (expr var2
)
2416 ((mexpt) (z varp2
,var2
) (n freevar2
,var2
))
2421 ((coeffpp) (e freevar2
,var2
))
2422 ((coeffpt) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) 2))
2423 ((coeffpt) (d freevar2
,var2
) (z varp2
,var2
))))
2429 ((coeffpp) (g freevar2
,var2
))
2430 ((coeffpt) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) 2))
2431 ((coeffpt) (f freevar2
,var2
) (z varp2
,var2
))))
2432 (u freevar2
,var2
)))))
2434 ;;; Recognize z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z+)f*z+g)
2436 (defun m2-exp-type-10 (expr var2
)
2439 ((mexpt) (z varp2
,var2
) (n freevar2
,var2
))
2443 ((coeffpt) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2444 ((coeffpt) (d freevar2
,var2
) (z varp2
,var2
))
2445 ((coeffpp) (e freevar2
,var2
))))
2449 ((coeffpt) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2450 ((coeffpt) (f freevar2
,var2
) (z varp2
,var2
))
2451 ((coeffpp) (g freevar2
,var2
)))))))
2453 ;;; Recognize z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u
2455 (defun m2-exp-type-10-1 (expr var2
)
2458 ((mexpt) (z varp2
,var2
) (n freevar2
,var2
))
2463 ((coeffpp) (e freevar2
,var2
))
2464 ((coeffpt) (b freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2465 ((coeffpt) (d freevar2
,var2
) (z varp2
,var2
))))
2471 ((coeffpp) (g freevar2
,var2
))
2472 ((coeffpt) (c freevar2
,var2
) ((mexpt) (z varp2
,var2
) ((rat) 1 2)))
2473 ((coeffpt) (f freevar2
,var2
) (z varp2
,var2
))))
2474 (u freevar2
,var2
)))))
2476 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2478 (defun integrate-exp-special (expr var2
&aux w const
)
2480 ;; First factor the expression.
2481 (setq expr
($factor expr
))
2483 ;; Remove constant factors.
2484 (setq w
(partition expr var2
1))
2485 (setq const
(car w
))
2489 ((m2-exp-type-1a (facsum-exponent expr var2
) var2
)
2491 (when *debug-integrate
*
2492 (format t
"~&Type 1a: (a^(c*(z^r)^p+d)^v : w = ~A~%" w
))
2496 ;; 1/(p*r*(a^(c*v*(var2^r)^p)))
2497 (inv (mul p r
(power a
(mul c v
(power (power var2 r
) p
)))))
2499 ;; (a^(d+c*(var2^r)^p))^v
2500 (power (power a
(add d
(mul c
(power (power var2 r
) p
)))) v
)
2501 ;; gamma_incomplete(1/(p*r), -c*v*(var2^r)^p*log(a))
2502 (take '(%gamma_incomplete
)
2504 (mul -
1 c v
(power (power var2 r
) p
) (take '(%log
) a
)))
2505 ;; (-c*v*(var2^r)^p*log(a))^(-1/(p*r))
2506 (power (mul -
1 c v
(power (power var2 r
) p
) (take '(%log
) a
))
2507 (div -
1 (mul p r
)))))
2509 ((m2-exp-type-2 (facsum-exponent expr var2
) var2
)
2512 (when *debug-integrate
*
2513 (format t
"~&Type 2: z^v*a^(b*z^r+d) : w = ~A~%" w
))
2519 (power var2
(add v
1))
2522 (mul -
1 b
(power var2 r
) ($log a
)))
2524 (mul -
1 b
(power var2 r
) ($log a
))
2525 (mul -
1 (div (add v
1) r
)))))
2527 ((m2-exp-type-2-1 (facsum-exponent expr var2
) var2
)
2529 (when *debug-integrate
*
2530 (format t
"~&Type 2-1: z^v*(%e^(a*z^r+b))^u : w = ~A~%" w
))
2535 (power '$%e
(mul -
1 a u
(power var2 r
)))
2536 (power (power '$%e
(add (mul a
(power var2 r
)) b
)) u
)
2537 (power var2
(add v
1))
2538 (power (mul -
1 a u
(power var2 r
)) (div (mul -
1 (add v
1)) r
))
2539 (take '(%gamma_incomplete
)
2541 (mul -
1 a u
(power var2 r
)))))
2543 ((m2-exp-type-3 (facsum-exponent expr var2
) var2
)
2545 (when *debug-integrate
*
2546 (format t
"~&Type 3: (a*z+b)^p*%e^(c*z+d) : w = ~A~%" w
))
2550 (power '$%e
(sub d
(div (mul b c
) a
)))
2551 (power (add b
(mul a var2
)) (add p
1))
2552 (ftake '%expintegral_e
(mul -
1 p
) (mul (div -
1 a
) c
(add b
(mul a var2
))))))
2554 ((m2-exp-type-4 expr var2
)
2556 (let (($trigsign nil
)) ; Do not simplify erfc(-x) !
2557 (when *debug-integrate
*
2558 (format t
"~&Type 4: d^(a*z^2+b/z^2+c) : w = ~A~%" w
))
2562 (div 1 (mul 4 (power (mul -
1 a
($log d
)) (div 1 2))))
2565 (power '$%pi
(div 1 2))
2568 (power (mul -
1 a
($log d
)) (div 1 2))
2569 (power (mul -
1 b
($log d
)) (div 1 2))))
2573 (div (power (mul -
1 b
($log d
)) (div 1 2)) var2
)
2574 (mul -
1 var2
(power (mul -
1 a
($log d
)) (div 1 2)))))
2578 (power (mul -
1 a
($log d
)) (div 1 2))
2579 (power (mul -
1 b
($log d
)) (div 1 2))))
2582 (mul var2
(power (mul -
1 a
($log d
)) (div 1 2)))
2583 (div (power (mul -
1 b
($log d
)) (div 1 2)) var2
)))))))))
2585 ((and (m2-exp-type-4-1 expr var2
)
2586 (poseven (cdras 'n w
)) ; only for n a positive, even integer
2587 (symbolp (cdras 'a w
))) ; a has to be a symbol
2589 (let (($trigsign nil
)) ; Do not simplify erfc(-x) !
2591 (when *debug-integrate
*
2592 (format t
"~&Type 4-1: z^(2*n)*d^(a*z^2+b/z^2+c) : w = ~A~%" w
))
2599 (power '$%pi
(div 1 2))
2600 (simplify (list '(%derivative
)
2604 (power ($log d
) (mul -
1 n
))
2610 (power (mul -
1 a
($log d
)) (div 1 2))
2611 (power (mul -
1 b
($log d
)) (div 1 2))))
2615 (power (mul -
1 b
($log d
)) (div 1 2))
2617 (mul var2
(power (mul -
1 ($log d
)) (div 1 2))))))))
2622 (power (mul -
1 a
($log d
)) (div 1 2))
2623 (power (mul -
1 b
($log d
)) (div 1 2))))
2626 (power (mul -
1 a
($log d
)) (div 1 2))
2627 (div (power (mul -
1 b
($log d
)) (div 1 2)) var2
)))))
2628 (power (mul -
1 a
($log d
)) (div 1 2)))
2631 ((and (m2-exp-type-5 (facsum-exponent expr var2
) var2
)
2632 (maxima-integerp (cdras 'n w
))
2633 (eq ($sign
(cdras 'n w
)) '$pos
))
2636 (when *debug-integrate
*
2637 (format t
"~&Type 5: z^n*d^(a*z^2+b*z+c) : w = ~A~%" w
))
2641 (div -
1 (mul 2 (power (mul a
($log d
)) (div 1 2))))
2643 (power d
(sub c
(div (mul b b
) (mul 4 a
))))
2644 (let ((index (gensumindex))
2648 (power 2 (sub index n
))
2649 (ftake '%binomial n index
)
2651 (div (add index
1) 2)
2654 (power (add b
(mul 2 a var2
)) 2)
2656 (power (mul a
($log d
)) (mul -
1 (add n
(div 1 2))))
2657 (power (mul -
1 b
($log d
)) (sub n index
))
2658 (power (mul (add b
(mul 2 a var2
)) ($log d
)) (add index
1))
2660 (mul (div -
1 a
) (power (add b
(mul 2 a var2
)) 2) ($log d
))
2661 (mul (div -
1 2) (add index
1))))
2664 ((and (m2-exp-type-5-1 (facsum-exponent expr var2
) var2
)
2665 (maxima-integerp (cdras 'n w
))
2666 (eq ($sign
(cdras 'n w
)) '$pos
))
2668 (when *debug-integrate
*
2669 (format t
"~&Type 5-1: z^n*(%e^(a*z^2+b*z+c))^u : w = ~A~%" w
))
2674 (add (mul -
1 (div (mul b b u
) (mul 4 a
)))
2675 (mul -
1 u
(add (mul a var2 var2
) (mul b var2
)))))
2676 (power a
(mul -
1 (add n
1)))
2678 (add (mul a var2 var2
) (mul b var2
) c
))
2680 (let ((index (gensumindex))
2683 (mul (power 2 (sub index n
))
2684 (power (mul -
1 b
) (sub n index
))
2685 (power (add b
(mul 2 a var2
)) (add index
1))
2686 (power (div (mul -
1 u
(power (add b
(mul 2 a var2
)) 2)) a
)
2687 (mul (div -
1 2) (add index
1)))
2688 (take '(%binomial
) n index
)
2689 (take '(%gamma_incomplete
)
2690 (div (add index
1) 2)
2691 (div (mul -
1 u
(power (add b
(mul 2 a var2
)) 2))
2695 ((and (m2-exp-type-6 (facsum-exponent expr var2
) var2
)
2696 (maxima-integerp (cdras 'n w
))
2697 (eq ($sign
(cdras 'n w
)) '$pos
))
2699 (when *debug-integrate
*
2700 (format t
"~&Type 6: z^n*d^(a*sqrt(z)+b*z+c) : w = ~A~%" w
))
2704 (power 2 (mul -
1 (add n
1)))
2705 (power d
(sub c
(div (mul a a
) (mul 4 b
))))
2706 (power (mul b
($log d
)) (mul -
2 (add n
1)))
2707 (let ((index1 (gensumindex))
2708 (index2 (gensumindex))
2713 (power -
1 (sub index1 index2
))
2715 (ftake '%binomial index1 index2
)
2716 (ftake '%binomial n index1
)
2718 (power (mul a
($log d
)) (sub (mul 2 n
) (add index1 index2
)))
2720 (mul (add a
(mul 2 b
(power var2
(div 1 2)))) ($log d
))
2721 (add index1 index2
))
2725 (power (add a
(mul 2 b
(power var2
(div 1 2)))) 2)
2727 (mul (div -
1 2) (add index1 index2
1)))
2733 (power (add a
(mul 2 b
(power var2
(div 1 2)))) 2)
2737 (div (add index1 index2
2) 2)
2740 (power (add a
(mul 2 b
(power var2
(div 1 2)))) 2)
2743 (add a
(mul 2 b
(power var2
(div 1 2))))
2746 (div (add index1 index2
1) 2)
2749 (power (add a
(mul 2 b
(power var2
(div 1 2)))) 2)
2754 ((and (m2-exp-type-6-1 (facsum-exponent expr var2
) var2
)
2755 (maxima-integerp (cdras 'n w
))
2756 (eq ($sign
(cdras 'n w
)) '$pos
))
2758 (when *debug-integrate
*
2759 (format t
"~&Type 6-1: z^n*(%e^(a*sqrt(z)+b*z+c))^u : w = ~A~%" w
))
2762 (power 2 (mul -
1 (add (mul 2 n
) 1)))
2764 (add (div (mul -
1 u a a
) (mul 4 b
))
2765 (mul u
(add (mul a
(power var2
(div 1 2)))
2768 (power b
(mul -
2 (add n
1)))
2770 (add (mul a
(power var2
(div 1 2)))
2773 (let ((index1 (gensumindex))
2774 (index2 (gensumindex))
2778 (mul (power -
1 (sub index1 index2
))
2780 (power a
(add (neg index2
) (neg index1
) (mul 2 n
)))
2781 (power (add a
(mul 2 b
(power var2
(div 1 2))))
2782 (add index1 index2
))
2783 (power (div (mul -
1 u
2787 (power var2
(div 1 2))))
2790 (mul (div -
1 2) (add index1 index2
1)))
2791 (take '(%binomial
) index1 index2
)
2792 (take '(%binomial
) n index1
)
2794 (add a
(mul 2 b
(power var2
(div 1 2))))
2795 (take '(%gamma_incomplete
)
2796 (div (add index1 index2
1) 2)
2805 (power (div (mul -
1 u
2814 (take '(%gamma_incomplete
)
2815 (div (add index1 index2
2) 2)
2819 (power var2
(div 1 2))))
2825 ((and (m2-exp-type-7 (facsum-exponent expr var2
) var2
)
2826 (eq ($sign
(sub (cdras 'r w
) (cdras 'r1 w
))) '$zero
))
2828 (when *debug-integrate
*
2829 (format t
"~&Type 7: z^n*a^(b*z^r+e)*h^(c*z^r+g) : w = ~A~%" w
))
2842 (add (mul b
($log a
)) (mul c
($log h
))))
2846 (mul -
1 (power var2 r
) (add (mul b
($log a
)) (mul c
($log h
)))))))
2848 ((and (m2-exp-type-7-1 (facsum-exponent expr var2
) var2
)
2849 (eq ($sign
(sub (cdras 'r w
) (cdras 'r1 w
))) '$zero
))
2851 (when *debug-integrate
*
2852 (format t
"~&Type 7-1: z^v*(%e^(b*z^r+e))^q*(%e^(c*z^r+g))^u : w = ~A~%" w
))
2856 (power '$%e
(mul -
1 (power var2 r
) (add (mul b q
) (mul c u
))))
2857 (power (power '$%e
(add e
(mul b
(power var2 r
)))) q
)
2858 (power (power '$%e
(add g
(mul c
(power var2 r
)))) u
)
2859 (power var2
(add v
1))
2860 (power (mul -
1 (power var2 r
) (add (mul b q
) (mul c u
)))
2861 (div (mul -
1 (add v
1)) r
))
2862 (take '(%gamma_incomplete
)
2864 (mul -
1 (power var2 r
) (add (mul b q
) (mul c u
))))))
2866 ((m2-exp-type-8 (facsum-exponent expr var2
) var2
)
2868 (when *debug-integrate
*
2869 (format t
"~&Type 8: a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
2870 (format t
"~& : w = ~A~%" w
))
2879 (power a
(add (mul b
(power var2
(div 1 2))) (mul d var2
)))
2880 (power h
(add (mul c
(power var2
(div 1 2))) (mul f var2
)))
2881 (div 1 (add (mul d
($log a
)) (mul f
($log h
)))))
2883 (power '$%pi
(div 1 2))
2887 (power (add (mul b
($log a
)) (mul c
($log h
))) 2)
2888 (mul 4 (add (mul d
($log a
)) (mul f
($log h
)))))))
2895 (power var2
(div 1 2))
2896 (add (mul d
($log a
)) (mul f
($log h
)))))
2898 (power (add (mul d
($log a
)) (mul f
($log h
))) (div 1 2)))))
2899 (add (mul b
($log a
)) (mul c
($log h
)))
2900 (power (add (mul d
($log a
)) (mul f
($log h
))) (div -
3 2))))))
2902 ((m2-exp-type-8-1 (facsum-exponent expr var2
) var2
)
2904 (when *debug-integrate
*
2905 (format t
"~&Type 8-1: (%e^(b*sqrt(z)+d*z+e))^u*(%e^(c*sqrt(z)+f*z+g))^v")
2906 (format t
"~& : w = ~A~%" w
))
2910 (power (add (mul d u
) (mul f v
)) (div -
3 2))
2913 (power (add (mul b u
)
2914 (mul 2 d u
(power var2
(div 1 2)))
2915 (mul v
(add c
(mul 2 f
(power var2
(div 1 2))))))
2917 (inv (mul 4 (add (mul d u
) (mul f v
))))))
2919 (add (mul b
(power var2
(div 1 2)))
2924 (add (mul c
(power var2
(div 1 2)))
2930 (mul (power (add (mul b u
)
2931 (mul 2 d u
(power var2
(div 1 2)))
2932 (mul v
(add c
(mul 2 f
(power var2
(div 1 2))))))
2934 (inv (mul 4 (add (mul d u
) (mul f v
))))))
2935 (power (add (mul d u
) (mul f v
)) (div 1 2)))
2937 (power '$%pi
(div 1 2))
2938 (add (mul b u
) (mul c v
))
2941 (mul 2 d u
(power var2
(div 1 2)))
2943 (mul 2 f v
(power var2
(div 1 2))))
2945 (power (add (mul d u
) (mul f v
))
2948 ((and (m2-exp-type-8-2 (facsum-exponent expr var2
) var2
)
2949 (eq ($sign
(sub (cdras 'r w
) (cdras 'r1 w
))) '$zero
))
2951 (when *debug-integrate
*
2952 (format t
"~&Type 8-2: (%e^(b*z^r+e))^u*(%e^(c*z^r+g))^v")
2953 (format t
"~& : w = ~A~%" w
))
2961 (add (mul b u
) (mul c v
))))
2963 (add (power var2 r
) e
))
2966 (add (power var2 r
) g
))
2971 (add (mul b u
) (mul c v
)))
2973 (take '(%gamma_incomplete
)
2975 (mul -
1 (power var2 r
) (add (mul b u
) (mul c v
))))))
2977 ((and (m2-exp-type-9 (facsum-exponent expr var2
) var2
)
2978 (maxima-integerp (cdras 'n w
))
2979 (eq ($sign
(cdras 'n w
)) '$pos
)
2980 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
2981 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
2983 (when *debug-integrate
*
2984 (format t
"~&Type 9: z^n*a^(b*z^2+d*z+e)*h^(c*z^2+f*z+g)")
2985 (format t
"~& : w = ~A~%" w
))
2994 (power (add (mul d
($log a
)) (mul f
($log h
))) 2)
2995 (mul -
4 (add (mul b
($log a
)) (mul c
($log h
))))))
2996 (power (add (mul b
($log a
)) (mul c
($log h
))) (mul -
1 (add n
1)))
2997 (let ((index (gensumindex))
3001 (power 2 (sub index n
))
3002 (ftake '%binomial n index
)
3004 (add (mul -
1 d
($log a
)) (mul -
1 f
($log h
)))
3008 (mul (add d
(mul 2 b var2
)) ($log a
))
3009 (mul (add f
(mul 2 c var2
)) ($log h
)))
3016 (mul (add d
(mul 2 b var2
)) ($log a
))
3017 (mul (add f
(mul 2 c var2
)) ($log h
)))
3019 (add (mul b
($log a
)) (mul c
($log h
)))))
3020 (div (add index
1) -
2))
3022 (div (add index
1) 2)
3027 (mul (add d
(mul 2 b var2
)) ($log a
))
3028 (mul (add f
(mul 2 c var2
)) ($log h
)))
3030 (mul 4 (add (mul b
($log a
)) (mul c
($log h
))))))))
3033 ((and (m2-exp-type-9-1 (facsum-exponent expr var2
) var2
)
3034 (maxima-integerp (cdras 'n w
))
3035 (eq ($sign
(cdras 'n w
)) '$pos
)
3036 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
3037 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
3039 (when *debug-integrate
*
3040 (format t
"~&Type 9-1: z^n*(%e^(b*z^2+d*z+e))^q*(%e^(c*z^2+f*z+g))^u")
3041 (format t
"~& : w = ~A~%" w
))
3045 (power (add (mul b q
) (mul c u
)) (div -
1 2))
3047 (add (div (power (add (mul d q
) (mul f u
)) 2)
3048 (mul -
4 (add (mul b q
) (mul c u
))))
3056 (mul var2
(add d
(mul b var2
)))))
3060 (mul var2
(add f
(mul c var2
)))))
3062 (let ((index (gensumindex))
3065 (mul (power 2 (sub index n
))
3066 (power (add (mul b q
) (mul c u
)) (neg (add n
(div 1 2))))
3067 (power (add (neg (mul d q
)) (neg (mul f u
)))
3069 (power (add (mul d q
)
3071 (mul 2 var2
(add (mul b q
) (mul c u
))))
3073 (power (div (power (add (mul d q
)
3080 (neg (add (mul b q
) (mul c u
))))
3081 (mul (div -
1 2) (add index
1)))
3082 (take '(%binomial
) n index
)
3083 (take '(%gamma_incomplete
)
3084 (div (add index
1) 2)
3085 (div (power (add (mul d q
)
3092 (mul -
4 (add (mul b q
) (mul c u
))))))
3095 ((and (m2-exp-type-10 (facsum-exponent expr var2
) var2
)
3096 (maxima-integerp (cdras 'n w
))
3097 (eq ($sign
(cdras 'n w
)) '$pos
)
3098 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
3099 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
3101 (when *debug-integrate
*
3102 (format t
"~&Type 10: z^n*a^(b*sqrt(z)+d*z+e)*h^(c*sqrt(z)+f*z+g)")
3103 (format t
"~& : w = ~A~%" w
))
3106 (power 2 (add (mul -
2 n
) -
1))
3110 (div (power (add (mul b
($log a
)) (mul c
($log h
))) 2)
3111 (mul -
4 (add (mul d
($log a
)) (mul f
($log h
))))))
3112 (power (add (mul d
($log a
)) (mul f
($log h
))) (mul -
2 (add n
1)))
3113 (let ((index1 (gensumindex))
3114 (index2 (gensumindex))
3118 (mul (power -
1 (sub index1 index2
))
3120 (ftake '%binomial index1 index2
)
3121 (ftake '%binomial n index1
)
3122 (power (add (mul b
($log a
)) (mul c
($log h
)))
3123 (sub (mul 2 n
) (add index1 index2
)))
3124 (power (add (mul b
($log a
))
3127 (power var2
(div 1 2))
3128 (add (mul d
($log a
)) (mul f
($log h
)))))
3129 (add index1 index2
))
3131 (div (power (add (mul b
($log a
))
3134 (power var2
(div 1 2))
3135 (add (mul d
($log a
))
3138 (add (mul d
($log a
)) (mul f
($log h
)))))
3139 (mul (div -
1 2) (add index1 index2
1)))
3140 (add (mul ($gamma_incomplete
(mul (div 1 2)
3141 (add index1 index2
1))
3143 (div (power (add (mul b
($log a
))
3146 (power var2
(div 1 2))
3147 (add (mul d
($log a
)) (mul f
($log h
)))))
3149 (add (mul d
($log a
)) (mul f
($log h
))))))
3150 (add (mul b
($log a
)) (mul c
($log h
)))
3151 (add (mul b
($log a
))
3154 (power var2
(div 1 2))
3155 (add (mul d
($log a
)) (mul f
($log h
))))))
3157 ($gamma_incomplete
(mul (div 1 2)
3158 (add index1 index2
2))
3160 (div (power (add (mul b
($log a
))
3163 (power var2
(div 1 2))
3164 (add (mul d
($log a
))
3167 (add (mul d
($log a
))
3168 (mul f
($log h
))))))
3169 (add (mul d
($log a
)) (mul f
($log h
)))
3171 (div (power (add (mul b
($log a
))
3174 (power var2
(div 1 2))
3175 (add (mul d
($log a
))
3178 (add (mul d
($log a
))
3184 ((and (m2-exp-type-10-1 (facsum-exponent expr var2
) var2
)
3185 (maxima-integerp (cdras 'n w
))
3186 (eq ($sign
(cdras 'n w
)) '$pos
)
3187 (or (not (eq ($sign
(cdras 'b w
)) '$zero
))
3188 (not (eq ($sign
(cdras 'c w
)) '$zero
))))
3190 (let ((bq+cu
(add (mul b q
) (mul c u
)))
3191 (dq+fu
(add (mul d q
) (mul f u
))))
3192 (when *debug-integrate
*
3193 (format t
"~&Type 10-1: z^n*(%e^(b*sqrt(z)+d*z+e))^q*(%e^(c*sqrt(z)+f*z+g))^u")
3194 (format t
"~& : w = ~A~%" w
))
3197 (power 2 (mul -
1 (add (mul 2 n
) 1)))
3199 (add (div (mul -
1 (power bq
+cu
2)) (mul 4 dq
+fu
))
3201 (mul -
1 b
(power var2
(div 1 2)) q
)
3203 (mul -
1 c
(power var2
(div 1 2)) u
)))
3205 (add (mul b
(power var2
(div 1 2)))
3210 (add (mul c
(power var2
(div 1 2)))
3214 (power dq
+fu
(mul -
2 (add n
1)))
3215 (let ((index1 (gensumindex))
3216 (index2 (gensumindex))
3220 (mul (power -
1 (sub index1 index2
))
3223 (add (neg index1
) (neg index2
) (mul 2 n
)))
3225 (mul 2 (power var2
(div 1 2)) dq
+fu
))
3226 (add index1 index2
))
3227 (power (div (power (add bq
+cu
3229 (power var2
(div 1 2))
3234 (add index1 index2
1)))
3235 (take '(%binomial
) index1 index2
)
3236 (take '(%binomial
) n index1
)
3240 (power var2
(div 1 2))
3242 (take '(%gamma_incomplete
)
3244 (add index1 index2
1))
3245 (div (power (add (mul b q
)
3248 (power var2
(div 1 2))
3254 (power (div (power (add bq
+cu
3256 (power var2
(div 1 2))
3262 (take '(%gamma_incomplete
)
3264 (add index1 index2
2))
3265 (div (power (add bq
+cu
3267 (power var2
(div 1 2))
3275 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3277 ;;; Do a facsum for the exponent of power functions.
3278 ;;; This is necessary to integrate all general forms. The pattern matcher is
3279 ;;; not powerful enough to do the job.
3281 (defun facsum-exponent (expr var2
)
3282 ;; Make sure that expr has the form ((mtimes) factor1 factor2 ...)
3283 (when (not (mtimesp expr
)) (setq expr
(list '(mtimes) expr
)))
3285 (l (cdr expr
) (cdr l
)))
3286 ((null l
) (cons (list 'mtimes
) result
))
3289 ;; Found an power function. Factor the exponent with facsum.
3290 (let* ((fac (mfuncall '$facsum
(caddr (car l
)) var2
))
3294 (cons (cons (list 'mexpt
)
3295 (cons (cadr (car l
))
3298 (list ($multthru
(inv den
) num
)))))
3302 (setq result
(cons (car l
) result
))))))
3304 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;