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