bessel_function.tcc 22 KB

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