1 ;;; calc-funcs.el --- well-known functions for Calc
3 ;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
5 ;; Author: David Gillespie <daveg@synaptics.com>
6 ;; Maintainer: Colin Walters <walters@debian.org>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY. No author or distributor
12 ;; accepts responsibility to anyone for the consequences of using it
13 ;; or for whether it serves any particular purpose or works at all,
14 ;; unless he says so in writing. Refer to the GNU Emacs General Public
15 ;; License for full details.
17 ;; Everyone is granted permission to copy, modify and redistribute
18 ;; GNU Emacs, but only under the conditions described in the
19 ;; GNU Emacs General Public License. A copy of this license is
20 ;; supposed to have been given to you along with GNU Emacs so you
21 ;; can know your rights and responsibilities. It should be in a
22 ;; file named COPYING. Among other things, the copyright notice
23 ;; and this notice must be preserved on all copies.
29 ;; This file is autoloaded from calc-ext.el.
34 (defun calc-Need-calc-funcs () nil
)
37 (defun calc-inc-gamma (arg)
41 (if (calc-is-hyperbolic)
42 (calc-binary-op "gamG" 'calcFunc-gammaG arg
)
43 (calc-binary-op "gamQ" 'calcFunc-gammaQ arg
))
44 (if (calc-is-hyperbolic)
45 (calc-binary-op "gamg" 'calcFunc-gammag arg
)
46 (calc-binary-op "gamP" 'calcFunc-gammaP arg
)))))
52 (calc-unary-op "erfc" 'calcFunc-erfc arg
)
53 (calc-unary-op "erf" 'calcFunc-erf arg
))))
55 (defun calc-erfc (arg)
60 (defun calc-beta (arg)
63 (calc-binary-op "beta" 'calcFunc-beta arg
)))
65 (defun calc-inc-beta ()
68 (if (calc-is-hyperbolic)
69 (calc-enter-result 3 "betB" (cons 'calcFunc-betaB
(calc-top-list-n 3)))
70 (calc-enter-result 3 "betI" (cons 'calcFunc-betaI
(calc-top-list-n 3))))))
72 (defun calc-bessel-J (arg)
75 (calc-binary-op "besJ" 'calcFunc-besJ arg
)))
77 (defun calc-bessel-Y (arg)
80 (calc-binary-op "besY" 'calcFunc-besY arg
)))
82 (defun calc-bernoulli-number (arg)
85 (if (calc-is-hyperbolic)
86 (calc-binary-op "bern" 'calcFunc-bern arg
)
87 (calc-unary-op "bern" 'calcFunc-bern arg
))))
89 (defun calc-euler-number (arg)
92 (if (calc-is-hyperbolic)
93 (calc-binary-op "eulr" 'calcFunc-euler arg
)
94 (calc-unary-op "eulr" 'calcFunc-euler arg
))))
96 (defun calc-stirling-number (arg)
99 (if (calc-is-hyperbolic)
100 (calc-binary-op "str2" 'calcFunc-stir2 arg
)
101 (calc-binary-op "str1" 'calcFunc-stir1 arg
))))
105 (calc-prob-dist "b" 3))
109 (calc-prob-dist "c" 2))
113 (calc-prob-dist "f" 3))
117 (calc-prob-dist "n" 3))
121 (calc-prob-dist "p" 2))
125 (calc-prob-dist "t" 2))
127 (defun calc-prob-dist (letter nargs
)
129 (if (calc-is-inverse)
130 (calc-enter-result nargs
(concat "ltp" letter
)
131 (append (list (intern (concat "calcFunc-ltp" letter
))
133 (calc-top-list-n (1- nargs
) 2)))
134 (calc-enter-result nargs
(concat "utp" letter
)
135 (append (list (intern (concat "calcFunc-utp" letter
))
137 (calc-top-list-n (1- nargs
) 2))))))
142 ;;; Sources: Numerical Recipes, Press et al;
143 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
148 (defun calcFunc-gamma (x)
149 (or (math-numberp x
) (math-reject-arg x
'numberp
))
150 (calcFunc-fact (math-add x -
1)))
152 (defun math-gammap1-raw (x &optional fprec nfprec
) ; compute gamma(1 + x)
154 (setq fprec
(math-float calc-internal-prec
)
155 nfprec
(math-float (- calc-internal-prec
))))
156 (cond ((math-lessp-float (calcFunc-re x
) fprec
)
157 (if (math-lessp-float (calcFunc-re x
) nfprec
)
160 (math-mul (math-gammap1-raw
161 (math-add (math-neg x
)
165 (math-mul (math-pi) x
)))))
166 (let ((xplus1 (math-add x
'(float 1 0))))
167 (math-div (math-gammap1-raw xplus1 fprec nfprec
) xplus1
))))
169 (math-lessp-float '(float 736276 0) x
))
171 (t ; re(x) now >= 10.0
172 (let ((xinv (math-div 1 x
))
173 (lnx (math-ln-raw x
)))
174 (math-mul (math-sqrt-two-pi)
177 (math-sub (math-mul (math-add x
'(float 5 -
1))
185 (defun math-gamma-series (sum x xinvsqr oterm n
)
186 (math-working "gamma" sum
)
187 (let* ((bn (math-bernoulli-number n
))
188 (term (math-mul (math-div-float (math-float (nth 1 bn
))
189 (math-float (* (nth 2 bn
)
192 (next (math-add sum term
)))
193 (if (math-nearly-equal sum next
)
195 (if (> n
(* 2 calc-internal-prec
))
197 ;; Need this because series eventually diverges for large enough n.
199 "*Gamma computation stopped early, not all digits may be valid")
201 (math-gamma-series next
(math-mul x xinvsqr
) xinvsqr term
(+ n
2))))))
204 ;;; Incomplete gamma function.
206 (defvar math-current-gamma-value nil
)
207 (defun calcFunc-gammaP (a x
)
208 (if (equal x
'(var inf var-inf
))
210 (math-inexact-result)
211 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
212 (or (math-numberp x
) (math-reject-arg x
'numberp
))
213 (if (and (math-num-integerp a
)
214 (integerp (setq a
(math-trunc a
)))
216 (math-sub 1 (calcFunc-gammaQ a x
))
217 (let ((math-current-gamma-value (calcFunc-gamma a
)))
218 (math-div (calcFunc-gammag a x
) math-current-gamma-value
)))))
220 (defun calcFunc-gammaQ (a x
)
221 (if (equal x
'(var inf var-inf
))
223 (math-inexact-result)
224 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
225 (or (math-numberp x
) (math-reject-arg x
'numberp
))
226 (if (and (math-num-integerp a
)
227 (integerp (setq a
(math-trunc a
)))
232 (math-with-extra-prec 1
233 (while (< (setq n
(1+ n
)) a
)
234 (setq term
(math-div (math-mul term x
) n
)
235 sum
(math-add sum term
))
236 (math-working "gamma" sum
))
237 (math-mul sum
(calcFunc-exp (math-neg x
)))))
238 (let ((math-current-gamma-value (calcFunc-gamma a
)))
239 (math-div (calcFunc-gammaG a x
) math-current-gamma-value
)))))
241 (defun calcFunc-gammag (a x
)
242 (if (equal x
'(var inf var-inf
))
244 (math-inexact-result)
245 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
246 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
247 (math-with-extra-prec 2
248 (setq a
(math-float a
))
249 (setq x
(math-float x
))
250 (if (or (math-negp (calcFunc-re a
))
251 (math-lessp-float (calcFunc-re x
)
252 (math-add-float (calcFunc-re a
)
254 (math-inc-gamma-series a x
)
255 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
256 (math-inc-gamma-cfrac a x
))))))
258 (defun calcFunc-gammaG (a x
)
259 (if (equal x
'(var inf var-inf
))
261 (math-inexact-result)
262 (or (Math-numberp a
) (math-reject-arg a
'numberp
))
263 (or (Math-numberp x
) (math-reject-arg x
'numberp
))
264 (math-with-extra-prec 2
265 (setq a
(math-float a
))
266 (setq x
(math-float x
))
267 (if (or (math-negp (calcFunc-re a
))
268 (math-lessp-float (calcFunc-re x
)
269 (math-add-float (math-abs-approx a
)
271 (math-sub (or math-current-gamma-value
(calcFunc-gamma a
))
272 (math-inc-gamma-series a x
))
273 (math-inc-gamma-cfrac a x
)))))
275 (defun math-inc-gamma-series (a x
)
278 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
279 (math-with-extra-prec 2
280 (let ((start (math-div '(float 1 0) a
)))
281 (math-inc-gamma-series-step start start a x
))))))
283 (defun math-inc-gamma-series-step (sum term a x
)
284 (math-working "gamma" sum
)
285 (setq a
(math-add a
'(float 1 0))
286 term
(math-div (math-mul term x
) a
))
287 (let ((next (math-add sum term
)))
288 (if (math-nearly-equal sum next
)
290 (math-inc-gamma-series-step next term a x
))))
292 (defun math-inc-gamma-cfrac (a x
)
294 (or math-current-gamma-value
(calcFunc-gamma a
))
295 (math-mul (math-exp-raw (math-sub (math-mul a
(math-ln-raw x
)) x
))
296 (math-inc-gamma-cfrac-step '(float 1 0) x
297 '(float 0 0) '(float 1 0)
298 '(float 1 0) '(float 1 0) '(float 0 0)
301 (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x
)
302 (let ((ana (math-sub n a
))
303 (anf (math-mul n fac
)))
304 (setq n
(math-add n
'(float 1 0))
305 a0
(math-mul (math-add a1
(math-mul a0 ana
)) fac
)
306 b0
(math-mul (math-add b1
(math-mul b0 ana
)) fac
)
307 a1
(math-add (math-mul x a0
) (math-mul anf a1
))
308 b1
(math-add (math-mul x b0
) (math-mul anf b1
)))
310 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x
)
311 (setq fac
(math-div '(float 1 0) a1
))
312 (let ((next (math-mul b1 fac
)))
313 (math-working "gamma" next
)
314 (if (math-nearly-equal next g
)
316 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x
))))))
321 (defun calcFunc-erf (x)
322 (if (equal x
'(var inf var-inf
))
324 (if (equal x
'(neg (var inf var-inf
)))
328 (let ((math-current-gamma-value (math-sqrt-pi)))
329 (math-to-same-complex-quad
330 (math-div (calcFunc-gammag '(float 5 -
1)
331 (math-sqr (math-to-complex-quad-one x
)))
332 math-current-gamma-value
)
335 (defun calcFunc-erfc (x)
336 (if (equal x
'(var inf var-inf
))
339 (let ((math-current-gamma-value (math-sqrt-pi)))
340 (math-div (calcFunc-gammaG '(float 5 -
1) (math-sqr x
))
341 math-current-gamma-value
))
342 (math-sub 1 (calcFunc-erf x
)))))
344 (defun math-to-complex-quad-one (x)
345 (if (eq (car-safe x
) 'polar
) (setq x
(math-complex x
)))
346 (if (eq (car-safe x
) 'cplx
)
347 (list 'cplx
(math-abs (nth 1 x
)) (math-abs (nth 2 x
)))
350 (defun math-to-same-complex-quad (x y
)
351 (if (eq (car-safe y
) 'cplx
)
352 (if (eq (car-safe x
) 'cplx
)
354 (if (math-negp (nth 1 y
)) (math-neg (nth 1 x
)) (nth 1 x
))
355 (if (math-negp (nth 2 y
)) (math-neg (nth 2 x
)) (nth 2 x
)))
356 (if (math-negp (nth 1 y
)) (math-neg x
) x
))
358 (if (eq (car-safe x
) 'cplx
)
359 (list 'cplx
(math-neg (nth 1 x
)) (nth 2 x
))
366 (defun calcFunc-beta (a b
)
367 (if (math-num-integerp a
)
368 (let ((am (math-add a -
1)))
369 (or (math-numberp b
) (math-reject-arg b
'numberp
))
370 (math-div 1 (math-mul b
(calcFunc-choose (math-add b am
) am
))))
371 (if (math-num-integerp b
)
373 (math-div (math-mul (calcFunc-gamma a
) (calcFunc-gamma b
))
374 (calcFunc-gamma (math-add a b
))))))
377 ;;; Incomplete beta function.
379 (defvar math-current-beta-value nil
)
380 (defun calcFunc-betaI (x a b
)
381 (cond ((math-zerop x
)
383 ((math-equal-int x
1)
386 (and (math-num-integerp a
)
388 (if (or (math-zerop b
)
389 (and (math-num-integerp b
)
391 (math-reject-arg b
'range
)
394 (and (math-num-integerp b
)
397 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
398 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
399 ((math-inexact-result))
400 (t (let ((math-current-beta-value (calcFunc-beta a b
)))
401 (math-div (calcFunc-betaB x a b
) math-current-beta-value
)))))
403 (defun calcFunc-betaB (x a b
)
407 ((math-equal-int x
1)
409 ((not (math-numberp x
)) (math-reject-arg x
'numberp
))
410 ((not (math-numberp a
)) (math-reject-arg a
'numberp
))
411 ((not (math-numberp b
)) (math-reject-arg b
'numberp
))
412 ((math-zerop a
) (math-reject-arg a
'nonzerop
))
413 ((math-zerop b
) (math-reject-arg b
'nonzerop
))
414 ((and (math-num-integerp b
)
416 (math-reject-arg b
'range
)
417 (Math-natnum-lessp (setq b
(math-trunc b
)) 20)))
418 (and calc-symbolic-mode
(or (math-floatp a
) (math-floatp b
))
419 (math-inexact-result))
421 (math-with-extra-prec 2
424 (sum (math-div term a
)))
425 (while (< (setq i
(1+ i
)) b
)
426 (setq term
(math-mul (math-div (math-mul term
(- i b
)) i
) x
)
427 sum
(math-add sum
(math-div term
(math-add a i
))))
428 (math-working "beta" sum
))
431 ((and (math-num-integerp a
)
433 (math-reject-arg a
'range
)
434 (Math-natnum-lessp (setq a
(math-trunc a
)) 20)))
435 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
436 (calcFunc-betaB (math-sub 1 x
) b a
)))
438 (math-inexact-result)
439 (math-with-extra-prec 2
440 (setq x
(math-float x
))
441 (setq a
(math-float a
))
442 (setq b
(math-float b
))
443 (let ((bt (math-exp-raw (math-add (math-mul a
(math-ln-raw x
))
444 (math-mul b
(math-ln-raw
445 (math-sub '(float 1 0)
447 (if (Math-lessp x
(math-div (math-add a
'(float 1 0))
448 (math-add (math-add a b
) '(float 2 0))))
449 (math-div (math-mul bt
(math-beta-cfrac a b x
)) a
)
450 (math-sub (or math-current-beta-value
(calcFunc-beta a b
))
451 (math-div (math-mul bt
452 (math-beta-cfrac b a
(math-sub 1 x
)))
455 (defun math-beta-cfrac (a b x
)
456 (let ((qab (math-add a b
))
457 (qap (math-add a
'(float 1 0)))
458 (qam (math-add a
'(float -
1 0))))
459 (math-beta-cfrac-step '(float 1 0)
460 (math-sub '(float 1 0)
461 (math-div (math-mul qab x
) qap
))
462 '(float 1 0) '(float 1 0)
466 (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x
)
467 (let* ((two-m (math-mul m
'(float 2 0)))
468 (d (math-div (math-mul (math-mul (math-sub b m
) m
) x
)
469 (math-mul (math-add qam two-m
) (math-add a two-m
))))
470 (ap (math-add az
(math-mul d am
)))
471 (bp (math-add bz
(math-mul d bm
)))
473 (math-div (math-mul (math-mul (math-add a m
) (math-add qab m
)) x
)
474 (math-mul (math-add qap two-m
) (math-add a two-m
)))))
475 (app (math-add ap
(math-mul d2 az
)))
476 (bpp (math-add bp
(math-mul d2 bz
)))
477 (next (math-div app bpp
)))
478 (math-working "beta" next
)
479 (if (math-nearly-equal next az
)
481 (math-beta-cfrac-step next
'(float 1 0)
482 (math-div ap bpp
) (math-div bp bpp
)
483 (math-add m
'(float 1 0))
484 qab qap qam a b x
))))
487 ;;; Bessel functions.
489 ;;; Should generalize this to handle arbitrary precision!
491 (defun calcFunc-besJ (v x
)
492 (or (math-numberp v
) (math-reject-arg v
'numberp
))
493 (or (math-numberp x
) (math-reject-arg x
'numberp
))
494 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
495 (math-with-extra-prec 3
496 (setq x
(math-float (math-normalize x
)))
497 (setq v
(math-float (math-normalize v
)))
498 (cond ((math-zerop x
)
502 ((math-inexact-result))
503 ((not (math-num-integerp v
))
504 (let ((start (math-div 1 (calcFunc-fact v
))))
505 (math-mul (math-besJ-series start start
507 (math-mul '(float -
25 -
2)
510 (math-pow (math-div x
2) v
))))
511 ((math-negp (setq v
(math-trunc v
)))
513 (math-neg (calcFunc-besJ (math-neg v
) x
))
514 (calcFunc-besJ (math-neg v
) x
)))
519 ((Math-lessp v
(math-abs-approx x
))
523 (two-over-x (math-div 2 x
))
525 (while (< (setq j
(1+ j
)) v
)
526 (setq bjp
(math-sub (math-mul (math-mul j two-over-x
) bj
)
532 (if (Math-lessp 100 v
) (math-reject-arg v
'range
))
533 (let* ((j (logior (+ v
(math-isqrt-small (* 40 v
))) 1))
534 (two-over-x (math-div 2 x
))
540 (while (> (setq j
(1- j
)) 0)
541 (setq bjm
(math-sub (math-mul (math-mul j two-over-x
) bj
)
545 (if (> (nth 2 (math-abs-approx bj
)) 10)
546 (setq bj
(math-mul bj
'(float 1 -
10))
547 bjp
(math-mul bjp
'(float 1 -
10))
548 ans
(and ans
(math-mul ans
'(float 1 -
10)))
549 sum
(math-mul sum
'(float 1 -
10))))
550 (or (setq jsum
(not jsum
))
551 (setq sum
(math-add sum bj
)))
554 (math-div ans
(math-sub (math-mul 2 sum
) bj
))))))))
556 (defun math-besJ-series (sum term k zz vk
)
557 (math-working "besJ" sum
)
560 term
(math-div (math-mul term zz
) (math-mul k vk
)))
561 (let ((next (math-add sum term
)))
562 (if (math-nearly-equal next sum
)
564 (math-besJ-series next term k zz vk
))))
566 (defun math-besJ0 (x &optional yflag
)
567 (cond ((and (not yflag
) (math-negp (calcFunc-re x
)))
568 (math-besJ0 (math-neg x
)))
569 ((Math-lessp '(float 8 0) (math-abs-approx x
))
570 (let* ((z (math-div '(float 8 0) x
))
572 (xx (math-add x
'(float (bigneg 164 398 785) -
9)))
573 (a1 (math-poly-eval y
574 '((float (bigpos 211 887 093 2) -
16)
575 (float (bigneg 639 370 073 2) -
15)
576 (float (bigpos 407 510 734 2) -
14)
577 (float (bigneg 627 628 098 1) -
12)
579 (a2 (math-poly-eval y
580 '((float (bigneg 152 935 934) -
16)
581 (float (bigpos 161 095 621 7) -
16)
582 (float (bigneg 651 147 911 6) -
15)
583 (float (bigpos 765 488 430 1) -
13)
584 (float (bigneg 995 499 562 1) -
11))))
585 (sc (math-sin-cos-raw xx
)))
587 (setq sc
(cons (math-neg (cdr sc
)) (car sc
))))
589 (math-div '(float (bigpos 722 619 636) -
9) x
))
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
595 '((float (bigneg 456 052 849 1) -
7)
596 (float (bigpos 017 233 739 7) -
5)
597 (float (bigneg 418 442 121 1) -
2)
598 (float (bigpos 407 196 516 6) -
1)
599 (float (bigneg 354 590 362 13) 0)
600 (float (bigpos 574 490 568 57) 0)))
603 (float (bigpos 712 532 678 2) -
7)
604 (float (bigpos 853 264 927 5) -
5)
605 (float (bigpos 718 680 494 9) -
3)
606 (float (bigpos 985 532 029 1) 0)
607 (float (bigpos 411 490 568 57) 0))))))))
609 (defun math-besJ1 (x &optional yflag
)
610 (cond ((and (math-negp (calcFunc-re x
)) (not yflag
))
611 (math-neg (math-besJ1 (math-neg x
))))
612 ((Math-lessp '(float 8 0) (math-abs-approx x
))
613 (let* ((z (math-div '(float 8 0) x
))
615 (xx (math-add x
'(float (bigneg 491 194 356 2) -
9)))
616 (a1 (math-poly-eval y
617 '((float (bigneg 019 337 240) -
15)
618 (float (bigpos 174 520 457 2) -
15)
619 (float (bigneg 496 396 516 3) -
14)
622 (a2 (math-poly-eval y
623 '((float (bigpos 412 787 105) -
15)
624 (float (bigneg 987 228 88) -
14)
625 (float (bigpos 096 199 449 8) -
15)
626 (float (bigneg 873 690 002 2) -
13)
627 (float (bigpos 995 499 687 4) -
11))))
628 (sc (math-sin-cos-raw xx
)))
630 (setq sc
(cons (math-neg (cdr sc
)) (car sc
)))
632 (setq sc
(cons (math-neg (car sc
)) (math-neg (cdr sc
))))))
633 (math-mul (math-sqrt (math-div '(float (bigpos 722 619 636) -
9) x
))
634 (math-sub (math-mul (cdr sc
) a1
)
635 (math-mul (car sc
) (math-mul z a2
))))))
637 (let ((y (math-sqr x
)))
640 (math-div (math-poly-eval y
641 '((float (bigneg 606 036 016 3) -
8)
642 (float (bigpos 826 044 157) -
4)
643 (float (bigneg 439 611 972 2) -
3)
644 (float (bigpos 531 968 423 2) -
1)
645 (float (bigneg 235 059 895 7) 0)
646 (float (bigpos 232 614 362 72) 0)))
649 (float (bigpos 397 991 769 3) -
7)
650 (float (bigpos 394 743 944 9) -
5)
651 (float (bigpos 474 330 858 1) -
2)
652 (float (bigpos 178 535 300 2) 0)
653 (float (bigpos 442 228 725 144)
656 (defun calcFunc-besY (v x
)
657 (math-inexact-result)
658 (or (math-numberp v
) (math-reject-arg v
'numberp
))
659 (or (math-numberp x
) (math-reject-arg x
'numberp
))
660 (let ((calc-internal-prec (min 8 calc-internal-prec
)))
661 (math-with-extra-prec 3
662 (setq x
(math-float (math-normalize x
)))
663 (setq v
(math-float (math-normalize v
)))
664 (cond ((not (math-num-integerp v
))
665 (let ((sc (math-sin-cos-raw (math-mul v
(math-pi)))))
666 (math-div (math-sub (math-mul (calcFunc-besJ v x
) (cdr sc
))
667 (calcFunc-besJ (math-neg v
) x
))
669 ((math-negp (setq v
(math-trunc v
)))
671 (math-neg (calcFunc-besY (math-neg v
) x
))
672 (calcFunc-besY (math-neg v
) x
)))
681 (two-over-x (math-div 2 x
))
683 (while (< (setq j
(1+ j
)) v
)
684 (setq byp
(math-sub (math-mul (math-mul j two-over-x
) by
)
690 (defun math-besY0 (x)
691 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
692 (let ((y (math-sqr x
)))
694 (math-div (math-poly-eval y
695 '((float (bigpos 733 622 284 2) -
7)
696 (float (bigneg 757 792 632 8) -
5)
697 (float (bigpos 129 988 087 1) -
2)
698 (float (bigneg 036 598 123 5) -
1)
699 (float (bigpos 065 834 062 7) 0)
700 (float (bigneg 389 821 957 2) 0)))
703 (float (bigpos 244 030 261 2) -
7)
704 (float (bigpos 647 472 474) -
4)
705 (float (bigpos 438 466 189 7) -
3)
706 (float (bigpos 648 499 452 7) -
1)
707 (float (bigpos 269 544 076 40) 0))))
708 (math-mul '(float (bigpos 772 619 636) -
9)
709 (math-mul (math-besJ0 x
) (math-ln-raw x
))))))
710 ((math-negp (calcFunc-re x
))
711 (math-add (math-besJ0 (math-neg x
) t
)
712 (math-mul '(cplx 0 2)
713 (math-besJ0 (math-neg x
)))))
717 (defun math-besY1 (x)
718 (cond ((Math-lessp (math-abs-approx x
) '(float 8 0))
719 (let ((y (math-sqr x
)))
723 (math-div (math-poly-eval y
724 '((float (bigpos 935 937 511 8) -
6)
725 (float (bigneg 726 922 237 4) -
3)
726 (float (bigpos 551 264 349 7) -
1)
727 (float (bigneg 139 438 153 5) 1)
728 (float (bigpos 439 527 127) 4)
729 (float (bigneg 943 604 900 4) 3)))
732 (float (bigpos 885 632 549 3) -
7)
733 (float (bigpos 605 042 102) -
3)
734 (float (bigpos 002 904 245 2) -
2)
735 (float (bigpos 367 650 733 3) 0)
736 (float (bigpos 664 419 244 4) 2)
737 (float (bigpos 057 958 249) 5)))))
738 (math-mul '(float (bigpos 772 619 636) -
9)
739 (math-sub (math-mul (math-besJ1 x
) (math-ln-raw x
))
741 ((math-negp (calcFunc-re x
))
743 (math-add (math-besJ1 (math-neg x
) t
)
744 (math-mul '(cplx 0 2)
745 (math-besJ1 (math-neg x
))))))
749 (defun math-poly-eval (x coefs
)
750 (let ((accum (car coefs
)))
751 (while (setq coefs
(cdr coefs
))
752 (setq accum
(math-add (car coefs
) (math-mul accum x
))))
756 ;;;; Bernoulli and Euler polynomials and numbers.
758 (defun calcFunc-bern (n &optional x
)
759 (if (and x
(not (math-zerop x
)))
760 (if (and calc-symbolic-mode
(math-floatp x
))
761 (math-inexact-result)
762 (math-build-polynomial-expr (math-bernoulli-coefs n
) x
))
763 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
766 (math-inexact-result)
767 (math-float (math-bernoulli-number (math-trunc n
))))
768 (math-bernoulli-number n
))))
770 (defun calcFunc-euler (n &optional x
)
771 (or (math-num-natnump n
) (math-reject-arg n
'natnump
))
773 (let* ((n1 (math-add n
1))
774 (coefs (math-bernoulli-coefs n1
))
775 (fac (math-div (math-pow 2 n1
) n1
))
777 (x1 (math-div (math-add x
1) 2))
780 (if (and calc-symbolic-mode
(math-floatp x
))
781 (math-inexact-result)
783 (math-sub (math-build-polynomial-expr coefs x1
)
784 (math-build-polynomial-expr coefs x2
))))
792 (math-mul (math-mul fac c
)
793 (math-sub (math-pow x1 k
)
797 (math-mul (math-pow 2 n
)
800 (math-inexact-result)
801 (calcFunc-euler n
'(float 5 -
1)))
802 (calcFunc-euler n
'(frac 1 2))))))
804 (defvar math-bernoulli-b-cache
'((frac -
174611
805 (bigpos 0 200 291 698 662 857 802))
806 (frac 43867 (bigpos 0 944 170 217 94 109 5))
807 (frac -
3617 (bigpos 0 880 842 622 670 10))
808 (frac 1 (bigpos 600 249 724 74))
809 (frac -
691 (bigpos 0 368 674 307 1))
810 (frac 1 (bigpos 160 900 47))
811 (frac -
1 (bigpos 600 209 1))
812 (frac 1 30240) (frac -
1 720)
815 (defvar math-bernoulli-B-cache
'((frac -
174611 330) (frac 43867 798)
816 (frac -
3617 510) (frac 7 6) (frac -
691 2730)
817 (frac 5 66) (frac -
1 30) (frac 1 42)
818 (frac -
1 30) (frac 1 6) 1 ))
820 (defvar math-bernoulli-cache-size
11)
821 (defun math-bernoulli-coefs (n)
822 (let* ((coefs (list (calcFunc-bern n
)))
827 (calc-prefer-frac (or (integerp n
) calc-prefer-frac
)))
828 (while (>= (setq k
(1- k
)) 0)
829 (setq term
(math-div term
(- nn k
))
830 coef
(math-mul term
(math-bernoulli-number k
))
831 coefs
(cons (if (consp n
) (math-float coef
) coef
) coefs
)
832 term
(math-mul term k
)))
835 (defun math-bernoulli-number (n)
841 (while (>= n math-bernoulli-cache-size
)
844 (fact 1) ; fact = (n-k+1)!
846 (p math-bernoulli-b-cache
)
847 (calc-prefer-frac t
))
848 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size
))
852 fact
(math-mul fact
(* nk
(1- nk
)))
853 sum
(math-add sum
(math-div (car p
) fact
))
855 (setq ofact
(math-mul ofact
(1- nk
))
856 sum
(math-sub (math-div '(frac 1 2) ofact
) sum
)
857 math-bernoulli-b-cache
(cons sum math-bernoulli-b-cache
)
858 math-bernoulli-B-cache
(cons (math-mul sum ofact
)
859 math-bernoulli-B-cache
)
860 math-bernoulli-cache-size
(1+ math-bernoulli-cache-size
))))
861 (nth (- math-bernoulli-cache-size n
1) math-bernoulli-B-cache
)))
864 ;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
866 ;;; A faster method would be to use "tangent numbers", c.f., Concrete
867 ;;; Mathematics pg. 273.
870 ;;; Probability distributions.
873 (defun calcFunc-utpb (x n p
)
874 (if math-expand-formulas
875 (math-normalize (list 'calcFunc-betaI p x
(list '+ (list '- n x
) 1)))
876 (calcFunc-betaI p x
(math-add (math-sub n x
) 1))))
877 (put 'calcFunc-utpb
'math-expandable t
)
879 (defun calcFunc-ltpb (x n p
)
880 (math-sub 1 (calcFunc-utpb x n p
)))
881 (put 'calcFunc-ltpb
'math-expandable t
)
884 (defun calcFunc-utpc (chisq v
)
885 (if math-expand-formulas
886 (math-normalize (list 'calcFunc-gammaQ
(list '/ v
2) (list '/ chisq
2)))
887 (calcFunc-gammaQ (math-div v
2) (math-div chisq
2))))
888 (put 'calcFunc-utpc
'math-expandable t
)
890 (defun calcFunc-ltpc (chisq v
)
891 (if math-expand-formulas
892 (math-normalize (list 'calcFunc-gammaP
(list '/ v
2) (list '/ chisq
2)))
893 (calcFunc-gammaP (math-div v
2) (math-div chisq
2))))
894 (put 'calcFunc-ltpc
'math-expandable t
)
897 (defun calcFunc-utpf (f v1 v2
)
898 (if math-expand-formulas
899 (math-normalize (list 'calcFunc-betaI
900 (list '/ v2
(list '+ v2
(list '* v1 f
)))
903 (calcFunc-betaI (math-div v2
(math-add v2
(math-mul v1 f
)))
906 (put 'calcFunc-utpf
'math-expandable t
)
908 (defun calcFunc-ltpf (f v1 v2
)
909 (math-sub 1 (calcFunc-utpf f v1 v2
)))
910 (put 'calcFunc-ltpf
'math-expandable t
)
913 (defun calcFunc-utpn (x mean sdev
)
914 (if math-expand-formulas
919 (list '/ (list '- mean x
)
920 (list '* sdev
(list 'calcFunc-sqrt
2)))))
922 (math-mul (math-add '(float 1 0)
924 (math-div (math-sub mean x
)
925 (math-mul sdev
(math-sqrt-2)))))
927 (put 'calcFunc-utpn
'math-expandable t
)
929 (defun calcFunc-ltpn (x mean sdev
)
930 (if math-expand-formulas
935 (list '/ (list '- x mean
)
936 (list '* sdev
(list 'calcFunc-sqrt
2)))))
938 (math-mul (math-add '(float 1 0)
940 (math-div (math-sub x mean
)
941 (math-mul sdev
(math-sqrt-2)))))
943 (put 'calcFunc-ltpn
'math-expandable t
)
946 (defun calcFunc-utpp (n x
)
947 (if math-expand-formulas
948 (math-normalize (list 'calcFunc-gammaP x n
))
949 (calcFunc-gammaP x n
)))
950 (put 'calcFunc-utpp
'math-expandable t
)
952 (defun calcFunc-ltpp (n x
)
953 (if math-expand-formulas
954 (math-normalize (list 'calcFunc-gammaQ x n
))
955 (calcFunc-gammaQ x n
)))
956 (put 'calcFunc-ltpp
'math-expandable t
)
958 ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
959 (defun calcFunc-utpt (tt v
)
960 (if math-expand-formulas
961 (math-normalize (list 'calcFunc-betaI
962 (list '/ v
(list '+ v
(list '^ tt
2)))
965 (calcFunc-betaI (math-div v
(math-add v
(math-sqr tt
)))
968 (put 'calcFunc-utpt
'math-expandable t
)
970 (defun calcFunc-ltpt (tt v
)
971 (math-sub 1 (calcFunc-utpt tt v
)))
972 (put 'calcFunc-ltpt
'math-expandable t
)
975 ;;; calc-funcs.el ends here