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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-alg-2 () nil)
38 (defun calc-derivative (var num)
39 (interactive "sDifferentiate with respect to: \np")
42 (error "Order of derivative must be positive"))
43 (let ((func (if (calc-is-hyperbolic) 'calcFunc-tderiv 'calcFunc-deriv))
45 (if (or (equal var "") (equal var "$"))
49 (setq var (math-read-expr var))
50 (when (eq (car-safe var) 'error)
51 (error "Bad format in expression: %s" (nth 1 var)))
54 (while (>= (setq num (1- num)) 0)
55 (setq expr (list func expr var)))
56 (calc-enter-result n "derv" expr))))
58 (defun calc-integral (var)
59 (interactive "sIntegration variable: ")
61 (if (or (equal var "") (equal var "$"))
62 (calc-enter-result 2 "intg" (list 'calcFunc-integ
65 (let ((var (math-read-expr var)))
66 (if (eq (car-safe var) 'error)
67 (error "Bad format in expression: %s" (nth 1 var)))
68 (calc-enter-result 1 "intg" (list 'calcFunc-integ
72 (defun calc-num-integral (&optional varname lowname highname)
73 (interactive "sIntegration variable: ")
74 (calc-tabular-command 'calcFunc-ninteg "Integration" "nint"
75 nil varname lowname highname))
77 (defun calc-summation (arg &optional varname lowname highname)
78 (interactive "P\nsSummation variable: ")
79 (calc-tabular-command 'calcFunc-sum "Summation" "sum"
80 arg varname lowname highname))
82 (defun calc-alt-summation (arg &optional varname lowname highname)
83 (interactive "P\nsSummation variable: ")
84 (calc-tabular-command 'calcFunc-asum "Summation" "asum"
85 arg varname lowname highname))
87 (defun calc-product (arg &optional varname lowname highname)
88 (interactive "P\nsIndex variable: ")
89 (calc-tabular-command 'calcFunc-prod "Index" "prod"
90 arg varname lowname highname))
92 (defun calc-tabulate (arg &optional varname lowname highname)
93 (interactive "P\nsIndex variable: ")
94 (calc-tabular-command 'calcFunc-table "Index" "tabl"
95 arg varname lowname highname))
97 (defun calc-tabular-command (func prompt prefix arg varname lowname highname)
99 (let (var (low nil) (high nil) (step nil) stepname stepnum (num 1) expr)
103 (if (or (equal varname "") (equal varname "$") (null varname))
104 (setq high (calc-top-n (+ stepnum 1))
105 low (calc-top-n (+ stepnum 2))
106 var (calc-top-n (+ stepnum 3))
108 (setq var (if (stringp varname) (math-read-expr varname) varname))
109 (if (eq (car-safe var) 'error)
110 (error "Bad format in expression: %s" (nth 1 var)))
112 (setq lowname (read-string (concat prompt " variable: " varname
114 (if (or (equal lowname "") (equal lowname "$"))
115 (setq high (calc-top-n (+ stepnum 1))
116 low (calc-top-n (+ stepnum 2))
118 (setq low (if (stringp lowname) (math-read-expr lowname) lowname))
119 (if (eq (car-safe low) 'error)
120 (error "Bad format in expression: %s" (nth 1 low)))
122 (setq highname (read-string (concat prompt " variable: " varname
125 (if (or (equal highname "") (equal highname "$"))
126 (setq high (calc-top-n (+ stepnum 1))
128 (setq high (if (stringp highname) (math-read-expr highname)
130 (if (eq (car-safe high) 'error)
131 (error "Bad format in expression: %s" (nth 1 high)))
134 (setq stepname (read-string (concat prompt " variable: "
139 (if (or (equal stepname "") (equal stepname "$"))
140 (setq step (calc-top-n 1)
142 (setq step (math-read-expr stepname))
143 (if (eq (car-safe step) 'error)
144 (error "Bad format in expression: %s"
148 (setq step (calc-top-n 1))
150 (setq step (prefix-numeric-value arg)))))
151 (setq expr (calc-top-n num))
152 (calc-enter-result num prefix (append (list func expr var low high)
153 (and step (list step)))))))
155 (defun calc-solve-for (var)
156 (interactive "sVariable to solve for: ")
158 (let ((func (if (calc-is-inverse)
159 (if (calc-is-hyperbolic) 'calcFunc-ffinv 'calcFunc-finv)
160 (if (calc-is-hyperbolic) 'calcFunc-fsolve 'calcFunc-solve))))
161 (if (or (equal var "") (equal var "$"))
162 (calc-enter-result 2 "solv" (list func
165 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
166 (not (string-match "\\[" var)))
167 (math-read-expr (concat "[" var "]"))
168 (math-read-expr var))))
169 (if (eq (car-safe var) 'error)
170 (error "Bad format in expression: %s" (nth 1 var)))
171 (calc-enter-result 1 "solv" (list func
175 (defun calc-poly-roots (var)
176 (interactive "sVariable to solve for: ")
178 (if (or (equal var "") (equal var "$"))
179 (calc-enter-result 2 "prts" (list 'calcFunc-roots
182 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
183 (not (string-match "\\[" var)))
184 (math-read-expr (concat "[" var "]"))
185 (math-read-expr var))))
186 (if (eq (car-safe var) 'error)
187 (error "Bad format in expression: %s" (nth 1 var)))
188 (calc-enter-result 1 "prts" (list 'calcFunc-roots
192 (defun calc-taylor (var nterms)
193 (interactive "sTaylor expansion variable: \nNNumber of terms: ")
195 (let ((var (math-read-expr var)))
196 (if (eq (car-safe var) 'error)
197 (error "Bad format in expression: %s" (nth 1 var)))
198 (calc-enter-result 1 "tylr" (list 'calcFunc-taylor
201 (prefix-numeric-value nterms))))))
204 ;; The following are global variables used by math-derivative and some
206 (defvar math-deriv-var)
207 (defvar math-deriv-total)
208 (defvar math-deriv-symb)
210 (defun math-derivative (expr)
211 (cond ((equal expr math-deriv-var)
213 ((or (Math-scalarp expr)
214 (eq (car expr) 'sdev)
215 (and (eq (car expr) 'var)
216 (or (not math-deriv-total)
217 (math-const-var expr)
219 (math-setup-declarations)
220 (memq 'const (nth 1 (or (assq (nth 2 expr)
222 math-decls-all)))))))
225 (math-add (math-derivative (nth 1 expr))
226 (math-derivative (nth 2 expr))))
228 (math-sub (math-derivative (nth 1 expr))
229 (math-derivative (nth 2 expr))))
230 ((memq (car expr) '(calcFunc-eq calcFunc-neq calcFunc-lt
231 calcFunc-gt calcFunc-leq calcFunc-geq))
233 (math-derivative (nth 1 expr))
234 (math-derivative (nth 2 expr))))
235 ((eq (car expr) 'neg)
236 (math-neg (math-derivative (nth 1 expr))))
238 (math-add (math-mul (nth 2 expr)
239 (math-derivative (nth 1 expr)))
240 (math-mul (nth 1 expr)
241 (math-derivative (nth 2 expr)))))
243 (math-sub (math-div (math-derivative (nth 1 expr))
245 (math-div (math-mul (nth 1 expr)
246 (math-derivative (nth 2 expr)))
247 (math-sqr (nth 2 expr)))))
249 (let ((du (math-derivative (nth 1 expr)))
250 (dv (math-derivative (nth 2 expr))))
252 (setq du (math-mul (nth 2 expr)
253 (math-mul (math-normalize
256 (math-add (nth 2 expr) -1)))
259 (setq dv (math-mul (math-normalize
260 (list 'calcFunc-ln (nth 1 expr)))
261 (math-mul expr dv))))
264 (math-derivative (nth 1 expr))) ; a reasonable definition
265 ((eq (car expr) 'vec)
266 (math-map-vec 'math-derivative expr))
267 ((and (memq (car expr) '(calcFunc-conj calcFunc-re calcFunc-im))
269 (list (car expr) (math-derivative (nth 1 expr))))
270 ((and (memq (car expr) '(calcFunc-subscr calcFunc-mrow calcFunc-mcol))
272 (let ((d (math-derivative (nth 1 expr))))
274 0 ; assume x and x_1 are independent vars
275 (list (car expr) d (nth 2 expr)))))
276 (t (or (and (symbolp (car expr))
277 (if (= (length expr) 2)
278 (let ((handler (get (car expr) 'math-derivative)))
280 (let ((deriv (math-derivative (nth 1 expr))))
281 (if (Math-zerop deriv)
283 (math-mul (funcall handler (nth 1 expr))
285 (let ((handler (get (car expr) 'math-derivative-n)))
287 (funcall handler expr)))))
288 (and (not (eq math-deriv-symb 'pre-expand))
289 (let ((exp (math-expand-formula expr)))
291 (or (let ((math-deriv-symb 'pre-expand))
292 (catch 'math-deriv (math-derivative expr)))
293 (math-derivative exp)))))
294 (if (or (Math-objvecp expr)
296 (not (symbolp (car expr))))
298 (throw 'math-deriv nil)
299 (list (if math-deriv-total 'calcFunc-tderiv 'calcFunc-deriv)
306 (while (setq arg (cdr arg))
307 (or (Math-zerop (setq derv (math-derivative (car arg))))
308 (let ((func (intern (concat (symbol-name (car expr))
313 (prop (cond ((= (length expr) 2)
322 'math-derivative-5))))
328 (let ((handler (get func prop)))
329 (or (and prop handler
330 (apply handler (cdr expr)))
331 (if (and math-deriv-symb
334 (throw 'math-deriv nil)
335 (cons func (cdr expr))))))))))
339 (defun calcFunc-deriv (expr math-deriv-var &optional deriv-value math-deriv-symb)
340 (let* ((math-deriv-total nil)
341 (res (catch 'math-deriv (math-derivative expr))))
342 (or (eq (car-safe res) 'calcFunc-deriv)
344 (setq res (math-normalize res)))
347 (math-expr-subst res math-deriv-var deriv-value)
350 (defun calcFunc-tderiv (expr math-deriv-var &optional deriv-value math-deriv-symb)
351 (math-setup-declarations)
352 (let* ((math-deriv-total t)
353 (res (catch 'math-deriv (math-derivative expr))))
354 (or (eq (car-safe res) 'calcFunc-tderiv)
356 (setq res (math-normalize res)))
359 (math-expr-subst res math-deriv-var deriv-value)
362 (put 'calcFunc-inv\' 'math-derivative-1
363 (function (lambda (u) (math-neg (math-div 1 (math-sqr u))))))
365 (put 'calcFunc-sqrt\' 'math-derivative-1
366 (function (lambda (u) (math-div 1 (math-mul 2 (list 'calcFunc-sqrt u))))))
368 (put 'calcFunc-deg\' 'math-derivative-1
369 (function (lambda (u) (math-div-float '(float 18 1) (math-pi)))))
371 (put 'calcFunc-rad\' 'math-derivative-1
372 (function (lambda (u) (math-pi-over-180))))
374 (put 'calcFunc-ln\' 'math-derivative-1
375 (function (lambda (u) (math-div 1 u))))
377 (put 'calcFunc-log10\' 'math-derivative-1
378 (function (lambda (u)
379 (math-div (math-div 1 (math-normalize '(calcFunc-ln 10)))
382 (put 'calcFunc-lnp1\' 'math-derivative-1
383 (function (lambda (u) (math-div 1 (math-add u 1)))))
385 (put 'calcFunc-log\' 'math-derivative-2
386 (function (lambda (x b)
387 (and (not (Math-zerop b))
388 (let ((lnv (math-normalize
389 (list 'calcFunc-ln b))))
390 (math-div 1 (math-mul lnv x)))))))
392 (put 'calcFunc-log\'2 'math-derivative-2
393 (function (lambda (x b)
394 (let ((lnv (list 'calcFunc-ln b)))
395 (math-neg (math-div (list 'calcFunc-log x b)
396 (math-mul lnv b)))))))
398 (put 'calcFunc-exp\' 'math-derivative-1
399 (function (lambda (u) (math-normalize (list 'calcFunc-exp u)))))
401 (put 'calcFunc-expm1\' 'math-derivative-1
402 (function (lambda (u) (math-normalize (list 'calcFunc-expm1 u)))))
404 (put 'calcFunc-sin\' 'math-derivative-1
405 (function (lambda (u) (math-to-radians-2 (math-normalize
406 (list 'calcFunc-cos u))))))
408 (put 'calcFunc-cos\' 'math-derivative-1
409 (function (lambda (u) (math-neg (math-to-radians-2
411 (list 'calcFunc-sin u)))))))
413 (put 'calcFunc-tan\' 'math-derivative-1
414 (function (lambda (u) (math-to-radians-2
415 (math-div 1 (math-sqr
417 (list 'calcFunc-cos u))))))))
419 (put 'calcFunc-arcsin\' 'math-derivative-1
420 (function (lambda (u)
422 (math-div 1 (math-normalize
424 (math-sub 1 (math-sqr u)))))))))
426 (put 'calcFunc-arccos\' 'math-derivative-1
427 (function (lambda (u)
429 (math-div -1 (math-normalize
431 (math-sub 1 (math-sqr u)))))))))
433 (put 'calcFunc-arctan\' 'math-derivative-1
434 (function (lambda (u) (math-from-radians-2
435 (math-div 1 (math-add 1 (math-sqr u)))))))
437 (put 'calcFunc-sinh\' 'math-derivative-1
438 (function (lambda (u) (math-normalize (list 'calcFunc-cosh u)))))
440 (put 'calcFunc-cosh\' 'math-derivative-1
441 (function (lambda (u) (math-normalize (list 'calcFunc-sinh u)))))
443 (put 'calcFunc-tanh\' 'math-derivative-1
444 (function (lambda (u) (math-div 1 (math-sqr
446 (list 'calcFunc-cosh u)))))))
448 (put 'calcFunc-arcsinh\' 'math-derivative-1
449 (function (lambda (u)
450 (math-div 1 (math-normalize
452 (math-add (math-sqr u) 1)))))))
454 (put 'calcFunc-arccosh\' 'math-derivative-1
455 (function (lambda (u)
456 (math-div 1 (math-normalize
458 (math-add (math-sqr u) -1)))))))
460 (put 'calcFunc-arctanh\' 'math-derivative-1
461 (function (lambda (u) (math-div 1 (math-sub 1 (math-sqr u))))))
463 (put 'calcFunc-bern\'2 'math-derivative-2
464 (function (lambda (n x)
465 (math-mul n (list 'calcFunc-bern (math-add n -1) x)))))
467 (put 'calcFunc-euler\'2 'math-derivative-2
468 (function (lambda (n x)
469 (math-mul n (list 'calcFunc-euler (math-add n -1) x)))))
471 (put 'calcFunc-gammag\'2 'math-derivative-2
472 (function (lambda (a x) (math-deriv-gamma a x 1))))
474 (put 'calcFunc-gammaG\'2 'math-derivative-2
475 (function (lambda (a x) (math-deriv-gamma a x -1))))
477 (put 'calcFunc-gammaP\'2 'math-derivative-2
478 (function (lambda (a x) (math-deriv-gamma a x
481 (list 'calcFunc-gamma
484 (put 'calcFunc-gammaQ\'2 'math-derivative-2
485 (function (lambda (a x) (math-deriv-gamma a x
488 (list 'calcFunc-gamma
491 (defun math-deriv-gamma (a x scale)
493 (math-mul (math-pow x (math-add a -1))
494 (list 'calcFunc-exp (math-neg x)))))
496 (put 'calcFunc-betaB\' 'math-derivative-3
497 (function (lambda (x a b) (math-deriv-beta x a b 1))))
499 (put 'calcFunc-betaI\' 'math-derivative-3
500 (function (lambda (x a b) (math-deriv-beta x a b
502 1 (list 'calcFunc-beta
505 (defun math-deriv-beta (x a b scale)
506 (math-mul (math-mul (math-pow x (math-add a -1))
507 (math-pow (math-sub 1 x) (math-add b -1)))
510 (put 'calcFunc-erf\' 'math-derivative-1
511 (function (lambda (x) (math-div 2
512 (math-mul (list 'calcFunc-exp
514 (if calc-symbolic-mode
519 (put 'calcFunc-erfc\' 'math-derivative-1
520 (function (lambda (x) (math-div -2
521 (math-mul (list 'calcFunc-exp
523 (if calc-symbolic-mode
528 (put 'calcFunc-besJ\'2 'math-derivative-2
529 (function (lambda (v z) (math-div (math-sub (list 'calcFunc-besJ
537 (put 'calcFunc-besY\'2 'math-derivative-2
538 (function (lambda (v z) (math-div (math-sub (list 'calcFunc-besY
546 (put 'calcFunc-sum 'math-derivative-n
549 (if (math-expr-contains (cons 'vec (cdr (cdr expr))) math-deriv-var)
550 (throw 'math-deriv nil)
552 (cons (math-derivative (nth 1 expr))
553 (cdr (cdr expr))))))))
555 (put 'calcFunc-prod 'math-derivative-n
558 (if (math-expr-contains (cons 'vec (cdr (cdr expr))) math-deriv-var)
559 (throw 'math-deriv nil)
562 (cons (math-div (math-derivative (nth 1 expr))
564 (cdr (cdr expr)))))))))
566 (put 'calcFunc-integ 'math-derivative-n
569 (if (= (length expr) 3)
570 (if (equal (nth 2 expr) math-deriv-var)
573 (list 'calcFunc-integ
574 (math-derivative (nth 1 expr))
576 (if (= (length expr) 5)
577 (let ((lower (math-expr-subst (nth 1 expr) (nth 2 expr)
579 (upper (math-expr-subst (nth 1 expr) (nth 2 expr)
581 (math-add (math-sub (math-mul upper
582 (math-derivative (nth 4 expr)))
584 (math-derivative (nth 3 expr))))
585 (if (equal (nth 2 expr) math-deriv-var)
588 (list 'calcFunc-integ
589 (math-derivative (nth 1 expr)) (nth 2 expr)
590 (nth 3 expr) (nth 4 expr)))))))))))
592 (put 'calcFunc-if 'math-derivative-n
595 (and (= (length expr) 4)
596 (list 'calcFunc-if (nth 1 expr)
597 (math-derivative (nth 2 expr))
598 (math-derivative (nth 3 expr)))))))
600 (put 'calcFunc-subscr 'math-derivative-n
603 (and (= (length expr) 3)
604 (list 'calcFunc-subscr (nth 1 expr)
605 (math-derivative (nth 2 expr)))))))
608 (defvar math-integ-var '(var X ---))
609 (defvar math-integ-var-2 '(var Y ---))
610 (defvar math-integ-vars (list 'f math-integ-var math-integ-var-2))
611 (defvar math-integ-var-list (list math-integ-var))
612 (defvar math-integ-var-list-list (list math-integ-var-list))
614 ;; math-integ-depth is a local variable for math-try-integral, but is used
615 ;; by math-integral and math-tracing-integral
616 ;; which are called (directly or indirectly) by math-try-integral.
617 (defvar math-integ-depth)
618 ;; math-integ-level is a local variable for math-try-integral, but is used
619 ;; by math-integral, math-do-integral, math-tracing-integral,
620 ;; math-sub-integration, math-integrate-by-parts and
621 ;; math-integrate-by-substitution, which are called (directly or
622 ;; indirectly) by math-try-integral.
623 (defvar math-integ-level)
624 ;; math-integral-limit is a local variable for calcFunc-integ, but is
625 ;; used by math-tracing-integral, math-sub-integration and
626 ;; math-try-integration.
627 (defvar math-integral-limit)
629 (defmacro math-tracing-integral (&rest parts)
632 (list 'save-excursion
633 '(set-buffer trace-buffer)
634 '(goto-char (point-max))
637 '(insert (make-string (- math-integral-limit
638 math-integ-level) 32)
639 (format "%2d " math-integ-depth)
640 (make-string math-integ-level 32)))
641 ;;(list 'condition-case 'err
643 ;; '(error (insert (prin1-to-string err))))
646 ;;; The following wrapper caches results and avoids infinite recursion.
647 ;;; Each cache entry is: ( A B ) Integral of A is B;
648 ;;; ( A N ) Integral of A failed at level N;
649 ;;; ( A busy ) Currently working on integral of A;
650 ;;; ( A parts ) Currently working, integ-by-parts;
651 ;;; ( A parts2 ) Currently working, integ-by-parts;
652 ;;; ( A cancelled ) Ignore this cache entry;
653 ;;; ( A [B] ) Same result as for math-cur-record = B.
655 ;; math-cur-record is a local variable for math-try-integral, but is used
656 ;; by math-integral, math-replace-integral-parts and math-integrate-by-parts
657 ;; which are called (directly or indirectly) by math-try-integral, as well as
658 ;; by calc-dump-integral-cache
659 (defvar math-cur-record)
660 ;; math-enable-subst and math-any-substs are local variables for
661 ;; calcFunc-integ, but are used by math-integral and math-try-integral.
662 (defvar math-enable-subst)
663 (defvar math-any-substs)
665 ;; math-integ-msg is a local variable for math-try-integral, but is
666 ;; used (both locally and non-locally) by math-integral.
667 (defvar math-integ-msg)
669 (defvar math-integral-cache nil)
670 (defvar math-integral-cache-state nil)
672 (defun math-integral (expr &optional simplify same-as-above)
673 (let* ((simp math-cur-record)
674 (math-cur-record (assoc expr math-integral-cache))
675 (math-integ-depth (1+ math-integ-depth))
677 (math-tracing-integral "Integrating "
678 (math-format-value expr 1000)
682 (math-tracing-integral "Found "
683 (math-format-value (nth 1 math-cur-record) 1000))
684 (and (consp (nth 1 math-cur-record))
685 (math-replace-integral-parts math-cur-record))
686 (math-tracing-integral " => "
687 (math-format-value (nth 1 math-cur-record) 1000)
689 (or (and math-cur-record
690 (not (eq (nth 1 math-cur-record) 'cancelled))
691 (or (not (integerp (nth 1 math-cur-record)))
692 (>= (nth 1 math-cur-record) math-integ-level)))
693 (and (math-integral-contains-parts expr)
699 (let (math-integ-msg)
700 (if (eq calc-display-working-message 'lots)
702 (calc-set-command-flag 'clear-message)
703 (setq math-integ-msg (format
704 "Working... Integrating %s"
705 (math-format-flat-expr expr 0)))
706 (message math-integ-msg)))
708 (setcar (cdr math-cur-record)
709 (if same-as-above (vector simp) 'busy))
710 (setq math-cur-record
711 (list expr (if same-as-above (vector simp) 'busy))
712 math-integral-cache (cons math-cur-record
713 math-integral-cache)))
714 (if (eq simplify 'yes)
716 (math-tracing-integral "Simplifying...")
717 (setq simp (math-simplify expr))
718 (setq val (if (equal simp expr)
720 (math-tracing-integral " no change\n")
721 (math-do-integral expr))
722 (math-tracing-integral " simplified\n")
723 (math-integral simp 'no t))))
724 (or (setq val (math-do-integral expr))
726 (let ((simp (math-simplify expr)))
727 (or (equal simp expr)
729 (math-tracing-integral "Trying again after "
730 "simplification...\n")
731 (setq val (math-integral simp 'no t))))))))
732 (if (eq calc-display-working-message 'lots)
733 (message math-integ-msg)))
734 (setcar (cdr math-cur-record) (or val
735 (if (or math-enable-subst
736 (not math-any-substs))
739 (setq val math-cur-record)
740 (while (vectorp (nth 1 val))
741 (setq val (aref (nth 1 val) 0)))
742 (setq val (if (memq (nth 1 val) '(parts parts2))
744 (setcar (cdr val) 'parts2)
745 (list 'var 'PARTS val))
746 (and (consp (nth 1 val))
748 (math-tracing-integral "Integral of "
749 (math-format-value expr 1000)
751 (math-format-value val 1000)
755 (defun math-integral-contains-parts (expr)
756 (if (Math-primp expr)
757 (and (eq (car-safe expr) 'var)
758 (eq (nth 1 expr) 'PARTS)
759 (listp (nth 2 expr)))
760 (while (and (setq expr (cdr expr))
761 (not (math-integral-contains-parts (car expr)))))
764 (defun math-replace-integral-parts (expr)
765 (or (Math-primp expr)
766 (while (setq expr (cdr expr))
767 (and (consp (car expr))
768 (if (eq (car (car expr)) 'var)
769 (and (eq (nth 1 (car expr)) 'PARTS)
770 (consp (nth 2 (car expr)))
771 (if (listp (nth 1 (nth 2 (car expr))))
773 (setcar expr (nth 1 (nth 2 (car expr))))
774 (math-replace-integral-parts (cons 'foo expr)))
775 (setcar (cdr math-cur-record) 'cancelled)))
776 (math-replace-integral-parts (car expr)))))))
778 (defvar math-linear-subst-tried t
779 "Non-nil means that a linear substitution has been tried.")
781 ;; The variable math-has-rules is a local variable for math-try-integral,
782 ;; but is used by math-do-integral, which is called (non-directly) by
783 ;; math-try-integral.
784 (defvar math-has-rules)
786 ;; math-old-integ is a local variable for math-do-integral, but is
787 ;; used by math-sub-integration.
788 (defvar math-old-integ)
790 ;; The variables math-t1, math-t2 and math-t3 are local to
791 ;; math-do-integral, math-try-solve-for and math-decompose-poly, but
792 ;; are used by functions they call (directly or indirectly);
793 ;; math-do-integral calls math-do-integral-methods;
794 ;; math-try-solve-for calls math-try-solve-prod,
795 ;; math-solve-find-root-term and math-solve-find-root-in-prod;
796 ;; math-decompose-poly calls math-solve-poly-funny-powers and
797 ;; math-solve-crunch-poly.
802 (defun math-do-integral (expr)
803 (let ((math-linear-subst-tried nil)
805 (or (cond ((not (math-expr-contains expr math-integ-var))
806 (math-mul expr math-integ-var))
807 ((equal expr math-integ-var)
808 (math-div (math-sqr expr) 2))
810 (and (setq math-t1 (math-integral (nth 1 expr)))
811 (setq math-t2 (math-integral (nth 2 expr)))
812 (math-add math-t1 math-t2)))
814 (and (setq math-t1 (math-integral (nth 1 expr)))
815 (setq math-t2 (math-integral (nth 2 expr)))
816 (math-sub math-t1 math-t2)))
817 ((eq (car expr) 'neg)
818 (and (setq math-t1 (math-integral (nth 1 expr)))
821 (cond ((not (math-expr-contains (nth 1 expr) math-integ-var))
822 (and (setq math-t1 (math-integral (nth 2 expr)))
823 (math-mul (nth 1 expr) math-t1)))
824 ((not (math-expr-contains (nth 2 expr) math-integ-var))
825 (and (setq math-t1 (math-integral (nth 1 expr)))
826 (math-mul math-t1 (nth 2 expr))))
827 ((memq (car-safe (nth 1 expr)) '(+ -))
828 (math-integral (list (car (nth 1 expr))
829 (math-mul (nth 1 (nth 1 expr))
831 (math-mul (nth 2 (nth 1 expr))
834 ((memq (car-safe (nth 2 expr)) '(+ -))
835 (math-integral (list (car (nth 2 expr))
836 (math-mul (nth 1 (nth 2 expr))
838 (math-mul (nth 2 (nth 2 expr))
842 (cond ((and (not (math-expr-contains (nth 1 expr)
844 (not (math-equal-int (nth 1 expr) 1)))
845 (and (setq math-t1 (math-integral (math-div 1 (nth 2 expr))))
846 (math-mul (nth 1 expr) math-t1)))
847 ((not (math-expr-contains (nth 2 expr) math-integ-var))
848 (and (setq math-t1 (math-integral (nth 1 expr)))
849 (math-div math-t1 (nth 2 expr))))
850 ((and (eq (car-safe (nth 1 expr)) '*)
851 (not (math-expr-contains (nth 1 (nth 1 expr))
853 (and (setq math-t1 (math-integral
854 (math-div (nth 2 (nth 1 expr))
856 (math-mul math-t1 (nth 1 (nth 1 expr)))))
857 ((and (eq (car-safe (nth 1 expr)) '*)
858 (not (math-expr-contains (nth 2 (nth 1 expr))
860 (and (setq math-t1 (math-integral
861 (math-div (nth 1 (nth 1 expr))
863 (math-mul math-t1 (nth 2 (nth 1 expr)))))
864 ((and (eq (car-safe (nth 2 expr)) '*)
865 (not (math-expr-contains (nth 1 (nth 2 expr))
867 (and (setq math-t1 (math-integral
868 (math-div (nth 1 expr)
869 (nth 2 (nth 2 expr)))))
870 (math-div math-t1 (nth 1 (nth 2 expr)))))
871 ((and (eq (car-safe (nth 2 expr)) '*)
872 (not (math-expr-contains (nth 2 (nth 2 expr))
874 (and (setq math-t1 (math-integral
875 (math-div (nth 1 expr)
876 (nth 1 (nth 2 expr)))))
877 (math-div math-t1 (nth 2 (nth 2 expr)))))
878 ((eq (car-safe (nth 2 expr)) 'calcFunc-exp)
880 (math-mul (nth 1 expr)
882 (math-neg (nth 1 (nth 2 expr)))))))))
884 (cond ((not (math-expr-contains (nth 1 expr) math-integ-var))
885 (or (and (setq math-t1 (math-is-polynomial (nth 2 expr)
888 (math-mul (nth 1 math-t1)
894 (math-mul (nth 2 expr)
899 ((not (math-expr-contains (nth 2 expr) math-integ-var))
900 (if (and (integerp (nth 2 expr)) (< (nth 2 expr) 0))
902 (list '/ 1 (math-pow (nth 1 expr) (- (nth 2 expr))))
904 (or (and (setq math-t1 (math-is-polynomial (nth 1 expr)
907 (setq math-t2 (math-add (nth 2 expr) 1))
908 (math-div (math-pow (nth 1 expr) math-t2)
909 (math-mul math-t2 (nth 1 math-t1))))
910 (and (Math-negp (nth 2 expr))
913 (math-pow (nth 1 expr)
919 ;; Integral of a polynomial.
920 (and (setq math-t1 (math-is-polynomial expr math-integ-var 20))
924 (if (setq accum (math-add accum
925 (math-div (math-mul (car math-t1)
930 math-t1 (cdr math-t1))
934 ;; Try looking it up!
935 (cond ((= (length expr) 2)
936 (and (symbolp (car expr))
937 (setq math-t1 (get (car expr) 'math-integral))
940 (not (setq math-t2 (funcall (car math-t1)
942 (setq math-t1 (cdr math-t1)))
943 (and math-t2 (math-normalize math-t2)))))
945 (and (symbolp (car expr))
946 (setq math-t1 (get (car expr) 'math-integral-2))
949 (not (setq math-t2 (funcall (car math-t1)
952 (setq math-t1 (cdr math-t1)))
953 (and math-t2 (math-normalize math-t2))))))
955 ;; Integral of a rational function.
956 (and (math-ratpoly-p expr math-integ-var)
957 (setq math-t1 (calcFunc-apart expr math-integ-var))
958 (not (equal math-t1 expr))
959 (math-integral math-t1))
961 ;; Try user-defined integration rules.
963 (let ((math-old-integ (symbol-function 'calcFunc-integ))
964 (input (list 'calcFunc-integtry expr math-integ-var))
968 (fset 'calcFunc-integ 'math-sub-integration)
969 (setq res (math-rewrite input
970 '(var IntegRules var-IntegRules)
972 (fset 'calcFunc-integ math-old-integ)
973 (and (not (equal res input))
974 (if (setq part (math-expr-calls
975 res '(calcFunc-integsubst)))
976 (and (memq (length part) '(3 4 5))
984 (math-integrate-by-substitution
987 (list 'calcFunc-integfailed
990 (if (not (math-expr-calls res
992 calcFunc-integfailed)))
994 (fset 'calcFunc-integ math-old-integ))))
996 ;; See if the function is a symbolic derivative.
997 (and (string-match "'" (symbol-name (car expr)))
998 (let ((name (symbol-name (car expr)))
999 (p expr) (n 0) (which nil) (bad nil))
1000 (while (setq n (1+ n) p (cdr p))
1001 (if (equal (car p) math-integ-var)
1002 (if which (setq bad t) (setq which n))
1003 (if (math-expr-contains (car p) math-integ-var)
1005 (and which (not bad)
1006 (let ((prime (if (= which 1) "'" (format "'%d" which))))
1007 (and (string-match (concat prime "\\('['0-9]*\\|$\\)")
1011 (substring name 0 (match-beginning 0))
1012 (substring name (+ (match-beginning 0)
1016 ;; Try transformation methods (parts, substitutions).
1017 (and (> math-integ-level 0)
1018 (math-do-integral-methods expr))
1020 ;; Try expanding the function's definition.
1021 (let ((res (math-expand-formula expr)))
1023 (math-integral res))))))
1025 (defun math-sub-integration (expr &rest rest)
1026 (or (if (or (not rest)
1027 (and (< math-integ-level math-integral-limit)
1028 (eq (car rest) math-integ-var)))
1029 (math-integral expr)
1030 (let ((res (apply math-old-integ expr rest)))
1031 (and (or (= math-integ-level math-integral-limit)
1032 (not (math-expr-calls res 'calcFunc-integ)))
1034 (list 'calcFunc-integfailed expr)))
1036 ;; math-so-far is a local variable for math-do-integral-methods, but
1037 ;; is used by math-integ-try-linear-substitutions and
1038 ;; math-integ-try-substitutions.
1039 (defvar math-so-far)
1041 ;; math-integ-expr is a local variable for math-do-integral-methods,
1042 ;; but is used by math-integ-try-linear-substitutions and
1043 ;; math-integ-try-substitutions.
1044 (defvar math-integ-expr)
1046 (defun math-do-integral-methods (math-integ-expr)
1047 (let ((math-so-far math-integ-var-list-list)
1050 ;; Integration by substitution, for various likely sub-expressions.
1051 ;; (In first pass, we look only for sub-exprs that are linear in X.)
1052 (or (math-integ-try-linear-substitutions math-integ-expr)
1053 (math-integ-try-substitutions math-integ-expr)
1055 ;; If function has sines and cosines, try tan(x/2) substitution.
1056 (and (let ((p (setq rat-in (math-expr-rational-in math-integ-expr))))
1058 (memq (car (car p)) '(calcFunc-sin
1061 (equal (nth 1 (car p)) math-integ-var))
1064 (or (and (math-integ-parts-easy math-integ-expr)
1065 (math-integ-try-parts math-integ-expr t))
1066 (math-integrate-by-good-substitution
1067 math-integ-expr (list 'calcFunc-tan (math-div math-integ-var 2)))))
1069 ;; If function has sinh and cosh, try tanh(x/2) substitution.
1070 (and (let ((p rat-in))
1072 (memq (car (car p)) '(calcFunc-sinh
1076 (equal (nth 1 (car p)) math-integ-var))
1079 (or (and (math-integ-parts-easy math-integ-expr)
1080 (math-integ-try-parts math-integ-expr t))
1081 (math-integrate-by-good-substitution
1082 math-integ-expr (list 'calcFunc-tanh (math-div math-integ-var 2)))))
1084 ;; If function has square roots, try sin, tan, or sec substitution.
1085 (and (let ((p rat-in))
1088 (or (equal (car p) math-integ-var)
1089 (and (eq (car (car p)) 'calcFunc-sqrt)
1090 (setq math-t1 (math-is-polynomial
1091 (nth 1 (setq math-t2 (car p)))
1092 math-integ-var 2)))))
1094 (and (null p) math-t1))
1095 (if (cdr (cdr math-t1))
1096 (if (math-guess-if-neg (nth 2 math-t1))
1097 (let* ((c (math-sqrt (math-neg (nth 2 math-t1))))
1098 (d (math-div (nth 1 math-t1) (math-mul -2 c)))
1099 (a (math-sqrt (math-add (car math-t1) (math-sqr d)))))
1100 (math-integrate-by-good-substitution
1101 math-integ-expr (list 'calcFunc-arcsin
1103 (math-add (math-mul c math-integ-var) d)
1105 (let* ((c (math-sqrt (nth 2 math-t1)))
1106 (d (math-div (nth 1 math-t1) (math-mul 2 c)))
1107 (aa (math-sub (car math-t1) (math-sqr d))))
1108 (if (and nil (not (and (eq d 0) (eq c 1))))
1109 (math-integrate-by-good-substitution
1110 math-integ-expr (math-add (math-mul c math-integ-var) d))
1111 (if (math-guess-if-neg aa)
1112 (math-integrate-by-good-substitution
1113 math-integ-expr (list 'calcFunc-arccosh
1115 (math-add (math-mul c math-integ-var)
1117 (math-sqrt (math-neg aa)))))
1118 (math-integrate-by-good-substitution
1119 math-integ-expr (list 'calcFunc-arcsinh
1121 (math-add (math-mul c math-integ-var)
1123 (math-sqrt aa))))))))
1124 (math-integrate-by-good-substitution math-integ-expr math-t2)) )
1126 ;; Try integration by parts.
1127 (math-integ-try-parts math-integ-expr)
1132 (defun math-integ-parts-easy (expr)
1133 (cond ((Math-primp expr) t)
1134 ((memq (car expr) '(+ - *))
1135 (and (math-integ-parts-easy (nth 1 expr))
1136 (math-integ-parts-easy (nth 2 expr))))
1138 (and (math-integ-parts-easy (nth 1 expr))
1139 (math-atomic-factorp (nth 2 expr))))
1141 (and (natnump (nth 2 expr))
1142 (math-integ-parts-easy (nth 1 expr))))
1143 ((eq (car expr) 'neg)
1144 (math-integ-parts-easy (nth 1 expr)))
1147 ;; math-prev-parts-v is local to calcFunc-integ (as well as
1148 ;; math-integrate-by-parts), but is used by math-integ-try-parts.
1149 (defvar math-prev-parts-v)
1151 ;; math-good-parts is local to calcFunc-integ (as well as
1152 ;; math-integ-try-parts), but is used by math-integrate-by-parts.
1153 (defvar math-good-parts)
1156 (defun math-integ-try-parts (expr &optional math-good-parts)
1157 ;; Integration by parts:
1158 ;; integ(f(x) g(x),x) = f(x) h(x) - integ(h(x) f'(x),x)
1159 ;; where h(x) = integ(g(x),x).
1160 (or (let ((exp (calcFunc-expand expr)))
1161 (and (not (equal exp expr))
1162 (math-integral exp)))
1163 (and (eq (car expr) '*)
1164 (let ((first-bad (or (math-polynomial-p (nth 1 expr)
1166 (equal (nth 2 expr) math-prev-parts-v))))
1167 (or (and first-bad ; so try this one first
1168 (math-integrate-by-parts (nth 1 expr) (nth 2 expr)))
1169 (math-integrate-by-parts (nth 2 expr) (nth 1 expr))
1170 (and (not first-bad)
1171 (math-integrate-by-parts (nth 1 expr) (nth 2 expr))))))
1172 (and (eq (car expr) '/)
1173 (math-expr-contains (nth 1 expr) math-integ-var)
1174 (let ((recip (math-div 1 (nth 2 expr))))
1175 (or (math-integrate-by-parts (nth 1 expr) recip)
1176 (math-integrate-by-parts recip (nth 1 expr)))))
1177 (and (eq (car expr) '^)
1178 (math-integrate-by-parts (math-pow (nth 1 expr)
1179 (math-sub (nth 2 expr) 1))
1182 (defun math-integrate-by-parts (u vprime)
1183 (let ((math-integ-level (if (or math-good-parts
1184 (math-polynomial-p u math-integ-var))
1186 (1- math-integ-level)))
1187 (math-doing-parts t)
1189 (and (>= math-integ-level 0)
1192 (setcar (cdr math-cur-record) 'parts)
1193 (math-tracing-integral "Integrating by parts, u = "
1194 (math-format-value u 1000)
1196 (math-format-value vprime 1000)
1198 (and (setq v (math-integral vprime))
1199 (setq temp (calcFunc-deriv u math-integ-var nil t))
1200 (setq temp (let ((math-prev-parts-v v))
1201 (math-integral (math-mul v temp) 'yes)))
1202 (setq temp (math-sub (math-mul u v) temp))
1203 (if (eq (nth 1 math-cur-record) 'parts)
1204 (calcFunc-expand temp)
1205 (setq v (list 'var 'PARTS math-cur-record)
1206 temp (let (calc-next-why)
1207 (math-solve-for (math-sub v temp) 0 v nil)))
1208 (and temp (not (integerp temp))
1209 (math-simplify-extended temp)))))
1210 (setcar (cdr math-cur-record) 'busy)))))
1212 ;;; This tries two different formulations, hoping the algebraic simplifier
1213 ;;; will be strong enough to handle at least one.
1214 (defun math-integrate-by-substitution (expr u &optional user uinv uinvprime)
1215 (and (> math-integ-level 0)
1216 (let ((math-integ-level (max (- math-integ-level 2) 0)))
1217 (math-integrate-by-good-substitution expr u user uinv uinvprime))))
1219 (defun math-integrate-by-good-substitution (expr u &optional user
1221 (let ((math-living-dangerously t)
1223 (and (setq uinv (if uinv
1224 (math-expr-subst uinv math-integ-var
1226 (let (calc-next-why)
1229 math-integ-var nil))))
1231 (math-tracing-integral "Integrating by substitution, u = "
1232 (math-format-value u 1000)
1234 (or (and (setq deriv (calcFunc-deriv u
1237 (setq temp (math-integral (math-expr-subst
1240 (math-div expr deriv)
1248 (and (setq deriv (or uinvprime
1249 (calcFunc-deriv uinv
1253 (setq temp (math-integral (math-mul
1266 (math-simplify-extended
1267 (math-expr-subst temp math-integ-var u)))))
1269 ;;; Look for substitutions of the form u = a x + b.
1270 (defun math-integ-try-linear-substitutions (sub-expr)
1271 (setq math-linear-subst-tried t)
1272 (and (not (Math-primp sub-expr))
1273 (or (and (not (memq (car sub-expr) '(+ - * / neg)))
1274 (not (and (eq (car sub-expr) '^)
1275 (integerp (nth 2 sub-expr))))
1276 (math-expr-contains sub-expr math-integ-var)
1278 (while (and (setq sub-expr (cdr sub-expr))
1279 (or (not (math-linear-in (car sub-expr)
1281 (assoc (car sub-expr) math-so-far)
1283 (setq math-so-far (cons (list (car sub-expr))
1286 (math-integrate-by-substitution
1287 math-integ-expr (car sub-expr))))))))
1290 (while (and (setq sub-expr (cdr sub-expr))
1291 (not (setq res (math-integ-try-linear-substitutions
1295 ;;; Recursively try different substitutions based on various sub-expressions.
1296 (defun math-integ-try-substitutions (sub-expr &optional allow-rat)
1297 (and (not (Math-primp sub-expr))
1298 (not (assoc sub-expr math-so-far))
1299 (math-expr-contains sub-expr math-integ-var)
1300 (or (and (if (and (not (memq (car sub-expr) '(+ - * / neg)))
1301 (not (and (eq (car sub-expr) '^)
1302 (integerp (nth 2 sub-expr)))))
1304 (prog1 allow-rat (setq allow-rat nil)))
1305 (not (eq sub-expr math-integ-expr))
1306 (or (math-integrate-by-substitution math-integ-expr sub-expr)
1307 (and (eq (car sub-expr) '^)
1308 (integerp (nth 2 sub-expr))
1309 (< (nth 2 sub-expr) 0)
1310 (math-integ-try-substitutions
1311 (math-pow (nth 1 sub-expr) (- (nth 2 sub-expr)))
1314 (setq math-so-far (cons (list sub-expr) math-so-far))
1315 (while (and (setq sub-expr (cdr sub-expr))
1316 (not (setq res (math-integ-try-substitutions
1317 (car sub-expr) allow-rat)))))
1320 ;; The variable math-expr-parts is local to math-expr-rational-in,
1321 ;; but is used by math-expr-rational-in-rec
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)
2283 (defun math-try-solve-for
2284 (math-solve-lhs math-solve-rhs &optional math-try-solve-sign no-poly)
2285 (let (math-t1 math-t2 math-t3)
2286 (cond ((equal math-solve-lhs math-solve-var)
2287 (setq math-solve-sign math-try-solve-sign)
2288 (if (eq math-solve-full 'all)
2289 (let ((vec (list 'vec (math-evaluate-expr math-solve-rhs)))
2291 (while math-solve-ranges
2292 (setq p (car math-solve-ranges)
2295 (while (setq p (cdr p))
2296 (setq newvec (nconc newvec
2297 (cdr (math-expr-subst
2298 vec var (car p))))))
2300 math-solve-ranges (cdr math-solve-ranges)))
2301 (math-normalize vec))
2303 ((Math-primp math-solve-lhs)
2305 ((and (eq (car math-solve-lhs) '-)
2306 (eq (car-safe (nth 1 math-solve-lhs)) (car-safe (nth 2 math-solve-lhs)))
2307 (Math-zerop math-solve-rhs)
2308 (= (length (nth 1 math-solve-lhs)) 2)
2309 (= (length (nth 2 math-solve-lhs)) 2)
2310 (setq math-t1 (get (car (nth 1 math-solve-lhs)) 'math-inverse))
2311 (setq math-t2 (funcall math-t1 '(var SOLVEDUM SOLVEDUM)))
2312 (eq (math-expr-contains-count math-t2 '(var SOLVEDUM SOLVEDUM)) 1)
2313 (setq math-t3 (math-solve-above-dummy math-t2))
2314 (setq math-t1 (math-try-solve-for
2315 (math-sub (nth 1 (nth 1 math-solve-lhs))
2318 (nth 1 (nth 2 math-solve-lhs))))
2321 ((eq (car math-solve-lhs) 'neg)
2322 (math-try-solve-for (nth 1 math-solve-lhs) (math-neg math-solve-rhs)
2323 (and math-try-solve-sign (- math-try-solve-sign))))
2324 ((and (not (eq math-solve-full 't)) (math-try-solve-prod)))
2327 (math-decompose-poly math-solve-lhs
2328 math-solve-var 15 math-solve-rhs)))
2329 (setq math-t1 (cdr (nth 1 math-t2))
2330 math-t1 (let ((math-solve-ranges math-solve-ranges))
2331 (cond ((= (length math-t1) 5)
2332 (apply 'math-solve-quartic (car math-t2) math-t1))
2333 ((= (length math-t1) 4)
2334 (apply 'math-solve-cubic (car math-t2) math-t1))
2335 ((= (length math-t1) 3)
2336 (apply 'math-solve-quadratic (car math-t2) math-t1))
2337 ((= (length math-t1) 2)
2338 (apply 'math-solve-linear
2339 (car math-t2) math-try-solve-sign math-t1))
2341 (math-poly-all-roots (car math-t2) math-t1))
2342 (calc-symbolic-mode nil)
2346 (math-poly-any-root (reverse math-t1) 0 t)
2349 (if (eq (nth 2 math-t2) 1)
2351 (math-solve-prod math-t1 (math-try-solve-for (nth 2 math-t2) 0 nil t)))
2352 (calc-record-why "*Unable to find a symbolic solution")
2354 ((and (math-solve-find-root-term math-solve-lhs nil)
2355 (eq (math-expr-contains-count math-solve-lhs math-t1) 1)) ; just in case
2356 (math-try-solve-for (math-simplify
2357 (math-sub (if (or math-t3 (math-evenp math-t2))
2358 (math-pow math-t1 math-t2)
2359 (math-neg (math-pow math-t1 math-t2)))
2361 (math-sub (math-normalize
2363 math-solve-lhs math-t1 0))
2365 math-t2 math-solve-var)))
2367 ((eq (car math-solve-lhs) '+)
2368 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2369 (math-try-solve-for (nth 2 math-solve-lhs)
2370 (math-sub math-solve-rhs (nth 1 math-solve-lhs))
2371 math-try-solve-sign))
2372 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2373 (math-try-solve-for (nth 1 math-solve-lhs)
2374 (math-sub math-solve-rhs (nth 2 math-solve-lhs))
2375 math-try-solve-sign))))
2376 ((eq (car math-solve-lhs) 'calcFunc-eq)
2377 (math-try-solve-for (math-sub (nth 1 math-solve-lhs) (nth 2 math-solve-lhs))
2378 math-solve-rhs math-try-solve-sign no-poly))
2379 ((eq (car math-solve-lhs) '-)
2380 (cond ((or (and (eq (car-safe (nth 1 math-solve-lhs)) 'calcFunc-sin)
2381 (eq (car-safe (nth 2 math-solve-lhs)) 'calcFunc-cos))
2382 (and (eq (car-safe (nth 1 math-solve-lhs)) 'calcFunc-cos)
2383 (eq (car-safe (nth 2 math-solve-lhs)) 'calcFunc-sin)))
2384 (math-try-solve-for (math-sub (nth 1 math-solve-lhs)
2385 (list (car (nth 1 math-solve-lhs))
2387 (math-quarter-circle t)
2388 (nth 1 (nth 2 math-solve-lhs)))))
2390 ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2391 (math-try-solve-for (nth 2 math-solve-lhs)
2392 (math-sub (nth 1 math-solve-lhs) math-solve-rhs)
2393 (and math-try-solve-sign
2394 (- math-try-solve-sign))))
2395 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2396 (math-try-solve-for (nth 1 math-solve-lhs)
2397 (math-add math-solve-rhs (nth 2 math-solve-lhs))
2398 math-try-solve-sign))))
2399 ((and (eq math-solve-full 't) (math-try-solve-prod)))
2400 ((and (eq (car math-solve-lhs) '%)
2401 (not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var)))
2402 (math-try-solve-for (nth 1 math-solve-lhs) (math-add math-solve-rhs
2404 (nth 2 math-solve-lhs)))))
2405 ((eq (car math-solve-lhs) 'calcFunc-log)
2406 (cond ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2407 (math-try-solve-for (nth 1 math-solve-lhs)
2408 (math-pow (nth 2 math-solve-lhs) math-solve-rhs)))
2409 ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2410 (math-try-solve-for (nth 2 math-solve-lhs) (math-pow
2411 (nth 1 math-solve-lhs)
2412 (math-div 1 math-solve-rhs))))))
2413 ((and (= (length math-solve-lhs) 2)
2414 (symbolp (car math-solve-lhs))
2415 (setq math-t1 (get (car math-solve-lhs) 'math-inverse))
2416 (setq math-t2 (funcall math-t1 math-solve-rhs)))
2417 (setq math-t1 (get (car math-solve-lhs) 'math-inverse-sign))
2418 (math-try-solve-for (nth 1 math-solve-lhs) (math-normalize math-t2)
2419 (and math-try-solve-sign math-t1
2420 (if (integerp math-t1)
2421 (* math-t1 math-try-solve-sign)
2422 (funcall math-t1 math-solve-lhs
2423 math-try-solve-sign)))))
2424 ((and (symbolp (car math-solve-lhs))
2425 (setq math-t1 (get (car math-solve-lhs) 'math-inverse-n))
2426 (setq math-t2 (funcall math-t1 math-solve-lhs math-solve-rhs)))
2428 ((setq math-t1 (math-expand-formula math-solve-lhs))
2429 (math-try-solve-for math-t1 math-solve-rhs math-try-solve-sign))
2431 (calc-record-why "*No inverse known" math-solve-lhs)
2435 (defun math-try-solve-prod ()
2436 (cond ((eq (car math-solve-lhs) '*)
2437 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2438 (math-try-solve-for (nth 2 math-solve-lhs)
2439 (math-div math-solve-rhs (nth 1 math-solve-lhs))
2440 (math-solve-sign math-try-solve-sign
2441 (nth 1 math-solve-lhs))))
2442 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2443 (math-try-solve-for (nth 1 math-solve-lhs)
2444 (math-div math-solve-rhs (nth 2 math-solve-lhs))
2445 (math-solve-sign math-try-solve-sign
2446 (nth 2 math-solve-lhs))))
2447 ((Math-zerop math-solve-rhs)
2448 (math-solve-prod (let ((math-solve-ranges math-solve-ranges))
2449 (math-try-solve-for (nth 2 math-solve-lhs) 0))
2450 (math-try-solve-for (nth 1 math-solve-lhs) 0)))))
2451 ((eq (car math-solve-lhs) '/)
2452 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2453 (math-try-solve-for (nth 2 math-solve-lhs)
2454 (math-div (nth 1 math-solve-lhs) math-solve-rhs)
2455 (math-solve-sign math-try-solve-sign
2456 (nth 1 math-solve-lhs))))
2457 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2458 (math-try-solve-for (nth 1 math-solve-lhs)
2459 (math-mul math-solve-rhs (nth 2 math-solve-lhs))
2460 (math-solve-sign math-try-solve-sign
2461 (nth 2 math-solve-lhs))))
2462 ((setq math-t1 (math-try-solve-for (math-sub (nth 1 math-solve-lhs)
2463 (math-mul (nth 2 math-solve-lhs)
2467 ((eq (car math-solve-lhs) '^)
2468 (cond ((not (math-expr-contains (nth 1 math-solve-lhs) math-solve-var))
2470 (nth 2 math-solve-lhs)
2471 (math-add (math-normalize
2472 (list 'calcFunc-log math-solve-rhs (nth 1 math-solve-lhs)))
2475 (math-mul '(var pi var-pi)
2479 (list 'calcFunc-ln (nth 1 math-solve-lhs)))))))
2480 ((not (math-expr-contains (nth 2 math-solve-lhs) math-solve-var))
2481 (cond ((and (integerp (nth 2 math-solve-lhs))
2482 (>= (nth 2 math-solve-lhs) 2)
2483 (setq math-t1 (math-integer-log2 (nth 2 math-solve-lhs))))
2484 (setq math-t2 math-solve-rhs)
2485 (if (and (eq math-solve-full t)
2486 (math-known-realp (nth 1 math-solve-lhs)))
2488 (while (>= (setq math-t1 (1- math-t1)) 0)
2489 (setq math-t2 (list 'calcFunc-sqrt math-t2)))
2490 (setq math-t2 (math-solve-get-sign math-t2)))
2491 (while (>= (setq math-t1 (1- math-t1)) 0)
2492 (setq math-t2 (math-solve-get-sign
2494 (list 'calcFunc-sqrt math-t2))))))
2496 (nth 1 math-solve-lhs)
2497 (math-normalize math-t2)))
2498 ((math-looks-negp (nth 2 math-solve-lhs))
2500 (list '^ (nth 1 math-solve-lhs)
2501 (math-neg (nth 2 math-solve-lhs)))
2502 (math-div 1 math-solve-rhs)))
2503 ((and (eq math-solve-full t)
2504 (Math-integerp (nth 2 math-solve-lhs))
2505 (math-known-realp (nth 1 math-solve-lhs)))
2506 (setq math-t1 (math-normalize
2507 (list 'calcFunc-nroot math-solve-rhs
2508 (nth 2 math-solve-lhs))))
2509 (if (math-evenp (nth 2 math-solve-lhs))
2510 (setq math-t1 (math-solve-get-sign math-t1)))
2512 (nth 1 math-solve-lhs) math-t1
2513 (and math-try-solve-sign
2514 (math-oddp (nth 2 math-solve-lhs))
2515 (math-solve-sign math-try-solve-sign
2516 (nth 2 math-solve-lhs)))))
2517 (t (math-try-solve-for
2518 (nth 1 math-solve-lhs)
2522 (if (Math-realp (nth 2 math-solve-lhs))
2527 (and (integerp (nth 2 math-solve-lhs))
2529 (nth 2 math-solve-lhs)))))
2530 (math-div (nth 2 math-solve-lhs) 2))
2537 (and (integerp (nth 2 math-solve-lhs))
2539 (nth 2 math-solve-lhs))))))
2540 (nth 2 math-solve-lhs)))))
2542 (list 'calcFunc-nroot
2544 (nth 2 math-solve-lhs))))
2545 (and math-try-solve-sign
2546 (math-oddp (nth 2 math-solve-lhs))
2547 (math-solve-sign math-try-solve-sign
2548 (nth 2 math-solve-lhs)))))))))
2551 (defun math-solve-prod (lsoln rsoln)
2556 ((eq math-solve-full 'all)
2557 (cons 'vec (append (cdr lsoln) (cdr rsoln))))
2560 (list 'calcFunc-gt (math-solve-get-sign 1) 0)
2565 ;;; This deals with negative, fractional, and symbolic powers of "x".
2566 ;; The variable math-solve-b is local to math-decompose-poly,
2567 ;; but is used by math-solve-poly-funny-powers.
2569 (defun math-solve-poly-funny-powers (sub-rhs) ; uses "t1", "t2"
2570 (setq math-t1 math-solve-lhs)
2571 (let ((pp math-poly-neg-powers)
2574 (setq fac (math-pow (car pp) (or math-poly-mult-powers 1))
2575 math-t1 (math-mul math-t1 fac)
2576 math-solve-rhs (math-mul math-solve-rhs fac)
2578 (if sub-rhs (setq math-t1 (math-sub math-t1 math-solve-rhs)))
2579 (let ((math-poly-neg-powers nil))
2580 (setq math-t2 (math-mul (or math-poly-mult-powers 1)
2581 (let ((calc-prefer-frac t))
2582 (math-div 1 math-poly-frac-powers)))
2583 math-t1 (math-is-polynomial
2584 (math-simplify (calcFunc-expand math-t1)) math-solve-b 50))))
2586 ;;; This converts "a x^8 + b x^5 + c x^2" to "(a (x^3)^2 + b (x^3) + c) * x^2".
2587 (defun math-solve-crunch-poly (max-degree) ; uses "t1", "t3"
2589 (while (and math-t1 (Math-zerop (car math-t1)))
2590 (setq math-t1 (cdr math-t1)
2593 (let* ((degree (1- (length math-t1)))
2595 (while (and (> scale 1) (= (car math-t3) 1))
2596 (and (= (% degree scale) 0)
2602 (if (= (% n scale) 0)
2603 (setq new-t1 (nconc new-t1 (list (car p))))
2604 (or (Math-zerop (car p))
2609 (setq math-t3 (cons scale (cdr math-t3))
2611 (setq scale (1- scale)))
2612 (setq math-t3 (list (math-mul (car math-t3) math-t2)
2613 (math-mul count math-t2)))
2614 (<= (1- (length math-t1)) max-degree)))))
2616 (defun calcFunc-poly (expr var &optional degree)
2618 (or (natnump degree) (math-reject-arg degree 'fixnatnump))
2620 (let ((p (math-is-polynomial expr var degree 'gen)))
2625 (math-reject-arg expr "Expected a polynomial"))))
2627 (defun calcFunc-gpoly (expr var &optional degree)
2629 (or (natnump degree) (math-reject-arg degree 'fixnatnump))
2631 (let* ((math-poly-base-variable var)
2632 (d (math-decompose-poly expr var degree nil)))
2635 (math-reject-arg expr "Expected a polynomial"))))
2637 (defun math-decompose-poly (math-solve-lhs math-solve-var degree sub-rhs)
2638 (let ((math-solve-rhs (or sub-rhs 1))
2639 math-t1 math-t2 math-t3)
2640 (setq math-t2 (math-polynomial-base
2643 (lambda (math-solve-b)
2644 (let ((math-poly-neg-powers '(1))
2645 (math-poly-mult-powers nil)
2646 (math-poly-frac-powers 1)
2647 (math-poly-exp-base t))
2648 (and (not (equal math-solve-b math-solve-lhs))
2649 (or (not (memq (car-safe math-solve-b) '(+ -))) sub-rhs)
2650 (setq math-t3 '(1 0) math-t2 1
2651 math-t1 (math-is-polynomial math-solve-lhs
2653 (if (and (equal math-poly-neg-powers '(1))
2654 (memq math-poly-mult-powers '(nil 1))
2655 (eq math-poly-frac-powers 1)
2657 (setq math-t1 (cons (math-sub (car math-t1) math-solve-rhs)
2659 (math-solve-poly-funny-powers sub-rhs))
2660 (math-solve-crunch-poly degree)
2661 (or (math-expr-contains math-solve-b math-solve-var)
2662 (math-expr-contains (car math-t3) math-solve-var))))))))
2664 (list (math-pow math-t2 (car math-t3))
2667 (math-pow math-t2 (nth 1 math-t3))
2668 (math-div (math-pow math-t2 (nth 1 math-t3)) math-solve-rhs))))))
2670 (defun math-solve-linear (var sign b a)
2671 (math-try-solve-for var
2672 (math-div (math-neg b) a)
2673 (math-solve-sign sign a)
2676 (defun math-solve-quadratic (var c b a)
2679 (if (math-looks-evenp b)
2680 (let ((halfb (math-div b 2)))
2684 (math-solve-get-sign
2686 (list 'calcFunc-sqrt
2687 (math-add (math-sqr halfb)
2688 (math-mul (math-neg c) a))))))
2693 (math-solve-get-sign
2695 (list 'calcFunc-sqrt
2696 (math-add (math-sqr b)
2697 (math-mul 4 (math-mul (math-neg c) a)))))))
2701 (defun math-solve-cubic (var d c b a)
2702 (let* ((p (math-div b a))
2706 (aa (math-sub q (math-div psqr 3)))
2708 (math-div (math-sub (math-mul 2 (math-mul psqr p))
2709 (math-mul 9 (math-mul p q)))
2713 (math-try-solve-for (math-pow (math-add var (math-div p 3)) 3)
2714 (math-neg bb) nil t)
2717 (math-mul (math-add var (math-div p 3))
2718 (math-add (math-sqr (math-add var (math-div p 3)))
2721 (setq m (math-mul 2 (list 'calcFunc-sqrt (math-div aa -3))))
2730 (math-sub (list 'calcFunc-arccos
2731 (math-div (math-mul 3 bb)
2735 (math-add 1 (math-solve-get-int
2738 calc-symbolic-mode))))
2743 (defun math-solve-quartic (var d c b a aa)
2744 (setq a (math-div a aa))
2745 (setq b (math-div b aa))
2746 (setq c (math-div c aa))
2747 (setq d (math-div d aa))
2750 (let* ((asqr (math-sqr a))
2751 (asqr4 (math-div asqr 4))
2752 (y (let ((math-solve-full nil)
2754 (math-solve-cubic math-solve-var
2756 (math-mul 4 (math-mul b d))
2759 (math-sub (math-mul a c)
2763 (rsqr (math-add (math-sub asqr4 b) y))
2764 (r (list 'calcFunc-sqrt rsqr))
2765 (sign1 (math-solve-get-sign 1))
2766 (de (list 'calcFunc-sqrt
2768 (math-sub (math-mul 3 asqr4)
2770 (if (Math-zerop rsqr)
2774 (list 'calcFunc-sqrt
2775 (math-sub (math-sqr y)
2781 (math-mul 4 (math-mul a b))
2787 (math-sub (math-add (math-mul sign1 (math-div r 2))
2788 (math-solve-get-sign (math-div de 2)))
2792 (defvar math-symbolic-solve nil)
2793 (defvar math-int-coefs nil)
2795 ;; The variable math-int-threshold is local to math-poly-all-roots,
2796 ;; but is used by math-poly-newton-root.
2797 (defvar math-int-threshold)
2798 ;; The variables math-int-scale, math-int-factors and math-double-roots
2799 ;; are local to math-poly-all-roots, but are used by math-poly-integer-root.
2800 (defvar math-int-scale)
2802 (defun math-poly-all-roots (var p &optional math-factoring)
2804 (let* ((math-symbolic-solve calc-symbolic-mode)
2806 (deg (1- (length p)))
2807 (orig-p (reverse p))
2808 (math-int-coefs nil)
2809 (math-int-scale nil)
2810 (math-double-roots nil)
2811 (math-int-factors nil)
2812 (math-int-threshold nil)
2814 ;; If rational coefficients, look for exact rational factors.
2815 (while (and pp (Math-ratp (car pp)))
2818 (if (or math-factoring math-symbolic-solve)
2820 (let ((lead (car orig-p))
2821 (calc-prefer-frac t)
2822 (scale (apply 'math-lcm-denoms p)))
2823 (setq math-int-scale (math-abs (math-mul scale lead))
2824 math-int-threshold (math-div '(float 5 -2) math-int-scale)
2825 math-int-coefs (cdr (math-div (cons 'vec orig-p) lead)))))
2827 (let ((calc-prefer-frac nil)
2828 (calc-symbolic-mode nil)
2830 (def-p (copy-sequence orig-p)))
2832 (if (Math-numberp (car pp))
2835 (while (> deg (if math-symbolic-solve 2 4))
2836 (let* ((x (math-poly-any-root def-p '(float 0 0) nil))
2838 (if (and (eq (car-safe x) 'cplx)
2839 (math-nearly-zerop (nth 2 x) (nth 1 x)))
2840 (setq x (calcFunc-re x)))
2842 (setq roots (cons x roots)))
2843 (or (math-numberp x)
2844 (setq x (math-evaluate-expr x)))
2847 (while (setq pp (cdr pp))
2850 (setq b (math-add (math-mul x b) c)))
2851 (setq def-p (cdr def-p)
2853 (setq p (reverse def-p))))
2855 (let ((math-solve-var '(var DUMMY var-DUMMY))
2856 (math-solve-sign nil)
2857 (math-solve-ranges nil)
2858 (math-solve-full 'all))
2859 (if (= (length p) (length math-int-coefs))
2860 (setq p (reverse math-int-coefs)))
2861 (setq roots (append (cdr (apply (cond ((= deg 2)
2862 'math-solve-quadratic)
2866 'math-solve-quartic))
2870 (setq roots (cons (math-div (math-neg (car p)) (nth 1 p))
2875 (math-poly-integer-root (car roots))
2876 (setq roots (cdr roots)))
2877 (list math-int-factors (nreverse math-int-coefs) math-int-scale))
2878 (let ((vec nil) res)
2880 (let ((root (car roots))
2881 (math-solve-full (and math-solve-full 'all)))
2882 (if (math-floatp root)
2883 (setq root (math-poly-any-root orig-p root t)))
2884 (setq vec (append vec
2885 (cdr (or (math-try-solve-for var root nil t)
2886 (throw 'ouch nil))))))
2887 (setq roots (cdr roots)))
2888 (setq vec (cons 'vec (nreverse vec)))
2889 (if math-symbolic-solve
2890 (setq vec (math-normalize vec)))
2891 (if (eq math-solve-full t)
2892 (list 'calcFunc-subscr
2894 (math-solve-get-int 1 (1- (length orig-p)) 1))
2897 (defun math-lcm-denoms (&rest fracs)
2900 (if (eq (car-safe (car fracs)) 'frac)
2901 (setq den (calcFunc-lcm den (nth 2 (car fracs)))))
2902 (setq fracs (cdr fracs)))
2905 (defun math-poly-any-root (p x polish) ; p is a reverse poly coeff list
2906 (let* ((newt (if (math-zerop x)
2907 (math-poly-newton-root
2908 p '(cplx (float 123 -6) (float 1 -4)) 4)
2909 (math-poly-newton-root p x 4)))
2910 (res (if (math-zerop (cdr newt))
2912 (if (and (math-lessp (cdr newt) '(float 1 -3)) (not polish))
2913 (setq newt (math-poly-newton-root p (car newt) 30)))
2914 (if (math-zerop (cdr newt))
2916 (math-poly-laguerre-root p x polish)))))
2917 (and math-symbolic-solve (math-floatp res)
2921 (defun math-poly-newton-root (p x iters)
2922 (let* ((calc-prefer-frac nil)
2923 (calc-symbolic-mode nil)
2924 (try-integer math-int-coefs)
2926 (while (and (> (setq iters (1- iters)) 0)
2928 (math-working "newton" x)
2931 (while (setq pp (cdr pp))
2932 (setq d (math-add (math-mul x d) b)
2933 b (math-add (math-mul x b) (car pp))))
2934 (not (math-zerop d)))
2936 (setq dx (math-div b d)
2939 (let ((adx (math-abs-approx dx)))
2940 (and (math-lessp adx math-int-threshold)
2941 (let ((iroot (math-poly-integer-root x)))
2944 (setq try-integer nil))))))
2945 (or (not (or (eq dx 0)
2946 (math-nearly-zerop dx (math-abs-approx x))))
2947 (progn (setq dx 0) nil)))))
2948 (cons x (if (math-zerop x)
2949 1 (math-div (math-abs-approx dx) (math-abs-approx x))))))
2951 (defun math-poly-integer-root (x)
2952 (and (math-lessp (calcFunc-xpon (math-abs-approx x)) calc-internal-prec)
2954 (let* ((calc-prefer-frac t)
2955 (xre (calcFunc-re x))
2956 (xim (calcFunc-im x))
2957 (xresq (math-sqr xre))
2958 (ximsq (math-sqr xim)))
2959 (if (math-lessp ximsq (calcFunc-scf xresq -1))
2960 ;; Look for linear factor
2961 (let* ((rnd (math-div (math-round (math-mul xre math-int-scale))
2963 (icp math-int-coefs)
2966 (while (setq icp (cdr icp))
2967 (setq newcoef (cons rem newcoef)
2968 rem (math-add (car icp)
2969 (math-mul rem rnd))))
2970 (and (math-zerop rem)
2972 (setq math-int-coefs (nreverse newcoef)
2973 math-int-factors (cons (list (math-neg rnd))
2976 ;; Look for irreducible quadratic factor
2977 (let* ((rnd1 (math-div (math-round
2978 (math-mul xre (math-mul -2 math-int-scale)))
2980 (sqscale (math-sqr math-int-scale))
2981 (rnd0 (math-div (math-round (math-mul (math-add xresq ximsq)
2984 (rem1 (car math-int-coefs))
2985 (icp (cdr math-int-coefs))
2988 (found (assoc (list rnd0 rnd1 (math-posp xim))
2992 (setq math-double-roots (delq found math-double-roots)
2994 (while (setq icp (cdr icp))
2996 newcoef (cons rem1 newcoef)
2997 rem1 (math-sub rem0 (math-mul this rnd1))
2998 rem0 (math-sub (car icp) (math-mul this rnd0)))))
2999 (and (math-zerop rem0)
3001 (let ((aa (math-div rnd1 -2)))
3002 (or found (setq math-int-coefs (reverse newcoef)
3003 math-double-roots (cons (list
3008 math-int-factors (cons (cons rnd0 rnd1)
3011 (let ((calc-symbolic-mode math-symbolic-solve))
3012 (math-mul (math-sqrt (math-sub (math-sqr aa)
3014 (if (math-negp xim) -1 1)))))))))))
3016 ;;; The following routine is from Numerical Recipes, section 9.5.
3017 (defun math-poly-laguerre-root (p x polish)
3018 (let* ((calc-prefer-frac nil)
3019 (calc-symbolic-mode nil)
3022 (try-newt (not polish))
3026 (and (or (< (setq iters (1+ iters)) 50)
3027 (math-reject-arg x "*Laguerre's method failed to converge"))
3028 (let ((err (math-abs-approx (car p)))
3029 (abx (math-abs-approx x))
3033 (while (setq pp (cdr pp))
3034 (setq f (math-add (math-mul x f) d)
3035 d (math-add (math-mul x d) b)
3036 b (math-add (math-mul x b) (car pp))
3037 err (math-add (math-abs-approx b) (math-mul abx err))))
3038 (math-lessp (calcFunc-scf err (- -2 calc-internal-prec))
3039 (math-abs-approx b)))
3040 (or (not (math-zerop d))
3041 (not (math-zerop f))
3043 (setq x (math-pow (math-neg b) (list 'frac 1 m)))
3045 (let* ((g (math-div d b))
3047 (h (math-sub g2 (math-mul 2 (math-div f b))))
3049 (math-mul (1- m) (math-sub (math-mul m h) g2))))
3050 (gp (math-add g sq))
3051 (gm (math-sub g sq)))
3052 (if (math-lessp (calcFunc-abssqr gp) (calcFunc-abssqr gm))
3054 (setq dx (math-div m gp)
3057 (math-lessp (math-abs-approx dx)
3058 (calcFunc-scf (math-abs-approx x) -3)))
3059 (let ((newt (math-poly-newton-root p x1 7)))
3062 (if (math-zerop (cdr newt))
3063 (setq x (car newt) x1 x)
3064 (if (math-lessp (cdr newt) '(float 1 -6))
3065 (let ((newt2 (math-poly-newton-root
3067 (if (math-zerop (cdr newt2))
3068 (setq x (car newt2) x1 x)
3069 (setq x (car newt))))))))
3071 (math-nearly-equal x x1))))
3072 (let ((cdx (math-abs-approx dx)))
3077 (math-lessp cdx dxold)
3080 (let ((digs (calcFunc-xpon
3081 (math-div (math-abs-approx x) cdx))))
3083 "*Could not attain full precision")
3085 (let ((calc-internal-prec (max 3 digs)))
3086 (setq x (math-normalize x))))))
3090 (math-lessp (calcFunc-scf (math-abs-approx x)
3091 (- calc-internal-prec))
3093 (or (and (math-floatp x)
3094 (math-poly-integer-root x))
3097 (defun math-solve-above-dummy (x)
3098 (and (not (Math-primp x))
3099 (if (and (equal (nth 1 x) '(var SOLVEDUM SOLVEDUM))
3103 (while (and (setq x (cdr x))
3104 (not (setq res (math-solve-above-dummy (car x))))))
3107 (defun math-solve-find-root-term (x neg) ; sets "t2", "t3"
3108 (if (math-solve-find-root-in-prod x)
3111 (and (memq (car-safe x) '(+ -))
3112 (or (math-solve-find-root-term (nth 1 x) neg)
3113 (math-solve-find-root-term (nth 2 x)
3114 (if (eq (car x) '-) (not neg) neg))))))
3116 (defun math-solve-find-root-in-prod (x)
3118 (math-expr-contains x math-solve-var)
3119 (or (and (eq (car x) 'calcFunc-sqrt)
3121 (and (eq (car x) '^)
3122 (or (and (memq (math-quarter-integer (nth 2 x)) '(1 2 3))
3124 (and (eq (car-safe (nth 2 x)) 'frac)
3125 (eq (nth 2 (nth 2 x)) 3)
3127 (and (memq (car x) '(* /))
3128 (or (and (not (math-expr-contains (nth 1 x) math-solve-var))
3129 (math-solve-find-root-in-prod (nth 2 x)))
3130 (and (not (math-expr-contains (nth 2 x) math-solve-var))
3131 (math-solve-find-root-in-prod (nth 1 x))))))))
3133 ;; The variable math-solve-vars is local to math-solve-system,
3134 ;; but is used by math-solve-system-rec.
3135 (defvar math-solve-vars)
3137 ;; The variable math-solve-simplifying is local to math-solve-system
3138 ;; and math-solve-system-rec, but is used by math-solve-system-subst.
3140 (defun math-solve-system (exprs math-solve-vars math-solve-full)
3141 (setq exprs (mapcar 'list (if (Math-vectorp exprs)
3144 math-solve-vars (if (Math-vectorp math-solve-vars)
3145 (cdr math-solve-vars)
3146 (list math-solve-vars)))
3147 (or (let ((math-solve-simplifying nil))
3148 (math-solve-system-rec exprs math-solve-vars nil))
3149 (let ((math-solve-simplifying t))
3150 (math-solve-system-rec exprs math-solve-vars nil))))
3152 ;;; The following backtracking solver works by choosing a variable
3153 ;;; and equation, and trying to solve the equation for the variable.
3154 ;;; If it succeeds it calls itself recursively with that variable and
3155 ;;; equation removed from their respective lists, and with the solution
3156 ;;; added to solns as well as being substituted into all existing
3157 ;;; equations. The algorithm terminates when any solution path
3158 ;;; manages to remove all the variables from var-list.
3160 ;;; To support calcFunc-roots, entries in eqn-list and solns are
3161 ;;; actually lists of equations.
3163 ;; The variables math-solve-system-res and math-solve-system-vv are
3164 ;; local to math-solve-system-rec, but are used by math-solve-system-subst.
3165 (defvar math-solve-system-vv)
3166 (defvar math-solve-system-res)
3169 (defun math-solve-system-rec (eqn-list var-list solns)
3172 (math-solve-system-res nil))
3174 ;; Try each variable in turn.
3178 (let* ((math-solve-system-vv (car v))
3180 (elim (eq (car-safe math-solve-system-vv) 'calcFunc-elim)))
3182 (setq math-solve-system-vv (nth 1 math-solve-system-vv)))
3184 ;; Try each equation in turn.
3191 (setq math-solve-system-res nil)
3193 ;; Try to solve for math-solve-system-vv the list of equations e2.
3195 (setq res2 (or (and (eq (car e2) eprev)
3197 (math-solve-for (car e2) 0
3198 math-solve-system-vv
3200 (setq eprev (car e2)
3201 math-solve-system-res (cons (if (eq math-solve-full 'all)
3204 math-solve-system-res)
3207 (setq math-solve-system-res nil)
3209 ;; Found a solution. Now try other variables.
3210 (setq math-solve-system-res (nreverse math-solve-system-res)
3211 math-solve-system-res (math-solve-system-rec
3213 'math-solve-system-subst
3215 (copy-sequence eqn-list)))
3216 (delq (car v) (copy-sequence var-list))
3217 (let ((math-solve-simplifying nil)
3223 (math-solve-system-subst
3229 math-solve-system-vv
3230 (apply 'append math-solve-system-res))
3232 (not math-solve-system-res))))
3234 (not math-solve-system-res)))
3236 math-solve-system-res)
3238 ;; Eliminated all variables, so now put solution into the proper format.
3239 (setq solns (sort solns
3242 (not (memq (car x) (memq (car y) math-solve-vars)))))))
3243 (if (eq math-solve-full 'all)
3248 (mapcar (function (lambda (x) (cons 'vec (cdr x)))) solns)
3249 (mapcar (function (lambda (x) (cons 'vec x))) eqn-list)))))
3253 (mapcar (function (lambda (x) (cons 'calcFunc-eq x))) solns)
3254 (mapcar 'car eqn-list)))))))
3256 (defun math-solve-system-subst (x) ; uses "res" and "v"
3258 (res2 math-solve-system-res))
3260 (setq accum (nconc accum
3263 (if math-solve-simplifying
3266 (car x) math-solve-system-vv r))
3268 (car x) math-solve-system-vv r))))
3275 ;; calc-command-flags is declared in calc.el
3276 (defvar calc-command-flags)
3278 (defun math-get-from-counter (name)
3279 (let ((ctr (assq name calc-command-flags)))
3281 (setcdr ctr (1+ (cdr ctr)))
3282 (setq ctr (cons name 1)
3283 calc-command-flags (cons ctr calc-command-flags)))
3286 (defvar var-GenCount)
3288 (defun math-solve-get-sign (val)
3289 (setq val (math-simplify val))
3290 (if (and (eq (car-safe val) '*)
3291 (Math-numberp (nth 1 val)))
3292 (list '* (nth 1 val) (math-solve-get-sign (nth 2 val)))
3293 (and (eq (car-safe val) 'calcFunc-sqrt)
3294 (eq (car-safe (nth 1 val)) '^)
3295 (setq val (math-normalize (list '^
3297 (math-div (nth 2 (nth 1 val)) 2)))))
3299 (if (and (calc-var-value 'var-GenCount)
3300 (Math-natnump var-GenCount)
3301 (not (eq math-solve-full 'all)))
3303 (math-mul (list 'calcFunc-as var-GenCount) val)
3304 (setq var-GenCount (math-add var-GenCount 1))
3305 (calc-refresh-evaltos 'var-GenCount))
3306 (let* ((var (concat "s" (int-to-string (math-get-from-counter 'solve-sign))))
3307 (var2 (list 'var (intern var) (intern (concat "var-" var)))))
3308 (if (eq math-solve-full 'all)
3309 (setq math-solve-ranges (cons (list var2 1 -1)
3310 math-solve-ranges)))
3311 (math-mul var2 val)))
3312 (calc-record-why "*Choosing positive solution")
3315 (defun math-solve-get-int (val &optional range first)
3317 (if (and (calc-var-value 'var-GenCount)
3318 (Math-natnump var-GenCount)
3319 (not (eq math-solve-full 'all)))
3321 (math-mul val (list 'calcFunc-an var-GenCount))
3322 (setq var-GenCount (math-add var-GenCount 1))
3323 (calc-refresh-evaltos 'var-GenCount))
3324 (let* ((var (concat "n" (int-to-string
3325 (math-get-from-counter 'solve-int))))
3326 (var2 (list 'var (intern var) (intern (concat "var-" var)))))
3327 (if (and range (eq math-solve-full 'all))
3328 (setq math-solve-ranges (cons (cons var2
3329 (cdr (calcFunc-index
3330 range (or first 0))))
3331 math-solve-ranges)))
3332 (math-mul val var2)))
3333 (calc-record-why "*Choosing 0 for arbitrary integer in solution")
3336 (defun math-solve-sign (sign expr)
3338 (let ((s1 (math-possible-signs expr)))
3339 (cond ((memq s1 '(4 6))
3344 (defun math-looks-evenp (expr)
3345 (if (Math-integerp expr)
3347 (if (memq (car expr) '(* /))
3348 (math-looks-evenp (nth 1 expr)))))
3350 (defun math-solve-for (lhs rhs math-solve-var math-solve-full &optional sign)
3351 (if (math-expr-contains rhs math-solve-var)
3352 (math-solve-for (math-sub lhs rhs) 0 math-solve-var math-solve-full)
3353 (and (math-expr-contains lhs math-solve-var)
3354 (math-with-extra-prec 1
3355 (let* ((math-poly-base-variable math-solve-var)
3356 (res (math-try-solve-for lhs rhs sign)))
3357 (if (and (eq math-solve-full 'all)
3358 (math-known-realp math-solve-var))
3359 (let ((old-len (length res))
3364 (and (not (memq (car-safe x)
3368 new-len (length res))
3369 (if (< new-len old-len)
3370 (calc-record-why (if (= new-len 1)
3371 "*All solutions were complex"
3373 "*Omitted %d complex solutions"
3374 (- old-len new-len)))))))
3377 (defun math-solve-eqn (expr var full)
3378 (if (memq (car-safe expr) '(calcFunc-neq calcFunc-lt calcFunc-gt
3379 calcFunc-leq calcFunc-geq))
3380 (let ((res (math-solve-for (cons '- (cdr expr))
3382 (if (eq (car expr) 'calcFunc-neq) nil 1))))
3384 (if (eq math-solve-sign 1)
3385 (list (car expr) var res)
3386 (if (eq math-solve-sign -1)
3387 (list (car expr) res var)
3388 (or (eq (car expr) 'calcFunc-neq)
3390 "*Can't determine direction of inequality"))
3391 (and (memq (car expr) '(calcFunc-neq calcFunc-lt calcFunc-gt))
3392 (list 'calcFunc-neq var res))))))
3393 (let ((res (math-solve-for expr 0 var full)))
3395 (list 'calcFunc-eq var res)))))
3397 (defun math-reject-solution (expr var func)
3398 (if (math-expr-contains expr var)
3399 (or (equal (car calc-next-why) '(* "Unable to find a symbolic solution"))
3400 (calc-record-why "*Unable to find a solution")))
3401 (list func expr var))
3403 (defun calcFunc-solve (expr var)
3404 (or (if (or (Math-vectorp expr) (Math-vectorp var))
3405 (math-solve-system expr var nil)
3406 (math-solve-eqn expr var nil))
3407 (math-reject-solution expr var 'calcFunc-solve)))
3409 (defun calcFunc-fsolve (expr var)
3410 (or (if (or (Math-vectorp expr) (Math-vectorp var))
3411 (math-solve-system expr var t)
3412 (math-solve-eqn expr var t))
3413 (math-reject-solution expr var 'calcFunc-fsolve)))
3415 (defun calcFunc-roots (expr var)
3416 (let ((math-solve-ranges nil))
3417 (or (if (or (Math-vectorp expr) (Math-vectorp var))
3418 (math-solve-system expr var 'all)
3419 (math-solve-for expr 0 var 'all))
3420 (math-reject-solution expr var 'calcFunc-roots))))
3422 (defun calcFunc-finv (expr var)
3423 (let ((res (math-solve-for expr math-integ-var var nil)))
3425 (math-normalize (math-expr-subst res math-integ-var var))
3426 (math-reject-solution expr var 'calcFunc-finv))))
3428 (defun calcFunc-ffinv (expr var)
3429 (let ((res (math-solve-for expr math-integ-var var t)))
3431 (math-normalize (math-expr-subst res math-integ-var var))
3432 (math-reject-solution expr var 'calcFunc-finv))))
3435 (put 'calcFunc-inv 'math-inverse
3436 (function (lambda (x) (math-div 1 x))))
3437 (put 'calcFunc-inv 'math-inverse-sign -1)
3439 (put 'calcFunc-sqrt 'math-inverse
3440 (function (lambda (x) (math-sqr x))))
3442 (put 'calcFunc-conj 'math-inverse
3443 (function (lambda (x) (list 'calcFunc-conj x))))
3445 (put 'calcFunc-abs 'math-inverse
3446 (function (lambda (x) (math-solve-get-sign x))))
3448 (put 'calcFunc-deg 'math-inverse
3449 (function (lambda (x) (list 'calcFunc-rad x))))
3450 (put 'calcFunc-deg 'math-inverse-sign 1)
3452 (put 'calcFunc-rad 'math-inverse
3453 (function (lambda (x) (list 'calcFunc-deg x))))
3454 (put 'calcFunc-rad 'math-inverse-sign 1)
3456 (put 'calcFunc-ln 'math-inverse
3457 (function (lambda (x) (list 'calcFunc-exp x))))
3458 (put 'calcFunc-ln 'math-inverse-sign 1)
3460 (put 'calcFunc-log10 'math-inverse
3461 (function (lambda (x) (list 'calcFunc-exp10 x))))
3462 (put 'calcFunc-log10 'math-inverse-sign 1)
3464 (put 'calcFunc-lnp1 'math-inverse
3465 (function (lambda (x) (list 'calcFunc-expm1 x))))
3466 (put 'calcFunc-lnp1 'math-inverse-sign 1)
3468 (put 'calcFunc-exp 'math-inverse
3469 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-ln x))
3471 (math-mul '(var pi var-pi)
3473 '(var i var-i))))))))
3474 (put 'calcFunc-exp 'math-inverse-sign 1)
3476 (put 'calcFunc-expm1 'math-inverse
3477 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-lnp1 x))
3479 (math-mul '(var pi var-pi)
3481 '(var i var-i))))))))
3482 (put 'calcFunc-expm1 'math-inverse-sign 1)
3484 (put 'calcFunc-sin 'math-inverse
3485 (function (lambda (x) (let ((n (math-solve-get-int 1)))
3486 (math-add (math-mul (math-normalize
3487 (list 'calcFunc-arcsin x))
3489 (math-mul (math-half-circle t)
3492 (put 'calcFunc-cos 'math-inverse
3493 (function (lambda (x) (math-add (math-solve-get-sign
3495 (list 'calcFunc-arccos x)))
3497 (math-full-circle t))))))
3499 (put 'calcFunc-tan 'math-inverse
3500 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-arctan x))
3502 (math-half-circle t))))))
3504 (put 'calcFunc-arcsin 'math-inverse
3505 (function (lambda (x) (math-normalize (list 'calcFunc-sin x)))))
3507 (put 'calcFunc-arccos 'math-inverse
3508 (function (lambda (x) (math-normalize (list 'calcFunc-cos x)))))
3510 (put 'calcFunc-arctan 'math-inverse
3511 (function (lambda (x) (math-normalize (list 'calcFunc-tan x)))))
3513 (put 'calcFunc-sinh 'math-inverse
3514 (function (lambda (x) (let ((n (math-solve-get-int 1)))
3515 (math-add (math-mul (math-normalize
3516 (list 'calcFunc-arcsinh x))
3518 (math-mul (math-half-circle t)
3522 (put 'calcFunc-sinh 'math-inverse-sign 1)
3524 (put 'calcFunc-cosh 'math-inverse
3525 (function (lambda (x) (math-add (math-solve-get-sign
3527 (list 'calcFunc-arccosh x)))
3528 (math-mul (math-full-circle t)
3530 '(var i var-i)))))))
3532 (put 'calcFunc-tanh 'math-inverse
3533 (function (lambda (x) (math-add (math-normalize
3534 (list 'calcFunc-arctanh x))
3535 (math-mul (math-half-circle t)
3537 '(var i var-i)))))))
3538 (put 'calcFunc-tanh 'math-inverse-sign 1)
3540 (put 'calcFunc-arcsinh 'math-inverse
3541 (function (lambda (x) (math-normalize (list 'calcFunc-sinh x)))))
3542 (put 'calcFunc-arcsinh 'math-inverse-sign 1)
3544 (put 'calcFunc-arccosh 'math-inverse
3545 (function (lambda (x) (math-normalize (list 'calcFunc-cosh x)))))
3547 (put 'calcFunc-arctanh 'math-inverse
3548 (function (lambda (x) (math-normalize (list 'calcFunc-tanh x)))))
3549 (put 'calcFunc-arctanh 'math-inverse-sign 1)
3553 (defun calcFunc-taylor (expr var num)
3554 (let ((x0 0) (v var))
3555 (if (memq (car-safe var) '(+ - calcFunc-eq))
3556 (setq x0 (if (eq (car var) '+) (math-neg (nth 2 var)) (nth 2 var))
3558 (or (and (eq (car-safe v) 'var)
3559 (math-expr-contains expr v)
3561 (let ((accum (math-expr-subst expr v x0))
3562 (var2 (if (eq (car var) 'calcFunc-eq)
3568 (while (and (<= (setq n (1+ n)) num)
3569 (setq fprime (calcFunc-deriv fprime v nil t)))
3570 (setq fprime (math-simplify fprime)
3571 nfac (math-mul nfac n)
3572 accum (math-add accum
3573 (math-div (math-mul (math-pow var2 n)
3578 (math-normalize accum))))
3579 (list 'calcFunc-taylor expr var num))))
3581 ;;; arch-tag: f2932ec8-dd63-418b-a542-11a644b9d4c4
3582 ;;; calcalg2.el ends here