1 ;;; calcalg3.el --- more algebraic functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
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.
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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
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
))
42 (defun math-map-binop (binop args1 args2
)
43 "Apply BINOP to the elements of the lists ARGS1 and ARGS2"
46 (funcall binop
(car args1
) (car args2
))
47 (funcall 'math-map-binop binop
(cdr args1
) (cdr args2
)))))
49 (defun calc-find-root (var)
50 (interactive "sVariable(s) to solve for: ")
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
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
69 (defun calc-find-minimum (var)
70 (interactive "sVariable(s) to minimize over: ")
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
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
94 (defun calc-find-maximum (var)
95 (interactive "sVariable to maximize over: ")
97 (calc-find-minimum var
))
100 (defun calc-poly-interp (arg)
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)))
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
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
)
120 (defvar calc-curve-fit-history nil
121 "History for calc-curve-fit.")
123 (defun calc-curve-fit (arg &optional calc-curve-model
124 calc-curve-coefnames calc-curve-varnames
)
127 (setq calc-aborted-prefix nil
)
128 (let ((func (if (calc-is-inverse) 'calcFunc-xfit
129 (if (calc-is-hyperbolic) 'calcFunc-efit
134 n calc-curve-nvars temp data
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)"
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
)
152 "Fit to model: %s:%s%s"
156 (setq key
(read-char))
160 (setq which
(%
(1+ which
) (length msgs
))))
162 (setq homog
(not homog
)))
166 (let ((data (calc-top 1)))
170 (not (= (length data
) 3)))
171 (setq plot
"Can't plot")
180 ((or (consp arg
) (eq arg
0))
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
)))
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
)
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
)))
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
218 (nth 1 calc-curve-coefnames
)
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
)
234 (cons 'vec
(cdr (cdr calc-curve-coefnames
)))
235 calc-curve-varnames
)))))
238 (setq calc-curve-model t
)
239 (require 'calc-nlfit
)
240 (calc-fit-s-shaped-logistic-curve func
))
243 (setq calc-curve-model t
)
244 (require 'calc-nlfit
)
245 (calc-fit-bell-shaped-logistic-curve func
))
248 (setq calc-curve-model t
)
249 (require 'calc-nlfit
)
250 (if (and plot
(not (stringp plot
)))
256 (math-map-binop 'calcFunc-div
258 (cdr (nth 1 plot
)))))))
259 (calc-fit-hubbert-linear-curve func
))
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
)
272 (^
10 (var a var-a
))))
275 (cons 'vec
(cdr (cdr calc-curve-coefnames
)))
276 calc-curve-varnames
))))))
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
))))
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
292 (cons 1 (cdr (calcFunc-map
297 calc-curve-varnames
)))))))
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
310 (list '-
(car v
) (nth 1 c
))
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
)
320 (var CFit var-CFit
)))
321 (calc-get-fit-variables 1 (1- (length calc-curve-coefnames
))
323 ((memq key
'(?\$ ?
\' ?u ?U
))
327 (let* ((calc-dollar-values calc-arg-values
)
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
)
337 (if (> calc-dollar-used
0)
338 (setq calc-curve-coefnames
340 (nthcdr (- (length calc-arg-values
)
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
))))))
348 (setq calc-curve-model
(cond ((eq key ?u
)
349 (calc-var-value 'var-Model1
))
351 (calc-var-value 'var-Model2
))
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
)
362 (or (nth 3 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
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
))
377 calc-curve-model calc-curve-coefnames
)
379 (let* ((coefs (math-all-vars-but calc-curve-model nil
))
387 (error "Not enough variables in model"))
388 (setq p
(nthcdr n coefs
))
391 (calc-get-fit-variables
392 (if with-y
(1+ calc-curve-nvars
) calc-curve-nvars
)
394 vars coefs with-y
)))))
396 (calc-record (list 'vec calc-curve-model
397 calc-curve-varnames calc-curve-coefnames
)
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
)
407 (if (= (length calc-curve-coefnames
) 2)
408 (nth 1 calc-curve-coefnames
)
409 calc-curve-coefnames
)
411 (if (consp calc-fit-to-trail
)
412 (calc-record (calc-normalize calc-fit-to-trail
) "parm"))))
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
)))
429 (math-min-list (car plot
) (cdr plot
))
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
)))))))
438 (defun calc-invent-independent-variables (n &optional but
)
439 (calc-invent-variables n but
'(x y z t
) "x"))
441 (defun calc-invent-parameter-variables (n &optional but
)
442 (calc-invent-variables n but
'(a b c d
) "a"))
444 (defun calc-invent-variables (num but names base
)
448 (while (and (> n
0) names
)
449 (setq var
(math-build-var-name (if (consp 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
)
456 (or (symbolp names
) (setq names
(cdr names
))))
459 (calc-invent-variables num but t base
))))
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"))
470 (setq defv
(calc-invent-independent-variables nv
)))
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)
479 (mapconcat 'symbol-name
480 (mapcar (function (lambda (v)
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
)
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")))
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
)))
520 ;;; The following algorithms are from Numerical Recipes chapter 9.
522 ;;; "rtnewt" with safety kludges
526 (defun math-newton-root (expr deriv guess orig-guess limit
)
527 (math-working "newton" guess
)
528 (let* ((var-DUMMY guess
)
530 (setq next
(math-evaluate-expr expr
)
531 dval
(math-evaluate-expr deriv
))
532 (if (and (Math-numberp next
)
534 (not (Math-zerop dval
)))
536 (setq next
(math-sub guess
(math-div next dval
)))
537 (if (math-nearly-equal guess
(setq next
(math-float next
)))
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
))
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"))))
547 ;;; Inspired by "rtsafe"
548 (defun math-newton-search-root (expr deriv guess vguess ostep oostep
550 (let ((var-DUMMY 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
)
558 guess
(math-add-float low ostep
)
560 vguess
(math-evaluate-expr expr
))
561 (or (Math-realp vguess
)
563 (setq ostep
(math-mul-float ostep
'(float 6 -
1))
564 guess
(math-add-float low ostep
)
566 vguess
(math-evaluate-expr expr
))
567 (or (math-realp vguess
)
569 (setq ostep
(math-mul-float ostep
'(float 123456 -
5))
570 guess
(math-add-float low ostep
)
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
)))
581 (if (eq (Math-negp vhigh
) pos
)
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
)))
597 vnext
(math-evaluate-expr expr
))
598 (if (or (Math-zerop vnext
)
599 (math-nearly-equal next guess
))
600 (list 'vec next vnext
)
602 (math-lessp-float (math-abs (or oostep
606 (math-mul-float '(float 2 0)
608 (math-newton-search-root expr deriv nil nil nil ostep
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
))))))
618 ;;; Search for a root in an interval with no overt zero crossing.
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
)
625 (defun math-search-root (expr deriv low vlow high vhigh
)
629 (iterlim (if (eq math-root-widen
'point
)
630 (+ calc-internal-prec
10)
632 (factor (if (eq math-root-widen
'point
)
635 (prev nil
) vprev waslow
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
))
651 low
(math-sub low diff
)
654 vlow
(math-evaluate-expr expr
))
657 high
(math-add high diff
)
660 vhigh
(math-evaluate-expr expr
)))))
663 (setq high prev vhigh vprev
)
664 (setq low prev vlow vprev
)))
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
))
674 (step (math-sub-float high low
))
676 (while (and (<= (setq levels
(1+ levels
)) 5)
680 step
(math-mul-float step
'(float 497 -
3)))
681 (while (and (cdr xp
) (not found
))
682 (if (Math-realp (car yp
))
685 (setq high
(math-add-float (car xp
) step
)
687 vhigh
(math-evaluate-expr expr
))
688 (math-working "search" high
)
689 (if (and (Math-realp vhigh
)
690 (eq (math-negp vhigh
) pos
))
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
))))))))
697 (if (Math-zerop vhigh
)
698 (list 'vec high vhigh
)
699 (if (Math-zerop vlow
)
702 (math-newton-search-root expr deriv nil nil nil nil
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"))))
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
))
714 (while (not (or (math-nearly-equal low
715 (setq step
(math-mul-float
717 mid
(math-add-float low step
)))
720 vmid
(math-evaluate-expr expr
))
722 (math-working "bisect" mid
)
723 (if (eq (Math-posp vmid
) pos
)
728 (list 'vec mid vmid
)))
732 (defvar math-root-vars
[(var DUMMY var-DUMMY
)])
734 (defun math-newton-multi (expr jacob n guess orig-guess limit
)
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
)))
748 (math-working "newton" next
)
749 (while (and (setq p
(cdr p
) p2
(cdr p2
))
750 (math-nearly-equal (car p
) (car p2
))))
752 (if (Math-lessp (math-abs-approx (math-sub next orig-guess
))
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
))))
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
)
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"
774 (length math-root-vars
))))))
775 (setq math-root-vars
(vconcat math-root-vars
776 (vector (list 'var symb symb
))))))
778 (while (< (setq m
(1+ m
)) n
)
779 (set (nth 2 (aref math-root-vars m
)) nil
))
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"))
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
)
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
))))
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
)
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
)
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
)
827 (expr (math-evaluate-expr expr
))
828 (deriv (calcFunc-deriv expr
'(var DUMMY var-DUMMY
) nil t
))
830 (and deriv
(setq deriv
(math-evaluate-expr deriv
)))
831 (setq guess
(math-float guess
))
832 (if (and (math-numberp guess
)
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
)
841 vlow
(math-evaluate-expr expr
)
843 math-root-widen
'point
)
844 (if (eq (car guess
) 'intv
)
846 (or (math-constp guess
) (math-reject-arg guess
'constp
))
847 (setq low
(nth 2 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
)))
854 vlow
(math-evaluate-expr expr
)
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
)
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
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
))))))))))
874 (defun calcFunc-root (expr var guess
)
875 (math-find-root expr var guess nil
))
877 (defun calcFunc-wroot (expr var guess
)
878 (math-find-root expr var guess t
))
883 ;;; The following algorithms come from Numerical Recipes, chapter 10.
885 (defvar math-min-vars
[(var DUMMY var-DUMMY
)])
887 (defun math-min-eval (expr a
)
890 (while (setq m
(1+ m
) a
(cdr a
))
891 (set (nth 2 (aref math-min-vars m
)) (car a
))))
893 (setq a
(math-evaluate-expr expr
))
896 (if (eq (car a
) 'float
)
898 (math-reject-arg a
'realp
))))
900 (defvar math-min-or-max
"minimum")
902 ;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c).
905 (defun math-widen-min (expr a b
)
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
)
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"
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
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
))
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
)
943 (if (math-lessp-float vb vu
)
946 (setq u
(math-add-float c
(math-mul-float '(float -
161803 -
5)
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
)
953 u
(math-add-float c
(math-mul-float
955 (math-sub-float b c
)))
956 vu
(math-min-eval expr u
)))
958 vu
(math-min-eval expr u
))))
959 (setq u
(math-add-float c
(math-mul-float '(float -
161803 -
5)
961 vu
(math-min-eval expr u
)))
965 (if (math-lessp-float a c
)
966 (list a va b vb c vc
)
967 (list c vc b vb a va
))))
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
)))
974 (step (math-sub-float c a
))
977 (while (and (<= (setq levels
(1+ levels
)) 5)
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
)))
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
)))
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
))))
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
))))
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
))))))
1020 (defun math-brent-min (expr prec a va x vx b vb
)
1021 (let ((iters (+ 20 (* 5 prec
)))
1026 (tol (list 'float
1 (- -
1 prec
)))
1027 (zeps (list 'float
1 (- -
5 prec
)))
1029 d u vu xm tol1 tol2 etemp p q r xv xw
)
1031 (setq xm
(math-mul-float '(float 5 -
1)
1032 (math-add-float a b
))
1033 tol1
(math-add-float
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
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"
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
)))
1059 (setq p
(math-neg-float p
))
1060 (setq q
(math-neg-float q
)))
1063 (if (and (math-lessp-float (math-abs p
)
1064 (math-abs (math-mul-float
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
))))
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
)
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
)
1086 (math-neg-float tol1
)
1089 vu
(math-min-eval expr u
))
1090 (if (math-lessp-float vx vu
)
1092 (if (math-lessp-float u x
)
1096 (not (math-lessp-float vw vu
)))
1101 (not (math-lessp-float vv vu
)))
1103 (if (math-lessp-float u x
)
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
)))
1117 (ftol (list 'float
1 (- prec
)))
1118 (fret (math-min-eval expr p
))
1119 fp ptt fptt xit i ibig del diff res
)
1125 (while (<= (setq i
(1+ i
)) n
)
1127 res
(math-line-min f1dim p
1130 p
(let ((calc-internal-prec prec
))
1131 (math-normalize (car res
)))
1133 diff
(math-abs (math-sub-float fptt fret
)))
1134 (if (math-lessp-float del diff
)
1138 (math-mul-float ftol
1139 (math-add-float (math-abs fp
)
1141 (math-mul-float '(float 2 0)
1142 (math-abs (math-sub-float fp
1144 (setq ptt
(math-sub (math-mul '(float 2 0) p
) pt
)
1147 fptt
(math-min-eval expr ptt
))
1148 (if (and (math-lessp-float fptt fp
)
1151 (math-mul-float '(float 2 0)
1154 (math-mul-float '(float 2 0)
1157 (math-sqr-float (math-sub-float
1158 (math-sub-float fp fret
) del
)))
1160 (math-sqr-float (math-sub-float fp fptt
)))))
1162 (setq res
(math-line-min f1dim p xit n prec
)
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
)))
1171 (defun math-line-min-func (expr n
)
1173 (while (< (setq m
(1+ m
)) n
)
1174 (set (nth 2 (aref math-min-vars m
))
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
)))
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
))))
1191 (defun math-find-minimum (expr var guess min-widen
)
1192 (let* ((calc-symbolic-mode nil
)
1195 (isvec (math-vectorp var
))
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"
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
1220 (list '+ (aref math-min-vars n
)
1222 (aref math-min-vars
(1+ n
))
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
)
1229 (setq expr
(math-expr-subst expr
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
)))
1237 (math-add (nth 2 (car guess
))
1238 (nth 3 (car guess
)))
1240 (math-float (nth 2 (car guess
)))
1241 (math-float (nth 3 (car guess
)))
1243 (math-reject-arg (car guess
) 'realp
)))
1246 (setq guesses
(nreverse guesses
)
1247 expr
(math-evaluate-expr expr
))
1249 (let* ((params (if (nth 1 (car guesses
))
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
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
))))
1267 (list 'vec
(list 'vec
(nth 1 res
)) (nth 2 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
)))
1274 (while (setq p
(cdr p
))
1275 (if (nth 3 (car guesses
))
1277 (nconc vec
(list (math-normalize
1278 (list 'cplx
(car p
) (nth 1 p
)))))
1280 guesses
(cdr guesses
)))
1281 (nconc vec
(list (car p
))))
1282 (setq guesses
(cdr guesses
)))
1284 (list 'vec vec
(nth 2 res
))
1285 (list 'vec
(nth 1 vec
) (nth 2 res
)))))))
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
)))
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
)))
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
)))))
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
)))))
1320 ;;; The following algorithms come from Numerical Recipes, chapter 3.
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
)))
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
1335 (put 'calcFunc-polint
'math-expandable t
)
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
)))
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
)
1354 (defun math-poly-interp (xa ya x ratp
)
1355 (let ((n (length xa
))
1359 (c (copy-sequence ya
))
1360 (d (copy-sequence ya
))
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
)
1367 temp
(math-abs (car xax
)))
1368 (if (or (null dif
) (math-lessp temp dif
))
1371 (setq xax
(nreverse xax
)
1374 (if (math-zerop dif
)
1376 (while (< (setq m
(1+ m
)) n
)
1382 (while (<= (setq i
(1+ i
)) (- n m
))
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
)))
1399 (if (< (+ ns ns
) (- n m
))
1400 (setq dy
(nth ns c
))
1403 (setq y
(math-add y dy
)))
1408 ;;; The following algorithms come from Numerical Recipes, chapter 4.
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
)
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))
1426 (setq sum
(math-ninteg-romberg
1427 'math-ninteg-midpoint expr
1428 (math-float lo
) (math-float thi
) 'inf
)
1430 (if (equal hi
'(var inf var-inf
))
1431 (let ((tlo (if (math-lessp '(float 2 0) lo
)
1433 (setq sum
(math-add sum
1434 (math-ninteg-romberg
1435 'math-ninteg-midpoint expr
1436 (math-float tlo
) (math-float hi
) 'inf
))
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
))))
1446 ;;; Open Romberg method; "qromo" in section 4.4.
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
)
1453 (defun math-ninteg-romberg (func expr lo hi mode
)
1454 (let ((curh '(float 1 0))
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
)))
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
))
1471 (setq ss
(car res
)))))
1475 (setq curh
(math-div-float curh
'(float 9 0))))
1477 (math-reject-arg nil
(format "*Integral failed to converge"))))))
1480 (defun math-ninteg-evaluate (expr x mode
)
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"))
1488 (setq res
(math-mul res
(math-sqr x
))))
1492 (defun math-ninteg-midpoint (expr lo hi mode
) ; uses "math-ninteg-temp"
1494 (let ((math-infinite-mode t
) temp
)
1495 (setq temp
(math-div 1 lo
)
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
)))
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
)
1516 x
(math-add x del3
)))
1517 (setq math-ninteg-temp
(list it3
1518 (math-add (math-div (nth 1 math-ninteg-temp
)
1520 (math-mul sum del
)))))
1521 (setq math-ninteg-temp
(list 1 (math-mul
1523 (math-ninteg-evaluate
1525 (math-mul (math-add lo hi
) '(float 5 -
1))
1527 (nth 1 math-ninteg-temp
))
1533 ;;; The following algorithms come from Numerical Recipes, chapter 14.
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
)
1544 (aref math-dummy-vars math-dummy-counter
)
1545 (setq math-dummy-counter
(1+ math-dummy-counter
))))
1547 (defvar math-in-fit
0)
1548 (defvar calc-fit-to-trail nil
)
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
))))
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
))))
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
))))
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
)
1573 (defun math-general-fit (expr vars coefs data mode
)
1574 (let ((calc-simplify-mode nil
)
1575 (math-dummy-counter math-dummy-counter
)
1577 (extended (eq mode
'full
))
1578 (math-fit-first-coef math-dummy-counter
)
1582 have-sdevs need-chisq chisq
1590 (var-YVAL nil
) (var-YVALX nil
)
1592 n nn m mm v dummy p
)
1594 ;; Validate and parse arguments.
1599 (if (math-vectorp expr
)
1600 (if (memq (length expr
) '(3 4))
1605 (math-dimension-error))
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
)))
1617 (setq coefs
(cons 'vec
(math-all-vars-but expr 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))
1625 (setq vars
(cons 'vec
(cdr p
)))
1627 (or (= (1- (length vars
)) v
)
1629 (math-reject-arg vars
"*Number of variables does not match data"))
1630 (setq m
(1- (length coefs
)))
1632 (math-reject-arg coefs
"*Need at least one parameter"))
1634 ;; Rewrite expr in terms of fitparam and fitvar, make into an equation.
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
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
1656 (or (eq (car-safe expr
) 'calcFunc-eq
)
1657 (setq expr
(list 'calcFunc-eq
(list 'calcFunc-fitvar v
) expr
)))
1659 (let ((calc-symbolic-mode nil
))
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
))
1666 expr
(math-evaluate-expr expr
))
1667 (or (and (eq (car-safe expr
) 'calcFunc-fitsystem
)
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
))
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
)))
1686 (xvals (make-vector mm
0))
1688 j k xval yval sigmasqr wt covj covjk covk betaj lud
)
1689 (while (<= (setq i
(1+ i
)) n
)
1691 ;; Assign various independent variables for this data point.
1695 (aset ptrs j
(cdr (aref ptrs j
)))
1696 (setq xval
(car (aref ptrs j
)))
1700 (if (eq (car-safe xval
) 'sdev
)
1701 (setq sigmasqr
(math-add (math-sqr (nth 2 xval
))
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
))
1710 xval
(nth 1 xval
))))
1711 (set (nth 2 (aref math-dummy-vars
(+ math-fit-first-var j
))) xval
)
1714 ;; Compute Y value for this data point.
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
))
1722 (setq have-sdevs sigmasqr
1723 need-chisq
(or extended
1724 (and (eq mode
'sdev
) (not have-sdevs
)))))
1728 (setq isigsq
(math-div 1 sigmasqr
))
1730 (setq weights
(cons isigsq weights
))))
1731 (math-reject-arg yval
"*Mixed error forms and plain numbers"))
1733 (math-reject-arg yval
"*Mixed error forms and plain numbers")))
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
)))
1742 (setq wt
(math-evaluate-expr (aref x-funcs j
)))
1744 (setq wt
(math-mul wt isigsq
)
1746 covjk
(car (setq covj
(cdr covj
)))
1749 (setq covjk
(cdr covjk
))
1750 (setcar covjk
(math-add (car covjk
)
1751 (math-mul wt
(aref xvals k
))))
1753 (setcar betaj
(math-add (car betaj
) (math-mul wt yval
)))
1756 (setq xy-values
(cons (append xvals
(list yval
)) xy-values
))))
1758 ;; Fill in symmetric half of covar matrix.
1761 (while (< j
(1- mm
))
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
)
1769 (setcar covjk
(nth j
(car covk
))))))
1771 ;; Solve the linear system.
1774 (setq covar
(math-matrix-inv-raw 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
)))
1784 ;; Compute chi-square statistic if necessary.
1792 (while (setq bp
(cdr bp
))
1793 (setq sum
(math-add sum
(math-mul (car bp
) (car 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
)))))
1801 ;; Convert coefficients back into original terms.
1802 (setq math-fit-new-coefs
(copy-sequence beta
))
1803 (let* ((bp math-fit-new-coefs
)
1808 (and mode
(not have-sdevs
)
1809 (setq sigdat
(if (<= n mm
)
1811 (math-div chisq
(- n mm
)))))
1813 (while (setq bp
(cdr bp
))
1814 (setcar bp
(math-make-sdev
1816 (math-sqrt (math-mul (nth (setq j
(1+ j
))
1817 (car (setq cp
(cdr cp
))))
1819 (setq math-fit-new-coefs
(math-evaluate-expr coef-filters
))
1820 (if calc-fit-to-trail
1821 (let ((bp math-fit-new-coefs
)
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
)))))))
1828 ;; Substitute best-fit coefficients back into original formula.
1829 (setq expr
(math-multi-subst
1834 (setq vec
(cons (list 'calcFunc-fitvar n
) vec
)
1838 (setq vec
(cons (list 'calcFunc-fitparam n
) vec
)
1841 (append (cdr math-fit-new-coefs
) (cdr vars
))))
1843 ;; Package the result.
1846 (list 'vec expr beta covar
1847 (let ((p coef-filters
)
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
)))
1856 (if (and have-sdevs
(> n mm
))
1857 (list 'calcFunc-utpc chisq
(- n mm
))
1858 '(var nan var-nan
)))
1862 (defun calcFunc-fitvar (x)
1863 (if (>= math-in-fit
2)
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
)))
1869 (defun calcFunc-fitparam (x)
1870 (if (>= math-in-fit
2)
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
)))
1876 (defun calcFunc-fitdummy (x)
1877 (if (= math-in-fit
3)
1878 (nth x math-fit-new-coefs
)
1879 (math-reject-arg x
)))
1881 (defun calcFunc-hasfitvars (expr)
1882 (if (Math-primp expr
)
1884 (if (eq (car expr
) 'calcFunc-fitvar
)
1886 (apply 'max
(mapcar 'calcFunc-hasfitvars
(cdr expr
))))))
1888 (defun calcFunc-hasfitparams (expr)
1889 (if (Math-primp expr
)
1891 (if (eq (car expr
) 'calcFunc-fitparam
)
1893 (apply 'max
(mapcar 'calcFunc-hasfitparams
(cdr expr
))))))
1896 (defun math-all-vars-but (expr but
)
1897 (let* ((vars (math-all-vars-in expr
))
1900 (setq vars
(delq (assoc (car-safe p
) vars
) vars
)
1902 (sort (mapcar 'car vars
)
1903 (function (lambda (x y
) (string< (nth 1 x
) (nth 1 y
)))))))
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
)
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
))
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
)))))
1929 ;;; calcalg3.el ends here