fraction.qbk 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. [section:cf Continued Fraction Evaluation]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/tools/fraction.hpp>
  5. ``
  6. namespace boost{ namespace math{ namespace tools{
  7. template <class Gen, class U>
  8. typename detail::fraction_traits<Gen>::result_type
  9. continued_fraction_b(Gen& g, const U& tolerance, boost::uintmax_t& max_terms)
  10. template <class Gen, class U>
  11. typename detail::fraction_traits<Gen>::result_type
  12. continued_fraction_b(Gen& g, const U& tolerance)
  13. template <class Gen, class U>
  14. typename detail::fraction_traits<Gen>::result_type
  15. continued_fraction_a(Gen& g, const U& tolerance, boost::uintmax_t& max_terms)
  16. template <class Gen, class U>
  17. typename detail::fraction_traits<Gen>::result_type
  18. continued_fraction_a(Gen& g, const U& tolerance)
  19. //
  20. // These interfaces are present for legacy reasons, and are now deprecated:
  21. //
  22. template <class Gen>
  23. typename detail::fraction_traits<Gen>::result_type
  24. continued_fraction_b(Gen& g, int bits);
  25. template <class Gen>
  26. typename detail::fraction_traits<Gen>::result_type
  27. continued_fraction_b(Gen& g, int bits, boost::uintmax_t& max_terms);
  28. template <class Gen>
  29. typename detail::fraction_traits<Gen>::result_type
  30. continued_fraction_a(Gen& g, int bits);
  31. template <class Gen>
  32. typename detail::fraction_traits<Gen>::result_type
  33. continued_fraction_a(Gen& g, int bits, boost::uintmax_t& max_terms);
  34. }}} // namespaces
  35. [h4 Description]
  36. [@http://en.wikipedia.org/wiki/Continued_fraction Continued fractions are a common method of approximation. ]
  37. These functions all evaluate the continued fraction described by the /generator/
  38. type argument. The functions with an "_a" suffix evaluate the fraction:
  39. [equation fraction2]
  40. and those with a "_b" suffix evaluate the fraction:
  41. [equation fraction1]
  42. This latter form is somewhat more natural in that it corresponds with the usual
  43. definition of a continued fraction, but note that the first /a/ value returned by
  44. the generator is discarded. Further, often the first /a/ and /b/ values in a
  45. continued fraction have different defining equations to the remaining terms, which
  46. may make the "_a" suffixed form more appropriate.
  47. The generator type should be a function object which supports the following
  48. operations:
  49. [table
  50. [[Expression] [Description]]
  51. [[Gen::result_type] [The type that is the result of invoking operator().
  52. This can be either an arithmetic or complex type, or a std::pair<> of arithmetic or complex types.]]
  53. [[g()] [Returns an object of type Gen::result_type.
  54. Each time this operator is called then the next pair of /a/ and /b/
  55. values is returned. Or, if result_type is an arithmetic type,
  56. then the next /b/ value is returned and all the /a/ values
  57. are assumed to 1.]]
  58. ]
  59. In all the continued fraction evaluation functions the /tolerance/ parameter is the
  60. precision desired in the result, evaluation of the fraction will
  61. continue until the last term evaluated leaves the relative error in the result
  62. less than /tolerance/. The deprecated interfaces take a number of digits precision
  63. here, internally they just convert this to a tolerance and forward call.
  64. If the optional /max_terms/ parameter is specified then no more than /max_terms/
  65. calls to the generator will be made, and on output,
  66. /max_terms/ will be set to actual number of
  67. calls made. This facility is particularly useful when profiling a continued
  68. fraction for convergence.
  69. [h4 Implementation]
  70. Internally these algorithms all use the modified Lentz algorithm: refer to
  71. Numeric Recipes in C++, W. H. Press et all, chapter 5,
  72. (especially 5.2 Evaluation of continued fractions, p 175 - 179)
  73. for more information, also
  74. Lentz, W.J. 1976, Applied Optics, vol. 15, pp. 668-671.
  75. [h4 Examples]
  76. [import ../../example/continued_fractions.cpp]
  77. All of these examples are in [@../../example/continued_fractions.cpp continued_fractions.cpp].
  78. The [@http://en.wikipedia.org/wiki/Golden_ratio golden ratio phi = 1.618033989...]
  79. can be computed from the simplest continued fraction of all:
  80. [equation fraction3]
  81. We begin by defining a generator function:
  82. [golden_ratio_1]
  83. The golden ratio can then be computed to double precision using:
  84. [cf_gr]
  85. It's more usual though to have to define both the /a/'s and the /b/'s
  86. when evaluating special functions by continued fractions, for example
  87. the tan function is defined by:
  88. [equation fraction4]
  89. So its generator object would look like:
  90. [cf_tan_fraction]
  91. Notice that if the continuant is subtracted from the /b/ terms,
  92. as is the case here, then all the /a/ terms returned by the generator
  93. will be negative. The tangent function can now be evaluated using:
  94. [cf_tan]
  95. Notice that this time we're using the "_b" suffixed version to evaluate
  96. the fraction: we're removing the leading /a/ term during fraction evaluation
  97. as it's different from all the others.
  98. Now we'll look at a couple of complex number examples, starting with the exponential
  99. integral which can be calculated via:
  100. [equation expint_n_3]
  101. So our functor looks like this:
  102. [cf_expint_fraction]
  103. We can finish the example by wrapping everything up in a function:
  104. [cf_expint]
  105. Notice how the termination condition is still expressed as a complex number, albeit one with zero imaginary part.
  106. Our final example will use [^continued_fraction_a], in fact there is only one special function in our code
  107. which uses that variant, and it's the upper incomplete gamma function (Q), which can be calculated via:
  108. [equation igamma9]
  109. In this case the first couple of terms are different from the rest, so our fraction will start with the first
  110. "regular" a term:
  111. [cf_upper_gamma_fraction]
  112. So now we can implement Q, this time using [^continued_fraction_a]:
  113. [cf_gamma_Q]
  114. [endsect] [/section:cf Continued Fraction Evaluation]
  115. [/
  116. Copyright 2006 John Maddock and Paul A. Bristow.
  117. Distributed under the Boost Software License, Version 1.0.
  118. (See accompanying file LICENSE_1_0.txt or copy at
  119. http://www.boost.org/LICENSE_1_0.txt).
  120. ]