Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

670 lines
22KB

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