]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-stuff.el
Add a provide statement.
[gnu-emacs] / lisp / calc / calc-stuff.el
1 ;;; calc-stuff.el --- miscellaneous functions for Calc
2
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <belanger@truman.edu>
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
25 ;;; Commentary:
26
27 ;;; Code:
28
29 ;; This file is autoloaded from calc-ext.el.
30 (require 'calc-ext)
31
32 (require 'calc-macs)
33
34 (defun calc-Need-calc-stuff () nil)
35
36
37 (defun calc-num-prefix (n)
38 "Use the number at the top of stack as the numeric prefix for the next command.
39 With a prefix, push that prefix as a number onto the stack."
40 (interactive "P")
41 (calc-wrapper
42 (if n
43 (calc-enter-result 0 "" (prefix-numeric-value n))
44 (let ((num (calc-top 1)))
45 (if (math-messy-integerp num)
46 (setq num (math-trunc num)))
47 (or (integerp num)
48 (error "Argument must be a small integer"))
49 (calc-pop-stack 1)
50 (setq prefix-arg num)
51 (message "%d-" num))))) ; a (lame) simulation of the real thing...
52
53
54 (defun calc-more-recursion-depth (n)
55 (interactive "P")
56 (calc-wrapper
57 (if (calc-is-inverse)
58 (calc-less-recursion-depth n)
59 (let ((n (if n (prefix-numeric-value n) 2)))
60 (if (> n 1)
61 (setq max-specpdl-size (* max-specpdl-size n)
62 max-lisp-eval-depth (* max-lisp-eval-depth n))))
63 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))))
64
65 (defun calc-less-recursion-depth (n)
66 (interactive "P")
67 (let ((n (if n (prefix-numeric-value n) 2)))
68 (if (> n 1)
69 (setq max-specpdl-size
70 (max (/ max-specpdl-size n) 600)
71 max-lisp-eval-depth
72 (max (/ max-lisp-eval-depth n) 200))))
73 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))
74
75
76 (defvar calc-which-why nil)
77 (defvar calc-last-why-command nil)
78 (defun calc-explain-why (why &optional more)
79 (if (eq (car why) '*)
80 (setq why (cdr why)))
81 (let* ((pred (car why))
82 (arg (nth 1 why))
83 (msg (cond ((not pred) "Wrong type of argument")
84 ((stringp pred) pred)
85 ((eq pred 'integerp) "Integer expected")
86 ((eq pred 'natnump)
87 (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
88 "Integer expected"
89 "Nonnegative integer expected"))
90 ((eq pred 'posintp)
91 (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
92 "Integer expected"
93 "Positive integer expected"))
94 ((eq pred 'fixnump)
95 (if (and arg (Math-integerp arg))
96 "Small integer expected"
97 "Integer expected"))
98 ((eq pred 'fixnatnump)
99 (if (and arg (Math-natnump arg))
100 "Small integer expected"
101 (if (and arg (Math-objvecp arg)
102 (not (Math-integerp arg)))
103 "Integer expected"
104 "Nonnegative integer expected")))
105 ((eq pred 'fixposintp)
106 (if (and arg (Math-integerp arg) (Math-posp arg))
107 "Small integer expected"
108 (if (and arg (Math-objvecp arg)
109 (not (Math-integerp arg)))
110 "Integer expected"
111 "Positive integer expected")))
112 ((eq pred 'posp) "Positive number expected")
113 ((eq pred 'negp) "Negative number expected")
114 ((eq pred 'nonzerop) "Nonzero number expected")
115 ((eq pred 'realp) "Real number expected")
116 ((eq pred 'anglep) "Real number expected")
117 ((eq pred 'hmsp) "HMS form expected")
118 ((eq pred 'datep)
119 (if (and arg (Math-objectp arg)
120 (not (Math-realp arg)))
121 "Real number or date form expected"
122 "Date form expected"))
123 ((eq pred 'numberp) "Number expected")
124 ((eq pred 'scalarp) "Number expected")
125 ((eq pred 'vectorp) "Vector or matrix expected")
126 ((eq pred 'numvecp) "Number or vector expected")
127 ((eq pred 'matrixp) "Matrix expected")
128 ((eq pred 'square-matrixp)
129 (if (and arg (math-matrixp arg))
130 "Square matrix expected"
131 "Matrix expected"))
132 ((eq pred 'objectp) "Number expected")
133 ((eq pred 'constp) "Constant expected")
134 ((eq pred 'range) "Argument out of range")
135 (t (format "%s expected" pred))))
136 (punc ": ")
137 (calc-can-abbrev-vectors t))
138 (while (setq why (cdr why))
139 (and (car why)
140 (setq msg (concat msg punc (if (stringp (car why))
141 (car why)
142 (math-format-flat-expr (car why) 0)))
143 punc ", ")))
144 (message "%s%s" msg (if more " [w=more]" ""))))
145
146 (defun calc-why ()
147 (interactive)
148 (if (not (eq this-command last-command))
149 (if (eq last-command calc-last-why-command)
150 (setq calc-which-why (cdr calc-why))
151 (setq calc-which-why calc-why)))
152 (if calc-which-why
153 (progn
154 (calc-explain-why (car calc-which-why) (cdr calc-which-why))
155 (setq calc-which-why (cdr calc-which-why)))
156 (if calc-why
157 (progn
158 (message "(No further explanations available)")
159 (setq calc-which-why calc-why))
160 (message "No explanations available"))))
161
162
163 (defun calc-version ()
164 (interactive)
165 (message "Calc %s" calc-version))
166
167 ;; The following caches are declared in other files, but are
168 ;; reset here.
169 (defvar math-lud-cache) ; calc-mtx.el
170 (defvar math-log2-cache) ; calc-bin.el
171 (defvar math-radix-digits-cache) ; calc-bin.el
172 (defvar math-radix-float-cache-tag) ; calc-bin.el
173 (defvar math-random-cache) ; calc-comb.el
174 (defvar math-max-digits-cache) ; calc-bin.el
175 (defvar math-integral-cache) ; calcalg2.el
176 (defvar math-units-table) ; calc-units.el
177 (defvar math-format-date-cache) ; calc-forms.el
178 (defvar math-holidays-cache-tag) ; calc-forms.el
179
180 (defun calc-flush-caches (&optional inhibit-msg)
181 (interactive "P")
182 (calc-wrapper
183 (setq math-lud-cache nil
184 math-log2-cache nil
185 math-radix-digits-cache nil
186 math-radix-float-cache-tag nil
187 math-random-cache nil
188 math-max-digits-cache nil
189 math-integral-cache nil
190 math-units-table nil
191 math-decls-cache-tag nil
192 math-eval-rules-cache-tag t
193 math-format-date-cache nil
194 math-holidays-cache-tag t)
195 (mapcar (function (lambda (x) (set x -100))) math-cache-list)
196 (unless inhibit-msg
197 (message "All internal calculator caches have been reset"))))
198
199
200 ;;; Conversions.
201
202 (defun calc-clean (n)
203 (interactive "P")
204 (calc-slow-wrapper
205 (calc-with-default-simplification
206 (let ((func (if (calc-is-hyperbolic) 'calcFunc-clean 'calcFunc-pclean)))
207 (calc-enter-result 1 "cln"
208 (if n
209 (let ((n (prefix-numeric-value n)))
210 (list func
211 (calc-top-n 1)
212 (if (<= n 0)
213 (+ n calc-internal-prec)
214 n)))
215 (list func (calc-top-n 1))))))))
216
217 (defun calc-clean-num (num)
218 (interactive "P")
219 (calc-clean (- (if num
220 (prefix-numeric-value num)
221 (if (and (>= last-command-char ?0)
222 (<= last-command-char ?9))
223 (- last-command-char ?0)
224 (error "Number required"))))))
225
226
227 (defvar math-chopping-small nil)
228 (defun calcFunc-clean (a &optional prec) ; [X X S] [Public]
229 (if prec
230 (cond ((Math-messy-integerp prec)
231 (calcFunc-clean a (math-trunc prec)))
232 ((or (not (integerp prec))
233 (< prec 3))
234 (calc-record-why "*Precision must be an integer 3 or above")
235 (list 'calcFunc-clean a prec))
236 ((not (Math-objvecp a))
237 (list 'calcFunc-clean a prec))
238 (t (let ((calc-internal-prec prec)
239 (math-chopping-small t))
240 (calcFunc-clean (math-normalize a)))))
241 (cond ((eq (car-safe a) 'polar)
242 (let ((theta (math-mod (nth 2 a)
243 (if (eq calc-angle-mode 'rad)
244 (math-two-pi)
245 360))))
246 (math-neg
247 (math-neg
248 (math-normalize
249 (list 'polar
250 (calcFunc-clean (nth 1 a))
251 (calcFunc-clean theta)))))))
252 ((memq (car-safe a) '(vec date hms))
253 (cons (car a) (mapcar 'calcFunc-clean (cdr a))))
254 ((memq (car-safe a) '(cplx mod sdev intv))
255 (math-normalize (cons (car a) (mapcar 'calcFunc-clean (cdr a)))))
256 ((eq (car-safe a) 'float)
257 (if math-chopping-small
258 (if (or (> (nth 2 a) (- calc-internal-prec))
259 (Math-lessp (- calc-internal-prec) (calcFunc-xpon a)))
260 (if (and (math-num-integerp a)
261 (math-lessp (calcFunc-xpon a) calc-internal-prec))
262 (math-trunc a)
263 a)
264 0)
265 a))
266 ((Math-objectp a) a)
267 ((math-infinitep a) a)
268 (t (list 'calcFunc-clean a)))))
269
270 (defun calcFunc-pclean (a &optional prec)
271 (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec)))
272 a))
273
274 (defun calcFunc-pfloat (a)
275 (math-map-over-constants 'math-float a))
276
277 (defun calcFunc-pfrac (a &optional tol)
278 (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol)))
279 a))
280
281 ;; The variable math-moc-func is local to math-map-over-constants,
282 ;; but is used by math-map-over-constants-rec, which is called by
283 ;; math-map-over-constants.
284 (defvar math-moc-func)
285
286 (defun math-map-over-constants (math-moc-func expr)
287 (math-map-over-constants-rec expr))
288
289 (defun math-map-over-constants-rec (expr)
290 (cond ((or (Math-primp expr)
291 (memq (car expr) '(intv sdev)))
292 (or (and (Math-objectp expr)
293 (funcall math-moc-func expr))
294 expr))
295 ((and (memq (car expr) '(^ calcFunc-subscr))
296 (eq math-moc-func 'math-float)
297 (= (length expr) 3)
298 (Math-integerp (nth 2 expr)))
299 (list (car expr)
300 (math-map-over-constants-rec (nth 1 expr))
301 (nth 2 expr)))
302 (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr))))))
303
304 ;;; arch-tag: 789332ef-a178-49d3-8fb7-5d7ed7e21f56
305 ;;; calc-stuff.el ends here