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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module defint
)
15 ;;; this is the definite integration package.
16 ;; defint does definite integration by trying to find an
17 ;;appropriate method for the integral in question. the first thing that
18 ;;is looked at is the endpoints of the problem.
20 ;; i(grand,var,a,b) will be used for integrate(grand,var,a,b)
22 ;; References are to "Evaluation of Definite Integrals by Symbolic
23 ;; Manipulation", by Paul S. Wang,
24 ;; (http://www.lcs.mit.edu/publications/pubs/pdf/MIT-LCS-TR-092.pdf)
26 ;; nointegrate is a macsyma level flag which inhibits indefinite
28 ;; abconv is a macsyma level flag which inhibits the absolute
31 ;; $defint is the top level function that takes the user input
32 ;;and does minor changes to make the integrand ready for the package.
34 ;; next comes defint, which is the function that does the
35 ;;integration. it is often called recursively from the bowels of the
36 ;;package. defint does some of the easy cases and dispatches to:
38 ;; dintegrate. this program first sees if the limits of
39 ;;integration are 0,inf or minf,inf. if so it sends the problem to
40 ;;ztoinf or mtoinf, respectivly.
41 ;; else, dintegrate tries:
43 ;; intsc1 - does integrals of sin's or cos's or exp(%i var)'s
44 ;; when the interval is 0,2 %pi or 0,%pi.
45 ;; method is conversion to rational function and find
46 ;; residues in the unit circle. [wang, pp 107-109]
48 ;; ratfnt - does rational functions over finite interval by
49 ;; doing polynomial part directly, and converting
50 ;; the rational part to an integral on 0,inf and finding
51 ;; the answer by residues.
53 ;; zto1 - i(x^(k-1)*(1-x)^(l-1),x,0,1) = beta(k,l) or
54 ;; i(log(x)*x^(x-1)*(1-x)^(l-1),x,0,1) = psi...
57 ;; dintrad- i(x^m/(a*x^2+b*x+c)^(n+3/2),x,0,inf) [wang, p 74]
59 ;; dintlog- i(log(g(x))*f(x),x,0,inf) = 0 (by symmetry) or
60 ;; tries an integration by parts. (only routine to
61 ;; try integration by parts) [wang, pp 93-95]
63 ;; dintexp- i(f(exp(k*x)),x,a,inf) = i(f(x+1)/(x+1),x,0,inf)
64 ;; or i(f(x)/x,x,0,inf)/k. First case hold for a=0;
65 ;; the second for a=minf. [wang 96-97]
67 ;;dintegrate also tries indefinite integration based on certain
68 ;;predicates (such as abconv) and tries breaking up the integrand
69 ;;over a sum or tries a change of variable.
71 ;; ztoinf is the routine for doing integrals over the range 0,inf.
72 ;; it goes over a series of routines and sees if any will work:
74 ;; scaxn - sc(b*x^n) (sc stands for sin or cos) [wang, pp 81-83]
76 ;; ssp - a*sc^n(r*x)/x^m [wang, pp 86,87]
78 ;; zmtorat- rational function. done by multiplication by plog(-x)
79 ;; and finding the residues over the keyhole contour
82 ;; log*rat- r(x)*log^n(x) [wang, pp 89-92]
84 ;; logquad0 log(x)/(a*x^2+b*x+c) uses formula
85 ;; i(log(x)/(x^2+2*x*a*cos(t)+a^2),x,0,inf) =
86 ;; t*log(a)/sin(t). a better formula might be
87 ;; i(log(x)/(x+b)/(x+c),x,0,inf) =
88 ;; (log^2(b)-log^2(c))/(2*(b-c))
90 ;; batapp - x^(p-1)/(b*x^n+a)^m uses formula related to the beta
91 ;; function [wang, p 71]
92 ;; there is also a special case when m=1 and a*b<0
95 ;; sinnu - x^-a*n(x)/d(x) [wang, pp 69-70]
97 ;; ggr - x^r*exp(a*x^n+b)
99 ;; dintexp- see dintegrate
101 ;; ztoinf also tries 1/2*mtoinf if the integrand is an even function
103 ;; mtoinf is the routine for doing integrals on minf,inf.
104 ;; it too tries a series of routines and sees if any succeed.
106 ;; scaxn - when the integrand is an even function, see ztoinf
108 ;; mtosc - exp(%i*m*x)*r(x) by residues on either the upper half
109 ;; plane or the lower half plane, depending on whether
110 ;; m is positive or negative.
112 ;; zmtorat- does rational function by finding residues in upper
115 ;; dintexp- see dintegrate
117 ;; rectzto%pi2 - poly(x)*rat(exp(x)) by finding residues in
118 ;; rectangle [wang, pp98-100]
120 ;; ggrm - x^r*exp((x+a)^n+b)
122 ;; mtoinf also tries 2*ztoinf if the integrand is an even function.
124 (load-macsyma-macros rzmac
)
126 (declare-top (special *def2
* pcprntd
*mtoinf
* rsn
*
127 sn
* sd
* leadcoef checkfactors
129 *ul1
* *ll1
* *dflag bptu bptd plm
* zn
130 *updn ul ll exp pe
* pl
* rl
* pl
*1 rl
*1
131 loopstop
* var nn
* nd
* dn
* p
*
133 $trigexpandplus $trigexpandtimes
135 *sin-cos-recur
* *rad-poly-recur
* *dintlog-recur
*
136 *dintexp-recur
* defintdebug
*defint-assumptions
*
137 *current-assumptions
*
138 *global-defint-assumptions
*)
139 ;;;rsn* is in comdenom. does a ratsimp of numerator.
141 (special $intanalysis $abconvtest $noprincipal $nointegrate
)
143 (special $solveradcan $solvetrigwarn
*roots
*failures
144 $logabs $tlimswitch $maxposex $maxnegex
145 $trigsign $savefactors $radexpand $breakup $%emode
146 $float $exptsubst dosimp context rp-polylogp
147 %p%i half%pi %pi2 half%pi3 varlist genvar
148 $domain $m1pbranch errorsw
150 ;;LIMITP T Causes $ASKSIGN to do special things
151 ;;For DEFINT like eliminate epsilon look for prin-inf
152 ;;take realpart and imagpart.
154 ;;If LIMITP is non-null ask-integer conses
155 ;;its assumptions onto this list.
157 ;If this switch is () then RPART returns ATAN's
160 (declare-top (special infinities real-infinities infinitesimals
))
162 ;;These are really defined in LIMIT but DEFINT uses them also.
163 (cond ((not (boundp 'infinities
))
164 (setq infinities
'($inf $minf $infinity
))
165 (setq real-infinities
'($inf $minf
))
166 (setq infinitesimals
'($zeroa $zerob
))))
168 (defmvar $intanalysis t
169 "When @code{true}, definite integration tries to find poles in the integrand
170 in the interval of integration.")
172 (defmvar defintdebug
() "If true Defint prints out debugging information")
174 (defmvar integerl nil
175 "An integer-list for non-atoms found out to be `integer's")
177 (defmvar nonintegerl nil
178 "A non-integer-list for non-atoms found out to be `noninteger's")
180 ;; Not really sure what this is meant to do, but it's used by MTORAT,
181 ;; KEYHOLE, and POLELIST.
182 (defvar *semirat
* nil
)
184 (defun $defint
(exp var ll ul
)
186 ;; Distribute $defint over equations, lists, and matrices.
191 (mapcar #'(lambda (e)
192 (simplify ($defint e var ll ul
)))
195 (let ((*global-defint-assumptions
* ())
196 (integer-info ()) (integerl integerl
) (nonintegerl nonintegerl
))
197 (with-new-context (context)
199 (let ((*defint-assumptions
* ()) (*def2
* ()) (*rad-poly-recur
* ())
200 (*sin-cos-recur
* ()) (*dintexp-recur
* ()) (*dintlog-recur
* 0.
)
201 (ans nil
) (orig-exp exp
) (orig-var var
)
202 (orig-ll ll
) (orig-ul ul
)
203 (pcprntd nil
) (*nodiverg nil
) ($logabs t
) ; (limitp t)
205 ($%edispflag nil
) ; to get internal representation
206 ($m1pbranch
())) ;Try this out.
208 (make-global-assumptions) ;sets *global-defint-assumptions*
209 (setq exp
(ratdisrep exp
))
210 (setq var
(ratdisrep var
))
211 (setq ll
(ratdisrep ll
))
212 (setq ul
(ratdisrep ul
))
213 (cond (($constantp var
)
214 (merror (intl:gettext
"defint: variable of integration cannot be a constant; found ~M") var
))
215 (($subvarp var
) (setq var
(gensym))
216 (setq exp
($substitute var orig-var exp
))))
217 (cond ((not (atom var
))
218 (merror (intl:gettext
"defint: variable of integration must be a simple or subscripted variable.~%defint: found ~M") var
))
222 (setq exp
($substitute var orig-var exp
))))
223 (unless (lenient-extended-realp ll
)
224 (merror (intl:gettext
"defint: lower limit of integration must be real; found ~M") ll
))
225 (unless (lenient-extended-realp ul
)
226 (merror (intl:gettext
"defint: upper limit of integration must be real; found ~M") ll
))
228 (cond ((setq ans
(defint exp var ll ul
))
229 (setq ans
(subst orig-var var ans
))
230 (cond ((atom ans
) ans
)
231 ((and (free ans
'%limit
)
232 (free ans
'%integrate
)
233 (or (not (free ans
'$inf
))
234 (not (free ans
'$minf
))
235 (not (free ans
'$infinity
))))
237 ((not (free ans
'$und
))
238 `((%integrate
) ,orig-exp
,orig-var
,orig-ll
,orig-ul
))
240 (t `((%integrate
) ,orig-exp
,orig-var
,orig-ll
,orig-ul
))))
241 (forget-global-assumptions)))))
243 (defun eezz (exp ll ul
)
244 (cond ((or (polyinx exp var nil
)
245 (catch 'pin%ex
(pin%ex exp
)))
246 (setq exp
(antideriv exp
))
247 ;; If antideriv can't do it, returns nil
248 ;; use limit to evaluate every answer returned by antideriv.
249 (cond ((null exp
) nil
)
250 (t (intsubs exp ll ul
))))))
251 ;;;Hack the expression up for exponentials.
253 (defun sinintp (expr var
)
254 ;; Is this expr a candidate for SININT ?
255 (let ((expr (factor expr
))
258 (setq numer
($num expr
))
259 (setq denom
($denom expr
))
260 (cond ((polyinx numer var nil
)
261 (cond ((and (polyinx denom var nil
)
262 (deg-lessp denom var
2))
264 ;;ERF type things go here.
265 ((let ((exponent (%einvolve numer
)))
266 (and (polyinx exponent var nil
)
267 (deg-lessp exponent var
2)))
268 (cond ((free denom var
)
271 (defun deg-lessp (expr var power
)
272 (cond ((or (atom expr
)
276 (do ((ops (cdr expr
) (cdr ops
)))
278 (cond ((not (deg-lessp (car ops
) var power
))
281 (and (or (not (alike1 (cadr expr
) var
))
282 (and (numberp (caddr expr
))
283 (not (eq (asksign (m+ power
(m- (caddr expr
))))
285 (deg-lessp (cadr expr
) var power
)))
287 (member 'array
(car expr
))
288 (not (eq var
(caar expr
))))
289 ;; We have some subscripted variable that's not our variable
290 ;; (I think), so it's deg-lessp.
292 ;; FIXME: Is this the best way to handle this? Are there
293 ;; other cases we're mising here?
300 (setq ans
(sinint a var
))
301 (cond ((among '%integrate ans
) nil
)
302 (t (simplify ans
)))))
304 ;; This routine tries to take a limit a couple of ways.
305 (defmfun get-limit
(exp var val
&optional
(dir '$plus dir?
))
307 (funcall #'limit-no-err exp var val dir
)
308 (funcall #'limit-no-err exp var val
))))
309 (if (and ans
(not (among '%limit ans
)))
311 (when (member val
'($inf $minf
) :test
#'eq
)
312 (setq ans
(limit-no-err (maxima-substitute (m^t var -
1) var exp
)
315 (if (eq val
'$inf
) '$plus
'$minus
)))
316 (if (among '%limit ans
) nil ans
)))))
318 (defun limit-no-err (&rest argvec
)
319 (declare (special errorsw
))
320 (let ((errorsw t
) (ans nil
))
321 (setq ans
(catch 'errorsw
(apply #'$limit argvec
)))
322 (if (eq ans t
) nil ans
)))
324 ;; test whether fun2 is inverse of fun1 at val
325 (defun test-inverse (fun1 var1 fun2 var2 val
)
326 (let* ((out1 (let ((var var1
))
327 (no-err-sub val fun1
)))
328 (out2 (let ((var var2
))
329 (no-err-sub out1 fun2
))))
332 ;; integration change of variable
333 (defun intcv (nv flag
)
334 (let ((d (bx**n
+a nv
))
335 (*roots
()) (*failures
()) ($breakup
()))
336 (cond ((and (eq ul
'$inf
)
338 (equal (cadr d
) 1)) ())
339 ((eq var
'yx
) ; new var cannot be same as old var
342 ;; This is a hack! If nv is of the form b*x^n+a, we can
343 ;; solve the equation manually instead of using solve.
344 ;; Why? Because solve asks us for the sign of yx and
347 ;; Solve yx = b*x^n+a, for x. Any root will do. So we
348 ;; have x = ((yx-a)/b)^(1/n).
349 (destructuring-bind (a n b
)
351 (let ((root (power* (div (sub 'yx a
) b
) (inv n
))))
354 (cond (flag (intcv2 d nv
))
358 (putprop 'yx t
'internal
);; keep var from appearing in questions to user
359 (solve (m+t
'yx
(m*t -
1 nv
)) var
1.
)
360 (cond ((setq d
;; look for root that is inverse of nv
361 (do* ((roots *roots
(cddr roots
))
362 (root (caddar roots
) (caddar roots
)))
364 (if (and (or (real-infinityp ll
)
365 (test-inverse nv var root
'yx ll
))
366 (or (real-infinityp ul
)
367 (test-inverse nv var root
'yx ul
)))
369 (cond (flag (intcv2 d nv
))
373 ;; d: original variable (var) as a function of 'yx
375 ;; nv: new variable ('yx) as a function of original variable (var)
377 (cond ((and (intcv2 d nv
)
378 (equal ($imagpart
*ll1
*) 0)
379 (equal ($imagpart
*ul1
*) 0)
380 (not (alike1 *ll1
* *ul1
*)))
382 (defint exp1
'yx
*ll1
* *ul1
*)))))
384 ;; converts limits of integration to values for new variable 'yx
387 (and (cond ((and (zerop1 (m+ ll ul
))
389 (setq exp1
(m* 2 exp1
)
390 *ll1
* (limcp nv var
0 '$plus
)))
391 (t (setq *ll1
* (limcp nv var ll
'$plus
))))
392 (setq *ul1
* (limcp nv var ul
'$minus
))))
394 ;; wrapper around limit, returns nil if
395 ;; limit not found (nounform returned), or undefined ($und or $ind)
396 (defun limcp (a b c d
)
397 (let ((ans ($limit a b c d
)))
398 (cond ((not (or (null ans
)
404 ;; rewrites exp, the integrand in terms of var,
405 ;; into exp1, the integrand in terms of 'yx.
407 (setq exp1
(m* (sdiff d
'yx
)
408 (subst d var
(subst 'yx nv exp
))))
409 (setq exp1
(sratsimp exp1
)))
411 (defun integrand-changevar (d newvar exp var
)
415 (defun defint (exp var ll ul
)
416 (let ((old-assumptions *defint-assumptions
*)
417 (*current-assumptions
* ())
421 (setq *current-assumptions
* (make-defint-assumptions 'noask
))
422 (let ((exp (resimplify exp
))
423 (var (resimplify var
))
426 ;; D (not used? -- cwh)
427 ans nn
* dn
* nd
* $noprincipal
)
428 (cond ((setq ans
(defint-list exp var ll ul
))
433 ((not (among var exp
))
434 (cond ((or (member ul
'($inf $minf
) :test
#'eq
)
435 (member ll
'($inf $minf
) :test
#'eq
))
437 (t (setq ans
(m* exp
(m+ ul
(m- ll
))))
439 ;; Look for integrals which involve log and exp functions.
440 ;; Maxima has a special algorithm to get general results.
441 ((and (setq ans
(defint-log-exp exp var ll ul
)))
443 (let* ((exp (rmconst1 exp
))
445 (exp (%i-out-of-denom
(cdr exp
))))
446 (cond ((and (not $nointegrate
)
448 (or (among 'mqapply exp
)
449 (not (member (caar exp
)
450 '(mexpt mplus mtimes %sin %cos
451 %tan %sinh %cosh %tanh
452 %log %asin %acos %atan
455 %derivative
) :test
#'eq
))))
456 (cond ((setq ans
(antideriv exp
))
457 (setq ans
(intsubs ans ll ul
))
460 (setq exp
(tansc exp
))
461 (cond ((setq ans
(initial-analysis exp var ll ul
))
462 (return (m* c ans
))))
464 (restore-defint-assumptions old-assumptions
*current-assumptions
*))))
466 (defun defint-list (exp var ll ul
)
468 (let ((ans (cons (car exp
)
471 (defint sub-exp var ll ul
))
473 (cond (ans (simplify ans
))
477 (defun initial-analysis (exp var ll ul
)
478 (let ((pole (cond ((not $intanalysis
)
479 '$no
) ;don't do any checking.
480 (t (poles-in-interval exp var ll ul
)))))
481 (cond ((eq pole
'$no
)
482 (cond ((and (oddfn exp var
)
483 (or (and (eq ll
'$minf
)
485 (eq ($sign
(m+ ll ul
))
487 (t (parse-integrand exp var ll ul
))))
488 ((eq pole
'$unknown
) ())
489 (t (principal-value-integral exp var ll ul pole
)))))
491 (defun parse-integrand (exp var ll ul
)
493 (cond ((setq ans
(eezz exp ll ul
)) ans
)
495 (setq ans
(method-by-limits exp var ll ul
))) ans
)
497 (setq ans
(intbyterm exp t
))) ans
)
498 ((setq ans
(method-by-limits exp var ll ul
)) ans
)
502 (cond ((not (freeof var e
))
507 (defun method-by-limits (exp var ll ul
)
508 (let ((old-assumptions *defint-assumptions
*))
509 (setq *current-assumptions
* (make-defint-assumptions 'noask
))
510 ;;Should be a PROG inside of unwind-protect, but Multics has a compiler
511 ;;bug wrt. and I want to test this code now.
513 (cond ((and (and (eq ul
'$inf
)
516 ((and (and (eq ul
'$inf
)
519 ;;;This seems((and (and (eq ul '$inf)
520 ;;;fairly losing (setq exp (subin (m+ ll var) exp))
522 ;;; (ztoinf exp var)))
525 (eq ($asksign ul
) '$pos
)
527 ;; ((and (and (equal ul 1.)
528 ;; (equal ll 0.)) (zto1 exp)))
529 (t (dintegrate exp var ll ul
)))
530 (restore-defint-assumptions old-assumptions
*defint-assumptions
*))))
533 (defun dintegrate (exp var ll ul
)
534 (let ((ans nil
) (arg nil
) (*scflag
* nil
)
535 (*dflag nil
) ($%emode t
))
536 ;;;NOT COMPLETE for sin's and cos's.
537 (cond ((and (not *sin-cos-recur
*)
541 ((and (not *rad-poly-recur
*)
542 (notinvolve exp
'(%log
))
543 (not (%einvolve exp
))
544 (method-radical-poly exp var ll ul
)))
545 ((and (not (equal *dintlog-recur
* 2.
))
546 (setq arg
(involve exp
'(%log
)))
548 ((and (not *dintexp-recur
*)
549 (setq arg
(%einvolve exp
))
551 ((and (not (ratp exp var
))
552 (setq ans
(let (($trigexpandtimes nil
)
555 (setq ans
($expand ans
))
556 (not (alike1 ans exp
))
558 ((setq ans
(antideriv exp
))
562 (defun method-radical-poly (exp var ll ul
)
564 (let ((*rad-poly-recur
* t
) ;recursion stopper
566 (cond ((and (sinintp exp var
)
567 (setq result
(antideriv exp
))
568 (intsubs result ll ul
)))
570 (setq result
(ratfnt exp
))))
575 (setq result
(cv exp
))))
578 (defun principal-value-integral (exp var ll ul poles
)
579 (let ((anti-deriv ()))
580 (cond ((not (null (setq anti-deriv
(antideriv exp
))))
581 (cond ((not (null poles
))
583 (cond ((take-principal anti-deriv ll ul poles
))
586 ;; adds up integrals of ranges between each pair of poles.
587 ;; checks if whole thing is divergent as limits of integration approach poles.
588 (defun take-principal (anti-deriv ll ul poles
&aux ans merged-list
)
589 ;;; calling $logcontract causes antiderivative of 1/(1-x^5) to blow up
590 ;; (setq anti-deriv (cond ((involve anti-deriv '(%log))
591 ;; ($logcontract anti-deriv))
594 (setq merged-list
(interval-list poles ll ul
))
595 (do ((current-pole (cdr merged-list
) (cdr current-pole
))
596 (previous-pole merged-list
(cdr previous-pole
)))
597 ((null current-pole
) t
)
599 (intsubs anti-deriv
(m+ (caar previous-pole
) 'epsilon
)
600 (m+ (caar current-pole
) (m- 'epsilon
))))))
602 (setq ans
(get-limit (get-limit ans
'epsilon
0 '$plus
) 'prin-inf
'$inf
))
604 (cond ((or (null ans
)
605 (not (free ans
'$infinity
))
606 (not (free ans
'$ind
))) ())
607 ((or (among '$minf ans
)
611 (t (principal) ans
)))
613 (defun interval-list (pole-list ll ul
)
614 (let ((first (car (first pole-list
)))
615 (last (caar (last pole-list
))))
618 (setq pole-list
(subst 'prin-inf
'$inf pole-list
))))
621 (setq pole-list
(append pole-list
(list (cons ul
'ignored
))))))
624 (setq pole-list
(subst (m- 'prin-inf
) '$minf pole-list
))))
625 (t (if (eq ll
'$minf
)
626 (setq ll
(m- 'prin-inf
)))
627 (setq pole-list
(append (list (cons ll
'ignored
)) pole-list
)))))
630 ;; Assumes EXP is a rational expression with no polynomial part and
631 ;; converts the finite integration to integration over a half-infinite
632 ;; interval. The substitution y = (x-a)/(b-x) is used. Equivalently,
633 ;; x = (b*y+a)/(y+1).
635 ;; (I'm guessing CV means Change Variable here.)
637 (if (not (or (real-infinityp ll
) (real-infinityp ul
)))
638 ;; FIXME! This is a hack. We apply the transformation with
639 ;; symbolic limits and then substitute the actual limits later.
640 ;; That way method-by-limits (usually?) sees a simpler
643 ;; See Bugs 938235 and 941457. These fail because $FACTOR is
644 ;; unable to factor the transformed result. This needs more
645 ;; work (in other places).
646 (let ((trans (integrand-changevar (m// (m+t
'll
(m*t
'ul
'yx
))
649 ;; If the limit is a number, use $substitute so we simplify
650 ;; the result. Do we really want to do this?
651 (setf trans
(if (mnump ll
)
652 ($substitute ll
'll trans
)
653 (subst ll
'll trans
)))
654 (setf trans
(if (mnump ul
)
655 ($substitute ul
'ul trans
)
656 (subst ul
'ul trans
)))
657 (method-by-limits trans
'yx
0.
'$inf
))
660 ;; Integrate rational functions over a finite interval by doing the
661 ;; polynomial part directly, and converting the rational part to an
662 ;; integral from 0 to inf. This is evaluated via residues.
665 ;; PQR divides the rational expression and returns the quotient
667 (flet ((try-antideriv (e lo hi
)
668 (let ((ans (antideriv e
)))
670 (intsubs ans lo hi
)))))
672 (cond ((equal 0.
(car e
))
673 ;; No polynomial part
674 (let ((ans (try-antideriv exp ll ul
)))
679 ;; Only polynomial part
680 (eezz (car e
) ll ul
))
682 ;; A non-zero quotient and remainder. Combine the results
684 (let ((ans (try-antideriv (m// (cdr e
) dn
*) ll ul
)))
686 (m+t
(eezz (car e
) ll ul
)
689 (m+t
(eezz (car e
) ll ul
)
690 (cv (m// (cdr e
) dn
*)))))))))))
692 ;; I think this takes a rational expression E, and finds the
693 ;; polynomial part. A cons is returned. The car is the quotient and
694 ;; the cdr is the remainder.
696 (let ((varlist (list var
)))
698 (setq e
(cdr (ratrep* e
)))
699 (setq dn
* (pdis (ratdenominator e
)))
700 (setq e
(pdivide (ratnumerator e
) (ratdenominator e
)))
701 (cons (simplify (rdis (car e
))) (simplify (rdis (cadr e
))))))
704 (defun intbyterm (exp *nodiverg
)
705 (let ((saved-exp exp
))
707 (let ((ans (catch 'divergent
708 (andmapcar #'(lambda (new-exp)
710 (defint new-exp var ll ul
)))
712 (cond ((null ans
) nil
)
714 (let ((*nodiverg nil
))
715 (cond ((setq ans
(antideriv saved-exp
))
718 (t (sratsimp (m+l ans
))))))
719 ;;;If leadop isn't plus don't do anything.
725 (a (cond ((and (zerop1 ($limit d var ll
'$plus
))
726 (eq (limit-pole (m+ exp
(m+ (m- ll
) var
))
731 (b (cond ((and (zerop1 ($limit d var ul
'$minus
))
732 (eq (limit-pole (m+ exp
(m+ ul
(m- var
)))
740 (cond (*nodiverg
(throw 'divergent
'divergent
))
741 (t (merror (intl:gettext
"defint: integral is divergent.")))))
743 (defun make-defint-assumptions (ask-or-not)
744 (cond ((null (order-limits ask-or-not
)) ())
745 (t (mapc 'forget
*defint-assumptions
*)
746 (setq *defint-assumptions
* ())
747 (let ((sign-ll (cond ((eq ll
'$inf
) '$pos
)
748 ((eq ll
'$minf
) '$neg
)
749 (t ($sign
($limit ll
)))))
750 (sign-ul (cond ((eq ul
'$inf
) '$pos
)
751 ((eq ul
'$minf
) '$neg
)
752 (t ($sign
($limit ul
)))))
753 (sign-ul-ll (cond ((and (eq ul
'$inf
)
754 (not (eq ll
'$inf
))) '$pos
)
756 (not (eq ll
'$minf
))) '$neg
)
757 (t ($sign
($limit
(m+ ul
(m- ll
))))))))
758 (cond ((eq sign-ul-ll
'$pos
)
759 (setq *defint-assumptions
*
760 `(,(assume `((mgreaterp) ,var
,ll
))
761 ,(assume `((mgreaterp) ,ul
,var
)))))
762 ((eq sign-ul-ll
'$neg
)
763 (setq *defint-assumptions
*
764 `(,(assume `((mgreaterp) ,var
,ul
))
765 ,(assume `((mgreaterp) ,ll
,var
))))))
766 (cond ((and (eq sign-ll
'$pos
)
768 (setq *defint-assumptions
*
769 `(,(assume `((mgreaterp) ,var
0))
770 ,@*defint-assumptions
*)))
771 ((and (eq sign-ll
'$neg
)
773 (setq *defint-assumptions
*
774 `(,(assume `((mgreaterp) 0 ,var
))
775 ,@*defint-assumptions
*)))
776 (t *defint-assumptions
*))))))
778 (defun restore-defint-assumptions (old-assumptions assumptions
)
779 (do ((llist assumptions
(cdr llist
)))
781 (forget (car llist
)))
782 (do ((llist old-assumptions
(cdr llist
)))
784 (assume (car llist
))))
786 (defun make-global-assumptions ()
787 (setq *global-defint-assumptions
*
788 (cons (assume '((mgreaterp) *z
* 0.
))
789 *global-defint-assumptions
*))
790 ;; *Z* is a "zero parameter" for this package.
791 ;; Its also used to transform.
792 ;; limit(exp,var,val,dir) -- limit(exp,tvar,0,dir)
793 (setq *global-defint-assumptions
*
794 (cons (assume '((mgreaterp) epsilon
0.
))
795 *global-defint-assumptions
*))
796 (setq *global-defint-assumptions
*
797 (cons (assume '((mlessp) epsilon
1.0e-8))
798 *global-defint-assumptions
*))
799 ;; EPSILON is used in principal value code to denote the familiar
800 ;; mathematical entity.
801 (setq *global-defint-assumptions
*
802 (cons (assume '((mgreaterp) prin-inf
1.0e+8))
803 *global-defint-assumptions
*)))
805 ;;; PRIN-INF Is a special symbol in the principal value code used to
806 ;;; denote an end-point which is proceeding to infinity.
808 (defun forget-global-assumptions ()
809 (do ((llist *global-defint-assumptions
* (cdr llist
)))
811 (forget (car llist
)))
812 (cond ((not (null integer-info
))
813 (do ((llist integer-info
(cdr llist
)))
815 (i-$remove
`(,(cadar llist
) ,(caddar llist
)))))))
817 (defun order-limits (ask-or-not)
818 (cond ((or (not (equal ($imagpart ll
) 0))
819 (not (equal ($imagpart ul
) 0))) ())
820 (t (cond ((alike1 ll
(m*t -
1 '$inf
))
822 (cond ((alike1 ul
(m*t -
1 '$inf
))
824 (cond ((alike1 ll
(m*t -
1 '$minf
))
826 (cond ((alike1 ul
(m*t -
1 '$minf
))
828 (cond ((eq ul
'$inf
) nil
)
830 (setq exp
(subin (m- var
) exp
))
836 (setq exp
(m- exp
))))
837 ;;Fix limits so that ll < ul.
838 (let ((d (complm ask-or-not
)))
846 (defun complm (ask-or-not)
847 (let ((askflag (cond ((eq ask-or-not
'ask
) t
)
850 (cond ((alike1 ul ll
) 0.
)
851 ((eq (setq a
(cond (askflag ($asksign
($limit
(m+t ul
(m- ll
)))))
852 (t ($sign
($limit
(m+t ul
(m- ll
)))))))
858 ;; Substitute a and b into integral e
860 ;; Looks for discontinuties in integral, and works around them.
863 ;; integrate(x^(2*n)*exp(-(x)^2),x) ==>
864 ;; -gamma_incomplete((2*n+1)/2,x^2)*x^(2*n+1)*abs(x)^(-2*n-1)/2
866 ;; the integral has a discontinuity at x=0.
868 (defun intsubs (e a b
)
869 (let ((edges (cond ((not $intanalysis
)
870 '$no
) ;don't do any checking.
871 (t (discontinuities-in-interval
872 (let (($algebraic t
))
876 (cond ((or (eq edges
'$no
)
877 (eq edges
'$unknown
))
878 (whole-intsubs e a b
))
880 (do* ((l edges
(cdr l
))
883 (b1 (cadr l
) (cadr l
)))
884 ((null (cdr l
)) (if (every (lambda (x) x
) total
)
887 (whole-intsubs e a1 b1
)
890 ;; look for terms with a negative exponent
892 ;; recursively traverses exp in order to find discontinuities such as
893 ;; erfc(1/x-x) at x=0
894 (defun discontinuities-denom (exp)
896 ((and (eq (caar exp
) 'mexpt
)
897 (not (freeof var
(cadr exp
)))
898 (not (member ($sign
(caddr exp
)) '($pos $pz
))))
899 (m^
(cadr exp
) (m- (caddr exp
))))
901 (m*l
(mapcar #'discontinuities-denom
(cdr exp
))))))
903 ;; returns list of places where exp might be discontinuous in var.
904 ;; list begins with ll and ends with ul, and include any values between
906 ;; return '$no or '$unknown if no discontinuities found.
907 (defun discontinuities-in-interval (exp var ll ul
)
908 (let* ((denom (discontinuities-denom exp
))
909 (roots (real-roots denom var
)))
910 (cond ((eq roots
'$failure
)
914 (t (do ((dummy roots
(cdr dummy
))
919 (sortgreat pole-list
)
922 (let ((soltn (caar dummy
)))
923 ;; (multiplicity (cdar dummy)) ;; not used
924 (if (strictly-in-interval soltn ll ul
)
925 (push soltn pole-list
))))))))
928 ;; Carefully substitute the integration limits A and B into the
930 (defun whole-intsubs (e a b
)
931 (cond ((easy-subs e a b
))
932 (t (setq *current-assumptions
*
933 (make-defint-assumptions 'ask
)) ;get forceful!
934 (let (($algebraic t
))
935 (setq e
(sratsimp e
))
936 (cond ((limit-subs e a b
))
937 (t (same-sheet-subs e a b
)))))))
939 ;; Try easy substitutions. Return NIL if we can't.
940 (defun easy-subs (e ll ul
)
941 (cond ((or (infinityp ll
) (infinityp ul
))
942 ;; Infinite limits aren't easy
945 (cond ((or (polyinx e var
())
946 (and (not (involve e
'(%log %asin %acos %atan %asinh %acosh %atanh %atan2
947 %gamma_incomplete %expintegral_ei
)))
948 (free ($denom e
) var
)))
949 ;; It's easy if we have a polynomial. I (rtoy) think
950 ;; it's also easy if the denominator is free of the
951 ;; integration variable and also if the expression
952 ;; doesn't involve inverse functions.
954 ;; gamma_incomplete and expintegral_ie
955 ;; included because of discontinuity in
956 ;; gamma_incomplete(0, exp(%i*x)) and
957 ;; expintegral_ei(exp(%i*x))
959 ;; XXX: Are there other cases we've forgotten about?
961 ;; So just try to substitute the limits into the
962 ;; expression. If no errors are produced, we're done.
963 (let ((ll-val (no-err-sub ll e
))
964 (ul-val (no-err-sub ul e
)))
965 (cond ((or (eq ll-val t
)
967 ;; no-err-sub has returned T. An error was catched.
974 (defun limit-subs (e ll ul
)
975 (cond ((involve e
'(%atan %gamma_incomplete %expintegral_ei
))
976 ()) ; functions with discontinuities
977 (t (setq e
($multthru e
))
978 (let ((a1 ($limit e var ll
'$plus
))
979 (a2 ($limit e var ul
'$minus
)))
980 (cond ((member a1
'($inf $minf $infinity
) :test
#'eq
)
981 (cond ((member a2
'($inf $minf $infinity
) :test
#'eq
)
982 (cond ((eq a2 a1
) ())
985 ((member a2
'($inf $minf $infinity
) :test
#'eq
) (diverg))
986 ((or (member a1
'($und $ind
) :test
#'eq
)
987 (member a2
'($und $ind
) :test
#'eq
)) ())
990 ;;;This function works only on things with ATAN's in them now.
991 (defun same-sheet-subs (exp ll ul
&aux ll-ans ul-ans
)
992 ;; POLES-IN-INTERVAL doesn't know about the poles of tan(x). Call
993 ;; trigsimp to convert tan into sin/cos, which POLES-IN-INTERVAL
994 ;; knows how to handle.
996 ;; XXX Should we fix POLES-IN-INTERVAL instead?
998 ;; XXX Is calling trigsimp too much? Should we just only try to
999 ;; substitute sin/cos for tan?
1001 ;; XXX Should the result try to convert sin/cos back into tan? (A
1002 ;; call to trigreduce would do it, among other things.)
1003 (let* ((exp (mfuncall '$trigsimp exp
))
1004 (poles (atan-poles exp ll ul
)))
1005 ;;POLES -> ((mlist) ((mequal) ((%atan) foo) replacement) ......)
1006 ;;We can then use $SUBSTITUTE
1007 (setq ll-ans
(limcp exp var ll
'$plus
))
1008 (setq exp
(sratsimp ($substitute poles exp
)))
1009 (setq ul-ans
(limcp exp var ul
'$minus
))
1012 (not (member ll-ans infinities
))
1013 (not (member ul-ans infinities
)))
1017 (defun atan-poles (exp ll ul
)
1018 `((mlist) ,@(atan-pole1 exp ll ul
)))
1020 (defun atan-pole1 (exp ll ul
&aux ipart
)
1023 ((matanp exp
) ;neglect multiplicity and '$unknowns for now.
1024 (desetq (exp . ipart
) (trisplit exp
))
1026 ((not (equal (sratsimp ipart
) 0)) ())
1027 (t (let ((pole (poles-in-interval (let (($algebraic t
))
1028 (sratsimp (cadr exp
)))
1030 (cond ((and pole
(not (or (eq pole
'$unknown
)
1032 (do ((l pole
(cdr l
)) (llist ()))
1035 ((zerop1 (m- (caar l
) ll
)) t
) ; don't worry about discontinuity
1036 ((zerop1 (m- (caar l
) ul
)) t
) ; at boundary of integration
1037 (t (let ((low-lim ($limit
(cadr exp
) var
(caar l
) '$minus
))
1038 (up-lim ($limit
(cadr exp
) var
(caar l
) '$plus
)))
1039 (cond ((and (not (eq low-lim up-lim
))
1040 (real-infinityp low-lim
)
1041 (real-infinityp up-lim
))
1042 (let ((change (if (eq low-lim
'$minf
)
1045 (setq llist
(cons `((mequal simp
) ,exp
,(m+ exp change
))
1046 llist
)))))))))))))))
1047 (t (do ((l (cdr exp
) (cdr l
))
1050 (setq llist
(append llist
(atan-pole1 (car l
) ll ul
)))))))
1052 (defun difapply (n d s fn1
)
1053 (prog (k m r $noprincipal
)
1054 (cond ((eq ($asksign
(m+ (deg d
) (m- s
) (m- 2.
))) '$neg
)
1056 (setq $noprincipal t
)
1057 (cond ((or (not (mexptp d
))
1058 (not (numberp (setq r
(caddr d
)))))
1062 (equal 1.
(deg (cadr d
))))
1064 (setq m
(deg (setq d
(cadr d
))))
1065 (setq k
(m// (m+ s
2.
) m
))
1066 (cond ((eq (ask-integer (m// (m+ s
2.
) m
) '$any
) '$yes
)
1068 (t (setq k
(m+ 1 k
))))
1069 (cond ((eq ($sign
(m+ r
(m- k
))) '$pos
)
1070 (return (diffhk fn1 n d k
(m+ r
(m- k
))))))))
1072 (defun diffhk (fn1 n d r m
)
1075 (setq d1
(funcall fn1 n
1078 (cond (d1 (return (difap1 d1 r
'*z
* m
0.
))))))
1080 (defun principal nil
1081 (cond ($noprincipal
(diverg))
1083 (format t
"Principal Value~%")
1086 ;; e is of form poly(x)*exp(m*%i*x)
1087 ;; s is degree of denominator
1088 ;; adds e to bptu or bptd according to sign of m
1091 (cond ((or (mnump e
) (constant e
))
1092 (setq bptu
(cons e bptu
)))
1093 (t (setq e
(rmconst1 e
))
1097 (setq e
(catch 'ptimes%e
(ptimes%e nn
* nd
*)))
1098 (cond ((null e
) nil
)
1099 (t (setq e
(m* c e
))
1100 (cond (*updn
(setq bptu
(cons e bptu
)))
1101 (t (setq bptd
(cons e bptd
))))))))))
1103 ;; check term is of form poly(x)*exp(m*%i*x)
1104 ;; n is degree of denominator
1105 (defun ptimes%e
(term n
)
1106 (cond ((and (mexptp term
)
1107 (eq (cadr term
) '$%e
)
1108 (polyinx (caddr term
) var nil
)
1109 (eq ($sign
(m+ (deg ($realpart
(caddr term
))) -
1))
1111 (eq ($sign
(m+ (deg (setq nn
* ($imagpart
(caddr term
))))
1114 (cond ((eq ($asksign
(ratdisrep (ratcoef nn
* var
))) '$pos
)
1116 (t (setq *updn nil
)))
1118 ((and (mtimesp term
)
1119 (setq nn
* (polfactors term
))
1120 (or (null (car nn
*))
1121 (eq ($sign
(m+ n
(m- (deg (car nn
*)))))
1123 (ptimes%e
(cadr nn
*) n
)
1125 (t (throw 'ptimes%e nil
))))
1127 (defun csemidown (n d var
)
1128 (let ((pcprntd t
)) ;Not sure what to do about PRINCIPAL values here.
1129 (princip (res n d
#'lowerhalf
#'(lambda (x)
1130 (cond ((equal ($imagpart x
) 0) t
)
1133 (defun lowerhalf (j)
1134 (eq ($asksign
($imagpart j
)) '$neg
))
1136 (defun upperhalf (j)
1137 (eq ($asksign
($imagpart j
)) '$pos
))
1140 (defun csemiup (n d var
)
1141 (let ((pcprntd t
)) ;I'm not sure what to do about PRINCIPAL values here.
1142 (princip (res n d
#'upperhalf
#'(lambda (x)
1143 (cond ((equal ($imagpart x
) 0) t
)
1147 (cond ((null n
) nil
)
1148 (t (m*t
'$%i
($rectform
(m+ (cond ((car n
)
1156 ;; exponentialize sin and cos
1159 ((polyinx e var nil
) e
)
1160 ((eq (caar e
) '%sin
)
1163 (m+t
(m^t
'$%e
(m*t
'$%i
(cadr e
)))
1164 (m- (m^t
'$%e
(m*t
(m- '$%i
) (cadr e
)))))))
1165 ((eq (caar e
) '%cos
)
1166 (mul* '((rat) 1.
2.
)
1167 (m+t
(m^t
'$%e
(m*t
'$%i
(cadr e
)))
1168 (m^t
'$%e
(m*t
(m- '$%i
) (cadr e
))))))
1170 (cons (list (caar e
)) (mapcar #'sconvert
(cdr e
)))))))
1172 (defun polfactors (exp)
1174 (cond ((mplusp exp
) nil
)
1175 (t (cond ((mtimesp exp
)
1176 (setq exp
(reverse (cdr exp
))))
1177 (t (setq exp
(list exp
))))
1178 (mapc #'(lambda (term)
1179 (cond ((polyinx term var nil
)
1181 (t (push term rest
))))
1183 (list (m*l poly
) (m*l rest
))))))
1187 (cond ((atom e
) (return e
))
1188 ((not (among '$%e e
)) (return e
))
1191 (setq d
($imagpart
(caddr e
)))
1192 (return (m* (m^t
'$%e
($realpart
(caddr e
)))
1194 (m*t
'$%i
`((%sin
) ,d
))))))
1195 (t (return (simplify (cons (list (caar e
))
1196 (mapcar #'esap
(cdr e
)))))))))
1198 ;; computes integral from minf to inf for expressions of the form
1199 ;; exp(%i*m*x)*r(x) by residues on either the upper half
1200 ;; plane or the lower half plane, depending on whether
1201 ;; m is positive or negative. [wang p. 77]
1203 ;; exponentializes sin and cos before applying residue method.
1204 ;; can handle some expressions with poles on real line, such as
1206 (defun mtosc (grand)
1211 plf bptu bptd s upans downans
)
1212 (cond ((not (or (polyinx d var nil
)
1213 (and (setq grand
(%einvolve d
))
1215 (polyinx (setq d
(sratsimp (m// d
(m^t
'$%e grand
))))
1218 (setq n
(m// n
(m^t
'$%e grand
)))))) nil
)
1219 ((equal (setq s
(deg d
)) 0) nil
)
1220 ;;;Above tests for applicability of this method.
1221 ((and (or (setq plf
(polfactors n
)) t
)
1222 (setq n
($expand
(cond ((car plf
)
1223 (m*t
'x
* (sconvert (cadr plf
))))
1225 (cond ((mplusp n
) (setq n
(cdr n
)))
1226 (t (setq n
(list n
))))
1228 (cond ((polyinx term var nil
)
1229 ;; call to $expand can create rational terms
1230 ;; with no exp(m*%i*x)
1231 (setq ratterms
(cons term ratterms
)))
1234 ;;;Function RIB sets up the values of BPTU and BPTD
1236 (setq bptu
(subst (car plf
) 'x
* bptu
))
1237 (setq bptd
(subst (car plf
) 'x
* bptd
))
1238 (setq ratterms
(subst (car plf
) 'x
* ratterms
))
1239 t
) ;CROCK, CROCK. This is TERRIBLE code.
1241 ;;;If there is BPTU then CSEMIUP must succeed.
1242 ;;;Likewise for BPTD.
1245 (let (($intanalysis nil
))
1246 ;; The original integrand was already
1247 ;; determined to have no poles by initial-analysis.
1248 ;; If individual terms of the expansion have poles, the poles
1249 ;; must cancel each other out, so we can ignore them.
1250 (try-defint (m// (m+l ratterms
) d
) var
'$minf
'$inf
))
1252 ;; if integral of ratterms is divergent, ratans is nil,
1253 ;; and mtosc returns nil
1255 (cond (bptu (setq upans
(csemiup (m+l bptu
) d var
)))
1257 (cond (bptd (setq downans
(csemidown (m+l bptd
) d var
)))
1258 (t (setq downans
0))))
1260 (sratsimp (m+ ratans
1261 (m* '$%pi
(m+ upans
(m- downans
)))))))))
1264 (defun evenfn (e var
)
1265 (let ((temp (m+ (m- e
)
1267 ($substitute
(m- var
) var e
))
1268 (t ($ratsubst
(m- var
) var e
))))))
1269 (cond ((zerop1 temp
)
1271 ((zerop1 (sratsimp temp
))
1275 (defun oddfn (e var
)
1276 (let ((temp (m+ e
(cond ((atom var
)
1277 ($substitute
(m- var
) var e
))
1278 (t ($ratsubst
(m- var
) var e
))))))
1279 (cond ((zerop1 temp
)
1281 ((zerop1 (sratsimp temp
))
1285 (defun ztoinf (grand var
)
1286 (prog (n d sn
* sd
* varlist
1288 ans r $savefactors checkfactors temp test-var
)
1289 (setq $savefactors t sn
* (setq sd
* (list 1.
)))
1290 (cond ((eq ($sign
(m+ loopstop
* -
1))
1293 ((setq temp
(or (scaxn grand
)
1296 ((involve grand
'(%sin %cos %tan
))
1297 (setq grand
(sconvert grand
))
1300 (cond ((polyinx grand var nil
)
1302 ((and (ratp grand var
)
1304 (andmapcar #'snumden
(cdr grand
)))
1312 (setq n
(rmconst1 nn
*))
1313 (setq d
(rmconst1 dn
*))
1318 (cond ((polyinx d var nil
)
1321 (cond ((and (setq r
(findp n
))
1322 (eq (ask-integer r
'$integer
) '$yes
)
1323 (setq test-var
(bxm d s
))
1324 (setq ans
(apply 'fan
(cons (m+ 1. r
) test-var
))))
1325 (return (m* (m// nc dc
) (sratsimp ans
))))
1326 ((and (ratp grand var
)
1327 (setq ans
(zmtorat n
(cond ((mtimesp d
) d
)
1330 (return (m* (m// nc dc
) ans
)))
1331 ((and (evenfn d var
)
1332 (setq nn
* (p*lognxp n s
)))
1333 (setq ans
(log*rat
(car nn
*) d
(cadr nn
*)))
1334 (return (m* (m// nc dc
) ans
)))
1335 ((involve grand
'(%log
))
1336 (cond ((setq ans
(logquad0 grand
))
1337 (return (m* (m// nc dc
) ans
)))
1340 (cond ((setq temp
(batapp grand
))
1344 (cond ((let ((*mtoinf
* nil
))
1345 (setq temp
(ggr grand t
)))
1348 (cond ((let ((*nodiverg t
))
1349 (setq ans
(catch 'divergent
1350 (andmapcar #'(lambda (g)
1353 (cond ((eq ans
'divergent
) nil
)
1354 (t (return (sratsimp (m+l ans
)))))))))
1356 (cond ((and (evenfn grand var
)
1357 (setq loopstop
* (m+ 1 loopstop
*))
1358 (setq ans
(method-by-limits grand var
'$minf
'$inf
)))
1359 (return (m*t
'((rat) 1.
2.
) ans
)))
1362 (defun ztorat (n d s
)
1363 (cond ((and (null *dflag
)
1364 (setq s
(difapply n d s
#'ztorat
)))
1366 ((setq n
(let ((plogabs ()))
1367 (keyhole (m* `((%plog
) ,(m- var
)) n
) d var
)))
1370 ;; Let's not signal an error here. Return nil so that we
1371 ;; eventually return a noun form if no other algorithm gives
1374 (merror (intl:gettext
"defint: keyhole integration failed.~%"))
1379 (defun logquad0 (exp)
1380 (let ((a ()) (b ()) (c ()))
1381 (cond ((setq exp
(logquad exp
))
1382 (setq a
(car exp
) b
(cadr exp
) c
(caddr exp
))
1383 ($asksign b
) ;let the data base know about the sign of B.
1384 (cond ((eq ($asksign c
) '$pos
)
1385 (setq c
(m^
(m// c a
) '((rat) 1.
2.
)))
1387 `((%acos
) ,(add* 'epsilon
(m// b
(mul* 2. a c
))))))
1388 (setq a
(m// (m* b
`((%log
) ,c
))
1389 (mul* a
(simplify `((%sin
) ,b
)) c
)))
1390 (get-limit a
'epsilon
0 '$plus
))))
1393 (defun logquad (exp)
1394 (let ((varlist (list var
)))
1396 (setq exp
(cdr (ratrep* exp
)))
1397 (cond ((and (alike1 (pdis (car exp
))
1399 (not (atom (cdr exp
)))
1400 (equal (cadr (cdr exp
)) 2.
)
1401 (not (equal (ptterm (cddr exp
) 0.
) 0.
)))
1402 (setq exp
(mapcar 'pdis
(cdr (oddelm (cdr exp
)))))))))
1404 (defun mtoinf (grand var
)
1405 (prog (ans sd
* sn
* p
* pe
* n d s nc dc $savefactors checkfactors temp
)
1406 (setq $savefactors t
)
1407 (setq sn
* (setq sd
* (list 1.
)))
1408 (cond ((eq ($sign
(m+ loopstop
* -
1)) '$pos
)
1410 ((involve grand
'(%sin %cos
))
1411 (cond ((and (evenfn grand var
)
1412 (or (setq temp
(scaxn grand
))
1413 (setq temp
(ssp grand
))))
1414 (return (m*t
2. temp
)))
1415 ((setq temp
(mtosc grand
))
1418 ((among '$%i
(%einvolve grand
))
1419 (cond ((setq temp
(mtosc grand
))
1422 (setq grand
($exponentialize grand
)) ; exponentializing before numden
1423 (cond ((polyinx grand var nil
) ; avoids losing multiplicities [ 1309432 ]
1425 ((and (ratp grand var
)
1427 (andmapcar #'snumden
(cdr grand
)))
1428 (setq nn
* (m*l sn
*) sn
* nil
)
1429 (setq dn
* (m*l sd
*) sd
* nil
))
1431 (setq n
(rmconst1 nn
*))
1432 (setq d
(rmconst1 dn
*))
1437 (cond ((polyinx d var nil
)
1439 (cond ((and (not (%einvolve grand
))
1440 (notinvolve exp
'(%sinh %cosh %tanh
))
1442 (eq (ask-integer p
* '$integer
) '$yes
)
1443 (setq pe
* (bxm d s
)))
1444 (cond ((and (eq (ask-integer (caddr pe
*) '$even
) '$yes
)
1445 (eq (ask-integer p
* '$even
) '$yes
))
1446 (cond ((setq ans
(apply 'fan
(cons (m+ 1. p
*) pe
*)))
1447 (setq ans
(m*t
2. ans
))
1448 (return (m* (m// nc dc
) ans
)))))
1449 ((equal (car pe
*) 1.
)
1450 (cond ((and (setq ans
(apply 'fan
(cons (m+ 1. p
*) pe
*)))
1451 (setq nn
* (fan (m+ 1. p
*)
1456 (setq ans
(m+ ans
(m*t
(m^ -
1 p
*) nn
*)))
1457 (return (m* (m// nc dc
) ans
))))))))
1458 (cond ((ratp grand var
)
1459 (setq ans
(m*t
'$%pi
(zmtorat n
(cond ((mtimesp d
) d
)
1463 (return (m* (m// nc dc
) ans
)))
1464 ((and (or (%einvolve grand
)
1465 (involve grand
'(%sinh %cosh %tanh
)))
1466 (p*pin%ex n
) ;setq's P* and PE*...Barf again.
1467 (setq ans
(catch 'pin%ex
(pin%ex d
))))
1468 ;; We have an integral of the form p(x)*F(exp(x)), where
1469 ;; p(x) is a polynomial.
1472 (return (dintexp grand var
)))
1473 ((not (and (zerop1 (get-limit grand var
'$inf
))
1474 (zerop1 (get-limit grand var
'$minf
))))
1475 ;; These limits must exist for the integral to converge.
1477 ((setq ans
(rectzto%pi2
(m*l p
*) (m*l pe
*) d
))
1478 ;; This only handles the case when the F(z) is a
1479 ;; rational function.
1480 (return (m* (m// nc dc
) ans
)))
1481 ((setq ans
(log-transform (m*l p
*) (m*l pe
*) d
))
1482 ;; If we get here, F(z) is not a rational function.
1483 ;; We transform it using the substitution x=log(y)
1484 ;; which gives us an integral of the form
1485 ;; p(log(y))*F(y)/y, which maxima should be able to
1487 (return (m* (m// nc dc
) ans
)))
1489 ;; Give up. We don't know how to handle this.
1492 (cond ((setq ans
(ggrm grand
))
1494 ((and (evenfn grand var
)
1495 (setq loopstop
* (m+ 1 loopstop
*))
1496 (setq ans
(method-by-limits grand var
0 '$inf
)))
1497 (return (m*t
2. ans
)))
1500 (defun linpower0 (exp var
)
1501 (cond ((and (setq exp
(linpower exp var
))
1502 (eq (ask-integer (caddr exp
) '$even
)
1504 (ratgreaterp 0.
(car exp
)))
1507 ;;; given (b*x+a)^n+c returns (a b n c)
1508 (defun linpower (exp var
)
1509 (let (linpart deg lc c varlist
)
1510 (cond ((not (polyp exp
)) nil
)
1511 (t (let ((varlist (list var
)))
1513 (setq linpart
(cadr (ratrep* exp
)))
1514 (cond ((atom linpart
)
1516 (t (setq deg
(cadr linpart
))
1517 ;;;get high degree of poly
1518 (setq linpart
($diff exp var
(m+ deg -
1)))
1519 ;;;diff down to linear.
1520 (setq lc
(sdiff linpart var
))
1521 ;;;all the way to constant.
1522 (setq linpart
(sratsimp (m// linpart lc
)))
1523 (setq lc
(sratsimp (m// lc
`((mfactorial) ,deg
))))
1524 ;;;get rid of factorial from differentiation.
1525 (setq c
(sratsimp (m+ exp
(m* (m- lc
)
1526 (m^ linpart deg
)))))))
1527 ;;;Sees if can be expressed as (a*x+b)^n + part freeof x.
1528 (cond ((not (among var c
))
1529 `(,lc
,linpart
,deg
,c
))
1532 (defun mtorat (n d s
)
1533 (let ((*semirat
* t
))
1534 (cond ((and (null *dflag
)
1535 (setq s
(difapply n d s
#'mtorat
)))
1537 (t (csemiup n d var
)))))
1539 (defun zmtorat (n d s fn1
)
1541 (cond ((eq ($sign
(m+ s
(m+ 1 (setq nn
* (deg n
)))))
1544 ((eq ($sign
(m+ s -
4))
1547 (setq d
($factor d
))
1548 (setq c
(rmconst1 d
))
1554 (setq n
(partnum n d
))
1556 (setq n
($xthru
(m+l
1557 (mapcar #'(lambda (a b
)
1558 (m// (funcall fn1
(car a
) b
(deg b
))
1562 (return (cond (c (m// n c
))
1566 (setq n
(funcall fn1 n d s
))
1567 (return (sratsimp (cond (c (m// n c
))
1570 (defun pfrnum (f g n n2 var
)
1571 (let ((varlist (list var
)) genvar
)
1572 (setq f
(polyform f
)
1576 (setq var
(caadr (ratrep* var
)))
1577 (setq f
(resprog0 f g n n2
))
1578 (list (list (pdis (cadr f
)) (pdis (cddr f
)))
1579 (list (pdis (caar f
)) (pdis (cdar f
))))))
1584 (setq f
(ratrep* e
))
1585 (and (equal (cddr f
) 1)
1587 (and (equal (length (setq d
(cddr f
))) 3)
1590 (return (list (car d
)
1592 (ptimes (cadr f
) (caddr d
)))))
1593 (merror "defint: bug from PFRNUM in RESIDU.")))
1595 (defun partnum (n dl
)
1596 (let ((n2 1) ans nl
)
1597 (do ((dl dl
(cdr dl
)))
1599 (nconc ans
(ncons (list n n2
))))
1600 (setq nl
(pfrnum (car dl
) (m*l
(cdr dl
)) n n2 var
))
1601 (setq ans
(nconc ans
(ncons (car nl
))))
1602 (setq n2
(cadadr nl
) n
(caadr nl
) nl nil
))))
1605 (prog (poly expo
*mtoinf
* mb varlist genvar l c gvar
)
1606 (setq varlist
(list var
))
1608 (cond ((and (setq expo
(%einvolve e
))
1609 (polyp (setq poly
(sratsimp (m// e
(m^t
'$%e expo
)))))
1610 (setq l
(catch 'ggrm
(ggr (m^t
'$%e expo
) nil
))))
1612 (setq mb
(m- (subin 0.
(cadr l
))))
1613 (setq poly
(m+ (subin (m+t mb var
) poly
)
1614 (subin (m+t mb
(m*t -
1 var
)) poly
))))
1616 (setq expo
(caddr l
)
1621 (setq poly
(cdr (ratrep* poly
)))
1622 (setq mb
(m^
(pdis (cdr poly
)) -
1)
1624 (setq gvar
(caadr (ratrep* var
)))
1625 (cond ((or (atom poly
)
1626 (pointergp gvar
(car poly
)))
1627 (setq poly
(list 0. poly
)))
1628 (t (setq poly
(cdr poly
))))
1629 (return (do ((poly poly
(cddr poly
)))
1631 (mul* (m^t
'$%e c
) (m^t expo -
1) mb
(m+l e
)))
1632 (setq e
(cons (ggrm1 (car poly
) (pdis (cadr poly
)) l expo
)
1635 (defun ggrm1 (d k a b
)
1636 (setq b
(m// (m+t
1. d
) b
))
1637 (m* k
`((%gamma
) ,b
) (m^ a
(m- b
))))
1639 ;; Compute the integral(n/d,x,0,inf) by computing the negative of the
1640 ;; sum of residues of log(-x)*n/d over the poles of n/d inside the
1641 ;; keyhole contour. This contour is basically an disk with a slit
1642 ;; along the positive real axis. n/d must be a rational function.
1643 (defun keyhole (n d var
)
1644 (let* ((*semirat
* ())
1647 ;; Ok if not on the positive real axis.
1648 (or (not (equal ($imagpart j
) 0))
1649 (eq ($asksign j
) '$neg
)))
1651 (cond ((eq ($asksign j
) '$pos
)
1656 ($rectform
($multthru
(m+ (cond ((car res
)
1663 ;; Look at an expression e of the form sin(r*x)^k, where k is an
1664 ;; integer. Return the list (1 r k). (Not sure if the first element
1665 ;; of the list is always 1 because I'm not sure what partition is
1666 ;; trying to do here.)
1669 (cond ((atom e
) (return nil
)))
1670 (setq e
(partition e var
1))
1673 (cond ((setq r
(sinrx e
))
1674 (return (list m r
1)))
1676 (eq (ask-integer (setq k
(caddr e
)) '$integer
) '$yes
)
1677 (setq r
(sinrx (cadr e
))))
1678 (return (list m r k
))))))
1680 ;; Look at an expression e of the form sin(r*x) and return r.
1682 (cond ((and (consp e
) (eq (caar e
) '%sin
))
1683 (cond ((eq (cadr e
) var
)
1685 ((and (setq e
(partition (cadr e
) var
1))
1691 ;; integrate(a*sc(r*x)^k/x^n,x,0,inf).
1694 ;; Get the argument of the involved trig function.
1695 (when (null (setq arg
(involve exp
'(%sin %cos
))))
1697 ;; I don't think this needs to be special.
1699 (declare (special n
))
1700 ;; Replace (1-cos(arg)^2) with sin(arg)^2.
1701 (setq exp
($substitute
;(m^t `((%sin) ,var) 2.)
1702 ;(m+t 1. (m- (m^t `((%cos) ,var) 2.)))
1703 ;; The code from above generates expressions with
1704 ;; a missing simp flag. Furthermore, the
1705 ;; substitution has to be done for the complete
1706 ;; argument of the trig function. (DK 02/2010)
1707 `((mexpt simp
) ((%sin simp
) ,arg
) 2)
1708 `((mplus) 1 ((mtimes) -
1 ((mexpt) ((%cos
) ,arg
) 2)))
1712 (cond ((and (setq n
(findp dn
*))
1713 (eq (ask-integer n
'$integer
) '$yes
))
1714 ;; n is the power of the denominator.
1715 (cond ((setq c
(skr u
))
1717 (return (scmp c n
)))
1719 (setq c
(andmapcar #'skr
(cdr u
))))
1720 ;; Do this for a sum of such terms.
1721 (return (m+l
(mapcar #'(lambda (j) (scmp j n
))
1724 ;; We have an integral of the form sin(r*x)^k/x^n. C is the list (1 r k).
1726 ;; The substitution y=r*x converts this integral to
1728 ;; r^(n-1)*integral(sin(y)^k/y^n,y,0,inf)
1730 ;; (If r is negative, we need to negate the result.)
1732 ;; The recursion Wang gives on p. 87 has a typo. The second term
1733 ;; should be subtracted from the first. This formula is given in G&R,
1734 ;; 3.82, formula 12.
1736 ;; integrate(sin(x)^r/x^s,x) =
1737 ;; r*(r-1)/(s-1)/(s-2)*integrate(sin(x)^(r-2)/x^(s-2),x)
1738 ;; - r^2/(s-1)/(s-2)*integrate(sin(x)^r/x^(s-2),x)
1740 ;; (Limits are assumed to be 0 to inf.)
1742 ;; This recursion ends up with integrals with s = 1 or 2 and
1744 ;; integrate(sin(x)^p/x,x,0,inf) = integrate(sin(x)^(p-1),x,0,%pi/2)
1746 ;; with p > 0 and odd. This latter integral is known to maxima, and
1747 ;; it's value is beta(p/2,1/2)/2.
1749 ;; integrate(sin(x)^2/x^2,x,0,inf) = %pi/2*binomial(q-3/2,q-1)
1754 ;; Compute sign(r)*r^(n-1)*integrate(sin(y)^k/y^n,y,0,inf)
1755 (destructuring-bind (mult r k
)
1757 (let ((recursion (sinsp k n
)))
1763 ;; Recursion failed. Return the integrand
1764 ;; The following code generates expressions with a missing simp flag
1765 ;; for the sin function. Use better simplifying code. (DK 02/2010)
1766 ; (let ((integrand (div (pow `((%sin) ,(m* r var))
1769 (let ((integrand (div (power (take '(%sin
) (mul r var
))
1773 `((%integrate
) ,integrand
,var
,ll
,ul
)))))))
1775 ;; integrate(sin(x)^n/x^2,x,0,inf) = pi/2*binomial(n-3/2,n-1).
1776 ;; Express in terms of Gamma functions, though.
1778 (m* half%pi
($makegamma
`((%binomial
) ,(m+t
(m+ n -
1) '((rat) -
1 2))
1782 ;; integrate(sin(x)^n/x,x,0,inf) = beta((n+1)/2,1/2)/2, for n odd and
1787 (t (bygamma (m+ n -
1) 0.
))))
1789 ;; This implements the recursion for computing
1790 ;; integrate(sin(y)^l/y^k,y,0,inf). (Note the change in notation from
1795 (cond ((eq ($sign
(m+ l
(m- (m+ k -
1))))
1797 ;; Integral diverges if l-(k-1) < 0.
1799 ((not (even1 (m+ l k
)))
1800 ;; If l + k is not even, return NIL. (Is this the right
1804 ;; We have integrate(sin(y)^l/y^2). Use sevn to evaluate.
1807 ;; We have integrate(sin(y)^l/y,y)
1809 ((eq ($sign
(m+ k -
2.
))
1811 (setq i
(m* (m+ k -
1)
1812 (setq j
(m+ k -
2.
))))
1813 ;; j = k-2, i = (k-1)*(k-2)
1816 ;; The main recursion:
1819 ;; = l*(l-1)/(k-1)/(k-2)*i(sin(y)^(l-2)/y^k)
1820 ;; - l^2/(k-1)/(k-1)*i(sin(y)^l/y^(k-2))
1823 (sinsp (m+ l -
2.
) j
))
1828 ;; Returns the fractional part of a?
1832 ;; Why do we return 0 if a is a number? Perhaps we really
1836 ;; If we're here, this basically assumes a is a rational.
1837 ;; Compute the remainder and return the result.
1838 (list (car a
) (rem (cadr a
) (caddr a
)) (caddr a
)))
1839 ((and (atom a
) (abless1 a
)) a
)
1842 (abless1 (caddr a
)))
1846 (cond ((polyinx e var nil
) 0.
)
1852 (m+l
(mapcar #'thrad e
)))))
1855 ;;; THE FOLLOWING FUNCTION IS FOR TRIG FUNCTIONS OF THE FOLLOWING TYPE:
1856 ;;; LOWER LIMIT=0 B A MULTIPLE OF %PI SCA FUNCTION OF SIN (X) COS (X)
1859 (defun period (p e var
)
1860 (and (alike1 (no-err-sub var e
) (setq e
(no-err-sub (m+ p var
) e
)))
1861 ;; means there was no error
1864 ; returns cons of (integer_part . fractional_part) of a
1866 ;; I think we really want to compute how many full periods are in a
1867 ;; and the remainder.
1868 (let* ((q (igprt (div a
(mul 2 '$%pi
))))
1869 (r (add a
(mul -
1 (mul q
2 '$%pi
)))))
1873 ;; Return the integer part of r.
1876 (mfuncall '$floor r
))
1879 ;;;Try making exp(%i*var) --> yy, if result is rational then do integral
1880 ;;;around unit circle. Make corrections for limits of integration if possible.
1882 (let* ((exp-form (sconvert sc
)) ;Exponentialize
1883 (rat-form (maxima-substitute 'yy
(m^t
'$%e
(m*t
'$%i var
))
1884 exp-form
))) ;Try to make Rational fun.
1885 (cond ((and (ratp rat-form
'yy
)
1886 (not (among var rat-form
)))
1887 (cond ((alike1 b %pi2
)
1888 (let ((ans (zto%pi2 rat-form
'yy
)))
1892 (evenfn exp-form var
))
1893 (let ((ans (zto%pi2 rat-form
'yy
)))
1894 (cond (ans (m*t
'((rat) 1.
2.
) ans
))
1896 ((and (alike1 b half%pi
)
1897 (evenfn exp-form var
)
1899 (no-err-sub (m+t
'$%pi
(m*t -
1 var
))
1901 (let ((ans (zto%pi2 rat-form
'yy
)))
1902 (cond (ans (m*t
'((rat) 1.
4.
) ans
))
1905 ;;; Do integrals of sin and cos. this routine makes sure lower limit
1907 (defun intsc1 (a b e
)
1908 ;; integrate(e,var,a,b)
1909 (let ((trigarg (find-first-trigarg e
))
1913 (*sin-cos-recur
* t
)) ;recursion stopper
1914 (prog (ans d nzp2 l int-zero-to-d int-nzp2 int-zero-to-c limit-diff
)
1915 (let* ((arg (simple-trig-arg trigarg
)) ;; pattern match sin(cc*x + bb)
1918 (new-var (gensym "NEW-VAR-")))
1920 (not (every-trigarg-alike e trigarg
)))
1922 (when (not (and (equal cc
1) (equal bb
0)))
1923 (setq e
(div (maxima-substitute (div (sub new-var bb
) cc
)
1926 (setq var new-var
) ;; change of variables to get sin(new-var)
1927 (setq a
(add bb
(mul a cc
)))
1928 (setq b
(add bb
(mul b cc
)))))
1929 (setq limit-diff
(m+ b
(m* -
1 a
)))
1930 (when (or (not (period %pi2 e var
))
1931 (not (and ($constantp a
)
1933 ;; Exit if b or a is not a constant or if the integrand
1934 ;; doesn't appear to have a period of 2 pi.
1937 ;; Multiples of 2*%pi in limits.
1938 (cond ((integerp (setq d
(let (($float nil
))
1939 (m// limit-diff %pi2
))))
1940 (cond ((setq ans
(intsc e %pi2 var
))
1941 (return (m* d ans
)))
1944 ;; The integral is not over a full period (2*%pi) or multiple
1945 ;; of a full period.
1947 ;; Wang p. 111: The integral integrate(f(x),x,a,b) can be
1950 ;; n * integrate(f,x,0,2*%pi) + integrate(f,x,0,c)
1951 ;; - integrate(f,x,0,d)
1953 ;; for some integer n and d >= 0, c < 2*%pi because there exist
1954 ;; integers p and q such that a = 2 * p *%pi + d and b = 2 * q
1955 ;; * %pi + c. Then n = q - p.
1957 ;; Compute q and c for the upper limit b.
1962 (setq int-zero-to-d
0.
)
1964 ;; Compute p and d for the lower limit a.
1966 ;; Compute -integrate(f,x,0,d)
1968 (cond ((setq ans
(try-intsc e
(cdr l
) var
))
1971 ;; Compute n = q - p (stored in nzp2)
1972 (setq nzp2
(m+ (car b
) (m- (car l
))))
1974 ;; Compute n*integrate(f,x,0,2*%pi)
1975 (setq int-nzp2
(cond ((zerop1 nzp2
)
1978 ((setq ans
(try-intsc e %pi2 var
))
1979 ;; n is not zero, so compute
1980 ;; integrate(f,x,0,2*%pi)
1982 ;; Unable to compute integrate(f,x,0,2*%pi)
1984 ;; Compute integrate(f,x,0,c)
1985 (setq int-zero-to-c
(try-intsc e
(cdr b
) var
))
1987 (return (cond ((and int-zero-to-d int-nzp2 int-zero-to-c
)
1988 ;; All three pieces succeeded.
1989 (add* int-zero-to-d int-nzp2 int-zero-to-c
))
1990 ((ratgreaterp %pi2 limit-diff
)
1991 ;; Less than 1 full period, so intsc can integrate it.
1992 ;; Apply the substitution to make the lower limit 0.
1993 ;; This is last resort because substitution often causes intsc to fail.
1994 (intsc (maxima-substitute (m+ a var
) var e
)
1999 ;; integrate(sc, var, 0, b), where sc is f(sin(x), cos(x)).
2000 ;; calls intsc with a wrapper to just return nil if integral is divergent,
2001 ;; rather than generating an error.
2002 (defun try-intsc (sc b var
)
2003 (let* ((*nodiverg t
)
2004 (ans (catch 'divergent
(intsc sc b var
))))
2005 (if (eq ans
'divergent
)
2009 ;; integrate(sc, var, 0, b), where sc is f(sin(x), cos(x)). I (rtoy)
2010 ;; think this expects b to be less than 2*%pi.
2011 (defun intsc (sc b var
)
2014 (multiple-value-bind (b sc
)
2015 (cond ((eq ($sign b
) '$neg
)
2017 (m* -
1 (subin (m*t -
1 var
) sc
))))
2020 ;; Partition the integrand SC into the factors that do not
2021 ;; contain VAR (the car part) and the parts that do (the cdr
2023 (setq sc
(partition sc var
1))
2024 (cond ((setq b
(intsc0 (cdr sc
) b var
))
2025 (m* (resimplify (car sc
)) b
))))))
2027 ;; integrate(sc, var, 0, b), where sc is f(sin(x), cos(x)).
2028 (defun intsc0 (sc b var
)
2029 ;; Determine if sc is a product of sin's and cos's.
2030 (let ((nn* (scprod sc
))
2033 ;; We have a product of sin's and cos's. We handle some
2034 ;; special cases here.
2035 (cond ((alike1 b half%pi
)
2036 ;; Wang p. 110, formula (1):
2037 ;; integrate(sin(x)^m*cos(x)^n, x, 0, %pi/2) =
2038 ;; gamma((m+1)/2)*gamma((n+1)/2)/2/gamma((n+m+2)/2)
2039 (bygamma (car nn
*) (cadr nn
*)))
2041 ;; Wang p. 110, near the bottom, says
2043 ;; int(f(sin(x),cos(x)), x, 0, %pi) =
2044 ;; int(f(sin(x),cos(x)) + f(sin(x),-cos(x)),x,0,%pi/2)
2045 (cond ((eq (real-branch (cadr nn
*) -
1) '$yes
)
2046 (m* (m+ 1.
(m^ -
1 (cadr nn
*)))
2047 (bygamma (car nn
*) (cadr nn
*))))))
2049 (cond ((or (and (eq (ask-integer (car nn
*) '$even
)
2051 (eq (ask-integer (cadr nn
*) '$even
)
2053 (and (ratnump (car nn
*))
2054 (eq (real-branch (car nn
*) -
1)
2056 (ratnump (cadr nn
*))
2057 (eq (real-branch (cadr nn
*) -
1)
2059 (m* 4.
(bygamma (car nn
*) (cadr nn
*))))
2060 ((or (eq (ask-integer (car nn
*) '$odd
) '$yes
)
2061 (eq (ask-integer (cadr nn
*) '$odd
) '$yes
))
2064 ((alike1 b half%pi3
)
2065 ;; Wang, p. 111 says
2067 ;; int(f(sin(x),cos(x)),x,0,3*%pi/2) =
2068 ;; int(f(sin(x),cos(x)),x,0,%pi)
2069 ;; + int(f(-sin(x),-cos(x)),x,0,%pi/2)
2070 (m* (m+ 1.
(m^ -
1 (cadr nn
*)) (m^ -
1 (m+l nn
*)))
2071 (bygamma (car nn
*) (cadr nn
*))))))
2073 ;; We don't have a product of sin's and cos's.
2074 (cond ((and (or (eq b
'$%pi
)
2077 (setq dn
* (scrat sc b
)))
2079 ((setq nn
* (antideriv sc
))
2080 (sin-cos-intsubs nn
* var
0. b
))
2083 ;;;Is careful about substitution of limits where the denominator may be zero
2084 ;;;because of various assumptions made.
2085 (defun sin-cos-intsubs (exp var ll ul
)
2087 (let ((l (mapcar #'sin-cos-intsubs1
(cdr exp
))))
2088 (if (not (some #'null l
))
2090 (t (sin-cos-intsubs1 exp
))))
2092 (defun sin-cos-intsubs1 (exp)
2093 (let* ((rat-exp ($rat exp
))
2094 (denom (pdis (cddr rat-exp
))))
2095 (cond ((equal ($csign denom
) '$zero
)
2097 (t (try-intsubs exp ll ul
)))))
2099 (defun try-intsubs (exp ll ul
)
2100 (let* ((*nodiverg t
)
2101 (ans (catch 'divergent
(intsubs exp ll ul
))))
2102 (if (eq ans
'divergent
)
2106 (defun try-defint (exp var ll ul
)
2107 (let* ((*nodiverg t
)
2108 (ans (catch 'divergent
(defint exp var ll ul
))))
2109 (if (eq ans
'divergent
)
2113 ;; Determine whether E is of the form sin(x)^m*cos(x)^n and return the
2116 (let ((great-minus-1 #'(lambda (temp)
2117 (ratgreaterp temp -
1)))
2120 ((setq m
(powerofx e
`((%sin
) ,var
) great-minus-1 var
))
2122 ((setq n
(powerofx e
`((%cos
) ,var
) great-minus-1 var
))
2126 (or (setq m
(powerofx (cadr e
) `((%sin
) ,var
) great-minus-1 var
))
2127 (setq n
(powerofx (cadr e
) `((%cos
) ,var
) great-minus-1 var
)))
2130 (setq m
(powerofx (caddr e
) `((%sin
) ,var
) great-minus-1 var
)))
2131 (t (setq n
(powerofx (caddr e
) `((%cos
) ,var
) great-minus-1 var
))))
2136 (defun real-branch (exponent value
)
2137 ;; Says wether (m^t value exponent) has at least one real branch.
2138 ;; Only works for values of 1 and -1 now. Returns $yes $no
2140 (cond ((equal value
1.
)
2142 ((eq (ask-integer exponent
'$integer
) '$yes
)
2145 (cond ((eq ($oddp
(caddr exponent
)) t
)
2150 ;; Compute beta((m+1)/2,(n+1)/2)/2.
2151 (defun bygamma (m n
)
2152 (let ((one-half (m//t
1.
2.
)))
2153 (m* one-half
`(($beta
) ,(m* one-half
(m+t
1. m
))
2154 ,(m* one-half
(m+t
1. n
))))))
2156 ;;Seems like Guys who call this don't agree on what it should return.
2157 (defun powerofx (e x p var
)
2158 (setq e
(cond ((not (among var e
)) nil
)
2163 (not (among var
(caddr e
))))
2165 (cond ((null e
) nil
)
2169 ;; Check e for an expression of the form x^kk*(b*x^n+a)^l. If it
2170 ;; matches, Return the two values kk and (list l a n b).
2173 (cond ((atom e
) nil
)
2175 ;; We have f(x)^y. Look to see if f(x) has the desired
2176 ;; form. Then f(x)^y has the desired form too, with
2177 ;; suitably modified values.
2179 ;; XXX: Should we ask for the sign of f(x) if y is not an
2180 ;; integer? This transformation we're going to do requires
2181 ;; that f(x)^y be real.
2182 (destructuring-bind (mexp base power
)
2184 (declare (ignore mexp
))
2185 (multiple-value-bind (kk cc
)
2188 ;; Got a match. Adjust kk and cc appropriately.
2189 (destructuring-bind (l a n b
)
2191 (values (mul kk power
)
2192 (list (mul l power
) a n b
)))))))
2195 (or (and (setq k
(findp (cadr e
)))
2196 (setq c
(bxm (caddr e
) (polyinx (caddr e
) var nil
))))
2197 (and (setq k
(findp (caddr e
)))
2198 (setq c
(bxm (cadr e
) (polyinx (cadr e
) var nil
))))))
2200 ((setq c
(bxm e
(polyinx e var nil
)))
2207 ;; (COND ((NOT (BATA0 E)) (RETURN NIL))
2208 ;; ((AND (EQUAL -1. (CADDDR C))
2209 ;; (EQ ($askSIGN (SETQ K (m+ 1. K)))
2211 ;; (EQ ($askSIGN (SETQ L (m+ 1. (CAR C))))
2214 ;; (m^ UL (CADDR C)))
2215 ;; (SETQ E (CADR C))
2216 ;; (EQ ($askSIGN (SETQ C (CADDR C))) '$pos))
2217 ;; (RETURN (M// (m* (m^ UL (m+t K (m* C (m+t -1. L))))
2218 ;; `(($BETA) ,(SETQ NN* (M// K C))
2223 ;; Integrals of the form i(log(x)^m*x^k*(a+b*x^n)^l,x,0,ul). There
2224 ;; are two cases to consider: One case has ul>0, b<0, a=-b*ul^n, k>-1,
2225 ;; l>-1, n>0, m a nonnegative integer. The second case has ul=inf, l < 0.
2227 ;; These integrals are essentially partial derivatives of the Beta
2228 ;; function (i.e. the Eulerian integral of the first kind). Note
2229 ;; that, currently, with the default setting intanalysis:true, this
2230 ;; function might not even be called for some of these integrals.
2231 ;; However, this can be palliated by setting intanalysis:false.
2234 (when (or (mtimesp e
) (mexptp e
))
2236 (log (list '(%log
) var
)))
2239 (find-if #'(lambda (fac)
2240 (powerofx fac log
#'set-m var
))
2242 (when (and (freeof var m
)
2243 (eq (ask-integer m
'$integer
) '$yes
)
2244 (not (eq ($asksign m
) '$neg
)))
2245 (setq e
(m//t e
(list '(mexpt) log m
)))
2248 (multiple-value-bind (kk s d r cc
)
2250 ;; We have i(x^kk/(d+cc*x^r)^s,x,0,inf) =
2251 ;; beta(aa,bb)/(cc^aa*d^bb*r). Compute this, and then
2252 ;; differentiate it m times to get the log term
2255 (let* ((aa (div (add 1 var
) r
))
2257 (m (if (eq ($asksign m
) '$zero
)
2260 (let ((res (div `(($beta
) ,aa
,bb
)
2264 ($at
($diff res var m
)
2265 (list '(mequal) var kk
)))))))
2267 (multiple-value-bind
2268 (k/n l n b
) (batap-new e
)
2270 (let ((beta (simplify (list '($beta
) k
/n l
)))
2271 (m (if (eq ($asksign m
) '$zero
) 0 m
)))
2272 ;; The result looks like B(k/n,l) ( ... ).
2273 ;; Perhaps, we should just $factor, instead of
2274 ;; pulling out beta like this.
2280 (m^t
(m-t b
) (m1-t l
))
2281 (m^t ul
(m*t n
(m1-t l
)))
2282 (m^t n
(m-t (m1+t m
)))
2283 ($at
($diff
(m*t
(m^t ul
(m*t n var
))
2284 (list '($beta
) var l
))
2286 (list '(mequal) var k
/n
)))
2290 ;;; If e is of the form given below, make the obvious change
2291 ;;; of variables (substituting ul*x^(1/n) for x) in order to reduce
2292 ;;; e to the usual form of the integrand in the Eulerian
2293 ;;; integral of the first kind.
2294 ;;; N. B: The old version of ZTO1 completely ignored this
2295 ;;; substitution; the log(x)s were just thrown in, which,
2296 ;;; of course would give wrong results.
2298 (defun batap-new (e)
2300 (multiple-value-bind (k c
)
2303 ;; e=x^k*(a+b*x^n)^l
2304 (destructuring-bind (l a n b
)
2306 (when (and (freeof var k
)
2309 (alike1 a
(m-t (m*t b
(m^t ul n
))))
2310 (eq ($asksign b
) '$neg
)
2311 (eq ($asksign
(setq k
(m1+t k
))) '$pos
)
2312 (eq ($asksign
(setq l
(m1+t l
))) '$pos
)
2313 (eq ($asksign n
) '$pos
))
2314 (values (m//t k n
) l n b
))))))
2317 ;; Wang p. 71 gives the following formula for a beta function:
2319 ;; integrate(x^(k-1)/(c*x^r+d)^s,x,0,inf)
2320 ;; = beta(a,b)/(c^a*d^b*r)
2322 ;; where a = k/r > 0, b = s - a > 0, s > k > 0, r > 0, c*d > 0.
2324 ;; This function matches this and returns k-1, d, r, c, a, b. And
2325 ;; also checks that all the conditions hold. If not, NIL is returned.
2327 (defun batap-inf (e)
2328 (multiple-value-bind (k c
)
2331 (destructuring-bind (l d r cc
)
2333 (let* ((s (mul -
1 l
))
2337 (when (and (freeof var k
)
2340 (eq ($asksign kk
) '$pos
)
2341 (eq ($asksign a
) '$pos
)
2342 (eq ($asksign b
) '$pos
)
2343 (eq ($asksign
(sub s k
)) '$pos
)
2344 (eq ($asksign r
) '$pos
)
2345 (eq ($asksign
(mul cc d
)) '$pos
))
2346 (values k s d r cc
)))))))
2349 ;; Handles beta integrals.
2351 (cond ((not (or (equal ll
0)
2353 (setq e
(subin (m+ ll var
) e
))))
2354 (multiple-value-bind (k c
)
2359 (destructuring-bind (l d al c
)
2361 ;; e = x^k*(d+c*x^al)^l.
2362 (let ((new-k (m// (m+ 1 k
) al
)))
2363 (when (and (ratgreaterp al
0.
)
2364 (eq ($asksign new-k
) '$pos
)
2365 (ratgreaterp (setq l
(m* -
1 l
))
2367 (eq ($asksign
(m* d c
))
2369 (setq l
(m+ l
(m*t -
1 new-k
)))
2370 (m// `(($beta
) ,new-k
,l
)
2371 (mul* al
(m^ c new-k
) (m^ d l
))))))))))
2374 ;; Compute exp(d)*gamma((c+1)/b)/b/a^((c+1)/b). In essence, this is
2375 ;; the value of integrate(x^c*exp(d-a*x^b),x,0,inf).
2376 (defun gamma1 (c a b d
)
2378 (m^
(m* b
(m^ a
(setq c
(m// (m+t c
1) b
)))) -
1)
2381 (defun zto%pi2
(grand var
)
2382 (let ((result (unitcir (sratsimp (m// grand var
)) var
)))
2383 (cond (result (sratsimp (m* (m- '$%i
) result
)))
2386 ;; Evaluates the contour integral of GRAND around the unit circle
2388 (defun unitcir (grand var
)
2391 (result (princip (res nn
* dn
*
2393 ;; Is pt stricly inside the unit circle?
2394 (setq sgn
(let ((limitp nil
))
2395 ($asksign
(m+ -
1 (cabs pt
)))))
2398 (declare (ignore pt
))
2399 ;; Is pt on the unit circle? (Use
2400 ;; the cached value computed
2404 (setq sgn nil
)))))))
2406 (m* '$%pi result
))))
2409 (defun logx1 (exp ll ul
)
2412 ((and (notinvolve exp
'(%sin %cos %tan %atan %asin %acos
))
2413 (setq arg
(involve exp
'(%log
))))
2415 (cond ((ratgreaterp 1. ll
)
2416 (cond ((not (eq ul
'$inf
))
2417 (intcv1 (m^t
'$%e
(m- 'yx
)) (m- `((%log
) ,var
))))
2418 (t (intcv1 (m^t
'$%e
'yx
) `((%log
) ,var
)))))))
2419 (t (intcv arg nil
)))))))
2422 ;; Wang 81-83. Unfortunately, the pdf version has page 82 as all
2423 ;; black, so here is, as best as I can tell, what Wang is doing.
2424 ;; Fortunately, p. 81 has the necessary hints.
2426 ;; First consider integrate(exp(%i*k*x^n),x) around the closed contour
2427 ;; consisting of the real axis from 0 to R, the arc from the angle 0
2428 ;; to %pi/(2*n) and the ray from the arc back to the origin.
2430 ;; There are no poles in this region, so the integral must be zero.
2431 ;; But consider the integral on the three parts. The real axis is the
2432 ;; integral we want. The return ray is
2434 ;; exp(%i*%pi/2/n) * integrate(exp(%i*k*(t*exp(%i*%pi/2/n))^n),t,R,0)
2435 ;; = exp(%i*%pi/2/n) * integrate(exp(%i*k*t^n*exp(%i*%pi/2)),t,R,0)
2436 ;; = -exp(%i*%pi/2/n) * integrate(exp(-k*t^n),t,0,R)
2438 ;; As R -> infinity, this last integral is gamma(1/n)/k^(1/n)/n.
2440 ;; We assume the integral on the circular arc approaches 0 as R ->
2441 ;; infinity. (Need to prove this.)
2445 ;; integrate(exp(%i*k*t^n),t,0,inf)
2446 ;; = exp(%i*%pi/2/n) * gamma(1/n)/k^(1/n)/n.
2448 ;; Equating real and imaginary parts gives us the desired results:
2450 ;; integrate(cos(k*t^n),t,0,inf) = G * cos(%pi/2/n)
2451 ;; integrate(sin(k*t^n),t,0,inf) = G * sin(%pi/2/n)
2453 ;; where G = gamma(1/n)/k^(1/n)/n.
2457 (cond ((atom e
) nil
)
2458 ((and (or (eq (caar e
) '%sin
)
2459 (eq (caar e
) '%cos
))
2461 (setq e
(bx**n
(cadr e
))))
2462 ;; Ok, we have cos(b*x^n) or sin(b*x^n), and we set e = (n
2464 (cond ((equal (car e
) 1.
)
2465 ;; n = 1. Give up. (Why not divergent?)
2467 ((zerop (setq s
(let ((sign ($asksign
(cadr e
))))
2468 (cond ((eq sign
'$pos
) 1)
2469 ((eq sign
'$neg
) -
1)
2470 ((eq sign
'$zero
) 0)))))
2471 ;; s is the sign of b. Give up if it's zero.
2473 ((not (eq ($asksign
(m+ -
1 (car e
))) '$pos
))
2474 ;; Give up if n-1 <= 0. (Why give up? Isn't the
2475 ;; integral divergent?)
2478 ;; We can apply our formula now. g = gamma(1/n)/n/b^(1/n)
2479 (setq g
(gamma1 0.
(m* s
(cadr e
)) (car e
) 0.
))
2480 (setq e
(m* g
`((,ind
) ,(m// half%pi
(car e
)))))
2481 (m* (cond ((and (eq ind
'%sin
)
2488 ;; this is the second part of the definite integral package
2490 (declare-top(special var plm
* pl
* rl
* pl
*1 rl
*1))
2492 (defun p*lognxp
(a s
)
2494 (cond ((not (among '%log a
))
2496 ((and (polyinx (setq b
(maxima-substitute 1.
`((%log
) ,var
) a
))
2498 (eq ($sign
(m+ s
(m+ 1 (deg b
))))
2501 (setq a
(lognxp (sratsimp (m// a b
)))))
2505 (cond ((atom a
) nil
)
2506 ((and (eq (caar a
) '%log
)
2507 (eq (cadr a
) var
)) 1.
)
2513 (defun logcpi0 (n d
)
2515 (setq pl
(polelist d
#'upperhalf
#'(lambda (j)
2516 (cond ((zerop1 j
) nil
)
2517 ((equal ($imagpart j
) 0)
2521 (setq factors
(car pl
)
2523 (cond ((or (cadr pl
)
2525 (setq dp
(sdiff d var
))))
2526 (cond ((setq plm
* (car pl
))
2527 (setq rlm
* (residue n
(cond (leadcoef factors
)
2530 (cond ((setq pl
* (cadr pl
))
2531 (setq rl
* (res1 n dp pl
*))))
2532 (cond ((setq pl
*1 (caddr pl
))
2533 (setq rl
*1 (res1 n dp pl
*1))))
2534 (return (m*t
(m//t
1.
2.
)
2537 (list (cond ((setq nn
* (append rl
* rlm
*))
2539 (cond (rl*1 (m+l rl
*1))))))))))
2541 (defun lognx2 (nn dn pl rl
)
2542 (do ((pl pl
(cdr pl
))
2547 (setq ans
(cons (m* dn
(car rl
) (m^
`((%plog
) ,(car pl
)) nn
))
2550 (defun logcpj (n d i
)
2553 (list (mul* (m*t
'$%i %pi2
)
2555 (residue (m* (m^
`((%plog
) ,var
) i
) n
)
2558 (lognx2 i
(m*t
'$%i %pi2
) pl
* rl
*)
2559 (lognx2 i %p%i pl
*1 rl
*1)))
2562 (simplify (m+l n
))))
2564 ;; Handle integral(n(x)/d(x)*log(x)^m,x,0,inf). n and d are
2566 (defun log*rat
(n d m
)
2567 (declare (special *i
* *j
*))
2568 (setq *i
* (make-array (1+ m
)))
2569 (setq *j
* (make-array (1+ m
)))
2570 (setf (aref *j
* 0) 0)
2571 (prog (leadcoef factors plm
* pl
* rl
* pl
*1 rl
*1 rlm
*)
2572 (dotimes (c m
(return (logcpi n d m
)))
2573 (setf (aref *i
* c
) (logcpi n d c
))
2574 (setf (aref *j
* c
) (logcpj n factors c
)))))
2576 (defun logcpi (n d c
)
2577 (declare (special *j
*))
2580 (m* '((rat) 1 2) (m+ (aref *j
* c
) (m* -
1 (sumi c
))))))
2583 (declare (special *i
*))
2588 (push (mul* ($makegamma
`((%binomial
) ,c
,k
))
2594 (defun fan (p m a n b
)
2595 (let ((povern (m// p n
))
2598 ((or (eq (ask-integer povern
'$integer
) '$yes
)
2599 (not (equal ($imagpart ab
) 0))) ())
2600 (t (let ((ind ($asksign ab
)))
2601 (cond ((eq ind
'$zero
) nil
)
2602 ((eq ind
'$neg
) nil
)
2603 ((not (ratgreaterp m povern
)) nil
)
2605 ($makegamma
`((%binomial
) ,(m+ -
1 m
(m- povern
))
2607 `((mabs) ,(m^ a
(m+ povern
(m- m
)))))
2610 `((%sin
) ,(m*t
'$%pi povern
)))))))))))
2613 ;;Makes a new poly such that np(x)-np(x+2*%i*%pi)=p(x).
2614 ;;Constructs general POLY of degree one higher than P with
2615 ;;arbitrary coeff. and then solves for coeffs by equating like powers
2616 ;;of the varibale of integration.
2617 ;;Can probably be made simpler now.
2620 (let ((n (deg p
)) (ans ()) (varlist ()) (gp ()) (cl ()) (zz ()))
2621 (setq ans
(genpoly (m+ 1 n
))) ;Make poly with gensyms of 1 higher deg.
2622 (setq cl
(cdr ans
)) ;Coefficient list
2623 (setq varlist
(append cl
(list var
))) ;Make VAR most important.
2624 (setq gp
(car ans
)) ;This is the poly with gensym coeffs.
2625 ;;;Now, poly(x)-poly(x+2*%i*%pi)=p(x), P is the original poly.
2626 (setq ans
(m+ gp
(subin (m+t
(m*t
'$%i %pi2
) var
) (m- gp
)) (m- p
)))
2628 (setq ans
(ratrep* ans
)) ;Rational rep with VAR leading.
2629 (setq zz
(coefsolve n cl
(cond ((not (eq (caadr ans
) ;What is Lead Var.
2630 (genfind (car ans
) var
)))
2631 (list 0 (cadr ans
))) ;No VAR in ans.
2632 ((cdadr ans
))))) ;The real Poly.
2633 (if (or (null zz
) (null gp
))
2635 ($substitute zz gp
)))) ;Substitute Values for gensyms.
2637 (defun coefsolve (n cl e
)
2639 (eql (ncons (pdis (ptterm e n
))) (cons (pdis (ptterm e m
)) eql
))
2640 (m (m+ n -
1) (m+ m -
1)))
2641 ((signp l m
) (solvex eql cl nil nil
))))
2643 ;; Integrate(p(x)*f(exp(x))/g(exp(x)),x,minf,inf) by applying the
2644 ;; transformation y = exp(x) to get
2645 ;; integrate(p(log(y))*f(y)/g(y)/y,y,0,inf). This should be handled
2647 (defun log-transform (p pe d
)
2648 (let ((new-p (subst (list '(%log
) var
) var p
))
2649 (new-pe (subst var
'z
* (catch 'pin%ex
(pin%ex pe
))))
2650 (new-d (subst var
'z
* (catch 'pin%ex
(pin%ex d
)))))
2651 (defint (div (div (mul new-p new-pe
) new-d
) var
) var
0 ul
)))
2653 ;; This implements Wang's algorithm in Chapter 5.2, pp. 98-100.
2655 ;; This is a very brief description of the algorithm. Basically, we
2656 ;; have integrate(R(exp(x))*p(x),x,minf,inf), where R(x) is a rational
2657 ;; function and p(x) is a polynomial.
2659 ;; We find a polynomial q(x) such that q(x) - q(x+2*%i*%pi) = p(x).
2660 ;; Then consider a contour integral of R(exp(z))*q(z) over a
2661 ;; rectangular contour. Opposite corners of the rectangle are (-R,
2662 ;; 2*%i*%pi) and (R, 0).
2664 ;; Wang shows that this contour integral, in the limit, is the
2665 ;; integral of R(exp(x))*q(x)-R(exp(x))*q(x+2*%i*%pi), which is
2666 ;; exactly the integral we're looking for.
2668 ;; Thus, to find the value of the contour integral, we just need the
2669 ;; residues of R(exp(z))*q(z). The only tricky part is that we want
2670 ;; the log function to have an imaginary part between 0 and 2*%pi
2671 ;; instead of -%pi to %pi.
2672 (defun rectzto%pi2
(p pe d
)
2673 ;; We have R(exp(x))*p(x) represented as p(x)*pe(exp(x))/d(exp(x)).
2674 (prog (dp n pl a b c denom-exponential
)
2675 (if (not (and (setq denom-exponential
(catch 'pin%ex
(pin%ex d
)))
2676 (%e-integer-coeff pe
)
2677 (%e-integer-coeff d
)))
2679 ;; At this point denom-exponential has converted d(exp(x)) to the
2680 ;; polynomial d(z), where z = exp(x).
2681 (setq n
(m* (cond ((null p
) -
1)
2682 (t ($expand
(m*t
'$%i %pi2
(makpoly p
)))))
2686 ;; Find the poles of the denominator. denom-exponential is the
2687 ;; denominator of R(x).
2689 ;; It seems as if polelist returns a list of several items.
2690 ;; The first element is a list consisting of the pole and (z -
2691 ;; pole). We don't care about this, so we take the rest of the
2693 (setq pl
(cdr (polelist denom-exponential
2695 ;; The imaginary part is nonzero,
2696 ;; or the realpart is negative.
2697 (or (not (equal ($imagpart j
) 0))
2698 (eq ($asksign
($realpart j
)) '$neg
)))
2700 ;; The realpart is not zero.
2701 (not (eq ($asksign
($realpart j
)) '$zero
)))))))
2702 ;; Not sure what this does.
2704 ;; No roots at all, so return
2708 ;; We have simple roots or roots in REGION1
2709 (setq dp
(sdiff d var
))))
2711 ;; The cadr of pl is the list of the simple poles of
2712 ;; denom-exponential. Take the log of them to find the
2713 ;; poles of the original expression. Then compute the
2714 ;; residues at each of these poles and sum them up and put
2715 ;; the result in B. (If no simple poles set B to 0.)
2716 (setq b
(mapcar #'log-imag-0-2%pi
(cadr pl
)))
2717 (setq b
(res1 n dp b
))
2721 ;; I think this handles the case of poles outside the
2722 ;; regions. The sum of these residues are placed in C.
2723 (let ((temp (mapcar #'log-imag-0-2%pi
(caddr pl
))))
2724 (setq c
(append temp
(mapcar #'(lambda (j)
2725 (m+ (m*t
'$%i %pi2
) j
))
2727 (setq c
(res1 n dp c
))
2731 ;; We have the repeated poles of deonom-exponential, so we
2732 ;; need to convert them to the actual pole values for
2733 ;; R(exp(x)), by taking the log of the value of poles.
2734 (let ((poles (mapcar #'(lambda (p)
2735 (log-imag-0-2%pi
(car p
)))
2737 (exp (m// n
(subst (m^t
'$%e var
) 'z
* denom-exponential
))))
2738 ;; Compute the residues at all of these poles and sum
2740 (setq a
(mapcar #'(lambda (j)
2741 ($residue exp var j
))
2745 (return (sratsimp (m+ a b
(m* '((rat) 1.
2.
) c
))))))
2748 (do ((i i
(m+ i -
1))
2749 (c (gensym) (gensym))
2753 (cons (m+l ans
) cl
))
2754 (setq ans
(cons (m* c
(m^t var i
)) ans
))
2755 (setq cl
(cons c cl
))))
2757 ;; Check to see if each term in exp that is of the form exp(k*x) has
2758 ;; an integer value for k.
2759 (defun %e-integer-coeff
(exp)
2760 (cond ((mapatom exp
) t
)
2762 (eq (cadr exp
) '$%e
))
2763 (eq (ask-integer ($coeff
(caddr exp
) var
) '$integer
)
2765 (t (every '%e-integer-coeff
(cdr exp
)))))
2767 (defun wlinearpoly (e var
)
2768 (cond ((and (setq e
(polyinx e var t
))
2772 ;; Test to see if exp is of the form f(exp(x)), and if so, replace
2773 ;; exp(x) with 'z*. That is, basically return f(z*).
2775 (declare (special $exponentialize
))
2776 (pin%ex0
(cond ((notinvolve exp
'(%sinh %cosh %tanh
))
2779 (let (($exponentialize t
))
2780 (setq exp
($expand exp
)))))))
2783 ;; Does e really need to be special here? Seems to be ok without
2784 ;; it; testsuite works.
2786 (declare (special e
))
2787 (cond ((not (among var e
))
2790 (throw 'pin%ex nil
))
2793 (cond ((eq (caddr e
) var
)
2795 ((let ((linterm (wlinearpoly (caddr e
) var
)))
2797 (m* (subin 0 e
) (m^t
'z
* linterm
)))))
2799 (throw 'pin%ex nil
))))
2801 (m*l
(mapcar #'pin%ex0
(cdr e
))))
2803 (m+l
(mapcar #'pin%ex0
(cdr e
))))
2805 (throw 'pin%ex nil
))))
2807 ;; Test to see if exp is of the form p(x)*f(exp(x)). If so, set p* to
2808 ;; be p(x) and set pe* to f(exp(x)).
2809 (defun p*pin%ex
(nd*)
2810 (setq nd
* ($factor nd
*))
2811 (cond ((polyinx nd
* var nil
)
2812 (setq p
* (cons nd
* p
*)) t
)
2813 ((catch 'pin%ex
(pin%ex nd
*))
2814 (setq pe
* (cons nd
* pe
*)) t
)
2816 (andmapcar #'p
*pin%ex
(cdr nd
*)))))
2819 (cond ((findp p
) nil
)
2820 ((setq nd
* (bx**n p
))
2821 (m^t var
(car nd
*)))
2822 ((setq p
(bx**n
+a p
))
2823 (m* (caddr p
) (m^t var
(cadr p
))))))
2825 ;; I think this is looking at f(exp(x)) and tries to find some
2826 ;; rational function R and some number k such that f(exp(x)) =
2828 (defun funclogor%e
(e)
2829 (prog (ans arg nvar r
)
2830 (cond ((or (ratp e var
)
2831 (involve e
'(%sin %cos %tan
))
2832 (not (setq arg
(xor (and (setq arg
(involve e
'(%log
)))
2836 ag
(setq nvar
(cond ((eq r
'%log
) `((%log
) ,arg
))
2837 (t (m^t
'$%e arg
))))
2838 (setq ans
(maxima-substitute (m^t
'yx -
1) (m^t nvar -
1) (maxima-substitute 'yx nvar e
)))
2839 (cond ((not (among var ans
)) (return (list (subst var
'yx ans
) nvar
)))
2841 (setq arg
(findsub arg
)))
2844 ;; Integration by parts.
2846 ;; integrate(u(x)*diff(v(x),x),x,a,b)
2848 ;; = u(x)*v(x)| - integrate(v(x)*diff(u(x),x))
2851 (defun dintbypart (u v a b
)
2852 ;;;SINCE ONLY CALLED FROM DINTLOG TO get RID OF LOGS - IF LOG REMAINS, QUIT
2853 (let ((ad (antideriv v
)))
2854 (cond ((or (null ad
)
2855 (involve ad
'(%log
)))
2857 (t (let ((p1 (m* u ad
))
2858 (p2 (m* ad
(sdiff u var
))))
2859 (let ((p1-part1 (get-limit p1 var b
'$minus
))
2860 (p1-part2 (get-limit p1 var a
'$plus
)))
2861 (cond ((or (null p1-part1
)
2864 (t (let ((p2 (let ((*def2
* t
))
2865 (defint p2 var a b
))))
2866 (cond (p2 (add* p1-part1
2871 ;; integrate(f(exp(k*x)),x,a,b), where f(z) is rational.
2873 ;; See Wang p. 96-97.
2875 ;; If the limits are minf to inf, we use the substitution y=exp(k*x)
2876 ;; to get integrate(f(y)/y,y,0,inf)/k. If the limits are 0 to inf,
2877 ;; use the substitution s+1=exp(k*x) to get
2878 ;; integrate(f(s+1)/(s+1),s,0,inf).
2879 (defun dintexp (exp ignored
&aux ans
)
2880 (declare (ignore ignored
))
2881 (let ((*dintexp-recur
* t
)) ;recursion stopper
2882 (cond ((and (sinintp exp var
) ;To be moved higher in the code.
2883 (setq ans
(antideriv exp
))
2884 (setq ans
(intsubs ans ll ul
)))
2885 ;; If we can integrate it directly, do so and take the
2886 ;; appropriate limits.
2888 ((setq ans
(funclogor%e exp
))
2889 ;; ans is the list (f(x) exp(k*x)).
2890 (cond ((and (equal ll
0.
)
2892 ;; Use the substitution s + 1 = exp(k*x). The
2893 ;; integral becomes integrate(f(s+1)/(s+1),s,0,inf)
2894 (setq ans
(m+t -
1 (cadr ans
))))
2896 ;; Use the substitution y=exp(k*x) because the
2897 ;; limits are minf to inf.
2898 (setq ans
(cadr ans
))))
2899 ;; Apply the substitution and integrate it.
2902 ;; integrate(log(g(x))*f(x),x,0,inf)
2903 (defun dintlog (exp arg
)
2904 (let ((*dintlog-recur
* (1+ *dintlog-recur
*))) ;recursion stopper
2906 (cond ((and (eq ul
'$inf
)
2909 (equal 1 (sratsimp (m// exp
(m* (m- (subin (m^t var -
1)
2912 ;; Make the substitution y=1/x. If the integrand has
2913 ;; exactly the same form, the answer has to be 0.
2915 ((and (setq ans
(let (($gamma_expand t
)) (logx1 exp ll ul
)))
2918 ((setq ans
(antideriv exp
))
2919 ;; It's easy if we have the antiderivative.
2920 ;; but intsubs sometimes gives results containing %limit
2921 (return (intsubs ans ll ul
))))
2922 ;; Ok, the easy cases didn't work. We now try integration by
2923 ;; parts. Set ANS to f(x).
2924 (setq ans
(m// exp
`((%log
) ,arg
)))
2925 (cond ((involve ans
'(%log
))
2926 ;; Bad. f(x) contains a log term, so we give up.
2929 (equal 0.
(no-err-sub 0. ans
))
2930 (setq d
(let ((*def2
* t
))
2931 (defint (m* ans
(m^t var
'*z
*))
2933 ;; The arg of the log function is the same as the
2934 ;; integration variable. We can do something a little
2935 ;; simpler than integration by parts. We have something
2936 ;; like f(x)*log(x). Consider f(x)*x^z. If we
2937 ;; differentiate this wrt to z, the integrand becomes
2938 ;; f(x)*log(x)*x^z. When we evaluate this at z = 0, we
2939 ;; get the desired integrand.
2941 ;; So we need f(0) to be 0 at 0. If we can integrate
2942 ;; f(x)*x^z, then we differentiate the result and
2943 ;; evaluate it at z = 0.
2944 (return (derivat '*z
* 1. d
0.
)))
2945 ((setq ans
(dintbypart `((%log
) ,arg
) ans ll ul
))
2946 ;; Try integration by parts.
2949 ;; Compute diff(e,var,n) at the point pt.
2950 (defun derivat (var n e pt
)
2951 (subin pt
(apply '$diff
(list e var n
))))
2955 ;; MAYBPC returns (COEF EXPO CONST)
2957 ;; This basically picks off b*x^n+a and returns the list
2958 ;; (b n a). It may also set the global *zd*.
2959 (defun maybpc (e var
)
2960 (declare (special *zd
*))
2961 (cond (*mtoinf
* (throw 'ggrm
(linpower0 e var
)))
2962 ((and (not *mtoinf
*)
2963 (null (setq e
(bx**n
+a e
)))) ;bx**n+a --> (a n b) or nil.
2964 nil
) ;with var being x.
2965 ;; At this point, e is of the form (a n b)
2966 ((and (among '$%i
(caddr e
))
2967 (zerop1 ($realpart
(caddr e
)))
2968 (setq zn
($imagpart
(caddr e
)))
2969 (eq ($asksign
(cadr e
)) '$pos
))
2970 ;; If we're here, b is complex, and n > 0. zn = imagpart(b).
2972 ;; Set var to the same sign as zn.
2973 (cond ((eq ($asksign zn
) '$neg
)
2977 ;; zd = exp(var*%i*%pi*(1+nd)/(2*n). (ZD is special!)
2978 (setq *zd
* (m^t
'$%e
(m// (mul* var
'$%i
'$%pi
(m+t
1 nd
*))
2981 `(,(caddr e
) ,(cadr e
) ,(car e
)))
2982 ((and (or (eq (setq var
($asksign
($realpart
(caddr e
)))) '$neg
)
2984 (equal ($imagpart
(cadr e
)) 0)
2985 (ratgreaterp (cadr e
) 0.
))
2986 ;; We're here if realpart(b) <= 0, and n >= 0. Then return -b, n, a.
2987 `(,(caddr e
) ,(cadr e
) ,(car e
)))))
2989 ;; Integrate x^m*exp(b*x^n+a), with realpart(m) > -1.
2991 ;; See Wang, pp. 84-85.
2993 ;; I believe the formula Wang gives is incorrect. The derivation is
2994 ;; correct except for the last step.
2996 ;; Let J = integrate(x^m*exp(%i*k*x^n),x,0,inf), with real k.
2998 ;; Consider the case for k < 0. Take a sector of a circle bounded by
2999 ;; the real line and the angle -%pi/(2*n), and by the radii, r and R.
3000 ;; Since there are no poles inside this contour, the integral
3002 ;; integrate(z^m*exp(%i*k*z^n),z) = 0
3004 ;; Then J = exp(-%pi*%i*(m+1)/(2*n))*integrate(R^m*exp(k*R^n),R,0,inf)
3006 ;; because the integral along the boundary is zero except for the part
3007 ;; on the real axis. (Proof?)
3009 ;; Wang seems to say this last integral is gamma(s/n/(-k)^s) where s =
3010 ;; (m+1)/n. But that seems wrong. If we use the substitution R =
3011 ;; (y/(-k))^(1/n), we end up with the result:
3013 ;; integrate(y^((m+1)/n-1)*exp(-y),y,0,inf)/(n*k^((m+1)/n).
3015 ;; or gamma((m+1)/n)/k^((m+1)/n)/n.
3017 ;; Note that this also handles the case of
3019 ;; integrate(x^m*exp(-k*x^n),x,0,inf);
3021 ;; where k is positive real number. A simple change of variables,
3024 ;; integrate(y^((m+1)/n-1)*exp(-y),y,0,inf)/(n*k^((m+1)/n))
3026 ;; which is the same form above.
3028 (prog (c *zd
* zn nn
* dn
* nd
* dosimp $%emode
)
3029 (declare (special *zd
*))
3031 (cond (ind (setq e
($expand e
))
3032 (cond ((and (mplusp e
)
3033 (let ((*nodiverg t
))
3034 (setq e
(catch 'divergent
3039 (cond ((eq e
'divergent
) nil
)
3040 (t (return (sratsimp (cons '(mplus) e
)))))))))
3041 (setq e
(rmconst1 e
))
3044 (cond ((setq e
(ggr1 e var
))
3045 ;; e = (m b n a). That is, the integral is of the form
3046 ;; x^m*exp(b*x^n+a). I think we want to compute
3047 ;; gamma((m+1)/n)/b^((m+1)/n)/n.
3049 ;; FIXME: If n > m + 1, the integral converges. We need
3050 ;; to check for this.
3051 (destructuring-bind (m b n a
)
3053 (when (and (not (zerop1 ($realpart b
)))
3054 (not (zerop1 ($imagpart b
))))
3055 ;; The derivation only holds if b is purely real or
3056 ;; purely imaginary. Give up if it's not.
3058 ;; Check for convergence. If b is complex, we need n -
3059 ;; m > 1. If b is real, we need b < 0.
3060 (when (and (zerop1 ($imagpart b
))
3061 (not (eq ($asksign b
) '$neg
)))
3063 (when (and (not (zerop1 ($imagpart b
)))
3064 (not (eq ($asksign
(sub n
(add m
1))) '$pos
)))
3067 (setq e
(gamma1 m
(cond ((zerop1 ($imagpart b
))
3068 ;; If we're here, b must be negative.
3071 ;; Complex b. Take the imaginary part
3072 `((mabs) ,($imagpart b
))))
3074 ;; NOTE: *zd* (Ick!) is special and might be set by maybpc.
3076 ;; FIXME: Why do we set %emode here? Shouldn't we just
3077 ;; bind it? And why do we want it bound to T anyway?
3078 ;; Shouldn't the user control that? The same goes for
3082 (setq e
(m* *zd
* e
))))))
3083 (cond (e (return (m* c e
))))))
3086 ;; Match x^m*exp(b*x^n+a). If it does, return (list m b n a).
3088 (cond ((atom e
) nil
)
3091 ;; We're looking at something like exp(f(var)). See if it's
3092 ;; of the form b*x^n+a, and return (list 0 b n a). (The 0 is
3093 ;; so we can graft something onto it if needed.)
3094 (cond ((setq e
(maybpc (caddr e
) var
))
3097 ;; E should be the product of exactly 2 terms
3099 ;; Check to see if one of the terms is of the form
3100 ;; var^p. If so, make sure the realpart of p > -1. If
3101 ;; so, check the other term has the right form via
3102 ;; another call to ggr1.
3103 (or (and (setq dn
* (xtorterm (cadr e
) var
))
3104 (ratgreaterp (setq nd
* ($realpart dn
*))
3106 (setq nn
* (ggr1 (caddr e
) var
)))
3107 (and (setq dn
* (xtorterm (caddr e
) var
))
3108 (ratgreaterp (setq nd
* ($realpart dn
*))
3110 (setq nn
* (ggr1 (cadr e
) var
)))))
3111 ;; Both terms have the right form and nn* contains the arg of
3112 ;; the exponential term. Put dn* as the car of nn*. The
3113 ;; result is something like (m b n a) when we have the
3114 ;; expression x^m*exp(b*x^n+a).
3118 ;; Match b*x^n+a. If a match is found, return the list (a n b).
3119 ;; Otherwise, return NIL
3125 (t (let ((a (no-err-sub 0. e
)))
3127 (t (setq e
(m+ e
(m*t -
1 a
)))
3128 (cond ((setq e
(bx**n e
))
3132 ;; Match b*x^n. Return the list (n b) if found or NIL if not.
3135 (and (setq n
(xexponget e var
))
3137 (setq e
(let (($maxposex
1)
3139 ($expand
(m// e
(m^t var n
)))))))
3142 (defun xexponget (e nn
*)
3143 (cond ((atom e
) (cond ((eq e var
) 1.
)))
3147 (not (among nn
* (caddr e
))))
3149 (t (some #'(lambda (j) (xexponget j nn
*)) (cdr e
)))))
3152 ;;; given (b*x^n+a)^m returns (m a n b)
3157 (involve e
'(%log %sin %cos %tan
))
3160 ((mexptp e
) (cond ((among var
(caddr e
)) nil
)
3161 ((setq r
(bx**n
+a
(cadr e
)))
3162 (cons (caddr e
) r
))))
3163 ((setq r
(bx**n
+a e
)) (cons 1. r
))
3165 ;;;Catches Unfactored forms.
3166 (setq m
(m// (sdiff e var
) e
))
3171 ((and (setq r
(bx**n
+a
(sratsimp r
)))
3172 (not (among var
(setq m
(m// m
(m* (cadr r
) (caddr r
)
3173 (m^t var
(m+t -
1 (cadr r
))))))))
3174 (setq e
(m// (subin 0. e
) (m^t
(car r
) m
))))
3177 (t (setq e
(m^ e
(m// 1. m
)))
3178 (list m
(m* e
(car r
)) (cadr r
)
3179 (m* e
(caddr r
))))))))
3182 ;;;Is E = VAR raised to some power? If so return power or 0.
3184 (cond ((not (among var e
)) 0.
)
3185 (t (xtorterm e var
))))
3187 (defun xtorterm (e var1
)
3188 ;;;Is E = VAR1 raised to some power? If so return power.
3189 (cond ((alike1 e var1
) 1.
)
3192 (alike1 (cadr e
) var1
)
3193 (not (among var
(caddr e
))))
3197 (m^
(m* (m^
(caddr l
) '((rat) 1 2))
3198 (m+ (cadr l
) (m^
(m* (car l
) (caddr l
))
3202 (defun radbyterm (d l
)
3207 (destructuring-let (((const . integrand
) (rmconst1 (car l
))))
3208 (setq ans
(cons (m* const
(dintrad0 integrand d
))
3211 (defun sqdtc (e ind
)
3212 (prog (a b c varlist
)
3213 (setq varlist
(list var
))
3215 (setq e
(cdadr (ratrep* e
)))
3216 (setq c
(pdis (ptterm e
0)))
3217 (setq b
(m*t
(m//t
1 2) (pdis (ptterm e
1))))
3218 (setq a
(pdis (ptterm e
2)))
3219 (cond ((and (eq ($asksign
(m+ b
(m^
(m* a c
)
3223 (not (eq ($asksign a
) '$neg
))
3224 (eq ($asksign c
) '$pos
))
3225 (and (eq ($asksign a
) '$pos
)
3226 (not (eq ($asksign c
) '$neg
)))))
3227 (return (list a b c
))))))
3229 (defun difap1 (e pwr var m pt
)
3230 (m// (mul* (cond ((eq (ask-integer m
'$even
) '$yes
)
3234 (derivat var m e pt
))
3235 `((%gamma
) ,(m+ pwr m
))))
3237 (defun sqrtinvolve (e)
3238 (cond ((atom e
) nil
)
3241 (and (mnump (caddr e
))
3242 (not (numberp (caddr e
)))
3243 (equal (caddr (caddr e
)) 2.
))
3244 (among var
(cadr e
)))
3246 (t (some #'sqrtinvolve
(cdr e
)))))
3248 (defun bydif (r s d
)
3250 (setq d
(m+ (m*t
'*z
* var
) d
))
3251 (cond ((or (zerop1 (setq p
(m+ s
(m*t -
1 r
))))
3252 (and (zerop1 (m+ 1 p
))
3254 (difap1 (dintrad0 b
(m^ d
'((rat) 3 2)))
3255 '((rat) 3 2) '*z
* r
0))
3256 ((eq ($asksign p
) '$pos
)
3257 (difap1 (difap1 (dintrad0 1 (m^
(m+t
'z
** d
)
3259 '((rat) 3 2) '*z
* r
0)
3260 '((rat) 3 2) 'z
** p
0)))))
3262 (defun dintrad0 (n d
)
3264 (cond ((and (mexptp d
)
3265 (equal (deg (cadr d
)) 2.
))
3266 (cond ((alike1 (caddr d
) '((rat) 3.
2.
))
3267 (cond ((and (equal n
1.
)
3268 (setq l
(sqdtc (cadr d
) t
)))
3271 (setq l
(sqdtc (cadr d
) nil
)))
3272 (tbf (reverse l
)))))
3273 ((and (setq r
(findp n
))
3274 (or (eq ($asksign
(m+ -
1.
(m- r
) (m*t
2.
3278 (setq s
(m+ '((rat) -
3.
2.
) (caddr d
)))
3279 (eq ($asksign s
) '$pos
)
3280 (eq (ask-integer s
'$integer
) '$yes
))
3281 (bydif r s
(cadr d
)))
3282 ((polyinx n var nil
)
3283 (radbyterm d
(cdr n
))))))))
3286 ;;;Looks at the IMAGINARY part of a log and puts it in the interval 0 2*%pi.
3287 (defun log-imag-0-2%pi
(x)
3288 (let ((plog (simplify ($rectform
`((%plog
) ,x
)))))
3289 ;; We take the $rectform above to make sure that the log is
3290 ;; expanded out for the situations where simplifying plog itself
3291 ;; doesn't do it. This should probably be considered a bug in the
3292 ;; plog simplifier and should be fixed there.
3293 (cond ((not (free plog
'%plog
))
3294 (subst '%log
'%plog plog
))
3296 (destructuring-let (((real . imag
) (trisplit plog
)))
3297 (cond ((eq ($asksign imag
) '$neg
)
3298 (setq imag
(m+ imag %pi2
)))
3299 ((eq ($asksign
(m- imag %pi2
)) '$pos
)
3300 (setq imag
(m- imag %pi2
)))
3302 (m+ real
(m* '$%i imag
)))))))
3305 ;;; Temporary fix for a lacking in taylor, which loses with %i in denom.
3306 ;;; Besides doesn't seem like a bad thing to do in general.
3307 (defun %i-out-of-denom
(exp)
3308 (let ((denom ($denom exp
)))
3309 (cond ((among '$%i denom
)
3310 ;; Multiply the denominator by it's conjugate to get rid of
3312 (let* ((den-conj (maxima-substitute (m- '$%i
) '$%i denom
))
3314 (new-denom (sratsimp (m* denom den-conj
))))
3315 ;; If the new denominator still contains %i, just give
3316 ;; up. Otherwise, multiply the numerator by the
3317 ;; conjugate and divide by the new denominator.
3318 (if (among '$%i new-denom
)
3320 (setq exp
(m// (m* num den-conj
) new-denom
)))))
3323 ;;; LL and UL must be real otherwise this routine return $UNKNOWN.
3324 ;;; Returns $no $unknown or a list of poles in the interval (ll ul)
3325 ;;; for exp w.r.t. var.
3326 ;;; Form of list ((pole . multiplicity) (pole1 . multiplicity) ....)
3327 (defun poles-in-interval (exp var ll ul
)
3328 (let* ((denom (cond ((mplusp exp
)
3329 ($denom
(sratsimp exp
)))
3331 (free (caddr exp
) var
)
3332 (eq ($asksign
(caddr exp
)) '$neg
))
3333 (m^
(cadr exp
) (m- (caddr exp
))))
3335 (roots (real-roots denom var
))
3336 (ll-pole (limit-pole exp var ll
'$plus
))
3337 (ul-pole (limit-pole exp var ul
'$minus
)))
3338 (cond ((or (eq roots
'$failure
)
3340 (null ul-pole
)) '$unknown
)
3341 ((and (or (eq roots
'$no
)
3342 (member ($csign denom
) '($pos $neg $pz
)))
3343 ;; this clause handles cases where we can't find the exact roots,
3344 ;; but we know that they occur outside the interval of integration.
3345 ;; example: integrate ((1+exp(t))/sqrt(t+exp(t)), t, 0, 1);
3347 (eq ul-pole
'$no
)) '$no
)
3348 (t (cond ((equal roots
'$no
)
3350 (do ((dummy roots
(cdr dummy
))
3351 (pole-list (cond ((not (eq ll-pole
'$no
))
3355 (cond ((not (eq ul-pole
'$no
))
3356 (sort-poles (push `(,ul .
1) pole-list
)))
3357 ((not (null pole-list
))
3358 (sort-poles pole-list
))
3360 (let* ((soltn (caar dummy
))
3361 ;; (multiplicity (cdar dummy)) (not used? -- cwh)
3362 (root-in-ll-ul (in-interval soltn ll ul
)))
3363 (cond ((eq root-in-ll-ul
'$no
) '$no
)
3364 ((eq root-in-ll-ul
'$yes
)
3365 (let ((lim-ans (is-a-pole exp soltn
)))
3366 (cond ((null lim-ans
)
3370 (t (push (car dummy
)
3371 pole-list
))))))))))))
3374 ;;;Returns $YES if there is no pole and $NO if there is one.
3375 (defun limit-pole (exp var limit direction
)
3376 (let ((ans (cond ((member limit
'($minf $inf
) :test
#'eq
)
3377 (cond ((eq (special-convergent-formp exp limit
) '$yes
)
3379 (t (get-limit (m* exp var
) var limit direction
))))
3381 (cond ((eq ans
'$no
) '$no
)
3383 ((eq ans
'$und
) '$no
)
3384 ((equal ans
0.
) '$no
)
3387 ;;;Takes care of forms that the ratio test fails on.
3388 (defun special-convergent-formp (exp limit
)
3389 (cond ((not (oscip exp
)) '$no
)
3390 ((or (eq (sc-converg-form exp limit
) '$yes
)
3391 (eq (exp-converg-form exp limit
) '$yes
))
3395 (defun exp-converg-form (exp limit
)
3397 (setq exparg
(%einvolve exp
))
3398 (cond ((or (null exparg
)
3399 (freeof '$%i exparg
))
3405 (sratsimp (m// exp
(m^t
'$%e exparg
))))))
3406 (equal (get-limit exp var limit
) 0))
3410 (defun sc-converg-form (exp limit
)
3411 (prog (scarg trigpow
)
3412 (setq exp
($expand exp
))
3413 (setq scarg
(involve (sin-sq-cos-sq-sub exp
) '(%sin %cos
)))
3414 (cond ((null scarg
) (return '$no
))
3415 ((and (polyinx scarg var
())
3416 (eq ($asksign
(m- ($hipow scarg var
) 1)) '$pos
)) (return '$yes
))
3417 ((not (freeof var
(sdiff scarg var
)))
3419 ((and (setq trigpow
($hipow exp
`((%sin
) ,scarg
)))
3420 (eq (ask-integer trigpow
'$odd
) '$yes
)
3421 (equal (get-limit (m// exp
`((%sin
) ,scarg
)) var limit
)
3424 ((and (setq trigpow
($hipow exp
`((%cos
) ,scarg
)))
3425 (eq (ask-integer trigpow
'$odd
) '$yes
)
3426 (equal (get-limit (m// exp
`((%cos
) ,scarg
)) var limit
)
3429 (t (return '$no
)))))
3431 (defun is-a-pole (exp soltn
)
3433 (m* (maxima-substitute (m+ 'epsilon soltn
) var exp
)
3437 (defun in-interval (place ll ul
)
3438 ;; real values for ll and ul; place can be imaginary.
3439 (let ((order (ask-greateq ul ll
)))
3440 (cond ((eq order
'$yes
))
3441 ((eq order
'$no
) (let ((temp ul
)) (setq ul ll ll temp
)))
3442 (t (merror (intl:gettext
"defint: failed to order limits of integration:~%~M")
3443 (list '(mlist simp
) ll ul
)))))
3444 (if (not (equal ($imagpart place
) 0))
3446 (let ((lesseq-ul (ask-greateq ul place
))
3447 (greateq-ll (ask-greateq place ll
)))
3448 (if (and (eq lesseq-ul
'$yes
) (eq greateq-ll
'$yes
)) '$yes
'$no
))))
3450 ;; returns true or nil
3451 (defun strictly-in-interval (place ll ul
)
3452 ;; real values for ll and ul; place can be imaginary.
3453 (and (equal ($imagpart place
) 0)
3455 (eq ($asksign
(m+ ul
(m- place
))) '$pos
))
3457 (eq ($asksign
(m+ place
(m- ll
))) '$pos
))))
3459 (defun real-roots (exp var
)
3460 (let (($solvetrigwarn
(cond (defintdebug t
) ;Rest of the code for
3461 (t ()))) ;TRIGS in denom needed.
3462 ($solveradcan
(cond ((or (among '$%i exp
)
3463 (among '$%e exp
)) t
)
3465 *roots
*failures
) ;special vars for solve.
3466 (cond ((not (among var exp
)) '$no
)
3467 (t (solve exp var
1)
3468 ;; If *failures is set, we may have missed some roots.
3469 ;; We still return the roots that we have found.
3470 (do ((dummy *roots
(cddr dummy
))
3473 (cond ((not (null rootlist
))
3476 (cond ((equal ($imagpart
(caddar dummy
)) 0)
3479 ($rectform
(caddar dummy
))
3483 (defun ask-greateq (x y
)
3484 ;;; Is x > y. X or Y can be $MINF or $INF, zeroA or zeroB.
3485 (let ((x (cond ((among 'zeroa x
)
3490 (subst 0 'epsilon x
))
3491 ((or (among '$inf x
)
3495 (y (cond ((among 'zeroa y
)
3500 (subst 0 'epsilon y
))
3501 ((or (among '$inf y
)
3513 (t (let ((ans ($asksign
(m+ x
(m- y
)))))
3514 (cond ((member ans
'($zero $pos
) :test
#'eq
)
3520 (defun sort-poles (pole-list)
3521 (sort pole-list
#'(lambda (x y
)
3522 (cond ((eq (ask-greateq (car x
) (car y
))
3527 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3529 ;;; Integrate Definite Integrals involving log and exp functions. The algorithm
3530 ;;; are taken from the paper "Evaluation of CLasses of Definite Integrals ..."
3531 ;;; by K.O.Geddes et. al.
3533 ;;; 1. CASE: Integrals generated by the Gamma funtion.
3538 ;;; I t log (t) expt(- t ) dt = s signum(s)
3545 ;;; (--- (gamma(z))! )
3551 ;;; The integral converges for:
3552 ;;; s # 0, m = 0, 1, 2, ... and realpart((w+1)/s) > 0.
3554 ;;; 2. CASE: Integrals generated by the Incomplete Gamma function.
3559 ;;; I t log (t) exp(- t ) dt = (--- (gamma_incomplete(a, x ))! )
3567 ;;; The integral converges for:
3568 ;;; s # 0, m = 0, 1, 2, ... and realpart((w+1)/s) > 0.
3569 ;;; The shown solution is valid for s>0. For s<0 gamma_incomplete has to be
3570 ;;; replaced by gamma(a) - gamma_incomplete(a,x^s).
3572 ;;; 3. CASE: Integrals generated by the beta function.
3577 ;;; I log (1 - t) (1 - t) t log (t) dt =
3585 ;;; --- (--- (beta(z, w))! )!
3591 ;;; The integral converges for:
3592 ;;; n, m = 0, 1, 2, ..., s > -1 and r > -1.
3593 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3595 (defvar *debug-defint-log
* nil
)
3597 ;;; Recognize c*z^w*log(z)^m*exp(-t^s)
3599 (defun m2-log-exp-1 (expr)
3600 (when *debug-defint-log
*
3601 (format t
"~&M2-LOG-EXP-1 with ~A~%" expr
))
3605 ((mexpt) (z varp
) (w freevar
))
3606 ((mexpt) $%e
((mtimes) -
1 ((mexpt) (z varp
) (s freevar0
))))
3607 ((mexpt) ((%log
) (z varp
)) (m freevar
)))))
3609 ;;; Recognize c*z^r*log(z)^n*(1-z)^s*log(1-z)^m
3611 (defun m2-log-exp-2 (expr)
3612 (when *debug-defint-log
*
3613 (format t
"~&M2-LOG-EXP-2 with ~A~%" expr
))
3617 ((mexpt) (z varp
) (r freevar
))
3618 ((mexpt) ((%log
) (z varp
)) (n freevar
))
3619 ((mexpt) ((mplus) 1 ((mtimes) -
1 (z varp
))) (s freevar
))
3620 ((mexpt) ((%log
) ((mplus) 1 ((mtimes)-
1 (z varp
)))) (m freevar
)))))
3622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3624 (defun defint-log-exp (expr var ll ul
)
3629 ;; var1 is used as a parameter for differentiation. Add var1>0 to the
3630 ;; database, to get the desired simplification of the differentiation of
3631 ;; the gamma_incomplete function.
3632 (setq *global-defint-assumptions
*
3633 (cons (assume `((mgreaterp) ,var1
0))
3634 *global-defint-assumptions
*))
3638 (setq x
(m2-log-exp-1 expr
)))
3639 ;; The integrand matches the cases 1 and 2.
3640 (let ((c (cdras 'c x
))
3644 ($gamma_expand nil
)) ; No expansion of Gamma functions.
3646 (when *debug-defint-log
*
3647 (format t
"~&DEFINT-LOG-EXP-1:~%")
3648 (format t
"~& : c = ~A~%" c
)
3649 (format t
"~& : w = ~A~%" w
)
3650 (format t
"~& : m = ~A~%" m
)
3651 (format t
"~& : s = ~A~%" s
))
3653 (cond ((and (zerop1 ll
)
3656 (not (eq ($sign s
) '$zero
))
3657 (eq ($sign
(div (add w
1) s
)) '$pos
))
3658 ;; Case 1: Generated by the Gamma function.
3661 (simplify (list '(%signum
) s
))
3662 (power s
(mul -
1 (add m
1)))
3663 ($at
($diff
(list '(%gamma
) var1
) var1 m
)
3666 (div (add w
1) s
))))))
3667 ((and (member ($sign ll
) '($pos $pz
))
3669 (or (= m
0) (= m
1)) ; Exclude m>1, because Maxima can not
3670 ; derivate the involved hypergeometric
3672 (or (and (eq ($sign s
) '$neg
)
3673 (eq ($sign
(div (add 1 w
) s
)) '$pos
))
3674 (and (eq ($sign s
) '$pos
)
3675 (eq ($sign
(div (add 1 w
) s
)) '$pos
))))
3676 ;; Case 2: Generated by the Incomplete Gamma function.
3677 (let ((f (if (eq ($sign s
) '$pos
)
3678 (list '(%gamma_incomplete
) var1
(power ll s
))
3679 (sub (list '(%gamma
) var1
)
3680 (list '(%gamma_incomplete
) var1
(power ll s
))))))
3683 (simplify (list '(%signum
) s
))
3684 (power s
(mul -
1 (add m
1)))
3685 ($at
($diff f var1 m
)
3686 (list '(mequal) var1
(div (add 1 w
) s
)))))))
3688 (setq result nil
)))))
3691 (setq x
(m2-log-exp-2 expr
)))
3692 ;; Case 3: Generated by the Beta function.
3693 (let ((c (cdras 'c x
))
3701 (when *debug-defint-log
*
3702 (format t
"~&DEFINT-LOG-EXP-2:~%")
3703 (format t
"~& : c = ~A~%" c
)
3704 (format t
"~& : r = ~A~%" r
)
3705 (format t
"~& : n = ~A~%" n
)
3706 (format t
"~& : s = ~A~%" s
)
3707 (format t
"~& : m = ~A~%" m
))
3709 (cond ((and (integerp m
)
3713 (eq ($sign
(add 1 r
)) '$pos
)
3714 (eq ($sign
(add 1 s
)) '$pos
))
3717 ($at
($diff
($at
($diff
(list '($beta
) var1 var2
)
3719 (list '(mequal) var2
(add 1 s
)))
3721 (list '(mequal) var1
(add 1 r
))))))
3723 (setq result nil
)))))
3726 ;; Simplify result and set $gamma_expand to global value
3727 (let (($gamma_expand $gamma_expand
)) (sratsimp result
))))
3729 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;