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