modified_bessel_func.tcc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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/modified_bessel_func.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. 246-249.
  40. #ifndef _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC
  41. #define _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_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. #elif defined(_GLIBCXX_TR1_CMATH)
  48. namespace tr1
  49. {
  50. #else
  51. # error do not include this header directly, use <cmath> or <tr1/cmath>
  52. #endif
  53. // [5.2] Special functions
  54. // Implementation-space details.
  55. namespace __detail
  56. {
  57. /**
  58. * @brief Compute the modified Bessel functions @f$ I_\nu(x) @f$ and
  59. * @f$ K_\nu(x) @f$ and their first derivatives
  60. * @f$ I'_\nu(x) @f$ and @f$ K'_\nu(x) @f$ respectively.
  61. * These four functions are computed together for numerical
  62. * stability.
  63. *
  64. * @param __nu The order of the Bessel functions.
  65. * @param __x The argument of the Bessel functions.
  66. * @param __Inu The output regular modified Bessel function.
  67. * @param __Knu The output irregular modified Bessel function.
  68. * @param __Ipnu The output derivative of the regular
  69. * modified Bessel function.
  70. * @param __Kpnu The output derivative of the irregular
  71. * modified Bessel function.
  72. */
  73. template <typename _Tp>
  74. void
  75. __bessel_ik(_Tp __nu, _Tp __x,
  76. _Tp & __Inu, _Tp & __Knu, _Tp & __Ipnu, _Tp & __Kpnu)
  77. {
  78. if (__x == _Tp(0))
  79. {
  80. if (__nu == _Tp(0))
  81. {
  82. __Inu = _Tp(1);
  83. __Ipnu = _Tp(0);
  84. }
  85. else if (__nu == _Tp(1))
  86. {
  87. __Inu = _Tp(0);
  88. __Ipnu = _Tp(0.5L);
  89. }
  90. else
  91. {
  92. __Inu = _Tp(0);
  93. __Ipnu = _Tp(0);
  94. }
  95. __Knu = std::numeric_limits<_Tp>::infinity();
  96. __Kpnu = -std::numeric_limits<_Tp>::infinity();
  97. return;
  98. }
  99. const _Tp __eps = std::numeric_limits<_Tp>::epsilon();
  100. const _Tp __fp_min = _Tp(10) * std::numeric_limits<_Tp>::epsilon();
  101. const int __max_iter = 15000;
  102. const _Tp __x_min = _Tp(2);
  103. const int __nl = static_cast<int>(__nu + _Tp(0.5L));
  104. const _Tp __mu = __nu - __nl;
  105. const _Tp __mu2 = __mu * __mu;
  106. const _Tp __xi = _Tp(1) / __x;
  107. const _Tp __xi2 = _Tp(2) * __xi;
  108. _Tp __h = __nu * __xi;
  109. if ( __h < __fp_min )
  110. __h = __fp_min;
  111. _Tp __b = __xi2 * __nu;
  112. _Tp __d = _Tp(0);
  113. _Tp __c = __h;
  114. int __i;
  115. for ( __i = 1; __i <= __max_iter; ++__i )
  116. {
  117. __b += __xi2;
  118. __d = _Tp(1) / (__b + __d);
  119. __c = __b + _Tp(1) / __c;
  120. const _Tp __del = __c * __d;
  121. __h *= __del;
  122. if (std::abs(__del - _Tp(1)) < __eps)
  123. break;
  124. }
  125. if (__i > __max_iter)
  126. std::__throw_runtime_error(__N("Argument x too large "
  127. "in __bessel_ik; "
  128. "try asymptotic expansion."));
  129. _Tp __Inul = __fp_min;
  130. _Tp __Ipnul = __h * __Inul;
  131. _Tp __Inul1 = __Inul;
  132. _Tp __Ipnu1 = __Ipnul;
  133. _Tp __fact = __nu * __xi;
  134. for (int __l = __nl; __l >= 1; --__l)
  135. {
  136. const _Tp __Inutemp = __fact * __Inul + __Ipnul;
  137. __fact -= __xi;
  138. __Ipnul = __fact * __Inutemp + __Inul;
  139. __Inul = __Inutemp;
  140. }
  141. _Tp __f = __Ipnul / __Inul;
  142. _Tp __Kmu, __Knu1;
  143. if (__x < __x_min)
  144. {
  145. const _Tp __x2 = __x / _Tp(2);
  146. const _Tp __pimu = __numeric_constants<_Tp>::__pi() * __mu;
  147. const _Tp __fact = (std::abs(__pimu) < __eps
  148. ? _Tp(1) : __pimu / std::sin(__pimu));
  149. _Tp __d = -std::log(__x2);
  150. _Tp __e = __mu * __d;
  151. const _Tp __fact2 = (std::abs(__e) < __eps
  152. ? _Tp(1) : std::sinh(__e) / __e);
  153. _Tp __gam1, __gam2, __gampl, __gammi;
  154. __gamma_temme(__mu, __gam1, __gam2, __gampl, __gammi);
  155. _Tp __ff = __fact
  156. * (__gam1 * std::cosh(__e) + __gam2 * __fact2 * __d);
  157. _Tp __sum = __ff;
  158. __e = std::exp(__e);
  159. _Tp __p = __e / (_Tp(2) * __gampl);
  160. _Tp __q = _Tp(1) / (_Tp(2) * __e * __gammi);
  161. _Tp __c = _Tp(1);
  162. __d = __x2 * __x2;
  163. _Tp __sum1 = __p;
  164. int __i;
  165. for (__i = 1; __i <= __max_iter; ++__i)
  166. {
  167. __ff = (__i * __ff + __p + __q) / (__i * __i - __mu2);
  168. __c *= __d / __i;
  169. __p /= __i - __mu;
  170. __q /= __i + __mu;
  171. const _Tp __del = __c * __ff;
  172. __sum += __del;
  173. const _Tp __del1 = __c * (__p - __i * __ff);
  174. __sum1 += __del1;
  175. if (std::abs(__del) < __eps * std::abs(__sum))
  176. break;
  177. }
  178. if (__i > __max_iter)
  179. std::__throw_runtime_error(__N("Bessel k series failed to converge "
  180. "in __bessel_ik."));
  181. __Kmu = __sum;
  182. __Knu1 = __sum1 * __xi2;
  183. }
  184. else
  185. {
  186. _Tp __b = _Tp(2) * (_Tp(1) + __x);
  187. _Tp __d = _Tp(1) / __b;
  188. _Tp __delh = __d;
  189. _Tp __h = __delh;
  190. _Tp __q1 = _Tp(0);
  191. _Tp __q2 = _Tp(1);
  192. _Tp __a1 = _Tp(0.25L) - __mu2;
  193. _Tp __q = __c = __a1;
  194. _Tp __a = -__a1;
  195. _Tp __s = _Tp(1) + __q * __delh;
  196. int __i;
  197. for (__i = 2; __i <= __max_iter; ++__i)
  198. {
  199. __a -= 2 * (__i - 1);
  200. __c = -__a * __c / __i;
  201. const _Tp __qnew = (__q1 - __b * __q2) / __a;
  202. __q1 = __q2;
  203. __q2 = __qnew;
  204. __q += __c * __qnew;
  205. __b += _Tp(2);
  206. __d = _Tp(1) / (__b + __a * __d);
  207. __delh = (__b * __d - _Tp(1)) * __delh;
  208. __h += __delh;
  209. const _Tp __dels = __q * __delh;
  210. __s += __dels;
  211. if ( std::abs(__dels / __s) < __eps )
  212. break;
  213. }
  214. if (__i > __max_iter)
  215. std::__throw_runtime_error(__N("Steed's method failed "
  216. "in __bessel_ik."));
  217. __h = __a1 * __h;
  218. __Kmu = std::sqrt(__numeric_constants<_Tp>::__pi() / (_Tp(2) * __x))
  219. * std::exp(-__x) / __s;
  220. __Knu1 = __Kmu * (__mu + __x + _Tp(0.5L) - __h) * __xi;
  221. }
  222. _Tp __Kpmu = __mu * __xi * __Kmu - __Knu1;
  223. _Tp __Inumu = __xi / (__f * __Kmu - __Kpmu);
  224. __Inu = __Inumu * __Inul1 / __Inul;
  225. __Ipnu = __Inumu * __Ipnu1 / __Inul;
  226. for ( __i = 1; __i <= __nl; ++__i )
  227. {
  228. const _Tp __Knutemp = (__mu + __i) * __xi2 * __Knu1 + __Kmu;
  229. __Kmu = __Knu1;
  230. __Knu1 = __Knutemp;
  231. }
  232. __Knu = __Kmu;
  233. __Kpnu = __nu * __xi * __Kmu - __Knu1;
  234. return;
  235. }
  236. /**
  237. * @brief Return the regular modified Bessel function of order
  238. * \f$ \nu \f$: \f$ I_{\nu}(x) \f$.
  239. *
  240. * The regular modified cylindrical Bessel function is:
  241. * @f[
  242. * I_{\nu}(x) = \sum_{k=0}^{\infty}
  243. * \frac{(x/2)^{\nu + 2k}}{k!\Gamma(\nu+k+1)}
  244. * @f]
  245. *
  246. * @param __nu The order of the regular modified Bessel function.
  247. * @param __x The argument of the regular modified Bessel function.
  248. * @return The output regular modified Bessel function.
  249. */
  250. template<typename _Tp>
  251. _Tp
  252. __cyl_bessel_i(_Tp __nu, _Tp __x)
  253. {
  254. if (__nu < _Tp(0) || __x < _Tp(0))
  255. std::__throw_domain_error(__N("Bad argument "
  256. "in __cyl_bessel_i."));
  257. else if (__isnan(__nu) || __isnan(__x))
  258. return std::numeric_limits<_Tp>::quiet_NaN();
  259. else if (__x * __x < _Tp(10) * (__nu + _Tp(1)))
  260. return __cyl_bessel_ij_series(__nu, __x, +_Tp(1), 200);
  261. else
  262. {
  263. _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
  264. __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
  265. return __I_nu;
  266. }
  267. }
  268. /**
  269. * @brief Return the irregular modified Bessel function
  270. * \f$ K_{\nu}(x) \f$ of order \f$ \nu \f$.
  271. *
  272. * The irregular modified Bessel function is defined by:
  273. * @f[
  274. * K_{\nu}(x) = \frac{\pi}{2}
  275. * \frac{I_{-\nu}(x) - I_{\nu}(x)}{\sin \nu\pi}
  276. * @f]
  277. * where for integral \f$ \nu = n \f$ a limit is taken:
  278. * \f$ lim_{\nu \to n} \f$.
  279. *
  280. * @param __nu The order of the irregular modified Bessel function.
  281. * @param __x The argument of the irregular modified Bessel function.
  282. * @return The output irregular modified Bessel function.
  283. */
  284. template<typename _Tp>
  285. _Tp
  286. __cyl_bessel_k(_Tp __nu, _Tp __x)
  287. {
  288. if (__nu < _Tp(0) || __x < _Tp(0))
  289. std::__throw_domain_error(__N("Bad argument "
  290. "in __cyl_bessel_k."));
  291. else if (__isnan(__nu) || __isnan(__x))
  292. return std::numeric_limits<_Tp>::quiet_NaN();
  293. else
  294. {
  295. _Tp __I_nu, __K_nu, __Ip_nu, __Kp_nu;
  296. __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
  297. return __K_nu;
  298. }
  299. }
  300. /**
  301. * @brief Compute the spherical modified Bessel functions
  302. * @f$ i_n(x) @f$ and @f$ k_n(x) @f$ and their first
  303. * derivatives @f$ i'_n(x) @f$ and @f$ k'_n(x) @f$
  304. * respectively.
  305. *
  306. * @param __n The order of the modified spherical Bessel function.
  307. * @param __x The argument of the modified spherical Bessel function.
  308. * @param __i_n The output regular modified spherical Bessel function.
  309. * @param __k_n The output irregular modified spherical
  310. * Bessel function.
  311. * @param __ip_n The output derivative of the regular modified
  312. * spherical Bessel function.
  313. * @param __kp_n The output derivative of the irregular modified
  314. * spherical Bessel function.
  315. */
  316. template <typename _Tp>
  317. void
  318. __sph_bessel_ik(unsigned int __n, _Tp __x,
  319. _Tp & __i_n, _Tp & __k_n, _Tp & __ip_n, _Tp & __kp_n)
  320. {
  321. const _Tp __nu = _Tp(__n) + _Tp(0.5L);
  322. _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
  323. __bessel_ik(__nu, __x, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
  324. const _Tp __factor = __numeric_constants<_Tp>::__sqrtpio2()
  325. / std::sqrt(__x);
  326. __i_n = __factor * __I_nu;
  327. __k_n = __factor * __K_nu;
  328. __ip_n = __factor * __Ip_nu - __i_n / (_Tp(2) * __x);
  329. __kp_n = __factor * __Kp_nu - __k_n / (_Tp(2) * __x);
  330. return;
  331. }
  332. /**
  333. * @brief Compute the Airy functions
  334. * @f$ Ai(x) @f$ and @f$ Bi(x) @f$ and their first
  335. * derivatives @f$ Ai'(x) @f$ and @f$ Bi(x) @f$
  336. * respectively.
  337. *
  338. * @param __x The argument of the Airy functions.
  339. * @param __Ai The output Airy function of the first kind.
  340. * @param __Bi The output Airy function of the second kind.
  341. * @param __Aip The output derivative of the Airy function
  342. * of the first kind.
  343. * @param __Bip The output derivative of the Airy function
  344. * of the second kind.
  345. */
  346. template <typename _Tp>
  347. void
  348. __airy(_Tp __x, _Tp & __Ai, _Tp & __Bi, _Tp & __Aip, _Tp & __Bip)
  349. {
  350. const _Tp __absx = std::abs(__x);
  351. const _Tp __rootx = std::sqrt(__absx);
  352. const _Tp __z = _Tp(2) * __absx * __rootx / _Tp(3);
  353. const _Tp _S_NaN = std::numeric_limits<_Tp>::quiet_NaN();
  354. const _Tp _S_inf = std::numeric_limits<_Tp>::infinity();
  355. if (__isnan(__x))
  356. __Bip = __Aip = __Bi = __Ai = std::numeric_limits<_Tp>::quiet_NaN();
  357. else if (__z == _S_inf)
  358. {
  359. __Aip = __Ai = _Tp(0);
  360. __Bip = __Bi = _S_inf;
  361. }
  362. else if (__z == -_S_inf)
  363. __Bip = __Aip = __Bi = __Ai = _Tp(0);
  364. else if (__x > _Tp(0))
  365. {
  366. _Tp __I_nu, __Ip_nu, __K_nu, __Kp_nu;
  367. __bessel_ik(_Tp(1) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
  368. __Ai = __rootx * __K_nu
  369. / (__numeric_constants<_Tp>::__sqrt3()
  370. * __numeric_constants<_Tp>::__pi());
  371. __Bi = __rootx * (__K_nu / __numeric_constants<_Tp>::__pi()
  372. + _Tp(2) * __I_nu / __numeric_constants<_Tp>::__sqrt3());
  373. __bessel_ik(_Tp(2) / _Tp(3), __z, __I_nu, __K_nu, __Ip_nu, __Kp_nu);
  374. __Aip = -__x * __K_nu
  375. / (__numeric_constants<_Tp>::__sqrt3()
  376. * __numeric_constants<_Tp>::__pi());
  377. __Bip = __x * (__K_nu / __numeric_constants<_Tp>::__pi()
  378. + _Tp(2) * __I_nu
  379. / __numeric_constants<_Tp>::__sqrt3());
  380. }
  381. else if (__x < _Tp(0))
  382. {
  383. _Tp __J_nu, __Jp_nu, __N_nu, __Np_nu;
  384. __bessel_jn(_Tp(1) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
  385. __Ai = __rootx * (__J_nu
  386. - __N_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
  387. __Bi = -__rootx * (__N_nu
  388. + __J_nu / __numeric_constants<_Tp>::__sqrt3()) / _Tp(2);
  389. __bessel_jn(_Tp(2) / _Tp(3), __z, __J_nu, __N_nu, __Jp_nu, __Np_nu);
  390. __Aip = __absx * (__N_nu / __numeric_constants<_Tp>::__sqrt3()
  391. + __J_nu) / _Tp(2);
  392. __Bip = __absx * (__J_nu / __numeric_constants<_Tp>::__sqrt3()
  393. - __N_nu) / _Tp(2);
  394. }
  395. else
  396. {
  397. // Reference:
  398. // Abramowitz & Stegun, page 446 section 10.4.4 on Airy functions.
  399. // The number is Ai(0) = 3^{-2/3}/\Gamma(2/3).
  400. __Ai = _Tp(0.35502805388781723926L);
  401. __Bi = __Ai * __numeric_constants<_Tp>::__sqrt3();
  402. // Reference:
  403. // Abramowitz & Stegun, page 446 section 10.4.5 on Airy functions.
  404. // The number is Ai'(0) = -3^{-1/3}/\Gamma(1/3).
  405. __Aip = -_Tp(0.25881940379280679840L);
  406. __Bip = -__Aip * __numeric_constants<_Tp>::__sqrt3();
  407. }
  408. return;
  409. }
  410. } // namespace __detail
  411. #if ! _GLIBCXX_USE_STD_SPEC_FUNCS && defined(_GLIBCXX_TR1_CMATH)
  412. } // namespace tr1
  413. #endif
  414. _GLIBCXX_END_NAMESPACE_VERSION
  415. }
  416. #endif // _GLIBCXX_TR1_MODIFIED_BESSEL_FUNC_TCC