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