Initial Commit
[packages] / xemacs-packages / calc / calc-stuff.el
1 ;; Calculator for GNU Emacs, part II [calc-stuff.el]
2 ;; Copyright (C) 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 ;; Written by Dave Gillespie, daveg@synaptics.com.
4
5 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs is distributed in the hope that it will be useful,
8 ;; but WITHOUT ANY WARRANTY.  No author or distributor
9 ;; accepts responsibility to anyone for the consequences of using it
10 ;; or for whether it serves any particular purpose or works at all,
11 ;; unless he says so in writing.  Refer to the GNU Emacs General Public
12 ;; License for full details.
13
14 ;; Everyone is granted permission to copy, modify and redistribute
15 ;; GNU Emacs, but only under the conditions described in the
16 ;; GNU Emacs General Public License.   A copy of this license is
17 ;; supposed to have been given to you along with GNU Emacs so you
18 ;; can know your rights and responsibilities.  It should be in a
19 ;; file named COPYING.  Among other things, the copyright notice
20 ;; and this notice must be preserved on all copies.
21
22
23
24 ;; This file is autoloaded from calc-ext.el.
25 (require 'calc-ext)
26
27 (require 'calc-macs)
28
29 (defun calc-Need-calc-stuff () nil)
30
31
32 (defun calc-num-prefix (n)
33   "Use the number at the top of stack as the numeric prefix for the next command.
34 With a prefix, push that prefix as a number onto the stack."
35   (interactive "P")
36   (calc-wrapper
37    (if n
38        (calc-enter-result 0 "" (prefix-numeric-value n))
39      (let ((num (calc-top 1)))
40        (if (math-messy-integerp num)
41            (setq num (math-trunc num)))
42        (or (integerp num)
43            (error "Argument must be a small integer"))
44        (calc-pop-stack 1)
45        (setq prefix-arg num)
46        (message "%d-" num))))    ; a (lame) simulation of the real thing...
47 )
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))))
59      (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth)))
60 )
61
62 (defun calc-less-recursion-depth (n)
63   (interactive "P")
64   (let ((n (if n (prefix-numeric-value n) 2)))
65     (if (> n 1)
66         (setq max-specpdl-size
67               (max (/ max-specpdl-size n) 600)
68               max-lisp-eval-depth
69               (max (/ max-lisp-eval-depth n) 200))))
70   (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth)
71 )
72
73
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 ", ")))
140     (message "%s%s" msg (if more "  [w=more]" "")))
141 )
142
143 (defun calc-why ()
144   (interactive)
145   (if (not (eq this-command last-command))
146       (if (eq last-command calc-last-why-command)
147           (setq calc-which-why (cdr calc-why))
148         (setq calc-which-why calc-why)))
149   (if calc-which-why
150       (progn
151         (calc-explain-why (car calc-which-why) (cdr calc-which-why))
152         (setq calc-which-why (cdr calc-which-why)))
153     (if calc-why
154         (progn
155           (message "(No further explanations available)")
156           (setq calc-which-why calc-why))
157       (message "No explanations available")))
158 )
159 (setq calc-which-why nil)
160 (setq calc-last-why-command nil)
161
162
163 (defun calc-version ()
164   (interactive)
165   (message "Calc %s, installed %s" calc-version calc-installed-date))
166
167
168 (defun calc-flush-caches ()
169   (interactive)
170   (calc-wrapper
171    (setq math-lud-cache nil
172          math-log2-cache nil
173          math-radix-digits-cache nil
174          math-radix-float-cache-tag nil
175          math-random-cache nil
176          math-max-digits-cache nil
177          math-checked-rewrites nil
178          math-integral-cache nil
179          math-units-table nil
180          math-decls-cache-tag nil
181          math-eval-rules-cache-tag t
182          math-graph-var-cache nil
183          math-graph-data-cache nil
184          math-format-date-cache nil
185          math-holidays-cache-tag t)
186    (mapcar (function (lambda (x) (set x -100))) math-cache-list)
187    (message "All internal calculator caches have been reset."))
188 )
189
190
191 ;;; Conversions.
192
193 (defun calc-clean (n)
194   (interactive "P")
195   (calc-slow-wrapper
196    (calc-with-default-simplification
197     (let ((func (if (calc-is-hyperbolic) 'calcFunc-clean 'calcFunc-pclean)))
198       (calc-enter-result 1 "cln"
199                          (if n
200                              (let ((n (prefix-numeric-value n)))
201                                (list func
202                                      (calc-top-n 1)
203                                      (if (<= n 0)
204                                          (+ n calc-internal-prec)
205                                        n)))
206                            (list func (calc-top-n 1)))))))
207 )
208
209 (defun calc-clean-num (num)
210   (interactive "P")
211   (calc-clean (- (if num
212                      (prefix-numeric-value num) 
213                    (if (and (>= last-command-char ?0)
214                             (<= last-command-char ?9))
215                        (- last-command-char ?0)
216                      (error "Number required")))))
217 )
218
219
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)
260           (t (list 'calcFunc-clean a))))
261 )
262 (setq math-chopping-small nil)
263
264 (defun calcFunc-pclean (a &optional prec)
265   (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec)))
266                            a)
267 )
268
269 (defun calcFunc-pfloat (a)
270   (math-map-over-constants 'math-float a)
271 )
272
273 (defun calcFunc-pfrac (a &optional tol)
274   (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol)))
275                            a)
276 )
277
278 (defun math-map-over-constants (func expr)
279   (math-map-over-constants-rec expr)
280 )
281
282 (defun math-map-over-constants-rec (expr)
283   (cond ((or (Math-primp expr)
284              (memq (car expr) '(intv sdev)))
285          (or (and (Math-objectp expr)
286                   (funcall func expr))
287              expr))
288         ((and (memq (car expr) '(^ calcFunc-subscr))
289               (eq func 'math-float)
290               (= (length expr) 3)
291               (Math-integerp (nth 2 expr)))
292          (list (car expr)
293                (math-map-over-constants-rec (nth 1 expr))
294                (nth 2 expr)))
295         (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr)))))
296 )
297
298
299
300