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/>.
26 ;; This file is autoloaded from calc-ext.el.
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
38 (xx (+ x
(* 9 (expt 10 (- n
))))))
43 (setq xx
(+ x
(* 9 (expt 10 (- 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
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).
61 (setq pow
(condition-case nil
64 ;; The following loop should stop when 10^(x+1) is too large.
65 (setq pow
(condition-case nil
72 (setq pow
(condition-case nil
76 "The largest exponent which Calc will convert to an Emacs float.")
78 (defvar math-smallest-emacs-expt
80 (while (condition-case nil
85 (while (condition-case nil
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."
96 (<= calc-internal-prec math-emacs-precision
)
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
)
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
)))))))
116 (defun calc-sqrt (arg)
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)
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)
134 (calc-binary-op "hypt" 'calcFunc-hypot arg
)))
141 (defun calc-log10 (arg)
143 (calc-hyperbolic-func)
146 (defun calc-log (arg)
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)
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)
165 (defun calc-exp (arg)
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)
179 (if (calc-is-inverse)
180 (calc-unary-op "ln+1" 'calcFunc-lnp1 arg
)
181 (calc-unary-op "ex-1" 'calcFunc-expm1 arg
))))
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)
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)
218 (defun calc-sinh (arg)
220 (calc-hyperbolic-func)
223 (defun calc-arcsinh (arg)
226 (calc-hyperbolic-func)
229 (defun calc-sec (arg)
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)
238 (calc-hyperbolic-func)
241 (defun calc-cos (arg)
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)
257 (defun calc-cosh (arg)
259 (calc-hyperbolic-func)
262 (defun calc-arccosh (arg)
265 (calc-hyperbolic-func)
268 (defun calc-csc (arg)
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)
277 (calc-hyperbolic-func)
280 (defun calc-sincos ()
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)
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)
303 (defun calc-tanh (arg)
305 (calc-hyperbolic-func)
308 (defun calc-arctanh (arg)
311 (calc-hyperbolic-func)
314 (defun calc-cot (arg)
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)
323 (calc-hyperbolic-func)
326 (defun calc-arctan2 ()
329 (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2
(calc-top-list-n 2)))))
331 (defun calc-conj (arg)
334 (calc-unary-op "conj" 'calcFunc-conj arg
)))
336 (defun calc-imaginary ()
339 (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1)))))
341 (defun calc-to-degrees (arg)
344 (calc-unary-op ">deg" 'calcFunc-deg arg
)))
346 (defun calc-to-radians (arg)
349 (calc-unary-op ">rad" 'calcFunc-rad arg
)))
352 (defun calc-degrees-mode (arg)
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 ()
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
))
377 (math-isqrt-small a
))
379 (math-normalize (cons 'bigpos
(cdr (math-isqrt-bignum (cdr a
))))))))
381 (defun calcFunc-isqrt (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
)))
391 (let* ((top (nthcdr (- len
2) a
)))
392 (math-isqrt-bignum-iter
394 (math-scale-bignum-digit-size
396 (1+ (math-isqrt-small
397 (+ (* (nth 1 top
) math-bignum-digit-size
) (car top
)))))
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
)))
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
)))
413 (math-isqrt-bignum-iter a g2
)
414 (cons (and (= comp
0)
415 (math-zerop-bignum (cdr q
))
419 (defun math-zerop-bignum (a)
422 (while (eq (car (setq a
(cdr a
))) 0))
425 (defun math-scale-bignum-digit-size (a n
) ; [L L S]
431 (defun math-isqrt-small (a) ; A > 0. [S S]
432 (let ((g (cond ((>= a
1000000) 10000)
437 (while (< (setq g2
(/ (+ g
(/ a g
)) 2)) g
)
444 ;;; Compute the square root of a number.
445 ;;; [T N] if possible, else [F N] if possible, else [C N]. [Public]
448 (and (Math-zerop a
) a
)
449 (and (math-known-nonposp a
)
450 (math-imaginary (math-sqrt (math-neg a
))))
452 (let ((sqrt (math-isqrt-small a
)))
453 (if (= (* sqrt sqrt
) a
)
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
)))))
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
)))
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
))))))
488 (math-make-float (math-normalize
489 (cons 'bigpos
(cdr res
)))
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
))
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
)
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
))))
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
)))
530 (math-mul (math-sqrt (math-infinite-dir a inf
)) inf
)))
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
))
549 ((math-use-emacs-fn 'sqrt a
))
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
))
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
)))
573 (and (not (consp ediff
))
576 (= (math-numdigs (nth 1 a
)) calc-internal-prec
))))
578 (setq ediff
(math-add (Math-integer-neg (nth 1 b
))
579 (math-scale-int (nth 1 a
) 1)))
580 (and (not (consp ediff
))
583 (= (math-numdigs (nth 1 b
)) calc-internal-prec
)))
585 (setq ediff
(math-add (Math-integer-neg (nth 1 a
))
586 (math-scale-int (nth 1 b
) 1)))
587 (and (not (consp ediff
))
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
)
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
)
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
))))
657 (defalias 'calcFunc-hypot
'math-hypot
)
659 (defun calcFunc-sqr (x)
664 (defun math-nth-root (a n
)
665 (cond ((= n
2) (math-sqrt a
))
669 (let ((root (math-nth-root-integer a n
)))
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
)
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
)))))
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.
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
707 1 (/ (+ (math-numdigs (nth 1 a
))
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
)
716 a
(math-ipow guess
(1- math-nrf-n
))))
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.
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
)
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
)
745 (defun calcFunc-nroot (x n
)
746 (calcFunc-pow x
(if (integerp 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)))
771 (defun math-from-radians (a) ; [N N]
772 (cond ((and (not math-simplifying-units
)
773 (eq calc-angle-mode
'deg
))
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
))
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))))
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))))
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
)
808 (aref [0 1 0 -
1] (math-mod (/ x
90) 4)))
810 (math-with-extra-prec 2
811 (math-sin-raw (math-to-radians (math-float 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
)
833 (aref [1 0 -
1 0] (math-mod (/ x
90) 4)))
835 (math-with-extra-prec 2
836 (math-cos-raw (math-to-radians (math-float 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
)))))
858 (math-make-intv (logior (nth 1 x
) 2)
861 (math-make-intv (logior (nth 1 x
) 1)
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]
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
)
885 (math-with-extra-prec 2
886 (math-tan-raw (math-to-radians (math-float 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
))
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
))
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
)
908 (nb (math-floor (math-div (math-sub (nth 3 xx
)
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
)
929 (eq calc-angle-mode
'rad
)
933 (math-with-extra-prec 2
934 (math-sec-raw (math-to-radians (math-float 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
))
944 (calc-record-why "*Division by zero")
945 (list 'calcFunc-sec x
))
946 (math-make-sdev (math-div-float '(float 1 0) (cdr sc
))
948 (math-mul xs
(car sc
))
949 (math-sqr (cdr sc
)))))))
950 (math-make-sdev (calcFunc-sec (nth 1 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
)
962 (nb (math-floor (math-div (math-sub (nth 3 xx
)
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
)))))
974 (if (math-evenp (math-div (math-add naa
1) 2))
975 (math-make-intv (logior (nth 1 int
) 2)
978 (math-make-intv (logior (nth 1 int
) 1)
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)
995 (math-with-extra-prec 2
996 (math-csc-raw (math-to-radians (math-float 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
))
1006 (calc-record-why "*Division by zero")
1007 (list 'calcFunc-csc x
))
1008 (math-make-sdev (math-div-float '(float 1 0) (car sc
))
1010 (math-mul xs
(cdr sc
))
1011 (math-sqr (car sc
)))))))
1012 (math-make-sdev (calcFunc-csc (nth 1 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
)))))
1032 (if (math-evenp (math-div naa
2))
1033 (math-make-intv (logior (nth 1 int
) 2)
1036 (math-make-intv (logior (nth 1 int
) 1)
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)
1052 (math-with-extra-prec 2
1053 (math-cot-raw (math-to-radians (math-float 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
))
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
))
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)))))
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
))))
1090 (math-mul-float (car sc
)
1091 (math-mul-float (math-add-float expx expmx
)
1093 (math-mul-float (cdr sc
)
1094 (math-mul-float (math-sub-float expx expmx
)
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
)))
1118 (math-mul-float (math-sqr (car sc
))
1120 (math-mul-float (math-sqr (cdr sc
))
1122 (and (not (eq (nth 1 d
) 0))
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
)))
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
)))
1143 (math-mul-float (math-sqr (car sc
))
1145 (math-mul-float (math-sqr (cdr sc
))
1147 (and (not (eq (nth 1 d
) 0))
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
)))
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
)))
1170 (and (not (eq (nth 1 d
) 0))
1173 (math-mul-float (car sc
) (cdr sc
))
1177 (math-mul-float sh ch
)
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
)
1203 (and (not (eq (nth 1 d
) 0))
1205 (math-div-float (car sc
) d
)
1206 (math-div-float (math-mul-float (math-sub-float expx
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))
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
))
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
))
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
))))
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]
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
))
1262 (math-with-extra-prec 2
1263 (math-from-radians (math-arcsin-raw (math-float x
)))))
1265 (math-make-sdev (calcFunc-arcsin (nth 1 x
))
1269 (math-sub 1 (math-sqr (nth 1 x
))))))))
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]
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
))
1285 (math-with-extra-prec 2
1286 (math-from-radians (math-arccos-raw (math-float x
)))))
1288 (math-make-sdev (calcFunc-arccos (nth 1 x
))
1292 (math-sub 1 (math-sqr (nth 1 x
))))))))
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]
1304 ((and (eq x
1) (eq calc-angle-mode
'deg
)) 45)
1305 ((and (eq x -
1) (eq calc-angle-mode
'deg
)) -
45)
1307 (math-with-extra-prec 2
1308 (math-from-radians (math-arctan-raw (math-float x
)))))
1310 (math-make-sdev (calcFunc-arctan (nth 1 x
))
1313 (math-add 1 (math-sqr (nth 1 x
)))))))
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
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
))))
1346 ((Math-integer-negp (nth 1 x
))
1347 (math-neg-float (math-arctan-raw (math-neg-float 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)
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]
1375 (math-with-extra-prec 2
1376 (math-from-radians (math-arctan2-raw (math-float y
)
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
)))
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)))
1408 (math-neg (math-pi-over-2))))
1410 (math-arctan-raw (math-div-float y x
)))
1412 (math-add-float (math-arctan-raw (math-div-float y x
))
1415 (math-sub-float (math-arctan-raw (math-div-float y x
))
1418 (defun calcFunc-arcsincos (x) ; [V N] [Public]
1419 (if (and (Math-vectorp x
)
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]
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
))))
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]
1451 ((math-zerop x
) '(float 0 0))
1452 (calc-symbolic-mode (signal 'inexact-result nil
))
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
)
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]
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
))
1489 (let ((expx (math-exp-raw (nth 1 x
)))
1490 (sc (math-sin-cos-raw (nth 2 x
))))
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
)))
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
)
1504 (if (math-lessp-float x
'(float -
921035 1))
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
)
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
))))
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")))
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
)))
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
)
1566 (let ((res (math-integer-log x
10)))
1568 (setq x
(cdr res
)))))
1570 ((and (eq (car-safe x
) 'frac
)
1572 (let ((res (math-integer-log (nth 2 x
) 10)))
1574 (setq x
(- (cdr res
))))))
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
))
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
)))))
1591 (math-div-float (math-ln-raw-2
1592 (list 'float
(nth 1 xf
) (- xdigs
)))
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
))
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
))))
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
)))
1618 (if calc-infinite-mode
1619 (math-div (calcFunc-ln x
) (calcFunc-ln b
))
1620 (math-reject-arg x
"*Logarithm of zero")))
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
)
1638 (bb (if (Math-lessp 1 b
)
1640 (setq sign
(- sign
))
1642 (res (if (Math-lessp xx bb
)
1643 (setq inv
(math-integer-log bb xx
))
1644 (math-integer-log xx bb
))))
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
)
1657 (math-make-sdev (calcFunc-log (nth 1 x
) b
)
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
)))
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
)
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)))
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
))
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
)))
1703 (while (and (setq pows
(cdr pows
))
1706 next
(math-mul pow
(car pows
)))
1707 (or (Math-lessp x next
)
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")))
1727 ((math-zerop x
) '(float 0 0))
1728 (calc-symbolic-mode (signal 'inexact-result nil
))
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
))))
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
)
1755 (math-mul-float (math-ln-raw
1756 (math-add-float (math-sqr-float (nth 1 x
))
1757 (math-sqr-float (nth 2 x
))))
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)
1766 (calc-symbolic-mode (signal 'inexact-result nil
))
1767 ((math-posp (nth 1 x
)) ; positive and real
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
))
1776 (math-reject-arg x
"*Logarithm of zero"))
1777 ((eq calc-complex-mode
'polar
) ; negative and real
1779 (list 'cplx
; negative and real
1780 (math-ln-raw (math-neg-float x
))
1782 (t (list 'cplx
; negative and real
1783 (math-ln-raw (math-neg-float x
))
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)))
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
))
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]
1838 (math-expand-formulas
1840 (list '/ (list '-
(list 'calcFunc-exp x
)
1841 (list 'calcFunc-exp
(list 'neg x
))) 2)))
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
)))))
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]
1864 (math-expand-formulas
1866 (list '/ (list '+ (list 'calcFunc-exp x
)
1867 (list 'calcFunc-exp
(list 'neg x
))) 2)))
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
))
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
)))
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]
1892 (math-expand-formulas
1894 (let ((expx (list 'calcFunc-exp x
))
1895 (expmx (list 'calcFunc-exp
(list 'neg x
))))
1897 (list '/ (list '- expx expmx
) (list '+ expx expmx
))))))
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
))
1908 (math-sqr (calcFunc-cosh (nth 1 x
))))))
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]
1925 (math-expand-formulas
1927 (list '/ 2 (list '+ (list 'calcFunc-exp x
)
1928 (list 'calcFunc-exp
(list 'neg 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
))
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
1957 (list '/ 2 (list '-
(list 'calcFunc-exp x
)
1958 (list 'calcFunc-exp
(list 'neg 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
))
1967 (math-mul (calcFunc-csch (nth 1 x
))
1968 (calcFunc-coth (nth 1 x
))))))
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
1989 (let ((expx (list 'calcFunc-exp x
))
1990 (expmx (list 'calcFunc-exp
(list 'neg x
))))
1992 (list '/ (list '+ expx expmx
) (list '- expx expmx
))))))
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
))
2003 (math-sqr (calcFunc-sinh (nth 1 x
))))))
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]
2023 (math-expand-formulas
2025 (list 'calcFunc-ln
(list '+ x
(list 'calcFunc-sqrt
2026 (list '+ (list '^ x
2) 1))))))
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
)
2032 ((eq (car-safe x
) 'sdev
)
2033 (math-make-sdev (calcFunc-arcsinh (nth 1 x
))
2036 (math-add (math-sqr (nth 1 x
)) 1)))))
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]
2051 ((and (eq x -
1) calc-symbolic-mode
)
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
2057 (list 'calcFunc-ln
(list '+ x
(list 'calcFunc-sqrt
2058 (list '-
(list '^ x
2) 1))))))
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
2068 (math-add x
(math-mul xp1
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
))
2081 (math-add (math-sqr (nth 1 x
)) -
1)))))
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]
2096 ((and (Math-equal-int x
1) calc-infinite-mode
)
2098 ((and (Math-equal-int x -
1) calc-infinite-mode
)
2099 '(neg (var inf var-inf
)))
2100 (math-expand-formulas
2102 (list 'calcFunc-ln
(list '+ 1 x
))
2103 (list 'calcFunc-ln
(list '-
1 x
))) 2))
2105 (if calc-symbolic-mode
(signal 'inexact-result nil
))
2106 (math-with-extra-prec 2
2107 (if (or (memq (car-safe x
) '(cplx polar
))
2109 (math-mul (math-sub (math-ln-raw (math-add '(float 1 0) x
))
2110 (math-ln-raw (math-sub '(float 1 0) x
)))
2112 (if (and (math-equal-int x
1) calc-infinite-mode
)
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
)
2119 ((eq (car-safe x
) 'sdev
)
2120 (math-make-sdev (calcFunc-arctanh (nth 1 x
))
2122 (math-sub 1 (math-sqr (nth 1 x
))))))
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
)
2138 (math-with-extra-prec 2
2139 (math-mul a
(math-pi-over-180))))
2141 (math-from-hms a
'rad
))
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
)
2155 (math-with-extra-prec 2
2156 (math-div a
(math-pi-over-180))))
2158 (math-from-hms a
'deg
))
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