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)
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.
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.
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.
19 * In addition, if you are integrating a periodic function over a period, the trapezoidal rule is better.
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.
29 #ifndef BOOST_MATH_QUADRATURE_TANH_SINH_HPP
30 #define BOOST_MATH_QUADRATURE_TANH_SINH_HPP
35 #include <boost/math/quadrature/detail/tanh_sinh_detail.hpp>
37 namespace boost{ namespace math{ namespace quadrature {
39 template<class Real, class Policy = policies::policy<> >
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)) {}
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;
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;
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;
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;
57 std::shared_ptr<detail::tanh_sinh_detail<Real, Policy>> m_imp;
60 template<class Real, class Policy>
62 auto 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
65 using boost::math::constants::half;
66 using boost::math::quadrature::detail::tanh_sinh_detail;
68 static const char* function = "tanh_sinh<%1%>::integrate";
70 typedef decltype(std::declval<F>()(std::declval<Real>())) result_type;
72 if (!(boost::math::isnan)(a) && !(boost::math::isnan)(b))
76 if ((a <= -tools::max_value<Real>()) && (b >= tools::max_value<Real>()))
78 auto u = [&](const Real& t, const Real& tc)->result_type
83 inv = 1 / ((2 - tc) * tc);
85 inv = 1 / ((2 + tc) * -tc);
88 return f(t*inv)*(1 + t_sq)*inv*inv;
90 Real limit = sqrt(tools::min_value<Real>()) * 4;
91 return m_imp->integrate(u, error, L1, function, limit, limit, tolerance, levels);
94 // Right limit is infinite:
95 if ((boost::math::isfinite)(a) && (b >= tools::max_value<Real>()))
97 auto u = [&](const Real& t, const Real& tc)->result_type
107 arg = a + tc / (2 - tc);
110 Real left_limit = sqrt(tools::min_value<Real>()) * 4;
111 result_type Q = Real(2) * m_imp->integrate(u, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
120 if ((boost::math::isfinite)(b) && (a <= -tools::max_value<Real>()))
122 auto v = [&](const Real& t, const Real& tc)->result_type
134 return f(b - arg) * z * z;
137 Real left_limit = sqrt(tools::min_value<Real>()) * 4;
138 result_type Q = Real(2) * m_imp->integrate(v, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
146 if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
150 return result_type(0);
154 return -this->integrate(f, b, a, tolerance, error, L1, levels);
156 Real avg = (a + b)*half<Real>();
157 Real diff = (b - a)*half<Real>();
158 Real avg_over_diff_m1 = a / diff;
159 Real avg_over_diff_p1 = b / diff;
160 bool have_small_left = fabs(a) < 0.5f;
161 bool have_small_right = fabs(b) < 0.5f;
162 Real left_min_complement = float_next(avg_over_diff_m1) - avg_over_diff_m1;
163 Real min_complement_limit = (std::max)(tools::min_value<Real>(), Real(tools::min_value<Real>() / diff));
164 if (left_min_complement < min_complement_limit)
165 left_min_complement = min_complement_limit;
166 Real right_min_complement = avg_over_diff_p1 - float_prior(avg_over_diff_p1);
167 if (right_min_complement < min_complement_limit)
168 right_min_complement = min_complement_limit;
170 // These asserts will fail only if rounding errors on
171 // type Real have accumulated so much error that it's
172 // broken our internal logic. Should that prove to be
173 // a persistent issue, we might need to add a bit of fudge
174 // factor to move left_min_complement and right_min_complement
175 // further from the end points of the range.
177 BOOST_ASSERT((left_min_complement * diff + a) > a);
178 BOOST_ASSERT((b - right_min_complement * diff) < b);
179 auto u = [&](Real z, Real zc)->result_type
185 return f(diff * (avg_over_diff_m1 - zc));
186 position = a - diff * zc;
191 return f(diff * (avg_over_diff_p1 - zc));
192 position = b - diff * zc;
195 position = avg + diff*z;
196 BOOST_ASSERT(position != a);
197 BOOST_ASSERT(position != b);
200 result_type Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
209 return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
212 template<class Real, class Policy>
214 auto 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
217 using boost::math::constants::half;
218 using boost::math::quadrature::detail::tanh_sinh_detail;
220 static const char* function = "tanh_sinh<%1%>::integrate";
222 if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
226 return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
228 auto u = [&](Real z, Real zc)->Real
231 return f((a - b) * zc / 2 + a, (b - a) * zc / 2);
233 return f((a - b) * zc / 2 + b, (b - a) * zc / 2);
235 Real diff = (b - a)*half<Real>();
236 Real left_min_complement = tools::min_value<Real>() * 4;
237 Real right_min_complement = tools::min_value<Real>() * 4;
238 Real Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
246 return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
249 template<class Real, class Policy>
251 auto 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
253 using boost::math::quadrature::detail::tanh_sinh_detail;
254 static const char* function = "tanh_sinh<%1%>::integrate";
255 Real min_complement = tools::epsilon<Real>();
256 return m_imp->integrate([&](const Real& arg, const Real&) { return f(arg); }, error, L1, function, min_complement, min_complement, tolerance, levels);
259 template<class Real, class Policy>
261 auto 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
263 using boost::math::quadrature::detail::tanh_sinh_detail;
264 static const char* function = "tanh_sinh<%1%>::integrate";
265 Real min_complement = tools::min_value<Real>() * 4;
266 return m_imp->integrate(f, error, L1, function, min_complement, min_complement, tolerance, levels);