Mercurial > emacs
annotate lisp/calc/calc-arith.el @ 41266:c08a55ae8e5d
(calc-wrapper, calc-slow-wrapper)
(math-showing-full-precision, math-with-extra-prec, math-working)
(calc-with-default-simplification)
(calc-with-trail-buffer): Use backtick.
(Math-zerop, Math-integer-negp, Math-integer-posp, Math-negp)
(Math-looks-negp, Math-posp, Math-integerp, Math-natnump)
(Math-ratp, Math-realp, Math-anglep, Math-numberp, Math-scalarp)
(Math-vectorp, Math-messy-integerp, Math-objectp, Math-objvecp)
(Math-integer-neg, Math-equal, Math-lessp, Math-primp)
(Math-num-integerp, Math-bignum-test, Math-equal-int)
(Math-natnum-lessp, math-format-radix-digit): Change to `defsubst'.
(calc-record-compilation-date-macro): Deleted. Callers updated.
(math-format-radix-digit): Move to calc-bin.el.
Change all toplevel `setq' forms to `defvar' forms, and move them
before their first use. Use `when', `unless'. Remove trailing
periods from error forms. Add description and headers suggested by
Emacs Lisp coding conventions.
author | Colin Walters <walters@gnu.org> |
---|---|
date | Mon, 19 Nov 2001 07:35:49 +0000 |
parents | 45130b458dac |
children | fcd507927105 |
rev | line source |
---|---|
40785 | 1 ;; Calculator for GNU Emacs, part II [calc-arith.el] |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
40785 | 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-arith () nil) | |
30 | |
31 | |
32 ;;; Arithmetic. | |
33 | |
34 (defun calc-min (arg) | |
35 (interactive "P") | |
36 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
37 (calc-binary-op "min" 'calcFunc-min arg '(var inf var-inf)))) |
40785 | 38 |
39 (defun calc-max (arg) | |
40 (interactive "P") | |
41 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
42 (calc-binary-op "max" 'calcFunc-max arg '(neg (var inf var-inf))))) |
40785 | 43 |
44 (defun calc-abs (arg) | |
45 (interactive "P") | |
46 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
47 (calc-unary-op "abs" 'calcFunc-abs arg))) |
40785 | 48 |
49 | |
50 (defun calc-idiv (arg) | |
51 (interactive "P") | |
52 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
53 (calc-binary-op "\\" 'calcFunc-idiv arg 1))) |
40785 | 54 |
55 | |
56 (defun calc-floor (arg) | |
57 (interactive "P") | |
58 (calc-slow-wrapper | |
59 (if (calc-is-inverse) | |
60 (if (calc-is-hyperbolic) | |
61 (calc-unary-op "ceil" 'calcFunc-fceil arg) | |
62 (calc-unary-op "ceil" 'calcFunc-ceil arg)) | |
63 (if (calc-is-hyperbolic) | |
64 (calc-unary-op "flor" 'calcFunc-ffloor arg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
65 (calc-unary-op "flor" 'calcFunc-floor arg))))) |
40785 | 66 |
67 (defun calc-ceiling (arg) | |
68 (interactive "P") | |
69 (calc-invert-func) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
70 (calc-floor arg)) |
40785 | 71 |
72 (defun calc-round (arg) | |
73 (interactive "P") | |
74 (calc-slow-wrapper | |
75 (if (calc-is-inverse) | |
76 (if (calc-is-hyperbolic) | |
77 (calc-unary-op "trnc" 'calcFunc-ftrunc arg) | |
78 (calc-unary-op "trnc" 'calcFunc-trunc arg)) | |
79 (if (calc-is-hyperbolic) | |
80 (calc-unary-op "rond" 'calcFunc-fround arg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
81 (calc-unary-op "rond" 'calcFunc-round arg))))) |
40785 | 82 |
83 (defun calc-trunc (arg) | |
84 (interactive "P") | |
85 (calc-invert-func) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
86 (calc-round arg)) |
40785 | 87 |
88 (defun calc-mant-part (arg) | |
89 (interactive "P") | |
90 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
91 (calc-unary-op "mant" 'calcFunc-mant arg))) |
40785 | 92 |
93 (defun calc-xpon-part (arg) | |
94 (interactive "P") | |
95 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
96 (calc-unary-op "xpon" 'calcFunc-xpon arg))) |
40785 | 97 |
98 (defun calc-scale-float (arg) | |
99 (interactive "P") | |
100 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
101 (calc-binary-op "scal" 'calcFunc-scf arg))) |
40785 | 102 |
103 (defun calc-abssqr (arg) | |
104 (interactive "P") | |
105 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
106 (calc-unary-op "absq" 'calcFunc-abssqr arg))) |
40785 | 107 |
108 (defun calc-sign (arg) | |
109 (interactive "P") | |
110 (calc-slow-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
111 (calc-unary-op "sign" 'calcFunc-sign arg))) |
40785 | 112 |
113 (defun calc-increment (arg) | |
114 (interactive "p") | |
115 (calc-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
116 (calc-enter-result 1 "incr" (list 'calcFunc-incr (calc-top-n 1) arg)))) |
40785 | 117 |
118 (defun calc-decrement (arg) | |
119 (interactive "p") | |
120 (calc-wrapper | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
121 (calc-enter-result 1 "decr" (list 'calcFunc-decr (calc-top-n 1) arg)))) |
40785 | 122 |
123 | |
124 (defun math-abs-approx (a) | |
125 (cond ((Math-negp a) | |
126 (math-neg a)) | |
127 ((Math-anglep a) | |
128 a) | |
129 ((eq (car a) 'cplx) | |
130 (math-add (math-abs (nth 1 a)) (math-abs (nth 2 a)))) | |
131 ((eq (car a) 'polar) | |
132 (nth 1 a)) | |
133 ((eq (car a) 'sdev) | |
134 (math-abs-approx (nth 1 a))) | |
135 ((eq (car a) 'intv) | |
136 (math-max (math-abs (nth 2 a)) (math-abs (nth 3 a)))) | |
137 ((eq (car a) 'date) | |
138 a) | |
139 ((eq (car a) 'vec) | |
140 (math-reduce-vec 'math-add-abs-approx a)) | |
141 ((eq (car a) 'calcFunc-abs) | |
142 (car a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
143 (t a))) |
40785 | 144 |
145 (defun math-add-abs-approx (a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
146 (math-add (math-abs-approx a) (math-abs-approx b))) |
40785 | 147 |
148 | |
149 ;;;; Declarations. | |
150 | |
151 (setq math-decls-cache-tag nil) | |
152 (setq math-decls-cache nil) | |
153 (setq math-decls-all nil) | |
154 | |
155 ;;; Math-decls-cache is an a-list where each entry is a list of the form: | |
156 ;;; (VAR TYPES RANGE) | |
157 ;;; where VAR is a variable name (with var- prefix) or function name; | |
158 ;;; TYPES is a list of type symbols (any, int, frac, ...) | |
159 ;;; RANGE is a sorted vector of intervals describing the range. | |
160 | |
161 (defun math-setup-declarations () | |
162 (or (eq math-decls-cache-tag (calc-var-value 'var-Decls)) | |
163 (let ((p (calc-var-value 'var-Decls)) | |
164 vec type range) | |
165 (setq math-decls-cache-tag p | |
166 math-decls-cache nil) | |
167 (and (eq (car-safe p) 'vec) | |
168 (while (setq p (cdr p)) | |
169 (and (eq (car-safe (car p)) 'vec) | |
170 (setq vec (nth 2 (car p))) | |
171 (condition-case err | |
172 (let ((v (nth 1 (car p)))) | |
173 (setq type nil range nil) | |
174 (or (eq (car-safe vec) 'vec) | |
175 (setq vec (list 'vec vec))) | |
176 (while (and (setq vec (cdr vec)) | |
177 (not (Math-objectp (car vec)))) | |
178 (and (eq (car-safe (car vec)) 'var) | |
179 (let ((st (assq (nth 1 (car vec)) | |
180 math-super-types))) | |
181 (cond (st (setq type (append type st))) | |
182 ((eq (nth 1 (car vec)) 'pos) | |
183 (setq type (append type | |
184 '(real number)) | |
185 range | |
186 '(intv 1 0 (var inf var-inf)))) | |
187 ((eq (nth 1 (car vec)) 'nonneg) | |
188 (setq type (append type | |
189 '(real number)) | |
190 range | |
191 '(intv 3 0 | |
192 (var inf var-inf)))))))) | |
193 (if vec | |
194 (setq type (append type '(real number)) | |
195 range (math-prepare-set (cons 'vec vec)))) | |
196 (setq type (list type range)) | |
197 (or (eq (car-safe v) 'vec) | |
198 (setq v (list 'vec v))) | |
199 (while (setq v (cdr v)) | |
200 (if (or (eq (car-safe (car v)) 'var) | |
201 (not (Math-primp (car v)))) | |
202 (setq math-decls-cache | |
203 (cons (cons (if (eq (car (car v)) 'var) | |
204 (nth 2 (car v)) | |
205 (car (car v))) | |
206 type) | |
207 math-decls-cache))))) | |
208 (error nil))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
209 (setq math-decls-all (assq 'var-All math-decls-cache))))) |
40785 | 210 |
211 (defvar math-super-types | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
212 '((int numint rat real number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
213 (numint real number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
214 (frac rat real number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
215 (rat real number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
216 (float real number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
217 (real number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
218 (number) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
219 (scalar) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
220 (matrix vector) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
221 (vector) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
222 (const))) |
40785 | 223 |
224 (defun math-known-scalarp (a &optional assume-scalar) | |
225 (math-setup-declarations) | |
226 (if (if calc-matrix-mode | |
227 (eq calc-matrix-mode 'scalar) | |
228 assume-scalar) | |
229 (not (math-check-known-matrixp a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
230 (math-check-known-scalarp a))) |
40785 | 231 |
232 (defun math-known-matrixp (a) | |
233 (and (not (Math-scalarp a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
234 (not (math-known-scalarp a t)))) |
40785 | 235 |
236 ;;; Try to prove that A is a scalar (i.e., a non-vector). | |
237 (defun math-check-known-scalarp (a) | |
238 (cond ((Math-objectp a) t) | |
239 ((memq (car a) math-scalar-functions) | |
240 t) | |
241 ((memq (car a) math-real-scalar-functions) | |
242 t) | |
243 ((memq (car a) math-scalar-if-args-functions) | |
244 (while (and (setq a (cdr a)) | |
245 (math-check-known-scalarp (car a)))) | |
246 (null a)) | |
247 ((eq (car a) '^) | |
248 (math-check-known-scalarp (nth 1 a))) | |
249 ((math-const-var a) t) | |
250 (t | |
251 (let ((decl (if (eq (car a) 'var) | |
252 (or (assq (nth 2 a) math-decls-cache) | |
253 math-decls-all) | |
254 (assq (car a) math-decls-cache)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
255 (memq 'scalar (nth 1 decl)))))) |
40785 | 256 |
257 ;;; Try to prove that A is *not* a scalar. | |
258 (defun math-check-known-matrixp (a) | |
259 (cond ((Math-objectp a) nil) | |
260 ((memq (car a) math-nonscalar-functions) | |
261 t) | |
262 ((memq (car a) math-scalar-if-args-functions) | |
263 (while (and (setq a (cdr a)) | |
264 (not (math-check-known-matrixp (car a))))) | |
265 a) | |
266 ((eq (car a) '^) | |
267 (math-check-known-matrixp (nth 1 a))) | |
268 ((math-const-var a) nil) | |
269 (t | |
270 (let ((decl (if (eq (car a) 'var) | |
271 (or (assq (nth 2 a) math-decls-cache) | |
272 math-decls-all) | |
273 (assq (car a) math-decls-cache)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
274 (memq 'vector (nth 1 decl)))))) |
40785 | 275 |
276 | |
277 ;;; Try to prove that A is a real (i.e., not complex). | |
278 (defun math-known-realp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
279 (< (math-possible-signs a) 8)) |
40785 | 280 |
281 ;;; Try to prove that A is real and positive. | |
282 (defun math-known-posp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
283 (eq (math-possible-signs a) 4)) |
40785 | 284 |
285 ;;; Try to prove that A is real and negative. | |
286 (defun math-known-negp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
287 (eq (math-possible-signs a) 1)) |
40785 | 288 |
289 ;;; Try to prove that A is real and nonnegative. | |
290 (defun math-known-nonnegp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
291 (memq (math-possible-signs a) '(2 4 6))) |
40785 | 292 |
293 ;;; Try to prove that A is real and nonpositive. | |
294 (defun math-known-nonposp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
295 (memq (math-possible-signs a) '(1 2 3))) |
40785 | 296 |
297 ;;; Try to prove that A is nonzero. | |
298 (defun math-known-nonzerop (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
299 (memq (math-possible-signs a) '(1 4 5 8 9 12 13))) |
40785 | 300 |
301 ;;; Return true if A is negative, or looks negative but we don't know. | |
302 (defun math-guess-if-neg (a) | |
303 (let ((sgn (math-possible-signs a))) | |
304 (if (memq sgn '(1 3)) | |
305 t | |
306 (if (memq sgn '(2 4 6)) | |
307 nil | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
308 (math-looks-negp a))))) |
40785 | 309 |
310 ;;; Find the possible signs of A, assuming A is a number of some kind. | |
311 ;;; Returns an integer with bits: 1 may be negative, | |
312 ;;; 2 may be zero, | |
313 ;;; 4 may be positive, | |
314 ;;; 8 may be nonreal. | |
315 | |
316 (defun math-possible-signs (a &optional origin) | |
317 (cond ((Math-objectp a) | |
318 (if origin (setq a (math-sub a origin))) | |
319 (cond ((Math-posp a) 4) | |
320 ((Math-negp a) 1) | |
321 ((Math-zerop a) 2) | |
322 ((eq (car a) 'intv) | |
323 (cond ((Math-zerop (nth 2 a)) 6) | |
324 ((Math-zerop (nth 3 a)) 3) | |
325 (t 7))) | |
326 ((eq (car a) 'sdev) | |
327 (if (math-known-realp (nth 1 a)) 7 15)) | |
328 (t 8))) | |
329 ((memq (car a) '(+ -)) | |
330 (cond ((Math-realp (nth 1 a)) | |
331 (if (eq (car a) '-) | |
332 (math-neg-signs | |
333 (math-possible-signs (nth 2 a) | |
334 (if origin | |
335 (math-add origin (nth 1 a)) | |
336 (nth 1 a)))) | |
337 (math-possible-signs (nth 2 a) | |
338 (if origin | |
339 (math-sub origin (nth 1 a)) | |
340 (math-neg (nth 1 a)))))) | |
341 ((Math-realp (nth 2 a)) | |
342 (let ((org (if (eq (car a) '-) | |
343 (nth 2 a) | |
344 (math-neg (nth 2 a))))) | |
345 (math-possible-signs (nth 1 a) | |
346 (if origin | |
347 (math-add origin org) | |
348 org)))) | |
349 (t | |
350 (let ((s1 (math-possible-signs (nth 1 a) origin)) | |
351 (s2 (math-possible-signs (nth 2 a)))) | |
352 (if (eq (car a) '-) (setq s2 (math-neg-signs s2))) | |
353 (cond ((eq s1 s2) s1) | |
354 ((eq s1 2) s2) | |
355 ((eq s2 2) s1) | |
356 ((>= s1 8) 15) | |
357 ((>= s2 8) 15) | |
358 ((and (eq s1 4) (eq s2 6)) 4) | |
359 ((and (eq s2 4) (eq s1 6)) 4) | |
360 ((and (eq s1 1) (eq s2 3)) 1) | |
361 ((and (eq s2 1) (eq s1 3)) 1) | |
362 (t 7)))))) | |
363 ((eq (car a) 'neg) | |
364 (math-neg-signs (math-possible-signs | |
365 (nth 1 a) | |
366 (and origin (math-neg origin))))) | |
367 ((and origin (Math-zerop origin) (setq origin nil) | |
368 nil)) | |
369 ((and (or (eq (car a) '*) | |
370 (and (eq (car a) '/) origin)) | |
371 (Math-realp (nth 1 a))) | |
372 (let ((s (if (eq (car a) '*) | |
373 (if (Math-zerop (nth 1 a)) | |
374 (math-possible-signs 0 origin) | |
375 (math-possible-signs (nth 2 a) | |
376 (math-div (or origin 0) | |
377 (nth 1 a)))) | |
378 (math-neg-signs | |
379 (math-possible-signs (nth 2 a) | |
380 (math-div (nth 1 a) | |
381 origin)))))) | |
382 (if (Math-negp (nth 1 a)) (math-neg-signs s) s))) | |
383 ((and (memq (car a) '(* /)) (Math-realp (nth 2 a))) | |
384 (let ((s (math-possible-signs (nth 1 a) | |
385 (if (eq (car a) '*) | |
386 (math-mul (or origin 0) (nth 2 a)) | |
387 (math-div (or origin 0) (nth 2 a)))))) | |
388 (if (Math-negp (nth 2 a)) (math-neg-signs s) s))) | |
389 ((eq (car a) 'vec) | |
390 (let ((signs 0)) | |
391 (while (and (setq a (cdr a)) (< signs 15)) | |
392 (setq signs (logior signs (math-possible-signs | |
393 (car a) origin)))) | |
394 signs)) | |
395 (t (let ((sign | |
396 (cond | |
397 ((memq (car a) '(* /)) | |
398 (let ((s1 (math-possible-signs (nth 1 a))) | |
399 (s2 (math-possible-signs (nth 2 a)))) | |
400 (cond ((>= s1 8) 15) | |
401 ((>= s2 8) 15) | |
402 ((and (eq (car a) '/) (memq s2 '(2 3 6 7))) 15) | |
403 (t | |
404 (logior (if (memq s1 '(4 5 6 7)) s2 0) | |
405 (if (memq s1 '(2 3 6 7)) 2 0) | |
406 (if (memq s1 '(1 3 5 7)) | |
407 (math-neg-signs s2) 0)))))) | |
408 ((eq (car a) '^) | |
409 (let ((s1 (math-possible-signs (nth 1 a))) | |
410 (s2 (math-possible-signs (nth 2 a)))) | |
411 (cond ((>= s1 8) 15) | |
412 ((>= s2 8) 15) | |
413 ((eq s1 4) 4) | |
414 ((eq s1 2) (if (eq s2 4) 2 15)) | |
415 ((eq s2 2) (if (memq s1 '(1 5)) 2 15)) | |
416 ((Math-integerp (nth 2 a)) | |
417 (if (math-evenp (nth 2 a)) | |
418 (if (memq s1 '(3 6 7)) 6 4) | |
419 s1)) | |
420 ((eq s1 6) (if (eq s2 4) 6 15)) | |
421 (t 7)))) | |
422 ((eq (car a) '%) | |
423 (let ((s2 (math-possible-signs (nth 2 a)))) | |
424 (cond ((>= s2 8) 7) | |
425 ((eq s2 2) 2) | |
426 ((memq s2 '(4 6)) 6) | |
427 ((memq s2 '(1 3)) 3) | |
428 (t 7)))) | |
429 ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr)) | |
430 (= (length a) 2)) | |
431 (let ((s1 (math-possible-signs (nth 1 a)))) | |
432 (cond ((eq s1 2) 2) | |
433 ((memq s1 '(1 4 5)) 4) | |
434 (t 6)))) | |
435 ((and (eq (car a) 'calcFunc-exp) (= (length a) 2)) | |
436 (let ((s1 (math-possible-signs (nth 1 a)))) | |
437 (if (>= s1 8) | |
438 15 | |
439 (if (or (not origin) (math-negp origin)) | |
440 4 | |
441 (setq origin (math-sub (or origin 0) 1)) | |
442 (if (Math-zerop origin) (setq origin nil)) | |
443 s1)))) | |
444 ((or (and (memq (car a) '(calcFunc-ln calcFunc-log10)) | |
445 (= (length a) 2)) | |
446 (and (eq (car a) 'calcFunc-log) | |
447 (= (length a) 3) | |
448 (math-known-posp (nth 2 a)))) | |
449 (if (math-known-nonnegp (nth 1 a)) | |
450 (math-possible-signs (nth 1 a) 1) | |
451 15)) | |
452 ((and (eq (car a) 'calcFunc-sqrt) (= (length a) 2)) | |
453 (let ((s1 (math-possible-signs (nth 1 a)))) | |
454 (if (memq s1 '(2 4 6)) s1 15))) | |
455 ((memq (car a) math-nonnegative-functions) 6) | |
456 ((memq (car a) math-positive-functions) 4) | |
457 ((memq (car a) math-real-functions) 7) | |
458 ((memq (car a) math-real-scalar-functions) 7) | |
459 ((and (memq (car a) math-real-if-arg-functions) | |
460 (= (length a) 2)) | |
461 (if (math-known-realp (nth 1 a)) 7 15))))) | |
462 (cond (sign | |
463 (if origin | |
464 (+ (logand sign 8) | |
465 (if (Math-posp origin) | |
466 (if (memq sign '(1 2 3 8 9 10 11)) 1 7) | |
467 (if (memq sign '(2 4 6 8 10 12 14)) 4 7))) | |
468 sign)) | |
469 ((math-const-var a) | |
470 (cond ((eq (nth 2 a) 'var-pi) | |
471 (if origin | |
472 (math-possible-signs (math-pi) origin) | |
473 4)) | |
474 ((eq (nth 2 a) 'var-e) | |
475 (if origin | |
476 (math-possible-signs (math-e) origin) | |
477 4)) | |
478 ((eq (nth 2 a) 'var-inf) 4) | |
479 ((eq (nth 2 a) 'var-uinf) 13) | |
480 ((eq (nth 2 a) 'var-i) 8) | |
481 (t 15))) | |
482 (t | |
483 (math-setup-declarations) | |
484 (let ((decl (if (eq (car a) 'var) | |
485 (or (assq (nth 2 a) math-decls-cache) | |
486 math-decls-all) | |
487 (assq (car a) math-decls-cache)))) | |
488 (if (and origin | |
489 (memq 'int (nth 1 decl)) | |
490 (not (Math-num-integerp origin))) | |
491 5 | |
492 (if (nth 2 decl) | |
493 (math-possible-signs (nth 2 decl) origin) | |
494 (if (memq 'real (nth 1 decl)) | |
495 7 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
496 15)))))))))) |
40785 | 497 |
498 (defun math-neg-signs (s1) | |
499 (if (>= s1 8) | |
500 (+ 8 (math-neg-signs (- s1 8))) | |
501 (+ (if (memq s1 '(1 3 5 7)) 4 0) | |
502 (if (memq s1 '(2 3 6 7)) 2 0) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
503 (if (memq s1 '(4 5 6 7)) 1 0)))) |
40785 | 504 |
505 | |
506 ;;; Try to prove that A is an integer. | |
507 (defun math-known-integerp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
508 (eq (math-possible-types a) 1)) |
40785 | 509 |
510 (defun math-known-num-integerp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
511 (<= (math-possible-types a t) 3)) |
40785 | 512 |
513 (defun math-known-imagp (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
514 (= (math-possible-types a) 16)) |
40785 | 515 |
516 | |
517 ;;; Find the possible types of A. | |
518 ;;; Returns an integer with bits: 1 may be integer. | |
519 ;;; 2 may be integer-valued float. | |
520 ;;; 4 may be fraction. | |
521 ;;; 8 may be non-integer-valued float. | |
522 ;;; 16 may be imaginary. | |
523 ;;; 32 may be non-real, non-imaginary. | |
524 ;;; Real infinities count as integers for the purposes of this function. | |
525 (defun math-possible-types (a &optional num) | |
526 (cond ((Math-objectp a) | |
527 (cond ((Math-integerp a) (if num 3 1)) | |
528 ((Math-messy-integerp a) (if num 3 2)) | |
529 ((eq (car a) 'frac) (if num 12 4)) | |
530 ((eq (car a) 'float) (if num 12 8)) | |
531 ((eq (car a) 'intv) | |
532 (if (equal (nth 2 a) (nth 3 a)) | |
533 (math-possible-types (nth 2 a)) | |
534 15)) | |
535 ((eq (car a) 'sdev) | |
536 (if (math-known-realp (nth 1 a)) 15 63)) | |
537 ((eq (car a) 'cplx) | |
538 (if (math-zerop (nth 1 a)) 16 32)) | |
539 ((eq (car a) 'polar) | |
540 (if (or (Math-equal (nth 2 a) (math-quarter-circle nil)) | |
541 (Math-equal (nth 2 a) | |
542 (math-neg (math-quarter-circle nil)))) | |
543 16 48)) | |
544 (t 63))) | |
545 ((eq (car a) '/) | |
546 (let* ((t1 (math-possible-types (nth 1 a) num)) | |
547 (t2 (math-possible-types (nth 2 a) num)) | |
548 (t12 (logior t1 t2))) | |
549 (if (< t12 16) | |
550 (if (> (logand t12 10) 0) | |
551 10 | |
552 (if (or (= t1 4) (= t2 4) calc-prefer-frac) | |
553 5 | |
554 15)) | |
555 (if (< t12 32) | |
556 (if (= t1 16) | |
557 (if (= t2 16) 15 | |
558 (if (< t2 16) 16 31)) | |
559 (if (= t2 16) | |
560 (if (< t1 16) 16 31) | |
561 31)) | |
562 63)))) | |
563 ((memq (car a) '(+ - * %)) | |
564 (let* ((t1 (math-possible-types (nth 1 a) num)) | |
565 (t2 (math-possible-types (nth 2 a) num)) | |
566 (t12 (logior t1 t2))) | |
567 (if (eq (car a) '%) | |
568 (setq t1 (logand t1 15) t2 (logand t2 15) t12 (logand t12 15))) | |
569 (if (< t12 16) | |
570 (let ((mask (if (<= t12 3) | |
571 1 | |
572 (if (and (or (and (<= t1 3) (= (logand t2 3) 0)) | |
573 (and (<= t2 3) (= (logand t1 3) 0))) | |
574 (memq (car a) '(+ -))) | |
575 4 | |
576 5)))) | |
577 (if num | |
578 (* mask 3) | |
579 (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0)) | |
580 mask 0) | |
581 (if (> (logand t12 10) 0) | |
582 (* mask 2) 0)))) | |
583 (if (< t12 32) | |
584 (if (eq (car a) '*) | |
585 (if (= t1 16) | |
586 (if (= t2 16) 15 | |
587 (if (< t2 16) 16 31)) | |
588 (if (= t2 16) | |
589 (if (< t1 16) 16 31) | |
590 31)) | |
591 (if (= t12 16) 16 | |
592 (if (or (and (= t1 16) (< t2 16)) | |
593 (and (= t2 16) (< t1 16))) 32 63))) | |
594 63)))) | |
595 ((eq (car a) 'neg) | |
596 (math-possible-types (nth 1 a))) | |
597 ((eq (car a) '^) | |
598 (let* ((t1 (math-possible-types (nth 1 a) num)) | |
599 (t2 (math-possible-types (nth 2 a) num)) | |
600 (t12 (logior t1 t2))) | |
601 (if (and (<= t2 3) (math-known-nonnegp (nth 2 a)) (< t1 16)) | |
602 (let ((mask (logior (if (> (logand t1 3) 0) 1 0) | |
603 (logand t1 4) | |
604 (if (> (logand t1 12) 0) 5 0)))) | |
605 (if num | |
606 (* mask 3) | |
607 (logior (if (and (> (logand t1 5) 0) (> (logand t2 5) 0)) | |
608 mask 0) | |
609 (if (> (logand t12 10) 0) | |
610 (* mask 2) 0)))) | |
611 (if (and (math-known-nonnegp (nth 1 a)) | |
612 (math-known-posp (nth 2 a))) | |
613 15 | |
614 63)))) | |
615 ((eq (car a) 'calcFunc-sqrt) | |
616 (let ((t1 (math-possible-signs (nth 1 a)))) | |
617 (logior (if (> (logand t1 2) 0) 3 0) | |
618 (if (> (logand t1 1) 0) 16 0) | |
619 (if (> (logand t1 4) 0) 15 0) | |
620 (if (> (logand t1 8) 0) 32 0)))) | |
621 ((eq (car a) 'vec) | |
622 (let ((types 0)) | |
623 (while (and (setq a (cdr a)) (< types 63)) | |
624 (setq types (logior types (math-possible-types (car a) t)))) | |
625 types)) | |
626 ((or (memq (car a) math-integer-functions) | |
627 (and (memq (car a) math-rounding-functions) | |
628 (math-known-nonnegp (or (nth 2 a) 0)))) | |
629 1) | |
630 ((or (memq (car a) math-num-integer-functions) | |
631 (and (memq (car a) math-float-rounding-functions) | |
632 (math-known-nonnegp (or (nth 2 a) 0)))) | |
633 2) | |
634 ((eq (car a) 'calcFunc-frac) | |
635 5) | |
636 ((and (eq (car a) 'calcFunc-float) (= (length a) 2)) | |
637 (let ((t1 (math-possible-types (nth 1 a)))) | |
638 (logior (if (> (logand t1 3) 0) 2 0) | |
639 (if (> (logand t1 12) 0) 8 0) | |
640 (logand t1 48)))) | |
641 ((and (memq (car a) '(calcFunc-abs calcFunc-abssqr)) | |
642 (= (length a) 2)) | |
643 (let ((t1 (math-possible-types (nth 1 a)))) | |
644 (if (>= t1 16) | |
645 15 | |
646 t1))) | |
647 ((math-const-var a) | |
648 (cond ((memq (nth 2 a) '(var-e var-pi var-phi var-gamma)) 8) | |
649 ((eq (nth 2 a) 'var-inf) 1) | |
650 ((eq (nth 2 a) 'var-i) 16) | |
651 (t 63))) | |
652 (t | |
653 (math-setup-declarations) | |
654 (let ((decl (if (eq (car a) 'var) | |
655 (or (assq (nth 2 a) math-decls-cache) | |
656 math-decls-all) | |
657 (assq (car a) math-decls-cache)))) | |
658 (cond ((memq 'int (nth 1 decl)) | |
659 1) | |
660 ((memq 'numint (nth 1 decl)) | |
661 3) | |
662 ((memq 'frac (nth 1 decl)) | |
663 4) | |
664 ((memq 'rat (nth 1 decl)) | |
665 5) | |
666 ((memq 'float (nth 1 decl)) | |
667 10) | |
668 ((nth 2 decl) | |
669 (math-possible-types (nth 2 decl))) | |
670 ((memq 'real (nth 1 decl)) | |
671 15) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
672 (t 63)))))) |
40785 | 673 |
674 (defun math-known-evenp (a) | |
675 (cond ((Math-integerp a) | |
676 (math-evenp a)) | |
677 ((Math-messy-integerp a) | |
678 (or (> (nth 2 a) 0) | |
679 (math-evenp (math-trunc a)))) | |
680 ((eq (car a) '*) | |
681 (if (math-known-evenp (nth 1 a)) | |
682 (math-known-num-integerp (nth 2 a)) | |
683 (if (math-known-num-integerp (nth 1 a)) | |
684 (math-known-evenp (nth 2 a))))) | |
685 ((memq (car a) '(+ -)) | |
686 (or (and (math-known-evenp (nth 1 a)) | |
687 (math-known-evenp (nth 2 a))) | |
688 (and (math-known-oddp (nth 1 a)) | |
689 (math-known-oddp (nth 2 a))))) | |
690 ((eq (car a) 'neg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
691 (math-known-evenp (nth 1 a))))) |
40785 | 692 |
693 (defun math-known-oddp (a) | |
694 (cond ((Math-integerp a) | |
695 (math-oddp a)) | |
696 ((Math-messy-integerp a) | |
697 (and (<= (nth 2 a) 0) | |
698 (math-oddp (math-trunc a)))) | |
699 ((memq (car a) '(+ -)) | |
700 (or (and (math-known-evenp (nth 1 a)) | |
701 (math-known-oddp (nth 2 a))) | |
702 (and (math-known-oddp (nth 1 a)) | |
703 (math-known-evenp (nth 2 a))))) | |
704 ((eq (car a) 'neg) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
705 (math-known-oddp (nth 1 a))))) |
40785 | 706 |
707 | |
708 (defun calcFunc-dreal (expr) | |
709 (let ((types (math-possible-types expr))) | |
710 (if (< types 16) 1 | |
711 (if (= (logand types 15) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
712 (math-reject-arg expr 'realp 'quiet))))) |
40785 | 713 |
714 (defun calcFunc-dimag (expr) | |
715 (let ((types (math-possible-types expr))) | |
716 (if (= types 16) 1 | |
717 (if (= (logand types 16) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
718 (math-reject-arg expr "Expected an imaginary number"))))) |
40785 | 719 |
720 (defun calcFunc-dpos (expr) | |
721 (let ((signs (math-possible-signs expr))) | |
722 (if (eq signs 4) 1 | |
723 (if (memq signs '(1 2 3)) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
724 (math-reject-arg expr 'posp 'quiet))))) |
40785 | 725 |
726 (defun calcFunc-dneg (expr) | |
727 (let ((signs (math-possible-signs expr))) | |
728 (if (eq signs 1) 1 | |
729 (if (memq signs '(2 4 6)) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
730 (math-reject-arg expr 'negp 'quiet))))) |
40785 | 731 |
732 (defun calcFunc-dnonneg (expr) | |
733 (let ((signs (math-possible-signs expr))) | |
734 (if (memq signs '(2 4 6)) 1 | |
735 (if (eq signs 1) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
736 (math-reject-arg expr 'posp 'quiet))))) |
40785 | 737 |
738 (defun calcFunc-dnonzero (expr) | |
739 (let ((signs (math-possible-signs expr))) | |
740 (if (memq signs '(1 4 5 8 9 12 13)) 1 | |
741 (if (eq signs 2) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
742 (math-reject-arg expr 'nonzerop 'quiet))))) |
40785 | 743 |
744 (defun calcFunc-dint (expr) | |
745 (let ((types (math-possible-types expr))) | |
746 (if (= types 1) 1 | |
747 (if (= (logand types 1) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
748 (math-reject-arg expr 'integerp 'quiet))))) |
40785 | 749 |
750 (defun calcFunc-dnumint (expr) | |
751 (let ((types (math-possible-types expr t))) | |
752 (if (<= types 3) 1 | |
753 (if (= (logand types 3) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
754 (math-reject-arg expr 'integerp 'quiet))))) |
40785 | 755 |
756 (defun calcFunc-dnatnum (expr) | |
757 (let ((res (calcFunc-dint expr))) | |
758 (if (eq res 1) | |
759 (calcFunc-dnonneg expr) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
760 res))) |
40785 | 761 |
762 (defun calcFunc-deven (expr) | |
763 (if (math-known-evenp expr) | |
764 1 | |
765 (if (or (math-known-oddp expr) | |
766 (= (logand (math-possible-types expr) 3) 0)) | |
767 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
768 (math-reject-arg expr "Can't tell if expression is odd or even")))) |
40785 | 769 |
770 (defun calcFunc-dodd (expr) | |
771 (if (math-known-oddp expr) | |
772 1 | |
773 (if (or (math-known-evenp expr) | |
774 (= (logand (math-possible-types expr) 3) 0)) | |
775 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
776 (math-reject-arg expr "Can't tell if expression is odd or even")))) |
40785 | 777 |
778 (defun calcFunc-drat (expr) | |
779 (let ((types (math-possible-types expr))) | |
780 (if (memq types '(1 4 5)) 1 | |
781 (if (= (logand types 5) 0) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
782 (math-reject-arg expr "Rational number expected"))))) |
40785 | 783 |
784 (defun calcFunc-drange (expr) | |
785 (math-setup-declarations) | |
786 (let (range) | |
787 (if (Math-realp expr) | |
788 (list 'vec expr) | |
789 (if (eq (car-safe expr) 'intv) | |
790 expr | |
791 (if (eq (car-safe expr) 'var) | |
792 (setq range (nth 2 (or (assq (nth 2 expr) math-decls-cache) | |
793 math-decls-all))) | |
794 (setq range (nth 2 (assq (car-safe expr) math-decls-cache)))) | |
795 (if range | |
796 (math-clean-set (copy-sequence range)) | |
797 (setq range (math-possible-signs expr)) | |
798 (if (< range 8) | |
799 (aref [(vec) | |
800 (intv 2 (neg (var inf var-inf)) 0) | |
801 (vec 0) | |
802 (intv 3 (neg (var inf var-inf)) 0) | |
803 (intv 1 0 (var inf var-inf)) | |
804 (vec (intv 2 (neg (var inf var-inf)) 0) | |
805 (intv 1 0 (var inf var-inf))) | |
806 (intv 3 0 (var inf var-inf)) | |
807 (intv 3 (neg (var inf var-inf)) (var inf var-inf))] range) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
808 (math-reject-arg expr 'realp 'quiet))))))) |
40785 | 809 |
810 (defun calcFunc-dscalar (a) | |
811 (if (math-known-scalarp a) 1 | |
812 (if (math-known-matrixp a) 0 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
813 (math-reject-arg a 'objectp 'quiet)))) |
40785 | 814 |
815 | |
816 ;;; The following lists are not exhaustive. | |
817 (defvar math-scalar-functions '(calcFunc-det | |
818 calcFunc-cnorm calcFunc-rnorm | |
819 calcFunc-vlen calcFunc-vcount | |
820 calcFunc-vsum calcFunc-vprod | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
821 calcFunc-vmin calcFunc-vmax)) |
40785 | 822 |
823 (defvar math-nonscalar-functions '(vec calcFunc-idn calcFunc-diag | |
824 calcFunc-cvec calcFunc-index | |
825 calcFunc-trn | |
826 | calcFunc-append | |
827 calcFunc-cons calcFunc-rcons | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
828 calcFunc-tail calcFunc-rhead)) |
40785 | 829 |
830 (defvar math-scalar-if-args-functions '(+ - * / neg)) | |
831 | |
832 (defvar math-real-functions '(calcFunc-arg | |
833 calcFunc-re calcFunc-im | |
834 calcFunc-floor calcFunc-ceil | |
835 calcFunc-trunc calcFunc-round | |
836 calcFunc-rounde calcFunc-roundu | |
837 calcFunc-ffloor calcFunc-fceil | |
838 calcFunc-ftrunc calcFunc-fround | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
839 calcFunc-frounde calcFunc-froundu)) |
40785 | 840 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
841 (defvar math-positive-functions '()) |
40785 | 842 |
843 (defvar math-nonnegative-functions '(calcFunc-cnorm calcFunc-rnorm | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
844 calcFunc-vlen calcFunc-vcount)) |
40785 | 845 |
846 (defvar math-real-scalar-functions '(% calcFunc-idiv calcFunc-abs | |
847 calcFunc-choose calcFunc-perm | |
848 calcFunc-eq calcFunc-neq | |
849 calcFunc-lt calcFunc-gt | |
850 calcFunc-leq calcFunc-geq | |
851 calcFunc-lnot | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
852 calcFunc-max calcFunc-min)) |
40785 | 853 |
854 (defvar math-real-if-arg-functions '(calcFunc-sin calcFunc-cos | |
855 calcFunc-tan calcFunc-arctan | |
856 calcFunc-sinh calcFunc-cosh | |
857 calcFunc-tanh calcFunc-exp | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
858 calcFunc-gamma calcFunc-fact)) |
40785 | 859 |
860 (defvar math-integer-functions '(calcFunc-idiv | |
861 calcFunc-isqrt calcFunc-ilog | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
862 calcFunc-vlen calcFunc-vcount)) |
40785 | 863 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
864 (defvar math-num-integer-functions '()) |
40785 | 865 |
866 (defvar math-rounding-functions '(calcFunc-floor | |
867 calcFunc-ceil | |
868 calcFunc-round calcFunc-trunc | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
869 calcFunc-rounde calcFunc-roundu)) |
40785 | 870 |
871 (defvar math-float-rounding-functions '(calcFunc-ffloor | |
872 calcFunc-fceil | |
873 calcFunc-fround calcFunc-ftrunc | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
874 calcFunc-frounde calcFunc-froundu)) |
40785 | 875 |
876 (defvar math-integer-if-args-functions '(+ - * % neg calcFunc-abs | |
877 calcFunc-min calcFunc-max | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
878 calcFunc-choose calcFunc-perm)) |
40785 | 879 |
880 | |
881 ;;;; Arithmetic. | |
882 | |
883 (defun calcFunc-neg (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
884 (math-normalize (list 'neg a))) |
40785 | 885 |
886 (defun math-neg-fancy (a) | |
887 (cond ((eq (car a) 'polar) | |
888 (list 'polar | |
889 (nth 1 a) | |
890 (if (math-posp (nth 2 a)) | |
891 (math-sub (nth 2 a) (math-half-circle nil)) | |
892 (math-add (nth 2 a) (math-half-circle nil))))) | |
893 ((eq (car a) 'mod) | |
894 (if (math-zerop (nth 1 a)) | |
895 a | |
896 (list 'mod (math-sub (nth 2 a) (nth 1 a)) (nth 2 a)))) | |
897 ((eq (car a) 'sdev) | |
898 (list 'sdev (math-neg (nth 1 a)) (nth 2 a))) | |
899 ((eq (car a) 'intv) | |
900 (math-make-intv (aref [0 2 1 3] (nth 1 a)) | |
901 (math-neg (nth 3 a)) | |
902 (math-neg (nth 2 a)))) | |
903 ((and math-simplify-only | |
904 (not (equal a math-simplify-only))) | |
905 (list 'neg a)) | |
906 ((eq (car a) '+) | |
907 (math-sub (math-neg (nth 1 a)) (nth 2 a))) | |
908 ((eq (car a) '-) | |
909 (math-sub (nth 2 a) (nth 1 a))) | |
910 ((and (memq (car a) '(* /)) | |
911 (math-okay-neg (nth 1 a))) | |
912 (list (car a) (math-neg (nth 1 a)) (nth 2 a))) | |
913 ((and (memq (car a) '(* /)) | |
914 (math-okay-neg (nth 2 a))) | |
915 (list (car a) (nth 1 a) (math-neg (nth 2 a)))) | |
916 ((and (memq (car a) '(* /)) | |
917 (or (math-objectp (nth 1 a)) | |
918 (and (eq (car (nth 1 a)) '*) | |
919 (math-objectp (nth 1 (nth 1 a)))))) | |
920 (list (car a) (math-neg (nth 1 a)) (nth 2 a))) | |
921 ((and (eq (car a) '/) | |
922 (or (math-objectp (nth 2 a)) | |
923 (and (eq (car (nth 2 a)) '*) | |
924 (math-objectp (nth 1 (nth 2 a)))))) | |
925 (list (car a) (nth 1 a) (math-neg (nth 2 a)))) | |
926 ((and (eq (car a) 'var) (memq (nth 2 a) '(var-uinf var-nan))) | |
927 a) | |
928 ((eq (car a) 'neg) | |
929 (nth 1 a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
930 (t (list 'neg a)))) |
40785 | 931 |
932 (defun math-okay-neg (a) | |
933 (or (math-looks-negp a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
934 (eq (car-safe a) '-))) |
40785 | 935 |
936 (defun math-neg-float (a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
937 (list 'float (Math-integer-neg (nth 1 a)) (nth 2 a))) |
40785 | 938 |
939 | |
940 (defun calcFunc-add (&rest rest) | |
941 (if rest | |
942 (let ((a (car rest))) | |
943 (while (setq rest (cdr rest)) | |
944 (setq a (list '+ a (car rest)))) | |
945 (math-normalize a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
946 0)) |
40785 | 947 |
948 (defun calcFunc-sub (&rest rest) | |
949 (if rest | |
950 (let ((a (car rest))) | |
951 (while (setq rest (cdr rest)) | |
952 (setq a (list '- a (car rest)))) | |
953 (math-normalize a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
954 0)) |
40785 | 955 |
956 (defun math-add-objects-fancy (a b) | |
957 (cond ((and (Math-numberp a) (Math-numberp b)) | |
958 (let ((aa (math-complex a)) | |
959 (bb (math-complex b))) | |
960 (math-normalize | |
961 (let ((res (list 'cplx | |
962 (math-add (nth 1 aa) (nth 1 bb)) | |
963 (math-add (nth 2 aa) (nth 2 bb))))) | |
964 (if (math-want-polar a b) | |
965 (math-polar res) | |
966 res))))) | |
967 ((or (Math-vectorp a) (Math-vectorp b)) | |
968 (math-map-vec-2 'math-add a b)) | |
969 ((eq (car-safe a) 'sdev) | |
970 (if (eq (car-safe b) 'sdev) | |
971 (math-make-sdev (math-add (nth 1 a) (nth 1 b)) | |
972 (math-hypot (nth 2 a) (nth 2 b))) | |
973 (and (or (Math-scalarp b) | |
974 (not (Math-objvecp b))) | |
975 (math-make-sdev (math-add (nth 1 a) b) (nth 2 a))))) | |
976 ((and (eq (car-safe b) 'sdev) | |
977 (or (Math-scalarp a) | |
978 (not (Math-objvecp a)))) | |
979 (math-make-sdev (math-add a (nth 1 b)) (nth 2 b))) | |
980 ((eq (car-safe a) 'intv) | |
981 (if (eq (car-safe b) 'intv) | |
982 (math-make-intv (logior (logand (nth 1 a) (nth 1 b)) | |
983 (if (equal (nth 2 a) | |
984 '(neg (var inf var-inf))) | |
985 (logand (nth 1 a) 2) 0) | |
986 (if (equal (nth 2 b) | |
987 '(neg (var inf var-inf))) | |
988 (logand (nth 1 b) 2) 0) | |
989 (if (equal (nth 3 a) '(var inf var-inf)) | |
990 (logand (nth 1 a) 1) 0) | |
991 (if (equal (nth 3 b) '(var inf var-inf)) | |
992 (logand (nth 1 b) 1) 0)) | |
993 (math-add (nth 2 a) (nth 2 b)) | |
994 (math-add (nth 3 a) (nth 3 b))) | |
995 (and (or (Math-anglep b) | |
996 (eq (car b) 'date) | |
997 (not (Math-objvecp b))) | |
998 (math-make-intv (nth 1 a) | |
999 (math-add (nth 2 a) b) | |
1000 (math-add (nth 3 a) b))))) | |
1001 ((and (eq (car-safe b) 'intv) | |
1002 (or (Math-anglep a) | |
1003 (eq (car a) 'date) | |
1004 (not (Math-objvecp a)))) | |
1005 (math-make-intv (nth 1 b) | |
1006 (math-add a (nth 2 b)) | |
1007 (math-add a (nth 3 b)))) | |
1008 ((eq (car-safe a) 'date) | |
1009 (cond ((eq (car-safe b) 'date) | |
1010 (math-add (nth 1 a) (nth 1 b))) | |
1011 ((eq (car-safe b) 'hms) | |
1012 (let ((parts (math-date-parts (nth 1 a)))) | |
1013 (list 'date | |
1014 (math-add (car parts) ; this minimizes roundoff | |
1015 (math-div (math-add | |
1016 (math-add (nth 1 parts) | |
1017 (nth 2 parts)) | |
1018 (math-add | |
1019 (math-mul (nth 1 b) 3600) | |
1020 (math-add (math-mul (nth 2 b) 60) | |
1021 (nth 3 b)))) | |
1022 86400))))) | |
1023 ((Math-realp b) | |
1024 (list 'date (math-add (nth 1 a) b))) | |
1025 (t nil))) | |
1026 ((eq (car-safe b) 'date) | |
1027 (math-add-objects-fancy b a)) | |
1028 ((and (eq (car-safe a) 'mod) | |
1029 (eq (car-safe b) 'mod) | |
1030 (equal (nth 2 a) (nth 2 b))) | |
1031 (math-make-mod (math-add (nth 1 a) (nth 1 b)) (nth 2 a))) | |
1032 ((and (eq (car-safe a) 'mod) | |
1033 (Math-anglep b)) | |
1034 (math-make-mod (math-add (nth 1 a) b) (nth 2 a))) | |
1035 ((and (eq (car-safe b) 'mod) | |
1036 (Math-anglep a)) | |
1037 (math-make-mod (math-add a (nth 1 b)) (nth 2 b))) | |
1038 ((and (or (eq (car-safe a) 'hms) (eq (car-safe b) 'hms)) | |
1039 (and (Math-anglep a) (Math-anglep b))) | |
1040 (or (eq (car-safe a) 'hms) (setq a (math-to-hms a))) | |
1041 (or (eq (car-safe b) 'hms) (setq b (math-to-hms b))) | |
1042 (math-normalize | |
1043 (if (math-negp a) | |
1044 (math-neg (math-add (math-neg a) (math-neg b))) | |
1045 (if (math-negp b) | |
1046 (let* ((s (math-add (nth 3 a) (nth 3 b))) | |
1047 (m (math-add (nth 2 a) (nth 2 b))) | |
1048 (h (math-add (nth 1 a) (nth 1 b)))) | |
1049 (if (math-negp s) | |
1050 (setq s (math-add s 60) | |
1051 m (math-add m -1))) | |
1052 (if (math-negp m) | |
1053 (setq m (math-add m 60) | |
1054 h (math-add h -1))) | |
1055 (if (math-negp h) | |
1056 (math-add b a) | |
1057 (list 'hms h m s))) | |
1058 (let* ((s (math-add (nth 3 a) (nth 3 b))) | |
1059 (m (math-add (nth 2 a) (nth 2 b))) | |
1060 (h (math-add (nth 1 a) (nth 1 b)))) | |
1061 (list 'hms h m s)))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1062 (t (calc-record-why "*Incompatible arguments for +" a b)))) |
40785 | 1063 |
1064 (defun math-add-symb-fancy (a b) | |
1065 (or (and math-simplify-only | |
1066 (not (equal a math-simplify-only)) | |
1067 (list '+ a b)) | |
1068 (and (eq (car-safe b) '+) | |
1069 (math-add (math-add a (nth 1 b)) | |
1070 (nth 2 b))) | |
1071 (and (eq (car-safe b) '-) | |
1072 (math-sub (math-add a (nth 1 b)) | |
1073 (nth 2 b))) | |
1074 (and (eq (car-safe b) 'neg) | |
1075 (eq (car-safe (nth 1 b)) '+) | |
1076 (math-sub (math-sub a (nth 1 (nth 1 b))) | |
1077 (nth 2 (nth 1 b)))) | |
1078 (and (or (and (Math-vectorp a) (math-known-scalarp b)) | |
1079 (and (Math-vectorp b) (math-known-scalarp a))) | |
1080 (math-map-vec-2 'math-add a b)) | |
1081 (let ((inf (math-infinitep a))) | |
1082 (cond | |
1083 (inf | |
1084 (let ((inf2 (math-infinitep b))) | |
1085 (if inf2 | |
1086 (if (or (memq (nth 2 inf) '(var-uinf var-nan)) | |
1087 (memq (nth 2 inf2) '(var-uinf var-nan))) | |
1088 '(var nan var-nan) | |
1089 (let ((dir (math-infinite-dir a inf)) | |
1090 (dir2 (math-infinite-dir b inf2))) | |
1091 (if (and (Math-objectp dir) (Math-objectp dir2)) | |
1092 (if (Math-equal dir dir2) | |
1093 a | |
1094 '(var nan var-nan))))) | |
1095 (if (and (equal a '(var inf var-inf)) | |
1096 (eq (car-safe b) 'intv) | |
1097 (memq (nth 1 b) '(2 3)) | |
1098 (equal (nth 2 b) '(neg (var inf var-inf)))) | |
1099 (list 'intv 3 (nth 2 b) a) | |
1100 (if (and (equal a '(neg (var inf var-inf))) | |
1101 (eq (car-safe b) 'intv) | |
1102 (memq (nth 1 b) '(1 3)) | |
1103 (equal (nth 3 b) '(var inf var-inf))) | |
1104 (list 'intv 3 a (nth 3 b)) | |
1105 a))))) | |
1106 ((math-infinitep b) | |
1107 (if (eq (car-safe a) 'intv) | |
1108 (math-add b a) | |
1109 b)) | |
1110 ((eq (car-safe a) '+) | |
1111 (let ((temp (math-combine-sum (nth 2 a) b nil nil t))) | |
1112 (and temp | |
1113 (math-add (nth 1 a) temp)))) | |
1114 ((eq (car-safe a) '-) | |
1115 (let ((temp (math-combine-sum (nth 2 a) b t nil t))) | |
1116 (and temp | |
1117 (math-add (nth 1 a) temp)))) | |
1118 ((and (Math-objectp a) (Math-objectp b)) | |
1119 nil) | |
1120 (t | |
1121 (math-combine-sum a b nil nil nil)))) | |
1122 (and (Math-looks-negp b) | |
1123 (list '- a (math-neg b))) | |
1124 (and (Math-looks-negp a) | |
1125 (list '- b (math-neg a))) | |
1126 (and (eq (car-safe a) 'calcFunc-idn) | |
1127 (= (length a) 2) | |
1128 (or (and (eq (car-safe b) 'calcFunc-idn) | |
1129 (= (length b) 2) | |
1130 (list 'calcFunc-idn (math-add (nth 1 a) (nth 1 b)))) | |
1131 (and (math-square-matrixp b) | |
1132 (math-add (math-mimic-ident (nth 1 a) b) b)) | |
1133 (and (math-known-scalarp b) | |
1134 (math-add (nth 1 a) b)))) | |
1135 (and (eq (car-safe b) 'calcFunc-idn) | |
1136 (= (length a) 2) | |
1137 (or (and (math-square-matrixp a) | |
1138 (math-add a (math-mimic-ident (nth 1 b) a))) | |
1139 (and (math-known-scalarp a) | |
1140 (math-add a (nth 1 b))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1141 (list '+ a b))) |
40785 | 1142 |
1143 | |
1144 (defun calcFunc-mul (&rest rest) | |
1145 (if rest | |
1146 (let ((a (car rest))) | |
1147 (while (setq rest (cdr rest)) | |
1148 (setq a (list '* a (car rest)))) | |
1149 (math-normalize a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1150 1)) |
40785 | 1151 |
1152 (defun math-mul-objects-fancy (a b) | |
1153 (cond ((and (Math-numberp a) (Math-numberp b)) | |
1154 (math-normalize | |
1155 (if (math-want-polar a b) | |
1156 (let ((a (math-polar a)) | |
1157 (b (math-polar b))) | |
1158 (list 'polar | |
1159 (math-mul (nth 1 a) (nth 1 b)) | |
1160 (math-fix-circular (math-add (nth 2 a) (nth 2 b))))) | |
1161 (setq a (math-complex a) | |
1162 b (math-complex b)) | |
1163 (list 'cplx | |
1164 (math-sub (math-mul (nth 1 a) (nth 1 b)) | |
1165 (math-mul (nth 2 a) (nth 2 b))) | |
1166 (math-add (math-mul (nth 1 a) (nth 2 b)) | |
1167 (math-mul (nth 2 a) (nth 1 b))))))) | |
1168 ((Math-vectorp a) | |
1169 (if (Math-vectorp b) | |
1170 (if (math-matrixp a) | |
1171 (if (math-matrixp b) | |
1172 (if (= (length (nth 1 a)) (length b)) | |
1173 (math-mul-mats a b) | |
1174 (math-dimension-error)) | |
1175 (if (= (length (nth 1 a)) 2) | |
1176 (if (= (length a) (length b)) | |
1177 (math-mul-mats a (list 'vec b)) | |
1178 (math-dimension-error)) | |
1179 (if (= (length (nth 1 a)) (length b)) | |
1180 (math-mul-mat-vec a b) | |
1181 (math-dimension-error)))) | |
1182 (if (math-matrixp b) | |
1183 (if (= (length a) (length b)) | |
1184 (nth 1 (math-mul-mats (list 'vec a) b)) | |
1185 (math-dimension-error)) | |
1186 (if (= (length a) (length b)) | |
1187 (math-dot-product a b) | |
1188 (math-dimension-error)))) | |
1189 (math-map-vec-2 'math-mul a b))) | |
1190 ((Math-vectorp b) | |
1191 (math-map-vec-2 'math-mul a b)) | |
1192 ((eq (car-safe a) 'sdev) | |
1193 (if (eq (car-safe b) 'sdev) | |
1194 (math-make-sdev (math-mul (nth 1 a) (nth 1 b)) | |
1195 (math-hypot (math-mul (nth 2 a) (nth 1 b)) | |
1196 (math-mul (nth 2 b) (nth 1 a)))) | |
1197 (and (or (Math-scalarp b) | |
1198 (not (Math-objvecp b))) | |
1199 (math-make-sdev (math-mul (nth 1 a) b) | |
1200 (math-mul (nth 2 a) b))))) | |
1201 ((and (eq (car-safe b) 'sdev) | |
1202 (or (Math-scalarp a) | |
1203 (not (Math-objvecp a)))) | |
1204 (math-make-sdev (math-mul a (nth 1 b)) (math-mul a (nth 2 b)))) | |
1205 ((and (eq (car-safe a) 'intv) (Math-anglep b)) | |
1206 (if (Math-negp b) | |
1207 (math-neg (math-mul a (math-neg b))) | |
1208 (math-make-intv (nth 1 a) | |
1209 (math-mul (nth 2 a) b) | |
1210 (math-mul (nth 3 a) b)))) | |
1211 ((and (eq (car-safe b) 'intv) (Math-anglep a)) | |
1212 (math-mul b a)) | |
1213 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1214 (eq (car-safe b) 'intv) (math-intv-constp b)) | |
1215 (let ((lo (math-mul a (nth 2 b))) | |
1216 (hi (math-mul a (nth 3 b)))) | |
1217 (or (eq (car-safe lo) 'intv) | |
1218 (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo))) | |
1219 (or (eq (car-safe hi) 'intv) | |
1220 (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi))) | |
1221 (math-combine-intervals | |
1222 (nth 2 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1223 (math-infinitep (nth 2 lo))) | |
1224 (memq (nth 1 lo) '(2 3))) | |
1225 (nth 3 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1226 (math-infinitep (nth 3 lo))) | |
1227 (memq (nth 1 lo) '(1 3))) | |
1228 (nth 2 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1229 (math-infinitep (nth 2 hi))) | |
1230 (memq (nth 1 hi) '(2 3))) | |
1231 (nth 3 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1232 (math-infinitep (nth 3 hi))) | |
1233 (memq (nth 1 hi) '(1 3)))))) | |
1234 ((and (eq (car-safe a) 'mod) | |
1235 (eq (car-safe b) 'mod) | |
1236 (equal (nth 2 a) (nth 2 b))) | |
1237 (math-make-mod (math-mul (nth 1 a) (nth 1 b)) (nth 2 a))) | |
1238 ((and (eq (car-safe a) 'mod) | |
1239 (Math-anglep b)) | |
1240 (math-make-mod (math-mul (nth 1 a) b) (nth 2 a))) | |
1241 ((and (eq (car-safe b) 'mod) | |
1242 (Math-anglep a)) | |
1243 (math-make-mod (math-mul a (nth 1 b)) (nth 2 b))) | |
1244 ((and (eq (car-safe a) 'hms) (Math-realp b)) | |
1245 (math-with-extra-prec 2 | |
1246 (math-to-hms (math-mul (math-from-hms a 'deg) b) 'deg))) | |
1247 ((and (eq (car-safe b) 'hms) (Math-realp a)) | |
1248 (math-mul b a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1249 (t (calc-record-why "*Incompatible arguments for *" a b)))) |
40785 | 1250 |
1251 ;;; Fast function to multiply floating-point numbers. | |
1252 (defun math-mul-float (a b) ; [F F F] | |
1253 (math-make-float (math-mul (nth 1 a) (nth 1 b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1254 (+ (nth 2 a) (nth 2 b)))) |
40785 | 1255 |
1256 (defun math-sqr-float (a) ; [F F] | |
1257 (math-make-float (math-mul (nth 1 a) (nth 1 a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1258 (+ (nth 2 a) (nth 2 a)))) |
40785 | 1259 |
1260 (defun math-intv-constp (a &optional finite) | |
1261 (and (or (Math-anglep (nth 2 a)) | |
1262 (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
1263 (or (not finite) | |
1264 (memq (nth 1 a) '(0 1))))) | |
1265 (or (Math-anglep (nth 3 a)) | |
1266 (and (equal (nth 3 a) '(var inf var-inf)) | |
1267 (or (not finite) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1268 (memq (nth 1 a) '(0 2))))))) |
40785 | 1269 |
1270 (defun math-mul-zero (a b) | |
1271 (if (math-known-matrixp b) | |
1272 (if (math-vectorp b) | |
1273 (math-map-vec-2 'math-mul a b) | |
1274 (math-mimic-ident 0 b)) | |
1275 (if (math-infinitep b) | |
1276 '(var nan var-nan) | |
1277 (let ((aa nil) (bb nil)) | |
1278 (if (and (eq (car-safe b) 'intv) | |
1279 (progn | |
1280 (and (equal (nth 2 b) '(neg (var inf var-inf))) | |
1281 (memq (nth 1 b) '(2 3)) | |
1282 (setq aa (nth 2 b))) | |
1283 (and (equal (nth 3 b) '(var inf var-inf)) | |
1284 (memq (nth 1 b) '(1 3)) | |
1285 (setq bb (nth 3 b))) | |
1286 (or aa bb))) | |
1287 (if (or (math-posp a) | |
1288 (and (math-zerop a) | |
1289 (or (memq calc-infinite-mode '(-1 1)) | |
1290 (setq aa '(neg (var inf var-inf)) | |
1291 bb '(var inf var-inf))))) | |
1292 (list 'intv 3 (or aa 0) (or bb 0)) | |
1293 (if (math-negp a) | |
1294 (math-neg (list 'intv 3 (or aa 0) (or bb 0))) | |
1295 '(var nan var-nan))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1296 (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0)))))) |
40785 | 1297 |
1298 | |
1299 (defun math-mul-symb-fancy (a b) | |
1300 (or (and math-simplify-only | |
1301 (not (equal a math-simplify-only)) | |
1302 (list '* a b)) | |
1303 (and (Math-equal-int a 1) | |
1304 b) | |
1305 (and (Math-equal-int a -1) | |
1306 (math-neg b)) | |
1307 (and (or (and (Math-vectorp a) (math-known-scalarp b)) | |
1308 (and (Math-vectorp b) (math-known-scalarp a))) | |
1309 (math-map-vec-2 'math-mul a b)) | |
1310 (and (Math-objectp b) (not (Math-objectp a)) | |
1311 (math-mul b a)) | |
1312 (and (eq (car-safe a) 'neg) | |
1313 (math-neg (math-mul (nth 1 a) b))) | |
1314 (and (eq (car-safe b) 'neg) | |
1315 (math-neg (math-mul a (nth 1 b)))) | |
1316 (and (eq (car-safe a) '*) | |
1317 (math-mul (nth 1 a) | |
1318 (math-mul (nth 2 a) b))) | |
1319 (and (eq (car-safe a) '^) | |
1320 (Math-looks-negp (nth 2 a)) | |
1321 (not (and (eq (car-safe b) '^) (Math-looks-negp (nth 2 b)))) | |
1322 (math-known-scalarp b t) | |
1323 (math-div b (math-normalize | |
1324 (list '^ (nth 1 a) (math-neg (nth 2 a)))))) | |
1325 (and (eq (car-safe b) '^) | |
1326 (Math-looks-negp (nth 2 b)) | |
1327 (not (and (eq (car-safe a) '^) (Math-looks-negp (nth 2 a)))) | |
1328 (math-div a (math-normalize | |
1329 (list '^ (nth 1 b) (math-neg (nth 2 b)))))) | |
1330 (and (eq (car-safe a) '/) | |
1331 (or (math-known-scalarp a t) (math-known-scalarp b t)) | |
1332 (let ((temp (math-combine-prod (nth 2 a) b t nil t))) | |
1333 (if temp | |
1334 (math-mul (nth 1 a) temp) | |
1335 (math-div (math-mul (nth 1 a) b) (nth 2 a))))) | |
1336 (and (eq (car-safe b) '/) | |
1337 (math-div (math-mul a (nth 1 b)) (nth 2 b))) | |
1338 (and (eq (car-safe b) '+) | |
1339 (Math-numberp a) | |
1340 (or (Math-numberp (nth 1 b)) | |
1341 (Math-numberp (nth 2 b))) | |
1342 (math-add (math-mul a (nth 1 b)) | |
1343 (math-mul a (nth 2 b)))) | |
1344 (and (eq (car-safe b) '-) | |
1345 (Math-numberp a) | |
1346 (or (Math-numberp (nth 1 b)) | |
1347 (Math-numberp (nth 2 b))) | |
1348 (math-sub (math-mul a (nth 1 b)) | |
1349 (math-mul a (nth 2 b)))) | |
1350 (and (eq (car-safe b) '*) | |
1351 (Math-numberp (nth 1 b)) | |
1352 (not (Math-numberp a)) | |
1353 (math-mul (nth 1 b) (math-mul a (nth 2 b)))) | |
1354 (and (eq (car-safe a) 'calcFunc-idn) | |
1355 (= (length a) 2) | |
1356 (or (and (eq (car-safe b) 'calcFunc-idn) | |
1357 (= (length b) 2) | |
1358 (list 'calcFunc-idn (math-mul (nth 1 a) (nth 1 b)))) | |
1359 (and (math-known-scalarp b) | |
1360 (list 'calcFunc-idn (math-mul (nth 1 a) b))) | |
1361 (and (math-known-matrixp b) | |
1362 (math-mul (nth 1 a) b)))) | |
1363 (and (eq (car-safe b) 'calcFunc-idn) | |
1364 (= (length b) 2) | |
1365 (or (and (math-known-scalarp a) | |
1366 (list 'calcFunc-idn (math-mul a (nth 1 b)))) | |
1367 (and (math-known-matrixp a) | |
1368 (math-mul a (nth 1 b))))) | |
1369 (and (math-looks-negp b) | |
1370 (math-mul (math-neg a) (math-neg b))) | |
1371 (and (eq (car-safe b) '-) | |
1372 (math-looks-negp a) | |
1373 (math-mul (math-neg a) (math-neg b))) | |
1374 (cond | |
1375 ((eq (car-safe b) '*) | |
1376 (let ((temp (math-combine-prod a (nth 1 b) nil nil t))) | |
1377 (and temp | |
1378 (math-mul temp (nth 2 b))))) | |
1379 (t | |
1380 (math-combine-prod a b nil nil nil))) | |
1381 (and (equal a '(var nan var-nan)) | |
1382 a) | |
1383 (and (equal b '(var nan var-nan)) | |
1384 b) | |
1385 (and (equal a '(var uinf var-uinf)) | |
1386 a) | |
1387 (and (equal b '(var uinf var-uinf)) | |
1388 b) | |
1389 (and (equal b '(var inf var-inf)) | |
1390 (let ((s1 (math-possible-signs a))) | |
1391 (cond ((eq s1 4) | |
1392 b) | |
1393 ((eq s1 6) | |
1394 '(intv 3 0 (var inf var-inf))) | |
1395 ((eq s1 1) | |
1396 (math-neg b)) | |
1397 ((eq s1 3) | |
1398 '(intv 3 (neg (var inf var-inf)) 0)) | |
1399 ((and (eq (car a) 'intv) (math-intv-constp a)) | |
1400 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))) | |
1401 ((and (eq (car a) 'cplx) | |
1402 (math-zerop (nth 1 a))) | |
1403 (list '* (list 'cplx 0 (calcFunc-sign (nth 2 a))) b)) | |
1404 ((eq (car a) 'polar) | |
1405 (list '* (list 'polar 1 (nth 2 a)) b))))) | |
1406 (and (equal a '(var inf var-inf)) | |
1407 (math-mul b a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1408 (list '* a b))) |
40785 | 1409 |
1410 | |
1411 (defun calcFunc-div (a &rest rest) | |
1412 (while rest | |
1413 (setq a (list '/ a (car rest)) | |
1414 rest (cdr rest))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1415 (math-normalize a)) |
40785 | 1416 |
1417 (defun math-div-objects-fancy (a b) | |
1418 (cond ((and (Math-numberp a) (Math-numberp b)) | |
1419 (math-normalize | |
1420 (cond ((math-want-polar a b) | |
1421 (let ((a (math-polar a)) | |
1422 (b (math-polar b))) | |
1423 (list 'polar | |
1424 (math-div (nth 1 a) (nth 1 b)) | |
1425 (math-fix-circular (math-sub (nth 2 a) | |
1426 (nth 2 b)))))) | |
1427 ((Math-realp b) | |
1428 (setq a (math-complex a)) | |
1429 (list 'cplx (math-div (nth 1 a) b) | |
1430 (math-div (nth 2 a) b))) | |
1431 (t | |
1432 (setq a (math-complex a) | |
1433 b (math-complex b)) | |
1434 (math-div | |
1435 (list 'cplx | |
1436 (math-add (math-mul (nth 1 a) (nth 1 b)) | |
1437 (math-mul (nth 2 a) (nth 2 b))) | |
1438 (math-sub (math-mul (nth 2 a) (nth 1 b)) | |
1439 (math-mul (nth 1 a) (nth 2 b)))) | |
1440 (math-add (math-sqr (nth 1 b)) | |
1441 (math-sqr (nth 2 b)))))))) | |
1442 ((math-matrixp b) | |
1443 (if (math-square-matrixp b) | |
1444 (let ((n1 (length b))) | |
1445 (if (Math-vectorp a) | |
1446 (if (math-matrixp a) | |
1447 (if (= (length a) n1) | |
1448 (math-lud-solve (math-matrix-lud b) a b) | |
1449 (if (= (length (nth 1 a)) n1) | |
1450 (math-transpose | |
1451 (math-lud-solve (math-matrix-lud | |
1452 (math-transpose b)) | |
1453 (math-transpose a) b)) | |
1454 (math-dimension-error))) | |
1455 (if (= (length a) n1) | |
1456 (math-mat-col (math-lud-solve (math-matrix-lud b) | |
1457 (math-col-matrix a) b) | |
1458 1) | |
1459 (math-dimension-error))) | |
1460 (if (Math-equal-int a 1) | |
1461 (calcFunc-inv b) | |
1462 (math-mul a (calcFunc-inv b))))) | |
1463 (math-reject-arg b 'square-matrixp))) | |
1464 ((and (Math-vectorp a) (Math-objectp b)) | |
1465 (math-map-vec-2 'math-div a b)) | |
1466 ((eq (car-safe a) 'sdev) | |
1467 (if (eq (car-safe b) 'sdev) | |
1468 (let ((x (math-div (nth 1 a) (nth 1 b)))) | |
1469 (math-make-sdev x | |
1470 (math-div (math-hypot (nth 2 a) | |
1471 (math-mul (nth 2 b) x)) | |
1472 (nth 1 b)))) | |
1473 (if (or (Math-scalarp b) | |
1474 (not (Math-objvecp b))) | |
1475 (math-make-sdev (math-div (nth 1 a) b) (math-div (nth 2 a) b)) | |
1476 (math-reject-arg 'realp b)))) | |
1477 ((and (eq (car-safe b) 'sdev) | |
1478 (or (Math-scalarp a) | |
1479 (not (Math-objvecp a)))) | |
1480 (let ((x (math-div a (nth 1 b)))) | |
1481 (math-make-sdev x | |
1482 (math-div (math-mul (nth 2 b) x) (nth 1 b))))) | |
1483 ((and (eq (car-safe a) 'intv) (Math-anglep b)) | |
1484 (if (Math-negp b) | |
1485 (math-neg (math-div a (math-neg b))) | |
1486 (math-make-intv (nth 1 a) | |
1487 (math-div (nth 2 a) b) | |
1488 (math-div (nth 3 a) b)))) | |
1489 ((and (eq (car-safe b) 'intv) (Math-anglep a)) | |
1490 (if (or (Math-posp (nth 2 b)) | |
1491 (and (Math-zerop (nth 2 b)) (or (memq (nth 1 b) '(0 1)) | |
1492 calc-infinite-mode))) | |
1493 (if (Math-negp a) | |
1494 (math-neg (math-div (math-neg a) b)) | |
1495 (let ((calc-infinite-mode 1)) | |
1496 (math-make-intv (aref [0 2 1 3] (nth 1 b)) | |
1497 (math-div a (nth 3 b)) | |
1498 (math-div a (nth 2 b))))) | |
1499 (if (or (Math-negp (nth 3 b)) | |
1500 (and (Math-zerop (nth 3 b)) (or (memq (nth 1 b) '(0 2)) | |
1501 calc-infinite-mode))) | |
1502 (math-neg (math-div a (math-neg b))) | |
1503 (if calc-infinite-mode | |
1504 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1505 (math-reject-arg b "*Division by zero"))))) | |
1506 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1507 (eq (car-safe b) 'intv) (math-intv-constp b)) | |
1508 (if (or (Math-posp (nth 2 b)) | |
1509 (and (Math-zerop (nth 2 b)) (or (memq (nth 1 b) '(0 1)) | |
1510 calc-infinite-mode))) | |
1511 (let* ((calc-infinite-mode 1) | |
1512 (lo (math-div a (nth 2 b))) | |
1513 (hi (math-div a (nth 3 b)))) | |
1514 (or (eq (car-safe lo) 'intv) | |
1515 (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) | |
1516 lo lo))) | |
1517 (or (eq (car-safe hi) 'intv) | |
1518 (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) | |
1519 hi hi))) | |
1520 (math-combine-intervals | |
1521 (nth 2 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1522 (and (math-infinitep (nth 2 lo)) | |
1523 (not (math-zerop (nth 2 b))))) | |
1524 (memq (nth 1 lo) '(2 3))) | |
1525 (nth 3 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1526 (and (math-infinitep (nth 3 lo)) | |
1527 (not (math-zerop (nth 2 b))))) | |
1528 (memq (nth 1 lo) '(1 3))) | |
1529 (nth 2 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1530 (and (math-infinitep (nth 2 hi)) | |
1531 (not (math-zerop (nth 3 b))))) | |
1532 (memq (nth 1 hi) '(2 3))) | |
1533 (nth 3 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1534 (and (math-infinitep (nth 3 hi)) | |
1535 (not (math-zerop (nth 3 b))))) | |
1536 (memq (nth 1 hi) '(1 3))))) | |
1537 (if (or (Math-negp (nth 3 b)) | |
1538 (and (Math-zerop (nth 3 b)) (or (memq (nth 1 b) '(0 2)) | |
1539 calc-infinite-mode))) | |
1540 (math-neg (math-div a (math-neg b))) | |
1541 (if calc-infinite-mode | |
1542 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1543 (math-reject-arg b "*Division by zero"))))) | |
1544 ((and (eq (car-safe a) 'mod) | |
1545 (eq (car-safe b) 'mod) | |
1546 (equal (nth 2 a) (nth 2 b))) | |
1547 (math-make-mod (math-div-mod (nth 1 a) (nth 1 b) (nth 2 a)) | |
1548 (nth 2 a))) | |
1549 ((and (eq (car-safe a) 'mod) | |
1550 (Math-anglep b)) | |
1551 (math-make-mod (math-div-mod (nth 1 a) b (nth 2 a)) (nth 2 a))) | |
1552 ((and (eq (car-safe b) 'mod) | |
1553 (Math-anglep a)) | |
1554 (math-make-mod (math-div-mod a (nth 1 b) (nth 2 b)) (nth 2 b))) | |
1555 ((eq (car-safe a) 'hms) | |
1556 (if (eq (car-safe b) 'hms) | |
1557 (math-with-extra-prec 1 | |
1558 (math-div (math-from-hms a 'deg) | |
1559 (math-from-hms b 'deg))) | |
1560 (math-with-extra-prec 2 | |
1561 (math-to-hms (math-div (math-from-hms a 'deg) b) 'deg)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1562 (t (calc-record-why "*Incompatible arguments for /" a b)))) |
40785 | 1563 |
1564 (defun math-div-by-zero (a b) | |
1565 (if (math-infinitep a) | |
1566 (if (or (equal a '(var nan var-nan)) | |
1567 (equal b '(var uinf var-uinf)) | |
1568 (memq calc-infinite-mode '(-1 1))) | |
1569 a | |
1570 '(var uinf var-uinf)) | |
1571 (if calc-infinite-mode | |
1572 (if (math-zerop a) | |
1573 '(var nan var-nan) | |
1574 (if (eq calc-infinite-mode 1) | |
1575 (math-mul a '(var inf var-inf)) | |
1576 (if (eq calc-infinite-mode -1) | |
1577 (math-mul a '(neg (var inf var-inf))) | |
1578 (if (eq (car-safe a) 'intv) | |
1579 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1580 '(var uinf var-uinf))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1581 (math-reject-arg a "*Division by zero")))) |
40785 | 1582 |
1583 (defun math-div-zero (a b) | |
1584 (if (math-known-matrixp b) | |
1585 (if (math-vectorp b) | |
1586 (math-map-vec-2 'math-div a b) | |
1587 (math-mimic-ident 0 b)) | |
1588 (if (equal b '(var nan var-nan)) | |
1589 b | |
1590 (if (and (eq (car-safe b) 'intv) (math-intv-constp b) | |
1591 (not (math-posp b)) (not (math-negp b))) | |
1592 (if calc-infinite-mode | |
1593 (list 'intv 3 | |
1594 (if (and (math-zerop (nth 2 b)) | |
1595 (memq calc-infinite-mode '(1 -1))) | |
1596 (nth 2 b) '(neg (var inf var-inf))) | |
1597 (if (and (math-zerop (nth 3 b)) | |
1598 (memq calc-infinite-mode '(1 -1))) | |
1599 (nth 3 b) '(var inf var-inf))) | |
1600 (math-reject-arg b "*Division by zero")) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1601 a)))) |
40785 | 1602 |
1603 (defun math-div-symb-fancy (a b) | |
1604 (or (and math-simplify-only | |
1605 (not (equal a math-simplify-only)) | |
1606 (list '/ a b)) | |
1607 (and (Math-equal-int b 1) a) | |
1608 (and (Math-equal-int b -1) (math-neg a)) | |
1609 (and (Math-vectorp a) (math-known-scalarp b) | |
1610 (math-map-vec-2 'math-div a b)) | |
1611 (and (eq (car-safe b) '^) | |
1612 (or (Math-looks-negp (nth 2 b)) (Math-equal-int a 1)) | |
1613 (math-mul a (math-normalize | |
1614 (list '^ (nth 1 b) (math-neg (nth 2 b)))))) | |
1615 (and (eq (car-safe a) 'neg) | |
1616 (math-neg (math-div (nth 1 a) b))) | |
1617 (and (eq (car-safe b) 'neg) | |
1618 (math-neg (math-div a (nth 1 b)))) | |
1619 (and (eq (car-safe a) '/) | |
1620 (math-div (nth 1 a) (math-mul (nth 2 a) b))) | |
1621 (and (eq (car-safe b) '/) | |
1622 (or (math-known-scalarp (nth 1 b) t) | |
1623 (math-known-scalarp (nth 2 b) t)) | |
1624 (math-div (math-mul a (nth 2 b)) (nth 1 b))) | |
1625 (and (eq (car-safe b) 'frac) | |
1626 (math-mul (math-make-frac (nth 2 b) (nth 1 b)) a)) | |
1627 (and (eq (car-safe a) '+) | |
1628 (or (Math-numberp (nth 1 a)) | |
1629 (Math-numberp (nth 2 a))) | |
1630 (Math-numberp b) | |
1631 (math-add (math-div (nth 1 a) b) | |
1632 (math-div (nth 2 a) b))) | |
1633 (and (eq (car-safe a) '-) | |
1634 (or (Math-numberp (nth 1 a)) | |
1635 (Math-numberp (nth 2 a))) | |
1636 (Math-numberp b) | |
1637 (math-sub (math-div (nth 1 a) b) | |
1638 (math-div (nth 2 a) b))) | |
1639 (and (or (eq (car-safe a) '-) | |
1640 (math-looks-negp a)) | |
1641 (math-looks-negp b) | |
1642 (math-div (math-neg a) (math-neg b))) | |
1643 (and (eq (car-safe b) '-) | |
1644 (math-looks-negp a) | |
1645 (math-div (math-neg a) (math-neg b))) | |
1646 (and (eq (car-safe a) 'calcFunc-idn) | |
1647 (= (length a) 2) | |
1648 (or (and (eq (car-safe b) 'calcFunc-idn) | |
1649 (= (length b) 2) | |
1650 (list 'calcFunc-idn (math-div (nth 1 a) (nth 1 b)))) | |
1651 (and (math-known-scalarp b) | |
1652 (list 'calcFunc-idn (math-div (nth 1 a) b))) | |
1653 (and (math-known-matrixp b) | |
1654 (math-div (nth 1 a) b)))) | |
1655 (and (eq (car-safe b) 'calcFunc-idn) | |
1656 (= (length b) 2) | |
1657 (or (and (math-known-scalarp a) | |
1658 (list 'calcFunc-idn (math-div a (nth 1 b)))) | |
1659 (and (math-known-matrixp a) | |
1660 (math-div a (nth 1 b))))) | |
1661 (if (and calc-matrix-mode | |
1662 (or (math-known-matrixp a) (math-known-matrixp b))) | |
1663 (math-combine-prod a b nil t nil) | |
1664 (if (eq (car-safe a) '*) | |
1665 (if (eq (car-safe b) '*) | |
1666 (let ((c (math-combine-prod (nth 1 a) (nth 1 b) nil t t))) | |
1667 (and c | |
1668 (math-div (math-mul c (nth 2 a)) (nth 2 b)))) | |
1669 (let ((c (math-combine-prod (nth 1 a) b nil t t))) | |
1670 (and c | |
1671 (math-mul c (nth 2 a))))) | |
1672 (if (eq (car-safe b) '*) | |
1673 (let ((c (math-combine-prod a (nth 1 b) nil t t))) | |
1674 (and c | |
1675 (math-div c (nth 2 b)))) | |
1676 (math-combine-prod a b nil t nil)))) | |
1677 (and (math-infinitep a) | |
1678 (if (math-infinitep b) | |
1679 '(var nan var-nan) | |
1680 (if (or (equal a '(var nan var-nan)) | |
1681 (equal a '(var uinf var-uinf))) | |
1682 a | |
1683 (if (equal a '(var inf var-inf)) | |
1684 (if (or (math-posp b) | |
1685 (and (eq (car-safe b) 'intv) | |
1686 (math-zerop (nth 2 b)))) | |
1687 (if (and (eq (car-safe b) 'intv) | |
1688 (not (math-intv-constp b t))) | |
1689 '(intv 3 0 (var inf var-inf)) | |
1690 a) | |
1691 (if (or (math-negp b) | |
1692 (and (eq (car-safe b) 'intv) | |
1693 (math-zerop (nth 3 b)))) | |
1694 (if (and (eq (car-safe b) 'intv) | |
1695 (not (math-intv-constp b t))) | |
1696 '(intv 3 (neg (var inf var-inf)) 0) | |
1697 (math-neg a)) | |
1698 (if (and (eq (car-safe b) 'intv) | |
1699 (math-negp (nth 2 b)) (math-posp (nth 3 b))) | |
1700 '(intv 3 (neg (var inf var-inf)) | |
1701 (var inf var-inf))))))))) | |
1702 (and (math-infinitep b) | |
1703 (if (equal b '(var nan var-nan)) | |
1704 b | |
1705 (let ((calc-infinite-mode 1)) | |
1706 (math-mul-zero b a)))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1707 (list '/ a b))) |
40785 | 1708 |
1709 | |
1710 (defun calcFunc-mod (a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1711 (math-normalize (list '% a b))) |
40785 | 1712 |
1713 (defun math-mod-fancy (a b) | |
1714 (cond ((equal b '(var inf var-inf)) | |
1715 (if (or (math-posp a) (math-zerop a)) | |
1716 a | |
1717 (if (math-negp a) | |
1718 b | |
1719 (if (eq (car-safe a) 'intv) | |
1720 (if (math-negp (nth 2 a)) | |
1721 '(intv 3 0 (var inf var-inf)) | |
1722 a) | |
1723 (list '% a b))))) | |
1724 ((and (eq (car-safe a) 'mod) (Math-realp b) (math-posp b)) | |
1725 (math-make-mod (nth 1 a) b)) | |
1726 ((and (eq (car-safe a) 'intv) (math-intv-constp a t) (math-posp b)) | |
1727 (math-mod-intv a b)) | |
1728 (t | |
1729 (if (Math-anglep a) | |
1730 (calc-record-why 'anglep b) | |
1731 (calc-record-why 'anglep a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1732 (list '% a b)))) |
40785 | 1733 |
1734 | |
1735 (defun calcFunc-pow (a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1736 (math-normalize (list '^ a b))) |
40785 | 1737 |
1738 (defun math-pow-of-zero (a b) | |
1739 (if (Math-zerop b) | |
1740 (if calc-infinite-mode | |
1741 '(var nan var-nan) | |
1742 (math-reject-arg (list '^ a b) "*Indeterminate form")) | |
1743 (if (math-floatp b) (setq a (math-float a))) | |
1744 (if (math-posp b) | |
1745 a | |
1746 (if (math-negp b) | |
1747 (math-div 1 a) | |
1748 (if (math-infinitep b) | |
1749 '(var nan var-nan) | |
1750 (if (and (eq (car b) 'intv) (math-intv-constp b) | |
1751 calc-infinite-mode) | |
1752 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1753 (if (math-objectp b) | |
1754 (list '^ a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1755 a))))))) |
40785 | 1756 |
1757 (defun math-pow-zero (a b) | |
1758 (if (eq (car-safe a) 'mod) | |
1759 (math-make-mod 1 (nth 2 a)) | |
1760 (if (math-known-matrixp a) | |
1761 (math-mimic-ident 1 a) | |
1762 (if (math-infinitep a) | |
1763 '(var nan var-nan) | |
1764 (if (and (eq (car a) 'intv) (math-intv-constp a) | |
1765 (or (and (not (math-posp a)) (not (math-negp a))) | |
1766 (not (math-intv-constp a t)))) | |
1767 '(intv 3 (neg (var inf var-inf)) (var inf var-inf)) | |
1768 (if (or (math-floatp a) (math-floatp b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1769 '(float 1 0) 1)))))) |
40785 | 1770 |
1771 (defun math-pow-fancy (a b) | |
1772 (cond ((and (Math-numberp a) (Math-numberp b)) | |
1773 (or (if (memq (math-quarter-integer b) '(1 2 3)) | |
1774 (let ((sqrt (math-sqrt (if (math-floatp b) | |
1775 (math-float a) a)))) | |
1776 (and (Math-numberp sqrt) | |
1777 (math-pow sqrt (math-mul 2 b)))) | |
1778 (and (eq (car b) 'frac) | |
1779 (integerp (nth 2 b)) | |
1780 (<= (nth 2 b) 10) | |
1781 (let ((root (math-nth-root a (nth 2 b)))) | |
1782 (and root (math-ipow root (nth 1 b)))))) | |
1783 (and (or (eq a 10) (equal a '(float 1 1))) | |
1784 (math-num-integerp b) | |
1785 (calcFunc-scf '(float 1 0) b)) | |
1786 (and calc-symbolic-mode | |
1787 (list '^ a b)) | |
1788 (math-with-extra-prec 2 | |
1789 (math-exp-raw | |
1790 (math-float (math-mul b (math-ln-raw (math-float a)))))))) | |
1791 ((or (not (Math-objvecp a)) | |
1792 (not (Math-objectp b))) | |
1793 (let (temp) | |
1794 (cond ((and math-simplify-only | |
1795 (not (equal a math-simplify-only))) | |
1796 (list '^ a b)) | |
1797 ((and (eq (car-safe a) '*) | |
1798 (or (math-known-num-integerp b) | |
1799 (math-known-nonnegp (nth 1 a)) | |
1800 (math-known-nonnegp (nth 2 a)))) | |
1801 (math-mul (math-pow (nth 1 a) b) | |
1802 (math-pow (nth 2 a) b))) | |
1803 ((and (eq (car-safe a) '/) | |
1804 (or (math-known-num-integerp b) | |
1805 (math-known-nonnegp (nth 2 a)))) | |
1806 (math-div (math-pow (nth 1 a) b) | |
1807 (math-pow (nth 2 a) b))) | |
1808 ((and (eq (car-safe a) '/) | |
1809 (math-known-nonnegp (nth 1 a)) | |
1810 (not (math-equal-int (nth 1 a) 1))) | |
1811 (math-mul (math-pow (nth 1 a) b) | |
1812 (math-pow (math-div 1 (nth 2 a)) b))) | |
1813 ((and (eq (car-safe a) '^) | |
1814 (or (math-known-num-integerp b) | |
1815 (math-known-nonnegp (nth 1 a)))) | |
1816 (math-pow (nth 1 a) (math-mul (nth 2 a) b))) | |
1817 ((and (eq (car-safe a) 'calcFunc-sqrt) | |
1818 (or (math-known-num-integerp b) | |
1819 (math-known-nonnegp (nth 1 a)))) | |
1820 (math-pow (nth 1 a) (math-div b 2))) | |
1821 ((and (eq (car-safe a) '^) | |
1822 (math-known-evenp (nth 2 a)) | |
1823 (memq (math-quarter-integer b) '(1 2 3)) | |
1824 (math-known-realp (nth 1 a))) | |
1825 (math-abs (math-pow (nth 1 a) (math-mul (nth 2 a) b)))) | |
1826 ((and (math-looks-negp a) | |
1827 (math-known-integerp b) | |
1828 (setq temp (or (and (math-known-evenp b) | |
1829 (math-pow (math-neg a) b)) | |
1830 (and (math-known-oddp b) | |
1831 (math-neg (math-pow (math-neg a) | |
1832 b)))))) | |
1833 temp) | |
1834 ((and (eq (car-safe a) 'calcFunc-abs) | |
1835 (math-known-realp (nth 1 a)) | |
1836 (math-known-evenp b)) | |
1837 (math-pow (nth 1 a) b)) | |
1838 ((math-infinitep a) | |
1839 (cond ((equal a '(var nan var-nan)) | |
1840 a) | |
1841 ((eq (car a) 'neg) | |
1842 (math-mul (math-pow -1 b) (math-pow (nth 1 a) b))) | |
1843 ((math-posp b) | |
1844 a) | |
1845 ((math-negp b) | |
1846 (if (math-floatp b) '(float 0 0) 0)) | |
1847 ((and (eq (car-safe b) 'intv) | |
1848 (math-intv-constp b)) | |
1849 '(intv 3 0 (var inf var-inf))) | |
1850 (t | |
1851 '(var nan var-nan)))) | |
1852 ((math-infinitep b) | |
1853 (let (scale) | |
1854 (cond ((math-negp b) | |
1855 (math-pow (math-div 1 a) (math-neg b))) | |
1856 ((not (math-posp b)) | |
1857 '(var nan var-nan)) | |
1858 ((math-equal-int (setq scale (calcFunc-abssqr a)) 1) | |
1859 '(var nan var-nan)) | |
1860 ((Math-lessp scale 1) | |
1861 (if (math-floatp a) '(float 0 0) 0)) | |
1862 ((Math-lessp 1 a) | |
1863 b) | |
1864 ((Math-lessp a -1) | |
1865 '(var uinf var-uinf)) | |
1866 ((and (eq (car a) 'intv) | |
1867 (math-intv-constp a)) | |
1868 (if (Math-lessp -1 a) | |
1869 (if (math-equal-int (nth 3 a) 1) | |
1870 '(intv 3 0 1) | |
1871 '(intv 3 0 (var inf var-inf))) | |
1872 '(intv 3 (neg (var inf var-inf)) | |
1873 (var inf var-inf)))) | |
1874 (t (list '^ a b))))) | |
1875 ((and (eq (car-safe a) 'calcFunc-idn) | |
1876 (= (length a) 2) | |
1877 (math-known-num-integerp b)) | |
1878 (list 'calcFunc-idn (math-pow (nth 1 a) b))) | |
1879 (t (if (Math-objectp a) | |
1880 (calc-record-why 'objectp b) | |
1881 (calc-record-why 'objectp a)) | |
1882 (list '^ a b))))) | |
1883 ((and (eq (car-safe a) 'sdev) (eq (car-safe b) 'sdev)) | |
1884 (if (and (math-constp a) (math-constp b)) | |
1885 (math-with-extra-prec 2 | |
1886 (let* ((ln (math-ln-raw (math-float (nth 1 a)))) | |
1887 (pow (math-exp-raw | |
1888 (math-float (math-mul (nth 1 b) ln))))) | |
1889 (math-make-sdev | |
1890 pow | |
1891 (math-mul | |
1892 pow | |
1893 (math-hypot (math-mul (nth 2 a) | |
1894 (math-div (nth 1 b) (nth 1 a))) | |
1895 (math-mul (nth 2 b) ln)))))) | |
1896 (let ((pow (math-pow (nth 1 a) (nth 1 b)))) | |
1897 (math-make-sdev | |
1898 pow | |
1899 (math-mul pow | |
1900 (math-hypot (math-mul (nth 2 a) | |
1901 (math-div (nth 1 b) (nth 1 a))) | |
1902 (math-mul (nth 2 b) (calcFunc-ln | |
1903 (nth 1 a))))))))) | |
1904 ((and (eq (car-safe a) 'sdev) (Math-numberp b)) | |
1905 (if (math-constp a) | |
1906 (math-with-extra-prec 2 | |
1907 (let ((pow (math-pow (nth 1 a) (math-sub b 1)))) | |
1908 (math-make-sdev (math-mul pow (nth 1 a)) | |
1909 (math-mul pow (math-mul (nth 2 a) b))))) | |
1910 (math-make-sdev (math-pow (nth 1 a) b) | |
1911 (math-mul (math-pow (nth 1 a) (math-add b -1)) | |
1912 (math-mul (nth 2 a) b))))) | |
1913 ((and (eq (car-safe b) 'sdev) (Math-numberp a)) | |
1914 (math-with-extra-prec 2 | |
1915 (let* ((ln (math-ln-raw (math-float a))) | |
1916 (pow (calcFunc-exp (math-mul (nth 1 b) ln)))) | |
1917 (math-make-sdev pow (math-mul pow (math-mul (nth 2 b) ln)))))) | |
1918 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1919 (Math-realp b) | |
1920 (or (Math-natnump b) | |
1921 (Math-posp (nth 2 a)) | |
1922 (and (math-zerop (nth 2 a)) | |
1923 (or (Math-posp b) | |
1924 (and (Math-integerp b) calc-infinite-mode))) | |
1925 (Math-negp (nth 3 a)) | |
1926 (and (math-zerop (nth 3 a)) | |
1927 (or (Math-posp b) | |
1928 (and (Math-integerp b) calc-infinite-mode))))) | |
1929 (if (math-evenp b) | |
1930 (setq a (math-abs a))) | |
1931 (let ((calc-infinite-mode (if (math-zerop (nth 3 a)) -1 1))) | |
1932 (math-sort-intv (nth 1 a) | |
1933 (math-pow (nth 2 a) b) | |
1934 (math-pow (nth 3 a) b)))) | |
1935 ((and (eq (car-safe b) 'intv) (math-intv-constp b) | |
1936 (Math-realp a) (Math-posp a)) | |
1937 (math-sort-intv (nth 1 b) | |
1938 (math-pow a (nth 2 b)) | |
1939 (math-pow a (nth 3 b)))) | |
1940 ((and (eq (car-safe a) 'intv) (math-intv-constp a) | |
1941 (eq (car-safe b) 'intv) (math-intv-constp b) | |
1942 (or (and (not (Math-negp (nth 2 a))) | |
1943 (not (Math-negp (nth 2 b)))) | |
1944 (and (Math-posp (nth 2 a)) | |
1945 (not (Math-posp (nth 3 b)))))) | |
1946 (let ((lo (math-pow a (nth 2 b))) | |
1947 (hi (math-pow a (nth 3 b)))) | |
1948 (or (eq (car-safe lo) 'intv) | |
1949 (setq lo (list 'intv (if (memq (nth 1 b) '(2 3)) 3 0) lo lo))) | |
1950 (or (eq (car-safe hi) 'intv) | |
1951 (setq hi (list 'intv (if (memq (nth 1 b) '(1 3)) 3 0) hi hi))) | |
1952 (math-combine-intervals | |
1953 (nth 2 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1954 (math-infinitep (nth 2 lo))) | |
1955 (memq (nth 1 lo) '(2 3))) | |
1956 (nth 3 lo) (and (or (memq (nth 1 b) '(2 3)) | |
1957 (math-infinitep (nth 3 lo))) | |
1958 (memq (nth 1 lo) '(1 3))) | |
1959 (nth 2 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1960 (math-infinitep (nth 2 hi))) | |
1961 (memq (nth 1 hi) '(2 3))) | |
1962 (nth 3 hi) (and (or (memq (nth 1 b) '(1 3)) | |
1963 (math-infinitep (nth 3 hi))) | |
1964 (memq (nth 1 hi) '(1 3)))))) | |
1965 ((and (eq (car-safe a) 'mod) (eq (car-safe b) 'mod) | |
1966 (equal (nth 2 a) (nth 2 b))) | |
1967 (math-make-mod (math-pow-mod (nth 1 a) (nth 1 b) (nth 2 a)) | |
1968 (nth 2 a))) | |
1969 ((and (eq (car-safe a) 'mod) (Math-anglep b)) | |
1970 (math-make-mod (math-pow-mod (nth 1 a) b (nth 2 a)) (nth 2 a))) | |
1971 ((and (eq (car-safe b) 'mod) (Math-anglep a)) | |
1972 (math-make-mod (math-pow-mod a (nth 1 b) (nth 2 b)) (nth 2 b))) | |
1973 ((not (Math-numberp a)) | |
1974 (math-reject-arg a 'numberp)) | |
1975 (t | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
1976 (math-reject-arg b 'numberp)))) |
40785 | 1977 |
1978 (defun math-quarter-integer (x) | |
1979 (if (Math-integerp x) | |
1980 0 | |
1981 (if (math-negp x) | |
1982 (progn | |
1983 (setq x (math-quarter-integer (math-neg x))) | |
1984 (and x (- 4 x))) | |
1985 (if (eq (car x) 'frac) | |
1986 (if (eq (nth 2 x) 2) | |
1987 2 | |
1988 (and (eq (nth 2 x) 4) | |
1989 (progn | |
1990 (setq x (nth 1 x)) | |
1991 (% (if (consp x) (nth 1 x) x) 4)))) | |
1992 (if (eq (car x) 'float) | |
1993 (if (>= (nth 2 x) 0) | |
1994 0 | |
1995 (if (= (nth 2 x) -1) | |
1996 (progn | |
1997 (setq x (nth 1 x)) | |
1998 (and (= (% (if (consp x) (nth 1 x) x) 10) 5) 2)) | |
1999 (if (= (nth 2 x) -2) | |
2000 (progn | |
2001 (setq x (nth 1 x) | |
2002 x (% (if (consp x) (nth 1 x) x) 100)) | |
2003 (if (= x 25) 1 | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2004 (if (= x 75) 3))))))))))) |
40785 | 2005 |
2006 ;;; This assumes A < M and M > 0. | |
2007 (defun math-pow-mod (a b m) ; [R R R R] | |
2008 (if (and (Math-integerp a) (Math-integerp b) (Math-integerp m)) | |
2009 (if (Math-negp b) | |
2010 (math-div-mod 1 (math-pow-mod a (Math-integer-neg b) m) m) | |
2011 (if (eq m 1) | |
2012 0 | |
2013 (math-pow-mod-step a b m))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2014 (math-mod (math-pow a b) m))) |
40785 | 2015 |
2016 (defun math-pow-mod-step (a n m) ; [I I I I] | |
2017 (math-working "pow" a) | |
2018 (let ((val (cond | |
2019 ((eq n 0) 1) | |
2020 ((eq n 1) a) | |
2021 (t | |
2022 (let ((rest (math-pow-mod-step | |
2023 (math-imod (math-mul a a) m) | |
2024 (math-div2 n) | |
2025 m))) | |
2026 (if (math-evenp n) | |
2027 rest | |
2028 (math-mod (math-mul a rest) m))))))) | |
2029 (math-working "pow" val) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2030 val)) |
40785 | 2031 |
2032 | |
2033 ;;; Compute the minimum of two real numbers. [R R R] [Public] | |
2034 (defun math-min (a b) | |
2035 (if (and (consp a) (eq (car a) 'intv)) | |
2036 (if (and (consp b) (eq (car b) 'intv)) | |
2037 (let ((lo (nth 2 a)) | |
2038 (lom (memq (nth 1 a) '(2 3))) | |
2039 (hi (nth 3 a)) | |
2040 (him (memq (nth 1 a) '(1 3))) | |
2041 res) | |
2042 (if (= (setq res (math-compare (nth 2 b) lo)) -1) | |
2043 (setq lo (nth 2 b) lom (memq (nth 1 b) '(2 3))) | |
2044 (if (= res 0) | |
2045 (setq lom (or lom (memq (nth 1 b) '(2 3)))))) | |
2046 (if (= (setq res (math-compare (nth 3 b) hi)) -1) | |
2047 (setq hi (nth 3 b) him (memq (nth 1 b) '(1 3))) | |
2048 (if (= res 0) | |
2049 (setq him (or him (memq (nth 1 b) '(1 3)))))) | |
2050 (math-make-intv (+ (if lom 2 0) (if him 1 0)) lo hi)) | |
2051 (math-min a (list 'intv 3 b b))) | |
2052 (if (and (consp b) (eq (car b) 'intv)) | |
2053 (math-min (list 'intv 3 a a) b) | |
2054 (let ((res (math-compare a b))) | |
2055 (if (= res 1) | |
2056 b | |
2057 (if (= res 2) | |
2058 '(var nan var-nan) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2059 a)))))) |
40785 | 2060 |
2061 (defun calcFunc-min (&optional a &rest b) | |
2062 (if (not a) | |
2063 '(var inf var-inf) | |
2064 (if (not (or (Math-anglep a) (eq (car a) 'date) | |
2065 (and (eq (car a) 'intv) (math-intv-constp a)) | |
2066 (math-infinitep a))) | |
2067 (math-reject-arg a 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2068 (math-min-list a b))) |
40785 | 2069 |
2070 (defun math-min-list (a b) | |
2071 (if b | |
2072 (if (or (Math-anglep (car b)) (eq (car b) 'date) | |
2073 (and (eq (car (car b)) 'intv) (math-intv-constp (car b))) | |
2074 (math-infinitep (car b))) | |
2075 (math-min-list (math-min a (car b)) (cdr b)) | |
2076 (math-reject-arg (car b) 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2077 a)) |
40785 | 2078 |
2079 ;;; Compute the maximum of two real numbers. [R R R] [Public] | |
2080 (defun math-max (a b) | |
2081 (if (or (and (consp a) (eq (car a) 'intv)) | |
2082 (and (consp b) (eq (car b) 'intv))) | |
2083 (math-neg (math-min (math-neg a) (math-neg b))) | |
2084 (let ((res (math-compare a b))) | |
2085 (if (= res -1) | |
2086 b | |
2087 (if (= res 2) | |
2088 '(var nan var-nan) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2089 a))))) |
40785 | 2090 |
2091 (defun calcFunc-max (&optional a &rest b) | |
2092 (if (not a) | |
2093 '(neg (var inf var-inf)) | |
2094 (if (not (or (Math-anglep a) (eq (car a) 'date) | |
2095 (and (eq (car a) 'intv) (math-intv-constp a)) | |
2096 (math-infinitep a))) | |
2097 (math-reject-arg a 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2098 (math-max-list a b))) |
40785 | 2099 |
2100 (defun math-max-list (a b) | |
2101 (if b | |
2102 (if (or (Math-anglep (car b)) (eq (car b) 'date) | |
2103 (and (eq (car (car b)) 'intv) (math-intv-constp (car b))) | |
2104 (math-infinitep (car b))) | |
2105 (math-max-list (math-max a (car b)) (cdr b)) | |
2106 (math-reject-arg (car b) 'anglep)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2107 a)) |
40785 | 2108 |
2109 | |
2110 ;;; Compute the absolute value of A. [O O; r r] [Public] | |
2111 (defun math-abs (a) | |
2112 (cond ((Math-negp a) | |
2113 (math-neg a)) | |
2114 ((Math-anglep a) | |
2115 a) | |
2116 ((eq (car a) 'cplx) | |
2117 (math-hypot (nth 1 a) (nth 2 a))) | |
2118 ((eq (car a) 'polar) | |
2119 (nth 1 a)) | |
2120 ((eq (car a) 'vec) | |
2121 (if (cdr (cdr (cdr a))) | |
2122 (math-sqrt (calcFunc-abssqr a)) | |
2123 (if (cdr (cdr a)) | |
2124 (math-hypot (nth 1 a) (nth 2 a)) | |
2125 (if (cdr a) | |
2126 (math-abs (nth 1 a)) | |
2127 a)))) | |
2128 ((eq (car a) 'sdev) | |
2129 (list 'sdev (math-abs (nth 1 a)) (nth 2 a))) | |
2130 ((and (eq (car a) 'intv) (math-intv-constp a)) | |
2131 (if (Math-posp a) | |
2132 a | |
2133 (let* ((nlo (math-neg (nth 2 a))) | |
2134 (res (math-compare nlo (nth 3 a)))) | |
2135 (cond ((= res 1) | |
2136 (math-make-intv (if (memq (nth 1 a) '(0 1)) 2 3) 0 nlo)) | |
2137 ((= res 0) | |
2138 (math-make-intv (if (eq (nth 1 a) 0) 2 3) 0 nlo)) | |
2139 (t | |
2140 (math-make-intv (if (memq (nth 1 a) '(0 2)) 2 3) | |
2141 0 (nth 3 a))))))) | |
2142 ((math-looks-negp a) | |
2143 (list 'calcFunc-abs (math-neg a))) | |
2144 ((let ((signs (math-possible-signs a))) | |
2145 (or (and (memq signs '(2 4 6)) a) | |
2146 (and (memq signs '(1 3)) (math-neg a))))) | |
2147 ((let ((inf (math-infinitep a))) | |
2148 (and inf | |
2149 (if (equal inf '(var nan var-nan)) | |
2150 inf | |
2151 '(var inf var-inf))))) | |
2152 (t (calc-record-why 'numvecp a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2153 (list 'calcFunc-abs a)))) |
40785 | 2154 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2155 (defalias 'calcFunc-abs 'math-abs) |
40785 | 2156 |
2157 (defun math-float-fancy (a) | |
2158 (cond ((eq (car a) 'intv) | |
2159 (cons (car a) (cons (nth 1 a) (mapcar 'math-float (nthcdr 2 a))))) | |
2160 ((and (memq (car a) '(* /)) | |
2161 (math-numberp (nth 1 a))) | |
2162 (list (car a) (math-float (nth 1 a)) | |
2163 (list 'calcFunc-float (nth 2 a)))) | |
2164 ((and (eq (car a) '/) | |
2165 (eq (car (nth 1 a)) '*) | |
2166 (math-numberp (nth 1 (nth 1 a)))) | |
2167 (list '* (math-float (nth 1 (nth 1 a))) | |
2168 (list 'calcFunc-float (list '/ (nth 2 (nth 1 a)) (nth 2 a))))) | |
2169 ((math-infinitep a) a) | |
2170 ((eq (car a) 'calcFunc-float) a) | |
2171 ((let ((func (assq (car a) '((calcFunc-floor . calcFunc-ffloor) | |
2172 (calcFunc-ceil . calcFunc-fceil) | |
2173 (calcFunc-trunc . calcFunc-ftrunc) | |
2174 (calcFunc-round . calcFunc-fround) | |
2175 (calcFunc-rounde . calcFunc-frounde) | |
2176 (calcFunc-roundu . calcFunc-froundu))))) | |
2177 (and func (cons (cdr func) (cdr a))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2178 (t (math-reject-arg a 'objectp)))) |
40785 | 2179 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2180 (defalias 'calcFunc-float 'math-float) |
40785 | 2181 |
2182 (defun math-trunc-fancy (a) | |
2183 (cond ((eq (car a) 'frac) (math-quotient (nth 1 a) (nth 2 a))) | |
2184 ((eq (car a) 'cplx) (math-trunc (nth 1 a))) | |
2185 ((eq (car a) 'polar) (math-trunc (math-complex a))) | |
2186 ((eq (car a) 'hms) (list 'hms (nth 1 a) 0 0)) | |
2187 ((eq (car a) 'date) (list 'date (math-trunc (nth 1 a)))) | |
2188 ((eq (car a) 'mod) | |
2189 (if (math-messy-integerp (nth 2 a)) | |
2190 (math-trunc (math-make-mod (nth 1 a) (math-trunc (nth 2 a)))) | |
2191 (math-make-mod (math-trunc (nth 1 a)) (nth 2 a)))) | |
2192 ((eq (car a) 'intv) | |
2193 (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
2194 (memq (nth 1 a) '(0 1))) | |
2195 0 2) | |
2196 (if (and (equal (nth 3 a) '(var inf var-inf)) | |
2197 (memq (nth 1 a) '(0 2))) | |
2198 0 1)) | |
2199 (if (and (Math-negp (nth 2 a)) | |
2200 (Math-num-integerp (nth 2 a)) | |
2201 (memq (nth 1 a) '(0 1))) | |
2202 (math-add (math-trunc (nth 2 a)) 1) | |
2203 (math-trunc (nth 2 a))) | |
2204 (if (and (Math-posp (nth 3 a)) | |
2205 (Math-num-integerp (nth 3 a)) | |
2206 (memq (nth 1 a) '(0 2))) | |
2207 (math-add (math-trunc (nth 3 a)) -1) | |
2208 (math-trunc (nth 3 a))))) | |
2209 ((math-provably-integerp a) a) | |
2210 ((Math-vectorp a) | |
2211 (math-map-vec (function (lambda (x) (math-trunc x prec))) a)) | |
2212 ((math-infinitep a) | |
2213 (if (or (math-posp a) (math-negp a)) | |
2214 a | |
2215 '(var nan var-nan))) | |
2216 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2217 (t (math-reject-arg a 'numberp)))) |
40785 | 2218 |
2219 (defun math-trunc-special (a prec) | |
2220 (if (Math-messy-integerp prec) | |
2221 (setq prec (math-trunc prec))) | |
2222 (or (integerp prec) | |
2223 (math-reject-arg prec 'fixnump)) | |
2224 (if (and (<= prec 0) | |
2225 (math-provably-integerp a)) | |
2226 a | |
2227 (calcFunc-scf (math-trunc (let ((calc-prefer-frac t)) | |
2228 (calcFunc-scf a prec))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2229 (- prec)))) |
40785 | 2230 |
2231 (defun math-to-integer (a) | |
2232 (let ((func (assq (car-safe a) '((calcFunc-ffloor . calcFunc-floor) | |
2233 (calcFunc-fceil . calcFunc-ceil) | |
2234 (calcFunc-ftrunc . calcFunc-trunc) | |
2235 (calcFunc-fround . calcFunc-round) | |
2236 (calcFunc-frounde . calcFunc-rounde) | |
2237 (calcFunc-froundu . calcFunc-roundu))))) | |
2238 (and func (= (length a) 2) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2239 (cons (cdr func) (cdr a))))) |
40785 | 2240 |
2241 (defun calcFunc-ftrunc (a &optional prec) | |
2242 (if (and (Math-messy-integerp a) | |
2243 (or (not prec) (and (integerp prec) | |
2244 (<= prec 0)))) | |
2245 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2246 (math-float (math-trunc a prec)))) |
40785 | 2247 |
2248 (defun math-floor-fancy (a) | |
2249 (cond ((math-provably-integerp a) a) | |
2250 ((eq (car a) 'hms) | |
2251 (if (or (math-posp a) | |
2252 (and (math-zerop (nth 2 a)) | |
2253 (math-zerop (nth 3 a)))) | |
2254 (math-trunc a) | |
2255 (math-add (math-trunc a) -1))) | |
2256 ((eq (car a) 'date) (list 'date (math-floor (nth 1 a)))) | |
2257 ((eq (car a) 'intv) | |
2258 (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
2259 (memq (nth 1 a) '(0 1))) | |
2260 0 2) | |
2261 (if (and (equal (nth 3 a) '(var inf var-inf)) | |
2262 (memq (nth 1 a) '(0 2))) | |
2263 0 1)) | |
2264 (math-floor (nth 2 a)) | |
2265 (if (and (Math-num-integerp (nth 3 a)) | |
2266 (memq (nth 1 a) '(0 2))) | |
2267 (math-add (math-floor (nth 3 a)) -1) | |
2268 (math-floor (nth 3 a))))) | |
2269 ((Math-vectorp a) | |
2270 (math-map-vec (function (lambda (x) (math-floor x prec))) a)) | |
2271 ((math-infinitep a) | |
2272 (if (or (math-posp a) (math-negp a)) | |
2273 a | |
2274 '(var nan var-nan))) | |
2275 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2276 (t (math-reject-arg a 'anglep)))) |
40785 | 2277 |
2278 (defun math-floor-special (a prec) | |
2279 (if (Math-messy-integerp prec) | |
2280 (setq prec (math-trunc prec))) | |
2281 (or (integerp prec) | |
2282 (math-reject-arg prec 'fixnump)) | |
2283 (if (and (<= prec 0) | |
2284 (math-provably-integerp a)) | |
2285 a | |
2286 (calcFunc-scf (math-floor (let ((calc-prefer-frac t)) | |
2287 (calcFunc-scf a prec))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2288 (- prec)))) |
40785 | 2289 |
2290 (defun calcFunc-ffloor (a &optional prec) | |
2291 (if (and (Math-messy-integerp a) | |
2292 (or (not prec) (and (integerp prec) | |
2293 (<= prec 0)))) | |
2294 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2295 (math-float (math-floor a prec)))) |
40785 | 2296 |
2297 ;;; Coerce A to be an integer (by truncation toward plus infinity). [I N] | |
2298 (defun math-ceiling (a &optional prec) ; [Public] | |
2299 (cond (prec | |
2300 (if (Math-messy-integerp prec) | |
2301 (setq prec (math-trunc prec))) | |
2302 (or (integerp prec) | |
2303 (math-reject-arg prec 'fixnump)) | |
2304 (if (and (<= prec 0) | |
2305 (math-provably-integerp a)) | |
2306 a | |
2307 (calcFunc-scf (math-ceiling (let ((calc-prefer-frac t)) | |
2308 (calcFunc-scf a prec))) | |
2309 (- prec)))) | |
2310 ((Math-integerp a) a) | |
2311 ((Math-messy-integerp a) (math-trunc a)) | |
2312 ((Math-realp a) | |
2313 (if (Math-posp a) | |
2314 (math-add (math-trunc a) 1) | |
2315 (math-trunc a))) | |
2316 ((math-provably-integerp a) a) | |
2317 ((eq (car a) 'hms) | |
2318 (if (or (math-negp a) | |
2319 (and (math-zerop (nth 2 a)) | |
2320 (math-zerop (nth 3 a)))) | |
2321 (math-trunc a) | |
2322 (math-add (math-trunc a) 1))) | |
2323 ((eq (car a) 'date) (list 'date (math-ceiling (nth 1 a)))) | |
2324 ((eq (car a) 'intv) | |
2325 (math-make-intv (+ (if (and (equal (nth 2 a) '(neg (var inf var-inf))) | |
2326 (memq (nth 1 a) '(0 1))) | |
2327 0 2) | |
2328 (if (and (equal (nth 3 a) '(var inf var-inf)) | |
2329 (memq (nth 1 a) '(0 2))) | |
2330 0 1)) | |
2331 (if (and (Math-num-integerp (nth 2 a)) | |
2332 (memq (nth 1 a) '(0 1))) | |
2333 (math-add (math-floor (nth 2 a)) 1) | |
2334 (math-ceiling (nth 2 a))) | |
2335 (math-ceiling (nth 3 a)))) | |
2336 ((Math-vectorp a) | |
2337 (math-map-vec (function (lambda (x) (math-ceiling x prec))) a)) | |
2338 ((math-infinitep a) | |
2339 (if (or (math-posp a) (math-negp a)) | |
2340 a | |
2341 '(var nan var-nan))) | |
2342 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2343 (t (math-reject-arg a 'anglep)))) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2344 |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2345 (defalias 'calcFunc-ceil 'math-ceiling) |
40785 | 2346 |
2347 (defun calcFunc-fceil (a &optional prec) | |
2348 (if (and (Math-messy-integerp a) | |
2349 (or (not prec) (and (integerp prec) | |
2350 (<= prec 0)))) | |
2351 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2352 (math-float (math-ceiling a prec)))) |
40785 | 2353 |
2354 (setq math-rounding-mode nil) | |
2355 | |
2356 ;;; Coerce A to be an integer (by rounding to nearest integer). [I N] [Public] | |
2357 (defun math-round (a &optional prec) | |
2358 (cond (prec | |
2359 (if (Math-messy-integerp prec) | |
2360 (setq prec (math-trunc prec))) | |
2361 (or (integerp prec) | |
2362 (math-reject-arg prec 'fixnump)) | |
2363 (if (and (<= prec 0) | |
2364 (math-provably-integerp a)) | |
2365 a | |
2366 (calcFunc-scf (math-round (let ((calc-prefer-frac t)) | |
2367 (calcFunc-scf a prec))) | |
2368 (- prec)))) | |
2369 ((Math-anglep a) | |
2370 (if (Math-num-integerp a) | |
2371 (math-trunc a) | |
2372 (if (and (Math-negp a) (not (eq math-rounding-mode 'up))) | |
2373 (math-neg (math-round (math-neg a))) | |
2374 (setq a (let ((calc-angle-mode 'deg)) ; in case of HMS forms | |
2375 (math-add a (if (Math-ratp a) | |
2376 '(frac 1 2) | |
2377 '(float 5 -1))))) | |
2378 (if (and (Math-num-integerp a) (eq math-rounding-mode 'even)) | |
2379 (progn | |
2380 (setq a (math-floor a)) | |
2381 (or (math-evenp a) | |
2382 (setq a (math-sub a 1))) | |
2383 a) | |
2384 (math-floor a))))) | |
2385 ((math-provably-integerp a) a) | |
2386 ((eq (car a) 'date) (list 'date (math-round (nth 1 a)))) | |
2387 ((eq (car a) 'intv) | |
2388 (math-floor (math-add a '(frac 1 2)))) | |
2389 ((Math-vectorp a) | |
2390 (math-map-vec (function (lambda (x) (math-round x prec))) a)) | |
2391 ((math-infinitep a) | |
2392 (if (or (math-posp a) (math-negp a)) | |
2393 a | |
2394 '(var nan var-nan))) | |
2395 ((math-to-integer a)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2396 (t (math-reject-arg a 'anglep)))) |
40785 | 2397 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2398 (defalias 'calcFunc-round 'math-round) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2399 |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2400 (defsubst calcFunc-rounde (a &optional prec) |
40785 | 2401 (let ((math-rounding-mode 'even)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2402 (math-round a prec))) |
40785 | 2403 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2404 (defsubst calcFunc-roundu (a &optional prec) |
40785 | 2405 (let ((math-rounding-mode 'up)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2406 (math-round a prec))) |
40785 | 2407 |
2408 (defun calcFunc-fround (a &optional prec) | |
2409 (if (and (Math-messy-integerp a) | |
2410 (or (not prec) (and (integerp prec) | |
2411 (<= prec 0)))) | |
2412 a | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2413 (math-float (math-round a prec)))) |
40785 | 2414 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2415 (defsubst calcFunc-frounde (a &optional prec) |
40785 | 2416 (let ((math-rounding-mode 'even)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2417 (calcFunc-fround a prec))) |
40785 | 2418 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2419 (defsubst calcFunc-froundu (a &optional prec) |
40785 | 2420 (let ((math-rounding-mode 'up)) |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2421 (calcFunc-fround a prec))) |
40785 | 2422 |
2423 ;;; Pull floating-point values apart into mantissa and exponent. | |
2424 (defun calcFunc-mant (x) | |
2425 (if (Math-realp x) | |
2426 (if (or (Math-ratp x) | |
2427 (eq (nth 1 x) 0)) | |
2428 x | |
2429 (list 'float (nth 1 x) (- 1 (math-numdigs (nth 1 x))))) | |
2430 (calc-record-why 'realp x) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2431 (list 'calcFunc-mant x))) |
40785 | 2432 |
2433 (defun calcFunc-xpon (x) | |
2434 (if (Math-realp x) | |
2435 (if (or (Math-ratp x) | |
2436 (eq (nth 1 x) 0)) | |
2437 0 | |
2438 (math-normalize (+ (nth 2 x) (1- (math-numdigs (nth 1 x)))))) | |
2439 (calc-record-why 'realp x) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2440 (list 'calcFunc-xpon x))) |
40785 | 2441 |
2442 (defun calcFunc-scf (x n) | |
2443 (if (integerp n) | |
2444 (cond ((eq n 0) | |
2445 x) | |
2446 ((Math-integerp x) | |
2447 (if (> n 0) | |
2448 (math-scale-int x n) | |
2449 (math-div x (math-scale-int 1 (- n))))) | |
2450 ((eq (car x) 'frac) | |
2451 (if (> n 0) | |
2452 (math-make-frac (math-scale-int (nth 1 x) n) (nth 2 x)) | |
2453 (math-make-frac (nth 1 x) (math-scale-int (nth 2 x) (- n))))) | |
2454 ((eq (car x) 'float) | |
2455 (math-make-float (nth 1 x) (+ (nth 2 x) n))) | |
2456 ((memq (car x) '(cplx sdev)) | |
2457 (math-normalize | |
2458 (list (car x) | |
2459 (calcFunc-scf (nth 1 x) n) | |
2460 (calcFunc-scf (nth 2 x) n)))) | |
2461 ((memq (car x) '(polar mod)) | |
2462 (math-normalize | |
2463 (list (car x) | |
2464 (calcFunc-scf (nth 1 x) n) | |
2465 (nth 2 x)))) | |
2466 ((eq (car x) 'intv) | |
2467 (math-normalize | |
2468 (list (car x) | |
2469 (nth 1 x) | |
2470 (calcFunc-scf (nth 2 x) n) | |
2471 (calcFunc-scf (nth 3 x) n)))) | |
2472 ((eq (car x) 'vec) | |
2473 (math-map-vec (function (lambda (x) (calcFunc-scf x n))) x)) | |
2474 ((math-infinitep x) | |
2475 x) | |
2476 (t | |
2477 (calc-record-why 'realp x) | |
2478 (list 'calcFunc-scf x n))) | |
2479 (if (math-messy-integerp n) | |
2480 (if (< (nth 2 n) 10) | |
2481 (calcFunc-scf x (math-trunc n)) | |
2482 (math-overflow n)) | |
2483 (if (math-integerp n) | |
2484 (math-overflow n) | |
2485 (calc-record-why 'integerp n) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2486 (list 'calcFunc-scf x n))))) |
40785 | 2487 |
2488 | |
2489 (defun calcFunc-incr (x &optional step relative-to) | |
2490 (or step (setq step 1)) | |
2491 (cond ((not (Math-integerp step)) | |
2492 (math-reject-arg step 'integerp)) | |
2493 ((Math-integerp x) | |
2494 (math-add x step)) | |
2495 ((eq (car x) 'float) | |
2496 (if (and (math-zerop x) | |
2497 (eq (car-safe relative-to) 'float)) | |
2498 (math-mul step | |
2499 (calcFunc-scf relative-to (- 1 calc-internal-prec))) | |
2500 (math-add-float x (math-make-float | |
2501 step | |
2502 (+ (nth 2 x) | |
2503 (- (math-numdigs (nth 1 x)) | |
2504 calc-internal-prec)))))) | |
2505 ((eq (car x) 'date) | |
2506 (if (Math-integerp (nth 1 x)) | |
2507 (math-add x step) | |
2508 (math-add x (list 'hms 0 0 step)))) | |
2509 (t | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2510 (math-reject-arg x 'realp)))) |
40785 | 2511 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2512 (defsubst calcFunc-decr (x &optional step relative-to) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2513 (calcFunc-incr x (math-neg (or step 1)) relative-to)) |
40785 | 2514 |
2515 (defun calcFunc-percent (x) | |
2516 (if (math-objectp x) | |
2517 (let ((calc-prefer-frac nil)) | |
2518 (math-div x 100)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2519 (list 'calcFunc-percent x))) |
40785 | 2520 |
2521 (defun calcFunc-relch (x y) | |
2522 (if (and (math-objectp x) (math-objectp y)) | |
2523 (math-div (math-sub y x) x) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2524 (list 'calcFunc-relch x y))) |
40785 | 2525 |
2526 ;;; Compute the absolute value squared of A. [F N] [Public] | |
2527 (defun calcFunc-abssqr (a) | |
2528 (cond ((Math-realp a) | |
2529 (math-mul a a)) | |
2530 ((eq (car a) 'cplx) | |
2531 (math-add (math-sqr (nth 1 a)) | |
2532 (math-sqr (nth 2 a)))) | |
2533 ((eq (car a) 'polar) | |
2534 (math-sqr (nth 1 a))) | |
2535 ((and (memq (car a) '(sdev intv)) (math-constp a)) | |
2536 (math-sqr (math-abs a))) | |
2537 ((eq (car a) 'vec) | |
2538 (math-reduce-vec 'math-add (math-map-vec 'calcFunc-abssqr a))) | |
2539 ((math-known-realp a) | |
2540 (math-pow a 2)) | |
2541 ((let ((inf (math-infinitep a))) | |
2542 (and inf | |
2543 (math-mul (calcFunc-abssqr (math-infinite-dir a inf)) inf)))) | |
2544 (t (calc-record-why 'numvecp a) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2545 (list 'calcFunc-abssqr a)))) |
40785 | 2546 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2547 (defsubst math-sqr (a) |
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2548 (math-mul a a)) |
40785 | 2549 |
2550 ;;;; Number theory. | |
2551 | |
2552 (defun calcFunc-idiv (a b) ; [I I I] [Public] | |
2553 (cond ((and (Math-natnump a) (Math-natnump b) (not (eq b 0))) | |
2554 (math-quotient a b)) | |
2555 ((Math-realp a) | |
2556 (if (Math-realp b) | |
2557 (let ((calc-prefer-frac t)) | |
2558 (math-floor (math-div a b))) | |
2559 (math-reject-arg b 'realp))) | |
2560 ((eq (car-safe a) 'hms) | |
2561 (if (eq (car-safe b) 'hms) | |
2562 (let ((calc-prefer-frac t)) | |
2563 (math-floor (math-div a b))) | |
2564 (math-reject-arg b 'hmsp))) | |
2565 ((and (or (eq (car-safe a) 'intv) (Math-realp a)) | |
2566 (or (eq (car-safe b) 'intv) (Math-realp b))) | |
2567 (math-floor (math-div a b))) | |
2568 ((or (math-infinitep a) | |
2569 (math-infinitep b)) | |
2570 (math-div a b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2571 (t (math-reject-arg a 'anglep)))) |
40785 | 2572 |
2573 | |
2574 ;;; Combine two terms being added, if possible. | |
2575 (defun math-combine-sum (a b nega negb scalar-okay) | |
2576 (if (and scalar-okay (Math-objvecp a) (Math-objvecp b)) | |
2577 (math-add-or-sub a b nega negb) | |
2578 (let ((amult 1) (bmult 1)) | |
2579 (and (consp a) | |
2580 (cond ((and (eq (car a) '*) | |
2581 (Math-objectp (nth 1 a))) | |
2582 (setq amult (nth 1 a) | |
2583 a (nth 2 a))) | |
2584 ((and (eq (car a) '/) | |
2585 (Math-objectp (nth 2 a))) | |
2586 (setq amult (if (Math-integerp (nth 2 a)) | |
2587 (list 'frac 1 (nth 2 a)) | |
2588 (math-div 1 (nth 2 a))) | |
2589 a (nth 1 a))) | |
2590 ((eq (car a) 'neg) | |
2591 (setq amult -1 | |
2592 a (nth 1 a))))) | |
2593 (and (consp b) | |
2594 (cond ((and (eq (car b) '*) | |
2595 (Math-objectp (nth 1 b))) | |
2596 (setq bmult (nth 1 b) | |
2597 b (nth 2 b))) | |
2598 ((and (eq (car b) '/) | |
2599 (Math-objectp (nth 2 b))) | |
2600 (setq bmult (if (Math-integerp (nth 2 b)) | |
2601 (list 'frac 1 (nth 2 b)) | |
2602 (math-div 1 (nth 2 b))) | |
2603 b (nth 1 b))) | |
2604 ((eq (car b) 'neg) | |
2605 (setq bmult -1 | |
2606 b (nth 1 b))))) | |
2607 (and (if math-simplifying | |
2608 (Math-equal a b) | |
2609 (equal a b)) | |
2610 (progn | |
2611 (if nega (setq amult (math-neg amult))) | |
2612 (if negb (setq bmult (math-neg bmult))) | |
2613 (setq amult (math-add amult bmult)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2614 (math-mul amult a)))))) |
40785 | 2615 |
2616 (defun math-add-or-sub (a b aneg bneg) | |
2617 (if aneg (setq a (math-neg a))) | |
2618 (if bneg (setq b (math-neg b))) | |
2619 (if (or (Math-vectorp a) (Math-vectorp b)) | |
2620 (math-normalize (list '+ a b)) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2621 (math-add a b))) |
40785 | 2622 |
2623 ;;; The following is expanded out four ways for speed. | |
2624 (defun math-combine-prod (a b inva invb scalar-okay) | |
2625 (cond | |
2626 ((or (and inva (Math-zerop a)) | |
2627 (and invb (Math-zerop b))) | |
2628 nil) | |
2629 ((and scalar-okay (Math-objvecp a) (Math-objvecp b)) | |
2630 (setq a (math-mul-or-div a b inva invb)) | |
2631 (and (Math-objvecp a) | |
2632 a)) | |
2633 ((and (eq (car-safe a) '^) | |
2634 inva | |
2635 (math-looks-negp (nth 2 a))) | |
2636 (math-mul (math-pow (nth 1 a) (math-neg (nth 2 a))) b)) | |
2637 ((and (eq (car-safe b) '^) | |
2638 invb | |
2639 (math-looks-negp (nth 2 b))) | |
2640 (math-mul a (math-pow (nth 1 b) (math-neg (nth 2 b))))) | |
2641 (t (let ((apow 1) (bpow 1)) | |
2642 (and (consp a) | |
2643 (cond ((and (eq (car a) '^) | |
2644 (or math-simplifying | |
2645 (Math-numberp (nth 2 a)))) | |
2646 (setq apow (nth 2 a) | |
2647 a (nth 1 a))) | |
2648 ((eq (car a) 'calcFunc-sqrt) | |
2649 (setq apow '(frac 1 2) | |
2650 a (nth 1 a))) | |
2651 ((and (eq (car a) 'calcFunc-exp) | |
2652 (or math-simplifying | |
2653 (Math-numberp (nth 1 a)))) | |
2654 (setq apow (nth 1 a) | |
2655 a math-combine-prod-e)))) | |
2656 (and (consp a) (eq (car a) 'frac) | |
2657 (Math-lessp (nth 1 a) (nth 2 a)) | |
2658 (setq a (math-div 1 a) apow (math-neg apow))) | |
2659 (and (consp b) | |
2660 (cond ((and (eq (car b) '^) | |
2661 (or math-simplifying | |
2662 (Math-numberp (nth 2 b)))) | |
2663 (setq bpow (nth 2 b) | |
2664 b (nth 1 b))) | |
2665 ((eq (car b) 'calcFunc-sqrt) | |
2666 (setq bpow '(frac 1 2) | |
2667 b (nth 1 b))) | |
2668 ((and (eq (car b) 'calcFunc-exp) | |
2669 (or math-simplifying | |
2670 (Math-numberp (nth 1 b)))) | |
2671 (setq bpow (nth 1 b) | |
2672 b math-combine-prod-e)))) | |
2673 (and (consp b) (eq (car b) 'frac) | |
2674 (Math-lessp (nth 1 b) (nth 2 b)) | |
2675 (setq b (math-div 1 b) bpow (math-neg bpow))) | |
2676 (if inva (setq apow (math-neg apow))) | |
2677 (if invb (setq bpow (math-neg bpow))) | |
2678 (or (and (if math-simplifying | |
2679 (math-commutative-equal a b) | |
2680 (equal a b)) | |
2681 (let ((sumpow (math-add apow bpow))) | |
2682 (and (or (not (Math-integerp a)) | |
2683 (Math-zerop sumpow) | |
2684 (eq (eq (car-safe apow) 'frac) | |
2685 (eq (car-safe bpow) 'frac))) | |
2686 (progn | |
2687 (and (math-looks-negp sumpow) | |
2688 (Math-ratp a) (Math-posp a) | |
2689 (setq a (math-div 1 a) | |
2690 sumpow (math-neg sumpow))) | |
2691 (cond ((equal sumpow '(frac 1 2)) | |
2692 (list 'calcFunc-sqrt a)) | |
2693 ((equal sumpow '(frac -1 2)) | |
2694 (math-div 1 (list 'calcFunc-sqrt a))) | |
2695 ((and (eq a math-combine-prod-e) | |
2696 (eq a b)) | |
2697 (list 'calcFunc-exp sumpow)) | |
2698 (t | |
2699 (condition-case err | |
2700 (math-pow a sumpow) | |
2701 (inexact-result (list '^ a sumpow))))))))) | |
2702 (and math-simplifying-units | |
2703 math-combining-units | |
2704 (let* ((ua (math-check-unit-name a)) | |
2705 ub) | |
2706 (and ua | |
2707 (eq ua (setq ub (math-check-unit-name b))) | |
2708 (progn | |
2709 (setq ua (if (eq (nth 1 a) (car ua)) | |
2710 1 | |
2711 (nth 1 (assq (aref (symbol-name (nth 1 a)) | |
2712 0) | |
2713 math-unit-prefixes))) | |
2714 ub (if (eq (nth 1 b) (car ub)) | |
2715 1 | |
2716 (nth 1 (assq (aref (symbol-name (nth 1 b)) | |
2717 0) | |
2718 math-unit-prefixes)))) | |
2719 (if (Math-lessp ua ub) | |
2720 (let (temp) | |
2721 (setq temp a a b b temp | |
2722 temp ua ua ub ub temp | |
2723 temp apow apow bpow bpow temp))) | |
2724 (math-mul (math-pow (math-div ua ub) apow) | |
2725 (math-pow b (math-add apow bpow))))))) | |
2726 (and (equal apow bpow) | |
2727 (Math-natnump a) (Math-natnump b) | |
2728 (cond ((equal apow '(frac 1 2)) | |
2729 (list 'calcFunc-sqrt (math-mul a b))) | |
2730 ((equal apow '(frac -1 2)) | |
2731 (math-div 1 (list 'calcFunc-sqrt (math-mul a b)))) | |
2732 (t | |
2733 (setq a (math-mul a b)) | |
2734 (condition-case err | |
2735 (math-pow a apow) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2736 (inexact-result (list '^ a apow))))))))))) |
40785 | 2737 (setq math-combine-prod-e '(var e var-e)) |
2738 | |
2739 (defun math-mul-or-div (a b ainv binv) | |
2740 (if (or (Math-vectorp a) (Math-vectorp b)) | |
2741 (math-normalize | |
2742 (if ainv | |
2743 (if binv | |
2744 (list '/ (math-div 1 a) b) | |
2745 (list '/ b a)) | |
2746 (if binv | |
2747 (list '/ a b) | |
2748 (list '* a b)))) | |
2749 (if ainv | |
2750 (if binv | |
2751 (math-div (math-div 1 a) b) | |
2752 (math-div b a)) | |
2753 (if binv | |
2754 (math-div a b) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2755 (math-mul a b))))) |
40785 | 2756 |
2757 (defun math-commutative-equal (a b) | |
2758 (if (memq (car-safe a) '(+ -)) | |
2759 (and (memq (car-safe b) '(+ -)) | |
2760 (let ((bterms nil) aterms p) | |
2761 (math-commutative-collect b nil) | |
2762 (setq aterms bterms bterms nil) | |
2763 (math-commutative-collect a nil) | |
2764 (and (= (length aterms) (length bterms)) | |
2765 (progn | |
2766 (while (and aterms | |
2767 (progn | |
2768 (setq p bterms) | |
2769 (while (and p (not (equal (car aterms) | |
2770 (car p)))) | |
2771 (setq p (cdr p))) | |
2772 p)) | |
2773 (setq bterms (delq (car p) bterms) | |
2774 aterms (cdr aterms))) | |
2775 (not aterms))))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2776 (equal a b))) |
40785 | 2777 |
2778 (defun math-commutative-collect (b neg) | |
2779 (if (eq (car-safe b) '+) | |
2780 (progn | |
2781 (math-commutative-collect (nth 1 b) neg) | |
2782 (math-commutative-collect (nth 2 b) neg)) | |
2783 (if (eq (car-safe b) '-) | |
2784 (progn | |
2785 (math-commutative-collect (nth 1 b) neg) | |
2786 (math-commutative-collect (nth 2 b) (not neg))) | |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2787 (setq bterms (cons (if neg (math-neg b) b) bterms))))) |
40785 | 2788 |
41041
45130b458dac
(calcFunc-abs, calcFunc-float, calcFunc-ceil, calcFunc-round): Use
Colin Walters <walters@gnu.org>
parents:
40785
diff
changeset
|
2789 ;;; calc-arith.el ends here |