]> code.delx.au - gnu-emacs/blob - lisp/calc/calc-math.el
Update copyright year to 2016
[gnu-emacs] / lisp / calc / calc-math.el
1 ;;; calc-math.el --- mathematical functions for Calc
2
3 ;; Copyright (C) 1990-1993, 2001-2016 Free Software Foundation, Inc.
4
5 ;; Author: David Gillespie <daveg@synaptics.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23
24 ;;; Code:
25
26 ;; This file is autoloaded from calc-ext.el.
27
28 (require 'calc-ext)
29 (require 'calc-macs)
30
31
32 ;;; Find out how many 9s in 9.9999... will give distinct Emacs floats,
33 ;;; then back off by one.
34
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.")
46
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....
50
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.")
77
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.")
91
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))))))
115
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))))
122
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))))
129
130
131 (defun calc-hypot (arg)
132 (interactive "P")
133 (calc-slow-wrapper
134 (calc-binary-op "hypt" 'calcFunc-hypot arg)))
135
136 (defun calc-ln (arg)
137 (interactive "P")
138 (calc-invert-func)
139 (calc-exp arg))
140
141 (defun calc-log10 (arg)
142 (interactive "P")
143 (calc-hyperbolic-func)
144 (calc-ln arg))
145
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))))
152
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))))
159
160 (defun calc-lnp1 (arg)
161 (interactive "P")
162 (calc-invert-func)
163 (calc-expm1 arg))
164
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)))))
175
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))))
182
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)))))))
201
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)))))
212
213 (defun calc-arcsin (arg)
214 (interactive "P")
215 (calc-invert-func)
216 (calc-sin arg))
217
218 (defun calc-sinh (arg)
219 (interactive "P")
220 (calc-hyperbolic-func)
221 (calc-sin arg))
222
223 (defun calc-arcsinh (arg)
224 (interactive "P")
225 (calc-invert-func)
226 (calc-hyperbolic-func)
227 (calc-sin arg))
228
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))))
235
236 (defun calc-sech (arg)
237 (interactive "P")
238 (calc-hyperbolic-func)
239 (calc-sec arg))
240
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)))))
251
252 (defun calc-arccos (arg)
253 (interactive "P")
254 (calc-invert-func)
255 (calc-cos arg))
256
257 (defun calc-cosh (arg)
258 (interactive "P")
259 (calc-hyperbolic-func)
260 (calc-cos arg))
261
262 (defun calc-arccosh (arg)
263 (interactive "P")
264 (calc-invert-func)
265 (calc-hyperbolic-func)
266 (calc-cos arg))
267
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))))
274
275 (defun calc-csch (arg)
276 (interactive "P")
277 (calc-hyperbolic-func)
278 (calc-csc arg))
279
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))))))
286
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)))))
297
298 (defun calc-arctan (arg)
299 (interactive "P")
300 (calc-invert-func)
301 (calc-tan arg))
302
303 (defun calc-tanh (arg)
304 (interactive "P")
305 (calc-hyperbolic-func)
306 (calc-tan arg))
307
308 (defun calc-arctanh (arg)
309 (interactive "P")
310 (calc-invert-func)
311 (calc-hyperbolic-func)
312 (calc-tan arg))
313
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))))
320
321 (defun calc-coth (arg)
322 (interactive "P")
323 (calc-hyperbolic-func)
324 (calc-cot arg))
325
326 (defun calc-arctan2 ()
327 (interactive)
328 (calc-slow-wrapper
329 (calc-enter-result 2 "atn2" (cons 'calcFunc-arctan2 (calc-top-list-n 2)))))
330
331 (defun calc-conj (arg)
332 (interactive "P")
333 (calc-wrapper
334 (calc-unary-op "conj" 'calcFunc-conj arg)))
335
336 (defun calc-imaginary ()
337 (interactive)
338 (calc-slow-wrapper
339 (calc-pop-push-record 1 "i*" (math-imaginary (calc-top-n 1)))))
340
341 (defun calc-to-degrees (arg)
342 (interactive "P")
343 (calc-wrapper
344 (calc-unary-op ">deg" 'calcFunc-deg arg)))
345
346 (defun calc-to-radians (arg)
347 (interactive "P")
348 (calc-wrapper
349 (calc-unary-op ">rad" 'calcFunc-rad arg)))
350
351
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"))))
361
362 (defun calc-radians-mode ()
363 (interactive)
364 (calc-wrapper
365 (calc-change-mode 'calc-angle-mode 'rad)
366 (message "Angles measured in radians")))
367
368
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))
378 (t
379 (math-normalize (cons 'bigpos (cdr (math-isqrt-bignum (cdr a))))))))
380
381 (defun calcFunc-isqrt (a)
382 (if (math-realp a)
383 (math-isqrt (math-floor a))
384 (math-floor (math-sqrt a))))
385
386
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
393 a
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
401 a
402 (math-scale-bignum-digit-size
403 (list (1+ (math-isqrt-small top)))
404 (/ len 2)))))))
405
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))))
418
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))))
424
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)))
429 a)
430
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)))
436 g2)
437 (while (< (setq g2 (/ (+ g (/ a g)) 2)) g)
438 (setq g g2))
439 g))
440
441
442
443
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)
447 (or
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)
535
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)))
539
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))))
544
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))
550 (t
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)))))
558
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)
564 g2
565 (math-sqrt-float-iter a g2))))
566
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))))))
591
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))))
611
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)))))
617
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))))
627
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)
636 a
637 '(var nan var-nan))
638 a)
639 (calc-record-why 'scalarp a)
640 (list 'calcFunc-hypot a b)))
641 ((not (Math-scalarp b))
642 (if (math-infinitep b)
643 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)
658
659 (defun calcFunc-sqr (x)
660 (math-pow x 2))
661
662
663
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)))
692
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)
699
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)))))))
711
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)
719 g2
720 (math-nth-root-float-iter a g2))))
721
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)
726
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)))))
732
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))))
744
745 (defun calcFunc-nroot (x n)
746 (calcFunc-pow x (if (integerp n)
747 (math-make-frac 1 n)
748 (math-div 1 n))))
749
750
751
752
753 ;;;; Transcendental functions.
754
755 ;;; All of these functions are defined on the complex plane.
756 ;;; (Branch cuts, etc. follow Steele's Common Lisp book.)
757
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).
762
763 (defun math-to-radians (a) ; [N N]
764 (cond ((eq (car-safe a) 'hms)
765 (math-from-hms a 'rad))
766 ((memq calc-angle-mode '(deg hms))
767 (math-mul a (math-pi-over-180)))
768 (t a)))
769
770 (defun math-from-radians (a) ; [N N]
771 (cond ((eq calc-angle-mode 'deg)
772 (if (math-constp a)
773 (math-div a (math-pi-over-180))
774 (list 'calcFunc-deg a)))
775 ((eq calc-angle-mode 'hms)
776 (math-to-hms a 'rad))
777 (t a)))
778
779 (defun math-to-radians-2 (a &optional force-symbolic) ; [N N]
780 (cond ((eq (car-safe a) 'hms)
781 (math-from-hms a 'rad))
782 ((memq calc-angle-mode '(deg hms))
783 (if (or calc-symbolic-mode force-symbolic)
784 (math-div (math-mul a '(var pi var-pi)) 180)
785 (math-mul a (math-pi-over-180))))
786 (t a)))
787
788 (defun math-from-radians-2 (a &optional force-symbolic) ; [N N]
789 (cond ((memq calc-angle-mode '(deg hms))
790 (if (or calc-symbolic-mode force-symbolic)
791 (math-div (math-mul 180 a) '(var pi var-pi))
792 (math-div a (math-pi-over-180))))
793 (t a)))
794
795
796
797 ;;; Sine, cosine, and tangent.
798
799 (defun calcFunc-sin (x) ; [N N] [Public]
800 (cond ((and (integerp x)
801 (if (eq calc-angle-mode 'deg)
802 (= (% x 90) 0)
803 (= x 0)))
804 (aref [0 1 0 -1] (math-mod (/ x 90) 4)))
805 ((Math-scalarp x)
806 (math-with-extra-prec 2
807 (math-sin-raw (math-to-radians (math-float x)))))
808 ((eq (car x) 'sdev)
809 (if (math-constp x)
810 (math-with-extra-prec 2
811 (let* ((xx (math-to-radians (math-float (nth 1 x))))
812 (xs (math-to-radians (math-float (nth 2 x))))
813 (sc (math-sin-cos-raw xx)))
814 (math-make-sdev (car sc) (math-mul xs (cdr sc)))))
815 (math-make-sdev (calcFunc-sin (nth 1 x))
816 (math-mul (nth 2 x) (calcFunc-cos (nth 1 x))))))
817 ((and (eq (car x) 'intv) (math-intv-constp x))
818 (calcFunc-cos (math-sub x (math-quarter-circle nil))))
819 ((equal x '(var nan var-nan))
820 x)
821 (t (calc-record-why 'scalarp x)
822 (list 'calcFunc-sin x))))
823
824 (defun calcFunc-cos (x) ; [N N] [Public]
825 (cond ((and (integerp x)
826 (if (eq calc-angle-mode 'deg)
827 (= (% x 90) 0)
828 (= x 0)))
829 (aref [1 0 -1 0] (math-mod (/ x 90) 4)))
830 ((Math-scalarp x)
831 (math-with-extra-prec 2
832 (math-cos-raw (math-to-radians (math-float x)))))
833 ((eq (car x) 'sdev)
834 (if (math-constp x)
835 (math-with-extra-prec 2
836 (let* ((xx (math-to-radians (math-float (nth 1 x))))
837 (xs (math-to-radians (math-float (nth 2 x))))
838 (sc (math-sin-cos-raw xx)))
839 (math-make-sdev (cdr sc) (math-mul xs (car sc)))))
840 (math-make-sdev (calcFunc-cos (nth 1 x))
841 (math-mul (nth 2 x) (calcFunc-sin (nth 1 x))))))
842 ((and (eq (car x) 'intv) (math-intv-constp x))
843 (math-with-extra-prec 2
844 (let* ((xx (math-to-radians (math-float x)))
845 (na (math-floor (math-div (nth 2 xx) (math-pi))))
846 (nb (math-floor (math-div (nth 3 xx) (math-pi))))
847 (span (math-sub nb na)))
848 (if (memq span '(0 1))
849 (let ((int (math-sort-intv (nth 1 x)
850 (math-cos-raw (nth 2 xx))
851 (math-cos-raw (nth 3 xx)))))
852 (if (eq span 1)
853 (if (math-evenp na)
854 (math-make-intv (logior (nth 1 x) 2)
855 -1
856 (nth 3 int))
857 (math-make-intv (logior (nth 1 x) 1)
858 (nth 2 int)
859 1))
860 int))
861 (list 'intv 3 -1 1)))))
862 ((equal x '(var nan var-nan))
863 x)
864 (t (calc-record-why 'scalarp x)
865 (list 'calcFunc-cos x))))
866
867 (defun calcFunc-sincos (x) ; [V N] [Public]
868 (if (Math-scalarp x)
869 (math-with-extra-prec 2
870 (let ((sc (math-sin-cos-raw (math-to-radians (math-float x)))))
871 (list 'vec (cdr sc) (car sc)))) ; the vector [cos, sin]
872 (list 'vec (calcFunc-sin x) (calcFunc-cos x))))
873
874 (defun calcFunc-tan (x) ; [N N] [Public]
875 (cond ((and (integerp x)
876 (if (eq calc-angle-mode 'deg)
877 (= (% x 180) 0)
878 (= x 0)))
879 0)
880 ((Math-scalarp x)
881 (math-with-extra-prec 2
882 (math-tan-raw (math-to-radians (math-float x)))))
883 ((eq (car x) 'sdev)
884 (if (math-constp x)
885 (math-with-extra-prec 2
886 (let* ((xx (math-to-radians (math-float (nth 1 x))))
887 (xs (math-to-radians (math-float (nth 2 x))))
888 (sc (math-sin-cos-raw xx)))
889 (if (and (math-zerop (cdr sc)) (not calc-infinite-mode))
890 (progn
891 (calc-record-why "*Division by zero")
892 (list 'calcFunc-tan x))
893 (math-make-sdev (math-div-float (car sc) (cdr sc))
894 (math-div-float xs (math-sqr (cdr sc)))))))
895 (math-make-sdev (calcFunc-tan (nth 1 x))
896 (math-div (nth 2 x)
897 (math-sqr (calcFunc-cos (nth 1 x)))))))
898 ((and (eq (car x) 'intv) (math-intv-constp x))
899 (or (math-with-extra-prec 2
900 (let* ((xx (math-to-radians (math-float x)))
901 (na (math-floor (math-div (math-sub (nth 2 xx)
902 (math-pi-over-2))
903 (math-pi))))
904 (nb (math-floor (math-div (math-sub (nth 3 xx)
905 (math-pi-over-2))
906 (math-pi)))))
907 (and (equal na nb)
908 (math-sort-intv (nth 1 x)
909 (math-tan-raw (nth 2 xx))
910 (math-tan-raw (nth 3 xx))))))
911 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
912 ((equal x '(var nan var-nan))
913 x)
914 (t (calc-record-why 'scalarp x)
915 (list 'calcFunc-tan x))))
916
917 (defun calcFunc-sec (x)
918 (cond ((and (integerp x)
919 (eq calc-angle-mode 'deg)
920 (= (% x 180) 0))
921 (if (= (% x 360) 0)
922 1
923 -1))
924 ((and (integerp x)
925 (eq calc-angle-mode 'rad)
926 (= x 0))
927 1)
928 ((Math-scalarp x)
929 (math-with-extra-prec 2
930 (math-sec-raw (math-to-radians (math-float x)))))
931 ((eq (car x) 'sdev)
932 (if (math-constp x)
933 (math-with-extra-prec 2
934 (let* ((xx (math-to-radians (math-float (nth 1 x))))
935 (xs (math-to-radians (math-float (nth 2 x))))
936 (sc (math-sin-cos-raw xx)))
937 (if (and (math-zerop (cdr sc))
938 (not calc-infinite-mode))
939 (progn
940 (calc-record-why "*Division by zero")
941 (list 'calcFunc-sec x))
942 (math-make-sdev (math-div-float '(float 1 0) (cdr sc))
943 (math-div-float
944 (math-mul xs (car sc))
945 (math-sqr (cdr sc)))))))
946 (math-make-sdev (calcFunc-sec (nth 1 x))
947 (math-div
948 (math-mul (nth 2 x)
949 (calcFunc-sin (nth 1 x)))
950 (math-sqr (calcFunc-cos (nth 1 x)))))))
951 ((and (eq (car x) 'intv)
952 (math-intv-constp x))
953 (math-with-extra-prec 2
954 (let* ((xx (math-to-radians (math-float x)))
955 (na (math-floor (math-div (math-sub (nth 2 xx)
956 (math-pi-over-2))
957 (math-pi))))
958 (nb (math-floor (math-div (math-sub (nth 3 xx)
959 (math-pi-over-2))
960 (math-pi))))
961 (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
962 (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
963 (span (math-sub nbb naa)))
964 (if (not (equal na nb))
965 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
966 (let ((int (math-sort-intv (nth 1 x)
967 (math-sec-raw (nth 2 xx))
968 (math-sec-raw (nth 3 xx)))))
969 (if (eq span 1)
970 (if (math-evenp (math-div (math-add naa 1) 2))
971 (math-make-intv (logior (nth 1 int) 2)
972 1
973 (nth 3 int))
974 (math-make-intv (logior (nth 1 int) 1)
975 (nth 2 int)
976 -1))
977 int))))))
978 ((equal x '(var nan var-nan))
979 x)
980 (t (calc-record-why 'scalarp x)
981 (list 'calcFunc-sec x))))
982
983 (defun calcFunc-csc (x)
984 (cond ((and (integerp x)
985 (eq calc-angle-mode 'deg)
986 (= (% (- x 90) 180) 0))
987 (if (= (% (- x 90) 360) 0)
988 1
989 -1))
990 ((Math-scalarp x)
991 (math-with-extra-prec 2
992 (math-csc-raw (math-to-radians (math-float x)))))
993 ((eq (car x) 'sdev)
994 (if (math-constp x)
995 (math-with-extra-prec 2
996 (let* ((xx (math-to-radians (math-float (nth 1 x))))
997 (xs (math-to-radians (math-float (nth 2 x))))
998 (sc (math-sin-cos-raw xx)))
999 (if (and (math-zerop (car sc))
1000 (not calc-infinite-mode))
1001 (progn
1002 (calc-record-why "*Division by zero")
1003 (list 'calcFunc-csc x))
1004 (math-make-sdev (math-div-float '(float 1 0) (car sc))
1005 (math-div-float
1006 (math-mul xs (cdr sc))
1007 (math-sqr (car sc)))))))
1008 (math-make-sdev (calcFunc-csc (nth 1 x))
1009 (math-div
1010 (math-mul (nth 2 x)
1011 (calcFunc-cos (nth 1 x)))
1012 (math-sqr (calcFunc-sin (nth 1 x)))))))
1013 ((and (eq (car x) 'intv)
1014 (math-intv-constp x))
1015 (math-with-extra-prec 2
1016 (let* ((xx (math-to-radians (math-float x)))
1017 (na (math-floor (math-div (nth 2 xx) (math-pi))))
1018 (nb (math-floor (math-div (nth 3 xx) (math-pi))))
1019 (naa (math-floor (math-div (nth 2 xx) (math-pi-over-2))))
1020 (nbb (math-floor (math-div (nth 3 xx) (math-pi-over-2))))
1021 (span (math-sub nbb naa)))
1022 (if (not (equal na nb))
1023 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
1024 (let ((int (math-sort-intv (nth 1 x)
1025 (math-csc-raw (nth 2 xx))
1026 (math-csc-raw (nth 3 xx)))))
1027 (if (eq span 1)
1028 (if (math-evenp (math-div naa 2))
1029 (math-make-intv (logior (nth 1 int) 2)
1030 1
1031 (nth 3 int))
1032 (math-make-intv (logior (nth 1 int) 1)
1033 (nth 2 int)
1034 -1))
1035 int))))))
1036 ((equal x '(var nan var-nan))
1037 x)
1038 (t (calc-record-why 'scalarp x)
1039 (list 'calcFunc-csc x))))
1040
1041 (defun calcFunc-cot (x) ; [N N] [Public]
1042 (cond ((and (integerp x)
1043 (if (eq calc-angle-mode 'deg)
1044 (= (% (- x 90) 180) 0)
1045 (= x 0)))
1046 0)
1047 ((Math-scalarp x)
1048 (math-with-extra-prec 2
1049 (math-cot-raw (math-to-radians (math-float x)))))
1050 ((eq (car x) 'sdev)
1051 (if (math-constp x)
1052 (math-with-extra-prec 2
1053 (let* ((xx (math-to-radians (math-float (nth 1 x))))
1054 (xs (math-to-radians (math-float (nth 2 x))))
1055 (sc (math-sin-cos-raw xx)))
1056 (if (and (math-zerop (car sc)) (not calc-infinite-mode))
1057 (progn
1058 (calc-record-why "*Division by zero")
1059 (list 'calcFunc-cot x))
1060 (math-make-sdev (math-div-float (cdr sc) (car sc))
1061 (math-div-float xs (math-sqr (car sc)))))))
1062 (math-make-sdev (calcFunc-cot (nth 1 x))
1063 (math-div (nth 2 x)
1064 (math-sqr (calcFunc-sin (nth 1 x)))))))
1065 ((and (eq (car x) 'intv) (math-intv-constp x))
1066 (or (math-with-extra-prec 2
1067 (let* ((xx (math-to-radians (math-float x)))
1068 (na (math-floor (math-div (nth 2 xx) (math-pi))))
1069 (nb (math-floor (math-div (nth 3 xx) (math-pi)))))
1070 (and (equal na nb)
1071 (math-sort-intv (nth 1 x)
1072 (math-cot-raw (nth 2 xx))
1073 (math-cot-raw (nth 3 xx))))))
1074 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))))
1075 ((equal x '(var nan var-nan))
1076 x)
1077 (t (calc-record-why 'scalarp x)
1078 (list 'calcFunc-cot x))))
1079
1080 (defun math-sin-raw (x &optional orgx) ; [N N]
1081 (cond ((eq (car x) 'cplx)
1082 (let* ((expx (math-exp-raw (nth 2 x)))
1083 (expmx (math-div-float '(float 1 0) expx))
1084 (sc (math-sin-cos-raw (nth 1 x))))
1085 (list 'cplx
1086 (math-mul-float (car sc)
1087 (math-mul-float (math-add-float expx expmx)
1088 '(float 5 -1)))
1089 (math-mul-float (cdr sc)
1090 (math-mul-float (math-sub-float expx expmx)
1091 '(float 5 -1))))))
1092 ((eq (car x) 'polar)
1093 (math-polar (math-sin-raw (math-complex x))))
1094 ((Math-integer-negp (nth 1 x))
1095 (math-neg-float (math-sin-raw (math-neg-float x) (if orgx orgx x))))
1096 ((math-lessp-float '(float 7 0) x) ; avoid inf loops due to roundoff
1097 (math-sin-raw (math-mod x (math-two-pi)) (if orgx orgx x)))
1098 (t (math-sin-raw-2 x (if orgx orgx x)))))
1099
1100 (defun math-cos-raw (x) ; [N N]
1101 (if (eq (car-safe x) 'polar)
1102 (math-polar (math-cos-raw (math-complex x)))
1103 (math-sin-raw (math-sub (math-pi-over-2) x) x)))
1104
1105 (defun math-sec-raw (x) ; [N N]
1106 (cond ((eq (car x) 'cplx)
1107 (let* ((x (math-mul x '(float 1 0)))
1108 (expx (math-exp-raw (nth 2 x)))
1109 (expmx (math-div-float '(float 1 0) expx))
1110 (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
1111 (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
1112 (sc (math-sin-cos-raw (nth 1 x)))
1113 (d (math-add-float
1114 (math-mul-float (math-sqr (car sc))
1115 (math-sqr sh))
1116 (math-mul-float (math-sqr (cdr sc))
1117 (math-sqr ch)))))
1118 (and (not (eq (nth 1 d) 0))
1119 (list 'cplx
1120 (math-div-float (math-mul-float (cdr sc) ch) d)
1121 (math-div-float (math-mul-float (car sc) sh) d)))))
1122 ((eq (car x) 'polar)
1123 (math-polar (math-sec-raw (math-complex x))))
1124 (t
1125 (let ((cs (math-cos-raw x)))
1126 (if (eq cs 0)
1127 (math-div 1 0)
1128 (math-div-float '(float 1 0) cs))))))
1129
1130 (defun math-csc-raw (x) ; [N N]
1131 (cond ((eq (car x) 'cplx)
1132 (let* ((x (math-mul x '(float 1 0)))
1133 (expx (math-exp-raw (nth 2 x)))
1134 (expmx (math-div-float '(float 1 0) expx))
1135 (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
1136 (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
1137 (sc (math-sin-cos-raw (nth 1 x)))
1138 (d (math-add-float
1139 (math-mul-float (math-sqr (car sc))
1140 (math-sqr ch))
1141 (math-mul-float (math-sqr (cdr sc))
1142 (math-sqr sh)))))
1143 (and (not (eq (nth 1 d) 0))
1144 (list 'cplx
1145 (math-div-float (math-mul-float (car sc) ch) d)
1146 (math-div-float (math-mul-float (cdr sc) sh) d)))))
1147 ((eq (car x) 'polar)
1148 (math-polar (math-csc-raw (math-complex x))))
1149 (t
1150 (let ((sn (math-sin-raw x)))
1151 (if (eq sn 0)
1152 (math-div 1 0)
1153 (math-div-float '(float 1 0) sn))))))
1154
1155 (defun math-cot-raw (x) ; [N N]
1156 (cond ((eq (car x) 'cplx)
1157 (let* ((x (math-mul x '(float 1 0)))
1158 (expx (math-exp-raw (nth 2 x)))
1159 (expmx (math-div-float '(float 1 0) expx))
1160 (sh (math-mul-float (math-sub-float expx expmx) '(float 5 -1)))
1161 (ch (math-mul-float (math-add-float expx expmx) '(float 5 -1)))
1162 (sc (math-sin-cos-raw (nth 1 x)))
1163 (d (math-add-float
1164 (math-sqr (car sc))
1165 (math-sqr sh))))
1166 (and (not (eq (nth 1 d) 0))
1167 (list 'cplx
1168 (math-div-float
1169 (math-mul-float (car sc) (cdr sc))
1170 d)
1171 (math-neg
1172 (math-div-float
1173 (math-mul-float sh ch)
1174 d))))))
1175 ((eq (car x) 'polar)
1176 (math-polar (math-cot-raw (math-complex x))))
1177 (t
1178 (let ((sc (math-sin-cos-raw x)))
1179 (if (eq (nth 1 (car sc)) 0)
1180 (math-div (cdr sc) 0)
1181 (math-div-float (cdr sc) (car sc)))))))
1182
1183
1184 ;;; This could use a smarter method: Reduce x as in math-sin-raw, then
1185 ;;; compute either sin(x) or cos(x), whichever is smaller, and compute
1186 ;;; the other using the identity sin(x)^2 + cos(x)^2 = 1.
1187 (defun math-sin-cos-raw (x) ; [F.F F] (result is (sin x . cos x))
1188 (cons (math-sin-raw x) (math-cos-raw x)))
1189
1190 (defun math-tan-raw (x) ; [N N]
1191 (cond ((eq (car x) 'cplx)
1192 (let* ((x (math-mul x '(float 2 0)))
1193 (expx (math-exp-raw (nth 2 x)))
1194 (expmx (math-div-float '(float 1 0) expx))
1195 (sc (math-sin-cos-raw (nth 1 x)))
1196 (d (math-add-float (cdr sc)
1197 (math-mul-float (math-add-float expx expmx)
1198 '(float 5 -1)))))
1199 (and (not (eq (nth 1 d) 0))
1200 (list 'cplx
1201 (math-div-float (car sc) d)
1202 (math-div-float (math-mul-float (math-sub-float expx
1203 expmx)
1204 '(float 5 -1)) d)))))
1205 ((eq (car x) 'polar)
1206 (math-polar (math-tan-raw (math-complex x))))
1207 (t
1208 (let ((sc (math-sin-cos-raw x)))
1209 (if (eq (nth 1 (cdr sc)) 0)
1210 (math-div (car sc) 0)
1211 (math-div-float (car sc) (cdr sc)))))))
1212
1213 (defun math-sin-raw-2 (x orgx) ; This avoids poss of inf recursion. [F F]
1214 (let ((xmpo2 (math-sub-float (math-pi-over-2) x)))
1215 (cond ((Math-integer-negp (nth 1 xmpo2))
1216 (math-neg-float (math-sin-raw-2 (math-sub-float x (math-pi))
1217 orgx)))
1218 ((math-lessp-float (math-pi-over-4) x)
1219 (math-cos-raw-2 xmpo2 orgx))
1220 ((math-lessp-float x (math-neg (math-pi-over-4)))
1221 (math-neg (math-cos-raw-2 (math-add (math-pi-over-2) x) orgx)))
1222 ((math-with-extra-prec -1 (math-nearly-zerop-float x orgx))
1223 '(float 0 0))
1224 ((math-use-emacs-fn 'sin x))
1225 (calc-symbolic-mode (signal 'inexact-result nil))
1226 (t (math-sin-series x 6 4 x (math-neg-float (math-sqr-float x)))))))
1227
1228 (defun math-cos-raw-2 (x orgx) ; [F F]
1229 (cond ((math-with-extra-prec -1 (math-nearly-zerop-float x orgx))
1230 '(float 1 0))
1231 ((math-use-emacs-fn 'cos x))
1232 (calc-symbolic-mode (signal 'inexact-result nil))
1233 (t (let ((xnegsqr (math-neg-float (math-sqr-float x))))
1234 (math-sin-series
1235 (math-add-float '(float 1 0)
1236 (math-mul-float xnegsqr '(float 5 -1)))
1237 24 5 xnegsqr xnegsqr)))))
1238
1239 (defun math-sin-series (sum nfac n x xnegsqr)
1240 (math-working "sin" sum)
1241 (let* ((nextx (math-mul-float x xnegsqr))
1242 (nextsum (math-add-float sum (math-div-float nextx
1243 (math-float nfac)))))
1244 (if (math-nearly-equal-float sum nextsum)
1245 sum
1246 (math-sin-series nextsum (math-mul nfac (* n (1+ n)))
1247 (+ n 2) nextx xnegsqr))))
1248
1249
1250 ;;; Inverse sine, cosine, tangent.
1251
1252 (defun calcFunc-arcsin (x) ; [N N] [Public]
1253 (cond ((eq x 0) 0)
1254 ((and (eq x 1) (eq calc-angle-mode 'deg)) 90)
1255 ((and (eq x -1) (eq calc-angle-mode 'deg)) -90)
1256 (calc-symbolic-mode (signal 'inexact-result nil))
1257 ((Math-numberp x)
1258 (math-with-extra-prec 2
1259 (math-from-radians (math-arcsin-raw (math-float x)))))
1260 ((eq (car x) 'sdev)
1261 (math-make-sdev (calcFunc-arcsin (nth 1 x))
1262 (math-from-radians
1263 (math-div (nth 2 x)
1264 (math-sqrt
1265 (math-sub 1 (math-sqr (nth 1 x))))))))
1266 ((eq (car x) 'intv)
1267 (math-sort-intv (nth 1 x)
1268 (calcFunc-arcsin (nth 2 x))
1269 (calcFunc-arcsin (nth 3 x))))
1270 ((equal x '(var nan var-nan))
1271 x)
1272 (t (calc-record-why 'numberp x)
1273 (list 'calcFunc-arcsin x))))
1274
1275 (defun calcFunc-arccos (x) ; [N N] [Public]
1276 (cond ((eq x 1) 0)
1277 ((and (eq x 0) (eq calc-angle-mode 'deg)) 90)
1278 ((and (eq x -1) (eq calc-angle-mode 'deg)) 180)
1279 (calc-symbolic-mode (signal 'inexact-result nil))
1280 ((Math-numberp x)
1281 (math-with-extra-prec 2
1282 (math-from-radians (math-arccos-raw (math-float x)))))
1283 ((eq (car x) 'sdev)
1284 (math-make-sdev (calcFunc-arccos (nth 1 x))
1285 (math-from-radians
1286 (math-div (nth 2 x)
1287 (math-sqrt
1288 (math-sub 1 (math-sqr (nth 1 x))))))))
1289 ((eq (car x) 'intv)
1290 (math-sort-intv (nth 1 x)
1291 (calcFunc-arccos (nth 2 x))
1292 (calcFunc-arccos (nth 3 x))))
1293 ((equal x '(var nan var-nan))
1294 x)
1295 (t (calc-record-why 'numberp x)
1296 (list 'calcFunc-arccos x))))
1297
1298 (defun calcFunc-arctan (x) ; [N N] [Public]
1299 (cond ((eq x 0) 0)
1300 ((and (eq x 1) (eq calc-angle-mode 'deg)) 45)
1301 ((and (eq x -1) (eq calc-angle-mode 'deg)) -45)
1302 ((Math-numberp x)
1303 (math-with-extra-prec 2
1304 (math-from-radians (math-arctan-raw (math-float x)))))
1305 ((eq (car x) 'sdev)
1306 (math-make-sdev (calcFunc-arctan (nth 1 x))
1307 (math-from-radians
1308 (math-div (nth 2 x)
1309 (math-add 1 (math-sqr (nth 1 x)))))))
1310 ((eq (car x) 'intv)
1311 (math-sort-intv (nth 1 x)
1312 (calcFunc-arctan (nth 2 x))
1313 (calcFunc-arctan (nth 3 x))))
1314 ((equal x '(var inf var-inf))
1315 (math-quarter-circle t))
1316 ((equal x '(neg (var inf var-inf)))
1317 (math-neg (math-quarter-circle t)))
1318 ((equal x '(var nan var-nan))
1319 x)
1320 (t (calc-record-why 'numberp x)
1321 (list 'calcFunc-arctan x))))
1322
1323 (defun math-arcsin-raw (x) ; [N N]
1324 (let ((a (math-sqrt-raw (math-sub '(float 1 0) (math-sqr x)))))
1325 (if (or (memq (car x) '(cplx polar))
1326 (memq (car a) '(cplx polar)))
1327 (math-with-extra-prec 2 ; use extra precision for difficult case
1328 (math-mul '(cplx 0 -1)
1329 (math-ln-raw (math-add (math-mul '(cplx 0 1) x) a))))
1330 (math-arctan2-raw x a))))
1331
1332 (defun math-arccos-raw (x) ; [N N]
1333 (math-sub (math-pi-over-2) (math-arcsin-raw x)))
1334
1335 (defun math-arctan-raw (x) ; [N N]
1336 (cond ((memq (car x) '(cplx polar))
1337 (math-with-extra-prec 2 ; extra-extra
1338 (math-div (math-sub
1339 (math-ln-raw (math-add 1 (math-mul '(cplx 0 1) x)))
1340 (math-ln-raw (math-add 1 (math-mul '(cplx 0 -1) x))))
1341 '(cplx 0 2))))
1342 ((Math-integer-negp (nth 1 x))
1343 (math-neg-float (math-arctan-raw (math-neg-float x))))
1344 ((math-zerop x) x)
1345 ((math-use-emacs-fn 'atan x))
1346 (calc-symbolic-mode (signal 'inexact-result nil))
1347 ((math-equal-int x 1) (math-pi-over-4))
1348 ((math-equal-int x -1) (math-neg (math-pi-over-4)))
1349 ((math-lessp-float '(float 414214 -6) x) ; if x > sqrt(2) - 1, reduce
1350 (if (math-lessp-float '(float 1 0) x)
1351 (math-sub-float (math-mul-float (math-pi) '(float 5 -1))
1352 (math-arctan-raw (math-div-float '(float 1 0) x)))
1353 (math-sub-float (math-mul-float (math-pi) '(float 25 -2))
1354 (math-arctan-raw (math-div-float
1355 (math-sub-float '(float 1 0) x)
1356 (math-add-float '(float 1 0)
1357 x))))))
1358 (t (math-arctan-series x 3 x (math-neg-float (math-sqr-float x))))))
1359
1360 (defun math-arctan-series (sum n x xnegsqr)
1361 (math-working "arctan" sum)
1362 (let* ((nextx (math-mul-float x xnegsqr))
1363 (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
1364 (if (math-nearly-equal-float sum nextsum)
1365 sum
1366 (math-arctan-series nextsum (+ n 2) nextx xnegsqr))))
1367
1368 (defun calcFunc-arctan2 (y x) ; [F R R] [Public]
1369 (if (Math-anglep y)
1370 (if (Math-anglep x)
1371 (math-with-extra-prec 2
1372 (math-from-radians (math-arctan2-raw (math-float y)
1373 (math-float x))))
1374 (calc-record-why 'anglep x)
1375 (list 'calcFunc-arctan2 y x))
1376 (if (and (or (math-infinitep x) (math-anglep x))
1377 (or (math-infinitep y) (math-anglep y)))
1378 (progn
1379 (if (math-posp x)
1380 (setq x 1)
1381 (if (math-negp x)
1382 (setq x -1)
1383 (or (math-zerop x)
1384 (setq x nil))))
1385 (if (math-posp y)
1386 (setq y 1)
1387 (if (math-negp y)
1388 (setq y -1)
1389 (or (math-zerop y)
1390 (setq y nil))))
1391 (if (and y x)
1392 (calcFunc-arctan2 y x)
1393 '(var nan var-nan)))
1394 (calc-record-why 'anglep y)
1395 (list 'calcFunc-arctan2 y x))))
1396
1397 (defun math-arctan2-raw (y x) ; [F R R]
1398 (cond ((math-zerop y)
1399 (if (math-negp x) (math-pi)
1400 (if (or (math-floatp x) (math-floatp y)) '(float 0 0) 0)))
1401 ((math-zerop x)
1402 (if (math-posp y)
1403 (math-pi-over-2)
1404 (math-neg (math-pi-over-2))))
1405 ((math-posp x)
1406 (math-arctan-raw (math-div-float y x)))
1407 ((math-posp y)
1408 (math-add-float (math-arctan-raw (math-div-float y x))
1409 (math-pi)))
1410 (t
1411 (math-sub-float (math-arctan-raw (math-div-float y x))
1412 (math-pi)))))
1413
1414 (defun calcFunc-arcsincos (x) ; [V N] [Public]
1415 (if (and (Math-vectorp x)
1416 (= (length x) 3))
1417 (calcFunc-arctan2 (nth 2 x) (nth 1 x))
1418 (math-reject-arg x "*Two-element vector expected")))
1419
1420
1421
1422 ;;; Exponential function.
1423
1424 (defun calcFunc-exp (x) ; [N N] [Public]
1425 (cond ((eq x 0) 1)
1426 ((and (memq x '(1 -1)) calc-symbolic-mode)
1427 (if (eq x 1) '(var e var-e) (math-div 1 '(var e var-e))))
1428 ((Math-numberp x)
1429 (math-with-extra-prec 2 (math-exp-raw (math-float x))))
1430 ((eq (car-safe x) 'sdev)
1431 (let ((ex (calcFunc-exp (nth 1 x))))
1432 (math-make-sdev ex (math-mul (nth 2 x) ex))))
1433 ((eq (car-safe x) 'intv)
1434 (math-make-intv (nth 1 x) (calcFunc-exp (nth 2 x))
1435 (calcFunc-exp (nth 3 x))))
1436 ((equal x '(var inf var-inf))
1437 x)
1438 ((equal x '(neg (var inf var-inf)))
1439 0)
1440 ((equal x '(var nan var-nan))
1441 x)
1442 (t (calc-record-why 'numberp x)
1443 (list 'calcFunc-exp x))))
1444
1445 (defun calcFunc-expm1 (x) ; [N N] [Public]
1446 (cond ((eq x 0) 0)
1447 ((math-zerop x) '(float 0 0))
1448 (calc-symbolic-mode (signal 'inexact-result nil))
1449 ((Math-numberp x)
1450 (math-with-extra-prec 2
1451 (let ((x (math-float x)))
1452 (if (and (eq (car x) 'float)
1453 (math-lessp-float x '(float 1 0))
1454 (math-lessp-float '(float -1 0) x))
1455 (math-exp-minus-1-raw x)
1456 (math-add (math-exp-raw x) -1)))))
1457 ((eq (car-safe x) 'sdev)
1458 (if (math-constp x)
1459 (let ((ex (calcFunc-expm1 (nth 1 x))))
1460 (math-make-sdev ex (math-mul (nth 2 x) (math-add ex 1))))
1461 (math-make-sdev (calcFunc-expm1 (nth 1 x))
1462 (math-mul (nth 2 x) (calcFunc-exp (nth 1 x))))))
1463 ((eq (car-safe x) 'intv)
1464 (math-make-intv (nth 1 x)
1465 (calcFunc-expm1 (nth 2 x))
1466 (calcFunc-expm1 (nth 3 x))))
1467 ((equal x '(var inf var-inf))
1468 x)
1469 ((equal x '(neg (var inf var-inf)))
1470 -1)
1471 ((equal x '(var nan var-nan))
1472 x)
1473 (t (calc-record-why 'numberp x)
1474 (list 'calcFunc-expm1 x))))
1475
1476 (defun calcFunc-exp10 (x) ; [N N] [Public]
1477 (if (eq x 0)
1478 1
1479 (math-pow '(float 1 1) x)))
1480
1481 (defun math-exp-raw (x) ; [N N]
1482 (cond ((math-zerop x) '(float 1 0))
1483 (calc-symbolic-mode (signal 'inexact-result nil))
1484 ((eq (car x) 'cplx)
1485 (let ((expx (math-exp-raw (nth 1 x)))
1486 (sc (math-sin-cos-raw (nth 2 x))))
1487 (list 'cplx
1488 (math-mul-float expx (cdr sc))
1489 (math-mul-float expx (car sc)))))
1490 ((eq (car x) 'polar)
1491 (let ((xc (math-complex x)))
1492 (list 'polar
1493 (math-exp-raw (nth 1 xc))
1494 (math-from-radians (nth 2 xc)))))
1495 ((math-use-emacs-fn 'exp x))
1496 ((or (math-lessp-float '(float 5 -1) x)
1497 (math-lessp-float x '(float -5 -1)))
1498 (if (math-lessp-float '(float 921035 1) x)
1499 (math-overflow)
1500 (if (math-lessp-float x '(float -921035 1))
1501 (math-underflow)))
1502 (let* ((two-x (math-mul-float x '(float 2 0)))
1503 (hint (math-scale-int (nth 1 two-x) (nth 2 two-x)))
1504 (hfrac (math-sub-float x (math-mul-float (math-float hint)
1505 '(float 5 -1)))))
1506 (math-mul-float (math-ipow (math-sqrt-e) hint)
1507 (math-add-float '(float 1 0)
1508 (math-exp-minus-1-raw hfrac)))))
1509 (t (math-add-float '(float 1 0) (math-exp-minus-1-raw x)))))
1510
1511 (defun math-exp-minus-1-raw (x) ; [F F]
1512 (math-exp-series x 2 3 x x))
1513
1514 (defun math-exp-series (sum nfac n xpow x)
1515 (math-working "exp" sum)
1516 (let* ((nextx (math-mul-float xpow x))
1517 (nextsum (math-add-float sum (math-div-float nextx
1518 (math-float nfac)))))
1519 (if (math-nearly-equal-float sum nextsum)
1520 sum
1521 (math-exp-series nextsum (math-mul nfac n) (1+ n) nextx x))))
1522
1523
1524
1525 ;;; Logarithms.
1526
1527 (defun calcFunc-ln (x) ; [N N] [Public]
1528 (cond ((math-zerop x)
1529 (if calc-infinite-mode
1530 '(neg (var inf var-inf))
1531 (math-reject-arg x "*Logarithm of zero")))
1532 ((eq x 1) 0)
1533 ((Math-numberp x)
1534 (math-with-extra-prec 2 (math-ln-raw (math-float x))))
1535 ((eq (car-safe x) 'sdev)
1536 (math-make-sdev (calcFunc-ln (nth 1 x))
1537 (math-div (nth 2 x) (nth 1 x))))
1538 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1539 (Math-zerop (nth 2 x))
1540 (not (math-intv-constp x))))
1541 (let ((calc-infinite-mode t))
1542 (math-make-intv (nth 1 x) (calcFunc-ln (nth 2 x))
1543 (calcFunc-ln (nth 3 x)))))
1544 ((equal x '(var e var-e))
1545 1)
1546 ((and (eq (car-safe x) '^)
1547 (equal (nth 1 x) '(var e var-e))
1548 (math-known-realp (nth 2 x)))
1549 (nth 2 x))
1550 ((math-infinitep x)
1551 (if (equal x '(var nan var-nan))
1552 x
1553 '(var inf var-inf)))
1554 (t (calc-record-why 'numberp x)
1555 (list 'calcFunc-ln x))))
1556
1557 (defun calcFunc-log10 (x) ; [N N] [Public]
1558 (cond ((math-equal-int x 1)
1559 (if (math-floatp x) '(float 0 0) 0))
1560 ((and (Math-integerp x)
1561 (math-posp x)
1562 (let ((res (math-integer-log x 10)))
1563 (and (car res)
1564 (setq x (cdr res)))))
1565 x)
1566 ((and (eq (car-safe x) 'frac)
1567 (eq (nth 1 x) 1)
1568 (let ((res (math-integer-log (nth 2 x) 10)))
1569 (and (car res)
1570 (setq x (- (cdr res))))))
1571 x)
1572 ((math-zerop x)
1573 (if calc-infinite-mode
1574 '(neg (var inf var-inf))
1575 (math-reject-arg x "*Logarithm of zero")))
1576 (calc-symbolic-mode (signal 'inexact-result nil))
1577 ((Math-numberp x)
1578 (math-with-extra-prec 2
1579 (let ((xf (math-float x)))
1580 (if (eq (nth 1 xf) 0)
1581 (math-reject-arg x "*Logarithm of zero"))
1582 (if (Math-integer-posp (nth 1 xf))
1583 (if (eq (nth 1 xf) 1) ; log10(1*10^n) = n
1584 (math-float (nth 2 xf))
1585 (let ((xdigs (1- (math-numdigs (nth 1 xf)))))
1586 (math-add-float
1587 (math-div-float (math-ln-raw-2
1588 (list 'float (nth 1 xf) (- xdigs)))
1589 (math-ln-10))
1590 (math-float (+ (nth 2 xf) xdigs)))))
1591 (math-div (calcFunc-ln xf) (math-ln-10))))))
1592 ((eq (car-safe x) 'sdev)
1593 (math-make-sdev (calcFunc-log10 (nth 1 x))
1594 (math-div (nth 2 x)
1595 (math-mul (nth 1 x) (math-ln-10)))))
1596 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1597 (not (math-intv-constp x))))
1598 (math-make-intv (nth 1 x)
1599 (calcFunc-log10 (nth 2 x))
1600 (calcFunc-log10 (nth 3 x))))
1601 ((math-infinitep x)
1602 (if (equal x '(var nan var-nan))
1603 x
1604 '(var inf var-inf)))
1605 (t (calc-record-why 'numberp x)
1606 (list 'calcFunc-log10 x))))
1607
1608 (defun calcFunc-log (x &optional b) ; [N N N] [Public]
1609 (cond ((or (null b) (equal b '(var e var-e)))
1610 (math-normalize (list 'calcFunc-ln x)))
1611 ((or (eq b 10) (equal b '(float 1 1)))
1612 (math-normalize (list 'calcFunc-log10 x)))
1613 ((math-zerop x)
1614 (if calc-infinite-mode
1615 (math-div (calcFunc-ln x) (calcFunc-ln b))
1616 (math-reject-arg x "*Logarithm of zero")))
1617 ((math-zerop b)
1618 (if calc-infinite-mode
1619 (math-div (calcFunc-ln x) (calcFunc-ln b))
1620 (math-reject-arg b "*Logarithm of zero")))
1621 ((math-equal-int b 1)
1622 (if calc-infinite-mode
1623 (math-div (calcFunc-ln x) 0)
1624 (math-reject-arg b "*Logarithm base one")))
1625 ((math-equal-int x 1)
1626 (if (math-floatp b) '(float 0 0) 0))
1627 ((and (Math-ratp x) (Math-ratp b)
1628 (math-posp x) (math-posp b)
1629 (let* ((sign 1) (inv nil)
1630 (xx (if (Math-lessp 1 x)
1631 x
1632 (setq sign -1)
1633 (math-div 1 x)))
1634 (bb (if (Math-lessp 1 b)
1635 b
1636 (setq sign (- sign))
1637 (math-div 1 b)))
1638 (res (if (Math-lessp xx bb)
1639 (setq inv (math-integer-log bb xx))
1640 (math-integer-log xx bb))))
1641 (and (car res)
1642 (setq x (if inv
1643 (math-div 1 (* sign (cdr res)))
1644 (* sign (cdr res)))))))
1645 x)
1646 (calc-symbolic-mode (signal 'inexact-result nil))
1647 ((and (Math-numberp x) (Math-numberp b))
1648 (math-with-extra-prec 2
1649 (math-div (math-ln-raw (math-float x))
1650 (math-log-base-raw b))))
1651 ((and (eq (car-safe x) 'sdev)
1652 (Math-numberp b))
1653 (math-make-sdev (calcFunc-log (nth 1 x) b)
1654 (math-div (nth 2 x)
1655 (math-mul (nth 1 x)
1656 (math-log-base-raw b)))))
1657 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1658 (not (math-intv-constp x)))
1659 (math-realp b))
1660 (math-make-intv (nth 1 x)
1661 (calcFunc-log (nth 2 x) b)
1662 (calcFunc-log (nth 3 x) b)))
1663 ((or (eq (car-safe x) 'intv) (eq (car-safe b) 'intv))
1664 (math-div (calcFunc-ln x) (calcFunc-ln b)))
1665 ((or (math-infinitep x)
1666 (math-infinitep b))
1667 (math-div (calcFunc-ln x) (calcFunc-ln b)))
1668 (t (if (Math-numberp b)
1669 (calc-record-why 'numberp x)
1670 (calc-record-why 'numberp b))
1671 (list 'calcFunc-log x b))))
1672
1673 (defun calcFunc-alog (x &optional b)
1674 (cond ((or (null b) (equal b '(var e var-e)))
1675 (math-normalize (list 'calcFunc-exp x)))
1676 (t (math-pow b x))))
1677
1678 (defun calcFunc-ilog (x b)
1679 (if (and (math-natnump x) (not (eq x 0))
1680 (math-natnump b) (not (eq b 0)))
1681 (if (eq b 1)
1682 (math-reject-arg x "*Logarithm base one")
1683 (if (Math-natnum-lessp x b)
1684 0
1685 (cdr (math-integer-log x b))))
1686 (math-floor (calcFunc-log x b))))
1687
1688 (defun math-integer-log (x b)
1689 (let ((pows (list b))
1690 (pow (math-sqr b))
1691 next
1692 sum n)
1693 (while (not (Math-lessp x pow))
1694 (setq pows (cons pow pows)
1695 pow (math-sqr pow)))
1696 (setq n (lsh 1 (1- (length pows)))
1697 sum n
1698 pow (car pows))
1699 (while (and (setq pows (cdr pows))
1700 (Math-lessp pow x))
1701 (setq n (/ n 2)
1702 next (math-mul pow (car pows)))
1703 (or (Math-lessp x next)
1704 (setq pow next
1705 sum (+ sum n))))
1706 (cons (equal pow x) sum)))
1707
1708
1709 (defvar math-log-base-cache nil)
1710 (defun math-log-base-raw (b) ; [N N]
1711 (if (not (and (equal (car math-log-base-cache) b)
1712 (eq (nth 1 math-log-base-cache) calc-internal-prec)))
1713 (setq math-log-base-cache (list b calc-internal-prec
1714 (math-ln-raw (math-float b)))))
1715 (nth 2 math-log-base-cache))
1716
1717 (defun calcFunc-lnp1 (x) ; [N N] [Public]
1718 (cond ((Math-equal-int x -1)
1719 (if calc-infinite-mode
1720 '(neg (var inf var-inf))
1721 (math-reject-arg x "*Logarithm of zero")))
1722 ((eq x 0) 0)
1723 ((math-zerop x) '(float 0 0))
1724 (calc-symbolic-mode (signal 'inexact-result nil))
1725 ((Math-numberp x)
1726 (math-with-extra-prec 2
1727 (let ((x (math-float x)))
1728 (if (and (eq (car x) 'float)
1729 (math-lessp-float x '(float 5 -1))
1730 (math-lessp-float '(float -5 -1) x))
1731 (math-ln-plus-1-raw x)
1732 (math-ln-raw (math-add-float x '(float 1 0)))))))
1733 ((eq (car-safe x) 'sdev)
1734 (math-make-sdev (calcFunc-lnp1 (nth 1 x))
1735 (math-div (nth 2 x) (math-add (nth 1 x) 1))))
1736 ((and (eq (car-safe x) 'intv) (or (Math-posp (nth 2 x))
1737 (not (math-intv-constp x))))
1738 (math-make-intv (nth 1 x)
1739 (calcFunc-lnp1 (nth 2 x))
1740 (calcFunc-lnp1 (nth 3 x))))
1741 ((math-infinitep x)
1742 (if (equal x '(var nan var-nan))
1743 x
1744 '(var inf var-inf)))
1745 (t (calc-record-why 'numberp x)
1746 (list 'calcFunc-lnp1 x))))
1747
1748 (defun math-ln-raw (x) ; [N N] --- must be float format!
1749 (cond ((eq (car-safe x) 'cplx)
1750 (list 'cplx
1751 (math-mul-float (math-ln-raw
1752 (math-add-float (math-sqr-float (nth 1 x))
1753 (math-sqr-float (nth 2 x))))
1754 '(float 5 -1))
1755 (math-arctan2-raw (nth 2 x) (nth 1 x))))
1756 ((eq (car x) 'polar)
1757 (math-polar (list 'cplx
1758 (math-ln-raw (nth 1 x))
1759 (math-to-radians (nth 2 x)))))
1760 ((Math-equal-int x 1)
1761 '(float 0 0))
1762 (calc-symbolic-mode (signal 'inexact-result nil))
1763 ((math-posp (nth 1 x)) ; positive and real
1764 (cond
1765 ((math-use-emacs-fn 'log x))
1766 (t
1767 (let ((xdigs (1- (math-numdigs (nth 1 x)))))
1768 (math-add-float (math-ln-raw-2 (list 'float (nth 1 x) (- xdigs)))
1769 (math-mul-float (math-float (+ (nth 2 x) xdigs))
1770 (math-ln-10)))))))
1771 ((math-zerop x)
1772 (math-reject-arg x "*Logarithm of zero"))
1773 ((eq calc-complex-mode 'polar) ; negative and real
1774 (math-polar
1775 (list 'cplx ; negative and real
1776 (math-ln-raw (math-neg-float x))
1777 (math-pi))))
1778 (t (list 'cplx ; negative and real
1779 (math-ln-raw (math-neg-float x))
1780 (math-pi)))))
1781
1782 (defun math-ln-raw-2 (x) ; [F F]
1783 (cond ((math-lessp-float '(float 14 -1) x)
1784 (math-add-float (math-ln-raw-2 (math-mul-float x '(float 5 -1)))
1785 (math-ln-2)))
1786 (t ; now .7 < x <= 1.4
1787 (math-ln-raw-3 (math-div-float (math-sub-float x '(float 1 0))
1788 (math-add-float x '(float 1 0)))))))
1789
1790 (defun math-ln-raw-3 (x) ; [F F]
1791 (math-mul-float (math-ln-raw-series x 3 x (math-sqr-float x))
1792 '(float 2 0)))
1793
1794 ;;; Compute ln((1+x)/(1-x))
1795 (defun math-ln-raw-series (sum n x xsqr)
1796 (math-working "log" sum)
1797 (let* ((nextx (math-mul-float x xsqr))
1798 (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
1799 (if (math-nearly-equal-float sum nextsum)
1800 sum
1801 (math-ln-raw-series nextsum (+ n 2) nextx xsqr))))
1802
1803 (defun math-ln-plus-1-raw (x)
1804 (math-lnp1-series x 2 x (math-neg x)))
1805
1806 (defun math-lnp1-series (sum n xpow x)
1807 (math-working "lnp1" sum)
1808 (let* ((nextx (math-mul-float xpow x))
1809 (nextsum (math-add-float sum (math-div-float nextx (math-float n)))))
1810 (if (math-nearly-equal-float sum nextsum)
1811 sum
1812 (math-lnp1-series nextsum (1+ n) nextx x))))
1813
1814 (defconst math-approx-ln-10
1815 (math-read-number-simple "2.302585092994045684018")
1816 "An approximation for ln(10).")
1817
1818 (math-defcache math-ln-10 math-approx-ln-10
1819 (math-ln-raw-2 '(float 1 1)))
1820
1821 (defconst math-approx-ln-2
1822 (math-read-number-simple "0.693147180559945309417")
1823 "An approximation for ln(2).")
1824
1825 (math-defcache math-ln-2 math-approx-ln-2
1826 (math-ln-raw-3 (math-float '(frac 1 3))))
1827
1828
1829
1830 ;;; Hyperbolic functions.
1831
1832 (defun calcFunc-sinh (x) ; [N N] [Public]
1833 (cond ((eq x 0) 0)
1834 (math-expand-formulas
1835 (math-normalize
1836 (list '/ (list '- (list 'calcFunc-exp x)
1837 (list 'calcFunc-exp (list 'neg x))) 2)))
1838 ((Math-numberp x)
1839 (if calc-symbolic-mode (signal 'inexact-result nil))
1840 (math-with-extra-prec 2
1841 (let ((expx (math-exp-raw (math-float x))))
1842 (math-mul (math-add expx (math-div -1 expx)) '(float 5 -1)))))
1843 ((eq (car-safe x) 'sdev)
1844 (math-make-sdev (calcFunc-sinh (nth 1 x))
1845 (math-mul (nth 2 x) (calcFunc-cosh (nth 1 x)))))
1846 ((eq (car x) 'intv)
1847 (math-sort-intv (nth 1 x)
1848 (calcFunc-sinh (nth 2 x))
1849 (calcFunc-sinh (nth 3 x))))
1850 ((or (equal x '(var inf var-inf))
1851 (equal x '(neg (var inf var-inf)))
1852 (equal x '(var nan var-nan)))
1853 x)
1854 (t (calc-record-why 'numberp x)
1855 (list 'calcFunc-sinh x))))
1856 (put 'calcFunc-sinh 'math-expandable t)
1857
1858 (defun calcFunc-cosh (x) ; [N N] [Public]
1859 (cond ((eq x 0) 1)
1860 (math-expand-formulas
1861 (math-normalize
1862 (list '/ (list '+ (list 'calcFunc-exp x)
1863 (list 'calcFunc-exp (list 'neg x))) 2)))
1864 ((Math-numberp x)
1865 (if calc-symbolic-mode (signal 'inexact-result nil))
1866 (math-with-extra-prec 2
1867 (let ((expx (math-exp-raw (math-float x))))
1868 (math-mul (math-add expx (math-div 1 expx)) '(float 5 -1)))))
1869 ((eq (car-safe x) 'sdev)
1870 (math-make-sdev (calcFunc-cosh (nth 1 x))
1871 (math-mul (nth 2 x)
1872 (calcFunc-sinh (nth 1 x)))))
1873 ((and (eq (car x) 'intv) (math-intv-constp x))
1874 (setq x (math-abs x))
1875 (math-sort-intv (nth 1 x)
1876 (calcFunc-cosh (nth 2 x))
1877 (calcFunc-cosh (nth 3 x))))
1878 ((or (equal x '(var inf var-inf))
1879 (equal x '(neg (var inf var-inf)))
1880 (equal x '(var nan var-nan)))
1881 (math-abs x))
1882 (t (calc-record-why 'numberp x)
1883 (list 'calcFunc-cosh x))))
1884 (put 'calcFunc-cosh 'math-expandable t)
1885
1886 (defun calcFunc-tanh (x) ; [N N] [Public]
1887 (cond ((eq x 0) 0)
1888 (math-expand-formulas
1889 (math-normalize
1890 (let ((expx (list 'calcFunc-exp x))
1891 (expmx (list 'calcFunc-exp (list 'neg x))))
1892 (math-normalize
1893 (list '/ (list '- expx expmx) (list '+ expx expmx))))))
1894 ((Math-numberp x)
1895 (if calc-symbolic-mode (signal 'inexact-result nil))
1896 (math-with-extra-prec 2
1897 (let* ((expx (calcFunc-exp (math-float x)))
1898 (expmx (math-div 1 expx)))
1899 (math-div (math-sub expx expmx)
1900 (math-add expx expmx)))))
1901 ((eq (car-safe x) 'sdev)
1902 (math-make-sdev (calcFunc-tanh (nth 1 x))
1903 (math-div (nth 2 x)
1904 (math-sqr (calcFunc-cosh (nth 1 x))))))
1905 ((eq (car x) 'intv)
1906 (math-sort-intv (nth 1 x)
1907 (calcFunc-tanh (nth 2 x))
1908 (calcFunc-tanh (nth 3 x))))
1909 ((equal x '(var inf var-inf))
1910 1)
1911 ((equal x '(neg (var inf var-inf)))
1912 -1)
1913 ((equal x '(var nan var-nan))
1914 x)
1915 (t (calc-record-why 'numberp x)
1916 (list 'calcFunc-tanh x))))
1917 (put 'calcFunc-tanh 'math-expandable t)
1918
1919 (defun calcFunc-sech (x) ; [N N] [Public]
1920 (cond ((eq x 0) 1)
1921 (math-expand-formulas
1922 (math-normalize
1923 (list '/ 2 (list '+ (list 'calcFunc-exp x)
1924 (list 'calcFunc-exp (list 'neg x))))))
1925 ((Math-numberp x)
1926 (if calc-symbolic-mode (signal 'inexact-result nil))
1927 (math-with-extra-prec 2
1928 (let ((expx (math-exp-raw (math-float x))))
1929 (math-div '(float 2 0) (math-add expx (math-div 1 expx))))))
1930 ((eq (car-safe x) 'sdev)
1931 (math-make-sdev (calcFunc-sech (nth 1 x))
1932 (math-mul (nth 2 x)
1933 (math-mul (calcFunc-sech (nth 1 x))
1934 (calcFunc-tanh (nth 1 x))))))
1935 ((and (eq (car x) 'intv) (math-intv-constp x))
1936 (setq x (math-abs x))
1937 (math-sort-intv (nth 1 x)
1938 (calcFunc-sech (nth 2 x))
1939 (calcFunc-sech (nth 3 x))))
1940 ((or (equal x '(var inf var-inf))
1941 (equal x '(neg (var inf var-inf))))
1942 0)
1943 ((equal x '(var nan var-nan))
1944 x)
1945 (t (calc-record-why 'numberp x)
1946 (list 'calcFunc-sech x))))
1947 (put 'calcFunc-sech 'math-expandable t)
1948
1949 (defun calcFunc-csch (x) ; [N N] [Public]
1950 (cond ((eq x 0) (math-div 1 0))
1951 (math-expand-formulas
1952 (math-normalize
1953 (list '/ 2 (list '- (list 'calcFunc-exp x)
1954 (list 'calcFunc-exp (list 'neg x))))))
1955 ((Math-numberp x)
1956 (if calc-symbolic-mode (signal 'inexact-result nil))
1957 (math-with-extra-prec 2
1958 (let ((expx (math-exp-raw (math-float x))))
1959 (math-div '(float 2 0) (math-add expx (math-div -1 expx))))))
1960 ((eq (car-safe x) 'sdev)
1961 (math-make-sdev (calcFunc-csch (nth 1 x))
1962 (math-mul (nth 2 x)
1963 (math-mul (calcFunc-csch (nth 1 x))
1964 (calcFunc-coth (nth 1 x))))))
1965 ((eq (car x) 'intv)
1966 (if (and (Math-negp (nth 2 x))
1967 (Math-posp (nth 3 x)))
1968 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
1969 (math-sort-intv (nth 1 x)
1970 (calcFunc-csch (nth 2 x))
1971 (calcFunc-csch (nth 3 x)))))
1972 ((or (equal x '(var inf var-inf))
1973 (equal x '(neg (var inf var-inf))))
1974 0)
1975 ((equal x '(var nan var-nan))
1976 x)
1977 (t (calc-record-why 'numberp x)
1978 (list 'calcFunc-csch x))))
1979 (put 'calcFunc-csch 'math-expandable t)
1980
1981 (defun calcFunc-coth (x) ; [N N] [Public]
1982 (cond ((eq x 0) (math-div 1 0))
1983 (math-expand-formulas
1984 (math-normalize
1985 (let ((expx (list 'calcFunc-exp x))
1986 (expmx (list 'calcFunc-exp (list 'neg x))))
1987 (math-normalize
1988 (list '/ (list '+ expx expmx) (list '- expx expmx))))))
1989 ((Math-numberp x)
1990 (if calc-symbolic-mode (signal 'inexact-result nil))
1991 (math-with-extra-prec 2
1992 (let* ((expx (calcFunc-exp (math-float x)))
1993 (expmx (math-div 1 expx)))
1994 (math-div (math-add expx expmx)
1995 (math-sub expx expmx)))))
1996 ((eq (car-safe x) 'sdev)
1997 (math-make-sdev (calcFunc-coth (nth 1 x))
1998 (math-div (nth 2 x)
1999 (math-sqr (calcFunc-sinh (nth 1 x))))))
2000 ((eq (car x) 'intv)
2001 (if (and (Math-negp (nth 2 x))
2002 (Math-posp (nth 3 x)))
2003 '(intv 3 (neg (var inf var-inf)) (var inf var-inf))
2004 (math-sort-intv (nth 1 x)
2005 (calcFunc-coth (nth 2 x))
2006 (calcFunc-coth (nth 3 x)))))
2007 ((equal x '(var inf var-inf))
2008 1)
2009 ((equal x '(neg (var inf var-inf)))
2010 -1)
2011 ((equal x '(var nan var-nan))
2012 x)
2013 (t (calc-record-why 'numberp x)
2014 (list 'calcFunc-coth x))))
2015 (put 'calcFunc-coth 'math-expandable t)
2016
2017 (defun calcFunc-arcsinh (x) ; [N N] [Public]
2018 (cond ((eq x 0) 0)
2019 (math-expand-formulas
2020 (math-normalize
2021 (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
2022 (list '+ (list '^ x 2) 1))))))
2023 ((Math-numberp x)
2024 (if calc-symbolic-mode (signal 'inexact-result nil))
2025 (math-with-extra-prec 2
2026 (math-ln-raw (math-add x (math-sqrt-raw (math-add (math-sqr x)
2027 '(float 1 0)))))))
2028 ((eq (car-safe x) 'sdev)
2029 (math-make-sdev (calcFunc-arcsinh (nth 1 x))
2030 (math-div (nth 2 x)
2031 (math-sqrt
2032 (math-add (math-sqr (nth 1 x)) 1)))))
2033 ((eq (car x) 'intv)
2034 (math-sort-intv (nth 1 x)
2035 (calcFunc-arcsinh (nth 2 x))
2036 (calcFunc-arcsinh (nth 3 x))))
2037 ((or (equal x '(var inf var-inf))
2038 (equal x '(neg (var inf var-inf)))
2039 (equal x '(var nan var-nan)))
2040 x)
2041 (t (calc-record-why 'numberp x)
2042 (list 'calcFunc-arcsinh x))))
2043 (put 'calcFunc-arcsinh 'math-expandable t)
2044
2045 (defun calcFunc-arccosh (x) ; [N N] [Public]
2046 (cond ((eq x 1) 0)
2047 ((and (eq x -1) calc-symbolic-mode)
2048 '(var pi var-pi))
2049 ((and (eq x 0) calc-symbolic-mode)
2050 (math-div (math-mul '(var pi var-pi) '(var i var-i)) 2))
2051 (math-expand-formulas
2052 (math-normalize
2053 (list 'calcFunc-ln (list '+ x (list 'calcFunc-sqrt
2054 (list '- (list '^ x 2) 1))))))
2055 ((Math-numberp x)
2056 (if calc-symbolic-mode (signal 'inexact-result nil))
2057 (if (Math-equal-int x -1)
2058 (math-imaginary (math-pi))
2059 (math-with-extra-prec 2
2060 (if (or t ; need to do this even in the real case!
2061 (memq (car-safe x) '(cplx polar)))
2062 (let ((xp1 (math-add 1 x))) ; this gets the branch cuts right
2063 (math-ln-raw
2064 (math-add x (math-mul xp1
2065 (math-sqrt-raw
2066 (math-div (math-sub
2067 x
2068 '(float 1 0))
2069 xp1))))))
2070 (math-ln-raw
2071 (math-add x (math-sqrt-raw (math-add (math-sqr x)
2072 '(float -1 0)))))))))
2073 ((eq (car-safe x) 'sdev)
2074 (math-make-sdev (calcFunc-arccosh (nth 1 x))
2075 (math-div (nth 2 x)
2076 (math-sqrt
2077 (math-add (math-sqr (nth 1 x)) -1)))))
2078 ((eq (car x) 'intv)
2079 (math-sort-intv (nth 1 x)
2080 (calcFunc-arccosh (nth 2 x))
2081 (calcFunc-arccosh (nth 3 x))))
2082 ((or (equal x '(var inf var-inf))
2083 (equal x '(neg (var inf var-inf)))
2084 (equal x '(var nan var-nan)))
2085 x)
2086 (t (calc-record-why 'numberp x)
2087 (list 'calcFunc-arccosh x))))
2088 (put 'calcFunc-arccosh 'math-expandable t)
2089
2090 (defun calcFunc-arctanh (x) ; [N N] [Public]
2091 (cond ((eq x 0) 0)
2092 ((and (Math-equal-int x 1) calc-infinite-mode)
2093 '(var inf var-inf))
2094 ((and (Math-equal-int x -1) calc-infinite-mode)
2095 '(neg (var inf var-inf)))
2096 (math-expand-formulas
2097 (list '/ (list '-
2098 (list 'calcFunc-ln (list '+ 1 x))
2099 (list 'calcFunc-ln (list '- 1 x))) 2))
2100 ((Math-numberp x)
2101 (if calc-symbolic-mode (signal 'inexact-result nil))
2102 (math-with-extra-prec 2
2103 (if (or (memq (car-safe x) '(cplx polar))
2104 (Math-lessp 1 x))
2105 (math-mul (math-sub (math-ln-raw (math-add '(float 1 0) x))
2106 (math-ln-raw (math-sub '(float 1 0) x)))
2107 '(float 5 -1))
2108 (if (and (math-equal-int x 1) calc-infinite-mode)
2109 '(var inf var-inf)
2110 (if (and (math-equal-int x -1) calc-infinite-mode)
2111 '(neg (var inf var-inf))
2112 (math-mul (math-ln-raw (math-div (math-add '(float 1 0) x)
2113 (math-sub 1 x)))
2114 '(float 5 -1)))))))
2115 ((eq (car-safe x) 'sdev)
2116 (math-make-sdev (calcFunc-arctanh (nth 1 x))
2117 (math-div (nth 2 x)
2118 (math-sub 1 (math-sqr (nth 1 x))))))
2119 ((eq (car x) 'intv)
2120 (math-sort-intv (nth 1 x)
2121 (calcFunc-arctanh (nth 2 x))
2122 (calcFunc-arctanh (nth 3 x))))
2123 ((equal x '(var nan var-nan))
2124 x)
2125 (t (calc-record-why 'numberp x)
2126 (list 'calcFunc-arctanh x))))
2127 (put 'calcFunc-arctanh 'math-expandable t)
2128
2129
2130 ;;; Convert A from HMS or degrees to radians.
2131 (defun calcFunc-rad (a) ; [R R] [Public]
2132 (cond ((or (Math-numberp a)
2133 (eq (car a) 'intv))
2134 (math-with-extra-prec 2
2135 (math-mul a (math-pi-over-180))))
2136 ((eq (car a) 'hms)
2137 (math-from-hms a 'rad))
2138 ((eq (car a) 'sdev)
2139 (math-make-sdev (calcFunc-rad (nth 1 a))
2140 (calcFunc-rad (nth 2 a))))
2141 (math-expand-formulas
2142 (math-div (math-mul a '(var pi var-pi)) 180))
2143 ((math-infinitep a) a)
2144 (t (list 'calcFunc-rad a))))
2145 (put 'calcFunc-rad 'math-expandable t)
2146
2147 ;;; Convert A from HMS or radians to degrees.
2148 (defun calcFunc-deg (a) ; [R R] [Public]
2149 (cond ((or (Math-numberp a)
2150 (eq (car a) 'intv))
2151 (math-with-extra-prec 2
2152 (math-div a (math-pi-over-180))))
2153 ((eq (car a) 'hms)
2154 (math-from-hms a 'deg))
2155 ((eq (car a) 'sdev)
2156 (math-make-sdev (calcFunc-deg (nth 1 a))
2157 (calcFunc-deg (nth 2 a))))
2158 (math-expand-formulas
2159 (math-div (math-mul 180 a) '(var pi var-pi)))
2160 ((math-infinitep a) a)
2161 (t (list 'calcFunc-deg a))))
2162 (put 'calcFunc-deg 'math-expandable t)
2163
2164 (provide 'calc-math)
2165
2166 ;;; calc-math.el ends here