Mercurial > emacs
annotate lisp/calc/calc-stuff.el @ 83188:1425b1df1da8
Merged in changes from CVS trunk.
Patches applied:
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-482
Update from CVS
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-483
Build-in-place tweak
* miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-484
Update from CVS
git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-228
author | Karoly Lorentey <lorentey@elte.hu> |
---|---|
date | Mon, 09 Aug 2004 11:05:10 +0000 |
parents | 695cf19ef79e |
children | 87b2b810675b 375f2633d815 |
rev | line source |
---|---|
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
1 ;;; calc-stuff.el --- miscellaneous functions for Calc |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
2 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
4 |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
5 ;; Author: David Gillespie <daveg@synaptics.com> |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
6 ;; Maintainers: D. Goel <deego@gnufans.org> |
49263
f4d68f97221e
Add new maintainer (deego).
Deepak Goel <deego@gnufans.org>
parents:
47694
diff
changeset
|
7 ;; Colin Walters <walters@debian.org> |
40785 | 8 |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; GNU Emacs is distributed in the hope that it will be useful, | |
12 ;; but WITHOUT ANY WARRANTY. No author or distributor | |
13 ;; accepts responsibility to anyone for the consequences of using it | |
14 ;; or for whether it serves any particular purpose or works at all, | |
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
16 ;; License for full details. | |
17 | |
18 ;; Everyone is granted permission to copy, modify and redistribute | |
19 ;; GNU Emacs, but only under the conditions described in the | |
20 ;; GNU Emacs General Public License. A copy of this license is | |
21 ;; supposed to have been given to you along with GNU Emacs so you | |
22 ;; can know your rights and responsibilities. It should be in a | |
23 ;; file named COPYING. Among other things, the copyright notice | |
24 ;; and this notice must be preserved on all copies. | |
25 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
26 ;;; Commentary: |
40785 | 27 |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
28 ;;; Code: |
40785 | 29 |
30 ;; This file is autoloaded from calc-ext.el. | |
31 (require 'calc-ext) | |
32 | |
33 (require 'calc-macs) | |
34 | |
35 (defun calc-Need-calc-stuff () nil) | |
36 | |
37 | |
38 (defun calc-num-prefix (n) | |
39 "Use the number at the top of stack as the numeric prefix for the next command. | |
40 With a prefix, push that prefix as a number onto the stack." | |
41 (interactive "P") | |
42 (calc-wrapper | |
43 (if n | |
44 (calc-enter-result 0 "" (prefix-numeric-value n)) | |
45 (let ((num (calc-top 1))) | |
46 (if (math-messy-integerp num) | |
47 (setq num (math-trunc num))) | |
48 (or (integerp num) | |
49 (error "Argument must be a small integer")) | |
50 (calc-pop-stack 1) | |
51 (setq prefix-arg num) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
52 (message "%d-" num))))) ; a (lame) simulation of the real thing... |
40785 | 53 |
54 | |
55 (defun calc-more-recursion-depth (n) | |
56 (interactive "P") | |
57 (calc-wrapper | |
58 (if (calc-is-inverse) | |
59 (calc-less-recursion-depth n) | |
60 (let ((n (if n (prefix-numeric-value n) 2))) | |
61 (if (> n 1) | |
62 (setq max-specpdl-size (* max-specpdl-size n) | |
63 max-lisp-eval-depth (* max-lisp-eval-depth n)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
64 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth)))) |
40785 | 65 |
66 (defun calc-less-recursion-depth (n) | |
67 (interactive "P") | |
68 (let ((n (if n (prefix-numeric-value n) 2))) | |
69 (if (> n 1) | |
70 (setq max-specpdl-size | |
71 (max (/ max-specpdl-size n) 600) | |
72 max-lisp-eval-depth | |
73 (max (/ max-lisp-eval-depth n) 200)))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
74 (message "max-lisp-eval-depth is now %d" max-lisp-eval-depth)) |
40785 | 75 |
76 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
77 (defvar calc-which-why nil) |
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
78 (defvar calc-last-why-command nil) |
40785 | 79 (defun calc-explain-why (why &optional more) |
80 (if (eq (car why) '*) | |
81 (setq why (cdr why))) | |
82 (let* ((pred (car why)) | |
83 (arg (nth 1 why)) | |
84 (msg (cond ((not pred) "Wrong type of argument") | |
85 ((stringp pred) pred) | |
86 ((eq pred 'integerp) "Integer expected") | |
87 ((eq pred 'natnump) | |
88 (if (and arg (Math-objvecp arg) (not (Math-integerp arg))) | |
89 "Integer expected" | |
90 "Nonnegative integer expected")) | |
91 ((eq pred 'posintp) | |
92 (if (and arg (Math-objvecp arg) (not (Math-integerp arg))) | |
93 "Integer expected" | |
94 "Positive integer expected")) | |
95 ((eq pred 'fixnump) | |
96 (if (and arg (Math-integerp arg)) | |
97 "Small integer expected" | |
98 "Integer expected")) | |
99 ((eq pred 'fixnatnump) | |
100 (if (and arg (Math-natnump arg)) | |
101 "Small integer expected" | |
102 (if (and arg (Math-objvecp arg) | |
103 (not (Math-integerp arg))) | |
104 "Integer expected" | |
105 "Nonnegative integer expected"))) | |
106 ((eq pred 'fixposintp) | |
107 (if (and arg (Math-integerp arg) (Math-posp arg)) | |
108 "Small integer expected" | |
109 (if (and arg (Math-objvecp arg) | |
110 (not (Math-integerp arg))) | |
111 "Integer expected" | |
112 "Positive integer expected"))) | |
113 ((eq pred 'posp) "Positive number expected") | |
114 ((eq pred 'negp) "Negative number expected") | |
115 ((eq pred 'nonzerop) "Nonzero number expected") | |
116 ((eq pred 'realp) "Real number expected") | |
117 ((eq pred 'anglep) "Real number expected") | |
118 ((eq pred 'hmsp) "HMS form expected") | |
119 ((eq pred 'datep) | |
120 (if (and arg (Math-objectp arg) | |
121 (not (Math-realp arg))) | |
122 "Real number or date form expected" | |
123 "Date form expected")) | |
124 ((eq pred 'numberp) "Number expected") | |
125 ((eq pred 'scalarp) "Number expected") | |
126 ((eq pred 'vectorp) "Vector or matrix expected") | |
127 ((eq pred 'numvecp) "Number or vector expected") | |
128 ((eq pred 'matrixp) "Matrix expected") | |
129 ((eq pred 'square-matrixp) | |
130 (if (and arg (math-matrixp arg)) | |
131 "Square matrix expected" | |
132 "Matrix expected")) | |
133 ((eq pred 'objectp) "Number expected") | |
134 ((eq pred 'constp) "Constant expected") | |
135 ((eq pred 'range) "Argument out of range") | |
136 (t (format "%s expected" pred)))) | |
137 (punc ": ") | |
138 (calc-can-abbrev-vectors t)) | |
139 (while (setq why (cdr why)) | |
140 (and (car why) | |
141 (setq msg (concat msg punc (if (stringp (car why)) | |
142 (car why) | |
143 (math-format-flat-expr (car why) 0))) | |
144 punc ", "))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
145 (message "%s%s" msg (if more " [w=more]" "")))) |
40785 | 146 |
147 (defun calc-why () | |
148 (interactive) | |
149 (if (not (eq this-command last-command)) | |
150 (if (eq last-command calc-last-why-command) | |
151 (setq calc-which-why (cdr calc-why)) | |
152 (setq calc-which-why calc-why))) | |
153 (if calc-which-why | |
154 (progn | |
155 (calc-explain-why (car calc-which-why) (cdr calc-which-why)) | |
156 (setq calc-which-why (cdr calc-which-why))) | |
157 (if calc-why | |
158 (progn | |
159 (message "(No further explanations available)") | |
160 (setq calc-which-why calc-why)) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
161 (message "No explanations available")))) |
40785 | 162 |
163 | |
164 (defun calc-version () | |
165 (interactive) | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
166 (message "Calc %s" calc-version)) |
40785 | 167 |
168 | |
47694
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
169 (defun calc-flush-caches (&optional inhibit-msg) |
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
170 (interactive "P") |
40785 | 171 (calc-wrapper |
172 (setq math-lud-cache nil | |
173 math-log2-cache nil | |
174 math-radix-digits-cache nil | |
175 math-radix-float-cache-tag nil | |
176 math-random-cache nil | |
177 math-max-digits-cache nil | |
178 math-checked-rewrites 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-graph-var-cache nil | |
184 math-graph-data-cache nil | |
185 math-format-date-cache nil | |
186 math-holidays-cache-tag t) | |
187 (mapcar (function (lambda (x) (set x -100))) math-cache-list) | |
47694
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
188 (unless inhibit-msg |
c7b1f2c10ed4
(calc-flush-caches): Add optional arg `inhibit-msg'.
Colin Walters <walters@gnu.org>
parents:
41271
diff
changeset
|
189 (message "All internal calculator caches have been reset")))) |
40785 | 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))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
207 (list func (calc-top-n 1)))))))) |
40785 | 208 |
209 (defun calc-clean-num (num) | |
210 (interactive "P") | |
211 (calc-clean (- (if num | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
49263
diff
changeset
|
212 (prefix-numeric-value num) |
40785 | 213 (if (and (>= last-command-char ?0) |
214 (<= last-command-char ?9)) | |
215 (- last-command-char ?0) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
216 (error "Number required")))))) |
40785 | 217 |
218 | |
41271
fcd507927105
Change all toplevel `setq' forms to `defvar' forms, and move them
Colin Walters <walters@gnu.org>
parents:
41047
diff
changeset
|
219 (defvar math-chopping-small nil) |
40785 | 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) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
260 (t (list 'calcFunc-clean a))))) |
40785 | 261 |
262 (defun calcFunc-pclean (a &optional prec) | |
263 (math-map-over-constants (function (lambda (x) (calcFunc-clean x prec))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
264 a)) |
40785 | 265 |
266 (defun calcFunc-pfloat (a) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
267 (math-map-over-constants 'math-float a)) |
40785 | 268 |
269 (defun calcFunc-pfrac (a &optional tol) | |
270 (math-map-over-constants (function (lambda (x) (calcFunc-frac x tol))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
271 a)) |
40785 | 272 |
273 (defun math-map-over-constants (func expr) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
274 (math-map-over-constants-rec expr)) |
40785 | 275 |
276 (defun math-map-over-constants-rec (expr) | |
277 (cond ((or (Math-primp expr) | |
278 (memq (car expr) '(intv sdev))) | |
279 (or (and (Math-objectp expr) | |
280 (funcall func expr)) | |
281 expr)) | |
282 ((and (memq (car expr) '(^ calcFunc-subscr)) | |
283 (eq func 'math-float) | |
284 (= (length expr) 3) | |
285 (Math-integerp (nth 2 expr))) | |
286 (list (car expr) | |
287 (math-map-over-constants-rec (nth 1 expr)) | |
288 (nth 2 expr))) | |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
289 (t (cons (car expr) (mapcar 'math-map-over-constants-rec (cdr expr)))))) |
40785 | 290 |
52401 | 291 ;;; arch-tag: 789332ef-a178-49d3-8fb7-5d7ed7e21f56 |
41047
73f364fd8aaa
Style cleanup; don't put closing parens on their
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
292 ;;; calc-stuff.el ends here |