| 1 |
/* specfunc/bessel_j.c |
| 2 |
* |
| 3 |
* Copyright (C) 1996,1997,1998,1999,2000,2001,2002,2003 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_pow_int.h" |
| 26 |
#include "gsl_sf_trig.h" |
| 27 |
#include "gsl_sf_bessel.h" |
| 28 |
|
| 29 |
#include "gsl_specfunc__error.h" |
| 30 |
|
| 31 |
#include "gsl_specfunc__bessel.h" |
| 32 |
#include "gsl_specfunc__bessel_olver.h" |
| 33 |
|
| 34 |
/*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/ |
| 35 |
|
| 36 |
int gsl_sf_bessel_j0_e(const double x, gsl_sf_result * result) |
| 37 |
{ |
| 38 |
double ax = fabs(x); |
| 39 |
|
| 40 |
/* CHECK_POINTER(result) */ |
| 41 |
|
| 42 |
if(ax < 0.5) { |
| 43 |
const double y = x*x; |
| 44 |
const double c1 = -1.0/6.0; |
| 45 |
const double c2 = 1.0/120.0; |
| 46 |
const double c3 = -1.0/5040.0; |
| 47 |
const double c4 = 1.0/362880.0; |
| 48 |
const double c5 = -1.0/39916800.0; |
| 49 |
const double c6 = 1.0/6227020800.0; |
| 50 |
result->val = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*(c5 + y*c6))))); |
| 51 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 52 |
return GSL_SUCCESS; |
| 53 |
} |
| 54 |
else { |
| 55 |
gsl_sf_result sin_result; |
| 56 |
const int stat = gsl_sf_sin_e(x, &sin_result); |
| 57 |
result->val = sin_result.val/x; |
| 58 |
result->err = fabs(sin_result.err/x); |
| 59 |
result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 60 |
return stat; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
int gsl_sf_bessel_j1_e(const double x, gsl_sf_result * result) |
| 66 |
{ |
| 67 |
double ax = fabs(x); |
| 68 |
|
| 69 |
/* CHECK_POINTER(result) */ |
| 70 |
|
| 71 |
if(x == 0.0) { |
| 72 |
result->val = 0.0; |
| 73 |
result->err = 0.0; |
| 74 |
return GSL_SUCCESS; |
| 75 |
} |
| 76 |
else if(ax < 3.1*GSL_DBL_MIN) { |
| 77 |
UNDERFLOW_ERROR(result); |
| 78 |
} |
| 79 |
else if(ax < 0.25) { |
| 80 |
const double y = x*x; |
| 81 |
const double c1 = -1.0/10.0; |
| 82 |
const double c2 = 1.0/280.0; |
| 83 |
const double c3 = -1.0/15120.0; |
| 84 |
const double c4 = 1.0/1330560.0; |
| 85 |
const double c5 = -1.0/172972800.0; |
| 86 |
const double sum = 1.0 + y*(c1 + y*(c2 + y*(c3 + y*(c4 + y*c5)))); |
| 87 |
result->val = x/3.0 * sum; |
| 88 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 89 |
return GSL_SUCCESS; |
| 90 |
} |
| 91 |
else { |
| 92 |
gsl_sf_result cos_result; |
| 93 |
gsl_sf_result sin_result; |
| 94 |
const int stat_cos = gsl_sf_cos_e(x, &cos_result); |
| 95 |
const int stat_sin = gsl_sf_sin_e(x, &sin_result); |
| 96 |
const double cos_x = cos_result.val; |
| 97 |
const double sin_x = sin_result.val; |
| 98 |
result->val = (sin_x/x - cos_x)/x; |
| 99 |
result->err = (fabs(sin_result.err/x) + fabs(cos_result.err))/fabs(x); |
| 100 |
result->err += 2.0 * GSL_DBL_EPSILON * (fabs(sin_x/(x*x)) + fabs(cos_x/x)); |
| 101 |
result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 102 |
return GSL_ERROR_SELECT_2(stat_cos, stat_sin); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
int gsl_sf_bessel_j2_e(const double x, gsl_sf_result * result) |
| 108 |
{ |
| 109 |
double ax = fabs(x); |
| 110 |
|
| 111 |
/* CHECK_POINTER(result) */ |
| 112 |
|
| 113 |
if(x == 0.0) { |
| 114 |
result->val = 0.0; |
| 115 |
result->err = 0.0; |
| 116 |
return GSL_SUCCESS; |
| 117 |
} |
| 118 |
else if(ax < 4.0*GSL_SQRT_DBL_MIN) { |
| 119 |
UNDERFLOW_ERROR(result); |
| 120 |
} |
| 121 |
else if(ax < 1.3) { |
| 122 |
const double y = x*x; |
| 123 |
const double c1 = -1.0/14.0; |
| 124 |
const double c2 = 1.0/504.0; |
| 125 |
const double c3 = -1.0/33264.0; |
| 126 |
const double c4 = 1.0/3459456.0; |
| 127 |
const double c5 = -1.0/518918400; |
| 128 |
const double c6 = 1.0/105859353600.0; |
| 129 |
const double c7 = -1.0/28158588057600.0; |
| 130 |
const double c8 = 1.0/9461285587353600.0; |
| 131 |
const double c9 = -1.0/3916972233164390400.0; |
| 132 |
const double sum = 1.0+y*(c1+y*(c2+y*(c3+y*(c4+y*(c5+y*(c6+y*(c7+y*(c8+y*c9)))))))); |
| 133 |
result->val = y/15.0 * sum; |
| 134 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 135 |
return GSL_SUCCESS; |
| 136 |
} |
| 137 |
else { |
| 138 |
gsl_sf_result cos_result; |
| 139 |
gsl_sf_result sin_result; |
| 140 |
const int stat_cos = gsl_sf_cos_e(x, &cos_result); |
| 141 |
const int stat_sin = gsl_sf_sin_e(x, &sin_result); |
| 142 |
const double cos_x = cos_result.val; |
| 143 |
const double sin_x = sin_result.val; |
| 144 |
const double f = (3.0/(x*x) - 1.0); |
| 145 |
result->val = (f * sin_x - 3.0*cos_x/x)/x; |
| 146 |
result->err = fabs(f * sin_result.err/x) + fabs((3.0*cos_result.err/x)/x); |
| 147 |
result->err += 2.0 * GSL_DBL_EPSILON * (fabs(f*sin_x/x) + 3.0*fabs(cos_x/(x*x))); |
| 148 |
result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 149 |
return GSL_ERROR_SELECT_2(stat_cos, stat_sin); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
int |
| 155 |
gsl_sf_bessel_jl_e(const int l, const double x, gsl_sf_result * result) |
| 156 |
{ |
| 157 |
if(l < 0 || x < 0.0) { |
| 158 |
DOMAIN_ERROR(result); |
| 159 |
} |
| 160 |
else if(x == 0.0) { |
| 161 |
result->val = ( l > 0 ? 0.0 : 1.0 ); |
| 162 |
result->err = 0.0; |
| 163 |
return GSL_SUCCESS; |
| 164 |
} |
| 165 |
else if(l == 0) { |
| 166 |
return gsl_sf_bessel_j0_e(x, result); |
| 167 |
} |
| 168 |
else if(l == 1) { |
| 169 |
return gsl_sf_bessel_j1_e(x, result); |
| 170 |
} |
| 171 |
else if(l == 2) { |
| 172 |
return gsl_sf_bessel_j2_e(x, result); |
| 173 |
} |
| 174 |
else if(x*x < 10.0*(l+0.5)/M_E) { |
| 175 |
gsl_sf_result b; |
| 176 |
int status = gsl_sf_bessel_IJ_taylor_e(l+0.5, x, -1, 50, GSL_DBL_EPSILON, &b); |
| 177 |
double pre = sqrt((0.5*M_PI)/x); |
| 178 |
result->val = pre * b.val; |
| 179 |
result->err = pre * b.err; |
| 180 |
result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 181 |
return status; |
| 182 |
} |
| 183 |
else if(GSL_ROOT4_DBL_EPSILON * x > (l*l + l + 1.0)) { |
| 184 |
gsl_sf_result b; |
| 185 |
int status = gsl_sf_bessel_Jnu_asympx_e(l + 0.5, x, &b); |
| 186 |
double pre = sqrt((0.5*M_PI)/x); |
| 187 |
result->val = pre * b.val; |
| 188 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err; |
| 189 |
return status; |
| 190 |
} |
| 191 |
else if(l > 1.0/GSL_ROOT6_DBL_EPSILON) { |
| 192 |
gsl_sf_result b; |
| 193 |
int status = gsl_sf_bessel_Jnu_asymp_Olver_e(l + 0.5, x, &b); |
| 194 |
double pre = sqrt((0.5*M_PI)/x); |
| 195 |
result->val = pre * b.val; |
| 196 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err; |
| 197 |
return status; |
| 198 |
} |
| 199 |
else if(x > 1000.0 && x > 100.0*l*l) |
| 200 |
{ |
| 201 |
/* We need this to avoid feeding large x to CF1; note that |
| 202 |
* due to the above check, we know that n <= 50. |
| 203 |
*/ |
| 204 |
gsl_sf_result b; |
| 205 |
int status = gsl_sf_bessel_Jnu_asympx_e(l + 0.5, x, &b); |
| 206 |
double pre = sqrt((0.5*M_PI)/x); |
| 207 |
result->val = pre * b.val; |
| 208 |
result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * b.err; |
| 209 |
return status; |
| 210 |
} |
| 211 |
else { |
| 212 |
double sgn; |
| 213 |
double ratio; |
| 214 |
int stat_CF1 = gsl_sf_bessel_J_CF1(l+0.5, x, &ratio, &sgn); |
| 215 |
double jellp1 = GSL_SQRT_DBL_EPSILON * ratio; |
| 216 |
double jell = GSL_SQRT_DBL_EPSILON; |
| 217 |
double jellm1; |
| 218 |
int ell; |
| 219 |
for(ell = l; ell > 0; ell--) { |
| 220 |
jellm1 = -jellp1 + (2*ell + 1)/x * jell; |
| 221 |
jellp1 = jell; |
| 222 |
jell = jellm1; |
| 223 |
} |
| 224 |
|
| 225 |
if(fabs(jell) > fabs(jellp1)) { |
| 226 |
gsl_sf_result j0_result; |
| 227 |
int stat_j0 = gsl_sf_bessel_j0_e(x, &j0_result); |
| 228 |
double pre = GSL_SQRT_DBL_EPSILON / jell; |
| 229 |
result->val = j0_result.val * pre; |
| 230 |
result->err = j0_result.err * fabs(pre); |
| 231 |
result->err += 2.0 * GSL_DBL_EPSILON * (0.5*l + 1.0) * fabs(result->val); |
| 232 |
return GSL_ERROR_SELECT_2(stat_j0, stat_CF1); |
| 233 |
} |
| 234 |
else { |
| 235 |
gsl_sf_result j1_result; |
| 236 |
int stat_j1 = gsl_sf_bessel_j1_e(x, &j1_result); |
| 237 |
double pre = GSL_SQRT_DBL_EPSILON / jellp1; |
| 238 |
result->val = j1_result.val * pre; |
| 239 |
result->err = j1_result.err * fabs(pre); |
| 240 |
result->err += 2.0 * GSL_DBL_EPSILON * (0.5*l + 1.0) * fabs(result->val); |
| 241 |
return GSL_ERROR_SELECT_2(stat_j1, stat_CF1); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
int |
| 248 |
gsl_sf_bessel_jl_array(const int lmax, const double x, double * result_array) |
| 249 |
{ |
| 250 |
/* CHECK_POINTER(result_array) */ |
| 251 |
|
| 252 |
if(lmax < 0 || x < 0.0) { |
| 253 |
int j; |
| 254 |
for(j=0; j<=lmax; j++) result_array[j] = 0.0; |
| 255 |
GSL_ERROR ("error", GSL_EDOM); |
| 256 |
} |
| 257 |
else if(x == 0.0) { |
| 258 |
int j; |
| 259 |
for(j=1; j<=lmax; j++) result_array[j] = 0.0; |
| 260 |
result_array[0] = 1.0; |
| 261 |
return GSL_SUCCESS; |
| 262 |
} |
| 263 |
else { |
| 264 |
gsl_sf_result r_jellp1; |
| 265 |
gsl_sf_result r_jell; |
| 266 |
int stat_0 = gsl_sf_bessel_jl_e(lmax+1, x, &r_jellp1); |
| 267 |
int stat_1 = gsl_sf_bessel_jl_e(lmax, x, &r_jell); |
| 268 |
double jellp1 = r_jellp1.val; |
| 269 |
double jell = r_jell.val; |
| 270 |
double jellm1; |
| 271 |
int ell; |
| 272 |
|
| 273 |
result_array[lmax] = jell; |
| 274 |
for(ell = lmax; ell >= 1; ell--) { |
| 275 |
jellm1 = -jellp1 + (2*ell + 1)/x * jell; |
| 276 |
jellp1 = jell; |
| 277 |
jell = jellm1; |
| 278 |
result_array[ell-1] = jellm1; |
| 279 |
} |
| 280 |
|
| 281 |
return GSL_ERROR_SELECT_2(stat_0, stat_1); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
int gsl_sf_bessel_jl_steed_array(const int lmax, const double x, double * jl_x) |
| 287 |
{ |
| 288 |
/* CHECK_POINTER(jl_x) */ |
| 289 |
|
| 290 |
if(lmax < 0 || x < 0.0) { |
| 291 |
int j; |
| 292 |
for(j=0; j<=lmax; j++) jl_x[j] = 0.0; |
| 293 |
GSL_ERROR ("error", GSL_EDOM); |
| 294 |
} |
| 295 |
else if(x == 0.0) { |
| 296 |
int j; |
| 297 |
for(j=1; j<=lmax; j++) jl_x[j] = 0.0; |
| 298 |
jl_x[0] = 1.0; |
| 299 |
return GSL_SUCCESS; |
| 300 |
} |
| 301 |
else if(x < 2.0*GSL_ROOT4_DBL_EPSILON) { |
| 302 |
/* first two terms of Taylor series */ |
| 303 |
double inv_fact = 1.0; /* 1/(1 3 5 ... (2l+1)) */ |
| 304 |
double x_l = 1.0; /* x^l */ |
| 305 |
int l; |
| 306 |
for(l=0; l<=lmax; l++) { |
| 307 |
jl_x[l] = x_l * inv_fact; |
| 308 |
jl_x[l] *= 1.0 - 0.5*x*x/(2.0*l+3.0); |
| 309 |
inv_fact /= 2.0*l+3.0; |
| 310 |
x_l *= x; |
| 311 |
} |
| 312 |
return GSL_SUCCESS; |
| 313 |
} |
| 314 |
else { |
| 315 |
/* Steed/Barnett algorithm [Comp. Phys. Comm. 21, 297 (1981)] */ |
| 316 |
double x_inv = 1.0/x; |
| 317 |
double W = 2.0*x_inv; |
| 318 |
double F = 1.0; |
| 319 |
double FP = (lmax+1.0) * x_inv; |
| 320 |
double B = 2.0*FP + x_inv; |
| 321 |
double end = B + 20000.0*W; |
| 322 |
double D = 1.0/B; |
| 323 |
double del = -D; |
| 324 |
|
| 325 |
FP += del; |
| 326 |
|
| 327 |
/* continued fraction */ |
| 328 |
do { |
| 329 |
B += W; |
| 330 |
D = 1.0/(B-D); |
| 331 |
del *= (B*D - 1.); |
| 332 |
FP += del; |
| 333 |
if(D < 0.0) F = -F; |
| 334 |
if(B > end) { |
| 335 |
GSL_ERROR ("error", GSL_EMAXITER); |
| 336 |
} |
| 337 |
} |
| 338 |
while(fabs(del) >= fabs(FP) * GSL_DBL_EPSILON); |
| 339 |
|
| 340 |
FP *= F; |
| 341 |
|
| 342 |
if(lmax > 0) { |
| 343 |
/* downward recursion */ |
| 344 |
double XP2 = FP; |
| 345 |
double PL = lmax * x_inv; |
| 346 |
int L = lmax; |
| 347 |
int LP; |
| 348 |
jl_x[lmax] = F; |
| 349 |
for(LP = 1; LP<=lmax; LP++) { |
| 350 |
jl_x[L-1] = PL * jl_x[L] + XP2; |
| 351 |
FP = PL*jl_x[L-1] - jl_x[L]; |
| 352 |
XP2 = FP; |
| 353 |
PL -= x_inv; |
| 354 |
--L; |
| 355 |
} |
| 356 |
F = jl_x[0]; |
| 357 |
} |
| 358 |
|
| 359 |
/* normalization */ |
| 360 |
W = x_inv / hypot(FP, F); |
| 361 |
jl_x[0] = W*F; |
| 362 |
if(lmax > 0) { |
| 363 |
int L; |
| 364 |
for(L=1; L<=lmax; L++) { |
| 365 |
jl_x[L] *= W; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
return GSL_SUCCESS; |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
/*-*-*-*-*-*-*-*-*-* Functions w/ Natural Prototypes *-*-*-*-*-*-*-*-*-*-*/ |
| 375 |
|
| 376 |
#include "gsl_specfunc__eval.h" |
| 377 |
|
| 378 |
double gsl_sf_bessel_j0(const double x) |
| 379 |
{ |
| 380 |
EVAL_RESULT(gsl_sf_bessel_j0_e(x, &result)); |
| 381 |
} |
| 382 |
|
| 383 |
double gsl_sf_bessel_j1(const double x) |
| 384 |
{ |
| 385 |
EVAL_RESULT(gsl_sf_bessel_j1_e(x, &result)); |
| 386 |
} |
| 387 |
|
| 388 |
double gsl_sf_bessel_j2(const double x) |
| 389 |
{ |
| 390 |
EVAL_RESULT(gsl_sf_bessel_j2_e(x, &result)); |
| 391 |
} |
| 392 |
|
| 393 |
double gsl_sf_bessel_jl(const int l, const double x) |
| 394 |
{ |
| 395 |
EVAL_RESULT(gsl_sf_bessel_jl_e(l, x, &result)); |
| 396 |
} |
| 397 |
|