1 /* specfunc/legendre_Qn.c
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 */
23 #include <gsl/gsl_math.h>
24 #include <gsl/gsl_errno.h>
25 #include <gsl/gsl_sf_bessel.h>
26 #include <gsl/gsl_sf_elementary.h>
27 #include <gsl/gsl_sf_exp.h>
28 #include <gsl/gsl_sf_pow_int.h>
29 #include <gsl/gsl_sf_legendre.h>
33 /* Evaluate f_{ell+1}/f_ell
34 * f_ell := Q^{b}_{a+ell}(x)
39 legendreQ_CF1_xgt1(int ell, double a, double b, double x, double * result)
41 const double RECUR_BIG = GSL_SQRT_DBL_MAX;
42 const int maxiter = 5000;
48 double a1 = ell + 1.0 + a + b;
49 double b1 = (2.0*(ell+1.0+a) + 1.0) * x;
50 double An = b1*Anm1 + a1*Anm2;
51 double Bn = b1*Bnm1 + a1*Bnm2;
66 bn = (2.0*lna + 1.0) * x;
67 An = bn*Anm1 + an*Anm2;
68 Bn = bn*Bnm1 + an*Bnm2;
70 if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) {
83 if(fabs(del - 1.0) < 4.0*GSL_DBL_EPSILON) break;
89 GSL_ERROR ("error", GSL_EMAXITER);
95 /* Uniform asymptotic for Q_l(x).
96 * Assumes x > -1.0 and x != 1.0.
97 * Discards second order and higher terms.
101 legendre_Ql_asymp_unif(const double ell, const double x, gsl_sf_result * result)
104 double u = ell + 0.5;
106 gsl_sf_result Y0, Y1;
107 int stat_Y0, stat_Y1;
113 /* B00 = 1/8 (1 - th cot(th) / th^2
114 * pre = sqrt(th/sin(th))
116 if(th < GSL_ROOT4_DBL_EPSILON) {
117 B00 = (1.0 + th*th/15.0)/24.0;
118 pre = 1.0 + th*th/12.0;
121 double sin_th = sqrt(1.0 - x*x);
122 double cot_th = x / sin_th;
123 B00 = 1.0/8.0 * (1.0 - th * cot_th) / (th*th);
124 pre = sqrt(th/sin_th);
127 stat_Y0 = gsl_sf_bessel_Y0_e(u*th, &Y0);
128 stat_Y1 = gsl_sf_bessel_Y1_e(u*th, &Y1);
130 sum = -0.5*M_PI * (Y0.val + th/u * Y1.val * B00);
132 stat_m = gsl_sf_multiply_e(pre, sum, result);
133 result->err += 0.5*M_PI * fabs(pre) * (Y0.err + fabs(th/u*B00)*Y1.err);
134 result->err += GSL_DBL_EPSILON * fabs(result->val);
136 return GSL_ERROR_SELECT_3(stat_m, stat_Y0, stat_Y1);
139 double u = ell + 0.5;
140 double xi = acosh(x);
141 gsl_sf_result K0_scaled, K1_scaled;
142 int stat_K0, stat_K1;
148 /* B00 = -1/8 (1 - xi coth(xi) / xi^2
149 * pre = sqrt(xi/sinh(xi))
151 if(xi < GSL_ROOT4_DBL_EPSILON) {
152 B00 = (1.0-xi*xi/15.0)/24.0;
153 pre = 1.0 - xi*xi/12.0;
156 double sinh_xi = sqrt(x*x - 1.0);
157 double coth_xi = x / sinh_xi;
158 B00 = -1.0/8.0 * (1.0 - xi * coth_xi) / (xi*xi);
159 pre = sqrt(xi/sinh_xi);
162 stat_K0 = gsl_sf_bessel_K0_scaled_e(u*xi, &K0_scaled);
163 stat_K1 = gsl_sf_bessel_K1_scaled_e(u*xi, &K1_scaled);
165 sum = K0_scaled.val - xi/u * K1_scaled.val * B00;
167 stat_e = gsl_sf_exp_mult_e(-u*xi, pre * sum, result);
168 result->err = GSL_DBL_EPSILON * fabs(result->val) * fabs(u*xi);
169 result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val);
171 return GSL_ERROR_SELECT_3(stat_e, stat_K0, stat_K1);
177 /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/
180 gsl_sf_legendre_Q0_e(const double x, gsl_sf_result * result)
182 /* CHECK_POINTER(result) */
184 if(x <= -1.0 || x == 1.0) {
185 DOMAIN_ERROR(result);
187 else if(x*x < GSL_ROOT6_DBL_EPSILON) { /* |x| <~ 0.05 */
188 const double c3 = 1.0/3.0;
189 const double c5 = 1.0/5.0;
190 const double c7 = 1.0/7.0;
191 const double c9 = 1.0/9.0;
192 const double c11 = 1.0/11.0;
193 const double y = x * x;
194 const double series = 1.0 + y*(c3 + y*(c5 + y*(c7 + y*(c9 + y*c11))));
195 result->val = x * series;
196 result->err = 2.0 * GSL_DBL_EPSILON * fabs(x);
200 result->val = 0.5 * log((1.0+x)/(1.0-x));
201 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
205 result->val = 0.5 * log((x+1.0)/(x-1.0));
206 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
209 else if(x*GSL_DBL_MIN < 2.0) {
210 const double y = 1.0/(x*x);
211 const double c1 = 1.0/3.0;
212 const double c2 = 1.0/5.0;
213 const double c3 = 1.0/7.0;
214 const double c4 = 1.0/9.0;
215 const double c5 = 1.0/11.0;
216 const double c6 = 1.0/13.0;
217 const double c7 = 1.0/15.0;
218 result->val = (1.0/x) * (1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*(c6 + y*c7)))))));
219 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
223 UNDERFLOW_ERROR(result);
229 gsl_sf_legendre_Q1_e(const double x, gsl_sf_result * result)
231 /* CHECK_POINTER(result) */
233 if(x <= -1.0 || x == 1.0) {
234 DOMAIN_ERROR(result);
236 else if(x*x < GSL_ROOT6_DBL_EPSILON) { /* |x| <~ 0.05 */
237 const double c3 = 1.0/3.0;
238 const double c5 = 1.0/5.0;
239 const double c7 = 1.0/7.0;
240 const double c9 = 1.0/9.0;
241 const double c11 = 1.0/11.0;
242 const double y = x * x;
243 const double series = 1.0 + y*(c3 + y*(c5 + y*(c7 + y*(c9 + y*c11))));
244 result->val = x * x * series - 1.0;
245 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
249 result->val = 0.5 * x * (log((1.0+x)/(1.0-x))) - 1.0;
250 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
254 result->val = 0.5 * x * log((x+1.0)/(x-1.0)) - 1.0;
255 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
258 else if(x*GSL_SQRT_DBL_MIN < 0.99/M_SQRT3) {
259 const double y = 1/(x*x);
260 const double c1 = 3.0/5.0;
261 const double c2 = 3.0/7.0;
262 const double c3 = 3.0/9.0;
263 const double c4 = 3.0/11.0;
264 const double c5 = 3.0/13.0;
265 const double c6 = 3.0/15.0;
266 const double c7 = 3.0/17.0;
267 const double c8 = 3.0/19.0;
268 const double sum = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*(c6 + y*(c7 + y*c8)))))));
269 result->val = sum / (3.0*x*x);
270 result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val);
274 UNDERFLOW_ERROR(result);
280 gsl_sf_legendre_Ql_e(const int l, const double x, gsl_sf_result * result)
282 /* CHECK_POINTER(result) */
284 if(x <= -1.0 || x == 1.0 || l < 0) {
285 DOMAIN_ERROR(result);
288 return gsl_sf_legendre_Q0_e(x, result);
291 return gsl_sf_legendre_Q1_e(x, result);
293 else if(l > 100000) {
294 return legendre_Ql_asymp_unif(l, x, result);
297 /* Forward recurrence.
299 gsl_sf_result Q0, Q1;
300 int stat_Q0 = gsl_sf_legendre_Q0_e(x, &Q0);
301 int stat_Q1 = gsl_sf_legendre_Q1_e(x, &Q1);
302 double Qellm1 = Q0.val;
303 double Qell = Q1.val;
306 for(ell=1; ell<l; ell++) {
307 Qellp1 = (x*(2.0*ell + 1.0) * Qell - ell * Qellm1) / (ell + 1.0);
312 result->err = GSL_DBL_EPSILON * l * fabs(result->val);
313 return GSL_ERROR_SELECT_2(stat_Q0, stat_Q1);
319 int stat_CF1 = legendreQ_CF1_xgt1(l, 0.0, 0.0, x, &rat);
321 double Qellp1 = rat * GSL_SQRT_DBL_MIN;
322 double Qell = GSL_SQRT_DBL_MIN;
325 for(ell=l; ell>0; ell--) {
326 Qellm1 = (x * (2.0*ell + 1.0) * Qell - (ell+1.0) * Qellp1) / ell;
331 if(fabs(Qell) > fabs(Qellp1)) {
333 stat_Q = gsl_sf_legendre_Q0_e(x, &Q0);
334 result->val = GSL_SQRT_DBL_MIN * Q0.val / Qell;
335 result->err = l * GSL_DBL_EPSILON * fabs(result->val);
339 stat_Q = gsl_sf_legendre_Q1_e(x, &Q1);
340 result->val = GSL_SQRT_DBL_MIN * Q1.val / Qellp1;
341 result->err = l * GSL_DBL_EPSILON * fabs(result->val);
344 return GSL_ERROR_SELECT_2(stat_Q, stat_CF1);
349 /*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/
353 double gsl_sf_legendre_Q0(const double x)
355 EVAL_RESULT(gsl_sf_legendre_Q0_e(x, &result));
358 double gsl_sf_legendre_Q1(const double x)
360 EVAL_RESULT(gsl_sf_legendre_Q1_e(x, &result));
363 double gsl_sf_legendre_Ql(const int l, const double x)
365 EVAL_RESULT(gsl_sf_legendre_Ql_e(l, x, &result));