annotate lisp/calc/calcalg3.el @ 95967:13aa0133c5af

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