(math-julian-date-beginning,math-julian-date-beginning-int) New constants.
[bpt/emacs.git] / lisp / calc / calc-funcs.el
CommitLineData
12f1951a
CW
1;;; calc-funcs.el --- well-known functions for Calc
2
58ba2f8f 3;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
f0fa15c5 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
12f1951a
CW
5
6;; Author: David Gillespie <daveg@synaptics.com>
e8fff8ed 7;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
136211a9
EZ
8
9;; This file is part of GNU Emacs.
10
7c671b23
GM
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)
14;; any later version.
15
136211a9 16;; GNU Emacs is distributed in the hope that it will be useful,
7c671b23
GM
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.
20
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.
136211a9 25
12f1951a 26;;; Commentary:
136211a9 27
12f1951a 28;;; Code:
136211a9
EZ
29
30;; This file is autoloaded from calc-ext.el.
136211a9 31
300f28d3 32(require 'calc-ext)
136211a9
EZ
33(require 'calc-macs)
34
136211a9
EZ
35(defun calc-inc-gamma (arg)
36 (interactive "P")
37 (calc-slow-wrapper
38 (if (calc-is-inverse)
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)
bf77c646 44 (calc-binary-op "gamP" 'calcFunc-gammaP arg)))))
136211a9
EZ
45
46(defun calc-erf (arg)
47 (interactive "P")
48 (calc-slow-wrapper
49 (if (calc-is-inverse)
50 (calc-unary-op "erfc" 'calcFunc-erfc arg)
bf77c646 51 (calc-unary-op "erf" 'calcFunc-erf arg))))
136211a9
EZ
52
53(defun calc-erfc (arg)
54 (interactive "P")
55 (calc-invert-func)
bf77c646 56 (calc-erf arg))
136211a9
EZ
57
58(defun calc-beta (arg)
59 (interactive "P")
60 (calc-slow-wrapper
bf77c646 61 (calc-binary-op "beta" 'calcFunc-beta arg)))
136211a9
EZ
62
63(defun calc-inc-beta ()
64 (interactive)
65 (calc-slow-wrapper
66 (if (calc-is-hyperbolic)
67 (calc-enter-result 3 "betB" (cons 'calcFunc-betaB (calc-top-list-n 3)))
bf77c646 68 (calc-enter-result 3 "betI" (cons 'calcFunc-betaI (calc-top-list-n 3))))))
136211a9
EZ
69
70(defun calc-bessel-J (arg)
71 (interactive "P")
72 (calc-slow-wrapper
bf77c646 73 (calc-binary-op "besJ" 'calcFunc-besJ arg)))
136211a9
EZ
74
75(defun calc-bessel-Y (arg)
76 (interactive "P")
77 (calc-slow-wrapper
bf77c646 78 (calc-binary-op "besY" 'calcFunc-besY arg)))
136211a9
EZ
79
80(defun calc-bernoulli-number (arg)
81 (interactive "P")
82 (calc-slow-wrapper
83 (if (calc-is-hyperbolic)
84 (calc-binary-op "bern" 'calcFunc-bern arg)
bf77c646 85 (calc-unary-op "bern" 'calcFunc-bern arg))))
136211a9
EZ
86
87(defun calc-euler-number (arg)
88 (interactive "P")
89 (calc-slow-wrapper
90 (if (calc-is-hyperbolic)
91 (calc-binary-op "eulr" 'calcFunc-euler arg)
bf77c646 92 (calc-unary-op "eulr" 'calcFunc-euler arg))))
136211a9
EZ
93
94(defun calc-stirling-number (arg)
95 (interactive "P")
96 (calc-slow-wrapper
97 (if (calc-is-hyperbolic)
98 (calc-binary-op "str2" 'calcFunc-stir2 arg)
bf77c646 99 (calc-binary-op "str1" 'calcFunc-stir1 arg))))
136211a9
EZ
100
101(defun calc-utpb ()
102 (interactive)
bf77c646 103 (calc-prob-dist "b" 3))
136211a9
EZ
104
105(defun calc-utpc ()
106 (interactive)
bf77c646 107 (calc-prob-dist "c" 2))
136211a9
EZ
108
109(defun calc-utpf ()
110 (interactive)
bf77c646 111 (calc-prob-dist "f" 3))
136211a9
EZ
112
113(defun calc-utpn ()
114 (interactive)
bf77c646 115 (calc-prob-dist "n" 3))
136211a9
EZ
116
117(defun calc-utpp ()
118 (interactive)
bf77c646 119 (calc-prob-dist "p" 2))
136211a9
EZ
120
121(defun calc-utpt ()
122 (interactive)
bf77c646 123 (calc-prob-dist "t" 2))
136211a9
EZ
124
125(defun calc-prob-dist (letter nargs)
126 (calc-slow-wrapper
127 (if (calc-is-inverse)
128 (calc-enter-result nargs (concat "ltp" letter)
129 (append (list (intern (concat "calcFunc-ltp" letter))
130 (calc-top-n 1))
131 (calc-top-list-n (1- nargs) 2)))
132 (calc-enter-result nargs (concat "utp" letter)
133 (append (list (intern (concat "calcFunc-utp" letter))
134 (calc-top-n 1))
bf77c646 135 (calc-top-list-n (1- nargs) 2))))))
136211a9
EZ
136
137
138
139
140;;; Sources: Numerical Recipes, Press et al;
141;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
142
143
144;;; Gamma function.
145
146(defun calcFunc-gamma (x)
147 (or (math-numberp x) (math-reject-arg x 'numberp))
bf77c646 148 (calcFunc-fact (math-add x -1)))
136211a9
EZ
149
150(defun math-gammap1-raw (x &optional fprec nfprec) ; compute gamma(1 + x)
151 (or fprec
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)
156 (math-neg (math-div
157 (math-pi)
158 (math-mul (math-gammap1-raw
159 (math-add (math-neg x)
160 '(float -1 0))
161 fprec nfprec)
162 (math-sin-raw
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))))
166 ((and (math-realp x)
167 (math-lessp-float '(float 736276 0) x))
168 (math-overflow))
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)
173 (math-exp-raw
174 (math-gamma-series
175 (math-sub (math-mul (math-add x '(float 5 -1))
176 lnx)
177 x)
178 xinv
179 (math-sqr xinv)
180 '(float 0 0)
bf77c646 181 2)))))))
136211a9
EZ
182
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)
188 (* n (1- n)))))
189 x))
190 (next (math-add sum term)))
191 (if (math-nearly-equal sum next)
192 next
193 (if (> n (* 2 calc-internal-prec))
194 (progn
195 ;; Need this because series eventually diverges for large enough n.
196 (calc-record-why
197 "*Gamma computation stopped early, not all digits may be valid")
198 next)
bf77c646 199 (math-gamma-series next (math-mul x xinvsqr) xinvsqr term (+ n 2))))))
136211a9
EZ
200
201
202;;; Incomplete gamma function.
203
12f1951a 204(defvar math-current-gamma-value nil)
136211a9
EZ
205(defun calcFunc-gammaP (a x)
206 (if (equal x '(var inf var-inf))
207 '(float 1 0)
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)))
213 (> a 0) (< a 20))
214 (math-sub 1 (calcFunc-gammaQ a x))
215 (let ((math-current-gamma-value (calcFunc-gamma a)))
bf77c646 216 (math-div (calcFunc-gammag a x) math-current-gamma-value)))))
136211a9
EZ
217
218(defun calcFunc-gammaQ (a x)
219 (if (equal x '(var inf var-inf))
220 '(float 0 0)
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)))
226 (> a 0) (< a 20))
227 (let ((n 0)
228 (sum '(float 1 0))
229 (term '(float 1 0)))
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)))
bf77c646 237 (math-div (calcFunc-gammaG a x) math-current-gamma-value)))))
136211a9
EZ
238
239(defun calcFunc-gammag (a x)
240 (if (equal x '(var inf var-inf))
241 (calcFunc-gamma a)
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)
251 '(float 1 0))))
252 (math-inc-gamma-series a x)
253 (math-sub (or math-current-gamma-value (calcFunc-gamma a))
bf77c646 254 (math-inc-gamma-cfrac a x))))))
136211a9
EZ
255
256(defun calcFunc-gammaG (a x)
257 (if (equal x '(var inf var-inf))
258 '(float 0 0)
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)
268 '(float 1 0))))
269 (math-sub (or math-current-gamma-value (calcFunc-gamma a))
270 (math-inc-gamma-series a x))
bf77c646 271 (math-inc-gamma-cfrac a x)))))
136211a9
EZ
272
273(defun math-inc-gamma-series (a x)
274 (if (Math-zerop x)
275 '(float 0 0)
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)))
bf77c646 279 (math-inc-gamma-series-step start start a x))))))
136211a9
EZ
280
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)
287 next
bf77c646 288 (math-inc-gamma-series-step next term a x))))
136211a9
EZ
289
290(defun math-inc-gamma-cfrac (a x)
291 (if (Math-zerop 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)
bf77c646 297 a x))))
136211a9
EZ
298
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)))
307 (if (math-zerop a1)
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)
313 next
bf77c646 314 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x))))))
136211a9
EZ
315
316
317;;; Error function.
318
319(defun calcFunc-erf (x)
320 (if (equal x '(var inf var-inf))
321 '(float 1 0)
322 (if (equal x '(neg (var inf var-inf)))
323 '(float -1 0)
324 (if (Math-zerop x)
325 x
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)
bf77c646 331 x))))))
136211a9
EZ
332
333(defun calcFunc-erfc (x)
334 (if (equal x '(var inf var-inf))
335 '(float 0 0)
336 (if (math-posp x)
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))
bf77c646 340 (math-sub 1 (calcFunc-erf x)))))
136211a9
EZ
341
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)))
bf77c646 346 x))
136211a9
EZ
347
348(defun math-to-same-complex-quad (x y)
349 (if (eq (car-safe y) 'cplx)
350 (if (eq (car-safe x) 'cplx)
351 (list '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))
355 (if (math-negp y)
356 (if (eq (car-safe x) 'cplx)
357 (list 'cplx (math-neg (nth 1 x)) (nth 2 x))
358 (math-neg x))
bf77c646 359 x)))
136211a9
EZ
360
361
362;;; Beta function.
363
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)
370 (calcFunc-beta b a)
371 (math-div (math-mul (calcFunc-gamma a) (calcFunc-gamma b))
bf77c646 372 (calcFunc-gamma (math-add a b))))))
136211a9
EZ
373
374
375;;; Incomplete beta function.
376
12f1951a 377(defvar math-current-beta-value nil)
136211a9
EZ
378(defun calcFunc-betaI (x a b)
379 (cond ((math-zerop x)
380 '(float 0 0))
381 ((math-equal-int x 1)
382 '(float 1 0))
383 ((or (math-zerop a)
384 (and (math-num-integerp a)
385 (math-negp a)))
386 (if (or (math-zerop b)
387 (and (math-num-integerp b)
388 (math-negp b)))
389 (math-reject-arg b 'range)
390 '(float 1 0)))
391 ((or (math-zerop b)
392 (and (math-num-integerp b)
393 (math-negp b)))
394 '(float 0 0))
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)))
bf77c646 399 (math-div (calcFunc-betaB x a b) math-current-beta-value)))))
136211a9
EZ
400
401(defun calcFunc-betaB (x a b)
402 (cond
403 ((math-zerop x)
404 '(float 0 0))
405 ((math-equal-int x 1)
406 (calcFunc-beta a b))
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)
413 (if (math-negp 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))
418 (math-mul
419 (math-with-extra-prec 2
420 (let* ((i 0)
421 (term 1)
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))
427 sum))
428 (math-pow x a)))
429 ((and (math-num-integerp a)
430 (if (math-negp 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)))
435 (t
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)
444 x)))))))
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)))
bf77c646 451 b))))))))
136211a9
EZ
452
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)
461 '(float 1 0)
bf77c646 462 qab qap qam a b x)))
136211a9
EZ
463
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)))
470 (d2 (math-neg
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)
478 next
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))
bf77c646 482 qab qap qam a b x))))
136211a9
EZ
483
484
485;;; Bessel functions.
486
487;;; Should generalize this to handle arbitrary precision!
488
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)
497 (if (math-zerop v)
498 '(float 1 0)
499 '(float 0 0)))
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
504 0
505 (math-mul '(float -25 -2)
506 (math-sqr x))
507 v)
508 (math-pow (math-div x 2) v))))
509 ((math-negp (setq v (math-trunc v)))
510 (if (math-oddp v)
511 (math-neg (calcFunc-besJ (math-neg v) x))
512 (calcFunc-besJ (math-neg v) x)))
513 ((eq v 0)
514 (math-besJ0 x))
515 ((eq v 1)
516 (math-besJ1 x))
517 ((Math-lessp v (math-abs-approx x))
518 (let ((j 0)
519 (bjm (math-besJ0 x))
520 (bj (math-besJ1 x))
521 (two-over-x (math-div 2 x))
522 bjp)
523 (while (< (setq j (1+ j)) v)
524 (setq bjp (math-sub (math-mul (math-mul j two-over-x) bj)
525 bjm)
526 bjm bj
527 bj bjp))
528 bj))
529 (t
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))
533 (jsum nil)
534 (bjp '(float 0 0))
535 (sum '(float 0 0))
536 (bj '(float 1 0))
537 bjm ans)
538 (while (> (setq j (1- j)) 0)
539 (setq bjm (math-sub (math-mul (math-mul j two-over-x) bj)
540 bjp)
541 bjp bj
542 bj bjm)
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)))
550 (if (= j v)
551 (setq ans bjp)))
bf77c646 552 (math-div ans (math-sub (math-mul 2 sum) bj))))))))
136211a9
EZ
553
554(defun math-besJ-series (sum term k zz vk)
555 (math-working "besJ" sum)
556 (setq k (1+ k)
557 vk (math-add 1 vk)
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)
561 next
bf77c646 562 (math-besJ-series next term k zz vk))))
136211a9
EZ
563
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))
569 (y (math-sqr z))
a8775b6d
JB
570 (xx (math-add x
571 (eval-when-compile
f83b9601 572 (math-read-number-simple "-0.785398164"))))
136211a9 573 (a1 (math-poly-eval y
a8775b6d
JB
574 (eval-when-compile
575 (list
f83b9601
JB
576 (math-read-number-simple "0.0000002093887211")
577 (math-read-number-simple "-0.000002073370639")
578 (math-read-number-simple "0.00002734510407")
579 (math-read-number-simple "-0.001098628627")
a8775b6d 580 '(float 1 0)))))
136211a9 581 (a2 (math-poly-eval y
a8775b6d
JB
582 (eval-when-compile
583 (list
f83b9601
JB
584 (math-read-number-simple "-0.0000000934935152")
585 (math-read-number-simple "0.0000007621095161")
586 (math-read-number-simple "-0.000006911147651")
587 (math-read-number-simple "0.0001430488765")
588 (math-read-number-simple "-0.01562499995")))))
136211a9
EZ
589 (sc (math-sin-cos-raw xx)))
590 (if yflag
591 (setq sc (cons (math-neg (cdr sc)) (car sc))))
592 (math-mul (math-sqrt
a8775b6d 593 (math-div (eval-when-compile
f83b9601 594 (math-read-number-simple "0.636619722"))
a8775b6d 595 x))
136211a9
EZ
596 (math-sub (math-mul (cdr sc) a1)
597 (math-mul (car sc) (math-mul z a2))))))
598 (t
599 (let ((y (math-sqr x)))
600 (math-div (math-poly-eval y
a8775b6d
JB
601 (eval-when-compile
602 (list
f83b9601
JB
603 (math-read-number-simple "-184.9052456")
604 (math-read-number-simple "77392.33017")
605 (math-read-number-simple "-11214424.18")
606 (math-read-number-simple "651619640.7")
607 (math-read-number-simple "-13362590354.0")
608 (math-read-number-simple "57568490574.0"))))
136211a9 609 (math-poly-eval y
a8775b6d
JB
610 (eval-when-compile
611 (list
612 '(float 1 0)
f83b9601
JB
613 (math-read-number-simple "267.8532712")
614 (math-read-number-simple "59272.64853")
615 (math-read-number-simple "9494680.718")
616 (math-read-number-simple "1029532985.0")
617 (math-read-number-simple "57568490411.0")))))))))
136211a9
EZ
618
619(defun math-besJ1 (x &optional yflag)
620 (cond ((and (math-negp (calcFunc-re x)) (not yflag))
621 (math-neg (math-besJ1 (math-neg x))))
622 ((Math-lessp '(float 8 0) (math-abs-approx x))
623 (let* ((z (math-div '(float 8 0) x))
624 (y (math-sqr z))
a8775b6d 625 (xx (math-add x (eval-when-compile
f83b9601 626 (math-read-number-simple "-2.356194491"))))
136211a9 627 (a1 (math-poly-eval y
a8775b6d
JB
628 (eval-when-compile
629 (list
f83b9601
JB
630 (math-read-number-simple "-0.000000240337019")
631 (math-read-number-simple "0.000002457520174")
632 (math-read-number-simple "-0.00003516396496")
a8775b6d
JB
633 '(float 183105 -8)
634 '(float 1 0)))))
136211a9 635 (a2 (math-poly-eval y
a8775b6d
JB
636 (eval-when-compile
637 (list
f83b9601
JB
638 (math-read-number-simple "0.000000105787412")
639 (math-read-number-simple "-0.00000088228987")
640 (math-read-number-simple "0.000008449199096")
641 (math-read-number-simple "-0.0002002690873")
642 (math-read-number-simple "0.04687499995")))))
136211a9
EZ
643 (sc (math-sin-cos-raw xx)))
644 (if yflag
645 (setq sc (cons (math-neg (cdr sc)) (car sc)))
646 (if (math-negp x)
647 (setq sc (cons (math-neg (car sc)) (math-neg (cdr sc))))))
a8775b6d
JB
648 (math-mul (math-sqrt (math-div
649 (eval-when-compile
f83b9601 650 (math-read-number-simple "0.636619722"))
a8775b6d 651 x))
136211a9
EZ
652 (math-sub (math-mul (cdr sc) a1)
653 (math-mul (car sc) (math-mul z a2))))))
654 (t
655 (let ((y (math-sqr x)))
656 (math-mul
657 x
658 (math-div (math-poly-eval y
a8775b6d
JB
659 (eval-when-compile
660 (list
f83b9601
JB
661 (math-read-number-simple "-30.16036606")
662 (math-read-number-simple "15704.4826")
663 (math-read-number-simple "-2972611.439")
664 (math-read-number-simple "242396853.1")
665 (math-read-number-simple "-7895059235.0")
666 (math-read-number-simple "72362614232.0"))))
136211a9 667 (math-poly-eval y
a8775b6d
JB
668 (eval-when-compile
669 (list
670 '(float 1 0)
f83b9601
JB
671 (math-read-number-simple "376.9991397")
672 (math-read-number-simple "99447.43394")
673 (math-read-number-simple "18583304.74")
674 (math-read-number-simple "2300535178.0")
675 (math-read-number-simple "144725228442.0"))))))))))
136211a9
EZ
676
677(defun calcFunc-besY (v x)
678 (math-inexact-result)
679 (or (math-numberp v) (math-reject-arg v 'numberp))
680 (or (math-numberp x) (math-reject-arg x 'numberp))
681 (let ((calc-internal-prec (min 8 calc-internal-prec)))
682 (math-with-extra-prec 3
683 (setq x (math-float (math-normalize x)))
684 (setq v (math-float (math-normalize v)))
685 (cond ((not (math-num-integerp v))
686 (let ((sc (math-sin-cos-raw (math-mul v (math-pi)))))
687 (math-div (math-sub (math-mul (calcFunc-besJ v x) (cdr sc))
688 (calcFunc-besJ (math-neg v) x))
689 (car sc))))
690 ((math-negp (setq v (math-trunc v)))
691 (if (math-oddp v)
692 (math-neg (calcFunc-besY (math-neg v) x))
693 (calcFunc-besY (math-neg v) x)))
694 ((eq v 0)
695 (math-besY0 x))
696 ((eq v 1)
697 (math-besY1 x))
698 (t
699 (let ((j 0)
700 (bym (math-besY0 x))
701 (by (math-besY1 x))
702 (two-over-x (math-div 2 x))
703 byp)
704 (while (< (setq j (1+ j)) v)
705 (setq byp (math-sub (math-mul (math-mul j two-over-x) by)
706 bym)
707 bym by
708 by byp))
bf77c646 709 by))))))
136211a9
EZ
710
711(defun math-besY0 (x)
712 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
713 (let ((y (math-sqr x)))
714 (math-add
715 (math-div (math-poly-eval y
a8775b6d
JB
716 (eval-when-compile
717 (list
f83b9601
JB
718 (math-read-number-simple "228.4622733")
719 (math-read-number-simple "-86327.92757")
720 (math-read-number-simple "10879881.29")
721 (math-read-number-simple "-512359803.6")
722 (math-read-number-simple "7062834065.0")
723 (math-read-number-simple "-2957821389.0"))))
136211a9 724 (math-poly-eval y
a8775b6d
JB
725 (eval-when-compile
726 (list
727 '(float 1 0)
f83b9601
JB
728 (math-read-number-simple "226.1030244")
729 (math-read-number-simple "47447.2647")
730 (math-read-number-simple "7189466.438")
731 (math-read-number-simple "745249964.8")
732 (math-read-number-simple "40076544269.0")))))
a8775b6d 733 (math-mul (eval-when-compile
f83b9601 734 (math-read-number-simple "0.636619772"))
136211a9
EZ
735 (math-mul (math-besJ0 x) (math-ln-raw x))))))
736 ((math-negp (calcFunc-re x))
737 (math-add (math-besJ0 (math-neg x) t)
738 (math-mul '(cplx 0 2)
739 (math-besJ0 (math-neg x)))))
740 (t
bf77c646 741 (math-besJ0 x t))))
136211a9
EZ
742
743(defun math-besY1 (x)
744 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
745 (let ((y (math-sqr x)))
746 (math-add
747 (math-mul
748 x
749 (math-div (math-poly-eval y
a8775b6d
JB
750 (eval-when-compile
751 (list
f83b9601
JB
752 (math-read-number-simple "8511.937935")
753 (math-read-number-simple "-4237922.726")
754 (math-read-number-simple "734926455.1")
755 (math-read-number-simple "-51534381390.0")
756 (math-read-number-simple "1275274390000.0")
757 (math-read-number-simple "-4900604943000.0"))))
136211a9 758 (math-poly-eval y
a8775b6d
JB
759 (eval-when-compile
760 (list
761 '(float 1 0)
f83b9601
JB
762 (math-read-number-simple "354.9632885")
763 (math-read-number-simple "102042.605")
764 (math-read-number-simple "22459040.02")
765 (math-read-number-simple "3733650367.0")
766 (math-read-number-simple "424441966400.0")
767 (math-read-number-simple "24995805700000.0"))))))
768 (math-mul (eval-when-compile (math-read-number-simple "0.636619772"))
a8775b6d 769 (math-sub (math-mul (math-besJ1 x) (math-ln-raw x))
136211a9
EZ
770 (math-div 1 x))))))
771 ((math-negp (calcFunc-re x))
772 (math-neg
773 (math-add (math-besJ1 (math-neg x) t)
774 (math-mul '(cplx 0 2)
775 (math-besJ1 (math-neg x))))))
776 (t
bf77c646 777 (math-besJ1 x t))))
136211a9
EZ
778
779(defun math-poly-eval (x coefs)
780 (let ((accum (car coefs)))
781 (while (setq coefs (cdr coefs))
782 (setq accum (math-add (car coefs) (math-mul accum x))))
bf77c646 783 accum))
136211a9
EZ
784
785
786;;;; Bernoulli and Euler polynomials and numbers.
787
788(defun calcFunc-bern (n &optional x)
789 (if (and x (not (math-zerop x)))
790 (if (and calc-symbolic-mode (math-floatp x))
791 (math-inexact-result)
792 (math-build-polynomial-expr (math-bernoulli-coefs n) x))
793 (or (math-num-natnump n) (math-reject-arg n 'natnump))
794 (if (consp n)
795 (progn
796 (math-inexact-result)
797 (math-float (math-bernoulli-number (math-trunc n))))
bf77c646 798 (math-bernoulli-number n))))
136211a9
EZ
799
800(defun calcFunc-euler (n &optional x)
801 (or (math-num-natnump n) (math-reject-arg n 'natnump))
802 (if x
803 (let* ((n1 (math-add n 1))
804 (coefs (math-bernoulli-coefs n1))
805 (fac (math-div (math-pow 2 n1) n1))
806 (k -1)
807 (x1 (math-div (math-add x 1) 2))
808 (x2 (math-div x 2)))
809 (if (math-numberp x)
810 (if (and calc-symbolic-mode (math-floatp x))
811 (math-inexact-result)
812 (math-mul fac
813 (math-sub (math-build-polynomial-expr coefs x1)
814 (math-build-polynomial-expr coefs x2))))
815 (calcFunc-collect
816 (math-reduce-vec
817 'math-add
818 (cons 'vec
819 (mapcar (function
820 (lambda (c)
821 (setq k (1+ k))
822 (math-mul (math-mul fac c)
823 (math-sub (math-pow x1 k)
824 (math-pow x2 k)))))
825 coefs)))
826 x)))
827 (math-mul (math-pow 2 n)
828 (if (consp n)
829 (progn
830 (math-inexact-result)
831 (calcFunc-euler n '(float 5 -1)))
bf77c646 832 (calcFunc-euler n '(frac 1 2))))))
136211a9 833
a8775b6d
JB
834(defvar math-bernoulli-b-cache
835 (eval-when-compile
836 (list
837 (list 'frac
838 -174611
f83b9601 839 (math-read-number-simple "802857662698291200000"))
a8775b6d
JB
840 (list 'frac
841 43867
f83b9601 842 (math-read-number-simple "5109094217170944000"))
a8775b6d
JB
843 (list 'frac
844 -3617
f83b9601 845 (math-read-number-simple "10670622842880000"))
a8775b6d
JB
846 (list 'frac
847 1
f83b9601 848 (math-read-number-simple "74724249600"))
a8775b6d
JB
849 (list 'frac
850 -691
f83b9601 851 (math-read-number-simple "1307674368000"))
a8775b6d
JB
852 (list 'frac
853 1
f83b9601 854 (math-read-number-simple "47900160"))
a8775b6d
JB
855 (list 'frac
856 -1
f83b9601 857 (math-read-number-simple "1209600"))
a8775b6d
JB
858 (list 'frac
859 1
860 30240)
861 (list 'frac
862 -1
863 720)
864 (list 'frac
865 1
866 12)
867 1 )))
12f1951a
CW
868
869(defvar math-bernoulli-B-cache '((frac -174611 330) (frac 43867 798)
870 (frac -3617 510) (frac 7 6) (frac -691 2730)
871 (frac 5 66) (frac -1 30) (frac 1 42)
872 (frac -1 30) (frac 1 6) 1 ))
873
874(defvar math-bernoulli-cache-size 11)
136211a9
EZ
875(defun math-bernoulli-coefs (n)
876 (let* ((coefs (list (calcFunc-bern n)))
877 (nn (math-trunc n))
878 (k nn)
879 (term nn)
880 coef
881 (calc-prefer-frac (or (integerp n) calc-prefer-frac)))
882 (while (>= (setq k (1- k)) 0)
883 (setq term (math-div term (- nn k))
884 coef (math-mul term (math-bernoulli-number k))
885 coefs (cons (if (consp n) (math-float coef) coef) coefs)
886 term (math-mul term k)))
bf77c646 887 (nreverse coefs)))
136211a9
EZ
888
889(defun math-bernoulli-number (n)
890 (if (= (% n 2) 1)
891 (if (= n 1)
892 '(frac -1 2)
893 0)
894 (setq n (/ n 2))
895 (while (>= n math-bernoulli-cache-size)
896 (let* ((sum 0)
897 (nk 1) ; nk = n-k+1
898 (fact 1) ; fact = (n-k+1)!
899 ofact
900 (p math-bernoulli-b-cache)
901 (calc-prefer-frac t))
902 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size))
903 (while p
904 (setq nk (+ nk 2)
905 ofact fact
906 fact (math-mul fact (* nk (1- nk)))
907 sum (math-add sum (math-div (car p) fact))
908 p (cdr p)))
909 (setq ofact (math-mul ofact (1- nk))
910 sum (math-sub (math-div '(frac 1 2) ofact) sum)
911 math-bernoulli-b-cache (cons sum math-bernoulli-b-cache)
912 math-bernoulli-B-cache (cons (math-mul sum ofact)
913 math-bernoulli-B-cache)
914 math-bernoulli-cache-size (1+ math-bernoulli-cache-size))))
bf77c646 915 (nth (- math-bernoulli-cache-size n 1) math-bernoulli-B-cache)))
136211a9
EZ
916
917;;; Bn = n! bn
918;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
919
920;;; A faster method would be to use "tangent numbers", c.f., Concrete
921;;; Mathematics pg. 273.
922
136211a9
EZ
923
924;;; Probability distributions.
925
926;;; Binomial.
927(defun calcFunc-utpb (x n p)
928 (if math-expand-formulas
929 (math-normalize (list 'calcFunc-betaI p x (list '+ (list '- n x) 1)))
bf77c646 930 (calcFunc-betaI p x (math-add (math-sub n x) 1))))
136211a9
EZ
931(put 'calcFunc-utpb 'math-expandable t)
932
933(defun calcFunc-ltpb (x n p)
bf77c646 934 (math-sub 1 (calcFunc-utpb x n p)))
136211a9
EZ
935(put 'calcFunc-ltpb 'math-expandable t)
936
937;;; Chi-square.
938(defun calcFunc-utpc (chisq v)
939 (if math-expand-formulas
940 (math-normalize (list 'calcFunc-gammaQ (list '/ v 2) (list '/ chisq 2)))
bf77c646 941 (calcFunc-gammaQ (math-div v 2) (math-div chisq 2))))
136211a9
EZ
942(put 'calcFunc-utpc 'math-expandable t)
943
944(defun calcFunc-ltpc (chisq v)
945 (if math-expand-formulas
946 (math-normalize (list 'calcFunc-gammaP (list '/ v 2) (list '/ chisq 2)))
bf77c646 947 (calcFunc-gammaP (math-div v 2) (math-div chisq 2))))
136211a9
EZ
948(put 'calcFunc-ltpc 'math-expandable t)
949
950;;; F-distribution.
951(defun calcFunc-utpf (f v1 v2)
952 (if math-expand-formulas
953 (math-normalize (list 'calcFunc-betaI
954 (list '/ v2 (list '+ v2 (list '* v1 f)))
955 (list '/ v2 2)
956 (list '/ v1 2)))
957 (calcFunc-betaI (math-div v2 (math-add v2 (math-mul v1 f)))
958 (math-div v2 2)
bf77c646 959 (math-div v1 2))))
136211a9
EZ
960(put 'calcFunc-utpf 'math-expandable t)
961
962(defun calcFunc-ltpf (f v1 v2)
bf77c646 963 (math-sub 1 (calcFunc-utpf f v1 v2)))
136211a9
EZ
964(put 'calcFunc-ltpf 'math-expandable t)
965
966;;; Normal.
967(defun calcFunc-utpn (x mean sdev)
968 (if math-expand-formulas
969 (math-normalize
970 (list '/
971 (list '+ 1
972 (list 'calcFunc-erf
973 (list '/ (list '- mean x)
974 (list '* sdev (list 'calcFunc-sqrt 2)))))
975 2))
976 (math-mul (math-add '(float 1 0)
977 (calcFunc-erf
978 (math-div (math-sub mean x)
979 (math-mul sdev (math-sqrt-2)))))
bf77c646 980 '(float 5 -1))))
136211a9
EZ
981(put 'calcFunc-utpn 'math-expandable t)
982
983(defun calcFunc-ltpn (x mean sdev)
984 (if math-expand-formulas
985 (math-normalize
986 (list '/
987 (list '+ 1
988 (list 'calcFunc-erf
989 (list '/ (list '- x mean)
990 (list '* sdev (list 'calcFunc-sqrt 2)))))
991 2))
992 (math-mul (math-add '(float 1 0)
993 (calcFunc-erf
994 (math-div (math-sub x mean)
995 (math-mul sdev (math-sqrt-2)))))
bf77c646 996 '(float 5 -1))))
136211a9
EZ
997(put 'calcFunc-ltpn 'math-expandable t)
998
999;;; Poisson.
1000(defun calcFunc-utpp (n x)
1001 (if math-expand-formulas
1002 (math-normalize (list 'calcFunc-gammaP x n))
bf77c646 1003 (calcFunc-gammaP x n)))
136211a9
EZ
1004(put 'calcFunc-utpp 'math-expandable t)
1005
1006(defun calcFunc-ltpp (n x)
1007 (if math-expand-formulas
1008 (math-normalize (list 'calcFunc-gammaQ x n))
bf77c646 1009 (calcFunc-gammaQ x n)))
136211a9
EZ
1010(put 'calcFunc-ltpp 'math-expandable t)
1011
1012;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
1013(defun calcFunc-utpt (tt v)
1014 (if math-expand-formulas
1015 (math-normalize (list 'calcFunc-betaI
1016 (list '/ v (list '+ v (list '^ tt 2)))
1017 (list '/ v 2)
1018 '(float 5 -1)))
1019 (calcFunc-betaI (math-div v (math-add v (math-sqr tt)))
1020 (math-div v 2)
bf77c646 1021 '(float 5 -1))))
136211a9
EZ
1022(put 'calcFunc-utpt 'math-expandable t)
1023
1024(defun calcFunc-ltpt (tt v)
bf77c646 1025 (math-sub 1 (calcFunc-utpt tt v)))
136211a9
EZ
1026(put 'calcFunc-ltpt 'math-expandable t)
1027
300f28d3 1028(provide 'calc-funcs)
136211a9 1029
ab5796a9 1030;;; arch-tag: 421ddb7a-550f-4dda-a31c-06638ebfc43a
bf77c646 1031;;; calc-funcs.el ends here