| 1 |
/* specfunc/legendre_Qn.c |
| 2 |
* |
| 3 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman |
| 4 |
* |
| 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. |
| 9 |
* |
| 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. |
| 14 |
* |
| 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. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Author: G. Jungman */ |
| 21 |
|
| 22 |
#include "gsl__config.h" |
| 23 |
#include "gsl_math.h" |
| 24 |
#include "gsl_errno.h" |
| 25 |
#include "gsl_sf_bessel.h" |
| 26 |
#include "gsl_sf_elementary.h" |
| 27 |
#include "gsl_sf_exp.h" |
| 28 |
#include "gsl_sf_pow_int.h" |
| 29 |
#include "gsl_sf_legendre.h" |
| 30 |
|
| 31 |
#include "gsl_specfunc__error.h" |
| 32 |
|
| 33 |
/* Evaluate f_{ell+1}/f_ell |
| 34 |
* f_ell := Q^{b}_{a+ell}(x) |
| 35 |
* x > 1 |
| 36 |
*/ |
| 37 |
static |
| 38 |
int |
| 39 |
legendreQ_CF1_xgt1(int ell, double a, double b, double x, double * result) |
| 40 |
{ |
| 41 |
const double RECUR_BIG = GSL_SQRT_DBL_MAX; |
| 42 |
const int maxiter = 5000; |
| 43 |
int n = 1; |
| 44 |
double Anm2 = 1.0; |
| 45 |
double Bnm2 = 0.0; |
| 46 |
double Anm1 = 0.0; |
| 47 |
double Bnm1 = 1.0; |
| 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; |
| 52 |
double an, bn; |
| 53 |
double fn = An/Bn; |
| 54 |
|
| 55 |
while(n < maxiter) { |
| 56 |
double old_fn; |
| 57 |
double del; |
| 58 |
double lna; |
| 59 |
n++; |
| 60 |
Anm2 = Anm1; |
| 61 |
Bnm2 = Bnm1; |
| 62 |
Anm1 = An; |
| 63 |
Bnm1 = Bn; |
| 64 |
lna = ell + n + a; |
| 65 |
an = b*b - lna*lna; |
| 66 |
bn = (2.0*lna + 1.0) * x; |
| 67 |
An = bn*Anm1 + an*Anm2; |
| 68 |
Bn = bn*Bnm1 + an*Bnm2; |
| 69 |
|
| 70 |
if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) { |
| 71 |
An /= RECUR_BIG; |
| 72 |
Bn /= RECUR_BIG; |
| 73 |
Anm1 /= RECUR_BIG; |
| 74 |
Bnm1 /= RECUR_BIG; |
| 75 |
Anm2 /= RECUR_BIG; |
| 76 |
Bnm2 /= RECUR_BIG; |
| 77 |
} |
| 78 |
|
| 79 |
old_fn = fn; |
| 80 |
fn = An/Bn; |
| 81 |
del = old_fn/fn; |
| 82 |
|
| 83 |
if(fabs(del - 1.0) < 4.0*GSL_DBL_EPSILON) break; |
| 84 |
} |
| 85 |
|
| 86 |
*result = fn; |
| 87 |
|
| 88 |
if(n == maxiter) |
| 89 |
GSL_ERROR ("error", GSL_EMAXITER); |
| 90 |
else |
| 91 |
return GSL_SUCCESS; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
/* Uniform asymptotic for Q_l(x). |
| 96 |
* Assumes x > -1.0 and x != 1.0. |
| 97 |
* Discards second order and higher terms. |
| 98 |
*/ |
| 99 |
static |
| 100 |
int |
| 101 |
legendre_Ql_asymp_unif(const double ell, const double x, gsl_sf_result * result) |
| 102 |
{ |
| 103 |
if(x < 1.0) { |
| 104 |
double u = ell + 0.5; |
| 105 |
double th = acos(x); |
| 106 |
gsl_sf_result Y0, Y1; |
| 107 |
int stat_Y0, stat_Y1; |
| 108 |
int stat_m; |
| 109 |
double pre; |
| 110 |
double B00; |
| 111 |
double sum; |
| 112 |
|
| 113 |
/* B00 = 1/8 (1 - th cot(th) / th^2 |
| 114 |
* pre = sqrt(th/sin(th)) |
| 115 |
*/ |
| 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; |
| 119 |
} |
| 120 |
else { |
| 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); |
| 125 |
} |
| 126 |
|
| 127 |
stat_Y0 = gsl_sf_bessel_Y0_e(u*th, &Y0); |
| 128 |
stat_Y1 = gsl_sf_bessel_Y1_e(u*th, &Y1); |
| 129 |
|
| 130 |
sum = -0.5*M_PI * (Y0.val + th/u * Y1.val * B00); |
| 131 |
|
| 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); |
| 135 |
|
| 136 |
return GSL_ERROR_SELECT_3(stat_m, stat_Y0, stat_Y1); |
| 137 |
} |
| 138 |
else { |
| 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; |
| 143 |
int stat_e; |
| 144 |
double pre; |
| 145 |
double B00; |
| 146 |
double sum; |
| 147 |
|
| 148 |
/* B00 = -1/8 (1 - xi coth(xi) / xi^2 |
| 149 |
* pre = sqrt(xi/sinh(xi)) |
| 150 |
*/ |
| 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; |
| 154 |
} |
| 155 |
else { |
| 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); |
| 160 |
} |
| 161 |
|
| 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); |
| 164 |
|
| 165 |
sum = K0_scaled.val - xi/u * K1_scaled.val * B00; |
| 166 |
|
| 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); |
| 170 |
|
| 171 |
return GSL_ERROR_SELECT_3(stat_e, stat_K0, stat_K1); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
/*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/ |
| 178 |
|
| 179 |
int |
| 180 |
gsl_sf_legendre_Q0_e(const double x, gsl_sf_result * result) |
| 181 |
{ |
| 182 |
/* CHECK_POINTER(result) */ |
| 183 |
|
| 184 |
if(x <= -1.0 || x == 1.0) { |
| 185 |
DOMAIN_ERROR(result); |
| 186 |
} |
| 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); |
| 197 |
return GSL_SUCCESS; |
| 198 |
} |
| 199 |
else if(x < 1.0) { |
| 200 |
result->val = 0.5 * log((1.0+x)/(1.0-x)); |
| 201 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 202 |
return GSL_SUCCESS; |
| 203 |
} |
| 204 |
else if(x < 10.0) { |
| 205 |
result->val = 0.5 * log((x+1.0)/(x-1.0)); |
| 206 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 207 |
return GSL_SUCCESS; |
| 208 |
} |
| 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); |
| 220 |
return GSL_SUCCESS; |
| 221 |
} |
| 222 |
else { |
| 223 |
UNDERFLOW_ERROR(result); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
int |
| 229 |
gsl_sf_legendre_Q1_e(const double x, gsl_sf_result * result) |
| 230 |
{ |
| 231 |
/* CHECK_POINTER(result) */ |
| 232 |
|
| 233 |
if(x <= -1.0 || x == 1.0) { |
| 234 |
DOMAIN_ERROR(result); |
| 235 |
} |
| 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); |
| 246 |
return GSL_SUCCESS; |
| 247 |
} |
| 248 |
else if(x < 1.0){ |
| 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); |
| 251 |
return GSL_SUCCESS; |
| 252 |
} |
| 253 |
else if(x < 6.0) { |
| 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); |
| 256 |
return GSL_SUCCESS; |
| 257 |
} |
| 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); |
| 271 |
return GSL_SUCCESS; |
| 272 |
} |
| 273 |
else { |
| 274 |
UNDERFLOW_ERROR(result); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
int |
| 280 |
gsl_sf_legendre_Ql_e(const int l, const double x, gsl_sf_result * result) |
| 281 |
{ |
| 282 |
/* CHECK_POINTER(result) */ |
| 283 |
|
| 284 |
if(x <= -1.0 || x == 1.0 || l < 0) { |
| 285 |
DOMAIN_ERROR(result); |
| 286 |
} |
| 287 |
else if(l == 0) { |
| 288 |
return gsl_sf_legendre_Q0_e(x, result); |
| 289 |
} |
| 290 |
else if(l == 1) { |
| 291 |
return gsl_sf_legendre_Q1_e(x, result); |
| 292 |
} |
| 293 |
else if(l > 100000) { |
| 294 |
return legendre_Ql_asymp_unif(l, x, result); |
| 295 |
} |
| 296 |
else if(x < 1.0){ |
| 297 |
/* Forward recurrence. |
| 298 |
*/ |
| 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; |
| 304 |
double Qellp1; |
| 305 |
int ell; |
| 306 |
for(ell=1; ell<l; ell++) { |
| 307 |
Qellp1 = (x*(2.0*ell + 1.0) * Qell - ell * Qellm1) / (ell + 1.0); |
| 308 |
Qellm1 = Qell; |
| 309 |
Qell = Qellp1; |
| 310 |
} |
| 311 |
result->val = Qell; |
| 312 |
result->err = GSL_DBL_EPSILON * l * fabs(result->val); |
| 313 |
return GSL_ERROR_SELECT_2(stat_Q0, stat_Q1); |
| 314 |
} |
| 315 |
else { |
| 316 |
/* x > 1.0 */ |
| 317 |
|
| 318 |
double rat; |
| 319 |
int stat_CF1 = legendreQ_CF1_xgt1(l, 0.0, 0.0, x, &rat); |
| 320 |
int stat_Q; |
| 321 |
double Qellp1 = rat * GSL_SQRT_DBL_MIN; |
| 322 |
double Qell = GSL_SQRT_DBL_MIN; |
| 323 |
double Qellm1; |
| 324 |
int ell; |
| 325 |
for(ell=l; ell>0; ell--) { |
| 326 |
Qellm1 = (x * (2.0*ell + 1.0) * Qell - (ell+1.0) * Qellp1) / ell; |
| 327 |
Qellp1 = Qell; |
| 328 |
Qell = Qellm1; |
| 329 |
} |
| 330 |
|
| 331 |
if(fabs(Qell) > fabs(Qellp1)) { |
| 332 |
gsl_sf_result Q0; |
| 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); |
| 336 |
} |
| 337 |
else { |
| 338 |
gsl_sf_result Q1; |
| 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); |
| 342 |
} |
| 343 |
|
| 344 |
return GSL_ERROR_SELECT_2(stat_Q, stat_CF1); |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
|
| 349 |
/*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/ |
| 350 |
|
| 351 |
#include "gsl_specfunc__eval.h" |
| 352 |
|
| 353 |
double gsl_sf_legendre_Q0(const double x) |
| 354 |
{ |
| 355 |
EVAL_RESULT(gsl_sf_legendre_Q0_e(x, &result)); |
| 356 |
} |
| 357 |
|
| 358 |
double gsl_sf_legendre_Q1(const double x) |
| 359 |
{ |
| 360 |
EVAL_RESULT(gsl_sf_legendre_Q1_e(x, &result)); |
| 361 |
} |
| 362 |
|
| 363 |
double gsl_sf_legendre_Ql(const int l, const double x) |
| 364 |
{ |
| 365 |
EVAL_RESULT(gsl_sf_legendre_Ql_e(l, x, &result)); |
| 366 |
} |