EasyPG 1.07 Released
[packages] / xemacs-packages / calc / calc-stuff.el
1 ;;; calc-stuff.el --- miscellaneous functions for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 ;; This file is autoloaded from calc-ext.el.
27
28 (require 'calc-ext)
29 (require 'calc-macs)
30
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."
34   (interactive "P")
35   (calc-wrapper
36    (if n
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)))
41        (or (integerp num)
42            (error "Argument must be a small integer"))
43        (calc-pop-stack 1)
44        (setq prefix-arg num)
45        (message "%d-" num)))))    ; a (lame) simulation of the real thing...
46
47
48 (defun calc-more-recursion-depth (n)
49   (interactive "P")
50   (calc-wrapper
51    (if (calc-is-inverse)
52        (calc-less-recursion-depth n)
53      (let ((n (if n (prefix-numeric-value n) 2)))
54        (if (> n 1)
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))))
58
59 (defun calc-less-recursion-depth (n)
60   (interactive "P")
61   (let ((n (if n (prefix-numeric-value n) 2)))
62     (if (> n 1)
63         (setq max-specpdl-size
64               (max (/ max-specpdl-size n) 600)
65               max-lisp-eval-depth
66               (max (/ max-lisp-eval-depth n) 200))))
67   (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth))
68
69
70 (defvar calc-which-why nil)
71 (defvar calc-last-why-command nil)
72 (defun calc-explain-why (why &optional more)
73   (if (eq (car why) '*)
74       (setq why (cdr why)))
75   (let* ((pred (car why))
76          (arg (nth 1 why))
77          (msg (cond ((not pred) "Wrong type of argument")
78                     ((stringp pred) pred)
79                     ((eq pred 'integerp) "Integer expected")
80                     ((eq pred 'natnump)
81                      (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
82                          "Integer expected"
83                        "Nonnegative integer expected"))
84                     ((eq pred 'posintp)
85                      (if (and arg (Math-objvecp arg) (not (Math-integerp arg)))
86                          "Integer expected"
87                        "Positive integer expected"))
88                     ((eq pred 'fixnump)
89                      (if (and arg (Math-integerp arg))
90                          "Small integer expected"
91                        "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)))
97                            "Integer expected"
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)))
104                            "Integer expected"
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")
112                     ((eq pred 'datep)
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"
125                        "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))))
130          (punc ": ")
131          (calc-can-abbrev-vectors t))
132     (while (setq why (cdr why))
133       (and (car why)
134            (setq msg (concat msg punc (if (stringp (car why))
135                                           (car why)
136                                         (math-format-flat-expr (car why) 0)))
137                  punc ", ")))
138     (message "%s%s" msg (if more "  [w=more]" ""))))
139
140 (defun calc-why ()
141   (interactive)
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)))
146   (if calc-which-why
147       (progn
148         (calc-explain-why (car calc-which-why) (cdr calc-which-why))
149         (setq calc-which-why (cdr calc-which-why)))
150     (if calc-why
151         (progn
152           (message "(No further explanations available)")
153           (setq calc-which-why calc-why))
154       (message "No explanations available"))))
155
156 ;; The following caches are declared in other files, but are
157 ;; reset here.
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
169
170 (defun calc-flush-caches (&optional inhibit-msg)
171   (interactive "P")
172   (calc-wrapper
173    (setq math-lud-cache nil
174          math-log2-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
180          math-units-table 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)
186    (unless inhibit-msg
187      (message "All internal calculator caches have been reset"))))
188
189
190 ;;; Conversions.
191
192 (defun calc-clean (n)
193   (interactive "P")
194   (calc-slow-wrapper
195    (calc-with-default-simplification
196     (let ((func (if (calc-is-hyperbolic) 'calcFunc-clean 'calcFunc-pclean)))
197       (calc-enter-result 1 "cln"
198                          (if n
199                              (let ((n (prefix-numeric-value n)))
200                                (list func
201                                      (calc-top-n 1)
202                                      (if (<= n 0)
203                                          (+ n calc-internal-prec)
204                                        n)))
205                            (list func (calc-top-n 1))))))))
206
207 (defun calc-clean-num (num)
208   (interactive "P")
209   (calc-clean (- (if num
210                      (prefix-numeric-value num)
211                    (if (and (>= last-command-char ?0)
212                             (<= last-command-char ?9))
213                        (- last-command-char ?0)
214                      (error "Number required"))))))
215
216
217 (defvar math-chopping-small nil)
218 (defun calcFunc-clean (a &optional prec)   ; [X X S] [Public]
219   (if prec
220       (cond ((Math-messy-integerp prec)
221              (calcFunc-clean a (math-trunc prec)))
222             ((or (not (integerp prec))
223                  (< prec 3))
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)
234                                       (math-two-pi)
235                                     360))))
236              (math-neg
237               (math-neg
238                (math-normalize
239                 (list 'polar
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))
252                        (math-trunc a)
253                      a)
254                  0)
255              a))
256           ((Math-objectp a) a)
257           ((math-infinitep a) a)
258           (t (list 'calcFunc-clean a)))))
259
260 (defun calcFunc-pclean (a &optional prec)
261   (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec)))
262                            a))
263
264 (defun calcFunc-pfloat (a)
265   (math-map-over-constants 'math-float a))
266
267 (defun calcFunc-pfrac (a &optional tol)
268   (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol)))
269                            a))
270
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)
275
276 (defun math-map-over-constants (math-moc-func expr)
277   (math-map-over-constants-rec expr))
278
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))
284              expr))
285         ((and (memq (car expr) '(^ calcFunc-subscr))
286               (eq math-moc-func 'math-float)
287               (= (length expr) 3)
288               (Math-integerp (nth 2 expr)))
289          (list (car expr)
290                (math-map-over-constants-rec (nth 1 expr))
291                (nth 2 expr)))
292         (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr))))))
293
294 (provide 'calc-stuff)
295
296 ;;; calc-stuff.el ends here