comparison lisp/calc/calc.el @ 41455:ede347046135

(Commentary): Cleanup, and add logistic curve fitting suggestion from Robert J. Chassell <bob@rattlesnake.com>.
author Colin Walters <walters@gnu.org>
date Sat, 24 Nov 2001 22:33:47 +0000
parents 1c09d8ddd155
children 7308bbc423d5
comparison
equal deleted inserted replaced
41454:b67d654516ad 41455:ede347046135
24 ;; file named COPYING. Among other things, the copyright notice 24 ;; file named COPYING. Among other things, the copyright notice
25 ;; and this notice must be preserved on all copies. 25 ;; and this notice must be preserved on all copies.
26 26
27 ;;; Commentary: 27 ;;; Commentary:
28 28
29 ;;; Calc is split into many files. This file is the main entry point. 29 ;; Calc is split into many files. This file is the main entry point.
30 ;;; This file includes autoload commands for various other basic Calc 30 ;; This file includes autoload commands for various other basic Calc
31 ;;; facilities. The more advanced features are based in calc-ext, which 31 ;; facilities. The more advanced features are based in calc-ext, which
32 ;;; in turn contains autoloads for the rest of the Calc files. This 32 ;; in turn contains autoloads for the rest of the Calc files. This
33 ;;; odd set of interactions is designed to make Calc's loading time 33 ;; odd set of interactions is designed to make Calc's loading time
34 ;;; be as short as possible when only simple calculations are needed. 34 ;; be as short as possible when only simple calculations are needed.
35 35
36 ;;; Suggested usage: 36 ;; Original author's address:
37 ;;; 37 ;; Dave Gillespie, daveg@synaptics.com, uunet!synaptx!daveg.
38 ;;; (autoload 'calc-dispatch "calc" "Emacs Calculator" t nil) 38 ;; Synaptics, Inc., 2698 Orchard Parkway, San Jose, CA 95134.
39 ;;; (global-set-key "\e#" 'calc-dispatch) 39 ;;
40 ;;; Type `M-# M-#' to start. 40 ;; The old address daveg@csvax.cs.caltech.edu will continue to
41 ;;; 41 ;; work for the foreseeable future.
42 ;;; The Calc home directory must be added to the Emacs load-path: 42 ;;
43 ;;; 43 ;; Bug reports and suggestions are always welcome! (Type M-x
44 ;;; (setq load-path (cons "/x/y/z/calc" load-path)) 44 ;; report-calc-bug to send them).
45 ;;; 45
46 ;;; where "/x/y/z/calc" represents the full name of the Calc home directory. 46 ;; All functions, macros, and Lisp variables defined here begin with one
47 ;;; 47 ;; of the prefixes "math", "Math", or "calc", with the exceptions of
48 ;;; See the file INSTALL for a complete list of recommeded autoload 48 ;; "full-calc", "full-calc-keypad", "another-calc", "quick-calc",
49 ;;; commands (though only calc-dispatch is absolutely necessary). 49 ;; "report-calc-bug", and "defmath". User-accessible variables begin
50 50 ;; with "var-".
51 51
52 ;;; Author's address: 52 ;;; TODO:
53 ;;; Dave Gillespie, daveg@synaptics.com, uunet!synaptx!daveg. 53
54 ;;; Synaptics, Inc., 2698 Orchard Parkway, San Jose, CA 95134. 54 ;; Fix rewrite mechanism to do less gratuitous rearrangement of terms.
55 ;;; 55 ;; Implement a pattern-based "refers" predicate.
56 ;;; The old address daveg@csvax.cs.caltech.edu will continue to 56 ;;
57 ;;; work for the foreseeable future. 57 ;; Make it possible to Undo a selection command.
58 ;;; 58 ;; Figure out how to allow selecting rows of matrices.
59 ;;; The latest version of Calc is always available from anonymous FTP 59 ;; If cursor was in selection before, move it after j n, j p, j L, etc.
60 ;;; on csvax.cs.caltech.edu [131.215.131.131]; look in ~ftp/pub/calc*. 60 ;; Consider reimplementing calc-delete-selection using rewrites.
61 ;;; It should also be available on prep.ai.mit.edu. 61 ;;
62 ;;; 62 ;; Implement line-breaking in non-flat compositions (is this desirable?).
63 ;;; Bug reports and suggestions are always welcome! 63 ;; Implement matrix formatting with multi-line components.
64 64 ;;
65 65 ;; Have "Z R" define a user command based on a set of rewrite rules.
66 ;;; All functions, macros, and Lisp variables defined here begin with one 66 ;; Support "incf" and "decf" in defmath definitions.
67 ;;; of the prefixes "math", "Math", or "calc", with the exceptions of 67 ;; Have defmath generate calls to calc-binary-op or calc-unary-op.
68 ;;; "full-calc", "full-calc-keypad", "another-calc", "quick-calc", 68 ;; Make some way to define algebraic functions using keyboard macros.
69 ;;; "report-calc-bug", and "defmath". User-accessible variables begin 69 ;;
70 ;;; with "var-". 70 ;; Allow calc-word-size=0 => Common Lisp-style signed bitwise arithmetic.
71 ;; Consider digamma function (and thus arb. prec. Euler's gamma constant).
72 ;; May as well make continued-fractions stuff available to the user.
73 ;;
74 ;; How about matrix eigenvalues, SVD, pseudo-inverse, etc.?
75 ;; Should cache matrix inverses as well as decompositions.
76 ;; If dividing by a non-square matrix, use least-squares automatically.
77 ;; Consider supporting matrix exponentials.
78 ;;
79 ;; Have ninteg detect and work around singularities at the endpoints.
80 ;; Use an adaptive subdivision algorithm for ninteg.
81 ;; Provide nsum and nprod to go along with ninteg.
82 ;;
83 ;; Handle TeX-mode parsing of \matrix{ ... } where ... contains braces.
84 ;; Support AmS-TeX's \{d,t,}frac, \{d,t,}binom notations.
85 ;; Format and parse sums and products in Eqn and Math modes.
86 ;;
87 ;; Get math-read-big-expr to read sums, products, etc.
88 ;; Change calc-grab-region to use math-read-big-expr.
89 ;; Have a way to define functions using := in Embedded Mode.
90 ;;
91 ;; Support polar plotting with GNUPLOT.
92 ;; Make a calc-graph-histogram function.
93 ;;
94 ;; Replace hokey formulas for complex functions with formulas designed
95 ;; to minimize roundoff while maintaining the proper branch cuts.
96 ;; Test accuracy of advanced math functions over whole complex plane.
97 ;; Extend Bessel functions to provide arbitrary precision.
98 ;; Extend advanced math functions to handle error forms and intervals.
99 ;; Provide a better implementation for math-sin-cos-raw.
100 ;; Provide a better implementation for math-hypot.
101 ;; Provide a better implementation for math-make-frac.
102 ;; Provide a better implementation for calcFunc-prfac.
103 ;; Provide a better implementation for calcFunc-factor.
104 ;;
105 ;; Provide more examples in the tutorial section of the manual.
106 ;; Cover in the tutorial: simplification modes, declarations,
107 ;; bitwise stuff, selections, matrix mapping, financial functions.
108 ;; Provide more Lisp programming examples in the manual.
109 ;; Finish the Internals section of the manual (and bring it up to date).
110 ;;
111 ;; Tim suggests adding spreadsheet-like features.
112 ;; Implement language modes for Gnuplot, Lisp, Ada, APL, ...?
113 ;;
114 ;; For atan series, if x > tan(pi/12) (about 0.268) reduce using the identity
115 ;; atan(x) = atan((x * sqrt(3) - 1) / (sqrt(3) + x)) + pi/6.
116 ;;
117 ;; A better integration algorithm:
118 ;; Use breadth-first instead of depth-first search, as follows:
119 ;; The integral cache allows unfinished integrals in symbolic notation
120 ;; on the righthand side. An entry with no unfinished integrals on the
121 ;; RHS is "complete"; references to it elsewhere are replaced by the
122 ;; integrated value. More than one cache entry for the same integral
123 ;; may exist, though if one becomes complete, the others may be deleted.
124 ;; The integrator works by using every applicable rule (such as
125 ;; substitution, parts, linearity, etc.) to generate possible righthand
126 ;; sides, all of which are entered into the cache. Now, as long as the
127 ;; target integral is not complete (and the time limit has not run out)
128 ;; choose an incomplete integral from the cache and, for every integral
129 ;; appearing in its RHS's, add those integrals to the cache using the
130 ;; same substitition, parts, etc. rules. The cache should be organized
131 ;; as a priority queue, choosing the "simplest" incomplete integral at
132 ;; each step, or choosing randomly among equally simple integrals.
133 ;; Simplicity equals small size, and few steps removed from the original
134 ;; target integral. Note that when the integrator finishes, incomplete
135 ;; integrals can be left in the cache, so the algorithm can start where
136 ;; it left off if another similar integral is later requested.
137 ;; Breadth-first search would avoid the nagging problem of, e.g., whether
138 ;; to use parts or substitution first, and which decomposition is best.
139 ;; All are tried, and any path that diverges will quickly be put on the
140 ;; back burner by the priority queue.
141 ;; Note: Probably a good idea to call math-simplify-extended before
142 ;; measuring a formula's simplicity.
143
144 ;; From: "Robert J. Chassell" <bob@rattlesnake.com>
145 ;; Subject: Re: fix for `Cannot open load file: calc-alg-3'
146 ;; To: walters@debian.org
147 ;; Date: Sat, 24 Nov 2001 21:44:21 +0000 (UTC)
148 ;;
149 ;; Could you add logistic curve fitting to the current list?
150 ;;
151 ;; (I guess the key binding for a logistic curve would have to be `s'
152 ;; since a logistic curve is an `s' curve; both `l' and `L' are already
153 ;; taken for logarithms.)
154 ;;
155 ;; Here is the current list for curve fitting;
156 ;;
157 ;; `1'
158 ;; Linear or multilinear. a + b x + c y + d z.
159 ;;
160 ;; `2-9'
161 ;; Polynomials. a + b x + c x^2 + d x^3.
162 ;;
163 ;; `e'
164 ;; Exponential. a exp(b x) exp(c y).
165 ;;
166 ;; `E'
167 ;; Base-10 exponential. a 10^(b x) 10^(c y).
168 ;;
169 ;; `x'
170 ;; Exponential (alternate notation). exp(a + b x + c y).
171 ;;
172 ;; `X'
173 ;; Base-10 exponential (alternate). 10^(a + b x + c y).
174 ;;
175 ;; `l'
176 ;; Logarithmic. a + b ln(x) + c ln(y).
177 ;;
178 ;; `L'
179 ;; Base-10 logarithmic. a + b log10(x) + c log10(y).
180 ;;
181 ;; `^'
182 ;; General exponential. a b^x c^y.
183 ;;
184 ;; `p'
185 ;; Power law. a x^b y^c.
186 ;;
187 ;; `q'
188 ;; Quadratic. a + b (x-c)^2 + d (x-e)^2.
189 ;;
190 ;; `g'
191 ;; Gaussian. (a / b sqrt(2 pi)) exp(-0.5*((x-c)/b)^2).
192 ;;
193 ;;
194 ;; Logistic curves are used a great deal in ecology, and in predicting
195 ;; human actions, such as use of different kinds of energy in a country
196 ;; (wood, coal, oil, natural gas, etc.) or the number of scientific
197 ;; papers a person publishes, or the number of movies made.
198 ;;
199 ;; (The less information on which to base the curve, the higher the error
200 ;; rate. Theodore Modis ran some Monte Carlo simulations and produced
201 ;; what may be useful set of confidence levels for different amounts of
202 ;; initial information.)
71 203
72 ;;; Code: 204 ;;; Code:
73
74 205
75 (provide 'calc) 206 (provide 'calc)
76 (require 'calc-macs) 207 (require 'calc-macs)
77 208
78 ;;; The "###autoload" comment will be used by Emacs version 19 for 209 ;;; The "###autoload" comment will be used by Emacs version 19 for
110 241
111 ;; If T, scan keymaps to find all DEL-like keys. 242 ;; If T, scan keymaps to find all DEL-like keys.
112 ;; If NIL, only DEL itself is mapped to calc-pop. 243 ;; If NIL, only DEL itself is mapped to calc-pop.
113 (defvar calc-scan-for-dels t) 244 (defvar calc-scan-for-dels t)
114 245
115
116
117 (defvar calc-extensions-loaded nil) 246 (defvar calc-extensions-loaded nil)
118
119
120 ;;; IDEAS:
121 ;;;
122 ;;; Fix rewrite mechanism to do less gratuitous rearrangement of terms.
123 ;;; Implement a pattern-based "refers" predicate.
124 ;;;
125 ;;; Make it possible to Undo a selection command.
126 ;;; Figure out how to allow selecting rows of matrices.
127 ;;; If cursor was in selection before, move it after j n, j p, j L, etc.
128 ;;; Consider reimplementing calc-delete-selection using rewrites.
129 ;;;
130 ;;; Implement line-breaking in non-flat compositions (is this desirable?).
131 ;;; Implement matrix formatting with multi-line components.
132 ;;;
133 ;;; Have "Z R" define a user command based on a set of rewrite rules.
134 ;;; Support "incf" and "decf" in defmath definitions.
135 ;;; Have defmath generate calls to calc-binary-op or calc-unary-op.
136 ;;; Make some way to define algebraic functions using keyboard macros.
137 ;;;
138 ;;; Allow calc-word-size=0 => Common Lisp-style signed bitwise arithmetic.
139 ;;; Consider digamma function (and thus arb. prec. Euler's gamma constant).
140 ;;; May as well make continued-fractions stuff available to the user.
141 ;;;
142 ;;; How about matrix eigenvalues, SVD, pseudo-inverse, etc.?
143 ;;; Should cache matrix inverses as well as decompositions.
144 ;;; If dividing by a non-square matrix, use least-squares automatically.
145 ;;; Consider supporting matrix exponentials.
146 ;;;
147 ;;; Have ninteg detect and work around singularities at the endpoints.
148 ;;; Use an adaptive subdivision algorithm for ninteg.
149 ;;; Provide nsum and nprod to go along with ninteg.
150 ;;;
151 ;;; Handle TeX-mode parsing of \matrix{ ... } where ... contains braces.
152 ;;; Support AmS-TeX's \{d,t,}frac, \{d,t,}binom notations.
153 ;;; Format and parse sums and products in Eqn and Math modes.
154 ;;;
155 ;;; Get math-read-big-expr to read sums, products, etc.
156 ;;; Change calc-grab-region to use math-read-big-expr.
157 ;;; Have a way to define functions using := in Embedded Mode.
158 ;;;
159 ;;; Support polar plotting with GNUPLOT.
160 ;;; Make a calc-graph-histogram function.
161 ;;;
162 ;;; Replace hokey formulas for complex functions with formulas designed
163 ;;; to minimize roundoff while maintaining the proper branch cuts.
164 ;;; Test accuracy of advanced math functions over whole complex plane.
165 ;;; Extend Bessel functions to provide arbitrary precision.
166 ;;; Extend advanced math functions to handle error forms and intervals.
167 ;;; Provide a better implementation for math-sin-cos-raw.
168 ;;; Provide a better implementation for math-hypot.
169 ;;; Provide a better implementation for math-make-frac.
170 ;;; Provide a better implementation for calcFunc-prfac.
171 ;;; Provide a better implementation for calcFunc-factor.
172 ;;;
173 ;;; Provide more examples in the tutorial section of the manual.
174 ;;; Cover in the tutorial: simplification modes, declarations,
175 ;;; bitwise stuff, selections, matrix mapping, financial functions.
176 ;;; Provide more Lisp programming examples in the manual.
177 ;;; Finish the Internals section of the manual (and bring it up to date).
178 ;;;
179 ;;; Tim suggests adding spreadsheet-like features.
180 ;;; Implement language modes for Gnuplot, Lisp, Ada, APL, ...?
181 ;;;
182
183
184 ;;; For atan series, if x > tan(pi/12) (about 0.268) reduce using the identity
185 ;;; atan(x) = atan((x * sqrt(3) - 1) / (sqrt(3) + x)) + pi/6.
186
187
188 ;;; A better integration algorithm:
189 ;;; Use breadth-first instead of depth-first search, as follows:
190 ;;; The integral cache allows unfinished integrals in symbolic notation
191 ;;; on the righthand side. An entry with no unfinished integrals on the
192 ;;; RHS is "complete"; references to it elsewhere are replaced by the
193 ;;; integrated value. More than one cache entry for the same integral
194 ;;; may exist, though if one becomes complete, the others may be deleted.
195 ;;; The integrator works by using every applicable rule (such as
196 ;;; substitution, parts, linearity, etc.) to generate possible righthand
197 ;;; sides, all of which are entered into the cache. Now, as long as the
198 ;;; target integral is not complete (and the time limit has not run out)
199 ;;; choose an incomplete integral from the cache and, for every integral
200 ;;; appearing in its RHS's, add those integrals to the cache using the
201 ;;; same substitition, parts, etc. rules. The cache should be organized
202 ;;; as a priority queue, choosing the "simplest" incomplete integral at
203 ;;; each step, or choosing randomly among equally simple integrals.
204 ;;; Simplicity equals small size, and few steps removed from the original
205 ;;; target integral. Note that when the integrator finishes, incomplete
206 ;;; integrals can be left in the cache, so the algorithm can start where
207 ;;; it left off if another similar integral is later requested.
208 ;;; Breadth-first search would avoid the nagging problem of, e.g., whether
209 ;;; to use parts or substitution first, and which decomposition is best.
210 ;;; All are tried, and any path that diverges will quickly be put on the
211 ;;; back burner by the priority queue.
212 ;;; Note: Probably a good idea to call math-simplify-extended before
213 ;;; measuring a formula's simplicity.
214
215
216
217
218
219 247
220 ;; Calculator stack. 248 ;; Calculator stack.
221 ;; Entries are 3-lists: Formula, Height (in lines), Selection (or nil). 249 ;; Entries are 3-lists: Formula, Height (in lines), Selection (or nil).
222 (defvar calc-stack '((top-of-stack 1 nil))) 250 (defvar calc-stack '((top-of-stack 1 nil)))
223 251