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 limit
)
16 ;;; **************************************************************
18 ;;; ** LIMIT PACKAGE **
20 ;;; **************************************************************
22 ;;; I believe a large portion of this file is described in Paul
23 ;;; Wang's thesis, "Evaluation of Definite Integrals by Symbolic
24 ;;; Manipulation," MIT/LCS/TR-92, Oct. 1971. This can be found at
25 ;;; https://web.archive.org/web/20191019131847/https://apps.dtic.mil/dtic/tr/fulltext/u2/732005.pdf
28 ;;; TOP LEVEL FUNCTION(S): $LIMIT $LDEFINT
30 (declare-top (special errorsw origval $lhospitallim low
*
31 *indicator half%pi nn
* dn
* numer denom exp var val varlist
32 *zexptsimp? $tlimswitch $logarc taylored logcombed
33 $exponentialize lhp? lhcount $ratfac genvar
34 loginprod? $limsubst $logabs a context limit-assumptions
35 limit-top limitp integer-info old-integer-info $keepfloat $logexpand
))
37 (defconstant +behavior-count
+ 4)
38 (defvar *behavior-count-now
*)
39 (defvar *getsignl-asksign-ok
* nil
)
41 (load-macsyma-macros rzmac
)
43 (defmvar infinities
'($inf $minf $infinity
)
44 "The types of infinities recognized by Maxima.
45 INFINITY is complex infinity")
47 (defmvar real-infinities
'($inf $minf
)
48 "The real infinities, `inf' is positive infinity, `minf' negative infinity")
50 (defmvar infinitesimals
'($zeroa $zerob
)
51 "The infinitesimals recognized by Maxima. ZEROA zero from above,
52 ZEROB zero from below")
54 (defmvar simplimplus-problems
()
55 "A list of all problems in the stack of recursive calls to simplimplus.")
57 (defmvar limit-answers
()
58 "An association list for storing limit answers.")
60 (defmvar limit-using-taylor
()
61 "Is the current limit computation using taylor expansion?")
63 (defmvar preserve-direction
() "Makes `limit' return Direction info.")
65 (unless (boundp 'integer-info
) (setq integer-info
()))
67 ;; This should be made to give more information about the error.
69 ;; (cond (errorsw (throw 'errorsw t))
70 ;; (t (merror "Discontinuity Encountered"))))
72 ;;(DEFUN PUTLIMVAL (E V)
73 ;; (let ((exp (cons '(%limit) (list e var val))))
74 ;; (cond ((not (assolike exp limit-answers))
75 ;; (setq limit-answers (cons (cons exp v) limit-answers))
79 (defun putlimval (e v
&aux exp
)
80 (setq exp
`((%limit
) ,e
,var
,val
))
81 (unless (assolike exp limit-answers
)
82 (push (cons exp v
) limit-answers
))
86 (let ((exp (cons '(%limit
) (list e var val
))))
87 (assolike exp limit-answers
)))
89 (defmacro limit-catch
(exp var val
)
91 (let ((ans (catch 'errorsw
92 (catch 'limit
(limit ,exp
,var
,val
'think
)))))
93 (if (or (null ans
) (eq ans t
))
97 (defmfun $limit
(&rest args
)
98 (let ((first-try (apply #'toplevel-$limit args
)))
99 (if (and (consp first-try
) (eq (caar first-try
) '%limit
))
100 (let ((*getsignl-asksign-ok
* t
))
101 (apply #'toplevel-$limit args
))
104 (defun toplevel-$limit
(&rest args
)
105 (let ((limit-assumptions ())
106 (old-integer-info ())
109 (declare (special limit-assumptions old-integer-info
110 $keepfloat limit-top
))
112 (setq old-integer-info integer-info
)
113 (setq integer-info
()))
116 (let ((exp1 ()) (lhcount $lhospitallim
) (*behavior-count-now
* 0)
117 (exp ()) (var ()) (val ()) (dr ())
118 (*indicator
()) (taylored ()) (origval ())
119 (logcombed ()) (lhp?
())
120 (varlist ()) (ans ()) (genvar ()) (loginprod?
())
121 (limit-answers ()) (limitp t
) (simplimplus-problems ())
122 (lenargs (length args
))
124 (declare (special lhcount
*behavior-count-now
* exp var val
*indicator
125 taylored origval logcombed lhp?
126 varlist genvar loginprod? limitp
))
128 (unless (or (= lenargs
3) (= lenargs
4) (= lenargs
1))
130 ;; Is it a LIST of Things?
131 (when (setq ans
(apply #'limit-list args
))
133 (setq exp1
(specrepcheck (first args
)))
134 (when (and (atom exp1
)
135 (member exp1
'(nil t
)))
136 ;; The expression is 'T or 'NIL. Return immediately.
139 (setq var
(setq genfoo
(gensym)) ; Use a gensym. Not foo.
142 (setq var
(second args
))
143 (when ($constantp var
)
144 (merror (intl:gettext
"limit: second argument must be a variable, not a constant; found: ~M") var
))
145 (unless (or ($subvarp var
) (atom var
))
146 (merror (intl:gettext
"limit: variable must be a symbol or subscripted symbol; found: ~M") var
))
147 (setq val
(infsimp (third args
)))
148 ;; infsimp converts -inf to minf. it also converts -infinity to
149 ;; infinity, although perhaps this should generate the error below.
150 (when (and (not (atom val
))
151 (some #'(lambda (x) (not (freeof x val
)))
153 (merror (intl:gettext
"limit: third argument must be a finite value or one of: inf, minf, infinity; found: ~M") val
))
154 (when (eq val
'$zeroa
) (setq dr
'$plus
))
155 (when (eq val
'$zerob
) (setq dr
'$minus
))))
157 (unless (member (fourth args
) '($plus $minus
) :test
#'eq
)
158 (merror (intl:gettext
"limit: direction must be either 'plus' or 'minus'; found: ~M") (fourth args
)))
159 (setq dr
(fourth args
))))
160 (if (and (atom var
) (not (among var val
)))
162 (let ((realvar var
)) ;; Var is funny so make it a gensym.
164 (setq exp
(maxima-substitute var realvar exp1
))
165 (putprop var realvar
'limitsub
)))
166 (unless (or $limsubst
(eq var genfoo
))
167 (when (limunknown exp
)
168 (return `((%limit
) ,@(cons exp1
(cdr args
))))))
169 (setq varlist
(ncons var
) genvar nil origval val
)
170 ;; Transform limits to minf to limits to inf by
171 ;; replacing var with -var everywhere.
172 (when (eq val
'$minf
)
175 exp
(subin (m* -
1 var
) exp
)))
177 ;; Hide noun form of %derivative, %integrate.
178 (setq exp
(hide exp
))
180 ;; Transform the limit value.
181 (unless (infinityp val
)
183 (let ((*atp
* t
) (realvar var
))
184 ;; *atp* prevents substitution from applying to vars
185 ;; bound by %sum, %product, %integrate, %limit
187 (putprop var t
'internal
)
188 (setq exp
(maxima-substitute (m+ val var
) realvar exp
))))
189 (setq val
(cond ((eq dr
'$plus
) '$zeroa
)
190 ((eq dr
'$minus
) '$zerob
)
194 ;; Make assumptions about limit var being very small or very large.
195 ;; Assumptions are forgotten upon exit.
196 (unless (= lenargs
1)
197 (limit-context var val dr
))
199 ;; Resimplify in light of new assumptions.
200 (setq exp
(resimplify
204 (limitsimp ($expand exp
1 0) var
))))))
206 (if (not (or (real-epsilonp val
) ;; if direction of limit not specified
208 (setq ans
(both-side exp var val
)) ;; compute from both sides
209 (let ((d (catch 'mabs
(mabs-subst exp var val
))))
210 (cond ;; otherwise try to remove absolute value
211 ((eq d
'$und
) (return '$und
))
214 (setq ans
(limit-catch exp var val
));; and find limit from one side
218 (setq ans
(catch 'taylor-catch
219 (let ((silent-taylor-flag t
))
220 (declare (special silent-taylor-flag
))
221 (gruntz1 exp var val
)))))
223 ;; try taylor series expansion if simple limit didn't work
224 (if (and (null ans
) ;; if no limit found and
225 $tlimswitch
;; user says ok to use taylor and
226 (not limit-using-taylor
));; not already doing taylor
227 (let ((limit-using-taylor t
))
228 (declare (special limit-using-taylor
))
229 (setq ans
(limit-catch exp var val
))))))
232 (return (clean-limit-exp ans
))
233 (return (cons '(%limit
) args
))))) ;; failure: return nounform
234 (restore-assumptions))))
236 (defun clean-limit-exp (exp)
237 (setq exp
(restorelim exp
))
238 (if preserve-direction exp
(ridofab exp
)))
240 (defun limit-list (exp1 &rest rest
)
242 `(,(car exp1
) ,@(mapcar #'(lambda (x) (apply #'toplevel-$limit
`(,x
,@rest
))) (cdr exp1
)))
245 (defun limit-context (var val direction
) ;Only works on entry!
247 (assume '((mgreaterp) lim-epsilon
0))
248 (assume '((mgreaterp) prin-inf
100000000))
249 (setq limit-assumptions
(make-limit-assumptions var val direction
))
254 (defun make-limit-assumptions (var val direction
)
255 (let ((new-assumptions))
256 (cond ((or (null var
) (null val
))
258 ((and (not (infinityp val
)) (null direction
))
261 `(,(assume `((mgreaterp) ,var
100000000)) ,@new-assumptions
))
263 `(,(assume `((mgreaterp) -
100000000 ,var
)) ,@new-assumptions
))
264 ((eq direction
'$plus
)
265 `(,(assume `((mgreaterp) ,var
0)) ,@new-assumptions
)) ;All limits around 0
266 ((eq direction
'$minus
)
267 `(,(assume `((mgreaterp) 0 ,var
)) ,@new-assumptions
))
271 (defun restore-assumptions ()
272 ;;;Hackery until assume and forget take reliable args. Nov. 9 1979.
274 (do ((assumption-list limit-assumptions
(cdr assumption-list
)))
275 ((null assumption-list
) t
)
276 (forget (car assumption-list
)))
277 (forget '((mgreaterp) lim-epsilon
0))
278 (forget '((mgreaterp) prin-inf
100000000))
279 (cond ((and (not (null integer-info
))
281 (do ((list integer-info
(cdr list
)))
283 (i-$remove
`(,(cadar list
) ,(caddar list
))))
284 (setq integer-info old-integer-info
))))
286 ;; The optional arg allows the caller to decide on the value of
287 ;; preserve-direction. Default is T, like it used to be.
288 (defun both-side (exp var val
&optional
(preserve t
))
289 (let* ((preserve-direction preserve
)
290 (la (toplevel-$limit exp var val
'$plus
)) lb
)
291 (when (eq la
'$und
) (return-from both-side
'$und
))
292 (setf lb
(toplevel-$limit exp var val
'$minus
))
293 (let ((ra (ridofab la
))
295 (cond ((eq t
(meqp ra rb
))
299 ; Maxima does not consider equal(ind,ind) to be true, but
300 ; if both one-sided limits are ind then we want to call
301 ; the two-sided limit ind (e.g., limit(sin(1/x),x,0)).
303 ((or (not (free la
'%limit
))
304 (not (free lb
'%limit
)))
306 ((and (infinityp la
) (infinityp lb
))
307 ; inf + minf => infinity
312 (defun limunknown (f)
313 (catch 'limunknown
(limunknown1 (specrepcheck f
))))
315 (defun limunknown1 (f)
316 (cond ((mapatom f
) nil
)
317 ((or (not (safe-get (caar f
) 'operators
))
318 (member (caar f
) '(%sum %product mncexpt
) :test
#'eq
)
319 ;;Special function code here i.e. for li[2](x).
320 (and (eq (caar f
) 'mqapply
)
321 (not (get (subfunname f
) 'specsimp
))))
322 (if (not (free f var
)) (throw 'limunknown t
)))
323 (t (mapc #'limunknown1
(cdr f
)) nil
)))
326 (if (involve e
'(%gamma
)) (setq e
($makefact e
)))
327 (cond ((involve e
'(mfactorial))
328 (setq e
(simplify ($minfactorial e
))))
332 ;; or nil if sign unknown or complex
334 (let ((z (ridofab z
)))
335 (if (not (free z var
)) (setq z
(toplevel-$limit z var val
)))
336 (let ((*complexsign
* t
))
337 (let ((sign (if *getsignl-asksign-ok
* ($asksign z
) ($sign z
))))
338 (cond ((eq sign
'$pos
) 1)
340 ((eq sign
'$zero
) 0))))))
342 (defun restorelim (exp)
343 (cond ((null exp
) nil
)
344 ((atom exp
) (or (and (symbolp exp
) (get exp
'limitsub
)) exp
))
345 ((and (consp (car exp
)) (eq (caar exp
) 'mrat
))
347 (cons (restorelim (cadr exp
))
348 (restorelim (cddr exp
)))))
349 (t (cons (car exp
) (mapcar #'restorelim
(cdr exp
))))))
352 (defun mabs-subst (exp var val
) ; RETURNS EXP WITH MABS REMOVED, OR THROWS.
353 (let ((d (involve exp
'(mabs)))
357 ((not (and (equal ($imagpart
(let ((v (limit-catch d var val
)))
358 ;; The above call might
359 ;; throw 'limit, so we
360 ;; need to catch it. If
362 ;; limit without ABS, we
363 ;; assume the limit is
364 ;; undefined. Is this
365 ;; right? Anyway, this
366 ;; fixes Bug 1548643.
371 (equal ($imagpart var
) 0)))
372 (cond ((eq arglim
'$infinity
)
373 ;; Check for $infinity as limit of argument.
376 (throw 'mabs
'retn
))))
377 (t (do ((ans d
(involve exp
'(mabs))) (a () ()))
379 (setq a
(mabs-subst ans var val
))
380 (setq d
(limit a var val t
))
384 (setq d
(behavior a var val
))
385 (if (zerop1 d
) (throw 'mabs
'retn
))))
388 (cond ((or (eq d
'$zeroa
) (eq d
'$inf
)
390 ;; fails on limit(abs(sin(x))/sin(x), x, inf)
391 (eq ($sign d
) '$pos
))
392 (setq exp
(maxima-substitute a
`((mabs) ,ans
) exp
)))
393 ((or (eq d
'$zerob
) (eq d
'$minf
)
394 (eq ($sign d
) '$neg
))
395 (setq exp
(maxima-substitute (m* -
1 a
) `((mabs) ,ans
) exp
)))
396 (t (throw 'mabs
'retn
))))
397 (t (throw 'mabs
'retn
))))))))))
399 ;; Called on an expression that might contain $INF, $MINF, $ZEROA, $ZEROB. Tries
400 ;; to simplify it to sort out things like inf^inf or inf+1.
402 (simpinf-ic exp
(count-general-inf exp
)))
404 (defun count-general-inf (expr)
405 (count-atoms-matching
406 (lambda (x) (or (infinityp x
) (real-epsilonp x
))) expr
))
408 (defun count-atoms-matching (predicate expr
)
409 "Count the number of atoms in the Maxima expression EXPR matching PREDICATE,
410 ignoring dummy variables and array indices."
412 ((atom expr
) (if (funcall predicate expr
) 1 0))
413 ;; Don't count atoms that occur as a limit of %integrate, %sum, %product,
415 ((member (caar expr
) dummy-variable-operators
)
416 (count-atoms-matching predicate
(cadr expr
)))
417 ;; Ignore array indices
418 ((member 'array
(car expr
)) 0)
420 for arg in
(cdr expr
)
421 summing
(count-atoms-matching predicate arg
)))))
423 (defun simpinf-ic (exp &optional infinity-count
)
425 ;; A very slow identity transformation...
428 ;; If there's only one infinity, we replace it by a variable and take the
429 ;; limit as that variable goes to infinity. Use $gensym in case we can't
430 ;; compute the answer and the limit leaks out.
431 (1 (let* ((val (or (inf-typep exp
) (epsilon-typep exp
)))
433 (expr (subst var val exp
))
434 (limit (toplevel-$limit expr var val
)))
436 ;; Now we look to see whether the computed limit is any simpler than
437 ;; what we shoved in (which we'll define as "doesn't contain EXPR as a
438 ;; subtree"). If so, return it.
439 ((not (subtree-p expr limit
:test
#'equal
))
442 ;; Otherwise, return the original form: apparently, we can't compute
443 ;; the limit we needed, and it's uglier than what we started with.
446 ;; If more than one infinity, we have to be a bit more careful.
448 (let* ((arguments (mapcar 'simpinf
(cdr exp
)))
449 (new-expression (cons (list (caar exp
)) arguments
))
452 ;; If any of the arguments are undefined, we are too.
453 ((among '$und arguments
) '$und
)
454 ;; If we ended up with something indeterminate, we punt and just return
456 ((amongl '(%limit $ind
) arguments
) exp
)
458 ;; Exponentiation & multiplication
459 ((mexptp exp
) (simpinf-expt (first arguments
) (second arguments
)))
460 ((mtimesp exp
) (simpinf-times arguments
))
462 ;; Down to at most one infinity? We do this after exponentiation to
463 ;; avoid zeroa^zeroa => 0^0, which will raise an error rather than just
464 ;; returning und. We do it after multiplication to avoid zeroa * inf =>
466 ((<= (setf infinities-left
(count-general-inf new-expression
)) 1)
467 (simpinf-ic new-expression infinities-left
))
470 ((mplusp exp
) (simpinf-plus arguments
))
473 (t new-expression
))))))
475 (defun simpinf-times (arguments)
476 (declare (special exp var val
))
477 ;; When we have a product, we need to spot that zeroa * zerob = zerob, zeroa *
478 ;; inf = und etc. Note that (SIMPINF '$ZEROA) => 0, so a nonzero atom is not
479 ;; an infinitesimal. Moreover, we can assume that each of ARGUMENTS is either
480 ;; a number, computed successfully by the recursive SIMPINF call, or maybe a
481 ;; %LIMIT noun-form (in which case, we aren't going to be able to tell the
484 ((member 0 arguments
)
486 ((find-if #'infinityp arguments
) '$und
)
487 ((every #'atom arguments
) 0)
490 ((member '$infinity arguments
)
491 (if (every #'atom arguments
)
495 (t (simplimit (cons '(mtimes) arguments
) var val
))))
497 (defun simpinf-expt (base exponent
)
498 ;; In the comments below, zero* represents one of 0, zeroa, zerob.
500 ;; TODO: In some cases we give up too early. E.g. inf^(2 + 1/inf) => inf^2
501 ;; (which should simplify to inf)
511 ((0 $zeroa $zerob
) '$und
)
512 (t (list '(mexpt) base exponent
))))
513 ;; minf^inf = infinity <== Or should it be und?
516 ;; minf^foo = minf^foo
521 ((0 $zeroa $zerob
) '$und
)
522 (t (list '(mexpt) base exponent
))))
526 ;; zero*^foo = zero*^foo
531 ((0 $zeroa $zerob
) '$und
)
532 (t (list '(mexpt) base exponent
))))
533 ;; a^b where a is pretty much anything except for a naked
534 ;; inf,minf,zeroa,zerob or 0.
537 ;; When a isn't crazy, try a^b = e^(b log(a))
538 ((not (amongl (append infinitesimals infinities
) base
))
539 (simpinf (m^
'$%e
(m* exponent
`((%log
) ,base
)))))
541 ;; No idea. Just return what we've found so far.
542 (t (list '(mexpt) base exponent
))))))
544 (defun simpinf-plus (arguments)
545 ;; We know that none of the arguments are infinitesimals, since SIMPINF never
546 ;; returns one of them. As such, we partition our arguments into infinities
547 ;; and everything else. The latter won't have any "hidden" infinities like
548 ;; lim(x,x,inf), since SIMPINF gave up on anything containing a %lim already.
549 (let ((bigs) (others))
550 (dolist (arg arguments
)
551 (cond ((infinityp arg
) (push arg bigs
))
552 (t (push arg others
))))
554 ;; inf + minf or the like
555 ((cdr (setf bigs
(delete-duplicates bigs
))) '$und
)
556 ;; inf + smaller + stuff
558 ;; I don't think this can happen, since SIMPINF goes back to the start if
559 ;; there are fewer than two infinities in the arguments, but let's be
561 (t (cons '(mplus) others
)))))
563 ;; Simplify expression with zeroa or zerob.
564 (defun simpab (small)
565 (cond ((null small
) ())
566 ((member small
'($zeroa $zerob $inf $minf $infinity
) :test
#'eq
) small
)
567 ((not (free small
'$ind
)) '$ind
) ;Not exactly right but not
568 ((not (free small
'$und
)) '$und
) ;causing trouble now.
569 ((mapatom small
) small
)
570 (t (let ((preserve-direction t
)
571 (new-small (subst (m^
'$inf -
1) '$zeroa
572 (subst (m^
'$minf -
1) '$zerob small
))))
573 (simpinf new-small
)))))
576 ;;;*I* INDICATES: T => USE LIMIT1,THINK, NIL => USE SIMPLIMIT.
577 (defun limit (exp var val
*i
*)
579 ((among '$und exp
) '$und
)
582 ((not (among var exp
))
583 (cond ((amongl '($inf $minf $infinity $ind
) exp
)
585 ((amongl '($zeroa $zerob
) exp
)
586 ;; Simplify expression with zeroa or zerob.
590 (t (putlimval exp
(cond ((and limit-using-taylor
593 (taylim exp var val
*i
*))
594 ((ratp exp var
) (ratlim exp
))
595 ((or (eq *i
* t
) (radicalp exp var
))
596 (limit1 exp var val
))
598 (cond ((or (mtimesp exp
) (mexptp exp
))
599 (limit1 exp var val
))
600 (t (simplimit exp var val
))))
601 (t (simplimit exp var val
)))))))
603 (defun limitsimp (exp var
)
604 (limitsimp-expt (sin-sq-cos-sq-sub exp
) var
))
605 ;;Hack for sin(x)^2+cos(x)^2.
607 ;; if var appears in base and power of expt,
608 ;; push var into power of of expt
609 (defun limitsimp-expt (exp var
)
610 (cond ((or (atom exp
)
612 (freeof var exp
)) exp
)
614 (not (freeof var
(cadr exp
)))
615 (not (freeof var
(caddr exp
))))
616 (m^
'$%e
(simplify `((%log
) ,exp
))))
617 (t (subst0 (cons (cons (caar exp
) ())
618 (mapcar #'(lambda (x)
619 (limitsimp-expt x var
))
623 (defun sin-sq-cos-sq-sub (exp) ;Hack ... Hack
624 (let ((arg (involve exp
'(%sin %cos
))))
627 (t (let ((new-exp ($substitute
(m+t
1 (m- (m^t
`((%sin simp
) ,arg
) 2)))
628 (m^t
`((%cos simp
) ,arg
) 2)
630 (m+t
1 (m- (m^t
`((%cos simp
) ,arg
) 2)))
631 (m^t
`((%sin simp
) ,arg
) 2)
633 (cond ((not (involve new-exp
'(%sin %cos
))) new-exp
)
636 (defun expand-trigs (x var
)
639 ((and (or (eq (caar x
) '%sin
)
641 (not (free (cadr x
) var
)))
643 ((member 'array
(car x
))
644 ;; Some kind of array reference. Return it.
646 (t (simplify (cons (ncons (caar x
))
647 (mapcar #'(lambda (x)
648 (expand-trigs x var
))
653 (cond ((not (involve e
654 '(%cot %csc %binomial
655 %sec %coth %sech %csch
656 %acot %acsc %asec %acoth
658 %jacobi_ns %jacobi_nc %jacobi_cs
659 %jacobi_ds %jacobi_dc
)))
661 (t ($ratsimp
(tansc1 e
)))))
663 (defun tansc1 (e &aux tem
)
665 ((and (setq e
(cons (car e
) (mapcar 'tansc1
(cdr e
)))) ()))
666 ((setq tem
(assoc (caar e
) '((%cot . %tan
) (%coth . %tanh
)
667 (%sec . %cos
) (%sech . %cosh
)
668 (%csc . %sin
) (%csch . %sinh
)) :test
#'eq
))
669 (tansc1 (m^
(list (ncons (cdr tem
)) (cadr e
)) -
1.
)))
670 ((setq tem
(assoc (caar e
) '((%jacobi_nc . %jacobi_cn
)
671 (%jacobi_ns . %jacobi_sn
)
672 (%jacobi_cs . %jacobi_sc
)
673 (%jacobi_ds . %jacobi_sd
)
674 (%jacobi_dc . %jacobi_cd
)) :test
#'eq
))
675 ;; Converts Jacobi elliptic function to its reciprocal
677 (tansc1 (m^
(list (ncons (cdr tem
)) (cadr e
) (third e
)) -
1.
)))
678 ((setq tem
(member (caar e
) '(%sinh %cosh %tanh
) :test
#'eq
))
679 (let (($exponentialize t
))
681 ((setq tem
(assoc (caar e
) '((%acsc . %asin
) (%asec . %acos
)
682 (%acot . %atan
) (%acsch . %asinh
)
683 (%asech . %acosh
) (%acoth . %atanh
)) :test
#'eq
))
684 (list (ncons (cdr tem
)) (m^t
(cadr e
) -
1.
)))
685 ((and (eq (caar e
) '%binomial
) (among var
(cdr e
)))
686 (m// `((mfactorial) ,(cadr e
))
687 (m* `((mfactorial) ,(m+t
(cadr e
) (m- (caddr e
))))
688 `((mfactorial) ,(caddr e
)))))
692 (cond ((not (involve ex
'(%sin %cos %tan %asin %acos %atan
693 %sinh %cosh %tanh %asinh %acosh %atanh
)))
699 ((eq (caar ex
) '%sinh
)
700 (m// (m+ (m^
'$%e
(cadr ex
)) (m- (m^
'$%e
(m- (cadr ex
)))))
702 ((eq (caar ex
) '%cosh
)
703 (m// (m+ (m^
'$%e
(cadr ex
)) (m^
'$%e
(m- (cadr ex
))))
705 ((and (member (caar ex
)
706 '(%sin %cos %tan %asin %acos %atan %sinh
707 %cosh %tanh %asinh %acosh %atanh
) :test
#'eq
)
710 (t (cons (car ex
) (mapcar #'hyperex0
(cdr ex
))))))
715 ;;Used by tlimit also.
716 (defun limit1 (exp var val
)
718 (let ((lhprogress? lhp?
)
721 (cond ((setq ans
(and (not (atom exp
)) (getlimval exp
)))
723 ((and (not (infinityp val
)) (setq ans
(simplimsubst val exp
)))
726 ;;;NUMDEN* => (values numerator denominator)
727 (multiple-value-bind (n dn
)
729 (cond ((not (among var dn
))
730 (return (simplimit (m// (simplimit n var val
) dn
) var val
)))
732 (return (simplimit (m* n
(simplimexpt dn -
1 (simplimit dn var val
) -
1)) var val
)))
734 (/#alike n
(car lhprogress?
))
735 (/#alike dn
(cdr lhprogress?
)))
736 (throw 'lhospital nil
)))
737 (return (limit2 n dn var val
))))))
742 (let ((deriv (sdiff (m// e f
) var
)))
744 ((=0 ($ratsimp deriv
)) t
)
747 (defun limit2 (n dn var val
)
748 (prog (n1 d1 lim-sign gcp sheur-ans
)
749 (setq n
(hyperex n
) dn
(hyperex dn
))
750 ;;;Change to uniform limit call.
751 (cond ((infinityp val
)
752 (setq d1
(limit dn var val nil
))
753 (setq n1
(limit n var val nil
)))
754 (t (cond ((setq n1
(simplimsubst val n
)) nil
)
755 (t (setq n1
(limit n var val nil
))))
756 (cond ((setq d1
(simplimsubst val dn
)) nil
)
757 (t (setq d1
(limit dn var val nil
))))))
758 (cond ((or (null n1
) (null d1
)) (return nil
))
759 (t (setq n1
(sratsimp n1
) d1
(sratsimp d1
))))
760 (cond ((or (involve n
'(mfactorial)) (involve dn
'(mfactorial)))
761 (let ((ans (limfact2 n dn var val
)))
762 (cond (ans (return ans
))))))
763 (cond ((and (zerop2 n1
) (zerop2 d1
))
764 (cond ((not (equal (setq gcp
(gcpower n dn
)) 1))
765 (return (colexpt n dn gcp
)))
766 ((and (real-epsilonp val
)
768 (not (free dn
'%log
)))
769 (return (liminv (m// n dn
))))
770 ((setq n1
(try-lhospital-quit n dn nil
))
772 ((and (zerop2 n1
) (not (member d1
'($ind $und
) :test
#'eq
))) (return 0))
774 (setq n1
(ridofab n1
))
775 (return (simplimtimes `(,n1
,(simplimexpt dn -
1 d1 -
1))))))
776 (setq n1
(ridofab n1
))
777 (setq d1
(ridofab d1
))
778 (cond ((or (eq d1
'$und
)
779 (and (eq n1
'$und
) (not (real-infinityp d1
))))
782 ;; At this point we have n1/$ind. Look if n1 is one of the
783 ;; infinities or zero.
784 (cond ((and (infinityp n1
) (eq ($sign dn
) '$pos
))
786 ((and (infinityp n1
) (eq ($sign dn
) '$neg
))
787 (return (simpinf (m* -
1 n1
))))
789 (or (eq ($sign dn
) '$pos
)
790 (eq ($sign dn
) '$neg
)))
793 ((eq n1
'$ind
) (return (cond ((infinityp d1
) 0)
796 ((and (real-infinityp d1
) (member n1
'($inf $und $minf
) :test
#'eq
))
797 (cond ((and (not (atom dn
)) (not (atom n
))
798 (cond ((not (equal (setq gcp
(gcpower n dn
)) 1))
799 (return (colexpt n dn gcp
)))
801 (or (involve dn
'(mfactorial %gamma
))
802 (involve n
'(mfactorial %gamma
))))
803 (return (limfact n dn
))))))
804 ((eq n1 d1
) (setq lim-sign
1) (go cp
))
805 (t (setq lim-sign -
1) (go cp
))))
806 ((and (infinityp d1
) (infinityp n1
))
807 (setq lim-sign
(if (or (eq d1
'$minf
) (eq n1
'$minf
)) -
1 1))
809 (t (return (simplimtimes `(,n1
,(m^ d1 -
1))))))
810 cp
(setq n
($expand n
) dn
($expand dn
))
812 (let ((new-n (m+l
(maxi (cdr n
)))))
813 (cond ((not (alike1 new-n n
))
814 (return (limit (m// new-n dn
) var val
'think
))))
818 (let ((new-dn (m+l
(maxi (cdr dn
)))))
819 (cond ((not (alike1 new-dn dn
))
820 (return (limit (m// n new-dn
) var val
'think
))))
823 (setq sheur-ans
(sheur0 n1 d1
))
824 (cond ((or (member sheur-ans
'($inf $zeroa
) :test
#'eq
)
825 (free sheur-ans var
))
826 (return (simplimtimes `(,lim-sign
,sheur-ans
))))
827 ((and (alike1 sheur-ans dn
)
829 ((member (setq n1
(cond ((expfactorp n1 d1
) (expfactor n1 d1 var
))
831 '($inf $zeroa
) :test
#'eq
)
833 ((not (null (setq n1
(cond ((expfactorp n dn
) (expfactor n dn var
))
836 ((and (alike1 sheur-ans dn
) (not (mplusp n
))))
837 ((not (alike1 sheur-ans
(m// n dn
)))
838 (return (simplimit (m// ($expand
(m// n sheur-ans
))
839 ($expand
(m// dn sheur-ans
)))
842 (cond ((and (not (and (eq val
'$inf
) (expp n
) (expp dn
)))
843 (setq n1
(try-lhospital-quit n dn nil
))
848 ;; Test whether both n and dn have form
849 ;; product of poly^poly
850 (defun expfactorp (n dn
)
851 (do ((llist (append (cond ((mtimesp n
) (cdr n
))
853 (cond ((mtimesp dn
) (cdr dn
))
856 (exp? t
) ;IS EVERY ELEMENT SO FAR
857 (factor nil
)) ;A POLY^POLY?
861 (setq factor
(car llist
))
862 (setq exp?
(or (polyinx factor var
())
864 (polyinx (cadr factor
) var
())
865 (polyinx (caddr factor
) var
()))))))
867 (defun expfactor (n dn var
) ;Attempts to evaluate limit by grouping
868 (prog (highest-deg) ; terms with similar exponents.
869 (let ((new-exp (exppoly n
))) ;exppoly unrats expon
870 (setq n
(car new-exp
) ;and rtns deg of expons
871 highest-deg
(cdr new-exp
)))
872 (cond ((null n
) (return nil
))) ;nil means expon is not
873 (let ((new-exp (exppoly dn
))) ;a rat func.
874 (setq dn
(car new-exp
)
875 highest-deg
(max highest-deg
(cdr new-exp
))))
877 (= highest-deg
0)) ; prevent infinite recursion
881 (degree highest-deg
(1- degree
))
888 (limit (m// numerator denominator
)
892 (let ((newnumer-factor (get-newexp&factors
896 (setq numerator
(car newnumer-factor
)
897 numfactors
(cdr newnumer-factor
)))
898 (let ((newdenom-factor (get-newexp&factors
902 (setq denominator
(car newdenom-factor
)
903 denfactors
(cdr newdenom-factor
)))
904 (setq answer
(simplimit (list '(mexpt)
906 (m// numfactors denfactors
))
907 (cond ((> degree
0) var
)
911 (cond ((member answer
'($ind $und
) :test
#'equal
)
912 ;; cannot handle limit(exp(x*%i)*x, x, inf);
914 ((member answer
'($inf $minf
) :test
#'equal
)
915 ;; 0, zeroa, zerob are passed through to next iteration
916 (return (simplimtimes (list (m// numerator denominator
) answer
)))))))))
918 (defun exppoly (exp) ;RETURNS EXPRESSION WITH UNRATTED EXPONENTS
922 (exp (cond ((mtimesp exp
)
926 ((null exp
) (cons new-exp highest-deg
))
927 (setq factor
(car exp
))
929 (m* (cond ((or (not (mexptp factor
))
930 (not (ratp (caddr factor
) var
)))
934 (ratdegree (caddr factor
))))
935 (m^
(cadr factor
) (unrat (caddr factor
)))))
938 (defun unrat (exp) ;RETURNS UNRATTED EXPRESION
939 (multiple-value-bind (n d
)
941 (let ((tem ($divide n d
)))
943 (m// (caddr tem
) d
)))))
945 (defun get-newexp&factors
(exp degree var
) ;RETURNS (CONS NEWEXP FACTORS)
946 (do ((terms (cond ((mtimesp exp
)(cdr exp
)) ; SUCH THAT
947 (t (ncons exp
))) ; NEWEXP*FACTORS^(VAR^DEGREE)
948 (cdr terms
)) ; IS EQUAL TO EXP.
955 (setq factor
(car terms
))
956 (cond ((not (mexptp factor
))
958 (setq factors
(m* factor factors
)))
959 (t (setq newexp
(m* factor newexp
)))))
961 (= (ratdegree (caddr factor
))
963 (setq factors
(m* (m^
(cadr factor
)
964 (leading-coef (caddr factor
)))
966 newexp
(m* (m^
(cadr factor
)
968 (m* (leading-coef (caddr factor
))
971 (t (setq newexp
(m* factor newexp
))))))
973 (defun leading-coef (rat)
974 (ratlim (m// rat
(m^ var
(ratdegree rat
)))))
976 (defun ratdegree (rat)
977 (multiple-value-bind (n d
)
979 (- (deg n
) (deg d
))))
981 (defun limfact2 (n d var val
)
982 (let ((n1 (reflect0 n var val
))
983 (d1 (reflect0 d var val
)))
984 (cond ((and (alike1 n n1
)
987 (t (limit (m// n1 d1
) var val
'think
)))))
989 ;; takes expression and returns operator at front with all flags removed
990 ;; except array flag.
991 ;; array flag must match for alike1 to consider two things to be the same.
992 ;; ((MTIMES SIMP) ... ) => (MTIMES)
993 ;; ((PSI SIMP ARRAY) 0) => (PSI ARRAY)
994 (defun operator-with-array-flag (exp)
995 (cond ((member 'array
(car exp
) :test
#'eq
)
996 (list (caar exp
) 'array
))
997 (t (list (caar exp
)))))
999 (defun reflect0 (exp var val
)
1000 (cond ((atom exp
) exp
)
1001 ((and (eq (caar exp
) 'mfactorial
)
1002 (let ((argval (limit (cadr exp
) var val
'think
)))
1003 (or (eq argval
'$minf
)
1004 (and (numberp argval
)
1006 (reflect (cadr exp
)))
1007 (t (cons (operator-with-array-flag exp
)
1010 (reflect0 term var val
)))
1013 (defun reflect (arg)
1016 (m^
(list (ncons 'mfactorial
)
1020 (m^
(list (ncons '%sin
)
1024 (defun limfact (n d
)
1026 (setq n
(stirling0 n
)
1028 (setq ans
(toplevel-$limit
(m// n d
) var
'$inf
))
1029 (cond ((and (atom ans
)
1030 (not (member ans
'(und ind
) :test
#'eq
))) ans
)
1031 ((eq (caar ans
) '%limit
) ())
1034 ;; substitute asymptotic approximations for gamma, factorial, and
1036 (defun stirling0 (e)
1038 ((and (setq e
(cons (car e
) (mapcar 'stirling0
(cdr e
))))
1040 ((and (eq (caar e
) '%gamma
)
1041 (eq (limit (cadr e
) var val
'think
) '$inf
))
1042 (stirling (cadr e
)))
1043 ((and (eq (caar e
) 'mfactorial
)
1044 (eq (limit (cadr e
) var val
'think
) '$inf
))
1045 (m* (cadr e
) (stirling (cadr e
))))
1046 ((and (eq (caar e
) 'mqapply
) ;; polylogarithm
1047 (eq (subfunname e
) '$li
)
1048 (integerp (car (subfunsubs e
))))
1049 (li-asymptotic-expansion (m- (car (subfunsubs e
)) 1)
1050 (car (subfunsubs e
))
1051 (car (subfunargs e
))))
1055 (maxima-substitute x
'$z
1057 ((mexpt simp
) 2 ((rat simp
) 1 2))
1058 ((mexpt simp
) $%pi
((rat simp
) 1 2))
1059 ((mexpt simp
) $z
((mplus simp
) ((rat simp
) -
1 2) $z
))
1060 ((mexpt simp
) $%e
((mtimes simp
) -
1 $z
)))))
1062 (defun no-err-sub (v e
&aux ans
)
1063 (let ((errorsw t
) (*zexptsimp? t
)
1065 ;; Don't print any error messages
1067 (declare (special errcatch
))
1068 ;; Should we just use IGNORE-ERRORS instead HANDLER-CASE here? I
1069 ;; (rtoy) am choosing the latter so that unexpected errors will
1070 ;; actually show up instead of being silently discarded.
1072 (setq ans
(catch 'errorsw
1074 (sratsimp (subin v e
)))))
1077 (cond ((null ans
) t
) ; Ratfun package returns NIL for failure.
1080 ;; substitute value v for var into expression e.
1081 ;; if result is defined and e is continuous, we have the limit.
1082 (defun simplimsubst (v e
)
1084 (cond ((involve e
'(mfactorial)) nil
)
1086 ;; functions that are defined at their discontinuities
1087 ((amongl '($atan2 $floor %round $ceiling %signum %integrate
1091 ;; substitute value into expression
1092 ((eq (setq ans
(no-err-sub (ridofab v
) e
)) t
)
1095 ((and (member v
'($zeroa $zerob
) :test
#'eq
) (=0 ($radcan ans
)))
1096 (setq ans
(behavior e var v
))
1097 (cond ((equal ans
1) '$zeroa
)
1098 ((equal ans -
1) '$zerob
)
1099 (t nil
))) ; behavior can't find direction
1102 ;;;returns (cons numerator denominator)
1104 (let ((e (factor (simplify e
)))
1110 (mapc #'forq
(cdr e
)))
1116 (setq numer
(car numer
)))
1118 (setq numer
(m*l numer
))))
1122 (setq denom
(car denom
)))
1124 (setq denom
(m*l denom
))))
1125 (values (factor numer
) (factor denom
))))
1127 ;;;FACTOR OR QUOTIENT
1128 ;;;Setq's the special vars numer and denom from numden*
1130 (cond ((and (mexptp e
)
1131 (not (freeof var e
))
1132 (null (pos-neg-p (caddr e
))))
1133 (push (m^
(cadr e
) (m* -
1.
(caddr e
))) denom
))
1134 (t (push e numer
))))
1136 ;;;Predicate to tell whether an expression is pos,zero or neg as var -> val.
1137 ;;;returns T if pos,zero. () if negative or don't know.
1138 (defun pos-neg-p (exp)
1139 (let ((ans (limit exp var val
'think
)))
1140 (cond ((and (not (member ans
'($und $ind $infinity
) :test
#'eq
))
1141 (equal ($imagpart ans
) 0))
1142 (let ((sign (getsignl ans
)))
1143 (cond ((or (equal sign
1)
1146 ((equal sign -
1) nil
))))
1149 (declare-top (unspecial n dn
))
1152 (cond ((radicalp e var
) nil
)
1153 ((member (caar e
) '(%log %sin %cos %tan %sinh %cosh %tanh mfactorial
1154 %asin %acos %atan %asinh %acosh %atanh
) :test
#'eq
) nil
)
1156 ((do ((e (cdr e
) (cdr e
)))
1158 (and (expp (car e
)) (return t
))))))
1162 (radicalp (cadr e
) var
)
1163 (among var
(caddr e
))
1164 (radicalp (caddr e
) var
)))
1167 (defun gcpower (a b
)
1168 ($gcd
(getexp a
) (getexp b
)))
1171 (cond ((and (mexptp exp
)
1172 (free (caddr exp
) var
)
1173 (eq (ask-integer (caddr exp
) '$integer
) '$yes
))
1175 ((mtimesp exp
) (getexplist (cdr exp
)))
1178 (defun getexplist (list)
1179 (cond ((null (cdr list
))
1180 (getexp (car list
)))
1181 (t ($gcd
(getexp (car list
))
1182 (getexplist (cdr list
))))))
1184 (defun limroot (exp power
)
1185 (cond ((or (atom exp
) (not (member (caar exp
) '(mtimes mexpt
) :test
#'eq
)))
1186 (limroot (list '(mexpt) exp
1) power
)) ;This is strange-JIM.
1187 ((mexptp exp
) (m^
(cadr exp
)
1188 (sratsimp (m* (caddr exp
) (m^ power -
1.
)))))
1189 (t (m*l
(mapcar #'(lambda (x)
1193 ;;NUMERATOR AND DENOMINATOR HAVE EXPONENTS WITH GCD OF GCP.
1194 ;;; Used to call simplimit but some of the transformations used here
1195 ;;; were not stable w.r.t. the simplifier, so try keeping exponent separate
1198 (defun colexpt (n dn gcp
)
1199 (let ((bas (m* (limroot n gcp
) (limroot dn
(m* -
1 gcp
))))
1202 (setq baslim
(limit bas var val
'think
))
1203 (setq expolim
(limit expo var val
'think
))
1204 (simplimexpt bas expo baslim expolim
)))
1206 ;; this function is responsible for the following bug:
1207 ;; limit(x^2 + %i*x, x, inf) -> inf (should be infinity)
1209 (cond ((member val
'($inf $infinity
) :test
#'eq
)
1210 (setq e
(maxima-substitute (m^t
'x -
1) var e
)))
1212 (setq e
(maxima-substitute (m^t -
1 (m^t
'x -
1)) var e
)))
1214 (setq e
(maxima-substitute (m- 'x
) var e
)))
1216 (setq e
(maxima-substitute 'x var e
)))
1217 ((setq e
(maxima-substitute (m+t
'x val
) var e
))))
1218 (destructuring-let* ((e (let (($ratfac
()))
1219 ($rat
(sratsimp e
) 'x
)))
1227 (sratsimp (m// ($ratdisrep
`(,h
,(locoef n g
) .
1))
1228 ($ratdisrep
`(,h
,(locoef d g
) .
1))))))
1230 (cond ((not (member val
'($zerob $zeroa $inf $minf
) :test
#'eq
))
1232 ((not (equal ($imagpart e
) 0))
1234 ((null (setq e
(getsignl ($realpart e
))))
1236 ((equal e
1) '$zeroa
)
1237 ((equal e -
1) '$zerob
)
1240 ((not (member val
'($zerob $zeroa $infinity $inf $minf
) :test
#'eq
))
1242 ((eq val
'$infinity
) '$infinity
)
1243 ((not (equal ($imagpart e
) 0)) '$infinity
)
1244 ((null (setq e
(getsignl ($realpart e
))))
1247 ((equal e -
1) '$minf
)
1251 (if (or (atom n
) (not (eq (car n
) x
)))
1256 (if (or (atom n
) (not (eq (car n
) x
)))
1260 (defun behavior (exp var val
) ; returns either -1, 0, 1.
1261 (if (= *behavior-count-now
* +behavior-count
+)
1263 (let ((*behavior-count-now
* (1+ *behavior-count-now
*)) pair sign
)
1264 (cond ((real-infinityp val
)
1265 (setq val
(cond ((eq val
'$inf
) '$zeroa
)
1266 ((eq val
'$minf
) '$zerob
)))
1267 (setq exp
(sratsimp (subin (m^ var -
1) exp
)))))
1268 (cond ((eq val
'$infinity
) 0) ; Needs more hacking for complex.
1270 (prog2 (setq pair
(partition exp var
1))
1271 (not (mtimesp (cdr pair
)))))
1272 (setq sign
(getsignl (car pair
)))
1273 (if (not (fixnump sign
))
1275 (mul sign
(behavior (cdr pair
) var val
))))
1276 ((and (=0 (no-err-sub (ridofab val
) exp
))
1278 (free (caddr exp
) var
)
1279 (equal (getsignl (caddr exp
)) 1))
1280 (let ((bas (cadr exp
)) (expo (caddr exp
)))
1281 (behavior-expt bas expo
)))
1282 (t (behavior-by-diff exp var val
))))))
1284 (defun behavior-expt (bas expo
)
1285 (let ((behavior (behavior bas var val
)))
1286 (cond ((= behavior
1) 1)
1288 ((eq (ask-integer expo
'$integer
) '$yes
)
1289 (cond ((eq (ask-integer expo
'$even
) '$yes
) 1)
1292 (cond ((evenp (cadr expo
)) 1)
1293 ((oddp (caddr expo
)) behavior
)
1297 (defun behavior-by-diff (exp var val
)
1298 (cond ((not (or (eq val
'$zeroa
) (eq val
'$zerob
))) 0)
1299 (t (let ((old-val val
) (old-exp exp
))
1300 (setq val
(ridofab val
))
1302 (exp (sratsimp (sdiff exp var
)) (sratsimp (sdiff exp var
)))
1304 (ans ())) ; This do wins by a return.
1305 ((> ct
1) 0) ; This loop used to run up to 5 times,
1306 ;; but the size of some expressions would blow up.
1307 (setq ans
(no-err-sub val exp
)) ;Why not do an EVENFN and ODDFN
1310 (return (behavior-numden old-exp var old-val
)))
1311 ((=0 ans
) ()) ;Do it again.
1312 (t (setq ans
(getsignl ans
))
1313 (cond (n (return ans
))
1315 (return (if (eq old-val
'$zeroa
) 1 -
1)))
1317 (return (if (eq old-val
'$zeroa
) -
1 1)))
1318 (t (return 0))))))))))
1320 (defun behavior-numden (exp var val
)
1321 (let ((num ($num exp
)) (denom ($denom exp
)))
1322 (cond ((equal denom
1) 0) ;Could be hacked more from here.
1323 (t (let ((num-behav (behavior num var val
))
1324 (denom-behav (behavior denom var val
)))
1325 (cond ((or (= num-behav
0) (= denom-behav
0)) 0)
1326 ((= num-behav denom-behav
) 1)
1329 (defun try-lhospital (n d ind
)
1330 ;;Make one catch for the whole bunch of lhospital trials.
1331 (let ((ans (lhospital-catch n d ind
)))
1332 (cond ((null ans
) ())
1333 ((not (free-infp ans
)) (simpinf ans
))
1334 ((not (free-epsilonp ans
)) (simpab ans
))
1337 (defun try-lhospital-quit (n d ind
)
1338 (let ((ans (or (lhospital-catch n d ind
)
1339 (lhospital-catch (m^ d -
1) (m^ n -
1) ind
))))
1340 (cond ((null ans
) (throw 'limit t
))
1341 ((not (free-infp ans
)) (simpinf ans
))
1342 ((not (free-epsilonp ans
)) (simpab ans
))
1345 (defun lhospital-catch (n d ind
)
1346 (cond ((> 0 lhcount
)
1347 (setq lhcount $lhospitallim
)
1348 (throw 'lhospital nil
))
1349 ((equal lhcount $lhospitallim
)
1350 (let ((lhcount (m+ lhcount -
1)))
1351 (catch 'lhospital
(lhospital n d ind
))))
1352 (t (setq lhcount
(m+ lhcount -
1))
1353 (prog1 (lhospital n d ind
)
1354 (setq lhcount
(m+ lhcount
1))))))
1355 ;;If this succeeds then raise LHCOUNT.
1358 (defun lhospital (n d ind
)
1359 (declare (special val lhp?
))
1361 (setq n
(m*l
(mapcar #'(lambda (term) (lhsimp term var val
)) (cdr n
)))))
1363 (setq d
(m*l
(mapcar #'(lambda (term) (lhsimp term var val
)) (cdr d
)))))
1364 (multiple-value-bind (n d
)
1366 (let (const nconst dconst
)
1367 (setq lhp?
(and (null ind
) (cons n d
)))
1368 (multiple-value-setq (nconst n
) (var-or-const n
))
1369 (multiple-value-setq (dconst d
) (var-or-const d
))
1371 (setq n
(stirling0 n
)) ;; replace factorial and %gamma
1372 (setq d
(stirling0 d
)) ;; with approximations
1374 (setq n
(sdiff n var
) ;; take derivatives for l'hospital
1377 (if (or (not (free n
'%derivative
)) (not (free d
'%derivative
)))
1378 (throw 'lhospital
()))
1379 (setq n
(expand-trigs (tansc n
) var
))
1380 (setq d
(expand-trigs (tansc d
) var
))
1382 (multiple-value-setq (const n d
) (remove-singularities n d
))
1383 (setq const
(m* const
(m// nconst dconst
)))
1384 (simpinf (let ((ans (if ind
1385 (limit2 n d var val
)
1386 (limit-numden n d val
))))
1387 ;; When the limit function returns, it's possible that it will return NIL
1388 ;; (gave up without finding a limit). It's also possible that it will
1389 ;; return something containing UND. We treat that as a failure too.
1390 (when (and ans
(freeof '$und ans
))
1391 (m* const ans
)))))))
1393 ;; Try to compute the limit of a quotient NUM/DEN, trying to massage the input
1394 ;; into a convenient form for LIMIT on the way.
1395 (defun limit-numden (n d val
)
1397 ;; For general arguments, the best approach seems to be to use
1398 ;; sratsimp to simplify the quotient as much as we can, then
1399 ;; $multthru, which splits it up into a sum (presumably useful
1400 ;; because limit(a+b) = limit(a) + limit(b) if the limits exist, and
1401 ;; the right hand side might be easier to calculate)
1403 ($multthru
(sratsimp (m// n d
))))
1405 ;; If we've already got a sum in the numerator, it seems to be
1406 ;; better not to recombine it. Call LIMIT on the whole lot, though,
1407 ;; because terms with infinite limits might cancel to give a finite
1410 (m+l
(mapcar #'(lambda (x)
1411 (sratsimp (m// x d
)))
1414 (limit expr var val
'think
)))
1416 ;; Heuristics for picking the right way to express a LHOSPITAL problem.
1417 (defun lhop-numden (num denom
)
1418 (declare (special var
))
1419 (cond ((let ((log-num (involve num
'(%log
))))
1420 (cond ((null log-num
) ())
1421 ((lessthan (num-of-logs (factor (sratsimp (sdiff (m^ num -
1) var
))))
1422 (num-of-logs (factor (sratsimp (sdiff num var
)))))
1423 (psetq num
(m^ denom -
1) denom
(m^ num -
1))
1426 ((let ((log-denom (involve denom
'(%log
))))
1427 (cond ((null log-denom
) ())
1428 ((lessthan (num-of-logs (sratsimp (sdiff (m^ denom -
1) var
)))
1429 (num-of-logs (sratsimp (sdiff denom var
))))
1430 (psetq denom
(m^ num -
1) num
(m^ denom -
1))
1433 ((let ((exp-num (%einvolve num
)))
1435 (cond ((%e-right-placep exp-num
)
1437 (t (psetq num
(m^ denom -
1)
1438 denom
(m^ num -
1)) t
)))
1440 ((let ((exp-den (%einvolve denom
)))
1442 (cond ((%e-right-placep exp-den
)
1444 (t (psetq num
(m^ denom -
1)
1445 denom
(m^ num -
1)) t
)))
1447 ((let ((scnum (involve num
'(%sin
))))
1448 (cond (scnum (cond ((trig-right-placep '%sin scnum
) t
)
1449 (t (psetq num
(m^ denom -
1)
1450 denom
(m^ num -
1)) t
)))
1452 ((let ((scden (involve denom
'(%sin
))))
1453 (cond (scden (cond ((trig-right-placep '%sin scden
) t
)
1454 (t (psetq num
(m^ denom -
1)
1455 denom
(m^ num -
1)) t
)))
1457 ((let ((scnum (involve num
'(%asin %acos %atan
))))
1458 ;; If the numerator contains an inverse trig and the
1459 ;; denominator or reciprocal of denominator is polynomial,
1460 ;; leave everything as is. If the inverse trig is moved to
1461 ;; the denominator, things get messy, even if the numerator
1462 ;; becomes a polynomial. This is not perfect.
1463 (cond ((and scnum
(or (polyinx denom var
())
1464 (polyinx (m^ denom -
1) var
())))
1467 ((or (oscip num
) (oscip denom
)))
1469 (psetq num
(m^ denom -
1) denom
(m^ num -
1))))
1472 ;;i don't know what to do here for some cases, may have to be refined.
1473 (defun num-of-logs (exp)
1474 (cond ((mapatom exp
) 0)
1475 ((equal (caar exp
) '%log
)
1476 (m+ 1 (num-of-log-l (cdr exp
))))
1477 ((and (mexptp exp
) (mnump (caddr exp
)))
1478 (m* (simplify `((mabs) ,(caddr exp
)))
1479 (num-of-logs (cadr exp
))))
1480 (t (num-of-log-l (cdr exp
)))))
1482 (defun num-of-log-l (llist)
1483 (do ((temp llist
(cdr temp
)) (ans 0))
1485 (setq ans
(m+ ans
(num-of-logs (car temp
))))))
1487 (defun %e-right-placep
(%e-arg
)
1488 (let ((%e-arg-diff
(sdiff %e-arg var
)))
1490 ((free %e-arg-diff var
)) ;simple cases
1491 ((or (and (mexptp denom
)
1492 (equal (cadr denom
) -
1))
1493 (polyinx (m^ denom -
1) var
())) ())
1494 ((let ((%e-arg-diff-lim
(ridofab (limit %e-arg-diff var val
'think
)))
1495 (%e-arg-exp-lim
(ridofab (limit (m^
'$%e %e-arg
) var val
'think
))))
1498 (format t
"%e-arg-dif-lim = ~A~%" %e-arg-diff-lim
)
1499 (format t
"%e-arg-exp-lim = ~A~%" %e-arg-exp-lim
))
1500 (cond ((equal %e-arg-diff-lim %e-arg-exp-lim
)
1502 ((and (mnump %e-arg-diff-lim
) (mnump %e-arg-exp-lim
))
1504 ((and (mnump %e-arg-diff-lim
) (infinityp %e-arg-exp-lim
))
1505 ;; This is meant to make maxima handle bug 1469411
1506 ;; correctly. Undoubtedly, this needs work.
1510 (defun trig-right-placep (trig-type arg
)
1511 (let ((arglim (ridofab (limit arg var val
'think
)))
1512 (triglim (ridofab (limit `((,trig-type
) ,arg
) var val
'think
))))
1513 (cond ((and (equal arglim
0) (equal triglim
0)) t
)
1514 ((and (infinityp arglim
) (infinityp triglim
)) t
)
1517 ;;Takes a numerator and a denominator. If they tries all combinations of
1518 ;;products to try and make a simpler set of subproblems for LHOSPITAL.
1519 (defun remove-singularities (numer denom
)
1520 (cond ((or (null numer
) (null denom
)
1521 (atom numer
) (atom denom
)
1522 (not (mtimesp numer
)) ;Leave this here for a while.
1523 (not (mtimesp denom
)))
1524 (values 1 numer denom
))
1527 (multiple-value-bind (num-consts num-vars
)
1528 (var-or-const numer
)
1529 (multiple-value-bind (denom-consts denom-vars
)
1530 (var-or-const denom
)
1531 (if (not (mtimesp num-vars
))
1532 (setq num-vars
(list num-vars
))
1533 (setq num-vars
(cdr num-vars
)))
1534 (if (not (mtimesp denom-vars
))
1535 (setq denom-vars
(list denom-vars
))
1536 (setq denom-vars
(cdr denom-vars
)))
1537 (do ((nl num-vars
(cdr nl
))
1538 (num-list (copy-list num-vars
))
1539 (den-list denom-vars den-list-temp
)
1540 (den-list-temp (copy-list denom-vars
)))
1541 ((null nl
) (values (m* const
(m// num-consts denom-consts
))
1543 (m*l den-list-temp
)))
1544 (do ((dl den-list
(cdr dl
)))
1546 (if (or (%einvolve
(car nl
)) (%einvolve
(car nl
)))
1548 (let ((lim (catch 'limit
(simpinf (simpab (limit (m// (car nl
) (car dl
))
1549 var val
'think
))))))
1550 (cond ((or (eq lim t
)
1552 (equal (ridofab lim
) 0)
1554 (not (free lim
'$inf
))
1555 (not (free lim
'$minf
))
1556 (not (free lim
'$infinity
))
1557 (not (free lim
'$ind
))
1558 (not (free lim
'$und
)))
1561 (setq const
(m* lim const
))
1562 (setq num-list
(delete (car nl
) num-list
:count
1 :test
#'equal
))
1563 (setq den-list-temp
(delete (car dl
) den-list-temp
:count
1 :test
#'equal
))
1564 (return t
)))))))))))))
1566 ;; separate terms that contain var from constant terms
1567 ;; returns (const-terms . var-terms)
1568 (defun var-or-const (expr)
1569 (setq expr
($factor expr
))
1577 (do ((l (cdr expr
) (cdr l
))
1580 ((null l
) (values const varl
))
1581 (if (free (car l
) var
)
1582 (setq const
(m* (car l
) const
))
1583 (setq varl
(m* (car l
) varl
)))))
1587 ;; if term goes to non-zero constant, replace with constant
1588 (defun lhsimp (term var val
)
1589 (cond ((atom term
) term
)
1591 (let ((term-value (ridofab (limit term var val
'think
))))
1592 (cond ((not (member term-value
1593 '($inf $minf $und $ind $infinity
0)))
1597 (defun bylog (expo bas
)
1600 (try-lhospital-quit (simplify `((%log
) ,(tansc bas
)))
1605 (defun simplimexpt (bas expo bl el
)
1606 (cond ((or (eq bl
'$und
) (eq el
'$und
)) '$und
)
1608 (cond ((eq el
'$inf
) (if (eq bl
'$zeroa
) bl
0))
1609 ((eq el
'$minf
) (if (eq bl
'$zeroa
) '$inf
'$infinity
))
1610 ((eq el
'$ind
) '$ind
)
1611 ((eq el
'$infinity
) '$und
)
1612 ((zerop2 el
) (bylog expo bas
))
1613 (t (cond ((equal (getsignl el
) -
1)
1614 (cond ((eq bl
'$zeroa
) '$inf
)
1616 (cond ((even1 el
) '$inf
)
1617 ((eq (ask-integer el
'$integer
) '$yes
)
1618 (if (eq (ask-integer el
'$even
) '$yes
)
1620 '$minf
)))) ;Gotta be ODD.
1621 (t (setq bas
(behavior bas var val
))
1622 (cond ((equal bas
1) '$inf
)
1623 ((equal bas -
1) '$minf
)
1624 (t (throw 'limit t
))))))
1626 (member bl
'($zeroa $zerob
) :test
#'eq
))
1627 (cond ((even1 el
) '$zeroa
)
1628 ((and (eq bl
'$zerob
)
1630 (evenp (caddr el
))) 0)
1632 ((and (equal (getsignl el
) 1)
1633 (eq bl
'$zeroa
)) bl
)
1634 ((equal (getsignl el
) 0)
1638 (cond ((zerop2 el
) (bylog expo bas
))
1640 ((eq el
'$inf
) '$infinity
)
1641 ((member el
'($infinity $ind
) :test
#'eq
) '$und
)
1642 ((equal (setq el
(getsignl el
)) 1) '$infinity
)
1645 (t (throw 'limit t
))))
1647 (cond ((eq el
'$inf
) '$inf
)
1648 ((equal el
'$minf
) 0)
1649 ((zerop2 el
) (bylog expo bas
))
1650 ((member el
'($infinity $ind
) :test
#'eq
) '$und
)
1651 (t (cond ((eql 0 (getsignl el
)) 1)
1652 ((ratgreaterp 0 el
) '$zeroa
)
1653 ((ratgreaterp el
0) '$inf
)
1654 (t (throw 'limit t
))))))
1656 (cond ((zerop2 el
) (bylog expo bas
))
1657 ((eq el
'$inf
) '$und
)
1658 ((equal el
'$minf
) 0)
1659 ;;;Why not generalize this. We can ask about the number. -Jim 2/23/81
1660 ((mnump el
) (cond ((mnegp el
)
1663 (if (eq (ask-integer el
'$integer
) '$yes
)
1664 (if (eq (ask-integer el
'$even
) '$yes
)
1668 (t (cond ((even1 el
) '$inf
)
1669 ((eq (ask-integer el
'$integer
) '$yes
)
1670 (if (eq (ask-integer el
'$even
) '$yes
)
1674 (loginprod?
(throw 'lip?
'lip
!))
1676 ((equal (simplify (ratdisrep (ridofab bl
))) 1)
1677 (if (infinityp el
) (bylog expo bas
) 1))
1678 ((and (equal (ridofab bl
) -
1)
1679 (infinityp el
)) '$ind
) ;LB
1680 ((eq bl
'$ind
) (cond ((or (zerop2 el
) (infinityp el
)) '$und
)
1681 ((not (equal (getsignl el
) -
1)) '$ind
)
1683 ((eq el
'$inf
) (cond ((abeq1 bl
)
1684 (if (equal (getsignl bl
) 1) 1 '$ind
))
1686 (if (equal (getsignl bl
) 1) '$zeroa
0))
1687 ((equal (getsignl (m1- bl
)) 1) '$inf
)
1688 ((equal (getsignl (m1- `((mabs) ,bl
))) 1) '$infinity
)
1689 (t (throw 'limit t
))))
1690 ((eq el
'$minf
) (cond ((abeq1 bl
)
1691 (if (equal (getsignl bl
) 1) 1 '$ind
))
1693 (if (equal (getsignl bl
) 1) '$zeroa
0))
1694 ((ratgreaterp 0 bl
) '$infinity
)
1697 (if (equal val
'$infinity
)
1698 '$und
;Not enough info to do anything.
1699 (destructuring-bind (real-el . imag-el
)
1701 (setq real-el
(limit real-el var origval nil
))
1702 (cond ((eq real-el
'$minf
)
1704 ((and (eq real-el
'$inf
)
1705 (not (equal (ridofab (limit imag-el var origval nil
)) 0)))
1707 ((eq real-el
'$infinity
)
1708 (throw 'limit t
)) ;; don't really know real component
1712 ((eq el
'$ind
) '$ind
)
1717 (cond ((numberp x
) (and (integerp x
) (evenp x
)))
1718 ((and (mnump x
) (evenp (cadr x
))))))
1720 ;; is absolute value less than one?
1724 (and (ratgreaterp 1. bl
) (ratgreaterp bl -
1.
)))
1725 (t (equal (getsignl (m1- `((mabs) ,bl
))) -
1.
))))
1727 ;; is absolute value equal to one?
1731 (or (equal 1. bl
) (equal bl -
1.
)))
1732 (t (equal (getsignl (m1- `((mabs) ,bl
))) 0))))
1734 (defun simplimit (exp var val
&aux op
)
1737 ((or (atom exp
) (mnump exp
)) exp
)
1738 ((and (not (infinityp val
))
1739 (not (amongl '(%sin %cos %atanh %cosh %sinh %tanh mfactorial %log
)
1741 (not (inf-typep exp
))
1742 (simplimsubst val exp
)))
1743 ((eq (caar exp
) '%limit
) (throw 'limit t
))
1744 ((mplusp exp
) (simplimplus exp
))
1745 ((mtimesp exp
) (simplimtimes (cdr exp
)))
1746 ((mexptp exp
) (simplimexpt (cadr exp
) (caddr exp
)
1747 (limit (cadr exp
) var val
'think
)
1748 (limit (caddr exp
) var val
'think
)))
1749 ((mlogp exp
) (simplimln exp var val
))
1750 ((member (caar exp
) '(%sin %cos
) :test
#'eq
)
1751 (simplimsc exp
(caar exp
) (limit (cadr exp
) var val
'think
)))
1752 ((eq (caar exp
) '%tan
) (simplim%tan
(cadr exp
)))
1753 ((eq (caar exp
) '%atan
) (simplim%atan
(limit (cadr exp
) var val
'think
)))
1754 ((eq (caar exp
) '$atan2
) (simplim%atan2 exp
))
1755 ((member (caar exp
) '(%sinh %cosh
) :test
#'eq
)
1756 (simplimsch (caar exp
) (limit (cadr exp
) var val
'think
)))
1757 ((eq (caar exp
) 'mfactorial
)
1758 (simplimfact exp var val
))
1759 ((member (caar exp
) '(%erf %tanh
) :test
#'eq
)
1760 (simplim%erf-%tanh
(caar exp
) (cadr exp
)))
1761 ((member (caar exp
) '(%acos %asin
) :test
#'eq
)
1762 (simplim%asin-%acos
(caar exp
) (limit (cadr exp
) var val
'think
)))
1763 ((eq (caar exp
) '%atanh
)
1764 (simplim%atanh
(limit (cadr exp
) var val
'think
) val
))
1765 ((eq (caar exp
) '%acosh
)
1766 (simplim%acosh
(limit (cadr exp
) var val
'think
)))
1767 ((eq (caar exp
) '%asinh
)
1768 (simplim%asinh
(limit (cadr exp
) var val
'think
)))
1769 ((eq (caar exp
) '%inverse_jacobi_ns
)
1770 (simplim%inverse_jacobi_ns
(limit (cadr exp
) var val
'think
) (third exp
)))
1771 ((eq (caar exp
) '%inverse_jacobi_nc
)
1772 (simplim%inverse_jacobi_nc
(limit (cadr exp
) var val
'think
) (third exp
)))
1773 ((eq (caar exp
) '%inverse_jacobi_sc
)
1774 (simplim%inverse_jacobi_sc
(limit (cadr exp
) var val
'think
) (third exp
)))
1775 ((eq (caar exp
) '%inverse_jacobi_cs
)
1776 (simplim%inverse_jacobi_cs
(limit (cadr exp
) var val
'think
) (third exp
)))
1777 ((eq (caar exp
) '%inverse_jacobi_dc
)
1778 (simplim%inverse_jacobi_dc
(limit (cadr exp
) var val
'think
) (third exp
)))
1779 ((eq (caar exp
) '%inverse_jacobi_ds
)
1780 (simplim%inverse_jacobi_ds
(limit (cadr exp
) var val
'think
) (third exp
)))
1781 ((and (eq (caar exp
) 'mqapply
)
1782 (eq (subfunname exp
) '$li
))
1783 (simplim$li
(subfunsubs exp
) (subfunargs exp
) val
))
1784 ((and (eq (caar exp
) 'mqapply
)
1785 (eq (subfunname exp
) '$psi
))
1786 (simplim$psi
(subfunsubs exp
) (subfunargs exp
) val
))
1787 ((and (eq (caar exp
) var
)
1788 (member 'array
(car exp
) :test
#'eq
)
1789 (every #'(lambda (sub-exp)
1792 exp
) ;LIMIT(B[I],B,INF); -> B[I]
1793 ((setq op
(safe-get (mop exp
) 'simplim%function
))
1794 ;; Lookup a simplim%function from the property list
1795 (funcall op exp var val
))
1797 (simplify (cons (operator-with-array-flag exp
)
1798 (mapcar #'(lambda (a)
1799 (limit a var val
'think
))
1801 (throw 'limit t
)))))
1804 (setq e
(resimplify (subst (m// 1 var
) var e
)))
1805 (let ((new-val (cond ((eq val
'$zeroa
) '$inf
)
1806 ((eq val
'$zerob
) '$minf
))))
1807 (if new-val
(let ((preserve-direction t
))
1808 (toplevel-$limit e var new-val
)) (throw 'limit t
))))
1810 (defun simplimtimes (exp)
1811 ;; The following test
1812 ;; handles (-1)^x * 2^x => (-2)^x => $infinity
1813 ;; wants to avoid (-1)^x * 2^x => $ind * $inf => $und
1815 (and (expfactorp (cons '(mtimes) exp
) 1)
1816 (expfactor (cons '(mtimes) exp
) 1 var
))))
1817 (when try
(return-from simplimtimes try
)))
1819 (let ((prod 1) (num 1) (denom 1)
1820 (zf nil
) (ind-flag nil
) (inf-type nil
)
1821 (constant-zero nil
) (constant-infty nil
))
1823 (let* ((loginprod?
(involve term
'(%log
)))
1824 (y (catch 'lip?
(limit term var val
'think
))))
1826 ;; limit failed due to log in product
1828 (return-from simplimtimes
(liminv (cons '(mtimes simp
) exp
))))
1830 ;; If the limit is infinitesimal or zero
1832 (setf num
(m* num term
)
1833 constant-zero
(or constant-zero
(not (among var term
))))
1836 (unless zf
(setf zf
1)))
1838 (setf zf
(* -
1 (or zf
1))))))
1840 ;; If the limit is not some form of infinity or
1841 ;; undefined/indeterminate.
1842 ((not (member y
'($inf $minf $infinity $ind $und
) :test
#'eq
))
1843 (setq prod
(m* prod y
)))
1845 ((eq y
'$und
) (return-from simplimtimes
'$und
))
1846 ((eq y
'$ind
) (setq ind-flag t
))
1848 ;; Some form of infinity
1850 (setf denom
(m* denom term
)
1851 constant-infty
(or constant-infty
(not (among var term
))))
1852 (unless (eq inf-type
'$infinity
)
1854 ((eq y
'$infinity
) (setq inf-type
'$infinity
))
1855 ((null inf-type
) (setf inf-type y
))
1856 ;; minf * minf or inf * inf
1857 ((eq y inf-type
) (setf inf-type
'$inf
))
1859 (t (setf inf-type
'$minf
))))))))
1862 ;; If there are zeros and infinities among the terms that are free of
1863 ;; VAR, then we have an expression like "inf * zeroa * f(x)" or
1864 ;; similar. That gives an undefined result. Note that we don't
1865 ;; necessarily have something undefined if only the zeros have a term
1866 ;; free of VAR. For example "zeroa * exp(-1/x) * 1/x" as x -> 0. And
1867 ;; similarly for the infinities.
1868 ((and constant-zero constant-infty
) '$und
)
1870 ;; If num=denom=1, we didn't find any explicit infinities or zeros, so we
1871 ;; either return the simplified product or ind
1872 ((and (eql num
1) (eql denom
1))
1873 (if ind-flag
'$ind prod
))
1874 ;; If denom=1 (and so num != 1), we have some form of zero
1878 (let ((sign (getsignl prod
)))
1879 (if (or (not sign
) (eq sign
'complex
))
1885 ;; If num=1 (and so denom != 1), we have some form of infinity
1887 (let ((sign ($csign prod
)))
1890 ((eq sign
'$pos
) inf-type
)
1891 ((eq sign
'$neg
) (case inf-type
1895 ((member sign
'($complex $imaginary
)) '$infinity
)
1896 ; sign is '$zero, $pnz, $pz, etc
1897 (t (throw 'limit t
)))))
1898 ;; Both zeros and infinities
1900 ;; All bets off if there are some infinities or some zeros, but it
1901 ;; needn't be undefined (see above)
1902 (when (or constant-zero constant-infty
) (throw 'limit t
))
1904 (let ((ans (limit2 num
(m^ denom -
1) var val
)))
1906 (simplimtimes (list prod ans
))
1907 (throw 'limit t
)))))))
1909 ;;;PUT CODE HERE TO ELIMINATE FAKE SINGULARITIES??
1911 (defun simplimplus (exp)
1912 (cond ((memalike exp simplimplus-problems
)
1915 (progn (push exp simplimplus-problems
)
1916 (let ((ans (catch 'limit
(simplimplus1 exp
))))
1917 (cond ((or (eq ans
())
1919 (among '%limit ans
))
1920 (let ((new-exp (sratsimp exp
)))
1921 (cond ((not (alike1 exp new-exp
))
1923 (limit new-exp var val
'think
))))
1924 (cond ((or (eq ans
())
1929 (pop simplimplus-problems
)))))
1931 (defun simplimplus1 (exp)
1932 (prog (sum y infl infinityl minfl indl
)
1934 (do ((exp (cdr exp
) (cdr exp
)) (f))
1935 ((or y
(null exp
)) nil
)
1936 (setq f
(limit (car exp
) var val
'think
))
1939 ((eq f
'$und
) (setq y t
))
1940 ((not (member f
'($inf $minf $infinity $ind
) :test
#'eq
))
1941 (setq sum
(m+ sum f
)))
1942 ((eq f
'$ind
) (push (car exp
) indl
))
1943 (infinityl (throw 'limit t
))
1944 ;;;Don't know what to do with an '$infinity and an $inf or $minf
1945 ((eq f
'$inf
) (push (car exp
) infl
))
1946 ((eq f
'$minf
) (push (car exp
) minfl
))
1948 (cond ((or infl minfl
)
1950 (t (push (car exp
) infinityl
))))))
1951 (cond ((not (or infl minfl indl infinityl
))
1952 (return (cond ((atom sum
) sum
)
1953 ((or (not (free sum
'$zeroa
))
1954 (not (free sum
'$zerob
)))
1957 (t (cond ((null infinityl
)
1958 (cond (infl (cond ((null minfl
) (return '$inf
))
1960 (minfl (return '$minf
))
1961 ((> (length indl
) 1)
1962 ;; At this point we have a sum of '$ind. We factor
1963 ;; the sum and try again. This way we get the limit
1964 ;; of expressions like (a-b)*ind, where (a-b)--> 0.
1965 (cond ((not (alike1 (setq y
($factorsum exp
)) exp
))
1966 (return (limit y var val
'think
)))
1969 (t (return '$ind
))))
1970 (t (setq infl
(append infl infinityl
))))))
1972 oon
(setq y
(m+l
(append minfl infl
)))
1973 (cond ((alike1 exp
(setq y
(sratsimp (hyperex y
))))
1974 (cond ((not (infinityp val
))
1975 (setq infl
(cnv infl val
)) ;THIS IS HORRIBLE!!!!
1976 (setq minfl
(cnv minfl val
))))
1978 (cond ((every #'(lambda (j) (radicalp j var
))
1979 (append infl minfl
))
1980 (setq y
(rheur infl minfl
)))
1981 (t (setq y
(sheur infl minfl
))))))
1982 (t (setq y
(limit y var val
'think
))))
1983 (cond ((or (eq y
())
1984 (eq y t
)) (return ()))
1985 ((infinityp y
) (return y
))
1986 (t (return (m+ sum y
))))))
1988 ;; Limit n/d, using heuristics on the order of growth.
1991 (cond ((and (free n var
)
1994 (t (setq n
(cpa n d nil
))
1996 (cond ((oscip orig-n
) '$und
)
1998 ((equal n -
1) '$zeroa
)
1999 ((equal n
0) (m// orig-n d
)))))))
2002 ;;;L1 is a list of INF's and L2 is a list of MINF's. Added together
2003 ;;;it is indeterminate.
2004 (defun sheur (l1 l2
)
2005 (let ((term (sheur1 l1 l2
)))
2006 (cond ((equal term
'$inf
) '$inf
)
2007 ((equal term
'$minf
) '$minf
)
2008 (t (let ((new-num (m+l
(mapcar #'(lambda (num-term)
2009 (m// num-term
(car l1
)))
2011 (cond ((limit2 new-num
(m// 1 (car l1
)) var val
))))))))
2014 (cond ((atom exp
) nil
)
2015 (t (setq exp
(nformat exp
))
2016 (cond ((and (eq (caar exp
) 'mquotient
)
2017 (among var
(caddr exp
)))
2020 (defun zerop2 (z) (=0 (ridofab z
)))
2022 (defun raise (a) (m+ a
'$zeroa
))
2024 (defun lower (a) (m+ a
'$zerob
))
2026 (defun sincoshk (exp1 l sc
)
2027 (cond ((equal l
1) (lower l
))
2028 ((equal l -
1) (raise l
))
2030 ((member val
'($zeroa $zerob
) :test
#'eq
) (spangside exp1 l
))
2033 (defun spangside (e l
)
2034 (setq e
(behavior e var val
))
2035 (cond ((equal e
1) (raise l
))
2036 ((equal e -
1) (lower l
))
2039 ;; get rid of zeroa and zerob
2041 (if (among '$zeroa e
) (setq e
(maxima-substitute 0 '$zeroa e
)))
2042 (if (among '$zerob e
) (setq e
(maxima-substitute 0 '$zerob e
)))
2046 ;; returns true if exp is a polynomial raised to a numeric power
2047 (defun simplerd (exp)
2049 (mnump (caddr exp
)) ;; exponent must be a number - no variables
2050 (polyp (cadr exp
))))
2052 (defun branch1 (exp val
)
2053 (cond ((polyp exp
) nil
)
2054 ((simplerd exp
) (zerop2 (subin val
(cadr exp
))))
2056 (loop for v on
(cdr exp
)
2057 when
(branch1 (car v
) val
)
2060 (defun branch (exp val
)
2061 (cond ((polyp exp
) nil
)
2062 ((or (simplerd exp
) (mtimesp exp
))
2065 (every #'(lambda (j) (branch j val
)) (the list
(cdr exp
))))))
2067 (defun ser0 (e n d val
)
2068 (cond ((and (branch n val
) (branch d val
))
2072 ;;;NN* gets set by POFX, called by SER1, to get a list of exponents.
2073 (setq nn
* (ratmin nn
*))
2074 (setq n
(sratsimp (m^ n
(m^ var nn
*))))
2075 (setq d
(sratsimp (m^ d
(m^ var nn
*))))
2076 (cond ((member val
'($zeroa $zerob
) :test
#'eq
) nil
)
2079 (t (try-lhospital-quit n d nil
))))
2081 (defun rheur (l1 l2
)
2083 (setq m1
(mapcar (function asymredu
) l1
))
2084 (setq m2
(mapcar (function asymredu
) l2
))
2085 (setq ans
(m+l
(append m1 m2
)))
2086 (cond ((rptrouble (m+l
(append l1 l2
)))
2087 (return (limit (simplify (rdsget (m+l
(append l1 l2
))))
2091 ((mplusp ans
) (return (sheur m1 m2
)))
2092 (t (return (limit ans var val t
))))))
2094 (defun rptrouble (rp)
2095 (not (equal (rddeg rp nil
) (rddeg (asymredu rp
) nil
))))
2097 (defun radicalp (exp var
)
2098 (cond ((polyinx exp var
()))
2099 ((mexptp exp
) (cond ((equal (caddr exp
) -
1.
)
2100 (radicalp (cadr exp
) var
))
2102 ((member (caar exp
) '(mplus mtimes
) :test
#'eq
)
2103 (every #'(lambda (j) (radicalp j var
))
2106 (defun involve (e nn
*)
2107 (declare (special var
))
2108 (cond ((atom e
) nil
)
2110 ((and (member (caar e
) nn
* :test
#'eq
) (among var
(cdr e
))) (cadr e
))
2111 (t (some #'(lambda (j) (involve j nn
*)) (cdr e
)))))
2113 (defun notinvolve (exp nn
*)
2114 (cond ((atom exp
) t
)
2116 ((member (caar exp
) nn
* :test
#'eq
) (not (among var
(cdr exp
))))
2117 ((every #'(lambda (j) (notinvolve j nn
*))
2120 (defun sheur1 (l1 l2
)
2122 (setq l1
(m+l
(maxi l1
)))
2123 (setq l2
(m+l
(maxi l2
)))
2124 (setq ans
(cpa l1 l2 t
))
2125 (return (cond ((=0 ans
) (m+ l1 l2
))
2126 ((equal ans
1.
) '$inf
)
2129 (defun zero-lim (cpa-list)
2130 (do ((l cpa-list
(cdr l
)))
2132 (and (eq (caar l
) 'gen
)
2133 (zerop2 (limit (cadar l
) var val
'think
))
2136 ;; Compare order of growth for R1 and R2. The result is 0, -1, +1
2137 ;; depending on the relative order of growth. 0 is returned if R1 and
2138 ;; R2 have the same growth; -1 if R1 grows much more slowly than R2;
2139 ;; +1 if R1 grows much more quickly than R2.
2140 (defun cpa (r1 r2 flag
)
2143 (cond ((alike1 t1 t2
) 0.
)
2145 (cond ((free t2 var
) 0.
)
2146 (t (let ((lim-ans (limit1 t2 var val
)))
2147 (cond ((not (member lim-ans
'($inf $minf $und $ind
) :test
#'eq
)) 0.
)
2150 (let ((lim-ans (limit1 t1 var val
)))
2151 (cond ((not (member lim-ans
'($inf $minf $und $ind
) :test
#'eq
)) 0.
)
2154 ;; Make T1 and T2 be a list of terms that are multiplied
2156 (cond ((mtimesp t1
) (setq t1
(cdr t1
)))
2157 (t (setq t1
(list t1
))))
2158 (cond ((mtimesp t2
) (setq t2
(cdr t2
)))
2159 (t (setq t2
(list t2
))))
2160 ;; Find the strengths of each term of T1 and T2
2161 (setq t1
(mapcar (function istrength
) t1
))
2162 (setq t2
(mapcar (function istrength
) t2
))
2163 ;; Compute the max of the strengths of the terms.
2164 (let ((ans (ismax t1
))
2166 (cond ((or (null ans
) (null d
))
2167 ;;(eq (car ans) 'gen) (eq (car d) 'gen))
2168 ;; ismax couldn't find highest term; give up
2171 (if (eq (car ans
) 'var
) (setq ans
(add-up-deg t1
)))
2172 (if (eq (car d
) 'var
) (setq d
(add-up-deg t2
)))
2173 ;; Can't just just compare dominating terms if there are
2174 ;; indeterm-inates present; e.g. X-X^2*LOG(1+1/X). So
2176 (cond ((or (zero-lim t1
)
2178 (cpa-indeterm ans d t1 t2 flag
))
2179 ((isgreaterp ans d
) 1.
)
2180 ((isgreaterp d ans
) -
1.
)
2183 (defun cpa-indeterm (ans d t1 t2 flag
)
2184 (cond ((not (eq (car ans
) 'var
))
2185 (setq ans
(gather ans t1
) d
(gather d t2
))))
2186 (let ((*indicator
(and (eq (car ans
) 'exp
)
2189 (setq test
(cpa1 ans d
))
2190 (cond ((and (zerop1 test
)
2191 (or (equal ($radcan
(m// (cadr ans
) (cadr d
))) 1.
)
2192 (and (polyp (cadr ans
))
2194 (equal (limit (m// (cadr ans
) (cadr d
)) var val
'think
)
2196 (let ((new-term1 (m// t1
(cadr ans
)))
2197 (new-term2 (m// t2
(cadr d
))))
2198 (cpa new-term1 new-term2 flag
)))
2201 (defun add-up-deg (strengthl)
2202 (do ((stl strengthl
(cdr stl
))
2205 ((null stl
) (list 'var
(m*l poxl
) (m+l degl
)))
2206 (cond ((eq (caar stl
) 'var
)
2207 (push (cadar stl
) poxl
)
2208 (push (caddar stl
) degl
)))))
2212 (cond ((eq (car p1
) 'gen
) (return 0.
)))
2213 (setq flag
(car p1
))
2218 (setq s1
(istrength p1
))
2219 (setq s2
(istrength p2
))
2222 ((isgreaterp s1 s2
) 1.
)
2223 ((isgreaterp s2 s1
) -
1.
)
2225 (setq *indicator nil
)
2227 ((and (poly? p1 var
) (poly? p2 var
))
2228 (setq p1
(m- p1 p2
))
2229 (cond ((zerop1 p1
) 0.
)
2230 (t (getsignl (hot-coef p1
)))))
2234 (list (m*t -
1 p2
))))
2235 (cond ((zerop2 s1
) 0.
)
2236 ((ratgreaterp s1
0.
) 1.
)
2240 (setq p1
(caddr p1
))
2241 (setq p2
(caddr p2
))
2242 (cond ((and (poly? p1 var
) (poly? p2 var
))
2243 (setq p1
(m- p1 p2
))
2244 (return (cond ((or (zerop1 p1
)
2245 (not (among var p1
)))
2247 (t (getsignl (hot-coef p1
))))))
2248 ((and (radicalp p1 var
) (radicalp p2 var
))
2251 (list (m*t -
1 p2
))))
2252 (return (cond ((eq s1
'$inf
) 1.
)
2253 ((eq s1
'$minf
) -
1.
)
2255 (cond ((ratgreaterp s1
0.
) 1.
)
2256 ((ratgreaterp 0. s1
) -
1.
)
2259 (t (return (cpa p1 p2 t
)))))
2261 (setq p1
(try-lhospital (asymredu p1
) (asymredu p2
) nil
))
2262 (return (cond ((zerop2 p1
) -
1.
)
2263 ((real-infinityp p1
) 1.
)
2266 ;;;EXPRESSIONS TO ISGREATERP ARE OF THE FOLLOWING FORMS
2267 ;;; ("VAR" POLY DEG)
2269 ;;; ("LOG" LOG(EXP))
2270 ;;; ("FACT" <A FACTORIAL EXPRESSION>)
2271 ;;; ("GEN" <ANY OTHER TYPE OF EXPRESSION>)
2273 (defun isgreaterp (a b
)
2276 (cond ((or (eq ta
'gen
)
2278 ((and (eq ta tb
) (eq ta
'var
))
2279 (ratgreaterp (caddr a
) (caddr b
)))
2280 ((and (eq ta tb
) (eq ta
'exp
))
2281 ;; Both are exponential order of infinity. Check the
2282 ;; exponents to determine which exponent is bigger.
2283 (eq (limit (m- `((%log
) ,(second a
)) `((%log
) ,(second b
)))
2286 ((member ta
(cdr (member tb
'(num log var exp fact gen
) :test
#'eq
)) :test
#'eq
)))))
2289 ;; Preprocess the list of products. Separate the terms that
2290 ;; exponentials and those that don't. Actually multiply the
2291 ;; exponential terms together to form a single term. Pass this and
2292 ;; the rest to ismax-core to find the max.
2293 (let (exp-terms non-exp-terms
)
2295 (if (eq 'exp
(car term
))
2296 (push term exp-terms
)
2297 (push term non-exp-terms
)))
2298 ;; Multiply the exp-terms together
2301 ;;(format t "exp-terms = ~A~%" exp-terms)
2302 (dolist (term exp-terms
)
2303 (setf product
(simplify (mul product
(second term
)))))
2304 ;;(format t "product = ~A~%" product)
2305 (setf product
`(exp ,($logcontract product
)))
2306 ;;(format t "product = ~A~%" product)
2307 (ismax-core (cons product non-exp-terms
)))
2310 (defun ismax-core (l)
2313 ((= (length l
) 1) (car l
)) ;If there is only 1 thing give it back.
2314 ((every #'(lambda (x)
2315 (not (eq (car x
) 'gen
))) l
)
2317 (do ((l1 (cdr l
) (cdr l1
))
2321 (cond ((isgreaterp temp-ans
(car l1
))
2322 (setq ans temp-ans
))
2323 ((isgreaterp (car l1
) temp-ans
)
2324 (setq temp-ans
(car l1
))
2325 (setq ans temp-ans
))
2326 (t (setq ans
())))))
2329 ;RETURNS LIST OF HIGH TERMS
2331 (cond ((atom all
) nil
)
2332 (t (do ((l (cdr all
) (cdr l
))
2334 (total 1) ; running total constant factor of hi-term
2335 (hi-terms (ncons (car all
)))
2337 ((null l
) (if (zerop2 total
) ; if high-order terms cancel each other
2338 all
; keep everything
2339 hi-terms
)) ; otherwise return list of high terms
2340 (setq compare
(limit (m// (car l
) hi-term
) var val
'think
))
2342 ((or (infinityp compare
)
2343 (and (eq compare
'$und
)
2344 (zerop2 (limit (m// hi-term
(car l
)) var val
'think
))))
2345 (setq total
1) ; have found new high term
2346 (setq hi-terms
(ncons (setq hi-term
(car l
)))))
2347 ((zerop2 compare
) nil
)
2348 ;; COMPARE IS IND, FINITE-VALUED, or und in both directions
2349 (t ; add to list of high terms
2350 (setq total
(m+ total compare
))
2351 (setq hi-terms
(append hi-terms
(ncons (car l
))))))))))
2355 (cond ((atom l
) (return nil
)))
2356 l1
(setq ans
(car l
))
2358 (cond ((null l
) (return ans
))
2359 ((ratgreaterp ans
(car l
)) (go l2
))
2364 (cond ((atom l
) (return nil
)))
2365 l1
(setq ans
(car l
))
2367 (cond ((null l
) (return ans
))
2368 ((ratgreaterp (car l
) ans
) (go l2
))
2376 ((or (mnump e
) (not (among var e
))) nil
)
2377 ((and (mexptp e
) (eq (cadr e
) var
))
2378 (push (caddr e
) nn
*))
2380 (t (mapc #'pofx
(cdr e
)))))
2383 (cond ((member val
'($zeroa $zerob
) :test
#'eq
) nil
)
2384 (t (setq e
(subin (m+ var val
) e
))))
2386 (cond ((pofx e
) e
)))
2388 (defun gather (ind l
)
2390 (setq ind
(car ind
))
2391 loop
(cond ((null l
)
2392 (return (list ind
(m*l ans
))))
2393 ((equal (caar l
) ind
)
2394 (push (cadar l
) ans
)))
2398 ; returns rough class-of-growth of term
2399 (defun istrength (term)
2400 (cond ((mnump term
) (list 'num term
))
2401 ((atom term
) (cond ((eq term var
)
2403 (t (list 'num term
))))
2404 ((not (among var term
)) (list 'num term
))
2406 (let ((temp (ismax-core (mapcar #'istrength
(cdr term
)))))
2407 (cond ((not (null temp
)) temp
)
2410 (let ((temp (mapcar #'istrength
(cdr term
)))
2412 (setq temp1
(ismax temp
))
2413 (cond ((null temp1
) `(gen ,term
))
2414 ((eq (car temp1
) 'log
) `(log ,temp
))
2415 ((eq (car temp1
) 'var
) (add-up-deg temp
))
2418 (real-infinityp (limit term var val t
)))
2419 (let ((logterm (logred term
)))
2420 (cond ((and (among var
(caddr term
))
2421 (member (car (istrength logterm
))
2422 '(var exp fact
) :test
#'eq
)
2423 (real-infinityp (limit logterm var val t
)))
2424 (list 'exp
(m^
'$%e logterm
)))
2425 ((not (among var
(caddr term
)))
2426 (let ((temp (istrength (cadr term
))))
2427 (cond ((not (alike1 temp term
))
2428 (rplaca (cdr temp
) term
)
2429 (and (eq (car temp
) 'var
)
2431 (m* (caddr temp
) (caddr term
))))
2435 ((and (eq (caar term
) '%log
)
2436 (real-infinityp (limit term var val t
)))
2437 (let ((stren (istrength (cadr term
))))
2438 (cond ((member (car stren
) '(log var
) :test
#'eq
)
2440 ((and (eq (car stren
) 'exp
)
2441 (eq (caar (second stren
)) 'mexpt
))
2442 (istrength (logred (second stren
))))
2444 ((eq (caar term
) 'mfactorial
)
2446 ((let ((temp (hyperex term
)))
2447 (and (not (alike1 term temp
))
2449 (t (list 'gen term
))))
2451 ;; log reduce - returns log of s1
2453 (or (and (eq (cadr s1
) '$%e
) (caddr s1
))
2454 (m* (caddr s1
) `((%log
) ,(cadr s1
)))))
2456 (defun asymredu (rd)
2457 (cond ((atom rd
) rd
)
2459 ((not (among var rd
)) rd
)
2460 ((polyinx rd var t
))
2462 (cond ((eq (cadr rd
) var
) rd
)
2464 (factor ($expand
(m^
(polyinx (cadr rd
) var t
)
2468 (t (simplify (cons (list (caar rd
))
2469 (mapcar #'asymredu
(cdr rd
)))))))
2472 (let ((dn** ()) (nn** ()))
2473 (cond ((atom rd
) rd
)
2475 ((not (among var rd
)) rd
)
2479 (cond ((eq (cadr rd
) var
) rd
)
2480 (t (setq dn
** (caddr rd
))
2481 (setq nn
** (factor (cadr rd
)))
2482 (cond ((mtimesp nn
**)
2483 (m*l
(mapcar #'(lambda (j) (m^ j dn
**))
2486 (t (simplify (cons (ncons (caar rd
))
2487 (mapcar #'rdfact
(cdr rd
))))))))
2489 (defun cnv (expl val
)
2490 (mapcar #'(lambda (e)
2491 (maxima-substitute (cond ((eq val
'$zerob
)
2492 (m* -
1 (m^ var -
1)))
2497 (t (m^
(m+ var
(m* -
1 val
)) -
1.
)))
2502 (defun pwtaylor (exp var l terms
)
2503 (prog (coef ans c mc
)
2504 (cond ((=0 terms
) (return nil
)) ((=0 l
) (setq mc t
)))
2507 loop
(setq c
(1+ c
))
2508 (cond ((or (> c
10.
) (equal c terms
))
2510 (t (setq exp
(sdiff exp var
))))
2511 tag1
(setq coef
($radcan
(subin l exp
)))
2512 (cond ((=0 coef
) (setq terms
(1+ terms
)) (go loop
)))
2519 (m^
`((mfactorial) ,c
) -
1)
2520 (m^
(if mc var
(m+t
(m*t -
1 l
) var
)) c
)))))
2525 ((simplerd e
) (rdtay e
))
2526 (t (cons (list (caar e
))
2527 (mapcar #'rdsget
(cdr e
))))))
2530 (cond (limit-using-taylor ($ratdisrep
($taylor rd var val
1.
)))
2534 (prog (varlist p c e d $ratfac
)
2535 (setq varlist
(ncons var
))
2536 (setq p
(ratnumerator (cdr (ratrep* (cadr rd
)))))
2537 (cond ((< (length p
) 3.
) (return rd
)))
2540 (setq c
(m^ var
(m* d e
)))
2541 (setq d
($ratsimp
(varinvert (m* (pdis p
) (m^ var
(m- d
)))
2543 (setq d
(pwtaylor (m^ d e
) var
0.
3.
))
2544 (return (m* c
(varinvert d var
)))))
2546 (defun varinvert (e var
) (subin (m^t var -
1.
) e
))
2549 (prog ((varlist (list var
)))
2550 (return (let (($ratfac nil
))
2552 (pdegr (cadr (ratrep* p
)))))))
2554 (defun rat-no-ratfac (e)
2555 (let (($ratfac nil
))
2560 (defun rddeg (rd low
*)
2561 (cond ((or (mnump rd
)
2562 (not (among var rd
)))
2567 (m* (deg (cadr rd
)) (caddr rd
)))
2569 (addn (mapcar #'(lambda (j)
2573 (setq rd
(andmapcar #'(lambda (j) (rddeg j low
*))
2575 (cond (low* (ratmin rd
))
2579 (cond ((or (atom pf
) (not (eq (caadr (ratf var
)) (car pf
))))
2581 (low* (cadr (reverse pf
)))
2583 ;;There is some confusion here. We need to be aware of Branch cuts etc....
2584 ;;when doing this section of code. It is not very carefully done so there
2585 ;;are bugs still lurking. Another misfortune is that LIMIT or its inferiors
2586 ;;sometimes decides to change the limit VAL in midstream. This must be corrected
2587 ;;since LIMIT's interaction with the data base environment must be maintained.
2588 ;;I'm not sure that this code can ever be called with VAL other than $INF but
2589 ;;there is a hook in the first important cond clause to cathc them anyway.
2592 (let ((num-power (rddeg n nil
))
2593 (den-power (rddeg d nil
))
2594 (coef ()) (coef-sign ()) (power ()))
2595 (setq coef
(m// ($ratcoef
($expand n
) var num-power
)
2596 ($ratcoef
($expand d
) var den-power
)))
2597 (setq coef-sign
(getsignl coef
))
2598 (setq power
(m// num-power den-power
))
2599 (cond ((eq (ask-integer power
'$integer
) '$integer
)
2600 (cond ((eq (ask-integer power
'$even
) '$even
) '$even
)
2601 (t '$odd
)))) ;Can be extended from here.
2602 (cond ((or (eq val
'$minf
)
2605 (equal val
0)) ()) ;Can be extended to cover some these.
2606 ((ratgreaterp den-power num-power
)
2607 (cond ((equal coef-sign
1.
) '$zeroa
)
2608 ((equal coef-sign -
1) '$zerob
)
2609 ((equal coef-sign
0) 0)
2611 ((ratgreaterp num-power den-power
)
2612 (cond ((equal coef-sign
1.
) '$inf
)
2613 ((equal coef-sign -
1) '$minf
)
2614 ((equal coef-sign
0) nil
) ; should never be zero
2615 ((null coef-sign
) '$infinity
)))
2618 (defun radlim (e n d
)
2620 (cond ((eq val
'$infinity
) (throw 'limit nil
))
2622 (setq nl
(m* var -
1))
2623 (setq n
(subin nl n
))
2624 (setq d
(subin nl d
))
2625 (setq val
'$inf
))) ;This is the Culprit. Doesn't tell the DATABASE.
2626 (cond ((eq val
'$inf
)
2627 (setq nl
(asymredu n
))
2628 (setq dl
(asymredu d
))
2630 ((or (rptrouble n
) (rptrouble d
))
2631 (return (limit (m* (rdsget n
) (m^
(rdsget d
) -
1.
)) var val t
)))
2632 (t (return (asy nl dl
))))))
2633 (setq nl
(limit n var val t
))
2634 (setq dl
(limit d var val t
))
2635 (cond ((and (zerop2 nl
) (zerop2 dl
))
2636 (cond ((or (polyp n
) (polyp d
))
2637 (return (try-lhospital-quit n d t
)))
2638 (t (return (ser0 e n d val
)))))
2639 (t (return ($radcan
(ratrad (m// n d
) n d nl dl
)))))))
2641 (defun ratrad (e n d nl dl
)
2643 (cond ((equal nl
0) (return 0))
2646 (cond ((equal dl
0) (setq d1
'$infinity
)) ;No direction Info.
2649 ((equal (setq d
(behavior d var val
)) 1)
2651 ((equal d -
1) (setq d1
'$minf
))
2652 (t (throw 'limit nil
))))
2655 (cond ((equal (setq n
(behavior n var val
)) 1)
2657 ((equal n -
1) (setq n1
'$zerob
))
2659 (t (return ($radcan
(ridofab (subin val e
))))))
2660 (return (simplimtimes (list n1 d1
)))))
2662 ;;; Limit of the Logarithm function
2664 (defun simplimln (expr var val
)
2665 ;; We need to be careful with log because of the branch cut on the
2666 ;; negative real axis. So we look at the imagpart of the argument. If
2667 ;; it's not identically zero, we compute the limit of the real and
2668 ;; imaginary parts and combine them. Otherwise, we can use the
2669 ;; original method for real limits.
2670 (let ((arglim (limit (cadr expr
) var val
'think
)))
2671 (cond ((eq arglim
'$inf
) '$inf
)
2672 ((member arglim
'($minf $infinity
) :test
#'eq
)
2674 ((member arglim
'($ind $und
) :test
#'eq
) '$und
)
2675 ((eq arglim
'$zeroa
) '$minf
)
2676 ((eq arglim
'$zerob
) '$infinity
)
2677 ((equalp arglim
0) '$infinity
)
2679 (let ((dir (behavior (cadr expr
) var val
)))
2680 (cond ((equal dir
1) '$zeroa
)
2681 ((equal dir -
1) '$zerob
)
2683 ((equalp ($imagpart
(cadr expr
)) 0)
2684 ;; argument is real.
2685 (simplify `((%log
) ,arglim
)))
2686 (t ;; argument is complex.
2687 (destructuring-bind (rp . ip
)
2689 (if (eq (setq rp
(limit rp var val
'think
)) '$minf
)
2690 ;; Realpart is minf, do not return minf+%i*ip but infinity.
2692 ;; Return a complex limit value.
2693 (add rp
(mul '$%i
(limit ip var val
'think
)))))))))
2695 ;;; Limit of the Factorial function
2697 (defun simplimfact (expr var val
)
2698 (let* ((arglim (limit (cadr expr
) var val
'think
)) ; Limit of the argument.
2700 (cond ((eq arglim
'$inf
) '$inf
)
2701 ((member arglim
'($minf $infinity $und $ind
) :test
#'eq
) '$und
)
2702 ((and (or (maxima-integerp arglim
)
2703 (setq arg2
(integer-representation-p arglim
)))
2704 (eq ($sign arg2
) '$neg
))
2705 ;; A negative integer or float or bigfloat representation.
2706 (let ((dir (limit (add (cadr expr
) (mul -
1 arg2
)) var val
'think
))
2707 (even (mevenp arg2
)))
2708 (cond ((or (and even
2718 (t (throw 'limit nil
)))))
2720 ;; Call simplifier to get value at the limit of the argument.
2721 (simplify (list '(mfactorial) arglim
))))))
2723 (defun simplim%erf-%tanh
(fn arg
)
2724 (let ((arglim (limit arg var val
'think
))
2727 (cond ((eq arglim
'$inf
) 1)
2728 ((eq arglim
'$minf
) -
1)
2729 ((eq arglim
'$infinity
)
2730 (destructuring-bind (rpart . ipart
)
2732 (setq rlim
(limit rpart var origval
'think
))
2733 (cond ((eq fn
'%tanh
)
2734 (cond ((equal rlim
'$inf
) 1)
2735 ((equal rlim
'$minf
) -
1)))
2737 (setq ans
(limit (m* rpart
(m^t ipart -
1)) var origval
'think
))
2738 (setq ans
($asksign
(m+ `((mabs) ,ans
) -
1)))
2739 (cond ((or (eq ans
'$pos
) (eq ans
'$zero
))
2740 (cond ((eq rlim
'$inf
) 1)
2741 ((eq rlim
'$minf
) -
1)
2744 ((eq arglim
'$und
) '$und
)
2745 ((member arglim
'($zeroa $zerob $ind
) :test
#'eq
) arglim
)
2746 ;;;Ignore tanh(%pi/2*%I) and multiples of the argument.
2748 ;; erf (or tanh) of a known value is just erf(arglim).
2749 (simplify (list (ncons fn
) arglim
))))))
2751 (defun simplim%atan
(exp1)
2752 (cond ((zerop2 exp1
) exp1
)
2753 ((member exp1
'($und $infinity
) :test
#'eq
)
2755 ((eq exp1
'$inf
) half%pi
)
2758 (t `((%atan
) ,exp1
))))
2760 ;; Most instances of atan2 are simplified to expressions in atan
2761 ;; by simpatan2 before we get to this point. This routine handles
2762 ;; tricky cases such as limit(atan2((x^2-2), x^3-2*x), x, sqrt(2), minus).
2763 ;; Taylor and Gruntz cannot handle the discontinuity at atan(0, -1)
2764 (defun simplim%atan2
(exp)
2765 (let* ((exp1 (cadr exp
))
2767 (lim1 (limit (cadr exp
) var val
'think
))
2768 (lim2 (limit (caddr exp
) var val
'think
))
2769 (sign2 ($csign lim2
)))
2770 (cond ((and (zerop2 lim1
) ;; atan2( 0+, + )
2772 lim1
) ;; result is zeroa or zerob
2773 ((and (eq lim1
'$zeroa
)
2776 ((and (eq lim1
'$zerob
) ;; atan2( 0-, - )
2779 ((and (eq lim1
'$zeroa
) ;; atan2( 0+, 0 )
2781 (simplim%atan
(limit (m// exp1 exp2
) var val
'think
)))
2782 ((and (eq lim1
'$zerob
) ;; atan2( 0-, 0 )
2784 (m+ (porm (eq lim2
'$zeroa
) '$%pi
)
2785 (simplim%atan
(limit (m// exp1 exp2
) var val
'think
))))
2786 ((member lim1
'($und $infinity
) :test
#'eq
)
2788 ((eq lim1
'$inf
) half%pi
)
2791 (t (take '($atan2
) lim1 lim2
)))))
2793 (defun simplimsch (sch arg
)
2794 (cond ((real-infinityp arg
)
2795 (cond ((eq sch
'%sinh
) arg
) (t '$inf
)))
2796 ((eq arg
'$infinity
) '$infinity
)
2797 ((eq arg
'$ind
) '$ind
)
2798 ((eq arg
'$und
) '$und
)
2799 (t (let (($exponentialize t
))
2800 (resimplify (list (ncons sch
) (ridofab arg
)))))))
2802 ;; simple limit of sin and cos
2803 (defun simplimsc (exp fn arg
)
2804 (cond ((member arg
'($inf $minf $ind
) :test
#'eq
) '$ind
)
2805 ((member arg
'($und $infinity
) :test
#'eq
)
2807 ((member arg
'($zeroa $zerob
) :test
#'eq
)
2808 (cond ((eq fn
'%sin
) arg
)
2809 (t (m+ 1 '$zerob
))))
2811 (simplify (list (ncons fn
) (ridofab arg
)))
2814 (defun simplim%tan
(arg)
2815 (let ((arg1 (ridofab (limit arg var val
'think
))))
2817 ((member arg1
'($inf $minf $infinity $ind $und
) :test
#'eq
) '$und
)
2819 (let ((c (trigred (pip arg1
))))
2820 (cond ((not (equal ($imagpart arg1
) 0)) '$infinity
)
2821 ((and (eq (caar c
) 'rat
)
2824 (setq arg1
(behavior arg var val
))
2825 (cond ((= arg1
1) '$inf
)
2826 ((= arg1 -
1) '$minf
)
2828 ((and (eq (caar c
) 'rat
)
2831 (setq arg1
(behavior arg var val
))
2832 (cond ((= arg1
1) '$minf
)
2835 (t (throw 'limit
())))))
2837 (setq arg1
(behavior arg var val
))
2838 (cond ((equal arg1
1) '$zeroa
)
2839 ((equal arg1 -
1) '$zerob
)
2841 (t (simp-%tan
(list '(%tan
) arg1
) 1 nil
)))))
2843 (defun simplim%asinh
(arg)
2844 (cond ((member arg
'($inf $minf $zeroa $zerob $ind $und
) :test
#'eq
)
2846 ((eq arg
'$infinity
) '$und
)
2847 (t (simplify (list '(%asinh
) (ridofab arg
))))))
2849 (defun simplim%acosh
(arg)
2850 (cond ((equal (ridofab arg
) 1) '$zeroa
)
2851 ((eq arg
'$inf
) arg
)
2852 ((eq arg
'$minf
) '$infinity
)
2853 ((member arg
'($und $ind $infinity
) :test
#'eq
) '$und
)
2854 (t (simplify (list '(%acosh
) (ridofab arg
))))))
2856 (defun simplim%atanh
(arg dir
)
2857 ;; Compute limit(atanh(x),x,arg). If ARG is +/-1, we need to take
2858 ;; into account which direction we're approaching ARG.
2859 (cond ((zerop2 arg
) arg
)
2860 ((member arg
'($ind $und $infinity $minf $inf
) :test
#'eq
)
2862 ((equal (setq arg
(ridofab arg
)) 1.
)
2863 ;; The limit at 1 should be complex infinity because atanh(x)
2864 ;; is complex for x > 1, but inf if we're approaching 1 from
2866 (if (eq dir
'$zerob
)
2870 ;; Same as above, except for the limit is at -1.
2871 (if (eq dir
'$zeroa
)
2874 (t (simplify (list '(%atanh
) arg
)))))
2876 (defun simplim%asin-%acos
(fn arg
)
2877 (cond ((member arg
'($und $ind $inf $minf $infinity
) :test
#'eq
)
2879 ((and (eq fn
'%asin
)
2880 (member arg
'($zeroa $zerob
) :test
#'eq
))
2882 (t (simplify (list (ncons fn
) (ridofab arg
))))))
2884 (defun simplim$li
(order arg val
)
2885 (if (and (not (equal (length order
) 1))
2886 (not (equal (length arg
) 1)))
2888 (setq order
(car order
)
2890 (if (not (equal order
2))
2892 (destructuring-bind (rpart . ipart
)
2894 (cond ((not (equal ipart
0))
2897 (setq rpart
(limit rpart var val
'think
))
2898 (cond ((eq rpart
'$zeroa
) '$zeroa
)
2899 ((eq rpart
'$zerob
) '$zerob
)
2900 ((eq rpart
'$minf
) '$minf
)
2901 ((eq rpart
'$inf
) '$infinity
)
2902 (t (simplify (subfunmake '$li
(list order
)
2903 (list rpart
))))))))))
2905 (defun simplim$psi
(order arg val
)
2906 (if (and (not (equal (length order
) 1))
2907 (not (equal (length arg
) 1)))
2909 (setq order
(car order
)
2911 (cond ((equal order
0)
2912 (destructuring-bind (rpart . ipart
)
2914 (cond ((not (equal ipart
0)) (throw 'limit
()))
2915 (t (setq rpart
(limit rpart var val
'think
))
2916 (cond ((eq rpart
'$zeroa
) '$minf
)
2917 ((eq rpart
'$zerob
) '$inf
)
2918 ((eq rpart
'$inf
) '$inf
)
2919 ((eq rpart
'$minf
) '$und
)
2920 ((equal (getsignl rpart
) -
1) (throw 'limit
()))
2921 (t (simplify (subfunmake '$psi
(list order
)
2922 (list rpart
)))))))))
2923 ((and (integerp order
) (> order
0)
2924 (equal (limit arg var val
'think
) '$inf
))
2925 (cond ((mevenp order
) '$zerob
)
2926 ((moddp order
) '$zeroa
)
2927 (t (throw 'limit
()))))
2928 (t (throw 'limit
()))))
2930 (defun simplim%inverse_jacobi_ns
(arg m
)
2931 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
2933 `((%inverse_jacobi_ns
) ,arg
,m
)))
2935 (defun simplim%inverse_jacobi_nc
(arg m
)
2936 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
2937 `((%elliptic_kc
) ,m
)
2938 `((%inverse_jacobi_nc
) ,arg
,m
)))
2940 (defun simplim%inverse_jacobi_sc
(arg m
)
2941 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
2942 `((%elliptic_kc
) ,m
)
2943 `((%inverse_jacobi_sc
) ,arg
,m
)))
2945 (defun simplim%inverse_jacobi_dc
(arg m
)
2946 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
2947 `((%elliptic_kc
) ,m
)
2948 `((%inverse_jacobi_dc
) ,arg
,m
)))
2950 (defun simplim%inverse_jacobi_cs
(arg m
)
2951 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
2953 `((%inverse_jacobi_cs
) ,arg
,m
)))
2955 (defun simplim%inverse_jacobi_ds
(arg m
)
2956 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
2958 `((%inverse_jacobi_ds
) ,arg
,m
)))
2960 (setf (get '%signum
'simplim%function
) 'simplim%signum
)
2962 (defun simplim%signum
(e x pt
)
2963 (let* ((e (limit (cadr e
) x pt
'think
)) (sgn (mnqp e
0)))
2964 (cond ((eq t sgn
) (take '(%signum
) e
)) ;; limit of argument of signum is not zero
2965 ((eq nil sgn
) '$und
) ;; limit of argument of signum is zero (noncontinuous)
2966 (t (throw 'limit nil
))))) ;; don't know
2968 ;; more functions for limit to handle
2970 (defun lfibtophi (e)
2971 (cond ((not (involve e
'($fib
))) e
)
2972 ((eq (caar e
) '$fib
)
2973 (let ((lnorecurse t
))
2974 ($fibtophi
(list '($fib
) (lfibtophi (cadr e
))) lnorecurse
)))
2976 (mapcar #'lfibtophi
(cdr e
))))))
2978 ;;; FOLLOWING CODE MAKES $LDEFINT WORK
2980 (defmfun $ldefint
(exp var ll ul
&aux $logabs ans a1 a2
)
2981 (setq $logabs t ans
(sinint exp var
)
2982 a1
(toplevel-$limit ans var ul
'$minus
)
2983 a2
(toplevel-$limit ans var ll
'$plus
))
2984 (and (member a1
'($inf $minf $infinity $und $ind
) :test
#'eq
)
2985 (setq a1
(nounlimit ans var ul
)))
2986 (and (member a2
'($inf $minf $infinity $und $ind
) :test
#'eq
)
2987 (setq a2
(nounlimit ans var ll
)))
2988 ($expand
(m- a1 a2
)))
2990 (defun nounlimit (exp var val
)
2991 (setq exp
(restorelim exp
))
2992 (nconc (list '(%limit
) exp var
(ridofab val
))
2993 (cond ((eq val
'$zeroa
) '($plus
))
2994 ((eq val
'$zerob
) '($minus
)))))
2996 ;; replace noun form of %derivative and indefinite %integrate with gensym.
2997 ;; prevents substitution x -> x+1 for limit('diff(x+2,x), x, 1)
2999 ;; however, this doesn't work for limit('diff(x+2,x)/x, x, inf)
3000 ;; because the rest of the limit code thinks the gensym is const wrt x.
3002 (cond ((atom exp
) exp
)
3003 ((or (eq '%derivative
(caar exp
))
3004 (and (eq '%integrate
(caar exp
)) ; indefinite integral
3005 (null (cdddr exp
))))
3006 (hidelim exp
(caar exp
)))
3007 (t (cons (car exp
) (mapcar 'hide
(cdr exp
))))))
3009 (defun hidelim (exp func
)
3010 (setq func
(gensym))
3018 (nounlimit e var val
)
3021 ;;;Used by Defint also.
3023 (or (involve e
'(%sin %cos %tan
))
3024 (among '$%i
(%einvolve e
))))
3026 (defun %einvolve
(e)
3027 (when (among '$%e e
) (%einvolve01 e
)))
3029 (defun %einvolve01
(e)
3030 (cond ((atom e
) nil
)
3034 (among var
(caddr e
)))
3036 (t (some #'%einvolve
(cdr e
)))))
3038 (declare-top (unspecial *indicator nn
* dn
* exp var val origval taylored
3039 $tlimswitch logcombed lhp? lhcount $ratfac
))
3045 ;; "On Computing Limits in a Symbolic Manipulation System"
3046 ;; PhD Dissertation ETH Zurich 1996
3048 ;; The algorithm identifies the most rapidly varying (MRV) subexpression,
3049 ;; replaces it with a new variable w, rewrites the expression in terms
3050 ;; of the new variable, and then repeats.
3052 ;; The algorithm doesn't handle oscillating functions, so it can't do things like
3053 ;; limit(sin(x)/x, x, inf).
3055 ;; To handle limits involving functions like gamma(x) and erf(x), the
3056 ;; gruntz algorithm requires them to be written in terms of asymptotic
3057 ;; expansions, which maxima cannot currently do.
3059 ;; The algorithm assumes that everything is real, so it can't
3060 ;; currently handle limit((-2)^x, x, inf).
3062 ;; This is one of the methods used by maxima's $limit.
3063 ;; It is also directly available to the user as $gruntz.
3066 ;; most rapidly varying subexpression of expression exp with respect to limit variable var.
3067 ;; returns a list of subexpressions which are in the same MRV equivalence class.
3068 (defun mrv (exp var
)
3069 (cond ((freeof var exp
)
3074 (mrv-max (mrv (cadr exp
) var
)
3075 (mrv (m*l
(cddr exp
)) var
)
3078 (mrv-max (mrv (cadr exp
) var
)
3079 (mrv (m+l
(cddr exp
)) var
)
3082 (cond ((freeof var
(caddr exp
))
3083 (mrv (cadr exp
) var
))
3084 ((member (limitinf (logred exp
) var
) '($inf $minf
) :test
#'eq
)
3085 (mrv-max (list exp
) (mrv (caddr exp
) var
) var
))
3086 (t (mrv-max (mrv (cadr exp
) var
) (mrv (caddr exp
) var
) var
))))
3088 (mrv (cadr exp
) var
))
3089 ((equal (length (cdr exp
)) 1)
3090 (mrv (cadr exp
) var
))
3091 ((equal (length (cdr exp
)) 2)
3092 (mrv-max (mrv (cadr exp
) var
)
3093 (mrv (caddr exp
) var
)
3095 (t (tay-error "mrv not implemented" exp
))))
3097 ;; takes two lists of expresions, f and g, and limit variable var.
3098 ;; members in each list are assumed to be in same MRV equivalence
3099 ;; class. returns MRV set of the union of the inputs - either f or g
3100 ;; or the union of f and g.
3101 (defun mrv-max (f g var
)
3108 (return (union f g
))))
3109 (let ((c (mrv-compare (car f
) (car g
) var
)))
3115 (return (union f g
)))
3116 (t (merror "MRV-MAX: expected '>' '<' or '='; found: ~M" c
))))))
3118 (defun mrv-compare (a b var
)
3119 (let ((c (limitinf (m// `((%log
) ,a
) `((%log
) ,b
)) var
)))
3122 ((member c
'($inf $minf
) :test
#'eq
)
3126 ;; rewrite expression exp by replacing members of MRV set omega with
3127 ;; expressions in terms of new variable wsym. return cons pair of new
3128 ;; version of exp and the log of the new variable wsym.
3129 (defun mrv-rewrite (exp omega var wsym
)
3130 (setq omega
(stable-sort omega
(lambda (x y
) (> (length (mrv x var
))
3131 (length (mrv y var
))))));FIXME consider a total order function with #'sort
3132 (let* ((g (car (last omega
)))
3134 (sig (equal (mrv-sign logg var
) 1))
3135 (w (if sig
(m// 1 wsym
) wsym
))
3136 (logw (if sig
(m* -
1 logg
) logg
)))
3137 (mapcar (lambda (x y
)
3138 ;;(mtell "y:~M x:~M exp:~M~%" y x exp)
3139 (setq exp
(syntactic-substitute y x exp
)))
3141 (mapcar (lambda (f) ;; rewrite each element of omega
3142 (let* ((logf (logred f
))
3143 (c (mrv-leadterm (m// logf logg
) var nil
)))
3144 (cond ((not (equal (cadr c
) 0))
3145 (merror "MRV-REWRITE: expected leading term to be constant in ~M" c
)))
3146 ;;(mtell "logg: ~M logf: ~M~%" logg logf)
3149 (m* (car c
) logg
))))))
3153 ;;; if log w(x) = h(x), rewrite all subexpressions of the form
3154 ;;; log(f(x)) as log(w^-c f(x)) + c h(x) with c the unique constant
3155 ;;; such that w^-c f(x) is strictly less rapidly varying than w.
3156 (defun mrv-rewrite-logs (exp wsym logw
)
3157 (cond ((atom exp
) exp
)
3159 (not (freeof wsym exp
)))
3160 (let* ((f (cadr exp
))
3161 (c ($lopow
(calculate-series f wsym
)
3164 (m* (m^ wsym
(m- c
))
3165 (mrv-rewrite-logs f wsym logw
)))
3170 (mrv-rewrite-logs e wsym logw
))
3173 ;; returns list of two elements: coeff and exponent of leading term of exp,
3174 ;; after rewriting exp in term of its MRV set omega.
3175 (defun mrv-leadterm (exp var omega
)
3176 (prog ((new-omega ()))
3177 (cond ((freeof var exp
)
3178 (return (list exp
0))))
3179 (dolist (term omega
)
3180 (cond ((subexp exp term
)
3181 (push term new-omega
))))
3182 (setq omega new-omega
)
3184 (setq omega
(mrv exp var
))))
3185 (cond ((member var omega
:test
#'eq
)
3186 (let* ((omega-up (mrv-moveup omega var
))
3187 (e-up (car (mrv-moveup (list exp
) var
)))
3188 (mrv-leadterm-up (mrv-leadterm e-up var omega-up
)))
3189 (return (mrv-movedown mrv-leadterm-up var
)))))
3190 (destructuring-let* ((wsym (gensym "w"))
3193 ((f . logw
) (mrv-rewrite exp omega var wsym
))
3194 (series (calculate-series (mrv-rewrite-logs f wsym logw
)
3196 (setq series
(maxima-substitute logw
`((%log
) ,wsym
) series
))
3197 (setq lo
($lopow series wsym
))
3198 (when (or (not ($constantp lo
))
3199 (not (free series
'%derivative
)))
3200 ;; (mtell "series: ~M lo: ~M~%" series lo)
3201 (tay-error "error in series expansion" f
))
3202 (setq coef
($coeff series wsym lo
))
3203 ;;(mtell "exp: ~M f: ~M~%" exp f)
3204 ;;(mtell "series: ~M~%coeff: ~M~%pow: ~M~%" series coef lo)
3205 (return (list coef lo
)))))
3207 (defun mrv-moveup (l var
)
3208 (mapcar (lambda (exp)
3209 (simplify-log-of-exp
3210 (syntactic-substitute `((mexpt) $%e
,var
) var exp
)))
3213 (defun mrv-movedown (l var
)
3214 (mapcar (lambda (exp) (syntactic-substitute `((%log simp
) ,var
) var exp
))
3217 ;; test whether sub is a subexpression of exp
3218 (defun subexp (exp sub
)
3219 (not (equal (maxima-substitute 'dummy
3224 ;; Generate $lhospitallim terms of taylor expansion.
3225 ;; Ideally we would use a lazy series representation that generates
3226 ;; more terms as higher order terms cancel.
3227 (defun calculate-series (exp var
)
3228 (assume `((mgreaterp) ,var
0))
3229 (putprop var t
'internal
);; keep var from appearing in questions to user
3230 (let ((series ($taylor exp var
0 $lhospitallim
)))
3231 (forget `((mgreaterp) ,var
0))
3234 (defun mrv-sign (exp var
)
3235 (cond ((freeof var exp
)
3236 (let ((sign ($sign
($radcan exp
))))
3237 (cond ((eq sign
'$zero
)
3243 (t (tay-error " cannot determine mrv-sign" exp
)))))
3247 (* (mrv-sign (cadr exp
) var
)
3248 (mrv-sign (m*l
(cddr exp
)) var
)))
3250 (equal (mrv-sign (cadr exp
) var
) 1))
3253 (cond ((equal (mrv-sign (cadr exp
) var
) -
1)
3254 (tay-error " complex expression in gruntz limit" exp
)))
3255 (mrv-sign (m+ -
1 (cadr exp
)) var
))
3257 (mrv-sign (limitinf exp var
) var
))
3258 (t (tay-error " cannot determine mrv-sign" exp
))))
3260 ;; gruntz algorithm for limit of exp as var goes to positive infinity
3261 (defun limitinf (exp var
)
3262 (prog (($exptsubst nil
))
3263 (cond ((freeof var exp
)
3265 (destructuring-let* ((c0-e0 (mrv-leadterm exp var nil
))
3268 (sig (mrv-sign e0 var
)))
3269 (cond ((equal sig
1)
3272 (cond ((equal (mrv-sign c0 var
) 1)
3274 ((equal (mrv-sign c0 var
) -
1)
3278 ;; example: gruntz(n^n/(n^n+(n-1)^n), n, inf);
3279 (tay-error " infinite recursion in limitinf" exp
))
3280 (return (limitinf c0 var
)))))))
3282 ;; user-level function equivalent to $limit.
3283 ;; direction must be specified if limit point is not infinite
3284 ;; The arguments are checked and a failure of taylor is catched.
3286 (defmfun $gruntz
(expr var val
&rest rest
)
3288 (when (> (length rest
) 1)
3290 (intl:gettext
"gruntz: too many arguments; expected just 3 or 4")))
3291 (setq dir
(car rest
))
3292 (when (and (not (member val
'($inf $minf $zeroa $zerob
)))
3293 (not (member dir
'($plus $minus
))))
3295 (intl:gettext
"gruntz: direction must be 'plus' or 'minus'")))
3297 (catch 'taylor-catch
3298 (let ((silent-taylor-flag t
))
3299 (declare (special silent-taylor-flag
))
3300 (gruntz1 expr var val dir
))))
3301 (if (or (null ans
) (eq ans t
))
3303 `(($gruntz simp
) ,expr
,var
, val
,dir
)
3304 `(($gruntz simp
) ,expr
,var
,val
))
3307 ;; This function is for internal use in $limit.
3308 (defun gruntz1 (exp var val
&rest rest
)
3309 (cond ((> (length rest
) 1)
3310 (merror (intl:gettext
"gruntz: too many arguments; expected just 3 or 4"))))
3311 (let (($logexpand t
) ; gruntz needs $logexpand T
3312 (newvar (gensym "w"))
3314 (cond ((eq val
'$inf
)
3317 (setq exp
(maxima-substitute (m* -
1 newvar
) var exp
)))
3319 (setq exp
(maxima-substitute (m// 1 newvar
) var exp
)))
3321 (setq exp
(maxima-substitute (m// -
1 newvar
) var exp
)))
3323 (setq exp
(maxima-substitute (m+ val
(m// 1 newvar
)) var exp
)))
3325 (setq exp
(maxima-substitute (m+ val
(m// -
1 newvar
)) var exp
)))
3326 (t (merror (intl:gettext
"gruntz: direction must be 'plus' or 'minus'; found: ~M") dir
)))
3327 (limitinf exp newvar
)))
3329 ;; substitute y for x in exp
3330 ;; similar to maxima-substitute but does not simplify result
3331 (defun syntactic-substitute (y x exp
)
3332 (cond ((alike1 x exp
) y
)
3335 (mapcar (lambda (exp)
3336 (syntactic-substitute y x exp
))
3339 ;; log(exp(subexpr)) -> subexpr
3340 ;; without simplifying entire exp
3341 (defun simplify-log-of-exp (exp)
3342 (cond ((atom exp
) exp
)
3345 (eq '$%e
(cadadr exp
)))
3348 (mapcar #'simplify-log-of-exp