bessel_ik.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // Copyright (c) 2006 Xiaogang Zhang
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_MATH_BESSEL_IK_HPP
  6. #define BOOST_MATH_BESSEL_IK_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. #include <cmath>
  11. #include <cstdint>
  12. #include <boost/math/special_functions/round.hpp>
  13. #include <boost/math/special_functions/gamma.hpp>
  14. #include <boost/math/special_functions/sin_pi.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. #include <boost/math/policies/error_handling.hpp>
  17. #include <boost/math/tools/config.hpp>
  18. // Modified Bessel functions of the first and second kind of fractional order
  19. namespace boost { namespace math {
  20. namespace detail {
  21. template <class T, class Policy>
  22. struct cyl_bessel_i_small_z
  23. {
  24. typedef T result_type;
  25. cyl_bessel_i_small_z(T v_, T z_) : k(0), v(v_), mult(z_*z_/4)
  26. {
  27. BOOST_MATH_STD_USING
  28. term = 1;
  29. }
  30. T operator()()
  31. {
  32. T result = term;
  33. ++k;
  34. term *= mult / k;
  35. term /= k + v;
  36. return result;
  37. }
  38. private:
  39. unsigned k;
  40. T v;
  41. T term;
  42. T mult;
  43. };
  44. template <class T, class Policy>
  45. inline T bessel_i_small_z_series(T v, T x, const Policy& pol)
  46. {
  47. BOOST_MATH_STD_USING
  48. T prefix;
  49. if(v < max_factorial<T>::value)
  50. {
  51. prefix = pow(x / 2, v) / boost::math::tgamma(v + 1, pol);
  52. }
  53. else
  54. {
  55. prefix = v * log(x / 2) - boost::math::lgamma(v + 1, pol);
  56. prefix = exp(prefix);
  57. }
  58. if(prefix == 0)
  59. return prefix;
  60. cyl_bessel_i_small_z<T, Policy> s(v, x);
  61. std::uintmax_t max_iter = policies::get_max_series_iterations<Policy>();
  62. T result = boost::math::tools::sum_series(s, boost::math::policies::get_epsilon<T, Policy>(), max_iter);
  63. policies::check_series_iterations<T>("boost::math::bessel_j_small_z_series<%1%>(%1%,%1%)", max_iter, pol);
  64. return prefix * result;
  65. }
  66. // Calculate K(v, x) and K(v+1, x) by method analogous to
  67. // Temme, Journal of Computational Physics, vol 21, 343 (1976)
  68. template <typename T, typename Policy>
  69. int temme_ik(T v, T x, T* result_K, T* K1, const Policy& pol)
  70. {
  71. T f, h, p, q, coef, sum, sum1, tolerance;
  72. T a, b, c, d, sigma, gamma1, gamma2;
  73. unsigned long k;
  74. BOOST_MATH_STD_USING
  75. using namespace boost::math::tools;
  76. using namespace boost::math::constants;
  77. // |x| <= 2, Temme series converge rapidly
  78. // |x| > 2, the larger the |x|, the slower the convergence
  79. BOOST_MATH_ASSERT(abs(x) <= 2);
  80. BOOST_MATH_ASSERT(abs(v) <= 0.5f);
  81. T gp = boost::math::tgamma1pm1(v, pol);
  82. T gm = boost::math::tgamma1pm1(-v, pol);
  83. a = log(x / 2);
  84. b = exp(v * a);
  85. sigma = -a * v;
  86. c = abs(v) < tools::epsilon<T>() ?
  87. T(1) : T(boost::math::sin_pi(v, pol) / (v * pi<T>()));
  88. d = abs(sigma) < tools::epsilon<T>() ?
  89. T(1) : T(sinh(sigma) / sigma);
  90. gamma1 = abs(v) < tools::epsilon<T>() ?
  91. T(-euler<T>()) : T((0.5f / v) * (gp - gm) * c);
  92. gamma2 = (2 + gp + gm) * c / 2;
  93. // initial values
  94. p = (gp + 1) / (2 * b);
  95. q = (1 + gm) * b / 2;
  96. f = (cosh(sigma) * gamma1 + d * (-a) * gamma2) / c;
  97. h = p;
  98. coef = 1;
  99. sum = coef * f;
  100. sum1 = coef * h;
  101. BOOST_MATH_INSTRUMENT_VARIABLE(p);
  102. BOOST_MATH_INSTRUMENT_VARIABLE(q);
  103. BOOST_MATH_INSTRUMENT_VARIABLE(f);
  104. BOOST_MATH_INSTRUMENT_VARIABLE(sigma);
  105. BOOST_MATH_INSTRUMENT_CODE(sinh(sigma));
  106. BOOST_MATH_INSTRUMENT_VARIABLE(gamma1);
  107. BOOST_MATH_INSTRUMENT_VARIABLE(gamma2);
  108. BOOST_MATH_INSTRUMENT_VARIABLE(c);
  109. BOOST_MATH_INSTRUMENT_VARIABLE(d);
  110. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  111. // series summation
  112. tolerance = tools::epsilon<T>();
  113. for (k = 1; k < policies::get_max_series_iterations<Policy>(); k++)
  114. {
  115. f = (k * f + p + q) / (k*k - v*v);
  116. p /= k - v;
  117. q /= k + v;
  118. h = p - k * f;
  119. coef *= x * x / (4 * k);
  120. sum += coef * f;
  121. sum1 += coef * h;
  122. if (abs(coef * f) < abs(sum) * tolerance)
  123. {
  124. break;
  125. }
  126. }
  127. policies::check_series_iterations<T>("boost::math::bessel_ik<%1%>(%1%,%1%) in temme_ik", k, pol);
  128. *result_K = sum;
  129. *K1 = 2 * sum1 / x;
  130. return 0;
  131. }
  132. // Evaluate continued fraction fv = I_(v+1) / I_v, derived from
  133. // Abramowitz and Stegun, Handbook of Mathematical Functions, 1972, 9.1.73
  134. template <typename T, typename Policy>
  135. int CF1_ik(T v, T x, T* fv, const Policy& pol)
  136. {
  137. T C, D, f, a, b, delta, tiny, tolerance;
  138. unsigned long k;
  139. BOOST_MATH_STD_USING
  140. // |x| <= |v|, CF1_ik converges rapidly
  141. // |x| > |v|, CF1_ik needs O(|x|) iterations to converge
  142. // modified Lentz's method, see
  143. // Lentz, Applied Optics, vol 15, 668 (1976)
  144. tolerance = 2 * tools::epsilon<T>();
  145. BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
  146. tiny = sqrt(tools::min_value<T>());
  147. BOOST_MATH_INSTRUMENT_VARIABLE(tiny);
  148. C = f = tiny; // b0 = 0, replace with tiny
  149. D = 0;
  150. for (k = 1; k < policies::get_max_series_iterations<Policy>(); k++)
  151. {
  152. a = 1;
  153. b = 2 * (v + k) / x;
  154. C = b + a / C;
  155. D = b + a * D;
  156. if (C == 0) { C = tiny; }
  157. if (D == 0) { D = tiny; }
  158. D = 1 / D;
  159. delta = C * D;
  160. f *= delta;
  161. BOOST_MATH_INSTRUMENT_VARIABLE(delta-1);
  162. if (abs(delta - 1) <= tolerance)
  163. {
  164. break;
  165. }
  166. }
  167. BOOST_MATH_INSTRUMENT_VARIABLE(k);
  168. policies::check_series_iterations<T>("boost::math::bessel_ik<%1%>(%1%,%1%) in CF1_ik", k, pol);
  169. *fv = f;
  170. return 0;
  171. }
  172. // Calculate K(v, x) and K(v+1, x) by evaluating continued fraction
  173. // z1 / z0 = U(v+1.5, 2v+1, 2x) / U(v+0.5, 2v+1, 2x), see
  174. // Thompson and Barnett, Computer Physics Communications, vol 47, 245 (1987)
  175. template <typename T, typename Policy>
  176. int CF2_ik(T v, T x, T* Kv, T* Kv1, const Policy& pol)
  177. {
  178. BOOST_MATH_STD_USING
  179. using namespace boost::math::constants;
  180. T S, C, Q, D, f, a, b, q, delta, tolerance, current, prev;
  181. unsigned long k;
  182. // |x| >= |v|, CF2_ik converges rapidly
  183. // |x| -> 0, CF2_ik fails to converge
  184. BOOST_MATH_ASSERT(abs(x) > 1);
  185. // Steed's algorithm, see Thompson and Barnett,
  186. // Journal of Computational Physics, vol 64, 490 (1986)
  187. tolerance = tools::epsilon<T>();
  188. a = v * v - 0.25f;
  189. b = 2 * (x + 1); // b1
  190. D = 1 / b; // D1 = 1 / b1
  191. f = delta = D; // f1 = delta1 = D1, coincidence
  192. prev = 0; // q0
  193. current = 1; // q1
  194. Q = C = -a; // Q1 = C1 because q1 = 1
  195. S = 1 + Q * delta; // S1
  196. BOOST_MATH_INSTRUMENT_VARIABLE(tolerance);
  197. BOOST_MATH_INSTRUMENT_VARIABLE(a);
  198. BOOST_MATH_INSTRUMENT_VARIABLE(b);
  199. BOOST_MATH_INSTRUMENT_VARIABLE(D);
  200. BOOST_MATH_INSTRUMENT_VARIABLE(f);
  201. for (k = 2; k < policies::get_max_series_iterations<Policy>(); k++) // starting from 2
  202. {
  203. // continued fraction f = z1 / z0
  204. a -= 2 * (k - 1);
  205. b += 2;
  206. D = 1 / (b + a * D);
  207. delta *= b * D - 1;
  208. f += delta;
  209. // series summation S = 1 + \sum_{n=1}^{\infty} C_n * z_n / z_0
  210. q = (prev - (b - 2) * current) / a;
  211. prev = current;
  212. current = q; // forward recurrence for q
  213. C *= -a / k;
  214. Q += C * q;
  215. S += Q * delta;
  216. //
  217. // Under some circumstances q can grow very small and C very
  218. // large, leading to under/overflow. This is particularly an
  219. // issue for types which have many digits precision but a narrow
  220. // exponent range. A typical example being a "double double" type.
  221. // To avoid this situation we can normalise q (and related prev/current)
  222. // and C. All other variables remain unchanged in value. A typical
  223. // test case occurs when x is close to 2, for example cyl_bessel_k(9.125, 2.125).
  224. //
  225. if(q < tools::epsilon<T>())
  226. {
  227. C *= q;
  228. prev /= q;
  229. current /= q;
  230. q = 1;
  231. }
  232. // S converges slower than f
  233. BOOST_MATH_INSTRUMENT_VARIABLE(Q * delta);
  234. BOOST_MATH_INSTRUMENT_VARIABLE(abs(S) * tolerance);
  235. BOOST_MATH_INSTRUMENT_VARIABLE(S);
  236. if (abs(Q * delta) < abs(S) * tolerance)
  237. {
  238. break;
  239. }
  240. }
  241. policies::check_series_iterations<T>("boost::math::bessel_ik<%1%>(%1%,%1%) in CF2_ik", k, pol);
  242. if(x >= tools::log_max_value<T>())
  243. *Kv = exp(0.5f * log(pi<T>() / (2 * x)) - x - log(S));
  244. else
  245. *Kv = sqrt(pi<T>() / (2 * x)) * exp(-x) / S;
  246. *Kv1 = *Kv * (0.5f + v + x + (v * v - 0.25f) * f) / x;
  247. BOOST_MATH_INSTRUMENT_VARIABLE(*Kv);
  248. BOOST_MATH_INSTRUMENT_VARIABLE(*Kv1);
  249. return 0;
  250. }
  251. enum{
  252. need_i = 1,
  253. need_k = 2
  254. };
  255. // Compute I(v, x) and K(v, x) simultaneously by Temme's method, see
  256. // Temme, Journal of Computational Physics, vol 19, 324 (1975)
  257. template <typename T, typename Policy>
  258. int bessel_ik(T v, T x, T* result_I, T* result_K, int kind, const Policy& pol)
  259. {
  260. // Kv1 = K_(v+1), fv = I_(v+1) / I_v
  261. // Ku1 = K_(u+1), fu = I_(u+1) / I_u
  262. T u, Iv, Kv, Kv1, Ku, Ku1, fv;
  263. T W, current, prev, next;
  264. bool reflect = false;
  265. unsigned n, k;
  266. int org_kind = kind;
  267. BOOST_MATH_INSTRUMENT_VARIABLE(v);
  268. BOOST_MATH_INSTRUMENT_VARIABLE(x);
  269. BOOST_MATH_INSTRUMENT_VARIABLE(kind);
  270. BOOST_MATH_STD_USING
  271. using namespace boost::math::tools;
  272. using namespace boost::math::constants;
  273. static const char* function = "boost::math::bessel_ik<%1%>(%1%,%1%)";
  274. if (v < 0)
  275. {
  276. reflect = true;
  277. v = -v; // v is non-negative from here
  278. kind |= need_k;
  279. }
  280. n = iround(v, pol);
  281. u = v - n; // -1/2 <= u < 1/2
  282. BOOST_MATH_INSTRUMENT_VARIABLE(n);
  283. BOOST_MATH_INSTRUMENT_VARIABLE(u);
  284. BOOST_MATH_ASSERT(x > 0); // Error handling for x <= 0 handled in cyl_bessel_i and cyl_bessel_k
  285. // x is positive until reflection
  286. W = 1 / x; // Wronskian
  287. if (x <= 2) // x in (0, 2]
  288. {
  289. temme_ik(u, x, &Ku, &Ku1, pol); // Temme series
  290. }
  291. else // x in (2, \infty)
  292. {
  293. CF2_ik(u, x, &Ku, &Ku1, pol); // continued fraction CF2_ik
  294. }
  295. BOOST_MATH_INSTRUMENT_VARIABLE(Ku);
  296. BOOST_MATH_INSTRUMENT_VARIABLE(Ku1);
  297. prev = Ku;
  298. current = Ku1;
  299. T scale = 1;
  300. T scale_sign = 1;
  301. for (k = 1; k <= n; k++) // forward recurrence for K
  302. {
  303. T fact = 2 * (u + k) / x;
  304. // Check for overflow: if (max - |prev|) / fact > max, then overflow
  305. // (max - |prev|) / fact > max
  306. // max * (1 - fact) > |prev|
  307. // if fact < 1: safe to compute overflow check
  308. // if fact >= 1: won't overflow
  309. const bool will_overflow = (fact < 1)
  310. ? tools::max_value<T>() * (1 - fact) > fabs(prev)
  311. : false;
  312. if(!will_overflow && ((tools::max_value<T>() - fabs(prev)) / fact < fabs(current)))
  313. {
  314. prev /= current;
  315. scale /= current;
  316. scale_sign *= ((boost::math::signbit)(current) ? -1 : 1);
  317. current = 1;
  318. }
  319. next = fact * current + prev;
  320. prev = current;
  321. current = next;
  322. }
  323. Kv = prev;
  324. Kv1 = current;
  325. BOOST_MATH_INSTRUMENT_VARIABLE(Kv);
  326. BOOST_MATH_INSTRUMENT_VARIABLE(Kv1);
  327. if(kind & need_i)
  328. {
  329. T lim = (4 * v * v + 10) / (8 * x);
  330. lim *= lim;
  331. lim *= lim;
  332. lim /= 24;
  333. if((lim < tools::epsilon<T>() * 10) && (x > 100))
  334. {
  335. // x is huge compared to v, CF1 may be very slow
  336. // to converge so use asymptotic expansion for large
  337. // x case instead. Note that the asymptotic expansion
  338. // isn't very accurate - so it's deliberately very hard
  339. // to get here - probably we're going to overflow:
  340. Iv = asymptotic_bessel_i_large_x(v, x, pol);
  341. }
  342. else if((v > 0) && (x / v < 0.25))
  343. {
  344. Iv = bessel_i_small_z_series(v, x, pol);
  345. }
  346. else
  347. {
  348. CF1_ik(v, x, &fv, pol); // continued fraction CF1_ik
  349. Iv = scale * W / (Kv * fv + Kv1); // Wronskian relation
  350. }
  351. }
  352. else
  353. Iv = std::numeric_limits<T>::quiet_NaN(); // any value will do
  354. if (reflect)
  355. {
  356. T z = (u + n % 2);
  357. T fact = (2 / pi<T>()) * (boost::math::sin_pi(z, pol) * Kv);
  358. if(fact == 0)
  359. *result_I = Iv;
  360. else if(tools::max_value<T>() * scale < fact)
  361. *result_I = (org_kind & need_i) ? T(sign(fact) * scale_sign * policies::raise_overflow_error<T>(function, nullptr, pol)) : T(0);
  362. else
  363. *result_I = Iv + fact / scale; // reflection formula
  364. }
  365. else
  366. {
  367. *result_I = Iv;
  368. }
  369. if(tools::max_value<T>() * scale < Kv)
  370. *result_K = (org_kind & need_k) ? T(sign(Kv) * scale_sign * policies::raise_overflow_error<T>(function, nullptr, pol)) : T(0);
  371. else
  372. *result_K = Kv / scale;
  373. BOOST_MATH_INSTRUMENT_VARIABLE(*result_I);
  374. BOOST_MATH_INSTRUMENT_VARIABLE(*result_K);
  375. return 0;
  376. }
  377. }}} // namespaces
  378. #endif // BOOST_MATH_BESSEL_IK_HPP