1 ;;; calc-funcs.el --- well-known functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file is autoloaded from calc-ext.el.
32 (defun calc-inc-gamma (arg)
36 (if (calc-is-hyperbolic)
37 (calc-binary-op "gamG" 'calcFunc-gammaG arg
)
38 (calc-binary-op "gamQ" 'calcFunc-gammaQ arg
))
39 (if (calc-is-hyperbolic)
40 (calc-binary-op "gamg" 'calcFunc-gammag arg
)
41 (calc-binary-op "gamP" 'calcFunc-gammaP arg
)))))
47 (calc-unary-op "erfc" 'calcFunc-erfc arg
)
48 (calc-unary-op "erf" 'calcFunc-erf arg
))))
50 (defun calc-erfc (arg)
55 (defun calc-beta (arg)
58 (calc-binary-op "beta" 'calcFunc-beta arg
)))
60 (defun calc-inc-beta ()
63 (if (calc-is-hyperbolic)
64 (calc-enter-result 3 "betB" (cons 'calcFunc-betaB
(calc-top-list-n 3)))
65 (calc-enter-result 3 "betI" (cons 'calcFunc-betaI
(calc-top-list-n 3))))))
67 (defun calc-bessel-J (arg)
70 (calc-binary-op "besJ" 'calcFunc-besJ arg
)))
72 (defun calc-bessel-Y (arg)
75 (calc-binary-op "besY" 'calcFunc-besY arg
)))
77 (defun calc-bernoulli-number (arg)
80 (if (calc-is-hyperbolic)
81 (calc-binary-op "bern" 'calcFunc-bern arg
)
82 (calc-unary-op "bern" 'calcFunc-bern arg
))))
84 (defun calc-euler-number (arg)
87 (if (calc-is-hyperbolic)
88 (calc-binary-op "eulr" 'calcFunc-euler arg
)
89 (calc-unary-op "eulr" 'calcFunc-euler arg
))))
91 (defun calc-stirling-number (arg)
94 (if (calc-is-hyperbolic)
95 (calc-binary-op "str2" 'calcFunc-stir2 arg
)
96 (calc-binary-op "str1" 'calcFunc-stir1 arg
))))
100 (calc-prob-dist "b" 3))
104 (calc-prob-dist "c" 2))
108 (calc-prob-dist "f" 3))
112 (calc-prob-dist "n" 3))
116 (calc-prob-dist "p" 2))
120 (calc-prob-dist "t" 2))
122 (defun calc-prob-dist (letter nargs
)
124 (if (calc-is-inverse)
125 (calc-enter-result nargs
(concat "ltp" letter
)
126 (append (list (intern (concat "calcFunc-ltp" letter
))
128 (calc-top-list-n (1- nargs
) 2)))
129 (calc-enter-result nargs
(concat "utp" letter
)
130 (append (list (intern (concat "calcFunc-utp" letter
))
132 (calc-top-list-n (1- nargs
) 2))))))
137 ;;; Sources: Numerical Recipes, Press et al;
138 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
143 (defun calcFunc-gamma (x)
144 (or (math-numberp x
) (math-reject-arg x
'numberp
))
145 (calcFunc-fact (math-add x -
1)))
147 (defun math-gammap1-raw (x &optional fprec nfprec
)
148 "Compute gamma(1+X) to the appropriate precision."
150 (setq fprec
(math-float calc-internal-prec
)
151 nfprec
(math-float (- calc-internal-prec
))))
152 (cond ((math-lessp-float (calcFunc-re x
) fprec
)
153 (if (math-lessp-float (calcFunc-re x
) nfprec
)
156 (math-mul (math-gammap1-raw
157 (math-add (math-neg x
)
161 (math-mul (math-pi) x
)))))
162 (let ((xplus1 (math-add x
'(float 1 0))))
163 (math-div (math-gammap1-raw xplus1 fprec nfprec
) xplus1
))))
165 (math-lessp-float '(float 736276 0) x
))
167 (t ; re(x) now >= 10.0
168 (let ((xinv (math-div 1 x
))
169 (lnx (math-ln-raw x
)))
170 (math-mul (math-sqrt-two-pi)
173 (math-sub (math-mul (math-add x
'(float 5 -
1))
181 (defun math-gamma-series (sum x xinvsqr oterm n
)
182 (math-working "gamma" sum
)
183 (let* ((bn (math-bernoulli-number n
))
184 (term (math-mul (math-div-float (math-float (nth 1 bn
))
185 (math-float (* (nth 2 bn
)
188 (next (math-add sum term
)))
189 (if (math-nearly-equal sum next
)
191 (if (> n
(* 2 calc-internal-prec
))
193 ;; Need this because series eventually diverges for large enough n.
195 "*Gamma computation stopped early, not all digits may be valid")
197 (math-gamma-series next
(math-mul x xinvsqr
) xinvsqr term
(+ n
2))))))
200 ;;; Incomplete gamma function.
202 (defvar math-current-gamma-value nil
)
203 (defun calcFunc-gammaP (a x
)
204 (if (equal x
'(var inf var-inf
))
206 (math-inexact-result)
207 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
208 (or (math-numberp x
) (math-reject-arg x
'numberp
))
209 (if (and (math-num-integerp a
)
210 (integerp (setq a
(math-trunc a
)))
212 (math-sub 1 (calcFunc-gammaQ a x
))
213 (let ((math-current-gamma-value (calcFunc-gamma a
)))
214 (math-div (calcFunc-gammag a x
) math-current-gamma-value
)))))
216 (defun calcFunc-gammaQ (a x
)
217 (if (equal x
'(var inf var-inf
))
219 (math-inexact-result)
220 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
221 (or (math-numberp x
) (math-reject-arg x
'numberp
))
222 (if (and (math-num-integerp a
)
223 (integerp (setq a
(math-trunc a
)))
228 (math-with-extra-prec 1
229 (while (< (setq n
(1+ n
)) a
)
230 (setq term
(math-div (math-mul term x
) n
)
231 sum
(math-add sum term
))
232 (math-working "gamma" sum
))
233 (math-mul sum
(calcFunc-exp (math-neg x
)))))
234 (let ((math-current-gamma-value (calcFunc-gamma a
)))
235 (math-div (calcFunc-gammaG a x
) math-current-gamma-value
)))))
237 (defun calcFunc-gammag (a x
)
238 (if (equal x
'(var inf var-inf
))
240 (math-inexact-result)
241 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
242 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
243 (math-with-extra-prec 2
244 (setq a
(math-float a
))
245 (setq x
(math-float x
))
246 (if (or (math-negp (calcFunc-re a
))
247 (math-lessp-float (calcFunc-re x
)
248 (math-add-float (calcFunc-re a
)
250 (math-inc-gamma-series a x
)
251 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
252 (math-inc-gamma-cfrac a x
))))))
254 (defun calcFunc-gammaG (a x
)
255 (if (equal x
'(var inf var-inf
))
257 (math-inexact-result)
258 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
259 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
260 (math-with-extra-prec 2
261 (setq a
(math-float a
))
262 (setq x
(math-float x
))
263 (if (or (math-negp (calcFunc-re a
))
264 (math-lessp-float (calcFunc-re x
)
265 (math-add-float (math-abs-approx a
)
267 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
268 (math-inc-gamma-series a x
))
269 (math-inc-gamma-cfrac a x
)))))
271 (defun math-inc-gamma-series (a x
)
274 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
275 (math-with-extra-prec 2
276 (let ((start (math-div '(float 1 0) a
)))
277 (math-inc-gamma-series-step start start a x
))))))
279 (defun math-inc-gamma-series-step (sum term a x
)
280 (math-working "gamma" sum
)
281 (setq a
(math-add a
'(float 1 0))
282 term
(math-div (math-mul term x
) a
))
283 (let ((next (math-add sum term
)))
284 (if (math-nearly-equal sum next
)
286 (math-inc-gamma-series-step next term a x
))))
288 (defun math-inc-gamma-cfrac (a x
)
290 (or math-current-gamma-value
(calcFunc-gamma a
))
291 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
292 (math-inc-gamma-cfrac-step '(float 1 0) x
293 '(float 0 0) '(float 1 0)
294 '(float 1 0) '(float 1 0) '(float 0 0)
297 (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x
)
298 (let ((ana (math-sub n a
))
299 (anf (math-mul n fac
)))
300 (setq n
(math-add n
'(float 1 0))
301 a0
(math-mul (math-add a1
(math-mul a0 ana
)) fac
)
302 b0
(math-mul (math-add b1
(math-mul b0 ana
)) fac
)
303 a1
(math-add (math-mul x a0
) (math-mul anf a1
))
304 b1
(math-add (math-mul x b0
) (math-mul anf b1
)))
306 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x
)
307 (setq fac
(math-div '(float 1 0) a1
))
308 (let ((next (math-mul b1 fac
)))
309 (math-working "gamma" next
)
310 (if (math-nearly-equal next g
)
312 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x
))))))
317 (defun calcFunc-erf (x)
318 (if (equal x
'(var inf var-inf
))
320 (if (equal x
'(neg (var inf var-inf
)))
324 (let ((math-current-gamma-value (math-sqrt-pi)))
325 (math-to-same-complex-quad
326 (math-div (calcFunc-gammag '(float 5 -
1)
327 (math-sqr (math-to-complex-quad-one x
)))
328 math-current-gamma-value
)
331 (defun calcFunc-erfc (x)
332 (if (equal x
'(var inf var-inf
))
335 (let ((math-current-gamma-value (math-sqrt-pi)))
336 (math-div (calcFunc-gammaG '(float 5 -
1) (math-sqr x
))
337 math-current-gamma-value
))
338 (math-sub 1 (calcFunc-erf x
)))))
340 (defun math-to-complex-quad-one (x)
341 (if (eq (car-safe x
) 'polar
) (setq x
(math-complex x
)))
342 (if (eq (car-safe x
) 'cplx
)
343 (list 'cplx
(math-abs (nth 1 x
)) (math-abs (nth 2 x
)))
346 (defun math-to-same-complex-quad (x y
)
347 (if (eq (car-safe y
) 'cplx
)
348 (if (eq (car-safe x
) 'cplx
)
350 (if (math-negp (nth 1 y
)) (math-neg (nth 1 x
)) (nth 1 x
))
351 (if (math-negp (nth 2 y
)) (math-neg (nth 2 x
)) (nth 2 x
)))
352 (if (math-negp (nth 1 y
)) (math-neg x
) x
))
354 (if (eq (car-safe x
) 'cplx
)
355 (list 'cplx
(math-neg (nth 1 x
)) (nth 2 x
))
362 (defun calcFunc-beta (a b
)
363 (if (math-num-integerp a
)
364 (let ((am (math-add a -
1)))
365 (or (math-numberp b
) (math-reject-arg b
'numberp
))
366 (math-div 1 (math-mul b
(calcFunc-choose (math-add b am
) am
))))
367 (if (math-num-integerp b
)
369 (math-div (math-mul (calcFunc-gamma a
) (calcFunc-gamma b
))
370 (calcFunc-gamma (math-add a b
))))))
373 ;;; Incomplete beta function.
375 (defvar math-current-beta-value nil
)
376 (defun calcFunc-betaI (x a b
)
377 (cond ((math-zerop x
)
379 ((math-equal-int x
1)
382 (and (math-num-integerp a
)
384 (if (or (math-zerop b
)
385 (and (math-num-integerp b
)
387 (math-reject-arg b
'range
)
390 (and (math-num-integerp b
)
393 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
394 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
395 ((math-inexact-result))
396 (t (let ((math-current-beta-value (calcFunc-beta a b
)))
397 (math-div (calcFunc-betaB x a b
) math-current-beta-value
)))))
399 (defun calcFunc-betaB (x a b
)
403 ((math-equal-int x
1)
405 ((not (math-numberp x
)) (math-reject-arg x
'numberp
))
406 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
407 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
408 ((math-zerop a
) (math-reject-arg a
'nonzerop
))
409 ((math-zerop b
) (math-reject-arg b
'nonzerop
))
410 ((and (math-num-integerp b
)
412 (math-reject-arg b
'range
)
413 (Math-natnum-lessp (setq b
(math-trunc b
)) 20)))
414 (and calc-symbolic-mode
(or (math-floatp a
) (math-floatp b
))
415 (math-inexact-result))
417 (math-with-extra-prec 2
420 (sum (math-div term a
)))
421 (while (< (setq i
(1+ i
)) b
)
422 (setq term
(math-mul (math-div (math-mul term
(- i b
)) i
) x
)
423 sum
(math-add sum
(math-div term
(math-add a i
))))
424 (math-working "beta" sum
))
427 ((and (math-num-integerp a
)
429 (math-reject-arg a
'range
)
430 (Math-natnum-lessp (setq a
(math-trunc a
)) 20)))
431 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
432 (calcFunc-betaB (math-sub 1 x
) b a
)))
434 (math-inexact-result)
435 (math-with-extra-prec 2
436 (setq x
(math-float x
))
437 (setq a
(math-float a
))
438 (setq b
(math-float b
))
439 (let ((bt (math-exp-raw (math-add (math-mul a
(math-ln-raw x
))
440 (math-mul b
(math-ln-raw
441 (math-sub '(float 1 0)
443 (if (Math-lessp x
(math-div (math-add a
'(float 1 0))
444 (math-add (math-add a b
) '(float 2 0))))
445 (math-div (math-mul bt
(math-beta-cfrac a b x
)) a
)
446 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
447 (math-div (math-mul bt
448 (math-beta-cfrac b a
(math-sub 1 x
)))
451 (defun math-beta-cfrac (a b x
)
452 (let ((qab (math-add a b
))
453 (qap (math-add a
'(float 1 0)))
454 (qam (math-add a
'(float -
1 0))))
455 (math-beta-cfrac-step '(float 1 0)
456 (math-sub '(float 1 0)
457 (math-div (math-mul qab x
) qap
))
458 '(float 1 0) '(float 1 0)
462 (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x
)
463 (let* ((two-m (math-mul m
'(float 2 0)))
464 (d (math-div (math-mul (math-mul (math-sub b m
) m
) x
)
465 (math-mul (math-add qam two-m
) (math-add a two-m
))))
466 (ap (math-add az
(math-mul d am
)))
467 (bp (math-add bz
(math-mul d bm
)))
469 (math-div (math-mul (math-mul (math-add a m
) (math-add qab m
)) x
)
470 (math-mul (math-add qap two-m
) (math-add a two-m
)))))
471 (app (math-add ap
(math-mul d2 az
)))
472 (bpp (math-add bp
(math-mul d2 bz
)))
473 (next (math-div app bpp
)))
474 (math-working "beta" next
)
475 (if (math-nearly-equal next az
)
477 (math-beta-cfrac-step next
'(float 1 0)
478 (math-div ap bpp
) (math-div bp bpp
)
479 (math-add m
'(float 1 0))
480 qab qap qam a b x
))))
483 ;;; Bessel functions.
485 ;;; Should generalize this to handle arbitrary precision!
487 (defun calcFunc-besJ (v x
)
488 (or (math-numberp v
) (math-reject-arg v
'numberp
))
489 (or (math-numberp x
) (math-reject-arg x
'numberp
))
490 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
491 (math-with-extra-prec 3
492 (setq x
(math-float (math-normalize x
)))
493 (setq v
(math-float (math-normalize v
)))
494 (cond ((math-zerop x
)
498 ((math-inexact-result))
499 ((not (math-num-integerp v
))
500 (let ((start (math-div 1 (calcFunc-fact v
))))
501 (math-mul (math-besJ-series start start
503 (math-mul '(float -
25 -
2)
506 (math-pow (math-div x
2) v
))))
507 ((math-negp (setq v
(math-trunc v
)))
509 (math-neg (calcFunc-besJ (math-neg v
) x
))
510 (calcFunc-besJ (math-neg v
) x
)))
515 ((Math-lessp v
(math-abs-approx x
))
519 (two-over-x (math-div 2 x
))
521 (while (< (setq j
(1+ j
)) v
)
522 (setq bjp
(math-sub (math-mul (math-mul j two-over-x
) bj
)
528 (if (Math-lessp 100 v
) (math-reject-arg v
'range
))
529 (let* ((j (logior (+ v
(math-isqrt-small (* 40 v
))) 1))
530 (two-over-x (math-div 2 x
))
536 (while (> (setq j
(1- j
)) 0)
537 (setq bjm
(math-sub (math-mul (math-mul j two-over-x
) bj
)
541 (if (> (nth 2 (math-abs-approx bj
)) 10)
542 (setq bj
(math-mul bj
'(float 1 -
10))
543 bjp
(math-mul bjp
'(float 1 -
10))
544 ans
(and ans
(math-mul ans
'(float 1 -
10)))
545 sum
(math-mul sum
'(float 1 -
10))))
546 (or (setq jsum
(not jsum
))
547 (setq sum
(math-add sum bj
)))
550 (math-div ans
(math-sub (math-mul 2 sum
) bj
))))))))
552 (defun math-besJ-series (sum term k zz vk
)
553 (math-working "besJ" sum
)
556 term
(math-div (math-mul term zz
) (math-mul k vk
)))
557 (let ((next (math-add sum term
)))
558 (if (math-nearly-equal next sum
)
560 (math-besJ-series next term k zz vk
))))
562 (defun math-besJ0 (x &optional yflag
)
563 (cond ((and (not yflag
) (math-negp (calcFunc-re x
)))
564 (math-besJ0 (math-neg x
)))
565 ((Math-lessp '(float 8 0) (math-abs-approx x
))
566 (let* ((z (math-div '(float 8 0) x
))
569 (math-read-number-simple "-0.785398164")))
570 (a1 (math-poly-eval y
572 (math-read-number-simple "0.0000002093887211")
573 (math-read-number-simple "-0.000002073370639")
574 (math-read-number-simple "0.00002734510407")
575 (math-read-number-simple "-0.001098628627")
577 (a2 (math-poly-eval y
579 (math-read-number-simple "-0.0000000934935152")
580 (math-read-number-simple "0.0000007621095161")
581 (math-read-number-simple "-0.000006911147651")
582 (math-read-number-simple "0.0001430488765")
583 (math-read-number-simple "-0.01562499995"))))
584 (sc (math-sin-cos-raw xx
)))
586 (setq sc
(cons (math-neg (cdr sc
)) (car sc
))))
588 (math-div (math-read-number-simple "0.636619722")
590 (math-sub (math-mul (cdr sc
) a1
)
591 (math-mul (car sc
) (math-mul z a2
))))))
593 (let ((y (math-sqr x
)))
594 (math-div (math-poly-eval y
596 (math-read-number-simple "-184.9052456")
597 (math-read-number-simple "77392.33017")
598 (math-read-number-simple "-11214424.18")
599 (math-read-number-simple "651619640.7")
600 (math-read-number-simple "-13362590354.0")
601 (math-read-number-simple "57568490574.0")))
605 (math-read-number-simple "267.8532712")
606 (math-read-number-simple "59272.64853")
607 (math-read-number-simple "9494680.718")
608 (math-read-number-simple "1029532985.0")
609 (math-read-number-simple "57568490411.0"))))))))
611 (defun math-besJ1 (x &optional yflag
)
612 (cond ((and (math-negp (calcFunc-re x
)) (not yflag
))
613 (math-neg (math-besJ1 (math-neg x
))))
614 ((Math-lessp '(float 8 0) (math-abs-approx x
))
615 (let* ((z (math-div '(float 8 0) x
))
617 (xx (math-add x
(math-read-number-simple "-2.356194491")))
618 (a1 (math-poly-eval y
620 (math-read-number-simple "-0.000000240337019")
621 (math-read-number-simple "0.000002457520174")
622 (math-read-number-simple "-0.00003516396496")
625 (a2 (math-poly-eval y
627 (math-read-number-simple "0.000000105787412")
628 (math-read-number-simple "-0.00000088228987")
629 (math-read-number-simple "0.000008449199096")
630 (math-read-number-simple "-0.0002002690873")
631 (math-read-number-simple "0.04687499995"))))
632 (sc (math-sin-cos-raw xx
)))
634 (setq sc
(cons (math-neg (cdr sc
)) (car sc
)))
636 (setq sc
(cons (math-neg (car sc
)) (math-neg (cdr sc
))))))
637 (math-mul (math-sqrt (math-div
638 (math-read-number-simple "0.636619722")
640 (math-sub (math-mul (cdr sc
) a1
)
641 (math-mul (car sc
) (math-mul z a2
))))))
643 (let ((y (math-sqr x
)))
646 (math-div (math-poly-eval y
648 (math-read-number-simple "-30.16036606")
649 (math-read-number-simple "15704.4826")
650 (math-read-number-simple "-2972611.439")
651 (math-read-number-simple "242396853.1")
652 (math-read-number-simple "-7895059235.0")
653 (math-read-number-simple "72362614232.0")))
657 (math-read-number-simple "376.9991397")
658 (math-read-number-simple "99447.43394")
659 (math-read-number-simple "18583304.74")
660 (math-read-number-simple "2300535178.0")
661 (math-read-number-simple "144725228442.0")))))))))
663 (defun calcFunc-besY (v x
)
664 (math-inexact-result)
665 (or (math-numberp v
) (math-reject-arg v
'numberp
))
666 (or (math-numberp x
) (math-reject-arg x
'numberp
))
667 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
668 (math-with-extra-prec 3
669 (setq x
(math-float (math-normalize x
)))
670 (setq v
(math-float (math-normalize v
)))
671 (cond ((not (math-num-integerp v
))
672 (let ((sc (math-sin-cos-raw (math-mul v
(math-pi)))))
673 (math-div (math-sub (math-mul (calcFunc-besJ v x
) (cdr sc
))
674 (calcFunc-besJ (math-neg v
) x
))
676 ((math-negp (setq v
(math-trunc v
)))
678 (math-neg (calcFunc-besY (math-neg v
) x
))
679 (calcFunc-besY (math-neg v
) x
)))
688 (two-over-x (math-div 2 x
))
690 (while (< (setq j
(1+ j
)) v
)
691 (setq byp
(math-sub (math-mul (math-mul j two-over-x
) by
)
697 (defun math-besY0 (x)
698 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
699 (let ((y (math-sqr x
)))
701 (math-div (math-poly-eval y
703 (math-read-number-simple "228.4622733")
704 (math-read-number-simple "-86327.92757")
705 (math-read-number-simple "10879881.29")
706 (math-read-number-simple "-512359803.6")
707 (math-read-number-simple "7062834065.0")
708 (math-read-number-simple "-2957821389.0")))
712 (math-read-number-simple "226.1030244")
713 (math-read-number-simple "47447.2647")
714 (math-read-number-simple "7189466.438")
715 (math-read-number-simple "745249964.8")
716 (math-read-number-simple "40076544269.0"))))
717 (math-mul (math-read-number-simple "0.636619772")
718 (math-mul (math-besJ0 x
) (math-ln-raw x
))))))
719 ((math-negp (calcFunc-re x
))
720 (math-add (math-besJ0 (math-neg x
) t
)
721 (math-mul '(cplx 0 2)
722 (math-besJ0 (math-neg x
)))))
726 (defun math-besY1 (x)
727 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
728 (let ((y (math-sqr x
)))
732 (math-div (math-poly-eval y
734 (math-read-number-simple "8511.937935")
735 (math-read-number-simple "-4237922.726")
736 (math-read-number-simple "734926455.1")
737 (math-read-number-simple "-51534381390.0")
738 (math-read-number-simple "1275274390000.0")
739 (math-read-number-simple "-4900604943000.0")))
743 (math-read-number-simple "354.9632885")
744 (math-read-number-simple "102042.605")
745 (math-read-number-simple "22459040.02")
746 (math-read-number-simple "3733650367.0")
747 (math-read-number-simple "424441966400.0")
748 (math-read-number-simple "24995805700000.0")))))
749 (math-mul (math-read-number-simple "0.636619772")
750 (math-sub (math-mul (math-besJ1 x
) (math-ln-raw x
))
752 ((math-negp (calcFunc-re x
))
754 (math-add (math-besJ1 (math-neg x
) t
)
755 (math-mul '(cplx 0 2)
756 (math-besJ1 (math-neg x
))))))
760 (defun math-poly-eval (x coefs
)
761 (let ((accum (car coefs
)))
762 (while (setq coefs
(cdr coefs
))
763 (setq accum
(math-add (car coefs
) (math-mul accum x
))))
767 ;;;; Bernoulli and Euler polynomials and numbers.
769 (defun calcFunc-bern (n &optional x
)
770 (if (and x
(not (math-zerop x
)))
771 (if (and calc-symbolic-mode
(math-floatp x
))
772 (math-inexact-result)
773 (math-build-polynomial-expr (math-bernoulli-coefs n
) x
))
774 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
777 (math-inexact-result)
778 (math-float (math-bernoulli-number (math-trunc n
))))
779 (math-bernoulli-number n
))))
781 (defun calcFunc-euler (n &optional x
)
782 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
784 (let* ((n1 (math-add n
1))
785 (coefs (math-bernoulli-coefs n1
))
786 (fac (math-div (math-pow 2 n1
) n1
))
788 (x1 (math-div (math-add x
1) 2))
791 (if (and calc-symbolic-mode
(math-floatp x
))
792 (math-inexact-result)
794 (math-sub (math-build-polynomial-expr coefs x1
)
795 (math-build-polynomial-expr coefs x2
))))
803 (math-mul (math-mul fac c
)
804 (math-sub (math-pow x1 k
)
808 (math-mul (math-pow 2 n
)
811 (math-inexact-result)
812 (calcFunc-euler n
'(float 5 -
1)))
813 (calcFunc-euler n
'(frac 1 2))))))
815 (defvar math-bernoulli-b-cache
819 (math-read-number-simple "802857662698291200000"))
822 (math-read-number-simple "5109094217170944000"))
825 (math-read-number-simple "10670622842880000"))
828 (math-read-number-simple "74724249600"))
831 (math-read-number-simple "1307674368000"))
834 (math-read-number-simple "47900160"))
837 (math-read-number-simple "1209600"))
849 (defvar math-bernoulli-B-cache
850 '((frac -
174611 330) (frac 43867 798)
851 (frac -
3617 510) (frac 7 6) (frac -
691 2730)
852 (frac 5 66) (frac -
1 30) (frac 1 42)
853 (frac -
1 30) (frac 1 6) 1 ))
855 (defvar math-bernoulli-cache-size
11)
856 (defun math-bernoulli-coefs (n)
857 (let* ((coefs (list (calcFunc-bern n
)))
862 (calc-prefer-frac (or (integerp n
) calc-prefer-frac
)))
863 (while (>= (setq k
(1- k
)) 0)
864 (setq term
(math-div term
(- nn k
))
865 coef
(math-mul term
(math-bernoulli-number k
))
866 coefs
(cons (if (consp n
) (math-float coef
) coef
) coefs
)
867 term
(math-mul term k
)))
870 (defun math-bernoulli-number (n)
876 (while (>= n math-bernoulli-cache-size
)
879 (fact 1) ; fact = (n-k+1)!
881 (p math-bernoulli-b-cache
)
882 (calc-prefer-frac t
))
883 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size
))
887 fact
(math-mul fact
(* nk
(1- nk
)))
888 sum
(math-add sum
(math-div (car p
) fact
))
890 (setq ofact
(math-mul ofact
(1- nk
))
891 sum
(math-sub (math-div '(frac 1 2) ofact
) sum
)
892 math-bernoulli-b-cache
(cons sum math-bernoulli-b-cache
)
893 math-bernoulli-B-cache
(cons (math-mul sum ofact
)
894 math-bernoulli-B-cache
)
895 math-bernoulli-cache-size
(1+ math-bernoulli-cache-size
))))
896 (nth (- math-bernoulli-cache-size n
1) math-bernoulli-B-cache
)))
899 ;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
901 ;;; A faster method would be to use "tangent numbers", c.f., Concrete
902 ;;; Mathematics pg. 273.
905 ;;; Probability distributions.
908 (defun calcFunc-utpb (x n p
)
909 (if math-expand-formulas
910 (math-normalize (list 'calcFunc-betaI p x
(list '+ (list '- n x
) 1)))
911 (calcFunc-betaI p x
(math-add (math-sub n x
) 1))))
912 (put 'calcFunc-utpb
'math-expandable t
)
914 (defun calcFunc-ltpb (x n p
)
915 (math-sub 1 (calcFunc-utpb x n p
)))
916 (put 'calcFunc-ltpb
'math-expandable t
)
919 (defun calcFunc-utpc (chisq v
)
920 (if math-expand-formulas
921 (math-normalize (list 'calcFunc-gammaQ
(list '/ v
2) (list '/ chisq
2)))
922 (calcFunc-gammaQ (math-div v
2) (math-div chisq
2))))
923 (put 'calcFunc-utpc
'math-expandable t
)
925 (defun calcFunc-ltpc (chisq v
)
926 (if math-expand-formulas
927 (math-normalize (list 'calcFunc-gammaP
(list '/ v
2) (list '/ chisq
2)))
928 (calcFunc-gammaP (math-div v
2) (math-div chisq
2))))
929 (put 'calcFunc-ltpc
'math-expandable t
)
932 (defun calcFunc-utpf (f v1 v2
)
933 (if math-expand-formulas
934 (math-normalize (list 'calcFunc-betaI
935 (list '/ v2
(list '+ v2
(list '* v1 f
)))
938 (calcFunc-betaI (math-div v2
(math-add v2
(math-mul v1 f
)))
941 (put 'calcFunc-utpf
'math-expandable t
)
943 (defun calcFunc-ltpf (f v1 v2
)
944 (math-sub 1 (calcFunc-utpf f v1 v2
)))
945 (put 'calcFunc-ltpf
'math-expandable t
)
948 (defun calcFunc-utpn (x mean sdev
)
949 (if math-expand-formulas
954 (list '/ (list '- mean x
)
955 (list '* sdev
(list 'calcFunc-sqrt
2)))))
957 (math-mul (math-add '(float 1 0)
959 (math-div (math-sub mean x
)
960 (math-mul sdev
(math-sqrt-2)))))
962 (put 'calcFunc-utpn
'math-expandable t
)
964 (defun calcFunc-ltpn (x mean sdev
)
965 (if math-expand-formulas
970 (list '/ (list '- x mean
)
971 (list '* sdev
(list 'calcFunc-sqrt
2)))))
973 (math-mul (math-add '(float 1 0)
975 (math-div (math-sub x mean
)
976 (math-mul sdev
(math-sqrt-2)))))
978 (put 'calcFunc-ltpn
'math-expandable t
)
981 (defun calcFunc-utpp (n x
)
982 (if math-expand-formulas
983 (math-normalize (list 'calcFunc-gammaP x n
))
984 (calcFunc-gammaP x n
)))
985 (put 'calcFunc-utpp
'math-expandable t
)
987 (defun calcFunc-ltpp (n x
)
988 (if math-expand-formulas
989 (math-normalize (list 'calcFunc-gammaQ x n
))
990 (calcFunc-gammaQ x n
)))
991 (put 'calcFunc-ltpp
'math-expandable t
)
993 ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
994 (defun calcFunc-utpt (tt v
)
995 (if math-expand-formulas
996 (math-normalize (list 'calcFunc-betaI
997 (list '/ v
(list '+ v
(list '^ tt
2)))
1000 (calcFunc-betaI (math-div v
(math-add v
(math-sqr tt
)))
1003 (put 'calcFunc-utpt
'math-expandable t
)
1005 (defun calcFunc-ltpt (tt v
)
1006 (math-sub 1 (calcFunc-utpt tt v
)))
1007 (put 'calcFunc-ltpt
'math-expandable t
)
1009 (provide 'calc-funcs
)
1011 ;;; calc-funcs.el ends here