1 ;;; calcalg3.el --- more algebraic functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 ;; Declare functions which are defined elsewhere.
33 (declare-function calc-fit-s-shaped-logistic-curve
"calc-nlfit" (arg))
34 (declare-function calc-fit-bell-shaped-logistic-curve
"calc-nlfit" (arg))
35 (declare-function calc-fit-hubbert-linear-curve
"calc-nlfit" (&optional sdv
))
36 (declare-function calc-graph-add-curve
"calc-graph" (xdata ydata
&optional zdata
))
37 (declare-function calc-graph-lookup
"calc-graph" (thing))
38 (declare-function calc-graph-set-styles
"calc-graph" (lines points
&optional yerr
))
39 (declare-function math-min-list
"calc-arith" (a b
))
40 (declare-function math-max-list
"calc-arith" (a b
))
43 (defun math-map-binop (binop args1 args2
)
44 "Apply BINOP to the elements of the lists ARGS1 and ARGS2"
47 (funcall binop
(car args1
) (car args2
))
48 (funcall 'math-map-binop binop
(cdr args1
) (cdr args2
)))))
50 (defun calc-find-root (var)
51 (interactive "sVariable(s) to solve for: ")
53 (let ((func (if (calc-is-hyperbolic) 'calcFunc-wroot
'calcFunc-root
)))
54 (if (or (equal var
"") (equal var
"$"))
55 (calc-enter-result 2 "root" (list func
59 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var
)
60 (not (string-match "\\[" var
)))
61 (math-read-expr (concat "[" var
"]"))
62 (math-read-expr var
))))
63 (if (eq (car-safe var
) 'error
)
64 (error "Bad format in expression: %s" (nth 1 var
)))
65 (calc-enter-result 1 "root" (list func
70 (defun calc-find-minimum (var)
71 (interactive "sVariable(s) to minimize over: ")
73 (let ((func (if (calc-is-inverse)
74 (if (calc-is-hyperbolic)
75 'calcFunc-wmaximize
'calcFunc-maximize
)
76 (if (calc-is-hyperbolic)
77 'calcFunc-wminimize
'calcFunc-minimize
)))
78 (tag (if (calc-is-inverse) "max" "min")))
79 (if (or (equal var
"") (equal var
"$"))
80 (calc-enter-result 2 tag
(list func
84 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var
)
85 (not (string-match "\\[" var
)))
86 (math-read-expr (concat "[" var
"]"))
87 (math-read-expr var
))))
88 (if (eq (car-safe var
) 'error
)
89 (error "Bad format in expression: %s" (nth 1 var
)))
90 (calc-enter-result 1 tag
(list func
95 (defun calc-find-maximum (var)
96 (interactive "sVariable to maximize over: ")
98 (calc-find-minimum var
))
101 (defun calc-poly-interp (arg)
104 (let ((data (calc-top 2)))
105 (if (or (consp arg
) (eq arg
0) (eq arg
2))
106 (setq data
(cons 'vec
(calc-top-list 2 2)))
108 (error "Bad prefix argument")))
109 (if (calc-is-hyperbolic)
110 (calc-enter-result 1 "rati" (list 'calcFunc-ratint data
(calc-top 1)))
111 (calc-enter-result 1 "poli" (list 'calcFunc-polint data
114 ;; The variables calc-curve-nvars, calc-curve-varnames, calc-curve-model and calc-curve-coefnames are local to calc-curve-fit, but are
115 ;; used by calc-get-fit-variables which is called by calc-curve-fit.
116 (defvar calc-curve-nvars
)
117 (defvar calc-curve-varnames
)
118 (defvar calc-curve-model
)
119 (defvar calc-curve-coefnames
)
121 (defvar calc-curve-fit-history nil
122 "History for calc-curve-fit.")
124 (defun calc-curve-fit (arg &optional calc-curve-model
125 calc-curve-coefnames calc-curve-varnames
)
128 (setq calc-aborted-prefix nil
)
129 (let ((func (if (calc-is-inverse) 'calcFunc-xfit
130 (if (calc-is-hyperbolic) 'calcFunc-efit
135 n calc-curve-nvars temp data
137 (msgs '( "(Press ? for help)"
138 "1 = linear or multilinear"
139 "2-9 = polynomial fits; i = interpolating polynomial"
140 "p = a x^b, ^ = a b^x"
141 "e = a exp(b x), x = exp(a + b x), l = a + b ln(x)"
142 "E = a 10^(b x), X = 10^(a + b x), L = a + b log10(x)"
144 "g = (a/b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)"
145 "s = a/(1 + exp(b (x - c)))"
146 "b = a exp(b (x - c))/(1 + exp(b (x - c)))^2"
147 "o = (y/x) = a (1 - x/b)"
148 "h prefix = homogeneous model (no constant term)"
149 "P prefix = plot result"
150 "' = alg entry, $ = stack, u = Model1, U = Model2")))
151 (while (not calc-curve-model
)
153 "Fit to model: %s:%s%s"
157 (setq key
(read-char))
161 (setq which
(%
(1+ which
) (length msgs
))))
163 (setq homog
(not homog
)))
167 (let ((data (calc-top 1)))
171 (not (= (length data
) 3)))
172 (setq plot
"Can't plot")
181 ((or (consp arg
) (eq arg
0))
184 data
(if (math-matrixp data
)
185 (append data
(list (calc-top (1- n
))))
186 (list 'vec data
(calc-top (1- n
))))))
187 ((> (setq arg
(prefix-numeric-value arg
)) 0)
188 (setq data
(cons 'vec
(calc-top-list arg
(1+ n
)))
190 (t (error "Bad prefix argument")))
191 (or (math-matrixp data
) (not (cdr (cdr data
)))
192 (error "Data matrix is not a matrix!"))
193 (setq calc-curve-nvars
(- (length data
) 2)
194 calc-curve-coefnames nil
195 calc-curve-varnames nil
)
197 ((= key ?
1) ; linear or multilinear
198 (calc-get-fit-variables calc-curve-nvars
199 (1+ calc-curve-nvars
) (and homog
0))
200 (setq calc-curve-model
201 (math-mul calc-curve-coefnames
202 (cons 'vec
(cons 1 (cdr calc-curve-varnames
))))))
203 ((and (>= key ?
2) (<= key ?
9)) ; polynomial
204 (calc-get-fit-variables 1 (- key ?
0 -
1) (and homog
0))
205 (setq calc-curve-model
206 (math-build-polynomial-expr (cdr calc-curve-coefnames
)
207 (nth 1 calc-curve-varnames
))))
208 ((= key ?i
) ; exact polynomial
209 (calc-get-fit-variables 1 (1- (length (nth 1 data
)))
211 (setq calc-curve-model
212 (math-build-polynomial-expr (cdr calc-curve-coefnames
)
213 (nth 1 calc-curve-varnames
))))
214 ((= key ?p
) ; power law
215 (calc-get-fit-variables calc-curve-nvars
216 (1+ calc-curve-nvars
) (and homog
1))
217 (setq calc-curve-model
219 (nth 1 calc-curve-coefnames
)
225 (cons 'vec
(cdr (cdr calc-curve-coefnames
))))))))
226 ((= key ?^
) ; exponential law
227 (calc-get-fit-variables calc-curve-nvars
228 (1+ calc-curve-nvars
) (and homog
1))
229 (setq calc-curve-model
230 (math-mul (nth 1 calc-curve-coefnames
)
235 (cons 'vec
(cdr (cdr calc-curve-coefnames
)))
236 calc-curve-varnames
)))))
239 (setq calc-curve-model t
)
240 (require 'calc-nlfit
)
241 (calc-fit-s-shaped-logistic-curve func
))
244 (setq calc-curve-model t
)
245 (require 'calc-nlfit
)
246 (calc-fit-bell-shaped-logistic-curve func
))
249 (setq calc-curve-model t
)
250 (require 'calc-nlfit
)
251 (if (and plot
(not (stringp plot
)))
257 (math-map-binop 'calcFunc-div
259 (cdr (nth 1 plot
)))))))
260 (calc-fit-hubbert-linear-curve func
))
262 (calc-get-fit-variables calc-curve-nvars
263 (1+ calc-curve-nvars
) (and homog
1))
264 (setq calc-curve-model
265 (math-mul (nth 1 calc-curve-coefnames
)
273 (^
10 (var a var-a
))))
276 (cons 'vec
(cdr (cdr calc-curve-coefnames
)))
277 calc-curve-varnames
))))))
279 (calc-get-fit-variables calc-curve-nvars
280 (1+ calc-curve-nvars
) (and homog
0))
281 (setq calc-curve-model
282 (math-mul calc-curve-coefnames
283 (cons 'vec
(cons 1 (cdr calc-curve-varnames
)))))
284 (setq calc-curve-model
(if (eq key ?x
)
285 (list 'calcFunc-exp calc-curve-model
)
286 (list '^
10 calc-curve-model
))))
288 (calc-get-fit-variables calc-curve-nvars
289 (1+ calc-curve-nvars
) (and homog
0))
290 (setq calc-curve-model
291 (math-mul calc-curve-coefnames
293 (cons 1 (cdr (calcFunc-map
298 calc-curve-varnames
)))))))
300 (calc-get-fit-variables calc-curve-nvars
301 (1+ (* 2 calc-curve-nvars
)) (and homog
0))
302 (let ((c calc-curve-coefnames
)
303 (v calc-curve-varnames
))
304 (setq calc-curve-model
(nth 1 c
))
305 (while (setq v
(cdr v
) c
(cdr (cdr c
)))
306 (setq calc-curve-model
(math-add
311 (list '-
(car v
) (nth 1 c
))
317 "(AFit / BFit sqrt(2 pi)) exp(-0.5 * ((XFit - CFit) / BFit)^2)")
318 calc-curve-varnames
'(vec (var XFit var-XFit
))
319 calc-curve-coefnames
'(vec (var AFit var-AFit
)
321 (var CFit var-CFit
)))
322 (calc-get-fit-variables 1 (1- (length calc-curve-coefnames
))
324 ((memq key
'(?\$ ?
\' ?u ?U
))
328 (let* ((calc-dollar-values calc-arg-values
)
330 (calc-hashes-used 0))
331 (setq calc-curve-model
332 (calc-do-alg-entry "" "Model formula: "
333 nil
'calc-curve-fit-history
))
334 (if (/= (length calc-curve-model
) 1)
335 (error "Bad format"))
336 (setq calc-curve-model
(car calc-curve-model
)
338 (if (> calc-dollar-used
0)
339 (setq calc-curve-coefnames
341 (nthcdr (- (length calc-arg-values
)
343 (reverse calc-arg-values
))))
344 (if (> calc-hashes-used
0)
345 (setq calc-curve-coefnames
346 (cons 'vec
(calc-invent-args
347 calc-hashes-used
))))))
349 (setq calc-curve-model
(cond ((eq key ?u
)
350 (calc-var-value 'var-Model1
))
352 (calc-var-value 'var-Model2
))
354 (or calc-curve-model
(error "User model not yet defined"))
355 (if (math-vectorp calc-curve-model
)
356 (if (and (memq (length calc-curve-model
) '(3 4))
357 (not (math-objvecp (nth 1 calc-curve-model
)))
358 (math-vectorp (nth 2 calc-curve-model
))
359 (or (null (nth 3 calc-curve-model
))
360 (math-vectorp (nth 3 calc-curve-model
))))
361 (setq calc-curve-varnames
(nth 2 calc-curve-model
)
363 (or (nth 3 calc-curve-model
)
367 calc-curve-varnames
)))
368 calc-curve-model
(nth 1 calc-curve-model
))
369 (error "Incorrect model specifier")))))
370 (or calc-curve-varnames
372 (eq (car-safe calc-curve-model
) 'calcFunc-eq
)))
373 (if calc-curve-coefnames
374 (calc-get-fit-variables
375 (if with-y
(1+ calc-curve-nvars
) calc-curve-nvars
)
376 (1- (length calc-curve-coefnames
))
378 calc-curve-model calc-curve-coefnames
)
380 (let* ((coefs (math-all-vars-but calc-curve-model nil
))
388 (error "Not enough variables in model"))
389 (setq p
(nthcdr n coefs
))
392 (calc-get-fit-variables
393 (if with-y
(1+ calc-curve-nvars
) calc-curve-nvars
)
395 vars coefs with-y
)))))
397 (calc-record (list 'vec calc-curve-model
398 calc-curve-varnames calc-curve-coefnames
)
402 (let ((calc-fit-to-trail t
))
403 (calc-enter-result n
(substring (symbol-name func
) 9)
404 (list func calc-curve-model
405 (if (= (length calc-curve-varnames
) 2)
406 (nth 1 calc-curve-varnames
)
408 (if (= (length calc-curve-coefnames
) 2)
409 (nth 1 calc-curve-coefnames
)
410 calc-curve-coefnames
)
412 (if (consp calc-fit-to-trail
)
413 (calc-record (calc-normalize calc-fit-to-trail
) "parm"))))
417 (let ((calc-graph-no-auto-view t
))
418 (calc-graph-delete t
)
419 (calc-graph-add-curve
420 (calc-graph-lookup (nth 1 plot
))
421 (calc-graph-lookup (nth 2 plot
)))
422 (unless (math-contains-sdev-p (nth 2 data
))
423 (calc-graph-set-styles nil nil
)
424 (calc-graph-point-style nil
))
425 (setq plot
(cdr (nth 1 plot
)))
430 (math-min-list (car plot
) (cdr plot
))
434 (math-max-list (car plot
) (cdr plot
)))))
435 (calc-graph-add-curve (calc-graph-lookup plot
)
436 (calc-graph-lookup (calc-top-n 1)))
437 (calc-graph-plot nil
)))))))
439 (defun calc-invent-independent-variables (n &optional but
)
440 (calc-invent-variables n but
'(x y z t
) "x"))
442 (defun calc-invent-parameter-variables (n &optional but
)
443 (calc-invent-variables n but
'(a b c d
) "a"))
445 (defun calc-invent-variables (num but names base
)
449 (while (and (> n
0) names
)
450 (setq var
(math-build-var-name (if (consp names
)
452 (concat base
(int-to-string
453 (setq nn
(1+ nn
)))))))
454 (or (math-expr-contains (cons 'vec but
) var
)
455 (setq vars
(cons var vars
)
457 (or (symbolp names
) (setq names
(cdr names
))))
460 (calc-invent-variables num but t base
))))
462 (defun calc-get-fit-variables (nv nc
&optional defv defc with-y homog
)
463 (or (= nv
(if with-y
(1+ calc-curve-nvars
) calc-curve-nvars
))
464 (error "Wrong number of data vectors for this type of model"))
471 (setq defv
(calc-invent-independent-variables nv
)))
473 (setq defc
(calc-invent-parameter-variables nc defv
)))
474 (let ((vars (read-string (format "Fitting variables (default %s; %s): "
475 (mapconcat 'symbol-name
476 (mapcar (function (lambda (v)
480 (mapconcat 'symbol-name
481 (mapcar (function (lambda (v)
486 (setq vars
(if (string-match "\\[" vars
)
487 (math-read-expr vars
)
488 (math-read-expr (concat "[" vars
"]"))))
489 (if (eq (car-safe vars
) 'error
)
490 (error "Bad format in expression: %s" (nth 2 vars
)))
491 (or (math-vectorp vars
)
492 (error "Expected a variable or vector of variables"))
493 (if (equal vars
'(vec))
494 (setq vars
(cons 'vec defv
)
495 coefs
(cons 'vec defc
))
496 (if (math-vectorp (nth 1 vars
))
497 (if (and (= (length vars
) 3)
498 (math-vectorp (nth 2 vars
)))
499 (setq coefs
(nth 2 vars
)
502 "Expected independent variables vector, then parameters vector"))
503 (setq coefs
(cons 'vec defc
))))
504 (or (= nv
(1- (length vars
)))
505 (and (not with-y
) (= (1+ nv
) (1- (length vars
))))
506 (error "Expected %d independent variable%s" nv
(if (= nv
1) "" "s")))
507 (or (= nc
(1- (length coefs
)))
508 (error "Expected %d parameter variable%s" nc
(if (= nc
1) "" "s")))
510 (setq coefs
(cons 'vec
(cons homog
(cdr coefs
)))))
511 (if calc-curve-varnames
512 (setq calc-curve-model
(math-multi-subst calc-curve-model
(cdr calc-curve-varnames
) (cdr vars
))))
513 (if calc-curve-coefnames
514 (setq calc-curve-model
(math-multi-subst calc-curve-model
(cdr calc-curve-coefnames
) (cdr coefs
))))
515 (setq calc-curve-varnames vars
516 calc-curve-coefnames coefs
)))
521 ;;; The following algorithms are from Numerical Recipes chapter 9.
523 ;;; "rtnewt" with safety kludges
527 (defun math-newton-root (expr deriv guess orig-guess limit
)
528 (math-working "newton" guess
)
529 (let* ((var-DUMMY guess
)
531 (setq next
(math-evaluate-expr expr
)
532 dval
(math-evaluate-expr deriv
))
533 (if (and (Math-numberp next
)
535 (not (Math-zerop dval
)))
537 (setq next
(math-sub guess
(math-div next dval
)))
538 (if (math-nearly-equal guess
(setq next
(math-float next
)))
540 (setq var-DUMMY next
)
541 (list 'vec next
(math-evaluate-expr expr
)))
542 (if (Math-lessp (math-abs-approx (math-sub next orig-guess
))
544 (math-newton-root expr deriv next orig-guess limit
)
545 (math-reject-arg next
"*Newton's method failed to converge"))))
546 (math-reject-arg next
"*Newton's method encountered a singularity"))))
548 ;;; Inspired by "rtsafe"
549 (defun math-newton-search-root (expr deriv guess vguess ostep oostep
551 (let ((var-DUMMY guess
)
555 (math-working "newton" (list 'intv
0 low high
))
556 (math-working "bisect" (list 'intv
0 low high
))
557 (setq ostep
(math-mul-float (math-sub-float high low
)
559 guess
(math-add-float low ostep
)
561 vguess
(math-evaluate-expr expr
))
562 (or (Math-realp vguess
)
564 (setq ostep
(math-mul-float ostep
'(float 6 -
1))
565 guess
(math-add-float low ostep
)
567 vguess
(math-evaluate-expr expr
))
568 (or (math-realp vguess
)
570 (setq ostep
(math-mul-float ostep
'(float 123456 -
5))
571 guess
(math-add-float low ostep
)
575 (setq vguess
(math-evaluate-expr expr
)))
576 (or (Math-realp vguess
)
577 (math-reject-arg guess
"*Newton's method encountered a singularity"))
578 (setq vguess
(math-float vguess
))
579 (if (eq (Math-negp vlow
) (setq pos
(Math-posp vguess
)))
582 (if (eq (Math-negp vhigh
) pos
)
586 (if (or (Math-zerop vguess
)
587 (math-nearly-equal low high
))
588 (list 'vec guess vguess
)
589 (setq step
(math-evaluate-expr deriv
))
590 (if (and (Math-realp step
)
591 (not (Math-zerop step
))
592 (setq step
(math-div-float vguess
(math-float step
))
593 next
(math-sub-float guess step
))
594 (not (math-lessp-float high next
))
595 (not (math-lessp-float next low
)))
598 vnext
(math-evaluate-expr expr
))
599 (if (or (Math-zerop vnext
)
600 (math-nearly-equal next guess
))
601 (list 'vec next vnext
)
603 (math-lessp-float (math-abs (or oostep
607 (math-mul-float '(float 2 0)
609 (math-newton-search-root expr deriv nil nil nil ostep
611 (math-newton-search-root expr deriv next vnext step ostep
612 low vlow high vhigh
))))
613 (if (or (and (Math-posp vlow
) (Math-posp vhigh
))
614 (and (Math-negp vlow
) (Math-negp vhigh
)))
615 (math-search-root expr deriv low vlow high vhigh
)
616 (math-newton-search-root expr deriv nil nil nil ostep
617 low vlow high vhigh
))))))
619 ;;; Search for a root in an interval with no overt zero crossing.
621 ;; The variable math-root-widen is local to math-find-root, but
622 ;; is used by math-search-root, which is called (directly and
623 ;; indirectly) by math-find-root.
624 (defvar math-root-widen
)
626 (defun math-search-root (expr deriv low vlow high vhigh
)
630 (iterlim (if (eq math-root-widen
'point
)
631 (+ calc-internal-prec
10)
633 (factor (if (eq math-root-widen
'point
)
636 (prev nil
) vprev waslow
638 (while (or (and (math-posp vlow
) (math-posp vhigh
))
639 (and (math-negp vlow
) (math-negp vhigh
)))
640 (math-working "widen" (list 'intv
0 low high
))
641 (if (> (setq iters
(1+ iters
)) iterlim
)
642 (math-reject-arg (list 'intv
0 low high
)
643 "*Unable to bracket root"))
644 (if (= iters calc-internal-prec
)
645 (setq factor
'(float 16 -
1)))
646 (setq diff
(math-mul-float (math-sub-float high low
) factor
))
647 (if (Math-zerop diff
)
648 (setq high
(calcFunc-incr high
10))
649 (if (math-lessp-float (math-abs vlow
) (math-abs vhigh
))
652 low
(math-sub low diff
)
655 vlow
(math-evaluate-expr expr
))
658 high
(math-add high diff
)
661 vhigh
(math-evaluate-expr expr
)))))
664 (setq high prev vhigh vprev
)
665 (setq low prev vlow vprev
)))
667 (or (Math-realp vlow
)
668 (math-reject-arg vlow
'realp
))
669 (or (Math-realp vhigh
)
670 (math-reject-arg vhigh
'realp
))
671 (let ((xvals (list low high
))
672 (yvals (list vlow vhigh
))
673 (pos (Math-posp vlow
))
675 (step (math-sub-float high low
))
677 (while (and (<= (setq levels
(1+ levels
)) 5)
681 step
(math-mul-float step
'(float 497 -
3)))
682 (while (and (cdr xp
) (not found
))
683 (if (Math-realp (car yp
))
686 (setq high
(math-add-float (car xp
) step
)
688 vhigh
(math-evaluate-expr expr
))
689 (math-working "search" high
)
690 (if (and (Math-realp vhigh
)
691 (eq (math-negp vhigh
) pos
))
693 (setcdr xp
(cons high
(cdr xp
)))
694 (setcdr yp
(cons vhigh
(cdr yp
)))
695 (setq xp
(cdr (cdr xp
))
696 yp
(cdr (cdr yp
))))))))
698 (if (Math-zerop vhigh
)
699 (list 'vec high vhigh
)
700 (if (Math-zerop vlow
)
703 (math-newton-search-root expr deriv nil nil nil nil
705 (math-bisect-root expr low vlow high vhigh
))))
706 (math-reject-arg (list 'intv
3 low high
)
707 "*Unable to find a sign change in this interval"))))
709 ;;; "rtbis" (but we should be using Brent's method)
710 (defun math-bisect-root (expr low vlow high vhigh
)
711 (let ((step (math-sub-float high low
))
712 (pos (Math-posp vhigh
))
715 (while (not (or (math-nearly-equal low
716 (setq step
(math-mul-float
718 mid
(math-add-float low step
)))
721 vmid
(math-evaluate-expr expr
))
723 (math-working "bisect" mid
)
724 (if (eq (Math-posp vmid
) pos
)
729 (list 'vec mid vmid
)))
733 (defvar math-root-vars
[(var DUMMY var-DUMMY
)])
735 (defun math-newton-multi (expr jacob n guess orig-guess limit
)
738 p2 expr-val jacob-val next
)
739 (while (< (setq p
(cdr p
) m
(1+ m
)) n
)
740 (set (nth 2 (aref math-root-vars m
)) (car p
)))
741 (setq expr-val
(math-evaluate-expr expr
)
742 jacob-val
(math-evaluate-expr jacob
))
743 (unless (and (math-constp expr-val
)
744 (math-constp jacob-val
))
745 (math-reject-arg guess
"*Newton's method encountered a singularity"))
746 (setq next
(math-add guess
(math-div (math-float (math-neg expr-val
))
747 (math-float jacob-val
)))
749 (math-working "newton" next
)
750 (while (and (setq p
(cdr p
) p2
(cdr p2
))
751 (math-nearly-equal (car p
) (car p2
))))
753 (if (Math-lessp (math-abs-approx (math-sub next orig-guess
))
755 (math-newton-multi expr jacob n next orig-guess limit
)
756 (math-reject-arg nil
"*Newton's method failed to converge"))
757 (list 'vec next expr-val
))))
760 (defun math-find-root (expr var guess math-root-widen
)
761 (if (eq (car-safe expr
) 'vec
)
762 (let ((n (1- (length expr
)))
763 (calc-symbolic-mode nil
)
767 (unless (eq (car-safe var
) 'vec
)
768 (math-reject-arg var
'vectorp
))
769 (unless (= (length var
) (1+ n
))
770 (math-dimension-error))
771 (setq expr
(copy-sequence expr
))
772 (while (>= n
(length math-root-vars
))
773 (let ((symb (intern (concat "math-root-v"
775 (length math-root-vars
))))))
776 (setq math-root-vars
(vconcat math-root-vars
777 (vector (list 'var symb symb
))))))
779 (while (< (setq m
(1+ m
)) n
)
780 (set (nth 2 (aref math-root-vars m
)) nil
))
782 (while (setq m
(1+ m
) p
(cdr p
))
783 (or (eq (car-safe (car p
)) 'var
)
784 (math-reject-arg var
"*Expected a variable"))
786 (while (setq p2
(cdr p2
))
787 (setcar p2
(math-expr-subst (car p2
) (car p
)
788 (aref math-root-vars m
)))))
789 (unless (eq (car-safe guess
) 'vec
)
790 (math-reject-arg guess
'vectorp
))
791 (unless (= (length guess
) (1+ n
))
792 (math-dimension-error))
793 (setq guess
(copy-sequence guess
)
795 (while (setq p
(cdr p
))
796 (or (Math-numberp (car guess
))
797 (math-reject-arg guess
'numberp
))
798 (setcar p
(math-float (car p
))))
800 (while (setq p
(cdr p
))
801 (if (assq (car-safe (car p
)) calc-tweak-eqn-table
)
802 (setcar p
(math-sub (nth 1 (car p
)) (nth 2 (car p
)))))
803 (setcar p
(math-evaluate-expr (car p
)))
804 (setq row
(list 'vec
)
806 (while (< (setq m
(1+ m
)) n
)
807 (nconc row
(list (math-evaluate-expr
808 (or (calcFunc-deriv (car p
)
809 (aref math-root-vars m
)
813 "*Formulas must be differentiable"))))))
814 (nconc jacob
(list row
)))
815 (setq m
(math-abs-approx guess
))
816 (math-newton-multi expr jacob n guess guess
817 (if (math-zerop m
) '(float 1 3) (math-mul m
10))))
818 (unless (eq (car-safe var
) 'var
)
819 (math-reject-arg var
"*Expected a variable"))
820 (unless (math-expr-contains expr var
)
821 (math-reject-arg expr
"*Formula does not contain specified variable"))
822 (if (assq (car expr
) calc-tweak-eqn-table
)
823 (setq expr
(math-sub (nth 1 expr
) (nth 2 expr
))))
824 (math-with-extra-prec 2
825 (setq expr
(math-expr-subst expr var
'(var DUMMY var-DUMMY
)))
826 (let* ((calc-symbolic-mode nil
)
828 (expr (math-evaluate-expr expr
))
829 (deriv (calcFunc-deriv expr
'(var DUMMY var-DUMMY
) nil t
))
831 (and deriv
(setq deriv
(math-evaluate-expr deriv
)))
832 (setq guess
(math-float guess
))
833 (if (and (math-numberp guess
)
835 (math-newton-root expr deriv guess guess
836 (if (math-zerop guess
) '(float 1 6)
837 (math-mul (math-abs-approx guess
) 100)))
838 (if (Math-realp guess
)
842 vlow
(math-evaluate-expr expr
)
844 math-root-widen
'point
)
845 (if (eq (car guess
) 'intv
)
847 (or (math-constp guess
) (math-reject-arg guess
'constp
))
848 (setq low
(nth 2 guess
)
850 (if (memq (nth 1 guess
) '(0 1))
851 (setq low
(calcFunc-incr low
1 high
)))
852 (if (memq (nth 1 guess
) '(0 2))
853 (setq high
(calcFunc-incr high -
1 low
)))
855 vlow
(math-evaluate-expr expr
)
857 vhigh
(math-evaluate-expr expr
)))
858 (if (math-complexp guess
)
859 (math-reject-arg "*Complex root finder must have derivative")
860 (math-reject-arg guess
'realp
))))
861 (if (Math-zerop vlow
)
863 (if (Math-zerop vhigh
)
864 (list 'vec high vhigh
)
865 (if (and deriv
(Math-numberp vlow
) (Math-numberp vhigh
))
866 (math-newton-search-root expr deriv nil nil nil nil
868 (if (or (and (Math-posp vlow
) (Math-posp vhigh
))
869 (and (Math-negp vlow
) (Math-negp vhigh
))
870 (not (Math-numberp vlow
))
871 (not (Math-numberp vhigh
)))
872 (math-search-root expr deriv low vlow high vhigh
)
873 (math-bisect-root expr low vlow high vhigh
))))))))))
875 (defun calcFunc-root (expr var guess
)
876 (math-find-root expr var guess nil
))
878 (defun calcFunc-wroot (expr var guess
)
879 (math-find-root expr var guess t
))
884 ;;; The following algorithms come from Numerical Recipes, chapter 10.
886 (defvar math-min-vars
[(var DUMMY var-DUMMY
)])
888 (defun math-min-eval (expr a
)
891 (while (setq m
(1+ m
) a
(cdr a
))
892 (set (nth 2 (aref math-min-vars m
)) (car a
))))
894 (setq a
(math-evaluate-expr expr
))
897 (if (eq (car a
) 'float
)
899 (math-reject-arg a
'realp
))))
901 (defvar math-min-or-max
"minimum")
903 ;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c).
906 (defun math-widen-min (expr a b
)
909 incr c va vb vc u vu r q ulim bc ba qr
)
910 (or b
(setq b
(math-mul a
'(float 101 -
2))))
911 (setq va
(math-min-eval expr a
)
912 vb
(math-min-eval expr b
))
913 (if (math-lessp-float va vb
)
916 (setq c
(math-add-float b
(math-mul-float '(float 161803 -
5)
917 (math-sub-float b a
)))
918 vc
(math-min-eval expr c
))
919 (while (and (not done
) (math-lessp-float vc vb
))
920 (math-working "widen" (list 'intv
0 a c
))
921 (if (= (setq iters
(1- iters
)) 0)
922 (math-reject-arg nil
(format "*Unable to find a %s near the interval"
924 (setq bc
(math-sub-float b c
)
925 ba
(math-sub-float b a
)
926 r
(math-mul-float ba
(math-sub-float vb vc
))
927 q
(math-mul-float bc
(math-sub-float vb va
))
928 qr
(math-sub-float q r
))
929 (if (math-lessp-float (math-abs qr
) '(float 1 -
20))
930 (setq qr
(if (math-negp qr
) '(float -
1 -
20) '(float 1 -
20))))
931 (setq u
(math-sub-float
933 (math-div-float (math-sub-float (math-mul-float bc q
)
934 (math-mul-float ba r
))
935 (math-mul-float '(float 2 0) qr
)))
936 ulim
(math-add-float b
(math-mul-float '(float -
1 2) bc
))
938 (if (if incr
(math-lessp-float b u
) (math-lessp-float u b
))
939 (if (if incr
(math-lessp-float u c
) (math-lessp-float c u
))
940 (if (math-lessp-float (setq vu
(math-min-eval expr u
)) vc
)
944 (if (math-lessp-float vb vu
)
947 (setq u
(math-add-float c
(math-mul-float '(float -
161803 -
5)
949 vu
(math-min-eval expr u
))))
950 (if (if incr
(math-lessp-float u ulim
) (math-lessp-float ulim u
))
951 (if (math-lessp-float (setq vu
(math-min-eval expr u
)) vc
)
954 u
(math-add-float c
(math-mul-float
956 (math-sub-float b c
)))
957 vu
(math-min-eval expr u
)))
959 vu
(math-min-eval expr u
))))
960 (setq u
(math-add-float c
(math-mul-float '(float -
161803 -
5)
962 vu
(math-min-eval expr u
)))
966 (if (math-lessp-float a c
)
967 (list a va b vb c vc
)
968 (list c vc b vb a va
))))
970 (defun math-narrow-min (expr a c intv
)
971 (let ((xvals (list a c
))
972 (yvals (list (math-min-eval expr a
)
973 (math-min-eval expr c
)))
975 (step (math-sub-float c a
))
978 (while (and (<= (setq levels
(1+ levels
)) 5)
982 step
(math-mul-float step
'(float 497 -
3)))
983 (while (and (cdr xp
) (not found
))
984 (setq b
(math-add-float (car xp
) step
))
985 (math-working "search" b
)
986 (setcdr xp
(cons b
(cdr xp
)))
987 (setcdr yp
(cons (math-min-eval expr b
) (cdr yp
)))
988 (if (and (math-lessp-float (nth 1 yp
) (car yp
))
989 (math-lessp-float (nth 1 yp
) (nth 2 yp
)))
993 (if (and (cdr (cdr yp
))
994 (math-lessp-float (nth 1 yp
) (car yp
))
995 (math-lessp-float (nth 1 yp
) (nth 2 yp
)))
1000 (list (car xp
) (car yp
)
1001 (nth 1 xp
) (nth 1 yp
)
1002 (nth 2 xp
) (nth 2 yp
))
1003 (or (if (math-lessp-float (car yvals
) (nth 1 yvals
))
1004 (and (memq (nth 1 intv
) '(2 3))
1005 (let ((min (car yvals
)))
1006 (while (and (setq yvals
(cdr yvals
))
1007 (math-lessp-float min
(car yvals
))))
1009 (list (nth 2 intv
) min
))))
1010 (and (memq (nth 1 intv
) '(1 3))
1011 (setq yvals
(nreverse yvals
))
1012 (let ((min (car yvals
)))
1013 (while (and (setq yvals
(cdr yvals
))
1014 (math-lessp-float min
(car yvals
))))
1016 (list (nth 3 intv
) min
)))))
1017 (math-reject-arg nil
(format "*Unable to find a %s in the interval"
1018 math-min-or-max
))))))
1021 (defun math-brent-min (expr prec a va x vx b vb
)
1022 (let ((iters (+ 20 (* 5 prec
)))
1027 (tol (list 'float
1 (- -
1 prec
)))
1028 (zeps (list 'float
1 (- -
5 prec
)))
1030 d u vu xm tol1 tol2 etemp p q r xv xw
)
1032 (setq xm
(math-mul-float '(float 5 -
1)
1033 (math-add-float a b
))
1034 tol1
(math-add-float
1036 (math-mul-float tol
(math-abs x
)))
1037 tol2
(math-mul-float tol1
'(float 2 0)))
1038 (math-lessp-float (math-sub-float tol2
1041 (math-sub-float b a
)))
1042 (math-abs (math-sub-float x xm
))))
1043 (if (= (setq iters
(1- iters
)) 0)
1044 (math-reject-arg nil
(format "*Unable to converge on a %s"
1046 (math-working "brent" x
)
1047 (if (math-lessp-float (math-abs e
) tol1
)
1048 (setq e
(if (math-lessp-float x xm
)
1049 (math-sub-float b x
)
1050 (math-sub-float a x
))
1051 d
(math-mul-float '(float 381966 -
6) e
))
1052 (setq xw
(math-sub-float x w
)
1053 r
(math-mul-float xw
(math-sub-float vx vv
))
1054 xv
(math-sub-float x v
)
1055 q
(math-mul-float xv
(math-sub-float vx vw
))
1056 p
(math-sub-float (math-mul-float xv q
)
1057 (math-mul-float xw r
))
1058 q
(math-mul-float '(float 2 0) (math-sub-float q r
)))
1060 (setq p
(math-neg-float p
))
1061 (setq q
(math-neg-float q
)))
1064 (if (and (math-lessp-float (math-abs p
)
1065 (math-abs (math-mul-float
1067 (math-mul-float q etemp
))))
1068 (math-lessp-float (math-mul-float
1069 q
(math-sub-float a x
)) p
)
1070 (math-lessp-float p
(math-mul-float
1071 q
(math-sub-float b x
))))
1073 (setq d
(math-div-float p q
)
1074 u
(math-add-float x d
))
1075 (if (or (math-lessp-float (math-sub-float u a
) tol2
)
1076 (math-lessp-float (math-sub-float b u
) tol2
))
1077 (setq d
(if (math-lessp-float xm x
)
1078 (math-neg-float tol1
)
1080 (setq e
(if (math-lessp-float x xm
)
1081 (math-sub-float b x
)
1082 (math-sub-float a x
))
1083 d
(math-mul-float '(float 381966 -
6) e
))))
1084 (setq u
(math-add-float x
1085 (if (math-lessp-float (math-abs d
) tol1
)
1087 (math-neg-float tol1
)
1090 vu
(math-min-eval expr u
))
1091 (if (math-lessp-float vx vu
)
1093 (if (math-lessp-float u x
)
1097 (not (math-lessp-float vw vu
)))
1102 (not (math-lessp-float vv vu
)))
1104 (if (math-lessp-float u x
)
1113 (defun math-powell-min (expr n guesses prec
)
1114 (let* ((f1dim (math-line-min-func expr n
))
1115 (xi (calcFunc-idn 1 n
))
1116 (p (cons 'vec
(mapcar 'car guesses
)))
1118 (ftol (list 'float
1 (- prec
)))
1119 (fret (math-min-eval expr p
))
1120 fp ptt fptt xit i ibig del diff res
)
1126 (while (<= (setq i
(1+ i
)) n
)
1128 res
(math-line-min f1dim p
1131 p
(let ((calc-internal-prec prec
))
1132 (math-normalize (car res
)))
1134 diff
(math-abs (math-sub-float fptt fret
)))
1135 (if (math-lessp-float del diff
)
1139 (math-mul-float ftol
1140 (math-add-float (math-abs fp
)
1142 (math-mul-float '(float 2 0)
1143 (math-abs (math-sub-float fp
1145 (setq ptt
(math-sub (math-mul '(float 2 0) p
) pt
)
1148 fptt
(math-min-eval expr ptt
))
1149 (if (and (math-lessp-float fptt fp
)
1152 (math-mul-float '(float 2 0)
1155 (math-mul-float '(float 2 0)
1158 (math-sqr-float (math-sub-float
1159 (math-sub-float fp fret
) del
)))
1161 (math-sqr-float (math-sub-float fp fptt
)))))
1163 (setq res
(math-line-min f1dim p xit n prec
)
1167 (while (<= (setq i
(1+ i
)) n
)
1168 (setcar (nthcdr ibig
(nth i xi
))
1169 (nth i
(nth 1 res
)))))))
1170 (list 'vec p fret
)))
1172 (defun math-line-min-func (expr n
)
1174 (while (< (setq m
(1+ m
)) n
)
1175 (set (nth 2 (aref math-min-vars m
))
1178 '(var DUMMY var-DUMMY
)
1179 (list 'calcFunc-mrow
'(var line-xi line-xi
) (1+ m
)))
1180 (list 'calcFunc-mrow
'(var line-p line-p
) (1+ m
)))))
1181 (math-evaluate-expr expr
)))
1183 (defun math-line-min (f1dim line-p line-xi n prec
)
1184 (let* ((var-DUMMY nil
)
1185 (expr (math-evaluate-expr f1dim
))
1186 (params (math-widen-min expr
'(float 0 0) '(float 1 0)))
1187 (res (apply 'math-brent-min expr prec params
))
1188 (xi (math-mul (nth 1 res
) line-xi
)))
1189 (list (math-add line-p xi
) xi
(nth 2 res
))))
1192 (defun math-find-minimum (expr var guess min-widen
)
1193 (let* ((calc-symbolic-mode nil
)
1196 (isvec (math-vectorp var
))
1198 (or (math-vectorp var
)
1199 (setq var
(list 'vec var
)))
1200 (or (math-vectorp guess
)
1201 (setq guess
(list 'vec guess
)))
1202 (or (= (length var
) (length guess
))
1203 (math-dimension-error))
1204 (while (setq var
(cdr var
) guess
(cdr guess
))
1205 (or (eq (car-safe (car var
)) 'var
)
1206 (math-reject-arg (car var
) "*Expected a variable"))
1207 (or (math-expr-contains expr
(car var
))
1208 (math-reject-arg (car var
)
1209 "*Formula does not contain specified variable"))
1210 (while (>= (1+ n
) (length math-min-vars
))
1211 (let ((symb (intern (concat "math-min-v"
1213 (length math-min-vars
))))))
1214 (setq math-min-vars
(vconcat math-min-vars
1215 (vector (list 'var symb symb
))))))
1216 (set (nth 2 (aref math-min-vars n
)) nil
)
1217 (set (nth 2 (aref math-min-vars
(1+ n
))) nil
)
1218 (if (math-complexp (car guess
))
1219 (setq expr
(math-expr-subst expr
1221 (list '+ (aref math-min-vars n
)
1223 (aref math-min-vars
(1+ n
))
1225 guesses
(let ((g (math-float (math-complex (car guess
)))))
1226 (cons (list (nth 2 g
) nil nil
)
1227 (cons (list (nth 1 g
) nil nil t
)
1230 (setq expr
(math-expr-subst expr
1232 (aref math-min-vars n
))
1233 guesses
(cons (if (math-realp (car guess
))
1234 (list (math-float (car guess
)) nil nil
)
1235 (if (and (eq (car-safe (car guess
)) 'intv
)
1236 (math-constp (car guess
)))
1238 (math-add (nth 2 (car guess
))
1239 (nth 3 (car guess
)))
1241 (math-float (nth 2 (car guess
)))
1242 (math-float (nth 3 (car guess
)))
1244 (math-reject-arg (car guess
) 'realp
)))
1247 (setq guesses
(nreverse guesses
)
1248 expr
(math-evaluate-expr expr
))
1250 (let* ((params (if (nth 1 (car guesses
))
1252 (math-widen-min expr
1253 (nth 1 (car guesses
))
1254 (nth 2 (car guesses
)))
1255 (math-narrow-min expr
1256 (nth 1 (car guesses
))
1257 (nth 2 (car guesses
))
1258 (nth 3 (car guesses
))))
1259 (math-widen-min expr
1262 (prec calc-internal-prec
)
1263 (res (if (cdr (cdr params
))
1264 (math-with-extra-prec (+ calc-internal-prec
2)
1265 (apply 'math-brent-min expr prec params
))
1266 (cons 'vec params
))))
1268 (list 'vec
(list 'vec
(nth 1 res
)) (nth 2 res
))
1270 (let* ((prec calc-internal-prec
)
1271 (res (math-with-extra-prec (+ calc-internal-prec
2)
1272 (math-powell-min expr n guesses prec
)))
1275 (while (setq p
(cdr p
))
1276 (if (nth 3 (car guesses
))
1278 (nconc vec
(list (math-normalize
1279 (list 'cplx
(car p
) (nth 1 p
)))))
1281 guesses
(cdr guesses
)))
1282 (nconc vec
(list (car p
))))
1283 (setq guesses
(cdr guesses
)))
1285 (list 'vec vec
(nth 2 res
))
1286 (list 'vec
(nth 1 vec
) (nth 2 res
)))))))
1288 (defun calcFunc-minimize (expr var guess
)
1289 (let ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1290 (math-min-or-max "minimum"))
1291 (math-find-minimum (math-normalize expr
)
1292 (math-normalize var
)
1293 (math-normalize guess
) nil
)))
1295 (defun calcFunc-wminimize (expr var guess
)
1296 (let ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1297 (math-min-or-max "minimum"))
1298 (math-find-minimum (math-normalize expr
)
1299 (math-normalize var
)
1300 (math-normalize guess
) t
)))
1302 (defun calcFunc-maximize (expr var guess
)
1303 (let* ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1304 (math-min-or-max "maximum")
1305 (res (math-find-minimum (math-normalize (math-neg expr
))
1306 (math-normalize var
)
1307 (math-normalize guess
) nil
)))
1308 (list 'vec
(nth 1 res
) (math-neg (nth 2 res
)))))
1310 (defun calcFunc-wmaximize (expr var guess
)
1311 (let* ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1312 (math-min-or-max "maximum")
1313 (res (math-find-minimum (math-normalize (math-neg expr
))
1314 (math-normalize var
)
1315 (math-normalize guess
) t
)))
1316 (list 'vec
(nth 1 res
) (math-neg (nth 2 res
)))))
1321 ;;; The following algorithms come from Numerical Recipes, chapter 3.
1323 (defun calcFunc-polint (data x
)
1324 (or (math-matrixp data
) (math-reject-arg data
'matrixp
))
1325 (or (= (length data
) 3)
1326 (math-reject-arg data
"*Wrong number of data rows"))
1327 (or (> (length (nth 1 data
)) 2)
1328 (math-reject-arg data
"*Too few data points"))
1329 (if (and (math-vectorp x
) (or (math-constp x
) math-expand-formulas
))
1330 (cons 'vec
(mapcar (function (lambda (x) (calcFunc-polint data x
)))
1332 (or (math-objectp x
) math-expand-formulas
(math-reject-arg x
'objectp
))
1333 (math-with-extra-prec 2
1334 (cons 'vec
(math-poly-interp (cdr (nth 1 data
)) (cdr (nth 2 data
)) x
1336 (put 'calcFunc-polint
'math-expandable t
)
1339 (defun calcFunc-ratint (data x
)
1340 (or (math-matrixp data
) (math-reject-arg data
'matrixp
))
1341 (or (= (length data
) 3)
1342 (math-reject-arg data
"*Wrong number of data rows"))
1343 (or (> (length (nth 1 data
)) 2)
1344 (math-reject-arg data
"*Too few data points"))
1345 (if (and (math-vectorp x
) (or (math-constp x
) math-expand-formulas
))
1346 (cons 'vec
(mapcar (function (lambda (x) (calcFunc-ratint data x
)))
1348 (or (math-objectp x
) math-expand-formulas
(math-reject-arg x
'objectp
))
1349 (math-with-extra-prec 2
1350 (cons 'vec
(math-poly-interp (cdr (nth 1 data
)) (cdr (nth 2 data
)) x
1351 (cdr (cdr (cdr (nth 1 data
)))))))))
1352 (put 'calcFunc-ratint
'math-expandable t
)
1355 (defun math-poly-interp (xa ya x ratp
)
1356 (let ((n (length xa
))
1360 (c (copy-sequence ya
))
1361 (d (copy-sequence ya
))
1364 y dy
(xp xa
) xpm cp dp temp
)
1365 (while (<= (setq i
(1+ i
)) n
)
1366 (setq xax
(cons (math-sub (car xp
) x
) xax
)
1368 temp
(math-abs (car xax
)))
1369 (if (or (null dif
) (math-lessp temp dif
))
1372 (setq xax
(nreverse xax
)
1375 (if (math-zerop dif
)
1377 (while (< (setq m
(1+ m
)) n
)
1383 (while (<= (setq i
(1+ i
)) (- n m
))
1385 (let ((t2 (math-div (math-mul (car xp
) (car dp
)) (car xpm
))))
1386 (setq temp
(math-div (math-sub (nth 1 cp
) (car dp
))
1387 (math-sub t2
(nth 1 cp
))))
1388 (setcar dp
(math-mul (nth 1 cp
) temp
))
1389 (setcar cp
(math-mul t2 temp
)))
1390 (if (math-equal (car xp
) (car xpm
))
1391 (math-reject-arg (cons 'vec xa
) "*Duplicate X values"))
1392 (setq temp
(math-div (math-sub (nth 1 cp
) (car dp
))
1393 (math-sub (car xp
) (car xpm
))))
1394 (setcar dp
(math-mul (car xpm
) temp
))
1395 (setcar cp
(math-mul (car xp
) temp
)))
1400 (if (< (+ ns ns
) (- n m
))
1401 (setq dy
(nth ns c
))
1404 (setq y
(math-add y dy
)))
1409 ;;; The following algorithms come from Numerical Recipes, chapter 4.
1411 (defun calcFunc-ninteg (expr var lo hi
)
1412 (setq lo
(math-evaluate-expr lo
)
1413 hi
(math-evaluate-expr hi
))
1414 (or (math-numberp lo
) (math-infinitep lo
) (math-reject-arg lo
'numberp
))
1415 (or (math-numberp hi
) (math-infinitep hi
) (math-reject-arg hi
'numberp
))
1416 (if (math-lessp hi lo
)
1417 (math-neg (calcFunc-ninteg expr var hi lo
))
1418 (setq expr
(math-expr-subst expr var
'(var DUMMY var-DUMMY
)))
1419 (let ((var-DUMMY nil
)
1420 (calc-symbolic-mode nil
)
1421 (calc-prefer-frac nil
)
1423 (setq expr
(math-evaluate-expr expr
))
1424 (if (equal lo
'(neg (var inf var-inf
)))
1425 (let ((thi (if (math-lessp hi
'(float -
2 0))
1427 (setq sum
(math-ninteg-romberg
1428 'math-ninteg-midpoint expr
1429 (math-float lo
) (math-float thi
) 'inf
)
1431 (if (equal hi
'(var inf var-inf
))
1432 (let ((tlo (if (math-lessp '(float 2 0) lo
)
1434 (setq sum
(math-add sum
1435 (math-ninteg-romberg
1436 'math-ninteg-midpoint expr
1437 (math-float tlo
) (math-float hi
) 'inf
))
1439 (or (math-equal lo hi
)
1440 (setq sum
(math-add sum
1441 (math-ninteg-romberg
1442 'math-ninteg-midpoint expr
1443 (math-float lo
) (math-float hi
) nil
))))
1447 ;;; Open Romberg method; "qromo" in section 4.4.
1449 ;; The variable math-ninteg-temp is local to math-ninteg-romberg,
1450 ;; but is used by math-ninteg-midpoint, which is used by
1451 ;; math-ninteg-romberg.
1452 (defvar math-ninteg-temp
)
1454 (defun math-ninteg-romberg (func expr lo hi mode
)
1455 (let ((curh '(float 1 0))
1460 (prec calc-internal-prec
)
1461 (math-ninteg-temp nil
))
1462 (math-with-extra-prec 2
1463 ;; Limit on "j" loop must be 14 or less to keep "it" from overflowing.
1464 (or (while (and (null ss
) (<= (setq j
(1+ j
)) 8))
1465 (setq s
(nconc s
(list (funcall func expr lo hi mode
)))
1466 h
(nconc h
(list curh
)))
1468 (let ((res (math-poly-interp h s
'(float 0 0) nil
)))
1469 (if (math-lessp (math-abs (nth 1 res
))
1470 (calcFunc-scf (math-abs (car res
))
1472 (setq ss
(car res
)))))
1476 (setq curh
(math-div-float curh
'(float 9 0))))
1478 (math-reject-arg nil
(format "*Integral failed to converge"))))))
1481 (defun math-ninteg-evaluate (expr x mode
)
1483 (setq x
(math-div '(float 1 0) x
)))
1484 (let* ((var-DUMMY x
)
1485 (res (math-evaluate-expr expr
)))
1486 (or (Math-numberp res
)
1487 (math-reject-arg res
"*Integrand does not evaluate to a number"))
1489 (setq res
(math-mul res
(math-sqr x
))))
1493 (defun math-ninteg-midpoint (expr lo hi mode
) ; uses "math-ninteg-temp"
1495 (let ((math-infinite-mode t
) temp
)
1496 (setq temp
(math-div 1 lo
)
1499 (if math-ninteg-temp
1500 (let* ((it3 (* 3 (car math-ninteg-temp
)))
1501 (math-working-step-2 (* 2 (car math-ninteg-temp
)))
1502 (math-working-step 0)
1503 (range (math-sub hi lo
))
1504 (del (math-div range
(math-float it3
)))
1505 (del2 (math-add del del
))
1506 (del3 (math-add del del2
))
1507 (x (math-add lo
(math-mul '(float 5 -
1) del
)))
1510 (while (<= (setq j
(1+ j
)) (car math-ninteg-temp
))
1511 (setq math-working-step
(1+ math-working-step
)
1512 temp
(math-ninteg-evaluate expr x mode
)
1513 math-working-step
(1+ math-working-step
)
1514 sum
(math-add sum
(math-add temp
(math-ninteg-evaluate
1515 expr
(math-add x del2
)
1517 x
(math-add x del3
)))
1518 (setq math-ninteg-temp
(list it3
1519 (math-add (math-div (nth 1 math-ninteg-temp
)
1521 (math-mul sum del
)))))
1522 (setq math-ninteg-temp
(list 1 (math-mul
1524 (math-ninteg-evaluate
1526 (math-mul (math-add lo hi
) '(float 5 -
1))
1528 (nth 1 math-ninteg-temp
))
1534 ;;; The following algorithms come from Numerical Recipes, chapter 14.
1536 (defvar math-dummy-vars
[(var DUMMY var-DUMMY
)])
1537 (defvar math-dummy-counter
0)
1538 (defun math-dummy-variable ()
1539 (if (= math-dummy-counter
(length math-dummy-vars
))
1540 (let ((symb (intern (format "math-dummy-%d" math-dummy-counter
))))
1541 (setq math-dummy-vars
(vconcat math-dummy-vars
1542 (vector (list 'var symb symb
))))))
1543 (set (nth 2 (aref math-dummy-vars math-dummy-counter
)) nil
)
1545 (aref math-dummy-vars math-dummy-counter
)
1546 (setq math-dummy-counter
(1+ math-dummy-counter
))))
1548 (defvar math-in-fit
0)
1549 (defvar calc-fit-to-trail nil
)
1551 (defun calcFunc-fit (expr vars
&optional coefs data
)
1552 (let ((math-in-fit 10))
1553 (math-with-extra-prec 2
1554 (math-general-fit expr vars coefs data nil
))))
1556 (defun calcFunc-efit (expr vars
&optional coefs data
)
1557 (let ((math-in-fit 10))
1558 (math-with-extra-prec 2
1559 (math-general-fit expr vars coefs data
'sdev
))))
1561 (defun calcFunc-xfit (expr vars
&optional coefs data
)
1562 (let ((math-in-fit 10))
1563 (math-with-extra-prec 2
1564 (math-general-fit expr vars coefs data
'full
))))
1566 ;; The variables math-fit-first-var, math-fit-first-coef and
1567 ;; math-fit-new-coefs are local to math-general-fit, but are used by
1568 ;; calcFunc-fitvar, calcFunc-fitparam and calcFunc-fitdummy
1569 ;; (respectively), which are used by math-general-fit.
1570 (defvar math-fit-first-var
)
1571 (defvar math-fit-first-coef
)
1572 (defvar math-fit-new-coefs
)
1574 (defun math-general-fit (expr vars coefs data mode
)
1575 (let ((calc-simplify-mode nil
)
1576 (math-dummy-counter math-dummy-counter
)
1578 (extended (eq mode
'full
))
1579 (math-fit-first-coef math-dummy-counter
)
1583 have-sdevs need-chisq chisq
1591 (var-YVAL nil
) (var-YVALX nil
)
1593 n nn m mm v dummy p
)
1595 ;; Validate and parse arguments.
1600 (if (math-vectorp expr
)
1601 (if (memq (length expr
) '(3 4))
1606 (math-dimension-error))
1610 (or (math-matrixp data
) (math-reject-arg data
'matrixp
))
1611 (setq v
(1- (length data
))
1612 n
(1- (length (nth 1 data
))))
1613 (or (math-vectorp vars
) (null vars
)
1614 (setq vars
(list 'vec vars
)))
1615 (or (math-vectorp coefs
) (null coefs
)
1616 (setq coefs
(list 'vec coefs
)))
1618 (setq coefs
(cons 'vec
(math-all-vars-but expr vars
))))
1620 (if (<= (1- (length coefs
)) v
)
1621 (math-reject-arg coefs
"*Not enough variables in model")
1622 (setq coefs
(copy-sequence coefs
))
1623 (let ((p (nthcdr (- (length coefs
) v
1624 (if (eq (car-safe expr
) 'calcFunc-eq
) 1 0))
1626 (setq vars
(cons 'vec
(cdr p
)))
1628 (or (= (1- (length vars
)) v
)
1630 (math-reject-arg vars
"*Number of variables does not match data"))
1631 (setq m
(1- (length coefs
)))
1633 (math-reject-arg coefs
"*Need at least one parameter"))
1635 ;; Rewrite expr in terms of fitparam and fitvar, make into an equation.
1637 (while (setq p
(cdr p
))
1638 (or (eq (car-safe (car p
)) 'var
)
1639 (math-reject-arg (car p
) "*Expected a variable"))
1640 (setq dummy
(math-dummy-variable)
1641 expr
(math-expr-subst expr
(car p
)
1642 (list 'calcFunc-fitparam
1643 (- math-dummy-counter math-fit-first-coef
)))))
1644 (setq math-fit-first-var math-dummy-counter
1646 (while (setq p
(cdr p
))
1647 (or (eq (car-safe (car p
)) 'var
)
1648 (math-reject-arg (car p
) "*Expected a variable"))
1649 (setq dummy
(math-dummy-variable)
1650 expr
(math-expr-subst expr
(car p
)
1651 (list 'calcFunc-fitvar
1652 (- math-dummy-counter math-fit-first-var
)))))
1653 (if (< math-dummy-counter
(+ math-fit-first-var v
))
1654 (setq dummy
(math-dummy-variable))) ; dependent variable may be unnamed
1657 (or (eq (car-safe expr
) 'calcFunc-eq
)
1658 (setq expr
(list 'calcFunc-eq
(list 'calcFunc-fitvar v
) expr
)))
1660 (let ((calc-symbolic-mode nil
))
1662 ;; Apply rewrites to put expr into a linear-like form.
1663 (setq expr
(math-evaluate-expr expr
)
1664 expr
(math-rewrite (list 'calcFunc-fitmodel expr
)
1665 '(var FitRules var-FitRules
))
1667 expr
(math-evaluate-expr expr
))
1668 (or (and (eq (car-safe expr
) 'calcFunc-fitsystem
)
1670 (math-vectorp (nth 2 expr
))
1671 (math-vectorp (nth 3 expr
))
1672 (> (length (nth 2 expr
)) 1)
1673 (= (length (nth 3 expr
)) (1+ m
)))
1674 (math-reject-arg plain-expr
"*Model expression is too complex"))
1675 (setq y-filter
(nth 1 expr
)
1676 x-funcs
(vconcat (cdr (nth 2 expr
)))
1677 coef-filters
(nth 3 expr
)
1678 mm
(length x-funcs
))
1679 (if (equal y-filter y-dummy
)
1680 (setq y-filter nil
))
1682 ;; Build the (square) system of linear equations to be solved.
1683 (setq beta
(cons 'vec
(make-list mm
0))
1684 covar
(cons 'vec
(mapcar 'copy-sequence
(make-list mm beta
))))
1685 (let* ((ptrs (vconcat (cdr data
)))
1687 (xvals (make-vector mm
0))
1689 j k xval yval sigmasqr wt covj covjk covk betaj lud
)
1690 (while (<= (setq i
(1+ i
)) n
)
1692 ;; Assign various independent variables for this data point.
1696 (aset ptrs j
(cdr (aref ptrs j
)))
1697 (setq xval
(car (aref ptrs j
)))
1701 (if (eq (car-safe xval
) 'sdev
)
1702 (setq sigmasqr
(math-add (math-sqr (nth 2 xval
))
1706 (setq xval
(math-make-sdev xval
1707 (math-sqrt sigmasqr
))))))
1708 (if (eq (car-safe xval
) 'sdev
)
1709 (setq sigmasqr
(math-add (math-sqr (nth 2 xval
))
1711 xval
(nth 1 xval
))))
1712 (set (nth 2 (aref math-dummy-vars
(+ math-fit-first-var j
))) xval
)
1715 ;; Compute Y value for this data point.
1717 (setq yval
(math-evaluate-expr y-filter
))
1718 (setq yval
(symbol-value (nth 2 y-dummy
))))
1719 (if (eq (car-safe yval
) 'sdev
)
1720 (setq sigmasqr
(math-sqr (nth 2 yval
))
1723 (setq have-sdevs sigmasqr
1724 need-chisq
(or extended
1725 (and (eq mode
'sdev
) (not have-sdevs
)))))
1729 (setq isigsq
(math-div 1 sigmasqr
))
1731 (setq weights
(cons isigsq weights
))))
1732 (math-reject-arg yval
"*Mixed error forms and plain numbers"))
1734 (math-reject-arg yval
"*Mixed error forms and plain numbers")))
1736 ;; Compute X values for this data point and update covar and beta.
1737 (if (eq (car-safe xval
) 'sdev
)
1738 (set (nth 2 y-dummy
) (nth 1 xval
)))
1743 (setq wt
(math-evaluate-expr (aref x-funcs j
)))
1745 (setq wt
(math-mul wt isigsq
)
1747 covjk
(car (setq covj
(cdr covj
)))
1750 (setq covjk
(cdr covjk
))
1751 (setcar covjk
(math-add (car covjk
)
1752 (math-mul wt
(aref xvals k
))))
1754 (setcar betaj
(math-add (car betaj
) (math-mul wt yval
)))
1757 (setq xy-values
(cons (append xvals
(list yval
)) xy-values
))))
1759 ;; Fill in symmetric half of covar matrix.
1762 (while (< j
(1- mm
))
1765 covjk
(nthcdr j
(car (setq covj
(cdr covj
))))
1766 covk
(nthcdr j covar
))
1767 (while (< (setq k
(1+ k
)) mm
)
1768 (setq covjk
(cdr covjk
)
1770 (setcar covjk
(nth j
(car covk
))))))
1772 ;; Solve the linear system.
1775 (setq covar
(math-matrix-inv-raw covar
))
1777 (setq beta
(math-mul covar beta
))
1778 (if (math-zerop (math-abs beta
))
1779 (setq covar
(calcFunc-diag 0 (1- (length beta
))))
1780 (math-reject-arg orig-expr
"*Singular matrix")))
1781 (or (math-vectorp covar
)
1782 (setq covar
(list 'vec
(list 'vec covar
)))))
1783 (setq beta
(math-div beta covar
)))
1785 ;; Compute chi-square statistic if necessary.
1793 (while (setq bp
(cdr bp
))
1794 (setq sum
(math-add sum
(math-mul (car bp
) (car xp
)))
1796 (setq sum
(math-sqr (math-sub (car xp
) sum
)))
1797 (if weights
(setq sum
(math-mul sum
(car weights
))))
1798 (setq chisq
(math-add chisq sum
)
1799 weights
(cdr weights
)
1800 xy-values
(cdr xy-values
)))))
1802 ;; Convert coefficients back into original terms.
1803 (setq math-fit-new-coefs
(copy-sequence beta
))
1804 (let* ((bp math-fit-new-coefs
)
1809 (and mode
(not have-sdevs
)
1810 (setq sigdat
(if (<= n mm
)
1812 (math-div chisq
(- n mm
)))))
1814 (while (setq bp
(cdr bp
))
1815 (setcar bp
(math-make-sdev
1817 (math-sqrt (math-mul (nth (setq j
(1+ j
))
1818 (car (setq cp
(cdr cp
))))
1820 (setq math-fit-new-coefs
(math-evaluate-expr coef-filters
))
1821 (if calc-fit-to-trail
1822 (let ((bp math-fit-new-coefs
)
1825 (while (setq bp
(cdr bp
) cp
(cdr cp
))
1826 (setq vec
(cons (list 'calcFunc-eq
(car cp
) (car bp
)) vec
)))
1827 (setq calc-fit-to-trail
(cons 'vec
(nreverse vec
)))))))
1829 ;; Substitute best-fit coefficients back into original formula.
1830 (setq expr
(math-multi-subst
1835 (setq vec
(cons (list 'calcFunc-fitvar n
) vec
)
1839 (setq vec
(cons (list 'calcFunc-fitparam n
) vec
)
1842 (append (cdr math-fit-new-coefs
) (cdr vars
))))
1844 ;; Package the result.
1847 (list 'vec expr beta covar
1848 (let ((p coef-filters
)
1850 (while (and (setq n
(1+ n
) p
(cdr p
))
1851 (eq (car-safe (car p
)) 'calcFunc-fitdummy
)
1852 (eq (nth 1 (car p
)) n
)))
1857 (if (and have-sdevs
(> n mm
))
1858 (list 'calcFunc-utpc chisq
(- n mm
))
1859 '(var nan var-nan
)))
1863 (defun calcFunc-fitvar (x)
1864 (if (>= math-in-fit
2)
1866 (setq x
(aref math-dummy-vars
(+ math-fit-first-var x -
1)))
1867 (or (calc-var-value (nth 2 x
)) x
))
1868 (math-reject-arg x
)))
1870 (defun calcFunc-fitparam (x)
1871 (if (>= math-in-fit
2)
1873 (setq x
(aref math-dummy-vars
(+ math-fit-first-coef x -
1)))
1874 (or (calc-var-value (nth 2 x
)) x
))
1875 (math-reject-arg x
)))
1877 (defun calcFunc-fitdummy (x)
1878 (if (= math-in-fit
3)
1879 (nth x math-fit-new-coefs
)
1880 (math-reject-arg x
)))
1882 (defun calcFunc-hasfitvars (expr)
1883 (if (Math-primp expr
)
1885 (if (eq (car expr
) 'calcFunc-fitvar
)
1887 (apply 'max
(mapcar 'calcFunc-hasfitvars
(cdr expr
))))))
1889 (defun calcFunc-hasfitparams (expr)
1890 (if (Math-primp expr
)
1892 (if (eq (car expr
) 'calcFunc-fitparam
)
1894 (apply 'max
(mapcar 'calcFunc-hasfitparams
(cdr expr
))))))
1897 (defun math-all-vars-but (expr but
)
1898 (let* ((vars (math-all-vars-in expr
))
1901 (setq vars
(delq (assoc (car-safe p
) vars
) vars
)
1903 (sort (mapcar 'car vars
)
1904 (function (lambda (x y
) (string< (nth 1 x
) (nth 1 y
)))))))
1906 ;; The variables math-all-vars-vars (the vars for math-all-vars) and
1907 ;; math-all-vars-found are local to math-all-vars-in, but are used by
1908 ;; math-all-vars-rec which is called by math-all-vars-in.
1909 (defvar math-all-vars-vars
)
1910 (defvar math-all-vars-found
)
1912 (defun math-all-vars-in (expr)
1913 (let ((math-all-vars-vars nil
)
1914 math-all-vars-found
)
1915 (math-all-vars-rec expr
)
1916 math-all-vars-vars
))
1918 (defun math-all-vars-rec (expr)
1919 (if (Math-primp expr
)
1920 (if (eq (car-safe expr
) 'var
)
1921 (or (math-const-var expr
)
1922 (if (setq math-all-vars-found
(assoc expr math-all-vars-vars
))
1923 (setcdr math-all-vars-found
(1+ (cdr math-all-vars-found
)))
1924 (setq math-all-vars-vars
(cons (cons expr
1) math-all-vars-vars
)))))
1925 (while (setq expr
(cdr expr
))
1926 (math-all-vars-rec (car expr
)))))
1930 ;;; calcalg3.el ends here