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