1 ;;; calcalg2.el --- more algebraic functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
34 (defun calc-Need-calc-alg-2 () nil)
37 (defun calc-derivative (var num)
38 (interactive "sDifferentiate with respect to: \np")
41 (error "Order of derivative must be positive"))
42 (let ((func (if (calc-is-hyperbolic) 'calcFunc-tderiv 'calcFunc-deriv))
44 (if (or (equal var "") (equal var "$"))
48 (setq var (math-read-expr var))
49 (when (eq (car-safe var) 'error)
50 (error "Bad format in expression: %s" (nth 1 var)))
53 (while (>= (setq num (1- num)) 0)
54 (setq expr (list func expr var)))
55 (calc-enter-result n "derv" expr))))
57 (defun calc-integral (var)
58 (interactive "sIntegration variable: ")
60 (if (or (equal var "") (equal var "$"))
61 (calc-enter-result 2 "intg" (list 'calcFunc-integ
64 (let ((var (math-read-expr var)))
65 (if (eq (car-safe var) 'error)
66 (error "Bad format in expression: %s" (nth 1 var)))
67 (calc-enter-result 1 "intg" (list 'calcFunc-integ
71 (defun calc-num-integral (&optional varname lowname highname)
72 (interactive "sIntegration variable: ")
73 (calc-tabular-command 'calcFunc-ninteg "Integration" "nint"
74 nil varname lowname highname))
76 (defun calc-summation (arg &optional varname lowname highname)
77 (interactive "P\nsSummation variable: ")
78 (calc-tabular-command 'calcFunc-sum "Summation" "sum"
79 arg varname lowname highname))
81 (defun calc-alt-summation (arg &optional varname lowname highname)
82 (interactive "P\nsSummation variable: ")
83 (calc-tabular-command 'calcFunc-asum "Summation" "asum"
84 arg varname lowname highname))
86 (defun calc-product (arg &optional varname lowname highname)
87 (interactive "P\nsIndex variable: ")
88 (calc-tabular-command 'calcFunc-prod "Index" "prod"
89 arg varname lowname highname))
91 (defun calc-tabulate (arg &optional varname lowname highname)
92 (interactive "P\nsIndex variable: ")
93 (calc-tabular-command 'calcFunc-table "Index" "tabl"
94 arg varname lowname highname))
96 (defun calc-tabular-command (func prompt prefix arg varname lowname highname)
98 (let (var (low nil) (high nil) (step nil) stepname stepnum (num 1) expr)
102 (if (or (equal varname "") (equal varname "$") (null varname))
103 (setq high (calc-top-n (+ stepnum 1))
104 low (calc-top-n (+ stepnum 2))
105 var (calc-top-n (+ stepnum 3))
107 (setq var (if (stringp varname) (math-read-expr varname) varname))
108 (if (eq (car-safe var) 'error)
109 (error "Bad format in expression: %s" (nth 1 var)))
111 (setq lowname (read-string (concat prompt " variable: " varname
113 (if (or (equal lowname "") (equal lowname "$"))
114 (setq high (calc-top-n (+ stepnum 1))
115 low (calc-top-n (+ stepnum 2))
117 (setq low (if (stringp lowname) (math-read-expr lowname) lowname))
118 (if (eq (car-safe low) 'error)
119 (error "Bad format in expression: %s" (nth 1 low)))
121 (setq highname (read-string (concat prompt " variable: " varname
124 (if (or (equal highname "") (equal highname "$"))
125 (setq high (calc-top-n (+ stepnum 1))
127 (setq high (if (stringp highname) (math-read-expr highname)
129 (if (eq (car-safe high) 'error)
130 (error "Bad format in expression: %s" (nth 1 high)))
133 (setq stepname (read-string (concat prompt " variable: "
138 (if (or (equal stepname "") (equal stepname "$"))
139 (setq step (calc-top-n 1)
141 (setq step (math-read-expr stepname))
142 (if (eq (car-safe step) 'error)
143 (error "Bad format in expression: %s"
147 (setq step (calc-top-n 1))
149 (setq step (prefix-numeric-value arg)))))
150 (setq expr (calc-top-n num))
151 (calc-enter-result num prefix (append (list func expr var low high)
152 (and step (list step)))))))
154 (defun calc-solve-for (var)
155 (interactive "sVariable to solve for: ")
157 (let ((func (if (calc-is-inverse)
158 (if (calc-is-hyperbolic) 'calcFunc-ffinv 'calcFunc-finv)
159 (if (calc-is-hyperbolic) 'calcFunc-fsolve 'calcFunc-solve))))
160 (if (or (equal var "") (equal var "$"))
161 (calc-enter-result 2 "solv" (list func
164 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
165 (not (string-match "\\[" var)))
166 (math-read-expr (concat "[" var "]"))
167 (math-read-expr var))))
168 (if (eq (car-safe var) 'error)
169 (error "Bad format in expression: %s" (nth 1 var)))
170 (calc-enter-result 1 "solv" (list func
174 (defun calc-poly-roots (var)
175 (interactive "sVariable to solve for: ")
177 (if (or (equal var "") (equal var "$"))
178 (calc-enter-result 2 "prts" (list 'calcFunc-roots
181 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
182 (not (string-match "\\[" var)))
183 (math-read-expr (concat "[" var "]"))
184 (math-read-expr var))))
185 (if (eq (car-safe var) 'error)
186 (error "Bad format in expression: %s" (nth 1 var)))
187 (calc-enter-result 1 "prts" (list 'calcFunc-roots
191 (defun calc-taylor (var nterms)
192 (interactive "sTaylor expansion variable: \nNNumber of terms: ")
194 (let ((var (math-read-expr var)))
195 (if (eq (car-safe var) 'error)
196 (error "Bad format in expression: %s" (nth 1 var)))
197 (calc-enter-result 1 "tylr" (list 'calcFunc-taylor
200 (prefix-numeric-value nterms))))))
203 ;; The following are global variables used by math-derivative and some
205 (defvar math-deriv-var)
206 (defvar math-deriv-total)
207 (defvar math-deriv-symb)
209 (defun math-derivative (expr)
210 (cond ((equal expr math-deriv-var)
212 ((or (Math-scalarp expr)
213 (eq (car expr) 'sdev)
214 (and (eq (car expr) 'var)
215 (or (not math-deriv-total)
216 (math-const-var expr)
218 (math-setup-declarations)
219 (memq 'const (nth 1 (or (assq (nth 2 expr)
221 math-decls-all)))))))
224 (math-add (math-derivative (nth 1 expr))
225 (math-derivative (nth 2 expr))))
227 (math-sub (math-derivative (nth 1 expr))
228 (math-derivative (nth 2 expr))))
229 ((memq (car expr) '(calcFunc-eq calcFunc-neq calcFunc-lt
230 calcFunc-gt calcFunc-leq calcFunc-geq))
232 (math-derivative (nth 1 expr))
233 (math-derivative (nth 2 expr))))
234 ((eq (car expr) 'neg)
235 (math-neg (math-derivative (nth 1 expr))))
237 (math-add (math-mul (nth 2 expr)
238 (math-derivative (nth 1 expr)))
239 (math-mul (nth 1 expr)
240 (math-derivative (nth 2 expr)))))
242 (math-sub (math-div (math-derivative (nth 1 expr))
244 (math-div (math-mul (nth 1 expr)
245 (math-derivative (nth 2 expr)))
246 (math-sqr (nth 2 expr)))))
248 (let ((du (math-derivative (nth 1 expr)))
249 (dv (math-derivative (nth 2 expr))))
251 (setq du (math-mul (nth 2 expr)
252 (math-mul (math-normalize
255 (math-add (nth 2 expr) -1)))
258 (setq dv (math-mul (math-normalize
259 (list 'calcFunc-ln (nth 1 expr)))
260 (math-mul expr dv))))
263 (math-derivative (nth 1 expr))) ; a reasonable definition
264 ((eq (car expr) 'vec)
265 (math-map-vec 'math-derivative expr))
266 ((and (memq (car expr) '(calcFunc-conj calcFunc-re calcFunc-im))
268 (list (car expr) (math-derivative (nth 1 expr))))
269 ((and (memq (car expr) '(calcFunc-subscr calcFunc-mrow calcFunc-mcol))
271 (let ((d (math-derivative (nth 1 expr))))
273 0 ; assume x and x_1 are independent vars
274 (list (car expr) d (nth 2 expr)))))
275 (t (or (and (symbolp (car expr))
276 (if (= (length expr) 2)
277 (let ((handler (get (car expr) 'math-derivative)))
279 (let ((deriv (math-derivative (nth 1 expr))))
280 (if (Math-zerop deriv)
282 (math-mul (funcall handler (nth 1 expr))
284 (let ((handler (get (car expr) 'math-derivative-n)))
286 (funcall handler expr)))))
287 (and (not (eq math-deriv-symb 'pre-expand))
288 (let ((exp (math-expand-formula expr)))
290 (or (let ((math-deriv-symb 'pre-expand))
291 (catch 'math-deriv (math-derivative expr)))
292 (math-derivative exp)))))
293 (if (or (Math-objvecp expr)
295 (not (symbolp (car expr))))
297 (throw 'math-deriv nil)
298 (list (if math-deriv-total 'calcFunc-tderiv 'calcFunc-deriv)
305 (while (setq arg (cdr arg))
306 (or (Math-zerop (setq derv (math-derivative (car arg))))
307 (let ((func (intern (concat (symbol-name (car expr))
312 (prop (cond ((= (length expr) 2)
321 'math-derivative-5))))
327 (let ((handler (get func prop)))
328 (or (and prop handler
329 (apply handler (cdr expr)))
330 (if (and math-deriv-symb
333 (throw 'math-deriv nil)
334 (cons func (cdr expr))))))))))
338 (defun calcFunc-deriv (expr math-deriv-var &optional deriv-value math-deriv-symb)
339 (let* ((math-deriv-total nil)
340 (res (catch 'math-deriv (math-derivative expr))))
341 (or (eq (car-safe res) 'calcFunc-deriv)
343 (setq res (math-normalize res)))
346 (math-expr-subst res math-deriv-var deriv-value)
349 (defun calcFunc-tderiv (expr math-deriv-var &optional deriv-value math-deriv-symb)
350 (math-setup-declarations)
351 (let* ((math-deriv-total t)
352 (res (catch 'math-deriv (math-derivative expr))))
353 (or (eq (car-safe res) 'calcFunc-tderiv)
355 (setq res (math-normalize res)))
358 (math-expr-subst res math-deriv-var deriv-value)
361 (put 'calcFunc-inv\' 'math-derivative-1
362 (function (lambda (u) (math-neg (math-div 1 (math-sqr u))))))
364 (put 'calcFunc-sqrt\' 'math-derivative-1
365 (function (lambda (u) (math-div 1 (math-mul 2 (list 'calcFunc-sqrt u))))))
367 (put 'calcFunc-deg\' 'math-derivative-1
368 (function (lambda (u) (math-div-float '(float 18 1) (math-pi)))))
370 (put 'calcFunc-rad\' 'math-derivative-1
371 (function (lambda (u) (math-pi-over-180))))
373 (put 'calcFunc-ln\' 'math-derivative-1
374 (function (lambda (u) (math-div 1 u))))
376 (put 'calcFunc-log10\' 'math-derivative-1
377 (function (lambda (u)
378 (math-div (math-div 1 (math-normalize '(calcFunc-ln 10)))
381 (put 'calcFunc-lnp1\' 'math-derivative-1
382 (function (lambda (u) (math-div 1 (math-add u 1)))))
384 (put 'calcFunc-log\' 'math-derivative-2
385 (function (lambda (x b)
386 (and (not (Math-zerop b))
387 (let ((lnv (math-normalize
388 (list 'calcFunc-ln b))))
389 (math-div 1 (math-mul lnv x)))))))
391 (put 'calcFunc-log\'2 'math-derivative-2
392 (function (lambda (x b)
393 (let ((lnv (list 'calcFunc-ln b)))
394 (math-neg (math-div (list 'calcFunc-log x b)
395 (math-mul lnv b)))))))
397 (put 'calcFunc-exp\' 'math-derivative-1
398 (function (lambda (u) (math-normalize (list 'calcFunc-exp u)))))
400 (put 'calcFunc-expm1\' 'math-derivative-1
401 (function (lambda (u) (math-normalize (list 'calcFunc-expm1 u)))))
403 (put 'calcFunc-sin\' 'math-derivative-1
404 (function (lambda (u) (math-to-radians-2 (math-normalize
405 (list 'calcFunc-cos u))))))
407 (put 'calcFunc-cos\' 'math-derivative-1
408 (function (lambda (u) (math-neg (math-to-radians-2
410 (list 'calcFunc-sin u)))))))
412 (put 'calcFunc-tan\' 'math-derivative-1
413 (function (lambda (u) (math-to-radians-2
414 (math-div 1 (math-sqr
416 (list 'calcFunc-cos u))))))))
418 (put 'calcFunc-arcsin\' 'math-derivative-1
419 (function (lambda (u)
421 (math-div 1 (math-normalize
423 (math-sub 1 (math-sqr u)))))))))
425 (put 'calcFunc-arccos\' 'math-derivative-1
426 (function (lambda (u)
428 (math-div -1 (math-normalize
430 (math-sub 1 (math-sqr u)))))))))
432 (put 'calcFunc-arctan\' 'math-derivative-1
433 (function (lambda (u) (math-from-radians-2
434 (math-div 1 (math-add 1 (math-sqr u)))))))
436 (put 'calcFunc-sinh\' 'math-derivative-1
437 (function (lambda (u) (math-normalize (list 'calcFunc-cosh u)))))
439 (put 'calcFunc-cosh\' 'math-derivative-1
440 (function (lambda (u) (math-normalize (list 'calcFunc-sinh u)))))
442 (put 'calcFunc-tanh\' 'math-derivative-1
443 (function (lambda (u) (math-div 1 (math-sqr
445 (list 'calcFunc-cosh u)))))))
447 (put 'calcFunc-arcsinh\' 'math-derivative-1
448 (function (lambda (u)
449 (math-div 1 (math-normalize
451 (math-add (math-sqr u) 1)))))))
453 (put 'calcFunc-arccosh\' 'math-derivative-1
454 (function (lambda (u)
455 (math-div 1 (math-normalize
457 (math-add (math-sqr u) -1)))))))
459 (put 'calcFunc-arctanh\' 'math-derivative-1
460 (function (lambda (u) (math-div 1 (math-sub 1 (math-sqr u))))))
462 (put 'calcFunc-bern\'2 'math-derivative-2
463 (function (lambda (n x)
464 (math-mul n (list 'calcFunc-bern (math-add n -1) x)))))
466 (put 'calcFunc-euler\'2 'math-derivative-2
467 (function (lambda (n x)
468 (math-mul n (list 'calcFunc-euler (math-add n -1) x)))))
470 (put 'calcFunc-gammag\'2 'math-derivative-2
471 (function (lambda (a x) (math-deriv-gamma a x 1))))
473 (put 'calcFunc-gammaG\'2 'math-derivative-2
474 (function (lambda (a x) (math-deriv-gamma a x -1))))
476 (put 'calcFunc-gammaP\'2 'math-derivative-2
477 (function (lambda (a x) (math-deriv-gamma a x
480 (list 'calcFunc-gamma
483 (put 'calcFunc-gammaQ\'2 'math-derivative-2
484 (function (lambda (a x) (math-deriv-gamma a x
487 (list 'calcFunc-gamma
490 (defun math-deriv-gamma (a x scale)
492 (math-mul (math-pow x (math-add a -1))
493 (list 'calcFunc-exp (math-neg x)))))
495 (put 'calcFunc-betaB\' 'math-derivative-3
496 (function (lambda (x a b) (math-deriv-beta x a b 1))))
498 (put 'calcFunc-betaI\' 'math-derivative-3
499 (function (lambda (x a b) (math-deriv-beta x a b
501 1 (list 'calcFunc-beta
504 (defun math-deriv-beta (x a b scale)
505 (math-mul (math-mul (math-pow x (math-add a -1))
506 (math-pow (math-sub 1 x) (math-add b -1)))
509 (put 'calcFunc-erf\' 'math-derivative-1
510 (function (lambda (x) (math-div 2
511 (math-mul (list 'calcFunc-exp
513 (if calc-symbolic-mode
518 (put 'calcFunc-erfc\' 'math-derivative-1
519 (function (lambda (x) (math-div -2
520 (math-mul (list 'calcFunc-exp
522 (if calc-symbolic-mode
527 (put 'calcFunc-besJ\'2 'math-derivative-2
528 (function (lambda (v z) (math-div (math-sub (list 'calcFunc-besJ
536 (put 'calcFunc-besY\'2 'math-derivative-2
537 (function (lambda (v z) (math-div (math-sub (list 'calcFunc-besY
545 (put 'calcFunc-sum 'math-derivative-n
548 (if (math-expr-contains (cons 'vec (cdr (cdr expr))) math-deriv-var)
549 (throw 'math-deriv nil)
551 (cons (math-derivative (nth 1 expr))
552 (cdr (cdr expr))))))))
554 (put 'calcFunc-prod 'math-derivative-n
557 (if (math-expr-contains (cons 'vec (cdr (cdr expr))) math-deriv-var)
558 (throw 'math-deriv nil)
561 (cons (math-div (math-derivative (nth 1 expr))
563 (cdr (cdr expr)))))))))
565 (put 'calcFunc-integ 'math-derivative-n
568 (if (= (length expr) 3)
569 (if (equal (nth 2 expr) math-deriv-var)
572 (list 'calcFunc-integ
573 (math-derivative (nth 1 expr))
575 (if (= (length expr) 5)
576 (let ((lower (math-expr-subst (nth 1 expr) (nth 2 expr)
578 (upper (math-expr-subst (nth 1 expr) (nth 2 expr)
580 (math-add (math-sub (math-mul upper
581 (math-derivative (nth 4 expr)))
583 (math-derivative (nth 3 expr))))
584 (if (equal (nth 2 expr) math-deriv-var)
587 (list 'calcFunc-integ
588 (math-derivative (nth 1 expr)) (nth 2 expr)
589 (nth 3 expr) (nth 4 expr)))))))))))
591 (put 'calcFunc-if 'math-derivative-n
594 (and (= (length expr) 4)
595 (list 'calcFunc-if (nth 1 expr)
596 (math-derivative (nth 2 expr))
597 (math-derivative (nth 3 expr)))))))
599 (put 'calcFunc-subscr 'math-derivative-n
602 (and (= (length expr) 3)
603 (list 'calcFunc-subscr (nth 1 expr)
604 (math-derivative (nth 2 expr)))))))
607 (defvar math-integ-var '(var X ---))
608 (defvar math-integ-var-2 '(var Y ---))
609 (defvar math-integ-vars (list 'f math-integ-var math-integ-var-2))
610 (defvar math-integ-var-list (list math-integ-var))
611 (defvar math-integ-var-list-list (list math-integ-var-list))
613 ;; math-integ-depth is a local variable for math-try-integral, but is used
614 ;; by math-integral and math-tracing-integral
615 ;; which are called (directly or indirectly) by math-try-integral.
616 (defvar math-integ-depth)
617 ;; math-integ-level is a local variable for math-try-integral, but is used
618 ;; by math-integral, math-do-integral, math-tracing-integral,
619 ;; math-sub-integration, math-integrate-by-parts and
620 ;; math-integrate-by-substitution, which are called (directly or
621 ;; indirectly) by math-try-integral.
622 (defvar math-integ-level)
623 ;; math-integral-limit is a local variable for calcFunc-integ, but is
624 ;; used by math-tracing-integral, math-sub-integration and
625 ;; math-try-integration.
626 (defvar math-integral-limit)
628 (defmacro math-tracing-integral (&rest parts)
631 (list 'save-excursion
632 '(set-buffer trace-buffer)
633 '(goto-char (point-max))
636 '(insert (make-string (- math-integral-limit
637 math-integ-level) 32)
638 (format "%2d " math-integ-depth)
639 (make-string math-integ-level 32)))
640 ;;(list 'condition-case 'err
642 ;; '(error (insert (prin1-to-string err))))
645 ;;; The following wrapper caches results and avoids infinite recursion.
646 ;;; Each cache entry is: ( A B ) Integral of A is B;
647 ;;; ( A N ) Integral of A failed at level N;
648 ;;; ( A busy ) Currently working on integral of A;
649 ;;; ( A parts ) Currently working, integ-by-parts;
650 ;;; ( A parts2 ) Currently working, integ-by-parts;
651 ;;; ( A cancelled ) Ignore this cache entry;
652 ;;; ( A [B] ) Same result as for math-cur-record = B.
654 ;; math-cur-record is a local variable for math-try-integral, but is used
655 ;; by math-integral, math-replace-integral-parts and math-integrate-by-parts
656 ;; which are called (directly or indirectly) by math-try-integral, as well as
657 ;; by calc-dump-integral-cache
658 (defvar math-cur-record)
659 ;; math-enable-subst and math-any-substs are local variables for
660 ;; calcFunc-integ, but are used by math-integral and math-try-integral.
661 (defvar math-enable-subst)
662 (defvar math-any-substs)
664 ;; math-integ-msg is a local variable for math-try-integral, but is
665 ;; used (both locally and non-locally) by math-integral.
666 (defvar math-integ-msg)
668 (defvar math-integral-cache nil)
669 (defvar math-integral-cache-state nil)
671 (defun math-integral (expr &optional simplify same-as-above)
672 (let* ((simp math-cur-record)
673 (math-cur-record (assoc expr math-integral-cache))
674 (math-integ-depth (1+ math-integ-depth))
676 (math-tracing-integral "Integrating "
677 (math-format-value expr 1000)
681 (math-tracing-integral "Found "
682 (math-format-value (nth 1 math-cur-record) 1000))
683 (and (consp (nth 1 math-cur-record))
684 (math-replace-integral-parts math-cur-record))
685 (math-tracing-integral " => "
686 (math-format-value (nth 1 math-cur-record) 1000)
688 (or (and math-cur-record
689 (not (eq (nth 1 math-cur-record) 'cancelled))
690 (or (not (integerp (nth 1 math-cur-record)))
691 (>= (nth 1 math-cur-record) math-integ-level)))
692 (and (math-integral-contains-parts expr)
698 (let (math-integ-msg)
699 (if (eq calc-display-working-message 'lots)
701 (calc-set-command-flag 'clear-message)
702 (setq math-integ-msg (format
703 "Working... Integrating %s"
704 (math-format-flat-expr expr 0)))
705 (message math-integ-msg)))
707 (setcar (cdr math-cur-record)
708 (if same-as-above (vector simp) 'busy))
709 (setq math-cur-record
710 (list expr (if same-as-above (vector simp) 'busy))
711 math-integral-cache (cons math-cur-record
712 math-integral-cache)))
713 (if (eq simplify 'yes)
715 (math-tracing-integral "Simplifying...")
716 (setq simp (math-simplify expr))
717 (setq val (if (equal simp expr)
719 (math-tracing-integral " no change\n")
720 (math-do-integral expr))
721 (math-tracing-integral " simplified\n")
722 (math-integral simp 'no t))))
723 (or (setq val (math-do-integral expr))
725 (let ((simp (math-simplify expr)))
726 (or (equal simp expr)
728 (math-tracing-integral "Trying again after "
729 "simplification...\n")
730 (setq val (math-integral simp 'no t))))))))
731 (if (eq calc-display-working-message 'lots)
732 (message math-integ-msg)))
733 (setcar (cdr math-cur-record) (or val
734 (if (or math-enable-subst
735 (not math-any-substs))
738 (setq val math-cur-record)
739 (while (vectorp (nth 1 val))
740 (setq val (aref (nth 1 val) 0)))
741 (setq val (if (memq (nth 1 val) '(parts parts2))
743 (setcar (cdr val) 'parts2)
744 (list 'var 'PARTS val))
745 (and (consp (nth 1 val))
747 (math-tracing-integral "Integral of "
748 (math-format-value expr 1000)
750 (math-format-value val 1000)
754 (defun math-integral-contains-parts (expr)
755 (if (Math-primp expr)
756 (and (eq (car-safe expr) 'var)
757 (eq (nth 1 expr) 'PARTS)
758 (listp (nth 2 expr)))
759 (while (and (setq expr (cdr expr))
760 (not (math-integral-contains-parts (car expr)))))
763 (defun math-replace-integral-parts (expr)
764 (or (Math-primp expr)
765 (while (setq expr (cdr expr))
766 (and (consp (car expr))
767 (if (eq (car (car expr)) 'var)
768 (and (eq (nth 1 (car expr)) 'PARTS)
769 (consp (nth 2 (car expr)))
770 (if (listp (nth 1 (nth 2 (car expr))))
772 (setcar expr (nth 1 (nth 2 (car expr))))
773 (math-replace-integral-parts (cons 'foo expr)))
774 (setcar (cdr math-cur-record) 'cancelled)))
775 (math-replace-integral-parts (car expr)))))))
777 (defvar math-linear-subst-tried t
778 "Non-nil means that a linear substitution has been tried.")
780 ;; The variable math-has-rules is a local variable for math-try-integral,
781 ;; but is used by math-do-integral, which is called (non-directly) by
782 ;; math-try-integral.
783 (defvar math-has-rules)
785 ;; math-old-integ is a local variable for math-do-integral, but is
786 ;; used by math-sub-integration.
787 (defvar math-old-integ)
789 ;; The variables math-t1, math-t2 and math-t3 are local to
790 ;; math-do-integral, math-try-solve-for and math-decompose-poly, but
791 ;; are used by functions they call (directly or indirectly);
792 ;; math-do-integral calls math-do-integral-methods;
793 ;; math-try-solve-for calls math-try-solve-prod,
794 ;; math-solve-find-root-term and math-solve-find-root-in-prod;
795 ;; math-decompose-poly calls math-solve-poly-funny-powers and
796 ;; math-solve-crunch-poly.
801 (defun math-do-integral (expr)
802 (let ((math-linear-subst-tried nil)
804 (or (cond ((not (math-expr-contains expr math-integ-var))
805 (math-mul expr math-integ-var))
806 ((equal expr math-integ-var)
807 (math-div (math-sqr expr) 2))
809 (and (setq math-t1 (math-integral (nth 1 expr)))
810 (setq math-t2 (math-integral (nth 2 expr)))
811 (math-add math-t1 math-t2)))
813 (and (setq math-t1 (math-integral (nth 1 expr)))
814 (setq math-t2 (math-integral (nth 2 expr)))
815 (math-sub math-t1 math-t2)))
816 ((eq (car expr) 'neg)
817 (and (setq math-t1 (math-integral (nth 1 expr)))
820 (cond ((not (math-expr-contains (nth 1 expr) math-integ-var))
821 (and (setq math-t1 (math-integral (nth 2 expr)))
822 (math-mul (nth 1 expr) math-t1)))
823 ((not (math-expr-contains (nth 2 expr) math-integ-var))
824 (and (setq math-t1 (math-integral (nth 1 expr)))
825 (math-mul math-t1 (nth 2 expr))))
826 ((memq (car-safe (nth 1 expr)) '(+ -))
827 (math-integral (list (car (nth 1 expr))
828 (math-mul (nth 1 (nth 1 expr))
830 (math-mul (nth 2 (nth 1 expr))
833 ((memq (car-safe (nth 2 expr)) '(+ -))
834 (math-integral (list (car (nth 2 expr))
835 (math-mul (nth 1 (nth 2 expr))
837 (math-mul (nth 2 (nth 2 expr))
841 (cond ((and (not (math-expr-contains (nth 1 expr)
843 (not (math-equal-int (nth 1 expr) 1)))
844 (and (setq math-t1 (math-integral (math-div 1 (nth 2 expr))))
845 (math-mul (nth 1 expr) math-t1)))
846 ((not (math-expr-contains (nth 2 expr) math-integ-var))
847 (and (setq math-t1 (math-integral (nth 1 expr)))
848 (math-div math-t1 (nth 2 expr))))
849 ((and (eq (car-safe (nth 1 expr)) '*)
850 (not (math-expr-contains (nth 1 (nth 1 expr))
852 (and (setq math-t1 (math-integral
853 (math-div (nth 2 (nth 1 expr))
855 (math-mul math-t1 (nth 1 (nth 1 expr)))))
856 ((and (eq (car-safe (nth 1 expr)) '*)
857 (not (math-expr-contains (nth 2 (nth 1 expr))
859 (and (setq math-t1 (math-integral
860 (math-div (nth 1 (nth 1 expr))
862 (math-mul math-t1 (nth 2 (nth 1 expr)))))
863 ((and (eq (car-safe (nth 2 expr)) '*)
864 (not (math-expr-contains (nth 1 (nth 2 expr))
866 (and (setq math-t1 (math-integral
867 (math-div (nth 1 expr)
868 (nth 2 (nth 2 expr)))))
869 (math-div math-t1 (nth 1 (nth 2 expr)))))
870 ((and (eq (car-safe (nth 2 expr)) '*)
871 (not (math-expr-contains (nth 2 (nth 2 expr))
873 (and (setq math-t1 (math-integral
874 (math-div (nth 1 expr)
875 (nth 1 (nth 2 expr)))))
876 (math-div math-t1 (nth 2 (nth 2 expr)))))
877 ((eq (car-safe (nth 2 expr)) 'calcFunc-exp)
879 (math-mul (nth 1 expr)
881 (math-neg (nth 1 (nth 2 expr)))))))))
883 (cond ((not (math-expr-contains (nth 1 expr) math-integ-var))
884 (or (and (setq math-t1 (math-is-polynomial (nth 2 expr)
887 (math-mul (nth 1 math-t1)
893 (math-mul (nth 2 expr)
898 ((not (math-expr-contains (nth 2 expr) math-integ-var))
899 (if (and (integerp (nth 2 expr)) (< (nth 2 expr) 0))
901 (list '/ 1 (math-pow (nth 1 expr) (- (nth 2 expr))))
903 (or (and (setq math-t1 (math-is-polynomial (nth 1 expr)
906 (setq math-t2 (math-add (nth 2 expr) 1))
907 (math-div (math-pow (nth 1 expr) math-t2)
908 (math-mul math-t2 (nth 1 math-t1))))
909 (and (Math-negp (nth 2 expr))
912 (math-pow (nth 1 expr)
918 ;; Integral of a polynomial.
919 (and (setq math-t1 (math-is-polynomial expr math-integ-var 20))
923 (if (setq accum (math-add accum
924 (math-div (math-mul (car math-t1)
929 math-t1 (cdr math-t1))
933 ;; Try looking it up!
934 (cond ((= (length expr) 2)
935 (and (symbolp (car expr))
936 (setq math-t1 (get (car expr) 'math-integral))
939 (not (setq math-t2 (funcall (car math-t1)
941 (setq math-t1 (cdr math-t1)))
942 (and math-t2 (math-normalize math-t2)))))
944 (and (symbolp (car expr))
945 (setq math-t1 (get (car expr) 'math-integral-2))
948 (not (setq math-t2 (funcall (car math-t1)
951 (setq math-t1 (cdr math-t1)))
952 (and math-t2 (math-normalize math-t2))))))
954 ;; Integral of a rational function.
955 (and (math-ratpoly-p expr math-integ-var)
956 (setq math-t1 (calcFunc-apart expr math-integ-var))
957 (not (equal math-t1 expr))
958 (math-integral math-t1))
960 ;; Try user-defined integration rules.
962 (let ((math-old-integ (symbol-function 'calcFunc-integ))
963 (input (list 'calcFunc-integtry expr math-integ-var))
967 (fset 'calcFunc-integ 'math-sub-integration)
968 (setq res (math-rewrite input
969 '(var IntegRules var-IntegRules)
971 (fset 'calcFunc-integ math-old-integ)
972 (and (not (equal res input))
973 (if (setq part (math-expr-calls
974 res '(calcFunc-integsubst)))
975 (and (memq (length part) '(3 4 5))
983 (math-integrate-by-substitution
986 (list 'calcFunc-integfailed
989 (if (not (math-expr-calls res
991 calcFunc-integfailed)))
993 (fset 'calcFunc-integ math-old-integ))))
995 ;; See if the function is a symbolic derivative.
996 (and (string-match "'" (symbol-name (car expr)))
997 (let ((name (symbol-name (car expr)))
998 (p expr) (n 0) (which nil) (bad nil))
999 (while (setq n (1+ n) p (cdr p))
1000 (if (equal (car p) math-integ-var)
1001 (if which (setq bad t) (setq which n))
1002 (if (math-expr-contains (car p) math-integ-var)
1004 (and which (not bad)
1005 (let ((prime (if (= which 1) "'" (format "'%d" which))))
1006 (and (string-match (concat prime "\\('['0-9]*\\|$\\)")
1010 (substring name 0 (match-beginning 0))
1011 (substring name (+ (match-beginning 0)
1015 ;; Try transformation methods (parts, substitutions).
1016 (and (> math-integ-level 0)
1017 (math-do-integral-methods expr))
1019 ;; Try expanding the function's definition.
1020 (let ((res (math-expand-formula expr)))
1022 (math-integral res))))))
1024 (defun math-sub-integration (expr &rest rest)
1025 (or (if (or (not rest)
1026 (and (< math-integ-level math-integral-limit)
1027 (eq (car rest) math-integ-var)))
1028 (math-integral expr)
1029 (let ((res (apply math-old-integ expr rest)))
1030 (and (or (= math-integ-level math-integral-limit)
1031 (not (math-expr-calls res 'calcFunc-integ)))
1033 (list 'calcFunc-integfailed expr)))
1035 ;; math-so-far is a local variable for math-do-integral-methods, but
1036 ;; is used by math-integ-try-linear-substitutions and
1037 ;; math-integ-try-substitutions.
1038 (defvar math-so-far)
1040 ;; math-integ-expr is a local variable for math-do-integral-methods,
1041 ;; but is used by math-integ-try-linear-substitutions and
1042 ;; math-integ-try-substitutions.
1043 (defvar math-integ-expr)
1045 (defun math-do-integral-methods (math-integ-expr)
1046 (let ((math-so-far math-integ-var-list-list)
1049 ;; Integration by substitution, for various likely sub-expressions.
1050 ;; (In first pass, we look only for sub-exprs that are linear in X.)
1051 (or (math-integ-try-linear-substitutions math-integ-expr)
1052 (math-integ-try-substitutions math-integ-expr)
1054 ;; If function has sines and cosines, try tan(x/2) substitution.
1055 (and (let ((p (setq rat-in (math-expr-rational-in math-integ-expr))))
1057 (memq (car (car p)) '(calcFunc-sin
1060 (equal (nth 1 (car p)) math-integ-var))
1063 (or (and (math-integ-parts-easy math-integ-expr)
1064 (math-integ-try-parts math-integ-expr t))
1065 (math-integrate-by-good-substitution
1066 math-integ-expr (list 'calcFunc-tan (math-div math-integ-var 2)))))
1068 ;; If function has sinh and cosh, try tanh(x/2) substitution.
1069 (and (let ((p rat-in))
1071 (memq (car (car p)) '(calcFunc-sinh
1075 (equal (nth 1 (car p)) math-integ-var))
1078 (or (and (math-integ-parts-easy math-integ-expr)
1079 (math-integ-try-parts math-integ-expr t))
1080 (math-integrate-by-good-substitution
1081 math-integ-expr (list 'calcFunc-tanh (math-div math-integ-var 2)))))
1083 ;; If function has square roots, try sin, tan, or sec substitution.
1084 (and (let ((p rat-in))
1087 (or (equal (car p) math-integ-var)
1088 (and (eq (car (car p)) 'calcFunc-sqrt)
1089 (setq math-t1 (math-is-polynomial
1090 (nth 1 (setq math-t2 (car p)))
1091 math-integ-var 2)))))
1093 (and (null p) math-t1))
1094 (if (cdr (cdr math-t1))
1095 (if (math-guess-if-neg (nth 2 math-t1))
1096 (let* ((c (math-sqrt (math-neg (nth 2 math-t1))))
1097 (d (math-div (nth 1 math-t1) (math-mul -2 c)))
1098 (a (math-sqrt (math-add (car math-t1) (math-sqr d)))))
1099 (math-integrate-by-good-substitution
1100 math-integ-expr (list 'calcFunc-arcsin
1102 (math-add (math-mul c math-integ-var) d)
1104 (let* ((c (math-sqrt (nth 2 math-t1)))
1105 (d (math-div (nth 1 math-t1) (math-mul 2 c)))
1106 (aa (math-sub (car math-t1) (math-sqr d))))
1107 (if (and nil (not (and (eq d 0) (eq c 1))))
1108 (math-integrate-by-good-substitution
1109 math-integ-expr (math-add (math-mul c math-integ-var) d))
1110 (if (math-guess-if-neg aa)
1111 (math-integrate-by-good-substitution
1112 math-integ-expr (list 'calcFunc-arccosh
1114 (math-add (math-mul c math-integ-var)
1116 (math-sqrt (math-neg aa)))))
1117 (math-integrate-by-good-substitution
1118 math-integ-expr (list 'calcFunc-arcsinh
1120 (math-add (math-mul c math-integ-var)
1122 (math-sqrt aa))))))))
1123 (math-integrate-by-good-substitution math-integ-expr math-t2)) )
1125 ;; Try integration by parts.
1126 (math-integ-try-parts math-integ-expr)
1131 (defun math-integ-parts-easy (expr)
1132 (cond ((Math-primp expr) t)
1133 ((memq (car expr) '(+ - *))
1134 (and (math-integ-parts-easy (nth 1 expr))
1135 (math-integ-parts-easy (nth 2 expr))))
1137 (and (math-integ-parts-easy (nth 1 expr))
1138 (math-atomic-factorp (nth 2 expr))))
1140 (and (natnump (nth 2 expr))
1141 (math-integ-parts-easy (nth 1 expr))))
1142 ((eq (car expr) 'neg)
1143 (math-integ-parts-easy (nth 1 expr)))
1146 ;; math-prev-parts-v is local to calcFunc-integ (as well as
1147 ;; math-integrate-by-parts), but is used by math-integ-try-parts.
1148 (defvar math-prev-parts-v)
1150 ;; math-good-parts is local to calcFunc-integ (as well as
1151 ;; math-integ-try-parts), but is used by math-integrate-by-parts.
1152 (defvar math-good-parts)
1155 (defun math-integ-try-parts (expr &optional math-good-parts)
1156 ;; Integration by parts:
1157 ;; integ(f(x) g(x),x) = f(x) h(x) - integ(h(x) f'(x),x)
1158 ;; where h(x) = integ(g(x),x).
1159 (or (let ((exp (calcFunc-expand expr)))
1160 (and (not (equal exp expr))
1161 (math-integral exp)))
1162 (and (eq (car expr) '*)
1163 (let ((first-bad (or (math-polynomial-p (nth 1 expr)
1165 (equal (nth 2 expr) math-prev-parts-v))))
1166 (or (and first-bad ; so try this one first
1167 (math-integrate-by-parts (nth 1 expr) (nth 2 expr)))
1168 (math-integrate-by-parts (nth 2 expr) (nth 1 expr))
1169 (and (not first-bad)
1170 (math-integrate-by-parts (nth 1 expr) (nth 2 expr))))))
1171 (and (eq (car expr) '/)
1172 (math-expr-contains (nth 1 expr) math-integ-var)
1173 (let ((recip (math-div 1 (nth 2 expr))))
1174 (or (math-integrate-by-parts (nth 1 expr) recip)
1175 (math-integrate-by-parts recip (nth 1 expr)))))
1176 (and (eq (car expr) '^)
1177 (math-integrate-by-parts (math-pow (nth 1 expr)
1178 (math-sub (nth 2 expr) 1))
1181 (defun math-integrate-by-parts (u vprime)
1182 (let ((math-integ-level (if (or math-good-parts
1183 (math-polynomial-p u math-integ-var))
1185 (1- math-integ-level)))
1186 (math-doing-parts t)
1188 (and (>= math-integ-level 0)
1191 (setcar (cdr math-cur-record) 'parts)
1192 (math-tracing-integral "Integrating by parts, u = "
1193 (math-format-value u 1000)
1195 (math-format-value vprime 1000)
1197 (and (setq v (math-integral vprime))
1198 (setq temp (calcFunc-deriv u math-integ-var nil t))
1199 (setq temp (let ((math-prev-parts-v v))
1200 (math-integral (math-mul v temp) 'yes)))
1201 (setq temp (math-sub (math-mul u v) temp))
1202 (if (eq (nth 1 math-cur-record) 'parts)
1203 (calcFunc-expand temp)
1204 (setq v (list 'var 'PARTS math-cur-record)
1205 temp (let (calc-next-why)
1206 (math-solve-for (math-sub v temp) 0 v nil)))
1207 (and temp (not (integerp temp))
1208 (math-simplify-extended temp)))))
1209 (setcar (cdr math-cur-record) 'busy)))))
1211 ;;; This tries two different formulations, hoping the algebraic simplifier
1212 ;;; will be strong enough to handle at least one.
1213 (defun math-integrate-by-substitution (expr u &optional user uinv uinvprime)
1214 (and (> math-integ-level 0)
1215 (let ((math-integ-level (max (- math-integ-level 2) 0)))
1216 (math-integrate-by-good-substitution expr u user uinv uinvprime))))
1218 (defun math-integrate-by-good-substitution (expr u &optional user
1220 (let ((math-living-dangerously t)
1222 (and (setq uinv (if uinv
1223 (math-expr-subst uinv math-integ-var
1225 (let (calc-next-why)
1228 math-integ-var nil))))
1230 (math-tracing-integral "Integrating by substitution, u = "
1231 (math-format-value u 1000)
1233 (or (and (setq deriv (calcFunc-deriv u
1236 (setq temp (math-integral (math-expr-subst
1239 (math-div expr deriv)
1247 (and (setq deriv (or uinvprime
1248 (calcFunc-deriv uinv
1252 (setq temp (math-integral (math-mul
1265 (math-simplify-extended
1266 (math-expr-subst temp math-integ-var u)))))
1268 ;;; Look for substitutions of the form u = a x + b.
1269 (defun math-integ-try-linear-substitutions (sub-expr)
1270 (setq math-linear-subst-tried t)
1271 (and (not (Math-primp sub-expr))
1272 (or (and (not (memq (car sub-expr) '(+ - * / neg)))
1273 (not (and (eq (car sub-expr) '^)
1274 (integerp (nth 2 sub-expr))))
1275 (math-expr-contains sub-expr math-integ-var)
1277 (while (and (setq sub-expr (cdr sub-expr))
1278 (or (not (math-linear-in (car sub-expr)
1280 (assoc (car sub-expr) math-so-far)
1282 (setq math-so-far (cons (list (car sub-expr))
1285 (math-integrate-by-substitution
1286 math-integ-expr (car sub-expr))))))))
1289 (while (and (setq sub-expr (cdr sub-expr))
1290 (not (setq res (math-integ-try-linear-substitutions
1294 ;;; Recursively try different substitutions based on various sub-expressions.
1295 (defun math-integ-try-substitutions (sub-expr &optional allow-rat)
1296 (and (not (Math-primp sub-expr))
1297 (not (assoc sub-expr math-so-far))
1298 (math-expr-contains sub-expr math-integ-var)
1299 (or (and (if (and (not (memq (car sub-expr) '(+ - * / neg)))
1300 (not (and (eq (car sub-expr) '^)
1301 (integerp (nth 2 sub-expr)))))
1303 (prog1 allow-rat (setq allow-rat nil)))
1304 (not (eq sub-expr math-integ-expr))
1305 (or (math-integrate-by-substitution math-integ-expr sub-expr)
1306 (and (eq (car sub-expr) '^)
1307 (integerp (nth 2 sub-expr))
1308 (< (nth 2 sub-expr) 0)
1309 (math-integ-try-substitutions
1310 (math-pow (nth 1 sub-expr) (- (nth 2 sub-expr)))
1313 (setq math-so-far (cons (list sub-expr) math-so-far))
1314 (while (and (setq sub-expr (cdr sub-expr))
1315 (not (setq res (math-integ-try-substitutions
1316 (car sub-expr) allow-rat)))))
1319 ;; The variable math-expr-parts is local to math-expr-rational-in,
1320 ;; but is used by math-expr-rational-in-rec
1321 (defvar math-expr-parts)
1323 (defun math-expr-rational-in (expr)
1324 (let ((math-expr-parts nil))
1325 (math-expr-rational-in-rec expr)
1326 (mapcar 'car math-expr-parts)))
1328 (defun math-expr-rational-in-rec (expr)
1329 (cond ((Math-primp expr)
1330 (and (equal expr math-integ-var)
1331 (not (assoc expr math-expr-parts))
1332 (setq math-expr-parts (cons (list expr) math-expr-parts))))
1333 ((or (memq (car expr) '(+ - * / neg))
1334 (and (eq (car expr) '^) (integerp (nth 2 expr))))
1335 (math-expr-rational-in-rec (nth 1 expr))
1336 (and (nth 2 expr) (math-expr-rational-in-rec (nth 2 expr))))
1337 ((and (eq (car expr) '^)
1338 (eq (math-quarter-integer (nth 2 expr)) 2))
1339 (math-expr-rational-in-rec (list 'calcFunc-sqrt (nth 1 expr))))
1341 (and (not (assoc expr math-expr-parts))
1342 (math-expr-contains expr math-integ-var)
1343 (setq math-expr-parts (cons (list expr) math-expr-parts))))))
1345 (defun math-expr-calls (expr funcs &optional arg-contains)
1347 (if (or (memq (car expr) funcs)
1348 (and (eq (car expr) '^) (eq (car funcs) 'calcFunc-sqrt)
1349 (eq (math-quarter-integer (nth 2 expr)) 2)))
1350 (and (or (not arg-contains)
1351 (math-expr-contains expr arg-contains))
1353 (and (not (Math-primp expr))
1355 (while (and (setq expr (cdr expr))
1356 (not (setq res (math-expr-calls
1357 (car expr) funcs arg-contains)))))
1360 (defun math-fix-const-terms (expr except-vars)
1361 (cond ((not (math-expr-depends expr except-vars)) 0)
1362 ((Math-primp expr) expr)
1364 (math-add (math-fix-const-terms (nth 1 expr) except-vars)
1365 (math-fix-const-terms (nth 2 expr) except-vars)))
1367 (math-sub (math-fix-const-terms (nth 1 expr) except-vars)
1368 (math-fix-const-terms (nth 2 expr) except-vars)))
1371 ;; Command for debugging the Calculator's symbolic integrator.
1372 (defun calc-dump-integral-cache (&optional arg)
1374 (let ((buf (current-buffer)))
1376 (let ((p math-integral-cache)
1378 (display-buffer (get-buffer-create "*Integral Cache*"))
1379 (set-buffer (get-buffer "*Integral Cache*"))
1382 (setq math-cur-record (car p))
1383 (or arg (math-replace-integral-parts math-cur-record))
1384 (insert (math-format-flat-expr (car math-cur-record) 0)
1386 (if (symbolp (nth 1 math-cur-record))
1387 (concat "(" (symbol-name (nth 1 math-cur-record)) ")")
1388 (math-format-flat-expr (nth 1 math-cur-record) 0))
1391 (goto-char (point-min)))
1394 ;; The variable math-max-integral-limit is local to calcFunc-integ,
1395 ;; but is used by math-try-integral.
1396 (defvar math-max-integral-limit)
1398 (defun math-try-integral (expr)
1399 (let ((math-integ-level math-integral-limit)
1400 (math-integ-depth 0)
1401 (math-integ-msg "Working...done")
1402 (math-cur-record nil) ; a technicality
1403 (math-integrating t)
1404 (calc-prefer-frac t)
1405 (calc-symbolic-mode t)
1406 (math-has-rules (calc-has-rules 'var-IntegRules)))
1407 (or (math-integral expr 'yes)
1408 (and math-any-substs
1409 (setq math-enable-subst t)
1410 (math-integral expr 'yes))
1411 (and (> math-max-integral-limit math-integral-limit)
1412 (setq math-integral-limit math-max-integral-limit
1413 math-integ-level math-integral-limit)
1414 (math-integral expr 'yes)))))
1416 (defvar var-IntegLimit nil)
1418 (defun calcFunc-integ (expr var &optional low high)
1420 ;; Do these even if the parts turn out not to be integrable.
1421 ((eq (car-safe expr) '+)
1422 (math-add (calcFunc-integ (nth 1 expr) var low high)
1423 (calcFunc-integ (nth 2 expr) var low high)))
1424 ((eq (car-safe expr) '-)
1425 (math-sub (calcFunc-integ (nth 1 expr) var low high)
1426 (calcFunc-integ (nth 2 expr) var low high)))
1427 ((eq (car-safe expr) 'neg)
1428 (math-neg (calcFunc-integ (nth 1 expr) var low high)))
1429 ((and (eq (car-safe expr) '*)
1430 (not (math-expr-contains (nth 1 expr) var)))
1431 (math-mul (nth 1 expr) (calcFunc-integ (nth 2 expr) var low high)))
1432 ((and (eq (car-safe expr) '*)
1433 (not (math-expr-contains (nth 2 expr) var)))
1434 (math-mul (calcFunc-integ (nth 1 expr) var low high) (nth 2 expr)))
1435 ((and (eq (car-safe expr) '/)
1436 (not (math-expr-contains (nth 1 expr) var))
1437 (not (math-equal-int (nth 1 expr) 1)))
1438 (math-mul (nth 1 expr)
1439 (calcFunc-integ (math-div 1 (nth 2 expr)) var low high)))
1440 ((and (eq (car-safe expr) '/)
1441 (not (math-expr-contains (nth 2 expr) var)))
1442 (math-div (calcFunc-integ (nth 1 expr) var low high) (nth 2 expr)))
1443 ((and (eq (car-safe expr) '/)
1444 (eq (car-safe (nth 1 expr)) '*)
1445 (not (math-expr-contains (nth 1 (nth 1 expr)) var)))
1446 (math-mul (nth 1 (nth 1 expr))
1447 (calcFunc-integ (math-div (nth 2 (nth 1 expr)) (nth 2 expr))
1449 ((and (eq (car-safe expr) '/)
1450 (eq (car-safe (nth 1 expr)) '*)
1451 (not (math-expr-contains (nth 2 (nth 1 expr)) var)))
1452 (math-mul (nth 2 (nth 1 expr))
1453 (calcFunc-integ (math-div (nth 1 (nth 1 expr)) (nth 2 expr))
1455 ((and (eq (car-safe expr) '/)
1456 (eq (car-safe (nth 2 expr)) '*)
1457 (not (math-expr-contains (nth 1 (nth 2 expr)) var)))
1458 (math-div (calcFunc-integ (math-div (nth 1 expr) (nth 2 (nth 2 expr)))
1460 (nth 1 (nth 2 expr))))
1461 ((and (eq (car-safe expr) '/)
1462 (eq (car-safe (nth 2 expr)) '*)
1463 (not (math-expr-contains (nth 2 (nth 2 expr)) var)))
1464 (math-div (calcFunc-integ (math-div (nth 1 expr) (nth 1 (nth 2 expr)))
1466 (nth 2 (nth 2 expr))))
1467 ((eq (car-safe expr) 'vec)
1468 (cons 'vec (mapcar (function (lambda (x) (calcFunc-integ x var low high)))
1471 (let ((state (list calc-angle-mode
1472 ;;calc-symbolic-mode
1475 (calc-var-value 'var-IntegRules)
1476 (calc-var-value 'var-IntegSimpRules))))
1477 (or (equal state math-integral-cache-state)
1478 (setq math-integral-cache-state state
1479 math-integral-cache nil)))
1480 (let* ((math-max-integral-limit (or (and (natnump var-IntegLimit)
1483 (math-integral-limit 1)
1484 (sexpr (math-expr-subst expr var math-integ-var))
1485 (trace-buffer (get-buffer "*Trace*"))
1486 (calc-language (if (eq calc-language 'big) nil calc-language))
1488 (math-enable-subst nil)
1489 (math-prev-parts-v nil)
1490 (math-doing-parts nil)
1491 (math-good-parts nil)
1494 (let ((calcbuf (current-buffer))
1495 (calcwin (selected-window)))
1498 (if (get-buffer-window trace-buffer)
1499 (select-window (get-buffer-window trace-buffer)))
1500 (set-buffer trace-buffer)
1501 (goto-char (point-max))
1502 (or (assq 'scroll-stop (buffer-local-variables))
1504 (make-local-variable 'scroll-step)
1505 (setq scroll-step 3)))
1507 (set-buffer calcbuf)
1508 (math-try-integral sexpr))
1509 (select-window calcwin)
1510 (set-buffer calcbuf)))
1511 (math-try-integral sexpr))))
1514 (if (calc-has-rules 'var-IntegAfterRules)
1515 (setq res (math-rewrite res '(var IntegAfterRules
1516 var-IntegAfterRules))))
1519 (math-sub (math-expr-subst res math-integ-var high)
1520 (math-expr-subst res math-integ-var low))
1521 (setq res (math-fix-const-terms res math-integ-vars))
1523 (math-expr-subst res math-integ-var low)
1524 (math-expr-subst res math-integ-var var)))))
1525 (append (list 'calcFunc-integ expr var)
1526 (and low (list low))
1527 (and high (list high))))))))
1530 (math-defintegral calcFunc-inv
1531 (math-integral (math-div 1 u)))
1533 (math-defintegral calcFunc-conj
1534 (let ((int (math-integral u)))
1536 (list 'calcFunc-conj int))))
1538 (math-defintegral calcFunc-deg
1539 (let ((int (math-integral u)))
1541 (list 'calcFunc-deg int))))
1543 (math-defintegral calcFunc-rad
1544 (let ((int (math-integral u)))
1546 (list 'calcFunc-rad int))))
1548 (math-defintegral calcFunc-re
1549 (let ((int (math-integral u)))
1551 (list 'calcFunc-re int))))
1553 (math-defintegral calcFunc-im
1554 (let ((int (math-integral u)))
1556 (list 'calcFunc-im int))))
1558 (math-defintegral calcFunc-sqrt
1559 (and (equal u math-integ-var)
1560 (math-mul '(frac 2 3)
1561 (list 'calcFunc-sqrt (math-pow u 3)))))
1563 (math-defintegral calcFunc-exp
1564 (or (and (equal u math-integ-var)
1565 (list 'calcFunc-exp u))
1566 (let ((p (math-is-polynomial u math-integ-var 2)))
1568 (let ((sqa (math-sqrt (math-neg (nth 2 p)))))
1571 (math-mul (math-div (list 'calcFunc-sqrt '(var pi var-pi))
1575 (math-div (math-sub (math-mul (car p)
1578 (math-sqr (nth 1 p))
1582 (math-sub (math-mul sqa math-integ-var)
1583 (math-div (nth 1 p) (math-mul 2 sqa)))))
1586 (math-defintegral calcFunc-ln
1587 (or (and (equal u math-integ-var)
1588 (math-sub (math-mul u (list 'calcFunc-ln u)) u))
1589 (and (eq (car u) '*)
1590 (math-integral (math-add (list 'calcFunc-ln (nth 1 u))
1591 (list 'calcFunc-ln (nth 2 u)))))
1592 (and (eq (car u) '/)
1593 (math-integral (math-sub (list 'calcFunc-ln (nth 1 u))
1594 (list 'calcFunc-ln (nth 2 u)))))
1595 (and (eq (car u) '^)
1596 (math-integral (math-mul (nth 2 u)
1597 (list 'calcFunc-ln (nth 1 u)))))))
1599 (math-defintegral calcFunc-log10
1600 (and (equal u math-integ-var)
1601 (math-sub (math-mul u (list 'calcFunc-ln u))
1602 (math-div u (list 'calcFunc-ln 10)))))
1604 (math-defintegral-2 calcFunc-log
1605 (math-integral (math-div (list 'calcFunc-ln u)
1606 (list 'calcFunc-ln v))))
1608 (math-defintegral calcFunc-sin
1609 (or (and (equal u math-integ-var)
1610 (math-neg (math-from-radians-2 (list 'calcFunc-cos u))))
1611 (and (nth 2 (math-is-polynomial u math-integ-var 2))
1612 (math-integral (math-to-exponentials (list 'calcFunc-sin u))))))
1614 (math-defintegral calcFunc-cos
1615 (or (and (equal u math-integ-var)
1616 (math-from-radians-2 (list 'calcFunc-sin u)))
1617 (and (nth 2 (math-is-polynomial u math-integ-var 2))
1618 (math-integral (math-to-exponentials (list 'calcFunc-cos u))))))
1620 (math-defintegral calcFunc-tan
1621 (and (equal u math-integ-var)
1622 (math-neg (math-from-radians-2
1623 (list 'calcFunc-ln (list 'calcFunc-cos u))))))
1625 (math-defintegral calcFunc-arcsin
1626 (and (equal u math-integ-var)
1627 (math-add (math-mul u (list 'calcFunc-arcsin u))
1628 (math-from-radians-2
1629 (list 'calcFunc-sqrt (math-sub 1 (math-sqr u)))))))
1631 (math-defintegral calcFunc-arccos
1632 (and (equal u math-integ-var)
1633 (math-sub (math-mul u (list 'calcFunc-arccos u))
1634 (math-from-radians-2
1635 (list 'calcFunc-sqrt (math-sub 1 (math-sqr u)))))))
1637 (math-defintegral calcFunc-arctan
1638 (and (equal u math-integ-var)
1639 (math-sub (math-mul u (list 'calcFunc-arctan u))
1640 (math-from-radians-2
1641 (math-div (list 'calcFunc-ln (math-add 1 (math-sqr u)))
1644 (math-defintegral calcFunc-sinh
1645 (and (equal u math-integ-var)
1646 (list 'calcFunc-cosh u)))
1648 (math-defintegral calcFunc-cosh
1649 (and (equal u math-integ-var)
1650 (list 'calcFunc-sinh u)))
1652 (math-defintegral calcFunc-tanh
1653 (and (equal u math-integ-var)
1654 (list 'calcFunc-ln (list 'calcFunc-cosh u))))
1656 (math-defintegral calcFunc-arcsinh
1657 (and (equal u math-integ-var)
1658 (math-sub (math-mul u (list 'calcFunc-arcsinh u))
1659 (list 'calcFunc-sqrt (math-add (math-sqr u) 1)))))
1661 (math-defintegral calcFunc-arccosh
1662 (and (equal u math-integ-var)
1663 (math-sub (math-mul u (list 'calcFunc-arccosh u))
1664 (list 'calcFunc-sqrt (math-sub 1 (math-sqr u))))))
1666 (math-defintegral calcFunc-arctanh
1667 (and (equal u math-integ-var)
1668 (math-sub (math-mul u (list 'calcFunc-arctan u))
1669 (math-div (list 'calcFunc-ln
1670 (math-add 1 (math-sqr u)))
1673 ;;; (Ax + B) / (ax^2 + bx + c)^n forms.
1674 (math-defintegral-2 /
1675 (math-integral-rational-funcs u v))
1677 (defun math-integral-rational-funcs (u v)
1678 (let ((pu (math-is-polynomial u math-integ-var 1))
1682 (if (and (eq (car-safe v) '^) (natnump (nth 2 v)))
1683 (setq vpow (nth 2 v)
1685 (and (setq pv (math-is-polynomial v math-integ-var 2))
1686 (let ((int (math-mul-thru
1688 (math-integral-q02 (car pv) (nth 1 pv)
1689 (nth 2 pv) v vpow))))
1691 (setq int (math-add int
1696 (nth 2 pv) v vpow)))))
1699 (defun math-integral-q12 (a b c v vpow)
1703 (math-sub (math-div math-integ-var b)
1704 (math-mul (math-div a (math-sqr b))
1705 (list 'calcFunc-ln v))))
1707 (math-div (math-add (list 'calcFunc-ln v)
1711 (let ((nm1 (math-sub vpow 1))
1712 (nm2 (math-sub vpow 2)))
1714 (math-div a (math-mul nm1 (math-pow v nm1)))
1715 (math-div 1 (math-mul nm2 (math-pow v nm2))))
1718 (setq q (math-sub (math-mul 4 (math-mul a c)) (math-sqr b))))
1719 (let ((part (math-div b (math-mul 2 c))))
1720 (math-mul-thru (math-pow c vpow)
1721 (math-integral-q12 part 1 nil
1722 (math-add math-integ-var part)
1725 (and (math-ratp q) (math-negp q)
1726 (let ((calc-symbolic-mode t))
1727 (math-ratp (math-sqrt (math-neg q))))
1728 (throw 'int-rat nil)) ; should have used calcFunc-apart first
1729 (math-sub (math-div (list 'calcFunc-ln v) (math-mul 2 c))
1730 (math-mul-thru (math-div b (math-mul 2 c))
1731 (math-integral-q02 a b c v 1))))
1733 (let ((n (1- vpow)))
1734 (math-sub (math-neg (math-div
1735 (math-add (math-mul b math-integ-var)
1737 (math-mul n (math-mul q (math-pow v n)))))
1738 (math-mul-thru (math-div (math-mul b (1- (* 2 n)))
1740 (math-integral-q02 a b c v n))))))))
1742 (defun math-integral-q02 (a b c v vpow)
1746 (math-div (list 'calcFunc-ln v) b))
1748 (math-div (math-pow v (- 1 vpow))
1749 (math-mul (- 1 vpow) b)))))
1751 (setq q (math-sub (math-mul 4 (math-mul a c)) (math-sqr b))))
1752 (let ((part (math-div b (math-mul 2 c))))
1753 (math-mul-thru (math-pow c vpow)
1754 (math-integral-q02 part 1 nil
1755 (math-add math-integ-var part)
1758 (setq part (math-add (math-mul 2 (math-mul c math-integ-var)) b))
1760 (let ((n (1- vpow)))
1761 (math-add (math-div part (math-mul n (math-mul q (math-pow v n))))
1762 (math-mul-thru (math-div (math-mul (- (* 4 n) 2) c)
1764 (math-integral-q02 a b c v n)))))
1765 ((math-guess-if-neg q)
1766 (setq rq (list 'calcFunc-sqrt (math-neg q)))
1767 ;;(math-div-thru (list 'calcFunc-ln
1768 ;; (math-div (math-sub part rq)
1769 ;; (math-add part rq)))
1771 (math-div (math-mul -2 (list 'calcFunc-arctanh
1772 (math-div part rq)))
1775 (setq rq (list 'calcFunc-sqrt q))
1776 (math-div (math-mul 2 (math-to-radians-2
1777 (list 'calcFunc-arctan
1778 (math-div part rq))))
1782 (math-defintegral calcFunc-erf
1783 (and (equal u math-integ-var)
1784 (math-add (math-mul u (list 'calcFunc-erf u))
1785 (math-div 1 (math-mul (list 'calcFunc-exp (math-sqr u))
1786 (list 'calcFunc-sqrt
1787 '(var pi var-pi)))))))
1789 (math-defintegral calcFunc-erfc
1790 (and (equal u math-integ-var)
1791 (math-sub (math-mul u (list 'calcFunc-erfc u))
1792 (math-div 1 (math-mul (list 'calcFunc-exp (math-sqr u))
1793 (list 'calcFunc-sqrt
1794 '(var pi var-pi)))))))
1799 (defvar math-tabulate-initial nil)
1800 (defvar math-tabulate-function nil)
1802 ;; The variables calc-low and calc-high are local to calcFunc-table,
1803 ;; but are used by math-scan-for-limits.
1807 (defun calcFunc-table (expr var &optional calc-low calc-high step)
1809 (setq calc-low '(neg (var inf var-inf)) calc-high '(var inf var-inf)))
1810 (or calc-high (setq calc-high calc-low calc-low 1))
1811 (and (or (math-infinitep calc-low) (math-infinitep calc-high))
1813 (math-scan-for-limits expr))
1814 (and step (math-zerop step) (math-reject-arg step 'nonzerop))
1815 (let ((known (+ (if (Math-objectp calc-low) 1 0)
1816 (if (Math-objectp calc-high) 1 0)
1817 (if (or (null step) (Math-objectp step)) 1 0)))
1818 (count '(var inf var-inf))
1820 (or (= known 2) ; handy optimization
1821 (equal calc-high '(var inf var-inf))
1823 (setq count (math-div (math-sub calc-high calc-low) (or step 1)))
1824 (or (Math-objectp count)
1825 (setq count (math-simplify count)))
1826 (if (Math-messy-integerp count)
1827 (setq count (math-trunc count)))))
1828 (if (Math-negp count)
1830 (if (integerp count)
1831 (let ((var-DUMMY nil)
1832 (vec math-tabulate-initial)
1833 (math-working-step-2 (1+ count))
1834 (math-working-step 0))
1835 (setq expr (math-evaluate-expr
1836 (math-expr-subst expr var '(var DUMMY var-DUMMY))))
1838 (setq math-working-step (1+ math-working-step)
1840 vec (cond ((eq math-tabulate-function 'calcFunc-sum)
1841 (math-add vec (math-evaluate-expr expr)))
1842 ((eq math-tabulate-function 'calcFunc-prod)
1843 (math-mul vec (math-evaluate-expr expr)))
1845 (cons (math-evaluate-expr expr) vec)))
1846 calc-low (math-add calc-low (or step 1))
1848 (if math-tabulate-function
1850 (cons 'vec (nreverse vec))))
1851 (if (Math-integerp count)
1852 (calc-record-why 'fixnump calc-high)
1853 (if (Math-num-integerp calc-low)
1854 (if (Math-num-integerp calc-high)
1855 (calc-record-why 'integerp step)
1856 (calc-record-why 'integerp calc-high))
1857 (calc-record-why 'integerp calc-low)))
1858 (append (list (or math-tabulate-function 'calcFunc-table)
1860 (and (not (and (equal calc-low '(neg (var inf var-inf)))
1861 (equal calc-high '(var inf var-inf))))
1862 (list calc-low calc-high))
1863 (and step (list step))))))
1865 (defun math-scan-for-limits (x)
1866 (cond ((Math-primp x))
1867 ((and (eq (car x) 'calcFunc-subscr)
1868 (Math-vectorp (nth 1 x))
1869 (math-expr-contains (nth 2 x) var))
1870 (let* ((calc-next-why nil)
1871 (low-val (math-solve-for (nth 2 x) 1 var nil))
1872 (high-val (math-solve-for (nth 2 x) (1- (length (nth 1 x)))
1875 (and low-val (math-realp low-val)
1876 high-val (math-realp high-val))
1877 (and (Math-lessp high-val low-val)
1878 (setq temp low-val low-val high-val high-val temp))
1879 (setq calc-low (math-max calc-low (math-ceiling low-val))
1880 calc-high (math-min calc-high (math-floor high-val)))))
1882 (while (setq x (cdr x))
1883 (math-scan-for-limits (car x))))))
1886 (defvar math-disable-sums nil)
1887 (defun calcFunc-sum (expr var &optional low high step)
1888 (if math-disable-sums (math-reject-arg))
1889 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2)))
1890 (math-sum-rec expr var low high step)))
1891 (math-disable-sums t))
1892 (math-normalize res)))
1894 (defun math-sum-rec (expr var &optional low high step)
1895 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf)))
1896 (and low (not high) (setq high low low 1))
1900 ((not (math-expr-contains expr var))
1901 (math-mul expr (math-add (math-div (math-sub high low) (or step 1))
1903 ((and step (not (math-equal-int step 1)))
1904 (if (math-negp step)
1905 (math-sum-rec expr var high low (math-neg step))
1906 (let ((lo (math-simplify (math-div low step))))
1907 (if (math-known-num-integerp lo)
1908 (math-sum-rec (math-normalize
1909 (math-expr-subst expr var
1910 (math-mul step var)))
1911 var lo (math-simplify (math-div high step)))
1912 (math-sum-rec (math-normalize
1913 (math-expr-subst expr var
1914 (math-add (math-mul step var)
1917 (math-simplify (math-div (math-sub high low)
1919 ((memq (setq t1 (math-compare low high)) '(0 1))
1921 (math-expr-subst expr var low)
1923 ((setq t1 (math-is-polynomial expr var 20))
1927 (setq poly (math-poly-mix poly 1
1928 (math-sum-integer-power n) (car t1))
1931 (setq n (math-build-polynomial-expr poly high))
1932 (if (memq low '(0 1))
1934 (math-sub n (math-build-polynomial-expr poly
1935 (math-sub low 1))))))
1936 ((and (memq (car expr) '(+ -))
1937 (setq t1 (math-sum-rec (nth 1 expr) var low high)
1938 t2 (math-sum-rec (nth 2 expr) var low high))
1939 (not (and (math-expr-calls t1 '(calcFunc-sum))
1940 (math-expr-calls t2 '(calcFunc-sum)))))
1941 (list (car expr) t1 t2))
1942 ((and (eq (car expr) '*)
1943 (setq t1 (math-sum-const-factors expr var)))
1944 (math-mul (car t1) (math-sum-rec (cdr t1) var low high)))
1945 ((and (eq (car expr) '*) (memq (car-safe (nth 1 expr)) '(+ -)))
1946 (math-sum-rec (math-add-or-sub (math-mul (nth 1 (nth 1 expr))
1948 (math-mul (nth 2 (nth 1 expr))
1950 nil (eq (car (nth 1 expr)) '-))
1952 ((and (eq (car expr) '*) (memq (car-safe (nth 2 expr)) '(+ -)))
1953 (math-sum-rec (math-add-or-sub (math-mul (nth 1 expr)
1954 (nth 1 (nth 2 expr)))
1955 (math-mul (nth 1 expr)
1956 (nth 2 (nth 2 expr)))
1957 nil (eq (car (nth 2 expr)) '-))
1959 ((and (eq (car expr) '/)
1960 (not (math-primp (nth 1 expr)))
1961 (setq t1 (math-sum-const-factors (nth 1 expr) var)))
1963 (math-sum-rec (math-div (cdr t1) (nth 2 expr))
1965 ((and (eq (car expr) '/)
1966 (setq t1 (math-sum-const-factors (nth 2 expr) var)))
1967 (math-div (math-sum-rec (math-div (nth 1 expr) (cdr t1))
1970 ((eq (car expr) 'neg)
1971 (math-neg (math-sum-rec (nth 1 expr) var low high)))
1972 ((and (eq (car expr) '^)
1973 (not (math-expr-contains (nth 1 expr) var))
1974 (setq t1 (math-is-polynomial (nth 2 expr) var 1)))
1975 (let ((x (math-pow (nth 1 expr) (nth 1 t1))))
1976 (math-div (math-mul (math-sub (math-pow x (math-add 1 high))
1978 (math-pow (nth 1 expr) (car t1)))
1980 ((and (setq t1 (math-to-exponentials expr))
1981 (setq t1 (math-sum-rec t1 var low high))
1982 (not (math-expr-calls t1 '(calcFunc-sum))))
1984 ((memq (car expr) '(calcFunc-ln calcFunc-log10))
1985 (list (car expr) (calcFunc-prod (nth 1 expr) var low high)))
1986 ((and (eq (car expr) 'calcFunc-log)
1988 (not (math-expr-contains (nth 2 expr) var)))
1990 (calcFunc-prod (nth 1 expr) var low high)
1992 (if (equal val '(var nan var-nan)) (setq val nil))
1994 (let* ((math-tabulate-initial 0)
1995 (math-tabulate-function 'calcFunc-sum))
1996 (calcFunc-table expr var low high)))))
1998 (defun calcFunc-asum (expr var low &optional high step no-mul-flag)
1999 (or high (setq high low low 1))
2000 (if (and step (not (math-equal-int step 1)))
2001 (if (math-negp step)
2002 (math-mul (math-pow -1 low)
2003 (calcFunc-asum expr var high low (math-neg step) t))
2004 (let ((lo (math-simplify (math-div low step))))
2005 (if (math-num-integerp lo)
2006 (calcFunc-asum (math-normalize
2007 (math-expr-subst expr var
2008 (math-mul step var)))
2009 var lo (math-simplify (math-div high step)))
2010 (calcFunc-asum (math-normalize
2011 (math-expr-subst expr var
2012 (math-add (math-mul step var)
2015 (math-simplify (math-div (math-sub high low)
2017 (math-mul (if no-mul-flag 1 (math-pow -1 low))
2018 (calcFunc-sum (math-mul (math-pow -1 var) expr) var low high))))
2020 (defun math-sum-const-factors (expr var)
2024 (while (eq (car-safe p) '*)
2025 (if (math-expr-contains (nth 1 p) var)
2026 (setq not-const (cons (nth 1 p) not-const))
2027 (setq const (cons (nth 1 p) const)))
2029 (if (math-expr-contains p var)
2030 (setq not-const (cons p not-const))
2031 (setq const (cons p const)))
2033 (cons (let ((temp (car const)))
2034 (while (setq const (cdr const))
2035 (setq temp (list '* (car const) temp)))
2037 (let ((temp (or (car not-const) 1)))
2038 (while (setq not-const (cdr not-const))
2039 (setq temp (list '* (car not-const) temp)))
2042 (defvar math-sum-int-pow-cache (list '(0 1)))
2043 ;; Following is from CRC Math Tables, 27th ed, pp. 52-53.
2044 (defun math-sum-integer-power (pow)
2045 (let ((calc-prefer-frac t)
2046 (n (length math-sum-int-pow-cache)))
2048 (let* ((new (list 0 0))
2050 (pp (cdr (nth (1- n) math-sum-int-pow-cache)))
2055 (setq q (math-div (car pp) p)
2056 new (cons (math-mul q n) new)
2057 sum (math-add sum q)
2060 (setcar lin (math-sub 1 (math-mul n sum)))
2061 (setq math-sum-int-pow-cache
2062 (nconc math-sum-int-pow-cache (list (nreverse new)))
2064 (nth pow math-sum-int-pow-cache)))
2066 (defun math-to-exponentials (expr)
2069 (let ((x (nth 1 expr))
2070 (pi (if calc-symbolic-mode '(var pi var-pi) (math-pi)))
2071 (i (if calc-symbolic-mode '(var i var-i) '(cplx 0 1))))
2072 (cond ((eq (car expr) 'calcFunc-exp)
2073 (list '^ '(var e var-e) x))
2074 ((eq (car expr) 'calcFunc-sin)
2075 (or (eq calc-angle-mode 'rad)
2076 (setq x (list '/ (list '* x pi) 180)))
2078 (list '^ '(var e var-e) (list '* x i))
2079 (list '^ '(var e var-e)
2080 (list 'neg (list '* x i))))
2082 ((eq (car expr) 'calcFunc-cos)
2083 (or (eq calc-angle-mode 'rad)
2084 (setq x (list '/ (list '* x pi) 180)))
2086 (list '^ '(var e var-e)
2088 (list '^ '(var e var-e)
2089 (list 'neg (list '* x i))))
2091 ((eq (car expr) 'calcFunc-sinh)
2093 (list '^ '(var e var-e) x)
2094 (list '^ '(var e var-e) (list 'neg x)))
2096 ((eq (car expr) 'calcFunc-cosh)
2098 (list '^ '(var e var-e) x)
2099 (list '^ '(var e var-e) (list 'neg x)))
2103 (defun math-to-exps (expr)
2104 (cond (calc-symbolic-mode expr)
2106 (if (equal expr '(var e var-e)) (math-e) expr))
2107 ((and (eq (car expr) '^)
2108 (equal (nth 1 expr) '(var e var-e)))
2109 (list 'calcFunc-exp (nth 2 expr)))
2111 (cons (car expr) (mapcar 'math-to-exps (cdr expr))))))
2114 (defvar math-disable-prods nil)
2115 (defun calcFunc-prod (expr var &optional low high step)
2116 (if math-disable-prods (math-reject-arg))
2117 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2)))
2118 (math-prod-rec expr var low high step)))
2119 (math-disable-prods t))
2120 (math-normalize res)))
2122 (defun math-prod-rec (expr var &optional low high step)
2123 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf)))
2124 (and low (not high) (setq high '(var inf var-inf)))
2128 ((not (math-expr-contains expr var))
2129 (math-pow expr (math-add (math-div (math-sub high low) (or step 1))
2131 ((and step (not (math-equal-int step 1)))
2132 (if (math-negp step)
2133 (math-prod-rec expr var high low (math-neg step))
2134 (let ((lo (math-simplify (math-div low step))))
2135 (if (math-known-num-integerp lo)
2136 (math-prod-rec (math-normalize
2137 (math-expr-subst expr var
2138 (math-mul step var)))
2139 var lo (math-simplify (math-div high step)))
2140 (math-prod-rec (math-normalize
2141 (math-expr-subst expr var
2142 (math-add (math-mul step
2146 (math-simplify (math-div (math-sub high low)
2148 ((and (memq (car expr) '(* /))
2149 (setq t1 (math-prod-rec (nth 1 expr) var low high)
2150 t2 (math-prod-rec (nth 2 expr) var low high))
2151 (not (and (math-expr-calls t1 '(calcFunc-prod))
2152 (math-expr-calls t2 '(calcFunc-prod)))))
2153 (list (car expr) t1 t2))
2154 ((and (eq (car expr) '^)
2155 (not (math-expr-contains (nth 2 expr) var)))
2156 (math-pow (math-prod-rec (nth 1 expr) var low high)
2158 ((and (eq (car expr) '^)
2159 (not (math-expr-contains (nth 1 expr) var)))
2160 (math-pow (nth 1 expr)
2161 (calcFunc-sum (nth 2 expr) var low high)))
2162 ((eq (car expr) 'sqrt)
2163 (math-normalize (list 'calcFunc-sqrt
2164 (list 'calcFunc-prod (nth 1 expr)
2166 ((eq (car expr) 'neg)
2167 (math-mul (math-pow -1 (math-add (math-sub high low) 1))
2168 (math-prod-rec (nth 1 expr) var low high)))
2169 ((eq (car expr) 'calcFunc-exp)
2170 (list 'calcFunc-exp (calcFunc-sum (nth 1 expr) var low high)))
2171 ((and (setq t1 (math-is-polynomial expr var 1))
2174 ((or (and (math-equal-int (nth 1 t1) 1)
2175 (setq low (math-simplify
2176 (math-add low (car t1)))
2178 (math-add high (car t1)))))
2179 (and (math-equal-int (nth 1 t1) -1)
2182 (math-sub (car t1) high))
2184 (math-sub (car t1) t2)))))
2185 (if (or (math-zerop low) (math-zerop high))
2187 (if (and (or (math-negp low) (math-negp high))
2188 (or (math-num-integerp low)
2189 (math-num-integerp high)))
2190 (if (math-posp high)
2192 (math-mul (math-pow -1
2194 (math-add low high) 1))
2196 (list 'calcFunc-fact
2198 (list 'calcFunc-fact
2199 (math-sub -1 high)))))
2201 (list 'calcFunc-fact high)
2202 (list 'calcFunc-fact (math-sub low 1))))))
2203 ((and (or (and (math-equal-int (nth 1 t1) 2)
2204 (setq t2 (math-simplify
2205 (math-add (math-mul low 2)
2208 (math-add (math-mul high 2)
2210 (and (math-equal-int (nth 1 t1) -2)
2211 (setq t2 (math-simplify
2218 (or (math-integerp t2)
2219 (and (math-messy-integerp t2)
2220 (setq t2 (math-trunc t2)))
2222 (and (math-messy-integerp t3)
2223 (setq t3 (math-trunc t3)))))
2224 (if (or (math-zerop t2) (math-zerop t3))
2226 (if (or (math-evenp t2) (math-evenp t3))
2227 (if (or (math-negp t2) (math-negp t3))
2228 (if (math-posp high)
2231 (list 'calcFunc-dfact
2233 (list 'calcFunc-dfact
2236 (list 'calcFunc-dfact t3)
2237 (list 'calcFunc-dfact
2242 (list '/ (list '- (list '- t2 t3)
2246 (list 'calcFunc-dfact
2248 (list 'calcFunc-dfact
2252 (list 'calcFunc-dfact t3)
2253 (list 'calcFunc-dfact
2257 (if (equal val '(var nan var-nan)) (setq val nil))
2259 (let* ((math-tabulate-initial 1)
2260 (math-tabulate-function 'calcFunc-prod))
2261 (calcFunc-table expr var low high)))))
2266 (defvar math-solve-ranges nil)
2267 (defvar math-solve-sign)
2268 ;;; Attempt to reduce math-solve-lhs = math-solve-rhs to
2269 ;;; math-solve-var = math-solve-rhs', where math-solve-var appears
2270 ;;; in math-solve-lhs but not in math-solve-rhs or math-solve-rhs';
2271 ;;; return math-solve-rhs'.
2272 ;;; Uses global values: math-solve-var, math-solve-full.
2273 (defvar math-solve-var)
2274 (defvar math-solve-full)
2276 ;; The variables math-solve-lhs, math-solve-rhs and math-try-solve-sign
2277 ;; are local to math-try-solve-for, but are used by math-try-solve-prod.
2278 ;; (math-solve-lhs and math-solve-rhs are is also local to
2279 ;; math-decompose-poly, but used by math-solve-poly-funny-powers.)
2280 (defvar math-solve-lhs)
2281 (defvar math-solve-rhs)
2282 (defvar math-try-solve-sign)
2284 (defun math-try-solve-for
2285 (math-solve-lhs math-solve-rhs &optional math-try-solve-sign no-poly)
2286 (let (math-t1 math-t2 math-t3)
2287 (cond ((equal math-solve-lhs math-solve-var)
2288 (setq math-solve-sign math-try-solve-sign)
2289 (if (eq math-solve-full 'all)
2290 (let ((vec (list 'vec (math-evaluate-expr math-solve-rhs)))
2292 (while math-solve-ranges
2293 (setq p (car math-solve-ranges)
2296 (while (setq p (cdr p))
2297 (setq newvec (nconc newvec
2298 (cdr (math-expr-subst
2299 vec var (car p))))))
2301 math-solve-ranges (cdr math-solve-ranges)))
2302 (math-normalize vec))
2304 ((Math-primp math-solve-lhs)
2306 ((and (eq (car math-solve-lhs) '-)
2307 (eq (car-safe (nth 1 math-solve-lhs)) (car-safe (nth 2 math-solve-lhs)))
2308 (Math-zerop math-solve-rhs)
2309 (= (length (nth 1 math-solve-lhs)) 2)
2310 (= (length (nth 2 math-solve-lhs)) 2)
2311 (setq math-t1 (get (car (nth 1 math-solve-lhs)) 'math-inverse))
2312 (setq math-t2 (funcall math-t1 '(var SOLVEDUM SOLVEDUM)))
2313 (eq (math-expr-contains-count math-t2 '(var SOLVEDUM SOLVEDUM)) 1)
2314 (setq math-t3 (math-solve-above-dummy math-t2))
2315 (setq math-t1 (math-try-solve-for
2316 (math-sub (nth 1 (nth 1 math-solve-lhs))
2319 (nth 1 (nth 2 math-solve-lhs))))
2322 ((eq (car math-solve-lhs) 'neg)
2323 (math-try-solve-for (nth 1 math-solve-lhs) (math-neg math-solve-rhs)
2324 (and math-try-solve-sign (- math-try-solve-sign))))
2325 ((and (not (eq math-solve-full 't)) (math-try-solve-prod)))
2328 (math-decompose-poly math-solve-lhs
2329 math-solve-var 15 math-solve-rhs)))
2330 (setq math-t1 (cdr (nth 1 math-t2))
2331 math-t1 (let ((math-solve-ranges math-solve-ranges))
2332 (cond ((= (length math-t1) 5)
2333 (apply 'math-solve-quartic (car math-t2) math-t1))
2334 ((= (length math-t1) 4)
2335 (apply 'math-solve-cubic (car math-t2) math-t1))
2336 ((= (length math-t1) 3)
2337 (apply 'math-solve-quadratic (car math-t2) math-t1))
2338 ((= (length math-t1) 2)
2339 (apply 'math-solve-linear
2340 (car math-t2) math-try-solve-sign math-t1))
2342 (math-poly-all-roots (car math-t2) math-t1))
2343 (calc-symbolic-mode nil)
2347 (math-poly-any-root (reverse math-t1) 0 t)
2350 (if (eq (nth 2 math-t2) 1)
2352 (math-solve-prod math-t1 (math-try-solve-for (nth 2 math-t2) 0 nil t)))
2353 (calc-record-why "*Unable to find a symbolic solution")
2355 ((and (math-solve-find-root-term math-solve-lhs nil)
2356 (eq (math-expr-contains-count math-solve-lhs math-t1) 1)) ; just in case
2357 (math-try-solve-for (math-simplify
2358 (math-sub (if (or math-t3 (math-evenp math-t2))
2359 (math-pow math-t1 math-t2)
2360 (math-neg (math-pow math-t1 math-t2)))
2362 (math-sub (math-normalize
2364 math-solve-lhs math-t1 0))
2366 math-t2 math-solve-var)))
2368 ((eq (car math-solve-lhs) '+)
2369 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2370 (math-try-solve-for (nth 2 math-solve-lhs)
2371 (math-sub math-solve-rhs (nth 1 math-solve-lhs))
2372 math-try-solve-sign))
2373 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2374 (math-try-solve-for (nth 1 math-solve-lhs)
2375 (math-sub math-solve-rhs (nth 2 math-solve-lhs))
2376 math-try-solve-sign))))
2377 ((eq (car math-solve-lhs) 'calcFunc-eq)
2378 (math-try-solve-for (math-sub (nth 1 math-solve-lhs) (nth 2 math-solve-lhs))
2379 math-solve-rhs math-try-solve-sign no-poly))
2380 ((eq (car math-solve-lhs) '-)
2381 (cond ((or (and (eq (car-safe (nth 1 math-solve-lhs)) 'calcFunc-sin)
2382 (eq (car-safe (nth 2 math-solve-lhs)) 'calcFunc-cos))
2383 (and (eq (car-safe (nth 1 math-solve-lhs)) 'calcFunc-cos)
2384 (eq (car-safe (nth 2 math-solve-lhs)) 'calcFunc-sin)))
2385 (math-try-solve-for (math-sub (nth 1 math-solve-lhs)
2386 (list (car (nth 1 math-solve-lhs))
2388 (math-quarter-circle t)
2389 (nth 1 (nth 2 math-solve-lhs)))))
2391 ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2392 (math-try-solve-for (nth 2 math-solve-lhs)
2393 (math-sub (nth 1 math-solve-lhs) math-solve-rhs)
2394 (and math-try-solve-sign
2395 (- math-try-solve-sign))))
2396 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2397 (math-try-solve-for (nth 1 math-solve-lhs)
2398 (math-add math-solve-rhs (nth 2 math-solve-lhs))
2399 math-try-solve-sign))))
2400 ((and (eq math-solve-full 't) (math-try-solve-prod)))
2401 ((and (eq (car math-solve-lhs) '%)
2402 (not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var)))
2403 (math-try-solve-for (nth 1 math-solve-lhs) (math-add math-solve-rhs
2405 (nth 2 math-solve-lhs)))))
2406 ((eq (car math-solve-lhs) 'calcFunc-log)
2407 (cond ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2408 (math-try-solve-for (nth 1 math-solve-lhs)
2409 (math-pow (nth 2 math-solve-lhs) math-solve-rhs)))
2410 ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2411 (math-try-solve-for (nth 2 math-solve-lhs) (math-pow
2412 (nth 1 math-solve-lhs)
2413 (math-div 1 math-solve-rhs))))))
2414 ((and (= (length math-solve-lhs) 2)
2415 (symbolp (car math-solve-lhs))
2416 (setq math-t1 (get (car math-solve-lhs) 'math-inverse))
2417 (setq math-t2 (funcall math-t1 math-solve-rhs)))
2418 (setq math-t1 (get (car math-solve-lhs) 'math-inverse-sign))
2419 (math-try-solve-for (nth 1 math-solve-lhs) (math-normalize math-t2)
2420 (and math-try-solve-sign math-t1
2421 (if (integerp math-t1)
2422 (* math-t1 math-try-solve-sign)
2423 (funcall math-t1 math-solve-lhs
2424 math-try-solve-sign)))))
2425 ((and (symbolp (car math-solve-lhs))
2426 (setq math-t1 (get (car math-solve-lhs) 'math-inverse-n))
2427 (setq math-t2 (funcall math-t1 math-solve-lhs math-solve-rhs)))
2429 ((setq math-t1 (math-expand-formula math-solve-lhs))
2430 (math-try-solve-for math-t1 math-solve-rhs math-try-solve-sign))
2432 (calc-record-why "*No inverse known" math-solve-lhs)
2436 (defun math-try-solve-prod ()
2437 (cond ((eq (car math-solve-lhs) '*)
2438 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2439 (math-try-solve-for (nth 2 math-solve-lhs)
2440 (math-div math-solve-rhs (nth 1 math-solve-lhs))
2441 (math-solve-sign math-try-solve-sign
2442 (nth 1 math-solve-lhs))))
2443 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2444 (math-try-solve-for (nth 1 math-solve-lhs)
2445 (math-div math-solve-rhs (nth 2 math-solve-lhs))
2446 (math-solve-sign math-try-solve-sign
2447 (nth 2 math-solve-lhs))))
2448 ((Math-zerop math-solve-rhs)
2449 (math-solve-prod (let ((math-solve-ranges math-solve-ranges))
2450 (math-try-solve-for (nth 2 math-solve-lhs) 0))
2451 (math-try-solve-for (nth 1 math-solve-lhs) 0)))))
2452 ((eq (car math-solve-lhs) '/)
2453 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2454 (math-try-solve-for (nth 2 math-solve-lhs)
2455 (math-div (nth 1 math-solve-lhs) math-solve-rhs)
2456 (math-solve-sign math-try-solve-sign
2457 (nth 1 math-solve-lhs))))
2458 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2459 (math-try-solve-for (nth 1 math-solve-lhs)
2460 (math-mul math-solve-rhs (nth 2 math-solve-lhs))
2461 (math-solve-sign math-try-solve-sign
2462 (nth 2 math-solve-lhs))))
2463 ((setq math-t1 (math-try-solve-for (math-sub (nth 1 math-solve-lhs)
2464 (math-mul (nth 2 math-solve-lhs)
2468 ((eq (car math-solve-lhs) '^)
2469 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2471 (nth 2 math-solve-lhs)
2472 (math-add (math-normalize
2473 (list 'calcFunc-log math-solve-rhs (nth 1 math-solve-lhs)))
2476 (math-mul '(var pi var-pi)
2480 (list 'calcFunc-ln (nth 1 math-solve-lhs)))))))
2481 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2482 (cond ((and (integerp (nth 2 math-solve-lhs))
2483 (>= (nth 2 math-solve-lhs) 2)
2484 (setq math-t1 (math-integer-log2 (nth 2 math-solve-lhs))))
2485 (setq math-t2 math-solve-rhs)
2486 (if (and (eq math-solve-full t)
2487 (math-known-realp (nth 1 math-solve-lhs)))
2489 (while (>= (setq math-t1 (1- math-t1)) 0)
2490 (setq math-t2 (list 'calcFunc-sqrt math-t2)))
2491 (setq math-t2 (math-solve-get-sign math-t2)))
2492 (while (>= (setq math-t1 (1- math-t1)) 0)
2493 (setq math-t2 (math-solve-get-sign
2495 (list 'calcFunc-sqrt math-t2))))))
2497 (nth 1 math-solve-lhs)
2498 (math-normalize math-t2)))
2499 ((math-looks-negp (nth 2 math-solve-lhs))
2501 (list '^ (nth 1 math-solve-lhs)
2502 (math-neg (nth 2 math-solve-lhs)))
2503 (math-div 1 math-solve-rhs)))
2504 ((and (eq math-solve-full t)
2505 (Math-integerp (nth 2 math-solve-lhs))
2506 (math-known-realp (nth 1 math-solve-lhs)))
2507 (setq math-t1 (math-normalize
2508 (list 'calcFunc-nroot math-solve-rhs
2509 (nth 2 math-solve-lhs))))
2510 (if (math-evenp (nth 2 math-solve-lhs))
2511 (setq math-t1 (math-solve-get-sign math-t1)))
2513 (nth 1 math-solve-lhs) math-t1
2514 (and math-try-solve-sign
2515 (math-oddp (nth 2 math-solve-lhs))
2516 (math-solve-sign math-try-solve-sign
2517 (nth 2 math-solve-lhs)))))
2518 (t (math-try-solve-for
2519 (nth 1 math-solve-lhs)
2523 (if (Math-realp (nth 2 math-solve-lhs))
2528 (and (integerp (nth 2 math-solve-lhs))
2530 (nth 2 math-solve-lhs)))))
2531 (math-div (nth 2 math-solve-lhs) 2))
2538 (and (integerp (nth 2 math-solve-lhs))
2540 (nth 2 math-solve-lhs))))))
2541 (nth 2 math-solve-lhs)))))
2543 (list 'calcFunc-nroot
2545 (nth 2 math-solve-lhs))))
2546 (and math-try-solve-sign
2547 (math-oddp (nth 2 math-solve-lhs))
2548 (math-solve-sign math-try-solve-sign
2549 (nth 2 math-solve-lhs)))))))))
2552 (defun math-solve-prod (lsoln rsoln)
2557 ((eq math-solve-full 'all)
2558 (cons 'vec (append (cdr lsoln) (cdr rsoln))))
2561 (list 'calcFunc-gt (math-solve-get-sign 1) 0)
2566 ;;; This deals with negative, fractional, and symbolic powers of "x".
2567 ;; The variable math-solve-b is local to math-decompose-poly,
2568 ;; but is used by math-solve-poly-funny-powers.
2569 (defvar math-solve-b)
2571 (defun math-solve-poly-funny-powers (sub-rhs) ; uses "t1", "t2"
2572 (setq math-t1 math-solve-lhs)
2573 (let ((pp math-poly-neg-powers)
2576 (setq fac (math-pow (car pp) (or math-poly-mult-powers 1))
2577 math-t1 (math-mul math-t1 fac)
2578 math-solve-rhs (math-mul math-solve-rhs fac)
2580 (if sub-rhs (setq math-t1 (math-sub math-t1 math-solve-rhs)))
2581 (let ((math-poly-neg-powers nil))
2582 (setq math-t2 (math-mul (or math-poly-mult-powers 1)
2583 (let ((calc-prefer-frac t))
2584 (math-div 1 math-poly-frac-powers)))
2585 math-t1 (math-is-polynomial
2586 (math-simplify (calcFunc-expand math-t1)) math-solve-b 50))))
2588 ;;; This converts "a x^8 + b x^5 + c x^2" to "(a (x^3)^2 + b (x^3) + c) * x^2".
2589 (defun math-solve-crunch-poly (max-degree) ; uses "t1", "t3"
2591 (while (and math-t1 (Math-zerop (car math-t1)))
2592 (setq math-t1 (cdr math-t1)
2595 (let* ((degree (1- (length math-t1)))
2597 (while (and (> scale 1) (= (car math-t3) 1))
2598 (and (= (% degree scale) 0)
2604 (if (= (% n scale) 0)
2605 (setq new-t1 (nconc new-t1 (list (car p))))
2606 (or (Math-zerop (car p))
2611 (setq math-t3 (cons scale (cdr math-t3))
2613 (setq scale (1- scale)))
2614 (setq math-t3 (list (math-mul (car math-t3) math-t2)
2615 (math-mul count math-t2)))
2616 (<= (1- (length math-t1)) max-degree)))))
2618 (defun calcFunc-poly (expr var &optional degree)
2620 (or (natnump degree) (math-reject-arg degree 'fixnatnump))
2622 (let ((p (math-is-polynomial expr var degree 'gen)))
2627 (math-reject-arg expr "Expected a polynomial"))))
2629 (defun calcFunc-gpoly (expr var &optional degree)
2631 (or (natnump degree) (math-reject-arg degree 'fixnatnump))
2633 (let* ((math-poly-base-variable var)
2634 (d (math-decompose-poly expr var degree nil)))
2637 (math-reject-arg expr "Expected a polynomial"))))
2639 (defun math-decompose-poly (math-solve-lhs math-solve-var degree sub-rhs)
2640 (let ((math-solve-rhs (or sub-rhs 1))
2641 math-t1 math-t2 math-t3)
2642 (setq math-t2 (math-polynomial-base
2645 (lambda (math-solve-b)
2646 (let ((math-poly-neg-powers '(1))
2647 (math-poly-mult-powers nil)
2648 (math-poly-frac-powers 1)
2649 (math-poly-exp-base t))
2650 (and (not (equal math-solve-b math-solve-lhs))
2651 (or (not (memq (car-safe math-solve-b) '(+ -))) sub-rhs)
2652 (setq math-t3 '(1 0) math-t2 1
2653 math-t1 (math-is-polynomial math-solve-lhs
2655 (if (and (equal math-poly-neg-powers '(1))
2656 (memq math-poly-mult-powers '(nil 1))
2657 (eq math-poly-frac-powers 1)
2659 (setq math-t1 (cons (math-sub (car math-t1) math-solve-rhs)
2661 (math-solve-poly-funny-powers sub-rhs))
2662 (math-solve-crunch-poly degree)
2663 (or (math-expr-contains math-solve-b math-solve-var)
2664 (math-expr-contains (car math-t3) math-solve-var))))))))
2666 (list (math-pow math-t2 (car math-t3))
2669 (math-pow math-t2 (nth 1 math-t3))
2670 (math-div (math-pow math-t2 (nth 1 math-t3)) math-solve-rhs))))))
2672 (defun math-solve-linear (var sign b a)
2673 (math-try-solve-for var
2674 (math-div (math-neg b) a)
2675 (math-solve-sign sign a)
2678 (defun math-solve-quadratic (var c b a)
2681 (if (math-looks-evenp b)
2682 (let ((halfb (math-div b 2)))
2686 (math-solve-get-sign
2688 (list 'calcFunc-sqrt
2689 (math-add (math-sqr halfb)
2690 (math-mul (math-neg c) a))))))
2695 (math-solve-get-sign
2697 (list 'calcFunc-sqrt
2698 (math-add (math-sqr b)
2699 (math-mul 4 (math-mul (math-neg c) a)))))))
2703 (defun math-solve-cubic (var d c b a)
2704 (let* ((p (math-div b a))
2708 (aa (math-sub q (math-div psqr 3)))
2710 (math-div (math-sub (math-mul 2 (math-mul psqr p))
2711 (math-mul 9 (math-mul p q)))
2715 (math-try-solve-for (math-pow (math-add var (math-div p 3)) 3)
2716 (math-neg bb) nil t)
2719 (math-mul (math-add var (math-div p 3))
2720 (math-add (math-sqr (math-add var (math-div p 3)))
2723 (setq m (math-mul 2 (list 'calcFunc-sqrt (math-div aa -3))))
2732 (math-sub (list 'calcFunc-arccos
2733 (math-div (math-mul 3 bb)
2737 (math-add 1 (math-solve-get-int
2740 calc-symbolic-mode))))
2745 (defun math-solve-quartic (var d c b a aa)
2746 (setq a (math-div a aa))
2747 (setq b (math-div b aa))
2748 (setq c (math-div c aa))
2749 (setq d (math-div d aa))
2752 (let* ((asqr (math-sqr a))
2753 (asqr4 (math-div asqr 4))
2754 (y (let ((math-solve-full nil)
2756 (math-solve-cubic math-solve-var
2758 (math-mul 4 (math-mul b d))
2761 (math-sub (math-mul a c)
2765 (rsqr (math-add (math-sub asqr4 b) y))
2766 (r (list 'calcFunc-sqrt rsqr))
2767 (sign1 (math-solve-get-sign 1))
2768 (de (list 'calcFunc-sqrt
2770 (math-sub (math-mul 3 asqr4)
2772 (if (Math-zerop rsqr)
2776 (list 'calcFunc-sqrt
2777 (math-sub (math-sqr y)
2783 (math-mul 4 (math-mul a b))
2789 (math-sub (math-add (math-mul sign1 (math-div r 2))
2790 (math-solve-get-sign (math-div de 2)))
2794 (defvar math-symbolic-solve nil)
2795 (defvar math-int-coefs nil)
2797 ;; The variable math-int-threshold is local to math-poly-all-roots,
2798 ;; but is used by math-poly-newton-root.
2799 (defvar math-int-threshold)
2800 ;; The variables math-int-scale, math-int-factors and math-double-roots
2801 ;; are local to math-poly-all-roots, but are used by math-poly-integer-root.
2802 (defvar math-int-scale)
2803 (defvar math-int-factors)
2804 (defvar math-double-roots)
2806 (defun math-poly-all-roots (var p &optional math-factoring)
2808 (let* ((math-symbolic-solve calc-symbolic-mode)
2810 (deg (1- (length p)))
2811 (orig-p (reverse p))
2812 (math-int-coefs nil)
2813 (math-int-scale nil)
2814 (math-double-roots nil)
2815 (math-int-factors nil)
2816 (math-int-threshold nil)
2818 ;; If rational coefficients, look for exact rational factors.
2819 (while (and pp (Math-ratp (car pp)))
2822 (if (or math-factoring math-symbolic-solve)
2824 (let ((lead (car orig-p))
2825 (calc-prefer-frac t)
2826 (scale (apply 'math-lcm-denoms p)))
2827 (setq math-int-scale (math-abs (math-mul scale lead))
2828 math-int-threshold (math-div '(float 5 -2) math-int-scale)
2829 math-int-coefs (cdr (math-div (cons 'vec orig-p) lead)))))
2831 (let ((calc-prefer-frac nil)
2832 (calc-symbolic-mode nil)
2834 (def-p (copy-sequence orig-p)))
2836 (if (Math-numberp (car pp))
2839 (while (> deg (if math-symbolic-solve 2 4))
2840 (let* ((x (math-poly-any-root def-p '(float 0 0) nil))
2842 (if (and (eq (car-safe x) 'cplx)
2843 (math-nearly-zerop (nth 2 x) (nth 1 x)))
2844 (setq x (calcFunc-re x)))
2846 (setq roots (cons x roots)))
2847 (or (math-numberp x)
2848 (setq x (math-evaluate-expr x)))
2851 (while (setq pp (cdr pp))
2854 (setq b (math-add (math-mul x b) c)))
2855 (setq def-p (cdr def-p)
2857 (setq p (reverse def-p))))
2859 (let ((math-solve-var '(var DUMMY var-DUMMY))
2860 (math-solve-sign nil)
2861 (math-solve-ranges nil)
2862 (math-solve-full 'all))
2863 (if (= (length p) (length math-int-coefs))
2864 (setq p (reverse math-int-coefs)))
2865 (setq roots (append (cdr (apply (cond ((= deg 2)
2866 'math-solve-quadratic)
2870 'math-solve-quartic))
2874 (setq roots (cons (math-div (math-neg (car p)) (nth 1 p))
2879 (math-poly-integer-root (car roots))
2880 (setq roots (cdr roots)))
2881 (list math-int-factors (nreverse math-int-coefs) math-int-scale))
2882 (let ((vec nil) res)
2884 (let ((root (car roots))
2885 (math-solve-full (and math-solve-full 'all)))
2886 (if (math-floatp root)
2887 (setq root (math-poly-any-root orig-p root t)))
2888 (setq vec (append vec
2889 (cdr (or (math-try-solve-for var root nil t)
2890 (throw 'ouch nil))))))
2891 (setq roots (cdr roots)))
2892 (setq vec (cons 'vec (nreverse vec)))
2893 (if math-symbolic-solve
2894 (setq vec (math-normalize vec)))
2895 (if (eq math-solve-full t)
2896 (list 'calcFunc-subscr
2898 (math-solve-get-int 1 (1- (length orig-p)) 1))
2901 (defun math-lcm-denoms (&rest fracs)
2904 (if (eq (car-safe (car fracs)) 'frac)
2905 (setq den (calcFunc-lcm den (nth 2 (car fracs)))))
2906 (setq fracs (cdr fracs)))
2909 (defun math-poly-any-root (p x polish) ; p is a reverse poly coeff list
2910 (let* ((newt (if (math-zerop x)
2911 (math-poly-newton-root
2912 p '(cplx (float 123 -6) (float 1 -4)) 4)
2913 (math-poly-newton-root p x 4)))
2914 (res (if (math-zerop (cdr newt))
2916 (if (and (math-lessp (cdr newt) '(float 1 -3)) (not polish))
2917 (setq newt (math-poly-newton-root p (car newt) 30)))
2918 (if (math-zerop (cdr newt))
2920 (math-poly-laguerre-root p x polish)))))
2921 (and math-symbolic-solve (math-floatp res)
2925 (defun math-poly-newton-root (p x iters)
2926 (let* ((calc-prefer-frac nil)
2927 (calc-symbolic-mode nil)
2928 (try-integer math-int-coefs)
2930 (while (and (> (setq iters (1- iters)) 0)
2932 (math-working "newton" x)
2935 (while (setq pp (cdr pp))
2936 (setq d (math-add (math-mul x d) b)
2937 b (math-add (math-mul x b) (car pp))))
2938 (not (math-zerop d)))
2940 (setq dx (math-div b d)
2943 (let ((adx (math-abs-approx dx)))
2944 (and (math-lessp adx math-int-threshold)
2945 (let ((iroot (math-poly-integer-root x)))
2948 (setq try-integer nil))))))
2949 (or (not (or (eq dx 0)
2950 (math-nearly-zerop dx (math-abs-approx x))))
2951 (progn (setq dx 0) nil)))))
2952 (cons x (if (math-zerop x)
2953 1 (math-div (math-abs-approx dx) (math-abs-approx x))))))
2955 (defun math-poly-integer-root (x)
2956 (and (math-lessp (calcFunc-xpon (math-abs-approx x)) calc-internal-prec)
2958 (let* ((calc-prefer-frac t)
2959 (xre (calcFunc-re x))
2960 (xim (calcFunc-im x))
2961 (xresq (math-sqr xre))
2962 (ximsq (math-sqr xim)))
2963 (if (math-lessp ximsq (calcFunc-scf xresq -1))
2964 ;; Look for linear factor
2965 (let* ((rnd (math-div (math-round (math-mul xre math-int-scale))
2967 (icp math-int-coefs)
2970 (while (setq icp (cdr icp))
2971 (setq newcoef (cons rem newcoef)
2972 rem (math-add (car icp)
2973 (math-mul rem rnd))))
2974 (and (math-zerop rem)
2976 (setq math-int-coefs (nreverse newcoef)
2977 math-int-factors (cons (list (math-neg rnd))
2980 ;; Look for irreducible quadratic factor
2981 (let* ((rnd1 (math-div (math-round
2982 (math-mul xre (math-mul -2 math-int-scale)))
2984 (sqscale (math-sqr math-int-scale))
2985 (rnd0 (math-div (math-round (math-mul (math-add xresq ximsq)
2988 (rem1 (car math-int-coefs))
2989 (icp (cdr math-int-coefs))
2992 (found (assoc (list rnd0 rnd1 (math-posp xim))
2996 (setq math-double-roots (delq found math-double-roots)
2998 (while (setq icp (cdr icp))
3000 newcoef (cons rem1 newcoef)
3001 rem1 (math-sub rem0 (math-mul this rnd1))
3002 rem0 (math-sub (car icp) (math-mul this rnd0)))))
3003 (and (math-zerop rem0)
3005 (let ((aa (math-div rnd1 -2)))
3006 (or found (setq math-int-coefs (reverse newcoef)
3007 math-double-roots (cons (list
3012 math-int-factors (cons (cons rnd0 rnd1)
3015 (let ((calc-symbolic-mode math-symbolic-solve))
3016 (math-mul (math-sqrt (math-sub (math-sqr aa)
3018 (if (math-negp xim) -1 1)))))))))))
3020 ;;; The following routine is from Numerical Recipes, section 9.5.
3021 (defun math-poly-laguerre-root (p x polish)
3022 (let* ((calc-prefer-frac nil)
3023 (calc-symbolic-mode nil)
3026 (try-newt (not polish))
3030 (and (or (< (setq iters (1+ iters)) 50)
3031 (math-reject-arg x "*Laguerre's method failed to converge"))
3032 (let ((err (math-abs-approx (car p)))
3033 (abx (math-abs-approx x))
3037 (while (setq pp (cdr pp))
3038 (setq f (math-add (math-mul x f) d)
3039 d (math-add (math-mul x d) b)
3040 b (math-add (math-mul x b) (car pp))
3041 err (math-add (math-abs-approx b) (math-mul abx err))))
3042 (math-lessp (calcFunc-scf err (- -2 calc-internal-prec))
3043 (math-abs-approx b)))
3044 (or (not (math-zerop d))
3045 (not (math-zerop f))
3047 (setq x (math-pow (math-neg b) (list 'frac 1 m)))
3049 (let* ((g (math-div d b))
3051 (h (math-sub g2 (math-mul 2 (math-div f b))))
3053 (math-mul (1- m) (math-sub (math-mul m h) g2))))
3054 (gp (math-add g sq))
3055 (gm (math-sub g sq)))
3056 (if (math-lessp (calcFunc-abssqr gp) (calcFunc-abssqr gm))
3058 (setq dx (math-div m gp)
3061 (math-lessp (math-abs-approx dx)
3062 (calcFunc-scf (math-abs-approx x) -3)))
3063 (let ((newt (math-poly-newton-root p x1 7)))
3066 (if (math-zerop (cdr newt))
3067 (setq x (car newt) x1 x)
3068 (if (math-lessp (cdr newt) '(float 1 -6))
3069 (let ((newt2 (math-poly-newton-root
3071 (if (math-zerop (cdr newt2))
3072 (setq x (car newt2) x1 x)
3073 (setq x (car newt))))))))
3075 (math-nearly-equal x x1))))
3076 (let ((cdx (math-abs-approx dx)))
3081 (math-lessp cdx dxold)
3084 (let ((digs (calcFunc-xpon
3085 (math-div (math-abs-approx x) cdx))))
3087 "*Could not attain full precision")
3089 (let ((calc-internal-prec (max 3 digs)))
3090 (setq x (math-normalize x))))))
3094 (math-lessp (calcFunc-scf (math-abs-approx x)
3095 (- calc-internal-prec))
3097 (or (and (math-floatp x)
3098 (math-poly-integer-root x))
3101 (defun math-solve-above-dummy (x)
3102 (and (not (Math-primp x))
3103 (if (and (equal (nth 1 x) '(var SOLVEDUM SOLVEDUM))
3107 (while (and (setq x (cdr x))
3108 (not (setq res (math-solve-above-dummy (car x))))))
3111 (defun math-solve-find-root-term (x neg) ; sets "t2", "t3"
3112 (if (math-solve-find-root-in-prod x)
3115 (and (memq (car-safe x) '(+ -))
3116 (or (math-solve-find-root-term (nth 1 x) neg)
3117 (math-solve-find-root-term (nth 2 x)
3118 (if (eq (car x) '-) (not neg) neg))))))
3120 (defun math-solve-find-root-in-prod (x)
3122 (math-expr-contains x math-solve-var)
3123 (or (and (eq (car x) 'calcFunc-sqrt)
3125 (and (eq (car x) '^)
3126 (or (and (memq (math-quarter-integer (nth 2 x)) '(1 2 3))
3128 (and (eq (car-safe (nth 2 x)) 'frac)
3129 (eq (nth 2 (nth 2 x)) 3)
3131 (and (memq (car x) '(* /))
3132 (or (and (not (math-expr-contains (nth 1 x) math-solve-var))
3133 (math-solve-find-root-in-prod (nth 2 x)))
3134 (and (not (math-expr-contains (nth 2 x) math-solve-var))
3135 (math-solve-find-root-in-prod (nth 1 x))))))))
3137 ;; The variable math-solve-vars is local to math-solve-system,
3138 ;; but is used by math-solve-system-rec.
3139 (defvar math-solve-vars)
3141 ;; The variable math-solve-simplifying is local to math-solve-system
3142 ;; and math-solve-system-rec, but is used by math-solve-system-subst.
3143 (defvar math-solve-simplifying)
3145 (defun math-solve-system (exprs math-solve-vars math-solve-full)
3146 (setq exprs (mapcar 'list (if (Math-vectorp exprs)
3149 math-solve-vars (if (Math-vectorp math-solve-vars)
3150 (cdr math-solve-vars)
3151 (list math-solve-vars)))
3152 (or (let ((math-solve-simplifying nil))
3153 (math-solve-system-rec exprs math-solve-vars nil))
3154 (let ((math-solve-simplifying t))
3155 (math-solve-system-rec exprs math-solve-vars nil))))
3157 ;;; The following backtracking solver works by choosing a variable
3158 ;;; and equation, and trying to solve the equation for the variable.
3159 ;;; If it succeeds it calls itself recursively with that variable and
3160 ;;; equation removed from their respective lists, and with the solution
3161 ;;; added to solns as well as being substituted into all existing
3162 ;;; equations. The algorithm terminates when any solution path
3163 ;;; manages to remove all the variables from var-list.
3165 ;;; To support calcFunc-roots, entries in eqn-list and solns are
3166 ;;; actually lists of equations.
3168 ;; The variables math-solve-system-res and math-solve-system-vv are
3169 ;; local to math-solve-system-rec, but are used by math-solve-system-subst.
3170 (defvar math-solve-system-vv)
3171 (defvar math-solve-system-res)
3174 (defun math-solve-system-rec (eqn-list var-list solns)
3177 (math-solve-system-res nil))
3179 ;; Try each variable in turn.
3183 (let* ((math-solve-system-vv (car v))
3185 (elim (eq (car-safe math-solve-system-vv) 'calcFunc-elim)))
3187 (setq math-solve-system-vv (nth 1 math-solve-system-vv)))
3189 ;; Try each equation in turn.
3196 (setq math-solve-system-res nil)
3198 ;; Try to solve for math-solve-system-vv the list of equations e2.
3200 (setq res2 (or (and (eq (car e2) eprev)
3202 (math-solve-for (car e2) 0
3203 math-solve-system-vv
3205 (setq eprev (car e2)
3206 math-solve-system-res (cons (if (eq math-solve-full 'all)
3209 math-solve-system-res)
3212 (setq math-solve-system-res nil)
3214 ;; Found a solution. Now try other variables.
3215 (setq math-solve-system-res (nreverse math-solve-system-res)
3216 math-solve-system-res (math-solve-system-rec
3218 'math-solve-system-subst
3220 (copy-sequence eqn-list)))
3221 (delq (car v) (copy-sequence var-list))
3222 (let ((math-solve-simplifying nil)
3228 (math-solve-system-subst
3234 math-solve-system-vv
3235 (apply 'append math-solve-system-res))
3237 (not math-solve-system-res))))
3239 (not math-solve-system-res)))
3241 math-solve-system-res)
3243 ;; Eliminated all variables, so now put solution into the proper format.
3244 (setq solns (sort solns
3247 (not (memq (car x) (memq (car y) math-solve-vars)))))))
3248 (if (eq math-solve-full 'all)
3253 (mapcar (function (lambda (x) (cons 'vec (cdr x)))) solns)
3254 (mapcar (function (lambda (x) (cons 'vec x))) eqn-list)))))
3258 (mapcar (function (lambda (x) (cons 'calcFunc-eq x))) solns)
3259 (mapcar 'car eqn-list)))))))
3261 (defun math-solve-system-subst (x) ; uses "res" and "v"
3263 (res2 math-solve-system-res))
3265 (setq accum (nconc accum
3268 (if math-solve-simplifying
3271 (car x) math-solve-system-vv r))
3273 (car x) math-solve-system-vv r))))
3280 ;; calc-command-flags is declared in calc.el
3281 (defvar calc-command-flags)
3283 (defun math-get-from-counter (name)
3284 (let ((ctr (assq name calc-command-flags)))
3286 (setcdr ctr (1+ (cdr ctr)))
3287 (setq ctr (cons name 1)
3288 calc-command-flags (cons ctr calc-command-flags)))
3291 (defvar var-GenCount)
3293 (defun math-solve-get-sign (val)
3294 (setq val (math-simplify val))
3295 (if (and (eq (car-safe val) '*)
3296 (Math-numberp (nth 1 val)))
3297 (list '* (nth 1 val) (math-solve-get-sign (nth 2 val)))
3298 (and (eq (car-safe val) 'calcFunc-sqrt)
3299 (eq (car-safe (nth 1 val)) '^)
3300 (setq val (math-normalize (list '^
3302 (math-div (nth 2 (nth 1 val)) 2)))))
3304 (if (and (calc-var-value 'var-GenCount)
3305 (Math-natnump var-GenCount)
3306 (not (eq math-solve-full 'all)))
3308 (math-mul (list 'calcFunc-as var-GenCount) val)
3309 (setq var-GenCount (math-add var-GenCount 1))
3310 (calc-refresh-evaltos 'var-GenCount))
3311 (let* ((var (concat "s" (int-to-string (math-get-from-counter 'solve-sign))))
3312 (var2 (list 'var (intern var) (intern (concat "var-" var)))))
3313 (if (eq math-solve-full 'all)
3314 (setq math-solve-ranges (cons (list var2 1 -1)
3315 math-solve-ranges)))
3316 (math-mul var2 val)))
3317 (calc-record-why "*Choosing positive solution")
3320 (defun math-solve-get-int (val &optional range first)
3322 (if (and (calc-var-value 'var-GenCount)
3323 (Math-natnump var-GenCount)
3324 (not (eq math-solve-full 'all)))
3326 (math-mul val (list 'calcFunc-an var-GenCount))
3327 (setq var-GenCount (math-add var-GenCount 1))
3328 (calc-refresh-evaltos 'var-GenCount))
3329 (let* ((var (concat "n" (int-to-string
3330 (math-get-from-counter 'solve-int))))
3331 (var2 (list 'var (intern var) (intern (concat "var-" var)))))
3332 (if (and range (eq math-solve-full 'all))
3333 (setq math-solve-ranges (cons (cons var2
3334 (cdr (calcFunc-index
3335 range (or first 0))))
3336 math-solve-ranges)))
3337 (math-mul val var2)))
3338 (calc-record-why "*Choosing 0 for arbitrary integer in solution")
3341 (defun math-solve-sign (sign expr)
3343 (let ((s1 (math-possible-signs expr)))
3344 (cond ((memq s1 '(4 6))
3349 (defun math-looks-evenp (expr)
3350 (if (Math-integerp expr)
3352 (if (memq (car expr) '(* /))
3353 (math-looks-evenp (nth 1 expr)))))
3355 (defun math-solve-for (lhs rhs math-solve-var math-solve-full &optional sign)
3356 (if (math-expr-contains rhs math-solve-var)
3357 (math-solve-for (math-sub lhs rhs) 0 math-solve-var math-solve-full)
3358 (and (math-expr-contains lhs math-solve-var)
3359 (math-with-extra-prec 1
3360 (let* ((math-poly-base-variable math-solve-var)
3361 (res (math-try-solve-for lhs rhs sign)))
3362 (if (and (eq math-solve-full 'all)
3363 (math-known-realp math-solve-var))
3364 (let ((old-len (length res))
3369 (and (not (memq (car-safe x)
3373 new-len (length res))
3374 (if (< new-len old-len)
3375 (calc-record-why (if (= new-len 1)
3376 "*All solutions were complex"
3378 "*Omitted %d complex solutions"
3379 (- old-len new-len)))))))
3382 (defun math-solve-eqn (expr var full)
3383 (if (memq (car-safe expr) '(calcFunc-neq calcFunc-lt calcFunc-gt
3384 calcFunc-leq calcFunc-geq))
3385 (let ((res (math-solve-for (cons '- (cdr expr))
3387 (if (eq (car expr) 'calcFunc-neq) nil 1))))
3389 (if (eq math-solve-sign 1)
3390 (list (car expr) var res)
3391 (if (eq math-solve-sign -1)
3392 (list (car expr) res var)
3393 (or (eq (car expr) 'calcFunc-neq)
3395 "*Can't determine direction of inequality"))
3396 (and (memq (car expr) '(calcFunc-neq calcFunc-lt calcFunc-gt))
3397 (list 'calcFunc-neq var res))))))
3398 (let ((res (math-solve-for expr 0 var full)))
3400 (list 'calcFunc-eq var res)))))
3402 (defun math-reject-solution (expr var func)
3403 (if (math-expr-contains expr var)
3404 (or (equal (car calc-next-why) '(* "Unable to find a symbolic solution"))
3405 (calc-record-why "*Unable to find a solution")))
3406 (list func expr var))
3408 (defun calcFunc-solve (expr var)
3409 (or (if (or (Math-vectorp expr) (Math-vectorp var))
3410 (math-solve-system expr var nil)
3411 (math-solve-eqn expr var nil))
3412 (math-reject-solution expr var 'calcFunc-solve)))
3414 (defun calcFunc-fsolve (expr var)
3415 (or (if (or (Math-vectorp expr) (Math-vectorp var))
3416 (math-solve-system expr var t)
3417 (math-solve-eqn expr var t))
3418 (math-reject-solution expr var 'calcFunc-fsolve)))
3420 (defun calcFunc-roots (expr var)
3421 (let ((math-solve-ranges nil))
3422 (or (if (or (Math-vectorp expr) (Math-vectorp var))
3423 (math-solve-system expr var 'all)
3424 (math-solve-for expr 0 var 'all))
3425 (math-reject-solution expr var 'calcFunc-roots))))
3427 (defun calcFunc-finv (expr var)
3428 (let ((res (math-solve-for expr math-integ-var var nil)))
3430 (math-normalize (math-expr-subst res math-integ-var var))
3431 (math-reject-solution expr var 'calcFunc-finv))))
3433 (defun calcFunc-ffinv (expr var)
3434 (let ((res (math-solve-for expr math-integ-var var t)))
3436 (math-normalize (math-expr-subst res math-integ-var var))
3437 (math-reject-solution expr var 'calcFunc-finv))))
3440 (put 'calcFunc-inv 'math-inverse
3441 (function (lambda (x) (math-div 1 x))))
3442 (put 'calcFunc-inv 'math-inverse-sign -1)
3444 (put 'calcFunc-sqrt 'math-inverse
3445 (function (lambda (x) (math-sqr x))))
3447 (put 'calcFunc-conj 'math-inverse
3448 (function (lambda (x) (list 'calcFunc-conj x))))
3450 (put 'calcFunc-abs 'math-inverse
3451 (function (lambda (x) (math-solve-get-sign x))))
3453 (put 'calcFunc-deg 'math-inverse
3454 (function (lambda (x) (list 'calcFunc-rad x))))
3455 (put 'calcFunc-deg 'math-inverse-sign 1)
3457 (put 'calcFunc-rad 'math-inverse
3458 (function (lambda (x) (list 'calcFunc-deg x))))
3459 (put 'calcFunc-rad 'math-inverse-sign 1)
3461 (put 'calcFunc-ln 'math-inverse
3462 (function (lambda (x) (list 'calcFunc-exp x))))
3463 (put 'calcFunc-ln 'math-inverse-sign 1)
3465 (put 'calcFunc-log10 'math-inverse
3466 (function (lambda (x) (list 'calcFunc-exp10 x))))
3467 (put 'calcFunc-log10 'math-inverse-sign 1)
3469 (put 'calcFunc-lnp1 'math-inverse
3470 (function (lambda (x) (list 'calcFunc-expm1 x))))
3471 (put 'calcFunc-lnp1 'math-inverse-sign 1)
3473 (put 'calcFunc-exp 'math-inverse
3474 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-ln x))
3476 (math-mul '(var pi var-pi)
3478 '(var i var-i))))))))
3479 (put 'calcFunc-exp 'math-inverse-sign 1)
3481 (put 'calcFunc-expm1 'math-inverse
3482 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-lnp1 x))
3484 (math-mul '(var pi var-pi)
3486 '(var i var-i))))))))
3487 (put 'calcFunc-expm1 'math-inverse-sign 1)
3489 (put 'calcFunc-sin 'math-inverse
3490 (function (lambda (x) (let ((n (math-solve-get-int 1)))
3491 (math-add (math-mul (math-normalize
3492 (list 'calcFunc-arcsin x))
3494 (math-mul (math-half-circle t)
3497 (put 'calcFunc-cos 'math-inverse
3498 (function (lambda (x) (math-add (math-solve-get-sign
3500 (list 'calcFunc-arccos x)))
3502 (math-full-circle t))))))
3504 (put 'calcFunc-tan 'math-inverse
3505 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-arctan x))
3507 (math-half-circle t))))))
3509 (put 'calcFunc-arcsin 'math-inverse
3510 (function (lambda (x) (math-normalize (list 'calcFunc-sin x)))))
3512 (put 'calcFunc-arccos 'math-inverse
3513 (function (lambda (x) (math-normalize (list 'calcFunc-cos x)))))
3515 (put 'calcFunc-arctan 'math-inverse
3516 (function (lambda (x) (math-normalize (list 'calcFunc-tan x)))))
3518 (put 'calcFunc-sinh 'math-inverse
3519 (function (lambda (x) (let ((n (math-solve-get-int 1)))
3520 (math-add (math-mul (math-normalize
3521 (list 'calcFunc-arcsinh x))
3523 (math-mul (math-half-circle t)
3527 (put 'calcFunc-sinh 'math-inverse-sign 1)
3529 (put 'calcFunc-cosh 'math-inverse
3530 (function (lambda (x) (math-add (math-solve-get-sign
3532 (list 'calcFunc-arccosh x)))
3533 (math-mul (math-full-circle t)
3535 '(var i var-i)))))))
3537 (put 'calcFunc-tanh 'math-inverse
3538 (function (lambda (x) (math-add (math-normalize
3539 (list 'calcFunc-arctanh x))
3540 (math-mul (math-half-circle t)
3542 '(var i var-i)))))))
3543 (put 'calcFunc-tanh 'math-inverse-sign 1)
3545 (put 'calcFunc-arcsinh 'math-inverse
3546 (function (lambda (x) (math-normalize (list 'calcFunc-sinh x)))))
3547 (put 'calcFunc-arcsinh 'math-inverse-sign 1)
3549 (put 'calcFunc-arccosh 'math-inverse
3550 (function (lambda (x) (math-normalize (list 'calcFunc-cosh x)))))
3552 (put 'calcFunc-arctanh 'math-inverse
3553 (function (lambda (x) (math-normalize (list 'calcFunc-tanh x)))))
3554 (put 'calcFunc-arctanh 'math-inverse-sign 1)
3558 (defun calcFunc-taylor (expr var num)
3559 (let ((x0 0) (v var))
3560 (if (memq (car-safe var) '(+ - calcFunc-eq))
3561 (setq x0 (if (eq (car var) '+) (math-neg (nth 2 var)) (nth 2 var))
3563 (or (and (eq (car-safe v) 'var)
3564 (math-expr-contains expr v)
3566 (let ((accum (math-expr-subst expr v x0))
3567 (var2 (if (eq (car var) 'calcFunc-eq)
3573 (while (and (<= (setq n (1+ n)) num)
3574 (setq fprime (calcFunc-deriv fprime v nil t)))
3575 (setq fprime (math-simplify fprime)
3576 nfac (math-mul nfac n)
3577 accum (math-add accum
3578 (math-div (math-mul (math-pow var2 n)
3583 (math-normalize accum))))
3584 (list 'calcFunc-taylor expr var num))))
3586 ;;; arch-tag: f2932ec8-dd63-418b-a542-11a644b9d4c4
3587 ;;; calcalg2.el ends here