bessel_k1.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. // Copyright (c) 2006 Xiaogang Zhang
  2. // Copyright (c) 2017 John Maddock
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_BESSEL_K1_HPP
  7. #define BOOST_MATH_BESSEL_K1_HPP
  8. #ifdef _MSC_VER
  9. #pragma once
  10. #pragma warning(push)
  11. #pragma warning(disable:4702) // Unreachable code (release mode only warning)
  12. #endif
  13. #include <boost/math/tools/rational.hpp>
  14. #include <boost/math/tools/big_constant.hpp>
  15. #include <boost/math/policies/error_handling.hpp>
  16. #include <boost/math/tools/assert.hpp>
  17. #if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
  18. //
  19. // This is the only way we can avoid
  20. // warning: non-standard suffix on floating constant [-Wpedantic]
  21. // when building with -Wall -pedantic. Neither __extension__
  22. // nor #pragma diagnostic ignored work :(
  23. //
  24. #pragma GCC system_header
  25. #endif
  26. // Modified Bessel function of the second kind of order zero
  27. // minimax rational approximations on intervals, see
  28. // Russon and Blair, Chalk River Report AECL-3461, 1969,
  29. // as revised by Pavel Holoborodko in "Rational Approximations
  30. // for the Modified Bessel Function of the Second Kind - K0(x)
  31. // for Computations with Double Precision", see
  32. // http://www.advanpix.com/2016/01/05/rational-approximations-for-the-modified-bessel-function-of-the-second-kind-k1-for-computations-with-double-precision/
  33. //
  34. // The actual coefficients used are our own derivation (by JM)
  35. // since we extend to both greater and lesser precision than the
  36. // references above. We can also improve performance WRT to
  37. // Holoborodko without loss of precision.
  38. namespace boost { namespace math { namespace detail{
  39. template <typename T>
  40. T bessel_k1(const T&);
  41. template <class T, class tag>
  42. struct bessel_k1_initializer
  43. {
  44. struct init
  45. {
  46. init()
  47. {
  48. do_init(tag());
  49. }
  50. static void do_init(const std::integral_constant<int, 113>&)
  51. {
  52. bessel_k1(T(0.5));
  53. bessel_k1(T(2));
  54. bessel_k1(T(6));
  55. }
  56. static void do_init(const std::integral_constant<int, 64>&)
  57. {
  58. bessel_k1(T(0.5));
  59. bessel_k1(T(6));
  60. }
  61. template <class U>
  62. static void do_init(const U&) {}
  63. void force_instantiate()const {}
  64. };
  65. static const init initializer;
  66. static void force_instantiate()
  67. {
  68. initializer.force_instantiate();
  69. }
  70. };
  71. template <class T, class tag>
  72. const typename bessel_k1_initializer<T, tag>::init bessel_k1_initializer<T, tag>::initializer;
  73. template <typename T, int N>
  74. inline T bessel_k1_imp(const T&, const std::integral_constant<int, N>&)
  75. {
  76. BOOST_MATH_ASSERT(0);
  77. return 0;
  78. }
  79. template <typename T>
  80. T bessel_k1_imp(const T& x, const std::integral_constant<int, 24>&)
  81. {
  82. BOOST_MATH_STD_USING
  83. if(x <= 1)
  84. {
  85. // Maximum Deviation Found: 3.090e-12
  86. // Expected Error Term : -3.053e-12
  87. // Maximum Relative Change in Control Points : 4.927e-02
  88. // Max Error found at float precision = Poly : 7.918347e-10
  89. static const T Y = 8.695471287e-02f;
  90. static const T P[] =
  91. {
  92. -3.621379531e-03f,
  93. 7.131781976e-03f,
  94. -1.535278300e-05f
  95. };
  96. static const T Q[] =
  97. {
  98. 1.000000000e+00f,
  99. -5.173102701e-02f,
  100. 9.203530671e-04f
  101. };
  102. T a = x * x / 4;
  103. a = ((tools::evaluate_rational(P, Q, a) + Y) * a * a + a / 2 + 1) * x / 2;
  104. // Maximum Deviation Found: 3.556e-08
  105. // Expected Error Term : -3.541e-08
  106. // Maximum Relative Change in Control Points : 8.203e-02
  107. static const T P2[] =
  108. {
  109. -3.079657469e-01f,
  110. -8.537108913e-02f,
  111. -4.640275408e-03f,
  112. -1.156442414e-04f
  113. };
  114. return tools::evaluate_polynomial(P2, T(x * x)) * x + 1 / x + log(x) * a;
  115. }
  116. else
  117. {
  118. // Maximum Deviation Found: 3.369e-08
  119. // Expected Error Term : -3.227e-08
  120. // Maximum Relative Change in Control Points : 9.917e-02
  121. // Max Error found at float precision = Poly : 6.084411e-08
  122. static const T Y = 1.450342178f;
  123. static const T P[] =
  124. {
  125. -1.970280088e-01f,
  126. 2.188747807e-02f,
  127. 7.270394756e-01f,
  128. 2.490678196e-01f
  129. };
  130. static const T Q[] =
  131. {
  132. 1.000000000e+00f,
  133. 2.274292882e+00f,
  134. 9.904984851e-01f,
  135. 4.585534549e-02f
  136. };
  137. if(x < tools::log_max_value<T>())
  138. return ((tools::evaluate_rational(P, Q, T(1 / x)) + Y) * exp(-x) / sqrt(x));
  139. else
  140. {
  141. T ex = exp(-x / 2);
  142. return ((tools::evaluate_rational(P, Q, T(1 / x)) + Y) * ex / sqrt(x)) * ex;
  143. }
  144. }
  145. }
  146. template <typename T>
  147. T bessel_k1_imp(const T& x, const std::integral_constant<int, 53>&)
  148. {
  149. BOOST_MATH_STD_USING
  150. if(x <= 1)
  151. {
  152. // Maximum Deviation Found: 1.922e-17
  153. // Expected Error Term : 1.921e-17
  154. // Maximum Relative Change in Control Points : 5.287e-03
  155. // Max Error found at double precision = Poly : 2.004747e-17
  156. static const T Y = 8.69547128677368164e-02f;
  157. static const T P[] =
  158. {
  159. -3.62137953440350228e-03,
  160. 7.11842087490330300e-03,
  161. 1.00302560256614306e-05,
  162. 1.77231085381040811e-06
  163. };
  164. static const T Q[] =
  165. {
  166. 1.00000000000000000e+00,
  167. -4.80414794429043831e-02,
  168. 9.85972641934416525e-04,
  169. -8.91196859397070326e-06
  170. };
  171. T a = x * x / 4;
  172. a = ((tools::evaluate_rational(P, Q, a) + Y) * a * a + a / 2 + 1) * x / 2;
  173. // Maximum Deviation Found: 4.053e-17
  174. // Expected Error Term : -4.053e-17
  175. // Maximum Relative Change in Control Points : 3.103e-04
  176. // Max Error found at double precision = Poly : 1.246698e-16
  177. static const T P2[] =
  178. {
  179. -3.07965757829206184e-01,
  180. -7.80929703673074907e-02,
  181. -2.70619343754051620e-03,
  182. -2.49549522229072008e-05
  183. };
  184. static const T Q2[] =
  185. {
  186. 1.00000000000000000e+00,
  187. -2.36316836412163098e-02,
  188. 2.64524577525962719e-04,
  189. -1.49749618004162787e-06
  190. };
  191. return tools::evaluate_rational(P2, Q2, T(x * x)) * x + 1 / x + log(x) * a;
  192. }
  193. else
  194. {
  195. // Maximum Deviation Found: 8.883e-17
  196. // Expected Error Term : -1.641e-17
  197. // Maximum Relative Change in Control Points : 2.786e-01
  198. // Max Error found at double precision = Poly : 1.258798e-16
  199. static const T Y = 1.45034217834472656f;
  200. static const T P[] =
  201. {
  202. -1.97028041029226295e-01,
  203. -2.32408961548087617e+00,
  204. -7.98269784507699938e+00,
  205. -2.39968410774221632e+00,
  206. 3.28314043780858713e+01,
  207. 5.67713761158496058e+01,
  208. 3.30907788466509823e+01,
  209. 6.62582288933739787e+00,
  210. 3.08851840645286691e-01
  211. };
  212. static const T Q[] =
  213. {
  214. 1.00000000000000000e+00,
  215. 1.41811409298826118e+01,
  216. 7.35979466317556420e+01,
  217. 1.77821793937080859e+02,
  218. 2.11014501598705982e+02,
  219. 1.19425262951064454e+02,
  220. 2.88448064302447607e+01,
  221. 2.27912927104139732e+00,
  222. 2.50358186953478678e-02
  223. };
  224. if(x < tools::log_max_value<T>())
  225. return ((tools::evaluate_rational(P, Q, T(1 / x)) + Y) * exp(-x) / sqrt(x));
  226. else
  227. {
  228. T ex = exp(-x / 2);
  229. return ((tools::evaluate_rational(P, Q, T(1 / x)) + Y) * ex / sqrt(x)) * ex;
  230. }
  231. }
  232. }
  233. template <typename T>
  234. T bessel_k1_imp(const T& x, const std::integral_constant<int, 64>&)
  235. {
  236. BOOST_MATH_STD_USING
  237. if(x <= 1)
  238. {
  239. // Maximum Deviation Found: 5.549e-23
  240. // Expected Error Term : -5.548e-23
  241. // Maximum Relative Change in Control Points : 2.002e-03
  242. // Max Error found at float80 precision = Poly : 9.352785e-22
  243. static const T Y = 8.695471286773681640625e-02f;
  244. static const T P[] =
  245. {
  246. BOOST_MATH_BIG_CONSTANT(T, 64, -3.621379534403483072861e-03),
  247. BOOST_MATH_BIG_CONSTANT(T, 64, 7.102135866103952705932e-03),
  248. BOOST_MATH_BIG_CONSTANT(T, 64, 4.167545240236717601167e-05),
  249. BOOST_MATH_BIG_CONSTANT(T, 64, 2.537484002571894870830e-06),
  250. BOOST_MATH_BIG_CONSTANT(T, 64, 6.603228256820000135990e-09)
  251. };
  252. static const T Q[] =
  253. {
  254. BOOST_MATH_BIG_CONSTANT(T, 64, 1.000000000000000000000e+00),
  255. BOOST_MATH_BIG_CONSTANT(T, 64, -4.354457194045068370363e-02),
  256. BOOST_MATH_BIG_CONSTANT(T, 64, 8.709137201220209072820e-04),
  257. BOOST_MATH_BIG_CONSTANT(T, 64, -9.676151796359590545143e-06),
  258. BOOST_MATH_BIG_CONSTANT(T, 64, 5.162715192766245311659e-08)
  259. };
  260. T a = x * x / 4;
  261. a = ((tools::evaluate_rational(P, Q, a) + Y) * a * a + a / 2 + 1) * x / 2;
  262. // Maximum Deviation Found: 1.995e-23
  263. // Expected Error Term : 1.995e-23
  264. // Maximum Relative Change in Control Points : 8.174e-04
  265. // Max Error found at float80 precision = Poly : 4.137325e-20
  266. static const T P2[] =
  267. {
  268. BOOST_MATH_BIG_CONSTANT(T, 64, -3.079657578292062244054e-01),
  269. BOOST_MATH_BIG_CONSTANT(T, 64, -7.963049154965966503231e-02),
  270. BOOST_MATH_BIG_CONSTANT(T, 64, -3.103277523735639924895e-03),
  271. BOOST_MATH_BIG_CONSTANT(T, 64, -4.023052834702215699504e-05),
  272. BOOST_MATH_BIG_CONSTANT(T, 64, -1.719459155018493821839e-07)
  273. };
  274. static const T Q2[] =
  275. {
  276. BOOST_MATH_BIG_CONSTANT(T, 64, 1.000000000000000000000e+00),
  277. BOOST_MATH_BIG_CONSTANT(T, 64, -1.863917670410152669768e-02),
  278. BOOST_MATH_BIG_CONSTANT(T, 64, 1.699367098849735298090e-04),
  279. BOOST_MATH_BIG_CONSTANT(T, 64, -9.309358790546076298429e-07),
  280. BOOST_MATH_BIG_CONSTANT(T, 64, 2.708893480271612711933e-09)
  281. };
  282. return tools::evaluate_rational(P2, Q2, T(x * x)) * x + 1 / x + log(x) * a;
  283. }
  284. else
  285. {
  286. // Maximum Deviation Found: 9.785e-20
  287. // Expected Error Term : -3.302e-21
  288. // Maximum Relative Change in Control Points : 3.432e-01
  289. // Max Error found at float80 precision = Poly : 1.083755e-19
  290. static const T Y = 1.450342178344726562500e+00f;
  291. static const T P[] =
  292. {
  293. BOOST_MATH_BIG_CONSTANT(T, 64, -1.970280410292263112917e-01),
  294. BOOST_MATH_BIG_CONSTANT(T, 64, -4.058564803062959169322e+00),
  295. BOOST_MATH_BIG_CONSTANT(T, 64, -3.036658174194917777473e+01),
  296. BOOST_MATH_BIG_CONSTANT(T, 64, -9.576825392332820142173e+01),
  297. BOOST_MATH_BIG_CONSTANT(T, 64, -6.706969489248020941949e+01),
  298. BOOST_MATH_BIG_CONSTANT(T, 64, 3.264572499406168221382e+02),
  299. BOOST_MATH_BIG_CONSTANT(T, 64, 8.584972047303151034100e+02),
  300. BOOST_MATH_BIG_CONSTANT(T, 64, 8.422082733280017909550e+02),
  301. BOOST_MATH_BIG_CONSTANT(T, 64, 3.738005441471368178383e+02),
  302. BOOST_MATH_BIG_CONSTANT(T, 64, 7.016938390144121276609e+01),
  303. BOOST_MATH_BIG_CONSTANT(T, 64, 4.319614662598089438939e+00),
  304. BOOST_MATH_BIG_CONSTANT(T, 64, 3.710715864316521856193e-02)
  305. };
  306. static const T Q[] =
  307. {
  308. BOOST_MATH_BIG_CONSTANT(T, 64, 1.000000000000000000000e+00),
  309. BOOST_MATH_BIG_CONSTANT(T, 64, 2.298433045824439052398e+01),
  310. BOOST_MATH_BIG_CONSTANT(T, 64, 2.082047745067709230037e+02),
  311. BOOST_MATH_BIG_CONSTANT(T, 64, 9.662367854250262046592e+02),
  312. BOOST_MATH_BIG_CONSTANT(T, 64, 2.504148628460454004686e+03),
  313. BOOST_MATH_BIG_CONSTANT(T, 64, 3.712730364911389908905e+03),
  314. BOOST_MATH_BIG_CONSTANT(T, 64, 3.108002081150068641112e+03),
  315. BOOST_MATH_BIG_CONSTANT(T, 64, 1.400149940532448553143e+03),
  316. BOOST_MATH_BIG_CONSTANT(T, 64, 3.083303048095846226299e+02),
  317. BOOST_MATH_BIG_CONSTANT(T, 64, 2.748706060530351833346e+01),
  318. BOOST_MATH_BIG_CONSTANT(T, 64, 6.321900849331506946977e-01),
  319. };
  320. if(x < tools::log_max_value<T>())
  321. return ((tools::evaluate_polynomial(P, T(1 / x)) / tools::evaluate_polynomial(Q, T(1 / x)) + Y) * exp(-x) / sqrt(x));
  322. else
  323. {
  324. T ex = exp(-x / 2);
  325. return ((tools::evaluate_polynomial(P, T(1 / x)) / tools::evaluate_polynomial(Q, T(1 / x)) + Y) * ex / sqrt(x)) * ex;
  326. }
  327. }
  328. }
  329. template <typename T>
  330. T bessel_k1_imp(const T& x, const std::integral_constant<int, 113>&)
  331. {
  332. BOOST_MATH_STD_USING
  333. if(x <= 1)
  334. {
  335. // Maximum Deviation Found: 7.120e-35
  336. // Expected Error Term : -7.119e-35
  337. // Maximum Relative Change in Control Points : 1.207e-03
  338. // Max Error found at float128 precision = Poly : 7.143688e-35
  339. static const T Y = 8.695471286773681640625000000000000000e-02f;
  340. static const T P[] =
  341. {
  342. BOOST_MATH_BIG_CONSTANT(T, 113, -3.621379534403483072916666666666595475e-03),
  343. BOOST_MATH_BIG_CONSTANT(T, 113, 7.074117676930975433219826471336547627e-03),
  344. BOOST_MATH_BIG_CONSTANT(T, 113, 9.631337631362776369069668419033041661e-05),
  345. BOOST_MATH_BIG_CONSTANT(T, 113, 3.468935967870048731821071646104412775e-06),
  346. BOOST_MATH_BIG_CONSTANT(T, 113, 2.956705020559599861444492614737168261e-08),
  347. BOOST_MATH_BIG_CONSTANT(T, 113, 2.347140307321161346703214099534250263e-10),
  348. BOOST_MATH_BIG_CONSTANT(T, 113, 5.569608494081482873946791086435679661e-13)
  349. };
  350. static const T Q[] =
  351. {
  352. BOOST_MATH_BIG_CONSTANT(T, 113, 1.000000000000000000000000000000000000e+00),
  353. BOOST_MATH_BIG_CONSTANT(T, 113, -3.580768910152105375615558920428350204e-02),
  354. BOOST_MATH_BIG_CONSTANT(T, 113, 6.197467671701485365363068445534557369e-04),
  355. BOOST_MATH_BIG_CONSTANT(T, 113, -6.707466533308630411966030561446666237e-06),
  356. BOOST_MATH_BIG_CONSTANT(T, 113, 4.846687802282250112624373388491123527e-08),
  357. BOOST_MATH_BIG_CONSTANT(T, 113, -2.248493131151981569517383040323900343e-10),
  358. BOOST_MATH_BIG_CONSTANT(T, 113, 5.319279786372775264555728921709381080e-13)
  359. };
  360. T a = x * x / 4;
  361. a = ((tools::evaluate_rational(P, Q, a) + Y) * a * a + a / 2 + 1) * x / 2;
  362. // Maximum Deviation Found: 4.473e-37
  363. // Expected Error Term : 4.473e-37
  364. // Maximum Relative Change in Control Points : 8.550e-04
  365. // Max Error found at float128 precision = Poly : 8.167701e-35
  366. static const T P2[] =
  367. {
  368. BOOST_MATH_BIG_CONSTANT(T, 113, -3.079657578292062244053600156878870690e-01),
  369. BOOST_MATH_BIG_CONSTANT(T, 113, -8.133183745732467770755578848987414875e-02),
  370. BOOST_MATH_BIG_CONSTANT(T, 113, -3.548968792764174773125420229299431951e-03),
  371. BOOST_MATH_BIG_CONSTANT(T, 113, -5.886125468718182876076972186152445490e-05),
  372. BOOST_MATH_BIG_CONSTANT(T, 113, -4.506712111733707245745396404449639865e-07),
  373. BOOST_MATH_BIG_CONSTANT(T, 113, -1.632502325880313239698965376754406011e-09),
  374. BOOST_MATH_BIG_CONSTANT(T, 113, -2.311973065898784812266544485665624227e-12)
  375. };
  376. static const T Q2[] =
  377. {
  378. BOOST_MATH_BIG_CONSTANT(T, 113, 1.000000000000000000000000000000000000e+00),
  379. BOOST_MATH_BIG_CONSTANT(T, 113, -1.311471216733781016657962995723287450e-02),
  380. BOOST_MATH_BIG_CONSTANT(T, 113, 8.571876054797365417068164018709472969e-05),
  381. BOOST_MATH_BIG_CONSTANT(T, 113, -3.630181215268238731442496851497901293e-07),
  382. BOOST_MATH_BIG_CONSTANT(T, 113, 1.070176111227805048604885986867484807e-09),
  383. BOOST_MATH_BIG_CONSTANT(T, 113, -2.129046580769872602793220056461084761e-12),
  384. BOOST_MATH_BIG_CONSTANT(T, 113, 2.294906469421390890762001971790074432e-15)
  385. };
  386. return tools::evaluate_rational(P2, Q2, T(x * x)) * x + 1 / x + log(x) * a;
  387. }
  388. else if(x < 4)
  389. {
  390. // Max error in interpolated form: 5.307e-37
  391. // Max Error found at float128 precision = Poly: 7.087862e-35
  392. static const T Y = 1.5023040771484375f;
  393. static const T P[] =
  394. {
  395. BOOST_MATH_BIG_CONSTANT(T, 113, -2.489899398329369710528254347931380044e-01),
  396. BOOST_MATH_BIG_CONSTANT(T, 113, -6.819080211203854781858815596508456873e+00),
  397. BOOST_MATH_BIG_CONSTANT(T, 113, -7.599915699069767382647695624952723034e+01),
  398. BOOST_MATH_BIG_CONSTANT(T, 113, -4.450211910821295507926582231071300718e+02),
  399. BOOST_MATH_BIG_CONSTANT(T, 113, -1.451374687870925175794150513723956533e+03),
  400. BOOST_MATH_BIG_CONSTANT(T, 113, -2.405805746895098802803503988539098226e+03),
  401. BOOST_MATH_BIG_CONSTANT(T, 113, -5.638808326778389656403861103277220518e+02),
  402. BOOST_MATH_BIG_CONSTANT(T, 113, 5.513958744081268456191778822780865708e+03),
  403. BOOST_MATH_BIG_CONSTANT(T, 113, 1.121301640926540743072258116122834804e+04),
  404. BOOST_MATH_BIG_CONSTANT(T, 113, 1.080094900175649541266613109971296190e+04),
  405. BOOST_MATH_BIG_CONSTANT(T, 113, 5.896531083639613332407534434915552429e+03),
  406. BOOST_MATH_BIG_CONSTANT(T, 113, 1.856602122319645694042555107114028437e+03),
  407. BOOST_MATH_BIG_CONSTANT(T, 113, 3.237121918853145421414003823957537419e+02),
  408. BOOST_MATH_BIG_CONSTANT(T, 113, 2.842072954561323076230238664623893504e+01),
  409. BOOST_MATH_BIG_CONSTANT(T, 113, 1.039705646510167437971862966128055524e+00),
  410. BOOST_MATH_BIG_CONSTANT(T, 113, 1.008418100718254816100425022904039530e-02)
  411. };
  412. static const T Q[] =
  413. {
  414. BOOST_MATH_BIG_CONSTANT(T, 113, 1.000000000000000000000000000000000000e+00),
  415. BOOST_MATH_BIG_CONSTANT(T, 113, 2.927456835239137986889227412815459529e+01),
  416. BOOST_MATH_BIG_CONSTANT(T, 113, 3.598985593265577043711382994516531273e+02),
  417. BOOST_MATH_BIG_CONSTANT(T, 113, 2.449897377085510281395819892689690579e+03),
  418. BOOST_MATH_BIG_CONSTANT(T, 113, 1.025555887684561913263090023158085327e+04),
  419. BOOST_MATH_BIG_CONSTANT(T, 113, 2.774140447181062463181892531100679195e+04),
  420. BOOST_MATH_BIG_CONSTANT(T, 113, 4.962055507843204417243602332246120418e+04),
  421. BOOST_MATH_BIG_CONSTANT(T, 113, 5.908269326976180183216954452196772931e+04),
  422. BOOST_MATH_BIG_CONSTANT(T, 113, 4.655160454422016855911700790722577942e+04),
  423. BOOST_MATH_BIG_CONSTANT(T, 113, 2.383586885019548163464418964577684608e+04),
  424. BOOST_MATH_BIG_CONSTANT(T, 113, 7.679920375586960324298491662159976419e+03),
  425. BOOST_MATH_BIG_CONSTANT(T, 113, 1.478586421028842906987799049804565008e+03),
  426. BOOST_MATH_BIG_CONSTANT(T, 113, 1.565384974896746094224942654383537090e+02),
  427. BOOST_MATH_BIG_CONSTANT(T, 113, 7.902617937084010911005732488607114511e+00),
  428. BOOST_MATH_BIG_CONSTANT(T, 113, 1.429293010387921526110949911029094926e-01),
  429. BOOST_MATH_BIG_CONSTANT(T, 113, 3.880342607911083143560111853491047663e-04)
  430. };
  431. return ((tools::evaluate_polynomial(P, T(1 / x)) / tools::evaluate_polynomial(Q, T(1 / x)) + Y) * exp(-x) / sqrt(x));
  432. }
  433. else
  434. {
  435. // Maximum Deviation Found: 4.359e-37
  436. // Expected Error Term : -6.565e-40
  437. // Maximum Relative Change in Control Points : 1.880e-01
  438. // Max Error found at float128 precision = Poly : 2.943572e-35
  439. static const T Y = 1.308816909790039062500000000000000000f;
  440. static const T P[] =
  441. {
  442. BOOST_MATH_BIG_CONSTANT(T, 113, -5.550277247453881129211735759447737350e-02),
  443. BOOST_MATH_BIG_CONSTANT(T, 113, -3.485883080219574328217554864956175929e+00),
  444. BOOST_MATH_BIG_CONSTANT(T, 113, -8.903760658131484239300875153154881958e+01),
  445. BOOST_MATH_BIG_CONSTANT(T, 113, -1.144813672213626237418235110712293337e+03),
  446. BOOST_MATH_BIG_CONSTANT(T, 113, -6.498400501156131446691826557494158173e+03),
  447. BOOST_MATH_BIG_CONSTANT(T, 113, 1.573531831870363502604119835922166116e+04),
  448. BOOST_MATH_BIG_CONSTANT(T, 113, 5.417416550054632009958262596048841154e+05),
  449. BOOST_MATH_BIG_CONSTANT(T, 113, 4.271266450613557412825896604269130661e+06),
  450. BOOST_MATH_BIG_CONSTANT(T, 113, 1.898386013314389952534433455681107783e+07),
  451. BOOST_MATH_BIG_CONSTANT(T, 113, 5.353798784656436259250791761023512750e+07),
  452. BOOST_MATH_BIG_CONSTANT(T, 113, 9.839619195427352438957774052763490067e+07),
  453. BOOST_MATH_BIG_CONSTANT(T, 113, 1.169246368651532232388152442538005637e+08),
  454. BOOST_MATH_BIG_CONSTANT(T, 113, 8.696368884166831199967845883371116431e+07),
  455. BOOST_MATH_BIG_CONSTANT(T, 113, 3.810226630422736458064005843327500169e+07),
  456. BOOST_MATH_BIG_CONSTANT(T, 113, 8.854996610560406127438950635716757614e+06),
  457. BOOST_MATH_BIG_CONSTANT(T, 113, 8.981057433937398731355768088809437625e+05),
  458. BOOST_MATH_BIG_CONSTANT(T, 113, 2.519440069856232098711793483639792952e+04)
  459. };
  460. static const T Q[] =
  461. {
  462. BOOST_MATH_BIG_CONSTANT(T, 113, 1.000000000000000000000000000000000000e+00),
  463. BOOST_MATH_BIG_CONSTANT(T, 113, 7.127348248283623146544565916604103560e+01),
  464. BOOST_MATH_BIG_CONSTANT(T, 113, 2.205092684176906740104488180754982065e+03),
  465. BOOST_MATH_BIG_CONSTANT(T, 113, 3.911249195069050636298346469740075758e+04),
  466. BOOST_MATH_BIG_CONSTANT(T, 113, 4.426103406579046249654548481377792614e+05),
  467. BOOST_MATH_BIG_CONSTANT(T, 113, 3.365861555422488771286500241966208541e+06),
  468. BOOST_MATH_BIG_CONSTANT(T, 113, 1.765377714160383676864913709252529840e+07),
  469. BOOST_MATH_BIG_CONSTANT(T, 113, 6.453822726931857253365138260720815246e+07),
  470. BOOST_MATH_BIG_CONSTANT(T, 113, 1.643207885048369990391975749439783892e+08),
  471. BOOST_MATH_BIG_CONSTANT(T, 113, 2.882540678243694621895816336640877878e+08),
  472. BOOST_MATH_BIG_CONSTANT(T, 113, 3.410120808992380266174106812005338148e+08),
  473. BOOST_MATH_BIG_CONSTANT(T, 113, 2.628138016559335882019310900426773027e+08),
  474. BOOST_MATH_BIG_CONSTANT(T, 113, 1.250794693811010646965360198541047961e+08),
  475. BOOST_MATH_BIG_CONSTANT(T, 113, 3.378723408195485594610593014072950078e+07),
  476. BOOST_MATH_BIG_CONSTANT(T, 113, 4.488253856312453816451380319061865560e+06),
  477. BOOST_MATH_BIG_CONSTANT(T, 113, 2.202167197882689873967723350537104582e+05),
  478. BOOST_MATH_BIG_CONSTANT(T, 113, 1.673233230356966539460728211412989843e+03)
  479. };
  480. if(x < tools::log_max_value<T>())
  481. return ((tools::evaluate_polynomial(P, T(1 / x)) / tools::evaluate_polynomial(Q, T(1 / x)) + Y) * exp(-x) / sqrt(x));
  482. else
  483. {
  484. T ex = exp(-x / 2);
  485. return ((tools::evaluate_polynomial(P, T(1 / x)) / tools::evaluate_polynomial(Q, T(1 / x)) + Y) * ex / sqrt(x)) * ex;
  486. }
  487. }
  488. }
  489. template <typename T>
  490. T bessel_k1_imp(const T& x, const std::integral_constant<int, 0>&)
  491. {
  492. if(boost::math::tools::digits<T>() <= 24)
  493. return bessel_k1_imp(x, std::integral_constant<int, 24>());
  494. else if(boost::math::tools::digits<T>() <= 53)
  495. return bessel_k1_imp(x, std::integral_constant<int, 53>());
  496. else if(boost::math::tools::digits<T>() <= 64)
  497. return bessel_k1_imp(x, std::integral_constant<int, 64>());
  498. else if(boost::math::tools::digits<T>() <= 113)
  499. return bessel_k1_imp(x, std::integral_constant<int, 113>());
  500. BOOST_MATH_ASSERT(0);
  501. return 0;
  502. }
  503. template <typename T>
  504. inline T bessel_k1(const T& x)
  505. {
  506. typedef std::integral_constant<int,
  507. ((std::numeric_limits<T>::digits == 0) || (std::numeric_limits<T>::radix != 2)) ?
  508. 0 :
  509. std::numeric_limits<T>::digits <= 24 ?
  510. 24 :
  511. std::numeric_limits<T>::digits <= 53 ?
  512. 53 :
  513. std::numeric_limits<T>::digits <= 64 ?
  514. 64 :
  515. std::numeric_limits<T>::digits <= 113 ?
  516. 113 : -1
  517. > tag_type;
  518. bessel_k1_initializer<T, tag_type>::force_instantiate();
  519. return bessel_k1_imp(x, tag_type());
  520. }
  521. }}} // namespaces
  522. #ifdef _MSC_VER
  523. #pragma warning(pop)
  524. #endif
  525. #endif // BOOST_MATH_BESSEL_K1_HPP