1 ;; Calculator for GNU Emacs, part II [calc-alg.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
5 ;; This file is part of GNU Emacs.
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY. No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing. Refer to the GNU Emacs General Public
12 ;; License for full details.
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License. A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities. It should be in a
19 ;; file named COPYING. Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
24 ;; This file is autoloaded from calc-ext.el.
29 (defun calc-Need-calc-alg () nil)
34 (defun calc-alg-evaluate (arg)
37 (calc-with-default-simplification
38 (let ((math-simplify-only nil))
39 (calc-modify-simplify-mode arg)
40 (calc-enter-result 1 "dsmp" (calc-top 1)))))
43 (defun calc-modify-simplify-mode (arg)
44 (if (= (math-abs arg) 2)
45 (setq calc-simplify-mode 'alg)
46 (if (>= (math-abs arg) 3)
47 (setq calc-simplify-mode 'ext)))
49 (setq calc-simplify-mode (list calc-simplify-mode)))
52 (defun calc-simplify ()
55 (calc-with-default-simplification
56 (calc-enter-result 1 "simp" (math-simplify (calc-top-n 1)))))
59 (defun calc-simplify-extended ()
62 (calc-with-default-simplification
63 (calc-enter-result 1 "esmp" (math-simplify-extended (calc-top-n 1)))))
66 (defun calc-expand-formula (arg)
69 (calc-with-default-simplification
70 (let ((math-simplify-only nil))
71 (calc-modify-simplify-mode arg)
72 (calc-enter-result 1 "expf"
74 (let ((math-expand-formulas t))
76 (let ((top (calc-top-n 1)))
77 (or (math-expand-formula top)
81 (defun calc-factor (arg)
84 (calc-unary-op "fctr" (if (calc-is-hyperbolic)
85 'calcFunc-factors 'calcFunc-factor)
89 (defun calc-expand (n)
92 (calc-enter-result 1 "expa"
93 (append (list 'calcFunc-expand
95 (and n (list (prefix-numeric-value n))))))
98 (defun calc-collect (&optional var)
99 (interactive "sCollect terms involving: ")
101 (if (or (equal var "") (equal var "$") (null var))
102 (calc-enter-result 2 "clct" (cons 'calcFunc-collect
103 (calc-top-list-n 2)))
104 (let ((var (math-read-expr var)))
105 (if (eq (car-safe var) 'error)
106 (error "Bad format in expression: %s" (nth 1 var)))
107 (calc-enter-result 1 "clct" (list 'calcFunc-collect
112 (defun calc-apart (arg)
115 (calc-unary-op "aprt" 'calcFunc-apart arg))
118 (defun calc-normalize-rat (arg)
121 (calc-unary-op "nrat" 'calcFunc-nrat arg))
124 (defun calc-poly-gcd (arg)
127 (calc-binary-op "pgcd" 'calcFunc-pgcd arg))
130 (defun calc-poly-div (arg)
133 (setq calc-poly-div-remainder nil)
134 (calc-binary-op "pdiv" 'calcFunc-pdiv arg)
135 (if (and calc-poly-div-remainder (null arg))
137 (calc-clear-command-flag 'clear-message)
138 (calc-record calc-poly-div-remainder "prem")
139 (if (not (Math-zerop calc-poly-div-remainder))
140 (message "(Remainder was %s)"
141 (math-format-flat-expr calc-poly-div-remainder 0))
142 (message "(No remainder)")))))
145 (defun calc-poly-rem (arg)
148 (calc-binary-op "prem" 'calcFunc-prem arg))
151 (defun calc-poly-div-rem (arg)
154 (if (calc-is-hyperbolic)
155 (calc-binary-op "pdvr" 'calcFunc-pdivide arg)
156 (calc-binary-op "pdvr" 'calcFunc-pdivrem arg)))
159 (defun calc-substitute (&optional oldname newname)
160 (interactive "sSubstitute old: ")
162 (let (old new (num 1) expr)
163 (if (or (equal oldname "") (equal oldname "$") (null oldname))
164 (setq new (calc-top-n 1)
169 (progn (calc-unread-command ?\C-a)
170 (setq newname (read-string (concat "Substitute old: "
174 (if (or (equal newname "") (equal newname "$") (null newname))
175 (setq new (calc-top-n 1)
178 (setq new (if (stringp newname) (math-read-expr newname) newname))
179 (if (eq (car-safe new) 'error)
180 (error "Bad format in expression: %s" (nth 1 new)))
181 (setq expr (calc-top-n 1)))
182 (setq old (if (stringp oldname) (math-read-expr oldname) oldname))
183 (if (eq (car-safe old) 'error)
184 (error "Bad format in expression: %s" (nth 1 old)))
185 (or (math-expr-contains expr old)
186 (error "No occurrences found.")))
187 (calc-enter-result num "sbst" (math-expr-subst expr old new))))
191 (defun calc-has-rules (name)
192 (setq name (calc-var-value name))
194 (memq (car name) '(vec calcFunc-assign calcFunc-condition))
198 (defun math-recompile-eval-rules ()
199 (setq math-eval-rules-cache (and (calc-has-rules 'var-EvalRules)
200 (math-compile-rewrites
201 '(var EvalRules var-EvalRules)))
202 math-eval-rules-cache-other (assq nil math-eval-rules-cache)
203 math-eval-rules-cache-tag (calc-var-value 'var-EvalRules))
207 ;;; Try to expand a formula according to its definition.
208 (defun math-expand-formula (expr)
211 (or (get (car expr) 'calc-user-defn)
212 (get (car expr) 'math-expandable))
213 (let ((res (let ((math-expand-formulas t))
214 (apply (car expr) (cdr expr)))))
215 (and (not (eq (car-safe res) (car expr)))
222 ;;; True if A comes before B in a canonical ordering of expressions. [P X X]
223 (defun math-beforep (a b) ; [Public]
224 (cond ((and (Math-realp a) (Math-realp b))
225 (let ((comp (math-compare a b)))
229 (> (length (memq (car-safe a)
230 '(bigneg nil bigpos frac float)))
231 (length (memq (car-safe b)
232 '(bigneg nil bigpos frac float))))))))
233 ((equal b '(neg (var inf var-inf))) nil)
234 ((equal a '(neg (var inf var-inf))) t)
235 ((equal a '(var inf var-inf)) nil)
236 ((equal b '(var inf var-inf)) t)
238 (if (and (eq (car-safe b) 'intv) (math-intv-constp b))
239 (if (or (math-beforep a (nth 2 b)) (Math-equal a (nth 2 b)))
244 (if (and (eq (car-safe a) 'intv) (math-intv-constp a))
245 (if (math-beforep (nth 2 a) b)
249 ((and (eq (car a) 'intv) (eq (car b) 'intv)
250 (math-intv-constp a) (math-intv-constp b))
251 (let ((comp (math-compare (nth 2 a) (nth 2 b))))
252 (cond ((eq comp -1) t)
254 ((and (memq (nth 1 a) '(2 3)) (memq (nth 1 b) '(0 1))) t)
255 ((and (memq (nth 1 a) '(0 1)) (memq (nth 1 b) '(2 3))) nil)
256 ((eq (setq comp (math-compare (nth 3 a) (nth 3 b))) -1) t)
258 ((and (memq (nth 1 a) '(0 2)) (memq (nth 1 b) '(1 3))) t)
260 ((not (eq (not (Math-objectp a)) (not (Math-objectp b))))
263 (if (eq (car b) 'var)
264 (string-lessp (symbol-name (nth 1 a)) (symbol-name (nth 1 b)))
265 (not (Math-numberp b))))
266 ((eq (car b) 'var) (Math-numberp a))
267 ((eq (car a) (car b))
268 (while (and (setq a (cdr a) b (cdr b)) a
269 (equal (car a) (car b))))
272 (math-beforep (car a) (car b)))))
273 (t (string-lessp (symbol-name (car a)) (symbol-name (car b)))))
277 (defun math-simplify-extended (a)
278 (let ((math-living-dangerously t))
281 (fset 'calcFunc-esimplify (symbol-function 'math-simplify-extended))
283 (defun math-simplify (top-expr)
284 (let ((math-simplifying t)
285 (top-only (consp calc-simplify-mode))
286 (simp-rules (append (and (calc-has-rules 'var-AlgSimpRules)
287 '((var AlgSimpRules var-AlgSimpRules)))
288 (and math-living-dangerously
289 (calc-has-rules 'var-ExtSimpRules)
290 '((var ExtSimpRules var-ExtSimpRules)))
291 (and math-simplifying-units
292 (calc-has-rules 'var-UnitSimpRules)
293 '((var UnitSimpRules var-UnitSimpRules)))
294 (and math-integrating
295 (calc-has-rules 'var-IntegSimpRules)
296 '((var IntegSimpRules var-IntegSimpRules)))))
299 (let ((r simp-rules))
300 (setq res (math-simplify-step (math-normalize top-expr))
301 calc-simplify-mode '(nil)
302 top-expr (math-normalize res))
304 (setq top-expr (math-rewrite top-expr (car r)
305 '(neg (var inf var-inf)))
307 (calc-with-default-simplification
308 (while (let ((r simp-rules))
309 (setq res (math-normalize top-expr))
311 (setq res (math-rewrite res (car r))
313 (not (equal top-expr (setq res (math-simplify-step res)))))
314 (setq top-expr res)))))
317 (fset 'calcFunc-simplify (symbol-function 'math-simplify))
319 ;;; The following has a "bug" in that if any recursive simplifications
320 ;;; occur only the first handler will be tried; this doesn't really
321 ;;; matter, since math-simplify-step is iterated to a fixed point anyway.
322 (defun math-simplify-step (a)
325 (let ((aa (if (or top-only
326 (memq (car a) '(calcFunc-quote calcFunc-condition
329 (cons (car a) (mapcar 'math-simplify-step (cdr a))))))
330 (and (symbolp (car aa))
331 (let ((handler (get (car aa) 'math-simplify)))
334 (equal (setq aa (or (funcall (car handler) aa)
337 (setq handler (cdr handler))))))
342 (defun math-need-std-simps ()
343 ;; Placeholder, to synchronize autoloading.
346 (math-defsimplify (+ -)
347 (math-simplify-plus))
349 (defun math-simplify-plus ()
350 (cond ((and (memq (car-safe (nth 1 expr)) '(+ -))
351 (Math-numberp (nth 2 (nth 1 expr)))
352 (not (Math-numberp (nth 2 expr))))
353 (let ((x (nth 2 expr))
355 (setcar (cdr (cdr expr)) (nth 2 (nth 1 expr)))
356 (setcar expr (car (nth 1 expr)))
357 (setcar (cdr (cdr (nth 1 expr))) x)
358 (setcar (nth 1 expr) op)))
359 ((and (eq (car expr) '+)
360 (Math-numberp (nth 1 expr))
361 (not (Math-numberp (nth 2 expr))))
362 (let ((x (nth 2 expr)))
363 (setcar (cdr (cdr expr)) (nth 1 expr))
364 (setcar (cdr expr) x))))
367 (while (memq (car-safe (setq aaa (nth 1 aa))) '(+ -))
368 (if (setq temp (math-combine-sum (nth 2 aaa) (nth 2 expr)
369 (eq (car aaa) '-) (eq (car expr) '-) t))
371 (setcar (cdr (cdr expr)) temp)
373 (setcar (cdr (cdr aaa)) 0)))
374 (setq aa (nth 1 aa)))
375 (if (setq temp (math-combine-sum aaa (nth 2 expr)
376 nil (eq (car expr) '-) t))
378 (setcar (cdr (cdr expr)) temp)
380 (setcar (cdr aa) 0)))
385 (math-simplify-times))
387 (defun math-simplify-times ()
388 (if (eq (car-safe (nth 2 expr)) '*)
389 (and (math-beforep (nth 1 (nth 2 expr)) (nth 1 expr))
390 (or (math-known-scalarp (nth 1 expr) t)
391 (math-known-scalarp (nth 1 (nth 2 expr)) t))
392 (let ((x (nth 1 expr)))
393 (setcar (cdr expr) (nth 1 (nth 2 expr)))
394 (setcar (cdr (nth 2 expr)) x)))
395 (and (math-beforep (nth 2 expr) (nth 1 expr))
396 (or (math-known-scalarp (nth 1 expr) t)
397 (math-known-scalarp (nth 2 expr) t))
398 (let ((x (nth 2 expr)))
399 (setcar (cdr (cdr expr)) (nth 1 expr))
400 (setcar (cdr expr) x))))
403 (safe t) (scalar (math-known-scalarp (nth 1 expr))))
404 (if (and (Math-ratp (nth 1 expr))
405 (setq temp (math-common-constant-factor (nth 2 expr))))
407 (setcar (cdr (cdr expr))
408 (math-cancel-common-factor (nth 2 expr) temp))
409 (setcar (cdr expr) (math-mul (nth 1 expr) temp))))
410 (while (and (eq (car-safe (setq aaa (nth 2 aa))) '*)
412 (if (setq temp (math-combine-prod (nth 1 expr) (nth 1 aaa) nil nil t))
414 (setcar (cdr expr) temp)
415 (setcar (cdr aaa) 1)))
416 (setq safe (or scalar (math-known-scalarp (nth 1 aaa) t))
418 (if (and (setq temp (math-combine-prod aaa (nth 1 expr) nil nil t))
421 (setcar (cdr expr) temp)
422 (setcar (cdr (cdr aa)) 1)))
423 (if (and (eq (car-safe (nth 1 expr)) 'frac)
424 (memq (nth 1 (nth 1 expr)) '(1 -1)))
425 (math-div (math-mul (nth 2 expr) (nth 1 (nth 1 expr)))
426 (nth 2 (nth 1 expr)))
431 (math-simplify-divide))
433 (defun math-simplify-divide ()
434 (let ((np (cdr expr))
436 (nn (and (or (eq (car expr) '/) (not (Math-realp (nth 2 expr))))
437 (math-common-constant-factor (nth 2 expr))))
441 (setq n (and (or (eq (car expr) '/) (not (Math-realp (nth 1 expr))))
442 (math-common-constant-factor (nth 1 expr))))
443 (if (and (eq (car-safe nn) 'frac) (eq (nth 1 nn) 1) (not n))
445 (setcar (cdr expr) (math-mul (nth 2 nn) (nth 1 expr)))
446 (setcar (cdr (cdr expr))
447 (math-cancel-common-factor (nth 2 expr) nn))
448 (if (and (math-negp nn)
449 (setq op (assq (car expr) calc-tweak-eqn-table)))
450 (setcar expr (nth 1 op))))
451 (if (and n (not (eq (setq n (math-frac-gcd n nn)) 1)))
454 (math-cancel-common-factor (nth 1 expr) n))
455 (setcar (cdr (cdr expr))
456 (math-cancel-common-factor (nth 2 expr) n))
457 (if (and (math-negp n)
458 (setq op (assq (car expr) calc-tweak-eqn-table)))
459 (setcar expr (nth 1 op))))))))
460 (if (and (eq (car-safe (car np)) '/)
461 (math-known-scalarp (nth 2 expr) t))
463 (setq np (cdr (nth 1 expr)))
464 (while (eq (car-safe (setq n (car np))) '*)
465 (and (math-known-scalarp (nth 2 n) t)
466 (math-simplify-divisor (cdr n) (cdr (cdr expr)) nil t))
467 (setq np (cdr (cdr n))))
468 (math-simplify-divisor np (cdr (cdr expr)) nil t)
470 np (cdr (cdr (nth 1 expr))))))
471 (while (eq (car-safe (setq n (car np))) '*)
472 (and (math-known-scalarp (nth 2 n) t)
473 (math-simplify-divisor (cdr n) (cdr (cdr expr)) nover t))
474 (setq np (cdr (cdr n))))
475 (math-simplify-divisor np (cdr (cdr expr)) nover t)
479 (defun math-simplify-divisor (np dp nover dover)
480 (cond ((eq (car-safe (car dp)) '/)
481 (math-simplify-divisor np (cdr (car dp)) nover dover)
482 (and (math-known-scalarp (nth 1 (car dp)) t)
483 (math-simplify-divisor np (cdr (cdr (car dp)))
485 ((or (or (eq (car expr) '/)
486 (let ((signs (math-possible-signs (car np))))
487 (or (memq signs '(1 4))
488 (and (memq (car expr) '(calcFunc-eq calcFunc-neq))
490 math-living-dangerously)))
491 (math-numberp (car np)))
494 (safe t) (scalar (math-known-scalarp n)))
495 (while (and (eq (car-safe (setq d (car dp))) '*)
497 (math-simplify-one-divisor np (cdr d))
498 (setq safe (or scalar (math-known-scalarp (nth 1 d) t))
501 (math-simplify-one-divisor np dp)))))
504 (defun math-simplify-one-divisor (np dp)
505 (if (setq temp (math-combine-prod (car np) (car dp) nover dover t))
507 (and (not (memq (car expr) '(/ calcFunc-eq calcFunc-neq)))
508 (math-known-negp (car dp))
509 (setq op (assq (car expr) calc-tweak-eqn-table))
510 (setcar expr (nth 1 op)))
511 (setcar np (if nover (math-div 1 temp) temp))
513 (and dover (not nover) (eq (car expr) '/)
514 (eq (car-safe (car dp)) 'calcFunc-sqrt)
515 (Math-integerp (nth 1 (car dp)))
517 (setcar np (math-mul (car np)
518 (list 'calcFunc-sqrt (nth 1 (car dp)))))
519 (setcar dp (nth 1 (car dp))))))
522 (defun math-common-constant-factor (expr)
523 (if (Math-realp expr)
525 (and (not (memq expr '(0 1 -1)))
527 (if (math-ratp (setq expr (math-to-simple-fraction expr)))
528 (math-common-constant-factor expr)))
529 (if (memq (car expr) '(+ - cplx sdev))
530 (let ((f1 (math-common-constant-factor (nth 1 expr)))
531 (f2 (math-common-constant-factor (nth 2 expr))))
533 (not (eq (setq f1 (math-frac-gcd f1 f2)) 1))
535 (if (memq (car expr) '(* polar))
536 (math-common-constant-factor (nth 1 expr))
537 (if (eq (car expr) '/)
538 (or (math-common-constant-factor (nth 1 expr))
539 (and (Math-integerp (nth 2 expr))
540 (list 'frac 1 (math-abs (nth 2 expr)))))))))
543 (defun math-cancel-common-factor (expr val)
544 (if (memq (car-safe expr) '(+ - cplx sdev))
546 (setcar (cdr expr) (math-cancel-common-factor (nth 1 expr) val))
547 (setcar (cdr (cdr expr)) (math-cancel-common-factor (nth 2 expr) val))
549 (if (eq (car-safe expr) '*)
550 (math-mul (math-cancel-common-factor (nth 1 expr) val) (nth 2 expr))
551 (math-div expr val)))
554 (defun math-frac-gcd (a b)
559 (if (and (Math-integerp a)
562 (and (Math-integerp a) (setq a (list 'frac a 1)))
563 (and (Math-integerp b) (setq b (list 'frac b 1)))
564 (math-make-frac (math-gcd (nth 1 a) (nth 1 b))
565 (math-gcd (nth 2 a) (nth 2 b))))))
571 (defun math-simplify-mod ()
572 (and (Math-realp (nth 2 expr))
573 (Math-posp (nth 2 expr))
574 (let ((lin (math-is-linear (nth 1 expr)))
577 (or (math-negp (car lin))
578 (not (Math-lessp (car lin) (nth 2 expr))))
581 (math-mul (nth 1 lin) (nth 2 lin))
582 (math-mod (car lin) (nth 2 expr)))
585 (not (math-equal-int (nth 1 lin) 1))
586 (math-num-integerp (nth 1 lin))
587 (math-num-integerp (nth 2 expr))
588 (setq t1 (calcFunc-gcd (nth 1 lin) (nth 2 expr)))
589 (not (math-equal-int t1 1))
594 (math-mul (math-div (nth 1 lin) t1)
596 (let ((calc-prefer-frac t))
597 (math-div (car lin) t1)))
598 (math-div (nth 2 expr) t1))))
599 (and (math-equal-int (nth 2 expr) 1)
600 (math-known-integerp (if lin
601 (math-mul (nth 1 lin) (nth 2 lin))
603 (if lin (math-mod (car lin) 1) 0)))))
606 (math-defsimplify (calcFunc-eq calcFunc-neq calcFunc-lt
607 calcFunc-gt calcFunc-leq calcFunc-geq)
608 (if (= (length expr) 3)
609 (math-simplify-ineq)))
611 (defun math-simplify-ineq ()
612 (let ((np (cdr expr))
614 (while (memq (car-safe (setq n (car np))) '(+ -))
615 (math-simplify-add-term (cdr (cdr n)) (cdr (cdr expr))
618 (math-simplify-add-term np (cdr (cdr expr)) nil (eq np (cdr expr)))
619 (math-simplify-divide)
620 (let ((signs (math-possible-signs (cons '- (cdr expr)))))
621 (or (cond ((eq (car expr) 'calcFunc-eq)
622 (or (and (eq signs 2) 1)
623 (and (memq signs '(1 4 5)) 0)))
624 ((eq (car expr) 'calcFunc-neq)
625 (or (and (eq signs 2) 0)
626 (and (memq signs '(1 4 5)) 1)))
627 ((eq (car expr) 'calcFunc-lt)
628 (or (and (eq signs 1) 1)
629 (and (memq signs '(2 4 6)) 0)))
630 ((eq (car expr) 'calcFunc-gt)
631 (or (and (eq signs 4) 1)
632 (and (memq signs '(1 2 3)) 0)))
633 ((eq (car expr) 'calcFunc-leq)
634 (or (and (eq signs 4) 0)
635 (and (memq signs '(1 2 3)) 1)))
636 ((eq (car expr) 'calcFunc-geq)
637 (or (and (eq signs 1) 0)
638 (and (memq signs '(2 4 6)) 1))))
642 (defun math-simplify-add-term (np dp minus lplain)
643 (or (math-vectorp (car np))
646 (while (memq (car-safe (setq n (car np) d (car dp))) '(+ -))
648 (if (setq temp (math-combine-sum n (nth 2 d)
649 minus (eq (car d) '+) t))
650 (if (or lplain (eq (math-looks-negp temp) minus))
652 (setcar np (setq n (if minus (math-neg temp) temp)))
653 (setcar (cdr (cdr d)) 0))
656 (setcar (cdr (cdr d)) (setq n (if (eq (car d) '+)
660 (if (setq temp (math-combine-sum n d minus t t))
663 (eq (math-looks-negp temp) minus)))
665 (setcar np (setq n (if minus (math-neg temp) temp)))
669 (setcar dp (setq n (math-neg temp))))))))
672 (math-defsimplify calcFunc-sin
673 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
674 (nth 1 (nth 1 expr)))
675 (and (math-looks-negp (nth 1 expr))
676 (math-neg (list 'calcFunc-sin (math-neg (nth 1 expr)))))
677 (and (eq calc-angle-mode 'rad)
678 (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
680 (math-known-sin (car n) (nth 1 n) 120 0))))
681 (and (eq calc-angle-mode 'deg)
682 (let ((n (math-integer-plus (nth 1 expr))))
684 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 0))))
685 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
686 (list 'calcFunc-sqrt (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))
687 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
688 (math-div (nth 1 (nth 1 expr))
690 (math-add 1 (math-sqr (nth 1 (nth 1 expr)))))))
691 (let ((m (math-should-expand-trig (nth 1 expr))))
692 (and m (integerp (car m))
693 (let ((n (car m)) (a (nth 1 m)))
695 (list '* (list 'calcFunc-sin (list '* (1- n) a))
696 (list 'calcFunc-cos a))
697 (list '* (list 'calcFunc-cos (list '* (1- n) a))
698 (list 'calcFunc-sin a)))))))
701 (math-defsimplify calcFunc-cos
702 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
703 (nth 1 (nth 1 expr)))
704 (and (math-looks-negp (nth 1 expr))
705 (list 'calcFunc-cos (math-neg (nth 1 expr))))
706 (and (eq calc-angle-mode 'rad)
707 (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
709 (math-known-sin (car n) (nth 1 n) 120 300))))
710 (and (eq calc-angle-mode 'deg)
711 (let ((n (math-integer-plus (nth 1 expr))))
713 (math-known-sin (car n) (nth 1 n) '(frac 2 3) 300))))
714 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
715 (list 'calcFunc-sqrt (math-sub 1 (math-sqr (nth 1 (nth 1 expr))))))
716 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
719 (math-add 1 (math-sqr (nth 1 (nth 1 expr)))))))
720 (let ((m (math-should-expand-trig (nth 1 expr))))
721 (and m (integerp (car m))
722 (let ((n (car m)) (a (nth 1 m)))
724 (list '* (list 'calcFunc-cos (list '* (1- n) a))
725 (list 'calcFunc-cos a))
726 (list '* (list 'calcFunc-sin (list '* (1- n) a))
727 (list 'calcFunc-sin a)))))))
730 (defun math-should-expand-trig (x &optional hyperbolic)
731 (let ((m (math-is-multiple x)))
732 (and math-living-dangerously
733 m (or (and (integerp (car m)) (> (car m) 1))
734 (equal (car m) '(frac 1 2)))
736 (memq (car-safe (nth 1 m))
738 '(calcFunc-arcsinh calcFunc-arccosh calcFunc-arctanh)
739 '(calcFunc-arcsin calcFunc-arccos calcFunc-arctan)))
740 (and (eq (car-safe (nth 1 m)) 'calcFunc-ln)
741 (eq hyperbolic 'exp)))
745 (defun math-known-sin (plus n mul off)
746 (setq n (math-mul n mul))
747 (and (math-num-integerp n)
748 (setq n (math-mod (math-add (math-trunc n) off) 240))
750 (and (setq n (math-known-sin plus (- n 120) 1 0))
754 (if (math-zerop plus)
755 (and (or calc-symbolic-mode
759 (10 . (/ (calcFunc-sqrt
760 (- 2 (calcFunc-sqrt 3))) 2))
761 (12 . (/ (- (calcFunc-sqrt 5) 1) 4))
762 (15 . (/ (calcFunc-sqrt
763 (- 2 (calcFunc-sqrt 2))) 2))
765 (24 . (* (^ (/ 1 2) (/ 3 2))
767 (- 5 (calcFunc-sqrt 5)))))
768 (30 . (/ (calcFunc-sqrt 2) 2))
769 (36 . (/ (+ (calcFunc-sqrt 5) 1) 4))
770 (40 . (/ (calcFunc-sqrt 3) 2))
771 (45 . (/ (calcFunc-sqrt
772 (+ 2 (calcFunc-sqrt 2))) 2))
773 (48 . (* (^ (/ 1 2) (/ 3 2))
775 (+ 5 (calcFunc-sqrt 5)))))
776 (50 . (/ (calcFunc-sqrt
777 (+ 2 (calcFunc-sqrt 3))) 2))
779 (cond ((eq n 0) (math-normalize (list 'calcFunc-sin plus)))
780 ((eq n 60) (math-normalize (list 'calcFunc-cos plus)))
784 (math-defsimplify calcFunc-tan
785 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan)
786 (nth 1 (nth 1 expr)))
787 (and (math-looks-negp (nth 1 expr))
788 (math-neg (list 'calcFunc-tan (math-neg (nth 1 expr)))))
789 (and (eq calc-angle-mode 'rad)
790 (let ((n (math-linear-in (nth 1 expr) '(var pi var-pi))))
792 (math-known-tan (car n) (nth 1 n) 120))))
793 (and (eq calc-angle-mode 'deg)
794 (let ((n (math-integer-plus (nth 1 expr))))
796 (math-known-tan (car n) (nth 1 n) '(frac 2 3)))))
797 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin)
798 (math-div (nth 1 (nth 1 expr))
800 (math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
801 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos)
802 (math-div (list 'calcFunc-sqrt
803 (math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))
804 (nth 1 (nth 1 expr))))
805 (let ((m (math-should-expand-trig (nth 1 expr))))
807 (if (equal (car m) '(frac 1 2))
808 (math-div (math-sub 1 (list 'calcFunc-cos (nth 1 m)))
809 (list 'calcFunc-sin (nth 1 m)))
810 (math-div (list 'calcFunc-sin (nth 1 expr))
811 (list 'calcFunc-cos (nth 1 expr)))))))
814 (defun math-known-tan (plus n mul)
815 (setq n (math-mul n mul))
816 (and (math-num-integerp n)
817 (setq n (math-mod (math-trunc n) 120))
819 (and (setq n (math-known-tan plus (- 120 n) 1))
821 (if (math-zerop plus)
822 (and (or calc-symbolic-mode
824 (cdr (assq n '( (0 . 0)
825 (10 . (- 2 (calcFunc-sqrt 3)))
827 (- 1 (* (/ 2 5) (calcFunc-sqrt 5)))))
828 (15 . (- (calcFunc-sqrt 2) 1))
829 (20 . (/ (calcFunc-sqrt 3) 3))
831 (- 5 (* 2 (calcFunc-sqrt 5)))))
834 (+ 1 (* (/ 2 5) (calcFunc-sqrt 5)))))
835 (40 . (calcFunc-sqrt 3))
836 (45 . (+ (calcFunc-sqrt 2) 1))
838 (+ 5 (* 2 (calcFunc-sqrt 5)))))
839 (50 . (+ 2 (calcFunc-sqrt 3)))
840 (60 . (var uinf var-uinf))))))
841 (cond ((eq n 0) (math-normalize (list 'calcFunc-tan plus)))
842 ((eq n 60) (math-normalize (list '/ -1
843 (list 'calcFunc-tan plus))))
847 (math-defsimplify calcFunc-sinh
848 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
849 (nth 1 (nth 1 expr)))
850 (and (math-looks-negp (nth 1 expr))
851 (math-neg (list 'calcFunc-sinh (math-neg (nth 1 expr)))))
852 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
853 math-living-dangerously
854 (list 'calcFunc-sqrt (math-sub (math-sqr (nth 1 (nth 1 expr))) 1)))
855 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
856 math-living-dangerously
857 (math-div (nth 1 (nth 1 expr))
859 (math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
860 (let ((m (math-should-expand-trig (nth 1 expr) t)))
861 (and m (integerp (car m))
862 (let ((n (car m)) (a (nth 1 m)))
865 (list '* (list 'calcFunc-sinh (list '* (1- n) a))
866 (list 'calcFunc-cosh a))
867 (list '* (list 'calcFunc-cosh (list '* (1- n) a))
868 (list 'calcFunc-sinh a))))))))
871 (math-defsimplify calcFunc-cosh
872 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
873 (nth 1 (nth 1 expr)))
874 (and (math-looks-negp (nth 1 expr))
875 (list 'calcFunc-cosh (math-neg (nth 1 expr))))
876 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
877 math-living-dangerously
878 (list 'calcFunc-sqrt (math-add (math-sqr (nth 1 (nth 1 expr))) 1)))
879 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
880 math-living-dangerously
883 (math-sub 1 (math-sqr (nth 1 (nth 1 expr)))))))
884 (let ((m (math-should-expand-trig (nth 1 expr) t)))
885 (and m (integerp (car m))
886 (let ((n (car m)) (a (nth 1 m)))
889 (list '* (list 'calcFunc-cosh (list '* (1- n) a))
890 (list 'calcFunc-cosh a))
891 (list '* (list 'calcFunc-sinh (list '* (1- n) a))
892 (list 'calcFunc-sinh a))))))))
895 (math-defsimplify calcFunc-tanh
896 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh)
897 (nth 1 (nth 1 expr)))
898 (and (math-looks-negp (nth 1 expr))
899 (math-neg (list 'calcFunc-tanh (math-neg (nth 1 expr)))))
900 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh)
901 math-living-dangerously
902 (math-div (nth 1 (nth 1 expr))
904 (math-add (math-sqr (nth 1 (nth 1 expr))) 1))))
905 (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh)
906 math-living-dangerously
907 (math-div (list 'calcFunc-sqrt
908 (math-sub (math-sqr (nth 1 (nth 1 expr))) 1))
909 (nth 1 (nth 1 expr))))
910 (let ((m (math-should-expand-trig (nth 1 expr) t)))
912 (if (equal (car m) '(frac 1 2))
913 (math-div (math-sub (list 'calcFunc-cosh (nth 1 m)) 1)
914 (list 'calcFunc-sinh (nth 1 m)))
915 (math-div (list 'calcFunc-sinh (nth 1 expr))
916 (list 'calcFunc-cosh (nth 1 expr)))))))
919 (math-defsimplify calcFunc-arcsin
920 (or (and (math-looks-negp (nth 1 expr))
921 (math-neg (list 'calcFunc-arcsin (math-neg (nth 1 expr)))))
922 (and (eq (nth 1 expr) 1)
923 (math-quarter-circle t))
924 (and (equal (nth 1 expr) '(frac 1 2))
925 (math-div (math-half-circle t) 6))
926 (and math-living-dangerously
927 (eq (car-safe (nth 1 expr)) 'calcFunc-sin)
928 (nth 1 (nth 1 expr)))
929 (and math-living-dangerously
930 (eq (car-safe (nth 1 expr)) 'calcFunc-cos)
931 (math-sub (math-quarter-circle t)
932 (nth 1 (nth 1 expr)))))
935 (math-defsimplify calcFunc-arccos
936 (or (and (eq (nth 1 expr) 0)
937 (math-quarter-circle t))
938 (and (eq (nth 1 expr) -1)
939 (math-half-circle t))
940 (and (equal (nth 1 expr) '(frac 1 2))
941 (math-div (math-half-circle t) 3))
942 (and (equal (nth 1 expr) '(frac -1 2))
943 (math-div (math-mul (math-half-circle t) 2) 3))
944 (and math-living-dangerously
945 (eq (car-safe (nth 1 expr)) 'calcFunc-cos)
946 (nth 1 (nth 1 expr)))
947 (and math-living-dangerously
948 (eq (car-safe (nth 1 expr)) 'calcFunc-sin)
949 (math-sub (math-quarter-circle t)
950 (nth 1 (nth 1 expr)))))
953 (math-defsimplify calcFunc-arctan
954 (or (and (math-looks-negp (nth 1 expr))
955 (math-neg (list 'calcFunc-arctan (math-neg (nth 1 expr)))))
956 (and (eq (nth 1 expr) 1)
957 (math-div (math-half-circle t) 4))
958 (and math-living-dangerously
959 (eq (car-safe (nth 1 expr)) 'calcFunc-tan)
960 (nth 1 (nth 1 expr))))
963 (math-defsimplify calcFunc-arcsinh
964 (or (and (math-looks-negp (nth 1 expr))
965 (math-neg (list 'calcFunc-arcsinh (math-neg (nth 1 expr)))))
966 (and (eq (car-safe (nth 1 expr)) 'calcFunc-sinh)
967 (or math-living-dangerously
968 (math-known-realp (nth 1 (nth 1 expr))))
969 (nth 1 (nth 1 expr))))
972 (math-defsimplify calcFunc-arccosh
973 (and (eq (car-safe (nth 1 expr)) 'calcFunc-cosh)
974 (or math-living-dangerously
975 (math-known-realp (nth 1 (nth 1 expr))))
976 (nth 1 (nth 1 expr)))
979 (math-defsimplify calcFunc-arctanh
980 (or (and (math-looks-negp (nth 1 expr))
981 (math-neg (list 'calcFunc-arctanh (math-neg (nth 1 expr)))))
982 (and (eq (car-safe (nth 1 expr)) 'calcFunc-tanh)
983 (or math-living-dangerously
984 (math-known-realp (nth 1 (nth 1 expr))))
985 (nth 1 (nth 1 expr))))
988 (math-defsimplify calcFunc-sqrt
992 (defun math-simplify-sqrt ()
993 (or (and (eq (car-safe (nth 1 expr)) 'frac)
994 (math-div (list 'calcFunc-sqrt (math-mul (nth 1 (nth 1 expr))
995 (nth 2 (nth 1 expr))))
996 (nth 2 (nth 1 expr))))
997 (let ((fac (if (math-objectp (nth 1 expr))
998 (math-squared-factor (nth 1 expr))
999 (math-common-constant-factor (nth 1 expr)))))
1000 (and fac (not (eq fac 1))
1001 (math-mul (math-normalize (list 'calcFunc-sqrt fac))
1003 (list 'calcFunc-sqrt
1004 (math-cancel-common-factor (nth 1 expr) fac))))))
1005 (and math-living-dangerously
1006 (or (and (eq (car-safe (nth 1 expr)) '-)
1007 (math-equal-int (nth 1 (nth 1 expr)) 1)
1008 (eq (car-safe (nth 2 (nth 1 expr))) '^)
1009 (math-equal-int (nth 2 (nth 2 (nth 1 expr))) 2)
1010 (or (and (eq (car-safe (nth 1 (nth 2 (nth 1 expr))))
1013 (nth 1 (nth 1 (nth 2 (nth 1 expr))))))
1014 (and (eq (car-safe (nth 1 (nth 2 (nth 1 expr))))
1017 (nth 1 (nth 1 (nth 2 (nth 1 expr))))))))
1018 (and (eq (car-safe (nth 1 expr)) '-)
1019 (math-equal-int (nth 2 (nth 1 expr)) 1)
1020 (eq (car-safe (nth 1 (nth 1 expr))) '^)
1021 (math-equal-int (nth 2 (nth 1 (nth 1 expr))) 2)
1022 (and (eq (car-safe (nth 1 (nth 1 (nth 1 expr))))
1024 (list 'calcFunc-sinh
1025 (nth 1 (nth 1 (nth 1 (nth 1 expr)))))))
1026 (and (eq (car-safe (nth 1 expr)) '+)
1027 (let ((a (nth 1 (nth 1 expr)))
1028 (b (nth 2 (nth 1 expr))))
1029 (and (or (and (math-equal-int a 1)
1030 (setq a b b (nth 1 (nth 1 expr))))
1031 (math-equal-int b 1))
1032 (eq (car-safe a) '^)
1033 (math-equal-int (nth 2 a) 2)
1034 (or (and (eq (car-safe (nth 1 a)) 'calcFunc-sinh)
1035 (list 'calcFunc-cosh (nth 1 (nth 1 a))))
1036 (and (eq (car-safe (nth 1 a)) 'calcFunc-tan)
1037 (list '/ 1 (list 'calcFunc-cos
1038 (nth 1 (nth 1 a)))))))))
1039 (and (eq (car-safe (nth 1 expr)) '^)
1041 (nth 1 (nth 1 expr))
1042 (math-div (nth 2 (nth 1 expr)) 2)))
1043 (and (eq (car-safe (nth 1 expr)) 'calcFunc-sqrt)
1044 (list '^ (nth 1 (nth 1 expr)) (math-div 1 4)))
1045 (and (memq (car-safe (nth 1 expr)) '(* /))
1046 (list (car (nth 1 expr))
1047 (list 'calcFunc-sqrt (nth 1 (nth 1 expr)))
1048 (list 'calcFunc-sqrt (nth 2 (nth 1 expr)))))
1049 (and (memq (car-safe (nth 1 expr)) '(+ -))
1050 (not (math-any-floats (nth 1 expr)))
1051 (let ((f (calcFunc-factors (calcFunc-expand
1053 (and (math-vectorp f)
1054 (or (> (length f) 2)
1055 (> (nth 2 (nth 1 f)) 1))
1056 (let ((out 1) (rest 1) (sums 1) fac pow)
1057 (while (setq f (cdr f))
1058 (setq fac (nth 1 (car f))
1059 pow (nth 2 (car f)))
1061 (setq out (math-mul out (math-pow
1065 (if (memq (car-safe fac) '(+ -))
1066 (setq sums (math-mul-thru sums fac))
1067 (setq rest (math-mul rest fac)))))
1068 (and (not (and (eq out 1) (memq rest '(1 -1))))
1071 (list 'calcFunc-sqrt
1072 (math-mul sums rest)))))))))))
1075 ;;; Rather than factoring x into primes, just check for the first ten primes.
1076 (defun math-squared-factor (x)
1077 (if (Math-integerp x)
1078 (let ((prsqr '(4 9 25 49 121 169 289 361 529 841))
1082 (if (eq (cdr (setq res (math-idivmod x (car prsqr)))) 0)
1084 fac (math-mul fac (car prsqr)))
1085 (setq prsqr (cdr prsqr))))
1089 (math-defsimplify calcFunc-exp
1090 (math-simplify-exp (nth 1 expr))
1093 (defun math-simplify-exp (x)
1094 (or (and (eq (car-safe x) 'calcFunc-ln)
1096 (and math-living-dangerously
1097 (or (and (eq (car-safe x) 'calcFunc-arcsinh)
1099 (list 'calcFunc-sqrt
1100 (math-add (math-sqr (nth 1 x)) 1))))
1101 (and (eq (car-safe x) 'calcFunc-arccosh)
1103 (list 'calcFunc-sqrt
1104 (math-sub (math-sqr (nth 1 x)) 1))))
1105 (and (eq (car-safe x) 'calcFunc-arctanh)
1106 (math-div (list 'calcFunc-sqrt (math-add 1 (nth 1 x)))
1107 (list 'calcFunc-sqrt (math-sub 1 (nth 1 x)))))
1108 (let ((m (math-should-expand-trig x 'exp)))
1109 (and m (integerp (car m))
1110 (list '^ (list 'calcFunc-exp (nth 1 m)) (car m))))))
1111 (and calc-symbolic-mode
1112 (math-known-imagp x)
1113 (let* ((ip (calcFunc-im x))
1114 (n (math-linear-in ip '(var pi var-pi)))
1117 (setq s (math-known-sin (car n) (nth 1 n) 120 0))
1118 (setq c (math-known-sin (car n) (nth 1 n) 120 300))
1119 (list '+ c (list '* s '(var i var-i)))))))
1122 (math-defsimplify calcFunc-ln
1123 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-exp)
1124 (or math-living-dangerously
1125 (math-known-realp (nth 1 (nth 1 expr))))
1126 (nth 1 (nth 1 expr)))
1127 (and (eq (car-safe (nth 1 expr)) '^)
1128 (equal (nth 1 (nth 1 expr)) '(var e var-e))
1129 (or math-living-dangerously
1130 (math-known-realp (nth 2 (nth 1 expr))))
1131 (nth 2 (nth 1 expr)))
1132 (and calc-symbolic-mode
1133 (math-known-negp (nth 1 expr))
1134 (math-add (list 'calcFunc-ln (math-neg (nth 1 expr)))
1136 (and calc-symbolic-mode
1137 (math-known-imagp (nth 1 expr))
1138 (let* ((ip (calcFunc-im (nth 1 expr)))
1139 (ips (math-possible-signs ip)))
1140 (or (and (memq ips '(4 6))
1141 (math-add (list 'calcFunc-ln ip)
1142 '(/ (* (var pi var-pi) (var i var-i)) 2)))
1143 (and (memq ips '(1 3))
1144 (math-sub (list 'calcFunc-ln (math-neg ip))
1145 '(/ (* (var pi var-pi) (var i var-i)) 2)))))))
1149 (math-simplify-pow))
1151 (defun math-simplify-pow ()
1152 (or (and math-living-dangerously
1153 (or (and (eq (car-safe (nth 1 expr)) '^)
1155 (nth 1 (nth 1 expr))
1156 (math-mul (nth 2 expr) (nth 2 (nth 1 expr)))))
1157 (and (eq (car-safe (nth 1 expr)) 'calcFunc-sqrt)
1159 (nth 1 (nth 1 expr))
1160 (math-div (nth 2 expr) 2)))
1161 (and (memq (car-safe (nth 1 expr)) '(* /))
1162 (list (car (nth 1 expr))
1163 (list '^ (nth 1 (nth 1 expr)) (nth 2 expr))
1164 (list '^ (nth 2 (nth 1 expr)) (nth 2 expr))))))
1165 (and (math-equal-int (nth 1 expr) 10)
1166 (eq (car-safe (nth 2 expr)) 'calcFunc-log10)
1167 (nth 1 (nth 2 expr)))
1168 (and (equal (nth 1 expr) '(var e var-e))
1169 (math-simplify-exp (nth 2 expr)))
1170 (and (eq (car-safe (nth 1 expr)) 'calcFunc-exp)
1171 (not math-integrating)
1172 (list 'calcFunc-exp (math-mul (nth 1 (nth 1 expr)) (nth 2 expr))))
1173 (and (equal (nth 1 expr) '(var i var-i))
1175 (math-num-integerp (nth 2 expr))
1176 (let ((x (math-mod (math-trunc (nth 2 expr)) 4)))
1178 ((eq x 1) (nth 1 expr))
1180 ((eq x 3) (math-neg (nth 1 expr))))))
1181 (and math-integrating
1182 (integerp (nth 2 expr))
1184 (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-cos)
1185 (math-mul (math-pow (nth 1 expr) (- (nth 2 expr) 2))
1189 (nth 1 (nth 1 expr)))))))
1190 (and (eq (car-safe (nth 1 expr)) 'calcFunc-cosh)
1191 (math-mul (math-pow (nth 1 expr) (- (nth 2 expr) 2))
1194 (list 'calcFunc-sinh
1195 (nth 1 (nth 1 expr)))))))))
1196 (and (eq (car-safe (nth 2 expr)) 'frac)
1197 (Math-ratp (nth 1 expr))
1198 (Math-posp (nth 1 expr))
1199 (if (equal (nth 2 expr) '(frac 1 2))
1200 (list 'calcFunc-sqrt (nth 1 expr))
1201 (let ((flr (math-floor (nth 2 expr))))
1202 (and (not (Math-zerop flr))
1203 (list '* (list '^ (nth 1 expr) flr)
1204 (list '^ (nth 1 expr)
1205 (math-sub (nth 2 expr) flr)))))))
1206 (and (eq (math-quarter-integer (nth 2 expr)) 2)
1207 (let ((temp (math-simplify-sqrt)))
1209 (list '^ temp (math-mul (nth 2 expr) 2))))))
1212 (math-defsimplify calcFunc-log10
1213 (and (eq (car-safe (nth 1 expr)) '^)
1214 (math-equal-int (nth 1 (nth 1 expr)) 10)
1215 (or math-living-dangerously
1216 (math-known-realp (nth 2 (nth 1 expr))))
1217 (nth 2 (nth 1 expr)))
1221 (math-defsimplify calcFunc-erf
1222 (or (and (math-looks-negp (nth 1 expr))
1223 (math-neg (list 'calcFunc-erf (math-neg (nth 1 expr)))))
1224 (and (eq (car-safe (nth 1 expr)) 'calcFunc-conj)
1225 (list 'calcFunc-conj (list 'calcFunc-erf (nth 1 (nth 1 expr))))))
1228 (math-defsimplify calcFunc-erfc
1229 (or (and (math-looks-negp (nth 1 expr))
1230 (math-sub 2 (list 'calcFunc-erfc (math-neg (nth 1 expr)))))
1231 (and (eq (car-safe (nth 1 expr)) 'calcFunc-conj)
1232 (list 'calcFunc-conj (list 'calcFunc-erfc (nth 1 (nth 1 expr))))))
1236 (defun math-linear-in (expr term &optional always)
1237 (if (math-expr-contains expr term)
1238 (let* ((calc-prefer-frac t)
1239 (p (math-is-polynomial expr term 1)))
1242 (and always (list expr 0)))
1245 (defun math-multiple-of (expr term)
1246 (let ((p (math-linear-in expr term)))
1248 (math-zerop (car p))
1252 (defun math-integer-plus (expr)
1253 (cond ((Math-integerp expr)
1255 ((and (memq (car expr) '(+ -))
1256 (Math-integerp (nth 1 expr)))
1257 (list (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr)))
1259 ((and (memq (car expr) '(+ -))
1260 (Math-integerp (nth 2 expr)))
1262 (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr)))))
1263 (t nil)) ; not perfect, but it'll do
1266 (defun math-is-linear (expr &optional always)
1269 (if (eq (car-safe expr) '+)
1270 (if (Math-objectp (nth 1 expr))
1271 (setq offset (nth 1 expr)
1273 (if (Math-objectp (nth 2 expr))
1274 (setq offset (nth 2 expr)
1275 expr (nth 1 expr))))
1276 (if (eq (car-safe expr) '-)
1277 (if (Math-objectp (nth 1 expr))
1278 (setq offset (nth 1 expr)
1279 expr (math-neg (nth 2 expr)))
1280 (if (Math-objectp (nth 2 expr))
1281 (setq offset (math-neg (nth 2 expr))
1282 expr (nth 1 expr))))))
1283 (setq coef (math-is-multiple expr always))
1285 (list offset (or (car coef) 1) (or (nth 1 coef) expr))
1290 (defun math-is-multiple (expr &optional always)
1291 (or (if (eq (car-safe expr) '*)
1292 (if (Math-objectp (nth 1 expr))
1293 (list (nth 1 expr) (nth 2 expr)))
1294 (if (eq (car-safe expr) '/)
1295 (if (and (Math-objectp (nth 1 expr))
1296 (not (math-equal-int (nth 1 expr) 1)))
1297 (list (nth 1 expr) (math-div 1 (nth 2 expr)))
1298 (if (Math-objectp (nth 2 expr))
1299 (list (math-div 1 (nth 2 expr)) (nth 1 expr))
1300 (let ((res (math-is-multiple (nth 1 expr))))
1303 (math-div (nth 2 (nth 1 expr)) (nth 2 expr)))
1304 (setq res (math-is-multiple (nth 2 expr)))
1306 (list (math-div 1 (car res))
1307 (math-div (nth 1 expr)
1308 (nth 2 (nth 2 expr)))))))))
1309 (if (eq (car-safe expr) 'neg)
1310 (list -1 (nth 1 expr)))))
1311 (if (Math-objvecp expr)
1318 (defun calcFunc-lin (expr &optional var)
1320 (let ((res (math-linear-in expr var t)))
1321 (or res (math-reject-arg expr "Linear term expected"))
1322 (list 'vec (car res) (nth 1 res) var))
1323 (let ((res (math-is-linear expr t)))
1324 (or res (math-reject-arg expr "Linear term expected"))
1328 (defun calcFunc-linnt (expr &optional var)
1330 (let ((res (math-linear-in expr var)))
1331 (or res (math-reject-arg expr "Linear term expected"))
1332 (list 'vec (car res) (nth 1 res) var))
1333 (let ((res (math-is-linear expr)))
1334 (or res (math-reject-arg expr "Linear term expected"))
1338 (defun calcFunc-islin (expr &optional var)
1339 (if (and (Math-objvecp expr) (not var))
1341 (calcFunc-lin expr var)
1345 (defun calcFunc-islinnt (expr &optional var)
1346 (if (Math-objvecp expr)
1348 (calcFunc-linnt expr var)
1355 ;;; Simple operations on expressions.
1357 ;;; Return number of ocurrences of thing in expr, or nil if none.
1358 (defun math-expr-contains-count (expr thing)
1359 (cond ((equal expr thing) 1)
1360 ((Math-primp expr) nil)
1363 (while (setq expr (cdr expr))
1364 (setq num (+ num (or (math-expr-contains-count
1365 (car expr) thing) 0))))
1370 (defun math-expr-contains (expr thing)
1371 (cond ((equal expr thing) 1)
1372 ((Math-primp expr) nil)
1374 (while (and (setq expr (cdr expr))
1375 (not (math-expr-contains (car expr) thing))))
1379 ;;; Return non-nil if any variable of thing occurs in expr.
1380 (defun math-expr-depends (expr thing)
1381 (if (Math-primp thing)
1382 (and (eq (car-safe thing) 'var)
1383 (math-expr-contains expr thing))
1384 (while (and (setq thing (cdr thing))
1385 (not (math-expr-depends expr (car thing)))))
1389 ;;; Substitute all occurrences of old for new in expr (non-destructive).
1390 (defun math-expr-subst (expr old new)
1391 (math-expr-subst-rec expr)
1393 (fset 'calcFunc-subst (symbol-function 'math-expr-subst))
1395 (defun math-expr-subst-rec (expr)
1396 (cond ((equal expr old) new)
1397 ((Math-primp expr) expr)
1398 ((memq (car expr) '(calcFunc-deriv
1400 (if (= (length expr) 2)
1401 (if (equal (nth 1 expr) old)
1402 (append expr (list new))
1404 (list (car expr) (nth 1 expr)
1405 (math-expr-subst-rec (nth 2 expr)))))
1408 (mapcar 'math-expr-subst-rec (cdr expr)))))
1411 ;;; Various measures of the size of an expression.
1412 (defun math-expr-weight (expr)
1413 (if (Math-primp expr)
1416 (while (setq expr (cdr expr))
1417 (setq w (+ w (math-expr-weight (car expr)))))
1421 (defun math-expr-height (expr)
1422 (if (Math-primp expr)
1425 (while (setq expr (cdr expr))
1426 (setq h (max h (math-expr-height (car expr)))))
1433 ;;; Polynomial operations (to support the integrator and solve-for).
1435 (defun calcFunc-collect (expr base)
1436 (let ((p (math-is-polynomial expr base 50 t)))
1438 (math-normalize ; fix selection bug
1439 (math-build-polynomial-expr p base))
1443 ;;; If expr is of the form "a + bx + cx^2 + ...", return the list (a b c ...),
1444 ;;; else return nil if not in polynomial form. If "loose", coefficients
1445 ;;; may contain x, e.g., sin(x) + cos(x) x^2 is a loose polynomial in x.
1446 (defun math-is-polynomial (expr var &optional degree loose)
1447 (let* ((math-poly-base-variable (if loose
1448 (if (eq loose 'gen) var '(var XXX XXX))
1449 math-poly-base-variable))
1450 (poly (math-is-poly-rec expr math-poly-neg-powers)))
1451 (and (or (null degree)
1452 (<= (length poly) (1+ degree)))
1456 (defun math-is-poly-rec (expr negpow)
1458 (or (cond ((or (equal expr var)
1459 (eq (car-safe expr) '^))
1462 (or (equal expr var)
1463 (setq pow (nth 2 expr)
1465 (or (eq math-poly-mult-powers 1)
1466 (setq pow (let ((m (math-is-multiple pow 1)))
1467 (and (eq (car-safe (car m)) 'cplx)
1468 (Math-zerop (nth 1 (car m)))
1469 (setq m (list (nth 2 (car m))
1472 (and (if math-poly-mult-powers
1473 (equal math-poly-mult-powers
1475 (setq math-poly-mult-powers (nth 1 m)))
1476 (or (equal expr var)
1477 (eq math-poly-mult-powers 1))
1481 (setq pow (math-to-simple-fraction pow))
1482 (and (eq (car-safe pow) 'frac)
1483 math-poly-frac-powers
1485 (setq math-poly-frac-powers
1486 (calcFunc-lcm math-poly-frac-powers
1488 (or (memq math-poly-frac-powers '(1 nil))
1489 (setq pow (math-mul pow math-poly-frac-powers)))
1495 (let ((p1 (if (equal expr var)
1497 (math-is-poly-rec expr nil)))
1502 (<= (* (1- (length p1)) n) degree))
1505 (setq accum (math-poly-mul accum p1)
1509 (math-is-poly-rec expr nil)
1510 (setq math-poly-neg-powers
1511 (cons (math-pow expr (- pow))
1512 math-poly-neg-powers))
1513 (list (list '^ expr pow))))))))
1514 ((Math-objectp expr)
1516 ((memq (car expr) '(+ -))
1517 (let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
1519 (let ((p2 (math-is-poly-rec (nth 2 expr) negpow)))
1521 (math-poly-mix p1 1 p2
1522 (if (eq (car expr) '+) 1 -1)))))))
1523 ((eq (car expr) 'neg)
1524 (mapcar 'math-neg (math-is-poly-rec (nth 1 expr) negpow)))
1526 (let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
1528 (let ((p2 (math-is-poly-rec (nth 2 expr) negpow)))
1531 (<= (- (+ (length p1) (length p2)) 2) degree))
1532 (math-poly-mul p1 p2))))))
1534 (and (or (not (math-poly-depends (nth 2 expr) var))
1536 (math-is-poly-rec (nth 2 expr) nil)
1537 (setq math-poly-neg-powers
1538 (cons (nth 2 expr) math-poly-neg-powers))))
1539 (not (Math-zerop (nth 2 expr)))
1540 (let ((p1 (math-is-poly-rec (nth 1 expr) negpow)))
1541 (mapcar (function (lambda (x) (math-div x (nth 2 expr))))
1543 ((and (eq (car expr) 'calcFunc-exp)
1544 (equal var '(var e var-e)))
1545 (math-is-poly-rec (list '^ var (nth 1 expr)) negpow))
1546 ((and (eq (car expr) 'calcFunc-sqrt)
1547 math-poly-frac-powers)
1548 (math-is-poly-rec (list '^ (nth 1 expr) '(frac 1 2)) negpow))
1550 (and (or (not (math-poly-depends expr var))
1552 (not (eq (car expr) 'vec))
1556 ;;; Check if expr is a polynomial in var; if so, return its degree.
1557 (defun math-polynomial-p (expr var)
1558 (cond ((equal expr var) 1)
1559 ((Math-primp expr) 0)
1560 ((memq (car expr) '(+ -))
1561 (let ((p1 (math-polynomial-p (nth 1 expr) var))
1563 (and p1 (setq p2 (math-polynomial-p (nth 2 expr) var))
1566 (let ((p1 (math-polynomial-p (nth 1 expr) var))
1568 (and p1 (setq p2 (math-polynomial-p (nth 2 expr) var))
1570 ((eq (car expr) 'neg)
1571 (math-polynomial-p (nth 1 expr) var))
1572 ((and (eq (car expr) '/)
1573 (not (math-poly-depends (nth 2 expr) var)))
1574 (math-polynomial-p (nth 1 expr) var))
1575 ((and (eq (car expr) '^)
1576 (natnump (nth 2 expr)))
1577 (let ((p1 (math-polynomial-p (nth 1 expr) var)))
1578 (and p1 (* p1 (nth 2 expr)))))
1579 ((math-poly-depends expr var) nil)
1583 (defun math-poly-depends (expr var)
1584 (if math-poly-base-variable
1585 (math-expr-contains expr math-poly-base-variable)
1586 (math-expr-depends expr var))
1589 ;;; Find the variable (or sub-expression) which is the base of polynomial expr.
1590 (defun math-polynomial-base (mpb-top-expr &optional mpb-pred)
1592 (setq mpb-pred (function (lambda (base) (math-polynomial-p
1593 mpb-top-expr base)))))
1594 (or (let ((const-ok nil))
1595 (math-polynomial-base-rec mpb-top-expr))
1597 (math-polynomial-base-rec mpb-top-expr)))
1600 (defun math-polynomial-base-rec (mpb-expr)
1601 (and (not (Math-objvecp mpb-expr))
1602 (or (and (memq (car mpb-expr) '(+ - *))
1603 (or (math-polynomial-base-rec (nth 1 mpb-expr))
1604 (math-polynomial-base-rec (nth 2 mpb-expr))))
1605 (and (memq (car mpb-expr) '(/ neg))
1606 (math-polynomial-base-rec (nth 1 mpb-expr)))
1607 (and (eq (car mpb-expr) '^)
1608 (math-polynomial-base-rec (nth 1 mpb-expr)))
1609 (and (eq (car mpb-expr) 'calcFunc-exp)
1610 (math-polynomial-base-rec '(var e var-e)))
1611 (and (or const-ok (math-expr-contains-vars mpb-expr))
1612 (funcall mpb-pred mpb-expr)
1616 ;;; Return non-nil if expr refers to any variables.
1617 (defun math-expr-contains-vars (expr)
1618 (or (eq (car-safe expr) 'var)
1619 (and (not (Math-primp expr))
1621 (while (and (setq expr (cdr expr))
1622 (not (math-expr-contains-vars (car expr)))))
1626 ;;; Simplify a polynomial in list form by stripping off high-end zeros.
1627 ;;; This always leaves the constant part, i.e., nil->nil and nonnil->nonnil.
1628 (defun math-poly-simplify (p)
1630 (if (Math-zerop (nth (1- (length p)) p))
1631 (let ((pp (copy-sequence p)))
1632 (while (and (cdr pp)
1633 (Math-zerop (nth (1- (length pp)) pp)))
1634 (setcdr (nthcdr (- (length pp) 2) pp) nil))
1639 ;;; Compute ac*a + bc*b for polynomials in list form a, b and
1640 ;;; coefficients ac, bc. Result may be unsimplified.
1641 (defun math-poly-mix (a ac b bc)
1643 (cons (math-add (math-mul (or (car a) 0) ac)
1644 (math-mul (or (car b) 0) bc))
1645 (math-poly-mix (cdr a) ac (cdr b) bc)))
1648 (defun math-poly-zerop (a)
1650 (and (null (cdr a)) (Math-zerop (car a))))
1653 ;;; Multiply two polynomials in list form.
1654 (defun math-poly-mul (a b)
1656 (math-poly-mix b (car a)
1657 (math-poly-mul (cdr a) (cons 0 b)) 1))
1660 ;;; Build an expression from a polynomial list.
1661 (defun math-build-polynomial-expr (p var)
1663 (if (Math-numberp var)
1664 (math-with-extra-prec 1
1665 (let* ((rp (reverse p))
1667 (while (setq rp (cdr rp))
1668 (setq accum (math-add (car rp) (math-mul accum var))))
1670 (let* ((rp (reverse p))
1671 (n (1- (length rp)))
1672 (accum (math-mul (car rp) (math-pow var n)))
1674 (while (setq rp (cdr rp))
1676 (or (math-zerop (car rp))
1677 (setq accum (list (if (math-looks-negp (car rp)) '- '+)
1679 (math-mul (if (math-looks-negp (car rp))
1682 (math-pow var n))))))
1688 (defun math-to-simple-fraction (f)
1689 (or (and (eq (car-safe f) 'float)
1690 (or (and (>= (nth 2 f) 0)
1691 (math-scale-int (nth 1 f) (nth 2 f)))
1692 (and (integerp (nth 1 f))
1695 (math-make-frac (nth 1 f)
1696 (math-scale-int 1 (- (nth 2 f)))))))