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