recurrence.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. // (C) Copyright Anton Bikineev 2014
  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_TOOLS_RECURRENCE_HPP_
  6. #define BOOST_MATH_TOOLS_RECURRENCE_HPP_
  7. #include <type_traits>
  8. #include <tuple>
  9. #include <utility>
  10. #include <boost/math/tools/config.hpp>
  11. #include <boost/math/tools/precision.hpp>
  12. #include <boost/math/tools/tuple.hpp>
  13. #include <boost/math/tools/fraction.hpp>
  14. #include <boost/math/tools/cxx03_warn.hpp>
  15. #include <boost/math/tools/assert.hpp>
  16. namespace boost {
  17. namespace math {
  18. namespace tools {
  19. namespace detail{
  20. //
  21. // Function ratios directly from recurrence relations:
  22. // H. Shintan, Note on Miller's recurrence algorithm, J. Sci. Hiroshima Univ. Ser. A-I
  23. // Math., 29 (1965), pp. 121 - 133.
  24. // and:
  25. // COMPUTATIONAL ASPECTS OF THREE-TERM RECURRENCE RELATIONS
  26. // WALTER GAUTSCHI
  27. // SIAM REVIEW Vol. 9, No. 1, January, 1967
  28. //
  29. template <class Recurrence>
  30. struct function_ratio_from_backwards_recurrence_fraction
  31. {
  32. typedef typename std::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;
  33. typedef std::pair<value_type, value_type> result_type;
  34. function_ratio_from_backwards_recurrence_fraction(const Recurrence& r) : r(r), k(0) {}
  35. result_type operator()()
  36. {
  37. value_type a, b, c;
  38. std::tie(a, b, c) = r(k);
  39. ++k;
  40. // an and bn defined as per Gauchi 1.16, not the same
  41. // as the usual continued fraction a' and b's.
  42. value_type bn = a / c;
  43. value_type an = b / c;
  44. return result_type(-bn, an);
  45. }
  46. private:
  47. function_ratio_from_backwards_recurrence_fraction operator=(const function_ratio_from_backwards_recurrence_fraction&) = delete;
  48. Recurrence r;
  49. int k;
  50. };
  51. template <class R, class T>
  52. struct recurrence_reverser
  53. {
  54. recurrence_reverser(const R& r) : r(r) {}
  55. std::tuple<T, T, T> operator()(int i)
  56. {
  57. using std::swap;
  58. std::tuple<T, T, T> t = r(-i);
  59. swap(std::get<0>(t), std::get<2>(t));
  60. return t;
  61. }
  62. R r;
  63. };
  64. template <class Recurrence>
  65. struct recurrence_offsetter
  66. {
  67. typedef decltype(std::declval<Recurrence&>()(0)) result_type;
  68. recurrence_offsetter(Recurrence const& rr, int offset) : r(rr), k(offset) {}
  69. result_type operator()(int i)
  70. {
  71. return r(i + k);
  72. }
  73. private:
  74. Recurrence r;
  75. int k;
  76. };
  77. } // namespace detail
  78. //
  79. // Given a stable backwards recurrence relation:
  80. // a f_n-1 + b f_n + c f_n+1 = 0
  81. // returns the ratio f_n / f_n-1
  82. //
  83. // Recurrence: a functor that returns a tuple of the factors (a,b,c).
  84. // factor: Convergence criteria, should be no less than machine epsilon.
  85. // max_iter: Maximum iterations to use solving the continued fraction.
  86. //
  87. template <class Recurrence, class T>
  88. T function_ratio_from_backwards_recurrence(const Recurrence& r, const T& factor, std::uintmax_t& max_iter)
  89. {
  90. detail::function_ratio_from_backwards_recurrence_fraction<Recurrence> f(r);
  91. return boost::math::tools::continued_fraction_a(f, factor, max_iter);
  92. }
  93. //
  94. // Given a stable forwards recurrence relation:
  95. // a f_n-1 + b f_n + c f_n+1 = 0
  96. // returns the ratio f_n / f_n+1
  97. //
  98. // Note that in most situations where this would be used, we're relying on
  99. // pseudo-convergence, as in most cases f_n will not be minimal as N -> -INF
  100. // as long as we reach convergence on the continued-fraction before f_n
  101. // switches behaviour, we should be fine.
  102. //
  103. // Recurrence: a functor that returns a tuple of the factors (a,b,c).
  104. // factor: Convergence criteria, should be no less than machine epsilon.
  105. // max_iter: Maximum iterations to use solving the continued fraction.
  106. //
  107. template <class Recurrence, class T>
  108. T function_ratio_from_forwards_recurrence(const Recurrence& r, const T& factor, std::uintmax_t& max_iter)
  109. {
  110. boost::math::tools::detail::function_ratio_from_backwards_recurrence_fraction<boost::math::tools::detail::recurrence_reverser<Recurrence, T> > f(r);
  111. return boost::math::tools::continued_fraction_a(f, factor, max_iter);
  112. }
  113. // solves usual recurrence relation for homogeneous
  114. // difference equation in stable forward direction
  115. // a(n)w(n-1) + b(n)w(n) + c(n)w(n+1) = 0
  116. //
  117. // Params:
  118. // get_coefs: functor returning a tuple, where
  119. // get<0>() is a(n); get<1>() is b(n); get<2>() is c(n);
  120. // last_index: index N to be found;
  121. // first: w(-1);
  122. // second: w(0);
  123. //
  124. template <class NextCoefs, class T>
  125. inline T apply_recurrence_relation_forward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, long long* log_scaling = nullptr, T* previous = nullptr)
  126. {
  127. BOOST_MATH_STD_USING
  128. using std::tuple;
  129. using std::get;
  130. using std::swap;
  131. T third;
  132. T a, b, c;
  133. for (unsigned k = 0; k < number_of_steps; ++k)
  134. {
  135. tie(a, b, c) = get_coefs(k);
  136. if ((log_scaling) &&
  137. ((fabs(tools::max_value<T>() * (c / (a * 2048))) < fabs(first))
  138. || (fabs(tools::max_value<T>() * (c / (b * 2048))) < fabs(second))
  139. || (fabs(tools::min_value<T>() * (c * 2048 / a)) > fabs(first))
  140. || (fabs(tools::min_value<T>() * (c * 2048 / b)) > fabs(second))
  141. ))
  142. {
  143. // Rescale everything:
  144. long long log_scale = lltrunc(log(fabs(second)));
  145. T scale = exp(T(-log_scale));
  146. second *= scale;
  147. first *= scale;
  148. *log_scaling += log_scale;
  149. }
  150. // scale each part separately to avoid spurious overflow:
  151. third = (a / -c) * first + (b / -c) * second;
  152. BOOST_MATH_ASSERT((boost::math::isfinite)(third));
  153. swap(first, second);
  154. swap(second, third);
  155. }
  156. if (previous)
  157. *previous = first;
  158. return second;
  159. }
  160. // solves usual recurrence relation for homogeneous
  161. // difference equation in stable backward direction
  162. // a(n)w(n-1) + b(n)w(n) + c(n)w(n+1) = 0
  163. //
  164. // Params:
  165. // get_coefs: functor returning a tuple, where
  166. // get<0>() is a(n); get<1>() is b(n); get<2>() is c(n);
  167. // number_of_steps: index N to be found;
  168. // first: w(1);
  169. // second: w(0);
  170. //
  171. template <class T, class NextCoefs>
  172. inline T apply_recurrence_relation_backward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, long long* log_scaling = nullptr, T* previous = nullptr)
  173. {
  174. BOOST_MATH_STD_USING
  175. using std::tuple;
  176. using std::get;
  177. using std::swap;
  178. T next;
  179. T a, b, c;
  180. for (unsigned k = 0; k < number_of_steps; ++k)
  181. {
  182. tie(a, b, c) = get_coefs(-static_cast<int>(k));
  183. if ((log_scaling) && (second != 0) &&
  184. ( (fabs(tools::max_value<T>() * (a / b) / 2048) < fabs(second))
  185. || (fabs(tools::max_value<T>() * (a / c) / 2048) < fabs(first))
  186. || (fabs(tools::min_value<T>() * (a / b) * 2048) > fabs(second))
  187. || (fabs(tools::min_value<T>() * (a / c) * 2048) > fabs(first))
  188. ))
  189. {
  190. // Rescale everything:
  191. int log_scale = itrunc(log(fabs(second)));
  192. T scale = exp(T(-log_scale));
  193. second *= scale;
  194. first *= scale;
  195. *log_scaling += log_scale;
  196. }
  197. // scale each part separately to avoid spurious overflow:
  198. next = (b / -a) * second + (c / -a) * first;
  199. BOOST_MATH_ASSERT((boost::math::isfinite)(next));
  200. swap(first, second);
  201. swap(second, next);
  202. }
  203. if (previous)
  204. *previous = first;
  205. return second;
  206. }
  207. template <class Recurrence>
  208. struct forward_recurrence_iterator
  209. {
  210. typedef typename std::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;
  211. forward_recurrence_iterator(const Recurrence& r, value_type f_n_minus_1, value_type f_n)
  212. : f_n_minus_1(f_n_minus_1), f_n(f_n), coef(r), k(0) {}
  213. forward_recurrence_iterator(const Recurrence& r, value_type f_n)
  214. : f_n(f_n), coef(r), k(0)
  215. {
  216. std::uintmax_t max_iter = boost::math::policies::get_max_series_iterations<boost::math::policies::policy<> >();
  217. f_n_minus_1 = f_n * boost::math::tools::function_ratio_from_forwards_recurrence(detail::recurrence_offsetter<Recurrence>(r, -1), value_type(boost::math::tools::epsilon<value_type>() * 2), max_iter);
  218. boost::math::policies::check_series_iterations<value_type>("forward_recurrence_iterator<>::forward_recurrence_iterator", max_iter, boost::math::policies::policy<>());
  219. }
  220. forward_recurrence_iterator& operator++()
  221. {
  222. using std::swap;
  223. value_type a, b, c;
  224. std::tie(a, b, c) = coef(k);
  225. value_type f_n_plus_1 = a * f_n_minus_1 / -c + b * f_n / -c;
  226. swap(f_n_minus_1, f_n);
  227. swap(f_n, f_n_plus_1);
  228. ++k;
  229. return *this;
  230. }
  231. forward_recurrence_iterator operator++(int)
  232. {
  233. forward_recurrence_iterator t(*this);
  234. ++(*this);
  235. return t;
  236. }
  237. value_type operator*() { return f_n; }
  238. value_type f_n_minus_1, f_n;
  239. Recurrence coef;
  240. int k;
  241. };
  242. template <class Recurrence>
  243. struct backward_recurrence_iterator
  244. {
  245. typedef typename std::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;
  246. backward_recurrence_iterator(const Recurrence& r, value_type f_n_plus_1, value_type f_n)
  247. : f_n_plus_1(f_n_plus_1), f_n(f_n), coef(r), k(0) {}
  248. backward_recurrence_iterator(const Recurrence& r, value_type f_n)
  249. : f_n(f_n), coef(r), k(0)
  250. {
  251. std::uintmax_t max_iter = boost::math::policies::get_max_series_iterations<boost::math::policies::policy<> >();
  252. f_n_plus_1 = f_n * boost::math::tools::function_ratio_from_backwards_recurrence(detail::recurrence_offsetter<Recurrence>(r, 1), value_type(boost::math::tools::epsilon<value_type>() * 2), max_iter);
  253. boost::math::policies::check_series_iterations<value_type>("backward_recurrence_iterator<>::backward_recurrence_iterator", max_iter, boost::math::policies::policy<>());
  254. }
  255. backward_recurrence_iterator& operator++()
  256. {
  257. using std::swap;
  258. value_type a, b, c;
  259. std::tie(a, b, c) = coef(k);
  260. value_type f_n_minus_1 = c * f_n_plus_1 / -a + b * f_n / -a;
  261. swap(f_n_plus_1, f_n);
  262. swap(f_n, f_n_minus_1);
  263. --k;
  264. return *this;
  265. }
  266. backward_recurrence_iterator operator++(int)
  267. {
  268. backward_recurrence_iterator t(*this);
  269. ++(*this);
  270. return t;
  271. }
  272. value_type operator*() { return f_n; }
  273. value_type f_n_plus_1, f_n;
  274. Recurrence coef;
  275. int k;
  276. };
  277. }
  278. }
  279. } // namespaces
  280. #endif // BOOST_MATH_TOOLS_RECURRENCE_HPP_