diff options
| author | Colin Walters | 2001-11-14 09:00:01 +0000 |
|---|---|---|
| committer | Colin Walters | 2001-11-14 09:00:01 +0000 |
| commit | d389648023884fc3ca5022a51796331f7cf75fb6 (patch) | |
| tree | 95dd17529e194ba2079dc2cf67de51d56916b8fd | |
| parent | 07ff2bc860a955bb35b95657600e823020f8d67a (diff) | |
| download | emacs-d389648023884fc3ca5022a51796331f7cf75fb6.tar.gz emacs-d389648023884fc3ca5022a51796331f7cf75fb6.zip | |
(calcFunc-esimplify, calcFunc-simplify, calcFunc-subst): Use
`defalias' instead of `fset' and `symbol-function'.
Style cleanup; don't put closing parens on their own line, add "foo.el
ends here" to each file, and update copyright date.
| -rw-r--r-- | lisp/calc/calc-alg.el | 283 |
1 files changed, 100 insertions, 183 deletions
diff --git a/lisp/calc/calc-alg.el b/lisp/calc/calc-alg.el index ab34cadbfcf..522deb2ee54 100644 --- a/lisp/calc/calc-alg.el +++ b/lisp/calc/calc-alg.el | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | ;; Calculator for GNU Emacs, part II [calc-alg.el] | 1 | ;; Calculator for GNU Emacs, part II [calc-alg.el] |
| 2 | ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | 2 | ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
| 3 | ;; Written by Dave Gillespie, daveg@synaptics.com. | 3 | ;; Written by Dave Gillespie, daveg@synaptics.com. |
| 4 | 4 | ||
| 5 | ;; This file is part of GNU Emacs. | 5 | ;; This file is part of GNU Emacs. |
| @@ -37,8 +37,7 @@ | |||
| 37 | (calc-with-default-simplification | 37 | (calc-with-default-simplification |
| 38 | (let ((math-simplify-only nil)) | 38 | (let ((math-simplify-only nil)) |
| 39 | (calc-modify-simplify-mode arg) | 39 | (calc-modify-simplify-mode arg) |
| 40 | (calc-enter-result 1 "dsmp" (calc-top 1))))) | 40 | (calc-enter-result 1 "dsmp" (calc-top 1)))))) |
| 41 | ) | ||
| 42 | 41 | ||
| 43 | (defun calc-modify-simplify-mode (arg) | 42 | (defun calc-modify-simplify-mode (arg) |
| 44 | (if (= (math-abs arg) 2) | 43 | (if (= (math-abs arg) 2) |
| @@ -46,22 +45,19 @@ | |||
| 46 | (if (>= (math-abs arg) 3) | 45 | (if (>= (math-abs arg) 3) |
| 47 | (setq calc-simplify-mode 'ext))) | 46 | (setq calc-simplify-mode 'ext))) |
| 48 | (if (< arg 0) | 47 | (if (< arg 0) |
| 49 | (setq calc-simplify-mode (list calc-simplify-mode))) | 48 | (setq calc-simplify-mode (list calc-simplify-mode)))) |
| 50 | ) | ||
| 51 | 49 | ||
| 52 | (defun calc-simplify () | 50 | (defun calc-simplify () |
| 53 | (interactive) | 51 | (interactive) |
| 54 | (calc-slow-wrapper | 52 | (calc-slow-wrapper |
| 55 | (calc-with-default-simplification | 53 | (calc-with-default-simplification |
| 56 | (calc-enter-result 1 "simp" (math-simplify (calc-top-n 1))))) | 54 | (calc-enter-result 1 "simp" (math-simplify (calc-top-n 1)))))) |
| 57 | ) | ||
| 58 | 55 | ||
| 59 | (defun calc-simplify-extended () | 56 | (defun calc-simplify-extended () |
| 60 | (interactive) | 57 | (interactive) |
| 61 | (calc-slow-wrapper | 58 | (calc-slow-wrapper |
| 62 | (calc-with-default-simplification | 59 | (calc-with-default-simplification |
| 63 | (calc-enter-result 1 "esmp" (math-simplify-extended (calc-top-n 1))))) | 60 | (calc-enter-result 1 "esmp" (math-simplify-extended (calc-top-n 1)))))) |
| 64 | ) | ||
| 65 | 61 | ||
| 66 | (defun calc-expand-formula (arg) | 62 | (defun calc-expand-formula (arg) |
| 67 | (interactive "p") | 63 | (interactive "p") |
| @@ -75,16 +71,14 @@ | |||
| 75 | (calc-top-n 1)) | 71 | (calc-top-n 1)) |
| 76 | (let ((top (calc-top-n 1))) | 72 | (let ((top (calc-top-n 1))) |
| 77 | (or (math-expand-formula top) | 73 | (or (math-expand-formula top) |
| 78 | top))))))) | 74 | top)))))))) |
| 79 | ) | ||
| 80 | 75 | ||
| 81 | (defun calc-factor (arg) | 76 | (defun calc-factor (arg) |
| 82 | (interactive "P") | 77 | (interactive "P") |
| 83 | (calc-slow-wrapper | 78 | (calc-slow-wrapper |
| 84 | (calc-unary-op "fctr" (if (calc-is-hyperbolic) | 79 | (calc-unary-op "fctr" (if (calc-is-hyperbolic) |
| 85 | 'calcFunc-factors 'calcFunc-factor) | 80 | 'calcFunc-factors 'calcFunc-factor) |
| 86 | arg)) | 81 | arg))) |
| 87 | ) | ||
| 88 | 82 | ||
| 89 | (defun calc-expand (n) | 83 | (defun calc-expand (n) |
| 90 | (interactive "P") | 84 | (interactive "P") |
| @@ -92,8 +86,7 @@ | |||
| 92 | (calc-enter-result 1 "expa" | 86 | (calc-enter-result 1 "expa" |
| 93 | (append (list 'calcFunc-expand | 87 | (append (list 'calcFunc-expand |
| 94 | (calc-top-n 1)) | 88 | (calc-top-n 1)) |
| 95 | (and n (list (prefix-numeric-value n)))))) | 89 | (and n (list (prefix-numeric-value n))))))) |
| 96 | ) | ||
| 97 | 90 | ||
| 98 | (defun calc-collect (&optional var) | 91 | (defun calc-collect (&optional var) |
| 99 | (interactive "sCollect terms involving: ") | 92 | (interactive "sCollect terms involving: ") |
| @@ -106,26 +99,22 @@ | |||
| 106 | (error "Bad format in expression: %s" (nth 1 var))) | 99 | (error "Bad format in expression: %s" (nth 1 var))) |
| 107 | (calc-enter-result 1 "clct" (list 'calcFunc-collect | 100 | (calc-enter-result 1 "clct" (list 'calcFunc-collect |
| 108 | (calc-top-n 1) | 101 | (calc-top-n 1) |
| 109 | var))))) | 102 | var)))))) |
| 110 | ) | ||
| 111 | 103 | ||
| 112 | (defun calc-apart (arg) | 104 | (defun calc-apart (arg) |
| 113 | (interactive "P") | 105 | (interactive "P") |
| 114 | (calc-slow-wrapper | 106 | (calc-slow-wrapper |
| 115 | (calc-unary-op "aprt" 'calcFunc-apart arg)) | 107 | (calc-unary-op "aprt" 'calcFunc-apart arg))) |
| 116 | ) | ||
| 117 | 108 | ||
| 118 | (defun calc-normalize-rat (arg) | 109 | (defun calc-normalize-rat (arg) |
| 119 | (interactive "P") | 110 | (interactive "P") |
| 120 | (calc-slow-wrapper | 111 | (calc-slow-wrapper |
| 121 | (calc-unary-op "nrat" 'calcFunc-nrat arg)) | 112 | (calc-unary-op "nrat" 'calcFunc-nrat arg))) |
| 122 | ) | ||
| 123 | 113 | ||
| 124 | (defun calc-poly-gcd (arg) | 114 | (defun calc-poly-gcd (arg) |
| 125 | (interactive "P") | 115 | (interactive "P") |
| 126 | (calc-slow-wrapper | 116 | (calc-slow-wrapper |
| 127 | (calc-binary-op "pgcd" 'calcFunc-pgcd arg)) | 117 | (calc-binary-op "pgcd" 'calcFunc-pgcd arg))) |
| 128 | ) | ||
| 129 | 118 | ||
| 130 | (defun calc-poly-div (arg) | 119 | (defun calc-poly-div (arg) |
| 131 | (interactive "P") | 120 | (interactive "P") |
| @@ -139,22 +128,19 @@ | |||
| 139 | (if (not (Math-zerop calc-poly-div-remainder)) | 128 | (if (not (Math-zerop calc-poly-div-remainder)) |
| 140 | (message "(Remainder was %s)" | 129 | (message "(Remainder was %s)" |
| 141 | (math-format-flat-expr calc-poly-div-remainder 0)) | 130 | (math-format-flat-expr calc-poly-div-remainder 0)) |
| 142 | (message "(No remainder)"))))) | 131 | (message "(No remainder)")))))) |
| 143 | ) | ||
| 144 | 132 | ||
| 145 | (defun calc-poly-rem (arg) | 133 | (defun calc-poly-rem (arg) |
| 146 | (interactive "P") | 134 | (interactive "P") |
| 147 | (calc-slow-wrapper | 135 | (calc-slow-wrapper |
| 148 | (calc-binary-op "prem" 'calcFunc-prem arg)) | 136 | (calc-binary-op "prem" 'calcFunc-prem arg))) |
| 149 | ) | ||
| 150 | 137 | ||
| 151 | (defun calc-poly-div-rem (arg) | 138 | (defun calc-poly-div-rem (arg) |
| 152 | (interactive "P") | 139 | (interactive "P") |
| 153 | (calc-slow-wrapper | 140 | (calc-slow-wrapper |
| 154 | (if (calc-is-hyperbolic) | 141 | (if (calc-is-hyperbolic) |
| 155 | (calc-binary-op "pdvr" 'calcFunc-pdivide arg) | 142 | (calc-binary-op "pdvr" 'calcFunc-pdivide arg) |
| 156 | (calc-binary-op "pdvr" 'calcFunc-pdivrem arg))) | 143 | (calc-binary-op "pdvr" 'calcFunc-pdivrem arg)))) |
| 157 | ) | ||
| 158 | 144 | ||
| 159 | (defun calc-substitute (&optional oldname newname) | 145 | (defun calc-substitute (&optional oldname newname) |
| 160 | (interactive "sSubstitute old: ") | 146 | (interactive "sSubstitute old: ") |
| @@ -184,24 +170,21 @@ | |||
| 184 | (error "Bad format in expression: %s" (nth 1 old))) | 170 | (error "Bad format in expression: %s" (nth 1 old))) |
| 185 | (or (math-expr-contains expr old) | 171 | (or (math-expr-contains expr old) |
| 186 | (error "No occurrences found."))) | 172 | (error "No occurrences found."))) |
| 187 | (calc-enter-result num "sbst" (math-expr-subst expr old new)))) | 173 | (calc-enter-result num "sbst" (math-expr-subst expr old new))))) |
| 188 | ) | ||
| 189 | 174 | ||
| 190 | 175 | ||
| 191 | (defun calc-has-rules (name) | 176 | (defun calc-has-rules (name) |
| 192 | (setq name (calc-var-value name)) | 177 | (setq name (calc-var-value name)) |
| 193 | (and (consp name) | 178 | (and (consp name) |
| 194 | (memq (car name) '(vec calcFunc-assign calcFunc-condition)) | 179 | (memq (car name) '(vec calcFunc-assign calcFunc-condition)) |
| 195 | name) | 180 | name)) |
| 196 | ) | ||
| 197 | 181 | ||
| 198 | (defun math-recompile-eval-rules () | 182 | (defun math-recompile-eval-rules () |
| 199 | (setq math-eval-rules-cache (and (calc-has-rules 'var-EvalRules) | 183 | (setq math-eval-rules-cache (and (calc-has-rules 'var-EvalRules) |
| 200 | (math-compile-rewrites | 184 | (math-compile-rewrites |
| 201 | '(var EvalRules var-EvalRules))) | 185 | '(var EvalRules var-EvalRules))) |
| 202 | math-eval-rules-cache-other (assq nil math-eval-rules-cache) | 186 | math-eval-rules-cache-other (assq nil math-eval-rules-cache) |
| 203 | math-eval-rules-cache-tag (calc-var-value 'var-EvalRules)) | 187 | math-eval-rules-cache-tag (calc-var-value 'var-EvalRules))) |
| 204 | ) | ||
| 205 | 188 | ||
| 206 | 189 | ||
| 207 | ;;; Try to expand a formula according to its definition. | 190 | ;;; Try to expand a formula according to its definition. |
| @@ -213,8 +196,7 @@ | |||
| 213 | (let ((res (let ((math-expand-formulas t)) | 196 | (let ((res (let ((math-expand-formulas t)) |
| 214 | (apply (car expr) (cdr expr))))) | 197 | (apply (car expr) (cdr expr))))) |
| 215 | (and (not (eq (car-safe res) (car expr))) | 198 | (and (not (eq (car-safe res) (car expr))) |
| 216 | res))) | 199 | res)))) |
| 217 | ) | ||
| 218 | 200 | ||
| 219 | 201 | ||
| 220 | 202 | ||
| @@ -270,15 +252,14 @@ | |||
| 270 | (and b | 252 | (and b |
| 271 | (or (null a) | 253 | (or (null a) |
| 272 | (math-beforep (car a) (car b))))) | 254 | (math-beforep (car a) (car b))))) |
| 273 | (t (string-lessp (symbol-name (car a)) (symbol-name (car b))))) | 255 | (t (string-lessp (symbol-name (car a)) (symbol-name (car b)))))) |
| 274 | ) | ||
| 275 | 256 | ||
| 276 | 257 | ||
| 277 | (defun math-simplify-extended (a) | 258 | (defsubst math-simplify-extended (a) |
| 278 | (let ((math-living-dangerously t)) | 259 | (let ((math-living-dangerously t)) |
| 279 | (math-simplify a)) | 260 | (math-simplify a))) |
| 280 | ) | 261 | |
| 281 | (fset 'calcFunc-esimplify (symbol-function 'math-simplify-extended)) | 262 | (defalias 'calcFunc-esimplify 'math-simplify-extended) |
| 282 | 263 | ||
| 283 | (defun math-simplify (top-expr) | 264 | (defun math-simplify (top-expr) |
| 284 | (let ((math-simplifying t) | 265 | (let ((math-simplifying t) |
| @@ -312,9 +293,9 @@ | |||
| 312 | r (cdr r))) | 293 | r (cdr r))) |
| 313 | (not (equal top-expr (setq res (math-simplify-step res))))) | 294 | (not (equal top-expr (setq res (math-simplify-step res))))) |
| 314 | (setq top-expr res))))) | 295 | (setq top-expr res))))) |
| 315 | top-expr | 296 | top-expr) |
| 316 | ) | 297 | |
| 317 | (fset 'calcFunc-simplify (symbol-function 'math-simplify)) | 298 | (defalias 'calcFunc-simplify 'math-simplify) |
| 318 | 299 | ||
| 319 | ;;; The following has a "bug" in that if any recursive simplifications | 300 | ;;; The following has a "bug" in that if any recursive simplifications |
| 320 | ;;; occur only the first handler will be tried; this doesn't really | 301 | ;;; occur only the first handler will be tried; this doesn't really |
| @@ -335,13 +316,12 @@ | |||
| 335 | aa)) | 316 | aa)) |
| 336 | a)) | 317 | a)) |
| 337 | (setq handler (cdr handler)))))) | 318 | (setq handler (cdr handler)))))) |
| 338 | aa)) | 319 | aa))) |
| 339 | ) | ||
| 340 | 320 | ||
| 341 | 321 | ||
| 322 | ;; Placeholder, to synchronize autoloading. | ||
| 342 | (defun math-need-std-simps () | 323 | (defun math-need-std-simps () |
| 343 | ;; Placeholder, to synchronize autoloading. | 324 | nil) |
| 344 | ) | ||
| 345 | 325 | ||
| 346 | (math-defsimplify (+ -) | 326 | (math-defsimplify (+ -) |
| 347 | (math-simplify-plus)) | 327 | (math-simplify-plus)) |
| @@ -378,8 +358,7 @@ | |||
| 378 | (setcar (cdr (cdr expr)) temp) | 358 | (setcar (cdr (cdr expr)) temp) |
| 379 | (setcar expr '+) | 359 | (setcar expr '+) |
| 380 | (setcar (cdr aa) 0))) | 360 | (setcar (cdr aa) 0))) |
| 381 | expr) | 361 | expr)) |
| 382 | ) | ||
| 383 | 362 | ||
| 384 | (math-defsimplify * | 363 | (math-defsimplify * |
| 385 | (math-simplify-times)) | 364 | (math-simplify-times)) |
| @@ -424,8 +403,7 @@ | |||
| 424 | (memq (nth 1 (nth 1 expr)) '(1 -1))) | 403 | (memq (nth 1 (nth 1 expr)) '(1 -1))) |
| 425 | (math-div (math-mul (nth 2 expr) (nth 1 (nth 1 expr))) | 404 | (math-div (math-mul (nth 2 expr) (nth 1 (nth 1 expr))) |
| 426 | (nth 2 (nth 1 expr))) | 405 | (nth 2 (nth 1 expr))) |
| 427 | expr)) | 406 | expr))) |
| 428 | ) | ||
| 429 | 407 | ||
| 430 | (math-defsimplify / | 408 | (math-defsimplify / |
| 431 | (math-simplify-divide)) | 409 | (math-simplify-divide)) |
| @@ -473,8 +451,7 @@ | |||
| 473 | (math-simplify-divisor (cdr n) (cdr (cdr expr)) nover t)) | 451 | (math-simplify-divisor (cdr n) (cdr (cdr expr)) nover t)) |
| 474 | (setq np (cdr (cdr n)))) | 452 | (setq np (cdr (cdr n)))) |
| 475 | (math-simplify-divisor np (cdr (cdr expr)) nover t) | 453 | (math-simplify-divisor np (cdr (cdr expr)) nover t) |
| 476 | expr) | 454 | expr)) |
| 477 | ) | ||
| 478 | 455 | ||
| 479 | (defun math-simplify-divisor (np dp nover dover) | 456 | (defun math-simplify-divisor (np dp nover dover) |
| 480 | (cond ((eq (car-safe (car dp)) '/) | 457 | (cond ((eq (car-safe (car dp)) '/) |
| @@ -498,8 +475,7 @@ | |||
| 498 | (setq safe (or scalar (math-known-scalarp (nth 1 d) t)) | 475 | (setq safe (or scalar (math-known-scalarp (nth 1 d) t)) |
| 499 | dp (cdr (cdr d)))) | 476 | dp (cdr (cdr d)))) |
| 500 | (if safe | 477 | (if safe |
| 501 | (math-simplify-one-divisor np dp))))) | 478 | (math-simplify-one-divisor np dp)))))) |
| 502 | ) | ||
| 503 | 479 | ||
| 504 | (defun math-simplify-one-divisor (np dp) | 480 | (defun math-simplify-one-divisor (np dp) |
| 505 | (if (setq temp (math-combine-prod (car np) (car dp) nover dover t)) | 481 | (if (setq temp (math-combine-prod (car np) (car dp) nover dover t)) |
| @@ -516,8 +492,7 @@ | |||
| 516 | (progn | 492 | (progn |
| 517 | (setcar np (math-mul (car np) | 493 | (setcar np (math-mul (car np) |
| 518 | (list 'calcFunc-sqrt (nth 1 (car dp))))) | 494 | (list 'calcFunc-sqrt (nth 1 (car dp))))) |
| 519 | (setcar dp (nth 1 (car dp)))))) | 495 | (setcar dp (nth 1 (car dp))))))) |
| 520 | ) | ||
| 521 | 496 | ||
| 522 | (defun math-common-constant-factor (expr) | 497 | (defun math-common-constant-factor (expr) |
| 523 | (if (Math-realp expr) | 498 | (if (Math-realp expr) |
| @@ -537,8 +512,7 @@ | |||
| 537 | (if (eq (car expr) '/) | 512 | (if (eq (car expr) '/) |
| 538 | (or (math-common-constant-factor (nth 1 expr)) | 513 | (or (math-common-constant-factor (nth 1 expr)) |
| 539 | (and (Math-integerp (nth 2 expr)) | 514 | (and (Math-integerp (nth 2 expr)) |
| 540 | (list 'frac 1 (math-abs (nth 2 expr))))))))) | 515 | (list 'frac 1 (math-abs (nth 2 expr)))))))))) |
| 541 | ) | ||
| 542 | 516 | ||
| 543 | (defun math-cancel-common-factor (expr val) | 517 | (defun math-cancel-common-factor (expr val) |
| 544 | (if (memq (car-safe expr) '(+ - cplx sdev)) | 518 | (if (memq (car-safe expr) '(+ - cplx sdev)) |
| @@ -548,8 +522,7 @@ | |||
| 548 | expr) | 522 | expr) |
| 549 | (if (eq (car-safe expr) '*) | 523 | (if (eq (car-safe expr) '*) |
| 550 | (math-mul (math-cancel-common-factor (nth 1 expr) val) (nth 2 expr)) | 524 | (math-mul (math-cancel-common-factor (nth 1 expr) val) (nth 2 expr)) |
| 551 | (math-div expr val))) | 525 | (math-div expr val)))) |
| 552 | ) | ||
| 553 | 526 | ||
| 554 | (defun math-frac-gcd (a b) | 527 | (defun math-frac-gcd (a b) |
| 555 | (if (Math-zerop a) | 528 | (if (Math-zerop a) |
| @@ -562,8 +535,7 @@ | |||
| 562 | (and (Math-integerp a) (setq a (list 'frac a 1))) | 535 | (and (Math-integerp a) (setq a (list 'frac a 1))) |
| 563 | (and (Math-integerp b) (setq b (list 'frac b 1))) | 536 | (and (Math-integerp b) (setq b (list 'frac b 1))) |
| 564 | (math-make-frac (math-gcd (nth 1 a) (nth 1 b)) | 537 | (math-make-frac (math-gcd (nth 1 a) (nth 1 b)) |
| 565 | (math-gcd (nth 2 a) (nth 2 b)))))) | 538 | (math-gcd (nth 2 a) (nth 2 b))))))) |
| 566 | ) | ||
| 567 | 539 | ||
| 568 | (math-defsimplify % | 540 | (math-defsimplify % |
| 569 | (math-simplify-mod)) | 541 | (math-simplify-mod)) |
| @@ -600,8 +572,7 @@ | |||
| 600 | (math-known-integerp (if lin | 572 | (math-known-integerp (if lin |
| 601 | (math-mul (nth 1 lin) (nth 2 lin)) | 573 | (math-mul (nth 1 lin) (nth 2 lin)) |
| 602 | (nth 1 expr))) | 574 | (nth 1 expr))) |
| 603 | (if lin (math-mod (car lin) 1) 0))))) | 575 | (if lin (math-mod (car lin) 1) 0)))))) |
| 604 | ) | ||
| 605 | 576 | ||
| 606 | (math-defsimplify (calcFunc-eq calcFunc-neq calcFunc-lt | 577 | (math-defsimplify (calcFunc-eq calcFunc-neq calcFunc-lt |
| 607 | calcFunc-gt calcFunc-leq calcFunc-geq) | 578 | calcFunc-gt calcFunc-leq calcFunc-geq) |
| @@ -636,8 +607,7 @@ | |||
| 636 | ((eq (car expr) 'calcFunc-geq) | 607 | ((eq (car expr) 'calcFunc-geq) |
| 637 | (or (and (eq signs 1) 0) | 608 | (or (and (eq signs 1) 0) |
| 638 | (and (memq signs '(2 4 6)) 1)))) | 609 | (and (memq signs '(2 4 6)) 1)))) |
| 639 | expr))) | 610 | expr)))) |
| 640 | ) | ||
| 641 | 611 | ||
| 642 | (defun math-simplify-add-term (np dp minus lplain) | 612 | (defun math-simplify-add-term (np dp minus lplain) |
| 643 | (or (math-vectorp (car np)) | 613 | (or (math-vectorp (car np)) |
| @@ -666,8 +636,7 @@ | |||
| 666 | (setcar dp 0)) | 636 | (setcar dp 0)) |
| 667 | (progn | 637 | (progn |
| 668 | (setcar np 0) | 638 | (setcar np 0) |
| 669 | (setcar dp (setq n (math-neg temp)))))))) | 639 | (setcar dp (setq n (math-neg temp))))))))) |
| 670 | ) | ||
| 671 | 640 | ||
| 672 | (math-defsimplify calcFunc-sin | 641 | (math-defsimplify calcFunc-sin |
| 673 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) | 642 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsin) |
| @@ -695,8 +664,7 @@ | |||
| 695 | (list '* (list 'calcFunc-sin (list '* (1- n) a)) | 664 | (list '* (list 'calcFunc-sin (list '* (1- n) a)) |
| 696 | (list 'calcFunc-cos a)) | 665 | (list 'calcFunc-cos a)) |
| 697 | (list '* (list 'calcFunc-cos (list '* (1- n) a)) | 666 | (list '* (list 'calcFunc-cos (list '* (1- n) a)) |
| 698 | (list 'calcFunc-sin a))))))) | 667 | (list 'calcFunc-sin a)))))))) |
| 699 | ) | ||
| 700 | 668 | ||
| 701 | (math-defsimplify calcFunc-cos | 669 | (math-defsimplify calcFunc-cos |
| 702 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos) | 670 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccos) |
| @@ -724,8 +692,7 @@ | |||
| 724 | (list '* (list 'calcFunc-cos (list '* (1- n) a)) | 692 | (list '* (list 'calcFunc-cos (list '* (1- n) a)) |
| 725 | (list 'calcFunc-cos a)) | 693 | (list 'calcFunc-cos a)) |
| 726 | (list '* (list 'calcFunc-sin (list '* (1- n) a)) | 694 | (list '* (list 'calcFunc-sin (list '* (1- n) a)) |
| 727 | (list 'calcFunc-sin a))))))) | 695 | (list 'calcFunc-sin a)))))))) |
| 728 | ) | ||
| 729 | 696 | ||
| 730 | (defun math-should-expand-trig (x &optional hyperbolic) | 697 | (defun math-should-expand-trig (x &optional hyperbolic) |
| 731 | (let ((m (math-is-multiple x))) | 698 | (let ((m (math-is-multiple x))) |
| @@ -739,8 +706,7 @@ | |||
| 739 | '(calcFunc-arcsin calcFunc-arccos calcFunc-arctan))) | 706 | '(calcFunc-arcsin calcFunc-arccos calcFunc-arctan))) |
| 740 | (and (eq (car-safe (nth 1 m)) 'calcFunc-ln) | 707 | (and (eq (car-safe (nth 1 m)) 'calcFunc-ln) |
| 741 | (eq hyperbolic 'exp))) | 708 | (eq hyperbolic 'exp))) |
| 742 | m)) | 709 | m))) |
| 743 | ) | ||
| 744 | 710 | ||
| 745 | (defun math-known-sin (plus n mul off) | 711 | (defun math-known-sin (plus n mul off) |
| 746 | (setq n (math-mul n mul)) | 712 | (setq n (math-mul n mul)) |
| @@ -778,8 +744,7 @@ | |||
| 778 | (60 . 1))))) | 744 | (60 . 1))))) |
| 779 | (cond ((eq n 0) (math-normalize (list 'calcFunc-sin plus))) | 745 | (cond ((eq n 0) (math-normalize (list 'calcFunc-sin plus))) |
| 780 | ((eq n 60) (math-normalize (list 'calcFunc-cos plus))) | 746 | ((eq n 60) (math-normalize (list 'calcFunc-cos plus))) |
| 781 | (t nil))))) | 747 | (t nil)))))) |
| 782 | ) | ||
| 783 | 748 | ||
| 784 | (math-defsimplify calcFunc-tan | 749 | (math-defsimplify calcFunc-tan |
| 785 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan) | 750 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctan) |
| @@ -808,8 +773,7 @@ | |||
| 808 | (math-div (math-sub 1 (list 'calcFunc-cos (nth 1 m))) | 773 | (math-div (math-sub 1 (list 'calcFunc-cos (nth 1 m))) |
| 809 | (list 'calcFunc-sin (nth 1 m))) | 774 | (list 'calcFunc-sin (nth 1 m))) |
| 810 | (math-div (list 'calcFunc-sin (nth 1 expr)) | 775 | (math-div (list 'calcFunc-sin (nth 1 expr)) |
| 811 | (list 'calcFunc-cos (nth 1 expr))))))) | 776 | (list 'calcFunc-cos (nth 1 expr)))))))) |
| 812 | ) | ||
| 813 | 777 | ||
| 814 | (defun math-known-tan (plus n mul) | 778 | (defun math-known-tan (plus n mul) |
| 815 | (setq n (math-mul n mul)) | 779 | (setq n (math-mul n mul)) |
| @@ -841,8 +805,7 @@ | |||
| 841 | (cond ((eq n 0) (math-normalize (list 'calcFunc-tan plus))) | 805 | (cond ((eq n 0) (math-normalize (list 'calcFunc-tan plus))) |
| 842 | ((eq n 60) (math-normalize (list '/ -1 | 806 | ((eq n 60) (math-normalize (list '/ -1 |
| 843 | (list 'calcFunc-tan plus)))) | 807 | (list 'calcFunc-tan plus)))) |
| 844 | (t nil))))) | 808 | (t nil)))))) |
| 845 | ) | ||
| 846 | 809 | ||
| 847 | (math-defsimplify calcFunc-sinh | 810 | (math-defsimplify calcFunc-sinh |
| 848 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh) | 811 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arcsinh) |
| @@ -865,8 +828,7 @@ | |||
| 865 | (list '* (list 'calcFunc-sinh (list '* (1- n) a)) | 828 | (list '* (list 'calcFunc-sinh (list '* (1- n) a)) |
| 866 | (list 'calcFunc-cosh a)) | 829 | (list 'calcFunc-cosh a)) |
| 867 | (list '* (list 'calcFunc-cosh (list '* (1- n) a)) | 830 | (list '* (list 'calcFunc-cosh (list '* (1- n) a)) |
| 868 | (list 'calcFunc-sinh a)))))))) | 831 | (list 'calcFunc-sinh a))))))))) |
| 869 | ) | ||
| 870 | 832 | ||
| 871 | (math-defsimplify calcFunc-cosh | 833 | (math-defsimplify calcFunc-cosh |
| 872 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh) | 834 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arccosh) |
| @@ -889,8 +851,7 @@ | |||
| 889 | (list '* (list 'calcFunc-cosh (list '* (1- n) a)) | 851 | (list '* (list 'calcFunc-cosh (list '* (1- n) a)) |
| 890 | (list 'calcFunc-cosh a)) | 852 | (list 'calcFunc-cosh a)) |
| 891 | (list '* (list 'calcFunc-sinh (list '* (1- n) a)) | 853 | (list '* (list 'calcFunc-sinh (list '* (1- n) a)) |
| 892 | (list 'calcFunc-sinh a)))))))) | 854 | (list 'calcFunc-sinh a))))))))) |
| 893 | ) | ||
| 894 | 855 | ||
| 895 | (math-defsimplify calcFunc-tanh | 856 | (math-defsimplify calcFunc-tanh |
| 896 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh) | 857 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-arctanh) |
| @@ -913,8 +874,7 @@ | |||
| 913 | (math-div (math-sub (list 'calcFunc-cosh (nth 1 m)) 1) | 874 | (math-div (math-sub (list 'calcFunc-cosh (nth 1 m)) 1) |
| 914 | (list 'calcFunc-sinh (nth 1 m))) | 875 | (list 'calcFunc-sinh (nth 1 m))) |
| 915 | (math-div (list 'calcFunc-sinh (nth 1 expr)) | 876 | (math-div (list 'calcFunc-sinh (nth 1 expr)) |
| 916 | (list 'calcFunc-cosh (nth 1 expr))))))) | 877 | (list 'calcFunc-cosh (nth 1 expr)))))))) |
| 917 | ) | ||
| 918 | 878 | ||
| 919 | (math-defsimplify calcFunc-arcsin | 879 | (math-defsimplify calcFunc-arcsin |
| 920 | (or (and (math-looks-negp (nth 1 expr)) | 880 | (or (and (math-looks-negp (nth 1 expr)) |
| @@ -929,8 +889,7 @@ | |||
| 929 | (and math-living-dangerously | 889 | (and math-living-dangerously |
| 930 | (eq (car-safe (nth 1 expr)) 'calcFunc-cos) | 890 | (eq (car-safe (nth 1 expr)) 'calcFunc-cos) |
| 931 | (math-sub (math-quarter-circle t) | 891 | (math-sub (math-quarter-circle t) |
| 932 | (nth 1 (nth 1 expr))))) | 892 | (nth 1 (nth 1 expr)))))) |
| 933 | ) | ||
| 934 | 893 | ||
| 935 | (math-defsimplify calcFunc-arccos | 894 | (math-defsimplify calcFunc-arccos |
| 936 | (or (and (eq (nth 1 expr) 0) | 895 | (or (and (eq (nth 1 expr) 0) |
| @@ -947,8 +906,7 @@ | |||
| 947 | (and math-living-dangerously | 906 | (and math-living-dangerously |
| 948 | (eq (car-safe (nth 1 expr)) 'calcFunc-sin) | 907 | (eq (car-safe (nth 1 expr)) 'calcFunc-sin) |
| 949 | (math-sub (math-quarter-circle t) | 908 | (math-sub (math-quarter-circle t) |
| 950 | (nth 1 (nth 1 expr))))) | 909 | (nth 1 (nth 1 expr)))))) |
| 951 | ) | ||
| 952 | 910 | ||
| 953 | (math-defsimplify calcFunc-arctan | 911 | (math-defsimplify calcFunc-arctan |
| 954 | (or (and (math-looks-negp (nth 1 expr)) | 912 | (or (and (math-looks-negp (nth 1 expr)) |
| @@ -957,8 +915,7 @@ | |||
| 957 | (math-div (math-half-circle t) 4)) | 915 | (math-div (math-half-circle t) 4)) |
| 958 | (and math-living-dangerously | 916 | (and math-living-dangerously |
| 959 | (eq (car-safe (nth 1 expr)) 'calcFunc-tan) | 917 | (eq (car-safe (nth 1 expr)) 'calcFunc-tan) |
| 960 | (nth 1 (nth 1 expr)))) | 918 | (nth 1 (nth 1 expr))))) |
| 961 | ) | ||
| 962 | 919 | ||
| 963 | (math-defsimplify calcFunc-arcsinh | 920 | (math-defsimplify calcFunc-arcsinh |
| 964 | (or (and (math-looks-negp (nth 1 expr)) | 921 | (or (and (math-looks-negp (nth 1 expr)) |
| @@ -966,15 +923,13 @@ | |||
| 966 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-sinh) | 923 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-sinh) |
| 967 | (or math-living-dangerously | 924 | (or math-living-dangerously |
| 968 | (math-known-realp (nth 1 (nth 1 expr)))) | 925 | (math-known-realp (nth 1 (nth 1 expr)))) |
| 969 | (nth 1 (nth 1 expr)))) | 926 | (nth 1 (nth 1 expr))))) |
| 970 | ) | ||
| 971 | 927 | ||
| 972 | (math-defsimplify calcFunc-arccosh | 928 | (math-defsimplify calcFunc-arccosh |
| 973 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-cosh) | 929 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-cosh) |
| 974 | (or math-living-dangerously | 930 | (or math-living-dangerously |
| 975 | (math-known-realp (nth 1 (nth 1 expr)))) | 931 | (math-known-realp (nth 1 (nth 1 expr)))) |
| 976 | (nth 1 (nth 1 expr))) | 932 | (nth 1 (nth 1 expr)))) |
| 977 | ) | ||
| 978 | 933 | ||
| 979 | (math-defsimplify calcFunc-arctanh | 934 | (math-defsimplify calcFunc-arctanh |
| 980 | (or (and (math-looks-negp (nth 1 expr)) | 935 | (or (and (math-looks-negp (nth 1 expr)) |
| @@ -982,12 +937,10 @@ | |||
| 982 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-tanh) | 937 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-tanh) |
| 983 | (or math-living-dangerously | 938 | (or math-living-dangerously |
| 984 | (math-known-realp (nth 1 (nth 1 expr)))) | 939 | (math-known-realp (nth 1 (nth 1 expr)))) |
| 985 | (nth 1 (nth 1 expr)))) | 940 | (nth 1 (nth 1 expr))))) |
| 986 | ) | ||
| 987 | 941 | ||
| 988 | (math-defsimplify calcFunc-sqrt | 942 | (math-defsimplify calcFunc-sqrt |
| 989 | (math-simplify-sqrt) | 943 | (math-simplify-sqrt)) |
| 990 | ) | ||
| 991 | 944 | ||
| 992 | (defun math-simplify-sqrt () | 945 | (defun math-simplify-sqrt () |
| 993 | (or (and (eq (car-safe (nth 1 expr)) 'frac) | 946 | (or (and (eq (car-safe (nth 1 expr)) 'frac) |
| @@ -1069,8 +1022,7 @@ | |||
| 1069 | (math-mul | 1022 | (math-mul |
| 1070 | out | 1023 | out |
| 1071 | (list 'calcFunc-sqrt | 1024 | (list 'calcFunc-sqrt |
| 1072 | (math-mul sums rest))))))))))) | 1025 | (math-mul sums rest)))))))))))) |
| 1073 | ) | ||
| 1074 | 1026 | ||
| 1075 | ;;; Rather than factoring x into primes, just check for the first ten primes. | 1027 | ;;; Rather than factoring x into primes, just check for the first ten primes. |
| 1076 | (defun math-squared-factor (x) | 1028 | (defun math-squared-factor (x) |
| @@ -1083,12 +1035,10 @@ | |||
| 1083 | (setq x (car res) | 1035 | (setq x (car res) |
| 1084 | fac (math-mul fac (car prsqr))) | 1036 | fac (math-mul fac (car prsqr))) |
| 1085 | (setq prsqr (cdr prsqr)))) | 1037 | (setq prsqr (cdr prsqr)))) |
| 1086 | fac)) | 1038 | fac))) |
| 1087 | ) | ||
| 1088 | 1039 | ||
| 1089 | (math-defsimplify calcFunc-exp | 1040 | (math-defsimplify calcFunc-exp |
| 1090 | (math-simplify-exp (nth 1 expr)) | 1041 | (math-simplify-exp (nth 1 expr))) |
| 1091 | ) | ||
| 1092 | 1042 | ||
| 1093 | (defun math-simplify-exp (x) | 1043 | (defun math-simplify-exp (x) |
| 1094 | (or (and (eq (car-safe x) 'calcFunc-ln) | 1044 | (or (and (eq (car-safe x) 'calcFunc-ln) |
| @@ -1116,8 +1066,7 @@ | |||
| 1116 | (and n | 1066 | (and n |
| 1117 | (setq s (math-known-sin (car n) (nth 1 n) 120 0)) | 1067 | (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)) | 1068 | (setq c (math-known-sin (car n) (nth 1 n) 120 300)) |
| 1119 | (list '+ c (list '* s '(var i var-i))))))) | 1069 | (list '+ c (list '* s '(var i var-i)))))))) |
| 1120 | ) | ||
| 1121 | 1070 | ||
| 1122 | (math-defsimplify calcFunc-ln | 1071 | (math-defsimplify calcFunc-ln |
| 1123 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-exp) | 1072 | (or (and (eq (car-safe (nth 1 expr)) 'calcFunc-exp) |
| @@ -1142,8 +1091,7 @@ | |||
| 1142 | '(/ (* (var pi var-pi) (var i var-i)) 2))) | 1091 | '(/ (* (var pi var-pi) (var i var-i)) 2))) |
| 1143 | (and (memq ips '(1 3)) | 1092 | (and (memq ips '(1 3)) |
| 1144 | (math-sub (list 'calcFunc-ln (math-neg ip)) | 1093 | (math-sub (list 'calcFunc-ln (math-neg ip)) |
| 1145 | '(/ (* (var pi var-pi) (var i var-i)) 2))))))) | 1094 | '(/ (* (var pi var-pi) (var i var-i)) 2)))))))) |
| 1146 | ) | ||
| 1147 | 1095 | ||
| 1148 | (math-defsimplify ^ | 1096 | (math-defsimplify ^ |
| 1149 | (math-simplify-pow)) | 1097 | (math-simplify-pow)) |
| @@ -1206,31 +1154,27 @@ | |||
| 1206 | (and (eq (math-quarter-integer (nth 2 expr)) 2) | 1154 | (and (eq (math-quarter-integer (nth 2 expr)) 2) |
| 1207 | (let ((temp (math-simplify-sqrt))) | 1155 | (let ((temp (math-simplify-sqrt))) |
| 1208 | (and temp | 1156 | (and temp |
| 1209 | (list '^ temp (math-mul (nth 2 expr) 2)))))) | 1157 | (list '^ temp (math-mul (nth 2 expr) 2))))))) |
| 1210 | ) | ||
| 1211 | 1158 | ||
| 1212 | (math-defsimplify calcFunc-log10 | 1159 | (math-defsimplify calcFunc-log10 |
| 1213 | (and (eq (car-safe (nth 1 expr)) '^) | 1160 | (and (eq (car-safe (nth 1 expr)) '^) |
| 1214 | (math-equal-int (nth 1 (nth 1 expr)) 10) | 1161 | (math-equal-int (nth 1 (nth 1 expr)) 10) |
| 1215 | (or math-living-dangerously | 1162 | (or math-living-dangerously |
| 1216 | (math-known-realp (nth 2 (nth 1 expr)))) | 1163 | (math-known-realp (nth 2 (nth 1 expr)))) |
| 1217 | (nth 2 (nth 1 expr))) | 1164 | (nth 2 (nth 1 expr)))) |
| 1218 | ) | ||
| 1219 | 1165 | ||
| 1220 | 1166 | ||
| 1221 | (math-defsimplify calcFunc-erf | 1167 | (math-defsimplify calcFunc-erf |
| 1222 | (or (and (math-looks-negp (nth 1 expr)) | 1168 | (or (and (math-looks-negp (nth 1 expr)) |
| 1223 | (math-neg (list 'calcFunc-erf (math-neg (nth 1 expr))))) | 1169 | (math-neg (list 'calcFunc-erf (math-neg (nth 1 expr))))) |
| 1224 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-conj) | 1170 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-conj) |
| 1225 | (list 'calcFunc-conj (list 'calcFunc-erf (nth 1 (nth 1 expr)))))) | 1171 | (list 'calcFunc-conj (list 'calcFunc-erf (nth 1 (nth 1 expr))))))) |
| 1226 | ) | ||
| 1227 | 1172 | ||
| 1228 | (math-defsimplify calcFunc-erfc | 1173 | (math-defsimplify calcFunc-erfc |
| 1229 | (or (and (math-looks-negp (nth 1 expr)) | 1174 | (or (and (math-looks-negp (nth 1 expr)) |
| 1230 | (math-sub 2 (list 'calcFunc-erfc (math-neg (nth 1 expr))))) | 1175 | (math-sub 2 (list 'calcFunc-erfc (math-neg (nth 1 expr))))) |
| 1231 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-conj) | 1176 | (and (eq (car-safe (nth 1 expr)) 'calcFunc-conj) |
| 1232 | (list 'calcFunc-conj (list 'calcFunc-erfc (nth 1 (nth 1 expr)))))) | 1177 | (list 'calcFunc-conj (list 'calcFunc-erfc (nth 1 (nth 1 expr))))))) |
| 1233 | ) | ||
| 1234 | 1178 | ||
| 1235 | 1179 | ||
| 1236 | (defun math-linear-in (expr term &optional always) | 1180 | (defun math-linear-in (expr term &optional always) |
| @@ -1239,16 +1183,15 @@ | |||
| 1239 | (p (math-is-polynomial expr term 1))) | 1183 | (p (math-is-polynomial expr term 1))) |
| 1240 | (and (cdr p) | 1184 | (and (cdr p) |
| 1241 | p)) | 1185 | p)) |
| 1242 | (and always (list expr 0))) | 1186 | (and always (list expr 0)))) |
| 1243 | ) | ||
| 1244 | 1187 | ||
| 1245 | (defun math-multiple-of (expr term) | 1188 | (defun math-multiple-of (expr term) |
| 1246 | (let ((p (math-linear-in expr term))) | 1189 | (let ((p (math-linear-in expr term))) |
| 1247 | (and p | 1190 | (and p |
| 1248 | (math-zerop (car p)) | 1191 | (math-zerop (car p)) |
| 1249 | (nth 1 p))) | 1192 | (nth 1 p)))) |
| 1250 | ) | ||
| 1251 | 1193 | ||
| 1194 | ; not perfect, but it'll do | ||
| 1252 | (defun math-integer-plus (expr) | 1195 | (defun math-integer-plus (expr) |
| 1253 | (cond ((Math-integerp expr) | 1196 | (cond ((Math-integerp expr) |
| 1254 | (list 0 expr)) | 1197 | (list 0 expr)) |
| @@ -1260,8 +1203,7 @@ | |||
| 1260 | (Math-integerp (nth 2 expr))) | 1203 | (Math-integerp (nth 2 expr))) |
| 1261 | (list (nth 1 expr) | 1204 | (list (nth 1 expr) |
| 1262 | (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr))))) | 1205 | (if (eq (car expr) '+) (nth 2 expr) (math-neg (nth 2 expr))))) |
| 1263 | (t nil)) ; not perfect, but it'll do | 1206 | (t nil))) |
| 1264 | ) | ||
| 1265 | 1207 | ||
| 1266 | (defun math-is-linear (expr &optional always) | 1208 | (defun math-is-linear (expr &optional always) |
| 1267 | (let ((offset nil) | 1209 | (let ((offset nil) |
| @@ -1284,8 +1226,7 @@ | |||
| 1284 | (if offset | 1226 | (if offset |
| 1285 | (list offset (or (car coef) 1) (or (nth 1 coef) expr)) | 1227 | (list offset (or (car coef) 1) (or (nth 1 coef) expr)) |
| 1286 | (if coef | 1228 | (if coef |
| 1287 | (cons 0 coef)))) | 1229 | (cons 0 coef))))) |
| 1288 | ) | ||
| 1289 | 1230 | ||
| 1290 | (defun math-is-multiple (expr &optional always) | 1231 | (defun math-is-multiple (expr &optional always) |
| 1291 | (or (if (eq (car-safe expr) '*) | 1232 | (or (if (eq (car-safe expr) '*) |
| @@ -1312,8 +1253,7 @@ | |||
| 1312 | (and (eq always 1) | 1253 | (and (eq always 1) |
| 1313 | (list expr 1)) | 1254 | (list expr 1)) |
| 1314 | (and always | 1255 | (and always |
| 1315 | (list 1 expr)))) | 1256 | (list 1 expr))))) |
| 1316 | ) | ||
| 1317 | 1257 | ||
| 1318 | (defun calcFunc-lin (expr &optional var) | 1258 | (defun calcFunc-lin (expr &optional var) |
| 1319 | (if var | 1259 | (if var |
| @@ -1322,8 +1262,7 @@ | |||
| 1322 | (list 'vec (car res) (nth 1 res) var)) | 1262 | (list 'vec (car res) (nth 1 res) var)) |
| 1323 | (let ((res (math-is-linear expr t))) | 1263 | (let ((res (math-is-linear expr t))) |
| 1324 | (or res (math-reject-arg expr "Linear term expected")) | 1264 | (or res (math-reject-arg expr "Linear term expected")) |
| 1325 | (cons 'vec res))) | 1265 | (cons 'vec res)))) |
| 1326 | ) | ||
| 1327 | 1266 | ||
| 1328 | (defun calcFunc-linnt (expr &optional var) | 1267 | (defun calcFunc-linnt (expr &optional var) |
| 1329 | (if var | 1268 | (if var |
| @@ -1332,22 +1271,19 @@ | |||
| 1332 | (list 'vec (car res) (nth 1 res) var)) | 1271 | (list 'vec (car res) (nth 1 res) var)) |
| 1333 | (let ((res (math-is-linear expr))) | 1272 | (let ((res (math-is-linear expr))) |
| 1334 | (or res (math-reject-arg expr "Linear term expected")) | 1273 | (or res (math-reject-arg expr "Linear term expected")) |
| 1335 | (cons 'vec res))) | 1274 | (cons 'vec res)))) |
| 1336 | ) | ||
| 1337 | 1275 | ||
| 1338 | (defun calcFunc-islin (expr &optional var) | 1276 | (defun calcFunc-islin (expr &optional var) |
| 1339 | (if (and (Math-objvecp expr) (not var)) | 1277 | (if (and (Math-objvecp expr) (not var)) |
| 1340 | 0 | 1278 | 0 |
| 1341 | (calcFunc-lin expr var) | 1279 | (calcFunc-lin expr var) |
| 1342 | 1) | 1280 | 1)) |
| 1343 | ) | ||
| 1344 | 1281 | ||
| 1345 | (defun calcFunc-islinnt (expr &optional var) | 1282 | (defun calcFunc-islinnt (expr &optional var) |
| 1346 | (if (Math-objvecp expr) | 1283 | (if (Math-objvecp expr) |
| 1347 | 0 | 1284 | 0 |
| 1348 | (calcFunc-linnt expr var) | 1285 | (calcFunc-linnt expr var) |
| 1349 | 1) | 1286 | 1)) |
| 1350 | ) | ||
| 1351 | 1287 | ||
| 1352 | 1288 | ||
| 1353 | 1289 | ||
| @@ -1364,8 +1300,7 @@ | |||
| 1364 | (setq num (+ num (or (math-expr-contains-count | 1300 | (setq num (+ num (or (math-expr-contains-count |
| 1365 | (car expr) thing) 0)))) | 1301 | (car expr) thing) 0)))) |
| 1366 | (and (> num 0) | 1302 | (and (> num 0) |
| 1367 | num)))) | 1303 | num))))) |
| 1368 | ) | ||
| 1369 | 1304 | ||
| 1370 | (defun math-expr-contains (expr thing) | 1305 | (defun math-expr-contains (expr thing) |
| 1371 | (cond ((equal expr thing) 1) | 1306 | (cond ((equal expr thing) 1) |
| @@ -1373,8 +1308,7 @@ | |||
| 1373 | (t | 1308 | (t |
| 1374 | (while (and (setq expr (cdr expr)) | 1309 | (while (and (setq expr (cdr expr)) |
| 1375 | (not (math-expr-contains (car expr) thing)))) | 1310 | (not (math-expr-contains (car expr) thing)))) |
| 1376 | expr)) | 1311 | expr))) |
| 1377 | ) | ||
| 1378 | 1312 | ||
| 1379 | ;;; Return non-nil if any variable of thing occurs in expr. | 1313 | ;;; Return non-nil if any variable of thing occurs in expr. |
| 1380 | (defun math-expr-depends (expr thing) | 1314 | (defun math-expr-depends (expr thing) |
| @@ -1383,14 +1317,13 @@ | |||
| 1383 | (math-expr-contains expr thing)) | 1317 | (math-expr-contains expr thing)) |
| 1384 | (while (and (setq thing (cdr thing)) | 1318 | (while (and (setq thing (cdr thing)) |
| 1385 | (not (math-expr-depends expr (car thing))))) | 1319 | (not (math-expr-depends expr (car thing))))) |
| 1386 | thing) | 1320 | thing)) |
| 1387 | ) | ||
| 1388 | 1321 | ||
| 1389 | ;;; Substitute all occurrences of old for new in expr (non-destructive). | 1322 | ;;; Substitute all occurrences of old for new in expr (non-destructive). |
| 1390 | (defun math-expr-subst (expr old new) | 1323 | (defun math-expr-subst (expr old new) |
| 1391 | (math-expr-subst-rec expr) | 1324 | (math-expr-subst-rec expr)) |
| 1392 | ) | 1325 | |
| 1393 | (fset 'calcFunc-subst (symbol-function 'math-expr-subst)) | 1326 | (defalias 'calcFunc-subst 'math-expr-subst) |
| 1394 | 1327 | ||
| 1395 | (defun math-expr-subst-rec (expr) | 1328 | (defun math-expr-subst-rec (expr) |
| 1396 | (cond ((equal expr old) new) | 1329 | (cond ((equal expr old) new) |
| @@ -1405,8 +1338,7 @@ | |||
| 1405 | (math-expr-subst-rec (nth 2 expr))))) | 1338 | (math-expr-subst-rec (nth 2 expr))))) |
| 1406 | (t | 1339 | (t |
| 1407 | (cons (car expr) | 1340 | (cons (car expr) |
| 1408 | (mapcar 'math-expr-subst-rec (cdr expr))))) | 1341 | (mapcar 'math-expr-subst-rec (cdr expr)))))) |
| 1409 | ) | ||
| 1410 | 1342 | ||
| 1411 | ;;; Various measures of the size of an expression. | 1343 | ;;; Various measures of the size of an expression. |
| 1412 | (defun math-expr-weight (expr) | 1344 | (defun math-expr-weight (expr) |
| @@ -1415,8 +1347,7 @@ | |||
| 1415 | (let ((w 1)) | 1347 | (let ((w 1)) |
| 1416 | (while (setq expr (cdr expr)) | 1348 | (while (setq expr (cdr expr)) |
| 1417 | (setq w (+ w (math-expr-weight (car expr))))) | 1349 | (setq w (+ w (math-expr-weight (car expr))))) |
| 1418 | w)) | 1350 | w))) |
| 1419 | ) | ||
| 1420 | 1351 | ||
| 1421 | (defun math-expr-height (expr) | 1352 | (defun math-expr-height (expr) |
| 1422 | (if (Math-primp expr) | 1353 | (if (Math-primp expr) |
| @@ -1424,8 +1355,7 @@ | |||
| 1424 | (let ((h 0)) | 1355 | (let ((h 0)) |
| 1425 | (while (setq expr (cdr expr)) | 1356 | (while (setq expr (cdr expr)) |
| 1426 | (setq h (max h (math-expr-height (car expr))))) | 1357 | (setq h (max h (math-expr-height (car expr))))) |
| 1427 | (1+ h))) | 1358 | (1+ h)))) |
| 1428 | ) | ||
| 1429 | 1359 | ||
| 1430 | 1360 | ||
| 1431 | 1361 | ||
| @@ -1437,8 +1367,7 @@ | |||
| 1437 | (if (cdr p) | 1367 | (if (cdr p) |
| 1438 | (math-normalize ; fix selection bug | 1368 | (math-normalize ; fix selection bug |
| 1439 | (math-build-polynomial-expr p base)) | 1369 | (math-build-polynomial-expr p base)) |
| 1440 | expr)) | 1370 | expr))) |
| 1441 | ) | ||
| 1442 | 1371 | ||
| 1443 | ;;; If expr is of the form "a + bx + cx^2 + ...", return the list (a b c ...), | 1372 | ;;; 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 | 1373 | ;;; else return nil if not in polynomial form. If "loose", coefficients |
| @@ -1450,8 +1379,7 @@ | |||
| 1450 | (poly (math-is-poly-rec expr math-poly-neg-powers))) | 1379 | (poly (math-is-poly-rec expr math-poly-neg-powers))) |
| 1451 | (and (or (null degree) | 1380 | (and (or (null degree) |
| 1452 | (<= (length poly) (1+ degree))) | 1381 | (<= (length poly) (1+ degree))) |
| 1453 | poly)) | 1382 | poly))) |
| 1454 | ) | ||
| 1455 | 1383 | ||
| 1456 | (defun math-is-poly-rec (expr negpow) | 1384 | (defun math-is-poly-rec (expr negpow) |
| 1457 | (math-poly-simplify | 1385 | (math-poly-simplify |
| @@ -1550,8 +1478,7 @@ | |||
| 1550 | (and (or (not (math-poly-depends expr var)) | 1478 | (and (or (not (math-poly-depends expr var)) |
| 1551 | loose) | 1479 | loose) |
| 1552 | (not (eq (car expr) 'vec)) | 1480 | (not (eq (car expr) 'vec)) |
| 1553 | (list expr)))) | 1481 | (list expr))))) |
| 1554 | ) | ||
| 1555 | 1482 | ||
| 1556 | ;;; Check if expr is a polynomial in var; if so, return its degree. | 1483 | ;;; Check if expr is a polynomial in var; if so, return its degree. |
| 1557 | (defun math-polynomial-p (expr var) | 1484 | (defun math-polynomial-p (expr var) |
| @@ -1577,14 +1504,12 @@ | |||
| 1577 | (let ((p1 (math-polynomial-p (nth 1 expr) var))) | 1504 | (let ((p1 (math-polynomial-p (nth 1 expr) var))) |
| 1578 | (and p1 (* p1 (nth 2 expr))))) | 1505 | (and p1 (* p1 (nth 2 expr))))) |
| 1579 | ((math-poly-depends expr var) nil) | 1506 | ((math-poly-depends expr var) nil) |
| 1580 | (t 0)) | 1507 | (t 0))) |
| 1581 | ) | ||
| 1582 | 1508 | ||
| 1583 | (defun math-poly-depends (expr var) | 1509 | (defun math-poly-depends (expr var) |
| 1584 | (if math-poly-base-variable | 1510 | (if math-poly-base-variable |
| 1585 | (math-expr-contains expr math-poly-base-variable) | 1511 | (math-expr-contains expr math-poly-base-variable) |
| 1586 | (math-expr-depends expr var)) | 1512 | (math-expr-depends expr var))) |
| 1587 | ) | ||
| 1588 | 1513 | ||
| 1589 | ;;; Find the variable (or sub-expression) which is the base of polynomial expr. | 1514 | ;;; Find the variable (or sub-expression) which is the base of polynomial expr. |
| 1590 | (defun math-polynomial-base (mpb-top-expr &optional mpb-pred) | 1515 | (defun math-polynomial-base (mpb-top-expr &optional mpb-pred) |
| @@ -1594,8 +1519,7 @@ | |||
| 1594 | (or (let ((const-ok nil)) | 1519 | (or (let ((const-ok nil)) |
| 1595 | (math-polynomial-base-rec mpb-top-expr)) | 1520 | (math-polynomial-base-rec mpb-top-expr)) |
| 1596 | (let ((const-ok t)) | 1521 | (let ((const-ok t)) |
| 1597 | (math-polynomial-base-rec mpb-top-expr))) | 1522 | (math-polynomial-base-rec mpb-top-expr)))) |
| 1598 | ) | ||
| 1599 | 1523 | ||
| 1600 | (defun math-polynomial-base-rec (mpb-expr) | 1524 | (defun math-polynomial-base-rec (mpb-expr) |
| 1601 | (and (not (Math-objvecp mpb-expr)) | 1525 | (and (not (Math-objvecp mpb-expr)) |
| @@ -1610,8 +1534,7 @@ | |||
| 1610 | (math-polynomial-base-rec '(var e var-e))) | 1534 | (math-polynomial-base-rec '(var e var-e))) |
| 1611 | (and (or const-ok (math-expr-contains-vars mpb-expr)) | 1535 | (and (or const-ok (math-expr-contains-vars mpb-expr)) |
| 1612 | (funcall mpb-pred mpb-expr) | 1536 | (funcall mpb-pred mpb-expr) |
| 1613 | mpb-expr))) | 1537 | mpb-expr)))) |
| 1614 | ) | ||
| 1615 | 1538 | ||
| 1616 | ;;; Return non-nil if expr refers to any variables. | 1539 | ;;; Return non-nil if expr refers to any variables. |
| 1617 | (defun math-expr-contains-vars (expr) | 1540 | (defun math-expr-contains-vars (expr) |
| @@ -1620,8 +1543,7 @@ | |||
| 1620 | (progn | 1543 | (progn |
| 1621 | (while (and (setq expr (cdr expr)) | 1544 | (while (and (setq expr (cdr expr)) |
| 1622 | (not (math-expr-contains-vars (car expr))))) | 1545 | (not (math-expr-contains-vars (car expr))))) |
| 1623 | expr))) | 1546 | expr)))) |
| 1624 | ) | ||
| 1625 | 1547 | ||
| 1626 | ;;; Simplify a polynomial in list form by stripping off high-end zeros. | 1548 | ;;; 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. | 1549 | ;;; This always leaves the constant part, i.e., nil->nil and nonnil->nonnil. |
| @@ -1633,8 +1555,7 @@ | |||
| 1633 | (Math-zerop (nth (1- (length pp)) pp))) | 1555 | (Math-zerop (nth (1- (length pp)) pp))) |
| 1634 | (setcdr (nthcdr (- (length pp) 2) pp) nil)) | 1556 | (setcdr (nthcdr (- (length pp) 2) pp) nil)) |
| 1635 | pp) | 1557 | pp) |
| 1636 | p)) | 1558 | p))) |
| 1637 | ) | ||
| 1638 | 1559 | ||
| 1639 | ;;; Compute ac*a + bc*b for polynomials in list form a, b and | 1560 | ;;; Compute ac*a + bc*b for polynomials in list form a, b and |
| 1640 | ;;; coefficients ac, bc. Result may be unsimplified. | 1561 | ;;; coefficients ac, bc. Result may be unsimplified. |
| @@ -1642,20 +1563,17 @@ | |||
| 1642 | (and (or a b) | 1563 | (and (or a b) |
| 1643 | (cons (math-add (math-mul (or (car a) 0) ac) | 1564 | (cons (math-add (math-mul (or (car a) 0) ac) |
| 1644 | (math-mul (or (car b) 0) bc)) | 1565 | (math-mul (or (car b) 0) bc)) |
| 1645 | (math-poly-mix (cdr a) ac (cdr b) bc))) | 1566 | (math-poly-mix (cdr a) ac (cdr b) bc)))) |
| 1646 | ) | ||
| 1647 | 1567 | ||
| 1648 | (defun math-poly-zerop (a) | 1568 | (defun math-poly-zerop (a) |
| 1649 | (or (null a) | 1569 | (or (null a) |
| 1650 | (and (null (cdr a)) (Math-zerop (car a)))) | 1570 | (and (null (cdr a)) (Math-zerop (car a))))) |
| 1651 | ) | ||
| 1652 | 1571 | ||
| 1653 | ;;; Multiply two polynomials in list form. | 1572 | ;;; Multiply two polynomials in list form. |
| 1654 | (defun math-poly-mul (a b) | 1573 | (defun math-poly-mul (a b) |
| 1655 | (and a b | 1574 | (and a b |
| 1656 | (math-poly-mix b (car a) | 1575 | (math-poly-mix b (car a) |
| 1657 | (math-poly-mul (cdr a) (cons 0 b)) 1)) | 1576 | (math-poly-mul (cdr a) (cons 0 b)) 1))) |
| 1658 | ) | ||
| 1659 | 1577 | ||
| 1660 | ;;; Build an expression from a polynomial list. | 1578 | ;;; Build an expression from a polynomial list. |
| 1661 | (defun math-build-polynomial-expr (p var) | 1579 | (defun math-build-polynomial-expr (p var) |
| @@ -1681,8 +1599,7 @@ | |||
| 1681 | (car rp)) | 1599 | (car rp)) |
| 1682 | (math-pow var n)))))) | 1600 | (math-pow var n)))))) |
| 1683 | accum)) | 1601 | accum)) |
| 1684 | 0) | 1602 | 0)) |
| 1685 | ) | ||
| 1686 | 1603 | ||
| 1687 | 1604 | ||
| 1688 | (defun math-to-simple-fraction (f) | 1605 | (defun math-to-simple-fraction (f) |
| @@ -1694,6 +1611,6 @@ | |||
| 1694 | (< (nth 1 f) 1000) | 1611 | (< (nth 1 f) 1000) |
| 1695 | (math-make-frac (nth 1 f) | 1612 | (math-make-frac (nth 1 f) |
| 1696 | (math-scale-int 1 (- (nth 2 f))))))) | 1613 | (math-scale-int 1 (- (nth 2 f))))))) |
| 1697 | f) | 1614 | f)) |
| 1698 | ) | ||
| 1699 | 1615 | ||
| 1616 | ;;; calc-alg.el ends here | ||