Mercurial > emacs
annotate lisp/calc/calcalg3.el @ 41569:f403516f2134
(fill-c++-comment): Use insert instead of insert-string.
author | Pavel Janík <Pavel@Janik.cz> |
---|---|
date | Mon, 26 Nov 2001 16:21:40 +0000 |
parents | fcd507927105 |
children | 395cff3e82cd |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calcalg3.el --- more algebraic functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
6 ;; Maintainer: Colin Walters <walters@debian.org> |
40785 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
40785 | 26 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
30 (require 'calc-ext) | |
31 | |
32 (require 'calc-macs) | |
33 | |
34 (defun calc-Need-calc-alg-3 () nil) | |
35 | |
36 | |
37 (defun calc-find-root (var) | |
38 (interactive "sVariable(s) to solve for: ") | |
39 (calc-slow-wrapper | |
40 (let ((func (if (calc-is-hyperbolic) 'calcFunc-wroot 'calcFunc-root))) | |
41 (if (or (equal var "") (equal var "$")) | |
42 (calc-enter-result 2 "root" (list func | |
43 (calc-top-n 3) | |
44 (calc-top-n 1) | |
45 (calc-top-n 2))) | |
46 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var) | |
47 (not (string-match "\\[" var))) | |
48 (math-read-expr (concat "[" var "]")) | |
49 (math-read-expr var)))) | |
50 (if (eq (car-safe var) 'error) | |
51 (error "Bad format in expression: %s" (nth 1 var))) | |
52 (calc-enter-result 1 "root" (list func | |
53 (calc-top-n 2) | |
54 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
55 (calc-top-n 1)))))))) |
40785 | 56 |
57 (defun calc-find-minimum (var) | |
58 (interactive "sVariable(s) to minimize over: ") | |
59 (calc-slow-wrapper | |
60 (let ((func (if (calc-is-inverse) | |
61 (if (calc-is-hyperbolic) | |
62 'calcFunc-wmaximize 'calcFunc-maximize) | |
63 (if (calc-is-hyperbolic) | |
64 'calcFunc-wminimize 'calcFunc-minimize))) | |
65 (tag (if (calc-is-inverse) "max" "min"))) | |
66 (if (or (equal var "") (equal var "$")) | |
67 (calc-enter-result 2 tag (list func | |
68 (calc-top-n 3) | |
69 (calc-top-n 1) | |
70 (calc-top-n 2))) | |
71 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var) | |
72 (not (string-match "\\[" var))) | |
73 (math-read-expr (concat "[" var "]")) | |
74 (math-read-expr var)))) | |
75 (if (eq (car-safe var) 'error) | |
76 (error "Bad format in expression: %s" (nth 1 var))) | |
77 (calc-enter-result 1 tag (list func | |
78 (calc-top-n 2) | |
79 var | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
80 (calc-top-n 1)))))))) |
40785 | 81 |
82 (defun calc-find-maximum (var) | |
83 (interactive "sVariable to maximize over: ") | |
84 (calc-invert-func) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
85 (calc-find-minimum var)) |
40785 | 86 |
87 | |
88 (defun calc-poly-interp (arg) | |
89 (interactive "P") | |
90 (calc-slow-wrapper | |
91 (let ((data (calc-top 2))) | |
92 (if (or (consp arg) (eq arg 0) (eq arg 2)) | |
93 (setq data (cons 'vec (calc-top-list 2 2))) | |
94 (or (null arg) | |
95 (error "Bad prefix argument"))) | |
96 (if (calc-is-hyperbolic) | |
97 (calc-enter-result 1 "rati" (list 'calcFunc-ratint data (calc-top 1))) | |
98 (calc-enter-result 1 "poli" (list 'calcFunc-polint data | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
99 (calc-top 1))))))) |
40785 | 100 |
101 | |
102 (defun calc-curve-fit (arg &optional model coefnames varnames) | |
103 (interactive "P") | |
104 (calc-slow-wrapper | |
105 (setq calc-aborted-prefix nil) | |
106 (let ((func (if (calc-is-inverse) 'calcFunc-xfit | |
107 (if (calc-is-hyperbolic) 'calcFunc-efit | |
108 'calcFunc-fit))) | |
109 key (which 0) | |
110 n nvars temp data | |
111 (homog nil) | |
112 (msgs '( "(Press ? for help)" | |
113 "1 = linear or multilinear" | |
114 "2-9 = polynomial fits; i = interpolating polynomial" | |
115 "p = a x^b, ^ = a b^x" | |
116 "e = a exp(b x), x = exp(a + b x), l = a + b ln(x)" | |
117 "E = a 10^(b x), X = 10^(a + b x), L = a + b log10(x)" | |
118 "q = a + b (x-c)^2" | |
119 "g = (a/b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)" | |
120 "h prefix = homogeneous model (no constant term)" | |
121 "' = alg entry, $ = stack, u = Model1, U = Model2"))) | |
122 (while (not model) | |
123 (message "Fit to model: %s:%s" | |
124 (nth which msgs) | |
125 (if homog " h" "")) | |
126 (setq key (read-char)) | |
127 (cond ((= key ?\C-g) | |
128 (keyboard-quit)) | |
129 ((= key ??) | |
130 (setq which (% (1+ which) (length msgs)))) | |
131 ((memq key '(?h ?H)) | |
132 (setq homog (not homog))) | |
133 ((progn | |
134 (if (eq key ?\$) | |
135 (setq n 1) | |
136 (setq n 0)) | |
137 (cond ((null arg) | |
138 (setq n (1+ n) | |
139 data (calc-top n))) | |
140 ((or (consp arg) (eq arg 0)) | |
141 (setq n (+ n 2) | |
142 data (calc-top n) | |
143 data (if (math-matrixp data) | |
144 (append data (list (calc-top (1- n)))) | |
145 (list 'vec data (calc-top (1- n)))))) | |
146 ((> (setq arg (prefix-numeric-value arg)) 0) | |
147 (setq data (cons 'vec (calc-top-list arg (1+ n))) | |
148 n (+ n arg))) | |
149 (t (error "Bad prefix argument"))) | |
150 (or (math-matrixp data) (not (cdr (cdr data))) | |
151 (error "Data matrix is not a matrix!")) | |
152 (setq nvars (- (length data) 2) | |
153 coefnames nil | |
154 varnames nil) | |
155 nil)) | |
156 ((= key ?1) ; linear or multilinear | |
157 (calc-get-fit-variables nvars (1+ nvars) (and homog 0)) | |
158 (setq model (math-mul coefnames | |
159 (cons 'vec (cons 1 (cdr varnames)))))) | |
160 ((and (>= key ?2) (<= key ?9)) ; polynomial | |
161 (calc-get-fit-variables 1 (- key ?0 -1) (and homog 0)) | |
162 (setq model (math-build-polynomial-expr (cdr coefnames) | |
163 (nth 1 varnames)))) | |
164 ((= key ?i) ; exact polynomial | |
165 (calc-get-fit-variables 1 (1- (length (nth 1 data))) | |
166 (and homog 0)) | |
167 (setq model (math-build-polynomial-expr (cdr coefnames) | |
168 (nth 1 varnames)))) | |
169 ((= key ?p) ; power law | |
170 (calc-get-fit-variables nvars (1+ nvars) (and homog 1)) | |
171 (setq model (math-mul (nth 1 coefnames) | |
172 (calcFunc-reduce | |
173 '(var mul var-mul) | |
174 (calcFunc-map | |
175 '(var pow var-pow) | |
176 varnames | |
177 (cons 'vec (cdr (cdr coefnames)))))))) | |
178 ((= key ?^) ; exponential law | |
179 (calc-get-fit-variables nvars (1+ nvars) (and homog 1)) | |
180 (setq model (math-mul (nth 1 coefnames) | |
181 (calcFunc-reduce | |
182 '(var mul var-mul) | |
183 (calcFunc-map | |
184 '(var pow var-pow) | |
185 (cons 'vec (cdr (cdr coefnames))) | |
186 varnames))))) | |
187 ((memq key '(?e ?E)) | |
188 (calc-get-fit-variables nvars (1+ nvars) (and homog 1)) | |
189 (setq model (math-mul (nth 1 coefnames) | |
190 (calcFunc-reduce | |
191 '(var mul var-mul) | |
192 (calcFunc-map | |
193 (if (eq key ?e) | |
194 '(var exp var-exp) | |
195 '(calcFunc-lambda | |
196 (var a var-a) | |
197 (^ 10 (var a var-a)))) | |
198 (calcFunc-map | |
199 '(var mul var-mul) | |
200 (cons 'vec (cdr (cdr coefnames))) | |
201 varnames)))))) | |
202 ((memq key '(?x ?X)) | |
203 (calc-get-fit-variables nvars (1+ nvars) (and homog 0)) | |
204 (setq model (math-mul coefnames | |
205 (cons 'vec (cons 1 (cdr varnames))))) | |
206 (setq model (if (eq key ?x) | |
207 (list 'calcFunc-exp model) | |
208 (list '^ 10 model)))) | |
209 ((memq key '(?l ?L)) | |
210 (calc-get-fit-variables nvars (1+ nvars) (and homog 0)) | |
211 (setq model (math-mul coefnames | |
212 (cons 'vec | |
213 (cons 1 (cdr (calcFunc-map | |
214 (if (eq key ?l) | |
215 '(var ln var-ln) | |
216 '(var log10 | |
217 var-log10)) | |
218 varnames))))))) | |
219 ((= key ?q) | |
220 (calc-get-fit-variables nvars (1+ (* 2 nvars)) (and homog 0)) | |
221 (let ((c coefnames) | |
222 (v varnames)) | |
223 (setq model (nth 1 c)) | |
224 (while (setq v (cdr v) c (cdr (cdr c))) | |
225 (setq model (math-add | |
226 model | |
227 (list '* | |
228 (car c) | |
229 (list '^ | |
230 (list '- (car v) (nth 1 c)) | |
231 2))))))) | |
232 ((= key ?g) | |
233 (setq model (math-read-expr "(AFit / BFit sqrt(2 pi)) exp(-0.5 * ((XFit - CFit) / BFit)^2)") | |
234 varnames '(vec (var XFit var-XFit)) | |
235 coefnames '(vec (var AFit var-AFit) | |
236 (var BFit var-BFit) | |
237 (var CFit var-CFit))) | |
238 (calc-get-fit-variables 1 (1- (length coefnames)) (and homog 1))) | |
239 ((memq key '(?\$ ?\' ?u ?U)) | |
240 (let* ((defvars nil) | |
241 (record-entry nil)) | |
242 (if (eq key ?\') | |
243 (let* ((calc-dollar-values calc-arg-values) | |
244 (calc-dollar-used 0) | |
245 (calc-hashes-used 0)) | |
246 (setq model (calc-do-alg-entry "" "Model formula: ")) | |
247 (if (/= (length model) 1) | |
248 (error "Bad format")) | |
249 (setq model (car model) | |
250 record-entry t) | |
251 (if (> calc-dollar-used 0) | |
252 (setq coefnames | |
253 (cons 'vec | |
254 (nthcdr (- (length calc-arg-values) | |
255 calc-dollar-used) | |
256 (reverse calc-arg-values)))) | |
257 (if (> calc-hashes-used 0) | |
258 (setq coefnames | |
259 (cons 'vec (calc-invent-args | |
260 calc-hashes-used)))))) | |
261 (progn | |
262 (setq model (cond ((eq key ?u) | |
263 (calc-var-value 'var-Model1)) | |
264 ((eq key ?U) | |
265 (calc-var-value 'var-Model2)) | |
266 (t (calc-top 1)))) | |
267 (or model (error "User model not yet defined")) | |
268 (if (math-vectorp model) | |
269 (if (and (memq (length model) '(3 4)) | |
270 (not (math-objvecp (nth 1 model))) | |
271 (math-vectorp (nth 2 model)) | |
272 (or (null (nth 3 model)) | |
273 (math-vectorp (nth 3 model)))) | |
274 (setq varnames (nth 2 model) | |
275 coefnames (or (nth 3 model) | |
276 (cons 'vec | |
277 (math-all-vars-but | |
278 model varnames))) | |
279 model (nth 1 model)) | |
280 (error "Incorrect model specifier"))))) | |
281 (or varnames | |
282 (let ((with-y (eq (car-safe model) 'calcFunc-eq))) | |
283 (if coefnames | |
284 (calc-get-fit-variables (if with-y (1+ nvars) nvars) | |
285 (1- (length coefnames)) | |
286 (math-all-vars-but | |
287 model coefnames) | |
288 nil with-y) | |
289 (let* ((coefs (math-all-vars-but model nil)) | |
290 (vars nil) | |
291 (n (- (length coefs) nvars (if with-y 2 1))) | |
292 p) | |
293 (if (< n 0) | |
294 (error "Not enough variables in model")) | |
295 (setq p (nthcdr n coefs)) | |
296 (setq vars (cdr p)) | |
297 (setcdr p nil) | |
298 (calc-get-fit-variables (if with-y (1+ nvars) nvars) | |
299 (length coefs) | |
300 vars coefs with-y))))) | |
301 (if record-entry | |
302 (calc-record (list 'vec model varnames coefnames) | |
303 "modl")))) | |
304 (t (beep)))) | |
305 (let ((calc-fit-to-trail t)) | |
306 (calc-enter-result n (substring (symbol-name func) 9) | |
307 (list func model | |
308 (if (= (length varnames) 2) | |
309 (nth 1 varnames) | |
310 varnames) | |
311 (if (= (length coefnames) 2) | |
312 (nth 1 coefnames) | |
313 coefnames) | |
314 data)) | |
315 (if (consp calc-fit-to-trail) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
316 (calc-record (calc-normalize calc-fit-to-trail) "parm")))))) |
40785 | 317 |
318 (defun calc-invent-independent-variables (n &optional but) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
319 (calc-invent-variables n but '(x y z t) "x")) |
40785 | 320 |
321 (defun calc-invent-parameter-variables (n &optional but) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
322 (calc-invent-variables n but '(a b c d) "a")) |
40785 | 323 |
324 (defun calc-invent-variables (num but names base) | |
325 (let ((vars nil) | |
326 (n num) (nn 0) | |
327 var) | |
328 (while (and (> n 0) names) | |
329 (setq var (math-build-var-name (if (consp names) | |
330 (car names) | |
331 (concat base (setq nn (1+ nn)))))) | |
332 (or (math-expr-contains (cons 'vec but) var) | |
333 (setq vars (cons var vars) | |
334 n (1- n))) | |
335 (or (symbolp names) (setq names (cdr names)))) | |
336 (if (= n 0) | |
337 (nreverse vars) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
338 (calc-invent-variables num but t base)))) |
40785 | 339 |
340 (defun calc-get-fit-variables (nv nc &optional defv defc with-y homog) | |
341 (or (= nv (if with-y (1+ nvars) nvars)) | |
342 (error "Wrong number of data vectors for this type of model")) | |
343 (if (integerp defv) | |
344 (setq homog defv | |
345 defv nil)) | |
346 (if homog | |
347 (setq nc (1- nc))) | |
348 (or defv | |
349 (setq defv (calc-invent-independent-variables nv))) | |
350 (or defc | |
351 (setq defc (calc-invent-parameter-variables nc defv))) | |
352 (let ((vars (read-string (format "Fitting variables: (default %s; %s) " | |
353 (mapconcat 'symbol-name | |
354 (mapcar (function (lambda (v) | |
355 (nth 1 v))) | |
356 defv) | |
357 ",") | |
358 (mapconcat 'symbol-name | |
359 (mapcar (function (lambda (v) | |
360 (nth 1 v))) | |
361 defc) | |
362 ",")))) | |
363 (coefs nil)) | |
364 (setq vars (if (string-match "\\[" vars) | |
365 (math-read-expr vars) | |
366 (math-read-expr (concat "[" vars "]")))) | |
367 (if (eq (car-safe vars) 'error) | |
368 (error "Bad format in expression: %s" (nth 2 vars))) | |
369 (or (math-vectorp vars) | |
370 (error "Expected a variable or vector of variables")) | |
371 (if (equal vars '(vec)) | |
372 (setq vars (cons 'vec defv) | |
373 coefs (cons 'vec defc)) | |
374 (if (math-vectorp (nth 1 vars)) | |
375 (if (and (= (length vars) 3) | |
376 (math-vectorp (nth 2 vars))) | |
377 (setq coefs (nth 2 vars) | |
378 vars (nth 1 vars)) | |
379 (error | |
380 "Expected independent variables vector, then parameters vector")) | |
381 (setq coefs (cons 'vec defc)))) | |
382 (or (= nv (1- (length vars))) | |
383 (and (not with-y) (= (1+ nv) (1- (length vars)))) | |
384 (error "Expected %d independent variable%s" nv (if (= nv 1) "" "s"))) | |
385 (or (= nc (1- (length coefs))) | |
386 (error "Expected %d parameter variable%s" nc (if (= nc 1) "" "s"))) | |
387 (if homog | |
388 (setq coefs (cons 'vec (cons homog (cdr coefs))))) | |
389 (if varnames | |
390 (setq model (math-multi-subst model (cdr varnames) (cdr vars)))) | |
391 (if coefnames | |
392 (setq model (math-multi-subst model (cdr coefnames) (cdr coefs)))) | |
393 (setq varnames vars | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
394 coefnames coefs))) |
40785 | 395 |
396 | |
397 | |
398 | |
399 ;;; The following algorithms are from Numerical Recipes chapter 9. | |
400 | |
401 ;;; "rtnewt" with safety kludges | |
402 (defun math-newton-root (expr deriv guess orig-guess limit) | |
403 (math-working "newton" guess) | |
404 (let* ((var-DUMMY guess) | |
405 next dval) | |
406 (setq next (math-evaluate-expr expr) | |
407 dval (math-evaluate-expr deriv)) | |
408 (if (and (Math-numberp next) | |
409 (Math-numberp dval) | |
410 (not (Math-zerop dval))) | |
411 (progn | |
412 (setq next (math-sub guess (math-div next dval))) | |
413 (if (math-nearly-equal guess (setq next (math-float next))) | |
414 (progn | |
415 (setq var-DUMMY next) | |
416 (list 'vec next (math-evaluate-expr expr))) | |
417 (if (Math-lessp (math-abs-approx (math-sub next orig-guess)) | |
418 limit) | |
419 (math-newton-root expr deriv next orig-guess limit) | |
420 (math-reject-arg next "*Newton's method failed to converge")))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
421 (math-reject-arg next "*Newton's method encountered a singularity")))) |
40785 | 422 |
423 ;;; Inspired by "rtsafe" | |
424 (defun math-newton-search-root (expr deriv guess vguess ostep oostep | |
425 low vlow high vhigh) | |
426 (let ((var-DUMMY guess) | |
427 (better t) | |
428 pos step next vnext) | |
429 (if guess | |
430 (math-working "newton" (list 'intv 0 low high)) | |
431 (math-working "bisect" (list 'intv 0 low high)) | |
432 (setq ostep (math-mul-float (math-sub-float high low) | |
433 '(float 5 -1)) | |
434 guess (math-add-float low ostep) | |
435 var-DUMMY guess | |
436 vguess (math-evaluate-expr expr)) | |
437 (or (Math-realp vguess) | |
438 (progn | |
439 (setq ostep (math-mul-float ostep '(float 6 -1)) | |
440 guess (math-add-float low ostep) | |
441 var-DUMMY guess | |
442 vguess (math-evaluate-expr expr)) | |
443 (or (math-realp vguess) | |
444 (progn | |
445 (setq ostep (math-mul-float ostep '(float 123456 -5)) | |
446 guess (math-add-float low ostep) | |
447 var-DUMMY guess | |
448 vguess nil)))))) | |
449 (or vguess | |
450 (setq vguess (math-evaluate-expr expr))) | |
451 (or (Math-realp vguess) | |
452 (math-reject-arg guess "*Newton's method encountered a singularity")) | |
453 (setq vguess (math-float vguess)) | |
454 (if (eq (Math-negp vlow) (setq pos (Math-posp vguess))) | |
455 (setq high guess | |
456 vhigh vguess) | |
457 (if (eq (Math-negp vhigh) pos) | |
458 (setq low guess | |
459 vlow vguess) | |
460 (setq better nil))) | |
461 (if (or (Math-zerop vguess) | |
462 (math-nearly-equal low high)) | |
463 (list 'vec guess vguess) | |
464 (setq step (math-evaluate-expr deriv)) | |
465 (if (and (Math-realp step) | |
466 (not (Math-zerop step)) | |
467 (setq step (math-div-float vguess (math-float step)) | |
468 next (math-sub-float guess step)) | |
469 (not (math-lessp-float high next)) | |
470 (not (math-lessp-float next low))) | |
471 (progn | |
472 (setq var-DUMMY next | |
473 vnext (math-evaluate-expr expr)) | |
474 (if (or (Math-zerop vnext) | |
475 (math-nearly-equal next guess)) | |
476 (list 'vec next vnext) | |
477 (if (and better | |
478 (math-lessp-float (math-abs (or oostep | |
479 (math-sub-float | |
480 high low))) | |
481 (math-abs | |
482 (math-mul-float '(float 2 0) | |
483 step)))) | |
484 (math-newton-search-root expr deriv nil nil nil ostep | |
485 low vlow high vhigh) | |
486 (math-newton-search-root expr deriv next vnext step ostep | |
487 low vlow high vhigh)))) | |
488 (if (or (and (Math-posp vlow) (Math-posp vhigh)) | |
489 (and (Math-negp vlow) (Math-negp vhigh))) | |
490 (math-search-root expr deriv low vlow high vhigh) | |
491 (math-newton-search-root expr deriv nil nil nil ostep | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
492 low vlow high vhigh)))))) |
40785 | 493 |
494 ;;; Search for a root in an interval with no overt zero crossing. | |
495 (defun math-search-root (expr deriv low vlow high vhigh) | |
496 (let (found) | |
497 (if root-widen | |
498 (let ((iters 0) | |
499 (iterlim (if (eq root-widen 'point) | |
500 (+ calc-internal-prec 10) | |
501 20)) | |
502 (factor (if (eq root-widen 'point) | |
503 '(float 9 0) | |
504 '(float 16 -1))) | |
505 (prev nil) vprev waslow | |
506 diff) | |
507 (while (or (and (math-posp vlow) (math-posp vhigh)) | |
508 (and (math-negp vlow) (math-negp vhigh))) | |
509 (math-working "widen" (list 'intv 0 low high)) | |
510 (if (> (setq iters (1+ iters)) iterlim) | |
511 (math-reject-arg (list 'intv 0 low high) | |
512 "*Unable to bracket root")) | |
513 (if (= iters calc-internal-prec) | |
514 (setq factor '(float 16 -1))) | |
515 (setq diff (math-mul-float (math-sub-float high low) factor)) | |
516 (if (Math-zerop diff) | |
517 (setq high (calcFunc-incr high 10)) | |
518 (if (math-lessp-float (math-abs vlow) (math-abs vhigh)) | |
519 (setq waslow t | |
520 prev low | |
521 low (math-sub low diff) | |
522 var-DUMMY low | |
523 vprev vlow | |
524 vlow (math-evaluate-expr expr)) | |
525 (setq waslow nil | |
526 prev high | |
527 high (math-add high diff) | |
528 var-DUMMY high | |
529 vprev vhigh | |
530 vhigh (math-evaluate-expr expr))))) | |
531 (if prev | |
532 (if waslow | |
533 (setq high prev vhigh vprev) | |
534 (setq low prev vlow vprev))) | |
535 (setq found t)) | |
536 (or (Math-realp vlow) | |
537 (math-reject-arg vlow 'realp)) | |
538 (or (Math-realp vhigh) | |
539 (math-reject-arg vhigh 'realp)) | |
540 (let ((xvals (list low high)) | |
541 (yvals (list vlow vhigh)) | |
542 (pos (Math-posp vlow)) | |
543 (levels 0) | |
544 (step (math-sub-float high low)) | |
545 xp yp var-DUMMY) | |
546 (while (and (<= (setq levels (1+ levels)) 5) | |
547 (not found)) | |
548 (setq xp xvals | |
549 yp yvals | |
550 step (math-mul-float step '(float 497 -3))) | |
551 (while (and (cdr xp) (not found)) | |
552 (if (Math-realp (car yp)) | |
553 (setq low (car xp) | |
554 vlow (car yp))) | |
555 (setq high (math-add-float (car xp) step) | |
556 var-DUMMY high | |
557 vhigh (math-evaluate-expr expr)) | |
558 (math-working "search" high) | |
559 (if (and (Math-realp vhigh) | |
560 (eq (math-negp vhigh) pos)) | |
561 (setq found t) | |
562 (setcdr xp (cons high (cdr xp))) | |
563 (setcdr yp (cons vhigh (cdr yp))) | |
564 (setq xp (cdr (cdr xp)) | |
565 yp (cdr (cdr yp)))))))) | |
566 (if found | |
567 (if (Math-zerop vhigh) | |
568 (list 'vec high vhigh) | |
569 (if (Math-zerop vlow) | |
570 (list 'vec low vlow) | |
571 (if deriv | |
572 (math-newton-search-root expr deriv nil nil nil nil | |
573 low vlow high vhigh) | |
574 (math-bisect-root expr low vlow high vhigh)))) | |
575 (math-reject-arg (list 'intv 3 low high) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
576 "*Unable to find a sign change in this interval")))) |
40785 | 577 |
578 ;;; "rtbis" (but we should be using Brent's method) | |
579 (defun math-bisect-root (expr low vlow high vhigh) | |
580 (let ((step (math-sub-float high low)) | |
581 (pos (Math-posp vhigh)) | |
582 var-DUMMY | |
583 mid vmid) | |
584 (while (not (or (math-nearly-equal low | |
585 (setq step (math-mul-float | |
586 step '(float 5 -1)) | |
587 mid (math-add-float low step))) | |
588 (progn | |
589 (setq var-DUMMY mid | |
590 vmid (math-evaluate-expr expr)) | |
591 (Math-zerop vmid)))) | |
592 (math-working "bisect" mid) | |
593 (if (eq (Math-posp vmid) pos) | |
594 (setq high mid | |
595 vhigh vmid) | |
596 (setq low mid | |
597 vlow vmid))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
598 (list 'vec mid vmid))) |
40785 | 599 |
600 ;;; "mnewt" | |
601 (defun math-newton-multi (expr jacob n guess orig-guess limit) | |
602 (let ((m -1) | |
603 (p guess) | |
604 p2 expr-val jacob-val next) | |
605 (while (< (setq p (cdr p) m (1+ m)) n) | |
606 (set (nth 2 (aref math-root-vars m)) (car p))) | |
607 (setq expr-val (math-evaluate-expr expr) | |
608 jacob-val (math-evaluate-expr jacob)) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
609 (unless (and (math-constp expr-val) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
610 (math-constp jacob-val)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
611 (math-reject-arg guess "*Newton's method encountered a singularity")) |
40785 | 612 (setq next (math-add guess (math-div (math-float (math-neg expr-val)) |
613 (math-float jacob-val))) | |
614 p guess p2 next) | |
615 (math-working "newton" next) | |
616 (while (and (setq p (cdr p) p2 (cdr p2)) | |
617 (math-nearly-equal (car p) (car p2)))) | |
618 (if p | |
619 (if (Math-lessp (math-abs-approx (math-sub next orig-guess)) | |
620 limit) | |
621 (math-newton-multi expr jacob n next orig-guess limit) | |
622 (math-reject-arg nil "*Newton's method failed to converge")) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
623 (list 'vec next expr-val)))) |
40785 | 624 |
625 (defvar math-root-vars [(var DUMMY var-DUMMY)]) | |
626 | |
627 (defun math-find-root (expr var guess root-widen) | |
628 (if (eq (car-safe expr) 'vec) | |
629 (let ((n (1- (length expr))) | |
630 (calc-symbolic-mode nil) | |
631 (var-DUMMY nil) | |
632 (jacob (list 'vec)) | |
633 p p2 m row) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
634 (unless (eq (car-safe var) 'vec) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
635 (math-reject-arg var 'vectorp)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
636 (unless (= (length var) (1+ n)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
637 (math-dimension-error)) |
40785 | 638 (setq expr (copy-sequence expr)) |
639 (while (>= n (length math-root-vars)) | |
640 (let ((symb (intern (concat "math-root-v" | |
641 (int-to-string | |
642 (length math-root-vars)))))) | |
643 (setq math-root-vars (vconcat math-root-vars | |
644 (vector (list 'var symb symb)))))) | |
645 (setq m -1) | |
646 (while (< (setq m (1+ m)) n) | |
647 (set (nth 2 (aref math-root-vars m)) nil)) | |
648 (setq m -1 p var) | |
649 (while (setq m (1+ m) p (cdr p)) | |
650 (or (eq (car-safe (car p)) 'var) | |
651 (math-reject-arg var "*Expected a variable")) | |
652 (setq p2 expr) | |
653 (while (setq p2 (cdr p2)) | |
654 (setcar p2 (math-expr-subst (car p2) (car p) | |
655 (aref math-root-vars m))))) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
656 (unless (eq (car-safe guess) 'vec) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
657 (math-reject-arg guess 'vectorp)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
658 (unless (= (length guess) (1+ n)) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
659 (math-dimension-error)) |
40785 | 660 (setq guess (copy-sequence guess) |
661 p guess) | |
662 (while (setq p (cdr p)) | |
663 (or (Math-numberp (car guess)) | |
664 (math-reject-arg guess 'numberp)) | |
665 (setcar p (math-float (car p)))) | |
666 (setq p expr) | |
667 (while (setq p (cdr p)) | |
668 (if (assq (car-safe (car p)) calc-tweak-eqn-table) | |
669 (setcar p (math-sub (nth 1 (car p)) (nth 2 (car p))))) | |
670 (setcar p (math-evaluate-expr (car p))) | |
671 (setq row (list 'vec) | |
672 m -1) | |
673 (while (< (setq m (1+ m)) n) | |
674 (nconc row (list (math-evaluate-expr | |
675 (or (calcFunc-deriv (car p) | |
676 (aref math-root-vars m) | |
677 nil t) | |
678 (math-reject-arg | |
679 expr | |
680 "*Formulas must be differentiable")))))) | |
681 (nconc jacob (list row))) | |
682 (setq m (math-abs-approx guess)) | |
683 (math-newton-multi expr jacob n guess guess | |
684 (if (math-zerop m) '(float 1 3) (math-mul m 10)))) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
685 (unless (eq (car-safe var) 'var) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
686 (math-reject-arg var "*Expected a variable")) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
687 (unless (math-expr-contains expr var) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
688 (math-reject-arg expr "*Formula does not contain specified variable")) |
40785 | 689 (if (assq (car expr) calc-tweak-eqn-table) |
690 (setq expr (math-sub (nth 1 expr) (nth 2 expr)))) | |
691 (math-with-extra-prec 2 | |
692 (setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY))) | |
693 (let* ((calc-symbolic-mode nil) | |
694 (var-DUMMY nil) | |
695 (expr (math-evaluate-expr expr)) | |
696 (deriv (calcFunc-deriv expr '(var DUMMY var-DUMMY) nil t)) | |
697 low high vlow vhigh) | |
698 (and deriv (setq deriv (math-evaluate-expr deriv))) | |
699 (setq guess (math-float guess)) | |
700 (if (and (math-numberp guess) | |
701 deriv) | |
702 (math-newton-root expr deriv guess guess | |
703 (if (math-zerop guess) '(float 1 6) | |
704 (math-mul (math-abs-approx guess) 100))) | |
705 (if (Math-realp guess) | |
706 (setq low guess | |
707 high guess | |
708 var-DUMMY guess | |
709 vlow (math-evaluate-expr expr) | |
710 vhigh vlow | |
711 root-widen 'point) | |
712 (if (eq (car guess) 'intv) | |
713 (progn | |
714 (or (math-constp guess) (math-reject-arg guess 'constp)) | |
715 (setq low (nth 2 guess) | |
716 high (nth 3 guess)) | |
717 (if (memq (nth 1 guess) '(0 1)) | |
718 (setq low (calcFunc-incr low 1 high))) | |
719 (if (memq (nth 1 guess) '(0 2)) | |
720 (setq high (calcFunc-incr high -1 low))) | |
721 (setq var-DUMMY low | |
722 vlow (math-evaluate-expr expr) | |
723 var-DUMMY high | |
724 vhigh (math-evaluate-expr expr))) | |
725 (if (math-complexp guess) | |
726 (math-reject-arg "*Complex root finder must have derivative") | |
727 (math-reject-arg guess 'realp)))) | |
728 (if (Math-zerop vlow) | |
729 (list 'vec low vlow) | |
730 (if (Math-zerop vhigh) | |
731 (list 'vec high vhigh) | |
732 (if (and deriv (Math-numberp vlow) (Math-numberp vhigh)) | |
733 (math-newton-search-root expr deriv nil nil nil nil | |
734 low vlow high vhigh) | |
735 (if (or (and (Math-posp vlow) (Math-posp vhigh)) | |
736 (and (Math-negp vlow) (Math-negp vhigh)) | |
737 (not (Math-numberp vlow)) | |
738 (not (Math-numberp vhigh))) | |
739 (math-search-root expr deriv low vlow high vhigh) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
740 (math-bisect-root expr low vlow high vhigh)))))))))) |
40785 | 741 |
742 (defun calcFunc-root (expr var guess) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
743 (math-find-root expr var guess nil)) |
40785 | 744 |
745 (defun calcFunc-wroot (expr var guess) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
746 (math-find-root expr var guess t)) |
40785 | 747 |
748 | |
749 | |
750 | |
751 ;;; The following algorithms come from Numerical Recipes, chapter 10. | |
752 | |
753 (defun math-min-eval (expr a) | |
754 (if (Math-vectorp a) | |
755 (let ((m -1)) | |
756 (while (setq m (1+ m) a (cdr a)) | |
757 (set (nth 2 (aref math-min-vars m)) (car a)))) | |
758 (setq var-DUMMY a)) | |
759 (setq a (math-evaluate-expr expr)) | |
760 (if (Math-ratp a) | |
761 (math-float a) | |
762 (if (eq (car a) 'float) | |
763 a | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
764 (math-reject-arg a 'realp)))) |
40785 | 765 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
766 (defvar math-min-or-max "minimum") |
40785 | 767 |
768 ;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c). | |
769 | |
770 ;;; "mnbrak" | |
771 (defun math-widen-min (expr a b) | |
772 (let ((done nil) | |
773 (iters 30) | |
774 incr c va vb vc u vu r q ulim bc ba qr) | |
775 (or b (setq b (math-mul a '(float 101 -2)))) | |
776 (setq va (math-min-eval expr a) | |
777 vb (math-min-eval expr b)) | |
778 (if (math-lessp-float va vb) | |
779 (setq u a a b b u | |
780 vu va va vb vb vu)) | |
781 (setq c (math-add-float b (math-mul-float '(float 161803 -5) | |
782 (math-sub-float b a))) | |
783 vc (math-min-eval expr c)) | |
784 (while (and (not done) (math-lessp-float vc vb)) | |
785 (math-working "widen" (list 'intv 0 a c)) | |
786 (if (= (setq iters (1- iters)) 0) | |
787 (math-reject-arg nil (format "*Unable to find a %s near the interval" | |
788 math-min-or-max))) | |
789 (setq bc (math-sub-float b c) | |
790 ba (math-sub-float b a) | |
791 r (math-mul-float ba (math-sub-float vb vc)) | |
792 q (math-mul-float bc (math-sub-float vb va)) | |
793 qr (math-sub-float q r)) | |
794 (if (math-lessp-float (math-abs qr) '(float 1 -20)) | |
795 (setq qr (if (math-negp qr) '(float -1 -20) '(float 1 -20)))) | |
796 (setq u (math-sub-float | |
797 b | |
798 (math-div-float (math-sub-float (math-mul-float bc q) | |
799 (math-mul-float ba r)) | |
800 (math-mul-float '(float 2 0) qr))) | |
801 ulim (math-add-float b (math-mul-float '(float -1 2) bc)) | |
802 incr (math-negp bc)) | |
803 (if (if incr (math-lessp-float b u) (math-lessp-float u b)) | |
804 (if (if incr (math-lessp-float u c) (math-lessp-float c u)) | |
805 (if (math-lessp-float (setq vu (math-min-eval expr u)) vc) | |
806 (setq a b va vb | |
807 b u vb vu | |
808 done t) | |
809 (if (math-lessp-float vb vu) | |
810 (setq c u vc vu | |
811 done t) | |
812 (setq u (math-add-float c (math-mul-float '(float -161803 -5) | |
813 bc)) | |
814 vu (math-min-eval expr u)))) | |
815 (if (if incr (math-lessp-float u ulim) (math-lessp-float ulim u)) | |
816 (if (math-lessp-float (setq vu (math-min-eval expr u)) vc) | |
817 (setq b c vb vc | |
818 c u vc vu | |
819 u (math-add-float c (math-mul-float | |
820 '(float -161803 -5) | |
821 (math-sub-float b c))) | |
822 vu (math-min-eval expr u))) | |
823 (setq u ulim | |
824 vu (math-min-eval expr u)))) | |
825 (setq u (math-add-float c (math-mul-float '(float -161803 -5) | |
826 bc)) | |
827 vu (math-min-eval expr u))) | |
828 (setq a b va vb | |
829 b c vb vc | |
830 c u vc vu)) | |
831 (if (math-lessp-float a c) | |
832 (list a va b vb c vc) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
833 (list c vc b vb a va)))) |
40785 | 834 |
835 (defun math-narrow-min (expr a c intv) | |
836 (let ((xvals (list a c)) | |
837 (yvals (list (math-min-eval expr a) | |
838 (math-min-eval expr c))) | |
839 (levels 0) | |
840 (step (math-sub-float c a)) | |
841 (found nil) | |
842 xp yp b) | |
843 (while (and (<= (setq levels (1+ levels)) 5) | |
844 (not found)) | |
845 (setq xp xvals | |
846 yp yvals | |
847 step (math-mul-float step '(float 497 -3))) | |
848 (while (and (cdr xp) (not found)) | |
849 (setq b (math-add-float (car xp) step)) | |
850 (math-working "search" b) | |
851 (setcdr xp (cons b (cdr xp))) | |
852 (setcdr yp (cons (math-min-eval expr b) (cdr yp))) | |
853 (if (and (math-lessp-float (nth 1 yp) (car yp)) | |
854 (math-lessp-float (nth 1 yp) (nth 2 yp))) | |
855 (setq found t) | |
856 (setq xp (cdr xp) | |
857 yp (cdr yp)) | |
858 (if (and (cdr (cdr yp)) | |
859 (math-lessp-float (nth 1 yp) (car yp)) | |
860 (math-lessp-float (nth 1 yp) (nth 2 yp))) | |
861 (setq found t) | |
862 (setq xp (cdr xp) | |
863 yp (cdr yp)))))) | |
864 (if found | |
865 (list (car xp) (car yp) | |
866 (nth 1 xp) (nth 1 yp) | |
867 (nth 2 xp) (nth 2 yp)) | |
868 (or (if (math-lessp-float (car yvals) (nth 1 yvals)) | |
869 (and (memq (nth 1 intv) '(2 3)) | |
870 (let ((min (car yvals))) | |
871 (while (and (setq yvals (cdr yvals)) | |
872 (math-lessp-float min (car yvals)))) | |
873 (and (not yvals) | |
874 (list (nth 2 intv) min)))) | |
875 (and (memq (nth 1 intv) '(1 3)) | |
876 (setq yvals (nreverse yvals)) | |
877 (let ((min (car yvals))) | |
878 (while (and (setq yvals (cdr yvals)) | |
879 (math-lessp-float min (car yvals)))) | |
880 (and (not yvals) | |
881 (list (nth 3 intv) min))))) | |
882 (math-reject-arg nil (format "*Unable to find a %s in the interval" | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
883 math-min-or-max)))))) |
40785 | 884 |
885 ;;; "brent" | |
886 (defun math-brent-min (expr prec a va x vx b vb) | |
887 (let ((iters (+ 20 (* 5 prec))) | |
888 (w x) | |
889 (vw vx) | |
890 (v x) | |
891 (vv vx) | |
892 (tol (list 'float 1 (- -1 prec))) | |
893 (zeps (list 'float 1 (- -5 prec))) | |
894 (e '(float 0 0)) | |
895 u vu xm tol1 tol2 etemp p q r xv xw) | |
896 (while (progn | |
897 (setq xm (math-mul-float '(float 5 -1) | |
898 (math-add-float a b)) | |
899 tol1 (math-add-float | |
900 zeps | |
901 (math-mul-float tol (math-abs x))) | |
902 tol2 (math-mul-float tol1 '(float 2 0))) | |
903 (math-lessp-float (math-sub-float tol2 | |
904 (math-mul-float | |
905 '(float 5 -1) | |
906 (math-sub-float b a))) | |
907 (math-abs (math-sub-float x xm)))) | |
908 (if (= (setq iters (1- iters)) 0) | |
909 (math-reject-arg nil (format "*Unable to converge on a %s" | |
910 math-min-or-max))) | |
911 (math-working "brent" x) | |
912 (if (math-lessp-float (math-abs e) tol1) | |
913 (setq e (if (math-lessp-float x xm) | |
914 (math-sub-float b x) | |
915 (math-sub-float a x)) | |
916 d (math-mul-float '(float 381966 -6) e)) | |
917 (setq xw (math-sub-float x w) | |
918 r (math-mul-float xw (math-sub-float vx vv)) | |
919 xv (math-sub-float x v) | |
920 q (math-mul-float xv (math-sub-float vx vw)) | |
921 p (math-sub-float (math-mul-float xv q) | |
922 (math-mul-float xw r)) | |
923 q (math-mul-float '(float 2 0) (math-sub-float q r))) | |
924 (if (math-posp q) | |
925 (setq p (math-neg-float p)) | |
926 (setq q (math-neg-float q))) | |
927 (setq etemp e | |
928 e d) | |
929 (if (and (math-lessp-float (math-abs p) | |
930 (math-abs (math-mul-float | |
931 '(float 5 -1) | |
932 (math-mul-float q etemp)))) | |
933 (math-lessp-float (math-mul-float | |
934 q (math-sub-float a x)) p) | |
935 (math-lessp-float p (math-mul-float | |
936 q (math-sub-float b x)))) | |
937 (progn | |
938 (setq d (math-div-float p q) | |
939 u (math-add-float x d)) | |
940 (if (or (math-lessp-float (math-sub-float u a) tol2) | |
941 (math-lessp-float (math-sub-float b u) tol2)) | |
942 (setq d (if (math-lessp-float xm x) | |
943 (math-neg-float tol1) | |
944 tol1)))) | |
945 (setq e (if (math-lessp-float x xm) | |
946 (math-sub-float b x) | |
947 (math-sub-float a x)) | |
948 d (math-mul-float '(float 381966 -6) e)))) | |
949 (setq u (math-add-float x | |
950 (if (math-lessp-float (math-abs d) tol1) | |
951 (if (math-negp d) | |
952 (math-neg-float tol1) | |
953 tol1) | |
954 d)) | |
955 vu (math-min-eval expr u)) | |
956 (if (math-lessp-float vx vu) | |
957 (progn | |
958 (if (math-lessp-float u x) | |
959 (setq a u) | |
960 (setq b u)) | |
961 (if (or (equal w x) | |
962 (not (math-lessp-float vw vu))) | |
963 (setq v w vv vw | |
964 w u vw vu) | |
965 (if (or (equal v x) | |
966 (equal v w) | |
967 (not (math-lessp-float vv vu))) | |
968 (setq v u vv vu)))) | |
969 (if (math-lessp-float u x) | |
970 (setq b x) | |
971 (setq a x)) | |
972 (setq v w vv vw | |
973 w x vw vx | |
974 x u vx vu))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
975 (list 'vec x vx))) |
40785 | 976 |
977 ;;; "powell" | |
978 (defun math-powell-min (expr n guesses prec) | |
979 (let* ((f1dim (math-line-min-func expr n)) | |
980 (xi (calcFunc-idn 1 n)) | |
981 (p (cons 'vec (mapcar 'car guesses))) | |
982 (pt p) | |
983 (ftol (list 'float 1 (- prec))) | |
984 (fret (math-min-eval expr p)) | |
985 fp ptt fptt xit i ibig del diff res) | |
986 (while (progn | |
987 (setq fp fret | |
988 ibig 0 | |
989 del '(float 0 0) | |
990 i 0) | |
991 (while (<= (setq i (1+ i)) n) | |
992 (setq fptt fret | |
993 res (math-line-min f1dim p | |
994 (math-mat-col xi i) | |
995 n prec) | |
996 p (let ((calc-internal-prec prec)) | |
997 (math-normalize (car res))) | |
998 fret (nth 2 res) | |
999 diff (math-abs (math-sub-float fptt fret))) | |
1000 (if (math-lessp-float del diff) | |
1001 (setq del diff | |
1002 ibig i))) | |
1003 (math-lessp-float | |
1004 (math-mul-float ftol | |
1005 (math-add-float (math-abs fp) | |
1006 (math-abs fret))) | |
1007 (math-mul-float '(float 2 0) | |
1008 (math-abs (math-sub-float fp | |
1009 fret))))) | |
1010 (setq ptt (math-sub (math-mul '(float 2 0) p) pt) | |
1011 xit (math-sub p pt) | |
1012 pt p | |
1013 fptt (math-min-eval expr ptt)) | |
1014 (if (and (math-lessp-float fptt fp) | |
1015 (math-lessp-float | |
1016 (math-mul-float | |
1017 (math-mul-float '(float 2 0) | |
1018 (math-add-float | |
1019 (math-sub-float fp | |
1020 (math-mul-float '(float 2 0) | |
1021 fret)) | |
1022 fptt)) | |
1023 (math-sqr-float (math-sub-float | |
1024 (math-sub-float fp fret) del))) | |
1025 (math-mul-float del | |
1026 (math-sqr-float (math-sub-float fp fptt))))) | |
1027 (progn | |
1028 (setq res (math-line-min f1dim p xit n prec) | |
1029 p (car res) | |
1030 fret (nth 2 res) | |
1031 i 0) | |
1032 (while (<= (setq i (1+ i)) n) | |
1033 (setcar (nthcdr ibig (nth i xi)) | |
1034 (nth i (nth 1 res))))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1035 (list 'vec p fret))) |
40785 | 1036 |
1037 (defun math-line-min-func (expr n) | |
1038 (let ((m -1)) | |
1039 (while (< (setq m (1+ m)) n) | |
1040 (set (nth 2 (aref math-min-vars m)) | |
1041 (list '+ | |
1042 (list '* | |
1043 '(var DUMMY var-DUMMY) | |
1044 (list 'calcFunc-mrow '(var line-xi line-xi) (1+ m))) | |
1045 (list 'calcFunc-mrow '(var line-p line-p) (1+ m))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1046 (math-evaluate-expr expr))) |
40785 | 1047 |
1048 (defun math-line-min (f1dim line-p line-xi n prec) | |
1049 (let* ((var-DUMMY nil) | |
1050 (expr (math-evaluate-expr f1dim)) | |
1051 (params (math-widen-min expr '(float 0 0) '(float 1 0))) | |
1052 (res (apply 'math-brent-min expr prec params)) | |
1053 (xi (math-mul (nth 1 res) line-xi))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1054 (list (math-add line-p xi) xi (nth 2 res)))) |
40785 | 1055 |
1056 | |
1057 (defvar math-min-vars [(var DUMMY var-DUMMY)]) | |
1058 | |
1059 (defun math-find-minimum (expr var guess min-widen) | |
1060 (let* ((calc-symbolic-mode nil) | |
1061 (n 0) | |
1062 (var-DUMMY nil) | |
1063 (isvec (math-vectorp var)) | |
1064 g guesses) | |
1065 (or (math-vectorp var) | |
1066 (setq var (list 'vec var))) | |
1067 (or (math-vectorp guess) | |
1068 (setq guess (list 'vec guess))) | |
1069 (or (= (length var) (length guess)) | |
1070 (math-dimension-error)) | |
1071 (while (setq var (cdr var) guess (cdr guess)) | |
1072 (or (eq (car-safe (car var)) 'var) | |
1073 (math-reject-arg (car vg) "*Expected a variable")) | |
1074 (or (math-expr-contains expr (car var)) | |
1075 (math-reject-arg (car var) | |
1076 "*Formula does not contain specified variable")) | |
1077 (while (>= (1+ n) (length math-min-vars)) | |
1078 (let ((symb (intern (concat "math-min-v" | |
1079 (int-to-string | |
1080 (length math-min-vars)))))) | |
1081 (setq math-min-vars (vconcat math-min-vars | |
1082 (vector (list 'var symb symb)))))) | |
1083 (set (nth 2 (aref math-min-vars n)) nil) | |
1084 (set (nth 2 (aref math-min-vars (1+ n))) nil) | |
1085 (if (math-complexp (car guess)) | |
1086 (setq expr (math-expr-subst expr | |
1087 (car var) | |
1088 (list '+ (aref math-min-vars n) | |
1089 (list '* | |
1090 (aref math-min-vars (1+ n)) | |
1091 '(cplx 0 1)))) | |
1092 guesses (let ((g (math-float (math-complex (car guess))))) | |
1093 (cons (list (nth 2 g) nil nil) | |
1094 (cons (list (nth 1 g) nil nil t) | |
1095 guesses))) | |
1096 n (+ n 2)) | |
1097 (setq expr (math-expr-subst expr | |
1098 (car var) | |
1099 (aref math-min-vars n)) | |
1100 guesses (cons (if (math-realp (car guess)) | |
1101 (list (math-float (car guess)) nil nil) | |
1102 (if (and (eq (car-safe (car guess)) 'intv) | |
1103 (math-constp (car guess))) | |
1104 (list (math-mul | |
1105 (math-add (nth 2 (car guess)) | |
1106 (nth 3 (car guess))) | |
1107 '(float 5 -1)) | |
1108 (math-float (nth 2 (car guess))) | |
1109 (math-float (nth 3 (car guess))) | |
1110 (car guess)) | |
1111 (math-reject-arg (car guess) 'realp))) | |
1112 guesses) | |
1113 n (1+ n)))) | |
1114 (setq guesses (nreverse guesses) | |
1115 expr (math-evaluate-expr expr)) | |
1116 (if (= n 1) | |
1117 (let* ((params (if (nth 1 (car guesses)) | |
1118 (if min-widen | |
1119 (math-widen-min expr | |
1120 (nth 1 (car guesses)) | |
1121 (nth 2 (car guesses))) | |
1122 (math-narrow-min expr | |
1123 (nth 1 (car guesses)) | |
1124 (nth 2 (car guesses)) | |
1125 (nth 3 (car guesses)))) | |
1126 (math-widen-min expr | |
1127 (car (car guesses)) | |
1128 nil))) | |
1129 (prec calc-internal-prec) | |
1130 (res (if (cdr (cdr params)) | |
1131 (math-with-extra-prec (+ calc-internal-prec 2) | |
1132 (apply 'math-brent-min expr prec params)) | |
1133 (cons 'vec params)))) | |
1134 (if isvec | |
1135 (list 'vec (list 'vec (nth 1 res)) (nth 2 res)) | |
1136 res)) | |
1137 (let* ((prec calc-internal-prec) | |
1138 (res (math-with-extra-prec (+ calc-internal-prec 2) | |
1139 (math-powell-min expr n guesses prec))) | |
1140 (p (nth 1 res)) | |
1141 (vec (list 'vec))) | |
1142 (while (setq p (cdr p)) | |
1143 (if (nth 3 (car guesses)) | |
1144 (progn | |
1145 (nconc vec (list (math-normalize | |
1146 (list 'cplx (car p) (nth 1 p))))) | |
1147 (setq p (cdr p) | |
1148 guesses (cdr guesses))) | |
1149 (nconc vec (list (car p)))) | |
1150 (setq guesses (cdr guesses))) | |
1151 (if isvec | |
1152 (list 'vec vec (nth 2 res)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1153 (list 'vec (nth 1 vec) (nth 2 res))))))) |
40785 | 1154 |
1155 (defun calcFunc-minimize (expr var guess) | |
1156 (let ((calc-internal-prec (max (/ calc-internal-prec 2) 3)) | |
1157 (math-min-or-max "minimum")) | |
1158 (math-find-minimum (math-normalize expr) | |
1159 (math-normalize var) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1160 (math-normalize guess) nil))) |
40785 | 1161 |
1162 (defun calcFunc-wminimize (expr var guess) | |
1163 (let ((calc-internal-prec (max (/ calc-internal-prec 2) 3)) | |
1164 (math-min-or-max "minimum")) | |
1165 (math-find-minimum (math-normalize expr) | |
1166 (math-normalize var) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1167 (math-normalize guess) t))) |
40785 | 1168 |
1169 (defun calcFunc-maximize (expr var guess) | |
1170 (let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3)) | |
1171 (math-min-or-max "maximum") | |
1172 (res (math-find-minimum (math-normalize (math-neg expr)) | |
1173 (math-normalize var) | |
1174 (math-normalize guess) nil))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1175 (list 'vec (nth 1 res) (math-neg (nth 2 res))))) |
40785 | 1176 |
1177 (defun calcFunc-wmaximize (expr var guess) | |
1178 (let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3)) | |
1179 (math-min-or-max "maximum") | |
1180 (res (math-find-minimum (math-normalize (math-neg expr)) | |
1181 (math-normalize var) | |
1182 (math-normalize guess) t))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1183 (list 'vec (nth 1 res) (math-neg (nth 2 res))))) |
40785 | 1184 |
1185 | |
1186 | |
1187 | |
1188 ;;; The following algorithms come from Numerical Recipes, chapter 3. | |
1189 | |
1190 (defun calcFunc-polint (data x) | |
1191 (or (math-matrixp data) (math-reject-arg data 'matrixp)) | |
1192 (or (= (length data) 3) | |
1193 (math-reject-arg data "*Wrong number of data rows")) | |
1194 (or (> (length (nth 1 data)) 2) | |
1195 (math-reject-arg data "*Too few data points")) | |
1196 (if (and (math-vectorp x) (or (math-constp x) math-expand-formulas)) | |
1197 (cons 'vec (mapcar (function (lambda (x) (calcFunc-polint data x))) | |
1198 (cdr x))) | |
1199 (or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp)) | |
1200 (math-with-extra-prec 2 | |
1201 (cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1202 nil))))) |
40785 | 1203 (put 'calcFunc-polint 'math-expandable t) |
1204 | |
1205 | |
1206 (defun calcFunc-ratint (data x) | |
1207 (or (math-matrixp data) (math-reject-arg data 'matrixp)) | |
1208 (or (= (length data) 3) | |
1209 (math-reject-arg data "*Wrong number of data rows")) | |
1210 (or (> (length (nth 1 data)) 2) | |
1211 (math-reject-arg data "*Too few data points")) | |
1212 (if (and (math-vectorp x) (or (math-constp x) math-expand-formulas)) | |
1213 (cons 'vec (mapcar (function (lambda (x) (calcFunc-ratint data x))) | |
1214 (cdr x))) | |
1215 (or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp)) | |
1216 (math-with-extra-prec 2 | |
1217 (cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1218 (cdr (cdr (cdr (nth 1 data))))))))) |
40785 | 1219 (put 'calcFunc-ratint 'math-expandable t) |
1220 | |
1221 | |
1222 (defun math-poly-interp (xa ya x ratp) | |
1223 (let ((n (length xa)) | |
1224 (dif nil) | |
1225 (ns nil) | |
1226 (xax nil) | |
1227 (c (copy-sequence ya)) | |
1228 (d (copy-sequence ya)) | |
1229 (i 0) | |
1230 (m 0) | |
1231 y dy (xp xa) xpm cp dp temp) | |
1232 (while (<= (setq i (1+ i)) n) | |
1233 (setq xax (cons (math-sub (car xp) x) xax) | |
1234 xp (cdr xp) | |
1235 temp (math-abs (car xax))) | |
1236 (if (or (null dif) (math-lessp temp dif)) | |
1237 (setq dif temp | |
1238 ns i))) | |
1239 (setq xax (nreverse xax) | |
1240 ns (1- ns) | |
1241 y (nth ns ya)) | |
1242 (if (math-zerop dif) | |
1243 (list y 0) | |
1244 (while (< (setq m (1+ m)) n) | |
1245 (setq i 0 | |
1246 xp xax | |
1247 xpm (nthcdr m xax) | |
1248 cp c | |
1249 dp d) | |
1250 (while (<= (setq i (1+ i)) (- n m)) | |
1251 (if ratp | |
1252 (let ((t2 (math-div (math-mul (car xp) (car dp)) (car xpm)))) | |
1253 (setq temp (math-div (math-sub (nth 1 cp) (car dp)) | |
1254 (math-sub t2 (nth 1 cp)))) | |
1255 (setcar dp (math-mul (nth 1 cp) temp)) | |
1256 (setcar cp (math-mul t2 temp))) | |
1257 (if (math-equal (car xp) (car xpm)) | |
1258 (math-reject-arg (cons 'vec xa) "*Duplicate X values")) | |
1259 (setq temp (math-div (math-sub (nth 1 cp) (car dp)) | |
1260 (math-sub (car xp) (car xpm)))) | |
1261 (setcar dp (math-mul (car xpm) temp)) | |
1262 (setcar cp (math-mul (car xp) temp))) | |
1263 (setq cp (cdr cp) | |
1264 dp (cdr dp) | |
1265 xp (cdr xp) | |
1266 xpm (cdr xpm))) | |
1267 (if (< (+ ns ns) (- n m)) | |
1268 (setq dy (nth ns c)) | |
1269 (setq ns (1- ns) | |
1270 dy (nth ns d))) | |
1271 (setq y (math-add y dy))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1272 (list y dy)))) |
40785 | 1273 |
1274 | |
1275 | |
1276 ;;; The following algorithms come from Numerical Recipes, chapter 4. | |
1277 | |
1278 (defun calcFunc-ninteg (expr var lo hi) | |
1279 (setq lo (math-evaluate-expr lo) | |
1280 hi (math-evaluate-expr hi)) | |
1281 (or (math-numberp lo) (math-infinitep lo) (math-reject-arg lo 'numberp)) | |
1282 (or (math-numberp hi) (math-infinitep hi) (math-reject-arg hi 'numberp)) | |
1283 (if (math-lessp hi lo) | |
1284 (math-neg (calcFunc-ninteg expr var hi lo)) | |
1285 (setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY))) | |
1286 (let ((var-DUMMY nil) | |
1287 (calc-symbolic-mode nil) | |
1288 (calc-prefer-frac nil) | |
1289 (sum 0)) | |
1290 (setq expr (math-evaluate-expr expr)) | |
1291 (if (equal lo '(neg (var inf var-inf))) | |
1292 (let ((thi (if (math-lessp hi '(float -2 0)) | |
1293 hi '(float -2 0)))) | |
1294 (setq sum (math-ninteg-romberg | |
1295 'math-ninteg-midpoint expr | |
1296 (math-float lo) (math-float thi) 'inf) | |
1297 lo thi))) | |
1298 (if (equal hi '(var inf var-inf)) | |
1299 (let ((tlo (if (math-lessp '(float 2 0) lo) | |
1300 lo '(float 2 0)))) | |
1301 (setq sum (math-add sum | |
1302 (math-ninteg-romberg | |
1303 'math-ninteg-midpoint expr | |
1304 (math-float tlo) (math-float hi) 'inf)) | |
1305 hi tlo))) | |
1306 (or (math-equal lo hi) | |
1307 (setq sum (math-add sum | |
1308 (math-ninteg-romberg | |
1309 'math-ninteg-midpoint expr | |
1310 (math-float lo) (math-float hi) nil)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1311 sum))) |
40785 | 1312 |
1313 | |
1314 ;;; Open Romberg method; "qromo" in section 4.4. | |
1315 (defun math-ninteg-romberg (func expr lo hi mode) | |
1316 (let ((curh '(float 1 0)) | |
1317 (h nil) | |
1318 (s nil) | |
1319 (j 0) | |
1320 (ss nil) | |
1321 (prec calc-internal-prec) | |
1322 (integ-temp nil)) | |
1323 (math-with-extra-prec 2 | |
1324 ;; Limit on "j" loop must be 14 or less to keep "it" from overflowing. | |
1325 (or (while (and (null ss) (<= (setq j (1+ j)) 8)) | |
1326 (setq s (nconc s (list (funcall func expr lo hi mode))) | |
1327 h (nconc h (list curh))) | |
1328 (if (>= j 3) | |
1329 (let ((res (math-poly-interp h s '(float 0 0) nil))) | |
1330 (if (math-lessp (math-abs (nth 1 res)) | |
1331 (calcFunc-scf (math-abs (car res)) | |
1332 (- prec))) | |
1333 (setq math-ninteg-convergence j | |
1334 ss (car res))))) | |
1335 (if (>= j 5) | |
1336 (setq s (cdr s) | |
1337 h (cdr h))) | |
1338 (setq curh (math-div-float curh '(float 9 0)))) | |
1339 ss | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1340 (math-reject-arg nil (format "*Integral failed to converge")))))) |
40785 | 1341 |
1342 | |
1343 (defun math-ninteg-evaluate (expr x mode) | |
1344 (if (eq mode 'inf) | |
1345 (setq x (math-div '(float 1 0) x))) | |
1346 (let* ((var-DUMMY x) | |
1347 (res (math-evaluate-expr expr))) | |
1348 (or (Math-numberp res) | |
1349 (math-reject-arg res "*Integrand does not evaluate to a number")) | |
1350 (if (eq mode 'inf) | |
1351 (setq res (math-mul res (math-sqr x)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1352 res)) |
40785 | 1353 |
1354 | |
1355 (defun math-ninteg-midpoint (expr lo hi mode) ; uses "integ-temp" | |
1356 (if (eq mode 'inf) | |
1357 (let ((math-infinite-mode t) temp) | |
1358 (setq temp (math-div 1 lo) | |
1359 lo (math-div 1 hi) | |
1360 hi temp))) | |
1361 (if integ-temp | |
1362 (let* ((it3 (* 3 (car integ-temp))) | |
1363 (math-working-step-2 (* 2 (car integ-temp))) | |
1364 (math-working-step 0) | |
1365 (range (math-sub hi lo)) | |
1366 (del (math-div range (math-float it3))) | |
1367 (del2 (math-add del del)) | |
1368 (del3 (math-add del del2)) | |
1369 (x (math-add lo (math-mul '(float 5 -1) del))) | |
1370 (sum '(float 0 0)) | |
1371 (j 0) temp) | |
1372 (while (<= (setq j (1+ j)) (car integ-temp)) | |
1373 (setq math-working-step (1+ math-working-step) | |
1374 temp (math-ninteg-evaluate expr x mode) | |
1375 math-working-step (1+ math-working-step) | |
1376 sum (math-add sum (math-add temp (math-ninteg-evaluate | |
1377 expr (math-add x del2) | |
1378 mode))) | |
1379 x (math-add x del3))) | |
1380 (setq integ-temp (list it3 | |
1381 (math-add (math-div (nth 1 integ-temp) | |
1382 '(float 3 0)) | |
1383 (math-mul sum del))))) | |
1384 (setq integ-temp (list 1 (math-mul | |
1385 (math-sub hi lo) | |
1386 (math-ninteg-evaluate | |
1387 expr | |
1388 (math-mul (math-add lo hi) '(float 5 -1)) | |
1389 mode))))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1390 (nth 1 integ-temp)) |
40785 | 1391 |
1392 | |
1393 | |
1394 | |
1395 | |
1396 ;;; The following algorithms come from Numerical Recipes, chapter 14. | |
1397 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1398 (defvar math-dummy-vars [(var DUMMY var-DUMMY)]) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1399 (defvar math-dummy-counter 0) |
40785 | 1400 (defun math-dummy-variable () |
1401 (if (= math-dummy-counter (length math-dummy-vars)) | |
1402 (let ((symb (intern (format "math-dummy-%d" math-dummy-counter)))) | |
1403 (setq math-dummy-vars (vconcat math-dummy-vars | |
1404 (vector (list 'var symb symb)))))) | |
1405 (set (nth 2 (aref math-dummy-vars math-dummy-counter)) nil) | |
1406 (prog1 | |
1407 (aref math-dummy-vars math-dummy-counter) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1408 (setq math-dummy-counter (1+ math-dummy-counter)))) |
40785 | 1409 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1410 (defvar math-in-fit 0) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1411 (defvar calc-fit-to-trail nil) |
40785 | 1412 |
1413 (defun calcFunc-fit (expr vars &optional coefs data) | |
1414 (let ((math-in-fit 10)) | |
1415 (math-with-extra-prec 2 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1416 (math-general-fit expr vars coefs data nil)))) |
40785 | 1417 |
1418 (defun calcFunc-efit (expr vars &optional coefs data) | |
1419 (let ((math-in-fit 10)) | |
1420 (math-with-extra-prec 2 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1421 (math-general-fit expr vars coefs data 'sdev)))) |
40785 | 1422 |
1423 (defun calcFunc-xfit (expr vars &optional coefs data) | |
1424 (let ((math-in-fit 10)) | |
1425 (math-with-extra-prec 2 | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1426 (math-general-fit expr vars coefs data 'full)))) |
40785 | 1427 |
1428 (defun math-general-fit (expr vars coefs data mode) | |
1429 (let ((calc-simplify-mode nil) | |
1430 (math-dummy-counter math-dummy-counter) | |
1431 (math-in-fit 1) | |
1432 (extended (eq mode 'full)) | |
1433 (first-coef math-dummy-counter) | |
1434 first-var | |
1435 (plain-expr expr) | |
1436 orig-expr | |
1437 have-sdevs need-chisq chisq | |
1438 (x-funcs nil) | |
1439 (y-filter nil) | |
1440 y-dummy | |
1441 (coef-filters nil) | |
1442 new-coefs | |
1443 (xy-values nil) | |
1444 (weights nil) | |
1445 (var-YVAL nil) (var-YVALX nil) | |
1446 covar beta | |
1447 n nn m mm v dummy p) | |
1448 | |
1449 ;; Validate and parse arguments. | |
1450 (or data | |
1451 (if coefs | |
1452 (setq data coefs | |
1453 coefs nil) | |
1454 (if (math-vectorp expr) | |
1455 (if (memq (length expr) '(3 4)) | |
1456 (setq data vars | |
1457 vars (nth 2 expr) | |
1458 coefs (nth 3 expr) | |
1459 expr (nth 1 expr)) | |
1460 (math-dimension-error)) | |
1461 (setq data vars | |
1462 vars nil | |
1463 coefs nil)))) | |
1464 (or (math-matrixp data) (math-reject-arg data 'matrixp)) | |
1465 (setq v (1- (length data)) | |
1466 n (1- (length (nth 1 data)))) | |
1467 (or (math-vectorp vars) (null vars) | |
1468 (setq vars (list 'vec vars))) | |
1469 (or (math-vectorp coefs) (null coefs) | |
1470 (setq coefs (list 'vec coefs))) | |
1471 (or coefs | |
1472 (setq coefs (cons 'vec (math-all-vars-but expr vars)))) | |
1473 (or vars | |
1474 (if (<= (1- (length coefs)) v) | |
1475 (math-reject-arg coefs "*Not enough variables in model") | |
1476 (setq coefs (copy-sequence coefs)) | |
1477 (let ((p (nthcdr (- (length coefs) v | |
1478 (if (eq (car-safe expr) 'calcFunc-eq) 1 0)) | |
1479 coefs))) | |
1480 (setq vars (cons 'vec (cdr p))) | |
1481 (setcdr p nil)))) | |
1482 (or (= (1- (length vars)) v) | |
1483 (= (length vars) v) | |
1484 (math-reject-arg vars "*Number of variables does not match data")) | |
1485 (setq m (1- (length coefs))) | |
1486 (if (< m 1) | |
1487 (math-reject-arg coefs "*Need at least one parameter")) | |
1488 | |
1489 ;; Rewrite expr in terms of fitparam and fitvar, make into an equation. | |
1490 (setq p coefs) | |
1491 (while (setq p (cdr p)) | |
1492 (or (eq (car-safe (car p)) 'var) | |
1493 (math-reject-arg (car p) "*Expected a variable")) | |
1494 (setq dummy (math-dummy-variable) | |
1495 expr (math-expr-subst expr (car p) | |
1496 (list 'calcFunc-fitparam | |
1497 (- math-dummy-counter first-coef))))) | |
1498 (setq first-var math-dummy-counter | |
1499 p vars) | |
1500 (while (setq p (cdr p)) | |
1501 (or (eq (car-safe (car p)) 'var) | |
1502 (math-reject-arg (car p) "*Expected a variable")) | |
1503 (setq dummy (math-dummy-variable) | |
1504 expr (math-expr-subst expr (car p) | |
1505 (list 'calcFunc-fitvar | |
1506 (- math-dummy-counter first-var))))) | |
1507 (if (< math-dummy-counter (+ first-var v)) | |
1508 (setq dummy (math-dummy-variable))) ; dependent variable may be unnamed | |
1509 (setq y-dummy dummy | |
1510 orig-expr expr) | |
1511 (or (eq (car-safe expr) 'calcFunc-eq) | |
1512 (setq expr (list 'calcFunc-eq (list 'calcFunc-fitvar v) expr))) | |
1513 | |
1514 (let ((calc-symbolic-mode nil)) | |
1515 | |
1516 ;; Apply rewrites to put expr into a linear-like form. | |
1517 (setq expr (math-evaluate-expr expr) | |
1518 expr (math-rewrite (list 'calcFunc-fitmodel expr) | |
1519 '(var FitRules var-FitRules)) | |
1520 math-in-fit 2 | |
1521 expr (math-evaluate-expr expr)) | |
1522 (or (and (eq (car-safe expr) 'calcFunc-fitsystem) | |
1523 (= (length expr) 4) | |
1524 (math-vectorp (nth 2 expr)) | |
1525 (math-vectorp (nth 3 expr)) | |
1526 (> (length (nth 2 expr)) 1) | |
1527 (= (length (nth 3 expr)) (1+ m))) | |
1528 (math-reject-arg plain-expr "*Model expression is too complex")) | |
1529 (setq y-filter (nth 1 expr) | |
1530 x-funcs (vconcat (cdr (nth 2 expr))) | |
1531 coef-filters (nth 3 expr) | |
1532 mm (length x-funcs)) | |
1533 (if (equal y-filter y-dummy) | |
1534 (setq y-filter nil)) | |
1535 | |
1536 ;; Build the (square) system of linear equations to be solved. | |
1537 (setq beta (cons 'vec (make-list mm 0)) | |
1538 covar (cons 'vec (mapcar 'copy-sequence (make-list mm beta)))) | |
1539 (let* ((ptrs (vconcat (cdr data))) | |
1540 (isigsq 1) | |
1541 (xvals (make-vector mm 0)) | |
1542 (i 0) | |
1543 j k xval yval sigmasqr wt covj covjk covk betaj lud) | |
1544 (while (<= (setq i (1+ i)) n) | |
1545 | |
1546 ;; Assign various independent variables for this data point. | |
1547 (setq j 0 | |
1548 sigmasqr nil) | |
1549 (while (< j v) | |
1550 (aset ptrs j (cdr (aref ptrs j))) | |
1551 (setq xval (car (aref ptrs j))) | |
1552 (if (= j (1- v)) | |
1553 (if sigmasqr | |
1554 (progn | |
1555 (if (eq (car-safe xval) 'sdev) | |
1556 (setq sigmasqr (math-add (math-sqr (nth 2 xval)) | |
1557 sigmasqr) | |
1558 xval (nth 1 xval))) | |
1559 (if y-filter | |
1560 (setq xval (math-make-sdev xval | |
1561 (math-sqrt sigmasqr)))))) | |
1562 (if (eq (car-safe xval) 'sdev) | |
1563 (setq sigmasqr (math-add (math-sqr (nth 2 xval)) | |
1564 (or sigmasqr 0)) | |
1565 xval (nth 1 xval)))) | |
1566 (set (nth 2 (aref math-dummy-vars (+ first-var j))) xval) | |
1567 (setq j (1+ j))) | |
1568 | |
1569 ;; Compute Y value for this data point. | |
1570 (if y-filter | |
1571 (setq yval (math-evaluate-expr y-filter)) | |
1572 (setq yval (symbol-value (nth 2 y-dummy)))) | |
1573 (if (eq (car-safe yval) 'sdev) | |
1574 (setq sigmasqr (math-sqr (nth 2 yval)) | |
1575 yval (nth 1 yval))) | |
1576 (if (= i 1) | |
1577 (setq have-sdevs sigmasqr | |
1578 need-chisq (or extended | |
1579 (and (eq mode 'sdev) (not have-sdevs))))) | |
1580 (if have-sdevs | |
1581 (if sigmasqr | |
1582 (progn | |
1583 (setq isigsq (math-div 1 sigmasqr)) | |
1584 (if need-chisq | |
1585 (setq weights (cons isigsq weights)))) | |
1586 (math-reject-arg yval "*Mixed error forms and plain numbers")) | |
1587 (if sigmasqr | |
1588 (math-reject-arg yval "*Mixed error forms and plain numbers"))) | |
1589 | |
1590 ;; Compute X values for this data point and update covar and beta. | |
1591 (if (eq (car-safe xval) 'sdev) | |
1592 (set (nth 2 y-dummy) (nth 1 xval))) | |
1593 (setq j 0 | |
1594 covj covar | |
1595 betaj beta) | |
1596 (while (< j mm) | |
1597 (setq wt (math-evaluate-expr (aref x-funcs j))) | |
1598 (aset xvals j wt) | |
1599 (setq wt (math-mul wt isigsq) | |
1600 betaj (cdr betaj) | |
1601 covjk (car (setq covj (cdr covj))) | |
1602 k 0) | |
1603 (while (<= k j) | |
1604 (setq covjk (cdr covjk)) | |
1605 (setcar covjk (math-add (car covjk) | |
1606 (math-mul wt (aref xvals k)))) | |
1607 (setq k (1+ k))) | |
1608 (setcar betaj (math-add (car betaj) (math-mul wt yval))) | |
1609 (setq j (1+ j))) | |
1610 (if need-chisq | |
1611 (setq xy-values (cons (append xvals (list yval)) xy-values)))) | |
1612 | |
1613 ;; Fill in symmetric half of covar matrix. | |
1614 (setq j 0 | |
1615 covj covar) | |
1616 (while (< j (1- mm)) | |
1617 (setq k j | |
1618 j (1+ j) | |
1619 covjk (nthcdr j (car (setq covj (cdr covj)))) | |
1620 covk (nthcdr j covar)) | |
1621 (while (< (setq k (1+ k)) mm) | |
1622 (setq covjk (cdr covjk) | |
1623 covk (cdr covk)) | |
1624 (setcar covjk (nth j (car covk)))))) | |
1625 | |
1626 ;; Solve the linear system. | |
1627 (if mode | |
1628 (progn | |
1629 (setq covar (math-matrix-inv-raw covar)) | |
1630 (if covar | |
1631 (setq beta (math-mul covar beta)) | |
1632 (if (math-zerop (math-abs beta)) | |
1633 (setq covar (calcFunc-diag 0 (1- (length beta)))) | |
1634 (math-reject-arg orig-expr "*Singular matrix"))) | |
1635 (or (math-vectorp covar) | |
1636 (setq covar (list 'vec (list 'vec covar))))) | |
1637 (setq beta (math-div beta covar))) | |
1638 | |
1639 ;; Compute chi-square statistic if necessary. | |
1640 (if need-chisq | |
1641 (let (bp xp sum) | |
1642 (setq chisq 0) | |
1643 (while xy-values | |
1644 (setq bp beta | |
1645 xp (car xy-values) | |
1646 sum 0) | |
1647 (while (setq bp (cdr bp)) | |
1648 (setq sum (math-add sum (math-mul (car bp) (car xp))) | |
1649 xp (cdr xp))) | |
1650 (setq sum (math-sqr (math-sub (car xp) sum))) | |
1651 (if weights (setq sum (math-mul sum (car weights)))) | |
1652 (setq chisq (math-add chisq sum) | |
1653 weights (cdr weights) | |
1654 xy-values (cdr xy-values))))) | |
1655 | |
1656 ;; Convert coefficients back into original terms. | |
1657 (setq new-coefs (copy-sequence beta)) | |
1658 (let* ((bp new-coefs) | |
1659 (cp covar) | |
1660 (sigdat 1) | |
1661 (math-in-fit 3) | |
1662 (j 0)) | |
1663 (and mode (not have-sdevs) | |
1664 (setq sigdat (if (<= n mm) | |
1665 0 | |
1666 (math-div chisq (- n mm))))) | |
1667 (if mode | |
1668 (while (setq bp (cdr bp)) | |
1669 (setcar bp (math-make-sdev | |
1670 (car bp) | |
1671 (math-sqrt (math-mul (nth (setq j (1+ j)) | |
1672 (car (setq cp (cdr cp)))) | |
1673 sigdat)))))) | |
1674 (setq new-coefs (math-evaluate-expr coef-filters)) | |
1675 (if calc-fit-to-trail | |
1676 (let ((bp new-coefs) | |
1677 (cp coefs) | |
1678 (vec nil)) | |
1679 (while (setq bp (cdr bp) cp (cdr cp)) | |
1680 (setq vec (cons (list 'calcFunc-eq (car cp) (car bp)) vec))) | |
1681 (setq calc-fit-to-trail (cons 'vec (nreverse vec))))))) | |
1682 | |
1683 ;; Substitute best-fit coefficients back into original formula. | |
1684 (setq expr (math-multi-subst | |
1685 orig-expr | |
1686 (let ((n v) | |
1687 (vec nil)) | |
1688 (while (>= n 1) | |
1689 (setq vec (cons (list 'calcFunc-fitvar n) vec) | |
1690 n (1- n))) | |
1691 (setq n m) | |
1692 (while (>= n 1) | |
1693 (setq vec (cons (list 'calcFunc-fitparam n) vec) | |
1694 n (1- n))) | |
1695 vec) | |
1696 (append (cdr new-coefs) (cdr vars)))) | |
1697 | |
1698 ;; Package the result. | |
1699 (math-normalize | |
1700 (if extended | |
1701 (list 'vec expr beta covar | |
1702 (let ((p coef-filters) | |
1703 (n 0)) | |
1704 (while (and (setq n (1+ n) p (cdr p)) | |
1705 (eq (car-safe (car p)) 'calcFunc-fitdummy) | |
1706 (eq (nth 1 (car p)) n))) | |
1707 (if p | |
1708 coef-filters | |
1709 (list 'vec))) | |
1710 chisq | |
1711 (if (and have-sdevs (> n mm)) | |
1712 (list 'calcFunc-utpc chisq (- n mm)) | |
1713 '(var nan var-nan))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1714 expr)))) |
40785 | 1715 |
1716 | |
1717 (defun calcFunc-fitvar (x) | |
1718 (if (>= math-in-fit 2) | |
1719 (progn | |
1720 (setq x (aref math-dummy-vars (+ first-var x -1))) | |
1721 (or (calc-var-value (nth 2 x)) x)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1722 (math-reject-arg x))) |
40785 | 1723 |
1724 (defun calcFunc-fitparam (x) | |
1725 (if (>= math-in-fit 2) | |
1726 (progn | |
1727 (setq x (aref math-dummy-vars (+ first-coef x -1))) | |
1728 (or (calc-var-value (nth 2 x)) x)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1729 (math-reject-arg x))) |
40785 | 1730 |
1731 (defun calcFunc-fitdummy (x) | |
1732 (if (= math-in-fit 3) | |
1733 (nth x new-coefs) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1734 (math-reject-arg x))) |
40785 | 1735 |
1736 (defun calcFunc-hasfitvars (expr) | |
1737 (if (Math-primp expr) | |
1738 0 | |
1739 (if (eq (car expr) 'calcFunc-fitvar) | |
1740 (nth 1 expr) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1741 (apply 'max (mapcar 'calcFunc-hasfitvars (cdr expr)))))) |
40785 | 1742 |
1743 (defun calcFunc-hasfitparams (expr) | |
1744 (if (Math-primp expr) | |
1745 0 | |
1746 (if (eq (car expr) 'calcFunc-fitparam) | |
1747 (nth 1 expr) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1748 (apply 'max (mapcar 'calcFunc-hasfitparams (cdr expr)))))) |
40785 | 1749 |
1750 | |
1751 (defun math-all-vars-but (expr but) | |
1752 (let* ((vars (math-all-vars-in expr)) | |
1753 (p but)) | |
1754 (while p | |
1755 (setq vars (delq (assoc (car-safe p) vars) vars) | |
1756 p (cdr p))) | |
1757 (sort (mapcar 'car vars) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1758 (function (lambda (x y) (string< (nth 1 x) (nth 1 y))))))) |
40785 | 1759 |
1760 (defun math-all-vars-in (expr) | |
1761 (let ((vars nil) | |
1762 found) | |
1763 (math-all-vars-rec expr) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1764 vars)) |
40785 | 1765 |
1766 (defun math-all-vars-rec (expr) | |
1767 (if (Math-primp expr) | |
1768 (if (eq (car-safe expr) 'var) | |
1769 (or (math-const-var expr) | |
1770 (if (setq found (assoc expr vars)) | |
1771 (setcdr found (1+ (cdr found))) | |
1772 (setq vars (cons (cons expr 1) vars))))) | |
1773 (while (setq expr (cdr expr)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1774 (math-all-vars-rec (car expr))))) |
40785 | 1775 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1776 ;;; calcalg3.el ends here |