| 1 | /* specfunc/bessel.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 | /* Miscellaneous support functions for Bessel function evaluations. |
| 22 | */ |
| 23 | #include "gsl__config.h" |
| 24 | #include "gsl_math.h" |
| 25 | #include "gsl_errno.h" |
| 26 | #include "gsl_sf_airy.h" |
| 27 | #include "gsl_sf_elementary.h" |
| 28 | #include "gsl_sf_exp.h" |
| 29 | #include "gsl_sf_gamma.h" |
| 30 | #include "gsl_sf_trig.h" |
| 31 | |
| 32 | #include "gsl_specfunc__error.h" |
| 33 | |
| 34 | #include "gsl_specfunc__bessel_amp_phase.h" |
| 35 | #include "gsl_specfunc__bessel_temme.h" |
| 36 | #include "gsl_specfunc__bessel.h" |
| 37 | |
| 38 | #define CubeRoot2_ 1.25992104989487316476721060728 |
| 39 | |
| 40 | |
| 41 | |
| 42 | /* Debye functions [Abramowitz+Stegun, 9.3.9-10] */ |
| 43 | |
| 44 | inline static double |
| 45 | debye_u1(const double * tpow) |
| 46 | { |
| 47 | return (3.0*tpow[1] - 5.0*tpow[3])/24.0; |
| 48 | } |
| 49 | |
| 50 | inline static double |
| 51 | debye_u2(const double * tpow) |
| 52 | { |
| 53 | return (81.0*tpow[2] - 462.0*tpow[4] + 385.0*tpow[6])/1152.0; |
| 54 | } |
| 55 | |
| 56 | inline |
| 57 | static double debye_u3(const double * tpow) |
| 58 | { |
| 59 | return (30375.0*tpow[3] - 369603.0*tpow[5] + 765765.0*tpow[7] - 425425.0*tpow[9])/414720.0; |
| 60 | } |
| 61 | |
| 62 | inline |
| 63 | static double debye_u4(const double * tpow) |
| 64 | { |
| 65 | return (4465125.0*tpow[4] - 94121676.0*tpow[6] + 349922430.0*tpow[8] - |
| 66 | 446185740.0*tpow[10] + 185910725.0*tpow[12])/39813120.0; |
| 67 | } |
| 68 | |
| 69 | inline |
| 70 | static double debye_u5(const double * tpow) |
| 71 | { |
| 72 | return (1519035525.0*tpow[5] - 49286948607.0*tpow[7] + |
| 73 | 284499769554.0*tpow[9] - 614135872350.0*tpow[11] + |
| 74 | 566098157625.0*tpow[13] - 188699385875.0*tpow[15])/6688604160.0; |
| 75 | } |
| 76 | |
| 77 | #if 0 |
| 78 | inline |
| 79 | static double debye_u6(const double * tpow) |
| 80 | { |
| 81 | return (2757049477875.0*tpow[6] - 127577298354750.0*tpow[8] + |
| 82 | 1050760774457901.0*tpow[10] - 3369032068261860.0*tpow[12] + |
| 83 | 5104696716244125.0*tpow[14] - 3685299006138750.0*tpow[16] + |
| 84 | 1023694168371875.0*tpow[18])/4815794995200.0; |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | |
| 89 | /*-*-*-*-*-*-*-*-*-*-*-* Functions with Error Codes *-*-*-*-*-*-*-*-*-*-*-*/ |
| 90 | |
| 91 | int |
| 92 | gsl_sf_bessel_IJ_taylor_e(const double nu, const double x, |
| 93 | const int sign, |
| 94 | const int kmax, |
| 95 | const double threshold, |
| 96 | gsl_sf_result * result |
| 97 | ) |
| 98 | { |
| 99 | /* CHECK_POINTER(result) */ |
| 100 | |
| 101 | if(nu < 0.0 || x < 0.0) { |
| 102 | DOMAIN_ERROR(result); |
| 103 | } |
| 104 | else if(x == 0.0) { |
| 105 | if(nu == 0.0) { |
| 106 | result->val = 1.0; |
| 107 | result->err = 0.0; |
| 108 | } |
| 109 | else { |
| 110 | result->val = 0.0; |
| 111 | result->err = 0.0; |
| 112 | } |
| 113 | return GSL_SUCCESS; |
| 114 | } |
| 115 | else { |
| 116 | gsl_sf_result prefactor; /* (x/2)^nu / Gamma(nu+1) */ |
| 117 | gsl_sf_result sum; |
| 118 | |
| 119 | int stat_pre; |
| 120 | int stat_sum; |
| 121 | int stat_mul; |
| 122 | |
| 123 | if(nu == 0.0) { |
| 124 | prefactor.val = 1.0; |
| 125 | prefactor.err = 0.0; |
| 126 | stat_pre = GSL_SUCCESS; |
| 127 | } |
| 128 | else if(nu < INT_MAX-1) { |
| 129 | /* Separate the integer part and use |
| 130 | * y^nu / Gamma(nu+1) = y^N /N! y^f / (N+1)_f, |
| 131 | * to control the error. |
| 132 | */ |
| 133 | const int N = (int)floor(nu + 0.5); |
| 134 | const double f = nu - N; |
| 135 | gsl_sf_result poch_factor; |
| 136 | gsl_sf_result tc_factor; |
| 137 | const int stat_poch = gsl_sf_poch_e(N+1.0, f, &poch_factor); |
| 138 | const int stat_tc = gsl_sf_taylorcoeff_e(N, 0.5*x, &tc_factor); |
| 139 | const double p = pow(0.5*x,f); |
| 140 | prefactor.val = tc_factor.val * p / poch_factor.val; |
| 141 | prefactor.err = tc_factor.err * p / poch_factor.val; |
| 142 | prefactor.err += fabs(prefactor.val) / poch_factor.val * poch_factor.err; |
| 143 | prefactor.err += 2.0 * GSL_DBL_EPSILON * fabs(prefactor.val); |
| 144 | stat_pre = GSL_ERROR_SELECT_2(stat_tc, stat_poch); |
| 145 | } |
| 146 | else { |
| 147 | gsl_sf_result lg; |
| 148 | const int stat_lg = gsl_sf_lngamma_e(nu+1.0, &lg); |
| 149 | const double term1 = nu*log(0.5*x); |
| 150 | const double term2 = lg.val; |
| 151 | const double ln_pre = term1 - term2; |
| 152 | const double ln_pre_err = GSL_DBL_EPSILON * (fabs(term1)+fabs(term2)) + lg.err; |
| 153 | const int stat_ex = gsl_sf_exp_err_e(ln_pre, ln_pre_err, &prefactor); |
| 154 | stat_pre = GSL_ERROR_SELECT_2(stat_ex, stat_lg); |
| 155 | } |
| 156 | |
| 157 | /* Evaluate the sum. |
| 158 | * [Abramowitz+Stegun, 9.1.10] |
| 159 | * [Abramowitz+Stegun, 9.6.7] |
| 160 | */ |
| 161 | { |
| 162 | const double y = sign * 0.25 * x*x; |
| 163 | double sumk = 1.0; |
| 164 | double term = 1.0; |
| 165 | int k; |
| 166 | |
| 167 | for(k=1; k<=kmax; k++) { |
| 168 | term *= y/((nu+k)*k); |
| 169 | sumk += term; |
| 170 | if(fabs(term/sumk) < threshold) break; |
| 171 | } |
| 172 | |
| 173 | sum.val = sumk; |
| 174 | sum.err = threshold * fabs(sumk); |
| 175 | |
| 176 | stat_sum = ( k >= kmax ? GSL_EMAXITER : GSL_SUCCESS ); |
| 177 | } |
| 178 | |
| 179 | stat_mul = gsl_sf_multiply_err_e(prefactor.val, prefactor.err, |
| 180 | sum.val, sum.err, |
| 181 | result); |
| 182 | |
| 183 | return GSL_ERROR_SELECT_3(stat_mul, stat_pre, stat_sum); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /* x >> nu*nu+1 |
| 189 | * error ~ O( ((nu*nu+1)/x)^4 ) |
| 190 | * |
| 191 | * empirical error analysis: |
| 192 | * choose GSL_ROOT4_MACH_EPS * x > (nu*nu + 1) |
| 193 | * |
| 194 | * This is not especially useful. When the argument gets |
| 195 | * large enough for this to apply, the cos() and sin() |
| 196 | * start loosing digits. However, this seems inevitable |
| 197 | * for this particular method. |
| 198 | * |
| 199 | * Wed Jun 25 14:39:38 MDT 2003 [GJ] |
| 200 | * This function was inconsistent since the Q term did not |
| 201 | * go to relative order eps^2. That's why the error estimate |
| 202 | * originally given was screwy (it didn't make sense that the |
| 203 | * "empirical" error was coming out O(eps^3)). |
| 204 | * With Q to proper order, the error is O(eps^4). |
| 205 | */ |
| 206 | int |
| 207 | gsl_sf_bessel_Jnu_asympx_e(const double nu, const double x, gsl_sf_result * result) |
| 208 | { |
| 209 | double mu = 4.0*nu*nu; |
| 210 | double mum1 = mu-1.0; |
| 211 | double mum9 = mu-9.0; |
| 212 | double mum25 = mu-25.0; |
| 213 | double chi = x - (0.5*nu + 0.25)*M_PI; |
| 214 | double P = 1.0 - mum1*mum9/(128.0*x*x); |
| 215 | double Q = mum1/(8.0*x) * (1.0 - mum9*mum25/(384.0*x*x)); |
| 216 | double pre = sqrt(2.0/(M_PI*x)); |
| 217 | double c = cos(chi); |
| 218 | double s = sin(chi); |
| 219 | double r = mu/x; |
| 220 | result->val = pre * (c*P - s*Q); |
| 221 | result->err = pre * GSL_DBL_EPSILON * (1.0 + fabs(x)) * (fabs(c*P) + fabs(s*Q)); |
| 222 | result->err += pre * fabs(0.1*r*r*r*r); |
| 223 | return GSL_SUCCESS; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | /* x >> nu*nu+1 |
| 228 | */ |
| 229 | int |
| 230 | gsl_sf_bessel_Ynu_asympx_e(const double nu, const double x, gsl_sf_result * result) |
| 231 | { |
| 232 | double ampl; |
| 233 | double theta; |
| 234 | double alpha = x; |
| 235 | double beta = -0.5*nu*M_PI; |
| 236 | int stat_a = gsl_sf_bessel_asymp_Mnu_e(nu, x, &l); |
| 237 | int stat_t = gsl_sf_bessel_asymp_thetanu_corr_e(nu, x, &theta); |
| 238 | double sin_alpha = sin(alpha); |
| 239 | double cos_alpha = cos(alpha); |
| 240 | double sin_chi = sin(beta + theta); |
| 241 | double cos_chi = cos(beta + theta); |
| 242 | double sin_term = sin_alpha * cos_chi + sin_chi * cos_alpha; |
| 243 | double sin_term_mag = fabs(sin_alpha * cos_chi) + fabs(sin_chi * cos_alpha); |
| 244 | result->val = ampl * sin_term; |
| 245 | result->err = fabs(ampl) * GSL_DBL_EPSILON * sin_term_mag; |
| 246 | result->err += fabs(result->val) * 2.0 * GSL_DBL_EPSILON; |
| 247 | |
| 248 | if(fabs(alpha) > 1.0/GSL_DBL_EPSILON) { |
| 249 | result->err *= 0.5 * fabs(alpha); |
| 250 | } |
| 251 | else if(fabs(alpha) > 1.0/GSL_SQRT_DBL_EPSILON) { |
| 252 | result->err *= 256.0 * fabs(alpha) * GSL_SQRT_DBL_EPSILON; |
| 253 | } |
| 254 | |
| 255 | return GSL_ERROR_SELECT_2(stat_t, stat_a); |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /* x >> nu*nu+1 |
| 260 | */ |
| 261 | int |
| 262 | gsl_sf_bessel_Inu_scaled_asympx_e(const double nu, const double x, gsl_sf_result * result) |
| 263 | { |
| 264 | double mu = 4.0*nu*nu; |
| 265 | double mum1 = mu-1.0; |
| 266 | double mum9 = mu-9.0; |
| 267 | double pre = 1.0/sqrt(2.0*M_PI*x); |
| 268 | double r = mu/x; |
| 269 | result->val = pre * (1.0 - mum1/(8.0*x) + mum1*mum9/(128.0*x*x)); |
| 270 | result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * fabs(0.1*r*r*r); |
| 271 | return GSL_SUCCESS; |
| 272 | } |
| 273 | |
| 274 | /* x >> nu*nu+1 |
| 275 | */ |
| 276 | int |
| 277 | gsl_sf_bessel_Knu_scaled_asympx_e(const double nu, const double x, gsl_sf_result * result) |
| 278 | { |
| 279 | double mu = 4.0*nu*nu; |
| 280 | double mum1 = mu-1.0; |
| 281 | double mum9 = mu-9.0; |
| 282 | double pre = sqrt(M_PI/(2.0*x)); |
| 283 | double r = nu/x; |
| 284 | result->val = pre * (1.0 + mum1/(8.0*x) + mum1*mum9/(128.0*x*x)); |
| 285 | result->err = 2.0 * GSL_DBL_EPSILON * fabs(result->val) + pre * fabs(0.1*r*r*r); |
| 286 | return GSL_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /* nu -> Inf; uniform in x > 0 [Abramowitz+Stegun, 9.7.7] |
| 291 | * |
| 292 | * error: |
| 293 | * The error has the form u_N(t)/nu^N where 0 <= t <= 1. |
| 294 | * It is not hard to show that |u_N(t)| is small for such t. |
| 295 | * We have N=6 here, and |u_6(t)| < 0.025, so the error is clearly |
| 296 | * bounded by 0.025/nu^6. This gives the asymptotic bound on nu |
| 297 | * seen below as nu ~ 100. For general MACH_EPS it will be |
| 298 | * nu > 0.5 / MACH_EPS^(1/6) |
| 299 | * When t is small, the bound is even better because |u_N(t)| vanishes |
| 300 | * as t->0. In fact u_N(t) ~ C t^N as t->0, with C ~= 0.1. |
| 301 | * We write |
| 302 | * err_N <= min(0.025, C(1/(1+(x/nu)^2))^3) / nu^6 |
| 303 | * therefore |
| 304 | * min(0.29/nu^2, 0.5/(nu^2+x^2)) < MACH_EPS^{1/3} |
| 305 | * and this is the general form. |
| 306 | * |
| 307 | * empirical error analysis, assuming 14 digit requirement: |
| 308 | * choose x > 50.000 nu ==> nu > 3 |
| 309 | * choose x > 10.000 nu ==> nu > 15 |
| 310 | * choose x > 2.000 nu ==> nu > 50 |
| 311 | * choose x > 1.000 nu ==> nu > 75 |
| 312 | * choose x > 0.500 nu ==> nu > 80 |
| 313 | * choose x > 0.100 nu ==> nu > 83 |
| 314 | * |
| 315 | * This makes sense. For x << nu, the error will be of the form u_N(1)/nu^N, |
| 316 | * since the polynomial term will be evaluated near t=1, so the bound |
| 317 | * on nu will become constant for small x. Furthermore, increasing x with |
| 318 | * nu fixed will decrease the error. |
| 319 | */ |
| 320 | int |
| 321 | gsl_sf_bessel_Inu_scaled_asymp_unif_e(const double nu, const double x, gsl_sf_result * result) |
| 322 | { |
| 323 | int i; |
| 324 | double z = x/nu; |
| 325 | double root_term = hypot(1.0,z); |
| 326 | double pre = 1.0/sqrt(2.0*M_PI*nu * root_term); |
| 327 | double eta = root_term + log(z/(1.0+root_term)); |
| 328 | double ex_arg = ( z < 1.0/GSL_ROOT3_DBL_EPSILON ? nu*(-z + eta) : -0.5*nu/z*(1.0 - 1.0/(12.0*z*z)) ); |
| 329 | gsl_sf_result ex_result; |
| 330 | int stat_ex = gsl_sf_exp_e(ex_arg, &ex_result); |
| 331 | if(stat_ex == GSL_SUCCESS) { |
| 332 | double t = 1.0/root_term; |
| 333 | double sum; |
| 334 | double tpow[16]; |
| 335 | tpow[0] = 1.0; |
| 336 | for(i=1; i<16; i++) tpow[i] = t * tpow[i-1]; |
| 337 | sum = 1.0 + debye_u1(tpow)/nu + debye_u2(tpow)/(nu*nu) + debye_u3(tpow)/(nu*nu*nu) |
| 338 | + debye_u4(tpow)/(nu*nu*nu*nu) + debye_u5(tpow)/(nu*nu*nu*nu*nu); |
| 339 | result->val = pre * ex_result.val * sum; |
| 340 | result->err = pre * ex_result.val / (nu*nu*nu*nu*nu*nu); |
| 341 | result->err += pre * ex_result.err * fabs(sum); |
| 342 | result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 343 | return GSL_SUCCESS; |
| 344 | } |
| 345 | else { |
| 346 | result->val = 0.0; |
| 347 | result->err = 0.0; |
| 348 | return stat_ex; |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* nu -> Inf; uniform in x > 0 [Abramowitz+Stegun, 9.7.8] |
| 354 | * |
| 355 | * error: |
| 356 | * identical to that above for Inu_scaled |
| 357 | */ |
| 358 | int |
| 359 | gsl_sf_bessel_Knu_scaled_asymp_unif_e(const double nu, const double x, gsl_sf_result * result) |
| 360 | { |
| 361 | int i; |
| 362 | double z = x/nu; |
| 363 | double root_term = hypot(1.0,z); |
| 364 | double pre = sqrt(M_PI/(2.0*nu*root_term)); |
| 365 | double eta = root_term + log(z/(1.0+root_term)); |
| 366 | double ex_arg = ( z < 1.0/GSL_ROOT3_DBL_EPSILON ? nu*(z - eta) : 0.5*nu/z*(1.0 + 1.0/(12.0*z*z)) ); |
| 367 | gsl_sf_result ex_result; |
| 368 | int stat_ex = gsl_sf_exp_e(ex_arg, &ex_result); |
| 369 | if(stat_ex == GSL_SUCCESS) { |
| 370 | double t = 1.0/root_term; |
| 371 | double sum; |
| 372 | double tpow[16]; |
| 373 | tpow[0] = 1.0; |
| 374 | for(i=1; i<16; i++) tpow[i] = t * tpow[i-1]; |
| 375 | sum = 1.0 - debye_u1(tpow)/nu + debye_u2(tpow)/(nu*nu) - debye_u3(tpow)/(nu*nu*nu) |
| 376 | + debye_u4(tpow)/(nu*nu*nu*nu) - debye_u5(tpow)/(nu*nu*nu*nu*nu); |
| 377 | result->val = pre * ex_result.val * sum; |
| 378 | result->err = pre * ex_result.err * fabs(sum); |
| 379 | result->err += pre * ex_result.val / (nu*nu*nu*nu*nu*nu); |
| 380 | result->err += 2.0 * GSL_DBL_EPSILON * fabs(result->val); |
| 381 | return GSL_SUCCESS; |
| 382 | } |
| 383 | else { |
| 384 | result->val = 0.0; |
| 385 | result->err = 0.0; |
| 386 | return stat_ex; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /* Evaluate J_mu(x),J_{mu+1}(x) and Y_mu(x),Y_{mu+1}(x) for |mu| < 1/2 |
| 392 | */ |
| 393 | int |
| 394 | gsl_sf_bessel_JY_mu_restricted(const double mu, const double x, |
| 395 | gsl_sf_result * Jmu, gsl_sf_result * Jmup1, |
| 396 | gsl_sf_result * Ymu, gsl_sf_result * Ymup1) |
| 397 | { |
| 398 | /* CHECK_POINTER(Jmu) */ |
| 399 | /* CHECK_POINTER(Jmup1) */ |
| 400 | /* CHECK_POINTER(Ymu) */ |
| 401 | /* CHECK_POINTER(Ymup1) */ |
| 402 | |
| 403 | if(x < 0.0 || fabs(mu) > 0.5) { |
| 404 | Jmu->val = 0.0; |
| 405 | Jmu->err = 0.0; |
| 406 | Jmup1->val = 0.0; |
| 407 | Jmup1->err = 0.0; |
| 408 | Ymu->val = 0.0; |
| 409 | Ymu->err = 0.0; |
| 410 | Ymup1->val = 0.0; |
| 411 | Ymup1->err = 0.0; |
| 412 | GSL_ERROR ("error", GSL_EDOM); |
| 413 | } |
| 414 | else if(x == 0.0) { |
| 415 | if(mu == 0.0) { |
| 416 | Jmu->val = 1.0; |
| 417 | Jmu->err = 0.0; |
| 418 | } |
| 419 | else { |
| 420 | Jmu->val = 0.0; |
| 421 | Jmu->err = 0.0; |
| 422 | } |
| 423 | Jmup1->val = 0.0; |
| 424 | Jmup1->err = 0.0; |
| 425 | Ymu->val = 0.0; |
| 426 | Ymu->err = 0.0; |
| 427 | Ymup1->val = 0.0; |
| 428 | Ymup1->err = 0.0; |
| 429 | GSL_ERROR ("error", GSL_EDOM); |
| 430 | } |
| 431 | else { |
| 432 | int stat_Y; |
| 433 | int stat_J; |
| 434 | |
| 435 | if(x < 2.0) { |
| 436 | /* Use Taylor series for J and the Temme series for Y. |
| 437 | * The Taylor series for J requires nu > 0, so we shift |
| 438 | * up one and use the recursion relation to get Jmu, in |
| 439 | * case mu < 0. |
| 440 | */ |
| 441 | gsl_sf_result Jmup2; |
| 442 | int stat_J1 = gsl_sf_bessel_IJ_taylor_e(mu+1.0, x, -1, 100, GSL_DBL_EPSILON, Jmup1); |
| 443 | int stat_J2 = gsl_sf_bessel_IJ_taylor_e(mu+2.0, x, -1, 100, GSL_DBL_EPSILON, &Jmup2); |
| 444 | double c = 2.0*(mu+1.0)/x; |
| 445 | Jmu->val = c * Jmup1->val - Jmup2.val; |
| 446 | Jmu->err = c * Jmup1->err + Jmup2.err; |
| 447 | Jmu->err += 2.0 * GSL_DBL_EPSILON * fabs(Jmu->val); |
| 448 | stat_J = GSL_ERROR_SELECT_2(stat_J1, stat_J2); |
| 449 | stat_Y = gsl_sf_bessel_Y_temme(mu, x, Ymu, Ymup1); |
| 450 | return GSL_ERROR_SELECT_2(stat_J, stat_Y); |
| 451 | } |
| 452 | else if(x < 1000.0) { |
| 453 | double P, Q; |
| 454 | double J_ratio; |
| 455 | double J_sgn; |
| 456 | const int stat_CF1 = gsl_sf_bessel_J_CF1(mu, x, &J_ratio, &J_sgn); |
| 457 | const int stat_CF2 = gsl_sf_bessel_JY_steed_CF2(mu, x, &P, &Q); |
| 458 | double Jprime_J_ratio = mu/x - J_ratio; |
| 459 | double gamma = (P - Jprime_J_ratio)/Q; |
| 460 | Jmu->val = J_sgn * sqrt(2.0/(M_PI*x) / (Q + gamma*(P-Jprime_J_ratio))); |
| 461 | Jmu->err = 4.0 * GSL_DBL_EPSILON * fabs(Jmu->val); |
| 462 | Jmup1->val = J_ratio * Jmu->val; |
| 463 | Jmup1->err = fabs(J_ratio) * Jmu->err; |
| 464 | Ymu->val = gamma * Jmu->val; |
| 465 | Ymu->err = fabs(gamma) * Jmu->err; |
| 466 | Ymup1->val = Ymu->val * (mu/x - P - Q/gamma); |
| 467 | Ymup1->err = Ymu->err * fabs(mu/x - P - Q/gamma) + 4.0*GSL_DBL_EPSILON*fabs(Ymup1->val); |
| 468 | return GSL_ERROR_SELECT_2(stat_CF1, stat_CF2); |
| 469 | } |
| 470 | else { |
| 471 | /* Use asymptotics for large argument. |
| 472 | */ |
| 473 | const int stat_J0 = gsl_sf_bessel_Jnu_asympx_e(mu, x, Jmu); |
| 474 | const int stat_J1 = gsl_sf_bessel_Jnu_asympx_e(mu+1.0, x, Jmup1); |
| 475 | const int stat_Y0 = gsl_sf_bessel_Ynu_asympx_e(mu, x, Ymu); |
| 476 | const int stat_Y1 = gsl_sf_bessel_Ynu_asympx_e(mu+1.0, x, Ymup1); |
| 477 | stat_J = GSL_ERROR_SELECT_2(stat_J0, stat_J1); |
| 478 | stat_Y = GSL_ERROR_SELECT_2(stat_Y0, stat_Y1); |
| 479 | return GSL_ERROR_SELECT_2(stat_J, stat_Y); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | |
| 485 | int |
| 486 | gsl_sf_bessel_J_CF1(const double nu, const double x, |
| 487 | double * ratio, double * sgn) |
| 488 | { |
| 489 | const double RECUR_BIG = GSL_SQRT_DBL_MAX; |
| 490 | const int maxiter = 10000; |
| 491 | int n = 1; |
| 492 | double Anm2 = 1.0; |
| 493 | double Bnm2 = 0.0; |
| 494 | double Anm1 = 0.0; |
| 495 | double Bnm1 = 1.0; |
| 496 | double a1 = x/(2.0*(nu+1.0)); |
| 497 | double An = Anm1 + a1*Anm2; |
| 498 | double Bn = Bnm1 + a1*Bnm2; |
| 499 | double an; |
| 500 | double fn = An/Bn; |
| 501 | double dn = a1; |
| 502 | double s = 1.0; |
| 503 | |
| 504 | while(n < maxiter) { |
| 505 | double old_fn; |
| 506 | double del; |
| 507 | n++; |
| 508 | Anm2 = Anm1; |
| 509 | Bnm2 = Bnm1; |
| 510 | Anm1 = An; |
| 511 | Bnm1 = Bn; |
| 512 | an = -x*x/(4.0*(nu+n-1.0)*(nu+n)); |
| 513 | An = Anm1 + an*Anm2; |
| 514 | Bn = Bnm1 + an*Bnm2; |
| 515 | |
| 516 | if(fabs(An) > RECUR_BIG || fabs(Bn) > RECUR_BIG) { |
| 517 | An /= RECUR_BIG; |
| 518 | Bn /= RECUR_BIG; |
| 519 | Anm1 /= RECUR_BIG; |
| 520 | Bnm1 /= RECUR_BIG; |
| 521 | Anm2 /= RECUR_BIG; |
| 522 | Bnm2 /= RECUR_BIG; |
| 523 | } |
| 524 | |
| 525 | old_fn = fn; |
| 526 | fn = An/Bn; |
| 527 | del = old_fn/fn; |
| 528 | |
| 529 | dn = 1.0 / (2.0*(nu+n)/x - dn); |
| 530 | if(dn < 0.0) s = -s; |
| 531 | |
| 532 | if(fabs(del - 1.0) < 2.0*GSL_DBL_EPSILON) break; |
| 533 | } |
| 534 | |
| 535 | *ratio = fn; |
| 536 | *sgn = s; |
| 537 | |
| 538 | if(n >= maxiter) |
| 539 | GSL_ERROR ("error", GSL_EMAXITER); |
| 540 | else |
| 541 | return GSL_SUCCESS; |
| 542 | } |
| 543 | |
| 544 | |
| 545 | |
| 546 | /* Evaluate the continued fraction CF1 for J_{nu+1}/J_nu |
| 547 | * using Gautschi (Euler) equivalent series. |
| 548 | * This exhibits an annoying problem because the |
| 549 | * a_k are not positive definite (in fact they are all negative). |
| 550 | * There are cases when rho_k blows up. Example: nu=1,x=4. |
| 551 | */ |
| 552 | #if 0 |
| 553 | int |
| 554 | gsl_sf_bessel_J_CF1_ser(const double nu, const double x, |
| 555 | double * ratio, double * sgn) |
| 556 | { |
| 557 | const int maxk = 20000; |
| 558 | double tk = 1.0; |
| 559 | double sum = 1.0; |
| 560 | double rhok = 0.0; |
| 561 | double dk = 0.0; |
| 562 | double s = 1.0; |
| 563 | int k; |
| 564 | |
| 565 | for(k=1; k<maxk; k++) { |
| 566 | double ak = -0.25 * (x/(nu+k)) * x/(nu+k+1.0); |
| 567 | rhok = -ak*(1.0 + rhok)/(1.0 + ak*(1.0 + rhok)); |
| 568 | tk *= rhok; |
| 569 | sum += tk; |
| 570 | |
| 571 | dk = 1.0 / (2.0/x - (nu+k-1.0)/(nu+k) * dk); |
| 572 | if(dk < 0.0) s = -s; |
| 573 | |
| 574 | if(fabs(tk/sum) < GSL_DBL_EPSILON) break; |
| 575 | } |
| 576 | |
| 577 | *ratio = x/(2.0*(nu+1.0)) * sum; |
| 578 | *sgn = s; |
| 579 | |
| 580 | if(k == maxk) |
| 581 | GSL_ERROR ("error", GSL_EMAXITER); |
| 582 | else |
| 583 | return GSL_SUCCESS; |
| 584 | } |
| 585 | #endif |
| 586 | |
| 587 | |
| 588 | /* Evaluate the continued fraction CF1 for I_{nu+1}/I_nu |
| 589 | * using Gautschi (Euler) equivalent series. |
| 590 | */ |
| 591 | int |
| 592 | gsl_sf_bessel_I_CF1_ser(const double nu, const double x, double * ratio) |
| 593 | { |
| 594 | const int maxk = 20000; |
| 595 | double tk = 1.0; |
| 596 | double sum = 1.0; |
| 597 | double rhok = 0.0; |
| 598 | int k; |
| 599 | |
| 600 | for(k=1; k<maxk; k++) { |
| 601 | double ak = 0.25 * (x/(nu+k)) * x/(nu+k+1.0); |
| 602 | rhok = -ak*(1.0 + rhok)/(1.0 + ak*(1.0 + rhok)); |
| 603 | tk *= rhok; |
| 604 | sum += tk; |
| 605 | if(fabs(tk/sum) < GSL_DBL_EPSILON) break; |
| 606 | } |
| 607 | |
| 608 | *ratio = x/(2.0*(nu+1.0)) * sum; |
| 609 | |
| 610 | if(k == maxk) |
| 611 | GSL_ERROR ("error", GSL_EMAXITER); |
| 612 | else |
| 613 | return GSL_SUCCESS; |
| 614 | } |
| 615 | |
| 616 | |
| 617 | int |
| 618 | gsl_sf_bessel_JY_steed_CF2(const double nu, const double x, |
| 619 | double * P, double * Q) |
| 620 | { |
| 621 | const int max_iter = 10000; |
| 622 | const double SMALL = 1.0e-100; |
| 623 | |
| 624 | int i = 1; |
| 625 | |
| 626 | double x_inv = 1.0/x; |
| 627 | double a = 0.25 - nu*nu; |
| 628 | double p = -0.5*x_inv; |
| 629 | double q = 1.0; |
| 630 | double br = 2.0*x; |
| 631 | double bi = 2.0; |
| 632 | double fact = a*x_inv/(p*p + q*q); |
| 633 | double cr = br + q*fact; |
| 634 | double ci = bi + p*fact; |
| 635 | double den = br*br + bi*bi; |
| 636 | double dr = br/den; |
| 637 | double di = -bi/den; |
| 638 | double dlr = cr*dr - ci*di; |
| 639 | double dli = cr*di + ci*dr; |
| 640 | double temp = p*dlr - q*dli; |
| 641 | q = p*dli + q*dlr; |
| 642 | p = temp; |
| 643 | for (i=2; i<=max_iter; i++) { |
| 644 | a += 2*(i-1); |
| 645 | bi += 2.0; |
| 646 | dr = a*dr + br; |
| 647 | di = a*di + bi; |
| 648 | if(fabs(dr)+fabs(di) < SMALL) dr = SMALL; |
| 649 | fact = a/(cr*cr+ci*ci); |
| 650 | cr = br + cr*fact; |
| 651 | ci = bi - ci*fact; |
| 652 | if(fabs(cr)+fabs(ci) < SMALL) cr = SMALL; |
| 653 | den = dr*dr + di*di; |
| 654 | dr /= den; |
| 655 | di /= -den; |
| 656 | dlr = cr*dr - ci*di; |
| 657 | dli = cr*di + ci*dr; |
| 658 | temp = p*dlr - q*dli; |
| 659 | q = p*dli + q*dlr; |
| 660 | p = temp; |
| 661 | if(fabs(dlr-1.0)+fabs(dli) < GSL_DBL_EPSILON) break; |
| 662 | } |
| 663 | |
| 664 | *P = p; |
| 665 | *Q = q; |
| 666 | |
| 667 | if(i == max_iter) |
| 668 | GSL_ERROR ("error", GSL_EMAXITER); |
| 669 | else |
| 670 | return GSL_SUCCESS; |
| 671 | } |
| 672 | |
| 673 | |
| 674 | /* Evaluate continued fraction CF2, using Thompson-Barnett-Temme method, |
| 675 | * to obtain values of exp(x)*K_nu and exp(x)*K_{nu+1}. |
| 676 | * |
| 677 | * This is unstable for small x; x > 2 is a good cutoff. |
| 678 | * Also requires |nu| < 1/2. |
| 679 | */ |
| 680 | int |
| 681 | gsl_sf_bessel_K_scaled_steed_temme_CF2(const double nu, const double x, |
| 682 | double * K_nu, double * K_nup1, |
| 683 | double * Kp_nu) |
| 684 | { |
| 685 | const int maxiter = 10000; |
| 686 | |
| 687 | int i = 1; |
| 688 | double bi = 2.0*(1.0 + x); |
| 689 | double di = 1.0/bi; |
| 690 | double delhi = di; |
| 691 | double hi = di; |
| 692 | |
| 693 | double qi = 0.0; |
| 694 | double qip1 = 1.0; |
| 695 | |
| 696 | double ai = -(0.25 - nu*nu); |
| 697 | double a1 = ai; |
| 698 | double ci = -ai; |
| 699 | double Qi = -ai; |
| 700 | |
| 701 | double s = 1.0 + Qi*delhi; |
| 702 | |
| 703 | for(i=2; i<=maxiter; i++) { |
| 704 | double dels; |
| 705 | double tmp; |
| 706 | ai -= 2.0*(i-1); |
| 707 | ci = -ai*ci/i; |
| 708 | tmp = (qi - bi*qip1)/ai; |
| 709 | qi = qip1; |
| 710 | qip1 = tmp; |
| 711 | Qi += ci*qip1; |
| 712 | bi += 2.0; |
| 713 | di = 1.0/(bi + ai*di); |
| 714 | delhi = (bi*di - 1.0) * delhi; |
| 715 | hi += delhi; |
| 716 | dels = Qi*delhi; |
| 717 | s += dels; |
| 718 | if(fabs(dels/s) < GSL_DBL_EPSILON) break; |
| 719 | } |
| 720 | |
| 721 | hi *= -a1; |
| 722 | |
| 723 | *K_nu = sqrt(M_PI/(2.0*x)) / s; |
| 724 | *K_nup1 = *K_nu * (nu + x + 0.5 - hi)/x; |
| 725 | *Kp_nu = - *K_nup1 + nu/x * *K_nu; |
| 726 | if(i == maxiter) |
| 727 | GSL_ERROR ("error", GSL_EMAXITER); |
| 728 | else |
| 729 | return GSL_SUCCESS; |
| 730 | } |
| 731 | |
| 732 | |
| 733 | int gsl_sf_bessel_cos_pi4_e(double y, double eps, gsl_sf_result * result) |
| 734 | { |
| 735 | const double sy = sin(y); |
| 736 | const double cy = cos(y); |
| 737 | const double s = sy + cy; |
| 738 | const double d = sy - cy; |
| 739 | const double abs_sum = fabs(cy) + fabs(sy); |
| 740 | double seps; |
| 741 | double ceps; |
| 742 | if(fabs(eps) < GSL_ROOT5_DBL_EPSILON) { |
| 743 | const double e2 = eps*eps; |
| 744 | seps = eps * (1.0 - e2/6.0 * (1.0 - e2/20.0)); |
| 745 | ceps = 1.0 - e2/2.0 * (1.0 - e2/12.0); |
| 746 | } |
| 747 | else { |
| 748 | seps = sin(eps); |
| 749 | ceps = cos(eps); |
| 750 | } |
| 751 | result->val = (ceps * s - seps * d)/ M_SQRT2; |
| 752 | result->err = 2.0 * GSL_DBL_EPSILON * (fabs(ceps) + fabs(seps)) * abs_sum / M_SQRT2; |
| 753 | |
| 754 | /* Try to account for error in evaluation of sin(y), cos(y). |
| 755 | * This is a little sticky because we don't really know |
| 756 | * how the library routines are doing their argument reduction. |
| 757 | * However, we will make a reasonable guess. |
| 758 | * FIXME ? |
| 759 | */ |
| 760 | if(y > 1.0/GSL_DBL_EPSILON) { |
| 761 | result->err *= 0.5 * y; |
| 762 | } |
| 763 | else if(y > 1.0/GSL_SQRT_DBL_EPSILON) { |
| 764 | result->err *= 256.0 * y * GSL_SQRT_DBL_EPSILON; |
| 765 | } |
| 766 | |
| 767 | return GSL_SUCCESS; |
| 768 | } |
| 769 | |
| 770 | |
| 771 | int gsl_sf_bessel_sin_pi4_e(double y, double eps, gsl_sf_result * result) |
| 772 | { |
| 773 | const double sy = sin(y); |
| 774 | const double cy = cos(y); |
| 775 | const double s = sy + cy; |
| 776 | const double d = sy - cy; |
| 777 | const double abs_sum = fabs(cy) + fabs(sy); |
| 778 | double seps; |
| 779 | double ceps; |
| 780 | if(fabs(eps) < GSL_ROOT5_DBL_EPSILON) { |
| 781 | const double e2 = eps*eps; |
| 782 | seps = eps * (1.0 - e2/6.0 * (1.0 - e2/20.0)); |
| 783 | ceps = 1.0 - e2/2.0 * (1.0 - e2/12.0); |
| 784 | } |
| 785 | else { |
| 786 | seps = sin(eps); |
| 787 | ceps = cos(eps); |
| 788 | } |
| 789 | result->val = (ceps * d + seps * s)/ M_SQRT2; |
| 790 | result->err = 2.0 * GSL_DBL_EPSILON * (fabs(ceps) + fabs(seps)) * abs_sum / M_SQRT2; |
| 791 | |
| 792 | /* Try to account for error in evaluation of sin(y), cos(y). |
| 793 | * See above. |
| 794 | * FIXME ? |
| 795 | */ |
| 796 | if(y > 1.0/GSL_DBL_EPSILON) { |
| 797 | result->err *= 0.5 * y; |
| 798 | } |
| 799 | else if(y > 1.0/GSL_SQRT_DBL_EPSILON) { |
| 800 | result->err *= 256.0 * y * GSL_SQRT_DBL_EPSILON; |
| 801 | } |
| 802 | |
| 803 | return GSL_SUCCESS; |
| 804 | } |
| 805 | |
| 806 | |
| 807 | /************************************************************************ |
| 808 | * * |
| 809 | Asymptotic approximations 8.11.5, 8.12.5, and 8.42.7 from |
| 810 | G.N.Watson, A Treatise on the Theory of Bessel Functions, |
| 811 | 2nd Edition (Cambridge University Press, 1944). |
| 812 | Higher terms in expansion for x near l given by |
| 813 | Airey in Phil. Mag. 31, 520 (1916). |
| 814 | |
| 815 | This approximation is accurate to near 0.1% at the boundaries |
| 816 | between the asymptotic regions; well away from the boundaries |
| 817 | the accuracy is better than 10^{-5}. |
| 818 | * * |
| 819 | ************************************************************************/ |
| 820 | #if 0 |
| 821 | double besselJ_meissel(double nu, double x) |
| 822 | { |
| 823 | double beta = pow(nu, 0.325); |
| 824 | double result; |
| 825 | |
| 826 | /* Fitted matching points. */ |
| 827 | double llimit = 1.1 * beta; |
| 828 | double ulimit = 1.3 * beta; |
| 829 | |
| 830 | double nu2 = nu * nu; |
| 831 | |
| 832 | if (nu < 5. && x < 1.) |
| 833 | { |
| 834 | /* Small argument and order. Use a Taylor expansion. */ |
| 835 | int k; |
| 836 | double xo2 = 0.5 * x; |
| 837 | double gamfactor = pow(nu,nu) * exp(-nu) * sqrt(nu * 2. * M_PI) |
| 838 | * (1. + 1./(12.*nu) + 1./(288.*nu*nu)); |
| 839 | double prefactor = pow(xo2, nu) / gamfactor; |
| 840 | double C[5]; |
| 841 | |
| 842 | C[0] = 1.; |
| 843 | C[1] = -C[0] / (nu+1.); |
| 844 | C[2] = -C[1] / (2.*(nu+2.)); |
| 845 | C[3] = -C[2] / (3.*(nu+3.)); |
| 846 | C[4] = -C[3] / (4.*(nu+4.)); |
| 847 | |
| 848 | result = 0.; |
| 849 | for(k=0; k<5; k++) |
| 850 | result += C[k] * pow(xo2, 2.*k); |
| 851 | |
| 852 | result *= prefactor; |
| 853 | } |
| 854 | else if(x < nu - llimit) |
| 855 | { |
| 856 | /* Small x region: x << l. */ |
| 857 | double z = x / nu; |
| 858 | double z2 = z*z; |
| 859 | double rtomz2 = sqrt(1.-z2); |
| 860 | double omz2_2 = (1.-z2)*(1.-z2); |
| 861 | |
| 862 | /* Calculate Meissel exponent. */ |
| 863 | double term1 = 1./(24.*nu) * ((2.+3.*z2)/((1.-z2)*rtomz2) -2.); |
| 864 | double term2 = - z2*(4. + z2)/(16.*nu2*(1.-z2)*omz2_2); |
| 865 | double V_nu = term1 + term2; |
| 866 | |
| 867 | /* Calculate the harmless prefactor. */ |
| 868 | double sterlingsum = 1. + 1./(12.*nu) + 1./(288*nu2); |
| 869 | double harmless = 1. / (sqrt(rtomz2*2.*M_PI*nu) * sterlingsum); |
| 870 | |
| 871 | /* Calculate the logarithm of the nu dependent prefactor. */ |
| 872 | double ln_nupre = rtomz2 + log(z) - log(1. + rtomz2); |
| 873 | |
| 874 | result = harmless * exp(nu*ln_nupre - V_nu); |
| 875 | } |
| 876 | else if(x < nu + ulimit) |
| 877 | { |
| 878 | /* Intermediate region 1: x near nu. */ |
| 879 | double eps = 1.-nu/x; |
| 880 | double eps_x = eps * x; |
| 881 | double eps_x_2 = eps_x * eps_x; |
| 882 | double xo6 = x/6.; |
| 883 | double B[6]; |
| 884 | static double gam[6] = {2.67894, 1.35412, 1., 0.89298, 0.902745, 1.}; |
| 885 | static double sf[6] = {0.866025, 0.866025, 0., -0.866025, -0.866025, 0.}; |
| 886 | |
| 887 | /* Some terms are identically zero, because sf[] can be zero. |
| 888 | * Some terms do not appear in the result. |
| 889 | */ |
| 890 | B[0] = 1.; |
| 891 | B[1] = eps_x; |
| 892 | /* B[2] = 0.5 * eps_x_2 - 1./20.; */ |
| 893 | B[3] = eps_x * (eps_x_2/6. - 1./15.); |
| 894 | B[4] = eps_x_2 * (eps_x_2 - 1.)/24. + 1./280.; |
| 895 | /* B[5] = eps_x * (eps_x_2*(0.5*eps_x_2 - 1.)/60. + 43./8400.); */ |
| 896 | |
| 897 | result = B[0] * gam[0] * sf[0] / pow(xo6, 1./3.); |
| 898 | result += B[1] * gam[1] * sf[1] / pow(xo6, 2./3.); |
| 899 | result += B[3] * gam[3] * sf[3] / pow(xo6, 4./3.); |
| 900 | result += B[4] * gam[4] * sf[4] / pow(xo6, 5./3.); |
| 901 | |
| 902 | result /= (3.*M_PI); |
| 903 | } |
| 904 | else |
| 905 | { |
| 906 | /* Region of very large argument. Use expansion |
| 907 | * for x>>l, and we need not be very exacting. |
| 908 | */ |
| 909 | double secb = x/nu; |
| 910 | double sec2b= secb*secb; |
| 911 | |
| 912 | double cotb = 1./sqrt(sec2b-1.); /* cotb=cot(beta) */ |
| 913 | |
| 914 | double beta = acos(nu/x); |
| 915 | double trigarg = nu/cotb - nu*beta - 0.25 * M_PI; |
| 916 | |
| 917 | double cot3b = cotb * cotb * cotb; |
| 918 | double cot6b = cot3b * cot3b; |
| 919 | |
| 920 | double sum1, sum2, expterm, prefactor, trigcos; |
| 921 | |
| 922 | sum1 = 2.0 + 3.0 * sec2b; |
| 923 | trigarg -= sum1 * cot3b / (24.0 * nu); |
| 924 | |
| 925 | trigcos = cos(trigarg); |
| 926 | |
| 927 | sum2 = 4.0 + sec2b; |
| 928 | expterm = sum2 * sec2b * cot6b / (16.0 * nu2); |
| 929 | |
| 930 | expterm = exp(-expterm); |
| 931 | prefactor = sqrt(2. * cotb / (nu * M_PI)); |
| 932 | |
| 933 | result = prefactor * expterm * trigcos; |
| 934 | } |
| 935 | |
| 936 | return result; |
| 937 | } |
| 938 | #endif |