Stylistic changes in tramp-cache.el
[emacs.git] / lisp / calc / calc-funcs.el
blob2bb460df3c903af86d501f12bd3ef8783cd8a1e8
1 ;;; calc-funcs.el --- well-known functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2017 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 ;; This file is autoloaded from calc-ext.el.
28 (require 'calc-ext)
29 (require 'calc-macs)
31 (defun calc-inc-gamma (arg)
32 (interactive "P")
33 (calc-slow-wrapper
34 (if (calc-is-inverse)
35 (if (calc-is-hyperbolic)
36 (calc-binary-op "gamG" 'calcFunc-gammaG arg)
37 (calc-binary-op "gamQ" 'calcFunc-gammaQ arg))
38 (if (calc-is-hyperbolic)
39 (calc-binary-op "gamg" 'calcFunc-gammag arg)
40 (calc-binary-op "gamP" 'calcFunc-gammaP arg)))))
42 (defun calc-erf (arg)
43 (interactive "P")
44 (calc-slow-wrapper
45 (if (calc-is-inverse)
46 (calc-unary-op "erfc" 'calcFunc-erfc arg)
47 (calc-unary-op "erf" 'calcFunc-erf arg))))
49 (defun calc-erfc (arg)
50 (interactive "P")
51 (calc-invert-func)
52 (calc-erf arg))
54 (defun calc-beta (arg)
55 (interactive "P")
56 (calc-slow-wrapper
57 (calc-binary-op "beta" 'calcFunc-beta arg)))
59 (defun calc-inc-beta ()
60 (interactive)
61 (calc-slow-wrapper
62 (if (calc-is-hyperbolic)
63 (calc-enter-result 3 "betB" (cons 'calcFunc-betaB (calc-top-list-n 3)))
64 (calc-enter-result 3 "betI" (cons 'calcFunc-betaI (calc-top-list-n 3))))))
66 (defun calc-bessel-J (arg)
67 (interactive "P")
68 (calc-slow-wrapper
69 (calc-binary-op "besJ" 'calcFunc-besJ arg)))
71 (defun calc-bessel-Y (arg)
72 (interactive "P")
73 (calc-slow-wrapper
74 (calc-binary-op "besY" 'calcFunc-besY arg)))
76 (defun calc-bernoulli-number (arg)
77 (interactive "P")
78 (calc-slow-wrapper
79 (if (calc-is-hyperbolic)
80 (calc-binary-op "bern" 'calcFunc-bern arg)
81 (calc-unary-op "bern" 'calcFunc-bern arg))))
83 (defun calc-euler-number (arg)
84 (interactive "P")
85 (calc-slow-wrapper
86 (if (calc-is-hyperbolic)
87 (calc-binary-op "eulr" 'calcFunc-euler arg)
88 (calc-unary-op "eulr" 'calcFunc-euler arg))))
90 (defun calc-stirling-number (arg)
91 (interactive "P")
92 (calc-slow-wrapper
93 (if (calc-is-hyperbolic)
94 (calc-binary-op "str2" 'calcFunc-stir2 arg)
95 (calc-binary-op "str1" 'calcFunc-stir1 arg))))
97 (defun calc-utpb ()
98 (interactive)
99 (calc-prob-dist "b" 3))
101 (defun calc-utpc ()
102 (interactive)
103 (calc-prob-dist "c" 2))
105 (defun calc-utpf ()
106 (interactive)
107 (calc-prob-dist "f" 3))
109 (defun calc-utpn ()
110 (interactive)
111 (calc-prob-dist "n" 3))
113 (defun calc-utpp ()
114 (interactive)
115 (calc-prob-dist "p" 2))
117 (defun calc-utpt ()
118 (interactive)
119 (calc-prob-dist "t" 2))
121 (defun calc-prob-dist (letter nargs)
122 (calc-slow-wrapper
123 (if (calc-is-inverse)
124 (calc-enter-result nargs (concat "ltp" letter)
125 (append (list (intern (concat "calcFunc-ltp" letter))
126 (calc-top-n 1))
127 (calc-top-list-n (1- nargs) 2)))
128 (calc-enter-result nargs (concat "utp" letter)
129 (append (list (intern (concat "calcFunc-utp" letter))
130 (calc-top-n 1))
131 (calc-top-list-n (1- nargs) 2))))))
136 ;;; Sources: Numerical Recipes, Press et al;
137 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
140 ;;; Gamma function.
142 (defun calcFunc-gamma (x)
143 (or (math-numberp x) (math-reject-arg x 'numberp))
144 (calcFunc-fact (math-add x -1)))
146 (defun math-gammap1-raw (x &optional fprec nfprec)
147 "Compute gamma(1+X) to the appropriate precision."
148 (or fprec
149 (setq fprec (math-float calc-internal-prec)
150 nfprec (math-float (- calc-internal-prec))))
151 (cond ((math-lessp-float (calcFunc-re x) fprec)
152 (if (math-lessp-float (calcFunc-re x) nfprec)
153 (math-neg (math-div
154 (math-pi)
155 (math-mul (math-gammap1-raw
156 (math-add (math-neg x)
157 '(float -1 0))
158 fprec nfprec)
159 (math-sin-raw
160 (math-mul (math-pi) x)))))
161 (let ((xplus1 (math-add x '(float 1 0))))
162 (math-div (math-gammap1-raw xplus1 fprec nfprec) xplus1))))
163 ((and (math-realp x)
164 (math-lessp-float '(float 736276 0) x))
165 (math-overflow))
166 (t ; re(x) now >= 10.0
167 (let ((xinv (math-div 1 x))
168 (lnx (math-ln-raw x)))
169 (math-mul (math-sqrt-two-pi)
170 (math-exp-raw
171 (math-gamma-series
172 (math-sub (math-mul (math-add x '(float 5 -1))
173 lnx)
175 xinv
176 (math-sqr xinv)
177 '(float 0 0)
178 2)))))))
180 (defun math-gamma-series (sum x xinvsqr oterm n)
181 (math-working "gamma" sum)
182 (let* ((bn (math-bernoulli-number n))
183 (term (math-mul (math-div-float (math-float (nth 1 bn))
184 (math-float (* (nth 2 bn)
185 (* n (1- n)))))
187 (next (math-add sum term)))
188 (if (math-nearly-equal sum next)
189 next
190 (if (> n (* 2 calc-internal-prec))
191 (progn
192 ;; Need this because series eventually diverges for large enough n.
193 (calc-record-why
194 "*Gamma computation stopped early, not all digits may be valid")
195 next)
196 (math-gamma-series next (math-mul x xinvsqr) xinvsqr term (+ n 2))))))
199 ;;; Incomplete gamma function.
201 (defvar math-current-gamma-value nil)
202 (defun calcFunc-gammaP (a x)
203 (if (equal x '(var inf var-inf))
204 '(float 1 0)
205 (math-inexact-result)
206 (or (Math-numberp a) (math-reject-arg a 'numberp))
207 (or (math-numberp x) (math-reject-arg x 'numberp))
208 (if (and (math-num-integerp a)
209 (integerp (setq a (math-trunc a)))
210 (> a 0) (< a 20))
211 (math-sub 1 (calcFunc-gammaQ a x))
212 (let ((math-current-gamma-value (calcFunc-gamma a)))
213 (math-div (calcFunc-gammag a x) math-current-gamma-value)))))
215 (defun calcFunc-gammaQ (a x)
216 (if (equal x '(var inf var-inf))
217 '(float 0 0)
218 (math-inexact-result)
219 (or (Math-numberp a) (math-reject-arg a 'numberp))
220 (or (math-numberp x) (math-reject-arg x 'numberp))
221 (if (and (math-num-integerp a)
222 (integerp (setq a (math-trunc a)))
223 (> a 0) (< a 20))
224 (let ((n 0)
225 (sum '(float 1 0))
226 (term '(float 1 0)))
227 (math-with-extra-prec 1
228 (while (< (setq n (1+ n)) a)
229 (setq term (math-div (math-mul term x) n)
230 sum (math-add sum term))
231 (math-working "gamma" sum))
232 (math-mul sum (calcFunc-exp (math-neg x)))))
233 (let ((math-current-gamma-value (calcFunc-gamma a)))
234 (math-div (calcFunc-gammaG a x) math-current-gamma-value)))))
236 (defun calcFunc-gammag (a x)
237 (if (equal x '(var inf var-inf))
238 (calcFunc-gamma a)
239 (math-inexact-result)
240 (or (Math-numberp a) (math-reject-arg a 'numberp))
241 (or (Math-numberp x) (math-reject-arg x 'numberp))
242 (math-with-extra-prec 2
243 (setq a (math-float a))
244 (setq x (math-float x))
245 (if (or (math-negp (calcFunc-re a))
246 (math-lessp-float (calcFunc-re x)
247 (math-add-float (calcFunc-re a)
248 '(float 1 0))))
249 (math-inc-gamma-series a x)
250 (math-sub (or math-current-gamma-value (calcFunc-gamma a))
251 (math-inc-gamma-cfrac a x))))))
253 (defun calcFunc-gammaG (a x)
254 (if (equal x '(var inf var-inf))
255 '(float 0 0)
256 (math-inexact-result)
257 (or (Math-numberp a) (math-reject-arg a 'numberp))
258 (or (Math-numberp x) (math-reject-arg x 'numberp))
259 (math-with-extra-prec 2
260 (setq a (math-float a))
261 (setq x (math-float x))
262 (if (or (math-negp (calcFunc-re a))
263 (math-lessp-float (calcFunc-re x)
264 (math-add-float (math-abs-approx a)
265 '(float 1 0))))
266 (math-sub (or math-current-gamma-value (calcFunc-gamma a))
267 (math-inc-gamma-series a x))
268 (math-inc-gamma-cfrac a x)))))
270 (defun math-inc-gamma-series (a x)
271 (if (Math-zerop x)
272 '(float 0 0)
273 (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x))
274 (math-with-extra-prec 2
275 (let ((start (math-div '(float 1 0) a)))
276 (math-inc-gamma-series-step start start a x))))))
278 (defun math-inc-gamma-series-step (sum term a x)
279 (math-working "gamma" sum)
280 (setq a (math-add a '(float 1 0))
281 term (math-div (math-mul term x) a))
282 (let ((next (math-add sum term)))
283 (if (math-nearly-equal sum next)
284 next
285 (math-inc-gamma-series-step next term a x))))
287 (defun math-inc-gamma-cfrac (a x)
288 (if (Math-zerop x)
289 (or math-current-gamma-value (calcFunc-gamma a))
290 (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x))
291 (math-inc-gamma-cfrac-step '(float 1 0) x
292 '(float 0 0) '(float 1 0)
293 '(float 1 0) '(float 1 0) '(float 0 0)
294 a x))))
296 (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x)
297 (let ((ana (math-sub n a))
298 (anf (math-mul n fac)))
299 (setq n (math-add n '(float 1 0))
300 a0 (math-mul (math-add a1 (math-mul a0 ana)) fac)
301 b0 (math-mul (math-add b1 (math-mul b0 ana)) fac)
302 a1 (math-add (math-mul x a0) (math-mul anf a1))
303 b1 (math-add (math-mul x b0) (math-mul anf b1)))
304 (if (math-zerop a1)
305 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x)
306 (setq fac (math-div '(float 1 0) a1))
307 (let ((next (math-mul b1 fac)))
308 (math-working "gamma" next)
309 (if (math-nearly-equal next g)
310 next
311 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x))))))
314 ;;; Error function.
316 (defun calcFunc-erf (x)
317 (if (equal x '(var inf var-inf))
318 '(float 1 0)
319 (if (equal x '(neg (var inf var-inf)))
320 '(float -1 0)
321 (if (Math-zerop x)
323 (let ((math-current-gamma-value (math-sqrt-pi)))
324 (math-to-same-complex-quad
325 (math-div (calcFunc-gammag '(float 5 -1)
326 (math-sqr (math-to-complex-quad-one x)))
327 math-current-gamma-value)
328 x))))))
330 (defun calcFunc-erfc (x)
331 (if (equal x '(var inf var-inf))
332 '(float 0 0)
333 (if (math-posp x)
334 (let ((math-current-gamma-value (math-sqrt-pi)))
335 (math-div (calcFunc-gammaG '(float 5 -1) (math-sqr x))
336 math-current-gamma-value))
337 (math-sub 1 (calcFunc-erf x)))))
339 (defun math-to-complex-quad-one (x)
340 (if (eq (car-safe x) 'polar) (setq x (math-complex x)))
341 (if (eq (car-safe x) 'cplx)
342 (list 'cplx (math-abs (nth 1 x)) (math-abs (nth 2 x)))
345 (defun math-to-same-complex-quad (x y)
346 (if (eq (car-safe y) 'cplx)
347 (if (eq (car-safe x) 'cplx)
348 (list 'cplx
349 (if (math-negp (nth 1 y)) (math-neg (nth 1 x)) (nth 1 x))
350 (if (math-negp (nth 2 y)) (math-neg (nth 2 x)) (nth 2 x)))
351 (if (math-negp (nth 1 y)) (math-neg x) x))
352 (if (math-negp y)
353 (if (eq (car-safe x) 'cplx)
354 (list 'cplx (math-neg (nth 1 x)) (nth 2 x))
355 (math-neg x))
356 x)))
359 ;;; Beta function.
361 (defun calcFunc-beta (a b)
362 (if (math-num-integerp a)
363 (let ((am (math-add a -1)))
364 (or (math-numberp b) (math-reject-arg b 'numberp))
365 (math-div 1 (math-mul b (calcFunc-choose (math-add b am) am))))
366 (if (math-num-integerp b)
367 (calcFunc-beta b a)
368 (math-div (math-mul (calcFunc-gamma a) (calcFunc-gamma b))
369 (calcFunc-gamma (math-add a b))))))
372 ;;; Incomplete beta function.
374 (defvar math-current-beta-value nil)
375 (defun calcFunc-betaI (x a b)
376 (cond ((math-zerop x)
377 '(float 0 0))
378 ((math-equal-int x 1)
379 '(float 1 0))
380 ((or (math-zerop a)
381 (and (math-num-integerp a)
382 (math-negp a)))
383 (if (or (math-zerop b)
384 (and (math-num-integerp b)
385 (math-negp b)))
386 (math-reject-arg b 'range)
387 '(float 1 0)))
388 ((or (math-zerop b)
389 (and (math-num-integerp b)
390 (math-negp b)))
391 '(float 0 0))
392 ((not (math-numberp a)) (math-reject-arg a 'numberp))
393 ((not (math-numberp b)) (math-reject-arg b 'numberp))
394 ((math-inexact-result))
395 (t (let ((math-current-beta-value (calcFunc-beta a b)))
396 (math-div (calcFunc-betaB x a b) math-current-beta-value)))))
398 (defun calcFunc-betaB (x a b)
399 (cond
400 ((math-zerop x)
401 '(float 0 0))
402 ((math-equal-int x 1)
403 (calcFunc-beta a b))
404 ((not (math-numberp x)) (math-reject-arg x 'numberp))
405 ((not (math-numberp a)) (math-reject-arg a 'numberp))
406 ((not (math-numberp b)) (math-reject-arg b 'numberp))
407 ((math-zerop a) (math-reject-arg a 'nonzerop))
408 ((math-zerop b) (math-reject-arg b 'nonzerop))
409 ((and (math-num-integerp b)
410 (if (math-negp b)
411 (math-reject-arg b 'range)
412 (Math-natnum-lessp (setq b (math-trunc b)) 20)))
413 (and calc-symbolic-mode (or (math-floatp a) (math-floatp b))
414 (math-inexact-result))
415 (math-mul
416 (math-with-extra-prec 2
417 (let* ((i 0)
418 (term 1)
419 (sum (math-div term a)))
420 (while (< (setq i (1+ i)) b)
421 (setq term (math-mul (math-div (math-mul term (- i b)) i) x)
422 sum (math-add sum (math-div term (math-add a i))))
423 (math-working "beta" sum))
424 sum))
425 (math-pow x a)))
426 ((and (math-num-integerp a)
427 (if (math-negp a)
428 (math-reject-arg a 'range)
429 (Math-natnum-lessp (setq a (math-trunc a)) 20)))
430 (math-sub (or math-current-beta-value (calcFunc-beta a b))
431 (calcFunc-betaB (math-sub 1 x) b a)))
433 (math-inexact-result)
434 (math-with-extra-prec 2
435 (setq x (math-float x))
436 (setq a (math-float a))
437 (setq b (math-float b))
438 (let ((bt (math-exp-raw (math-add (math-mul a (math-ln-raw x))
439 (math-mul b (math-ln-raw
440 (math-sub '(float 1 0)
441 x)))))))
442 (if (Math-lessp x (math-div (math-add a '(float 1 0))
443 (math-add (math-add a b) '(float 2 0))))
444 (math-div (math-mul bt (math-beta-cfrac a b x)) a)
445 (math-sub (or math-current-beta-value (calcFunc-beta a b))
446 (math-div (math-mul bt
447 (math-beta-cfrac b a (math-sub 1 x)))
448 b))))))))
450 (defun math-beta-cfrac (a b x)
451 (let ((qab (math-add a b))
452 (qap (math-add a '(float 1 0)))
453 (qam (math-add a '(float -1 0))))
454 (math-beta-cfrac-step '(float 1 0)
455 (math-sub '(float 1 0)
456 (math-div (math-mul qab x) qap))
457 '(float 1 0) '(float 1 0)
458 '(float 1 0)
459 qab qap qam a b x)))
461 (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x)
462 (let* ((two-m (math-mul m '(float 2 0)))
463 (d (math-div (math-mul (math-mul (math-sub b m) m) x)
464 (math-mul (math-add qam two-m) (math-add a two-m))))
465 (ap (math-add az (math-mul d am)))
466 (bp (math-add bz (math-mul d bm)))
467 (d2 (math-neg
468 (math-div (math-mul (math-mul (math-add a m) (math-add qab m)) x)
469 (math-mul (math-add qap two-m) (math-add a two-m)))))
470 (app (math-add ap (math-mul d2 az)))
471 (bpp (math-add bp (math-mul d2 bz)))
472 (next (math-div app bpp)))
473 (math-working "beta" next)
474 (if (math-nearly-equal next az)
475 next
476 (math-beta-cfrac-step next '(float 1 0)
477 (math-div ap bpp) (math-div bp bpp)
478 (math-add m '(float 1 0))
479 qab qap qam a b x))))
482 ;;; Bessel functions.
484 ;;; Should generalize this to handle arbitrary precision!
486 (defun calcFunc-besJ (v x)
487 (or (math-numberp v) (math-reject-arg v 'numberp))
488 (or (math-numberp x) (math-reject-arg x 'numberp))
489 (let ((calc-internal-prec (min 8 calc-internal-prec)))
490 (math-with-extra-prec 3
491 (setq x (math-float (math-normalize x)))
492 (setq v (math-float (math-normalize v)))
493 (cond ((math-zerop x)
494 (if (math-zerop v)
495 '(float 1 0)
496 '(float 0 0)))
497 ((math-inexact-result))
498 ((not (math-num-integerp v))
499 (let ((start (math-div 1 (calcFunc-fact v))))
500 (math-mul (math-besJ-series start start
502 (math-mul '(float -25 -2)
503 (math-sqr x))
505 (math-pow (math-div x 2) v))))
506 ((math-negp (setq v (math-trunc v)))
507 (if (math-oddp v)
508 (math-neg (calcFunc-besJ (math-neg v) x))
509 (calcFunc-besJ (math-neg v) x)))
510 ((eq v 0)
511 (math-besJ0 x))
512 ((eq v 1)
513 (math-besJ1 x))
514 ((Math-lessp v (math-abs-approx x))
515 (let ((j 0)
516 (bjm (math-besJ0 x))
517 (bj (math-besJ1 x))
518 (two-over-x (math-div 2 x))
519 bjp)
520 (while (< (setq j (1+ j)) v)
521 (setq bjp (math-sub (math-mul (math-mul j two-over-x) bj)
522 bjm)
523 bjm bj
524 bj bjp))
525 bj))
527 (if (Math-lessp 100 v) (math-reject-arg v 'range))
528 (let* ((j (logior (+ v (math-isqrt-small (* 40 v))) 1))
529 (two-over-x (math-div 2 x))
530 (jsum nil)
531 (bjp '(float 0 0))
532 (sum '(float 0 0))
533 (bj '(float 1 0))
534 bjm ans)
535 (while (> (setq j (1- j)) 0)
536 (setq bjm (math-sub (math-mul (math-mul j two-over-x) bj)
537 bjp)
538 bjp bj
539 bj bjm)
540 (if (> (nth 2 (math-abs-approx bj)) 10)
541 (setq bj (math-mul bj '(float 1 -10))
542 bjp (math-mul bjp '(float 1 -10))
543 ans (and ans (math-mul ans '(float 1 -10)))
544 sum (math-mul sum '(float 1 -10))))
545 (or (setq jsum (not jsum))
546 (setq sum (math-add sum bj)))
547 (if (= j v)
548 (setq ans bjp)))
549 (math-div ans (math-sub (math-mul 2 sum) bj))))))))
551 (defun math-besJ-series (sum term k zz vk)
552 (math-working "besJ" sum)
553 (setq k (1+ k)
554 vk (math-add 1 vk)
555 term (math-div (math-mul term zz) (math-mul k vk)))
556 (let ((next (math-add sum term)))
557 (if (math-nearly-equal next sum)
558 next
559 (math-besJ-series next term k zz vk))))
561 (defun math-besJ0 (x &optional yflag)
562 (cond ((and (not yflag) (math-negp (calcFunc-re x)))
563 (math-besJ0 (math-neg x)))
564 ((Math-lessp '(float 8 0) (math-abs-approx x))
565 (let* ((z (math-div '(float 8 0) x))
566 (y (math-sqr z))
567 (xx (math-add x
568 (math-read-number-simple "-0.785398164")))
569 (a1 (math-poly-eval y
570 (list
571 (math-read-number-simple "0.0000002093887211")
572 (math-read-number-simple "-0.000002073370639")
573 (math-read-number-simple "0.00002734510407")
574 (math-read-number-simple "-0.001098628627")
575 '(float 1 0))))
576 (a2 (math-poly-eval y
577 (list
578 (math-read-number-simple "-0.0000000934935152")
579 (math-read-number-simple "0.0000007621095161")
580 (math-read-number-simple "-0.000006911147651")
581 (math-read-number-simple "0.0001430488765")
582 (math-read-number-simple "-0.01562499995"))))
583 (sc (math-sin-cos-raw xx)))
584 (if yflag
585 (setq sc (cons (math-neg (cdr sc)) (car sc))))
586 (math-mul (math-sqrt
587 (math-div (math-read-number-simple "0.636619722")
589 (math-sub (math-mul (cdr sc) a1)
590 (math-mul (car sc) (math-mul z a2))))))
592 (let ((y (math-sqr x)))
593 (math-div (math-poly-eval y
594 (list
595 (math-read-number-simple "-184.9052456")
596 (math-read-number-simple "77392.33017")
597 (math-read-number-simple "-11214424.18")
598 (math-read-number-simple "651619640.7")
599 (math-read-number-simple "-13362590354.0")
600 (math-read-number-simple "57568490574.0")))
601 (math-poly-eval y
602 (list
603 '(float 1 0)
604 (math-read-number-simple "267.8532712")
605 (math-read-number-simple "59272.64853")
606 (math-read-number-simple "9494680.718")
607 (math-read-number-simple "1029532985.0")
608 (math-read-number-simple "57568490411.0"))))))))
610 (defun math-besJ1 (x &optional yflag)
611 (cond ((and (math-negp (calcFunc-re x)) (not yflag))
612 (math-neg (math-besJ1 (math-neg x))))
613 ((Math-lessp '(float 8 0) (math-abs-approx x))
614 (let* ((z (math-div '(float 8 0) x))
615 (y (math-sqr z))
616 (xx (math-add x (math-read-number-simple "-2.356194491")))
617 (a1 (math-poly-eval y
618 (list
619 (math-read-number-simple "-0.000000240337019")
620 (math-read-number-simple "0.000002457520174")
621 (math-read-number-simple "-0.00003516396496")
622 '(float 183105 -8)
623 '(float 1 0))))
624 (a2 (math-poly-eval y
625 (list
626 (math-read-number-simple "0.000000105787412")
627 (math-read-number-simple "-0.00000088228987")
628 (math-read-number-simple "0.000008449199096")
629 (math-read-number-simple "-0.0002002690873")
630 (math-read-number-simple "0.04687499995"))))
631 (sc (math-sin-cos-raw xx)))
632 (if yflag
633 (setq sc (cons (math-neg (cdr sc)) (car sc)))
634 (if (math-negp x)
635 (setq sc (cons (math-neg (car sc)) (math-neg (cdr sc))))))
636 (math-mul (math-sqrt (math-div
637 (math-read-number-simple "0.636619722")
639 (math-sub (math-mul (cdr sc) a1)
640 (math-mul (car sc) (math-mul z a2))))))
642 (let ((y (math-sqr x)))
643 (math-mul
645 (math-div (math-poly-eval y
646 (list
647 (math-read-number-simple "-30.16036606")
648 (math-read-number-simple "15704.4826")
649 (math-read-number-simple "-2972611.439")
650 (math-read-number-simple "242396853.1")
651 (math-read-number-simple "-7895059235.0")
652 (math-read-number-simple "72362614232.0")))
653 (math-poly-eval y
654 (list
655 '(float 1 0)
656 (math-read-number-simple "376.9991397")
657 (math-read-number-simple "99447.43394")
658 (math-read-number-simple "18583304.74")
659 (math-read-number-simple "2300535178.0")
660 (math-read-number-simple "144725228442.0")))))))))
662 (defun calcFunc-besY (v x)
663 (math-inexact-result)
664 (or (math-numberp v) (math-reject-arg v 'numberp))
665 (or (math-numberp x) (math-reject-arg x 'numberp))
666 (let ((calc-internal-prec (min 8 calc-internal-prec)))
667 (math-with-extra-prec 3
668 (setq x (math-float (math-normalize x)))
669 (setq v (math-float (math-normalize v)))
670 (cond ((not (math-num-integerp v))
671 (let ((sc (math-sin-cos-raw (math-mul v (math-pi)))))
672 (math-div (math-sub (math-mul (calcFunc-besJ v x) (cdr sc))
673 (calcFunc-besJ (math-neg v) x))
674 (car sc))))
675 ((math-negp (setq v (math-trunc v)))
676 (if (math-oddp v)
677 (math-neg (calcFunc-besY (math-neg v) x))
678 (calcFunc-besY (math-neg v) x)))
679 ((eq v 0)
680 (math-besY0 x))
681 ((eq v 1)
682 (math-besY1 x))
684 (let ((j 0)
685 (bym (math-besY0 x))
686 (by (math-besY1 x))
687 (two-over-x (math-div 2 x))
688 byp)
689 (while (< (setq j (1+ j)) v)
690 (setq byp (math-sub (math-mul (math-mul j two-over-x) by)
691 bym)
692 bym by
693 by byp))
694 by))))))
696 (defun math-besY0 (x)
697 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
698 (let ((y (math-sqr x)))
699 (math-add
700 (math-div (math-poly-eval y
701 (list
702 (math-read-number-simple "228.4622733")
703 (math-read-number-simple "-86327.92757")
704 (math-read-number-simple "10879881.29")
705 (math-read-number-simple "-512359803.6")
706 (math-read-number-simple "7062834065.0")
707 (math-read-number-simple "-2957821389.0")))
708 (math-poly-eval y
709 (list
710 '(float 1 0)
711 (math-read-number-simple "226.1030244")
712 (math-read-number-simple "47447.2647")
713 (math-read-number-simple "7189466.438")
714 (math-read-number-simple "745249964.8")
715 (math-read-number-simple "40076544269.0"))))
716 (math-mul (math-read-number-simple "0.636619772")
717 (math-mul (math-besJ0 x) (math-ln-raw x))))))
718 ((math-negp (calcFunc-re x))
719 (math-add (math-besJ0 (math-neg x) t)
720 (math-mul '(cplx 0 2)
721 (math-besJ0 (math-neg x)))))
723 (math-besJ0 x t))))
725 (defun math-besY1 (x)
726 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
727 (let ((y (math-sqr x)))
728 (math-add
729 (math-mul
731 (math-div (math-poly-eval y
732 (list
733 (math-read-number-simple "8511.937935")
734 (math-read-number-simple "-4237922.726")
735 (math-read-number-simple "734926455.1")
736 (math-read-number-simple "-51534381390.0")
737 (math-read-number-simple "1275274390000.0")
738 (math-read-number-simple "-4900604943000.0")))
739 (math-poly-eval y
740 (list
741 '(float 1 0)
742 (math-read-number-simple "354.9632885")
743 (math-read-number-simple "102042.605")
744 (math-read-number-simple "22459040.02")
745 (math-read-number-simple "3733650367.0")
746 (math-read-number-simple "424441966400.0")
747 (math-read-number-simple "24995805700000.0")))))
748 (math-mul (math-read-number-simple "0.636619772")
749 (math-sub (math-mul (math-besJ1 x) (math-ln-raw x))
750 (math-div 1 x))))))
751 ((math-negp (calcFunc-re x))
752 (math-neg
753 (math-add (math-besJ1 (math-neg x) t)
754 (math-mul '(cplx 0 2)
755 (math-besJ1 (math-neg x))))))
757 (math-besJ1 x t))))
759 (defun math-poly-eval (x coefs)
760 (let ((accum (car coefs)))
761 (while (setq coefs (cdr coefs))
762 (setq accum (math-add (car coefs) (math-mul accum x))))
763 accum))
766 ;;;; Bernoulli and Euler polynomials and numbers.
768 (defun calcFunc-bern (n &optional x)
769 (if (and x (not (math-zerop x)))
770 (if (and calc-symbolic-mode (math-floatp x))
771 (math-inexact-result)
772 (math-build-polynomial-expr (math-bernoulli-coefs n) x))
773 (or (math-num-natnump n) (math-reject-arg n 'natnump))
774 (if (consp n)
775 (progn
776 (math-inexact-result)
777 (math-float (math-bernoulli-number (math-trunc n))))
778 (math-bernoulli-number n))))
780 (defun calcFunc-euler (n &optional x)
781 (or (math-num-natnump n) (math-reject-arg n 'natnump))
782 (if x
783 (let* ((n1 (math-add n 1))
784 (coefs (math-bernoulli-coefs n1))
785 (fac (math-div (math-pow 2 n1) n1))
786 (k -1)
787 (x1 (math-div (math-add x 1) 2))
788 (x2 (math-div x 2)))
789 (if (math-numberp x)
790 (if (and calc-symbolic-mode (math-floatp x))
791 (math-inexact-result)
792 (math-mul fac
793 (math-sub (math-build-polynomial-expr coefs x1)
794 (math-build-polynomial-expr coefs x2))))
795 (calcFunc-collect
796 (math-reduce-vec
797 'math-add
798 (cons 'vec
799 (mapcar (function
800 (lambda (c)
801 (setq k (1+ k))
802 (math-mul (math-mul fac c)
803 (math-sub (math-pow x1 k)
804 (math-pow x2 k)))))
805 coefs)))
806 x)))
807 (math-mul (math-pow 2 n)
808 (if (consp n)
809 (progn
810 (math-inexact-result)
811 (calcFunc-euler n '(float 5 -1)))
812 (calcFunc-euler n '(frac 1 2))))))
814 (defvar math-bernoulli-b-cache
815 (list
816 (list 'frac
817 -174611
818 (math-read-number-simple "802857662698291200000"))
819 (list 'frac
820 43867
821 (math-read-number-simple "5109094217170944000"))
822 (list 'frac
823 -3617
824 (math-read-number-simple "10670622842880000"))
825 (list 'frac
827 (math-read-number-simple "74724249600"))
828 (list 'frac
829 -691
830 (math-read-number-simple "1307674368000"))
831 (list 'frac
833 (math-read-number-simple "47900160"))
834 (list 'frac
836 (math-read-number-simple "1209600"))
837 (list 'frac
839 30240)
840 (list 'frac
842 720)
843 (list 'frac
845 12)
846 1 ))
848 (defvar math-bernoulli-B-cache
849 '((frac -174611 330) (frac 43867 798)
850 (frac -3617 510) (frac 7 6) (frac -691 2730)
851 (frac 5 66) (frac -1 30) (frac 1 42)
852 (frac -1 30) (frac 1 6) 1 ))
854 (defvar math-bernoulli-cache-size 11)
855 (defun math-bernoulli-coefs (n)
856 (let* ((coefs (list (calcFunc-bern n)))
857 (nn (math-trunc n))
858 (k nn)
859 (term nn)
860 coef
861 (calc-prefer-frac (or (integerp n) calc-prefer-frac)))
862 (while (>= (setq k (1- k)) 0)
863 (setq term (math-div term (- nn k))
864 coef (math-mul term (math-bernoulli-number k))
865 coefs (cons (if (consp n) (math-float coef) coef) coefs)
866 term (math-mul term k)))
867 (nreverse coefs)))
869 (defun math-bernoulli-number (n)
870 (if (= (% n 2) 1)
871 (if (= n 1)
872 '(frac -1 2)
874 (setq n (/ n 2))
875 (while (>= n math-bernoulli-cache-size)
876 (let* ((sum 0)
877 (nk 1) ; nk = n-k+1
878 (fact 1) ; fact = (n-k+1)!
879 ofact
880 (p math-bernoulli-b-cache)
881 (calc-prefer-frac t))
882 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size))
883 (while p
884 (setq nk (+ nk 2)
885 ofact fact
886 fact (math-mul fact (* nk (1- nk)))
887 sum (math-add sum (math-div (car p) fact))
888 p (cdr p)))
889 (setq ofact (math-mul ofact (1- nk))
890 sum (math-sub (math-div '(frac 1 2) ofact) sum)
891 math-bernoulli-b-cache (cons sum math-bernoulli-b-cache)
892 math-bernoulli-B-cache (cons (math-mul sum ofact)
893 math-bernoulli-B-cache)
894 math-bernoulli-cache-size (1+ math-bernoulli-cache-size))))
895 (nth (- math-bernoulli-cache-size n 1) math-bernoulli-B-cache)))
897 ;;; Bn = n! bn
898 ;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
900 ;;; A faster method would be to use "tangent numbers", c.f., Concrete
901 ;;; Mathematics pg. 273.
904 ;;; Probability distributions.
906 ;;; Binomial.
907 (defun calcFunc-utpb (x n p)
908 (if math-expand-formulas
909 (math-normalize (list 'calcFunc-betaI p x (list '+ (list '- n x) 1)))
910 (calcFunc-betaI p x (math-add (math-sub n x) 1))))
911 (put 'calcFunc-utpb 'math-expandable t)
913 (defun calcFunc-ltpb (x n p)
914 (math-sub 1 (calcFunc-utpb x n p)))
915 (put 'calcFunc-ltpb 'math-expandable t)
917 ;;; Chi-square.
918 (defun calcFunc-utpc (chisq v)
919 (if math-expand-formulas
920 (math-normalize (list 'calcFunc-gammaQ (list '/ v 2) (list '/ chisq 2)))
921 (calcFunc-gammaQ (math-div v 2) (math-div chisq 2))))
922 (put 'calcFunc-utpc 'math-expandable t)
924 (defun calcFunc-ltpc (chisq v)
925 (if math-expand-formulas
926 (math-normalize (list 'calcFunc-gammaP (list '/ v 2) (list '/ chisq 2)))
927 (calcFunc-gammaP (math-div v 2) (math-div chisq 2))))
928 (put 'calcFunc-ltpc 'math-expandable t)
930 ;;; F-distribution.
931 (defun calcFunc-utpf (f v1 v2)
932 (if math-expand-formulas
933 (math-normalize (list 'calcFunc-betaI
934 (list '/ v2 (list '+ v2 (list '* v1 f)))
935 (list '/ v2 2)
936 (list '/ v1 2)))
937 (calcFunc-betaI (math-div v2 (math-add v2 (math-mul v1 f)))
938 (math-div v2 2)
939 (math-div v1 2))))
940 (put 'calcFunc-utpf 'math-expandable t)
942 (defun calcFunc-ltpf (f v1 v2)
943 (math-sub 1 (calcFunc-utpf f v1 v2)))
944 (put 'calcFunc-ltpf 'math-expandable t)
946 ;;; Normal.
947 (defun calcFunc-utpn (x mean sdev)
948 (if math-expand-formulas
949 (math-normalize
950 (list '/
951 (list '+ 1
952 (list 'calcFunc-erf
953 (list '/ (list '- mean x)
954 (list '* sdev (list 'calcFunc-sqrt 2)))))
956 (math-mul (math-add '(float 1 0)
957 (calcFunc-erf
958 (math-div (math-sub mean x)
959 (math-mul sdev (math-sqrt-2)))))
960 '(float 5 -1))))
961 (put 'calcFunc-utpn 'math-expandable t)
963 (defun calcFunc-ltpn (x mean sdev)
964 (if math-expand-formulas
965 (math-normalize
966 (list '/
967 (list '+ 1
968 (list 'calcFunc-erf
969 (list '/ (list '- x mean)
970 (list '* sdev (list 'calcFunc-sqrt 2)))))
972 (math-mul (math-add '(float 1 0)
973 (calcFunc-erf
974 (math-div (math-sub x mean)
975 (math-mul sdev (math-sqrt-2)))))
976 '(float 5 -1))))
977 (put 'calcFunc-ltpn 'math-expandable t)
979 ;;; Poisson.
980 (defun calcFunc-utpp (n x)
981 (if math-expand-formulas
982 (math-normalize (list 'calcFunc-gammaP x n))
983 (calcFunc-gammaP x n)))
984 (put 'calcFunc-utpp 'math-expandable t)
986 (defun calcFunc-ltpp (n x)
987 (if math-expand-formulas
988 (math-normalize (list 'calcFunc-gammaQ x n))
989 (calcFunc-gammaQ x n)))
990 (put 'calcFunc-ltpp 'math-expandable t)
992 ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
993 (defun calcFunc-utpt (tt v)
994 (if math-expand-formulas
995 (math-normalize (list 'calcFunc-betaI
996 (list '/ v (list '+ v (list '^ tt 2)))
997 (list '/ v 2)
998 '(float 5 -1)))
999 (calcFunc-betaI (math-div v (math-add v (math-sqr tt)))
1000 (math-div v 2)
1001 '(float 5 -1))))
1002 (put 'calcFunc-utpt 'math-expandable t)
1004 (defun calcFunc-ltpt (tt v)
1005 (math-sub 1 (calcFunc-utpt tt v)))
1006 (put 'calcFunc-ltpt 'math-expandable t)
1008 (provide 'calc-funcs)
1010 ;;; calc-funcs.el ends here