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