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_log.h>
27 #include <gsl/gsl_sf_exp.h>
28 #include <gsl/gsl_sf_gamma.h>
29 #include <gsl/gsl_sf_hyperg.h>
35 isnegint (const double x)
37 return (x < 0) && (x == floor(x));
46 gsl_sf_result * result
49 const unsigned int max_iter = 512; /* control iterations */
50 const double cutoff = 2.0 * GSL_DBL_MIN; /* control the zero cutoff */
51 unsigned int iter_count = 0;
54 /* standard initialization for continued fraction */
55 double num_term = 1.0;
56 double den_term = 1.0 - (a+b)*x/(a+1.0);
57 if (fabs(den_term) < cutoff) den_term = cutoff;
58 den_term = 1.0/den_term;
61 while(iter_count < max_iter) {
62 const int k = iter_count + 1;
63 double coeff = k*(b-k)*x/(((a-1.0)+2*k)*(a+2*k));
67 den_term = 1.0 + coeff*den_term;
68 num_term = 1.0 + coeff/num_term;
69 if(fabs(den_term) < cutoff) den_term = cutoff;
70 if(fabs(num_term) < cutoff) num_term = cutoff;
71 den_term = 1.0/den_term;
73 delta_frac = den_term * num_term;
76 coeff = -(a+k)*(a+b+k)*x/((a+2*k)*(a+2*k+1.0));
79 den_term = 1.0 + coeff*den_term;
80 num_term = 1.0 + coeff/num_term;
81 if(fabs(den_term) < cutoff) den_term = cutoff;
82 if(fabs(num_term) < cutoff) num_term = cutoff;
83 den_term = 1.0/den_term;
85 delta_frac = den_term*num_term;
88 if(fabs(delta_frac-1.0) < 2.0*GSL_DBL_EPSILON) break;
94 result->err = iter_count * 4.0 * GSL_DBL_EPSILON * fabs(cf);
96 if(iter_count >= max_iter)
97 GSL_ERROR ("error", GSL_EMAXITER);
104 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
111 gsl_sf_result * result
114 if(x < 0.0 || x > 1.0) {
115 DOMAIN_ERROR(result);
116 } else if (isnegint(a) || isnegint(b)) {
117 DOMAIN_ERROR(result);
118 } else if (isnegint(a+b)) {
119 DOMAIN_ERROR(result);
120 } else if(x == 0.0) {
129 } else if (a <= 0 || b <= 0) {
130 gsl_sf_result f, beta;
132 const int stat_f = gsl_sf_hyperg_2F1_e(a, 1-b, a+1, x, &f);
133 const int stat_beta = gsl_sf_beta_e(a, b, &beta);
134 double prefactor = (pow(x, a) / a);
135 result->val = prefactor * f.val / beta.val;
136 result->err = fabs(prefactor) * f.err/ fabs(beta.val) + fabs(result->val/beta.val) * beta.err;
138 stat = GSL_ERROR_SELECT_2(stat_f, stat_beta);
139 if(stat == GSL_SUCCESS) {
140 CHECK_UNDERFLOW(result);
144 gsl_sf_result ln_beta;
146 gsl_sf_result ln_1mx;
147 gsl_sf_result prefactor;
148 const int stat_ln_beta = gsl_sf_lnbeta_e(a, b, &ln_beta);
149 const int stat_ln_1mx = gsl_sf_log_1plusx_e(-x, &ln_1mx);
150 const int stat_ln_x = gsl_sf_log_e(x, &ln_x);
151 const int stat_ln = GSL_ERROR_SELECT_3(stat_ln_beta, stat_ln_1mx, stat_ln_x);
153 const double ln_pre_val = -ln_beta.val + a * ln_x.val + b * ln_1mx.val;
154 const double ln_pre_err = ln_beta.err + fabs(a*ln_x.err) + fabs(b*ln_1mx.err);
155 const int stat_exp = gsl_sf_exp_err_e(ln_pre_val, ln_pre_err, &prefactor);
157 if(stat_ln != GSL_SUCCESS) {
160 GSL_ERROR ("error", GSL_ESANITY);
163 if(x < (a + 1.0)/(a+b+2.0)) {
164 /* Apply continued fraction directly. */
166 const int stat_cf = beta_cont_frac(a, b, x, &cf);
168 result->val = prefactor.val * cf.val / a;
169 result->err = (fabs(prefactor.err * cf.val) + fabs(prefactor.val * cf.err))/a;
171 stat = GSL_ERROR_SELECT_2(stat_exp, stat_cf);
172 if(stat == GSL_SUCCESS) {
173 CHECK_UNDERFLOW(result);
178 /* Apply continued fraction after hypergeometric transformation. */
180 const int stat_cf = beta_cont_frac(b, a, 1.0-x, &cf);
182 const double term = prefactor.val * cf.val / b;
183 result->val = 1.0 - term;
184 result->err = fabs(prefactor.err * cf.val)/b;
185 result->err += fabs(prefactor.val * cf.err)/b;
186 result->err += 2.0 * GSL_DBL_EPSILON * (1.0 + fabs(term));
187 stat = GSL_ERROR_SELECT_2(stat_exp, stat_cf);
188 if(stat == GSL_SUCCESS) {
189 CHECK_UNDERFLOW(result);
197 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
201 double gsl_sf_beta_inc(const double a, const double b, const double x)
203 EVAL_RESULT(gsl_sf_beta_inc_e(a, b, x, &result));