Add arch tagline
[bpt/emacs.git] / lisp / calc / calc-math.el
CommitLineData
3132f345
CW
1;;; calc-math.el --- mathematical functions for Calc
2
58ba2f8f 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
f0fa15c5 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
3132f345
CW
5
6;; Author: David Gillespie <daveg@synaptics.com>
e8fff8ed 7;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9
EZ
8
9;; This file is part of GNU Emacs.
10
7c671b23
GM
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
075969b4 13;; the Free Software Foundation; either version 3, or (at your option)
7c671b23
GM
14;; any later version.
15
136211a9 16;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
136211a9 25
3132f345 26;;; Commentary:
136211a9 27
3132f345 28;;; Code:
136211a9
EZ
29
30;; This file is autoloaded from calc-ext.el.
136211a9 31
95995a85 32(require 'calc-ext)
136211a9
EZ
33(require 'calc-macs)
34
178b8baf
JB
35
36;;; Find out how many 9s in 9.9999... will give distinct Emacs floats,
37;;; then back off by one.
38
39(defvar math-emacs-precision
40 (let* ((n 1)
41 (x 9)
42 (xx (+ x (* 9 (expt 10 (- n))))))
43 (while (/= x xx)
44 (progn
45 (setq n (1+ n))
46 (setq x xx)
47 (setq xx (+ x (* 9 (expt 10 (- n)))))))
48 (1- n))
49 "The number of digits in an Emacs float.")
50
51;;; Find the largest power of 10 which is an Emacs float,
52;;; then back off by one so that any float d.dddd...eN
53;;; is an Emacs float, for acceptable d.dddd....
54
55(defvar math-largest-emacs-expt
56 (let ((x 1))
57 (while (condition-case nil
58 (expt 10.0 x)
59 (error nil))
60 (setq x (* 2 x)))
61 (setq x (/ x 2))
62 (while (condition-case nil
63 (expt 10.0 x)
64 (error nil))
65 (setq x (1+ x)))
66 (- x 2))
67 "The largest exponent which Calc will convert to an Emacs float.")
68
69(defvar math-smallest-emacs-expt
70 (let ((x -1))
71 (while (condition-case nil
72 (expt 10.0 x)
73 (error nil))
74 (setq x (* 2 x)))
75 (setq x (/ x 2))
76 (while (condition-case nil
77 (expt 10.0 x)
78 (error nil))
79 (setq x (1- x)))
80 (+ x 2))
81 "The smallest exponent which Calc will convert to an Emacs float.")
82
83(defun math-use-emacs-fn (fn x)
84 "Use the native Emacs function FN to evaluate the Calc number X.
85If this can't be done, return NIL."
86 (and
87 (<= calc-internal-prec math-emacs-precision)
88 (math-realp x)
89 (let* ((fx (math-float x))
90 (xpon (+ (nth 2 x) (1- (math-numdigs (nth 1 x))))))
91 (and (<= math-smallest-emacs-expt xpon)
92 (<= xpon math-largest-emacs-expt)
93 (condition-case nil
94 (math-read-number
95 (number-to-string
96 (funcall fn
97 (string-to-number (math-format-number (math-float x))))))
98 (error nil))))))
99
136211a9
EZ
100(defun calc-sqrt (arg)
101 (interactive "P")
102 (calc-slow-wrapper
103 (if (calc-is-inverse)
104 (calc-unary-op "^2" 'calcFunc-sqr arg)
491c3062 105 (calc-unary-op "sqrt" 'calcFunc-sqrt arg))))
136211a9
EZ
106
107(defun calc-isqrt (arg)
108 (interactive "P")
109 (calc-slow-wrapper
110 (if (calc-is-inverse)
111 (calc-unary-op "^2" 'calcFunc-sqr arg)
491c3062 112 (calc-unary-op "isqt" 'calcFunc-isqrt arg))))
136211a9
EZ
113
114
115(defun calc-hypot (arg)
116 (interactive "P")
117 (calc-slow-wrapper
491c3062 118 (calc-binary-op "hypt" 'calcFunc-hypot arg)))
136211a9
EZ
119
120(defun calc-ln (arg)
121 (interactive "P")
122 (calc-invert-func)
491c3062 123 (calc-exp arg))
136211a9
EZ
124
125(defun calc-log10 (arg)
126 (interactive "P")
127 (calc-hyperbolic-func)
491c3062 128 (calc-ln arg))
136211a9
EZ
129
130(defun calc-log (arg)
131 (interactive "P")
132 (calc-slow-wrapper
133 (if (calc-is-inverse)
134 (calc-binary-op "alog" 'calcFunc-alog arg)
491c3062 135 (calc-binary-op "log" 'calcFunc-log arg))))
136211a9
EZ
136
137(defun calc-ilog (arg)
138 (interactive "P")
139 (calc-slow-wrapper
140 (if (calc-is-inverse)
141 (calc-binary-op "alog" 'calcFunc-alog arg)
491c3062 142 (calc-binary-op "ilog" 'calcFunc-ilog arg))))
136211a9
EZ
143
144(defun calc-lnp1 (arg)
145 (interactive "P")
146 (calc-invert-func)
491c3062 147 (calc-expm1 arg))
136211a9
EZ
148
149(defun calc-exp (arg)
150 (interactive "P")
151 (calc-slow-wrapper
152 (if (calc-is-hyperbolic)
153 (if (calc-is-inverse)
154 (calc-unary-op "lg10" 'calcFunc-log10 arg)
155 (calc-unary-op "10^" 'calcFunc-exp10 arg))
156 (if (calc-is-inverse)
157 (calc-unary-op "ln" 'calcFunc-ln arg)
491c3062 158 (calc-unary-op "exp" 'calcFunc-exp arg)))))
136211a9
EZ
159
160(defun calc-expm1 (arg)
161 (interactive "P")
162 (calc-slow-wrapper
163 (if (calc-is-inverse)
164 (calc-unary-op "ln+1" 'calcFunc-lnp1 arg)
491c3062 165 (calc-unary-op "ex-1" 'calcFunc-expm1 arg))))
136211a9
EZ
166
167(defun calc-pi ()
168 (interactive)
169 (calc-slow-wrapper
170 (if (calc-is-inverse)
171 (if (calc-is-hyperbolic)
172 (if calc-symbolic-mode
173 (calc-pop-push-record 0 "phi" '(var phi var-phi))
174 (calc-pop-push-record 0 "phi" (math-phi)))
175 (if calc-symbolic-mode
176 (calc-pop-push-record 0 "gmma" '(var gamma var-gamma))
177 (calc-pop-push-record 0 "gmma" (math-gamma-const))))
178 (if (calc-is-hyperbolic)
179 (if calc-symbolic-mode
180 (calc-pop-push-record 0 "e" '(var e var-e))
181 (calc-pop-push-record 0 "e" (math-e)))
182 (if calc-symbolic-mode
183 (calc-pop-push-record 0 "pi" '(var pi var-pi))
491c3062 184 (calc-pop-push-record 0 "pi" (math-pi)))))))
136211a9
EZ
185
186(defun calc-sin (arg)
187 (interactive "P")
188 (calc-slow-wrapper
189 (if (calc-is-hyperbolic)
190 (if (calc-is-inverse)
191 (calc-unary-op "asnh" 'calcFunc-arcsinh arg)
192 (calc-unary-op "sinh" 'calcFunc-sinh arg))
193 (if (calc-is-inverse)
194 (calc-unary-op "asin" 'calcFunc-arcsin arg)
491c3062 195 (calc-unary-op "sin" 'calcFunc-sin arg)))))
136211a9
EZ
196
197(defun calc-arcsin (arg)
198 (interactive "P")
199 (calc-invert-func)
491c3062 200 (calc-sin arg))
136211a9
EZ
201
202(defun calc-sinh (arg)
203 (interactive "P")
204 (calc-hyperbolic-func)
491c3062 205 (calc-sin arg))
136211a9
EZ
206
207(defun calc-arcsinh (arg)
208 (interactive "P")
209 (calc-invert-func)
210 (calc-hyperbolic-func)
491c3062 211 (calc-sin arg))
136211a9 212
f53e6c20
JB
213(defun calc-sec (arg)
214 (interactive "P")
215 (calc-slow-wrapper
216 (if (calc-is-hyperbolic)
217 (calc-unary-op "sech" 'calcFunc-sech arg)
218 (calc-unary-op "sec" 'calcFunc-sec arg))))
219
220(defun calc-sech (arg)
221 (interactive "P")
222 (calc-hyperbolic-func)
223 (calc-sec arg))
224
136211a9
EZ
225(defun calc-cos (arg)
226 (interactive "P")
227 (calc-slow-wrapper
228 (if (calc-is-hyperbolic)
229 (if (calc-is-inverse)
230 (calc-unary-op "acsh" 'calcFunc-arccosh arg)
231 (calc-unary-op "cosh" 'calcFunc-cosh arg))
232 (if (calc-is-inverse)
233 (calc-unary-op "acos" 'calcFunc-arccos arg)
491c3062 234 (calc-unary-op "cos" 'calcFunc-cos arg)))))
136211a9
EZ
235
236(defun calc-arccos (arg)
237 (interactive "P")
238 (calc-invert-func)
491c3062 239 (calc-cos arg))
136211a9
EZ
240
241(defun calc-cosh (arg)
242 (interactive "P")
243 (calc-hyperbolic-func)
491c3062 244 (calc-cos arg))
136211a9
EZ
245
246(defun calc-arccosh (arg)
247 (interactive "P")
248 (calc-invert-func)
249 (calc-hyperbolic-func)
491c3062 250 (calc-cos arg))
136211a9 251
f53e6c20
JB
252(defun calc-csc (arg)
253 (interactive "P")
254 (calc-slow-wrapper
255 (if (calc-is-hyperbolic)
256 (calc-unary-op "csch" 'calcFunc-csch arg)
257 (calc-unary-op "csc" 'calcFunc-csc arg))))
258
259(defun calc-csch (arg)
260 (interactive "P")
261 (calc-hyperbolic-func)
262 (calc-csc arg))
263
136211a9
EZ
264(defun calc-sincos ()
265 (interactive)
266 (calc-slow-wrapper
267 (if (calc-is-inverse)
268 (calc-enter-result 1 "asnc" (list 'calcFunc-arcsincos (calc-top-n 1)))
491c3062 269 (calc-enter-result 1 "sncs" (list 'calcFunc-sincos (calc-top-n 1))))))
136211a9
EZ
270
271(defun calc-tan (arg)
272 (interactive "P")
273 (calc-slow-wrapper
274 (if (calc-is-hyperbolic)
275 (if (calc-is-inverse)
276 (calc-unary-op "atnh" 'calcFunc-arctanh arg)
277 (calc-unary-op "tanh" 'calcFunc-tanh arg))
278 (if (calc-is-inverse)
279 (calc-unary-op "atan" 'calcFunc-arctan arg)
491c3062 280 (calc-unary-op "tan" 'calcFunc-tan arg)))))
136211a9
EZ
281
282(defun calc-arctan (arg)
283 (interactive "P")
284 (calc-invert-func)
491c3062 285 (calc-tan arg))
136211a9
EZ
286
287(defun calc-tanh (arg)
288 (interactive "P")
289 (calc-hyperbolic-func)
491c3062 290 (calc-tan arg))
136211a9
EZ
291
292(defun calc-arctanh (arg)
293 (interactive "P")
294 (calc-invert-func)
295 (calc-hyperbolic-func)
491c3062 296 (calc-tan arg))
136211a9 297
f53e6c20
JB
298(defun calc-cot (arg)
299 (interactive "P")
300 (calc-slow-wrapper
301 (if (calc-is-hyperbolic)
302 (calc-unary-op "coth" 'calcFunc-coth arg)
303 (calc-unary-op "cot" 'calcFunc-cot arg))))
304
eb1ef455 305(defun calc-coth (arg)
f53e6c20
JB
306 (interactive "P")
307 (calc-hyperbolic-func)
eb1ef455 308 (calc-cot arg))
f53e6c20 309
136211a9
EZ
310(defun calc-arctan2 ()
311 (interactive)
312 (calc-slow-wrapper
491c3062 313 (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2)))))
136211a9
EZ
314
315(defun calc-conj (arg)
316 (interactive "P")
317 (calc-wrapper
491c3062 318 (calc-unary-op "conj" 'calcFunc-conj arg)))
136211a9
EZ
319
320(defun calc-imaginary ()
321 (interactive)
322 (calc-slow-wrapper
491c3062 323 (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1)))))
136211a9 324
136211a9
EZ
325(defun calc-to-degrees (arg)
326 (interactive "P")
327 (calc-wrapper
491c3062 328 (calc-unary-op ">deg" 'calcFunc-deg arg)))
136211a9
EZ
329
330(defun calc-to-radians (arg)
331 (interactive "P")
332 (calc-wrapper
491c3062 333 (calc-unary-op ">rad" 'calcFunc-rad arg)))
136211a9
EZ
334
335
336(defun calc-degrees-mode (arg)
337 (interactive "p")
338 (cond ((= arg 1)
339 (calc-wrapper
340 (calc-change-mode 'calc-angle-mode 'deg)
3132f345 341 (message "Angles measured in degrees")))
136211a9
EZ
342 ((= arg 2) (calc-radians-mode))
343 ((= arg 3) (calc-hms-mode))
491c3062 344 (t (error "Prefix argument out of range"))))
136211a9
EZ
345
346(defun calc-radians-mode ()
347 (interactive)
348 (calc-wrapper
349 (calc-change-mode 'calc-angle-mode 'rad)
3132f345 350 (message "Angles measured in radians")))
136211a9
EZ
351
352
353;;; Compute the integer square-root floor(sqrt(A)). A > 0. [I I] [Public]
354;;; This method takes advantage of the fact that Newton's method starting
355;;; with an overestimate always works, even using truncating integer division!
356(defun math-isqrt (a)
357 (cond ((Math-zerop a) a)
358 ((not (math-natnump a))
359 (math-reject-arg a 'natnump))
360 ((integerp a)
361 (math-isqrt-small a))
362 (t
491c3062 363 (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a))))))))
136211a9
EZ
364
365(defun calcFunc-isqrt (a)
366 (if (math-realp a)
367 (math-isqrt (math-floor a))
491c3062 368 (math-floor (math-sqrt a))))
136211a9
EZ
369
370
f0529b5b 371;;; This returns (flag . result) where the flag is t if A is a perfect square.
136211a9
EZ
372(defun math-isqrt-bignum (a) ; [P.l L]
373 (let ((len (length a)))
374 (if (= (% len 2) 0)
375 (let* ((top (nthcdr (- len 2) a)))
376 (math-isqrt-bignum-iter
377 a
98888d77 378 (math-scale-bignum-digit-size
136211a9
EZ
379 (math-bignum-big
380 (1+ (math-isqrt-small
98888d77 381 (+ (* (nth 1 top) math-bignum-digit-size) (car top)))))
136211a9
EZ
382 (1- (/ len 2)))))
383 (let* ((top (nth (1- len) a)))
384 (math-isqrt-bignum-iter
385 a
98888d77 386 (math-scale-bignum-digit-size
136211a9 387 (list (1+ (math-isqrt-small top)))
491c3062 388 (/ len 2)))))))
136211a9
EZ
389
390(defun math-isqrt-bignum-iter (a guess) ; [l L l]
391 (math-working "isqrt" (cons 'bigpos guess))
392 (let* ((q (math-div-bignum a guess))
393 (s (math-add-bignum (car q) guess))
394 (g2 (math-div2-bignum s))
395 (comp (math-compare-bignum g2 guess)))
396 (if (< comp 0)
397 (math-isqrt-bignum-iter a g2)
398 (cons (and (= comp 0)
399 (math-zerop-bignum (cdr q))
400 (= (% (car s) 2) 0))
491c3062 401 guess))))
136211a9
EZ
402
403(defun math-zerop-bignum (a)
404 (and (eq (car a) 0)
405 (progn
406 (while (eq (car (setq a (cdr a))) 0))
491c3062 407 (null a))))
136211a9 408
98888d77 409(defun math-scale-bignum-digit-size (a n) ; [L L S]
136211a9
EZ
410 (while (> n 0)
411 (setq a (cons 0 a)
412 n (1- n)))
491c3062 413 a)
136211a9
EZ
414
415(defun math-isqrt-small (a) ; A > 0. [S S]
98888d77
JB
416 (let ((g (cond ((>= a 1000000) 10000)
417 ((>= a 10000) 1000)
136211a9
EZ
418 ((>= a 100) 100)
419 (t 10)))
420 g2)
421 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
422 (setq g g2))
491c3062 423 g))
136211a9
EZ
424
425
426
427
428;;; Compute the square root of a number.
429;;; [T N] if possible, else [F N] if possible, else [C N]. [Public]
430(defun math-sqrt (a)
431 (or
432 (and (Math-zerop a) a)
433 (and (math-known-nonposp a)
434 (math-imaginary (math-sqrt (math-neg a))))
435 (and (integerp a)
436 (let ((sqrt (math-isqrt-small a)))
437 (if (= (* sqrt sqrt) a)
438 sqrt
439 (if calc-symbolic-mode
440 (list 'calcFunc-sqrt a)
441 (math-sqrt-float (math-float a) (math-float sqrt))))))
442 (and (eq (car-safe a) 'bigpos)
443 (let* ((res (math-isqrt-bignum (cdr a)))
444 (sqrt (math-normalize (cons 'bigpos (cdr res)))))
445 (if (car res)
446 sqrt
447 (if calc-symbolic-mode
448 (list 'calcFunc-sqrt a)
449 (math-sqrt-float (math-float a) (math-float sqrt))))))
450 (and (eq (car-safe a) 'frac)
451 (let* ((num-res (math-isqrt-bignum (cdr (Math-bignum-test (nth 1 a)))))
452 (num-sqrt (math-normalize (cons 'bigpos (cdr num-res))))
453 (den-res (math-isqrt-bignum (cdr (Math-bignum-test (nth 2 a)))))
454 (den-sqrt (math-normalize (cons 'bigpos (cdr den-res)))))
455 (if (and (car num-res) (car den-res))
456 (list 'frac num-sqrt den-sqrt)
457 (if calc-symbolic-mode
458 (if (or (car num-res) (car den-res))
459 (math-div (if (car num-res)
460 num-sqrt (list 'calcFunc-sqrt (nth 1 a)))
461 (if (car den-res)
462 den-sqrt (list 'calcFunc-sqrt (nth 2 a))))
463 (list 'calcFunc-sqrt a))
464 (math-sqrt-float (math-float a)
465 (math-div (math-float num-sqrt) den-sqrt))))))
466 (and (eq (car-safe a) 'float)
467 (if calc-symbolic-mode
468 (if (= (% (nth 2 a) 2) 0)
469 (let ((res (math-isqrt-bignum
470 (cdr (Math-bignum-test (nth 1 a))))))
471 (if (car res)
472 (math-make-float (math-normalize
473 (cons 'bigpos (cdr res)))
474 (/ (nth 2 a) 2))
475 (signal 'inexact-result nil)))
476 (signal 'inexact-result nil))
477 (math-sqrt-float a)))
478 (and (eq (car-safe a) 'cplx)
479 (math-with-extra-prec 2
480 (let* ((d (math-abs a))
481 (imag (math-sqrt (math-mul (math-sub d (nth 1 a))
482 '(float 5 -1)))))
483 (list 'cplx
484 (math-sqrt (math-mul (math-add d (nth 1 a)) '(float 5 -1)))
485 (if (math-negp (nth 2 a)) (math-neg imag) imag)))))
486 (and (eq (car-safe a) 'polar)
487 (list 'polar
488 (math-sqrt (nth 1 a))
489 (math-mul (nth 2 a) '(float 5 -1))))
490 (and (eq (car-safe a) 'sdev)
491 (let ((sqrt (math-sqrt (nth 1 a))))
492 (math-make-sdev sqrt
493 (math-div (nth 2 a) (math-mul sqrt 2)))))
494 (and (eq (car-safe a) 'intv)
495 (not (math-negp (nth 2 a)))
496 (math-make-intv (nth 1 a) (math-sqrt (nth 2 a)) (math-sqrt (nth 3 a))))
497 (and (eq (car-safe a) '*)
498 (or (math-known-nonnegp (nth 1 a))
499 (math-known-nonnegp (nth 2 a)))
500 (math-mul (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a))))
501 (and (eq (car-safe a) '/)
502 (or (and (math-known-nonnegp (nth 2 a))
503 (math-div (math-sqrt (nth 1 a)) (math-sqrt (nth 2 a))))
504 (and (math-known-nonnegp (nth 1 a))
505 (not (math-equal-int (nth 1 a) 1))
506 (math-mul (math-sqrt (nth 1 a))
507 (math-sqrt (math-div 1 (nth 2 a)))))))
508 (and (eq (car-safe a) '^)
509 (math-known-evenp (nth 2 a))
510 (math-known-realp (nth 1 a))
511 (math-abs (math-pow (nth 1 a) (math-div (nth 2 a) 2))))
512 (let ((inf (math-infinitep a)))
513 (and inf
514 (math-mul (math-sqrt (math-infinite-dir a inf)) inf)))
515 (progn
516 (calc-record-why 'numberp a)
491c3062 517 (list 'calcFunc-sqrt a))))
3132f345 518(defalias 'calcFunc-sqrt 'math-sqrt)
136211a9
EZ
519
520(defun math-infinite-dir (a &optional inf)
521 (or inf (setq inf (math-infinitep a)))
491c3062 522 (math-normalize (math-expr-subst a inf 1)))
136211a9
EZ
523
524(defun math-sqrt-float (a &optional guess) ; [F F F]
525 (if calc-symbolic-mode
526 (signal 'inexact-result nil)
491c3062 527 (math-with-extra-prec 1 (math-sqrt-raw a guess))))
136211a9
EZ
528
529(defun math-sqrt-raw (a &optional guess) ; [F F F]
530 (if (not (Math-posp a))
531 (math-sqrt a)
17cc361e
JB
532 (cond
533 ((math-use-emacs-fn 'sqrt a))
534 (t
535 (if (null guess)
536 (let ((ldiff (- (math-numdigs (nth 1 a)) 6)))
537 (or (= (% (+ (nth 2 a) ldiff) 2) 0) (setq ldiff (1+ ldiff)))
538 (setq guess (math-make-float (math-isqrt-small
539 (math-scale-int (nth 1 a) (- ldiff)))
540 (/ (+ (nth 2 a) ldiff) 2)))))
541 (math-sqrt-float-iter a guess)))))
136211a9
EZ
542
543(defun math-sqrt-float-iter (a guess) ; [F F F]
544 (math-working "sqrt" guess)
545 (let ((g2 (math-mul-float (math-add-float guess (math-div-float a guess))
546 '(float 5 -1))))
547 (if (math-nearly-equal-float g2 guess)
548 g2
491c3062 549 (math-sqrt-float-iter a g2))))
136211a9
EZ
550
551;;; True if A and B differ only in the last digit of precision. [P F F]
552(defun math-nearly-equal-float (a b)
553 (let ((ediff (- (nth 2 a) (nth 2 b))))
554 (cond ((= ediff 0) ;; Expanded out for speed
555 (setq ediff (math-add (Math-integer-neg (nth 1 a)) (nth 1 b)))
556 (or (eq ediff 0)
557 (and (not (consp ediff))
558 (< ediff 10)
559 (> ediff -10)
560 (= (math-numdigs (nth 1 a)) calc-internal-prec))))
561 ((= ediff 1)
562 (setq ediff (math-add (Math-integer-neg (nth 1 b))
563 (math-scale-int (nth 1 a) 1)))
564 (and (not (consp ediff))
565 (< ediff 10)
566 (> ediff -10)
567 (= (math-numdigs (nth 1 b)) calc-internal-prec)))
568 ((= ediff -1)
569 (setq ediff (math-add (Math-integer-neg (nth 1 a))
570 (math-scale-int (nth 1 b) 1)))
571 (and (not (consp ediff))
572 (< ediff 10)
573 (> ediff -10)
491c3062 574 (= (math-numdigs (nth 1 a)) calc-internal-prec))))))
136211a9
EZ
575
576(defun math-nearly-equal (a b) ; [P N N] [Public]
577 (setq a (math-float a))
578 (setq b (math-float b))
579 (if (eq (car a) 'polar) (setq a (math-complex a)))
580 (if (eq (car b) 'polar) (setq b (math-complex b)))
581 (if (eq (car a) 'cplx)
582 (if (eq (car b) 'cplx)
583 (and (or (math-nearly-equal-float (nth 1 a) (nth 1 b))
584 (and (math-nearly-zerop-float (nth 1 a) (nth 2 a))
585 (math-nearly-zerop-float (nth 1 b) (nth 2 b))))
586 (or (math-nearly-equal-float (nth 2 a) (nth 2 b))
587 (and (math-nearly-zerop-float (nth 2 a) (nth 1 a))
588 (math-nearly-zerop-float (nth 2 b) (nth 1 b)))))
589 (and (math-nearly-equal-float (nth 1 a) b)
590 (math-nearly-zerop-float (nth 2 a) b)))
591 (if (eq (car b) 'cplx)
592 (and (math-nearly-equal-float a (nth 1 b))
593 (math-nearly-zerop-float a (nth 2 b)))
491c3062 594 (math-nearly-equal-float a b))))
136211a9
EZ
595
596;;; True if A is nearly zero compared to B. [P F F]
597(defun math-nearly-zerop-float (a b)
598 (or (eq (nth 1 a) 0)
599 (<= (+ (math-numdigs (nth 1 a)) (nth 2 a))
491c3062 600 (1+ (- (+ (math-numdigs (nth 1 b)) (nth 2 b)) calc-internal-prec)))))
136211a9
EZ
601
602(defun math-nearly-zerop (a b) ; [P N R] [Public]
603 (setq a (math-float a))
604 (setq b (math-float b))
605 (if (eq (car a) 'cplx)
606 (and (math-nearly-zerop-float (nth 1 a) b)
607 (math-nearly-zerop-float (nth 2 a) b))
608 (if (eq (car a) 'polar)
609 (math-nearly-zerop-float (nth 1 a) b)
491c3062 610 (math-nearly-zerop-float a b))))
136211a9
EZ
611
612;;; This implementation could be improved, accuracy-wise.
613(defun math-hypot (a b)
614 (cond ((Math-zerop a) (math-abs b))
615 ((Math-zerop b) (math-abs a))
616 ((not (Math-scalarp a))
617 (if (math-infinitep a)
618 (if (math-infinitep b)
619 (if (equal a b)
620 a
621 '(var nan var-nan))
622 a)
623 (calc-record-why 'scalarp a)
624 (list 'calcFunc-hypot a b)))
625 ((not (Math-scalarp b))
626 (if (math-infinitep b)
627 b
628 (calc-record-why 'scalarp b)
629 (list 'calcFunc-hypot a b)))
630 ((and (Math-numberp a) (Math-numberp b))
631 (math-with-extra-prec 1
632 (math-sqrt (math-add (calcFunc-abssqr a) (calcFunc-abssqr b)))))
633 ((eq (car-safe a) 'hms)
634 (if (eq (car-safe b) 'hms) ; this helps sdev's of hms forms
635 (math-to-hms (math-hypot (math-from-hms a 'deg)
636 (math-from-hms b 'deg)))
637 (math-to-hms (math-hypot (math-from-hms a 'deg) b))))
638 ((eq (car-safe b) 'hms)
639 (math-to-hms (math-hypot a (math-from-hms b 'deg))))
491c3062 640 (t nil)))
3132f345 641(defalias 'calcFunc-hypot 'math-hypot)
136211a9
EZ
642
643(defun calcFunc-sqr (x)
491c3062 644 (math-pow x 2))
136211a9
EZ
645
646
647
648(defun math-nth-root (a n)
649 (cond ((= n 2) (math-sqrt a))
650 ((Math-zerop a) a)
651 ((Math-negp a) nil)
652 ((Math-integerp a)
653 (let ((root (math-nth-root-integer a n)))
654 (if (car root)
655 (cdr root)
656 (and (not calc-symbolic-mode)
657 (math-nth-root-float (math-float a) n
658 (math-float (cdr root)))))))
659 ((eq (car-safe a) 'frac)
660 (let* ((num-root (math-nth-root-integer (nth 1 a) n))
661 (den-root (math-nth-root-integer (nth 2 a) n)))
662 (if (and (car num-root) (car den-root))
663 (list 'frac (cdr num-root) (cdr den-root))
664 (and (not calc-symbolic-mode)
665 (math-nth-root-float
666 (math-float a) n
667 (math-div-float (math-float (cdr num-root))
668 (math-float (cdr den-root))))))))
669 ((eq (car-safe a) 'float)
670 (and (not calc-symbolic-mode)
671 (math-nth-root-float a n)))
672 ((eq (car-safe a) 'polar)
673 (let ((root (math-nth-root (nth 1 a) n)))
674 (and root (list 'polar root (math-div (nth 2 a) n)))))
491c3062 675 (t nil)))
136211a9 676
86498823
JB
677;; The variables math-nrf-n, math-nrf-nf and math-nrf-nfm1 are local
678;; to math-nth-root-float, but are used by math-nth-root-float-iter,
679;; which is called by math-nth-root-float.
680(defvar math-nrf-n)
681(defvar math-nrf-nf)
682(defvar math-nrf-nfm1)
683
684(defun math-nth-root-float (a math-nrf-n &optional guess)
136211a9
EZ
685 (math-inexact-result)
686 (math-with-extra-prec 1
86498823
JB
687 (let ((math-nrf-nf (math-float math-nrf-n))
688 (math-nrf-nfm1 (math-float (1- math-nrf-n))))
136211a9
EZ
689 (math-nth-root-float-iter a (or guess
690 (math-make-float
691 1 (/ (+ (math-numdigs (nth 1 a))
692 (nth 2 a)
86498823
JB
693 (/ math-nrf-n 2))
694 math-nrf-n)))))))
136211a9 695
86498823 696(defun math-nth-root-float-iter (a guess)
136211a9 697 (math-working "root" guess)
86498823 698 (let ((g2 (math-div-float (math-add-float (math-mul math-nrf-nfm1 guess)
136211a9 699 (math-div-float
86498823
JB
700 a (math-ipow guess (1- math-nrf-n))))
701 math-nrf-nf)))
136211a9
EZ
702 (if (math-nearly-equal-float g2 guess)
703 g2
491c3062 704 (math-nth-root-float-iter a g2))))
136211a9 705
86498823
JB
706;; The variable math-nri-n is local to math-nth-root-integer, but
707;; is used by math-nth-root-int-iter, which is called by
708;; math-nth-root-int.
709(defvar math-nri-n)
710
711(defun math-nth-root-integer (a math-nri-n &optional guess) ; [I I S]
136211a9
EZ
712 (math-nth-root-int-iter a (or guess
713 (math-scale-int 1 (/ (+ (math-numdigs a)
86498823
JB
714 (1- math-nri-n))
715 math-nri-n)))))
136211a9 716
86498823 717(defun math-nth-root-int-iter (a guess)
136211a9 718 (math-working "root" guess)
86498823
JB
719 (let* ((q (math-idivmod a (math-ipow guess (1- math-nri-n))))
720 (s (math-add (car q) (math-mul (1- math-nri-n) guess)))
721 (g2 (math-idivmod s math-nri-n)))
136211a9
EZ
722 (if (Math-natnum-lessp (car g2) guess)
723 (math-nth-root-int-iter a (car g2))
724 (cons (and (equal (car g2) guess)
725 (eq (cdr q) 0)
726 (eq (cdr g2) 0))
491c3062 727 guess))))
136211a9
EZ
728
729(defun calcFunc-nroot (x n)
730 (calcFunc-pow x (if (integerp n)
731 (math-make-frac 1 n)
491c3062 732 (math-div 1 n))))
136211a9
EZ
733
734
735
736
737;;;; Transcendental functions.
738
739;;; All of these functions are defined on the complex plane.
740;;; (Branch cuts, etc. follow Steele's Common Lisp book.)
741
742;;; Most functions increase calc-internal-prec by 2 digits, then round
743;;; down afterward. "-raw" functions use the current precision, require
744;;; their arguments to be in float (or complex float) format, and always
745;;; work in radians (where applicable).
746
747(defun math-to-radians (a) ; [N N]
748 (cond ((eq (car-safe a) 'hms)
749 (math-from-hms a 'rad))
750 ((memq calc-angle-mode '(deg hms))
751 (math-mul a (math-pi-over-180)))
491c3062 752 (t a)))
136211a9
EZ
753
754(defun math-from-radians (a) ; [N N]
755 (cond ((eq calc-angle-mode 'deg)
756 (if (math-constp a)
757 (math-div a (math-pi-over-180))
758 (list 'calcFunc-deg a)))
759 ((eq calc-angle-mode 'hms)
760 (math-to-hms a 'rad))
491c3062 761 (t a)))
136211a9
EZ
762
763(defun math-to-radians-2 (a) ; [N N]
764 (cond ((eq (car-safe a) 'hms)
765 (math-from-hms a 'rad))
766 ((memq calc-angle-mode '(deg hms))
767 (if calc-symbolic-mode
768 (math-div (math-mul a '(var pi var-pi)) 180)
769 (math-mul a (math-pi-over-180))))
491c3062 770 (t a)))
136211a9
EZ
771
772(defun math-from-radians-2 (a) ; [N N]
773 (cond ((memq calc-angle-mode '(deg hms))
774 (if calc-symbolic-mode
775 (math-div (math-mul 180 a) '(var pi var-pi))
776 (math-div a (math-pi-over-180))))
491c3062 777 (t a)))
136211a9
EZ
778
779
780
781;;; Sine, cosine, and tangent.
782
783(defun calcFunc-sin (x) ; [N N] [Public]
784 (cond ((and (integerp x)
785 (if (eq calc-angle-mode 'deg)
786 (= (% x 90) 0)
787 (= x 0)))
788 (aref [0 1 0 -1] (math-mod (/ x 90) 4)))
789 ((Math-scalarp x)
790 (math-with-extra-prec 2
791 (math-sin-raw (math-to-radians (math-float x)))))
792 ((eq (car x) 'sdev)
793 (if (math-constp x)
794 (math-with-extra-prec 2
795 (let* ((xx (math-to-radians (math-float (nth 1 x))))
796 (xs (math-to-radians (math-float (nth 2 x))))
797 (sc (math-sin-cos-raw xx)))
798 (math-make-sdev (car sc) (math-mul xs (cdr sc)))))
799 (math-make-sdev (calcFunc-sin (nth 1 x))
800 (math-mul (nth 2 x) (calcFunc-cos (nth 1 x))))))
801 ((and (eq (car x) 'intv) (math-intv-constp x))
802 (calcFunc-cos (math-sub x (math-quarter-circle nil))))
803 ((equal x '(var nan var-nan))
804 x)
805 (t (calc-record-why 'scalarp x)
491c3062 806 (list 'calcFunc-sin x))))
136211a9
EZ
807
808(defun calcFunc-cos (x) ; [N N] [Public]
809 (cond ((and (integerp x)
810 (if (eq calc-angle-mode 'deg)
811 (= (% x 90) 0)
812 (= x 0)))
813 (aref [1 0 -1 0] (math-mod (/ x 90) 4)))
814 ((Math-scalarp x)
815 (math-with-extra-prec 2
816 (math-cos-raw (math-to-radians (math-float x)))))
817 ((eq (car x) 'sdev)
818 (if (math-constp x)
819 (math-with-extra-prec 2
820 (let* ((xx (math-to-radians (math-float (nth 1 x))))
821 (xs (math-to-radians (math-float (nth 2 x))))
822 (sc (math-sin-cos-raw xx)))
823 (math-make-sdev (cdr sc) (math-mul xs (car sc)))))
824 (math-make-sdev (calcFunc-cos (nth 1 x))
825 (math-mul (nth 2 x) (calcFunc-sin (nth 1 x))))))
826 ((and (eq (car x) 'intv) (math-intv-constp x))
827 (math-with-extra-prec 2
828 (let* ((xx (math-to-radians (math-float x)))
829 (na (math-floor (math-div (nth 2 xx) (math-pi))))
830 (nb (math-floor (math-div (nth 3 xx) (math-pi))))
831 (span (math-sub nb na)))
832 (if (memq span '(0 1))
833 (let ((int (math-sort-intv (nth 1 x)
834 (math-cos-raw (nth 2 xx))
835 (math-cos-raw (nth 3 xx)))))
836 (if (eq span 1)
837 (if (math-evenp na)
838 (math-make-intv (logior (nth 1 x) 2)
839 -1
840 (nth 3 int))
841 (math-make-intv (logior (nth 1 x) 1)
842 (nth 2 int)
843 1))
844 int))
845 (list 'intv 3 -1 1)))))
846 ((equal x '(var nan var-nan))
847 x)
848 (t (calc-record-why 'scalarp x)
491c3062 849 (list 'calcFunc-cos x))))
136211a9
EZ
850
851(defun calcFunc-sincos (x) ; [V N] [Public]
852 (if (Math-scalarp x)
853 (math-with-extra-prec 2
854 (let ((sc (math-sin-cos-raw (math-to-radians (math-float x)))))
855 (list 'vec (cdr sc) (car sc)))) ; the vector [cos, sin]
491c3062 856 (list 'vec (calcFunc-sin x) (calcFunc-cos x))))
136211a9
EZ
857
858(defun calcFunc-tan (x) ; [N N] [Public]
859 (cond ((and (integerp x)
860 (if (eq calc-angle-mode 'deg)
861 (= (% x 180) 0)
862 (= x 0)))
863 0)
864 ((Math-scalarp x)
865 (math-with-extra-prec 2
866 (math-tan-raw (math-to-radians (math-float x)))))
867 ((eq (car x) 'sdev)
868 (if (math-constp x)
869 (math-with-extra-prec 2
870 (let* ((xx (math-to-radians (math-float (nth 1 x))))
871 (xs (math-to-radians (math-float (nth 2 x))))
872 (sc (math-sin-cos-raw xx)))
873 (if (and (math-zerop (cdr sc)) (not calc-infinite-mode))
874 (progn
875 (calc-record-why "*Division by zero")
876 (list 'calcFunc-tan x))
877 (math-make-sdev (math-div-float (car sc) (cdr sc))
878 (math-div-float xs (math-sqr (cdr sc)))))))
879 (math-make-sdev (calcFunc-tan (nth 1 x))
880 (math-div (nth 2 x)
881 (math-sqr (calcFunc-cos (nth 1 x)))))))
882 ((and (eq (car x) 'intv) (math-intv-constp x))
883 (or (math-with-extra-prec 2
884 (let* ((xx (math-to-radians (math-float x)))
885 (na (math-floor (math-div (math-sub (nth 2 xx)
886 (math-pi-over-2))
887 (math-pi))))
888 (nb (math-floor (math-div (math-sub (nth 3 xx)
889 (math-pi-over-2))
890 (math-pi)))))
891 (and (equal na nb)
892 (math-sort-intv (nth 1 x)
893 (math-tan-raw (nth 2 xx))
894 (math-tan-raw (nth 3 xx))))))
895 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
896 ((equal x '(var nan var-nan))
897 x)
898 (t (calc-record-why 'scalarp x)
491c3062 899 (list 'calcFunc-tan x))))
136211a9 900
f53e6c20
JB
901(defun calcFunc-sec (x)
902 (cond ((and (integerp x)
903 (eq calc-angle-mode 'deg)
904 (= (% x 180) 0))
905 (if (= (% x 360) 0)
906 1
907 -1))
908 ((and (integerp x)
909 (eq calc-angle-mode 'rad)
910 (= x 0))
911 1)
912 ((Math-scalarp x)
913 (math-with-extra-prec 2
914 (math-sec-raw (math-to-radians (math-float x)))))
915 ((eq (car x) 'sdev)
916 (if (math-constp x)
917 (math-with-extra-prec 2
918 (let* ((xx (math-to-radians (math-float (nth 1 x))))
919 (xs (math-to-radians (math-float (nth 2 x))))
920 (sc (math-sin-cos-raw xx)))
921 (if (and (math-zerop (cdr sc))
922 (not calc-infinite-mode))
923 (progn
924 (calc-record-why "*Division by zero")
925 (list 'calcFunc-sec x))
926 (math-make-sdev (math-div-float '(float 1 0) (cdr sc))
927 (math-div-float
928 (math-mul xs (car sc))
929 (math-sqr (cdr sc)))))))
930 (math-make-sdev (calcFunc-sec (nth 1 x))
931 (math-div
932 (math-mul (nth 2 x)
933 (calcFunc-sin (nth 1 x)))
934 (math-sqr (calcFunc-cos (nth 1 x)))))))
935 ((and (eq (car x) 'intv)
936 (math-intv-constp x))
937 (math-with-extra-prec 2
938 (let* ((xx (math-to-radians (math-float x)))
939 (na (math-floor (math-div (math-sub (nth 2 xx)
940 (math-pi-over-2))
941 (math-pi))))
942 (nb (math-floor (math-div (math-sub (nth 3 xx)
943 (math-pi-over-2))
944 (math-pi))))
945 (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
946 (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
947 (span (math-sub nbb naa)))
948 (if (not (equal na nb))
949 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
950 (let ((int (math-sort-intv (nth 1 x)
951 (math-sec-raw (nth 2 xx))
952 (math-sec-raw (nth 3 xx)))))
953 (if (eq span 1)
954 (if (math-evenp (math-div (math-add naa 1) 2))
955 (math-make-intv (logior (nth 1 int) 2)
956 1
957 (nth 3 int))
958 (math-make-intv (logior (nth 1 int) 1)
959 (nth 2 int)
960 -1))
961 int))))))
962 ((equal x '(var nan var-nan))
963 x)
964 (t (calc-record-why 'scalarp x)
965 (list 'calcFunc-sec x))))
966
967(defun calcFunc-csc (x)
968 (cond ((and (integerp x)
969 (eq calc-angle-mode 'deg)
970 (= (% (- x 90) 180) 0))
971 (if (= (% (- x 90) 360) 0)
972 1
973 -1))
974 ((Math-scalarp x)
975 (math-with-extra-prec 2
976 (math-csc-raw (math-to-radians (math-float x)))))
977 ((eq (car x) 'sdev)
978 (if (math-constp x)
979 (math-with-extra-prec 2
980 (let* ((xx (math-to-radians (math-float (nth 1 x))))
981 (xs (math-to-radians (math-float (nth 2 x))))
982 (sc (math-sin-cos-raw xx)))
983 (if (and (math-zerop (car sc))
984 (not calc-infinite-mode))
985 (progn
986 (calc-record-why "*Division by zero")
987 (list 'calcFunc-csc x))
988 (math-make-sdev (math-div-float '(float 1 0) (car sc))
989 (math-div-float
990 (math-mul xs (cdr sc))
991 (math-sqr (car sc)))))))
992 (math-make-sdev (calcFunc-csc (nth 1 x))
993 (math-div
994 (math-mul (nth 2 x)
995 (calcFunc-cos (nth 1 x)))
996 (math-sqr (calcFunc-sin (nth 1 x)))))))
997 ((and (eq (car x) 'intv)
998 (math-intv-constp x))
999 (math-with-extra-prec 2
1000 (let* ((xx (math-to-radians (math-float x)))
1001 (na (math-floor (math-div (nth 2 xx) (math-pi))))
1002 (nb (math-floor (math-div (nth 3 xx) (math-pi))))
1003 (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
1004 (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
1005 (span (math-sub nbb naa)))
1006 (if (not (equal na nb))
1007 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
1008 (let ((int (math-sort-intv (nth 1 x)
1009 (math-csc-raw (nth 2 xx))
1010 (math-csc-raw (nth 3 xx)))))
1011 (if (eq span 1)
1012 (if (math-evenp (math-div naa 2))
1013 (math-make-intv (logior (nth 1 int) 2)
1014 1
1015 (nth 3 int))
1016 (math-make-intv (logior (nth 1 int) 1)
1017 (nth 2 int)
1018 -1))
1019 int))))))
1020 ((equal x '(var nan var-nan))
1021 x)
1022 (t (calc-record-why 'scalarp x)
1023 (list 'calcFunc-csc x))))
1024
1025(defun calcFunc-cot (x) ; [N N] [Public]
1026 (cond ((and (integerp x)
1027 (if (eq calc-angle-mode 'deg)
1028 (= (% (- x 90) 180) 0)
1029 (= x 0)))
1030 0)
1031 ((Math-scalarp x)
1032 (math-with-extra-prec 2
1033 (math-cot-raw (math-to-radians (math-float x)))))
1034 ((eq (car x) 'sdev)
1035 (if (math-constp x)
1036 (math-with-extra-prec 2
1037 (let* ((xx (math-to-radians (math-float (nth 1 x))))
1038 (xs (math-to-radians (math-float (nth 2 x))))
1039 (sc (math-sin-cos-raw xx)))
1040 (if (and (math-zerop (car sc)) (not calc-infinite-mode))
1041 (progn
1042 (calc-record-why "*Division by zero")
1043 (list 'calcFunc-cot x))
1044 (math-make-sdev (math-div-float (cdr sc) (car sc))
1045 (math-div-float xs (math-sqr (car sc)))))))
1046 (math-make-sdev (calcFunc-cot (nth 1 x))
1047 (math-div (nth 2 x)
1048 (math-sqr (calcFunc-sin (nth 1 x)))))))
1049 ((and (eq (car x) 'intv) (math-intv-constp x))
1050 (or (math-with-extra-prec 2
1051 (let* ((xx (math-to-radians (math-float x)))
1052 (na (math-floor (math-div (nth 2 xx) (math-pi))))
eb1ef455 1053 (nb (math-floor (math-div (nth 3 xx) (math-pi)))))
f53e6c20
JB
1054 (and (equal na nb)
1055 (math-sort-intv (nth 1 x)
1056 (math-cot-raw (nth 2 xx))
eb1ef455 1057 (math-cot-raw (nth 3 xx))))))
f53e6c20
JB
1058 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
1059 ((equal x '(var nan var-nan))
1060 x)
1061 (t (calc-record-why 'scalarp x)
1062 (list 'calcFunc-cot x))))
1063
136211a9
EZ
1064(defun math-sin-raw (x) ; [N N]
1065 (cond ((eq (car x) 'cplx)
1066 (let* ((expx (math-exp-raw (nth 2 x)))
1067 (expmx (math-div-float '(float 1 0) expx))
1068 (sc (math-sin-cos-raw (nth 1 x))))
1069 (list 'cplx
1070 (math-mul-float (car sc)
1071 (math-mul-float (math-add-float expx expmx)
1072 '(float 5 -1)))
1073 (math-mul-float (cdr sc)
1074 (math-mul-float (math-sub-float expx expmx)
1075 '(float 5 -1))))))
1076 ((eq (car x) 'polar)
1077 (math-polar (math-sin-raw (math-complex x))))
1078 ((Math-integer-negp (nth 1 x))
1079 (math-neg-float (math-sin-raw (math-neg-float x))))
1080 ((math-lessp-float '(float 7 0) x) ; avoid inf loops due to roundoff
1081 (math-sin-raw (math-mod x (math-two-pi))))
491c3062 1082 (t (math-sin-raw-2 x x))))
136211a9
EZ
1083
1084(defun math-cos-raw (x) ; [N N]
1085 (if (eq (car-safe x) 'polar)
1086 (math-polar (math-cos-raw (math-complex x)))
491c3062 1087 (math-sin-raw (math-sub (math-pi-over-2) x))))
136211a9 1088
f53e6c20
JB
1089(defun math-sec-raw (x) ; [N N]
1090 (cond ((eq (car x) 'cplx)
1091 (let* ((x (math-mul x '(float 1 0)))
1092 (expx (math-exp-raw (nth 2 x)))
1093 (expmx (math-div-float '(float 1 0) expx))
1094 (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
1095 (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
1096 (sc (math-sin-cos-raw (nth 1 x)))
1097 (d (math-add-float
1098 (math-mul-float (math-sqr (car sc))
1099 (math-sqr sh))
1100 (math-mul-float (math-sqr (cdr sc))
1101 (math-sqr ch)))))
1102 (and (not (eq (nth 1 d) 0))
1103 (list 'cplx
1104 (math-div-float (math-mul-float (cdr sc) ch) d)
1105 (math-div-float (math-mul-float (car sc) sh) d)))))
1106 ((eq (car x) 'polar)
1107 (math-polar (math-sec-raw (math-complex x))))
1108 (t
1109 (let ((cs (math-cos-raw x)))
1110 (if (eq cs 0)
1111 (math-div 1 0)
1112 (math-div-float '(float 1 0) cs))))))
1113
1114(defun math-csc-raw (x) ; [N N]
1115 (cond ((eq (car x) 'cplx)
1116 (let* ((x (math-mul x '(float 1 0)))
1117 (expx (math-exp-raw (nth 2 x)))
1118 (expmx (math-div-float '(float 1 0) expx))
1119 (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
1120 (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
1121 (sc (math-sin-cos-raw (nth 1 x)))
1122 (d (math-add-float
1123 (math-mul-float (math-sqr (car sc))
1124 (math-sqr ch))
1125 (math-mul-float (math-sqr (cdr sc))
1126 (math-sqr sh)))))
1127 (and (not (eq (nth 1 d) 0))
1128 (list 'cplx
1129 (math-div-float (math-mul-float (car sc) ch) d)
1130 (math-div-float (math-mul-float (cdr sc) sh) d)))))
1131 ((eq (car x) 'polar)
6ec30302 1132 (math-polar (math-csc-raw (math-complex x))))
f53e6c20
JB
1133 (t
1134 (let ((sn (math-sin-raw x)))
1135 (if (eq sn 0)
1136 (math-div 1 0)
1137 (math-div-float '(float 1 0) sn))))))
1138
1139(defun math-cot-raw (x) ; [N N]
1140 (cond ((eq (car x) 'cplx)
1141 (let* ((x (math-mul x '(float 1 0)))
1142 (expx (math-exp-raw (nth 2 x)))
1143 (expmx (math-div-float '(float 1 0) expx))
1144 (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
1145 (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
1146 (sc (math-sin-cos-raw (nth 1 x)))
1147 (d (math-add-float
1148 (math-sqr (car sc))
1149 (math-sqr sh))))
1150 (and (not (eq (nth 1 d) 0))
1151 (list 'cplx
1152 (math-div-float
1153 (math-mul-float (car sc) (cdr sc))
1154 d)
1155 (math-neg
1156 (math-div-float
1157 (math-mul-float sh ch)
1158 d))))))
1159 ((eq (car x) 'polar)
1160 (math-polar (math-cot-raw (math-complex x))))
1161 (t
1162 (let ((sc (math-sin-cos-raw x)))
1163 (if (eq (nth 1 (car sc)) 0)
1164 (math-div (cdr sc) 0)
1165 (math-div-float (cdr sc) (car sc)))))))
1166
1167
136211a9
EZ
1168;;; This could use a smarter method: Reduce x as in math-sin-raw, then
1169;;; compute either sin(x) or cos(x), whichever is smaller, and compute
1170;;; the other using the identity sin(x)^2 + cos(x)^2 = 1.
1171(defun math-sin-cos-raw (x) ; [F.F F] (result is (sin x . cos x))
491c3062 1172 (cons (math-sin-raw x) (math-cos-raw x)))
136211a9
EZ
1173
1174(defun math-tan-raw (x) ; [N N]
1175 (cond ((eq (car x) 'cplx)
1176 (let* ((x (math-mul x '(float 2 0)))
1177 (expx (math-exp-raw (nth 2 x)))
1178 (expmx (math-div-float '(float 1 0) expx))
1179 (sc (math-sin-cos-raw (nth 1 x)))
1180 (d (math-add-float (cdr sc)
1181 (math-mul-float (math-add-float expx expmx)
1182 '(float 5 -1)))))
1183 (and (not (eq (nth 1 d) 0))
1184 (list 'cplx
1185 (math-div-float (car sc) d)
1186 (math-div-float (math-mul-float (math-sub-float expx
1187 expmx)
1188 '(float 5 -1)) d)))))
1189 ((eq (car x) 'polar)
1190 (math-polar (math-tan-raw (math-complex x))))
1191 (t
1192 (let ((sc (math-sin-cos-raw x)))
1193 (if (eq (nth 1 (cdr sc)) 0)
1194 (math-div (car sc) 0)
491c3062 1195 (math-div-float (car sc) (cdr sc)))))))
136211a9
EZ
1196
1197(defun math-sin-raw-2 (x orgx) ; This avoids poss of inf recursion. [F F]
1198 (let ((xmpo2 (math-sub-float (math-pi-over-2) x)))
1199 (cond ((Math-integer-negp (nth 1 xmpo2))
1200 (math-neg-float (math-sin-raw-2 (math-sub-float x (math-pi))
1201 orgx)))
1202 ((math-lessp-float (math-pi-over-4) x)
1203 (math-cos-raw-2 xmpo2 orgx))
1204 ((math-lessp-float x (math-neg (math-pi-over-4)))
1205 (math-neg (math-cos-raw-2 (math-add (math-pi-over-2) x) orgx)))
1206 ((math-nearly-zerop-float x orgx) '(float 0 0))
17cc361e 1207 ((math-use-emacs-fn 'sin x))
136211a9 1208 (calc-symbolic-mode (signal 'inexact-result nil))
491c3062 1209 (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x)))))))
136211a9
EZ
1210
1211(defun math-cos-raw-2 (x orgx) ; [F F]
1212 (cond ((math-nearly-zerop-float x orgx) '(float 1 0))
17cc361e 1213 ((math-use-emacs-fn 'cos x))
136211a9
EZ
1214 (calc-symbolic-mode (signal 'inexact-result nil))
1215 (t (let ((xnegsqr (math-neg-float (math-sqr-float x))))
1216 (math-sin-series
1217 (math-add-float '(float 1 0)
1218 (math-mul-float xnegsqr '(float 5 -1)))
491c3062 1219 24 5 xnegsqr xnegsqr)))))
136211a9
EZ
1220
1221(defun math-sin-series (sum nfac n x xnegsqr)
1222 (math-working "sin" sum)
1223 (let* ((nextx (math-mul-float x xnegsqr))
1224 (nextsum (math-add-float sum (math-div-float nextx
1225 (math-float nfac)))))
1226 (if (math-nearly-equal-float sum nextsum)
1227 sum
1228 (math-sin-series nextsum (math-mul nfac (* n (1+ n)))
491c3062 1229 (+ n 2) nextx xnegsqr))))
136211a9
EZ
1230
1231
1232;;; Inverse sine, cosine, tangent.
1233
1234(defun calcFunc-arcsin (x) ; [N N] [Public]
1235 (cond ((eq x 0) 0)
1236 ((and (eq x 1) (eq calc-angle-mode 'deg)) 90)
1237 ((and (eq x -1) (eq calc-angle-mode 'deg)) -90)
1238 (calc-symbolic-mode (signal 'inexact-result nil))
1239 ((Math-numberp x)
1240 (math-with-extra-prec 2
1241 (math-from-radians (math-arcsin-raw (math-float x)))))
1242 ((eq (car x) 'sdev)
1243 (math-make-sdev (calcFunc-arcsin (nth 1 x))
1244 (math-from-radians
1245 (math-div (nth 2 x)
1246 (math-sqrt
1247 (math-sub 1 (math-sqr (nth 1 x))))))))
1248 ((eq (car x) 'intv)
1249 (math-sort-intv (nth 1 x)
1250 (calcFunc-arcsin (nth 2 x))
1251 (calcFunc-arcsin (nth 3 x))))
1252 ((equal x '(var nan var-nan))
1253 x)
1254 (t (calc-record-why 'numberp x)
491c3062 1255 (list 'calcFunc-arcsin x))))
136211a9
EZ
1256
1257(defun calcFunc-arccos (x) ; [N N] [Public]
1258 (cond ((eq x 1) 0)
1259 ((and (eq x 0) (eq calc-angle-mode 'deg)) 90)
1260 ((and (eq x -1) (eq calc-angle-mode 'deg)) 180)
1261 (calc-symbolic-mode (signal 'inexact-result nil))
1262 ((Math-numberp x)
1263 (math-with-extra-prec 2
1264 (math-from-radians (math-arccos-raw (math-float x)))))
1265 ((eq (car x) 'sdev)
1266 (math-make-sdev (calcFunc-arccos (nth 1 x))
1267 (math-from-radians
1268 (math-div (nth 2 x)
1269 (math-sqrt
1270 (math-sub 1 (math-sqr (nth 1 x))))))))
1271 ((eq (car x) 'intv)
1272 (math-sort-intv (nth 1 x)
1273 (calcFunc-arccos (nth 2 x))
1274 (calcFunc-arccos (nth 3 x))))
1275 ((equal x '(var nan var-nan))
1276 x)
1277 (t (calc-record-why 'numberp x)
491c3062 1278 (list 'calcFunc-arccos x))))
136211a9
EZ
1279
1280(defun calcFunc-arctan (x) ; [N N] [Public]
1281 (cond ((eq x 0) 0)
1282 ((and (eq x 1) (eq calc-angle-mode 'deg)) 45)
1283 ((and (eq x -1) (eq calc-angle-mode 'deg)) -45)
1284 ((Math-numberp x)
1285 (math-with-extra-prec 2
1286 (math-from-radians (math-arctan-raw (math-float x)))))
1287 ((eq (car x) 'sdev)
1288 (math-make-sdev (calcFunc-arctan (nth 1 x))
1289 (math-from-radians
1290 (math-div (nth 2 x)
1291 (math-add 1 (math-sqr (nth 1 x)))))))
1292 ((eq (car x) 'intv)
1293 (math-sort-intv (nth 1 x)
1294 (calcFunc-arctan (nth 2 x))
1295 (calcFunc-arctan (nth 3 x))))
1296 ((equal x '(var inf var-inf))
1297 (math-quarter-circle t))
1298 ((equal x '(neg (var inf var-inf)))
1299 (math-neg (math-quarter-circle t)))
1300 ((equal x '(var nan var-nan))
1301 x)
1302 (t (calc-record-why 'numberp x)
491c3062 1303 (list 'calcFunc-arctan x))))
136211a9
EZ
1304
1305(defun math-arcsin-raw (x) ; [N N]
1306 (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
1307 (if (or (memq (car x) '(cplx polar))
1308 (memq (car a) '(cplx polar)))
1309 (math-with-extra-prec 2 ; use extra precision for difficult case
1310 (math-mul '(cplx 0 -1)
1311 (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
491c3062 1312 (math-arctan2-raw x a))))
136211a9
EZ
1313
1314(defun math-arccos-raw (x) ; [N N]
491c3062 1315 (math-sub (math-pi-over-2) (math-arcsin-raw x)))
136211a9
EZ
1316
1317(defun math-arctan-raw (x) ; [N N]
1318 (cond ((memq (car x) '(cplx polar))
1319 (math-with-extra-prec 2 ; extra-extra
1320 (math-div (math-sub
1321 (math-ln-raw (math-add 1 (math-mul '(cplx 0 1) x)))
1322 (math-ln-raw (math-add 1 (math-mul '(cplx 0 -1) x))))
1323 '(cplx 0 2))))
1324 ((Math-integer-negp (nth 1 x))
1325 (math-neg-float (math-arctan-raw (math-neg-float x))))
1326 ((math-zerop x) x)
17cc361e 1327 ((math-use-emacs-fn 'atan x))
136211a9
EZ
1328 (calc-symbolic-mode (signal 'inexact-result nil))
1329 ((math-equal-int x 1) (math-pi-over-4))
1330 ((math-equal-int x -1) (math-neg (math-pi-over-4)))
1331 ((math-lessp-float '(float 414214 -6) x) ; if x > sqrt(2) - 1, reduce
1332 (if (math-lessp-float '(float 1 0) x)
1333 (math-sub-float (math-mul-float (math-pi) '(float 5 -1))
1334 (math-arctan-raw (math-div-float '(float 1 0) x)))
1335 (math-sub-float (math-mul-float (math-pi) '(float 25 -2))
1336 (math-arctan-raw (math-div-float
1337 (math-sub-float '(float 1 0) x)
1338 (math-add-float '(float 1 0)
1339 x))))))
491c3062 1340 (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x))))))
136211a9
EZ
1341
1342(defun math-arctan-series (sum n x xnegsqr)
1343 (math-working "arctan" sum)
1344 (let* ((nextx (math-mul-float x xnegsqr))
1345 (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
1346 (if (math-nearly-equal-float sum nextsum)
1347 sum
491c3062 1348 (math-arctan-series nextsum (+ n 2) nextx xnegsqr))))
136211a9
EZ
1349
1350(defun calcFunc-arctan2 (y x) ; [F R R] [Public]
1351 (if (Math-anglep y)
1352 (if (Math-anglep x)
1353 (math-with-extra-prec 2
1354 (math-from-radians (math-arctan2-raw (math-float y)
1355 (math-float x))))
1356 (calc-record-why 'anglep x)
1357 (list 'calcFunc-arctan2 y x))
1358 (if (and (or (math-infinitep x) (math-anglep x))
1359 (or (math-infinitep y) (math-anglep y)))
1360 (progn
1361 (if (math-posp x)
1362 (setq x 1)
1363 (if (math-negp x)
1364 (setq x -1)
1365 (or (math-zerop x)
1366 (setq x nil))))
1367 (if (math-posp y)
1368 (setq y 1)
1369 (if (math-negp y)
1370 (setq y -1)
1371 (or (math-zerop y)
1372 (setq y nil))))
1373 (if (and y x)
1374 (calcFunc-arctan2 y x)
1375 '(var nan var-nan)))
1376 (calc-record-why 'anglep y)
491c3062 1377 (list 'calcFunc-arctan2 y x))))
136211a9
EZ
1378
1379(defun math-arctan2-raw (y x) ; [F R R]
1380 (cond ((math-zerop y)
1381 (if (math-negp x) (math-pi)
1382 (if (or (math-floatp x) (math-floatp y)) '(float 0 0) 0)))
1383 ((math-zerop x)
1384 (if (math-posp y)
1385 (math-pi-over-2)
1386 (math-neg (math-pi-over-2))))
1387 ((math-posp x)
1388 (math-arctan-raw (math-div-float y x)))
1389 ((math-posp y)
1390 (math-add-float (math-arctan-raw (math-div-float y x))
1391 (math-pi)))
1392 (t
1393 (math-sub-float (math-arctan-raw (math-div-float y x))
491c3062 1394 (math-pi)))))
136211a9
EZ
1395
1396(defun calcFunc-arcsincos (x) ; [V N] [Public]
1397 (if (and (Math-vectorp x)
1398 (= (length x) 3))
1399 (calcFunc-arctan2 (nth 2 x) (nth 1 x))
491c3062 1400 (math-reject-arg x "*Two-element vector expected")))
136211a9
EZ
1401
1402
1403
1404;;; Exponential function.
1405
1406(defun calcFunc-exp (x) ; [N N] [Public]
1407 (cond ((eq x 0) 1)
1408 ((and (memq x '(1 -1)) calc-symbolic-mode)
1409 (if (eq x 1) '(var e var-e) (math-div 1 '(var e var-e))))
1410 ((Math-numberp x)
1411 (math-with-extra-prec 2 (math-exp-raw (math-float x))))
1412 ((eq (car-safe x) 'sdev)
1413 (let ((ex (calcFunc-exp (nth 1 x))))
1414 (math-make-sdev ex (math-mul (nth 2 x) ex))))
1415 ((eq (car-safe x) 'intv)
1416 (math-make-intv (nth 1 x) (calcFunc-exp (nth 2 x))
1417 (calcFunc-exp (nth 3 x))))
1418 ((equal x '(var inf var-inf))
1419 x)
1420 ((equal x '(neg (var inf var-inf)))
1421 0)
1422 ((equal x '(var nan var-nan))
1423 x)
1424 (t (calc-record-why 'numberp x)
491c3062 1425 (list 'calcFunc-exp x))))
136211a9
EZ
1426
1427(defun calcFunc-expm1 (x) ; [N N] [Public]
1428 (cond ((eq x 0) 0)
1429 ((math-zerop x) '(float 0 0))
1430 (calc-symbolic-mode (signal 'inexact-result nil))
1431 ((Math-numberp x)
1432 (math-with-extra-prec 2
1433 (let ((x (math-float x)))
1434 (if (and (eq (car x) 'float)
1435 (math-lessp-float x '(float 1 0))
1436 (math-lessp-float '(float -1 0) x))
1437 (math-exp-minus-1-raw x)
1438 (math-add (math-exp-raw x) -1)))))
1439 ((eq (car-safe x) 'sdev)
1440 (if (math-constp x)
1441 (let ((ex (calcFunc-expm1 (nth 1 x))))
1442 (math-make-sdev ex (math-mul (nth 2 x) (math-add ex 1))))
1443 (math-make-sdev (calcFunc-expm1 (nth 1 x))
1444 (math-mul (nth 2 x) (calcFunc-exp (nth 1 x))))))
1445 ((eq (car-safe x) 'intv)
1446 (math-make-intv (nth 1 x)
1447 (calcFunc-expm1 (nth 2 x))
1448 (calcFunc-expm1 (nth 3 x))))
1449 ((equal x '(var inf var-inf))
1450 x)
1451 ((equal x '(neg (var inf var-inf)))
1452 -1)
1453 ((equal x '(var nan var-nan))
1454 x)
1455 (t (calc-record-why 'numberp x)
491c3062 1456 (list 'calcFunc-expm1 x))))
136211a9
EZ
1457
1458(defun calcFunc-exp10 (x) ; [N N] [Public]
1459 (if (eq x 0)
1460 1
491c3062 1461 (math-pow '(float 1 1) x)))
136211a9
EZ
1462
1463(defun math-exp-raw (x) ; [N N]
1464 (cond ((math-zerop x) '(float 1 0))
1465 (calc-symbolic-mode (signal 'inexact-result nil))
1466 ((eq (car x) 'cplx)
1467 (let ((expx (math-exp-raw (nth 1 x)))
1468 (sc (math-sin-cos-raw (nth 2 x))))
1469 (list 'cplx
1470 (math-mul-float expx (cdr sc))
1471 (math-mul-float expx (car sc)))))
1472 ((eq (car x) 'polar)
1473 (let ((xc (math-complex x)))
1474 (list 'polar
1475 (math-exp-raw (nth 1 xc))
1476 (math-from-radians (nth 2 xc)))))
178b8baf 1477 ((math-use-emacs-fn 'exp x))
136211a9
EZ
1478 ((or (math-lessp-float '(float 5 -1) x)
1479 (math-lessp-float x '(float -5 -1)))
1480 (if (math-lessp-float '(float 921035 1) x)
1481 (math-overflow)
1482 (if (math-lessp-float x '(float -921035 1))
1483 (math-underflow)))
1484 (let* ((two-x (math-mul-float x '(float 2 0)))
1485 (hint (math-scale-int (nth 1 two-x) (nth 2 two-x)))
1486 (hfrac (math-sub-float x (math-mul-float (math-float hint)
1487 '(float 5 -1)))))
1488 (math-mul-float (math-ipow (math-sqrt-e) hint)
1489 (math-add-float '(float 1 0)
1490 (math-exp-minus-1-raw hfrac)))))
491c3062 1491 (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x)))))
136211a9
EZ
1492
1493(defun math-exp-minus-1-raw (x) ; [F F]
491c3062 1494 (math-exp-series x 2 3 x x))
136211a9
EZ
1495
1496(defun math-exp-series (sum nfac n xpow x)
1497 (math-working "exp" sum)
1498 (let* ((nextx (math-mul-float xpow x))
1499 (nextsum (math-add-float sum (math-div-float nextx
1500 (math-float nfac)))))
1501 (if (math-nearly-equal-float sum nextsum)
1502 sum
491c3062 1503 (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x))))
136211a9
EZ
1504
1505
1506
1507;;; Logarithms.
1508
1509(defun calcFunc-ln (x) ; [N N] [Public]
1510 (cond ((math-zerop x)
1511 (if calc-infinite-mode
1512 '(neg (var inf var-inf))
1513 (math-reject-arg x "*Logarithm of zero")))
1514 ((eq x 1) 0)
1515 ((Math-numberp x)
1516 (math-with-extra-prec 2 (math-ln-raw (math-float x))))
1517 ((eq (car-safe x) 'sdev)
1518 (math-make-sdev (calcFunc-ln (nth 1 x))
1519 (math-div (nth 2 x) (nth 1 x))))
1520 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1521 (Math-zerop (nth 2 x))
1522 (not (math-intv-constp x))))
1523 (let ((calc-infinite-mode t))
1524 (math-make-intv (nth 1 x) (calcFunc-ln (nth 2 x))
1525 (calcFunc-ln (nth 3 x)))))
1526 ((equal x '(var e var-e))
1527 1)
1528 ((and (eq (car-safe x) '^)
1529 (equal (nth 1 x) '(var e var-e))
1530 (math-known-realp (nth 2 x)))
1531 (nth 2 x))
1532 ((math-infinitep x)
1533 (if (equal x '(var nan var-nan))
1534 x
1535 '(var inf var-inf)))
1536 (t (calc-record-why 'numberp x)
491c3062 1537 (list 'calcFunc-ln x))))
136211a9
EZ
1538
1539(defun calcFunc-log10 (x) ; [N N] [Public]
1540 (cond ((math-equal-int x 1)
1541 (if (math-floatp x) '(float 0 0) 0))
1542 ((and (Math-integerp x)
1543 (math-posp x)
1544 (let ((res (math-integer-log x 10)))
1545 (and (car res)
1546 (setq x (cdr res)))))
1547 x)
1548 ((and (eq (car-safe x) 'frac)
1549 (eq (nth 1 x) 1)
1550 (let ((res (math-integer-log (nth 2 x) 10)))
1551 (and (car res)
1552 (setq x (- (cdr res))))))
1553 x)
1554 ((math-zerop x)
1555 (if calc-infinite-mode
1556 '(neg (var inf var-inf))
1557 (math-reject-arg x "*Logarithm of zero")))
1558 (calc-symbolic-mode (signal 'inexact-result nil))
1559 ((Math-numberp x)
1560 (math-with-extra-prec 2
1561 (let ((xf (math-float x)))
1562 (if (eq (nth 1 xf) 0)
1563 (math-reject-arg x "*Logarithm of zero"))
1564 (if (Math-integer-posp (nth 1 xf))
1565 (if (eq (nth 1 xf) 1) ; log10(1*10^n) = n
1566 (math-float (nth 2 xf))
1567 (let ((xdigs (1- (math-numdigs (nth 1 xf)))))
1568 (math-add-float
1569 (math-div-float (math-ln-raw-2
1570 (list 'float (nth 1 xf) (- xdigs)))
1571 (math-ln-10))
1572 (math-float (+ (nth 2 xf) xdigs)))))
1573 (math-div (calcFunc-ln xf) (math-ln-10))))))
1574 ((eq (car-safe x) 'sdev)
1575 (math-make-sdev (calcFunc-log10 (nth 1 x))
1576 (math-div (nth 2 x)
1577 (math-mul (nth 1 x) (math-ln-10)))))
1578 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1579 (not (math-intv-constp x))))
1580 (math-make-intv (nth 1 x)
1581 (calcFunc-log10 (nth 2 x))
1582 (calcFunc-log10 (nth 3 x))))
1583 ((math-infinitep x)
1584 (if (equal x '(var nan var-nan))
1585 x
1586 '(var inf var-inf)))
1587 (t (calc-record-why 'numberp x)
491c3062 1588 (list 'calcFunc-log10 x))))
136211a9
EZ
1589
1590(defun calcFunc-log (x &optional b) ; [N N N] [Public]
1591 (cond ((or (null b) (equal b '(var e var-e)))
1592 (math-normalize (list 'calcFunc-ln x)))
1593 ((or (eq b 10) (equal b '(float 1 1)))
1594 (math-normalize (list 'calcFunc-log10 x)))
1595 ((math-zerop x)
1596 (if calc-infinite-mode
1597 (math-div (calcFunc-ln x) (calcFunc-ln b))
1598 (math-reject-arg x "*Logarithm of zero")))
1599 ((math-zerop b)
1600 (if calc-infinite-mode
1601 (math-div (calcFunc-ln x) (calcFunc-ln b))
1602 (math-reject-arg b "*Logarithm of zero")))
1603 ((math-equal-int b 1)
1604 (if calc-infinite-mode
1605 (math-div (calcFunc-ln x) 0)
1606 (math-reject-arg b "*Logarithm base one")))
1607 ((math-equal-int x 1)
86498823 1608 (if (math-floatp b) '(float 0 0) 0))
136211a9
EZ
1609 ((and (Math-ratp x) (Math-ratp b)
1610 (math-posp x) (math-posp b)
1611 (let* ((sign 1) (inv nil)
1612 (xx (if (Math-lessp 1 x)
1613 x
1614 (setq sign -1)
1615 (math-div 1 x)))
1616 (bb (if (Math-lessp 1 b)
1617 b
1618 (setq sign (- sign))
1619 (math-div 1 b)))
1620 (res (if (Math-lessp xx bb)
1621 (setq inv (math-integer-log bb xx))
1622 (math-integer-log xx bb))))
1623 (and (car res)
1624 (setq x (if inv
1625 (math-div 1 (* sign (cdr res)))
1626 (* sign (cdr res)))))))
1627 x)
1628 (calc-symbolic-mode (signal 'inexact-result nil))
1629 ((and (Math-numberp x) (Math-numberp b))
1630 (math-with-extra-prec 2
1631 (math-div (math-ln-raw (math-float x))
1632 (math-log-base-raw b))))
1633 ((and (eq (car-safe x) 'sdev)
1634 (Math-numberp b))
1635 (math-make-sdev (calcFunc-log (nth 1 x) b)
1636 (math-div (nth 2 x)
1637 (math-mul (nth 1 x)
1638 (math-log-base-raw b)))))
1639 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1640 (not (math-intv-constp x)))
1641 (math-realp b))
1642 (math-make-intv (nth 1 x)
1643 (calcFunc-log (nth 2 x) b)
1644 (calcFunc-log (nth 3 x) b)))
1645 ((or (eq (car-safe x) 'intv) (eq (car-safe b) 'intv))
1646 (math-div (calcFunc-ln x) (calcFunc-ln b)))
1647 ((or (math-infinitep x)
1648 (math-infinitep b))
1649 (math-div (calcFunc-ln x) (calcFunc-ln b)))
1650 (t (if (Math-numberp b)
1651 (calc-record-why 'numberp x)
1652 (calc-record-why 'numberp b))
491c3062 1653 (list 'calcFunc-log x b))))
136211a9
EZ
1654
1655(defun calcFunc-alog (x &optional b)
1656 (cond ((or (null b) (equal b '(var e var-e)))
1657 (math-normalize (list 'calcFunc-exp x)))
491c3062 1658 (t (math-pow b x))))
136211a9
EZ
1659
1660(defun calcFunc-ilog (x b)
1661 (if (and (math-natnump x) (not (eq x 0))
1662 (math-natnump b) (not (eq b 0)))
1663 (if (eq b 1)
1664 (math-reject-arg x "*Logarithm base one")
1665 (if (Math-natnum-lessp x b)
1666 0
1667 (cdr (math-integer-log x b))))
491c3062 1668 (math-floor (calcFunc-log x b))))
136211a9
EZ
1669
1670(defun math-integer-log (x b)
1671 (let ((pows (list b))
1672 (pow (math-sqr b))
1673 next
1674 sum n)
1675 (while (not (Math-lessp x pow))
1676 (setq pows (cons pow pows)
1677 pow (math-sqr pow)))
1678 (setq n (lsh 1 (1- (length pows)))
1679 sum n
1680 pow (car pows))
1681 (while (and (setq pows (cdr pows))
1682 (Math-lessp pow x))
1683 (setq n (/ n 2)
1684 next (math-mul pow (car pows)))
1685 (or (Math-lessp x next)
1686 (setq pow next
1687 sum (+ sum n))))
491c3062 1688 (cons (equal pow x) sum)))
136211a9
EZ
1689
1690
3132f345 1691(defvar math-log-base-cache nil)
136211a9
EZ
1692(defun math-log-base-raw (b) ; [N N]
1693 (if (not (and (equal (car math-log-base-cache) b)
1694 (eq (nth 1 math-log-base-cache) calc-internal-prec)))
1695 (setq math-log-base-cache (list b calc-internal-prec
1696 (math-ln-raw (math-float b)))))
491c3062 1697 (nth 2 math-log-base-cache))
136211a9
EZ
1698
1699(defun calcFunc-lnp1 (x) ; [N N] [Public]
1700 (cond ((Math-equal-int x -1)
1701 (if calc-infinite-mode
1702 '(neg (var inf var-inf))
1703 (math-reject-arg x "*Logarithm of zero")))
1704 ((eq x 0) 0)
1705 ((math-zerop x) '(float 0 0))
1706 (calc-symbolic-mode (signal 'inexact-result nil))
1707 ((Math-numberp x)
1708 (math-with-extra-prec 2
1709 (let ((x (math-float x)))
1710 (if (and (eq (car x) 'float)
1711 (math-lessp-float x '(float 5 -1))
1712 (math-lessp-float '(float -5 -1) x))
1713 (math-ln-plus-1-raw x)
1714 (math-ln-raw (math-add-float x '(float 1 0)))))))
1715 ((eq (car-safe x) 'sdev)
1716 (math-make-sdev (calcFunc-lnp1 (nth 1 x))
1717 (math-div (nth 2 x) (math-add (nth 1 x) 1))))
1718 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1719 (not (math-intv-constp x))))
1720 (math-make-intv (nth 1 x)
1721 (calcFunc-lnp1 (nth 2 x))
1722 (calcFunc-lnp1 (nth 3 x))))
1723 ((math-infinitep x)
1724 (if (equal x '(var nan var-nan))
1725 x
1726 '(var inf var-inf)))
1727 (t (calc-record-why 'numberp x)
491c3062 1728 (list 'calcFunc-lnp1 x))))
136211a9
EZ
1729
1730(defun math-ln-raw (x) ; [N N] --- must be float format!
1731 (cond ((eq (car-safe x) 'cplx)
1732 (list 'cplx
1733 (math-mul-float (math-ln-raw
1734 (math-add-float (math-sqr-float (nth 1 x))
1735 (math-sqr-float (nth 2 x))))
1736 '(float 5 -1))
1737 (math-arctan2-raw (nth 2 x) (nth 1 x))))
1738 ((eq (car x) 'polar)
1739 (math-polar (list 'cplx
1740 (math-ln-raw (nth 1 x))
1741 (math-to-radians (nth 2 x)))))
1742 ((Math-equal-int x 1)
1743 '(float 0 0))
1744 (calc-symbolic-mode (signal 'inexact-result nil))
1745 ((math-posp (nth 1 x)) ; positive and real
17cc361e
JB
1746 (cond
1747 ((math-use-emacs-fn 'log x))
1748 (t
1749 (let ((xdigs (1- (math-numdigs (nth 1 x)))))
1750 (math-add-float (math-ln-raw-2 (list 'float (nth 1 x) (- xdigs)))
1751 (math-mul-float (math-float (+ (nth 2 x) xdigs))
1752 (math-ln-10)))))))
136211a9
EZ
1753 ((math-zerop x)
1754 (math-reject-arg x "*Logarithm of zero"))
1755 ((eq calc-complex-mode 'polar) ; negative and real
1756 (math-polar
1757 (list 'cplx ; negative and real
1758 (math-ln-raw (math-neg-float x))
1759 (math-pi))))
1760 (t (list 'cplx ; negative and real
1761 (math-ln-raw (math-neg-float x))
491c3062 1762 (math-pi)))))
136211a9
EZ
1763
1764(defun math-ln-raw-2 (x) ; [F F]
1765 (cond ((math-lessp-float '(float 14 -1) x)
1766 (math-add-float (math-ln-raw-2 (math-mul-float x '(float 5 -1)))
1767 (math-ln-2)))
1768 (t ; now .7 < x <= 1.4
1769 (math-ln-raw-3 (math-div-float (math-sub-float x '(float 1 0))
491c3062 1770 (math-add-float x '(float 1 0)))))))
136211a9
EZ
1771
1772(defun math-ln-raw-3 (x) ; [F F]
1773 (math-mul-float (math-ln-raw-series x 3 x (math-sqr-float x))
491c3062 1774 '(float 2 0)))
136211a9
EZ
1775
1776;;; Compute ln((1+x)/(1-x))
1777(defun math-ln-raw-series (sum n x xsqr)
1778 (math-working "log" sum)
1779 (let* ((nextx (math-mul-float x xsqr))
1780 (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
1781 (if (math-nearly-equal-float sum nextsum)
1782 sum
491c3062 1783 (math-ln-raw-series nextsum (+ n 2) nextx xsqr))))
136211a9
EZ
1784
1785(defun math-ln-plus-1-raw (x)
491c3062 1786 (math-lnp1-series x 2 x (math-neg x)))
136211a9
EZ
1787
1788(defun math-lnp1-series (sum n xpow x)
1789 (math-working "lnp1" sum)
1790 (let* ((nextx (math-mul-float xpow x))
1791 (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
1792 (if (math-nearly-equal-float sum nextsum)
1793 sum
491c3062 1794 (math-lnp1-series nextsum (1+ n) nextx x))))
136211a9 1795
4603f755 1796(defconst math-approx-ln-10
82472f45 1797 (math-read-number-simple "2.302585092994045684018")
94c95a35 1798 "An approximation for ln(10).")
4603f755
JB
1799
1800(math-defcache math-ln-10 math-approx-ln-10
136211a9
EZ
1801 (math-ln-raw-2 '(float 1 1)))
1802
4603f755 1803(defconst math-approx-ln-2
82472f45 1804 (math-read-number-simple "0.693147180559945309417")
94c95a35 1805 "An approximation for ln(2).")
4603f755
JB
1806
1807(math-defcache math-ln-2 math-approx-ln-2
136211a9
EZ
1808 (math-ln-raw-3 (math-float '(frac 1 3))))
1809
1810
1811
1812;;; Hyperbolic functions.
1813
1814(defun calcFunc-sinh (x) ; [N N] [Public]
1815 (cond ((eq x 0) 0)
1816 (math-expand-formulas
1817 (math-normalize
1818 (list '/ (list '- (list 'calcFunc-exp x)
1819 (list 'calcFunc-exp (list 'neg x))) 2)))
1820 ((Math-numberp x)
1821 (if calc-symbolic-mode (signal 'inexact-result nil))
1822 (math-with-extra-prec 2
1823 (let ((expx (math-exp-raw (math-float x))))
1824 (math-mul (math-add expx (math-div -1 expx)) '(float 5 -1)))))
1825 ((eq (car-safe x) 'sdev)
1826 (math-make-sdev (calcFunc-sinh (nth 1 x))
1827 (math-mul (nth 2 x) (calcFunc-cosh (nth 1 x)))))
1828 ((eq (car x) 'intv)
1829 (math-sort-intv (nth 1 x)
1830 (calcFunc-sinh (nth 2 x))
1831 (calcFunc-sinh (nth 3 x))))
1832 ((or (equal x '(var inf var-inf))
1833 (equal x '(neg (var inf var-inf)))
1834 (equal x '(var nan var-nan)))
1835 x)
1836 (t (calc-record-why 'numberp x)
491c3062 1837 (list 'calcFunc-sinh x))))
136211a9
EZ
1838(put 'calcFunc-sinh 'math-expandable t)
1839
1840(defun calcFunc-cosh (x) ; [N N] [Public]
1841 (cond ((eq x 0) 1)
1842 (math-expand-formulas
1843 (math-normalize
1844 (list '/ (list '+ (list 'calcFunc-exp x)
1845 (list 'calcFunc-exp (list 'neg x))) 2)))
1846 ((Math-numberp x)
1847 (if calc-symbolic-mode (signal 'inexact-result nil))
1848 (math-with-extra-prec 2
1849 (let ((expx (math-exp-raw (math-float x))))
1850 (math-mul (math-add expx (math-div 1 expx)) '(float 5 -1)))))
1851 ((eq (car-safe x) 'sdev)
1852 (math-make-sdev (calcFunc-cosh (nth 1 x))
1853 (math-mul (nth 2 x)
1854 (calcFunc-sinh (nth 1 x)))))
1855 ((and (eq (car x) 'intv) (math-intv-constp x))
1856 (setq x (math-abs x))
1857 (math-sort-intv (nth 1 x)
1858 (calcFunc-cosh (nth 2 x))
1859 (calcFunc-cosh (nth 3 x))))
1860 ((or (equal x '(var inf var-inf))
1861 (equal x '(neg (var inf var-inf)))
1862 (equal x '(var nan var-nan)))
1863 (math-abs x))
1864 (t (calc-record-why 'numberp x)
491c3062 1865 (list 'calcFunc-cosh x))))
136211a9
EZ
1866(put 'calcFunc-cosh 'math-expandable t)
1867
1868(defun calcFunc-tanh (x) ; [N N] [Public]
1869 (cond ((eq x 0) 0)
1870 (math-expand-formulas
1871 (math-normalize
1872 (let ((expx (list 'calcFunc-exp x))
1873 (expmx (list 'calcFunc-exp (list 'neg x))))
1874 (math-normalize
1875 (list '/ (list '- expx expmx) (list '+ expx expmx))))))
1876 ((Math-numberp x)
1877 (if calc-symbolic-mode (signal 'inexact-result nil))
1878 (math-with-extra-prec 2
1879 (let* ((expx (calcFunc-exp (math-float x)))
1880 (expmx (math-div 1 expx)))
1881 (math-div (math-sub expx expmx)
1882 (math-add expx expmx)))))
1883 ((eq (car-safe x) 'sdev)
1884 (math-make-sdev (calcFunc-tanh (nth 1 x))
1885 (math-div (nth 2 x)
1886 (math-sqr (calcFunc-cosh (nth 1 x))))))
1887 ((eq (car x) 'intv)
1888 (math-sort-intv (nth 1 x)
1889 (calcFunc-tanh (nth 2 x))
1890 (calcFunc-tanh (nth 3 x))))
1891 ((equal x '(var inf var-inf))
1892 1)
1893 ((equal x '(neg (var inf var-inf)))
1894 -1)
1895 ((equal x '(var nan var-nan))
1896 x)
1897 (t (calc-record-why 'numberp x)
491c3062 1898 (list 'calcFunc-tanh x))))
136211a9
EZ
1899(put 'calcFunc-tanh 'math-expandable t)
1900
f53e6c20
JB
1901(defun calcFunc-sech (x) ; [N N] [Public]
1902 (cond ((eq x 0) 1)
1903 (math-expand-formulas
1904 (math-normalize
1905 (list '/ 2 (list '+ (list 'calcFunc-exp x)
1906 (list 'calcFunc-exp (list 'neg x))))))
1907 ((Math-numberp x)
1908 (if calc-symbolic-mode (signal 'inexact-result nil))
1909 (math-with-extra-prec 2
1910 (let ((expx (math-exp-raw (math-float x))))
1911 (math-div '(float 2 0) (math-add expx (math-div 1 expx))))))
1912 ((eq (car-safe x) 'sdev)
1913 (math-make-sdev (calcFunc-sech (nth 1 x))
1914 (math-mul (nth 2 x)
1915 (math-mul (calcFunc-sech (nth 1 x))
1916 (calcFunc-tanh (nth 1 x))))))
1917 ((and (eq (car x) 'intv) (math-intv-constp x))
1918 (setq x (math-abs x))
1919 (math-sort-intv (nth 1 x)
1920 (calcFunc-sech (nth 2 x))
1921 (calcFunc-sech (nth 3 x))))
1922 ((or (equal x '(var inf var-inf))
1923 (equal x '(neg (var inf var-inf))))
1924 0)
1925 ((equal x '(var nan var-nan))
1926 x)
1927 (t (calc-record-why 'numberp x)
1928 (list 'calcFunc-sech x))))
1929(put 'calcFunc-sech 'math-expandable t)
1930
1931(defun calcFunc-csch (x) ; [N N] [Public]
1932 (cond ((eq x 0) (math-div 1 0))
1933 (math-expand-formulas
1934 (math-normalize
1935 (list '/ 2 (list '- (list 'calcFunc-exp x)
1936 (list 'calcFunc-exp (list 'neg x))))))
1937 ((Math-numberp x)
1938 (if calc-symbolic-mode (signal 'inexact-result nil))
1939 (math-with-extra-prec 2
1940 (let ((expx (math-exp-raw (math-float x))))
1941 (math-div '(float 2 0) (math-add expx (math-div -1 expx))))))
1942 ((eq (car-safe x) 'sdev)
1943 (math-make-sdev (calcFunc-csch (nth 1 x))
1944 (math-mul (nth 2 x)
1945 (math-mul (calcFunc-csch (nth 1 x))
1946 (calcFunc-coth (nth 1 x))))))
1947 ((eq (car x) 'intv)
1948 (if (and (Math-negp (nth 2 x))
1949 (Math-posp (nth 3 x)))
1950 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
1951 (math-sort-intv (nth 1 x)
1952 (calcFunc-csch (nth 2 x))
1953 (calcFunc-csch (nth 3 x)))))
1954 ((or (equal x '(var inf var-inf))
1955 (equal x '(neg (var inf var-inf))))
1956 0)
1957 ((equal x '(var nan var-nan))
1958 x)
1959 (t (calc-record-why 'numberp x)
1960 (list 'calcFunc-csch x))))
1961(put 'calcFunc-csch 'math-expandable t)
1962
1963(defun calcFunc-coth (x) ; [N N] [Public]
1964 (cond ((eq x 0) (math-div 1 0))
1965 (math-expand-formulas
1966 (math-normalize
1967 (let ((expx (list 'calcFunc-exp x))
1968 (expmx (list 'calcFunc-exp (list 'neg x))))
1969 (math-normalize
1970 (list '/ (list '+ expx expmx) (list '- expx expmx))))))
1971 ((Math-numberp x)
1972 (if calc-symbolic-mode (signal 'inexact-result nil))
1973 (math-with-extra-prec 2
1974 (let* ((expx (calcFunc-exp (math-float x)))
1975 (expmx (math-div 1 expx)))
1976 (math-div (math-add expx expmx)
1977 (math-sub expx expmx)))))
1978 ((eq (car-safe x) 'sdev)
1979 (math-make-sdev (calcFunc-coth (nth 1 x))
1980 (math-div (nth 2 x)
1981 (math-sqr (calcFunc-sinh (nth 1 x))))))
1982 ((eq (car x) 'intv)
1983 (if (and (Math-negp (nth 2 x))
1984 (Math-posp (nth 3 x)))
1985 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
1986 (math-sort-intv (nth 1 x)
1987 (calcFunc-coth (nth 2 x))
1988 (calcFunc-coth (nth 3 x)))))
1989 ((equal x '(var inf var-inf))
1990 1)
1991 ((equal x '(neg (var inf var-inf)))
1992 -1)
1993 ((equal x '(var nan var-nan))
1994 x)
1995 (t (calc-record-why 'numberp x)
1996 (list 'calcFunc-coth x))))
1997(put 'calcFunc-coth 'math-expandable t)
1998
136211a9
EZ
1999(defun calcFunc-arcsinh (x) ; [N N] [Public]
2000 (cond ((eq x 0) 0)
2001 (math-expand-formulas
2002 (math-normalize
2003 (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
2004 (list '+ (list '^ x 2) 1))))))
2005 ((Math-numberp x)
2006 (if calc-symbolic-mode (signal 'inexact-result nil))
2007 (math-with-extra-prec 2
2008 (math-ln-raw (math-add x (math-sqrt-raw (math-add (math-sqr x)
2009 '(float 1 0)))))))
2010 ((eq (car-safe x) 'sdev)
2011 (math-make-sdev (calcFunc-arcsinh (nth 1 x))
2012 (math-div (nth 2 x)
2013 (math-sqrt
2014 (math-add (math-sqr (nth 1 x)) 1)))))
2015 ((eq (car x) 'intv)
2016 (math-sort-intv (nth 1 x)
2017 (calcFunc-arcsinh (nth 2 x))
2018 (calcFunc-arcsinh (nth 3 x))))
2019 ((or (equal x '(var inf var-inf))
2020 (equal x '(neg (var inf var-inf)))
2021 (equal x '(var nan var-nan)))
2022 x)
2023 (t (calc-record-why 'numberp x)
491c3062 2024 (list 'calcFunc-arcsinh x))))
136211a9
EZ
2025(put 'calcFunc-arcsinh 'math-expandable t)
2026
2027(defun calcFunc-arccosh (x) ; [N N] [Public]
2028 (cond ((eq x 1) 0)
2029 ((and (eq x -1) calc-symbolic-mode)
2030 '(var pi var-pi))
2031 ((and (eq x 0) calc-symbolic-mode)
2032 (math-div (math-mul '(var pi var-pi) '(var i var-i)) 2))
2033 (math-expand-formulas
2034 (math-normalize
2035 (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
2036 (list '- (list '^ x 2) 1))))))
2037 ((Math-numberp x)
2038 (if calc-symbolic-mode (signal 'inexact-result nil))
2039 (if (Math-equal-int x -1)
2040 (math-imaginary (math-pi))
2041 (math-with-extra-prec 2
2042 (if (or t ; need to do this even in the real case!
2043 (memq (car-safe x) '(cplx polar)))
2044 (let ((xp1 (math-add 1 x))) ; this gets the branch cuts right
2045 (math-ln-raw
2046 (math-add x (math-mul xp1
2047 (math-sqrt-raw
2048 (math-div (math-sub
2049 x
2050 '(float 1 0))
2051 xp1))))))
2052 (math-ln-raw
2053 (math-add x (math-sqrt-raw (math-add (math-sqr x)
2054 '(float -1 0)))))))))
2055 ((eq (car-safe x) 'sdev)
2056 (math-make-sdev (calcFunc-arccosh (nth 1 x))
2057 (math-div (nth 2 x)
2058 (math-sqrt
2059 (math-add (math-sqr (nth 1 x)) -1)))))
2060 ((eq (car x) 'intv)
2061 (math-sort-intv (nth 1 x)
2062 (calcFunc-arccosh (nth 2 x))
2063 (calcFunc-arccosh (nth 3 x))))
2064 ((or (equal x '(var inf var-inf))
2065 (equal x '(neg (var inf var-inf)))
2066 (equal x '(var nan var-nan)))
2067 x)
2068 (t (calc-record-why 'numberp x)
491c3062 2069 (list 'calcFunc-arccosh x))))
136211a9
EZ
2070(put 'calcFunc-arccosh 'math-expandable t)
2071
2072(defun calcFunc-arctanh (x) ; [N N] [Public]
2073 (cond ((eq x 0) 0)
2074 ((and (Math-equal-int x 1) calc-infinite-mode)
2075 '(var inf var-inf))
2076 ((and (Math-equal-int x -1) calc-infinite-mode)
2077 '(neg (var inf var-inf)))
2078 (math-expand-formulas
2079 (list '/ (list '-
2080 (list 'calcFunc-ln (list '+ 1 x))
2081 (list 'calcFunc-ln (list '- 1 x))) 2))
2082 ((Math-numberp x)
2083 (if calc-symbolic-mode (signal 'inexact-result nil))
2084 (math-with-extra-prec 2
2085 (if (or (memq (car-safe x) '(cplx polar))
2086 (Math-lessp 1 x))
2087 (math-mul (math-sub (math-ln-raw (math-add '(float 1 0) x))
2088 (math-ln-raw (math-sub '(float 1 0) x)))
2089 '(float 5 -1))
2090 (if (and (math-equal-int x 1) calc-infinite-mode)
2091 '(var inf var-inf)
2092 (if (and (math-equal-int x -1) calc-infinite-mode)
2093 '(neg (var inf var-inf))
2094 (math-mul (math-ln-raw (math-div (math-add '(float 1 0) x)
2095 (math-sub 1 x)))
2096 '(float 5 -1)))))))
2097 ((eq (car-safe x) 'sdev)
2098 (math-make-sdev (calcFunc-arctanh (nth 1 x))
2099 (math-div (nth 2 x)
2100 (math-sub 1 (math-sqr (nth 1 x))))))
2101 ((eq (car x) 'intv)
2102 (math-sort-intv (nth 1 x)
2103 (calcFunc-arctanh (nth 2 x))
2104 (calcFunc-arctanh (nth 3 x))))
2105 ((equal x '(var nan var-nan))
2106 x)
2107 (t (calc-record-why 'numberp x)
491c3062 2108 (list 'calcFunc-arctanh x))))
136211a9
EZ
2109(put 'calcFunc-arctanh 'math-expandable t)
2110
2111
2112;;; Convert A from HMS or degrees to radians.
2113(defun calcFunc-rad (a) ; [R R] [Public]
2114 (cond ((or (Math-numberp a)
2115 (eq (car a) 'intv))
2116 (math-with-extra-prec 2
2117 (math-mul a (math-pi-over-180))))
2118 ((eq (car a) 'hms)
2119 (math-from-hms a 'rad))
2120 ((eq (car a) 'sdev)
2121 (math-make-sdev (calcFunc-rad (nth 1 a))
2122 (calcFunc-rad (nth 2 a))))
2123 (math-expand-formulas
2124 (math-div (math-mul a '(var pi var-pi)) 180))
2125 ((math-infinitep a) a)
491c3062 2126 (t (list 'calcFunc-rad a))))
136211a9
EZ
2127(put 'calcFunc-rad 'math-expandable t)
2128
2129;;; Convert A from HMS or radians to degrees.
2130(defun calcFunc-deg (a) ; [R R] [Public]
2131 (cond ((or (Math-numberp a)
2132 (eq (car a) 'intv))
2133 (math-with-extra-prec 2
2134 (math-div a (math-pi-over-180))))
2135 ((eq (car a) 'hms)
2136 (math-from-hms a 'deg))
2137 ((eq (car a) 'sdev)
2138 (math-make-sdev (calcFunc-deg (nth 1 a))
2139 (calcFunc-deg (nth 2 a))))
2140 (math-expand-formulas
2141 (math-div (math-mul 180 a) '(var pi var-pi)))
2142 ((math-infinitep a) a)
491c3062 2143 (t (list 'calcFunc-deg a))))
136211a9
EZ
2144(put 'calcFunc-deg 'math-expandable t)
2145
95995a85
JB
2146(provide 'calc-math)
2147
ab5796a9 2148;;; arch-tag: c7367e8e-d0b8-4f70-8577-2fb3f31dbb4c
491c3062 2149;;; calc-math.el ends here