1 ;;; calcalg3.el --- more algebraic functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-Need-calc-alg-3 () nil
)
38 (defun calc-find-root (var)
39 (interactive "sVariable(s) to solve for: ")
41 (let ((func (if (calc-is-hyperbolic) 'calcFunc-wroot
'calcFunc-root
)))
42 (if (or (equal var
"") (equal var
"$"))
43 (calc-enter-result 2 "root" (list func
47 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var
)
48 (not (string-match "\\[" var
)))
49 (math-read-expr (concat "[" var
"]"))
50 (math-read-expr var
))))
51 (if (eq (car-safe var
) 'error
)
52 (error "Bad format in expression: %s" (nth 1 var
)))
53 (calc-enter-result 1 "root" (list func
58 (defun calc-find-minimum (var)
59 (interactive "sVariable(s) to minimize over: ")
61 (let ((func (if (calc-is-inverse)
62 (if (calc-is-hyperbolic)
63 'calcFunc-wmaximize
'calcFunc-maximize
)
64 (if (calc-is-hyperbolic)
65 'calcFunc-wminimize
'calcFunc-minimize
)))
66 (tag (if (calc-is-inverse) "max" "min")))
67 (if (or (equal var
"") (equal var
"$"))
68 (calc-enter-result 2 tag
(list func
72 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var
)
73 (not (string-match "\\[" var
)))
74 (math-read-expr (concat "[" var
"]"))
75 (math-read-expr var
))))
76 (if (eq (car-safe var
) 'error
)
77 (error "Bad format in expression: %s" (nth 1 var
)))
78 (calc-enter-result 1 tag
(list func
83 (defun calc-find-maximum (var)
84 (interactive "sVariable to maximize over: ")
86 (calc-find-minimum var
))
89 (defun calc-poly-interp (arg)
92 (let ((data (calc-top 2)))
93 (if (or (consp arg
) (eq arg
0) (eq arg
2))
94 (setq data
(cons 'vec
(calc-top-list 2 2)))
96 (error "Bad prefix argument")))
97 (if (calc-is-hyperbolic)
98 (calc-enter-result 1 "rati" (list 'calcFunc-ratint data
(calc-top 1)))
99 (calc-enter-result 1 "poli" (list 'calcFunc-polint data
103 (defun calc-curve-fit (arg &optional model coefnames varnames
)
106 (setq calc-aborted-prefix nil
)
107 (let ((func (if (calc-is-inverse) 'calcFunc-xfit
108 (if (calc-is-hyperbolic) 'calcFunc-efit
113 (msgs '( "(Press ? for help)"
114 "1 = linear or multilinear"
115 "2-9 = polynomial fits; i = interpolating polynomial"
116 "p = a x^b, ^ = a b^x"
117 "e = a exp(b x), x = exp(a + b x), l = a + b ln(x)"
118 "E = a 10^(b x), X = 10^(a + b x), L = a + b log10(x)"
120 "g = (a/b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)"
121 "h prefix = homogeneous model (no constant term)"
122 "' = alg entry, $ = stack, u = Model1, U = Model2")))
124 (message "Fit to model: %s:%s"
127 (setq key
(read-char))
131 (setq which
(%
(1+ which
) (length msgs
))))
133 (setq homog
(not homog
)))
141 ((or (consp arg
) (eq arg
0))
144 data
(if (math-matrixp data
)
145 (append data
(list (calc-top (1- n
))))
146 (list 'vec data
(calc-top (1- n
))))))
147 ((> (setq arg
(prefix-numeric-value arg
)) 0)
148 (setq data
(cons 'vec
(calc-top-list arg
(1+ n
)))
150 (t (error "Bad prefix argument")))
151 (or (math-matrixp data
) (not (cdr (cdr data
)))
152 (error "Data matrix is not a matrix!"))
153 (setq nvars
(- (length data
) 2)
157 ((= key ?
1) ; linear or multilinear
158 (calc-get-fit-variables nvars
(1+ nvars
) (and homog
0))
159 (setq model
(math-mul coefnames
160 (cons 'vec
(cons 1 (cdr varnames
))))))
161 ((and (>= key ?
2) (<= key ?
9)) ; polynomial
162 (calc-get-fit-variables 1 (- key ?
0 -
1) (and homog
0))
163 (setq model
(math-build-polynomial-expr (cdr coefnames
)
165 ((= key ?i
) ; exact polynomial
166 (calc-get-fit-variables 1 (1- (length (nth 1 data
)))
168 (setq model
(math-build-polynomial-expr (cdr coefnames
)
170 ((= key ?p
) ; power law
171 (calc-get-fit-variables nvars
(1+ nvars
) (and homog
1))
172 (setq model
(math-mul (nth 1 coefnames
)
178 (cons 'vec
(cdr (cdr coefnames
))))))))
179 ((= key ?^
) ; exponential law
180 (calc-get-fit-variables nvars
(1+ nvars
) (and homog
1))
181 (setq model
(math-mul (nth 1 coefnames
)
186 (cons 'vec
(cdr (cdr coefnames
)))
189 (calc-get-fit-variables nvars
(1+ nvars
) (and homog
1))
190 (setq model
(math-mul (nth 1 coefnames
)
198 (^
10 (var a var-a
))))
201 (cons 'vec
(cdr (cdr coefnames
)))
204 (calc-get-fit-variables nvars
(1+ nvars
) (and homog
0))
205 (setq model
(math-mul coefnames
206 (cons 'vec
(cons 1 (cdr varnames
)))))
207 (setq model
(if (eq key ?x
)
208 (list 'calcFunc-exp model
)
209 (list '^
10 model
))))
211 (calc-get-fit-variables nvars
(1+ nvars
) (and homog
0))
212 (setq model
(math-mul coefnames
214 (cons 1 (cdr (calcFunc-map
221 (calc-get-fit-variables nvars
(1+ (* 2 nvars
)) (and homog
0))
224 (setq model
(nth 1 c
))
225 (while (setq v
(cdr v
) c
(cdr (cdr c
)))
226 (setq model
(math-add
231 (list '-
(car v
) (nth 1 c
))
234 (setq model
(math-read-expr "(AFit / BFit sqrt(2 pi)) exp(-0.5 * ((XFit - CFit) / BFit)^2)")
235 varnames
'(vec (var XFit var-XFit
))
236 coefnames
'(vec (var AFit var-AFit
)
238 (var CFit var-CFit
)))
239 (calc-get-fit-variables 1 (1- (length coefnames
)) (and homog
1)))
240 ((memq key
'(?\$ ?
\' ?u ?U
))
244 (let* ((calc-dollar-values calc-arg-values
)
246 (calc-hashes-used 0))
247 (setq model
(calc-do-alg-entry "" "Model formula: "))
248 (if (/= (length model
) 1)
249 (error "Bad format"))
250 (setq model
(car model
)
252 (if (> calc-dollar-used
0)
255 (nthcdr (- (length calc-arg-values
)
257 (reverse calc-arg-values
))))
258 (if (> calc-hashes-used
0)
260 (cons 'vec
(calc-invent-args
261 calc-hashes-used
))))))
263 (setq model
(cond ((eq key ?u
)
264 (calc-var-value 'var-Model1
))
266 (calc-var-value 'var-Model2
))
268 (or model
(error "User model not yet defined"))
269 (if (math-vectorp model
)
270 (if (and (memq (length model
) '(3 4))
271 (not (math-objvecp (nth 1 model
)))
272 (math-vectorp (nth 2 model
))
273 (or (null (nth 3 model
))
274 (math-vectorp (nth 3 model
))))
275 (setq varnames
(nth 2 model
)
276 coefnames
(or (nth 3 model
)
281 (error "Incorrect model specifier")))))
283 (let ((with-y (eq (car-safe model
) 'calcFunc-eq
)))
285 (calc-get-fit-variables (if with-y
(1+ nvars
) nvars
)
286 (1- (length coefnames
))
290 (let* ((coefs (math-all-vars-but model nil
))
292 (n (- (length coefs
) nvars
(if with-y
2 1)))
295 (error "Not enough variables in model"))
296 (setq p
(nthcdr n coefs
))
299 (calc-get-fit-variables (if with-y
(1+ nvars
) nvars
)
301 vars coefs with-y
)))))
303 (calc-record (list 'vec model varnames coefnames
)
306 (let ((calc-fit-to-trail t
))
307 (calc-enter-result n
(substring (symbol-name func
) 9)
309 (if (= (length varnames
) 2)
312 (if (= (length coefnames
) 2)
316 (if (consp calc-fit-to-trail
)
317 (calc-record (calc-normalize calc-fit-to-trail
) "parm"))))))
319 (defun calc-invent-independent-variables (n &optional but
)
320 (calc-invent-variables n but
'(x y z t
) "x"))
322 (defun calc-invent-parameter-variables (n &optional but
)
323 (calc-invent-variables n but
'(a b c d
) "a"))
325 (defun calc-invent-variables (num but names base
)
329 (while (and (> n
0) names
)
330 (setq var
(math-build-var-name (if (consp names
)
332 (concat base
(int-to-string
333 (setq nn
(1+ nn
)))))))
334 (or (math-expr-contains (cons 'vec but
) var
)
335 (setq vars
(cons var vars
)
337 (or (symbolp names
) (setq names
(cdr names
))))
340 (calc-invent-variables num but t base
))))
342 (defun calc-get-fit-variables (nv nc
&optional defv defc with-y homog
)
343 (or (= nv
(if with-y
(1+ nvars
) nvars
))
344 (error "Wrong number of data vectors for this type of model"))
351 (setq defv
(calc-invent-independent-variables nv
)))
353 (setq defc
(calc-invent-parameter-variables nc defv
)))
354 (let ((vars (read-string (format "Fitting variables: (default %s; %s) "
355 (mapconcat 'symbol-name
356 (mapcar (function (lambda (v)
360 (mapconcat 'symbol-name
361 (mapcar (function (lambda (v)
366 (setq vars
(if (string-match "\\[" vars
)
367 (math-read-expr vars
)
368 (math-read-expr (concat "[" vars
"]"))))
369 (if (eq (car-safe vars
) 'error
)
370 (error "Bad format in expression: %s" (nth 2 vars
)))
371 (or (math-vectorp vars
)
372 (error "Expected a variable or vector of variables"))
373 (if (equal vars
'(vec))
374 (setq vars
(cons 'vec defv
)
375 coefs
(cons 'vec defc
))
376 (if (math-vectorp (nth 1 vars
))
377 (if (and (= (length vars
) 3)
378 (math-vectorp (nth 2 vars
)))
379 (setq coefs
(nth 2 vars
)
382 "Expected independent variables vector, then parameters vector"))
383 (setq coefs
(cons 'vec defc
))))
384 (or (= nv
(1- (length vars
)))
385 (and (not with-y
) (= (1+ nv
) (1- (length vars
))))
386 (error "Expected %d independent variable%s" nv
(if (= nv
1) "" "s")))
387 (or (= nc
(1- (length coefs
)))
388 (error "Expected %d parameter variable%s" nc
(if (= nc
1) "" "s")))
390 (setq coefs
(cons 'vec
(cons homog
(cdr coefs
)))))
392 (setq model
(math-multi-subst model
(cdr varnames
) (cdr vars
))))
394 (setq model
(math-multi-subst model
(cdr coefnames
) (cdr coefs
))))
401 ;;; The following algorithms are from Numerical Recipes chapter 9.
403 ;;; "rtnewt" with safety kludges
404 (defun math-newton-root (expr deriv guess orig-guess limit
)
405 (math-working "newton" guess
)
406 (let* ((var-DUMMY guess
)
408 (setq next
(math-evaluate-expr expr
)
409 dval
(math-evaluate-expr deriv
))
410 (if (and (Math-numberp next
)
412 (not (Math-zerop dval
)))
414 (setq next
(math-sub guess
(math-div next dval
)))
415 (if (math-nearly-equal guess
(setq next
(math-float next
)))
417 (setq var-DUMMY next
)
418 (list 'vec next
(math-evaluate-expr expr
)))
419 (if (Math-lessp (math-abs-approx (math-sub next orig-guess
))
421 (math-newton-root expr deriv next orig-guess limit
)
422 (math-reject-arg next
"*Newton's method failed to converge"))))
423 (math-reject-arg next
"*Newton's method encountered a singularity"))))
425 ;;; Inspired by "rtsafe"
426 (defun math-newton-search-root (expr deriv guess vguess ostep oostep
428 (let ((var-DUMMY guess
)
432 (math-working "newton" (list 'intv
0 low high
))
433 (math-working "bisect" (list 'intv
0 low high
))
434 (setq ostep
(math-mul-float (math-sub-float high low
)
436 guess
(math-add-float low ostep
)
438 vguess
(math-evaluate-expr expr
))
439 (or (Math-realp vguess
)
441 (setq ostep
(math-mul-float ostep
'(float 6 -
1))
442 guess
(math-add-float low ostep
)
444 vguess
(math-evaluate-expr expr
))
445 (or (math-realp vguess
)
447 (setq ostep
(math-mul-float ostep
'(float 123456 -
5))
448 guess
(math-add-float low ostep
)
452 (setq vguess
(math-evaluate-expr expr
)))
453 (or (Math-realp vguess
)
454 (math-reject-arg guess
"*Newton's method encountered a singularity"))
455 (setq vguess
(math-float vguess
))
456 (if (eq (Math-negp vlow
) (setq pos
(Math-posp vguess
)))
459 (if (eq (Math-negp vhigh
) pos
)
463 (if (or (Math-zerop vguess
)
464 (math-nearly-equal low high
))
465 (list 'vec guess vguess
)
466 (setq step
(math-evaluate-expr deriv
))
467 (if (and (Math-realp step
)
468 (not (Math-zerop step
))
469 (setq step
(math-div-float vguess
(math-float step
))
470 next
(math-sub-float guess step
))
471 (not (math-lessp-float high next
))
472 (not (math-lessp-float next low
)))
475 vnext
(math-evaluate-expr expr
))
476 (if (or (Math-zerop vnext
)
477 (math-nearly-equal next guess
))
478 (list 'vec next vnext
)
480 (math-lessp-float (math-abs (or oostep
484 (math-mul-float '(float 2 0)
486 (math-newton-search-root expr deriv nil nil nil ostep
488 (math-newton-search-root expr deriv next vnext step ostep
489 low vlow high vhigh
))))
490 (if (or (and (Math-posp vlow
) (Math-posp vhigh
))
491 (and (Math-negp vlow
) (Math-negp vhigh
)))
492 (math-search-root expr deriv low vlow high vhigh
)
493 (math-newton-search-root expr deriv nil nil nil ostep
494 low vlow high vhigh
))))))
496 ;;; Search for a root in an interval with no overt zero crossing.
497 (defun math-search-root (expr deriv low vlow high vhigh
)
501 (iterlim (if (eq root-widen
'point
)
502 (+ calc-internal-prec
10)
504 (factor (if (eq root-widen
'point
)
507 (prev nil
) vprev waslow
509 (while (or (and (math-posp vlow
) (math-posp vhigh
))
510 (and (math-negp vlow
) (math-negp vhigh
)))
511 (math-working "widen" (list 'intv
0 low high
))
512 (if (> (setq iters
(1+ iters
)) iterlim
)
513 (math-reject-arg (list 'intv
0 low high
)
514 "*Unable to bracket root"))
515 (if (= iters calc-internal-prec
)
516 (setq factor
'(float 16 -
1)))
517 (setq diff
(math-mul-float (math-sub-float high low
) factor
))
518 (if (Math-zerop diff
)
519 (setq high
(calcFunc-incr high
10))
520 (if (math-lessp-float (math-abs vlow
) (math-abs vhigh
))
523 low
(math-sub low diff
)
526 vlow
(math-evaluate-expr expr
))
529 high
(math-add high diff
)
532 vhigh
(math-evaluate-expr expr
)))))
535 (setq high prev vhigh vprev
)
536 (setq low prev vlow vprev
)))
538 (or (Math-realp vlow
)
539 (math-reject-arg vlow
'realp
))
540 (or (Math-realp vhigh
)
541 (math-reject-arg vhigh
'realp
))
542 (let ((xvals (list low high
))
543 (yvals (list vlow vhigh
))
544 (pos (Math-posp vlow
))
546 (step (math-sub-float high low
))
548 (while (and (<= (setq levels
(1+ levels
)) 5)
552 step
(math-mul-float step
'(float 497 -
3)))
553 (while (and (cdr xp
) (not found
))
554 (if (Math-realp (car yp
))
557 (setq high
(math-add-float (car xp
) step
)
559 vhigh
(math-evaluate-expr expr
))
560 (math-working "search" high
)
561 (if (and (Math-realp vhigh
)
562 (eq (math-negp vhigh
) pos
))
564 (setcdr xp
(cons high
(cdr xp
)))
565 (setcdr yp
(cons vhigh
(cdr yp
)))
566 (setq xp
(cdr (cdr xp
))
567 yp
(cdr (cdr yp
))))))))
569 (if (Math-zerop vhigh
)
570 (list 'vec high vhigh
)
571 (if (Math-zerop vlow
)
574 (math-newton-search-root expr deriv nil nil nil nil
576 (math-bisect-root expr low vlow high vhigh
))))
577 (math-reject-arg (list 'intv
3 low high
)
578 "*Unable to find a sign change in this interval"))))
580 ;;; "rtbis" (but we should be using Brent's method)
581 (defun math-bisect-root (expr low vlow high vhigh
)
582 (let ((step (math-sub-float high low
))
583 (pos (Math-posp vhigh
))
586 (while (not (or (math-nearly-equal low
587 (setq step
(math-mul-float
589 mid
(math-add-float low step
)))
592 vmid
(math-evaluate-expr expr
))
594 (math-working "bisect" mid
)
595 (if (eq (Math-posp vmid
) pos
)
600 (list 'vec mid vmid
)))
603 (defun math-newton-multi (expr jacob n guess orig-guess limit
)
606 p2 expr-val jacob-val next
)
607 (while (< (setq p
(cdr p
) m
(1+ m
)) n
)
608 (set (nth 2 (aref math-root-vars m
)) (car p
)))
609 (setq expr-val
(math-evaluate-expr expr
)
610 jacob-val
(math-evaluate-expr jacob
))
611 (unless (and (math-constp expr-val
)
612 (math-constp jacob-val
))
613 (math-reject-arg guess
"*Newton's method encountered a singularity"))
614 (setq next
(math-add guess
(math-div (math-float (math-neg expr-val
))
615 (math-float jacob-val
)))
617 (math-working "newton" next
)
618 (while (and (setq p
(cdr p
) p2
(cdr p2
))
619 (math-nearly-equal (car p
) (car p2
))))
621 (if (Math-lessp (math-abs-approx (math-sub next orig-guess
))
623 (math-newton-multi expr jacob n next orig-guess limit
)
624 (math-reject-arg nil
"*Newton's method failed to converge"))
625 (list 'vec next expr-val
))))
627 (defvar math-root-vars
[(var DUMMY var-DUMMY
)])
629 (defun math-find-root (expr var guess root-widen
)
630 (if (eq (car-safe expr
) 'vec
)
631 (let ((n (1- (length expr
)))
632 (calc-symbolic-mode nil
)
636 (unless (eq (car-safe var
) 'vec
)
637 (math-reject-arg var
'vectorp
))
638 (unless (= (length var
) (1+ n
))
639 (math-dimension-error))
640 (setq expr
(copy-sequence expr
))
641 (while (>= n
(length math-root-vars
))
642 (let ((symb (intern (concat "math-root-v"
644 (length math-root-vars
))))))
645 (setq math-root-vars
(vconcat math-root-vars
646 (vector (list 'var symb symb
))))))
648 (while (< (setq m
(1+ m
)) n
)
649 (set (nth 2 (aref math-root-vars m
)) nil
))
651 (while (setq m
(1+ m
) p
(cdr p
))
652 (or (eq (car-safe (car p
)) 'var
)
653 (math-reject-arg var
"*Expected a variable"))
655 (while (setq p2
(cdr p2
))
656 (setcar p2
(math-expr-subst (car p2
) (car p
)
657 (aref math-root-vars m
)))))
658 (unless (eq (car-safe guess
) 'vec
)
659 (math-reject-arg guess
'vectorp
))
660 (unless (= (length guess
) (1+ n
))
661 (math-dimension-error))
662 (setq guess
(copy-sequence guess
)
664 (while (setq p
(cdr p
))
665 (or (Math-numberp (car guess
))
666 (math-reject-arg guess
'numberp
))
667 (setcar p
(math-float (car p
))))
669 (while (setq p
(cdr p
))
670 (if (assq (car-safe (car p
)) calc-tweak-eqn-table
)
671 (setcar p
(math-sub (nth 1 (car p
)) (nth 2 (car p
)))))
672 (setcar p
(math-evaluate-expr (car p
)))
673 (setq row
(list 'vec
)
675 (while (< (setq m
(1+ m
)) n
)
676 (nconc row
(list (math-evaluate-expr
677 (or (calcFunc-deriv (car p
)
678 (aref math-root-vars m
)
682 "*Formulas must be differentiable"))))))
683 (nconc jacob
(list row
)))
684 (setq m
(math-abs-approx guess
))
685 (math-newton-multi expr jacob n guess guess
686 (if (math-zerop m
) '(float 1 3) (math-mul m
10))))
687 (unless (eq (car-safe var
) 'var
)
688 (math-reject-arg var
"*Expected a variable"))
689 (unless (math-expr-contains expr var
)
690 (math-reject-arg expr
"*Formula does not contain specified variable"))
691 (if (assq (car expr
) calc-tweak-eqn-table
)
692 (setq expr
(math-sub (nth 1 expr
) (nth 2 expr
))))
693 (math-with-extra-prec 2
694 (setq expr
(math-expr-subst expr var
'(var DUMMY var-DUMMY
)))
695 (let* ((calc-symbolic-mode nil
)
697 (expr (math-evaluate-expr expr
))
698 (deriv (calcFunc-deriv expr
'(var DUMMY var-DUMMY
) nil t
))
700 (and deriv
(setq deriv
(math-evaluate-expr deriv
)))
701 (setq guess
(math-float guess
))
702 (if (and (math-numberp guess
)
704 (math-newton-root expr deriv guess guess
705 (if (math-zerop guess
) '(float 1 6)
706 (math-mul (math-abs-approx guess
) 100)))
707 (if (Math-realp guess
)
711 vlow
(math-evaluate-expr expr
)
714 (if (eq (car guess
) 'intv
)
716 (or (math-constp guess
) (math-reject-arg guess
'constp
))
717 (setq low
(nth 2 guess
)
719 (if (memq (nth 1 guess
) '(0 1))
720 (setq low
(calcFunc-incr low
1 high
)))
721 (if (memq (nth 1 guess
) '(0 2))
722 (setq high
(calcFunc-incr high -
1 low
)))
724 vlow
(math-evaluate-expr expr
)
726 vhigh
(math-evaluate-expr expr
)))
727 (if (math-complexp guess
)
728 (math-reject-arg "*Complex root finder must have derivative")
729 (math-reject-arg guess
'realp
))))
730 (if (Math-zerop vlow
)
732 (if (Math-zerop vhigh
)
733 (list 'vec high vhigh
)
734 (if (and deriv
(Math-numberp vlow
) (Math-numberp vhigh
))
735 (math-newton-search-root expr deriv nil nil nil nil
737 (if (or (and (Math-posp vlow
) (Math-posp vhigh
))
738 (and (Math-negp vlow
) (Math-negp vhigh
))
739 (not (Math-numberp vlow
))
740 (not (Math-numberp vhigh
)))
741 (math-search-root expr deriv low vlow high vhigh
)
742 (math-bisect-root expr low vlow high vhigh
))))))))))
744 (defun calcFunc-root (expr var guess
)
745 (math-find-root expr var guess nil
))
747 (defun calcFunc-wroot (expr var guess
)
748 (math-find-root expr var guess t
))
753 ;;; The following algorithms come from Numerical Recipes, chapter 10.
755 (defun math-min-eval (expr a
)
758 (while (setq m
(1+ m
) a
(cdr a
))
759 (set (nth 2 (aref math-min-vars m
)) (car a
))))
761 (setq a
(math-evaluate-expr expr
))
764 (if (eq (car a
) 'float
)
766 (math-reject-arg a
'realp
))))
768 (defvar math-min-or-max
"minimum")
770 ;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c).
773 (defun math-widen-min (expr a b
)
776 incr c va vb vc u vu r q ulim bc ba qr
)
777 (or b
(setq b
(math-mul a
'(float 101 -
2))))
778 (setq va
(math-min-eval expr a
)
779 vb
(math-min-eval expr b
))
780 (if (math-lessp-float va vb
)
783 (setq c
(math-add-float b
(math-mul-float '(float 161803 -
5)
784 (math-sub-float b a
)))
785 vc
(math-min-eval expr c
))
786 (while (and (not done
) (math-lessp-float vc vb
))
787 (math-working "widen" (list 'intv
0 a c
))
788 (if (= (setq iters
(1- iters
)) 0)
789 (math-reject-arg nil
(format "*Unable to find a %s near the interval"
791 (setq bc
(math-sub-float b c
)
792 ba
(math-sub-float b a
)
793 r
(math-mul-float ba
(math-sub-float vb vc
))
794 q
(math-mul-float bc
(math-sub-float vb va
))
795 qr
(math-sub-float q r
))
796 (if (math-lessp-float (math-abs qr
) '(float 1 -
20))
797 (setq qr
(if (math-negp qr
) '(float -
1 -
20) '(float 1 -
20))))
798 (setq u
(math-sub-float
800 (math-div-float (math-sub-float (math-mul-float bc q
)
801 (math-mul-float ba r
))
802 (math-mul-float '(float 2 0) qr
)))
803 ulim
(math-add-float b
(math-mul-float '(float -
1 2) bc
))
805 (if (if incr
(math-lessp-float b u
) (math-lessp-float u b
))
806 (if (if incr
(math-lessp-float u c
) (math-lessp-float c u
))
807 (if (math-lessp-float (setq vu
(math-min-eval expr u
)) vc
)
811 (if (math-lessp-float vb vu
)
814 (setq u
(math-add-float c
(math-mul-float '(float -
161803 -
5)
816 vu
(math-min-eval expr u
))))
817 (if (if incr
(math-lessp-float u ulim
) (math-lessp-float ulim u
))
818 (if (math-lessp-float (setq vu
(math-min-eval expr u
)) vc
)
821 u
(math-add-float c
(math-mul-float
823 (math-sub-float b c
)))
824 vu
(math-min-eval expr u
)))
826 vu
(math-min-eval expr u
))))
827 (setq u
(math-add-float c
(math-mul-float '(float -
161803 -
5)
829 vu
(math-min-eval expr u
)))
833 (if (math-lessp-float a c
)
834 (list a va b vb c vc
)
835 (list c vc b vb a va
))))
837 (defun math-narrow-min (expr a c intv
)
838 (let ((xvals (list a c
))
839 (yvals (list (math-min-eval expr a
)
840 (math-min-eval expr c
)))
842 (step (math-sub-float c a
))
845 (while (and (<= (setq levels
(1+ levels
)) 5)
849 step
(math-mul-float step
'(float 497 -
3)))
850 (while (and (cdr xp
) (not found
))
851 (setq b
(math-add-float (car xp
) step
))
852 (math-working "search" b
)
853 (setcdr xp
(cons b
(cdr xp
)))
854 (setcdr yp
(cons (math-min-eval expr b
) (cdr yp
)))
855 (if (and (math-lessp-float (nth 1 yp
) (car yp
))
856 (math-lessp-float (nth 1 yp
) (nth 2 yp
)))
860 (if (and (cdr (cdr yp
))
861 (math-lessp-float (nth 1 yp
) (car yp
))
862 (math-lessp-float (nth 1 yp
) (nth 2 yp
)))
867 (list (car xp
) (car yp
)
868 (nth 1 xp
) (nth 1 yp
)
869 (nth 2 xp
) (nth 2 yp
))
870 (or (if (math-lessp-float (car yvals
) (nth 1 yvals
))
871 (and (memq (nth 1 intv
) '(2 3))
872 (let ((min (car yvals
)))
873 (while (and (setq yvals
(cdr yvals
))
874 (math-lessp-float min
(car yvals
))))
876 (list (nth 2 intv
) min
))))
877 (and (memq (nth 1 intv
) '(1 3))
878 (setq yvals
(nreverse yvals
))
879 (let ((min (car yvals
)))
880 (while (and (setq yvals
(cdr yvals
))
881 (math-lessp-float min
(car yvals
))))
883 (list (nth 3 intv
) min
)))))
884 (math-reject-arg nil
(format "*Unable to find a %s in the interval"
885 math-min-or-max
))))))
888 (defun math-brent-min (expr prec a va x vx b vb
)
889 (let ((iters (+ 20 (* 5 prec
)))
894 (tol (list 'float
1 (- -
1 prec
)))
895 (zeps (list 'float
1 (- -
5 prec
)))
897 u vu xm tol1 tol2 etemp p q r xv xw
)
899 (setq xm
(math-mul-float '(float 5 -
1)
900 (math-add-float a b
))
903 (math-mul-float tol
(math-abs x
)))
904 tol2
(math-mul-float tol1
'(float 2 0)))
905 (math-lessp-float (math-sub-float tol2
908 (math-sub-float b a
)))
909 (math-abs (math-sub-float x xm
))))
910 (if (= (setq iters
(1- iters
)) 0)
911 (math-reject-arg nil
(format "*Unable to converge on a %s"
913 (math-working "brent" x
)
914 (if (math-lessp-float (math-abs e
) tol1
)
915 (setq e
(if (math-lessp-float x xm
)
917 (math-sub-float a x
))
918 d
(math-mul-float '(float 381966 -
6) e
))
919 (setq xw
(math-sub-float x w
)
920 r
(math-mul-float xw
(math-sub-float vx vv
))
921 xv
(math-sub-float x v
)
922 q
(math-mul-float xv
(math-sub-float vx vw
))
923 p
(math-sub-float (math-mul-float xv q
)
924 (math-mul-float xw r
))
925 q
(math-mul-float '(float 2 0) (math-sub-float q r
)))
927 (setq p
(math-neg-float p
))
928 (setq q
(math-neg-float q
)))
931 (if (and (math-lessp-float (math-abs p
)
932 (math-abs (math-mul-float
934 (math-mul-float q etemp
))))
935 (math-lessp-float (math-mul-float
936 q
(math-sub-float a x
)) p
)
937 (math-lessp-float p
(math-mul-float
938 q
(math-sub-float b x
))))
940 (setq d
(math-div-float p q
)
941 u
(math-add-float x d
))
942 (if (or (math-lessp-float (math-sub-float u a
) tol2
)
943 (math-lessp-float (math-sub-float b u
) tol2
))
944 (setq d
(if (math-lessp-float xm x
)
945 (math-neg-float tol1
)
947 (setq e
(if (math-lessp-float x xm
)
949 (math-sub-float a x
))
950 d
(math-mul-float '(float 381966 -
6) e
))))
951 (setq u
(math-add-float x
952 (if (math-lessp-float (math-abs d
) tol1
)
954 (math-neg-float tol1
)
957 vu
(math-min-eval expr u
))
958 (if (math-lessp-float vx vu
)
960 (if (math-lessp-float u x
)
964 (not (math-lessp-float vw vu
)))
969 (not (math-lessp-float vv vu
)))
971 (if (math-lessp-float u x
)
980 (defun math-powell-min (expr n guesses prec
)
981 (let* ((f1dim (math-line-min-func expr n
))
982 (xi (calcFunc-idn 1 n
))
983 (p (cons 'vec
(mapcar 'car guesses
)))
985 (ftol (list 'float
1 (- prec
)))
986 (fret (math-min-eval expr p
))
987 fp ptt fptt xit i ibig del diff res
)
993 (while (<= (setq i
(1+ i
)) n
)
995 res
(math-line-min f1dim p
998 p
(let ((calc-internal-prec prec
))
999 (math-normalize (car res
)))
1001 diff
(math-abs (math-sub-float fptt fret
)))
1002 (if (math-lessp-float del diff
)
1006 (math-mul-float ftol
1007 (math-add-float (math-abs fp
)
1009 (math-mul-float '(float 2 0)
1010 (math-abs (math-sub-float fp
1012 (setq ptt
(math-sub (math-mul '(float 2 0) p
) pt
)
1015 fptt
(math-min-eval expr ptt
))
1016 (if (and (math-lessp-float fptt fp
)
1019 (math-mul-float '(float 2 0)
1022 (math-mul-float '(float 2 0)
1025 (math-sqr-float (math-sub-float
1026 (math-sub-float fp fret
) del
)))
1028 (math-sqr-float (math-sub-float fp fptt
)))))
1030 (setq res
(math-line-min f1dim p xit n prec
)
1034 (while (<= (setq i
(1+ i
)) n
)
1035 (setcar (nthcdr ibig
(nth i xi
))
1036 (nth i
(nth 1 res
)))))))
1037 (list 'vec p fret
)))
1039 (defun math-line-min-func (expr n
)
1041 (while (< (setq m
(1+ m
)) n
)
1042 (set (nth 2 (aref math-min-vars m
))
1045 '(var DUMMY var-DUMMY
)
1046 (list 'calcFunc-mrow
'(var line-xi line-xi
) (1+ m
)))
1047 (list 'calcFunc-mrow
'(var line-p line-p
) (1+ m
)))))
1048 (math-evaluate-expr expr
)))
1050 (defun math-line-min (f1dim line-p line-xi n prec
)
1051 (let* ((var-DUMMY nil
)
1052 (expr (math-evaluate-expr f1dim
))
1053 (params (math-widen-min expr
'(float 0 0) '(float 1 0)))
1054 (res (apply 'math-brent-min expr prec params
))
1055 (xi (math-mul (nth 1 res
) line-xi
)))
1056 (list (math-add line-p xi
) xi
(nth 2 res
))))
1059 (defvar math-min-vars
[(var DUMMY var-DUMMY
)])
1061 (defun math-find-minimum (expr var guess min-widen
)
1062 (let* ((calc-symbolic-mode nil
)
1065 (isvec (math-vectorp var
))
1067 (or (math-vectorp var
)
1068 (setq var
(list 'vec var
)))
1069 (or (math-vectorp guess
)
1070 (setq guess
(list 'vec guess
)))
1071 (or (= (length var
) (length guess
))
1072 (math-dimension-error))
1073 (while (setq var
(cdr var
) guess
(cdr guess
))
1074 (or (eq (car-safe (car var
)) 'var
)
1075 (math-reject-arg (car vg
) "*Expected a variable"))
1076 (or (math-expr-contains expr
(car var
))
1077 (math-reject-arg (car var
)
1078 "*Formula does not contain specified variable"))
1079 (while (>= (1+ n
) (length math-min-vars
))
1080 (let ((symb (intern (concat "math-min-v"
1082 (length math-min-vars
))))))
1083 (setq math-min-vars
(vconcat math-min-vars
1084 (vector (list 'var symb symb
))))))
1085 (set (nth 2 (aref math-min-vars n
)) nil
)
1086 (set (nth 2 (aref math-min-vars
(1+ n
))) nil
)
1087 (if (math-complexp (car guess
))
1088 (setq expr
(math-expr-subst expr
1090 (list '+ (aref math-min-vars n
)
1092 (aref math-min-vars
(1+ n
))
1094 guesses
(let ((g (math-float (math-complex (car guess
)))))
1095 (cons (list (nth 2 g
) nil nil
)
1096 (cons (list (nth 1 g
) nil nil t
)
1099 (setq expr
(math-expr-subst expr
1101 (aref math-min-vars n
))
1102 guesses
(cons (if (math-realp (car guess
))
1103 (list (math-float (car guess
)) nil nil
)
1104 (if (and (eq (car-safe (car guess
)) 'intv
)
1105 (math-constp (car guess
)))
1107 (math-add (nth 2 (car guess
))
1108 (nth 3 (car guess
)))
1110 (math-float (nth 2 (car guess
)))
1111 (math-float (nth 3 (car guess
)))
1113 (math-reject-arg (car guess
) 'realp
)))
1116 (setq guesses
(nreverse guesses
)
1117 expr
(math-evaluate-expr expr
))
1119 (let* ((params (if (nth 1 (car guesses
))
1121 (math-widen-min expr
1122 (nth 1 (car guesses
))
1123 (nth 2 (car guesses
)))
1124 (math-narrow-min expr
1125 (nth 1 (car guesses
))
1126 (nth 2 (car guesses
))
1127 (nth 3 (car guesses
))))
1128 (math-widen-min expr
1131 (prec calc-internal-prec
)
1132 (res (if (cdr (cdr params
))
1133 (math-with-extra-prec (+ calc-internal-prec
2)
1134 (apply 'math-brent-min expr prec params
))
1135 (cons 'vec params
))))
1137 (list 'vec
(list 'vec
(nth 1 res
)) (nth 2 res
))
1139 (let* ((prec calc-internal-prec
)
1140 (res (math-with-extra-prec (+ calc-internal-prec
2)
1141 (math-powell-min expr n guesses prec
)))
1144 (while (setq p
(cdr p
))
1145 (if (nth 3 (car guesses
))
1147 (nconc vec
(list (math-normalize
1148 (list 'cplx
(car p
) (nth 1 p
)))))
1150 guesses
(cdr guesses
)))
1151 (nconc vec
(list (car p
))))
1152 (setq guesses
(cdr guesses
)))
1154 (list 'vec vec
(nth 2 res
))
1155 (list 'vec
(nth 1 vec
) (nth 2 res
)))))))
1157 (defun calcFunc-minimize (expr var guess
)
1158 (let ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1159 (math-min-or-max "minimum"))
1160 (math-find-minimum (math-normalize expr
)
1161 (math-normalize var
)
1162 (math-normalize guess
) nil
)))
1164 (defun calcFunc-wminimize (expr var guess
)
1165 (let ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1166 (math-min-or-max "minimum"))
1167 (math-find-minimum (math-normalize expr
)
1168 (math-normalize var
)
1169 (math-normalize guess
) t
)))
1171 (defun calcFunc-maximize (expr var guess
)
1172 (let* ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1173 (math-min-or-max "maximum")
1174 (res (math-find-minimum (math-normalize (math-neg expr
))
1175 (math-normalize var
)
1176 (math-normalize guess
) nil
)))
1177 (list 'vec
(nth 1 res
) (math-neg (nth 2 res
)))))
1179 (defun calcFunc-wmaximize (expr var guess
)
1180 (let* ((calc-internal-prec (max (/ calc-internal-prec
2) 3))
1181 (math-min-or-max "maximum")
1182 (res (math-find-minimum (math-normalize (math-neg expr
))
1183 (math-normalize var
)
1184 (math-normalize guess
) t
)))
1185 (list 'vec
(nth 1 res
) (math-neg (nth 2 res
)))))
1190 ;;; The following algorithms come from Numerical Recipes, chapter 3.
1192 (defun calcFunc-polint (data x
)
1193 (or (math-matrixp data
) (math-reject-arg data
'matrixp
))
1194 (or (= (length data
) 3)
1195 (math-reject-arg data
"*Wrong number of data rows"))
1196 (or (> (length (nth 1 data
)) 2)
1197 (math-reject-arg data
"*Too few data points"))
1198 (if (and (math-vectorp x
) (or (math-constp x
) math-expand-formulas
))
1199 (cons 'vec
(mapcar (function (lambda (x) (calcFunc-polint data x
)))
1201 (or (math-objectp x
) math-expand-formulas
(math-reject-arg x
'objectp
))
1202 (math-with-extra-prec 2
1203 (cons 'vec
(math-poly-interp (cdr (nth 1 data
)) (cdr (nth 2 data
)) x
1205 (put 'calcFunc-polint
'math-expandable t
)
1208 (defun calcFunc-ratint (data x
)
1209 (or (math-matrixp data
) (math-reject-arg data
'matrixp
))
1210 (or (= (length data
) 3)
1211 (math-reject-arg data
"*Wrong number of data rows"))
1212 (or (> (length (nth 1 data
)) 2)
1213 (math-reject-arg data
"*Too few data points"))
1214 (if (and (math-vectorp x
) (or (math-constp x
) math-expand-formulas
))
1215 (cons 'vec
(mapcar (function (lambda (x) (calcFunc-ratint data x
)))
1217 (or (math-objectp x
) math-expand-formulas
(math-reject-arg x
'objectp
))
1218 (math-with-extra-prec 2
1219 (cons 'vec
(math-poly-interp (cdr (nth 1 data
)) (cdr (nth 2 data
)) x
1220 (cdr (cdr (cdr (nth 1 data
)))))))))
1221 (put 'calcFunc-ratint
'math-expandable t
)
1224 (defun math-poly-interp (xa ya x ratp
)
1225 (let ((n (length xa
))
1229 (c (copy-sequence ya
))
1230 (d (copy-sequence ya
))
1233 y dy
(xp xa
) xpm cp dp temp
)
1234 (while (<= (setq i
(1+ i
)) n
)
1235 (setq xax
(cons (math-sub (car xp
) x
) xax
)
1237 temp
(math-abs (car xax
)))
1238 (if (or (null dif
) (math-lessp temp dif
))
1241 (setq xax
(nreverse xax
)
1244 (if (math-zerop dif
)
1246 (while (< (setq m
(1+ m
)) n
)
1252 (while (<= (setq i
(1+ i
)) (- n m
))
1254 (let ((t2 (math-div (math-mul (car xp
) (car dp
)) (car xpm
))))
1255 (setq temp
(math-div (math-sub (nth 1 cp
) (car dp
))
1256 (math-sub t2
(nth 1 cp
))))
1257 (setcar dp
(math-mul (nth 1 cp
) temp
))
1258 (setcar cp
(math-mul t2 temp
)))
1259 (if (math-equal (car xp
) (car xpm
))
1260 (math-reject-arg (cons 'vec xa
) "*Duplicate X values"))
1261 (setq temp
(math-div (math-sub (nth 1 cp
) (car dp
))
1262 (math-sub (car xp
) (car xpm
))))
1263 (setcar dp
(math-mul (car xpm
) temp
))
1264 (setcar cp
(math-mul (car xp
) temp
)))
1269 (if (< (+ ns ns
) (- n m
))
1270 (setq dy
(nth ns c
))
1273 (setq y
(math-add y dy
)))
1278 ;;; The following algorithms come from Numerical Recipes, chapter 4.
1280 (defun calcFunc-ninteg (expr var lo hi
)
1281 (setq lo
(math-evaluate-expr lo
)
1282 hi
(math-evaluate-expr hi
))
1283 (or (math-numberp lo
) (math-infinitep lo
) (math-reject-arg lo
'numberp
))
1284 (or (math-numberp hi
) (math-infinitep hi
) (math-reject-arg hi
'numberp
))
1285 (if (math-lessp hi lo
)
1286 (math-neg (calcFunc-ninteg expr var hi lo
))
1287 (setq expr
(math-expr-subst expr var
'(var DUMMY var-DUMMY
)))
1288 (let ((var-DUMMY nil
)
1289 (calc-symbolic-mode nil
)
1290 (calc-prefer-frac nil
)
1292 (setq expr
(math-evaluate-expr expr
))
1293 (if (equal lo
'(neg (var inf var-inf
)))
1294 (let ((thi (if (math-lessp hi
'(float -
2 0))
1296 (setq sum
(math-ninteg-romberg
1297 'math-ninteg-midpoint expr
1298 (math-float lo
) (math-float thi
) 'inf
)
1300 (if (equal hi
'(var inf var-inf
))
1301 (let ((tlo (if (math-lessp '(float 2 0) lo
)
1303 (setq sum
(math-add sum
1304 (math-ninteg-romberg
1305 'math-ninteg-midpoint expr
1306 (math-float tlo
) (math-float hi
) 'inf
))
1308 (or (math-equal lo hi
)
1309 (setq sum
(math-add sum
1310 (math-ninteg-romberg
1311 'math-ninteg-midpoint expr
1312 (math-float lo
) (math-float hi
) nil
))))
1316 ;;; Open Romberg method; "qromo" in section 4.4.
1317 (defun math-ninteg-romberg (func expr lo hi mode
)
1318 (let ((curh '(float 1 0))
1323 (prec calc-internal-prec
)
1325 (math-with-extra-prec 2
1326 ;; Limit on "j" loop must be 14 or less to keep "it" from overflowing.
1327 (or (while (and (null ss
) (<= (setq j
(1+ j
)) 8))
1328 (setq s
(nconc s
(list (funcall func expr lo hi mode
)))
1329 h
(nconc h
(list curh
)))
1331 (let ((res (math-poly-interp h s
'(float 0 0) nil
)))
1332 (if (math-lessp (math-abs (nth 1 res
))
1333 (calcFunc-scf (math-abs (car res
))
1335 (setq math-ninteg-convergence j
1340 (setq curh
(math-div-float curh
'(float 9 0))))
1342 (math-reject-arg nil
(format "*Integral failed to converge"))))))
1345 (defun math-ninteg-evaluate (expr x mode
)
1347 (setq x
(math-div '(float 1 0) x
)))
1348 (let* ((var-DUMMY x
)
1349 (res (math-evaluate-expr expr
)))
1350 (or (Math-numberp res
)
1351 (math-reject-arg res
"*Integrand does not evaluate to a number"))
1353 (setq res
(math-mul res
(math-sqr x
))))
1357 (defun math-ninteg-midpoint (expr lo hi mode
) ; uses "integ-temp"
1359 (let ((math-infinite-mode t
) temp
)
1360 (setq temp
(math-div 1 lo
)
1364 (let* ((it3 (* 3 (car integ-temp
)))
1365 (math-working-step-2 (* 2 (car integ-temp
)))
1366 (math-working-step 0)
1367 (range (math-sub hi lo
))
1368 (del (math-div range
(math-float it3
)))
1369 (del2 (math-add del del
))
1370 (del3 (math-add del del2
))
1371 (x (math-add lo
(math-mul '(float 5 -
1) del
)))
1374 (while (<= (setq j
(1+ j
)) (car integ-temp
))
1375 (setq math-working-step
(1+ math-working-step
)
1376 temp
(math-ninteg-evaluate expr x mode
)
1377 math-working-step
(1+ math-working-step
)
1378 sum
(math-add sum
(math-add temp
(math-ninteg-evaluate
1379 expr
(math-add x del2
)
1381 x
(math-add x del3
)))
1382 (setq integ-temp
(list it3
1383 (math-add (math-div (nth 1 integ-temp
)
1385 (math-mul sum del
)))))
1386 (setq integ-temp
(list 1 (math-mul
1388 (math-ninteg-evaluate
1390 (math-mul (math-add lo hi
) '(float 5 -
1))
1398 ;;; The following algorithms come from Numerical Recipes, chapter 14.
1400 (defvar math-dummy-vars
[(var DUMMY var-DUMMY
)])
1401 (defvar math-dummy-counter
0)
1402 (defun math-dummy-variable ()
1403 (if (= math-dummy-counter
(length math-dummy-vars
))
1404 (let ((symb (intern (format "math-dummy-%d" math-dummy-counter
))))
1405 (setq math-dummy-vars
(vconcat math-dummy-vars
1406 (vector (list 'var symb symb
))))))
1407 (set (nth 2 (aref math-dummy-vars math-dummy-counter
)) nil
)
1409 (aref math-dummy-vars math-dummy-counter
)
1410 (setq math-dummy-counter
(1+ math-dummy-counter
))))
1412 (defvar math-in-fit
0)
1413 (defvar calc-fit-to-trail nil
)
1415 (defun calcFunc-fit (expr vars
&optional coefs data
)
1416 (let ((math-in-fit 10))
1417 (math-with-extra-prec 2
1418 (math-general-fit expr vars coefs data nil
))))
1420 (defun calcFunc-efit (expr vars
&optional coefs data
)
1421 (let ((math-in-fit 10))
1422 (math-with-extra-prec 2
1423 (math-general-fit expr vars coefs data
'sdev
))))
1425 (defun calcFunc-xfit (expr vars
&optional coefs data
)
1426 (let ((math-in-fit 10))
1427 (math-with-extra-prec 2
1428 (math-general-fit expr vars coefs data
'full
))))
1430 (defun math-general-fit (expr vars coefs data mode
)
1431 (let ((calc-simplify-mode nil
)
1432 (math-dummy-counter math-dummy-counter
)
1434 (extended (eq mode
'full
))
1435 (first-coef math-dummy-counter
)
1439 have-sdevs need-chisq chisq
1447 (var-YVAL nil
) (var-YVALX nil
)
1449 n nn m mm v dummy p
)
1451 ;; Validate and parse arguments.
1456 (if (math-vectorp expr
)
1457 (if (memq (length expr
) '(3 4))
1462 (math-dimension-error))
1466 (or (math-matrixp data
) (math-reject-arg data
'matrixp
))
1467 (setq v
(1- (length data
))
1468 n
(1- (length (nth 1 data
))))
1469 (or (math-vectorp vars
) (null vars
)
1470 (setq vars
(list 'vec vars
)))
1471 (or (math-vectorp coefs
) (null coefs
)
1472 (setq coefs
(list 'vec coefs
)))
1474 (setq coefs
(cons 'vec
(math-all-vars-but expr vars
))))
1476 (if (<= (1- (length coefs
)) v
)
1477 (math-reject-arg coefs
"*Not enough variables in model")
1478 (setq coefs
(copy-sequence coefs
))
1479 (let ((p (nthcdr (- (length coefs
) v
1480 (if (eq (car-safe expr
) 'calcFunc-eq
) 1 0))
1482 (setq vars
(cons 'vec
(cdr p
)))
1484 (or (= (1- (length vars
)) v
)
1486 (math-reject-arg vars
"*Number of variables does not match data"))
1487 (setq m
(1- (length coefs
)))
1489 (math-reject-arg coefs
"*Need at least one parameter"))
1491 ;; Rewrite expr in terms of fitparam and fitvar, make into an equation.
1493 (while (setq p
(cdr p
))
1494 (or (eq (car-safe (car p
)) 'var
)
1495 (math-reject-arg (car p
) "*Expected a variable"))
1496 (setq dummy
(math-dummy-variable)
1497 expr
(math-expr-subst expr
(car p
)
1498 (list 'calcFunc-fitparam
1499 (- math-dummy-counter first-coef
)))))
1500 (setq first-var math-dummy-counter
1502 (while (setq p
(cdr p
))
1503 (or (eq (car-safe (car p
)) 'var
)
1504 (math-reject-arg (car p
) "*Expected a variable"))
1505 (setq dummy
(math-dummy-variable)
1506 expr
(math-expr-subst expr
(car p
)
1507 (list 'calcFunc-fitvar
1508 (- math-dummy-counter first-var
)))))
1509 (if (< math-dummy-counter
(+ first-var v
))
1510 (setq dummy
(math-dummy-variable))) ; dependent variable may be unnamed
1513 (or (eq (car-safe expr
) 'calcFunc-eq
)
1514 (setq expr
(list 'calcFunc-eq
(list 'calcFunc-fitvar v
) expr
)))
1516 (let ((calc-symbolic-mode nil
))
1518 ;; Apply rewrites to put expr into a linear-like form.
1519 (setq expr
(math-evaluate-expr expr
)
1520 expr
(math-rewrite (list 'calcFunc-fitmodel expr
)
1521 '(var FitRules var-FitRules
))
1523 expr
(math-evaluate-expr expr
))
1524 (or (and (eq (car-safe expr
) 'calcFunc-fitsystem
)
1526 (math-vectorp (nth 2 expr
))
1527 (math-vectorp (nth 3 expr
))
1528 (> (length (nth 2 expr
)) 1)
1529 (= (length (nth 3 expr
)) (1+ m
)))
1530 (math-reject-arg plain-expr
"*Model expression is too complex"))
1531 (setq y-filter
(nth 1 expr
)
1532 x-funcs
(vconcat (cdr (nth 2 expr
)))
1533 coef-filters
(nth 3 expr
)
1534 mm
(length x-funcs
))
1535 (if (equal y-filter y-dummy
)
1536 (setq y-filter nil
))
1538 ;; Build the (square) system of linear equations to be solved.
1539 (setq beta
(cons 'vec
(make-list mm
0))
1540 covar
(cons 'vec
(mapcar 'copy-sequence
(make-list mm beta
))))
1541 (let* ((ptrs (vconcat (cdr data
)))
1543 (xvals (make-vector mm
0))
1545 j k xval yval sigmasqr wt covj covjk covk betaj lud
)
1546 (while (<= (setq i
(1+ i
)) n
)
1548 ;; Assign various independent variables for this data point.
1552 (aset ptrs j
(cdr (aref ptrs j
)))
1553 (setq xval
(car (aref ptrs j
)))
1557 (if (eq (car-safe xval
) 'sdev
)
1558 (setq sigmasqr
(math-add (math-sqr (nth 2 xval
))
1562 (setq xval
(math-make-sdev xval
1563 (math-sqrt sigmasqr
))))))
1564 (if (eq (car-safe xval
) 'sdev
)
1565 (setq sigmasqr
(math-add (math-sqr (nth 2 xval
))
1567 xval
(nth 1 xval
))))
1568 (set (nth 2 (aref math-dummy-vars
(+ first-var j
))) xval
)
1571 ;; Compute Y value for this data point.
1573 (setq yval
(math-evaluate-expr y-filter
))
1574 (setq yval
(symbol-value (nth 2 y-dummy
))))
1575 (if (eq (car-safe yval
) 'sdev
)
1576 (setq sigmasqr
(math-sqr (nth 2 yval
))
1579 (setq have-sdevs sigmasqr
1580 need-chisq
(or extended
1581 (and (eq mode
'sdev
) (not have-sdevs
)))))
1585 (setq isigsq
(math-div 1 sigmasqr
))
1587 (setq weights
(cons isigsq weights
))))
1588 (math-reject-arg yval
"*Mixed error forms and plain numbers"))
1590 (math-reject-arg yval
"*Mixed error forms and plain numbers")))
1592 ;; Compute X values for this data point and update covar and beta.
1593 (if (eq (car-safe xval
) 'sdev
)
1594 (set (nth 2 y-dummy
) (nth 1 xval
)))
1599 (setq wt
(math-evaluate-expr (aref x-funcs j
)))
1601 (setq wt
(math-mul wt isigsq
)
1603 covjk
(car (setq covj
(cdr covj
)))
1606 (setq covjk
(cdr covjk
))
1607 (setcar covjk
(math-add (car covjk
)
1608 (math-mul wt
(aref xvals k
))))
1610 (setcar betaj
(math-add (car betaj
) (math-mul wt yval
)))
1613 (setq xy-values
(cons (append xvals
(list yval
)) xy-values
))))
1615 ;; Fill in symmetric half of covar matrix.
1618 (while (< j
(1- mm
))
1621 covjk
(nthcdr j
(car (setq covj
(cdr covj
))))
1622 covk
(nthcdr j covar
))
1623 (while (< (setq k
(1+ k
)) mm
)
1624 (setq covjk
(cdr covjk
)
1626 (setcar covjk
(nth j
(car covk
))))))
1628 ;; Solve the linear system.
1631 (setq covar
(math-matrix-inv-raw covar
))
1633 (setq beta
(math-mul covar beta
))
1634 (if (math-zerop (math-abs beta
))
1635 (setq covar
(calcFunc-diag 0 (1- (length beta
))))
1636 (math-reject-arg orig-expr
"*Singular matrix")))
1637 (or (math-vectorp covar
)
1638 (setq covar
(list 'vec
(list 'vec covar
)))))
1639 (setq beta
(math-div beta covar
)))
1641 ;; Compute chi-square statistic if necessary.
1649 (while (setq bp
(cdr bp
))
1650 (setq sum
(math-add sum
(math-mul (car bp
) (car xp
)))
1652 (setq sum
(math-sqr (math-sub (car xp
) sum
)))
1653 (if weights
(setq sum
(math-mul sum
(car weights
))))
1654 (setq chisq
(math-add chisq sum
)
1655 weights
(cdr weights
)
1656 xy-values
(cdr xy-values
)))))
1658 ;; Convert coefficients back into original terms.
1659 (setq new-coefs
(copy-sequence beta
))
1660 (let* ((bp new-coefs
)
1665 (and mode
(not have-sdevs
)
1666 (setq sigdat
(if (<= n mm
)
1668 (math-div chisq
(- n mm
)))))
1670 (while (setq bp
(cdr bp
))
1671 (setcar bp
(math-make-sdev
1673 (math-sqrt (math-mul (nth (setq j
(1+ j
))
1674 (car (setq cp
(cdr cp
))))
1676 (setq new-coefs
(math-evaluate-expr coef-filters
))
1677 (if calc-fit-to-trail
1678 (let ((bp new-coefs
)
1681 (while (setq bp
(cdr bp
) cp
(cdr cp
))
1682 (setq vec
(cons (list 'calcFunc-eq
(car cp
) (car bp
)) vec
)))
1683 (setq calc-fit-to-trail
(cons 'vec
(nreverse vec
)))))))
1685 ;; Substitute best-fit coefficients back into original formula.
1686 (setq expr
(math-multi-subst
1691 (setq vec
(cons (list 'calcFunc-fitvar n
) vec
)
1695 (setq vec
(cons (list 'calcFunc-fitparam n
) vec
)
1698 (append (cdr new-coefs
) (cdr vars
))))
1700 ;; Package the result.
1703 (list 'vec expr beta covar
1704 (let ((p coef-filters
)
1706 (while (and (setq n
(1+ n
) p
(cdr p
))
1707 (eq (car-safe (car p
)) 'calcFunc-fitdummy
)
1708 (eq (nth 1 (car p
)) n
)))
1713 (if (and have-sdevs
(> n mm
))
1714 (list 'calcFunc-utpc chisq
(- n mm
))
1715 '(var nan var-nan
)))
1719 (defun calcFunc-fitvar (x)
1720 (if (>= math-in-fit
2)
1722 (setq x
(aref math-dummy-vars
(+ first-var x -
1)))
1723 (or (calc-var-value (nth 2 x
)) x
))
1724 (math-reject-arg x
)))
1726 (defun calcFunc-fitparam (x)
1727 (if (>= math-in-fit
2)
1729 (setq x
(aref math-dummy-vars
(+ first-coef x -
1)))
1730 (or (calc-var-value (nth 2 x
)) x
))
1731 (math-reject-arg x
)))
1733 (defun calcFunc-fitdummy (x)
1734 (if (= math-in-fit
3)
1736 (math-reject-arg x
)))
1738 (defun calcFunc-hasfitvars (expr)
1739 (if (Math-primp expr
)
1741 (if (eq (car expr
) 'calcFunc-fitvar
)
1743 (apply 'max
(mapcar 'calcFunc-hasfitvars
(cdr expr
))))))
1745 (defun calcFunc-hasfitparams (expr)
1746 (if (Math-primp expr
)
1748 (if (eq (car expr
) 'calcFunc-fitparam
)
1750 (apply 'max
(mapcar 'calcFunc-hasfitparams
(cdr expr
))))))
1753 (defun math-all-vars-but (expr but
)
1754 (let* ((vars (math-all-vars-in expr
))
1757 (setq vars
(delq (assoc (car-safe p
) vars
) vars
)
1759 (sort (mapcar 'car vars
)
1760 (function (lambda (x y
) (string< (nth 1 x
) (nth 1 y
)))))))
1762 (defun math-all-vars-in (expr)
1765 (math-all-vars-rec expr
)
1768 (defun math-all-vars-rec (expr)
1769 (if (Math-primp expr
)
1770 (if (eq (car-safe expr
) 'var
)
1771 (or (math-const-var expr
)
1772 (if (setq found
(assoc expr vars
))
1773 (setcdr found
(1+ (cdr found
)))
1774 (setq vars
(cons (cons expr
1) vars
)))))
1775 (while (setq expr
(cdr expr
))
1776 (math-all-vars-rec (car expr
)))))
1778 ;;; arch-tag: ff9f2920-8111-48b5-b3fa-b0682c3e44a6
1779 ;;; calcalg3.el ends here