recurrence.qbk 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. [section:recurrence Tools For 3-Term Recurrence Relations]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/tools/recurrence.hpp>
  5. ``
  6. namespace boost{ namespace math{ namespace tools{
  7. template <class Recurrence, class T>
  8. T function_ratio_from_backwards_recurrence(const Recurrence& r, const T& factor, boost::uintmax_t& max_iter);
  9. template <class Recurrence, class T>
  10. T function_ratio_from_forwards_recurrence(const Recurrence& r, const T& factor, boost::uintmax_t& max_iter);
  11. template <class NextCoefs, class T>
  12. T apply_recurrence_relation_forward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, int* log_scaling = 0, T* previous = 0);
  13. template <class T, class NextCoefs>
  14. T apply_recurrence_relation_backward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, int* log_scaling = 0, T* previous = 0);
  15. template <class Recurrence>
  16. struct forward_recurrence_iterator;
  17. template <class Recurrence>
  18. struct backward_recurrence_iterator;
  19. }}} // namespaces
  20. [h4 Description]
  21. All of the tools in this header require a description of the recurrence relation: this takes the form of
  22. a functor that returns a tuple containing the 3 coefficients, specifically, given a recurrence relation:
  23. [/\Large $$ a_nF_{n-1} + b_nF_n + c_nF_{n+1} = 0 $$]
  24. [equation three_term_recurrence]
  25. And a functor `F` then the expression:
  26. [expression F(n);]
  27. Returns a tuple containing [role serif_italic { a[sub n], b[sub n], c[sub n] }].
  28. For example, the recurrence relation for the Bessel J and Y functions when written in this form is:
  29. [/\Large $$ J_{v-1}(x) - \frac{2v}{x}J_v(x) + J_{v+1}(x)= 0 $$]
  30. [$../equations/three_term_recurrence_bessel_jy.svg]
  31. Therefore, given local variables /x/ and /v/ of type `double` the recurrence relation for Bessel J and Y can be encoded
  32. in a lambda expression like this:
  33. auto recurrence_functor_jy = [&](int n) { return std::make_tuple(1.0, -2 * (v + n) / x, 1.0); };
  34. Similarly, the Bessel I and K recurrence relation differs just by the sign of the final term:
  35. [/\Large $$ I_{v-1}(x) - \frac{2v}{x}I_v(x) - I_{v+1}(x)= 0 $$]
  36. [$../equations/three_term_recurrence_bessel_ik.svg]
  37. And this could be encoded as:
  38. auto recurrence_functor_ik = [&](int n) { return std::make_tuple(1.0, -2 * (v + n) / x, -1.0); };
  39. The tools are then as follows:
  40. template <class Recurrence, class T>
  41. T function_ratio_from_backwards_recurrence(const Recurrence& r, const T& factor, boost::uintmax_t& max_iter);
  42. Given a functor `r` which encodes the recurrence relation for function `F` at some location /n/, then returns the ratio:
  43. [/\Large $$ F_n / F_{n-1} $$]
  44. [$../equations/three_term_recurrence_backwards_ratio.svg]
  45. This calculation is stable only if recurrence is stable in the backwards direction. Further the ratio calculated
  46. is for the dominant solution (in the backwards direction) of the recurrence relation, if there are multiple solutions,
  47. then there is no guarantee that this will find the one you want or expect.
  48. Argument /factor/ is the tolerance required for convergence of the continued fraction associated with
  49. the recurrence relation, and should be no smaller than machine epsilon. Argument /max_iter/ sets
  50. the maximum number of permitted iterations in the associated continued fraction.
  51. template <class Recurrence, class T>
  52. T function_ratio_from_forwards_recurrence(const Recurrence& r, const T& factor, boost::uintmax_t& max_iter);
  53. Given a functor `r` which encodes the recurrence relation for function F at some location /n/, then returns the ratio:
  54. [/\Large $$ F_n / F_{n+1} $$]
  55. [$../equations/three_term_recurrence_forwards_ratio.svg]
  56. This calculation is stable only if recurrence is stable in the forwards direction. Further the ratio calculated
  57. is for the dominant solution (in the forwards direction) of the recurrence relation, if there are multiple solutions,
  58. then there is no guarantee that this will find the one you want or expect.
  59. Argument /factor/ is the tolerance required for convergence of the continued fraction associated with
  60. the recurrence relation, and should be no smaller than machine epsilon. Argument /max_iter/ sets
  61. the maximum number of permitted iterations in the associated continued fraction.
  62. template <class NextCoefs, class T>
  63. T apply_recurrence_relation_forward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, int* log_scaling = 0, T* previous = 0);
  64. Applies a recurrence relation in a stable forward direction, starting with the values F[sub n-1] and F[sub n].
  65. [variablelist
  66. [[get_coefs] [Functor that returns the corefficients of the recurrence relation. The coefficients should be centered on position /second/.]]
  67. [[number_of_steps][The number of steps to apply the recurrence relation onwards from /second/.]]
  68. [[first] [The value of F[sub n-1]]]
  69. [[second] [The value of F[sub n]]]
  70. [[log_scaling][When provided, the recurrence relations may be rescaled internally to avoid over/underflow issues. The result should be multiplied by `exp(*log_scaling)` to get the true value of the result.]]
  71. [[previous][When provided, is set to the value of F[sub n + number_of_steps - 1]]
  72. ]
  73. ]
  74. Returns F[sub n + number_of_steps].
  75. template <class NextCoefs, class T>
  76. T apply_recurrence_relation_backward(const NextCoefs& get_coefs, unsigned number_of_steps, T first, T second, int* log_scaling = 0, T* previous = 0);
  77. Applies a recurrence relation in a stable backward direction, starting with the values F[sub n+1] and F[sub n].
  78. [variablelist
  79. [[get_coefs] [Functor that returns the corefficients of the recurrence relation. The coefficients should be centered on position /second/.]]
  80. [[number_of_steps][The number of steps to apply the recurrence relation backwards from /second/.]]
  81. [[first] [The value of F[sub n+1]]]
  82. [[second] [The value of F[sub n]]]
  83. [[log_scaling][When provided, the recurrence relations may be rescaled internally to avoid over/underflow issues. The result should be multiplied by `exp(*log_scaling)` to get the true value of the result.]]
  84. [[previous][When provided, is set to the value of F[sub n - number_of_steps + 1]]
  85. ]
  86. ]
  87. Returns F[sub n - number_of_steps].
  88. template <class Recurrence>
  89. struct forward_recurrence_iterator
  90. {
  91. typedef typename boost::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;
  92. forward_recurrence_iterator(const Recurrence& r, value_type f_n_minus_1, value_type f_n);
  93. forward_recurrence_iterator(const Recurrence& r, value_type f_n);
  94. /* Operators omitted for clarity */
  95. };
  96. Type `forward_recurrence_iterator` defines a forward-iterator for a recurrence relation stable in the
  97. forward direction. The constructors take the recurrence relation, plus either one or two values: if
  98. only one value is provided, then the second is computed by using the recurrence relation to calculate the function ratio.
  99. template <class Recurrence>
  100. struct backward_recurrence_iterator
  101. {
  102. typedef typename boost::remove_reference<decltype(std::get<0>(std::declval<Recurrence&>()(0)))>::type value_type;
  103. backward_recurrence_iterator(const Recurrence& r, value_type f_n_plus_1, value_type f_n);
  104. backward_recurrence_iterator(const Recurrence& r, value_type f_n);
  105. /* Operators omitted for clarity */
  106. };
  107. Type `backward_recurrence_iterator` defines a forward-iterator for a recurrence relation stable in the
  108. backward direction. The constructors take the recurrence relation, plus either one or two values: if
  109. only one value is provided, then the second is computed by using the recurrence relation to calculate the function ratio.
  110. Note that /incrementing/ this iterator moves the value returned successively to F[sub n-1], F[sub n-2] etc.
  111. [endsect] [/section:recurrence Tools For 3-Term Recurrence Relations]
  112. [/
  113. Copyright 2019 John Maddock.
  114. Distributed under the Boost Software License, Version 1.0.
  115. (See accompanying file LICENSE_1_0.txt or copy at
  116. http://www.boost.org/LICENSE_1_0.txt).
  117. ]