tanh_sinh.hpp 11 KB

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