Xmipp  v3.23.11-Nereus
specialfunctions.cpp
Go to the documentation of this file.
1 /*************************************************************************
2 Copyright (c) Sergey Bochkanov (ALGLIB project).
3 
4 >>> SOURCE LICENSE >>>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation (www.fsf.org); either version 2 of the
8 License, or (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 A copy of the GNU General Public License is available at
16 http://www.fsf.org/licensing/licenses
17 >>> END OF LICENSE >>>
18 *************************************************************************/
19 #include "stdafx.h"
20 #include "specialfunctions.h"
21 
22 // disable some irrelevant warnings
23 #if (AE_COMPILER==AE_MSVC)
24 #pragma warning(disable:4100)
25 #pragma warning(disable:4127)
26 #pragma warning(disable:4702)
27 #pragma warning(disable:4996)
28 #endif
29 using namespace std;
30 
32 //
33 // THIS SECTION CONTAINS IMPLEMENTATION OF C++ INTERFACE
34 //
36 namespace alglib
37 {
38 
39 
40 /*************************************************************************
41 Gamma function
42 
43 Input parameters:
44  X - argument
45 
46 Domain:
47  0 < X < 171.6
48  -170 < X < 0, X is not an integer.
49 
50 Relative error:
51  arithmetic domain # trials peak rms
52  IEEE -170,-33 20000 2.3e-15 3.3e-16
53  IEEE -33, 33 20000 9.4e-16 2.2e-16
54  IEEE 33, 171.6 20000 2.3e-15 3.2e-16
55 
56 Cephes Math Library Release 2.8: June, 2000
57 Original copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
58 Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
59 *************************************************************************/
60 double gammafunction(const double x)
61 {
62  alglib_impl::ae_state _alglib_env_state;
63  alglib_impl::ae_state_init(&_alglib_env_state);
64  try
65  {
66  double result = alglib_impl::gammafunction(x, &_alglib_env_state);
67  alglib_impl::ae_state_clear(&_alglib_env_state);
68  return *(reinterpret_cast<double*>(&result));
69  }
71  {
72  throw ap_error(_alglib_env_state.error_msg);
73  }
74 }
75 
76 /*************************************************************************
77 Natural logarithm of gamma function
78 
79 Input parameters:
80  X - argument
81 
82 Result:
83  logarithm of the absolute value of the Gamma(X).
84 
85 Output parameters:
86  SgnGam - sign(Gamma(X))
87 
88 Domain:
89  0 < X < 2.55e305
90  -2.55e305 < X < 0, X is not an integer.
91 
92 ACCURACY:
93 arithmetic domain # trials peak rms
94  IEEE 0, 3 28000 5.4e-16 1.1e-16
95  IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
96 The error criterion was relative when the function magnitude
97 was greater than one but absolute when it was less than one.
98 
99 The following test used the relative error criterion, though
100 at certain points the relative error could be much higher than
101 indicated.
102  IEEE -200, -4 10000 4.8e-16 1.3e-16
103 
104 Cephes Math Library Release 2.8: June, 2000
105 Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
106 Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
107 *************************************************************************/
108 double lngamma(const double x, double &sgngam)
109 {
110  alglib_impl::ae_state _alglib_env_state;
111  alglib_impl::ae_state_init(&_alglib_env_state);
112  try
113  {
114  double result = alglib_impl::lngamma(x, &sgngam, &_alglib_env_state);
115  alglib_impl::ae_state_clear(&_alglib_env_state);
116  return *(reinterpret_cast<double*>(&result));
117  }
119  {
120  throw ap_error(_alglib_env_state.error_msg);
121  }
122 }
123 
124 /*************************************************************************
125 Error function
126 
127 The integral is
128 
129  x
130  -
131  2 | | 2
132  erf(x) = -------- | exp( - t ) dt.
133  sqrt(pi) | |
134  -
135  0
136 
137 For 0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2); otherwise
138 erf(x) = 1 - erfc(x).
139 
140 
141 ACCURACY:
142 
143  Relative error:
144 arithmetic domain # trials peak rms
145  IEEE 0,1 30000 3.7e-16 1.0e-16
146 
147 Cephes Math Library Release 2.8: June, 2000
148 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
149 *************************************************************************/
150 double errorfunction(const double x)
151 {
152  alglib_impl::ae_state _alglib_env_state;
153  alglib_impl::ae_state_init(&_alglib_env_state);
154  try
155  {
156  double result = alglib_impl::errorfunction(x, &_alglib_env_state);
157  alglib_impl::ae_state_clear(&_alglib_env_state);
158  return *(reinterpret_cast<double*>(&result));
159  }
161  {
162  throw ap_error(_alglib_env_state.error_msg);
163  }
164 }
165 
166 /*************************************************************************
167 Complementary error function
168 
169  1 - erf(x) =
170 
171  inf.
172  -
173  2 | | 2
174  erfc(x) = -------- | exp( - t ) dt
175  sqrt(pi) | |
176  -
177  x
178 
179 
180 For small x, erfc(x) = 1 - erf(x); otherwise rational
181 approximations are computed.
182 
183 
184 ACCURACY:
185 
186  Relative error:
187 arithmetic domain # trials peak rms
188  IEEE 0,26.6417 30000 5.7e-14 1.5e-14
189 
190 Cephes Math Library Release 2.8: June, 2000
191 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
192 *************************************************************************/
193 double errorfunctionc(const double x)
194 {
195  alglib_impl::ae_state _alglib_env_state;
196  alglib_impl::ae_state_init(&_alglib_env_state);
197  try
198  {
199  double result = alglib_impl::errorfunctionc(x, &_alglib_env_state);
200  alglib_impl::ae_state_clear(&_alglib_env_state);
201  return *(reinterpret_cast<double*>(&result));
202  }
204  {
205  throw ap_error(_alglib_env_state.error_msg);
206  }
207 }
208 
209 /*************************************************************************
210 Normal distribution function
211 
212 Returns the area under the Gaussian probability density
213 function, integrated from minus infinity to x:
214 
215  x
216  -
217  1 | | 2
218  ndtr(x) = --------- | exp( - t /2 ) dt
219  sqrt(2pi) | |
220  -
221  -inf.
222 
223  = ( 1 + erf(z) ) / 2
224  = erfc(z) / 2
225 
226 where z = x/sqrt(2). Computation is via the functions
227 erf and erfc.
228 
229 
230 ACCURACY:
231 
232  Relative error:
233 arithmetic domain # trials peak rms
234  IEEE -13,0 30000 3.4e-14 6.7e-15
235 
236 Cephes Math Library Release 2.8: June, 2000
237 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
238 *************************************************************************/
239 double normaldistribution(const double x)
240 {
241  alglib_impl::ae_state _alglib_env_state;
242  alglib_impl::ae_state_init(&_alglib_env_state);
243  try
244  {
245  double result = alglib_impl::normaldistribution(x, &_alglib_env_state);
246  alglib_impl::ae_state_clear(&_alglib_env_state);
247  return *(reinterpret_cast<double*>(&result));
248  }
250  {
251  throw ap_error(_alglib_env_state.error_msg);
252  }
253 }
254 
255 /*************************************************************************
256 Inverse of the error function
257 
258 Cephes Math Library Release 2.8: June, 2000
259 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
260 *************************************************************************/
261 double inverf(const double e)
262 {
263  alglib_impl::ae_state _alglib_env_state;
264  alglib_impl::ae_state_init(&_alglib_env_state);
265  try
266  {
267  double result = alglib_impl::inverf(e, &_alglib_env_state);
268  alglib_impl::ae_state_clear(&_alglib_env_state);
269  return *(reinterpret_cast<double*>(&result));
270  }
272  {
273  throw ap_error(_alglib_env_state.error_msg);
274  }
275 }
276 
277 /*************************************************************************
278 Inverse of Normal distribution function
279 
280 Returns the argument, x, for which the area under the
281 Gaussian probability density function (integrated from
282 minus infinity to x) is equal to y.
283 
284 
285 For small arguments 0 < y < exp(-2), the program computes
286 z = sqrt( -2.0 * log(y) ); then the approximation is
287 x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
288 There are two rational functions P/Q, one for 0 < y < exp(-32)
289 and the other for y up to exp(-2). For larger arguments,
290 w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
291 
292 ACCURACY:
293 
294  Relative error:
295 arithmetic domain # trials peak rms
296  IEEE 0.125, 1 20000 7.2e-16 1.3e-16
297  IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
298 
299 Cephes Math Library Release 2.8: June, 2000
300 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
301 *************************************************************************/
302 double invnormaldistribution(const double y0)
303 {
304  alglib_impl::ae_state _alglib_env_state;
305  alglib_impl::ae_state_init(&_alglib_env_state);
306  try
307  {
308  double result = alglib_impl::invnormaldistribution(y0, &_alglib_env_state);
309  alglib_impl::ae_state_clear(&_alglib_env_state);
310  return *(reinterpret_cast<double*>(&result));
311  }
313  {
314  throw ap_error(_alglib_env_state.error_msg);
315  }
316 }
317 
318 /*************************************************************************
319 Incomplete gamma integral
320 
321 The function is defined by
322 
323  x
324  -
325  1 | | -t a-1
326  igam(a,x) = ----- | e t dt.
327  - | |
328  | (a) -
329  0
330 
331 
332 In this implementation both arguments must be positive.
333 The integral is evaluated by either a power series or
334 continued fraction expansion, depending on the relative
335 values of a and x.
336 
337 ACCURACY:
338 
339  Relative error:
340 arithmetic domain # trials peak rms
341  IEEE 0,30 200000 3.6e-14 2.9e-15
342  IEEE 0,100 300000 9.9e-14 1.5e-14
343 
344 Cephes Math Library Release 2.8: June, 2000
345 Copyright 1985, 1987, 2000 by Stephen L. Moshier
346 *************************************************************************/
347 double incompletegamma(const double a, const double x)
348 {
349  alglib_impl::ae_state _alglib_env_state;
350  alglib_impl::ae_state_init(&_alglib_env_state);
351  try
352  {
353  double result = alglib_impl::incompletegamma(a, x, &_alglib_env_state);
354  alglib_impl::ae_state_clear(&_alglib_env_state);
355  return *(reinterpret_cast<double*>(&result));
356  }
358  {
359  throw ap_error(_alglib_env_state.error_msg);
360  }
361 }
362 
363 /*************************************************************************
364 Complemented incomplete gamma integral
365 
366 The function is defined by
367 
368 
369  igamc(a,x) = 1 - igam(a,x)
370 
371  inf.
372  -
373  1 | | -t a-1
374  = ----- | e t dt.
375  - | |
376  | (a) -
377  x
378 
379 
380 In this implementation both arguments must be positive.
381 The integral is evaluated by either a power series or
382 continued fraction expansion, depending on the relative
383 values of a and x.
384 
385 ACCURACY:
386 
387 Tested at random a, x.
388  a x Relative error:
389 arithmetic domain domain # trials peak rms
390  IEEE 0.5,100 0,100 200000 1.9e-14 1.7e-15
391  IEEE 0.01,0.5 0,100 200000 1.4e-13 1.6e-15
392 
393 Cephes Math Library Release 2.8: June, 2000
394 Copyright 1985, 1987, 2000 by Stephen L. Moshier
395 *************************************************************************/
396 double incompletegammac(const double a, const double x)
397 {
398  alglib_impl::ae_state _alglib_env_state;
399  alglib_impl::ae_state_init(&_alglib_env_state);
400  try
401  {
402  double result = alglib_impl::incompletegammac(a, x, &_alglib_env_state);
403  alglib_impl::ae_state_clear(&_alglib_env_state);
404  return *(reinterpret_cast<double*>(&result));
405  }
407  {
408  throw ap_error(_alglib_env_state.error_msg);
409  }
410 }
411 
412 /*************************************************************************
413 Inverse of complemented incomplete gamma integral
414 
415 Given p, the function finds x such that
416 
417  igamc( a, x ) = p.
418 
419 Starting with the approximate value
420 
421  3
422  x = a t
423 
424  where
425 
426  t = 1 - d - ndtri(p) sqrt(d)
427 
428 and
429 
430  d = 1/9a,
431 
432 the routine performs up to 10 Newton iterations to find the
433 root of igamc(a,x) - p = 0.
434 
435 ACCURACY:
436 
437 Tested at random a, p in the intervals indicated.
438 
439  a p Relative error:
440 arithmetic domain domain # trials peak rms
441  IEEE 0.5,100 0,0.5 100000 1.0e-14 1.7e-15
442  IEEE 0.01,0.5 0,0.5 100000 9.0e-14 3.4e-15
443  IEEE 0.5,10000 0,0.5 20000 2.3e-13 3.8e-14
444 
445 Cephes Math Library Release 2.8: June, 2000
446 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
447 *************************************************************************/
448 double invincompletegammac(const double a, const double y0)
449 {
450  alglib_impl::ae_state _alglib_env_state;
451  alglib_impl::ae_state_init(&_alglib_env_state);
452  try
453  {
454  double result = alglib_impl::invincompletegammac(a, y0, &_alglib_env_state);
455  alglib_impl::ae_state_clear(&_alglib_env_state);
456  return *(reinterpret_cast<double*>(&result));
457  }
459  {
460  throw ap_error(_alglib_env_state.error_msg);
461  }
462 }
463 
464 /*************************************************************************
465 Airy function
466 
467 Solution of the differential equation
468 
469 y"(x) = xy.
470 
471 The function returns the two independent solutions Ai, Bi
472 and their first derivatives Ai'(x), Bi'(x).
473 
474 Evaluation is by power series summation for small x,
475 by rational minimax approximations for large x.
476 
477 
478 
479 ACCURACY:
480 Error criterion is absolute when function <= 1, relative
481 when function > 1, except * denotes relative error criterion.
482 For large negative x, the absolute error increases as x^1.5.
483 For large positive x, the relative error increases as x^1.5.
484 
485 Arithmetic domain function # trials peak rms
486 IEEE -10, 0 Ai 10000 1.6e-15 2.7e-16
487 IEEE 0, 10 Ai 10000 2.3e-14* 1.8e-15*
488 IEEE -10, 0 Ai' 10000 4.6e-15 7.6e-16
489 IEEE 0, 10 Ai' 10000 1.8e-14* 1.5e-15*
490 IEEE -10, 10 Bi 30000 4.2e-15 5.3e-16
491 IEEE -10, 10 Bi' 30000 4.9e-15 7.3e-16
492 
493 Cephes Math Library Release 2.8: June, 2000
494 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
495 *************************************************************************/
496 void airy(const double x, double &ai, double &aip, double &bi, double &bip)
497 {
498  alglib_impl::ae_state _alglib_env_state;
499  alglib_impl::ae_state_init(&_alglib_env_state);
500  try
501  {
502  alglib_impl::airy(x, &ai, &aip, &bi, &bip, &_alglib_env_state);
503  alglib_impl::ae_state_clear(&_alglib_env_state);
504  return;
505  }
507  {
508  throw ap_error(_alglib_env_state.error_msg);
509  }
510 }
511 
512 /*************************************************************************
513 Bessel function of order zero
514 
515 Returns Bessel function of order zero of the argument.
516 
517 The domain is divided into the intervals [0, 5] and
518 (5, infinity). In the first interval the following rational
519 approximation is used:
520 
521 
522  2 2
523 (w - r ) (w - r ) P (w) / Q (w)
524  1 2 3 8
525 
526  2
527 where w = x and the two r's are zeros of the function.
528 
529 In the second interval, the Hankel asymptotic expansion
530 is employed with two rational functions of degree 6/6
531 and 7/7.
532 
533 ACCURACY:
534 
535  Absolute error:
536 arithmetic domain # trials peak rms
537  IEEE 0, 30 60000 4.2e-16 1.1e-16
538 
539 Cephes Math Library Release 2.8: June, 2000
540 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
541 *************************************************************************/
542 double besselj0(const double x)
543 {
544  alglib_impl::ae_state _alglib_env_state;
545  alglib_impl::ae_state_init(&_alglib_env_state);
546  try
547  {
548  double result = alglib_impl::besselj0(x, &_alglib_env_state);
549  alglib_impl::ae_state_clear(&_alglib_env_state);
550  return *(reinterpret_cast<double*>(&result));
551  }
553  {
554  throw ap_error(_alglib_env_state.error_msg);
555  }
556 }
557 
558 /*************************************************************************
559 Bessel function of order one
560 
561 Returns Bessel function of order one of the argument.
562 
563 The domain is divided into the intervals [0, 8] and
564 (8, infinity). In the first interval a 24 term Chebyshev
565 expansion is used. In the second, the asymptotic
566 trigonometric representation is employed using two
567 rational functions of degree 5/5.
568 
569 ACCURACY:
570 
571  Absolute error:
572 arithmetic domain # trials peak rms
573  IEEE 0, 30 30000 2.6e-16 1.1e-16
574 
575 Cephes Math Library Release 2.8: June, 2000
576 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
577 *************************************************************************/
578 double besselj1(const double x)
579 {
580  alglib_impl::ae_state _alglib_env_state;
581  alglib_impl::ae_state_init(&_alglib_env_state);
582  try
583  {
584  double result = alglib_impl::besselj1(x, &_alglib_env_state);
585  alglib_impl::ae_state_clear(&_alglib_env_state);
586  return *(reinterpret_cast<double*>(&result));
587  }
589  {
590  throw ap_error(_alglib_env_state.error_msg);
591  }
592 }
593 
594 /*************************************************************************
595 Bessel function of integer order
596 
597 Returns Bessel function of order n, where n is a
598 (possibly negative) integer.
599 
600 The ratio of jn(x) to j0(x) is computed by backward
601 recurrence. First the ratio jn/jn-1 is found by a
602 continued fraction expansion. Then the recurrence
603 relating successive orders is applied until j0 or j1 is
604 reached.
605 
606 If n = 0 or 1 the routine for j0 or j1 is called
607 directly.
608 
609 ACCURACY:
610 
611  Absolute error:
612 arithmetic range # trials peak rms
613  IEEE 0, 30 5000 4.4e-16 7.9e-17
614 
615 
616 Not suitable for large n or x. Use jv() (fractional order) instead.
617 
618 Cephes Math Library Release 2.8: June, 2000
619 Copyright 1984, 1987, 2000 by Stephen L. Moshier
620 *************************************************************************/
621 double besseljn(const ae_int_t n, const double x)
622 {
623  alglib_impl::ae_state _alglib_env_state;
624  alglib_impl::ae_state_init(&_alglib_env_state);
625  try
626  {
627  double result = alglib_impl::besseljn(n, x, &_alglib_env_state);
628  alglib_impl::ae_state_clear(&_alglib_env_state);
629  return *(reinterpret_cast<double*>(&result));
630  }
632  {
633  throw ap_error(_alglib_env_state.error_msg);
634  }
635 }
636 
637 /*************************************************************************
638 Bessel function of the second kind, order zero
639 
640 Returns Bessel function of the second kind, of order
641 zero, of the argument.
642 
643 The domain is divided into the intervals [0, 5] and
644 (5, infinity). In the first interval a rational approximation
645 R(x) is employed to compute
646  y0(x) = R(x) + 2 * log(x) * j0(x) / PI.
647 Thus a call to j0() is required.
648 
649 In the second interval, the Hankel asymptotic expansion
650 is employed with two rational functions of degree 6/6
651 and 7/7.
652 
653 
654 
655 ACCURACY:
656 
657  Absolute error, when y0(x) < 1; else relative error:
658 
659 arithmetic domain # trials peak rms
660  IEEE 0, 30 30000 1.3e-15 1.6e-16
661 
662 Cephes Math Library Release 2.8: June, 2000
663 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
664 *************************************************************************/
665 double bessely0(const double x)
666 {
667  alglib_impl::ae_state _alglib_env_state;
668  alglib_impl::ae_state_init(&_alglib_env_state);
669  try
670  {
671  double result = alglib_impl::bessely0(x, &_alglib_env_state);
672  alglib_impl::ae_state_clear(&_alglib_env_state);
673  return *(reinterpret_cast<double*>(&result));
674  }
676  {
677  throw ap_error(_alglib_env_state.error_msg);
678  }
679 }
680 
681 /*************************************************************************
682 Bessel function of second kind of order one
683 
684 Returns Bessel function of the second kind of order one
685 of the argument.
686 
687 The domain is divided into the intervals [0, 8] and
688 (8, infinity). In the first interval a 25 term Chebyshev
689 expansion is used, and a call to j1() is required.
690 In the second, the asymptotic trigonometric representation
691 is employed using two rational functions of degree 5/5.
692 
693 ACCURACY:
694 
695  Absolute error:
696 arithmetic domain # trials peak rms
697  IEEE 0, 30 30000 1.0e-15 1.3e-16
698 
699 Cephes Math Library Release 2.8: June, 2000
700 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
701 *************************************************************************/
702 double bessely1(const double x)
703 {
704  alglib_impl::ae_state _alglib_env_state;
705  alglib_impl::ae_state_init(&_alglib_env_state);
706  try
707  {
708  double result = alglib_impl::bessely1(x, &_alglib_env_state);
709  alglib_impl::ae_state_clear(&_alglib_env_state);
710  return *(reinterpret_cast<double*>(&result));
711  }
713  {
714  throw ap_error(_alglib_env_state.error_msg);
715  }
716 }
717 
718 /*************************************************************************
719 Bessel function of second kind of integer order
720 
721 Returns Bessel function of order n, where n is a
722 (possibly negative) integer.
723 
724 The function is evaluated by forward recurrence on
725 n, starting with values computed by the routines
726 y0() and y1().
727 
728 If n = 0 or 1 the routine for y0 or y1 is called
729 directly.
730 
731 ACCURACY:
732  Absolute error, except relative
733  when y > 1:
734 arithmetic domain # trials peak rms
735  IEEE 0, 30 30000 3.4e-15 4.3e-16
736 
737 Cephes Math Library Release 2.8: June, 2000
738 Copyright 1984, 1987, 2000 by Stephen L. Moshier
739 *************************************************************************/
740 double besselyn(const ae_int_t n, const double x)
741 {
742  alglib_impl::ae_state _alglib_env_state;
743  alglib_impl::ae_state_init(&_alglib_env_state);
744  try
745  {
746  double result = alglib_impl::besselyn(n, x, &_alglib_env_state);
747  alglib_impl::ae_state_clear(&_alglib_env_state);
748  return *(reinterpret_cast<double*>(&result));
749  }
751  {
752  throw ap_error(_alglib_env_state.error_msg);
753  }
754 }
755 
756 /*************************************************************************
757 Modified Bessel function of order zero
758 
759 Returns modified Bessel function of order zero of the
760 argument.
761 
762 The function is defined as i0(x) = j0( ix ).
763 
764 The range is partitioned into the two intervals [0,8] and
765 (8, infinity). Chebyshev polynomial expansions are employed
766 in each interval.
767 
768 ACCURACY:
769 
770  Relative error:
771 arithmetic domain # trials peak rms
772  IEEE 0,30 30000 5.8e-16 1.4e-16
773 
774 Cephes Math Library Release 2.8: June, 2000
775 Copyright 1984, 1987, 2000 by Stephen L. Moshier
776 *************************************************************************/
777 double besseli0(const double x)
778 {
779  alglib_impl::ae_state _alglib_env_state;
780  alglib_impl::ae_state_init(&_alglib_env_state);
781  try
782  {
783  double result = alglib_impl::besseli0(x, &_alglib_env_state);
784  alglib_impl::ae_state_clear(&_alglib_env_state);
785  return *(reinterpret_cast<double*>(&result));
786  }
788  {
789  throw ap_error(_alglib_env_state.error_msg);
790  }
791 }
792 
793 /*************************************************************************
794 Modified Bessel function of order one
795 
796 Returns modified Bessel function of order one of the
797 argument.
798 
799 The function is defined as i1(x) = -i j1( ix ).
800 
801 The range is partitioned into the two intervals [0,8] and
802 (8, infinity). Chebyshev polynomial expansions are employed
803 in each interval.
804 
805 ACCURACY:
806 
807  Relative error:
808 arithmetic domain # trials peak rms
809  IEEE 0, 30 30000 1.9e-15 2.1e-16
810 
811 Cephes Math Library Release 2.8: June, 2000
812 Copyright 1985, 1987, 2000 by Stephen L. Moshier
813 *************************************************************************/
814 double besseli1(const double x)
815 {
816  alglib_impl::ae_state _alglib_env_state;
817  alglib_impl::ae_state_init(&_alglib_env_state);
818  try
819  {
820  double result = alglib_impl::besseli1(x, &_alglib_env_state);
821  alglib_impl::ae_state_clear(&_alglib_env_state);
822  return *(reinterpret_cast<double*>(&result));
823  }
825  {
826  throw ap_error(_alglib_env_state.error_msg);
827  }
828 }
829 
830 /*************************************************************************
831 Modified Bessel function, second kind, order zero
832 
833 Returns modified Bessel function of the second kind
834 of order zero of the argument.
835 
836 The range is partitioned into the two intervals [0,8] and
837 (8, infinity). Chebyshev polynomial expansions are employed
838 in each interval.
839 
840 ACCURACY:
841 
842 Tested at 2000 random points between 0 and 8. Peak absolute
843 error (relative when K0 > 1) was 1.46e-14; rms, 4.26e-15.
844  Relative error:
845 arithmetic domain # trials peak rms
846  IEEE 0, 30 30000 1.2e-15 1.6e-16
847 
848 Cephes Math Library Release 2.8: June, 2000
849 Copyright 1984, 1987, 2000 by Stephen L. Moshier
850 *************************************************************************/
851 double besselk0(const double x)
852 {
853  alglib_impl::ae_state _alglib_env_state;
854  alglib_impl::ae_state_init(&_alglib_env_state);
855  try
856  {
857  double result = alglib_impl::besselk0(x, &_alglib_env_state);
858  alglib_impl::ae_state_clear(&_alglib_env_state);
859  return *(reinterpret_cast<double*>(&result));
860  }
862  {
863  throw ap_error(_alglib_env_state.error_msg);
864  }
865 }
866 
867 /*************************************************************************
868 Modified Bessel function, second kind, order one
869 
870 Computes the modified Bessel function of the second kind
871 of order one of the argument.
872 
873 The range is partitioned into the two intervals [0,2] and
874 (2, infinity). Chebyshev polynomial expansions are employed
875 in each interval.
876 
877 ACCURACY:
878 
879  Relative error:
880 arithmetic domain # trials peak rms
881  IEEE 0, 30 30000 1.2e-15 1.6e-16
882 
883 Cephes Math Library Release 2.8: June, 2000
884 Copyright 1984, 1987, 2000 by Stephen L. Moshier
885 *************************************************************************/
886 double besselk1(const double x)
887 {
888  alglib_impl::ae_state _alglib_env_state;
889  alglib_impl::ae_state_init(&_alglib_env_state);
890  try
891  {
892  double result = alglib_impl::besselk1(x, &_alglib_env_state);
893  alglib_impl::ae_state_clear(&_alglib_env_state);
894  return *(reinterpret_cast<double*>(&result));
895  }
897  {
898  throw ap_error(_alglib_env_state.error_msg);
899  }
900 }
901 
902 /*************************************************************************
903 Modified Bessel function, second kind, integer order
904 
905 Returns modified Bessel function of the second kind
906 of order n of the argument.
907 
908 The range is partitioned into the two intervals [0,9.55] and
909 (9.55, infinity). An ascending power series is used in the
910 low range, and an asymptotic expansion in the high range.
911 
912 ACCURACY:
913 
914  Relative error:
915 arithmetic domain # trials peak rms
916  IEEE 0,30 90000 1.8e-8 3.0e-10
917 
918 Error is high only near the crossover point x = 9.55
919 between the two expansions used.
920 
921 Cephes Math Library Release 2.8: June, 2000
922 Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
923 *************************************************************************/
924 double besselkn(const ae_int_t nn, const double x)
925 {
926  alglib_impl::ae_state _alglib_env_state;
927  alglib_impl::ae_state_init(&_alglib_env_state);
928  try
929  {
930  double result = alglib_impl::besselkn(nn, x, &_alglib_env_state);
931  alglib_impl::ae_state_clear(&_alglib_env_state);
932  return *(reinterpret_cast<double*>(&result));
933  }
935  {
936  throw ap_error(_alglib_env_state.error_msg);
937  }
938 }
939 
940 /*************************************************************************
941 Beta function
942 
943 
944  - -
945  | (a) | (b)
946 beta( a, b ) = -----------.
947  -
948  | (a+b)
949 
950 For large arguments the logarithm of the function is
951 evaluated using lgam(), then exponentiated.
952 
953 ACCURACY:
954 
955  Relative error:
956 arithmetic domain # trials peak rms
957  IEEE 0,30 30000 8.1e-14 1.1e-14
958 
959 Cephes Math Library Release 2.0: April, 1987
960 Copyright 1984, 1987 by Stephen L. Moshier
961 *************************************************************************/
962 double beta(const double a, const double b)
963 {
964  alglib_impl::ae_state _alglib_env_state;
965  alglib_impl::ae_state_init(&_alglib_env_state);
966  try
967  {
968  double result = alglib_impl::beta(a, b, &_alglib_env_state);
969  alglib_impl::ae_state_clear(&_alglib_env_state);
970  return *(reinterpret_cast<double*>(&result));
971  }
973  {
974  throw ap_error(_alglib_env_state.error_msg);
975  }
976 }
977 
978 /*************************************************************************
979 Incomplete beta integral
980 
981 Returns incomplete beta integral of the arguments, evaluated
982 from zero to x. The function is defined as
983 
984  x
985  - -
986  | (a+b) | | a-1 b-1
987  ----------- | t (1-t) dt.
988  - - | |
989  | (a) | (b) -
990  0
991 
992 The domain of definition is 0 <= x <= 1. In this
993 implementation a and b are restricted to positive values.
994 The integral from x to 1 may be obtained by the symmetry
995 relation
996 
997  1 - incbet( a, b, x ) = incbet( b, a, 1-x ).
998 
999 The integral is evaluated by a continued fraction expansion
1000 or, when b*x is small, by a power series.
1001 
1002 ACCURACY:
1003 
1004 Tested at uniformly distributed random points (a,b,x) with a and b
1005 in "domain" and x between 0 and 1.
1006  Relative error
1007 arithmetic domain # trials peak rms
1008  IEEE 0,5 10000 6.9e-15 4.5e-16
1009  IEEE 0,85 250000 2.2e-13 1.7e-14
1010  IEEE 0,1000 30000 5.3e-12 6.3e-13
1011  IEEE 0,10000 250000 9.3e-11 7.1e-12
1012  IEEE 0,100000 10000 8.7e-10 4.8e-11
1013 Outputs smaller than the IEEE gradual underflow threshold
1014 were excluded from these statistics.
1015 
1016 Cephes Math Library, Release 2.8: June, 2000
1017 Copyright 1984, 1995, 2000 by Stephen L. Moshier
1018 *************************************************************************/
1019 double incompletebeta(const double a, const double b, const double x)
1020 {
1021  alglib_impl::ae_state _alglib_env_state;
1022  alglib_impl::ae_state_init(&_alglib_env_state);
1023  try
1024  {
1025  double result = alglib_impl::incompletebeta(a, b, x, &_alglib_env_state);
1026  alglib_impl::ae_state_clear(&_alglib_env_state);
1027  return *(reinterpret_cast<double*>(&result));
1028  }
1030  {
1031  throw ap_error(_alglib_env_state.error_msg);
1032  }
1033 }
1034 
1035 /*************************************************************************
1036 Inverse of incomplete beta integral
1037 
1038 Given y, the function finds x such that
1039 
1040  incbet( a, b, x ) = y .
1041 
1042 The routine performs interval halving or Newton iterations to find the
1043 root of incbet(a,b,x) - y = 0.
1044 
1045 
1046 ACCURACY:
1047 
1048  Relative error:
1049  x a,b
1050 arithmetic domain domain # trials peak rms
1051  IEEE 0,1 .5,10000 50000 5.8e-12 1.3e-13
1052  IEEE 0,1 .25,100 100000 1.8e-13 3.9e-15
1053  IEEE 0,1 0,5 50000 1.1e-12 5.5e-15
1054 With a and b constrained to half-integer or integer values:
1055  IEEE 0,1 .5,10000 50000 5.8e-12 1.1e-13
1056  IEEE 0,1 .5,100 100000 1.7e-14 7.9e-16
1057 With a = .5, b constrained to half-integer or integer values:
1058  IEEE 0,1 .5,10000 10000 8.3e-11 1.0e-11
1059 
1060 Cephes Math Library Release 2.8: June, 2000
1061 Copyright 1984, 1996, 2000 by Stephen L. Moshier
1062 *************************************************************************/
1063 double invincompletebeta(const double a, const double b, const double y)
1064 {
1065  alglib_impl::ae_state _alglib_env_state;
1066  alglib_impl::ae_state_init(&_alglib_env_state);
1067  try
1068  {
1069  double result = alglib_impl::invincompletebeta(a, b, y, &_alglib_env_state);
1070  alglib_impl::ae_state_clear(&_alglib_env_state);
1071  return *(reinterpret_cast<double*>(&result));
1072  }
1074  {
1075  throw ap_error(_alglib_env_state.error_msg);
1076  }
1077 }
1078 
1079 /*************************************************************************
1080 Binomial distribution
1081 
1082 Returns the sum of the terms 0 through k of the Binomial
1083 probability density:
1084 
1085  k
1086  -- ( n ) j n-j
1087  > ( ) p (1-p)
1088  -- ( j )
1089  j=0
1090 
1091 The terms are not summed directly; instead the incomplete
1092 beta integral is employed, according to the formula
1093 
1094 y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
1095 
1096 The arguments must be positive, with p ranging from 0 to 1.
1097 
1098 ACCURACY:
1099 
1100 Tested at random points (a,b,p), with p between 0 and 1.
1101 
1102  a,b Relative error:
1103 arithmetic domain # trials peak rms
1104  For p between 0.001 and 1:
1105  IEEE 0,100 100000 4.3e-15 2.6e-16
1106 
1107 Cephes Math Library Release 2.8: June, 2000
1108 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1109 *************************************************************************/
1110 double binomialdistribution(const ae_int_t k, const ae_int_t n, const double p)
1111 {
1112  alglib_impl::ae_state _alglib_env_state;
1113  alglib_impl::ae_state_init(&_alglib_env_state);
1114  try
1115  {
1116  double result = alglib_impl::binomialdistribution(k, n, p, &_alglib_env_state);
1117  alglib_impl::ae_state_clear(&_alglib_env_state);
1118  return *(reinterpret_cast<double*>(&result));
1119  }
1121  {
1122  throw ap_error(_alglib_env_state.error_msg);
1123  }
1124 }
1125 
1126 /*************************************************************************
1127 Complemented binomial distribution
1128 
1129 Returns the sum of the terms k+1 through n of the Binomial
1130 probability density:
1131 
1132  n
1133  -- ( n ) j n-j
1134  > ( ) p (1-p)
1135  -- ( j )
1136  j=k+1
1137 
1138 The terms are not summed directly; instead the incomplete
1139 beta integral is employed, according to the formula
1140 
1141 y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
1142 
1143 The arguments must be positive, with p ranging from 0 to 1.
1144 
1145 ACCURACY:
1146 
1147 Tested at random points (a,b,p).
1148 
1149  a,b Relative error:
1150 arithmetic domain # trials peak rms
1151  For p between 0.001 and 1:
1152  IEEE 0,100 100000 6.7e-15 8.2e-16
1153  For p between 0 and .001:
1154  IEEE 0,100 100000 1.5e-13 2.7e-15
1155 
1156 Cephes Math Library Release 2.8: June, 2000
1157 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1158 *************************************************************************/
1159 double binomialcdistribution(const ae_int_t k, const ae_int_t n, const double p)
1160 {
1161  alglib_impl::ae_state _alglib_env_state;
1162  alglib_impl::ae_state_init(&_alglib_env_state);
1163  try
1164  {
1165  double result = alglib_impl::binomialcdistribution(k, n, p, &_alglib_env_state);
1166  alglib_impl::ae_state_clear(&_alglib_env_state);
1167  return *(reinterpret_cast<double*>(&result));
1168  }
1170  {
1171  throw ap_error(_alglib_env_state.error_msg);
1172  }
1173 }
1174 
1175 /*************************************************************************
1176 Inverse binomial distribution
1177 
1178 Finds the event probability p such that the sum of the
1179 terms 0 through k of the Binomial probability density
1180 is equal to the given cumulative probability y.
1181 
1182 This is accomplished using the inverse beta integral
1183 function and the relation
1184 
1185 1 - p = incbi( n-k, k+1, y ).
1186 
1187 ACCURACY:
1188 
1189 Tested at random points (a,b,p).
1190 
1191  a,b Relative error:
1192 arithmetic domain # trials peak rms
1193  For p between 0.001 and 1:
1194  IEEE 0,100 100000 2.3e-14 6.4e-16
1195  IEEE 0,10000 100000 6.6e-12 1.2e-13
1196  For p between 10^-6 and 0.001:
1197  IEEE 0,100 100000 2.0e-12 1.3e-14
1198  IEEE 0,10000 100000 1.5e-12 3.2e-14
1199 
1200 Cephes Math Library Release 2.8: June, 2000
1201 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1202 *************************************************************************/
1203 double invbinomialdistribution(const ae_int_t k, const ae_int_t n, const double y)
1204 {
1205  alglib_impl::ae_state _alglib_env_state;
1206  alglib_impl::ae_state_init(&_alglib_env_state);
1207  try
1208  {
1209  double result = alglib_impl::invbinomialdistribution(k, n, y, &_alglib_env_state);
1210  alglib_impl::ae_state_clear(&_alglib_env_state);
1211  return *(reinterpret_cast<double*>(&result));
1212  }
1214  {
1215  throw ap_error(_alglib_env_state.error_msg);
1216  }
1217 }
1218 
1219 /*************************************************************************
1220 Calculation of the value of the Chebyshev polynomials of the
1221 first and second kinds.
1222 
1223 Parameters:
1224  r - polynomial kind, either 1 or 2.
1225  n - degree, n>=0
1226  x - argument, -1 <= x <= 1
1227 
1228 Result:
1229  the value of the Chebyshev polynomial at x
1230 *************************************************************************/
1231 double chebyshevcalculate(const ae_int_t r, const ae_int_t n, const double x)
1232 {
1233  alglib_impl::ae_state _alglib_env_state;
1234  alglib_impl::ae_state_init(&_alglib_env_state);
1235  try
1236  {
1237  double result = alglib_impl::chebyshevcalculate(r, n, x, &_alglib_env_state);
1238  alglib_impl::ae_state_clear(&_alglib_env_state);
1239  return *(reinterpret_cast<double*>(&result));
1240  }
1242  {
1243  throw ap_error(_alglib_env_state.error_msg);
1244  }
1245 }
1246 
1247 /*************************************************************************
1248 Summation of Chebyshev polynomials using Clenshaw’s recurrence formula.
1249 
1250 This routine calculates
1251  c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
1252 or
1253  c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
1254 depending on the R.
1255 
1256 Parameters:
1257  r - polynomial kind, either 1 or 2.
1258  n - degree, n>=0
1259  x - argument
1260 
1261 Result:
1262  the value of the Chebyshev polynomial at x
1263 *************************************************************************/
1264 double chebyshevsum(const real_1d_array &c, const ae_int_t r, const ae_int_t n, const double x)
1265 {
1266  alglib_impl::ae_state _alglib_env_state;
1267  alglib_impl::ae_state_init(&_alglib_env_state);
1268  try
1269  {
1270  double result = alglib_impl::chebyshevsum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), r, n, x, &_alglib_env_state);
1271  alglib_impl::ae_state_clear(&_alglib_env_state);
1272  return *(reinterpret_cast<double*>(&result));
1273  }
1275  {
1276  throw ap_error(_alglib_env_state.error_msg);
1277  }
1278 }
1279 
1280 /*************************************************************************
1281 Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
1282 
1283 Input parameters:
1284  N - polynomial degree, n>=0
1285 
1286 Output parameters:
1287  C - coefficients
1288 *************************************************************************/
1290 {
1291  alglib_impl::ae_state _alglib_env_state;
1292  alglib_impl::ae_state_init(&_alglib_env_state);
1293  try
1294  {
1295  alglib_impl::chebyshevcoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
1296  alglib_impl::ae_state_clear(&_alglib_env_state);
1297  return;
1298  }
1300  {
1301  throw ap_error(_alglib_env_state.error_msg);
1302  }
1303 }
1304 
1305 /*************************************************************************
1306 Conversion of a series of Chebyshev polynomials to a power series.
1307 
1308 Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
1309 B[0] + B[1]*X + ... + B[N]*X^N.
1310 
1311 Input parameters:
1312  A - Chebyshev series coefficients
1313  N - degree, N>=0
1314 
1315 Output parameters
1316  B - power series coefficients
1317 *************************************************************************/
1319 {
1320  alglib_impl::ae_state _alglib_env_state;
1321  alglib_impl::ae_state_init(&_alglib_env_state);
1322  try
1323  {
1324  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);
1325  alglib_impl::ae_state_clear(&_alglib_env_state);
1326  return;
1327  }
1329  {
1330  throw ap_error(_alglib_env_state.error_msg);
1331  }
1332 }
1333 
1334 /*************************************************************************
1335 Chi-square distribution
1336 
1337 Returns the area under the left hand tail (from 0 to x)
1338 of the Chi square probability density function with
1339 v degrees of freedom.
1340 
1341 
1342  x
1343  -
1344  1 | | v/2-1 -t/2
1345  P( x | v ) = ----------- | t e dt
1346  v/2 - | |
1347  2 | (v/2) -
1348  0
1349 
1350 where x is the Chi-square variable.
1351 
1352 The incomplete gamma integral is used, according to the
1353 formula
1354 
1355 y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
1356 
1357 The arguments must both be positive.
1358 
1359 ACCURACY:
1360 
1361 See incomplete gamma function
1362 
1363 
1364 Cephes Math Library Release 2.8: June, 2000
1365 Copyright 1984, 1987, 2000 by Stephen L. Moshier
1366 *************************************************************************/
1367 double chisquaredistribution(const double v, const double x)
1368 {
1369  alglib_impl::ae_state _alglib_env_state;
1370  alglib_impl::ae_state_init(&_alglib_env_state);
1371  try
1372  {
1373  double result = alglib_impl::chisquaredistribution(v, x, &_alglib_env_state);
1374  alglib_impl::ae_state_clear(&_alglib_env_state);
1375  return *(reinterpret_cast<double*>(&result));
1376  }
1378  {
1379  throw ap_error(_alglib_env_state.error_msg);
1380  }
1381 }
1382 
1383 /*************************************************************************
1384 Complemented Chi-square distribution
1385 
1386 Returns the area under the right hand tail (from x to
1387 infinity) of the Chi square probability density function
1388 with v degrees of freedom:
1389 
1390  inf.
1391  -
1392  1 | | v/2-1 -t/2
1393  P( x | v ) = ----------- | t e dt
1394  v/2 - | |
1395  2 | (v/2) -
1396  x
1397 
1398 where x is the Chi-square variable.
1399 
1400 The incomplete gamma integral is used, according to the
1401 formula
1402 
1403 y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
1404 
1405 The arguments must both be positive.
1406 
1407 ACCURACY:
1408 
1409 See incomplete gamma function
1410 
1411 Cephes Math Library Release 2.8: June, 2000
1412 Copyright 1984, 1987, 2000 by Stephen L. Moshier
1413 *************************************************************************/
1414 double chisquarecdistribution(const double v, const double x)
1415 {
1416  alglib_impl::ae_state _alglib_env_state;
1417  alglib_impl::ae_state_init(&_alglib_env_state);
1418  try
1419  {
1420  double result = alglib_impl::chisquarecdistribution(v, x, &_alglib_env_state);
1421  alglib_impl::ae_state_clear(&_alglib_env_state);
1422  return *(reinterpret_cast<double*>(&result));
1423  }
1425  {
1426  throw ap_error(_alglib_env_state.error_msg);
1427  }
1428 }
1429 
1430 /*************************************************************************
1431 Inverse of complemented Chi-square distribution
1432 
1433 Finds the Chi-square argument x such that the integral
1434 from x to infinity of the Chi-square density is equal
1435 to the given cumulative probability y.
1436 
1437 This is accomplished using the inverse gamma integral
1438 function and the relation
1439 
1440  x/2 = igami( df/2, y );
1441 
1442 ACCURACY:
1443 
1444 See inverse incomplete gamma function
1445 
1446 
1447 Cephes Math Library Release 2.8: June, 2000
1448 Copyright 1984, 1987, 2000 by Stephen L. Moshier
1449 *************************************************************************/
1450 double invchisquaredistribution(const double v, const double y)
1451 {
1452  alglib_impl::ae_state _alglib_env_state;
1453  alglib_impl::ae_state_init(&_alglib_env_state);
1454  try
1455  {
1456  double result = alglib_impl::invchisquaredistribution(v, y, &_alglib_env_state);
1457  alglib_impl::ae_state_clear(&_alglib_env_state);
1458  return *(reinterpret_cast<double*>(&result));
1459  }
1461  {
1462  throw ap_error(_alglib_env_state.error_msg);
1463  }
1464 }
1465 
1466 /*************************************************************************
1467 Dawson's Integral
1468 
1469 Approximates the integral
1470 
1471  x
1472  -
1473  2 | | 2
1474  dawsn(x) = exp( -x ) | exp( t ) dt
1475  | |
1476  -
1477  0
1478 
1479 Three different rational approximations are employed, for
1480 the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
1481 
1482 ACCURACY:
1483 
1484  Relative error:
1485 arithmetic domain # trials peak rms
1486  IEEE 0,10 10000 6.9e-16 1.0e-16
1487 
1488 Cephes Math Library Release 2.8: June, 2000
1489 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1490 *************************************************************************/
1491 double dawsonintegral(const double x)
1492 {
1493  alglib_impl::ae_state _alglib_env_state;
1494  alglib_impl::ae_state_init(&_alglib_env_state);
1495  try
1496  {
1497  double result = alglib_impl::dawsonintegral(x, &_alglib_env_state);
1498  alglib_impl::ae_state_clear(&_alglib_env_state);
1499  return *(reinterpret_cast<double*>(&result));
1500  }
1502  {
1503  throw ap_error(_alglib_env_state.error_msg);
1504  }
1505 }
1506 
1507 /*************************************************************************
1508 Complete elliptic integral of the first kind
1509 
1510 Approximates the integral
1511 
1512 
1513 
1514  pi/2
1515  -
1516  | |
1517  | dt
1518 K(m) = | ------------------
1519  | 2
1520  | | sqrt( 1 - m sin t )
1521  -
1522  0
1523 
1524 using the approximation
1525 
1526  P(x) - log x Q(x).
1527 
1528 ACCURACY:
1529 
1530  Relative error:
1531 arithmetic domain # trials peak rms
1532  IEEE 0,1 30000 2.5e-16 6.8e-17
1533 
1534 Cephes Math Library, Release 2.8: June, 2000
1535 Copyright 1984, 1987, 2000 by Stephen L. Moshier
1536 *************************************************************************/
1537 double ellipticintegralk(const double m)
1538 {
1539  alglib_impl::ae_state _alglib_env_state;
1540  alglib_impl::ae_state_init(&_alglib_env_state);
1541  try
1542  {
1543  double result = alglib_impl::ellipticintegralk(m, &_alglib_env_state);
1544  alglib_impl::ae_state_clear(&_alglib_env_state);
1545  return *(reinterpret_cast<double*>(&result));
1546  }
1548  {
1549  throw ap_error(_alglib_env_state.error_msg);
1550  }
1551 }
1552 
1553 /*************************************************************************
1554 Complete elliptic integral of the first kind
1555 
1556 Approximates the integral
1557 
1558 
1559 
1560  pi/2
1561  -
1562  | |
1563  | dt
1564 K(m) = | ------------------
1565  | 2
1566  | | sqrt( 1 - m sin t )
1567  -
1568  0
1569 
1570 where m = 1 - m1, using the approximation
1571 
1572  P(x) - log x Q(x).
1573 
1574 The argument m1 is used rather than m so that the logarithmic
1575 singularity at m = 1 will be shifted to the origin; this
1576 preserves maximum accuracy.
1577 
1578 K(0) = pi/2.
1579 
1580 ACCURACY:
1581 
1582  Relative error:
1583 arithmetic domain # trials peak rms
1584  IEEE 0,1 30000 2.5e-16 6.8e-17
1585 
1586 Àëãîðèòì âçÿò èç áèáëèîòåêè Cephes
1587 *************************************************************************/
1588 double ellipticintegralkhighprecision(const double m1)
1589 {
1590  alglib_impl::ae_state _alglib_env_state;
1591  alglib_impl::ae_state_init(&_alglib_env_state);
1592  try
1593  {
1594  double result = alglib_impl::ellipticintegralkhighprecision(m1, &_alglib_env_state);
1595  alglib_impl::ae_state_clear(&_alglib_env_state);
1596  return *(reinterpret_cast<double*>(&result));
1597  }
1599  {
1600  throw ap_error(_alglib_env_state.error_msg);
1601  }
1602 }
1603 
1604 /*************************************************************************
1605 Incomplete elliptic integral of the first kind F(phi|m)
1606 
1607 Approximates the integral
1608 
1609 
1610 
1611  phi
1612  -
1613  | |
1614  | dt
1615 F(phi_\m) = | ------------------
1616  | 2
1617  | | sqrt( 1 - m sin t )
1618  -
1619  0
1620 
1621 of amplitude phi and modulus m, using the arithmetic -
1622 geometric mean algorithm.
1623 
1624 
1625 
1626 
1627 ACCURACY:
1628 
1629 Tested at random points with m in [0, 1] and phi as indicated.
1630 
1631  Relative error:
1632 arithmetic domain # trials peak rms
1633  IEEE -10,10 200000 7.4e-16 1.0e-16
1634 
1635 Cephes Math Library Release 2.8: June, 2000
1636 Copyright 1984, 1987, 2000 by Stephen L. Moshier
1637 *************************************************************************/
1638 double incompleteellipticintegralk(const double phi, const double m)
1639 {
1640  alglib_impl::ae_state _alglib_env_state;
1641  alglib_impl::ae_state_init(&_alglib_env_state);
1642  try
1643  {
1644  double result = alglib_impl::incompleteellipticintegralk(phi, m, &_alglib_env_state);
1645  alglib_impl::ae_state_clear(&_alglib_env_state);
1646  return *(reinterpret_cast<double*>(&result));
1647  }
1649  {
1650  throw ap_error(_alglib_env_state.error_msg);
1651  }
1652 }
1653 
1654 /*************************************************************************
1655 Complete elliptic integral of the second kind
1656 
1657 Approximates the integral
1658 
1659 
1660  pi/2
1661  -
1662  | | 2
1663 E(m) = | sqrt( 1 - m sin t ) dt
1664  | |
1665  -
1666  0
1667 
1668 using the approximation
1669 
1670  P(x) - x log x Q(x).
1671 
1672 ACCURACY:
1673 
1674  Relative error:
1675 arithmetic domain # trials peak rms
1676  IEEE 0, 1 10000 2.1e-16 7.3e-17
1677 
1678 Cephes Math Library, Release 2.8: June, 2000
1679 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
1680 *************************************************************************/
1681 double ellipticintegrale(const double m)
1682 {
1683  alglib_impl::ae_state _alglib_env_state;
1684  alglib_impl::ae_state_init(&_alglib_env_state);
1685  try
1686  {
1687  double result = alglib_impl::ellipticintegrale(m, &_alglib_env_state);
1688  alglib_impl::ae_state_clear(&_alglib_env_state);
1689  return *(reinterpret_cast<double*>(&result));
1690  }
1692  {
1693  throw ap_error(_alglib_env_state.error_msg);
1694  }
1695 }
1696 
1697 /*************************************************************************
1698 Incomplete elliptic integral of the second kind
1699 
1700 Approximates the integral
1701 
1702 
1703  phi
1704  -
1705  | |
1706  | 2
1707 E(phi_\m) = | sqrt( 1 - m sin t ) dt
1708  |
1709  | |
1710  -
1711  0
1712 
1713 of amplitude phi and modulus m, using the arithmetic -
1714 geometric mean algorithm.
1715 
1716 ACCURACY:
1717 
1718 Tested at random arguments with phi in [-10, 10] and m in
1719 [0, 1].
1720  Relative error:
1721 arithmetic domain # trials peak rms
1722  IEEE -10,10 150000 3.3e-15 1.4e-16
1723 
1724 Cephes Math Library Release 2.8: June, 2000
1725 Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
1726 *************************************************************************/
1727 double incompleteellipticintegrale(const double phi, const double m)
1728 {
1729  alglib_impl::ae_state _alglib_env_state;
1730  alglib_impl::ae_state_init(&_alglib_env_state);
1731  try
1732  {
1733  double result = alglib_impl::incompleteellipticintegrale(phi, m, &_alglib_env_state);
1734  alglib_impl::ae_state_clear(&_alglib_env_state);
1735  return *(reinterpret_cast<double*>(&result));
1736  }
1738  {
1739  throw ap_error(_alglib_env_state.error_msg);
1740  }
1741 }
1742 
1743 /*************************************************************************
1744 Exponential integral Ei(x)
1745 
1746  x
1747  - t
1748  | | e
1749  Ei(x) = -|- --- dt .
1750  | | t
1751  -
1752  -inf
1753 
1754 Not defined for x <= 0.
1755 See also expn.c.
1756 
1757 
1758 
1759 ACCURACY:
1760 
1761  Relative error:
1762 arithmetic domain # trials peak rms
1763  IEEE 0,100 50000 8.6e-16 1.3e-16
1764 
1765 Cephes Math Library Release 2.8: May, 1999
1766 Copyright 1999 by Stephen L. Moshier
1767 *************************************************************************/
1768 double exponentialintegralei(const double x)
1769 {
1770  alglib_impl::ae_state _alglib_env_state;
1771  alglib_impl::ae_state_init(&_alglib_env_state);
1772  try
1773  {
1774  double result = alglib_impl::exponentialintegralei(x, &_alglib_env_state);
1775  alglib_impl::ae_state_clear(&_alglib_env_state);
1776  return *(reinterpret_cast<double*>(&result));
1777  }
1779  {
1780  throw ap_error(_alglib_env_state.error_msg);
1781  }
1782 }
1783 
1784 /*************************************************************************
1785 Exponential integral En(x)
1786 
1787 Evaluates the exponential integral
1788 
1789  inf.
1790  -
1791  | | -xt
1792  | e
1793  E (x) = | ---- dt.
1794  n | n
1795  | | t
1796  -
1797  1
1798 
1799 
1800 Both n and x must be nonnegative.
1801 
1802 The routine employs either a power series, a continued
1803 fraction, or an asymptotic formula depending on the
1804 relative values of n and x.
1805 
1806 ACCURACY:
1807 
1808  Relative error:
1809 arithmetic domain # trials peak rms
1810  IEEE 0, 30 10000 1.7e-15 3.6e-16
1811 
1812 Cephes Math Library Release 2.8: June, 2000
1813 Copyright 1985, 2000 by Stephen L. Moshier
1814 *************************************************************************/
1815 double exponentialintegralen(const double x, const ae_int_t n)
1816 {
1817  alglib_impl::ae_state _alglib_env_state;
1818  alglib_impl::ae_state_init(&_alglib_env_state);
1819  try
1820  {
1821  double result = alglib_impl::exponentialintegralen(x, n, &_alglib_env_state);
1822  alglib_impl::ae_state_clear(&_alglib_env_state);
1823  return *(reinterpret_cast<double*>(&result));
1824  }
1826  {
1827  throw ap_error(_alglib_env_state.error_msg);
1828  }
1829 }
1830 
1831 /*************************************************************************
1832 F distribution
1833 
1834 Returns the area from zero to x under the F density
1835 function (also known as Snedcor's density or the
1836 variance ratio density). This is the density
1837 of x = (u1/df1)/(u2/df2), where u1 and u2 are random
1838 variables having Chi square distributions with df1
1839 and df2 degrees of freedom, respectively.
1840 The incomplete beta integral is used, according to the
1841 formula
1842 
1843 P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
1844 
1845 
1846 The arguments a and b are greater than zero, and x is
1847 nonnegative.
1848 
1849 ACCURACY:
1850 
1851 Tested at random points (a,b,x).
1852 
1853  x a,b Relative error:
1854 arithmetic domain domain # trials peak rms
1855  IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
1856  IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
1857  IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
1858  IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
1859 
1860 Cephes Math Library Release 2.8: June, 2000
1861 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1862 *************************************************************************/
1863 double fdistribution(const ae_int_t a, const ae_int_t b, const double x)
1864 {
1865  alglib_impl::ae_state _alglib_env_state;
1866  alglib_impl::ae_state_init(&_alglib_env_state);
1867  try
1868  {
1869  double result = alglib_impl::fdistribution(a, b, x, &_alglib_env_state);
1870  alglib_impl::ae_state_clear(&_alglib_env_state);
1871  return *(reinterpret_cast<double*>(&result));
1872  }
1874  {
1875  throw ap_error(_alglib_env_state.error_msg);
1876  }
1877 }
1878 
1879 /*************************************************************************
1880 Complemented F distribution
1881 
1882 Returns the area from x to infinity under the F density
1883 function (also known as Snedcor's density or the
1884 variance ratio density).
1885 
1886 
1887  inf.
1888  -
1889  1 | | a-1 b-1
1890 1-P(x) = ------ | t (1-t) dt
1891  B(a,b) | |
1892  -
1893  x
1894 
1895 
1896 The incomplete beta integral is used, according to the
1897 formula
1898 
1899 P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
1900 
1901 
1902 ACCURACY:
1903 
1904 Tested at random points (a,b,x) in the indicated intervals.
1905  x a,b Relative error:
1906 arithmetic domain domain # trials peak rms
1907  IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
1908  IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
1909  IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
1910  IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
1911 
1912 Cephes Math Library Release 2.8: June, 2000
1913 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1914 *************************************************************************/
1915 double fcdistribution(const ae_int_t a, const ae_int_t b, const double x)
1916 {
1917  alglib_impl::ae_state _alglib_env_state;
1918  alglib_impl::ae_state_init(&_alglib_env_state);
1919  try
1920  {
1921  double result = alglib_impl::fcdistribution(a, b, x, &_alglib_env_state);
1922  alglib_impl::ae_state_clear(&_alglib_env_state);
1923  return *(reinterpret_cast<double*>(&result));
1924  }
1926  {
1927  throw ap_error(_alglib_env_state.error_msg);
1928  }
1929 }
1930 
1931 /*************************************************************************
1932 Inverse of complemented F distribution
1933 
1934 Finds the F density argument x such that the integral
1935 from x to infinity of the F density is equal to the
1936 given probability p.
1937 
1938 This is accomplished using the inverse beta integral
1939 function and the relations
1940 
1941  z = incbi( df2/2, df1/2, p )
1942  x = df2 (1-z) / (df1 z).
1943 
1944 Note: the following relations hold for the inverse of
1945 the uncomplemented F distribution:
1946 
1947  z = incbi( df1/2, df2/2, p )
1948  x = df2 z / (df1 (1-z)).
1949 
1950 ACCURACY:
1951 
1952 Tested at random points (a,b,p).
1953 
1954  a,b Relative error:
1955 arithmetic domain # trials peak rms
1956  For p between .001 and 1:
1957  IEEE 1,100 100000 8.3e-15 4.7e-16
1958  IEEE 1,10000 100000 2.1e-11 1.4e-13
1959  For p between 10^-6 and 10^-3:
1960  IEEE 1,100 50000 1.3e-12 8.4e-15
1961  IEEE 1,10000 50000 3.0e-12 4.8e-14
1962 
1963 Cephes Math Library Release 2.8: June, 2000
1964 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
1965 *************************************************************************/
1966 double invfdistribution(const ae_int_t a, const ae_int_t b, const double y)
1967 {
1968  alglib_impl::ae_state _alglib_env_state;
1969  alglib_impl::ae_state_init(&_alglib_env_state);
1970  try
1971  {
1972  double result = alglib_impl::invfdistribution(a, b, y, &_alglib_env_state);
1973  alglib_impl::ae_state_clear(&_alglib_env_state);
1974  return *(reinterpret_cast<double*>(&result));
1975  }
1977  {
1978  throw ap_error(_alglib_env_state.error_msg);
1979  }
1980 }
1981 
1982 /*************************************************************************
1983 Fresnel integral
1984 
1985 Evaluates the Fresnel integrals
1986 
1987  x
1988  -
1989  | |
1990 C(x) = | cos(pi/2 t**2) dt,
1991  | |
1992  -
1993  0
1994 
1995  x
1996  -
1997  | |
1998 S(x) = | sin(pi/2 t**2) dt.
1999  | |
2000  -
2001  0
2002 
2003 
2004 The integrals are evaluated by a power series for x < 1.
2005 For x >= 1 auxiliary functions f(x) and g(x) are employed
2006 such that
2007 
2008 C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
2009 S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
2010 
2011 
2012 
2013 ACCURACY:
2014 
2015  Relative error.
2016 
2017 Arithmetic function domain # trials peak rms
2018  IEEE S(x) 0, 10 10000 2.0e-15 3.2e-16
2019  IEEE C(x) 0, 10 10000 1.8e-15 3.3e-16
2020 
2021 Cephes Math Library Release 2.8: June, 2000
2022 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
2023 *************************************************************************/
2024 void fresnelintegral(const double x, double &c, double &s)
2025 {
2026  alglib_impl::ae_state _alglib_env_state;
2027  alglib_impl::ae_state_init(&_alglib_env_state);
2028  try
2029  {
2030  alglib_impl::fresnelintegral(x, &c, &s, &_alglib_env_state);
2031  alglib_impl::ae_state_clear(&_alglib_env_state);
2032  return;
2033  }
2035  {
2036  throw ap_error(_alglib_env_state.error_msg);
2037  }
2038 }
2039 
2040 /*************************************************************************
2041 Calculation of the value of the Hermite polynomial.
2042 
2043 Parameters:
2044  n - degree, n>=0
2045  x - argument
2046 
2047 Result:
2048  the value of the Hermite polynomial Hn at x
2049 *************************************************************************/
2050 double hermitecalculate(const ae_int_t n, const double x)
2051 {
2052  alglib_impl::ae_state _alglib_env_state;
2053  alglib_impl::ae_state_init(&_alglib_env_state);
2054  try
2055  {
2056  double result = alglib_impl::hermitecalculate(n, x, &_alglib_env_state);
2057  alglib_impl::ae_state_clear(&_alglib_env_state);
2058  return *(reinterpret_cast<double*>(&result));
2059  }
2061  {
2062  throw ap_error(_alglib_env_state.error_msg);
2063  }
2064 }
2065 
2066 /*************************************************************************
2067 Summation of Hermite polynomials using Clenshaw’s recurrence formula.
2068 
2069 This routine calculates
2070  c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
2071 
2072 Parameters:
2073  n - degree, n>=0
2074  x - argument
2075 
2076 Result:
2077  the value of the Hermite polynomial at x
2078 *************************************************************************/
2079 double hermitesum(const real_1d_array &c, const ae_int_t n, const double x)
2080 {
2081  alglib_impl::ae_state _alglib_env_state;
2082  alglib_impl::ae_state_init(&_alglib_env_state);
2083  try
2084  {
2085  double result = alglib_impl::hermitesum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), n, x, &_alglib_env_state);
2086  alglib_impl::ae_state_clear(&_alglib_env_state);
2087  return *(reinterpret_cast<double*>(&result));
2088  }
2090  {
2091  throw ap_error(_alglib_env_state.error_msg);
2092  }
2093 }
2094 
2095 /*************************************************************************
2096 Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
2097 
2098 Input parameters:
2099  N - polynomial degree, n>=0
2100 
2101 Output parameters:
2102  C - coefficients
2103 *************************************************************************/
2105 {
2106  alglib_impl::ae_state _alglib_env_state;
2107  alglib_impl::ae_state_init(&_alglib_env_state);
2108  try
2109  {
2110  alglib_impl::hermitecoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
2111  alglib_impl::ae_state_clear(&_alglib_env_state);
2112  return;
2113  }
2115  {
2116  throw ap_error(_alglib_env_state.error_msg);
2117  }
2118 }
2119 
2120 /*************************************************************************
2121 Jacobian Elliptic Functions
2122 
2123 Evaluates the Jacobian elliptic functions sn(u|m), cn(u|m),
2124 and dn(u|m) of parameter m between 0 and 1, and real
2125 argument u.
2126 
2127 These functions are periodic, with quarter-period on the
2128 real axis equal to the complete elliptic integral
2129 ellpk(1.0-m).
2130 
2131 Relation to incomplete elliptic integral:
2132 If u = ellik(phi,m), then sn(u|m) = sin(phi),
2133 and cn(u|m) = cos(phi). Phi is called the amplitude of u.
2134 
2135 Computation is by means of the arithmetic-geometric mean
2136 algorithm, except when m is within 1e-9 of 0 or 1. In the
2137 latter case with m close to 1, the approximation applies
2138 only for phi < pi/2.
2139 
2140 ACCURACY:
2141 
2142 Tested at random points with u between 0 and 10, m between
2143 0 and 1.
2144 
2145  Absolute error (* = relative error):
2146 arithmetic function # trials peak rms
2147  IEEE phi 10000 9.2e-16* 1.4e-16*
2148  IEEE sn 50000 4.1e-15 4.6e-16
2149  IEEE cn 40000 3.6e-15 4.4e-16
2150  IEEE dn 10000 1.3e-12 1.8e-14
2151 
2152  Peak error observed in consistency check using addition
2153 theorem for sn(u+v) was 4e-16 (absolute). Also tested by
2154 the above relation to the incomplete elliptic integral.
2155 Accuracy deteriorates when u is large.
2156 
2157 Cephes Math Library Release 2.8: June, 2000
2158 Copyright 1984, 1987, 2000 by Stephen L. Moshier
2159 *************************************************************************/
2160 void jacobianellipticfunctions(const double u, const double m, double &sn, double &cn, double &dn, double &ph)
2161 {
2162  alglib_impl::ae_state _alglib_env_state;
2163  alglib_impl::ae_state_init(&_alglib_env_state);
2164  try
2165  {
2166  alglib_impl::jacobianellipticfunctions(u, m, &sn, &cn, &dn, &ph, &_alglib_env_state);
2167  alglib_impl::ae_state_clear(&_alglib_env_state);
2168  return;
2169  }
2171  {
2172  throw ap_error(_alglib_env_state.error_msg);
2173  }
2174 }
2175 
2176 /*************************************************************************
2177 Calculation of the value of the Laguerre polynomial.
2178 
2179 Parameters:
2180  n - degree, n>=0
2181  x - argument
2182 
2183 Result:
2184  the value of the Laguerre polynomial Ln at x
2185 *************************************************************************/
2186 double laguerrecalculate(const ae_int_t n, const double x)
2187 {
2188  alglib_impl::ae_state _alglib_env_state;
2189  alglib_impl::ae_state_init(&_alglib_env_state);
2190  try
2191  {
2192  double result = alglib_impl::laguerrecalculate(n, x, &_alglib_env_state);
2193  alglib_impl::ae_state_clear(&_alglib_env_state);
2194  return *(reinterpret_cast<double*>(&result));
2195  }
2197  {
2198  throw ap_error(_alglib_env_state.error_msg);
2199  }
2200 }
2201 
2202 /*************************************************************************
2203 Summation of Laguerre polynomials using Clenshaw’s recurrence formula.
2204 
2205 This routine calculates c[0]*L0(x) + c[1]*L1(x) + ... + c[N]*LN(x)
2206 
2207 Parameters:
2208  n - degree, n>=0
2209  x - argument
2210 
2211 Result:
2212  the value of the Laguerre polynomial at x
2213 *************************************************************************/
2214 double laguerresum(const real_1d_array &c, const ae_int_t n, const double x)
2215 {
2216  alglib_impl::ae_state _alglib_env_state;
2217  alglib_impl::ae_state_init(&_alglib_env_state);
2218  try
2219  {
2220  double result = alglib_impl::laguerresum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), n, x, &_alglib_env_state);
2221  alglib_impl::ae_state_clear(&_alglib_env_state);
2222  return *(reinterpret_cast<double*>(&result));
2223  }
2225  {
2226  throw ap_error(_alglib_env_state.error_msg);
2227  }
2228 }
2229 
2230 /*************************************************************************
2231 Representation of Ln as C[0] + C[1]*X + ... + C[N]*X^N
2232 
2233 Input parameters:
2234  N - polynomial degree, n>=0
2235 
2236 Output parameters:
2237  C - coefficients
2238 *************************************************************************/
2240 {
2241  alglib_impl::ae_state _alglib_env_state;
2242  alglib_impl::ae_state_init(&_alglib_env_state);
2243  try
2244  {
2245  alglib_impl::laguerrecoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
2246  alglib_impl::ae_state_clear(&_alglib_env_state);
2247  return;
2248  }
2250  {
2251  throw ap_error(_alglib_env_state.error_msg);
2252  }
2253 }
2254 
2255 /*************************************************************************
2256 Calculation of the value of the Legendre polynomial Pn.
2257 
2258 Parameters:
2259  n - degree, n>=0
2260  x - argument
2261 
2262 Result:
2263  the value of the Legendre polynomial Pn at x
2264 *************************************************************************/
2265 double legendrecalculate(const ae_int_t n, const double x)
2266 {
2267  alglib_impl::ae_state _alglib_env_state;
2268  alglib_impl::ae_state_init(&_alglib_env_state);
2269  try
2270  {
2271  double result = alglib_impl::legendrecalculate(n, x, &_alglib_env_state);
2272  alglib_impl::ae_state_clear(&_alglib_env_state);
2273  return *(reinterpret_cast<double*>(&result));
2274  }
2276  {
2277  throw ap_error(_alglib_env_state.error_msg);
2278  }
2279 }
2280 
2281 /*************************************************************************
2282 Summation of Legendre polynomials using Clenshaw’s recurrence formula.
2283 
2284 This routine calculates
2285  c[0]*P0(x) + c[1]*P1(x) + ... + c[N]*PN(x)
2286 
2287 Parameters:
2288  n - degree, n>=0
2289  x - argument
2290 
2291 Result:
2292  the value of the Legendre polynomial at x
2293 *************************************************************************/
2294 double legendresum(const real_1d_array &c, const ae_int_t n, const double x)
2295 {
2296  alglib_impl::ae_state _alglib_env_state;
2297  alglib_impl::ae_state_init(&_alglib_env_state);
2298  try
2299  {
2300  double result = alglib_impl::legendresum(const_cast<alglib_impl::ae_vector*>(c.c_ptr()), n, x, &_alglib_env_state);
2301  alglib_impl::ae_state_clear(&_alglib_env_state);
2302  return *(reinterpret_cast<double*>(&result));
2303  }
2305  {
2306  throw ap_error(_alglib_env_state.error_msg);
2307  }
2308 }
2309 
2310 /*************************************************************************
2311 Representation of Pn as C[0] + C[1]*X + ... + C[N]*X^N
2312 
2313 Input parameters:
2314  N - polynomial degree, n>=0
2315 
2316 Output parameters:
2317  C - coefficients
2318 *************************************************************************/
2320 {
2321  alglib_impl::ae_state _alglib_env_state;
2322  alglib_impl::ae_state_init(&_alglib_env_state);
2323  try
2324  {
2325  alglib_impl::legendrecoefficients(n, const_cast<alglib_impl::ae_vector*>(c.c_ptr()), &_alglib_env_state);
2326  alglib_impl::ae_state_clear(&_alglib_env_state);
2327  return;
2328  }
2330  {
2331  throw ap_error(_alglib_env_state.error_msg);
2332  }
2333 }
2334 
2335 /*************************************************************************
2336 Poisson distribution
2337 
2338 Returns the sum of the first k+1 terms of the Poisson
2339 distribution:
2340 
2341  k j
2342  -- -m m
2343  > e --
2344  -- j!
2345  j=0
2346 
2347 The terms are not summed directly; instead the incomplete
2348 gamma integral is employed, according to the relation
2349 
2350 y = pdtr( k, m ) = igamc( k+1, m ).
2351 
2352 The arguments must both be positive.
2353 ACCURACY:
2354 
2355 See incomplete gamma function
2356 
2357 Cephes Math Library Release 2.8: June, 2000
2358 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2359 *************************************************************************/
2360 double poissondistribution(const ae_int_t k, const double m)
2361 {
2362  alglib_impl::ae_state _alglib_env_state;
2363  alglib_impl::ae_state_init(&_alglib_env_state);
2364  try
2365  {
2366  double result = alglib_impl::poissondistribution(k, m, &_alglib_env_state);
2367  alglib_impl::ae_state_clear(&_alglib_env_state);
2368  return *(reinterpret_cast<double*>(&result));
2369  }
2371  {
2372  throw ap_error(_alglib_env_state.error_msg);
2373  }
2374 }
2375 
2376 /*************************************************************************
2377 Complemented Poisson distribution
2378 
2379 Returns the sum of the terms k+1 to infinity of the Poisson
2380 distribution:
2381 
2382  inf. j
2383  -- -m m
2384  > e --
2385  -- j!
2386  j=k+1
2387 
2388 The terms are not summed directly; instead the incomplete
2389 gamma integral is employed, according to the formula
2390 
2391 y = pdtrc( k, m ) = igam( k+1, m ).
2392 
2393 The arguments must both be positive.
2394 
2395 ACCURACY:
2396 
2397 See incomplete gamma function
2398 
2399 Cephes Math Library Release 2.8: June, 2000
2400 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2401 *************************************************************************/
2402 double poissoncdistribution(const ae_int_t k, const double m)
2403 {
2404  alglib_impl::ae_state _alglib_env_state;
2405  alglib_impl::ae_state_init(&_alglib_env_state);
2406  try
2407  {
2408  double result = alglib_impl::poissoncdistribution(k, m, &_alglib_env_state);
2409  alglib_impl::ae_state_clear(&_alglib_env_state);
2410  return *(reinterpret_cast<double*>(&result));
2411  }
2413  {
2414  throw ap_error(_alglib_env_state.error_msg);
2415  }
2416 }
2417 
2418 /*************************************************************************
2419 Inverse Poisson distribution
2420 
2421 Finds the Poisson variable x such that the integral
2422 from 0 to x of the Poisson density is equal to the
2423 given probability y.
2424 
2425 This is accomplished using the inverse gamma integral
2426 function and the relation
2427 
2428  m = igami( k+1, y ).
2429 
2430 ACCURACY:
2431 
2432 See inverse incomplete gamma function
2433 
2434 Cephes Math Library Release 2.8: June, 2000
2435 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2436 *************************************************************************/
2437 double invpoissondistribution(const ae_int_t k, const double y)
2438 {
2439  alglib_impl::ae_state _alglib_env_state;
2440  alglib_impl::ae_state_init(&_alglib_env_state);
2441  try
2442  {
2443  double result = alglib_impl::invpoissondistribution(k, y, &_alglib_env_state);
2444  alglib_impl::ae_state_clear(&_alglib_env_state);
2445  return *(reinterpret_cast<double*>(&result));
2446  }
2448  {
2449  throw ap_error(_alglib_env_state.error_msg);
2450  }
2451 }
2452 
2453 /*************************************************************************
2454 Psi (digamma) function
2455 
2456  d -
2457  psi(x) = -- ln | (x)
2458  dx
2459 
2460 is the logarithmic derivative of the gamma function.
2461 For integer x,
2462  n-1
2463  -
2464 psi(n) = -EUL + > 1/k.
2465  -
2466  k=1
2467 
2468 This formula is used for 0 < n <= 10. If x is negative, it
2469 is transformed to a positive argument by the reflection
2470 formula psi(1-x) = psi(x) + pi cot(pi x).
2471 For general positive x, the argument is made greater than 10
2472 using the recurrence psi(x+1) = psi(x) + 1/x.
2473 Then the following asymptotic expansion is applied:
2474 
2475  inf. B
2476  - 2k
2477 psi(x) = log(x) - 1/2x - > -------
2478  - 2k
2479  k=1 2k x
2480 
2481 where the B2k are Bernoulli numbers.
2482 
2483 ACCURACY:
2484  Relative error (except absolute when |psi| < 1):
2485 arithmetic domain # trials peak rms
2486  IEEE 0,30 30000 1.3e-15 1.4e-16
2487  IEEE -30,0 40000 1.5e-15 2.2e-16
2488 
2489 Cephes Math Library Release 2.8: June, 2000
2490 Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier
2491 *************************************************************************/
2492 double psi(const double x)
2493 {
2494  alglib_impl::ae_state _alglib_env_state;
2495  alglib_impl::ae_state_init(&_alglib_env_state);
2496  try
2497  {
2498  double result = alglib_impl::psi(x, &_alglib_env_state);
2499  alglib_impl::ae_state_clear(&_alglib_env_state);
2500  return *(reinterpret_cast<double*>(&result));
2501  }
2503  {
2504  throw ap_error(_alglib_env_state.error_msg);
2505  }
2506 }
2507 
2508 /*************************************************************************
2509 Student's t distribution
2510 
2511 Computes the integral from minus infinity to t of the Student
2512 t distribution with integer k > 0 degrees of freedom:
2513 
2514  t
2515  -
2516  | |
2517  - | 2 -(k+1)/2
2518  | ( (k+1)/2 ) | ( x )
2519  ---------------------- | ( 1 + --- ) dx
2520  - | ( k )
2521  sqrt( k pi ) | ( k/2 ) |
2522  | |
2523  -
2524  -inf.
2525 
2526 Relation to incomplete beta integral:
2527 
2528  1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
2529 where
2530  z = k/(k + t**2).
2531 
2532 For t < -2, this is the method of computation. For higher t,
2533 a direct method is derived from integration by parts.
2534 Since the function is symmetric about t=0, the area under the
2535 right tail of the density is found by calling the function
2536 with -t instead of t.
2537 
2538 ACCURACY:
2539 
2540 Tested at random 1 <= k <= 25. The "domain" refers to t.
2541  Relative error:
2542 arithmetic domain # trials peak rms
2543  IEEE -100,-2 50000 5.9e-15 1.4e-15
2544  IEEE -2,100 500000 2.7e-15 4.9e-17
2545 
2546 Cephes Math Library Release 2.8: June, 2000
2547 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2548 *************************************************************************/
2549 double studenttdistribution(const ae_int_t k, const double t)
2550 {
2551  alglib_impl::ae_state _alglib_env_state;
2552  alglib_impl::ae_state_init(&_alglib_env_state);
2553  try
2554  {
2555  double result = alglib_impl::studenttdistribution(k, t, &_alglib_env_state);
2556  alglib_impl::ae_state_clear(&_alglib_env_state);
2557  return *(reinterpret_cast<double*>(&result));
2558  }
2560  {
2561  throw ap_error(_alglib_env_state.error_msg);
2562  }
2563 }
2564 
2565 /*************************************************************************
2566 Functional inverse of Student's t distribution
2567 
2568 Given probability p, finds the argument t such that stdtr(k,t)
2569 is equal to p.
2570 
2571 ACCURACY:
2572 
2573 Tested at random 1 <= k <= 100. The "domain" refers to p:
2574  Relative error:
2575 arithmetic domain # trials peak rms
2576  IEEE .001,.999 25000 5.7e-15 8.0e-16
2577  IEEE 10^-6,.001 25000 2.0e-12 2.9e-14
2578 
2579 Cephes Math Library Release 2.8: June, 2000
2580 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
2581 *************************************************************************/
2582 double invstudenttdistribution(const ae_int_t k, const double p)
2583 {
2584  alglib_impl::ae_state _alglib_env_state;
2585  alglib_impl::ae_state_init(&_alglib_env_state);
2586  try
2587  {
2588  double result = alglib_impl::invstudenttdistribution(k, p, &_alglib_env_state);
2589  alglib_impl::ae_state_clear(&_alglib_env_state);
2590  return *(reinterpret_cast<double*>(&result));
2591  }
2593  {
2594  throw ap_error(_alglib_env_state.error_msg);
2595  }
2596 }
2597 
2598 /*************************************************************************
2599 Sine and cosine integrals
2600 
2601 Evaluates the integrals
2602 
2603  x
2604  -
2605  | cos t - 1
2606  Ci(x) = eul + ln x + | --------- dt,
2607  | t
2608  -
2609  0
2610  x
2611  -
2612  | sin t
2613  Si(x) = | ----- dt
2614  | t
2615  -
2616  0
2617 
2618 where eul = 0.57721566490153286061 is Euler's constant.
2619 The integrals are approximated by rational functions.
2620 For x > 8 auxiliary functions f(x) and g(x) are employed
2621 such that
2622 
2623 Ci(x) = f(x) sin(x) - g(x) cos(x)
2624 Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
2625 
2626 
2627 ACCURACY:
2628  Test interval = [0,50].
2629 Absolute error, except relative when > 1:
2630 arithmetic function # trials peak rms
2631  IEEE Si 30000 4.4e-16 7.3e-17
2632  IEEE Ci 30000 6.9e-16 5.1e-17
2633 
2634 Cephes Math Library Release 2.1: January, 1989
2635 Copyright 1984, 1987, 1989 by Stephen L. Moshier
2636 *************************************************************************/
2637 void sinecosineintegrals(const double x, double &si, double &ci)
2638 {
2639  alglib_impl::ae_state _alglib_env_state;
2640  alglib_impl::ae_state_init(&_alglib_env_state);
2641  try
2642  {
2643  alglib_impl::sinecosineintegrals(x, &si, &ci, &_alglib_env_state);
2644  alglib_impl::ae_state_clear(&_alglib_env_state);
2645  return;
2646  }
2648  {
2649  throw ap_error(_alglib_env_state.error_msg);
2650  }
2651 }
2652 
2653 /*************************************************************************
2654 Hyperbolic sine and cosine integrals
2655 
2656 Approximates the integrals
2657 
2658  x
2659  -
2660  | | cosh t - 1
2661  Chi(x) = eul + ln x + | ----------- dt,
2662  | | t
2663  -
2664  0
2665 
2666  x
2667  -
2668  | | sinh t
2669  Shi(x) = | ------ dt
2670  | | t
2671  -
2672  0
2673 
2674 where eul = 0.57721566490153286061 is Euler's constant.
2675 The integrals are evaluated by power series for x < 8
2676 and by Chebyshev expansions for x between 8 and 88.
2677 For large x, both functions approach exp(x)/2x.
2678 Arguments greater than 88 in magnitude return MAXNUM.
2679 
2680 
2681 ACCURACY:
2682 
2683 Test interval 0 to 88.
2684  Relative error:
2685 arithmetic function # trials peak rms
2686  IEEE Shi 30000 6.9e-16 1.6e-16
2687  Absolute error, except relative when |Chi| > 1:
2688  IEEE Chi 30000 8.4e-16 1.4e-16
2689 
2690 Cephes Math Library Release 2.8: June, 2000
2691 Copyright 1984, 1987, 2000 by Stephen L. Moshier
2692 *************************************************************************/
2693 void hyperbolicsinecosineintegrals(const double x, double &shi, double &chi)
2694 {
2695  alglib_impl::ae_state _alglib_env_state;
2696  alglib_impl::ae_state_init(&_alglib_env_state);
2697  try
2698  {
2699  alglib_impl::hyperbolicsinecosineintegrals(x, &shi, &chi, &_alglib_env_state);
2700  alglib_impl::ae_state_clear(&_alglib_env_state);
2701  return;
2702  }
2704  {
2705  throw ap_error(_alglib_env_state.error_msg);
2706  }
2707 }
2708 }
2709 
2711 //
2712 // THIS SECTION CONTAINS IMPLEMENTATION OF COMPUTATIONAL CORE
2713 //
2715 namespace alglib_impl
2716 {
2717 static double gammafunc_gammastirf(double x, ae_state *_state);
2718 
2719 
2720 
2721 
2722 
2723 
2724 
2725 
2726 static void bessel_besselmfirstcheb(double c,
2727  double* b0,
2728  double* b1,
2729  double* b2,
2730  ae_state *_state);
2731 static void bessel_besselmnextcheb(double x,
2732  double c,
2733  double* b0,
2734  double* b1,
2735  double* b2,
2736  ae_state *_state);
2737 static void bessel_besselm1firstcheb(double c,
2738  double* b0,
2739  double* b1,
2740  double* b2,
2741  ae_state *_state);
2742 static void bessel_besselm1nextcheb(double x,
2743  double c,
2744  double* b0,
2745  double* b1,
2746  double* b2,
2747  ae_state *_state);
2748 static void bessel_besselasympt0(double x,
2749  double* pzero,
2750  double* qzero,
2751  ae_state *_state);
2752 static void bessel_besselasympt1(double x,
2753  double* pzero,
2754  double* qzero,
2755  ae_state *_state);
2756 
2757 
2758 
2759 
2760 static double ibetaf_incompletebetafe(double a,
2761  double b,
2762  double x,
2763  double big,
2764  double biginv,
2765  ae_state *_state);
2766 static double ibetaf_incompletebetafe2(double a,
2767  double b,
2768  double x,
2769  double big,
2770  double biginv,
2771  ae_state *_state);
2772 static double ibetaf_incompletebetaps(double a,
2773  double b,
2774  double x,
2775  double maxgam,
2776  ae_state *_state);
2777 
2778 
2779 
2780 
2781 
2782 
2783 
2784 
2785 
2786 
2787 
2788 
2789 
2790 
2791 
2792 
2793 
2794 
2795 
2796 
2797 
2798 
2799 
2800 
2801 
2802 
2803 
2804 
2805 
2806 
2807 
2808 
2809 static void trigintegrals_chebiterationshichi(double x,
2810  double c,
2811  double* b0,
2812  double* b1,
2813  double* b2,
2814  ae_state *_state);
2815 
2816 
2817 
2818 
2819 
2820 /*************************************************************************
2821 Gamma function
2822 
2823 Input parameters:
2824  X - argument
2825 
2826 Domain:
2827  0 < X < 171.6
2828  -170 < X < 0, X is not an integer.
2829 
2830 Relative error:
2831  arithmetic domain # trials peak rms
2832  IEEE -170,-33 20000 2.3e-15 3.3e-16
2833  IEEE -33, 33 20000 9.4e-16 2.2e-16
2834  IEEE 33, 171.6 20000 2.3e-15 3.2e-16
2835 
2836 Cephes Math Library Release 2.8: June, 2000
2837 Original copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
2838 Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
2839 *************************************************************************/
2840 double gammafunction(double x, ae_state *_state)
2841 {
2842 #ifndef ALGLIB_INTERCEPTS_SPECFUNCS
2843  double p;
2844  double pp;
2845  double q;
2846  double qq;
2847  double z;
2848  ae_int_t i;
2849  double sgngam;
2850  double result;
2851 
2852 
2853  sgngam = 1;
2854  q = ae_fabs(x, _state);
2855  if( ae_fp_greater(q,33.0) )
2856  {
2857  if( ae_fp_less(x,0.0) )
2858  {
2859  p = ae_ifloor(q, _state);
2860  i = ae_round(p, _state);
2861  if( i%2==0 )
2862  {
2863  sgngam = -1;
2864  }
2865  z = q-p;
2866  if( ae_fp_greater(z,0.5) )
2867  {
2868  p = p+1;
2869  z = q-p;
2870  }
2871  z = q*ae_sin(ae_pi*z, _state);
2872  z = ae_fabs(z, _state);
2873  z = ae_pi/(z*gammafunc_gammastirf(q, _state));
2874  }
2875  else
2876  {
2877  z = gammafunc_gammastirf(x, _state);
2878  }
2879  result = sgngam*z;
2880  return result;
2881  }
2882  z = 1;
2883  while(ae_fp_greater_eq(x,3))
2884  {
2885  x = x-1;
2886  z = z*x;
2887  }
2888  while(ae_fp_less(x,0))
2889  {
2890  if( ae_fp_greater(x,-0.000000001) )
2891  {
2892  result = z/((1+0.5772156649015329*x)*x);
2893  return result;
2894  }
2895  z = z/x;
2896  x = x+1;
2897  }
2898  while(ae_fp_less(x,2))
2899  {
2900  if( ae_fp_less(x,0.000000001) )
2901  {
2902  result = z/((1+0.5772156649015329*x)*x);
2903  return result;
2904  }
2905  z = z/x;
2906  x = x+1.0;
2907  }
2908  if( ae_fp_eq(x,2) )
2909  {
2910  result = z;
2911  return result;
2912  }
2913  x = x-2.0;
2914  pp = 1.60119522476751861407E-4;
2915  pp = 1.19135147006586384913E-3+x*pp;
2916  pp = 1.04213797561761569935E-2+x*pp;
2917  pp = 4.76367800457137231464E-2+x*pp;
2918  pp = 2.07448227648435975150E-1+x*pp;
2919  pp = 4.94214826801497100753E-1+x*pp;
2920  pp = 9.99999999999999996796E-1+x*pp;
2921  qq = -2.31581873324120129819E-5;
2922  qq = 5.39605580493303397842E-4+x*qq;
2923  qq = -4.45641913851797240494E-3+x*qq;
2924  qq = 1.18139785222060435552E-2+x*qq;
2925  qq = 3.58236398605498653373E-2+x*qq;
2926  qq = -2.34591795718243348568E-1+x*qq;
2927  qq = 7.14304917030273074085E-2+x*qq;
2928  qq = 1.00000000000000000320+x*qq;
2929  result = z*pp/qq;
2930  return result;
2931 #else
2932  return _ialglib_i_gammafunction(x);
2933 #endif
2934 }
2935 
2936 
2937 /*************************************************************************
2938 Natural logarithm of gamma function
2939 
2940 Input parameters:
2941  X - argument
2942 
2943 Result:
2944  logarithm of the absolute value of the Gamma(X).
2945 
2946 Output parameters:
2947  SgnGam - sign(Gamma(X))
2948 
2949 Domain:
2950  0 < X < 2.55e305
2951  -2.55e305 < X < 0, X is not an integer.
2952 
2953 ACCURACY:
2954 arithmetic domain # trials peak rms
2955  IEEE 0, 3 28000 5.4e-16 1.1e-16
2956  IEEE 2.718, 2.556e305 40000 3.5e-16 8.3e-17
2957 The error criterion was relative when the function magnitude
2958 was greater than one but absolute when it was less than one.
2959 
2960 The following test used the relative error criterion, though
2961 at certain points the relative error could be much higher than
2962 indicated.
2963  IEEE -200, -4 10000 4.8e-16 1.3e-16
2964 
2965 Cephes Math Library Release 2.8: June, 2000
2966 Copyright 1984, 1987, 1989, 1992, 2000 by Stephen L. Moshier
2967 Translated to AlgoPascal by Bochkanov Sergey (2005, 2006, 2007).
2968 *************************************************************************/
2969 double lngamma(double x, double* sgngam, ae_state *_state)
2970 {
2971 #ifndef ALGLIB_INTERCEPTS_SPECFUNCS
2972  double a;
2973  double b;
2974  double c;
2975  double p;
2976  double q;
2977  double u;
2978  double w;
2979  double z;
2980  ae_int_t i;
2981  double logpi;
2982  double ls2pi;
2983  double tmp;
2984  double result;
2985 
2986  *sgngam = 0;
2987 
2988  *sgngam = 1;
2989  logpi = 1.14472988584940017414;
2990  ls2pi = 0.91893853320467274178;
2991  if( ae_fp_less(x,-34.0) )
2992  {
2993  q = -x;
2994  w = lngamma(q, &tmp, _state);
2995  p = ae_ifloor(q, _state);
2996  i = ae_round(p, _state);
2997  if( i%2==0 )
2998  {
2999  *sgngam = -1;
3000  }
3001  else
3002  {
3003  *sgngam = 1;
3004  }
3005  z = q-p;
3006  if( ae_fp_greater(z,0.5) )
3007  {
3008  p = p+1;
3009  z = p-q;
3010  }
3011  z = q*ae_sin(ae_pi*z, _state);
3012  result = logpi-ae_log(z, _state)-w;
3013  return result;
3014  }
3015  if( ae_fp_less(x,13) )
3016  {
3017  z = 1;
3018  p = 0;
3019  u = x;
3020  while(ae_fp_greater_eq(u,3))
3021  {
3022  p = p-1;
3023  u = x+p;
3024  z = z*u;
3025  }
3026  while(ae_fp_less(u,2))
3027  {
3028  z = z/u;
3029  p = p+1;
3030  u = x+p;
3031  }
3032  if( ae_fp_less(z,0) )
3033  {
3034  *sgngam = -1;
3035  z = -z;
3036  }
3037  else
3038  {
3039  *sgngam = 1;
3040  }
3041  if( ae_fp_eq(u,2) )
3042  {
3043  result = ae_log(z, _state);
3044  return result;
3045  }
3046  p = p-2;
3047  x = x+p;
3048  b = -1378.25152569120859100;
3049  b = -38801.6315134637840924+x*b;
3050  b = -331612.992738871184744+x*b;
3051  b = -1162370.97492762307383+x*b;
3052  b = -1721737.00820839662146+x*b;
3053  b = -853555.664245765465627+x*b;
3054  c = 1;
3055  c = -351.815701436523470549+x*c;
3056  c = -17064.2106651881159223+x*c;
3057  c = -220528.590553854454839+x*c;
3058  c = -1139334.44367982507207+x*c;
3059  c = -2532523.07177582951285+x*c;
3060  c = -2018891.41433532773231+x*c;
3061  p = x*b/c;
3062  result = ae_log(z, _state)+p;
3063  return result;
3064  }
3065  q = (x-0.5)*ae_log(x, _state)-x+ls2pi;
3066  if( ae_fp_greater(x,100000000) )
3067  {
3068  result = q;
3069  return result;
3070  }
3071  p = 1/(x*x);
3072  if( ae_fp_greater_eq(x,1000.0) )
3073  {
3074  q = q+((7.9365079365079365079365*0.0001*p-2.7777777777777777777778*0.001)*p+0.0833333333333333333333)/x;
3075  }
3076  else
3077  {
3078  a = 8.11614167470508450300*0.0001;
3079  a = -5.95061904284301438324*0.0001+p*a;
3080  a = 7.93650340457716943945*0.0001+p*a;
3081  a = -2.77777777730099687205*0.001+p*a;
3082  a = 8.33333333333331927722*0.01+p*a;
3083  q = q+a/x;
3084  }
3085  result = q;
3086  return result;
3087 #else
3088  return _ialglib_i_lngamma(x, sgngam);
3089 #endif
3090 }
3091 
3092 
3093 static double gammafunc_gammastirf(double x, ae_state *_state)
3094 {
3095  double y;
3096  double w;
3097  double v;
3098  double stir;
3099  double result;
3100 
3101 
3102  w = 1/x;
3103  stir = 7.87311395793093628397E-4;
3104  stir = -2.29549961613378126380E-4+w*stir;
3105  stir = -2.68132617805781232825E-3+w*stir;
3106  stir = 3.47222221605458667310E-3+w*stir;
3107  stir = 8.33333333333482257126E-2+w*stir;
3108  w = 1+w*stir;
3109  y = ae_exp(x, _state);
3110  if( ae_fp_greater(x,143.01608) )
3111  {
3112  v = ae_pow(x, 0.5*x-0.25, _state);
3113  y = v*(v/y);
3114  }
3115  else
3116  {
3117  y = ae_pow(x, x-0.5, _state)/y;
3118  }
3119  result = 2.50662827463100050242*y*w;
3120  return result;
3121 }
3122 
3123 
3124 
3125 
3126 /*************************************************************************
3127 Error function
3128 
3129 The integral is
3130 
3131  x
3132  -
3133  2 | | 2
3134  erf(x) = -------- | exp( - t ) dt.
3135  sqrt(pi) | |
3136  -
3137  0
3138 
3139 For 0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2); otherwise
3140 erf(x) = 1 - erfc(x).
3141 
3142 
3143 ACCURACY:
3144 
3145  Relative error:
3146 arithmetic domain # trials peak rms
3147  IEEE 0,1 30000 3.7e-16 1.0e-16
3148 
3149 Cephes Math Library Release 2.8: June, 2000
3150 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3151 *************************************************************************/
3152 double errorfunction(double x, ae_state *_state)
3153 {
3154  double xsq;
3155  double s;
3156  double p;
3157  double q;
3158  double result;
3159 
3160 
3161  s = ae_sign(x, _state);
3162  x = ae_fabs(x, _state);
3163  if( ae_fp_less(x,0.5) )
3164  {
3165  xsq = x*x;
3166  p = 0.007547728033418631287834;
3167  p = -0.288805137207594084924010+xsq*p;
3168  p = 14.3383842191748205576712+xsq*p;
3169  p = 38.0140318123903008244444+xsq*p;
3170  p = 3017.82788536507577809226+xsq*p;
3171  p = 7404.07142710151470082064+xsq*p;
3172  p = 80437.3630960840172832162+xsq*p;
3173  q = 0.0;
3174  q = 1.00000000000000000000000+xsq*q;
3175  q = 38.0190713951939403753468+xsq*q;
3176  q = 658.070155459240506326937+xsq*q;
3177  q = 6379.60017324428279487120+xsq*q;
3178  q = 34216.5257924628539769006+xsq*q;
3179  q = 80437.3630960840172826266+xsq*q;
3180  result = s*1.1283791670955125738961589031*x*p/q;
3181  return result;
3182  }
3183  if( ae_fp_greater_eq(x,10) )
3184  {
3185  result = s;
3186  return result;
3187  }
3188  result = s*(1-errorfunctionc(x, _state));
3189  return result;
3190 }
3191 
3192 
3193 /*************************************************************************
3194 Complementary error function
3195 
3196  1 - erf(x) =
3197 
3198  inf.
3199  -
3200  2 | | 2
3201  erfc(x) = -------- | exp( - t ) dt
3202  sqrt(pi) | |
3203  -
3204  x
3205 
3206 
3207 For small x, erfc(x) = 1 - erf(x); otherwise rational
3208 approximations are computed.
3209 
3210 
3211 ACCURACY:
3212 
3213  Relative error:
3214 arithmetic domain # trials peak rms
3215  IEEE 0,26.6417 30000 5.7e-14 1.5e-14
3216 
3217 Cephes Math Library Release 2.8: June, 2000
3218 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3219 *************************************************************************/
3220 double errorfunctionc(double x, ae_state *_state)
3221 {
3222  double p;
3223  double q;
3224  double result;
3225 
3226 
3227  if( ae_fp_less(x,0) )
3228  {
3229  result = 2-errorfunctionc(-x, _state);
3230  return result;
3231  }
3232  if( ae_fp_less(x,0.5) )
3233  {
3234  result = 1.0-errorfunction(x, _state);
3235  return result;
3236  }
3237  if( ae_fp_greater_eq(x,10) )
3238  {
3239  result = 0;
3240  return result;
3241  }
3242  p = 0.0;
3243  p = 0.5641877825507397413087057563+x*p;
3244  p = 9.675807882987265400604202961+x*p;
3245  p = 77.08161730368428609781633646+x*p;
3246  p = 368.5196154710010637133875746+x*p;
3247  p = 1143.262070703886173606073338+x*p;
3248  p = 2320.439590251635247384768711+x*p;
3249  p = 2898.0293292167655611275846+x*p;
3250  p = 1826.3348842295112592168999+x*p;
3251  q = 1.0;
3252  q = 17.14980943627607849376131193+x*q;
3253  q = 137.1255960500622202878443578+x*q;
3254  q = 661.7361207107653469211984771+x*q;
3255  q = 2094.384367789539593790281779+x*q;
3256  q = 4429.612803883682726711528526+x*q;
3257  q = 6089.5424232724435504633068+x*q;
3258  q = 4958.82756472114071495438422+x*q;
3259  q = 1826.3348842295112595576438+x*q;
3260  result = ae_exp(-ae_sqr(x, _state), _state)*p/q;
3261  return result;
3262 }
3263 
3264 
3265 /*************************************************************************
3266 Normal distribution function
3267 
3268 Returns the area under the Gaussian probability density
3269 function, integrated from minus infinity to x:
3270 
3271  x
3272  -
3273  1 | | 2
3274  ndtr(x) = --------- | exp( - t /2 ) dt
3275  sqrt(2pi) | |
3276  -
3277  -inf.
3278 
3279  = ( 1 + erf(z) ) / 2
3280  = erfc(z) / 2
3281 
3282 where z = x/sqrt(2). Computation is via the functions
3283 erf and erfc.
3284 
3285 
3286 ACCURACY:
3287 
3288  Relative error:
3289 arithmetic domain # trials peak rms
3290  IEEE -13,0 30000 3.4e-14 6.7e-15
3291 
3292 Cephes Math Library Release 2.8: June, 2000
3293 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3294 *************************************************************************/
3295 double normaldistribution(double x, ae_state *_state)
3296 {
3297  double result;
3298 
3299 
3300  result = 0.5*(errorfunction(x/1.41421356237309504880, _state)+1);
3301  return result;
3302 }
3303 
3304 
3305 /*************************************************************************
3306 Inverse of the error function
3307 
3308 Cephes Math Library Release 2.8: June, 2000
3309 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3310 *************************************************************************/
3311 double inverf(double e, ae_state *_state)
3312 {
3313  double result;
3314 
3315 
3316  result = invnormaldistribution(0.5*(e+1), _state)/ae_sqrt(2, _state);
3317  return result;
3318 }
3319 
3320 
3321 /*************************************************************************
3322 Inverse of Normal distribution function
3323 
3324 Returns the argument, x, for which the area under the
3325 Gaussian probability density function (integrated from
3326 minus infinity to x) is equal to y.
3327 
3328 
3329 For small arguments 0 < y < exp(-2), the program computes
3330 z = sqrt( -2.0 * log(y) ); then the approximation is
3331 x = z - log(z)/z - (1/z) P(1/z) / Q(1/z).
3332 There are two rational functions P/Q, one for 0 < y < exp(-32)
3333 and the other for y up to exp(-2). For larger arguments,
3334 w = y - 0.5, and x/sqrt(2pi) = w + w**3 R(w**2)/S(w**2)).
3335 
3336 ACCURACY:
3337 
3338  Relative error:
3339 arithmetic domain # trials peak rms
3340  IEEE 0.125, 1 20000 7.2e-16 1.3e-16
3341  IEEE 3e-308, 0.135 50000 4.6e-16 9.8e-17
3342 
3343 Cephes Math Library Release 2.8: June, 2000
3344 Copyright 1984, 1987, 1988, 1992, 2000 by Stephen L. Moshier
3345 *************************************************************************/
3346 double invnormaldistribution(double y0, ae_state *_state)
3347 {
3348  double expm2;
3349  double s2pi;
3350  double x;
3351  double y;
3352  double z;
3353  double y2;
3354  double x0;
3355  double x1;
3356  ae_int_t code;
3357  double p0;
3358  double q0;
3359  double p1;
3360  double q1;
3361  double p2;
3362  double q2;
3363  double result;
3364 
3365 
3366  expm2 = 0.13533528323661269189;
3367  s2pi = 2.50662827463100050242;
3368  if( ae_fp_less_eq(y0,0) )
3369  {
3370  result = -ae_maxrealnumber;
3371  return result;
3372  }
3373  if( ae_fp_greater_eq(y0,1) )
3374  {
3375  result = ae_maxrealnumber;
3376  return result;
3377  }
3378  code = 1;
3379  y = y0;
3380  if( ae_fp_greater(y,1.0-expm2) )
3381  {
3382  y = 1.0-y;
3383  code = 0;
3384  }
3385  if( ae_fp_greater(y,expm2) )
3386  {
3387  y = y-0.5;
3388  y2 = y*y;
3389  p0 = -59.9633501014107895267;
3390  p0 = 98.0010754185999661536+y2*p0;
3391  p0 = -56.6762857469070293439+y2*p0;
3392  p0 = 13.9312609387279679503+y2*p0;
3393  p0 = -1.23916583867381258016+y2*p0;
3394  q0 = 1;
3395  q0 = 1.95448858338141759834+y2*q0;
3396  q0 = 4.67627912898881538453+y2*q0;
3397  q0 = 86.3602421390890590575+y2*q0;
3398  q0 = -225.462687854119370527+y2*q0;
3399  q0 = 200.260212380060660359+y2*q0;
3400  q0 = -82.0372256168333339912+y2*q0;
3401  q0 = 15.9056225126211695515+y2*q0;
3402  q0 = -1.18331621121330003142+y2*q0;
3403  x = y+y*y2*p0/q0;
3404  x = x*s2pi;
3405  result = x;
3406  return result;
3407  }
3408  x = ae_sqrt(-2.0*ae_log(y, _state), _state);
3409  x0 = x-ae_log(x, _state)/x;
3410  z = 1.0/x;
3411  if( ae_fp_less(x,8.0) )
3412  {
3413  p1 = 4.05544892305962419923;
3414  p1 = 31.5251094599893866154+z*p1;
3415  p1 = 57.1628192246421288162+z*p1;
3416  p1 = 44.0805073893200834700+z*p1;
3417  p1 = 14.6849561928858024014+z*p1;
3418  p1 = 2.18663306850790267539+z*p1;
3419  p1 = -1.40256079171354495875*0.1+z*p1;
3420  p1 = -3.50424626827848203418*0.01+z*p1;
3421  p1 = -8.57456785154685413611*0.0001+z*p1;
3422  q1 = 1;
3423  q1 = 15.7799883256466749731+z*q1;
3424  q1 = 45.3907635128879210584+z*q1;
3425  q1 = 41.3172038254672030440+z*q1;
3426  q1 = 15.0425385692907503408+z*q1;
3427  q1 = 2.50464946208309415979+z*q1;
3428  q1 = -1.42182922854787788574*0.1+z*q1;
3429  q1 = -3.80806407691578277194*0.01+z*q1;
3430  q1 = -9.33259480895457427372*0.0001+z*q1;
3431  x1 = z*p1/q1;
3432  }
3433  else
3434  {
3435  p2 = 3.23774891776946035970;
3436  p2 = 6.91522889068984211695+z*p2;
3437  p2 = 3.93881025292474443415+z*p2;
3438  p2 = 1.33303460815807542389+z*p2;
3439  p2 = 2.01485389549179081538*0.1+z*p2;
3440  p2 = 1.23716634817820021358*0.01+z*p2;
3441  p2 = 3.01581553508235416007*0.0001+z*p2;
3442  p2 = 2.65806974686737550832*0.000001+z*p2;
3443  p2 = 6.23974539184983293730*0.000000001+z*p2;
3444  q2 = 1;
3445  q2 = 6.02427039364742014255+z*q2;
3446  q2 = 3.67983563856160859403+z*q2;
3447  q2 = 1.37702099489081330271+z*q2;
3448  q2 = 2.16236993594496635890*0.1+z*q2;
3449  q2 = 1.34204006088543189037*0.01+z*q2;
3450  q2 = 3.28014464682127739104*0.0001+z*q2;
3451  q2 = 2.89247864745380683936*0.000001+z*q2;
3452  q2 = 6.79019408009981274425*0.000000001+z*q2;
3453  x1 = z*p2/q2;
3454  }
3455  x = x0-x1;
3456  if( code!=0 )
3457  {
3458  x = -x;
3459  }
3460  result = x;
3461  return result;
3462 }
3463 
3464 
3465 
3466 
3467 /*************************************************************************
3468 Incomplete gamma integral
3469 
3470 The function is defined by
3471 
3472  x
3473  -
3474  1 | | -t a-1
3475  igam(a,x) = ----- | e t dt.
3476  - | |
3477  | (a) -
3478  0
3479 
3480 
3481 In this implementation both arguments must be positive.
3482 The integral is evaluated by either a power series or
3483 continued fraction expansion, depending on the relative
3484 values of a and x.
3485 
3486 ACCURACY:
3487 
3488  Relative error:
3489 arithmetic domain # trials peak rms
3490  IEEE 0,30 200000 3.6e-14 2.9e-15
3491  IEEE 0,100 300000 9.9e-14 1.5e-14
3492 
3493 Cephes Math Library Release 2.8: June, 2000
3494 Copyright 1985, 1987, 2000 by Stephen L. Moshier
3495 *************************************************************************/
3496 double incompletegamma(double a, double x, ae_state *_state)
3497 {
3498  double igammaepsilon;
3499  double ans;
3500  double ax;
3501  double c;
3502  double r;
3503  double tmp;
3504  double result;
3505 
3506 
3507  igammaepsilon = 0.000000000000001;
3508  if( ae_fp_less_eq(x,0)||ae_fp_less_eq(a,0) )
3509  {
3510  result = 0;
3511  return result;
3512  }
3513  if( ae_fp_greater(x,1)&&ae_fp_greater(x,a) )
3514  {
3515  result = 1-incompletegammac(a, x, _state);
3516  return result;
3517  }
3518  ax = a*ae_log(x, _state)-x-lngamma(a, &tmp, _state);
3519  if( ae_fp_less(ax,-709.78271289338399) )
3520  {
3521  result = 0;
3522  return result;
3523  }
3524  ax = ae_exp(ax, _state);
3525  r = a;
3526  c = 1;
3527  ans = 1;
3528  do
3529  {
3530  r = r+1;
3531  c = c*x/r;
3532  ans = ans+c;
3533  }
3534  while(ae_fp_greater(c/ans,igammaepsilon));
3535  result = ans*ax/a;
3536  return result;
3537 }
3538 
3539 
3540 /*************************************************************************
3541 Complemented incomplete gamma integral
3542 
3543 The function is defined by
3544 
3545 
3546  igamc(a,x) = 1 - igam(a,x)
3547 
3548  inf.
3549  -
3550  1 | | -t a-1
3551  = ----- | e t dt.
3552  - | |
3553  | (a) -
3554  x
3555 
3556 
3557 In this implementation both arguments must be positive.
3558 The integral is evaluated by either a power series or
3559 continued fraction expansion, depending on the relative
3560 values of a and x.
3561 
3562 ACCURACY:
3563 
3564 Tested at random a, x.
3565  a x Relative error:
3566 arithmetic domain domain # trials peak rms
3567  IEEE 0.5,100 0,100 200000 1.9e-14 1.7e-15
3568  IEEE 0.01,0.5 0,100 200000 1.4e-13 1.6e-15
3569 
3570 Cephes Math Library Release 2.8: June, 2000
3571 Copyright 1985, 1987, 2000 by Stephen L. Moshier
3572 *************************************************************************/
3573 double incompletegammac(double a, double x, ae_state *_state)
3574 {
3575  double igammaepsilon;
3576  double igammabignumber;
3577  double igammabignumberinv;
3578  double ans;
3579  double ax;
3580  double c;
3581  double yc;
3582  double r;
3583  double t;
3584  double y;
3585  double z;
3586  double pk;
3587  double pkm1;
3588  double pkm2;
3589  double qk;
3590  double qkm1;
3591  double qkm2;
3592  double tmp;
3593  double result;
3594 
3595 
3596  igammaepsilon = 0.000000000000001;
3597  igammabignumber = 4503599627370496.0;
3598  igammabignumberinv = 2.22044604925031308085*0.0000000000000001;
3599  if( ae_fp_less_eq(x,0)||ae_fp_less_eq(a,0) )
3600  {
3601  result = 1;
3602  return result;
3603  }
3604  if( ae_fp_less(x,1)||ae_fp_less(x,a) )
3605  {
3606  result = 1-incompletegamma(a, x, _state);
3607  return result;
3608  }
3609  ax = a*ae_log(x, _state)-x-lngamma(a, &tmp, _state);
3610  if( ae_fp_less(ax,-709.78271289338399) )
3611  {
3612  result = 0;
3613  return result;
3614  }
3615  ax = ae_exp(ax, _state);
3616  y = 1-a;
3617  z = x+y+1;
3618  c = 0;
3619  pkm2 = 1;
3620  qkm2 = x;
3621  pkm1 = x+1;
3622  qkm1 = z*x;
3623  ans = pkm1/qkm1;
3624  do
3625  {
3626  c = c+1;
3627  y = y+1;
3628  z = z+2;
3629  yc = y*c;
3630  pk = pkm1*z-pkm2*yc;
3631  qk = qkm1*z-qkm2*yc;
3632  if( ae_fp_neq(qk,0) )
3633  {
3634  r = pk/qk;
3635  t = ae_fabs((ans-r)/r, _state);
3636  ans = r;
3637  }
3638  else
3639  {
3640  t = 1;
3641  }
3642  pkm2 = pkm1;
3643  pkm1 = pk;
3644  qkm2 = qkm1;
3645  qkm1 = qk;
3646  if( ae_fp_greater(ae_fabs(pk, _state),igammabignumber) )
3647  {
3648  pkm2 = pkm2*igammabignumberinv;
3649  pkm1 = pkm1*igammabignumberinv;
3650  qkm2 = qkm2*igammabignumberinv;
3651  qkm1 = qkm1*igammabignumberinv;
3652  }
3653  }
3654  while(ae_fp_greater(t,igammaepsilon));
3655  result = ans*ax;
3656  return result;
3657 }
3658 
3659 
3660 /*************************************************************************
3661 Inverse of complemented incomplete gamma integral
3662 
3663 Given p, the function finds x such that
3664 
3665  igamc( a, x ) = p.
3666 
3667 Starting with the approximate value
3668 
3669  3
3670  x = a t
3671 
3672  where
3673 
3674  t = 1 - d - ndtri(p) sqrt(d)
3675 
3676 and
3677 
3678  d = 1/9a,
3679 
3680 the routine performs up to 10 Newton iterations to find the
3681 root of igamc(a,x) - p = 0.
3682 
3683 ACCURACY:
3684 
3685 Tested at random a, p in the intervals indicated.
3686 
3687  a p Relative error:
3688 arithmetic domain domain # trials peak rms
3689  IEEE 0.5,100 0,0.5 100000 1.0e-14 1.7e-15
3690  IEEE 0.01,0.5 0,0.5 100000 9.0e-14 3.4e-15
3691  IEEE 0.5,10000 0,0.5 20000 2.3e-13 3.8e-14
3692 
3693 Cephes Math Library Release 2.8: June, 2000
3694 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
3695 *************************************************************************/
3696 double invincompletegammac(double a, double y0, ae_state *_state)
3697 {
3698  double igammaepsilon;
3699  double iinvgammabignumber;
3700  double x0;
3701  double x1;
3702  double x;
3703  double yl;
3704  double yh;
3705  double y;
3706  double d;
3707  double lgm;
3708  double dithresh;
3709  ae_int_t i;
3710  ae_int_t dir;
3711  double tmp;
3712  double result;
3713 
3714 
3715  igammaepsilon = 0.000000000000001;
3716  iinvgammabignumber = 4503599627370496.0;
3717  x0 = iinvgammabignumber;
3718  yl = 0;
3719  x1 = 0;
3720  yh = 1;
3721  dithresh = 5*igammaepsilon;
3722  d = 1/(9*a);
3723  y = 1-d-invnormaldistribution(y0, _state)*ae_sqrt(d, _state);
3724  x = a*y*y*y;
3725  lgm = lngamma(a, &tmp, _state);
3726  i = 0;
3727  while(i<10)
3728  {
3729  if( ae_fp_greater(x,x0)||ae_fp_less(x,x1) )
3730  {
3731  d = 0.0625;
3732  break;
3733  }
3734  y = incompletegammac(a, x, _state);
3735  if( ae_fp_less(y,yl)||ae_fp_greater(y,yh) )
3736  {
3737  d = 0.0625;
3738  break;
3739  }
3740  if( ae_fp_less(y,y0) )
3741  {
3742  x0 = x;
3743  yl = y;
3744  }
3745  else
3746  {
3747  x1 = x;
3748  yh = y;
3749  }
3750  d = (a-1)*ae_log(x, _state)-x-lgm;
3751  if( ae_fp_less(d,-709.78271289338399) )
3752  {
3753  d = 0.0625;
3754  break;
3755  }
3756  d = -ae_exp(d, _state);
3757  d = (y-y0)/d;
3758  if( ae_fp_less(ae_fabs(d/x, _state),igammaepsilon) )
3759  {
3760  result = x;
3761  return result;
3762  }
3763  x = x-d;
3764  i = i+1;
3765  }
3766  if( ae_fp_eq(x0,iinvgammabignumber) )
3767  {
3768  if( ae_fp_less_eq(x,0) )
3769  {
3770  x = 1;
3771  }
3772  while(ae_fp_eq(x0,iinvgammabignumber))
3773  {
3774  x = (1+d)*x;
3775  y = incompletegammac(a, x, _state);
3776  if( ae_fp_less(y,y0) )
3777  {
3778  x0 = x;
3779  yl = y;
3780  break;
3781  }
3782  d = d+d;
3783  }
3784  }
3785  d = 0.5;
3786  dir = 0;
3787  i = 0;
3788  while(i<400)
3789  {
3790  x = x1+d*(x0-x1);
3791  y = incompletegammac(a, x, _state);
3792  lgm = (x0-x1)/(x1+x0);
3793  if( ae_fp_less(ae_fabs(lgm, _state),dithresh) )
3794  {
3795  break;
3796  }
3797  lgm = (y-y0)/y0;
3798  if( ae_fp_less(ae_fabs(lgm, _state),dithresh) )
3799  {
3800  break;
3801  }
3802  if( ae_fp_less_eq(x,0.0) )
3803  {
3804  break;
3805  }
3806  if( ae_fp_greater_eq(y,y0) )
3807  {
3808  x1 = x;
3809  yh = y;
3810  if( dir<0 )
3811  {
3812  dir = 0;
3813  d = 0.5;
3814  }
3815  else
3816  {
3817  if( dir>1 )
3818  {
3819  d = 0.5*d+0.5;
3820  }
3821  else
3822  {
3823  d = (y0-yl)/(yh-yl);
3824  }
3825  }
3826  dir = dir+1;
3827  }
3828  else
3829  {
3830  x0 = x;
3831  yl = y;
3832  if( dir>0 )
3833  {
3834  dir = 0;
3835  d = 0.5;
3836  }
3837  else
3838  {
3839  if( dir<-1 )
3840  {
3841  d = 0.5*d;
3842  }
3843  else
3844  {
3845  d = (y0-yl)/(yh-yl);
3846  }
3847  }
3848  dir = dir-1;
3849  }
3850  i = i+1;
3851  }
3852  result = x;
3853  return result;
3854 }
3855 
3856 
3857 
3858 
3859 /*************************************************************************
3860 Airy function
3861 
3862 Solution of the differential equation
3863 
3864 y"(x) = xy.
3865 
3866 The function returns the two independent solutions Ai, Bi
3867 and their first derivatives Ai'(x), Bi'(x).
3868 
3869 Evaluation is by power series summation for small x,
3870 by rational minimax approximations for large x.
3871 
3872 
3873 
3874 ACCURACY:
3875 Error criterion is absolute when function <= 1, relative
3876 when function > 1, except * denotes relative error criterion.
3877 For large negative x, the absolute error increases as x^1.5.
3878 For large positive x, the relative error increases as x^1.5.
3879 
3880 Arithmetic domain function # trials peak rms
3881 IEEE -10, 0 Ai 10000 1.6e-15 2.7e-16
3882 IEEE 0, 10 Ai 10000 2.3e-14* 1.8e-15*
3883 IEEE -10, 0 Ai' 10000 4.6e-15 7.6e-16
3884 IEEE 0, 10 Ai' 10000 1.8e-14* 1.5e-15*
3885 IEEE -10, 10 Bi 30000 4.2e-15 5.3e-16
3886 IEEE -10, 10 Bi' 30000 4.9e-15 7.3e-16
3887 
3888 Cephes Math Library Release 2.8: June, 2000
3889 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
3890 *************************************************************************/
3891 void airy(double x,
3892  double* ai,
3893  double* aip,
3894  double* bi,
3895  double* bip,
3896  ae_state *_state)
3897 {
3898  double z;
3899  double zz;
3900  double t;
3901  double f;
3902  double g;
3903  double uf;
3904  double ug;
3905  double k;
3906  double zeta;
3907  double theta;
3908  ae_int_t domflg;
3909  double c1;
3910  double c2;
3911  double sqrt3;
3912  double sqpii;
3913  double afn;
3914  double afd;
3915  double agn;
3916  double agd;
3917  double apfn;
3918  double apfd;
3919  double apgn;
3920  double apgd;
3921  double an;
3922  double ad;
3923  double apn;
3924  double apd;
3925  double bn16;
3926  double bd16;
3927  double bppn;
3928  double bppd;
3929 
3930  *ai = 0;
3931  *aip = 0;
3932  *bi = 0;
3933  *bip = 0;
3934 
3935  sqpii = 5.64189583547756286948E-1;
3936  c1 = 0.35502805388781723926;
3937  c2 = 0.258819403792806798405;
3938  sqrt3 = 1.732050807568877293527;
3939  domflg = 0;
3940  if( ae_fp_greater(x,25.77) )
3941  {
3942  *ai = 0;
3943  *aip = 0;
3944  *bi = ae_maxrealnumber;
3945  *bip = ae_maxrealnumber;
3946  return;
3947  }
3948  if( ae_fp_less(x,-2.09) )
3949  {
3950  domflg = 15;
3951  t = ae_sqrt(-x, _state);
3952  zeta = -2.0*x*t/3.0;
3953  t = ae_sqrt(t, _state);
3954  k = sqpii/t;
3955  z = 1.0/zeta;
3956  zz = z*z;
3957  afn = -1.31696323418331795333E-1;
3958  afn = afn*zz-6.26456544431912369773E-1;
3959  afn = afn*zz-6.93158036036933542233E-1;
3960  afn = afn*zz-2.79779981545119124951E-1;
3961  afn = afn*zz-4.91900132609500318020E-2;
3962  afn = afn*zz-4.06265923594885404393E-3;
3963  afn = afn*zz-1.59276496239262096340E-4;
3964  afn = afn*zz-2.77649108155232920844E-6;
3965  afn = afn*zz-1.67787698489114633780E-8;
3966  afd = 1.00000000000000000000E0;
3967  afd = afd*zz+1.33560420706553243746E1;
3968  afd = afd*zz+3.26825032795224613948E1;
3969  afd = afd*zz+2.67367040941499554804E1;
3970  afd = afd*zz+9.18707402907259625840E0;
3971  afd = afd*zz+1.47529146771666414581E0;
3972  afd = afd*zz+1.15687173795188044134E-1;
3973  afd = afd*zz+4.40291641615211203805E-3;
3974  afd = afd*zz+7.54720348287414296618E-5;
3975  afd = afd*zz+4.51850092970580378464E-7;
3976  uf = 1.0+zz*afn/afd;
3977  agn = 1.97339932091685679179E-2;
3978  agn = agn*zz+3.91103029615688277255E-1;
3979  agn = agn*zz+1.06579897599595591108E0;
3980  agn = agn*zz+9.39169229816650230044E-1;
3981  agn = agn*zz+3.51465656105547619242E-1;
3982  agn = agn*zz+6.33888919628925490927E-2;
3983  agn = agn*zz+5.85804113048388458567E-3;
3984  agn = agn*zz+2.82851600836737019778E-4;
3985  agn = agn*zz+6.98793669997260967291E-6;
3986  agn = agn*zz+8.11789239554389293311E-8;
3987  agn = agn*zz+3.41551784765923618484E-10;
3988  agd = 1.00000000000000000000E0;
3989  agd = agd*zz+9.30892908077441974853E0;
3990  agd = agd*zz+1.98352928718312140417E1;
3991  agd = agd*zz+1.55646628932864612953E1;
3992  agd = agd*zz+5.47686069422975497931E0;
3993  agd = agd*zz+9.54293611618961883998E-1;
3994  agd = agd*zz+8.64580826352392193095E-2;
3995  agd = agd*zz+4.12656523824222607191E-3;
3996  agd = agd*zz+1.01259085116509135510E-4;
3997  agd = agd*zz+1.17166733214413521882E-6;
3998  agd = agd*zz+4.91834570062930015649E-9;
3999  ug = z*agn/agd;
4000  theta = zeta+0.25*ae_pi;
4001  f = ae_sin(theta, _state);
4002  g = ae_cos(theta, _state);
4003  *ai = k*(f*uf-g*ug);
4004  *bi = k*(g*uf+f*ug);
4005  apfn = 1.85365624022535566142E-1;
4006  apfn = apfn*zz+8.86712188052584095637E-1;
4007  apfn = apfn*zz+9.87391981747398547272E-1;
4008  apfn = apfn*zz+4.01241082318003734092E-1;
4009  apfn = apfn*zz+7.10304926289631174579E-2;
4010  apfn = apfn*zz+5.90618657995661810071E-3;
4011  apfn = apfn*zz+2.33051409401776799569E-4;
4012  apfn = apfn*zz+4.08718778289035454598E-6;
4013  apfn = apfn*zz+2.48379932900442457853E-8;
4014  apfd = 1.00000000000000000000E0;
4015  apfd = apfd*zz+1.47345854687502542552E1;
4016  apfd = apfd*zz+3.75423933435489594466E1;
4017  apfd = apfd*zz+3.14657751203046424330E1;
4018  apfd = apfd*zz+1.09969125207298778536E1;
4019  apfd = apfd*zz+1.78885054766999417817E0;
4020  apfd = apfd*zz+1.41733275753662636873E-1;
4021  apfd = apfd*zz+5.44066067017226003627E-3;
4022  apfd = apfd*zz+9.39421290654511171663E-5;
4023  apfd = apfd*zz+5.65978713036027009243E-7;
4024  uf = 1.0+zz*apfn/apfd;
4025  apgn = -3.55615429033082288335E-2;
4026  apgn = apgn*zz-6.37311518129435504426E-1;
4027  apgn = apgn*zz-1.70856738884312371053E0;
4028  apgn = apgn*zz-1.50221872117316635393E0;
4029  apgn = apgn*zz-5.63606665822102676611E-1;
4030  apgn = apgn*zz-1.02101031120216891789E-1;
4031  apgn = apgn*zz-9.48396695961445269093E-3;
4032  apgn = apgn*zz-4.60325307486780994357E-4;
4033  apgn = apgn*zz-1.14300836484517375919E-5;
4034  apgn = apgn*zz-1.33415518685547420648E-7;
4035  apgn = apgn*zz-5.63803833958893494476E-10;
4036  apgd = 1.00000000000000000000E0;
4037  apgd = apgd*zz+9.85865801696130355144E0;
4038  apgd = apgd*zz+2.16401867356585941885E1;
4039  apgd = apgd*zz+1.73130776389749389525E1;
4040  apgd = apgd*zz+6.17872175280828766327E0;
4041  apgd = apgd*zz+1.08848694396321495475E0;
4042  apgd = apgd*zz+9.95005543440888479402E-2;
4043  apgd = apgd*zz+4.78468199683886610842E-3;
4044  apgd = apgd*zz+1.18159633322838625562E-4;
4045  apgd = apgd*zz+1.37480673554219441465E-6;
4046  apgd = apgd*zz+5.79912514929147598821E-9;
4047  ug = z*apgn/apgd;
4048  k = sqpii*t;
4049  *aip = -k*(g*uf+f*ug);
4050  *bip = k*(f*uf-g*ug);
4051  return;
4052  }
4053  if( ae_fp_greater_eq(x,2.09) )
4054  {
4055  domflg = 5;
4056  t = ae_sqrt(x, _state);
4057  zeta = 2.0*x*t/3.0;
4058  g = ae_exp(zeta, _state);
4059  t = ae_sqrt(t, _state);
4060  k = 2.0*t*g;
4061  z = 1.0/zeta;
4062  an = 3.46538101525629032477E-1;
4063  an = an*z+1.20075952739645805542E1;
4064  an = an*z+7.62796053615234516538E1;
4065  an = an*z+1.68089224934630576269E2;
4066  an = an*z+1.59756391350164413639E2;
4067  an = an*z+7.05360906840444183113E1;
4068  an = an*z+1.40264691163389668864E1;
4069  an = an*z+9.99999999999999995305E-1;
4070  ad = 5.67594532638770212846E-1;
4071  ad = ad*z+1.47562562584847203173E1;
4072  ad = ad*z+8.45138970141474626562E1;
4073  ad = ad*z+1.77318088145400459522E2;
4074  ad = ad*z+1.64234692871529701831E2;
4075  ad = ad*z+7.14778400825575695274E1;
4076  ad = ad*z+1.40959135607834029598E1;
4077  ad = ad*z+1.00000000000000000470E0;
4078  f = an/ad;
4079  *ai = sqpii*f/k;
4080  k = -0.5*sqpii*t/g;
4081  apn = 6.13759184814035759225E-1;
4082  apn = apn*z+1.47454670787755323881E1;
4083  apn = apn*z+8.20584123476060982430E1;
4084  apn = apn*z+1.71184781360976385540E2;
4085  apn = apn*z+1.59317847137141783523E2;
4086  apn = apn*z+6.99778599330103016170E1;
4087  apn = apn*z+1.39470856980481566958E1;
4088  apn = apn*z+1.00000000000000000550E0;
4089  apd = 3.34203677749736953049E-1;
4090  apd = apd*z+1.11810297306158156705E1;
4091  apd = apd*z+7.11727352147859965283E1;
4092  apd = apd*z+1.58778084372838313640E2;
4093  apd = apd*z+1.53206427475809220834E2;
4094  apd = apd*z+6.86752304592780337944E1;
4095  apd = apd*z+1.38498634758259442477E1;
4096  apd = apd*z+9.99999999999999994502E-1;
4097  f = apn/apd;
4098  *aip = f*k;
4099  if( ae_fp_greater(x,8.3203353) )
4100  {
4101  bn16 = -2.53240795869364152689E-1;
4102  bn16 = bn16*z+5.75285167332467384228E-1;
4103  bn16 = bn16*z-3.29907036873225371650E-1;
4104  bn16 = bn16*z+6.44404068948199951727E-2;
4105  bn16 = bn16*z-3.82519546641336734394E-3;
4106  bd16 = 1.00000000000000000000E0;
4107  bd16 = bd16*z-7.15685095054035237902E0;
4108  bd16 = bd16*z+1.06039580715664694291E1;
4109  bd16 = bd16*z-5.23246636471251500874E0;
4110  bd16 = bd16*z+9.57395864378383833152E-1;
4111  bd16 = bd16*z-5.50828147163549611107E-2;
4112  f = z*bn16/bd16;
4113  k = sqpii*g;
4114  *bi = k*(1.0+f)/t;
4115  bppn = 4.65461162774651610328E-1;
4116  bppn = bppn*z-1.08992173800493920734E0;
4117  bppn = bppn*z+6.38800117371827987759E-1;
4118  bppn = bppn*z-1.26844349553102907034E-1;
4119  bppn = bppn*z+7.62487844342109852105E-3;
4120  bppd = 1.00000000000000000000E0;
4121  bppd = bppd*z-8.70622787633159124240E0;
4122  bppd = bppd*z+1.38993162704553213172E1;
4123  bppd = bppd*z-7.14116144616431159572E0;
4124  bppd = bppd*z+1.34008595960680518666E0;
4125  bppd = bppd*z-7.84273211323341930448E-2;
4126  f = z*bppn/bppd;
4127  *bip = k*t*(1.0+f);
4128  return;
4129  }
4130  }
4131  f = 1.0;
4132  g = x;
4133  t = 1.0;
4134  uf = 1.0;
4135  ug = x;
4136  k = 1.0;
4137  z = x*x*x;
4139  {
4140  uf = uf*z;
4141  k = k+1.0;
4142  uf = uf/k;
4143  ug = ug*z;
4144  k = k+1.0;
4145  ug = ug/k;
4146  uf = uf/k;
4147  f = f+uf;
4148  k = k+1.0;
4149  ug = ug/k;
4150  g = g+ug;
4151  t = ae_fabs(uf/f, _state);
4152  }
4153  uf = c1*f;
4154  ug = c2*g;
4155  if( domflg%2==0 )
4156  {
4157  *ai = uf-ug;
4158  }
4159  if( domflg/2%2==0 )
4160  {
4161  *bi = sqrt3*(uf+ug);
4162  }
4163  k = 4.0;
4164  uf = x*x/2.0;
4165  ug = z/3.0;
4166  f = uf;
4167  g = 1.0+ug;
4168  uf = uf/3.0;
4169  t = 1.0;
4171  {
4172  uf = uf*z;
4173  ug = ug/k;
4174  k = k+1.0;
4175  ug = ug*z;
4176  uf = uf/k;
4177  f = f+uf;
4178  k = k+1.0;
4179  ug = ug/k;
4180  uf = uf/k;
4181  g = g+ug;
4182  k = k+1.0;
4183  t = ae_fabs(ug/g, _state);
4184  }
4185  uf = c1*f;
4186  ug = c2*g;
4187  if( domflg/4%2==0 )
4188  {
4189  *aip = uf-ug;
4190  }
4191  if( domflg/8%2==0 )
4192  {
4193  *bip = sqrt3*(uf+ug);
4194  }
4195 }
4196 
4197 
4198 
4199 
4200 /*************************************************************************
4201 Bessel function of order zero
4202 
4203 Returns Bessel function of order zero of the argument.
4204 
4205 The domain is divided into the intervals [0, 5] and
4206 (5, infinity). In the first interval the following rational
4207 approximation is used:
4208 
4209 
4210  2 2
4211 (w - r ) (w - r ) P (w) / Q (w)
4212  1 2 3 8
4213 
4214  2
4215 where w = x and the two r's are zeros of the function.
4216 
4217 In the second interval, the Hankel asymptotic expansion
4218 is employed with two rational functions of degree 6/6
4219 and 7/7.
4220 
4221 ACCURACY:
4222 
4223  Absolute error:
4224 arithmetic domain # trials peak rms
4225  IEEE 0, 30 60000 4.2e-16 1.1e-16
4226 
4227 Cephes Math Library Release 2.8: June, 2000
4228 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
4229 *************************************************************************/
4230 double besselj0(double x, ae_state *_state)
4231 {
4232  double xsq;
4233  double nn;
4234  double pzero;
4235  double qzero;
4236  double p1;
4237  double q1;
4238  double result;
4239 
4240 
4241  if( ae_fp_less(x,0) )
4242  {
4243  x = -x;
4244  }
4245  if( ae_fp_greater(x,8.0) )
4246  {
4247  bessel_besselasympt0(x, &pzero, &qzero, _state);
4248  nn = x-ae_pi/4;
4249  result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_cos(nn, _state)-qzero*ae_sin(nn, _state));
4250  return result;
4251  }
4252  xsq = ae_sqr(x, _state);
4253  p1 = 26857.86856980014981415848441;
4254  p1 = -40504123.71833132706360663322+xsq*p1;
4255  p1 = 25071582855.36881945555156435+xsq*p1;
4256  p1 = -8085222034853.793871199468171+xsq*p1;
4257  p1 = 1434354939140344.111664316553+xsq*p1;
4258  p1 = -136762035308817138.6865416609+xsq*p1;
4259  p1 = 6382059341072356562.289432465+xsq*p1;
4260  p1 = -117915762910761053603.8440800+xsq*p1;
4261  p1 = 493378725179413356181.6813446+xsq*p1;
4262  q1 = 1.0;
4263  q1 = 1363.063652328970604442810507+xsq*q1;
4264  q1 = 1114636.098462985378182402543+xsq*q1;
4265  q1 = 669998767.2982239671814028660+xsq*q1;
4266  q1 = 312304311494.1213172572469442+xsq*q1;
4267  q1 = 112775673967979.8507056031594+xsq*q1;
4268  q1 = 30246356167094626.98627330784+xsq*q1;
4269  q1 = 5428918384092285160.200195092+xsq*q1;
4270  q1 = 493378725179413356211.3278438+xsq*q1;
4271  result = p1/q1;
4272  return result;
4273 }
4274 
4275 
4276 /*************************************************************************
4277 Bessel function of order one
4278 
4279 Returns Bessel function of order one of the argument.
4280 
4281 The domain is divided into the intervals [0, 8] and
4282 (8, infinity). In the first interval a 24 term Chebyshev
4283 expansion is used. In the second, the asymptotic
4284 trigonometric representation is employed using two
4285 rational functions of degree 5/5.
4286 
4287 ACCURACY:
4288 
4289  Absolute error:
4290 arithmetic domain # trials peak rms
4291  IEEE 0, 30 30000 2.6e-16 1.1e-16
4292 
4293 Cephes Math Library Release 2.8: June, 2000
4294 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
4295 *************************************************************************/
4296 double besselj1(double x, ae_state *_state)
4297 {
4298  double s;
4299  double xsq;
4300  double nn;
4301  double pzero;
4302  double qzero;
4303  double p1;
4304  double q1;
4305  double result;
4306 
4307 
4308  s = ae_sign(x, _state);
4309  if( ae_fp_less(x,0) )
4310  {
4311  x = -x;
4312  }
4313  if( ae_fp_greater(x,8.0) )
4314  {
4315  bessel_besselasympt1(x, &pzero, &qzero, _state);
4316  nn = x-3*ae_pi/4;
4317  result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_cos(nn, _state)-qzero*ae_sin(nn, _state));
4318  if( ae_fp_less(s,0) )
4319  {
4320  result = -result;
4321  }
4322  return result;
4323  }
4324  xsq = ae_sqr(x, _state);
4325  p1 = 2701.122710892323414856790990;
4326  p1 = -4695753.530642995859767162166+xsq*p1;
4327  p1 = 3413234182.301700539091292655+xsq*p1;
4328  p1 = -1322983480332.126453125473247+xsq*p1;
4329  p1 = 290879526383477.5409737601689+xsq*p1;
4330  p1 = -35888175699101060.50743641413+xsq*p1;
4331  p1 = 2316433580634002297.931815435+xsq*p1;
4332  p1 = -66721065689249162980.20941484+xsq*p1;
4333  p1 = 581199354001606143928.050809+xsq*p1;
4334  q1 = 1.0;
4335  q1 = 1606.931573481487801970916749+xsq*q1;
4336  q1 = 1501793.594998585505921097578+xsq*q1;
4337  q1 = 1013863514.358673989967045588+xsq*q1;
4338  q1 = 524371026216.7649715406728642+xsq*q1;
4339  q1 = 208166122130760.7351240184229+xsq*q1;
4340  q1 = 60920613989175217.46105196863+xsq*q1;
4341  q1 = 11857707121903209998.37113348+xsq*q1;
4342  q1 = 1162398708003212287858.529400+xsq*q1;
4343  result = s*x*p1/q1;
4344  return result;
4345 }
4346 
4347 
4348 /*************************************************************************
4349 Bessel function of integer order
4350 
4351 Returns Bessel function of order n, where n is a
4352 (possibly negative) integer.
4353 
4354 The ratio of jn(x) to j0(x) is computed by backward
4355 recurrence. First the ratio jn/jn-1 is found by a
4356 continued fraction expansion. Then the recurrence
4357 relating successive orders is applied until j0 or j1 is
4358 reached.
4359 
4360 If n = 0 or 1 the routine for j0 or j1 is called
4361 directly.
4362 
4363 ACCURACY:
4364 
4365  Absolute error:
4366 arithmetic range # trials peak rms
4367  IEEE 0, 30 5000 4.4e-16 7.9e-17
4368 
4369 
4370 Not suitable for large n or x. Use jv() (fractional order) instead.
4371 
4372 Cephes Math Library Release 2.8: June, 2000
4373 Copyright 1984, 1987, 2000 by Stephen L. Moshier
4374 *************************************************************************/
4375 double besseljn(ae_int_t n, double x, ae_state *_state)
4376 {
4377  double pkm2;
4378  double pkm1;
4379  double pk;
4380  double xk;
4381  double r;
4382  double ans;
4383  ae_int_t k;
4384  ae_int_t sg;
4385  double result;
4386 
4387 
4388  if( n<0 )
4389  {
4390  n = -n;
4391  if( n%2==0 )
4392  {
4393  sg = 1;
4394  }
4395  else
4396  {
4397  sg = -1;
4398  }
4399  }
4400  else
4401  {
4402  sg = 1;
4403  }
4404  if( ae_fp_less(x,0) )
4405  {
4406  if( n%2!=0 )
4407  {
4408  sg = -sg;
4409  }
4410  x = -x;
4411  }
4412  if( n==0 )
4413  {
4414  result = sg*besselj0(x, _state);
4415  return result;
4416  }
4417  if( n==1 )
4418  {
4419  result = sg*besselj1(x, _state);
4420  return result;
4421  }
4422  if( n==2 )
4423  {
4424  if( ae_fp_eq(x,0) )
4425  {
4426  result = 0;
4427  }
4428  else
4429  {
4430  result = sg*(2.0*besselj1(x, _state)/x-besselj0(x, _state));
4431  }
4432  return result;
4433  }
4434  if( ae_fp_less(x,ae_machineepsilon) )
4435  {
4436  result = 0;
4437  return result;
4438  }
4439  k = 53;
4440  pk = 2*(n+k);
4441  ans = pk;
4442  xk = x*x;
4443  do
4444  {
4445  pk = pk-2.0;
4446  ans = pk-xk/ans;
4447  k = k-1;
4448  }
4449  while(k!=0);
4450  ans = x/ans;
4451  pk = 1.0;
4452  pkm1 = 1.0/ans;
4453  k = n-1;
4454  r = 2*k;
4455  do
4456  {
4457  pkm2 = (pkm1*r-pk*x)/x;
4458  pk = pkm1;
4459  pkm1 = pkm2;
4460  r = r-2.0;
4461  k = k-1;
4462  }
4463  while(k!=0);
4464  if( ae_fp_greater(ae_fabs(pk, _state),ae_fabs(pkm1, _state)) )
4465  {
4466  ans = besselj1(x, _state)/pk;
4467  }
4468  else
4469  {
4470  ans = besselj0(x, _state)/pkm1;
4471  }
4472  result = sg*ans;
4473  return result;
4474 }
4475 
4476 
4477 /*************************************************************************
4478 Bessel function of the second kind, order zero
4479 
4480 Returns Bessel function of the second kind, of order
4481 zero, of the argument.
4482 
4483 The domain is divided into the intervals [0, 5] and
4484 (5, infinity). In the first interval a rational approximation
4485 R(x) is employed to compute
4486  y0(x) = R(x) + 2 * log(x) * j0(x) / PI.
4487 Thus a call to j0() is required.
4488 
4489 In the second interval, the Hankel asymptotic expansion
4490 is employed with two rational functions of degree 6/6
4491 and 7/7.
4492 
4493 
4494 
4495 ACCURACY:
4496 
4497  Absolute error, when y0(x) < 1; else relative error:
4498 
4499 arithmetic domain # trials peak rms
4500  IEEE 0, 30 30000 1.3e-15 1.6e-16
4501 
4502 Cephes Math Library Release 2.8: June, 2000
4503 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
4504 *************************************************************************/
4505 double bessely0(double x, ae_state *_state)
4506 {
4507  double nn;
4508  double xsq;
4509  double pzero;
4510  double qzero;
4511  double p4;
4512  double q4;
4513  double result;
4514 
4515 
4516  if( ae_fp_greater(x,8.0) )
4517  {
4518  bessel_besselasympt0(x, &pzero, &qzero, _state);
4519  nn = x-ae_pi/4;
4520  result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_sin(nn, _state)+qzero*ae_cos(nn, _state));
4521  return result;
4522  }
4523  xsq = ae_sqr(x, _state);
4524  p4 = -41370.35497933148554125235152;
4525  p4 = 59152134.65686889654273830069+xsq*p4;
4526  p4 = -34363712229.79040378171030138+xsq*p4;
4527  p4 = 10255208596863.94284509167421+xsq*p4;
4528  p4 = -1648605817185729.473122082537+xsq*p4;
4529  p4 = 137562431639934407.8571335453+xsq*p4;
4530  p4 = -5247065581112764941.297350814+xsq*p4;
4531  p4 = 65874732757195549259.99402049+xsq*p4;
4532  p4 = -27502866786291095837.01933175+xsq*p4;
4533  q4 = 1.0;
4534  q4 = 1282.452772478993804176329391+xsq*q4;
4535  q4 = 1001702.641288906265666651753+xsq*q4;
4536  q4 = 579512264.0700729537480087915+xsq*q4;
4537  q4 = 261306575504.1081249568482092+xsq*q4;
4538  q4 = 91620380340751.85262489147968+xsq*q4;
4539  q4 = 23928830434997818.57439356652+xsq*q4;
4540  q4 = 4192417043410839973.904769661+xsq*q4;
4541  q4 = 372645883898616588198.9980+xsq*q4;
4542  result = p4/q4+2/ae_pi*besselj0(x, _state)*ae_log(x, _state);
4543  return result;
4544 }
4545 
4546 
4547 /*************************************************************************
4548 Bessel function of second kind of order one
4549 
4550 Returns Bessel function of the second kind of order one
4551 of the argument.
4552 
4553 The domain is divided into the intervals [0, 8] and
4554 (8, infinity). In the first interval a 25 term Chebyshev
4555 expansion is used, and a call to j1() is required.
4556 In the second, the asymptotic trigonometric representation
4557 is employed using two rational functions of degree 5/5.
4558 
4559 ACCURACY:
4560 
4561  Absolute error:
4562 arithmetic domain # trials peak rms
4563  IEEE 0, 30 30000 1.0e-15 1.3e-16
4564 
4565 Cephes Math Library Release 2.8: June, 2000
4566 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
4567 *************************************************************************/
4568 double bessely1(double x, ae_state *_state)
4569 {
4570  double nn;
4571  double xsq;
4572  double pzero;
4573  double qzero;
4574  double p4;
4575  double q4;
4576  double result;
4577 
4578 
4579  if( ae_fp_greater(x,8.0) )
4580  {
4581  bessel_besselasympt1(x, &pzero, &qzero, _state);
4582  nn = x-3*ae_pi/4;
4583  result = ae_sqrt(2/ae_pi/x, _state)*(pzero*ae_sin(nn, _state)+qzero*ae_cos(nn, _state));
4584  return result;
4585  }
4586  xsq = ae_sqr(x, _state);
4587  p4 = -2108847.540133123652824139923;
4588  p4 = 3639488548.124002058278999428+xsq*p4;
4589  p4 = -2580681702194.450950541426399+xsq*p4;
4590  p4 = 956993023992168.3481121552788+xsq*p4;
4591  p4 = -196588746272214065.8820322248+xsq*p4;
4592  p4 = 21931073399177975921.11427556+xsq*p4;
4593  p4 = -1212297555414509577913.561535+xsq*p4;
4594  p4 = 26554738314348543268942.48968+xsq*p4;
4595  p4 = -99637534243069222259967.44354+xsq*p4;
4596  q4 = 1.0;
4597  q4 = 1612.361029677000859332072312+xsq*q4;
4598  q4 = 1563282.754899580604737366452+xsq*q4;
4599  q4 = 1128686837.169442121732366891+xsq*q4;
4600  q4 = 646534088126.5275571961681500+xsq*q4;
4601  q4 = 297663212564727.6729292742282+xsq*q4;
4602  q4 = 108225825940881955.2553850180+xsq*q4;
4603  q4 = 29549879358971486742.90758119+xsq*q4;
4604  q4 = 5435310377188854170800.653097+xsq*q4;
4605  q4 = 508206736694124324531442.4152+xsq*q4;
4606  result = x*p4/q4+2/ae_pi*(besselj1(x, _state)*ae_log(x, _state)-1/x);
4607  return result;
4608 }
4609 
4610 
4611 /*************************************************************************
4612 Bessel function of second kind of integer order
4613 
4614 Returns Bessel function of order n, where n is a
4615 (possibly negative) integer.
4616 
4617 The function is evaluated by forward recurrence on
4618 n, starting with values computed by the routines
4619 y0() and y1().
4620 
4621 If n = 0 or 1 the routine for y0 or y1 is called
4622 directly.
4623 
4624 ACCURACY:
4625  Absolute error, except relative
4626  when y > 1:
4627 arithmetic domain # trials peak rms
4628  IEEE 0, 30 30000 3.4e-15 4.3e-16
4629 
4630 Cephes Math Library Release 2.8: June, 2000
4631 Copyright 1984, 1987, 2000 by Stephen L. Moshier
4632 *************************************************************************/
4633 double besselyn(ae_int_t n, double x, ae_state *_state)
4634 {
4635  ae_int_t i;
4636  double a;
4637  double b;
4638  double tmp;
4639  double s;
4640  double result;
4641 
4642 
4643  s = 1;
4644  if( n<0 )
4645  {
4646  n = -n;
4647  if( n%2!=0 )
4648  {
4649  s = -1;
4650  }
4651  }
4652  if( n==0 )
4653  {
4654  result = bessely0(x, _state);
4655  return result;
4656  }
4657  if( n==1 )
4658  {
4659  result = s*bessely1(x, _state);
4660  return result;
4661  }
4662  a = bessely0(x, _state);
4663  b = bessely1(x, _state);
4664  for(i=1; i<=n-1; i++)
4665  {
4666  tmp = b;
4667  b = 2*i/x*b-a;
4668  a = tmp;
4669  }
4670  result = s*b;
4671  return result;
4672 }
4673 
4674 
4675 /*************************************************************************
4676 Modified Bessel function of order zero
4677 
4678 Returns modified Bessel function of order zero of the
4679 argument.
4680 
4681 The function is defined as i0(x) = j0( ix ).
4682 
4683 The range is partitioned into the two intervals [0,8] and
4684 (8, infinity). Chebyshev polynomial expansions are employed
4685 in each interval.
4686 
4687 ACCURACY:
4688 
4689  Relative error:
4690 arithmetic domain # trials peak rms
4691  IEEE 0,30 30000 5.8e-16 1.4e-16
4692 
4693 Cephes Math Library Release 2.8: June, 2000
4694 Copyright 1984, 1987, 2000 by Stephen L. Moshier
4695 *************************************************************************/
4696 double besseli0(double x, ae_state *_state)
4697 {
4698  double y;
4699  double v;
4700  double z;
4701  double b0;
4702  double b1;
4703  double b2;
4704  double result;
4705 
4706 
4707  if( ae_fp_less(x,0) )
4708  {
4709  x = -x;
4710  }
4711  if( ae_fp_less_eq(x,8.0) )
4712  {
4713  y = x/2.0-2.0;
4714  bessel_besselmfirstcheb(-4.41534164647933937950E-18, &b0, &b1, &b2, _state);
4715  bessel_besselmnextcheb(y, 3.33079451882223809783E-17, &b0, &b1, &b2, _state);
4716  bessel_besselmnextcheb(y, -2.43127984654795469359E-16, &b0, &b1, &b2, _state);
4717  bessel_besselmnextcheb(y, 1.71539128555513303061E-15, &b0, &b1, &b2, _state);
4718  bessel_besselmnextcheb(y, -1.16853328779934516808E-14, &b0, &b1, &b2, _state);
4719  bessel_besselmnextcheb(y, 7.67618549860493561688E-14, &b0, &b1, &b2, _state);
4720  bessel_besselmnextcheb(y, -4.85644678311192946090E-13, &b0, &b1, &b2, _state);
4721  bessel_besselmnextcheb(y, 2.95505266312963983461E-12, &b0, &b1, &b2, _state);
4722  bessel_besselmnextcheb(y, -1.72682629144155570723E-11, &b0, &b1, &b2, _state);
4723  bessel_besselmnextcheb(y, 9.67580903537323691224E-11, &b0, &b1, &b2, _state);
4724  bessel_besselmnextcheb(y, -5.18979560163526290666E-10, &b0, &b1, &b2, _state);
4725  bessel_besselmnextcheb(y, 2.65982372468238665035E-9, &b0, &b1, &b2, _state);
4726  bessel_besselmnextcheb(y, -1.30002500998624804212E-8, &b0, &b1, &b2, _state);
4727  bessel_besselmnextcheb(y, 6.04699502254191894932E-8, &b0, &b1, &b2, _state);
4728  bessel_besselmnextcheb(y, -2.67079385394061173391E-7, &b0, &b1, &b2, _state);
4729  bessel_besselmnextcheb(y, 1.11738753912010371815E-6, &b0, &b1, &b2, _state);
4730  bessel_besselmnextcheb(y, -4.41673835845875056359E-6, &b0, &b1, &b2, _state);
4731  bessel_besselmnextcheb(y, 1.64484480707288970893E-5, &b0, &b1, &b2, _state);
4732  bessel_besselmnextcheb(y, -5.75419501008210370398E-5, &b0, &b1, &b2, _state);
4733  bessel_besselmnextcheb(y, 1.88502885095841655729E-4, &b0, &b1, &b2, _state);
4734  bessel_besselmnextcheb(y, -5.76375574538582365885E-4, &b0, &b1, &b2, _state);
4735  bessel_besselmnextcheb(y, 1.63947561694133579842E-3, &b0, &b1, &b2, _state);
4736  bessel_besselmnextcheb(y, -4.32430999505057594430E-3, &b0, &b1, &b2, _state);
4737  bessel_besselmnextcheb(y, 1.05464603945949983183E-2, &b0, &b1, &b2, _state);
4738  bessel_besselmnextcheb(y, -2.37374148058994688156E-2, &b0, &b1, &b2, _state);
4739  bessel_besselmnextcheb(y, 4.93052842396707084878E-2, &b0, &b1, &b2, _state);
4740  bessel_besselmnextcheb(y, -9.49010970480476444210E-2, &b0, &b1, &b2, _state);
4741  bessel_besselmnextcheb(y, 1.71620901522208775349E-1, &b0, &b1, &b2, _state);
4742  bessel_besselmnextcheb(y, -3.04682672343198398683E-1, &b0, &b1, &b2, _state);
4743  bessel_besselmnextcheb(y, 6.76795274409476084995E-1, &b0, &b1, &b2, _state);
4744  v = 0.5*(b0-b2);
4745  result = ae_exp(x, _state)*v;
4746  return result;
4747  }
4748  z = 32.0/x-2.0;
4749  bessel_besselmfirstcheb(-7.23318048787475395456E-18, &b0, &b1, &b2, _state);
4750  bessel_besselmnextcheb(z, -4.83050448594418207126E-18, &b0, &b1, &b2, _state);
4751  bessel_besselmnextcheb(z, 4.46562142029675999901E-17, &b0, &b1, &b2, _state);
4752  bessel_besselmnextcheb(z, 3.46122286769746109310E-17, &b0, &b1, &b2, _state);
4753  bessel_besselmnextcheb(z, -2.82762398051658348494E-16, &b0, &b1, &b2, _state);
4754  bessel_besselmnextcheb(z, -3.42548561967721913462E-16, &b0, &b1, &b2, _state);
4755  bessel_besselmnextcheb(z, 1.77256013305652638360E-15, &b0, &b1, &b2, _state);
4756  bessel_besselmnextcheb(z, 3.81168066935262242075E-15, &b0, &b1, &b2, _state);
4757  bessel_besselmnextcheb(z, -9.55484669882830764870E-15, &b0, &b1, &b2, _state);
4758  bessel_besselmnextcheb(z, -4.15056934728722208663E-14, &b0, &b1, &b2, _state);
4759  bessel_besselmnextcheb(z, 1.54008621752140982691E-14, &b0, &b1, &b2, _state);
4760  bessel_besselmnextcheb(z, 3.85277838274214270114E-13, &b0, &b1, &b2, _state);
4761  bessel_besselmnextcheb(z, 7.18012445138366623367E-13, &b0, &b1, &b2, _state);
4762  bessel_besselmnextcheb(z, -1.79417853150680611778E-12, &b0, &b1, &b2, _state);
4763  bessel_besselmnextcheb(z, -1.32158118404477131188E-11, &b0, &b1, &b2, _state);
4764  bessel_besselmnextcheb(z, -3.14991652796324136454E-11, &b0, &b1, &b2, _state);
4765  bessel_besselmnextcheb(z, 1.18891471078464383424E-11, &b0, &b1, &b2, _state);
4766  bessel_besselmnextcheb(z, 4.94060238822496958910E-10, &b0, &b1, &b2, _state);
4767  bessel_besselmnextcheb(z, 3.39623202570838634515E-9, &b0, &b1, &b2, _state);
4768  bessel_besselmnextcheb(z, 2.26666899049817806459E-8, &b0, &b1, &b2, _state);
4769  bessel_besselmnextcheb(z, 2.04891858946906374183E-7, &b0, &b1, &b2, _state);
4770  bessel_besselmnextcheb(z, 2.89137052083475648297E-6, &b0, &b1, &b2, _state);
4771  bessel_besselmnextcheb(z, 6.88975834691682398426E-5, &b0, &b1, &b2, _state);
4772  bessel_besselmnextcheb(z, 3.36911647825569408990E-3, &b0, &b1, &b2, _state);
4773  bessel_besselmnextcheb(z, 8.04490411014108831608E-1, &b0, &b1, &b2, _state);
4774  v = 0.5*(b0-b2);
4775  result = ae_exp(x, _state)*v/ae_sqrt(x, _state);
4776  return result;
4777 }
4778 
4779 
4780 /*************************************************************************
4781 Modified Bessel function of order one
4782 
4783 Returns modified Bessel function of order one of the
4784 argument.
4785 
4786 The function is defined as i1(x) = -i j1( ix ).
4787 
4788 The range is partitioned into the two intervals [0,8] and
4789 (8, infinity). Chebyshev polynomial expansions are employed
4790 in each interval.
4791 
4792 ACCURACY:
4793 
4794  Relative error:
4795 arithmetic domain # trials peak rms
4796  IEEE 0, 30 30000 1.9e-15 2.1e-16
4797 
4798 Cephes Math Library Release 2.8: June, 2000
4799 Copyright 1985, 1987, 2000 by Stephen L. Moshier
4800 *************************************************************************/
4801 double besseli1(double x, ae_state *_state)
4802 {
4803  double y;
4804  double z;
4805  double v;
4806  double b0;
4807  double b1;
4808  double b2;
4809  double result;
4810 
4811 
4812  z = ae_fabs(x, _state);
4813  if( ae_fp_less_eq(z,8.0) )
4814  {
4815  y = z/2.0-2.0;
4816  bessel_besselm1firstcheb(2.77791411276104639959E-18, &b0, &b1, &b2, _state);
4817  bessel_besselm1nextcheb(y, -2.11142121435816608115E-17, &b0, &b1, &b2, _state);
4818  bessel_besselm1nextcheb(y, 1.55363195773620046921E-16, &b0, &b1, &b2, _state);
4819  bessel_besselm1nextcheb(y, -1.10559694773538630805E-15, &b0, &b1, &b2, _state);
4820  bessel_besselm1nextcheb(y, 7.60068429473540693410E-15, &b0, &b1, &b2, _state);
4821  bessel_besselm1nextcheb(y, -5.04218550472791168711E-14, &b0, &b1, &b2, _state);
4822  bessel_besselm1nextcheb(y, 3.22379336594557470981E-13, &b0, &b1, &b2, _state);
4823  bessel_besselm1nextcheb(y, -1.98397439776494371520E-12, &b0, &b1, &b2, _state);
4824  bessel_besselm1nextcheb(y, 1.17361862988909016308E-11, &b0, &b1, &b2, _state);
4825  bessel_besselm1nextcheb(y, -6.66348972350202774223E-11, &b0, &b1, &b2, _state);
4826  bessel_besselm1nextcheb(y, 3.62559028155211703701E-10, &b0, &b1, &b2, _state);
4827  bessel_besselm1nextcheb(y, -1.88724975172282928790E-9, &b0, &b1, &b2, _state);
4828  bessel_besselm1nextcheb(y, 9.38153738649577178388E-9, &b0, &b1, &b2, _state);
4829  bessel_besselm1nextcheb(y, -4.44505912879632808065E-8, &b0, &b1, &b2, _state);
4830  bessel_besselm1nextcheb(y, 2.00329475355213526229E-7, &b0, &b1, &b2, _state);
4831  bessel_besselm1nextcheb(y, -8.56872026469545474066E-7, &b0, &b1, &b2, _state);
4832  bessel_besselm1nextcheb(y, 3.47025130813767847674E-6, &b0, &b1, &b2, _state);
4833  bessel_besselm1nextcheb(y, -1.32731636560394358279E-5, &b0, &b1, &b2, _state);
4834  bessel_besselm1nextcheb(y, 4.78156510755005422638E-5, &b0, &b1, &b2, _state);
4835  bessel_besselm1nextcheb(y, -1.61760815825896745588E-4, &b0, &b1, &b2, _state);
4836  bessel_besselm1nextcheb(y, 5.12285956168575772895E-4, &b0, &b1, &b2, _state);
4837  bessel_besselm1nextcheb(y, -1.51357245063125314899E-3, &b0, &b1, &b2, _state);
4838  bessel_besselm1nextcheb(y, 4.15642294431288815669E-3, &b0, &b1, &b2, _state);
4839  bessel_besselm1nextcheb(y, -1.05640848946261981558E-2, &b0, &b1, &b2, _state);
4840  bessel_besselm1nextcheb(y, 2.47264490306265168283E-2, &b0, &b1, &b2, _state);
4841  bessel_besselm1nextcheb(y, -5.29459812080949914269E-2, &b0, &b1, &b2, _state);
4842  bessel_besselm1nextcheb(y, 1.02643658689847095384E-1, &b0, &b1, &b2, _state);
4843  bessel_besselm1nextcheb(y, -1.76416518357834055153E-1, &b0, &b1, &b2, _state);
4844  bessel_besselm1nextcheb(y, 2.52587186443633654823E-1, &b0, &b1, &b2, _state);
4845  v = 0.5*(b0-b2);
4846  z = v*z*ae_exp(z, _state);
4847  }
4848  else
4849  {
4850  y = 32.0/z-2.0;
4851  bessel_besselm1firstcheb(7.51729631084210481353E-18, &b0, &b1, &b2, _state);
4852  bessel_besselm1nextcheb(y, 4.41434832307170791151E-18, &b0, &b1, &b2, _state);
4853  bessel_besselm1nextcheb(y, -4.65030536848935832153E-17, &b0, &b1, &b2, _state);
4854  bessel_besselm1nextcheb(y, -3.20952592199342395980E-17, &b0, &b1, &b2, _state);
4855  bessel_besselm1nextcheb(y, 2.96262899764595013876E-16, &b0, &b1, &b2, _state);
4856  bessel_besselm1nextcheb(y, 3.30820231092092828324E-16, &b0, &b1, &b2, _state);
4857  bessel_besselm1nextcheb(y, -1.88035477551078244854E-15, &b0, &b1, &b2, _state);
4858  bessel_besselm1nextcheb(y, -3.81440307243700780478E-15, &b0, &b1, &b2, _state);
4859  bessel_besselm1nextcheb(y, 1.04202769841288027642E-14, &b0, &b1, &b2, _state);
4860  bessel_besselm1nextcheb(y, 4.27244001671195135429E-14, &b0, &b1, &b2, _state);
4861  bessel_besselm1nextcheb(y, -2.10154184277266431302E-14, &b0, &b1, &b2, _state);
4862  bessel_besselm1nextcheb(y, -4.08355111109219731823E-13, &b0, &b1, &b2, _state);
4863  bessel_besselm1nextcheb(y, -7.19855177624590851209E-13, &b0, &b1, &b2, _state);
4864  bessel_besselm1nextcheb(y, 2.03562854414708950722E-12, &b0, &b1, &b2, _state);
4865  bessel_besselm1nextcheb(y, 1.41258074366137813316E-11, &b0, &b1, &b2, _state);
4866  bessel_besselm1nextcheb(y, 3.25260358301548823856E-11, &b0, &b1, &b2, _state);
4867  bessel_besselm1nextcheb(y, -1.89749581235054123450E-11, &b0, &b1, &b2, _state);
4868  bessel_besselm1nextcheb(y, -5.58974346219658380687E-10, &b0, &b1, &b2, _state);
4869  bessel_besselm1nextcheb(y, -3.83538038596423702205E-9, &b0, &b1, &b2, _state);
4870  bessel_besselm1nextcheb(y, -2.63146884688951950684E-8, &b0, &b1, &b2, _state);
4871  bessel_besselm1nextcheb(y, -2.51223623787020892529E-7, &b0, &b1, &b2, _state);
4872  bessel_besselm1nextcheb(y, -3.88256480887769039346E-6, &b0, &b1, &b2, _state);
4873  bessel_besselm1nextcheb(y, -1.10588938762623716291E-4, &b0, &b1, &b2, _state);
4874  bessel_besselm1nextcheb(y, -9.76109749136146840777E-3, &b0, &b1, &b2, _state);
4875  bessel_besselm1nextcheb(y, 7.78576235018280120474E-1, &b0, &b1, &b2, _state);
4876  v = 0.5*(b0-b2);
4877  z = v*ae_exp(z, _state)/ae_sqrt(z, _state);
4878  }
4879  if( ae_fp_less(x,0) )
4880  {
4881  z = -z;
4882  }
4883  result = z;
4884  return result;
4885 }
4886 
4887 
4888 /*************************************************************************
4889 Modified Bessel function, second kind, order zero
4890 
4891 Returns modified Bessel function of the second kind
4892 of order zero of the argument.
4893 
4894 The range is partitioned into the two intervals [0,8] and
4895 (8, infinity). Chebyshev polynomial expansions are employed
4896 in each interval.
4897 
4898 ACCURACY:
4899 
4900 Tested at 2000 random points between 0 and 8. Peak absolute
4901 error (relative when K0 > 1) was 1.46e-14; rms, 4.26e-15.
4902  Relative error:
4903 arithmetic domain # trials peak rms
4904  IEEE 0, 30 30000 1.2e-15 1.6e-16
4905 
4906 Cephes Math Library Release 2.8: June, 2000
4907 Copyright 1984, 1987, 2000 by Stephen L. Moshier
4908 *************************************************************************/
4909 double besselk0(double x, ae_state *_state)
4910 {
4911  double y;
4912  double z;
4913  double v;
4914  double b0;
4915  double b1;
4916  double b2;
4917  double result;
4918 
4919 
4920  ae_assert(ae_fp_greater(x,0), "Domain error in BesselK0: x<=0", _state);
4921  if( ae_fp_less_eq(x,2) )
4922  {
4923  y = x*x-2.0;
4924  bessel_besselmfirstcheb(1.37446543561352307156E-16, &b0, &b1, &b2, _state);
4925  bessel_besselmnextcheb(y, 4.25981614279661018399E-14, &b0, &b1, &b2, _state);
4926  bessel_besselmnextcheb(y, 1.03496952576338420167E-11, &b0, &b1, &b2, _state);
4927  bessel_besselmnextcheb(y, 1.90451637722020886025E-9, &b0, &b1, &b2, _state);
4928  bessel_besselmnextcheb(y, 2.53479107902614945675E-7, &b0, &b1, &b2, _state);
4929  bessel_besselmnextcheb(y, 2.28621210311945178607E-5, &b0, &b1, &b2, _state);
4930  bessel_besselmnextcheb(y, 1.26461541144692592338E-3, &b0, &b1, &b2, _state);
4931  bessel_besselmnextcheb(y, 3.59799365153615016266E-2, &b0, &b1, &b2, _state);
4932  bessel_besselmnextcheb(y, 3.44289899924628486886E-1, &b0, &b1, &b2, _state);
4933  bessel_besselmnextcheb(y, -5.35327393233902768720E-1, &b0, &b1, &b2, _state);
4934  v = 0.5*(b0-b2);
4935  v = v-ae_log(0.5*x, _state)*besseli0(x, _state);
4936  }
4937  else
4938  {
4939  z = 8.0/x-2.0;
4940  bessel_besselmfirstcheb(5.30043377268626276149E-18, &b0, &b1, &b2, _state);
4941  bessel_besselmnextcheb(z, -1.64758043015242134646E-17, &b0, &b1, &b2, _state);
4942  bessel_besselmnextcheb(z, 5.21039150503902756861E-17, &b0, &b1, &b2, _state);
4943  bessel_besselmnextcheb(z, -1.67823109680541210385E-16, &b0, &b1, &b2, _state);
4944  bessel_besselmnextcheb(z, 5.51205597852431940784E-16, &b0, &b1, &b2, _state);
4945  bessel_besselmnextcheb(z, -1.84859337734377901440E-15, &b0, &b1, &b2, _state);
4946  bessel_besselmnextcheb(z, 6.34007647740507060557E-15, &b0, &b1, &b2, _state);
4947  bessel_besselmnextcheb(z, -2.22751332699166985548E-14, &b0, &b1, &b2, _state);
4948  bessel_besselmnextcheb(z, 8.03289077536357521100E-14, &b0, &b1, &b2, _state);
4949  bessel_besselmnextcheb(z, -2.98009692317273043925E-13, &b0, &b1, &b2, _state);
4950  bessel_besselmnextcheb(z, 1.14034058820847496303E-12, &b0, &b1, &b2, _state);
4951  bessel_besselmnextcheb(z, -4.51459788337394416547E-12, &b0, &b1, &b2, _state);
4952  bessel_besselmnextcheb(z, 1.85594911495471785253E-11, &b0, &b1, &b2, _state);
4953  bessel_besselmnextcheb(z, -7.95748924447710747776E-11, &b0, &b1, &b2, _state);
4954  bessel_besselmnextcheb(z, 3.57739728140030116597E-10, &b0, &b1, &b2, _state);
4955  bessel_besselmnextcheb(z, -1.69753450938905987466E-9, &b0, &b1, &b2, _state);
4956  bessel_besselmnextcheb(z, 8.57403401741422608519E-9, &b0, &b1, &b2, _state);
4957  bessel_besselmnextcheb(z, -4.66048989768794782956E-8, &b0, &b1, &b2, _state);
4958  bessel_besselmnextcheb(z, 2.76681363944501510342E-7, &b0, &b1, &b2, _state);
4959  bessel_besselmnextcheb(z, -1.83175552271911948767E-6, &b0, &b1, &b2, _state);
4960  bessel_besselmnextcheb(z, 1.39498137188764993662E-5, &b0, &b1, &b2, _state);
4961  bessel_besselmnextcheb(z, -1.28495495816278026384E-4, &b0, &b1, &b2, _state);
4962  bessel_besselmnextcheb(z, 1.56988388573005337491E-3, &b0, &b1, &b2, _state);
4963  bessel_besselmnextcheb(z, -3.14481013119645005427E-2, &b0, &b1, &b2, _state);
4964  bessel_besselmnextcheb(z, 2.44030308206595545468E0, &b0, &b1, &b2, _state);
4965  v = 0.5*(b0-b2);
4966  v = v*ae_exp(-x, _state)/ae_sqrt(x, _state);
4967  }
4968  result = v;
4969  return result;
4970 }
4971 
4972 
4973 /*************************************************************************
4974 Modified Bessel function, second kind, order one
4975 
4976 Computes the modified Bessel function of the second kind
4977 of order one of the argument.
4978 
4979 The range is partitioned into the two intervals [0,2] and
4980 (2, infinity). Chebyshev polynomial expansions are employed
4981 in each interval.
4982 
4983 ACCURACY:
4984 
4985  Relative error:
4986 arithmetic domain # trials peak rms
4987  IEEE 0, 30 30000 1.2e-15 1.6e-16
4988 
4989 Cephes Math Library Release 2.8: June, 2000
4990 Copyright 1984, 1987, 2000 by Stephen L. Moshier
4991 *************************************************************************/
4992 double besselk1(double x, ae_state *_state)
4993 {
4994  double y;
4995  double z;
4996  double v;
4997  double b0;
4998  double b1;
4999  double b2;
5000  double result;
5001 
5002 
5003  z = 0.5*x;
5004  ae_assert(ae_fp_greater(z,0), "Domain error in K1", _state);
5005  if( ae_fp_less_eq(x,2) )
5006  {
5007  y = x*x-2.0;
5008  bessel_besselm1firstcheb(-7.02386347938628759343E-18, &b0, &b1, &b2, _state);
5009  bessel_besselm1nextcheb(y, -2.42744985051936593393E-15, &b0, &b1, &b2, _state);
5010  bessel_besselm1nextcheb(y, -6.66690169419932900609E-13, &b0, &b1, &b2, _state);
5011  bessel_besselm1nextcheb(y, -1.41148839263352776110E-10, &b0, &b1, &b2, _state);
5012  bessel_besselm1nextcheb(y, -2.21338763073472585583E-8, &b0, &b1, &b2, _state);
5013  bessel_besselm1nextcheb(y, -2.43340614156596823496E-6, &b0, &b1, &b2, _state);
5014  bessel_besselm1nextcheb(y, -1.73028895751305206302E-4, &b0, &b1, &b2, _state);
5015  bessel_besselm1nextcheb(y, -6.97572385963986435018E-3, &b0, &b1, &b2, _state);
5016  bessel_besselm1nextcheb(y, -1.22611180822657148235E-1, &b0, &b1, &b2, _state);
5017  bessel_besselm1nextcheb(y, -3.53155960776544875667E-1, &b0, &b1, &b2, _state);
5018  bessel_besselm1nextcheb(y, 1.52530022733894777053E0, &b0, &b1, &b2, _state);
5019  v = 0.5*(b0-b2);
5020  result = ae_log(z, _state)*besseli1(x, _state)+v/x;
5021  }
5022  else
5023  {
5024  y = 8.0/x-2.0;
5025  bessel_besselm1firstcheb(-5.75674448366501715755E-18, &b0, &b1, &b2, _state);
5026  bessel_besselm1nextcheb(y, 1.79405087314755922667E-17, &b0, &b1, &b2, _state);
5027  bessel_besselm1nextcheb(y, -5.68946255844285935196E-17, &b0, &b1, &b2, _state);
5028  bessel_besselm1nextcheb(y, 1.83809354436663880070E-16, &b0, &b1, &b2, _state);
5029  bessel_besselm1nextcheb(y, -6.05704724837331885336E-16, &b0, &b1, &b2, _state);
5030  bessel_besselm1nextcheb(y, 2.03870316562433424052E-15, &b0, &b1, &b2, _state);
5031  bessel_besselm1nextcheb(y, -7.01983709041831346144E-15, &b0, &b1, &b2, _state);
5032  bessel_besselm1nextcheb(y, 2.47715442448130437068E-14, &b0, &b1, &b2, _state);
5033  bessel_besselm1nextcheb(y, -8.97670518232499435011E-14, &b0, &b1, &b2, _state);
5034  bessel_besselm1nextcheb(y, 3.34841966607842919884E-13, &b0, &b1, &b2, _state);
5035  bessel_besselm1nextcheb(y, -1.28917396095102890680E-12, &b0, &b1, &b2, _state);
5036  bessel_besselm1nextcheb(y, 5.13963967348173025100E-12, &b0, &b1, &b2, _state);
5037  bessel_besselm1nextcheb(y, -2.12996783842756842877E-11, &b0, &b1, &b2, _state);
5038  bessel_besselm1nextcheb(y, 9.21831518760500529508E-11, &b0, &b1, &b2, _state);
5039  bessel_besselm1nextcheb(y, -4.19035475934189648750E-10, &b0, &b1, &b2, _state);
5040  bessel_besselm1nextcheb(y, 2.01504975519703286596E-9, &b0, &b1, &b2, _state);
5041  bessel_besselm1nextcheb(y, -1.03457624656780970260E-8, &b0, &b1, &b2, _state);
5042  bessel_besselm1nextcheb(y, 5.74108412545004946722E-8, &b0, &b1, &b2, _state);
5043  bessel_besselm1nextcheb(y, -3.50196060308781257119E-7, &b0, &b1, &b2, _state);
5044  bessel_besselm1nextcheb(y, 2.40648494783721712015E-6, &b0, &b1, &b2, _state);
5045  bessel_besselm1nextcheb(y, -1.93619797416608296024E-5, &b0, &b1, &b2, _state);
5046  bessel_besselm1nextcheb(y, 1.95215518471351631108E-4, &b0, &b1, &b2, _state);
5047  bessel_besselm1nextcheb(y, -2.85781685962277938680E-3, &b0, &b1, &b2, _state);
5048  bessel_besselm1nextcheb(y, 1.03923736576817238437E-1, &b0, &b1, &b2, _state);
5049  bessel_besselm1nextcheb(y, 2.72062619048444266945E0, &b0, &b1, &b2, _state);
5050  v = 0.5*(b0-b2);
5051  result = ae_exp(-x, _state)*v/ae_sqrt(x, _state);
5052  }
5053  return result;
5054 }
5055 
5056 
5057 /*************************************************************************
5058 Modified Bessel function, second kind, integer order
5059 
5060 Returns modified Bessel function of the second kind
5061 of order n of the argument.
5062 
5063 The range is partitioned into the two intervals [0,9.55] and
5064 (9.55, infinity). An ascending power series is used in the
5065 low range, and an asymptotic expansion in the high range.
5066 
5067 ACCURACY:
5068 
5069  Relative error:
5070 arithmetic domain # trials peak rms
5071  IEEE 0,30 90000 1.8e-8 3.0e-10
5072 
5073 Error is high only near the crossover point x = 9.55
5074 between the two expansions used.
5075 
5076 Cephes Math Library Release 2.8: June, 2000
5077 Copyright 1984, 1987, 1988, 2000 by Stephen L. Moshier
5078 *************************************************************************/
5079 double besselkn(ae_int_t nn, double x, ae_state *_state)
5080 {
5081  double k;
5082  double kf;
5083  double nk1f;
5084  double nkf;
5085  double zn;
5086  double t;
5087  double s;
5088  double z0;
5089  double z;
5090  double ans;
5091  double fn;
5092  double pn;
5093  double pk;
5094  double zmn;
5095  double tlg;
5096  double tox;
5097  ae_int_t i;
5098  ae_int_t n;
5099  double eul;
5100  double result;
5101 
5102 
5103  eul = 5.772156649015328606065e-1;
5104  if( nn<0 )
5105  {
5106  n = -nn;
5107  }
5108  else
5109  {
5110  n = nn;
5111  }
5112  ae_assert(n<=31, "Overflow in BesselKN", _state);
5113  ae_assert(ae_fp_greater(x,0), "Domain error in BesselKN", _state);
5114  if( ae_fp_less_eq(x,9.55) )
5115  {
5116  ans = 0.0;
5117  z0 = 0.25*x*x;
5118  fn = 1.0;
5119  pn = 0.0;
5120  zmn = 1.0;
5121  tox = 2.0/x;
5122  if( n>0 )
5123  {
5124  pn = -eul;
5125  k = 1.0;
5126  for(i=1; i<=n-1; i++)
5127  {
5128  pn = pn+1.0/k;
5129  k = k+1.0;
5130  fn = fn*k;
5131  }
5132  zmn = tox;
5133  if( n==1 )
5134  {
5135  ans = 1.0/x;
5136  }
5137  else
5138  {
5139  nk1f = fn/n;
5140  kf = 1.0;
5141  s = nk1f;
5142  z = -z0;
5143  zn = 1.0;
5144  for(i=1; i<=n-1; i++)
5145  {
5146  nk1f = nk1f/(n-i);
5147  kf = kf*i;
5148  zn = zn*z;
5149  t = nk1f*zn/kf;
5150  s = s+t;
5151  ae_assert(ae_fp_greater(ae_maxrealnumber-ae_fabs(t, _state),ae_fabs(s, _state)), "Overflow in BesselKN", _state);
5152  ae_assert(!(ae_fp_greater(tox,1.0)&&ae_fp_less(ae_maxrealnumber/tox,zmn)), "Overflow in BesselKN", _state);
5153  zmn = zmn*tox;
5154  }
5155  s = s*0.5;
5156  t = ae_fabs(s, _state);
5157  ae_assert(!(ae_fp_greater(zmn,1.0)&&ae_fp_less(ae_maxrealnumber/zmn,t)), "Overflow in BesselKN", _state);
5158  ae_assert(!(ae_fp_greater(t,1.0)&&ae_fp_less(ae_maxrealnumber/t,zmn)), "Overflow in BesselKN", _state);
5159  ans = s*zmn;
5160  }
5161  }
5162  tlg = 2.0*ae_log(0.5*x, _state);
5163  pk = -eul;
5164  if( n==0 )
5165  {
5166  pn = pk;
5167  t = 1.0;
5168  }
5169  else
5170  {
5171  pn = pn+1.0/n;
5172  t = 1.0/fn;
5173  }
5174  s = (pk+pn-tlg)*t;
5175  k = 1.0;
5176  do
5177  {
5178  t = t*(z0/(k*(k+n)));
5179  pk = pk+1.0/k;
5180  pn = pn+1.0/(k+n);
5181  s = s+(pk+pn-tlg)*t;
5182  k = k+1.0;
5183  }
5184  while(ae_fp_greater(ae_fabs(t/s, _state),ae_machineepsilon));
5185  s = 0.5*s/zmn;
5186  if( n%2!=0 )
5187  {
5188  s = -s;
5189  }
5190  ans = ans+s;
5191  result = ans;
5192  return result;
5193  }
5194  if( ae_fp_greater(x,ae_log(ae_maxrealnumber, _state)) )
5195  {
5196  result = 0;
5197  return result;
5198  }
5199  k = n;
5200  pn = 4.0*k*k;
5201  pk = 1.0;
5202  z0 = 8.0*x;
5203  fn = 1.0;
5204  t = 1.0;
5205  s = t;
5206  nkf = ae_maxrealnumber;
5207  i = 0;
5208  do
5209  {
5210  z = pn-pk*pk;
5211  t = t*z/(fn*z0);
5212  nk1f = ae_fabs(t, _state);
5213  if( i>=n&&ae_fp_greater(nk1f,nkf) )
5214  {
5215  break;
5216  }
5217  nkf = nk1f;
5218  s = s+t;
5219  fn = fn+1.0;
5220  pk = pk+2.0;
5221  i = i+1;
5222  }
5223  while(ae_fp_greater(ae_fabs(t/s, _state),ae_machineepsilon));
5224  result = ae_exp(-x, _state)*ae_sqrt(ae_pi/(2.0*x), _state)*s;
5225  return result;
5226 }
5227 
5228 
5229 /*************************************************************************
5230 Internal subroutine
5231 
5232 Cephes Math Library Release 2.8: June, 2000
5233 Copyright 1984, 1987, 2000 by Stephen L. Moshier
5234 *************************************************************************/
5235 static void bessel_besselmfirstcheb(double c,
5236  double* b0,
5237  double* b1,
5238  double* b2,
5239  ae_state *_state)
5240 {
5241 
5242 
5243  *b0 = c;
5244  *b1 = 0.0;
5245  *b2 = 0.0;
5246 }
5247 
5248 
5249 /*************************************************************************
5250 Internal subroutine
5251 
5252 Cephes Math Library Release 2.8: June, 2000
5253 Copyright 1984, 1987, 2000 by Stephen L. Moshier
5254 *************************************************************************/
5255 static void bessel_besselmnextcheb(double x,
5256  double c,
5257  double* b0,
5258  double* b1,
5259  double* b2,
5260  ae_state *_state)
5261 {
5262 
5263 
5264  *b2 = *b1;
5265  *b1 = *b0;
5266  *b0 = x*(*b1)-(*b2)+c;
5267 }
5268 
5269 
5270 /*************************************************************************
5271 Internal subroutine
5272 
5273 Cephes Math Library Release 2.8: June, 2000
5274 Copyright 1984, 1987, 2000 by Stephen L. Moshier
5275 *************************************************************************/
5276 static void bessel_besselm1firstcheb(double c,
5277  double* b0,
5278  double* b1,
5279  double* b2,
5280  ae_state *_state)
5281 {
5282 
5283 
5284  *b0 = c;
5285  *b1 = 0.0;
5286  *b2 = 0.0;
5287 }
5288 
5289 
5290 /*************************************************************************
5291 Internal subroutine
5292 
5293 Cephes Math Library Release 2.8: June, 2000
5294 Copyright 1984, 1987, 2000 by Stephen L. Moshier
5295 *************************************************************************/
5296 static void bessel_besselm1nextcheb(double x,
5297  double c,
5298  double* b0,
5299  double* b1,
5300  double* b2,
5301  ae_state *_state)
5302 {
5303 
5304 
5305  *b2 = *b1;
5306  *b1 = *b0;
5307  *b0 = x*(*b1)-(*b2)+c;
5308 }
5309 
5310 
5311 static void bessel_besselasympt0(double x,
5312  double* pzero,
5313  double* qzero,
5314  ae_state *_state)
5315 {
5316  double xsq;
5317  double p2;
5318  double q2;
5319  double p3;
5320  double q3;
5321 
5322  *pzero = 0;
5323  *qzero = 0;
5324 
5325  xsq = 64.0/(x*x);
5326  p2 = 0.0;
5327  p2 = 2485.271928957404011288128951+xsq*p2;
5328  p2 = 153982.6532623911470917825993+xsq*p2;
5329  p2 = 2016135.283049983642487182349+xsq*p2;
5330  p2 = 8413041.456550439208464315611+xsq*p2;
5331  p2 = 12332384.76817638145232406055+xsq*p2;
5332  p2 = 5393485.083869438325262122897+xsq*p2;
5333  q2 = 1.0;
5334  q2 = 2615.700736920839685159081813+xsq*q2;
5335  q2 = 156001.7276940030940592769933+xsq*q2;
5336  q2 = 2025066.801570134013891035236+xsq*q2;
5337  q2 = 8426449.050629797331554404810+xsq*q2;
5338  q2 = 12338310.22786324960844856182+xsq*q2;
5339  q2 = 5393485.083869438325560444960+xsq*q2;
5340  p3 = -0.0;
5341  p3 = -4.887199395841261531199129300+xsq*p3;
5342  p3 = -226.2630641933704113967255053+xsq*p3;
5343  p3 = -2365.956170779108192723612816+xsq*p3;
5344  p3 = -8239.066313485606568803548860+xsq*p3;
5345  p3 = -10381.41698748464093880530341+xsq*p3;
5346  p3 = -3984.617357595222463506790588+xsq*p3;
5347  q3 = 1.0;
5348  q3 = 408.7714673983499223402830260+xsq*q3;
5349  q3 = 15704.89191515395519392882766+xsq*q3;
5350  q3 = 156021.3206679291652539287109+xsq*q3;
5351  q3 = 533291.3634216897168722255057+xsq*q3;
5352  q3 = 666745.4239319826986004038103+xsq*q3;
5353  q3 = 255015.5108860942382983170882+xsq*q3;
5354  *pzero = p2/q2;
5355  *qzero = 8*p3/q3/x;
5356 }
5357 
5358 
5359 static void bessel_besselasympt1(double x,
5360  double* pzero,
5361  double* qzero,
5362  ae_state *_state)
5363 {
5364  double xsq;
5365  double p2;
5366  double q2;
5367  double p3;
5368  double q3;
5369 
5370  *pzero = 0;
5371  *qzero = 0;
5372 
5373  xsq = 64.0/(x*x);
5374  p2 = -1611.616644324610116477412898;
5375  p2 = -109824.0554345934672737413139+xsq*p2;
5376  p2 = -1523529.351181137383255105722+xsq*p2;
5377  p2 = -6603373.248364939109255245434+xsq*p2;
5378  p2 = -9942246.505077641195658377899+xsq*p2;
5379  p2 = -4435757.816794127857114720794+xsq*p2;
5380  q2 = 1.0;
5381  q2 = -1455.009440190496182453565068+xsq*q2;
5382  q2 = -107263.8599110382011903063867+xsq*q2;
5383  q2 = -1511809.506634160881644546358+xsq*q2;
5384  q2 = -6585339.479723087072826915069+xsq*q2;
5385  q2 = -9934124.389934585658967556309+xsq*q2;
5386  q2 = -4435757.816794127856828016962+xsq*q2;
5387  p3 = 35.26513384663603218592175580;
5388  p3 = 1706.375429020768002061283546+xsq*p3;
5389  p3 = 18494.26287322386679652009819+xsq*p3;
5390  p3 = 66178.83658127083517939992166+xsq*p3;
5391  p3 = 85145.16067533570196555001171+xsq*p3;
5392  p3 = 33220.91340985722351859704442+xsq*p3;
5393  q3 = 1.0;
5394  q3 = 863.8367769604990967475517183+xsq*q3;
5395  q3 = 37890.22974577220264142952256+xsq*q3;
5396  q3 = 400294.4358226697511708610813+xsq*q3;
5397  q3 = 1419460.669603720892855755253+xsq*q3;
5398  q3 = 1819458.042243997298924553839+xsq*q3;
5399  q3 = 708712.8194102874357377502472+xsq*q3;
5400  *pzero = p2/q2;
5401  *qzero = 8*p3/q3/x;
5402 }
5403 
5404 
5405 
5406 
5407 /*************************************************************************
5408 Beta function
5409 
5410 
5411  - -
5412  | (a) | (b)
5413 beta( a, b ) = -----------.
5414  -
5415  | (a+b)
5416 
5417 For large arguments the logarithm of the function is
5418 evaluated using lgam(), then exponentiated.
5419 
5420 ACCURACY:
5421 
5422  Relative error:
5423 arithmetic domain # trials peak rms
5424  IEEE 0,30 30000 8.1e-14 1.1e-14
5425 
5426 Cephes Math Library Release 2.0: April, 1987
5427 Copyright 1984, 1987 by Stephen L. Moshier
5428 *************************************************************************/
5429 double beta(double a, double b, ae_state *_state)
5430 {
5431  double y;
5432  double sg;
5433  double s;
5434  double result;
5435 
5436 
5437  sg = 1;
5438  ae_assert(ae_fp_greater(a,0)||ae_fp_neq(a,ae_ifloor(a, _state)), "Overflow in Beta", _state);
5439  ae_assert(ae_fp_greater(b,0)||ae_fp_neq(b,ae_ifloor(b, _state)), "Overflow in Beta", _state);
5440  y = a+b;
5441  if( ae_fp_greater(ae_fabs(y, _state),171.624376956302725) )
5442  {
5443  y = lngamma(y, &s, _state);
5444  sg = sg*s;
5445  y = lngamma(b, &s, _state)-y;
5446  sg = sg*s;
5447  y = lngamma(a, &s, _state)+y;
5448  sg = sg*s;
5449  ae_assert(ae_fp_less_eq(y,ae_log(ae_maxrealnumber, _state)), "Overflow in Beta", _state);
5450  result = sg*ae_exp(y, _state);
5451  return result;
5452  }
5453  y = gammafunction(y, _state);
5454  ae_assert(ae_fp_neq(y,0), "Overflow in Beta", _state);
5455  if( ae_fp_greater(a,b) )
5456  {
5457  y = gammafunction(a, _state)/y;
5458  y = y*gammafunction(b, _state);
5459  }
5460  else
5461  {
5462  y = gammafunction(b, _state)/y;
5463  y = y*gammafunction(a, _state);
5464  }
5465  result = y;
5466  return result;
5467 }
5468 
5469 
5470 
5471 
5472 /*************************************************************************
5473 Incomplete beta integral
5474 
5475 Returns incomplete beta integral of the arguments, evaluated
5476 from zero to x. The function is defined as
5477 
5478  x
5479  - -
5480  | (a+b) | | a-1 b-1
5481  ----------- | t (1-t) dt.
5482  - - | |
5483  | (a) | (b) -
5484  0
5485 
5486 The domain of definition is 0 <= x <= 1. In this
5487 implementation a and b are restricted to positive values.
5488 The integral from x to 1 may be obtained by the symmetry
5489 relation
5490 
5491  1 - incbet( a, b, x ) = incbet( b, a, 1-x ).
5492 
5493 The integral is evaluated by a continued fraction expansion
5494 or, when b*x is small, by a power series.
5495 
5496 ACCURACY:
5497 
5498 Tested at uniformly distributed random points (a,b,x) with a and b
5499 in "domain" and x between 0 and 1.
5500  Relative error
5501 arithmetic domain # trials peak rms
5502  IEEE 0,5 10000 6.9e-15 4.5e-16
5503  IEEE 0,85 250000 2.2e-13 1.7e-14
5504  IEEE 0,1000 30000 5.3e-12 6.3e-13
5505  IEEE 0,10000 250000 9.3e-11 7.1e-12
5506  IEEE 0,100000 10000 8.7e-10 4.8e-11
5507 Outputs smaller than the IEEE gradual underflow threshold
5508 were excluded from these statistics.
5509 
5510 Cephes Math Library, Release 2.8: June, 2000
5511 Copyright 1984, 1995, 2000 by Stephen L. Moshier
5512 *************************************************************************/
5513 double incompletebeta(double a, double b, double x, ae_state *_state)
5514 {
5515  double t;
5516  double xc;
5517  double w;
5518  double y;
5519  ae_int_t flag;
5520  double sg;
5521  double big;
5522  double biginv;
5523  double maxgam;
5524  double minlog;
5525  double maxlog;
5526  double result;
5527 
5528 
5529  big = 4.503599627370496e15;
5530  biginv = 2.22044604925031308085e-16;
5531  maxgam = 171.624376956302725;
5532  minlog = ae_log(ae_minrealnumber, _state);
5533  maxlog = ae_log(ae_maxrealnumber, _state);
5534  ae_assert(ae_fp_greater(a,0)&&ae_fp_greater(b,0), "Domain error in IncompleteBeta", _state);
5535  ae_assert(ae_fp_greater_eq(x,0)&&ae_fp_less_eq(x,1), "Domain error in IncompleteBeta", _state);
5536  if( ae_fp_eq(x,0) )
5537  {
5538  result = 0;
5539  return result;
5540  }
5541  if( ae_fp_eq(x,1) )
5542  {
5543  result = 1;
5544  return result;
5545  }
5546  flag = 0;
5547  if( ae_fp_less_eq(b*x,1.0)&&ae_fp_less_eq(x,0.95) )
5548  {
5549  result = ibetaf_incompletebetaps(a, b, x, maxgam, _state);
5550  return result;
5551  }
5552  w = 1.0-x;
5553  if( ae_fp_greater(x,a/(a+b)) )
5554  {
5555  flag = 1;
5556  t = a;
5557  a = b;
5558  b = t;
5559  xc = x;
5560  x = w;
5561  }
5562  else
5563  {
5564  xc = w;
5565  }
5566  if( (flag==1&&ae_fp_less_eq(b*x,1.0))&&ae_fp_less_eq(x,0.95) )
5567  {
5568  t = ibetaf_incompletebetaps(a, b, x, maxgam, _state);
5570  {
5571  result = 1.0-ae_machineepsilon;
5572  }
5573  else
5574  {
5575  result = 1.0-t;
5576  }
5577  return result;
5578  }
5579  y = x*(a+b-2.0)-(a-1.0);
5580  if( ae_fp_less(y,0.0) )
5581  {
5582  w = ibetaf_incompletebetafe(a, b, x, big, biginv, _state);
5583  }
5584  else
5585  {
5586  w = ibetaf_incompletebetafe2(a, b, x, big, biginv, _state)/xc;
5587  }
5588  y = a*ae_log(x, _state);
5589  t = b*ae_log(xc, _state);
5590  if( (ae_fp_less(a+b,maxgam)&&ae_fp_less(ae_fabs(y, _state),maxlog))&&ae_fp_less(ae_fabs(t, _state),maxlog) )
5591  {
5592  t = ae_pow(xc, b, _state);
5593  t = t*ae_pow(x, a, _state);
5594  t = t/a;
5595  t = t*w;
5596  t = t*(gammafunction(a+b, _state)/(gammafunction(a, _state)*gammafunction(b, _state)));
5597  if( flag==1 )
5598  {
5600  {
5601  result = 1.0-ae_machineepsilon;
5602  }
5603  else
5604  {
5605  result = 1.0-t;
5606  }
5607  }
5608  else
5609  {
5610  result = t;
5611  }
5612  return result;
5613  }
5614  y = y+t+lngamma(a+b, &sg, _state)-lngamma(a, &sg, _state)-lngamma(b, &sg, _state);
5615  y = y+ae_log(w/a, _state);
5616  if( ae_fp_less(y,minlog) )
5617  {
5618  t = 0.0;
5619  }
5620  else
5621  {
5622  t = ae_exp(y, _state);
5623  }
5624  if( flag==1 )
5625  {
5627  {
5628  t = 1.0-ae_machineepsilon;
5629  }
5630  else
5631  {
5632  t = 1.0-t;
5633  }
5634  }
5635  result = t;
5636  return result;
5637 }
5638 
5639 
5640 /*************************************************************************
5641 Inverse of incomplete beta integral
5642 
5643 Given y, the function finds x such that
5644 
5645  incbet( a, b, x ) = y .
5646 
5647 The routine performs interval halving or Newton iterations to find the
5648 root of incbet(a,b,x) - y = 0.
5649 
5650 
5651 ACCURACY:
5652 
5653  Relative error:
5654  x a,b
5655 arithmetic domain domain # trials peak rms
5656  IEEE 0,1 .5,10000 50000 5.8e-12 1.3e-13
5657  IEEE 0,1 .25,100 100000 1.8e-13 3.9e-15
5658  IEEE 0,1 0,5 50000 1.1e-12 5.5e-15
5659 With a and b constrained to half-integer or integer values:
5660  IEEE 0,1 .5,10000 50000 5.8e-12 1.1e-13
5661  IEEE 0,1 .5,100 100000 1.7e-14 7.9e-16
5662 With a = .5, b constrained to half-integer or integer values:
5663  IEEE 0,1 .5,10000 10000 8.3e-11 1.0e-11
5664 
5665 Cephes Math Library Release 2.8: June, 2000
5666 Copyright 1984, 1996, 2000 by Stephen L. Moshier
5667 *************************************************************************/
5668 double invincompletebeta(double a, double b, double y, ae_state *_state)
5669 {
5670  double aaa;
5671  double bbb;
5672  double y0;
5673  double d;
5674  double yyy;
5675  double x;
5676  double x0;
5677  double x1;
5678  double lgm;
5679  double yp;
5680  double di;
5681  double dithresh;
5682  double yl;
5683  double yh;
5684  double xt;
5685  ae_int_t i;
5686  ae_int_t rflg;
5687  ae_int_t dir;
5688  ae_int_t nflg;
5689  double s;
5690  ae_int_t mainlooppos;
5691  ae_int_t ihalve;
5692  ae_int_t ihalvecycle;
5693  ae_int_t newt;
5694  ae_int_t newtcycle;
5695  ae_int_t breaknewtcycle;
5696  ae_int_t breakihalvecycle;
5697  double result;
5698 
5699 
5700  i = 0;
5701  ae_assert(ae_fp_greater_eq(y,0)&&ae_fp_less_eq(y,1), "Domain error in InvIncompleteBeta", _state);
5702 
5703  /*
5704  * special cases
5705  */
5706  if( ae_fp_eq(y,0) )
5707  {
5708  result = 0;
5709  return result;
5710  }
5711  if( ae_fp_eq(y,1.0) )
5712  {
5713  result = 1;
5714  return result;
5715  }
5716 
5717  /*
5718  * these initializations are not really necessary,
5719  * but without them compiler complains about 'possibly uninitialized variables'.
5720  */
5721  dithresh = 0;
5722  rflg = 0;
5723  aaa = 0;
5724  bbb = 0;
5725  y0 = 0;
5726  x = 0;
5727  yyy = 0;
5728  lgm = 0;
5729  dir = 0;
5730  di = 0;
5731 
5732  /*
5733  * normal initializations
5734  */
5735  x0 = 0.0;
5736  yl = 0.0;
5737  x1 = 1.0;
5738  yh = 1.0;
5739  nflg = 0;
5740  mainlooppos = 0;
5741  ihalve = 1;
5742  ihalvecycle = 2;
5743  newt = 3;
5744  newtcycle = 4;
5745  breaknewtcycle = 5;
5746  breakihalvecycle = 6;
5747 
5748  /*
5749  * main loop
5750  */
5751  for(;;)
5752  {
5753 
5754  /*
5755  * start
5756  */
5757  if( mainlooppos==0 )
5758  {
5759  if( ae_fp_less_eq(a,1.0)||ae_fp_less_eq(b,1.0) )
5760  {
5761  dithresh = 1.0e-6;
5762  rflg = 0;
5763  aaa = a;
5764  bbb = b;
5765  y0 = y;
5766  x = aaa/(aaa+bbb);
5767  yyy = incompletebeta(aaa, bbb, x, _state);
5768  mainlooppos = ihalve;
5769  continue;
5770  }
5771  else
5772  {
5773  dithresh = 1.0e-4;
5774  }
5775  yp = -invnormaldistribution(y, _state);
5776  if( ae_fp_greater(y,0.5) )
5777  {
5778  rflg = 1;
5779  aaa = b;
5780  bbb = a;
5781  y0 = 1.0-y;
5782  yp = -yp;
5783  }
5784  else
5785  {
5786  rflg = 0;
5787  aaa = a;
5788  bbb = b;
5789  y0 = y;
5790  }
5791  lgm = (yp*yp-3.0)/6.0;
5792  x = 2.0/(1.0/(2.0*aaa-1.0)+1.0/(2.0*bbb-1.0));
5793  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));
5794  d = 2.0*d;
5795  if( ae_fp_less(d,ae_log(ae_minrealnumber, _state)) )
5796  {
5797  x = 0;
5798  break;
5799  }
5800  x = aaa/(aaa+bbb*ae_exp(d, _state));
5801  yyy = incompletebeta(aaa, bbb, x, _state);
5802  yp = (yyy-y0)/y0;
5803  if( ae_fp_less(ae_fabs(yp, _state),0.2) )
5804  {
5805  mainlooppos = newt;
5806  continue;
5807  }
5808  mainlooppos = ihalve;
5809  continue;
5810  }
5811 
5812  /*
5813  * ihalve
5814  */
5815  if( mainlooppos==ihalve )
5816  {
5817  dir = 0;
5818  di = 0.5;
5819  i = 0;
5820  mainlooppos = ihalvecycle;
5821  continue;
5822  }
5823 
5824  /*
5825  * ihalvecycle
5826  */
5827  if( mainlooppos==ihalvecycle )
5828  {
5829  if( i<=99 )
5830  {
5831  if( i!=0 )
5832  {
5833  x = x0+di*(x1-x0);
5834  if( ae_fp_eq(x,1.0) )
5835  {
5836  x = 1.0-ae_machineepsilon;
5837  }
5838  if( ae_fp_eq(x,0.0) )
5839  {
5840  di = 0.5;
5841  x = x0+di*(x1-x0);
5842  if( ae_fp_eq(x,0.0) )
5843  {
5844  break;
5845  }
5846  }
5847  yyy = incompletebeta(aaa, bbb, x, _state);
5848  yp = (x1-x0)/(x1+x0);
5849  if( ae_fp_less(ae_fabs(yp, _state),dithresh) )
5850  {
5851  mainlooppos = newt;
5852  continue;
5853  }
5854  yp = (yyy-y0)/y0;
5855  if( ae_fp_less(ae_fabs(yp, _state),dithresh) )
5856  {
5857  mainlooppos = newt;
5858  continue;
5859  }
5860  }
5861  if( ae_fp_less(yyy,y0) )
5862  {
5863  x0 = x;
5864  yl = yyy;
5865  if( dir<0 )
5866  {
5867  dir = 0;
5868  di = 0.5;
5869  }
5870  else
5871  {
5872  if( dir>3 )
5873  {
5874  di = 1.0-(1.0-di)*(1.0-di);
5875  }
5876  else
5877  {
5878  if( dir>1 )
5879  {
5880  di = 0.5*di+0.5;
5881  }
5882  else
5883  {
5884  di = (y0-yyy)/(yh-yl);
5885  }
5886  }
5887  }
5888  dir = dir+1;
5889  if( ae_fp_greater(x0,0.75) )
5890  {
5891  if( rflg==1 )
5892  {
5893  rflg = 0;
5894  aaa = a;
5895  bbb = b;
5896  y0 = y;
5897  }
5898  else
5899  {
5900  rflg = 1;
5901  aaa = b;
5902  bbb = a;
5903  y0 = 1.0-y;
5904  }
5905  x = 1.0-x;
5906  yyy = incompletebeta(aaa, bbb, x, _state);
5907  x0 = 0.0;
5908  yl = 0.0;
5909  x1 = 1.0;
5910  yh = 1.0;
5911  mainlooppos = ihalve;
5912  continue;
5913  }
5914  }
5915  else
5916  {
5917  x1 = x;
5918  if( rflg==1&&ae_fp_less(x1,ae_machineepsilon) )
5919  {
5920  x = 0.0;
5921  break;
5922  }
5923  yh = yyy;
5924  if( dir>0 )
5925  {
5926  dir = 0;
5927  di = 0.5;
5928  }
5929  else
5930  {
5931  if( dir<-3 )
5932  {
5933  di = di*di;
5934  }
5935  else
5936  {
5937  if( dir<-1 )
5938  {
5939  di = 0.5*di;
5940  }
5941  else
5942  {
5943  di = (yyy-y0)/(yh-yl);
5944  }
5945  }
5946  }
5947  dir = dir-1;
5948  }
5949  i = i+1;
5950  mainlooppos = ihalvecycle;
5951  continue;
5952  }
5953  else
5954  {
5955  mainlooppos = breakihalvecycle;
5956  continue;
5957  }
5958  }
5959 
5960  /*
5961  * breakihalvecycle
5962  */
5963  if( mainlooppos==breakihalvecycle )
5964  {
5965  if( ae_fp_greater_eq(x0,1.0) )
5966  {
5967  x = 1.0-ae_machineepsilon;
5968  break;
5969  }
5970  if( ae_fp_less_eq(x,0.0) )
5971  {
5972  x = 0.0;
5973  break;
5974  }
5975  mainlooppos = newt;
5976  continue;
5977  }
5978 
5979  /*
5980  * newt
5981  */
5982  if( mainlooppos==newt )
5983  {
5984  if( nflg!=0 )
5985  {
5986  break;
5987  }
5988  nflg = 1;
5989  lgm = lngamma(aaa+bbb, &s, _state)-lngamma(aaa, &s, _state)-lngamma(bbb, &s, _state);
5990  i = 0;
5991  mainlooppos = newtcycle;
5992  continue;
5993  }
5994 
5995  /*
5996  * newtcycle
5997  */
5998  if( mainlooppos==newtcycle )
5999  {
6000  if( i<=7 )
6001  {
6002  if( i!=0 )
6003  {
6004  yyy = incompletebeta(aaa, bbb, x, _state);
6005  }
6006  if( ae_fp_less(yyy,yl) )
6007  {
6008  x = x0;
6009  yyy = yl;
6010  }
6011  else
6012  {
6013  if( ae_fp_greater(yyy,yh) )
6014  {
6015  x = x1;
6016  yyy = yh;
6017  }
6018  else
6019  {
6020  if( ae_fp_less(yyy,y0) )
6021  {
6022  x0 = x;
6023  yl = yyy;
6024  }
6025  else
6026  {
6027  x1 = x;
6028  yh = yyy;
6029  }
6030  }
6031  }
6032  if( ae_fp_eq(x,1.0)||ae_fp_eq(x,0.0) )
6033  {
6034  mainlooppos = breaknewtcycle;
6035  continue;
6036  }
6037  d = (aaa-1.0)*ae_log(x, _state)+(bbb-1.0)*ae_log(1.0-x, _state)+lgm;
6038  if( ae_fp_less(d,ae_log(ae_minrealnumber, _state)) )
6039  {
6040  break;
6041  }
6042  if( ae_fp_greater(d,ae_log(ae_maxrealnumber, _state)) )
6043  {
6044  mainlooppos = breaknewtcycle;
6045  continue;
6046  }
6047  d = ae_exp(d, _state);
6048  d = (yyy-y0)/d;
6049  xt = x-d;
6050  if( ae_fp_less_eq(xt,x0) )
6051  {
6052  yyy = (x-x0)/(x1-x0);
6053  xt = x0+0.5*yyy*(x-x0);
6054  if( ae_fp_less_eq(xt,0.0) )
6055  {
6056  mainlooppos = breaknewtcycle;
6057  continue;
6058  }
6059  }
6060  if( ae_fp_greater_eq(xt,x1) )
6061  {
6062  yyy = (x1-x)/(x1-x0);
6063  xt = x1-0.5*yyy*(x1-x);
6064  if( ae_fp_greater_eq(xt,1.0) )
6065  {
6066  mainlooppos = breaknewtcycle;
6067  continue;
6068  }
6069  }
6070  x = xt;
6071  if( ae_fp_less(ae_fabs(d/x, _state),128.0*ae_machineepsilon) )
6072  {
6073  break;
6074  }
6075  i = i+1;
6076  mainlooppos = newtcycle;
6077  continue;
6078  }
6079  else
6080  {
6081  mainlooppos = breaknewtcycle;
6082  continue;
6083  }
6084  }
6085 
6086  /*
6087  * breaknewtcycle
6088  */
6089  if( mainlooppos==breaknewtcycle )
6090  {
6091  dithresh = 256.0*ae_machineepsilon;
6092  mainlooppos = ihalve;
6093  continue;
6094  }
6095  }
6096 
6097  /*
6098  * done
6099  */
6100  if( rflg!=0 )
6101  {
6103  {
6104  x = 1.0-ae_machineepsilon;
6105  }
6106  else
6107  {
6108  x = 1.0-x;
6109  }
6110  }
6111  result = x;
6112  return result;
6113 }
6114 
6115 
6116 /*************************************************************************
6117 Continued fraction expansion #1 for incomplete beta integral
6118 
6119 Cephes Math Library, Release 2.8: June, 2000
6120 Copyright 1984, 1995, 2000 by Stephen L. Moshier
6121 *************************************************************************/
6122 static double ibetaf_incompletebetafe(double a,
6123  double b,
6124  double x,
6125  double big,
6126  double biginv,
6127  ae_state *_state)
6128 {
6129  double xk;
6130  double pk;
6131  double pkm1;
6132  double pkm2;
6133  double qk;
6134  double qkm1;
6135  double qkm2;
6136  double k1;
6137  double k2;
6138  double k3;
6139  double k4;
6140  double k5;
6141  double k6;
6142  double k7;
6143  double k8;
6144  double r;
6145  double t;
6146  double ans;
6147  double thresh;
6148  ae_int_t n;
6149  double result;
6150 
6151 
6152  k1 = a;
6153  k2 = a+b;
6154  k3 = a;
6155  k4 = a+1.0;
6156  k5 = 1.0;
6157  k6 = b-1.0;
6158  k7 = k4;
6159  k8 = a+2.0;
6160  pkm2 = 0.0;
6161  qkm2 = 1.0;
6162  pkm1 = 1.0;
6163  qkm1 = 1.0;
6164  ans = 1.0;
6165  r = 1.0;
6166  n = 0;
6167  thresh = 3.0*ae_machineepsilon;
6168  do
6169  {
6170  xk = -x*k1*k2/(k3*k4);
6171  pk = pkm1+pkm2*xk;
6172  qk = qkm1+qkm2*xk;
6173  pkm2 = pkm1;
6174  pkm1 = pk;
6175  qkm2 = qkm1;
6176  qkm1 = qk;
6177  xk = x*k5*k6/(k7*k8);
6178  pk = pkm1+pkm2*xk;
6179  qk = qkm1+qkm2*xk;
6180  pkm2 = pkm1;
6181  pkm1 = pk;
6182  qkm2 = qkm1;
6183  qkm1 = qk;
6184  if( ae_fp_neq(qk,0) )
6185  {
6186  r = pk/qk;
6187  }
6188  if( ae_fp_neq(r,0) )
6189  {
6190  t = ae_fabs((ans-r)/r, _state);
6191  ans = r;
6192  }
6193  else
6194  {
6195  t = 1.0;
6196  }
6197  if( ae_fp_less(t,thresh) )
6198  {
6199  break;
6200  }
6201  k1 = k1+1.0;
6202  k2 = k2+1.0;
6203  k3 = k3+2.0;
6204  k4 = k4+2.0;
6205  k5 = k5+1.0;
6206  k6 = k6-1.0;
6207  k7 = k7+2.0;
6208  k8 = k8+2.0;
6209  if( ae_fp_greater(ae_fabs(qk, _state)+ae_fabs(pk, _state),big) )
6210  {
6211  pkm2 = pkm2*biginv;
6212  pkm1 = pkm1*biginv;
6213  qkm2 = qkm2*biginv;
6214  qkm1 = qkm1*biginv;
6215  }
6216  if( ae_fp_less(ae_fabs(qk, _state),biginv)||ae_fp_less(ae_fabs(pk, _state),biginv) )
6217  {
6218  pkm2 = pkm2*big;
6219  pkm1 = pkm1*big;
6220  qkm2 = qkm2*big;
6221  qkm1 = qkm1*big;
6222  }
6223  n = n+1;
6224  }
6225  while(n!=300);
6226  result = ans;
6227  return result;
6228 }
6229 
6230 
6231 /*************************************************************************
6232 Continued fraction expansion #2
6233 for incomplete beta integral
6234 
6235 Cephes Math Library, Release 2.8: June, 2000
6236 Copyright 1984, 1995, 2000 by Stephen L. Moshier
6237 *************************************************************************/
6238 static double ibetaf_incompletebetafe2(double a,
6239  double b,
6240  double x,
6241  double big,
6242  double biginv,
6243  ae_state *_state)
6244 {
6245  double xk;
6246  double pk;
6247  double pkm1;
6248  double pkm2;
6249  double qk;
6250  double qkm1;
6251  double qkm2;
6252  double k1;
6253  double k2;
6254  double k3;
6255  double k4;
6256  double k5;
6257  double k6;
6258  double k7;
6259  double k8;
6260  double r;
6261  double t;
6262  double ans;
6263  double z;
6264  double thresh;
6265  ae_int_t n;
6266  double result;
6267 
6268 
6269  k1 = a;
6270  k2 = b-1.0;
6271  k3 = a;
6272  k4 = a+1.0;
6273  k5 = 1.0;
6274  k6 = a+b;
6275  k7 = a+1.0;
6276  k8 = a+2.0;
6277  pkm2 = 0.0;
6278  qkm2 = 1.0;
6279  pkm1 = 1.0;
6280  qkm1 = 1.0;
6281  z = x/(1.0-x);
6282  ans = 1.0;
6283  r = 1.0;
6284  n = 0;
6285  thresh = 3.0*ae_machineepsilon;
6286  do
6287  {
6288  xk = -z*k1*k2/(k3*k4);
6289  pk = pkm1+pkm2*xk;
6290  qk = qkm1+qkm2*xk;
6291  pkm2 = pkm1;
6292  pkm1 = pk;
6293  qkm2 = qkm1;
6294  qkm1 = qk;
6295  xk = z*k5*k6/(k7*k8);
6296  pk = pkm1+pkm2*xk;
6297  qk = qkm1+qkm2*xk;
6298  pkm2 = pkm1;
6299  pkm1 = pk;
6300  qkm2 = qkm1;
6301  qkm1 = qk;
6302  if( ae_fp_neq(qk,0) )
6303  {
6304  r = pk/qk;
6305  }
6306  if( ae_fp_neq(r,0) )
6307  {
6308  t = ae_fabs((ans-r)/r, _state);
6309  ans = r;
6310  }
6311  else
6312  {
6313  t = 1.0;
6314  }
6315  if( ae_fp_less(t,thresh) )
6316  {
6317  break;
6318  }
6319  k1 = k1+1.0;
6320  k2 = k2-1.0;
6321  k3 = k3+2.0;
6322  k4 = k4+2.0;
6323  k5 = k5+1.0;
6324  k6 = k6+1.0;
6325  k7 = k7+2.0;
6326  k8 = k8+2.0;
6327  if( ae_fp_greater(ae_fabs(qk, _state)+ae_fabs(pk, _state),big) )
6328  {
6329  pkm2 = pkm2*biginv;
6330  pkm1 = pkm1*biginv;
6331  qkm2 = qkm2*biginv;
6332  qkm1 = qkm1*biginv;
6333  }
6334  if( ae_fp_less(ae_fabs(qk, _state),biginv)||ae_fp_less(ae_fabs(pk, _state),biginv) )
6335  {
6336  pkm2 = pkm2*big;
6337  pkm1 = pkm1*big;
6338  qkm2 = qkm2*big;
6339  qkm1 = qkm1*big;
6340  }
6341  n = n+1;
6342  }
6343  while(n!=300);
6344  result = ans;
6345  return result;
6346 }
6347 
6348 
6349 /*************************************************************************
6350 Power series for incomplete beta integral.
6351 Use when b*x is small and x not too close to 1.
6352 
6353 Cephes Math Library, Release 2.8: June, 2000
6354 Copyright 1984, 1995, 2000 by Stephen L. Moshier
6355 *************************************************************************/
6356 static double ibetaf_incompletebetaps(double a,
6357  double b,
6358  double x,
6359  double maxgam,
6360  ae_state *_state)
6361 {
6362  double s;
6363  double t;
6364  double u;
6365  double v;
6366  double n;
6367  double t1;
6368  double z;
6369  double ai;
6370  double sg;
6371  double result;
6372 
6373 
6374  ai = 1.0/a;
6375  u = (1.0-b)*x;
6376  v = u/(a+1.0);
6377  t1 = v;
6378  t = u;
6379  n = 2.0;
6380  s = 0.0;
6381  z = ae_machineepsilon*ai;
6382  while(ae_fp_greater(ae_fabs(v, _state),z))
6383  {
6384  u = (n-b)*x/n;
6385  t = t*u;
6386  v = t/(a+n);
6387  s = s+v;
6388  n = n+1.0;
6389  }
6390  s = s+t1;
6391  s = s+ai;
6392  u = a*ae_log(x, _state);
6393  if( ae_fp_less(a+b,maxgam)&&ae_fp_less(ae_fabs(u, _state),ae_log(ae_maxrealnumber, _state)) )
6394  {
6395  t = gammafunction(a+b, _state)/(gammafunction(a, _state)*gammafunction(b, _state));
6396  s = s*t*ae_pow(x, a, _state);
6397  }
6398  else
6399  {
6400  t = lngamma(a+b, &sg, _state)-lngamma(a, &sg, _state)-lngamma(b, &sg, _state)+u+ae_log(s, _state);
6401  if( ae_fp_less(t,ae_log(ae_minrealnumber, _state)) )
6402  {
6403  s = 0.0;
6404  }
6405  else
6406  {
6407  s = ae_exp(t, _state);
6408  }
6409  }
6410  result = s;
6411  return result;
6412 }
6413 
6414 
6415 
6416 
6417 /*************************************************************************
6418 Binomial distribution
6419 
6420 Returns the sum of the terms 0 through k of the Binomial
6421 probability density:
6422 
6423  k
6424  -- ( n ) j n-j
6425  > ( ) p (1-p)
6426  -- ( j )
6427  j=0
6428 
6429 The terms are not summed directly; instead the incomplete
6430 beta integral is employed, according to the formula
6431 
6432 y = bdtr( k, n, p ) = incbet( n-k, k+1, 1-p ).
6433 
6434 The arguments must be positive, with p ranging from 0 to 1.
6435 
6436 ACCURACY:
6437 
6438 Tested at random points (a,b,p), with p between 0 and 1.
6439 
6440  a,b Relative error:
6441 arithmetic domain # trials peak rms
6442  For p between 0.001 and 1:
6443  IEEE 0,100 100000 4.3e-15 2.6e-16
6444 
6445 Cephes Math Library Release 2.8: June, 2000
6446 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
6447 *************************************************************************/
6449  ae_int_t n,
6450  double p,
6451  ae_state *_state)
6452 {
6453  double dk;
6454  double dn;
6455  double result;
6456 
6457 
6458  ae_assert(ae_fp_greater_eq(p,0)&&ae_fp_less_eq(p,1), "Domain error in BinomialDistribution", _state);
6459  ae_assert(k>=-1&&k<=n, "Domain error in BinomialDistribution", _state);
6460  if( k==-1 )
6461  {
6462  result = 0;
6463  return result;
6464  }
6465  if( k==n )
6466  {
6467  result = 1;
6468  return result;
6469  }
6470  dn = n-k;
6471  if( k==0 )
6472  {
6473  dk = ae_pow(1.0-p, dn, _state);
6474  }
6475  else
6476  {
6477  dk = k+1;
6478  dk = incompletebeta(dn, dk, 1.0-p, _state);
6479  }
6480  result = dk;
6481  return result;
6482 }
6483 
6484 
6485 /*************************************************************************
6486 Complemented binomial distribution
6487 
6488 Returns the sum of the terms k+1 through n of the Binomial
6489 probability density:
6490 
6491  n
6492  -- ( n ) j n-j
6493  > ( ) p (1-p)
6494  -- ( j )
6495  j=k+1
6496 
6497 The terms are not summed directly; instead the incomplete
6498 beta integral is employed, according to the formula
6499 
6500 y = bdtrc( k, n, p ) = incbet( k+1, n-k, p ).
6501 
6502 The arguments must be positive, with p ranging from 0 to 1.
6503 
6504 ACCURACY:
6505 
6506 Tested at random points (a,b,p).
6507 
6508  a,b Relative error:
6509 arithmetic domain # trials peak rms
6510  For p between 0.001 and 1:
6511  IEEE 0,100 100000 6.7e-15 8.2e-16
6512  For p between 0 and .001:
6513  IEEE 0,100 100000 1.5e-13 2.7e-15
6514 
6515 Cephes Math Library Release 2.8: June, 2000
6516 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
6517 *************************************************************************/
6519  ae_int_t n,
6520  double p,
6521  ae_state *_state)
6522 {
6523  double dk;
6524  double dn;
6525  double result;
6526 
6527 
6528  ae_assert(ae_fp_greater_eq(p,0)&&ae_fp_less_eq(p,1), "Domain error in BinomialDistributionC", _state);
6529  ae_assert(k>=-1&&k<=n, "Domain error in BinomialDistributionC", _state);
6530  if( k==-1 )
6531  {
6532  result = 1;
6533  return result;
6534  }
6535  if( k==n )
6536  {
6537  result = 0;
6538  return result;
6539  }
6540  dn = n-k;
6541  if( k==0 )
6542  {
6543  if( ae_fp_less(p,0.01) )
6544  {
6545  dk = -nuexpm1(dn*nulog1p(-p, _state), _state);
6546  }
6547  else
6548  {
6549  dk = 1.0-ae_pow(1.0-p, dn, _state);
6550  }
6551  }
6552  else
6553  {
6554  dk = k+1;
6555  dk = incompletebeta(dk, dn, p, _state);
6556  }
6557  result = dk;
6558  return result;
6559 }
6560 
6561 
6562 /*************************************************************************
6563 Inverse binomial distribution
6564 
6565 Finds the event probability p such that the sum of the
6566 terms 0 through k of the Binomial probability density
6567 is equal to the given cumulative probability y.
6568 
6569 This is accomplished using the inverse beta integral
6570 function and the relation
6571 
6572 1 - p = incbi( n-k, k+1, y ).
6573 
6574 ACCURACY:
6575 
6576 Tested at random points (a,b,p).
6577 
6578  a,b Relative error:
6579 arithmetic domain # trials peak rms
6580  For p between 0.001 and 1:
6581  IEEE 0,100 100000 2.3e-14 6.4e-16
6582  IEEE 0,10000 100000 6.6e-12 1.2e-13
6583  For p between 10^-6 and 0.001:
6584  IEEE 0,100 100000 2.0e-12 1.3e-14
6585  IEEE 0,10000 100000 1.5e-12 3.2e-14
6586 
6587 Cephes Math Library Release 2.8: June, 2000
6588 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
6589 *************************************************************************/
6591  ae_int_t n,
6592  double y,
6593  ae_state *_state)
6594 {
6595  double dk;
6596  double dn;
6597  double p;
6598  double result;
6599 
6600 
6601  ae_assert(k>=0&&k<n, "Domain error in InvBinomialDistribution", _state);
6602  dn = n-k;
6603  if( k==0 )
6604  {
6605  if( ae_fp_greater(y,0.8) )
6606  {
6607  p = -nuexpm1(nulog1p(y-1.0, _state)/dn, _state);
6608  }
6609  else
6610  {
6611  p = 1.0-ae_pow(y, 1.0/dn, _state);
6612  }
6613  }
6614  else
6615  {
6616  dk = k+1;
6617  p = incompletebeta(dn, dk, 0.5, _state);
6618  if( ae_fp_greater(p,0.5) )
6619  {
6620  p = invincompletebeta(dk, dn, 1.0-y, _state);
6621  }
6622  else
6623  {
6624  p = 1.0-invincompletebeta(dn, dk, y, _state);
6625  }
6626  }
6627  result = p;
6628  return result;
6629 }
6630 
6631 
6632 
6633 
6634 /*************************************************************************
6635 Calculation of the value of the Chebyshev polynomials of the
6636 first and second kinds.
6637 
6638 Parameters:
6639  r - polynomial kind, either 1 or 2.
6640  n - degree, n>=0
6641  x - argument, -1 <= x <= 1
6642 
6643 Result:
6644  the value of the Chebyshev polynomial at x
6645 *************************************************************************/
6647  ae_int_t n,
6648  double x,
6649  ae_state *_state)
6650 {
6651  ae_int_t i;
6652  double a;
6653  double b;
6654  double result;
6655 
6656 
6657  result = 0;
6658 
6659  /*
6660  * Prepare A and B
6661  */
6662  if( r==1 )
6663  {
6664  a = 1;
6665  b = x;
6666  }
6667  else
6668  {
6669  a = 1;
6670  b = 2*x;
6671  }
6672 
6673  /*
6674  * Special cases: N=0 or N=1
6675  */
6676  if( n==0 )
6677  {
6678  result = a;
6679  return result;
6680  }
6681  if( n==1 )
6682  {
6683  result = b;
6684  return result;
6685  }
6686 
6687  /*
6688  * General case: N>=2
6689  */
6690  for(i=2; i<=n; i++)
6691  {
6692  result = 2*x*b-a;
6693  a = b;
6694  b = result;
6695  }
6696  return result;
6697 }
6698 
6699 
6700 /*************************************************************************
6701 Summation of Chebyshev polynomials using Clenshaw’s recurrence formula.
6702 
6703 This routine calculates
6704  c[0]*T0(x) + c[1]*T1(x) + ... + c[N]*TN(x)
6705 or
6706  c[0]*U0(x) + c[1]*U1(x) + ... + c[N]*UN(x)
6707 depending on the R.
6708 
6709 Parameters:
6710  r - polynomial kind, either 1 or 2.
6711  n - degree, n>=0
6712  x - argument
6713 
6714 Result:
6715  the value of the Chebyshev polynomial at x
6716 *************************************************************************/
6717 double chebyshevsum(/* Real */ ae_vector* c,
6718  ae_int_t r,
6719  ae_int_t n,
6720  double x,
6721  ae_state *_state)
6722 {
6723  double b1;
6724  double b2;
6725  ae_int_t i;
6726  double result;
6727 
6728 
6729  b1 = 0;
6730  b2 = 0;
6731  for(i=n; i>=1; i--)
6732  {
6733  result = 2*x*b1-b2+c->ptr.p_double[i];
6734  b2 = b1;
6735  b1 = result;
6736  }
6737  if( r==1 )
6738  {
6739  result = -b2+x*b1+c->ptr.p_double[0];
6740  }
6741  else
6742  {
6743  result = -b2+2*x*b1+c->ptr.p_double[0];
6744  }
6745  return result;
6746 }
6747 
6748 
6749 /*************************************************************************
6750 Representation of Tn as C[0] + C[1]*X + ... + C[N]*X^N
6751 
6752 Input parameters:
6753  N - polynomial degree, n>=0
6754 
6755 Output parameters:
6756  C - coefficients
6757 *************************************************************************/
6759  /* Real */ ae_vector* c,
6760  ae_state *_state)
6761 {
6762  ae_int_t i;
6763 
6764  ae_vector_clear(c);
6765 
6766  ae_vector_set_length(c, n+1, _state);
6767  for(i=0; i<=n; i++)
6768  {
6769  c->ptr.p_double[i] = 0;
6770  }
6771  if( n==0||n==1 )
6772  {
6773  c->ptr.p_double[n] = 1;
6774  }
6775  else
6776  {
6777  c->ptr.p_double[n] = ae_exp((n-1)*ae_log(2, _state), _state);
6778  for(i=0; i<=n/2-1; i++)
6779  {
6780  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);
6781  }
6782  }
6783 }
6784 
6785 
6786 /*************************************************************************
6787 Conversion of a series of Chebyshev polynomials to a power series.
6788 
6789 Represents A[0]*T0(x) + A[1]*T1(x) + ... + A[N]*Tn(x) as
6790 B[0] + B[1]*X + ... + B[N]*X^N.
6791 
6792 Input parameters:
6793  A - Chebyshev series coefficients
6794  N - degree, N>=0
6795 
6796 Output parameters
6797  B - power series coefficients
6798 *************************************************************************/
6799 void fromchebyshev(/* Real */ ae_vector* a,
6800  ae_int_t n,
6801  /* Real */ ae_vector* b,
6802  ae_state *_state)
6803 {
6804  ae_int_t i;
6805  ae_int_t k;
6806  double e;
6807  double d;
6808 
6809  ae_vector_clear(b);
6810 
6811  ae_vector_set_length(b, n+1, _state);
6812  for(i=0; i<=n; i++)
6813  {
6814  b->ptr.p_double[i] = 0;
6815  }
6816  d = 0;
6817  i = 0;
6818  do
6819  {
6820  k = i;
6821  do
6822  {
6823  e = b->ptr.p_double[k];
6824  b->ptr.p_double[k] = 0;
6825  if( i<=1&&k==i )
6826  {
6827  b->ptr.p_double[k] = 1;
6828  }
6829  else
6830  {
6831  if( i!=0 )
6832  {
6833  b->ptr.p_double[k] = 2*d;
6834  }
6835  if( k>i+1 )
6836  {
6837  b->ptr.p_double[k] = b->ptr.p_double[k]-b->ptr.p_double[k-2];
6838  }
6839  }
6840  d = e;
6841  k = k+1;
6842  }
6843  while(k<=n);
6844  d = b->ptr.p_double[i];
6845  e = 0;
6846  k = i;
6847  while(k<=n)
6848  {
6849  e = e+b->ptr.p_double[k]*a->ptr.p_double[k];
6850  k = k+2;
6851  }
6852  b->ptr.p_double[i] = e;
6853  i = i+1;
6854  }
6855  while(i<=n);
6856 }
6857 
6858 
6859 
6860 
6861 /*************************************************************************
6862 Chi-square distribution
6863 
6864 Returns the area under the left hand tail (from 0 to x)
6865 of the Chi square probability density function with
6866 v degrees of freedom.
6867 
6868 
6869  x
6870  -
6871  1 | | v/2-1 -t/2
6872  P( x | v ) = ----------- | t e dt
6873  v/2 - | |
6874  2 | (v/2) -
6875  0
6876 
6877 where x is the Chi-square variable.
6878 
6879 The incomplete gamma integral is used, according to the
6880 formula
6881 
6882 y = chdtr( v, x ) = igam( v/2.0, x/2.0 ).
6883 
6884 The arguments must both be positive.
6885 
6886 ACCURACY:
6887 
6888 See incomplete gamma function
6889 
6890 
6891 Cephes Math Library Release 2.8: June, 2000
6892 Copyright 1984, 1987, 2000 by Stephen L. Moshier
6893 *************************************************************************/
6894 double chisquaredistribution(double v, double x, ae_state *_state)
6895 {
6896  double result;
6897 
6898 
6899  ae_assert(ae_fp_greater_eq(x,0)&&ae_fp_greater_eq(v,1), "Domain error in ChiSquareDistribution", _state);
6900  result = incompletegamma(v/2.0, x/2.0, _state);
6901  return result;
6902 }
6903 
6904 
6905 /*************************************************************************
6906 Complemented Chi-square distribution
6907 
6908 Returns the area under the right hand tail (from x to
6909 infinity) of the Chi square probability density function
6910 with v degrees of freedom:
6911 
6912  inf.
6913  -
6914  1 | | v/2-1 -t/2
6915  P( x | v ) = ----------- | t e dt
6916  v/2 - | |
6917  2 | (v/2) -
6918  x
6919 
6920 where x is the Chi-square variable.
6921 
6922 The incomplete gamma integral is used, according to the
6923 formula
6924 
6925 y = chdtr( v, x ) = igamc( v/2.0, x/2.0 ).
6926 
6927 The arguments must both be positive.
6928 
6929 ACCURACY:
6930 
6931 See incomplete gamma function
6932 
6933 Cephes Math Library Release 2.8: June, 2000
6934 Copyright 1984, 1987, 2000 by Stephen L. Moshier
6935 *************************************************************************/
6936 double chisquarecdistribution(double v, double x, ae_state *_state)
6937 {
6938  double result;
6939 
6940 
6941  ae_assert(ae_fp_greater_eq(x,0)&&ae_fp_greater_eq(v,1), "Domain error in ChiSquareDistributionC", _state);
6942  result = incompletegammac(v/2.0, x/2.0, _state);
6943  return result;
6944 }
6945 
6946 
6947 /*************************************************************************
6948 Inverse of complemented Chi-square distribution
6949 
6950 Finds the Chi-square argument x such that the integral
6951 from x to infinity of the Chi-square density is equal
6952 to the given cumulative probability y.
6953 
6954 This is accomplished using the inverse gamma integral
6955 function and the relation
6956 
6957  x/2 = igami( df/2, y );
6958 
6959 ACCURACY:
6960 
6961 See inverse incomplete gamma function
6962 
6963 
6964 Cephes Math Library Release 2.8: June, 2000
6965 Copyright 1984, 1987, 2000 by Stephen L. Moshier
6966 *************************************************************************/
6967 double invchisquaredistribution(double v, double y, ae_state *_state)
6968 {
6969  double result;
6970 
6971 
6972  ae_assert((ae_fp_greater_eq(y,0)&&ae_fp_less_eq(y,1))&&ae_fp_greater_eq(v,1), "Domain error in InvChiSquareDistribution", _state);
6973  result = 2*invincompletegammac(0.5*v, y, _state);
6974  return result;
6975 }
6976 
6977 
6978 
6979 
6980 /*************************************************************************
6981 Dawson's Integral
6982 
6983 Approximates the integral
6984 
6985  x
6986  -
6987  2 | | 2
6988  dawsn(x) = exp( -x ) | exp( t ) dt
6989  | |
6990  -
6991  0
6992 
6993 Three different rational approximations are employed, for
6994 the intervals 0 to 3.25; 3.25 to 6.25; and 6.25 up.
6995 
6996 ACCURACY:
6997 
6998  Relative error:
6999 arithmetic domain # trials peak rms
7000  IEEE 0,10 10000 6.9e-16 1.0e-16
7001 
7002 Cephes Math Library Release 2.8: June, 2000
7003 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
7004 *************************************************************************/
7005 double dawsonintegral(double x, ae_state *_state)
7006 {
7007  double x2;
7008  double y;
7009  ae_int_t sg;
7010  double an;
7011  double ad;
7012  double bn;
7013  double bd;
7014  double cn;
7015  double cd;
7016  double result;
7017 
7018 
7019  sg = 1;
7020  if( ae_fp_less(x,0) )
7021  {
7022  sg = -1;
7023  x = -x;
7024  }
7025  if( ae_fp_less(x,3.25) )
7026  {
7027  x2 = x*x;
7028  an = 1.13681498971755972054E-11;
7029  an = an*x2+8.49262267667473811108E-10;
7030  an = an*x2+1.94434204175553054283E-8;
7031  an = an*x2+9.53151741254484363489E-7;
7032  an = an*x2+3.07828309874913200438E-6;
7033  an = an*x2+3.52513368520288738649E-4;
7034  an = an*x2+(-8.50149846724410912031E-4);
7035  an = an*x2+4.22618223005546594270E-2;
7036  an = an*x2+(-9.17480371773452345351E-2);
7037  an = an*x2+9.99999999999999994612E-1;
7038  ad = 2.40372073066762605484E-11;
7039  ad = ad*x2+1.48864681368493396752E-9;
7040  ad = ad*x2+5.21265281010541664570E-8;
7041  ad = ad*x2+1.27258478273186970203E-6;
7042  ad = ad*x2+2.32490249820789513991E-5;
7043  ad = ad*x2+3.25524741826057911661E-4;
7044  ad = ad*x2+3.48805814657162590916E-3;
7045  ad = ad*x2+2.79448531198828973716E-2;
7046  ad = ad*x2+1.58874241960120565368E-1;
7047  ad = ad*x2+5.74918629489320327824E-1;
7048  ad = ad*x2+1.00000000000000000539E0;
7049  y = x*an/ad;
7050  result = sg*y;
7051  return result;
7052  }
7053  x2 = 1.0/(x*x);
7054  if( ae_fp_less(x,6.25) )
7055  {
7056  bn = 5.08955156417900903354E-1;
7057  bn = bn*x2-2.44754418142697847934E-1;
7058  bn = bn*x2+9.41512335303534411857E-2;
7059  bn = bn*x2-2.18711255142039025206E-2;
7060  bn = bn*x2+3.66207612329569181322E-3;
7061  bn = bn*x2-4.23209114460388756528E-4;
7062  bn = bn*x2+3.59641304793896631888E-5;
7063  bn = bn*x2-2.14640351719968974225E-6;
7064  bn = bn*x2+9.10010780076391431042E-8;
7065  bn = bn*x2-2.40274520828250956942E-9;
7066  bn = bn*x2+3.59233385440928410398E-11;
7067  bd = 1.00000000000000000000E0;
7068  bd = bd*x2-6.31839869873368190192E-1;
7069  bd = bd*x2+2.36706788228248691528E-1;
7070  bd = bd*x2-5.31806367003223277662E-2;
7071  bd = bd*x2+8.48041718586295374409E-3;
7072  bd = bd*x2-9.47996768486665330168E-4;
7073  bd = bd*x2+7.81025592944552338085E-5;
7074  bd = bd*x2-4.55875153252442634831E-6;
7075  bd = bd*x2+1.89100358111421846170E-7;
7076  bd = bd*x2-4.91324691331920606875E-9;
7077  bd = bd*x2+7.18466403235734541950E-11;
7078  y = 1.0/x+x2*bn/(bd*x);
7079  result = sg*0.5*y;
7080  return result;
7081  }
7082  if( ae_fp_greater(x,1.0E9) )
7083  {
7084  result = sg*0.5/x;
7085  return result;
7086  }
7087  cn = -5.90592860534773254987E-1;
7088  cn = cn*x2+6.29235242724368800674E-1;
7089  cn = cn*x2-1.72858975380388136411E-1;
7090  cn = cn*x2+1.64837047825189632310E-2;
7091  cn = cn*x2-4.86827613020462700845E-4;
7092  cd = 1.00000000000000000000E0;
7093  cd = cd*x2-2.69820057197544900361E0;
7094  cd = cd*x2+1.73270799045947845857E0;
7095  cd = cd*x2-3.93708582281939493482E-1;
7096  cd = cd*x2+3.44278924041233391079E-2;
7097  cd = cd*x2-9.73655226040941223894E-4;
7098  y = 1.0/x+x2*cn/(cd*x);
7099  result = sg*0.5*y;
7100  return result;
7101 }
7102 
7103 
7104 
7105 
7106 /*************************************************************************
7107 Complete elliptic integral of the first kind
7108 
7109 Approximates the integral
7110 
7111 
7112 
7113  pi/2
7114  -
7115  | |
7116  | dt
7117 K(m) = | ------------------
7118  | 2
7119  | | sqrt( 1 - m sin t )
7120  -
7121  0
7122 
7123 using the approximation
7124 
7125  P(x) - log x Q(x).
7126 
7127 ACCURACY:
7128 
7129  Relative error:
7130 arithmetic domain # trials peak rms
7131  IEEE 0,1 30000 2.5e-16 6.8e-17
7132 
7133 Cephes Math Library, Release 2.8: June, 2000
7134 Copyright 1984, 1987, 2000 by Stephen L. Moshier
7135 *************************************************************************/
7136 double ellipticintegralk(double m, ae_state *_state)
7137 {
7138  double result;
7139 
7140 
7141  result = ellipticintegralkhighprecision(1.0-m, _state);
7142  return result;
7143 }
7144 
7145 
7146 /*************************************************************************
7147 Complete elliptic integral of the first kind
7148 
7149 Approximates the integral
7150 
7151 
7152 
7153  pi/2
7154  -
7155  | |
7156  | dt
7157 K(m) = | ------------------
7158  | 2
7159  | | sqrt( 1 - m sin t )
7160  -
7161  0
7162 
7163 where m = 1 - m1, using the approximation
7164 
7165  P(x) - log x Q(x).
7166 
7167 The argument m1 is used rather than m so that the logarithmic
7168 singularity at m = 1 will be shifted to the origin; this
7169 preserves maximum accuracy.
7170 
7171 K(0) = pi/2.
7172 
7173 ACCURACY:
7174 
7175  Relative error:
7176 arithmetic domain # trials peak rms
7177  IEEE 0,1 30000 2.5e-16 6.8e-17
7178 
7179 Àëãîðèòì âçÿò èç áèáëèîòåêè Cephes
7180 *************************************************************************/
7181 double ellipticintegralkhighprecision(double m1, ae_state *_state)
7182 {
7183  double p;
7184  double q;
7185  double result;
7186 
7187 
7189  {
7190  result = 1.3862943611198906188E0-0.5*ae_log(m1, _state);
7191  }
7192  else
7193  {
7194  p = 1.37982864606273237150E-4;
7195  p = p*m1+2.28025724005875567385E-3;
7196  p = p*m1+7.97404013220415179367E-3;
7197  p = p*m1+9.85821379021226008714E-3;
7198  p = p*m1+6.87489687449949877925E-3;
7199  p = p*m1+6.18901033637687613229E-3;
7200  p = p*m1+8.79078273952743772254E-3;
7201  p = p*m1+1.49380448916805252718E-2;
7202  p = p*m1+3.08851465246711995998E-2;
7203  p = p*m1+9.65735902811690126535E-2;
7204  p = p*m1+1.38629436111989062502E0;
7205  q = 2.94078955048598507511E-5;
7206  q = q*m1+9.14184723865917226571E-4;
7207  q = q*m1+5.94058303753167793257E-3;
7208  q = q*m1+1.54850516649762399335E-2;
7209  q = q*m1+2.39089602715924892727E-2;
7210  q = q*m1+3.01204715227604046988E-2;
7211  q = q*m1+3.73774314173823228969E-2;
7212  q = q*m1+4.88280347570998239232E-2;
7213  q = q*m1+7.03124996963957469739E-2;
7214  q = q*m1+1.24999999999870820058E-1;
7215  q = q*m1+4.99999999999999999821E-1;
7216  result = p-q*ae_log(m1, _state);
7217  }
7218  return result;
7219 }
7220 
7221 
7222 /*************************************************************************
7223 Incomplete elliptic integral of the first kind F(phi|m)
7224 
7225 Approximates the integral
7226 
7227 
7228 
7229  phi
7230  -
7231  | |
7232  | dt
7233 F(phi_\m) = | ------------------
7234  | 2
7235  | | sqrt( 1 - m sin t )
7236  -
7237  0
7238 
7239 of amplitude phi and modulus m, using the arithmetic -
7240 geometric mean algorithm.
7241 
7242 
7243 
7244 
7245 ACCURACY:
7246 
7247 Tested at random points with m in [0, 1] and phi as indicated.
7248 
7249  Relative error:
7250 arithmetic domain # trials peak rms
7251  IEEE -10,10 200000 7.4e-16 1.0e-16
7252 
7253 Cephes Math Library Release 2.8: June, 2000
7254 Copyright 1984, 1987, 2000 by Stephen L. Moshier
7255 *************************************************************************/
7256 double incompleteellipticintegralk(double phi, double m, ae_state *_state)
7257 {
7258  double a;
7259  double b;
7260  double c;
7261  double e;
7262  double temp;
7263  double pio2;
7264  double t;
7265  double k;
7266  ae_int_t d;
7267  ae_int_t md;
7268  ae_int_t s;
7269  ae_int_t npio2;
7270  double result;
7271 
7272 
7273  pio2 = 1.57079632679489661923;
7274  if( ae_fp_eq(m,0) )
7275  {
7276  result = phi;
7277  return result;
7278  }
7279  a = 1-m;
7280  if( ae_fp_eq(a,0) )
7281  {
7282  result = ae_log(ae_tan(0.5*(pio2+phi), _state), _state);
7283  return result;
7284  }
7285  npio2 = ae_ifloor(phi/pio2, _state);
7286  if( npio2%2!=0 )
7287  {
7288  npio2 = npio2+1;
7289  }
7290  if( npio2!=0 )
7291  {
7292  k = ellipticintegralk(1-a, _state);
7293  phi = phi-npio2*pio2;
7294  }
7295  else
7296  {
7297  k = 0;
7298  }
7299  if( ae_fp_less(phi,0) )
7300  {
7301  phi = -phi;
7302  s = -1;
7303  }
7304  else
7305  {
7306  s = 0;
7307  }
7308  b = ae_sqrt(a, _state);
7309  t = ae_tan(phi, _state);
7310  if( ae_fp_greater(ae_fabs(t, _state),10) )
7311  {
7312  e = 1.0/(b*t);
7313  if( ae_fp_less(ae_fabs(e, _state),10) )
7314  {
7315  e = ae_atan(e, _state);
7316  if( npio2==0 )
7317  {
7318  k = ellipticintegralk(1-a, _state);
7319  }
7320  temp = k-incompleteellipticintegralk(e, m, _state);
7321  if( s<0 )
7322  {
7323  temp = -temp;
7324  }
7325  result = temp+npio2*k;
7326  return result;
7327  }
7328  }
7329  a = 1.0;
7330  c = ae_sqrt(m, _state);
7331  d = 1;
7332  md = 0;
7333  while(ae_fp_greater(ae_fabs(c/a, _state),ae_machineepsilon))
7334  {
7335  temp = b/a;
7336  phi = phi+ae_atan(t*temp, _state)+md*ae_pi;
7337  md = ae_trunc((phi+pio2)/ae_pi, _state);
7338  t = t*(1.0+temp)/(1.0-temp*t*t);
7339  c = 0.5*(a-b);
7340  temp = ae_sqrt(a*b, _state);
7341  a = 0.5*(a+b);
7342  b = temp;
7343  d = d+d;
7344  }
7345  temp = (ae_atan(t, _state)+md*ae_pi)/(d*a);
7346  if( s<0 )
7347  {
7348  temp = -temp;
7349  }
7350  result = temp+npio2*k;
7351  return result;
7352 }
7353 
7354 
7355 /*************************************************************************
7356 Complete elliptic integral of the second kind
7357 
7358 Approximates the integral
7359 
7360 
7361  pi/2
7362  -
7363  | | 2
7364 E(m) = | sqrt( 1 - m sin t ) dt
7365  | |
7366  -
7367  0
7368 
7369 using the approximation
7370 
7371  P(x) - x log x Q(x).
7372 
7373 ACCURACY:
7374 
7375  Relative error:
7376 arithmetic domain # trials peak rms
7377  IEEE 0, 1 10000 2.1e-16 7.3e-17
7378 
7379 Cephes Math Library, Release 2.8: June, 2000
7380 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
7381 *************************************************************************/
7382 double ellipticintegrale(double m, ae_state *_state)
7383 {
7384  double p;
7385  double q;
7386  double result;
7387 
7388 
7389  ae_assert(ae_fp_greater_eq(m,0)&&ae_fp_less_eq(m,1), "Domain error in EllipticIntegralE: m<0 or m>1", _state);
7390  m = 1-m;
7391  if( ae_fp_eq(m,0) )
7392  {
7393  result = 1;
7394  return result;
7395  }
7396  p = 1.53552577301013293365E-4;
7397  p = p*m+2.50888492163602060990E-3;
7398  p = p*m+8.68786816565889628429E-3;
7399  p = p*m+1.07350949056076193403E-2;
7400  p = p*m+7.77395492516787092951E-3;
7401  p = p*m+7.58395289413514708519E-3;
7402  p = p*m+1.15688436810574127319E-2;
7403  p = p*m+2.18317996015557253103E-2;
7404  p = p*m+5.68051945617860553470E-2;
7405  p = p*m+4.43147180560990850618E-1;
7406  p = p*m+1.00000000000000000299E0;
7407  q = 3.27954898576485872656E-5;
7408  q = q*m+1.00962792679356715133E-3;
7409  q = q*m+6.50609489976927491433E-3;
7410  q = q*m+1.68862163993311317300E-2;
7411  q = q*m+2.61769742454493659583E-2;
7412  q = q*m+3.34833904888224918614E-2;
7413  q = q*m+4.27180926518931511717E-2;
7414  q = q*m+5.85936634471101055642E-2;
7415  q = q*m+9.37499997197644278445E-2;
7416  q = q*m+2.49999999999888314361E-1;
7417  result = p-q*m*ae_log(m, _state);
7418  return result;
7419 }
7420 
7421 
7422 /*************************************************************************
7423 Incomplete elliptic integral of the second kind
7424 
7425 Approximates the integral
7426 
7427 
7428  phi
7429  -
7430  | |
7431  | 2
7432 E(phi_\m) = | sqrt( 1 - m sin t ) dt
7433  |
7434  | |
7435  -
7436  0
7437 
7438 of amplitude phi and modulus m, using the arithmetic -
7439 geometric mean algorithm.
7440 
7441 ACCURACY:
7442 
7443 Tested at random arguments with phi in [-10, 10] and m in
7444 [0, 1].
7445  Relative error:
7446 arithmetic domain # trials peak rms
7447  IEEE -10,10 150000 3.3e-15 1.4e-16
7448 
7449 Cephes Math Library Release 2.8: June, 2000
7450 Copyright 1984, 1987, 1993, 2000 by Stephen L. Moshier
7451 *************************************************************************/
7452 double incompleteellipticintegrale(double phi, double m, ae_state *_state)
7453 {
7454  double pio2;
7455  double a;
7456  double b;
7457  double c;
7458  double e;
7459  double temp;
7460  double lphi;
7461  double t;
7462  double ebig;
7463  ae_int_t d;
7464  ae_int_t md;
7465  ae_int_t npio2;
7466  ae_int_t s;
7467  double result;
7468 
7469 
7470  pio2 = 1.57079632679489661923;
7471  if( ae_fp_eq(m,0) )
7472  {
7473  result = phi;
7474  return result;
7475  }
7476  lphi = phi;
7477  npio2 = ae_ifloor(lphi/pio2, _state);
7478  if( npio2%2!=0 )
7479  {
7480  npio2 = npio2+1;
7481  }
7482  lphi = lphi-npio2*pio2;
7483  if( ae_fp_less(lphi,0) )
7484  {
7485  lphi = -lphi;
7486  s = -1;
7487  }
7488  else
7489  {
7490  s = 1;
7491  }
7492  a = 1.0-m;
7493  ebig = ellipticintegrale(m, _state);
7494  if( ae_fp_eq(a,0) )
7495  {
7496  temp = ae_sin(lphi, _state);
7497  if( s<0 )
7498  {
7499  temp = -temp;
7500  }
7501  result = temp+npio2*ebig;
7502  return result;
7503  }
7504  t = ae_tan(lphi, _state);
7505  b = ae_sqrt(a, _state);
7506 
7507  /*
7508  * Thanks to Brian Fitzgerald <fitzgb@mml0.meche.rpi.edu>
7509  * for pointing out an instability near odd multiples of pi/2
7510  */
7511  if( ae_fp_greater(ae_fabs(t, _state),10) )
7512  {
7513 
7514  /*
7515  * Transform the amplitude
7516  */
7517  e = 1.0/(b*t);
7518 
7519  /*
7520  * ... but avoid multiple recursions.
7521  */
7522  if( ae_fp_less(ae_fabs(e, _state),10) )
7523  {
7524  e = ae_atan(e, _state);
7525  temp = ebig+m*ae_sin(lphi, _state)*ae_sin(e, _state)-incompleteellipticintegrale(e, m, _state);
7526  if( s<0 )
7527  {
7528  temp = -temp;
7529  }
7530  result = temp+npio2*ebig;
7531  return result;
7532  }
7533  }
7534  c = ae_sqrt(m, _state);
7535  a = 1.0;
7536  d = 1;
7537  e = 0.0;
7538  md = 0;
7539  while(ae_fp_greater(ae_fabs(c/a, _state),ae_machineepsilon))
7540  {
7541  temp = b/a;
7542  lphi = lphi+ae_atan(t*temp, _state)+md*ae_pi;
7543  md = ae_trunc((lphi+pio2)/ae_pi, _state);
7544  t = t*(1.0+temp)/(1.0-temp*t*t);
7545  c = 0.5*(a-b);
7546  temp = ae_sqrt(a*b, _state);
7547  a = 0.5*(a+b);
7548  b = temp;
7549  d = d+d;
7550  e = e+c*ae_sin(lphi, _state);
7551  }
7552  temp = ebig/ellipticintegralk(m, _state);
7553  temp = temp*((ae_atan(t, _state)+md*ae_pi)/(d*a));
7554  temp = temp+e;
7555  if( s<0 )
7556  {
7557  temp = -temp;
7558  }
7559  result = temp+npio2*ebig;
7560  return result;
7561 }
7562 
7563 
7564 
7565 
7566 /*************************************************************************
7567 Exponential integral Ei(x)
7568 
7569  x
7570  - t
7571  | | e
7572  Ei(x) = -|- --- dt .
7573  | | t
7574  -
7575  -inf
7576 
7577 Not defined for x <= 0.
7578 See also expn.c.
7579 
7580 
7581 
7582 ACCURACY:
7583 
7584  Relative error:
7585 arithmetic domain # trials peak rms
7586  IEEE 0,100 50000 8.6e-16 1.3e-16
7587 
7588 Cephes Math Library Release 2.8: May, 1999
7589 Copyright 1999 by Stephen L. Moshier
7590 *************************************************************************/
7591 double exponentialintegralei(double x, ae_state *_state)
7592 {
7593  double eul;
7594  double f;
7595  double f1;
7596  double f2;
7597  double w;
7598  double result;
7599 
7600 
7601  eul = 0.5772156649015328606065;
7602  if( ae_fp_less_eq(x,0) )
7603  {
7604  result = 0;
7605  return result;
7606  }
7607  if( ae_fp_less(x,2) )
7608  {
7609  f1 = -5.350447357812542947283;
7610  f1 = f1*x+218.5049168816613393830;
7611  f1 = f1*x-4176.572384826693777058;
7612  f1 = f1*x+55411.76756393557601232;
7613  f1 = f1*x-331338.1331178144034309;
7614  f1 = f1*x+1592627.163384945414220;
7615  f2 = 1.000000000000000000000;
7616  f2 = f2*x-52.50547959112862969197;
7617  f2 = f2*x+1259.616186786790571525;
7618  f2 = f2*x-17565.49581973534652631;
7619  f2 = f2*x+149306.2117002725991967;
7620  f2 = f2*x-729494.9239640527645655;
7621  f2 = f2*x+1592627.163384945429726;
7622  f = f1/f2;
7623  result = eul+ae_log(x, _state)+x*f;
7624  return result;
7625  }
7626  if( ae_fp_less(x,4) )
7627  {
7628  w = 1/x;
7629  f1 = 1.981808503259689673238E-2;
7630  f1 = f1*w-1.271645625984917501326;
7631  f1 = f1*w-2.088160335681228318920;
7632  f1 = f1*w+2.755544509187936721172;
7633  f1 = f1*w-4.409507048701600257171E-1;
7634  f1 = f1*w+4.665623805935891391017E-2;
7635  f1 = f1*w-1.545042679673485262580E-3;
7636  f1 = f1*w+7.059980605299617478514E-5;
7637  f2 = 1.000000000000000000000;
7638  f2 = f2*w+1.476498670914921440652;
7639  f2 = f2*w+5.629177174822436244827E-1;
7640  f2 = f2*w+1.699017897879307263248E-1;
7641  f2 = f2*w+2.291647179034212017463E-2;
7642  f2 = f2*w+4.450150439728752875043E-3;
7643  f2 = f2*w+1.727439612206521482874E-4;
7644  f2 = f2*w+3.953167195549672482304E-5;
7645  f = f1/f2;
7646  result = ae_exp(x, _state)*w*(1+w*f);
7647  return result;
7648  }
7649  if( ae_fp_less(x,8) )
7650  {
7651  w = 1/x;
7652  f1 = -1.373215375871208729803;
7653  f1 = f1*w-7.084559133740838761406E-1;
7654  f1 = f1*w+1.580806855547941010501;
7655  f1 = f1*w-2.601500427425622944234E-1;
7656  f1 = f1*w+2.994674694113713763365E-2;
7657  f1 = f1*w-1.038086040188744005513E-3;
7658  f1 = f1*w+4.371064420753005429514E-5;
7659  f1 = f1*w+2.141783679522602903795E-6;
7660  f2 = 1.000000000000000000000;
7661  f2 = f2*w+8.585231423622028380768E-1;
7662  f2 = f2*w+4.483285822873995129957E-1;
7663  f2 = f2*w+7.687932158124475434091E-2;
7664  f2 = f2*w+2.449868241021887685904E-2;
7665  f2 = f2*w+8.832165941927796567926E-4;
7666  f2 = f2*w+4.590952299511353531215E-4;
7667  f2 = f2*w+(-4.729848351866523044863E-6);
7668  f2 = f2*w+2.665195537390710170105E-6;
7669  f = f1/f2;
7670  result = ae_exp(x, _state)*w*(1+w*f);
7671  return result;
7672  }
7673  if( ae_fp_less(x,16) )
7674  {
7675  w = 1/x;
7676  f1 = -2.106934601691916512584;
7677  f1 = f1*w+1.732733869664688041885;
7678  f1 = f1*w-2.423619178935841904839E-1;
7679  f1 = f1*w+2.322724180937565842585E-2;
7680  f1 = f1*w+2.372880440493179832059E-4;
7681  f1 = f1*w-8.343219561192552752335E-5;
7682  f1 = f1*w+1.363408795605250394881E-5;
7683  f1 = f1*w-3.655412321999253963714E-7;
7684  f1 = f1*w+1.464941733975961318456E-8;
7685  f1 = f1*w+6.176407863710360207074E-10;
7686  f2 = 1.000000000000000000000;
7687  f2 = f2*w-2.298062239901678075778E-1;
7688  f2 = f2*w+1.105077041474037862347E-1;
7689  f2 = f2*w-1.566542966630792353556E-2;
7690  f2 = f2*w+2.761106850817352773874E-3;
7691  f2 = f2*w-2.089148012284048449115E-4;
7692  f2 = f2*w+1.708528938807675304186E-5;
7693  f2 = f2*w-4.459311796356686423199E-7;
7694  f2 = f2*w+1.394634930353847498145E-8;
7695  f2 = f2*w+6.150865933977338354138E-10;
7696  f = f1/f2;
7697  result = ae_exp(x, _state)*w*(1+w*f);
7698  return result;
7699  }
7700  if( ae_fp_less(x,32) )
7701  {
7702  w = 1/x;
7703  f1 = -2.458119367674020323359E-1;
7704  f1 = f1*w-1.483382253322077687183E-1;
7705  f1 = f1*w+7.248291795735551591813E-2;
7706  f1 = f1*w-1.348315687380940523823E-2;
7707  f1 = f1*w+1.342775069788636972294E-3;
7708  f1 = f1*w-7.942465637159712264564E-5;
7709  f1 = f1*w+2.644179518984235952241E-6;
7710  f1 = f1*w-4.239473659313765177195E-8;
7711  f2 = 1.000000000000000000000;
7712  f2 = f2*w-1.044225908443871106315E-1;
7713  f2 = f2*w-2.676453128101402655055E-1;
7714  f2 = f2*w+9.695000254621984627876E-2;
7715  f2 = f2*w-1.601745692712991078208E-2;
7716  f2 = f2*w+1.496414899205908021882E-3;
7717  f2 = f2*w-8.462452563778485013756E-5;
7718  f2 = f2*w+2.728938403476726394024E-6;
7719  f2 = f2*w-4.239462431819542051337E-8;
7720  f = f1/f2;
7721  result = ae_exp(x, _state)*w*(1+w*f);
7722  return result;
7723  }
7724  if( ae_fp_less(x,64) )
7725  {
7726  w = 1/x;
7727  f1 = 1.212561118105456670844E-1;
7728  f1 = f1*w-5.823133179043894485122E-1;
7729  f1 = f1*w+2.348887314557016779211E-1;
7730  f1 = f1*w-3.040034318113248237280E-2;
7731  f1 = f1*w+1.510082146865190661777E-3;
7732  f1 = f1*w-2.523137095499571377122E-5;
7733  f2 = 1.000000000000000000000;
7734  f2 = f2*w-1.002252150365854016662;
7735  f2 = f2*w+2.928709694872224144953E-1;
7736  f2 = f2*w-3.337004338674007801307E-2;
7737  f2 = f2*w+1.560544881127388842819E-3;
7738  f2 = f2*w-2.523137093603234562648E-5;
7739  f = f1/f2;
7740  result = ae_exp(x, _state)*w*(1+w*f);
7741  return result;
7742  }
7743  w = 1/x;
7744  f1 = -7.657847078286127362028E-1;
7745  f1 = f1*w+6.886192415566705051750E-1;
7746  f1 = f1*w-2.132598113545206124553E-1;
7747  f1 = f1*w+3.346107552384193813594E-2;
7748  f1 = f1*w-3.076541477344756050249E-3;
7749  f1 = f1*w+1.747119316454907477380E-4;
7750  f1 = f1*w-6.103711682274170530369E-6;
7751  f1 = f1*w+1.218032765428652199087E-7;
7752  f1 = f1*w-1.086076102793290233007E-9;
7753  f2 = 1.000000000000000000000;
7754  f2 = f2*w-1.888802868662308731041;
7755  f2 = f2*w+1.066691687211408896850;
7756  f2 = f2*w-2.751915982306380647738E-1;
7757  f2 = f2*w+3.930852688233823569726E-2;
7758  f2 = f2*w-3.414684558602365085394E-3;
7759  f2 = f2*w+1.866844370703555398195E-4;
7760  f2 = f2*w-6.345146083130515357861E-6;
7761  f2 = f2*w+1.239754287483206878024E-7;
7762  f2 = f2*w-1.086076102793126632978E-9;
7763  f = f1/f2;
7764  result = ae_exp(x, _state)*w*(1+w*f);
7765  return result;
7766 }
7767 
7768 
7769 /*************************************************************************
7770 Exponential integral En(x)
7771 
7772 Evaluates the exponential integral
7773 
7774  inf.
7775  -
7776  | | -xt
7777  | e
7778  E (x) = | ---- dt.
7779  n | n
7780  | | t
7781  -
7782  1
7783 
7784 
7785 Both n and x must be nonnegative.
7786 
7787 The routine employs either a power series, a continued
7788 fraction, or an asymptotic formula depending on the
7789 relative values of n and x.
7790 
7791 ACCURACY:
7792 
7793  Relative error:
7794 arithmetic domain # trials peak rms
7795  IEEE 0, 30 10000 1.7e-15 3.6e-16
7796 
7797 Cephes Math Library Release 2.8: June, 2000
7798 Copyright 1985, 2000 by Stephen L. Moshier
7799 *************************************************************************/
7800 double exponentialintegralen(double x, ae_int_t n, ae_state *_state)
7801 {
7802  double r;
7803  double t;
7804  double yk;
7805  double xk;
7806  double pk;
7807  double pkm1;
7808  double pkm2;
7809  double qk;
7810  double qkm1;
7811  double qkm2;
7812  double psi;
7813  double z;
7814  ae_int_t i;
7815  ae_int_t k;
7816  double big;
7817  double eul;
7818  double result;
7819 
7820 
7821  eul = 0.57721566490153286060;
7822  big = 1.44115188075855872*ae_pow(10, 17, _state);
7823  if( ((n<0||ae_fp_less(x,0))||ae_fp_greater(x,170))||(ae_fp_eq(x,0)&&n<2) )
7824  {
7825  result = -1;
7826  return result;
7827  }
7828  if( ae_fp_eq(x,0) )
7829  {
7830  result = (double)1/(double)(n-1);
7831  return result;
7832  }
7833  if( n==0 )
7834  {
7835  result = ae_exp(-x, _state)/x;
7836  return result;
7837  }
7838  if( n>5000 )
7839  {
7840  xk = x+n;
7841  yk = 1/(xk*xk);
7842  t = n;
7843  result = yk*t*(6*x*x-8*t*x+t*t);
7844  result = yk*(result+t*(t-2.0*x));
7845  result = yk*(result+t);
7846  result = (result+1)*ae_exp(-x, _state)/xk;
7847  return result;
7848  }
7849  if( ae_fp_less_eq(x,1) )
7850  {
7851  psi = -eul-ae_log(x, _state);
7852  for(i=1; i<=n-1; i++)
7853  {
7854  psi = psi+(double)1/(double)i;
7855  }
7856  z = -x;
7857  xk = 0;
7858  yk = 1;
7859  pk = 1-n;
7860  if( n==1 )
7861  {
7862  result = 0.0;
7863  }
7864  else
7865  {
7866  result = 1.0/pk;
7867  }
7868  do
7869  {
7870  xk = xk+1;
7871  yk = yk*z/xk;
7872  pk = pk+1;
7873  if( ae_fp_neq(pk,0) )
7874  {
7875  result = result+yk/pk;
7876  }
7877  if( ae_fp_neq(result,0) )
7878  {
7879  t = ae_fabs(yk/result, _state);
7880  }
7881  else
7882  {
7883  t = 1;
7884  }
7885  }
7887  t = 1;
7888  for(i=1; i<=n-1; i++)
7889  {
7890  t = t*z/i;
7891  }
7892  result = psi*t-result;
7893  return result;
7894  }
7895  else
7896  {
7897  k = 1;
7898  pkm2 = 1;
7899  qkm2 = x;
7900  pkm1 = 1.0;
7901  qkm1 = x+n;
7902  result = pkm1/qkm1;
7903  do
7904  {
7905  k = k+1;
7906  if( k%2==1 )
7907  {
7908  yk = 1;
7909  xk = n+(double)(k-1)/(double)2;
7910  }
7911  else
7912  {
7913  yk = x;
7914  xk = (double)k/(double)2;
7915  }
7916  pk = pkm1*yk+pkm2*xk;
7917  qk = qkm1*yk+qkm2*xk;
7918  if( ae_fp_neq(qk,0) )
7919  {
7920  r = pk/qk;
7921  t = ae_fabs((result-r)/r, _state);
7922  result = r;
7923  }
7924  else
7925  {
7926  t = 1;
7927  }
7928  pkm2 = pkm1;
7929  pkm1 = pk;
7930  qkm2 = qkm1;
7931  qkm1 = qk;
7932  if( ae_fp_greater(ae_fabs(pk, _state),big) )
7933  {
7934  pkm2 = pkm2/big;
7935  pkm1 = pkm1/big;
7936  qkm2 = qkm2/big;
7937  qkm1 = qkm1/big;
7938  }
7939  }
7941  result = result*ae_exp(-x, _state);
7942  }
7943  return result;
7944 }
7945 
7946 
7947 
7948 
7949 /*************************************************************************
7950 F distribution
7951 
7952 Returns the area from zero to x under the F density
7953 function (also known as Snedcor's density or the
7954 variance ratio density). This is the density
7955 of x = (u1/df1)/(u2/df2), where u1 and u2 are random
7956 variables having Chi square distributions with df1
7957 and df2 degrees of freedom, respectively.
7958 The incomplete beta integral is used, according to the
7959 formula
7960 
7961 P(x) = incbet( df1/2, df2/2, (df1*x/(df2 + df1*x) ).
7962 
7963 
7964 The arguments a and b are greater than zero, and x is
7965 nonnegative.
7966 
7967 ACCURACY:
7968 
7969 Tested at random points (a,b,x).
7970 
7971  x a,b Relative error:
7972 arithmetic domain domain # trials peak rms
7973  IEEE 0,1 0,100 100000 9.8e-15 1.7e-15
7974  IEEE 1,5 0,100 100000 6.5e-15 3.5e-16
7975  IEEE 0,1 1,10000 100000 2.2e-11 3.3e-12
7976  IEEE 1,5 1,10000 100000 1.1e-11 1.7e-13
7977 
7978 Cephes Math Library Release 2.8: June, 2000
7979 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
7980 *************************************************************************/
7981 double fdistribution(ae_int_t a, ae_int_t b, double x, ae_state *_state)
7982 {
7983  double w;
7984  double result;
7985 
7986 
7987  ae_assert((a>=1&&b>=1)&&ae_fp_greater_eq(x,0), "Domain error in FDistribution", _state);
7988  w = a*x;
7989  w = w/(b+w);
7990  result = incompletebeta(0.5*a, 0.5*b, w, _state);
7991  return result;
7992 }
7993 
7994 
7995 /*************************************************************************
7996 Complemented F distribution
7997 
7998 Returns the area from x to infinity under the F density
7999 function (also known as Snedcor's density or the
8000 variance ratio density).
8001 
8002 
8003  inf.
8004  -
8005  1 | | a-1 b-1
8006 1-P(x) = ------ | t (1-t) dt
8007  B(a,b) | |
8008  -
8009  x
8010 
8011 
8012 The incomplete beta integral is used, according to the
8013 formula
8014 
8015 P(x) = incbet( df2/2, df1/2, (df2/(df2 + df1*x) ).
8016 
8017 
8018 ACCURACY:
8019 
8020 Tested at random points (a,b,x) in the indicated intervals.
8021  x a,b Relative error:
8022 arithmetic domain domain # trials peak rms
8023  IEEE 0,1 1,100 100000 3.7e-14 5.9e-16
8024  IEEE 1,5 1,100 100000 8.0e-15 1.6e-15
8025  IEEE 0,1 1,10000 100000 1.8e-11 3.5e-13
8026  IEEE 1,5 1,10000 100000 2.0e-11 3.0e-12
8027 
8028 Cephes Math Library Release 2.8: June, 2000
8029 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8030 *************************************************************************/
8031 double fcdistribution(ae_int_t a, ae_int_t b, double x, ae_state *_state)
8032 {
8033  double w;
8034  double result;
8035 
8036 
8037  ae_assert((a>=1&&b>=1)&&ae_fp_greater_eq(x,0), "Domain error in FCDistribution", _state);
8038  w = b/(b+a*x);
8039  result = incompletebeta(0.5*b, 0.5*a, w, _state);
8040  return result;
8041 }
8042 
8043 
8044 /*************************************************************************
8045 Inverse of complemented F distribution
8046 
8047 Finds the F density argument x such that the integral
8048 from x to infinity of the F density is equal to the
8049 given probability p.
8050 
8051 This is accomplished using the inverse beta integral
8052 function and the relations
8053 
8054  z = incbi( df2/2, df1/2, p )
8055  x = df2 (1-z) / (df1 z).
8056 
8057 Note: the following relations hold for the inverse of
8058 the uncomplemented F distribution:
8059 
8060  z = incbi( df1/2, df2/2, p )
8061  x = df2 z / (df1 (1-z)).
8062 
8063 ACCURACY:
8064 
8065 Tested at random points (a,b,p).
8066 
8067  a,b Relative error:
8068 arithmetic domain # trials peak rms
8069  For p between .001 and 1:
8070  IEEE 1,100 100000 8.3e-15 4.7e-16
8071  IEEE 1,10000 100000 2.1e-11 1.4e-13
8072  For p between 10^-6 and 10^-3:
8073  IEEE 1,100 50000 1.3e-12 8.4e-15
8074  IEEE 1,10000 50000 3.0e-12 4.8e-14
8075 
8076 Cephes Math Library Release 2.8: June, 2000
8077 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8078 *************************************************************************/
8080  ae_int_t b,
8081  double y,
8082  ae_state *_state)
8083 {
8084  double w;
8085  double result;
8086 
8087 
8088  ae_assert(((a>=1&&b>=1)&&ae_fp_greater(y,0))&&ae_fp_less_eq(y,1), "Domain error in InvFDistribution", _state);
8089 
8090  /*
8091  * Compute probability for x = 0.5
8092  */
8093  w = incompletebeta(0.5*b, 0.5*a, 0.5, _state);
8094 
8095  /*
8096  * If that is greater than y, then the solution w < .5
8097  * Otherwise, solve at 1-y to remove cancellation in (b - b*w)
8098  */
8099  if( ae_fp_greater(w,y)||ae_fp_less(y,0.001) )
8100  {
8101  w = invincompletebeta(0.5*b, 0.5*a, y, _state);
8102  result = (b-b*w)/(a*w);
8103  }
8104  else
8105  {
8106  w = invincompletebeta(0.5*a, 0.5*b, 1.0-y, _state);
8107  result = b*w/(a*(1.0-w));
8108  }
8109  return result;
8110 }
8111 
8112 
8113 
8114 
8115 /*************************************************************************
8116 Fresnel integral
8117 
8118 Evaluates the Fresnel integrals
8119 
8120  x
8121  -
8122  | |
8123 C(x) = | cos(pi/2 t**2) dt,
8124  | |
8125  -
8126  0
8127 
8128  x
8129  -
8130  | |
8131 S(x) = | sin(pi/2 t**2) dt.
8132  | |
8133  -
8134  0
8135 
8136 
8137 The integrals are evaluated by a power series for x < 1.
8138 For x >= 1 auxiliary functions f(x) and g(x) are employed
8139 such that
8140 
8141 C(x) = 0.5 + f(x) sin( pi/2 x**2 ) - g(x) cos( pi/2 x**2 )
8142 S(x) = 0.5 - f(x) cos( pi/2 x**2 ) - g(x) sin( pi/2 x**2 )
8143 
8144 
8145 
8146 ACCURACY:
8147 
8148  Relative error.
8149 
8150 Arithmetic function domain # trials peak rms
8151  IEEE S(x) 0, 10 10000 2.0e-15 3.2e-16
8152  IEEE C(x) 0, 10 10000 1.8e-15 3.3e-16
8153 
8154 Cephes Math Library Release 2.8: June, 2000
8155 Copyright 1984, 1987, 1989, 2000 by Stephen L. Moshier
8156 *************************************************************************/
8157 void fresnelintegral(double x, double* c, double* s, ae_state *_state)
8158 {
8159  double xxa;
8160  double f;
8161  double g;
8162  double cc;
8163  double ss;
8164  double t;
8165  double u;
8166  double x2;
8167  double sn;
8168  double sd;
8169  double cn;
8170  double cd;
8171  double fn;
8172  double fd;
8173  double gn;
8174  double gd;
8175  double mpi;
8176  double mpio2;
8177 
8178 
8179  mpi = 3.14159265358979323846;
8180  mpio2 = 1.57079632679489661923;
8181  xxa = x;
8182  x = ae_fabs(xxa, _state);
8183  x2 = x*x;
8184  if( ae_fp_less(x2,2.5625) )
8185  {
8186  t = x2*x2;
8187  sn = -2.99181919401019853726E3;
8188  sn = sn*t+7.08840045257738576863E5;
8189  sn = sn*t-6.29741486205862506537E7;
8190  sn = sn*t+2.54890880573376359104E9;
8191  sn = sn*t-4.42979518059697779103E10;
8192  sn = sn*t+3.18016297876567817986E11;
8193  sd = 1.00000000000000000000E0;
8194  sd = sd*t+2.81376268889994315696E2;
8195  sd = sd*t+4.55847810806532581675E4;
8196  sd = sd*t+5.17343888770096400730E6;
8197  sd = sd*t+4.19320245898111231129E8;
8198  sd = sd*t+2.24411795645340920940E10;
8199  sd = sd*t+6.07366389490084639049E11;
8200  cn = -4.98843114573573548651E-8;
8201  cn = cn*t+9.50428062829859605134E-6;
8202  cn = cn*t-6.45191435683965050962E-4;
8203  cn = cn*t+1.88843319396703850064E-2;
8204  cn = cn*t-2.05525900955013891793E-1;
8205  cn = cn*t+9.99999999999999998822E-1;
8206  cd = 3.99982968972495980367E-12;
8207  cd = cd*t+9.15439215774657478799E-10;
8208  cd = cd*t+1.25001862479598821474E-7;
8209  cd = cd*t+1.22262789024179030997E-5;
8210  cd = cd*t+8.68029542941784300606E-4;
8211  cd = cd*t+4.12142090722199792936E-2;
8212  cd = cd*t+1.00000000000000000118E0;
8213  *s = ae_sign(xxa, _state)*x*x2*sn/sd;
8214  *c = ae_sign(xxa, _state)*x*cn/cd;
8215  return;
8216  }
8217  if( ae_fp_greater(x,36974.0) )
8218  {
8219  *c = ae_sign(xxa, _state)*0.5;
8220  *s = ae_sign(xxa, _state)*0.5;
8221  return;
8222  }
8223  x2 = x*x;
8224  t = mpi*x2;
8225  u = 1/(t*t);
8226  t = 1/t;
8227  fn = 4.21543555043677546506E-1;
8228  fn = fn*u+1.43407919780758885261E-1;
8229  fn = fn*u+1.15220955073585758835E-2;
8230  fn = fn*u+3.45017939782574027900E-4;
8231  fn = fn*u+4.63613749287867322088E-6;
8232  fn = fn*u+3.05568983790257605827E-8;
8233  fn = fn*u+1.02304514164907233465E-10;
8234  fn = fn*u+1.72010743268161828879E-13;
8235  fn = fn*u+1.34283276233062758925E-16;
8236  fn = fn*u+3.76329711269987889006E-20;
8237  fd = 1.00000000000000000000E0;
8238  fd = fd*u+7.51586398353378947175E-1;
8239  fd = fd*u+1.16888925859191382142E-1;
8240  fd = fd*u+6.44051526508858611005E-3;
8241  fd = fd*u+1.55934409164153020873E-4;
8242  fd = fd*u+1.84627567348930545870E-6;
8243  fd = fd*u+1.12699224763999035261E-8;
8244  fd = fd*u+3.60140029589371370404E-11;
8245  fd = fd*u+5.88754533621578410010E-14;
8246  fd = fd*u+4.52001434074129701496E-17;
8247  fd = fd*u+1.25443237090011264384E-20;
8248  gn = 5.04442073643383265887E-1;
8249  gn = gn*u+1.97102833525523411709E-1;
8250  gn = gn*u+1.87648584092575249293E-2;
8251  gn = gn*u+6.84079380915393090172E-4;
8252  gn = gn*u+1.15138826111884280931E-5;
8253  gn = gn*u+9.82852443688422223854E-8;
8254  gn = gn*u+4.45344415861750144738E-10;
8255  gn = gn*u+1.08268041139020870318E-12;
8256  gn = gn*u+1.37555460633261799868E-15;
8257  gn = gn*u+8.36354435630677421531E-19;
8258  gn = gn*u+1.86958710162783235106E-22;
8259  gd = 1.00000000000000000000E0;
8260  gd = gd*u+1.47495759925128324529E0;
8261  gd = gd*u+3.37748989120019970451E-1;
8262  gd = gd*u+2.53603741420338795122E-2;
8263  gd = gd*u+8.14679107184306179049E-4;
8264  gd = gd*u+1.27545075667729118702E-5;
8265  gd = gd*u+1.04314589657571990585E-7;
8266  gd = gd*u+4.60680728146520428211E-10;
8267  gd = gd*u+1.10273215066240270757E-12;
8268  gd = gd*u+1.38796531259578871258E-15;
8269  gd = gd*u+8.39158816283118707363E-19;
8270  gd = gd*u+1.86958710162783236342E-22;
8271  f = 1-u*fn/fd;
8272  g = t*gn/gd;
8273  t = mpio2*x2;
8274  cc = ae_cos(t, _state);
8275  ss = ae_sin(t, _state);
8276  t = mpi*x;
8277  *c = 0.5+(f*ss-g*cc)/t;
8278  *s = 0.5-(f*cc+g*ss)/t;
8279  *c = *c*ae_sign(xxa, _state);
8280  *s = *s*ae_sign(xxa, _state);
8281 }
8282 
8283 
8284 
8285 
8286 /*************************************************************************
8287 Calculation of the value of the Hermite polynomial.
8288 
8289 Parameters:
8290  n - degree, n>=0
8291  x - argument
8292 
8293 Result:
8294  the value of the Hermite polynomial Hn at x
8295 *************************************************************************/
8296 double hermitecalculate(ae_int_t n, double x, ae_state *_state)
8297 {
8298  ae_int_t i;
8299  double a;
8300  double b;
8301  double result;
8302 
8303 
8304  result = 0;
8305 
8306  /*
8307  * Prepare A and B
8308  */
8309  a = 1;
8310  b = 2*x;
8311 
8312  /*
8313  * Special cases: N=0 or N=1
8314  */
8315  if( n==0 )
8316  {
8317  result = a;
8318  return result;
8319  }
8320  if( n==1 )
8321  {
8322  result = b;
8323  return result;
8324  }
8325 
8326  /*
8327  * General case: N>=2
8328  */
8329  for(i=2; i<=n; i++)
8330  {
8331  result = 2*x*b-2*(i-1)*a;
8332  a = b;
8333  b = result;
8334  }
8335  return result;
8336 }
8337 
8338 
8339 /*************************************************************************
8340 Summation of Hermite polynomials using Clenshaw’s recurrence formula.
8341 
8342 This routine calculates
8343  c[0]*H0(x) + c[1]*H1(x) + ... + c[N]*HN(x)
8344 
8345 Parameters:
8346  n - degree, n>=0
8347  x - argument
8348 
8349 Result:
8350  the value of the Hermite polynomial at x
8351 *************************************************************************/
8352 double hermitesum(/* Real */ ae_vector* c,
8353  ae_int_t n,
8354  double x,
8355  ae_state *_state)
8356 {
8357  double b1;
8358  double b2;
8359  ae_int_t i;
8360  double result;
8361 
8362 
8363  b1 = 0;
8364  b2 = 0;
8365  result = 0;
8366  for(i=n; i>=0; i--)
8367  {
8368  result = 2*(x*b1-(i+1)*b2)+c->ptr.p_double[i];
8369  b2 = b1;
8370  b1 = result;
8371  }
8372  return result;
8373 }
8374 
8375 
8376 /*************************************************************************
8377 Representation of Hn as C[0] + C[1]*X + ... + C[N]*X^N
8378 
8379 Input parameters:
8380  N - polynomial degree, n>=0
8381 
8382 Output parameters:
8383  C - coefficients
8384 *************************************************************************/
8386  /* Real */ ae_vector* c,
8387  ae_state *_state)
8388 {
8389  ae_int_t i;
8390 
8391  ae_vector_clear(c);
8392 
8393  ae_vector_set_length(c, n+1, _state);
8394  for(i=0; i<=n; i++)
8395  {
8396  c->ptr.p_double[i] = 0;
8397  }
8398  c->ptr.p_double[n] = ae_exp(n*ae_log(2, _state), _state);
8399  for(i=0; i<=n/2-1; i++)
8400  {
8401  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);
8402  }
8403 }
8404 
8405 
8406 
8407 
8408 /*************************************************************************
8409 Jacobian Elliptic Functions
8410 
8411 Evaluates the Jacobian elliptic functions sn(u|m), cn(u|m),
8412 and dn(u|m) of parameter m between 0 and 1, and real
8413 argument u.
8414 
8415 These functions are periodic, with quarter-period on the
8416 real axis equal to the complete elliptic integral
8417 ellpk(1.0-m).
8418 
8419 Relation to incomplete elliptic integral:
8420 If u = ellik(phi,m), then sn(u|m) = sin(phi),
8421 and cn(u|m) = cos(phi). Phi is called the amplitude of u.
8422 
8423 Computation is by means of the arithmetic-geometric mean
8424 algorithm, except when m is within 1e-9 of 0 or 1. In the
8425 latter case with m close to 1, the approximation applies
8426 only for phi < pi/2.
8427 
8428 ACCURACY:
8429 
8430 Tested at random points with u between 0 and 10, m between
8431 0 and 1.
8432 
8433  Absolute error (* = relative error):
8434 arithmetic function # trials peak rms
8435  IEEE phi 10000 9.2e-16* 1.4e-16*
8436  IEEE sn 50000 4.1e-15 4.6e-16
8437  IEEE cn 40000 3.6e-15 4.4e-16
8438  IEEE dn 10000 1.3e-12 1.8e-14
8439 
8440  Peak error observed in consistency check using addition
8441 theorem for sn(u+v) was 4e-16 (absolute). Also tested by
8442 the above relation to the incomplete elliptic integral.
8443 Accuracy deteriorates when u is large.
8444 
8445 Cephes Math Library Release 2.8: June, 2000
8446 Copyright 1984, 1987, 2000 by Stephen L. Moshier
8447 *************************************************************************/
8449  double m,
8450  double* sn,
8451  double* cn,
8452  double* dn,
8453  double* ph,
8454  ae_state *_state)
8455 {
8456  ae_frame _frame_block;
8457  double ai;
8458  double b;
8459  double phi;
8460  double t;
8461  double twon;
8462  ae_vector a;
8463  ae_vector c;
8464  ae_int_t i;
8465 
8466  ae_frame_make(_state, &_frame_block);
8467  *sn = 0;
8468  *cn = 0;
8469  *dn = 0;
8470  *ph = 0;
8471  ae_vector_init(&a, 0, DT_REAL, _state, ae_true);
8472  ae_vector_init(&c, 0, DT_REAL, _state, ae_true);
8473 
8474  ae_assert(ae_fp_greater_eq(m,0)&&ae_fp_less_eq(m,1), "Domain error in JacobianEllipticFunctions: m<0 or m>1", _state);
8475  ae_vector_set_length(&a, 8+1, _state);
8476  ae_vector_set_length(&c, 8+1, _state);
8477  if( ae_fp_less(m,1.0e-9) )
8478  {
8479  t = ae_sin(u, _state);
8480  b = ae_cos(u, _state);
8481  ai = 0.25*m*(u-t*b);
8482  *sn = t-ai*b;
8483  *cn = b+ai*t;
8484  *ph = u-ai;
8485  *dn = 1.0-0.5*m*t*t;
8486  ae_frame_leave(_state);
8487  return;
8488  }
8489  if( ae_fp_greater_eq(m,0.9999999999) )
8490  {
8491  ai = 0.25*(1.0-m);
8492  b = ae_cosh(u, _state);
8493  t = ae_tanh(u, _state);
8494  phi = 1.0/b;
8495  twon = b*ae_sinh(u, _state);
8496  *sn = t+ai*(twon-u)/(b*b);
8497  *ph = 2.0*ae_atan(ae_exp(u, _state), _state)-1.57079632679489661923+ai*(twon-u)/b;
8498  ai = ai*t*phi;
8499  *cn = phi-ai*(twon-u);
8500  *dn = phi+ai*(twon+u);
8501  ae_frame_leave(_state);
8502  return;
8503  }
8504  a.ptr.p_double[0] = 1.0;
8505  b = ae_sqrt(1.0-m, _state);
8506  c.ptr.p_double[0] = ae_sqrt(m, _state);
8507  twon = 1.0;
8508  i = 0;
8509  while(ae_fp_greater(ae_fabs(c.ptr.p_double[i]/a.ptr.p_double[i], _state),ae_machineepsilon))
8510  {
8511  if( i>7 )
8512  {
8513  ae_assert(ae_false, "Overflow in JacobianEllipticFunctions", _state);
8514  break;
8515  }
8516  ai = a.ptr.p_double[i];
8517  i = i+1;
8518  c.ptr.p_double[i] = 0.5*(ai-b);
8519  t = ae_sqrt(ai*b, _state);
8520  a.ptr.p_double[i] = 0.5*(ai+b);
8521  b = t;
8522  twon = twon*2.0;
8523  }
8524  phi = twon*a.ptr.p_double[i]*u;
8525  do
8526  {
8527  t = c.ptr.p_double[i]*ae_sin(phi, _state)/a.ptr.p_double[i];
8528  b = phi;
8529  phi = (ae_asin(t, _state)+phi)/2.0;
8530  i = i-1;
8531  }
8532  while(i!=0);
8533  *sn = ae_sin(phi, _state);
8534  t = ae_cos(phi, _state);
8535  *cn = t;
8536  *dn = t/ae_cos(phi-b, _state);
8537  *ph = phi;
8538  ae_frame_leave(_state);
8539 }
8540 
8541 
8542 
8543 
8544 /*************************************************************************
8545 Calculation of the value of the Laguerre polynomial.
8546 
8547 Parameters:
8548  n - degree, n>=0
8549  x - argument
8550 
8551 Result:
8552  the value of the Laguerre polynomial Ln at x
8553 *************************************************************************/
8554 double laguerrecalculate(ae_int_t n, double x, ae_state *_state)
8555 {
8556  double a;
8557  double b;
8558  double i;
8559  double result;
8560 
8561 
8562  result = 1;
8563  a = 1;
8564  b = 1-x;
8565  if( n==1 )
8566  {
8567  result = b;
8568  }
8569  i = 2;
8570  while(ae_fp_less_eq(i,n))
8571  {
8572  result = ((2*i-1-x)*b-(i-1)*a)/i;
8573  a = b;
8574  b = result;
8575  i = i+1;
8576  }
8577  return result;
8578 }
8579 
8580 
8581 /*************************************************************************
8582 Summation of Laguerre polynomials using Clenshaw’s recurrence formula.
8583 
8584 This routine calculates c[0]*L0(x) + c[1]*L1(x) + ... + c[N]*LN(x)
8585 
8586 Parameters:
8587  n - degree, n>=0
8588  x - argument
8589 
8590 Result:
8591  the value of the Laguerre polynomial at x
8592 *************************************************************************/
8593 double laguerresum(/* Real */ ae_vector* c,
8594  ae_int_t n,
8595  double x,
8596  ae_state *_state)
8597 {
8598  double b1;
8599  double b2;
8600  ae_int_t i;
8601  double result;
8602 
8603 
8604  b1 = 0;
8605  b2 = 0;
8606  result = 0;
8607  for(i=n; i>=0; i--)
8608  {
8609  result = (2*i+1-x)*b1/(i+1)-(i+1)*b2/(i+2)+c->ptr.p_double[i];
8610  b2 = b1;
8611  b1 = result;
8612  }
8613  return result;
8614 }
8615 
8616 
8617 /*************************************************************************
8618 Representation of Ln as C[0] + C[1]*X + ... + C[N]*X^N
8619 
8620 Input parameters:
8621  N - polynomial degree, n>=0
8622 
8623 Output parameters:
8624  C - coefficients
8625 *************************************************************************/
8627  /* Real */ ae_vector* c,
8628  ae_state *_state)
8629 {
8630  ae_int_t i;
8631 
8632  ae_vector_clear(c);
8633 
8634  ae_vector_set_length(c, n+1, _state);
8635  c->ptr.p_double[0] = 1;
8636  for(i=0; i<=n-1; i++)
8637  {
8638  c->ptr.p_double[i+1] = -c->ptr.p_double[i]*(n-i)/(i+1)/(i+1);
8639  }
8640 }
8641 
8642 
8643 
8644 
8645 /*************************************************************************
8646 Calculation of the value of the Legendre polynomial Pn.
8647 
8648 Parameters:
8649  n - degree, n>=0
8650  x - argument
8651 
8652 Result:
8653  the value of the Legendre polynomial Pn at x
8654 *************************************************************************/
8655 double legendrecalculate(ae_int_t n, double x, ae_state *_state)
8656 {
8657  double a;
8658  double b;
8659  ae_int_t i;
8660  double result;
8661 
8662 
8663  result = 1;
8664  a = 1;
8665  b = x;
8666  if( n==0 )
8667  {
8668  result = a;
8669  return result;
8670  }
8671  if( n==1 )
8672  {
8673  result = b;
8674  return result;
8675  }
8676  for(i=2; i<=n; i++)
8677  {
8678  result = ((2*i-1)*x*b-(i-1)*a)/i;
8679  a = b;
8680  b = result;
8681  }
8682  return result;
8683 }
8684 
8685 
8686 /*************************************************************************
8687 Summation of Legendre polynomials using Clenshaw’s recurrence formula.
8688 
8689 This routine calculates
8690  c[0]*P0(x) + c[1]*P1(x) + ... + c[N]*PN(x)
8691 
8692 Parameters:
8693  n - degree, n>=0
8694  x - argument
8695 
8696 Result:
8697  the value of the Legendre polynomial at x
8698 *************************************************************************/
8699 double legendresum(/* Real */ ae_vector* c,
8700  ae_int_t n,
8701  double x,
8702  ae_state *_state)
8703 {
8704  double b1;
8705  double b2;
8706  ae_int_t i;
8707  double result;
8708 
8709 
8710  b1 = 0;
8711  b2 = 0;
8712  result = 0;
8713  for(i=n; i>=0; i--)
8714  {
8715  result = (2*i+1)*x*b1/(i+1)-(i+1)*b2/(i+2)+c->ptr.p_double[i];
8716  b2 = b1;
8717  b1 = result;
8718  }
8719  return result;
8720 }
8721 
8722 
8723 /*************************************************************************
8724 Representation of Pn as C[0] + C[1]*X + ... + C[N]*X^N
8725 
8726 Input parameters:
8727  N - polynomial degree, n>=0
8728 
8729 Output parameters:
8730  C - coefficients
8731 *************************************************************************/
8733  /* Real */ ae_vector* c,
8734  ae_state *_state)
8735 {
8736  ae_int_t i;
8737 
8738  ae_vector_clear(c);
8739 
8740  ae_vector_set_length(c, n+1, _state);
8741  for(i=0; i<=n; i++)
8742  {
8743  c->ptr.p_double[i] = 0;
8744  }
8745  c->ptr.p_double[n] = 1;
8746  for(i=1; i<=n; i++)
8747  {
8748  c->ptr.p_double[n] = c->ptr.p_double[n]*(n+i)/2/i;
8749  }
8750  for(i=0; i<=n/2-1; i++)
8751  {
8752  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);
8753  }
8754 }
8755 
8756 
8757 
8758 
8759 /*************************************************************************
8760 Poisson distribution
8761 
8762 Returns the sum of the first k+1 terms of the Poisson
8763 distribution:
8764 
8765  k j
8766  -- -m m
8767  > e --
8768  -- j!
8769  j=0
8770 
8771 The terms are not summed directly; instead the incomplete
8772 gamma integral is employed, according to the relation
8773 
8774 y = pdtr( k, m ) = igamc( k+1, m ).
8775 
8776 The arguments must both be positive.
8777 ACCURACY:
8778 
8779 See incomplete gamma function
8780 
8781 Cephes Math Library Release 2.8: June, 2000
8782 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8783 *************************************************************************/
8784 double poissondistribution(ae_int_t k, double m, ae_state *_state)
8785 {
8786  double result;
8787 
8788 
8789  ae_assert(k>=0&&ae_fp_greater(m,0), "Domain error in PoissonDistribution", _state);
8790  result = incompletegammac(k+1, m, _state);
8791  return result;
8792 }
8793 
8794 
8795 /*************************************************************************
8796 Complemented Poisson distribution
8797 
8798 Returns the sum of the terms k+1 to infinity of the Poisson
8799 distribution:
8800 
8801  inf. j
8802  -- -m m
8803  > e --
8804  -- j!
8805  j=k+1
8806 
8807 The terms are not summed directly; instead the incomplete
8808 gamma integral is employed, according to the formula
8809 
8810 y = pdtrc( k, m ) = igam( k+1, m ).
8811 
8812 The arguments must both be positive.
8813 
8814 ACCURACY:
8815 
8816 See incomplete gamma function
8817 
8818 Cephes Math Library Release 2.8: June, 2000
8819 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8820 *************************************************************************/
8821 double poissoncdistribution(ae_int_t k, double m, ae_state *_state)
8822 {
8823  double result;
8824 
8825 
8826  ae_assert(k>=0&&ae_fp_greater(m,0), "Domain error in PoissonDistributionC", _state);
8827  result = incompletegamma(k+1, m, _state);
8828  return result;
8829 }
8830 
8831 
8832 /*************************************************************************
8833 Inverse Poisson distribution
8834 
8835 Finds the Poisson variable x such that the integral
8836 from 0 to x of the Poisson density is equal to the
8837 given probability y.
8838 
8839 This is accomplished using the inverse gamma integral
8840 function and the relation
8841 
8842  m = igami( k+1, y ).
8843 
8844 ACCURACY:
8845 
8846 See inverse incomplete gamma function
8847 
8848 Cephes Math Library Release 2.8: June, 2000
8849 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
8850 *************************************************************************/
8851 double invpoissondistribution(ae_int_t k, double y, ae_state *_state)
8852 {
8853  double result;
8854 
8855 
8856  ae_assert((k>=0&&ae_fp_greater_eq(y,0))&&ae_fp_less(y,1), "Domain error in InvPoissonDistribution", _state);
8857  result = invincompletegammac(k+1, y, _state);
8858  return result;
8859 }
8860 
8861 
8862 
8863 
8864 /*************************************************************************
8865 Psi (digamma) function
8866 
8867  d -
8868  psi(x) = -- ln | (x)
8869  dx
8870 
8871 is the logarithmic derivative of the gamma function.
8872 For integer x,
8873  n-1
8874  -
8875 psi(n) = -EUL + > 1/k.
8876  -
8877  k=1
8878 
8879 This formula is used for 0 < n <= 10. If x is negative, it
8880 is transformed to a positive argument by the reflection
8881 formula psi(1-x) = psi(x) + pi cot(pi x).
8882 For general positive x, the argument is made greater than 10
8883 using the recurrence psi(x+1) = psi(x) + 1/x.
8884 Then the following asymptotic expansion is applied:
8885 
8886  inf. B
8887  - 2k
8888 psi(x) = log(x) - 1/2x - > -------
8889  - 2k
8890  k=1 2k x
8891 
8892 where the B2k are Bernoulli numbers.
8893 
8894 ACCURACY:
8895  Relative error (except absolute when |psi| < 1):
8896 arithmetic domain # trials peak rms
8897  IEEE 0,30 30000 1.3e-15 1.4e-16
8898  IEEE -30,0 40000 1.5e-15 2.2e-16
8899 
8900 Cephes Math Library Release 2.8: June, 2000
8901 Copyright 1984, 1987, 1992, 2000 by Stephen L. Moshier
8902 *************************************************************************/
8903 double psi(double x, ae_state *_state)
8904 {
8905  double p;
8906  double q;
8907  double nz;
8908  double s;
8909  double w;
8910  double y;
8911  double z;
8912  double polv;
8913  ae_int_t i;
8914  ae_int_t n;
8915  ae_int_t negative;
8916  double result;
8917 
8918 
8919  negative = 0;
8920  nz = 0.0;
8921  if( ae_fp_less_eq(x,0) )
8922  {
8923  negative = 1;
8924  q = x;
8925  p = ae_ifloor(q, _state);
8926  if( ae_fp_eq(p,q) )
8927  {
8928  ae_assert(ae_false, "Singularity in Psi(x)", _state);
8929  result = ae_maxrealnumber;
8930  return result;
8931  }
8932  nz = q-p;
8933  if( ae_fp_neq(nz,0.5) )
8934  {
8935  if( ae_fp_greater(nz,0.5) )
8936  {
8937  p = p+1.0;
8938  nz = q-p;
8939  }
8940  nz = ae_pi/ae_tan(ae_pi*nz, _state);
8941  }
8942  else
8943  {
8944  nz = 0.0;
8945  }
8946  x = 1.0-x;
8947  }
8948  if( ae_fp_less_eq(x,10.0)&&ae_fp_eq(x,ae_ifloor(x, _state)) )
8949  {
8950  y = 0.0;
8951  n = ae_ifloor(x, _state);
8952  for(i=1; i<=n-1; i++)
8953  {
8954  w = i;
8955  y = y+1.0/w;
8956  }
8957  y = y-0.57721566490153286061;
8958  }
8959  else
8960  {
8961  s = x;
8962  w = 0.0;
8963  while(ae_fp_less(s,10.0))
8964  {
8965  w = w+1.0/s;
8966  s = s+1.0;
8967  }
8968  if( ae_fp_less(s,1.0E17) )
8969  {
8970  z = 1.0/(s*s);
8971  polv = 8.33333333333333333333E-2;
8972  polv = polv*z-2.10927960927960927961E-2;
8973  polv = polv*z+7.57575757575757575758E-3;
8974  polv = polv*z-4.16666666666666666667E-3;
8975  polv = polv*z+3.96825396825396825397E-3;
8976  polv = polv*z-8.33333333333333333333E-3;
8977  polv = polv*z+8.33333333333333333333E-2;
8978  y = z*polv;
8979  }
8980  else
8981  {
8982  y = 0.0;
8983  }
8984  y = ae_log(s, _state)-0.5/s-y-w;
8985  }
8986  if( negative!=0 )
8987  {
8988  y = y-nz;
8989  }
8990  result = y;
8991  return result;
8992 }
8993 
8994 
8995 
8996 
8997 /*************************************************************************
8998 Student's t distribution
8999 
9000 Computes the integral from minus infinity to t of the Student
9001 t distribution with integer k > 0 degrees of freedom:
9002 
9003  t
9004  -
9005  | |
9006  - | 2 -(k+1)/2
9007  | ( (k+1)/2 ) | ( x )
9008  ---------------------- | ( 1 + --- ) dx
9009  - | ( k )
9010  sqrt( k pi ) | ( k/2 ) |
9011  | |
9012  -
9013  -inf.
9014 
9015 Relation to incomplete beta integral:
9016 
9017  1 - stdtr(k,t) = 0.5 * incbet( k/2, 1/2, z )
9018 where
9019  z = k/(k + t**2).
9020 
9021 For t < -2, this is the method of computation. For higher t,
9022 a direct method is derived from integration by parts.
9023 Since the function is symmetric about t=0, the area under the
9024 right tail of the density is found by calling the function
9025 with -t instead of t.
9026 
9027 ACCURACY:
9028 
9029 Tested at random 1 <= k <= 25. The "domain" refers to t.
9030  Relative error:
9031 arithmetic domain # trials peak rms
9032  IEEE -100,-2 50000 5.9e-15 1.4e-15
9033  IEEE -2,100 500000 2.7e-15 4.9e-17
9034 
9035 Cephes Math Library Release 2.8: June, 2000
9036 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
9037 *************************************************************************/
9038 double studenttdistribution(ae_int_t k, double t, ae_state *_state)
9039 {
9040  double x;
9041  double rk;
9042  double z;
9043  double f;
9044  double tz;
9045  double p;
9046  double xsqk;
9047  ae_int_t j;
9048  double result;
9049 
9050 
9051  ae_assert(k>0, "Domain error in StudentTDistribution", _state);
9052  if( ae_fp_eq(t,0) )
9053  {
9054  result = 0.5;
9055  return result;
9056  }
9057  if( ae_fp_less(t,-2.0) )
9058  {
9059  rk = k;
9060  z = rk/(rk+t*t);
9061  result = 0.5*incompletebeta(0.5*rk, 0.5, z, _state);
9062  return result;
9063  }
9064  if( ae_fp_less(t,0) )
9065  {
9066  x = -t;
9067  }
9068  else
9069  {
9070  x = t;
9071  }
9072  rk = k;
9073  z = 1.0+x*x/rk;
9074  if( k%2!=0 )
9075  {
9076  xsqk = x/ae_sqrt(rk, _state);
9077  p = ae_atan(xsqk, _state);
9078  if( k>1 )
9079  {
9080  f = 1.0;
9081  tz = 1.0;
9082  j = 3;
9083  while(j<=k-2&&ae_fp_greater(tz/f,ae_machineepsilon))
9084  {
9085  tz = tz*((j-1)/(z*j));
9086  f = f+tz;
9087  j = j+2;
9088  }
9089  p = p+f*xsqk/z;
9090  }
9091  p = p*2.0/ae_pi;
9092  }
9093  else
9094  {
9095  f = 1.0;
9096  tz = 1.0;
9097  j = 2;
9098  while(j<=k-2&&ae_fp_greater(tz/f,ae_machineepsilon))
9099  {
9100  tz = tz*((j-1)/(z*j));
9101  f = f+tz;
9102  j = j+2;
9103  }
9104  p = f*x/ae_sqrt(z*rk, _state);
9105  }
9106  if( ae_fp_less(t,0) )
9107  {
9108  p = -p;
9109  }
9110  result = 0.5+0.5*p;
9111  return result;
9112 }
9113 
9114 
9115 /*************************************************************************
9116 Functional inverse of Student's t distribution
9117 
9118 Given probability p, finds the argument t such that stdtr(k,t)
9119 is equal to p.
9120 
9121 ACCURACY:
9122 
9123 Tested at random 1 <= k <= 100. The "domain" refers to p:
9124  Relative error:
9125 arithmetic domain # trials peak rms
9126  IEEE .001,.999 25000 5.7e-15 8.0e-16
9127  IEEE 10^-6,.001 25000 2.0e-12 2.9e-14
9128 
9129 Cephes Math Library Release 2.8: June, 2000
9130 Copyright 1984, 1987, 1995, 2000 by Stephen L. Moshier
9131 *************************************************************************/
9132 double invstudenttdistribution(ae_int_t k, double p, ae_state *_state)
9133 {
9134  double t;
9135  double rk;
9136  double z;
9137  ae_int_t rflg;
9138  double result;
9139 
9140 
9141  ae_assert((k>0&&ae_fp_greater(p,0))&&ae_fp_less(p,1), "Domain error in InvStudentTDistribution", _state);
9142  rk = k;
9143  if( ae_fp_greater(p,0.25)&&ae_fp_less(p,0.75) )
9144  {
9145  if( ae_fp_eq(p,0.5) )
9146  {
9147  result = 0;
9148  return result;
9149  }
9150  z = 1.0-2.0*p;
9151  z = invincompletebeta(0.5, 0.5*rk, ae_fabs(z, _state), _state);
9152  t = ae_sqrt(rk*z/(1.0-z), _state);
9153  if( ae_fp_less(p,0.5) )
9154  {
9155  t = -t;
9156  }
9157  result = t;
9158  return result;
9159  }
9160  rflg = -1;
9161  if( ae_fp_greater_eq(p,0.5) )
9162  {
9163  p = 1.0-p;
9164  rflg = 1;
9165  }
9166  z = invincompletebeta(0.5*rk, 0.5, 2.0*p, _state);
9167  if( ae_fp_less(ae_maxrealnumber*z,rk) )
9168  {
9169  result = rflg*ae_maxrealnumber;
9170  return result;
9171  }
9172  t = ae_sqrt(rk/z-rk, _state);
9173  result = rflg*t;
9174  return result;
9175 }
9176 
9177 
9178 
9179 
9180 /*************************************************************************
9181 Sine and cosine integrals
9182 
9183 Evaluates the integrals
9184 
9185  x
9186  -
9187  | cos t - 1
9188  Ci(x) = eul + ln x + | --------- dt,
9189  | t
9190  -
9191  0
9192  x
9193  -
9194  | sin t
9195  Si(x) = | ----- dt
9196  | t
9197  -
9198  0
9199 
9200 where eul = 0.57721566490153286061 is Euler's constant.
9201 The integrals are approximated by rational functions.
9202 For x > 8 auxiliary functions f(x) and g(x) are employed
9203 such that
9204 
9205 Ci(x) = f(x) sin(x) - g(x) cos(x)
9206 Si(x) = pi/2 - f(x) cos(x) - g(x) sin(x)
9207 
9208 
9209 ACCURACY:
9210  Test interval = [0,50].
9211 Absolute error, except relative when > 1:
9212 arithmetic function # trials peak rms
9213  IEEE Si 30000 4.4e-16 7.3e-17
9214  IEEE Ci 30000 6.9e-16 5.1e-17
9215 
9216 Cephes Math Library Release 2.1: January, 1989
9217 Copyright 1984, 1987, 1989 by Stephen L. Moshier
9218 *************************************************************************/
9219 void sinecosineintegrals(double x,
9220  double* si,
9221  double* ci,
9222  ae_state *_state)
9223 {
9224  double z;
9225  double c;
9226  double s;
9227  double f;
9228  double g;
9229  ae_int_t sg;
9230  double sn;
9231  double sd;
9232  double cn;
9233  double cd;
9234  double fn;
9235  double fd;
9236  double gn;
9237  double gd;
9238 
9239  *si = 0;
9240  *ci = 0;
9241 
9242  if( ae_fp_less(x,0) )
9243  {
9244  sg = -1;
9245  x = -x;
9246  }
9247  else
9248  {
9249  sg = 0;
9250  }
9251  if( ae_fp_eq(x,0) )
9252  {
9253  *si = 0;
9254  *ci = -ae_maxrealnumber;
9255  return;
9256  }
9257  if( ae_fp_greater(x,1.0E9) )
9258  {
9259  *si = 1.570796326794896619-ae_cos(x, _state)/x;
9260  *ci = ae_sin(x, _state)/x;
9261  return;
9262  }
9263  if( ae_fp_less_eq(x,4) )
9264  {
9265  z = x*x;
9266  sn = -8.39167827910303881427E-11;
9267  sn = sn*z+4.62591714427012837309E-8;
9268  sn = sn*z-9.75759303843632795789E-6;
9269  sn = sn*z+9.76945438170435310816E-4;
9270  sn = sn*z-4.13470316229406538752E-2;
9271  sn = sn*z+1.00000000000000000302E0;
9272  sd = 2.03269266195951942049E-12;
9273  sd = sd*z+1.27997891179943299903E-9;
9274  sd = sd*z+4.41827842801218905784E-7;
9275  sd = sd*z+9.96412122043875552487E-5;
9276  sd = sd*z+1.42085239326149893930E-2;
9277  sd = sd*z+9.99999999999999996984E-1;
9278  s = x*sn/sd;
9279  cn = 2.02524002389102268789E-11;
9280  cn = cn*z-1.35249504915790756375E-8;
9281  cn = cn*z+3.59325051419993077021E-6;
9282  cn = cn*z-4.74007206873407909465E-4;
9283  cn = cn*z+2.89159652607555242092E-2;
9284  cn = cn*z-1.00000000000000000080E0;
9285  cd = 4.07746040061880559506E-12;
9286  cd = cd*z+3.06780997581887812692E-9;
9287  cd = cd*z+1.23210355685883423679E-6;
9288  cd = cd*z+3.17442024775032769882E-4;
9289  cd = cd*z+5.10028056236446052392E-2;
9290  cd = cd*z+4.00000000000000000080E0;
9291  c = z*cn/cd;
9292  if( sg!=0 )
9293  {
9294  s = -s;
9295  }
9296  *si = s;
9297  *ci = 0.57721566490153286061+ae_log(x, _state)+c;
9298  return;
9299  }
9300  s = ae_sin(x, _state);
9301  c = ae_cos(x, _state);
9302  z = 1.0/(x*x);
9303  if( ae_fp_less(x,8) )
9304  {
9305  fn = 4.23612862892216586994E0;
9306  fn = fn*z+5.45937717161812843388E0;
9307  fn = fn*z+1.62083287701538329132E0;
9308  fn = fn*z+1.67006611831323023771E-1;
9309  fn = fn*z+6.81020132472518137426E-3;
9310  fn = fn*z+1.08936580650328664411E-4;
9311  fn = fn*z+5.48900223421373614008E-7;
9312  fd = 1.00000000000000000000E0;
9313  fd = fd*z+8.16496634205391016773E0;
9314  fd = fd*z+7.30828822505564552187E0;
9315  fd = fd*z+1.86792257950184183883E0;
9316  fd = fd*z+1.78792052963149907262E-1;
9317  fd = fd*z+7.01710668322789753610E-3;
9318  fd = fd*z+1.10034357153915731354E-4;
9319  fd = fd*z+5.48900252756255700982E-7;
9320  f = fn/(x*fd);
9321  gn = 8.71001698973114191777E-2;
9322  gn = gn*z+6.11379109952219284151E-1;
9323  gn = gn*z+3.97180296392337498885E-1;
9324  gn = gn*z+7.48527737628469092119E-2;
9325  gn = gn*z+5.38868681462177273157E-3;
9326  gn = gn*z+1.61999794598934024525E-4;
9327  gn = gn*z+1.97963874140963632189E-6;
9328  gn = gn*z+7.82579040744090311069E-9;
9329  gd = 1.00000000000000000000E0;
9330  gd = gd*z+1.64402202413355338886E0;
9331  gd = gd*z+6.66296701268987968381E-1;
9332  gd = gd*z+9.88771761277688796203E-2;
9333  gd = gd*z+6.22396345441768420760E-3;
9334  gd = gd*z+1.73221081474177119497E-4;
9335  gd = gd*z+2.02659182086343991969E-6;
9336  gd = gd*z+7.82579218933534490868E-9;
9337  g = z*gn/gd;
9338  }
9339  else
9340  {
9341  fn = 4.55880873470465315206E-1;
9342  fn = fn*z+7.13715274100146711374E-1;
9343  fn = fn*z+1.60300158222319456320E-1;
9344  fn = fn*z+1.16064229408124407915E-2;
9345  fn = fn*z+3.49556442447859055605E-4;
9346  fn = fn*z+4.86215430826454749482E-6;
9347  fn = fn*z+3.20092790091004902806E-8;
9348  fn = fn*z+9.41779576128512936592E-11;
9349  fn = fn*z+9.70507110881952024631E-14;
9350  fd = 1.00000000000000000000E0;
9351  fd = fd*z+9.17463611873684053703E-1;
9352  fd = fd*z+1.78685545332074536321E-1;
9353  fd = fd*z+1.22253594771971293032E-2;
9354  fd = fd*z+3.58696481881851580297E-4;
9355  fd = fd*z+4.92435064317881464393E-6;
9356  fd = fd*z+3.21956939101046018377E-8;
9357  fd = fd*z+9.43720590350276732376E-11;
9358  fd = fd*z+9.70507110881952025725E-14;
9359  f = fn/(x*fd);
9360  gn = 6.97359953443276214934E-1;
9361  gn = gn*z+3.30410979305632063225E-1;
9362  gn = gn*z+3.84878767649974295920E-2;
9363  gn = gn*z+1.71718239052347903558E-3;
9364  gn = gn*z+3.48941165502279436777E-5;
9365  gn = gn*z+3.47131167084116673800E-7;
9366  gn = gn*z+1.70404452782044526189E-9;
9367  gn = gn*z+3.85945925430276600453E-12;
9368  gn = gn*z+3.14040098946363334640E-15;
9369  gd = 1.00000000000000000000E0;
9370  gd = gd*z+1.68548898811011640017E0;
9371  gd = gd*z+4.87852258695304967486E-1;
9372  gd = gd*z+4.67913194259625806320E-2;
9373  gd = gd*z+1.90284426674399523638E-3;
9374  gd = gd*z+3.68475504442561108162E-5;
9375  gd = gd*z+3.57043223443740838771E-7;
9376  gd = gd*z+1.72693748966316146736E-9;
9377  gd = gd*z+3.87830166023954706752E-12;
9378  gd = gd*z+3.14040098946363335242E-15;
9379  g = z*gn/gd;
9380  }
9381  *si = 1.570796326794896619-f*c-g*s;
9382  if( sg!=0 )
9383  {
9384  *si = -*si;
9385  }
9386  *ci = f*s-g*c;
9387 }
9388 
9389 
9390 /*************************************************************************
9391 Hyperbolic sine and cosine integrals
9392 
9393 Approximates the integrals
9394 
9395  x
9396  -
9397  | | cosh t - 1
9398  Chi(x) = eul + ln x + | ----------- dt,
9399  | | t
9400  -
9401  0
9402 
9403  x
9404  -
9405  | | sinh t
9406  Shi(x) = | ------ dt
9407  | | t
9408  -
9409  0
9410 
9411 where eul = 0.57721566490153286061 is Euler's constant.
9412 The integrals are evaluated by power series for x < 8
9413 and by Chebyshev expansions for x between 8 and 88.
9414 For large x, both functions approach exp(x)/2x.
9415 Arguments greater than 88 in magnitude return MAXNUM.
9416 
9417 
9418 ACCURACY:
9419 
9420 Test interval 0 to 88.
9421  Relative error:
9422 arithmetic function # trials peak rms
9423  IEEE Shi 30000 6.9e-16 1.6e-16
9424  Absolute error, except relative when |Chi| > 1:
9425  IEEE Chi 30000 8.4e-16 1.4e-16
9426 
9427 Cephes Math Library Release 2.8: June, 2000
9428 Copyright 1984, 1987, 2000 by Stephen L. Moshier
9429 *************************************************************************/
9431  double* shi,
9432  double* chi,
9433  ae_state *_state)
9434 {
9435  double k;
9436  double z;
9437  double c;
9438  double s;
9439  double a;
9440  ae_int_t sg;
9441  double b0;
9442  double b1;
9443  double b2;
9444 
9445  *shi = 0;
9446  *chi = 0;
9447 
9448  if( ae_fp_less(x,0) )
9449  {
9450  sg = -1;
9451  x = -x;
9452  }
9453  else
9454  {
9455  sg = 0;
9456  }
9457  if( ae_fp_eq(x,0) )
9458  {
9459  *shi = 0;
9460  *chi = -ae_maxrealnumber;
9461  return;
9462  }
9463  if( ae_fp_less(x,8.0) )
9464  {
9465  z = x*x;
9466  a = 1.0;
9467  s = 1.0;
9468  c = 0.0;
9469  k = 2.0;
9470  do
9471  {
9472  a = a*z/k;
9473  c = c+a/k;
9474  k = k+1.0;
9475  a = a/k;
9476  s = s+a/k;
9477  k = k+1.0;
9478  }
9479  while(ae_fp_greater_eq(ae_fabs(a/s, _state),ae_machineepsilon));
9480  s = s*x;
9481  }
9482  else
9483  {
9484  if( ae_fp_less(x,18.0) )
9485  {
9486  a = (576.0/x-52.0)/10.0;
9487  k = ae_exp(x, _state)/x;
9488  b0 = 1.83889230173399459482E-17;
9489  b1 = 0.0;
9490  trigintegrals_chebiterationshichi(a, -9.55485532279655569575E-17, &b0, &b1, &b2, _state);
9491  trigintegrals_chebiterationshichi(a, 2.04326105980879882648E-16, &b0, &b1, &b2, _state);
9492  trigintegrals_chebiterationshichi(a, 1.09896949074905343022E-15, &b0, &b1, &b2, _state);
9493  trigintegrals_chebiterationshichi(a, -1.31313534344092599234E-14, &b0, &b1, &b2, _state);
9494  trigintegrals_chebiterationshichi(a, 5.93976226264314278932E-14, &b0, &b1, &b2, _state);
9495  trigintegrals_chebiterationshichi(a, -3.47197010497749154755E-14, &b0, &b1, &b2, _state);
9496  trigintegrals_chebiterationshichi(a, -1.40059764613117131000E-12, &b0, &b1, &b2, _state);
9497  trigintegrals_chebiterationshichi(a, 9.49044626224223543299E-12, &b0, &b1, &b2, _state);
9498  trigintegrals_chebiterationshichi(a, -1.61596181145435454033E-11, &b0, &b1, &b2, _state);
9499  trigintegrals_chebiterationshichi(a, -1.77899784436430310321E-10, &b0, &b1, &b2, _state);
9500  trigintegrals_chebiterationshichi(a, 1.35455469767246947469E-9, &b0, &b1, &b2, _state);
9501  trigintegrals_chebiterationshichi(a, -1.03257121792819495123E-9, &b0, &b1, &b2, _state);
9502  trigintegrals_chebiterationshichi(a, -3.56699611114982536845E-8, &b0, &b1, &b2, _state);
9503  trigintegrals_chebiterationshichi(a, 1.44818877384267342057E-7, &b0, &b1, &b2, _state);
9504  trigintegrals_chebiterationshichi(a, 7.82018215184051295296E-7, &b0, &b1, &b2, _state);
9505  trigintegrals_chebiterationshichi(a, -5.39919118403805073710E-6, &b0, &b1, &b2, _state);
9506  trigintegrals_chebiterationshichi(a, -3.12458202168959833422E-5, &b0, &b1, &b2, _state);
9507  trigintegrals_chebiterationshichi(a, 8.90136741950727517826E-5, &b0, &b1, &b2, _state);
9508  trigintegrals_chebiterationshichi(a, 2.02558474743846862168E-3, &b0, &b1, &b2, _state);
9509  trigintegrals_chebiterationshichi(a, 2.96064440855633256972E-2, &b0, &b1, &b2, _state);
9510  trigintegrals_chebiterationshichi(a, 1.11847751047257036625E0, &b0, &b1, &b2, _state);
9511  s = k*0.5*(b0-b2);
9512  b0 = -8.12435385225864036372E-18;
9513  b1 = 0.0;
9514  trigintegrals_chebiterationshichi(a, 2.17586413290339214377E-17, &b0, &b1, &b2, _state);
9515  trigintegrals_chebiterationshichi(a, 5.22624394924072204667E-17, &b0, &b1, &b2, _state);
9516  trigintegrals_chebiterationshichi(a, -9.48812110591690559363E-16, &b0, &b1, &b2, _state);
9517  trigintegrals_chebiterationshichi(a, 5.35546311647465209166E-15, &b0, &b1, &b2, _state);
9518  trigintegrals_chebiterationshichi(a, -1.21009970113732918701E-14, &b0, &b1, &b2, _state);
9519  trigintegrals_chebiterationshichi(a, -6.00865178553447437951E-14, &b0, &b1, &b2, _state);
9520  trigintegrals_chebiterationshichi(a, 7.16339649156028587775E-13, &b0, &b1, &b2, _state);
9521  trigintegrals_chebiterationshichi(a, -2.93496072607599856104E-12, &b0, &b1, &b2, _state);
9522  trigintegrals_chebiterationshichi(a, -1.40359438136491256904E-12, &b0, &b1, &b2, _state);
9523  trigintegrals_chebiterationshichi(a, 8.76302288609054966081E-11, &b0, &b1, &b2, _state);
9524  trigintegrals_chebiterationshichi(a, -4.40092476213282340617E-10, &b0, &b1, &b2, _state);
9525  trigintegrals_chebiterationshichi(a, -1.87992075640569295479E-10, &b0, &b1, &b2, _state);
9526  trigintegrals_chebiterationshichi(a, 1.31458150989474594064E-8, &b0, &b1, &b2, _state);
9527  trigintegrals_chebiterationshichi(a, -4.75513930924765465590E-8, &b0, &b1, &b2, _state);
9528  trigintegrals_chebiterationshichi(a, -2.21775018801848880741E-7, &b0, &b1, &b2, _state);
9529  trigintegrals_chebiterationshichi(a, 1.94635531373272490962E-6, &b0, &b1, &b2, _state);
9530  trigintegrals_chebiterationshichi(a, 4.33505889257316408893E-6, &b0, &b1, &b2, _state);
9531  trigintegrals_chebiterationshichi(a, -6.13387001076494349496E-5, &b0, &b1, &b2, _state);
9532  trigintegrals_chebiterationshichi(a, -3.13085477492997465138E-4, &b0, &b1, &b2, _state);
9533  trigintegrals_chebiterationshichi(a, 4.97164789823116062801E-4, &b0, &b1, &b2, _state);
9534  trigintegrals_chebiterationshichi(a, 2.64347496031374526641E-2, &b0, &b1, &b2, _state);
9535  trigintegrals_chebiterationshichi(a, 1.11446150876699213025E0, &b0, &b1, &b2, _state);
9536  c = k*0.5*(b0-b2);
9537  }
9538  else
9539  {
9540  if( ae_fp_less_eq(x,88.0) )
9541  {
9542  a = (6336.0/x-212.0)/70.0;
9543  k = ae_exp(x, _state)/x;
9544  b0 = -1.05311574154850938805E-17;
9545  b1 = 0.0;
9546  trigintegrals_chebiterationshichi(a, 2.62446095596355225821E-17, &b0, &b1, &b2, _state);
9547  trigintegrals_chebiterationshichi(a, 8.82090135625368160657E-17, &b0, &b1, &b2, _state);
9548  trigintegrals_chebiterationshichi(a, -3.38459811878103047136E-16, &b0, &b1, &b2, _state);
9549  trigintegrals_chebiterationshichi(a, -8.30608026366935789136E-16, &b0, &b1, &b2, _state);
9550  trigintegrals_chebiterationshichi(a, 3.93397875437050071776E-15, &b0, &b1, &b2, _state);
9551  trigintegrals_chebiterationshichi(a, 1.01765565969729044505E-14, &b0, &b1, &b2, _state);
9552  trigintegrals_chebiterationshichi(a, -4.21128170307640802703E-14, &b0, &b1, &b2, _state);
9553  trigintegrals_chebiterationshichi(a, -1.60818204519802480035E-13, &b0, &b1, &b2, _state);
9554  trigintegrals_chebiterationshichi(a, 3.34714954175994481761E-13, &b0, &b1, &b2, _state);
9555  trigintegrals_chebiterationshichi(a, 2.72600352129153073807E-12, &b0, &b1, &b2, _state);
9556  trigintegrals_chebiterationshichi(a, 1.66894954752839083608E-12, &b0, &b1, &b2, _state);
9557  trigintegrals_chebiterationshichi(a, -3.49278141024730899554E-11, &b0, &b1, &b2, _state);
9558  trigintegrals_chebiterationshichi(a, -1.58580661666482709598E-10, &b0, &b1, &b2, _state);
9559  trigintegrals_chebiterationshichi(a, -1.79289437183355633342E-10, &b0, &b1, &b2, _state);
9560  trigintegrals_chebiterationshichi(a, 1.76281629144264523277E-9, &b0, &b1, &b2, _state);
9561  trigintegrals_chebiterationshichi(a, 1.69050228879421288846E-8, &b0, &b1, &b2, _state);
9562  trigintegrals_chebiterationshichi(a, 1.25391771228487041649E-7, &b0, &b1, &b2, _state);
9563  trigintegrals_chebiterationshichi(a, 1.16229947068677338732E-6, &b0, &b1, &b2, _state);
9564  trigintegrals_chebiterationshichi(a, 1.61038260117376323993E-5, &b0, &b1, &b2, _state);
9565  trigintegrals_chebiterationshichi(a, 3.49810375601053973070E-4, &b0, &b1, &b2, _state);
9566  trigintegrals_chebiterationshichi(a, 1.28478065259647610779E-2, &b0, &b1, &b2, _state);
9567  trigintegrals_chebiterationshichi(a, 1.03665722588798326712E0, &b0, &b1, &b2, _state);
9568  s = k*0.5*(b0-b2);
9569  b0 = 8.06913408255155572081E-18;
9570  b1 = 0.0;
9571  trigintegrals_chebiterationshichi(a, -2.08074168180148170312E-17, &b0, &b1, &b2, _state);
9572  trigintegrals_chebiterationshichi(a, -5.98111329658272336816E-17, &b0, &b1, &b2, _state);
9573  trigintegrals_chebiterationshichi(a, 2.68533951085945765591E-16, &b0, &b1, &b2, _state);
9574  trigintegrals_chebiterationshichi(a, 4.52313941698904694774E-16, &b0, &b1, &b2, _state);
9575  trigintegrals_chebiterationshichi(a, -3.10734917335299464535E-15, &b0, &b1, &b2, _state);
9576  trigintegrals_chebiterationshichi(a, -4.42823207332531972288E-15, &b0, &b1, &b2, _state);
9577  trigintegrals_chebiterationshichi(a, 3.49639695410806959872E-14, &b0, &b1, &b2, _state);
9578  trigintegrals_chebiterationshichi(a, 6.63406731718911586609E-14, &b0, &b1, &b2, _state);
9579  trigintegrals_chebiterationshichi(a, -3.71902448093119218395E-13, &b0, &b1, &b2, _state);
9580  trigintegrals_chebiterationshichi(a, -1.27135418132338309016E-12, &b0, &b1, &b2, _state);
9581  trigintegrals_chebiterationshichi(a, 2.74851141935315395333E-12, &b0, &b1, &b2, _state);
9582  trigintegrals_chebiterationshichi(a, 2.33781843985453438400E-11, &b0, &b1, &b2, _state);
9583  trigintegrals_chebiterationshichi(a, 2.71436006377612442764E-11, &b0, &b1, &b2, _state);
9584  trigintegrals_chebiterationshichi(a, -2.56600180000355990529E-10, &b0, &b1, &b2, _state);
9585  trigintegrals_chebiterationshichi(a, -1.61021375163803438552E-9, &b0, &b1, &b2, _state);
9586  trigintegrals_chebiterationshichi(a, -4.72543064876271773512E-9, &b0, &b1, &b2, _state);
9587  trigintegrals_chebiterationshichi(a, -3.00095178028681682282E-9, &b0, &b1, &b2, _state);
9588  trigintegrals_chebiterationshichi(a, 7.79387474390914922337E-8, &b0, &b1, &b2, _state);
9589  trigintegrals_chebiterationshichi(a, 1.06942765566401507066E-6, &b0, &b1, &b2, _state);
9590  trigintegrals_chebiterationshichi(a, 1.59503164802313196374E-5, &b0, &b1, &b2, _state);
9591  trigintegrals_chebiterationshichi(a, 3.49592575153777996871E-4, &b0, &b1, &b2, _state);
9592  trigintegrals_chebiterationshichi(a, 1.28475387530065247392E-2, &b0, &b1, &b2, _state);
9593  trigintegrals_chebiterationshichi(a, 1.03665693917934275131E0, &b0, &b1, &b2, _state);
9594  c = k*0.5*(b0-b2);
9595  }
9596  else
9597  {
9598  if( sg!=0 )
9599  {
9600  *shi = -ae_maxrealnumber;
9601  }
9602  else
9603  {
9604  *shi = ae_maxrealnumber;
9605  }
9606  *chi = ae_maxrealnumber;
9607  return;
9608  }
9609  }
9610  }
9611  if( sg!=0 )
9612  {
9613  s = -s;
9614  }
9615  *shi = s;
9616  *chi = 0.57721566490153286061+ae_log(x, _state)+c;
9617 }
9618 
9619 
9620 static void trigintegrals_chebiterationshichi(double x,
9621  double c,
9622  double* b0,
9623  double* b1,
9624  double* b2,
9625  ae_state *_state)
9626 {
9627 
9628 
9629  *b2 = *b1;
9630  *b1 = *b0;
9631  *b0 = x*(*b1)-(*b2)+c;
9632 }
9633 
9634 
9635 
9636 }
9637 
struct alglib_impl::ae_state ae_state
void hyperbolicsinecosineintegrals(double x, double *shi, double *chi, ae_state *_state)
double beta(double a, double b, ae_state *_state)
ae_bool ae_fp_greater_eq(double v1, double v2)
Definition: ap.cpp:1351
double incompleteellipticintegralk(double phi, double m, ae_state *_state)
double invbinomialdistribution(ae_int_t k, ae_int_t n, double y, ae_state *_state)
double poissoncdistribution(ae_int_t k, double m, ae_state *_state)
#define yc
void airy(double x, double *ai, double *aip, double *bi, double *bip, ae_state *_state)
double invfdistribution(ae_int_t a, ae_int_t b, double y, ae_state *_state)
double besselj1(double x, ae_state *_state)
double binomialcdistribution(ae_int_t k, ae_int_t n, double p, ae_state *_state)
double ae_sin(double x, ae_state *state)
Definition: ap.cpp:1630
double bessely1(double x, ae_state *_state)
double ae_fabs(double x, ae_state *state)
Definition: ap.cpp:1520
double errorfunction(double x, ae_state *_state)
double ae_tan(double x, ae_state *state)
Definition: ap.cpp:1640
double chebyshevsum(ae_vector *c, ae_int_t r, ae_int_t n, double x, ae_state *_state)
doublereal * c
doublereal * g
double ae_pow(double x, double y, ae_state *state)
Definition: ap.cpp:1684
void * cd
#define ae_false
Definition: ap.h:196
#define pp(s, x)
Definition: ml2d.cpp:473
#define q0
void ae_frame_make(ae_state *state, ae_frame *tmp)
Definition: ap.cpp:402
void fresnelintegral(double x, double *c, double *s, ae_state *_state)
static double * y
void fromchebyshev(ae_vector *a, ae_int_t n, ae_vector *b, ae_state *_state)
void jacobianellipticfunctions(double u, double m, double *sn, double *cn, double *dn, double *ph, ae_state *_state)
double psi(double x, ae_state *_state)
void hermitecoefficients(ae_int_t n, ae_vector *c, ae_state *_state)
double invincompletegammac(double a, double y0, ae_state *_state)
double besseli0(double x, ae_state *_state)
ae_int_t ae_sign(double x, ae_state *state)
Definition: ap.cpp:1540
doublereal * w
double * p_double
Definition: ap.h:437
double normaldistribution(double x, ae_state *_state)
#define z0
void ae_state_clear(ae_state *state)
Definition: ap.cpp:373
double hermitecalculate(ae_int_t n, double x, ae_state *_state)
ae_bool ae_fp_eq(double v1, double v2)
Definition: ap.cpp:1313
double exponentialintegralen(double x, ae_int_t n, ae_state *_state)
#define q1
double fcdistribution(ae_int_t a, ae_int_t b, double x, ae_state *_state)
double * di
double incompletebeta(double a, double b, double x, ae_state *_state)
double ae_cos(double x, ae_state *state)
Definition: ap.cpp:1635
doublereal * x
#define i
#define ae_pi
Definition: ap.h:828
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
doublereal * d
double theta
double studenttdistribution(ae_int_t k, double t, ae_state *_state)
void laguerrecoefficients(ae_int_t n, ae_vector *c, ae_state *_state)
double ae_asin(double x, ae_state *state)
Definition: ap.cpp:1659
double chisquaredistribution(double v, double x, ae_state *_state)
double chisquarecdistribution(double v, double x, ae_state *_state)
double fdistribution(ae_int_t a, ae_int_t b, double x, ae_state *_state)
doublereal * b
double incompletegammac(double a, double x, ae_state *_state)
long flag
#define y0
#define x0
double laguerrecalculate(ae_int_t n, double x, ae_state *_state)
double * f
void ae_vector_clear(ae_vector *dst)
Definition: ap.cpp:692
double invchisquaredistribution(double v, double y, ae_state *_state)
double incompleteellipticintegrale(double phi, double m, ae_state *_state)
double errorfunctionc(double x, ae_state *_state)
ae_bool ae_fp_less(double v1, double v2)
Definition: ap.cpp:1327
double exponentialintegralei(double x, ae_state *_state)
double invpoissondistribution(ae_int_t k, double y, ae_state *_state)
ae_bool ae_fp_neq(double v1, double v2)
Definition: ap.cpp:1321
double z
double dawsonintegral(double x, ae_state *_state)
void chebyshevcoefficients(ae_int_t n, ae_vector *c, ae_state *_state)
double ellipticintegralkhighprecision(double m1, ae_state *_state)
ae_error_type
Definition: ap.h:201
double besselk0(double x, ae_state *_state)
double bessely0(double x, ae_state *_state)
ae_bool ae_vector_set_length(ae_vector *dst, ae_int_t newsize, ae_state *state)
Definition: ap.cpp:658
double chebyshevcalculate(ae_int_t r, ae_int_t n, double x, ae_state *_state)
double besselj0(double x, ae_state *_state)
#define q2
double ae_log(double x, ae_state *state)
Definition: ap.cpp:1679
double gammafunction(double x, ae_state *_state)
double invincompletebeta(double a, double b, double y, ae_state *_state)
double nuexpm1(double x, ae_state *_state)
const alglib_impl::ae_vector * c_ptr() const
Definition: ap.cpp:5907
double invnormaldistribution(double y0, ae_state *_state)
#define j
double ellipticintegralk(double m, ae_state *_state)
int m
double invstudenttdistribution(ae_int_t k, double p, ae_state *_state)
double ae_sinh(double x, ae_state *state)
Definition: ap.cpp:1645
ae_int_t ae_ifloor(double x, ae_state *state)
Definition: ap.cpp:1557
void ae_state_init(ae_state *state)
Definition: ap.cpp:309
double ae_sqrt(double x, ae_state *state)
Definition: ap.cpp:1535
void ae_assert(ae_bool cond, const char *msg, ae_state *state)
Definition: ap.cpp:1227
union alglib_impl::ae_vector::@11 ptr
const char *volatile error_msg
Definition: ap.h:389
double hermitesum(ae_vector *c, ae_int_t n, double x, ae_state *_state)
double ae_atan(double x, ae_state *state)
Definition: ap.cpp:1669
double besselk1(double x, ae_state *_state)
double nulog1p(double x, ae_state *_state)
#define ae_machineepsilon
Definition: ap.h:825
double ae_cosh(double x, ae_state *state)
Definition: ap.cpp:1650
double ae_exp(double x, ae_state *state)
Definition: ap.cpp:1689
double ae_tanh(double x, ae_state *state)
Definition: ap.cpp:1654
void legendrecoefficients(ae_int_t n, ae_vector *c, ae_state *_state)
double besselyn(ae_int_t n, double x, ae_state *_state)
ptrdiff_t ae_int_t
Definition: ap.h:186
doublereal * u
double besseljn(ae_int_t n, double x, ae_state *_state)
ae_bool ae_vector_init(ae_vector *dst, ae_int_t size, ae_datatype datatype, ae_state *state, ae_bool make_automatic)
Definition: ap.cpp:580
double ae_sqr(double x, ae_state *state)
Definition: ap.cpp:1530
void sinecosineintegrals(double x, double *si, double *ci, ae_state *_state)
double incompletegamma(double a, double x, ae_state *_state)
double binomialdistribution(ae_int_t k, ae_int_t n, double p, ae_state *_state)
double ellipticintegrale(double m, ae_state *_state)
double inverf(double e, ae_state *_state)
ae_bool ae_fp_less_eq(double v1, double v2)
Definition: ap.cpp:1335
ae_int_t ae_round(double x, ae_state *state)
Definition: ap.cpp:1547
double lngamma(double x, double *sgngam, ae_state *_state)
alglib_impl::ae_int_t ae_int_t
Definition: ap.h:889
void ae_frame_leave(ae_state *state)
Definition: ap.cpp:415
#define ae_true
Definition: ap.h:195
int * n
ae_bool ae_fp_greater(double v1, double v2)
Definition: ap.cpp:1343
#define q3
doublereal * a
#define ae_minrealnumber
Definition: ap.h:827
double besselkn(ae_int_t nn, double x, ae_state *_state)
double legendresum(ae_vector *c, ae_int_t n, double x, ae_state *_state)
double besseli1(double x, ae_state *_state)
ae_int_t ae_trunc(double x, ae_state *state)
Definition: ap.cpp:1552
double laguerresum(ae_vector *c, ae_int_t n, double x, ae_state *_state)
double legendrecalculate(ae_int_t n, double x, ae_state *_state)
double poissondistribution(ae_int_t k, double m, ae_state *_state)
#define ae_maxrealnumber
Definition: ap.h:826
#define xc