1 // (C) Copyright John Maddock 2005-2006.
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)
6 #ifndef BOOST_MATH_TOOLS_SERIES_INCLUDED
7 #define BOOST_MATH_TOOLS_SERIES_INCLUDED
13 #include <boost/config/no_tr1/cmath.hpp>
14 #include <boost/cstdint.hpp>
15 #include <boost/limits.hpp>
16 #include <boost/math/tools/config.hpp>
18 namespace boost{ namespace math{ namespace tools{
21 // Simple series summation come first:
23 template <class Functor, class U, class V>
24 inline typename Functor::result_type sum_series(Functor& func, const U& factor, boost::uintmax_t& max_terms, const V& init_value) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
28 typedef typename Functor::result_type result_type;
30 boost::uintmax_t counter = max_terms;
32 result_type result = init_value;
33 result_type next_term;
38 while((abs(factor * result) < abs(next_term)) && --counter);
40 // set max_terms to the actual number of terms of the series evaluated:
41 max_terms = max_terms - counter;
46 template <class Functor, class U>
47 inline typename Functor::result_type sum_series(Functor& func, const U& factor, boost::uintmax_t& max_terms) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
49 typename Functor::result_type init_value = 0;
50 return sum_series(func, factor, max_terms, init_value);
53 template <class Functor, class U>
54 inline typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms, const U& init_value) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
57 typedef typename Functor::result_type result_type;
58 result_type factor = ldexp(result_type(1), 1 - bits);
59 return sum_series(func, factor, max_terms, init_value);
62 template <class Functor>
63 inline typename Functor::result_type sum_series(Functor& func, int bits) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
66 typedef typename Functor::result_type result_type;
67 boost::uintmax_t iters = (std::numeric_limits<boost::uintmax_t>::max)();
68 result_type init_val = 0;
69 return sum_series(func, bits, iters, init_val);
72 template <class Functor>
73 inline typename Functor::result_type sum_series(Functor& func, int bits, boost::uintmax_t& max_terms) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
76 typedef typename Functor::result_type result_type;
77 result_type init_val = 0;
78 return sum_series(func, bits, max_terms, init_val);
81 template <class Functor, class U>
82 inline typename Functor::result_type sum_series(Functor& func, int bits, const U& init_value) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
85 boost::uintmax_t iters = (std::numeric_limits<boost::uintmax_t>::max)();
86 return sum_series(func, bits, iters, init_value);
91 template <class Functor, class U, class V>
92 inline typename Functor::result_type checked_sum_series(Functor& func, const U& factor, boost::uintmax_t& max_terms, const V& init_value, V& norm) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
96 typedef typename Functor::result_type result_type;
98 boost::uintmax_t counter = max_terms;
100 result_type result = init_value;
101 result_type next_term;
105 norm += fabs(next_term);
106 } while ((abs(factor * result) < abs(next_term)) && --counter);
108 // set max_terms to the actual number of terms of the series evaluated:
109 max_terms = max_terms - counter;
116 // Algorithm kahan_sum_series invokes Functor func until the N'th
117 // term is too small to have any effect on the total, the terms
118 // are added using the Kahan summation method.
120 // CAUTION: Optimizing compilers combined with extended-precision
121 // machine registers conspire to render this algorithm partly broken:
122 // double rounding of intermediate terms (first to a long double machine
123 // register, and then to a double result) cause the rounding error computed
124 // by the algorithm to be off by up to 1ulp. However this occurs rarely, and
125 // in any case the result is still much better than a naive summation.
127 template <class Functor>
128 inline typename Functor::result_type kahan_sum_series(Functor& func, int bits) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
132 typedef typename Functor::result_type result_type;
134 result_type factor = pow(result_type(2), bits);
135 result_type result = func();
136 result_type next_term, y, t;
137 result_type carry = 0;
140 y = next_term - carry;
146 while(fabs(result) < fabs(factor * next_term));
150 template <class Functor>
151 inline typename Functor::result_type kahan_sum_series(Functor& func, int bits, boost::uintmax_t& max_terms) BOOST_NOEXCEPT_IF(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
155 typedef typename Functor::result_type result_type;
157 boost::uintmax_t counter = max_terms;
159 result_type factor = ldexp(result_type(1), bits);
160 result_type result = func();
161 result_type next_term, y, t;
162 result_type carry = 0;
165 y = next_term - carry;
171 while((fabs(result) < fabs(factor * next_term)) && --counter);
173 // set max_terms to the actual number of terms of the series evaluated:
174 max_terms = max_terms - counter;
183 #endif // BOOST_MATH_TOOLS_SERIES_INCLUDED