1 // Special functions -*- C++ -*- 2 3 // Copyright (C) 2006-2016 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 // 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 // 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file tr1/bessel_function.tcc 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. @headername{tr1/cmath} 28 */ 29 30 // 31 // ISO C++ 14882 TR1: 5.2 Special functions 32 // 33 34 // Written by Edward Smith-Rowland. 35 // 36 // References: 37 // (1) Handbook of Mathematical Functions, 38 // ed. Milton Abramowitz and Irene A. Stegun, 39 // Dover Publications, 40 // Section 9, pp. 355-434, Section 10 pp. 435-478 41 // (2) The Gnu Scientific Library, http://www.gnu.org/software/gsl 42 // (3) Numerical Recipes in C, by W. H. Press, S. A. Teukolsky, 43 // W. T. Vetterling, B. P. Flannery, Cambridge University Press (1992), 44 // 2nd ed, pp. 240-245 45 46 #ifndef _GLIBCXX_TR1_BESSEL_FUNCTION_TCC 47 #define _GLIBCXX_TR1_BESSEL_FUNCTION_TCC 1 48 49 #include "special_function_util.h" 50 51 namespace std _GLIBCXX_VISIBILITY(default) 52 { 53 #if __STDCPP_WANT_MATH_SPEC_FUNCS__ 54 # define _GLIBCXX_MATH_NS ::std 55 #elif defined(_GLIBCXX_TR1_CMATH) 56 namespace tr1 57 { 58 # define _GLIBCXX_MATH_NS ::std::tr1 59 #else 60 # error do not include this header directly, use <cmath> or <tr1/cmath> 61 #endif 62 // [5.2] Special functions 63 64 // Implementation-space details. 65 namespace __detail 66 { 67 _GLIBCXX_BEGIN_NAMESPACE_VERSION 68 69 /** 70 * @brief Compute the gamma functions required by the Temme series 71 * expansions of @f$ N_\nu(x) @f$ and @f$ K_\nu(x) @f$. 72 * @f[ 73 * \Gamma_1 = \frac{1}{2\mu} 74 * [\frac{1}{\Gamma(1 - \mu)} - \frac{1}{\Gamma(1 + \mu)}] 75 * @f] 76 * and 77 * @f[ 78 * \Gamma_2 = \frac{1}{2} 79 * [\frac{1}{\Gamma(1 - \mu)} + \frac{1}{\Gamma(1 + \mu)}] 80 * @f] 81 * where @f$ -1/2 <= \mu <= 1/2 @f$ is @f$ \mu = \nu - N @f$ and @f$ N @f$. 82 * is the nearest integer to @f$ \nu @f$. 83 * The values of \f$ \Gamma(1 + \mu) \f$ and \f$ \Gamma(1 - \mu) \f$ 84 * are returned as well. 85 * 86 * The accuracy requirements on this are exquisite. 87 * 88 * @param __mu The input parameter of the gamma functions. 89 * @param __gam1 The output function \f$ \Gamma_1(\mu) \f$ 90 * @param __gam2 The output function \f$ \Gamma_2(\mu) \f$ 91 * @param __gampl The output function \f$ \Gamma(1 + \mu) \f$ 92 * @param __gammi The output function \f$ \Gamma(1 - \mu) \f$ 93 */ 94 template <typename _Tp> 95 void __gamma_temme(_Tp __mu,_Tp & __gam1,_Tp & __gam2,_Tp & __gampl,_Tp & __gammi)96 __gamma_temme(_Tp __mu, 97 _Tp & __gam1, _Tp & __gam2, _Tp & __gampl, _Tp & __gammi) 98 { 99 #if _GLIBCXX_USE_C99_MATH_TR1 100 __gampl = _Tp(1) / _GLIBCXX_MATH_NS::tgamma(_Tp(1) + __mu); 101 __gammi = _Tp(1) / _GLIBCXX_MATH_NS::tgamma(_Tp(1) - __mu); 102 #else 103 __gampl = _Tp(1) / __gamma(_Tp(1) + __mu); 104 __gammi = _Tp(1) / __gamma(_Tp(1) - __mu); 105 #endif 106 107 if (std::abs(__mu) < std::numeric_limits<_Tp>::epsilon()) 108 __gam1 = -_Tp(__numeric_constants<_Tp>::__gamma_e()); 109 else 110 __gam1 = (__gammi - __gampl) / (_Tp(2) * __mu); 111 112 __gam2 = (__gammi + __gampl) / (_Tp(2)); 113 114 return; 115 } 116 117 118 /** 119 * @brief Compute the Bessel @f$ J_\nu(x) @f$ and Neumann 120 * @f$ N_\nu(x) @f$ functions and their first derivatives 121 * @f$ J'_\nu(x) @f$ and @f$ N'_\nu(x) @f$ respectively. 122 * These four functions are computed together for numerical 123 * stability. 124 * 125 * @param __nu The order of the Bessel functions. 126 * @param __x The argument of the Bessel functions. 127 * @param __Jnu The output Bessel function of the first kind. 128 * @param __Nnu The output Neumann function (Bessel function of the second kind). 129 * @param __Jpnu The output derivative of the Bessel function of the first kind. 130 * @param __Npnu The output derivative of the Neumann function. 131 */ 132 template <typename _Tp> 133 void __bessel_jn(_Tp __nu,_Tp __x,_Tp & __Jnu,_Tp & __Nnu,_Tp & __Jpnu,_Tp & __Npnu)134 __bessel_jn(_Tp __nu, _Tp __x, 135 _Tp & __Jnu, _Tp & __Nnu, _Tp & __Jpnu, _Tp & __Npnu) 136 { 137 if (__x == _Tp(0)) 138 { 139 if (__nu == _Tp(0)) 140 { 141 __Jnu = _Tp(1); 142 __Jpnu = _Tp(0); 143 } 144 else if (__nu == _Tp(1)) 145 { 146 __Jnu = _Tp(0); 147 __Jpnu = _Tp(0.5L); 148 } 149 else 150 { 151 __Jnu = _Tp(0); 152 __Jpnu = _Tp(0); 153 } 154 __Nnu = -std::numeric_limits<_Tp>::infinity(); 155 __Npnu = std::numeric_limits<_Tp>::infinity(); 156 return; 157 } 158 159 const _Tp __eps = std::numeric_limits<_Tp>::epsilon(); 160 // When the multiplier is N i.e. 161 // fp_min = N * min() 162 // Then J_0 and N_0 tank at x = 8 * N (J_0 = 0 and N_0 = nan)! 163 //const _Tp __fp_min = _Tp(20) * std::numeric_limits<_Tp>::min(); 164 const _Tp __fp_min = std::sqrt(std::numeric_limits<_Tp>::min()); 165 const int __max_iter = 15000; 166 const _Tp __x_min = _Tp(2); 167 168 const int __nl = (__x < __x_min 169 ? static_cast<int>(__nu + _Tp(0.5L)) 170 : std::max(0, static_cast<int>(__nu - __x + _Tp(1.5L)))); 171 172 const _Tp __mu = __nu - __nl; 173 const _Tp __mu2 = __mu * __mu; 174 const _Tp __xi = _Tp(1) / __x; 175 const _Tp __xi2 = _Tp(2) * __xi; 176 _Tp __w = __xi2 / __numeric_constants<_Tp>::__pi(); 177 int __isign = 1; 178 _Tp __h = __nu * __xi; 179 if (__h < __fp_min) 180 __h = __fp_min; 181 _Tp __b = __xi2 * __nu; 182 _Tp __d = _Tp(0); 183 _Tp __c = __h; 184 int __i; 185 for (__i = 1; __i <= __max_iter; ++__i) 186 { 187 __b += __xi2; 188 __d = __b - __d; 189 if (std::abs(__d) < __fp_min) 190 __d = __fp_min; 191 __c = __b - _Tp(1) / __c; 192 if (std::abs(__c) < __fp_min) 193 __c = __fp_min; 194 __d = _Tp(1) / __d; 195 const _Tp __del = __c * __d; 196 __h *= __del; 197 if (__d < _Tp(0)) 198 __isign = -__isign; 199 if (std::abs(__del - _Tp(1)) < __eps) 200 break; 201 } 202 if (__i > __max_iter) 203 std::__throw_runtime_error(__N("Argument x too large in __bessel_jn; " 204 "try asymptotic expansion.")); 205 _Tp __Jnul = __isign * __fp_min; 206 _Tp __Jpnul = __h * __Jnul; 207 _Tp __Jnul1 = __Jnul; 208 _Tp __Jpnu1 = __Jpnul; 209 _Tp __fact = __nu * __xi; 210 for ( int __l = __nl; __l >= 1; --__l ) 211 { 212 const _Tp __Jnutemp = __fact * __Jnul + __Jpnul; 213 __fact -= __xi; 214 __Jpnul = __fact * __Jnutemp - __Jnul; 215 __Jnul = __Jnutemp; 216 } 217 if (__Jnul == _Tp(0)) 218 __Jnul = __eps; 219 _Tp __f= __Jpnul / __Jnul; 220 _Tp __Nmu, __Nnu1, __Npmu, __Jmu; 221 if (__x < __x_min) 222 { 223 const _Tp __x2 = __x / _Tp(2); 224 const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu; 225 _Tp __fact = (std::abs(__pimu) < __eps 226 ? _Tp(1) : __pimu / std::sin(__pimu)); 227 _Tp __d = -std::log(__x2); 228 _Tp __e = __mu * __d; 229 _Tp __fact2 = (std::abs(__e) < __eps 230 ? _Tp(1) : std::sinh(__e) / __e); 231 _Tp __gam1, __gam2, __gampl, __gammi; 232 __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi); 233 _Tp __ff = (_Tp(2) / __numeric_constants<_Tp>::__pi()) 234 * __fact * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d); 235 __e = std::exp(__e); 236 _Tp __p = __e / (__numeric_constants<_Tp>::__pi() * __gampl); 237 _Tp __q = _Tp(1) / (__e * __numeric_constants<_Tp>::__pi() * __gammi); 238 const _Tp __pimu2 = __pimu / _Tp(2); 239 _Tp __fact3 = (std::abs(__pimu2) < __eps 240 ? _Tp(1) : std::sin(__pimu2) / __pimu2 ); 241 _Tp __r = __numeric_constants<_Tp>::__pi() * __pimu2 * __fact3 * __fact3; 242 _Tp __c = _Tp(1); 243 __d = -__x2 * __x2; 244 _Tp __sum = __ff + __r * __q; 245 _Tp __sum1 = __p; 246 for (__i = 1; __i <= __max_iter; ++__i) 247 { 248 __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2); 249 __c *= __d / _Tp(__i); 250 __p /= _Tp(__i) - __mu; 251 __q /= _Tp(__i) + __mu; 252 const _Tp __del = __c * (__ff + __r * __q); 253 __sum += __del; 254 const _Tp __del1 = __c * __p - __i * __del; 255 __sum1 += __del1; 256 if ( std::abs(__del) < __eps * (_Tp(1) + std::abs(__sum)) ) 257 break; 258 } 259 if ( __i > __max_iter ) 260 std::__throw_runtime_error(__N("Bessel y series failed to converge " 261 "in __bessel_jn.")); 262 __Nmu = -__sum; 263 __Nnu1 = -__sum1 * __xi2; 264 __Npmu = __mu * __xi * __Nmu - __Nnu1; 265 __Jmu = __w / (__Npmu - __f * __Nmu); 266 } 267 else 268 { 269 _Tp __a = _Tp(0.25L) - __mu2; 270 _Tp __q = _Tp(1); 271 _Tp __p = -__xi / _Tp(2); 272 _Tp __br = _Tp(2) * __x; 273 _Tp __bi = _Tp(2); 274 _Tp __fact = __a * __xi / (__p * __p + __q * __q); 275 _Tp __cr = __br + __q * __fact; 276 _Tp __ci = __bi + __p * __fact; 277 _Tp __den = __br * __br + __bi * __bi; 278 _Tp __dr = __br / __den; 279 _Tp __di = -__bi / __den; 280 _Tp __dlr = __cr * __dr - __ci * __di; 281 _Tp __dli = __cr * __di + __ci * __dr; 282 _Tp __temp = __p * __dlr - __q * __dli; 283 __q = __p * __dli + __q * __dlr; 284 __p = __temp; 285 int __i; 286 for (__i = 2; __i <= __max_iter; ++__i) 287 { 288 __a += _Tp(2 * (__i - 1)); 289 __bi += _Tp(2); 290 __dr = __a * __dr + __br; 291 __di = __a * __di + __bi; 292 if (std::abs(__dr) + std::abs(__di) < __fp_min) 293 __dr = __fp_min; 294 __fact = __a / (__cr * __cr + __ci * __ci); 295 __cr = __br + __cr * __fact; 296 __ci = __bi - __ci * __fact; 297 if (std::abs(__cr) + std::abs(__ci) < __fp_min) 298 __cr = __fp_min; 299 __den = __dr * __dr + __di * __di; 300 __dr /= __den; 301 __di /= -__den; 302 __dlr = __cr * __dr - __ci * __di; 303 __dli = __cr * __di + __ci * __dr; 304 __temp = __p * __dlr - __q * __dli; 305 __q = __p * __dli + __q * __dlr; 306 __p = __temp; 307 if (std::abs(__dlr - _Tp(1)) + std::abs(__dli) < __eps) 308 break; 309 } 310 if (__i > __max_iter) 311 std::__throw_runtime_error(__N("Lentz's method failed " 312 "in __bessel_jn.")); 313 const _Tp __gam = (__p - __f) / __q; 314 __Jmu = std::sqrt(__w / ((__p - __f) * __gam + __q)); 315 #if _GLIBCXX_USE_C99_MATH_TR1 316 __Jmu = _GLIBCXX_MATH_NS::copysign(__Jmu, __Jnul); 317 #else 318 if (__Jmu * __Jnul < _Tp(0)) 319 __Jmu = -__Jmu; 320 #endif 321 __Nmu = __gam * __Jmu; 322 __Npmu = (__p + __q / __gam) * __Nmu; 323 __Nnu1 = __mu * __xi * __Nmu - __Npmu; 324 } 325 __fact = __Jmu / __Jnul; 326 __Jnu = __fact * __Jnul1; 327 __Jpnu = __fact * __Jpnu1; 328 for (__i = 1; __i <= __nl; ++__i) 329 { 330 const _Tp __Nnutemp = (__mu + __i) * __xi2 * __Nnu1 - __Nmu; 331 __Nmu = __Nnu1; 332 __Nnu1 = __Nnutemp; 333 } 334 __Nnu = __Nmu; 335 __Npnu = __nu * __xi * __Nmu - __Nnu1; 336 337 return; 338 } 339 340 341 /** 342 * @brief This routine computes the asymptotic cylindrical Bessel 343 * and Neumann functions of order nu: \f$ J_{\nu} \f$, 344 * \f$ N_{\nu} \f$. 345 * 346 * References: 347 * (1) Handbook of Mathematical Functions, 348 * ed. Milton Abramowitz and Irene A. Stegun, 349 * Dover Publications, 350 * Section 9 p. 364, Equations 9.2.5-9.2.10 351 * 352 * @param __nu The order of the Bessel functions. 353 * @param __x The argument of the Bessel functions. 354 * @param __Jnu The output Bessel function of the first kind. 355 * @param __Nnu The output Neumann function (Bessel function of the second kind). 356 */ 357 template <typename _Tp> 358 void __cyl_bessel_jn_asymp(_Tp __nu,_Tp __x,_Tp & __Jnu,_Tp & __Nnu)359 __cyl_bessel_jn_asymp(_Tp __nu, _Tp __x, _Tp & __Jnu, _Tp & __Nnu) 360 { 361 const _Tp __mu = _Tp(4) * __nu * __nu; 362 const _Tp __mum1 = __mu - _Tp(1); 363 const _Tp __mum9 = __mu - _Tp(9); 364 const _Tp __mum25 = __mu - _Tp(25); 365 const _Tp __mum49 = __mu - _Tp(49); 366 const _Tp __xx = _Tp(64) * __x * __x; 367 const _Tp __P = _Tp(1) - __mum1 * __mum9 / (_Tp(2) * __xx) 368 * (_Tp(1) - __mum25 * __mum49 / (_Tp(12) * __xx)); 369 const _Tp __Q = __mum1 / (_Tp(8) * __x) 370 * (_Tp(1) - __mum9 * __mum25 / (_Tp(6) * __xx)); 371 372 const _Tp __chi = __x - (__nu + _Tp(0.5L)) 373 * __numeric_constants<_Tp>::__pi_2(); 374 const _Tp __c = std::cos(__chi); 375 const _Tp __s = std::sin(__chi); 376 377 const _Tp __coef = std::sqrt(_Tp(2) 378 / (__numeric_constants<_Tp>::__pi() * __x)); 379 __Jnu = __coef * (__c * __P - __s * __Q); 380 __Nnu = __coef * (__s * __P + __c * __Q); 381 382 return; 383 } 384 385 386 /** 387 * @brief This routine returns the cylindrical Bessel functions 388 * of order \f$ \nu \f$: \f$ J_{\nu} \f$ or \f$ I_{\nu} \f$ 389 * by series expansion. 390 * 391 * The modified cylindrical Bessel function is: 392 * @f[ 393 * Z_{\nu}(x) = \sum_{k=0}^{\infty} 394 * \frac{\sigma^k (x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)} 395 * @f] 396 * where \f$ \sigma = +1 \f$ or\f$ -1 \f$ for 397 * \f$ Z = I \f$ or \f$ J \f$ respectively. 398 * 399 * See Abramowitz & Stegun, 9.1.10 400 * Abramowitz & Stegun, 9.6.7 401 * (1) Handbook of Mathematical Functions, 402 * ed. Milton Abramowitz and Irene A. Stegun, 403 * Dover Publications, 404 * Equation 9.1.10 p. 360 and Equation 9.6.10 p. 375 405 * 406 * @param __nu The order of the Bessel function. 407 * @param __x The argument of the Bessel function. 408 * @param __sgn The sign of the alternate terms 409 * -1 for the Bessel function of the first kind. 410 * +1 for the modified Bessel function of the first kind. 411 * @return The output Bessel function. 412 */ 413 template <typename _Tp> 414 _Tp __cyl_bessel_ij_series(_Tp __nu,_Tp __x,_Tp __sgn,unsigned int __max_iter)415 __cyl_bessel_ij_series(_Tp __nu, _Tp __x, _Tp __sgn, 416 unsigned int __max_iter) 417 { 418 if (__x == _Tp(0)) 419 return __nu == _Tp(0) ? _Tp(1) : _Tp(0); 420 421 const _Tp __x2 = __x / _Tp(2); 422 _Tp __fact = __nu * std::log(__x2); 423 #if _GLIBCXX_USE_C99_MATH_TR1 424 __fact -= _GLIBCXX_MATH_NS::lgamma(__nu + _Tp(1)); 425 #else 426 __fact -= __log_gamma(__nu + _Tp(1)); 427 #endif 428 __fact = std::exp(__fact); 429 const _Tp __xx4 = __sgn * __x2 * __x2; 430 _Tp __Jn = _Tp(1); 431 _Tp __term = _Tp(1); 432 433 for (unsigned int __i = 1; __i < __max_iter; ++__i) 434 { 435 __term *= __xx4 / (_Tp(__i) * (__nu + _Tp(__i))); 436 __Jn += __term; 437 if (std::abs(__term / __Jn) < std::numeric_limits<_Tp>::epsilon()) 438 break; 439 } 440 441 return __fact * __Jn; 442 } 443 444 445 /** 446 * @brief Return the Bessel function of order \f$ \nu \f$: 447 * \f$ J_{\nu}(x) \f$. 448 * 449 * The cylindrical Bessel function is: 450 * @f[ 451 * J_{\nu}(x) = \sum_{k=0}^{\infty} 452 * \frac{(-1)^k (x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)} 453 * @f] 454 * 455 * @param __nu The order of the Bessel function. 456 * @param __x The argument of the Bessel function. 457 * @return The output Bessel function. 458 */ 459 template<typename _Tp> 460 _Tp __cyl_bessel_j(_Tp __nu,_Tp __x)461 __cyl_bessel_j(_Tp __nu, _Tp __x) 462 { 463 if (__nu < _Tp(0) || __x < _Tp(0)) 464 std::__throw_domain_error(__N("Bad argument " 465 "in __cyl_bessel_j.")); 466 else if (__isnan(__nu) || __isnan(__x)) 467 return std::numeric_limits<_Tp>::quiet_NaN(); 468 else if (__x * __x < _Tp(10) * (__nu + _Tp(1))) 469 return __cyl_bessel_ij_series(__nu, __x, -_Tp(1), 200); 470 else if (__x > _Tp(1000)) 471 { 472 _Tp __J_nu, __N_nu; 473 __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu); 474 return __J_nu; 475 } 476 else 477 { 478 _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu; 479 __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu); 480 return __J_nu; 481 } 482 } 483 484 485 /** 486 * @brief Return the Neumann function of order \f$ \nu \f$: 487 * \f$ N_{\nu}(x) \f$. 488 * 489 * The Neumann function is defined by: 490 * @f[ 491 * N_{\nu}(x) = \frac{J_{\nu}(x) \cos \nu\pi - J_{-\nu}(x)} 492 * {\sin \nu\pi} 493 * @f] 494 * where for integral \f$ \nu = n \f$ a limit is taken: 495 * \f$ lim_{\nu \to n} \f$. 496 * 497 * @param __nu The order of the Neumann function. 498 * @param __x The argument of the Neumann function. 499 * @return The output Neumann function. 500 */ 501 template<typename _Tp> 502 _Tp __cyl_neumann_n(_Tp __nu,_Tp __x)503 __cyl_neumann_n(_Tp __nu, _Tp __x) 504 { 505 if (__nu < _Tp(0) || __x < _Tp(0)) 506 std::__throw_domain_error(__N("Bad argument " 507 "in __cyl_neumann_n.")); 508 else if (__isnan(__nu) || __isnan(__x)) 509 return std::numeric_limits<_Tp>::quiet_NaN(); 510 else if (__x > _Tp(1000)) 511 { 512 _Tp __J_nu, __N_nu; 513 __cyl_bessel_jn_asymp(__nu, __x, __J_nu, __N_nu); 514 return __N_nu; 515 } 516 else 517 { 518 _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu; 519 __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu); 520 return __N_nu; 521 } 522 } 523 524 525 /** 526 * @brief Compute the spherical Bessel @f$ j_n(x) @f$ 527 * and Neumann @f$ n_n(x) @f$ functions and their first 528 * derivatives @f$ j'_n(x) @f$ and @f$ n'_n(x) @f$ 529 * respectively. 530 * 531 * @param __n The order of the spherical Bessel function. 532 * @param __x The argument of the spherical Bessel function. 533 * @param __j_n The output spherical Bessel function. 534 * @param __n_n The output spherical Neumann function. 535 * @param __jp_n The output derivative of the spherical Bessel function. 536 * @param __np_n The output derivative of the spherical Neumann function. 537 */ 538 template <typename _Tp> 539 void __sph_bessel_jn(unsigned int __n,_Tp __x,_Tp & __j_n,_Tp & __n_n,_Tp & __jp_n,_Tp & __np_n)540 __sph_bessel_jn(unsigned int __n, _Tp __x, 541 _Tp & __j_n, _Tp & __n_n, _Tp & __jp_n, _Tp & __np_n) 542 { 543 const _Tp __nu = _Tp(__n) + _Tp(0.5L); 544 545 _Tp __J_nu, __N_nu, __Jp_nu, __Np_nu; 546 __bessel_jn(__nu, __x, __J_nu, __N_nu, __Jp_nu, __Np_nu); 547 548 const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2() 549 / std::sqrt(__x); 550 551 __j_n = __factor * __J_nu; 552 __n_n = __factor * __N_nu; 553 __jp_n = __factor * __Jp_nu - __j_n / (_Tp(2) * __x); 554 __np_n = __factor * __Np_nu - __n_n / (_Tp(2) * __x); 555 556 return; 557 } 558 559 560 /** 561 * @brief Return the spherical Bessel function 562 * @f$ j_n(x) @f$ of order n. 563 * 564 * The spherical Bessel function is defined by: 565 * @f[ 566 * j_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} J_{n+1/2}(x) 567 * @f] 568 * 569 * @param __n The order of the spherical Bessel function. 570 * @param __x The argument of the spherical Bessel function. 571 * @return The output spherical Bessel function. 572 */ 573 template <typename _Tp> 574 _Tp __sph_bessel(unsigned int __n,_Tp __x)575 __sph_bessel(unsigned int __n, _Tp __x) 576 { 577 if (__x < _Tp(0)) 578 std::__throw_domain_error(__N("Bad argument " 579 "in __sph_bessel.")); 580 else if (__isnan(__x)) 581 return std::numeric_limits<_Tp>::quiet_NaN(); 582 else if (__x == _Tp(0)) 583 { 584 if (__n == 0) 585 return _Tp(1); 586 else 587 return _Tp(0); 588 } 589 else 590 { 591 _Tp __j_n, __n_n, __jp_n, __np_n; 592 __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n); 593 return __j_n; 594 } 595 } 596 597 598 /** 599 * @brief Return the spherical Neumann function 600 * @f$ n_n(x) @f$. 601 * 602 * The spherical Neumann function is defined by: 603 * @f[ 604 * n_n(x) = \left( \frac{\pi}{2x} \right) ^{1/2} N_{n+1/2}(x) 605 * @f] 606 * 607 * @param __n The order of the spherical Neumann function. 608 * @param __x The argument of the spherical Neumann function. 609 * @return The output spherical Neumann function. 610 */ 611 template <typename _Tp> 612 _Tp __sph_neumann(unsigned int __n,_Tp __x)613 __sph_neumann(unsigned int __n, _Tp __x) 614 { 615 if (__x < _Tp(0)) 616 std::__throw_domain_error(__N("Bad argument " 617 "in __sph_neumann.")); 618 else if (__isnan(__x)) 619 return std::numeric_limits<_Tp>::quiet_NaN(); 620 else if (__x == _Tp(0)) 621 return -std::numeric_limits<_Tp>::infinity(); 622 else 623 { 624 _Tp __j_n, __n_n, __jp_n, __np_n; 625 __sph_bessel_jn(__n, __x, __j_n, __n_n, __jp_n, __np_n); 626 return __n_n; 627 } 628 } 629 630 _GLIBCXX_END_NAMESPACE_VERSION 631 } // namespace __detail 632 #undef _GLIBCXX_MATH_NS 633 #if ! __STDCPP_WANT_MATH_SPEC_FUNCS__ && defined(_GLIBCXX_TR1_CMATH) 634 } // namespace tr1 635 #endif 636 } 637 638 #endif // _GLIBCXX_TR1_BESSEL_FUNCTION_TCC 639