1 /* specfunc/gamma_inc.c
3 * Copyright (C) 2007 Brian Gough
4 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 /* Author: G. Jungman */
24 #include <gsl/gsl_math.h>
25 #include <gsl/gsl_errno.h>
26 #include <gsl/gsl_sf_erf.h>
27 #include <gsl/gsl_sf_exp.h>
28 #include <gsl/gsl_sf_log.h>
29 #include <gsl/gsl_sf_gamma.h>
30 #include <gsl/gsl_sf_expint.h>
35 * D(a,x) := x^a e^(-x) / Gamma(a+1)
39 gamma_inc_D(const double a, const double x, gsl_sf_result * result)
44 gsl_sf_lngamma_e(a+1.0, &lg);
45 lnr = a * log(x) - x - lg.val;
46 result->val = exp(lnr);
47 result->err = 2.0 * GSL_DBL_EPSILON * (fabs(lnr) + 1.0) * fabs(result->val);
52 gsl_sf_result ln_term;
57 ln_term.val = ln_u - u + 1.0;
58 ln_term.err = (fabs(ln_u) + fabs(u) + 1.0) * GSL_DBL_EPSILON;
61 gsl_sf_log_1plusx_mx_e(mu, &ln_term); /* log(1+mu) - mu */
63 gsl_sf_gammastar_e(a, &gstar);
64 term1 = exp(a*ln_term.val)/sqrt(2.0*M_PI*a);
65 result->val = term1/gstar.val;
66 result->err = 2.0 * GSL_DBL_EPSILON * (fabs(a*ln_term.val) + 1.0) * fabs(result->val);
67 result->err += gstar.err/fabs(gstar.val) * fabs(result->val);
74 /* P series representation.
78 gamma_inc_P_series(const double a, const double x, gsl_sf_result * result)
80 const int nmax = 5000;
83 int stat_D = gamma_inc_D(a, x, &D);
88 for(n=1; n<nmax; n++) {
91 if(fabs(term/sum) < GSL_DBL_EPSILON) break;
94 result->val = D.val * sum;
95 result->err = D.err * fabs(sum);
96 result->err += (1.0 + n) * GSL_DBL_EPSILON * fabs(result->val);
99 GSL_ERROR ("error", GSL_EMAXITER);
105 /* Q large x asymptotic
109 gamma_inc_Q_large_x(const double a, const double x, gsl_sf_result * result)
111 const int nmax = 5000;
114 const int stat_D = gamma_inc_D(a, x, &D);
120 for(n=1; n<nmax; n++) {
122 if(fabs(term/last) > 1.0) break;
123 if(fabs(term/sum) < GSL_DBL_EPSILON) break;
128 result->val = D.val * (a/x) * sum;
129 result->err = D.err * fabs((a/x) * sum);
130 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
133 GSL_ERROR ("error in large x asymptotic", GSL_EMAXITER);
139 /* Uniform asymptotic for x near a, a and x large.
140 * See [Temme, p. 285]
144 gamma_inc_Q_asymp_unif(const double a, const double x, gsl_sf_result * result)
146 const double rta = sqrt(a);
147 const double eps = (x-a)/a;
149 gsl_sf_result ln_term;
150 const int stat_ln = gsl_sf_log_1plusx_mx_e(eps, &ln_term); /* log(1+eps) - eps */
151 const double eta = GSL_SIGN(eps) * sqrt(-2.0*ln_term.val);
158 /* This used to say erfc(eta*M_SQRT2*rta), which is wrong.
159 * The sqrt(2) is in the denominator. Oops.
160 * Fixed: [GJ] Mon Nov 15 13:25:32 MST 2004
162 gsl_sf_erfc_e(eta*rta/M_SQRT2, &erfc);
164 if(fabs(eps) < GSL_ROOT5_DBL_EPSILON) {
165 c0 = -1.0/3.0 + eps*(1.0/12.0 - eps*(23.0/540.0 - eps*(353.0/12960.0 - eps*589.0/30240.0)));
166 c1 = -1.0/540.0 - eps/288.0;
169 const double rt_term = sqrt(-2.0 * ln_term.val/(eps*eps));
170 const double lam = x/a;
171 c0 = (1.0 - 1.0/rt_term)/eps;
172 c1 = -(eta*eta*eta * (lam*lam + 10.0*lam + 1.0) - 12.0 * eps*eps*eps) / (12.0 * eta*eta*eta*eps*eps*eps);
175 R = exp(-0.5*a*eta*eta)/(M_SQRT2*M_SQRTPI*rta) * (c0 + c1/a);
177 result->val = 0.5 * erfc.val + R;
178 result->err = GSL_DBL_EPSILON * fabs(R * 0.5 * a*eta*eta) + 0.5 * erfc.err;
179 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
185 /* Continued fraction which occurs in evaluation
186 * of Q(a,x) or Gamma(a,x).
188 * 1 (1-a)/x 1/x (2-a)/x 2/x (3-a)/x
189 * F(a,x) = ---- ------- ----- -------- ----- -------- ...
190 * 1 + 1 + 1 + 1 + 1 + 1 +
192 * Hans E. Plesser, 2002-01-22 (hans dot plesser at itf dot nlh dot no).
194 * Split out from gamma_inc_Q_CF() by GJ [Tue Apr 1 13:16:41 MST 2003].
195 * See gamma_inc_Q_CF() below.
199 gamma_inc_F_CF(const double a, const double x, gsl_sf_result * result)
201 const int nmax = 5000;
202 const double small = gsl_pow_3 (GSL_DBL_EPSILON);
204 double hn = 1.0; /* convergent */
205 double Cn = 1.0 / small;
209 /* n == 1 has a_1, b_1, b_0 independent of a,x,
210 so that has been done by hand */
211 for ( n = 2 ; n < nmax ; n++ )
222 if ( fabs(Dn) < small )
225 if ( fabs(Cn) < small )
230 if(fabs(delta-1.0) < GSL_DBL_EPSILON) break;
234 result->err = 2.0*GSL_DBL_EPSILON * fabs(hn);
235 result->err += GSL_DBL_EPSILON * (2.0 + 0.5*n) * fabs(result->val);
238 GSL_ERROR ("error in CF for F(a,x)", GSL_EMAXITER);
244 /* Continued fraction for Q.
246 * Q(a,x) = D(a,x) a/x F(a,x)
248 * Hans E. Plesser, 2002-01-22 (hans dot plesser at itf dot nlh dot no):
250 * Since the Gautschi equivalent series method for CF evaluation may lead
251 * to singularities, I have replaced it with the modified Lentz algorithm
254 * I J Thompson and A R Barnett
255 * Coulomb and Bessel Functions of Complex Arguments and Order
256 * J Computational Physics 64:490-509 (1986)
258 * In consequence, gamma_inc_Q_CF_protected() is now obsolete and has been
261 * Identification of terms between the above equation for F(a, x) and
262 * the first equation in the appendix of Thompson&Barnett is as follows:
264 * b_0 = 0, b_n = 1 for all n > 0
267 * a_n = (n/2-a)/x for n even
268 * a_n = (n-1)/(2x) for n odd
273 gamma_inc_Q_CF(const double a, const double x, gsl_sf_result * result)
277 const int stat_D = gamma_inc_D(a, x, &D);
278 const int stat_F = gamma_inc_F_CF(a, x, &F);
280 result->val = D.val * (a/x) * F.val;
281 result->err = D.err * fabs((a/x) * F.val) + fabs(D.val * a/x * F.err);
283 return GSL_ERROR_SELECT_2(stat_F, stat_D);
287 /* Useful for small a and x. Handles the subtraction analytically.
291 gamma_inc_Q_series(const double a, const double x, gsl_sf_result * result)
293 double term1; /* 1 - x^a/Gamma(a+1) */
294 double sum; /* 1 + (a+1)/(a+2)(-x)/2! + (a+1)/(a+3)(-x)^2/3! + ... */
296 double term2; /* a temporary variable used at the end */
299 /* Evaluate series for 1 - x^a/Gamma(a+1), small a
301 const double pg21 = -2.404113806319188570799476; /* PolyGamma[2,1] */
302 const double lnx = log(x);
303 const double el = M_EULER+lnx;
304 const double c1 = -el;
305 const double c2 = M_PI*M_PI/12.0 - 0.5*el*el;
306 const double c3 = el*(M_PI*M_PI/12.0 - el*el/6.0) + pg21/6.0;
307 const double c4 = -0.04166666666666666667
308 * (-1.758243446661483480 + lnx)
309 * (-0.764428657272716373 + lnx)
310 * ( 0.723980571623507657 + lnx)
311 * ( 4.107554191916823640 + lnx);
312 const double c5 = -0.0083333333333333333
313 * (-2.06563396085715900 + lnx)
314 * (-1.28459889470864700 + lnx)
315 * (-0.27583535756454143 + lnx)
316 * ( 1.33677371336239618 + lnx)
317 * ( 5.17537282427561550 + lnx);
318 const double c6 = -0.0013888888888888889
319 * (-2.30814336454783200 + lnx)
320 * (-1.65846557706987300 + lnx)
321 * (-0.88768082560020400 + lnx)
322 * ( 0.17043847751371778 + lnx)
323 * ( 1.92135970115863890 + lnx)
324 * ( 6.22578557795474900 + lnx);
325 const double c7 = -0.00019841269841269841
326 * (-2.5078657901291800 + lnx)
327 * (-1.9478900888958200 + lnx)
328 * (-1.3194837322612730 + lnx)
329 * (-0.5281322700249279 + lnx)
330 * ( 0.5913834939078759 + lnx)
331 * ( 2.4876819633378140 + lnx)
332 * ( 7.2648160783762400 + lnx);
333 const double c8 = -0.00002480158730158730
334 * (-2.677341544966400 + lnx)
335 * (-2.182810448271700 + lnx)
336 * (-1.649350342277400 + lnx)
337 * (-1.014099048290790 + lnx)
338 * (-0.191366955370652 + lnx)
339 * ( 0.995403817918724 + lnx)
340 * ( 3.041323283529310 + lnx)
341 * ( 8.295966556941250 + lnx);
342 const double c9 = -2.75573192239859e-6
343 * (-2.8243487670469080 + lnx)
344 * (-2.3798494322701120 + lnx)
345 * (-1.9143674728689960 + lnx)
346 * (-1.3814529102920370 + lnx)
347 * (-0.7294312810261694 + lnx)
348 * ( 0.1299079285269565 + lnx)
349 * ( 1.3873333251885240 + lnx)
350 * ( 3.5857258865210760 + lnx)
351 * ( 9.3214237073814600 + lnx);
352 const double c10 = -2.75573192239859e-7
353 * (-2.9540329644556910 + lnx)
354 * (-2.5491366926991850 + lnx)
355 * (-2.1348279229279880 + lnx)
356 * (-1.6741881076349450 + lnx)
357 * (-1.1325949616098420 + lnx)
358 * (-0.4590034650618494 + lnx)
359 * ( 0.4399352987435699 + lnx)
360 * ( 1.7702236517651670 + lnx)
361 * ( 4.1231539047474080 + lnx)
362 * ( 10.342627908148680 + lnx);
364 term1 = a*(c1+a*(c2+a*(c3+a*(c4+a*(c5+a*(c6+a*(c7+a*(c8+a*(c9+a*c10)))))))));
370 const int nmax = 5000;
375 for(n=1; n<nmax; n++) {
377 sum += (a+1.0)/(a+n+1.0)*t;
378 if(fabs(t/sum) < GSL_DBL_EPSILON) break;
382 stat_sum = GSL_EMAXITER;
384 stat_sum = GSL_SUCCESS;
387 term2 = (1.0 - term1) * a/(a+1.0) * x * sum;
388 result->val = term1 + term2;
389 result->err = GSL_DBL_EPSILON * (fabs(term1) + 2.0*fabs(term2));
390 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
395 /* series for small a and x, but not defined for a == 0 */
397 gamma_inc_series(double a, double x, gsl_sf_result * result)
401 const int stat_Q = gamma_inc_Q_series(a, x, &Q);
402 const int stat_G = gsl_sf_gamma_e(a, &G);
403 result->val = Q.val * G.val;
404 result->err = fabs(Q.val * G.err) + fabs(Q.err * G.val);
405 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
407 return GSL_ERROR_SELECT_2(stat_Q, stat_G);
412 gamma_inc_a_gt_0(double a, double x, gsl_sf_result * result)
414 /* x > 0 and a > 0; use result for Q */
417 const int stat_Q = gsl_sf_gamma_inc_Q_e(a, x, &Q);
418 const int stat_G = gsl_sf_gamma_e(a, &G);
420 result->val = G.val * Q.val;
421 result->err = fabs(G.val * Q.err) + fabs(G.err * Q.val);
422 result->err += 2.0*GSL_DBL_EPSILON * fabs(result->val);
424 return GSL_ERROR_SELECT_2(stat_G, stat_Q);
429 gamma_inc_CF(double a, double x, gsl_sf_result * result)
433 const double am1lgx = (a-1.0)*log(x);
434 const int stat_F = gamma_inc_F_CF(a, x, &F);
435 const int stat_E = gsl_sf_exp_err_e(am1lgx - x, GSL_DBL_EPSILON*fabs(am1lgx), &pre);
437 result->val = F.val * pre.val;
438 result->err = fabs(F.err * pre.val) + fabs(F.val * pre.err);
439 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
441 return GSL_ERROR_SELECT_2(stat_F, stat_E);
445 /* evaluate Gamma(0,x), x > 0 */
446 #define GAMMA_INC_A_0(x, result) gsl_sf_expint_E1_e(x, result)
449 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
452 gsl_sf_gamma_inc_Q_e(const double a, const double x, gsl_sf_result * result)
454 if(a < 0.0 || x < 0.0) {
455 DOMAIN_ERROR(result);
468 else if(x <= 0.5*a) {
469 /* If the series is quick, do that. It is
473 int stat_P = gamma_inc_P_series(a, x, &P);
474 result->val = 1.0 - P.val;
476 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
479 else if(a >= 1.0e+06 && (x-a)*(x-a) < a) {
480 /* Then try the difficult asymptotic regime.
481 * This is the only way to do this region.
483 return gamma_inc_Q_asymp_unif(a, x, result);
485 else if(a < 0.2 && x < 5.0) {
486 /* Cancellations at small a must be handled
487 * analytically; x should not be too big
488 * either since the series terms grow
491 return gamma_inc_Q_series(a, x, result);
495 /* Continued fraction is excellent for x >~ a.
496 * We do not let x be too large when x > a since
497 * it is somewhat pointless to try this there;
498 * the function is rapidly decreasing for
499 * x large and x > a, and it will just
500 * underflow in that region anyway. We
501 * catch that case in the standard
504 return gamma_inc_Q_CF(a, x, result);
507 return gamma_inc_Q_large_x(a, x, result);
511 if(x > a - sqrt(a)) {
512 /* Continued fraction again. The convergence
513 * is a little slower here, but that is fine.
514 * We have to trade that off against the slow
515 * convergence of the series, which is the
518 return gamma_inc_Q_CF(a, x, result);
522 int stat_P = gamma_inc_P_series(a, x, &P);
523 result->val = 1.0 - P.val;
525 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
533 gsl_sf_gamma_inc_P_e(const double a, const double x, gsl_sf_result * result)
535 if(a <= 0.0 || x < 0.0) {
536 DOMAIN_ERROR(result);
543 else if(x < 20.0 || x < 0.5*a) {
544 /* Do the easy series cases. Robust and quick.
546 return gamma_inc_P_series(a, x, result);
548 else if(a > 1.0e+06 && (x-a)*(x-a) < a) {
549 /* Crossover region. Note that Q and P are
550 * roughly the same order of magnitude here,
551 * so the subtraction is stable.
554 int stat_Q = gamma_inc_Q_asymp_unif(a, x, &Q);
555 result->val = 1.0 - Q.val;
557 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
561 /* Q <~ P in this area, so the
562 * subtractions are stable.
567 stat_Q = gamma_inc_Q_CF(a, x, &Q);
570 stat_Q = gamma_inc_Q_large_x(a, x, &Q);
572 result->val = 1.0 - Q.val;
574 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
578 if((x-a)*(x-a) < a) {
579 /* This condition is meant to insure
580 * that Q is not very close to 1,
581 * so the subtraction is stable.
584 int stat_Q = gamma_inc_Q_CF(a, x, &Q);
585 result->val = 1.0 - Q.val;
587 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
591 return gamma_inc_P_series(a, x, result);
598 gsl_sf_gamma_inc_e(const double a, const double x, gsl_sf_result * result)
601 DOMAIN_ERROR(result);
604 return gsl_sf_gamma_e(a, result);
608 return GAMMA_INC_A_0(x, result);
612 return gamma_inc_a_gt_0(a, x, result);
616 /* continued fraction seems to fail for x too small; otherwise
617 it is ok, independent of the value of |x/a|, because of the
618 non-oscillation in the expansion, i.e. the CF is
619 un-conditionally convergent for a < 0 and x > 0
621 return gamma_inc_CF(a, x, result);
623 else if(fabs(a) < 0.5)
625 return gamma_inc_series(a, x, result);
629 /* a = fa + da; da >= 0 */
630 const double fa = floor(a);
631 const double da = a - fa;
634 const int stat_g_da = ( da > 0.0 ? gamma_inc_a_gt_0(da, x, &g_da)
635 : GAMMA_INC_A_0(x, &g_da));
638 double gax = g_da.val;
640 /* Gamma(alpha-1,x) = 1/(alpha-1) (Gamma(a,x) - x^(alpha-1) e^-x) */
643 const double shift = exp(-x + (alpha-1.0)*log(x));
644 gax = (gax - shift) / (alpha - 1.0);
649 result->err = 2.0*(1.0 + fabs(a))*GSL_DBL_EPSILON*fabs(gax);
656 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
660 double gsl_sf_gamma_inc_P(const double a, const double x)
662 EVAL_RESULT(gsl_sf_gamma_inc_P_e(a, x, &result));
665 double gsl_sf_gamma_inc_Q(const double a, const double x)
667 EVAL_RESULT(gsl_sf_gamma_inc_Q_e(a, x, &result));
670 double gsl_sf_gamma_inc(const double a, const double x)
672 EVAL_RESULT(gsl_sf_gamma_inc_e(a, x, &result));