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_exp.h>
27 #include <gsl/gsl_sf_gamma.h>
28 #include <gsl/gsl_sf_laguerre.h>
32 /*-*-*-*-*-*-*-*-*-*-*-* Private Section *-*-*-*-*-*-*-*-*-*-*-*/
35 /* based on the large 2b-4a asymptotic for 1F1
36 * [Abramowitz+Stegun, 13.5.21]
37 * L^a_n(x) = (a+1)_n / n! 1F1(-n,a+1,x)
39 * The second term (ser_term2) is from Slater,"The Confluent
40 * Hypergeometric Function" p.73. I think there may be an error in
41 * the first term of the expression given there, comparing with AS
42 * 13.5.21 (cf sin(a\pi+\Theta) vs sin(a\pi) + sin(\Theta)) - but the
43 * second term appears correct.
48 laguerre_large_n(const int n, const double alpha, const double x,
49 gsl_sf_result * result)
52 const double b = alpha + 1.0;
53 const double eta = 2.0*b - 4.0*a;
54 const double cos2th = x/eta;
55 const double sin2th = 1.0 - cos2th;
56 const double eps = asin(sqrt(cos2th)); /* theta = pi/2 - eps */
57 const double pre_h = 0.25*M_PI*M_PI*eta*eta*cos2th*sin2th;
60 int stat_lg = gsl_sf_lngamma_e(b+n, &lg_b);
61 int stat_lf = gsl_sf_lnfact_e(n, &lnfact);
62 double pre_term1 = 0.5*(1.0-b)*log(0.25*x*eta);
63 double pre_term2 = 0.25*log(pre_h);
64 double lnpre_val = lg_b.val - lnfact.val + 0.5*x + pre_term1 - pre_term2;
65 double lnpre_err = lg_b.err + lnfact.err + GSL_DBL_EPSILON * (fabs(pre_term1)+fabs(pre_term2));
67 double phi1 = 0.25*eta*(2*eps + sin(2.0*eps));
68 double ser_term1 = -sin(phi1);
70 double A1 = (1.0/12.0)*(5.0/(4.0*sin2th)+(3.0*b*b-6.0*b+2.0)*sin2th - 1.0);
71 double ser_term2 = -A1 * cos(phi1)/(0.25*eta*sin(2.0*eps));
73 double ser_val = ser_term1 + ser_term2;
74 double ser_err = ser_term2*ser_term2 + GSL_DBL_EPSILON * (fabs(ser_term1) + fabs(ser_term2));
75 int stat_e = gsl_sf_exp_mult_err_e(lnpre_val, lnpre_err, ser_val, ser_err, result);
76 result->err += 2.0 * GSL_SQRT_DBL_EPSILON * fabs(result->val);
77 return GSL_ERROR_SELECT_3(stat_e, stat_lf, stat_lg);
81 /* Evaluate polynomial based on confluent hypergeometric representation.
83 * L^a_n(x) = (a+1)_n / n! 1F1(-n,a+1,x)
85 * assumes n > 0 and a != negative integer greater than -n
89 laguerre_n_cp(const int n, const double a, const double x, gsl_sf_result * result)
95 int stat_f = gsl_sf_lnfact_e(n, &lnfact);
96 int stat_g1 = gsl_sf_lngamma_sgn_e(a+1.0+n, &lg1, &s1);
97 int stat_g2 = gsl_sf_lngamma_sgn_e(a+1.0, &lg2, &s2);
98 double poly_1F1_val = 1.0;
99 double poly_1F1_err = 0.0;
103 double lnpre_val = (lg1.val - lg2.val) - lnfact.val;
104 double lnpre_err = lg1.err + lg2.err + lnfact.err + 2.0 * GSL_DBL_EPSILON * fabs(lnpre_val);
106 for(k=n-1; k>=0; k--) {
107 double t = (-n+k)/(a+1.0+k) * (x/(k+1));
108 double r = t + 1.0/poly_1F1_val;
109 if(r > 0.9*GSL_DBL_MAX/poly_1F1_val) {
110 /* internal error only, don't call the error handler */
111 INTERNAL_OVERFLOW_ERROR(result);
114 /* Collect the Horner terms. */
115 poly_1F1_val = 1.0 + t * poly_1F1_val;
116 poly_1F1_err += GSL_DBL_EPSILON + fabs(t) * poly_1F1_err;
120 stat_e = gsl_sf_exp_mult_err_e(lnpre_val, lnpre_err,
121 poly_1F1_val, poly_1F1_err,
124 return GSL_ERROR_SELECT_4(stat_e, stat_f, stat_g1, stat_g2);
128 /* Evaluate the polynomial based on the confluent hypergeometric
129 * function in a safe way, with no restriction on the arguments.
135 laguerre_n_poly_safe(const int n, const double a, const double x, gsl_sf_result * result)
137 const double b = a + 1.0;
138 const double mx = -x;
139 const double tc_sgn = (x < 0.0 ? 1.0 : (GSL_IS_ODD(n) ? -1.0 : 1.0));
141 int stat_tc = gsl_sf_taylorcoeff_e(n, fabs(x), &tc);
143 if(stat_tc == GSL_SUCCESS) {
144 double term = tc.val * tc_sgn;
145 double sum_val = term;
146 double sum_err = tc.err;
148 for(k=n-1; k>=0; k--) {
149 term *= ((b+k)/(n-k))*(k+1.0)/mx;
151 sum_err += 4.0 * GSL_DBL_EPSILON * fabs(term);
153 result->val = sum_val;
154 result->err = sum_err + 2.0 * GSL_DBL_EPSILON * fabs(result->val);
157 else if(stat_tc == GSL_EOVRFLW) {
158 result->val = 0.0; /* FIXME: should be Inf */
171 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*/
174 gsl_sf_laguerre_1_e(const double a, const double x, gsl_sf_result * result)
176 /* CHECK_POINTER(result) */
179 result->val = 1.0 + a - x;
180 result->err = 2.0 * GSL_DBL_EPSILON * (1.0 + fabs(a) + fabs(x));
186 gsl_sf_laguerre_2_e(const double a, const double x, gsl_sf_result * result)
188 /* CHECK_POINTER(result) */
191 result->val = 0.5*x*x;
192 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
196 double c0 = 0.5 * (2.0+a)*(1.0+a);
197 double c1 = -(2.0+a);
198 double c2 = -0.5/(2.0+a);
199 result->val = c0 + c1*x*(1.0 + c2*x);
200 result->err = 2.0 * GSL_DBL_EPSILON * (fabs(c0) + 2.0 * fabs(c1*x) * (1.0 + 2.0 * fabs(c2*x)));
201 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
207 gsl_sf_laguerre_3_e(const double a, const double x, gsl_sf_result * result)
209 /* CHECK_POINTER(result) */
212 double x2_6 = x*x/6.0;
213 result->val = x2_6 * (3.0 - x);
214 result->err = x2_6 * (3.0 + fabs(x)) * 2.0 * GSL_DBL_EPSILON;
215 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
219 result->val = -x*x/6.0;
220 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
224 double c0 = (3.0+a)*(2.0+a)*(1.0+a) / 6.0;
225 double c1 = -c0 * 3.0 / (1.0+a);
226 double c2 = -1.0/(2.0+a);
227 double c3 = -1.0/(3.0*(3.0+a));
228 result->val = c0 + c1*x*(1.0 + c2*x*(1.0 + c3*x));
229 result->err = 1.0 + 2.0 * fabs(c3*x);
230 result->err = 1.0 + 2.0 * fabs(c2*x) * result->err;
231 result->err = 2.0 * GSL_DBL_EPSILON * (fabs(c0) + 2.0 * fabs(c1*x) * result->err);
232 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
238 int gsl_sf_laguerre_n_e(const int n, const double a, const double x,
239 gsl_sf_result * result)
241 /* CHECK_POINTER(result) */
244 DOMAIN_ERROR(result);
252 result->val = 1.0 + a - x;
253 result->err = 2.0 * GSL_DBL_EPSILON * (1.0 + fabs(a) + fabs(x));
257 double product = a + 1.0;
259 for(k=2; k<=n; k++) {
260 product *= (a + k)/k;
262 result->val = product;
263 result->err = 2.0 * (n + 1.0) * GSL_DBL_EPSILON * fabs(product) + GSL_DBL_EPSILON;
266 else if(x < 0.0 && a > -1.0) {
267 /* In this case all the terms in the polynomial
268 * are of the same sign. Note that this also
269 * catches overflows correctly.
271 return laguerre_n_cp(n, a, x, result);
273 else if(n < 5 || (x > 0.0 && a < -n-1)) {
274 /* Either the polynomial will not lose too much accuracy
275 * or all the terms are negative. In any case,
276 * the error estimate here is good. We try both
277 * explicit summation methods, as they have different
278 * characteristics. One may underflow/overflow while the
281 if(laguerre_n_cp(n, a, x, result) == GSL_SUCCESS)
284 return laguerre_n_poly_safe(n, a, x, result);
286 else if(n > 1.0e+07 && x > 0.0 && a > -1.0 && x < 2.0*(a+1.0)+4.0*n) {
287 return laguerre_large_n(n, a, x, result);
289 else if(a >= 0.0 || (x > 0.0 && a < -n-1)) {
291 int stat_lg2 = gsl_sf_laguerre_2_e(a, x, &lg2);
292 double Lkm1 = 1.0 + a - x;
298 Lkp1 = (-(k+a)*Lkm1 + (2.0*k+a+1.0-x)*Lk)/(k+1.0);
303 result->err = (fabs(lg2.err/lg2.val) + GSL_DBL_EPSILON) * n * fabs(Lk);
307 /* Despair... or magic? */
308 return laguerre_n_poly_safe(n, a, x, result);
313 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
317 double gsl_sf_laguerre_1(double a, double x)
319 EVAL_RESULT(gsl_sf_laguerre_1_e(a, x, &result));
322 double gsl_sf_laguerre_2(double a, double x)
324 EVAL_RESULT(gsl_sf_laguerre_2_e(a, x, &result));
327 double gsl_sf_laguerre_3(double a, double x)
329 EVAL_RESULT(gsl_sf_laguerre_3_e(a, x, &result));
332 double gsl_sf_laguerre_n(int n, double a, double x)
334 EVAL_RESULT(gsl_sf_laguerre_n_e(n, a, x, &result));