Line data Source code
1 : /*************************************************************************
2 : ALGLIB 3.17.0 (source code generated 2020-12-27)
3 : Copyright (c) Sergey Bochkanov (ALGLIB project).
4 :
5 : >>> SOURCE LICENSE >>>
6 : This program is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation (www.fsf.org); either version 2 of the
9 : License, or (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : A copy of the GNU General Public License is available at
17 : http://www.fsf.org/licensing/licenses
18 : >>> END OF LICENSE >>>
19 : *************************************************************************/
20 : #ifdef _MSC_VER
21 : #define _CRT_SECURE_NO_WARNINGS
22 : #endif
23 : #include "stdafx.h"
24 : #include "specialfunctions.h"
25 :
26 : // disable some irrelevant warnings
27 : #if (AE_COMPILER==AE_MSVC) && !defined(AE_ALL_WARNINGS)
28 : #pragma warning(disable:4100)
29 : #pragma warning(disable:4127)
30 : #pragma warning(disable:4611)
31 : #pragma warning(disable:4702)
32 : #pragma warning(disable:4996)
33 : #endif
34 :
35 : /////////////////////////////////////////////////////////////////////////
36 : //
37 : // THIS SECTION CONTAINS IMPLEMENTATION OF C++ INTERFACE
38 : //
39 : /////////////////////////////////////////////////////////////////////////
40 : namespace alglib
41 : {
42 :
43 : #if defined(AE_COMPILE_GAMMAFUNC) || !defined(AE_PARTIAL_BUILD)
44 :
45 : #endif
46 :
47 : #if defined(AE_COMPILE_NORMALDISTR) || !defined(AE_PARTIAL_BUILD)
48 :
49 : #endif
50 :
51 : #if defined(AE_COMPILE_IGAMMAF) || !defined(AE_PARTIAL_BUILD)
52 :
53 : #endif
54 :
55 : #if defined(AE_COMPILE_ELLIPTIC) || !defined(AE_PARTIAL_BUILD)
56 :
57 : #endif
58 :
59 : #if defined(AE_COMPILE_HERMITE) || !defined(AE_PARTIAL_BUILD)
60 :
61 : #endif
62 :
63 : #if defined(AE_COMPILE_DAWSON) || !defined(AE_PARTIAL_BUILD)
64 :
65 : #endif
66 :
67 : #if defined(AE_COMPILE_TRIGINTEGRALS) || !defined(AE_PARTIAL_BUILD)
68 :
69 : #endif
70 :
71 : #if defined(AE_COMPILE_POISSONDISTR) || !defined(AE_PARTIAL_BUILD)
72 :
73 : #endif
74 :
75 : #if defined(AE_COMPILE_BESSEL) || !defined(AE_PARTIAL_BUILD)
76 :
77 : #endif
78 :
79 : #if defined(AE_COMPILE_IBETAF) || !defined(AE_PARTIAL_BUILD)
80 :
81 : #endif
82 :
83 : #if defined(AE_COMPILE_FDISTR) || !defined(AE_PARTIAL_BUILD)
84 :
85 : #endif
86 :
87 : #if defined(AE_COMPILE_FRESNEL) || !defined(AE_PARTIAL_BUILD)
88 :
89 : #endif
90 :
91 : #if defined(AE_COMPILE_JACOBIANELLIPTIC) || !defined(AE_PARTIAL_BUILD)
92 :
93 : #endif
94 :
95 : #if defined(AE_COMPILE_PSIF) || !defined(AE_PARTIAL_BUILD)
96 :
97 : #endif
98 :
99 : #if defined(AE_COMPILE_EXPINTEGRALS) || !defined(AE_PARTIAL_BUILD)
100 :
101 : #endif
102 :
103 : #if defined(AE_COMPILE_LAGUERRE) || !defined(AE_PARTIAL_BUILD)
104 :
105 : #endif
106 :
107 : #if defined(AE_COMPILE_CHISQUAREDISTR) || !defined(AE_PARTIAL_BUILD)
108 :
109 : #endif
110 :
111 : #if defined(AE_COMPILE_LEGENDRE) || !defined(AE_PARTIAL_BUILD)
112 :
113 : #endif
114 :
115 : #if defined(AE_COMPILE_BETAF) || !defined(AE_PARTIAL_BUILD)
116 :
117 : #endif
118 :
119 : #if defined(AE_COMPILE_CHEBYSHEV) || !defined(AE_PARTIAL_BUILD)
120 :
121 : #endif
122 :
123 : #if defined(AE_COMPILE_STUDENTTDISTR) || !defined(AE_PARTIAL_BUILD)
124 :
125 : #endif
126 :
127 : #if defined(AE_COMPILE_BINOMIALDISTR) || !defined(AE_PARTIAL_BUILD)
128 :
129 : #endif
130 :
131 : #if defined(AE_COMPILE_AIRYF) || !defined(AE_PARTIAL_BUILD)
132 :
133 : #endif
134 :
135 : #if defined(AE_COMPILE_GAMMAFUNC) || !defined(AE_PARTIAL_BUILD)
136 : /*************************************************************************
137 : Gamma function
138 :
139 : Input parameters:
140 : X - argument
141 :
142 : Domain:
143 : 0 < X < 171.6
144 : -170 < X < 0, X is not an integer.
145 :
146 : Relative error:
147 : arithmetic domain # trials peak rms
148 : IEEE -170,-33 20000 2.3e-15 3.3e-16
149 : IEEE -33, 33 20000 9.4e-16 2.2e-16
150 : IEEE 33, 171.6 20000 2.3e-15 3.2e-16
151 :
152 : Cephes Math Library Release 2.8: June, 2000
153 : Original copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
154 : Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
155 : *************************************************************************/
156 0 : double gammafunction(const double x, const xparams _xparams)
157 : {
158 : jmp_buf _break_jump;
159 : alglib_impl::ae_state _alglib_env_state;
160 0 : alglib_impl::ae_state_init(&_alglib_env_state);
161 0 : if( setjmp(_break_jump) )
162 : {
163 : #if !defined(AE_NO_EXCEPTIONS)
164 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
165 : #else
166 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
167 : return 0;
168 : #endif
169 : }
170 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
171 0 : if( _xparams.flags!=0x0 )
172 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
173 0 : double result = alglib_impl::gammafunction(x, &_alglib_env_state);
174 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
175 0 : return *(reinterpret_cast<double*>(&result));
176 : }
177 :
178 : /*************************************************************************
179 : Natural logarithm of gamma function
180 :
181 : Input parameters:
182 : X - argument
183 :
184 : Result:
185 : logarithm of the absolute value of the Gamma(X).
186 :
187 : Output parameters:
188 : SgnGam - sign(Gamma(X))
189 :
190 : Domain:
191 : 0 < X < 2.55e305
192 : -2.55e305 < X < 0, X is not an integer.
193 :
194 : ACCURACY:
195 : arithmetic domain # trials peak rms
196 : IEEE 0, 3 28000 5.4e-16 1.1e-16
197 : IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
198 : The error criterion was relative when the function magnitude
199 : was greater than one but absolute when it was less than one.
200 :
201 : The following test used the relative error criterion, though
202 : at certain points the relative error could be much higher than
203 : indicated.
204 : IEEE -200, -4 10000 4.8e-16 1.3e-16
205 :
206 : Cephes Math Library Release 2.8: June, 2000
207 : Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
208 : Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
209 : *************************************************************************/
210 0 : double lngamma(const double x, double &sgngam, const xparams _xparams)
211 : {
212 : jmp_buf _break_jump;
213 : alglib_impl::ae_state _alglib_env_state;
214 0 : alglib_impl::ae_state_init(&_alglib_env_state);
215 0 : if( setjmp(_break_jump) )
216 : {
217 : #if !defined(AE_NO_EXCEPTIONS)
218 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
219 : #else
220 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
221 : return 0;
222 : #endif
223 : }
224 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
225 0 : if( _xparams.flags!=0x0 )
226 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
227 0 : double result = alglib_impl::lngamma(x, &sgngam, &_alglib_env_state);
228 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
229 0 : return *(reinterpret_cast<double*>(&result));
230 : }
231 : #endif
232 :
233 : #if defined(AE_COMPILE_NORMALDISTR) || !defined(AE_PARTIAL_BUILD)
234 : /*************************************************************************
235 : Error function
236 :
237 : The integral is
238 :
239 : x
240 : -
241 : 2 | | 2
242 : erf(x) = -------- | exp( - t ) dt.
243 : sqrt(pi) | |
244 : -
245 : 0
246 :
247 : For 0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2); otherwise
248 : erf(x) = 1 - erfc(x).
249 :
250 :
251 : ACCURACY:
252 :
253 : Relative error:
254 : arithmetic domain # trials peak rms
255 : IEEE 0,1 30000 3.7e-16 1.0e-16
256 :
257 : Cephes Math Library Release 2.8: June, 2000
258 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
259 : *************************************************************************/
260 0 : double errorfunction(const double x, const xparams _xparams)
261 : {
262 : jmp_buf _break_jump;
263 : alglib_impl::ae_state _alglib_env_state;
264 0 : alglib_impl::ae_state_init(&_alglib_env_state);
265 0 : if( setjmp(_break_jump) )
266 : {
267 : #if !defined(AE_NO_EXCEPTIONS)
268 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
269 : #else
270 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
271 : return 0;
272 : #endif
273 : }
274 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
275 0 : if( _xparams.flags!=0x0 )
276 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
277 0 : double result = alglib_impl::errorfunction(x, &_alglib_env_state);
278 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
279 0 : return *(reinterpret_cast<double*>(&result));
280 : }
281 :
282 : /*************************************************************************
283 : Complementary error function
284 :
285 : 1 - erf(x) =
286 :
287 : inf.
288 : -
289 : 2 | | 2
290 : erfc(x) = -------- | exp( - t ) dt
291 : sqrt(pi) | |
292 : -
293 : x
294 :
295 :
296 : For small x, erfc(x) = 1 - erf(x); otherwise rational
297 : approximations are computed.
298 :
299 :
300 : ACCURACY:
301 :
302 : Relative error:
303 : arithmetic domain # trials peak rms
304 : IEEE 0,26.6417 30000 5.7e-14 1.5e-14
305 :
306 : Cephes Math Library Release 2.8: June, 2000
307 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
308 : *************************************************************************/
309 0 : double errorfunctionc(const double x, const xparams _xparams)
310 : {
311 : jmp_buf _break_jump;
312 : alglib_impl::ae_state _alglib_env_state;
313 0 : alglib_impl::ae_state_init(&_alglib_env_state);
314 0 : if( setjmp(_break_jump) )
315 : {
316 : #if !defined(AE_NO_EXCEPTIONS)
317 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
318 : #else
319 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
320 : return 0;
321 : #endif
322 : }
323 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
324 0 : if( _xparams.flags!=0x0 )
325 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
326 0 : double result = alglib_impl::errorfunctionc(x, &_alglib_env_state);
327 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
328 0 : return *(reinterpret_cast<double*>(&result));
329 : }
330 :
331 : /*************************************************************************
332 : Same as normalcdf(), obsolete name.
333 : *************************************************************************/
334 0 : double normaldistribution(const double x, const xparams _xparams)
335 : {
336 : jmp_buf _break_jump;
337 : alglib_impl::ae_state _alglib_env_state;
338 0 : alglib_impl::ae_state_init(&_alglib_env_state);
339 0 : if( setjmp(_break_jump) )
340 : {
341 : #if !defined(AE_NO_EXCEPTIONS)
342 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
343 : #else
344 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
345 : return 0;
346 : #endif
347 : }
348 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
349 0 : if( _xparams.flags!=0x0 )
350 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
351 0 : double result = alglib_impl::normaldistribution(x, &_alglib_env_state);
352 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
353 0 : return *(reinterpret_cast<double*>(&result));
354 : }
355 :
356 : /*************************************************************************
357 : Normal distribution PDF
358 :
359 : Returns Gaussian probability density function:
360 :
361 : 1
362 : f(x) = --------- * exp(-x^2/2)
363 : sqrt(2pi)
364 :
365 : Cephes Math Library Release 2.8: June, 2000
366 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
367 : *************************************************************************/
368 0 : double normalpdf(const double x, const xparams _xparams)
369 : {
370 : jmp_buf _break_jump;
371 : alglib_impl::ae_state _alglib_env_state;
372 0 : alglib_impl::ae_state_init(&_alglib_env_state);
373 0 : if( setjmp(_break_jump) )
374 : {
375 : #if !defined(AE_NO_EXCEPTIONS)
376 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
377 : #else
378 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
379 : return 0;
380 : #endif
381 : }
382 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
383 0 : if( _xparams.flags!=0x0 )
384 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
385 0 : double result = alglib_impl::normalpdf(x, &_alglib_env_state);
386 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
387 0 : return *(reinterpret_cast<double*>(&result));
388 : }
389 :
390 : /*************************************************************************
391 : Normal distribution CDF
392 :
393 : Returns the area under the Gaussian probability density
394 : function, integrated from minus infinity to x:
395 :
396 : x
397 : -
398 : 1 | | 2
399 : ndtr(x) = --------- | exp( - t /2 ) dt
400 : sqrt(2pi) | |
401 : -
402 : -inf.
403 :
404 : = ( 1 + erf(z) ) / 2
405 : = erfc(z) / 2
406 :
407 : where z = x/sqrt(2). Computation is via the functions
408 : erf and erfc.
409 :
410 :
411 : ACCURACY:
412 :
413 : Relative error:
414 : arithmetic domain # trials peak rms
415 : IEEE -13,0 30000 3.4e-14 6.7e-15
416 :
417 : Cephes Math Library Release 2.8: June, 2000
418 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
419 : *************************************************************************/
420 0 : double normalcdf(const double x, const xparams _xparams)
421 : {
422 : jmp_buf _break_jump;
423 : alglib_impl::ae_state _alglib_env_state;
424 0 : alglib_impl::ae_state_init(&_alglib_env_state);
425 0 : if( setjmp(_break_jump) )
426 : {
427 : #if !defined(AE_NO_EXCEPTIONS)
428 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
429 : #else
430 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
431 : return 0;
432 : #endif
433 : }
434 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
435 0 : if( _xparams.flags!=0x0 )
436 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
437 0 : double result = alglib_impl::normalcdf(x, &_alglib_env_state);
438 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
439 0 : return *(reinterpret_cast<double*>(&result));
440 : }
441 :
442 : /*************************************************************************
443 : Inverse of the error function
444 :
445 : Cephes Math Library Release 2.8: June, 2000
446 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
447 : *************************************************************************/
448 0 : double inverf(const double e, const xparams _xparams)
449 : {
450 : jmp_buf _break_jump;
451 : alglib_impl::ae_state _alglib_env_state;
452 0 : alglib_impl::ae_state_init(&_alglib_env_state);
453 0 : if( setjmp(_break_jump) )
454 : {
455 : #if !defined(AE_NO_EXCEPTIONS)
456 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
457 : #else
458 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
459 : return 0;
460 : #endif
461 : }
462 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
463 0 : if( _xparams.flags!=0x0 )
464 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
465 0 : double result = alglib_impl::inverf(e, &_alglib_env_state);
466 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
467 0 : return *(reinterpret_cast<double*>(&result));
468 : }
469 :
470 : /*************************************************************************
471 : Same as invnormalcdf(), deprecated name
472 : *************************************************************************/
473 0 : double invnormaldistribution(const double y0, const xparams _xparams)
474 : {
475 : jmp_buf _break_jump;
476 : alglib_impl::ae_state _alglib_env_state;
477 0 : alglib_impl::ae_state_init(&_alglib_env_state);
478 0 : if( setjmp(_break_jump) )
479 : {
480 : #if !defined(AE_NO_EXCEPTIONS)
481 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
482 : #else
483 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
484 : return 0;
485 : #endif
486 : }
487 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
488 0 : if( _xparams.flags!=0x0 )
489 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
490 0 : double result = alglib_impl::invnormaldistribution(y0, &_alglib_env_state);
491 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
492 0 : return *(reinterpret_cast<double*>(&result));
493 : }
494 :
495 : /*************************************************************************
496 : Inverse of Normal CDF
497 :
498 : Returns the argument, x, for which the area under the
499 : Gaussian probability density function (integrated from
500 : minus infinity to x) is equal to y.
501 :
502 :
503 : For small arguments 0 < y < exp(-2), the program computes
504 : z = sqrt( -2.0 * log(y) ); then the approximation is
505 : x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
506 : There are two rational functions P/Q, one for 0 < y < exp(-32)
507 : and the other for y up to exp(-2). For larger arguments,
508 : w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
509 :
510 : ACCURACY:
511 :
512 : Relative error:
513 : arithmetic domain # trials peak rms
514 : IEEE 0.125, 1 20000 7.2e-16 1.3e-16
515 : IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
516 :
517 : Cephes Math Library Release 2.8: June, 2000
518 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
519 : *************************************************************************/
520 0 : double invnormalcdf(const double y0, const xparams _xparams)
521 : {
522 : jmp_buf _break_jump;
523 : alglib_impl::ae_state _alglib_env_state;
524 0 : alglib_impl::ae_state_init(&_alglib_env_state);
525 0 : if( setjmp(_break_jump) )
526 : {
527 : #if !defined(AE_NO_EXCEPTIONS)
528 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
529 : #else
530 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
531 : return 0;
532 : #endif
533 : }
534 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
535 0 : if( _xparams.flags!=0x0 )
536 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
537 0 : double result = alglib_impl::invnormalcdf(y0, &_alglib_env_state);
538 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
539 0 : return *(reinterpret_cast<double*>(&result));
540 : }
541 :
542 : /*************************************************************************
543 : Bivariate normal PDF
544 :
545 : Returns probability density function of the bivariate Gaussian with
546 : correlation parameter equal to Rho:
547 :
548 : 1 ( x^2 - 2*rho*x*y + y^2 )
549 : f(x,y,rho) = ----------------- * exp( - ----------------------- )
550 : 2pi*sqrt(1-rho^2) ( 2*(1-rho^2) )
551 :
552 :
553 : with -1<rho<+1 and arbitrary x, y.
554 :
555 : This function won't fail as long as Rho is in (-1,+1) range.
556 :
557 : -- ALGLIB --
558 : Copyright 15.11.2019 by Bochkanov Sergey
559 : *************************************************************************/
560 0 : double bivariatenormalpdf(const double x, const double y, const double rho, const xparams _xparams)
561 : {
562 : jmp_buf _break_jump;
563 : alglib_impl::ae_state _alglib_env_state;
564 0 : alglib_impl::ae_state_init(&_alglib_env_state);
565 0 : if( setjmp(_break_jump) )
566 : {
567 : #if !defined(AE_NO_EXCEPTIONS)
568 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
569 : #else
570 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
571 : return 0;
572 : #endif
573 : }
574 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
575 0 : if( _xparams.flags!=0x0 )
576 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
577 0 : double result = alglib_impl::bivariatenormalpdf(x, y, rho, &_alglib_env_state);
578 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
579 0 : return *(reinterpret_cast<double*>(&result));
580 : }
581 :
582 : /*************************************************************************
583 : Bivariate normal CDF
584 :
585 : Returns the area under the bivariate Gaussian PDF with correlation
586 : parameter equal to Rho, integrated from minus infinity to (x,y):
587 :
588 :
589 : x y
590 : - -
591 : 1 | | | |
592 : bvn(x,y,rho) = ------------------- | | f(u,v,rho)*du*dv
593 : 2pi*sqrt(1-rho^2) | | | |
594 : - -
595 : -INF -INF
596 :
597 :
598 : where
599 :
600 : ( u^2 - 2*rho*u*v + v^2 )
601 : f(u,v,rho) = exp( - ----------------------- )
602 : ( 2*(1-rho^2) )
603 :
604 :
605 : with -1<rho<+1 and arbitrary x, y.
606 :
607 : This subroutine uses high-precision approximation scheme proposed by
608 : Alan Genz in "Numerical Computation of Rectangular Bivariate and
609 : Trivariate Normal and t probabilities", which computes CDF with
610 : absolute error roughly equal to 1e-14.
611 :
612 : This function won't fail as long as Rho is in (-1,+1) range.
613 :
614 : -- ALGLIB --
615 : Copyright 15.11.2019 by Bochkanov Sergey
616 : *************************************************************************/
617 0 : double bivariatenormalcdf(const double x, const double y, const double rho, const xparams _xparams)
618 : {
619 : jmp_buf _break_jump;
620 : alglib_impl::ae_state _alglib_env_state;
621 0 : alglib_impl::ae_state_init(&_alglib_env_state);
622 0 : if( setjmp(_break_jump) )
623 : {
624 : #if !defined(AE_NO_EXCEPTIONS)
625 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
626 : #else
627 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
628 : return 0;
629 : #endif
630 : }
631 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
632 0 : if( _xparams.flags!=0x0 )
633 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
634 0 : double result = alglib_impl::bivariatenormalcdf(x, y, rho, &_alglib_env_state);
635 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
636 0 : return *(reinterpret_cast<double*>(&result));
637 : }
638 : #endif
639 :
640 : #if defined(AE_COMPILE_IGAMMAF) || !defined(AE_PARTIAL_BUILD)
641 : /*************************************************************************
642 : Incomplete gamma integral
643 :
644 : The function is defined by
645 :
646 : x
647 : -
648 : 1 | | -t a-1
649 : igam(a,x) = ----- | e t dt.
650 : - | |
651 : | (a) -
652 : 0
653 :
654 :
655 : In this implementation both arguments must be positive.
656 : The integral is evaluated by either a power series or
657 : continued fraction expansion, depending on the relative
658 : values of a and x.
659 :
660 : ACCURACY:
661 :
662 : Relative error:
663 : arithmetic domain # trials peak rms
664 : IEEE 0,30 200000 3.6e-14 2.9e-15
665 : IEEE 0,100 300000 9.9e-14 1.5e-14
666 :
667 : Cephes Math Library Release 2.8: June, 2000
668 : Copyright 1985, 1987, 2000 by Stephen L. Moshier
669 : *************************************************************************/
670 0 : double incompletegamma(const double a, const double x, const xparams _xparams)
671 : {
672 : jmp_buf _break_jump;
673 : alglib_impl::ae_state _alglib_env_state;
674 0 : alglib_impl::ae_state_init(&_alglib_env_state);
675 0 : if( setjmp(_break_jump) )
676 : {
677 : #if !defined(AE_NO_EXCEPTIONS)
678 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
679 : #else
680 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
681 : return 0;
682 : #endif
683 : }
684 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
685 0 : if( _xparams.flags!=0x0 )
686 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
687 0 : double result = alglib_impl::incompletegamma(a, x, &_alglib_env_state);
688 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
689 0 : return *(reinterpret_cast<double*>(&result));
690 : }
691 :
692 : /*************************************************************************
693 : Complemented incomplete gamma integral
694 :
695 : The function is defined by
696 :
697 :
698 : igamc(a,x) = 1 - igam(a,x)
699 :
700 : inf.
701 : -
702 : 1 | | -t a-1
703 : = ----- | e t dt.
704 : - | |
705 : | (a) -
706 : x
707 :
708 :
709 : In this implementation both arguments must be positive.
710 : The integral is evaluated by either a power series or
711 : continued fraction expansion, depending on the relative
712 : values of a and x.
713 :
714 : ACCURACY:
715 :
716 : Tested at random a, x.
717 : a x Relative error:
718 : arithmetic domain domain # trials peak rms
719 : IEEE 0.5,100 0,100 200000 1.9e-14 1.7e-15
720 : IEEE 0.01,0.5 0,100 200000 1.4e-13 1.6e-15
721 :
722 : Cephes Math Library Release 2.8: June, 2000
723 : Copyright 1985, 1987, 2000 by Stephen L. Moshier
724 : *************************************************************************/
725 0 : double incompletegammac(const double a, const double x, const xparams _xparams)
726 : {
727 : jmp_buf _break_jump;
728 : alglib_impl::ae_state _alglib_env_state;
729 0 : alglib_impl::ae_state_init(&_alglib_env_state);
730 0 : if( setjmp(_break_jump) )
731 : {
732 : #if !defined(AE_NO_EXCEPTIONS)
733 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
734 : #else
735 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
736 : return 0;
737 : #endif
738 : }
739 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
740 0 : if( _xparams.flags!=0x0 )
741 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
742 0 : double result = alglib_impl::incompletegammac(a, x, &_alglib_env_state);
743 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
744 0 : return *(reinterpret_cast<double*>(&result));
745 : }
746 :
747 : /*************************************************************************
748 : Inverse of complemented imcomplete gamma integral
749 :
750 : Given p, the function finds x such that
751 :
752 : igamc( a, x ) = p.
753 :
754 : Starting with the approximate value
755 :
756 : 3
757 : x = a t
758 :
759 : where
760 :
761 : t = 1 - d - ndtri(p) sqrt(d)
762 :
763 : and
764 :
765 : d = 1/9a,
766 :
767 : the routine performs up to 10 Newton iterations to find the
768 : root of igamc(a,x) - p = 0.
769 :
770 : ACCURACY:
771 :
772 : Tested at random a, p in the intervals indicated.
773 :
774 : a p Relative error:
775 : arithmetic domain domain # trials peak rms
776 : IEEE 0.5,100 0,0.5 100000 1.0e-14 1.7e-15
777 : IEEE 0.01,0.5 0,0.5 100000 9.0e-14 3.4e-15
778 : IEEE 0.5,10000 0,0.5 20000 2.3e-13 3.8e-14
779 :
780 : Cephes Math Library Release 2.8: June, 2000
781 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
782 : *************************************************************************/
783 0 : double invincompletegammac(const double a, const double y0, const xparams _xparams)
784 : {
785 : jmp_buf _break_jump;
786 : alglib_impl::ae_state _alglib_env_state;
787 0 : alglib_impl::ae_state_init(&_alglib_env_state);
788 0 : if( setjmp(_break_jump) )
789 : {
790 : #if !defined(AE_NO_EXCEPTIONS)
791 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
792 : #else
793 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
794 : return 0;
795 : #endif
796 : }
797 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
798 0 : if( _xparams.flags!=0x0 )
799 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
800 0 : double result = alglib_impl::invincompletegammac(a, y0, &_alglib_env_state);
801 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
802 0 : return *(reinterpret_cast<double*>(&result));
803 : }
804 : #endif
805 :
806 : #if defined(AE_COMPILE_ELLIPTIC) || !defined(AE_PARTIAL_BUILD)
807 : /*************************************************************************
808 : Complete elliptic integral of the first kind
809 :
810 : Approximates the integral
811 :
812 :
813 :
814 : pi/2
815 : -
816 : | |
817 : | dt
818 : K(m) = | ------------------
819 : | 2
820 : | | sqrt( 1 - m sin t )
821 : -
822 : 0
823 :
824 : using the approximation
825 :
826 : P(x) - log x Q(x).
827 :
828 : ACCURACY:
829 :
830 : Relative error:
831 : arithmetic domain # trials peak rms
832 : IEEE 0,1 30000 2.5e-16 6.8e-17
833 :
834 : Cephes Math Library, Release 2.8: June, 2000
835 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
836 : *************************************************************************/
837 0 : double ellipticintegralk(const double m, const xparams _xparams)
838 : {
839 : jmp_buf _break_jump;
840 : alglib_impl::ae_state _alglib_env_state;
841 0 : alglib_impl::ae_state_init(&_alglib_env_state);
842 0 : if( setjmp(_break_jump) )
843 : {
844 : #if !defined(AE_NO_EXCEPTIONS)
845 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
846 : #else
847 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
848 : return 0;
849 : #endif
850 : }
851 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
852 0 : if( _xparams.flags!=0x0 )
853 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
854 0 : double result = alglib_impl::ellipticintegralk(m, &_alglib_env_state);
855 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
856 0 : return *(reinterpret_cast<double*>(&result));
857 : }
858 :
859 : /*************************************************************************
860 : Complete elliptic integral of the first kind
861 :
862 : Approximates the integral
863 :
864 :
865 :
866 : pi/2
867 : -
868 : | |
869 : | dt
870 : K(m) = | ------------------
871 : | 2
872 : | | sqrt( 1 - m sin t )
873 : -
874 : 0
875 :
876 : where m = 1 - m1, using the approximation
877 :
878 : P(x) - log x Q(x).
879 :
880 : The argument m1 is used rather than m so that the logarithmic
881 : singularity at m = 1 will be shifted to the origin; this
882 : preserves maximum accuracy.
883 :
884 : K(0) = pi/2.
885 :
886 : ACCURACY:
887 :
888 : Relative error:
889 : arithmetic domain # trials peak rms
890 : IEEE 0,1 30000 2.5e-16 6.8e-17
891 :
892 : Cephes Math Library, Release 2.8: June, 2000
893 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
894 : *************************************************************************/
895 0 : double ellipticintegralkhighprecision(const double m1, const xparams _xparams)
896 : {
897 : jmp_buf _break_jump;
898 : alglib_impl::ae_state _alglib_env_state;
899 0 : alglib_impl::ae_state_init(&_alglib_env_state);
900 0 : if( setjmp(_break_jump) )
901 : {
902 : #if !defined(AE_NO_EXCEPTIONS)
903 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
904 : #else
905 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
906 : return 0;
907 : #endif
908 : }
909 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
910 0 : if( _xparams.flags!=0x0 )
911 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
912 0 : double result = alglib_impl::ellipticintegralkhighprecision(m1, &_alglib_env_state);
913 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
914 0 : return *(reinterpret_cast<double*>(&result));
915 : }
916 :
917 : /*************************************************************************
918 : Incomplete elliptic integral of the first kind F(phi|m)
919 :
920 : Approximates the integral
921 :
922 :
923 :
924 : phi
925 : -
926 : | |
927 : | dt
928 : F(phi_\m) = | ------------------
929 : | 2
930 : | | sqrt( 1 - m sin t )
931 : -
932 : 0
933 :
934 : of amplitude phi and modulus m, using the arithmetic -
935 : geometric mean algorithm.
936 :
937 :
938 :
939 :
940 : ACCURACY:
941 :
942 : Tested at random points with m in [0, 1] and phi as indicated.
943 :
944 : Relative error:
945 : arithmetic domain # trials peak rms
946 : IEEE -10,10 200000 7.4e-16 1.0e-16
947 :
948 : Cephes Math Library Release 2.8: June, 2000
949 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
950 : *************************************************************************/
951 0 : double incompleteellipticintegralk(const double phi, const double m, const xparams _xparams)
952 : {
953 : jmp_buf _break_jump;
954 : alglib_impl::ae_state _alglib_env_state;
955 0 : alglib_impl::ae_state_init(&_alglib_env_state);
956 0 : if( setjmp(_break_jump) )
957 : {
958 : #if !defined(AE_NO_EXCEPTIONS)
959 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
960 : #else
961 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
962 : return 0;
963 : #endif
964 : }
965 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
966 0 : if( _xparams.flags!=0x0 )
967 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
968 0 : double result = alglib_impl::incompleteellipticintegralk(phi, m, &_alglib_env_state);
969 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
970 0 : return *(reinterpret_cast<double*>(&result));
971 : }
972 :
973 : /*************************************************************************
974 : Complete elliptic integral of the second kind
975 :
976 : Approximates the integral
977 :
978 :
979 : pi/2
980 : -
981 : | | 2
982 : E(m) = | sqrt( 1 - m sin t ) dt
983 : | |
984 : -
985 : 0
986 :
987 : using the approximation
988 :
989 : P(x) - x log x Q(x).
990 :
991 : ACCURACY:
992 :
993 : Relative error:
994 : arithmetic domain # trials peak rms
995 : IEEE 0, 1 10000 2.1e-16 7.3e-17
996 :
997 : Cephes Math Library, Release 2.8: June, 2000
998 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
999 : *************************************************************************/
1000 0 : double ellipticintegrale(const double m, const xparams _xparams)
1001 : {
1002 : jmp_buf _break_jump;
1003 : alglib_impl::ae_state _alglib_env_state;
1004 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1005 0 : if( setjmp(_break_jump) )
1006 : {
1007 : #if !defined(AE_NO_EXCEPTIONS)
1008 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1009 : #else
1010 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1011 : return 0;
1012 : #endif
1013 : }
1014 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1015 0 : if( _xparams.flags!=0x0 )
1016 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1017 0 : double result = alglib_impl::ellipticintegrale(m, &_alglib_env_state);
1018 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1019 0 : return *(reinterpret_cast<double*>(&result));
1020 : }
1021 :
1022 : /*************************************************************************
1023 : Incomplete elliptic integral of the second kind
1024 :
1025 : Approximates the integral
1026 :
1027 :
1028 : phi
1029 : -
1030 : | |
1031 : | 2
1032 : E(phi_\m) = | sqrt( 1 - m sin t ) dt
1033 : |
1034 : | |
1035 : -
1036 : 0
1037 :
1038 : of amplitude phi and modulus m, using the arithmetic -
1039 : geometric mean algorithm.
1040 :
1041 : ACCURACY:
1042 :
1043 : Tested at random arguments with phi in [-10, 10] and m in
1044 : [0, 1].
1045 : Relative error:
1046 : arithmetic domain # trials peak rms
1047 : IEEE -10,10 150000 3.3e-15 1.4e-16
1048 :
1049 : Cephes Math Library Release 2.8: June, 2000
1050 : Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
1051 : *************************************************************************/
1052 0 : double incompleteellipticintegrale(const double phi, const double m, const xparams _xparams)
1053 : {
1054 : jmp_buf _break_jump;
1055 : alglib_impl::ae_state _alglib_env_state;
1056 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1057 0 : if( setjmp(_break_jump) )
1058 : {
1059 : #if !defined(AE_NO_EXCEPTIONS)
1060 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1061 : #else
1062 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1063 : return 0;
1064 : #endif
1065 : }
1066 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1067 0 : if( _xparams.flags!=0x0 )
1068 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1069 0 : double result = alglib_impl::incompleteellipticintegrale(phi, m, &_alglib_env_state);
1070 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1071 0 : return *(reinterpret_cast<double*>(&result));
1072 : }
1073 : #endif
1074 :
1075 : #if defined(AE_COMPILE_HERMITE) || !defined(AE_PARTIAL_BUILD)
1076 : /*************************************************************************
1077 : Calculation of the value of the Hermite polynomial.
1078 :
1079 : Parameters:
1080 : n - degree, n>=0
1081 : x - argument
1082 :
1083 : Result:
1084 : the value of the Hermite polynomial Hn at x
1085 : *************************************************************************/
1086 0 : double hermitecalculate(const ae_int_t n, const double x, const xparams _xparams)
1087 : {
1088 : jmp_buf _break_jump;
1089 : alglib_impl::ae_state _alglib_env_state;
1090 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1091 0 : if( setjmp(_break_jump) )
1092 : {
1093 : #if !defined(AE_NO_EXCEPTIONS)
1094 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1095 : #else
1096 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1097 : return 0;
1098 : #endif
1099 : }
1100 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1101 0 : if( _xparams.flags!=0x0 )
1102 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1103 0 : double result = alglib_impl::hermitecalculate(n, x, &_alglib_env_state);
1104 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1105 0 : return *(reinterpret_cast<double*>(&result));
1106 : }
1107 :
1108 : /*************************************************************************
1109 : Summation of Hermite polynomials using Clenshaw's recurrence formula.
1110 :
1111 : This routine calculates
1112 : c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
1113 :
1114 : Parameters:
1115 : n - degree, n>=0
1116 : x - argument
1117 :
1118 : Result:
1119 : the value of the Hermite polynomial at x
1120 : *************************************************************************/
1121 0 : double hermitesum(const real_1d_array &c, const ae_int_t n, const double x, const xparams _xparams)
1122 : {
1123 : jmp_buf _break_jump;
1124 : alglib_impl::ae_state _alglib_env_state;
1125 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1126 0 : if( setjmp(_break_jump) )
1127 : {
1128 : #if !defined(AE_NO_EXCEPTIONS)
1129 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1130 : #else
1131 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1132 : return 0;
1133 : #endif
1134 : }
1135 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1136 0 : if( _xparams.flags!=0x0 )
1137 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1138 0 : double result = alglib_impl::hermitesum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), n, x, &_alglib_env_state);
1139 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1140 0 : return *(reinterpret_cast<double*>(&result));
1141 : }
1142 :
1143 : /*************************************************************************
1144 : Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
1145 :
1146 : Input parameters:
1147 : N - polynomial degree, n>=0
1148 :
1149 : Output parameters:
1150 : C - coefficients
1151 : *************************************************************************/
1152 0 : void hermitecoefficients(const ae_int_t n, real_1d_array &c, const xparams _xparams)
1153 : {
1154 : jmp_buf _break_jump;
1155 : alglib_impl::ae_state _alglib_env_state;
1156 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1157 0 : if( setjmp(_break_jump) )
1158 : {
1159 : #if !defined(AE_NO_EXCEPTIONS)
1160 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1161 : #else
1162 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1163 : return;
1164 : #endif
1165 : }
1166 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1167 0 : if( _xparams.flags!=0x0 )
1168 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1169 0 : alglib_impl::hermitecoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
1170 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1171 0 : return;
1172 : }
1173 : #endif
1174 :
1175 : #if defined(AE_COMPILE_DAWSON) || !defined(AE_PARTIAL_BUILD)
1176 : /*************************************************************************
1177 : Dawson's Integral
1178 :
1179 : Approximates the integral
1180 :
1181 : x
1182 : -
1183 : 2 | | 2
1184 : dawsn(x) = exp( -x ) | exp( t ) dt
1185 : | |
1186 : -
1187 : 0
1188 :
1189 : Three different rational approximations are employed, for
1190 : the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
1191 :
1192 : ACCURACY:
1193 :
1194 : Relative error:
1195 : arithmetic domain # trials peak rms
1196 : IEEE 0,10 10000 6.9e-16 1.0e-16
1197 :
1198 : Cephes Math Library Release 2.8: June, 2000
1199 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1200 : *************************************************************************/
1201 0 : double dawsonintegral(const double x, const xparams _xparams)
1202 : {
1203 : jmp_buf _break_jump;
1204 : alglib_impl::ae_state _alglib_env_state;
1205 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1206 0 : if( setjmp(_break_jump) )
1207 : {
1208 : #if !defined(AE_NO_EXCEPTIONS)
1209 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1210 : #else
1211 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1212 : return 0;
1213 : #endif
1214 : }
1215 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1216 0 : if( _xparams.flags!=0x0 )
1217 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1218 0 : double result = alglib_impl::dawsonintegral(x, &_alglib_env_state);
1219 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1220 0 : return *(reinterpret_cast<double*>(&result));
1221 : }
1222 : #endif
1223 :
1224 : #if defined(AE_COMPILE_TRIGINTEGRALS) || !defined(AE_PARTIAL_BUILD)
1225 : /*************************************************************************
1226 : Sine and cosine integrals
1227 :
1228 : Evaluates the integrals
1229 :
1230 : x
1231 : -
1232 : | cos t - 1
1233 : Ci(x) = eul + ln x + | --------- dt,
1234 : | t
1235 : -
1236 : 0
1237 : x
1238 : -
1239 : | sin t
1240 : Si(x) = | ----- dt
1241 : | t
1242 : -
1243 : 0
1244 :
1245 : where eul = 0.57721566490153286061 is Euler's constant.
1246 : The integrals are approximated by rational functions.
1247 : For x > 8 auxiliary functions f(x) and g(x) are employed
1248 : such that
1249 :
1250 : Ci(x) = f(x) sin(x) - g(x) cos(x)
1251 : Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
1252 :
1253 :
1254 : ACCURACY:
1255 : Test interval = [0,50].
1256 : Absolute error, except relative when > 1:
1257 : arithmetic function # trials peak rms
1258 : IEEE Si 30000 4.4e-16 7.3e-17
1259 : IEEE Ci 30000 6.9e-16 5.1e-17
1260 :
1261 : Cephes Math Library Release 2.1: January, 1989
1262 : Copyright 1984, 1987, 1989 by Stephen L. Moshier
1263 : *************************************************************************/
1264 0 : void sinecosineintegrals(const double x, double &si, double &ci, const xparams _xparams)
1265 : {
1266 : jmp_buf _break_jump;
1267 : alglib_impl::ae_state _alglib_env_state;
1268 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1269 0 : if( setjmp(_break_jump) )
1270 : {
1271 : #if !defined(AE_NO_EXCEPTIONS)
1272 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1273 : #else
1274 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1275 : return;
1276 : #endif
1277 : }
1278 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1279 0 : if( _xparams.flags!=0x0 )
1280 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1281 0 : alglib_impl::sinecosineintegrals(x, &si, &ci, &_alglib_env_state);
1282 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1283 0 : return;
1284 : }
1285 :
1286 : /*************************************************************************
1287 : Hyperbolic sine and cosine integrals
1288 :
1289 : Approximates the integrals
1290 :
1291 : x
1292 : -
1293 : | | cosh t - 1
1294 : Chi(x) = eul + ln x + | ----------- dt,
1295 : | | t
1296 : -
1297 : 0
1298 :
1299 : x
1300 : -
1301 : | | sinh t
1302 : Shi(x) = | ------ dt
1303 : | | t
1304 : -
1305 : 0
1306 :
1307 : where eul = 0.57721566490153286061 is Euler's constant.
1308 : The integrals are evaluated by power series for x < 8
1309 : and by Chebyshev expansions for x between 8 and 88.
1310 : For large x, both functions approach exp(x)/2x.
1311 : Arguments greater than 88 in magnitude return MAXNUM.
1312 :
1313 :
1314 : ACCURACY:
1315 :
1316 : Test interval 0 to 88.
1317 : Relative error:
1318 : arithmetic function # trials peak rms
1319 : IEEE Shi 30000 6.9e-16 1.6e-16
1320 : Absolute error, except relative when |Chi| > 1:
1321 : IEEE Chi 30000 8.4e-16 1.4e-16
1322 :
1323 : Cephes Math Library Release 2.8: June, 2000
1324 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
1325 : *************************************************************************/
1326 0 : void hyperbolicsinecosineintegrals(const double x, double &shi, double &chi, const xparams _xparams)
1327 : {
1328 : jmp_buf _break_jump;
1329 : alglib_impl::ae_state _alglib_env_state;
1330 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1331 0 : if( setjmp(_break_jump) )
1332 : {
1333 : #if !defined(AE_NO_EXCEPTIONS)
1334 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1335 : #else
1336 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1337 : return;
1338 : #endif
1339 : }
1340 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1341 0 : if( _xparams.flags!=0x0 )
1342 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1343 0 : alglib_impl::hyperbolicsinecosineintegrals(x, &shi, &chi, &_alglib_env_state);
1344 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1345 0 : return;
1346 : }
1347 : #endif
1348 :
1349 : #if defined(AE_COMPILE_POISSONDISTR) || !defined(AE_PARTIAL_BUILD)
1350 : /*************************************************************************
1351 : Poisson distribution
1352 :
1353 : Returns the sum of the first k+1 terms of the Poisson
1354 : distribution:
1355 :
1356 : k j
1357 : -- -m m
1358 : > e --
1359 : -- j!
1360 : j=0
1361 :
1362 : The terms are not summed directly; instead the incomplete
1363 : gamma integral is employed, according to the relation
1364 :
1365 : y = pdtr( k, m ) = igamc( k+1, m ).
1366 :
1367 : The arguments must both be positive.
1368 : ACCURACY:
1369 :
1370 : See incomplete gamma function
1371 :
1372 : Cephes Math Library Release 2.8: June, 2000
1373 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1374 : *************************************************************************/
1375 0 : double poissondistribution(const ae_int_t k, const double m, const xparams _xparams)
1376 : {
1377 : jmp_buf _break_jump;
1378 : alglib_impl::ae_state _alglib_env_state;
1379 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1380 0 : if( setjmp(_break_jump) )
1381 : {
1382 : #if !defined(AE_NO_EXCEPTIONS)
1383 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1384 : #else
1385 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1386 : return 0;
1387 : #endif
1388 : }
1389 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1390 0 : if( _xparams.flags!=0x0 )
1391 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1392 0 : double result = alglib_impl::poissondistribution(k, m, &_alglib_env_state);
1393 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1394 0 : return *(reinterpret_cast<double*>(&result));
1395 : }
1396 :
1397 : /*************************************************************************
1398 : Complemented Poisson distribution
1399 :
1400 : Returns the sum of the terms k+1 to infinity of the Poisson
1401 : distribution:
1402 :
1403 : inf. j
1404 : -- -m m
1405 : > e --
1406 : -- j!
1407 : j=k+1
1408 :
1409 : The terms are not summed directly; instead the incomplete
1410 : gamma integral is employed, according to the formula
1411 :
1412 : y = pdtrc( k, m ) = igam( k+1, m ).
1413 :
1414 : The arguments must both be positive.
1415 :
1416 : ACCURACY:
1417 :
1418 : See incomplete gamma function
1419 :
1420 : Cephes Math Library Release 2.8: June, 2000
1421 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1422 : *************************************************************************/
1423 0 : double poissoncdistribution(const ae_int_t k, const double m, const xparams _xparams)
1424 : {
1425 : jmp_buf _break_jump;
1426 : alglib_impl::ae_state _alglib_env_state;
1427 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1428 0 : if( setjmp(_break_jump) )
1429 : {
1430 : #if !defined(AE_NO_EXCEPTIONS)
1431 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1432 : #else
1433 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1434 : return 0;
1435 : #endif
1436 : }
1437 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1438 0 : if( _xparams.flags!=0x0 )
1439 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1440 0 : double result = alglib_impl::poissoncdistribution(k, m, &_alglib_env_state);
1441 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1442 0 : return *(reinterpret_cast<double*>(&result));
1443 : }
1444 :
1445 : /*************************************************************************
1446 : Inverse Poisson distribution
1447 :
1448 : Finds the Poisson variable x such that the integral
1449 : from 0 to x of the Poisson density is equal to the
1450 : given probability y.
1451 :
1452 : This is accomplished using the inverse gamma integral
1453 : function and the relation
1454 :
1455 : m = igami( k+1, y ).
1456 :
1457 : ACCURACY:
1458 :
1459 : See inverse incomplete gamma function
1460 :
1461 : Cephes Math Library Release 2.8: June, 2000
1462 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1463 : *************************************************************************/
1464 0 : double invpoissondistribution(const ae_int_t k, const double y, const xparams _xparams)
1465 : {
1466 : jmp_buf _break_jump;
1467 : alglib_impl::ae_state _alglib_env_state;
1468 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1469 0 : if( setjmp(_break_jump) )
1470 : {
1471 : #if !defined(AE_NO_EXCEPTIONS)
1472 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1473 : #else
1474 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1475 : return 0;
1476 : #endif
1477 : }
1478 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1479 0 : if( _xparams.flags!=0x0 )
1480 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1481 0 : double result = alglib_impl::invpoissondistribution(k, y, &_alglib_env_state);
1482 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1483 0 : return *(reinterpret_cast<double*>(&result));
1484 : }
1485 : #endif
1486 :
1487 : #if defined(AE_COMPILE_BESSEL) || !defined(AE_PARTIAL_BUILD)
1488 : /*************************************************************************
1489 : Bessel function of order zero
1490 :
1491 : Returns Bessel function of order zero of the argument.
1492 :
1493 : The domain is divided into the intervals [0, 5] and
1494 : (5, infinity). In the first interval the following rational
1495 : approximation is used:
1496 :
1497 :
1498 : 2 2
1499 : (w - r ) (w - r ) P (w) / Q (w)
1500 : 1 2 3 8
1501 :
1502 : 2
1503 : where w = x and the two r's are zeros of the function.
1504 :
1505 : In the second interval, the Hankel asymptotic expansion
1506 : is employed with two rational functions of degree 6/6
1507 : and 7/7.
1508 :
1509 : ACCURACY:
1510 :
1511 : Absolute error:
1512 : arithmetic domain # trials peak rms
1513 : IEEE 0, 30 60000 4.2e-16 1.1e-16
1514 :
1515 : Cephes Math Library Release 2.8: June, 2000
1516 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1517 : *************************************************************************/
1518 0 : double besselj0(const double x, const xparams _xparams)
1519 : {
1520 : jmp_buf _break_jump;
1521 : alglib_impl::ae_state _alglib_env_state;
1522 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1523 0 : if( setjmp(_break_jump) )
1524 : {
1525 : #if !defined(AE_NO_EXCEPTIONS)
1526 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1527 : #else
1528 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1529 : return 0;
1530 : #endif
1531 : }
1532 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1533 0 : if( _xparams.flags!=0x0 )
1534 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1535 0 : double result = alglib_impl::besselj0(x, &_alglib_env_state);
1536 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1537 0 : return *(reinterpret_cast<double*>(&result));
1538 : }
1539 :
1540 : /*************************************************************************
1541 : Bessel function of order one
1542 :
1543 : Returns Bessel function of order one of the argument.
1544 :
1545 : The domain is divided into the intervals [0, 8] and
1546 : (8, infinity). In the first interval a 24 term Chebyshev
1547 : expansion is used. In the second, the asymptotic
1548 : trigonometric representation is employed using two
1549 : rational functions of degree 5/5.
1550 :
1551 : ACCURACY:
1552 :
1553 : Absolute error:
1554 : arithmetic domain # trials peak rms
1555 : IEEE 0, 30 30000 2.6e-16 1.1e-16
1556 :
1557 : Cephes Math Library Release 2.8: June, 2000
1558 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1559 : *************************************************************************/
1560 0 : double besselj1(const double x, const xparams _xparams)
1561 : {
1562 : jmp_buf _break_jump;
1563 : alglib_impl::ae_state _alglib_env_state;
1564 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1565 0 : if( setjmp(_break_jump) )
1566 : {
1567 : #if !defined(AE_NO_EXCEPTIONS)
1568 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1569 : #else
1570 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1571 : return 0;
1572 : #endif
1573 : }
1574 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1575 0 : if( _xparams.flags!=0x0 )
1576 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1577 0 : double result = alglib_impl::besselj1(x, &_alglib_env_state);
1578 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1579 0 : return *(reinterpret_cast<double*>(&result));
1580 : }
1581 :
1582 : /*************************************************************************
1583 : Bessel function of integer order
1584 :
1585 : Returns Bessel function of order n, where n is a
1586 : (possibly negative) integer.
1587 :
1588 : The ratio of jn(x) to j0(x) is computed by backward
1589 : recurrence. First the ratio jn/jn-1 is found by a
1590 : continued fraction expansion. Then the recurrence
1591 : relating successive orders is applied until j0 or j1 is
1592 : reached.
1593 :
1594 : If n = 0 or 1 the routine for j0 or j1 is called
1595 : directly.
1596 :
1597 : ACCURACY:
1598 :
1599 : Absolute error:
1600 : arithmetic range # trials peak rms
1601 : IEEE 0, 30 5000 4.4e-16 7.9e-17
1602 :
1603 :
1604 : Not suitable for large n or x. Use jv() (fractional order) instead.
1605 :
1606 : Cephes Math Library Release 2.8: June, 2000
1607 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
1608 : *************************************************************************/
1609 0 : double besseljn(const ae_int_t n, const double x, const xparams _xparams)
1610 : {
1611 : jmp_buf _break_jump;
1612 : alglib_impl::ae_state _alglib_env_state;
1613 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1614 0 : if( setjmp(_break_jump) )
1615 : {
1616 : #if !defined(AE_NO_EXCEPTIONS)
1617 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1618 : #else
1619 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1620 : return 0;
1621 : #endif
1622 : }
1623 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1624 0 : if( _xparams.flags!=0x0 )
1625 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1626 0 : double result = alglib_impl::besseljn(n, x, &_alglib_env_state);
1627 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1628 0 : return *(reinterpret_cast<double*>(&result));
1629 : }
1630 :
1631 : /*************************************************************************
1632 : Bessel function of the second kind, order zero
1633 :
1634 : Returns Bessel function of the second kind, of order
1635 : zero, of the argument.
1636 :
1637 : The domain is divided into the intervals [0, 5] and
1638 : (5, infinity). In the first interval a rational approximation
1639 : R(x) is employed to compute
1640 : y0(x) = R(x) + 2 * log(x) * j0(x) / PI.
1641 : Thus a call to j0() is required.
1642 :
1643 : In the second interval, the Hankel asymptotic expansion
1644 : is employed with two rational functions of degree 6/6
1645 : and 7/7.
1646 :
1647 :
1648 :
1649 : ACCURACY:
1650 :
1651 : Absolute error, when y0(x) < 1; else relative error:
1652 :
1653 : arithmetic domain # trials peak rms
1654 : IEEE 0, 30 30000 1.3e-15 1.6e-16
1655 :
1656 : Cephes Math Library Release 2.8: June, 2000
1657 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1658 : *************************************************************************/
1659 0 : double bessely0(const double x, const xparams _xparams)
1660 : {
1661 : jmp_buf _break_jump;
1662 : alglib_impl::ae_state _alglib_env_state;
1663 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1664 0 : if( setjmp(_break_jump) )
1665 : {
1666 : #if !defined(AE_NO_EXCEPTIONS)
1667 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1668 : #else
1669 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1670 : return 0;
1671 : #endif
1672 : }
1673 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1674 0 : if( _xparams.flags!=0x0 )
1675 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1676 0 : double result = alglib_impl::bessely0(x, &_alglib_env_state);
1677 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1678 0 : return *(reinterpret_cast<double*>(&result));
1679 : }
1680 :
1681 : /*************************************************************************
1682 : Bessel function of second kind of order one
1683 :
1684 : Returns Bessel function of the second kind of order one
1685 : of the argument.
1686 :
1687 : The domain is divided into the intervals [0, 8] and
1688 : (8, infinity). In the first interval a 25 term Chebyshev
1689 : expansion is used, and a call to j1() is required.
1690 : In the second, the asymptotic trigonometric representation
1691 : is employed using two rational functions of degree 5/5.
1692 :
1693 : ACCURACY:
1694 :
1695 : Absolute error:
1696 : arithmetic domain # trials peak rms
1697 : IEEE 0, 30 30000 1.0e-15 1.3e-16
1698 :
1699 : Cephes Math Library Release 2.8: June, 2000
1700 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1701 : *************************************************************************/
1702 0 : double bessely1(const double x, const xparams _xparams)
1703 : {
1704 : jmp_buf _break_jump;
1705 : alglib_impl::ae_state _alglib_env_state;
1706 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1707 0 : if( setjmp(_break_jump) )
1708 : {
1709 : #if !defined(AE_NO_EXCEPTIONS)
1710 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1711 : #else
1712 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1713 : return 0;
1714 : #endif
1715 : }
1716 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1717 0 : if( _xparams.flags!=0x0 )
1718 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1719 0 : double result = alglib_impl::bessely1(x, &_alglib_env_state);
1720 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1721 0 : return *(reinterpret_cast<double*>(&result));
1722 : }
1723 :
1724 : /*************************************************************************
1725 : Bessel function of second kind of integer order
1726 :
1727 : Returns Bessel function of order n, where n is a
1728 : (possibly negative) integer.
1729 :
1730 : The function is evaluated by forward recurrence on
1731 : n, starting with values computed by the routines
1732 : y0() and y1().
1733 :
1734 : If n = 0 or 1 the routine for y0 or y1 is called
1735 : directly.
1736 :
1737 : ACCURACY:
1738 : Absolute error, except relative
1739 : when y > 1:
1740 : arithmetic domain # trials peak rms
1741 : IEEE 0, 30 30000 3.4e-15 4.3e-16
1742 :
1743 : Cephes Math Library Release 2.8: June, 2000
1744 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
1745 : *************************************************************************/
1746 0 : double besselyn(const ae_int_t n, const double x, const xparams _xparams)
1747 : {
1748 : jmp_buf _break_jump;
1749 : alglib_impl::ae_state _alglib_env_state;
1750 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1751 0 : if( setjmp(_break_jump) )
1752 : {
1753 : #if !defined(AE_NO_EXCEPTIONS)
1754 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1755 : #else
1756 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1757 : return 0;
1758 : #endif
1759 : }
1760 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1761 0 : if( _xparams.flags!=0x0 )
1762 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1763 0 : double result = alglib_impl::besselyn(n, x, &_alglib_env_state);
1764 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1765 0 : return *(reinterpret_cast<double*>(&result));
1766 : }
1767 :
1768 : /*************************************************************************
1769 : Modified Bessel function of order zero
1770 :
1771 : Returns modified Bessel function of order zero of the
1772 : argument.
1773 :
1774 : The function is defined as i0(x) = j0( ix ).
1775 :
1776 : The range is partitioned into the two intervals [0,8] and
1777 : (8, infinity). Chebyshev polynomial expansions are employed
1778 : in each interval.
1779 :
1780 : ACCURACY:
1781 :
1782 : Relative error:
1783 : arithmetic domain # trials peak rms
1784 : IEEE 0,30 30000 5.8e-16 1.4e-16
1785 :
1786 : Cephes Math Library Release 2.8: June, 2000
1787 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
1788 : *************************************************************************/
1789 0 : double besseli0(const double x, const xparams _xparams)
1790 : {
1791 : jmp_buf _break_jump;
1792 : alglib_impl::ae_state _alglib_env_state;
1793 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1794 0 : if( setjmp(_break_jump) )
1795 : {
1796 : #if !defined(AE_NO_EXCEPTIONS)
1797 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1798 : #else
1799 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1800 : return 0;
1801 : #endif
1802 : }
1803 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1804 0 : if( _xparams.flags!=0x0 )
1805 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1806 0 : double result = alglib_impl::besseli0(x, &_alglib_env_state);
1807 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1808 0 : return *(reinterpret_cast<double*>(&result));
1809 : }
1810 :
1811 : /*************************************************************************
1812 : Modified Bessel function of order one
1813 :
1814 : Returns modified Bessel function of order one of the
1815 : argument.
1816 :
1817 : The function is defined as i1(x) = -i j1( ix ).
1818 :
1819 : The range is partitioned into the two intervals [0,8] and
1820 : (8, infinity). Chebyshev polynomial expansions are employed
1821 : in each interval.
1822 :
1823 : ACCURACY:
1824 :
1825 : Relative error:
1826 : arithmetic domain # trials peak rms
1827 : IEEE 0, 30 30000 1.9e-15 2.1e-16
1828 :
1829 : Cephes Math Library Release 2.8: June, 2000
1830 : Copyright 1985, 1987, 2000 by Stephen L. Moshier
1831 : *************************************************************************/
1832 0 : double besseli1(const double x, const xparams _xparams)
1833 : {
1834 : jmp_buf _break_jump;
1835 : alglib_impl::ae_state _alglib_env_state;
1836 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1837 0 : if( setjmp(_break_jump) )
1838 : {
1839 : #if !defined(AE_NO_EXCEPTIONS)
1840 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1841 : #else
1842 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1843 : return 0;
1844 : #endif
1845 : }
1846 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1847 0 : if( _xparams.flags!=0x0 )
1848 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1849 0 : double result = alglib_impl::besseli1(x, &_alglib_env_state);
1850 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1851 0 : return *(reinterpret_cast<double*>(&result));
1852 : }
1853 :
1854 : /*************************************************************************
1855 : Modified Bessel function, second kind, order zero
1856 :
1857 : Returns modified Bessel function of the second kind
1858 : of order zero of the argument.
1859 :
1860 : The range is partitioned into the two intervals [0,8] and
1861 : (8, infinity). Chebyshev polynomial expansions are employed
1862 : in each interval.
1863 :
1864 : ACCURACY:
1865 :
1866 : Tested at 2000 random points between 0 and 8. Peak absolute
1867 : error (relative when K0 > 1) was 1.46e-14; rms, 4.26e-15.
1868 : Relative error:
1869 : arithmetic domain # trials peak rms
1870 : IEEE 0, 30 30000 1.2e-15 1.6e-16
1871 :
1872 : Cephes Math Library Release 2.8: June, 2000
1873 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
1874 : *************************************************************************/
1875 0 : double besselk0(const double x, const xparams _xparams)
1876 : {
1877 : jmp_buf _break_jump;
1878 : alglib_impl::ae_state _alglib_env_state;
1879 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1880 0 : if( setjmp(_break_jump) )
1881 : {
1882 : #if !defined(AE_NO_EXCEPTIONS)
1883 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1884 : #else
1885 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1886 : return 0;
1887 : #endif
1888 : }
1889 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1890 0 : if( _xparams.flags!=0x0 )
1891 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1892 0 : double result = alglib_impl::besselk0(x, &_alglib_env_state);
1893 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1894 0 : return *(reinterpret_cast<double*>(&result));
1895 : }
1896 :
1897 : /*************************************************************************
1898 : Modified Bessel function, second kind, order one
1899 :
1900 : Computes the modified Bessel function of the second kind
1901 : of order one of the argument.
1902 :
1903 : The range is partitioned into the two intervals [0,2] and
1904 : (2, infinity). Chebyshev polynomial expansions are employed
1905 : in each interval.
1906 :
1907 : ACCURACY:
1908 :
1909 : Relative error:
1910 : arithmetic domain # trials peak rms
1911 : IEEE 0, 30 30000 1.2e-15 1.6e-16
1912 :
1913 : Cephes Math Library Release 2.8: June, 2000
1914 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
1915 : *************************************************************************/
1916 0 : double besselk1(const double x, const xparams _xparams)
1917 : {
1918 : jmp_buf _break_jump;
1919 : alglib_impl::ae_state _alglib_env_state;
1920 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1921 0 : if( setjmp(_break_jump) )
1922 : {
1923 : #if !defined(AE_NO_EXCEPTIONS)
1924 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1925 : #else
1926 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1927 : return 0;
1928 : #endif
1929 : }
1930 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1931 0 : if( _xparams.flags!=0x0 )
1932 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1933 0 : double result = alglib_impl::besselk1(x, &_alglib_env_state);
1934 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1935 0 : return *(reinterpret_cast<double*>(&result));
1936 : }
1937 :
1938 : /*************************************************************************
1939 : Modified Bessel function, second kind, integer order
1940 :
1941 : Returns modified Bessel function of the second kind
1942 : of order n of the argument.
1943 :
1944 : The range is partitioned into the two intervals [0,9.55] and
1945 : (9.55, infinity). An ascending power series is used in the
1946 : low range, and an asymptotic expansion in the high range.
1947 :
1948 : ACCURACY:
1949 :
1950 : Relative error:
1951 : arithmetic domain # trials peak rms
1952 : IEEE 0,30 90000 1.8e-8 3.0e-10
1953 :
1954 : Error is high only near the crossover point x = 9.55
1955 : between the two expansions used.
1956 :
1957 : Cephes Math Library Release 2.8: June, 2000
1958 : Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
1959 : *************************************************************************/
1960 0 : double besselkn(const ae_int_t nn, const double x, const xparams _xparams)
1961 : {
1962 : jmp_buf _break_jump;
1963 : alglib_impl::ae_state _alglib_env_state;
1964 0 : alglib_impl::ae_state_init(&_alglib_env_state);
1965 0 : if( setjmp(_break_jump) )
1966 : {
1967 : #if !defined(AE_NO_EXCEPTIONS)
1968 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
1969 : #else
1970 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
1971 : return 0;
1972 : #endif
1973 : }
1974 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
1975 0 : if( _xparams.flags!=0x0 )
1976 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
1977 0 : double result = alglib_impl::besselkn(nn, x, &_alglib_env_state);
1978 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
1979 0 : return *(reinterpret_cast<double*>(&result));
1980 : }
1981 : #endif
1982 :
1983 : #if defined(AE_COMPILE_IBETAF) || !defined(AE_PARTIAL_BUILD)
1984 : /*************************************************************************
1985 : Incomplete beta integral
1986 :
1987 : Returns incomplete beta integral of the arguments, evaluated
1988 : from zero to x. The function is defined as
1989 :
1990 : x
1991 : - -
1992 : | (a+b) | | a-1 b-1
1993 : ----------- | t (1-t) dt.
1994 : - - | |
1995 : | (a) | (b) -
1996 : 0
1997 :
1998 : The domain of definition is 0 <= x <= 1. In this
1999 : implementation a and b are restricted to positive values.
2000 : The integral from x to 1 may be obtained by the symmetry
2001 : relation
2002 :
2003 : 1 - incbet( a, b, x ) = incbet( b, a, 1-x ).
2004 :
2005 : The integral is evaluated by a continued fraction expansion
2006 : or, when b*x is small, by a power series.
2007 :
2008 : ACCURACY:
2009 :
2010 : Tested at uniformly distributed random points (a,b,x) with a and b
2011 : in "domain" and x between 0 and 1.
2012 : Relative error
2013 : arithmetic domain # trials peak rms
2014 : IEEE 0,5 10000 6.9e-15 4.5e-16
2015 : IEEE 0,85 250000 2.2e-13 1.7e-14
2016 : IEEE 0,1000 30000 5.3e-12 6.3e-13
2017 : IEEE 0,10000 250000 9.3e-11 7.1e-12
2018 : IEEE 0,100000 10000 8.7e-10 4.8e-11
2019 : Outputs smaller than the IEEE gradual underflow threshold
2020 : were excluded from these statistics.
2021 :
2022 : Cephes Math Library, Release 2.8: June, 2000
2023 : Copyright 1984, 1995, 2000 by Stephen L. Moshier
2024 : *************************************************************************/
2025 0 : double incompletebeta(const double a, const double b, const double x, const xparams _xparams)
2026 : {
2027 : jmp_buf _break_jump;
2028 : alglib_impl::ae_state _alglib_env_state;
2029 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2030 0 : if( setjmp(_break_jump) )
2031 : {
2032 : #if !defined(AE_NO_EXCEPTIONS)
2033 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2034 : #else
2035 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2036 : return 0;
2037 : #endif
2038 : }
2039 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2040 0 : if( _xparams.flags!=0x0 )
2041 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2042 0 : double result = alglib_impl::incompletebeta(a, b, x, &_alglib_env_state);
2043 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2044 0 : return *(reinterpret_cast<double*>(&result));
2045 : }
2046 :
2047 : /*************************************************************************
2048 : Inverse of imcomplete beta integral
2049 :
2050 : Given y, the function finds x such that
2051 :
2052 : incbet( a, b, x ) = y .
2053 :
2054 : The routine performs interval halving or Newton iterations to find the
2055 : root of incbet(a,b,x) - y = 0.
2056 :
2057 :
2058 : ACCURACY:
2059 :
2060 : Relative error:
2061 : x a,b
2062 : arithmetic domain domain # trials peak rms
2063 : IEEE 0,1 .5,10000 50000 5.8e-12 1.3e-13
2064 : IEEE 0,1 .25,100 100000 1.8e-13 3.9e-15
2065 : IEEE 0,1 0,5 50000 1.1e-12 5.5e-15
2066 : With a and b constrained to half-integer or integer values:
2067 : IEEE 0,1 .5,10000 50000 5.8e-12 1.1e-13
2068 : IEEE 0,1 .5,100 100000 1.7e-14 7.9e-16
2069 : With a = .5, b constrained to half-integer or integer values:
2070 : IEEE 0,1 .5,10000 10000 8.3e-11 1.0e-11
2071 :
2072 : Cephes Math Library Release 2.8: June, 2000
2073 : Copyright 1984, 1996, 2000 by Stephen L. Moshier
2074 : *************************************************************************/
2075 0 : double invincompletebeta(const double a, const double b, const double y, const xparams _xparams)
2076 : {
2077 : jmp_buf _break_jump;
2078 : alglib_impl::ae_state _alglib_env_state;
2079 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2080 0 : if( setjmp(_break_jump) )
2081 : {
2082 : #if !defined(AE_NO_EXCEPTIONS)
2083 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2084 : #else
2085 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2086 : return 0;
2087 : #endif
2088 : }
2089 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2090 0 : if( _xparams.flags!=0x0 )
2091 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2092 0 : double result = alglib_impl::invincompletebeta(a, b, y, &_alglib_env_state);
2093 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2094 0 : return *(reinterpret_cast<double*>(&result));
2095 : }
2096 : #endif
2097 :
2098 : #if defined(AE_COMPILE_FDISTR) || !defined(AE_PARTIAL_BUILD)
2099 : /*************************************************************************
2100 : F distribution
2101 :
2102 : Returns the area from zero to x under the F density
2103 : function (also known as Snedcor's density or the
2104 : variance ratio density). This is the density
2105 : of x = (u1/df1)/(u2/df2), where u1 and u2 are random
2106 : variables having Chi square distributions with df1
2107 : and df2 degrees of freedom, respectively.
2108 : The incomplete beta integral is used, according to the
2109 : formula
2110 :
2111 : P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
2112 :
2113 :
2114 : The arguments a and b are greater than zero, and x is
2115 : nonnegative.
2116 :
2117 : ACCURACY:
2118 :
2119 : Tested at random points (a,b,x).
2120 :
2121 : x a,b Relative error:
2122 : arithmetic domain domain # trials peak rms
2123 : IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
2124 : IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
2125 : IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
2126 : IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
2127 :
2128 : Cephes Math Library Release 2.8: June, 2000
2129 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2130 : *************************************************************************/
2131 0 : double fdistribution(const ae_int_t a, const ae_int_t b, const double x, const xparams _xparams)
2132 : {
2133 : jmp_buf _break_jump;
2134 : alglib_impl::ae_state _alglib_env_state;
2135 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2136 0 : if( setjmp(_break_jump) )
2137 : {
2138 : #if !defined(AE_NO_EXCEPTIONS)
2139 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2140 : #else
2141 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2142 : return 0;
2143 : #endif
2144 : }
2145 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2146 0 : if( _xparams.flags!=0x0 )
2147 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2148 0 : double result = alglib_impl::fdistribution(a, b, x, &_alglib_env_state);
2149 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2150 0 : return *(reinterpret_cast<double*>(&result));
2151 : }
2152 :
2153 : /*************************************************************************
2154 : Complemented F distribution
2155 :
2156 : Returns the area from x to infinity under the F density
2157 : function (also known as Snedcor's density or the
2158 : variance ratio density).
2159 :
2160 :
2161 : inf.
2162 : -
2163 : 1 | | a-1 b-1
2164 : 1-P(x) = ------ | t (1-t) dt
2165 : B(a,b) | |
2166 : -
2167 : x
2168 :
2169 :
2170 : The incomplete beta integral is used, according to the
2171 : formula
2172 :
2173 : P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
2174 :
2175 :
2176 : ACCURACY:
2177 :
2178 : Tested at random points (a,b,x) in the indicated intervals.
2179 : x a,b Relative error:
2180 : arithmetic domain domain # trials peak rms
2181 : IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
2182 : IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
2183 : IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
2184 : IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
2185 :
2186 : Cephes Math Library Release 2.8: June, 2000
2187 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2188 : *************************************************************************/
2189 0 : double fcdistribution(const ae_int_t a, const ae_int_t b, const double x, const xparams _xparams)
2190 : {
2191 : jmp_buf _break_jump;
2192 : alglib_impl::ae_state _alglib_env_state;
2193 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2194 0 : if( setjmp(_break_jump) )
2195 : {
2196 : #if !defined(AE_NO_EXCEPTIONS)
2197 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2198 : #else
2199 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2200 : return 0;
2201 : #endif
2202 : }
2203 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2204 0 : if( _xparams.flags!=0x0 )
2205 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2206 0 : double result = alglib_impl::fcdistribution(a, b, x, &_alglib_env_state);
2207 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2208 0 : return *(reinterpret_cast<double*>(&result));
2209 : }
2210 :
2211 : /*************************************************************************
2212 : Inverse of complemented F distribution
2213 :
2214 : Finds the F density argument x such that the integral
2215 : from x to infinity of the F density is equal to the
2216 : given probability p.
2217 :
2218 : This is accomplished using the inverse beta integral
2219 : function and the relations
2220 :
2221 : z = incbi( df2/2, df1/2, p )
2222 : x = df2 (1-z) / (df1 z).
2223 :
2224 : Note: the following relations hold for the inverse of
2225 : the uncomplemented F distribution:
2226 :
2227 : z = incbi( df1/2, df2/2, p )
2228 : x = df2 z / (df1 (1-z)).
2229 :
2230 : ACCURACY:
2231 :
2232 : Tested at random points (a,b,p).
2233 :
2234 : a,b Relative error:
2235 : arithmetic domain # trials peak rms
2236 : For p between .001 and 1:
2237 : IEEE 1,100 100000 8.3e-15 4.7e-16
2238 : IEEE 1,10000 100000 2.1e-11 1.4e-13
2239 : For p between 10^-6 and 10^-3:
2240 : IEEE 1,100 50000 1.3e-12 8.4e-15
2241 : IEEE 1,10000 50000 3.0e-12 4.8e-14
2242 :
2243 : Cephes Math Library Release 2.8: June, 2000
2244 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2245 : *************************************************************************/
2246 0 : double invfdistribution(const ae_int_t a, const ae_int_t b, const double y, const xparams _xparams)
2247 : {
2248 : jmp_buf _break_jump;
2249 : alglib_impl::ae_state _alglib_env_state;
2250 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2251 0 : if( setjmp(_break_jump) )
2252 : {
2253 : #if !defined(AE_NO_EXCEPTIONS)
2254 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2255 : #else
2256 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2257 : return 0;
2258 : #endif
2259 : }
2260 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2261 0 : if( _xparams.flags!=0x0 )
2262 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2263 0 : double result = alglib_impl::invfdistribution(a, b, y, &_alglib_env_state);
2264 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2265 0 : return *(reinterpret_cast<double*>(&result));
2266 : }
2267 : #endif
2268 :
2269 : #if defined(AE_COMPILE_FRESNEL) || !defined(AE_PARTIAL_BUILD)
2270 : /*************************************************************************
2271 : Fresnel integral
2272 :
2273 : Evaluates the Fresnel integrals
2274 :
2275 : x
2276 : -
2277 : | |
2278 : C(x) = | cos(pi/2 t**2) dt,
2279 : | |
2280 : -
2281 : 0
2282 :
2283 : x
2284 : -
2285 : | |
2286 : S(x) = | sin(pi/2 t**2) dt.
2287 : | |
2288 : -
2289 : 0
2290 :
2291 :
2292 : The integrals are evaluated by a power series for x < 1.
2293 : For x >= 1 auxiliary functions f(x) and g(x) are employed
2294 : such that
2295 :
2296 : C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
2297 : S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
2298 :
2299 :
2300 :
2301 : ACCURACY:
2302 :
2303 : Relative error.
2304 :
2305 : Arithmetic function domain # trials peak rms
2306 : IEEE S(x) 0, 10 10000 2.0e-15 3.2e-16
2307 : IEEE C(x) 0, 10 10000 1.8e-15 3.3e-16
2308 :
2309 : Cephes Math Library Release 2.8: June, 2000
2310 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
2311 : *************************************************************************/
2312 0 : void fresnelintegral(const double x, double &c, double &s, const xparams _xparams)
2313 : {
2314 : jmp_buf _break_jump;
2315 : alglib_impl::ae_state _alglib_env_state;
2316 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2317 0 : if( setjmp(_break_jump) )
2318 : {
2319 : #if !defined(AE_NO_EXCEPTIONS)
2320 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2321 : #else
2322 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2323 : return;
2324 : #endif
2325 : }
2326 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2327 0 : if( _xparams.flags!=0x0 )
2328 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2329 0 : alglib_impl::fresnelintegral(x, &c, &s, &_alglib_env_state);
2330 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2331 0 : return;
2332 : }
2333 : #endif
2334 :
2335 : #if defined(AE_COMPILE_JACOBIANELLIPTIC) || !defined(AE_PARTIAL_BUILD)
2336 : /*************************************************************************
2337 : Jacobian Elliptic Functions
2338 :
2339 : Evaluates the Jacobian elliptic functions sn(u|m), cn(u|m),
2340 : and dn(u|m) of parameter m between 0 and 1, and real
2341 : argument u.
2342 :
2343 : These functions are periodic, with quarter-period on the
2344 : real axis equal to the complete elliptic integral
2345 : ellpk(1.0-m).
2346 :
2347 : Relation to incomplete elliptic integral:
2348 : If u = ellik(phi,m), then sn(u|m) = sin(phi),
2349 : and cn(u|m) = cos(phi). Phi is called the amplitude of u.
2350 :
2351 : Computation is by means of the arithmetic-geometric mean
2352 : algorithm, except when m is within 1e-9 of 0 or 1. In the
2353 : latter case with m close to 1, the approximation applies
2354 : only for phi < pi/2.
2355 :
2356 : ACCURACY:
2357 :
2358 : Tested at random points with u between 0 and 10, m between
2359 : 0 and 1.
2360 :
2361 : Absolute error (* = relative error):
2362 : arithmetic function # trials peak rms
2363 : IEEE phi 10000 9.2e-16* 1.4e-16*
2364 : IEEE sn 50000 4.1e-15 4.6e-16
2365 : IEEE cn 40000 3.6e-15 4.4e-16
2366 : IEEE dn 10000 1.3e-12 1.8e-14
2367 :
2368 : Peak error observed in consistency check using addition
2369 : theorem for sn(u+v) was 4e-16 (absolute). Also tested by
2370 : the above relation to the incomplete elliptic integral.
2371 : Accuracy deteriorates when u is large.
2372 :
2373 : Cephes Math Library Release 2.8: June, 2000
2374 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
2375 : *************************************************************************/
2376 0 : void jacobianellipticfunctions(const double u, const double m, double &sn, double &cn, double &dn, double &ph, const xparams _xparams)
2377 : {
2378 : jmp_buf _break_jump;
2379 : alglib_impl::ae_state _alglib_env_state;
2380 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2381 0 : if( setjmp(_break_jump) )
2382 : {
2383 : #if !defined(AE_NO_EXCEPTIONS)
2384 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2385 : #else
2386 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2387 : return;
2388 : #endif
2389 : }
2390 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2391 0 : if( _xparams.flags!=0x0 )
2392 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2393 0 : alglib_impl::jacobianellipticfunctions(u, m, &sn, &cn, &dn, &ph, &_alglib_env_state);
2394 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2395 0 : return;
2396 : }
2397 : #endif
2398 :
2399 : #if defined(AE_COMPILE_PSIF) || !defined(AE_PARTIAL_BUILD)
2400 : /*************************************************************************
2401 : Psi (digamma) function
2402 :
2403 : d -
2404 : psi(x) = -- ln | (x)
2405 : dx
2406 :
2407 : is the logarithmic derivative of the gamma function.
2408 : For integer x,
2409 : n-1
2410 : -
2411 : psi(n) = -EUL + > 1/k.
2412 : -
2413 : k=1
2414 :
2415 : This formula is used for 0 < n <= 10. If x is negative, it
2416 : is transformed to a positive argument by the reflection
2417 : formula psi(1-x) = psi(x) + pi cot(pi x).
2418 : For general positive x, the argument is made greater than 10
2419 : using the recurrence psi(x+1) = psi(x) + 1/x.
2420 : Then the following asymptotic expansion is applied:
2421 :
2422 : inf. B
2423 : - 2k
2424 : psi(x) = log(x) - 1/2x - > -------
2425 : - 2k
2426 : k=1 2k x
2427 :
2428 : where the B2k are Bernoulli numbers.
2429 :
2430 : ACCURACY:
2431 : Relative error (except absolute when |psi| < 1):
2432 : arithmetic domain # trials peak rms
2433 : IEEE 0,30 30000 1.3e-15 1.4e-16
2434 : IEEE -30,0 40000 1.5e-15 2.2e-16
2435 :
2436 : Cephes Math Library Release 2.8: June, 2000
2437 : Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier
2438 : *************************************************************************/
2439 0 : double psi(const double x, const xparams _xparams)
2440 : {
2441 : jmp_buf _break_jump;
2442 : alglib_impl::ae_state _alglib_env_state;
2443 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2444 0 : if( setjmp(_break_jump) )
2445 : {
2446 : #if !defined(AE_NO_EXCEPTIONS)
2447 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2448 : #else
2449 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2450 : return 0;
2451 : #endif
2452 : }
2453 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2454 0 : if( _xparams.flags!=0x0 )
2455 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2456 0 : double result = alglib_impl::psi(x, &_alglib_env_state);
2457 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2458 0 : return *(reinterpret_cast<double*>(&result));
2459 : }
2460 : #endif
2461 :
2462 : #if defined(AE_COMPILE_EXPINTEGRALS) || !defined(AE_PARTIAL_BUILD)
2463 : /*************************************************************************
2464 : Exponential integral Ei(x)
2465 :
2466 : x
2467 : - t
2468 : | | e
2469 : Ei(x) = -|- --- dt .
2470 : | | t
2471 : -
2472 : -inf
2473 :
2474 : Not defined for x <= 0.
2475 : See also expn.c.
2476 :
2477 :
2478 :
2479 : ACCURACY:
2480 :
2481 : Relative error:
2482 : arithmetic domain # trials peak rms
2483 : IEEE 0,100 50000 8.6e-16 1.3e-16
2484 :
2485 : Cephes Math Library Release 2.8: May, 1999
2486 : Copyright 1999 by Stephen L. Moshier
2487 : *************************************************************************/
2488 0 : double exponentialintegralei(const double x, const xparams _xparams)
2489 : {
2490 : jmp_buf _break_jump;
2491 : alglib_impl::ae_state _alglib_env_state;
2492 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2493 0 : if( setjmp(_break_jump) )
2494 : {
2495 : #if !defined(AE_NO_EXCEPTIONS)
2496 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2497 : #else
2498 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2499 : return 0;
2500 : #endif
2501 : }
2502 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2503 0 : if( _xparams.flags!=0x0 )
2504 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2505 0 : double result = alglib_impl::exponentialintegralei(x, &_alglib_env_state);
2506 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2507 0 : return *(reinterpret_cast<double*>(&result));
2508 : }
2509 :
2510 : /*************************************************************************
2511 : Exponential integral En(x)
2512 :
2513 : Evaluates the exponential integral
2514 :
2515 : inf.
2516 : -
2517 : | | -xt
2518 : | e
2519 : E (x) = | ---- dt.
2520 : n | n
2521 : | | t
2522 : -
2523 : 1
2524 :
2525 :
2526 : Both n and x must be nonnegative.
2527 :
2528 : The routine employs either a power series, a continued
2529 : fraction, or an asymptotic formula depending on the
2530 : relative values of n and x.
2531 :
2532 : ACCURACY:
2533 :
2534 : Relative error:
2535 : arithmetic domain # trials peak rms
2536 : IEEE 0, 30 10000 1.7e-15 3.6e-16
2537 :
2538 : Cephes Math Library Release 2.8: June, 2000
2539 : Copyright 1985, 2000 by Stephen L. Moshier
2540 : *************************************************************************/
2541 0 : double exponentialintegralen(const double x, const ae_int_t n, const xparams _xparams)
2542 : {
2543 : jmp_buf _break_jump;
2544 : alglib_impl::ae_state _alglib_env_state;
2545 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2546 0 : if( setjmp(_break_jump) )
2547 : {
2548 : #if !defined(AE_NO_EXCEPTIONS)
2549 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2550 : #else
2551 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2552 : return 0;
2553 : #endif
2554 : }
2555 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2556 0 : if( _xparams.flags!=0x0 )
2557 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2558 0 : double result = alglib_impl::exponentialintegralen(x, n, &_alglib_env_state);
2559 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2560 0 : return *(reinterpret_cast<double*>(&result));
2561 : }
2562 : #endif
2563 :
2564 : #if defined(AE_COMPILE_LAGUERRE) || !defined(AE_PARTIAL_BUILD)
2565 : /*************************************************************************
2566 : Calculation of the value of the Laguerre polynomial.
2567 :
2568 : Parameters:
2569 : n - degree, n>=0
2570 : x - argument
2571 :
2572 : Result:
2573 : the value of the Laguerre polynomial Ln at x
2574 : *************************************************************************/
2575 0 : double laguerrecalculate(const ae_int_t n, const double x, const xparams _xparams)
2576 : {
2577 : jmp_buf _break_jump;
2578 : alglib_impl::ae_state _alglib_env_state;
2579 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2580 0 : if( setjmp(_break_jump) )
2581 : {
2582 : #if !defined(AE_NO_EXCEPTIONS)
2583 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2584 : #else
2585 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2586 : return 0;
2587 : #endif
2588 : }
2589 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2590 0 : if( _xparams.flags!=0x0 )
2591 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2592 0 : double result = alglib_impl::laguerrecalculate(n, x, &_alglib_env_state);
2593 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2594 0 : return *(reinterpret_cast<double*>(&result));
2595 : }
2596 :
2597 : /*************************************************************************
2598 : Summation of Laguerre polynomials using Clenshaw's recurrence formula.
2599 :
2600 : This routine calculates c[0]*L0(x) + c[1]*L1(x) + ... + c[N]*LN(x)
2601 :
2602 : Parameters:
2603 : n - degree, n>=0
2604 : x - argument
2605 :
2606 : Result:
2607 : the value of the Laguerre polynomial at x
2608 : *************************************************************************/
2609 0 : double laguerresum(const real_1d_array &c, const ae_int_t n, const double x, const xparams _xparams)
2610 : {
2611 : jmp_buf _break_jump;
2612 : alglib_impl::ae_state _alglib_env_state;
2613 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2614 0 : if( setjmp(_break_jump) )
2615 : {
2616 : #if !defined(AE_NO_EXCEPTIONS)
2617 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2618 : #else
2619 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2620 : return 0;
2621 : #endif
2622 : }
2623 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2624 0 : if( _xparams.flags!=0x0 )
2625 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2626 0 : double result = alglib_impl::laguerresum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), n, x, &_alglib_env_state);
2627 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2628 0 : return *(reinterpret_cast<double*>(&result));
2629 : }
2630 :
2631 : /*************************************************************************
2632 : Representation of Ln as C[0] + C[1]*X + ... + C[N]*X^N
2633 :
2634 : Input parameters:
2635 : N - polynomial degree, n>=0
2636 :
2637 : Output parameters:
2638 : C - coefficients
2639 : *************************************************************************/
2640 0 : void laguerrecoefficients(const ae_int_t n, real_1d_array &c, const xparams _xparams)
2641 : {
2642 : jmp_buf _break_jump;
2643 : alglib_impl::ae_state _alglib_env_state;
2644 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2645 0 : if( setjmp(_break_jump) )
2646 : {
2647 : #if !defined(AE_NO_EXCEPTIONS)
2648 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2649 : #else
2650 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2651 : return;
2652 : #endif
2653 : }
2654 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2655 0 : if( _xparams.flags!=0x0 )
2656 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2657 0 : alglib_impl::laguerrecoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
2658 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2659 0 : return;
2660 : }
2661 : #endif
2662 :
2663 : #if defined(AE_COMPILE_CHISQUAREDISTR) || !defined(AE_PARTIAL_BUILD)
2664 : /*************************************************************************
2665 : Chi-square distribution
2666 :
2667 : Returns the area under the left hand tail (from 0 to x)
2668 : of the Chi square probability density function with
2669 : v degrees of freedom.
2670 :
2671 :
2672 : x
2673 : -
2674 : 1 | | v/2-1 -t/2
2675 : P( x | v ) = ----------- | t e dt
2676 : v/2 - | |
2677 : 2 | (v/2) -
2678 : 0
2679 :
2680 : where x is the Chi-square variable.
2681 :
2682 : The incomplete gamma integral is used, according to the
2683 : formula
2684 :
2685 : y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
2686 :
2687 : The arguments must both be positive.
2688 :
2689 : ACCURACY:
2690 :
2691 : See incomplete gamma function
2692 :
2693 :
2694 : Cephes Math Library Release 2.8: June, 2000
2695 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
2696 : *************************************************************************/
2697 0 : double chisquaredistribution(const double v, const double x, const xparams _xparams)
2698 : {
2699 : jmp_buf _break_jump;
2700 : alglib_impl::ae_state _alglib_env_state;
2701 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2702 0 : if( setjmp(_break_jump) )
2703 : {
2704 : #if !defined(AE_NO_EXCEPTIONS)
2705 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2706 : #else
2707 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2708 : return 0;
2709 : #endif
2710 : }
2711 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2712 0 : if( _xparams.flags!=0x0 )
2713 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2714 0 : double result = alglib_impl::chisquaredistribution(v, x, &_alglib_env_state);
2715 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2716 0 : return *(reinterpret_cast<double*>(&result));
2717 : }
2718 :
2719 : /*************************************************************************
2720 : Complemented Chi-square distribution
2721 :
2722 : Returns the area under the right hand tail (from x to
2723 : infinity) of the Chi square probability density function
2724 : with v degrees of freedom:
2725 :
2726 : inf.
2727 : -
2728 : 1 | | v/2-1 -t/2
2729 : P( x | v ) = ----------- | t e dt
2730 : v/2 - | |
2731 : 2 | (v/2) -
2732 : x
2733 :
2734 : where x is the Chi-square variable.
2735 :
2736 : The incomplete gamma integral is used, according to the
2737 : formula
2738 :
2739 : y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
2740 :
2741 : The arguments must both be positive.
2742 :
2743 : ACCURACY:
2744 :
2745 : See incomplete gamma function
2746 :
2747 : Cephes Math Library Release 2.8: June, 2000
2748 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
2749 : *************************************************************************/
2750 0 : double chisquarecdistribution(const double v, const double x, const xparams _xparams)
2751 : {
2752 : jmp_buf _break_jump;
2753 : alglib_impl::ae_state _alglib_env_state;
2754 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2755 0 : if( setjmp(_break_jump) )
2756 : {
2757 : #if !defined(AE_NO_EXCEPTIONS)
2758 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2759 : #else
2760 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2761 : return 0;
2762 : #endif
2763 : }
2764 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2765 0 : if( _xparams.flags!=0x0 )
2766 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2767 0 : double result = alglib_impl::chisquarecdistribution(v, x, &_alglib_env_state);
2768 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2769 0 : return *(reinterpret_cast<double*>(&result));
2770 : }
2771 :
2772 : /*************************************************************************
2773 : Inverse of complemented Chi-square distribution
2774 :
2775 : Finds the Chi-square argument x such that the integral
2776 : from x to infinity of the Chi-square density is equal
2777 : to the given cumulative probability y.
2778 :
2779 : This is accomplished using the inverse gamma integral
2780 : function and the relation
2781 :
2782 : x/2 = igami( df/2, y );
2783 :
2784 : ACCURACY:
2785 :
2786 : See inverse incomplete gamma function
2787 :
2788 :
2789 : Cephes Math Library Release 2.8: June, 2000
2790 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
2791 : *************************************************************************/
2792 0 : double invchisquaredistribution(const double v, const double y, const xparams _xparams)
2793 : {
2794 : jmp_buf _break_jump;
2795 : alglib_impl::ae_state _alglib_env_state;
2796 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2797 0 : if( setjmp(_break_jump) )
2798 : {
2799 : #if !defined(AE_NO_EXCEPTIONS)
2800 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2801 : #else
2802 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2803 : return 0;
2804 : #endif
2805 : }
2806 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2807 0 : if( _xparams.flags!=0x0 )
2808 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2809 0 : double result = alglib_impl::invchisquaredistribution(v, y, &_alglib_env_state);
2810 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2811 0 : return *(reinterpret_cast<double*>(&result));
2812 : }
2813 : #endif
2814 :
2815 : #if defined(AE_COMPILE_LEGENDRE) || !defined(AE_PARTIAL_BUILD)
2816 : /*************************************************************************
2817 : Calculation of the value of the Legendre polynomial Pn.
2818 :
2819 : Parameters:
2820 : n - degree, n>=0
2821 : x - argument
2822 :
2823 : Result:
2824 : the value of the Legendre polynomial Pn at x
2825 : *************************************************************************/
2826 0 : double legendrecalculate(const ae_int_t n, const double x, const xparams _xparams)
2827 : {
2828 : jmp_buf _break_jump;
2829 : alglib_impl::ae_state _alglib_env_state;
2830 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2831 0 : if( setjmp(_break_jump) )
2832 : {
2833 : #if !defined(AE_NO_EXCEPTIONS)
2834 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2835 : #else
2836 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2837 : return 0;
2838 : #endif
2839 : }
2840 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2841 0 : if( _xparams.flags!=0x0 )
2842 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2843 0 : double result = alglib_impl::legendrecalculate(n, x, &_alglib_env_state);
2844 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2845 0 : return *(reinterpret_cast<double*>(&result));
2846 : }
2847 :
2848 : /*************************************************************************
2849 : Summation of Legendre polynomials using Clenshaw's recurrence formula.
2850 :
2851 : This routine calculates
2852 : c[0]*P0(x) + c[1]*P1(x) + ... + c[N]*PN(x)
2853 :
2854 : Parameters:
2855 : n - degree, n>=0
2856 : x - argument
2857 :
2858 : Result:
2859 : the value of the Legendre polynomial at x
2860 : *************************************************************************/
2861 0 : double legendresum(const real_1d_array &c, const ae_int_t n, const double x, const xparams _xparams)
2862 : {
2863 : jmp_buf _break_jump;
2864 : alglib_impl::ae_state _alglib_env_state;
2865 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2866 0 : if( setjmp(_break_jump) )
2867 : {
2868 : #if !defined(AE_NO_EXCEPTIONS)
2869 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2870 : #else
2871 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2872 : return 0;
2873 : #endif
2874 : }
2875 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2876 0 : if( _xparams.flags!=0x0 )
2877 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2878 0 : double result = alglib_impl::legendresum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), n, x, &_alglib_env_state);
2879 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2880 0 : return *(reinterpret_cast<double*>(&result));
2881 : }
2882 :
2883 : /*************************************************************************
2884 : Representation of Pn as C[0] + C[1]*X + ... + C[N]*X^N
2885 :
2886 : Input parameters:
2887 : N - polynomial degree, n>=0
2888 :
2889 : Output parameters:
2890 : C - coefficients
2891 : *************************************************************************/
2892 0 : void legendrecoefficients(const ae_int_t n, real_1d_array &c, const xparams _xparams)
2893 : {
2894 : jmp_buf _break_jump;
2895 : alglib_impl::ae_state _alglib_env_state;
2896 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2897 0 : if( setjmp(_break_jump) )
2898 : {
2899 : #if !defined(AE_NO_EXCEPTIONS)
2900 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2901 : #else
2902 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2903 : return;
2904 : #endif
2905 : }
2906 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2907 0 : if( _xparams.flags!=0x0 )
2908 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2909 0 : alglib_impl::legendrecoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
2910 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2911 0 : return;
2912 : }
2913 : #endif
2914 :
2915 : #if defined(AE_COMPILE_BETAF) || !defined(AE_PARTIAL_BUILD)
2916 : /*************************************************************************
2917 : Beta function
2918 :
2919 :
2920 : - -
2921 : | (a) | (b)
2922 : beta( a, b ) = -----------.
2923 : -
2924 : | (a+b)
2925 :
2926 : For large arguments the logarithm of the function is
2927 : evaluated using lgam(), then exponentiated.
2928 :
2929 : ACCURACY:
2930 :
2931 : Relative error:
2932 : arithmetic domain # trials peak rms
2933 : IEEE 0,30 30000 8.1e-14 1.1e-14
2934 :
2935 : Cephes Math Library Release 2.0: April, 1987
2936 : Copyright 1984, 1987 by Stephen L. Moshier
2937 : *************************************************************************/
2938 0 : double beta(const double a, const double b, const xparams _xparams)
2939 : {
2940 : jmp_buf _break_jump;
2941 : alglib_impl::ae_state _alglib_env_state;
2942 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2943 0 : if( setjmp(_break_jump) )
2944 : {
2945 : #if !defined(AE_NO_EXCEPTIONS)
2946 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2947 : #else
2948 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2949 : return 0;
2950 : #endif
2951 : }
2952 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2953 0 : if( _xparams.flags!=0x0 )
2954 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2955 0 : double result = alglib_impl::beta(a, b, &_alglib_env_state);
2956 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2957 0 : return *(reinterpret_cast<double*>(&result));
2958 : }
2959 : #endif
2960 :
2961 : #if defined(AE_COMPILE_CHEBYSHEV) || !defined(AE_PARTIAL_BUILD)
2962 : /*************************************************************************
2963 : Calculation of the value of the Chebyshev polynomials of the
2964 : first and second kinds.
2965 :
2966 : Parameters:
2967 : r - polynomial kind, either 1 or 2.
2968 : n - degree, n>=0
2969 : x - argument, -1 <= x <= 1
2970 :
2971 : Result:
2972 : the value of the Chebyshev polynomial at x
2973 : *************************************************************************/
2974 0 : double chebyshevcalculate(const ae_int_t r, const ae_int_t n, const double x, const xparams _xparams)
2975 : {
2976 : jmp_buf _break_jump;
2977 : alglib_impl::ae_state _alglib_env_state;
2978 0 : alglib_impl::ae_state_init(&_alglib_env_state);
2979 0 : if( setjmp(_break_jump) )
2980 : {
2981 : #if !defined(AE_NO_EXCEPTIONS)
2982 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
2983 : #else
2984 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
2985 : return 0;
2986 : #endif
2987 : }
2988 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
2989 0 : if( _xparams.flags!=0x0 )
2990 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
2991 0 : double result = alglib_impl::chebyshevcalculate(r, n, x, &_alglib_env_state);
2992 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
2993 0 : return *(reinterpret_cast<double*>(&result));
2994 : }
2995 :
2996 : /*************************************************************************
2997 : Summation of Chebyshev polynomials using Clenshaw's recurrence formula.
2998 :
2999 : This routine calculates
3000 : c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
3001 : or
3002 : c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
3003 : depending on the R.
3004 :
3005 : Parameters:
3006 : r - polynomial kind, either 1 or 2.
3007 : n - degree, n>=0
3008 : x - argument
3009 :
3010 : Result:
3011 : the value of the Chebyshev polynomial at x
3012 : *************************************************************************/
3013 0 : double chebyshevsum(const real_1d_array &c, const ae_int_t r, const ae_int_t n, const double x, const xparams _xparams)
3014 : {
3015 : jmp_buf _break_jump;
3016 : alglib_impl::ae_state _alglib_env_state;
3017 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3018 0 : if( setjmp(_break_jump) )
3019 : {
3020 : #if !defined(AE_NO_EXCEPTIONS)
3021 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3022 : #else
3023 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3024 : return 0;
3025 : #endif
3026 : }
3027 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3028 0 : if( _xparams.flags!=0x0 )
3029 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3030 0 : double result = alglib_impl::chebyshevsum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), r, n, x, &_alglib_env_state);
3031 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3032 0 : return *(reinterpret_cast<double*>(&result));
3033 : }
3034 :
3035 : /*************************************************************************
3036 : Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
3037 :
3038 : Input parameters:
3039 : N - polynomial degree, n>=0
3040 :
3041 : Output parameters:
3042 : C - coefficients
3043 : *************************************************************************/
3044 0 : void chebyshevcoefficients(const ae_int_t n, real_1d_array &c, const xparams _xparams)
3045 : {
3046 : jmp_buf _break_jump;
3047 : alglib_impl::ae_state _alglib_env_state;
3048 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3049 0 : if( setjmp(_break_jump) )
3050 : {
3051 : #if !defined(AE_NO_EXCEPTIONS)
3052 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3053 : #else
3054 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3055 : return;
3056 : #endif
3057 : }
3058 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3059 0 : if( _xparams.flags!=0x0 )
3060 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3061 0 : alglib_impl::chebyshevcoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
3062 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3063 0 : return;
3064 : }
3065 :
3066 : /*************************************************************************
3067 : Conversion of a series of Chebyshev polynomials to a power series.
3068 :
3069 : Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
3070 : B[0] + B[1]*X + ... + B[N]*X^N.
3071 :
3072 : Input parameters:
3073 : A - Chebyshev series coefficients
3074 : N - degree, N>=0
3075 :
3076 : Output parameters
3077 : B - power series coefficients
3078 : *************************************************************************/
3079 0 : void fromchebyshev(const real_1d_array &a, const ae_int_t n, real_1d_array &b, const xparams _xparams)
3080 : {
3081 : jmp_buf _break_jump;
3082 : alglib_impl::ae_state _alglib_env_state;
3083 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3084 0 : if( setjmp(_break_jump) )
3085 : {
3086 : #if !defined(AE_NO_EXCEPTIONS)
3087 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3088 : #else
3089 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3090 : return;
3091 : #endif
3092 : }
3093 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3094 0 : if( _xparams.flags!=0x0 )
3095 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3096 0 : alglib_impl::fromchebyshev(const_cast<alglib_impl::ae_vector*>(a.c_ptr()), n, const_cast<alglib_impl::ae_vector*>(b.c_ptr()), &_alglib_env_state);
3097 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3098 0 : return;
3099 : }
3100 : #endif
3101 :
3102 : #if defined(AE_COMPILE_STUDENTTDISTR) || !defined(AE_PARTIAL_BUILD)
3103 : /*************************************************************************
3104 : Student's t distribution
3105 :
3106 : Computes the integral from minus infinity to t of the Student
3107 : t distribution with integer k > 0 degrees of freedom:
3108 :
3109 : t
3110 : -
3111 : | |
3112 : - | 2 -(k+1)/2
3113 : | ( (k+1)/2 ) | ( x )
3114 : ---------------------- | ( 1 + --- ) dx
3115 : - | ( k )
3116 : sqrt( k pi ) | ( k/2 ) |
3117 : | |
3118 : -
3119 : -inf.
3120 :
3121 : Relation to incomplete beta integral:
3122 :
3123 : 1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
3124 : where
3125 : z = k/(k + t**2).
3126 :
3127 : For t < -2, this is the method of computation. For higher t,
3128 : a direct method is derived from integration by parts.
3129 : Since the function is symmetric about t=0, the area under the
3130 : right tail of the density is found by calling the function
3131 : with -t instead of t.
3132 :
3133 : ACCURACY:
3134 :
3135 : Tested at random 1 <= k <= 25. The "domain" refers to t.
3136 : Relative error:
3137 : arithmetic domain # trials peak rms
3138 : IEEE -100,-2 50000 5.9e-15 1.4e-15
3139 : IEEE -2,100 500000 2.7e-15 4.9e-17
3140 :
3141 : Cephes Math Library Release 2.8: June, 2000
3142 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
3143 : *************************************************************************/
3144 0 : double studenttdistribution(const ae_int_t k, const double t, const xparams _xparams)
3145 : {
3146 : jmp_buf _break_jump;
3147 : alglib_impl::ae_state _alglib_env_state;
3148 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3149 0 : if( setjmp(_break_jump) )
3150 : {
3151 : #if !defined(AE_NO_EXCEPTIONS)
3152 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3153 : #else
3154 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3155 : return 0;
3156 : #endif
3157 : }
3158 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3159 0 : if( _xparams.flags!=0x0 )
3160 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3161 0 : double result = alglib_impl::studenttdistribution(k, t, &_alglib_env_state);
3162 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3163 0 : return *(reinterpret_cast<double*>(&result));
3164 : }
3165 :
3166 : /*************************************************************************
3167 : Functional inverse of Student's t distribution
3168 :
3169 : Given probability p, finds the argument t such that stdtr(k,t)
3170 : is equal to p.
3171 :
3172 : ACCURACY:
3173 :
3174 : Tested at random 1 <= k <= 100. The "domain" refers to p:
3175 : Relative error:
3176 : arithmetic domain # trials peak rms
3177 : IEEE .001,.999 25000 5.7e-15 8.0e-16
3178 : IEEE 10^-6,.001 25000 2.0e-12 2.9e-14
3179 :
3180 : Cephes Math Library Release 2.8: June, 2000
3181 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
3182 : *************************************************************************/
3183 0 : double invstudenttdistribution(const ae_int_t k, const double p, const xparams _xparams)
3184 : {
3185 : jmp_buf _break_jump;
3186 : alglib_impl::ae_state _alglib_env_state;
3187 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3188 0 : if( setjmp(_break_jump) )
3189 : {
3190 : #if !defined(AE_NO_EXCEPTIONS)
3191 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3192 : #else
3193 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3194 : return 0;
3195 : #endif
3196 : }
3197 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3198 0 : if( _xparams.flags!=0x0 )
3199 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3200 0 : double result = alglib_impl::invstudenttdistribution(k, p, &_alglib_env_state);
3201 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3202 0 : return *(reinterpret_cast<double*>(&result));
3203 : }
3204 : #endif
3205 :
3206 : #if defined(AE_COMPILE_BINOMIALDISTR) || !defined(AE_PARTIAL_BUILD)
3207 : /*************************************************************************
3208 : Binomial distribution
3209 :
3210 : Returns the sum of the terms 0 through k of the Binomial
3211 : probability density:
3212 :
3213 : k
3214 : -- ( n ) j n-j
3215 : > ( ) p (1-p)
3216 : -- ( j )
3217 : j=0
3218 :
3219 : The terms are not summed directly; instead the incomplete
3220 : beta integral is employed, according to the formula
3221 :
3222 : y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
3223 :
3224 : The arguments must be positive, with p ranging from 0 to 1.
3225 :
3226 : ACCURACY:
3227 :
3228 : Tested at random points (a,b,p), with p between 0 and 1.
3229 :
3230 : a,b Relative error:
3231 : arithmetic domain # trials peak rms
3232 : For p between 0.001 and 1:
3233 : IEEE 0,100 100000 4.3e-15 2.6e-16
3234 :
3235 : Cephes Math Library Release 2.8: June, 2000
3236 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
3237 : *************************************************************************/
3238 0 : double binomialdistribution(const ae_int_t k, const ae_int_t n, const double p, const xparams _xparams)
3239 : {
3240 : jmp_buf _break_jump;
3241 : alglib_impl::ae_state _alglib_env_state;
3242 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3243 0 : if( setjmp(_break_jump) )
3244 : {
3245 : #if !defined(AE_NO_EXCEPTIONS)
3246 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3247 : #else
3248 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3249 : return 0;
3250 : #endif
3251 : }
3252 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3253 0 : if( _xparams.flags!=0x0 )
3254 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3255 0 : double result = alglib_impl::binomialdistribution(k, n, p, &_alglib_env_state);
3256 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3257 0 : return *(reinterpret_cast<double*>(&result));
3258 : }
3259 :
3260 : /*************************************************************************
3261 : Complemented binomial distribution
3262 :
3263 : Returns the sum of the terms k+1 through n of the Binomial
3264 : probability density:
3265 :
3266 : n
3267 : -- ( n ) j n-j
3268 : > ( ) p (1-p)
3269 : -- ( j )
3270 : j=k+1
3271 :
3272 : The terms are not summed directly; instead the incomplete
3273 : beta integral is employed, according to the formula
3274 :
3275 : y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
3276 :
3277 : The arguments must be positive, with p ranging from 0 to 1.
3278 :
3279 : ACCURACY:
3280 :
3281 : Tested at random points (a,b,p).
3282 :
3283 : a,b Relative error:
3284 : arithmetic domain # trials peak rms
3285 : For p between 0.001 and 1:
3286 : IEEE 0,100 100000 6.7e-15 8.2e-16
3287 : For p between 0 and .001:
3288 : IEEE 0,100 100000 1.5e-13 2.7e-15
3289 :
3290 : Cephes Math Library Release 2.8: June, 2000
3291 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
3292 : *************************************************************************/
3293 0 : double binomialcdistribution(const ae_int_t k, const ae_int_t n, const double p, const xparams _xparams)
3294 : {
3295 : jmp_buf _break_jump;
3296 : alglib_impl::ae_state _alglib_env_state;
3297 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3298 0 : if( setjmp(_break_jump) )
3299 : {
3300 : #if !defined(AE_NO_EXCEPTIONS)
3301 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3302 : #else
3303 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3304 : return 0;
3305 : #endif
3306 : }
3307 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3308 0 : if( _xparams.flags!=0x0 )
3309 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3310 0 : double result = alglib_impl::binomialcdistribution(k, n, p, &_alglib_env_state);
3311 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3312 0 : return *(reinterpret_cast<double*>(&result));
3313 : }
3314 :
3315 : /*************************************************************************
3316 : Inverse binomial distribution
3317 :
3318 : Finds the event probability p such that the sum of the
3319 : terms 0 through k of the Binomial probability density
3320 : is equal to the given cumulative probability y.
3321 :
3322 : This is accomplished using the inverse beta integral
3323 : function and the relation
3324 :
3325 : 1 - p = incbi( n-k, k+1, y ).
3326 :
3327 : ACCURACY:
3328 :
3329 : Tested at random points (a,b,p).
3330 :
3331 : a,b Relative error:
3332 : arithmetic domain # trials peak rms
3333 : For p between 0.001 and 1:
3334 : IEEE 0,100 100000 2.3e-14 6.4e-16
3335 : IEEE 0,10000 100000 6.6e-12 1.2e-13
3336 : For p between 10^-6 and 0.001:
3337 : IEEE 0,100 100000 2.0e-12 1.3e-14
3338 : IEEE 0,10000 100000 1.5e-12 3.2e-14
3339 :
3340 : Cephes Math Library Release 2.8: June, 2000
3341 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
3342 : *************************************************************************/
3343 0 : double invbinomialdistribution(const ae_int_t k, const ae_int_t n, const double y, const xparams _xparams)
3344 : {
3345 : jmp_buf _break_jump;
3346 : alglib_impl::ae_state _alglib_env_state;
3347 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3348 0 : if( setjmp(_break_jump) )
3349 : {
3350 : #if !defined(AE_NO_EXCEPTIONS)
3351 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3352 : #else
3353 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3354 : return 0;
3355 : #endif
3356 : }
3357 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3358 0 : if( _xparams.flags!=0x0 )
3359 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3360 0 : double result = alglib_impl::invbinomialdistribution(k, n, y, &_alglib_env_state);
3361 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3362 0 : return *(reinterpret_cast<double*>(&result));
3363 : }
3364 : #endif
3365 :
3366 : #if defined(AE_COMPILE_AIRYF) || !defined(AE_PARTIAL_BUILD)
3367 : /*************************************************************************
3368 : Airy function
3369 :
3370 : Solution of the differential equation
3371 :
3372 : y"(x) = xy.
3373 :
3374 : The function returns the two independent solutions Ai, Bi
3375 : and their first derivatives Ai'(x), Bi'(x).
3376 :
3377 : Evaluation is by power series summation for small x,
3378 : by rational minimax approximations for large x.
3379 :
3380 :
3381 :
3382 : ACCURACY:
3383 : Error criterion is absolute when function <= 1, relative
3384 : when function > 1, except * denotes relative error criterion.
3385 : For large negative x, the absolute error increases as x^1.5.
3386 : For large positive x, the relative error increases as x^1.5.
3387 :
3388 : Arithmetic domain function # trials peak rms
3389 : IEEE -10, 0 Ai 10000 1.6e-15 2.7e-16
3390 : IEEE 0, 10 Ai 10000 2.3e-14* 1.8e-15*
3391 : IEEE -10, 0 Ai' 10000 4.6e-15 7.6e-16
3392 : IEEE 0, 10 Ai' 10000 1.8e-14* 1.5e-15*
3393 : IEEE -10, 10 Bi 30000 4.2e-15 5.3e-16
3394 : IEEE -10, 10 Bi' 30000 4.9e-15 7.3e-16
3395 :
3396 : Cephes Math Library Release 2.8: June, 2000
3397 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
3398 : *************************************************************************/
3399 0 : void airy(const double x, double &ai, double &aip, double &bi, double &bip, const xparams _xparams)
3400 : {
3401 : jmp_buf _break_jump;
3402 : alglib_impl::ae_state _alglib_env_state;
3403 0 : alglib_impl::ae_state_init(&_alglib_env_state);
3404 0 : if( setjmp(_break_jump) )
3405 : {
3406 : #if !defined(AE_NO_EXCEPTIONS)
3407 0 : _ALGLIB_CPP_EXCEPTION(_alglib_env_state.error_msg);
3408 : #else
3409 : _ALGLIB_SET_ERROR_FLAG(_alglib_env_state.error_msg);
3410 : return;
3411 : #endif
3412 : }
3413 0 : ae_state_set_break_jump(&_alglib_env_state, &_break_jump);
3414 0 : if( _xparams.flags!=0x0 )
3415 0 : ae_state_set_flags(&_alglib_env_state, _xparams.flags);
3416 0 : alglib_impl::airy(x, &ai, &aip, &bi, &bip, &_alglib_env_state);
3417 0 : alglib_impl::ae_state_clear(&_alglib_env_state);
3418 0 : return;
3419 : }
3420 : #endif
3421 : }
3422 :
3423 : /////////////////////////////////////////////////////////////////////////
3424 : //
3425 : // THIS SECTION CONTAINS IMPLEMENTATION OF COMPUTATIONAL CORE
3426 : //
3427 : /////////////////////////////////////////////////////////////////////////
3428 : namespace alglib_impl
3429 : {
3430 : #if defined(AE_COMPILE_GAMMAFUNC) || !defined(AE_PARTIAL_BUILD)
3431 : static double gammafunc_gammastirf(double x, ae_state *_state);
3432 :
3433 :
3434 : #endif
3435 : #if defined(AE_COMPILE_NORMALDISTR) || !defined(AE_PARTIAL_BUILD)
3436 : static double normaldistr_bvnintegrate3(double rangea,
3437 : double rangeb,
3438 : double x,
3439 : double y,
3440 : double gw,
3441 : double gx,
3442 : ae_state *_state);
3443 : static double normaldistr_bvnintegrate6(double rangea,
3444 : double rangeb,
3445 : double x,
3446 : double y,
3447 : double s,
3448 : double gw,
3449 : double gx,
3450 : ae_state *_state);
3451 :
3452 :
3453 : #endif
3454 : #if defined(AE_COMPILE_IGAMMAF) || !defined(AE_PARTIAL_BUILD)
3455 :
3456 :
3457 : #endif
3458 : #if defined(AE_COMPILE_ELLIPTIC) || !defined(AE_PARTIAL_BUILD)
3459 :
3460 :
3461 : #endif
3462 : #if defined(AE_COMPILE_HERMITE) || !defined(AE_PARTIAL_BUILD)
3463 :
3464 :
3465 : #endif
3466 : #if defined(AE_COMPILE_DAWSON) || !defined(AE_PARTIAL_BUILD)
3467 :
3468 :
3469 : #endif
3470 : #if defined(AE_COMPILE_TRIGINTEGRALS) || !defined(AE_PARTIAL_BUILD)
3471 : static void trigintegrals_chebiterationshichi(double x,
3472 : double c,
3473 : double* b0,
3474 : double* b1,
3475 : double* b2,
3476 : ae_state *_state);
3477 :
3478 :
3479 : #endif
3480 : #if defined(AE_COMPILE_POISSONDISTR) || !defined(AE_PARTIAL_BUILD)
3481 :
3482 :
3483 : #endif
3484 : #if defined(AE_COMPILE_BESSEL) || !defined(AE_PARTIAL_BUILD)
3485 : static void bessel_besselmfirstcheb(double c,
3486 : double* b0,
3487 : double* b1,
3488 : double* b2,
3489 : ae_state *_state);
3490 : static void bessel_besselmnextcheb(double x,
3491 : double c,
3492 : double* b0,
3493 : double* b1,
3494 : double* b2,
3495 : ae_state *_state);
3496 : static void bessel_besselm1firstcheb(double c,
3497 : double* b0,
3498 : double* b1,
3499 : double* b2,
3500 : ae_state *_state);
3501 : static void bessel_besselm1nextcheb(double x,
3502 : double c,
3503 : double* b0,
3504 : double* b1,
3505 : double* b2,
3506 : ae_state *_state);
3507 : static void bessel_besselasympt0(double x,
3508 : double* pzero,
3509 : double* qzero,
3510 : ae_state *_state);
3511 : static void bessel_besselasympt1(double x,
3512 : double* pzero,
3513 : double* qzero,
3514 : ae_state *_state);
3515 :
3516 :
3517 : #endif
3518 : #if defined(AE_COMPILE_IBETAF) || !defined(AE_PARTIAL_BUILD)
3519 : static double ibetaf_incompletebetafe(double a,
3520 : double b,
3521 : double x,
3522 : double big,
3523 : double biginv,
3524 : ae_state *_state);
3525 : static double ibetaf_incompletebetafe2(double a,
3526 : double b,
3527 : double x,
3528 : double big,
3529 : double biginv,
3530 : ae_state *_state);
3531 : static double ibetaf_incompletebetaps(double a,
3532 : double b,
3533 : double x,
3534 : double maxgam,
3535 : ae_state *_state);
3536 :
3537 :
3538 : #endif
3539 : #if defined(AE_COMPILE_FDISTR) || !defined(AE_PARTIAL_BUILD)
3540 :
3541 :
3542 : #endif
3543 : #if defined(AE_COMPILE_FRESNEL) || !defined(AE_PARTIAL_BUILD)
3544 :
3545 :
3546 : #endif
3547 : #if defined(AE_COMPILE_JACOBIANELLIPTIC) || !defined(AE_PARTIAL_BUILD)
3548 :
3549 :
3550 : #endif
3551 : #if defined(AE_COMPILE_PSIF) || !defined(AE_PARTIAL_BUILD)
3552 :
3553 :
3554 : #endif
3555 : #if defined(AE_COMPILE_EXPINTEGRALS) || !defined(AE_PARTIAL_BUILD)
3556 :
3557 :
3558 : #endif
3559 : #if defined(AE_COMPILE_LAGUERRE) || !defined(AE_PARTIAL_BUILD)
3560 :
3561 :
3562 : #endif
3563 : #if defined(AE_COMPILE_CHISQUAREDISTR) || !defined(AE_PARTIAL_BUILD)
3564 :
3565 :
3566 : #endif
3567 : #if defined(AE_COMPILE_LEGENDRE) || !defined(AE_PARTIAL_BUILD)
3568 :
3569 :
3570 : #endif
3571 : #if defined(AE_COMPILE_BETAF) || !defined(AE_PARTIAL_BUILD)
3572 :
3573 :
3574 : #endif
3575 : #if defined(AE_COMPILE_CHEBYSHEV) || !defined(AE_PARTIAL_BUILD)
3576 :
3577 :
3578 : #endif
3579 : #if defined(AE_COMPILE_STUDENTTDISTR) || !defined(AE_PARTIAL_BUILD)
3580 :
3581 :
3582 : #endif
3583 : #if defined(AE_COMPILE_BINOMIALDISTR) || !defined(AE_PARTIAL_BUILD)
3584 :
3585 :
3586 : #endif
3587 : #if defined(AE_COMPILE_AIRYF) || !defined(AE_PARTIAL_BUILD)
3588 :
3589 :
3590 : #endif
3591 :
3592 : #if defined(AE_COMPILE_GAMMAFUNC) || !defined(AE_PARTIAL_BUILD)
3593 :
3594 :
3595 : /*************************************************************************
3596 : Gamma function
3597 :
3598 : Input parameters:
3599 : X - argument
3600 :
3601 : Domain:
3602 : 0 < X < 171.6
3603 : -170 < X < 0, X is not an integer.
3604 :
3605 : Relative error:
3606 : arithmetic domain # trials peak rms
3607 : IEEE -170,-33 20000 2.3e-15 3.3e-16
3608 : IEEE -33, 33 20000 9.4e-16 2.2e-16
3609 : IEEE 33, 171.6 20000 2.3e-15 3.2e-16
3610 :
3611 : Cephes Math Library Release 2.8: June, 2000
3612 : Original copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
3613 : Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
3614 : *************************************************************************/
3615 0 : double gammafunction(double x, ae_state *_state)
3616 : {
3617 : #ifndef ALGLIB_INTERCEPTS_SPECFUNCS
3618 : double p;
3619 : double pp;
3620 : double q;
3621 : double qq;
3622 : double z;
3623 : ae_int_t i;
3624 : double sgngam;
3625 : double result;
3626 :
3627 :
3628 0 : sgngam = (double)(1);
3629 0 : q = ae_fabs(x, _state);
3630 0 : if( ae_fp_greater(q,33.0) )
3631 : {
3632 0 : if( ae_fp_less(x,0.0) )
3633 : {
3634 0 : p = (double)(ae_ifloor(q, _state));
3635 0 : i = ae_round(p, _state);
3636 0 : if( i%2==0 )
3637 : {
3638 0 : sgngam = (double)(-1);
3639 : }
3640 0 : z = q-p;
3641 0 : if( ae_fp_greater(z,0.5) )
3642 : {
3643 0 : p = p+1;
3644 0 : z = q-p;
3645 : }
3646 0 : z = q*ae_sin(ae_pi*z, _state);
3647 0 : z = ae_fabs(z, _state);
3648 0 : z = ae_pi/(z*gammafunc_gammastirf(q, _state));
3649 : }
3650 : else
3651 : {
3652 0 : z = gammafunc_gammastirf(x, _state);
3653 : }
3654 0 : result = sgngam*z;
3655 0 : return result;
3656 : }
3657 0 : z = (double)(1);
3658 0 : while(ae_fp_greater_eq(x,(double)(3)))
3659 : {
3660 0 : x = x-1;
3661 0 : z = z*x;
3662 : }
3663 0 : while(ae_fp_less(x,(double)(0)))
3664 : {
3665 0 : if( ae_fp_greater(x,-0.000000001) )
3666 : {
3667 0 : result = z/((1+0.5772156649015329*x)*x);
3668 0 : return result;
3669 : }
3670 0 : z = z/x;
3671 0 : x = x+1;
3672 : }
3673 0 : while(ae_fp_less(x,(double)(2)))
3674 : {
3675 0 : if( ae_fp_less(x,0.000000001) )
3676 : {
3677 0 : result = z/((1+0.5772156649015329*x)*x);
3678 0 : return result;
3679 : }
3680 0 : z = z/x;
3681 0 : x = x+1.0;
3682 : }
3683 0 : if( ae_fp_eq(x,(double)(2)) )
3684 : {
3685 0 : result = z;
3686 0 : return result;
3687 : }
3688 0 : x = x-2.0;
3689 0 : pp = 1.60119522476751861407E-4;
3690 0 : pp = 1.19135147006586384913E-3+x*pp;
3691 0 : pp = 1.04213797561761569935E-2+x*pp;
3692 0 : pp = 4.76367800457137231464E-2+x*pp;
3693 0 : pp = 2.07448227648435975150E-1+x*pp;
3694 0 : pp = 4.94214826801497100753E-1+x*pp;
3695 0 : pp = 9.99999999999999996796E-1+x*pp;
3696 0 : qq = -2.31581873324120129819E-5;
3697 0 : qq = 5.39605580493303397842E-4+x*qq;
3698 0 : qq = -4.45641913851797240494E-3+x*qq;
3699 0 : qq = 1.18139785222060435552E-2+x*qq;
3700 0 : qq = 3.58236398605498653373E-2+x*qq;
3701 0 : qq = -2.34591795718243348568E-1+x*qq;
3702 0 : qq = 7.14304917030273074085E-2+x*qq;
3703 0 : qq = 1.00000000000000000320+x*qq;
3704 0 : result = z*pp/qq;
3705 0 : return result;
3706 : #else
3707 : return _ialglib_i_gammafunction(x);
3708 : #endif
3709 : }
3710 :
3711 :
3712 : /*************************************************************************
3713 : Natural logarithm of gamma function
3714 :
3715 : Input parameters:
3716 : X - argument
3717 :
3718 : Result:
3719 : logarithm of the absolute value of the Gamma(X).
3720 :
3721 : Output parameters:
3722 : SgnGam - sign(Gamma(X))
3723 :
3724 : Domain:
3725 : 0 < X < 2.55e305
3726 : -2.55e305 < X < 0, X is not an integer.
3727 :
3728 : ACCURACY:
3729 : arithmetic domain # trials peak rms
3730 : IEEE 0, 3 28000 5.4e-16 1.1e-16
3731 : IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
3732 : The error criterion was relative when the function magnitude
3733 : was greater than one but absolute when it was less than one.
3734 :
3735 : The following test used the relative error criterion, though
3736 : at certain points the relative error could be much higher than
3737 : indicated.
3738 : IEEE -200, -4 10000 4.8e-16 1.3e-16
3739 :
3740 : Cephes Math Library Release 2.8: June, 2000
3741 : Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
3742 : Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
3743 : *************************************************************************/
3744 0 : double lngamma(double x, double* sgngam, ae_state *_state)
3745 : {
3746 : #ifndef ALGLIB_INTERCEPTS_SPECFUNCS
3747 : double a;
3748 : double b;
3749 : double c;
3750 : double p;
3751 : double q;
3752 : double u;
3753 : double w;
3754 : double z;
3755 : ae_int_t i;
3756 : double logpi;
3757 : double ls2pi;
3758 : double tmp;
3759 : double result;
3760 :
3761 0 : *sgngam = 0;
3762 :
3763 0 : *sgngam = (double)(1);
3764 0 : logpi = 1.14472988584940017414;
3765 0 : ls2pi = 0.91893853320467274178;
3766 0 : if( ae_fp_less(x,-34.0) )
3767 : {
3768 0 : q = -x;
3769 0 : w = lngamma(q, &tmp, _state);
3770 0 : p = (double)(ae_ifloor(q, _state));
3771 0 : i = ae_round(p, _state);
3772 0 : if( i%2==0 )
3773 : {
3774 0 : *sgngam = (double)(-1);
3775 : }
3776 : else
3777 : {
3778 0 : *sgngam = (double)(1);
3779 : }
3780 0 : z = q-p;
3781 0 : if( ae_fp_greater(z,0.5) )
3782 : {
3783 0 : p = p+1;
3784 0 : z = p-q;
3785 : }
3786 0 : z = q*ae_sin(ae_pi*z, _state);
3787 0 : result = logpi-ae_log(z, _state)-w;
3788 0 : return result;
3789 : }
3790 0 : if( ae_fp_less(x,(double)(13)) )
3791 : {
3792 0 : z = (double)(1);
3793 0 : p = (double)(0);
3794 0 : u = x;
3795 0 : while(ae_fp_greater_eq(u,(double)(3)))
3796 : {
3797 0 : p = p-1;
3798 0 : u = x+p;
3799 0 : z = z*u;
3800 : }
3801 0 : while(ae_fp_less(u,(double)(2)))
3802 : {
3803 0 : z = z/u;
3804 0 : p = p+1;
3805 0 : u = x+p;
3806 : }
3807 0 : if( ae_fp_less(z,(double)(0)) )
3808 : {
3809 0 : *sgngam = (double)(-1);
3810 0 : z = -z;
3811 : }
3812 : else
3813 : {
3814 0 : *sgngam = (double)(1);
3815 : }
3816 0 : if( ae_fp_eq(u,(double)(2)) )
3817 : {
3818 0 : result = ae_log(z, _state);
3819 0 : return result;
3820 : }
3821 0 : p = p-2;
3822 0 : x = x+p;
3823 0 : b = -1378.25152569120859100;
3824 0 : b = -38801.6315134637840924+x*b;
3825 0 : b = -331612.992738871184744+x*b;
3826 0 : b = -1162370.97492762307383+x*b;
3827 0 : b = -1721737.00820839662146+x*b;
3828 0 : b = -853555.664245765465627+x*b;
3829 0 : c = (double)(1);
3830 0 : c = -351.815701436523470549+x*c;
3831 0 : c = -17064.2106651881159223+x*c;
3832 0 : c = -220528.590553854454839+x*c;
3833 0 : c = -1139334.44367982507207+x*c;
3834 0 : c = -2532523.07177582951285+x*c;
3835 0 : c = -2018891.41433532773231+x*c;
3836 0 : p = x*b/c;
3837 0 : result = ae_log(z, _state)+p;
3838 0 : return result;
3839 : }
3840 0 : q = (x-0.5)*ae_log(x, _state)-x+ls2pi;
3841 0 : if( ae_fp_greater(x,(double)(100000000)) )
3842 : {
3843 0 : result = q;
3844 0 : return result;
3845 : }
3846 0 : p = 1/(x*x);
3847 0 : if( ae_fp_greater_eq(x,1000.0) )
3848 : {
3849 0 : q = q+((7.9365079365079365079365*0.0001*p-2.7777777777777777777778*0.001)*p+0.0833333333333333333333)/x;
3850 : }
3851 : else
3852 : {
3853 0 : a = 8.11614167470508450300*0.0001;
3854 0 : a = -5.95061904284301438324*0.0001+p*a;
3855 0 : a = 7.93650340457716943945*0.0001+p*a;
3856 0 : a = -2.77777777730099687205*0.001+p*a;
3857 0 : a = 8.33333333333331927722*0.01+p*a;
3858 0 : q = q+a/x;
3859 : }
3860 0 : result = q;
3861 0 : return result;
3862 : #else
3863 : return _ialglib_i_lngamma(x, sgngam);
3864 : #endif
3865 : }
3866 :
3867 :
3868 0 : static double gammafunc_gammastirf(double x, ae_state *_state)
3869 : {
3870 : double y;
3871 : double w;
3872 : double v;
3873 : double stir;
3874 : double result;
3875 :
3876 :
3877 0 : w = 1/x;
3878 0 : stir = 7.87311395793093628397E-4;
3879 0 : stir = -2.29549961613378126380E-4+w*stir;
3880 0 : stir = -2.68132617805781232825E-3+w*stir;
3881 0 : stir = 3.47222221605458667310E-3+w*stir;
3882 0 : stir = 8.33333333333482257126E-2+w*stir;
3883 0 : w = 1+w*stir;
3884 0 : y = ae_exp(x, _state);
3885 0 : if( ae_fp_greater(x,143.01608) )
3886 : {
3887 0 : v = ae_pow(x, 0.5*x-0.25, _state);
3888 0 : y = v*(v/y);
3889 : }
3890 : else
3891 : {
3892 0 : y = ae_pow(x, x-0.5, _state)/y;
3893 : }
3894 0 : result = 2.50662827463100050242*y*w;
3895 0 : return result;
3896 : }
3897 :
3898 :
3899 : #endif
3900 : #if defined(AE_COMPILE_NORMALDISTR) || !defined(AE_PARTIAL_BUILD)
3901 :
3902 :
3903 : /*************************************************************************
3904 : Error function
3905 :
3906 : The integral is
3907 :
3908 : x
3909 : -
3910 : 2 | | 2
3911 : erf(x) = -------- | exp( - t ) dt.
3912 : sqrt(pi) | |
3913 : -
3914 : 0
3915 :
3916 : For 0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2); otherwise
3917 : erf(x) = 1 - erfc(x).
3918 :
3919 :
3920 : ACCURACY:
3921 :
3922 : Relative error:
3923 : arithmetic domain # trials peak rms
3924 : IEEE 0,1 30000 3.7e-16 1.0e-16
3925 :
3926 : Cephes Math Library Release 2.8: June, 2000
3927 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3928 : *************************************************************************/
3929 0 : double errorfunction(double x, ae_state *_state)
3930 : {
3931 : double xsq;
3932 : double s;
3933 : double p;
3934 : double q;
3935 : double result;
3936 :
3937 :
3938 0 : s = (double)(ae_sign(x, _state));
3939 0 : x = ae_fabs(x, _state);
3940 0 : if( ae_fp_less(x,0.5) )
3941 : {
3942 0 : xsq = x*x;
3943 0 : p = 0.007547728033418631287834;
3944 0 : p = -0.288805137207594084924010+xsq*p;
3945 0 : p = 14.3383842191748205576712+xsq*p;
3946 0 : p = 38.0140318123903008244444+xsq*p;
3947 0 : p = 3017.82788536507577809226+xsq*p;
3948 0 : p = 7404.07142710151470082064+xsq*p;
3949 0 : p = 80437.3630960840172832162+xsq*p;
3950 0 : q = 0.0;
3951 0 : q = 1.00000000000000000000000+xsq*q;
3952 0 : q = 38.0190713951939403753468+xsq*q;
3953 0 : q = 658.070155459240506326937+xsq*q;
3954 0 : q = 6379.60017324428279487120+xsq*q;
3955 0 : q = 34216.5257924628539769006+xsq*q;
3956 0 : q = 80437.3630960840172826266+xsq*q;
3957 0 : result = s*1.1283791670955125738961589031*x*p/q;
3958 0 : return result;
3959 : }
3960 0 : if( ae_fp_greater_eq(x,(double)(10)) )
3961 : {
3962 0 : result = s;
3963 0 : return result;
3964 : }
3965 0 : result = s*(1-errorfunctionc(x, _state));
3966 0 : return result;
3967 : }
3968 :
3969 :
3970 : /*************************************************************************
3971 : Complementary error function
3972 :
3973 : 1 - erf(x) =
3974 :
3975 : inf.
3976 : -
3977 : 2 | | 2
3978 : erfc(x) = -------- | exp( - t ) dt
3979 : sqrt(pi) | |
3980 : -
3981 : x
3982 :
3983 :
3984 : For small x, erfc(x) = 1 - erf(x); otherwise rational
3985 : approximations are computed.
3986 :
3987 :
3988 : ACCURACY:
3989 :
3990 : Relative error:
3991 : arithmetic domain # trials peak rms
3992 : IEEE 0,26.6417 30000 5.7e-14 1.5e-14
3993 :
3994 : Cephes Math Library Release 2.8: June, 2000
3995 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3996 : *************************************************************************/
3997 0 : double errorfunctionc(double x, ae_state *_state)
3998 : {
3999 : double p;
4000 : double q;
4001 : double result;
4002 :
4003 :
4004 0 : if( ae_fp_less(x,(double)(0)) )
4005 : {
4006 0 : result = 2-errorfunctionc(-x, _state);
4007 0 : return result;
4008 : }
4009 0 : if( ae_fp_less(x,0.5) )
4010 : {
4011 0 : result = 1.0-errorfunction(x, _state);
4012 0 : return result;
4013 : }
4014 0 : if( ae_fp_greater_eq(x,(double)(10)) )
4015 : {
4016 0 : result = (double)(0);
4017 0 : return result;
4018 : }
4019 0 : p = 0.0;
4020 0 : p = 0.5641877825507397413087057563+x*p;
4021 0 : p = 9.675807882987265400604202961+x*p;
4022 0 : p = 77.08161730368428609781633646+x*p;
4023 0 : p = 368.5196154710010637133875746+x*p;
4024 0 : p = 1143.262070703886173606073338+x*p;
4025 0 : p = 2320.439590251635247384768711+x*p;
4026 0 : p = 2898.0293292167655611275846+x*p;
4027 0 : p = 1826.3348842295112592168999+x*p;
4028 0 : q = 1.0;
4029 0 : q = 17.14980943627607849376131193+x*q;
4030 0 : q = 137.1255960500622202878443578+x*q;
4031 0 : q = 661.7361207107653469211984771+x*q;
4032 0 : q = 2094.384367789539593790281779+x*q;
4033 0 : q = 4429.612803883682726711528526+x*q;
4034 0 : q = 6089.5424232724435504633068+x*q;
4035 0 : q = 4958.82756472114071495438422+x*q;
4036 0 : q = 1826.3348842295112595576438+x*q;
4037 0 : result = ae_exp(-ae_sqr(x, _state), _state)*p/q;
4038 0 : return result;
4039 : }
4040 :
4041 :
4042 : /*************************************************************************
4043 : Same as normalcdf(), obsolete name.
4044 : *************************************************************************/
4045 0 : double normaldistribution(double x, ae_state *_state)
4046 : {
4047 : double result;
4048 :
4049 :
4050 0 : result = 0.5*(errorfunction(x/1.41421356237309504880, _state)+1);
4051 0 : return result;
4052 : }
4053 :
4054 :
4055 : /*************************************************************************
4056 : Normal distribution PDF
4057 :
4058 : Returns Gaussian probability density function:
4059 :
4060 : 1
4061 : f(x) = --------- * exp(-x^2/2)
4062 : sqrt(2pi)
4063 :
4064 : Cephes Math Library Release 2.8: June, 2000
4065 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
4066 : *************************************************************************/
4067 0 : double normalpdf(double x, ae_state *_state)
4068 : {
4069 : double result;
4070 :
4071 :
4072 0 : ae_assert(ae_isfinite(x, _state), "NormalPDF: X is infinite", _state);
4073 0 : result = ae_exp(-x*x/2, _state)/ae_sqrt(2*ae_pi, _state);
4074 0 : return result;
4075 : }
4076 :
4077 :
4078 : /*************************************************************************
4079 : Normal distribution CDF
4080 :
4081 : Returns the area under the Gaussian probability density
4082 : function, integrated from minus infinity to x:
4083 :
4084 : x
4085 : -
4086 : 1 | | 2
4087 : ndtr(x) = --------- | exp( - t /2 ) dt
4088 : sqrt(2pi) | |
4089 : -
4090 : -inf.
4091 :
4092 : = ( 1 + erf(z) ) / 2
4093 : = erfc(z) / 2
4094 :
4095 : where z = x/sqrt(2). Computation is via the functions
4096 : erf and erfc.
4097 :
4098 :
4099 : ACCURACY:
4100 :
4101 : Relative error:
4102 : arithmetic domain # trials peak rms
4103 : IEEE -13,0 30000 3.4e-14 6.7e-15
4104 :
4105 : Cephes Math Library Release 2.8: June, 2000
4106 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
4107 : *************************************************************************/
4108 0 : double normalcdf(double x, ae_state *_state)
4109 : {
4110 : double result;
4111 :
4112 :
4113 0 : result = 0.5*(errorfunction(x/1.41421356237309504880, _state)+1);
4114 0 : return result;
4115 : }
4116 :
4117 :
4118 : /*************************************************************************
4119 : Inverse of the error function
4120 :
4121 : Cephes Math Library Release 2.8: June, 2000
4122 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
4123 : *************************************************************************/
4124 0 : double inverf(double e, ae_state *_state)
4125 : {
4126 : double result;
4127 :
4128 :
4129 0 : result = invnormaldistribution(0.5*(e+1), _state)/ae_sqrt((double)(2), _state);
4130 0 : return result;
4131 : }
4132 :
4133 :
4134 : /*************************************************************************
4135 : Same as invnormalcdf(), deprecated name
4136 : *************************************************************************/
4137 0 : double invnormaldistribution(double y0, ae_state *_state)
4138 : {
4139 : double result;
4140 :
4141 :
4142 0 : result = invnormalcdf(y0, _state);
4143 0 : return result;
4144 : }
4145 :
4146 :
4147 : /*************************************************************************
4148 : Inverse of Normal CDF
4149 :
4150 : Returns the argument, x, for which the area under the
4151 : Gaussian probability density function (integrated from
4152 : minus infinity to x) is equal to y.
4153 :
4154 :
4155 : For small arguments 0 < y < exp(-2), the program computes
4156 : z = sqrt( -2.0 * log(y) ); then the approximation is
4157 : x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
4158 : There are two rational functions P/Q, one for 0 < y < exp(-32)
4159 : and the other for y up to exp(-2). For larger arguments,
4160 : w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
4161 :
4162 : ACCURACY:
4163 :
4164 : Relative error:
4165 : arithmetic domain # trials peak rms
4166 : IEEE 0.125, 1 20000 7.2e-16 1.3e-16
4167 : IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
4168 :
4169 : Cephes Math Library Release 2.8: June, 2000
4170 : Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
4171 : *************************************************************************/
4172 0 : double invnormalcdf(double y0, ae_state *_state)
4173 : {
4174 : double expm2;
4175 : double s2pi;
4176 : double x;
4177 : double y;
4178 : double z;
4179 : double y2;
4180 : double x0;
4181 : double x1;
4182 : ae_int_t code;
4183 : double p0;
4184 : double q0;
4185 : double p1;
4186 : double q1;
4187 : double p2;
4188 : double q2;
4189 : double result;
4190 :
4191 :
4192 0 : expm2 = 0.13533528323661269189;
4193 0 : s2pi = 2.50662827463100050242;
4194 0 : if( ae_fp_less_eq(y0,(double)(0)) )
4195 : {
4196 0 : result = -ae_maxrealnumber;
4197 0 : return result;
4198 : }
4199 0 : if( ae_fp_greater_eq(y0,(double)(1)) )
4200 : {
4201 0 : result = ae_maxrealnumber;
4202 0 : return result;
4203 : }
4204 0 : code = 1;
4205 0 : y = y0;
4206 0 : if( ae_fp_greater(y,1.0-expm2) )
4207 : {
4208 0 : y = 1.0-y;
4209 0 : code = 0;
4210 : }
4211 0 : if( ae_fp_greater(y,expm2) )
4212 : {
4213 0 : y = y-0.5;
4214 0 : y2 = y*y;
4215 0 : p0 = -59.9633501014107895267;
4216 0 : p0 = 98.0010754185999661536+y2*p0;
4217 0 : p0 = -56.6762857469070293439+y2*p0;
4218 0 : p0 = 13.9312609387279679503+y2*p0;
4219 0 : p0 = -1.23916583867381258016+y2*p0;
4220 0 : q0 = (double)(1);
4221 0 : q0 = 1.95448858338141759834+y2*q0;
4222 0 : q0 = 4.67627912898881538453+y2*q0;
4223 0 : q0 = 86.3602421390890590575+y2*q0;
4224 0 : q0 = -225.462687854119370527+y2*q0;
4225 0 : q0 = 200.260212380060660359+y2*q0;
4226 0 : q0 = -82.0372256168333339912+y2*q0;
4227 0 : q0 = 15.9056225126211695515+y2*q0;
4228 0 : q0 = -1.18331621121330003142+y2*q0;
4229 0 : x = y+y*y2*p0/q0;
4230 0 : x = x*s2pi;
4231 0 : result = x;
4232 0 : return result;
4233 : }
4234 0 : x = ae_sqrt(-2.0*ae_log(y, _state), _state);
4235 0 : x0 = x-ae_log(x, _state)/x;
4236 0 : z = 1.0/x;
4237 0 : if( ae_fp_less(x,8.0) )
4238 : {
4239 0 : p1 = 4.05544892305962419923;
4240 0 : p1 = 31.5251094599893866154+z*p1;
4241 0 : p1 = 57.1628192246421288162+z*p1;
4242 0 : p1 = 44.0805073893200834700+z*p1;
4243 0 : p1 = 14.6849561928858024014+z*p1;
4244 0 : p1 = 2.18663306850790267539+z*p1;
4245 0 : p1 = -1.40256079171354495875*0.1+z*p1;
4246 0 : p1 = -3.50424626827848203418*0.01+z*p1;
4247 0 : p1 = -8.57456785154685413611*0.0001+z*p1;
4248 0 : q1 = (double)(1);
4249 0 : q1 = 15.7799883256466749731+z*q1;
4250 0 : q1 = 45.3907635128879210584+z*q1;
4251 0 : q1 = 41.3172038254672030440+z*q1;
4252 0 : q1 = 15.0425385692907503408+z*q1;
4253 0 : q1 = 2.50464946208309415979+z*q1;
4254 0 : q1 = -1.42182922854787788574*0.1+z*q1;
4255 0 : q1 = -3.80806407691578277194*0.01+z*q1;
4256 0 : q1 = -9.33259480895457427372*0.0001+z*q1;
4257 0 : x1 = z*p1/q1;
4258 : }
4259 : else
4260 : {
4261 0 : p2 = 3.23774891776946035970;
4262 0 : p2 = 6.91522889068984211695+z*p2;
4263 0 : p2 = 3.93881025292474443415+z*p2;
4264 0 : p2 = 1.33303460815807542389+z*p2;
4265 0 : p2 = 2.01485389549179081538*0.1+z*p2;
4266 0 : p2 = 1.23716634817820021358*0.01+z*p2;
4267 0 : p2 = 3.01581553508235416007*0.0001+z*p2;
4268 0 : p2 = 2.65806974686737550832*0.000001+z*p2;
4269 0 : p2 = 6.23974539184983293730*0.000000001+z*p2;
4270 0 : q2 = (double)(1);
4271 0 : q2 = 6.02427039364742014255+z*q2;
4272 0 : q2 = 3.67983563856160859403+z*q2;
4273 0 : q2 = 1.37702099489081330271+z*q2;
4274 0 : q2 = 2.16236993594496635890*0.1+z*q2;
4275 0 : q2 = 1.34204006088543189037*0.01+z*q2;
4276 0 : q2 = 3.28014464682127739104*0.0001+z*q2;
4277 0 : q2 = 2.89247864745380683936*0.000001+z*q2;
4278 0 : q2 = 6.79019408009981274425*0.000000001+z*q2;
4279 0 : x1 = z*p2/q2;
4280 : }
4281 0 : x = x0-x1;
4282 0 : if( code!=0 )
4283 : {
4284 0 : x = -x;
4285 : }
4286 0 : result = x;
4287 0 : return result;
4288 : }
4289 :
4290 :
4291 : /*************************************************************************
4292 : Bivariate normal PDF
4293 :
4294 : Returns probability density function of the bivariate Gaussian with
4295 : correlation parameter equal to Rho:
4296 :
4297 : 1 ( x^2 - 2*rho*x*y + y^2 )
4298 : f(x,y,rho) = ----------------- * exp( - ----------------------- )
4299 : 2pi*sqrt(1-rho^2) ( 2*(1-rho^2) )
4300 :
4301 :
4302 : with -1<rho<+1 and arbitrary x, y.
4303 :
4304 : This function won't fail as long as Rho is in (-1,+1) range.
4305 :
4306 : -- ALGLIB --
4307 : Copyright 15.11.2019 by Bochkanov Sergey
4308 : *************************************************************************/
4309 0 : double bivariatenormalpdf(double x,
4310 : double y,
4311 : double rho,
4312 : ae_state *_state)
4313 : {
4314 : double onerho2;
4315 : double result;
4316 :
4317 :
4318 0 : ae_assert(ae_isfinite(x, _state), "BivariateNormalCDF: X is infinite", _state);
4319 0 : ae_assert(ae_isfinite(y, _state), "BivariateNormalCDF: Y is infinite", _state);
4320 0 : ae_assert(ae_isfinite(rho, _state), "BivariateNormalCDF: Rho is infinite", _state);
4321 0 : ae_assert(ae_fp_less((double)(-1),rho)&&ae_fp_less(rho,(double)(1)), "BivariateNormalCDF: Rho is not in (-1,+1) range", _state);
4322 0 : onerho2 = (1-rho)*(1+rho);
4323 0 : result = ae_exp(-(x*x+y*y-2*rho*x*y)/(2*onerho2), _state)/(2*ae_pi*ae_sqrt(onerho2, _state));
4324 0 : return result;
4325 : }
4326 :
4327 :
4328 : /*************************************************************************
4329 : Bivariate normal CDF
4330 :
4331 : Returns the area under the bivariate Gaussian PDF with correlation
4332 : parameter equal to Rho, integrated from minus infinity to (x,y):
4333 :
4334 :
4335 : x y
4336 : - -
4337 : 1 | | | |
4338 : bvn(x,y,rho) = ------------------- | | f(u,v,rho)*du*dv
4339 : 2pi*sqrt(1-rho^2) | | | |
4340 : - -
4341 : -INF -INF
4342 :
4343 :
4344 : where
4345 :
4346 : ( u^2 - 2*rho*u*v + v^2 )
4347 : f(u,v,rho) = exp( - ----------------------- )
4348 : ( 2*(1-rho^2) )
4349 :
4350 :
4351 : with -1<rho<+1 and arbitrary x, y.
4352 :
4353 : This subroutine uses high-precision approximation scheme proposed by
4354 : Alan Genz in "Numerical Computation of Rectangular Bivariate and
4355 : Trivariate Normal and t probabilities", which computes CDF with
4356 : absolute error roughly equal to 1e-14.
4357 :
4358 : This function won't fail as long as Rho is in (-1,+1) range.
4359 :
4360 : -- ALGLIB --
4361 : Copyright 15.11.2019 by Bochkanov Sergey
4362 : *************************************************************************/
4363 0 : double bivariatenormalcdf(double x,
4364 : double y,
4365 : double rho,
4366 : ae_state *_state)
4367 : {
4368 : double rangea;
4369 : double rangeb;
4370 : double s;
4371 : double v;
4372 : double v0;
4373 : double v1;
4374 : double fxys;
4375 : double ta;
4376 : double tb;
4377 : double tc;
4378 : double result;
4379 :
4380 :
4381 0 : ae_assert(ae_isfinite(x, _state), "BivariateNormalCDF: X is infinite", _state);
4382 0 : ae_assert(ae_isfinite(y, _state), "BivariateNormalCDF: Y is infinite", _state);
4383 0 : ae_assert(ae_isfinite(rho, _state), "BivariateNormalCDF: Rho is infinite", _state);
4384 0 : ae_assert(ae_fp_less((double)(-1),rho)&&ae_fp_less(rho,(double)(1)), "BivariateNormalCDF: Rho is not in (-1,+1) range", _state);
4385 0 : if( ae_fp_eq(rho,(double)(0)) )
4386 : {
4387 0 : result = normalcdf(x, _state)*normalcdf(y, _state);
4388 0 : return result;
4389 : }
4390 0 : if( ae_fp_less_eq(ae_fabs(rho, _state),0.8) )
4391 : {
4392 :
4393 : /*
4394 : * Rho is small, compute integral using using formula (3) by Alan Genz, integrated
4395 : * by means of 10-point Gauss-Legendre quadrature
4396 : */
4397 0 : rangea = (double)(0);
4398 0 : rangeb = ae_asin(rho, _state);
4399 0 : v = (double)(0);
4400 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2491470458134028, -0.1252334085114689, _state);
4401 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2491470458134028, 0.1252334085114689, _state);
4402 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2334925365383548, -0.3678314989981802, _state);
4403 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2334925365383548, 0.3678314989981802, _state);
4404 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2031674267230659, -0.5873179542866175, _state);
4405 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.2031674267230659, 0.5873179542866175, _state);
4406 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1600783285433462, -0.7699026741943047, _state);
4407 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1600783285433462, 0.7699026741943047, _state);
4408 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1069393259953184, -0.9041172563704749, _state);
4409 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.1069393259953184, 0.9041172563704749, _state);
4410 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.0471753363865118, -0.9815606342467192, _state);
4411 0 : v = v+normaldistr_bvnintegrate3(rangea, rangeb, x, y, 0.0471753363865118, 0.9815606342467192, _state);
4412 0 : v = v*0.5*(rangeb-rangea)/(2*ae_pi);
4413 0 : result = normalcdf(x, _state)*normalcdf(y, _state)+v;
4414 : }
4415 : else
4416 : {
4417 :
4418 : /*
4419 : * Rho is large, compute integral using using formula (6) by Alan Genz, integrated
4420 : * by means of 20-point Gauss-Legendre quadrature.
4421 : */
4422 0 : x = -x;
4423 0 : y = -y;
4424 0 : s = (double)(ae_sign(rho, _state));
4425 0 : if( ae_fp_greater(s,(double)(0)) )
4426 : {
4427 0 : fxys = normalcdf(-ae_maxreal(x, y, _state), _state);
4428 : }
4429 : else
4430 : {
4431 0 : fxys = ae_maxreal(0.0, normalcdf(-x, _state)-normalcdf(y, _state), _state);
4432 : }
4433 0 : rangea = (double)(0);
4434 0 : rangeb = ae_sqrt((1-rho)*(1+rho), _state);
4435 :
4436 : /*
4437 : * Compute first term (analytic integral) from formula (6)
4438 : */
4439 0 : ta = rangeb;
4440 0 : tb = ae_fabs(x-s*y, _state);
4441 0 : tc = (4-s*x*y)/8;
4442 0 : v0 = ta*(1-tc*(tb*tb-ta*ta)/3)*ae_exp(-tb*tb/(2*ta*ta), _state)-tb*(1-tc*tb*tb/3)*ae_sqrt(2*ae_pi, _state)*normalcdf(-tb/ta, _state);
4443 0 : v0 = v0*ae_exp(-s*x*y/2, _state)/(2*ae_pi);
4444 :
4445 : /*
4446 : * Compute second term (numerical integral, 20-point Gauss-Legendre rule) from formula (6)
4447 : */
4448 0 : v1 = (double)(0);
4449 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1527533871307258, -0.0765265211334973, _state);
4450 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1527533871307258, 0.0765265211334973, _state);
4451 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1491729864726037, -0.2277858511416451, _state);
4452 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1491729864726037, 0.2277858511416451, _state);
4453 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1420961093183820, -0.3737060887154195, _state);
4454 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1420961093183820, 0.3737060887154195, _state);
4455 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1316886384491766, -0.5108670019508271, _state);
4456 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1316886384491766, 0.5108670019508271, _state);
4457 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1181945319615184, -0.6360536807265150, _state);
4458 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1181945319615184, 0.6360536807265150, _state);
4459 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1019301198172404, -0.7463319064601508, _state);
4460 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.1019301198172404, 0.7463319064601508, _state);
4461 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0832767415767048, -0.8391169718222188, _state);
4462 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0832767415767048, 0.8391169718222188, _state);
4463 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0626720483341091, -0.9122344282513259, _state);
4464 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0626720483341091, 0.9122344282513259, _state);
4465 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0406014298003869, -0.9639719272779138, _state);
4466 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0406014298003869, 0.9639719272779138, _state);
4467 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0176140071391521, -0.9931285991850949, _state);
4468 0 : v1 = v1+normaldistr_bvnintegrate6(rangea, rangeb, x, y, s, 0.0176140071391521, 0.9931285991850949, _state);
4469 0 : v1 = v1*0.5*(rangeb-rangea)/(2*ae_pi);
4470 0 : result = fxys-s*(v0+v1);
4471 : }
4472 0 : result = ae_maxreal(result, (double)(0), _state);
4473 0 : result = ae_minreal(result, (double)(1), _state);
4474 0 : return result;
4475 : }
4476 :
4477 :
4478 : /*************************************************************************
4479 : Internal function which computes integrand of formula (3) by Alan
4480 : Genz times Gaussian weights (passed by user).
4481 :
4482 : -- ALGLIB --
4483 : Copyright 15.11.2019 by Bochkanov Sergey
4484 : *************************************************************************/
4485 0 : static double normaldistr_bvnintegrate3(double rangea,
4486 : double rangeb,
4487 : double x,
4488 : double y,
4489 : double gw,
4490 : double gx,
4491 : ae_state *_state)
4492 : {
4493 : double r;
4494 : double t2;
4495 : double dd;
4496 : double sinr;
4497 : double cosr;
4498 : double result;
4499 :
4500 :
4501 0 : r = (rangeb-rangea)*0.5*gx+(rangeb+rangea)*0.5;
4502 0 : t2 = ae_tan(0.5*r, _state);
4503 0 : dd = 1/(1+t2*t2);
4504 0 : sinr = 2*t2*dd;
4505 0 : cosr = (1-t2*t2)*dd;
4506 0 : result = gw*ae_exp(-(x*x+y*y-2*x*y*sinr)/(2*cosr*cosr), _state);
4507 0 : return result;
4508 : }
4509 :
4510 :
4511 : /*************************************************************************
4512 : Internal function which computes integrand of formula (6) by Alan
4513 : Genz times Gaussian weights (passed by user).
4514 :
4515 : -- ALGLIB --
4516 : Copyright 15.11.2019 by Bochkanov Sergey
4517 : *************************************************************************/
4518 0 : static double normaldistr_bvnintegrate6(double rangea,
4519 : double rangeb,
4520 : double x,
4521 : double y,
4522 : double s,
4523 : double gw,
4524 : double gx,
4525 : ae_state *_state)
4526 : {
4527 : double r;
4528 : double exphsk22x2;
4529 : double exphsk2;
4530 : double sqrt1x2;
4531 : double exphsk1sqrt1x2;
4532 : double result;
4533 :
4534 :
4535 0 : r = (rangeb-rangea)*0.5*gx+(rangeb+rangea)*0.5;
4536 0 : exphsk22x2 = ae_exp(-(x-s*y)*(x-s*y)/(2*r*r), _state);
4537 0 : exphsk2 = ae_exp(-x*s*y/2, _state);
4538 0 : sqrt1x2 = ae_sqrt((1-r)*(1+r), _state);
4539 0 : exphsk1sqrt1x2 = ae_exp(-x*s*y/(1+sqrt1x2), _state);
4540 0 : result = gw*exphsk22x2*(exphsk1sqrt1x2/sqrt1x2-exphsk2*(1+(4-x*y*s)*r*r/8));
4541 0 : return result;
4542 : }
4543 :
4544 :
4545 : #endif
4546 : #if defined(AE_COMPILE_IGAMMAF) || !defined(AE_PARTIAL_BUILD)
4547 :
4548 :
4549 : /*************************************************************************
4550 : Incomplete gamma integral
4551 :
4552 : The function is defined by
4553 :
4554 : x
4555 : -
4556 : 1 | | -t a-1
4557 : igam(a,x) = ----- | e t dt.
4558 : - | |
4559 : | (a) -
4560 : 0
4561 :
4562 :
4563 : In this implementation both arguments must be positive.
4564 : The integral is evaluated by either a power series or
4565 : continued fraction expansion, depending on the relative
4566 : values of a and x.
4567 :
4568 : ACCURACY:
4569 :
4570 : Relative error:
4571 : arithmetic domain # trials peak rms
4572 : IEEE 0,30 200000 3.6e-14 2.9e-15
4573 : IEEE 0,100 300000 9.9e-14 1.5e-14
4574 :
4575 : Cephes Math Library Release 2.8: June, 2000
4576 : Copyright 1985, 1987, 2000 by Stephen L. Moshier
4577 : *************************************************************************/
4578 0 : double incompletegamma(double a, double x, ae_state *_state)
4579 : {
4580 : double igammaepsilon;
4581 : double ans;
4582 : double ax;
4583 : double c;
4584 : double r;
4585 : double tmp;
4586 : double result;
4587 :
4588 :
4589 0 : igammaepsilon = 0.000000000000001;
4590 0 : if( ae_fp_less_eq(x,(double)(0))||ae_fp_less_eq(a,(double)(0)) )
4591 : {
4592 0 : result = (double)(0);
4593 0 : return result;
4594 : }
4595 0 : if( ae_fp_greater(x,(double)(1))&&ae_fp_greater(x,a) )
4596 : {
4597 0 : result = 1-incompletegammac(a, x, _state);
4598 0 : return result;
4599 : }
4600 0 : ax = a*ae_log(x, _state)-x-lngamma(a, &tmp, _state);
4601 0 : if( ae_fp_less(ax,-709.78271289338399) )
4602 : {
4603 0 : result = (double)(0);
4604 0 : return result;
4605 : }
4606 0 : ax = ae_exp(ax, _state);
4607 0 : r = a;
4608 0 : c = (double)(1);
4609 0 : ans = (double)(1);
4610 : do
4611 : {
4612 0 : r = r+1;
4613 0 : c = c*x/r;
4614 0 : ans = ans+c;
4615 : }
4616 0 : while(ae_fp_greater(c/ans,igammaepsilon));
4617 0 : result = ans*ax/a;
4618 0 : return result;
4619 : }
4620 :
4621 :
4622 : /*************************************************************************
4623 : Complemented incomplete gamma integral
4624 :
4625 : The function is defined by
4626 :
4627 :
4628 : igamc(a,x) = 1 - igam(a,x)
4629 :
4630 : inf.
4631 : -
4632 : 1 | | -t a-1
4633 : = ----- | e t dt.
4634 : - | |
4635 : | (a) -
4636 : x
4637 :
4638 :
4639 : In this implementation both arguments must be positive.
4640 : The integral is evaluated by either a power series or
4641 : continued fraction expansion, depending on the relative
4642 : values of a and x.
4643 :
4644 : ACCURACY:
4645 :
4646 : Tested at random a, x.
4647 : a x Relative error:
4648 : arithmetic domain domain # trials peak rms
4649 : IEEE 0.5,100 0,100 200000 1.9e-14 1.7e-15
4650 : IEEE 0.01,0.5 0,100 200000 1.4e-13 1.6e-15
4651 :
4652 : Cephes Math Library Release 2.8: June, 2000
4653 : Copyright 1985, 1987, 2000 by Stephen L. Moshier
4654 : *************************************************************************/
4655 0 : double incompletegammac(double a, double x, ae_state *_state)
4656 : {
4657 : double igammaepsilon;
4658 : double igammabignumber;
4659 : double igammabignumberinv;
4660 : double ans;
4661 : double ax;
4662 : double c;
4663 : double yc;
4664 : double r;
4665 : double t;
4666 : double y;
4667 : double z;
4668 : double pk;
4669 : double pkm1;
4670 : double pkm2;
4671 : double qk;
4672 : double qkm1;
4673 : double qkm2;
4674 : double tmp;
4675 : double result;
4676 :
4677 :
4678 0 : igammaepsilon = 0.000000000000001;
4679 0 : igammabignumber = 4503599627370496.0;
4680 0 : igammabignumberinv = 2.22044604925031308085*0.0000000000000001;
4681 0 : if( ae_fp_less_eq(x,(double)(0))||ae_fp_less_eq(a,(double)(0)) )
4682 : {
4683 0 : result = (double)(1);
4684 0 : return result;
4685 : }
4686 0 : if( ae_fp_less(x,(double)(1))||ae_fp_less(x,a) )
4687 : {
4688 0 : result = 1-incompletegamma(a, x, _state);
4689 0 : return result;
4690 : }
4691 0 : ax = a*ae_log(x, _state)-x-lngamma(a, &tmp, _state);
4692 0 : if( ae_fp_less(ax,-709.78271289338399) )
4693 : {
4694 0 : result = (double)(0);
4695 0 : return result;
4696 : }
4697 0 : ax = ae_exp(ax, _state);
4698 0 : y = 1-a;
4699 0 : z = x+y+1;
4700 0 : c = (double)(0);
4701 0 : pkm2 = (double)(1);
4702 0 : qkm2 = x;
4703 0 : pkm1 = x+1;
4704 0 : qkm1 = z*x;
4705 0 : ans = pkm1/qkm1;
4706 : do
4707 : {
4708 0 : c = c+1;
4709 0 : y = y+1;
4710 0 : z = z+2;
4711 0 : yc = y*c;
4712 0 : pk = pkm1*z-pkm2*yc;
4713 0 : qk = qkm1*z-qkm2*yc;
4714 0 : if( ae_fp_neq(qk,(double)(0)) )
4715 : {
4716 0 : r = pk/qk;
4717 0 : t = ae_fabs((ans-r)/r, _state);
4718 0 : ans = r;
4719 : }
4720 : else
4721 : {
4722 0 : t = (double)(1);
4723 : }
4724 0 : pkm2 = pkm1;
4725 0 : pkm1 = pk;
4726 0 : qkm2 = qkm1;
4727 0 : qkm1 = qk;
4728 0 : if( ae_fp_greater(ae_fabs(pk, _state),igammabignumber) )
4729 : {
4730 0 : pkm2 = pkm2*igammabignumberinv;
4731 0 : pkm1 = pkm1*igammabignumberinv;
4732 0 : qkm2 = qkm2*igammabignumberinv;
4733 0 : qkm1 = qkm1*igammabignumberinv;
4734 : }
4735 : }
4736 0 : while(ae_fp_greater(t,igammaepsilon));
4737 0 : result = ans*ax;
4738 0 : return result;
4739 : }
4740 :
4741 :
4742 : /*************************************************************************
4743 : Inverse of complemented imcomplete gamma integral
4744 :
4745 : Given p, the function finds x such that
4746 :
4747 : igamc( a, x ) = p.
4748 :
4749 : Starting with the approximate value
4750 :
4751 : 3
4752 : x = a t
4753 :
4754 : where
4755 :
4756 : t = 1 - d - ndtri(p) sqrt(d)
4757 :
4758 : and
4759 :
4760 : d = 1/9a,
4761 :
4762 : the routine performs up to 10 Newton iterations to find the
4763 : root of igamc(a,x) - p = 0.
4764 :
4765 : ACCURACY:
4766 :
4767 : Tested at random a, p in the intervals indicated.
4768 :
4769 : a p Relative error:
4770 : arithmetic domain domain # trials peak rms
4771 : IEEE 0.5,100 0,0.5 100000 1.0e-14 1.7e-15
4772 : IEEE 0.01,0.5 0,0.5 100000 9.0e-14 3.4e-15
4773 : IEEE 0.5,10000 0,0.5 20000 2.3e-13 3.8e-14
4774 :
4775 : Cephes Math Library Release 2.8: June, 2000
4776 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
4777 : *************************************************************************/
4778 0 : double invincompletegammac(double a, double y0, ae_state *_state)
4779 : {
4780 : double igammaepsilon;
4781 : double iinvgammabignumber;
4782 : double x0;
4783 : double x1;
4784 : double x;
4785 : double yl;
4786 : double yh;
4787 : double y;
4788 : double d;
4789 : double lgm;
4790 : double dithresh;
4791 : ae_int_t i;
4792 : ae_int_t dir;
4793 : double tmp;
4794 : double result;
4795 :
4796 :
4797 0 : igammaepsilon = 0.000000000000001;
4798 0 : iinvgammabignumber = 4503599627370496.0;
4799 0 : x0 = iinvgammabignumber;
4800 0 : yl = (double)(0);
4801 0 : x1 = (double)(0);
4802 0 : yh = (double)(1);
4803 0 : dithresh = 5*igammaepsilon;
4804 0 : d = 1/(9*a);
4805 0 : y = 1-d-invnormaldistribution(y0, _state)*ae_sqrt(d, _state);
4806 0 : x = a*y*y*y;
4807 0 : lgm = lngamma(a, &tmp, _state);
4808 0 : i = 0;
4809 0 : while(i<10)
4810 : {
4811 0 : if( ae_fp_greater(x,x0)||ae_fp_less(x,x1) )
4812 : {
4813 0 : d = 0.0625;
4814 0 : break;
4815 : }
4816 0 : y = incompletegammac(a, x, _state);
4817 0 : if( ae_fp_less(y,yl)||ae_fp_greater(y,yh) )
4818 : {
4819 0 : d = 0.0625;
4820 0 : break;
4821 : }
4822 0 : if( ae_fp_less(y,y0) )
4823 : {
4824 0 : x0 = x;
4825 0 : yl = y;
4826 : }
4827 : else
4828 : {
4829 0 : x1 = x;
4830 0 : yh = y;
4831 : }
4832 0 : d = (a-1)*ae_log(x, _state)-x-lgm;
4833 0 : if( ae_fp_less(d,-709.78271289338399) )
4834 : {
4835 0 : d = 0.0625;
4836 0 : break;
4837 : }
4838 0 : d = -ae_exp(d, _state);
4839 0 : d = (y-y0)/d;
4840 0 : if( ae_fp_less(ae_fabs(d/x, _state),igammaepsilon) )
4841 : {
4842 0 : result = x;
4843 0 : return result;
4844 : }
4845 0 : x = x-d;
4846 0 : i = i+1;
4847 : }
4848 0 : if( ae_fp_eq(x0,iinvgammabignumber) )
4849 : {
4850 0 : if( ae_fp_less_eq(x,(double)(0)) )
4851 : {
4852 0 : x = (double)(1);
4853 : }
4854 0 : while(ae_fp_eq(x0,iinvgammabignumber))
4855 : {
4856 0 : x = (1+d)*x;
4857 0 : y = incompletegammac(a, x, _state);
4858 0 : if( ae_fp_less(y,y0) )
4859 : {
4860 0 : x0 = x;
4861 0 : yl = y;
4862 0 : break;
4863 : }
4864 0 : d = d+d;
4865 : }
4866 : }
4867 0 : d = 0.5;
4868 0 : dir = 0;
4869 0 : i = 0;
4870 0 : while(i<400)
4871 : {
4872 0 : x = x1+d*(x0-x1);
4873 0 : y = incompletegammac(a, x, _state);
4874 0 : lgm = (x0-x1)/(x1+x0);
4875 0 : if( ae_fp_less(ae_fabs(lgm, _state),dithresh) )
4876 : {
4877 0 : break;
4878 : }
4879 0 : lgm = (y-y0)/y0;
4880 0 : if( ae_fp_less(ae_fabs(lgm, _state),dithresh) )
4881 : {
4882 0 : break;
4883 : }
4884 0 : if( ae_fp_less_eq(x,0.0) )
4885 : {
4886 0 : break;
4887 : }
4888 0 : if( ae_fp_greater_eq(y,y0) )
4889 : {
4890 0 : x1 = x;
4891 0 : yh = y;
4892 0 : if( dir<0 )
4893 : {
4894 0 : dir = 0;
4895 0 : d = 0.5;
4896 : }
4897 : else
4898 : {
4899 0 : if( dir>1 )
4900 : {
4901 0 : d = 0.5*d+0.5;
4902 : }
4903 : else
4904 : {
4905 0 : d = (y0-yl)/(yh-yl);
4906 : }
4907 : }
4908 0 : dir = dir+1;
4909 : }
4910 : else
4911 : {
4912 0 : x0 = x;
4913 0 : yl = y;
4914 0 : if( dir>0 )
4915 : {
4916 0 : dir = 0;
4917 0 : d = 0.5;
4918 : }
4919 : else
4920 : {
4921 0 : if( dir<-1 )
4922 : {
4923 0 : d = 0.5*d;
4924 : }
4925 : else
4926 : {
4927 0 : d = (y0-yl)/(yh-yl);
4928 : }
4929 : }
4930 0 : dir = dir-1;
4931 : }
4932 0 : i = i+1;
4933 : }
4934 0 : result = x;
4935 0 : return result;
4936 : }
4937 :
4938 :
4939 : #endif
4940 : #if defined(AE_COMPILE_ELLIPTIC) || !defined(AE_PARTIAL_BUILD)
4941 :
4942 :
4943 : /*************************************************************************
4944 : Complete elliptic integral of the first kind
4945 :
4946 : Approximates the integral
4947 :
4948 :
4949 :
4950 : pi/2
4951 : -
4952 : | |
4953 : | dt
4954 : K(m) = | ------------------
4955 : | 2
4956 : | | sqrt( 1 - m sin t )
4957 : -
4958 : 0
4959 :
4960 : using the approximation
4961 :
4962 : P(x) - log x Q(x).
4963 :
4964 : ACCURACY:
4965 :
4966 : Relative error:
4967 : arithmetic domain # trials peak rms
4968 : IEEE 0,1 30000 2.5e-16 6.8e-17
4969 :
4970 : Cephes Math Library, Release 2.8: June, 2000
4971 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
4972 : *************************************************************************/
4973 0 : double ellipticintegralk(double m, ae_state *_state)
4974 : {
4975 : double result;
4976 :
4977 :
4978 0 : result = ellipticintegralkhighprecision(1.0-m, _state);
4979 0 : return result;
4980 : }
4981 :
4982 :
4983 : /*************************************************************************
4984 : Complete elliptic integral of the first kind
4985 :
4986 : Approximates the integral
4987 :
4988 :
4989 :
4990 : pi/2
4991 : -
4992 : | |
4993 : | dt
4994 : K(m) = | ------------------
4995 : | 2
4996 : | | sqrt( 1 - m sin t )
4997 : -
4998 : 0
4999 :
5000 : where m = 1 - m1, using the approximation
5001 :
5002 : P(x) - log x Q(x).
5003 :
5004 : The argument m1 is used rather than m so that the logarithmic
5005 : singularity at m = 1 will be shifted to the origin; this
5006 : preserves maximum accuracy.
5007 :
5008 : K(0) = pi/2.
5009 :
5010 : ACCURACY:
5011 :
5012 : Relative error:
5013 : arithmetic domain # trials peak rms
5014 : IEEE 0,1 30000 2.5e-16 6.8e-17
5015 :
5016 : Cephes Math Library, Release 2.8: June, 2000
5017 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
5018 : *************************************************************************/
5019 0 : double ellipticintegralkhighprecision(double m1, ae_state *_state)
5020 : {
5021 : double p;
5022 : double q;
5023 : double result;
5024 :
5025 :
5026 0 : if( ae_fp_less_eq(m1,ae_machineepsilon) )
5027 : {
5028 0 : result = 1.3862943611198906188E0-0.5*ae_log(m1, _state);
5029 : }
5030 : else
5031 : {
5032 0 : p = 1.37982864606273237150E-4;
5033 0 : p = p*m1+2.28025724005875567385E-3;
5034 0 : p = p*m1+7.97404013220415179367E-3;
5035 0 : p = p*m1+9.85821379021226008714E-3;
5036 0 : p = p*m1+6.87489687449949877925E-3;
5037 0 : p = p*m1+6.18901033637687613229E-3;
5038 0 : p = p*m1+8.79078273952743772254E-3;
5039 0 : p = p*m1+1.49380448916805252718E-2;
5040 0 : p = p*m1+3.08851465246711995998E-2;
5041 0 : p = p*m1+9.65735902811690126535E-2;
5042 0 : p = p*m1+1.38629436111989062502E0;
5043 0 : q = 2.94078955048598507511E-5;
5044 0 : q = q*m1+9.14184723865917226571E-4;
5045 0 : q = q*m1+5.94058303753167793257E-3;
5046 0 : q = q*m1+1.54850516649762399335E-2;
5047 0 : q = q*m1+2.39089602715924892727E-2;
5048 0 : q = q*m1+3.01204715227604046988E-2;
5049 0 : q = q*m1+3.73774314173823228969E-2;
5050 0 : q = q*m1+4.88280347570998239232E-2;
5051 0 : q = q*m1+7.03124996963957469739E-2;
5052 0 : q = q*m1+1.24999999999870820058E-1;
5053 0 : q = q*m1+4.99999999999999999821E-1;
5054 0 : result = p-q*ae_log(m1, _state);
5055 : }
5056 0 : return result;
5057 : }
5058 :
5059 :
5060 : /*************************************************************************
5061 : Incomplete elliptic integral of the first kind F(phi|m)
5062 :
5063 : Approximates the integral
5064 :
5065 :
5066 :
5067 : phi
5068 : -
5069 : | |
5070 : | dt
5071 : F(phi_\m) = | ------------------
5072 : | 2
5073 : | | sqrt( 1 - m sin t )
5074 : -
5075 : 0
5076 :
5077 : of amplitude phi and modulus m, using the arithmetic -
5078 : geometric mean algorithm.
5079 :
5080 :
5081 :
5082 :
5083 : ACCURACY:
5084 :
5085 : Tested at random points with m in [0, 1] and phi as indicated.
5086 :
5087 : Relative error:
5088 : arithmetic domain # trials peak rms
5089 : IEEE -10,10 200000 7.4e-16 1.0e-16
5090 :
5091 : Cephes Math Library Release 2.8: June, 2000
5092 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
5093 : *************************************************************************/
5094 0 : double incompleteellipticintegralk(double phi, double m, ae_state *_state)
5095 : {
5096 : double a;
5097 : double b;
5098 : double c;
5099 : double e;
5100 : double temp;
5101 : double pio2;
5102 : double t;
5103 : double k;
5104 : ae_int_t d;
5105 : ae_int_t md;
5106 : ae_int_t s;
5107 : ae_int_t npio2;
5108 : double result;
5109 :
5110 :
5111 0 : pio2 = 1.57079632679489661923;
5112 0 : if( ae_fp_eq(m,(double)(0)) )
5113 : {
5114 0 : result = phi;
5115 0 : return result;
5116 : }
5117 0 : a = 1-m;
5118 0 : if( ae_fp_eq(a,(double)(0)) )
5119 : {
5120 0 : result = ae_log(ae_tan(0.5*(pio2+phi), _state), _state);
5121 0 : return result;
5122 : }
5123 0 : npio2 = ae_ifloor(phi/pio2, _state);
5124 0 : if( npio2%2!=0 )
5125 : {
5126 0 : npio2 = npio2+1;
5127 : }
5128 0 : if( npio2!=0 )
5129 : {
5130 0 : k = ellipticintegralk(1-a, _state);
5131 0 : phi = phi-npio2*pio2;
5132 : }
5133 : else
5134 : {
5135 0 : k = (double)(0);
5136 : }
5137 0 : if( ae_fp_less(phi,(double)(0)) )
5138 : {
5139 0 : phi = -phi;
5140 0 : s = -1;
5141 : }
5142 : else
5143 : {
5144 0 : s = 0;
5145 : }
5146 0 : b = ae_sqrt(a, _state);
5147 0 : t = ae_tan(phi, _state);
5148 0 : if( ae_fp_greater(ae_fabs(t, _state),(double)(10)) )
5149 : {
5150 0 : e = 1.0/(b*t);
5151 0 : if( ae_fp_less(ae_fabs(e, _state),(double)(10)) )
5152 : {
5153 0 : e = ae_atan(e, _state);
5154 0 : if( npio2==0 )
5155 : {
5156 0 : k = ellipticintegralk(1-a, _state);
5157 : }
5158 0 : temp = k-incompleteellipticintegralk(e, m, _state);
5159 0 : if( s<0 )
5160 : {
5161 0 : temp = -temp;
5162 : }
5163 0 : result = temp+npio2*k;
5164 0 : return result;
5165 : }
5166 : }
5167 0 : a = 1.0;
5168 0 : c = ae_sqrt(m, _state);
5169 0 : d = 1;
5170 0 : md = 0;
5171 0 : while(ae_fp_greater(ae_fabs(c/a, _state),ae_machineepsilon))
5172 : {
5173 0 : temp = b/a;
5174 0 : phi = phi+ae_atan(t*temp, _state)+md*ae_pi;
5175 0 : md = ae_trunc((phi+pio2)/ae_pi, _state);
5176 0 : t = t*(1.0+temp)/(1.0-temp*t*t);
5177 0 : c = 0.5*(a-b);
5178 0 : temp = ae_sqrt(a*b, _state);
5179 0 : a = 0.5*(a+b);
5180 0 : b = temp;
5181 0 : d = d+d;
5182 : }
5183 0 : temp = (ae_atan(t, _state)+md*ae_pi)/(d*a);
5184 0 : if( s<0 )
5185 : {
5186 0 : temp = -temp;
5187 : }
5188 0 : result = temp+npio2*k;
5189 0 : return result;
5190 : }
5191 :
5192 :
5193 : /*************************************************************************
5194 : Complete elliptic integral of the second kind
5195 :
5196 : Approximates the integral
5197 :
5198 :
5199 : pi/2
5200 : -
5201 : | | 2
5202 : E(m) = | sqrt( 1 - m sin t ) dt
5203 : | |
5204 : -
5205 : 0
5206 :
5207 : using the approximation
5208 :
5209 : P(x) - x log x Q(x).
5210 :
5211 : ACCURACY:
5212 :
5213 : Relative error:
5214 : arithmetic domain # trials peak rms
5215 : IEEE 0, 1 10000 2.1e-16 7.3e-17
5216 :
5217 : Cephes Math Library, Release 2.8: June, 2000
5218 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
5219 : *************************************************************************/
5220 0 : double ellipticintegrale(double m, ae_state *_state)
5221 : {
5222 : double p;
5223 : double q;
5224 : double result;
5225 :
5226 :
5227 0 : ae_assert(ae_fp_greater_eq(m,(double)(0))&&ae_fp_less_eq(m,(double)(1)), "Domain error in EllipticIntegralE: m<0 or m>1", _state);
5228 0 : m = 1-m;
5229 0 : if( ae_fp_eq(m,(double)(0)) )
5230 : {
5231 0 : result = (double)(1);
5232 0 : return result;
5233 : }
5234 0 : p = 1.53552577301013293365E-4;
5235 0 : p = p*m+2.50888492163602060990E-3;
5236 0 : p = p*m+8.68786816565889628429E-3;
5237 0 : p = p*m+1.07350949056076193403E-2;
5238 0 : p = p*m+7.77395492516787092951E-3;
5239 0 : p = p*m+7.58395289413514708519E-3;
5240 0 : p = p*m+1.15688436810574127319E-2;
5241 0 : p = p*m+2.18317996015557253103E-2;
5242 0 : p = p*m+5.68051945617860553470E-2;
5243 0 : p = p*m+4.43147180560990850618E-1;
5244 0 : p = p*m+1.00000000000000000299E0;
5245 0 : q = 3.27954898576485872656E-5;
5246 0 : q = q*m+1.00962792679356715133E-3;
5247 0 : q = q*m+6.50609489976927491433E-3;
5248 0 : q = q*m+1.68862163993311317300E-2;
5249 0 : q = q*m+2.61769742454493659583E-2;
5250 0 : q = q*m+3.34833904888224918614E-2;
5251 0 : q = q*m+4.27180926518931511717E-2;
5252 0 : q = q*m+5.85936634471101055642E-2;
5253 0 : q = q*m+9.37499997197644278445E-2;
5254 0 : q = q*m+2.49999999999888314361E-1;
5255 0 : result = p-q*m*ae_log(m, _state);
5256 0 : return result;
5257 : }
5258 :
5259 :
5260 : /*************************************************************************
5261 : Incomplete elliptic integral of the second kind
5262 :
5263 : Approximates the integral
5264 :
5265 :
5266 : phi
5267 : -
5268 : | |
5269 : | 2
5270 : E(phi_\m) = | sqrt( 1 - m sin t ) dt
5271 : |
5272 : | |
5273 : -
5274 : 0
5275 :
5276 : of amplitude phi and modulus m, using the arithmetic -
5277 : geometric mean algorithm.
5278 :
5279 : ACCURACY:
5280 :
5281 : Tested at random arguments with phi in [-10, 10] and m in
5282 : [0, 1].
5283 : Relative error:
5284 : arithmetic domain # trials peak rms
5285 : IEEE -10,10 150000 3.3e-15 1.4e-16
5286 :
5287 : Cephes Math Library Release 2.8: June, 2000
5288 : Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
5289 : *************************************************************************/
5290 0 : double incompleteellipticintegrale(double phi, double m, ae_state *_state)
5291 : {
5292 : double pio2;
5293 : double a;
5294 : double b;
5295 : double c;
5296 : double e;
5297 : double temp;
5298 : double lphi;
5299 : double t;
5300 : double ebig;
5301 : ae_int_t d;
5302 : ae_int_t md;
5303 : ae_int_t npio2;
5304 : ae_int_t s;
5305 : double result;
5306 :
5307 :
5308 0 : pio2 = 1.57079632679489661923;
5309 0 : if( ae_fp_eq(m,(double)(0)) )
5310 : {
5311 0 : result = phi;
5312 0 : return result;
5313 : }
5314 0 : lphi = phi;
5315 0 : npio2 = ae_ifloor(lphi/pio2, _state);
5316 0 : if( npio2%2!=0 )
5317 : {
5318 0 : npio2 = npio2+1;
5319 : }
5320 0 : lphi = lphi-npio2*pio2;
5321 0 : if( ae_fp_less(lphi,(double)(0)) )
5322 : {
5323 0 : lphi = -lphi;
5324 0 : s = -1;
5325 : }
5326 : else
5327 : {
5328 0 : s = 1;
5329 : }
5330 0 : a = 1.0-m;
5331 0 : ebig = ellipticintegrale(m, _state);
5332 0 : if( ae_fp_eq(a,(double)(0)) )
5333 : {
5334 0 : temp = ae_sin(lphi, _state);
5335 0 : if( s<0 )
5336 : {
5337 0 : temp = -temp;
5338 : }
5339 0 : result = temp+npio2*ebig;
5340 0 : return result;
5341 : }
5342 0 : t = ae_tan(lphi, _state);
5343 0 : b = ae_sqrt(a, _state);
5344 :
5345 : /*
5346 : * Thanks to Brian Fitzgerald <fitzgb@mml0.meche.rpi.edu>
5347 : * for pointing out an instability near odd multiples of pi/2
5348 : */
5349 0 : if( ae_fp_greater(ae_fabs(t, _state),(double)(10)) )
5350 : {
5351 :
5352 : /*
5353 : * Transform the amplitude
5354 : */
5355 0 : e = 1.0/(b*t);
5356 :
5357 : /*
5358 : * ... but avoid multiple recursions.
5359 : */
5360 0 : if( ae_fp_less(ae_fabs(e, _state),(double)(10)) )
5361 : {
5362 0 : e = ae_atan(e, _state);
5363 0 : temp = ebig+m*ae_sin(lphi, _state)*ae_sin(e, _state)-incompleteellipticintegrale(e, m, _state);
5364 0 : if( s<0 )
5365 : {
5366 0 : temp = -temp;
5367 : }
5368 0 : result = temp+npio2*ebig;
5369 0 : return result;
5370 : }
5371 : }
5372 0 : c = ae_sqrt(m, _state);
5373 0 : a = 1.0;
5374 0 : d = 1;
5375 0 : e = 0.0;
5376 0 : md = 0;
5377 0 : while(ae_fp_greater(ae_fabs(c/a, _state),ae_machineepsilon))
5378 : {
5379 0 : temp = b/a;
5380 0 : lphi = lphi+ae_atan(t*temp, _state)+md*ae_pi;
5381 0 : md = ae_trunc((lphi+pio2)/ae_pi, _state);
5382 0 : t = t*(1.0+temp)/(1.0-temp*t*t);
5383 0 : c = 0.5*(a-b);
5384 0 : temp = ae_sqrt(a*b, _state);
5385 0 : a = 0.5*(a+b);
5386 0 : b = temp;
5387 0 : d = d+d;
5388 0 : e = e+c*ae_sin(lphi, _state);
5389 : }
5390 0 : temp = ebig/ellipticintegralk(m, _state);
5391 0 : temp = temp*((ae_atan(t, _state)+md*ae_pi)/(d*a));
5392 0 : temp = temp+e;
5393 0 : if( s<0 )
5394 : {
5395 0 : temp = -temp;
5396 : }
5397 0 : result = temp+npio2*ebig;
5398 0 : return result;
5399 : }
5400 :
5401 :
5402 : #endif
5403 : #if defined(AE_COMPILE_HERMITE) || !defined(AE_PARTIAL_BUILD)
5404 :
5405 :
5406 : /*************************************************************************
5407 : Calculation of the value of the Hermite polynomial.
5408 :
5409 : Parameters:
5410 : n - degree, n>=0
5411 : x - argument
5412 :
5413 : Result:
5414 : the value of the Hermite polynomial Hn at x
5415 : *************************************************************************/
5416 0 : double hermitecalculate(ae_int_t n, double x, ae_state *_state)
5417 : {
5418 : ae_int_t i;
5419 : double a;
5420 : double b;
5421 : double result;
5422 :
5423 :
5424 0 : result = (double)(0);
5425 :
5426 : /*
5427 : * Prepare A and B
5428 : */
5429 0 : a = (double)(1);
5430 0 : b = 2*x;
5431 :
5432 : /*
5433 : * Special cases: N=0 or N=1
5434 : */
5435 0 : if( n==0 )
5436 : {
5437 0 : result = a;
5438 0 : return result;
5439 : }
5440 0 : if( n==1 )
5441 : {
5442 0 : result = b;
5443 0 : return result;
5444 : }
5445 :
5446 : /*
5447 : * General case: N>=2
5448 : */
5449 0 : for(i=2; i<=n; i++)
5450 : {
5451 0 : result = 2*x*b-2*(i-1)*a;
5452 0 : a = b;
5453 0 : b = result;
5454 : }
5455 0 : return result;
5456 : }
5457 :
5458 :
5459 : /*************************************************************************
5460 : Summation of Hermite polynomials using Clenshaw's recurrence formula.
5461 :
5462 : This routine calculates
5463 : c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
5464 :
5465 : Parameters:
5466 : n - degree, n>=0
5467 : x - argument
5468 :
5469 : Result:
5470 : the value of the Hermite polynomial at x
5471 : *************************************************************************/
5472 0 : double hermitesum(/* Real */ ae_vector* c,
5473 : ae_int_t n,
5474 : double x,
5475 : ae_state *_state)
5476 : {
5477 : double b1;
5478 : double b2;
5479 : ae_int_t i;
5480 : double result;
5481 :
5482 :
5483 0 : b1 = (double)(0);
5484 0 : b2 = (double)(0);
5485 0 : result = (double)(0);
5486 0 : for(i=n; i>=0; i--)
5487 : {
5488 0 : result = 2*(x*b1-(i+1)*b2)+c->ptr.p_double[i];
5489 0 : b2 = b1;
5490 0 : b1 = result;
5491 : }
5492 0 : return result;
5493 : }
5494 :
5495 :
5496 : /*************************************************************************
5497 : Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
5498 :
5499 : Input parameters:
5500 : N - polynomial degree, n>=0
5501 :
5502 : Output parameters:
5503 : C - coefficients
5504 : *************************************************************************/
5505 0 : void hermitecoefficients(ae_int_t n,
5506 : /* Real */ ae_vector* c,
5507 : ae_state *_state)
5508 : {
5509 : ae_int_t i;
5510 :
5511 0 : ae_vector_clear(c);
5512 :
5513 0 : ae_vector_set_length(c, n+1, _state);
5514 0 : for(i=0; i<=n; i++)
5515 : {
5516 0 : c->ptr.p_double[i] = (double)(0);
5517 : }
5518 0 : c->ptr.p_double[n] = ae_exp(n*ae_log((double)(2), _state), _state);
5519 0 : for(i=0; i<=n/2-1; i++)
5520 : {
5521 0 : c->ptr.p_double[n-2*(i+1)] = -c->ptr.p_double[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1);
5522 : }
5523 0 : }
5524 :
5525 :
5526 : #endif
5527 : #if defined(AE_COMPILE_DAWSON) || !defined(AE_PARTIAL_BUILD)
5528 :
5529 :
5530 : /*************************************************************************
5531 : Dawson's Integral
5532 :
5533 : Approximates the integral
5534 :
5535 : x
5536 : -
5537 : 2 | | 2
5538 : dawsn(x) = exp( -x ) | exp( t ) dt
5539 : | |
5540 : -
5541 : 0
5542 :
5543 : Three different rational approximations are employed, for
5544 : the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
5545 :
5546 : ACCURACY:
5547 :
5548 : Relative error:
5549 : arithmetic domain # trials peak rms
5550 : IEEE 0,10 10000 6.9e-16 1.0e-16
5551 :
5552 : Cephes Math Library Release 2.8: June, 2000
5553 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
5554 : *************************************************************************/
5555 0 : double dawsonintegral(double x, ae_state *_state)
5556 : {
5557 : double x2;
5558 : double y;
5559 : ae_int_t sg;
5560 : double an;
5561 : double ad;
5562 : double bn;
5563 : double bd;
5564 : double cn;
5565 : double cd;
5566 : double result;
5567 :
5568 :
5569 0 : sg = 1;
5570 0 : if( ae_fp_less(x,(double)(0)) )
5571 : {
5572 0 : sg = -1;
5573 0 : x = -x;
5574 : }
5575 0 : if( ae_fp_less(x,3.25) )
5576 : {
5577 0 : x2 = x*x;
5578 0 : an = 1.13681498971755972054E-11;
5579 0 : an = an*x2+8.49262267667473811108E-10;
5580 0 : an = an*x2+1.94434204175553054283E-8;
5581 0 : an = an*x2+9.53151741254484363489E-7;
5582 0 : an = an*x2+3.07828309874913200438E-6;
5583 0 : an = an*x2+3.52513368520288738649E-4;
5584 0 : an = an*x2+(-8.50149846724410912031E-4);
5585 0 : an = an*x2+4.22618223005546594270E-2;
5586 0 : an = an*x2+(-9.17480371773452345351E-2);
5587 0 : an = an*x2+9.99999999999999994612E-1;
5588 0 : ad = 2.40372073066762605484E-11;
5589 0 : ad = ad*x2+1.48864681368493396752E-9;
5590 0 : ad = ad*x2+5.21265281010541664570E-8;
5591 0 : ad = ad*x2+1.27258478273186970203E-6;
5592 0 : ad = ad*x2+2.32490249820789513991E-5;
5593 0 : ad = ad*x2+3.25524741826057911661E-4;
5594 0 : ad = ad*x2+3.48805814657162590916E-3;
5595 0 : ad = ad*x2+2.79448531198828973716E-2;
5596 0 : ad = ad*x2+1.58874241960120565368E-1;
5597 0 : ad = ad*x2+5.74918629489320327824E-1;
5598 0 : ad = ad*x2+1.00000000000000000539E0;
5599 0 : y = x*an/ad;
5600 0 : result = sg*y;
5601 0 : return result;
5602 : }
5603 0 : x2 = 1.0/(x*x);
5604 0 : if( ae_fp_less(x,6.25) )
5605 : {
5606 0 : bn = 5.08955156417900903354E-1;
5607 0 : bn = bn*x2-2.44754418142697847934E-1;
5608 0 : bn = bn*x2+9.41512335303534411857E-2;
5609 0 : bn = bn*x2-2.18711255142039025206E-2;
5610 0 : bn = bn*x2+3.66207612329569181322E-3;
5611 0 : bn = bn*x2-4.23209114460388756528E-4;
5612 0 : bn = bn*x2+3.59641304793896631888E-5;
5613 0 : bn = bn*x2-2.14640351719968974225E-6;
5614 0 : bn = bn*x2+9.10010780076391431042E-8;
5615 0 : bn = bn*x2-2.40274520828250956942E-9;
5616 0 : bn = bn*x2+3.59233385440928410398E-11;
5617 0 : bd = 1.00000000000000000000E0;
5618 0 : bd = bd*x2-6.31839869873368190192E-1;
5619 0 : bd = bd*x2+2.36706788228248691528E-1;
5620 0 : bd = bd*x2-5.31806367003223277662E-2;
5621 0 : bd = bd*x2+8.48041718586295374409E-3;
5622 0 : bd = bd*x2-9.47996768486665330168E-4;
5623 0 : bd = bd*x2+7.81025592944552338085E-5;
5624 0 : bd = bd*x2-4.55875153252442634831E-6;
5625 0 : bd = bd*x2+1.89100358111421846170E-7;
5626 0 : bd = bd*x2-4.91324691331920606875E-9;
5627 0 : bd = bd*x2+7.18466403235734541950E-11;
5628 0 : y = 1.0/x+x2*bn/(bd*x);
5629 0 : result = sg*0.5*y;
5630 0 : return result;
5631 : }
5632 0 : if( ae_fp_greater(x,1.0E9) )
5633 : {
5634 0 : result = sg*0.5/x;
5635 0 : return result;
5636 : }
5637 0 : cn = -5.90592860534773254987E-1;
5638 0 : cn = cn*x2+6.29235242724368800674E-1;
5639 0 : cn = cn*x2-1.72858975380388136411E-1;
5640 0 : cn = cn*x2+1.64837047825189632310E-2;
5641 0 : cn = cn*x2-4.86827613020462700845E-4;
5642 0 : cd = 1.00000000000000000000E0;
5643 0 : cd = cd*x2-2.69820057197544900361E0;
5644 0 : cd = cd*x2+1.73270799045947845857E0;
5645 0 : cd = cd*x2-3.93708582281939493482E-1;
5646 0 : cd = cd*x2+3.44278924041233391079E-2;
5647 0 : cd = cd*x2-9.73655226040941223894E-4;
5648 0 : y = 1.0/x+x2*cn/(cd*x);
5649 0 : result = sg*0.5*y;
5650 0 : return result;
5651 : }
5652 :
5653 :
5654 : #endif
5655 : #if defined(AE_COMPILE_TRIGINTEGRALS) || !defined(AE_PARTIAL_BUILD)
5656 :
5657 :
5658 : /*************************************************************************
5659 : Sine and cosine integrals
5660 :
5661 : Evaluates the integrals
5662 :
5663 : x
5664 : -
5665 : | cos t - 1
5666 : Ci(x) = eul + ln x + | --------- dt,
5667 : | t
5668 : -
5669 : 0
5670 : x
5671 : -
5672 : | sin t
5673 : Si(x) = | ----- dt
5674 : | t
5675 : -
5676 : 0
5677 :
5678 : where eul = 0.57721566490153286061 is Euler's constant.
5679 : The integrals are approximated by rational functions.
5680 : For x > 8 auxiliary functions f(x) and g(x) are employed
5681 : such that
5682 :
5683 : Ci(x) = f(x) sin(x) - g(x) cos(x)
5684 : Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
5685 :
5686 :
5687 : ACCURACY:
5688 : Test interval = [0,50].
5689 : Absolute error, except relative when > 1:
5690 : arithmetic function # trials peak rms
5691 : IEEE Si 30000 4.4e-16 7.3e-17
5692 : IEEE Ci 30000 6.9e-16 5.1e-17
5693 :
5694 : Cephes Math Library Release 2.1: January, 1989
5695 : Copyright 1984, 1987, 1989 by Stephen L. Moshier
5696 : *************************************************************************/
5697 0 : void sinecosineintegrals(double x,
5698 : double* si,
5699 : double* ci,
5700 : ae_state *_state)
5701 : {
5702 : double z;
5703 : double c;
5704 : double s;
5705 : double f;
5706 : double g;
5707 : ae_int_t sg;
5708 : double sn;
5709 : double sd;
5710 : double cn;
5711 : double cd;
5712 : double fn;
5713 : double fd;
5714 : double gn;
5715 : double gd;
5716 :
5717 0 : *si = 0;
5718 0 : *ci = 0;
5719 :
5720 0 : if( ae_fp_less(x,(double)(0)) )
5721 : {
5722 0 : sg = -1;
5723 0 : x = -x;
5724 : }
5725 : else
5726 : {
5727 0 : sg = 0;
5728 : }
5729 0 : if( ae_fp_eq(x,(double)(0)) )
5730 : {
5731 0 : *si = (double)(0);
5732 0 : *ci = -ae_maxrealnumber;
5733 0 : return;
5734 : }
5735 0 : if( ae_fp_greater(x,1.0E9) )
5736 : {
5737 0 : *si = 1.570796326794896619-ae_cos(x, _state)/x;
5738 0 : *ci = ae_sin(x, _state)/x;
5739 0 : return;
5740 : }
5741 0 : if( ae_fp_less_eq(x,(double)(4)) )
5742 : {
5743 0 : z = x*x;
5744 0 : sn = -8.39167827910303881427E-11;
5745 0 : sn = sn*z+4.62591714427012837309E-8;
5746 0 : sn = sn*z-9.75759303843632795789E-6;
5747 0 : sn = sn*z+9.76945438170435310816E-4;
5748 0 : sn = sn*z-4.13470316229406538752E-2;
5749 0 : sn = sn*z+1.00000000000000000302E0;
5750 0 : sd = 2.03269266195951942049E-12;
5751 0 : sd = sd*z+1.27997891179943299903E-9;
5752 0 : sd = sd*z+4.41827842801218905784E-7;
5753 0 : sd = sd*z+9.96412122043875552487E-5;
5754 0 : sd = sd*z+1.42085239326149893930E-2;
5755 0 : sd = sd*z+9.99999999999999996984E-1;
5756 0 : s = x*sn/sd;
5757 0 : cn = 2.02524002389102268789E-11;
5758 0 : cn = cn*z-1.35249504915790756375E-8;
5759 0 : cn = cn*z+3.59325051419993077021E-6;
5760 0 : cn = cn*z-4.74007206873407909465E-4;
5761 0 : cn = cn*z+2.89159652607555242092E-2;
5762 0 : cn = cn*z-1.00000000000000000080E0;
5763 0 : cd = 4.07746040061880559506E-12;
5764 0 : cd = cd*z+3.06780997581887812692E-9;
5765 0 : cd = cd*z+1.23210355685883423679E-6;
5766 0 : cd = cd*z+3.17442024775032769882E-4;
5767 0 : cd = cd*z+5.10028056236446052392E-2;
5768 0 : cd = cd*z+4.00000000000000000080E0;
5769 0 : c = z*cn/cd;
5770 0 : if( sg!=0 )
5771 : {
5772 0 : s = -s;
5773 : }
5774 0 : *si = s;
5775 0 : *ci = 0.57721566490153286061+ae_log(x, _state)+c;
5776 0 : return;
5777 : }
5778 0 : s = ae_sin(x, _state);
5779 0 : c = ae_cos(x, _state);
5780 0 : z = 1.0/(x*x);
5781 0 : if( ae_fp_less(x,(double)(8)) )
5782 : {
5783 0 : fn = 4.23612862892216586994E0;
5784 0 : fn = fn*z+5.45937717161812843388E0;
5785 0 : fn = fn*z+1.62083287701538329132E0;
5786 0 : fn = fn*z+1.67006611831323023771E-1;
5787 0 : fn = fn*z+6.81020132472518137426E-3;
5788 0 : fn = fn*z+1.08936580650328664411E-4;
5789 0 : fn = fn*z+5.48900223421373614008E-7;
5790 0 : fd = 1.00000000000000000000E0;
5791 0 : fd = fd*z+8.16496634205391016773E0;
5792 0 : fd = fd*z+7.30828822505564552187E0;
5793 0 : fd = fd*z+1.86792257950184183883E0;
5794 0 : fd = fd*z+1.78792052963149907262E-1;
5795 0 : fd = fd*z+7.01710668322789753610E-3;
5796 0 : fd = fd*z+1.10034357153915731354E-4;
5797 0 : fd = fd*z+5.48900252756255700982E-7;
5798 0 : f = fn/(x*fd);
5799 0 : gn = 8.71001698973114191777E-2;
5800 0 : gn = gn*z+6.11379109952219284151E-1;
5801 0 : gn = gn*z+3.97180296392337498885E-1;
5802 0 : gn = gn*z+7.48527737628469092119E-2;
5803 0 : gn = gn*z+5.38868681462177273157E-3;
5804 0 : gn = gn*z+1.61999794598934024525E-4;
5805 0 : gn = gn*z+1.97963874140963632189E-6;
5806 0 : gn = gn*z+7.82579040744090311069E-9;
5807 0 : gd = 1.00000000000000000000E0;
5808 0 : gd = gd*z+1.64402202413355338886E0;
5809 0 : gd = gd*z+6.66296701268987968381E-1;
5810 0 : gd = gd*z+9.88771761277688796203E-2;
5811 0 : gd = gd*z+6.22396345441768420760E-3;
5812 0 : gd = gd*z+1.73221081474177119497E-4;
5813 0 : gd = gd*z+2.02659182086343991969E-6;
5814 0 : gd = gd*z+7.82579218933534490868E-9;
5815 0 : g = z*gn/gd;
5816 : }
5817 : else
5818 : {
5819 0 : fn = 4.55880873470465315206E-1;
5820 0 : fn = fn*z+7.13715274100146711374E-1;
5821 0 : fn = fn*z+1.60300158222319456320E-1;
5822 0 : fn = fn*z+1.16064229408124407915E-2;
5823 0 : fn = fn*z+3.49556442447859055605E-4;
5824 0 : fn = fn*z+4.86215430826454749482E-6;
5825 0 : fn = fn*z+3.20092790091004902806E-8;
5826 0 : fn = fn*z+9.41779576128512936592E-11;
5827 0 : fn = fn*z+9.70507110881952024631E-14;
5828 0 : fd = 1.00000000000000000000E0;
5829 0 : fd = fd*z+9.17463611873684053703E-1;
5830 0 : fd = fd*z+1.78685545332074536321E-1;
5831 0 : fd = fd*z+1.22253594771971293032E-2;
5832 0 : fd = fd*z+3.58696481881851580297E-4;
5833 0 : fd = fd*z+4.92435064317881464393E-6;
5834 0 : fd = fd*z+3.21956939101046018377E-8;
5835 0 : fd = fd*z+9.43720590350276732376E-11;
5836 0 : fd = fd*z+9.70507110881952025725E-14;
5837 0 : f = fn/(x*fd);
5838 0 : gn = 6.97359953443276214934E-1;
5839 0 : gn = gn*z+3.30410979305632063225E-1;
5840 0 : gn = gn*z+3.84878767649974295920E-2;
5841 0 : gn = gn*z+1.71718239052347903558E-3;
5842 0 : gn = gn*z+3.48941165502279436777E-5;
5843 0 : gn = gn*z+3.47131167084116673800E-7;
5844 0 : gn = gn*z+1.70404452782044526189E-9;
5845 0 : gn = gn*z+3.85945925430276600453E-12;
5846 0 : gn = gn*z+3.14040098946363334640E-15;
5847 0 : gd = 1.00000000000000000000E0;
5848 0 : gd = gd*z+1.68548898811011640017E0;
5849 0 : gd = gd*z+4.87852258695304967486E-1;
5850 0 : gd = gd*z+4.67913194259625806320E-2;
5851 0 : gd = gd*z+1.90284426674399523638E-3;
5852 0 : gd = gd*z+3.68475504442561108162E-5;
5853 0 : gd = gd*z+3.57043223443740838771E-7;
5854 0 : gd = gd*z+1.72693748966316146736E-9;
5855 0 : gd = gd*z+3.87830166023954706752E-12;
5856 0 : gd = gd*z+3.14040098946363335242E-15;
5857 0 : g = z*gn/gd;
5858 : }
5859 0 : *si = 1.570796326794896619-f*c-g*s;
5860 0 : if( sg!=0 )
5861 : {
5862 0 : *si = -*si;
5863 : }
5864 0 : *ci = f*s-g*c;
5865 : }
5866 :
5867 :
5868 : /*************************************************************************
5869 : Hyperbolic sine and cosine integrals
5870 :
5871 : Approximates the integrals
5872 :
5873 : x
5874 : -
5875 : | | cosh t - 1
5876 : Chi(x) = eul + ln x + | ----------- dt,
5877 : | | t
5878 : -
5879 : 0
5880 :
5881 : x
5882 : -
5883 : | | sinh t
5884 : Shi(x) = | ------ dt
5885 : | | t
5886 : -
5887 : 0
5888 :
5889 : where eul = 0.57721566490153286061 is Euler's constant.
5890 : The integrals are evaluated by power series for x < 8
5891 : and by Chebyshev expansions for x between 8 and 88.
5892 : For large x, both functions approach exp(x)/2x.
5893 : Arguments greater than 88 in magnitude return MAXNUM.
5894 :
5895 :
5896 : ACCURACY:
5897 :
5898 : Test interval 0 to 88.
5899 : Relative error:
5900 : arithmetic function # trials peak rms
5901 : IEEE Shi 30000 6.9e-16 1.6e-16
5902 : Absolute error, except relative when |Chi| > 1:
5903 : IEEE Chi 30000 8.4e-16 1.4e-16
5904 :
5905 : Cephes Math Library Release 2.8: June, 2000
5906 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
5907 : *************************************************************************/
5908 0 : void hyperbolicsinecosineintegrals(double x,
5909 : double* shi,
5910 : double* chi,
5911 : ae_state *_state)
5912 : {
5913 : double k;
5914 : double z;
5915 : double c;
5916 : double s;
5917 : double a;
5918 : ae_int_t sg;
5919 : double b0;
5920 : double b1;
5921 : double b2;
5922 :
5923 0 : *shi = 0;
5924 0 : *chi = 0;
5925 :
5926 0 : if( ae_fp_less(x,(double)(0)) )
5927 : {
5928 0 : sg = -1;
5929 0 : x = -x;
5930 : }
5931 : else
5932 : {
5933 0 : sg = 0;
5934 : }
5935 0 : if( ae_fp_eq(x,(double)(0)) )
5936 : {
5937 0 : *shi = (double)(0);
5938 0 : *chi = -ae_maxrealnumber;
5939 0 : return;
5940 : }
5941 0 : if( ae_fp_less(x,8.0) )
5942 : {
5943 0 : z = x*x;
5944 0 : a = 1.0;
5945 0 : s = 1.0;
5946 0 : c = 0.0;
5947 0 : k = 2.0;
5948 : do
5949 : {
5950 0 : a = a*z/k;
5951 0 : c = c+a/k;
5952 0 : k = k+1.0;
5953 0 : a = a/k;
5954 0 : s = s+a/k;
5955 0 : k = k+1.0;
5956 : }
5957 0 : while(ae_fp_greater_eq(ae_fabs(a/s, _state),ae_machineepsilon));
5958 0 : s = s*x;
5959 : }
5960 : else
5961 : {
5962 0 : if( ae_fp_less(x,18.0) )
5963 : {
5964 0 : a = (576.0/x-52.0)/10.0;
5965 0 : k = ae_exp(x, _state)/x;
5966 0 : b0 = 1.83889230173399459482E-17;
5967 0 : b1 = 0.0;
5968 0 : trigintegrals_chebiterationshichi(a, -9.55485532279655569575E-17, &b0, &b1, &b2, _state);
5969 0 : trigintegrals_chebiterationshichi(a, 2.04326105980879882648E-16, &b0, &b1, &b2, _state);
5970 0 : trigintegrals_chebiterationshichi(a, 1.09896949074905343022E-15, &b0, &b1, &b2, _state);
5971 0 : trigintegrals_chebiterationshichi(a, -1.31313534344092599234E-14, &b0, &b1, &b2, _state);
5972 0 : trigintegrals_chebiterationshichi(a, 5.93976226264314278932E-14, &b0, &b1, &b2, _state);
5973 0 : trigintegrals_chebiterationshichi(a, -3.47197010497749154755E-14, &b0, &b1, &b2, _state);
5974 0 : trigintegrals_chebiterationshichi(a, -1.40059764613117131000E-12, &b0, &b1, &b2, _state);
5975 0 : trigintegrals_chebiterationshichi(a, 9.49044626224223543299E-12, &b0, &b1, &b2, _state);
5976 0 : trigintegrals_chebiterationshichi(a, -1.61596181145435454033E-11, &b0, &b1, &b2, _state);
5977 0 : trigintegrals_chebiterationshichi(a, -1.77899784436430310321E-10, &b0, &b1, &b2, _state);
5978 0 : trigintegrals_chebiterationshichi(a, 1.35455469767246947469E-9, &b0, &b1, &b2, _state);
5979 0 : trigintegrals_chebiterationshichi(a, -1.03257121792819495123E-9, &b0, &b1, &b2, _state);
5980 0 : trigintegrals_chebiterationshichi(a, -3.56699611114982536845E-8, &b0, &b1, &b2, _state);
5981 0 : trigintegrals_chebiterationshichi(a, 1.44818877384267342057E-7, &b0, &b1, &b2, _state);
5982 0 : trigintegrals_chebiterationshichi(a, 7.82018215184051295296E-7, &b0, &b1, &b2, _state);
5983 0 : trigintegrals_chebiterationshichi(a, -5.39919118403805073710E-6, &b0, &b1, &b2, _state);
5984 0 : trigintegrals_chebiterationshichi(a, -3.12458202168959833422E-5, &b0, &b1, &b2, _state);
5985 0 : trigintegrals_chebiterationshichi(a, 8.90136741950727517826E-5, &b0, &b1, &b2, _state);
5986 0 : trigintegrals_chebiterationshichi(a, 2.02558474743846862168E-3, &b0, &b1, &b2, _state);
5987 0 : trigintegrals_chebiterationshichi(a, 2.96064440855633256972E-2, &b0, &b1, &b2, _state);
5988 0 : trigintegrals_chebiterationshichi(a, 1.11847751047257036625E0, &b0, &b1, &b2, _state);
5989 0 : s = k*0.5*(b0-b2);
5990 0 : b0 = -8.12435385225864036372E-18;
5991 0 : b1 = 0.0;
5992 0 : trigintegrals_chebiterationshichi(a, 2.17586413290339214377E-17, &b0, &b1, &b2, _state);
5993 0 : trigintegrals_chebiterationshichi(a, 5.22624394924072204667E-17, &b0, &b1, &b2, _state);
5994 0 : trigintegrals_chebiterationshichi(a, -9.48812110591690559363E-16, &b0, &b1, &b2, _state);
5995 0 : trigintegrals_chebiterationshichi(a, 5.35546311647465209166E-15, &b0, &b1, &b2, _state);
5996 0 : trigintegrals_chebiterationshichi(a, -1.21009970113732918701E-14, &b0, &b1, &b2, _state);
5997 0 : trigintegrals_chebiterationshichi(a, -6.00865178553447437951E-14, &b0, &b1, &b2, _state);
5998 0 : trigintegrals_chebiterationshichi(a, 7.16339649156028587775E-13, &b0, &b1, &b2, _state);
5999 0 : trigintegrals_chebiterationshichi(a, -2.93496072607599856104E-12, &b0, &b1, &b2, _state);
6000 0 : trigintegrals_chebiterationshichi(a, -1.40359438136491256904E-12, &b0, &b1, &b2, _state);
6001 0 : trigintegrals_chebiterationshichi(a, 8.76302288609054966081E-11, &b0, &b1, &b2, _state);
6002 0 : trigintegrals_chebiterationshichi(a, -4.40092476213282340617E-10, &b0, &b1, &b2, _state);
6003 0 : trigintegrals_chebiterationshichi(a, -1.87992075640569295479E-10, &b0, &b1, &b2, _state);
6004 0 : trigintegrals_chebiterationshichi(a, 1.31458150989474594064E-8, &b0, &b1, &b2, _state);
6005 0 : trigintegrals_chebiterationshichi(a, -4.75513930924765465590E-8, &b0, &b1, &b2, _state);
6006 0 : trigintegrals_chebiterationshichi(a, -2.21775018801848880741E-7, &b0, &b1, &b2, _state);
6007 0 : trigintegrals_chebiterationshichi(a, 1.94635531373272490962E-6, &b0, &b1, &b2, _state);
6008 0 : trigintegrals_chebiterationshichi(a, 4.33505889257316408893E-6, &b0, &b1, &b2, _state);
6009 0 : trigintegrals_chebiterationshichi(a, -6.13387001076494349496E-5, &b0, &b1, &b2, _state);
6010 0 : trigintegrals_chebiterationshichi(a, -3.13085477492997465138E-4, &b0, &b1, &b2, _state);
6011 0 : trigintegrals_chebiterationshichi(a, 4.97164789823116062801E-4, &b0, &b1, &b2, _state);
6012 0 : trigintegrals_chebiterationshichi(a, 2.64347496031374526641E-2, &b0, &b1, &b2, _state);
6013 0 : trigintegrals_chebiterationshichi(a, 1.11446150876699213025E0, &b0, &b1, &b2, _state);
6014 0 : c = k*0.5*(b0-b2);
6015 : }
6016 : else
6017 : {
6018 0 : if( ae_fp_less_eq(x,88.0) )
6019 : {
6020 0 : a = (6336.0/x-212.0)/70.0;
6021 0 : k = ae_exp(x, _state)/x;
6022 0 : b0 = -1.05311574154850938805E-17;
6023 0 : b1 = 0.0;
6024 0 : trigintegrals_chebiterationshichi(a, 2.62446095596355225821E-17, &b0, &b1, &b2, _state);
6025 0 : trigintegrals_chebiterationshichi(a, 8.82090135625368160657E-17, &b0, &b1, &b2, _state);
6026 0 : trigintegrals_chebiterationshichi(a, -3.38459811878103047136E-16, &b0, &b1, &b2, _state);
6027 0 : trigintegrals_chebiterationshichi(a, -8.30608026366935789136E-16, &b0, &b1, &b2, _state);
6028 0 : trigintegrals_chebiterationshichi(a, 3.93397875437050071776E-15, &b0, &b1, &b2, _state);
6029 0 : trigintegrals_chebiterationshichi(a, 1.01765565969729044505E-14, &b0, &b1, &b2, _state);
6030 0 : trigintegrals_chebiterationshichi(a, -4.21128170307640802703E-14, &b0, &b1, &b2, _state);
6031 0 : trigintegrals_chebiterationshichi(a, -1.60818204519802480035E-13, &b0, &b1, &b2, _state);
6032 0 : trigintegrals_chebiterationshichi(a, 3.34714954175994481761E-13, &b0, &b1, &b2, _state);
6033 0 : trigintegrals_chebiterationshichi(a, 2.72600352129153073807E-12, &b0, &b1, &b2, _state);
6034 0 : trigintegrals_chebiterationshichi(a, 1.66894954752839083608E-12, &b0, &b1, &b2, _state);
6035 0 : trigintegrals_chebiterationshichi(a, -3.49278141024730899554E-11, &b0, &b1, &b2, _state);
6036 0 : trigintegrals_chebiterationshichi(a, -1.58580661666482709598E-10, &b0, &b1, &b2, _state);
6037 0 : trigintegrals_chebiterationshichi(a, -1.79289437183355633342E-10, &b0, &b1, &b2, _state);
6038 0 : trigintegrals_chebiterationshichi(a, 1.76281629144264523277E-9, &b0, &b1, &b2, _state);
6039 0 : trigintegrals_chebiterationshichi(a, 1.69050228879421288846E-8, &b0, &b1, &b2, _state);
6040 0 : trigintegrals_chebiterationshichi(a, 1.25391771228487041649E-7, &b0, &b1, &b2, _state);
6041 0 : trigintegrals_chebiterationshichi(a, 1.16229947068677338732E-6, &b0, &b1, &b2, _state);
6042 0 : trigintegrals_chebiterationshichi(a, 1.61038260117376323993E-5, &b0, &b1, &b2, _state);
6043 0 : trigintegrals_chebiterationshichi(a, 3.49810375601053973070E-4, &b0, &b1, &b2, _state);
6044 0 : trigintegrals_chebiterationshichi(a, 1.28478065259647610779E-2, &b0, &b1, &b2, _state);
6045 0 : trigintegrals_chebiterationshichi(a, 1.03665722588798326712E0, &b0, &b1, &b2, _state);
6046 0 : s = k*0.5*(b0-b2);
6047 0 : b0 = 8.06913408255155572081E-18;
6048 0 : b1 = 0.0;
6049 0 : trigintegrals_chebiterationshichi(a, -2.08074168180148170312E-17, &b0, &b1, &b2, _state);
6050 0 : trigintegrals_chebiterationshichi(a, -5.98111329658272336816E-17, &b0, &b1, &b2, _state);
6051 0 : trigintegrals_chebiterationshichi(a, 2.68533951085945765591E-16, &b0, &b1, &b2, _state);
6052 0 : trigintegrals_chebiterationshichi(a, 4.52313941698904694774E-16, &b0, &b1, &b2, _state);
6053 0 : trigintegrals_chebiterationshichi(a, -3.10734917335299464535E-15, &b0, &b1, &b2, _state);
6054 0 : trigintegrals_chebiterationshichi(a, -4.42823207332531972288E-15, &b0, &b1, &b2, _state);
6055 0 : trigintegrals_chebiterationshichi(a, 3.49639695410806959872E-14, &b0, &b1, &b2, _state);
6056 0 : trigintegrals_chebiterationshichi(a, 6.63406731718911586609E-14, &b0, &b1, &b2, _state);
6057 0 : trigintegrals_chebiterationshichi(a, -3.71902448093119218395E-13, &b0, &b1, &b2, _state);
6058 0 : trigintegrals_chebiterationshichi(a, -1.27135418132338309016E-12, &b0, &b1, &b2, _state);
6059 0 : trigintegrals_chebiterationshichi(a, 2.74851141935315395333E-12, &b0, &b1, &b2, _state);
6060 0 : trigintegrals_chebiterationshichi(a, 2.33781843985453438400E-11, &b0, &b1, &b2, _state);
6061 0 : trigintegrals_chebiterationshichi(a, 2.71436006377612442764E-11, &b0, &b1, &b2, _state);
6062 0 : trigintegrals_chebiterationshichi(a, -2.56600180000355990529E-10, &b0, &b1, &b2, _state);
6063 0 : trigintegrals_chebiterationshichi(a, -1.61021375163803438552E-9, &b0, &b1, &b2, _state);
6064 0 : trigintegrals_chebiterationshichi(a, -4.72543064876271773512E-9, &b0, &b1, &b2, _state);
6065 0 : trigintegrals_chebiterationshichi(a, -3.00095178028681682282E-9, &b0, &b1, &b2, _state);
6066 0 : trigintegrals_chebiterationshichi(a, 7.79387474390914922337E-8, &b0, &b1, &b2, _state);
6067 0 : trigintegrals_chebiterationshichi(a, 1.06942765566401507066E-6, &b0, &b1, &b2, _state);
6068 0 : trigintegrals_chebiterationshichi(a, 1.59503164802313196374E-5, &b0, &b1, &b2, _state);
6069 0 : trigintegrals_chebiterationshichi(a, 3.49592575153777996871E-4, &b0, &b1, &b2, _state);
6070 0 : trigintegrals_chebiterationshichi(a, 1.28475387530065247392E-2, &b0, &b1, &b2, _state);
6071 0 : trigintegrals_chebiterationshichi(a, 1.03665693917934275131E0, &b0, &b1, &b2, _state);
6072 0 : c = k*0.5*(b0-b2);
6073 : }
6074 : else
6075 : {
6076 0 : if( sg!=0 )
6077 : {
6078 0 : *shi = -ae_maxrealnumber;
6079 : }
6080 : else
6081 : {
6082 0 : *shi = ae_maxrealnumber;
6083 : }
6084 0 : *chi = ae_maxrealnumber;
6085 0 : return;
6086 : }
6087 : }
6088 : }
6089 0 : if( sg!=0 )
6090 : {
6091 0 : s = -s;
6092 : }
6093 0 : *shi = s;
6094 0 : *chi = 0.57721566490153286061+ae_log(x, _state)+c;
6095 : }
6096 :
6097 :
6098 0 : static void trigintegrals_chebiterationshichi(double x,
6099 : double c,
6100 : double* b0,
6101 : double* b1,
6102 : double* b2,
6103 : ae_state *_state)
6104 : {
6105 :
6106 :
6107 0 : *b2 = *b1;
6108 0 : *b1 = *b0;
6109 0 : *b0 = x*(*b1)-(*b2)+c;
6110 0 : }
6111 :
6112 :
6113 : #endif
6114 : #if defined(AE_COMPILE_POISSONDISTR) || !defined(AE_PARTIAL_BUILD)
6115 :
6116 :
6117 : /*************************************************************************
6118 : Poisson distribution
6119 :
6120 : Returns the sum of the first k+1 terms of the Poisson
6121 : distribution:
6122 :
6123 : k j
6124 : -- -m m
6125 : > e --
6126 : -- j!
6127 : j=0
6128 :
6129 : The terms are not summed directly; instead the incomplete
6130 : gamma integral is employed, according to the relation
6131 :
6132 : y = pdtr( k, m ) = igamc( k+1, m ).
6133 :
6134 : The arguments must both be positive.
6135 : ACCURACY:
6136 :
6137 : See incomplete gamma function
6138 :
6139 : Cephes Math Library Release 2.8: June, 2000
6140 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
6141 : *************************************************************************/
6142 0 : double poissondistribution(ae_int_t k, double m, ae_state *_state)
6143 : {
6144 : double result;
6145 :
6146 :
6147 0 : ae_assert(k>=0&&ae_fp_greater(m,(double)(0)), "Domain error in PoissonDistribution", _state);
6148 0 : result = incompletegammac((double)(k+1), m, _state);
6149 0 : return result;
6150 : }
6151 :
6152 :
6153 : /*************************************************************************
6154 : Complemented Poisson distribution
6155 :
6156 : Returns the sum of the terms k+1 to infinity of the Poisson
6157 : distribution:
6158 :
6159 : inf. j
6160 : -- -m m
6161 : > e --
6162 : -- j!
6163 : j=k+1
6164 :
6165 : The terms are not summed directly; instead the incomplete
6166 : gamma integral is employed, according to the formula
6167 :
6168 : y = pdtrc( k, m ) = igam( k+1, m ).
6169 :
6170 : The arguments must both be positive.
6171 :
6172 : ACCURACY:
6173 :
6174 : See incomplete gamma function
6175 :
6176 : Cephes Math Library Release 2.8: June, 2000
6177 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
6178 : *************************************************************************/
6179 0 : double poissoncdistribution(ae_int_t k, double m, ae_state *_state)
6180 : {
6181 : double result;
6182 :
6183 :
6184 0 : ae_assert(k>=0&&ae_fp_greater(m,(double)(0)), "Domain error in PoissonDistributionC", _state);
6185 0 : result = incompletegamma((double)(k+1), m, _state);
6186 0 : return result;
6187 : }
6188 :
6189 :
6190 : /*************************************************************************
6191 : Inverse Poisson distribution
6192 :
6193 : Finds the Poisson variable x such that the integral
6194 : from 0 to x of the Poisson density is equal to the
6195 : given probability y.
6196 :
6197 : This is accomplished using the inverse gamma integral
6198 : function and the relation
6199 :
6200 : m = igami( k+1, y ).
6201 :
6202 : ACCURACY:
6203 :
6204 : See inverse incomplete gamma function
6205 :
6206 : Cephes Math Library Release 2.8: June, 2000
6207 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
6208 : *************************************************************************/
6209 0 : double invpoissondistribution(ae_int_t k, double y, ae_state *_state)
6210 : {
6211 : double result;
6212 :
6213 :
6214 0 : ae_assert((k>=0&&ae_fp_greater_eq(y,(double)(0)))&&ae_fp_less(y,(double)(1)), "Domain error in InvPoissonDistribution", _state);
6215 0 : result = invincompletegammac((double)(k+1), y, _state);
6216 0 : return result;
6217 : }
6218 :
6219 :
6220 : #endif
6221 : #if defined(AE_COMPILE_BESSEL) || !defined(AE_PARTIAL_BUILD)
6222 :
6223 :
6224 : /*************************************************************************
6225 : Bessel function of order zero
6226 :
6227 : Returns Bessel function of order zero of the argument.
6228 :
6229 : The domain is divided into the intervals [0, 5] and
6230 : (5, infinity). In the first interval the following rational
6231 : approximation is used:
6232 :
6233 :
6234 : 2 2
6235 : (w - r ) (w - r ) P (w) / Q (w)
6236 : 1 2 3 8
6237 :
6238 : 2
6239 : where w = x and the two r's are zeros of the function.
6240 :
6241 : In the second interval, the Hankel asymptotic expansion
6242 : is employed with two rational functions of degree 6/6
6243 : and 7/7.
6244 :
6245 : ACCURACY:
6246 :
6247 : Absolute error:
6248 : arithmetic domain # trials peak rms
6249 : IEEE 0, 30 60000 4.2e-16 1.1e-16
6250 :
6251 : Cephes Math Library Release 2.8: June, 2000
6252 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
6253 : *************************************************************************/
6254 0 : double besselj0(double x, ae_state *_state)
6255 : {
6256 : double xsq;
6257 : double nn;
6258 : double pzero;
6259 : double qzero;
6260 : double p1;
6261 : double q1;
6262 : double result;
6263 :
6264 :
6265 0 : if( ae_fp_less(x,(double)(0)) )
6266 : {
6267 0 : x = -x;
6268 : }
6269 0 : if( ae_fp_greater(x,8.0) )
6270 : {
6271 0 : bessel_besselasympt0(x, &pzero, &qzero, _state);
6272 0 : nn = x-ae_pi/4;
6273 0 : result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_cos(nn, _state)-qzero*ae_sin(nn, _state));
6274 0 : return result;
6275 : }
6276 0 : xsq = ae_sqr(x, _state);
6277 0 : p1 = 26857.86856980014981415848441;
6278 0 : p1 = -40504123.71833132706360663322+xsq*p1;
6279 0 : p1 = 25071582855.36881945555156435+xsq*p1;
6280 0 : p1 = -8085222034853.793871199468171+xsq*p1;
6281 0 : p1 = 1434354939140344.111664316553+xsq*p1;
6282 0 : p1 = -136762035308817138.6865416609+xsq*p1;
6283 0 : p1 = 6382059341072356562.289432465+xsq*p1;
6284 0 : p1 = -117915762910761053603.8440800+xsq*p1;
6285 0 : p1 = 493378725179413356181.6813446+xsq*p1;
6286 0 : q1 = 1.0;
6287 0 : q1 = 1363.063652328970604442810507+xsq*q1;
6288 0 : q1 = 1114636.098462985378182402543+xsq*q1;
6289 0 : q1 = 669998767.2982239671814028660+xsq*q1;
6290 0 : q1 = 312304311494.1213172572469442+xsq*q1;
6291 0 : q1 = 112775673967979.8507056031594+xsq*q1;
6292 0 : q1 = 30246356167094626.98627330784+xsq*q1;
6293 0 : q1 = 5428918384092285160.200195092+xsq*q1;
6294 0 : q1 = 493378725179413356211.3278438+xsq*q1;
6295 0 : result = p1/q1;
6296 0 : return result;
6297 : }
6298 :
6299 :
6300 : /*************************************************************************
6301 : Bessel function of order one
6302 :
6303 : Returns Bessel function of order one of the argument.
6304 :
6305 : The domain is divided into the intervals [0, 8] and
6306 : (8, infinity). In the first interval a 24 term Chebyshev
6307 : expansion is used. In the second, the asymptotic
6308 : trigonometric representation is employed using two
6309 : rational functions of degree 5/5.
6310 :
6311 : ACCURACY:
6312 :
6313 : Absolute error:
6314 : arithmetic domain # trials peak rms
6315 : IEEE 0, 30 30000 2.6e-16 1.1e-16
6316 :
6317 : Cephes Math Library Release 2.8: June, 2000
6318 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
6319 : *************************************************************************/
6320 0 : double besselj1(double x, ae_state *_state)
6321 : {
6322 : double s;
6323 : double xsq;
6324 : double nn;
6325 : double pzero;
6326 : double qzero;
6327 : double p1;
6328 : double q1;
6329 : double result;
6330 :
6331 :
6332 0 : s = (double)(ae_sign(x, _state));
6333 0 : if( ae_fp_less(x,(double)(0)) )
6334 : {
6335 0 : x = -x;
6336 : }
6337 0 : if( ae_fp_greater(x,8.0) )
6338 : {
6339 0 : bessel_besselasympt1(x, &pzero, &qzero, _state);
6340 0 : nn = x-3*ae_pi/4;
6341 0 : result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_cos(nn, _state)-qzero*ae_sin(nn, _state));
6342 0 : if( ae_fp_less(s,(double)(0)) )
6343 : {
6344 0 : result = -result;
6345 : }
6346 0 : return result;
6347 : }
6348 0 : xsq = ae_sqr(x, _state);
6349 0 : p1 = 2701.122710892323414856790990;
6350 0 : p1 = -4695753.530642995859767162166+xsq*p1;
6351 0 : p1 = 3413234182.301700539091292655+xsq*p1;
6352 0 : p1 = -1322983480332.126453125473247+xsq*p1;
6353 0 : p1 = 290879526383477.5409737601689+xsq*p1;
6354 0 : p1 = -35888175699101060.50743641413+xsq*p1;
6355 0 : p1 = 2316433580634002297.931815435+xsq*p1;
6356 0 : p1 = -66721065689249162980.20941484+xsq*p1;
6357 0 : p1 = 581199354001606143928.050809+xsq*p1;
6358 0 : q1 = 1.0;
6359 0 : q1 = 1606.931573481487801970916749+xsq*q1;
6360 0 : q1 = 1501793.594998585505921097578+xsq*q1;
6361 0 : q1 = 1013863514.358673989967045588+xsq*q1;
6362 0 : q1 = 524371026216.7649715406728642+xsq*q1;
6363 0 : q1 = 208166122130760.7351240184229+xsq*q1;
6364 0 : q1 = 60920613989175217.46105196863+xsq*q1;
6365 0 : q1 = 11857707121903209998.37113348+xsq*q1;
6366 0 : q1 = 1162398708003212287858.529400+xsq*q1;
6367 0 : result = s*x*p1/q1;
6368 0 : return result;
6369 : }
6370 :
6371 :
6372 : /*************************************************************************
6373 : Bessel function of integer order
6374 :
6375 : Returns Bessel function of order n, where n is a
6376 : (possibly negative) integer.
6377 :
6378 : The ratio of jn(x) to j0(x) is computed by backward
6379 : recurrence. First the ratio jn/jn-1 is found by a
6380 : continued fraction expansion. Then the recurrence
6381 : relating successive orders is applied until j0 or j1 is
6382 : reached.
6383 :
6384 : If n = 0 or 1 the routine for j0 or j1 is called
6385 : directly.
6386 :
6387 : ACCURACY:
6388 :
6389 : Absolute error:
6390 : arithmetic range # trials peak rms
6391 : IEEE 0, 30 5000 4.4e-16 7.9e-17
6392 :
6393 :
6394 : Not suitable for large n or x. Use jv() (fractional order) instead.
6395 :
6396 : Cephes Math Library Release 2.8: June, 2000
6397 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
6398 : *************************************************************************/
6399 0 : double besseljn(ae_int_t n, double x, ae_state *_state)
6400 : {
6401 : double pkm2;
6402 : double pkm1;
6403 : double pk;
6404 : double xk;
6405 : double r;
6406 : double ans;
6407 : ae_int_t k;
6408 : ae_int_t sg;
6409 : double result;
6410 :
6411 :
6412 0 : if( n<0 )
6413 : {
6414 0 : n = -n;
6415 0 : if( n%2==0 )
6416 : {
6417 0 : sg = 1;
6418 : }
6419 : else
6420 : {
6421 0 : sg = -1;
6422 : }
6423 : }
6424 : else
6425 : {
6426 0 : sg = 1;
6427 : }
6428 0 : if( ae_fp_less(x,(double)(0)) )
6429 : {
6430 0 : if( n%2!=0 )
6431 : {
6432 0 : sg = -sg;
6433 : }
6434 0 : x = -x;
6435 : }
6436 0 : if( n==0 )
6437 : {
6438 0 : result = sg*besselj0(x, _state);
6439 0 : return result;
6440 : }
6441 0 : if( n==1 )
6442 : {
6443 0 : result = sg*besselj1(x, _state);
6444 0 : return result;
6445 : }
6446 0 : if( n==2 )
6447 : {
6448 0 : if( ae_fp_eq(x,(double)(0)) )
6449 : {
6450 0 : result = (double)(0);
6451 : }
6452 : else
6453 : {
6454 0 : result = sg*(2.0*besselj1(x, _state)/x-besselj0(x, _state));
6455 : }
6456 0 : return result;
6457 : }
6458 0 : if( ae_fp_less(x,ae_machineepsilon) )
6459 : {
6460 0 : result = (double)(0);
6461 0 : return result;
6462 : }
6463 0 : k = 53;
6464 0 : pk = (double)(2*(n+k));
6465 0 : ans = pk;
6466 0 : xk = x*x;
6467 : do
6468 : {
6469 0 : pk = pk-2.0;
6470 0 : ans = pk-xk/ans;
6471 0 : k = k-1;
6472 : }
6473 0 : while(k!=0);
6474 0 : ans = x/ans;
6475 0 : pk = 1.0;
6476 0 : pkm1 = 1.0/ans;
6477 0 : k = n-1;
6478 0 : r = (double)(2*k);
6479 : do
6480 : {
6481 0 : pkm2 = (pkm1*r-pk*x)/x;
6482 0 : pk = pkm1;
6483 0 : pkm1 = pkm2;
6484 0 : r = r-2.0;
6485 0 : k = k-1;
6486 : }
6487 0 : while(k!=0);
6488 0 : if( ae_fp_greater(ae_fabs(pk, _state),ae_fabs(pkm1, _state)) )
6489 : {
6490 0 : ans = besselj1(x, _state)/pk;
6491 : }
6492 : else
6493 : {
6494 0 : ans = besselj0(x, _state)/pkm1;
6495 : }
6496 0 : result = sg*ans;
6497 0 : return result;
6498 : }
6499 :
6500 :
6501 : /*************************************************************************
6502 : Bessel function of the second kind, order zero
6503 :
6504 : Returns Bessel function of the second kind, of order
6505 : zero, of the argument.
6506 :
6507 : The domain is divided into the intervals [0, 5] and
6508 : (5, infinity). In the first interval a rational approximation
6509 : R(x) is employed to compute
6510 : y0(x) = R(x) + 2 * log(x) * j0(x) / PI.
6511 : Thus a call to j0() is required.
6512 :
6513 : In the second interval, the Hankel asymptotic expansion
6514 : is employed with two rational functions of degree 6/6
6515 : and 7/7.
6516 :
6517 :
6518 :
6519 : ACCURACY:
6520 :
6521 : Absolute error, when y0(x) < 1; else relative error:
6522 :
6523 : arithmetic domain # trials peak rms
6524 : IEEE 0, 30 30000 1.3e-15 1.6e-16
6525 :
6526 : Cephes Math Library Release 2.8: June, 2000
6527 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
6528 : *************************************************************************/
6529 0 : double bessely0(double x, ae_state *_state)
6530 : {
6531 : double nn;
6532 : double xsq;
6533 : double pzero;
6534 : double qzero;
6535 : double p4;
6536 : double q4;
6537 : double result;
6538 :
6539 :
6540 0 : if( ae_fp_greater(x,8.0) )
6541 : {
6542 0 : bessel_besselasympt0(x, &pzero, &qzero, _state);
6543 0 : nn = x-ae_pi/4;
6544 0 : result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_sin(nn, _state)+qzero*ae_cos(nn, _state));
6545 0 : return result;
6546 : }
6547 0 : xsq = ae_sqr(x, _state);
6548 0 : p4 = -41370.35497933148554125235152;
6549 0 : p4 = 59152134.65686889654273830069+xsq*p4;
6550 0 : p4 = -34363712229.79040378171030138+xsq*p4;
6551 0 : p4 = 10255208596863.94284509167421+xsq*p4;
6552 0 : p4 = -1648605817185729.473122082537+xsq*p4;
6553 0 : p4 = 137562431639934407.8571335453+xsq*p4;
6554 0 : p4 = -5247065581112764941.297350814+xsq*p4;
6555 0 : p4 = 65874732757195549259.99402049+xsq*p4;
6556 0 : p4 = -27502866786291095837.01933175+xsq*p4;
6557 0 : q4 = 1.0;
6558 0 : q4 = 1282.452772478993804176329391+xsq*q4;
6559 0 : q4 = 1001702.641288906265666651753+xsq*q4;
6560 0 : q4 = 579512264.0700729537480087915+xsq*q4;
6561 0 : q4 = 261306575504.1081249568482092+xsq*q4;
6562 0 : q4 = 91620380340751.85262489147968+xsq*q4;
6563 0 : q4 = 23928830434997818.57439356652+xsq*q4;
6564 0 : q4 = 4192417043410839973.904769661+xsq*q4;
6565 0 : q4 = 372645883898616588198.9980+xsq*q4;
6566 0 : result = p4/q4+2/ae_pi*besselj0(x, _state)*ae_log(x, _state);
6567 0 : return result;
6568 : }
6569 :
6570 :
6571 : /*************************************************************************
6572 : Bessel function of second kind of order one
6573 :
6574 : Returns Bessel function of the second kind of order one
6575 : of the argument.
6576 :
6577 : The domain is divided into the intervals [0, 8] and
6578 : (8, infinity). In the first interval a 25 term Chebyshev
6579 : expansion is used, and a call to j1() is required.
6580 : In the second, the asymptotic trigonometric representation
6581 : is employed using two rational functions of degree 5/5.
6582 :
6583 : ACCURACY:
6584 :
6585 : Absolute error:
6586 : arithmetic domain # trials peak rms
6587 : IEEE 0, 30 30000 1.0e-15 1.3e-16
6588 :
6589 : Cephes Math Library Release 2.8: June, 2000
6590 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
6591 : *************************************************************************/
6592 0 : double bessely1(double x, ae_state *_state)
6593 : {
6594 : double nn;
6595 : double xsq;
6596 : double pzero;
6597 : double qzero;
6598 : double p4;
6599 : double q4;
6600 : double result;
6601 :
6602 :
6603 0 : if( ae_fp_greater(x,8.0) )
6604 : {
6605 0 : bessel_besselasympt1(x, &pzero, &qzero, _state);
6606 0 : nn = x-3*ae_pi/4;
6607 0 : result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_sin(nn, _state)+qzero*ae_cos(nn, _state));
6608 0 : return result;
6609 : }
6610 0 : xsq = ae_sqr(x, _state);
6611 0 : p4 = -2108847.540133123652824139923;
6612 0 : p4 = 3639488548.124002058278999428+xsq*p4;
6613 0 : p4 = -2580681702194.450950541426399+xsq*p4;
6614 0 : p4 = 956993023992168.3481121552788+xsq*p4;
6615 0 : p4 = -196588746272214065.8820322248+xsq*p4;
6616 0 : p4 = 21931073399177975921.11427556+xsq*p4;
6617 0 : p4 = -1212297555414509577913.561535+xsq*p4;
6618 0 : p4 = 26554738314348543268942.48968+xsq*p4;
6619 0 : p4 = -99637534243069222259967.44354+xsq*p4;
6620 0 : q4 = 1.0;
6621 0 : q4 = 1612.361029677000859332072312+xsq*q4;
6622 0 : q4 = 1563282.754899580604737366452+xsq*q4;
6623 0 : q4 = 1128686837.169442121732366891+xsq*q4;
6624 0 : q4 = 646534088126.5275571961681500+xsq*q4;
6625 0 : q4 = 297663212564727.6729292742282+xsq*q4;
6626 0 : q4 = 108225825940881955.2553850180+xsq*q4;
6627 0 : q4 = 29549879358971486742.90758119+xsq*q4;
6628 0 : q4 = 5435310377188854170800.653097+xsq*q4;
6629 0 : q4 = 508206736694124324531442.4152+xsq*q4;
6630 0 : result = x*p4/q4+2/ae_pi*(besselj1(x, _state)*ae_log(x, _state)-1/x);
6631 0 : return result;
6632 : }
6633 :
6634 :
6635 : /*************************************************************************
6636 : Bessel function of second kind of integer order
6637 :
6638 : Returns Bessel function of order n, where n is a
6639 : (possibly negative) integer.
6640 :
6641 : The function is evaluated by forward recurrence on
6642 : n, starting with values computed by the routines
6643 : y0() and y1().
6644 :
6645 : If n = 0 or 1 the routine for y0 or y1 is called
6646 : directly.
6647 :
6648 : ACCURACY:
6649 : Absolute error, except relative
6650 : when y > 1:
6651 : arithmetic domain # trials peak rms
6652 : IEEE 0, 30 30000 3.4e-15 4.3e-16
6653 :
6654 : Cephes Math Library Release 2.8: June, 2000
6655 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
6656 : *************************************************************************/
6657 0 : double besselyn(ae_int_t n, double x, ae_state *_state)
6658 : {
6659 : ae_int_t i;
6660 : double a;
6661 : double b;
6662 : double tmp;
6663 : double s;
6664 : double result;
6665 :
6666 :
6667 0 : s = (double)(1);
6668 0 : if( n<0 )
6669 : {
6670 0 : n = -n;
6671 0 : if( n%2!=0 )
6672 : {
6673 0 : s = (double)(-1);
6674 : }
6675 : }
6676 0 : if( n==0 )
6677 : {
6678 0 : result = bessely0(x, _state);
6679 0 : return result;
6680 : }
6681 0 : if( n==1 )
6682 : {
6683 0 : result = s*bessely1(x, _state);
6684 0 : return result;
6685 : }
6686 0 : a = bessely0(x, _state);
6687 0 : b = bessely1(x, _state);
6688 0 : for(i=1; i<=n-1; i++)
6689 : {
6690 0 : tmp = b;
6691 0 : b = 2*i/x*b-a;
6692 0 : a = tmp;
6693 : }
6694 0 : result = s*b;
6695 0 : return result;
6696 : }
6697 :
6698 :
6699 : /*************************************************************************
6700 : Modified Bessel function of order zero
6701 :
6702 : Returns modified Bessel function of order zero of the
6703 : argument.
6704 :
6705 : The function is defined as i0(x) = j0( ix ).
6706 :
6707 : The range is partitioned into the two intervals [0,8] and
6708 : (8, infinity). Chebyshev polynomial expansions are employed
6709 : in each interval.
6710 :
6711 : ACCURACY:
6712 :
6713 : Relative error:
6714 : arithmetic domain # trials peak rms
6715 : IEEE 0,30 30000 5.8e-16 1.4e-16
6716 :
6717 : Cephes Math Library Release 2.8: June, 2000
6718 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
6719 : *************************************************************************/
6720 0 : double besseli0(double x, ae_state *_state)
6721 : {
6722 : double y;
6723 : double v;
6724 : double z;
6725 : double b0;
6726 : double b1;
6727 : double b2;
6728 : double result;
6729 :
6730 :
6731 0 : if( ae_fp_less(x,(double)(0)) )
6732 : {
6733 0 : x = -x;
6734 : }
6735 0 : if( ae_fp_less_eq(x,8.0) )
6736 : {
6737 0 : y = x/2.0-2.0;
6738 0 : bessel_besselmfirstcheb(-4.41534164647933937950E-18, &b0, &b1, &b2, _state);
6739 0 : bessel_besselmnextcheb(y, 3.33079451882223809783E-17, &b0, &b1, &b2, _state);
6740 0 : bessel_besselmnextcheb(y, -2.43127984654795469359E-16, &b0, &b1, &b2, _state);
6741 0 : bessel_besselmnextcheb(y, 1.71539128555513303061E-15, &b0, &b1, &b2, _state);
6742 0 : bessel_besselmnextcheb(y, -1.16853328779934516808E-14, &b0, &b1, &b2, _state);
6743 0 : bessel_besselmnextcheb(y, 7.67618549860493561688E-14, &b0, &b1, &b2, _state);
6744 0 : bessel_besselmnextcheb(y, -4.85644678311192946090E-13, &b0, &b1, &b2, _state);
6745 0 : bessel_besselmnextcheb(y, 2.95505266312963983461E-12, &b0, &b1, &b2, _state);
6746 0 : bessel_besselmnextcheb(y, -1.72682629144155570723E-11, &b0, &b1, &b2, _state);
6747 0 : bessel_besselmnextcheb(y, 9.67580903537323691224E-11, &b0, &b1, &b2, _state);
6748 0 : bessel_besselmnextcheb(y, -5.18979560163526290666E-10, &b0, &b1, &b2, _state);
6749 0 : bessel_besselmnextcheb(y, 2.65982372468238665035E-9, &b0, &b1, &b2, _state);
6750 0 : bessel_besselmnextcheb(y, -1.30002500998624804212E-8, &b0, &b1, &b2, _state);
6751 0 : bessel_besselmnextcheb(y, 6.04699502254191894932E-8, &b0, &b1, &b2, _state);
6752 0 : bessel_besselmnextcheb(y, -2.67079385394061173391E-7, &b0, &b1, &b2, _state);
6753 0 : bessel_besselmnextcheb(y, 1.11738753912010371815E-6, &b0, &b1, &b2, _state);
6754 0 : bessel_besselmnextcheb(y, -4.41673835845875056359E-6, &b0, &b1, &b2, _state);
6755 0 : bessel_besselmnextcheb(y, 1.64484480707288970893E-5, &b0, &b1, &b2, _state);
6756 0 : bessel_besselmnextcheb(y, -5.75419501008210370398E-5, &b0, &b1, &b2, _state);
6757 0 : bessel_besselmnextcheb(y, 1.88502885095841655729E-4, &b0, &b1, &b2, _state);
6758 0 : bessel_besselmnextcheb(y, -5.76375574538582365885E-4, &b0, &b1, &b2, _state);
6759 0 : bessel_besselmnextcheb(y, 1.63947561694133579842E-3, &b0, &b1, &b2, _state);
6760 0 : bessel_besselmnextcheb(y, -4.32430999505057594430E-3, &b0, &b1, &b2, _state);
6761 0 : bessel_besselmnextcheb(y, 1.05464603945949983183E-2, &b0, &b1, &b2, _state);
6762 0 : bessel_besselmnextcheb(y, -2.37374148058994688156E-2, &b0, &b1, &b2, _state);
6763 0 : bessel_besselmnextcheb(y, 4.93052842396707084878E-2, &b0, &b1, &b2, _state);
6764 0 : bessel_besselmnextcheb(y, -9.49010970480476444210E-2, &b0, &b1, &b2, _state);
6765 0 : bessel_besselmnextcheb(y, 1.71620901522208775349E-1, &b0, &b1, &b2, _state);
6766 0 : bessel_besselmnextcheb(y, -3.04682672343198398683E-1, &b0, &b1, &b2, _state);
6767 0 : bessel_besselmnextcheb(y, 6.76795274409476084995E-1, &b0, &b1, &b2, _state);
6768 0 : v = 0.5*(b0-b2);
6769 0 : result = ae_exp(x, _state)*v;
6770 0 : return result;
6771 : }
6772 0 : z = 32.0/x-2.0;
6773 0 : bessel_besselmfirstcheb(-7.23318048787475395456E-18, &b0, &b1, &b2, _state);
6774 0 : bessel_besselmnextcheb(z, -4.83050448594418207126E-18, &b0, &b1, &b2, _state);
6775 0 : bessel_besselmnextcheb(z, 4.46562142029675999901E-17, &b0, &b1, &b2, _state);
6776 0 : bessel_besselmnextcheb(z, 3.46122286769746109310E-17, &b0, &b1, &b2, _state);
6777 0 : bessel_besselmnextcheb(z, -2.82762398051658348494E-16, &b0, &b1, &b2, _state);
6778 0 : bessel_besselmnextcheb(z, -3.42548561967721913462E-16, &b0, &b1, &b2, _state);
6779 0 : bessel_besselmnextcheb(z, 1.77256013305652638360E-15, &b0, &b1, &b2, _state);
6780 0 : bessel_besselmnextcheb(z, 3.81168066935262242075E-15, &b0, &b1, &b2, _state);
6781 0 : bessel_besselmnextcheb(z, -9.55484669882830764870E-15, &b0, &b1, &b2, _state);
6782 0 : bessel_besselmnextcheb(z, -4.15056934728722208663E-14, &b0, &b1, &b2, _state);
6783 0 : bessel_besselmnextcheb(z, 1.54008621752140982691E-14, &b0, &b1, &b2, _state);
6784 0 : bessel_besselmnextcheb(z, 3.85277838274214270114E-13, &b0, &b1, &b2, _state);
6785 0 : bessel_besselmnextcheb(z, 7.18012445138366623367E-13, &b0, &b1, &b2, _state);
6786 0 : bessel_besselmnextcheb(z, -1.79417853150680611778E-12, &b0, &b1, &b2, _state);
6787 0 : bessel_besselmnextcheb(z, -1.32158118404477131188E-11, &b0, &b1, &b2, _state);
6788 0 : bessel_besselmnextcheb(z, -3.14991652796324136454E-11, &b0, &b1, &b2, _state);
6789 0 : bessel_besselmnextcheb(z, 1.18891471078464383424E-11, &b0, &b1, &b2, _state);
6790 0 : bessel_besselmnextcheb(z, 4.94060238822496958910E-10, &b0, &b1, &b2, _state);
6791 0 : bessel_besselmnextcheb(z, 3.39623202570838634515E-9, &b0, &b1, &b2, _state);
6792 0 : bessel_besselmnextcheb(z, 2.26666899049817806459E-8, &b0, &b1, &b2, _state);
6793 0 : bessel_besselmnextcheb(z, 2.04891858946906374183E-7, &b0, &b1, &b2, _state);
6794 0 : bessel_besselmnextcheb(z, 2.89137052083475648297E-6, &b0, &b1, &b2, _state);
6795 0 : bessel_besselmnextcheb(z, 6.88975834691682398426E-5, &b0, &b1, &b2, _state);
6796 0 : bessel_besselmnextcheb(z, 3.36911647825569408990E-3, &b0, &b1, &b2, _state);
6797 0 : bessel_besselmnextcheb(z, 8.04490411014108831608E-1, &b0, &b1, &b2, _state);
6798 0 : v = 0.5*(b0-b2);
6799 0 : result = ae_exp(x, _state)*v/ae_sqrt(x, _state);
6800 0 : return result;
6801 : }
6802 :
6803 :
6804 : /*************************************************************************
6805 : Modified Bessel function of order one
6806 :
6807 : Returns modified Bessel function of order one of the
6808 : argument.
6809 :
6810 : The function is defined as i1(x) = -i j1( ix ).
6811 :
6812 : The range is partitioned into the two intervals [0,8] and
6813 : (8, infinity). Chebyshev polynomial expansions are employed
6814 : in each interval.
6815 :
6816 : ACCURACY:
6817 :
6818 : Relative error:
6819 : arithmetic domain # trials peak rms
6820 : IEEE 0, 30 30000 1.9e-15 2.1e-16
6821 :
6822 : Cephes Math Library Release 2.8: June, 2000
6823 : Copyright 1985, 1987, 2000 by Stephen L. Moshier
6824 : *************************************************************************/
6825 0 : double besseli1(double x, ae_state *_state)
6826 : {
6827 : double y;
6828 : double z;
6829 : double v;
6830 : double b0;
6831 : double b1;
6832 : double b2;
6833 : double result;
6834 :
6835 :
6836 0 : z = ae_fabs(x, _state);
6837 0 : if( ae_fp_less_eq(z,8.0) )
6838 : {
6839 0 : y = z/2.0-2.0;
6840 0 : bessel_besselm1firstcheb(2.77791411276104639959E-18, &b0, &b1, &b2, _state);
6841 0 : bessel_besselm1nextcheb(y, -2.11142121435816608115E-17, &b0, &b1, &b2, _state);
6842 0 : bessel_besselm1nextcheb(y, 1.55363195773620046921E-16, &b0, &b1, &b2, _state);
6843 0 : bessel_besselm1nextcheb(y, -1.10559694773538630805E-15, &b0, &b1, &b2, _state);
6844 0 : bessel_besselm1nextcheb(y, 7.60068429473540693410E-15, &b0, &b1, &b2, _state);
6845 0 : bessel_besselm1nextcheb(y, -5.04218550472791168711E-14, &b0, &b1, &b2, _state);
6846 0 : bessel_besselm1nextcheb(y, 3.22379336594557470981E-13, &b0, &b1, &b2, _state);
6847 0 : bessel_besselm1nextcheb(y, -1.98397439776494371520E-12, &b0, &b1, &b2, _state);
6848 0 : bessel_besselm1nextcheb(y, 1.17361862988909016308E-11, &b0, &b1, &b2, _state);
6849 0 : bessel_besselm1nextcheb(y, -6.66348972350202774223E-11, &b0, &b1, &b2, _state);
6850 0 : bessel_besselm1nextcheb(y, 3.62559028155211703701E-10, &b0, &b1, &b2, _state);
6851 0 : bessel_besselm1nextcheb(y, -1.88724975172282928790E-9, &b0, &b1, &b2, _state);
6852 0 : bessel_besselm1nextcheb(y, 9.38153738649577178388E-9, &b0, &b1, &b2, _state);
6853 0 : bessel_besselm1nextcheb(y, -4.44505912879632808065E-8, &b0, &b1, &b2, _state);
6854 0 : bessel_besselm1nextcheb(y, 2.00329475355213526229E-7, &b0, &b1, &b2, _state);
6855 0 : bessel_besselm1nextcheb(y, -8.56872026469545474066E-7, &b0, &b1, &b2, _state);
6856 0 : bessel_besselm1nextcheb(y, 3.47025130813767847674E-6, &b0, &b1, &b2, _state);
6857 0 : bessel_besselm1nextcheb(y, -1.32731636560394358279E-5, &b0, &b1, &b2, _state);
6858 0 : bessel_besselm1nextcheb(y, 4.78156510755005422638E-5, &b0, &b1, &b2, _state);
6859 0 : bessel_besselm1nextcheb(y, -1.61760815825896745588E-4, &b0, &b1, &b2, _state);
6860 0 : bessel_besselm1nextcheb(y, 5.12285956168575772895E-4, &b0, &b1, &b2, _state);
6861 0 : bessel_besselm1nextcheb(y, -1.51357245063125314899E-3, &b0, &b1, &b2, _state);
6862 0 : bessel_besselm1nextcheb(y, 4.15642294431288815669E-3, &b0, &b1, &b2, _state);
6863 0 : bessel_besselm1nextcheb(y, -1.05640848946261981558E-2, &b0, &b1, &b2, _state);
6864 0 : bessel_besselm1nextcheb(y, 2.47264490306265168283E-2, &b0, &b1, &b2, _state);
6865 0 : bessel_besselm1nextcheb(y, -5.29459812080949914269E-2, &b0, &b1, &b2, _state);
6866 0 : bessel_besselm1nextcheb(y, 1.02643658689847095384E-1, &b0, &b1, &b2, _state);
6867 0 : bessel_besselm1nextcheb(y, -1.76416518357834055153E-1, &b0, &b1, &b2, _state);
6868 0 : bessel_besselm1nextcheb(y, 2.52587186443633654823E-1, &b0, &b1, &b2, _state);
6869 0 : v = 0.5*(b0-b2);
6870 0 : z = v*z*ae_exp(z, _state);
6871 : }
6872 : else
6873 : {
6874 0 : y = 32.0/z-2.0;
6875 0 : bessel_besselm1firstcheb(7.51729631084210481353E-18, &b0, &b1, &b2, _state);
6876 0 : bessel_besselm1nextcheb(y, 4.41434832307170791151E-18, &b0, &b1, &b2, _state);
6877 0 : bessel_besselm1nextcheb(y, -4.65030536848935832153E-17, &b0, &b1, &b2, _state);
6878 0 : bessel_besselm1nextcheb(y, -3.20952592199342395980E-17, &b0, &b1, &b2, _state);
6879 0 : bessel_besselm1nextcheb(y, 2.96262899764595013876E-16, &b0, &b1, &b2, _state);
6880 0 : bessel_besselm1nextcheb(y, 3.30820231092092828324E-16, &b0, &b1, &b2, _state);
6881 0 : bessel_besselm1nextcheb(y, -1.88035477551078244854E-15, &b0, &b1, &b2, _state);
6882 0 : bessel_besselm1nextcheb(y, -3.81440307243700780478E-15, &b0, &b1, &b2, _state);
6883 0 : bessel_besselm1nextcheb(y, 1.04202769841288027642E-14, &b0, &b1, &b2, _state);
6884 0 : bessel_besselm1nextcheb(y, 4.27244001671195135429E-14, &b0, &b1, &b2, _state);
6885 0 : bessel_besselm1nextcheb(y, -2.10154184277266431302E-14, &b0, &b1, &b2, _state);
6886 0 : bessel_besselm1nextcheb(y, -4.08355111109219731823E-13, &b0, &b1, &b2, _state);
6887 0 : bessel_besselm1nextcheb(y, -7.19855177624590851209E-13, &b0, &b1, &b2, _state);
6888 0 : bessel_besselm1nextcheb(y, 2.03562854414708950722E-12, &b0, &b1, &b2, _state);
6889 0 : bessel_besselm1nextcheb(y, 1.41258074366137813316E-11, &b0, &b1, &b2, _state);
6890 0 : bessel_besselm1nextcheb(y, 3.25260358301548823856E-11, &b0, &b1, &b2, _state);
6891 0 : bessel_besselm1nextcheb(y, -1.89749581235054123450E-11, &b0, &b1, &b2, _state);
6892 0 : bessel_besselm1nextcheb(y, -5.58974346219658380687E-10, &b0, &b1, &b2, _state);
6893 0 : bessel_besselm1nextcheb(y, -3.83538038596423702205E-9, &b0, &b1, &b2, _state);
6894 0 : bessel_besselm1nextcheb(y, -2.63146884688951950684E-8, &b0, &b1, &b2, _state);
6895 0 : bessel_besselm1nextcheb(y, -2.51223623787020892529E-7, &b0, &b1, &b2, _state);
6896 0 : bessel_besselm1nextcheb(y, -3.88256480887769039346E-6, &b0, &b1, &b2, _state);
6897 0 : bessel_besselm1nextcheb(y, -1.10588938762623716291E-4, &b0, &b1, &b2, _state);
6898 0 : bessel_besselm1nextcheb(y, -9.76109749136146840777E-3, &b0, &b1, &b2, _state);
6899 0 : bessel_besselm1nextcheb(y, 7.78576235018280120474E-1, &b0, &b1, &b2, _state);
6900 0 : v = 0.5*(b0-b2);
6901 0 : z = v*ae_exp(z, _state)/ae_sqrt(z, _state);
6902 : }
6903 0 : if( ae_fp_less(x,(double)(0)) )
6904 : {
6905 0 : z = -z;
6906 : }
6907 0 : result = z;
6908 0 : return result;
6909 : }
6910 :
6911 :
6912 : /*************************************************************************
6913 : Modified Bessel function, second kind, order zero
6914 :
6915 : Returns modified Bessel function of the second kind
6916 : of order zero of the argument.
6917 :
6918 : The range is partitioned into the two intervals [0,8] and
6919 : (8, infinity). Chebyshev polynomial expansions are employed
6920 : in each interval.
6921 :
6922 : ACCURACY:
6923 :
6924 : Tested at 2000 random points between 0 and 8. Peak absolute
6925 : error (relative when K0 > 1) was 1.46e-14; rms, 4.26e-15.
6926 : Relative error:
6927 : arithmetic domain # trials peak rms
6928 : IEEE 0, 30 30000 1.2e-15 1.6e-16
6929 :
6930 : Cephes Math Library Release 2.8: June, 2000
6931 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
6932 : *************************************************************************/
6933 0 : double besselk0(double x, ae_state *_state)
6934 : {
6935 : double y;
6936 : double z;
6937 : double v;
6938 : double b0;
6939 : double b1;
6940 : double b2;
6941 : double result;
6942 :
6943 :
6944 0 : ae_assert(ae_fp_greater(x,(double)(0)), "Domain error in BesselK0: x<=0", _state);
6945 0 : if( ae_fp_less_eq(x,(double)(2)) )
6946 : {
6947 0 : y = x*x-2.0;
6948 0 : bessel_besselmfirstcheb(1.37446543561352307156E-16, &b0, &b1, &b2, _state);
6949 0 : bessel_besselmnextcheb(y, 4.25981614279661018399E-14, &b0, &b1, &b2, _state);
6950 0 : bessel_besselmnextcheb(y, 1.03496952576338420167E-11, &b0, &b1, &b2, _state);
6951 0 : bessel_besselmnextcheb(y, 1.90451637722020886025E-9, &b0, &b1, &b2, _state);
6952 0 : bessel_besselmnextcheb(y, 2.53479107902614945675E-7, &b0, &b1, &b2, _state);
6953 0 : bessel_besselmnextcheb(y, 2.28621210311945178607E-5, &b0, &b1, &b2, _state);
6954 0 : bessel_besselmnextcheb(y, 1.26461541144692592338E-3, &b0, &b1, &b2, _state);
6955 0 : bessel_besselmnextcheb(y, 3.59799365153615016266E-2, &b0, &b1, &b2, _state);
6956 0 : bessel_besselmnextcheb(y, 3.44289899924628486886E-1, &b0, &b1, &b2, _state);
6957 0 : bessel_besselmnextcheb(y, -5.35327393233902768720E-1, &b0, &b1, &b2, _state);
6958 0 : v = 0.5*(b0-b2);
6959 0 : v = v-ae_log(0.5*x, _state)*besseli0(x, _state);
6960 : }
6961 : else
6962 : {
6963 0 : z = 8.0/x-2.0;
6964 0 : bessel_besselmfirstcheb(5.30043377268626276149E-18, &b0, &b1, &b2, _state);
6965 0 : bessel_besselmnextcheb(z, -1.64758043015242134646E-17, &b0, &b1, &b2, _state);
6966 0 : bessel_besselmnextcheb(z, 5.21039150503902756861E-17, &b0, &b1, &b2, _state);
6967 0 : bessel_besselmnextcheb(z, -1.67823109680541210385E-16, &b0, &b1, &b2, _state);
6968 0 : bessel_besselmnextcheb(z, 5.51205597852431940784E-16, &b0, &b1, &b2, _state);
6969 0 : bessel_besselmnextcheb(z, -1.84859337734377901440E-15, &b0, &b1, &b2, _state);
6970 0 : bessel_besselmnextcheb(z, 6.34007647740507060557E-15, &b0, &b1, &b2, _state);
6971 0 : bessel_besselmnextcheb(z, -2.22751332699166985548E-14, &b0, &b1, &b2, _state);
6972 0 : bessel_besselmnextcheb(z, 8.03289077536357521100E-14, &b0, &b1, &b2, _state);
6973 0 : bessel_besselmnextcheb(z, -2.98009692317273043925E-13, &b0, &b1, &b2, _state);
6974 0 : bessel_besselmnextcheb(z, 1.14034058820847496303E-12, &b0, &b1, &b2, _state);
6975 0 : bessel_besselmnextcheb(z, -4.51459788337394416547E-12, &b0, &b1, &b2, _state);
6976 0 : bessel_besselmnextcheb(z, 1.85594911495471785253E-11, &b0, &b1, &b2, _state);
6977 0 : bessel_besselmnextcheb(z, -7.95748924447710747776E-11, &b0, &b1, &b2, _state);
6978 0 : bessel_besselmnextcheb(z, 3.57739728140030116597E-10, &b0, &b1, &b2, _state);
6979 0 : bessel_besselmnextcheb(z, -1.69753450938905987466E-9, &b0, &b1, &b2, _state);
6980 0 : bessel_besselmnextcheb(z, 8.57403401741422608519E-9, &b0, &b1, &b2, _state);
6981 0 : bessel_besselmnextcheb(z, -4.66048989768794782956E-8, &b0, &b1, &b2, _state);
6982 0 : bessel_besselmnextcheb(z, 2.76681363944501510342E-7, &b0, &b1, &b2, _state);
6983 0 : bessel_besselmnextcheb(z, -1.83175552271911948767E-6, &b0, &b1, &b2, _state);
6984 0 : bessel_besselmnextcheb(z, 1.39498137188764993662E-5, &b0, &b1, &b2, _state);
6985 0 : bessel_besselmnextcheb(z, -1.28495495816278026384E-4, &b0, &b1, &b2, _state);
6986 0 : bessel_besselmnextcheb(z, 1.56988388573005337491E-3, &b0, &b1, &b2, _state);
6987 0 : bessel_besselmnextcheb(z, -3.14481013119645005427E-2, &b0, &b1, &b2, _state);
6988 0 : bessel_besselmnextcheb(z, 2.44030308206595545468E0, &b0, &b1, &b2, _state);
6989 0 : v = 0.5*(b0-b2);
6990 0 : v = v*ae_exp(-x, _state)/ae_sqrt(x, _state);
6991 : }
6992 0 : result = v;
6993 0 : return result;
6994 : }
6995 :
6996 :
6997 : /*************************************************************************
6998 : Modified Bessel function, second kind, order one
6999 :
7000 : Computes the modified Bessel function of the second kind
7001 : of order one of the argument.
7002 :
7003 : The range is partitioned into the two intervals [0,2] and
7004 : (2, infinity). Chebyshev polynomial expansions are employed
7005 : in each interval.
7006 :
7007 : ACCURACY:
7008 :
7009 : Relative error:
7010 : arithmetic domain # trials peak rms
7011 : IEEE 0, 30 30000 1.2e-15 1.6e-16
7012 :
7013 : Cephes Math Library Release 2.8: June, 2000
7014 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
7015 : *************************************************************************/
7016 0 : double besselk1(double x, ae_state *_state)
7017 : {
7018 : double y;
7019 : double z;
7020 : double v;
7021 : double b0;
7022 : double b1;
7023 : double b2;
7024 : double result;
7025 :
7026 :
7027 0 : z = 0.5*x;
7028 0 : ae_assert(ae_fp_greater(z,(double)(0)), "Domain error in K1", _state);
7029 0 : if( ae_fp_less_eq(x,(double)(2)) )
7030 : {
7031 0 : y = x*x-2.0;
7032 0 : bessel_besselm1firstcheb(-7.02386347938628759343E-18, &b0, &b1, &b2, _state);
7033 0 : bessel_besselm1nextcheb(y, -2.42744985051936593393E-15, &b0, &b1, &b2, _state);
7034 0 : bessel_besselm1nextcheb(y, -6.66690169419932900609E-13, &b0, &b1, &b2, _state);
7035 0 : bessel_besselm1nextcheb(y, -1.41148839263352776110E-10, &b0, &b1, &b2, _state);
7036 0 : bessel_besselm1nextcheb(y, -2.21338763073472585583E-8, &b0, &b1, &b2, _state);
7037 0 : bessel_besselm1nextcheb(y, -2.43340614156596823496E-6, &b0, &b1, &b2, _state);
7038 0 : bessel_besselm1nextcheb(y, -1.73028895751305206302E-4, &b0, &b1, &b2, _state);
7039 0 : bessel_besselm1nextcheb(y, -6.97572385963986435018E-3, &b0, &b1, &b2, _state);
7040 0 : bessel_besselm1nextcheb(y, -1.22611180822657148235E-1, &b0, &b1, &b2, _state);
7041 0 : bessel_besselm1nextcheb(y, -3.53155960776544875667E-1, &b0, &b1, &b2, _state);
7042 0 : bessel_besselm1nextcheb(y, 1.52530022733894777053E0, &b0, &b1, &b2, _state);
7043 0 : v = 0.5*(b0-b2);
7044 0 : result = ae_log(z, _state)*besseli1(x, _state)+v/x;
7045 : }
7046 : else
7047 : {
7048 0 : y = 8.0/x-2.0;
7049 0 : bessel_besselm1firstcheb(-5.75674448366501715755E-18, &b0, &b1, &b2, _state);
7050 0 : bessel_besselm1nextcheb(y, 1.79405087314755922667E-17, &b0, &b1, &b2, _state);
7051 0 : bessel_besselm1nextcheb(y, -5.68946255844285935196E-17, &b0, &b1, &b2, _state);
7052 0 : bessel_besselm1nextcheb(y, 1.83809354436663880070E-16, &b0, &b1, &b2, _state);
7053 0 : bessel_besselm1nextcheb(y, -6.05704724837331885336E-16, &b0, &b1, &b2, _state);
7054 0 : bessel_besselm1nextcheb(y, 2.03870316562433424052E-15, &b0, &b1, &b2, _state);
7055 0 : bessel_besselm1nextcheb(y, -7.01983709041831346144E-15, &b0, &b1, &b2, _state);
7056 0 : bessel_besselm1nextcheb(y, 2.47715442448130437068E-14, &b0, &b1, &b2, _state);
7057 0 : bessel_besselm1nextcheb(y, -8.97670518232499435011E-14, &b0, &b1, &b2, _state);
7058 0 : bessel_besselm1nextcheb(y, 3.34841966607842919884E-13, &b0, &b1, &b2, _state);
7059 0 : bessel_besselm1nextcheb(y, -1.28917396095102890680E-12, &b0, &b1, &b2, _state);
7060 0 : bessel_besselm1nextcheb(y, 5.13963967348173025100E-12, &b0, &b1, &b2, _state);
7061 0 : bessel_besselm1nextcheb(y, -2.12996783842756842877E-11, &b0, &b1, &b2, _state);
7062 0 : bessel_besselm1nextcheb(y, 9.21831518760500529508E-11, &b0, &b1, &b2, _state);
7063 0 : bessel_besselm1nextcheb(y, -4.19035475934189648750E-10, &b0, &b1, &b2, _state);
7064 0 : bessel_besselm1nextcheb(y, 2.01504975519703286596E-9, &b0, &b1, &b2, _state);
7065 0 : bessel_besselm1nextcheb(y, -1.03457624656780970260E-8, &b0, &b1, &b2, _state);
7066 0 : bessel_besselm1nextcheb(y, 5.74108412545004946722E-8, &b0, &b1, &b2, _state);
7067 0 : bessel_besselm1nextcheb(y, -3.50196060308781257119E-7, &b0, &b1, &b2, _state);
7068 0 : bessel_besselm1nextcheb(y, 2.40648494783721712015E-6, &b0, &b1, &b2, _state);
7069 0 : bessel_besselm1nextcheb(y, -1.93619797416608296024E-5, &b0, &b1, &b2, _state);
7070 0 : bessel_besselm1nextcheb(y, 1.95215518471351631108E-4, &b0, &b1, &b2, _state);
7071 0 : bessel_besselm1nextcheb(y, -2.85781685962277938680E-3, &b0, &b1, &b2, _state);
7072 0 : bessel_besselm1nextcheb(y, 1.03923736576817238437E-1, &b0, &b1, &b2, _state);
7073 0 : bessel_besselm1nextcheb(y, 2.72062619048444266945E0, &b0, &b1, &b2, _state);
7074 0 : v = 0.5*(b0-b2);
7075 0 : result = ae_exp(-x, _state)*v/ae_sqrt(x, _state);
7076 : }
7077 0 : return result;
7078 : }
7079 :
7080 :
7081 : /*************************************************************************
7082 : Modified Bessel function, second kind, integer order
7083 :
7084 : Returns modified Bessel function of the second kind
7085 : of order n of the argument.
7086 :
7087 : The range is partitioned into the two intervals [0,9.55] and
7088 : (9.55, infinity). An ascending power series is used in the
7089 : low range, and an asymptotic expansion in the high range.
7090 :
7091 : ACCURACY:
7092 :
7093 : Relative error:
7094 : arithmetic domain # trials peak rms
7095 : IEEE 0,30 90000 1.8e-8 3.0e-10
7096 :
7097 : Error is high only near the crossover point x = 9.55
7098 : between the two expansions used.
7099 :
7100 : Cephes Math Library Release 2.8: June, 2000
7101 : Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
7102 : *************************************************************************/
7103 0 : double besselkn(ae_int_t nn, double x, ae_state *_state)
7104 : {
7105 : double k;
7106 : double kf;
7107 : double nk1f;
7108 : double nkf;
7109 : double zn;
7110 : double t;
7111 : double s;
7112 : double z0;
7113 : double z;
7114 : double ans;
7115 : double fn;
7116 : double pn;
7117 : double pk;
7118 : double zmn;
7119 : double tlg;
7120 : double tox;
7121 : ae_int_t i;
7122 : ae_int_t n;
7123 : double eul;
7124 : double result;
7125 :
7126 :
7127 0 : eul = 5.772156649015328606065e-1;
7128 0 : if( nn<0 )
7129 : {
7130 0 : n = -nn;
7131 : }
7132 : else
7133 : {
7134 0 : n = nn;
7135 : }
7136 0 : ae_assert(n<=31, "Overflow in BesselKN", _state);
7137 0 : ae_assert(ae_fp_greater(x,(double)(0)), "Domain error in BesselKN", _state);
7138 0 : if( ae_fp_less_eq(x,9.55) )
7139 : {
7140 0 : ans = 0.0;
7141 0 : z0 = 0.25*x*x;
7142 0 : fn = 1.0;
7143 0 : pn = 0.0;
7144 0 : zmn = 1.0;
7145 0 : tox = 2.0/x;
7146 0 : if( n>0 )
7147 : {
7148 0 : pn = -eul;
7149 0 : k = 1.0;
7150 0 : for(i=1; i<=n-1; i++)
7151 : {
7152 0 : pn = pn+1.0/k;
7153 0 : k = k+1.0;
7154 0 : fn = fn*k;
7155 : }
7156 0 : zmn = tox;
7157 0 : if( n==1 )
7158 : {
7159 0 : ans = 1.0/x;
7160 : }
7161 : else
7162 : {
7163 0 : nk1f = fn/n;
7164 0 : kf = 1.0;
7165 0 : s = nk1f;
7166 0 : z = -z0;
7167 0 : zn = 1.0;
7168 0 : for(i=1; i<=n-1; i++)
7169 : {
7170 0 : nk1f = nk1f/(n-i);
7171 0 : kf = kf*i;
7172 0 : zn = zn*z;
7173 0 : t = nk1f*zn/kf;
7174 0 : s = s+t;
7175 0 : ae_assert(ae_fp_greater(ae_maxrealnumber-ae_fabs(t, _state),ae_fabs(s, _state)), "Overflow in BesselKN", _state);
7176 0 : ae_assert(!(ae_fp_greater(tox,1.0)&&ae_fp_less(ae_maxrealnumber/tox,zmn)), "Overflow in BesselKN", _state);
7177 0 : zmn = zmn*tox;
7178 : }
7179 0 : s = s*0.5;
7180 0 : t = ae_fabs(s, _state);
7181 0 : ae_assert(!(ae_fp_greater(zmn,1.0)&&ae_fp_less(ae_maxrealnumber/zmn,t)), "Overflow in BesselKN", _state);
7182 0 : ae_assert(!(ae_fp_greater(t,1.0)&&ae_fp_less(ae_maxrealnumber/t,zmn)), "Overflow in BesselKN", _state);
7183 0 : ans = s*zmn;
7184 : }
7185 : }
7186 0 : tlg = 2.0*ae_log(0.5*x, _state);
7187 0 : pk = -eul;
7188 0 : if( n==0 )
7189 : {
7190 0 : pn = pk;
7191 0 : t = 1.0;
7192 : }
7193 : else
7194 : {
7195 0 : pn = pn+1.0/n;
7196 0 : t = 1.0/fn;
7197 : }
7198 0 : s = (pk+pn-tlg)*t;
7199 0 : k = 1.0;
7200 : do
7201 : {
7202 0 : t = t*(z0/(k*(k+n)));
7203 0 : pk = pk+1.0/k;
7204 0 : pn = pn+1.0/(k+n);
7205 0 : s = s+(pk+pn-tlg)*t;
7206 0 : k = k+1.0;
7207 : }
7208 0 : while(ae_fp_greater(ae_fabs(t/s, _state),ae_machineepsilon));
7209 0 : s = 0.5*s/zmn;
7210 0 : if( n%2!=0 )
7211 : {
7212 0 : s = -s;
7213 : }
7214 0 : ans = ans+s;
7215 0 : result = ans;
7216 0 : return result;
7217 : }
7218 0 : if( ae_fp_greater(x,ae_log(ae_maxrealnumber, _state)) )
7219 : {
7220 0 : result = (double)(0);
7221 0 : return result;
7222 : }
7223 0 : k = (double)(n);
7224 0 : pn = 4.0*k*k;
7225 0 : pk = 1.0;
7226 0 : z0 = 8.0*x;
7227 0 : fn = 1.0;
7228 0 : t = 1.0;
7229 0 : s = t;
7230 0 : nkf = ae_maxrealnumber;
7231 0 : i = 0;
7232 : do
7233 : {
7234 0 : z = pn-pk*pk;
7235 0 : t = t*z/(fn*z0);
7236 0 : nk1f = ae_fabs(t, _state);
7237 0 : if( i>=n&&ae_fp_greater(nk1f,nkf) )
7238 : {
7239 0 : break;
7240 : }
7241 0 : nkf = nk1f;
7242 0 : s = s+t;
7243 0 : fn = fn+1.0;
7244 0 : pk = pk+2.0;
7245 0 : i = i+1;
7246 : }
7247 0 : while(ae_fp_greater(ae_fabs(t/s, _state),ae_machineepsilon));
7248 0 : result = ae_exp(-x, _state)*ae_sqrt(ae_pi/(2.0*x), _state)*s;
7249 0 : return result;
7250 : }
7251 :
7252 :
7253 : /*************************************************************************
7254 : Internal subroutine
7255 :
7256 : Cephes Math Library Release 2.8: June, 2000
7257 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
7258 : *************************************************************************/
7259 0 : static void bessel_besselmfirstcheb(double c,
7260 : double* b0,
7261 : double* b1,
7262 : double* b2,
7263 : ae_state *_state)
7264 : {
7265 :
7266 :
7267 0 : *b0 = c;
7268 0 : *b1 = 0.0;
7269 0 : *b2 = 0.0;
7270 0 : }
7271 :
7272 :
7273 : /*************************************************************************
7274 : Internal subroutine
7275 :
7276 : Cephes Math Library Release 2.8: June, 2000
7277 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
7278 : *************************************************************************/
7279 0 : static void bessel_besselmnextcheb(double x,
7280 : double c,
7281 : double* b0,
7282 : double* b1,
7283 : double* b2,
7284 : ae_state *_state)
7285 : {
7286 :
7287 :
7288 0 : *b2 = *b1;
7289 0 : *b1 = *b0;
7290 0 : *b0 = x*(*b1)-(*b2)+c;
7291 0 : }
7292 :
7293 :
7294 : /*************************************************************************
7295 : Internal subroutine
7296 :
7297 : Cephes Math Library Release 2.8: June, 2000
7298 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
7299 : *************************************************************************/
7300 0 : static void bessel_besselm1firstcheb(double c,
7301 : double* b0,
7302 : double* b1,
7303 : double* b2,
7304 : ae_state *_state)
7305 : {
7306 :
7307 :
7308 0 : *b0 = c;
7309 0 : *b1 = 0.0;
7310 0 : *b2 = 0.0;
7311 0 : }
7312 :
7313 :
7314 : /*************************************************************************
7315 : Internal subroutine
7316 :
7317 : Cephes Math Library Release 2.8: June, 2000
7318 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
7319 : *************************************************************************/
7320 0 : static void bessel_besselm1nextcheb(double x,
7321 : double c,
7322 : double* b0,
7323 : double* b1,
7324 : double* b2,
7325 : ae_state *_state)
7326 : {
7327 :
7328 :
7329 0 : *b2 = *b1;
7330 0 : *b1 = *b0;
7331 0 : *b0 = x*(*b1)-(*b2)+c;
7332 0 : }
7333 :
7334 :
7335 0 : static void bessel_besselasympt0(double x,
7336 : double* pzero,
7337 : double* qzero,
7338 : ae_state *_state)
7339 : {
7340 : double xsq;
7341 : double p2;
7342 : double q2;
7343 : double p3;
7344 : double q3;
7345 :
7346 0 : *pzero = 0;
7347 0 : *qzero = 0;
7348 :
7349 0 : xsq = 64.0/(x*x);
7350 0 : p2 = 0.0;
7351 0 : p2 = 2485.271928957404011288128951+xsq*p2;
7352 0 : p2 = 153982.6532623911470917825993+xsq*p2;
7353 0 : p2 = 2016135.283049983642487182349+xsq*p2;
7354 0 : p2 = 8413041.456550439208464315611+xsq*p2;
7355 0 : p2 = 12332384.76817638145232406055+xsq*p2;
7356 0 : p2 = 5393485.083869438325262122897+xsq*p2;
7357 0 : q2 = 1.0;
7358 0 : q2 = 2615.700736920839685159081813+xsq*q2;
7359 0 : q2 = 156001.7276940030940592769933+xsq*q2;
7360 0 : q2 = 2025066.801570134013891035236+xsq*q2;
7361 0 : q2 = 8426449.050629797331554404810+xsq*q2;
7362 0 : q2 = 12338310.22786324960844856182+xsq*q2;
7363 0 : q2 = 5393485.083869438325560444960+xsq*q2;
7364 0 : p3 = -0.0;
7365 0 : p3 = -4.887199395841261531199129300+xsq*p3;
7366 0 : p3 = -226.2630641933704113967255053+xsq*p3;
7367 0 : p3 = -2365.956170779108192723612816+xsq*p3;
7368 0 : p3 = -8239.066313485606568803548860+xsq*p3;
7369 0 : p3 = -10381.41698748464093880530341+xsq*p3;
7370 0 : p3 = -3984.617357595222463506790588+xsq*p3;
7371 0 : q3 = 1.0;
7372 0 : q3 = 408.7714673983499223402830260+xsq*q3;
7373 0 : q3 = 15704.89191515395519392882766+xsq*q3;
7374 0 : q3 = 156021.3206679291652539287109+xsq*q3;
7375 0 : q3 = 533291.3634216897168722255057+xsq*q3;
7376 0 : q3 = 666745.4239319826986004038103+xsq*q3;
7377 0 : q3 = 255015.5108860942382983170882+xsq*q3;
7378 0 : *pzero = p2/q2;
7379 0 : *qzero = 8*p3/q3/x;
7380 0 : }
7381 :
7382 :
7383 0 : static void bessel_besselasympt1(double x,
7384 : double* pzero,
7385 : double* qzero,
7386 : ae_state *_state)
7387 : {
7388 : double xsq;
7389 : double p2;
7390 : double q2;
7391 : double p3;
7392 : double q3;
7393 :
7394 0 : *pzero = 0;
7395 0 : *qzero = 0;
7396 :
7397 0 : xsq = 64.0/(x*x);
7398 0 : p2 = -1611.616644324610116477412898;
7399 0 : p2 = -109824.0554345934672737413139+xsq*p2;
7400 0 : p2 = -1523529.351181137383255105722+xsq*p2;
7401 0 : p2 = -6603373.248364939109255245434+xsq*p2;
7402 0 : p2 = -9942246.505077641195658377899+xsq*p2;
7403 0 : p2 = -4435757.816794127857114720794+xsq*p2;
7404 0 : q2 = 1.0;
7405 0 : q2 = -1455.009440190496182453565068+xsq*q2;
7406 0 : q2 = -107263.8599110382011903063867+xsq*q2;
7407 0 : q2 = -1511809.506634160881644546358+xsq*q2;
7408 0 : q2 = -6585339.479723087072826915069+xsq*q2;
7409 0 : q2 = -9934124.389934585658967556309+xsq*q2;
7410 0 : q2 = -4435757.816794127856828016962+xsq*q2;
7411 0 : p3 = 35.26513384663603218592175580;
7412 0 : p3 = 1706.375429020768002061283546+xsq*p3;
7413 0 : p3 = 18494.26287322386679652009819+xsq*p3;
7414 0 : p3 = 66178.83658127083517939992166+xsq*p3;
7415 0 : p3 = 85145.16067533570196555001171+xsq*p3;
7416 0 : p3 = 33220.91340985722351859704442+xsq*p3;
7417 0 : q3 = 1.0;
7418 0 : q3 = 863.8367769604990967475517183+xsq*q3;
7419 0 : q3 = 37890.22974577220264142952256+xsq*q3;
7420 0 : q3 = 400294.4358226697511708610813+xsq*q3;
7421 0 : q3 = 1419460.669603720892855755253+xsq*q3;
7422 0 : q3 = 1819458.042243997298924553839+xsq*q3;
7423 0 : q3 = 708712.8194102874357377502472+xsq*q3;
7424 0 : *pzero = p2/q2;
7425 0 : *qzero = 8*p3/q3/x;
7426 0 : }
7427 :
7428 :
7429 : #endif
7430 : #if defined(AE_COMPILE_IBETAF) || !defined(AE_PARTIAL_BUILD)
7431 :
7432 :
7433 : /*************************************************************************
7434 : Incomplete beta integral
7435 :
7436 : Returns incomplete beta integral of the arguments, evaluated
7437 : from zero to x. The function is defined as
7438 :
7439 : x
7440 : - -
7441 : | (a+b) | | a-1 b-1
7442 : ----------- | t (1-t) dt.
7443 : - - | |
7444 : | (a) | (b) -
7445 : 0
7446 :
7447 : The domain of definition is 0 <= x <= 1. In this
7448 : implementation a and b are restricted to positive values.
7449 : The integral from x to 1 may be obtained by the symmetry
7450 : relation
7451 :
7452 : 1 - incbet( a, b, x ) = incbet( b, a, 1-x ).
7453 :
7454 : The integral is evaluated by a continued fraction expansion
7455 : or, when b*x is small, by a power series.
7456 :
7457 : ACCURACY:
7458 :
7459 : Tested at uniformly distributed random points (a,b,x) with a and b
7460 : in "domain" and x between 0 and 1.
7461 : Relative error
7462 : arithmetic domain # trials peak rms
7463 : IEEE 0,5 10000 6.9e-15 4.5e-16
7464 : IEEE 0,85 250000 2.2e-13 1.7e-14
7465 : IEEE 0,1000 30000 5.3e-12 6.3e-13
7466 : IEEE 0,10000 250000 9.3e-11 7.1e-12
7467 : IEEE 0,100000 10000 8.7e-10 4.8e-11
7468 : Outputs smaller than the IEEE gradual underflow threshold
7469 : were excluded from these statistics.
7470 :
7471 : Cephes Math Library, Release 2.8: June, 2000
7472 : Copyright 1984, 1995, 2000 by Stephen L. Moshier
7473 : *************************************************************************/
7474 0 : double incompletebeta(double a, double b, double x, ae_state *_state)
7475 : {
7476 : double t;
7477 : double xc;
7478 : double w;
7479 : double y;
7480 : ae_int_t flag;
7481 : double sg;
7482 : double big;
7483 : double biginv;
7484 : double maxgam;
7485 : double minlog;
7486 : double maxlog;
7487 : double result;
7488 :
7489 :
7490 0 : big = 4.503599627370496e15;
7491 0 : biginv = 2.22044604925031308085e-16;
7492 0 : maxgam = 171.624376956302725;
7493 0 : minlog = ae_log(ae_minrealnumber, _state);
7494 0 : maxlog = ae_log(ae_maxrealnumber, _state);
7495 0 : ae_assert(ae_fp_greater(a,(double)(0))&&ae_fp_greater(b,(double)(0)), "Domain error in IncompleteBeta", _state);
7496 0 : ae_assert(ae_fp_greater_eq(x,(double)(0))&&ae_fp_less_eq(x,(double)(1)), "Domain error in IncompleteBeta", _state);
7497 0 : if( ae_fp_eq(x,(double)(0)) )
7498 : {
7499 0 : result = (double)(0);
7500 0 : return result;
7501 : }
7502 0 : if( ae_fp_eq(x,(double)(1)) )
7503 : {
7504 0 : result = (double)(1);
7505 0 : return result;
7506 : }
7507 0 : flag = 0;
7508 0 : if( ae_fp_less_eq(b*x,1.0)&&ae_fp_less_eq(x,0.95) )
7509 : {
7510 0 : result = ibetaf_incompletebetaps(a, b, x, maxgam, _state);
7511 0 : return result;
7512 : }
7513 0 : w = 1.0-x;
7514 0 : if( ae_fp_greater(x,a/(a+b)) )
7515 : {
7516 0 : flag = 1;
7517 0 : t = a;
7518 0 : a = b;
7519 0 : b = t;
7520 0 : xc = x;
7521 0 : x = w;
7522 : }
7523 : else
7524 : {
7525 0 : xc = w;
7526 : }
7527 0 : if( (flag==1&&ae_fp_less_eq(b*x,1.0))&&ae_fp_less_eq(x,0.95) )
7528 : {
7529 0 : t = ibetaf_incompletebetaps(a, b, x, maxgam, _state);
7530 0 : if( ae_fp_less_eq(t,ae_machineepsilon) )
7531 : {
7532 0 : result = 1.0-ae_machineepsilon;
7533 : }
7534 : else
7535 : {
7536 0 : result = 1.0-t;
7537 : }
7538 0 : return result;
7539 : }
7540 0 : y = x*(a+b-2.0)-(a-1.0);
7541 0 : if( ae_fp_less(y,0.0) )
7542 : {
7543 0 : w = ibetaf_incompletebetafe(a, b, x, big, biginv, _state);
7544 : }
7545 : else
7546 : {
7547 0 : w = ibetaf_incompletebetafe2(a, b, x, big, biginv, _state)/xc;
7548 : }
7549 0 : y = a*ae_log(x, _state);
7550 0 : t = b*ae_log(xc, _state);
7551 0 : if( (ae_fp_less(a+b,maxgam)&&ae_fp_less(ae_fabs(y, _state),maxlog))&&ae_fp_less(ae_fabs(t, _state),maxlog) )
7552 : {
7553 0 : t = ae_pow(xc, b, _state);
7554 0 : t = t*ae_pow(x, a, _state);
7555 0 : t = t/a;
7556 0 : t = t*w;
7557 0 : t = t*(gammafunction(a+b, _state)/(gammafunction(a, _state)*gammafunction(b, _state)));
7558 0 : if( flag==1 )
7559 : {
7560 0 : if( ae_fp_less_eq(t,ae_machineepsilon) )
7561 : {
7562 0 : result = 1.0-ae_machineepsilon;
7563 : }
7564 : else
7565 : {
7566 0 : result = 1.0-t;
7567 : }
7568 : }
7569 : else
7570 : {
7571 0 : result = t;
7572 : }
7573 0 : return result;
7574 : }
7575 0 : y = y+t+lngamma(a+b, &sg, _state)-lngamma(a, &sg, _state)-lngamma(b, &sg, _state);
7576 0 : y = y+ae_log(w/a, _state);
7577 0 : if( ae_fp_less(y,minlog) )
7578 : {
7579 0 : t = 0.0;
7580 : }
7581 : else
7582 : {
7583 0 : t = ae_exp(y, _state);
7584 : }
7585 0 : if( flag==1 )
7586 : {
7587 0 : if( ae_fp_less_eq(t,ae_machineepsilon) )
7588 : {
7589 0 : t = 1.0-ae_machineepsilon;
7590 : }
7591 : else
7592 : {
7593 0 : t = 1.0-t;
7594 : }
7595 : }
7596 0 : result = t;
7597 0 : return result;
7598 : }
7599 :
7600 :
7601 : /*************************************************************************
7602 : Inverse of imcomplete beta integral
7603 :
7604 : Given y, the function finds x such that
7605 :
7606 : incbet( a, b, x ) = y .
7607 :
7608 : The routine performs interval halving or Newton iterations to find the
7609 : root of incbet(a,b,x) - y = 0.
7610 :
7611 :
7612 : ACCURACY:
7613 :
7614 : Relative error:
7615 : x a,b
7616 : arithmetic domain domain # trials peak rms
7617 : IEEE 0,1 .5,10000 50000 5.8e-12 1.3e-13
7618 : IEEE 0,1 .25,100 100000 1.8e-13 3.9e-15
7619 : IEEE 0,1 0,5 50000 1.1e-12 5.5e-15
7620 : With a and b constrained to half-integer or integer values:
7621 : IEEE 0,1 .5,10000 50000 5.8e-12 1.1e-13
7622 : IEEE 0,1 .5,100 100000 1.7e-14 7.9e-16
7623 : With a = .5, b constrained to half-integer or integer values:
7624 : IEEE 0,1 .5,10000 10000 8.3e-11 1.0e-11
7625 :
7626 : Cephes Math Library Release 2.8: June, 2000
7627 : Copyright 1984, 1996, 2000 by Stephen L. Moshier
7628 : *************************************************************************/
7629 0 : double invincompletebeta(double a, double b, double y, ae_state *_state)
7630 : {
7631 : double aaa;
7632 : double bbb;
7633 : double y0;
7634 : double d;
7635 : double yyy;
7636 : double x;
7637 : double x0;
7638 : double x1;
7639 : double lgm;
7640 : double yp;
7641 : double di;
7642 : double dithresh;
7643 : double yl;
7644 : double yh;
7645 : double xt;
7646 : ae_int_t i;
7647 : ae_int_t rflg;
7648 : ae_int_t dir;
7649 : ae_int_t nflg;
7650 : double s;
7651 : ae_int_t mainlooppos;
7652 : ae_int_t ihalve;
7653 : ae_int_t ihalvecycle;
7654 : ae_int_t newt;
7655 : ae_int_t newtcycle;
7656 : ae_int_t breaknewtcycle;
7657 : ae_int_t breakihalvecycle;
7658 : double result;
7659 :
7660 :
7661 0 : i = 0;
7662 0 : ae_assert(ae_fp_greater_eq(y,(double)(0))&&ae_fp_less_eq(y,(double)(1)), "Domain error in InvIncompleteBeta", _state);
7663 :
7664 : /*
7665 : * special cases
7666 : */
7667 0 : if( ae_fp_eq(y,(double)(0)) )
7668 : {
7669 0 : result = (double)(0);
7670 0 : return result;
7671 : }
7672 0 : if( ae_fp_eq(y,1.0) )
7673 : {
7674 0 : result = (double)(1);
7675 0 : return result;
7676 : }
7677 :
7678 : /*
7679 : * these initializations are not really necessary,
7680 : * but without them compiler complains about 'possibly uninitialized variables'.
7681 : */
7682 0 : dithresh = (double)(0);
7683 0 : rflg = 0;
7684 0 : aaa = (double)(0);
7685 0 : bbb = (double)(0);
7686 0 : y0 = (double)(0);
7687 0 : x = (double)(0);
7688 0 : yyy = (double)(0);
7689 0 : lgm = (double)(0);
7690 0 : dir = 0;
7691 0 : di = (double)(0);
7692 :
7693 : /*
7694 : * normal initializations
7695 : */
7696 0 : x0 = 0.0;
7697 0 : yl = 0.0;
7698 0 : x1 = 1.0;
7699 0 : yh = 1.0;
7700 0 : nflg = 0;
7701 0 : mainlooppos = 0;
7702 0 : ihalve = 1;
7703 0 : ihalvecycle = 2;
7704 0 : newt = 3;
7705 0 : newtcycle = 4;
7706 0 : breaknewtcycle = 5;
7707 0 : breakihalvecycle = 6;
7708 :
7709 : /*
7710 : * main loop
7711 : */
7712 : for(;;)
7713 : {
7714 :
7715 : /*
7716 : * start
7717 : */
7718 0 : if( mainlooppos==0 )
7719 : {
7720 0 : if( ae_fp_less_eq(a,1.0)||ae_fp_less_eq(b,1.0) )
7721 : {
7722 0 : dithresh = 1.0e-6;
7723 0 : rflg = 0;
7724 0 : aaa = a;
7725 0 : bbb = b;
7726 0 : y0 = y;
7727 0 : x = aaa/(aaa+bbb);
7728 0 : yyy = incompletebeta(aaa, bbb, x, _state);
7729 0 : mainlooppos = ihalve;
7730 0 : continue;
7731 : }
7732 : else
7733 : {
7734 0 : dithresh = 1.0e-4;
7735 : }
7736 0 : yp = -invnormaldistribution(y, _state);
7737 0 : if( ae_fp_greater(y,0.5) )
7738 : {
7739 0 : rflg = 1;
7740 0 : aaa = b;
7741 0 : bbb = a;
7742 0 : y0 = 1.0-y;
7743 0 : yp = -yp;
7744 : }
7745 : else
7746 : {
7747 0 : rflg = 0;
7748 0 : aaa = a;
7749 0 : bbb = b;
7750 0 : y0 = y;
7751 : }
7752 0 : lgm = (yp*yp-3.0)/6.0;
7753 0 : x = 2.0/(1.0/(2.0*aaa-1.0)+1.0/(2.0*bbb-1.0));
7754 0 : d = yp*ae_sqrt(x+lgm, _state)/x-(1.0/(2.0*bbb-1.0)-1.0/(2.0*aaa-1.0))*(lgm+5.0/6.0-2.0/(3.0*x));
7755 0 : d = 2.0*d;
7756 0 : if( ae_fp_less(d,ae_log(ae_minrealnumber, _state)) )
7757 : {
7758 0 : x = (double)(0);
7759 0 : break;
7760 : }
7761 0 : x = aaa/(aaa+bbb*ae_exp(d, _state));
7762 0 : yyy = incompletebeta(aaa, bbb, x, _state);
7763 0 : yp = (yyy-y0)/y0;
7764 0 : if( ae_fp_less(ae_fabs(yp, _state),0.2) )
7765 : {
7766 0 : mainlooppos = newt;
7767 0 : continue;
7768 : }
7769 0 : mainlooppos = ihalve;
7770 0 : continue;
7771 : }
7772 :
7773 : /*
7774 : * ihalve
7775 : */
7776 0 : if( mainlooppos==ihalve )
7777 : {
7778 0 : dir = 0;
7779 0 : di = 0.5;
7780 0 : i = 0;
7781 0 : mainlooppos = ihalvecycle;
7782 0 : continue;
7783 : }
7784 :
7785 : /*
7786 : * ihalvecycle
7787 : */
7788 0 : if( mainlooppos==ihalvecycle )
7789 : {
7790 0 : if( i<=99 )
7791 : {
7792 0 : if( i!=0 )
7793 : {
7794 0 : x = x0+di*(x1-x0);
7795 0 : if( ae_fp_eq(x,1.0) )
7796 : {
7797 0 : x = 1.0-ae_machineepsilon;
7798 : }
7799 0 : if( ae_fp_eq(x,0.0) )
7800 : {
7801 0 : di = 0.5;
7802 0 : x = x0+di*(x1-x0);
7803 0 : if( ae_fp_eq(x,0.0) )
7804 : {
7805 0 : break;
7806 : }
7807 : }
7808 0 : yyy = incompletebeta(aaa, bbb, x, _state);
7809 0 : yp = (x1-x0)/(x1+x0);
7810 0 : if( ae_fp_less(ae_fabs(yp, _state),dithresh) )
7811 : {
7812 0 : mainlooppos = newt;
7813 0 : continue;
7814 : }
7815 0 : yp = (yyy-y0)/y0;
7816 0 : if( ae_fp_less(ae_fabs(yp, _state),dithresh) )
7817 : {
7818 0 : mainlooppos = newt;
7819 0 : continue;
7820 : }
7821 : }
7822 0 : if( ae_fp_less(yyy,y0) )
7823 : {
7824 0 : x0 = x;
7825 0 : yl = yyy;
7826 0 : if( dir<0 )
7827 : {
7828 0 : dir = 0;
7829 0 : di = 0.5;
7830 : }
7831 : else
7832 : {
7833 0 : if( dir>3 )
7834 : {
7835 0 : di = 1.0-(1.0-di)*(1.0-di);
7836 : }
7837 : else
7838 : {
7839 0 : if( dir>1 )
7840 : {
7841 0 : di = 0.5*di+0.5;
7842 : }
7843 : else
7844 : {
7845 0 : di = (y0-yyy)/(yh-yl);
7846 : }
7847 : }
7848 : }
7849 0 : dir = dir+1;
7850 0 : if( ae_fp_greater(x0,0.75) )
7851 : {
7852 0 : if( rflg==1 )
7853 : {
7854 0 : rflg = 0;
7855 0 : aaa = a;
7856 0 : bbb = b;
7857 0 : y0 = y;
7858 : }
7859 : else
7860 : {
7861 0 : rflg = 1;
7862 0 : aaa = b;
7863 0 : bbb = a;
7864 0 : y0 = 1.0-y;
7865 : }
7866 0 : x = 1.0-x;
7867 0 : yyy = incompletebeta(aaa, bbb, x, _state);
7868 0 : x0 = 0.0;
7869 0 : yl = 0.0;
7870 0 : x1 = 1.0;
7871 0 : yh = 1.0;
7872 0 : mainlooppos = ihalve;
7873 0 : continue;
7874 : }
7875 : }
7876 : else
7877 : {
7878 0 : x1 = x;
7879 0 : if( rflg==1&&ae_fp_less(x1,ae_machineepsilon) )
7880 : {
7881 0 : x = 0.0;
7882 0 : break;
7883 : }
7884 0 : yh = yyy;
7885 0 : if( dir>0 )
7886 : {
7887 0 : dir = 0;
7888 0 : di = 0.5;
7889 : }
7890 : else
7891 : {
7892 0 : if( dir<-3 )
7893 : {
7894 0 : di = di*di;
7895 : }
7896 : else
7897 : {
7898 0 : if( dir<-1 )
7899 : {
7900 0 : di = 0.5*di;
7901 : }
7902 : else
7903 : {
7904 0 : di = (yyy-y0)/(yh-yl);
7905 : }
7906 : }
7907 : }
7908 0 : dir = dir-1;
7909 : }
7910 0 : i = i+1;
7911 0 : mainlooppos = ihalvecycle;
7912 0 : continue;
7913 : }
7914 : else
7915 : {
7916 0 : mainlooppos = breakihalvecycle;
7917 0 : continue;
7918 : }
7919 : }
7920 :
7921 : /*
7922 : * breakihalvecycle
7923 : */
7924 0 : if( mainlooppos==breakihalvecycle )
7925 : {
7926 0 : if( ae_fp_greater_eq(x0,1.0) )
7927 : {
7928 0 : x = 1.0-ae_machineepsilon;
7929 0 : break;
7930 : }
7931 0 : if( ae_fp_less_eq(x,0.0) )
7932 : {
7933 0 : x = 0.0;
7934 0 : break;
7935 : }
7936 0 : mainlooppos = newt;
7937 0 : continue;
7938 : }
7939 :
7940 : /*
7941 : * newt
7942 : */
7943 0 : if( mainlooppos==newt )
7944 : {
7945 0 : if( nflg!=0 )
7946 : {
7947 0 : break;
7948 : }
7949 0 : nflg = 1;
7950 0 : lgm = lngamma(aaa+bbb, &s, _state)-lngamma(aaa, &s, _state)-lngamma(bbb, &s, _state);
7951 0 : i = 0;
7952 0 : mainlooppos = newtcycle;
7953 0 : continue;
7954 : }
7955 :
7956 : /*
7957 : * newtcycle
7958 : */
7959 0 : if( mainlooppos==newtcycle )
7960 : {
7961 0 : if( i<=7 )
7962 : {
7963 0 : if( i!=0 )
7964 : {
7965 0 : yyy = incompletebeta(aaa, bbb, x, _state);
7966 : }
7967 0 : if( ae_fp_less(yyy,yl) )
7968 : {
7969 0 : x = x0;
7970 0 : yyy = yl;
7971 : }
7972 : else
7973 : {
7974 0 : if( ae_fp_greater(yyy,yh) )
7975 : {
7976 0 : x = x1;
7977 0 : yyy = yh;
7978 : }
7979 : else
7980 : {
7981 0 : if( ae_fp_less(yyy,y0) )
7982 : {
7983 0 : x0 = x;
7984 0 : yl = yyy;
7985 : }
7986 : else
7987 : {
7988 0 : x1 = x;
7989 0 : yh = yyy;
7990 : }
7991 : }
7992 : }
7993 0 : if( ae_fp_eq(x,1.0)||ae_fp_eq(x,0.0) )
7994 : {
7995 0 : mainlooppos = breaknewtcycle;
7996 0 : continue;
7997 : }
7998 0 : d = (aaa-1.0)*ae_log(x, _state)+(bbb-1.0)*ae_log(1.0-x, _state)+lgm;
7999 0 : if( ae_fp_less(d,ae_log(ae_minrealnumber, _state)) )
8000 : {
8001 0 : break;
8002 : }
8003 0 : if( ae_fp_greater(d,ae_log(ae_maxrealnumber, _state)) )
8004 : {
8005 0 : mainlooppos = breaknewtcycle;
8006 0 : continue;
8007 : }
8008 0 : d = ae_exp(d, _state);
8009 0 : d = (yyy-y0)/d;
8010 0 : xt = x-d;
8011 0 : if( ae_fp_less_eq(xt,x0) )
8012 : {
8013 0 : yyy = (x-x0)/(x1-x0);
8014 0 : xt = x0+0.5*yyy*(x-x0);
8015 0 : if( ae_fp_less_eq(xt,0.0) )
8016 : {
8017 0 : mainlooppos = breaknewtcycle;
8018 0 : continue;
8019 : }
8020 : }
8021 0 : if( ae_fp_greater_eq(xt,x1) )
8022 : {
8023 0 : yyy = (x1-x)/(x1-x0);
8024 0 : xt = x1-0.5*yyy*(x1-x);
8025 0 : if( ae_fp_greater_eq(xt,1.0) )
8026 : {
8027 0 : mainlooppos = breaknewtcycle;
8028 0 : continue;
8029 : }
8030 : }
8031 0 : x = xt;
8032 0 : if( ae_fp_less(ae_fabs(d/x, _state),128.0*ae_machineepsilon) )
8033 : {
8034 0 : break;
8035 : }
8036 0 : i = i+1;
8037 0 : mainlooppos = newtcycle;
8038 0 : continue;
8039 : }
8040 : else
8041 : {
8042 0 : mainlooppos = breaknewtcycle;
8043 0 : continue;
8044 : }
8045 : }
8046 :
8047 : /*
8048 : * breaknewtcycle
8049 : */
8050 0 : if( mainlooppos==breaknewtcycle )
8051 : {
8052 0 : dithresh = 256.0*ae_machineepsilon;
8053 0 : mainlooppos = ihalve;
8054 0 : continue;
8055 : }
8056 : }
8057 :
8058 : /*
8059 : * done
8060 : */
8061 0 : if( rflg!=0 )
8062 : {
8063 0 : if( ae_fp_less_eq(x,ae_machineepsilon) )
8064 : {
8065 0 : x = 1.0-ae_machineepsilon;
8066 : }
8067 : else
8068 : {
8069 0 : x = 1.0-x;
8070 : }
8071 : }
8072 0 : result = x;
8073 0 : return result;
8074 : }
8075 :
8076 :
8077 : /*************************************************************************
8078 : Continued fraction expansion #1 for incomplete beta integral
8079 :
8080 : Cephes Math Library, Release 2.8: June, 2000
8081 : Copyright 1984, 1995, 2000 by Stephen L. Moshier
8082 : *************************************************************************/
8083 0 : static double ibetaf_incompletebetafe(double a,
8084 : double b,
8085 : double x,
8086 : double big,
8087 : double biginv,
8088 : ae_state *_state)
8089 : {
8090 : double xk;
8091 : double pk;
8092 : double pkm1;
8093 : double pkm2;
8094 : double qk;
8095 : double qkm1;
8096 : double qkm2;
8097 : double k1;
8098 : double k2;
8099 : double k3;
8100 : double k4;
8101 : double k5;
8102 : double k6;
8103 : double k7;
8104 : double k8;
8105 : double r;
8106 : double t;
8107 : double ans;
8108 : double thresh;
8109 : ae_int_t n;
8110 : double result;
8111 :
8112 :
8113 0 : k1 = a;
8114 0 : k2 = a+b;
8115 0 : k3 = a;
8116 0 : k4 = a+1.0;
8117 0 : k5 = 1.0;
8118 0 : k6 = b-1.0;
8119 0 : k7 = k4;
8120 0 : k8 = a+2.0;
8121 0 : pkm2 = 0.0;
8122 0 : qkm2 = 1.0;
8123 0 : pkm1 = 1.0;
8124 0 : qkm1 = 1.0;
8125 0 : ans = 1.0;
8126 0 : r = 1.0;
8127 0 : n = 0;
8128 0 : thresh = 3.0*ae_machineepsilon;
8129 : do
8130 : {
8131 0 : xk = -x*k1*k2/(k3*k4);
8132 0 : pk = pkm1+pkm2*xk;
8133 0 : qk = qkm1+qkm2*xk;
8134 0 : pkm2 = pkm1;
8135 0 : pkm1 = pk;
8136 0 : qkm2 = qkm1;
8137 0 : qkm1 = qk;
8138 0 : xk = x*k5*k6/(k7*k8);
8139 0 : pk = pkm1+pkm2*xk;
8140 0 : qk = qkm1+qkm2*xk;
8141 0 : pkm2 = pkm1;
8142 0 : pkm1 = pk;
8143 0 : qkm2 = qkm1;
8144 0 : qkm1 = qk;
8145 0 : if( ae_fp_neq(qk,(double)(0)) )
8146 : {
8147 0 : r = pk/qk;
8148 : }
8149 0 : if( ae_fp_neq(r,(double)(0)) )
8150 : {
8151 0 : t = ae_fabs((ans-r)/r, _state);
8152 0 : ans = r;
8153 : }
8154 : else
8155 : {
8156 0 : t = 1.0;
8157 : }
8158 0 : if( ae_fp_less(t,thresh) )
8159 : {
8160 0 : break;
8161 : }
8162 0 : k1 = k1+1.0;
8163 0 : k2 = k2+1.0;
8164 0 : k3 = k3+2.0;
8165 0 : k4 = k4+2.0;
8166 0 : k5 = k5+1.0;
8167 0 : k6 = k6-1.0;
8168 0 : k7 = k7+2.0;
8169 0 : k8 = k8+2.0;
8170 0 : if( ae_fp_greater(ae_fabs(qk, _state)+ae_fabs(pk, _state),big) )
8171 : {
8172 0 : pkm2 = pkm2*biginv;
8173 0 : pkm1 = pkm1*biginv;
8174 0 : qkm2 = qkm2*biginv;
8175 0 : qkm1 = qkm1*biginv;
8176 : }
8177 0 : if( ae_fp_less(ae_fabs(qk, _state),biginv)||ae_fp_less(ae_fabs(pk, _state),biginv) )
8178 : {
8179 0 : pkm2 = pkm2*big;
8180 0 : pkm1 = pkm1*big;
8181 0 : qkm2 = qkm2*big;
8182 0 : qkm1 = qkm1*big;
8183 : }
8184 0 : n = n+1;
8185 : }
8186 0 : while(n!=300);
8187 0 : result = ans;
8188 0 : return result;
8189 : }
8190 :
8191 :
8192 : /*************************************************************************
8193 : Continued fraction expansion #2
8194 : for incomplete beta integral
8195 :
8196 : Cephes Math Library, Release 2.8: June, 2000
8197 : Copyright 1984, 1995, 2000 by Stephen L. Moshier
8198 : *************************************************************************/
8199 0 : static double ibetaf_incompletebetafe2(double a,
8200 : double b,
8201 : double x,
8202 : double big,
8203 : double biginv,
8204 : ae_state *_state)
8205 : {
8206 : double xk;
8207 : double pk;
8208 : double pkm1;
8209 : double pkm2;
8210 : double qk;
8211 : double qkm1;
8212 : double qkm2;
8213 : double k1;
8214 : double k2;
8215 : double k3;
8216 : double k4;
8217 : double k5;
8218 : double k6;
8219 : double k7;
8220 : double k8;
8221 : double r;
8222 : double t;
8223 : double ans;
8224 : double z;
8225 : double thresh;
8226 : ae_int_t n;
8227 : double result;
8228 :
8229 :
8230 0 : k1 = a;
8231 0 : k2 = b-1.0;
8232 0 : k3 = a;
8233 0 : k4 = a+1.0;
8234 0 : k5 = 1.0;
8235 0 : k6 = a+b;
8236 0 : k7 = a+1.0;
8237 0 : k8 = a+2.0;
8238 0 : pkm2 = 0.0;
8239 0 : qkm2 = 1.0;
8240 0 : pkm1 = 1.0;
8241 0 : qkm1 = 1.0;
8242 0 : z = x/(1.0-x);
8243 0 : ans = 1.0;
8244 0 : r = 1.0;
8245 0 : n = 0;
8246 0 : thresh = 3.0*ae_machineepsilon;
8247 : do
8248 : {
8249 0 : xk = -z*k1*k2/(k3*k4);
8250 0 : pk = pkm1+pkm2*xk;
8251 0 : qk = qkm1+qkm2*xk;
8252 0 : pkm2 = pkm1;
8253 0 : pkm1 = pk;
8254 0 : qkm2 = qkm1;
8255 0 : qkm1 = qk;
8256 0 : xk = z*k5*k6/(k7*k8);
8257 0 : pk = pkm1+pkm2*xk;
8258 0 : qk = qkm1+qkm2*xk;
8259 0 : pkm2 = pkm1;
8260 0 : pkm1 = pk;
8261 0 : qkm2 = qkm1;
8262 0 : qkm1 = qk;
8263 0 : if( ae_fp_neq(qk,(double)(0)) )
8264 : {
8265 0 : r = pk/qk;
8266 : }
8267 0 : if( ae_fp_neq(r,(double)(0)) )
8268 : {
8269 0 : t = ae_fabs((ans-r)/r, _state);
8270 0 : ans = r;
8271 : }
8272 : else
8273 : {
8274 0 : t = 1.0;
8275 : }
8276 0 : if( ae_fp_less(t,thresh) )
8277 : {
8278 0 : break;
8279 : }
8280 0 : k1 = k1+1.0;
8281 0 : k2 = k2-1.0;
8282 0 : k3 = k3+2.0;
8283 0 : k4 = k4+2.0;
8284 0 : k5 = k5+1.0;
8285 0 : k6 = k6+1.0;
8286 0 : k7 = k7+2.0;
8287 0 : k8 = k8+2.0;
8288 0 : if( ae_fp_greater(ae_fabs(qk, _state)+ae_fabs(pk, _state),big) )
8289 : {
8290 0 : pkm2 = pkm2*biginv;
8291 0 : pkm1 = pkm1*biginv;
8292 0 : qkm2 = qkm2*biginv;
8293 0 : qkm1 = qkm1*biginv;
8294 : }
8295 0 : if( ae_fp_less(ae_fabs(qk, _state),biginv)||ae_fp_less(ae_fabs(pk, _state),biginv) )
8296 : {
8297 0 : pkm2 = pkm2*big;
8298 0 : pkm1 = pkm1*big;
8299 0 : qkm2 = qkm2*big;
8300 0 : qkm1 = qkm1*big;
8301 : }
8302 0 : n = n+1;
8303 : }
8304 0 : while(n!=300);
8305 0 : result = ans;
8306 0 : return result;
8307 : }
8308 :
8309 :
8310 : /*************************************************************************
8311 : Power series for incomplete beta integral.
8312 : Use when b*x is small and x not too close to 1.
8313 :
8314 : Cephes Math Library, Release 2.8: June, 2000
8315 : Copyright 1984, 1995, 2000 by Stephen L. Moshier
8316 : *************************************************************************/
8317 0 : static double ibetaf_incompletebetaps(double a,
8318 : double b,
8319 : double x,
8320 : double maxgam,
8321 : ae_state *_state)
8322 : {
8323 : double s;
8324 : double t;
8325 : double u;
8326 : double v;
8327 : double n;
8328 : double t1;
8329 : double z;
8330 : double ai;
8331 : double sg;
8332 : double result;
8333 :
8334 :
8335 0 : ai = 1.0/a;
8336 0 : u = (1.0-b)*x;
8337 0 : v = u/(a+1.0);
8338 0 : t1 = v;
8339 0 : t = u;
8340 0 : n = 2.0;
8341 0 : s = 0.0;
8342 0 : z = ae_machineepsilon*ai;
8343 0 : while(ae_fp_greater(ae_fabs(v, _state),z))
8344 : {
8345 0 : u = (n-b)*x/n;
8346 0 : t = t*u;
8347 0 : v = t/(a+n);
8348 0 : s = s+v;
8349 0 : n = n+1.0;
8350 : }
8351 0 : s = s+t1;
8352 0 : s = s+ai;
8353 0 : u = a*ae_log(x, _state);
8354 0 : if( ae_fp_less(a+b,maxgam)&&ae_fp_less(ae_fabs(u, _state),ae_log(ae_maxrealnumber, _state)) )
8355 : {
8356 0 : t = gammafunction(a+b, _state)/(gammafunction(a, _state)*gammafunction(b, _state));
8357 0 : s = s*t*ae_pow(x, a, _state);
8358 : }
8359 : else
8360 : {
8361 0 : t = lngamma(a+b, &sg, _state)-lngamma(a, &sg, _state)-lngamma(b, &sg, _state)+u+ae_log(s, _state);
8362 0 : if( ae_fp_less(t,ae_log(ae_minrealnumber, _state)) )
8363 : {
8364 0 : s = 0.0;
8365 : }
8366 : else
8367 : {
8368 0 : s = ae_exp(t, _state);
8369 : }
8370 : }
8371 0 : result = s;
8372 0 : return result;
8373 : }
8374 :
8375 :
8376 : #endif
8377 : #if defined(AE_COMPILE_FDISTR) || !defined(AE_PARTIAL_BUILD)
8378 :
8379 :
8380 : /*************************************************************************
8381 : F distribution
8382 :
8383 : Returns the area from zero to x under the F density
8384 : function (also known as Snedcor's density or the
8385 : variance ratio density). This is the density
8386 : of x = (u1/df1)/(u2/df2), where u1 and u2 are random
8387 : variables having Chi square distributions with df1
8388 : and df2 degrees of freedom, respectively.
8389 : The incomplete beta integral is used, according to the
8390 : formula
8391 :
8392 : P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
8393 :
8394 :
8395 : The arguments a and b are greater than zero, and x is
8396 : nonnegative.
8397 :
8398 : ACCURACY:
8399 :
8400 : Tested at random points (a,b,x).
8401 :
8402 : x a,b Relative error:
8403 : arithmetic domain domain # trials peak rms
8404 : IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
8405 : IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
8406 : IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
8407 : IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
8408 :
8409 : Cephes Math Library Release 2.8: June, 2000
8410 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8411 : *************************************************************************/
8412 0 : double fdistribution(ae_int_t a, ae_int_t b, double x, ae_state *_state)
8413 : {
8414 : double w;
8415 : double result;
8416 :
8417 :
8418 0 : ae_assert((a>=1&&b>=1)&&ae_fp_greater_eq(x,(double)(0)), "Domain error in FDistribution", _state);
8419 0 : w = a*x;
8420 0 : w = w/(b+w);
8421 0 : result = incompletebeta(0.5*a, 0.5*b, w, _state);
8422 0 : return result;
8423 : }
8424 :
8425 :
8426 : /*************************************************************************
8427 : Complemented F distribution
8428 :
8429 : Returns the area from x to infinity under the F density
8430 : function (also known as Snedcor's density or the
8431 : variance ratio density).
8432 :
8433 :
8434 : inf.
8435 : -
8436 : 1 | | a-1 b-1
8437 : 1-P(x) = ------ | t (1-t) dt
8438 : B(a,b) | |
8439 : -
8440 : x
8441 :
8442 :
8443 : The incomplete beta integral is used, according to the
8444 : formula
8445 :
8446 : P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
8447 :
8448 :
8449 : ACCURACY:
8450 :
8451 : Tested at random points (a,b,x) in the indicated intervals.
8452 : x a,b Relative error:
8453 : arithmetic domain domain # trials peak rms
8454 : IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
8455 : IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
8456 : IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
8457 : IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
8458 :
8459 : Cephes Math Library Release 2.8: June, 2000
8460 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8461 : *************************************************************************/
8462 0 : double fcdistribution(ae_int_t a, ae_int_t b, double x, ae_state *_state)
8463 : {
8464 : double w;
8465 : double result;
8466 :
8467 :
8468 0 : ae_assert((a>=1&&b>=1)&&ae_fp_greater_eq(x,(double)(0)), "Domain error in FCDistribution", _state);
8469 0 : w = b/(b+a*x);
8470 0 : result = incompletebeta(0.5*b, 0.5*a, w, _state);
8471 0 : return result;
8472 : }
8473 :
8474 :
8475 : /*************************************************************************
8476 : Inverse of complemented F distribution
8477 :
8478 : Finds the F density argument x such that the integral
8479 : from x to infinity of the F density is equal to the
8480 : given probability p.
8481 :
8482 : This is accomplished using the inverse beta integral
8483 : function and the relations
8484 :
8485 : z = incbi( df2/2, df1/2, p )
8486 : x = df2 (1-z) / (df1 z).
8487 :
8488 : Note: the following relations hold for the inverse of
8489 : the uncomplemented F distribution:
8490 :
8491 : z = incbi( df1/2, df2/2, p )
8492 : x = df2 z / (df1 (1-z)).
8493 :
8494 : ACCURACY:
8495 :
8496 : Tested at random points (a,b,p).
8497 :
8498 : a,b Relative error:
8499 : arithmetic domain # trials peak rms
8500 : For p between .001 and 1:
8501 : IEEE 1,100 100000 8.3e-15 4.7e-16
8502 : IEEE 1,10000 100000 2.1e-11 1.4e-13
8503 : For p between 10^-6 and 10^-3:
8504 : IEEE 1,100 50000 1.3e-12 8.4e-15
8505 : IEEE 1,10000 50000 3.0e-12 4.8e-14
8506 :
8507 : Cephes Math Library Release 2.8: June, 2000
8508 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8509 : *************************************************************************/
8510 0 : double invfdistribution(ae_int_t a,
8511 : ae_int_t b,
8512 : double y,
8513 : ae_state *_state)
8514 : {
8515 : double w;
8516 : double result;
8517 :
8518 :
8519 0 : ae_assert(((a>=1&&b>=1)&&ae_fp_greater(y,(double)(0)))&&ae_fp_less_eq(y,(double)(1)), "Domain error in InvFDistribution", _state);
8520 :
8521 : /*
8522 : * Compute probability for x = 0.5
8523 : */
8524 0 : w = incompletebeta(0.5*b, 0.5*a, 0.5, _state);
8525 :
8526 : /*
8527 : * If that is greater than y, then the solution w < .5
8528 : * Otherwise, solve at 1-y to remove cancellation in (b - b*w)
8529 : */
8530 0 : if( ae_fp_greater(w,y)||ae_fp_less(y,0.001) )
8531 : {
8532 0 : w = invincompletebeta(0.5*b, 0.5*a, y, _state);
8533 0 : result = (b-b*w)/(a*w);
8534 : }
8535 : else
8536 : {
8537 0 : w = invincompletebeta(0.5*a, 0.5*b, 1.0-y, _state);
8538 0 : result = b*w/(a*(1.0-w));
8539 : }
8540 0 : return result;
8541 : }
8542 :
8543 :
8544 : #endif
8545 : #if defined(AE_COMPILE_FRESNEL) || !defined(AE_PARTIAL_BUILD)
8546 :
8547 :
8548 : /*************************************************************************
8549 : Fresnel integral
8550 :
8551 : Evaluates the Fresnel integrals
8552 :
8553 : x
8554 : -
8555 : | |
8556 : C(x) = | cos(pi/2 t**2) dt,
8557 : | |
8558 : -
8559 : 0
8560 :
8561 : x
8562 : -
8563 : | |
8564 : S(x) = | sin(pi/2 t**2) dt.
8565 : | |
8566 : -
8567 : 0
8568 :
8569 :
8570 : The integrals are evaluated by a power series for x < 1.
8571 : For x >= 1 auxiliary functions f(x) and g(x) are employed
8572 : such that
8573 :
8574 : C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
8575 : S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
8576 :
8577 :
8578 :
8579 : ACCURACY:
8580 :
8581 : Relative error.
8582 :
8583 : Arithmetic function domain # trials peak rms
8584 : IEEE S(x) 0, 10 10000 2.0e-15 3.2e-16
8585 : IEEE C(x) 0, 10 10000 1.8e-15 3.3e-16
8586 :
8587 : Cephes Math Library Release 2.8: June, 2000
8588 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
8589 : *************************************************************************/
8590 0 : void fresnelintegral(double x, double* c, double* s, ae_state *_state)
8591 : {
8592 : double xxa;
8593 : double f;
8594 : double g;
8595 : double cc;
8596 : double ss;
8597 : double t;
8598 : double u;
8599 : double x2;
8600 : double sn;
8601 : double sd;
8602 : double cn;
8603 : double cd;
8604 : double fn;
8605 : double fd;
8606 : double gn;
8607 : double gd;
8608 : double mpi;
8609 : double mpio2;
8610 :
8611 :
8612 0 : mpi = 3.14159265358979323846;
8613 0 : mpio2 = 1.57079632679489661923;
8614 0 : xxa = x;
8615 0 : x = ae_fabs(xxa, _state);
8616 0 : x2 = x*x;
8617 0 : if( ae_fp_less(x2,2.5625) )
8618 : {
8619 0 : t = x2*x2;
8620 0 : sn = -2.99181919401019853726E3;
8621 0 : sn = sn*t+7.08840045257738576863E5;
8622 0 : sn = sn*t-6.29741486205862506537E7;
8623 0 : sn = sn*t+2.54890880573376359104E9;
8624 0 : sn = sn*t-4.42979518059697779103E10;
8625 0 : sn = sn*t+3.18016297876567817986E11;
8626 0 : sd = 1.00000000000000000000E0;
8627 0 : sd = sd*t+2.81376268889994315696E2;
8628 0 : sd = sd*t+4.55847810806532581675E4;
8629 0 : sd = sd*t+5.17343888770096400730E6;
8630 0 : sd = sd*t+4.19320245898111231129E8;
8631 0 : sd = sd*t+2.24411795645340920940E10;
8632 0 : sd = sd*t+6.07366389490084639049E11;
8633 0 : cn = -4.98843114573573548651E-8;
8634 0 : cn = cn*t+9.50428062829859605134E-6;
8635 0 : cn = cn*t-6.45191435683965050962E-4;
8636 0 : cn = cn*t+1.88843319396703850064E-2;
8637 0 : cn = cn*t-2.05525900955013891793E-1;
8638 0 : cn = cn*t+9.99999999999999998822E-1;
8639 0 : cd = 3.99982968972495980367E-12;
8640 0 : cd = cd*t+9.15439215774657478799E-10;
8641 0 : cd = cd*t+1.25001862479598821474E-7;
8642 0 : cd = cd*t+1.22262789024179030997E-5;
8643 0 : cd = cd*t+8.68029542941784300606E-4;
8644 0 : cd = cd*t+4.12142090722199792936E-2;
8645 0 : cd = cd*t+1.00000000000000000118E0;
8646 0 : *s = ae_sign(xxa, _state)*x*x2*sn/sd;
8647 0 : *c = ae_sign(xxa, _state)*x*cn/cd;
8648 0 : return;
8649 : }
8650 0 : if( ae_fp_greater(x,36974.0) )
8651 : {
8652 0 : *c = ae_sign(xxa, _state)*0.5;
8653 0 : *s = ae_sign(xxa, _state)*0.5;
8654 0 : return;
8655 : }
8656 0 : x2 = x*x;
8657 0 : t = mpi*x2;
8658 0 : u = 1/(t*t);
8659 0 : t = 1/t;
8660 0 : fn = 4.21543555043677546506E-1;
8661 0 : fn = fn*u+1.43407919780758885261E-1;
8662 0 : fn = fn*u+1.15220955073585758835E-2;
8663 0 : fn = fn*u+3.45017939782574027900E-4;
8664 0 : fn = fn*u+4.63613749287867322088E-6;
8665 0 : fn = fn*u+3.05568983790257605827E-8;
8666 0 : fn = fn*u+1.02304514164907233465E-10;
8667 0 : fn = fn*u+1.72010743268161828879E-13;
8668 0 : fn = fn*u+1.34283276233062758925E-16;
8669 0 : fn = fn*u+3.76329711269987889006E-20;
8670 0 : fd = 1.00000000000000000000E0;
8671 0 : fd = fd*u+7.51586398353378947175E-1;
8672 0 : fd = fd*u+1.16888925859191382142E-1;
8673 0 : fd = fd*u+6.44051526508858611005E-3;
8674 0 : fd = fd*u+1.55934409164153020873E-4;
8675 0 : fd = fd*u+1.84627567348930545870E-6;
8676 0 : fd = fd*u+1.12699224763999035261E-8;
8677 0 : fd = fd*u+3.60140029589371370404E-11;
8678 0 : fd = fd*u+5.88754533621578410010E-14;
8679 0 : fd = fd*u+4.52001434074129701496E-17;
8680 0 : fd = fd*u+1.25443237090011264384E-20;
8681 0 : gn = 5.04442073643383265887E-1;
8682 0 : gn = gn*u+1.97102833525523411709E-1;
8683 0 : gn = gn*u+1.87648584092575249293E-2;
8684 0 : gn = gn*u+6.84079380915393090172E-4;
8685 0 : gn = gn*u+1.15138826111884280931E-5;
8686 0 : gn = gn*u+9.82852443688422223854E-8;
8687 0 : gn = gn*u+4.45344415861750144738E-10;
8688 0 : gn = gn*u+1.08268041139020870318E-12;
8689 0 : gn = gn*u+1.37555460633261799868E-15;
8690 0 : gn = gn*u+8.36354435630677421531E-19;
8691 0 : gn = gn*u+1.86958710162783235106E-22;
8692 0 : gd = 1.00000000000000000000E0;
8693 0 : gd = gd*u+1.47495759925128324529E0;
8694 0 : gd = gd*u+3.37748989120019970451E-1;
8695 0 : gd = gd*u+2.53603741420338795122E-2;
8696 0 : gd = gd*u+8.14679107184306179049E-4;
8697 0 : gd = gd*u+1.27545075667729118702E-5;
8698 0 : gd = gd*u+1.04314589657571990585E-7;
8699 0 : gd = gd*u+4.60680728146520428211E-10;
8700 0 : gd = gd*u+1.10273215066240270757E-12;
8701 0 : gd = gd*u+1.38796531259578871258E-15;
8702 0 : gd = gd*u+8.39158816283118707363E-19;
8703 0 : gd = gd*u+1.86958710162783236342E-22;
8704 0 : f = 1-u*fn/fd;
8705 0 : g = t*gn/gd;
8706 0 : t = mpio2*x2;
8707 0 : cc = ae_cos(t, _state);
8708 0 : ss = ae_sin(t, _state);
8709 0 : t = mpi*x;
8710 0 : *c = 0.5+(f*ss-g*cc)/t;
8711 0 : *s = 0.5-(f*cc+g*ss)/t;
8712 0 : *c = *c*ae_sign(xxa, _state);
8713 0 : *s = *s*ae_sign(xxa, _state);
8714 : }
8715 :
8716 :
8717 : #endif
8718 : #if defined(AE_COMPILE_JACOBIANELLIPTIC) || !defined(AE_PARTIAL_BUILD)
8719 :
8720 :
8721 : /*************************************************************************
8722 : Jacobian Elliptic Functions
8723 :
8724 : Evaluates the Jacobian elliptic functions sn(u|m), cn(u|m),
8725 : and dn(u|m) of parameter m between 0 and 1, and real
8726 : argument u.
8727 :
8728 : These functions are periodic, with quarter-period on the
8729 : real axis equal to the complete elliptic integral
8730 : ellpk(1.0-m).
8731 :
8732 : Relation to incomplete elliptic integral:
8733 : If u = ellik(phi,m), then sn(u|m) = sin(phi),
8734 : and cn(u|m) = cos(phi). Phi is called the amplitude of u.
8735 :
8736 : Computation is by means of the arithmetic-geometric mean
8737 : algorithm, except when m is within 1e-9 of 0 or 1. In the
8738 : latter case with m close to 1, the approximation applies
8739 : only for phi < pi/2.
8740 :
8741 : ACCURACY:
8742 :
8743 : Tested at random points with u between 0 and 10, m between
8744 : 0 and 1.
8745 :
8746 : Absolute error (* = relative error):
8747 : arithmetic function # trials peak rms
8748 : IEEE phi 10000 9.2e-16* 1.4e-16*
8749 : IEEE sn 50000 4.1e-15 4.6e-16
8750 : IEEE cn 40000 3.6e-15 4.4e-16
8751 : IEEE dn 10000 1.3e-12 1.8e-14
8752 :
8753 : Peak error observed in consistency check using addition
8754 : theorem for sn(u+v) was 4e-16 (absolute). Also tested by
8755 : the above relation to the incomplete elliptic integral.
8756 : Accuracy deteriorates when u is large.
8757 :
8758 : Cephes Math Library Release 2.8: June, 2000
8759 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
8760 : *************************************************************************/
8761 0 : void jacobianellipticfunctions(double u,
8762 : double m,
8763 : double* sn,
8764 : double* cn,
8765 : double* dn,
8766 : double* ph,
8767 : ae_state *_state)
8768 : {
8769 : ae_frame _frame_block;
8770 : double ai;
8771 : double b;
8772 : double phi;
8773 : double t;
8774 : double twon;
8775 : ae_vector a;
8776 : ae_vector c;
8777 : ae_int_t i;
8778 :
8779 0 : ae_frame_make(_state, &_frame_block);
8780 0 : memset(&a, 0, sizeof(a));
8781 0 : memset(&c, 0, sizeof(c));
8782 0 : *sn = 0;
8783 0 : *cn = 0;
8784 0 : *dn = 0;
8785 0 : *ph = 0;
8786 0 : ae_vector_init(&a, 0, DT_REAL, _state, ae_true);
8787 0 : ae_vector_init(&c, 0, DT_REAL, _state, ae_true);
8788 :
8789 0 : ae_assert(ae_fp_greater_eq(m,(double)(0))&&ae_fp_less_eq(m,(double)(1)), "Domain error in JacobianEllipticFunctions: m<0 or m>1", _state);
8790 0 : ae_vector_set_length(&a, 8+1, _state);
8791 0 : ae_vector_set_length(&c, 8+1, _state);
8792 0 : if( ae_fp_less(m,1.0e-9) )
8793 : {
8794 0 : t = ae_sin(u, _state);
8795 0 : b = ae_cos(u, _state);
8796 0 : ai = 0.25*m*(u-t*b);
8797 0 : *sn = t-ai*b;
8798 0 : *cn = b+ai*t;
8799 0 : *ph = u-ai;
8800 0 : *dn = 1.0-0.5*m*t*t;
8801 0 : ae_frame_leave(_state);
8802 0 : return;
8803 : }
8804 0 : if( ae_fp_greater_eq(m,0.9999999999) )
8805 : {
8806 0 : ai = 0.25*(1.0-m);
8807 0 : b = ae_cosh(u, _state);
8808 0 : t = ae_tanh(u, _state);
8809 0 : phi = 1.0/b;
8810 0 : twon = b*ae_sinh(u, _state);
8811 0 : *sn = t+ai*(twon-u)/(b*b);
8812 0 : *ph = 2.0*ae_atan(ae_exp(u, _state), _state)-1.57079632679489661923+ai*(twon-u)/b;
8813 0 : ai = ai*t*phi;
8814 0 : *cn = phi-ai*(twon-u);
8815 0 : *dn = phi+ai*(twon+u);
8816 0 : ae_frame_leave(_state);
8817 0 : return;
8818 : }
8819 0 : a.ptr.p_double[0] = 1.0;
8820 0 : b = ae_sqrt(1.0-m, _state);
8821 0 : c.ptr.p_double[0] = ae_sqrt(m, _state);
8822 0 : twon = 1.0;
8823 0 : i = 0;
8824 0 : while(ae_fp_greater(ae_fabs(c.ptr.p_double[i]/a.ptr.p_double[i], _state),ae_machineepsilon))
8825 : {
8826 0 : if( i>7 )
8827 : {
8828 0 : ae_assert(ae_false, "Overflow in JacobianEllipticFunctions", _state);
8829 0 : break;
8830 : }
8831 0 : ai = a.ptr.p_double[i];
8832 0 : i = i+1;
8833 0 : c.ptr.p_double[i] = 0.5*(ai-b);
8834 0 : t = ae_sqrt(ai*b, _state);
8835 0 : a.ptr.p_double[i] = 0.5*(ai+b);
8836 0 : b = t;
8837 0 : twon = twon*2.0;
8838 : }
8839 0 : phi = twon*a.ptr.p_double[i]*u;
8840 : do
8841 : {
8842 0 : t = c.ptr.p_double[i]*ae_sin(phi, _state)/a.ptr.p_double[i];
8843 0 : b = phi;
8844 0 : phi = (ae_asin(t, _state)+phi)/2.0;
8845 0 : i = i-1;
8846 : }
8847 0 : while(i!=0);
8848 0 : *sn = ae_sin(phi, _state);
8849 0 : t = ae_cos(phi, _state);
8850 0 : *cn = t;
8851 0 : *dn = t/ae_cos(phi-b, _state);
8852 0 : *ph = phi;
8853 0 : ae_frame_leave(_state);
8854 : }
8855 :
8856 :
8857 : #endif
8858 : #if defined(AE_COMPILE_PSIF) || !defined(AE_PARTIAL_BUILD)
8859 :
8860 :
8861 : /*************************************************************************
8862 : Psi (digamma) function
8863 :
8864 : d -
8865 : psi(x) = -- ln | (x)
8866 : dx
8867 :
8868 : is the logarithmic derivative of the gamma function.
8869 : For integer x,
8870 : n-1
8871 : -
8872 : psi(n) = -EUL + > 1/k.
8873 : -
8874 : k=1
8875 :
8876 : This formula is used for 0 < n <= 10. If x is negative, it
8877 : is transformed to a positive argument by the reflection
8878 : formula psi(1-x) = psi(x) + pi cot(pi x).
8879 : For general positive x, the argument is made greater than 10
8880 : using the recurrence psi(x+1) = psi(x) + 1/x.
8881 : Then the following asymptotic expansion is applied:
8882 :
8883 : inf. B
8884 : - 2k
8885 : psi(x) = log(x) - 1/2x - > -------
8886 : - 2k
8887 : k=1 2k x
8888 :
8889 : where the B2k are Bernoulli numbers.
8890 :
8891 : ACCURACY:
8892 : Relative error (except absolute when |psi| < 1):
8893 : arithmetic domain # trials peak rms
8894 : IEEE 0,30 30000 1.3e-15 1.4e-16
8895 : IEEE -30,0 40000 1.5e-15 2.2e-16
8896 :
8897 : Cephes Math Library Release 2.8: June, 2000
8898 : Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier
8899 : *************************************************************************/
8900 0 : double psi(double x, ae_state *_state)
8901 : {
8902 : double p;
8903 : double q;
8904 : double nz;
8905 : double s;
8906 : double w;
8907 : double y;
8908 : double z;
8909 : double polv;
8910 : ae_int_t i;
8911 : ae_int_t n;
8912 : ae_int_t negative;
8913 : double result;
8914 :
8915 :
8916 0 : negative = 0;
8917 0 : nz = 0.0;
8918 0 : if( ae_fp_less_eq(x,(double)(0)) )
8919 : {
8920 0 : negative = 1;
8921 0 : q = x;
8922 0 : p = (double)(ae_ifloor(q, _state));
8923 0 : if( ae_fp_eq(p,q) )
8924 : {
8925 0 : ae_assert(ae_false, "Singularity in Psi(x)", _state);
8926 0 : result = ae_maxrealnumber;
8927 0 : return result;
8928 : }
8929 0 : nz = q-p;
8930 0 : if( ae_fp_neq(nz,0.5) )
8931 : {
8932 0 : if( ae_fp_greater(nz,0.5) )
8933 : {
8934 0 : p = p+1.0;
8935 0 : nz = q-p;
8936 : }
8937 0 : nz = ae_pi/ae_tan(ae_pi*nz, _state);
8938 : }
8939 : else
8940 : {
8941 0 : nz = 0.0;
8942 : }
8943 0 : x = 1.0-x;
8944 : }
8945 0 : if( ae_fp_less_eq(x,10.0)&&ae_fp_eq(x,(double)(ae_ifloor(x, _state))) )
8946 : {
8947 0 : y = 0.0;
8948 0 : n = ae_ifloor(x, _state);
8949 0 : for(i=1; i<=n-1; i++)
8950 : {
8951 0 : w = (double)(i);
8952 0 : y = y+1.0/w;
8953 : }
8954 0 : y = y-0.57721566490153286061;
8955 : }
8956 : else
8957 : {
8958 0 : s = x;
8959 0 : w = 0.0;
8960 0 : while(ae_fp_less(s,10.0))
8961 : {
8962 0 : w = w+1.0/s;
8963 0 : s = s+1.0;
8964 : }
8965 0 : if( ae_fp_less(s,1.0E17) )
8966 : {
8967 0 : z = 1.0/(s*s);
8968 0 : polv = 8.33333333333333333333E-2;
8969 0 : polv = polv*z-2.10927960927960927961E-2;
8970 0 : polv = polv*z+7.57575757575757575758E-3;
8971 0 : polv = polv*z-4.16666666666666666667E-3;
8972 0 : polv = polv*z+3.96825396825396825397E-3;
8973 0 : polv = polv*z-8.33333333333333333333E-3;
8974 0 : polv = polv*z+8.33333333333333333333E-2;
8975 0 : y = z*polv;
8976 : }
8977 : else
8978 : {
8979 0 : y = 0.0;
8980 : }
8981 0 : y = ae_log(s, _state)-0.5/s-y-w;
8982 : }
8983 0 : if( negative!=0 )
8984 : {
8985 0 : y = y-nz;
8986 : }
8987 0 : result = y;
8988 0 : return result;
8989 : }
8990 :
8991 :
8992 : #endif
8993 : #if defined(AE_COMPILE_EXPINTEGRALS) || !defined(AE_PARTIAL_BUILD)
8994 :
8995 :
8996 : /*************************************************************************
8997 : Exponential integral Ei(x)
8998 :
8999 : x
9000 : - t
9001 : | | e
9002 : Ei(x) = -|- --- dt .
9003 : | | t
9004 : -
9005 : -inf
9006 :
9007 : Not defined for x <= 0.
9008 : See also expn.c.
9009 :
9010 :
9011 :
9012 : ACCURACY:
9013 :
9014 : Relative error:
9015 : arithmetic domain # trials peak rms
9016 : IEEE 0,100 50000 8.6e-16 1.3e-16
9017 :
9018 : Cephes Math Library Release 2.8: May, 1999
9019 : Copyright 1999 by Stephen L. Moshier
9020 : *************************************************************************/
9021 0 : double exponentialintegralei(double x, ae_state *_state)
9022 : {
9023 : double eul;
9024 : double f;
9025 : double f1;
9026 : double f2;
9027 : double w;
9028 : double result;
9029 :
9030 :
9031 0 : eul = 0.5772156649015328606065;
9032 0 : if( ae_fp_less_eq(x,(double)(0)) )
9033 : {
9034 0 : result = (double)(0);
9035 0 : return result;
9036 : }
9037 0 : if( ae_fp_less(x,(double)(2)) )
9038 : {
9039 0 : f1 = -5.350447357812542947283;
9040 0 : f1 = f1*x+218.5049168816613393830;
9041 0 : f1 = f1*x-4176.572384826693777058;
9042 0 : f1 = f1*x+55411.76756393557601232;
9043 0 : f1 = f1*x-331338.1331178144034309;
9044 0 : f1 = f1*x+1592627.163384945414220;
9045 0 : f2 = 1.000000000000000000000;
9046 0 : f2 = f2*x-52.50547959112862969197;
9047 0 : f2 = f2*x+1259.616186786790571525;
9048 0 : f2 = f2*x-17565.49581973534652631;
9049 0 : f2 = f2*x+149306.2117002725991967;
9050 0 : f2 = f2*x-729494.9239640527645655;
9051 0 : f2 = f2*x+1592627.163384945429726;
9052 0 : f = f1/f2;
9053 0 : result = eul+ae_log(x, _state)+x*f;
9054 0 : return result;
9055 : }
9056 0 : if( ae_fp_less(x,(double)(4)) )
9057 : {
9058 0 : w = 1/x;
9059 0 : f1 = 1.981808503259689673238E-2;
9060 0 : f1 = f1*w-1.271645625984917501326;
9061 0 : f1 = f1*w-2.088160335681228318920;
9062 0 : f1 = f1*w+2.755544509187936721172;
9063 0 : f1 = f1*w-4.409507048701600257171E-1;
9064 0 : f1 = f1*w+4.665623805935891391017E-2;
9065 0 : f1 = f1*w-1.545042679673485262580E-3;
9066 0 : f1 = f1*w+7.059980605299617478514E-5;
9067 0 : f2 = 1.000000000000000000000;
9068 0 : f2 = f2*w+1.476498670914921440652;
9069 0 : f2 = f2*w+5.629177174822436244827E-1;
9070 0 : f2 = f2*w+1.699017897879307263248E-1;
9071 0 : f2 = f2*w+2.291647179034212017463E-2;
9072 0 : f2 = f2*w+4.450150439728752875043E-3;
9073 0 : f2 = f2*w+1.727439612206521482874E-4;
9074 0 : f2 = f2*w+3.953167195549672482304E-5;
9075 0 : f = f1/f2;
9076 0 : result = ae_exp(x, _state)*w*(1+w*f);
9077 0 : return result;
9078 : }
9079 0 : if( ae_fp_less(x,(double)(8)) )
9080 : {
9081 0 : w = 1/x;
9082 0 : f1 = -1.373215375871208729803;
9083 0 : f1 = f1*w-7.084559133740838761406E-1;
9084 0 : f1 = f1*w+1.580806855547941010501;
9085 0 : f1 = f1*w-2.601500427425622944234E-1;
9086 0 : f1 = f1*w+2.994674694113713763365E-2;
9087 0 : f1 = f1*w-1.038086040188744005513E-3;
9088 0 : f1 = f1*w+4.371064420753005429514E-5;
9089 0 : f1 = f1*w+2.141783679522602903795E-6;
9090 0 : f2 = 1.000000000000000000000;
9091 0 : f2 = f2*w+8.585231423622028380768E-1;
9092 0 : f2 = f2*w+4.483285822873995129957E-1;
9093 0 : f2 = f2*w+7.687932158124475434091E-2;
9094 0 : f2 = f2*w+2.449868241021887685904E-2;
9095 0 : f2 = f2*w+8.832165941927796567926E-4;
9096 0 : f2 = f2*w+4.590952299511353531215E-4;
9097 0 : f2 = f2*w+(-4.729848351866523044863E-6);
9098 0 : f2 = f2*w+2.665195537390710170105E-6;
9099 0 : f = f1/f2;
9100 0 : result = ae_exp(x, _state)*w*(1+w*f);
9101 0 : return result;
9102 : }
9103 0 : if( ae_fp_less(x,(double)(16)) )
9104 : {
9105 0 : w = 1/x;
9106 0 : f1 = -2.106934601691916512584;
9107 0 : f1 = f1*w+1.732733869664688041885;
9108 0 : f1 = f1*w-2.423619178935841904839E-1;
9109 0 : f1 = f1*w+2.322724180937565842585E-2;
9110 0 : f1 = f1*w+2.372880440493179832059E-4;
9111 0 : f1 = f1*w-8.343219561192552752335E-5;
9112 0 : f1 = f1*w+1.363408795605250394881E-5;
9113 0 : f1 = f1*w-3.655412321999253963714E-7;
9114 0 : f1 = f1*w+1.464941733975961318456E-8;
9115 0 : f1 = f1*w+6.176407863710360207074E-10;
9116 0 : f2 = 1.000000000000000000000;
9117 0 : f2 = f2*w-2.298062239901678075778E-1;
9118 0 : f2 = f2*w+1.105077041474037862347E-1;
9119 0 : f2 = f2*w-1.566542966630792353556E-2;
9120 0 : f2 = f2*w+2.761106850817352773874E-3;
9121 0 : f2 = f2*w-2.089148012284048449115E-4;
9122 0 : f2 = f2*w+1.708528938807675304186E-5;
9123 0 : f2 = f2*w-4.459311796356686423199E-7;
9124 0 : f2 = f2*w+1.394634930353847498145E-8;
9125 0 : f2 = f2*w+6.150865933977338354138E-10;
9126 0 : f = f1/f2;
9127 0 : result = ae_exp(x, _state)*w*(1+w*f);
9128 0 : return result;
9129 : }
9130 0 : if( ae_fp_less(x,(double)(32)) )
9131 : {
9132 0 : w = 1/x;
9133 0 : f1 = -2.458119367674020323359E-1;
9134 0 : f1 = f1*w-1.483382253322077687183E-1;
9135 0 : f1 = f1*w+7.248291795735551591813E-2;
9136 0 : f1 = f1*w-1.348315687380940523823E-2;
9137 0 : f1 = f1*w+1.342775069788636972294E-3;
9138 0 : f1 = f1*w-7.942465637159712264564E-5;
9139 0 : f1 = f1*w+2.644179518984235952241E-6;
9140 0 : f1 = f1*w-4.239473659313765177195E-8;
9141 0 : f2 = 1.000000000000000000000;
9142 0 : f2 = f2*w-1.044225908443871106315E-1;
9143 0 : f2 = f2*w-2.676453128101402655055E-1;
9144 0 : f2 = f2*w+9.695000254621984627876E-2;
9145 0 : f2 = f2*w-1.601745692712991078208E-2;
9146 0 : f2 = f2*w+1.496414899205908021882E-3;
9147 0 : f2 = f2*w-8.462452563778485013756E-5;
9148 0 : f2 = f2*w+2.728938403476726394024E-6;
9149 0 : f2 = f2*w-4.239462431819542051337E-8;
9150 0 : f = f1/f2;
9151 0 : result = ae_exp(x, _state)*w*(1+w*f);
9152 0 : return result;
9153 : }
9154 0 : if( ae_fp_less(x,(double)(64)) )
9155 : {
9156 0 : w = 1/x;
9157 0 : f1 = 1.212561118105456670844E-1;
9158 0 : f1 = f1*w-5.823133179043894485122E-1;
9159 0 : f1 = f1*w+2.348887314557016779211E-1;
9160 0 : f1 = f1*w-3.040034318113248237280E-2;
9161 0 : f1 = f1*w+1.510082146865190661777E-3;
9162 0 : f1 = f1*w-2.523137095499571377122E-5;
9163 0 : f2 = 1.000000000000000000000;
9164 0 : f2 = f2*w-1.002252150365854016662;
9165 0 : f2 = f2*w+2.928709694872224144953E-1;
9166 0 : f2 = f2*w-3.337004338674007801307E-2;
9167 0 : f2 = f2*w+1.560544881127388842819E-3;
9168 0 : f2 = f2*w-2.523137093603234562648E-5;
9169 0 : f = f1/f2;
9170 0 : result = ae_exp(x, _state)*w*(1+w*f);
9171 0 : return result;
9172 : }
9173 0 : w = 1/x;
9174 0 : f1 = -7.657847078286127362028E-1;
9175 0 : f1 = f1*w+6.886192415566705051750E-1;
9176 0 : f1 = f1*w-2.132598113545206124553E-1;
9177 0 : f1 = f1*w+3.346107552384193813594E-2;
9178 0 : f1 = f1*w-3.076541477344756050249E-3;
9179 0 : f1 = f1*w+1.747119316454907477380E-4;
9180 0 : f1 = f1*w-6.103711682274170530369E-6;
9181 0 : f1 = f1*w+1.218032765428652199087E-7;
9182 0 : f1 = f1*w-1.086076102793290233007E-9;
9183 0 : f2 = 1.000000000000000000000;
9184 0 : f2 = f2*w-1.888802868662308731041;
9185 0 : f2 = f2*w+1.066691687211408896850;
9186 0 : f2 = f2*w-2.751915982306380647738E-1;
9187 0 : f2 = f2*w+3.930852688233823569726E-2;
9188 0 : f2 = f2*w-3.414684558602365085394E-3;
9189 0 : f2 = f2*w+1.866844370703555398195E-4;
9190 0 : f2 = f2*w-6.345146083130515357861E-6;
9191 0 : f2 = f2*w+1.239754287483206878024E-7;
9192 0 : f2 = f2*w-1.086076102793126632978E-9;
9193 0 : f = f1/f2;
9194 0 : result = ae_exp(x, _state)*w*(1+w*f);
9195 0 : return result;
9196 : }
9197 :
9198 :
9199 : /*************************************************************************
9200 : Exponential integral En(x)
9201 :
9202 : Evaluates the exponential integral
9203 :
9204 : inf.
9205 : -
9206 : | | -xt
9207 : | e
9208 : E (x) = | ---- dt.
9209 : n | n
9210 : | | t
9211 : -
9212 : 1
9213 :
9214 :
9215 : Both n and x must be nonnegative.
9216 :
9217 : The routine employs either a power series, a continued
9218 : fraction, or an asymptotic formula depending on the
9219 : relative values of n and x.
9220 :
9221 : ACCURACY:
9222 :
9223 : Relative error:
9224 : arithmetic domain # trials peak rms
9225 : IEEE 0, 30 10000 1.7e-15 3.6e-16
9226 :
9227 : Cephes Math Library Release 2.8: June, 2000
9228 : Copyright 1985, 2000 by Stephen L. Moshier
9229 : *************************************************************************/
9230 0 : double exponentialintegralen(double x, ae_int_t n, ae_state *_state)
9231 : {
9232 : double r;
9233 : double t;
9234 : double yk;
9235 : double xk;
9236 : double pk;
9237 : double pkm1;
9238 : double pkm2;
9239 : double qk;
9240 : double qkm1;
9241 : double qkm2;
9242 : double psi;
9243 : double z;
9244 : ae_int_t i;
9245 : ae_int_t k;
9246 : double big;
9247 : double eul;
9248 : double result;
9249 :
9250 :
9251 0 : eul = 0.57721566490153286060;
9252 0 : big = 1.44115188075855872*ae_pow((double)(10), (double)(17), _state);
9253 0 : if( ((n<0||ae_fp_less(x,(double)(0)))||ae_fp_greater(x,(double)(170)))||(ae_fp_eq(x,(double)(0))&&n<2) )
9254 : {
9255 0 : result = (double)(-1);
9256 0 : return result;
9257 : }
9258 0 : if( ae_fp_eq(x,(double)(0)) )
9259 : {
9260 0 : result = (double)1/(double)(n-1);
9261 0 : return result;
9262 : }
9263 0 : if( n==0 )
9264 : {
9265 0 : result = ae_exp(-x, _state)/x;
9266 0 : return result;
9267 : }
9268 0 : if( n>5000 )
9269 : {
9270 0 : xk = x+n;
9271 0 : yk = 1/(xk*xk);
9272 0 : t = (double)(n);
9273 0 : result = yk*t*(6*x*x-8*t*x+t*t);
9274 0 : result = yk*(result+t*(t-2.0*x));
9275 0 : result = yk*(result+t);
9276 0 : result = (result+1)*ae_exp(-x, _state)/xk;
9277 0 : return result;
9278 : }
9279 0 : if( ae_fp_less_eq(x,(double)(1)) )
9280 : {
9281 0 : psi = -eul-ae_log(x, _state);
9282 0 : for(i=1; i<=n-1; i++)
9283 : {
9284 0 : psi = psi+(double)1/(double)i;
9285 : }
9286 0 : z = -x;
9287 0 : xk = (double)(0);
9288 0 : yk = (double)(1);
9289 0 : pk = (double)(1-n);
9290 0 : if( n==1 )
9291 : {
9292 0 : result = 0.0;
9293 : }
9294 : else
9295 : {
9296 0 : result = 1.0/pk;
9297 : }
9298 : do
9299 : {
9300 0 : xk = xk+1;
9301 0 : yk = yk*z/xk;
9302 0 : pk = pk+1;
9303 0 : if( ae_fp_neq(pk,(double)(0)) )
9304 : {
9305 0 : result = result+yk/pk;
9306 : }
9307 0 : if( ae_fp_neq(result,(double)(0)) )
9308 : {
9309 0 : t = ae_fabs(yk/result, _state);
9310 : }
9311 : else
9312 : {
9313 0 : t = (double)(1);
9314 : }
9315 : }
9316 0 : while(ae_fp_greater_eq(t,ae_machineepsilon));
9317 0 : t = (double)(1);
9318 0 : for(i=1; i<=n-1; i++)
9319 : {
9320 0 : t = t*z/i;
9321 : }
9322 0 : result = psi*t-result;
9323 0 : return result;
9324 : }
9325 : else
9326 : {
9327 0 : k = 1;
9328 0 : pkm2 = (double)(1);
9329 0 : qkm2 = x;
9330 0 : pkm1 = 1.0;
9331 0 : qkm1 = x+n;
9332 0 : result = pkm1/qkm1;
9333 : do
9334 : {
9335 0 : k = k+1;
9336 0 : if( k%2==1 )
9337 : {
9338 0 : yk = (double)(1);
9339 0 : xk = n+(double)(k-1)/(double)2;
9340 : }
9341 : else
9342 : {
9343 0 : yk = x;
9344 0 : xk = (double)k/(double)2;
9345 : }
9346 0 : pk = pkm1*yk+pkm2*xk;
9347 0 : qk = qkm1*yk+qkm2*xk;
9348 0 : if( ae_fp_neq(qk,(double)(0)) )
9349 : {
9350 0 : r = pk/qk;
9351 0 : t = ae_fabs((result-r)/r, _state);
9352 0 : result = r;
9353 : }
9354 : else
9355 : {
9356 0 : t = (double)(1);
9357 : }
9358 0 : pkm2 = pkm1;
9359 0 : pkm1 = pk;
9360 0 : qkm2 = qkm1;
9361 0 : qkm1 = qk;
9362 0 : if( ae_fp_greater(ae_fabs(pk, _state),big) )
9363 : {
9364 0 : pkm2 = pkm2/big;
9365 0 : pkm1 = pkm1/big;
9366 0 : qkm2 = qkm2/big;
9367 0 : qkm1 = qkm1/big;
9368 : }
9369 : }
9370 0 : while(ae_fp_greater_eq(t,ae_machineepsilon));
9371 0 : result = result*ae_exp(-x, _state);
9372 : }
9373 0 : return result;
9374 : }
9375 :
9376 :
9377 : #endif
9378 : #if defined(AE_COMPILE_LAGUERRE) || !defined(AE_PARTIAL_BUILD)
9379 :
9380 :
9381 : /*************************************************************************
9382 : Calculation of the value of the Laguerre polynomial.
9383 :
9384 : Parameters:
9385 : n - degree, n>=0
9386 : x - argument
9387 :
9388 : Result:
9389 : the value of the Laguerre polynomial Ln at x
9390 : *************************************************************************/
9391 0 : double laguerrecalculate(ae_int_t n, double x, ae_state *_state)
9392 : {
9393 : double a;
9394 : double b;
9395 : double i;
9396 : double result;
9397 :
9398 :
9399 0 : result = (double)(1);
9400 0 : a = (double)(1);
9401 0 : b = 1-x;
9402 0 : if( n==1 )
9403 : {
9404 0 : result = b;
9405 : }
9406 0 : i = (double)(2);
9407 0 : while(ae_fp_less_eq(i,(double)(n)))
9408 : {
9409 0 : result = ((2*i-1-x)*b-(i-1)*a)/i;
9410 0 : a = b;
9411 0 : b = result;
9412 0 : i = i+1;
9413 : }
9414 0 : return result;
9415 : }
9416 :
9417 :
9418 : /*************************************************************************
9419 : Summation of Laguerre polynomials using Clenshaw's recurrence formula.
9420 :
9421 : This routine calculates c[0]*L0(x) + c[1]*L1(x) + ... + c[N]*LN(x)
9422 :
9423 : Parameters:
9424 : n - degree, n>=0
9425 : x - argument
9426 :
9427 : Result:
9428 : the value of the Laguerre polynomial at x
9429 : *************************************************************************/
9430 0 : double laguerresum(/* Real */ ae_vector* c,
9431 : ae_int_t n,
9432 : double x,
9433 : ae_state *_state)
9434 : {
9435 : double b1;
9436 : double b2;
9437 : ae_int_t i;
9438 : double result;
9439 :
9440 :
9441 0 : b1 = (double)(0);
9442 0 : b2 = (double)(0);
9443 0 : result = (double)(0);
9444 0 : for(i=n; i>=0; i--)
9445 : {
9446 0 : result = (2*i+1-x)*b1/(i+1)-(i+1)*b2/(i+2)+c->ptr.p_double[i];
9447 0 : b2 = b1;
9448 0 : b1 = result;
9449 : }
9450 0 : return result;
9451 : }
9452 :
9453 :
9454 : /*************************************************************************
9455 : Representation of Ln as C[0] + C[1]*X + ... + C[N]*X^N
9456 :
9457 : Input parameters:
9458 : N - polynomial degree, n>=0
9459 :
9460 : Output parameters:
9461 : C - coefficients
9462 : *************************************************************************/
9463 0 : void laguerrecoefficients(ae_int_t n,
9464 : /* Real */ ae_vector* c,
9465 : ae_state *_state)
9466 : {
9467 : ae_int_t i;
9468 :
9469 0 : ae_vector_clear(c);
9470 :
9471 0 : ae_vector_set_length(c, n+1, _state);
9472 0 : c->ptr.p_double[0] = (double)(1);
9473 0 : for(i=0; i<=n-1; i++)
9474 : {
9475 0 : c->ptr.p_double[i+1] = -c->ptr.p_double[i]*(n-i)/(i+1)/(i+1);
9476 : }
9477 0 : }
9478 :
9479 :
9480 : #endif
9481 : #if defined(AE_COMPILE_CHISQUAREDISTR) || !defined(AE_PARTIAL_BUILD)
9482 :
9483 :
9484 : /*************************************************************************
9485 : Chi-square distribution
9486 :
9487 : Returns the area under the left hand tail (from 0 to x)
9488 : of the Chi square probability density function with
9489 : v degrees of freedom.
9490 :
9491 :
9492 : x
9493 : -
9494 : 1 | | v/2-1 -t/2
9495 : P( x | v ) = ----------- | t e dt
9496 : v/2 - | |
9497 : 2 | (v/2) -
9498 : 0
9499 :
9500 : where x is the Chi-square variable.
9501 :
9502 : The incomplete gamma integral is used, according to the
9503 : formula
9504 :
9505 : y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
9506 :
9507 : The arguments must both be positive.
9508 :
9509 : ACCURACY:
9510 :
9511 : See incomplete gamma function
9512 :
9513 :
9514 : Cephes Math Library Release 2.8: June, 2000
9515 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
9516 : *************************************************************************/
9517 0 : double chisquaredistribution(double v, double x, ae_state *_state)
9518 : {
9519 : double result;
9520 :
9521 :
9522 0 : ae_assert(ae_fp_greater_eq(x,(double)(0))&&ae_fp_greater_eq(v,(double)(1)), "Domain error in ChiSquareDistribution", _state);
9523 0 : result = incompletegamma(v/2.0, x/2.0, _state);
9524 0 : return result;
9525 : }
9526 :
9527 :
9528 : /*************************************************************************
9529 : Complemented Chi-square distribution
9530 :
9531 : Returns the area under the right hand tail (from x to
9532 : infinity) of the Chi square probability density function
9533 : with v degrees of freedom:
9534 :
9535 : inf.
9536 : -
9537 : 1 | | v/2-1 -t/2
9538 : P( x | v ) = ----------- | t e dt
9539 : v/2 - | |
9540 : 2 | (v/2) -
9541 : x
9542 :
9543 : where x is the Chi-square variable.
9544 :
9545 : The incomplete gamma integral is used, according to the
9546 : formula
9547 :
9548 : y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
9549 :
9550 : The arguments must both be positive.
9551 :
9552 : ACCURACY:
9553 :
9554 : See incomplete gamma function
9555 :
9556 : Cephes Math Library Release 2.8: June, 2000
9557 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
9558 : *************************************************************************/
9559 0 : double chisquarecdistribution(double v, double x, ae_state *_state)
9560 : {
9561 : double result;
9562 :
9563 :
9564 0 : ae_assert(ae_fp_greater_eq(x,(double)(0))&&ae_fp_greater_eq(v,(double)(1)), "Domain error in ChiSquareDistributionC", _state);
9565 0 : result = incompletegammac(v/2.0, x/2.0, _state);
9566 0 : return result;
9567 : }
9568 :
9569 :
9570 : /*************************************************************************
9571 : Inverse of complemented Chi-square distribution
9572 :
9573 : Finds the Chi-square argument x such that the integral
9574 : from x to infinity of the Chi-square density is equal
9575 : to the given cumulative probability y.
9576 :
9577 : This is accomplished using the inverse gamma integral
9578 : function and the relation
9579 :
9580 : x/2 = igami( df/2, y );
9581 :
9582 : ACCURACY:
9583 :
9584 : See inverse incomplete gamma function
9585 :
9586 :
9587 : Cephes Math Library Release 2.8: June, 2000
9588 : Copyright 1984, 1987, 2000 by Stephen L. Moshier
9589 : *************************************************************************/
9590 0 : double invchisquaredistribution(double v, double y, ae_state *_state)
9591 : {
9592 : double result;
9593 :
9594 :
9595 0 : ae_assert((ae_fp_greater_eq(y,(double)(0))&&ae_fp_less_eq(y,(double)(1)))&&ae_fp_greater_eq(v,(double)(1)), "Domain error in InvChiSquareDistribution", _state);
9596 0 : result = 2*invincompletegammac(0.5*v, y, _state);
9597 0 : return result;
9598 : }
9599 :
9600 :
9601 : #endif
9602 : #if defined(AE_COMPILE_LEGENDRE) || !defined(AE_PARTIAL_BUILD)
9603 :
9604 :
9605 : /*************************************************************************
9606 : Calculation of the value of the Legendre polynomial Pn.
9607 :
9608 : Parameters:
9609 : n - degree, n>=0
9610 : x - argument
9611 :
9612 : Result:
9613 : the value of the Legendre polynomial Pn at x
9614 : *************************************************************************/
9615 0 : double legendrecalculate(ae_int_t n, double x, ae_state *_state)
9616 : {
9617 : double a;
9618 : double b;
9619 : ae_int_t i;
9620 : double result;
9621 :
9622 :
9623 0 : result = (double)(1);
9624 0 : a = (double)(1);
9625 0 : b = x;
9626 0 : if( n==0 )
9627 : {
9628 0 : result = a;
9629 0 : return result;
9630 : }
9631 0 : if( n==1 )
9632 : {
9633 0 : result = b;
9634 0 : return result;
9635 : }
9636 0 : for(i=2; i<=n; i++)
9637 : {
9638 0 : result = ((2*i-1)*x*b-(i-1)*a)/i;
9639 0 : a = b;
9640 0 : b = result;
9641 : }
9642 0 : return result;
9643 : }
9644 :
9645 :
9646 : /*************************************************************************
9647 : Summation of Legendre polynomials using Clenshaw's recurrence formula.
9648 :
9649 : This routine calculates
9650 : c[0]*P0(x) + c[1]*P1(x) + ... + c[N]*PN(x)
9651 :
9652 : Parameters:
9653 : n - degree, n>=0
9654 : x - argument
9655 :
9656 : Result:
9657 : the value of the Legendre polynomial at x
9658 : *************************************************************************/
9659 0 : double legendresum(/* Real */ ae_vector* c,
9660 : ae_int_t n,
9661 : double x,
9662 : ae_state *_state)
9663 : {
9664 : double b1;
9665 : double b2;
9666 : ae_int_t i;
9667 : double result;
9668 :
9669 :
9670 0 : b1 = (double)(0);
9671 0 : b2 = (double)(0);
9672 0 : result = (double)(0);
9673 0 : for(i=n; i>=0; i--)
9674 : {
9675 0 : result = (2*i+1)*x*b1/(i+1)-(i+1)*b2/(i+2)+c->ptr.p_double[i];
9676 0 : b2 = b1;
9677 0 : b1 = result;
9678 : }
9679 0 : return result;
9680 : }
9681 :
9682 :
9683 : /*************************************************************************
9684 : Representation of Pn as C[0] + C[1]*X + ... + C[N]*X^N
9685 :
9686 : Input parameters:
9687 : N - polynomial degree, n>=0
9688 :
9689 : Output parameters:
9690 : C - coefficients
9691 : *************************************************************************/
9692 0 : void legendrecoefficients(ae_int_t n,
9693 : /* Real */ ae_vector* c,
9694 : ae_state *_state)
9695 : {
9696 : ae_int_t i;
9697 :
9698 0 : ae_vector_clear(c);
9699 :
9700 0 : ae_vector_set_length(c, n+1, _state);
9701 0 : for(i=0; i<=n; i++)
9702 : {
9703 0 : c->ptr.p_double[i] = (double)(0);
9704 : }
9705 0 : c->ptr.p_double[n] = (double)(1);
9706 0 : for(i=1; i<=n; i++)
9707 : {
9708 0 : c->ptr.p_double[n] = c->ptr.p_double[n]*(n+i)/2/i;
9709 : }
9710 0 : for(i=0; i<=n/2-1; i++)
9711 : {
9712 0 : c->ptr.p_double[n-2*(i+1)] = -c->ptr.p_double[n-2*i]*(n-2*i)*(n-2*i-1)/2/(i+1)/(2*(n-i)-1);
9713 : }
9714 0 : }
9715 :
9716 :
9717 : #endif
9718 : #if defined(AE_COMPILE_BETAF) || !defined(AE_PARTIAL_BUILD)
9719 :
9720 :
9721 : /*************************************************************************
9722 : Beta function
9723 :
9724 :
9725 : - -
9726 : | (a) | (b)
9727 : beta( a, b ) = -----------.
9728 : -
9729 : | (a+b)
9730 :
9731 : For large arguments the logarithm of the function is
9732 : evaluated using lgam(), then exponentiated.
9733 :
9734 : ACCURACY:
9735 :
9736 : Relative error:
9737 : arithmetic domain # trials peak rms
9738 : IEEE 0,30 30000 8.1e-14 1.1e-14
9739 :
9740 : Cephes Math Library Release 2.0: April, 1987
9741 : Copyright 1984, 1987 by Stephen L. Moshier
9742 : *************************************************************************/
9743 0 : double beta(double a, double b, ae_state *_state)
9744 : {
9745 : double y;
9746 : double sg;
9747 : double s;
9748 : double result;
9749 :
9750 :
9751 0 : sg = (double)(1);
9752 0 : ae_assert(ae_fp_greater(a,(double)(0))||ae_fp_neq(a,(double)(ae_ifloor(a, _state))), "Overflow in Beta", _state);
9753 0 : ae_assert(ae_fp_greater(b,(double)(0))||ae_fp_neq(b,(double)(ae_ifloor(b, _state))), "Overflow in Beta", _state);
9754 0 : y = a+b;
9755 0 : if( ae_fp_greater(ae_fabs(y, _state),171.624376956302725) )
9756 : {
9757 0 : y = lngamma(y, &s, _state);
9758 0 : sg = sg*s;
9759 0 : y = lngamma(b, &s, _state)-y;
9760 0 : sg = sg*s;
9761 0 : y = lngamma(a, &s, _state)+y;
9762 0 : sg = sg*s;
9763 0 : ae_assert(ae_fp_less_eq(y,ae_log(ae_maxrealnumber, _state)), "Overflow in Beta", _state);
9764 0 : result = sg*ae_exp(y, _state);
9765 0 : return result;
9766 : }
9767 0 : y = gammafunction(y, _state);
9768 0 : ae_assert(ae_fp_neq(y,(double)(0)), "Overflow in Beta", _state);
9769 0 : if( ae_fp_greater(a,b) )
9770 : {
9771 0 : y = gammafunction(a, _state)/y;
9772 0 : y = y*gammafunction(b, _state);
9773 : }
9774 : else
9775 : {
9776 0 : y = gammafunction(b, _state)/y;
9777 0 : y = y*gammafunction(a, _state);
9778 : }
9779 0 : result = y;
9780 0 : return result;
9781 : }
9782 :
9783 :
9784 : #endif
9785 : #if defined(AE_COMPILE_CHEBYSHEV) || !defined(AE_PARTIAL_BUILD)
9786 :
9787 :
9788 : /*************************************************************************
9789 : Calculation of the value of the Chebyshev polynomials of the
9790 : first and second kinds.
9791 :
9792 : Parameters:
9793 : r - polynomial kind, either 1 or 2.
9794 : n - degree, n>=0
9795 : x - argument, -1 <= x <= 1
9796 :
9797 : Result:
9798 : the value of the Chebyshev polynomial at x
9799 : *************************************************************************/
9800 0 : double chebyshevcalculate(ae_int_t r,
9801 : ae_int_t n,
9802 : double x,
9803 : ae_state *_state)
9804 : {
9805 : ae_int_t i;
9806 : double a;
9807 : double b;
9808 : double result;
9809 :
9810 :
9811 0 : result = (double)(0);
9812 :
9813 : /*
9814 : * Prepare A and B
9815 : */
9816 0 : if( r==1 )
9817 : {
9818 0 : a = (double)(1);
9819 0 : b = x;
9820 : }
9821 : else
9822 : {
9823 0 : a = (double)(1);
9824 0 : b = 2*x;
9825 : }
9826 :
9827 : /*
9828 : * Special cases: N=0 or N=1
9829 : */
9830 0 : if( n==0 )
9831 : {
9832 0 : result = a;
9833 0 : return result;
9834 : }
9835 0 : if( n==1 )
9836 : {
9837 0 : result = b;
9838 0 : return result;
9839 : }
9840 :
9841 : /*
9842 : * General case: N>=2
9843 : */
9844 0 : for(i=2; i<=n; i++)
9845 : {
9846 0 : result = 2*x*b-a;
9847 0 : a = b;
9848 0 : b = result;
9849 : }
9850 0 : return result;
9851 : }
9852 :
9853 :
9854 : /*************************************************************************
9855 : Summation of Chebyshev polynomials using Clenshaw's recurrence formula.
9856 :
9857 : This routine calculates
9858 : c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
9859 : or
9860 : c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
9861 : depending on the R.
9862 :
9863 : Parameters:
9864 : r - polynomial kind, either 1 or 2.
9865 : n - degree, n>=0
9866 : x - argument
9867 :
9868 : Result:
9869 : the value of the Chebyshev polynomial at x
9870 : *************************************************************************/
9871 0 : double chebyshevsum(/* Real */ ae_vector* c,
9872 : ae_int_t r,
9873 : ae_int_t n,
9874 : double x,
9875 : ae_state *_state)
9876 : {
9877 : double b1;
9878 : double b2;
9879 : ae_int_t i;
9880 : double result;
9881 :
9882 :
9883 0 : b1 = (double)(0);
9884 0 : b2 = (double)(0);
9885 0 : for(i=n; i>=1; i--)
9886 : {
9887 0 : result = 2*x*b1-b2+c->ptr.p_double[i];
9888 0 : b2 = b1;
9889 0 : b1 = result;
9890 : }
9891 0 : if( r==1 )
9892 : {
9893 0 : result = -b2+x*b1+c->ptr.p_double[0];
9894 : }
9895 : else
9896 : {
9897 0 : result = -b2+2*x*b1+c->ptr.p_double[0];
9898 : }
9899 0 : return result;
9900 : }
9901 :
9902 :
9903 : /*************************************************************************
9904 : Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
9905 :
9906 : Input parameters:
9907 : N - polynomial degree, n>=0
9908 :
9909 : Output parameters:
9910 : C - coefficients
9911 : *************************************************************************/
9912 0 : void chebyshevcoefficients(ae_int_t n,
9913 : /* Real */ ae_vector* c,
9914 : ae_state *_state)
9915 : {
9916 : ae_int_t i;
9917 :
9918 0 : ae_vector_clear(c);
9919 :
9920 0 : ae_vector_set_length(c, n+1, _state);
9921 0 : for(i=0; i<=n; i++)
9922 : {
9923 0 : c->ptr.p_double[i] = (double)(0);
9924 : }
9925 0 : if( n==0||n==1 )
9926 : {
9927 0 : c->ptr.p_double[n] = (double)(1);
9928 : }
9929 : else
9930 : {
9931 0 : c->ptr.p_double[n] = ae_exp((n-1)*ae_log((double)(2), _state), _state);
9932 0 : for(i=0; i<=n/2-1; i++)
9933 : {
9934 0 : c->ptr.p_double[n-2*(i+1)] = -c->ptr.p_double[n-2*i]*(n-2*i)*(n-2*i-1)/4/(i+1)/(n-i-1);
9935 : }
9936 : }
9937 0 : }
9938 :
9939 :
9940 : /*************************************************************************
9941 : Conversion of a series of Chebyshev polynomials to a power series.
9942 :
9943 : Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
9944 : B[0] + B[1]*X + ... + B[N]*X^N.
9945 :
9946 : Input parameters:
9947 : A - Chebyshev series coefficients
9948 : N - degree, N>=0
9949 :
9950 : Output parameters
9951 : B - power series coefficients
9952 : *************************************************************************/
9953 0 : void fromchebyshev(/* Real */ ae_vector* a,
9954 : ae_int_t n,
9955 : /* Real */ ae_vector* b,
9956 : ae_state *_state)
9957 : {
9958 : ae_int_t i;
9959 : ae_int_t k;
9960 : double e;
9961 : double d;
9962 :
9963 0 : ae_vector_clear(b);
9964 :
9965 0 : ae_vector_set_length(b, n+1, _state);
9966 0 : for(i=0; i<=n; i++)
9967 : {
9968 0 : b->ptr.p_double[i] = (double)(0);
9969 : }
9970 0 : d = (double)(0);
9971 0 : i = 0;
9972 : do
9973 : {
9974 0 : k = i;
9975 : do
9976 : {
9977 0 : e = b->ptr.p_double[k];
9978 0 : b->ptr.p_double[k] = (double)(0);
9979 0 : if( i<=1&&k==i )
9980 : {
9981 0 : b->ptr.p_double[k] = (double)(1);
9982 : }
9983 : else
9984 : {
9985 0 : if( i!=0 )
9986 : {
9987 0 : b->ptr.p_double[k] = 2*d;
9988 : }
9989 0 : if( k>i+1 )
9990 : {
9991 0 : b->ptr.p_double[k] = b->ptr.p_double[k]-b->ptr.p_double[k-2];
9992 : }
9993 : }
9994 0 : d = e;
9995 0 : k = k+1;
9996 : }
9997 0 : while(k<=n);
9998 0 : d = b->ptr.p_double[i];
9999 0 : e = (double)(0);
10000 0 : k = i;
10001 0 : while(k<=n)
10002 : {
10003 0 : e = e+b->ptr.p_double[k]*a->ptr.p_double[k];
10004 0 : k = k+2;
10005 : }
10006 0 : b->ptr.p_double[i] = e;
10007 0 : i = i+1;
10008 : }
10009 0 : while(i<=n);
10010 0 : }
10011 :
10012 :
10013 : #endif
10014 : #if defined(AE_COMPILE_STUDENTTDISTR) || !defined(AE_PARTIAL_BUILD)
10015 :
10016 :
10017 : /*************************************************************************
10018 : Student's t distribution
10019 :
10020 : Computes the integral from minus infinity to t of the Student
10021 : t distribution with integer k > 0 degrees of freedom:
10022 :
10023 : t
10024 : -
10025 : | |
10026 : - | 2 -(k+1)/2
10027 : | ( (k+1)/2 ) | ( x )
10028 : ---------------------- | ( 1 + --- ) dx
10029 : - | ( k )
10030 : sqrt( k pi ) | ( k/2 ) |
10031 : | |
10032 : -
10033 : -inf.
10034 :
10035 : Relation to incomplete beta integral:
10036 :
10037 : 1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
10038 : where
10039 : z = k/(k + t**2).
10040 :
10041 : For t < -2, this is the method of computation. For higher t,
10042 : a direct method is derived from integration by parts.
10043 : Since the function is symmetric about t=0, the area under the
10044 : right tail of the density is found by calling the function
10045 : with -t instead of t.
10046 :
10047 : ACCURACY:
10048 :
10049 : Tested at random 1 <= k <= 25. The "domain" refers to t.
10050 : Relative error:
10051 : arithmetic domain # trials peak rms
10052 : IEEE -100,-2 50000 5.9e-15 1.4e-15
10053 : IEEE -2,100 500000 2.7e-15 4.9e-17
10054 :
10055 : Cephes Math Library Release 2.8: June, 2000
10056 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
10057 : *************************************************************************/
10058 0 : double studenttdistribution(ae_int_t k, double t, ae_state *_state)
10059 : {
10060 : double x;
10061 : double rk;
10062 : double z;
10063 : double f;
10064 : double tz;
10065 : double p;
10066 : double xsqk;
10067 : ae_int_t j;
10068 : double result;
10069 :
10070 :
10071 0 : ae_assert(k>0, "Domain error in StudentTDistribution", _state);
10072 0 : if( ae_fp_eq(t,(double)(0)) )
10073 : {
10074 0 : result = 0.5;
10075 0 : return result;
10076 : }
10077 0 : if( ae_fp_less(t,-2.0) )
10078 : {
10079 0 : rk = (double)(k);
10080 0 : z = rk/(rk+t*t);
10081 0 : result = 0.5*incompletebeta(0.5*rk, 0.5, z, _state);
10082 0 : return result;
10083 : }
10084 0 : if( ae_fp_less(t,(double)(0)) )
10085 : {
10086 0 : x = -t;
10087 : }
10088 : else
10089 : {
10090 0 : x = t;
10091 : }
10092 0 : rk = (double)(k);
10093 0 : z = 1.0+x*x/rk;
10094 0 : if( k%2!=0 )
10095 : {
10096 0 : xsqk = x/ae_sqrt(rk, _state);
10097 0 : p = ae_atan(xsqk, _state);
10098 0 : if( k>1 )
10099 : {
10100 0 : f = 1.0;
10101 0 : tz = 1.0;
10102 0 : j = 3;
10103 0 : while(j<=k-2&&ae_fp_greater(tz/f,ae_machineepsilon))
10104 : {
10105 0 : tz = tz*((j-1)/(z*j));
10106 0 : f = f+tz;
10107 0 : j = j+2;
10108 : }
10109 0 : p = p+f*xsqk/z;
10110 : }
10111 0 : p = p*2.0/ae_pi;
10112 : }
10113 : else
10114 : {
10115 0 : f = 1.0;
10116 0 : tz = 1.0;
10117 0 : j = 2;
10118 0 : while(j<=k-2&&ae_fp_greater(tz/f,ae_machineepsilon))
10119 : {
10120 0 : tz = tz*((j-1)/(z*j));
10121 0 : f = f+tz;
10122 0 : j = j+2;
10123 : }
10124 0 : p = f*x/ae_sqrt(z*rk, _state);
10125 : }
10126 0 : if( ae_fp_less(t,(double)(0)) )
10127 : {
10128 0 : p = -p;
10129 : }
10130 0 : result = 0.5+0.5*p;
10131 0 : return result;
10132 : }
10133 :
10134 :
10135 : /*************************************************************************
10136 : Functional inverse of Student's t distribution
10137 :
10138 : Given probability p, finds the argument t such that stdtr(k,t)
10139 : is equal to p.
10140 :
10141 : ACCURACY:
10142 :
10143 : Tested at random 1 <= k <= 100. The "domain" refers to p:
10144 : Relative error:
10145 : arithmetic domain # trials peak rms
10146 : IEEE .001,.999 25000 5.7e-15 8.0e-16
10147 : IEEE 10^-6,.001 25000 2.0e-12 2.9e-14
10148 :
10149 : Cephes Math Library Release 2.8: June, 2000
10150 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
10151 : *************************************************************************/
10152 0 : double invstudenttdistribution(ae_int_t k, double p, ae_state *_state)
10153 : {
10154 : double t;
10155 : double rk;
10156 : double z;
10157 : ae_int_t rflg;
10158 : double result;
10159 :
10160 :
10161 0 : ae_assert((k>0&&ae_fp_greater(p,(double)(0)))&&ae_fp_less(p,(double)(1)), "Domain error in InvStudentTDistribution", _state);
10162 0 : rk = (double)(k);
10163 0 : if( ae_fp_greater(p,0.25)&&ae_fp_less(p,0.75) )
10164 : {
10165 0 : if( ae_fp_eq(p,0.5) )
10166 : {
10167 0 : result = (double)(0);
10168 0 : return result;
10169 : }
10170 0 : z = 1.0-2.0*p;
10171 0 : z = invincompletebeta(0.5, 0.5*rk, ae_fabs(z, _state), _state);
10172 0 : t = ae_sqrt(rk*z/(1.0-z), _state);
10173 0 : if( ae_fp_less(p,0.5) )
10174 : {
10175 0 : t = -t;
10176 : }
10177 0 : result = t;
10178 0 : return result;
10179 : }
10180 0 : rflg = -1;
10181 0 : if( ae_fp_greater_eq(p,0.5) )
10182 : {
10183 0 : p = 1.0-p;
10184 0 : rflg = 1;
10185 : }
10186 0 : z = invincompletebeta(0.5*rk, 0.5, 2.0*p, _state);
10187 0 : if( ae_fp_less(ae_maxrealnumber*z,rk) )
10188 : {
10189 0 : result = rflg*ae_maxrealnumber;
10190 0 : return result;
10191 : }
10192 0 : t = ae_sqrt(rk/z-rk, _state);
10193 0 : result = rflg*t;
10194 0 : return result;
10195 : }
10196 :
10197 :
10198 : #endif
10199 : #if defined(AE_COMPILE_BINOMIALDISTR) || !defined(AE_PARTIAL_BUILD)
10200 :
10201 :
10202 : /*************************************************************************
10203 : Binomial distribution
10204 :
10205 : Returns the sum of the terms 0 through k of the Binomial
10206 : probability density:
10207 :
10208 : k
10209 : -- ( n ) j n-j
10210 : > ( ) p (1-p)
10211 : -- ( j )
10212 : j=0
10213 :
10214 : The terms are not summed directly; instead the incomplete
10215 : beta integral is employed, according to the formula
10216 :
10217 : y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
10218 :
10219 : The arguments must be positive, with p ranging from 0 to 1.
10220 :
10221 : ACCURACY:
10222 :
10223 : Tested at random points (a,b,p), with p between 0 and 1.
10224 :
10225 : a,b Relative error:
10226 : arithmetic domain # trials peak rms
10227 : For p between 0.001 and 1:
10228 : IEEE 0,100 100000 4.3e-15 2.6e-16
10229 :
10230 : Cephes Math Library Release 2.8: June, 2000
10231 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
10232 : *************************************************************************/
10233 0 : double binomialdistribution(ae_int_t k,
10234 : ae_int_t n,
10235 : double p,
10236 : ae_state *_state)
10237 : {
10238 : double dk;
10239 : double dn;
10240 : double result;
10241 :
10242 :
10243 0 : ae_assert(ae_fp_greater_eq(p,(double)(0))&&ae_fp_less_eq(p,(double)(1)), "Domain error in BinomialDistribution", _state);
10244 0 : ae_assert(k>=-1&&k<=n, "Domain error in BinomialDistribution", _state);
10245 0 : if( k==-1 )
10246 : {
10247 0 : result = (double)(0);
10248 0 : return result;
10249 : }
10250 0 : if( k==n )
10251 : {
10252 0 : result = (double)(1);
10253 0 : return result;
10254 : }
10255 0 : dn = (double)(n-k);
10256 0 : if( k==0 )
10257 : {
10258 0 : dk = ae_pow(1.0-p, dn, _state);
10259 : }
10260 : else
10261 : {
10262 0 : dk = (double)(k+1);
10263 0 : dk = incompletebeta(dn, dk, 1.0-p, _state);
10264 : }
10265 0 : result = dk;
10266 0 : return result;
10267 : }
10268 :
10269 :
10270 : /*************************************************************************
10271 : Complemented binomial distribution
10272 :
10273 : Returns the sum of the terms k+1 through n of the Binomial
10274 : probability density:
10275 :
10276 : n
10277 : -- ( n ) j n-j
10278 : > ( ) p (1-p)
10279 : -- ( j )
10280 : j=k+1
10281 :
10282 : The terms are not summed directly; instead the incomplete
10283 : beta integral is employed, according to the formula
10284 :
10285 : y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
10286 :
10287 : The arguments must be positive, with p ranging from 0 to 1.
10288 :
10289 : ACCURACY:
10290 :
10291 : Tested at random points (a,b,p).
10292 :
10293 : a,b Relative error:
10294 : arithmetic domain # trials peak rms
10295 : For p between 0.001 and 1:
10296 : IEEE 0,100 100000 6.7e-15 8.2e-16
10297 : For p between 0 and .001:
10298 : IEEE 0,100 100000 1.5e-13 2.7e-15
10299 :
10300 : Cephes Math Library Release 2.8: June, 2000
10301 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
10302 : *************************************************************************/
10303 0 : double binomialcdistribution(ae_int_t k,
10304 : ae_int_t n,
10305 : double p,
10306 : ae_state *_state)
10307 : {
10308 : double dk;
10309 : double dn;
10310 : double result;
10311 :
10312 :
10313 0 : ae_assert(ae_fp_greater_eq(p,(double)(0))&&ae_fp_less_eq(p,(double)(1)), "Domain error in BinomialDistributionC", _state);
10314 0 : ae_assert(k>=-1&&k<=n, "Domain error in BinomialDistributionC", _state);
10315 0 : if( k==-1 )
10316 : {
10317 0 : result = (double)(1);
10318 0 : return result;
10319 : }
10320 0 : if( k==n )
10321 : {
10322 0 : result = (double)(0);
10323 0 : return result;
10324 : }
10325 0 : dn = (double)(n-k);
10326 0 : if( k==0 )
10327 : {
10328 0 : if( ae_fp_less(p,0.01) )
10329 : {
10330 0 : dk = -nuexpm1(dn*nulog1p(-p, _state), _state);
10331 : }
10332 : else
10333 : {
10334 0 : dk = 1.0-ae_pow(1.0-p, dn, _state);
10335 : }
10336 : }
10337 : else
10338 : {
10339 0 : dk = (double)(k+1);
10340 0 : dk = incompletebeta(dk, dn, p, _state);
10341 : }
10342 0 : result = dk;
10343 0 : return result;
10344 : }
10345 :
10346 :
10347 : /*************************************************************************
10348 : Inverse binomial distribution
10349 :
10350 : Finds the event probability p such that the sum of the
10351 : terms 0 through k of the Binomial probability density
10352 : is equal to the given cumulative probability y.
10353 :
10354 : This is accomplished using the inverse beta integral
10355 : function and the relation
10356 :
10357 : 1 - p = incbi( n-k, k+1, y ).
10358 :
10359 : ACCURACY:
10360 :
10361 : Tested at random points (a,b,p).
10362 :
10363 : a,b Relative error:
10364 : arithmetic domain # trials peak rms
10365 : For p between 0.001 and 1:
10366 : IEEE 0,100 100000 2.3e-14 6.4e-16
10367 : IEEE 0,10000 100000 6.6e-12 1.2e-13
10368 : For p between 10^-6 and 0.001:
10369 : IEEE 0,100 100000 2.0e-12 1.3e-14
10370 : IEEE 0,10000 100000 1.5e-12 3.2e-14
10371 :
10372 : Cephes Math Library Release 2.8: June, 2000
10373 : Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
10374 : *************************************************************************/
10375 0 : double invbinomialdistribution(ae_int_t k,
10376 : ae_int_t n,
10377 : double y,
10378 : ae_state *_state)
10379 : {
10380 : double dk;
10381 : double dn;
10382 : double p;
10383 : double result;
10384 :
10385 :
10386 0 : ae_assert(k>=0&&k<n, "Domain error in InvBinomialDistribution", _state);
10387 0 : dn = (double)(n-k);
10388 0 : if( k==0 )
10389 : {
10390 0 : if( ae_fp_greater(y,0.8) )
10391 : {
10392 0 : p = -nuexpm1(nulog1p(y-1.0, _state)/dn, _state);
10393 : }
10394 : else
10395 : {
10396 0 : p = 1.0-ae_pow(y, 1.0/dn, _state);
10397 : }
10398 : }
10399 : else
10400 : {
10401 0 : dk = (double)(k+1);
10402 0 : p = incompletebeta(dn, dk, 0.5, _state);
10403 0 : if( ae_fp_greater(p,0.5) )
10404 : {
10405 0 : p = invincompletebeta(dk, dn, 1.0-y, _state);
10406 : }
10407 : else
10408 : {
10409 0 : p = 1.0-invincompletebeta(dn, dk, y, _state);
10410 : }
10411 : }
10412 0 : result = p;
10413 0 : return result;
10414 : }
10415 :
10416 :
10417 : #endif
10418 : #if defined(AE_COMPILE_AIRYF) || !defined(AE_PARTIAL_BUILD)
10419 :
10420 :
10421 : /*************************************************************************
10422 : Airy function
10423 :
10424 : Solution of the differential equation
10425 :
10426 : y"(x) = xy.
10427 :
10428 : The function returns the two independent solutions Ai, Bi
10429 : and their first derivatives Ai'(x), Bi'(x).
10430 :
10431 : Evaluation is by power series summation for small x,
10432 : by rational minimax approximations for large x.
10433 :
10434 :
10435 :
10436 : ACCURACY:
10437 : Error criterion is absolute when function <= 1, relative
10438 : when function > 1, except * denotes relative error criterion.
10439 : For large negative x, the absolute error increases as x^1.5.
10440 : For large positive x, the relative error increases as x^1.5.
10441 :
10442 : Arithmetic domain function # trials peak rms
10443 : IEEE -10, 0 Ai 10000 1.6e-15 2.7e-16
10444 : IEEE 0, 10 Ai 10000 2.3e-14* 1.8e-15*
10445 : IEEE -10, 0 Ai' 10000 4.6e-15 7.6e-16
10446 : IEEE 0, 10 Ai' 10000 1.8e-14* 1.5e-15*
10447 : IEEE -10, 10 Bi 30000 4.2e-15 5.3e-16
10448 : IEEE -10, 10 Bi' 30000 4.9e-15 7.3e-16
10449 :
10450 : Cephes Math Library Release 2.8: June, 2000
10451 : Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
10452 : *************************************************************************/
10453 0 : void airy(double x,
10454 : double* ai,
10455 : double* aip,
10456 : double* bi,
10457 : double* bip,
10458 : ae_state *_state)
10459 : {
10460 : double z;
10461 : double zz;
10462 : double t;
10463 : double f;
10464 : double g;
10465 : double uf;
10466 : double ug;
10467 : double k;
10468 : double zeta;
10469 : double theta;
10470 : ae_int_t domflg;
10471 : double c1;
10472 : double c2;
10473 : double sqrt3;
10474 : double sqpii;
10475 : double afn;
10476 : double afd;
10477 : double agn;
10478 : double agd;
10479 : double apfn;
10480 : double apfd;
10481 : double apgn;
10482 : double apgd;
10483 : double an;
10484 : double ad;
10485 : double apn;
10486 : double apd;
10487 : double bn16;
10488 : double bd16;
10489 : double bppn;
10490 : double bppd;
10491 :
10492 0 : *ai = 0;
10493 0 : *aip = 0;
10494 0 : *bi = 0;
10495 0 : *bip = 0;
10496 :
10497 0 : sqpii = 5.64189583547756286948E-1;
10498 0 : c1 = 0.35502805388781723926;
10499 0 : c2 = 0.258819403792806798405;
10500 0 : sqrt3 = 1.732050807568877293527;
10501 0 : domflg = 0;
10502 0 : if( ae_fp_greater(x,25.77) )
10503 : {
10504 0 : *ai = (double)(0);
10505 0 : *aip = (double)(0);
10506 0 : *bi = ae_maxrealnumber;
10507 0 : *bip = ae_maxrealnumber;
10508 0 : return;
10509 : }
10510 0 : if( ae_fp_less(x,-2.09) )
10511 : {
10512 0 : domflg = 15;
10513 0 : t = ae_sqrt(-x, _state);
10514 0 : zeta = -2.0*x*t/3.0;
10515 0 : t = ae_sqrt(t, _state);
10516 0 : k = sqpii/t;
10517 0 : z = 1.0/zeta;
10518 0 : zz = z*z;
10519 0 : afn = -1.31696323418331795333E-1;
10520 0 : afn = afn*zz-6.26456544431912369773E-1;
10521 0 : afn = afn*zz-6.93158036036933542233E-1;
10522 0 : afn = afn*zz-2.79779981545119124951E-1;
10523 0 : afn = afn*zz-4.91900132609500318020E-2;
10524 0 : afn = afn*zz-4.06265923594885404393E-3;
10525 0 : afn = afn*zz-1.59276496239262096340E-4;
10526 0 : afn = afn*zz-2.77649108155232920844E-6;
10527 0 : afn = afn*zz-1.67787698489114633780E-8;
10528 0 : afd = 1.00000000000000000000E0;
10529 0 : afd = afd*zz+1.33560420706553243746E1;
10530 0 : afd = afd*zz+3.26825032795224613948E1;
10531 0 : afd = afd*zz+2.67367040941499554804E1;
10532 0 : afd = afd*zz+9.18707402907259625840E0;
10533 0 : afd = afd*zz+1.47529146771666414581E0;
10534 0 : afd = afd*zz+1.15687173795188044134E-1;
10535 0 : afd = afd*zz+4.40291641615211203805E-3;
10536 0 : afd = afd*zz+7.54720348287414296618E-5;
10537 0 : afd = afd*zz+4.51850092970580378464E-7;
10538 0 : uf = 1.0+zz*afn/afd;
10539 0 : agn = 1.97339932091685679179E-2;
10540 0 : agn = agn*zz+3.91103029615688277255E-1;
10541 0 : agn = agn*zz+1.06579897599595591108E0;
10542 0 : agn = agn*zz+9.39169229816650230044E-1;
10543 0 : agn = agn*zz+3.51465656105547619242E-1;
10544 0 : agn = agn*zz+6.33888919628925490927E-2;
10545 0 : agn = agn*zz+5.85804113048388458567E-3;
10546 0 : agn = agn*zz+2.82851600836737019778E-4;
10547 0 : agn = agn*zz+6.98793669997260967291E-6;
10548 0 : agn = agn*zz+8.11789239554389293311E-8;
10549 0 : agn = agn*zz+3.41551784765923618484E-10;
10550 0 : agd = 1.00000000000000000000E0;
10551 0 : agd = agd*zz+9.30892908077441974853E0;
10552 0 : agd = agd*zz+1.98352928718312140417E1;
10553 0 : agd = agd*zz+1.55646628932864612953E1;
10554 0 : agd = agd*zz+5.47686069422975497931E0;
10555 0 : agd = agd*zz+9.54293611618961883998E-1;
10556 0 : agd = agd*zz+8.64580826352392193095E-2;
10557 0 : agd = agd*zz+4.12656523824222607191E-3;
10558 0 : agd = agd*zz+1.01259085116509135510E-4;
10559 0 : agd = agd*zz+1.17166733214413521882E-6;
10560 0 : agd = agd*zz+4.91834570062930015649E-9;
10561 0 : ug = z*agn/agd;
10562 0 : theta = zeta+0.25*ae_pi;
10563 0 : f = ae_sin(theta, _state);
10564 0 : g = ae_cos(theta, _state);
10565 0 : *ai = k*(f*uf-g*ug);
10566 0 : *bi = k*(g*uf+f*ug);
10567 0 : apfn = 1.85365624022535566142E-1;
10568 0 : apfn = apfn*zz+8.86712188052584095637E-1;
10569 0 : apfn = apfn*zz+9.87391981747398547272E-1;
10570 0 : apfn = apfn*zz+4.01241082318003734092E-1;
10571 0 : apfn = apfn*zz+7.10304926289631174579E-2;
10572 0 : apfn = apfn*zz+5.90618657995661810071E-3;
10573 0 : apfn = apfn*zz+2.33051409401776799569E-4;
10574 0 : apfn = apfn*zz+4.08718778289035454598E-6;
10575 0 : apfn = apfn*zz+2.48379932900442457853E-8;
10576 0 : apfd = 1.00000000000000000000E0;
10577 0 : apfd = apfd*zz+1.47345854687502542552E1;
10578 0 : apfd = apfd*zz+3.75423933435489594466E1;
10579 0 : apfd = apfd*zz+3.14657751203046424330E1;
10580 0 : apfd = apfd*zz+1.09969125207298778536E1;
10581 0 : apfd = apfd*zz+1.78885054766999417817E0;
10582 0 : apfd = apfd*zz+1.41733275753662636873E-1;
10583 0 : apfd = apfd*zz+5.44066067017226003627E-3;
10584 0 : apfd = apfd*zz+9.39421290654511171663E-5;
10585 0 : apfd = apfd*zz+5.65978713036027009243E-7;
10586 0 : uf = 1.0+zz*apfn/apfd;
10587 0 : apgn = -3.55615429033082288335E-2;
10588 0 : apgn = apgn*zz-6.37311518129435504426E-1;
10589 0 : apgn = apgn*zz-1.70856738884312371053E0;
10590 0 : apgn = apgn*zz-1.50221872117316635393E0;
10591 0 : apgn = apgn*zz-5.63606665822102676611E-1;
10592 0 : apgn = apgn*zz-1.02101031120216891789E-1;
10593 0 : apgn = apgn*zz-9.48396695961445269093E-3;
10594 0 : apgn = apgn*zz-4.60325307486780994357E-4;
10595 0 : apgn = apgn*zz-1.14300836484517375919E-5;
10596 0 : apgn = apgn*zz-1.33415518685547420648E-7;
10597 0 : apgn = apgn*zz-5.63803833958893494476E-10;
10598 0 : apgd = 1.00000000000000000000E0;
10599 0 : apgd = apgd*zz+9.85865801696130355144E0;
10600 0 : apgd = apgd*zz+2.16401867356585941885E1;
10601 0 : apgd = apgd*zz+1.73130776389749389525E1;
10602 0 : apgd = apgd*zz+6.17872175280828766327E0;
10603 0 : apgd = apgd*zz+1.08848694396321495475E0;
10604 0 : apgd = apgd*zz+9.95005543440888479402E-2;
10605 0 : apgd = apgd*zz+4.78468199683886610842E-3;
10606 0 : apgd = apgd*zz+1.18159633322838625562E-4;
10607 0 : apgd = apgd*zz+1.37480673554219441465E-6;
10608 0 : apgd = apgd*zz+5.79912514929147598821E-9;
10609 0 : ug = z*apgn/apgd;
10610 0 : k = sqpii*t;
10611 0 : *aip = -k*(g*uf+f*ug);
10612 0 : *bip = k*(f*uf-g*ug);
10613 0 : return;
10614 : }
10615 0 : if( ae_fp_greater_eq(x,2.09) )
10616 : {
10617 0 : domflg = 5;
10618 0 : t = ae_sqrt(x, _state);
10619 0 : zeta = 2.0*x*t/3.0;
10620 0 : g = ae_exp(zeta, _state);
10621 0 : t = ae_sqrt(t, _state);
10622 0 : k = 2.0*t*g;
10623 0 : z = 1.0/zeta;
10624 0 : an = 3.46538101525629032477E-1;
10625 0 : an = an*z+1.20075952739645805542E1;
10626 0 : an = an*z+7.62796053615234516538E1;
10627 0 : an = an*z+1.68089224934630576269E2;
10628 0 : an = an*z+1.59756391350164413639E2;
10629 0 : an = an*z+7.05360906840444183113E1;
10630 0 : an = an*z+1.40264691163389668864E1;
10631 0 : an = an*z+9.99999999999999995305E-1;
10632 0 : ad = 5.67594532638770212846E-1;
10633 0 : ad = ad*z+1.47562562584847203173E1;
10634 0 : ad = ad*z+8.45138970141474626562E1;
10635 0 : ad = ad*z+1.77318088145400459522E2;
10636 0 : ad = ad*z+1.64234692871529701831E2;
10637 0 : ad = ad*z+7.14778400825575695274E1;
10638 0 : ad = ad*z+1.40959135607834029598E1;
10639 0 : ad = ad*z+1.00000000000000000470E0;
10640 0 : f = an/ad;
10641 0 : *ai = sqpii*f/k;
10642 0 : k = -0.5*sqpii*t/g;
10643 0 : apn = 6.13759184814035759225E-1;
10644 0 : apn = apn*z+1.47454670787755323881E1;
10645 0 : apn = apn*z+8.20584123476060982430E1;
10646 0 : apn = apn*z+1.71184781360976385540E2;
10647 0 : apn = apn*z+1.59317847137141783523E2;
10648 0 : apn = apn*z+6.99778599330103016170E1;
10649 0 : apn = apn*z+1.39470856980481566958E1;
10650 0 : apn = apn*z+1.00000000000000000550E0;
10651 0 : apd = 3.34203677749736953049E-1;
10652 0 : apd = apd*z+1.11810297306158156705E1;
10653 0 : apd = apd*z+7.11727352147859965283E1;
10654 0 : apd = apd*z+1.58778084372838313640E2;
10655 0 : apd = apd*z+1.53206427475809220834E2;
10656 0 : apd = apd*z+6.86752304592780337944E1;
10657 0 : apd = apd*z+1.38498634758259442477E1;
10658 0 : apd = apd*z+9.99999999999999994502E-1;
10659 0 : f = apn/apd;
10660 0 : *aip = f*k;
10661 0 : if( ae_fp_greater(x,8.3203353) )
10662 : {
10663 0 : bn16 = -2.53240795869364152689E-1;
10664 0 : bn16 = bn16*z+5.75285167332467384228E-1;
10665 0 : bn16 = bn16*z-3.29907036873225371650E-1;
10666 0 : bn16 = bn16*z+6.44404068948199951727E-2;
10667 0 : bn16 = bn16*z-3.82519546641336734394E-3;
10668 0 : bd16 = 1.00000000000000000000E0;
10669 0 : bd16 = bd16*z-7.15685095054035237902E0;
10670 0 : bd16 = bd16*z+1.06039580715664694291E1;
10671 0 : bd16 = bd16*z-5.23246636471251500874E0;
10672 0 : bd16 = bd16*z+9.57395864378383833152E-1;
10673 0 : bd16 = bd16*z-5.50828147163549611107E-2;
10674 0 : f = z*bn16/bd16;
10675 0 : k = sqpii*g;
10676 0 : *bi = k*(1.0+f)/t;
10677 0 : bppn = 4.65461162774651610328E-1;
10678 0 : bppn = bppn*z-1.08992173800493920734E0;
10679 0 : bppn = bppn*z+6.38800117371827987759E-1;
10680 0 : bppn = bppn*z-1.26844349553102907034E-1;
10681 0 : bppn = bppn*z+7.62487844342109852105E-3;
10682 0 : bppd = 1.00000000000000000000E0;
10683 0 : bppd = bppd*z-8.70622787633159124240E0;
10684 0 : bppd = bppd*z+1.38993162704553213172E1;
10685 0 : bppd = bppd*z-7.14116144616431159572E0;
10686 0 : bppd = bppd*z+1.34008595960680518666E0;
10687 0 : bppd = bppd*z-7.84273211323341930448E-2;
10688 0 : f = z*bppn/bppd;
10689 0 : *bip = k*t*(1.0+f);
10690 0 : return;
10691 : }
10692 : }
10693 0 : f = 1.0;
10694 0 : g = x;
10695 0 : t = 1.0;
10696 0 : uf = 1.0;
10697 0 : ug = x;
10698 0 : k = 1.0;
10699 0 : z = x*x*x;
10700 0 : while(ae_fp_greater(t,ae_machineepsilon))
10701 : {
10702 0 : uf = uf*z;
10703 0 : k = k+1.0;
10704 0 : uf = uf/k;
10705 0 : ug = ug*z;
10706 0 : k = k+1.0;
10707 0 : ug = ug/k;
10708 0 : uf = uf/k;
10709 0 : f = f+uf;
10710 0 : k = k+1.0;
10711 0 : ug = ug/k;
10712 0 : g = g+ug;
10713 0 : t = ae_fabs(uf/f, _state);
10714 : }
10715 0 : uf = c1*f;
10716 0 : ug = c2*g;
10717 0 : if( domflg%2==0 )
10718 : {
10719 0 : *ai = uf-ug;
10720 : }
10721 0 : if( domflg/2%2==0 )
10722 : {
10723 0 : *bi = sqrt3*(uf+ug);
10724 : }
10725 0 : k = 4.0;
10726 0 : uf = x*x/2.0;
10727 0 : ug = z/3.0;
10728 0 : f = uf;
10729 0 : g = 1.0+ug;
10730 0 : uf = uf/3.0;
10731 0 : t = 1.0;
10732 0 : while(ae_fp_greater(t,ae_machineepsilon))
10733 : {
10734 0 : uf = uf*z;
10735 0 : ug = ug/k;
10736 0 : k = k+1.0;
10737 0 : ug = ug*z;
10738 0 : uf = uf/k;
10739 0 : f = f+uf;
10740 0 : k = k+1.0;
10741 0 : ug = ug/k;
10742 0 : uf = uf/k;
10743 0 : g = g+ug;
10744 0 : k = k+1.0;
10745 0 : t = ae_fabs(ug/g, _state);
10746 : }
10747 0 : uf = c1*f;
10748 0 : ug = c2*g;
10749 0 : if( domflg/4%2==0 )
10750 : {
10751 0 : *aip = uf-ug;
10752 : }
10753 0 : if( domflg/8%2==0 )
10754 : {
10755 0 : *bip = sqrt3*(uf+ug);
10756 : }
10757 : }
10758 :
10759 :
10760 : #endif
10761 :
10762 : }
10763 :
|