(while-no-input): Don't splice BODY directly into the `or' form.
[emacs.git] / lisp / calc / calc-stuff.el
blob8840ad827e12e2cbeebe1648bf212d43ace5f194
1 ;;; calc-stuff.el --- miscellaneous functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This file is autoloaded from calc-ext.el.
32 (require 'calc-ext)
33 (require 'calc-macs)
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)
49 (message "%d-" num))))) ; a (lame) simulation of the real thing...
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))))
61 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))))
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))))
71 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))
74 (defvar calc-which-why nil)
75 (defvar calc-last-why-command nil)
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 ", ")))
142 (message "%s%s" msg (if more " [w=more]" ""))))
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))
158 (message "No explanations available"))))
161 (defun calc-version ()
162 (interactive)
163 (message "Calc %s" calc-version))
165 ;; The following caches are declared in other files, but are
166 ;; reset here.
167 (defvar math-lud-cache) ; calc-mtx.el
168 (defvar math-log2-cache) ; calc-bin.el
169 (defvar math-radix-digits-cache) ; calc-bin.el
170 (defvar math-radix-float-cache-tag) ; calc-bin.el
171 (defvar math-random-cache) ; calc-comb.el
172 (defvar math-max-digits-cache) ; calc-bin.el
173 (defvar math-integral-cache) ; calcalg2.el
174 (defvar math-units-table) ; calc-units.el
175 (defvar math-decls-cache-tag) ; calc-arith.el
176 (defvar math-format-date-cache) ; calc-forms.el
177 (defvar math-holidays-cache-tag) ; calc-forms.el
179 (defun calc-flush-caches (&optional inhibit-msg)
180 (interactive "P")
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 (mapc (function (lambda (x) (set x -100))) math-cache-list)
195 (unless inhibit-msg
196 (message "All internal calculator caches have been reset"))))
199 ;;; Conversions.
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)))
214 (list func (calc-top-n 1))))))))
216 (defun calc-clean-num (num)
217 (interactive "P")
218 (calc-clean (- (if num
219 (prefix-numeric-value num)
220 (if (and (>= last-command-char ?0)
221 (<= last-command-char ?9))
222 (- last-command-char ?0)
223 (error "Number required"))))))
226 (defvar math-chopping-small nil)
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)
265 ((Math-objectp a) a)
266 ((math-infinitep a) a)
267 (t (list 'calcFunc-clean a)))))
269 (defun calcFunc-pclean (a &optional prec)
270 (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec)))
273 (defun calcFunc-pfloat (a)
274 (math-map-over-constants 'math-float a))
276 (defun calcFunc-pfrac (a &optional tol)
277 (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol)))
280 ;; The variable math-moc-func is local to math-map-over-constants,
281 ;; but is used by math-map-over-constants-rec, which is called by
282 ;; math-map-over-constants.
283 (defvar math-moc-func)
285 (defun math-map-over-constants (math-moc-func expr)
286 (math-map-over-constants-rec expr))
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)
292 (funcall math-moc-func expr))
293 expr))
294 ((and (memq (car expr) '(^ calcFunc-subscr))
295 (eq math-moc-func 'math-float)
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)))
301 (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr))))))
303 (provide 'calc-stuff)
305 ;;; arch-tag: 789332ef-a178-49d3-8fb7-5d7ed7e21f56
306 ;;; calc-stuff.el ends here