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;
71 static_assert(!std::is_integral<result_type>::value,
72 "The return type cannot be integral, it must be either a real or complex floating point type.");
73 if (!(boost::math::isnan)(a) && !(boost::math::isnan)(b))
77 if ((a <= -tools::max_value<Real>()) && (b >= tools::max_value<Real>()))
79 auto u = [&](const Real& t, const Real& tc)->result_type
84 inv = 1 / ((2 - tc) * tc);
86 inv = 1 / ((2 + tc) * -tc);
89 return f(t*inv)*(1 + t_sq)*inv*inv;
91 Real limit = sqrt(tools::min_value<Real>()) * 4;
92 return m_imp->integrate(u, error, L1, function, limit, limit, tolerance, levels);
95 // Right limit is infinite:
96 if ((boost::math::isfinite)(a) && (b >= tools::max_value<Real>()))
98 auto u = [&](const Real& t, const Real& tc)->result_type
108 arg = a + tc / (2 - tc);
111 Real left_limit = sqrt(tools::min_value<Real>()) * 4;
112 result_type Q = Real(2) * m_imp->integrate(u, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
125 if ((boost::math::isfinite)(b) && (a <= -tools::max_value<Real>()))
127 auto v = [&](const Real& t, const Real& tc)->result_type
139 return f(b - arg) * z * z;
142 Real left_limit = sqrt(tools::min_value<Real>()) * 4;
143 result_type Q = Real(2) * m_imp->integrate(v, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
155 if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
159 return result_type(0);
163 return -this->integrate(f, b, a, tolerance, error, L1, levels);
165 Real avg = (a + b)*half<Real>();
166 Real diff = (b - a)*half<Real>();
167 Real avg_over_diff_m1 = a / diff;
168 Real avg_over_diff_p1 = b / diff;
169 bool have_small_left = fabs(a) < 0.5f;
170 bool have_small_right = fabs(b) < 0.5f;
171 Real left_min_complement = float_next(avg_over_diff_m1) - avg_over_diff_m1;
172 Real min_complement_limit = (std::max)(tools::min_value<Real>(), Real(tools::min_value<Real>() / diff));
173 if (left_min_complement < min_complement_limit)
174 left_min_complement = min_complement_limit;
175 Real right_min_complement = avg_over_diff_p1 - float_prior(avg_over_diff_p1);
176 if (right_min_complement < min_complement_limit)
177 right_min_complement = min_complement_limit;
179 // These asserts will fail only if rounding errors on
180 // type Real have accumulated so much error that it's
181 // broken our internal logic. Should that prove to be
182 // a persistent issue, we might need to add a bit of fudge
183 // factor to move left_min_complement and right_min_complement
184 // further from the end points of the range.
186 BOOST_MATH_ASSERT((left_min_complement * diff + a) > a);
187 BOOST_MATH_ASSERT((b - right_min_complement * diff) < b);
188 auto u = [&](Real z, Real zc)->result_type
194 return f(diff * (avg_over_diff_m1 - zc));
195 position = a - diff * zc;
200 return f(diff * (avg_over_diff_p1 - zc));
201 position = b - diff * zc;
204 position = avg + diff*z;
205 BOOST_MATH_ASSERT(position != a);
206 BOOST_MATH_ASSERT(position != b);
209 result_type Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
222 return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
225 template<class Real, class Policy>
227 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
230 using boost::math::constants::half;
231 using boost::math::quadrature::detail::tanh_sinh_detail;
233 static const char* function = "tanh_sinh<%1%>::integrate";
235 if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
239 return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
241 auto u = [&](Real z, Real zc)->Real
244 return f((a - b) * zc / 2 + a, (b - a) * zc / 2);
246 return f((a - b) * zc / 2 + b, (b - a) * zc / 2);
248 Real diff = (b - a)*half<Real>();
249 Real left_min_complement = tools::min_value<Real>() * 4;
250 Real right_min_complement = tools::min_value<Real>() * 4;
251 Real Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
263 return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
266 template<class Real, class Policy>
268 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
270 using boost::math::quadrature::detail::tanh_sinh_detail;
271 static const char* function = "tanh_sinh<%1%>::integrate";
272 Real min_complement = tools::epsilon<Real>();
273 return m_imp->integrate([&](const Real& arg, const Real&) { return f(arg); }, error, L1, function, min_complement, min_complement, tolerance, levels);
276 template<class Real, class Policy>
278 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
280 using boost::math::quadrature::detail::tanh_sinh_detail;
281 static const char* function = "tanh_sinh<%1%>::integrate";
282 Real min_complement = tools::min_value<Real>() * 4;
283 return m_imp->integrate(f, error, L1, function, min_complement, min_complement, tolerance, levels);