More documentation and comment out debugging print.
[maxima.git] / src / tlimit.lisp
blob797559041654a6582dd6fe0e11576ac0e6c3b0a4
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1980 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (declare-top (special taylored))
15 (macsyma-module tlimit)
17 (load-macsyma-macros rzmac)
19 ;; TOP LEVEL FUNCTION(S): $TLIMIT $TLDEFINT
21 (defmfun $tlimit (&rest args)
22 (let ((limit-using-taylor t))
23 (declare (special limit-using-taylor))
24 (apply #'$limit args)))
26 (defmfun $tldefint (exp var ll ul)
27 (let ((limit-using-taylor t))
28 (declare (special limit-using-taylor))
29 ($ldefint exp var ll ul)))
31 ;; Taylor cannot handle conjugate, ceiling, floor, unit_step, or signum
32 ;; expressions, so let's tell tlimit to *not* try. We also disallow
33 ;; expressions containing $ind.
34 (defun tlimp (e x)
35 (or (and ($mapatom e) (not (eq e '$ind)) (not (eq e '$und)))
36 (and (consp e)
37 (consp (car e))
38 (or
39 (known-ps (caar e))
40 (and (eq (caar e) 'mqapply) (known-ps (subfunname e)))
41 (member (caar e) (list 'mplus 'mtimes 'mexpt '%log))
42 (get (caar e) 'grad)
43 ($freeof x e))
44 (every #'(lambda (q) (tlimp q x)) (cdr e)))))
46 ;; Dispatch Taylor, but recurse on the order until either the recursion
47 ;; depth is 15 or the Taylor polynomial is nonzero. When Taylor
48 ;; fails to find a nonzero Taylor polynomial or the recursion depth is
49 ;; too great, return nil.
51 ;; This recursion on the order attempts to handle limits such as
52 ;; tlimit(2^n/n^5, n, inf) correctly.
54 ;; We set up a reasonable environment for calling taylor. Arguably, setting
55 ;; these option variables is overly removes the users ability to adjust these
56 ;; option variables. When $taylor_logexpand is true, taylor does some
57 ;; principal branch violating transformations, so we set it to nil.
59 ;; I know of no compelling reason for defaulting the taylor order to
60 ;; lhospitallim, but this is documented in the user documentation).
62 (defun tlimit-taylor (e x pt n &optional (d 0))
63 (let ((ee 0)
64 (silent-taylor-flag t)
65 ($taylordepth 8)
66 ($taylor_logexpand nil)
67 ($taylor_simplifier #'sratsimp))
68 (setq ee (ratdisrep (catch 'taylor-catch ($taylor e x pt n))))
69 (cond ((and ee (not (alike1 ee 0))) ee)
70 ;; When taylor returns zero and the depth d is less than 16,
71 ;; declare a do-over; otherwise return nil.
72 ((and ee (< d 16))
73 (tlimit-taylor e x pt (* 4 (max 1 n)) (+ d 1)))
74 (t nil))))
76 ;; Previously when the taylor series failed, there was code for deciding
77 ;; whether to call limit1 or simplimit. The choice depended on the last
78 ;; argument to taylim (previously named *i*) and the main operator of the
79 ;; expression. This code dispenses with this logic and dispatches limit1
80 ;; when Maxima is unable to find the taylor polynomial. This change orphans
81 ;; the last argument of taylim.
82 (defun taylim (e var val flag)
83 (declare (ignore flag))
84 (let ((et nil))
85 (when (tlimp e var)
86 (setq e (stirling0 e))
87 (setq et (tlimit-taylor e var (ridofab val) $lhospitallim 0)))
88 (if et (let ((taylored t)) (limit et var val 'think)) (limit1 e var val))))