Mercurial > emacs
annotate lisp/calc/calcalg2.el @ 41091:418fff19f92e
Minor clarification.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Thu, 15 Nov 2001 18:53:38 +0000 |
parents | 73f364fd8aaa |
children | fcd507927105 |
rev | line source |
---|---|
40785 | 1 ;; Calculator for GNU Emacs, part II [calc-alg-2.el] |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
40785 | 3 ;; Written by Dave Gillespie, daveg@synaptics.com. |
4 | |
5 ;; This file is part of GNU Emacs. | |
6 | |
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. | |
13 | |
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. | |
21 | |
22 | |
23 | |
24 ;; This file is autoloaded from calc-ext.el. | |
25 (require 'calc-ext) | |
26 | |
27 (require 'calc-macs) | |
28 | |
29 (defun calc-Need-calc-alg-2 () nil) | |
30 | |
31 | |
32 (defun calc-derivative (var num) | |
33 (interactive "sDifferentiate with respect to: \np") | |
34 (calc-slow-wrapper | |
35 (and (< num 0) (error "Order of derivative must be positive")) | |
36 (let ((func (if (calc-is-hyperbolic) 'calcFunc-tderiv 'calcFunc-deriv)) | |
37 n expr) | |
38 (if (or (equal var "") (equal var "$")) | |
39 (setq n 2 | |
40 expr (calc-top-n 2) | |
41 var (calc-top-n 1)) | |
42 (setq var (math-read-expr var)) | |
43 (if (eq (car-safe var) 'error) | |
44 (error "Bad format in expression: %s" (nth 1 var))) | |
45 (setq n 1 | |
46 expr (calc-top-n 1))) | |
47 (while (>= (setq num (1- num)) 0) | |
48 (setq expr (list func expr var))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
49 (calc-enter-result n "derv" expr)))) |
40785 | 50 |
51 (defun calc-integral (var) | |
52 (interactive "sIntegration variable: ") | |
53 (calc-slow-wrapper | |
54 (if (or (equal var "") (equal var "$")) | |
55 (calc-enter-result 2 "intg" (list 'calcFunc-integ | |
56 (calc-top-n 2) | |
57 (calc-top-n 1))) | |
58 (let ((var (math-read-expr var))) | |
59 (if (eq (car-safe var) 'error) | |
60 (error "Bad format in expression: %s" (nth 1 var))) | |
61 (calc-enter-result 1 "intg" (list 'calcFunc-integ | |
62 (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
63 var)))))) |
40785 | 64 |
65 (defun calc-num-integral (&optional varname lowname highname) | |
66 (interactive "sIntegration variable: ") | |
67 (calc-tabular-command 'calcFunc-ninteg "Integration" "nint" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
68 nil varname lowname highname)) |
40785 | 69 |
70 (defun calc-summation (arg &optional varname lowname highname) | |
71 (interactive "P\nsSummation variable: ") | |
72 (calc-tabular-command 'calcFunc-sum "Summation" "sum" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
73 arg varname lowname highname)) |
40785 | 74 |
75 (defun calc-alt-summation (arg &optional varname lowname highname) | |
76 (interactive "P\nsSummation variable: ") | |
77 (calc-tabular-command 'calcFunc-asum "Summation" "asum" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
78 arg varname lowname highname)) |
40785 | 79 |
80 (defun calc-product (arg &optional varname lowname highname) | |
81 (interactive "P\nsIndex variable: ") | |
82 (calc-tabular-command 'calcFunc-prod "Index" "prod" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
83 arg varname lowname highname)) |
40785 | 84 |
85 (defun calc-tabulate (arg &optional varname lowname highname) | |
86 (interactive "P\nsIndex variable: ") | |
87 (calc-tabular-command 'calcFunc-table "Index" "tabl" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
88 arg varname lowname highname)) |
40785 | 89 |
90 (defun calc-tabular-command (func prompt prefix arg varname lowname highname) | |
91 (calc-slow-wrapper | |
92 (let (var (low nil) (high nil) (step nil) stepname stepnum (num 1) expr) | |
93 (if (consp arg) | |
94 (setq stepnum 1) | |
95 (setq stepnum 0)) | |
96 (if (or (equal varname "") (equal varname "$") (null varname)) | |
97 (setq high (calc-top-n (+ stepnum 1)) | |
98 low (calc-top-n (+ stepnum 2)) | |
99 var (calc-top-n (+ stepnum 3)) | |
100 num (+ stepnum 4)) | |
101 (setq var (if (stringp varname) (math-read-expr varname) varname)) | |
102 (if (eq (car-safe var) 'error) | |
103 (error "Bad format in expression: %s" (nth 1 var))) | |
104 (or lowname | |
105 (setq lowname (read-string (concat prompt " variable: " varname | |
106 ", from: ")))) | |
107 (if (or (equal lowname "") (equal lowname "$")) | |
108 (setq high (calc-top-n (+ stepnum 1)) | |
109 low (calc-top-n (+ stepnum 2)) | |
110 num (+ stepnum 3)) | |
111 (setq low (if (stringp lowname) (math-read-expr lowname) lowname)) | |
112 (if (eq (car-safe low) 'error) | |
113 (error "Bad format in expression: %s" (nth 1 low))) | |
114 (or highname | |
115 (setq highname (read-string (concat prompt " variable: " varname | |
116 ", from: " lowname | |
117 ", to: ")))) | |
118 (if (or (equal highname "") (equal highname "$")) | |
119 (setq high (calc-top-n (+ stepnum 1)) | |
120 num (+ stepnum 2)) | |
121 (setq high (if (stringp highname) (math-read-expr highname) | |
122 highname)) | |
123 (if (eq (car-safe high) 'error) | |
124 (error "Bad format in expression: %s" (nth 1 high))) | |
125 (if (consp arg) | |
126 (progn | |
127 (setq stepname (read-string (concat prompt " variable: " | |
128 varname | |
129 ", from: " lowname | |
130 ", to: " highname | |
131 ", step: "))) | |
132 (if (or (equal stepname "") (equal stepname "$")) | |
133 (setq step (calc-top-n 1) | |
134 num 2) | |
135 (setq step (math-read-expr stepname)) | |
136 (if (eq (car-safe step) 'error) | |
137 (error "Bad format in expression: %s" | |
138 (nth 1 step))))))))) | |
139 (or step | |
140 (if (consp arg) | |
141 (setq step (calc-top-n 1)) | |
142 (if arg | |
143 (setq step (prefix-numeric-value arg))))) | |
144 (setq expr (calc-top-n num)) | |
145 (calc-enter-result num prefix (append (list func expr var low high) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
146 (and step (list step))))))) |
40785 | 147 |
148 (defun calc-solve-for (var) | |
149 (interactive "sVariable to solve for: ") | |
150 (calc-slow-wrapper | |
151 (let ((func (if (calc-is-inverse) | |
152 (if (calc-is-hyperbolic) 'calcFunc-ffinv 'calcFunc-finv) | |
153 (if (calc-is-hyperbolic) 'calcFunc-fsolve 'calcFunc-solve)))) | |
154 (if (or (equal var "") (equal var "$")) | |
155 (calc-enter-result 2 "solv" (list func | |
156 (calc-top-n 2) | |
157 (calc-top-n 1))) | |
158 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var) | |
159 (not (string-match "\\[" var))) | |
160 (math-read-expr (concat "[" var "]")) | |
161 (math-read-expr var)))) | |
162 (if (eq (car-safe var) 'error) | |
163 (error "Bad format in expression: %s" (nth 1 var))) | |
164 (calc-enter-result 1 "solv" (list func | |
165 (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
166 var))))))) |
40785 | 167 |
168 (defun calc-poly-roots (var) | |
169 (interactive "sVariable to solve for: ") | |
170 (calc-slow-wrapper | |
171 (if (or (equal var "") (equal var "$")) | |
172 (calc-enter-result 2 "prts" (list 'calcFunc-roots | |
173 (calc-top-n 2) | |
174 (calc-top-n 1))) | |
175 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var) | |
176 (not (string-match "\\[" var))) | |
177 (math-read-expr (concat "[" var "]")) | |
178 (math-read-expr var)))) | |
179 (if (eq (car-safe var) 'error) | |
180 (error "Bad format in expression: %s" (nth 1 var))) | |
181 (calc-enter-result 1 "prts" (list 'calcFunc-roots | |
182 (calc-top-n 1) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
183 var)))))) |
40785 | 184 |
185 (defun calc-taylor (var nterms) | |
186 (interactive "sTaylor expansion variable: \nNNumber of terms: ") | |
187 (calc-slow-wrapper | |
188 (let ((var (math-read-expr var))) | |
189 (if (eq (car-safe var) 'error) | |
190 (error "Bad format in expression: %s" (nth 1 var))) | |
191 (calc-enter-result 1 "tylr" (list 'calcFunc-taylor | |
192 (calc-top-n 1) | |
193 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
194 (prefix-numeric-value nterms)))))) |
40785 | 195 |
196 | |
197 (defun math-derivative (expr) ; uses global values: deriv-var, deriv-total. | |
198 (cond ((equal expr deriv-var) | |
199 1) | |
200 ((or (Math-scalarp expr) | |
201 (eq (car expr) 'sdev) | |
202 (and (eq (car expr) 'var) | |
203 (or (not deriv-total) | |
204 (math-const-var expr) | |
205 (progn | |
206 (math-setup-declarations) | |
207 (memq 'const (nth 1 (or (assq (nth 2 expr) | |
208 math-decls-cache) | |
209 math-decls-all))))))) | |
210 0) | |
211 ((eq (car expr) '+) | |
212 (math-add (math-derivative (nth 1 expr)) | |
213 (math-derivative (nth 2 expr)))) | |
214 ((eq (car expr) '-) | |
215 (math-sub (math-derivative (nth 1 expr)) | |
216 (math-derivative (nth 2 expr)))) | |
217 ((memq (car expr) '(calcFunc-eq calcFunc-neq calcFunc-lt | |
218 calcFunc-gt calcFunc-leq calcFunc-geq)) | |
219 (list (car expr) | |
220 (math-derivative (nth 1 expr)) | |
221 (math-derivative (nth 2 expr)))) | |
222 ((eq (car expr) 'neg) | |
223 (math-neg (math-derivative (nth 1 expr)))) | |
224 ((eq (car expr) '*) | |
225 (math-add (math-mul (nth 2 expr) | |
226 (math-derivative (nth 1 expr))) | |
227 (math-mul (nth 1 expr) | |
228 (math-derivative (nth 2 expr))))) | |
229 ((eq (car expr) '/) | |
230 (math-sub (math-div (math-derivative (nth 1 expr)) | |
231 (nth 2 expr)) | |
232 (math-div (math-mul (nth 1 expr) | |
233 (math-derivative (nth 2 expr))) | |
234 (math-sqr (nth 2 expr))))) | |
235 ((eq (car expr) '^) | |
236 (let ((du (math-derivative (nth 1 expr))) | |
237 (dv (math-derivative (nth 2 expr)))) | |
238 (or (Math-zerop du) | |
239 (setq du (math-mul (nth 2 expr) | |
240 (math-mul (math-normalize | |
241 (list '^ | |
242 (nth 1 expr) | |
243 (math-add (nth 2 expr) -1))) | |
244 du)))) | |
245 (or (Math-zerop dv) | |
246 (setq dv (math-mul (math-normalize | |
247 (list 'calcFunc-ln (nth 1 expr))) | |
248 (math-mul expr dv)))) | |
249 (math-add du dv))) | |
250 ((eq (car expr) '%) | |
251 (math-derivative (nth 1 expr))) ; a reasonable definition | |
252 ((eq (car expr) 'vec) | |
253 (math-map-vec 'math-derivative expr)) | |
254 ((and (memq (car expr) '(calcFunc-conj calcFunc-re calcFunc-im)) | |
255 (= (length expr) 2)) | |
256 (list (car expr) (math-derivative (nth 1 expr)))) | |
257 ((and (memq (car expr) '(calcFunc-subscr calcFunc-mrow calcFunc-mcol)) | |
258 (= (length expr) 3)) | |
259 (let ((d (math-derivative (nth 1 expr)))) | |
260 (if (math-numberp d) | |
261 0 ; assume x and x_1 are independent vars | |
262 (list (car expr) d (nth 2 expr))))) | |
263 (t (or (and (symbolp (car expr)) | |
264 (if (= (length expr) 2) | |
265 (let ((handler (get (car expr) 'math-derivative))) | |
266 (and handler | |
267 (let ((deriv (math-derivative (nth 1 expr)))) | |
268 (if (Math-zerop deriv) | |
269 deriv | |
270 (math-mul (funcall handler (nth 1 expr)) | |
271 deriv))))) | |
272 (let ((handler (get (car expr) 'math-derivative-n))) | |
273 (and handler | |
274 (funcall handler expr))))) | |
275 (and (not (eq deriv-symb 'pre-expand)) | |
276 (let ((exp (math-expand-formula expr))) | |
277 (and exp | |
278 (or (let ((deriv-symb 'pre-expand)) | |
279 (catch 'math-deriv (math-derivative expr))) | |
280 (math-derivative exp))))) | |
281 (if (or (Math-objvecp expr) | |
282 (eq (car expr) 'var) | |
283 (not (symbolp (car expr)))) | |
284 (if deriv-symb | |
285 (throw 'math-deriv nil) | |
286 (list (if deriv-total 'calcFunc-tderiv 'calcFunc-deriv) | |
287 expr | |
288 deriv-var)) | |
289 (let ((accum 0) | |
290 (arg expr) | |
291 (n 1) | |
292 derv) | |
293 (while (setq arg (cdr arg)) | |
294 (or (Math-zerop (setq derv (math-derivative (car arg)))) | |
295 (let ((func (intern (concat (symbol-name (car expr)) | |
296 "'" | |
297 (if (> n 1) | |
298 (int-to-string n) | |
299 "")))) | |
300 (prop (cond ((= (length expr) 2) | |
301 'math-derivative-1) | |
302 ((= (length expr) 3) | |
303 'math-derivative-2) | |
304 ((= (length expr) 4) | |
305 'math-derivative-3) | |
306 ((= (length expr) 5) | |
307 'math-derivative-4) | |
308 ((= (length expr) 6) | |
309 'math-derivative-5)))) | |
310 (setq accum | |
311 (math-add | |
312 accum | |
313 (math-mul | |
314 derv | |
315 (let ((handler (get func prop))) | |
316 (or (and prop handler | |
317 (apply handler (cdr expr))) | |
318 (if (and deriv-symb | |
319 (not (get func | |
320 'calc-user-defn))) | |
321 (throw 'math-deriv nil) | |
322 (cons func (cdr expr)))))))))) | |
323 (setq n (1+ n))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
324 accum)))))) |
40785 | 325 |
326 (defun calcFunc-deriv (expr deriv-var &optional deriv-value deriv-symb) | |
327 (let* ((deriv-total nil) | |
328 (res (catch 'math-deriv (math-derivative expr)))) | |
329 (or (eq (car-safe res) 'calcFunc-deriv) | |
330 (null res) | |
331 (setq res (math-normalize res))) | |
332 (and res | |
333 (if deriv-value | |
334 (math-expr-subst res deriv-var deriv-value) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
335 res)))) |
40785 | 336 |
337 (defun calcFunc-tderiv (expr deriv-var &optional deriv-value deriv-symb) | |
338 (math-setup-declarations) | |
339 (let* ((deriv-total t) | |
340 (res (catch 'math-deriv (math-derivative expr)))) | |
341 (or (eq (car-safe res) 'calcFunc-tderiv) | |
342 (null res) | |
343 (setq res (math-normalize res))) | |
344 (and res | |
345 (if deriv-value | |
346 (math-expr-subst res deriv-var deriv-value) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
347 res)))) |
40785 | 348 |
349 (put 'calcFunc-inv\' 'math-derivative-1 | |
350 (function (lambda (u) (math-neg (math-div 1 (math-sqr u)))))) | |
351 | |
352 (put 'calcFunc-sqrt\' 'math-derivative-1 | |
353 (function (lambda (u) (math-div 1 (math-mul 2 (list 'calcFunc-sqrt u)))))) | |
354 | |
355 (put 'calcFunc-deg\' 'math-derivative-1 | |
356 (function (lambda (u) (math-div-float '(float 18 1) (math-pi))))) | |
357 | |
358 (put 'calcFunc-rad\' 'math-derivative-1 | |
359 (function (lambda (u) (math-pi-over-180)))) | |
360 | |
361 (put 'calcFunc-ln\' 'math-derivative-1 | |
362 (function (lambda (u) (math-div 1 u)))) | |
363 | |
364 (put 'calcFunc-log10\' 'math-derivative-1 | |
365 (function (lambda (u) | |
366 (math-div (math-div 1 (math-normalize '(calcFunc-ln 10))) | |
367 u)))) | |
368 | |
369 (put 'calcFunc-lnp1\' 'math-derivative-1 | |
370 (function (lambda (u) (math-div 1 (math-add u 1))))) | |
371 | |
372 (put 'calcFunc-log\' 'math-derivative-2 | |
373 (function (lambda (x b) | |
374 (and (not (Math-zerop b)) | |
375 (let ((lnv (math-normalize | |
376 (list 'calcFunc-ln b)))) | |
377 (math-div 1 (math-mul lnv x))))))) | |
378 | |
379 (put 'calcFunc-log\'2 'math-derivative-2 | |
380 (function (lambda (x b) | |
381 (let ((lnv (list 'calcFunc-ln b))) | |
382 (math-neg (math-div (list 'calcFunc-log x b) | |
383 (math-mul lnv b))))))) | |
384 | |
385 (put 'calcFunc-exp\' 'math-derivative-1 | |
386 (function (lambda (u) (math-normalize (list 'calcFunc-exp u))))) | |
387 | |
388 (put 'calcFunc-expm1\' 'math-derivative-1 | |
389 (function (lambda (u) (math-normalize (list 'calcFunc-expm1 u))))) | |
390 | |
391 (put 'calcFunc-sin\' 'math-derivative-1 | |
392 (function (lambda (u) (math-to-radians-2 (math-normalize | |
393 (list 'calcFunc-cos u)))))) | |
394 | |
395 (put 'calcFunc-cos\' 'math-derivative-1 | |
396 (function (lambda (u) (math-neg (math-to-radians-2 | |
397 (math-normalize | |
398 (list 'calcFunc-sin u))))))) | |
399 | |
400 (put 'calcFunc-tan\' 'math-derivative-1 | |
401 (function (lambda (u) (math-to-radians-2 | |
402 (math-div 1 (math-sqr | |
403 (math-normalize | |
404 (list 'calcFunc-cos u)))))))) | |
405 | |
406 (put 'calcFunc-arcsin\' 'math-derivative-1 | |
407 (function (lambda (u) | |
408 (math-from-radians-2 | |
409 (math-div 1 (math-normalize | |
410 (list 'calcFunc-sqrt | |
411 (math-sub 1 (math-sqr u))))))))) | |
412 | |
413 (put 'calcFunc-arccos\' 'math-derivative-1 | |
414 (function (lambda (u) | |
415 (math-from-radians-2 | |
416 (math-div -1 (math-normalize | |
417 (list 'calcFunc-sqrt | |
418 (math-sub 1 (math-sqr u))))))))) | |
419 | |
420 (put 'calcFunc-arctan\' 'math-derivative-1 | |
421 (function (lambda (u) (math-from-radians-2 | |
422 (math-div 1 (math-add 1 (math-sqr u))))))) | |
423 | |
424 (put 'calcFunc-sinh\' 'math-derivative-1 | |
425 (function (lambda (u) (math-normalize (list 'calcFunc-cosh u))))) | |
426 | |
427 (put 'calcFunc-cosh\' 'math-derivative-1 | |
428 (function (lambda (u) (math-normalize (list 'calcFunc-sinh u))))) | |
429 | |
430 (put 'calcFunc-tanh\' 'math-derivative-1 | |
431 (function (lambda (u) (math-div 1 (math-sqr | |
432 (math-normalize | |
433 (list 'calcFunc-cosh u))))))) | |
434 | |
435 (put 'calcFunc-arcsinh\' 'math-derivative-1 | |
436 (function (lambda (u) | |
437 (math-div 1 (math-normalize | |
438 (list 'calcFunc-sqrt | |
439 (math-add (math-sqr u) 1))))))) | |
440 | |
441 (put 'calcFunc-arccosh\' 'math-derivative-1 | |
442 (function (lambda (u) | |
443 (math-div 1 (math-normalize | |
444 (list 'calcFunc-sqrt | |
445 (math-add (math-sqr u) -1))))))) | |
446 | |
447 (put 'calcFunc-arctanh\' 'math-derivative-1 | |
448 (function (lambda (u) (math-div 1 (math-sub 1 (math-sqr u)))))) | |
449 | |
450 (put 'calcFunc-bern\'2 'math-derivative-2 | |
451 (function (lambda (n x) | |
452 (math-mul n (list 'calcFunc-bern (math-add n -1) x))))) | |
453 | |
454 (put 'calcFunc-euler\'2 'math-derivative-2 | |
455 (function (lambda (n x) | |
456 (math-mul n (list 'calcFunc-euler (math-add n -1) x))))) | |
457 | |
458 (put 'calcFunc-gammag\'2 'math-derivative-2 | |
459 (function (lambda (a x) (math-deriv-gamma a x 1)))) | |
460 | |
461 (put 'calcFunc-gammaG\'2 'math-derivative-2 | |
462 (function (lambda (a x) (math-deriv-gamma a x -1)))) | |
463 | |
464 (put 'calcFunc-gammaP\'2 'math-derivative-2 | |
465 (function (lambda (a x) (math-deriv-gamma a x | |
466 (math-div | |
467 1 (math-normalize | |
468 (list 'calcFunc-gamma | |
469 a))))))) | |
470 | |
471 (put 'calcFunc-gammaQ\'2 'math-derivative-2 | |
472 (function (lambda (a x) (math-deriv-gamma a x | |
473 (math-div | |
474 -1 (math-normalize | |
475 (list 'calcFunc-gamma | |
476 a))))))) | |
477 | |
478 (defun math-deriv-gamma (a x scale) | |
479 (math-mul scale | |
480 (math-mul (math-pow x (math-add a -1)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
481 (list 'calcFunc-exp (math-neg x))))) |
40785 | 482 |
483 (put 'calcFunc-betaB\' 'math-derivative-3 | |
484 (function (lambda (x a b) (math-deriv-beta x a b 1)))) | |
485 | |
486 (put 'calcFunc-betaI\' 'math-derivative-3 | |
487 (function (lambda (x a b) (math-deriv-beta x a b | |
488 (math-div | |
489 1 (list 'calcFunc-beta | |
490 a b)))))) | |
491 | |
492 (defun math-deriv-beta (x a b scale) | |
493 (math-mul (math-mul (math-pow x (math-add a -1)) | |
494 (math-pow (math-sub 1 x) (math-add b -1))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
495 scale)) |
40785 | 496 |
497 (put 'calcFunc-erf\' 'math-derivative-1 | |
498 (function (lambda (x) (math-div 2 | |
499 (math-mul (list 'calcFunc-exp | |
500 (math-sqr x)) | |
501 (if calc-symbolic-mode | |
502 '(calcFunc-sqrt | |
503 (var pi var-pi)) | |
504 (math-sqrt-pi))))))) | |
505 | |
506 (put 'calcFunc-erfc\' 'math-derivative-1 | |
507 (function (lambda (x) (math-div -2 | |
508 (math-mul (list 'calcFunc-exp | |
509 (math-sqr x)) | |
510 (if calc-symbolic-mode | |
511 '(calcFunc-sqrt | |
512 (var pi var-pi)) | |
513 (math-sqrt-pi))))))) | |
514 | |
515 (put 'calcFunc-besJ\'2 'math-derivative-2 | |
516 (function (lambda (v z) (math-div (math-sub (list 'calcFunc-besJ | |
517 (math-add v -1) | |
518 z) | |
519 (list 'calcFunc-besJ | |
520 (math-add v 1) | |
521 z)) | |
522 2)))) | |
523 | |
524 (put 'calcFunc-besY\'2 'math-derivative-2 | |
525 (function (lambda (v z) (math-div (math-sub (list 'calcFunc-besY | |
526 (math-add v -1) | |
527 z) | |
528 (list 'calcFunc-besY | |
529 (math-add v 1) | |
530 z)) | |
531 2)))) | |
532 | |
533 (put 'calcFunc-sum 'math-derivative-n | |
534 (function | |
535 (lambda (expr) | |
536 (if (math-expr-contains (cons 'vec (cdr (cdr expr))) deriv-var) | |
537 (throw 'math-deriv nil) | |
538 (cons 'calcFunc-sum | |
539 (cons (math-derivative (nth 1 expr)) | |
540 (cdr (cdr expr)))))))) | |
541 | |
542 (put 'calcFunc-prod 'math-derivative-n | |
543 (function | |
544 (lambda (expr) | |
545 (if (math-expr-contains (cons 'vec (cdr (cdr expr))) deriv-var) | |
546 (throw 'math-deriv nil) | |
547 (math-mul expr | |
548 (cons 'calcFunc-sum | |
549 (cons (math-div (math-derivative (nth 1 expr)) | |
550 (nth 1 expr)) | |
551 (cdr (cdr expr))))))))) | |
552 | |
553 (put 'calcFunc-integ 'math-derivative-n | |
554 (function | |
555 (lambda (expr) | |
556 (if (= (length expr) 3) | |
557 (if (equal (nth 2 expr) deriv-var) | |
558 (nth 1 expr) | |
559 (math-normalize | |
560 (list 'calcFunc-integ | |
561 (math-derivative (nth 1 expr)) | |
562 (nth 2 expr)))) | |
563 (if (= (length expr) 5) | |
564 (let ((lower (math-expr-subst (nth 1 expr) (nth 2 expr) | |
565 (nth 3 expr))) | |
566 (upper (math-expr-subst (nth 1 expr) (nth 2 expr) | |
567 (nth 4 expr)))) | |
568 (math-add (math-sub (math-mul upper | |
569 (math-derivative (nth 4 expr))) | |
570 (math-mul lower | |
571 (math-derivative (nth 3 expr)))) | |
572 (if (equal (nth 2 expr) deriv-var) | |
573 0 | |
574 (math-normalize | |
575 (list 'calcFunc-integ | |
576 (math-derivative (nth 1 expr)) (nth 2 expr) | |
577 (nth 3 expr) (nth 4 expr))))))))))) | |
578 | |
579 (put 'calcFunc-if 'math-derivative-n | |
580 (function | |
581 (lambda (expr) | |
582 (and (= (length expr) 4) | |
583 (list 'calcFunc-if (nth 1 expr) | |
584 (math-derivative (nth 2 expr)) | |
585 (math-derivative (nth 3 expr))))))) | |
586 | |
587 (put 'calcFunc-subscr 'math-derivative-n | |
588 (function | |
589 (lambda (expr) | |
590 (and (= (length expr) 3) | |
591 (list 'calcFunc-subscr (nth 1 expr) | |
592 (math-derivative (nth 2 expr))))))) | |
593 | |
594 | |
595 | |
596 | |
597 | |
598 (setq math-integ-var '(var X ---)) | |
599 (setq math-integ-var-2 '(var Y ---)) | |
600 (setq math-integ-vars (list 'f math-integ-var math-integ-var-2)) | |
601 (setq math-integ-var-list (list math-integ-var)) | |
602 (setq math-integ-var-list-list (list math-integ-var-list)) | |
603 | |
604 (defmacro math-tracing-integral (&rest parts) | |
605 (list 'and | |
606 'trace-buffer | |
607 (list 'save-excursion | |
608 '(set-buffer trace-buffer) | |
609 '(goto-char (point-max)) | |
610 (list 'and | |
611 '(bolp) | |
612 '(insert (make-string (- math-integral-limit | |
613 math-integ-level) 32) | |
614 (format "%2d " math-integ-depth) | |
615 (make-string math-integ-level 32))) | |
616 ;;(list 'condition-case 'err | |
617 (cons 'insert parts) | |
618 ;; '(error (insert (prin1-to-string err)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
619 '(sit-for 0)))) |
40785 | 620 |
621 ;;; The following wrapper caches results and avoids infinite recursion. | |
622 ;;; Each cache entry is: ( A B ) Integral of A is B; | |
623 ;;; ( A N ) Integral of A failed at level N; | |
624 ;;; ( A busy ) Currently working on integral of A; | |
625 ;;; ( A parts ) Currently working, integ-by-parts; | |
626 ;;; ( A parts2 ) Currently working, integ-by-parts; | |
627 ;;; ( A cancelled ) Ignore this cache entry; | |
628 ;;; ( A [B] ) Same result as for cur-record = B. | |
629 (defun math-integral (expr &optional simplify same-as-above) | |
630 (let* ((simp cur-record) | |
631 (cur-record (assoc expr math-integral-cache)) | |
632 (math-integ-depth (1+ math-integ-depth)) | |
633 (val 'cancelled)) | |
634 (math-tracing-integral "Integrating " | |
635 (math-format-value expr 1000) | |
636 "...\n") | |
637 (and cur-record | |
638 (progn | |
639 (math-tracing-integral "Found " | |
640 (math-format-value (nth 1 cur-record) 1000)) | |
641 (and (consp (nth 1 cur-record)) | |
642 (math-replace-integral-parts cur-record)) | |
643 (math-tracing-integral " => " | |
644 (math-format-value (nth 1 cur-record) 1000) | |
645 "\n"))) | |
646 (or (and cur-record | |
647 (not (eq (nth 1 cur-record) 'cancelled)) | |
648 (or (not (integerp (nth 1 cur-record))) | |
649 (>= (nth 1 cur-record) math-integ-level))) | |
650 (and (math-integral-contains-parts expr) | |
651 (progn | |
652 (setq val nil) | |
653 t)) | |
654 (unwind-protect | |
655 (progn | |
656 (let (math-integ-msg) | |
657 (if (eq calc-display-working-message 'lots) | |
658 (progn | |
659 (calc-set-command-flag 'clear-message) | |
660 (setq math-integ-msg (format | |
661 "Working... Integrating %s" | |
662 (math-format-flat-expr expr 0))) | |
663 (message math-integ-msg))) | |
664 (if cur-record | |
665 (setcar (cdr cur-record) | |
666 (if same-as-above (vector simp) 'busy)) | |
667 (setq cur-record | |
668 (list expr (if same-as-above (vector simp) 'busy)) | |
669 math-integral-cache (cons cur-record | |
670 math-integral-cache))) | |
671 (if (eq simplify 'yes) | |
672 (progn | |
673 (math-tracing-integral "Simplifying...") | |
674 (setq simp (math-simplify expr)) | |
675 (setq val (if (equal simp expr) | |
676 (progn | |
677 (math-tracing-integral " no change\n") | |
678 (math-do-integral expr)) | |
679 (math-tracing-integral " simplified\n") | |
680 (math-integral simp 'no t)))) | |
681 (or (setq val (math-do-integral expr)) | |
682 (eq simplify 'no) | |
683 (let ((simp (math-simplify expr))) | |
684 (or (equal simp expr) | |
685 (progn | |
686 (math-tracing-integral "Trying again after " | |
687 "simplification...\n") | |
688 (setq val (math-integral simp 'no t)))))))) | |
689 (if (eq calc-display-working-message 'lots) | |
690 (message math-integ-msg))) | |
691 (setcar (cdr cur-record) (or val | |
692 (if (or math-enable-subst | |
693 (not math-any-substs)) | |
694 math-integ-level | |
695 'cancelled))))) | |
696 (setq val cur-record) | |
697 (while (vectorp (nth 1 val)) | |
698 (setq val (aref (nth 1 val) 0))) | |
699 (setq val (if (memq (nth 1 val) '(parts parts2)) | |
700 (progn | |
701 (setcar (cdr val) 'parts2) | |
702 (list 'var 'PARTS val)) | |
703 (and (consp (nth 1 val)) | |
704 (nth 1 val)))) | |
705 (math-tracing-integral "Integral of " | |
706 (math-format-value expr 1000) | |
707 " is " | |
708 (math-format-value val 1000) | |
709 "\n") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
710 val)) |
40785 | 711 (defvar math-integral-cache nil) |
712 (defvar math-integral-cache-state nil) | |
713 | |
714 (defun math-integral-contains-parts (expr) | |
715 (if (Math-primp expr) | |
716 (and (eq (car-safe expr) 'var) | |
717 (eq (nth 1 expr) 'PARTS) | |
718 (listp (nth 2 expr))) | |
719 (while (and (setq expr (cdr expr)) | |
720 (not (math-integral-contains-parts (car expr))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
721 expr)) |
40785 | 722 |
723 (defun math-replace-integral-parts (expr) | |
724 (or (Math-primp expr) | |
725 (while (setq expr (cdr expr)) | |
726 (and (consp (car expr)) | |
727 (if (eq (car (car expr)) 'var) | |
728 (and (eq (nth 1 (car expr)) 'PARTS) | |
729 (consp (nth 2 (car expr))) | |
730 (if (listp (nth 1 (nth 2 (car expr)))) | |
731 (progn | |
732 (setcar expr (nth 1 (nth 2 (car expr)))) | |
733 (math-replace-integral-parts (cons 'foo expr))) | |
734 (setcar (cdr cur-record) 'cancelled))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
735 (math-replace-integral-parts (car expr))))))) |
40785 | 736 |
737 (defun math-do-integral (expr) | |
738 (let (t1 t2) | |
739 (or (cond ((not (math-expr-contains expr math-integ-var)) | |
740 (math-mul expr math-integ-var)) | |
741 ((equal expr math-integ-var) | |
742 (math-div (math-sqr expr) 2)) | |
743 ((eq (car expr) '+) | |
744 (and (setq t1 (math-integral (nth 1 expr))) | |
745 (setq t2 (math-integral (nth 2 expr))) | |
746 (math-add t1 t2))) | |
747 ((eq (car expr) '-) | |
748 (and (setq t1 (math-integral (nth 1 expr))) | |
749 (setq t2 (math-integral (nth 2 expr))) | |
750 (math-sub t1 t2))) | |
751 ((eq (car expr) 'neg) | |
752 (and (setq t1 (math-integral (nth 1 expr))) | |
753 (math-neg t1))) | |
754 ((eq (car expr) '*) | |
755 (cond ((not (math-expr-contains (nth 1 expr) math-integ-var)) | |
756 (and (setq t1 (math-integral (nth 2 expr))) | |
757 (math-mul (nth 1 expr) t1))) | |
758 ((not (math-expr-contains (nth 2 expr) math-integ-var)) | |
759 (and (setq t1 (math-integral (nth 1 expr))) | |
760 (math-mul t1 (nth 2 expr)))) | |
761 ((memq (car-safe (nth 1 expr)) '(+ -)) | |
762 (math-integral (list (car (nth 1 expr)) | |
763 (math-mul (nth 1 (nth 1 expr)) | |
764 (nth 2 expr)) | |
765 (math-mul (nth 2 (nth 1 expr)) | |
766 (nth 2 expr))) | |
767 'yes t)) | |
768 ((memq (car-safe (nth 2 expr)) '(+ -)) | |
769 (math-integral (list (car (nth 2 expr)) | |
770 (math-mul (nth 1 (nth 2 expr)) | |
771 (nth 1 expr)) | |
772 (math-mul (nth 2 (nth 2 expr)) | |
773 (nth 1 expr))) | |
774 'yes t)))) | |
775 ((eq (car expr) '/) | |
776 (cond ((and (not (math-expr-contains (nth 1 expr) | |
777 math-integ-var)) | |
778 (not (math-equal-int (nth 1 expr) 1))) | |
779 (and (setq t1 (math-integral (math-div 1 (nth 2 expr)))) | |
780 (math-mul (nth 1 expr) t1))) | |
781 ((not (math-expr-contains (nth 2 expr) math-integ-var)) | |
782 (and (setq t1 (math-integral (nth 1 expr))) | |
783 (math-div t1 (nth 2 expr)))) | |
784 ((and (eq (car-safe (nth 1 expr)) '*) | |
785 (not (math-expr-contains (nth 1 (nth 1 expr)) | |
786 math-integ-var))) | |
787 (and (setq t1 (math-integral | |
788 (math-div (nth 2 (nth 1 expr)) | |
789 (nth 2 expr)))) | |
790 (math-mul t1 (nth 1 (nth 1 expr))))) | |
791 ((and (eq (car-safe (nth 1 expr)) '*) | |
792 (not (math-expr-contains (nth 2 (nth 1 expr)) | |
793 math-integ-var))) | |
794 (and (setq t1 (math-integral | |
795 (math-div (nth 1 (nth 1 expr)) | |
796 (nth 2 expr)))) | |
797 (math-mul t1 (nth 2 (nth 1 expr))))) | |
798 ((and (eq (car-safe (nth 2 expr)) '*) | |
799 (not (math-expr-contains (nth 1 (nth 2 expr)) | |
800 math-integ-var))) | |
801 (and (setq t1 (math-integral | |
802 (math-div (nth 1 expr) | |
803 (nth 2 (nth 2 expr))))) | |
804 (math-div t1 (nth 1 (nth 2 expr))))) | |
805 ((and (eq (car-safe (nth 2 expr)) '*) | |
806 (not (math-expr-contains (nth 2 (nth 2 expr)) | |
807 math-integ-var))) | |
808 (and (setq t1 (math-integral | |
809 (math-div (nth 1 expr) | |
810 (nth 1 (nth 2 expr))))) | |
811 (math-div t1 (nth 2 (nth 2 expr))))) | |
812 ((eq (car-safe (nth 2 expr)) 'calcFunc-exp) | |
813 (math-integral | |
814 (math-mul (nth 1 expr) | |
815 (list 'calcFunc-exp | |
816 (math-neg (nth 1 (nth 2 expr))))))))) | |
817 ((eq (car expr) '^) | |
818 (cond ((not (math-expr-contains (nth 1 expr) math-integ-var)) | |
819 (or (and (setq t1 (math-is-polynomial (nth 2 expr) | |
820 math-integ-var 1)) | |
821 (math-div expr | |
822 (math-mul (nth 1 t1) | |
823 (math-normalize | |
824 (list 'calcFunc-ln | |
825 (nth 1 expr)))))) | |
826 (math-integral | |
827 (list 'calcFunc-exp | |
828 (math-mul (nth 2 expr) | |
829 (math-normalize | |
830 (list 'calcFunc-ln | |
831 (nth 1 expr))))) | |
832 'yes t))) | |
833 ((not (math-expr-contains (nth 2 expr) math-integ-var)) | |
834 (if (and (integerp (nth 2 expr)) (< (nth 2 expr) 0)) | |
835 (math-integral | |
836 (list '/ 1 (math-pow (nth 1 expr) (- (nth 2 expr)))) | |
837 nil t) | |
838 (or (and (setq t1 (math-is-polynomial (nth 1 expr) | |
839 math-integ-var | |
840 1)) | |
841 (setq t2 (math-add (nth 2 expr) 1)) | |
842 (math-div (math-pow (nth 1 expr) t2) | |
843 (math-mul t2 (nth 1 t1)))) | |
844 (and (Math-negp (nth 2 expr)) | |
845 (math-integral | |
846 (math-div 1 | |
847 (math-pow (nth 1 expr) | |
848 (math-neg | |
849 (nth 2 expr)))) | |
850 nil t)) | |
851 nil)))))) | |
852 | |
853 ;; Integral of a polynomial. | |
854 (and (setq t1 (math-is-polynomial expr math-integ-var 20)) | |
855 (let ((accum 0) | |
856 (n 1)) | |
857 (while t1 | |
858 (if (setq accum (math-add accum | |
859 (math-div (math-mul (car t1) | |
860 (math-pow | |
861 math-integ-var | |
862 n)) | |
863 n)) | |
864 t1 (cdr t1)) | |
865 (setq n (1+ n)))) | |
866 accum)) | |
867 | |
868 ;; Try looking it up! | |
869 (cond ((= (length expr) 2) | |
870 (and (symbolp (car expr)) | |
871 (setq t1 (get (car expr) 'math-integral)) | |
872 (progn | |
873 (while (and t1 | |
874 (not (setq t2 (funcall (car t1) | |
875 (nth 1 expr))))) | |
876 (setq t1 (cdr t1))) | |
877 (and t2 (math-normalize t2))))) | |
878 ((= (length expr) 3) | |
879 (and (symbolp (car expr)) | |
880 (setq t1 (get (car expr) 'math-integral-2)) | |
881 (progn | |
882 (while (and t1 | |
883 (not (setq t2 (funcall (car t1) | |
884 (nth 1 expr) | |
885 (nth 2 expr))))) | |
886 (setq t1 (cdr t1))) | |
887 (and t2 (math-normalize t2)))))) | |
888 | |
889 ;; Integral of a rational function. | |
890 (and (math-ratpoly-p expr math-integ-var) | |
891 (setq t1 (calcFunc-apart expr math-integ-var)) | |
892 (not (equal t1 expr)) | |
893 (math-integral t1)) | |
894 | |
895 ;; Try user-defined integration rules. | |
896 (and has-rules | |
897 (let ((math-old-integ (symbol-function 'calcFunc-integ)) | |
898 (input (list 'calcFunc-integtry expr math-integ-var)) | |
899 res part) | |
900 (unwind-protect | |
901 (progn | |
902 (fset 'calcFunc-integ 'math-sub-integration) | |
903 (setq res (math-rewrite input | |
904 '(var IntegRules var-IntegRules) | |
905 1)) | |
906 (fset 'calcFunc-integ math-old-integ) | |
907 (and (not (equal res input)) | |
908 (if (setq part (math-expr-calls | |
909 res '(calcFunc-integsubst))) | |
910 (and (memq (length part) '(3 4 5)) | |
911 (let ((parts (mapcar | |
912 (function | |
913 (lambda (x) | |
914 (math-expr-subst | |
915 x (nth 2 part) | |
916 math-integ-var))) | |
917 (cdr part)))) | |
918 (math-integrate-by-substitution | |
919 expr (car parts) t | |
920 (or (nth 2 parts) | |
921 (list 'calcFunc-integfailed | |
922 math-integ-var)) | |
923 (nth 3 parts)))) | |
924 (if (not (math-expr-calls res | |
925 '(calcFunc-integtry | |
926 calcFunc-integfailed))) | |
927 res)))) | |
928 (fset 'calcFunc-integ math-old-integ)))) | |
929 | |
930 ;; See if the function is a symbolic derivative. | |
931 (and (string-match "'" (symbol-name (car expr))) | |
932 (let ((name (symbol-name (car expr))) | |
933 (p expr) (n 0) (which nil) (bad nil)) | |
934 (while (setq n (1+ n) p (cdr p)) | |
935 (if (equal (car p) math-integ-var) | |
936 (if which (setq bad t) (setq which n)) | |
937 (if (math-expr-contains (car p) math-integ-var) | |
938 (setq bad t)))) | |
939 (and which (not bad) | |
940 (let ((prime (if (= which 1) "'" (format "'%d" which)))) | |
941 (and (string-match (concat prime "\\('['0-9]*\\|$\\)") | |
942 name) | |
943 (cons (intern | |
944 (concat | |
945 (substring name 0 (match-beginning 0)) | |
946 (substring name (+ (match-beginning 0) | |
947 (length prime))))) | |
948 (cdr expr))))))) | |
949 | |
950 ;; Try transformation methods (parts, substitutions). | |
951 (and (> math-integ-level 0) | |
952 (math-do-integral-methods expr)) | |
953 | |
954 ;; Try expanding the function's definition. | |
955 (let ((res (math-expand-formula expr))) | |
956 (and res | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
957 (math-integral res)))))) |
40785 | 958 |
959 (defun math-sub-integration (expr &rest rest) | |
960 (or (if (or (not rest) | |
961 (and (< math-integ-level math-integral-limit) | |
962 (eq (car rest) math-integ-var))) | |
963 (math-integral expr) | |
964 (let ((res (apply math-old-integ expr rest))) | |
965 (and (or (= math-integ-level math-integral-limit) | |
966 (not (math-expr-calls res 'calcFunc-integ))) | |
967 res))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
968 (list 'calcFunc-integfailed expr))) |
40785 | 969 |
970 (defun math-do-integral-methods (expr) | |
971 (let ((so-far math-integ-var-list-list) | |
972 rat-in) | |
973 | |
974 ;; Integration by substitution, for various likely sub-expressions. | |
975 ;; (In first pass, we look only for sub-exprs that are linear in X.) | |
976 (or (if math-enable-subst | |
977 (math-integ-try-substitutions expr) | |
978 (math-integ-try-linear-substitutions expr)) | |
979 | |
980 ;; If function has sines and cosines, try tan(x/2) substitution. | |
981 (and (let ((p (setq rat-in (math-expr-rational-in expr)))) | |
982 (while (and p | |
983 (memq (car (car p)) '(calcFunc-sin | |
984 calcFunc-cos | |
985 calcFunc-tan)) | |
986 (equal (nth 1 (car p)) math-integ-var)) | |
987 (setq p (cdr p))) | |
988 (null p)) | |
989 (or (and (math-integ-parts-easy expr) | |
990 (math-integ-try-parts expr t)) | |
991 (math-integrate-by-good-substitution | |
992 expr (list 'calcFunc-tan (math-div math-integ-var 2))))) | |
993 | |
994 ;; If function has sinh and cosh, try tanh(x/2) substitution. | |
995 (and (let ((p rat-in)) | |
996 (while (and p | |
997 (memq (car (car p)) '(calcFunc-sinh | |
998 calcFunc-cosh | |
999 calcFunc-tanh | |
1000 calcFunc-exp)) | |
1001 (equal (nth 1 (car p)) math-integ-var)) | |
1002 (setq p (cdr p))) | |
1003 (null p)) | |
1004 (or (and (math-integ-parts-easy expr) | |
1005 (math-integ-try-parts expr t)) | |
1006 (math-integrate-by-good-substitution | |
1007 expr (list 'calcFunc-tanh (math-div math-integ-var 2))))) | |
1008 | |
1009 ;; If function has square roots, try sin, tan, or sec substitution. | |
1010 (and (let ((p rat-in)) | |
1011 (setq t1 nil) | |
1012 (while (and p | |
1013 (or (equal (car p) math-integ-var) | |
1014 (and (eq (car (car p)) 'calcFunc-sqrt) | |
1015 (setq t1 (math-is-polynomial | |
1016 (nth 1 (setq t2 (car p))) | |
1017 math-integ-var 2))))) | |
1018 (setq p (cdr p))) | |
1019 (and (null p) t1)) | |
1020 (if (cdr (cdr t1)) | |
1021 (if (math-guess-if-neg (nth 2 t1)) | |
1022 (let* ((c (math-sqrt (math-neg (nth 2 t1)))) | |
1023 (d (math-div (nth 1 t1) (math-mul -2 c))) | |
1024 (a (math-sqrt (math-add (car t1) (math-sqr d))))) | |
1025 (math-integrate-by-good-substitution | |
1026 expr (list 'calcFunc-arcsin | |
1027 (math-div-thru | |
1028 (math-add (math-mul c math-integ-var) d) | |
1029 a)))) | |
1030 (let* ((c (math-sqrt (nth 2 t1))) | |
1031 (d (math-div (nth 1 t1) (math-mul 2 c))) | |
1032 (aa (math-sub (car t1) (math-sqr d)))) | |
1033 (if (and nil (not (and (eq d 0) (eq c 1)))) | |
1034 (math-integrate-by-good-substitution | |
1035 expr (math-add (math-mul c math-integ-var) d)) | |
1036 (if (math-guess-if-neg aa) | |
1037 (math-integrate-by-good-substitution | |
1038 expr (list 'calcFunc-arccosh | |
1039 (math-div-thru | |
1040 (math-add (math-mul c math-integ-var) | |
1041 d) | |
1042 (math-sqrt (math-neg aa))))) | |
1043 (math-integrate-by-good-substitution | |
1044 expr (list 'calcFunc-arcsinh | |
1045 (math-div-thru | |
1046 (math-add (math-mul c math-integ-var) | |
1047 d) | |
1048 (math-sqrt aa)))))))) | |
1049 (math-integrate-by-good-substitution expr t2)) ) | |
1050 | |
1051 ;; Try integration by parts. | |
1052 (math-integ-try-parts expr) | |
1053 | |
1054 ;; Give up. | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1055 nil))) |
40785 | 1056 |
1057 (defun math-integ-parts-easy (expr) | |
1058 (cond ((Math-primp expr) t) | |
1059 ((memq (car expr) '(+ - *)) | |
1060 (and (math-integ-parts-easy (nth 1 expr)) | |
1061 (math-integ-parts-easy (nth 2 expr)))) | |
1062 ((eq (car expr) '/) | |
1063 (and (math-integ-parts-easy (nth 1 expr)) | |
1064 (math-atomic-factorp (nth 2 expr)))) | |
1065 ((eq (car expr) '^) | |
1066 (and (natnump (nth 2 expr)) | |
1067 (math-integ-parts-easy (nth 1 expr)))) | |
1068 ((eq (car expr) 'neg) | |
1069 (math-integ-parts-easy (nth 1 expr))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1070 (t t))) |
40785 | 1071 |
1072 (defun math-integ-try-parts (expr &optional math-good-parts) | |
1073 ;; Integration by parts: | |
1074 ;; integ(f(x) g(x),x) = f(x) h(x) - integ(h(x) f'(x),x) | |
1075 ;; where h(x) = integ(g(x),x). | |
1076 (or (let ((exp (calcFunc-expand expr))) | |
1077 (and (not (equal exp expr)) | |
1078 (math-integral exp))) | |
1079 (and (eq (car expr) '*) | |
1080 (let ((first-bad (or (math-polynomial-p (nth 1 expr) | |
1081 math-integ-var) | |
1082 (equal (nth 2 expr) math-prev-parts-v)))) | |
1083 (or (and first-bad ; so try this one first | |
1084 (math-integrate-by-parts (nth 1 expr) (nth 2 expr))) | |
1085 (math-integrate-by-parts (nth 2 expr) (nth 1 expr)) | |
1086 (and (not first-bad) | |
1087 (math-integrate-by-parts (nth 1 expr) (nth 2 expr)))))) | |
1088 (and (eq (car expr) '/) | |
1089 (math-expr-contains (nth 1 expr) math-integ-var) | |
1090 (let ((recip (math-div 1 (nth 2 expr)))) | |
1091 (or (math-integrate-by-parts (nth 1 expr) recip) | |
1092 (math-integrate-by-parts recip (nth 1 expr))))) | |
1093 (and (eq (car expr) '^) | |
1094 (math-integrate-by-parts (math-pow (nth 1 expr) | |
1095 (math-sub (nth 2 expr) 1)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1096 (nth 1 expr))))) |
40785 | 1097 |
1098 (defun math-integrate-by-parts (u vprime) | |
1099 (let ((math-integ-level (if (or math-good-parts | |
1100 (math-polynomial-p u math-integ-var)) | |
1101 math-integ-level | |
1102 (1- math-integ-level))) | |
1103 (math-doing-parts t) | |
1104 v temp) | |
1105 (and (>= math-integ-level 0) | |
1106 (unwind-protect | |
1107 (progn | |
1108 (setcar (cdr cur-record) 'parts) | |
1109 (math-tracing-integral "Integrating by parts, u = " | |
1110 (math-format-value u 1000) | |
1111 ", v' = " | |
1112 (math-format-value vprime 1000) | |
1113 "\n") | |
1114 (and (setq v (math-integral vprime)) | |
1115 (setq temp (calcFunc-deriv u math-integ-var nil t)) | |
1116 (setq temp (let ((math-prev-parts-v v)) | |
1117 (math-integral (math-mul v temp) 'yes))) | |
1118 (setq temp (math-sub (math-mul u v) temp)) | |
1119 (if (eq (nth 1 cur-record) 'parts) | |
1120 (calcFunc-expand temp) | |
1121 (setq v (list 'var 'PARTS cur-record) | |
1122 var-thing (list 'vec (math-sub v temp) v) | |
1123 temp (let (calc-next-why) | |
1124 (math-solve-for (math-sub v temp) 0 v nil))) | |
1125 (and temp (not (integerp temp)) | |
1126 (math-simplify-extended temp))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1127 (setcar (cdr cur-record) 'busy))))) |
40785 | 1128 |
1129 ;;; This tries two different formulations, hoping the algebraic simplifier | |
1130 ;;; will be strong enough to handle at least one. | |
1131 (defun math-integrate-by-substitution (expr u &optional user uinv uinvprime) | |
1132 (and (> math-integ-level 0) | |
1133 (let ((math-integ-level (max (- math-integ-level 2) 0))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1134 (math-integrate-by-good-substitution expr u user uinv uinvprime)))) |
40785 | 1135 |
1136 (defun math-integrate-by-good-substitution (expr u &optional user | |
1137 uinv uinvprime) | |
1138 (let ((math-living-dangerously t) | |
1139 deriv temp) | |
1140 (and (setq uinv (if uinv | |
1141 (math-expr-subst uinv math-integ-var | |
1142 math-integ-var-2) | |
1143 (let (calc-next-why) | |
1144 (math-solve-for u | |
1145 math-integ-var-2 | |
1146 math-integ-var nil)))) | |
1147 (progn | |
1148 (math-tracing-integral "Integrating by substitution, u = " | |
1149 (math-format-value u 1000) | |
1150 "\n") | |
1151 (or (and (setq deriv (calcFunc-deriv u | |
1152 math-integ-var nil | |
1153 (not user))) | |
1154 (setq temp (math-integral (math-expr-subst | |
1155 (math-expr-subst | |
1156 (math-expr-subst | |
1157 (math-div expr deriv) | |
1158 u | |
1159 math-integ-var-2) | |
1160 math-integ-var | |
1161 uinv) | |
1162 math-integ-var-2 | |
1163 math-integ-var) | |
1164 'yes))) | |
1165 (and (setq deriv (or uinvprime | |
1166 (calcFunc-deriv uinv | |
1167 math-integ-var-2 | |
1168 math-integ-var | |
1169 (not user)))) | |
1170 (setq temp (math-integral (math-mul | |
1171 (math-expr-subst | |
1172 (math-expr-subst | |
1173 (math-expr-subst | |
1174 expr | |
1175 u | |
1176 math-integ-var-2) | |
1177 math-integ-var | |
1178 uinv) | |
1179 math-integ-var-2 | |
1180 math-integ-var) | |
1181 deriv) | |
1182 'yes))))) | |
1183 (math-simplify-extended | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1184 (math-expr-subst temp math-integ-var u))))) |
40785 | 1185 |
1186 ;;; Look for substitutions of the form u = a x + b. | |
1187 (defun math-integ-try-linear-substitutions (sub-expr) | |
1188 (and (not (Math-primp sub-expr)) | |
1189 (or (and (not (memq (car sub-expr) '(+ - * / neg))) | |
1190 (not (and (eq (car sub-expr) '^) | |
1191 (integerp (nth 2 sub-expr)))) | |
1192 (math-expr-contains sub-expr math-integ-var) | |
1193 (let ((res nil)) | |
1194 (while (and (setq sub-expr (cdr sub-expr)) | |
1195 (or (not (math-linear-in (car sub-expr) | |
1196 math-integ-var)) | |
1197 (assoc (car sub-expr) so-far) | |
1198 (progn | |
1199 (setq so-far (cons (list (car sub-expr)) | |
1200 so-far)) | |
1201 (not (setq res | |
1202 (math-integrate-by-substitution | |
1203 expr (car sub-expr)))))))) | |
1204 res)) | |
1205 (let ((res nil)) | |
1206 (while (and (setq sub-expr (cdr sub-expr)) | |
1207 (not (setq res (math-integ-try-linear-substitutions | |
1208 (car sub-expr)))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1209 res)))) |
40785 | 1210 |
1211 ;;; Recursively try different substitutions based on various sub-expressions. | |
1212 (defun math-integ-try-substitutions (sub-expr &optional allow-rat) | |
1213 (and (not (Math-primp sub-expr)) | |
1214 (not (assoc sub-expr so-far)) | |
1215 (math-expr-contains sub-expr math-integ-var) | |
1216 (or (and (if (and (not (memq (car sub-expr) '(+ - * / neg))) | |
1217 (not (and (eq (car sub-expr) '^) | |
1218 (integerp (nth 2 sub-expr))))) | |
1219 (setq allow-rat t) | |
1220 (prog1 allow-rat (setq allow-rat nil))) | |
1221 (not (eq sub-expr expr)) | |
1222 (or (math-integrate-by-substitution expr sub-expr) | |
1223 (and (eq (car sub-expr) '^) | |
1224 (integerp (nth 2 sub-expr)) | |
1225 (< (nth 2 sub-expr) 0) | |
1226 (math-integ-try-substitutions | |
1227 (math-pow (nth 1 sub-expr) (- (nth 2 sub-expr))) | |
1228 t)))) | |
1229 (let ((res nil)) | |
1230 (setq so-far (cons (list sub-expr) so-far)) | |
1231 (while (and (setq sub-expr (cdr sub-expr)) | |
1232 (not (setq res (math-integ-try-substitutions | |
1233 (car sub-expr) allow-rat))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1234 res)))) |
40785 | 1235 |
1236 (defun math-expr-rational-in (expr) | |
1237 (let ((parts nil)) | |
1238 (math-expr-rational-in-rec expr) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1239 (mapcar 'car parts))) |
40785 | 1240 |
1241 (defun math-expr-rational-in-rec (expr) | |
1242 (cond ((Math-primp expr) | |
1243 (and (equal expr math-integ-var) | |
1244 (not (assoc expr parts)) | |
1245 (setq parts (cons (list expr) parts)))) | |
1246 ((or (memq (car expr) '(+ - * / neg)) | |
1247 (and (eq (car expr) '^) (integerp (nth 2 expr)))) | |
1248 (math-expr-rational-in-rec (nth 1 expr)) | |
1249 (and (nth 2 expr) (math-expr-rational-in-rec (nth 2 expr)))) | |
1250 ((and (eq (car expr) '^) | |
1251 (eq (math-quarter-integer (nth 2 expr)) 2)) | |
1252 (math-expr-rational-in-rec (list 'calcFunc-sqrt (nth 1 expr)))) | |
1253 (t | |
1254 (and (not (assoc expr parts)) | |
1255 (math-expr-contains expr math-integ-var) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1256 (setq parts (cons (list expr) parts)))))) |
40785 | 1257 |
1258 (defun math-expr-calls (expr funcs &optional arg-contains) | |
1259 (if (consp expr) | |
1260 (if (or (memq (car expr) funcs) | |
1261 (and (eq (car expr) '^) (eq (car funcs) 'calcFunc-sqrt) | |
1262 (eq (math-quarter-integer (nth 2 expr)) 2))) | |
1263 (and (or (not arg-contains) | |
1264 (math-expr-contains expr arg-contains)) | |
1265 expr) | |
1266 (and (not (Math-primp expr)) | |
1267 (let ((res nil)) | |
1268 (while (and (setq expr (cdr expr)) | |
1269 (not (setq res (math-expr-calls | |
1270 (car expr) funcs arg-contains))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1271 res))))) |
40785 | 1272 |
1273 (defun math-fix-const-terms (expr except-vars) | |
1274 (cond ((not (math-expr-depends expr except-vars)) 0) | |
1275 ((Math-primp expr) expr) | |
1276 ((eq (car expr) '+) | |
1277 (math-add (math-fix-const-terms (nth 1 expr) except-vars) | |
1278 (math-fix-const-terms (nth 2 expr) except-vars))) | |
1279 ((eq (car expr) '-) | |
1280 (math-sub (math-fix-const-terms (nth 1 expr) except-vars) | |
1281 (math-fix-const-terms (nth 2 expr) except-vars))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1282 (t expr))) |
40785 | 1283 |
1284 ;; Command for debugging the Calculator's symbolic integrator. | |
1285 (defun calc-dump-integral-cache (&optional arg) | |
1286 (interactive "P") | |
1287 (let ((buf (current-buffer))) | |
1288 (unwind-protect | |
1289 (let ((p math-integral-cache) | |
1290 cur-record) | |
1291 (display-buffer (get-buffer-create "*Integral Cache*")) | |
1292 (set-buffer (get-buffer "*Integral Cache*")) | |
1293 (erase-buffer) | |
1294 (while p | |
1295 (setq cur-record (car p)) | |
1296 (or arg (math-replace-integral-parts cur-record)) | |
1297 (insert (math-format-flat-expr (car cur-record) 0) | |
1298 " --> " | |
1299 (if (symbolp (nth 1 cur-record)) | |
1300 (concat "(" (symbol-name (nth 1 cur-record)) ")") | |
1301 (math-format-flat-expr (nth 1 cur-record) 0)) | |
1302 "\n") | |
1303 (setq p (cdr p))) | |
1304 (goto-char (point-min))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1305 (set-buffer buf)))) |
40785 | 1306 |
1307 (defun math-try-integral (expr) | |
1308 (let ((math-integ-level math-integral-limit) | |
1309 (math-integ-depth 0) | |
1310 (math-integ-msg "Working...done") | |
1311 (cur-record nil) ; a technicality | |
1312 (math-integrating t) | |
1313 (calc-prefer-frac t) | |
1314 (calc-symbolic-mode t) | |
1315 (has-rules (calc-has-rules 'var-IntegRules))) | |
1316 (or (math-integral expr 'yes) | |
1317 (and math-any-substs | |
1318 (setq math-enable-subst t) | |
1319 (math-integral expr 'yes)) | |
1320 (and (> math-max-integral-limit math-integral-limit) | |
1321 (setq math-integral-limit math-max-integral-limit | |
1322 math-integ-level math-integral-limit) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1323 (math-integral expr 'yes))))) |
40785 | 1324 |
1325 (defun calcFunc-integ (expr var &optional low high) | |
1326 (cond | |
1327 ;; Do these even if the parts turn out not to be integrable. | |
1328 ((eq (car-safe expr) '+) | |
1329 (math-add (calcFunc-integ (nth 1 expr) var low high) | |
1330 (calcFunc-integ (nth 2 expr) var low high))) | |
1331 ((eq (car-safe expr) '-) | |
1332 (math-sub (calcFunc-integ (nth 1 expr) var low high) | |
1333 (calcFunc-integ (nth 2 expr) var low high))) | |
1334 ((eq (car-safe expr) 'neg) | |
1335 (math-neg (calcFunc-integ (nth 1 expr) var low high))) | |
1336 ((and (eq (car-safe expr) '*) | |
1337 (not (math-expr-contains (nth 1 expr) var))) | |
1338 (math-mul (nth 1 expr) (calcFunc-integ (nth 2 expr) var low high))) | |
1339 ((and (eq (car-safe expr) '*) | |
1340 (not (math-expr-contains (nth 2 expr) var))) | |
1341 (math-mul (calcFunc-integ (nth 1 expr) var low high) (nth 2 expr))) | |
1342 ((and (eq (car-safe expr) '/) | |
1343 (not (math-expr-contains (nth 1 expr) var)) | |
1344 (not (math-equal-int (nth 1 expr) 1))) | |
1345 (math-mul (nth 1 expr) | |
1346 (calcFunc-integ (math-div 1 (nth 2 expr)) var low high))) | |
1347 ((and (eq (car-safe expr) '/) | |
1348 (not (math-expr-contains (nth 2 expr) var))) | |
1349 (math-div (calcFunc-integ (nth 1 expr) var low high) (nth 2 expr))) | |
1350 ((and (eq (car-safe expr) '/) | |
1351 (eq (car-safe (nth 1 expr)) '*) | |
1352 (not (math-expr-contains (nth 1 (nth 1 expr)) var))) | |
1353 (math-mul (nth 1 (nth 1 expr)) | |
1354 (calcFunc-integ (math-div (nth 2 (nth 1 expr)) (nth 2 expr)) | |
1355 var low high))) | |
1356 ((and (eq (car-safe expr) '/) | |
1357 (eq (car-safe (nth 1 expr)) '*) | |
1358 (not (math-expr-contains (nth 2 (nth 1 expr)) var))) | |
1359 (math-mul (nth 2 (nth 1 expr)) | |
1360 (calcFunc-integ (math-div (nth 1 (nth 1 expr)) (nth 2 expr)) | |
1361 var low high))) | |
1362 ((and (eq (car-safe expr) '/) | |
1363 (eq (car-safe (nth 2 expr)) '*) | |
1364 (not (math-expr-contains (nth 1 (nth 2 expr)) var))) | |
1365 (math-div (calcFunc-integ (math-div (nth 1 expr) (nth 2 (nth 2 expr))) | |
1366 var low high) | |
1367 (nth 1 (nth 2 expr)))) | |
1368 ((and (eq (car-safe expr) '/) | |
1369 (eq (car-safe (nth 2 expr)) '*) | |
1370 (not (math-expr-contains (nth 2 (nth 2 expr)) var))) | |
1371 (math-div (calcFunc-integ (math-div (nth 1 expr) (nth 1 (nth 2 expr))) | |
1372 var low high) | |
1373 (nth 2 (nth 2 expr)))) | |
1374 ((eq (car-safe expr) 'vec) | |
1375 (cons 'vec (mapcar (function (lambda (x) (calcFunc-integ x var low high))) | |
1376 (cdr expr)))) | |
1377 (t | |
1378 (let ((state (list calc-angle-mode | |
1379 ;;calc-symbolic-mode | |
1380 ;;calc-prefer-frac | |
1381 calc-internal-prec | |
1382 (calc-var-value 'var-IntegRules) | |
1383 (calc-var-value 'var-IntegSimpRules)))) | |
1384 (or (equal state math-integral-cache-state) | |
1385 (setq math-integral-cache-state state | |
1386 math-integral-cache nil))) | |
1387 (let* ((math-max-integral-limit (or (and (boundp 'var-IntegLimit) | |
1388 (natnump var-IntegLimit) | |
1389 var-IntegLimit) | |
1390 3)) | |
1391 (math-integral-limit 1) | |
1392 (sexpr (math-expr-subst expr var math-integ-var)) | |
1393 (trace-buffer (get-buffer "*Trace*")) | |
1394 (calc-language (if (eq calc-language 'big) nil calc-language)) | |
1395 (math-any-substs t) | |
1396 (math-enable-subst nil) | |
1397 (math-prev-parts-v nil) | |
1398 (math-doing-parts nil) | |
1399 (math-good-parts nil) | |
1400 (res | |
1401 (if trace-buffer | |
1402 (let ((calcbuf (current-buffer)) | |
1403 (calcwin (selected-window))) | |
1404 (unwind-protect | |
1405 (progn | |
1406 (if (get-buffer-window trace-buffer) | |
1407 (select-window (get-buffer-window trace-buffer))) | |
1408 (set-buffer trace-buffer) | |
1409 (goto-char (point-max)) | |
1410 (or (assq 'scroll-stop (buffer-local-variables)) | |
1411 (progn | |
1412 (make-local-variable 'scroll-step) | |
1413 (setq scroll-step 3))) | |
1414 (insert "\n\n\n") | |
1415 (set-buffer calcbuf) | |
1416 (math-try-integral sexpr)) | |
1417 (select-window calcwin) | |
1418 (set-buffer calcbuf))) | |
1419 (math-try-integral sexpr)))) | |
1420 (if res | |
1421 (progn | |
1422 (if (calc-has-rules 'var-IntegAfterRules) | |
1423 (setq res (math-rewrite res '(var IntegAfterRules | |
1424 var-IntegAfterRules)))) | |
1425 (math-simplify | |
1426 (if (and low high) | |
1427 (math-sub (math-expr-subst res math-integ-var high) | |
1428 (math-expr-subst res math-integ-var low)) | |
1429 (setq res (math-fix-const-terms res math-integ-vars)) | |
1430 (if low | |
1431 (math-expr-subst res math-integ-var low) | |
1432 (math-expr-subst res math-integ-var var))))) | |
1433 (append (list 'calcFunc-integ expr var) | |
1434 (and low (list low)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1435 (and high (list high)))))))) |
40785 | 1436 |
1437 | |
1438 (math-defintegral calcFunc-inv | |
1439 (math-integral (math-div 1 u))) | |
1440 | |
1441 (math-defintegral calcFunc-conj | |
1442 (let ((int (math-integral u))) | |
1443 (and int | |
1444 (list 'calcFunc-conj int)))) | |
1445 | |
1446 (math-defintegral calcFunc-deg | |
1447 (let ((int (math-integral u))) | |
1448 (and int | |
1449 (list 'calcFunc-deg int)))) | |
1450 | |
1451 (math-defintegral calcFunc-rad | |
1452 (let ((int (math-integral u))) | |
1453 (and int | |
1454 (list 'calcFunc-rad int)))) | |
1455 | |
1456 (math-defintegral calcFunc-re | |
1457 (let ((int (math-integral u))) | |
1458 (and int | |
1459 (list 'calcFunc-re int)))) | |
1460 | |
1461 (math-defintegral calcFunc-im | |
1462 (let ((int (math-integral u))) | |
1463 (and int | |
1464 (list 'calcFunc-im int)))) | |
1465 | |
1466 (math-defintegral calcFunc-sqrt | |
1467 (and (equal u math-integ-var) | |
1468 (math-mul '(frac 2 3) | |
1469 (list 'calcFunc-sqrt (math-pow u 3))))) | |
1470 | |
1471 (math-defintegral calcFunc-exp | |
1472 (or (and (equal u math-integ-var) | |
1473 (list 'calcFunc-exp u)) | |
1474 (let ((p (math-is-polynomial u math-integ-var 2))) | |
1475 (and (nth 2 p) | |
1476 (let ((sqa (math-sqrt (math-neg (nth 2 p))))) | |
1477 (math-div | |
1478 (math-mul | |
1479 (math-mul (math-div (list 'calcFunc-sqrt '(var pi var-pi)) | |
1480 sqa) | |
1481 (math-normalize | |
1482 (list 'calcFunc-exp | |
1483 (math-div (math-sub (math-mul (car p) | |
1484 (nth 2 p)) | |
1485 (math-div | |
1486 (math-sqr (nth 1 p)) | |
1487 4)) | |
1488 (nth 2 p))))) | |
1489 (list 'calcFunc-erf | |
1490 (math-sub (math-mul sqa math-integ-var) | |
1491 (math-div (nth 1 p) (math-mul 2 sqa))))) | |
1492 2)))))) | |
1493 | |
1494 (math-defintegral calcFunc-ln | |
1495 (or (and (equal u math-integ-var) | |
1496 (math-sub (math-mul u (list 'calcFunc-ln u)) u)) | |
1497 (and (eq (car u) '*) | |
1498 (math-integral (math-add (list 'calcFunc-ln (nth 1 u)) | |
1499 (list 'calcFunc-ln (nth 2 u))))) | |
1500 (and (eq (car u) '/) | |
1501 (math-integral (math-sub (list 'calcFunc-ln (nth 1 u)) | |
1502 (list 'calcFunc-ln (nth 2 u))))) | |
1503 (and (eq (car u) '^) | |
1504 (math-integral (math-mul (nth 2 u) | |
1505 (list 'calcFunc-ln (nth 1 u))))))) | |
1506 | |
1507 (math-defintegral calcFunc-log10 | |
1508 (and (equal u math-integ-var) | |
1509 (math-sub (math-mul u (list 'calcFunc-ln u)) | |
1510 (math-div u (list 'calcFunc-ln 10))))) | |
1511 | |
1512 (math-defintegral-2 calcFunc-log | |
1513 (math-integral (math-div (list 'calcFunc-ln u) | |
1514 (list 'calcFunc-ln v)))) | |
1515 | |
1516 (math-defintegral calcFunc-sin | |
1517 (or (and (equal u math-integ-var) | |
1518 (math-neg (math-from-radians-2 (list 'calcFunc-cos u)))) | |
1519 (and (nth 2 (math-is-polynomial u math-integ-var 2)) | |
1520 (math-integral (math-to-exponentials (list 'calcFunc-sin u)))))) | |
1521 | |
1522 (math-defintegral calcFunc-cos | |
1523 (or (and (equal u math-integ-var) | |
1524 (math-from-radians-2 (list 'calcFunc-sin u))) | |
1525 (and (nth 2 (math-is-polynomial u math-integ-var 2)) | |
1526 (math-integral (math-to-exponentials (list 'calcFunc-cos u)))))) | |
1527 | |
1528 (math-defintegral calcFunc-tan | |
1529 (and (equal u math-integ-var) | |
1530 (math-neg (math-from-radians-2 | |
1531 (list 'calcFunc-ln (list 'calcFunc-cos u)))))) | |
1532 | |
1533 (math-defintegral calcFunc-arcsin | |
1534 (and (equal u math-integ-var) | |
1535 (math-add (math-mul u (list 'calcFunc-arcsin u)) | |
1536 (math-from-radians-2 | |
1537 (list 'calcFunc-sqrt (math-sub 1 (math-sqr u))))))) | |
1538 | |
1539 (math-defintegral calcFunc-arccos | |
1540 (and (equal u math-integ-var) | |
1541 (math-sub (math-mul u (list 'calcFunc-arccos u)) | |
1542 (math-from-radians-2 | |
1543 (list 'calcFunc-sqrt (math-sub 1 (math-sqr u))))))) | |
1544 | |
1545 (math-defintegral calcFunc-arctan | |
1546 (and (equal u math-integ-var) | |
1547 (math-sub (math-mul u (list 'calcFunc-arctan u)) | |
1548 (math-from-radians-2 | |
1549 (math-div (list 'calcFunc-ln (math-add 1 (math-sqr u))) | |
1550 2))))) | |
1551 | |
1552 (math-defintegral calcFunc-sinh | |
1553 (and (equal u math-integ-var) | |
1554 (list 'calcFunc-cosh u))) | |
1555 | |
1556 (math-defintegral calcFunc-cosh | |
1557 (and (equal u math-integ-var) | |
1558 (list 'calcFunc-sinh u))) | |
1559 | |
1560 (math-defintegral calcFunc-tanh | |
1561 (and (equal u math-integ-var) | |
1562 (list 'calcFunc-ln (list 'calcFunc-cosh u)))) | |
1563 | |
1564 (math-defintegral calcFunc-arcsinh | |
1565 (and (equal u math-integ-var) | |
1566 (math-sub (math-mul u (list 'calcFunc-arcsinh u)) | |
1567 (list 'calcFunc-sqrt (math-add (math-sqr u) 1))))) | |
1568 | |
1569 (math-defintegral calcFunc-arccosh | |
1570 (and (equal u math-integ-var) | |
1571 (math-sub (math-mul u (list 'calcFunc-arccosh u)) | |
1572 (list 'calcFunc-sqrt (math-sub 1 (math-sqr u)))))) | |
1573 | |
1574 (math-defintegral calcFunc-arctanh | |
1575 (and (equal u math-integ-var) | |
1576 (math-sub (math-mul u (list 'calcFunc-arctan u)) | |
1577 (math-div (list 'calcFunc-ln | |
1578 (math-add 1 (math-sqr u))) | |
1579 2)))) | |
1580 | |
1581 ;;; (Ax + B) / (ax^2 + bx + c)^n forms. | |
1582 (math-defintegral-2 / | |
1583 (math-integral-rational-funcs u v)) | |
1584 | |
1585 (defun math-integral-rational-funcs (u v) | |
1586 (let ((pu (math-is-polynomial u math-integ-var 1)) | |
1587 (vpow 1) pv) | |
1588 (and pu | |
1589 (catch 'int-rat | |
1590 (if (and (eq (car-safe v) '^) (natnump (nth 2 v))) | |
1591 (setq vpow (nth 2 v) | |
1592 v (nth 1 v))) | |
1593 (and (setq pv (math-is-polynomial v math-integ-var 2)) | |
1594 (let ((int (math-mul-thru | |
1595 (car pu) | |
1596 (math-integral-q02 (car pv) (nth 1 pv) | |
1597 (nth 2 pv) v vpow)))) | |
1598 (if (cdr pu) | |
1599 (setq int (math-add int | |
1600 (math-mul-thru | |
1601 (nth 1 pu) | |
1602 (math-integral-q12 | |
1603 (car pv) (nth 1 pv) | |
1604 (nth 2 pv) v vpow))))) | |
1605 int)))))) | |
1606 | |
1607 (defun math-integral-q12 (a b c v vpow) | |
1608 (let (q) | |
1609 (cond ((not c) | |
1610 (cond ((= vpow 1) | |
1611 (math-sub (math-div math-integ-var b) | |
1612 (math-mul (math-div a (math-sqr b)) | |
1613 (list 'calcFunc-ln v)))) | |
1614 ((= vpow 2) | |
1615 (math-div (math-add (list 'calcFunc-ln v) | |
1616 (math-div a v)) | |
1617 (math-sqr b))) | |
1618 (t | |
1619 (let ((nm1 (math-sub vpow 1)) | |
1620 (nm2 (math-sub vpow 2))) | |
1621 (math-div (math-sub | |
1622 (math-div a (math-mul nm1 (math-pow v nm1))) | |
1623 (math-div 1 (math-mul nm2 (math-pow v nm2)))) | |
1624 (math-sqr b)))))) | |
1625 ((math-zerop | |
1626 (setq q (math-sub (math-mul 4 (math-mul a c)) (math-sqr b)))) | |
1627 (let ((part (math-div b (math-mul 2 c)))) | |
1628 (math-mul-thru (math-pow c vpow) | |
1629 (math-integral-q12 part 1 nil | |
1630 (math-add math-integ-var part) | |
1631 (* vpow 2))))) | |
1632 ((= vpow 1) | |
1633 (and (math-ratp q) (math-negp q) | |
1634 (let ((calc-symbolic-mode t)) | |
1635 (math-ratp (math-sqrt (math-neg q)))) | |
1636 (throw 'int-rat nil)) ; should have used calcFunc-apart first | |
1637 (math-sub (math-div (list 'calcFunc-ln v) (math-mul 2 c)) | |
1638 (math-mul-thru (math-div b (math-mul 2 c)) | |
1639 (math-integral-q02 a b c v 1)))) | |
1640 (t | |
1641 (let ((n (1- vpow))) | |
1642 (math-sub (math-neg (math-div | |
1643 (math-add (math-mul b math-integ-var) | |
1644 (math-mul 2 a)) | |
1645 (math-mul n (math-mul q (math-pow v n))))) | |
1646 (math-mul-thru (math-div (math-mul b (1- (* 2 n))) | |
1647 (math-mul n q)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1648 (math-integral-q02 a b c v n)))))))) |
40785 | 1649 |
1650 (defun math-integral-q02 (a b c v vpow) | |
1651 (let (q rq part) | |
1652 (cond ((not c) | |
1653 (cond ((= vpow 1) | |
1654 (math-div (list 'calcFunc-ln v) b)) | |
1655 (t | |
1656 (math-div (math-pow v (- 1 vpow)) | |
1657 (math-mul (- 1 vpow) b))))) | |
1658 ((math-zerop | |
1659 (setq q (math-sub (math-mul 4 (math-mul a c)) (math-sqr b)))) | |
1660 (let ((part (math-div b (math-mul 2 c)))) | |
1661 (math-mul-thru (math-pow c vpow) | |
1662 (math-integral-q02 part 1 nil | |
1663 (math-add math-integ-var part) | |
1664 (* vpow 2))))) | |
1665 ((progn | |
1666 (setq part (math-add (math-mul 2 (math-mul c math-integ-var)) b)) | |
1667 (> vpow 1)) | |
1668 (let ((n (1- vpow))) | |
1669 (math-add (math-div part (math-mul n (math-mul q (math-pow v n)))) | |
1670 (math-mul-thru (math-div (math-mul (- (* 4 n) 2) c) | |
1671 (math-mul n q)) | |
1672 (math-integral-q02 a b c v n))))) | |
1673 ((math-guess-if-neg q) | |
1674 (setq rq (list 'calcFunc-sqrt (math-neg q))) | |
1675 ;;(math-div-thru (list 'calcFunc-ln | |
1676 ;; (math-div (math-sub part rq) | |
1677 ;; (math-add part rq))) | |
1678 ;; rq) | |
1679 (math-div (math-mul -2 (list 'calcFunc-arctanh | |
1680 (math-div part rq))) | |
1681 rq)) | |
1682 (t | |
1683 (setq rq (list 'calcFunc-sqrt q)) | |
1684 (math-div (math-mul 2 (math-to-radians-2 | |
1685 (list 'calcFunc-arctan | |
1686 (math-div part rq)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1687 rq))))) |
40785 | 1688 |
1689 | |
1690 (math-defintegral calcFunc-erf | |
1691 (and (equal u math-integ-var) | |
1692 (math-add (math-mul u (list 'calcFunc-erf u)) | |
1693 (math-div 1 (math-mul (list 'calcFunc-exp (math-sqr u)) | |
1694 (list 'calcFunc-sqrt | |
1695 '(var pi var-pi))))))) | |
1696 | |
1697 (math-defintegral calcFunc-erfc | |
1698 (and (equal u math-integ-var) | |
1699 (math-sub (math-mul u (list 'calcFunc-erfc u)) | |
1700 (math-div 1 (math-mul (list 'calcFunc-exp (math-sqr u)) | |
1701 (list 'calcFunc-sqrt | |
1702 '(var pi var-pi))))))) | |
1703 | |
1704 | |
1705 | |
1706 | |
1707 (defun calcFunc-table (expr var &optional low high step) | |
1708 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf))) | |
1709 (or high (setq high low low 1)) | |
1710 (and (or (math-infinitep low) (math-infinitep high)) | |
1711 (not step) | |
1712 (math-scan-for-limits expr)) | |
1713 (and step (math-zerop step) (math-reject-arg step 'nonzerop)) | |
1714 (let ((known (+ (if (Math-objectp low) 1 0) | |
1715 (if (Math-objectp high) 1 0) | |
1716 (if (or (null step) (Math-objectp step)) 1 0))) | |
1717 (count '(var inf var-inf)) | |
1718 vec) | |
1719 (or (= known 2) ; handy optimization | |
1720 (equal high '(var inf var-inf)) | |
1721 (progn | |
1722 (setq count (math-div (math-sub high low) (or step 1))) | |
1723 (or (Math-objectp count) | |
1724 (setq count (math-simplify count))) | |
1725 (if (Math-messy-integerp count) | |
1726 (setq count (math-trunc count))))) | |
1727 (if (Math-negp count) | |
1728 (setq count -1)) | |
1729 (if (integerp count) | |
1730 (let ((var-DUMMY nil) | |
1731 (vec math-tabulate-initial) | |
1732 (math-working-step-2 (1+ count)) | |
1733 (math-working-step 0)) | |
1734 (setq expr (math-evaluate-expr | |
1735 (math-expr-subst expr var '(var DUMMY var-DUMMY)))) | |
1736 (while (>= count 0) | |
1737 (setq math-working-step (1+ math-working-step) | |
1738 var-DUMMY low | |
1739 vec (cond ((eq math-tabulate-function 'calcFunc-sum) | |
1740 (math-add vec (math-evaluate-expr expr))) | |
1741 ((eq math-tabulate-function 'calcFunc-prod) | |
1742 (math-mul vec (math-evaluate-expr expr))) | |
1743 (t | |
1744 (cons (math-evaluate-expr expr) vec))) | |
1745 low (math-add low (or step 1)) | |
1746 count (1- count))) | |
1747 (if math-tabulate-function | |
1748 vec | |
1749 (cons 'vec (nreverse vec)))) | |
1750 (if (Math-integerp count) | |
1751 (calc-record-why 'fixnump high) | |
1752 (if (Math-num-integerp low) | |
1753 (if (Math-num-integerp high) | |
1754 (calc-record-why 'integerp step) | |
1755 (calc-record-why 'integerp high)) | |
1756 (calc-record-why 'integerp low))) | |
1757 (append (list (or math-tabulate-function 'calcFunc-table) | |
1758 expr var) | |
1759 (and (not (and (equal low '(neg (var inf var-inf))) | |
1760 (equal high '(var inf var-inf)))) | |
1761 (list low high)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1762 (and step (list step)))))) |
40785 | 1763 |
1764 (setq math-tabulate-initial nil) | |
1765 (setq math-tabulate-function nil) | |
1766 | |
1767 (defun math-scan-for-limits (x) | |
1768 (cond ((Math-primp x)) | |
1769 ((and (eq (car x) 'calcFunc-subscr) | |
1770 (Math-vectorp (nth 1 x)) | |
1771 (math-expr-contains (nth 2 x) var)) | |
1772 (let* ((calc-next-why nil) | |
1773 (low-val (math-solve-for (nth 2 x) 1 var nil)) | |
1774 (high-val (math-solve-for (nth 2 x) (1- (length (nth 1 x))) | |
1775 var nil)) | |
1776 temp) | |
1777 (and low-val (math-realp low-val) | |
1778 high-val (math-realp high-val)) | |
1779 (and (Math-lessp high-val low-val) | |
1780 (setq temp low-val low-val high-val high-val temp)) | |
1781 (setq low (math-max low (math-ceiling low-val)) | |
1782 high (math-min high (math-floor high-val))))) | |
1783 (t | |
1784 (while (setq x (cdr x)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1785 (math-scan-for-limits (car x)))))) |
40785 | 1786 |
1787 | |
1788 (defun calcFunc-sum (expr var &optional low high step) | |
1789 (if math-disable-sums (math-reject-arg)) | |
1790 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2))) | |
1791 (math-sum-rec expr var low high step))) | |
1792 (math-disable-sums t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1793 (math-normalize res))) |
40785 | 1794 (setq math-disable-sums nil) |
1795 | |
1796 (defun math-sum-rec (expr var &optional low high step) | |
1797 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf))) | |
1798 (and low (not high) (setq high low low 1)) | |
1799 (let (t1 t2 val) | |
1800 (setq val | |
1801 (cond | |
1802 ((not (math-expr-contains expr var)) | |
1803 (math-mul expr (math-add (math-div (math-sub high low) (or step 1)) | |
1804 1))) | |
1805 ((and step (not (math-equal-int step 1))) | |
1806 (if (math-negp step) | |
1807 (math-sum-rec expr var high low (math-neg step)) | |
1808 (let ((lo (math-simplify (math-div low step)))) | |
1809 (if (math-known-num-integerp lo) | |
1810 (math-sum-rec (math-normalize | |
1811 (math-expr-subst expr var | |
1812 (math-mul step var))) | |
1813 var lo (math-simplify (math-div high step))) | |
1814 (math-sum-rec (math-normalize | |
1815 (math-expr-subst expr var | |
1816 (math-add (math-mul step var) | |
1817 low))) | |
1818 var 0 | |
1819 (math-simplify (math-div (math-sub high low) | |
1820 step))))))) | |
1821 ((memq (setq t1 (math-compare low high)) '(0 1)) | |
1822 (if (eq t1 0) | |
1823 (math-expr-subst expr var low) | |
1824 0)) | |
1825 ((setq t1 (math-is-polynomial expr var 20)) | |
1826 (let ((poly nil) | |
1827 (n 0)) | |
1828 (while t1 | |
1829 (setq poly (math-poly-mix poly 1 | |
1830 (math-sum-integer-power n) (car t1)) | |
1831 n (1+ n) | |
1832 t1 (cdr t1))) | |
1833 (setq n (math-build-polynomial-expr poly high)) | |
1834 (if (memq low '(0 1)) | |
1835 n | |
1836 (math-sub n (math-build-polynomial-expr poly | |
1837 (math-sub low 1)))))) | |
1838 ((and (memq (car expr) '(+ -)) | |
1839 (setq t1 (math-sum-rec (nth 1 expr) var low high) | |
1840 t2 (math-sum-rec (nth 2 expr) var low high)) | |
1841 (not (and (math-expr-calls t1 '(calcFunc-sum)) | |
1842 (math-expr-calls t2 '(calcFunc-sum))))) | |
1843 (list (car expr) t1 t2)) | |
1844 ((and (eq (car expr) '*) | |
1845 (setq t1 (math-sum-const-factors expr var))) | |
1846 (math-mul (car t1) (math-sum-rec (cdr t1) var low high))) | |
1847 ((and (eq (car expr) '*) (memq (car-safe (nth 1 expr)) '(+ -))) | |
1848 (math-sum-rec (math-add-or-sub (math-mul (nth 1 (nth 1 expr)) | |
1849 (nth 2 expr)) | |
1850 (math-mul (nth 2 (nth 1 expr)) | |
1851 (nth 2 expr)) | |
1852 nil (eq (car (nth 1 expr)) '-)) | |
1853 var low high)) | |
1854 ((and (eq (car expr) '*) (memq (car-safe (nth 2 expr)) '(+ -))) | |
1855 (math-sum-rec (math-add-or-sub (math-mul (nth 1 expr) | |
1856 (nth 1 (nth 2 expr))) | |
1857 (math-mul (nth 1 expr) | |
1858 (nth 2 (nth 2 expr))) | |
1859 nil (eq (car (nth 2 expr)) '-)) | |
1860 var low high)) | |
1861 ((and (eq (car expr) '/) | |
1862 (not (math-primp (nth 1 expr))) | |
1863 (setq t1 (math-sum-const-factors (nth 1 expr) var))) | |
1864 (math-mul (car t1) | |
1865 (math-sum-rec (math-div (cdr t1) (nth 2 expr)) | |
1866 var low high))) | |
1867 ((and (eq (car expr) '/) | |
1868 (setq t1 (math-sum-const-factors (nth 2 expr) var))) | |
1869 (math-div (math-sum-rec (math-div (nth 1 expr) (cdr t1)) | |
1870 var low high) | |
1871 (car t1))) | |
1872 ((eq (car expr) 'neg) | |
1873 (math-neg (math-sum-rec (nth 1 expr) var low high))) | |
1874 ((and (eq (car expr) '^) | |
1875 (not (math-expr-contains (nth 1 expr) var)) | |
1876 (setq t1 (math-is-polynomial (nth 2 expr) var 1))) | |
1877 (let ((x (math-pow (nth 1 expr) (nth 1 t1)))) | |
1878 (math-div (math-mul (math-sub (math-pow x (math-add 1 high)) | |
1879 (math-pow x low)) | |
1880 (math-pow (nth 1 expr) (car t1))) | |
1881 (math-sub x 1)))) | |
1882 ((and (setq t1 (math-to-exponentials expr)) | |
1883 (setq t1 (math-sum-rec t1 var low high)) | |
1884 (not (math-expr-calls t1 '(calcFunc-sum)))) | |
1885 (math-to-exps t1)) | |
1886 ((memq (car expr) '(calcFunc-ln calcFunc-log10)) | |
1887 (list (car expr) (calcFunc-prod (nth 1 expr) var low high))) | |
1888 ((and (eq (car expr) 'calcFunc-log) | |
1889 (= (length expr) 3) | |
1890 (not (math-expr-contains (nth 2 expr) var))) | |
1891 (list 'calcFunc-log | |
1892 (calcFunc-prod (nth 1 expr) var low high) | |
1893 (nth 2 expr))))) | |
1894 (if (equal val '(var nan var-nan)) (setq val nil)) | |
1895 (or val | |
1896 (let* ((math-tabulate-initial 0) | |
1897 (math-tabulate-function 'calcFunc-sum)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1898 (calcFunc-table expr var low high))))) |
40785 | 1899 |
1900 (defun calcFunc-asum (expr var low &optional high step no-mul-flag) | |
1901 (or high (setq high low low 1)) | |
1902 (if (and step (not (math-equal-int step 1))) | |
1903 (if (math-negp step) | |
1904 (math-mul (math-pow -1 low) | |
1905 (calcFunc-asum expr var high low (math-neg step) t)) | |
1906 (let ((lo (math-simplify (math-div low step)))) | |
1907 (if (math-num-integerp lo) | |
1908 (calcFunc-asum (math-normalize | |
1909 (math-expr-subst expr var | |
1910 (math-mul step var))) | |
1911 var lo (math-simplify (math-div high step))) | |
1912 (calcFunc-asum (math-normalize | |
1913 (math-expr-subst expr var | |
1914 (math-add (math-mul step var) | |
1915 low))) | |
1916 var 0 | |
1917 (math-simplify (math-div (math-sub high low) | |
1918 step)))))) | |
1919 (math-mul (if no-mul-flag 1 (math-pow -1 low)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1920 (calcFunc-sum (math-mul (math-pow -1 var) expr) var low high)))) |
40785 | 1921 |
1922 (defun math-sum-const-factors (expr var) | |
1923 (let ((const nil) | |
1924 (not-const nil) | |
1925 (p expr)) | |
1926 (while (eq (car-safe p) '*) | |
1927 (if (math-expr-contains (nth 1 p) var) | |
1928 (setq not-const (cons (nth 1 p) not-const)) | |
1929 (setq const (cons (nth 1 p) const))) | |
1930 (setq p (nth 2 p))) | |
1931 (if (math-expr-contains p var) | |
1932 (setq not-const (cons p not-const)) | |
1933 (setq const (cons p const))) | |
1934 (and const | |
1935 (cons (let ((temp (car const))) | |
1936 (while (setq const (cdr const)) | |
1937 (setq temp (list '* (car const) temp))) | |
1938 temp) | |
1939 (let ((temp (or (car not-const) 1))) | |
1940 (while (setq not-const (cdr not-const)) | |
1941 (setq temp (list '* (car not-const) temp))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1942 temp))))) |
40785 | 1943 |
1944 ;; Following is from CRC Math Tables, 27th ed, pp. 52-53. | |
1945 (defun math-sum-integer-power (pow) | |
1946 (let ((calc-prefer-frac t) | |
1947 (n (length math-sum-int-pow-cache))) | |
1948 (while (<= n pow) | |
1949 (let* ((new (list 0 0)) | |
1950 (lin new) | |
1951 (pp (cdr (nth (1- n) math-sum-int-pow-cache))) | |
1952 (p 2) | |
1953 (sum 0) | |
1954 q) | |
1955 (while pp | |
1956 (setq q (math-div (car pp) p) | |
1957 new (cons (math-mul q n) new) | |
1958 sum (math-add sum q) | |
1959 p (1+ p) | |
1960 pp (cdr pp))) | |
1961 (setcar lin (math-sub 1 (math-mul n sum))) | |
1962 (setq math-sum-int-pow-cache | |
1963 (nconc math-sum-int-pow-cache (list (nreverse new))) | |
1964 n (1+ n)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1965 (nth pow math-sum-int-pow-cache))) |
40785 | 1966 (setq math-sum-int-pow-cache (list '(0 1))) |
1967 | |
1968 (defun math-to-exponentials (expr) | |
1969 (and (consp expr) | |
1970 (= (length expr) 2) | |
1971 (let ((x (nth 1 expr)) | |
1972 (pi (if calc-symbolic-mode '(var pi var-pi) (math-pi))) | |
1973 (i (if calc-symbolic-mode '(var i var-i) '(cplx 0 1)))) | |
1974 (cond ((eq (car expr) 'calcFunc-exp) | |
1975 (list '^ '(var e var-e) x)) | |
1976 ((eq (car expr) 'calcFunc-sin) | |
1977 (or (eq calc-angle-mode 'rad) | |
1978 (setq x (list '/ (list '* x pi) 180))) | |
1979 (list '/ (list '- | |
1980 (list '^ '(var e var-e) (list '* x i)) | |
1981 (list '^ '(var e var-e) | |
1982 (list 'neg (list '* x i)))) | |
1983 (list '* 2 i))) | |
1984 ((eq (car expr) 'calcFunc-cos) | |
1985 (or (eq calc-angle-mode 'rad) | |
1986 (setq x (list '/ (list '* x pi) 180))) | |
1987 (list '/ (list '+ | |
1988 (list '^ '(var e var-e) | |
1989 (list '* x i)) | |
1990 (list '^ '(var e var-e) | |
1991 (list 'neg (list '* x i)))) | |
1992 2)) | |
1993 ((eq (car expr) 'calcFunc-sinh) | |
1994 (list '/ (list '- | |
1995 (list '^ '(var e var-e) x) | |
1996 (list '^ '(var e var-e) (list 'neg x))) | |
1997 2)) | |
1998 ((eq (car expr) 'calcFunc-cosh) | |
1999 (list '/ (list '+ | |
2000 (list '^ '(var e var-e) x) | |
2001 (list '^ '(var e var-e) (list 'neg x))) | |
2002 2)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2003 (t nil))))) |
40785 | 2004 |
2005 (defun math-to-exps (expr) | |
2006 (cond (calc-symbolic-mode expr) | |
2007 ((Math-primp expr) | |
2008 (if (equal expr '(var e var-e)) (math-e) expr)) | |
2009 ((and (eq (car expr) '^) | |
2010 (equal (nth 1 expr) '(var e var-e))) | |
2011 (list 'calcFunc-exp (nth 2 expr))) | |
2012 (t | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2013 (cons (car expr) (mapcar 'math-to-exps (cdr expr)))))) |
40785 | 2014 |
2015 | |
2016 (defun calcFunc-prod (expr var &optional low high step) | |
2017 (if math-disable-prods (math-reject-arg)) | |
2018 (let* ((res (let* ((calc-internal-prec (+ calc-internal-prec 2))) | |
2019 (math-prod-rec expr var low high step))) | |
2020 (math-disable-prods t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2021 (math-normalize res))) |
40785 | 2022 (setq math-disable-prods nil) |
2023 | |
2024 (defun math-prod-rec (expr var &optional low high step) | |
2025 (or low (setq low '(neg (var inf var-inf)) high '(var inf var-inf))) | |
2026 (and low (not high) (setq high '(var inf var-inf))) | |
2027 (let (t1 t2 t3 val) | |
2028 (setq val | |
2029 (cond | |
2030 ((not (math-expr-contains expr var)) | |
2031 (math-pow expr (math-add (math-div (math-sub high low) (or step 1)) | |
2032 1))) | |
2033 ((and step (not (math-equal-int step 1))) | |
2034 (if (math-negp step) | |
2035 (math-prod-rec expr var high low (math-neg step)) | |
2036 (let ((lo (math-simplify (math-div low step)))) | |
2037 (if (math-known-num-integerp lo) | |
2038 (math-prod-rec (math-normalize | |
2039 (math-expr-subst expr var | |
2040 (math-mul step var))) | |
2041 var lo (math-simplify (math-div high step))) | |
2042 (math-prod-rec (math-normalize | |
2043 (math-expr-subst expr var | |
2044 (math-add (math-mul step | |
2045 var) | |
2046 low))) | |
2047 var 0 | |
2048 (math-simplify (math-div (math-sub high low) | |
2049 step))))))) | |
2050 ((and (memq (car expr) '(* /)) | |
2051 (setq t1 (math-prod-rec (nth 1 expr) var low high) | |
2052 t2 (math-prod-rec (nth 2 expr) var low high)) | |
2053 (not (and (math-expr-calls t1 '(calcFunc-prod)) | |
2054 (math-expr-calls t2 '(calcFunc-prod))))) | |
2055 (list (car expr) t1 t2)) | |
2056 ((and (eq (car expr) '^) | |
2057 (not (math-expr-contains (nth 2 expr) var))) | |
2058 (math-pow (math-prod-rec (nth 1 expr) var low high) | |
2059 (nth 2 expr))) | |
2060 ((and (eq (car expr) '^) | |
2061 (not (math-expr-contains (nth 1 expr) var))) | |
2062 (math-pow (nth 1 expr) | |
2063 (calcFunc-sum (nth 2 expr) var low high))) | |
2064 ((eq (car expr) 'sqrt) | |
2065 (math-normalize (list 'calcFunc-sqrt | |
2066 (list 'calcFunc-prod (nth 1 expr) | |
2067 var low high)))) | |
2068 ((eq (car expr) 'neg) | |
2069 (math-mul (math-pow -1 (math-add (math-sub high low) 1)) | |
2070 (math-prod-rec (nth 1 expr) var low high))) | |
2071 ((eq (car expr) 'calcFunc-exp) | |
2072 (list 'calcFunc-exp (calcFunc-sum (nth 1 expr) var low high))) | |
2073 ((and (setq t1 (math-is-polynomial expr var 1)) | |
2074 (setq t2 | |
2075 (cond | |
2076 ((or (and (math-equal-int (nth 1 t1) 1) | |
2077 (setq low (math-simplify | |
2078 (math-add low (car t1))) | |
2079 high (math-simplify | |
2080 (math-add high (car t1))))) | |
2081 (and (math-equal-int (nth 1 t1) -1) | |
2082 (setq t2 low | |
2083 low (math-simplify | |
2084 (math-sub (car t1) high)) | |
2085 high (math-simplify | |
2086 (math-sub (car t1) t2))))) | |
2087 (if (or (math-zerop low) (math-zerop high)) | |
2088 0 | |
2089 (if (and (or (math-negp low) (math-negp high)) | |
2090 (or (math-num-integerp low) | |
2091 (math-num-integerp high))) | |
2092 (if (math-posp high) | |
2093 0 | |
2094 (math-mul (math-pow -1 | |
2095 (math-add | |
2096 (math-add low high) 1)) | |
2097 (list '/ | |
2098 (list 'calcFunc-fact | |
2099 (math-neg low)) | |
2100 (list 'calcFunc-fact | |
2101 (math-sub -1 high))))) | |
2102 (list '/ | |
2103 (list 'calcFunc-fact high) | |
2104 (list 'calcFunc-fact (math-sub low 1)))))) | |
2105 ((and (or (and (math-equal-int (nth 1 t1) 2) | |
2106 (setq t2 (math-simplify | |
2107 (math-add (math-mul low 2) | |
2108 (car t1))) | |
2109 t3 (math-simplify | |
2110 (math-add (math-mul high 2) | |
2111 (car t1))))) | |
2112 (and (math-equal-int (nth 1 t1) -2) | |
2113 (setq t2 (math-simplify | |
2114 (math-sub (car t1) | |
2115 (math-mul high 2))) | |
2116 t3 (math-simplify | |
2117 (math-sub (car t1) | |
2118 (math-mul low | |
2119 2)))))) | |
2120 (or (math-integerp t2) | |
2121 (and (math-messy-integerp t2) | |
2122 (setq t2 (math-trunc t2))) | |
2123 (math-integerp t3) | |
2124 (and (math-messy-integerp t3) | |
2125 (setq t3 (math-trunc t3))))) | |
2126 (if (or (math-zerop t2) (math-zerop t3)) | |
2127 0 | |
2128 (if (or (math-evenp t2) (math-evenp t3)) | |
2129 (if (or (math-negp t2) (math-negp t3)) | |
2130 (if (math-posp high) | |
2131 0 | |
2132 (list '/ | |
2133 (list 'calcFunc-dfact | |
2134 (math-neg t2)) | |
2135 (list 'calcFunc-dfact | |
2136 (math-sub -2 t3)))) | |
2137 (list '/ | |
2138 (list 'calcFunc-dfact t3) | |
2139 (list 'calcFunc-dfact | |
2140 (math-sub t2 2)))) | |
2141 (if (math-negp t3) | |
2142 (list '* | |
2143 (list '^ -1 | |
2144 (list '/ (list '- (list '- t2 t3) | |
2145 2) | |
2146 2)) | |
2147 (list '/ | |
2148 (list 'calcFunc-dfact | |
2149 (math-neg t2)) | |
2150 (list 'calcFunc-dfact | |
2151 (math-sub -2 t3)))) | |
2152 (if (math-posp t2) | |
2153 (list '/ | |
2154 (list 'calcFunc-dfact t3) | |
2155 (list 'calcFunc-dfact | |
2156 (math-sub t2 2))) | |
2157 nil)))))))) | |
2158 t2))) | |
2159 (if (equal val '(var nan var-nan)) (setq val nil)) | |
2160 (or val | |
2161 (let* ((math-tabulate-initial 1) | |
2162 (math-tabulate-function 'calcFunc-prod)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2163 (calcFunc-table expr var low high))))) |
40785 | 2164 |
2165 | |
2166 | |
2167 | |
2168 ;;; Attempt to reduce lhs = rhs to solve-var = rhs', where solve-var appears | |
2169 ;;; in lhs but not in rhs or rhs'; return rhs'. | |
2170 ;;; Uses global values: solve-*. | |
2171 (defun math-try-solve-for (lhs rhs &optional sign no-poly) | |
2172 (let (t1 t2 t3) | |
2173 (cond ((equal lhs solve-var) | |
2174 (setq math-solve-sign sign) | |
2175 (if (eq solve-full 'all) | |
2176 (let ((vec (list 'vec (math-evaluate-expr rhs))) | |
2177 newvec var p) | |
2178 (while math-solve-ranges | |
2179 (setq p (car math-solve-ranges) | |
2180 var (car p) | |
2181 newvec (list 'vec)) | |
2182 (while (setq p (cdr p)) | |
2183 (setq newvec (nconc newvec | |
2184 (cdr (math-expr-subst | |
2185 vec var (car p)))))) | |
2186 (setq vec newvec | |
2187 math-solve-ranges (cdr math-solve-ranges))) | |
2188 (math-normalize vec)) | |
2189 rhs)) | |
2190 ((Math-primp lhs) | |
2191 nil) | |
2192 ((and (eq (car lhs) '-) | |
2193 (eq (car-safe (nth 1 lhs)) (car-safe (nth 2 lhs))) | |
2194 (Math-zerop rhs) | |
2195 (= (length (nth 1 lhs)) 2) | |
2196 (= (length (nth 2 lhs)) 2) | |
2197 (setq t1 (get (car (nth 1 lhs)) 'math-inverse)) | |
2198 (setq t2 (funcall t1 '(var SOLVEDUM SOLVEDUM))) | |
2199 (eq (math-expr-contains-count t2 '(var SOLVEDUM SOLVEDUM)) 1) | |
2200 (setq t3 (math-solve-above-dummy t2)) | |
2201 (setq t1 (math-try-solve-for (math-sub (nth 1 (nth 1 lhs)) | |
2202 (math-expr-subst | |
2203 t2 t3 | |
2204 (nth 1 (nth 2 lhs)))) | |
2205 0))) | |
2206 t1) | |
2207 ((eq (car lhs) 'neg) | |
2208 (math-try-solve-for (nth 1 lhs) (math-neg rhs) | |
2209 (and sign (- sign)))) | |
2210 ((and (not (eq solve-full 't)) (math-try-solve-prod))) | |
2211 ((and (not no-poly) | |
2212 (setq t2 (math-decompose-poly lhs solve-var 15 rhs))) | |
2213 (setq t1 (cdr (nth 1 t2)) | |
2214 t1 (let ((math-solve-ranges math-solve-ranges)) | |
2215 (cond ((= (length t1) 5) | |
2216 (apply 'math-solve-quartic (car t2) t1)) | |
2217 ((= (length t1) 4) | |
2218 (apply 'math-solve-cubic (car t2) t1)) | |
2219 ((= (length t1) 3) | |
2220 (apply 'math-solve-quadratic (car t2) t1)) | |
2221 ((= (length t1) 2) | |
2222 (apply 'math-solve-linear (car t2) sign t1)) | |
2223 (solve-full | |
2224 (math-poly-all-roots (car t2) t1)) | |
2225 (calc-symbolic-mode nil) | |
2226 (t | |
2227 (math-try-solve-for | |
2228 (car t2) | |
2229 (math-poly-any-root (reverse t1) 0 t) | |
2230 nil t))))) | |
2231 (if t1 | |
2232 (if (eq (nth 2 t2) 1) | |
2233 t1 | |
2234 (math-solve-prod t1 (math-try-solve-for (nth 2 t2) 0 nil t))) | |
2235 (calc-record-why "*Unable to find a symbolic solution") | |
2236 nil)) | |
2237 ((and (math-solve-find-root-term lhs nil) | |
2238 (eq (math-expr-contains-count lhs t1) 1)) ; just in case | |
2239 (math-try-solve-for (math-simplify | |
2240 (math-sub (if (or t3 (math-evenp t2)) | |
2241 (math-pow t1 t2) | |
2242 (math-neg (math-pow t1 t2))) | |
2243 (math-expand-power | |
2244 (math-sub (math-normalize | |
2245 (math-expr-subst | |
2246 lhs t1 0)) | |
2247 rhs) | |
2248 t2 solve-var))) | |
2249 0)) | |
2250 ((eq (car lhs) '+) | |
2251 (cond ((not (math-expr-contains (nth 1 lhs) solve-var)) | |
2252 (math-try-solve-for (nth 2 lhs) | |
2253 (math-sub rhs (nth 1 lhs)) | |
2254 sign)) | |
2255 ((not (math-expr-contains (nth 2 lhs) solve-var)) | |
2256 (math-try-solve-for (nth 1 lhs) | |
2257 (math-sub rhs (nth 2 lhs)) | |
2258 sign)))) | |
2259 ((eq (car lhs) 'calcFunc-eq) | |
2260 (math-try-solve-for (math-sub (nth 1 lhs) (nth 2 lhs)) | |
2261 rhs sign no-poly)) | |
2262 ((eq (car lhs) '-) | |
2263 (cond ((or (and (eq (car-safe (nth 1 lhs)) 'calcFunc-sin) | |
2264 (eq (car-safe (nth 2 lhs)) 'calcFunc-cos)) | |
2265 (and (eq (car-safe (nth 1 lhs)) 'calcFunc-cos) | |
2266 (eq (car-safe (nth 2 lhs)) 'calcFunc-sin))) | |
2267 (math-try-solve-for (math-sub (nth 1 lhs) | |
2268 (list (car (nth 1 lhs)) | |
2269 (math-sub | |
2270 (math-quarter-circle t) | |
2271 (nth 1 (nth 2 lhs))))) | |
2272 rhs)) | |
2273 ((not (math-expr-contains (nth 1 lhs) solve-var)) | |
2274 (math-try-solve-for (nth 2 lhs) | |
2275 (math-sub (nth 1 lhs) rhs) | |
2276 (and sign (- sign)))) | |
2277 ((not (math-expr-contains (nth 2 lhs) solve-var)) | |
2278 (math-try-solve-for (nth 1 lhs) | |
2279 (math-add rhs (nth 2 lhs)) | |
2280 sign)))) | |
2281 ((and (eq solve-full 't) (math-try-solve-prod))) | |
2282 ((and (eq (car lhs) '%) | |
2283 (not (math-expr-contains (nth 2 lhs) solve-var))) | |
2284 (math-try-solve-for (nth 1 lhs) (math-add rhs | |
2285 (math-solve-get-int | |
2286 (nth 2 lhs))))) | |
2287 ((eq (car lhs) 'calcFunc-log) | |
2288 (cond ((not (math-expr-contains (nth 2 lhs) solve-var)) | |
2289 (math-try-solve-for (nth 1 lhs) (math-pow (nth 2 lhs) rhs))) | |
2290 ((not (math-expr-contains (nth 1 lhs) solve-var)) | |
2291 (math-try-solve-for (nth 2 lhs) (math-pow | |
2292 (nth 1 lhs) | |
2293 (math-div 1 rhs)))))) | |
2294 ((and (= (length lhs) 2) | |
2295 (symbolp (car lhs)) | |
2296 (setq t1 (get (car lhs) 'math-inverse)) | |
2297 (setq t2 (funcall t1 rhs))) | |
2298 (setq t1 (get (car lhs) 'math-inverse-sign)) | |
2299 (math-try-solve-for (nth 1 lhs) (math-normalize t2) | |
2300 (and sign t1 | |
2301 (if (integerp t1) | |
2302 (* t1 sign) | |
2303 (funcall t1 lhs sign))))) | |
2304 ((and (symbolp (car lhs)) | |
2305 (setq t1 (get (car lhs) 'math-inverse-n)) | |
2306 (setq t2 (funcall t1 lhs rhs))) | |
2307 t2) | |
2308 ((setq t1 (math-expand-formula lhs)) | |
2309 (math-try-solve-for t1 rhs sign)) | |
2310 (t | |
2311 (calc-record-why "*No inverse known" lhs) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2312 nil)))) |
40785 | 2313 |
2314 (setq math-solve-ranges nil) | |
2315 | |
2316 (defun math-try-solve-prod () | |
2317 (cond ((eq (car lhs) '*) | |
2318 (cond ((not (math-expr-contains (nth 1 lhs) solve-var)) | |
2319 (math-try-solve-for (nth 2 lhs) | |
2320 (math-div rhs (nth 1 lhs)) | |
2321 (math-solve-sign sign (nth 1 lhs)))) | |
2322 ((not (math-expr-contains (nth 2 lhs) solve-var)) | |
2323 (math-try-solve-for (nth 1 lhs) | |
2324 (math-div rhs (nth 2 lhs)) | |
2325 (math-solve-sign sign (nth 2 lhs)))) | |
2326 ((Math-zerop rhs) | |
2327 (math-solve-prod (let ((math-solve-ranges math-solve-ranges)) | |
2328 (math-try-solve-for (nth 2 lhs) 0)) | |
2329 (math-try-solve-for (nth 1 lhs) 0))))) | |
2330 ((eq (car lhs) '/) | |
2331 (cond ((not (math-expr-contains (nth 1 lhs) solve-var)) | |
2332 (math-try-solve-for (nth 2 lhs) | |
2333 (math-div (nth 1 lhs) rhs) | |
2334 (math-solve-sign sign (nth 1 lhs)))) | |
2335 ((not (math-expr-contains (nth 2 lhs) solve-var)) | |
2336 (math-try-solve-for (nth 1 lhs) | |
2337 (math-mul rhs (nth 2 lhs)) | |
2338 (math-solve-sign sign (nth 2 lhs)))) | |
2339 ((setq t1 (math-try-solve-for (math-sub (nth 1 lhs) | |
2340 (math-mul (nth 2 lhs) | |
2341 rhs)) | |
2342 0)) | |
2343 t1))) | |
2344 ((eq (car lhs) '^) | |
2345 (cond ((not (math-expr-contains (nth 1 lhs) solve-var)) | |
2346 (math-try-solve-for | |
2347 (nth 2 lhs) | |
2348 (math-add (math-normalize | |
2349 (list 'calcFunc-log rhs (nth 1 lhs))) | |
2350 (math-div | |
2351 (math-mul 2 | |
2352 (math-mul '(var pi var-pi) | |
2353 (math-solve-get-int | |
2354 '(var i var-i)))) | |
2355 (math-normalize | |
2356 (list 'calcFunc-ln (nth 1 lhs))))))) | |
2357 ((not (math-expr-contains (nth 2 lhs) solve-var)) | |
2358 (cond ((and (integerp (nth 2 lhs)) | |
2359 (>= (nth 2 lhs) 2) | |
2360 (setq t1 (math-integer-log2 (nth 2 lhs)))) | |
2361 (setq t2 rhs) | |
2362 (if (and (eq solve-full t) | |
2363 (math-known-realp (nth 1 lhs))) | |
2364 (progn | |
2365 (while (>= (setq t1 (1- t1)) 0) | |
2366 (setq t2 (list 'calcFunc-sqrt t2))) | |
2367 (setq t2 (math-solve-get-sign t2))) | |
2368 (while (>= (setq t1 (1- t1)) 0) | |
2369 (setq t2 (math-solve-get-sign | |
2370 (math-normalize | |
2371 (list 'calcFunc-sqrt t2)))))) | |
2372 (math-try-solve-for | |
2373 (nth 1 lhs) | |
2374 (math-normalize t2))) | |
2375 ((math-looks-negp (nth 2 lhs)) | |
2376 (math-try-solve-for | |
2377 (list '^ (nth 1 lhs) (math-neg (nth 2 lhs))) | |
2378 (math-div 1 rhs))) | |
2379 ((and (eq solve-full t) | |
2380 (Math-integerp (nth 2 lhs)) | |
2381 (math-known-realp (nth 1 lhs))) | |
2382 (setq t1 (math-normalize | |
2383 (list 'calcFunc-nroot rhs (nth 2 lhs)))) | |
2384 (if (math-evenp (nth 2 lhs)) | |
2385 (setq t1 (math-solve-get-sign t1))) | |
2386 (math-try-solve-for | |
2387 (nth 1 lhs) t1 | |
2388 (and sign | |
2389 (math-oddp (nth 2 lhs)) | |
2390 (math-solve-sign sign (nth 2 lhs))))) | |
2391 (t (math-try-solve-for | |
2392 (nth 1 lhs) | |
2393 (math-mul | |
2394 (math-normalize | |
2395 (list 'calcFunc-exp | |
2396 (if (Math-realp (nth 2 lhs)) | |
2397 (math-div (math-mul | |
2398 '(var pi var-pi) | |
2399 (math-solve-get-int | |
2400 '(var i var-i) | |
2401 (and (integerp (nth 2 lhs)) | |
2402 (math-abs | |
2403 (nth 2 lhs))))) | |
2404 (math-div (nth 2 lhs) 2)) | |
2405 (math-div (math-mul | |
2406 2 | |
2407 (math-mul | |
2408 '(var pi var-pi) | |
2409 (math-solve-get-int | |
2410 '(var i var-i) | |
2411 (and (integerp (nth 2 lhs)) | |
2412 (math-abs | |
2413 (nth 2 lhs)))))) | |
2414 (nth 2 lhs))))) | |
2415 (math-normalize | |
2416 (list 'calcFunc-nroot | |
2417 rhs | |
2418 (nth 2 lhs)))) | |
2419 (and sign | |
2420 (math-oddp (nth 2 lhs)) | |
2421 (math-solve-sign sign (nth 2 lhs))))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2422 (t nil))) |
40785 | 2423 |
2424 (defun math-solve-prod (lsoln rsoln) | |
2425 (cond ((null lsoln) | |
2426 rsoln) | |
2427 ((null rsoln) | |
2428 lsoln) | |
2429 ((eq solve-full 'all) | |
2430 (cons 'vec (append (cdr lsoln) (cdr rsoln)))) | |
2431 (solve-full | |
2432 (list 'calcFunc-if | |
2433 (list 'calcFunc-gt (math-solve-get-sign 1) 0) | |
2434 lsoln | |
2435 rsoln)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2436 (t lsoln))) |
40785 | 2437 |
2438 ;;; This deals with negative, fractional, and symbolic powers of "x". | |
2439 (defun math-solve-poly-funny-powers (sub-rhs) ; uses "t1", "t2" | |
2440 (setq t1 lhs) | |
2441 (let ((pp math-poly-neg-powers) | |
2442 fac) | |
2443 (while pp | |
2444 (setq fac (math-pow (car pp) (or math-poly-mult-powers 1)) | |
2445 t1 (math-mul t1 fac) | |
2446 rhs (math-mul rhs fac) | |
2447 pp (cdr pp)))) | |
2448 (if sub-rhs (setq t1 (math-sub t1 rhs))) | |
2449 (let ((math-poly-neg-powers nil)) | |
2450 (setq t2 (math-mul (or math-poly-mult-powers 1) | |
2451 (let ((calc-prefer-frac t)) | |
2452 (math-div 1 math-poly-frac-powers))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2453 t1 (math-is-polynomial (math-simplify (calcFunc-expand t1)) b 50)))) |
40785 | 2454 |
2455 ;;; This converts "a x^8 + b x^5 + c x^2" to "(a (x^3)^2 + b (x^3) + c) * x^2". | |
2456 (defun math-solve-crunch-poly (max-degree) ; uses "t1", "t3" | |
2457 (let ((count 0)) | |
2458 (while (and t1 (Math-zerop (car t1))) | |
2459 (setq t1 (cdr t1) | |
2460 count (1+ count))) | |
2461 (and t1 | |
2462 (let* ((degree (1- (length t1))) | |
2463 (scale degree)) | |
2464 (while (and (> scale 1) (= (car t3) 1)) | |
2465 (and (= (% degree scale) 0) | |
2466 (let ((p t1) | |
2467 (n 0) | |
2468 (new-t1 nil) | |
2469 (okay t)) | |
2470 (while (and p okay) | |
2471 (if (= (% n scale) 0) | |
2472 (setq new-t1 (nconc new-t1 (list (car p)))) | |
2473 (or (Math-zerop (car p)) | |
2474 (setq okay nil))) | |
2475 (setq p (cdr p) | |
2476 n (1+ n))) | |
2477 (if okay | |
2478 (setq t3 (cons scale (cdr t3)) | |
2479 t1 new-t1)))) | |
2480 (setq scale (1- scale))) | |
2481 (setq t3 (list (math-mul (car t3) t2) (math-mul count t2))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2482 (<= (1- (length t1)) max-degree))))) |
40785 | 2483 |
2484 (defun calcFunc-poly (expr var &optional degree) | |
2485 (if degree | |
2486 (or (natnump degree) (math-reject-arg degree 'fixnatnump)) | |
2487 (setq degree 50)) | |
2488 (let ((p (math-is-polynomial expr var degree 'gen))) | |
2489 (if p | |
2490 (if (equal p '(0)) | |
2491 (list 'vec) | |
2492 (cons 'vec p)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2493 (math-reject-arg expr "Expected a polynomial")))) |
40785 | 2494 |
2495 (defun calcFunc-gpoly (expr var &optional degree) | |
2496 (if degree | |
2497 (or (natnump degree) (math-reject-arg degree 'fixnatnump)) | |
2498 (setq degree 50)) | |
2499 (let* ((math-poly-base-variable var) | |
2500 (d (math-decompose-poly expr var degree nil))) | |
2501 (if d | |
2502 (cons 'vec d) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2503 (math-reject-arg expr "Expected a polynomial")))) |
40785 | 2504 |
2505 (defun math-decompose-poly (lhs solve-var degree sub-rhs) | |
2506 (let ((rhs (or sub-rhs 1)) | |
2507 t1 t2 t3) | |
2508 (setq t2 (math-polynomial-base | |
2509 lhs | |
2510 (function | |
2511 (lambda (b) | |
2512 (let ((math-poly-neg-powers '(1)) | |
2513 (math-poly-mult-powers nil) | |
2514 (math-poly-frac-powers 1) | |
2515 (math-poly-exp-base t)) | |
2516 (and (not (equal b lhs)) | |
2517 (or (not (memq (car-safe b) '(+ -))) sub-rhs) | |
2518 (setq t3 '(1 0) t2 1 | |
2519 t1 (math-is-polynomial lhs b 50)) | |
2520 (if (and (equal math-poly-neg-powers '(1)) | |
2521 (memq math-poly-mult-powers '(nil 1)) | |
2522 (eq math-poly-frac-powers 1) | |
2523 sub-rhs) | |
2524 (setq t1 (cons (math-sub (car t1) rhs) | |
2525 (cdr t1))) | |
2526 (math-solve-poly-funny-powers sub-rhs)) | |
2527 (math-solve-crunch-poly degree) | |
2528 (or (math-expr-contains b solve-var) | |
2529 (math-expr-contains (car t3) solve-var)))))))) | |
2530 (if t2 | |
2531 (list (math-pow t2 (car t3)) | |
2532 (cons 'vec t1) | |
2533 (if sub-rhs | |
2534 (math-pow t2 (nth 1 t3)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2535 (math-div (math-pow t2 (nth 1 t3)) rhs)))))) |
40785 | 2536 |
2537 (defun math-solve-linear (var sign b a) | |
2538 (math-try-solve-for var | |
2539 (math-div (math-neg b) a) | |
2540 (math-solve-sign sign a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2541 t)) |
40785 | 2542 |
2543 (defun math-solve-quadratic (var c b a) | |
2544 (math-try-solve-for | |
2545 var | |
2546 (if (math-looks-evenp b) | |
2547 (let ((halfb (math-div b 2))) | |
2548 (math-div | |
2549 (math-add | |
2550 (math-neg halfb) | |
2551 (math-solve-get-sign | |
2552 (math-normalize | |
2553 (list 'calcFunc-sqrt | |
2554 (math-add (math-sqr halfb) | |
2555 (math-mul (math-neg c) a)))))) | |
2556 a)) | |
2557 (math-div | |
2558 (math-add | |
2559 (math-neg b) | |
2560 (math-solve-get-sign | |
2561 (math-normalize | |
2562 (list 'calcFunc-sqrt | |
2563 (math-add (math-sqr b) | |
2564 (math-mul 4 (math-mul (math-neg c) a))))))) | |
2565 (math-mul 2 a))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2566 nil t)) |
40785 | 2567 |
2568 (defun math-solve-cubic (var d c b a) | |
2569 (let* ((p (math-div b a)) | |
2570 (q (math-div c a)) | |
2571 (r (math-div d a)) | |
2572 (psqr (math-sqr p)) | |
2573 (aa (math-sub q (math-div psqr 3))) | |
2574 (bb (math-add r | |
2575 (math-div (math-sub (math-mul 2 (math-mul psqr p)) | |
2576 (math-mul 9 (math-mul p q))) | |
2577 27))) | |
2578 m) | |
2579 (if (Math-zerop aa) | |
2580 (math-try-solve-for (math-pow (math-add var (math-div p 3)) 3) | |
2581 (math-neg bb) nil t) | |
2582 (if (Math-zerop bb) | |
2583 (math-try-solve-for | |
2584 (math-mul (math-add var (math-div p 3)) | |
2585 (math-add (math-sqr (math-add var (math-div p 3))) | |
2586 aa)) | |
2587 0 nil t) | |
2588 (setq m (math-mul 2 (list 'calcFunc-sqrt (math-div aa -3)))) | |
2589 (math-try-solve-for | |
2590 var | |
2591 (math-sub | |
2592 (math-normalize | |
2593 (math-mul | |
2594 m | |
2595 (list 'calcFunc-cos | |
2596 (math-div | |
2597 (math-sub (list 'calcFunc-arccos | |
2598 (math-div (math-mul 3 bb) | |
2599 (math-mul aa m))) | |
2600 (math-mul 2 | |
2601 (math-mul | |
2602 (math-add 1 (math-solve-get-int | |
2603 1 3)) | |
2604 (math-half-circle | |
2605 calc-symbolic-mode)))) | |
2606 3)))) | |
2607 (math-div p 3)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2608 nil t))))) |
40785 | 2609 |
2610 (defun math-solve-quartic (var d c b a aa) | |
2611 (setq a (math-div a aa)) | |
2612 (setq b (math-div b aa)) | |
2613 (setq c (math-div c aa)) | |
2614 (setq d (math-div d aa)) | |
2615 (math-try-solve-for | |
2616 var | |
2617 (let* ((asqr (math-sqr a)) | |
2618 (asqr4 (math-div asqr 4)) | |
2619 (y (let ((solve-full nil) | |
2620 calc-next-why) | |
2621 (math-solve-cubic solve-var | |
2622 (math-sub (math-sub | |
2623 (math-mul 4 (math-mul b d)) | |
2624 (math-mul asqr d)) | |
2625 (math-sqr c)) | |
2626 (math-sub (math-mul a c) | |
2627 (math-mul 4 d)) | |
2628 (math-neg b) | |
2629 1))) | |
2630 (rsqr (math-add (math-sub asqr4 b) y)) | |
2631 (r (list 'calcFunc-sqrt rsqr)) | |
2632 (sign1 (math-solve-get-sign 1)) | |
2633 (de (list 'calcFunc-sqrt | |
2634 (math-add | |
2635 (math-sub (math-mul 3 asqr4) | |
2636 (math-mul 2 b)) | |
2637 (if (Math-zerop rsqr) | |
2638 (math-mul | |
2639 2 | |
2640 (math-mul sign1 | |
2641 (list 'calcFunc-sqrt | |
2642 (math-sub (math-sqr y) | |
2643 (math-mul 4 d))))) | |
2644 (math-sub | |
2645 (math-mul sign1 | |
2646 (math-div | |
2647 (math-sub (math-sub | |
2648 (math-mul 4 (math-mul a b)) | |
2649 (math-mul 8 c)) | |
2650 (math-mul asqr a)) | |
2651 (math-mul 4 r))) | |
2652 rsqr)))))) | |
2653 (math-normalize | |
2654 (math-sub (math-add (math-mul sign1 (math-div r 2)) | |
2655 (math-solve-get-sign (math-div de 2))) | |
2656 (math-div a 4)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2657 nil t)) |
40785 | 2658 |
2659 (defun math-poly-all-roots (var p &optional math-factoring) | |
2660 (catch 'ouch | |
2661 (let* ((math-symbolic-solve calc-symbolic-mode) | |
2662 (roots nil) | |
2663 (deg (1- (length p))) | |
2664 (orig-p (reverse p)) | |
2665 (math-int-coefs nil) | |
2666 (math-int-scale nil) | |
2667 (math-double-roots nil) | |
2668 (math-int-factors nil) | |
2669 (math-int-threshold nil) | |
2670 (pp p)) | |
2671 ;; If rational coefficients, look for exact rational factors. | |
2672 (while (and pp (Math-ratp (car pp))) | |
2673 (setq pp (cdr pp))) | |
2674 (if pp | |
2675 (if (or math-factoring math-symbolic-solve) | |
2676 (throw 'ouch nil)) | |
2677 (let ((lead (car orig-p)) | |
2678 (calc-prefer-frac t) | |
2679 (scale (apply 'math-lcm-denoms p))) | |
2680 (setq math-int-scale (math-abs (math-mul scale lead)) | |
2681 math-int-threshold (math-div '(float 5 -2) math-int-scale) | |
2682 math-int-coefs (cdr (math-div (cons 'vec orig-p) lead))))) | |
2683 (if (> deg 4) | |
2684 (let ((calc-prefer-frac nil) | |
2685 (calc-symbolic-mode nil) | |
2686 (pp p) | |
2687 (def-p (copy-sequence orig-p))) | |
2688 (while pp | |
2689 (if (Math-numberp (car pp)) | |
2690 (setq pp (cdr pp)) | |
2691 (throw 'ouch nil))) | |
2692 (while (> deg (if math-symbolic-solve 2 4)) | |
2693 (let* ((x (math-poly-any-root def-p '(float 0 0) nil)) | |
2694 b c pp) | |
2695 (if (and (eq (car-safe x) 'cplx) | |
2696 (math-nearly-zerop (nth 2 x) (nth 1 x))) | |
2697 (setq x (calcFunc-re x))) | |
2698 (or math-factoring | |
2699 (setq roots (cons x roots))) | |
2700 (or (math-numberp x) | |
2701 (setq x (math-evaluate-expr x))) | |
2702 (setq pp def-p | |
2703 b (car def-p)) | |
2704 (while (setq pp (cdr pp)) | |
2705 (setq c (car pp)) | |
2706 (setcar pp b) | |
2707 (setq b (math-add (math-mul x b) c))) | |
2708 (setq def-p (cdr def-p) | |
2709 deg (1- deg)))) | |
2710 (setq p (reverse def-p)))) | |
2711 (if (> deg 1) | |
2712 (let ((solve-var '(var DUMMY var-DUMMY)) | |
2713 (math-solve-sign nil) | |
2714 (math-solve-ranges nil) | |
2715 (solve-full 'all)) | |
2716 (if (= (length p) (length math-int-coefs)) | |
2717 (setq p (reverse math-int-coefs))) | |
2718 (setq roots (append (cdr (apply (cond ((= deg 2) | |
2719 'math-solve-quadratic) | |
2720 ((= deg 3) | |
2721 'math-solve-cubic) | |
2722 (t | |
2723 'math-solve-quartic)) | |
2724 solve-var p)) | |
2725 roots))) | |
2726 (if (> deg 0) | |
2727 (setq roots (cons (math-div (math-neg (car p)) (nth 1 p)) | |
2728 roots)))) | |
2729 (if math-factoring | |
2730 (progn | |
2731 (while roots | |
2732 (math-poly-integer-root (car roots)) | |
2733 (setq roots (cdr roots))) | |
2734 (list math-int-factors (nreverse math-int-coefs) math-int-scale)) | |
2735 (let ((vec nil) res) | |
2736 (while roots | |
2737 (let ((root (car roots)) | |
2738 (solve-full (and solve-full 'all))) | |
2739 (if (math-floatp root) | |
2740 (setq root (math-poly-any-root orig-p root t))) | |
2741 (setq vec (append vec | |
2742 (cdr (or (math-try-solve-for var root nil t) | |
2743 (throw 'ouch nil)))))) | |
2744 (setq roots (cdr roots))) | |
2745 (setq vec (cons 'vec (nreverse vec))) | |
2746 (if math-symbolic-solve | |
2747 (setq vec (math-normalize vec))) | |
2748 (if (eq solve-full t) | |
2749 (list 'calcFunc-subscr | |
2750 vec | |
2751 (math-solve-get-int 1 (1- (length orig-p)) 1)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2752 vec)))))) |
40785 | 2753 (setq math-symbolic-solve nil) |
2754 | |
2755 (defun math-lcm-denoms (&rest fracs) | |
2756 (let ((den 1)) | |
2757 (while fracs | |
2758 (if (eq (car-safe (car fracs)) 'frac) | |
2759 (setq den (calcFunc-lcm den (nth 2 (car fracs))))) | |
2760 (setq fracs (cdr fracs))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2761 den)) |
40785 | 2762 |
2763 (defun math-poly-any-root (p x polish) ; p is a reverse poly coeff list | |
2764 (let* ((newt (if (math-zerop x) | |
2765 (math-poly-newton-root | |
2766 p '(cplx (float 123 -6) (float 1 -4)) 4) | |
2767 (math-poly-newton-root p x 4))) | |
2768 (res (if (math-zerop (cdr newt)) | |
2769 (car newt) | |
2770 (if (and (math-lessp (cdr newt) '(float 1 -3)) (not polish)) | |
2771 (setq newt (math-poly-newton-root p (car newt) 30))) | |
2772 (if (math-zerop (cdr newt)) | |
2773 (car newt) | |
2774 (math-poly-laguerre-root p x polish))))) | |
2775 (and math-symbolic-solve (math-floatp res) | |
2776 (throw 'ouch nil)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2777 res)) |
40785 | 2778 |
2779 (defun math-poly-newton-root (p x iters) | |
2780 (let* ((calc-prefer-frac nil) | |
2781 (calc-symbolic-mode nil) | |
2782 (try-integer math-int-coefs) | |
2783 (dx x) b d) | |
2784 (while (and (> (setq iters (1- iters)) 0) | |
2785 (let ((pp p)) | |
2786 (math-working "newton" x) | |
2787 (setq b (car p) | |
2788 d 0) | |
2789 (while (setq pp (cdr pp)) | |
2790 (setq d (math-add (math-mul x d) b) | |
2791 b (math-add (math-mul x b) (car pp)))) | |
2792 (not (math-zerop d))) | |
2793 (progn | |
2794 (setq dx (math-div b d) | |
2795 x (math-sub x dx)) | |
2796 (if try-integer | |
2797 (let ((adx (math-abs-approx dx))) | |
2798 (and (math-lessp adx math-int-threshold) | |
2799 (let ((iroot (math-poly-integer-root x))) | |
2800 (if iroot | |
2801 (setq x iroot dx 0) | |
2802 (setq try-integer nil)))))) | |
2803 (or (not (or (eq dx 0) | |
2804 (math-nearly-zerop dx (math-abs-approx x)))) | |
2805 (progn (setq dx 0) nil))))) | |
2806 (cons x (if (math-zerop x) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2807 1 (math-div (math-abs-approx dx) (math-abs-approx x)))))) |
40785 | 2808 |
2809 (defun math-poly-integer-root (x) | |
2810 (and (math-lessp (calcFunc-xpon (math-abs-approx x)) calc-internal-prec) | |
2811 math-int-coefs | |
2812 (let* ((calc-prefer-frac t) | |
2813 (xre (calcFunc-re x)) | |
2814 (xim (calcFunc-im x)) | |
2815 (xresq (math-sqr xre)) | |
2816 (ximsq (math-sqr xim))) | |
2817 (if (math-lessp ximsq (calcFunc-scf xresq -1)) | |
2818 ;; Look for linear factor | |
2819 (let* ((rnd (math-div (math-round (math-mul xre math-int-scale)) | |
2820 math-int-scale)) | |
2821 (icp math-int-coefs) | |
2822 (rem (car icp)) | |
2823 (newcoef nil)) | |
2824 (while (setq icp (cdr icp)) | |
2825 (setq newcoef (cons rem newcoef) | |
2826 rem (math-add (car icp) | |
2827 (math-mul rem rnd)))) | |
2828 (and (math-zerop rem) | |
2829 (progn | |
2830 (setq math-int-coefs (nreverse newcoef) | |
2831 math-int-factors (cons (list (math-neg rnd)) | |
2832 math-int-factors)) | |
2833 rnd))) | |
2834 ;; Look for irreducible quadratic factor | |
2835 (let* ((rnd1 (math-div (math-round | |
2836 (math-mul xre (math-mul -2 math-int-scale))) | |
2837 math-int-scale)) | |
2838 (sqscale (math-sqr math-int-scale)) | |
2839 (rnd0 (math-div (math-round (math-mul (math-add xresq ximsq) | |
2840 sqscale)) | |
2841 sqscale)) | |
2842 (rem1 (car math-int-coefs)) | |
2843 (icp (cdr math-int-coefs)) | |
2844 (rem0 (car icp)) | |
2845 (newcoef nil) | |
2846 (found (assoc (list rnd0 rnd1 (math-posp xim)) | |
2847 math-double-roots)) | |
2848 this) | |
2849 (if found | |
2850 (setq math-double-roots (delq found math-double-roots) | |
2851 rem0 0 rem1 0) | |
2852 (while (setq icp (cdr icp)) | |
2853 (setq this rem1 | |
2854 newcoef (cons rem1 newcoef) | |
2855 rem1 (math-sub rem0 (math-mul this rnd1)) | |
2856 rem0 (math-sub (car icp) (math-mul this rnd0))))) | |
2857 (and (math-zerop rem0) | |
2858 (math-zerop rem1) | |
2859 (let ((aa (math-div rnd1 -2))) | |
2860 (or found (setq math-int-coefs (reverse newcoef) | |
2861 math-double-roots (cons (list | |
2862 (list | |
2863 rnd0 rnd1 | |
2864 (math-negp xim))) | |
2865 math-double-roots) | |
2866 math-int-factors (cons (cons rnd0 rnd1) | |
2867 math-int-factors))) | |
2868 (math-add aa | |
2869 (let ((calc-symbolic-mode math-symbolic-solve)) | |
2870 (math-mul (math-sqrt (math-sub (math-sqr aa) | |
2871 rnd0)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2872 (if (math-negp xim) -1 1))))))))))) |
40785 | 2873 (setq math-int-coefs nil) |
2874 | |
2875 ;;; The following routine is from Numerical Recipes, section 9.5. | |
2876 (defun math-poly-laguerre-root (p x polish) | |
2877 (let* ((calc-prefer-frac nil) | |
2878 (calc-symbolic-mode nil) | |
2879 (iters 0) | |
2880 (m (1- (length p))) | |
2881 (try-newt (not polish)) | |
2882 (tried-newt nil) | |
2883 b d f x1 dx dxold) | |
2884 (while | |
2885 (and (or (< (setq iters (1+ iters)) 50) | |
2886 (math-reject-arg x "*Laguerre's method failed to converge")) | |
2887 (let ((err (math-abs-approx (car p))) | |
2888 (abx (math-abs-approx x)) | |
2889 (pp p)) | |
2890 (setq b (car p) | |
2891 d 0 f 0) | |
2892 (while (setq pp (cdr pp)) | |
2893 (setq f (math-add (math-mul x f) d) | |
2894 d (math-add (math-mul x d) b) | |
2895 b (math-add (math-mul x b) (car pp)) | |
2896 err (math-add (math-abs-approx b) (math-mul abx err)))) | |
2897 (math-lessp (calcFunc-scf err (- -2 calc-internal-prec)) | |
2898 (math-abs-approx b))) | |
2899 (or (not (math-zerop d)) | |
2900 (not (math-zerop f)) | |
2901 (progn | |
2902 (setq x (math-pow (math-neg b) (list 'frac 1 m))) | |
2903 nil)) | |
2904 (let* ((g (math-div d b)) | |
2905 (g2 (math-sqr g)) | |
2906 (h (math-sub g2 (math-mul 2 (math-div f b)))) | |
2907 (sq (math-sqrt | |
2908 (math-mul (1- m) (math-sub (math-mul m h) g2)))) | |
2909 (gp (math-add g sq)) | |
2910 (gm (math-sub g sq))) | |
2911 (if (math-lessp (calcFunc-abssqr gp) (calcFunc-abssqr gm)) | |
2912 (setq gp gm)) | |
2913 (setq dx (math-div m gp) | |
2914 x1 (math-sub x dx)) | |
2915 (if (and try-newt | |
2916 (math-lessp (math-abs-approx dx) | |
2917 (calcFunc-scf (math-abs-approx x) -3))) | |
2918 (let ((newt (math-poly-newton-root p x1 7))) | |
2919 (setq tried-newt t | |
2920 try-newt nil) | |
2921 (if (math-zerop (cdr newt)) | |
2922 (setq x (car newt) x1 x) | |
2923 (if (math-lessp (cdr newt) '(float 1 -6)) | |
2924 (let ((newt2 (math-poly-newton-root | |
2925 p (car newt) 20))) | |
2926 (if (math-zerop (cdr newt2)) | |
2927 (setq x (car newt2) x1 x) | |
2928 (setq x (car newt)))))))) | |
2929 (not (or (eq x x1) | |
2930 (math-nearly-equal x x1)))) | |
2931 (let ((cdx (math-abs-approx dx))) | |
2932 (setq x x1 | |
2933 tried-newt nil) | |
2934 (prog1 | |
2935 (or (<= iters 6) | |
2936 (math-lessp cdx dxold) | |
2937 (progn | |
2938 (if polish | |
2939 (let ((digs (calcFunc-xpon | |
2940 (math-div (math-abs-approx x) cdx)))) | |
2941 (calc-record-why | |
2942 "*Could not attain full precision") | |
2943 (if (natnump digs) | |
2944 (let ((calc-internal-prec (max 3 digs))) | |
2945 (setq x (math-normalize x)))))) | |
2946 nil)) | |
2947 (setq dxold cdx))) | |
2948 (or polish | |
2949 (math-lessp (calcFunc-scf (math-abs-approx x) | |
2950 (- calc-internal-prec)) | |
2951 dxold)))) | |
2952 (or (and (math-floatp x) | |
2953 (math-poly-integer-root x)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2954 x))) |
40785 | 2955 |
2956 (defun math-solve-above-dummy (x) | |
2957 (and (not (Math-primp x)) | |
2958 (if (and (equal (nth 1 x) '(var SOLVEDUM SOLVEDUM)) | |
2959 (= (length x) 2)) | |
2960 x | |
2961 (let ((res nil)) | |
2962 (while (and (setq x (cdr x)) | |
2963 (not (setq res (math-solve-above-dummy (car x)))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2964 res)))) |
40785 | 2965 |
2966 (defun math-solve-find-root-term (x neg) ; sets "t2", "t3" | |
2967 (if (math-solve-find-root-in-prod x) | |
2968 (setq t3 neg | |
2969 t1 x) | |
2970 (and (memq (car-safe x) '(+ -)) | |
2971 (or (math-solve-find-root-term (nth 1 x) neg) | |
2972 (math-solve-find-root-term (nth 2 x) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2973 (if (eq (car x) '-) (not neg) neg)))))) |
40785 | 2974 |
2975 (defun math-solve-find-root-in-prod (x) | |
2976 (and (consp x) | |
2977 (math-expr-contains x solve-var) | |
2978 (or (and (eq (car x) 'calcFunc-sqrt) | |
2979 (setq t2 2)) | |
2980 (and (eq (car x) '^) | |
2981 (or (and (memq (math-quarter-integer (nth 2 x)) '(1 2 3)) | |
2982 (setq t2 2)) | |
2983 (and (eq (car-safe (nth 2 x)) 'frac) | |
2984 (eq (nth 2 (nth 2 x)) 3) | |
2985 (setq t2 3)))) | |
2986 (and (memq (car x) '(* /)) | |
2987 (or (and (not (math-expr-contains (nth 1 x) solve-var)) | |
2988 (math-solve-find-root-in-prod (nth 2 x))) | |
2989 (and (not (math-expr-contains (nth 2 x) solve-var)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2990 (math-solve-find-root-in-prod (nth 1 x)))))))) |
40785 | 2991 |
2992 | |
2993 (defun math-solve-system (exprs solve-vars solve-full) | |
2994 (setq exprs (mapcar 'list (if (Math-vectorp exprs) | |
2995 (cdr exprs) | |
2996 (list exprs))) | |
2997 solve-vars (if (Math-vectorp solve-vars) | |
2998 (cdr solve-vars) | |
2999 (list solve-vars))) | |
3000 (or (let ((math-solve-simplifying nil)) | |
3001 (math-solve-system-rec exprs solve-vars nil)) | |
3002 (let ((math-solve-simplifying t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3003 (math-solve-system-rec exprs solve-vars nil)))) |
40785 | 3004 |
3005 ;;; The following backtracking solver works by choosing a variable | |
3006 ;;; and equation, and trying to solve the equation for the variable. | |
3007 ;;; If it succeeds it calls itself recursively with that variable and | |
3008 ;;; equation removed from their respective lists, and with the solution | |
3009 ;;; added to solns as well as being substituted into all existing | |
3010 ;;; equations. The algorithm terminates when any solution path | |
3011 ;;; manages to remove all the variables from var-list. | |
3012 | |
3013 ;;; To support calcFunc-roots, entries in eqn-list and solns are | |
3014 ;;; actually lists of equations. | |
3015 | |
3016 (defun math-solve-system-rec (eqn-list var-list solns) | |
3017 (if var-list | |
3018 (let ((v var-list) | |
3019 (res nil)) | |
3020 | |
3021 ;; Try each variable in turn. | |
3022 (while | |
3023 (and | |
3024 v | |
3025 (let* ((vv (car v)) | |
3026 (e eqn-list) | |
3027 (elim (eq (car-safe vv) 'calcFunc-elim))) | |
3028 (if elim | |
3029 (setq vv (nth 1 vv))) | |
3030 | |
3031 ;; Try each equation in turn. | |
3032 (while | |
3033 (and | |
3034 e | |
3035 (let ((e2 (car e)) | |
3036 (eprev nil) | |
3037 res2) | |
3038 (setq res nil) | |
3039 | |
3040 ;; Try to solve for vv the list of equations e2. | |
3041 (while (and e2 | |
3042 (setq res2 (or (and (eq (car e2) eprev) | |
3043 res2) | |
3044 (math-solve-for (car e2) 0 vv | |
3045 solve-full)))) | |
3046 (setq eprev (car e2) | |
3047 res (cons (if (eq solve-full 'all) | |
3048 (cdr res2) | |
3049 (list res2)) | |
3050 res) | |
3051 e2 (cdr e2))) | |
3052 (if e2 | |
3053 (setq res nil) | |
3054 | |
3055 ;; Found a solution. Now try other variables. | |
3056 (setq res (nreverse res) | |
3057 res (math-solve-system-rec | |
3058 (mapcar | |
3059 'math-solve-system-subst | |
3060 (delq (car e) | |
3061 (copy-sequence eqn-list))) | |
3062 (delq (car v) (copy-sequence var-list)) | |
3063 (let ((math-solve-simplifying nil) | |
3064 (s (mapcar | |
3065 (function | |
3066 (lambda (x) | |
3067 (cons | |
3068 (car x) | |
3069 (math-solve-system-subst | |
3070 (cdr x))))) | |
3071 solns))) | |
3072 (if elim | |
3073 s | |
3074 (cons (cons vv (apply 'append res)) | |
3075 s))))) | |
3076 (not res)))) | |
3077 (setq e (cdr e))) | |
3078 (not res))) | |
3079 (setq v (cdr v))) | |
3080 res) | |
3081 | |
3082 ;; Eliminated all variables, so now put solution into the proper format. | |
3083 (setq solns (sort solns | |
3084 (function | |
3085 (lambda (x y) | |
3086 (not (memq (car x) (memq (car y) solve-vars))))))) | |
3087 (if (eq solve-full 'all) | |
3088 (math-transpose | |
3089 (math-normalize | |
3090 (cons 'vec | |
3091 (if solns | |
3092 (mapcar (function (lambda (x) (cons 'vec (cdr x)))) solns) | |
3093 (mapcar (function (lambda (x) (cons 'vec x))) eqn-list))))) | |
3094 (math-normalize | |
3095 (cons 'vec | |
3096 (if solns | |
3097 (mapcar (function (lambda (x) (cons 'calcFunc-eq x))) solns) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3098 (mapcar 'car eqn-list))))))) |
40785 | 3099 |
3100 (defun math-solve-system-subst (x) ; uses "res" and "v" | |
3101 (let ((accum nil) | |
3102 (res2 res)) | |
3103 (while x | |
3104 (setq accum (nconc accum | |
3105 (mapcar (function | |
3106 (lambda (r) | |
3107 (if math-solve-simplifying | |
3108 (math-simplify | |
3109 (math-expr-subst (car x) vv r)) | |
3110 (math-expr-subst (car x) vv r)))) | |
3111 (car res2))) | |
3112 x (cdr x) | |
3113 res2 (cdr res2))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3114 accum)) |
40785 | 3115 |
3116 | |
3117 (defun math-get-from-counter (name) | |
3118 (let ((ctr (assq name calc-command-flags))) | |
3119 (if ctr | |
3120 (setcdr ctr (1+ (cdr ctr))) | |
3121 (setq ctr (cons name 1) | |
3122 calc-command-flags (cons ctr calc-command-flags))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3123 (cdr ctr))) |
40785 | 3124 |
3125 (defun math-solve-get-sign (val) | |
3126 (setq val (math-simplify val)) | |
3127 (if (and (eq (car-safe val) '*) | |
3128 (Math-numberp (nth 1 val))) | |
3129 (list '* (nth 1 val) (math-solve-get-sign (nth 2 val))) | |
3130 (and (eq (car-safe val) 'calcFunc-sqrt) | |
3131 (eq (car-safe (nth 1 val)) '^) | |
3132 (setq val (math-normalize (list '^ | |
3133 (nth 1 (nth 1 val)) | |
3134 (math-div (nth 2 (nth 1 val)) 2))))) | |
3135 (if solve-full | |
3136 (if (and (calc-var-value 'var-GenCount) | |
3137 (Math-natnump var-GenCount) | |
3138 (not (eq solve-full 'all))) | |
3139 (prog1 | |
3140 (math-mul (list 'calcFunc-as var-GenCount) val) | |
3141 (setq var-GenCount (math-add var-GenCount 1)) | |
3142 (calc-refresh-evaltos 'var-GenCount)) | |
3143 (let* ((var (concat "s" (math-get-from-counter 'solve-sign))) | |
3144 (var2 (list 'var (intern var) (intern (concat "var-" var))))) | |
3145 (if (eq solve-full 'all) | |
3146 (setq math-solve-ranges (cons (list var2 1 -1) | |
3147 math-solve-ranges))) | |
3148 (math-mul var2 val))) | |
3149 (calc-record-why "*Choosing positive solution") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3150 val))) |
40785 | 3151 |
3152 (defun math-solve-get-int (val &optional range first) | |
3153 (if solve-full | |
3154 (if (and (calc-var-value 'var-GenCount) | |
3155 (Math-natnump var-GenCount) | |
3156 (not (eq solve-full 'all))) | |
3157 (prog1 | |
3158 (math-mul val (list 'calcFunc-an var-GenCount)) | |
3159 (setq var-GenCount (math-add var-GenCount 1)) | |
3160 (calc-refresh-evaltos 'var-GenCount)) | |
3161 (let* ((var (concat "n" (math-get-from-counter 'solve-int))) | |
3162 (var2 (list 'var (intern var) (intern (concat "var-" var))))) | |
3163 (if (and range (eq solve-full 'all)) | |
3164 (setq math-solve-ranges (cons (cons var2 | |
3165 (cdr (calcFunc-index | |
3166 range (or first 0)))) | |
3167 math-solve-ranges))) | |
3168 (math-mul val var2))) | |
3169 (calc-record-why "*Choosing 0 for arbitrary integer in solution") | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3170 0)) |
40785 | 3171 |
3172 (defun math-solve-sign (sign expr) | |
3173 (and sign | |
3174 (let ((s1 (math-possible-signs expr))) | |
3175 (cond ((memq s1 '(4 6)) | |
3176 sign) | |
3177 ((memq s1 '(1 3)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3178 (- sign)))))) |
40785 | 3179 |
3180 (defun math-looks-evenp (expr) | |
3181 (if (Math-integerp expr) | |
3182 (math-evenp expr) | |
3183 (if (memq (car expr) '(* /)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3184 (math-looks-evenp (nth 1 expr))))) |
40785 | 3185 |
3186 (defun math-solve-for (lhs rhs solve-var solve-full &optional sign) | |
3187 (if (math-expr-contains rhs solve-var) | |
3188 (math-solve-for (math-sub lhs rhs) 0 solve-var solve-full) | |
3189 (and (math-expr-contains lhs solve-var) | |
3190 (math-with-extra-prec 1 | |
3191 (let* ((math-poly-base-variable solve-var) | |
3192 (res (math-try-solve-for lhs rhs sign))) | |
3193 (if (and (eq solve-full 'all) | |
3194 (math-known-realp solve-var)) | |
3195 (let ((old-len (length res)) | |
3196 new-len) | |
3197 (setq res (delq nil | |
3198 (mapcar (function | |
3199 (lambda (x) | |
3200 (and (not (memq (car-safe x) | |
3201 '(cplx polar))) | |
3202 x))) | |
3203 res)) | |
3204 new-len (length res)) | |
3205 (if (< new-len old-len) | |
3206 (calc-record-why (if (= new-len 1) | |
3207 "*All solutions were complex" | |
3208 (format | |
3209 "*Omitted %d complex solutions" | |
3210 (- old-len new-len))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3211 res))))) |
40785 | 3212 |
3213 (defun math-solve-eqn (expr var full) | |
3214 (if (memq (car-safe expr) '(calcFunc-neq calcFunc-lt calcFunc-gt | |
3215 calcFunc-leq calcFunc-geq)) | |
3216 (let ((res (math-solve-for (cons '- (cdr expr)) | |
3217 0 var full | |
3218 (if (eq (car expr) 'calcFunc-neq) nil 1)))) | |
3219 (and res | |
3220 (if (eq math-solve-sign 1) | |
3221 (list (car expr) var res) | |
3222 (if (eq math-solve-sign -1) | |
3223 (list (car expr) res var) | |
3224 (or (eq (car expr) 'calcFunc-neq) | |
3225 (calc-record-why | |
3226 "*Can't determine direction of inequality")) | |
3227 (and (memq (car expr) '(calcFunc-neq calcFunc-lt calcFunc-gt)) | |
3228 (list 'calcFunc-neq var res)))))) | |
3229 (let ((res (math-solve-for expr 0 var full))) | |
3230 (and res | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3231 (list 'calcFunc-eq var res))))) |
40785 | 3232 |
3233 (defun math-reject-solution (expr var func) | |
3234 (if (math-expr-contains expr var) | |
3235 (or (equal (car calc-next-why) '(* "Unable to find a symbolic solution")) | |
3236 (calc-record-why "*Unable to find a solution"))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3237 (list func expr var)) |
40785 | 3238 |
3239 (defun calcFunc-solve (expr var) | |
3240 (or (if (or (Math-vectorp expr) (Math-vectorp var)) | |
3241 (math-solve-system expr var nil) | |
3242 (math-solve-eqn expr var nil)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3243 (math-reject-solution expr var 'calcFunc-solve))) |
40785 | 3244 |
3245 (defun calcFunc-fsolve (expr var) | |
3246 (or (if (or (Math-vectorp expr) (Math-vectorp var)) | |
3247 (math-solve-system expr var t) | |
3248 (math-solve-eqn expr var t)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3249 (math-reject-solution expr var 'calcFunc-fsolve))) |
40785 | 3250 |
3251 (defun calcFunc-roots (expr var) | |
3252 (let ((math-solve-ranges nil)) | |
3253 (or (if (or (Math-vectorp expr) (Math-vectorp var)) | |
3254 (math-solve-system expr var 'all) | |
3255 (math-solve-for expr 0 var 'all)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3256 (math-reject-solution expr var 'calcFunc-roots)))) |
40785 | 3257 |
3258 (defun calcFunc-finv (expr var) | |
3259 (let ((res (math-solve-for expr math-integ-var var nil))) | |
3260 (if res | |
3261 (math-normalize (math-expr-subst res math-integ-var var)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3262 (math-reject-solution expr var 'calcFunc-finv)))) |
40785 | 3263 |
3264 (defun calcFunc-ffinv (expr var) | |
3265 (let ((res (math-solve-for expr math-integ-var var t))) | |
3266 (if res | |
3267 (math-normalize (math-expr-subst res math-integ-var var)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3268 (math-reject-solution expr var 'calcFunc-finv)))) |
40785 | 3269 |
3270 | |
3271 (put 'calcFunc-inv 'math-inverse | |
3272 (function (lambda (x) (math-div 1 x)))) | |
3273 (put 'calcFunc-inv 'math-inverse-sign -1) | |
3274 | |
3275 (put 'calcFunc-sqrt 'math-inverse | |
3276 (function (lambda (x) (math-sqr x)))) | |
3277 | |
3278 (put 'calcFunc-conj 'math-inverse | |
3279 (function (lambda (x) (list 'calcFunc-conj x)))) | |
3280 | |
3281 (put 'calcFunc-abs 'math-inverse | |
3282 (function (lambda (x) (math-solve-get-sign x)))) | |
3283 | |
3284 (put 'calcFunc-deg 'math-inverse | |
3285 (function (lambda (x) (list 'calcFunc-rad x)))) | |
3286 (put 'calcFunc-deg 'math-inverse-sign 1) | |
3287 | |
3288 (put 'calcFunc-rad 'math-inverse | |
3289 (function (lambda (x) (list 'calcFunc-deg x)))) | |
3290 (put 'calcFunc-rad 'math-inverse-sign 1) | |
3291 | |
3292 (put 'calcFunc-ln 'math-inverse | |
3293 (function (lambda (x) (list 'calcFunc-exp x)))) | |
3294 (put 'calcFunc-ln 'math-inverse-sign 1) | |
3295 | |
3296 (put 'calcFunc-log10 'math-inverse | |
3297 (function (lambda (x) (list 'calcFunc-exp10 x)))) | |
3298 (put 'calcFunc-log10 'math-inverse-sign 1) | |
3299 | |
3300 (put 'calcFunc-lnp1 'math-inverse | |
3301 (function (lambda (x) (list 'calcFunc-expm1 x)))) | |
3302 (put 'calcFunc-lnp1 'math-inverse-sign 1) | |
3303 | |
3304 (put 'calcFunc-exp 'math-inverse | |
3305 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-ln x)) | |
3306 (math-mul 2 | |
3307 (math-mul '(var pi var-pi) | |
3308 (math-solve-get-int | |
3309 '(var i var-i)))))))) | |
3310 (put 'calcFunc-exp 'math-inverse-sign 1) | |
3311 | |
3312 (put 'calcFunc-expm1 'math-inverse | |
3313 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-lnp1 x)) | |
3314 (math-mul 2 | |
3315 (math-mul '(var pi var-pi) | |
3316 (math-solve-get-int | |
3317 '(var i var-i)))))))) | |
3318 (put 'calcFunc-expm1 'math-inverse-sign 1) | |
3319 | |
3320 (put 'calcFunc-sin 'math-inverse | |
3321 (function (lambda (x) (let ((n (math-solve-get-int 1))) | |
3322 (math-add (math-mul (math-normalize | |
3323 (list 'calcFunc-arcsin x)) | |
3324 (math-pow -1 n)) | |
3325 (math-mul (math-half-circle t) | |
3326 n)))))) | |
3327 | |
3328 (put 'calcFunc-cos 'math-inverse | |
3329 (function (lambda (x) (math-add (math-solve-get-sign | |
3330 (math-normalize | |
3331 (list 'calcFunc-arccos x))) | |
3332 (math-solve-get-int | |
3333 (math-full-circle t)))))) | |
3334 | |
3335 (put 'calcFunc-tan 'math-inverse | |
3336 (function (lambda (x) (math-add (math-normalize (list 'calcFunc-arctan x)) | |
3337 (math-solve-get-int | |
3338 (math-half-circle t)))))) | |
3339 | |
3340 (put 'calcFunc-arcsin 'math-inverse | |
3341 (function (lambda (x) (math-normalize (list 'calcFunc-sin x))))) | |
3342 | |
3343 (put 'calcFunc-arccos 'math-inverse | |
3344 (function (lambda (x) (math-normalize (list 'calcFunc-cos x))))) | |
3345 | |
3346 (put 'calcFunc-arctan 'math-inverse | |
3347 (function (lambda (x) (math-normalize (list 'calcFunc-tan x))))) | |
3348 | |
3349 (put 'calcFunc-sinh 'math-inverse | |
3350 (function (lambda (x) (let ((n (math-solve-get-int 1))) | |
3351 (math-add (math-mul (math-normalize | |
3352 (list 'calcFunc-arcsinh x)) | |
3353 (math-pow -1 n)) | |
3354 (math-mul (math-half-circle t) | |
3355 (math-mul | |
3356 '(var i var-i) | |
3357 n))))))) | |
3358 (put 'calcFunc-sinh 'math-inverse-sign 1) | |
3359 | |
3360 (put 'calcFunc-cosh 'math-inverse | |
3361 (function (lambda (x) (math-add (math-solve-get-sign | |
3362 (math-normalize | |
3363 (list 'calcFunc-arccosh x))) | |
3364 (math-mul (math-full-circle t) | |
3365 (math-solve-get-int | |
3366 '(var i var-i))))))) | |
3367 | |
3368 (put 'calcFunc-tanh 'math-inverse | |
3369 (function (lambda (x) (math-add (math-normalize | |
3370 (list 'calcFunc-arctanh x)) | |
3371 (math-mul (math-half-circle t) | |
3372 (math-solve-get-int | |
3373 '(var i var-i))))))) | |
3374 (put 'calcFunc-tanh 'math-inverse-sign 1) | |
3375 | |
3376 (put 'calcFunc-arcsinh 'math-inverse | |
3377 (function (lambda (x) (math-normalize (list 'calcFunc-sinh x))))) | |
3378 (put 'calcFunc-arcsinh 'math-inverse-sign 1) | |
3379 | |
3380 (put 'calcFunc-arccosh 'math-inverse | |
3381 (function (lambda (x) (math-normalize (list 'calcFunc-cosh x))))) | |
3382 | |
3383 (put 'calcFunc-arctanh 'math-inverse | |
3384 (function (lambda (x) (math-normalize (list 'calcFunc-tanh x))))) | |
3385 (put 'calcFunc-arctanh 'math-inverse-sign 1) | |
3386 | |
3387 | |
3388 | |
3389 (defun calcFunc-taylor (expr var num) | |
3390 (let ((x0 0) (v var)) | |
3391 (if (memq (car-safe var) '(+ - calcFunc-eq)) | |
3392 (setq x0 (if (eq (car var) '+) (math-neg (nth 2 var)) (nth 2 var)) | |
3393 v (nth 1 var))) | |
3394 (or (and (eq (car-safe v) 'var) | |
3395 (math-expr-contains expr v) | |
3396 (natnump num) | |
3397 (let ((accum (math-expr-subst expr v x0)) | |
3398 (var2 (if (eq (car var) 'calcFunc-eq) | |
3399 (cons '- (cdr var)) | |
3400 var)) | |
3401 (n 0) | |
3402 (nfac 1) | |
3403 (fprime expr)) | |
3404 (while (and (<= (setq n (1+ n)) num) | |
3405 (setq fprime (calcFunc-deriv fprime v nil t))) | |
3406 (setq fprime (math-simplify fprime) | |
3407 nfac (math-mul nfac n) | |
3408 accum (math-add accum | |
3409 (math-div (math-mul (math-pow var2 n) | |
3410 (math-expr-subst | |
3411 fprime v x0)) | |
3412 nfac)))) | |
3413 (and fprime | |
3414 (math-normalize accum)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3415 (list 'calcFunc-taylor expr var num)))) |
40785 | 3416 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3417 ;;; calcalg2.el ends here |