Mercurial > emacs
annotate lisp/calc/calc-stuff.el @ 59169:5ba3e8b1f040
(site-run-file): Don't allow setting it with Custom.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Tue, 28 Dec 2004 15:30:39 +0000 |
parents | 0efdac1406ea |
children | a27ed02e5a65 f2ebccfa87d4 |
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 ;;; calc-stuff.el --- miscellaneous 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 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 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
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
58546
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
6 ;; Maintainer: Jay Belanger <belanger@truman.edu> |
40785 | 7 |
8 ;; This file is part of GNU Emacs. | |
9 | |
10 ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 ;; accepts responsibility to anyone for the consequences of using it | |
13 ;; or for whether it serves any particular purpose or works at all, | |
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 ;; License for full details. | |
16 | |
17 ;; Everyone is granted permission to copy, modify and redistribute | |
18 ;; GNU Emacs, but only under the conditions described in the | |
19 ;; GNU Emacs General Public License. A copy of this license is | |
20 ;; supposed to have been given to you along with GNU Emacs so you | |
21 ;; can know your rights and responsibilities. It should be in a | |
22 ;; file named COPYING. Among other things, the copyright notice | |
23 ;; and this notice must be preserved on all copies. | |
24 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
25 ;;; Commentary: |
40785 | 26 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
27 ;;; Code: |
40785 | 28 |
29 ;; This file is autoloaded from calc-ext.el. | |
58674
0efdac1406ea
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58546
diff
changeset
|
30 |
40785 | 31 (require 'calc-ext) |
32 (require 'calc-macs) | |
33 | |
34 (defun calc-num-prefix (n) | |
35 "Use the number at the top of stack as the numeric prefix for the next command. | |
36 With a prefix, push that prefix as a number onto the stack." | |
37 (interactive "P") | |
38 (calc-wrapper | |
39 (if n | |
40 (calc-enter-result 0 "" (prefix-numeric-value n)) | |
41 (let ((num (calc-top 1))) | |
42 (if (math-messy-integerp num) | |
43 (setq num (math-trunc num))) | |
44 (or (integerp num) | |
45 (error "Argument must be a small integer")) | |
46 (calc-pop-stack 1) | |
47 (setq prefix-arg num) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
48 (message "%d-" num))))) ; a (lame) simulation of the real thing... |
40785 | 49 |
50 | |
51 (defun calc-more-recursion-depth (n) | |
52 (interactive "P") | |
53 (calc-wrapper | |
54 (if (calc-is-inverse) | |
55 (calc-less-recursion-depth n) | |
56 (let ((n (if n (prefix-numeric-value n) 2))) | |
57 (if (> n 1) | |
58 (setq max-specpdl-size (* max-specpdl-size n) | |
59 max-lisp-eval-depth (* max-lisp-eval-depth n)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
60 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth)))) |
40785 | 61 |
62 (defun calc-less-recursion-depth (n) | |
63 (interactive "P") | |
64 (let ((n (if n (prefix-numeric-value n) 2))) | |
65 (if (> n 1) | |
66 (setq max-specpdl-size | |
67 (max (/ max-specpdl-size n) 600) | |
68 max-lisp-eval-depth | |
69 (max (/ max-lisp-eval-depth n) 200)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
70 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth)) |
40785 | 71 |
72 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
73 (defvar calc-which-why nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
74 (defvar calc-last-why-command nil) |
40785 | 75 (defun calc-explain-why (why &optional more) |
76 (if (eq (car why) '*) | |
77 (setq why (cdr why))) | |
78 (let* ((pred (car why)) | |
79 (arg (nth 1 why)) | |
80 (msg (cond ((not pred) "Wrong type of argument") | |
81 ((stringp pred) pred) | |
82 ((eq pred 'integerp) "Integer expected") | |
83 ((eq pred 'natnump) | |
84 (if (and arg (Math-objvecp arg) (not (Math-integerp arg))) | |
85 "Integer expected" | |
86 "Nonnegative integer expected")) | |
87 ((eq pred 'posintp) | |
88 (if (and arg (Math-objvecp arg) (not (Math-integerp arg))) | |
89 "Integer expected" | |
90 "Positive integer expected")) | |
91 ((eq pred 'fixnump) | |
92 (if (and arg (Math-integerp arg)) | |
93 "Small integer expected" | |
94 "Integer expected")) | |
95 ((eq pred 'fixnatnump) | |
96 (if (and arg (Math-natnump arg)) | |
97 "Small integer expected" | |
98 (if (and arg (Math-objvecp arg) | |
99 (not (Math-integerp arg))) | |
100 "Integer expected" | |
101 "Nonnegative integer expected"))) | |
102 ((eq pred 'fixposintp) | |
103 (if (and arg (Math-integerp arg) (Math-posp arg)) | |
104 "Small integer expected" | |
105 (if (and arg (Math-objvecp arg) | |
106 (not (Math-integerp arg))) | |
107 "Integer expected" | |
108 "Positive integer expected"))) | |
109 ((eq pred 'posp) "Positive number expected") | |
110 ((eq pred 'negp) "Negative number expected") | |
111 ((eq pred 'nonzerop) "Nonzero number expected") | |
112 ((eq pred 'realp) "Real number expected") | |
113 ((eq pred 'anglep) "Real number expected") | |
114 ((eq pred 'hmsp) "HMS form expected") | |
115 ((eq pred 'datep) | |
116 (if (and arg (Math-objectp arg) | |
117 (not (Math-realp arg))) | |
118 "Real number or date form expected" | |
119 "Date form expected")) | |
120 ((eq pred 'numberp) "Number expected") | |
121 ((eq pred 'scalarp) "Number expected") | |
122 ((eq pred 'vectorp) "Vector or matrix expected") | |
123 ((eq pred 'numvecp) "Number or vector expected") | |
124 ((eq pred 'matrixp) "Matrix expected") | |
125 ((eq pred 'square-matrixp) | |
126 (if (and arg (math-matrixp arg)) | |
127 "Square matrix expected" | |
128 "Matrix expected")) | |
129 ((eq pred 'objectp) "Number expected") | |
130 ((eq pred 'constp) "Constant expected") | |
131 ((eq pred 'range) "Argument out of range") | |
132 (t (format "%s expected" pred)))) | |
133 (punc ": ") | |
134 (calc-can-abbrev-vectors t)) | |
135 (while (setq why (cdr why)) | |
136 (and (car why) | |
137 (setq msg (concat msg punc (if (stringp (car why)) | |
138 (car why) | |
139 (math-format-flat-expr (car why) 0))) | |
140 punc ", "))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
141 (message "%s%s" msg (if more " [w=more]" "")))) |
40785 | 142 |
143 (defun calc-why () | |
144 (interactive) | |
145 (if (not (eq this-command last-command)) | |
146 (if (eq last-command calc-last-why-command) | |
147 (setq calc-which-why (cdr calc-why)) | |
148 (setq calc-which-why calc-why))) | |
149 (if calc-which-why | |
150 (progn | |
151 (calc-explain-why (car calc-which-why) (cdr calc-which-why)) | |
152 (setq calc-which-why (cdr calc-which-why))) | |
153 (if calc-why | |
154 (progn | |
155 (message "(No further explanations available)") | |
156 (setq calc-which-why calc-why)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
157 (message "No explanations available")))) |
40785 | 158 |
159 | |
160 (defun calc-version () | |
161 (interactive) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
162 (message "Calc %s" calc-version)) |
40785 | 163 |
58546
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
164 ;; The following caches are declared in other files, but are |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
165 ;; reset here. |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
166 (defvar math-lud-cache) ; calc-mtx.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
167 (defvar math-log2-cache) ; calc-bin.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
168 (defvar math-radix-digits-cache) ; calc-bin.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
169 (defvar math-radix-float-cache-tag) ; calc-bin.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
170 (defvar math-random-cache) ; calc-comb.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
171 (defvar math-max-digits-cache) ; calc-bin.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
172 (defvar math-integral-cache) ; calcalg2.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
173 (defvar math-units-table) ; calc-units.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
174 (defvar math-format-date-cache) ; calc-forms.el |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
175 (defvar math-holidays-cache-tag) ; calc-forms.el |
40785 | 176 |
47694
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
177 (defun calc-flush-caches (&optional inhibit-msg) |
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
178 (interactive "P") |
40785 | 179 (calc-wrapper |
180 (setq math-lud-cache nil | |
181 math-log2-cache nil | |
182 math-radix-digits-cache nil | |
183 math-radix-float-cache-tag nil | |
184 math-random-cache nil | |
185 math-max-digits-cache nil | |
186 math-integral-cache nil | |
187 math-units-table nil | |
188 math-decls-cache-tag nil | |
189 math-eval-rules-cache-tag t | |
190 math-format-date-cache nil | |
191 math-holidays-cache-tag t) | |
192 (mapcar (function (lambda (x) (set x -100))) math-cache-list) | |
47694
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
193 (unless inhibit-msg |
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
194 (message "All internal calculator caches have been reset")))) |
40785 | 195 |
196 | |
197 ;;; Conversions. | |
198 | |
199 (defun calc-clean (n) | |
200 (interactive "P") | |
201 (calc-slow-wrapper | |
202 (calc-with-default-simplification | |
203 (let ((func (if (calc-is-hyperbolic) 'calcFunc-clean 'calcFunc-pclean))) | |
204 (calc-enter-result 1 "cln" | |
205 (if n | |
206 (let ((n (prefix-numeric-value n))) | |
207 (list func | |
208 (calc-top-n 1) | |
209 (if (<= n 0) | |
210 (+ n calc-internal-prec) | |
211 n))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
212 (list func (calc-top-n 1)))))))) |
40785 | 213 |
214 (defun calc-clean-num (num) | |
215 (interactive "P") | |
216 (calc-clean (- (if num | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
217 (prefix-numeric-value num) |
40785 | 218 (if (and (>= last-command-char ?0) |
219 (<= last-command-char ?9)) | |
220 (- last-command-char ?0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
221 (error "Number required")))))) |
40785 | 222 |
223 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
224 (defvar math-chopping-small nil) |
40785 | 225 (defun calcFunc-clean (a &optional prec) ; [X X S] [Public] |
226 (if prec | |
227 (cond ((Math-messy-integerp prec) | |
228 (calcFunc-clean a (math-trunc prec))) | |
229 ((or (not (integerp prec)) | |
230 (< prec 3)) | |
231 (calc-record-why "*Precision must be an integer 3 or above") | |
232 (list 'calcFunc-clean a prec)) | |
233 ((not (Math-objvecp a)) | |
234 (list 'calcFunc-clean a prec)) | |
235 (t (let ((calc-internal-prec prec) | |
236 (math-chopping-small t)) | |
237 (calcFunc-clean (math-normalize a))))) | |
238 (cond ((eq (car-safe a) 'polar) | |
239 (let ((theta (math-mod (nth 2 a) | |
240 (if (eq calc-angle-mode 'rad) | |
241 (math-two-pi) | |
242 360)))) | |
243 (math-neg | |
244 (math-neg | |
245 (math-normalize | |
246 (list 'polar | |
247 (calcFunc-clean (nth 1 a)) | |
248 (calcFunc-clean theta))))))) | |
249 ((memq (car-safe a) '(vec date hms)) | |
250 (cons (car a) (mapcar 'calcFunc-clean (cdr a)))) | |
251 ((memq (car-safe a) '(cplx mod sdev intv)) | |
252 (math-normalize (cons (car a) (mapcar 'calcFunc-clean (cdr a))))) | |
253 ((eq (car-safe a) 'float) | |
254 (if math-chopping-small | |
255 (if (or (> (nth 2 a) (- calc-internal-prec)) | |
256 (Math-lessp (- calc-internal-prec) (calcFunc-xpon a))) | |
257 (if (and (math-num-integerp a) | |
258 (math-lessp (calcFunc-xpon a) calc-internal-prec)) | |
259 (math-trunc a) | |
260 a) | |
261 0) | |
262 a)) | |
263 ((Math-objectp a) a) | |
264 ((math-infinitep a) a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
265 (t (list 'calcFunc-clean a))))) |
40785 | 266 |
267 (defun calcFunc-pclean (a &optional prec) | |
268 (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
269 a)) |
40785 | 270 |
271 (defun calcFunc-pfloat (a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
272 (math-map-over-constants 'math-float a)) |
40785 | 273 |
274 (defun calcFunc-pfrac (a &optional tol) | |
275 (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
276 a)) |
40785 | 277 |
58546
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
278 ;; The variable math-moc-func is local to math-map-over-constants, |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
279 ;; but is used by math-map-over-constants-rec, which is called by |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
280 ;; math-map-over-constants. |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
281 (defvar math-moc-func) |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
282 |
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
283 (defun math-map-over-constants (math-moc-func expr) |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
284 (math-map-over-constants-rec expr)) |
40785 | 285 |
286 (defun math-map-over-constants-rec (expr) | |
287 (cond ((or (Math-primp expr) | |
288 (memq (car expr) '(intv sdev))) | |
289 (or (and (Math-objectp expr) | |
58546
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
290 (funcall math-moc-func expr)) |
40785 | 291 expr)) |
292 ((and (memq (car expr) '(^ calcFunc-subscr)) | |
58546
87b2b810675b
(calc-flush-caches): Remove unnecessary variables.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
52401
diff
changeset
|
293 (eq math-moc-func 'math-float) |
40785 | 294 (= (length expr) 3) |
295 (Math-integerp (nth 2 expr))) | |
296 (list (car expr) | |
297 (math-map-over-constants-rec (nth 1 expr)) | |
298 (nth 2 expr))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
299 (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr)))))) |
40785 | 300 |
58674
0efdac1406ea
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58546
diff
changeset
|
301 (provide 'calc-stuff) |
0efdac1406ea
Add a provide statement.
Jay Belanger <jay.p.belanger@gmail.com>
parents:
58546
diff
changeset
|
302 |
52401 | 303 ;;; arch-tag: 789332ef-a178-49d3-8fb7-5d7ed7e21f56 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
304 ;;; calc-stuff.el ends here |