hypergeometric_1F1_recurrence.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // Copyright 2014 Anton Bikineev
  3. // Copyright 2014 Christopher Kormanyos
  4. // Copyright 2014 John Maddock
  5. // Copyright 2014 Paul Bristow
  6. // Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. #ifndef BOOST_HYPERGEOMETRIC_1F1_RECURRENCE_HPP_
  10. #define BOOST_HYPERGEOMETRIC_1F1_RECURRENCE_HPP_
  11. #include <boost/math/special_functions/modf.hpp>
  12. #include <boost/math/special_functions/next.hpp>
  13. #include <boost/math/tools/recurrence.hpp>
  14. #include <boost/math/special_functions/detail/hypergeometric_pFq_checked_series.hpp>
  15. namespace boost { namespace math { namespace detail {
  16. // forward declaration for initial values
  17. template <class T, class Policy>
  18. inline T hypergeometric_1F1_imp(const T& a, const T& b, const T& z, const Policy& pol);
  19. template <class T, class Policy>
  20. inline T hypergeometric_1F1_imp(const T& a, const T& b, const T& z, const Policy& pol, int& log_scaling);
  21. template <class T>
  22. struct hypergeometric_1F1_recurrence_a_coefficients
  23. {
  24. typedef boost::math::tuple<T, T, T> result_type;
  25. hypergeometric_1F1_recurrence_a_coefficients(const T& a, const T& b, const T& z):
  26. a(a), b(b), z(z)
  27. {
  28. }
  29. result_type operator()(boost::intmax_t i) const
  30. {
  31. const T ai = a + i;
  32. const T an = b - ai;
  33. const T bn = (2 * ai - b + z);
  34. const T cn = -ai;
  35. return boost::math::make_tuple(an, bn, cn);
  36. }
  37. private:
  38. const T a, b, z;
  39. hypergeometric_1F1_recurrence_a_coefficients operator=(const hypergeometric_1F1_recurrence_a_coefficients&);
  40. };
  41. template <class T>
  42. struct hypergeometric_1F1_recurrence_b_coefficients
  43. {
  44. typedef boost::math::tuple<T, T, T> result_type;
  45. hypergeometric_1F1_recurrence_b_coefficients(const T& a, const T& b, const T& z):
  46. a(a), b(b), z(z)
  47. {
  48. }
  49. result_type operator()(boost::intmax_t i) const
  50. {
  51. const T bi = b + i;
  52. const T an = bi * (bi - 1);
  53. const T bn = bi * (1 - bi - z);
  54. const T cn = z * (bi - a);
  55. return boost::math::make_tuple(an, bn, cn);
  56. }
  57. private:
  58. const T a, b, z;
  59. hypergeometric_1F1_recurrence_b_coefficients& operator=(const hypergeometric_1F1_recurrence_b_coefficients&);
  60. };
  61. //
  62. // for use when we're recursing to a small b:
  63. //
  64. template <class T>
  65. struct hypergeometric_1F1_recurrence_small_b_coefficients
  66. {
  67. typedef boost::math::tuple<T, T, T> result_type;
  68. hypergeometric_1F1_recurrence_small_b_coefficients(const T& a, const T& b, const T& z, int N) :
  69. a(a), b(b), z(z), N(N)
  70. {
  71. }
  72. result_type operator()(boost::intmax_t i) const
  73. {
  74. const T bi = b + (i + N);
  75. const T bi_minus_1 = b + (i + N - 1);
  76. const T an = bi * bi_minus_1;
  77. const T bn = bi * (-bi_minus_1 - z);
  78. const T cn = z * (bi - a);
  79. return boost::math::make_tuple(an, bn, cn);
  80. }
  81. private:
  82. hypergeometric_1F1_recurrence_small_b_coefficients operator=(const hypergeometric_1F1_recurrence_small_b_coefficients&);
  83. const T a, b, z;
  84. int N;
  85. };
  86. template <class T>
  87. struct hypergeometric_1F1_recurrence_a_and_b_coefficients
  88. {
  89. typedef boost::math::tuple<T, T, T> result_type;
  90. hypergeometric_1F1_recurrence_a_and_b_coefficients(const T& a, const T& b, const T& z, int offset = 0):
  91. a(a), b(b), z(z), offset(offset)
  92. {
  93. }
  94. result_type operator()(boost::intmax_t i) const
  95. {
  96. const T ai = a + (offset + i);
  97. const T bi = b + (offset + i);
  98. const T an = bi * (b + (offset + i - 1));
  99. const T bn = bi * (z - (b + (offset + i - 1)));
  100. const T cn = -ai * z;
  101. return boost::math::make_tuple(an, bn, cn);
  102. }
  103. private:
  104. const T a, b, z;
  105. int offset;
  106. hypergeometric_1F1_recurrence_a_and_b_coefficients operator=(const hypergeometric_1F1_recurrence_a_and_b_coefficients&);
  107. };
  108. #if 0
  109. //
  110. // These next few recurrence relations are archived for future refernece, some of them are novel, though all
  111. // are trivially derived from the existing well known relations:
  112. //
  113. // Recurrence relation for double-stepping on both a and b:
  114. // - b(b-1)(b-2) / (2-b+z) M(a-2,b-2,z) + [b(a-1)z / (2-b+z) + b(1-b+z) + abz(b+1) /(b+1)(z-b)] M(a,b,z) - a(a+1)z^2 / (b+1)(z-b) M(a+2,b+2,z)
  115. //
  116. template <class T>
  117. struct hypergeometric_1F1_recurrence_2a_and_2b_coefficients
  118. {
  119. typedef boost::math::tuple<T, T, T> result_type;
  120. hypergeometric_1F1_recurrence_2a_and_2b_coefficients(const T& a, const T& b, const T& z, int offset = 0) :
  121. a(a), b(b), z(z), offset(offset)
  122. {
  123. }
  124. result_type operator()(boost::intmax_t i) const
  125. {
  126. i *= 2;
  127. const T ai = a + (offset + i);
  128. const T bi = b + (offset + i);
  129. const T an = -bi * (b + (offset + i - 1)) * (b + (offset + i - 2)) / (-(b + (offset + i - 2)) + z);
  130. const T bn = bi * (a + (offset + i - 1)) * z / (z - (b + (offset + i - 2)))
  131. + bi * (z - (b + (offset + i - 1)))
  132. + ai * bi * z * (b + (offset + i + 1)) / ((b + (offset + i + 1)) * (z - bi));
  133. const T cn = -ai * (a + (offset + i + 1)) * z * z / ((b + (offset + i + 1)) * (z - bi));
  134. return boost::math::make_tuple(an, bn, cn);
  135. }
  136. private:
  137. const T a, b, z;
  138. int offset;
  139. hypergeometric_1F1_recurrence_2a_and_2b_coefficients operator=(const hypergeometric_1F1_recurrence_2a_and_2b_coefficients&);
  140. };
  141. //
  142. // Recurrence relation for double-stepping on a:
  143. // -(b-a)(1 + b - a)/(2a-2-b+z)M(a-2,b,z) + [(b-a)(a-1)/(2a-2-b+z) + (2a-b+z) + a(b-a-1)/(2a+2-b+z)]M(a,b,z) -a(a+1)/(2a+2-b+z)M(a+2,b,z)
  144. //
  145. template <class T>
  146. struct hypergeometric_1F1_recurrence_2a_coefficients
  147. {
  148. typedef boost::math::tuple<T, T, T> result_type;
  149. hypergeometric_1F1_recurrence_2a_coefficients(const T& a, const T& b, const T& z, int offset = 0) :
  150. a(a), b(b), z(z), offset(offset)
  151. {
  152. }
  153. result_type operator()(boost::intmax_t i) const
  154. {
  155. i *= 2;
  156. const T ai = a + (offset + i);
  157. // -(b-a)(1 + b - a)/(2a-2-b+z)
  158. const T an = -(b - ai) * (b - (a + (offset + i - 1))) / (2 * (a + (offset + i - 1)) - b + z);
  159. const T bn = (b - ai) * (a + (offset + i - 1)) / (2 * (a + (offset + i - 1)) - b + z) + (2 * ai - b + z) + ai * (b - (a + (offset + i + 1))) / (2 * (a + (offset + i + 1)) - b + z);
  160. const T cn = -ai * (a + (offset + i + 1)) / (2 * (a + (offset + i + 1)) - b + z);
  161. return boost::math::make_tuple(an, bn, cn);
  162. }
  163. private:
  164. const T a, b, z;
  165. int offset;
  166. hypergeometric_1F1_recurrence_2a_coefficients operator=(const hypergeometric_1F1_recurrence_2a_coefficients&);
  167. };
  168. //
  169. // Recurrence relation for double-stepping on b:
  170. // b(b-1)^2(b-2)/((1-b)(2-b-z)) M(a,b-2,z) + [zb(b-1)(b-1-a)/((1-b)(2-b-z)) + b(1-b-z) + z(b-a)(b+1)b/((b+1)(b+z)) ] M(a,b,z) + z^2(b-a)(b+1-a)/((b+1)(b+z)) M(a,b+2,z)
  171. //
  172. template <class T>
  173. struct hypergeometric_1F1_recurrence_2b_coefficients
  174. {
  175. typedef boost::math::tuple<T, T, T> result_type;
  176. hypergeometric_1F1_recurrence_2b_coefficients(const T& a, const T& b, const T& z, int offset = 0) :
  177. a(a), b(b), z(z), offset(offset)
  178. {
  179. }
  180. result_type operator()(boost::intmax_t i) const
  181. {
  182. i *= 2;
  183. const T bi = b + (offset + i);
  184. const T bi_m1 = b + (offset + i - 1);
  185. const T bi_p1 = b + (offset + i + 1);
  186. const T bi_m2 = b + (offset + i - 2);
  187. const T an = bi * (bi_m1) * (bi_m1) * (bi_m2) / (-bi_m1 * (-bi_m2 - z));
  188. const T bn = z * bi * bi_m1 * (bi_m1 - a) / (-bi_m1 * (-bi_m2 - z)) + bi * (-bi_m1 - z) + z * (bi - a) * bi_p1 * bi / (bi_p1 * (bi + z));
  189. const T cn = z * z * (bi - a) * (bi_p1 - a) / (bi_p1 * (bi + z));
  190. return boost::math::make_tuple(an, bn, cn);
  191. }
  192. private:
  193. const T a, b, z;
  194. int offset;
  195. hypergeometric_1F1_recurrence_2b_coefficients operator=(const hypergeometric_1F1_recurrence_2b_coefficients&);
  196. };
  197. //
  198. // Recurrence relation for a+ b-:
  199. // -z(b-a)(a-1-b)/(b(a-1+z)) M(a-1,b+1,z) + [(b-a)(a-1)b/(b(a-1+z)) + (2a-b+z) + a(b-a-1)/(a+z)] M(a,b,z) + a(1-b)/(a+z) M(a+1,b-1,z)
  200. //
  201. // This is potentially the most useful of these novel recurrences.
  202. // - - + - +
  203. template <class T>
  204. struct hypergeometric_1F1_recurrence_a_plus_b_minus_coefficients
  205. {
  206. typedef boost::math::tuple<T, T, T> result_type;
  207. hypergeometric_1F1_recurrence_a_plus_b_minus_coefficients(const T& a, const T& b, const T& z, int offset = 0) :
  208. a(a), b(b), z(z), offset(offset)
  209. {
  210. }
  211. result_type operator()(boost::intmax_t i) const
  212. {
  213. const T ai = a + (offset + i);
  214. const T bi = b - (offset + i);
  215. const T an = -z * (bi - ai) * (ai - 1 - bi) / (bi * (ai - 1 + z));
  216. const T bn = z * ((-1 / (ai + z) - 1 / (ai + z - 1)) * (bi + z - 1) + 3) + bi - 1;
  217. const T cn = ai * (1 - bi) / (ai + z);
  218. return boost::math::make_tuple(an, bn, cn);
  219. }
  220. private:
  221. const T a, b, z;
  222. int offset;
  223. hypergeometric_1F1_recurrence_a_plus_b_minus_coefficients operator=(const hypergeometric_1F1_recurrence_a_plus_b_minus_coefficients&);
  224. };
  225. #endif
  226. template <class T, class Policy>
  227. inline T hypergeometric_1F1_backward_recurrence_for_negative_a(const T& a, const T& b, const T& z, const Policy& pol, const char* function, int& log_scaling)
  228. {
  229. BOOST_MATH_STD_USING // modf, frexp, fabs, pow
  230. boost::intmax_t integer_part = 0;
  231. T ak = modf(a, &integer_part);
  232. //
  233. // We need ak-1 positive to avoid infinite recursion below:
  234. //
  235. if (0 != ak)
  236. {
  237. ak += 2;
  238. integer_part -= 2;
  239. }
  240. if (-integer_part > static_cast<boost::intmax_t>(policies::get_max_series_iterations<Policy>()))
  241. return policies::raise_evaluation_error<T>(function, "1F1 arguments sit in a range with a so negative that we have no evaluation method, got a = %1%", std::numeric_limits<T>::quiet_NaN(), pol);
  242. T first, second;
  243. if(ak == 0)
  244. {
  245. first = 1;
  246. ak -= 1;
  247. second = 1 - z / b;
  248. }
  249. else
  250. {
  251. int scaling1(0), scaling2(0);
  252. first = detail::hypergeometric_1F1_imp(ak, b, z, pol, scaling1);
  253. ak -= 1;
  254. second = detail::hypergeometric_1F1_imp(ak, b, z, pol, scaling2);
  255. if (scaling1 != scaling2)
  256. {
  257. second *= exp(T(scaling2 - scaling1));
  258. }
  259. log_scaling += scaling1;
  260. }
  261. ++integer_part;
  262. detail::hypergeometric_1F1_recurrence_a_coefficients<T> s(ak, b, z);
  263. return tools::apply_recurrence_relation_backward(s,
  264. static_cast<unsigned int>(std::abs(integer_part)),
  265. first,
  266. second, &log_scaling);
  267. }
  268. template <class T, class Policy>
  269. T hypergeometric_1F1_backwards_recursion_on_b_for_negative_a(const T& a, const T& b, const T& z, const Policy& pol, const char*, int& log_scaling)
  270. {
  271. using std::swap;
  272. BOOST_MATH_STD_USING // modf, frexp, fabs, pow
  273. //
  274. // We compute
  275. //
  276. // M[a + a_shift, b + b_shift; z]
  277. //
  278. // and recurse backwards on a and b down to
  279. //
  280. // M[a, b, z]
  281. //
  282. // With a + a_shift > 1 and b + b_shift > z
  283. //
  284. // There are 3 distinct regions to ensure stability during the recursions:
  285. //
  286. // a > 0 : stable for backwards on a
  287. // a < 0, b > 0 : stable for backwards on a and b
  288. // a < 0, b < 0 : stable for backwards on b (as long as |b| is small).
  289. //
  290. // We could simplify things by ignoring the middle region, but it's more efficient
  291. // to recurse on a and b together when we can.
  292. //
  293. BOOST_ASSERT(a < -1); // Not tested nor taken for -1 < a < 0
  294. int b_shift = itrunc(z - b) + 2;
  295. int a_shift = itrunc(-a);
  296. if (a + a_shift != 0)
  297. {
  298. a_shift += 2;
  299. }
  300. //
  301. // If the shifts are so large that we would throw an evaluation_error, try the series instead,
  302. // even though this will almost certainly throw as well:
  303. //
  304. if (b_shift > static_cast<boost::intmax_t>(boost::math::policies::get_max_series_iterations<Policy>()))
  305. return hypergeometric_1F1_checked_series_impl(a, b, z, pol, log_scaling);
  306. if (a_shift > static_cast<boost::intmax_t>(boost::math::policies::get_max_series_iterations<Policy>()))
  307. return hypergeometric_1F1_checked_series_impl(a, b, z, pol, log_scaling);
  308. int a_b_shift = b < 0 ? itrunc(b + b_shift) : b_shift; // The max we can shift on a and b together
  309. int leading_a_shift = (std::min)(3, a_shift); // Just enough to make a negative
  310. if (a_b_shift > a_shift - 3)
  311. {
  312. a_b_shift = a_shift < 3 ? 0 : a_shift - 3;
  313. }
  314. else
  315. {
  316. // Need to ensure that leading_a_shift is large enough that a will reach it's target
  317. // after the first 2 phases (-,0) and (-,-) are over:
  318. leading_a_shift = a_shift - a_b_shift;
  319. }
  320. int trailing_b_shift = b_shift - a_b_shift;
  321. if (a_b_shift < 5)
  322. {
  323. // Might as well do things in two steps rather than 3:
  324. if (a_b_shift > 0)
  325. {
  326. leading_a_shift += a_b_shift;
  327. trailing_b_shift += a_b_shift;
  328. }
  329. a_b_shift = 0;
  330. --leading_a_shift;
  331. }
  332. BOOST_ASSERT(leading_a_shift > 1);
  333. BOOST_ASSERT(a_b_shift + leading_a_shift + (a_b_shift == 0 ? 1 : 0) == a_shift);
  334. BOOST_ASSERT(a_b_shift + trailing_b_shift == b_shift);
  335. if ((trailing_b_shift == 0) && (fabs(b) < 0.5) && a_b_shift)
  336. {
  337. // Better to have the final recursion on b alone, otherwise we lose precision when b is very small:
  338. int diff = (std::min)(a_b_shift, 3);
  339. a_b_shift -= diff;
  340. leading_a_shift += diff;
  341. trailing_b_shift += diff;
  342. }
  343. T first, second;
  344. int scale1(0), scale2(0);
  345. first = boost::math::detail::hypergeometric_1F1_imp(T(a + a_shift), T(b + b_shift), z, pol, scale1);
  346. //
  347. // It would be good to compute "second" from first and the ratio - unfortunately we are right on the cusp
  348. // recursion on a switching from stable backwards to stable forwards behaviour and so this is not possible here.
  349. //
  350. second = boost::math::detail::hypergeometric_1F1_imp(T(a + a_shift - 1), T(b + b_shift), z, pol, scale2);
  351. if (scale1 != scale2)
  352. second *= exp(T(scale2 - scale1));
  353. log_scaling += scale1;
  354. //
  355. // Now we have [a + a_shift, b + b_shift, z] and [a + a_shift - 1, b + b_shift, z]
  356. // and want to recurse until [a + a_shift - leading_a_shift, b + b_shift, z] and [a + a_shift - leadng_a_shift - 1, b + b_shift, z]
  357. // which is leading_a_shift -1 steps.
  358. //
  359. second = boost::math::tools::apply_recurrence_relation_backward(
  360. hypergeometric_1F1_recurrence_a_coefficients<T>(a + a_shift - 1, b + b_shift, z),
  361. leading_a_shift, first, second, &log_scaling, &first);
  362. if (a_b_shift)
  363. {
  364. //
  365. // Now we need to switch to an a+b shift so that we have:
  366. // [a + a_shift - leading_a_shift, b + b_shift, z] and [a + a_shift - leadng_a_shift - 1, b + b_shift - 1, z]
  367. // A&S 13.4.3 gives us what we need:
  368. //
  369. {
  370. // local a's and b's:
  371. T la = a + a_shift - leading_a_shift - 1;
  372. T lb = b + b_shift;
  373. second = ((1 + la - lb) * second - la * first) / (1 - lb);
  374. }
  375. //
  376. // Now apply a_b_shift - 1 recursions to get down to
  377. // [a + 1, b + trailing_b_shift + 1, z] and [a, b + trailing_b_shift, z]
  378. //
  379. second = boost::math::tools::apply_recurrence_relation_backward(
  380. hypergeometric_1F1_recurrence_a_and_b_coefficients<T>(a, b + b_shift - a_b_shift, z, a_b_shift - 1),
  381. a_b_shift - 1, first, second, &log_scaling, &first);
  382. //
  383. // Now we need to switch to a b shift, a different application of A&S 13.4.3
  384. // will get us there, we leave "second" where it is, and move "first" sideways:
  385. //
  386. {
  387. T lb = b + trailing_b_shift + 1;
  388. first = (second * (lb - 1) - a * first) / -(1 + a - lb);
  389. }
  390. }
  391. else
  392. {
  393. //
  394. // We have M[a+1, b+b_shift, z] and M[a, b+b_shift, z] and need M[a, b+b_shift-1, z] for
  395. // recursion on b: A&S 13.4.3 gives us what we need.
  396. //
  397. T third = -(second * (1 + a - b - b_shift) - first * a) / (b + b_shift - 1);
  398. swap(first, second);
  399. swap(second, third);
  400. --trailing_b_shift;
  401. }
  402. //
  403. // Finish off by applying trailing_b_shift recursions:
  404. //
  405. if (trailing_b_shift)
  406. {
  407. second = boost::math::tools::apply_recurrence_relation_backward(
  408. hypergeometric_1F1_recurrence_small_b_coefficients<T>(a, b, z, trailing_b_shift),
  409. trailing_b_shift, first, second, &log_scaling);
  410. }
  411. return second;
  412. }
  413. } } } // namespaces
  414. #endif // BOOST_HYPERGEOMETRIC_1F1_RECURRENCE_HPP_