series.qbk 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. [section:series_evaluation Series Evaluation]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/tools/series.hpp>
  5. ``
  6. namespace boost{ namespace math{ namespace tools{
  7. template <class Functor, class U, class V>
  8. inline typename Functor::result_type sum_series(Functor& func, const U& tolerance, boost::uintmax_t& max_terms, const V& init_value);
  9. template <class Functor, class U, class V>
  10. inline typename Functor::result_type sum_series(Functor& func, const U& tolerance, boost::uintmax_t& max_terms);
  11. //
  12. // The following interfaces are now deprecated:
  13. //
  14. template <class Functor>
  15. typename Functor::result_type sum_series(Functor& func, int bits);
  16. template <class Functor>
  17. typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms);
  18. template <class Functor, class U>
  19. typename Functor::result_type sum_series(Functor& func, int bits, U init_value);
  20. template <class Functor, class U>
  21. typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms, U init_value);
  22. template <class Functor>
  23. typename Functor::result_type kahan_sum_series(Functor& func, int bits);
  24. template <class Functor>
  25. typename Functor::result_type kahan_sum_series(Functor& func, int bits, boost::uintmax_t& max_terms);
  26. }}} // namespaces
  27. [h4 Description]
  28. These algorithms are intended for the
  29. [@http://en.wikipedia.org/wiki/Series_%28mathematics%29 summation of infinite series].
  30. Each of the algorithms takes a nullary-function object as the first argument:
  31. the function object will be repeatedly invoked to pull successive terms from
  32. the series being summed.
  33. The second argument is the precision required,
  34. summation will stop when the next term is less than
  35. /tolerance/ times the result. The deprecated versions of `sum_series`
  36. take an integer number of bits here - internally they just convert this to
  37. a tolerance and forward the call.
  38. The third argument /max_terms/ sets an upper limit on the number
  39. of terms of the series to evaluate. In addition, on exit the function will
  40. set /max_terms/ to the actual number of terms of the series that were
  41. evaluated: this is particularly useful for profiling the convergence
  42. properties of a new series.
  43. The final optional argument /init_value/ is the initial value of the sum
  44. to which the terms of the series should be added. This is useful in two situations:
  45. * Where the first value of the series has a different formula to successive
  46. terms. In this case the first value in the series can be passed as the
  47. last argument and the logic of the function object can then be simplified
  48. to return subsequent terms.
  49. * Where the series is being added (or subtracted) from some other value:
  50. termination of the series will likely occur much more rapidly if that other
  51. value is passed as the last argument. For example, there are several functions
  52. that can be expressed as /1 - S(z)/ where S(z) is an infinite series. In this
  53. case, pass -1 as the last argument and then negate the result of the summation
  54. to get the result of /1 - S(z)/.
  55. The two /kahan_sum_series/ variants of these algorithms maintain a carry term
  56. that corrects for roundoff error during summation.
  57. They are inspired by the
  58. [@http://en.wikipedia.org/wiki/Kahan_Summation_Algorithm /Kahan Summation Formula/]
  59. that appears in
  60. [@http://docs.sun.com/source/806-3568/ncg_goldberg.html What Every Computer Scientist Should Know About Floating-Point Arithmetic].
  61. However, it should be pointed out that there are very few series that require
  62. summation in this way.
  63. [h4 Examples]
  64. [import ../../example/series.cpp]
  65. These examples are all in [@../../example/series.cpp]
  66. Let's suppose we want to implement /log(1+x)/ via its infinite series,
  67. [equation log1pseries]
  68. We begin by writing a small function object to return successive terms
  69. of the series:
  70. [series_log1p]
  71. Implementing log(1+x) is now fairly trivial:
  72. [series_log1p_func]
  73. We can almost use the code above for complex numbers as well - unfortunately we need a slightly different
  74. definition for epsilon, and within the functor, mixed complex and integer arithmetic is sadly not supported
  75. (as of C++17), so we need to cast out integers to floats:
  76. [series_clog1p_func]
  77. Of course with a few traits classes and a bit of meta-programming we could fold these two implementations into one, but that's beyond the scope of these examples.
  78. [endsect] [/section Series Evaluation]
  79. [/
  80. Copyright 2006 John Maddock and Paul A. Bristow.
  81. Distributed under the Boost Software License, Version 1.0.
  82. (See accompanying file LICENSE_1_0.txt or copy at
  83. http://www.boost.org/LICENSE_1_0.txt).
  84. ]