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 <belanger@truman.edu>
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 2, 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
) ; compute gamma(1 + x)
152 (setq fprec
(math-float calc-internal-prec
)
153 nfprec
(math-float (- calc-internal-prec
))))
154 (cond ((math-lessp-float (calcFunc-re x
) fprec
)
155 (if (math-lessp-float (calcFunc-re x
) nfprec
)
158 (math-mul (math-gammap1-raw
159 (math-add (math-neg x
)
163 (math-mul (math-pi) x
)))))
164 (let ((xplus1 (math-add x
'(float 1 0))))
165 (math-div (math-gammap1-raw xplus1 fprec nfprec
) xplus1
))))
167 (math-lessp-float '(float 736276 0) x
))
169 (t ; re(x) now >= 10.0
170 (let ((xinv (math-div 1 x
))
171 (lnx (math-ln-raw x
)))
172 (math-mul (math-sqrt-two-pi)
175 (math-sub (math-mul (math-add x
'(float 5 -
1))
183 (defun math-gamma-series (sum x xinvsqr oterm n
)
184 (math-working "gamma" sum
)
185 (let* ((bn (math-bernoulli-number n
))
186 (term (math-mul (math-div-float (math-float (nth 1 bn
))
187 (math-float (* (nth 2 bn
)
190 (next (math-add sum term
)))
191 (if (math-nearly-equal sum next
)
193 (if (> n
(* 2 calc-internal-prec
))
195 ;; Need this because series eventually diverges for large enough n.
197 "*Gamma computation stopped early, not all digits may be valid")
199 (math-gamma-series next
(math-mul x xinvsqr
) xinvsqr term
(+ n
2))))))
202 ;;; Incomplete gamma function.
204 (defvar math-current-gamma-value nil
)
205 (defun calcFunc-gammaP (a x
)
206 (if (equal x
'(var inf var-inf
))
208 (math-inexact-result)
209 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
210 (or (math-numberp x
) (math-reject-arg x
'numberp
))
211 (if (and (math-num-integerp a
)
212 (integerp (setq a
(math-trunc a
)))
214 (math-sub 1 (calcFunc-gammaQ a x
))
215 (let ((math-current-gamma-value (calcFunc-gamma a
)))
216 (math-div (calcFunc-gammag a x
) math-current-gamma-value
)))))
218 (defun calcFunc-gammaQ (a x
)
219 (if (equal x
'(var inf var-inf
))
221 (math-inexact-result)
222 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
223 (or (math-numberp x
) (math-reject-arg x
'numberp
))
224 (if (and (math-num-integerp a
)
225 (integerp (setq a
(math-trunc a
)))
230 (math-with-extra-prec 1
231 (while (< (setq n
(1+ n
)) a
)
232 (setq term
(math-div (math-mul term x
) n
)
233 sum
(math-add sum term
))
234 (math-working "gamma" sum
))
235 (math-mul sum
(calcFunc-exp (math-neg x
)))))
236 (let ((math-current-gamma-value (calcFunc-gamma a
)))
237 (math-div (calcFunc-gammaG a x
) math-current-gamma-value
)))))
239 (defun calcFunc-gammag (a x
)
240 (if (equal x
'(var inf var-inf
))
242 (math-inexact-result)
243 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
244 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
245 (math-with-extra-prec 2
246 (setq a
(math-float a
))
247 (setq x
(math-float x
))
248 (if (or (math-negp (calcFunc-re a
))
249 (math-lessp-float (calcFunc-re x
)
250 (math-add-float (calcFunc-re a
)
252 (math-inc-gamma-series a x
)
253 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
254 (math-inc-gamma-cfrac a x
))))))
256 (defun calcFunc-gammaG (a x
)
257 (if (equal x
'(var inf var-inf
))
259 (math-inexact-result)
260 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
261 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
262 (math-with-extra-prec 2
263 (setq a
(math-float a
))
264 (setq x
(math-float x
))
265 (if (or (math-negp (calcFunc-re a
))
266 (math-lessp-float (calcFunc-re x
)
267 (math-add-float (math-abs-approx a
)
269 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
270 (math-inc-gamma-series a x
))
271 (math-inc-gamma-cfrac a x
)))))
273 (defun math-inc-gamma-series (a x
)
276 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
277 (math-with-extra-prec 2
278 (let ((start (math-div '(float 1 0) a
)))
279 (math-inc-gamma-series-step start start a x
))))))
281 (defun math-inc-gamma-series-step (sum term a x
)
282 (math-working "gamma" sum
)
283 (setq a
(math-add a
'(float 1 0))
284 term
(math-div (math-mul term x
) a
))
285 (let ((next (math-add sum term
)))
286 (if (math-nearly-equal sum next
)
288 (math-inc-gamma-series-step next term a x
))))
290 (defun math-inc-gamma-cfrac (a x
)
292 (or math-current-gamma-value
(calcFunc-gamma a
))
293 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
294 (math-inc-gamma-cfrac-step '(float 1 0) x
295 '(float 0 0) '(float 1 0)
296 '(float 1 0) '(float 1 0) '(float 0 0)
299 (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x
)
300 (let ((ana (math-sub n a
))
301 (anf (math-mul n fac
)))
302 (setq n
(math-add n
'(float 1 0))
303 a0
(math-mul (math-add a1
(math-mul a0 ana
)) fac
)
304 b0
(math-mul (math-add b1
(math-mul b0 ana
)) fac
)
305 a1
(math-add (math-mul x a0
) (math-mul anf a1
))
306 b1
(math-add (math-mul x b0
) (math-mul anf b1
)))
308 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x
)
309 (setq fac
(math-div '(float 1 0) a1
))
310 (let ((next (math-mul b1 fac
)))
311 (math-working "gamma" next
)
312 (if (math-nearly-equal next g
)
314 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x
))))))
319 (defun calcFunc-erf (x)
320 (if (equal x
'(var inf var-inf
))
322 (if (equal x
'(neg (var inf var-inf
)))
326 (let ((math-current-gamma-value (math-sqrt-pi)))
327 (math-to-same-complex-quad
328 (math-div (calcFunc-gammag '(float 5 -
1)
329 (math-sqr (math-to-complex-quad-one x
)))
330 math-current-gamma-value
)
333 (defun calcFunc-erfc (x)
334 (if (equal x
'(var inf var-inf
))
337 (let ((math-current-gamma-value (math-sqrt-pi)))
338 (math-div (calcFunc-gammaG '(float 5 -
1) (math-sqr x
))
339 math-current-gamma-value
))
340 (math-sub 1 (calcFunc-erf x
)))))
342 (defun math-to-complex-quad-one (x)
343 (if (eq (car-safe x
) 'polar
) (setq x
(math-complex x
)))
344 (if (eq (car-safe x
) 'cplx
)
345 (list 'cplx
(math-abs (nth 1 x
)) (math-abs (nth 2 x
)))
348 (defun math-to-same-complex-quad (x y
)
349 (if (eq (car-safe y
) 'cplx
)
350 (if (eq (car-safe x
) 'cplx
)
352 (if (math-negp (nth 1 y
)) (math-neg (nth 1 x
)) (nth 1 x
))
353 (if (math-negp (nth 2 y
)) (math-neg (nth 2 x
)) (nth 2 x
)))
354 (if (math-negp (nth 1 y
)) (math-neg x
) x
))
356 (if (eq (car-safe x
) 'cplx
)
357 (list 'cplx
(math-neg (nth 1 x
)) (nth 2 x
))
364 (defun calcFunc-beta (a b
)
365 (if (math-num-integerp a
)
366 (let ((am (math-add a -
1)))
367 (or (math-numberp b
) (math-reject-arg b
'numberp
))
368 (math-div 1 (math-mul b
(calcFunc-choose (math-add b am
) am
))))
369 (if (math-num-integerp b
)
371 (math-div (math-mul (calcFunc-gamma a
) (calcFunc-gamma b
))
372 (calcFunc-gamma (math-add a b
))))))
375 ;;; Incomplete beta function.
377 (defvar math-current-beta-value nil
)
378 (defun calcFunc-betaI (x a b
)
379 (cond ((math-zerop x
)
381 ((math-equal-int x
1)
384 (and (math-num-integerp a
)
386 (if (or (math-zerop b
)
387 (and (math-num-integerp b
)
389 (math-reject-arg b
'range
)
392 (and (math-num-integerp b
)
395 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
396 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
397 ((math-inexact-result))
398 (t (let ((math-current-beta-value (calcFunc-beta a b
)))
399 (math-div (calcFunc-betaB x a b
) math-current-beta-value
)))))
401 (defun calcFunc-betaB (x a b
)
405 ((math-equal-int x
1)
407 ((not (math-numberp x
)) (math-reject-arg x
'numberp
))
408 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
409 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
410 ((math-zerop a
) (math-reject-arg a
'nonzerop
))
411 ((math-zerop b
) (math-reject-arg b
'nonzerop
))
412 ((and (math-num-integerp b
)
414 (math-reject-arg b
'range
)
415 (Math-natnum-lessp (setq b
(math-trunc b
)) 20)))
416 (and calc-symbolic-mode
(or (math-floatp a
) (math-floatp b
))
417 (math-inexact-result))
419 (math-with-extra-prec 2
422 (sum (math-div term a
)))
423 (while (< (setq i
(1+ i
)) b
)
424 (setq term
(math-mul (math-div (math-mul term
(- i b
)) i
) x
)
425 sum
(math-add sum
(math-div term
(math-add a i
))))
426 (math-working "beta" sum
))
429 ((and (math-num-integerp a
)
431 (math-reject-arg a
'range
)
432 (Math-natnum-lessp (setq a
(math-trunc a
)) 20)))
433 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
434 (calcFunc-betaB (math-sub 1 x
) b a
)))
436 (math-inexact-result)
437 (math-with-extra-prec 2
438 (setq x
(math-float x
))
439 (setq a
(math-float a
))
440 (setq b
(math-float b
))
441 (let ((bt (math-exp-raw (math-add (math-mul a
(math-ln-raw x
))
442 (math-mul b
(math-ln-raw
443 (math-sub '(float 1 0)
445 (if (Math-lessp x
(math-div (math-add a
'(float 1 0))
446 (math-add (math-add a b
) '(float 2 0))))
447 (math-div (math-mul bt
(math-beta-cfrac a b x
)) a
)
448 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
449 (math-div (math-mul bt
450 (math-beta-cfrac b a
(math-sub 1 x
)))
453 (defun math-beta-cfrac (a b x
)
454 (let ((qab (math-add a b
))
455 (qap (math-add a
'(float 1 0)))
456 (qam (math-add a
'(float -
1 0))))
457 (math-beta-cfrac-step '(float 1 0)
458 (math-sub '(float 1 0)
459 (math-div (math-mul qab x
) qap
))
460 '(float 1 0) '(float 1 0)
464 (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x
)
465 (let* ((two-m (math-mul m
'(float 2 0)))
466 (d (math-div (math-mul (math-mul (math-sub b m
) m
) x
)
467 (math-mul (math-add qam two-m
) (math-add a two-m
))))
468 (ap (math-add az
(math-mul d am
)))
469 (bp (math-add bz
(math-mul d bm
)))
471 (math-div (math-mul (math-mul (math-add a m
) (math-add qab m
)) x
)
472 (math-mul (math-add qap two-m
) (math-add a two-m
)))))
473 (app (math-add ap
(math-mul d2 az
)))
474 (bpp (math-add bp
(math-mul d2 bz
)))
475 (next (math-div app bpp
)))
476 (math-working "beta" next
)
477 (if (math-nearly-equal next az
)
479 (math-beta-cfrac-step next
'(float 1 0)
480 (math-div ap bpp
) (math-div bp bpp
)
481 (math-add m
'(float 1 0))
482 qab qap qam a b x
))))
485 ;;; Bessel functions.
487 ;;; Should generalize this to handle arbitrary precision!
489 (defun calcFunc-besJ (v x
)
490 (or (math-numberp v
) (math-reject-arg v
'numberp
))
491 (or (math-numberp x
) (math-reject-arg x
'numberp
))
492 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
493 (math-with-extra-prec 3
494 (setq x
(math-float (math-normalize x
)))
495 (setq v
(math-float (math-normalize v
)))
496 (cond ((math-zerop x
)
500 ((math-inexact-result))
501 ((not (math-num-integerp v
))
502 (let ((start (math-div 1 (calcFunc-fact v
))))
503 (math-mul (math-besJ-series start start
505 (math-mul '(float -
25 -
2)
508 (math-pow (math-div x
2) v
))))
509 ((math-negp (setq v
(math-trunc v
)))
511 (math-neg (calcFunc-besJ (math-neg v
) x
))
512 (calcFunc-besJ (math-neg v
) x
)))
517 ((Math-lessp v
(math-abs-approx x
))
521 (two-over-x (math-div 2 x
))
523 (while (< (setq j
(1+ j
)) v
)
524 (setq bjp
(math-sub (math-mul (math-mul j two-over-x
) bj
)
530 (if (Math-lessp 100 v
) (math-reject-arg v
'range
))
531 (let* ((j (logior (+ v
(math-isqrt-small (* 40 v
))) 1))
532 (two-over-x (math-div 2 x
))
538 (while (> (setq j
(1- j
)) 0)
539 (setq bjm
(math-sub (math-mul (math-mul j two-over-x
) bj
)
543 (if (> (nth 2 (math-abs-approx bj
)) 10)
544 (setq bj
(math-mul bj
'(float 1 -
10))
545 bjp
(math-mul bjp
'(float 1 -
10))
546 ans
(and ans
(math-mul ans
'(float 1 -
10)))
547 sum
(math-mul sum
'(float 1 -
10))))
548 (or (setq jsum
(not jsum
))
549 (setq sum
(math-add sum bj
)))
552 (math-div ans
(math-sub (math-mul 2 sum
) bj
))))))))
554 (defun math-besJ-series (sum term k zz vk
)
555 (math-working "besJ" sum
)
558 term
(math-div (math-mul term zz
) (math-mul k vk
)))
559 (let ((next (math-add sum term
)))
560 (if (math-nearly-equal next sum
)
562 (math-besJ-series next term k zz vk
))))
564 (defun math-besJ0 (x &optional yflag
)
565 (cond ((and (not yflag
) (math-negp (calcFunc-re x
)))
566 (math-besJ0 (math-neg x
)))
567 ((Math-lessp '(float 8 0) (math-abs-approx x
))
568 (let* ((z (math-div '(float 8 0) x
))
570 (xx (math-add x
'(float (bigneg 164 398 785) -
9)))
571 (a1 (math-poly-eval y
572 '((float (bigpos 211 887 093 2) -
16)
573 (float (bigneg 639 370 073 2) -
15)
574 (float (bigpos 407 510 734 2) -
14)
575 (float (bigneg 627 628 098 1) -
12)
577 (a2 (math-poly-eval y
578 '((float (bigneg 152 935 934) -
16)
579 (float (bigpos 161 095 621 7) -
16)
580 (float (bigneg 651 147 911 6) -
15)
581 (float (bigpos 765 488 430 1) -
13)
582 (float (bigneg 995 499 562 1) -
11))))
583 (sc (math-sin-cos-raw xx
)))
585 (setq sc
(cons (math-neg (cdr sc
)) (car sc
))))
587 (math-div '(float (bigpos 722 619 636) -
9) x
))
588 (math-sub (math-mul (cdr sc
) a1
)
589 (math-mul (car sc
) (math-mul z a2
))))))
591 (let ((y (math-sqr x
)))
592 (math-div (math-poly-eval y
593 '((float (bigneg 456 052 849 1) -
7)
594 (float (bigpos 017 233 739 7) -
5)
595 (float (bigneg 418 442 121 1) -
2)
596 (float (bigpos 407 196 516 6) -
1)
597 (float (bigneg 354 590 362 13) 0)
598 (float (bigpos 574 490 568 57) 0)))
601 (float (bigpos 712 532 678 2) -
7)
602 (float (bigpos 853 264 927 5) -
5)
603 (float (bigpos 718 680 494 9) -
3)
604 (float (bigpos 985 532 029 1) 0)
605 (float (bigpos 411 490 568 57) 0))))))))
607 (defun math-besJ1 (x &optional yflag
)
608 (cond ((and (math-negp (calcFunc-re x
)) (not yflag
))
609 (math-neg (math-besJ1 (math-neg x
))))
610 ((Math-lessp '(float 8 0) (math-abs-approx x
))
611 (let* ((z (math-div '(float 8 0) x
))
613 (xx (math-add x
'(float (bigneg 491 194 356 2) -
9)))
614 (a1 (math-poly-eval y
615 '((float (bigneg 019 337 240) -
15)
616 (float (bigpos 174 520 457 2) -
15)
617 (float (bigneg 496 396 516 3) -
14)
620 (a2 (math-poly-eval y
621 '((float (bigpos 412 787 105) -
15)
622 (float (bigneg 987 228 88) -
14)
623 (float (bigpos 096 199 449 8) -
15)
624 (float (bigneg 873 690 002 2) -
13)
625 (float (bigpos 995 499 687 4) -
11))))
626 (sc (math-sin-cos-raw xx
)))
628 (setq sc
(cons (math-neg (cdr sc
)) (car sc
)))
630 (setq sc
(cons (math-neg (car sc
)) (math-neg (cdr sc
))))))
631 (math-mul (math-sqrt (math-div '(float (bigpos 722 619 636) -
9) x
))
632 (math-sub (math-mul (cdr sc
) a1
)
633 (math-mul (car sc
) (math-mul z a2
))))))
635 (let ((y (math-sqr x
)))
638 (math-div (math-poly-eval y
639 '((float (bigneg 606 036 016 3) -
8)
640 (float (bigpos 826 044 157) -
4)
641 (float (bigneg 439 611 972 2) -
3)
642 (float (bigpos 531 968 423 2) -
1)
643 (float (bigneg 235 059 895 7) 0)
644 (float (bigpos 232 614 362 72) 0)))
647 (float (bigpos 397 991 769 3) -
7)
648 (float (bigpos 394 743 944 9) -
5)
649 (float (bigpos 474 330 858 1) -
2)
650 (float (bigpos 178 535 300 2) 0)
651 (float (bigpos 442 228 725 144)
654 (defun calcFunc-besY (v x
)
655 (math-inexact-result)
656 (or (math-numberp v
) (math-reject-arg v
'numberp
))
657 (or (math-numberp x
) (math-reject-arg x
'numberp
))
658 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
659 (math-with-extra-prec 3
660 (setq x
(math-float (math-normalize x
)))
661 (setq v
(math-float (math-normalize v
)))
662 (cond ((not (math-num-integerp v
))
663 (let ((sc (math-sin-cos-raw (math-mul v
(math-pi)))))
664 (math-div (math-sub (math-mul (calcFunc-besJ v x
) (cdr sc
))
665 (calcFunc-besJ (math-neg v
) x
))
667 ((math-negp (setq v
(math-trunc v
)))
669 (math-neg (calcFunc-besY (math-neg v
) x
))
670 (calcFunc-besY (math-neg v
) x
)))
679 (two-over-x (math-div 2 x
))
681 (while (< (setq j
(1+ j
)) v
)
682 (setq byp
(math-sub (math-mul (math-mul j two-over-x
) by
)
688 (defun math-besY0 (x)
689 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
690 (let ((y (math-sqr x
)))
692 (math-div (math-poly-eval y
693 '((float (bigpos 733 622 284 2) -
7)
694 (float (bigneg 757 792 632 8) -
5)
695 (float (bigpos 129 988 087 1) -
2)
696 (float (bigneg 036 598 123 5) -
1)
697 (float (bigpos 065 834 062 7) 0)
698 (float (bigneg 389 821 957 2) 0)))
701 (float (bigpos 244 030 261 2) -
7)
702 (float (bigpos 647 472 474) -
4)
703 (float (bigpos 438 466 189 7) -
3)
704 (float (bigpos 648 499 452 7) -
1)
705 (float (bigpos 269 544 076 40) 0))))
706 (math-mul '(float (bigpos 772 619 636) -
9)
707 (math-mul (math-besJ0 x
) (math-ln-raw x
))))))
708 ((math-negp (calcFunc-re x
))
709 (math-add (math-besJ0 (math-neg x
) t
)
710 (math-mul '(cplx 0 2)
711 (math-besJ0 (math-neg x
)))))
715 (defun math-besY1 (x)
716 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
717 (let ((y (math-sqr x
)))
721 (math-div (math-poly-eval y
722 '((float (bigpos 935 937 511 8) -
6)
723 (float (bigneg 726 922 237 4) -
3)
724 (float (bigpos 551 264 349 7) -
1)
725 (float (bigneg 139 438 153 5) 1)
726 (float (bigpos 439 527 127) 4)
727 (float (bigneg 943 604 900 4) 3)))
730 (float (bigpos 885 632 549 3) -
7)
731 (float (bigpos 605 042 102) -
3)
732 (float (bigpos 002 904 245 2) -
2)
733 (float (bigpos 367 650 733 3) 0)
734 (float (bigpos 664 419 244 4) 2)
735 (float (bigpos 057 958 249) 5)))))
736 (math-mul '(float (bigpos 772 619 636) -
9)
737 (math-sub (math-mul (math-besJ1 x
) (math-ln-raw x
))
739 ((math-negp (calcFunc-re x
))
741 (math-add (math-besJ1 (math-neg x
) t
)
742 (math-mul '(cplx 0 2)
743 (math-besJ1 (math-neg x
))))))
747 (defun math-poly-eval (x coefs
)
748 (let ((accum (car coefs
)))
749 (while (setq coefs
(cdr coefs
))
750 (setq accum
(math-add (car coefs
) (math-mul accum x
))))
754 ;;;; Bernoulli and Euler polynomials and numbers.
756 (defun calcFunc-bern (n &optional x
)
757 (if (and x
(not (math-zerop x
)))
758 (if (and calc-symbolic-mode
(math-floatp x
))
759 (math-inexact-result)
760 (math-build-polynomial-expr (math-bernoulli-coefs n
) x
))
761 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
764 (math-inexact-result)
765 (math-float (math-bernoulli-number (math-trunc n
))))
766 (math-bernoulli-number n
))))
768 (defun calcFunc-euler (n &optional x
)
769 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
771 (let* ((n1 (math-add n
1))
772 (coefs (math-bernoulli-coefs n1
))
773 (fac (math-div (math-pow 2 n1
) n1
))
775 (x1 (math-div (math-add x
1) 2))
778 (if (and calc-symbolic-mode
(math-floatp x
))
779 (math-inexact-result)
781 (math-sub (math-build-polynomial-expr coefs x1
)
782 (math-build-polynomial-expr coefs x2
))))
790 (math-mul (math-mul fac c
)
791 (math-sub (math-pow x1 k
)
795 (math-mul (math-pow 2 n
)
798 (math-inexact-result)
799 (calcFunc-euler n
'(float 5 -
1)))
800 (calcFunc-euler n
'(frac 1 2))))))
802 (defvar math-bernoulli-b-cache
'((frac -
174611
803 (bigpos 0 200 291 698 662 857 802))
804 (frac 43867 (bigpos 0 944 170 217 94 109 5))
805 (frac -
3617 (bigpos 0 880 842 622 670 10))
806 (frac 1 (bigpos 600 249 724 74))
807 (frac -
691 (bigpos 0 368 674 307 1))
808 (frac 1 (bigpos 160 900 47))
809 (frac -
1 (bigpos 600 209 1))
810 (frac 1 30240) (frac -
1 720)
813 (defvar math-bernoulli-B-cache
'((frac -
174611 330) (frac 43867 798)
814 (frac -
3617 510) (frac 7 6) (frac -
691 2730)
815 (frac 5 66) (frac -
1 30) (frac 1 42)
816 (frac -
1 30) (frac 1 6) 1 ))
818 (defvar math-bernoulli-cache-size
11)
819 (defun math-bernoulli-coefs (n)
820 (let* ((coefs (list (calcFunc-bern n
)))
825 (calc-prefer-frac (or (integerp n
) calc-prefer-frac
)))
826 (while (>= (setq k
(1- k
)) 0)
827 (setq term
(math-div term
(- nn k
))
828 coef
(math-mul term
(math-bernoulli-number k
))
829 coefs
(cons (if (consp n
) (math-float coef
) coef
) coefs
)
830 term
(math-mul term k
)))
833 (defun math-bernoulli-number (n)
839 (while (>= n math-bernoulli-cache-size
)
842 (fact 1) ; fact = (n-k+1)!
844 (p math-bernoulli-b-cache
)
845 (calc-prefer-frac t
))
846 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size
))
850 fact
(math-mul fact
(* nk
(1- nk
)))
851 sum
(math-add sum
(math-div (car p
) fact
))
853 (setq ofact
(math-mul ofact
(1- nk
))
854 sum
(math-sub (math-div '(frac 1 2) ofact
) sum
)
855 math-bernoulli-b-cache
(cons sum math-bernoulli-b-cache
)
856 math-bernoulli-B-cache
(cons (math-mul sum ofact
)
857 math-bernoulli-B-cache
)
858 math-bernoulli-cache-size
(1+ math-bernoulli-cache-size
))))
859 (nth (- math-bernoulli-cache-size n
1) math-bernoulli-B-cache
)))
862 ;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
864 ;;; A faster method would be to use "tangent numbers", c.f., Concrete
865 ;;; Mathematics pg. 273.
868 ;;; Probability distributions.
871 (defun calcFunc-utpb (x n p
)
872 (if math-expand-formulas
873 (math-normalize (list 'calcFunc-betaI p x
(list '+ (list '- n x
) 1)))
874 (calcFunc-betaI p x
(math-add (math-sub n x
) 1))))
875 (put 'calcFunc-utpb
'math-expandable t
)
877 (defun calcFunc-ltpb (x n p
)
878 (math-sub 1 (calcFunc-utpb x n p
)))
879 (put 'calcFunc-ltpb
'math-expandable t
)
882 (defun calcFunc-utpc (chisq v
)
883 (if math-expand-formulas
884 (math-normalize (list 'calcFunc-gammaQ
(list '/ v
2) (list '/ chisq
2)))
885 (calcFunc-gammaQ (math-div v
2) (math-div chisq
2))))
886 (put 'calcFunc-utpc
'math-expandable t
)
888 (defun calcFunc-ltpc (chisq v
)
889 (if math-expand-formulas
890 (math-normalize (list 'calcFunc-gammaP
(list '/ v
2) (list '/ chisq
2)))
891 (calcFunc-gammaP (math-div v
2) (math-div chisq
2))))
892 (put 'calcFunc-ltpc
'math-expandable t
)
895 (defun calcFunc-utpf (f v1 v2
)
896 (if math-expand-formulas
897 (math-normalize (list 'calcFunc-betaI
898 (list '/ v2
(list '+ v2
(list '* v1 f
)))
901 (calcFunc-betaI (math-div v2
(math-add v2
(math-mul v1 f
)))
904 (put 'calcFunc-utpf
'math-expandable t
)
906 (defun calcFunc-ltpf (f v1 v2
)
907 (math-sub 1 (calcFunc-utpf f v1 v2
)))
908 (put 'calcFunc-ltpf
'math-expandable t
)
911 (defun calcFunc-utpn (x mean sdev
)
912 (if math-expand-formulas
917 (list '/ (list '- mean x
)
918 (list '* sdev
(list 'calcFunc-sqrt
2)))))
920 (math-mul (math-add '(float 1 0)
922 (math-div (math-sub mean x
)
923 (math-mul sdev
(math-sqrt-2)))))
925 (put 'calcFunc-utpn
'math-expandable t
)
927 (defun calcFunc-ltpn (x mean sdev
)
928 (if math-expand-formulas
933 (list '/ (list '- x mean
)
934 (list '* sdev
(list 'calcFunc-sqrt
2)))))
936 (math-mul (math-add '(float 1 0)
938 (math-div (math-sub x mean
)
939 (math-mul sdev
(math-sqrt-2)))))
941 (put 'calcFunc-ltpn
'math-expandable t
)
944 (defun calcFunc-utpp (n x
)
945 (if math-expand-formulas
946 (math-normalize (list 'calcFunc-gammaP x n
))
947 (calcFunc-gammaP x n
)))
948 (put 'calcFunc-utpp
'math-expandable t
)
950 (defun calcFunc-ltpp (n x
)
951 (if math-expand-formulas
952 (math-normalize (list 'calcFunc-gammaQ x n
))
953 (calcFunc-gammaQ x n
)))
954 (put 'calcFunc-ltpp
'math-expandable t
)
956 ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
957 (defun calcFunc-utpt (tt v
)
958 (if math-expand-formulas
959 (math-normalize (list 'calcFunc-betaI
960 (list '/ v
(list '+ v
(list '^ tt
2)))
963 (calcFunc-betaI (math-div v
(math-add v
(math-sqr tt
)))
966 (put 'calcFunc-utpt
'math-expandable t
)
968 (defun calcFunc-ltpt (tt v
)
969 (math-sub 1 (calcFunc-utpt tt v
)))
970 (put 'calcFunc-ltpt
'math-expandable t
)
972 (provide 'calc-funcs
)
974 ;;; arch-tag: 421ddb7a-550f-4dda-a31c-06638ebfc43a
975 ;;; calc-funcs.el ends here