1 ;;; calc-stuff.el --- miscellaneous functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This file is autoloaded from calc-ext.el.
31 (defun calc-num-prefix (n)
32 "Use the number at the top of stack as the numeric prefix for the next command.
33 With a prefix, push that prefix as a number onto the stack."
37 (calc-enter-result 0 "" (prefix-numeric-value n))
38 (let ((num (calc-top 1)))
39 (if (math-messy-integerp num)
40 (setq num (math-trunc num)))
42 (error "Argument must be a small integer"))
45 (message "%d-" num))))) ; a (lame) simulation of the real thing...
48 (defun calc-more-recursion-depth (n)
52 (calc-less-recursion-depth n)
53 (let ((n (if n (prefix-numeric-value n) 2)))
55 (setq max-specpdl-size (* max-specpdl-size n)
56 max-lisp-eval-depth (* max-lisp-eval-depth n))))
57 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))))
59 (defun calc-less-recursion-depth (n)
61 (let ((n (if n (prefix-numeric-value n) 2)))
63 (setq max-specpdl-size
64 (max (/ max-specpdl-size n) 600)
66 (max (/ max-lisp-eval-depth n) 200))))
67 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))
70 (defvar calc-which-why nil)
71 (defvar calc-last-why-command nil)
72 (defun calc-explain-why (why &optional more)
75 (let* ((pred (car why))
77 (msg (cond ((not pred) "Wrong type of argument")
79 ((eq pred 'integerp) "Integer expected")
81 (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
83 "Nonnegative integer expected"))
85 (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
87 "Positive integer expected"))
89 (if (and arg (Math-integerp arg))
90 "Small integer expected"
92 ((eq pred 'fixnatnump)
93 (if (and arg (Math-natnump arg))
94 "Small integer expected"
95 (if (and arg (Math-objvecp arg)
96 (not (Math-integerp arg)))
98 "Nonnegative integer expected")))
99 ((eq pred 'fixposintp)
100 (if (and arg (Math-integerp arg) (Math-posp arg))
101 "Small integer expected"
102 (if (and arg (Math-objvecp arg)
103 (not (Math-integerp arg)))
105 "Positive integer expected")))
106 ((eq pred 'posp) "Positive number expected")
107 ((eq pred 'negp) "Negative number expected")
108 ((eq pred 'nonzerop) "Nonzero number expected")
109 ((eq pred 'realp) "Real number expected")
110 ((eq pred 'anglep) "Real number expected")
111 ((eq pred 'hmsp) "HMS form expected")
113 (if (and arg (Math-objectp arg)
114 (not (Math-realp arg)))
115 "Real number or date form expected"
116 "Date form expected"))
117 ((eq pred 'numberp) "Number expected")
118 ((eq pred 'scalarp) "Number expected")
119 ((eq pred 'vectorp) "Vector or matrix expected")
120 ((eq pred 'numvecp) "Number or vector expected")
121 ((eq pred 'matrixp) "Matrix expected")
122 ((eq pred 'square-matrixp)
123 (if (and arg (math-matrixp arg))
124 "Square matrix expected"
126 ((eq pred 'objectp) "Number expected")
127 ((eq pred 'constp) "Constant expected")
128 ((eq pred 'range) "Argument out of range")
129 (t (format "%s expected" pred))))
131 (calc-can-abbrev-vectors t))
132 (while (setq why (cdr why))
134 (setq msg (concat msg punc (if (stringp (car why))
136 (math-format-flat-expr (car why) 0)))
138 (message "%s%s" msg (if more " [w=more]" ""))))
142 (if (not (eq this-command last-command))
143 (if (eq last-command calc-last-why-command)
144 (setq calc-which-why (cdr calc-why))
145 (setq calc-which-why calc-why)))
148 (calc-explain-why (car calc-which-why) (cdr calc-which-why))
149 (setq calc-which-why (cdr calc-which-why)))
152 (message "(No further explanations available)")
153 (setq calc-which-why calc-why))
154 (message "No explanations available"))))
156 ;; The following caches are declared in other files, but are
158 (defvar math-lud-cache) ; calc-mtx.el
159 (defvar math-log2-cache) ; calc-bin.el
160 (defvar math-radix-digits-cache) ; calc-bin.el
161 (defvar math-radix-float-cache-tag) ; calc-bin.el
162 (defvar math-random-cache) ; calc-comb.el
163 (defvar math-max-digits-cache) ; calc-bin.el
164 (defvar math-integral-cache) ; calcalg2.el
165 (defvar math-units-table) ; calc-units.el
166 (defvar math-decls-cache-tag) ; calc-arith.el
167 (defvar math-format-date-cache) ; calc-forms.el
168 (defvar math-holidays-cache-tag) ; calc-forms.el
170 (defun calc-flush-caches (&optional inhibit-msg)
173 (setq math-lud-cache nil
175 math-radix-digits-cache nil
176 math-radix-float-cache-tag nil
177 math-random-cache nil
178 math-max-digits-cache nil
179 math-integral-cache nil
181 math-decls-cache-tag nil
182 math-eval-rules-cache-tag t
183 math-format-date-cache nil
184 math-holidays-cache-tag t)
185 (mapc (function (lambda (x) (set x -100))) math-cache-list)
187 (message "All internal calculator caches have been reset"))))
192 (defun calc-clean (n)
195 (calc-with-default-simplification
196 (let ((func (if (calc-is-hyperbolic) 'calcFunc-clean 'calcFunc-pclean)))
197 (calc-enter-result 1 "cln"
199 (let ((n (prefix-numeric-value n)))
203 (+ n calc-internal-prec)
205 (list func (calc-top-n 1))))))))
207 (defun calc-clean-num (num)
209 (calc-clean (- (if num
210 (prefix-numeric-value num)
211 (if (and (>= last-command-event ?0)
212 (<= last-command-event ?9))
213 (- last-command-event ?0)
214 (error "Number required"))))))
217 (defvar math-chopping-small nil)
218 (defun calcFunc-clean (a &optional prec) ; [X X S] [Public]
220 (cond ((Math-messy-integerp prec)
221 (calcFunc-clean a (math-trunc prec)))
222 ((or (not (integerp prec))
224 (calc-record-why "*Precision must be an integer 3 or above")
225 (list 'calcFunc-clean a prec))
226 ((not (Math-objvecp a))
227 (list 'calcFunc-clean a prec))
228 (t (let ((calc-internal-prec prec)
229 (math-chopping-small t))
230 (calcFunc-clean (math-normalize a)))))
231 (cond ((eq (car-safe a) 'polar)
232 (let ((theta (math-mod (nth 2 a)
233 (if (eq calc-angle-mode 'rad)
240 (calcFunc-clean (nth 1 a))
241 (calcFunc-clean theta)))))))
242 ((memq (car-safe a) '(vec date hms))
243 (cons (car a) (mapcar 'calcFunc-clean (cdr a))))
244 ((memq (car-safe a) '(cplx mod sdev intv))
245 (math-normalize (cons (car a) (mapcar 'calcFunc-clean (cdr a)))))
246 ((eq (car-safe a) 'float)
247 (if math-chopping-small
248 (if (or (> (nth 2 a) (- calc-internal-prec))
249 (Math-lessp (- calc-internal-prec) (calcFunc-xpon a)))
250 (if (and (math-num-integerp a)
251 (math-lessp (calcFunc-xpon a) calc-internal-prec))
257 ((math-infinitep a) a)
258 (t (list 'calcFunc-clean a)))))
260 (defun calcFunc-pclean (a &optional prec)
261 (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec)))
264 (defun calcFunc-pfloat (a)
265 (math-map-over-constants 'math-float a))
267 (defun calcFunc-pfrac (a &optional tol)
268 (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol)))
271 ;; The variable math-moc-func is local to math-map-over-constants,
272 ;; but is used by math-map-over-constants-rec, which is called by
273 ;; math-map-over-constants.
274 (defvar math-moc-func)
276 (defun math-map-over-constants (math-moc-func expr)
277 (math-map-over-constants-rec expr))
279 (defun math-map-over-constants-rec (expr)
280 (cond ((or (Math-primp expr)
281 (memq (car expr) '(intv sdev)))
282 (or (and (Math-objectp expr)
283 (funcall math-moc-func expr))
285 ((and (memq (car expr) '(^ calcFunc-subscr))
286 (eq math-moc-func 'math-float)
288 (Math-integerp (nth 2 expr)))
290 (math-map-over-constants-rec (nth 1 expr))
292 (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr))))))
294 (provide 'calc-stuff)
296 ;;; calc-stuff.el ends here