]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/math/quadrature/tanh_sinh.hpp
import quincy beta 17.1.0
[ceph.git] / ceph / src / boost / boost / math / quadrature / tanh_sinh.hpp
CommitLineData
b32b8144
FG
1// Copyright Nick Thompson, 2017
2// Use, modification and distribution are subject to the
3// Boost Software License, Version 1.0.
4// (See accompanying file LICENSE_1_0.txt
5// or copy at http://www.boost.org/LICENSE_1_0.txt)
6
7/*
8 * This class performs tanh-sinh quadrature on the real line.
9 * Tanh-sinh quadrature is exponentially convergent for integrands in Hardy spaces,
10 * (see https://en.wikipedia.org/wiki/Hardy_space for a formal definition), and is optimal for a random function from that class.
11 *
12 * The tanh-sinh quadrature is one of a class of so called "double exponential quadratures"-there is a large family of them,
13 * but this one seems to be the most commonly used.
14 *
15 * As always, there are caveats: For instance, if the function you want to integrate is not holomorphic on the unit disk,
16 * then the rapid convergence will be spoiled. In this case, a more appropriate quadrature is (say) Romberg, which does not
17 * require the function to be holomorphic, only differentiable up to some order.
18 *
19 * In addition, if you are integrating a periodic function over a period, the trapezoidal rule is better.
20 *
21 * References:
22 *
23 * 1) Mori, Masatake. "Quadrature formulas obtained by variable transformation and the DE-rule." Journal of Computational and Applied Mathematics 12 (1985): 119-130.
24 * 2) Bailey, David H., Karthik Jeyabalan, and Xiaoye S. Li. "A comparison of three high-precision quadrature schemes." Experimental Mathematics 14.3 (2005): 317-329.
25 * 3) Press, William H., et al. "Numerical recipes third edition: the art of scientific computing." Cambridge University Press 32 (2007): 10013-2473.
26 *
27 */
28
29#ifndef BOOST_MATH_QUADRATURE_TANH_SINH_HPP
30#define BOOST_MATH_QUADRATURE_TANH_SINH_HPP
31
32#include <cmath>
33#include <limits>
34#include <memory>
35#include <boost/math/quadrature/detail/tanh_sinh_detail.hpp>
36
37namespace boost{ namespace math{ namespace quadrature {
38
39template<class Real, class Policy = policies::policy<> >
40class tanh_sinh
41{
42public:
43 tanh_sinh(size_t max_refinements = 15, const Real& min_complement = tools::min_value<Real>() * 4)
44 : m_imp(std::make_shared<detail::tanh_sinh_detail<Real, Policy>>(max_refinements, min_complement)) {}
45
46 template<class F>
92f5a8d4 47 auto integrate(const F f, Real a, Real b, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>())) const;
b32b8144 48 template<class F>
92f5a8d4 49 auto integrate(const F f, Real a, Real b, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const;
b32b8144
FG
50
51 template<class F>
92f5a8d4 52 auto integrate(const F f, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>())) const;
b32b8144 53 template<class F>
92f5a8d4 54 auto integrate(const F f, Real tolerance = tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const;
b32b8144
FG
55
56private:
57 std::shared_ptr<detail::tanh_sinh_detail<Real, Policy>> m_imp;
58};
59
60template<class Real, class Policy>
61template<class F>
92f5a8d4 62auto tanh_sinh<Real, Policy>::integrate(const F f, Real a, Real b, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>())) const
b32b8144
FG
63{
64 BOOST_MATH_STD_USING
65 using boost::math::constants::half;
66 using boost::math::quadrature::detail::tanh_sinh_detail;
67
68 static const char* function = "tanh_sinh<%1%>::integrate";
69
92f5a8d4
TL
70 typedef decltype(std::declval<F>()(std::declval<Real>())) result_type;
71
b32b8144
FG
72 if (!(boost::math::isnan)(a) && !(boost::math::isnan)(b))
73 {
74
75 // Infinite limits:
76 if ((a <= -tools::max_value<Real>()) && (b >= tools::max_value<Real>()))
77 {
92f5a8d4 78 auto u = [&](const Real& t, const Real& tc)->result_type
b32b8144
FG
79 {
80 Real t_sq = t*t;
81 Real inv;
82 if (t > 0.5f)
83 inv = 1 / ((2 - tc) * tc);
84 else if(t < -0.5)
85 inv = 1 / ((2 + tc) * -tc);
86 else
87 inv = 1 / (1 - t_sq);
88 return f(t*inv)*(1 + t_sq)*inv*inv;
89 };
90 Real limit = sqrt(tools::min_value<Real>()) * 4;
91 return m_imp->integrate(u, error, L1, function, limit, limit, tolerance, levels);
92 }
93
94 // Right limit is infinite:
95 if ((boost::math::isfinite)(a) && (b >= tools::max_value<Real>()))
96 {
92f5a8d4 97 auto u = [&](const Real& t, const Real& tc)->result_type
b32b8144
FG
98 {
99 Real z, arg;
100 if (t > -0.5f)
101 z = 1 / (t + 1);
102 else
103 z = -1 / tc;
104 if (t < 0.5)
105 arg = 2 * z + a - 1;
106 else
107 arg = a + tc / (2 - tc);
108 return f(arg)*z*z;
109 };
110 Real left_limit = sqrt(tools::min_value<Real>()) * 4;
92f5a8d4 111 result_type Q = Real(2) * m_imp->integrate(u, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
b32b8144
FG
112 if (L1)
113 {
114 *L1 *= 2;
115 }
20effc67
TL
116 if (error)
117 {
118 *error *= 2;
119 }
b32b8144
FG
120
121 return Q;
122 }
123
124 if ((boost::math::isfinite)(b) && (a <= -tools::max_value<Real>()))
125 {
92f5a8d4 126 auto v = [&](const Real& t, const Real& tc)->result_type
b32b8144
FG
127 {
128 Real z;
129 if (t > -0.5)
130 z = 1 / (t + 1);
131 else
132 z = -1 / tc;
133 Real arg;
134 if (t < 0.5)
135 arg = 2 * z - 1;
136 else
137 arg = tc / (2 - tc);
138 return f(b - arg) * z * z;
139 };
140
141 Real left_limit = sqrt(tools::min_value<Real>()) * 4;
92f5a8d4 142 result_type Q = Real(2) * m_imp->integrate(v, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
b32b8144
FG
143 if (L1)
144 {
145 *L1 *= 2;
146 }
20effc67
TL
147 if (error)
148 {
149 *error *= 2;
150 }
b32b8144
FG
151 return Q;
152 }
153
154 if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
155 {
f67539c2 156 if (a == b)
b32b8144 157 {
f67539c2
TL
158 return result_type(0);
159 }
160 if (b < a)
161 {
162 return -this->integrate(f, b, a, tolerance, error, L1, levels);
b32b8144
FG
163 }
164 Real avg = (a + b)*half<Real>();
165 Real diff = (b - a)*half<Real>();
166 Real avg_over_diff_m1 = a / diff;
167 Real avg_over_diff_p1 = b / diff;
168 bool have_small_left = fabs(a) < 0.5f;
169 bool have_small_right = fabs(b) < 0.5f;
170 Real left_min_complement = float_next(avg_over_diff_m1) - avg_over_diff_m1;
92f5a8d4
TL
171 Real min_complement_limit = (std::max)(tools::min_value<Real>(), Real(tools::min_value<Real>() / diff));
172 if (left_min_complement < min_complement_limit)
173 left_min_complement = min_complement_limit;
b32b8144 174 Real right_min_complement = avg_over_diff_p1 - float_prior(avg_over_diff_p1);
92f5a8d4
TL
175 if (right_min_complement < min_complement_limit)
176 right_min_complement = min_complement_limit;
177 //
178 // These asserts will fail only if rounding errors on
179 // type Real have accumulated so much error that it's
180 // broken our internal logic. Should that prove to be
181 // a persistent issue, we might need to add a bit of fudge
182 // factor to move left_min_complement and right_min_complement
183 // further from the end points of the range.
184 //
185 BOOST_ASSERT((left_min_complement * diff + a) > a);
186 BOOST_ASSERT((b - right_min_complement * diff) < b);
187 auto u = [&](Real z, Real zc)->result_type
b32b8144 188 {
92f5a8d4
TL
189 Real position;
190 if (z < -0.5)
191 {
192 if(have_small_left)
193 return f(diff * (avg_over_diff_m1 - zc));
194 position = a - diff * zc;
195 }
20effc67 196 else if (z > 0.5)
92f5a8d4
TL
197 {
198 if(have_small_right)
199 return f(diff * (avg_over_diff_p1 - zc));
200 position = b - diff * zc;
201 }
202 else
203 position = avg + diff*z;
b32b8144
FG
204 BOOST_ASSERT(position != a);
205 BOOST_ASSERT(position != b);
206 return f(position);
207 };
92f5a8d4 208 result_type Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
b32b8144
FG
209
210 if (L1)
211 {
212 *L1 *= diff;
213 }
20effc67
TL
214 if (error)
215 {
216 *error *= diff;
217 }
b32b8144
FG
218 return Q;
219 }
220 }
221 return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
222}
223
224template<class Real, class Policy>
225template<class F>
92f5a8d4 226auto tanh_sinh<Real, Policy>::integrate(const F f, Real a, Real b, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const
b32b8144
FG
227{
228 BOOST_MATH_STD_USING
229 using boost::math::constants::half;
230 using boost::math::quadrature::detail::tanh_sinh_detail;
231
232 static const char* function = "tanh_sinh<%1%>::integrate";
233
234 if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
235 {
236 if (b <= a)
237 {
238 return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
239 }
240 auto u = [&](Real z, Real zc)->Real
241 {
242 if (z < 0)
243 return f((a - b) * zc / 2 + a, (b - a) * zc / 2);
244 else
245 return f((a - b) * zc / 2 + b, (b - a) * zc / 2);
246 };
247 Real diff = (b - a)*half<Real>();
248 Real left_min_complement = tools::min_value<Real>() * 4;
249 Real right_min_complement = tools::min_value<Real>() * 4;
250 Real Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
251
252 if (L1)
253 {
254 *L1 *= diff;
255 }
20effc67
TL
256 if (error)
257 {
258 *error *= diff;
259 }
b32b8144
FG
260 return Q;
261 }
262 return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
263}
264
265template<class Real, class Policy>
266template<class F>
92f5a8d4 267auto tanh_sinh<Real, Policy>::integrate(const F f, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>())) const
b32b8144
FG
268{
269 using boost::math::quadrature::detail::tanh_sinh_detail;
270 static const char* function = "tanh_sinh<%1%>::integrate";
271 Real min_complement = tools::epsilon<Real>();
272 return m_imp->integrate([&](const Real& arg, const Real&) { return f(arg); }, error, L1, function, min_complement, min_complement, tolerance, levels);
273}
274
275template<class Real, class Policy>
276template<class F>
92f5a8d4 277auto tanh_sinh<Real, Policy>::integrate(const F f, Real tolerance, Real* error, Real* L1, std::size_t* levels) ->decltype(std::declval<F>()(std::declval<Real>(), std::declval<Real>())) const
b32b8144
FG
278{
279 using boost::math::quadrature::detail::tanh_sinh_detail;
280 static const char* function = "tanh_sinh<%1%>::integrate";
281 Real min_complement = tools::min_value<Real>() * 4;
282 return m_imp->integrate(f, error, L1, function, min_complement, min_complement, tolerance, levels);
283}
284
285}
286}
287}
288#endif