1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1982 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 origval
31 *indicator numer denom exp var val
33 $exponentialize lhp? lhcount
34 loginprod? a context limit-assumptions
35 limit-top integer-info old-integer-info
))
37 (defconstant +behavior-count
+ 4)
38 (defvar *behavior-count-now
*)
39 (defvar *getsignl-asksign-ok
* nil
)
41 (load-macsyma-macros rzmac
)
43 (defmvar simplimplus-problems
()
44 "A list of all problems in the stack of recursive calls to simplimplus.")
46 (defmvar limit-answers
()
47 "An association list for storing limit answers.")
49 (defmvar limit-using-taylor
()
50 "Is the current limit computation using taylor expansion?")
52 (defmvar preserve-direction
() "Makes `limit' return Direction info.")
54 (unless (boundp 'integer-info
) (setq integer-info
()))
56 ;; For limits toward infinity for the gruntz code, we assume that the limit
57 ;; variable exceeds *large-positive-number*. This value matches the value
58 ;; that the limit code uses for the same purpose.
59 (defmvar *large-positive-number
* (expt 10 8))
61 ;; Don't ask sign questions about $ind.
62 (putprop '$ind t
'internal
)
64 ;; This should be made to give more information about the error.
66 ;; (cond (errorsw (throw 'errorsw t))
67 ;; (t (merror "Discontinuity Encountered"))))
69 ;;(DEFUN PUTLIMVAL (E V)
70 ;; (let ((exp (cons '(%limit) (list e var val))))
71 ;; (cond ((not (assolike exp limit-answers))
72 ;; (setq limit-answers (cons (cons exp v) limit-answers))
76 (defun putlimval (e v
&aux exp
)
77 (setq exp
`((%limit
) ,e
,var
,val
))
78 (unless (assolike exp limit-answers
)
79 (push (cons exp v
) limit-answers
))
83 (let ((exp (cons '(%limit
) (list e var val
))))
84 (assolike exp limit-answers
)))
86 (defmacro limit-catch
(exp var val
)
88 (let ((ans (catch 'errorsw
89 (catch 'limit
(limit ,exp
,var
,val
'think
)))))
90 (if (or (null ans
) (eq ans t
))
94 (defmfun $limit
(&rest args
)
95 (let ((first-try (apply #'toplevel-$limit args
)))
96 (if (and (consp first-try
) (eq (caar first-try
) '%limit
))
97 (let ((*getsignl-asksign-ok
* t
))
98 (apply #'toplevel-$limit args
))
100 ;; The function toplevel-$limit sets $numer, $%enumer, %emode, and
101 ;; $%e_to_numlog to false. When any of these option variables are true,
102 ;; we resimplify the limit in the current context.
104 (when (or $numer $%enumer $%emode $%e_to_numlog
)
105 (setq first-try
(resimplify first-try
)))
108 ;; Return true iff the expression e has a subexpression that
109 ;; is an indefinite integral.
110 (defun indefinite-integral-p (e x
)
111 (cond (($mapatom e
) nil
)
112 ((and (eq (caar e
) '%integrate
) (alike x
(third e
)))
115 (some #'(lambda (q) (indefinite-integral-p q x
)) (cdr e
)))))
117 (defun toplevel-$limit
(&rest args
)
118 (let ((limit-assumptions ())
119 (old-integer-info ())
126 (declare (special limit-assumptions old-integer-info
129 (setq old-integer-info integer-info
)
130 (setq integer-info
()))
133 (let ((exp1 ()) (lhcount $lhospitallim
) (*behavior-count-now
* 0)
134 (exp ()) (var ()) (val ()) (dr ())
135 (*indicator
()) (taylored ()) (origval ())
136 (logcombed ()) (lhp?
())
137 (varlist ()) (ans ()) (genvar ()) (loginprod?
())
138 (limit-answers ()) (limitp t
) (simplimplus-problems ())
139 (lenargs (length args
))
141 (declare (special lhcount
*behavior-count-now
* exp var val
*indicator
142 taylored origval logcombed lhp?
143 varlist genvar loginprod? limitp
))
145 (unless (or (= lenargs
3) (= lenargs
4) (= lenargs
1))
147 ;; Is it a LIST of Things?
148 (when (setq ans
(apply #'limit-list args
))
150 (setq exp1
(specrepcheck (first args
)))
151 (when (and (atom exp1
)
152 (member exp1
'(nil t
)))
153 ;; The expression is 'T or 'NIL. Return immediately.
156 (setq var
(setq genfoo
(gensym)) ; Use a gensym. Not foo.
159 (setq var
(second args
))
160 (when ($constantp var
)
161 (merror (intl:gettext
"limit: second argument must be a variable, not a constant; found: ~M") var
))
162 (unless (or ($subvarp var
) (atom var
))
163 (merror (intl:gettext
"limit: variable must be a symbol or subscripted symbol; found: ~M") var
))
164 (setq val
(infsimp (third args
)))
165 ;; infsimp converts -inf to minf. it also converts -infinity to
166 ;; infinity, although perhaps this should generate the error below.
167 (when (and (not (atom val
))
168 (some #'(lambda (x) (not (freeof x val
)))
170 (merror (intl:gettext
"limit: third argument must be a finite value or one of: inf, minf, infinity; found: ~M") val
))
171 (when (eq val
'$zeroa
) (setq dr
'$plus
))
172 (when (eq val
'$zerob
) (setq dr
'$minus
))))
174 (unless (member (fourth args
) '($plus $minus
) :test
#'eq
)
175 (merror (intl:gettext
"limit: direction must be either 'plus' or 'minus'; found: ~M") (fourth args
)))
176 (setq dr
(fourth args
))))
177 (if (and (atom var
) (not (among var val
)))
179 (let ((realvar var
)) ;; Var is funny so make it a gensym.
181 (setq exp
(maxima-substitute var realvar exp1
))
182 (putprop var realvar
'limitsub
)))
184 ;; When exp involves an indefinite integral whose integration variable
185 ;; is the limit variable, return a limit nounform. Do this even when
186 ;; limsubst is true. Thus limit(int(f(x),x,a) is trapped here, but
187 ;; limit(int(f(x,z),x,a) is not.
188 (when (indefinite-integral-p exp var
)
189 (return (cons (list '%limit
) args
)))
191 ;; This returns a limit nounform for expressions such as
192 ;; limit(x < 8,x,0), limit(diff(f(x),x,x,0), and ...
193 (unless (eq var genfoo
)
194 (when (limunknown exp
)
195 (return `((%limit
) ,@(cons exp1
(cdr args
))))))
197 (setq varlist
(ncons var
) genvar nil origval val
)
198 ;; Transform limits to minf to limits to inf by
199 ;; replacing var with -var everywhere.
200 (when (eq val
'$minf
)
203 exp
(subin (m* -
1 var
) exp
)))
205 ;; Transform the limit value.
206 (unless (infinityp val
)
208 (let ((*atp
* t
) (realvar var
))
209 ;; *atp* prevents substitution from applying to vars
210 ;; bound by %sum, %product, %integrate, %limit
212 (putprop var t
'internal
)
213 (setq exp
(derivative-subst exp val var realvar
))
214 (setq exp
(maxima-substitute (m+ val var
) realvar exp
))))
215 (setq val
(cond ((eq dr
'$plus
) '$zeroa
)
216 ((eq dr
'$minus
) '$zerob
)
220 ;; Make assumptions about limit var being very small or very large.
221 ;; Assumptions are forgotten upon exit.
222 (unless (= lenargs
1)
223 (limit-context var val dr
))
224 ;; Resimplify in light of new assumptions. Changing ($expand exp 1 0)
225 ;; to ($expand exp 0 0) results in a bad testsuite failure for
226 ;; (assume(a>2), limit(integrate(t/log(t),t,2,a)/a,a,inf)) and
227 ;; some testsuite results that are more messy.
228 (setq exp
(resimplify
231 ($expand exp
1 0)))))
233 (if (not (or (real-epsilonp val
) ;; if direction of limit not specified
235 (setq ans
(both-side exp var val
)) ;; compute from both sides
237 (setq exp
(mabs-subst exp var val
))
238 (setq ans
(limit-catch exp var val
));; and find limit from one side
242 (setq ans
(catch 'taylor-catch
243 (let ((silent-taylor-flag t
))
244 (declare (special silent-taylor-flag
))
245 (gruntz1 exp var val
)))))
247 ;; try taylor series expansion if simple limit didn't work
248 (if (and (null ans
) ;; if no limit found and
249 $tlimswitch
;; user says ok to use taylor and
250 (not limit-using-taylor
));; not already doing taylor
251 (let ((limit-using-taylor t
))
252 (declare (special limit-using-taylor
))
253 (setq ans
(limit-catch exp var val
))))))
256 (return (clean-limit-exp ans
))
257 (return (cons '(%limit
) args
))))) ;; failure: return nounform
258 (restore-assumptions))))
260 (defun clean-limit-exp (exp)
261 (setq exp
(restorelim exp
))
262 (if preserve-direction exp
(ridofab exp
)))
264 ;; Users who want limit to map over equality (mequal) will need to do that
266 (defun limit-list (exp1 &rest rest
)
267 (if (and (mbagp exp1
) (not (mequalp exp1
)))
268 `(,(car exp1
) ,@(mapcar #'(lambda (x) (apply #'toplevel-$limit
`(,x
,@rest
))) (cdr exp1
)))
271 (defun limit-context (var val direction
) ;Only works on entry!
273 (assume '((mgreaterp) lim-epsilon
0))
274 (assume '((mgreaterp) prin-inf
100000000))
275 (setq limit-assumptions
(make-limit-assumptions var val direction
))
280 (defun make-limit-assumptions (var val direction
)
281 (let ((new-assumptions))
282 (cond ((or (null var
) (null val
))
284 ((and (not (infinityp val
)) (null direction
))
287 `(,(assume `((mgreaterp) ,var
100000000)) ,@new-assumptions
))
289 `(,(assume `((mgreaterp) -
100000000 ,var
)) ,@new-assumptions
))
290 ((eq direction
'$plus
)
291 `(,(assume `((mgreaterp) ,var
0)) ,@new-assumptions
)) ;All limits around 0
292 ((eq direction
'$minus
)
293 `(,(assume `((mgreaterp) 0 ,var
)) ,@new-assumptions
))
297 (defun restore-assumptions ()
298 ;;;Hackery until assume and forget take reliable args. Nov. 9 1979.
300 (do ((assumption-list limit-assumptions
(cdr assumption-list
)))
301 ((null assumption-list
) t
)
302 (forget (car assumption-list
)))
303 (forget '((mgreaterp) lim-epsilon
0))
304 (forget '((mgreaterp) prin-inf
100000000))
305 (cond ((and (not (null integer-info
))
307 (do ((list integer-info
(cdr list
)))
309 (i-$remove
`(,(cadar list
) ,(caddar list
))))
310 (setq integer-info old-integer-info
))))
312 ;; The optional arg allows the caller to decide on the value of
313 ;; preserve-direction. Default is nil, since we immediately ridofab.
314 (defun both-side (exp var val
&optional
(preserve nil
))
315 (let* ((preserve-direction preserve
)
316 (la (toplevel-$limit exp var val
'$plus
)) lb
)
317 ; Immediately propagate an und without trying the
319 (when (eq la
'$und
) (return-from both-side
'$und
))
320 (setf lb
(toplevel-$limit exp var val
'$minus
))
321 ; Immediately propagate an und
322 (when (eq lb
'$und
) (return-from both-side
'$und
))
323 (let ((ra (ridofab la
))
325 (cond ((eq t
(meqp ra rb
))
329 ; Maxima does not consider equal(ind,ind) to be true, but
330 ; if both one-sided limits are ind then we want to call
331 ; the two-sided limit ind (e.g., limit(sin(1/x),x,0)).
333 ((or (not (free la
'%limit
))
334 (not (free lb
'%limit
)))
337 (let ((infa (infinityp la
))
338 (infb (infinityp lb
)))
339 (cond ((and infa infb
)
340 ; inf + minf => infinity
347 ;; Return true when Maxima should return a limit nounform for limit(e,x,var,val).
348 (defun limunknown (e)
349 (not (limit-ok e var
)))
351 ;; Return true when Maxima's limit code should be able to determine limit
352 (defun limit-ok (e x
)
354 ((mapatom e
) t
) ;mapatoms are OK
355 ;; return nil for an indefinite integral
356 ((and (eq (caar e
) '%integrate
) (alike x
(third e
)) (null (fourth e
))) nil
)
357 ;; Return true for integrate(X,z,a,b) provided (a) freeof(x,X) (b) limit-ok(a,x)
358 ;; and (c) limit-ok(b,x). The function simplim%integrate doesn't recoginize
359 ;; $limsubst, but this function does.
360 ((eq (caar e
) '%integrate
)
361 (and (limit-ok (fourth e
) x
) (limit-ok (fifth e
) x
))
362 (or $limsubst
($freeof
(third e
) x
)))
363 ;; when the operator of e either (a) has a simplim%function, (b)
364 ;; is a simplifying operator (including subscripted operators),
365 ;; or (c) is $fib, check that each argument of e is OK.
368 (ok (or (get op
'operators
)
369 (get op
'simplim%function
)
370 (and (eq op
'mqapply
) (get (subfunname e
) 'specsimp
))
372 (and (or ok $limsubst
)
373 (or (every #'(lambda(q) (limit-ok q x
)) (cdr e
)) ($freeof x e
)))))))
375 ;; The functionality of this code has been blended into extra-simp. But
376 ;; there are some differences: (a) when e depends on the global var, this
377 ;; code converts *every* gamma function into a factorial, but extra-simp only
378 ;; converts subexpressions of the form gamma(X), where X depends on var to
379 ;; factorial form (b) this code dispatches minfactorial, but extra-simp
382 ;; The function factosimp is not called by the limit code or by any
385 (if (involve e
'(%gamma
)) (setq e
($makefact e
)))
386 (cond ((involve e
'(mfactorial))
387 (setq e
(simplify ($minfactorial e
))))
390 ;; Define sgn = csign(limit(z,var,val)). Return -1, 0, 1, or nil when
391 ;; sgn is neg, zero, pos, or complex, imaginary or unknown (including when
392 ;; the limit is ind), respectively. Also, when the limit is either zerob or zeroa,
395 ;; This function obeys the flag *getsignl-asksign-ok*, so it only does
396 ;; an asksign when *getsignl-asksign-ok* is true.
398 ;; When the limit is either zerob or zeroa, it's reasonable to return -1 or 1,
399 ;; respectively, but that causes a testsuite regression. Also when z is
400 ;; positive but limit(z,var,val) is ind, possibly this function could return 1, but
404 (setq z
(let ((preserve-direction nil
)) (limit-catch z var val
)))
406 ;; Don't call csign on ind, und, infinity, zeroa, or zerob.
407 ;; When z is either zeroa or zerob, return 0.
408 ((or (eq z
'$ind
) (eq z
'$und
) (eq z
'$infinity
)) nil
)
412 (setq sgn
(if *getsignl-asksign-ok
* ($asksign z
) ($csign z
)))
413 (cond ((eq sgn
'$pos
) 1)
418 (defun restorelim (exp)
419 (cond ((null exp
) nil
)
420 ((atom exp
) (or (and (symbolp exp
) (get exp
'limitsub
)) exp
))
421 ((and (consp (car exp
)) (eq (caar exp
) 'mrat
))
423 (cons (restorelim (cadr exp
))
424 (restorelim (cddr exp
)))))
425 (t (cons (car exp
) (mapcar #'restorelim
(cdr exp
))))))
427 ;; For each subexpression of e of the form abs(X), decide if abs(X) can be
428 ;; replaced by -X or by X. Define ans = limit(X,var, val) and
429 ;; bee = behavior(X,var,val). Cases:
431 ;; (a) negative (ans = -1) & decreasing (bee = -1), do abs(X) --> -X
432 ;; (b) positive (ans = 1) & increasing (bee = 1), do abs(X) --> X
433 ;; (c) zero (ans = 0) & decreasing (bee = -1), do abs(X) --> -X
434 ;; (d) zero (ans = 0) & X increasing (bee = 1), do abs(X) --> X
436 ;; This function misses cases such as abs(1-x^2) for x near 0.
437 ;; For that case, we get ans = 1 & bee = -1.
439 ;; If one-sided limits assumed both lower and upper bounds for the limit
440 ;; variable (something like 0 < x < tiny for a limit of x toward 0 from above),
441 ;; some subexpressions of the form abs(X) could be simplifed away before
442 ;; going through this function.
444 ;; This function was rewritten and algorithmally altered by Barton Willis in
445 ;; January 2024. Previously, it was responsible for the bug
446 ;; limit(abs(sin(x))/sin(x), x, inf) = 1. Also, the old version also prevented
447 ;; limit(cos(a*x),x,inf) from doing an asksign on a, and it gave an
448 ;; internal error for limit((abs(sin(x)*cos(x)-x^4) -x)/x^3,x,0,plus).
449 (defun mabs-subst (e var val
)
451 (cond (($mapatom e
) e
)
452 ;; Found a subexpression of the form abs(X). Decide if its OK
453 ;; to replace abs(X) by -X or by X. We could skip this when
454 ;; X is freeof var, but I'm not sure that we win by doing do
455 ((and (consp e
) (eq (caar e
) 'mabs
))
456 (setq e
(mabs-subst (cadr e
) var val
)) ;recurse on mabs-subst
457 (setq ans
(getsignl e
))
458 (setq bee
(if ans
(behavior e var val
) nil
))
460 ((and (eql ans -
1) (eql bee -
1)) (mul -
1 e
)) ;negative & decreasing
461 ((and (eql ans
1) (eql bee
1)) e
) ; positive & increasing
462 ((and (eql ans
0) (eql bee -
1)) (mul -
1 e
)) ;zero & decreasing
463 ((and (eql ans
0) (eql bee
1)) e
) ;zero & increasing
464 (t (ftake 'mabs e
)))) ; don't know
465 ;; Map mabs-subst over a subscripted function.
469 (mapcar #'(lambda (q) (mabs-subst q var val
)) (subfunsubs e
))
470 (mapcar #'(lambda (q) (mabs-subst q var val
)) (subfunargs e
))))
471 ;; Map mabs-subst over the expression.
473 (mapcar #'(lambda (q) (mabs-subst q var val
)) (cdr e
)))))))
475 ;; Called on an expression that might contain $INF, $MINF, $ZEROA, $ZEROB. Tries
476 ;; to simplify it to sort out things like inf^inf or inf+1.
478 (simpinf-ic exp
(count-general-inf exp
)))
480 (defun count-general-inf (expr)
481 (count-atoms-matching
482 (lambda (x) (or (infinityp x
) (real-epsilonp x
))) expr
))
484 (defun count-atoms-matching (predicate expr
)
485 "Count the number of atoms in the Maxima expression EXPR matching PREDICATE,
486 ignoring dummy variables and array indices."
488 ((atom expr
) (if (funcall predicate expr
) 1 0))
489 ;; Don't count atoms that occur as a limit of %integrate, %sum, %product,
491 ((member (caar expr
) dummy-variable-operators
)
492 (count-atoms-matching predicate
(cadr expr
)))
493 ;; Ignore array indices
494 ((member 'array
(car expr
)) 0)
496 for arg in
(cdr expr
)
497 summing
(count-atoms-matching predicate arg
)))))
499 (defun simpinf-ic (exp &optional infinity-count
)
501 ;; A very slow identity transformation...
504 ;; If there's only one infinity, we replace it by a variable and take the
505 ;; limit as that variable goes to infinity. Use $gensym in case we can't
506 ;; compute the answer and the limit leaks out.
507 (1 (let* ((val (or (inf-typep exp
) (epsilon-typep exp
)))
509 (expr (subst var val exp
))
510 (limit (toplevel-$limit expr var val
)))
512 ;; Now we look to see whether the computed limit is any simpler than
513 ;; what we shoved in (which we'll define as "doesn't contain EXPR as a
514 ;; subtree"). If so, return it.
515 ((not (subtree-p expr limit
:test
#'equal
))
518 ;; Otherwise, return the original form: apparently, we can't compute
519 ;; the limit we needed, and it's uglier than what we started with.
522 ;; If more than one infinity, we have to be a bit more careful.
524 (let* ((arguments (mapcar 'simpinf
(cdr exp
)))
525 (new-expression (cons (list (caar exp
)) arguments
))
528 ;; If any of the arguments are undefined, we are too.
529 ((among '$und arguments
) '$und
)
530 ;; If we ended up with something indeterminate, we punt and just return
532 ((amongl '(%limit $ind
) arguments
) exp
)
534 ;; Exponentiation & multiplication
535 ((mexptp exp
) (simpinf-expt (first arguments
) (second arguments
)))
536 ((mtimesp exp
) (simpinf-times arguments
))
538 ;; Down to at most one infinity? We do this after exponentiation to
539 ;; avoid zeroa^zeroa => 0^0, which will raise an error rather than just
540 ;; returning und. We do it after multiplication to avoid zeroa * inf =>
542 ((<= (setf infinities-left
(count-general-inf new-expression
)) 1)
543 (simpinf-ic new-expression infinities-left
))
546 ((mplusp exp
) (simpinf-plus arguments
))
549 (t new-expression
))))))
551 (defun simpinf-times (arguments)
552 (declare (special exp var val
))
553 ;; When we have a product, we need to spot that zeroa * zerob = zerob, zeroa *
554 ;; inf = und etc. Note that (SIMPINF '$ZEROA) => 0, so a nonzero atom is not
555 ;; an infinitesimal. Moreover, we can assume that each of ARGUMENTS is either
556 ;; a number, computed successfully by the recursive SIMPINF call, or maybe a
557 ;; %LIMIT noun-form (in which case, we aren't going to be able to tell the
560 ((member 0 arguments
)
562 ((find-if #'infinityp arguments
) '$und
)
563 ((every #'atom arguments
) 0)
566 ((member '$infinity arguments
)
567 (if (every #'atom arguments
)
571 (t (simplimit (cons '(mtimes) arguments
) var val
))))
573 (defun simpinf-expt (base exponent
)
574 ;; In the comments below, zero* represents one of 0, zeroa, zerob.
576 ;; TODO: In some cases we give up too early. E.g. inf^(2 + 1/inf) => inf^2
577 ;; (which should simplify to inf)
587 ((0 $zeroa $zerob
) '$und
)
588 (t (list '(mexpt) base exponent
))))
589 ;; minf^inf = infinity <== Or should it be und?
592 ;; minf^foo = minf^foo
597 ((0 $zeroa $zerob
) '$und
)
598 (t (list '(mexpt) base exponent
))))
602 ;; zero*^foo = zero*^foo
607 ((0 $zeroa $zerob
) '$und
)
608 (t (list '(mexpt) base exponent
))))
609 ;; a^b where a is pretty much anything except for a naked
610 ;; inf,minf,zeroa,zerob or 0.
613 ;; When a isn't crazy, try a^b = e^(b log(a))
614 ((not (amongl (append *infinitesimals
* *infinities
*) base
))
615 (simpinf (m^
'$%e
(m* exponent
`((%log
) ,base
)))))
617 ;; No idea. Just return what we've found so far.
618 (t (list '(mexpt) base exponent
))))))
620 (defun simpinf-plus (arguments)
621 ;; We know that none of the arguments are infinitesimals, since SIMPINF never
622 ;; returns one of them. As such, we partition our arguments into infinities
623 ;; and everything else. The latter won't have any "hidden" infinities like
624 ;; lim(x,x,inf), since SIMPINF gave up on anything containing a %lim already.
625 (let ((bigs) (others))
626 (dolist (arg arguments
)
627 (cond ((infinityp arg
) (push arg bigs
))
628 (t (push arg others
))))
630 ;; inf + minf or the like
631 ((cdr (setf bigs
(delete-duplicates bigs
))) '$und
)
632 ;; inf + smaller + stuff
634 ;; I don't think this can happen, since SIMPINF goes back to the start if
635 ;; there are fewer than two infinities in the arguments, but let's be
637 (t (cons '(mplus) others
)))))
639 ;; Simplify expression with zeroa or zerob.
640 (defun simpab (small)
641 (cond ((null small
) ())
642 ((member small
'($zeroa $zerob $inf $minf $infinity
) :test
#'eq
) small
)
643 ((not (free small
'$ind
)) '$ind
) ;Not exactly right but not
644 ((not (free small
'$und
)) '$und
) ;causing trouble now.
645 ((mapatom small
) small
)
646 (t (let ((preserve-direction t
)
647 (new-small (subst (m^
'$inf -
1) '$zeroa
648 (subst (m^
'$minf -
1) '$zerob small
))))
649 (simpinf new-small
)))))
652 ;;;*I* INDICATES: T => USE LIMIT1,THINK, NIL => USE SIMPLIMIT.
653 (defun limit (exp var val
*i
*)
655 ((among '$und exp
) '$und
)
658 ((not (among var exp
))
659 (cond ((amongl '($inf $minf $infinity $ind
) exp
)
661 ((amongl '($zeroa $zerob
) exp
)
662 ;; Simplify expression with zeroa or zerob.
666 (t (putlimval exp
(cond ((and limit-using-taylor
669 (taylim exp var val
*i
*))
670 ((ratp exp var
) (ratlim exp
))
671 ((or (eq *i
* t
) (radicalp exp var
))
672 (limit1 exp var val
))
674 (cond ((or (mtimesp exp
) (mexptp exp
))
675 (limit1 exp var val
))
676 (t (simplimit exp var val
))))
677 (t (simplimit exp var val
)))))))
679 (defun limitsimp (exp var
)
680 (limitsimp-expt (sin-sq-cos-sq-sub exp
) var
))
682 ;; if var appears in base and power of expt,
683 ;; push var into power of of expt
684 (defun limitsimp-expt (exp var
)
685 (cond ((or (atom exp
)
687 (freeof var exp
)) exp
)
689 (not (freeof var
(cadr exp
)))
690 (not (freeof var
(caddr exp
))))
691 (m^
'$%e
(simplify `((%log
) ,exp
))))
692 (t (subst0 (cons (cons (caar exp
) ())
693 (mapcar #'(lambda (x)
694 (limitsimp-expt x var
))
698 (defun gather-args-of (e fn x
)
699 (cond (($mapatom e
) nil
)
700 ((and (eq fn
(caar e
)) (not (freeof x e
))) (cdr e
))
702 (remove-duplicates (reduce #'append
703 (mapcar #'(lambda (q)
704 (gather-args-of q fn x
)) (cdr e
))) :test
#'alike1
))))
706 ;; Replace every subexpression of e of the form cos(X)^2 + sin(X)^2, where X
707 ;; depends on x, by 1.
708 (defun sin-sq-cos-sq-sub (e &optional
(x var
))
709 (let ((ccc nil
) (z) (ee))
710 (cond (($mapatom e
) e
)
712 (setq ccc
(gather-args-of e
'%cos x
))
715 (setq ee
(maxima-substitute z
(power (ftake '%cos g
) 2) e
))
716 (setq ee
(maxima-substitute (sub 1 z
) (power (ftake '%sin g
) 2) ee
))
717 (when (freeof z
(sratsimp ee
))
720 ;; maybe this isn't needed, but it's not wrong.
721 ((eq 'mqapply
(caar e
))
722 (subftake (caar (second e
))
723 (mapcar #'(lambda (q) (sin-sq-cos-sq-sub q x
)) (subfunsubs e
))
724 (mapcar #'(lambda (q) (sin-sq-cos-sq-sub q x
)) (subfunargs e
))))
725 ;; map sin-sq-cos-sq-sub over the args
727 (fapply (caar e
) (mapcar #'(lambda (q) (sin-sq-cos-sq-sub q x
)) (cdr e
)))))))
729 (defun expand-trigs (x var
)
732 ((and (or (eq (caar x
) '%sin
)
734 (not (free (cadr x
) var
)))
736 ((member 'array
(car x
))
737 ;; Some kind of array reference. Return it.
739 (t (simplify (cons (ncons (caar x
))
740 (mapcar #'(lambda (x)
741 (expand-trigs x var
))
744 ;; The functionality of tansc has been blended into extra-simp, so we
745 ;; could orphan tansc. But extra-simp does more than tansc and tansc is
746 ;; called by the definite integration code and by the limit code. So we'll
749 (cond ((not (involve e
750 '(%cot %csc %binomial
751 %sec %coth %sech %csch
752 %acot %acsc %asec %acoth
754 %jacobi_ns %jacobi_nc %jacobi_cs
755 %jacobi_ds %jacobi_dc
)))
757 (t (sratsimp (tansc1 e
)))))
759 (defun tansc1 (e &aux tem
)
761 ((and (setq e
(cons (car e
) (mapcar 'tansc1
(cdr e
)))) ()))
762 ((setq tem
(assoc (caar e
) '((%cot . %tan
) (%coth . %tanh
)
763 (%sec . %cos
) (%sech . %cosh
)
764 (%csc . %sin
) (%csch . %sinh
)) :test
#'eq
))
765 (tansc1 (m^
(list (ncons (cdr tem
)) (cadr e
)) -
1.
)))
766 ((setq tem
(assoc (caar e
) '((%jacobi_nc . %jacobi_cn
)
767 (%jacobi_ns . %jacobi_sn
)
768 (%jacobi_cs . %jacobi_sc
)
769 (%jacobi_ds . %jacobi_sd
)
770 (%jacobi_dc . %jacobi_cd
)) :test
#'eq
))
771 ;; Converts Jacobi elliptic function to its reciprocal
773 (tansc1 (m^
(list (ncons (cdr tem
)) (cadr e
) (third e
)) -
1.
)))
774 ((setq tem
(member (caar e
) '(%sinh %cosh %tanh
) :test
#'eq
))
775 (let (($exponentialize t
))
777 ((setq tem
(assoc (caar e
) '((%acsc . %asin
) (%asec . %acos
)
778 (%acot . %atan
) (%acsch . %asinh
)
779 (%asech . %acosh
) (%acoth . %atanh
)) :test
#'eq
))
780 (list (ncons (cdr tem
)) (m^t
(cadr e
) -
1.
)))
781 ((and (eq (caar e
) '%binomial
) (among var
(cdr e
)))
782 (m// `((mfactorial) ,(cadr e
))
783 (m* `((mfactorial) ,(m+t
(cadr e
) (m- (caddr e
))))
784 `((mfactorial) ,(caddr e
)))))
787 ;; Like TANSC but dependency on VAR is explicit. Use this instead of
788 ;; TANSC when possible.
789 (defun tansc-var (e var1
)
790 (cond ((not (involve-var e var1
791 '(%cot %csc %binomial
792 %sec %coth %sech %csch
793 %acot %acsc %asec %acoth
795 %jacobi_ns %jacobi_nc %jacobi_cs
796 %jacobi_ds %jacobi_dc
)))
800 ((tansc1-v (e &aux tem
)
801 ;; Copy of TANSC1, but VAR replaced by VAR1
804 ((and (setq e
(cons (car e
) (mapcar #'tansc1-v
(cdr e
))))
806 ((setq tem
(assoc (caar e
)
807 '((%cot . %tan
) (%coth . %tanh
)
808 (%sec . %cos
) (%sech . %cosh
)
809 (%csc . %sin
) (%csch . %sinh
))
811 (tansc1-v (m^
(list (ncons (cdr tem
)) (cadr e
)) -
1.
)))
812 ((setq tem
(assoc (caar e
)
813 '((%jacobi_nc . %jacobi_cn
)
814 (%jacobi_ns . %jacobi_sn
)
815 (%jacobi_cs . %jacobi_sc
)
816 (%jacobi_ds . %jacobi_sd
)
817 (%jacobi_dc . %jacobi_cd
))
819 ;; Converts Jacobi elliptic function to its reciprocal
821 (tansc1-v (m^
(list (ncons (cdr tem
)) (cadr e
) (third e
))
823 ((setq tem
(member (caar e
) '(%sinh %cosh %tanh
) :test
#'eq
))
824 (let (($exponentialize t
))
826 ((setq tem
(assoc (caar e
)
827 '((%acsc . %asin
) (%asec . %acos
)
828 (%acot . %atan
) (%acsch . %asinh
)
829 (%asech . %acosh
) (%acoth . %atanh
))
831 (list (ncons (cdr tem
)) (m^t
(cadr e
) -
1.
)))
832 ((and (eq (caar e
) '%binomial
)
833 (among var1
(cdr e
)))
834 (m// `((mfactorial) ,(cadr e
))
835 (m* `((mfactorial) ,(m+t
(cadr e
) (m- (caddr e
))))
836 `((mfactorial) ,(caddr e
)))))
838 (sratsimp (tansc1-v e
))))))
841 (cond ((not (involve ex
'(%sin %cos %tan %asin %acos %atan
842 %sinh %cosh %tanh %asinh %acosh %atanh
)))
848 ((eq (caar ex
) '%sinh
)
849 (m// (m+ (m^
'$%e
(cadr ex
)) (m- (m^
'$%e
(m- (cadr ex
)))))
851 ((eq (caar ex
) '%cosh
)
852 (m// (m+ (m^
'$%e
(cadr ex
)) (m^
'$%e
(m- (cadr ex
))))
854 ((and (member (caar ex
)
855 '(%sin %cos %tan %asin %acos %atan %sinh
856 %cosh %tanh %asinh %acosh %atanh
) :test
#'eq
)
859 (t (cons (car ex
) (mapcar #'hyperex0
(cdr ex
))))))
864 ;;Used by tlimit also.
865 (defun limit1 (exp var val
)
867 (let ((lhprogress? lhp?
)
870 (cond ((setq ans
(and (not (atom exp
)) (getlimval exp
)))
872 ((and (not (infinityp val
)) (setq ans
(simplimsubst val exp
)))
875 ;;;NUMDEN* => (values numerator denominator)
876 (multiple-value-bind (n dn
)
878 (cond ((not (among var dn
))
879 (return (simplimit (m// (simplimit n var val
) dn
) var val
)))
881 (return (simplimit (m* n
(simplimexpt dn -
1 (simplimit dn var val
) -
1)) var val
)))
883 (/#alike n
(car lhprogress?
))
884 (/#alike dn
(cdr lhprogress?
)))
885 (throw 'lhospital nil
)))
886 (return (limit2 n dn var val
))))))
889 ;; If e/f is free of var (a special), return true. This code
890 ;; assumes that f is not zero. First, we test if e & f
891 ;; are alike--this check is relatively fast; second, we check
892 ;; if e/f is free of var.
894 (or (alike1 e f
) (freeof var
(sratsimp (div e f
)))))
896 (defun limit2 (n dn var val
)
897 (prog (n1 d1 lim-sign gcp sheur-ans
)
898 (setq n
(hyperex n
) dn
(hyperex dn
))
899 (setq d1
(limit dn var val nil
)
900 n1
(limit n var val nil
))
901 (cond ((or (null n1
) (null d1
)) (return nil
))
902 (t (setq n1
(sratsimp n1
) d1
(sratsimp d1
))))
903 (cond ((or (involve n
'(mfactorial)) (involve dn
'(mfactorial)))
904 (let ((ans (limfact2 n dn var val
)))
905 (cond (ans (return ans
))))))
906 (cond ((and (zerop2 n1
) (zerop2 d1
))
907 (cond ((not (equal (setq gcp
(gcpower n dn
)) 1))
908 (return (colexpt n dn gcp
)))
909 ((and (real-epsilonp val
)
911 (not (free dn
'%log
)))
912 (return (liminv (m// n dn
))))
913 ((setq n1
(try-lhospital-quit n dn nil
))
915 ((and (zerop2 n1
) (not (member d1
'($ind $und
) :test
#'eq
))) (return 0))
917 (setq n1
(ridofab n1
))
918 (return (simplimtimes `(,n1
,(simplimexpt dn -
1 d1 -
1))))))
919 (setq n1
(ridofab n1
))
920 (setq d1
(ridofab d1
))
921 (cond ((or (eq d1
'$und
)
922 (and (eq n1
'$und
) (not (real-infinityp d1
))))
925 ;; At this point we have n1/$ind. Look if n1 is one of the
926 ;; infinities or zero.
927 (cond ((and (infinityp n1
) (eq ($sign dn
) '$pos
))
929 ((and (infinityp n1
) (eq ($sign dn
) '$neg
))
930 (return (simpinf (m* -
1 n1
))))
932 (or (eq ($sign dn
) '$pos
)
933 (eq ($sign dn
) '$neg
)))
936 ((eq n1
'$ind
) (return (cond ((infinityp d1
) 0)
939 ((and (real-infinityp d1
) (member n1
'($inf $und $minf
) :test
#'eq
))
940 (cond ((and (not (atom dn
)) (not (atom n
))
941 (cond ((not (equal (setq gcp
(gcpower n dn
)) 1))
942 (return (colexpt n dn gcp
)))
944 (or (involve dn
'(mfactorial %gamma
))
945 (involve n
'(mfactorial %gamma
))))
946 (return (limfact n dn
))))))
947 ((eq n1 d1
) (setq lim-sign
1) (go cp
))
948 (t (setq lim-sign -
1) (go cp
))))
949 ((and (infinityp d1
) (infinityp n1
))
950 (setq lim-sign
(if (or (eq d1
'$minf
) (eq n1
'$minf
)) -
1 1))
952 (t (return (simplimtimes `(,n1
,(m^ d1 -
1))))))
953 cp
(setq n
($expand n
) dn
($expand dn
))
955 (let ((new-n (m+l
(maxi (cdr n
)))))
956 (cond ((not (alike1 new-n n
))
957 (return (limit (m// new-n dn
) var val
'think
))))
961 (let ((new-dn (m+l
(maxi (cdr dn
)))))
962 (cond ((not (alike1 new-dn dn
))
963 (return (limit (m// n new-dn
) var val
'think
))))
966 (setq sheur-ans
(sheur0 n1 d1
))
967 (cond ((or (member sheur-ans
'($inf $zeroa
) :test
#'eq
)
968 (free sheur-ans var
))
969 (return (simplimtimes `(,lim-sign
,sheur-ans
))))
970 ((and (alike1 sheur-ans dn
)
972 ((member (setq n1
(cond ((expfactorp n1 d1
) (expfactor n1 d1 var
))
974 '($inf $zeroa
) :test
#'eq
)
976 ((not (null (setq n1
(cond ((expfactorp n dn
) (expfactor n dn var
))
979 ((and (alike1 sheur-ans dn
) (not (mplusp n
))))
980 ((not (alike1 sheur-ans
(m// n dn
)))
981 (return (simplimit (m// ($expand
(m// n sheur-ans
))
982 ($expand
(m// dn sheur-ans
)))
985 (cond ((and (not (and (eq val
'$inf
) (expp n
) (expp dn
)))
986 (setq n1
(try-lhospital-quit n dn nil
))
991 ;; Test whether both n and dn have form
992 ;; product of poly^poly.
994 ;; It's important that both n and dn be free of extended real numbers
995 ;; (minf, zerob, ...), so we check for that too (see bug #4222).
996 (defun expfactorp (n dn
)
997 (do ((llist (append (cond ((mtimesp n
) (cdr n
))
999 (cond ((mtimesp dn
) (cdr dn
))
1002 (exp? t
) ;IS EVERY ELEMENT SO FAR
1003 (factor nil
)) ;A POLY^POLY?
1007 (setq factor
(car llist
))
1010 (not (amongl (list '$minf
'$zerob
'$zeroa
'$ind
'$und
'$inf
'$infinity
) factor
))
1011 (or (polyinx factor var
())
1012 (and (mexptp factor
)
1013 (polyinx (cadr factor
) var
())
1014 (polyinx (caddr factor
) var
())))))))
1016 (defun expfactor (n dn var
) ;Attempts to evaluate limit by grouping
1017 (prog (highest-deg) ; terms with similar exponents.
1018 (let ((new-exp (exppoly n
))) ;exppoly unrats expon
1019 (setq n
(car new-exp
) ;and rtns deg of expons
1020 highest-deg
(cdr new-exp
)))
1021 (cond ((null n
) (return nil
))) ;nil means expon is not
1022 (let ((new-exp (exppoly dn
))) ;a rat func.
1023 (setq dn
(car new-exp
)
1024 highest-deg
(max highest-deg
(cdr new-exp
))))
1025 (cond ((or (null dn
)
1026 (= highest-deg
0)) ; prevent infinite recursion
1030 (degree highest-deg
(1- degree
))
1037 (limit (m// numerator denominator
)
1041 (let ((newnumer-factor (get-newexp&factors
1045 (setq numerator
(car newnumer-factor
)
1046 numfactors
(cdr newnumer-factor
)))
1047 (let ((newdenom-factor (get-newexp&factors
1051 (setq denominator
(car newdenom-factor
)
1052 denfactors
(cdr newdenom-factor
)))
1053 (setq answer
(simplimit (list '(mexpt)
1055 (m// numfactors denfactors
))
1056 (cond ((> degree
0) var
)
1060 (cond ((member answer
'($ind $und
) :test
#'equal
)
1061 ;; cannot handle limit(exp(x*%i)*x, x, inf);
1063 ((member answer
'($inf $minf
) :test
#'equal
)
1064 ;; 0, zeroa, zerob are passed through to next iteration
1065 (return (simplimtimes (list (m// numerator denominator
) answer
)))))))))
1067 (defun exppoly (exp) ;RETURNS EXPRESSION WITH UNRATTED EXPONENTS
1071 (exp (cond ((mtimesp exp
)
1075 ((null exp
) (cons new-exp highest-deg
))
1076 (setq factor
(car exp
))
1078 (m* (cond ((or (not (mexptp factor
))
1079 (not (ratp (caddr factor
) var
)))
1081 (t (setq highest-deg
1083 (ratdegree (caddr factor
))))
1084 (m^
(cadr factor
) (unrat (caddr factor
)))))
1087 (defun unrat (exp) ;RETURNS UNRATTED EXPRESION
1088 (multiple-value-bind (n d
)
1090 (let ((tem ($divide n d
)))
1092 (m// (caddr tem
) d
)))))
1094 (defun get-newexp&factors
(exp degree var
) ;RETURNS (CONS NEWEXP FACTORS)
1095 (do ((terms (cond ((mtimesp exp
)(cdr exp
)) ; SUCH THAT
1096 (t (ncons exp
))) ; NEWEXP*FACTORS^(VAR^DEGREE)
1097 (cdr terms
)) ; IS EQUAL TO EXP.
1104 (setq factor
(car terms
))
1105 (cond ((not (mexptp factor
))
1107 (setq factors
(m* factor factors
)))
1108 (t (setq newexp
(m* factor newexp
)))))
1110 (= (ratdegree (caddr factor
))
1112 (setq factors
(m* (m^
(cadr factor
)
1113 (leading-coef (caddr factor
)))
1115 newexp
(m* (m^
(cadr factor
)
1117 (m* (leading-coef (caddr factor
))
1120 (t (setq newexp
(m* factor newexp
))))))
1122 (defun leading-coef (rat)
1123 (ratlim (m// rat
(m^ var
(ratdegree rat
)))))
1125 (defun ratdegree (rat)
1126 (multiple-value-bind (n d
)
1128 (- (deg n
) (deg d
))))
1130 (defun limfact2 (n d var val
)
1131 (let ((n1 (reflect0 n var val
))
1132 (d1 (reflect0 d var val
)))
1133 (cond ((and (alike1 n n1
)
1136 (t (limit (m// n1 d1
) var val
'think
)))))
1138 ;; takes expression and returns operator at front with all flags removed
1139 ;; except array flag.
1140 ;; array flag must match for alike1 to consider two things to be the same.
1141 ;; ((MTIMES SIMP) ... ) => (MTIMES)
1142 ;; ((PSI SIMP ARRAY) 0) => (PSI ARRAY)
1143 (defun operator-with-array-flag (exp)
1144 (cond ((member 'array
(car exp
) :test
#'eq
)
1145 (list (caar exp
) 'array
))
1146 (t (list (caar exp
)))))
1148 (defun reflect0 (exp var val
)
1149 (cond ((atom exp
) exp
)
1150 ((and (eq (caar exp
) 'mfactorial
)
1151 (let ((argval (limit (cadr exp
) var val
'think
)))
1152 (or (eq argval
'$minf
)
1153 (and (numberp argval
)
1155 (reflect (cadr exp
)))
1156 (t (cons (operator-with-array-flag exp
)
1159 (reflect0 term var val
)))
1162 (defun reflect (arg)
1165 (m^
(list (ncons 'mfactorial
)
1169 (m^
(list (ncons '%sin
)
1173 (defun limfact (n d
)
1175 (setq n
(stirling0 n
)
1177 (setq ans
(toplevel-$limit
(m// n d
) var
'$inf
))
1178 (cond ((and (atom ans
)
1179 (not (member ans
'(und ind
) :test
#'eq
))) ans
)
1180 ((eq (caar ans
) '%limit
) ())
1183 ;; substitute asymptotic approximations for gamma, factorial, and
1185 (defun stirling0 (e)
1187 ((and (setq e
(cons (car e
) (mapcar 'stirling0
(cdr e
))))
1189 ((eq (caar e
) '%gamma
)
1190 (let ((ans (limit (cadr e
) var val
'think
)))
1191 (cond ((memq ans
'($inf $minf $infinity
))
1192 (stirling (cadr e
)))
1194 (add (div 1 (cadr e
)) (mul -
1 '$%gamma
)))
1195 ((and (integerp ans
) (< ans
0))
1196 (div (if (oddp ans
) -
1 1)
1197 (mul (ftake 'mfactorial
(mul -
1 ans
)) (sub (cadr e
) ans
))))
1198 (t (ftake '%gamma
(cadr e
))))))
1200 ((eq (caar e
) 'mfactorial
)
1201 (let ((n (limit (cadr e
) var val
'think
)))
1203 (stirling (add 1 (cadr e
))))
1204 ((and (integerp n
) (< n
0))
1206 (div (if (oddp n
) 1 -
1)
1207 (mul (ftake 'mfactorial
(sub n
1)) (add var n
))))
1209 ((and (eq (caar e
) 'mqapply
) ;; polylogarithm
1210 (eq (subfunname e
) '$li
)
1211 (let ((arglim (limit (car (subfunargs e
)) var val
'think
)))
1212 (or (eq arglim
'$inf
) (eq arglim
'$minf
)))
1213 (integerp (car (subfunsubs e
))))
1214 (li-asymptotic-expansion (m- (car (subfunsubs e
)) 1)
1215 (car (subfunsubs e
))
1216 (car (subfunargs e
))))
1220 "Return sqrt(2*%pi/x)*(x/%e)^x, the Stirling approximation of
1221 gamma(x). The argument x can be any valid expression."
1222 (mul (power (div (mul 2 '$%pi
) x
)
1227 (defun no-err-sub (v e
&aux ans
)
1228 (let ((errorsw t
) (*zexptsimp? t
)
1230 ;; Don't print any error messages
1232 ;; Should we just use IGNORE-ERRORS instead HANDLER-CASE here? I
1233 ;; (rtoy) am choosing the latter so that unexpected errors will
1234 ;; actually show up instead of being silently discarded.
1236 (setq ans
(catch 'errorsw
1238 (sratsimp (subin v e
)))))
1241 (cond ((null ans
) t
) ; Ratfun package returns NIL for failure.
1244 ;; Like NO-ERR-SUB but dependency on VAR is explicit. Use this
1245 ;; instead when possible.
1246 (defun no-err-sub-var (v e var1
&aux ans
)
1247 (let ((errorsw t
) (*zexptsimp? t
)
1249 ;; Don't print any error messages
1251 ;; Should we just use IGNORE-ERRORS instead HANDLER-CASE here? I
1252 ;; (rtoy) am choosing the latter so that unexpected errors will
1253 ;; actually show up instead of being silently discarded.
1255 (setq ans
(catch 'errorsw
1257 (sratsimp (subin-var v e var1
)))))
1260 (cond ((null ans
) t
) ; Ratfun package returns NIL for failure.
1263 (defun simplim%mabs
(e x pt
)
1264 (let ((lim (limit (cadr e
) x pt
'think
)))
1265 (cond ((or (eq lim
'$zeroa
) (eql lim
0) (eq lim
'$ind
)) lim
)
1266 ((eq lim
'$zerob
) '$zeroa
)
1267 ((eq lim
'$und
) (throw 'limit nil
))
1268 ((or (eq lim
'$minf
) (eq lim
'$inf
) (eq lim
'$infinity
)) '$inf
)
1269 (t (ftake 'mabs lim
)))))
1270 (setf (get 'mabs
'simplim%function
) 'simplim%mabs
)
1272 (defun extended-real-p (e)
1273 (member e
(list '$minf
'$zerob
'$zeroa
'$ind
'$und
'$inf
'$infinity
)))
1275 ;; Evaluate the limit of e as var (special) approaches v using direct substitution.
1276 ;; This function is the last of the chain of methods tried by limit. As such it
1277 ;; shouldn't call higher level functions such as simplimit or limit--doing so
1278 ;; would risk creating an infinite loop.
1280 ;; This function special cases sums, products, and powers. It declines to use
1281 ;; direct substitution on any expression whose main operator has a simplim%function
1282 ;; function. Generally, limits of functions that have a simplim%function should be
1283 ;; handled by those specialized functions, not by simplimsubst. Having said this,
1284 ;; it's OK for simplimsubst to special case an operator and do direct substitution
1285 ;; when its OK. The cases of log, cosine, and sine happen often enough that these
1286 ;; functions are special cased.
1288 ;; Possibly simplimsubst should decline to do direct substitution on functions
1289 ;; that have a limit function, for example inverse_jacobi_ns.
1291 ;; For all other kinds of expressions, this function assumes that if substitution
1292 ;; doesn't result in an error (division by zero, for example), the function
1293 ;; is continuous at the limit point. That's dodgy. This dodginess underscores the
1294 ;; need to define a simplim%function functions that are not continuous on their
1297 ;; This function sometimes receives an un-simplified expression. Maybe they should be
1298 ;; simplified, maybe not.
1300 ;; Locally setting $numer and $%enumer to nil keeps some limits, for example
1301 ;; limit(sin(x)/x,x,0) from returning 1.0 when numer is true. (Barton Willis)
1303 (defun simplimsubst (v e
)
1304 (let ((ans nil
) ($numer nil
) ($%enumer nil
) (ee))
1306 ;; When e is a number, return it.
1308 ;; When e is a mapatom, substitute v for var and return.
1309 (($mapatom e
) ($substitute v var e
))
1310 ;; Special case mexpt expressions. Decline direct substitution for
1312 ((and (mexptp e
) (not (extended-real-p v
)))
1313 (let ((x (simplimsubst v
(second e
)))
1314 (n (simplimsubst v
(third e
))))
1315 ;; Decline direct substitution (DS) for 0^negative. Also decline
1316 ;; DS when x is on the negative real axis and n isn't an integer.
1317 ;; Additionally, we require that DS is OK for both x & n.
1318 (if (and x n
(not (and (zerop2 x
) (eq t
(mgqp 0 n
)))) ; not 0^negative
1319 (or (off-negative-real-axisp x
) (integerp n
)))
1320 (ftake 'mexpt x n
) nil
)))
1321 ;; Special case product and sum expressions. Again, we decline direct
1322 ;; substitution for extended reals.
1323 ((and (or (mplusp e
) (mtimesp e
)) (not (extended-real-p v
)))
1324 (setq ee
(mapcar #'(lambda(q) (simplimsubst v q
)) (cdr e
)))
1325 (if (some #'(lambda (q) (eq q nil
)) ee
) nil
1326 (simplifya (cons (list (caar e
)) ee
) t
)))
1327 ;; Decline direct substitution for sums, products, and powers for
1328 ;; the extended real case.
1329 ((and (or (mexptp e
) (mplusp e
) (mtimesp e
)) (extended-real-p v
))
1331 ;; Special case log expressions--possibly the log case happens
1332 ;; often enough to make this worthwhile.
1333 ((and (consp e
) (consp (car e
)) (eq '%log
(caar e
)))
1334 (let ((w (simplimsubst v
(cadr e
))))
1335 (if (and w
(off-negative-real-axisp w
)) (ftake '%log w
) nil
)))
1336 ;; Special case %cos and %sin expressions--we could special case others.
1337 ;; The lenient-realp check declines direct substitution for complex arguments--
1338 ;; this check could be relaxed.
1339 ((and (consp e
) (consp (car e
)) (or (member (caar e
) (list '%cos
'%sin
))))
1340 (let ((op (caar e
)))
1341 (setq e
(simplimsubst v
(second e
)))
1342 (if (and e
(lenient-realp e
) (not (extended-real-p e
))) (ftake op e
) nil
)))
1343 ;; Don't use direct substitution on expressions whose main operator has
1344 ;; a simplim%function.
1345 ((and (consp e
) (consp (car e
)) (get (caar e
) 'simplim%function
)) nil
)
1346 ;; The function no-err-sub returns true when there is an error
1347 ((not (eq t
(setq ans
(no-err-sub (ridofab v
) e
))))
1348 ;; Previously the condition (zerop2 ans) was (=0 ($radcan ans)). In
1349 ;; December 2021, the testsuite + the share testsuite only gives ans = 0,
1350 ;; making the radcan unneeded.
1351 (cond ((and (member v
'($zeroa $zerob
) :test
#'eq
) (zerop2 ans
))
1352 (setq ans
(behavior e var v
))
1353 (cond ((eql ans
1) '$zeroa
)
1354 ((eql ans -
1) '$zerob
)
1355 (t nil
))) ;behavior can't find direction
1357 ;; direct substitution fails, so return nil.
1360 ;;;returns (cons numerator denominator)
1362 (let ((e (factor (simplify e
)))
1368 (mapc #'forq
(cdr e
)))
1374 (setq numer
(car numer
)))
1376 (setq numer
(m*l numer
))))
1380 (setq denom
(car denom
)))
1382 (setq denom
(m*l denom
))))
1383 (values (factor numer
) (factor denom
))))
1385 ;;;FACTOR OR QUOTIENT
1386 ;;;Setq's the special vars numer and denom from numden*
1388 (cond ((and (mexptp e
)
1389 (not (freeof var e
))
1390 (null (pos-neg-p (caddr e
))))
1391 (push (m^
(cadr e
) (m* -
1.
(caddr e
))) denom
))
1392 (t (push e numer
))))
1394 ;;;Predicate to tell whether an expression is pos,zero or neg as var -> val.
1395 ;;;returns T if pos,zero. () if negative or don't know.
1396 (defun pos-neg-p (exp)
1397 (let ((ans (limit exp var val
'think
)))
1398 (cond ((and (not (member ans
'($und $ind $infinity
) :test
#'eq
))
1399 (equal ($imagpart ans
) 0))
1400 (let ((sign (getsignl ans
)))
1401 (cond ((or (equal sign
1)
1404 ((equal sign -
1) nil
))))
1407 (declare-top (unspecial n dn
))
1410 (cond ((radicalp e var
) nil
)
1411 ((member (caar e
) '(%log %sin %cos %tan %sinh %cosh %tanh mfactorial
1412 %asin %acos %atan %asinh %acosh %atanh
) :test
#'eq
) nil
)
1414 ((do ((e (cdr e
) (cdr e
)))
1416 (and (expp (car e
)) (return t
))))))
1420 (radicalp (cadr e
) var
)
1421 (among var
(caddr e
))
1422 (radicalp (caddr e
) var
)))
1425 (defun gcpower (a b
)
1426 ($gcd
(getexp a
) (getexp b
)))
1429 (cond ((and (mexptp exp
)
1430 (free (caddr exp
) var
)
1431 (eq (ask-integer (caddr exp
) '$integer
) '$yes
))
1433 ((mtimesp exp
) (getexplist (cdr exp
)))
1436 (defun getexplist (list)
1437 (cond ((null (cdr list
))
1438 (getexp (car list
)))
1439 (t ($gcd
(getexp (car list
))
1440 (getexplist (cdr list
))))))
1442 (defun limroot (exp power
)
1443 (cond ((or (atom exp
) (not (member (caar exp
) '(mtimes mexpt
) :test
#'eq
)))
1444 (limroot (list '(mexpt) exp
1) power
)) ;This is strange-JIM.
1445 ((mexptp exp
) (m^
(cadr exp
)
1446 (sratsimp (m* (caddr exp
) (m^ power -
1.
)))))
1447 (t (m*l
(mapcar #'(lambda (x)
1451 ;;NUMERATOR AND DENOMINATOR HAVE EXPONENTS WITH GCD OF GCP.
1452 ;;; Used to call simplimit but some of the transformations used here
1453 ;;; were not stable w.r.t. the simplifier, so try keeping exponent separate
1456 (defun colexpt (n dn gcp
)
1457 (let ((bas (m* (limroot n gcp
) (limroot dn
(m* -
1 gcp
))))
1460 (setq baslim
(limit bas var val
'think
))
1461 (setq expolim
(limit expo var val
'think
))
1462 (simplimexpt bas expo baslim expolim
)))
1464 ;; this function is responsible for the following bug:
1465 ;; limit(x^2 + %i*x, x, inf) -> inf (should be infinity)
1467 (setq e
(sratsimp ($trigreduce e
)))
1468 (cond ((member val
'($inf $infinity
) :test
#'eq
)
1469 (setq e
(maxima-substitute (m^t
'x -
1) var e
)))
1471 (setq e
(maxima-substitute (m^t -
1 (m^t
'x -
1)) var e
)))
1473 (setq e
(maxima-substitute (m- 'x
) var e
)))
1475 (setq e
(maxima-substitute 'x var e
)))
1476 ((setq e
(maxima-substitute (m+t
'x val
) var e
))))
1477 (destructuring-let* ((e (let (($ratfac
()))
1478 ($rat
(sratsimp e
) 'x
)))
1486 (sratsimp (m// ($ratdisrep
`(,h
,(locoef n g
) .
1))
1487 ($ratdisrep
`(,h
,(locoef d g
) .
1))))))
1489 (cond ((not (member val
'($zerob $zeroa $inf $minf
) :test
#'eq
))
1491 ((not (equal ($imagpart e
) 0))
1493 ((null (setq e
(getsignl ($realpart e
))))
1495 ((equal e
1) '$zeroa
)
1496 ((equal e -
1) '$zerob
)
1499 ((not (member val
'($zerob $zeroa $infinity $inf $minf
) :test
#'eq
))
1501 ((eq val
'$infinity
) '$infinity
)
1502 ((not (equal ($imagpart e
) 0)) '$infinity
)
1503 ((null (setq e
(getsignl ($realpart e
))))
1506 ((equal e -
1) '$minf
)
1510 (if (or (atom n
) (not (eq (car n
) x
)))
1515 (if (or (atom n
) (not (eq (car n
) x
)))
1519 ;; This function tries to determine the increasing/decreasing
1520 ;; behavior of an expression exp with respect to some variable var.
1521 ;; val determines the mode:
1522 ;; - $zeroa: From "positive zero" towards the right
1523 ;; - $zerob: From "negative zero" towards the left
1524 ;; - $inf: From "positive infinity" towards the left
1525 ;; - $minf: From "negative infinity" towards the right
1526 ;; Return values are -1 (decreasing), +1 (increasing) or 0 (don't know).
1527 (defun behavior (exp var val
)
1528 ;; $inf/$minf is handled by substituting 1/var for var
1529 ;; and setting val to $zeroa/$zerob.
1530 (cond ((real-infinityp val
)
1532 ((eq val
'$inf
) '$zeroa
)
1533 ((eq val
'$minf
) '$zerob
))
1534 exp
(sratsimp (subin (m^ var -
1) exp
)))))
1536 ((not (member val
'($zeroa $zerob $inf $minf
)))
1538 ;; Shortcut for constants.
1541 ;; Shortcut for the variable itself.
1543 (if (member val
'($zeroa $minf
) :test
#'eq
) 1 -
1))
1544 ;; This limits the recursion depth of the function.
1545 ;; Before giving up, always try behavior-by-diff, which doesn't recurse.
1546 ((= *behavior-count-now
* +behavior-count
+)
1547 (behavior-by-diff exp var val
))
1549 (let ((*behavior-count-now
* (1+ *behavior-count-now
*)) pair sign ans
)
1551 ;; Pull out constant factors with known signs from a product:
1552 ;; behavior(c * f(x)) = signum(c) * behavior(f(x))
1554 (prog2 (setq pair
(partition exp var
1))
1555 (not (equal (car pair
) 1))))
1556 (setq sign
(getsignl (car pair
)))
1557 (if (not (fixnump sign
))
1559 (mul sign
(behavior (cdr pair
) var val
))))
1560 ;; Pull out constant terms from a sum:
1561 ;; behavior(c + f(x)) = behavior(f(x))
1563 (prog2 (setq pair
(partition exp var
0))
1564 (not (equal (car pair
) 0))))
1565 (behavior (cdr pair
) var val
))
1566 ;; Handle f(x)^c for the case f(0) = 0 and c > 0
1567 ;; (probably other cases could be handled, too)
1569 (free (caddr exp
) var
)
1570 (=0 (no-err-sub 0 exp
))
1571 (equal (getsignl (caddr exp
)) 1)
1572 (not (equal 0 (setq ans
(behavior-expt (cadr exp
) (caddr exp
))))))
1575 ;; behavior(1 / f(x)) = -behavior(f(x))
1576 ((equal ($num exp
) 1)
1577 (- (behavior ($denom exp
) var val
)))
1578 ;; Handle c^f(x) for c > 1:
1579 ;; behavior(c^f(x)) = behavior(f(x))
1581 (free (cadr exp
) var
)
1582 (equal 1 (getsignl (m- (cadr exp
) 1)))
1583 (not (equal 0 (setq ans
(behavior (caddr exp
) var val
)))))
1585 ;; Handle c^f(x) for 0 < c < 1:
1586 ;; behavior(c^f(x)) = -behavior(f(x))
1588 (free (cadr exp
) var
)
1589 (equal 1 (getsignl (cadr exp
)))
1590 (equal -
1 (getsignl (m- (cadr exp
) 1)))
1591 (not (equal 0 (setq ans
(behavior (caddr exp
) var val
)))))
1593 ;; atan, erf, sinh and tanh are monotonically increasing,
1594 ;; so their behavior is equal to the behavior of their arguments.
1595 ;; behavior(monotonically_increasing(f(x))) = behavior(f(x))
1596 ((member (caar exp
) '(%atan %erf %sinh %tanh
) :test
#'eq
)
1597 (behavior (cadr exp
) var val
))
1598 ;; Similarly, acot is monotonically decreasing left and right of x = 0.
1599 ;; The singularity doesn't matter for our purposes.
1600 ;; behavior(monotonically_decreasing(f(x))) = -behavior(f(x))
1601 ((eq (caar exp
) '%acot
)
1602 (- (behavior (cadr exp
) var val
)))
1603 ;; Note: More functions could be added here.
1604 ;; Ideally, use properties defined on the functions.
1605 ;; Handle log(f(x)) for f(0) = 0:
1606 ;; If behavior(f(x)) = 1, then behavior(log(f(x))) = 1
1608 (=0 (no-err-sub 0 (cadr exp
)))
1609 (equal 1 (behavior (cadr exp
) var val
)))
1611 ;; Try to determine the behavior by taking the derivative.
1612 ((not (equal 0 (setq ans
(behavior-by-diff exp var val
))))
1614 ;; If exp is a sum and all terms have the same behavior, return that.
1615 ;; The sum of increasing functions is increasing.
1616 ;; The sum of decreasing functions is decreasing.
1618 (not (equal 0 (setq ans
(behavior-all-same exp var val
)))))
1622 (defun behavior-all-same (exp var val
)
1623 (let* ((exps (cdr exp
)) (first-behavior (behavior (first exps
) var val
)))
1624 (if (or (equal first-behavior
0)
1625 (not (every #'(lambda (exp) (equal (behavior exp var val
) first-behavior
))
1630 (defun behavior-expt (bas expo
)
1631 (let ((behavior (behavior bas var val
)))
1632 (cond ((= behavior
1) 1)
1634 ((eq (ask-integer expo
'$integer
) '$yes
)
1635 (cond ((eq (ask-integer expo
'$even
) '$yes
) 1)
1638 (cond ((evenp (cadr expo
)) 1)
1639 ((oddp (caddr expo
)) behavior
)
1643 (defun behavior-by-diff (exp var val
)
1645 (exp (sratsimp (sdiff exp var
)) (sratsimp (sdiff exp var
)))
1647 (ans ())) ; This do wins by a return.
1648 ((> ct
1) 0) ; This loop used to run up to 5 times,
1649 ;; but the size of some expressions would blow up.
1650 (setq ans
(no-err-sub 0 exp
)) ;Why not do an EVENFN and ODDFN
1654 ((=0 ans
) ()) ;Do it again.
1655 (t (setq ans
(getsignl ans
))
1656 (cond (n (return ans
))
1658 (return (if (eq val
'$zeroa
) 1 -
1)))
1660 (return (if (eq val
'$zeroa
) -
1 1)))
1663 (defun try-lhospital (n d ind
)
1664 ;;Make one catch for the whole bunch of lhospital trials.
1665 (let ((ans (lhospital-catch n d ind
)))
1666 (cond ((null ans
) ())
1667 ((not (free-infp ans
)) (simpinf ans
))
1668 ((not (free-epsilonp ans
)) (simpab ans
))
1671 (defun try-lhospital-quit (n d ind
)
1672 (let ((ans (or (lhospital-catch n d ind
)
1673 (lhospital-catch (m^ d -
1) (m^ n -
1) ind
))))
1674 (cond ((null ans
) (throw 'limit t
))
1675 ((not (free-infp ans
)) (simpinf ans
))
1676 ((not (free-epsilonp ans
)) (simpab ans
))
1679 (defun lhospital-catch (n d ind
)
1680 (cond ((> 0 lhcount
)
1681 (setq lhcount $lhospitallim
)
1682 (throw 'lhospital nil
))
1683 ((equal lhcount $lhospitallim
)
1684 (let ((lhcount (m+ lhcount -
1)))
1685 (catch 'lhospital
(lhospital n d ind
))))
1686 (t (setq lhcount
(m+ lhcount -
1))
1687 (prog1 (lhospital n d ind
)
1688 (setq lhcount
(m+ lhcount
1))))))
1689 ;;If this succeeds then raise LHCOUNT.
1692 (defun lhospital (n d ind
)
1693 (declare (special val lhp?
))
1695 (setq n
(m*l
(mapcar #'(lambda (term) (lhsimp term var val
)) (cdr n
)))))
1697 (setq d
(m*l
(mapcar #'(lambda (term) (lhsimp term var val
)) (cdr d
)))))
1698 (multiple-value-bind (n d
)
1700 (let (const nconst dconst
)
1701 (setq lhp?
(and (null ind
) (cons n d
)))
1702 (multiple-value-setq (nconst n
) (var-or-const n
))
1703 (multiple-value-setq (dconst d
) (var-or-const d
))
1705 (setq n
(stirling0 n
)) ;; replace factorial and %gamma
1706 (setq d
(stirling0 d
)) ;; with approximations
1708 (setq n
(sdiff n var
) ;; take derivatives for l'hospital
1711 (if (or (not (free n
'%derivative
)) (not (free d
'%derivative
)))
1712 (throw 'lhospital
()))
1713 (let (($trigexpandtimes nil
)) ;see #895 limit x->inf sin(100/x)*x very slow
1714 (setq n
(expand-trigs (tansc n
) var
))
1715 (setq d
(expand-trigs (tansc d
) var
)))
1717 (multiple-value-setq (const n d
) (remove-singularities n d
))
1718 (setq const
(m* const
(m// nconst dconst
)))
1719 (simpinf (let ((ans (if ind
1720 (limit2 n d var val
)
1721 (limit-numden n d val
))))
1722 ;; When the limit function returns, it's possible that it will return NIL
1723 ;; (gave up without finding a limit). It's also possible that it will
1724 ;; return something containing UND. We treat that as a failure too.
1725 (when (and ans
(freeof '$und ans
))
1726 (sratsimp (m* const ans
))))))))
1728 ;; Try to compute the limit of a quotient NUM/DEN, trying to massage the input
1729 ;; into a convenient form for LIMIT on the way.
1730 (defun limit-numden (n d val
)
1732 ;; For general arguments, the best approach seems to be to use
1733 ;; sratsimp to simplify the quotient as much as we can, then
1734 ;; $multthru, which splits it up into a sum (presumably useful
1735 ;; because limit(a+b) = limit(a) + limit(b) if the limits exist, and
1736 ;; the right hand side might be easier to calculate)
1738 ($multthru
(sratsimp (m// n d
))))
1740 ;; If we've already got a sum in the numerator, it seems to be
1741 ;; better not to recombine it. Call LIMIT on the whole lot, though,
1742 ;; because terms with infinite limits might cancel to give a finite
1745 (m+l
(mapcar #'(lambda (x)
1746 (sratsimp (m// x d
)))
1749 (limit expr var val
'think
)))
1751 ;; Heuristics for picking the right way to express a LHOSPITAL problem.
1752 (defun lhop-numden (num denom
)
1753 (declare (special var
))
1754 (cond ((let ((log-num (involve num
'(%log
))))
1755 (cond ((null log-num
) ())
1756 ((lessthan (num-of-logs (factor (sratsimp (sdiff (m^ num -
1) var
))))
1757 (num-of-logs (factor (sratsimp (sdiff num var
)))))
1758 (psetq num
(m^ denom -
1) denom
(m^ num -
1))
1761 ((let ((log-denom (involve denom
'(%log
))))
1762 (cond ((null log-denom
) ())
1763 ((lessthan (num-of-logs (sratsimp (sdiff (m^ denom -
1) var
)))
1764 (num-of-logs (sratsimp (sdiff denom var
))))
1765 (psetq denom
(m^ num -
1) num
(m^ denom -
1))
1768 ((let ((exp-num (%einvolve num
)))
1770 (cond ((%e-right-placep exp-num
)
1772 (t (psetq num
(m^ denom -
1)
1773 denom
(m^ num -
1)) t
)))
1775 ((let ((exp-den (%einvolve denom
)))
1777 (cond ((%e-right-placep exp-den
)
1779 (t (psetq num
(m^ denom -
1)
1780 denom
(m^ num -
1)) t
)))
1782 ((let ((scnum (involve num
'(%sin
))))
1783 (cond (scnum (cond ((trig-right-placep '%sin scnum
) t
)
1784 (t (psetq num
(m^ denom -
1)
1785 denom
(m^ num -
1)) t
)))
1787 ((let ((scden (involve denom
'(%sin
))))
1788 (cond (scden (cond ((trig-right-placep '%sin scden
) t
)
1789 (t (psetq num
(m^ denom -
1)
1790 denom
(m^ num -
1)) t
)))
1792 ((let ((scnum (involve num
'(%asin %acos %atan
))))
1793 ;; If the numerator contains an inverse trig and the
1794 ;; denominator or reciprocal of denominator is polynomial,
1795 ;; leave everything as is. If the inverse trig is moved to
1796 ;; the denominator, things get messy, even if the numerator
1797 ;; becomes a polynomial. This is not perfect.
1798 (cond ((and scnum
(or (polyinx denom var
())
1799 (polyinx (m^ denom -
1) var
())))
1802 ((or (oscip num
) (oscip denom
)))
1804 (psetq num
(m^ denom -
1) denom
(m^ num -
1))))
1807 ;;i don't know what to do here for some cases, may have to be refined.
1808 (defun num-of-logs (exp)
1809 (cond ((mapatom exp
) 0)
1810 ((equal (caar exp
) '%log
)
1811 (m+ 1 (num-of-log-l (cdr exp
))))
1812 ((and (mexptp exp
) (mnump (caddr exp
)))
1813 (m* (simplify `((mabs) ,(caddr exp
)))
1814 (num-of-logs (cadr exp
))))
1815 (t (num-of-log-l (cdr exp
)))))
1817 (defun num-of-log-l (llist)
1818 (do ((temp llist
(cdr temp
)) (ans 0))
1820 (setq ans
(m+ ans
(num-of-logs (car temp
))))))
1822 (defun %e-right-placep
(%e-arg
)
1823 (let ((%e-arg-diff
(sdiff %e-arg var
)))
1825 ((free %e-arg-diff var
)) ;simple cases
1826 ((or (and (mexptp denom
)
1827 (equal (cadr denom
) -
1))
1828 (polyinx (m^ denom -
1) var
())) ())
1829 ((let ((%e-arg-diff-lim
(ridofab (limit %e-arg-diff var val
'think
)))
1830 (%e-arg-exp-lim
(ridofab (limit (m^
'$%e %e-arg
) var val
'think
))))
1833 (format t
"%e-arg-dif-lim = ~A~%" %e-arg-diff-lim
)
1834 (format t
"%e-arg-exp-lim = ~A~%" %e-arg-exp-lim
))
1835 (cond ((equal %e-arg-diff-lim %e-arg-exp-lim
)
1837 ((and (mnump %e-arg-diff-lim
) (mnump %e-arg-exp-lim
))
1839 ((and (mnump %e-arg-diff-lim
) (infinityp %e-arg-exp-lim
))
1840 ;; This is meant to make maxima handle bug 1469411
1841 ;; correctly. Undoubtedly, this needs work.
1845 (defun trig-right-placep (trig-type arg
)
1846 (let ((arglim (ridofab (limit arg var val
'think
)))
1847 (triglim (ridofab (limit `((,trig-type
) ,arg
) var val
'think
))))
1848 (cond ((and (equal arglim
0) (equal triglim
0)) t
)
1849 ((and (infinityp arglim
) (infinityp triglim
)) t
)
1852 ;;Takes a numerator and a denominator. If they tries all combinations of
1853 ;;products to try and make a simpler set of subproblems for LHOSPITAL.
1854 (defun remove-singularities (numer denom
)
1855 (cond ((or (null numer
) (null denom
)
1856 (atom numer
) (atom denom
)
1857 (not (mtimesp numer
)) ;Leave this here for a while.
1858 (not (mtimesp denom
)))
1859 (values 1 numer denom
))
1862 (multiple-value-bind (num-consts num-vars
)
1863 (var-or-const numer
)
1864 (multiple-value-bind (denom-consts denom-vars
)
1865 (var-or-const denom
)
1866 (if (not (mtimesp num-vars
))
1867 (setq num-vars
(list num-vars
))
1868 (setq num-vars
(cdr num-vars
)))
1869 (if (not (mtimesp denom-vars
))
1870 (setq denom-vars
(list denom-vars
))
1871 (setq denom-vars
(cdr denom-vars
)))
1872 (do ((nl num-vars
(cdr nl
))
1873 (num-list (copy-list num-vars
))
1874 (den-list denom-vars den-list-temp
)
1875 (den-list-temp (copy-list denom-vars
)))
1876 ((null nl
) (values (m* const
(m// num-consts denom-consts
))
1878 (m*l den-list-temp
)))
1879 (do ((dl den-list
(cdr dl
)))
1881 (if (or (%einvolve
(car nl
)) (%einvolve
(car nl
)))
1883 (let ((lim (catch 'limit
(simpinf (simpab (limit (m// (car nl
) (car dl
))
1884 var val
'think
))))))
1885 (cond ((or (eq lim t
)
1887 (equal (ridofab lim
) 0)
1889 (not (free lim
'$inf
))
1890 (not (free lim
'$minf
))
1891 (not (free lim
'$infinity
))
1892 (not (free lim
'$ind
))
1893 (not (free lim
'$und
)))
1896 (setq const
(m* lim const
))
1897 (setq num-list
(delete (car nl
) num-list
:count
1 :test
#'equal
))
1898 (setq den-list-temp
(delete (car dl
) den-list-temp
:count
1 :test
#'equal
))
1899 (return t
)))))))))))))
1901 ;; separate terms that contain var from constant terms
1902 ;; returns (const-terms . var-terms)
1903 (defun var-or-const (expr)
1904 (setq expr
($factor expr
))
1912 (do ((l (cdr expr
) (cdr l
))
1915 ((null l
) (values const varl
))
1916 (if (free (car l
) var
)
1917 (setq const
(m* (car l
) const
))
1918 (setq varl
(m* (car l
) varl
)))))
1922 ;; if term goes to non-zero constant, replace with constant
1923 (defun lhsimp (term var val
)
1924 (cond (($mapatom term
) term
)
1926 (let ((term-value (ridofab (limit term var val
'think
))))
1927 (if (and (not (member term-value
'($inf $minf $und $ind $infinity
)))
1928 (eq t
(mnqp term-value
0))) term-value term
)))))
1930 (defun bylog (expo bas
)
1933 (try-lhospital-quit (simplify `((%log
) ,(tansc bas
)))
1938 (defun simplimexpt (bas expo bl el
)
1939 (cond ((or (eq bl
'$und
) (eq el
'$und
)) '$und
)
1941 (cond ((eq el
'$inf
) (if (eq bl
'$zeroa
) bl
0))
1942 ((eq el
'$minf
) (if (eq bl
'$zeroa
) '$inf
'$infinity
))
1943 ((eq el
'$ind
) '$ind
)
1944 ((eq el
'$infinity
) '$und
)
1945 ((zerop2 el
) (bylog expo bas
))
1946 (t (cond ((equal (getsignl el
) -
1)
1947 (cond ((eq bl
'$zeroa
) '$inf
)
1949 (cond ((even1 el
) '$inf
)
1950 ((eq (ask-integer el
'$integer
) '$yes
)
1951 (if (eq (ask-integer el
'$even
) '$yes
)
1953 '$minf
)))) ;Gotta be ODD.
1954 (t (setq bas
(behavior bas var val
))
1955 (cond ((equal bas
1) '$inf
)
1956 ((equal bas -
1) '$minf
)
1957 (t (throw 'limit t
))))))
1959 (member bl
'($zeroa $zerob
) :test
#'eq
))
1960 (cond ((even1 el
) '$zeroa
)
1961 ((and (eq bl
'$zerob
)
1963 (evenp (caddr el
))) 0)
1965 ((equal (getsignl el
) 1)
1966 (if (eq bl
'$zeroa
) bl
0))
1967 ((equal (getsignl el
) 0)
1969 (t (throw 'limit t
))))))
1971 (cond ((zerop2 el
) (bylog expo bas
))
1973 ((eq el
'$inf
) '$infinity
)
1974 ((member el
'($infinity $ind
) :test
#'eq
) '$und
)
1975 ((equal (setq el
(getsignl el
)) 1) '$infinity
)
1978 (t (throw 'limit t
))))
1980 (cond ((eq el
'$inf
) '$inf
)
1981 ((equal el
'$minf
) 0)
1982 ((zerop2 el
) (bylog expo bas
))
1983 ((member el
'($infinity $ind
) :test
#'eq
) '$und
)
1984 (t (cond ((eql 0 (getsignl el
)) 1)
1985 ((ratgreaterp 0 el
) '$zeroa
)
1986 ((ratgreaterp el
0) '$inf
)
1987 (t (throw 'limit t
))))))
1989 (cond ((zerop2 el
) (bylog expo bas
))
1990 ((eq el
'$inf
) '$und
)
1991 ((equal el
'$minf
) 0)
1992 ;;;Why not generalize this. We can ask about the number. -Jim 2/23/81
1993 ((mnump el
) (cond ((mnegp el
)
1996 (if (eq (ask-integer el
'$integer
) '$yes
)
1997 (if (eq (ask-integer el
'$even
) '$yes
)
2001 (t (cond ((even1 el
) '$inf
)
2002 ((eq (ask-integer el
'$integer
) '$yes
)
2003 (if (eq (ask-integer el
'$even
) '$yes
)
2007 (loginprod?
(throw 'lip?
'lip
!))
2009 ((equal (simplify (ratdisrep (ridofab bl
))) 1)
2010 (if (infinityp el
) (bylog expo bas
) 1))
2011 ((and (equal (ridofab bl
) -
1)
2012 (infinityp el
)) '$ind
) ;LB
2013 ((eq bl
'$ind
) (cond ((or (zerop2 el
) (infinityp el
)) '$und
)
2014 ((not (equal (getsignl el
) -
1)) '$ind
)
2016 ((eq el
'$inf
) (cond ((abeq1 bl
)
2017 (if (equal (getsignl bl
) 1) 1 '$ind
))
2019 (if (equal (getsignl bl
) 1) '$zeroa
0))
2020 ((equal (getsignl (m1- bl
)) 1) '$inf
)
2021 ((equal (getsignl (m1- `((mabs) ,bl
))) 1) '$infinity
)
2022 (t (throw 'limit t
))))
2023 ((eq el
'$minf
) (cond ((abeq1 bl
)
2024 (if (equal (getsignl bl
) 1) 1 '$ind
))
2026 (if (equal (getsignl bl
) 1) '$zeroa
0))
2027 ((ratgreaterp 0 bl
) '$infinity
)
2030 (if (equal val
'$infinity
)
2031 '$und
;Not enough info to do anything.
2032 (destructuring-bind (real-el . imag-el
)
2034 (setq real-el
(limit real-el var origval nil
))
2035 (cond ((eq real-el
'$minf
)
2037 ((and (eq real-el
'$inf
)
2038 (not (equal (ridofab (limit imag-el var origval nil
)) 0)))
2040 ((eq real-el
'$infinity
)
2041 (throw 'limit t
)) ;; don't really know real component
2045 ((eq el
'$ind
) '$ind
)
2048 ;; When bl is off the negative real axis, use direct substitution
2049 ((off-negative-real-axisp bl
) (ftake 'mexpt bl el
))
2051 ;; We're looking at (neg + {zerob, 0 zeroa} %i)^el. We need to
2052 ;; do a rectform on bas and decide if the imaginary part is
2053 ;; zerob, 0, or zeroa.
2054 (let ((x) (y) (xlim) (ylim) (preserve-direction t
))
2055 (setq bas
(risplit bas
))
2058 (setq xlim
(limit x var val
'think
))
2059 (setq ylim
(limit y var val
'think
))
2060 (cond ((eql 0 y
) (ftake 'mexpt bl el
))
2062 (mul (ftake 'mexpt
(mul -
1 xlim
) el
)
2063 (ftake 'mexpt
'$%e
(mul '$%i
'$%pi el
))))
2065 (mul (ftake 'mexpt
(mul -
1 xlim
) el
)
2066 (ftake 'mexpt
'$%e
(mul -
1 '$%i
'$%pi el
))))
2067 (t (throw 'limit nil
)))))))
2070 (cond ((numberp x
) (and (integerp x
) (evenp x
)))
2071 ((and (mnump x
) (evenp (cadr x
))))))
2073 ;; is absolute value less than one?
2077 (and (ratgreaterp 1. bl
) (ratgreaterp bl -
1.
)))
2078 (t (equal (getsignl (m1- `((mabs) ,bl
))) -
1.
))))
2080 ;; is absolute value equal to one?
2084 (or (equal 1. bl
) (equal bl -
1.
)))
2085 (t (equal (getsignl (m1- `((mabs) ,bl
))) 0))))
2087 (defun simplimit (exp var val
&aux op
)
2090 ((or (atom exp
) (mnump exp
)) exp
)
2091 ;; Lookup and dispatch a simplim%function from the property list
2092 ((setq op
(safe-get (mop exp
) 'simplim%function
))
2093 (funcall op exp var val
))
2095 ;; And do the same for subscripted:
2096 ((and (consp exp
) (consp (car exp
)) (eq (caar exp
) 'mqapply
)
2097 (setq op
(safe-get (subfunname exp
) 'simplim%function
)))
2098 (funcall op exp var val
))
2100 ;; Without the call to rootscontract,
2101 ;; limit(((-4)*x^2-10*x+24)/((4*x+8)^(1/3)+2),x,-4)
2102 ;; returns zero (should be 66). This bug is due to the fact that
2103 ;; 4^(1/3)*2^(1/3)-2 does not simplify to zero. Although the call
2104 ;; to rootscontract takes care of this case, almost surely there are
2105 ;; many other limit problems that need more than rootscontract.
2106 ((mplusp exp
) (let (($rootsconmode nil
)) ($rootscontract
(simplimplus exp
))))
2107 ((mtimesp exp
) (simplimtimes (cdr exp
)))
2108 ((mexptp exp
) (simplimexpt (cadr exp
) (caddr exp
)
2109 (limit (cadr exp
) var val
'think
)
2110 (limit (caddr exp
) var val
'think
)))
2111 ((member (caar exp
) '(%sin %cos
) :test
#'eq
)
2112 (simplimsc exp
(caar exp
) (limit (cadr exp
) var val
'think
)))
2113 ((eq (caar exp
) '%tan
) (simplim%tan
(cadr exp
)))
2114 ((member (caar exp
) '(%sinh %cosh
) :test
#'eq
)
2115 (simplimsch (caar exp
) (limit (cadr exp
) var val
'think
)))
2116 ((member (caar exp
) '(%erf %tanh
) :test
#'eq
)
2117 (simplim%erf-%tanh
(caar exp
) (cadr exp
)))
2118 ((eq (caar exp
) '%atanh
)
2119 (simplim%atanh
(limit (cadr exp
) var val
'think
) val
))
2120 ((eq (caar exp
) '%acosh
)
2121 (simplim%acosh
(limit (cadr exp
) var val
'think
)))
2122 ((eq (caar exp
) '%asinh
)
2123 (simplim%asinh
(limit (cadr exp
) var val
'think
)))
2124 ((eq (caar exp
) '%inverse_jacobi_ns
)
2125 (simplim%inverse_jacobi_ns
(limit (cadr exp
) var val
'think
) (third exp
)))
2126 ((eq (caar exp
) '%inverse_jacobi_nc
)
2127 (simplim%inverse_jacobi_nc
(limit (cadr exp
) var val
'think
) (third exp
)))
2128 ((eq (caar exp
) '%inverse_jacobi_sc
)
2129 (simplim%inverse_jacobi_sc
(limit (cadr exp
) var val
'think
) (third exp
)))
2130 ((eq (caar exp
) '%inverse_jacobi_cs
)
2131 (simplim%inverse_jacobi_cs
(limit (cadr exp
) var val
'think
) (third exp
)))
2132 ((eq (caar exp
) '%inverse_jacobi_dc
)
2133 (simplim%inverse_jacobi_dc
(limit (cadr exp
) var val
'think
) (third exp
)))
2134 ((eq (caar exp
) '%inverse_jacobi_ds
)
2135 (simplim%inverse_jacobi_ds
(limit (cadr exp
) var val
'think
) (third exp
)))
2136 ((and (eq (caar exp
) 'mqapply
)
2137 (eq (subfunname exp
) '$psi
))
2138 (simplim$psi
(subfunsubs exp
) (subfunargs exp
) val
))
2139 ((and (eq (caar exp
) var
)
2140 (member 'array
(car exp
) :test
#'eq
)
2141 (every #'(lambda (sub-exp)
2144 exp
) ;LIMIT(B[I],B,INF); -> B[I]
2145 ;; When limsubst is true, limit(f(n+1)/f(n),n,inf) = 1. The user documentation
2146 ;; warns against setting limsubst to true.
2148 (simplify (cons (operator-with-array-flag exp
)
2149 (mapcar #'(lambda (a)
2150 (limit a var val
'think
))
2152 (throw 'limit t
)))))
2155 (setq e
(resimplify (subst (m// 1 var
) var e
)))
2156 (let ((new-val (cond ((eq val
'$zeroa
) '$inf
)
2157 ((eq val
'$zerob
) '$minf
))))
2158 (if new-val
(let ((preserve-direction t
))
2159 (toplevel-$limit e var new-val
)) (throw 'limit t
))))
2161 (defun simplimtimes (exp)
2162 ;; The following test
2163 ;; handles (-1)^x * 2^x => (-2)^x => $infinity
2164 ;; wants to avoid (-1)^x * 2^x => $ind * $inf => $und
2166 (and (expfactorp (cons '(mtimes) exp
) 1)
2167 (expfactor (cons '(mtimes) exp
) 1 var
))))
2168 (when try
(return-from simplimtimes try
)))
2170 (let ((prod 1) (num 1) (denom 1)
2171 (zf nil
) (ind-flag nil
) (inf-type nil
)
2172 (constant-zero nil
) (constant-infty nil
))
2174 (let* ((loginprod?
(involve term
'(%log
)))
2175 (y (catch 'lip?
(limit term var val
'think
))))
2177 ;; limit failed due to log in product
2179 (return-from simplimtimes
(liminv (cons '(mtimes simp
) exp
))))
2181 ;; If the limit is infinitesimal or zero
2183 (setf num
(m* num term
)
2184 constant-zero
(or constant-zero
(not (among var term
))))
2187 (unless zf
(setf zf
1)))
2189 (setf zf
(* -
1 (or zf
1))))))
2191 ;; If the limit is not some form of infinity or
2192 ;; undefined/indeterminate.
2193 ((not (member y
'($inf $minf $infinity $ind $und
) :test
#'eq
))
2194 (setq prod
(m* prod y
)))
2196 ((eq y
'$und
) (return-from simplimtimes
'$und
))
2197 ((eq y
'$ind
) (setq ind-flag t
))
2199 ;; Some form of infinity
2201 (setf denom
(m* denom term
)
2202 constant-infty
(or constant-infty
(not (among var term
))))
2203 (unless (eq inf-type
'$infinity
)
2205 ((eq y
'$infinity
) (setq inf-type
'$infinity
))
2206 ((null inf-type
) (setf inf-type y
))
2207 ;; minf * minf or inf * inf
2208 ((eq y inf-type
) (setf inf-type
'$inf
))
2210 (t (setf inf-type
'$minf
))))))))
2213 ;; If there are zeros and infinities among the terms that are free of
2214 ;; VAR, then we have an expression like "inf * zeroa * f(x)" or
2215 ;; similar. That gives an undefined result. Note that we don't
2216 ;; necessarily have something undefined if only the zeros have a term
2217 ;; free of VAR. For example "zeroa * exp(-1/x) * 1/x" as x -> 0. And
2218 ;; similarly for the infinities.
2219 ((and constant-zero constant-infty
) '$und
)
2221 ;; If num=denom=1, we didn't find any explicit infinities or zeros, so we
2222 ;; either return the simplified product or ind
2223 ((and (eql num
1) (eql denom
1))
2224 (if ind-flag
'$ind prod
))
2225 ;; If denom=1 (and so num != 1), we have some form of zero
2229 (let ((sign (getsignl prod
)))
2230 (if (or (not sign
) (eq sign
'complex
))
2236 ;; If num=1 (and so denom != 1), we have some form of infinity
2238 (let ((sign ($csign prod
)))
2241 ((eq sign
'$pos
) inf-type
)
2242 ((eq sign
'$neg
) (case inf-type
2246 ((member sign
'($complex $imaginary
)) '$infinity
)
2247 ; sign is '$zero, $pnz, $pz, etc
2248 (t (throw 'limit t
)))))
2249 ;; Both zeros and infinities
2251 ;; All bets off if there are some infinities or some zeros, but it
2252 ;; needn't be undefined (see above)
2253 (when (or constant-zero constant-infty
) (throw 'limit t
))
2255 (let ((ans (limit2 num
(m^ denom -
1) var val
)))
2257 (simplimtimes (list prod ans
))
2258 (throw 'limit t
)))))))
2260 ;;;PUT CODE HERE TO ELIMINATE FAKE SINGULARITIES??
2262 ;; At one time when simplimplus1 failed to find a limit, this function
2263 ;; applied ratsimp and tried again. This scheme didn't fix any testsuite
2264 ;; bugs and it slowed the code, so I removed it (Barton Willis, July 2023).
2265 (defun simplimplus (exp)
2266 (cond ((memalike exp simplimplus-problems
) (throw 'limit t
))
2268 (progn (push exp simplimplus-problems
)
2269 (let ((ans (catch 'limit
(simplimplus1 exp
))))
2270 (if (or (eq ans nil
) (eq ans t
) (among '%limit ans
))
2271 (throw 'limit t
) (resimp-extra-simp ans
))))
2272 (pop simplimplus-problems
)))))
2274 (defun simplimplus1 (exp)
2275 (prog (sum y infl infinityl minfl indl undl zerobl zeroal ans r
)
2276 (do ((exp (cdr exp
) (cdr exp
)) (f))
2278 (setq f
(limit (car exp
) var val
'think
))
2279 (cond ((null f
) (throw 'limit t
))
2280 ((eq f
'$und
) (push (car exp
) undl
))
2281 ((eq f
'$zerob
) (push (car exp
) zerobl
))
2282 ((eq f
'$zeroa
) (push (car exp
) zeroal
))
2283 ((eq f
'$ind
) (push (car exp
) indl
))
2284 ((eq f
'$inf
) (push (car exp
) infl
))
2285 ((eq f
'$minf
) (push (car exp
) minfl
))
2286 ((eq f
'$infinity
) (push (car exp
) infinityl
))
2289 ;; When there are two or more infinity terms, we dispatch gruntz1 on
2290 ;; the rectangular form of their sum. When gruntz1 is able to find the
2291 ;; limit, we modify the lists infinityl, minfl, ... accordingly.
2292 (when (and infinityl
(cdr infinityl
) (not (among '$li
(cons '(mlist) infinityl
))))
2293 (setq ans
(risplit (fapply 'mplus infinityl
)))
2295 (let ((re (car ans
)) (im (cdr ans
)))
2296 (setq re
(car (errcatch (gruntz1 re var val
))))
2297 (setq im
(car (errcatch (gruntz1 im var val
))))
2299 (cond ((or (null re
) (null im
)) nil
)
2300 ((infinityp im
) '$infinity
)
2302 (t (add re
(mul '$%i im
))))))
2303 (cond ((eq r
'$infinity
) (list (fapply 'mplus infinityl
)))
2304 ((eq r nil
) (throw 'limit t
))
2306 (setq infinityl nil
)
2307 (cond ((eq r
'$zerob
) (push ans zerobl
))
2308 ((eq r
'$zeroa
) (push ans zeroal
))
2309 ((eq r
'$ind
) (push ans indl
))
2310 ((eq r
'$und
) (push ans undl
))
2311 ((eq r
'$minf
) (push ans minfl
))
2312 ((eq r
'$inf
) (push ans infl
))
2313 (t (push r sum
))))))
2315 ;; Unfortunately, this code does not handle the case of one or more
2316 ;; infinity terms and either a minf or inf term. So we throw an error.
2317 (when (and infinityl
(or minfl infl
))
2320 ;; Blend the zerob, zeroa, and sum terms. When there are both zerob
2321 ;; and zeroa terms, ignore them. When preserve-direction is true and
2322 ;; there are zerob terms, push zerob into the sum terms. And do the same
2323 ;; for zeroa. After that, add the terms in the list sum.
2324 (when (and preserve-direction zerobl
(null zeroal
))
2326 (when (and preserve-direction zeroal
(null zerobl
))
2329 ;; When indl has two or more members, we attempt to condense the
2330 ;; ind terms into a single term.
2331 (when (and indl
(cdr indl
))
2332 ;; Use factor to attempt to condense the ind terms
2333 ;; into a finite term. This is needed for cases such as
2334 ;; limit(cos(1/x)*sin(x)-sin(x),x,inf). When there is only
2335 ;; one ind term, set ans to ind.
2336 (setq ans
($factor
(fapply 'mplus indl
)))
2337 (setq r
(if (mplusp ans
) nil
(limit ans var val
'think
)))
2338 (cond ((eq r
'$zerob
)
2344 ((eq r
'$ind
) (setq indl
(list ans
)))
2348 ((or (eq r
'$minf
) (eq r
'$inf
) (eq r
'$infinity
))
2352 ;; Add the members of the list sum.
2353 (setq sum
(fapply 'mplus sum
))
2356 (if (or infl minfl indl infinityl
)
2357 (setq infinityl
(append undl infinityl
)); x^2 + x*sin(x)
2358 (return '$und
))) ; 1 + x*sin(x)
2359 ((not (or infl minfl indl infinityl
))
2360 (return (cond ((atom sum
) sum
)
2361 ((or (not (free sum
'$zeroa
))
2362 (not (free sum
'$zerob
)))
2365 (t (cond ((null infinityl
)
2366 (cond (infl (cond ((null minfl
) (return '$inf
))
2368 (minfl (return '$minf
))
2369 (indl (return '$ind
))))
2370 (t (setq infl
(append infl infinityl
))))))
2372 oon
(setq y
(m+l
(append minfl infl
)))
2373 (cond ((alike1 exp
(setq y
(sratsimp (hyperex y
))))
2374 (cond ((not (infinityp val
))
2375 (setq infl
(cnv infl val
)) ;THIS IS HORRIBLE!!!!
2376 (setq minfl
(cnv minfl val
))))
2378 (cond ((every #'(lambda (j) (radicalp j var
))
2379 (append infl minfl
))
2380 (setq y
(rheur infl minfl
)))
2381 (t (setq y
(sheur infl minfl
))))))
2382 (t (setq y
(limit y var val
'think
))))
2383 (cond ((or (eq y
())
2384 (eq y t
)) (return ()))
2385 ((infinityp y
) (return y
))
2386 (indl (return '$ind
))
2387 (t (return (m+ sum y
))))))
2389 ;; Limit n/d, using heuristics on the order of growth.
2392 (cond ((and (free n var
)
2395 (t (setq n
(cpa n d nil
))
2397 (cond ((oscip orig-n
) '$und
)
2399 ((equal n -
1) '$zeroa
)
2400 ((equal n
0) (m// orig-n d
)))))))
2403 ;;;L1 is a list of INF's and L2 is a list of MINF's. Added together
2404 ;;;it is indeterminate.
2405 (defun sheur (l1 l2
)
2406 (let ((term (sheur1 l1 l2
)))
2407 (cond ((equal term
'$inf
) '$inf
)
2408 ((equal term
'$minf
) '$minf
)
2409 (t (let ((new-num (m+l
(mapcar #'(lambda (num-term)
2410 (m// num-term
(car l1
)))
2412 (cond ((limit2 new-num
(m// 1 (car l1
)) var val
))))))))
2415 (cond ((atom exp
) nil
)
2416 (t (setq exp
(nformat exp
))
2417 (cond ((and (eq (caar exp
) 'mquotient
)
2418 (among var
(caddr exp
)))
2421 (defun zerop2 (z) (=0 (ridofab z
)))
2423 (defun raise (a) (m+ a
'$zeroa
))
2425 (defun lower (a) (m+ a
'$zerob
))
2427 (defun sincoshk (exp1 l sc
)
2428 (cond ((equal l
1) (lower l
))
2429 ((equal l -
1) (raise l
))
2431 ((member val
'($zeroa $zerob
) :test
#'eq
) (spangside exp1 l
))
2434 (defun spangside (e l
)
2435 (setq e
(behavior e var val
))
2436 (cond ((equal e
1) (raise l
))
2437 ((equal e -
1) (lower l
))
2440 ;; get rid of zeroa and zerob
2442 (if (among '$zeroa e
) (setq e
(maxima-substitute 0 '$zeroa e
)))
2443 (if (among '$zerob e
) (setq e
(maxima-substitute 0 '$zerob e
)))
2447 ;; returns true if exp is a polynomial raised to a numeric power
2448 (defun simplerd (exp)
2450 (mnump (caddr exp
)) ;; exponent must be a number - no variables
2451 (polyp (cadr exp
))))
2453 (defun branch1 (exp val
)
2454 (cond ((polyp exp
) nil
)
2455 ((simplerd exp
) (zerop2 (subin val
(cadr exp
))))
2457 (loop for v on
(cdr exp
)
2458 when
(branch1 (car v
) val
)
2461 (defun branch (exp val
)
2462 (cond ((polyp exp
) nil
)
2463 ((or (simplerd exp
) (mtimesp exp
))
2466 (every #'(lambda (j) (branch j val
)) (the list
(cdr exp
))))))
2468 (defun ser0 (e n d val
)
2469 (cond ((and (branch n val
) (branch d val
))
2473 ;;;NN* gets set by POFX, called by SER1, to get a list of exponents.
2474 (setq nn
* (ratmin nn
*))
2475 (setq n
(sratsimp (m^ n
(m^ var nn
*))))
2476 (setq d
(sratsimp (m^ d
(m^ var nn
*))))
2477 (cond ((member val
'($zeroa $zerob
) :test
#'eq
) nil
)
2480 (t (try-lhospital-quit n d nil
))))
2482 (defun rheur (l1 l2
)
2484 (setq m1
(mapcar (function asymredu
) l1
))
2485 (setq m2
(mapcar (function asymredu
) l2
))
2486 (setq ans
(m+l
(append m1 m2
)))
2487 (cond ((rptrouble (m+l
(append l1 l2
)))
2488 (return (limit (simplify (rdsget (m+l
(append l1 l2
))))
2492 ((mplusp ans
) (return (sheur m1 m2
)))
2493 (t (return (limit ans var val t
))))))
2495 (defun rptrouble (rp)
2496 (not (equal (rddeg rp nil
) (rddeg (asymredu rp
) nil
))))
2498 (defun radicalp (exp var
)
2499 (cond ((polyinx exp var
()))
2500 ((mexptp exp
) (cond ((equal (caddr exp
) -
1.
)
2501 (radicalp (cadr exp
) var
))
2503 ((member (caar exp
) '(mplus mtimes
) :test
#'eq
)
2504 (every #'(lambda (j) (radicalp j var
))
2507 (defun involve (e nn
*)
2508 (declare (special var
))
2509 (cond ((atom e
) nil
)
2511 ((and (member (caar e
) nn
* :test
#'eq
) (among var
(cdr e
))) (cadr e
))
2512 (t (some #'(lambda (j) (involve j nn
*)) (cdr e
)))))
2514 ;; Just like INVOLVE, except the dependency on VAR is explicit (second
2515 ;; arg here). Use this instead.
2516 (defun involve-var (e var1 nn
*)
2517 (cond ((atom e
) nil
)
2519 ((and (member (caar e
) nn
* :test
#'eq
)
2520 (among var1
(cdr e
)))
2524 (involve-var j var1 nn
*))
2527 ;; Why not implement this as (NOT (INVOLVE EXP NN*))?
2528 (defun notinvolve (exp nn
*)
2529 (cond ((atom exp
) t
)
2531 ((member (caar exp
) nn
* :test
#'eq
) (not (among var
(cdr exp
))))
2532 ((every #'(lambda (j) (notinvolve j nn
*))
2535 ;; Just like NOTINVOLVE, except the dependency on VAR is explicit
2536 ;; (second arg here). Use this instead.
2537 (Defun notinvolve-var
(exp var1 nn
*)
2542 ((member (caar exp
) nn
* :test
#'eq
)
2543 (not (among var1
(cdr exp
))))
2544 ((every #'(lambda (j)
2545 (notinvolve-var j var1 nn
*))
2548 (defun sheur1 (l1 l2
)
2550 (setq l1
(mapcar #'stirling0 l1
))
2551 (setq l2
(mapcar #'stirling0 l2
))
2552 (setq l1
(m+l
(maxi l1
)))
2553 (setq l2
(m+l
(maxi l2
)))
2554 (setq ans
(cpa l1 l2 t
))
2555 (return (cond ((=0 ans
) (m+ l1 l2
))
2556 ((equal ans
1.
) '$inf
)
2559 (defun zero-lim (cpa-list)
2560 (do ((l cpa-list
(cdr l
)))
2562 (and (eq (caar l
) 'gen
)
2563 (zerop2 (limit (cadar l
) var val
'think
))
2566 ;; Compare order of growth for R1 and R2. The result is 0, -1, +1
2567 ;; depending on the relative order of growth. 0 is returned if R1 and
2568 ;; R2 have the same growth; -1 if R1 grows much more slowly than R2;
2569 ;; +1 if R1 grows much more quickly than R2.
2570 (defun cpa (r1 r2 flag
)
2573 (cond ((alike1 t1 t2
) 0.
)
2575 (cond ((free t2 var
) 0.
)
2576 (t (let ((lim-ans (limit1 t2 var val
)))
2577 (cond ((not (member lim-ans
'($inf $minf $und $ind
) :test
#'eq
)) 0.
)
2580 (let ((lim-ans (limit1 t1 var val
)))
2581 (cond ((not (member lim-ans
'($inf $minf $und $ind
) :test
#'eq
)) 0.
)
2584 ;; Make T1 and T2 be a list of terms that are multiplied
2586 (cond ((mtimesp t1
) (setq t1
(cdr t1
)))
2587 (t (setq t1
(list t1
))))
2588 (cond ((mtimesp t2
) (setq t2
(cdr t2
)))
2589 (t (setq t2
(list t2
))))
2590 ;; Find the strengths of each term of T1 and T2
2591 (setq t1
(mapcar (function istrength
) t1
))
2592 (setq t2
(mapcar (function istrength
) t2
))
2593 ;; Compute the max of the strengths of the terms.
2594 (let ((ans (ismax t1
))
2596 (cond ((or (null ans
) (null d
))
2597 ;;(eq (car ans) 'gen) (eq (car d) 'gen))
2598 ;; ismax couldn't find highest term; give up
2601 (if (eq (car ans
) 'var
) (setq ans
(add-up-deg t1
)))
2602 (if (eq (car d
) 'var
) (setq d
(add-up-deg t2
)))
2603 ;; Can't just just compare dominating terms if there are
2604 ;; indeterm-inates present; e.g. X-X^2*LOG(1+1/X). So
2606 (cond ((or (zero-lim t1
)
2608 (cpa-indeterm ans d t1 t2 flag
))
2609 ((isgreaterp ans d
) 1.
)
2610 ((isgreaterp d ans
) -
1.
)
2613 (defun cpa-indeterm (ans d t1 t2 flag
)
2614 (cond ((not (eq (car ans
) 'var
))
2615 (setq ans
(gather ans t1
) d
(gather d t2
))))
2616 (let ((*indicator
(and (eq (car ans
) 'exp
)
2619 (setq test
(cpa1 ans d
))
2620 (cond ((and (zerop1 test
)
2621 (or (equal ($radcan
(m// (cadr ans
) (cadr d
))) 1.
)
2622 (and (polyp (cadr ans
))
2624 (equal (limit (m// (cadr ans
) (cadr d
)) var val
'think
)
2626 (let ((new-term1 (m// t1
(cadr ans
)))
2627 (new-term2 (m// t2
(cadr d
))))
2628 (cpa new-term1 new-term2 flag
)))
2631 (defun add-up-deg (strengthl)
2632 (do ((stl strengthl
(cdr stl
))
2635 ((null stl
) (list 'var
(m*l poxl
) (m+l degl
)))
2636 (cond ((eq (caar stl
) 'var
)
2637 (push (cadar stl
) poxl
)
2638 (push (caddar stl
) degl
)))))
2642 (cond ((eq (car p1
) 'gen
) (return 0.
)))
2643 (setq flag
(car p1
))
2648 (setq s1
(istrength p1
))
2649 (setq s2
(istrength p2
))
2652 ((isgreaterp s1 s2
) 1.
)
2653 ((isgreaterp s2 s1
) -
1.
)
2655 (setq *indicator nil
)
2657 ((and (poly? p1 var
) (poly? p2 var
))
2658 (setq p1
(m- p1 p2
))
2659 (cond ((zerop1 p1
) 0.
)
2660 (t (getsignl (hot-coef p1
)))))
2664 (list (m*t -
1 p2
))))
2665 (cond ((zerop2 s1
) 0.
)
2666 ((ratgreaterp s1
0.
) 1.
)
2670 (setq p1
(caddr p1
))
2671 (setq p2
(caddr p2
))
2672 (cond ((and (poly? p1 var
) (poly? p2 var
))
2673 (setq p1
(m- p1 p2
))
2674 (return (cond ((or (zerop1 p1
)
2675 (not (among var p1
)))
2677 (t (getsignl (hot-coef p1
))))))
2678 ((and (radicalp p1 var
) (radicalp p2 var
))
2681 (list (m*t -
1 p2
))))
2682 (return (cond ((eq s1
'$inf
) 1.
)
2683 ((eq s1
'$minf
) -
1.
)
2685 (cond ((ratgreaterp s1
0.
) 1.
)
2686 ((ratgreaterp 0. s1
) -
1.
)
2689 (t (return (cpa p1 p2 t
)))))
2691 (setq p1
(try-lhospital (asymredu p1
) (asymredu p2
) nil
))
2692 (return (cond ((zerop2 p1
) -
1.
)
2693 ((real-infinityp p1
) 1.
)
2696 ;;;EXPRESSIONS TO ISGREATERP ARE OF THE FOLLOWING FORMS
2697 ;;; ("VAR" POLY DEG)
2699 ;;; ("LOG" LOG(EXP))
2700 ;;; ("FACT" <A FACTORIAL EXPRESSION>)
2701 ;;; ("GEN" <ANY OTHER TYPE OF EXPRESSION>)
2703 (defun isgreaterp (a b
)
2706 (cond ((or (eq ta
'gen
)
2708 ((and (eq ta tb
) (eq ta
'var
))
2709 (ratgreaterp (caddr a
) (caddr b
)))
2710 ((and (eq ta tb
) (eq ta
'exp
))
2711 ;; Both are exponential order of infinity. Check the
2712 ;; exponents to determine which exponent is bigger.
2713 (eq (limit (m- `((%log
) ,(second a
)) `((%log
) ,(second b
)))
2716 ((member ta
(cdr (member tb
'(num log var exp fact gen
) :test
#'eq
)) :test
#'eq
)))))
2719 ;; Preprocess the list of products. Separate the terms that
2720 ;; exponentials and those that don't. Actually multiply the
2721 ;; exponential terms together to form a single term. Pass this and
2722 ;; the rest to ismax-core to find the max.
2723 (let (exp-terms non-exp-terms
)
2725 (if (eq 'exp
(car term
))
2726 (push term exp-terms
)
2727 (push term non-exp-terms
)))
2728 ;; Multiply the exp-terms together
2731 ;;(format t "exp-terms = ~A~%" exp-terms)
2732 (dolist (term exp-terms
)
2733 (setf product
(simplify (mul product
(second term
)))))
2734 ;;(format t "product = ~A~%" product)
2735 (setf product
`(exp ,($logcontract product
)))
2736 ;;(format t "product = ~A~%" product)
2737 (ismax-core (cons product non-exp-terms
)))
2740 (defun ismax-core (l)
2743 ((= (length l
) 1) (car l
)) ;If there is only 1 thing give it back.
2744 ((every #'(lambda (x)
2745 (not (eq (car x
) 'gen
))) l
)
2747 (do ((l1 (cdr l
) (cdr l1
))
2751 (cond ((isgreaterp temp-ans
(car l1
))
2752 (setq ans temp-ans
))
2753 ((isgreaterp (car l1
) temp-ans
)
2754 (setq temp-ans
(car l1
))
2755 (setq ans temp-ans
))
2756 (t (setq ans
())))))
2759 ;RETURNS LIST OF HIGH TERMS
2761 (cond ((atom all
) nil
)
2762 (t (do ((l (cdr all
) (cdr l
))
2764 (total 1) ; running total constant factor of hi-term
2765 (hi-terms (ncons (car all
)))
2767 ((null l
) (if (zerop2 total
) ; if high-order terms cancel each other
2768 all
; keep everything
2769 hi-terms
)) ; otherwise return list of high terms
2770 (setq compare
(limit (m// (car l
) hi-term
) var val
'think
))
2772 ((or (infinityp compare
)
2773 (and (eq compare
'$und
)
2774 (zerop2 (limit (m// hi-term
(car l
)) var val
'think
))))
2775 (setq total
1) ; have found new high term
2776 (setq hi-terms
(ncons (setq hi-term
(car l
)))))
2777 ((zerop2 compare
) nil
)
2778 ;; COMPARE IS IND, FINITE-VALUED, or und in both directions
2779 (t ; add to list of high terms
2780 (setq total
(m+ total compare
))
2781 (setq hi-terms
(append hi-terms
(ncons (car l
))))))))))
2785 (cond ((atom l
) (return nil
)))
2786 l1
(setq ans
(car l
))
2788 (cond ((null l
) (return ans
))
2789 ((ratgreaterp ans
(car l
)) (go l2
))
2794 (cond ((atom l
) (return nil
)))
2795 l1
(setq ans
(car l
))
2797 (cond ((null l
) (return ans
))
2798 ((ratgreaterp (car l
) ans
) (go l2
))
2806 ((or (mnump e
) (not (among var e
))) nil
)
2807 ((and (mexptp e
) (eq (cadr e
) var
))
2808 (push (caddr e
) nn
*))
2810 (t (mapc #'pofx
(cdr e
)))))
2813 (cond ((member val
'($zeroa $zerob
) :test
#'eq
) nil
)
2814 (t (setq e
(subin (m+ var val
) e
))))
2816 (cond ((pofx e
) e
)))
2818 (defun gather (ind l
)
2820 (setq ind
(car ind
))
2821 loop
(cond ((null l
)
2822 (return (list ind
(m*l ans
))))
2823 ((equal (caar l
) ind
)
2824 (push (cadar l
) ans
)))
2828 ; returns rough class-of-growth of term
2829 (defun istrength (term)
2830 (cond ((mnump term
) (list 'num term
))
2831 ((atom term
) (cond ((eq term var
)
2833 (t (list 'num term
))))
2834 ((not (among var term
)) (list 'num term
))
2836 (let ((temp (ismax-core (mapcar #'istrength
(cdr term
)))))
2837 (cond ((not (null temp
)) temp
)
2840 (let ((temp (mapcar #'istrength
(cdr term
)))
2842 (setq temp1
(ismax temp
))
2843 (cond ((null temp1
) `(gen ,term
))
2844 ((eq (car temp1
) 'log
) `(log ,temp
))
2845 ((eq (car temp1
) 'var
) (add-up-deg temp
))
2848 (real-infinityp (limit term var val t
)))
2849 (let ((logterm (logred term
)))
2850 (cond ((and (among var
(caddr term
))
2851 (member (car (istrength logterm
))
2852 '(var exp fact
) :test
#'eq
)
2853 (real-infinityp (limit logterm var val t
)))
2854 (list 'exp
(m^
'$%e logterm
)))
2855 ((not (among var
(caddr term
)))
2856 (let ((temp (istrength (cadr term
))))
2857 (cond ((not (alike1 temp term
))
2858 (rplaca (cdr temp
) term
)
2859 (and (eq (car temp
) 'var
)
2861 (m* (caddr temp
) (caddr term
))))
2865 ((and (eq (caar term
) '%log
)
2866 (real-infinityp (limit term var val t
)))
2867 (let ((stren (istrength (cadr term
))))
2868 (cond ((member (car stren
) '(log var
) :test
#'eq
)
2870 ((and (eq (car stren
) 'exp
)
2871 (eq (caar (second stren
)) 'mexpt
))
2872 (istrength (logred (second stren
))))
2874 ((eq (caar term
) 'mfactorial
)
2876 ((let ((temp (hyperex term
)))
2877 (and (not (alike1 term temp
))
2879 (t (list 'gen term
))))
2881 ;; log reduce - returns log of s1
2882 ;; When s1 is not of the form a^b, this code doesn't return log(s1).
2883 ;; Running the testsuite does send non exptp expressions to this function.
2885 (or (and (eq (cadr s1
) '$%e
) (caddr s1
))
2886 (m* (caddr s1
) `((%log
) ,(cadr s1
)))))
2888 (defun asymredu (rd)
2889 (cond ((atom rd
) rd
)
2891 ((not (among var rd
)) rd
)
2892 ((polyinx rd var t
))
2894 (cond ((eq (cadr rd
) var
) rd
)
2896 (factor ($expand
(m^
(polyinx (cadr rd
) var t
)
2900 (t (simplify (cons (list (caar rd
))
2901 (mapcar #'asymredu
(cdr rd
)))))))
2904 (let ((dn** ()) (nn** ()))
2905 (cond ((atom rd
) rd
)
2907 ((not (among var rd
)) rd
)
2911 (cond ((eq (cadr rd
) var
) rd
)
2912 (t (setq dn
** (caddr rd
))
2913 (setq nn
** (factor (cadr rd
)))
2914 (cond ((mtimesp nn
**)
2915 (m*l
(mapcar #'(lambda (j) (m^ j dn
**))
2918 (t (simplify (cons (ncons (caar rd
))
2919 (mapcar #'rdfact
(cdr rd
))))))))
2921 (defun cnv (expl val
)
2922 (mapcar #'(lambda (e)
2923 (maxima-substitute (cond ((eq val
'$zerob
)
2924 (m* -
1 (m^ var -
1)))
2929 (t (m^
(m+ var
(m* -
1 val
)) -
1.
)))
2934 ;; We could replace pwtaylor with a call to (tlimit-taylor exp var l terms).
2935 ;; Other than eliminating clutter, I know of no advantages to doing so.
2936 (defun pwtaylor (exp var l terms
)
2937 (prog (coef ans c mc
)
2938 (cond ((=0 terms
) (return nil
)) ((=0 l
) (setq mc t
)))
2941 loop
(setq c
(1+ c
))
2942 (cond ((or (> c
10.
) (equal c terms
))
2944 (t (setq exp
(sdiff exp var
))))
2945 tag1
(setq coef
($radcan
(subin l exp
)))
2946 (cond ((=0 coef
) (setq terms
(1+ terms
)) (go loop
)))
2953 (m^
`((mfactorial) ,c
) -
1)
2954 (m^
(if mc var
(m+t
(m*t -
1 l
) var
)) c
)))))
2959 ((simplerd e
) (rdtay e
))
2960 (t (cons (list (caar e
))
2961 (mapcar #'rdsget
(cdr e
))))))
2964 (cond (limit-using-taylor ($ratdisrep
($taylor rd var val
1.
)))
2968 (prog (varlist p c e d $ratfac
)
2969 (setq varlist
(ncons var
))
2970 (setq p
(ratnumerator (cdr (ratrep* (cadr rd
)))))
2971 (cond ((< (length p
) 3.
) (return rd
)))
2974 (setq c
(m^ var
(m* d e
)))
2975 (setq d
($ratsimp
(varinvert (m* (pdis p
) (m^ var
(m- d
)))
2977 (setq d
(pwtaylor (m^ d e
) var
0.
3.
))
2978 (return (m* c
(varinvert d var
)))))
2980 (defun varinvert (e var
) (subin (m^t var -
1.
) e
))
2983 (prog ((varlist (list var
)))
2984 (return (let (($ratfac nil
))
2986 (pdegr (cadr (ratrep* p
)))))))
2988 ;; Like DEG, but dependency on VAR is explicit. Use this instead when
2990 (defun deg-var (p var1
)
2991 (prog ((varlist (list var1
)))
2992 (flet ((pdegr-var (pf)
2993 (cond ((or (atom pf
)
2994 (not (eq (caadr (ratf var1
))
2998 (cadr (reverse pf
)))
3001 (return (let (($ratfac nil
))
3003 (pdegr-var (cadr (ratrep* p
))))))))
3005 (defun rat-no-ratfac (e)
3006 (let (($ratfac nil
))
3011 (defun rddeg (rd low
*)
3012 (cond ((or (mnump rd
)
3013 (not (among var rd
)))
3018 (m* (deg (cadr rd
)) (caddr rd
)))
3020 (addn (mapcar #'(lambda (j)
3024 (setq rd
(andmapcar #'(lambda (j) (rddeg j low
*))
3026 (cond (low* (ratmin rd
))
3030 (cond ((or (atom pf
) (not (eq (caadr (ratf var
)) (car pf
))))
3032 (low* (cadr (reverse pf
)))
3034 ;;There is some confusion here. We need to be aware of Branch cuts etc....
3035 ;;when doing this section of code. It is not very carefully done so there
3036 ;;are bugs still lurking. Another misfortune is that LIMIT or its inferiors
3037 ;;sometimes decides to change the limit VAL in midstream. This must be corrected
3038 ;;since LIMIT's interaction with the data base environment must be maintained.
3039 ;;I'm not sure that this code can ever be called with VAL other than $INF but
3040 ;;there is a hook in the first important cond clause to cathc them anyway.
3043 (let ((num-power (rddeg n nil
))
3044 (den-power (rddeg d nil
))
3045 (coef ()) (coef-sign ()) (power ()))
3046 (setq coef
(m// ($ratcoef
($expand n
) var num-power
)
3047 ($ratcoef
($expand d
) var den-power
)))
3048 (setq coef-sign
(getsignl coef
))
3049 (setq power
(m// num-power den-power
))
3050 (cond ((eq (ask-integer power
'$integer
) '$integer
)
3051 (cond ((eq (ask-integer power
'$even
) '$even
) '$even
)
3052 (t '$odd
)))) ;Can be extended from here.
3053 (cond ((or (eq val
'$minf
)
3056 (equal val
0)) ()) ;Can be extended to cover some these.
3057 ((ratgreaterp den-power num-power
)
3058 (cond ((equal coef-sign
1.
) '$zeroa
)
3059 ((equal coef-sign -
1) '$zerob
)
3060 ((equal coef-sign
0) 0)
3062 ((ratgreaterp num-power den-power
)
3063 (cond ((equal coef-sign
1.
) '$inf
)
3064 ((equal coef-sign -
1) '$minf
)
3065 ((equal coef-sign
0) nil
) ; should never be zero
3066 ((null coef-sign
) '$infinity
)))
3069 (defun radlim (e n d
)
3071 (cond ((eq val
'$infinity
) (throw 'limit nil
))
3073 (setq nl
(m* var -
1))
3074 (setq n
(subin nl n
))
3075 (setq d
(subin nl d
))
3076 (setq val
'$inf
))) ;This is the Culprit. Doesn't tell the DATABASE.
3077 (cond ((eq val
'$inf
)
3078 (setq nl
(asymredu n
))
3079 (setq dl
(asymredu d
))
3081 ((or (rptrouble n
) (rptrouble d
))
3082 (return (limit (m* (rdsget n
) (m^
(rdsget d
) -
1.
)) var val t
)))
3083 (t (return (asy nl dl
))))))
3084 (setq nl
(limit n var val t
))
3085 (setq dl
(limit d var val t
))
3086 (cond ((and (zerop2 nl
) (zerop2 dl
))
3087 (cond ((or (polyp n
) (polyp d
))
3088 (return (try-lhospital-quit n d t
)))
3089 (t (return (ser0 e n d val
)))))
3090 (t (return ($radcan
(ratrad (m// n d
) n d nl dl
)))))))
3092 (defun ratrad (e n d nl dl
)
3094 (cond ((equal nl
0) (return 0))
3097 (cond ((equal dl
0) (setq d1
'$infinity
)) ;No direction Info.
3100 ((equal (setq d
(behavior d var val
)) 1)
3102 ((equal d -
1) (setq d1
'$minf
))
3103 (t (throw 'limit nil
))))
3106 (cond ((equal (setq n
(behavior n var val
)) 1)
3108 ((equal n -
1) (setq n1
'$zerob
))
3110 (t (return ($radcan
(ridofab (subin val e
))))))
3111 (return (simplimtimes (list n1 d1
)))))
3113 ;;; Limit(log(XXX), var, 0, val), where val is either zerob (limit from below)
3114 ;;; or zeroa (limit from above).
3115 (defun simplimln (expr var val
)
3116 (let ((arglim (limit (cadr expr
) var val
'think
)) (dir))
3117 (cond ((eq arglim
'$inf
) '$inf
) ;log(inf) = inf
3118 ;;log(minf,infinity,zerob) = infinity & log(0) = infinity
3119 ((or (member arglim
'($minf $infinity $zerob
)))
3121 ((eq arglim
'$zeroa
) '$minf
) ;log(zeroa) = minf
3122 ;; log(ind) = ind when ind > 0 else und
3124 (if (eq t
(mgrp (cadr expr
) 0)) '$ind
'$und
))
3126 ((eq arglim
'$und
) '$und
)
3127 ((member arglim
'($ind $und
)) '$und
)
3128 ;; log(1^(-)) = zerob, log(1^(+)) = zeroa & log(1)=0
3130 (if (or (eq val
'$zerob
) (eq var
'$zeroa
)) val
0))
3131 ;; Special case of arglim = 0
3133 (setq dir
(behavior (cadr expr
) var val
))
3134 (cond ((eql dir -
1) '$infinity
)
3135 ((eql dir
0) '$infinity
)
3136 ((eql dir
1) '$minf
)))
3137 ;; When arglim is off the negative real axis, use direct substitution
3138 ((off-negative-real-axisp arglim
)
3139 (ftake '%log arglim
))
3141 ;; We know that arglim is a negative real number, say xx.
3142 ;; When the imaginary part of (cadr expr) near var is negative,
3143 ;; return log(-x) - %i*pi; when the imaginary part of (cadr expr)
3144 ;; near var is positive return log(-x) + %i*pi; and when
3145 ;; we cannot determine the behavior of the imaginary part,
3146 ;; return a nounform. The value of val (either zeroa or zerob)
3147 ;; determines what is meant by "near" (smaller than var when
3148 ;; val is zerob and larger than var when val is zeroa).
3149 (setq dir
(behavior ($imagpart
(cadr expr
)) var val
))
3150 (cond ((or (eql dir
1) (eql dir -
1))
3151 (add (ftake '%log
(mul -
1 arglim
)) (mul dir
'$%i
'$%pi
)))
3152 (t (throw 'limit nil
))))))) ;do a nounform return
3153 (setf (get '%log
'simplim%function
) 'simplimln
)
3154 (setf (get '%plog
'simplim%function
) 'simplimln
)
3156 (defun simplim%limit
(e x pt
)
3157 (declare (ignore e x pt
))
3159 (setf (get '%limit
'simplim%function
) 'simplim%limit
)
3161 (defun simplim%unit_step
(e var val
)
3162 (let ((lim (limit (cadr e
) var val
'think
)))
3163 (cond ((eq lim
'$und
) '$und
)
3164 ((eq lim
'$ind
) '$ind
)
3165 ((eq lim
'$zerob
) 0)
3166 ((eq lim
'$zeroa
) 1)
3167 ((not (lenient-realp lim
)) (throw 'limit nil
)) ;catches infinity too
3168 ;; catches minf & inf cases
3169 ((eq t
(mgrp 0 lim
)) 0)
3170 ((eq t
(mgrp lim
0)) 1)
3172 (setf (get '$unit_step
'simplim%function
) 'simplim%unit_step
)
3174 (defun simplim%conjugate
(e var val
)
3175 (let ((lim (limit (cadr e
) var val
'think
)))
3176 (cond ((off-negative-real-axisp lim
)
3177 (ftake '$conjugate lim
))
3178 (t (throw 'limit nil
)))))
3179 (setf (get '$conjugate
'simplim%function
) 'simplim%conjugate
)
3181 (defun simplim%imagpart
(e var val
)
3182 (let ((lim (limit (cadr e
) var val
'think
)))
3183 (cond ((eq lim
'$und
) '$und
)
3185 ((eq lim
'$infinity
) (throw 'limit nil
))
3186 (t (mfuncall '$imagpart lim
)))))
3187 (setf (get '$imagpart
'simplim%function
) 'simplim%imagpart
)
3188 (setf (get '%imagpart
'simplim%function
) 'simplim%imagpart
)
3190 (defun simplim%realpart
(e var val
)
3191 (let ((lim (limit (cadr e
) var val
'think
)))
3192 (cond ((eq lim
'$und
) '$und
)
3193 ((eq lim
'$ind
) '$ind
)
3194 ((eq lim
'$infinity
) (throw 'limit nil
))
3195 (t (mfuncall '$realpart lim
)))))
3196 (setf (get '$realpart
'simplim%function
) 'simplim%realpart
)
3197 (setf (get '%realpart
'simplim%function
) 'simplim%realpart
)
3198 ;;; Limit of the Factorial function
3200 (defun simplimfact (expr var val
)
3201 (let* ((arglim (limit (cadr expr
) var val
'think
)) ; Limit of the argument.
3203 (cond ((eq arglim
'$inf
) '$inf
)
3204 ((member arglim
'($minf $infinity $und $ind
) :test
#'eq
) '$und
)
3206 ((and (or (maxima-integerp arglim
)
3207 (setq arg2
(integer-representation-p arglim
)))
3208 (eq ($sign arg2
) '$neg
))
3209 ;; A negative integer or float or bigfloat representation.
3210 (let ((dir (limit (add (cadr expr
) (mul -
1 arg2
)) var val
'think
))
3211 (even (mevenp arg2
)))
3212 (cond ((or (and even
3222 (t (throw 'limit nil
)))))
3224 ;; Call simplifier to get value at the limit of the argument.
3225 (simplify (list '(mfactorial) arglim
))))))
3226 (setf (get 'mfactorial
'simplim%function
) 'simplimfact
)
3228 (defun simplim%erf-%tanh
(fn arg
)
3229 (let ((arglim (limit arg var val
'think
))
3232 (cond ((eq arglim
'$inf
) 1)
3233 ((eq arglim
'$minf
) -
1)
3234 ((eq arglim
'$infinity
)
3235 (destructuring-bind (rpart . ipart
)
3237 (setq rlim
(limit rpart var origval
'think
))
3238 (cond ((eq fn
'%tanh
)
3239 (cond ((equal rlim
'$inf
) 1)
3240 ((equal rlim
'$minf
) -
1)))
3242 (setq ans
(limit (m* rpart
(m^t ipart -
1)) var origval
'think
))
3243 (setq ans
($asksign
(m+ `((mabs) ,ans
) -
1)))
3244 (cond ((or (eq ans
'$pos
) (eq ans
'$zero
))
3245 (cond ((eq rlim
'$inf
) 1)
3246 ((eq rlim
'$minf
) -
1)
3249 ((eq arglim
'$und
) '$und
)
3250 ((member arglim
'($zeroa $zerob $ind
) :test
#'eq
) arglim
)
3251 ;;;Ignore tanh(%pi/2*%I) and multiples of the argument.
3253 ;; erf (or tanh) of a known value is just erf(arglim).
3254 (simplify (list (ncons fn
) arglim
))))))
3256 (defun in-domain-of-atan (z)
3257 (setq z
(trisplit z
)) ; split z into real and imaginary parts
3258 (let ((x (car z
)) (y (cdr z
))) ;z = x+%i*y
3261 (eq t
(meqp x
0)) ;Re(z) = 0
3262 (or (eq t
(mgqp -
1 y
)) ;-1 >= Im(z)
3263 (eq t
(mgqp y
1))))))) ; Im(z) >= 1
3265 (defun simplim%atan
(e x pt
)
3266 (let ((lim (limit (cadr e
) x pt
'think
)))
3267 (cond ((or (eq lim
'$zeroa
) (eq lim
'$zerob
) (eq lim
0) (eq lim
'$ind
)) lim
)
3268 ((or (eq lim
'$und
) (eq lim
'$infinity
)) (throw 'limit nil
))
3269 ((eq lim
'$inf
) #$%pi
/2$
) ;atan(inf) -> %pi/2
3270 ((eq lim
'$minf
) #$-%pi
/2$
) ;atan(-inf) -> -%pi/2
3271 ((in-domain-of-atan (ridofab lim
)) ; direct substitution
3272 (ftake '%atan
(ridofab lim
)))
3273 (t (limit ($logarc e
) x pt
'think
)))))
3274 (setf (get '%atan
'simplim%function
) 'simplim%atan
)
3276 (defmvar extended-reals
3277 (append *infinitesimals
* *infinities
* (list '$und
'$ind
)))
3279 ;; Most instances of atan2 are simplified to atan expressions, but this routine
3280 ;; handles tricky cases such as limit(atan2((x^2-2), x^3-2*x), x, sqrt(2), minus).
3281 ;; Taylor and Gruntz cannot handle the discontinuity at atan(0, -1)
3283 ;; When possible, we want to evaluate the limit of an atan2 expression using
3284 ;; direct substitution--that produces, I think, the least surprising values.
3286 ;; The general simplifier catches atan2(0,0) and it transforms atan2(minf or inf,X)
3287 ;; and atan2(X, minf or inf) into an atan expression, but it doesn't catch
3288 ;; atan2(X, zerob or zeroa) or atan2(zerob or zeroa, X). For the other extended
3289 ;; real (ind,und, or infinity) arguments, the general simplifier gives sign errors.
3291 (defun simplim%atan2
(e v pt
)
3292 (let ((y (second e
))
3299 (setq xlim
(limit x v pt
'think
))
3300 (setq ylim
(limit y v pt
'think
))
3301 (setq xlim-z
(ridofab xlim
)
3302 ylim-z
(ridofab ylim
))
3303 ;; For cases for which direct substitution fails, normalize
3304 ;; x & y and try again.
3305 (setq q
(cond ((eq xlim
'$inf
) x
)
3311 ((and (eq xlim
'$zerob
) (zerop2 ylim
))
3313 ((and (eq xlim
'$zeroa
) (zerop2 ylim
))
3315 ((and (eq ylim
'$zerob
) (zerop2 xlim
))
3317 ((and (eq ylim
'$zeroa
) (zerop2 xlim
))
3321 (when (not (eql q
1))
3324 (setq xlim
(limit x v pt
'think
))
3325 (setq ylim
(limit y v pt
'think
))
3326 (setq xlim-z
(ridofab xlim
)
3327 ylim-z
(ridofab ylim
)))
3330 ((and (eq '$zerob ylim
) (eq t
(mgrp 0 xlim
)))
3332 ((and (eq '$zerob ylim
) (eq t
(mgrp xlim
0)))
3334 ((and (eq '$zeroa ylim
) (eq t
(mgrp 0 xlim
)))
3336 ((and (eq '$zeroa ylim
) (eq t
(mgrp xlim
0)))
3338 ((and (eql xlim
1) (eql ylim
'$inf
))
3340 ((and (eql xlim -
1) (eql ylim
0))
3343 ;; Use direct substitution when ylim-z # 0 or xlim-z > 0. We need
3344 ;; to check that xlim-z & ylim-z are real too.
3345 ((and (lenient-realp xlim-z
)
3346 (lenient-realp ylim-z
)
3347 (or (eq t
(mnqp ylim-z
0))
3348 (eq t
(mgrp xlim-z
0))))
3349 (ftake '%atan2 ylim-z xlim-z
))
3351 (throw 'limit nil
)))))
3352 (setf (get '%atan2
'simplim%function
) 'simplim%atan2
)
3354 (defun simplimsch (sch arg
)
3355 (cond ((real-infinityp arg
)
3356 (cond ((eq sch
'%sinh
) arg
) (t '$inf
)))
3357 ((eq arg
'$infinity
) '$infinity
)
3358 ((eq arg
'$ind
) '$ind
)
3359 ((eq arg
'$und
) '$und
)
3360 ((and (eq sch
'%sinh
)
3361 (or (eq arg
'$zerob
) (eq arg
'$zeroa
)))
3363 (t (let (($exponentialize t
))
3364 (resimplify (list (ncons sch
) (ridofab arg
)))))))
3366 ;; simple limit of sin and cos
3367 (defun simplimsc (exp fn arg
)
3368 (cond ((member arg
'($inf $minf $ind
) :test
#'eq
) '$ind
)
3369 ((member arg
'($und $infinity
) :test
#'eq
)
3371 ((member arg
'($zeroa $zerob
) :test
#'eq
)
3372 (cond ((eq fn
'%sin
) arg
)
3373 (t (m+ 1 '$zerob
))))
3375 (simplify (list (ncons fn
) (ridofab arg
)))
3378 (defun simplim%tan
(arg)
3379 (let ((arglim (limit arg var val
'think
)))
3381 ((member arglim
'($inf $minf $ind
) :test
#'eq
)
3383 ((member arglim
'($und $infinity
) :test
#'eq
)
3386 ;; Write the limit of the argument as c*%pi + rest.
3388 ((c (or (pip arglim
) 0))
3389 (rest (sratsimp (m- arglim
(m* '$%pi c
))))
3391 ;; Check if tan(x) has a zero or pole at x=arglim.
3392 ;; zero: tan(n*%pi + 0*)
3393 ;; pole: tan((2*n+1)*%pi/2 + 0*)
3394 ;; 0* can be $zeroa, $zerob or 0.
3395 (if (and (member rest
'(0 $zeroa $zerob
) :test
#'equal
)
3396 (or (setq hit-zero
(integerp c
))
3397 (and (ratnump c
) (equal (caddr c
) 2))))
3398 ;; This is a zero or a pole.
3399 ;; Determine on which side of the zero/pole we are.
3400 ;; If rest is $zeroa or $zerob, use that.
3401 ;; Otherwise (rest = 0), try to determine the side
3402 ;; using the behavior of the argument.
3404 ((side (cond ((eq rest
'$zeroa
) 1)
3405 ((eq rest
'$zerob
) -
1)
3406 (t (behavior arg var val
)))))
3408 ;; For a zero, if we don't know the side, just return 0.
3410 ((equal side
1) '$zeroa
)
3411 ((equal side -
1) '$zerob
)
3413 ;; For a pole, we need to know the side.
3414 ;; Otherwise, we can't determine the limit.
3416 ((equal side
1) '$minf
)
3417 ((equal side -
1) '$inf
)
3418 (t (throw 'limit t
)))))
3419 ;; No zero or pole - substitute in the limit of the argument.
3420 (take '(%tan
) (ridofab arglim
))))))))
3422 (defun simplim%asinh
(arg)
3423 (cond ((member arg
'($inf $minf $zeroa $zerob $ind $und
) :test
#'eq
)
3425 ((eq arg
'$infinity
) '$und
)
3426 (t (simplify (list '(%asinh
) (ridofab arg
))))))
3428 (defun simplim%acosh
(arg)
3429 (cond ((equal (ridofab arg
) 1) '$zeroa
)
3430 ((eq arg
'$inf
) arg
)
3431 ((eq arg
'$minf
) '$infinity
)
3432 ((member arg
'($und $ind $infinity
) :test
#'eq
) '$und
)
3433 (t (simplify (list '(%acosh
) (ridofab arg
))))))
3435 (defun simplim%atanh
(arg dir
)
3436 ;; Compute limit(atanh(x),x,arg). If ARG is +/-1, we need to take
3437 ;; into account which direction we're approaching ARG.
3438 (cond ((zerop2 arg
) arg
)
3439 ((member arg
'($ind $und $infinity $minf $inf
) :test
#'eq
)
3441 ((equal (setq arg
(ridofab arg
)) 1.
)
3442 ;; The limit at 1 should be complex infinity because atanh(x)
3443 ;; is complex for x > 1, but inf if we're approaching 1 from
3445 (if (eq dir
'$zerob
)
3449 ;; Same as above, except for the limit is at -1.
3450 (if (eq dir
'$zeroa
)
3453 (t (simplify (list '(%atanh
) arg
)))))
3455 (defun simplim%asin
(e x pt
)
3456 (let ((lim (limit (cadr e
) x pt
'think
)) (dir) (lim-sgn))
3457 (cond ((member lim
'($zeroa $zerob
)) lim
) ;asin(zeoroa/b) = zeroa/b
3458 ((member lim
'($minf
'$inf
'$infinity
)) '$infinity
)
3459 ((eq lim
'$ind
) '$ind
) ;asin(ind)=ind
3460 ((eq lim
'$und
) '$und
) ;asin(und)=und
3461 ((in-domain-of-asin lim
) ;direct substitution
3464 (setq e
(trisplit (cadr e
))) ;overwrite e!
3465 (setq dir
(behavior (cdr e
) x pt
))
3466 (setq lim-sgn
($csign
(car e
))) ;lim-sgn = sign limit(Re(e))
3469 (throw 'limit t
)) ;unable to find behavior of imaginary part
3471 ;; For the values of asin on the branch cuts, see DLMF 4.23.20 & 4.23.21
3472 ;; Diagram of the values of asin just above and below the branch cuts
3474 ;; asin(x) pi - asin(x)
3475 ;;................ -1 ....0.... 1 ...............
3476 ;; -pi - asin(x) asin(x)
3478 ;; Let's start in northwest and rotate counterclockwise:
3479 ((and (eq '$neg lim-sgn
) (eq dir
1))
3481 ((and (eq '$pos lim-sgn
) (eq dir
1))
3482 (sub '$%pi
(ftake '%asin lim
)))
3483 ((and (eq '$pos lim-sgn
) (eq dir -
1))
3485 ((and (eq '$neg lim-sgn
) (eq dir -
1))
3486 (sub (mul -
1 '$%pi
) (ftake '%asin lim
)))
3488 ;; unable to find sign of real part of lim.
3489 (throw 'limit t
)))))))
3490 (setf (get '%asin
'simplim%function
) 'simplim%asin
)
3492 (defun simplim%acos
(e x pt
)
3493 (let ((lim (limit (cadr e
) x pt
'think
)) (dir) (lim-sgn))
3494 (cond ((in-domain-of-asin lim
) ;direct substitution
3496 ((member lim
'($und $ind $inf $minf $infinity
)) ;boundary cases
3499 (setq e
(trisplit (cadr e
))) ;overwrite e!
3500 (setq dir
(behavior (cdr e
) x pt
))
3501 (setq lim-sgn
($csign lim
))
3504 (throw 'limit t
)) ;unable to find behavior of imaginary part
3505 ;; for the values of acos on the branch cuts, see DLMF 4.23.24 & 4.23.25
3506 ;; http://dlmf.nist.gov/4.23.E24
3507 ((or (eq '$pos lim-sgn
) (eq '$neg lim-sgn
))
3508 ;; continuous from above
3509 (if (eql dir
1) (ftake '%acos lim
) (sub (mul 2 '$%pi
) (ftake '%acos lim
))))
3511 ;; unable to find sign of real part of lim.
3512 (throw 'limit t
)))))))
3513 (setf (get '%acos
'simplim%function
) 'simplim%acos
)
3515 ;; Limit of an %integrate expression. For a definite integral
3516 ;; integrate(ee,var,a,b), when ee is free of the limit variable
3517 (defun simplim%integrate
(e x pt
)
3518 (let* ((ee (second e
)) ;ee = integrand
3519 (var (third e
)) ;integration variable
3520 (a (fourth e
)) ;lower limit or nil if indefinite
3521 (b (fifth e
)) ;lower limit or nil if indefinite
3523 (cond ((and a b
($freeof x ee
) ($freeof x var
))
3524 (setq alim
(limit a x pt
'think
))
3525 (setq blim
(limit b x pt
'think
))
3526 (if (and (lenient-extended-realp alim
)
3527 (lenient-extended-realp blim
)
3528 (not (eq alim
'$infinity
))
3529 (not (eq blim
'$infinity
)))
3530 (ftake '%integrate ee var alim blim
)
3533 (throw 'limit t
)))))
3534 (setf (get '%integrate
'simplim%function
) 'simplim%integrate
)
3536 (defun subftake (op subarg arg
)
3537 (simplifya (subfunmake op subarg arg
) t
))
3539 (defun off-one-to-inf (z)
3540 (setq z
(trisplit z
)) ; split z into x+%i*y
3542 (eq t
(mnqp (cdr z
) 0)) ; y # 0
3543 (eq t
(mgrp 1 (car z
))))) ; x < 1
3545 (defun simplim%li
(expr x pt
)
3546 (let ((n (car (subfunsubs expr
))) (e (car (subfunargs expr
))))
3548 (setq e
(limit e x pt
'think
))
3549 (cond ((and (eq e
'$minf
) (integerp n
) (>= n
2))
3551 ((and (eq e
'$inf
) (integerp n
) (>= n
2))
3553 ((or (eql (ridofab e
) 1) (and (not (extended-real-p e
)) (off-one-to-inf e
)))
3554 ;; Limit of li[s](1) can be evaluated by just
3555 ;; substituting in 1.
3556 ;; Same for li[s](x) when x is < 1.
3557 (subftake '$li
(list n
) (list e
)))
3558 (t (throw 'limit nil
))))
3559 ;; Claim ignorance when order depends on limit variable.
3560 (t (throw 'limit nil
)))))
3562 (setf (get '$li
'simplim%function
) 'simplim%li
)
3563 (setf (get '%li
'simplim%function
) 'simplim%li
)
3565 (defun simplim$psi
(order arg val
)
3566 (if (and (not (equal (length order
) 1))
3567 (not (equal (length arg
) 1)))
3569 (setq order
(car order
)
3571 (cond ((equal order
0)
3572 (destructuring-bind (rpart . ipart
)
3574 (cond ((not (equal ipart
0)) (throw 'limit
()))
3575 (t (setq rpart
(limit rpart var val
'think
))
3576 (cond ((eq rpart
'$zeroa
) '$minf
)
3577 ((eq rpart
'$zerob
) '$inf
)
3578 ((eq rpart
'$inf
) '$inf
)
3579 ((eq rpart
'$minf
) '$und
)
3580 ((equal (getsignl rpart
) -
1) (throw 'limit
()))
3581 (t (simplify (subfunmake '$psi
(list order
)
3582 (list rpart
)))))))))
3583 ((and (integerp order
) (> order
0)
3584 (equal (limit arg var val
'think
) '$inf
))
3585 (cond ((mevenp order
) '$zerob
)
3586 ((moddp order
) '$zeroa
)
3587 (t (throw 'limit
()))))
3588 (t (throw 'limit
()))))
3590 (defun simplim%inverse_jacobi_ns
(arg m
)
3591 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
3593 `((%inverse_jacobi_ns
) ,arg
,m
)))
3595 (defun simplim%inverse_jacobi_nc
(arg m
)
3596 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
3597 `((%elliptic_kc
) ,m
)
3598 `((%inverse_jacobi_nc
) ,arg
,m
)))
3600 (defun simplim%inverse_jacobi_sc
(arg m
)
3601 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
3602 `((%elliptic_kc
) ,m
)
3603 `((%inverse_jacobi_sc
) ,arg
,m
)))
3605 (defun simplim%inverse_jacobi_dc
(arg m
)
3606 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
3607 `((%elliptic_kc
) ,m
)
3608 `((%inverse_jacobi_dc
) ,arg
,m
)))
3610 (defun simplim%inverse_jacobi_cs
(arg m
)
3611 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
3613 `((%inverse_jacobi_cs
) ,arg
,m
)))
3615 (defun simplim%inverse_jacobi_ds
(arg m
)
3616 (if (or (eq arg
'$inf
) (eq arg
'$minf
))
3618 `((%inverse_jacobi_ds
) ,arg
,m
)))
3620 (defun simplim%signum
(e x pt
)
3621 (let ((e (limit (cadr e
) x pt
'think
)) (sgn))
3622 (cond ((eq '$minf e
) -
1)
3624 ((eq '$infinity e
) '$und
)
3625 ((eq '$ind e
) '$ind
)
3630 (setq sgn
(mnqp e
0))
3631 (cond ((eq t sgn
) (ftake '%signum e
))
3632 (t (throw 'limit nil
))))))) ; don't know
3633 (setf (get '%signum
'simplim%function
) 'simplim%signum
)
3635 ;; more functions for limit to handle
3637 (defun lfibtophi (e)
3638 (cond ((not (involve e
'($fib
))) e
)
3639 ((eq (caar e
) '$fib
)
3640 (let ((lnorecurse t
))
3641 ($fibtophi
(list '($fib
) (lfibtophi (cadr e
))) lnorecurse
)))
3643 (mapcar #'lfibtophi
(cdr e
))))))
3645 ;;; FOLLOWING CODE MAKES $LDEFINT WORK
3647 (defmfun $ldefint
(exp var ll ul
&aux $logabs ans a1 a2
)
3648 (setq $logabs t ans
(sinint exp var
)
3649 a1
(toplevel-$limit ans var ul
'$minus
)
3650 a2
(toplevel-$limit ans var ll
'$plus
))
3651 (and (member a1
'($inf $minf $infinity $und $ind
) :test
#'eq
)
3652 (setq a1
(nounlimit ans var ul
)))
3653 (and (member a2
'($inf $minf $infinity $und $ind
) :test
#'eq
)
3654 (setq a2
(nounlimit ans var ll
)))
3655 ($expand
(m- a1 a2
)))
3657 (defun nounlimit (exp var val
)
3658 (setq exp
(restorelim exp
))
3659 (nconc (list '(%limit
) exp var
(ridofab val
))
3660 (cond ((eq val
'$zeroa
) '($plus
))
3661 ((eq val
'$zerob
) '($minus
)))))
3663 ;; substitute inside noun form of %derivative
3664 ;; for cases such as limit('diff(x+2,x), x, 1)
3665 ;; -> limit('diff(xx+3), xx, 0)
3667 ;; maxima-substitute with *atp* skips over %derivative
3669 ;; substitutes diff(f(realvar), realvar, n)
3670 ;; -> diff(f(var+val), var, n)
3671 (defun derivative-subst (exp val var realvar
)
3672 (cond ((atom exp
) exp
)
3673 ((eq '%derivative
(caar exp
))
3676 (cons ;; the function being differentiated
3677 (maxima-substitute (m+ val var
) realvar
(cadr exp
))
3678 (cons ;; the var of differentiation
3679 (maxima-substitute var realvar
(caddr exp
))
3680 (cdddr exp
))))) ;; the order of the derivative
3682 (mapcar (lambda (x) (derivative-subst x val var realvar
))
3686 (or (involve e
'(%sin %cos %tan
))
3687 (among '$%i
(%einvolve e
))))
3689 ;; Like OSCIP, but the dependency on VAR is explicit (second arg).
3690 ;; Use this instead when possible.
3691 (defun oscip-var (e var1
)
3692 (or (involve-var e var1
'(%sin %cos %tan
))
3693 (among '$%i
(%einvolve-var e var1
))))
3695 (defun %einvolve
(e)
3696 (when (among '$%e e
) (%einvolve01 e
)))
3698 (defun %einvolve01
(e)
3699 (cond ((atom e
) nil
)
3703 (among var
(caddr e
)))
3705 (t (some #'%einvolve
(cdr e
)))))
3707 ;; Just like %EINVOLVE, except the dependency on VAR is explicit
3708 ;; (second arg here). Use this instead when possible.
3709 (defun %einvolve-var
(e var1
)
3710 (flet ((%einvolve01-var
(e)
3711 ;; A copy of %ENVOLVE01, but for %EINVOLVE-VAR.
3718 (among var1
(caddr e
)))
3720 (t (some #'(lambda (ee)
3721 (%einvolve-var ee var1
))
3723 (when (among '$%e e
)
3724 (%einvolve01-var e
))))
3727 (declare-top (unspecial *indicator exp var val origval taylored
3728 $tlimswitch logcombed lhp? lhcount
))
3734 ;; "On Computing Limits in a Symbolic Manipulation System"
3735 ;; PhD Dissertation ETH Zurich 1996
3737 ;; The algorithm identifies the most rapidly varying (MRV) subexpression,
3738 ;; replaces it with a new variable w, rewrites the expression in terms
3739 ;; of the new variable, and then repeats.
3741 ;; The algorithm doesn't handle oscillating functions, so it can't do things like
3742 ;; limit(sin(x)/x, x, inf).
3744 ;; To handle limits involving functions like gamma(x) and erf(x), the
3745 ;; gruntz algorithm requires them to be written in terms of asymptotic
3746 ;; expansions, which maxima cannot currently do.
3748 ;; The algorithm assumes that everything is real, so it can't
3749 ;; currently handle limit((-2)^x, x, inf).
3751 ;; This is one of the methods used by maxima's $limit.
3752 ;; It is also directly available to the user as $gruntz.
3755 ;; most rapidly varying subexpression of expression exp with respect to limit variable var.
3756 ;; returns a list of subexpressions which are in the same MRV equivalence class.
3757 (defun mrv (exp var
)
3758 (cond ((freeof var exp
)
3763 (mrv-max (mrv (cadr exp
) var
)
3764 (mrv (m*l
(cddr exp
)) var
)
3767 (mrv-max (mrv (cadr exp
) var
)
3768 (mrv (m+l
(cddr exp
)) var
)
3771 (cond ((freeof var
(caddr exp
))
3772 (mrv (cadr exp
) var
))
3773 ((member (limitinf (logred exp
) var
) '($inf $minf
) :test
#'eq
)
3774 (mrv-max (list exp
) (mrv (caddr exp
) var
) var
))
3775 (t (mrv-max (mrv (cadr exp
) var
) (mrv (caddr exp
) var
) var
))))
3777 (mrv (cadr exp
) var
))
3778 ((equal (length (cdr exp
)) 1)
3779 (mrv (cadr exp
) var
))
3780 ((equal (length (cdr exp
)) 2)
3781 (mrv-max (mrv (cadr exp
) var
)
3782 (mrv (caddr exp
) var
)
3784 (t (tay-error "mrv not implemented" exp
))))
3786 ;; takes two lists of expressions, f and g, and limit variable var.
3787 ;; members in each list are assumed to be in same MRV equivalence
3788 ;; class. returns MRV set of the union of the inputs - either f or g
3789 ;; or the union of f and g.
3790 (defun mrv-max (f g var
)
3797 (return (union f g
))))
3798 (let ((c (mrv-compare (car f
) (car g
) var
)))
3804 (return (union f g
)))
3805 (t (merror "MRV-MAX: expected '>' '<' or '='; found: ~M" c
))))))
3807 (defun mrv-compare (a b var
)
3808 (let ((c (limitinf (m// `((%log
) ,a
) `((%log
) ,b
)) var
)))
3811 ((member c
'($inf $minf
) :test
#'eq
)
3815 ;; rewrite expression exp by replacing members of MRV set omega with
3816 ;; expressions in terms of new variable wsym. return cons pair of new
3817 ;; version of exp and the log of the new variable wsym.
3818 (defun mrv-rewrite (exp omega var wsym
)
3819 (setq omega
(stable-sort omega
(lambda (x y
) (> (length (mrv x var
))
3820 (length (mrv y var
))))));FIXME consider a total order function with #'sort
3821 (let* ((g (car (last omega
)))
3823 (sig (equal (mrv-sign logg var
) 1))
3824 (w (if sig
(m// 1 wsym
) wsym
))
3825 (logw (if sig
(m* -
1 logg
) logg
)))
3826 (mapcar (lambda (x y
)
3827 ;;(mtell "y:~M x:~M exp:~M~%" y x exp)
3828 (setq exp
(syntactic-substitute y x exp
)))
3830 (mapcar (lambda (f) ;; rewrite each element of omega
3831 (let* ((logf (logred f
))
3832 (c (mrv-leadterm (m// logf logg
) var nil
)))
3833 (cond ((not (equal (cadr c
) 0))
3834 (merror "MRV-REWRITE: expected leading term to be constant in ~M" c
)))
3835 ;;(mtell "logg: ~M logf: ~M~%" logg logf)
3838 (m* (car c
) logg
))))))
3842 ;;; if log w(x) = h(x), rewrite all subexpressions of the form
3843 ;;; log(f(x)) as log(w^-c f(x)) + c h(x) with c the unique constant
3844 ;;; such that w^-c f(x) is strictly less rapidly varying than w.
3845 (defun mrv-rewrite-logs (exp wsym logw
)
3846 (cond ((atom exp
) exp
)
3848 (not (freeof wsym exp
)))
3849 (let* ((f (cadr exp
))
3850 (c ($lopow
(calculate-series f wsym
)
3853 (m* (m^ wsym
(m- c
))
3854 (mrv-rewrite-logs f wsym logw
)))
3859 (mrv-rewrite-logs e wsym logw
))
3862 ;; returns list of two elements: coeff and exponent of leading term of exp,
3863 ;; after rewriting exp in term of its MRV set omega.
3864 (defun mrv-leadterm (exp var omega
)
3865 (prog ((new-omega ()))
3866 (cond ((freeof var exp
)
3867 (return (list exp
0))))
3868 (dolist (term omega
)
3869 (cond ((subexp exp term
)
3870 (push term new-omega
))))
3871 (setq omega new-omega
)
3873 (setq omega
(mrv exp var
))))
3874 (cond ((member var omega
:test
#'eq
)
3875 (let* ((omega-up (mrv-moveup omega var
))
3876 (e-up (car (mrv-moveup (list exp
) var
)))
3877 (mrv-leadterm-up (mrv-leadterm e-up var omega-up
)))
3878 (return (mrv-movedown mrv-leadterm-up var
)))))
3879 (destructuring-let* ((wsym (gensym "w"))
3882 ((f . logw
) (mrv-rewrite exp omega var wsym
))
3883 (series (calculate-series (mrv-rewrite-logs f wsym logw
)
3885 (setq series
(maxima-substitute logw
`((%log
) ,wsym
) series
))
3886 (setq lo
($lopow series wsym
))
3887 (when (or (not ($constantp lo
))
3888 (not (free series
'%derivative
)))
3889 ;; (mtell "series: ~M lo: ~M~%" series lo)
3890 (tay-error "error in series expansion" f
))
3891 (setq coef
($coeff series wsym lo
))
3892 (when (not (free coef wsym
))
3893 (tay-error "MRV-LEADTERM: failed to extract leading coefficient; obtained" coef
))
3894 ;;(mtell "exp: ~M f: ~M~%" exp f)
3895 ;;(mtell "series: ~M~%coeff: ~M~%pow: ~M~%" series coef lo)
3896 (return (list coef lo
)))))
3898 (defun mrv-moveup (l var
)
3899 (mapcar (lambda (exp)
3900 (simplify-log-of-exp
3901 (syntactic-substitute `((mexpt) $%e
,var
) var exp
)))
3904 (defun mrv-movedown (l var
)
3905 (mapcar (lambda (exp) (syntactic-substitute `((%log simp
) ,var
) var exp
))
3908 ;; test whether sub is a subexpression of exp
3909 (defun subexp (exp sub
)
3910 (let ((dummy (gensym)))
3911 (putprop dummy t
'internal
)
3912 (not (alike1 (maxima-substitute dummy
3917 ;; Generate $lhospitallim terms of taylor expansion.
3918 ;; Ideally we would use a lazy series representation that generates
3919 ;; more terms as higher order terms cancel.
3920 (defun calculate-series (exp var
)
3921 (let ((cntx ($supcontext
)) ($taylor_simplifier
#'extra-simp
))
3925 (mfuncall '$assume
(ftake 'mgreaterp var
0))
3926 (putprop var t
'internal
); keep var from appearing in questions to user
3927 ($taylor exp var
0 $lhospitallim
))
3928 (remprop var
'internal
)
3929 ($killcontext cntx
))))
3931 (defun mrv-sign (exp var
)
3932 (cond ((freeof var exp
)
3933 (let ((sign ($sign
($radcan exp
))))
3934 (cond ((eq sign
'$zero
)
3940 (t (tay-error " cannot determine mrv-sign" exp
)))))
3944 (* (mrv-sign (cadr exp
) var
)
3945 (mrv-sign (m*l
(cddr exp
)) var
)))
3947 (equal (mrv-sign (cadr exp
) var
) 1))
3950 (cond ((equal (mrv-sign (cadr exp
) var
) -
1)
3951 (tay-error " complex expression in gruntz limit" exp
)))
3952 (mrv-sign (m+ -
1 (cadr exp
)) var
))
3954 (mrv-sign (limitinf exp var
) var
))
3955 (t (tay-error " cannot determine mrv-sign" exp
))))
3957 ;; gruntz algorithm for limit of exp as var goes to positive infinity
3958 (defun limitinf (exp var
)
3959 (prog (($exptsubst nil
))
3960 (cond ((freeof var exp
)
3962 (destructuring-let* ((c0-e0 (mrv-leadterm exp var nil
))
3965 (sig (mrv-sign e0 var
)))
3966 (cond ((equal sig
1)
3969 (cond ((equal (mrv-sign c0 var
) 1)
3971 ((equal (mrv-sign c0 var
) -
1)
3975 ;; example: gruntz(n^n/(n^n+(n-1)^n), n, inf);
3976 (tay-error " infinite recursion in limitinf" exp
))
3977 (return (limitinf c0 var
)))))))
3979 ;; user-level function equivalent to $limit.
3980 ;; direction must be specified if limit point is not infinite
3981 ;; The arguments are checked and a failure of taylor is catched.
3983 (defmfun $gruntz
(expr var val
&rest rest
)
3985 (when (> (length rest
) 1)
3987 (intl:gettext
"gruntz: too many arguments; expected just 3 or 4")))
3988 (setq dir
(car rest
))
3989 (when (and (not (member val
'($inf $minf $zeroa $zerob
)))
3990 (not (member dir
'($plus $minus
))))
3992 (intl:gettext
"gruntz: direction must be 'plus' or 'minus'")))
3994 (catch 'taylor-catch
3995 (let ((silent-taylor-flag t
))
3996 (gruntz1 expr var val dir
))))
3997 (if (or (null ans
) (eq ans t
))
3999 `(($gruntz simp
) ,expr
,var
, val
,dir
)
4000 `(($gruntz simp
) ,expr
,var
,val
))
4003 ;; Additional simplifications for the limit function. Specifically:
4004 ;; (a) replace every mapatom that is declared to be zero by zero
4005 ;; (b) dispatch radcan on expressions of the form (positive integer)^XXX
4006 ;; (c) log(negative number) --> log(-negative number) + %i*%pi
4007 ;; (d) apply cos(X)^2 + sin(X)^2 --> 1, when X depends on var
4008 ;; (e) convert, gamma functions, binomial coefficients, and beta functions to
4010 ;; (f) do some reciprocial function transformations; for example
4011 ;; csc(X) --> 1/sin(X)
4012 ;; (g) do transformations similar to acsc(X) --> asin(1/X).
4013 ;; (h) convert fibonacci functions to power form.
4014 ;; The mechanism (a) isn't perfect--if a+b is declared to zero, it doesn't
4015 ;; simplify a+b+c to c, for example.
4017 ;; This should be moved to the jacobi function code. And likely, we should
4018 ;; set the reciprocal property for the other jacobi functions.
4019 (mapcar #'(lambda (q) (setf (get (car q
) 'recip
) (cdr q
)))
4020 '((%jacobi_nc . %jacobi_cn
)
4021 (%jacobi_ns . %jacobi_sn
)
4022 (%jacobi_cs . %jacobi_sc
)
4023 (%jacobi_ds . %jacobi_sd
)
4024 (%jacobi_dc . %jacobi_cd
)))
4026 (defun extra-simp (e)
4027 (declare (special var
))
4028 (let ((var-present (not (freeof var e
))))
4029 (cond ((extended-real-p e
) e
) ;we don't want to call sign on ind, so catch this
4030 (($mapatom e
) ;if e is declared zero, return 0; otherwise e
4031 (if (eq '$zero
($csign e
)) 0 e
))
4032 ;; dispatch radcan on (positive integer)^Y
4033 ((and (mexptp e
) (integerp (cadr e
)) (> (cadr e
) 0))
4034 ($radcan
(ftake 'mexpt
(cadr e
) (extra-simp (caddr e
)))))
4035 ;; log(negative number) --> log(-negative number) + %i*%pi. This is
4036 ;; needed for a nice result for integrate(x^3/(exp(x)-1),x,0,inf), for
4038 ((and (eq '%log
(caar e
)) ($numberp
(cadr e
)) (eq t
(mgrp 0 (cadr e
))))
4039 (add (ftake '%log
(mul -
1 (cadr e
))) (mul '$%i
'$%pi
)))
4040 ;; When e isn't freeof var and e is a sum, map extra-simp over the
4041 ;; summands, add the results, and apply sin-sq-cos-sq-sub.
4042 ((and var-present
(mplusp e
))
4043 (sin-sq-cos-sq-sub (fapply 'mplus
(mapcar #'extra-simp
(cdr e
))) var
))
4044 ;; Convert gamma functions to factorials. Eventually, we should convert
4045 ;; factorials to gamma functions, I think (BW).
4046 ((and var-present
(eq '%gamma
(caar e
)))
4047 (ftake 'mfactorial
(extra-simp (sub (cadr e
) 1))))
4048 ;; Exponentialize the hyperbolic functions. It might be nicer to not do
4049 ;; this, but without this we get an error for limit(diff(log(tan(%pi/2*tanh(x))),x),x,inf).
4050 ((and var-present
(member (caar e
) (list '%sinh
'%cosh
'%tanh
'%sech
'%csch
'%coth
)))
4051 (extra-simp ($exponentialize e
)))
4052 ;; When X depends on var, apply reciprocal function identities such as
4053 ;; csc(X) --> 1/sin(X). Specifially, do this for operators '%sec, '%csc,
4054 ;; '%cot, '%jacobi_nc, '%jacobi_ns, %jacobi_cs, %jacobi_ds, and %jacobi_dc.
4055 ;; Since the hyperbolics are exponentialized, we don't do this for the
4057 ((and var-present
(member (caar e
)
4058 (list '%sec
'%csc
'%cot
'%jacobi_nc
'%jacobi_ns
'%jacobi_cs
'%jacobi_ds
'%jacobi_dc
)))
4059 (div 1 (fapply (get (caar e
) 'recip
) (mapcar #'extra-simp
(cdr e
)))))
4060 ;; When X or Y depends on var, convert binomial(X,Y) to factorial form.
4061 ;; Same for beta(x,y). Again, I think it would be better to convert to
4062 ;; gamma function form.
4063 ((and var-present
(member (caar e
) (list '%binomial
'%beta
)))
4064 (extra-simp ($makefact e
)))
4065 ;; When X depends on var, do acsc(X) --> asin(1/X). Do the same
4066 ;; for asec, acot, acsch, asech, and acoth.
4067 ((and var-present
(member (caar e
) '(%acsc %asec %acot %acsch %asech %acoth
)))
4068 (ftake (get (get (get (caar e
) '$inverse
) 'recip
) '$inverse
)
4069 (div 1 (extra-simp (cadr e
)))))
4070 ;; When X depends on var, convert fib(X) to its power form.
4071 ((and var-present
(eq '$fib
(caar e
)))
4072 (extra-simp ($fibtophi e
)))
4073 (($subvarp
(mop e
)) ;subscripted function
4076 (mapcar #'extra-simp
(subfunsubs e
))
4077 (mapcar #'extra-simp
(subfunargs e
))))
4078 (t (fapply (caar e
) (mapcar #'extra-simp
(cdr e
)))))))
4080 ;; Call extra-simp followed by a call to resimplify. This is analogus to
4082 (defun resimp-extra-simp (e) (resimplify (extra-simp e
)))
4084 ;; This function is for internal use in $limit.
4086 ;; The function gruntz1 standardizes the limit point to inf and the limit variable
4087 ;; to a gensym. Since the limit point is possibly altered by this function, we
4088 ;; need to make the appropriate assumptions on the limit variable. This is done
4090 (defun gruntz1 (exp var val
&rest rest
)
4091 (cond ((> (length rest
) 1)
4092 (merror (intl:gettext
"gruntz: too many arguments; expected just 3 or 4"))))
4093 (let (($logexpand t
) ; gruntz needs $logexpand T
4094 (newvar (gensym "w"))
4096 (putprop newvar t
'internal
); keep var from appearing in questions to user
4097 (cond ((eq val
'$inf
)
4098 (setq exp
(maxima-substitute newvar var exp
)))
4100 (setq exp
(maxima-substitute (m* -
1 newvar
) var exp
)))
4102 (setq exp
(maxima-substitute (m// 1 newvar
) var exp
)))
4104 (setq exp
(maxima-substitute (m// -
1 newvar
) var exp
)))
4106 (setq exp
(maxima-substitute (m+ val
(m// 1 newvar
)) var exp
)))
4108 (setq exp
(maxima-substitute (m+ val
(m// -
1 newvar
)) var exp
)))
4109 (t (merror (intl:gettext
"gruntz: direction must be 'plus' or 'minus'; found: ~M") dir
)))
4110 (let ((cx ($supcontext
)))
4113 (mfuncall '$assume
(ftake 'mlessp
*large-positive-number
* newvar
)) ; *large-positive-number* < newvar
4114 (mfuncall '$assume
(ftake 'mlessp
0 'lim-epsilon
)) ; 0 < lim-epsilon
4115 (mfuncall '$assume
(ftake 'mlessp
*large-positive-number
* 'prin-inf
)) ; *large-positive-number* < prin-inf
4116 (mfuncall '$activate cx
) ;not sure this is needed, but OK
4117 (setq exp
(resimplify exp
)) ;simplify in new context
4118 (setq exp
(resimp-extra-simp (sratsimp exp
))) ;additional simplifications
4119 (limitinf exp newvar
)) ;compute & return limit
4120 ($killcontext cx
))))) ;kill context & forget all new facts.
4122 ;; substitute y for x in exp
4123 ;; similar to maxima-substitute but does not simplify result
4124 (defun syntactic-substitute (y x exp
)
4125 (cond ((alike1 x exp
) y
)
4128 (mapcar (lambda (exp)
4129 (syntactic-substitute y x exp
))
4132 ;; log(exp(subexpr)) -> subexpr
4133 ;; without simplifying entire exp
4134 (defun simplify-log-of-exp (exp)
4135 (cond ((atom exp
) exp
)
4138 (eq '$%e
(cadadr exp
)))
4141 (mapcar #'simplify-log-of-exp