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/>.
26 ;; This file is autoloaded from calc-ext.el.
31 (defun calc-inc-gamma (arg)
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
)))))
46 (calc-unary-op "erfc" 'calcFunc-erfc arg
)
47 (calc-unary-op "erf" 'calcFunc-erf arg
))))
49 (defun calc-erfc (arg)
54 (defun calc-beta (arg)
57 (calc-binary-op "beta" 'calcFunc-beta arg
)))
59 (defun calc-inc-beta ()
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)
69 (calc-binary-op "besJ" 'calcFunc-besJ arg
)))
71 (defun calc-bessel-Y (arg)
74 (calc-binary-op "besY" 'calcFunc-besY arg
)))
76 (defun calc-bernoulli-number (arg)
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)
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)
93 (if (calc-is-hyperbolic)
94 (calc-binary-op "str2" 'calcFunc-stir2 arg
)
95 (calc-binary-op "str1" 'calcFunc-stir1 arg
))))
99 (calc-prob-dist "b" 3))
103 (calc-prob-dist "c" 2))
107 (calc-prob-dist "f" 3))
111 (calc-prob-dist "n" 3))
115 (calc-prob-dist "p" 2))
119 (calc-prob-dist "t" 2))
121 (defun calc-prob-dist (letter nargs
)
123 (if (calc-is-inverse)
124 (calc-enter-result nargs
(concat "ltp" letter
)
125 (append (list (intern (concat "calcFunc-ltp" letter
))
127 (calc-top-list-n (1- nargs
) 2)))
128 (calc-enter-result nargs
(concat "utp" letter
)
129 (append (list (intern (concat "calcFunc-utp" letter
))
131 (calc-top-list-n (1- nargs
) 2))))))
136 ;;; Sources: Numerical Recipes, Press et al;
137 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
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."
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
)
155 (math-mul (math-gammap1-raw
156 (math-add (math-neg x
)
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
))))
164 (math-lessp-float '(float 736276 0) x
))
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)
172 (math-sub (math-mul (math-add x
'(float 5 -
1))
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
)
187 (next (math-add sum term
)))
188 (if (math-nearly-equal sum next
)
190 (if (> n
(* 2 calc-internal-prec
))
192 ;; Need this because series eventually diverges for large enough n.
194 "*Gamma computation stopped early, not all digits may be valid")
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
))
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
)))
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
))
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
)))
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
))
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
)
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
))
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
)
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
)
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
)
285 (math-inc-gamma-series-step next term a x
))))
287 (defun math-inc-gamma-cfrac (a 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)
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
)))
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
)
311 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x
))))))
316 (defun calcFunc-erf (x)
317 (if (equal x
'(var inf var-inf
))
319 (if (equal x
'(neg (var inf var-inf
)))
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
)
330 (defun calcFunc-erfc (x)
331 (if (equal x
'(var inf var-inf
))
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
)
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
))
353 (if (eq (car-safe x
) 'cplx
)
354 (list 'cplx
(math-neg (nth 1 x
)) (nth 2 x
))
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
)
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
)
378 ((math-equal-int x
1)
381 (and (math-num-integerp a
)
383 (if (or (math-zerop b
)
384 (and (math-num-integerp b
)
386 (math-reject-arg b
'range
)
389 (and (math-num-integerp b
)
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
)
402 ((math-equal-int x
1)
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
)
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))
416 (math-with-extra-prec 2
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
))
426 ((and (math-num-integerp 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)
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
)))
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)
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
)))
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
)
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
)
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)
505 (math-pow (math-div x
2) v
))))
506 ((math-negp (setq v
(math-trunc v
)))
508 (math-neg (calcFunc-besJ (math-neg v
) x
))
509 (calcFunc-besJ (math-neg v
) x
)))
514 ((Math-lessp v
(math-abs-approx x
))
518 (two-over-x (math-div 2 x
))
520 (while (< (setq j
(1+ j
)) v
)
521 (setq bjp
(math-sub (math-mul (math-mul j two-over-x
) 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
))
535 (while (> (setq j
(1- j
)) 0)
536 (setq bjm
(math-sub (math-mul (math-mul j two-over-x
) bj
)
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
)))
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
)
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
)
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
))
568 (math-read-number-simple "-0.785398164")))
569 (a1 (math-poly-eval y
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")
576 (a2 (math-poly-eval y
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
)))
585 (setq sc
(cons (math-neg (cdr sc
)) (car sc
))))
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
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")))
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
))
616 (xx (math-add x
(math-read-number-simple "-2.356194491")))
617 (a1 (math-poly-eval y
619 (math-read-number-simple "-0.000000240337019")
620 (math-read-number-simple "0.000002457520174")
621 (math-read-number-simple "-0.00003516396496")
624 (a2 (math-poly-eval y
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
)))
633 (setq sc
(cons (math-neg (cdr sc
)) (car sc
)))
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
)))
645 (math-div (math-poly-eval y
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")))
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
))
675 ((math-negp (setq v
(math-trunc v
)))
677 (math-neg (calcFunc-besY (math-neg v
) x
))
678 (calcFunc-besY (math-neg v
) x
)))
687 (two-over-x (math-div 2 x
))
689 (while (< (setq j
(1+ j
)) v
)
690 (setq byp
(math-sub (math-mul (math-mul j two-over-x
) by
)
696 (defun math-besY0 (x)
697 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
698 (let ((y (math-sqr x
)))
700 (math-div (math-poly-eval y
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")))
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
)))))
725 (defun math-besY1 (x)
726 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
727 (let ((y (math-sqr x
)))
731 (math-div (math-poly-eval y
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")))
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
))
751 ((math-negp (calcFunc-re x
))
753 (math-add (math-besJ1 (math-neg x
) t
)
754 (math-mul '(cplx 0 2)
755 (math-besJ1 (math-neg x
))))))
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
))))
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
))
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
))
783 (let* ((n1 (math-add n
1))
784 (coefs (math-bernoulli-coefs n1
))
785 (fac (math-div (math-pow 2 n1
) n1
))
787 (x1 (math-div (math-add x
1) 2))
790 (if (and calc-symbolic-mode
(math-floatp x
))
791 (math-inexact-result)
793 (math-sub (math-build-polynomial-expr coefs x1
)
794 (math-build-polynomial-expr coefs x2
))))
802 (math-mul (math-mul fac c
)
803 (math-sub (math-pow x1 k
)
807 (math-mul (math-pow 2 n
)
810 (math-inexact-result)
811 (calcFunc-euler n
'(float 5 -
1)))
812 (calcFunc-euler n
'(frac 1 2))))))
814 (defvar math-bernoulli-b-cache
818 (math-read-number-simple "802857662698291200000"))
821 (math-read-number-simple "5109094217170944000"))
824 (math-read-number-simple "10670622842880000"))
827 (math-read-number-simple "74724249600"))
830 (math-read-number-simple "1307674368000"))
833 (math-read-number-simple "47900160"))
836 (math-read-number-simple "1209600"))
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
)))
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
)))
869 (defun math-bernoulli-number (n)
875 (while (>= n math-bernoulli-cache-size
)
878 (fact 1) ; fact = (n-k+1)!
880 (p math-bernoulli-b-cache
)
881 (calc-prefer-frac t
))
882 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size
))
886 fact
(math-mul fact
(* nk
(1- nk
)))
887 sum
(math-add sum
(math-div (car p
) fact
))
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
)))
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.
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
)
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
)
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
)))
937 (calcFunc-betaI (math-div v2
(math-add v2
(math-mul v1 f
)))
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
)
947 (defun calcFunc-utpn (x mean sdev
)
948 (if math-expand-formulas
953 (list '/ (list '- mean x
)
954 (list '* sdev
(list 'calcFunc-sqrt
2)))))
956 (math-mul (math-add '(float 1 0)
958 (math-div (math-sub mean x
)
959 (math-mul sdev
(math-sqrt-2)))))
961 (put 'calcFunc-utpn
'math-expandable t
)
963 (defun calcFunc-ltpn (x mean sdev
)
964 (if math-expand-formulas
969 (list '/ (list '- x mean
)
970 (list '* sdev
(list 'calcFunc-sqrt
2)))))
972 (math-mul (math-add '(float 1 0)
974 (math-div (math-sub x mean
)
975 (math-mul sdev
(math-sqrt-2)))))
977 (put 'calcFunc-ltpn
'math-expandable t
)
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)))
999 (calcFunc-betaI (math-div v
(math-add v
(math-sqr tt
)))
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