Commit f7adda54 authored by Jay Belanger's avatar Jay Belanger
Browse files

(math-integrate-by-parts): Removed unused variable var-thing.

(math-integ-depth, math-integ-level, math-integral-limit)
(math-enable-subst, math-any-substs, math-integ-msg)
(math-prev-parts-v, math-good-parts, math-max-integral-limit)
(math-int-threshold, math-int-factors, math-double-roots)
(math-solve-simplifying, var-IntegLimit, math-solve-sign)
(var-GenCount):  Declared these variables.
(calcFunc-integ):  Don't check if var-IntegLimit is bound.

(math-integral-cache, math-integral-cache-state):  Move declarations
to earlier in the file.

(math-deriv-var, math-deriv-total, math-deriv-symb):  New variables.
(math-derivative, calcFunc-deriv, calcFunc-tderiv):  Replace
variables deriv-var, deriv-total and deriv-symb by declared variables
math-deriv-var, math-deriv-total and math-deriv-symb.

(math-cur-record):  New variable.
(math-integral, math-replace-integral-parts, math-integrate-by-parts)
(calc-dump-integral-cache, math-try-integral):  Replace variable
cur-record by declared variable math-cur-record.

(math-has-rules):  New variable.
(math-try-integral, math-do-integral):  Use declared variable
math-has-rules instead of has-rules.

(math-t1, math-t2, math-t3):  New variables.
(math-do-integral, math-do-integral-methods, math-try-solve-for)
(math-try-solve-prod, math-solve-poly-funny-powers)
(math-solve-crunch-poly, math-decompose-poly)
(math-solve-find-root-term, math-find-root-in-prod):  Replace
variables t1, t2, t3 by declared variables math-t1, math-t2,
math-t3.

(math-so-far, math-integ-expr):  New variables.
(math-do-integral-methods, math-integ-try-linear-substitutions)
(math-integ-try-substitutions):  Replace variables so-far and expr by
declared variables math-so-far and math-integ-expr.

(math-expr-parts):  New variable.
(math-expr-rational-in, math-expr-rational-in-rec):  Replace variable
parts by declared variable math-expr-parts.

(calc-low, calc-high):  New variables.
(calcFunc-table, math-scan-for-limits):  Replaced variable low and
high with the declared variable calc-low and calc-high.

(math-solve-var, math-solve-full):  New variables.
(math-try-solve-for, math-try-solve-prod, math-solve-prod)
(math-decompose-poly, math-solve-quartic, math-poly-all-roots)
(math-solve-find-root-in-prod, math-solve-for, math-solve-system)
(math-solve-system-rec, math-solve-get-sign, math-solve-get-int):
Replace variables solve-var and solve-full with declared variables
math-solve-var and math-solve-full.

(math-solve-vars):  New variable.
(math-solve-system, math-solve-system-rec):  Replace variable
solve-vars with declared variable math-solve-vars.

(math-try-solve-sign):  New variable.
(math-try-solve-for, math-try-solve-prod):  Replace variable
sign by declared variable math-try-solve-sign.

(math-solve-b):  New variable.
(math-solve-poly-funny-powers, math-decompose-poly):  Replace variable
b by declared variable math-solve-b.

(math-solve-system-vv, math-solve-res):  New variables
(math-solve-system-rec, math-solve-system-subst):  Replaced variables
vv and res with declared variables math-solve-system-vv and
math-solve-system-res.
parent 853e2e5e
......@@ -201,13 +201,19 @@
(prefix-numeric-value nterms))))))
(defun math-derivative (expr) ; uses global values: deriv-var, deriv-total.
(cond ((equal expr deriv-var)
;; The following are global variables used by math-derivative and some
;; related functions
(defvar math-deriv-var)
(defvar math-deriv-total)
(defvar math-deriv-symb)
(defun math-derivative (expr)
(cond ((equal expr math-deriv-var)
1)
((or (Math-scalarp expr)
(eq (car expr) 'sdev)
(and (eq (car expr) 'var)
(or (not deriv-total)
(or (not math-deriv-total)
(math-const-var expr)
(progn
(math-setup-declarations)
......@@ -279,20 +285,20 @@
(let ((handler (get (car expr) 'math-derivative-n)))
(and handler
(funcall handler expr)))))
(and (not (eq deriv-symb 'pre-expand))
(and (not (eq math-deriv-symb 'pre-expand))
(let ((exp (math-expand-formula expr)))
(and exp
(or (let ((deriv-symb 'pre-expand))
(or (let ((math-deriv-symb 'pre-expand))
(catch 'math-deriv (math-derivative expr)))
(math-derivative exp)))))
(if (or (Math-objvecp expr)
(eq (car expr) 'var)
(not (symbolp (car expr))))
(if deriv-symb
(if math-deriv-symb
(throw 'math-deriv nil)
(list (if deriv-total 'calcFunc-tderiv 'calcFunc-deriv)
(list (if math-deriv-total 'calcFunc-tderiv 'calcFunc-deriv)
expr
deriv-var))
math-deriv-var))
(let ((accum 0)
(arg expr)
(n 1)
......@@ -322,7 +328,7 @@
(let ((handler (get func prop)))
(or (and prop handler
(apply handler (cdr expr)))
(if (and deriv-symb
(if (and math-deriv-symb
(not (get func
'calc-user-defn)))
(throw 'math-deriv nil)
......@@ -330,27 +336,27 @@
(setq n (1+ n)))
accum))))))
(defun calcFunc-deriv (expr deriv-var &optional deriv-value deriv-symb)
(let* ((deriv-total nil)
(defun calcFunc-deriv (expr math-deriv-var &optional deriv-value math-deriv-symb)
(let* ((math-deriv-total nil)
(res (catch 'math-deriv (math-derivative expr))))
(or (eq (car-safe res) 'calcFunc-deriv)
(null res)
(setq res (math-normalize res)))
(and res
(if deriv-value
(math-expr-subst res deriv-var deriv-value)
(math-expr-subst res math-deriv-var deriv-value)
res))))
(defun calcFunc-tderiv (expr deriv-var &optional deriv-value deriv-symb)
(defun calcFunc-tderiv (expr math-deriv-var &optional deriv-value math-deriv-symb)
(math-setup-declarations)
(let* ((deriv-total t)
(let* ((math-deriv-total t)
(res (catch 'math-deriv (math-derivative expr))))
(or (eq (car-safe res) 'calcFunc-tderiv)
(null res)
(setq res (math-normalize res)))
(and res
(if deriv-value
(math-expr-subst res deriv-var deriv-value)
(math-expr-subst res math-deriv-var deriv-value)
res))))
(put 'calcFunc-inv\' 'math-derivative-1
......@@ -540,7 +546,7 @@
(put 'calcFunc-sum 'math-derivative-n
(function
(lambda (expr)
(if (math-expr-contains (cons 'vec (cdr (cdr expr))) deriv-var)
(if (math-expr-contains (cons 'vec (cdr (cdr expr))) math-deriv-var)
(throw 'math-deriv nil)
(cons 'calcFunc-sum
(cons (math-derivative (nth 1 expr))
......@@ -549,7 +555,7 @@
(put 'calcFunc-prod 'math-derivative-n
(function
(lambda (expr)
(if (math-expr-contains (cons 'vec (cdr (cdr expr))) deriv-var)
(if (math-expr-contains (cons 'vec (cdr (cdr expr))) math-deriv-var)
(throw 'math-deriv nil)
(math-mul expr
(cons 'calcFunc-sum
......@@ -561,7 +567,7 @@
(function
(lambda (expr)
(if (= (length expr) 3)
(if (equal (nth 2 expr) deriv-var)
(if (equal (nth 2 expr) math-deriv-var)
(nth 1 expr)
(math-normalize
(list 'calcFunc-integ
......@@ -576,7 +582,7 @@
(math-derivative (nth 4 expr)))
(math-mul lower
(math-derivative (nth 3 expr))))
(if (equal (nth 2 expr) deriv-var)
(if (equal (nth 2 expr) math-deriv-var)
0
(math-normalize
(list 'calcFunc-integ
......@@ -605,6 +611,21 @@
(defvar math-integ-var-list (list math-integ-var))
(defvar math-integ-var-list-list (list math-integ-var-list))
;; math-integ-depth is a local variable for math-try-integral, but is used
;; by math-integral and math-tracing-integral
;; which are called (directly or indirectly) by math-try-integral.
(defvar math-integ-depth)
;; math-integ-level is a local variable for math-try-integral, but is used
;; by math-integral, math-do-integral, math-tracing-integral,
;; math-sub-integration, math-integrate-by-parts and
;; math-integrate-by-substitution, which are called (directly or
;; indirectly) by math-try-integral.
(defvar math-integ-level)
;; math-integral-limit is a local variable for calcFunc-integ, but is
;; used by math-tracing-integral, math-sub-integration and
;; math-try-integration.
(defvar math-integral-limit)
(defmacro math-tracing-integral (&rest parts)
(list 'and
'trace-buffer
......@@ -629,28 +650,46 @@
;;; ( A parts ) Currently working, integ-by-parts;
;;; ( A parts2 ) Currently working, integ-by-parts;
;;; ( A cancelled ) Ignore this cache entry;
;;; ( A [B] ) Same result as for cur-record = B.
;;; ( A [B] ) Same result as for math-cur-record = B.
;; math-cur-record is a local variable for math-try-integral, but is used
;; by math-integral, math-replace-integral-parts and math-integrate-by-parts
;; which are called (directly or indirectly) by math-try-integral, as well as
;; by calc-dump-integral-cache
(defvar math-cur-record)
;; math-enable-subst and math-any-substs are local variables for
;; calcFunc-integ, but are used by math-integral and math-try-integral.
(defvar math-enable-subst)
(defvar math-any-substs)
;; math-integ-msg is a local variable for math-try-integral, but is
;; used (both locally and non-locally) by math-integral.
(defvar math-integ-msg)
(defvar math-integral-cache nil)
(defvar math-integral-cache-state nil)
(defun math-integral (expr &optional simplify same-as-above)
(let* ((simp cur-record)
(cur-record (assoc expr math-integral-cache))
(let* ((simp math-cur-record)
(math-cur-record (assoc expr math-integral-cache))
(math-integ-depth (1+ math-integ-depth))
(val 'cancelled))
(math-tracing-integral "Integrating "
(math-format-value expr 1000)
"...\n")
(and cur-record
(and math-cur-record
(progn
(math-tracing-integral "Found "
(math-format-value (nth 1 cur-record) 1000))
(and (consp (nth 1 cur-record))
(math-replace-integral-parts cur-record))
(math-format-value (nth 1 math-cur-record) 1000))
(and (consp (nth 1 math-cur-record))
(math-replace-integral-parts math-cur-record))
(math-tracing-integral " => "
(math-format-value (nth 1 cur-record) 1000)
(math-format-value (nth 1 math-cur-record) 1000)
"\n")))
(or (and cur-record
(not (eq (nth 1 cur-record) 'cancelled))
(or (not (integerp (nth 1 cur-record)))
(>= (nth 1 cur-record) math-integ-level)))
(or (and math-cur-record
(not (eq (nth 1 math-cur-record) 'cancelled))
(or (not (integerp (nth 1 math-cur-record)))
(>= (nth 1 math-cur-record) math-integ-level)))
(and (math-integral-contains-parts expr)
(progn
(setq val nil)
......@@ -665,12 +704,12 @@
"Working... Integrating %s"
(math-format-flat-expr expr 0)))
(message math-integ-msg)))
(if cur-record
(setcar (cdr cur-record)
(if math-cur-record
(setcar (cdr math-cur-record)
(if same-as-above (vector simp) 'busy))
(setq cur-record
(setq math-cur-record
(list expr (if same-as-above (vector simp) 'busy))
math-integral-cache (cons cur-record
math-integral-cache (cons math-cur-record
math-integral-cache)))
(if (eq simplify 'yes)
(progn
......@@ -692,12 +731,12 @@
(setq val (math-integral simp 'no t))))))))
(if (eq calc-display-working-message 'lots)
(message math-integ-msg)))
(setcar (cdr cur-record) (or val
(setcar (cdr math-cur-record) (or val
(if (or math-enable-subst
(not math-any-substs))
math-integ-level
'cancelled)))))
(setq val cur-record)
(setq val math-cur-record)
(while (vectorp (nth 1 val))
(setq val (aref (nth 1 val) 0)))
(setq val (if (memq (nth 1 val) '(parts parts2))
......@@ -712,8 +751,6 @@
(math-format-value val 1000)
"\n")
val))
(defvar math-integral-cache nil)
(defvar math-integral-cache-state nil)
(defun math-integral-contains-parts (expr)
(if (Math-primp expr)
......@@ -735,37 +772,58 @@
(progn
(setcar expr (nth 1 (nth 2 (car expr))))
(math-replace-integral-parts (cons 'foo expr)))
(setcar (cdr cur-record) 'cancelled)))
(setcar (cdr math-cur-record) 'cancelled)))
(math-replace-integral-parts (car expr)))))))
(defvar math-linear-subst-tried t
"Non-nil means that a linear substitution has been tried.")
;; The variable math-has-rules is a local variable for math-try-integral,
;; but is used by math-do-integral, which is called (non-directly) by
;; math-try-integral.
(defvar math-has-rules)
;; math-old-integ is a local variable for math-do-integral, but is
;; used by math-sub-integration.
(defvar math-old-integ)
;; The variables math-t1, math-t2 and math-t3 are local to
;; math-do-integral, math-try-solve-for and math-decompose-poly, but
;; are used by functions they call (directly or indirectly);
;; math-do-integral calls math-do-integral-methods;
;; math-try-solve-for calls math-try-solve-prod,
;; math-solve-find-root-term and math-solve-find-root-in-prod;
;; math-decompose-poly calls math-solve-poly-funny-powers and
;; math-solve-crunch-poly.
(defvar math-t1)
(defvar math-t2)
(defvar math-t3)
(defun math-do-integral (expr)
(let ((math-linear-subst-tried nil)
t1 t2)
math-t1 math-t2)
(or (cond ((not (math-expr-contains expr math-integ-var))
(math-mul expr math-integ-var))
((equal expr math-integ-var)
(math-div (math-sqr expr) 2))
((eq (car expr) '+)
(and (setq t1 (math-integral (nth 1 expr)))
(setq t2 (math-integral (nth 2 expr)))
(math-add t1 t2)))
(and (setq math-t1 (math-integral (nth 1 expr)))
(setq math-t2 (math-integral (nth 2 expr)))
(math-add math-t1 math-t2)))
((eq (car expr) '-)
(and (setq t1 (math-integral (nth 1 expr)))
(setq t2 (math-integral (nth 2 expr)))
(math-sub t1 t2)))
(and (setq math-t1 (math-integral (nth 1 expr)))
(setq math-t2 (math-integral (nth 2 expr)))
(math-sub math-t1 math-t2)))
((eq (car expr) 'neg)
(and (setq t1 (math-integral (nth 1 expr)))
(math-neg t1)))
(and (setq math-t1 (math-integral (nth 1 expr)))
(math-neg math-t1)))
((eq (car expr) '*)
(cond ((not (math-expr-contains (nth 1 expr) math-integ-var))
(and (setq t1 (math-integral (nth 2 expr)))
(math-mul (nth 1 expr) t1)))
(and (setq math-t1 (math-integral (nth 2 expr)))
(math-mul (nth 1 expr) math-t1)))
((not (math-expr-contains (nth 2 expr) math-integ-var))
(and (setq t1 (math-integral (nth 1 expr)))
(math-mul t1 (nth 2 expr))))
(and (setq math-t1 (math-integral (nth 1 expr)))
(math-mul math-t1 (nth 2 expr))))
((memq (car-safe (nth 1 expr)) '(+ -))
(math-integral (list (car (nth 1 expr))
(math-mul (nth 1 (nth 1 expr))
......@@ -784,39 +842,39 @@
(cond ((and (not (math-expr-contains (nth 1 expr)
math-integ-var))
(not (math-equal-int (nth 1 expr) 1)))
(and (setq t1 (math-integral (math-div 1 (nth 2 expr))))
(math-mul (nth 1 expr) t1)))
(and (setq math-t1 (math-integral (math-div 1 (nth 2 expr))))
(math-mul (nth 1 expr) math-t1)))
((not (math-expr-contains (nth 2 expr) math-integ-var))
(and (setq t1 (math-integral (nth 1 expr)))
(math-div t1 (nth 2 expr))))
(and (setq math-t1 (math-integral (nth 1 expr)))
(math-div math-t1 (nth 2 expr))))
((and (eq (car-safe (nth 1 expr)) '*)
(not (math-expr-contains (nth 1 (nth 1 expr))
math-integ-var)))
(and (setq t1 (math-integral
(and (setq math-t1 (math-integral
(math-div (nth 2 (nth 1 expr))
(nth 2 expr))))
(math-mul t1 (nth 1 (nth 1 expr)))))
(math-mul math-t1 (nth 1 (nth 1 expr)))))
((and (eq (car-safe (nth 1 expr)) '*)
(not (math-expr-contains (nth 2 (nth 1 expr))
math-integ-var)))
(and (setq t1 (math-integral
(and (setq math-t1 (math-integral
(math-div (nth 1 (nth 1 expr))
(nth 2 expr))))
(math-mul t1 (nth 2 (nth 1 expr)))))
(math-mul math-t1 (nth 2 (nth 1 expr)))))
((and (eq (car-safe (nth 2 expr)) '*)
(not (math-expr-contains (nth 1 (nth 2 expr))
math-integ-var)))
(and (setq t1 (math-integral
(and (setq math-t1 (math-integral
(math-div (nth 1 expr)
(nth 2 (nth 2 expr)))))
(math-div t1 (nth 1 (nth 2 expr)))))
(math-div math-t1 (nth 1 (nth 2 expr)))))
((and (eq (car-safe (nth 2 expr)) '*)
(not (math-expr-contains (nth 2 (nth 2 expr))
math-integ-var)))
(and (setq t1 (math-integral
(and (setq math-t1 (math-integral
(math-div (nth 1 expr)
(nth 1 (nth 2 expr)))))
(math-div t1 (nth 2 (nth 2 expr)))))
(math-div math-t1 (nth 2 (nth 2 expr)))))
((eq (car-safe (nth 2 expr)) 'calcFunc-exp)
(math-integral
(math-mul (nth 1 expr)
......@@ -824,10 +882,10 @@
(math-neg (nth 1 (nth 2 expr)))))))))
((eq (car expr) '^)
(cond ((not (math-expr-contains (nth 1 expr) math-integ-var))
(or (and (setq t1 (math-is-polynomial (nth 2 expr)
(or (and (setq math-t1 (math-is-polynomial (nth 2 expr)
math-integ-var 1))
(math-div expr
(math-mul (nth 1 t1)
(math-mul (nth 1 math-t1)
(math-normalize
(list 'calcFunc-ln
(nth 1 expr))))))
......@@ -843,12 +901,12 @@
(math-integral
(list '/ 1 (math-pow (nth 1 expr) (- (nth 2 expr))))
nil t)
(or (and (setq t1 (math-is-polynomial (nth 1 expr)
(or (and (setq math-t1 (math-is-polynomial (nth 1 expr)
math-integ-var
1))
(setq t2 (math-add (nth 2 expr) 1))
(math-div (math-pow (nth 1 expr) t2)
(math-mul t2 (nth 1 t1))))
(setq math-t2 (math-add (nth 2 expr) 1))
(math-div (math-pow (nth 1 expr) math-t2)
(math-mul math-t2 (nth 1 math-t1))))
(and (Math-negp (nth 2 expr))
(math-integral
(math-div 1
......@@ -859,49 +917,49 @@
nil))))))
;; Integral of a polynomial.
(and (setq t1 (math-is-polynomial expr math-integ-var 20))
(and (setq math-t1 (math-is-polynomial expr math-integ-var 20))
(let ((accum 0)
(n 1))
(while t1
(while math-t1
(if (setq accum (math-add accum
(math-div (math-mul (car t1)
(math-div (math-mul (car math-t1)
(math-pow
math-integ-var
n))
n))
t1 (cdr t1))
math-t1 (cdr math-t1))
(setq n (1+ n))))
accum))
;; Try looking it up!
(cond ((= (length expr) 2)
(and (symbolp (car expr))
(setq t1 (get (car expr) 'math-integral))
(setq math-t1 (get (car expr) 'math-integral))
(progn
(while (and t1
(not (setq t2 (funcall (car t1)
(while (and math-t1
(not (setq math-t2 (funcall (car math-t1)
(nth 1 expr)))))
(setq t1 (cdr t1)))
(and t2 (math-normalize t2)))))
(setq math-t1 (cdr math-t1)))
(and math-t2 (math-normalize math-t2)))))
((= (length expr) 3)
(and (symbolp (car expr))
(setq t1 (get (car expr) 'math-integral-2))
(setq math-t1 (get (car expr) 'math-integral-2))
(progn
(while (and t1
(not (setq t2 (funcall (car t1)
(while (and math-t1
(not (setq math-t2 (funcall (car math-t1)
(nth 1 expr)
(nth 2 expr)))))
(setq t1 (cdr t1)))
(and t2 (math-normalize t2))))))
(setq math-t1 (cdr math-t1)))
(and math-t2 (math-normalize math-t2))))))
;; Integral of a rational function.
(and (math-ratpoly-p expr math-integ-var)
(setq t1 (calcFunc-apart expr math-integ-var))
(not (equal t1 expr))
(math-integral t1))
(setq math-t1 (calcFunc-apart expr math-integ-var))
(not (equal math-t1 expr))
(math-integral math-t1))
;; Try user-defined integration rules.
(and has-rules
(and math-has-rules
(let ((math-old-integ (symbol-function 'calcFunc-integ))
(input (list 'calcFunc-integtry expr math-integ-var))
res part)
......@@ -975,17 +1033,27 @@
res)))
(list 'calcFunc-integfailed expr)))
(defun math-do-integral-methods (expr)
(let ((so-far math-integ-var-list-list)
;; math-so-far is a local variable for math-do-integral-methods, but
;; is used by math-integ-try-linear-substitutions and
;; math-integ-try-substitutions.
(defvar math-so-far)
;; math-integ-expr is a local variable for math-do-integral-methods,
;; but is used by math-integ-try-linear-substitutions and
;; math-integ-try-substitutions.
(defvar math-integ-expr)
(defun math-do-integral-methods (math-integ-expr)
(let ((math-so-far math-integ-var-list-list)
rat-in)
;; Integration by substitution, for various likely sub-expressions.
;; (In first pass, we look only for sub-exprs that are linear in X.)
(or (math-integ-try-linear-substitutions expr)
(math-integ-try-substitutions expr)
(or (math-integ-try-linear-substitutions math-integ-expr)
(math-integ-try-substitutions math-integ-expr)
;; If function has sines and cosines, try tan(x/2) substitution.
(and (let ((p (setq rat-in (math-expr-rational-in expr))))
(and (let ((p (setq rat-in (math-expr-rational-in math-integ-expr))))
(while (and p
(memq (car (car p)) '(calcFunc-sin
calcFunc-cos
......@@ -993,10 +1061,10 @@
(equal (nth 1 (car p)) math-integ-var))
(setq p (cdr p)))
(null p))
(or (and (math-integ-parts-easy expr)
(math-integ-try-parts expr t))
(or (and (math-integ-parts-easy math-integ-expr)
(math-integ-try-parts math-integ-expr t))
(math-integrate-by-good-substitution
expr (list 'calcFunc-tan (math-div math-integ-var 2)))))
math-integ-expr (list 'calcFunc-tan (math-div math-integ-var 2)))))
;; If function has sinh and cosh, try tanh(x/2) substitution.
(and (let ((p rat-in))
......@@ -1008,55 +1076,55 @@
(equal (nth 1 (car p)) math-integ-var))
(setq p (cdr p)))
(null p))
(or (and (math-integ-parts-easy expr)
(math-integ-try-parts expr t))
(or (and (math-integ-parts-easy math-integ-expr)
(math-integ-try-parts math-integ-expr t))
(math-integrate-by-good-substitution
expr (list 'calcFunc-tanh (math-div math-integ-var 2)))))
math-integ-expr (list 'calcFunc-tanh (math-div math-integ-var 2)))))
;; If function has square roots, try sin, tan, or sec substitution.
(and (let ((p rat-in))
(setq t1 nil)
(setq math-t1 nil)
(while (and p
(or (equal (car p) math-integ-var)
(and (eq (car (car p)) 'calcFunc-sqrt)
(setq t1 (math-is-polynomial
(nth 1 (setq t2 (car p)))
(setq math-t1 (math-is-polynomial
(nth 1 (setq math-t2 (car p)))
math-integ-var 2)))))
(setq p (cdr p)))
(and (null p) t1))
(if (cdr (cdr t1))
(if (math-guess-if-neg (nth 2 t1))
(let* ((c (math-sqrt (math-neg (nth 2 t1))))
(d (math-div (nth 1 t1) (math-mul -2 c)))
(a (math-sqrt (math-add (car t1) (math-sqr d)))))
(and (null p) math-t1))
(if (cdr (cdr math-t1))
(if (math-guess-if-neg (nth 2 math-t1))
(let* ((c (math-sqrt (math-neg (nth 2 math-t1))))
(d (math-div (nth 1 math-t1) (math-mul -2 c)))
(a (math-sqrt (math-add (car math-t1) (math-sqr d)))))
(math-integrate-by-good-substitution
expr (list 'calcFunc-arcsin
math-integ-expr (list 'calcFunc-arcsin
(math-div-thru
(math-add (math-mul c math-integ-var) d)
a))))
(let* ((c (math-sqrt (nth 2 t1)))
(d (math-div (nth 1 t1) (math-mul 2 c)))
(aa (math-sub (car t1) (math-sqr d))))
(let* ((c (math-sqrt (nth 2 math-t1)))
(d (math-div (nth 1 math-t1) (math-mul 2 c)))
(aa (math-sub (car math-t1) (math-sqr d))))
(if (and nil (not (and (eq d 0) (eq c 1))))
(math-integrate-by-good-substitution
expr (math-add (math-mul c math-integ-var) d))
math-integ-expr (math-add (math-mul c math-integ-var) d))
(if (math-guess-if-neg aa)
(math-integrate-by-good-substitution
expr (list 'calcFunc-arccosh
math-integ-expr (list 'calcFunc-arccosh
(math-div-thru
(math-add (math-mul c math-integ-var)
d)
(math-sqrt (math-neg aa)))))
(math-integrate-by-good-substitution
expr (list 'calcFunc-arcsinh
math-integ-expr (list 'calcFunc-arcsinh
(math-div-thru
(math-add (math-mul c math-integ-var)
d)
(math-sqrt aa))))))))
(math-integrate-by-good-substitution expr t2)) )
(math-integrate-by-good-substitution math-integ-expr math-t2)) )
;; Try integration by parts.
(math-integ-try-parts expr)
(math-integ-try-parts math-integ-expr)
;; Give up.
nil)))
......@@ -1076,6 +1144,15 @@
(math-integ-parts-easy (nth 1 expr)))
(t t)))
;; math-prev-parts-v is local to calcFunc-integ (as well as
;; math-integrate-by-parts), but is used by math-integ-try-parts.
(defvar math-prev-parts-v)
;; math-good-parts is local to calcFunc-integ (as well as
;; math-integ-try-parts), but is used by math-integrate-by-parts.
(defvar math-good-parts)
(defun math-integ-try-parts (expr &optional math-good-parts)
;; Integration by parts:
;; integ(f(x) g(x),x) = f(x) h(x) - integ(h(x) f'(x),x)
......@@ -1112,7 +1189,7 @@
(and (>= math-integ-level 0)
(unwind-protect
(progn
(setcar (cdr cur-record) 'parts)
(setcar (cdr math-cur-record) 'parts)
(math-tracing-integral "Integrating by parts, u = "
(math-format-value u 1000)
", v' = "
......@@ -1123,15 +1200,14 @@
(setq temp (let ((math-prev-parts-v v))
(math-integral (math-mul v temp) 'yes)))
(setq temp (math-sub (math-mul u v) temp))
(if (eq (nth 1 cur-record) 'parts)
(if (eq (nth 1 math-cur-record) 'parts)
(calcFunc-expand temp)
(setq v (list 'var 'PARTS cur-record)
var-thing (list 'vec (math-sub v temp) v)
(setq v (list 'var 'PARTS math-cur-record)
temp (let (calc-next-why)
(math-solve-for (math-sub v temp) 0 v nil)))
(and temp (not (integerp temp))
(math-simplify-extended temp)))))
(setcar (cdr cur-record) 'busy)))))
(setcar (cdr math-cur-record) 'busy)))))
;;; This tries two different formulations, hoping the algebraic simplifier
;;; will be strong enough to handle at least one.
......@@ -1202,13 +1278,13 @@
(while (and (setq sub-expr (cdr sub-expr))
(or (not (math-linear-in (car sub-expr)
math-integ-var))
(assoc (car sub-expr) so-far)
(assoc (car sub-expr) math-so-far)
(progn
(setq so-far (cons (list (car sub-expr))
so-far))
(setq math-so-far (cons (list (car sub-expr))
math-so-far))
(not (setq res
(math-integrate-by-substitution
expr (car sub-expr))))))))
math-integ-expr (car sub-expr))))))))
res))
(let ((res nil))
(while (and (setq sub-expr (cdr sub-expr))
......@@ -1219,15 +1295,15 @@
;;; Recursively try different substitutions based on various sub-expressions.
(defun math-integ-try-substitutions (sub-expr &optional allow-rat)
(and (not (Math-primp sub-expr))
(not (assoc sub-expr so-far))
(not (assoc sub-expr math-so-far))
(math-expr-contains sub-expr math-integ-var)
(or (and (if (and (not (memq (car sub-expr) '(+ - * / neg)))
(not (and (eq (car sub-expr) '^)
(integerp (nth 2 sub-expr)))))
(setq allow-rat t)
(prog1 allow-rat (setq allow-rat nil)))
(not (eq sub-expr expr))
(or (math-integrate-by-substitution expr sub-expr)
(not (eq sub-expr math-integ-expr))
(or (math-integrate-by-substitution math-integ-expr sub-expr)
(and (eq (car sub-expr) '^)
(integerp (nth 2 sub-expr))
(< (nth 2 sub-expr) 0)
......@@ -1235,22 +1311,25 @@
(math-pow (nth 1 sub-expr) (- (nth 2 sub-expr)))
t))))