Standardize case of "GTK+" in a few manuals
[emacs.git] / lisp / calc / calc-funcs.el
blobb5857a8bbbf9b70a810a5b3e368eeeb3d132e3ab
1 ;;; calc-funcs.el --- well-known functions for Calc
3 ;; Copyright (C) 1990-1993, 2001-2012 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/>.
23 ;;; Commentary:
25 ;;; Code:
27 ;; This file is autoloaded from calc-ext.el.
29 (require 'calc-ext)
30 (require 'calc-macs)
32 (defun calc-inc-gamma (arg)
33 (interactive "P")
34 (calc-slow-wrapper
35 (if (calc-is-inverse)
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)))))
43 (defun calc-erf (arg)
44 (interactive "P")
45 (calc-slow-wrapper
46 (if (calc-is-inverse)
47 (calc-unary-op "erfc" 'calcFunc-erfc arg)
48 (calc-unary-op "erf" 'calcFunc-erf arg))))
50 (defun calc-erfc (arg)
51 (interactive "P")
52 (calc-invert-func)
53 (calc-erf arg))
55 (defun calc-beta (arg)
56 (interactive "P")
57 (calc-slow-wrapper
58 (calc-binary-op "beta" 'calcFunc-beta arg)))
60 (defun calc-inc-beta ()
61 (interactive)
62 (calc-slow-wrapper
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)
68 (interactive "P")
69 (calc-slow-wrapper
70 (calc-binary-op "besJ" 'calcFunc-besJ arg)))
72 (defun calc-bessel-Y (arg)
73 (interactive "P")
74 (calc-slow-wrapper
75 (calc-binary-op "besY" 'calcFunc-besY arg)))
77 (defun calc-bernoulli-number (arg)
78 (interactive "P")
79 (calc-slow-wrapper
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)
85 (interactive "P")
86 (calc-slow-wrapper
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)
92 (interactive "P")
93 (calc-slow-wrapper
94 (if (calc-is-hyperbolic)
95 (calc-binary-op "str2" 'calcFunc-stir2 arg)
96 (calc-binary-op "str1" 'calcFunc-stir1 arg))))
98 (defun calc-utpb ()
99 (interactive)
100 (calc-prob-dist "b" 3))
102 (defun calc-utpc ()
103 (interactive)
104 (calc-prob-dist "c" 2))
106 (defun calc-utpf ()
107 (interactive)
108 (calc-prob-dist "f" 3))
110 (defun calc-utpn ()
111 (interactive)
112 (calc-prob-dist "n" 3))
114 (defun calc-utpp ()
115 (interactive)
116 (calc-prob-dist "p" 2))
118 (defun calc-utpt ()
119 (interactive)
120 (calc-prob-dist "t" 2))
122 (defun calc-prob-dist (letter nargs)
123 (calc-slow-wrapper
124 (if (calc-is-inverse)
125 (calc-enter-result nargs (concat "ltp" letter)
126 (append (list (intern (concat "calcFunc-ltp" letter))
127 (calc-top-n 1))
128 (calc-top-list-n (1- nargs) 2)))
129 (calc-enter-result nargs (concat "utp" letter)
130 (append (list (intern (concat "calcFunc-utp" letter))
131 (calc-top-n 1))
132 (calc-top-list-n (1- nargs) 2))))))
137 ;;; Sources: Numerical Recipes, Press et al;
138 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
141 ;;; Gamma function.
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."
149 (or fprec
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)
154 (math-neg (math-div
155 (math-pi)
156 (math-mul (math-gammap1-raw
157 (math-add (math-neg x)
158 '(float -1 0))
159 fprec nfprec)
160 (math-sin-raw
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))))
164 ((and (math-realp x)
165 (math-lessp-float '(float 736276 0) x))
166 (math-overflow))
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)
171 (math-exp-raw
172 (math-gamma-series
173 (math-sub (math-mul (math-add x '(float 5 -1))
174 lnx)
176 xinv
177 (math-sqr xinv)
178 '(float 0 0)
179 2)))))))
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)
186 (* n (1- n)))))
188 (next (math-add sum term)))
189 (if (math-nearly-equal sum next)
190 next
191 (if (> n (* 2 calc-internal-prec))
192 (progn
193 ;; Need this because series eventually diverges for large enough n.
194 (calc-record-why
195 "*Gamma computation stopped early, not all digits may be valid")
196 next)
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))
205 '(float 1 0)
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)))
211 (> a 0) (< a 20))
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))
218 '(float 0 0)
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)))
224 (> a 0) (< a 20))
225 (let ((n 0)
226 (sum '(float 1 0))
227 (term '(float 1 0)))
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))
239 (calcFunc-gamma a)
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)
249 '(float 1 0))))
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))
256 '(float 0 0)
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)
266 '(float 1 0))))
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)
272 (if (Math-zerop x)
273 '(float 0 0)
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)
285 next
286 (math-inc-gamma-series-step next term a x))))
288 (defun math-inc-gamma-cfrac (a x)
289 (if (Math-zerop 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)
295 a x))))
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)))
305 (if (math-zerop a1)
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)
311 next
312 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x))))))
315 ;;; Error function.
317 (defun calcFunc-erf (x)
318 (if (equal x '(var inf var-inf))
319 '(float 1 0)
320 (if (equal x '(neg (var inf var-inf)))
321 '(float -1 0)
322 (if (Math-zerop x)
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)
329 x))))))
331 (defun calcFunc-erfc (x)
332 (if (equal x '(var inf var-inf))
333 '(float 0 0)
334 (if (math-posp x)
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)
349 (list '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))
353 (if (math-negp y)
354 (if (eq (car-safe x) 'cplx)
355 (list 'cplx (math-neg (nth 1 x)) (nth 2 x))
356 (math-neg x))
357 x)))
360 ;;; Beta function.
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)
368 (calcFunc-beta b a)
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)
378 '(float 0 0))
379 ((math-equal-int x 1)
380 '(float 1 0))
381 ((or (math-zerop a)
382 (and (math-num-integerp a)
383 (math-negp a)))
384 (if (or (math-zerop b)
385 (and (math-num-integerp b)
386 (math-negp b)))
387 (math-reject-arg b 'range)
388 '(float 1 0)))
389 ((or (math-zerop b)
390 (and (math-num-integerp b)
391 (math-negp b)))
392 '(float 0 0))
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)
400 (cond
401 ((math-zerop x)
402 '(float 0 0))
403 ((math-equal-int x 1)
404 (calcFunc-beta a b))
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)
411 (if (math-negp 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))
416 (math-mul
417 (math-with-extra-prec 2
418 (let* ((i 0)
419 (term 1)
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))
425 sum))
426 (math-pow x a)))
427 ((and (math-num-integerp a)
428 (if (math-negp 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)
442 x)))))))
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)))
449 b))))))))
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)
459 '(float 1 0)
460 qab qap qam a b x)))
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)))
468 (d2 (math-neg
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)
476 next
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)
495 (if (math-zerop v)
496 '(float 1 0)
497 '(float 0 0)))
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)
504 (math-sqr x))
506 (math-pow (math-div x 2) v))))
507 ((math-negp (setq v (math-trunc v)))
508 (if (math-oddp v)
509 (math-neg (calcFunc-besJ (math-neg v) x))
510 (calcFunc-besJ (math-neg v) x)))
511 ((eq v 0)
512 (math-besJ0 x))
513 ((eq v 1)
514 (math-besJ1 x))
515 ((Math-lessp v (math-abs-approx x))
516 (let ((j 0)
517 (bjm (math-besJ0 x))
518 (bj (math-besJ1 x))
519 (two-over-x (math-div 2 x))
520 bjp)
521 (while (< (setq j (1+ j)) v)
522 (setq bjp (math-sub (math-mul (math-mul j two-over-x) bj)
523 bjm)
524 bjm bj
525 bj bjp))
526 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))
531 (jsum nil)
532 (bjp '(float 0 0))
533 (sum '(float 0 0))
534 (bj '(float 1 0))
535 bjm ans)
536 (while (> (setq j (1- j)) 0)
537 (setq bjm (math-sub (math-mul (math-mul j two-over-x) bj)
538 bjp)
539 bjp bj
540 bj bjm)
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)))
548 (if (= j v)
549 (setq ans bjp)))
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)
554 (setq k (1+ k)
555 vk (math-add 1 vk)
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)
559 next
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))
567 (y (math-sqr z))
568 (xx (math-add x
569 (math-read-number-simple "-0.785398164")))
570 (a1 (math-poly-eval y
571 (list
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")
576 '(float 1 0))))
577 (a2 (math-poly-eval y
578 (list
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)))
585 (if yflag
586 (setq sc (cons (math-neg (cdr sc)) (car sc))))
587 (math-mul (math-sqrt
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
595 (list
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")))
602 (math-poly-eval y
603 (list
604 '(float 1 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))
616 (y (math-sqr z))
617 (xx (math-add x (math-read-number-simple "-2.356194491")))
618 (a1 (math-poly-eval y
619 (list
620 (math-read-number-simple "-0.000000240337019")
621 (math-read-number-simple "0.000002457520174")
622 (math-read-number-simple "-0.00003516396496")
623 '(float 183105 -8)
624 '(float 1 0))))
625 (a2 (math-poly-eval y
626 (list
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)))
633 (if yflag
634 (setq sc (cons (math-neg (cdr sc)) (car sc)))
635 (if (math-negp x)
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)))
644 (math-mul
646 (math-div (math-poly-eval y
647 (list
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")))
654 (math-poly-eval y
655 (list
656 '(float 1 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))
675 (car sc))))
676 ((math-negp (setq v (math-trunc v)))
677 (if (math-oddp v)
678 (math-neg (calcFunc-besY (math-neg v) x))
679 (calcFunc-besY (math-neg v) x)))
680 ((eq v 0)
681 (math-besY0 x))
682 ((eq v 1)
683 (math-besY1 x))
685 (let ((j 0)
686 (bym (math-besY0 x))
687 (by (math-besY1 x))
688 (two-over-x (math-div 2 x))
689 byp)
690 (while (< (setq j (1+ j)) v)
691 (setq byp (math-sub (math-mul (math-mul j two-over-x) by)
692 bym)
693 bym by
694 by byp))
695 by))))))
697 (defun math-besY0 (x)
698 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
699 (let ((y (math-sqr x)))
700 (math-add
701 (math-div (math-poly-eval y
702 (list
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")))
709 (math-poly-eval y
710 (list
711 '(float 1 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)))))
724 (math-besJ0 x t))))
726 (defun math-besY1 (x)
727 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
728 (let ((y (math-sqr x)))
729 (math-add
730 (math-mul
732 (math-div (math-poly-eval y
733 (list
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")))
740 (math-poly-eval y
741 (list
742 '(float 1 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))
751 (math-div 1 x))))))
752 ((math-negp (calcFunc-re x))
753 (math-neg
754 (math-add (math-besJ1 (math-neg x) t)
755 (math-mul '(cplx 0 2)
756 (math-besJ1 (math-neg x))))))
758 (math-besJ1 x t))))
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))))
764 accum))
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))
775 (if (consp n)
776 (progn
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))
783 (if x
784 (let* ((n1 (math-add n 1))
785 (coefs (math-bernoulli-coefs n1))
786 (fac (math-div (math-pow 2 n1) n1))
787 (k -1)
788 (x1 (math-div (math-add x 1) 2))
789 (x2 (math-div x 2)))
790 (if (math-numberp x)
791 (if (and calc-symbolic-mode (math-floatp x))
792 (math-inexact-result)
793 (math-mul fac
794 (math-sub (math-build-polynomial-expr coefs x1)
795 (math-build-polynomial-expr coefs x2))))
796 (calcFunc-collect
797 (math-reduce-vec
798 'math-add
799 (cons 'vec
800 (mapcar (function
801 (lambda (c)
802 (setq k (1+ k))
803 (math-mul (math-mul fac c)
804 (math-sub (math-pow x1 k)
805 (math-pow x2 k)))))
806 coefs)))
807 x)))
808 (math-mul (math-pow 2 n)
809 (if (consp n)
810 (progn
811 (math-inexact-result)
812 (calcFunc-euler n '(float 5 -1)))
813 (calcFunc-euler n '(frac 1 2))))))
815 (defvar math-bernoulli-b-cache
816 (list
817 (list 'frac
818 -174611
819 (math-read-number-simple "802857662698291200000"))
820 (list 'frac
821 43867
822 (math-read-number-simple "5109094217170944000"))
823 (list 'frac
824 -3617
825 (math-read-number-simple "10670622842880000"))
826 (list 'frac
828 (math-read-number-simple "74724249600"))
829 (list 'frac
830 -691
831 (math-read-number-simple "1307674368000"))
832 (list 'frac
834 (math-read-number-simple "47900160"))
835 (list 'frac
837 (math-read-number-simple "1209600"))
838 (list 'frac
840 30240)
841 (list 'frac
843 720)
844 (list 'frac
846 12)
847 1 ))
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)))
858 (nn (math-trunc n))
859 (k nn)
860 (term nn)
861 coef
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)))
868 (nreverse coefs)))
870 (defun math-bernoulli-number (n)
871 (if (= (% n 2) 1)
872 (if (= n 1)
873 '(frac -1 2)
875 (setq n (/ n 2))
876 (while (>= n math-bernoulli-cache-size)
877 (let* ((sum 0)
878 (nk 1) ; nk = n-k+1
879 (fact 1) ; fact = (n-k+1)!
880 ofact
881 (p math-bernoulli-b-cache)
882 (calc-prefer-frac t))
883 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size))
884 (while p
885 (setq nk (+ nk 2)
886 ofact fact
887 fact (math-mul fact (* nk (1- nk)))
888 sum (math-add sum (math-div (car p) fact))
889 p (cdr p)))
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)))
898 ;;; Bn = n! bn
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.
907 ;;; Binomial.
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)
918 ;;; Chi-square.
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)
931 ;;; F-distribution.
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)))
936 (list '/ v2 2)
937 (list '/ v1 2)))
938 (calcFunc-betaI (math-div v2 (math-add v2 (math-mul v1 f)))
939 (math-div v2 2)
940 (math-div v1 2))))
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)
947 ;;; Normal.
948 (defun calcFunc-utpn (x mean sdev)
949 (if math-expand-formulas
950 (math-normalize
951 (list '/
952 (list '+ 1
953 (list 'calcFunc-erf
954 (list '/ (list '- mean x)
955 (list '* sdev (list 'calcFunc-sqrt 2)))))
957 (math-mul (math-add '(float 1 0)
958 (calcFunc-erf
959 (math-div (math-sub mean x)
960 (math-mul sdev (math-sqrt-2)))))
961 '(float 5 -1))))
962 (put 'calcFunc-utpn 'math-expandable t)
964 (defun calcFunc-ltpn (x mean sdev)
965 (if math-expand-formulas
966 (math-normalize
967 (list '/
968 (list '+ 1
969 (list 'calcFunc-erf
970 (list '/ (list '- x mean)
971 (list '* sdev (list 'calcFunc-sqrt 2)))))
973 (math-mul (math-add '(float 1 0)
974 (calcFunc-erf
975 (math-div (math-sub x mean)
976 (math-mul sdev (math-sqrt-2)))))
977 '(float 5 -1))))
978 (put 'calcFunc-ltpn 'math-expandable t)
980 ;;; Poisson.
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)))
998 (list '/ v 2)
999 '(float 5 -1)))
1000 (calcFunc-betaI (math-div v (math-add v (math-sqr tt)))
1001 (math-div v 2)
1002 '(float 5 -1))))
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