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