123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- #ifndef BOOST_MATH_TOOLS_SERIES_INCLUDED
- #define BOOST_MATH_TOOLS_SERIES_INCLUDED
- #ifdef _MSC_VER
- #pragma once
- #endif
- #include <cmath>
- #include <cstdint>
- #include <limits>
- #include <boost/math/tools/config.hpp>
- namespace boost{ namespace math{ namespace tools{
- template <class Functor, class U, class V>
- inline typename Functor::result_type sum_series(Functor& func, const U& factor, std::uintmax_t& max_terms, const V& init_value) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- std::uintmax_t counter = max_terms;
- result_type result = init_value;
- result_type next_term;
- do{
- next_term = func();
- result += next_term;
- }
- while((abs(factor * result) < abs(next_term)) && --counter);
-
- max_terms = max_terms - counter;
- return result;
- }
- template <class Functor, class U>
- inline typename Functor::result_type sum_series(Functor& func, const U& factor, std::uintmax_t& max_terms) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- typename Functor::result_type init_value = 0;
- return sum_series(func, factor, max_terms, init_value);
- }
- template <class Functor, class U>
- inline typename Functor::result_type sum_series(Functor& func, int bits, std::uintmax_t& max_terms, const U& init_value) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- result_type factor = ldexp(result_type(1), 1 - bits);
- return sum_series(func, factor, max_terms, init_value);
- }
- template <class Functor>
- inline typename Functor::result_type sum_series(Functor& func, int bits) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- std::uintmax_t iters = (std::numeric_limits<std::uintmax_t>::max)();
- result_type init_val = 0;
- return sum_series(func, bits, iters, init_val);
- }
- template <class Functor>
- inline typename Functor::result_type sum_series(Functor& func, int bits, std::uintmax_t& max_terms) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- result_type init_val = 0;
- return sum_series(func, bits, max_terms, init_val);
- }
- template <class Functor, class U>
- inline typename Functor::result_type sum_series(Functor& func, int bits, const U& init_value) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- std::uintmax_t iters = (std::numeric_limits<std::uintmax_t>::max)();
- return sum_series(func, bits, iters, init_value);
- }
- template <class Functor, class U, class V>
- inline typename Functor::result_type checked_sum_series(Functor& func, const U& factor, std::uintmax_t& max_terms, const V& init_value, V& norm) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- std::uintmax_t counter = max_terms;
- result_type result = init_value;
- result_type next_term;
- do {
- next_term = func();
- result += next_term;
- norm += fabs(next_term);
- } while ((abs(factor * result) < abs(next_term)) && --counter);
-
- max_terms = max_terms - counter;
- return result;
- }
- template <class Functor>
- inline typename Functor::result_type kahan_sum_series(Functor& func, int bits) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- result_type factor = pow(result_type(2), result_type(bits));
- result_type result = func();
- result_type next_term, y, t;
- result_type carry = 0;
- do{
- next_term = func();
- y = next_term - carry;
- t = result + y;
- carry = t - result;
- carry -= y;
- result = t;
- }
- while(fabs(result) < fabs(factor * next_term));
- return result;
- }
- template <class Functor>
- inline typename Functor::result_type kahan_sum_series(Functor& func, int bits, std::uintmax_t& max_terms) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
- {
- BOOST_MATH_STD_USING
- typedef typename Functor::result_type result_type;
- std::uintmax_t counter = max_terms;
- result_type factor = ldexp(result_type(1), bits);
- result_type result = func();
- result_type next_term, y, t;
- result_type carry = 0;
- do{
- next_term = func();
- y = next_term - carry;
- t = result + y;
- carry = t - result;
- carry -= y;
- result = t;
- }
- while((fabs(result) < fabs(factor * next_term)) && --counter);
-
- max_terms = max_terms - counter;
- return result;
- }
- }
- }
- }
- #endif
|