1 ;;; calc-funcs.el --- well-known functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Author: David Gillespie <daveg@synaptics.com>
7 ;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
30 ;; This file is autoloaded from calc-ext.el.
35 (defun calc-inc-gamma (arg)
39 (if (calc-is-hyperbolic)
40 (calc-binary-op "gamG" 'calcFunc-gammaG arg
)
41 (calc-binary-op "gamQ" 'calcFunc-gammaQ arg
))
42 (if (calc-is-hyperbolic)
43 (calc-binary-op "gamg" 'calcFunc-gammag arg
)
44 (calc-binary-op "gamP" 'calcFunc-gammaP arg
)))))
50 (calc-unary-op "erfc" 'calcFunc-erfc arg
)
51 (calc-unary-op "erf" 'calcFunc-erf arg
))))
53 (defun calc-erfc (arg)
58 (defun calc-beta (arg)
61 (calc-binary-op "beta" 'calcFunc-beta arg
)))
63 (defun calc-inc-beta ()
66 (if (calc-is-hyperbolic)
67 (calc-enter-result 3 "betB" (cons 'calcFunc-betaB
(calc-top-list-n 3)))
68 (calc-enter-result 3 "betI" (cons 'calcFunc-betaI
(calc-top-list-n 3))))))
70 (defun calc-bessel-J (arg)
73 (calc-binary-op "besJ" 'calcFunc-besJ arg
)))
75 (defun calc-bessel-Y (arg)
78 (calc-binary-op "besY" 'calcFunc-besY arg
)))
80 (defun calc-bernoulli-number (arg)
83 (if (calc-is-hyperbolic)
84 (calc-binary-op "bern" 'calcFunc-bern arg
)
85 (calc-unary-op "bern" 'calcFunc-bern arg
))))
87 (defun calc-euler-number (arg)
90 (if (calc-is-hyperbolic)
91 (calc-binary-op "eulr" 'calcFunc-euler arg
)
92 (calc-unary-op "eulr" 'calcFunc-euler arg
))))
94 (defun calc-stirling-number (arg)
97 (if (calc-is-hyperbolic)
98 (calc-binary-op "str2" 'calcFunc-stir2 arg
)
99 (calc-binary-op "str1" 'calcFunc-stir1 arg
))))
103 (calc-prob-dist "b" 3))
107 (calc-prob-dist "c" 2))
111 (calc-prob-dist "f" 3))
115 (calc-prob-dist "n" 3))
119 (calc-prob-dist "p" 2))
123 (calc-prob-dist "t" 2))
125 (defun calc-prob-dist (letter nargs
)
127 (if (calc-is-inverse)
128 (calc-enter-result nargs
(concat "ltp" letter
)
129 (append (list (intern (concat "calcFunc-ltp" letter
))
131 (calc-top-list-n (1- nargs
) 2)))
132 (calc-enter-result nargs
(concat "utp" letter
)
133 (append (list (intern (concat "calcFunc-utp" letter
))
135 (calc-top-list-n (1- nargs
) 2))))))
140 ;;; Sources: Numerical Recipes, Press et al;
141 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
146 (defun calcFunc-gamma (x)
147 (or (math-numberp x
) (math-reject-arg x
'numberp
))
148 (calcFunc-fact (math-add x -
1)))
150 (defun math-gammap1-raw (x &optional fprec nfprec
)
151 "Compute gamma(1+X) to the appropriate precision."
153 (setq fprec
(math-float calc-internal-prec
)
154 nfprec
(math-float (- calc-internal-prec
))))
155 (cond ((math-lessp-float (calcFunc-re x
) fprec
)
156 (if (math-lessp-float (calcFunc-re x
) nfprec
)
159 (math-mul (math-gammap1-raw
160 (math-add (math-neg x
)
164 (math-mul (math-pi) x
)))))
165 (let ((xplus1 (math-add x
'(float 1 0))))
166 (math-div (math-gammap1-raw xplus1 fprec nfprec
) xplus1
))))
168 (math-lessp-float '(float 736276 0) x
))
170 (t ; re(x) now >= 10.0
171 (let ((xinv (math-div 1 x
))
172 (lnx (math-ln-raw x
)))
173 (math-mul (math-sqrt-two-pi)
176 (math-sub (math-mul (math-add x
'(float 5 -
1))
184 (defun math-gamma-series (sum x xinvsqr oterm n
)
185 (math-working "gamma" sum
)
186 (let* ((bn (math-bernoulli-number n
))
187 (term (math-mul (math-div-float (math-float (nth 1 bn
))
188 (math-float (* (nth 2 bn
)
191 (next (math-add sum term
)))
192 (if (math-nearly-equal sum next
)
194 (if (> n
(* 2 calc-internal-prec
))
196 ;; Need this because series eventually diverges for large enough n.
198 "*Gamma computation stopped early, not all digits may be valid")
200 (math-gamma-series next
(math-mul x xinvsqr
) xinvsqr term
(+ n
2))))))
203 ;;; Incomplete gamma function.
205 (defvar math-current-gamma-value nil
)
206 (defun calcFunc-gammaP (a x
)
207 (if (equal x
'(var inf var-inf
))
209 (math-inexact-result)
210 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
211 (or (math-numberp x
) (math-reject-arg x
'numberp
))
212 (if (and (math-num-integerp a
)
213 (integerp (setq a
(math-trunc a
)))
215 (math-sub 1 (calcFunc-gammaQ a x
))
216 (let ((math-current-gamma-value (calcFunc-gamma a
)))
217 (math-div (calcFunc-gammag a x
) math-current-gamma-value
)))))
219 (defun calcFunc-gammaQ (a x
)
220 (if (equal x
'(var inf var-inf
))
222 (math-inexact-result)
223 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
224 (or (math-numberp x
) (math-reject-arg x
'numberp
))
225 (if (and (math-num-integerp a
)
226 (integerp (setq a
(math-trunc a
)))
231 (math-with-extra-prec 1
232 (while (< (setq n
(1+ n
)) a
)
233 (setq term
(math-div (math-mul term x
) n
)
234 sum
(math-add sum term
))
235 (math-working "gamma" sum
))
236 (math-mul sum
(calcFunc-exp (math-neg x
)))))
237 (let ((math-current-gamma-value (calcFunc-gamma a
)))
238 (math-div (calcFunc-gammaG a x
) math-current-gamma-value
)))))
240 (defun calcFunc-gammag (a x
)
241 (if (equal x
'(var inf var-inf
))
243 (math-inexact-result)
244 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
245 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
246 (math-with-extra-prec 2
247 (setq a
(math-float a
))
248 (setq x
(math-float x
))
249 (if (or (math-negp (calcFunc-re a
))
250 (math-lessp-float (calcFunc-re x
)
251 (math-add-float (calcFunc-re a
)
253 (math-inc-gamma-series a x
)
254 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
255 (math-inc-gamma-cfrac a x
))))))
257 (defun calcFunc-gammaG (a x
)
258 (if (equal x
'(var inf var-inf
))
260 (math-inexact-result)
261 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
262 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
263 (math-with-extra-prec 2
264 (setq a
(math-float a
))
265 (setq x
(math-float x
))
266 (if (or (math-negp (calcFunc-re a
))
267 (math-lessp-float (calcFunc-re x
)
268 (math-add-float (math-abs-approx a
)
270 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
271 (math-inc-gamma-series a x
))
272 (math-inc-gamma-cfrac a x
)))))
274 (defun math-inc-gamma-series (a x
)
277 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
278 (math-with-extra-prec 2
279 (let ((start (math-div '(float 1 0) a
)))
280 (math-inc-gamma-series-step start start a x
))))))
282 (defun math-inc-gamma-series-step (sum term a x
)
283 (math-working "gamma" sum
)
284 (setq a
(math-add a
'(float 1 0))
285 term
(math-div (math-mul term x
) a
))
286 (let ((next (math-add sum term
)))
287 (if (math-nearly-equal sum next
)
289 (math-inc-gamma-series-step next term a x
))))
291 (defun math-inc-gamma-cfrac (a x
)
293 (or math-current-gamma-value
(calcFunc-gamma a
))
294 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
295 (math-inc-gamma-cfrac-step '(float 1 0) x
296 '(float 0 0) '(float 1 0)
297 '(float 1 0) '(float 1 0) '(float 0 0)
300 (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x
)
301 (let ((ana (math-sub n a
))
302 (anf (math-mul n fac
)))
303 (setq n
(math-add n
'(float 1 0))
304 a0
(math-mul (math-add a1
(math-mul a0 ana
)) fac
)
305 b0
(math-mul (math-add b1
(math-mul b0 ana
)) fac
)
306 a1
(math-add (math-mul x a0
) (math-mul anf a1
))
307 b1
(math-add (math-mul x b0
) (math-mul anf b1
)))
309 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x
)
310 (setq fac
(math-div '(float 1 0) a1
))
311 (let ((next (math-mul b1 fac
)))
312 (math-working "gamma" next
)
313 (if (math-nearly-equal next g
)
315 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x
))))))
320 (defun calcFunc-erf (x)
321 (if (equal x
'(var inf var-inf
))
323 (if (equal x
'(neg (var inf var-inf
)))
327 (let ((math-current-gamma-value (math-sqrt-pi)))
328 (math-to-same-complex-quad
329 (math-div (calcFunc-gammag '(float 5 -
1)
330 (math-sqr (math-to-complex-quad-one x
)))
331 math-current-gamma-value
)
334 (defun calcFunc-erfc (x)
335 (if (equal x
'(var inf var-inf
))
338 (let ((math-current-gamma-value (math-sqrt-pi)))
339 (math-div (calcFunc-gammaG '(float 5 -
1) (math-sqr x
))
340 math-current-gamma-value
))
341 (math-sub 1 (calcFunc-erf x
)))))
343 (defun math-to-complex-quad-one (x)
344 (if (eq (car-safe x
) 'polar
) (setq x
(math-complex x
)))
345 (if (eq (car-safe x
) 'cplx
)
346 (list 'cplx
(math-abs (nth 1 x
)) (math-abs (nth 2 x
)))
349 (defun math-to-same-complex-quad (x y
)
350 (if (eq (car-safe y
) 'cplx
)
351 (if (eq (car-safe x
) 'cplx
)
353 (if (math-negp (nth 1 y
)) (math-neg (nth 1 x
)) (nth 1 x
))
354 (if (math-negp (nth 2 y
)) (math-neg (nth 2 x
)) (nth 2 x
)))
355 (if (math-negp (nth 1 y
)) (math-neg x
) x
))
357 (if (eq (car-safe x
) 'cplx
)
358 (list 'cplx
(math-neg (nth 1 x
)) (nth 2 x
))
365 (defun calcFunc-beta (a b
)
366 (if (math-num-integerp a
)
367 (let ((am (math-add a -
1)))
368 (or (math-numberp b
) (math-reject-arg b
'numberp
))
369 (math-div 1 (math-mul b
(calcFunc-choose (math-add b am
) am
))))
370 (if (math-num-integerp b
)
372 (math-div (math-mul (calcFunc-gamma a
) (calcFunc-gamma b
))
373 (calcFunc-gamma (math-add a b
))))))
376 ;;; Incomplete beta function.
378 (defvar math-current-beta-value nil
)
379 (defun calcFunc-betaI (x a b
)
380 (cond ((math-zerop x
)
382 ((math-equal-int x
1)
385 (and (math-num-integerp a
)
387 (if (or (math-zerop b
)
388 (and (math-num-integerp b
)
390 (math-reject-arg b
'range
)
393 (and (math-num-integerp b
)
396 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
397 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
398 ((math-inexact-result))
399 (t (let ((math-current-beta-value (calcFunc-beta a b
)))
400 (math-div (calcFunc-betaB x a b
) math-current-beta-value
)))))
402 (defun calcFunc-betaB (x a b
)
406 ((math-equal-int x
1)
408 ((not (math-numberp x
)) (math-reject-arg x
'numberp
))
409 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
410 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
411 ((math-zerop a
) (math-reject-arg a
'nonzerop
))
412 ((math-zerop b
) (math-reject-arg b
'nonzerop
))
413 ((and (math-num-integerp b
)
415 (math-reject-arg b
'range
)
416 (Math-natnum-lessp (setq b
(math-trunc b
)) 20)))
417 (and calc-symbolic-mode
(or (math-floatp a
) (math-floatp b
))
418 (math-inexact-result))
420 (math-with-extra-prec 2
423 (sum (math-div term a
)))
424 (while (< (setq i
(1+ i
)) b
)
425 (setq term
(math-mul (math-div (math-mul term
(- i b
)) i
) x
)
426 sum
(math-add sum
(math-div term
(math-add a i
))))
427 (math-working "beta" sum
))
430 ((and (math-num-integerp a
)
432 (math-reject-arg a
'range
)
433 (Math-natnum-lessp (setq a
(math-trunc a
)) 20)))
434 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
435 (calcFunc-betaB (math-sub 1 x
) b a
)))
437 (math-inexact-result)
438 (math-with-extra-prec 2
439 (setq x
(math-float x
))
440 (setq a
(math-float a
))
441 (setq b
(math-float b
))
442 (let ((bt (math-exp-raw (math-add (math-mul a
(math-ln-raw x
))
443 (math-mul b
(math-ln-raw
444 (math-sub '(float 1 0)
446 (if (Math-lessp x
(math-div (math-add a
'(float 1 0))
447 (math-add (math-add a b
) '(float 2 0))))
448 (math-div (math-mul bt
(math-beta-cfrac a b x
)) a
)
449 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
450 (math-div (math-mul bt
451 (math-beta-cfrac b a
(math-sub 1 x
)))
454 (defun math-beta-cfrac (a b x
)
455 (let ((qab (math-add a b
))
456 (qap (math-add a
'(float 1 0)))
457 (qam (math-add a
'(float -
1 0))))
458 (math-beta-cfrac-step '(float 1 0)
459 (math-sub '(float 1 0)
460 (math-div (math-mul qab x
) qap
))
461 '(float 1 0) '(float 1 0)
465 (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x
)
466 (let* ((two-m (math-mul m
'(float 2 0)))
467 (d (math-div (math-mul (math-mul (math-sub b m
) m
) x
)
468 (math-mul (math-add qam two-m
) (math-add a two-m
))))
469 (ap (math-add az
(math-mul d am
)))
470 (bp (math-add bz
(math-mul d bm
)))
472 (math-div (math-mul (math-mul (math-add a m
) (math-add qab m
)) x
)
473 (math-mul (math-add qap two-m
) (math-add a two-m
)))))
474 (app (math-add ap
(math-mul d2 az
)))
475 (bpp (math-add bp
(math-mul d2 bz
)))
476 (next (math-div app bpp
)))
477 (math-working "beta" next
)
478 (if (math-nearly-equal next az
)
480 (math-beta-cfrac-step next
'(float 1 0)
481 (math-div ap bpp
) (math-div bp bpp
)
482 (math-add m
'(float 1 0))
483 qab qap qam a b x
))))
486 ;;; Bessel functions.
488 ;;; Should generalize this to handle arbitrary precision!
490 (defun calcFunc-besJ (v x
)
491 (or (math-numberp v
) (math-reject-arg v
'numberp
))
492 (or (math-numberp x
) (math-reject-arg x
'numberp
))
493 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
494 (math-with-extra-prec 3
495 (setq x
(math-float (math-normalize x
)))
496 (setq v
(math-float (math-normalize v
)))
497 (cond ((math-zerop x
)
501 ((math-inexact-result))
502 ((not (math-num-integerp v
))
503 (let ((start (math-div 1 (calcFunc-fact v
))))
504 (math-mul (math-besJ-series start start
506 (math-mul '(float -
25 -
2)
509 (math-pow (math-div x
2) v
))))
510 ((math-negp (setq v
(math-trunc v
)))
512 (math-neg (calcFunc-besJ (math-neg v
) x
))
513 (calcFunc-besJ (math-neg v
) x
)))
518 ((Math-lessp v
(math-abs-approx x
))
522 (two-over-x (math-div 2 x
))
524 (while (< (setq j
(1+ j
)) v
)
525 (setq bjp
(math-sub (math-mul (math-mul j two-over-x
) bj
)
531 (if (Math-lessp 100 v
) (math-reject-arg v
'range
))
532 (let* ((j (logior (+ v
(math-isqrt-small (* 40 v
))) 1))
533 (two-over-x (math-div 2 x
))
539 (while (> (setq j
(1- j
)) 0)
540 (setq bjm
(math-sub (math-mul (math-mul j two-over-x
) bj
)
544 (if (> (nth 2 (math-abs-approx bj
)) 10)
545 (setq bj
(math-mul bj
'(float 1 -
10))
546 bjp
(math-mul bjp
'(float 1 -
10))
547 ans
(and ans
(math-mul ans
'(float 1 -
10)))
548 sum
(math-mul sum
'(float 1 -
10))))
549 (or (setq jsum
(not jsum
))
550 (setq sum
(math-add sum bj
)))
553 (math-div ans
(math-sub (math-mul 2 sum
) bj
))))))))
555 (defun math-besJ-series (sum term k zz vk
)
556 (math-working "besJ" sum
)
559 term
(math-div (math-mul term zz
) (math-mul k vk
)))
560 (let ((next (math-add sum term
)))
561 (if (math-nearly-equal next sum
)
563 (math-besJ-series next term k zz vk
))))
565 (defun math-besJ0 (x &optional yflag
)
566 (cond ((and (not yflag
) (math-negp (calcFunc-re x
)))
567 (math-besJ0 (math-neg x
)))
568 ((Math-lessp '(float 8 0) (math-abs-approx x
))
569 (let* ((z (math-div '(float 8 0) x
))
572 (math-read-number-simple "-0.785398164")))
573 (a1 (math-poly-eval y
575 (math-read-number-simple "0.0000002093887211")
576 (math-read-number-simple "-0.000002073370639")
577 (math-read-number-simple "0.00002734510407")
578 (math-read-number-simple "-0.001098628627")
580 (a2 (math-poly-eval y
582 (math-read-number-simple "-0.0000000934935152")
583 (math-read-number-simple "0.0000007621095161")
584 (math-read-number-simple "-0.000006911147651")
585 (math-read-number-simple "0.0001430488765")
586 (math-read-number-simple "-0.01562499995"))))
587 (sc (math-sin-cos-raw xx
)))
589 (setq sc
(cons (math-neg (cdr sc
)) (car sc
))))
591 (math-div (math-read-number-simple "0.636619722")
593 (math-sub (math-mul (cdr sc
) a1
)
594 (math-mul (car sc
) (math-mul z a2
))))))
596 (let ((y (math-sqr x
)))
597 (math-div (math-poly-eval y
599 (math-read-number-simple "-184.9052456")
600 (math-read-number-simple "77392.33017")
601 (math-read-number-simple "-11214424.18")
602 (math-read-number-simple "651619640.7")
603 (math-read-number-simple "-13362590354.0")
604 (math-read-number-simple "57568490574.0")))
608 (math-read-number-simple "267.8532712")
609 (math-read-number-simple "59272.64853")
610 (math-read-number-simple "9494680.718")
611 (math-read-number-simple "1029532985.0")
612 (math-read-number-simple "57568490411.0"))))))))
614 (defun math-besJ1 (x &optional yflag
)
615 (cond ((and (math-negp (calcFunc-re x
)) (not yflag
))
616 (math-neg (math-besJ1 (math-neg x
))))
617 ((Math-lessp '(float 8 0) (math-abs-approx x
))
618 (let* ((z (math-div '(float 8 0) x
))
620 (xx (math-add x
(math-read-number-simple "-2.356194491")))
621 (a1 (math-poly-eval y
623 (math-read-number-simple "-0.000000240337019")
624 (math-read-number-simple "0.000002457520174")
625 (math-read-number-simple "-0.00003516396496")
628 (a2 (math-poly-eval y
630 (math-read-number-simple "0.000000105787412")
631 (math-read-number-simple "-0.00000088228987")
632 (math-read-number-simple "0.000008449199096")
633 (math-read-number-simple "-0.0002002690873")
634 (math-read-number-simple "0.04687499995"))))
635 (sc (math-sin-cos-raw xx
)))
637 (setq sc
(cons (math-neg (cdr sc
)) (car sc
)))
639 (setq sc
(cons (math-neg (car sc
)) (math-neg (cdr sc
))))))
640 (math-mul (math-sqrt (math-div
641 (math-read-number-simple "0.636619722")
643 (math-sub (math-mul (cdr sc
) a1
)
644 (math-mul (car sc
) (math-mul z a2
))))))
646 (let ((y (math-sqr x
)))
649 (math-div (math-poly-eval y
651 (math-read-number-simple "-30.16036606")
652 (math-read-number-simple "15704.4826")
653 (math-read-number-simple "-2972611.439")
654 (math-read-number-simple "242396853.1")
655 (math-read-number-simple "-7895059235.0")
656 (math-read-number-simple "72362614232.0")))
660 (math-read-number-simple "376.9991397")
661 (math-read-number-simple "99447.43394")
662 (math-read-number-simple "18583304.74")
663 (math-read-number-simple "2300535178.0")
664 (math-read-number-simple "144725228442.0")))))))))
666 (defun calcFunc-besY (v x
)
667 (math-inexact-result)
668 (or (math-numberp v
) (math-reject-arg v
'numberp
))
669 (or (math-numberp x
) (math-reject-arg x
'numberp
))
670 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
671 (math-with-extra-prec 3
672 (setq x
(math-float (math-normalize x
)))
673 (setq v
(math-float (math-normalize v
)))
674 (cond ((not (math-num-integerp v
))
675 (let ((sc (math-sin-cos-raw (math-mul v
(math-pi)))))
676 (math-div (math-sub (math-mul (calcFunc-besJ v x
) (cdr sc
))
677 (calcFunc-besJ (math-neg v
) x
))
679 ((math-negp (setq v
(math-trunc v
)))
681 (math-neg (calcFunc-besY (math-neg v
) x
))
682 (calcFunc-besY (math-neg v
) x
)))
691 (two-over-x (math-div 2 x
))
693 (while (< (setq j
(1+ j
)) v
)
694 (setq byp
(math-sub (math-mul (math-mul j two-over-x
) by
)
700 (defun math-besY0 (x)
701 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
702 (let ((y (math-sqr x
)))
704 (math-div (math-poly-eval y
706 (math-read-number-simple "228.4622733")
707 (math-read-number-simple "-86327.92757")
708 (math-read-number-simple "10879881.29")
709 (math-read-number-simple "-512359803.6")
710 (math-read-number-simple "7062834065.0")
711 (math-read-number-simple "-2957821389.0")))
715 (math-read-number-simple "226.1030244")
716 (math-read-number-simple "47447.2647")
717 (math-read-number-simple "7189466.438")
718 (math-read-number-simple "745249964.8")
719 (math-read-number-simple "40076544269.0"))))
720 (math-mul (math-read-number-simple "0.636619772")
721 (math-mul (math-besJ0 x
) (math-ln-raw x
))))))
722 ((math-negp (calcFunc-re x
))
723 (math-add (math-besJ0 (math-neg x
) t
)
724 (math-mul '(cplx 0 2)
725 (math-besJ0 (math-neg x
)))))
729 (defun math-besY1 (x)
730 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
731 (let ((y (math-sqr x
)))
735 (math-div (math-poly-eval y
737 (math-read-number-simple "8511.937935")
738 (math-read-number-simple "-4237922.726")
739 (math-read-number-simple "734926455.1")
740 (math-read-number-simple "-51534381390.0")
741 (math-read-number-simple "1275274390000.0")
742 (math-read-number-simple "-4900604943000.0")))
746 (math-read-number-simple "354.9632885")
747 (math-read-number-simple "102042.605")
748 (math-read-number-simple "22459040.02")
749 (math-read-number-simple "3733650367.0")
750 (math-read-number-simple "424441966400.0")
751 (math-read-number-simple "24995805700000.0")))))
752 (math-mul (math-read-number-simple "0.636619772")
753 (math-sub (math-mul (math-besJ1 x
) (math-ln-raw x
))
755 ((math-negp (calcFunc-re x
))
757 (math-add (math-besJ1 (math-neg x
) t
)
758 (math-mul '(cplx 0 2)
759 (math-besJ1 (math-neg x
))))))
763 (defun math-poly-eval (x coefs
)
764 (let ((accum (car coefs
)))
765 (while (setq coefs
(cdr coefs
))
766 (setq accum
(math-add (car coefs
) (math-mul accum x
))))
770 ;;;; Bernoulli and Euler polynomials and numbers.
772 (defun calcFunc-bern (n &optional x
)
773 (if (and x
(not (math-zerop x
)))
774 (if (and calc-symbolic-mode
(math-floatp x
))
775 (math-inexact-result)
776 (math-build-polynomial-expr (math-bernoulli-coefs n
) x
))
777 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
780 (math-inexact-result)
781 (math-float (math-bernoulli-number (math-trunc n
))))
782 (math-bernoulli-number n
))))
784 (defun calcFunc-euler (n &optional x
)
785 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
787 (let* ((n1 (math-add n
1))
788 (coefs (math-bernoulli-coefs n1
))
789 (fac (math-div (math-pow 2 n1
) n1
))
791 (x1 (math-div (math-add x
1) 2))
794 (if (and calc-symbolic-mode
(math-floatp x
))
795 (math-inexact-result)
797 (math-sub (math-build-polynomial-expr coefs x1
)
798 (math-build-polynomial-expr coefs x2
))))
806 (math-mul (math-mul fac c
)
807 (math-sub (math-pow x1 k
)
811 (math-mul (math-pow 2 n
)
814 (math-inexact-result)
815 (calcFunc-euler n
'(float 5 -
1)))
816 (calcFunc-euler n
'(frac 1 2))))))
818 (defvar math-bernoulli-b-cache
822 (math-read-number-simple "802857662698291200000"))
825 (math-read-number-simple "5109094217170944000"))
828 (math-read-number-simple "10670622842880000"))
831 (math-read-number-simple "74724249600"))
834 (math-read-number-simple "1307674368000"))
837 (math-read-number-simple "47900160"))
840 (math-read-number-simple "1209600"))
852 (defvar math-bernoulli-B-cache
853 '((frac -
174611 330) (frac 43867 798)
854 (frac -
3617 510) (frac 7 6) (frac -
691 2730)
855 (frac 5 66) (frac -
1 30) (frac 1 42)
856 (frac -
1 30) (frac 1 6) 1 ))
858 (defvar math-bernoulli-cache-size
11)
859 (defun math-bernoulli-coefs (n)
860 (let* ((coefs (list (calcFunc-bern n
)))
865 (calc-prefer-frac (or (integerp n
) calc-prefer-frac
)))
866 (while (>= (setq k
(1- k
)) 0)
867 (setq term
(math-div term
(- nn k
))
868 coef
(math-mul term
(math-bernoulli-number k
))
869 coefs
(cons (if (consp n
) (math-float coef
) coef
) coefs
)
870 term
(math-mul term k
)))
873 (defun math-bernoulli-number (n)
879 (while (>= n math-bernoulli-cache-size
)
882 (fact 1) ; fact = (n-k+1)!
884 (p math-bernoulli-b-cache
)
885 (calc-prefer-frac t
))
886 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size
))
890 fact
(math-mul fact
(* nk
(1- nk
)))
891 sum
(math-add sum
(math-div (car p
) fact
))
893 (setq ofact
(math-mul ofact
(1- nk
))
894 sum
(math-sub (math-div '(frac 1 2) ofact
) sum
)
895 math-bernoulli-b-cache
(cons sum math-bernoulli-b-cache
)
896 math-bernoulli-B-cache
(cons (math-mul sum ofact
)
897 math-bernoulli-B-cache
)
898 math-bernoulli-cache-size
(1+ math-bernoulli-cache-size
))))
899 (nth (- math-bernoulli-cache-size n
1) math-bernoulli-B-cache
)))
902 ;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
904 ;;; A faster method would be to use "tangent numbers", c.f., Concrete
905 ;;; Mathematics pg. 273.
908 ;;; Probability distributions.
911 (defun calcFunc-utpb (x n p
)
912 (if math-expand-formulas
913 (math-normalize (list 'calcFunc-betaI p x
(list '+ (list '- n x
) 1)))
914 (calcFunc-betaI p x
(math-add (math-sub n x
) 1))))
915 (put 'calcFunc-utpb
'math-expandable t
)
917 (defun calcFunc-ltpb (x n p
)
918 (math-sub 1 (calcFunc-utpb x n p
)))
919 (put 'calcFunc-ltpb
'math-expandable t
)
922 (defun calcFunc-utpc (chisq v
)
923 (if math-expand-formulas
924 (math-normalize (list 'calcFunc-gammaQ
(list '/ v
2) (list '/ chisq
2)))
925 (calcFunc-gammaQ (math-div v
2) (math-div chisq
2))))
926 (put 'calcFunc-utpc
'math-expandable t
)
928 (defun calcFunc-ltpc (chisq v
)
929 (if math-expand-formulas
930 (math-normalize (list 'calcFunc-gammaP
(list '/ v
2) (list '/ chisq
2)))
931 (calcFunc-gammaP (math-div v
2) (math-div chisq
2))))
932 (put 'calcFunc-ltpc
'math-expandable t
)
935 (defun calcFunc-utpf (f v1 v2
)
936 (if math-expand-formulas
937 (math-normalize (list 'calcFunc-betaI
938 (list '/ v2
(list '+ v2
(list '* v1 f
)))
941 (calcFunc-betaI (math-div v2
(math-add v2
(math-mul v1 f
)))
944 (put 'calcFunc-utpf
'math-expandable t
)
946 (defun calcFunc-ltpf (f v1 v2
)
947 (math-sub 1 (calcFunc-utpf f v1 v2
)))
948 (put 'calcFunc-ltpf
'math-expandable t
)
951 (defun calcFunc-utpn (x mean sdev
)
952 (if math-expand-formulas
957 (list '/ (list '- mean x
)
958 (list '* sdev
(list 'calcFunc-sqrt
2)))))
960 (math-mul (math-add '(float 1 0)
962 (math-div (math-sub mean x
)
963 (math-mul sdev
(math-sqrt-2)))))
965 (put 'calcFunc-utpn
'math-expandable t
)
967 (defun calcFunc-ltpn (x mean sdev
)
968 (if math-expand-formulas
973 (list '/ (list '- x mean
)
974 (list '* sdev
(list 'calcFunc-sqrt
2)))))
976 (math-mul (math-add '(float 1 0)
978 (math-div (math-sub x mean
)
979 (math-mul sdev
(math-sqrt-2)))))
981 (put 'calcFunc-ltpn
'math-expandable t
)
984 (defun calcFunc-utpp (n x
)
985 (if math-expand-formulas
986 (math-normalize (list 'calcFunc-gammaP x n
))
987 (calcFunc-gammaP x n
)))
988 (put 'calcFunc-utpp
'math-expandable t
)
990 (defun calcFunc-ltpp (n x
)
991 (if math-expand-formulas
992 (math-normalize (list 'calcFunc-gammaQ x n
))
993 (calcFunc-gammaQ x n
)))
994 (put 'calcFunc-ltpp
'math-expandable t
)
996 ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
997 (defun calcFunc-utpt (tt v
)
998 (if math-expand-formulas
999 (math-normalize (list 'calcFunc-betaI
1000 (list '/ v
(list '+ v
(list '^ tt
2)))
1003 (calcFunc-betaI (math-div v
(math-add v
(math-sqr tt
)))
1006 (put 'calcFunc-utpt
'math-expandable t
)
1008 (defun calcFunc-ltpt (tt v
)
1009 (math-sub 1 (calcFunc-utpt tt v
)))
1010 (put 'calcFunc-ltpt
'math-expandable t
)
1012 (provide 'calc-funcs
)
1014 ;;; arch-tag: 421ddb7a-550f-4dda-a31c-06638ebfc43a
1015 ;;; calc-funcs.el ends here