]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/math/tools/series.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / math / tools / series.hpp
CommitLineData
7c673cae
FG
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)
5
6#ifndef BOOST_MATH_TOOLS_SERIES_INCLUDED
7#define BOOST_MATH_TOOLS_SERIES_INCLUDED
8
9#ifdef _MSC_VER
10#pragma once
11#endif
12
1e59de90
TL
13#include <cmath>
14#include <cstdint>
15#include <limits>
7c673cae
FG
16#include <boost/math/tools/config.hpp>
17
18namespace boost{ namespace math{ namespace tools{
19
20//
21// Simple series summation come first:
22//
23template <class Functor, class U, class V>
1e59de90 24inline 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>()()))
7c673cae
FG
25{
26 BOOST_MATH_STD_USING
27
28 typedef typename Functor::result_type result_type;
29
1e59de90 30 std::uintmax_t counter = max_terms;
7c673cae
FG
31
32 result_type result = init_value;
33 result_type next_term;
34 do{
35 next_term = func();
36 result += next_term;
37 }
92f5a8d4 38 while((abs(factor * result) < abs(next_term)) && --counter);
7c673cae
FG
39
40 // set max_terms to the actual number of terms of the series evaluated:
41 max_terms = max_terms - counter;
42
43 return result;
44}
45
46template <class Functor, class U>
1e59de90 47inline 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>()()))
7c673cae
FG
48{
49 typename Functor::result_type init_value = 0;
50 return sum_series(func, factor, max_terms, init_value);
51}
52
53template <class Functor, class U>
1e59de90 54inline 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>()()))
7c673cae
FG
55{
56 BOOST_MATH_STD_USING
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);
60}
61
62template <class Functor>
1e59de90 63inline typename Functor::result_type sum_series(Functor& func, int bits) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
7c673cae
FG
64{
65 BOOST_MATH_STD_USING
66 typedef typename Functor::result_type result_type;
1e59de90 67 std::uintmax_t iters = (std::numeric_limits<std::uintmax_t>::max)();
7c673cae
FG
68 result_type init_val = 0;
69 return sum_series(func, bits, iters, init_val);
70}
71
72template <class Functor>
1e59de90 73inline 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>()()))
7c673cae
FG
74{
75 BOOST_MATH_STD_USING
76 typedef typename Functor::result_type result_type;
77 result_type init_val = 0;
78 return sum_series(func, bits, max_terms, init_val);
79}
80
81template <class Functor, class U>
1e59de90 82inline 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>()()))
7c673cae
FG
83{
84 BOOST_MATH_STD_USING
1e59de90 85 std::uintmax_t iters = (std::numeric_limits<std::uintmax_t>::max)();
7c673cae
FG
86 return sum_series(func, bits, iters, init_value);
87}
92f5a8d4
TL
88//
89// Checked summation:
90//
91template <class Functor, class U, class V>
1e59de90 92inline 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>()()))
92f5a8d4
TL
93{
94 BOOST_MATH_STD_USING
95
96 typedef typename Functor::result_type result_type;
97
1e59de90 98 std::uintmax_t counter = max_terms;
92f5a8d4
TL
99
100 result_type result = init_value;
101 result_type next_term;
102 do {
103 next_term = func();
104 result += next_term;
105 norm += fabs(next_term);
106 } while ((abs(factor * result) < abs(next_term)) && --counter);
107
108 // set max_terms to the actual number of terms of the series evaluated:
109 max_terms = max_terms - counter;
110
111 return result;
112}
113
7c673cae
FG
114
115//
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.
119//
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.
126//
127template <class Functor>
1e59de90 128inline typename Functor::result_type kahan_sum_series(Functor& func, int bits) noexcept(BOOST_MATH_IS_FLOAT(typename Functor::result_type) && noexcept(std::declval<Functor>()()))
7c673cae
FG
129{
130 BOOST_MATH_STD_USING
131
132 typedef typename Functor::result_type result_type;
133
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;
138 do{
139 next_term = func();
140 y = next_term - carry;
141 t = result + y;
142 carry = t - result;
143 carry -= y;
144 result = t;
145 }
146 while(fabs(result) < fabs(factor * next_term));
147 return result;
148}
149
150template <class Functor>
1e59de90 151inline 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>()()))
7c673cae
FG
152{
153 BOOST_MATH_STD_USING
154
155 typedef typename Functor::result_type result_type;
156
1e59de90 157 std::uintmax_t counter = max_terms;
7c673cae
FG
158
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;
163 do{
164 next_term = func();
165 y = next_term - carry;
166 t = result + y;
167 carry = t - result;
168 carry -= y;
169 result = t;
170 }
171 while((fabs(result) < fabs(factor * next_term)) && --counter);
172
173 // set max_terms to the actual number of terms of the series evaluated:
174 max_terms = max_terms - counter;
175
176 return result;
177}
178
179} // namespace tools
180} // namespace math
181} // namespace boost
182
183#endif // BOOST_MATH_TOOLS_SERIES_INCLUDED
184