Commit | Line | Data |
---|---|---|
3132f345 CW |
1 | ;;; calc-math.el --- mathematical functions for Calc |
2 | ||
491c3062 | 3 | ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc. |
3132f345 CW |
4 | |
5 | ;; Author: David Gillespie <daveg@synaptics.com> | |
6 | ;; Maintainer: Colin Walters <walters@debian.org> | |
136211a9 EZ |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
10 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
11 | ;; but WITHOUT ANY WARRANTY. No author or distributor | |
12 | ;; accepts responsibility to anyone for the consequences of using it | |
13 | ;; or for whether it serves any particular purpose or works at all, | |
14 | ;; unless he says so in writing. Refer to the GNU Emacs General Public | |
15 | ;; License for full details. | |
16 | ||
17 | ;; Everyone is granted permission to copy, modify and redistribute | |
18 | ;; GNU Emacs, but only under the conditions described in the | |
19 | ;; GNU Emacs General Public License. A copy of this license is | |
20 | ;; supposed to have been given to you along with GNU Emacs so you | |
21 | ;; can know your rights and responsibilities. It should be in a | |
22 | ;; file named COPYING. Among other things, the copyright notice | |
23 | ;; and this notice must be preserved on all copies. | |
24 | ||
3132f345 | 25 | ;;; Commentary: |
136211a9 | 26 | |
3132f345 | 27 | ;;; Code: |
136211a9 EZ |
28 | |
29 | ;; This file is autoloaded from calc-ext.el. | |
30 | (require 'calc-ext) | |
31 | ||
32 | (require 'calc-macs) | |
33 | ||
34 | (defun calc-Need-calc-math () nil) | |
35 | ||
36 | ||
37 | (defun calc-sqrt (arg) | |
38 | (interactive "P") | |
39 | (calc-slow-wrapper | |
40 | (if (calc-is-inverse) | |
41 | (calc-unary-op "^2" 'calcFunc-sqr arg) | |
491c3062 | 42 | (calc-unary-op "sqrt" 'calcFunc-sqrt arg)))) |
136211a9 EZ |
43 | |
44 | (defun calc-isqrt (arg) | |
45 | (interactive "P") | |
46 | (calc-slow-wrapper | |
47 | (if (calc-is-inverse) | |
48 | (calc-unary-op "^2" 'calcFunc-sqr arg) | |
491c3062 | 49 | (calc-unary-op "isqt" 'calcFunc-isqrt arg)))) |
136211a9 EZ |
50 | |
51 | ||
52 | (defun calc-hypot (arg) | |
53 | (interactive "P") | |
54 | (calc-slow-wrapper | |
491c3062 | 55 | (calc-binary-op "hypt" 'calcFunc-hypot arg))) |
136211a9 EZ |
56 | |
57 | (defun calc-ln (arg) | |
58 | (interactive "P") | |
59 | (calc-invert-func) | |
491c3062 | 60 | (calc-exp arg)) |
136211a9 EZ |
61 | |
62 | (defun calc-log10 (arg) | |
63 | (interactive "P") | |
64 | (calc-hyperbolic-func) | |
491c3062 | 65 | (calc-ln arg)) |
136211a9 EZ |
66 | |
67 | (defun calc-log (arg) | |
68 | (interactive "P") | |
69 | (calc-slow-wrapper | |
70 | (if (calc-is-inverse) | |
71 | (calc-binary-op "alog" 'calcFunc-alog arg) | |
491c3062 | 72 | (calc-binary-op "log" 'calcFunc-log arg)))) |
136211a9 EZ |
73 | |
74 | (defun calc-ilog (arg) | |
75 | (interactive "P") | |
76 | (calc-slow-wrapper | |
77 | (if (calc-is-inverse) | |
78 | (calc-binary-op "alog" 'calcFunc-alog arg) | |
491c3062 | 79 | (calc-binary-op "ilog" 'calcFunc-ilog arg)))) |
136211a9 EZ |
80 | |
81 | (defun calc-lnp1 (arg) | |
82 | (interactive "P") | |
83 | (calc-invert-func) | |
491c3062 | 84 | (calc-expm1 arg)) |
136211a9 EZ |
85 | |
86 | (defun calc-exp (arg) | |
87 | (interactive "P") | |
88 | (calc-slow-wrapper | |
89 | (if (calc-is-hyperbolic) | |
90 | (if (calc-is-inverse) | |
91 | (calc-unary-op "lg10" 'calcFunc-log10 arg) | |
92 | (calc-unary-op "10^" 'calcFunc-exp10 arg)) | |
93 | (if (calc-is-inverse) | |
94 | (calc-unary-op "ln" 'calcFunc-ln arg) | |
491c3062 | 95 | (calc-unary-op "exp" 'calcFunc-exp arg))))) |
136211a9 EZ |
96 | |
97 | (defun calc-expm1 (arg) | |
98 | (interactive "P") | |
99 | (calc-slow-wrapper | |
100 | (if (calc-is-inverse) | |
101 | (calc-unary-op "ln+1" 'calcFunc-lnp1 arg) | |
491c3062 | 102 | (calc-unary-op "ex-1" 'calcFunc-expm1 arg)))) |
136211a9 EZ |
103 | |
104 | (defun calc-pi () | |
105 | (interactive) | |
106 | (calc-slow-wrapper | |
107 | (if (calc-is-inverse) | |
108 | (if (calc-is-hyperbolic) | |
109 | (if calc-symbolic-mode | |
110 | (calc-pop-push-record 0 "phi" '(var phi var-phi)) | |
111 | (calc-pop-push-record 0 "phi" (math-phi))) | |
112 | (if calc-symbolic-mode | |
113 | (calc-pop-push-record 0 "gmma" '(var gamma var-gamma)) | |
114 | (calc-pop-push-record 0 "gmma" (math-gamma-const)))) | |
115 | (if (calc-is-hyperbolic) | |
116 | (if calc-symbolic-mode | |
117 | (calc-pop-push-record 0 "e" '(var e var-e)) | |
118 | (calc-pop-push-record 0 "e" (math-e))) | |
119 | (if calc-symbolic-mode | |
120 | (calc-pop-push-record 0 "pi" '(var pi var-pi)) | |
491c3062 | 121 | (calc-pop-push-record 0 "pi" (math-pi))))))) |
136211a9 EZ |
122 | |
123 | (defun calc-sin (arg) | |
124 | (interactive "P") | |
125 | (calc-slow-wrapper | |
126 | (if (calc-is-hyperbolic) | |
127 | (if (calc-is-inverse) | |
128 | (calc-unary-op "asnh" 'calcFunc-arcsinh arg) | |
129 | (calc-unary-op "sinh" 'calcFunc-sinh arg)) | |
130 | (if (calc-is-inverse) | |
131 | (calc-unary-op "asin" 'calcFunc-arcsin arg) | |
491c3062 | 132 | (calc-unary-op "sin" 'calcFunc-sin arg))))) |
136211a9 EZ |
133 | |
134 | (defun calc-arcsin (arg) | |
135 | (interactive "P") | |
136 | (calc-invert-func) | |
491c3062 | 137 | (calc-sin arg)) |
136211a9 EZ |
138 | |
139 | (defun calc-sinh (arg) | |
140 | (interactive "P") | |
141 | (calc-hyperbolic-func) | |
491c3062 | 142 | (calc-sin arg)) |
136211a9 EZ |
143 | |
144 | (defun calc-arcsinh (arg) | |
145 | (interactive "P") | |
146 | (calc-invert-func) | |
147 | (calc-hyperbolic-func) | |
491c3062 | 148 | (calc-sin arg)) |
136211a9 EZ |
149 | |
150 | (defun calc-cos (arg) | |
151 | (interactive "P") | |
152 | (calc-slow-wrapper | |
153 | (if (calc-is-hyperbolic) | |
154 | (if (calc-is-inverse) | |
155 | (calc-unary-op "acsh" 'calcFunc-arccosh arg) | |
156 | (calc-unary-op "cosh" 'calcFunc-cosh arg)) | |
157 | (if (calc-is-inverse) | |
158 | (calc-unary-op "acos" 'calcFunc-arccos arg) | |
491c3062 | 159 | (calc-unary-op "cos" 'calcFunc-cos arg))))) |
136211a9 EZ |
160 | |
161 | (defun calc-arccos (arg) | |
162 | (interactive "P") | |
163 | (calc-invert-func) | |
491c3062 | 164 | (calc-cos arg)) |
136211a9 EZ |
165 | |
166 | (defun calc-cosh (arg) | |
167 | (interactive "P") | |
168 | (calc-hyperbolic-func) | |
491c3062 | 169 | (calc-cos arg)) |
136211a9 EZ |
170 | |
171 | (defun calc-arccosh (arg) | |
172 | (interactive "P") | |
173 | (calc-invert-func) | |
174 | (calc-hyperbolic-func) | |
491c3062 | 175 | (calc-cos arg)) |
136211a9 EZ |
176 | |
177 | (defun calc-sincos () | |
178 | (interactive) | |
179 | (calc-slow-wrapper | |
180 | (if (calc-is-inverse) | |
181 | (calc-enter-result 1 "asnc" (list 'calcFunc-arcsincos (calc-top-n 1))) | |
491c3062 | 182 | (calc-enter-result 1 "sncs" (list 'calcFunc-sincos (calc-top-n 1)))))) |
136211a9 EZ |
183 | |
184 | (defun calc-tan (arg) | |
185 | (interactive "P") | |
186 | (calc-slow-wrapper | |
187 | (if (calc-is-hyperbolic) | |
188 | (if (calc-is-inverse) | |
189 | (calc-unary-op "atnh" 'calcFunc-arctanh arg) | |
190 | (calc-unary-op "tanh" 'calcFunc-tanh arg)) | |
191 | (if (calc-is-inverse) | |
192 | (calc-unary-op "atan" 'calcFunc-arctan arg) | |
491c3062 | 193 | (calc-unary-op "tan" 'calcFunc-tan arg))))) |
136211a9 EZ |
194 | |
195 | (defun calc-arctan (arg) | |
196 | (interactive "P") | |
197 | (calc-invert-func) | |
491c3062 | 198 | (calc-tan arg)) |
136211a9 EZ |
199 | |
200 | (defun calc-tanh (arg) | |
201 | (interactive "P") | |
202 | (calc-hyperbolic-func) | |
491c3062 | 203 | (calc-tan arg)) |
136211a9 EZ |
204 | |
205 | (defun calc-arctanh (arg) | |
206 | (interactive "P") | |
207 | (calc-invert-func) | |
208 | (calc-hyperbolic-func) | |
491c3062 | 209 | (calc-tan arg)) |
136211a9 EZ |
210 | |
211 | (defun calc-arctan2 () | |
212 | (interactive) | |
213 | (calc-slow-wrapper | |
491c3062 | 214 | (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2))))) |
136211a9 EZ |
215 | |
216 | (defun calc-conj (arg) | |
217 | (interactive "P") | |
218 | (calc-wrapper | |
491c3062 | 219 | (calc-unary-op "conj" 'calcFunc-conj arg))) |
136211a9 EZ |
220 | |
221 | (defun calc-imaginary () | |
222 | (interactive) | |
223 | (calc-slow-wrapper | |
491c3062 | 224 | (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1))))) |
136211a9 EZ |
225 | |
226 | ||
227 | ||
228 | (defun calc-to-degrees (arg) | |
229 | (interactive "P") | |
230 | (calc-wrapper | |
491c3062 | 231 | (calc-unary-op ">deg" 'calcFunc-deg arg))) |
136211a9 EZ |
232 | |
233 | (defun calc-to-radians (arg) | |
234 | (interactive "P") | |
235 | (calc-wrapper | |
491c3062 | 236 | (calc-unary-op ">rad" 'calcFunc-rad arg))) |
136211a9 EZ |
237 | |
238 | ||
239 | (defun calc-degrees-mode (arg) | |
240 | (interactive "p") | |
241 | (cond ((= arg 1) | |
242 | (calc-wrapper | |
243 | (calc-change-mode 'calc-angle-mode 'deg) | |
3132f345 | 244 | (message "Angles measured in degrees"))) |
136211a9 EZ |
245 | ((= arg 2) (calc-radians-mode)) |
246 | ((= arg 3) (calc-hms-mode)) | |
491c3062 | 247 | (t (error "Prefix argument out of range")))) |
136211a9 EZ |
248 | |
249 | (defun calc-radians-mode () | |
250 | (interactive) | |
251 | (calc-wrapper | |
252 | (calc-change-mode 'calc-angle-mode 'rad) | |
3132f345 | 253 | (message "Angles measured in radians"))) |
136211a9 EZ |
254 | |
255 | ||
256 | ;;; Compute the integer square-root floor(sqrt(A)). A > 0. [I I] [Public] | |
257 | ;;; This method takes advantage of the fact that Newton's method starting | |
258 | ;;; with an overestimate always works, even using truncating integer division! | |
259 | (defun math-isqrt (a) | |
260 | (cond ((Math-zerop a) a) | |
261 | ((not (math-natnump a)) | |
262 | (math-reject-arg a 'natnump)) | |
263 | ((integerp a) | |
264 | (math-isqrt-small a)) | |
265 | (t | |
491c3062 | 266 | (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a)))))))) |
136211a9 EZ |
267 | |
268 | (defun calcFunc-isqrt (a) | |
269 | (if (math-realp a) | |
270 | (math-isqrt (math-floor a)) | |
491c3062 | 271 | (math-floor (math-sqrt a)))) |
136211a9 EZ |
272 | |
273 | ||
274 | ;;; This returns (flag . result) where the flag is T if A is a perfect square. | |
275 | (defun math-isqrt-bignum (a) ; [P.l L] | |
276 | (let ((len (length a))) | |
277 | (if (= (% len 2) 0) | |
278 | (let* ((top (nthcdr (- len 2) a))) | |
279 | (math-isqrt-bignum-iter | |
280 | a | |
281 | (math-scale-bignum-3 | |
282 | (math-bignum-big | |
283 | (1+ (math-isqrt-small | |
284 | (+ (* (nth 1 top) 1000) (car top))))) | |
285 | (1- (/ len 2))))) | |
286 | (let* ((top (nth (1- len) a))) | |
287 | (math-isqrt-bignum-iter | |
288 | a | |
289 | (math-scale-bignum-3 | |
290 | (list (1+ (math-isqrt-small top))) | |
491c3062 | 291 | (/ len 2))))))) |
136211a9 EZ |
292 | |
293 | (defun math-isqrt-bignum-iter (a guess) ; [l L l] | |
294 | (math-working "isqrt" (cons 'bigpos guess)) | |
295 | (let* ((q (math-div-bignum a guess)) | |
296 | (s (math-add-bignum (car q) guess)) | |
297 | (g2 (math-div2-bignum s)) | |
298 | (comp (math-compare-bignum g2 guess))) | |
299 | (if (< comp 0) | |
300 | (math-isqrt-bignum-iter a g2) | |
301 | (cons (and (= comp 0) | |
302 | (math-zerop-bignum (cdr q)) | |
303 | (= (% (car s) 2) 0)) | |
491c3062 | 304 | guess)))) |
136211a9 EZ |
305 | |
306 | (defun math-zerop-bignum (a) | |
307 | (and (eq (car a) 0) | |
308 | (progn | |
309 | (while (eq (car (setq a (cdr a))) 0)) | |
491c3062 | 310 | (null a)))) |
136211a9 EZ |
311 | |
312 | (defun math-scale-bignum-3 (a n) ; [L L S] | |
313 | (while (> n 0) | |
314 | (setq a (cons 0 a) | |
315 | n (1- n))) | |
491c3062 | 316 | a) |
136211a9 EZ |
317 | |
318 | (defun math-isqrt-small (a) ; A > 0. [S S] | |
319 | (let ((g (cond ((>= a 10000) 1000) | |
320 | ((>= a 100) 100) | |
321 | (t 10))) | |
322 | g2) | |
323 | (while (< (setq g2 (/ (+ g (/ a g)) 2)) g) | |
324 | (setq g g2)) | |
491c3062 | 325 | g)) |
136211a9 EZ |
326 | |
327 | ||
328 | ||
329 | ||
330 | ;;; Compute the square root of a number. | |
331 | ;;; [T N] if possible, else [F N] if possible, else [C N]. [Public] | |
332 | (defun math-sqrt (a) | |
333 | (or | |
334 | (and (Math-zerop a) a) | |
335 | (and (math-known-nonposp a) | |
336 | (math-imaginary (math-sqrt (math-neg a)))) | |
337 | (and (integerp a) | |
338 | (let ((sqrt (math-isqrt-small a))) | |
339 | (if (= (* sqrt sqrt) a) | |
340 | sqrt | |
341 | (if calc-symbolic-mode | |
342 | (list 'calcFunc-sqrt a) | |
343 | (math-sqrt-float (math-float a) (math-float sqrt)))))) | |
344 | (and (eq (car-safe a) 'bigpos) | |
345 | (let* ((res (math-isqrt-bignum (cdr a))) | |
346 | (sqrt (math-normalize (cons 'bigpos (cdr res))))) | |
347 | (if (car res) | |
348 | sqrt | |
349 | (if calc-symbolic-mode | |
350 | (list 'calcFunc-sqrt a) | |
351 | (math-sqrt-float (math-float a) (math-float sqrt)))))) | |
352 | (and (eq (car-safe a) 'frac) | |
353 | (let* ((num-res (math-isqrt-bignum (cdr (Math-bignum-test (nth 1 a))))) | |
354 | (num-sqrt (math-normalize (cons 'bigpos (cdr num-res)))) | |
355 | (den-res (math-isqrt-bignum (cdr (Math-bignum-test (nth 2 a))))) | |
356 | (den-sqrt (math-normalize (cons 'bigpos (cdr den-res))))) | |
357 | (if (and (car num-res) (car den-res)) | |
358 | (list 'frac num-sqrt den-sqrt) | |
359 | (if calc-symbolic-mode | |
360 | (if (or (car num-res) (car den-res)) | |
361 | (math-div (if (car num-res) | |
362 | num-sqrt (list 'calcFunc-sqrt (nth 1 a))) | |
363 | (if (car den-res) | |
364 | den-sqrt (list 'calcFunc-sqrt (nth 2 a)))) | |
365 | (list 'calcFunc-sqrt a)) | |
366 | (math-sqrt-float (math-float a) | |
367 | (math-div (math-float num-sqrt) den-sqrt)))))) | |
368 | (and (eq (car-safe a) 'float) | |
369 | (if calc-symbolic-mode | |
370 | (if (= (% (nth 2 a) 2) 0) | |
371 | (let ((res (math-isqrt-bignum | |
372 | (cdr (Math-bignum-test (nth 1 a)))))) | |
373 | (if (car res) | |
374 | (math-make-float (math-normalize | |
375 | (cons 'bigpos (cdr res))) | |
376 | (/ (nth 2 a) 2)) | |
377 | (signal 'inexact-result nil))) | |
378 | (signal 'inexact-result nil)) | |
379 | (math-sqrt-float a))) | |
380 | (and (eq (car-safe a) 'cplx) | |
381 | (math-with-extra-prec 2 | |
382 | (let* ((d (math-abs a)) | |
383 | (imag (math-sqrt (math-mul (math-sub d (nth 1 a)) | |
384 | '(float 5 -1))))) | |
385 | (list 'cplx | |
386 | (math-sqrt (math-mul (math-add d (nth 1 a)) '(float 5 -1))) | |
387 | (if (math-negp (nth 2 a)) (math-neg imag) imag))))) | |
388 | (and (eq (car-safe a) 'polar) | |
389 | (list 'polar | |
390 | (math-sqrt (nth 1 a)) | |
391 | (math-mul (nth 2 a) '(float 5 -1)))) | |
392 | (and (eq (car-safe a) 'sdev) | |
393 | (let ((sqrt (math-sqrt (nth 1 a)))) | |
394 | (math-make-sdev sqrt | |
395 | (math-div (nth 2 a) (math-mul sqrt 2))))) | |
396 | (and (eq (car-safe a) 'intv) | |
397 | (not (math-negp (nth 2 a))) | |
398 | (math-make-intv (nth 1 a) (math-sqrt (nth 2 a)) (math-sqrt (nth 3 a)))) | |
399 | (and (eq (car-safe a) '*) | |
400 | (or (math-known-nonnegp (nth 1 a)) | |
401 | (math-known-nonnegp (nth 2 a))) | |
402 | (math-mul (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a)))) | |
403 | (and (eq (car-safe a) '/) | |
404 | (or (and (math-known-nonnegp (nth 2 a)) | |
405 | (math-div (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a)))) | |
406 | (and (math-known-nonnegp (nth 1 a)) | |
407 | (not (math-equal-int (nth 1 a) 1)) | |
408 | (math-mul (math-sqrt (nth 1 a)) | |
409 | (math-sqrt (math-div 1 (nth 2 a))))))) | |
410 | (and (eq (car-safe a) '^) | |
411 | (math-known-evenp (nth 2 a)) | |
412 | (math-known-realp (nth 1 a)) | |
413 | (math-abs (math-pow (nth 1 a) (math-div (nth 2 a) 2)))) | |
414 | (let ((inf (math-infinitep a))) | |
415 | (and inf | |
416 | (math-mul (math-sqrt (math-infinite-dir a inf)) inf))) | |
417 | (progn | |
418 | (calc-record-why 'numberp a) | |
491c3062 | 419 | (list 'calcFunc-sqrt a)))) |
3132f345 | 420 | (defalias 'calcFunc-sqrt 'math-sqrt) |
136211a9 EZ |
421 | |
422 | (defun math-infinite-dir (a &optional inf) | |
423 | (or inf (setq inf (math-infinitep a))) | |
491c3062 | 424 | (math-normalize (math-expr-subst a inf 1))) |
136211a9 EZ |
425 | |
426 | (defun math-sqrt-float (a &optional guess) ; [F F F] | |
427 | (if calc-symbolic-mode | |
428 | (signal 'inexact-result nil) | |
491c3062 | 429 | (math-with-extra-prec 1 (math-sqrt-raw a guess)))) |
136211a9 EZ |
430 | |
431 | (defun math-sqrt-raw (a &optional guess) ; [F F F] | |
432 | (if (not (Math-posp a)) | |
433 | (math-sqrt a) | |
434 | (if (null guess) | |
435 | (let ((ldiff (- (math-numdigs (nth 1 a)) 6))) | |
436 | (or (= (% (+ (nth 2 a) ldiff) 2) 0) (setq ldiff (1+ ldiff))) | |
437 | (setq guess (math-make-float (math-isqrt-small | |
438 | (math-scale-int (nth 1 a) (- ldiff))) | |
439 | (/ (+ (nth 2 a) ldiff) 2))))) | |
491c3062 | 440 | (math-sqrt-float-iter a guess))) |
136211a9 EZ |
441 | |
442 | (defun math-sqrt-float-iter (a guess) ; [F F F] | |
443 | (math-working "sqrt" guess) | |
444 | (let ((g2 (math-mul-float (math-add-float guess (math-div-float a guess)) | |
445 | '(float 5 -1)))) | |
446 | (if (math-nearly-equal-float g2 guess) | |
447 | g2 | |
491c3062 | 448 | (math-sqrt-float-iter a g2)))) |
136211a9 EZ |
449 | |
450 | ;;; True if A and B differ only in the last digit of precision. [P F F] | |
451 | (defun math-nearly-equal-float (a b) | |
452 | (let ((ediff (- (nth 2 a) (nth 2 b)))) | |
453 | (cond ((= ediff 0) ;; Expanded out for speed | |
454 | (setq ediff (math-add (Math-integer-neg (nth 1 a)) (nth 1 b))) | |
455 | (or (eq ediff 0) | |
456 | (and (not (consp ediff)) | |
457 | (< ediff 10) | |
458 | (> ediff -10) | |
459 | (= (math-numdigs (nth 1 a)) calc-internal-prec)))) | |
460 | ((= ediff 1) | |
461 | (setq ediff (math-add (Math-integer-neg (nth 1 b)) | |
462 | (math-scale-int (nth 1 a) 1))) | |
463 | (and (not (consp ediff)) | |
464 | (< ediff 10) | |
465 | (> ediff -10) | |
466 | (= (math-numdigs (nth 1 b)) calc-internal-prec))) | |
467 | ((= ediff -1) | |
468 | (setq ediff (math-add (Math-integer-neg (nth 1 a)) | |
469 | (math-scale-int (nth 1 b) 1))) | |
470 | (and (not (consp ediff)) | |
471 | (< ediff 10) | |
472 | (> ediff -10) | |
491c3062 | 473 | (= (math-numdigs (nth 1 a)) calc-internal-prec)))))) |
136211a9 EZ |
474 | |
475 | (defun math-nearly-equal (a b) ; [P N N] [Public] | |
476 | (setq a (math-float a)) | |
477 | (setq b (math-float b)) | |
478 | (if (eq (car a) 'polar) (setq a (math-complex a))) | |
479 | (if (eq (car b) 'polar) (setq b (math-complex b))) | |
480 | (if (eq (car a) 'cplx) | |
481 | (if (eq (car b) 'cplx) | |
482 | (and (or (math-nearly-equal-float (nth 1 a) (nth 1 b)) | |
483 | (and (math-nearly-zerop-float (nth 1 a) (nth 2 a)) | |
484 | (math-nearly-zerop-float (nth 1 b) (nth 2 b)))) | |
485 | (or (math-nearly-equal-float (nth 2 a) (nth 2 b)) | |
486 | (and (math-nearly-zerop-float (nth 2 a) (nth 1 a)) | |
487 | (math-nearly-zerop-float (nth 2 b) (nth 1 b))))) | |
488 | (and (math-nearly-equal-float (nth 1 a) b) | |
489 | (math-nearly-zerop-float (nth 2 a) b))) | |
490 | (if (eq (car b) 'cplx) | |
491 | (and (math-nearly-equal-float a (nth 1 b)) | |
492 | (math-nearly-zerop-float a (nth 2 b))) | |
491c3062 | 493 | (math-nearly-equal-float a b)))) |
136211a9 EZ |
494 | |
495 | ;;; True if A is nearly zero compared to B. [P F F] | |
496 | (defun math-nearly-zerop-float (a b) | |
497 | (or (eq (nth 1 a) 0) | |
498 | (<= (+ (math-numdigs (nth 1 a)) (nth 2 a)) | |
491c3062 | 499 | (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec))))) |
136211a9 EZ |
500 | |
501 | (defun math-nearly-zerop (a b) ; [P N R] [Public] | |
502 | (setq a (math-float a)) | |
503 | (setq b (math-float b)) | |
504 | (if (eq (car a) 'cplx) | |
505 | (and (math-nearly-zerop-float (nth 1 a) b) | |
506 | (math-nearly-zerop-float (nth 2 a) b)) | |
507 | (if (eq (car a) 'polar) | |
508 | (math-nearly-zerop-float (nth 1 a) b) | |
491c3062 | 509 | (math-nearly-zerop-float a b)))) |
136211a9 EZ |
510 | |
511 | ;;; This implementation could be improved, accuracy-wise. | |
512 | (defun math-hypot (a b) | |
513 | (cond ((Math-zerop a) (math-abs b)) | |
514 | ((Math-zerop b) (math-abs a)) | |
515 | ((not (Math-scalarp a)) | |
516 | (if (math-infinitep a) | |
517 | (if (math-infinitep b) | |
518 | (if (equal a b) | |
519 | a | |
520 | '(var nan var-nan)) | |
521 | a) | |
522 | (calc-record-why 'scalarp a) | |
523 | (list 'calcFunc-hypot a b))) | |
524 | ((not (Math-scalarp b)) | |
525 | (if (math-infinitep b) | |
526 | b | |
527 | (calc-record-why 'scalarp b) | |
528 | (list 'calcFunc-hypot a b))) | |
529 | ((and (Math-numberp a) (Math-numberp b)) | |
530 | (math-with-extra-prec 1 | |
531 | (math-sqrt (math-add (calcFunc-abssqr a) (calcFunc-abssqr b))))) | |
532 | ((eq (car-safe a) 'hms) | |
533 | (if (eq (car-safe b) 'hms) ; this helps sdev's of hms forms | |
534 | (math-to-hms (math-hypot (math-from-hms a 'deg) | |
535 | (math-from-hms b 'deg))) | |
536 | (math-to-hms (math-hypot (math-from-hms a 'deg) b)))) | |
537 | ((eq (car-safe b) 'hms) | |
538 | (math-to-hms (math-hypot a (math-from-hms b 'deg)))) | |
491c3062 | 539 | (t nil))) |
3132f345 | 540 | (defalias 'calcFunc-hypot 'math-hypot) |
136211a9 EZ |
541 | |
542 | (defun calcFunc-sqr (x) | |
491c3062 | 543 | (math-pow x 2)) |
136211a9 EZ |
544 | |
545 | ||
546 | ||
547 | (defun math-nth-root (a n) | |
548 | (cond ((= n 2) (math-sqrt a)) | |
549 | ((Math-zerop a) a) | |
550 | ((Math-negp a) nil) | |
551 | ((Math-integerp a) | |
552 | (let ((root (math-nth-root-integer a n))) | |
553 | (if (car root) | |
554 | (cdr root) | |
555 | (and (not calc-symbolic-mode) | |
556 | (math-nth-root-float (math-float a) n | |
557 | (math-float (cdr root))))))) | |
558 | ((eq (car-safe a) 'frac) | |
559 | (let* ((num-root (math-nth-root-integer (nth 1 a) n)) | |
560 | (den-root (math-nth-root-integer (nth 2 a) n))) | |
561 | (if (and (car num-root) (car den-root)) | |
562 | (list 'frac (cdr num-root) (cdr den-root)) | |
563 | (and (not calc-symbolic-mode) | |
564 | (math-nth-root-float | |
565 | (math-float a) n | |
566 | (math-div-float (math-float (cdr num-root)) | |
567 | (math-float (cdr den-root)))))))) | |
568 | ((eq (car-safe a) 'float) | |
569 | (and (not calc-symbolic-mode) | |
570 | (math-nth-root-float a n))) | |
571 | ((eq (car-safe a) 'polar) | |
572 | (let ((root (math-nth-root (nth 1 a) n))) | |
573 | (and root (list 'polar root (math-div (nth 2 a) n))))) | |
491c3062 | 574 | (t nil))) |
136211a9 EZ |
575 | |
576 | (defun math-nth-root-float (a n &optional guess) | |
577 | (math-inexact-result) | |
578 | (math-with-extra-prec 1 | |
579 | (let ((nf (math-float n)) | |
580 | (nfm1 (math-float (1- n)))) | |
581 | (math-nth-root-float-iter a (or guess | |
582 | (math-make-float | |
583 | 1 (/ (+ (math-numdigs (nth 1 a)) | |
584 | (nth 2 a) | |
585 | (/ n 2)) | |
491c3062 | 586 | n))))))) |
136211a9 EZ |
587 | |
588 | (defun math-nth-root-float-iter (a guess) ; uses "n", "nf", "nfm1" | |
589 | (math-working "root" guess) | |
590 | (let ((g2 (math-div-float (math-add-float (math-mul nfm1 guess) | |
591 | (math-div-float | |
592 | a (math-ipow guess (1- n)))) | |
593 | nf))) | |
594 | (if (math-nearly-equal-float g2 guess) | |
595 | g2 | |
491c3062 | 596 | (math-nth-root-float-iter a g2)))) |
136211a9 EZ |
597 | |
598 | (defun math-nth-root-integer (a n &optional guess) ; [I I S] | |
599 | (math-nth-root-int-iter a (or guess | |
600 | (math-scale-int 1 (/ (+ (math-numdigs a) | |
601 | (1- n)) | |
491c3062 | 602 | n))))) |
136211a9 EZ |
603 | |
604 | (defun math-nth-root-int-iter (a guess) ; uses "n" | |
605 | (math-working "root" guess) | |
606 | (let* ((q (math-idivmod a (math-ipow guess (1- n)))) | |
607 | (s (math-add (car q) (math-mul (1- n) guess))) | |
608 | (g2 (math-idivmod s n))) | |
609 | (if (Math-natnum-lessp (car g2) guess) | |
610 | (math-nth-root-int-iter a (car g2)) | |
611 | (cons (and (equal (car g2) guess) | |
612 | (eq (cdr q) 0) | |
613 | (eq (cdr g2) 0)) | |
491c3062 | 614 | guess)))) |
136211a9 EZ |
615 | |
616 | (defun calcFunc-nroot (x n) | |
617 | (calcFunc-pow x (if (integerp n) | |
618 | (math-make-frac 1 n) | |
491c3062 | 619 | (math-div 1 n)))) |
136211a9 EZ |
620 | |
621 | ||
622 | ||
623 | ||
624 | ;;;; Transcendental functions. | |
625 | ||
626 | ;;; All of these functions are defined on the complex plane. | |
627 | ;;; (Branch cuts, etc. follow Steele's Common Lisp book.) | |
628 | ||
629 | ;;; Most functions increase calc-internal-prec by 2 digits, then round | |
630 | ;;; down afterward. "-raw" functions use the current precision, require | |
631 | ;;; their arguments to be in float (or complex float) format, and always | |
632 | ;;; work in radians (where applicable). | |
633 | ||
634 | (defun math-to-radians (a) ; [N N] | |
635 | (cond ((eq (car-safe a) 'hms) | |
636 | (math-from-hms a 'rad)) | |
637 | ((memq calc-angle-mode '(deg hms)) | |
638 | (math-mul a (math-pi-over-180))) | |
491c3062 | 639 | (t a))) |
136211a9 EZ |
640 | |
641 | (defun math-from-radians (a) ; [N N] | |
642 | (cond ((eq calc-angle-mode 'deg) | |
643 | (if (math-constp a) | |
644 | (math-div a (math-pi-over-180)) | |
645 | (list 'calcFunc-deg a))) | |
646 | ((eq calc-angle-mode 'hms) | |
647 | (math-to-hms a 'rad)) | |
491c3062 | 648 | (t a))) |
136211a9 EZ |
649 | |
650 | (defun math-to-radians-2 (a) ; [N N] | |
651 | (cond ((eq (car-safe a) 'hms) | |
652 | (math-from-hms a 'rad)) | |
653 | ((memq calc-angle-mode '(deg hms)) | |
654 | (if calc-symbolic-mode | |
655 | (math-div (math-mul a '(var pi var-pi)) 180) | |
656 | (math-mul a (math-pi-over-180)))) | |
491c3062 | 657 | (t a))) |
136211a9 EZ |
658 | |
659 | (defun math-from-radians-2 (a) ; [N N] | |
660 | (cond ((memq calc-angle-mode '(deg hms)) | |
661 | (if calc-symbolic-mode | |
662 | (math-div (math-mul 180 a) '(var pi var-pi)) | |
663 | (math-div a (math-pi-over-180)))) | |
491c3062 | 664 | (t a))) |
136211a9 EZ |
665 | |
666 | ||
667 | ||
668 | ;;; Sine, cosine, and tangent. | |
669 | ||
670 | (defun calcFunc-sin (x) ; [N N] [Public] | |
671 | (cond ((and (integerp x) | |
672 | (if (eq calc-angle-mode 'deg) | |
673 | (= (% x 90) 0) | |
674 | (= x 0))) | |
675 | (aref [0 1 0 -1] (math-mod (/ x 90) 4))) | |
676 | ((Math-scalarp x) | |
677 | (math-with-extra-prec 2 | |
678 | (math-sin-raw (math-to-radians (math-float x))))) | |
679 | ((eq (car x) 'sdev) | |
680 | (if (math-constp x) | |
681 | (math-with-extra-prec 2 | |
682 | (let* ((xx (math-to-radians (math-float (nth 1 x)))) | |
683 | (xs (math-to-radians (math-float (nth 2 x)))) | |
684 | (sc (math-sin-cos-raw xx))) | |
685 | (math-make-sdev (car sc) (math-mul xs (cdr sc))))) | |
686 | (math-make-sdev (calcFunc-sin (nth 1 x)) | |
687 | (math-mul (nth 2 x) (calcFunc-cos (nth 1 x)))))) | |
688 | ((and (eq (car x) 'intv) (math-intv-constp x)) | |
689 | (calcFunc-cos (math-sub x (math-quarter-circle nil)))) | |
690 | ((equal x '(var nan var-nan)) | |
691 | x) | |
692 | (t (calc-record-why 'scalarp x) | |
491c3062 | 693 | (list 'calcFunc-sin x)))) |
136211a9 EZ |
694 | |
695 | (defun calcFunc-cos (x) ; [N N] [Public] | |
696 | (cond ((and (integerp x) | |
697 | (if (eq calc-angle-mode 'deg) | |
698 | (= (% x 90) 0) | |
699 | (= x 0))) | |
700 | (aref [1 0 -1 0] (math-mod (/ x 90) 4))) | |
701 | ((Math-scalarp x) | |
702 | (math-with-extra-prec 2 | |
703 | (math-cos-raw (math-to-radians (math-float x))))) | |
704 | ((eq (car x) 'sdev) | |
705 | (if (math-constp x) | |
706 | (math-with-extra-prec 2 | |
707 | (let* ((xx (math-to-radians (math-float (nth 1 x)))) | |
708 | (xs (math-to-radians (math-float (nth 2 x)))) | |
709 | (sc (math-sin-cos-raw xx))) | |
710 | (math-make-sdev (cdr sc) (math-mul xs (car sc))))) | |
711 | (math-make-sdev (calcFunc-cos (nth 1 x)) | |
712 | (math-mul (nth 2 x) (calcFunc-sin (nth 1 x)))))) | |
713 | ((and (eq (car x) 'intv) (math-intv-constp x)) | |
714 | (math-with-extra-prec 2 | |
715 | (let* ((xx (math-to-radians (math-float x))) | |
716 | (na (math-floor (math-div (nth 2 xx) (math-pi)))) | |
717 | (nb (math-floor (math-div (nth 3 xx) (math-pi)))) | |
718 | (span (math-sub nb na))) | |
719 | (if (memq span '(0 1)) | |
720 | (let ((int (math-sort-intv (nth 1 x) | |
721 | (math-cos-raw (nth 2 xx)) | |
722 | (math-cos-raw (nth 3 xx))))) | |
723 | (if (eq span 1) | |
724 | (if (math-evenp na) | |
725 | (math-make-intv (logior (nth 1 x) 2) | |
726 | -1 | |
727 | (nth 3 int)) | |
728 | (math-make-intv (logior (nth 1 x) 1) | |
729 | (nth 2 int) | |
730 | 1)) | |
731 | int)) | |
732 | (list 'intv 3 -1 1))))) | |
733 | ((equal x '(var nan var-nan)) | |
734 | x) | |
735 | (t (calc-record-why 'scalarp x) | |
491c3062 | 736 | (list 'calcFunc-cos x)))) |
136211a9 EZ |
737 | |
738 | (defun calcFunc-sincos (x) ; [V N] [Public] | |
739 | (if (Math-scalarp x) | |
740 | (math-with-extra-prec 2 | |
741 | (let ((sc (math-sin-cos-raw (math-to-radians (math-float x))))) | |
742 | (list 'vec (cdr sc) (car sc)))) ; the vector [cos, sin] | |
491c3062 | 743 | (list 'vec (calcFunc-sin x) (calcFunc-cos x)))) |
136211a9 EZ |
744 | |
745 | (defun calcFunc-tan (x) ; [N N] [Public] | |
746 | (cond ((and (integerp x) | |
747 | (if (eq calc-angle-mode 'deg) | |
748 | (= (% x 180) 0) | |
749 | (= x 0))) | |
750 | 0) | |
751 | ((Math-scalarp x) | |
752 | (math-with-extra-prec 2 | |
753 | (math-tan-raw (math-to-radians (math-float x))))) | |
754 | ((eq (car x) 'sdev) | |
755 | (if (math-constp x) | |
756 | (math-with-extra-prec 2 | |
757 | (let* ((xx (math-to-radians (math-float (nth 1 x)))) | |
758 | (xs (math-to-radians (math-float (nth 2 x)))) | |
759 | (sc (math-sin-cos-raw xx))) | |
760 | (if (and (math-zerop (cdr sc)) (not calc-infinite-mode)) | |
761 | (progn | |
762 | (calc-record-why "*Division by zero") | |
763 | (list 'calcFunc-tan x)) | |
764 | (math-make-sdev (math-div-float (car sc) (cdr sc)) | |
765 | (math-div-float xs (math-sqr (cdr sc))))))) | |
766 | (math-make-sdev (calcFunc-tan (nth 1 x)) | |
767 | (math-div (nth 2 x) | |
768 | (math-sqr (calcFunc-cos (nth 1 x))))))) | |
769 | ((and (eq (car x) 'intv) (math-intv-constp x)) | |
770 | (or (math-with-extra-prec 2 | |
771 | (let* ((xx (math-to-radians (math-float x))) | |
772 | (na (math-floor (math-div (math-sub (nth 2 xx) | |
773 | (math-pi-over-2)) | |
774 | (math-pi)))) | |
775 | (nb (math-floor (math-div (math-sub (nth 3 xx) | |
776 | (math-pi-over-2)) | |
777 | (math-pi))))) | |
778 | (and (equal na nb) | |
779 | (math-sort-intv (nth 1 x) | |
780 | (math-tan-raw (nth 2 xx)) | |
781 | (math-tan-raw (nth 3 xx)))))) | |
782 | '(intv 3 (neg (var inf var-inf)) (var inf var-inf)))) | |
783 | ((equal x '(var nan var-nan)) | |
784 | x) | |
785 | (t (calc-record-why 'scalarp x) | |
491c3062 | 786 | (list 'calcFunc-tan x)))) |
136211a9 EZ |
787 | |
788 | (defun math-sin-raw (x) ; [N N] | |
789 | (cond ((eq (car x) 'cplx) | |
790 | (let* ((expx (math-exp-raw (nth 2 x))) | |
791 | (expmx (math-div-float '(float 1 0) expx)) | |
792 | (sc (math-sin-cos-raw (nth 1 x)))) | |
793 | (list 'cplx | |
794 | (math-mul-float (car sc) | |
795 | (math-mul-float (math-add-float expx expmx) | |
796 | '(float 5 -1))) | |
797 | (math-mul-float (cdr sc) | |
798 | (math-mul-float (math-sub-float expx expmx) | |
799 | '(float 5 -1)))))) | |
800 | ((eq (car x) 'polar) | |
801 | (math-polar (math-sin-raw (math-complex x)))) | |
802 | ((Math-integer-negp (nth 1 x)) | |
803 | (math-neg-float (math-sin-raw (math-neg-float x)))) | |
804 | ((math-lessp-float '(float 7 0) x) ; avoid inf loops due to roundoff | |
805 | (math-sin-raw (math-mod x (math-two-pi)))) | |
491c3062 | 806 | (t (math-sin-raw-2 x x)))) |
136211a9 EZ |
807 | |
808 | (defun math-cos-raw (x) ; [N N] | |
809 | (if (eq (car-safe x) 'polar) | |
810 | (math-polar (math-cos-raw (math-complex x))) | |
491c3062 | 811 | (math-sin-raw (math-sub (math-pi-over-2) x)))) |
136211a9 EZ |
812 | |
813 | ;;; This could use a smarter method: Reduce x as in math-sin-raw, then | |
814 | ;;; compute either sin(x) or cos(x), whichever is smaller, and compute | |
815 | ;;; the other using the identity sin(x)^2 + cos(x)^2 = 1. | |
816 | (defun math-sin-cos-raw (x) ; [F.F F] (result is (sin x . cos x)) | |
491c3062 | 817 | (cons (math-sin-raw x) (math-cos-raw x))) |
136211a9 EZ |
818 | |
819 | (defun math-tan-raw (x) ; [N N] | |
820 | (cond ((eq (car x) 'cplx) | |
821 | (let* ((x (math-mul x '(float 2 0))) | |
822 | (expx (math-exp-raw (nth 2 x))) | |
823 | (expmx (math-div-float '(float 1 0) expx)) | |
824 | (sc (math-sin-cos-raw (nth 1 x))) | |
825 | (d (math-add-float (cdr sc) | |
826 | (math-mul-float (math-add-float expx expmx) | |
827 | '(float 5 -1))))) | |
828 | (and (not (eq (nth 1 d) 0)) | |
829 | (list 'cplx | |
830 | (math-div-float (car sc) d) | |
831 | (math-div-float (math-mul-float (math-sub-float expx | |
832 | expmx) | |
833 | '(float 5 -1)) d))))) | |
834 | ((eq (car x) 'polar) | |
835 | (math-polar (math-tan-raw (math-complex x)))) | |
836 | (t | |
837 | (let ((sc (math-sin-cos-raw x))) | |
838 | (if (eq (nth 1 (cdr sc)) 0) | |
839 | (math-div (car sc) 0) | |
491c3062 | 840 | (math-div-float (car sc) (cdr sc))))))) |
136211a9 EZ |
841 | |
842 | (defun math-sin-raw-2 (x orgx) ; This avoids poss of inf recursion. [F F] | |
843 | (let ((xmpo2 (math-sub-float (math-pi-over-2) x))) | |
844 | (cond ((Math-integer-negp (nth 1 xmpo2)) | |
845 | (math-neg-float (math-sin-raw-2 (math-sub-float x (math-pi)) | |
846 | orgx))) | |
847 | ((math-lessp-float (math-pi-over-4) x) | |
848 | (math-cos-raw-2 xmpo2 orgx)) | |
849 | ((math-lessp-float x (math-neg (math-pi-over-4))) | |
850 | (math-neg (math-cos-raw-2 (math-add (math-pi-over-2) x) orgx))) | |
851 | ((math-nearly-zerop-float x orgx) '(float 0 0)) | |
852 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
491c3062 | 853 | (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x))))))) |
136211a9 EZ |
854 | |
855 | (defun math-cos-raw-2 (x orgx) ; [F F] | |
856 | (cond ((math-nearly-zerop-float x orgx) '(float 1 0)) | |
857 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
858 | (t (let ((xnegsqr (math-neg-float (math-sqr-float x)))) | |
859 | (math-sin-series | |
860 | (math-add-float '(float 1 0) | |
861 | (math-mul-float xnegsqr '(float 5 -1))) | |
491c3062 | 862 | 24 5 xnegsqr xnegsqr))))) |
136211a9 EZ |
863 | |
864 | (defun math-sin-series (sum nfac n x xnegsqr) | |
865 | (math-working "sin" sum) | |
866 | (let* ((nextx (math-mul-float x xnegsqr)) | |
867 | (nextsum (math-add-float sum (math-div-float nextx | |
868 | (math-float nfac))))) | |
869 | (if (math-nearly-equal-float sum nextsum) | |
870 | sum | |
871 | (math-sin-series nextsum (math-mul nfac (* n (1+ n))) | |
491c3062 | 872 | (+ n 2) nextx xnegsqr)))) |
136211a9 EZ |
873 | |
874 | ||
875 | ;;; Inverse sine, cosine, tangent. | |
876 | ||
877 | (defun calcFunc-arcsin (x) ; [N N] [Public] | |
878 | (cond ((eq x 0) 0) | |
879 | ((and (eq x 1) (eq calc-angle-mode 'deg)) 90) | |
880 | ((and (eq x -1) (eq calc-angle-mode 'deg)) -90) | |
881 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
882 | ((Math-numberp x) | |
883 | (math-with-extra-prec 2 | |
884 | (math-from-radians (math-arcsin-raw (math-float x))))) | |
885 | ((eq (car x) 'sdev) | |
886 | (math-make-sdev (calcFunc-arcsin (nth 1 x)) | |
887 | (math-from-radians | |
888 | (math-div (nth 2 x) | |
889 | (math-sqrt | |
890 | (math-sub 1 (math-sqr (nth 1 x)))))))) | |
891 | ((eq (car x) 'intv) | |
892 | (math-sort-intv (nth 1 x) | |
893 | (calcFunc-arcsin (nth 2 x)) | |
894 | (calcFunc-arcsin (nth 3 x)))) | |
895 | ((equal x '(var nan var-nan)) | |
896 | x) | |
897 | (t (calc-record-why 'numberp x) | |
491c3062 | 898 | (list 'calcFunc-arcsin x)))) |
136211a9 EZ |
899 | |
900 | (defun calcFunc-arccos (x) ; [N N] [Public] | |
901 | (cond ((eq x 1) 0) | |
902 | ((and (eq x 0) (eq calc-angle-mode 'deg)) 90) | |
903 | ((and (eq x -1) (eq calc-angle-mode 'deg)) 180) | |
904 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
905 | ((Math-numberp x) | |
906 | (math-with-extra-prec 2 | |
907 | (math-from-radians (math-arccos-raw (math-float x))))) | |
908 | ((eq (car x) 'sdev) | |
909 | (math-make-sdev (calcFunc-arccos (nth 1 x)) | |
910 | (math-from-radians | |
911 | (math-div (nth 2 x) | |
912 | (math-sqrt | |
913 | (math-sub 1 (math-sqr (nth 1 x)))))))) | |
914 | ((eq (car x) 'intv) | |
915 | (math-sort-intv (nth 1 x) | |
916 | (calcFunc-arccos (nth 2 x)) | |
917 | (calcFunc-arccos (nth 3 x)))) | |
918 | ((equal x '(var nan var-nan)) | |
919 | x) | |
920 | (t (calc-record-why 'numberp x) | |
491c3062 | 921 | (list 'calcFunc-arccos x)))) |
136211a9 EZ |
922 | |
923 | (defun calcFunc-arctan (x) ; [N N] [Public] | |
924 | (cond ((eq x 0) 0) | |
925 | ((and (eq x 1) (eq calc-angle-mode 'deg)) 45) | |
926 | ((and (eq x -1) (eq calc-angle-mode 'deg)) -45) | |
927 | ((Math-numberp x) | |
928 | (math-with-extra-prec 2 | |
929 | (math-from-radians (math-arctan-raw (math-float x))))) | |
930 | ((eq (car x) 'sdev) | |
931 | (math-make-sdev (calcFunc-arctan (nth 1 x)) | |
932 | (math-from-radians | |
933 | (math-div (nth 2 x) | |
934 | (math-add 1 (math-sqr (nth 1 x))))))) | |
935 | ((eq (car x) 'intv) | |
936 | (math-sort-intv (nth 1 x) | |
937 | (calcFunc-arctan (nth 2 x)) | |
938 | (calcFunc-arctan (nth 3 x)))) | |
939 | ((equal x '(var inf var-inf)) | |
940 | (math-quarter-circle t)) | |
941 | ((equal x '(neg (var inf var-inf))) | |
942 | (math-neg (math-quarter-circle t))) | |
943 | ((equal x '(var nan var-nan)) | |
944 | x) | |
945 | (t (calc-record-why 'numberp x) | |
491c3062 | 946 | (list 'calcFunc-arctan x)))) |
136211a9 EZ |
947 | |
948 | (defun math-arcsin-raw (x) ; [N N] | |
949 | (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x))))) | |
950 | (if (or (memq (car x) '(cplx polar)) | |
951 | (memq (car a) '(cplx polar))) | |
952 | (math-with-extra-prec 2 ; use extra precision for difficult case | |
953 | (math-mul '(cplx 0 -1) | |
954 | (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a)))) | |
491c3062 | 955 | (math-arctan2-raw x a)))) |
136211a9 EZ |
956 | |
957 | (defun math-arccos-raw (x) ; [N N] | |
491c3062 | 958 | (math-sub (math-pi-over-2) (math-arcsin-raw x))) |
136211a9 EZ |
959 | |
960 | (defun math-arctan-raw (x) ; [N N] | |
961 | (cond ((memq (car x) '(cplx polar)) | |
962 | (math-with-extra-prec 2 ; extra-extra | |
963 | (math-div (math-sub | |
964 | (math-ln-raw (math-add 1 (math-mul '(cplx 0 1) x))) | |
965 | (math-ln-raw (math-add 1 (math-mul '(cplx 0 -1) x)))) | |
966 | '(cplx 0 2)))) | |
967 | ((Math-integer-negp (nth 1 x)) | |
968 | (math-neg-float (math-arctan-raw (math-neg-float x)))) | |
969 | ((math-zerop x) x) | |
970 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
971 | ((math-equal-int x 1) (math-pi-over-4)) | |
972 | ((math-equal-int x -1) (math-neg (math-pi-over-4))) | |
973 | ((math-lessp-float '(float 414214 -6) x) ; if x > sqrt(2) - 1, reduce | |
974 | (if (math-lessp-float '(float 1 0) x) | |
975 | (math-sub-float (math-mul-float (math-pi) '(float 5 -1)) | |
976 | (math-arctan-raw (math-div-float '(float 1 0) x))) | |
977 | (math-sub-float (math-mul-float (math-pi) '(float 25 -2)) | |
978 | (math-arctan-raw (math-div-float | |
979 | (math-sub-float '(float 1 0) x) | |
980 | (math-add-float '(float 1 0) | |
981 | x)))))) | |
491c3062 | 982 | (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x)))))) |
136211a9 EZ |
983 | |
984 | (defun math-arctan-series (sum n x xnegsqr) | |
985 | (math-working "arctan" sum) | |
986 | (let* ((nextx (math-mul-float x xnegsqr)) | |
987 | (nextsum (math-add-float sum (math-div-float nextx (math-float n))))) | |
988 | (if (math-nearly-equal-float sum nextsum) | |
989 | sum | |
491c3062 | 990 | (math-arctan-series nextsum (+ n 2) nextx xnegsqr)))) |
136211a9 EZ |
991 | |
992 | (defun calcFunc-arctan2 (y x) ; [F R R] [Public] | |
993 | (if (Math-anglep y) | |
994 | (if (Math-anglep x) | |
995 | (math-with-extra-prec 2 | |
996 | (math-from-radians (math-arctan2-raw (math-float y) | |
997 | (math-float x)))) | |
998 | (calc-record-why 'anglep x) | |
999 | (list 'calcFunc-arctan2 y x)) | |
1000 | (if (and (or (math-infinitep x) (math-anglep x)) | |
1001 | (or (math-infinitep y) (math-anglep y))) | |
1002 | (progn | |
1003 | (if (math-posp x) | |
1004 | (setq x 1) | |
1005 | (if (math-negp x) | |
1006 | (setq x -1) | |
1007 | (or (math-zerop x) | |
1008 | (setq x nil)))) | |
1009 | (if (math-posp y) | |
1010 | (setq y 1) | |
1011 | (if (math-negp y) | |
1012 | (setq y -1) | |
1013 | (or (math-zerop y) | |
1014 | (setq y nil)))) | |
1015 | (if (and y x) | |
1016 | (calcFunc-arctan2 y x) | |
1017 | '(var nan var-nan))) | |
1018 | (calc-record-why 'anglep y) | |
491c3062 | 1019 | (list 'calcFunc-arctan2 y x)))) |
136211a9 EZ |
1020 | |
1021 | (defun math-arctan2-raw (y x) ; [F R R] | |
1022 | (cond ((math-zerop y) | |
1023 | (if (math-negp x) (math-pi) | |
1024 | (if (or (math-floatp x) (math-floatp y)) '(float 0 0) 0))) | |
1025 | ((math-zerop x) | |
1026 | (if (math-posp y) | |
1027 | (math-pi-over-2) | |
1028 | (math-neg (math-pi-over-2)))) | |
1029 | ((math-posp x) | |
1030 | (math-arctan-raw (math-div-float y x))) | |
1031 | ((math-posp y) | |
1032 | (math-add-float (math-arctan-raw (math-div-float y x)) | |
1033 | (math-pi))) | |
1034 | (t | |
1035 | (math-sub-float (math-arctan-raw (math-div-float y x)) | |
491c3062 | 1036 | (math-pi))))) |
136211a9 EZ |
1037 | |
1038 | (defun calcFunc-arcsincos (x) ; [V N] [Public] | |
1039 | (if (and (Math-vectorp x) | |
1040 | (= (length x) 3)) | |
1041 | (calcFunc-arctan2 (nth 2 x) (nth 1 x)) | |
491c3062 | 1042 | (math-reject-arg x "*Two-element vector expected"))) |
136211a9 EZ |
1043 | |
1044 | ||
1045 | ||
1046 | ;;; Exponential function. | |
1047 | ||
1048 | (defun calcFunc-exp (x) ; [N N] [Public] | |
1049 | (cond ((eq x 0) 1) | |
1050 | ((and (memq x '(1 -1)) calc-symbolic-mode) | |
1051 | (if (eq x 1) '(var e var-e) (math-div 1 '(var e var-e)))) | |
1052 | ((Math-numberp x) | |
1053 | (math-with-extra-prec 2 (math-exp-raw (math-float x)))) | |
1054 | ((eq (car-safe x) 'sdev) | |
1055 | (let ((ex (calcFunc-exp (nth 1 x)))) | |
1056 | (math-make-sdev ex (math-mul (nth 2 x) ex)))) | |
1057 | ((eq (car-safe x) 'intv) | |
1058 | (math-make-intv (nth 1 x) (calcFunc-exp (nth 2 x)) | |
1059 | (calcFunc-exp (nth 3 x)))) | |
1060 | ((equal x '(var inf var-inf)) | |
1061 | x) | |
1062 | ((equal x '(neg (var inf var-inf))) | |
1063 | 0) | |
1064 | ((equal x '(var nan var-nan)) | |
1065 | x) | |
1066 | (t (calc-record-why 'numberp x) | |
491c3062 | 1067 | (list 'calcFunc-exp x)))) |
136211a9 EZ |
1068 | |
1069 | (defun calcFunc-expm1 (x) ; [N N] [Public] | |
1070 | (cond ((eq x 0) 0) | |
1071 | ((math-zerop x) '(float 0 0)) | |
1072 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
1073 | ((Math-numberp x) | |
1074 | (math-with-extra-prec 2 | |
1075 | (let ((x (math-float x))) | |
1076 | (if (and (eq (car x) 'float) | |
1077 | (math-lessp-float x '(float 1 0)) | |
1078 | (math-lessp-float '(float -1 0) x)) | |
1079 | (math-exp-minus-1-raw x) | |
1080 | (math-add (math-exp-raw x) -1))))) | |
1081 | ((eq (car-safe x) 'sdev) | |
1082 | (if (math-constp x) | |
1083 | (let ((ex (calcFunc-expm1 (nth 1 x)))) | |
1084 | (math-make-sdev ex (math-mul (nth 2 x) (math-add ex 1)))) | |
1085 | (math-make-sdev (calcFunc-expm1 (nth 1 x)) | |
1086 | (math-mul (nth 2 x) (calcFunc-exp (nth 1 x)))))) | |
1087 | ((eq (car-safe x) 'intv) | |
1088 | (math-make-intv (nth 1 x) | |
1089 | (calcFunc-expm1 (nth 2 x)) | |
1090 | (calcFunc-expm1 (nth 3 x)))) | |
1091 | ((equal x '(var inf var-inf)) | |
1092 | x) | |
1093 | ((equal x '(neg (var inf var-inf))) | |
1094 | -1) | |
1095 | ((equal x '(var nan var-nan)) | |
1096 | x) | |
1097 | (t (calc-record-why 'numberp x) | |
491c3062 | 1098 | (list 'calcFunc-expm1 x)))) |
136211a9 EZ |
1099 | |
1100 | (defun calcFunc-exp10 (x) ; [N N] [Public] | |
1101 | (if (eq x 0) | |
1102 | 1 | |
491c3062 | 1103 | (math-pow '(float 1 1) x))) |
136211a9 EZ |
1104 | |
1105 | (defun math-exp-raw (x) ; [N N] | |
1106 | (cond ((math-zerop x) '(float 1 0)) | |
1107 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
1108 | ((eq (car x) 'cplx) | |
1109 | (let ((expx (math-exp-raw (nth 1 x))) | |
1110 | (sc (math-sin-cos-raw (nth 2 x)))) | |
1111 | (list 'cplx | |
1112 | (math-mul-float expx (cdr sc)) | |
1113 | (math-mul-float expx (car sc))))) | |
1114 | ((eq (car x) 'polar) | |
1115 | (let ((xc (math-complex x))) | |
1116 | (list 'polar | |
1117 | (math-exp-raw (nth 1 xc)) | |
1118 | (math-from-radians (nth 2 xc))))) | |
1119 | ((or (math-lessp-float '(float 5 -1) x) | |
1120 | (math-lessp-float x '(float -5 -1))) | |
1121 | (if (math-lessp-float '(float 921035 1) x) | |
1122 | (math-overflow) | |
1123 | (if (math-lessp-float x '(float -921035 1)) | |
1124 | (math-underflow))) | |
1125 | (let* ((two-x (math-mul-float x '(float 2 0))) | |
1126 | (hint (math-scale-int (nth 1 two-x) (nth 2 two-x))) | |
1127 | (hfrac (math-sub-float x (math-mul-float (math-float hint) | |
1128 | '(float 5 -1))))) | |
1129 | (math-mul-float (math-ipow (math-sqrt-e) hint) | |
1130 | (math-add-float '(float 1 0) | |
1131 | (math-exp-minus-1-raw hfrac))))) | |
491c3062 | 1132 | (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x))))) |
136211a9 EZ |
1133 | |
1134 | (defun math-exp-minus-1-raw (x) ; [F F] | |
491c3062 | 1135 | (math-exp-series x 2 3 x x)) |
136211a9 EZ |
1136 | |
1137 | (defun math-exp-series (sum nfac n xpow x) | |
1138 | (math-working "exp" sum) | |
1139 | (let* ((nextx (math-mul-float xpow x)) | |
1140 | (nextsum (math-add-float sum (math-div-float nextx | |
1141 | (math-float nfac))))) | |
1142 | (if (math-nearly-equal-float sum nextsum) | |
1143 | sum | |
491c3062 | 1144 | (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x)))) |
136211a9 EZ |
1145 | |
1146 | ||
1147 | ||
1148 | ;;; Logarithms. | |
1149 | ||
1150 | (defun calcFunc-ln (x) ; [N N] [Public] | |
1151 | (cond ((math-zerop x) | |
1152 | (if calc-infinite-mode | |
1153 | '(neg (var inf var-inf)) | |
1154 | (math-reject-arg x "*Logarithm of zero"))) | |
1155 | ((eq x 1) 0) | |
1156 | ((Math-numberp x) | |
1157 | (math-with-extra-prec 2 (math-ln-raw (math-float x)))) | |
1158 | ((eq (car-safe x) 'sdev) | |
1159 | (math-make-sdev (calcFunc-ln (nth 1 x)) | |
1160 | (math-div (nth 2 x) (nth 1 x)))) | |
1161 | ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x)) | |
1162 | (Math-zerop (nth 2 x)) | |
1163 | (not (math-intv-constp x)))) | |
1164 | (let ((calc-infinite-mode t)) | |
1165 | (math-make-intv (nth 1 x) (calcFunc-ln (nth 2 x)) | |
1166 | (calcFunc-ln (nth 3 x))))) | |
1167 | ((equal x '(var e var-e)) | |
1168 | 1) | |
1169 | ((and (eq (car-safe x) '^) | |
1170 | (equal (nth 1 x) '(var e var-e)) | |
1171 | (math-known-realp (nth 2 x))) | |
1172 | (nth 2 x)) | |
1173 | ((math-infinitep x) | |
1174 | (if (equal x '(var nan var-nan)) | |
1175 | x | |
1176 | '(var inf var-inf))) | |
1177 | (t (calc-record-why 'numberp x) | |
491c3062 | 1178 | (list 'calcFunc-ln x)))) |
136211a9 EZ |
1179 | |
1180 | (defun calcFunc-log10 (x) ; [N N] [Public] | |
1181 | (cond ((math-equal-int x 1) | |
1182 | (if (math-floatp x) '(float 0 0) 0)) | |
1183 | ((and (Math-integerp x) | |
1184 | (math-posp x) | |
1185 | (let ((res (math-integer-log x 10))) | |
1186 | (and (car res) | |
1187 | (setq x (cdr res))))) | |
1188 | x) | |
1189 | ((and (eq (car-safe x) 'frac) | |
1190 | (eq (nth 1 x) 1) | |
1191 | (let ((res (math-integer-log (nth 2 x) 10))) | |
1192 | (and (car res) | |
1193 | (setq x (- (cdr res)))))) | |
1194 | x) | |
1195 | ((math-zerop x) | |
1196 | (if calc-infinite-mode | |
1197 | '(neg (var inf var-inf)) | |
1198 | (math-reject-arg x "*Logarithm of zero"))) | |
1199 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
1200 | ((Math-numberp x) | |
1201 | (math-with-extra-prec 2 | |
1202 | (let ((xf (math-float x))) | |
1203 | (if (eq (nth 1 xf) 0) | |
1204 | (math-reject-arg x "*Logarithm of zero")) | |
1205 | (if (Math-integer-posp (nth 1 xf)) | |
1206 | (if (eq (nth 1 xf) 1) ; log10(1*10^n) = n | |
1207 | (math-float (nth 2 xf)) | |
1208 | (let ((xdigs (1- (math-numdigs (nth 1 xf))))) | |
1209 | (math-add-float | |
1210 | (math-div-float (math-ln-raw-2 | |
1211 | (list 'float (nth 1 xf) (- xdigs))) | |
1212 | (math-ln-10)) | |
1213 | (math-float (+ (nth 2 xf) xdigs))))) | |
1214 | (math-div (calcFunc-ln xf) (math-ln-10)))))) | |
1215 | ((eq (car-safe x) 'sdev) | |
1216 | (math-make-sdev (calcFunc-log10 (nth 1 x)) | |
1217 | (math-div (nth 2 x) | |
1218 | (math-mul (nth 1 x) (math-ln-10))))) | |
1219 | ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x)) | |
1220 | (not (math-intv-constp x)))) | |
1221 | (math-make-intv (nth 1 x) | |
1222 | (calcFunc-log10 (nth 2 x)) | |
1223 | (calcFunc-log10 (nth 3 x)))) | |
1224 | ((math-infinitep x) | |
1225 | (if (equal x '(var nan var-nan)) | |
1226 | x | |
1227 | '(var inf var-inf))) | |
1228 | (t (calc-record-why 'numberp x) | |
491c3062 | 1229 | (list 'calcFunc-log10 x)))) |
136211a9 EZ |
1230 | |
1231 | (defun calcFunc-log (x &optional b) ; [N N N] [Public] | |
1232 | (cond ((or (null b) (equal b '(var e var-e))) | |
1233 | (math-normalize (list 'calcFunc-ln x))) | |
1234 | ((or (eq b 10) (equal b '(float 1 1))) | |
1235 | (math-normalize (list 'calcFunc-log10 x))) | |
1236 | ((math-zerop x) | |
1237 | (if calc-infinite-mode | |
1238 | (math-div (calcFunc-ln x) (calcFunc-ln b)) | |
1239 | (math-reject-arg x "*Logarithm of zero"))) | |
1240 | ((math-zerop b) | |
1241 | (if calc-infinite-mode | |
1242 | (math-div (calcFunc-ln x) (calcFunc-ln b)) | |
1243 | (math-reject-arg b "*Logarithm of zero"))) | |
1244 | ((math-equal-int b 1) | |
1245 | (if calc-infinite-mode | |
1246 | (math-div (calcFunc-ln x) 0) | |
1247 | (math-reject-arg b "*Logarithm base one"))) | |
1248 | ((math-equal-int x 1) | |
1249 | (if (or (math-floatp a) (math-floatp b)) '(float 0 0) 0)) | |
1250 | ((and (Math-ratp x) (Math-ratp b) | |
1251 | (math-posp x) (math-posp b) | |
1252 | (let* ((sign 1) (inv nil) | |
1253 | (xx (if (Math-lessp 1 x) | |
1254 | x | |
1255 | (setq sign -1) | |
1256 | (math-div 1 x))) | |
1257 | (bb (if (Math-lessp 1 b) | |
1258 | b | |
1259 | (setq sign (- sign)) | |
1260 | (math-div 1 b))) | |
1261 | (res (if (Math-lessp xx bb) | |
1262 | (setq inv (math-integer-log bb xx)) | |
1263 | (math-integer-log xx bb)))) | |
1264 | (and (car res) | |
1265 | (setq x (if inv | |
1266 | (math-div 1 (* sign (cdr res))) | |
1267 | (* sign (cdr res))))))) | |
1268 | x) | |
1269 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
1270 | ((and (Math-numberp x) (Math-numberp b)) | |
1271 | (math-with-extra-prec 2 | |
1272 | (math-div (math-ln-raw (math-float x)) | |
1273 | (math-log-base-raw b)))) | |
1274 | ((and (eq (car-safe x) 'sdev) | |
1275 | (Math-numberp b)) | |
1276 | (math-make-sdev (calcFunc-log (nth 1 x) b) | |
1277 | (math-div (nth 2 x) | |
1278 | (math-mul (nth 1 x) | |
1279 | (math-log-base-raw b))))) | |
1280 | ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x)) | |
1281 | (not (math-intv-constp x))) | |
1282 | (math-realp b)) | |
1283 | (math-make-intv (nth 1 x) | |
1284 | (calcFunc-log (nth 2 x) b) | |
1285 | (calcFunc-log (nth 3 x) b))) | |
1286 | ((or (eq (car-safe x) 'intv) (eq (car-safe b) 'intv)) | |
1287 | (math-div (calcFunc-ln x) (calcFunc-ln b))) | |
1288 | ((or (math-infinitep x) | |
1289 | (math-infinitep b)) | |
1290 | (math-div (calcFunc-ln x) (calcFunc-ln b))) | |
1291 | (t (if (Math-numberp b) | |
1292 | (calc-record-why 'numberp x) | |
1293 | (calc-record-why 'numberp b)) | |
491c3062 | 1294 | (list 'calcFunc-log x b)))) |
136211a9 EZ |
1295 | |
1296 | (defun calcFunc-alog (x &optional b) | |
1297 | (cond ((or (null b) (equal b '(var e var-e))) | |
1298 | (math-normalize (list 'calcFunc-exp x))) | |
491c3062 | 1299 | (t (math-pow b x)))) |
136211a9 EZ |
1300 | |
1301 | (defun calcFunc-ilog (x b) | |
1302 | (if (and (math-natnump x) (not (eq x 0)) | |
1303 | (math-natnump b) (not (eq b 0))) | |
1304 | (if (eq b 1) | |
1305 | (math-reject-arg x "*Logarithm base one") | |
1306 | (if (Math-natnum-lessp x b) | |
1307 | 0 | |
1308 | (cdr (math-integer-log x b)))) | |
491c3062 | 1309 | (math-floor (calcFunc-log x b)))) |
136211a9 EZ |
1310 | |
1311 | (defun math-integer-log (x b) | |
1312 | (let ((pows (list b)) | |
1313 | (pow (math-sqr b)) | |
1314 | next | |
1315 | sum n) | |
1316 | (while (not (Math-lessp x pow)) | |
1317 | (setq pows (cons pow pows) | |
1318 | pow (math-sqr pow))) | |
1319 | (setq n (lsh 1 (1- (length pows))) | |
1320 | sum n | |
1321 | pow (car pows)) | |
1322 | (while (and (setq pows (cdr pows)) | |
1323 | (Math-lessp pow x)) | |
1324 | (setq n (/ n 2) | |
1325 | next (math-mul pow (car pows))) | |
1326 | (or (Math-lessp x next) | |
1327 | (setq pow next | |
1328 | sum (+ sum n)))) | |
491c3062 | 1329 | (cons (equal pow x) sum))) |
136211a9 EZ |
1330 | |
1331 | ||
3132f345 | 1332 | (defvar math-log-base-cache nil) |
136211a9 EZ |
1333 | (defun math-log-base-raw (b) ; [N N] |
1334 | (if (not (and (equal (car math-log-base-cache) b) | |
1335 | (eq (nth 1 math-log-base-cache) calc-internal-prec))) | |
1336 | (setq math-log-base-cache (list b calc-internal-prec | |
1337 | (math-ln-raw (math-float b))))) | |
491c3062 | 1338 | (nth 2 math-log-base-cache)) |
136211a9 EZ |
1339 | |
1340 | (defun calcFunc-lnp1 (x) ; [N N] [Public] | |
1341 | (cond ((Math-equal-int x -1) | |
1342 | (if calc-infinite-mode | |
1343 | '(neg (var inf var-inf)) | |
1344 | (math-reject-arg x "*Logarithm of zero"))) | |
1345 | ((eq x 0) 0) | |
1346 | ((math-zerop x) '(float 0 0)) | |
1347 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
1348 | ((Math-numberp x) | |
1349 | (math-with-extra-prec 2 | |
1350 | (let ((x (math-float x))) | |
1351 | (if (and (eq (car x) 'float) | |
1352 | (math-lessp-float x '(float 5 -1)) | |
1353 | (math-lessp-float '(float -5 -1) x)) | |
1354 | (math-ln-plus-1-raw x) | |
1355 | (math-ln-raw (math-add-float x '(float 1 0))))))) | |
1356 | ((eq (car-safe x) 'sdev) | |
1357 | (math-make-sdev (calcFunc-lnp1 (nth 1 x)) | |
1358 | (math-div (nth 2 x) (math-add (nth 1 x) 1)))) | |
1359 | ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x)) | |
1360 | (not (math-intv-constp x)))) | |
1361 | (math-make-intv (nth 1 x) | |
1362 | (calcFunc-lnp1 (nth 2 x)) | |
1363 | (calcFunc-lnp1 (nth 3 x)))) | |
1364 | ((math-infinitep x) | |
1365 | (if (equal x '(var nan var-nan)) | |
1366 | x | |
1367 | '(var inf var-inf))) | |
1368 | (t (calc-record-why 'numberp x) | |
491c3062 | 1369 | (list 'calcFunc-lnp1 x)))) |
136211a9 EZ |
1370 | |
1371 | (defun math-ln-raw (x) ; [N N] --- must be float format! | |
1372 | (cond ((eq (car-safe x) 'cplx) | |
1373 | (list 'cplx | |
1374 | (math-mul-float (math-ln-raw | |
1375 | (math-add-float (math-sqr-float (nth 1 x)) | |
1376 | (math-sqr-float (nth 2 x)))) | |
1377 | '(float 5 -1)) | |
1378 | (math-arctan2-raw (nth 2 x) (nth 1 x)))) | |
1379 | ((eq (car x) 'polar) | |
1380 | (math-polar (list 'cplx | |
1381 | (math-ln-raw (nth 1 x)) | |
1382 | (math-to-radians (nth 2 x))))) | |
1383 | ((Math-equal-int x 1) | |
1384 | '(float 0 0)) | |
1385 | (calc-symbolic-mode (signal 'inexact-result nil)) | |
1386 | ((math-posp (nth 1 x)) ; positive and real | |
1387 | (let ((xdigs (1- (math-numdigs (nth 1 x))))) | |
1388 | (math-add-float (math-ln-raw-2 (list 'float (nth 1 x) (- xdigs))) | |
1389 | (math-mul-float (math-float (+ (nth 2 x) xdigs)) | |
1390 | (math-ln-10))))) | |
1391 | ((math-zerop x) | |
1392 | (math-reject-arg x "*Logarithm of zero")) | |
1393 | ((eq calc-complex-mode 'polar) ; negative and real | |
1394 | (math-polar | |
1395 | (list 'cplx ; negative and real | |
1396 | (math-ln-raw (math-neg-float x)) | |
1397 | (math-pi)))) | |
1398 | (t (list 'cplx ; negative and real | |
1399 | (math-ln-raw (math-neg-float x)) | |
491c3062 | 1400 | (math-pi))))) |
136211a9 EZ |
1401 | |
1402 | (defun math-ln-raw-2 (x) ; [F F] | |
1403 | (cond ((math-lessp-float '(float 14 -1) x) | |
1404 | (math-add-float (math-ln-raw-2 (math-mul-float x '(float 5 -1))) | |
1405 | (math-ln-2))) | |
1406 | (t ; now .7 < x <= 1.4 | |
1407 | (math-ln-raw-3 (math-div-float (math-sub-float x '(float 1 0)) | |
491c3062 | 1408 | (math-add-float x '(float 1 0))))))) |
136211a9 EZ |
1409 | |
1410 | (defun math-ln-raw-3 (x) ; [F F] | |
1411 | (math-mul-float (math-ln-raw-series x 3 x (math-sqr-float x)) | |
491c3062 | 1412 | '(float 2 0))) |
136211a9 EZ |
1413 | |
1414 | ;;; Compute ln((1+x)/(1-x)) | |
1415 | (defun math-ln-raw-series (sum n x xsqr) | |
1416 | (math-working "log" sum) | |
1417 | (let* ((nextx (math-mul-float x xsqr)) | |
1418 | (nextsum (math-add-float sum (math-div-float nextx (math-float n))))) | |
1419 | (if (math-nearly-equal-float sum nextsum) | |
1420 | sum | |
491c3062 | 1421 | (math-ln-raw-series nextsum (+ n 2) nextx xsqr)))) |
136211a9 EZ |
1422 | |
1423 | (defun math-ln-plus-1-raw (x) | |
491c3062 | 1424 | (math-lnp1-series x 2 x (math-neg x))) |
136211a9 EZ |
1425 | |
1426 | (defun math-lnp1-series (sum n xpow x) | |
1427 | (math-working "lnp1" sum) | |
1428 | (let* ((nextx (math-mul-float xpow x)) | |
1429 | (nextsum (math-add-float sum (math-div-float nextx (math-float n))))) | |
1430 | (if (math-nearly-equal-float sum nextsum) | |
1431 | sum | |
491c3062 | 1432 | (math-lnp1-series nextsum (1+ n) nextx x)))) |
136211a9 EZ |
1433 | |
1434 | (math-defcache math-ln-10 (float (bigpos 018 684 045 994 092 585 302 2) -21) | |
1435 | (math-ln-raw-2 '(float 1 1))) | |
1436 | ||
1437 | (math-defcache math-ln-2 (float (bigpos 417 309 945 559 180 147 693) -21) | |
1438 | (math-ln-raw-3 (math-float '(frac 1 3)))) | |
1439 | ||
1440 | ||
1441 | ||
1442 | ;;; Hyperbolic functions. | |
1443 | ||
1444 | (defun calcFunc-sinh (x) ; [N N] [Public] | |
1445 | (cond ((eq x 0) 0) | |
1446 | (math-expand-formulas | |
1447 | (math-normalize | |
1448 | (list '/ (list '- (list 'calcFunc-exp x) | |
1449 | (list 'calcFunc-exp (list 'neg x))) 2))) | |
1450 | ((Math-numberp x) | |
1451 | (if calc-symbolic-mode (signal 'inexact-result nil)) | |
1452 | (math-with-extra-prec 2 | |
1453 | (let ((expx (math-exp-raw (math-float x)))) | |
1454 | (math-mul (math-add expx (math-div -1 expx)) '(float 5 -1))))) | |
1455 | ((eq (car-safe x) 'sdev) | |
1456 | (math-make-sdev (calcFunc-sinh (nth 1 x)) | |
1457 | (math-mul (nth 2 x) (calcFunc-cosh (nth 1 x))))) | |
1458 | ((eq (car x) 'intv) | |
1459 | (math-sort-intv (nth 1 x) | |
1460 | (calcFunc-sinh (nth 2 x)) | |
1461 | (calcFunc-sinh (nth 3 x)))) | |
1462 | ((or (equal x '(var inf var-inf)) | |
1463 | (equal x '(neg (var inf var-inf))) | |
1464 | (equal x '(var nan var-nan))) | |
1465 | x) | |
1466 | (t (calc-record-why 'numberp x) | |
491c3062 | 1467 | (list 'calcFunc-sinh x)))) |
136211a9 EZ |
1468 | (put 'calcFunc-sinh 'math-expandable t) |
1469 | ||
1470 | (defun calcFunc-cosh (x) ; [N N] [Public] | |
1471 | (cond ((eq x 0) 1) | |
1472 | (math-expand-formulas | |
1473 | (math-normalize | |
1474 | (list '/ (list '+ (list 'calcFunc-exp x) | |
1475 | (list 'calcFunc-exp (list 'neg x))) 2))) | |
1476 | ((Math-numberp x) | |
1477 | (if calc-symbolic-mode (signal 'inexact-result nil)) | |
1478 | (math-with-extra-prec 2 | |
1479 | (let ((expx (math-exp-raw (math-float x)))) | |
1480 | (math-mul (math-add expx (math-div 1 expx)) '(float 5 -1))))) | |
1481 | ((eq (car-safe x) 'sdev) | |
1482 | (math-make-sdev (calcFunc-cosh (nth 1 x)) | |
1483 | (math-mul (nth 2 x) | |
1484 | (calcFunc-sinh (nth 1 x))))) | |
1485 | ((and (eq (car x) 'intv) (math-intv-constp x)) | |
1486 | (setq x (math-abs x)) | |
1487 | (math-sort-intv (nth 1 x) | |
1488 | (calcFunc-cosh (nth 2 x)) | |
1489 | (calcFunc-cosh (nth 3 x)))) | |
1490 | ((or (equal x '(var inf var-inf)) | |
1491 | (equal x '(neg (var inf var-inf))) | |
1492 | (equal x '(var nan var-nan))) | |
1493 | (math-abs x)) | |
1494 | (t (calc-record-why 'numberp x) | |
491c3062 | 1495 | (list 'calcFunc-cosh x)))) |
136211a9 EZ |
1496 | (put 'calcFunc-cosh 'math-expandable t) |
1497 | ||
1498 | (defun calcFunc-tanh (x) ; [N N] [Public] | |
1499 | (cond ((eq x 0) 0) | |
1500 | (math-expand-formulas | |
1501 | (math-normalize | |
1502 | (let ((expx (list 'calcFunc-exp x)) | |
1503 | (expmx (list 'calcFunc-exp (list 'neg x)))) | |
1504 | (math-normalize | |
1505 | (list '/ (list '- expx expmx) (list '+ expx expmx)))))) | |
1506 | ((Math-numberp x) | |
1507 | (if calc-symbolic-mode (signal 'inexact-result nil)) | |
1508 | (math-with-extra-prec 2 | |
1509 | (let* ((expx (calcFunc-exp (math-float x))) | |
1510 | (expmx (math-div 1 expx))) | |
1511 | (math-div (math-sub expx expmx) | |
1512 | (math-add expx expmx))))) | |
1513 | ((eq (car-safe x) 'sdev) | |
1514 | (math-make-sdev (calcFunc-tanh (nth 1 x)) | |
1515 | (math-div (nth 2 x) | |
1516 | (math-sqr (calcFunc-cosh (nth 1 x)))))) | |
1517 | ((eq (car x) 'intv) | |
1518 | (math-sort-intv (nth 1 x) | |
1519 | (calcFunc-tanh (nth 2 x)) | |
1520 | (calcFunc-tanh (nth 3 x)))) | |
1521 | ((equal x '(var inf var-inf)) | |
1522 | 1) | |
1523 | ((equal x '(neg (var inf var-inf))) | |
1524 | -1) | |
1525 | ((equal x '(var nan var-nan)) | |
1526 | x) | |
1527 | (t (calc-record-why 'numberp x) | |
491c3062 | 1528 | (list 'calcFunc-tanh x)))) |
136211a9 EZ |
1529 | (put 'calcFunc-tanh 'math-expandable t) |
1530 | ||
1531 | (defun calcFunc-arcsinh (x) ; [N N] [Public] | |
1532 | (cond ((eq x 0) 0) | |
1533 | (math-expand-formulas | |
1534 | (math-normalize | |
1535 | (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt | |
1536 | (list '+ (list '^ x 2) 1)))))) | |
1537 | ((Math-numberp x) | |
1538 | (if calc-symbolic-mode (signal 'inexact-result nil)) | |
1539 | (math-with-extra-prec 2 | |
1540 | (math-ln-raw (math-add x (math-sqrt-raw (math-add (math-sqr x) | |
1541 | '(float 1 0))))))) | |
1542 | ((eq (car-safe x) 'sdev) | |
1543 | (math-make-sdev (calcFunc-arcsinh (nth 1 x)) | |
1544 | (math-div (nth 2 x) | |
1545 | (math-sqrt | |
1546 | (math-add (math-sqr (nth 1 x)) 1))))) | |
1547 | ((eq (car x) 'intv) | |
1548 | (math-sort-intv (nth 1 x) | |
1549 | (calcFunc-arcsinh (nth 2 x)) | |
1550 | (calcFunc-arcsinh (nth 3 x)))) | |
1551 | ((or (equal x '(var inf var-inf)) | |
1552 | (equal x '(neg (var inf var-inf))) | |
1553 | (equal x '(var nan var-nan))) | |
1554 | x) | |
1555 | (t (calc-record-why 'numberp x) | |
491c3062 | 1556 | (list 'calcFunc-arcsinh x)))) |
136211a9 EZ |
1557 | (put 'calcFunc-arcsinh 'math-expandable t) |
1558 | ||
1559 | (defun calcFunc-arccosh (x) ; [N N] [Public] | |
1560 | (cond ((eq x 1) 0) | |
1561 | ((and (eq x -1) calc-symbolic-mode) | |
1562 | '(var pi var-pi)) | |
1563 | ((and (eq x 0) calc-symbolic-mode) | |
1564 | (math-div (math-mul '(var pi var-pi) '(var i var-i)) 2)) | |
1565 | (math-expand-formulas | |
1566 | (math-normalize | |
1567 | (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt | |
1568 | (list '- (list '^ x 2) 1)))))) | |
1569 | ((Math-numberp x) | |
1570 | (if calc-symbolic-mode (signal 'inexact-result nil)) | |
1571 | (if (Math-equal-int x -1) | |
1572 | (math-imaginary (math-pi)) | |
1573 | (math-with-extra-prec 2 | |
1574 | (if (or t ; need to do this even in the real case! | |
1575 | (memq (car-safe x) '(cplx polar))) | |
1576 | (let ((xp1 (math-add 1 x))) ; this gets the branch cuts right | |
1577 | (math-ln-raw | |
1578 | (math-add x (math-mul xp1 | |
1579 | (math-sqrt-raw | |
1580 | (math-div (math-sub | |
1581 | x | |
1582 | '(float 1 0)) | |
1583 | xp1)))))) | |
1584 | (math-ln-raw | |
1585 | (math-add x (math-sqrt-raw (math-add (math-sqr x) | |
1586 | '(float -1 0))))))))) | |
1587 | ((eq (car-safe x) 'sdev) | |
1588 | (math-make-sdev (calcFunc-arccosh (nth 1 x)) | |
1589 | (math-div (nth 2 x) | |
1590 | (math-sqrt | |
1591 | (math-add (math-sqr (nth 1 x)) -1))))) | |
1592 | ((eq (car x) 'intv) | |
1593 | (math-sort-intv (nth 1 x) | |
1594 | (calcFunc-arccosh (nth 2 x)) | |
1595 | (calcFunc-arccosh (nth 3 x)))) | |
1596 | ((or (equal x '(var inf var-inf)) | |
1597 | (equal x '(neg (var inf var-inf))) | |
1598 | (equal x '(var nan var-nan))) | |
1599 | x) | |
1600 | (t (calc-record-why 'numberp x) | |
491c3062 | 1601 | (list 'calcFunc-arccosh x)))) |
136211a9 EZ |
1602 | (put 'calcFunc-arccosh 'math-expandable t) |
1603 | ||
1604 | (defun calcFunc-arctanh (x) ; [N N] [Public] | |
1605 | (cond ((eq x 0) 0) | |
1606 | ((and (Math-equal-int x 1) calc-infinite-mode) | |
1607 | '(var inf var-inf)) | |
1608 | ((and (Math-equal-int x -1) calc-infinite-mode) | |
1609 | '(neg (var inf var-inf))) | |
1610 | (math-expand-formulas | |
1611 | (list '/ (list '- | |
1612 | (list 'calcFunc-ln (list '+ 1 x)) | |
1613 | (list 'calcFunc-ln (list '- 1 x))) 2)) | |
1614 | ((Math-numberp x) | |
1615 | (if calc-symbolic-mode (signal 'inexact-result nil)) | |
1616 | (math-with-extra-prec 2 | |
1617 | (if (or (memq (car-safe x) '(cplx polar)) | |
1618 | (Math-lessp 1 x)) | |
1619 | (math-mul (math-sub (math-ln-raw (math-add '(float 1 0) x)) | |
1620 | (math-ln-raw (math-sub '(float 1 0) x))) | |
1621 | '(float 5 -1)) | |
1622 | (if (and (math-equal-int x 1) calc-infinite-mode) | |
1623 | '(var inf var-inf) | |
1624 | (if (and (math-equal-int x -1) calc-infinite-mode) | |
1625 | '(neg (var inf var-inf)) | |
1626 | (math-mul (math-ln-raw (math-div (math-add '(float 1 0) x) | |
1627 | (math-sub 1 x))) | |
1628 | '(float 5 -1))))))) | |
1629 | ((eq (car-safe x) 'sdev) | |
1630 | (math-make-sdev (calcFunc-arctanh (nth 1 x)) | |
1631 | (math-div (nth 2 x) | |
1632 | (math-sub 1 (math-sqr (nth 1 x)))))) | |
1633 | ((eq (car x) 'intv) | |
1634 | (math-sort-intv (nth 1 x) | |
1635 | (calcFunc-arctanh (nth 2 x)) | |
1636 | (calcFunc-arctanh (nth 3 x)))) | |
1637 | ((equal x '(var nan var-nan)) | |
1638 | x) | |
1639 | (t (calc-record-why 'numberp x) | |
491c3062 | 1640 | (list 'calcFunc-arctanh x)))) |
136211a9 EZ |
1641 | (put 'calcFunc-arctanh 'math-expandable t) |
1642 | ||
1643 | ||
1644 | ;;; Convert A from HMS or degrees to radians. | |
1645 | (defun calcFunc-rad (a) ; [R R] [Public] | |
1646 | (cond ((or (Math-numberp a) | |
1647 | (eq (car a) 'intv)) | |
1648 | (math-with-extra-prec 2 | |
1649 | (math-mul a (math-pi-over-180)))) | |
1650 | ((eq (car a) 'hms) | |
1651 | (math-from-hms a 'rad)) | |
1652 | ((eq (car a) 'sdev) | |
1653 | (math-make-sdev (calcFunc-rad (nth 1 a)) | |
1654 | (calcFunc-rad (nth 2 a)))) | |
1655 | (math-expand-formulas | |
1656 | (math-div (math-mul a '(var pi var-pi)) 180)) | |
1657 | ((math-infinitep a) a) | |
491c3062 | 1658 | (t (list 'calcFunc-rad a)))) |
136211a9 EZ |
1659 | (put 'calcFunc-rad 'math-expandable t) |
1660 | ||
1661 | ;;; Convert A from HMS or radians to degrees. | |
1662 | (defun calcFunc-deg (a) ; [R R] [Public] | |
1663 | (cond ((or (Math-numberp a) | |
1664 | (eq (car a) 'intv)) | |
1665 | (math-with-extra-prec 2 | |
1666 | (math-div a (math-pi-over-180)))) | |
1667 | ((eq (car a) 'hms) | |
1668 | (math-from-hms a 'deg)) | |
1669 | ((eq (car a) 'sdev) | |
1670 | (math-make-sdev (calcFunc-deg (nth 1 a)) | |
1671 | (calcFunc-deg (nth 2 a)))) | |
1672 | (math-expand-formulas | |
1673 | (math-div (math-mul 180 a) '(var pi var-pi))) | |
1674 | ((math-infinitep a) a) | |
491c3062 | 1675 | (t (list 'calcFunc-deg a)))) |
136211a9 EZ |
1676 | (put 'calcFunc-deg 'math-expandable t) |
1677 | ||
491c3062 | 1678 | ;;; calc-math.el ends here |