1 ;;; calcalg3.el --- more algebraic functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 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 <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
32 (defun math-map-binop (binop args1 args2)
33 "Apply BINOP to the elements of the lists ARGS1 and ARGS2"
36 (funcall binop (car args1) (car args2))
37 (funcall 'math-map-binop binop (cdr args1) (cdr args2)))))
39 (defun calc-find-root (var)
40 (interactive "sVariable(s) to solve for: ")
42 (let ((func (if (calc-is-hyperbolic) 'calcFunc-wroot 'calcFunc-root)))
43 (if (or (equal var "") (equal var "$"))
44 (calc-enter-result 2 "root" (list func
48 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
49 (not (string-match "\\[" var)))
50 (math-read-expr (concat "[" var "]"))
51 (math-read-expr var))))
52 (if (eq (car-safe var) 'error)
53 (error "Bad format in expression: %s" (nth 1 var)))
54 (calc-enter-result 1 "root" (list func
59 (defun calc-find-minimum (var)
60 (interactive "sVariable(s) to minimize over: ")
62 (let ((func (if (calc-is-inverse)
63 (if (calc-is-hyperbolic)
64 'calcFunc-wmaximize 'calcFunc-maximize)
65 (if (calc-is-hyperbolic)
66 'calcFunc-wminimize 'calcFunc-minimize)))
67 (tag (if (calc-is-inverse) "max" "min")))
68 (if (or (equal var "") (equal var "$"))
69 (calc-enter-result 2 tag (list func
73 (let ((var (if (and (string-match ",\\|[^ ] +[^ ]" var)
74 (not (string-match "\\[" var)))
75 (math-read-expr (concat "[" var "]"))
76 (math-read-expr var))))
77 (if (eq (car-safe var) 'error)
78 (error "Bad format in expression: %s" (nth 1 var)))
79 (calc-enter-result 1 tag (list func
84 (defun calc-find-maximum (var)
85 (interactive "sVariable to maximize over: ")
87 (calc-find-minimum var))
90 (defun calc-poly-interp (arg)
93 (let ((data (calc-top 2)))
94 (if (or (consp arg) (eq arg 0) (eq arg 2))
95 (setq data (cons 'vec (calc-top-list 2 2)))
97 (error "Bad prefix argument")))
98 (if (calc-is-hyperbolic)
99 (calc-enter-result 1 "rati" (list 'calcFunc-ratint data (calc-top 1)))
100 (calc-enter-result 1 "poli" (list 'calcFunc-polint data
103 ;; The variables calc-curve-nvars, calc-curve-varnames,
104 ;; calc-curve-model and calc-curve-coefnames are local to calc-curve-fit, but are
105 ;; used by calc-get-fit-variables which is called by calc-curve-fit.
106 (defvar calc-curve-nvars)
107 (defvar calc-curve-varnames)
108 (defvar calc-curve-model)
109 (defvar calc-curve-coefnames)
111 (defvar calc-curve-fit-history nil
112 "History for calc-curve-fit.")
114 (defvar calc-graph-no-auto-view)
116 (defun calc-curve-fit (arg &optional calc-curve-model
117 calc-curve-coefnames calc-curve-varnames)
120 (setq calc-aborted-prefix nil)
121 (let ((func (if (calc-is-inverse) 'calcFunc-xfit
122 (if (calc-is-hyperbolic) 'calcFunc-efit
127 n calc-curve-nvars data ;temp
129 (msgs '( "(Press ? for help)"
130 "1 = linear or multilinear"
131 "2-9 = polynomial fits; i = interpolating polynomial"
132 "p = a x^b, ^ = a b^x"
133 "e = a exp(b x), x = exp(a + b x), l = a + b ln(x)"
134 "E = a 10^(b x), X = 10^(a + b x), L = a + b log10(x)"
136 "g = (a/b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2)"
137 "s = a/(1 + exp(b (x - c)))"
138 "b = a exp(b (x - c))/(1 + exp(b (x - c)))^2"
139 "o = (y/x) = a (1 - x/b)"
140 "h prefix = homogeneous model (no constant term)"
141 "P prefix = plot result"
142 "' = alg entry, $ = stack, u = Model1, U = Model2")))
143 (while (not calc-curve-model)
145 "Fit to model: %s:%s%s"
149 (setq key (read-char))
153 (setq which (% (1+ which) (length msgs))))
155 (setq homog (not homog)))
159 (let ((data (calc-top 1)))
163 (not (= (length data) 3)))
164 (setq plot "Can't plot")
173 ((or (consp arg) (eq arg 0))
176 data (if (math-matrixp data)
177 (append data (list (calc-top (1- n))))
178 (list 'vec data (calc-top (1- n))))))
179 ((> (setq arg (prefix-numeric-value arg)) 0)
180 (setq data (cons 'vec (calc-top-list arg (1+ n)))
182 (t (error "Bad prefix argument")))
183 (or (math-matrixp data) (not (cdr (cdr data)))
184 (error "Data matrix is not a matrix!"))
185 (setq calc-curve-nvars (- (length data) 2)
186 calc-curve-coefnames nil
187 calc-curve-varnames nil)
189 ((= key ?1) ; linear or multilinear
190 (calc-get-fit-variables calc-curve-nvars
191 (1+ calc-curve-nvars) (and homog 0))
192 (setq calc-curve-model
193 (math-mul calc-curve-coefnames
194 (cons 'vec (cons 1 (cdr calc-curve-varnames))))))
195 ((and (>= key ?2) (<= key ?9)) ; polynomial
196 (calc-get-fit-variables 1 (- key ?0 -1) (and homog 0))
197 (setq calc-curve-model
198 (math-build-polynomial-expr (cdr calc-curve-coefnames)
199 (nth 1 calc-curve-varnames))))
200 ((= key ?i) ; exact polynomial
201 (calc-get-fit-variables 1 (1- (length (nth 1 data)))
203 (setq calc-curve-model
204 (math-build-polynomial-expr (cdr calc-curve-coefnames)
205 (nth 1 calc-curve-varnames))))
206 ((= key ?p) ; power law
207 (calc-get-fit-variables calc-curve-nvars
208 (1+ calc-curve-nvars) (and homog 1))
209 (setq calc-curve-model
211 (nth 1 calc-curve-coefnames)
217 (cons 'vec (cdr (cdr calc-curve-coefnames))))))))
218 ((= key ?^) ; exponential law
219 (calc-get-fit-variables calc-curve-nvars
220 (1+ calc-curve-nvars) (and homog 1))
221 (setq calc-curve-model
222 (math-mul (nth 1 calc-curve-coefnames)
227 (cons 'vec (cdr (cdr calc-curve-coefnames)))
228 calc-curve-varnames)))))
231 (setq calc-curve-model t)
232 (require 'calc-nlfit)
233 (calc-fit-s-shaped-logistic-curve func))
236 (setq calc-curve-model t)
237 (require 'calc-nlfit)
238 (calc-fit-bell-shaped-logistic-curve func))
241 (setq calc-curve-model t)
242 (require 'calc-nlfit)
243 (if (and plot (not (stringp plot)))
249 (math-map-binop 'calcFunc-div
251 (cdr (nth 1 plot)))))))
252 (calc-fit-hubbert-linear-curve func))
254 (calc-get-fit-variables calc-curve-nvars
255 (1+ calc-curve-nvars) (and homog 1))
256 (setq calc-curve-model
257 (math-mul (nth 1 calc-curve-coefnames)
265 (^ 10 (var a var-a))))
268 (cons 'vec (cdr (cdr calc-curve-coefnames)))
269 calc-curve-varnames))))))
271 (calc-get-fit-variables calc-curve-nvars
272 (1+ calc-curve-nvars) (and homog 0))
273 (setq calc-curve-model
274 (math-mul calc-curve-coefnames
275 (cons 'vec (cons 1 (cdr calc-curve-varnames)))))
276 (setq calc-curve-model (if (eq key ?x)
277 (list 'calcFunc-exp calc-curve-model)
278 (list '^ 10 calc-curve-model))))
280 (calc-get-fit-variables calc-curve-nvars
281 (1+ calc-curve-nvars) (and homog 0))
282 (setq calc-curve-model
283 (math-mul calc-curve-coefnames
285 (cons 1 (cdr (calcFunc-map
290 calc-curve-varnames)))))))
292 (calc-get-fit-variables calc-curve-nvars
293 (1+ (* 2 calc-curve-nvars)) (and homog 0))
294 (let ((c calc-curve-coefnames)
295 (v calc-curve-varnames))
296 (setq calc-curve-model (nth 1 c))
297 (while (setq v (cdr v) c (cdr (cdr c)))
298 (setq calc-curve-model (math-add
303 (list '- (car v) (nth 1 c))
309 "(AFit / BFit sqrt(2 pi)) exp(-0.5 * ((XFit - CFit) / BFit)^2)")
310 calc-curve-varnames '(vec (var XFit var-XFit))
311 calc-curve-coefnames '(vec (var AFit var-AFit)
313 (var CFit var-CFit)))
314 (calc-get-fit-variables 1 (1- (length calc-curve-coefnames))
316 ((memq key '(?\$ ?\' ?u ?U))
317 (let* (;(defvars nil)
320 (let* ((calc-dollar-values calc-arg-values)
322 (calc-hashes-used 0))
323 (setq calc-curve-model
324 (calc-do-alg-entry "" "Model formula: "
325 nil 'calc-curve-fit-history))
326 (if (/= (length calc-curve-model) 1)
327 (error "Bad format"))
328 (setq calc-curve-model (car calc-curve-model)
330 (if (> calc-dollar-used 0)
331 (setq calc-curve-coefnames
333 (nthcdr (- (length calc-arg-values)
335 (reverse calc-arg-values))))
336 (if (> calc-hashes-used 0)
337 (setq calc-curve-coefnames
338 (cons 'vec (calc-invent-args
339 calc-hashes-used))))))
341 (setq calc-curve-model (cond ((eq key ?u)
342 (calc-var-value 'var-Model1))
344 (calc-var-value 'var-Model2))
346 (or calc-curve-model (error "User model not yet defined"))
347 (if (math-vectorp calc-curve-model)
348 (if (and (memq (length calc-curve-model) '(3 4))
349 (not (math-objvecp (nth 1 calc-curve-model)))
350 (math-vectorp (nth 2 calc-curve-model))
351 (or (null (nth 3 calc-curve-model))
352 (math-vectorp (nth 3 calc-curve-model))))
353 (setq calc-curve-varnames (nth 2 calc-curve-model)
355 (or (nth 3 calc-curve-model)
359 calc-curve-varnames)))
360 calc-curve-model (nth 1 calc-curve-model))
361 (error "Incorrect model specifier")))))
362 (or calc-curve-varnames
364 (eq (car-safe calc-curve-model) 'calcFunc-eq)))
365 (if calc-curve-coefnames
366 (calc-get-fit-variables
367 (if with-y (1+ calc-curve-nvars) calc-curve-nvars)
368 (1- (length calc-curve-coefnames))
370 calc-curve-model calc-curve-coefnames)
372 (let* ((coefs (math-all-vars-but calc-curve-model nil))
380 (error "Not enough variables in model"))
381 (setq p (nthcdr n coefs))
384 (calc-get-fit-variables
385 (if with-y (1+ calc-curve-nvars) calc-curve-nvars)
387 vars coefs with-y)))))
389 (calc-record (list 'vec calc-curve-model
390 calc-curve-varnames calc-curve-coefnames)
394 (let ((calc-fit-to-trail t))
395 (calc-enter-result n (substring (symbol-name func) 9)
396 (list func calc-curve-model
397 (if (= (length calc-curve-varnames) 2)
398 (nth 1 calc-curve-varnames)
400 (if (= (length calc-curve-coefnames) 2)
401 (nth 1 calc-curve-coefnames)
402 calc-curve-coefnames)
404 (if (consp calc-fit-to-trail)
405 (calc-record (calc-normalize calc-fit-to-trail) "parm"))))
409 (let ((calc-graph-no-auto-view t))
410 (calc-graph-delete t)
411 (calc-graph-add-curve
412 (calc-graph-lookup (nth 1 plot))
413 (calc-graph-lookup (nth 2 plot)))
414 (unless (math-contains-sdev-p (nth 2 data))
415 (calc-graph-set-styles nil nil)
416 (calc-graph-point-style nil))
417 (setq plot (cdr (nth 1 plot)))
422 (math-min-list (car plot) (cdr plot))
426 (math-max-list (car plot) (cdr plot)))))
427 (calc-graph-add-curve (calc-graph-lookup plot)
428 (calc-graph-lookup (calc-top-n 1)))
429 (calc-graph-plot nil)))))))
431 (defun calc-invent-independent-variables (n &optional but)
432 (calc-invent-variables n but '(x y z t) "x"))
434 (defun calc-invent-parameter-variables (n &optional but)
435 (calc-invent-variables n but '(a b c d) "a"))
437 (defun calc-invent-variables (num but names base)
441 (while (and (> n 0) names)
442 (setq var (math-build-var-name (if (consp names)
444 (concat base (int-to-string
445 (setq nn (1+ nn)))))))
446 (or (math-expr-contains (cons 'vec but) var)
447 (setq vars (cons var vars)
449 (or (symbolp names) (setq names (cdr names))))
452 (calc-invent-variables num but t base))))
454 (defun calc-get-fit-variables (nv nc &optional defv defc with-y homog)
455 (or (= nv (if with-y (1+ calc-curve-nvars) calc-curve-nvars))
456 (error "Wrong number of data vectors for this type of model"))
463 (setq defv (calc-invent-independent-variables nv)))
465 (setq defc (calc-invent-parameter-variables nc defv)))
466 (let ((vars (read-string (format "Fitting variables (default %s; %s): "
467 (mapconcat 'symbol-name
468 (mapcar (function (lambda (v)
472 (mapconcat 'symbol-name
473 (mapcar (function (lambda (v)
478 (setq vars (if (string-match "\\[" vars)
479 (math-read-expr vars)
480 (math-read-expr (concat "[" vars "]"))))
481 (if (eq (car-safe vars) 'error)
482 (error "Bad format in expression: %s" (nth 2 vars)))
483 (or (math-vectorp vars)
484 (error "Expected a variable or vector of variables"))
485 (if (equal vars '(vec))
486 (setq vars (cons 'vec defv)
487 coefs (cons 'vec defc))
488 (if (math-vectorp (nth 1 vars))
489 (if (and (= (length vars) 3)
490 (math-vectorp (nth 2 vars)))
491 (setq coefs (nth 2 vars)
494 "Expected independent variables vector, then parameters vector"))
495 (setq coefs (cons 'vec defc))))
496 (or (= nv (1- (length vars)))
497 (and (not with-y) (= (1+ nv) (1- (length vars))))
498 (error "Expected %d independent variable%s" nv (if (= nv 1) "" "s")))
499 (or (= nc (1- (length coefs)))
500 (error "Expected %d parameter variable%s" nc (if (= nc 1) "" "s")))
502 (setq coefs (cons 'vec (cons homog (cdr coefs)))))
503 (if calc-curve-varnames
504 (setq calc-curve-model (math-multi-subst calc-curve-model (cdr calc-curve-varnames) (cdr vars))))
505 (if calc-curve-coefnames
506 (setq calc-curve-model (math-multi-subst calc-curve-model (cdr calc-curve-coefnames) (cdr coefs))))
507 (setq calc-curve-varnames vars
508 calc-curve-coefnames coefs)))
513 ;;; The following algorithms are from Numerical Recipes chapter 9.
515 ;;; "rtnewt" with safety kludges
519 (defun math-newton-root (expr deriv guess orig-guess limit)
520 (math-working "newton" guess)
521 (let* ((var-DUMMY guess)
523 (setq next (math-evaluate-expr expr)
524 dval (math-evaluate-expr deriv))
525 (if (and (Math-numberp next)
527 (not (Math-zerop dval)))
529 (setq next (math-sub guess (math-div next dval)))
530 (if (math-nearly-equal guess (setq next (math-float next)))
532 (setq var-DUMMY next)
533 (list 'vec next (math-evaluate-expr expr)))
534 (if (Math-lessp (math-abs-approx (math-sub next orig-guess))
536 (math-newton-root expr deriv next orig-guess limit)
537 (math-reject-arg next "*Newton's method failed to converge"))))
538 (math-reject-arg next "*Newton's method encountered a singularity"))))
540 ;;; Inspired by "rtsafe"
541 (defun math-newton-search-root (expr deriv guess vguess ostep oostep
543 (let ((var-DUMMY guess)
547 (math-working "newton" (list 'intv 0 low high))
548 (math-working "bisect" (list 'intv 0 low high))
549 (setq ostep (math-mul-float (math-sub-float high low)
551 guess (math-add-float low ostep)
553 vguess (math-evaluate-expr expr))
554 (or (Math-realp vguess)
556 (setq ostep (math-mul-float ostep '(float 6 -1))
557 guess (math-add-float low ostep)
559 vguess (math-evaluate-expr expr))
560 (or (math-realp vguess)
562 (setq ostep (math-mul-float ostep '(float 123456 -5))
563 guess (math-add-float low ostep)
567 (setq vguess (math-evaluate-expr expr)))
568 (or (Math-realp vguess)
569 (math-reject-arg guess "*Newton's method encountered a singularity"))
570 (setq vguess (math-float vguess))
571 (if (eq (Math-negp vlow) (setq pos (Math-posp vguess)))
574 (if (eq (Math-negp vhigh) pos)
578 (if (or (Math-zerop vguess)
579 (math-nearly-equal low high))
580 (list 'vec guess vguess)
581 (setq step (math-evaluate-expr deriv))
582 (if (and (Math-realp step)
583 (not (Math-zerop step))
584 (setq step (math-div-float vguess (math-float step))
585 next (math-sub-float guess step))
586 (not (math-lessp-float high next))
587 (not (math-lessp-float next low)))
590 vnext (math-evaluate-expr expr))
591 (if (or (Math-zerop vnext)
592 (math-nearly-equal next guess))
593 (list 'vec next vnext)
595 (math-lessp-float (math-abs (or oostep
599 (math-mul-float '(float 2 0)
601 (math-newton-search-root expr deriv nil nil nil ostep
603 (math-newton-search-root expr deriv next vnext step ostep
604 low vlow high vhigh))))
605 (if (or (and (Math-posp vlow) (Math-posp vhigh))
606 (and (Math-negp vlow) (Math-negp vhigh)))
607 (math-search-root expr deriv low vlow high vhigh)
608 (math-newton-search-root expr deriv nil nil nil ostep
609 low vlow high vhigh))))))
611 ;;; Search for a root in an interval with no overt zero crossing.
613 ;; The variable math-root-widen is local to math-find-root, but
614 ;; is used by math-search-root, which is called (directly and
615 ;; indirectly) by math-find-root.
616 (defvar math-root-widen)
618 (defun math-search-root (expr deriv low vlow high vhigh)
622 (iterlim (if (eq math-root-widen 'point)
623 (+ calc-internal-prec 10)
625 (factor (if (eq math-root-widen 'point)
628 (prev nil) vprev waslow
630 (while (or (and (math-posp vlow) (math-posp vhigh))
631 (and (math-negp vlow) (math-negp vhigh)))
632 (math-working "widen" (list 'intv 0 low high))
633 (if (> (setq iters (1+ iters)) iterlim)
634 (math-reject-arg (list 'intv 0 low high)
635 "*Unable to bracket root"))
636 (if (= iters calc-internal-prec)
637 (setq factor '(float 16 -1)))
638 (setq diff (math-mul-float (math-sub-float high low) factor))
639 (if (Math-zerop diff)
640 (setq high (calcFunc-incr high 10))
641 (if (math-lessp-float (math-abs vlow) (math-abs vhigh))
644 low (math-sub low diff)
647 vlow (math-evaluate-expr expr))
650 high (math-add high diff)
653 vhigh (math-evaluate-expr expr)))))
656 (setq high prev vhigh vprev)
657 (setq low prev vlow vprev)))
659 (or (Math-realp vlow)
660 (math-reject-arg vlow 'realp))
661 (or (Math-realp vhigh)
662 (math-reject-arg vhigh 'realp))
663 (let ((xvals (list low high))
664 (yvals (list vlow vhigh))
665 (pos (Math-posp vlow))
667 (step (math-sub-float high low))
669 (while (and (<= (setq levels (1+ levels)) 5)
673 step (math-mul-float step '(float 497 -3)))
674 (while (and (cdr xp) (not found))
675 (if (Math-realp (car yp))
678 (setq high (math-add-float (car xp) step)
680 vhigh (math-evaluate-expr expr))
681 (math-working "search" high)
682 (if (and (Math-realp vhigh)
683 (eq (math-negp vhigh) pos))
685 (setcdr xp (cons high (cdr xp)))
686 (setcdr yp (cons vhigh (cdr yp)))
687 (setq xp (cdr (cdr xp))
688 yp (cdr (cdr yp))))))))
690 (if (Math-zerop vhigh)
691 (list 'vec high vhigh)
692 (if (Math-zerop vlow)
695 (math-newton-search-root expr deriv nil nil nil nil
697 (math-bisect-root expr low vlow high vhigh))))
698 (math-reject-arg (list 'intv 3 low high)
699 "*Unable to find a sign change in this interval"))))
701 ;;; "rtbis" (but we should be using Brent's method)
702 (defun math-bisect-root (expr low vlow high vhigh)
703 (let ((step (math-sub-float high low))
704 (pos (Math-posp vhigh))
707 (while (not (or (math-nearly-equal low
708 (setq step (math-mul-float
710 mid (math-add-float low step)))
713 vmid (math-evaluate-expr expr))
715 (math-working "bisect" mid)
716 (if (eq (Math-posp vmid) pos)
721 (list 'vec mid vmid)))
725 (defvar math-root-vars [(var DUMMY var-DUMMY)])
727 (defun math-newton-multi (expr jacob n guess orig-guess limit)
730 p2 expr-val jacob-val next)
731 (while (< (setq p (cdr p) m (1+ m)) n)
732 (set (nth 2 (aref math-root-vars m)) (car p)))
733 (setq expr-val (math-evaluate-expr expr)
734 jacob-val (math-evaluate-expr jacob))
735 (unless (and (math-constp expr-val)
736 (math-constp jacob-val))
737 (math-reject-arg guess "*Newton's method encountered a singularity"))
738 (setq next (math-add guess (math-div (math-float (math-neg expr-val))
739 (math-float jacob-val)))
741 (math-working "newton" next)
742 (while (and (setq p (cdr p) p2 (cdr p2))
743 (math-nearly-equal (car p) (car p2))))
745 (if (Math-lessp (math-abs-approx (math-sub next orig-guess))
747 (math-newton-multi expr jacob n next orig-guess limit)
748 (math-reject-arg nil "*Newton's method failed to converge"))
749 (list 'vec next expr-val))))
752 (defun math-find-root (expr var guess math-root-widen)
753 (if (eq (car-safe expr) 'vec)
754 (let ((n (1- (length expr)))
755 (calc-symbolic-mode nil)
759 (unless (eq (car-safe var) 'vec)
760 (math-reject-arg var 'vectorp))
761 (unless (= (length var) (1+ n))
762 (math-dimension-error))
763 (setq expr (copy-sequence expr))
764 (while (>= n (length math-root-vars))
765 (let ((symb (intern (concat "math-root-v"
767 (length math-root-vars))))))
768 (setq math-root-vars (vconcat math-root-vars
769 (vector (list 'var symb symb))))))
771 (while (< (setq m (1+ m)) n)
772 (set (nth 2 (aref math-root-vars m)) nil))
774 (while (setq m (1+ m) p (cdr p))
775 (or (eq (car-safe (car p)) 'var)
776 (math-reject-arg var "*Expected a variable"))
778 (while (setq p2 (cdr p2))
779 (setcar p2 (math-expr-subst (car p2) (car p)
780 (aref math-root-vars m)))))
781 (unless (eq (car-safe guess) 'vec)
782 (math-reject-arg guess 'vectorp))
783 (unless (= (length guess) (1+ n))
784 (math-dimension-error))
785 (setq guess (copy-sequence guess)
787 (while (setq p (cdr p))
788 (or (Math-numberp (car guess))
789 (math-reject-arg guess 'numberp))
790 (setcar p (math-float (car p))))
792 (while (setq p (cdr p))
793 (if (assq (car-safe (car p)) calc-tweak-eqn-table)
794 (setcar p (math-sub (nth 1 (car p)) (nth 2 (car p)))))
795 (setcar p (math-evaluate-expr (car p)))
796 (setq row (list 'vec)
798 (while (< (setq m (1+ m)) n)
799 (nconc row (list (math-evaluate-expr
800 (or (calcFunc-deriv (car p)
801 (aref math-root-vars m)
805 "*Formulas must be differentiable"))))))
806 (nconc jacob (list row)))
807 (setq m (math-abs-approx guess))
808 (math-newton-multi expr jacob n guess guess
809 (if (math-zerop m) '(float 1 3) (math-mul m 10))))
810 (unless (eq (car-safe var) 'var)
811 (math-reject-arg var "*Expected a variable"))
812 (unless (math-expr-contains expr var)
813 (math-reject-arg expr "*Formula does not contain specified variable"))
814 (if (assq (car expr) calc-tweak-eqn-table)
815 (setq expr (math-sub (nth 1 expr) (nth 2 expr))))
816 (math-with-extra-prec 2
817 (setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY)))
818 (let* ((calc-symbolic-mode nil)
820 (expr (math-evaluate-expr expr))
821 (deriv (calcFunc-deriv expr '(var DUMMY var-DUMMY) nil t))
823 (and deriv (setq deriv (math-evaluate-expr deriv)))
824 (setq guess (math-float guess))
825 (if (and (math-numberp guess)
827 (math-newton-root expr deriv guess guess
828 (if (math-zerop guess) '(float 1 6)
829 (math-mul (math-abs-approx guess) 100)))
830 (if (Math-realp guess)
834 vlow (math-evaluate-expr expr)
836 math-root-widen 'point)
837 (if (eq (car guess) 'intv)
839 (or (math-constp guess) (math-reject-arg guess 'constp))
840 (setq low (nth 2 guess)
842 (if (memq (nth 1 guess) '(0 1))
843 (setq low (calcFunc-incr low 1 high)))
844 (if (memq (nth 1 guess) '(0 2))
845 (setq high (calcFunc-incr high -1 low)))
847 vlow (math-evaluate-expr expr)
849 vhigh (math-evaluate-expr expr)))
850 (if (math-complexp guess)
851 (math-reject-arg "*Complex root finder must have derivative")
852 (math-reject-arg guess 'realp))))
853 (if (Math-zerop vlow)
855 (if (Math-zerop vhigh)
856 (list 'vec high vhigh)
857 (if (and deriv (Math-numberp vlow) (Math-numberp vhigh))
858 (math-newton-search-root expr deriv nil nil nil nil
860 (if (or (and (Math-posp vlow) (Math-posp vhigh))
861 (and (Math-negp vlow) (Math-negp vhigh))
862 (not (Math-numberp vlow))
863 (not (Math-numberp vhigh)))
864 (math-search-root expr deriv low vlow high vhigh)
865 (math-bisect-root expr low vlow high vhigh))))))))))
867 (defun calcFunc-root (expr var guess)
868 (math-find-root expr var guess nil))
870 (defun calcFunc-wroot (expr var guess)
871 (math-find-root expr var guess t))
876 ;;; The following algorithms come from Numerical Recipes, chapter 10.
878 (defvar math-min-vars [(var DUMMY var-DUMMY)])
880 (defun math-min-eval (expr a)
883 (while (setq m (1+ m) a (cdr a))
884 (set (nth 2 (aref math-min-vars m)) (car a))))
886 (setq a (math-evaluate-expr expr))
889 (if (eq (car a) 'float)
891 (math-reject-arg a 'realp))))
893 (defvar math-min-or-max "minimum")
895 ;;; A bracket for a minimum is a < b < c where f(b) < f(a) and f(b) < f(c).
898 (defun math-widen-min (expr a b)
901 incr c va vb vc u vu r q ulim bc ba qr)
902 (or b (setq b (math-mul a '(float 101 -2))))
903 (setq va (math-min-eval expr a)
904 vb (math-min-eval expr b))
905 (if (math-lessp-float va vb)
908 (setq c (math-add-float b (math-mul-float '(float 161803 -5)
909 (math-sub-float b a)))
910 vc (math-min-eval expr c))
911 (while (and (not done) (math-lessp-float vc vb))
912 (math-working "widen" (list 'intv 0 a c))
913 (if (= (setq iters (1- iters)) 0)
914 (math-reject-arg nil (format "*Unable to find a %s near the interval"
916 (setq bc (math-sub-float b c)
917 ba (math-sub-float b a)
918 r (math-mul-float ba (math-sub-float vb vc))
919 q (math-mul-float bc (math-sub-float vb va))
920 qr (math-sub-float q r))
921 (if (math-lessp-float (math-abs qr) '(float 1 -20))
922 (setq qr (if (math-negp qr) '(float -1 -20) '(float 1 -20))))
923 (setq u (math-sub-float
925 (math-div-float (math-sub-float (math-mul-float bc q)
926 (math-mul-float ba r))
927 (math-mul-float '(float 2 0) qr)))
928 ulim (math-add-float b (math-mul-float '(float -1 2) bc))
930 (if (if incr (math-lessp-float b u) (math-lessp-float u b))
931 (if (if incr (math-lessp-float u c) (math-lessp-float c u))
932 (if (math-lessp-float (setq vu (math-min-eval expr u)) vc)
936 (if (math-lessp-float vb vu)
939 (setq u (math-add-float c (math-mul-float '(float -161803 -5)
941 vu (math-min-eval expr u))))
942 (if (if incr (math-lessp-float u ulim) (math-lessp-float ulim u))
943 (if (math-lessp-float (setq vu (math-min-eval expr u)) vc)
946 u (math-add-float c (math-mul-float
948 (math-sub-float b c)))
949 vu (math-min-eval expr u)))
951 vu (math-min-eval expr u))))
952 (setq u (math-add-float c (math-mul-float '(float -161803 -5)
954 vu (math-min-eval expr u)))
958 (if (math-lessp-float a c)
959 (list a va b vb c vc)
960 (list c vc b vb a va))))
962 (defun math-narrow-min (expr a c intv)
963 (let ((xvals (list a c))
964 (yvals (list (math-min-eval expr a)
965 (math-min-eval expr c)))
967 (step (math-sub-float c a))
970 (while (and (<= (setq levels (1+ levels)) 5)
974 step (math-mul-float step '(float 497 -3)))
975 (while (and (cdr xp) (not found))
976 (setq b (math-add-float (car xp) step))
977 (math-working "search" b)
978 (setcdr xp (cons b (cdr xp)))
979 (setcdr yp (cons (math-min-eval expr b) (cdr yp)))
980 (if (and (math-lessp-float (nth 1 yp) (car yp))
981 (math-lessp-float (nth 1 yp) (nth 2 yp)))
985 (if (and (cdr (cdr yp))
986 (math-lessp-float (nth 1 yp) (car yp))
987 (math-lessp-float (nth 1 yp) (nth 2 yp)))
992 (list (car xp) (car yp)
993 (nth 1 xp) (nth 1 yp)
994 (nth 2 xp) (nth 2 yp))
995 (or (if (math-lessp-float (car yvals) (nth 1 yvals))
996 (and (memq (nth 1 intv) '(2 3))
997 (let ((min (car yvals)))
998 (while (and (setq yvals (cdr yvals))
999 (math-lessp-float min (car yvals))))
1001 (list (nth 2 intv) min))))
1002 (and (memq (nth 1 intv) '(1 3))
1003 (setq yvals (nreverse yvals))
1004 (let ((min (car yvals)))
1005 (while (and (setq yvals (cdr yvals))
1006 (math-lessp-float min (car yvals))))
1008 (list (nth 3 intv) min)))))
1009 (math-reject-arg nil (format "*Unable to find a %s in the interval"
1010 math-min-or-max))))))
1013 (defun math-brent-min (expr prec a va x vx b vb)
1014 (let ((iters (+ 20 (* 5 prec)))
1019 (tol (list 'float 1 (- -1 prec)))
1020 (zeps (list 'float 1 (- -5 prec)))
1022 d u vu xm tol1 tol2 etemp p q r xv xw)
1024 (setq xm (math-mul-float '(float 5 -1)
1025 (math-add-float a b))
1026 tol1 (math-add-float
1028 (math-mul-float tol (math-abs x)))
1029 tol2 (math-mul-float tol1 '(float 2 0)))
1030 (math-lessp-float (math-sub-float tol2
1033 (math-sub-float b a)))
1034 (math-abs (math-sub-float x xm))))
1035 (if (= (setq iters (1- iters)) 0)
1036 (math-reject-arg nil (format "*Unable to converge on a %s"
1038 (math-working "brent" x)
1039 (if (math-lessp-float (math-abs e) tol1)
1040 (setq e (if (math-lessp-float x xm)
1041 (math-sub-float b x)
1042 (math-sub-float a x))
1043 d (math-mul-float '(float 381966 -6) e))
1044 (setq xw (math-sub-float x w)
1045 r (math-mul-float xw (math-sub-float vx vv))
1046 xv (math-sub-float x v)
1047 q (math-mul-float xv (math-sub-float vx vw))
1048 p (math-sub-float (math-mul-float xv q)
1049 (math-mul-float xw r))
1050 q (math-mul-float '(float 2 0) (math-sub-float q r)))
1052 (setq p (math-neg-float p))
1053 (setq q (math-neg-float q)))
1056 (if (and (math-lessp-float (math-abs p)
1057 (math-abs (math-mul-float
1059 (math-mul-float q etemp))))
1060 (math-lessp-float (math-mul-float
1061 q (math-sub-float a x)) p)
1062 (math-lessp-float p (math-mul-float
1063 q (math-sub-float b x))))
1065 (setq d (math-div-float p q)
1066 u (math-add-float x d))
1067 (if (or (math-lessp-float (math-sub-float u a) tol2)
1068 (math-lessp-float (math-sub-float b u) tol2))
1069 (setq d (if (math-lessp-float xm x)
1070 (math-neg-float tol1)
1072 (setq e (if (math-lessp-float x xm)
1073 (math-sub-float b x)
1074 (math-sub-float a x))
1075 d (math-mul-float '(float 381966 -6) e))))
1076 (setq u (math-add-float x
1077 (if (math-lessp-float (math-abs d) tol1)
1079 (math-neg-float tol1)
1082 vu (math-min-eval expr u))
1083 (if (math-lessp-float vx vu)
1085 (if (math-lessp-float u x)
1089 (not (math-lessp-float vw vu)))
1094 (not (math-lessp-float vv vu)))
1096 (if (math-lessp-float u x)
1105 (defun math-powell-min (expr n guesses prec)
1106 (let* ((f1dim (math-line-min-func expr n))
1107 (xi (calcFunc-idn 1 n))
1108 (p (cons 'vec (mapcar 'car guesses)))
1110 (ftol (list 'float 1 (- prec)))
1111 (fret (math-min-eval expr p))
1112 fp ptt fptt xit i ibig del diff res)
1118 (while (<= (setq i (1+ i)) n)
1120 res (math-line-min f1dim p
1123 p (let ((calc-internal-prec prec))
1124 (math-normalize (car res)))
1126 diff (math-abs (math-sub-float fptt fret)))
1127 (if (math-lessp-float del diff)
1131 (math-mul-float ftol
1132 (math-add-float (math-abs fp)
1134 (math-mul-float '(float 2 0)
1135 (math-abs (math-sub-float fp
1137 (setq ptt (math-sub (math-mul '(float 2 0) p) pt)
1140 fptt (math-min-eval expr ptt))
1141 (if (and (math-lessp-float fptt fp)
1144 (math-mul-float '(float 2 0)
1147 (math-mul-float '(float 2 0)
1150 (math-sqr-float (math-sub-float
1151 (math-sub-float fp fret) del)))
1153 (math-sqr-float (math-sub-float fp fptt)))))
1155 (setq res (math-line-min f1dim p xit n prec)
1159 (while (<= (setq i (1+ i)) n)
1160 (setcar (nthcdr ibig (nth i xi))
1161 (nth i (nth 1 res)))))))
1162 (list 'vec p fret)))
1164 (defun math-line-min-func (expr n)
1166 (while (< (setq m (1+ m)) n)
1167 (set (nth 2 (aref math-min-vars m))
1170 '(var DUMMY var-DUMMY)
1171 (list 'calcFunc-mrow '(var line-xi line-xi) (1+ m)))
1172 (list 'calcFunc-mrow '(var line-p line-p) (1+ m)))))
1173 (math-evaluate-expr expr)))
1175 (defun math-line-min (f1dim line-p line-xi n prec)
1176 (let* ((var-DUMMY nil)
1177 (expr (math-evaluate-expr f1dim))
1178 (params (math-widen-min expr '(float 0 0) '(float 1 0)))
1179 (res (apply 'math-brent-min expr prec params))
1180 (xi (math-mul (nth 1 res) line-xi)))
1181 (list (math-add line-p xi) xi (nth 2 res))))
1184 (defun math-find-minimum (expr var guess min-widen)
1185 (let* ((calc-symbolic-mode nil)
1188 (isvec (math-vectorp var))
1190 (or (math-vectorp var)
1191 (setq var (list 'vec var)))
1192 (or (math-vectorp guess)
1193 (setq guess (list 'vec guess)))
1194 (or (= (length var) (length guess))
1195 (math-dimension-error))
1196 (while (setq var (cdr var) guess (cdr guess))
1197 (or (eq (car-safe (car var)) 'var)
1198 (math-reject-arg (car var) "*Expected a variable"))
1199 (or (math-expr-contains expr (car var))
1200 (math-reject-arg (car var)
1201 "*Formula does not contain specified variable"))
1202 (while (>= (1+ n) (length math-min-vars))
1203 (let ((symb (intern (concat "math-min-v"
1205 (length math-min-vars))))))
1206 (setq math-min-vars (vconcat math-min-vars
1207 (vector (list 'var symb symb))))))
1208 (set (nth 2 (aref math-min-vars n)) nil)
1209 (set (nth 2 (aref math-min-vars (1+ n))) nil)
1210 (if (math-complexp (car guess))
1211 (setq expr (math-expr-subst expr
1213 (list '+ (aref math-min-vars n)
1215 (aref math-min-vars (1+ n))
1217 guesses (let ((g (math-float (math-complex (car guess)))))
1218 (cons (list (nth 2 g) nil nil)
1219 (cons (list (nth 1 g) nil nil t)
1222 (setq expr (math-expr-subst expr
1224 (aref math-min-vars n))
1225 guesses (cons (if (math-realp (car guess))
1226 (list (math-float (car guess)) nil nil)
1227 (if (and (eq (car-safe (car guess)) 'intv)
1228 (math-constp (car guess)))
1230 (math-add (nth 2 (car guess))
1231 (nth 3 (car guess)))
1233 (math-float (nth 2 (car guess)))
1234 (math-float (nth 3 (car guess)))
1236 (math-reject-arg (car guess) 'realp)))
1239 (setq guesses (nreverse guesses)
1240 expr (math-evaluate-expr expr))
1242 (let* ((params (if (nth 1 (car guesses))
1244 (math-widen-min expr
1245 (nth 1 (car guesses))
1246 (nth 2 (car guesses)))
1247 (math-narrow-min expr
1248 (nth 1 (car guesses))
1249 (nth 2 (car guesses))
1250 (nth 3 (car guesses))))
1251 (math-widen-min expr
1254 (prec calc-internal-prec)
1255 (res (if (cdr (cdr params))
1256 (math-with-extra-prec (+ calc-internal-prec 2)
1257 (apply 'math-brent-min expr prec params))
1258 (cons 'vec params))))
1260 (list 'vec (list 'vec (nth 1 res)) (nth 2 res))
1262 (let* ((prec calc-internal-prec)
1263 (res (math-with-extra-prec (+ calc-internal-prec 2)
1264 (math-powell-min expr n guesses prec)))
1267 (while (setq p (cdr p))
1268 (if (nth 3 (car guesses))
1270 (nconc vec (list (math-normalize
1271 (list 'cplx (car p) (nth 1 p)))))
1273 guesses (cdr guesses)))
1274 (nconc vec (list (car p))))
1275 (setq guesses (cdr guesses)))
1277 (list 'vec vec (nth 2 res))
1278 (list 'vec (nth 1 vec) (nth 2 res)))))))
1280 (defun calcFunc-minimize (expr var guess)
1281 (let ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
1282 (math-min-or-max "minimum"))
1283 (math-find-minimum (math-normalize expr)
1284 (math-normalize var)
1285 (math-normalize guess) nil)))
1287 (defun calcFunc-wminimize (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) t)))
1294 (defun calcFunc-maximize (expr var guess)
1295 (let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
1296 (math-min-or-max "maximum")
1297 (res (math-find-minimum (math-normalize (math-neg expr))
1298 (math-normalize var)
1299 (math-normalize guess) nil)))
1300 (list 'vec (nth 1 res) (math-neg (nth 2 res)))))
1302 (defun calcFunc-wmaximize (expr var guess)
1303 (let* ((calc-internal-prec (max (/ calc-internal-prec 2) 3))
1304 (math-min-or-max "maximum")
1305 (res (math-find-minimum (math-normalize (math-neg expr))
1306 (math-normalize var)
1307 (math-normalize guess) t)))
1308 (list 'vec (nth 1 res) (math-neg (nth 2 res)))))
1313 ;;; The following algorithms come from Numerical Recipes, chapter 3.
1315 (defun calcFunc-polint (data x)
1316 (or (math-matrixp data) (math-reject-arg data 'matrixp))
1317 (or (= (length data) 3)
1318 (math-reject-arg data "*Wrong number of data rows"))
1319 (or (> (length (nth 1 data)) 2)
1320 (math-reject-arg data "*Too few data points"))
1321 (if (and (math-vectorp x) (or (math-constp x) math-expand-formulas))
1322 (cons 'vec (mapcar (function (lambda (x) (calcFunc-polint data x)))
1324 (or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp))
1325 (math-with-extra-prec 2
1326 (cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x
1328 (put 'calcFunc-polint 'math-expandable t)
1331 (defun calcFunc-ratint (data x)
1332 (or (math-matrixp data) (math-reject-arg data 'matrixp))
1333 (or (= (length data) 3)
1334 (math-reject-arg data "*Wrong number of data rows"))
1335 (or (> (length (nth 1 data)) 2)
1336 (math-reject-arg data "*Too few data points"))
1337 (if (and (math-vectorp x) (or (math-constp x) math-expand-formulas))
1338 (cons 'vec (mapcar (function (lambda (x) (calcFunc-ratint data x)))
1340 (or (math-objectp x) math-expand-formulas (math-reject-arg x 'objectp))
1341 (math-with-extra-prec 2
1342 (cons 'vec (math-poly-interp (cdr (nth 1 data)) (cdr (nth 2 data)) x
1343 (cdr (cdr (cdr (nth 1 data)))))))))
1344 (put 'calcFunc-ratint 'math-expandable t)
1347 (defun math-poly-interp (xa ya x ratp)
1348 (let ((n (length xa))
1352 (c (copy-sequence ya))
1353 (d (copy-sequence ya))
1356 y dy (xp xa) xpm cp dp temp)
1357 (while (<= (setq i (1+ i)) n)
1358 (setq xax (cons (math-sub (car xp) x) xax)
1360 temp (math-abs (car xax)))
1361 (if (or (null dif) (math-lessp temp dif))
1364 (setq xax (nreverse xax)
1367 (if (math-zerop dif)
1369 (while (< (setq m (1+ m)) n)
1375 (while (<= (setq i (1+ i)) (- n m))
1377 (let ((t2 (math-div (math-mul (car xp) (car dp)) (car xpm))))
1378 (setq temp (math-div (math-sub (nth 1 cp) (car dp))
1379 (math-sub t2 (nth 1 cp))))
1380 (setcar dp (math-mul (nth 1 cp) temp))
1381 (setcar cp (math-mul t2 temp)))
1382 (if (math-equal (car xp) (car xpm))
1383 (math-reject-arg (cons 'vec xa) "*Duplicate X values"))
1384 (setq temp (math-div (math-sub (nth 1 cp) (car dp))
1385 (math-sub (car xp) (car xpm))))
1386 (setcar dp (math-mul (car xpm) temp))
1387 (setcar cp (math-mul (car xp) temp)))
1392 (if (< (+ ns ns) (- n m))
1393 (setq dy (nth ns c))
1396 (setq y (math-add y dy)))
1401 ;;; The following algorithms come from Numerical Recipes, chapter 4.
1403 (defun calcFunc-ninteg (expr var lo hi)
1404 (setq lo (math-evaluate-expr lo)
1405 hi (math-evaluate-expr hi))
1406 (or (math-numberp lo) (math-infinitep lo) (math-reject-arg lo 'numberp))
1407 (or (math-numberp hi) (math-infinitep hi) (math-reject-arg hi 'numberp))
1408 (if (math-lessp hi lo)
1409 (math-neg (calcFunc-ninteg expr var hi lo))
1410 (setq expr (math-expr-subst expr var '(var DUMMY var-DUMMY)))
1411 (let ((var-DUMMY nil)
1412 (calc-symbolic-mode nil)
1413 (calc-prefer-frac nil)
1415 (setq expr (math-evaluate-expr expr))
1416 (if (equal lo '(neg (var inf var-inf)))
1417 (let ((thi (if (math-lessp hi '(float -2 0))
1419 (setq sum (math-ninteg-romberg
1420 'math-ninteg-midpoint expr
1421 (math-float lo) (math-float thi) 'inf)
1423 (if (equal hi '(var inf var-inf))
1424 (let ((tlo (if (math-lessp '(float 2 0) lo)
1426 (setq sum (math-add sum
1427 (math-ninteg-romberg
1428 'math-ninteg-midpoint expr
1429 (math-float tlo) (math-float hi) 'inf))
1431 (or (math-equal lo hi)
1432 (setq sum (math-add sum
1433 (math-ninteg-romberg
1434 'math-ninteg-midpoint expr
1435 (math-float lo) (math-float hi) nil))))
1439 ;;; Open Romberg method; "qromo" in section 4.4.
1441 ;; The variable math-ninteg-temp is local to math-ninteg-romberg,
1442 ;; but is used by math-ninteg-midpoint, which is used by
1443 ;; math-ninteg-romberg.
1444 (defvar math-ninteg-temp)
1446 (defun math-ninteg-romberg (func expr lo hi mode)
1447 (let ((curh '(float 1 0))
1452 (prec calc-internal-prec)
1453 (math-ninteg-temp nil))
1454 (math-with-extra-prec 2
1455 ;; Limit on "j" loop must be 14 or less to keep "it" from overflowing.
1456 (or (while (and (null ss) (<= (setq j (1+ j)) 8))
1457 (setq s (nconc s (list (funcall func expr lo hi mode)))
1458 h (nconc h (list curh)))
1460 (let ((res (math-poly-interp h s '(float 0 0) nil)))
1461 (if (math-lessp (math-abs (nth 1 res))
1462 (calcFunc-scf (math-abs (car res))
1464 (setq ss (car res)))))
1468 (setq curh (math-div-float curh '(float 9 0))))
1470 (math-reject-arg nil (format "*Integral failed to converge"))))))
1473 (defun math-ninteg-evaluate (expr x mode)
1475 (setq x (math-div '(float 1 0) x)))
1476 (let* ((var-DUMMY x)
1477 (res (math-evaluate-expr expr)))
1478 (or (Math-numberp res)
1479 (math-reject-arg res "*Integrand does not evaluate to a number"))
1481 (setq res (math-mul res (math-sqr x))))
1484 (defvar math-infinite-mode)
1486 (defun math-ninteg-midpoint (expr lo hi mode) ; uses "math-ninteg-temp"
1488 (let ((math-infinite-mode t) temp)
1489 (setq temp (math-div 1 lo)
1492 (if math-ninteg-temp
1493 (let* ((it3 (* 3 (car math-ninteg-temp)))
1494 (math-working-step-2 (* 2 (car math-ninteg-temp)))
1495 (math-working-step 0)
1496 (range (math-sub hi lo))
1497 (del (math-div range (math-float it3)))
1498 (del2 (math-add del del))
1499 (del3 (math-add del del2))
1500 (x (math-add lo (math-mul '(float 5 -1) del)))
1503 (while (<= (setq j (1+ j)) (car math-ninteg-temp))
1504 (setq math-working-step (1+ math-working-step)
1505 temp (math-ninteg-evaluate expr x mode)
1506 math-working-step (1+ math-working-step)
1507 sum (math-add sum (math-add temp (math-ninteg-evaluate
1508 expr (math-add x del2)
1510 x (math-add x del3)))
1511 (setq math-ninteg-temp (list it3
1512 (math-add (math-div (nth 1 math-ninteg-temp)
1514 (math-mul sum del)))))
1515 (setq math-ninteg-temp (list 1 (math-mul
1517 (math-ninteg-evaluate
1519 (math-mul (math-add lo hi) '(float 5 -1))
1521 (nth 1 math-ninteg-temp))
1527 ;;; The following algorithms come from Numerical Recipes, chapter 14.
1529 (defvar math-dummy-vars [(var DUMMY var-DUMMY)])
1530 (defvar math-dummy-counter 0)
1531 (defun math-dummy-variable ()
1532 (if (= math-dummy-counter (length math-dummy-vars))
1533 (let ((symb (intern (format "math-dummy-%d" math-dummy-counter))))
1534 (setq math-dummy-vars (vconcat math-dummy-vars
1535 (vector (list 'var symb symb))))))
1536 (set (nth 2 (aref math-dummy-vars math-dummy-counter)) nil)
1538 (aref math-dummy-vars math-dummy-counter)
1539 (setq math-dummy-counter (1+ math-dummy-counter))))
1541 (defvar math-in-fit 0)
1542 (defvar calc-fit-to-trail nil)
1544 (defun calcFunc-fit (expr vars &optional coefs data)
1545 (let ((math-in-fit 10))
1546 (math-with-extra-prec 2
1547 (math-general-fit expr vars coefs data nil))))
1549 (defun calcFunc-efit (expr vars &optional coefs data)
1550 (let ((math-in-fit 10))
1551 (math-with-extra-prec 2
1552 (math-general-fit expr vars coefs data 'sdev))))
1554 (defun calcFunc-xfit (expr vars &optional coefs data)
1555 (let ((math-in-fit 10))
1556 (math-with-extra-prec 2
1557 (math-general-fit expr vars coefs data 'full))))
1559 ;; The variables math-fit-first-var, math-fit-first-coef and
1560 ;; math-fit-new-coefs are local to math-general-fit, but are used by
1561 ;; calcFunc-fitvar, calcFunc-fitparam and calcFunc-fitdummy
1562 ;; (respectively), which are used by math-general-fit.
1563 (defvar math-fit-first-var)
1564 (defvar math-fit-first-coef)
1565 (defvar math-fit-new-coefs)
1567 (defun math-general-fit (expr vars coefs data mode)
1568 (let ((calc-simplify-mode nil)
1569 (math-dummy-counter math-dummy-counter)
1571 (extended (eq mode 'full))
1572 (math-fit-first-coef math-dummy-counter)
1576 have-sdevs need-chisq chisq
1587 ;; Validate and parse arguments.
1592 (if (math-vectorp expr)
1593 (if (memq (length expr) '(3 4))
1598 (math-dimension-error))
1602 (or (math-matrixp data) (math-reject-arg data 'matrixp))
1603 (setq v (1- (length data))
1604 n (1- (length (nth 1 data))))
1605 (or (math-vectorp vars) (null vars)
1606 (setq vars (list 'vec vars)))
1607 (or (math-vectorp coefs) (null coefs)
1608 (setq coefs (list 'vec coefs)))
1610 (setq coefs (cons 'vec (math-all-vars-but expr vars))))
1612 (if (<= (1- (length coefs)) v)
1613 (math-reject-arg coefs "*Not enough variables in model")
1614 (setq coefs (copy-sequence coefs))
1615 (let ((p (nthcdr (- (length coefs) v
1616 (if (eq (car-safe expr) 'calcFunc-eq) 1 0))
1618 (setq vars (cons 'vec (cdr p)))
1620 (or (= (1- (length vars)) v)
1622 (math-reject-arg vars "*Number of variables does not match data"))
1623 (setq m (1- (length coefs)))
1625 (math-reject-arg coefs "*Need at least one parameter"))
1627 ;; Rewrite expr in terms of fitparam and fitvar, make into an equation.
1629 (while (setq p (cdr p))
1630 (or (eq (car-safe (car p)) 'var)
1631 (math-reject-arg (car p) "*Expected a variable"))
1632 (setq dummy (math-dummy-variable)
1633 expr (math-expr-subst expr (car p)
1634 (list 'calcFunc-fitparam
1635 (- math-dummy-counter math-fit-first-coef)))))
1636 (setq math-fit-first-var math-dummy-counter
1638 (while (setq p (cdr p))
1639 (or (eq (car-safe (car p)) 'var)
1640 (math-reject-arg (car p) "*Expected a variable"))
1641 (setq dummy (math-dummy-variable)
1642 expr (math-expr-subst expr (car p)
1643 (list 'calcFunc-fitvar
1644 (- math-dummy-counter math-fit-first-var)))))
1645 (if (< math-dummy-counter (+ math-fit-first-var v))
1646 (setq dummy (math-dummy-variable))) ; dependent variable may be unnamed
1649 (or (eq (car-safe expr) 'calcFunc-eq)
1650 (setq expr (list 'calcFunc-eq (list 'calcFunc-fitvar v) expr)))
1652 (let ((calc-symbolic-mode nil))
1654 ;; Apply rewrites to put expr into a linear-like form.
1655 (setq expr (math-evaluate-expr expr)
1656 expr (math-rewrite (list 'calcFunc-fitmodel expr)
1657 '(var FitRules var-FitRules))
1659 expr (math-evaluate-expr expr))
1660 (or (and (eq (car-safe expr) 'calcFunc-fitsystem)
1662 (math-vectorp (nth 2 expr))
1663 (math-vectorp (nth 3 expr))
1664 (> (length (nth 2 expr)) 1)
1665 (= (length (nth 3 expr)) (1+ m)))
1666 (math-reject-arg plain-expr "*Model expression is too complex"))
1667 (setq y-filter (nth 1 expr)
1668 x-funcs (vconcat (cdr (nth 2 expr)))
1669 coef-filters (nth 3 expr)
1670 mm (length x-funcs))
1671 (if (equal y-filter y-dummy)
1672 (setq y-filter nil))
1674 ;; Build the (square) system of linear equations to be solved.
1675 (setq beta (cons 'vec (make-list mm 0))
1676 covar (cons 'vec (mapcar 'copy-sequence (make-list mm beta))))
1677 (let* ((ptrs (vconcat (cdr data)))
1679 (xvals (make-vector mm 0))
1681 j k xval yval sigmasqr wt covj covjk covk betaj) ; lud)
1682 (while (<= (setq i (1+ i)) n)
1684 ;; Assign various independent variables for this data point.
1688 (aset ptrs j (cdr (aref ptrs j)))
1689 (setq xval (car (aref ptrs j)))
1693 (if (eq (car-safe xval) 'sdev)
1694 (setq sigmasqr (math-add (math-sqr (nth 2 xval))
1698 (setq xval (math-make-sdev xval
1699 (math-sqrt sigmasqr))))))
1700 (if (eq (car-safe xval) 'sdev)
1701 (setq sigmasqr (math-add (math-sqr (nth 2 xval))
1703 xval (nth 1 xval))))
1704 (set (nth 2 (aref math-dummy-vars (+ math-fit-first-var j))) xval)
1707 ;; Compute Y value for this data point.
1709 (setq yval (math-evaluate-expr y-filter))
1710 (setq yval (symbol-value (nth 2 y-dummy))))
1711 (if (eq (car-safe yval) 'sdev)
1712 (setq sigmasqr (math-sqr (nth 2 yval))
1715 (setq have-sdevs sigmasqr
1716 need-chisq (or extended
1717 (and (eq mode 'sdev) (not have-sdevs)))))
1721 (setq isigsq (math-div 1 sigmasqr))
1723 (setq weights (cons isigsq weights))))
1724 (math-reject-arg yval "*Mixed error forms and plain numbers"))
1726 (math-reject-arg yval "*Mixed error forms and plain numbers")))
1728 ;; Compute X values for this data point and update covar and beta.
1729 (if (eq (car-safe xval) 'sdev)
1730 (set (nth 2 y-dummy) (nth 1 xval)))
1735 (setq wt (math-evaluate-expr (aref x-funcs j)))
1737 (setq wt (math-mul wt isigsq)
1739 covjk (car (setq covj (cdr covj)))
1742 (setq covjk (cdr covjk))
1743 (setcar covjk (math-add (car covjk)
1744 (math-mul wt (aref xvals k))))
1746 (setcar betaj (math-add (car betaj) (math-mul wt yval)))
1749 (setq xy-values (cons (append xvals (list yval)) xy-values))))
1751 ;; Fill in symmetric half of covar matrix.
1754 (while (< j (1- mm))
1757 covjk (nthcdr j (car (setq covj (cdr covj))))
1758 covk (nthcdr j covar))
1759 (while (< (setq k (1+ k)) mm)
1760 (setq covjk (cdr covjk)
1762 (setcar covjk (nth j (car covk))))))
1764 ;; Solve the linear system.
1767 (setq covar (math-matrix-inv-raw covar))
1769 (setq beta (math-mul covar beta))
1770 (if (math-zerop (math-abs beta))
1771 (setq covar (calcFunc-diag 0 (1- (length beta))))
1772 (math-reject-arg orig-expr "*Singular matrix")))
1773 (or (math-vectorp covar)
1774 (setq covar (list 'vec (list 'vec covar)))))
1775 (setq beta (math-div beta covar)))
1777 ;; Compute chi-square statistic if necessary.
1785 (while (setq bp (cdr bp))
1786 (setq sum (math-add sum (math-mul (car bp) (car xp)))
1788 (setq sum (math-sqr (math-sub (car xp) sum)))
1789 (if weights (setq sum (math-mul sum (car weights))))
1790 (setq chisq (math-add chisq sum)
1791 weights (cdr weights)
1792 xy-values (cdr xy-values)))))
1794 ;; Convert coefficients back into original terms.
1795 (setq math-fit-new-coefs (copy-sequence beta))
1796 (let* ((bp math-fit-new-coefs)
1801 (and mode (not have-sdevs)
1802 (setq sigdat (if (<= n mm)
1804 (math-div chisq (- n mm)))))
1806 (while (setq bp (cdr bp))
1807 (setcar bp (math-make-sdev
1809 (math-sqrt (math-mul (nth (setq j (1+ j))
1810 (car (setq cp (cdr cp))))
1812 (setq math-fit-new-coefs (math-evaluate-expr coef-filters))
1813 (if calc-fit-to-trail
1814 (let ((bp math-fit-new-coefs)
1817 (while (setq bp (cdr bp) cp (cdr cp))
1818 (setq vec (cons (list 'calcFunc-eq (car cp) (car bp)) vec)))
1819 (setq calc-fit-to-trail (cons 'vec (nreverse vec)))))))
1821 ;; Substitute best-fit coefficients back into original formula.
1822 (setq expr (math-multi-subst
1827 (setq vec (cons (list 'calcFunc-fitvar n) vec)
1831 (setq vec (cons (list 'calcFunc-fitparam n) vec)
1834 (append (cdr math-fit-new-coefs) (cdr vars))))
1836 ;; Package the result.
1839 (list 'vec expr beta covar
1840 (let ((p coef-filters)
1842 (while (and (setq n (1+ n) p (cdr p))
1843 (eq (car-safe (car p)) 'calcFunc-fitdummy)
1844 (eq (nth 1 (car p)) n)))
1849 (if (and have-sdevs (> n mm))
1850 (list 'calcFunc-utpc chisq (- n mm))
1851 '(var nan var-nan)))
1855 (defun calcFunc-fitvar (x)
1856 (if (>= math-in-fit 2)
1858 (setq x (aref math-dummy-vars (+ math-fit-first-var x -1)))
1859 (or (calc-var-value (nth 2 x)) x))
1860 (math-reject-arg x)))
1862 (defun calcFunc-fitparam (x)
1863 (if (>= math-in-fit 2)
1865 (setq x (aref math-dummy-vars (+ math-fit-first-coef x -1)))
1866 (or (calc-var-value (nth 2 x)) x))
1867 (math-reject-arg x)))
1869 (defun calcFunc-fitdummy (x)
1870 (if (= math-in-fit 3)
1871 (nth x math-fit-new-coefs)
1872 (math-reject-arg x)))
1874 (defun calcFunc-hasfitvars (expr)
1875 (if (Math-primp expr)
1877 (if (eq (car expr) 'calcFunc-fitvar)
1879 (apply 'max (mapcar 'calcFunc-hasfitvars (cdr expr))))))
1881 (defun calcFunc-hasfitparams (expr)
1882 (if (Math-primp expr)
1884 (if (eq (car expr) 'calcFunc-fitparam)
1886 (apply 'max (mapcar 'calcFunc-hasfitparams (cdr expr))))))
1889 (defun math-all-vars-but (expr but)
1890 (let* ((vars (math-all-vars-in expr))
1893 (setq vars (delq (assoc (car-safe p) vars) vars)
1895 (sort (mapcar 'car vars)
1896 (function (lambda (x y) (string< (nth 1 x) (nth 1 y)))))))
1898 ;; The variables math-all-vars-vars (the vars for math-all-vars) and
1899 ;; math-all-vars-found are local to math-all-vars-in, but are used by
1900 ;; math-all-vars-rec which is called by math-all-vars-in.
1901 (defvar math-all-vars-vars)
1902 (defvar math-all-vars-found)
1904 (defun math-all-vars-in (expr)
1905 (let ((math-all-vars-vars nil)
1906 math-all-vars-found)
1907 (math-all-vars-rec expr)
1908 math-all-vars-vars))
1910 (defun math-all-vars-rec (expr)
1911 (if (Math-primp expr)
1912 (if (eq (car-safe expr) 'var)
1913 (or (math-const-var expr)
1914 (if (setq math-all-vars-found (assoc expr math-all-vars-vars))
1915 (setcdr math-all-vars-found (1+ (cdr math-all-vars-found)))
1916 (setq math-all-vars-vars (cons (cons expr 1) math-all-vars-vars)))))
1917 (while (setq expr (cdr expr))
1918 (math-all-vars-rec (car expr)))))
1922 ;;; calcalg3.el ends here