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, respectively.
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 (defmfun $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") ul
))
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 (defun 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 ;; Call ANTIDERIV with logabs disabled,
457 ;; because the Risch algorithm assumes
458 ;; the integral of 1/x is log(x), not log(abs(x)).
459 ;; Why not just assume logabs = false within RISCHINT itself?
460 ;; Well, there's at least one existing result which requires
461 ;; logabs = true in RISCHINT, so try to make a minimal change here instead.
462 (cond ((setq ans
(let ($logabs
) (antideriv exp
)))
463 (setq ans
(intsubs ans ll ul
))
464 (return (cond (ans (m* c ans
)) (t nil
))))
466 (setq exp
(tansc exp
))
467 (cond ((setq ans
(initial-analysis exp var ll ul
))
468 (return (m* c ans
))))
470 (restore-defint-assumptions old-assumptions
*current-assumptions
*))))
472 (defun defint-list (exp var ll ul
)
474 (let ((ans (cons (car exp
)
477 (defint sub-exp var ll ul
))
479 (cond (ans (simplify ans
))
483 (defun initial-analysis (exp var ll ul
)
484 (let ((pole (cond ((not $intanalysis
)
485 '$no
) ;don't do any checking.
486 (t (poles-in-interval exp var ll ul
)))))
487 (cond ((eq pole
'$no
)
488 (cond ((and (oddfn exp var
)
489 (or (and (eq ll
'$minf
)
491 (eq ($sign
(m+ ll ul
))
493 (t (parse-integrand exp var ll ul
))))
494 ((eq pole
'$unknown
) ())
495 (t (principal-value-integral exp var ll ul pole
)))))
497 (defun parse-integrand (exp var ll ul
)
499 (cond ((setq ans
(eezz exp ll ul
)) ans
)
501 (setq ans
(method-by-limits exp var ll ul
))) ans
)
503 (setq ans
(intbyterm exp t
))) ans
)
504 ((setq ans
(method-by-limits exp var ll ul
)) ans
)
508 (cond ((not (freeof var e
))
513 (defun method-by-limits (exp var ll ul
)
514 (let ((old-assumptions *defint-assumptions
*))
515 (setq *current-assumptions
* (make-defint-assumptions 'noask
))
516 ;;Should be a PROG inside of unwind-protect, but Multics has a compiler
517 ;;bug wrt. and I want to test this code now.
519 (cond ((and (and (eq ul
'$inf
)
522 ((and (and (eq ul
'$inf
)
525 ;;;This seems((and (and (eq ul '$inf)
526 ;;;fairly losing (setq exp (subin (m+ ll var) exp))
528 ;;; (ztoinf exp var)))
531 (eq ($asksign ul
) '$pos
)
533 ;; ((and (and (equal ul 1.)
534 ;; (equal ll 0.)) (zto1 exp)))
535 (t (dintegrate exp var ll ul
)))
536 (restore-defint-assumptions old-assumptions
*defint-assumptions
*))))
539 (defun dintegrate (exp var ll ul
)
540 (let ((ans nil
) (arg nil
) (*scflag
* nil
)
541 (*dflag nil
) ($%emode t
))
542 ;;;NOT COMPLETE for sin's and cos's.
543 (cond ((and (not *sin-cos-recur
*)
547 ((and (not *rad-poly-recur
*)
548 (notinvolve exp
'(%log
))
549 (not (%einvolve exp
))
550 (method-radical-poly exp var ll ul
)))
551 ((and (not (equal *dintlog-recur
* 2.
))
552 (setq arg
(involve exp
'(%log
)))
554 ((and (not *dintexp-recur
*)
555 (setq arg
(%einvolve exp
))
557 ((and (not (ratp exp var
))
558 (setq ans
(let (($trigexpandtimes nil
)
561 (setq ans
($expand ans
))
562 (not (alike1 ans exp
))
564 ;; Call ANTIDERIV with logabs disabled,
565 ;; because the Risch algorithm assumes
566 ;; the integral of 1/x is log(x), not log(abs(x)).
567 ;; Why not just assume logabs = false within RISCHINT itself?
568 ;; Well, there's at least one existing result which requires
569 ;; logabs = true in RISCHINT, so try to make a minimal change here instead.
570 ((setq ans
(let ($logabs
) (antideriv exp
)))
574 (defun method-radical-poly (exp var ll ul
)
576 (let ((*rad-poly-recur
* t
) ;recursion stopper
578 (cond ((and (sinintp exp var
)
579 (setq result
(antideriv exp
))
580 (intsubs result ll ul
)))
582 (setq result
(ratfnt exp
))))
587 (setq result
(cv exp
))))
590 (defun principal-value-integral (exp var ll ul poles
)
591 (let ((anti-deriv ()))
592 (cond ((not (null (setq anti-deriv
(antideriv exp
))))
593 (cond ((not (null poles
))
595 (cond ((take-principal anti-deriv ll ul poles
))
598 ;; adds up integrals of ranges between each pair of poles.
599 ;; checks if whole thing is divergent as limits of integration approach poles.
600 (defun take-principal (anti-deriv ll ul poles
&aux ans merged-list
)
601 ;;; calling $logcontract causes antiderivative of 1/(1-x^5) to blow up
602 ;; (setq anti-deriv (cond ((involve anti-deriv '(%log))
603 ;; ($logcontract anti-deriv))
606 (setq merged-list
(interval-list poles ll ul
))
607 (do ((current-pole (cdr merged-list
) (cdr current-pole
))
608 (previous-pole merged-list
(cdr previous-pole
)))
609 ((null current-pole
) t
)
611 (intsubs anti-deriv
(m+ (caar previous-pole
) 'epsilon
)
612 (m+ (caar current-pole
) (m- 'epsilon
))))))
614 (setq ans
(get-limit (get-limit ans
'epsilon
0 '$plus
) 'prin-inf
'$inf
))
616 (cond ((or (null ans
)
617 (not (free ans
'$infinity
))
618 (not (free ans
'$ind
))) ())
619 ((or (among '$minf ans
)
623 (t (principal) ans
)))
625 (defun interval-list (pole-list ll ul
)
626 (let ((first (car (first pole-list
)))
627 (last (caar (last pole-list
))))
630 (setq pole-list
(subst 'prin-inf
'$inf pole-list
))))
633 (setq pole-list
(append pole-list
(list (cons ul
'ignored
))))))
636 (setq pole-list
(subst (m- 'prin-inf
) '$minf pole-list
))))
637 (t (if (eq ll
'$minf
)
638 (setq ll
(m- 'prin-inf
)))
639 (setq pole-list
(append (list (cons ll
'ignored
)) pole-list
)))))
642 ;; Assumes EXP is a rational expression with no polynomial part and
643 ;; converts the finite integration to integration over a half-infinite
644 ;; interval. The substitution y = (x-a)/(b-x) is used. Equivalently,
645 ;; x = (b*y+a)/(y+1).
647 ;; (I'm guessing CV means Change Variable here.)
649 (if (not (or (real-infinityp ll
) (real-infinityp ul
)))
650 ;; FIXME! This is a hack. We apply the transformation with
651 ;; symbolic limits and then substitute the actual limits later.
652 ;; That way method-by-limits (usually?) sees a simpler
655 ;; See Bugs 938235 and 941457. These fail because $FACTOR is
656 ;; unable to factor the transformed result. This needs more
657 ;; work (in other places).
658 (let ((trans (integrand-changevar (m// (m+t
'll
(m*t
'ul
'yx
))
661 ;; If the limit is a number, use $substitute so we simplify
662 ;; the result. Do we really want to do this?
663 (setf trans
(if (mnump ll
)
664 ($substitute ll
'll trans
)
665 (subst ll
'll trans
)))
666 (setf trans
(if (mnump ul
)
667 ($substitute ul
'ul trans
)
668 (subst ul
'ul trans
)))
669 (method-by-limits trans
'yx
0.
'$inf
))
672 ;; Integrate rational functions over a finite interval by doing the
673 ;; polynomial part directly, and converting the rational part to an
674 ;; integral from 0 to inf. This is evaluated via residues.
677 ;; PQR divides the rational expression and returns the quotient
679 (flet ((try-antideriv (e lo hi
)
680 (let ((ans (antideriv e
)))
682 (intsubs ans lo hi
)))))
684 (cond ((equal 0.
(car e
))
685 ;; No polynomial part
686 (let ((ans (try-antideriv exp ll ul
)))
691 ;; Only polynomial part
692 (eezz (car e
) ll ul
))
694 ;; A non-zero quotient and remainder. Combine the results
696 (let ((ans (try-antideriv (m// (cdr e
) dn
*) ll ul
)))
698 (m+t
(eezz (car e
) ll ul
)
701 (m+t
(eezz (car e
) ll ul
)
702 (cv (m// (cdr e
) dn
*)))))))))))
704 ;; I think this takes a rational expression E, and finds the
705 ;; polynomial part. A cons is returned. The car is the quotient and
706 ;; the cdr is the remainder.
708 (let ((varlist (list var
)))
710 (setq e
(cdr (ratrep* e
)))
711 (setq dn
* (pdis (ratdenominator e
)))
712 (setq e
(pdivide (ratnumerator e
) (ratdenominator e
)))
713 (cons (simplify (rdis (car e
))) (simplify (rdis (cadr e
))))))
716 (defun intbyterm (exp *nodiverg
)
717 (let ((saved-exp exp
))
719 (let ((ans (catch 'divergent
720 (andmapcar #'(lambda (new-exp)
722 (defint new-exp var ll ul
)))
724 (cond ((null ans
) nil
)
726 (let ((*nodiverg nil
))
727 (cond ((setq ans
(antideriv saved-exp
))
730 (t (sratsimp (m+l ans
))))))
731 ;;;If leadop isn't plus don't do anything.
737 (a (cond ((and (zerop1 ($limit d var ll
'$plus
))
738 (eq (limit-pole (m+ exp
(m+ (m- ll
) var
))
743 (b (cond ((and (zerop1 ($limit d var ul
'$minus
))
744 (eq (limit-pole (m+ exp
(m+ ul
(m- var
)))
752 (cond (*nodiverg
(throw 'divergent
'divergent
))
753 (t (merror (intl:gettext
"defint: integral is divergent.")))))
755 (defun make-defint-assumptions (ask-or-not)
756 (cond ((null (order-limits ask-or-not
)) ())
757 (t (mapc 'forget
*defint-assumptions
*)
758 (setq *defint-assumptions
* ())
759 (let ((sign-ll (cond ((eq ll
'$inf
) '$pos
)
760 ((eq ll
'$minf
) '$neg
)
761 (t ($sign
($limit ll
)))))
762 (sign-ul (cond ((eq ul
'$inf
) '$pos
)
763 ((eq ul
'$minf
) '$neg
)
764 (t ($sign
($limit ul
)))))
765 (sign-ul-ll (cond ((and (eq ul
'$inf
)
766 (not (eq ll
'$inf
))) '$pos
)
768 (not (eq ll
'$minf
))) '$neg
)
769 (t ($sign
($limit
(m+ ul
(m- ll
))))))))
770 (cond ((eq sign-ul-ll
'$pos
)
771 (setq *defint-assumptions
*
772 `(,(assume `((mgreaterp) ,var
,ll
))
773 ,(assume `((mgreaterp) ,ul
,var
)))))
774 ((eq sign-ul-ll
'$neg
)
775 (setq *defint-assumptions
*
776 `(,(assume `((mgreaterp) ,var
,ul
))
777 ,(assume `((mgreaterp) ,ll
,var
))))))
778 (cond ((and (eq sign-ll
'$pos
)
780 (setq *defint-assumptions
*
781 `(,(assume `((mgreaterp) ,var
0))
782 ,@*defint-assumptions
*)))
783 ((and (eq sign-ll
'$neg
)
785 (setq *defint-assumptions
*
786 `(,(assume `((mgreaterp) 0 ,var
))
787 ,@*defint-assumptions
*)))
788 (t *defint-assumptions
*))))))
790 (defun restore-defint-assumptions (old-assumptions assumptions
)
791 (do ((llist assumptions
(cdr llist
)))
793 (forget (car llist
)))
794 (do ((llist old-assumptions
(cdr llist
)))
796 (assume (car llist
))))
798 (defun make-global-assumptions ()
799 (setq *global-defint-assumptions
*
800 (cons (assume '((mgreaterp) *z
* 0.
))
801 *global-defint-assumptions
*))
802 ;; *Z* is a "zero parameter" for this package.
803 ;; Its also used to transform.
804 ;; limit(exp,var,val,dir) -- limit(exp,tvar,0,dir)
805 (setq *global-defint-assumptions
*
806 (cons (assume '((mgreaterp) epsilon
0.
))
807 *global-defint-assumptions
*))
808 (setq *global-defint-assumptions
*
809 (cons (assume '((mlessp) epsilon
1.0e-8))
810 *global-defint-assumptions
*))
811 ;; EPSILON is used in principal value code to denote the familiar
812 ;; mathematical entity.
813 (setq *global-defint-assumptions
*
814 (cons (assume '((mgreaterp) prin-inf
1.0e+8))
815 *global-defint-assumptions
*)))
817 ;;; PRIN-INF Is a special symbol in the principal value code used to
818 ;;; denote an end-point which is proceeding to infinity.
820 (defun forget-global-assumptions ()
821 (do ((llist *global-defint-assumptions
* (cdr llist
)))
823 (forget (car llist
)))
824 (cond ((not (null integer-info
))
825 (do ((llist integer-info
(cdr llist
)))
827 (i-$remove
`(,(cadar llist
) ,(caddar llist
)))))))
829 (defun order-limits (ask-or-not)
830 (cond ((or (not (equal ($imagpart ll
) 0))
831 (not (equal ($imagpart ul
) 0))) ())
832 (t (cond ((alike1 ll
(m*t -
1 '$inf
))
834 (cond ((alike1 ul
(m*t -
1 '$inf
))
836 (cond ((alike1 ll
(m*t -
1 '$minf
))
838 (cond ((alike1 ul
(m*t -
1 '$minf
))
841 ; We have minf <= ll = ul <= inf
844 ; We have minf <= ll < ul = inf
847 ; We have minf = ll < ul < inf
855 ; so that minf < ll < ul = inf
856 (setq exp
(subin (m- var
) exp
))
860 (equal (complm ask-or-not
) -
1))
861 ; We have minf <= ul < ll
868 ; so that minf <= ll < ul
873 (defun complm (ask-or-not)
874 (let ((askflag (cond ((eq ask-or-not
'ask
) t
)
877 (cond ((alike1 ul ll
) 0.
)
878 ((eq (setq a
(cond (askflag ($asksign
($limit
(m+t ul
(m- ll
)))))
879 (t ($sign
($limit
(m+t ul
(m- ll
)))))))
885 ;; Substitute a and b into integral e
887 ;; Looks for discontinuties in integral, and works around them.
890 ;; integrate(x^(2*n)*exp(-(x)^2),x) ==>
891 ;; -gamma_incomplete((2*n+1)/2,x^2)*x^(2*n+1)*abs(x)^(-2*n-1)/2
893 ;; the integral has a discontinuity at x=0.
895 (defun intsubs (e a b
)
896 (let ((edges (cond ((not $intanalysis
)
897 '$no
) ;don't do any checking.
898 (t (discontinuities-in-interval
899 (let (($algebraic t
))
903 (cond ((or (eq edges
'$no
)
904 (eq edges
'$unknown
))
905 (whole-intsubs e a b
))
907 (do* ((l edges
(cdr l
))
910 (b1 (cadr l
) (cadr l
)))
911 ((null (cdr l
)) (if (every (lambda (x) x
) total
)
914 (whole-intsubs e a1 b1
)
917 ;; look for terms with a negative exponent
919 ;; recursively traverses exp in order to find discontinuities such as
920 ;; erfc(1/x-x) at x=0
921 (defun discontinuities-denom (exp)
923 ((and (eq (caar exp
) 'mexpt
)
924 (not (freeof var
(cadr exp
)))
925 (not (member ($sign
(caddr exp
)) '($pos $pz
))))
926 (m^
(cadr exp
) (m- (caddr exp
))))
928 (m*l
(mapcar #'discontinuities-denom
(cdr exp
))))))
930 ;; returns list of places where exp might be discontinuous in var.
931 ;; list begins with ll and ends with ul, and include any values between
933 ;; return '$no or '$unknown if no discontinuities found.
934 (defun discontinuities-in-interval (exp var ll ul
)
935 (let* ((denom (discontinuities-denom exp
))
936 (roots (real-roots denom var
)))
937 (cond ((eq roots
'$failure
)
941 (t (do ((dummy roots
(cdr dummy
))
946 (sortgreat pole-list
)
949 (let ((soltn (caar dummy
)))
950 ;; (multiplicity (cdar dummy)) ;; not used
951 (if (strictly-in-interval soltn ll ul
)
952 (push soltn pole-list
))))))))
955 ;; Carefully substitute the integration limits A and B into the
957 (defun whole-intsubs (e a b
)
958 (cond ((easy-subs e a b
))
959 (t (setq *current-assumptions
*
960 (make-defint-assumptions 'ask
)) ;get forceful!
961 (let (($algebraic t
))
962 (setq e
(sratsimp e
))
963 (cond ((limit-subs e a b
))
964 (t (same-sheet-subs e a b
)))))))
966 ;; Try easy substitutions. Return NIL if we can't.
967 (defun easy-subs (e ll ul
)
968 (cond ((or (infinityp ll
) (infinityp ul
))
969 ;; Infinite limits aren't easy
972 (cond ((or (polyinx e var
())
973 (and (not (involve e
'(%log %asin %acos %atan %asinh %acosh %atanh %atan2
974 %gamma_incomplete %expintegral_ei
)))
975 (free ($denom e
) var
)))
976 ;; It's easy if we have a polynomial. I (rtoy) think
977 ;; it's also easy if the denominator is free of the
978 ;; integration variable and also if the expression
979 ;; doesn't involve inverse functions.
981 ;; gamma_incomplete and expintegral_ie
982 ;; included because of discontinuity in
983 ;; gamma_incomplete(0, exp(%i*x)) and
984 ;; expintegral_ei(exp(%i*x))
986 ;; XXX: Are there other cases we've forgotten about?
988 ;; So just try to substitute the limits into the
989 ;; expression. If no errors are produced, we're done.
990 (let ((ll-val (no-err-sub ll e
))
991 (ul-val (no-err-sub ul e
)))
992 (cond ((or (eq ll-val t
)
994 ;; no-err-sub has returned T. An error was catched.
1001 (defun limit-subs (e ll ul
)
1002 (cond ((involve e
'(%atan %gamma_incomplete %expintegral_ei
))
1003 ()) ; functions with discontinuities
1004 (t (setq e
($multthru e
))
1005 (let ((a1 ($limit e var ll
'$plus
))
1006 (a2 ($limit e var ul
'$minus
)))
1007 (combine-ll-ans-ul-ans a1 a2
)))))
1009 ;; check for divergent integral
1010 (defun combine-ll-ans-ul-ans (a1 a2
)
1011 (cond ((member a1
'($inf $minf $infinity
) :test
#'eq
)
1012 (cond ((member a2
'($inf $minf $infinity
) :test
#'eq
)
1013 (cond ((eq a2 a1
) ())
1016 ((member a2
'($inf $minf $infinity
) :test
#'eq
) (diverg))
1017 ((or (member a1
'($und $ind
) :test
#'eq
)
1018 (member a2
'($und $ind
) :test
#'eq
)) ())
1021 ;;;This function works only on things with ATAN's in them now.
1022 (defun same-sheet-subs (exp ll ul
&aux ll-ans ul-ans
)
1023 ;; POLES-IN-INTERVAL doesn't know about the poles of tan(x). Call
1024 ;; trigsimp to convert tan into sin/cos, which POLES-IN-INTERVAL
1025 ;; knows how to handle.
1027 ;; XXX Should we fix POLES-IN-INTERVAL instead?
1029 ;; XXX Is calling trigsimp too much? Should we just only try to
1030 ;; substitute sin/cos for tan?
1032 ;; XXX Should the result try to convert sin/cos back into tan? (A
1033 ;; call to trigreduce would do it, among other things.)
1034 (let* ((exp (mfuncall '$trigsimp exp
))
1035 (poles (atan-poles exp ll ul
)))
1036 ;;POLES -> ((mlist) ((mequal) ((%atan) foo) replacement) ......)
1037 ;;We can then use $SUBSTITUTE
1038 (setq ll-ans
(limcp exp var ll
'$plus
))
1039 (setq exp
(sratsimp ($substitute poles exp
)))
1040 (setq ul-ans
(limcp exp var ul
'$minus
))
1043 (combine-ll-ans-ul-ans ll-ans ul-ans
)
1046 (defun atan-poles (exp ll ul
)
1047 `((mlist) ,@(atan-pole1 exp ll ul
)))
1049 (defun atan-pole1 (exp ll ul
&aux ipart
)
1052 ((matanp exp
) ;neglect multiplicity and '$unknowns for now.
1053 (desetq (exp . ipart
) (trisplit exp
))
1055 ((not (equal (sratsimp ipart
) 0)) ())
1056 (t (let ((pole (poles-in-interval (let (($algebraic t
))
1057 (sratsimp (cadr exp
)))
1059 (cond ((and pole
(not (or (eq pole
'$unknown
)
1061 (do ((l pole
(cdr l
)) (llist ()))
1064 ((zerop1 (m- (caar l
) ll
)) t
) ; don't worry about discontinuity
1065 ((zerop1 (m- (caar l
) ul
)) t
) ; at boundary of integration
1066 (t (let ((low-lim ($limit
(cadr exp
) var
(caar l
) '$minus
))
1067 (up-lim ($limit
(cadr exp
) var
(caar l
) '$plus
)))
1068 (cond ((and (not (eq low-lim up-lim
))
1069 (real-infinityp low-lim
)
1070 (real-infinityp up-lim
))
1071 (let ((change (if (eq low-lim
'$minf
)
1074 (setq llist
(cons `((mequal simp
) ,exp
,(m+ exp change
))
1075 llist
)))))))))))))))
1076 (t (do ((l (cdr exp
) (cdr l
))
1079 (setq llist
(append llist
(atan-pole1 (car l
) ll ul
)))))))
1081 (defun difapply (n d s fn1
)
1082 (prog (k m r $noprincipal
)
1083 (cond ((eq ($asksign
(m+ (deg d
) (m- s
) (m- 2.
))) '$neg
)
1085 (setq $noprincipal t
)
1086 (cond ((or (not (mexptp d
))
1087 (not (numberp (setq r
(caddr d
)))))
1091 (equal 1.
(deg (cadr d
))))
1093 (setq m
(deg (setq d
(cadr d
))))
1094 (setq k
(m// (m+ s
2.
) m
))
1095 (cond ((eq (ask-integer (m// (m+ s
2.
) m
) '$any
) '$yes
)
1097 (t (setq k
(m+ 1 k
))))
1098 (cond ((eq ($sign
(m+ r
(m- k
))) '$pos
)
1099 (return (diffhk fn1 n d k
(m+ r
(m- k
))))))))
1101 (defun diffhk (fn1 n d r m
)
1104 (setq d1
(funcall fn1 n
1107 (cond (d1 (return (difap1 d1 r
'*z
* m
0.
))))))
1109 (defun principal nil
1110 (cond ($noprincipal
(diverg))
1112 (format t
"Principal Value~%")
1115 ;; e is of form poly(x)*exp(m*%i*x)
1116 ;; s is degree of denominator
1117 ;; adds e to bptu or bptd according to sign of m
1120 (cond ((or (mnump e
) (constant e
))
1121 (setq bptu
(cons e bptu
)))
1122 (t (setq e
(rmconst1 e
))
1126 (setq e
(catch 'ptimes%e
(ptimes%e nn
* nd
*)))
1127 (cond ((null e
) nil
)
1128 (t (setq e
(m* c e
))
1129 (cond (*updn
(setq bptu
(cons e bptu
)))
1130 (t (setq bptd
(cons e bptd
))))))))))
1132 ;; check term is of form poly(x)*exp(m*%i*x)
1133 ;; n is degree of denominator
1134 (defun ptimes%e
(term n
)
1135 (cond ((and (mexptp term
)
1136 (eq (cadr term
) '$%e
)
1137 (polyinx (caddr term
) var nil
)
1138 (eq ($sign
(m+ (deg ($realpart
(caddr term
))) -
1))
1140 (eq ($sign
(m+ (deg (setq nn
* ($imagpart
(caddr term
))))
1143 (cond ((eq ($asksign
(ratdisrep (ratcoef nn
* var
))) '$pos
)
1145 (t (setq *updn nil
)))
1147 ((and (mtimesp term
)
1148 (setq nn
* (polfactors term
))
1149 (or (null (car nn
*))
1150 (eq ($sign
(m+ n
(m- (deg (car nn
*)))))
1152 (not (alike1 (cadr nn
*) term
))
1153 (ptimes%e
(cadr nn
*) n
)
1155 (t (throw 'ptimes%e nil
))))
1157 (defun csemidown (n d var
)
1158 (let ((pcprntd t
)) ;Not sure what to do about PRINCIPAL values here.
1159 (princip (res n d
#'lowerhalf
#'(lambda (x)
1160 (cond ((equal ($imagpart x
) 0) t
)
1163 (defun lowerhalf (j)
1164 (eq ($asksign
($imagpart j
)) '$neg
))
1166 (defun upperhalf (j)
1167 (eq ($asksign
($imagpart j
)) '$pos
))
1170 (defun csemiup (n d var
)
1171 (let ((pcprntd t
)) ;I'm not sure what to do about PRINCIPAL values here.
1172 (princip (res n d
#'upperhalf
#'(lambda (x)
1173 (cond ((equal ($imagpart x
) 0) t
)
1177 (cond ((null n
) nil
)
1178 (t (m*t
'$%i
($rectform
(m+ (cond ((car n
)
1186 ;; exponentialize sin and cos
1189 ((polyinx e var nil
) e
)
1190 ((eq (caar e
) '%sin
)
1193 (m+t
(m^t
'$%e
(m*t
'$%i
(cadr e
)))
1194 (m- (m^t
'$%e
(m*t
(m- '$%i
) (cadr e
)))))))
1195 ((eq (caar e
) '%cos
)
1196 (mul* '((rat) 1.
2.
)
1197 (m+t
(m^t
'$%e
(m*t
'$%i
(cadr e
)))
1198 (m^t
'$%e
(m*t
(m- '$%i
) (cadr e
))))))
1200 (cons (list (caar e
)) (mapcar #'sconvert
(cdr e
)))))))
1202 (defun polfactors (exp)
1204 (cond ((mplusp exp
) nil
)
1205 (t (cond ((mtimesp exp
)
1206 (setq exp
(reverse (cdr exp
))))
1207 (t (setq exp
(list exp
))))
1208 (mapc #'(lambda (term)
1209 (cond ((polyinx term var nil
)
1211 (t (push term rest
))))
1213 (list (m*l poly
) (m*l rest
))))))
1217 (cond ((atom e
) (return e
))
1218 ((not (among '$%e e
)) (return e
))
1221 (setq d
($imagpart
(caddr e
)))
1222 (return (m* (m^t
'$%e
($realpart
(caddr e
)))
1224 (m*t
'$%i
`((%sin
) ,d
))))))
1225 (t (return (simplify (cons (list (caar e
))
1226 (mapcar #'esap
(cdr e
)))))))))
1228 ;; computes integral from minf to inf for expressions of the form
1229 ;; exp(%i*m*x)*r(x) by residues on either the upper half
1230 ;; plane or the lower half plane, depending on whether
1231 ;; m is positive or negative. [wang p. 77]
1233 ;; exponentializes sin and cos before applying residue method.
1234 ;; can handle some expressions with poles on real line, such as
1236 (defun mtosc (grand)
1241 plf bptu bptd s upans downans
)
1242 (cond ((not (or (polyinx d var nil
)
1243 (and (setq grand
(%einvolve d
))
1245 (polyinx (setq d
(sratsimp (m// d
(m^t
'$%e grand
))))
1248 (setq n
(m// n
(m^t
'$%e grand
)))))) nil
)
1249 ((equal (setq s
(deg d
)) 0) nil
)
1250 ;;;Above tests for applicability of this method.
1251 ((and (or (setq plf
(polfactors n
)) t
)
1252 (setq n
($expand
(cond ((car plf
)
1253 (m*t
'x
* (sconvert (cadr plf
))))
1255 (cond ((mplusp n
) (setq n
(cdr n
)))
1256 (t (setq n
(list n
))))
1258 (cond ((polyinx term var nil
)
1259 ;; call to $expand can create rational terms
1260 ;; with no exp(m*%i*x)
1261 (setq ratterms
(cons term ratterms
)))
1264 ;;;Function RIB sets up the values of BPTU and BPTD
1266 (setq bptu
(subst (car plf
) 'x
* bptu
))
1267 (setq bptd
(subst (car plf
) 'x
* bptd
))
1268 (setq ratterms
(subst (car plf
) 'x
* ratterms
))
1269 t
) ;CROCK, CROCK. This is TERRIBLE code.
1271 ;;;If there is BPTU then CSEMIUP must succeed.
1272 ;;;Likewise for BPTD.
1275 (let (($intanalysis nil
))
1276 ;; The original integrand was already
1277 ;; determined to have no poles by initial-analysis.
1278 ;; If individual terms of the expansion have poles, the poles
1279 ;; must cancel each other out, so we can ignore them.
1280 (try-defint (m// (m+l ratterms
) d
) var
'$minf
'$inf
))
1282 ;; if integral of ratterms is divergent, ratans is nil,
1283 ;; and mtosc returns nil
1285 (cond (bptu (setq upans
(csemiup (m+l bptu
) d var
)))
1287 (cond (bptd (setq downans
(csemidown (m+l bptd
) d var
)))
1288 (t (setq downans
0))))
1290 (sratsimp (m+ ratans
1291 (m* '$%pi
(m+ upans
(m- downans
)))))))))
1294 (defun evenfn (e var
)
1295 (let ((temp (m+ (m- e
)
1297 ($substitute
(m- var
) var e
))
1298 (t ($ratsubst
(m- var
) var e
))))))
1299 (cond ((zerop1 temp
)
1301 ((zerop1 (sratsimp temp
))
1305 (defun oddfn (e var
)
1306 (let ((temp (m+ e
(cond ((atom var
)
1307 ($substitute
(m- var
) var e
))
1308 (t ($ratsubst
(m- var
) var e
))))))
1309 (cond ((zerop1 temp
)
1311 ((zerop1 (sratsimp temp
))
1315 (defun ztoinf (grand var
)
1316 (prog (n d sn
* sd
* varlist
1318 ans r $savefactors checkfactors temp test-var
)
1319 (setq $savefactors t sn
* (setq sd
* (list 1.
)))
1320 (cond ((eq ($sign
(m+ loopstop
* -
1))
1323 ((setq temp
(or (scaxn grand
)
1326 ((involve grand
'(%sin %cos %tan
))
1327 (setq grand
(sconvert grand
))
1330 (cond ((polyinx grand var nil
)
1332 ((and (ratp grand var
)
1334 (andmapcar #'snumden
(cdr grand
)))
1342 (setq n
(rmconst1 nn
*))
1343 (setq d
(rmconst1 dn
*))
1348 (cond ((polyinx d var nil
)
1351 (cond ((and (setq r
(findp n
))
1352 (eq (ask-integer r
'$integer
) '$yes
)
1353 (setq test-var
(bxm d s
))
1354 (setq ans
(apply 'fan
(cons (m+ 1. r
) test-var
))))
1355 (return (m* (m// nc dc
) (sratsimp ans
))))
1356 ((and (ratp grand var
)
1357 (setq ans
(zmtorat n
(cond ((mtimesp d
) d
)
1360 (return (m* (m// nc dc
) ans
)))
1361 ((and (evenfn d var
)
1362 (setq nn
* (p*lognxp n s
)))
1363 (setq ans
(log*rat
(car nn
*) d
(cadr nn
*)))
1364 (return (m* (m// nc dc
) ans
)))
1365 ((involve grand
'(%log
))
1366 (cond ((setq ans
(logquad0 grand
))
1367 (return (m* (m// nc dc
) ans
)))
1370 (cond ((setq temp
(batapp grand
))
1374 (cond ((let ((*mtoinf
* nil
))
1375 (setq temp
(ggr grand t
)))
1378 (cond ((let ((*nodiverg t
))
1379 (setq ans
(catch 'divergent
1380 (andmapcar #'(lambda (g)
1383 (cond ((eq ans
'divergent
) nil
)
1384 (t (return (sratsimp (m+l ans
)))))))))
1386 (cond ((and (evenfn grand var
)
1387 (setq loopstop
* (m+ 1 loopstop
*))
1388 (setq ans
(method-by-limits grand var
'$minf
'$inf
)))
1389 (return (m*t
'((rat) 1.
2.
) ans
)))
1392 (defun ztorat (n d s
)
1393 (cond ((and (null *dflag
)
1394 (setq s
(difapply n d s
#'ztorat
)))
1396 ((setq n
(let ((plogabs ()))
1397 (keyhole (m* `((%plog
) ,(m- var
)) n
) d var
)))
1400 ;; Let's not signal an error here. Return nil so that we
1401 ;; eventually return a noun form if no other algorithm gives
1404 (merror (intl:gettext
"defint: keyhole integration failed.~%"))
1409 (defun logquad0 (exp)
1410 (let ((a ()) (b ()) (c ()))
1411 (cond ((setq exp
(logquad exp
))
1412 (setq a
(car exp
) b
(cadr exp
) c
(caddr exp
))
1413 ($asksign b
) ;let the data base know about the sign of B.
1414 (cond ((eq ($asksign c
) '$pos
)
1415 (setq c
(m^
(m// c a
) '((rat) 1.
2.
)))
1417 `((%acos
) ,(add* 'epsilon
(m// b
(mul* 2. a c
))))))
1418 (setq a
(m// (m* b
`((%log
) ,c
))
1419 (mul* a
(simplify `((%sin
) ,b
)) c
)))
1420 (get-limit a
'epsilon
0 '$plus
))))
1423 (defun logquad (exp)
1424 (let ((varlist (list var
)))
1426 (setq exp
(cdr (ratrep* exp
)))
1427 (cond ((and (alike1 (pdis (car exp
))
1429 (not (atom (cdr exp
)))
1430 (equal (cadr (cdr exp
)) 2.
)
1431 (not (equal (ptterm (cddr exp
) 0.
) 0.
)))
1432 (setq exp
(mapcar 'pdis
(cdr (oddelm (cdr exp
)))))))))
1434 (defun mtoinf (grand var
)
1435 (prog (ans ans1 sd
* sn
* p
* pe
* n d s nc dc $savefactors checkfactors temp
)
1436 (setq $savefactors t
)
1437 (setq sn
* (setq sd
* (list 1.
)))
1438 (cond ((eq ($sign
(m+ loopstop
* -
1)) '$pos
)
1440 ((involve grand
'(%sin %cos
))
1441 (cond ((and (evenfn grand var
)
1442 (or (setq temp
(scaxn grand
))
1443 (setq temp
(ssp grand
))))
1444 (return (m*t
2. temp
)))
1445 ((setq temp
(mtosc grand
))
1448 ((among '$%i
(%einvolve grand
))
1449 (cond ((setq temp
(mtosc grand
))
1452 (setq grand
($exponentialize grand
)) ; exponentializing before numden
1453 (cond ((polyinx grand var nil
) ; avoids losing multiplicities [ 1309432 ]
1455 ((and (ratp grand var
)
1457 (andmapcar #'snumden
(cdr grand
)))
1458 (setq nn
* (m*l sn
*) sn
* nil
)
1459 (setq dn
* (m*l sd
*) sd
* nil
))
1461 (setq n
(rmconst1 nn
*))
1462 (setq d
(rmconst1 dn
*))
1467 (cond ((polyinx d var nil
)
1469 (cond ((and (not (%einvolve grand
))
1470 (notinvolve exp
'(%sinh %cosh %tanh
))
1472 (eq (ask-integer p
* '$integer
) '$yes
)
1473 (setq pe
* (bxm d s
)))
1474 (cond ((and (eq (ask-integer (caddr pe
*) '$even
) '$yes
)
1475 (eq (ask-integer p
* '$even
) '$yes
))
1476 (cond ((setq ans
(apply 'fan
(cons (m+ 1. p
*) pe
*)))
1477 (setq ans
(m*t
2. ans
))
1478 (return (m* (m// nc dc
) ans
)))))
1479 ((equal (car pe
*) 1.
)
1480 (cond ((and (setq ans
(apply 'fan
(cons (m+ 1. p
*) pe
*)))
1481 (setq nn
* (fan (m+ 1. p
*)
1486 (setq ans
(m+ ans
(m*t
(m^ -
1 p
*) nn
*)))
1487 (return (m* (m// nc dc
) ans
))))))))
1489 ((and (ratp grand var
)
1490 (setq ans1
(zmtorat n
(cond ((mtimesp d
) d
) (t ($sqfr d
))) s
#'mtorat
)))
1491 (setq ans
(m*t
'$%pi ans1
))
1492 (return (m* (m// nc dc
) ans
)))
1493 ((and (or (%einvolve grand
)
1494 (involve grand
'(%sinh %cosh %tanh
)))
1495 (p*pin%ex n
) ;setq's P* and PE*...Barf again.
1496 (setq ans
(catch 'pin%ex
(pin%ex d
))))
1497 ;; We have an integral of the form p(x)*F(exp(x)), where
1498 ;; p(x) is a polynomial.
1501 (return (dintexp grand var
)))
1502 ((not (and (zerop1 (get-limit grand var
'$inf
))
1503 (zerop1 (get-limit grand var
'$minf
))))
1504 ;; These limits must exist for the integral to converge.
1506 ((setq ans
(rectzto%pi2
(m*l p
*) (m*l pe
*) d
))
1507 ;; This only handles the case when the F(z) is a
1508 ;; rational function.
1509 (return (m* (m// nc dc
) ans
)))
1510 ((setq ans
(log-transform (m*l p
*) (m*l pe
*) d
))
1511 ;; If we get here, F(z) is not a rational function.
1512 ;; We transform it using the substitution x=log(y)
1513 ;; which gives us an integral of the form
1514 ;; p(log(y))*F(y)/y, which maxima should be able to
1516 (return (m* (m// nc dc
) ans
)))
1518 ;; Give up. We don't know how to handle this.
1521 (cond ((setq ans
(ggrm grand
))
1523 ((and (evenfn grand var
)
1524 (setq loopstop
* (m+ 1 loopstop
*))
1525 (setq ans
(method-by-limits grand var
0 '$inf
)))
1526 (return (m*t
2. ans
)))
1529 (defun linpower0 (exp var
)
1530 (cond ((and (setq exp
(linpower exp var
))
1531 (eq (ask-integer (caddr exp
) '$even
)
1533 (ratgreaterp 0.
(car exp
)))
1536 ;;; given (b*x+a)^n+c returns (a b n c)
1537 (defun linpower (exp var
)
1538 (let (linpart deg lc c varlist
)
1539 (cond ((not (polyp exp
)) nil
)
1540 (t (let ((varlist (list var
)))
1542 (setq linpart
(cadr (ratrep* exp
)))
1543 (cond ((atom linpart
)
1545 (t (setq deg
(cadr linpart
))
1546 ;;;get high degree of poly
1547 (setq linpart
($diff exp var
(m+ deg -
1)))
1548 ;;;diff down to linear.
1549 (setq lc
(sdiff linpart var
))
1550 ;;;all the way to constant.
1551 (setq linpart
(sratsimp (m// linpart lc
)))
1552 (setq lc
(sratsimp (m// lc
`((mfactorial) ,deg
))))
1553 ;;;get rid of factorial from differentiation.
1554 (setq c
(sratsimp (m+ exp
(m* (m- lc
)
1555 (m^ linpart deg
)))))))
1556 ;;;Sees if can be expressed as (a*x+b)^n + part freeof x.
1557 (cond ((not (among var c
))
1558 `(,lc
,linpart
,deg
,c
))
1561 (defun mtorat (n d s
)
1562 (let ((*semirat
* t
))
1563 (cond ((and (null *dflag
)
1564 (setq s
(difapply n d s
#'mtorat
)))
1566 (t (csemiup n d var
)))))
1568 (defun zmtorat (n d s fn1
)
1570 (cond ((eq ($sign
(m+ s
(m+ 1 (setq nn
* (deg n
)))))
1573 ((eq ($sign
(m+ s -
4))
1576 (setq d
($factor d
))
1577 (setq c
(rmconst1 d
))
1583 (setq n
(partnum n d
))
1585 (setq n
($xthru
(m+l
1586 (mapcar #'(lambda (a b
)
1587 (let ((foo (funcall fn1
(car a
) b
(deg b
))))
1588 (if foo
(m// foo
(cadr a
))
1589 (return-from zmtorat nil
))))
1592 (return (cond (c (m// n c
))
1596 (setq n
(funcall fn1 n d s
))
1597 (return (when n
(sratsimp (cond (c (m// n c
))
1600 (defun pfrnum (f g n n2 var
)
1601 (let ((varlist (list var
)) genvar
)
1602 (setq f
(polyform f
)
1606 (setq var
(caadr (ratrep* var
)))
1607 (setq f
(resprog0 f g n n2
))
1608 (list (list (pdis (cadr f
)) (pdis (cddr f
)))
1609 (list (pdis (caar f
)) (pdis (cdar f
))))))
1614 (setq f
(ratrep* e
))
1615 (and (equal (cddr f
) 1)
1617 (and (equal (length (setq d
(cddr f
))) 3)
1620 (return (list (car d
)
1622 (ptimes (cadr f
) (caddr d
)))))
1623 (merror "defint: bug from PFRNUM in RESIDU.")))
1625 (defun partnum (n dl
)
1626 (let ((n2 1) ans nl
)
1627 (do ((dl dl
(cdr dl
)))
1629 (nconc ans
(ncons (list n n2
))))
1630 (setq nl
(pfrnum (car dl
) (m*l
(cdr dl
)) n n2 var
))
1631 (setq ans
(nconc ans
(ncons (car nl
))))
1632 (setq n2
(cadadr nl
) n
(caadr nl
) nl nil
))))
1635 (prog (poly expo
*mtoinf
* mb varlist genvar l c gvar
)
1636 (setq varlist
(list var
))
1638 (cond ((and (setq expo
(%einvolve e
))
1639 (polyp (setq poly
(sratsimp (m// e
(m^t
'$%e expo
)))))
1640 (setq l
(catch 'ggrm
(ggr (m^t
'$%e expo
) nil
))))
1642 (setq mb
(m- (subin 0.
(cadr l
))))
1643 (setq poly
(m+ (subin (m+t mb var
) poly
)
1644 (subin (m+t mb
(m*t -
1 var
)) poly
))))
1646 (setq expo
(caddr l
)
1651 (setq poly
(cdr (ratrep* poly
)))
1652 (setq mb
(m^
(pdis (cdr poly
)) -
1)
1654 (setq gvar
(caadr (ratrep* var
)))
1655 (cond ((or (atom poly
)
1656 (pointergp gvar
(car poly
)))
1657 (setq poly
(list 0. poly
)))
1658 (t (setq poly
(cdr poly
))))
1659 (return (do ((poly poly
(cddr poly
)))
1661 (mul* (m^t
'$%e c
) (m^t expo -
1) mb
(m+l e
)))
1662 (setq e
(cons (ggrm1 (car poly
) (pdis (cadr poly
)) l expo
)
1665 (defun ggrm1 (d k a b
)
1666 (setq b
(m// (m+t
1. d
) b
))
1667 (m* k
`((%gamma
) ,b
) (m^ a
(m- b
))))
1669 ;; Compute the integral(n/d,x,0,inf) by computing the negative of the
1670 ;; sum of residues of log(-x)*n/d over the poles of n/d inside the
1671 ;; keyhole contour. This contour is basically an disk with a slit
1672 ;; along the positive real axis. n/d must be a rational function.
1673 (defun keyhole (n d var
)
1674 (let* ((*semirat
* ())
1677 ;; Ok if not on the positive real axis.
1678 (or (not (equal ($imagpart j
) 0))
1679 (eq ($asksign j
) '$neg
)))
1681 (cond ((eq ($asksign j
) '$pos
)
1686 ($rectform
($multthru
(m+ (cond ((car res
)
1693 ;; Look at an expression e of the form sin(r*x)^k, where k is an
1694 ;; integer. Return the list (1 r k). (Not sure if the first element
1695 ;; of the list is always 1 because I'm not sure what partition is
1696 ;; trying to do here.)
1699 (cond ((atom e
) (return nil
)))
1700 (setq e
(partition e var
1))
1703 (cond ((setq r
(sinrx e
))
1704 (return (list m r
1)))
1706 (eq (ask-integer (setq k
(caddr e
)) '$integer
) '$yes
)
1707 (setq r
(sinrx (cadr e
))))
1708 (return (list m r k
))))))
1710 ;; Look at an expression e of the form sin(r*x) and return r.
1712 (cond ((and (consp e
) (eq (caar e
) '%sin
))
1713 (cond ((eq (cadr e
) var
)
1715 ((and (setq e
(partition (cadr e
) var
1))
1721 ;; integrate(a*sc(r*x)^k/x^n,x,0,inf).
1724 ;; Get the argument of the involved trig function.
1725 (when (null (setq arg
(involve exp
'(%sin %cos
))))
1727 ;; I don't think this needs to be special.
1729 (declare (special n
))
1730 ;; Replace (1-cos(arg)^2) with sin(arg)^2.
1731 (setq exp
($substitute
;(m^t `((%sin) ,var) 2.)
1732 ;(m+t 1. (m- (m^t `((%cos) ,var) 2.)))
1733 ;; The code from above generates expressions with
1734 ;; a missing simp flag. Furthermore, the
1735 ;; substitution has to be done for the complete
1736 ;; argument of the trig function. (DK 02/2010)
1737 `((mexpt simp
) ((%sin simp
) ,arg
) 2)
1738 `((mplus) 1 ((mtimes) -
1 ((mexpt) ((%cos
) ,arg
) 2)))
1742 (cond ((and (setq n
(findp dn
*))
1743 (eq (ask-integer n
'$integer
) '$yes
))
1744 ;; n is the power of the denominator.
1745 (cond ((setq c
(skr u
))
1747 (return (scmp c n
)))
1749 (setq c
(andmapcar #'skr
(cdr u
))))
1750 ;; Do this for a sum of such terms.
1751 (return (m+l
(mapcar #'(lambda (j) (scmp j n
))
1754 ;; We have an integral of the form sin(r*x)^k/x^n. C is the list (1 r k).
1756 ;; The substitution y=r*x converts this integral to
1758 ;; r^(n-1)*integral(sin(y)^k/y^n,y,0,inf)
1760 ;; (If r is negative, we need to negate the result.)
1762 ;; The recursion Wang gives on p. 87 has a typo. The second term
1763 ;; should be subtracted from the first. This formula is given in G&R,
1764 ;; 3.82, formula 12.
1766 ;; integrate(sin(x)^r/x^s,x) =
1767 ;; r*(r-1)/(s-1)/(s-2)*integrate(sin(x)^(r-2)/x^(s-2),x)
1768 ;; - r^2/(s-1)/(s-2)*integrate(sin(x)^r/x^(s-2),x)
1770 ;; (Limits are assumed to be 0 to inf.)
1772 ;; This recursion ends up with integrals with s = 1 or 2 and
1774 ;; integrate(sin(x)^p/x,x,0,inf) = integrate(sin(x)^(p-1),x,0,%pi/2)
1776 ;; with p > 0 and odd. This latter integral is known to maxima, and
1777 ;; it's value is beta(p/2,1/2)/2.
1779 ;; integrate(sin(x)^2/x^2,x,0,inf) = %pi/2*binomial(q-3/2,q-1)
1784 ;; Compute sign(r)*r^(n-1)*integrate(sin(y)^k/y^n,y,0,inf)
1785 (destructuring-bind (mult r k
)
1787 (let ((recursion (sinsp k n
)))
1793 ;; Recursion failed. Return the integrand
1794 ;; The following code generates expressions with a missing simp flag
1795 ;; for the sin function. Use better simplifying code. (DK 02/2010)
1796 ; (let ((integrand (div (pow `((%sin) ,(m* r var))
1799 (let ((integrand (div (power (take '(%sin
) (mul r var
))
1803 `((%integrate
) ,integrand
,var
,ll
,ul
)))))))
1805 ;; integrate(sin(x)^n/x^2,x,0,inf) = pi/2*binomial(n-3/2,n-1).
1806 ;; Express in terms of Gamma functions, though.
1808 (m* half%pi
($makegamma
`((%binomial
) ,(m+t
(m+ n -
1) '((rat) -
1 2))
1812 ;; integrate(sin(x)^n/x,x,0,inf) = beta((n+1)/2,1/2)/2, for n odd and
1817 (t (bygamma (m+ n -
1) 0.
))))
1819 ;; This implements the recursion for computing
1820 ;; integrate(sin(y)^l/y^k,y,0,inf). (Note the change in notation from
1825 (cond ((eq ($sign
(m+ l
(m- (m+ k -
1))))
1827 ;; Integral diverges if l-(k-1) < 0.
1829 ((not (even1 (m+ l k
)))
1830 ;; If l + k is not even, return NIL. (Is this the right
1834 ;; We have integrate(sin(y)^l/y^2). Use sevn to evaluate.
1837 ;; We have integrate(sin(y)^l/y,y)
1839 ((eq ($sign
(m+ k -
2.
))
1841 (setq i
(m* (m+ k -
1)
1842 (setq j
(m+ k -
2.
))))
1843 ;; j = k-2, i = (k-1)*(k-2)
1846 ;; The main recursion:
1849 ;; = l*(l-1)/(k-1)/(k-2)*i(sin(y)^(l-2)/y^k)
1850 ;; - l^2/(k-1)/(k-1)*i(sin(y)^l/y^(k-2))
1853 (sinsp (m+ l -
2.
) j
))
1858 ;; Returns the fractional part of a?
1862 ;; Why do we return 0 if a is a number? Perhaps we really
1866 ;; If we're here, this basically assumes a is a rational.
1867 ;; Compute the remainder and return the result.
1868 (list (car a
) (rem (cadr a
) (caddr a
)) (caddr a
)))
1869 ((and (atom a
) (abless1 a
)) a
)
1872 (abless1 (caddr a
)))
1876 (cond ((polyinx e var nil
) 0.
)
1882 (m+l
(mapcar #'thrad e
)))))
1885 ;;; THE FOLLOWING FUNCTION IS FOR TRIG FUNCTIONS OF THE FOLLOWING TYPE:
1886 ;;; LOWER LIMIT=0 B A MULTIPLE OF %PI SCA FUNCTION OF SIN (X) COS (X)
1889 (defun period (p e var
)
1890 (and (alike1 (no-err-sub var e
) (setq e
(no-err-sub (m+ p var
) e
)))
1891 ;; means there was no error
1894 ; returns cons of (integer_part . fractional_part) of a
1896 ;; I think we really want to compute how many full periods are in a
1897 ;; and the remainder.
1898 (let* ((q (igprt (div a
(mul 2 '$%pi
))))
1899 (r (add a
(mul -
1 (mul q
2 '$%pi
)))))
1902 ; returns cons of (integer_part . fractional_part) of a
1903 (defun lower-infr (a)
1904 ;; I think we really want to compute how many full periods are in a
1905 ;; and the remainder.
1906 (let* (;(q (igprt (div a (mul 2 '$%pi))))
1907 (q (mfuncall '$ceiling
(div a
(mul 2 '$%pi
))))
1908 (r (add a
(mul -
1 (mul q
2 '$%pi
)))))
1912 ;; Return the integer part of r.
1915 (mfuncall '$floor r
))
1918 ;;;Try making exp(%i*var) --> yy, if result is rational then do integral
1919 ;;;around unit circle. Make corrections for limits of integration if possible.
1921 (let* ((exp-form (sconvert sc
)) ;Exponentialize
1922 (rat-form (maxima-substitute 'yy
(m^t
'$%e
(m*t
'$%i var
))
1923 exp-form
))) ;Try to make Rational fun.
1924 (cond ((and (ratp rat-form
'yy
)
1925 (not (among var rat-form
)))
1926 (cond ((alike1 b %pi2
)
1927 (let ((ans (zto%pi2 rat-form
'yy
)))
1931 (evenfn exp-form var
))
1932 (let ((ans (zto%pi2 rat-form
'yy
)))
1933 (cond (ans (m*t
'((rat) 1.
2.
) ans
))
1935 ((and (alike1 b half%pi
)
1936 (evenfn exp-form var
)
1938 (no-err-sub (m+t
'$%pi
(m*t -
1 var
))
1940 (let ((ans (zto%pi2 rat-form
'yy
)))
1941 (cond (ans (m*t
'((rat) 1.
4.
) ans
))
1944 ;;; Do integrals of sin and cos. this routine makes sure lower limit
1946 (defun intsc1 (a b e
)
1947 ;; integrate(e,var,a,b)
1948 (let ((trigarg (find-first-trigarg e
))
1952 (*sin-cos-recur
* t
)) ;recursion stopper
1953 (prog (ans d nzp2 l int-zero-to-d int-nzp2 int-zero-to-c limit-diff
)
1954 (let* ((arg (simple-trig-arg trigarg
)) ;; pattern match sin(cc*x + bb)
1957 (new-var (gensym "NEW-VAR-")))
1959 (not (every-trigarg-alike e trigarg
)))
1961 (when (not (and (equal cc
1) (equal bb
0)))
1962 (setq e
(div (maxima-substitute (div (sub new-var bb
) cc
)
1965 (setq var new-var
) ;; change of variables to get sin(new-var)
1966 (setq a
(add bb
(mul a cc
)))
1967 (setq b
(add bb
(mul b cc
)))))
1968 (setq limit-diff
(m+ b
(m* -
1 a
)))
1969 (when (or (not (period %pi2 e var
))
1970 (member a infinities
)
1971 (member b infinities
)
1972 (not (and ($constantp a
)
1974 ;; Exit if b or a is not a constant or if the integrand
1975 ;; doesn't appear to have a period of 2 pi.
1978 ;; Multiples of 2*%pi in limits.
1979 (cond ((integerp (setq d
(let (($float nil
))
1980 (m// limit-diff %pi2
))))
1981 (cond ((setq ans
(intsc e %pi2 var
))
1982 (return (m* d ans
)))
1985 ;; The integral is not over a full period (2*%pi) or multiple
1986 ;; of a full period.
1988 ;; Wang p. 111: The integral integrate(f(x),x,a,b) can be
1991 ;; n * integrate(f,x,0,2*%pi) + integrate(f,x,0,c)
1992 ;; - integrate(f,x,0,d)
1994 ;; for some integer n and d >= 0, c < 2*%pi because there exist
1995 ;; integers p and q such that a = 2 * p *%pi + d and b = 2 * q
1996 ;; * %pi + c. Then n = q - p.
1998 ;; Compute q and c for the upper limit b.
2003 (setq int-zero-to-d
0.
)
2005 ;; Compute p and d for the lower limit a.
2007 ;; avoid an extra trip around the circle - helps skip principal values
2008 (if (ratgreaterp (car b
) (car l
)) ; if q > p
2009 (setq l
(cons (add 1 (car l
)) ; p += 1
2010 (add (mul -
1 %pi2
) (cdr l
))))) ; d -= 2*%pi
2012 ;; Compute -integrate(f,x,0,d)
2014 (cond ((setq ans
(try-intsc e
(cdr l
) var
))
2017 ;; Compute n = q - p (stored in nzp2)
2018 (setq nzp2
(m+ (car b
) (m- (car l
))))
2020 ;; Compute n*integrate(f,x,0,2*%pi)
2021 (setq int-nzp2
(cond ((zerop1 nzp2
)
2024 ((setq ans
(try-intsc e %pi2 var
))
2025 ;; n is not zero, so compute
2026 ;; integrate(f,x,0,2*%pi)
2028 ;; Unable to compute integrate(f,x,0,2*%pi)
2030 ;; Compute integrate(f,x,0,c)
2031 (setq int-zero-to-c
(try-intsc e
(cdr b
) var
))
2033 (return (cond ((and int-zero-to-d int-nzp2 int-zero-to-c
)
2034 ;; All three pieces succeeded.
2035 (add* int-zero-to-d int-nzp2 int-zero-to-c
))
2036 ((ratgreaterp %pi2 limit-diff
)
2037 ;; Less than 1 full period, so intsc can integrate it.
2038 ;; Apply the substitution to make the lower limit 0.
2039 ;; This is last resort because substitution often causes intsc to fail.
2040 (intsc (maxima-substitute (m+ a var
) var e
)
2045 ;; integrate(sc, var, 0, b), where sc is f(sin(x), cos(x)).
2046 ;; calls intsc with a wrapper to just return nil if integral is divergent,
2047 ;; rather than generating an error.
2048 (defun try-intsc (sc b var
)
2049 (let* ((*nodiverg t
)
2050 (ans (catch 'divergent
(intsc sc b var
))))
2051 (if (eq ans
'divergent
)
2055 ;; integrate(sc, var, 0, b), where sc is f(sin(x), cos(x)). I (rtoy)
2056 ;; think this expects b to be less than 2*%pi.
2057 (defun intsc (sc b var
)
2060 (multiple-value-bind (b sc
)
2061 (cond ((eq ($sign b
) '$neg
)
2063 (m* -
1 (subin (m*t -
1 var
) sc
))))
2066 ;; Partition the integrand SC into the factors that do not
2067 ;; contain VAR (the car part) and the parts that do (the cdr
2069 (setq sc
(partition sc var
1))
2070 (cond ((setq b
(intsc0 (cdr sc
) b var
))
2071 (m* (resimplify (car sc
)) b
))))))
2073 ;; integrate(sc, var, 0, b), where sc is f(sin(x), cos(x)).
2074 (defun intsc0 (sc b var
)
2075 ;; Determine if sc is a product of sin's and cos's.
2076 (let ((nn* (scprod sc
))
2079 ;; We have a product of sin's and cos's. We handle some
2080 ;; special cases here.
2081 (cond ((alike1 b half%pi
)
2082 ;; Wang p. 110, formula (1):
2083 ;; integrate(sin(x)^m*cos(x)^n, x, 0, %pi/2) =
2084 ;; gamma((m+1)/2)*gamma((n+1)/2)/2/gamma((n+m+2)/2)
2085 (bygamma (car nn
*) (cadr nn
*)))
2087 ;; Wang p. 110, near the bottom, says
2089 ;; int(f(sin(x),cos(x)), x, 0, %pi) =
2090 ;; int(f(sin(x),cos(x)) + f(sin(x),-cos(x)),x,0,%pi/2)
2091 (cond ((eq (real-branch (cadr nn
*) -
1) '$yes
)
2092 (m* (m+ 1.
(m^ -
1 (cadr nn
*)))
2093 (bygamma (car nn
*) (cadr nn
*))))))
2095 (cond ((or (and (eq (ask-integer (car nn
*) '$even
)
2097 (eq (ask-integer (cadr nn
*) '$even
)
2099 (and (ratnump (car nn
*))
2100 (eq (real-branch (car nn
*) -
1)
2102 (ratnump (cadr nn
*))
2103 (eq (real-branch (cadr nn
*) -
1)
2105 (m* 4.
(bygamma (car nn
*) (cadr nn
*))))
2106 ((or (eq (ask-integer (car nn
*) '$odd
) '$yes
)
2107 (eq (ask-integer (cadr nn
*) '$odd
) '$yes
))
2110 ((alike1 b half%pi3
)
2111 ;; Wang, p. 111 says
2113 ;; int(f(sin(x),cos(x)),x,0,3*%pi/2) =
2114 ;; int(f(sin(x),cos(x)),x,0,%pi)
2115 ;; + int(f(-sin(x),-cos(x)),x,0,%pi/2)
2116 (m* (m+ 1.
(m^ -
1 (cadr nn
*)) (m^ -
1 (m+l nn
*)))
2117 (bygamma (car nn
*) (cadr nn
*))))))
2119 ;; We don't have a product of sin's and cos's.
2120 (cond ((and (or (eq b
'$%pi
)
2123 (setq dn
* (scrat sc b
)))
2125 ((setq nn
* (antideriv sc
))
2126 (sin-cos-intsubs nn
* var
0. b
))
2129 ;;;Is careful about substitution of limits where the denominator may be zero
2130 ;;;because of various assumptions made.
2131 (defun sin-cos-intsubs (exp var ll ul
)
2133 (let ((l (mapcar #'sin-cos-intsubs1
(cdr exp
))))
2134 (if (not (some #'null l
))
2136 (t (sin-cos-intsubs1 exp
))))
2138 (defun sin-cos-intsubs1 (exp)
2139 (let* ((rat-exp ($rat exp
))
2140 (denom (pdis (cddr rat-exp
))))
2141 (cond ((equal ($csign denom
) '$zero
)
2143 (t (try-intsubs exp ll ul
)))))
2145 (defun try-intsubs (exp ll ul
)
2146 (let* ((*nodiverg t
)
2147 (ans (catch 'divergent
(intsubs exp ll ul
))))
2148 (if (eq ans
'divergent
)
2152 (defun try-defint (exp var ll ul
)
2153 (let* ((*nodiverg t
)
2154 (ans (catch 'divergent
(defint exp var ll ul
))))
2155 (if (eq ans
'divergent
)
2159 ;; Determine whether E is of the form sin(x)^m*cos(x)^n and return the
2162 (let ((great-minus-1 #'(lambda (temp)
2163 (ratgreaterp temp -
1)))
2166 ((setq m
(powerofx e
`((%sin
) ,var
) great-minus-1 var
))
2168 ((setq n
(powerofx e
`((%cos
) ,var
) great-minus-1 var
))
2172 (or (setq m
(powerofx (cadr e
) `((%sin
) ,var
) great-minus-1 var
))
2173 (setq n
(powerofx (cadr e
) `((%cos
) ,var
) great-minus-1 var
)))
2176 (setq m
(powerofx (caddr e
) `((%sin
) ,var
) great-minus-1 var
)))
2177 (t (setq n
(powerofx (caddr e
) `((%cos
) ,var
) great-minus-1 var
))))
2182 (defun real-branch (exponent value
)
2183 ;; Says wether (m^t value exponent) has at least one real branch.
2184 ;; Only works for values of 1 and -1 now. Returns $yes $no
2186 (cond ((equal value
1.
)
2188 ((eq (ask-integer exponent
'$integer
) '$yes
)
2191 (cond ((eq ($oddp
(caddr exponent
)) t
)
2196 ;; Compute beta((m+1)/2,(n+1)/2)/2.
2197 (defun bygamma (m n
)
2198 (let ((one-half (m//t
1.
2.
)))
2199 (m* one-half
`(($beta
) ,(m* one-half
(m+t
1. m
))
2200 ,(m* one-half
(m+t
1. n
))))))
2202 ;;Seems like Guys who call this don't agree on what it should return.
2203 (defun powerofx (e x p var
)
2204 (setq e
(cond ((not (among var e
)) nil
)
2209 (not (among var
(caddr e
))))
2211 (cond ((null e
) nil
)
2215 ;; Check e for an expression of the form x^kk*(b*x^n+a)^l. If it
2216 ;; matches, Return the two values kk and (list l a n b).
2219 (cond ((atom e
) nil
)
2221 ;; We have f(x)^y. Look to see if f(x) has the desired
2222 ;; form. Then f(x)^y has the desired form too, with
2223 ;; suitably modified values.
2225 ;; XXX: Should we ask for the sign of f(x) if y is not an
2226 ;; integer? This transformation we're going to do requires
2227 ;; that f(x)^y be real.
2228 (destructuring-bind (mexp base power
)
2230 (declare (ignore mexp
))
2231 (multiple-value-bind (kk cc
)
2234 ;; Got a match. Adjust kk and cc appropriately.
2235 (destructuring-bind (l a n b
)
2237 (values (mul kk power
)
2238 (list (mul l power
) a n b
)))))))
2241 (or (and (setq k
(findp (cadr e
)))
2242 (setq c
(bxm (caddr e
) (polyinx (caddr e
) var nil
))))
2243 (and (setq k
(findp (caddr e
)))
2244 (setq c
(bxm (cadr e
) (polyinx (cadr e
) var nil
))))))
2246 ((setq c
(bxm e
(polyinx e var nil
)))
2253 ;; (COND ((NOT (BATA0 E)) (RETURN NIL))
2254 ;; ((AND (EQUAL -1. (CADDDR C))
2255 ;; (EQ ($askSIGN (SETQ K (m+ 1. K)))
2257 ;; (EQ ($askSIGN (SETQ L (m+ 1. (CAR C))))
2260 ;; (m^ UL (CADDR C)))
2261 ;; (SETQ E (CADR C))
2262 ;; (EQ ($askSIGN (SETQ C (CADDR C))) '$pos))
2263 ;; (RETURN (M// (m* (m^ UL (m+t K (m* C (m+t -1. L))))
2264 ;; `(($BETA) ,(SETQ NN* (M// K C))
2269 ;; Integrals of the form i(log(x)^m*x^k*(a+b*x^n)^l,x,0,ul). There
2270 ;; are two cases to consider: One case has ul>0, b<0, a=-b*ul^n, k>-1,
2271 ;; l>-1, n>0, m a nonnegative integer. The second case has ul=inf, l < 0.
2273 ;; These integrals are essentially partial derivatives of the Beta
2274 ;; function (i.e. the Eulerian integral of the first kind). Note
2275 ;; that, currently, with the default setting intanalysis:true, this
2276 ;; function might not even be called for some of these integrals.
2277 ;; However, this can be palliated by setting intanalysis:false.
2280 (when (or (mtimesp e
) (mexptp e
))
2282 (log (list '(%log
) var
)))
2285 (find-if #'(lambda (fac)
2286 (powerofx fac log
#'set-m var
))
2288 (when (and (freeof var m
)
2289 (eq (ask-integer m
'$integer
) '$yes
)
2290 (not (eq ($asksign m
) '$neg
)))
2291 (setq e
(m//t e
(list '(mexpt) log m
)))
2294 (multiple-value-bind (kk s d r cc
)
2296 ;; We have i(x^kk/(d+cc*x^r)^s,x,0,inf) =
2297 ;; beta(aa,bb)/(cc^aa*d^bb*r). Compute this, and then
2298 ;; differentiate it m times to get the log term
2301 (let* ((aa (div (add 1 var
) r
))
2303 (m (if (eq ($asksign m
) '$zero
)
2306 (let ((res (div `(($beta
) ,aa
,bb
)
2310 ($at
($diff res var m
)
2311 (list '(mequal) var kk
)))))))
2313 (multiple-value-bind
2314 (k/n l n b
) (batap-new e
)
2316 (let ((beta (simplify (list '($beta
) k
/n l
)))
2317 (m (if (eq ($asksign m
) '$zero
) 0 m
)))
2318 ;; The result looks like B(k/n,l) ( ... ).
2319 ;; Perhaps, we should just $factor, instead of
2320 ;; pulling out beta like this.
2326 (m^t
(m-t b
) (m1-t l
))
2327 (m^t ul
(m*t n
(m1-t l
)))
2328 (m^t n
(m-t (m1+t m
)))
2329 ($at
($diff
(m*t
(m^t ul
(m*t n var
))
2330 (list '($beta
) var l
))
2332 (list '(mequal) var k
/n
)))
2336 ;;; If e is of the form given below, make the obvious change
2337 ;;; of variables (substituting ul*x^(1/n) for x) in order to reduce
2338 ;;; e to the usual form of the integrand in the Eulerian
2339 ;;; integral of the first kind.
2340 ;;; N. B: The old version of ZTO1 completely ignored this
2341 ;;; substitution; the log(x)s were just thrown in, which,
2342 ;;; of course would give wrong results.
2344 (defun batap-new (e)
2346 (multiple-value-bind (k c
)
2349 ;; e=x^k*(a+b*x^n)^l
2350 (destructuring-bind (l a n b
)
2352 (when (and (freeof var k
)
2355 (alike1 a
(m-t (m*t b
(m^t ul n
))))
2356 (eq ($asksign b
) '$neg
)
2357 (eq ($asksign
(setq k
(m1+t k
))) '$pos
)
2358 (eq ($asksign
(setq l
(m1+t l
))) '$pos
)
2359 (eq ($asksign n
) '$pos
))
2360 (values (m//t k n
) l n b
))))))
2363 ;; Wang p. 71 gives the following formula for a beta function:
2365 ;; integrate(x^(k-1)/(c*x^r+d)^s,x,0,inf)
2366 ;; = beta(a,b)/(c^a*d^b*r)
2368 ;; where a = k/r > 0, b = s - a > 0, s > k > 0, r > 0, c*d > 0.
2370 ;; This function matches this and returns k-1, d, r, c, a, b. And
2371 ;; also checks that all the conditions hold. If not, NIL is returned.
2373 (defun batap-inf (e)
2374 (multiple-value-bind (k c
)
2377 (destructuring-bind (l d r cc
)
2379 (let* ((s (mul -
1 l
))
2383 (when (and (freeof var k
)
2386 (eq ($asksign kk
) '$pos
)
2387 (eq ($asksign a
) '$pos
)
2388 (eq ($asksign b
) '$pos
)
2389 (eq ($asksign
(sub s k
)) '$pos
)
2390 (eq ($asksign r
) '$pos
)
2391 (eq ($asksign
(mul cc d
)) '$pos
))
2392 (values k s d r cc
)))))))
2395 ;; Handles beta integrals.
2397 (cond ((not (or (equal ll
0)
2399 (setq e
(subin (m+ ll var
) e
))))
2400 (multiple-value-bind (k c
)
2405 (destructuring-bind (l d al c
)
2407 ;; e = x^k*(d+c*x^al)^l.
2408 (let ((new-k (m// (m+ 1 k
) al
)))
2409 (when (and (ratgreaterp al
0.
)
2410 (eq ($asksign new-k
) '$pos
)
2411 (ratgreaterp (setq l
(m* -
1 l
))
2413 (eq ($asksign
(m* d c
))
2415 (setq l
(m+ l
(m*t -
1 new-k
)))
2416 (m// `(($beta
) ,new-k
,l
)
2417 (mul* al
(m^ c new-k
) (m^ d l
))))))))))
2420 ;; Compute exp(d)*gamma((c+1)/b)/b/a^((c+1)/b). In essence, this is
2421 ;; the value of integrate(x^c*exp(d-a*x^b),x,0,inf).
2422 (defun gamma1 (c a b d
)
2424 (m^
(m* b
(m^ a
(setq c
(m// (m+t c
1) b
)))) -
1)
2427 (defun zto%pi2
(grand var
)
2428 (let ((result (unitcir (sratsimp (m// grand var
)) var
)))
2429 (cond (result (sratsimp (m* (m- '$%i
) result
)))
2432 ;; Evaluates the contour integral of GRAND around the unit circle
2434 (defun unitcir (grand var
)
2437 (result (princip (res nn
* dn
*
2439 ;; Is pt stricly inside the unit circle?
2440 (setq sgn
(let ((limitp nil
))
2441 ($asksign
(m+ -
1 (cabs pt
)))))
2444 (declare (ignore pt
))
2445 ;; Is pt on the unit circle? (Use
2446 ;; the cached value computed
2450 (setq sgn nil
)))))))
2452 (m* '$%pi result
))))
2455 (defun logx1 (exp ll ul
)
2458 ((and (notinvolve exp
'(%sin %cos %tan %atan %asin %acos
))
2459 (setq arg
(involve exp
'(%log
))))
2461 (cond ((ratgreaterp 1. ll
)
2462 (cond ((not (eq ul
'$inf
))
2463 (intcv1 (m^t
'$%e
(m- 'yx
)) (m- `((%log
) ,var
))))
2464 (t (intcv1 (m^t
'$%e
'yx
) `((%log
) ,var
)))))))
2465 (t (intcv arg nil
)))))))
2468 ;; Wang 81-83. Unfortunately, the pdf version has page 82 as all
2469 ;; black, so here is, as best as I can tell, what Wang is doing.
2470 ;; Fortunately, p. 81 has the necessary hints.
2472 ;; First consider integrate(exp(%i*k*x^n),x) around the closed contour
2473 ;; consisting of the real axis from 0 to R, the arc from the angle 0
2474 ;; to %pi/(2*n) and the ray from the arc back to the origin.
2476 ;; There are no poles in this region, so the integral must be zero.
2477 ;; But consider the integral on the three parts. The real axis is the
2478 ;; integral we want. The return ray is
2480 ;; exp(%i*%pi/2/n) * integrate(exp(%i*k*(t*exp(%i*%pi/2/n))^n),t,R,0)
2481 ;; = exp(%i*%pi/2/n) * integrate(exp(%i*k*t^n*exp(%i*%pi/2)),t,R,0)
2482 ;; = -exp(%i*%pi/2/n) * integrate(exp(-k*t^n),t,0,R)
2484 ;; As R -> infinity, this last integral is gamma(1/n)/k^(1/n)/n.
2486 ;; We assume the integral on the circular arc approaches 0 as R ->
2487 ;; infinity. (Need to prove this.)
2491 ;; integrate(exp(%i*k*t^n),t,0,inf)
2492 ;; = exp(%i*%pi/2/n) * gamma(1/n)/k^(1/n)/n.
2494 ;; Equating real and imaginary parts gives us the desired results:
2496 ;; integrate(cos(k*t^n),t,0,inf) = G * cos(%pi/2/n)
2497 ;; integrate(sin(k*t^n),t,0,inf) = G * sin(%pi/2/n)
2499 ;; where G = gamma(1/n)/k^(1/n)/n.
2503 (cond ((atom e
) nil
)
2504 ((and (or (eq (caar e
) '%sin
)
2505 (eq (caar e
) '%cos
))
2507 (setq e
(bx**n
(cadr e
))))
2508 ;; Ok, we have cos(b*x^n) or sin(b*x^n), and we set e = (n
2510 (cond ((equal (car e
) 1.
)
2511 ;; n = 1. Give up. (Why not divergent?)
2513 ((zerop (setq s
(let ((sign ($asksign
(cadr e
))))
2514 (cond ((eq sign
'$pos
) 1)
2515 ((eq sign
'$neg
) -
1)
2516 ((eq sign
'$zero
) 0)))))
2517 ;; s is the sign of b. Give up if it's zero.
2519 ((not (eq ($asksign
(m+ -
1 (car e
))) '$pos
))
2520 ;; Give up if n-1 <= 0. (Why give up? Isn't the
2521 ;; integral divergent?)
2524 ;; We can apply our formula now. g = gamma(1/n)/n/b^(1/n)
2525 (setq g
(gamma1 0.
(m* s
(cadr e
)) (car e
) 0.
))
2526 (setq e
(m* g
`((,ind
) ,(m// half%pi
(car e
)))))
2527 (m* (cond ((and (eq ind
'%sin
)
2534 ;; this is the second part of the definite integral package
2536 (declare-top(special var plm
* pl
* rl
* pl
*1 rl
*1))
2538 (defun p*lognxp
(a s
)
2540 (cond ((not (among '%log a
))
2542 ((and (polyinx (setq b
(maxima-substitute 1.
`((%log
) ,var
) a
))
2544 (eq ($sign
(m+ s
(m+ 1 (deg b
))))
2547 (setq a
(lognxp (sratsimp (m// a b
)))))
2551 (cond ((atom a
) nil
)
2552 ((and (eq (caar a
) '%log
)
2553 (eq (cadr a
) var
)) 1.
)
2559 (defun logcpi0 (n d
)
2561 (setq pl
(polelist d
#'upperhalf
#'(lambda (j)
2562 (cond ((zerop1 j
) nil
)
2563 ((equal ($imagpart j
) 0)
2567 (setq factors
(car pl
)
2569 (cond ((or (cadr pl
)
2571 (setq dp
(sdiff d var
))))
2572 (cond ((setq plm
* (car pl
))
2573 (setq rlm
* (residue n
(cond (leadcoef factors
)
2576 (cond ((setq pl
* (cadr pl
))
2577 (setq rl
* (res1 n dp pl
*))))
2578 (cond ((setq pl
*1 (caddr pl
))
2579 (setq rl
*1 (res1 n dp pl
*1))))
2580 (return (m*t
(m//t
1.
2.
)
2583 (list (cond ((setq nn
* (append rl
* rlm
*))
2585 (cond (rl*1 (m+l rl
*1))))))))))
2587 (defun lognx2 (nn dn pl rl
)
2588 (do ((pl pl
(cdr pl
))
2593 (setq ans
(cons (m* dn
(car rl
) (m^
`((%plog
) ,(car pl
)) nn
))
2596 (defun logcpj (n d i
)
2599 (list (mul* (m*t
'$%i %pi2
)
2601 (residue (m* (m^
`((%plog
) ,var
) i
) n
)
2604 (lognx2 i
(m*t
'$%i %pi2
) pl
* rl
*)
2605 (lognx2 i %p%i pl
*1 rl
*1)))
2608 (simplify (m+l n
))))
2610 ;; Handle integral(n(x)/d(x)*log(x)^m,x,0,inf). n and d are
2612 (defun log*rat
(n d m
)
2613 (declare (special *i
* *j
*))
2614 (setq *i
* (make-array (1+ m
)))
2615 (setq *j
* (make-array (1+ m
)))
2616 (setf (aref *j
* 0) 0)
2617 (prog (leadcoef factors plm
* pl
* rl
* pl
*1 rl
*1 rlm
*)
2618 (dotimes (c m
(return (logcpi n d m
)))
2619 (setf (aref *i
* c
) (logcpi n d c
))
2620 (setf (aref *j
* c
) (logcpj n factors c
)))))
2622 (defun logcpi (n d c
)
2623 (declare (special *j
*))
2626 (m* '((rat) 1 2) (m+ (aref *j
* c
) (m* -
1 (sumi c
))))))
2629 (declare (special *i
*))
2634 (push (mul* ($makegamma
`((%binomial
) ,c
,k
))
2640 (defun fan (p m a n b
)
2641 (let ((povern (m// p n
))
2644 ((or (eq (ask-integer povern
'$integer
) '$yes
)
2645 (not (equal ($imagpart ab
) 0))) ())
2646 (t (let ((ind ($asksign ab
)))
2647 (cond ((eq ind
'$zero
) nil
)
2648 ((eq ind
'$neg
) nil
)
2649 ((not (ratgreaterp m povern
)) nil
)
2651 ($makegamma
`((%binomial
) ,(m+ -
1 m
(m- povern
))
2653 `((mabs) ,(m^ a
(m+ povern
(m- m
)))))
2656 `((%sin
) ,(m*t
'$%pi povern
)))))))))))
2659 ;;Makes a new poly such that np(x)-np(x+2*%i*%pi)=p(x).
2660 ;;Constructs general POLY of degree one higher than P with
2661 ;;arbitrary coeff. and then solves for coeffs by equating like powers
2662 ;;of the varibale of integration.
2663 ;;Can probably be made simpler now.
2666 (let ((n (deg p
)) (ans ()) (varlist ()) (gp ()) (cl ()) (zz ()))
2667 (setq ans
(genpoly (m+ 1 n
))) ;Make poly with gensyms of 1 higher deg.
2668 (setq cl
(cdr ans
)) ;Coefficient list
2669 (setq varlist
(append cl
(list var
))) ;Make VAR most important.
2670 (setq gp
(car ans
)) ;This is the poly with gensym coeffs.
2671 ;;;Now, poly(x)-poly(x+2*%i*%pi)=p(x), P is the original poly.
2672 (setq ans
(m+ gp
(subin (m+t
(m*t
'$%i %pi2
) var
) (m- gp
)) (m- p
)))
2674 (setq ans
(ratrep* ans
)) ;Rational rep with VAR leading.
2675 (setq zz
(coefsolve n cl
(cond ((not (eq (caadr ans
) ;What is Lead Var.
2676 (genfind (car ans
) var
)))
2677 (list 0 (cadr ans
))) ;No VAR in ans.
2678 ((cdadr ans
))))) ;The real Poly.
2679 (if (or (null zz
) (null gp
))
2681 ($substitute zz gp
)))) ;Substitute Values for gensyms.
2683 (defun coefsolve (n cl e
)
2685 (eql (ncons (pdis (ptterm e n
))) (cons (pdis (ptterm e m
)) eql
))
2686 (m (m+ n -
1) (m+ m -
1)))
2687 ((signp l m
) (solvex eql cl nil nil
))))
2689 ;; Integrate(p(x)*f(exp(x))/g(exp(x)),x,minf,inf) by applying the
2690 ;; transformation y = exp(x) to get
2691 ;; integrate(p(log(y))*f(y)/g(y)/y,y,0,inf). This should be handled
2693 (defun log-transform (p pe d
)
2694 (let ((new-p (subst (list '(%log
) var
) var p
))
2695 (new-pe (subst var
'z
* (catch 'pin%ex
(pin%ex pe
))))
2696 (new-d (subst var
'z
* (catch 'pin%ex
(pin%ex d
)))))
2697 (defint (div (div (mul new-p new-pe
) new-d
) var
) var
0 ul
)))
2699 ;; This implements Wang's algorithm in Chapter 5.2, pp. 98-100.
2701 ;; This is a very brief description of the algorithm. Basically, we
2702 ;; have integrate(R(exp(x))*p(x),x,minf,inf), where R(x) is a rational
2703 ;; function and p(x) is a polynomial.
2705 ;; We find a polynomial q(x) such that q(x) - q(x+2*%i*%pi) = p(x).
2706 ;; Then consider a contour integral of R(exp(z))*q(z) over a
2707 ;; rectangular contour. Opposite corners of the rectangle are (-R,
2708 ;; 2*%i*%pi) and (R, 0).
2710 ;; Wang shows that this contour integral, in the limit, is the
2711 ;; integral of R(exp(x))*q(x)-R(exp(x))*q(x+2*%i*%pi), which is
2712 ;; exactly the integral we're looking for.
2714 ;; Thus, to find the value of the contour integral, we just need the
2715 ;; residues of R(exp(z))*q(z). The only tricky part is that we want
2716 ;; the log function to have an imaginary part between 0 and 2*%pi
2717 ;; instead of -%pi to %pi.
2718 (defun rectzto%pi2
(p pe d
)
2719 ;; We have R(exp(x))*p(x) represented as p(x)*pe(exp(x))/d(exp(x)).
2720 (prog (dp n pl a b c denom-exponential
)
2721 (if (not (and (setq denom-exponential
(catch 'pin%ex
(pin%ex d
)))
2722 (%e-integer-coeff pe
)
2723 (%e-integer-coeff d
)))
2725 ;; At this point denom-exponential has converted d(exp(x)) to the
2726 ;; polynomial d(z), where z = exp(x).
2727 (setq n
(m* (cond ((null p
) -
1)
2728 (t ($expand
(m*t
'$%i %pi2
(makpoly p
)))))
2732 ;; Find the poles of the denominator. denom-exponential is the
2733 ;; denominator of R(x).
2735 ;; It seems as if polelist returns a list of several items.
2736 ;; The first element is a list consisting of the pole and (z -
2737 ;; pole). We don't care about this, so we take the rest of the
2739 (setq pl
(cdr (polelist denom-exponential
2741 ;; The imaginary part is nonzero,
2742 ;; or the realpart is negative.
2743 (or (not (equal ($imagpart j
) 0))
2744 (eq ($asksign
($realpart j
)) '$neg
)))
2746 ;; The realpart is not zero.
2747 (not (eq ($asksign
($realpart j
)) '$zero
)))))))
2748 ;; Not sure what this does.
2750 ;; No roots at all, so return
2754 ;; We have simple roots or roots in REGION1
2755 (setq dp
(sdiff d var
))))
2757 ;; The cadr of pl is the list of the simple poles of
2758 ;; denom-exponential. Take the log of them to find the
2759 ;; poles of the original expression. Then compute the
2760 ;; residues at each of these poles and sum them up and put
2761 ;; the result in B. (If no simple poles set B to 0.)
2762 (setq b
(mapcar #'log-imag-0-2%pi
(cadr pl
)))
2763 (setq b
(res1 n dp b
))
2767 ;; I think this handles the case of poles outside the
2768 ;; regions. The sum of these residues are placed in C.
2769 (let ((temp (mapcar #'log-imag-0-2%pi
(caddr pl
))))
2770 (setq c
(append temp
(mapcar #'(lambda (j)
2771 (m+ (m*t
'$%i %pi2
) j
))
2773 (setq c
(res1 n dp c
))
2777 ;; We have the repeated poles of deonom-exponential, so we
2778 ;; need to convert them to the actual pole values for
2779 ;; R(exp(x)), by taking the log of the value of poles.
2780 (let ((poles (mapcar #'(lambda (p)
2781 (log-imag-0-2%pi
(car p
)))
2783 (exp (m// n
(subst (m^t
'$%e var
) 'z
* denom-exponential
))))
2784 ;; Compute the residues at all of these poles and sum
2786 (setq a
(mapcar #'(lambda (j)
2787 ($residue exp var j
))
2791 (return (sratsimp (m+ a b
(m* '((rat) 1.
2.
) c
))))))
2794 (do ((i i
(m+ i -
1))
2795 (c (gensym) (gensym))
2799 (cons (m+l ans
) cl
))
2800 (setq ans
(cons (m* c
(m^t var i
)) ans
))
2801 (setq cl
(cons c cl
))))
2803 ;; Check to see if each term in exp that is of the form exp(k*x) has
2804 ;; an integer value for k.
2805 (defun %e-integer-coeff
(exp)
2806 (cond ((mapatom exp
) t
)
2808 (eq (cadr exp
) '$%e
))
2809 (eq (ask-integer ($coeff
(caddr exp
) var
) '$integer
)
2811 (t (every '%e-integer-coeff
(cdr exp
)))))
2813 (defun wlinearpoly (e var
)
2814 (cond ((and (setq e
(polyinx e var t
))
2818 ;; Test to see if exp is of the form f(exp(x)), and if so, replace
2819 ;; exp(x) with 'z*. That is, basically return f(z*).
2821 (declare (special $exponentialize
))
2822 (pin%ex0
(cond ((notinvolve exp
'(%sinh %cosh %tanh
))
2825 (let (($exponentialize t
))
2826 (setq exp
($expand exp
)))))))
2829 ;; Does e really need to be special here? Seems to be ok without
2830 ;; it; testsuite works.
2832 (declare (special e
))
2833 (cond ((not (among var e
))
2836 (throw 'pin%ex nil
))
2839 (cond ((eq (caddr e
) var
)
2841 ((let ((linterm (wlinearpoly (caddr e
) var
)))
2843 (m* (subin 0 e
) (m^t
'z
* linterm
)))))
2845 (throw 'pin%ex nil
))))
2847 (m*l
(mapcar #'pin%ex0
(cdr e
))))
2849 (m+l
(mapcar #'pin%ex0
(cdr e
))))
2851 (throw 'pin%ex nil
))))
2853 ;; Test to see if exp is of the form p(x)*f(exp(x)). If so, set p* to
2854 ;; be p(x) and set pe* to f(exp(x)).
2855 (defun p*pin%ex
(nd*)
2856 (setq nd
* ($factor nd
*))
2857 (cond ((polyinx nd
* var nil
)
2858 (setq p
* (cons nd
* p
*)) t
)
2859 ((catch 'pin%ex
(pin%ex nd
*))
2860 (setq pe
* (cons nd
* pe
*)) t
)
2862 (andmapcar #'p
*pin%ex
(cdr nd
*)))))
2865 (cond ((findp p
) nil
)
2866 ((setq nd
* (bx**n p
))
2867 (m^t var
(car nd
*)))
2868 ((setq p
(bx**n
+a p
))
2869 (m* (caddr p
) (m^t var
(cadr p
))))))
2871 ;; I think this is looking at f(exp(x)) and tries to find some
2872 ;; rational function R and some number k such that f(exp(x)) =
2874 (defun funclogor%e
(e)
2875 (prog (ans arg nvar r
)
2876 (cond ((or (ratp e var
)
2877 (involve e
'(%sin %cos %tan
))
2878 (not (setq arg
(xor (and (setq arg
(involve e
'(%log
)))
2882 ag
(setq nvar
(cond ((eq r
'%log
) `((%log
) ,arg
))
2883 (t (m^t
'$%e arg
))))
2884 (setq ans
(maxima-substitute (m^t
'yx -
1) (m^t nvar -
1) (maxima-substitute 'yx nvar e
)))
2885 (cond ((not (among var ans
)) (return (list (subst var
'yx ans
) nvar
)))
2887 (setq arg
(findsub arg
)))
2890 ;; Integration by parts.
2892 ;; integrate(u(x)*diff(v(x),x),x,a,b)
2894 ;; = u(x)*v(x)| - integrate(v(x)*diff(u(x),x))
2897 (defun dintbypart (u v a b
)
2898 ;;;SINCE ONLY CALLED FROM DINTLOG TO get RID OF LOGS - IF LOG REMAINS, QUIT
2899 (let ((ad (antideriv v
)))
2900 (cond ((or (null ad
)
2901 (involve ad
'(%log
)))
2903 (t (let ((p1 (m* u ad
))
2904 (p2 (m* ad
(sdiff u var
))))
2905 (let ((p1-part1 (get-limit p1 var b
'$minus
))
2906 (p1-part2 (get-limit p1 var a
'$plus
)))
2907 (cond ((or (null p1-part1
)
2910 (t (let ((p2 (let ((*def2
* t
))
2911 (defint p2 var a b
))))
2912 (cond (p2 (add* p1-part1
2917 ;; integrate(f(exp(k*x)),x,a,b), where f(z) is rational.
2919 ;; See Wang p. 96-97.
2921 ;; If the limits are minf to inf, we use the substitution y=exp(k*x)
2922 ;; to get integrate(f(y)/y,y,0,inf)/k. If the limits are 0 to inf,
2923 ;; use the substitution s+1=exp(k*x) to get
2924 ;; integrate(f(s+1)/(s+1),s,0,inf).
2925 (defun dintexp (exp ignored
&aux ans
)
2926 (declare (ignore ignored
))
2927 (let ((*dintexp-recur
* t
)) ;recursion stopper
2928 (cond ((and (sinintp exp var
) ;To be moved higher in the code.
2929 (setq ans
(antideriv exp
))
2930 (setq ans
(intsubs ans ll ul
)))
2931 ;; If we can integrate it directly, do so and take the
2932 ;; appropriate limits.
2934 ((setq ans
(funclogor%e exp
))
2935 ;; ans is the list (f(x) exp(k*x)).
2936 (cond ((and (equal ll
0.
)
2938 ;; Use the substitution s + 1 = exp(k*x). The
2939 ;; integral becomes integrate(f(s+1)/(s+1),s,0,inf)
2940 (setq ans
(m+t -
1 (cadr ans
))))
2942 ;; Use the substitution y=exp(k*x) because the
2943 ;; limits are minf to inf.
2944 (setq ans
(cadr ans
))))
2945 ;; Apply the substitution and integrate it.
2948 ;; integrate(log(g(x))*f(x),x,0,inf)
2949 (defun dintlog (exp arg
)
2950 (let ((*dintlog-recur
* (1+ *dintlog-recur
*))) ;recursion stopper
2952 (cond ((and (eq ul
'$inf
)
2955 (equal 1 (sratsimp (m// exp
(m* (m- (subin (m^t var -
1)
2958 ;; Make the substitution y=1/x. If the integrand has
2959 ;; exactly the same form, the answer has to be 0.
2961 ((and (setq ans
(let (($gamma_expand t
)) (logx1 exp ll ul
)))
2964 ((setq ans
(antideriv exp
))
2965 ;; It's easy if we have the antiderivative.
2966 ;; but intsubs sometimes gives results containing %limit
2967 (return (intsubs ans ll ul
))))
2968 ;; Ok, the easy cases didn't work. We now try integration by
2969 ;; parts. Set ANS to f(x).
2970 (setq ans
(m// exp
`((%log
) ,arg
)))
2971 (cond ((involve ans
'(%log
))
2972 ;; Bad. f(x) contains a log term, so we give up.
2975 (equal 0.
(no-err-sub 0. ans
))
2976 (setq d
(let ((*def2
* t
))
2977 (defint (m* ans
(m^t var
'*z
*))
2979 ;; The arg of the log function is the same as the
2980 ;; integration variable. We can do something a little
2981 ;; simpler than integration by parts. We have something
2982 ;; like f(x)*log(x). Consider f(x)*x^z. If we
2983 ;; differentiate this wrt to z, the integrand becomes
2984 ;; f(x)*log(x)*x^z. When we evaluate this at z = 0, we
2985 ;; get the desired integrand.
2987 ;; So we need f(0) to be 0 at 0. If we can integrate
2988 ;; f(x)*x^z, then we differentiate the result and
2989 ;; evaluate it at z = 0.
2990 (return (derivat '*z
* 1. d
0.
)))
2991 ((setq ans
(dintbypart `((%log
) ,arg
) ans ll ul
))
2992 ;; Try integration by parts.
2995 ;; Compute diff(e,var,n) at the point pt.
2996 (defun derivat (var n e pt
)
2997 (subin pt
(apply '$diff
(list e var n
))))
3001 ;; MAYBPC returns (COEF EXPO CONST)
3003 ;; This basically picks off b*x^n+a and returns the list
3004 ;; (b n a). It may also set the global *zd*.
3005 (defun maybpc (e var
)
3006 (declare (special *zd
*))
3007 (cond (*mtoinf
* (throw 'ggrm
(linpower0 e var
)))
3008 ((and (not *mtoinf
*)
3009 (null (setq e
(bx**n
+a e
)))) ;bx**n+a --> (a n b) or nil.
3010 nil
) ;with var being x.
3011 ;; At this point, e is of the form (a n b)
3012 ((and (among '$%i
(caddr e
))
3013 (zerop1 ($realpart
(caddr e
)))
3014 (setq zn
($imagpart
(caddr e
)))
3015 (eq ($asksign
(cadr e
)) '$pos
))
3016 ;; If we're here, b is complex, and n > 0. zn = imagpart(b).
3018 ;; Set var to the same sign as zn.
3019 (cond ((eq ($asksign zn
) '$neg
)
3023 ;; zd = exp(var*%i*%pi*(1+nd)/(2*n). (ZD is special!)
3024 (setq *zd
* (m^t
'$%e
(m// (mul* var
'$%i
'$%pi
(m+t
1 nd
*))
3027 `(,(caddr e
) ,(cadr e
) ,(car e
)))
3028 ((and (or (eq (setq var
($asksign
($realpart
(caddr e
)))) '$neg
)
3030 (equal ($imagpart
(cadr e
)) 0)
3031 (ratgreaterp (cadr e
) 0.
))
3032 ;; We're here if realpart(b) <= 0, and n >= 0. Then return -b, n, a.
3033 `(,(caddr e
) ,(cadr e
) ,(car e
)))))
3035 ;; Integrate x^m*exp(b*x^n+a), with realpart(m) > -1.
3037 ;; See Wang, pp. 84-85.
3039 ;; I believe the formula Wang gives is incorrect. The derivation is
3040 ;; correct except for the last step.
3042 ;; Let J = integrate(x^m*exp(%i*k*x^n),x,0,inf), with real k.
3044 ;; Consider the case for k < 0. Take a sector of a circle bounded by
3045 ;; the real line and the angle -%pi/(2*n), and by the radii, r and R.
3046 ;; Since there are no poles inside this contour, the integral
3048 ;; integrate(z^m*exp(%i*k*z^n),z) = 0
3050 ;; Then J = exp(-%pi*%i*(m+1)/(2*n))*integrate(R^m*exp(k*R^n),R,0,inf)
3052 ;; because the integral along the boundary is zero except for the part
3053 ;; on the real axis. (Proof?)
3055 ;; Wang seems to say this last integral is gamma(s/n/(-k)^s) where s =
3056 ;; (m+1)/n. But that seems wrong. If we use the substitution R =
3057 ;; (y/(-k))^(1/n), we end up with the result:
3059 ;; integrate(y^((m+1)/n-1)*exp(-y),y,0,inf)/(n*k^((m+1)/n).
3061 ;; or gamma((m+1)/n)/k^((m+1)/n)/n.
3063 ;; Note that this also handles the case of
3065 ;; integrate(x^m*exp(-k*x^n),x,0,inf);
3067 ;; where k is positive real number. A simple change of variables,
3070 ;; integrate(y^((m+1)/n-1)*exp(-y),y,0,inf)/(n*k^((m+1)/n))
3072 ;; which is the same form above.
3074 (prog (c *zd
* zn nn
* dn
* nd
* dosimp $%emode
)
3075 (declare (special *zd
*))
3077 (cond (ind (setq e
($expand e
))
3078 (cond ((and (mplusp e
)
3079 (let ((*nodiverg t
))
3080 (setq e
(catch 'divergent
3085 (cond ((eq e
'divergent
) nil
)
3086 (t (return (sratsimp (cons '(mplus) e
)))))))))
3087 (setq e
(rmconst1 e
))
3090 (cond ((setq e
(ggr1 e var
))
3091 ;; e = (m b n a). That is, the integral is of the form
3092 ;; x^m*exp(b*x^n+a). I think we want to compute
3093 ;; gamma((m+1)/n)/b^((m+1)/n)/n.
3095 ;; FIXME: If n > m + 1, the integral converges. We need
3096 ;; to check for this.
3097 (destructuring-bind (m b n a
)
3099 (when (and (not (zerop1 ($realpart b
)))
3100 (not (zerop1 ($imagpart b
))))
3101 ;; The derivation only holds if b is purely real or
3102 ;; purely imaginary. Give up if it's not.
3104 ;; Check for convergence. If b is complex, we need n -
3105 ;; m > 1. If b is real, we need b < 0.
3106 (when (and (zerop1 ($imagpart b
))
3107 (not (eq ($asksign b
) '$neg
)))
3109 (when (and (not (zerop1 ($imagpart b
)))
3110 (not (eq ($asksign
(sub n
(add m
1))) '$pos
)))
3113 (setq e
(gamma1 m
(cond ((zerop1 ($imagpart b
))
3114 ;; If we're here, b must be negative.
3117 ;; Complex b. Take the imaginary part
3118 `((mabs) ,($imagpart b
))))
3120 ;; NOTE: *zd* (Ick!) is special and might be set by maybpc.
3122 ;; FIXME: Why do we set %emode here? Shouldn't we just
3123 ;; bind it? And why do we want it bound to T anyway?
3124 ;; Shouldn't the user control that? The same goes for
3128 (setq e
(m* *zd
* e
))))))
3129 (cond (e (return (m* c e
))))))
3132 ;; Match x^m*exp(b*x^n+a). If it does, return (list m b n a).
3134 (cond ((atom e
) nil
)
3137 ;; We're looking at something like exp(f(var)). See if it's
3138 ;; of the form b*x^n+a, and return (list 0 b n a). (The 0 is
3139 ;; so we can graft something onto it if needed.)
3140 (cond ((setq e
(maybpc (caddr e
) var
))
3143 ;; E should be the product of exactly 2 terms
3145 ;; Check to see if one of the terms is of the form
3146 ;; var^p. If so, make sure the realpart of p > -1. If
3147 ;; so, check the other term has the right form via
3148 ;; another call to ggr1.
3149 (or (and (setq dn
* (xtorterm (cadr e
) var
))
3150 (ratgreaterp (setq nd
* ($realpart dn
*))
3152 (setq nn
* (ggr1 (caddr e
) var
)))
3153 (and (setq dn
* (xtorterm (caddr e
) var
))
3154 (ratgreaterp (setq nd
* ($realpart dn
*))
3156 (setq nn
* (ggr1 (cadr e
) var
)))))
3157 ;; Both terms have the right form and nn* contains the arg of
3158 ;; the exponential term. Put dn* as the car of nn*. The
3159 ;; result is something like (m b n a) when we have the
3160 ;; expression x^m*exp(b*x^n+a).
3164 ;; Match b*x^n+a. If a match is found, return the list (a n b).
3165 ;; Otherwise, return NIL
3171 (t (let ((a (no-err-sub 0. e
)))
3173 (t (setq e
(m+ e
(m*t -
1 a
)))
3174 (cond ((setq e
(bx**n e
))
3178 ;; Match b*x^n. Return the list (n b) if found or NIL if not.
3181 (and (setq n
(xexponget e var
))
3183 (setq e
(let (($maxposex
1)
3185 ($expand
(m// e
(m^t var n
)))))))
3188 (defun xexponget (e nn
*)
3189 (cond ((atom e
) (cond ((eq e var
) 1.
)))
3193 (not (among nn
* (caddr e
))))
3195 (t (some #'(lambda (j) (xexponget j nn
*)) (cdr e
)))))
3198 ;;; given (b*x^n+a)^m returns (m a n b)
3203 (involve e
'(%log %sin %cos %tan
))
3206 ((mexptp e
) (cond ((among var
(caddr e
)) nil
)
3207 ((setq r
(bx**n
+a
(cadr e
)))
3208 (cons (caddr e
) r
))))
3209 ((setq r
(bx**n
+a e
)) (cons 1. r
))
3211 ;;;Catches Unfactored forms.
3212 (setq m
(m// (sdiff e var
) e
))
3217 ((and (setq r
(bx**n
+a
(sratsimp r
)))
3218 (not (among var
(setq m
(m// m
(m* (cadr r
) (caddr r
)
3219 (m^t var
(m+t -
1 (cadr r
))))))))
3220 (setq e
(m// (subin 0. e
) (m^t
(car r
) m
))))
3223 (t (setq e
(m^ e
(m// 1. m
)))
3224 (list m
(m* e
(car r
)) (cadr r
)
3225 (m* e
(caddr r
))))))))
3228 ;;;Is E = VAR raised to some power? If so return power or 0.
3230 (cond ((not (among var e
)) 0.
)
3231 (t (xtorterm e var
))))
3233 (defun xtorterm (e var1
)
3234 ;;;Is E = VAR1 raised to some power? If so return power.
3235 (cond ((alike1 e var1
) 1.
)
3238 (alike1 (cadr e
) var1
)
3239 (not (among var
(caddr e
))))
3243 (m^
(m* (m^
(caddr l
) '((rat) 1 2))
3244 (m+ (cadr l
) (m^
(m* (car l
) (caddr l
))
3248 (defun radbyterm (d l
)
3253 (destructuring-let (((const . integrand
) (rmconst1 (car l
))))
3254 (setq ans
(cons (m* const
(dintrad0 integrand d
))
3257 (defun sqdtc (e ind
)
3258 (prog (a b c varlist
)
3259 (setq varlist
(list var
))
3261 (setq e
(cdadr (ratrep* e
)))
3262 (setq c
(pdis (ptterm e
0)))
3263 (setq b
(m*t
(m//t
1 2) (pdis (ptterm e
1))))
3264 (setq a
(pdis (ptterm e
2)))
3265 (cond ((and (eq ($asksign
(m+ b
(m^
(m* a c
)
3269 (not (eq ($asksign a
) '$neg
))
3270 (eq ($asksign c
) '$pos
))
3271 (and (eq ($asksign a
) '$pos
)
3272 (not (eq ($asksign c
) '$neg
)))))
3273 (return (list a b c
))))))
3275 (defun difap1 (e pwr var m pt
)
3276 (m// (mul* (cond ((eq (ask-integer m
'$even
) '$yes
)
3280 (derivat var m e pt
))
3281 `((%gamma
) ,(m+ pwr m
))))
3283 (defun sqrtinvolve (e)
3284 (cond ((atom e
) nil
)
3287 (and (mnump (caddr e
))
3288 (not (numberp (caddr e
)))
3289 (equal (caddr (caddr e
)) 2.
))
3290 (among var
(cadr e
)))
3292 (t (some #'sqrtinvolve
(cdr e
)))))
3294 (defun bydif (r s d
)
3296 (setq d
(m+ (m*t
'*z
* var
) d
))
3297 (cond ((or (zerop1 (setq p
(m+ s
(m*t -
1 r
))))
3298 (and (zerop1 (m+ 1 p
))
3300 (difap1 (dintrad0 b
(m^ d
'((rat) 3 2)))
3301 '((rat) 3 2) '*z
* r
0))
3302 ((eq ($asksign p
) '$pos
)
3303 (difap1 (difap1 (dintrad0 1 (m^
(m+t
'z
** d
)
3305 '((rat) 3 2) '*z
* r
0)
3306 '((rat) 3 2) 'z
** p
0)))))
3308 (defun dintrad0 (n d
)
3310 (cond ((and (mexptp d
)
3311 (equal (deg (cadr d
)) 2.
))
3312 (cond ((alike1 (caddr d
) '((rat) 3.
2.
))
3313 (cond ((and (equal n
1.
)
3314 (setq l
(sqdtc (cadr d
) t
)))
3317 (setq l
(sqdtc (cadr d
) nil
)))
3318 (tbf (reverse l
)))))
3319 ((and (setq r
(findp n
))
3320 (or (eq ($asksign
(m+ -
1.
(m- r
) (m*t
2.
3324 (setq s
(m+ '((rat) -
3.
2.
) (caddr d
)))
3325 (eq ($asksign s
) '$pos
)
3326 (eq (ask-integer s
'$integer
) '$yes
))
3327 (bydif r s
(cadr d
)))
3328 ((polyinx n var nil
)
3329 (radbyterm d
(cdr n
))))))))
3332 ;;;Looks at the IMAGINARY part of a log and puts it in the interval 0 2*%pi.
3333 (defun log-imag-0-2%pi
(x)
3334 (let ((plog (simplify ($rectform
`((%plog
) ,x
)))))
3335 ;; We take the $rectform above to make sure that the log is
3336 ;; expanded out for the situations where simplifying plog itself
3337 ;; doesn't do it. This should probably be considered a bug in the
3338 ;; plog simplifier and should be fixed there.
3339 (cond ((not (free plog
'%plog
))
3340 (subst '%log
'%plog plog
))
3342 (destructuring-let (((real . imag
) (trisplit plog
)))
3343 (cond ((eq ($asksign imag
) '$neg
)
3344 (setq imag
(m+ imag %pi2
)))
3345 ((eq ($asksign
(m- imag %pi2
)) '$pos
)
3346 (setq imag
(m- imag %pi2
)))
3348 (m+ real
(m* '$%i imag
)))))))
3351 ;;; Temporary fix for a lacking in taylor, which loses with %i in denom.
3352 ;;; Besides doesn't seem like a bad thing to do in general.
3353 (defun %i-out-of-denom
(exp)
3354 (let ((denom ($denom exp
)))
3355 (cond ((among '$%i denom
)
3356 ;; Multiply the denominator by it's conjugate to get rid of
3358 (let* ((den-conj (maxima-substitute (m- '$%i
) '$%i denom
))
3360 (new-denom (sratsimp (m* denom den-conj
))))
3361 ;; If the new denominator still contains %i, just give
3362 ;; up. Otherwise, multiply the numerator by the
3363 ;; conjugate and divide by the new denominator.
3364 (if (among '$%i new-denom
)
3366 (setq exp
(m// (m* num den-conj
) new-denom
)))))
3369 ;;; LL and UL must be real otherwise this routine return $UNKNOWN.
3370 ;;; Returns $no $unknown or a list of poles in the interval (ll ul)
3371 ;;; for exp w.r.t. var.
3372 ;;; Form of list ((pole . multiplicity) (pole1 . multiplicity) ....)
3373 (defun poles-in-interval (exp var ll ul
)
3374 (let* ((denom (cond ((mplusp exp
)
3375 ($denom
(sratsimp exp
)))
3377 (free (caddr exp
) var
)
3378 (eq ($asksign
(caddr exp
)) '$neg
))
3379 (m^
(cadr exp
) (m- (caddr exp
))))
3381 (roots (real-roots denom var
))
3382 (ll-pole (limit-pole exp var ll
'$plus
))
3383 (ul-pole (limit-pole exp var ul
'$minus
)))
3384 (cond ((or (eq roots
'$failure
)
3386 (null ul-pole
)) '$unknown
)
3387 ((and (or (eq roots
'$no
)
3388 (member ($csign denom
) '($pos $neg $pn
)))
3389 ;; this clause handles cases where we can't find the exact roots,
3390 ;; but we know that they occur outside the interval of integration.
3391 ;; example: integrate ((1+exp(t))/sqrt(t+exp(t)), t, 0, 1);
3393 (eq ul-pole
'$no
)) '$no
)
3394 (t (cond ((equal roots
'$no
)
3396 (do ((dummy roots
(cdr dummy
))
3397 (pole-list (cond ((not (eq ll-pole
'$no
))
3401 (cond ((not (eq ul-pole
'$no
))
3402 (sort-poles (push `(,ul .
1) pole-list
)))
3403 ((not (null pole-list
))
3404 (sort-poles pole-list
))
3406 (let* ((soltn (caar dummy
))
3407 ;; (multiplicity (cdar dummy)) (not used? -- cwh)
3408 (root-in-ll-ul (in-interval soltn ll ul
)))
3409 (cond ((eq root-in-ll-ul
'$no
) '$no
)
3410 ((eq root-in-ll-ul
'$yes
)
3411 (let ((lim-ans (is-a-pole exp soltn
)))
3412 (cond ((null lim-ans
)
3416 (t (push (car dummy
)
3417 pole-list
))))))))))))
3420 ;;;Returns $YES if there is no pole and $NO if there is one.
3421 (defun limit-pole (exp var limit direction
)
3422 (let ((ans (cond ((member limit
'($minf $inf
) :test
#'eq
)
3423 (cond ((eq (special-convergent-formp exp limit
) '$yes
)
3425 (t (get-limit (m* exp var
) var limit direction
))))
3427 (cond ((eq ans
'$no
) '$no
)
3429 ((eq ans
'$und
) '$no
)
3430 ((equal ans
0.
) '$no
)
3433 ;;;Takes care of forms that the ratio test fails on.
3434 (defun special-convergent-formp (exp limit
)
3435 (cond ((not (oscip exp
)) '$no
)
3436 ((or (eq (sc-converg-form exp limit
) '$yes
)
3437 (eq (exp-converg-form exp limit
) '$yes
))
3441 (defun exp-converg-form (exp limit
)
3443 (setq exparg
(%einvolve exp
))
3444 (cond ((or (null exparg
)
3445 (freeof '$%i exparg
))
3451 (sratsimp (m// exp
(m^t
'$%e exparg
))))))
3452 (equal (get-limit exp var limit
) 0))
3456 (defun sc-converg-form (exp limit
)
3457 (prog (scarg trigpow
)
3458 (setq exp
($expand exp
))
3459 (setq scarg
(involve (sin-sq-cos-sq-sub exp
) '(%sin %cos
)))
3460 (cond ((null scarg
) (return '$no
))
3461 ((and (polyinx scarg var
())
3462 (eq ($asksign
(m- ($hipow scarg var
) 1)) '$pos
)) (return '$yes
))
3463 ((not (freeof var
(sdiff scarg var
)))
3465 ((and (setq trigpow
($hipow exp
`((%sin
) ,scarg
)))
3466 (eq (ask-integer trigpow
'$odd
) '$yes
)
3467 (equal (get-limit (m// exp
`((%sin
) ,scarg
)) var limit
)
3470 ((and (setq trigpow
($hipow exp
`((%cos
) ,scarg
)))
3471 (eq (ask-integer trigpow
'$odd
) '$yes
)
3472 (equal (get-limit (m// exp
`((%cos
) ,scarg
)) var limit
)
3475 (t (return '$no
)))))
3477 (defun is-a-pole (exp soltn
)
3479 (m* (maxima-substitute (m+ 'epsilon soltn
) var exp
)
3483 (defun in-interval (place ll ul
)
3484 ;; real values for ll and ul; place can be imaginary.
3485 (let ((order (ask-greateq ul ll
)))
3486 (cond ((eq order
'$yes
))
3487 ((eq order
'$no
) (let ((temp ul
)) (setq ul ll ll temp
)))
3488 (t (merror (intl:gettext
"defint: failed to order limits of integration:~%~M")
3489 (list '(mlist simp
) ll ul
)))))
3490 (if (not (equal ($imagpart place
) 0))
3492 (let ((lesseq-ul (ask-greateq ul place
))
3493 (greateq-ll (ask-greateq place ll
)))
3494 (if (and (eq lesseq-ul
'$yes
) (eq greateq-ll
'$yes
)) '$yes
'$no
))))
3496 ;; returns true or nil
3497 (defun strictly-in-interval (place ll ul
)
3498 ;; real values for ll and ul; place can be imaginary.
3499 (and (equal ($imagpart place
) 0)
3501 (eq ($asksign
(m+ ul
(m- place
))) '$pos
))
3503 (eq ($asksign
(m+ place
(m- ll
))) '$pos
))))
3505 (defun real-roots (exp var
)
3506 (let (($solvetrigwarn
(cond (defintdebug t
) ;Rest of the code for
3507 (t ()))) ;TRIGS in denom needed.
3508 ($solveradcan
(cond ((or (among '$%i exp
)
3509 (among '$%e exp
)) t
)
3511 *roots
*failures
) ;special vars for solve.
3512 (cond ((not (among var exp
)) '$no
)
3513 (t (solve exp var
1)
3514 ;; If *failures is set, we may have missed some roots.
3515 ;; We still return the roots that we have found.
3516 (do ((dummy *roots
(cddr dummy
))
3519 (cond ((not (null rootlist
))
3522 (cond ((equal ($imagpart
(caddar dummy
)) 0)
3525 ($rectform
(caddar dummy
))
3529 (defun ask-greateq (x y
)
3530 ;;; Is x > y. X or Y can be $MINF or $INF, zeroA or zeroB.
3531 (let ((x (cond ((among 'zeroa x
)
3536 (subst 0 'epsilon x
))
3537 ((or (among '$inf x
)
3541 (y (cond ((among 'zeroa y
)
3546 (subst 0 'epsilon y
))
3547 ((or (among '$inf y
)
3559 (t (let ((ans ($asksign
(m+ x
(m- y
)))))
3560 (cond ((member ans
'($zero $pos
) :test
#'eq
)
3566 (defun sort-poles (pole-list)
3567 (sort pole-list
#'(lambda (x y
)
3568 (cond ((eq (ask-greateq (car x
) (car y
))
3573 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3575 ;;; Integrate Definite Integrals involving log and exp functions. The algorithm
3576 ;;; are taken from the paper "Evaluation of CLasses of Definite Integrals ..."
3577 ;;; by K.O.Geddes et. al.
3579 ;;; 1. CASE: Integrals generated by the Gamma function.
3584 ;;; I t log (t) expt(- t ) dt = s signum(s)
3591 ;;; (--- (gamma(z))! )
3597 ;;; The integral converges for:
3598 ;;; s # 0, m = 0, 1, 2, ... and realpart((w+1)/s) > 0.
3600 ;;; 2. CASE: Integrals generated by the Incomplete Gamma function.
3605 ;;; I t log (t) exp(- t ) dt = (--- (gamma_incomplete(a, x ))! )
3613 ;;; The integral converges for:
3614 ;;; s # 0, m = 0, 1, 2, ... and realpart((w+1)/s) > 0.
3615 ;;; The shown solution is valid for s>0. For s<0 gamma_incomplete has to be
3616 ;;; replaced by gamma(a) - gamma_incomplete(a,x^s).
3618 ;;; 3. CASE: Integrals generated by the beta function.
3623 ;;; I log (1 - t) (1 - t) t log (t) dt =
3631 ;;; --- (--- (beta(z, w))! )!
3637 ;;; The integral converges for:
3638 ;;; n, m = 0, 1, 2, ..., s > -1 and r > -1.
3639 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3641 (defvar *debug-defint-log
* nil
)
3643 ;;; Recognize c*z^w*log(z)^m*exp(-t^s)
3645 (defun m2-log-exp-1 (expr)
3646 (when *debug-defint-log
*
3647 (format t
"~&M2-LOG-EXP-1 with ~A~%" expr
))
3651 ((mexpt) (z varp
) (w freevar
))
3652 ((mexpt) $%e
((mtimes) -
1 ((mexpt) (z varp
) (s freevar0
))))
3653 ((mexpt) ((%log
) (z varp
)) (m freevar
)))))
3655 ;;; Recognize c*z^r*log(z)^n*(1-z)^s*log(1-z)^m
3657 (defun m2-log-exp-2 (expr)
3658 (when *debug-defint-log
*
3659 (format t
"~&M2-LOG-EXP-2 with ~A~%" expr
))
3663 ((mexpt) (z varp
) (r freevar
))
3664 ((mexpt) ((%log
) (z varp
)) (n freevar
))
3665 ((mexpt) ((mplus) 1 ((mtimes) -
1 (z varp
))) (s freevar
))
3666 ((mexpt) ((%log
) ((mplus) 1 ((mtimes)-
1 (z varp
)))) (m freevar
)))))
3668 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3670 (defun defint-log-exp (expr var ll ul
)
3675 ;; var1 is used as a parameter for differentiation. Add var1>0 to the
3676 ;; database, to get the desired simplification of the differentiation of
3677 ;; the gamma_incomplete function.
3678 (setq *global-defint-assumptions
*
3679 (cons (assume `((mgreaterp) ,var1
0))
3680 *global-defint-assumptions
*))
3684 (setq x
(m2-log-exp-1 expr
)))
3685 ;; The integrand matches the cases 1 and 2.
3686 (let ((c (cdras 'c x
))
3690 ($gamma_expand nil
)) ; No expansion of Gamma functions.
3692 (when *debug-defint-log
*
3693 (format t
"~&DEFINT-LOG-EXP-1:~%")
3694 (format t
"~& : c = ~A~%" c
)
3695 (format t
"~& : w = ~A~%" w
)
3696 (format t
"~& : m = ~A~%" m
)
3697 (format t
"~& : s = ~A~%" s
))
3699 (cond ((and (zerop1 ll
)
3702 (not (eq ($sign s
) '$zero
))
3703 (eq ($sign
(div (add w
1) s
)) '$pos
))
3704 ;; Case 1: Generated by the Gamma function.
3707 (simplify (list '(%signum
) s
))
3708 (power s
(mul -
1 (add m
1)))
3709 ($at
($diff
(list '(%gamma
) var1
) var1 m
)
3712 (div (add w
1) s
))))))
3713 ((and (member ($sign ll
) '($pos $pz
))
3715 (or (= m
0) (= m
1)) ; Exclude m>1, because Maxima can not
3716 ; derivate the involved hypergeometric
3718 (or (and (eq ($sign s
) '$neg
)
3719 (eq ($sign
(div (add 1 w
) s
)) '$pos
))
3720 (and (eq ($sign s
) '$pos
)
3721 (eq ($sign
(div (add 1 w
) s
)) '$pos
))))
3722 ;; Case 2: Generated by the Incomplete Gamma function.
3723 (let ((f (if (eq ($sign s
) '$pos
)
3724 (list '(%gamma_incomplete
) var1
(power ll s
))
3725 (sub (list '(%gamma
) var1
)
3726 (list '(%gamma_incomplete
) var1
(power ll s
))))))
3729 (simplify (list '(%signum
) s
))
3730 (power s
(mul -
1 (add m
1)))
3731 ($at
($diff f var1 m
)
3732 (list '(mequal) var1
(div (add 1 w
) s
)))))))
3734 (setq result nil
)))))
3737 (setq x
(m2-log-exp-2 expr
)))
3738 ;; Case 3: Generated by the Beta function.
3739 (let ((c (cdras 'c x
))
3747 (when *debug-defint-log
*
3748 (format t
"~&DEFINT-LOG-EXP-2:~%")
3749 (format t
"~& : c = ~A~%" c
)
3750 (format t
"~& : r = ~A~%" r
)
3751 (format t
"~& : n = ~A~%" n
)
3752 (format t
"~& : s = ~A~%" s
)
3753 (format t
"~& : m = ~A~%" m
))
3755 (cond ((and (integerp m
)
3759 (eq ($sign
(add 1 r
)) '$pos
)
3760 (eq ($sign
(add 1 s
)) '$pos
))
3763 ($at
($diff
($at
($diff
(list '($beta
) var1 var2
)
3765 (list '(mequal) var2
(add 1 s
)))
3767 (list '(mequal) var1
(add 1 r
))))))
3769 (setq result nil
)))))
3772 ;; Simplify result and set $gamma_expand to global value
3773 (let (($gamma_expand $gamma_expand
)) (sratsimp result
))))
3775 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;