(displayed-month, displayed-year): Define for compiler.
[emacs.git] / lisp / calc / calc-funcs.el
blob65da1376da3088681d732167e2e06517e966e63b
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 ;; Maintainers: D. Goel <deego@gnufans.org>
7 ;; Colin Walters <walters@debian.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is distributed in the hope that it will be useful,
12 ;; but WITHOUT ANY WARRANTY. No author or distributor
13 ;; accepts responsibility to anyone for the consequences of using it
14 ;; or for whether it serves any particular purpose or works at all,
15 ;; unless he says so in writing. Refer to the GNU Emacs General Public
16 ;; License for full details.
18 ;; Everyone is granted permission to copy, modify and redistribute
19 ;; GNU Emacs, but only under the conditions described in the
20 ;; GNU Emacs General Public License. A copy of this license is
21 ;; supposed to have been given to you along with GNU Emacs so you
22 ;; can know your rights and responsibilities. It should be in a
23 ;; file named COPYING. Among other things, the copyright notice
24 ;; and this notice must be preserved on all copies.
26 ;;; Commentary:
28 ;;; Code:
30 ;; This file is autoloaded from calc-ext.el.
31 (require 'calc-ext)
33 (require 'calc-macs)
35 (defun calc-Need-calc-funcs () nil)
38 (defun calc-inc-gamma (arg)
39 (interactive "P")
40 (calc-slow-wrapper
41 (if (calc-is-inverse)
42 (if (calc-is-hyperbolic)
43 (calc-binary-op "gamG" 'calcFunc-gammaG arg)
44 (calc-binary-op "gamQ" 'calcFunc-gammaQ arg))
45 (if (calc-is-hyperbolic)
46 (calc-binary-op "gamg" 'calcFunc-gammag arg)
47 (calc-binary-op "gamP" 'calcFunc-gammaP arg)))))
49 (defun calc-erf (arg)
50 (interactive "P")
51 (calc-slow-wrapper
52 (if (calc-is-inverse)
53 (calc-unary-op "erfc" 'calcFunc-erfc arg)
54 (calc-unary-op "erf" 'calcFunc-erf arg))))
56 (defun calc-erfc (arg)
57 (interactive "P")
58 (calc-invert-func)
59 (calc-erf arg))
61 (defun calc-beta (arg)
62 (interactive "P")
63 (calc-slow-wrapper
64 (calc-binary-op "beta" 'calcFunc-beta arg)))
66 (defun calc-inc-beta ()
67 (interactive)
68 (calc-slow-wrapper
69 (if (calc-is-hyperbolic)
70 (calc-enter-result 3 "betB" (cons 'calcFunc-betaB (calc-top-list-n 3)))
71 (calc-enter-result 3 "betI" (cons 'calcFunc-betaI (calc-top-list-n 3))))))
73 (defun calc-bessel-J (arg)
74 (interactive "P")
75 (calc-slow-wrapper
76 (calc-binary-op "besJ" 'calcFunc-besJ arg)))
78 (defun calc-bessel-Y (arg)
79 (interactive "P")
80 (calc-slow-wrapper
81 (calc-binary-op "besY" 'calcFunc-besY arg)))
83 (defun calc-bernoulli-number (arg)
84 (interactive "P")
85 (calc-slow-wrapper
86 (if (calc-is-hyperbolic)
87 (calc-binary-op "bern" 'calcFunc-bern arg)
88 (calc-unary-op "bern" 'calcFunc-bern arg))))
90 (defun calc-euler-number (arg)
91 (interactive "P")
92 (calc-slow-wrapper
93 (if (calc-is-hyperbolic)
94 (calc-binary-op "eulr" 'calcFunc-euler arg)
95 (calc-unary-op "eulr" 'calcFunc-euler arg))))
97 (defun calc-stirling-number (arg)
98 (interactive "P")
99 (calc-slow-wrapper
100 (if (calc-is-hyperbolic)
101 (calc-binary-op "str2" 'calcFunc-stir2 arg)
102 (calc-binary-op "str1" 'calcFunc-stir1 arg))))
104 (defun calc-utpb ()
105 (interactive)
106 (calc-prob-dist "b" 3))
108 (defun calc-utpc ()
109 (interactive)
110 (calc-prob-dist "c" 2))
112 (defun calc-utpf ()
113 (interactive)
114 (calc-prob-dist "f" 3))
116 (defun calc-utpn ()
117 (interactive)
118 (calc-prob-dist "n" 3))
120 (defun calc-utpp ()
121 (interactive)
122 (calc-prob-dist "p" 2))
124 (defun calc-utpt ()
125 (interactive)
126 (calc-prob-dist "t" 2))
128 (defun calc-prob-dist (letter nargs)
129 (calc-slow-wrapper
130 (if (calc-is-inverse)
131 (calc-enter-result nargs (concat "ltp" letter)
132 (append (list (intern (concat "calcFunc-ltp" letter))
133 (calc-top-n 1))
134 (calc-top-list-n (1- nargs) 2)))
135 (calc-enter-result nargs (concat "utp" letter)
136 (append (list (intern (concat "calcFunc-utp" letter))
137 (calc-top-n 1))
138 (calc-top-list-n (1- nargs) 2))))))
143 ;;; Sources: Numerical Recipes, Press et al;
144 ;;; Handbook of Mathematical Functions, Abramowitz & Stegun.
147 ;;; Gamma function.
149 (defun calcFunc-gamma (x)
150 (or (math-numberp x) (math-reject-arg x 'numberp))
151 (calcFunc-fact (math-add x -1)))
153 (defun math-gammap1-raw (x &optional fprec nfprec) ; compute gamma(1 + x)
154 (or fprec
155 (setq fprec (math-float calc-internal-prec)
156 nfprec (math-float (- calc-internal-prec))))
157 (cond ((math-lessp-float (calcFunc-re x) fprec)
158 (if (math-lessp-float (calcFunc-re x) nfprec)
159 (math-neg (math-div
160 (math-pi)
161 (math-mul (math-gammap1-raw
162 (math-add (math-neg x)
163 '(float -1 0))
164 fprec nfprec)
165 (math-sin-raw
166 (math-mul (math-pi) x)))))
167 (let ((xplus1 (math-add x '(float 1 0))))
168 (math-div (math-gammap1-raw xplus1 fprec nfprec) xplus1))))
169 ((and (math-realp x)
170 (math-lessp-float '(float 736276 0) x))
171 (math-overflow))
172 (t ; re(x) now >= 10.0
173 (let ((xinv (math-div 1 x))
174 (lnx (math-ln-raw x)))
175 (math-mul (math-sqrt-two-pi)
176 (math-exp-raw
177 (math-gamma-series
178 (math-sub (math-mul (math-add x '(float 5 -1))
179 lnx)
181 xinv
182 (math-sqr xinv)
183 '(float 0 0)
184 2)))))))
186 (defun math-gamma-series (sum x xinvsqr oterm n)
187 (math-working "gamma" sum)
188 (let* ((bn (math-bernoulli-number n))
189 (term (math-mul (math-div-float (math-float (nth 1 bn))
190 (math-float (* (nth 2 bn)
191 (* n (1- n)))))
193 (next (math-add sum term)))
194 (if (math-nearly-equal sum next)
195 next
196 (if (> n (* 2 calc-internal-prec))
197 (progn
198 ;; Need this because series eventually diverges for large enough n.
199 (calc-record-why
200 "*Gamma computation stopped early, not all digits may be valid")
201 next)
202 (math-gamma-series next (math-mul x xinvsqr) xinvsqr term (+ n 2))))))
205 ;;; Incomplete gamma function.
207 (defvar math-current-gamma-value nil)
208 (defun calcFunc-gammaP (a x)
209 (if (equal x '(var inf var-inf))
210 '(float 1 0)
211 (math-inexact-result)
212 (or (Math-numberp a) (math-reject-arg a 'numberp))
213 (or (math-numberp x) (math-reject-arg x 'numberp))
214 (if (and (math-num-integerp a)
215 (integerp (setq a (math-trunc a)))
216 (> a 0) (< a 20))
217 (math-sub 1 (calcFunc-gammaQ a x))
218 (let ((math-current-gamma-value (calcFunc-gamma a)))
219 (math-div (calcFunc-gammag a x) math-current-gamma-value)))))
221 (defun calcFunc-gammaQ (a x)
222 (if (equal x '(var inf var-inf))
223 '(float 0 0)
224 (math-inexact-result)
225 (or (Math-numberp a) (math-reject-arg a 'numberp))
226 (or (math-numberp x) (math-reject-arg x 'numberp))
227 (if (and (math-num-integerp a)
228 (integerp (setq a (math-trunc a)))
229 (> a 0) (< a 20))
230 (let ((n 0)
231 (sum '(float 1 0))
232 (term '(float 1 0)))
233 (math-with-extra-prec 1
234 (while (< (setq n (1+ n)) a)
235 (setq term (math-div (math-mul term x) n)
236 sum (math-add sum term))
237 (math-working "gamma" sum))
238 (math-mul sum (calcFunc-exp (math-neg x)))))
239 (let ((math-current-gamma-value (calcFunc-gamma a)))
240 (math-div (calcFunc-gammaG a x) math-current-gamma-value)))))
242 (defun calcFunc-gammag (a x)
243 (if (equal x '(var inf var-inf))
244 (calcFunc-gamma a)
245 (math-inexact-result)
246 (or (Math-numberp a) (math-reject-arg a 'numberp))
247 (or (Math-numberp x) (math-reject-arg x 'numberp))
248 (math-with-extra-prec 2
249 (setq a (math-float a))
250 (setq x (math-float x))
251 (if (or (math-negp (calcFunc-re a))
252 (math-lessp-float (calcFunc-re x)
253 (math-add-float (calcFunc-re a)
254 '(float 1 0))))
255 (math-inc-gamma-series a x)
256 (math-sub (or math-current-gamma-value (calcFunc-gamma a))
257 (math-inc-gamma-cfrac a x))))))
259 (defun calcFunc-gammaG (a x)
260 (if (equal x '(var inf var-inf))
261 '(float 0 0)
262 (math-inexact-result)
263 (or (Math-numberp a) (math-reject-arg a 'numberp))
264 (or (Math-numberp x) (math-reject-arg x 'numberp))
265 (math-with-extra-prec 2
266 (setq a (math-float a))
267 (setq x (math-float x))
268 (if (or (math-negp (calcFunc-re a))
269 (math-lessp-float (calcFunc-re x)
270 (math-add-float (math-abs-approx a)
271 '(float 1 0))))
272 (math-sub (or math-current-gamma-value (calcFunc-gamma a))
273 (math-inc-gamma-series a x))
274 (math-inc-gamma-cfrac a x)))))
276 (defun math-inc-gamma-series (a x)
277 (if (Math-zerop x)
278 '(float 0 0)
279 (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x))
280 (math-with-extra-prec 2
281 (let ((start (math-div '(float 1 0) a)))
282 (math-inc-gamma-series-step start start a x))))))
284 (defun math-inc-gamma-series-step (sum term a x)
285 (math-working "gamma" sum)
286 (setq a (math-add a '(float 1 0))
287 term (math-div (math-mul term x) a))
288 (let ((next (math-add sum term)))
289 (if (math-nearly-equal sum next)
290 next
291 (math-inc-gamma-series-step next term a x))))
293 (defun math-inc-gamma-cfrac (a x)
294 (if (Math-zerop x)
295 (or math-current-gamma-value (calcFunc-gamma a))
296 (math-mul (math-exp-raw (math-sub (math-mul a (math-ln-raw x)) x))
297 (math-inc-gamma-cfrac-step '(float 1 0) x
298 '(float 0 0) '(float 1 0)
299 '(float 1 0) '(float 1 0) '(float 0 0)
300 a x))))
302 (defun math-inc-gamma-cfrac-step (a0 a1 b0 b1 n fac g a x)
303 (let ((ana (math-sub n a))
304 (anf (math-mul n fac)))
305 (setq n (math-add n '(float 1 0))
306 a0 (math-mul (math-add a1 (math-mul a0 ana)) fac)
307 b0 (math-mul (math-add b1 (math-mul b0 ana)) fac)
308 a1 (math-add (math-mul x a0) (math-mul anf a1))
309 b1 (math-add (math-mul x b0) (math-mul anf b1)))
310 (if (math-zerop a1)
311 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac g a x)
312 (setq fac (math-div '(float 1 0) a1))
313 (let ((next (math-mul b1 fac)))
314 (math-working "gamma" next)
315 (if (math-nearly-equal next g)
316 next
317 (math-inc-gamma-cfrac-step a0 a1 b0 b1 n fac next a x))))))
320 ;;; Error function.
322 (defun calcFunc-erf (x)
323 (if (equal x '(var inf var-inf))
324 '(float 1 0)
325 (if (equal x '(neg (var inf var-inf)))
326 '(float -1 0)
327 (if (Math-zerop x)
329 (let ((math-current-gamma-value (math-sqrt-pi)))
330 (math-to-same-complex-quad
331 (math-div (calcFunc-gammag '(float 5 -1)
332 (math-sqr (math-to-complex-quad-one x)))
333 math-current-gamma-value)
334 x))))))
336 (defun calcFunc-erfc (x)
337 (if (equal x '(var inf var-inf))
338 '(float 0 0)
339 (if (math-posp x)
340 (let ((math-current-gamma-value (math-sqrt-pi)))
341 (math-div (calcFunc-gammaG '(float 5 -1) (math-sqr x))
342 math-current-gamma-value))
343 (math-sub 1 (calcFunc-erf x)))))
345 (defun math-to-complex-quad-one (x)
346 (if (eq (car-safe x) 'polar) (setq x (math-complex x)))
347 (if (eq (car-safe x) 'cplx)
348 (list 'cplx (math-abs (nth 1 x)) (math-abs (nth 2 x)))
351 (defun math-to-same-complex-quad (x y)
352 (if (eq (car-safe y) 'cplx)
353 (if (eq (car-safe x) 'cplx)
354 (list 'cplx
355 (if (math-negp (nth 1 y)) (math-neg (nth 1 x)) (nth 1 x))
356 (if (math-negp (nth 2 y)) (math-neg (nth 2 x)) (nth 2 x)))
357 (if (math-negp (nth 1 y)) (math-neg x) x))
358 (if (math-negp y)
359 (if (eq (car-safe x) 'cplx)
360 (list 'cplx (math-neg (nth 1 x)) (nth 2 x))
361 (math-neg x))
362 x)))
365 ;;; Beta function.
367 (defun calcFunc-beta (a b)
368 (if (math-num-integerp a)
369 (let ((am (math-add a -1)))
370 (or (math-numberp b) (math-reject-arg b 'numberp))
371 (math-div 1 (math-mul b (calcFunc-choose (math-add b am) am))))
372 (if (math-num-integerp b)
373 (calcFunc-beta b a)
374 (math-div (math-mul (calcFunc-gamma a) (calcFunc-gamma b))
375 (calcFunc-gamma (math-add a b))))))
378 ;;; Incomplete beta function.
380 (defvar math-current-beta-value nil)
381 (defun calcFunc-betaI (x a b)
382 (cond ((math-zerop x)
383 '(float 0 0))
384 ((math-equal-int x 1)
385 '(float 1 0))
386 ((or (math-zerop a)
387 (and (math-num-integerp a)
388 (math-negp a)))
389 (if (or (math-zerop b)
390 (and (math-num-integerp b)
391 (math-negp b)))
392 (math-reject-arg b 'range)
393 '(float 1 0)))
394 ((or (math-zerop b)
395 (and (math-num-integerp b)
396 (math-negp b)))
397 '(float 0 0))
398 ((not (math-numberp a)) (math-reject-arg a 'numberp))
399 ((not (math-numberp b)) (math-reject-arg b 'numberp))
400 ((math-inexact-result))
401 (t (let ((math-current-beta-value (calcFunc-beta a b)))
402 (math-div (calcFunc-betaB x a b) math-current-beta-value)))))
404 (defun calcFunc-betaB (x a b)
405 (cond
406 ((math-zerop x)
407 '(float 0 0))
408 ((math-equal-int x 1)
409 (calcFunc-beta a b))
410 ((not (math-numberp x)) (math-reject-arg x 'numberp))
411 ((not (math-numberp a)) (math-reject-arg a 'numberp))
412 ((not (math-numberp b)) (math-reject-arg b 'numberp))
413 ((math-zerop a) (math-reject-arg a 'nonzerop))
414 ((math-zerop b) (math-reject-arg b 'nonzerop))
415 ((and (math-num-integerp b)
416 (if (math-negp b)
417 (math-reject-arg b 'range)
418 (Math-natnum-lessp (setq b (math-trunc b)) 20)))
419 (and calc-symbolic-mode (or (math-floatp a) (math-floatp b))
420 (math-inexact-result))
421 (math-mul
422 (math-with-extra-prec 2
423 (let* ((i 0)
424 (term 1)
425 (sum (math-div term a)))
426 (while (< (setq i (1+ i)) b)
427 (setq term (math-mul (math-div (math-mul term (- i b)) i) x)
428 sum (math-add sum (math-div term (math-add a i))))
429 (math-working "beta" sum))
430 sum))
431 (math-pow x a)))
432 ((and (math-num-integerp a)
433 (if (math-negp a)
434 (math-reject-arg a 'range)
435 (Math-natnum-lessp (setq a (math-trunc a)) 20)))
436 (math-sub (or math-current-beta-value (calcFunc-beta a b))
437 (calcFunc-betaB (math-sub 1 x) b a)))
439 (math-inexact-result)
440 (math-with-extra-prec 2
441 (setq x (math-float x))
442 (setq a (math-float a))
443 (setq b (math-float b))
444 (let ((bt (math-exp-raw (math-add (math-mul a (math-ln-raw x))
445 (math-mul b (math-ln-raw
446 (math-sub '(float 1 0)
447 x)))))))
448 (if (Math-lessp x (math-div (math-add a '(float 1 0))
449 (math-add (math-add a b) '(float 2 0))))
450 (math-div (math-mul bt (math-beta-cfrac a b x)) a)
451 (math-sub (or math-current-beta-value (calcFunc-beta a b))
452 (math-div (math-mul bt
453 (math-beta-cfrac b a (math-sub 1 x)))
454 b))))))))
456 (defun math-beta-cfrac (a b x)
457 (let ((qab (math-add a b))
458 (qap (math-add a '(float 1 0)))
459 (qam (math-add a '(float -1 0))))
460 (math-beta-cfrac-step '(float 1 0)
461 (math-sub '(float 1 0)
462 (math-div (math-mul qab x) qap))
463 '(float 1 0) '(float 1 0)
464 '(float 1 0)
465 qab qap qam a b x)))
467 (defun math-beta-cfrac-step (az bz am bm m qab qap qam a b x)
468 (let* ((two-m (math-mul m '(float 2 0)))
469 (d (math-div (math-mul (math-mul (math-sub b m) m) x)
470 (math-mul (math-add qam two-m) (math-add a two-m))))
471 (ap (math-add az (math-mul d am)))
472 (bp (math-add bz (math-mul d bm)))
473 (d2 (math-neg
474 (math-div (math-mul (math-mul (math-add a m) (math-add qab m)) x)
475 (math-mul (math-add qap two-m) (math-add a two-m)))))
476 (app (math-add ap (math-mul d2 az)))
477 (bpp (math-add bp (math-mul d2 bz)))
478 (next (math-div app bpp)))
479 (math-working "beta" next)
480 (if (math-nearly-equal next az)
481 next
482 (math-beta-cfrac-step next '(float 1 0)
483 (math-div ap bpp) (math-div bp bpp)
484 (math-add m '(float 1 0))
485 qab qap qam a b x))))
488 ;;; Bessel functions.
490 ;;; Should generalize this to handle arbitrary precision!
492 (defun calcFunc-besJ (v x)
493 (or (math-numberp v) (math-reject-arg v 'numberp))
494 (or (math-numberp x) (math-reject-arg x 'numberp))
495 (let ((calc-internal-prec (min 8 calc-internal-prec)))
496 (math-with-extra-prec 3
497 (setq x (math-float (math-normalize x)))
498 (setq v (math-float (math-normalize v)))
499 (cond ((math-zerop x)
500 (if (math-zerop v)
501 '(float 1 0)
502 '(float 0 0)))
503 ((math-inexact-result))
504 ((not (math-num-integerp v))
505 (let ((start (math-div 1 (calcFunc-fact v))))
506 (math-mul (math-besJ-series start start
508 (math-mul '(float -25 -2)
509 (math-sqr x))
511 (math-pow (math-div x 2) v))))
512 ((math-negp (setq v (math-trunc v)))
513 (if (math-oddp v)
514 (math-neg (calcFunc-besJ (math-neg v) x))
515 (calcFunc-besJ (math-neg v) x)))
516 ((eq v 0)
517 (math-besJ0 x))
518 ((eq v 1)
519 (math-besJ1 x))
520 ((Math-lessp v (math-abs-approx x))
521 (let ((j 0)
522 (bjm (math-besJ0 x))
523 (bj (math-besJ1 x))
524 (two-over-x (math-div 2 x))
525 bjp)
526 (while (< (setq j (1+ j)) v)
527 (setq bjp (math-sub (math-mul (math-mul j two-over-x) bj)
528 bjm)
529 bjm bj
530 bj bjp))
531 bj))
533 (if (Math-lessp 100 v) (math-reject-arg v 'range))
534 (let* ((j (logior (+ v (math-isqrt-small (* 40 v))) 1))
535 (two-over-x (math-div 2 x))
536 (jsum nil)
537 (bjp '(float 0 0))
538 (sum '(float 0 0))
539 (bj '(float 1 0))
540 bjm ans)
541 (while (> (setq j (1- j)) 0)
542 (setq bjm (math-sub (math-mul (math-mul j two-over-x) bj)
543 bjp)
544 bjp bj
545 bj bjm)
546 (if (> (nth 2 (math-abs-approx bj)) 10)
547 (setq bj (math-mul bj '(float 1 -10))
548 bjp (math-mul bjp '(float 1 -10))
549 ans (and ans (math-mul ans '(float 1 -10)))
550 sum (math-mul sum '(float 1 -10))))
551 (or (setq jsum (not jsum))
552 (setq sum (math-add sum bj)))
553 (if (= j v)
554 (setq ans bjp)))
555 (math-div ans (math-sub (math-mul 2 sum) bj))))))))
557 (defun math-besJ-series (sum term k zz vk)
558 (math-working "besJ" sum)
559 (setq k (1+ k)
560 vk (math-add 1 vk)
561 term (math-div (math-mul term zz) (math-mul k vk)))
562 (let ((next (math-add sum term)))
563 (if (math-nearly-equal next sum)
564 next
565 (math-besJ-series next term k zz vk))))
567 (defun math-besJ0 (x &optional yflag)
568 (cond ((and (not yflag) (math-negp (calcFunc-re x)))
569 (math-besJ0 (math-neg x)))
570 ((Math-lessp '(float 8 0) (math-abs-approx x))
571 (let* ((z (math-div '(float 8 0) x))
572 (y (math-sqr z))
573 (xx (math-add x '(float (bigneg 164 398 785) -9)))
574 (a1 (math-poly-eval y
575 '((float (bigpos 211 887 093 2) -16)
576 (float (bigneg 639 370 073 2) -15)
577 (float (bigpos 407 510 734 2) -14)
578 (float (bigneg 627 628 098 1) -12)
579 (float 1 0))))
580 (a2 (math-poly-eval y
581 '((float (bigneg 152 935 934) -16)
582 (float (bigpos 161 095 621 7) -16)
583 (float (bigneg 651 147 911 6) -15)
584 (float (bigpos 765 488 430 1) -13)
585 (float (bigneg 995 499 562 1) -11))))
586 (sc (math-sin-cos-raw xx)))
587 (if yflag
588 (setq sc (cons (math-neg (cdr sc)) (car sc))))
589 (math-mul (math-sqrt
590 (math-div '(float (bigpos 722 619 636) -9) x))
591 (math-sub (math-mul (cdr sc) a1)
592 (math-mul (car sc) (math-mul z a2))))))
594 (let ((y (math-sqr x)))
595 (math-div (math-poly-eval y
596 '((float (bigneg 456 052 849 1) -7)
597 (float (bigpos 017 233 739 7) -5)
598 (float (bigneg 418 442 121 1) -2)
599 (float (bigpos 407 196 516 6) -1)
600 (float (bigneg 354 590 362 13) 0)
601 (float (bigpos 574 490 568 57) 0)))
602 (math-poly-eval y
603 '((float 1 0)
604 (float (bigpos 712 532 678 2) -7)
605 (float (bigpos 853 264 927 5) -5)
606 (float (bigpos 718 680 494 9) -3)
607 (float (bigpos 985 532 029 1) 0)
608 (float (bigpos 411 490 568 57) 0))))))))
610 (defun math-besJ1 (x &optional yflag)
611 (cond ((and (math-negp (calcFunc-re x)) (not yflag))
612 (math-neg (math-besJ1 (math-neg x))))
613 ((Math-lessp '(float 8 0) (math-abs-approx x))
614 (let* ((z (math-div '(float 8 0) x))
615 (y (math-sqr z))
616 (xx (math-add x '(float (bigneg 491 194 356 2) -9)))
617 (a1 (math-poly-eval y
618 '((float (bigneg 019 337 240) -15)
619 (float (bigpos 174 520 457 2) -15)
620 (float (bigneg 496 396 516 3) -14)
621 (float 183105 -8)
622 (float 1 0))))
623 (a2 (math-poly-eval y
624 '((float (bigpos 412 787 105) -15)
625 (float (bigneg 987 228 88) -14)
626 (float (bigpos 096 199 449 8) -15)
627 (float (bigneg 873 690 002 2) -13)
628 (float (bigpos 995 499 687 4) -11))))
629 (sc (math-sin-cos-raw xx)))
630 (if yflag
631 (setq sc (cons (math-neg (cdr sc)) (car sc)))
632 (if (math-negp x)
633 (setq sc (cons (math-neg (car sc)) (math-neg (cdr sc))))))
634 (math-mul (math-sqrt (math-div '(float (bigpos 722 619 636) -9) x))
635 (math-sub (math-mul (cdr sc) a1)
636 (math-mul (car sc) (math-mul z a2))))))
638 (let ((y (math-sqr x)))
639 (math-mul
641 (math-div (math-poly-eval y
642 '((float (bigneg 606 036 016 3) -8)
643 (float (bigpos 826 044 157) -4)
644 (float (bigneg 439 611 972 2) -3)
645 (float (bigpos 531 968 423 2) -1)
646 (float (bigneg 235 059 895 7) 0)
647 (float (bigpos 232 614 362 72) 0)))
648 (math-poly-eval y
649 '((float 1 0)
650 (float (bigpos 397 991 769 3) -7)
651 (float (bigpos 394 743 944 9) -5)
652 (float (bigpos 474 330 858 1) -2)
653 (float (bigpos 178 535 300 2) 0)
654 (float (bigpos 442 228 725 144)
655 0)))))))))
657 (defun calcFunc-besY (v x)
658 (math-inexact-result)
659 (or (math-numberp v) (math-reject-arg v 'numberp))
660 (or (math-numberp x) (math-reject-arg x 'numberp))
661 (let ((calc-internal-prec (min 8 calc-internal-prec)))
662 (math-with-extra-prec 3
663 (setq x (math-float (math-normalize x)))
664 (setq v (math-float (math-normalize v)))
665 (cond ((not (math-num-integerp v))
666 (let ((sc (math-sin-cos-raw (math-mul v (math-pi)))))
667 (math-div (math-sub (math-mul (calcFunc-besJ v x) (cdr sc))
668 (calcFunc-besJ (math-neg v) x))
669 (car sc))))
670 ((math-negp (setq v (math-trunc v)))
671 (if (math-oddp v)
672 (math-neg (calcFunc-besY (math-neg v) x))
673 (calcFunc-besY (math-neg v) x)))
674 ((eq v 0)
675 (math-besY0 x))
676 ((eq v 1)
677 (math-besY1 x))
679 (let ((j 0)
680 (bym (math-besY0 x))
681 (by (math-besY1 x))
682 (two-over-x (math-div 2 x))
683 byp)
684 (while (< (setq j (1+ j)) v)
685 (setq byp (math-sub (math-mul (math-mul j two-over-x) by)
686 bym)
687 bym by
688 by byp))
689 by))))))
691 (defun math-besY0 (x)
692 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
693 (let ((y (math-sqr x)))
694 (math-add
695 (math-div (math-poly-eval y
696 '((float (bigpos 733 622 284 2) -7)
697 (float (bigneg 757 792 632 8) -5)
698 (float (bigpos 129 988 087 1) -2)
699 (float (bigneg 036 598 123 5) -1)
700 (float (bigpos 065 834 062 7) 0)
701 (float (bigneg 389 821 957 2) 0)))
702 (math-poly-eval y
703 '((float 1 0)
704 (float (bigpos 244 030 261 2) -7)
705 (float (bigpos 647 472 474) -4)
706 (float (bigpos 438 466 189 7) -3)
707 (float (bigpos 648 499 452 7) -1)
708 (float (bigpos 269 544 076 40) 0))))
709 (math-mul '(float (bigpos 772 619 636) -9)
710 (math-mul (math-besJ0 x) (math-ln-raw x))))))
711 ((math-negp (calcFunc-re x))
712 (math-add (math-besJ0 (math-neg x) t)
713 (math-mul '(cplx 0 2)
714 (math-besJ0 (math-neg x)))))
716 (math-besJ0 x t))))
718 (defun math-besY1 (x)
719 (cond ((Math-lessp (math-abs-approx x) '(float 8 0))
720 (let ((y (math-sqr x)))
721 (math-add
722 (math-mul
724 (math-div (math-poly-eval y
725 '((float (bigpos 935 937 511 8) -6)
726 (float (bigneg 726 922 237 4) -3)
727 (float (bigpos 551 264 349 7) -1)
728 (float (bigneg 139 438 153 5) 1)
729 (float (bigpos 439 527 127) 4)
730 (float (bigneg 943 604 900 4) 3)))
731 (math-poly-eval y
732 '((float 1 0)
733 (float (bigpos 885 632 549 3) -7)
734 (float (bigpos 605 042 102) -3)
735 (float (bigpos 002 904 245 2) -2)
736 (float (bigpos 367 650 733 3) 0)
737 (float (bigpos 664 419 244 4) 2)
738 (float (bigpos 057 958 249) 5)))))
739 (math-mul '(float (bigpos 772 619 636) -9)
740 (math-sub (math-mul (math-besJ1 x) (math-ln-raw x))
741 (math-div 1 x))))))
742 ((math-negp (calcFunc-re x))
743 (math-neg
744 (math-add (math-besJ1 (math-neg x) t)
745 (math-mul '(cplx 0 2)
746 (math-besJ1 (math-neg x))))))
748 (math-besJ1 x t))))
750 (defun math-poly-eval (x coefs)
751 (let ((accum (car coefs)))
752 (while (setq coefs (cdr coefs))
753 (setq accum (math-add (car coefs) (math-mul accum x))))
754 accum))
757 ;;;; Bernoulli and Euler polynomials and numbers.
759 (defun calcFunc-bern (n &optional x)
760 (if (and x (not (math-zerop x)))
761 (if (and calc-symbolic-mode (math-floatp x))
762 (math-inexact-result)
763 (math-build-polynomial-expr (math-bernoulli-coefs n) x))
764 (or (math-num-natnump n) (math-reject-arg n 'natnump))
765 (if (consp n)
766 (progn
767 (math-inexact-result)
768 (math-float (math-bernoulli-number (math-trunc n))))
769 (math-bernoulli-number n))))
771 (defun calcFunc-euler (n &optional x)
772 (or (math-num-natnump n) (math-reject-arg n 'natnump))
773 (if x
774 (let* ((n1 (math-add n 1))
775 (coefs (math-bernoulli-coefs n1))
776 (fac (math-div (math-pow 2 n1) n1))
777 (k -1)
778 (x1 (math-div (math-add x 1) 2))
779 (x2 (math-div x 2)))
780 (if (math-numberp x)
781 (if (and calc-symbolic-mode (math-floatp x))
782 (math-inexact-result)
783 (math-mul fac
784 (math-sub (math-build-polynomial-expr coefs x1)
785 (math-build-polynomial-expr coefs x2))))
786 (calcFunc-collect
787 (math-reduce-vec
788 'math-add
789 (cons 'vec
790 (mapcar (function
791 (lambda (c)
792 (setq k (1+ k))
793 (math-mul (math-mul fac c)
794 (math-sub (math-pow x1 k)
795 (math-pow x2 k)))))
796 coefs)))
797 x)))
798 (math-mul (math-pow 2 n)
799 (if (consp n)
800 (progn
801 (math-inexact-result)
802 (calcFunc-euler n '(float 5 -1)))
803 (calcFunc-euler n '(frac 1 2))))))
805 (defvar math-bernoulli-b-cache '((frac -174611
806 (bigpos 0 200 291 698 662 857 802))
807 (frac 43867 (bigpos 0 944 170 217 94 109 5))
808 (frac -3617 (bigpos 0 880 842 622 670 10))
809 (frac 1 (bigpos 600 249 724 74))
810 (frac -691 (bigpos 0 368 674 307 1))
811 (frac 1 (bigpos 160 900 47))
812 (frac -1 (bigpos 600 209 1))
813 (frac 1 30240) (frac -1 720)
814 (frac 1 12) 1 ))
816 (defvar math-bernoulli-B-cache '((frac -174611 330) (frac 43867 798)
817 (frac -3617 510) (frac 7 6) (frac -691 2730)
818 (frac 5 66) (frac -1 30) (frac 1 42)
819 (frac -1 30) (frac 1 6) 1 ))
821 (defvar math-bernoulli-cache-size 11)
822 (defun math-bernoulli-coefs (n)
823 (let* ((coefs (list (calcFunc-bern n)))
824 (nn (math-trunc n))
825 (k nn)
826 (term nn)
827 coef
828 (calc-prefer-frac (or (integerp n) calc-prefer-frac)))
829 (while (>= (setq k (1- k)) 0)
830 (setq term (math-div term (- nn k))
831 coef (math-mul term (math-bernoulli-number k))
832 coefs (cons (if (consp n) (math-float coef) coef) coefs)
833 term (math-mul term k)))
834 (nreverse coefs)))
836 (defun math-bernoulli-number (n)
837 (if (= (% n 2) 1)
838 (if (= n 1)
839 '(frac -1 2)
841 (setq n (/ n 2))
842 (while (>= n math-bernoulli-cache-size)
843 (let* ((sum 0)
844 (nk 1) ; nk = n-k+1
845 (fact 1) ; fact = (n-k+1)!
846 ofact
847 (p math-bernoulli-b-cache)
848 (calc-prefer-frac t))
849 (math-working "bernoulli B" (* 2 math-bernoulli-cache-size))
850 (while p
851 (setq nk (+ nk 2)
852 ofact fact
853 fact (math-mul fact (* nk (1- nk)))
854 sum (math-add sum (math-div (car p) fact))
855 p (cdr p)))
856 (setq ofact (math-mul ofact (1- nk))
857 sum (math-sub (math-div '(frac 1 2) ofact) sum)
858 math-bernoulli-b-cache (cons sum math-bernoulli-b-cache)
859 math-bernoulli-B-cache (cons (math-mul sum ofact)
860 math-bernoulli-B-cache)
861 math-bernoulli-cache-size (1+ math-bernoulli-cache-size))))
862 (nth (- math-bernoulli-cache-size n 1) math-bernoulli-B-cache)))
864 ;;; Bn = n! bn
865 ;;; bn = - sum_k=0^n-1 bk / (n-k+1)!
867 ;;; A faster method would be to use "tangent numbers", c.f., Concrete
868 ;;; Mathematics pg. 273.
871 ;;; Probability distributions.
873 ;;; Binomial.
874 (defun calcFunc-utpb (x n p)
875 (if math-expand-formulas
876 (math-normalize (list 'calcFunc-betaI p x (list '+ (list '- n x) 1)))
877 (calcFunc-betaI p x (math-add (math-sub n x) 1))))
878 (put 'calcFunc-utpb 'math-expandable t)
880 (defun calcFunc-ltpb (x n p)
881 (math-sub 1 (calcFunc-utpb x n p)))
882 (put 'calcFunc-ltpb 'math-expandable t)
884 ;;; Chi-square.
885 (defun calcFunc-utpc (chisq v)
886 (if math-expand-formulas
887 (math-normalize (list 'calcFunc-gammaQ (list '/ v 2) (list '/ chisq 2)))
888 (calcFunc-gammaQ (math-div v 2) (math-div chisq 2))))
889 (put 'calcFunc-utpc 'math-expandable t)
891 (defun calcFunc-ltpc (chisq v)
892 (if math-expand-formulas
893 (math-normalize (list 'calcFunc-gammaP (list '/ v 2) (list '/ chisq 2)))
894 (calcFunc-gammaP (math-div v 2) (math-div chisq 2))))
895 (put 'calcFunc-ltpc 'math-expandable t)
897 ;;; F-distribution.
898 (defun calcFunc-utpf (f v1 v2)
899 (if math-expand-formulas
900 (math-normalize (list 'calcFunc-betaI
901 (list '/ v2 (list '+ v2 (list '* v1 f)))
902 (list '/ v2 2)
903 (list '/ v1 2)))
904 (calcFunc-betaI (math-div v2 (math-add v2 (math-mul v1 f)))
905 (math-div v2 2)
906 (math-div v1 2))))
907 (put 'calcFunc-utpf 'math-expandable t)
909 (defun calcFunc-ltpf (f v1 v2)
910 (math-sub 1 (calcFunc-utpf f v1 v2)))
911 (put 'calcFunc-ltpf 'math-expandable t)
913 ;;; Normal.
914 (defun calcFunc-utpn (x mean sdev)
915 (if math-expand-formulas
916 (math-normalize
917 (list '/
918 (list '+ 1
919 (list 'calcFunc-erf
920 (list '/ (list '- mean x)
921 (list '* sdev (list 'calcFunc-sqrt 2)))))
923 (math-mul (math-add '(float 1 0)
924 (calcFunc-erf
925 (math-div (math-sub mean x)
926 (math-mul sdev (math-sqrt-2)))))
927 '(float 5 -1))))
928 (put 'calcFunc-utpn 'math-expandable t)
930 (defun calcFunc-ltpn (x mean sdev)
931 (if math-expand-formulas
932 (math-normalize
933 (list '/
934 (list '+ 1
935 (list 'calcFunc-erf
936 (list '/ (list '- x mean)
937 (list '* sdev (list 'calcFunc-sqrt 2)))))
939 (math-mul (math-add '(float 1 0)
940 (calcFunc-erf
941 (math-div (math-sub x mean)
942 (math-mul sdev (math-sqrt-2)))))
943 '(float 5 -1))))
944 (put 'calcFunc-ltpn 'math-expandable t)
946 ;;; Poisson.
947 (defun calcFunc-utpp (n x)
948 (if math-expand-formulas
949 (math-normalize (list 'calcFunc-gammaP x n))
950 (calcFunc-gammaP x n)))
951 (put 'calcFunc-utpp 'math-expandable t)
953 (defun calcFunc-ltpp (n x)
954 (if math-expand-formulas
955 (math-normalize (list 'calcFunc-gammaQ x n))
956 (calcFunc-gammaQ x n)))
957 (put 'calcFunc-ltpp 'math-expandable t)
959 ;;; Student's t. (As defined in Abramowitz & Stegun and Numerical Recipes.)
960 (defun calcFunc-utpt (tt v)
961 (if math-expand-formulas
962 (math-normalize (list 'calcFunc-betaI
963 (list '/ v (list '+ v (list '^ tt 2)))
964 (list '/ v 2)
965 '(float 5 -1)))
966 (calcFunc-betaI (math-div v (math-add v (math-sqr tt)))
967 (math-div v 2)
968 '(float 5 -1))))
969 (put 'calcFunc-utpt 'math-expandable t)
971 (defun calcFunc-ltpt (tt v)
972 (math-sub 1 (calcFunc-utpt tt v)))
973 (put 'calcFunc-ltpt 'math-expandable t)
976 ;;; calc-funcs.el ends here