3 * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
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; either version 3 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 /* Author: G. Jungman */
22 /* Miscellaneous implementations of use
23 * for evaluation of hypergeometric functions.
26 #include <gsl/gsl_math.h>
27 #include <gsl/gsl_errno.h>
28 #include <gsl/gsl_sf_exp.h>
29 #include <gsl/gsl_sf_gamma.h>
34 #define SUM_LARGE (1.0e-5*GSL_DBL_MAX)
38 gsl_sf_hyperg_1F1_series_e(const double a, const double b, const double x,
39 gsl_sf_result * result
47 double max_abs_del = 1.0;
51 while(abs_del/fabs(sum_val) > 0.25*GSL_DBL_EPSILON) {
59 result->val = sum_val;
60 result->err = sum_err;
61 result->err += 2.0 * GSL_DBL_EPSILON * n * fabs(sum_val);
66 result->val = sum_val;
67 result->err = sum_err;
68 GSL_ERROR ("hypergeometric series failed to converge", GSL_EFAILED);
73 if(abs_u > 1.0 && max_abs_del > GSL_DBL_MAX/abs_u) {
74 result->val = sum_val;
75 result->err = fabs(sum_val);
76 GSL_ERROR ("overflow", GSL_EOVRFLW);
80 if(fabs(sum_val) > SUM_LARGE) {
81 result->val = sum_val;
82 result->err = fabs(sum_val);
83 GSL_ERROR ("overflow", GSL_EOVRFLW);
87 max_abs_del = GSL_MAX_DBL(abs_del, max_abs_del);
88 sum_err += 2.0*GSL_DBL_EPSILON*abs_del;
95 result->val = sum_val;
96 result->err = sum_err;
97 result->err += abs_del;
98 result->err += 2.0 * GSL_DBL_EPSILON * n * fabs(sum_val);
105 gsl_sf_hyperg_1F1_large_b_e(const double a, const double b, const double x, gsl_sf_result * result)
107 if(fabs(x/b) < 1.0) {
108 const double u = x/b;
109 const double v = 1.0/(1.0-u);
110 const double pre = pow(v,a);
111 const double uv = u*v;
112 const double uv2 = uv*uv;
113 const double t1 = a*(a+1.0)/(2.0*b)*uv2;
114 const double t2a = a*(a+1.0)/(24.0*b*b)*uv2;
115 const double t2b = 12.0 + 16.0*(a+2.0)*uv + 3.0*(a+2.0)*(a+3.0)*uv2;
116 const double t2 = t2a*t2b;
117 result->val = pre * (1.0 - t1 + t2);
118 result->err = pre * GSL_DBL_EPSILON * (1.0 + fabs(t1) + fabs(t2));
119 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
123 DOMAIN_ERROR(result);
129 gsl_sf_hyperg_U_large_b_e(const double a, const double b, const double x,
130 gsl_sf_result * result,
131 double * ln_multiplier
134 double N = floor(b); /* b = N + eps */
137 if(fabs(eps) < GSL_SQRT_DBL_EPSILON) {
142 double tmp = (1.0-b)*log(x);
143 gsl_sf_result lg_bm1;
145 gsl_sf_lngamma_e(b-1.0, &lg_bm1);
146 gsl_sf_lngamma_e(a, &lg_a);
147 lnpre_val = tmp + x + lg_bm1.val - lg_a.val;
148 lnpre_err = lg_bm1.err + lg_a.err + GSL_DBL_EPSILON * (fabs(x) + fabs(tmp));
149 gsl_sf_hyperg_1F1_large_b_e(1.0-a, 2.0-b, -x, &M);
152 gsl_sf_result lg_1mb;
153 gsl_sf_result lg_1pamb;
154 gsl_sf_lngamma_e(1.0-b, &lg_1mb);
155 gsl_sf_lngamma_e(1.0+a-b, &lg_1pamb);
156 lnpre_val = lg_1mb.val - lg_1pamb.val;
157 lnpre_err = lg_1mb.err + lg_1pamb.err;
158 gsl_sf_hyperg_1F1_large_b_e(a, b, x, &M);
161 if(lnpre_val > GSL_LOG_DBL_MAX-10.0) {
164 *ln_multiplier = lnpre_val;
165 GSL_ERROR ("overflow", GSL_EOVRFLW);
169 int stat_e = gsl_sf_exp_err_e(lnpre_val, lnpre_err, &epre);
170 result->val = epre.val * M.val;
171 result->err = epre.val * M.err + epre.err * fabs(M.val);
172 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
173 *ln_multiplier = 0.0;
178 double omb_lnx = (1.0-b)*log(x);
179 gsl_sf_result lg_1mb; double sgn_1mb;
180 gsl_sf_result lg_1pamb; double sgn_1pamb;
181 gsl_sf_result lg_bm1; double sgn_bm1;
182 gsl_sf_result lg_a; double sgn_a;
183 gsl_sf_result M1, M2;
184 double lnpre1_val, lnpre2_val;
185 double lnpre1_err, lnpre2_err;
186 double sgpre1, sgpre2;
187 gsl_sf_hyperg_1F1_large_b_e( a, b, x, &M1);
188 gsl_sf_hyperg_1F1_large_b_e(1.0-a, 2.0-b, x, &M2);
190 gsl_sf_lngamma_sgn_e(1.0-b, &lg_1mb, &sgn_1mb);
191 gsl_sf_lngamma_sgn_e(1.0+a-b, &lg_1pamb, &sgn_1pamb);
193 gsl_sf_lngamma_sgn_e(b-1.0, &lg_bm1, &sgn_bm1);
194 gsl_sf_lngamma_sgn_e(a, &lg_a, &sgn_a);
196 lnpre1_val = lg_1mb.val - lg_1pamb.val;
197 lnpre1_err = lg_1mb.err + lg_1pamb.err;
198 lnpre2_val = lg_bm1.val - lg_a.val - omb_lnx - x;
199 lnpre2_err = lg_bm1.err + lg_a.err + GSL_DBL_EPSILON * (fabs(omb_lnx)+fabs(x));
200 sgpre1 = sgn_1mb * sgn_1pamb;
201 sgpre2 = sgn_bm1 * sgn_a;
203 if(lnpre1_val > GSL_LOG_DBL_MAX-10.0 || lnpre2_val > GSL_LOG_DBL_MAX-10.0) {
204 double max_lnpre_val = GSL_MAX(lnpre1_val,lnpre2_val);
205 double max_lnpre_err = GSL_MAX(lnpre1_err,lnpre2_err);
206 double lp1 = lnpre1_val - max_lnpre_val;
207 double lp2 = lnpre2_val - max_lnpre_val;
208 double t1 = sgpre1*exp(lp1);
209 double t2 = sgpre2*exp(lp2);
210 result->val = t1*M1.val + t2*M2.val;
211 result->err = fabs(t1)*M1.err + fabs(t2)*M2.err;
212 result->err += GSL_DBL_EPSILON * exp(max_lnpre_err) * (fabs(t1*M1.val) + fabs(t2*M2.val));
213 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
214 *ln_multiplier = max_lnpre_val;
215 GSL_ERROR ("overflow", GSL_EOVRFLW);
218 double t1 = sgpre1*exp(lnpre1_val);
219 double t2 = sgpre2*exp(lnpre2_val);
220 result->val = t1*M1.val + t2*M2.val;
221 result->err = fabs(t1) * M1.err + fabs(t2)*M2.err;
222 result->err += GSL_DBL_EPSILON * (exp(lnpre1_err)*fabs(t1*M1.val) + exp(lnpre2_err)*fabs(t2*M2.val));
223 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
224 *ln_multiplier = 0.0;
232 /* [Carlson, p.109] says the error in truncating this asymptotic series
233 * is less than the absolute value of the first neglected term.
235 * A termination argument is provided, so that the series will
236 * be summed at most up to n=n_trunc. If n_trunc is set negative,
237 * then the series is summed until it appears to start diverging.
240 gsl_sf_hyperg_2F0_series_e(const double a, const double b, const double x,
242 gsl_sf_result * result
245 const int maxiter = 2000;
251 double abs_del = 1.0;
252 double max_abs_del = 1.0;
253 double last_abs_del = 1.0;
255 while(abs_del/fabs(sum) > GSL_DBL_EPSILON && n < maxiter) {
257 double u = an * (bn/n * x);
258 double abs_u = fabs(u);
260 if(abs_u > 1.0 && (max_abs_del > GSL_DBL_MAX/abs_u)) {
262 result->err = fabs(sum);
263 GSL_ERROR ("overflow", GSL_EOVRFLW);
271 if(abs_del > last_abs_del) break; /* series is probably starting to grow */
273 last_abs_del = abs_del;
274 max_abs_del = GSL_MAX(abs_del, max_abs_del);
280 if(an == 0.0 || bn == 0.0) break; /* series terminated */
282 if(n_trunc >= 0 && n >= n_trunc) break; /* reached requested timeout */
286 result->err = GSL_DBL_EPSILON * n + abs_del;
288 GSL_ERROR ("error", GSL_EMAXITER);