tanh_sinh.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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(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(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(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(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(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. typedef decltype(std::declval<F>()(std::declval<Real>())) result_type;
  60. if (!(boost::math::isnan)(a) && !(boost::math::isnan)(b))
  61. {
  62. // Infinite limits:
  63. if ((a <= -tools::max_value<Real>()) && (b >= tools::max_value<Real>()))
  64. {
  65. auto u = [&](const Real& t, const Real& tc)->result_type
  66. {
  67. Real t_sq = t*t;
  68. Real inv;
  69. if (t > 0.5f)
  70. inv = 1 / ((2 - tc) * tc);
  71. else if(t < -0.5)
  72. inv = 1 / ((2 + tc) * -tc);
  73. else
  74. inv = 1 / (1 - t_sq);
  75. return f(t*inv)*(1 + t_sq)*inv*inv;
  76. };
  77. Real limit = sqrt(tools::min_value<Real>()) * 4;
  78. return m_imp->integrate(u, error, L1, function, limit, limit, tolerance, levels);
  79. }
  80. // Right limit is infinite:
  81. if ((boost::math::isfinite)(a) && (b >= tools::max_value<Real>()))
  82. {
  83. auto u = [&](const Real& t, const Real& tc)->result_type
  84. {
  85. Real z, arg;
  86. if (t > -0.5f)
  87. z = 1 / (t + 1);
  88. else
  89. z = -1 / tc;
  90. if (t < 0.5)
  91. arg = 2 * z + a - 1;
  92. else
  93. arg = a + tc / (2 - tc);
  94. return f(arg)*z*z;
  95. };
  96. Real left_limit = sqrt(tools::min_value<Real>()) * 4;
  97. result_type Q = Real(2) * m_imp->integrate(u, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
  98. if (L1)
  99. {
  100. *L1 *= 2;
  101. }
  102. return Q;
  103. }
  104. if ((boost::math::isfinite)(b) && (a <= -tools::max_value<Real>()))
  105. {
  106. auto v = [&](const Real& t, const Real& tc)->result_type
  107. {
  108. Real z;
  109. if (t > -0.5)
  110. z = 1 / (t + 1);
  111. else
  112. z = -1 / tc;
  113. Real arg;
  114. if (t < 0.5)
  115. arg = 2 * z - 1;
  116. else
  117. arg = tc / (2 - tc);
  118. return f(b - arg) * z * z;
  119. };
  120. Real left_limit = sqrt(tools::min_value<Real>()) * 4;
  121. result_type Q = Real(2) * m_imp->integrate(v, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
  122. if (L1)
  123. {
  124. *L1 *= 2;
  125. }
  126. return Q;
  127. }
  128. if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
  129. {
  130. if (b <= a)
  131. {
  132. return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
  133. }
  134. Real avg = (a + b)*half<Real>();
  135. Real diff = (b - a)*half<Real>();
  136. Real avg_over_diff_m1 = a / diff;
  137. Real avg_over_diff_p1 = b / diff;
  138. bool have_small_left = fabs(a) < 0.5f;
  139. bool have_small_right = fabs(b) < 0.5f;
  140. Real left_min_complement = float_next(avg_over_diff_m1) - avg_over_diff_m1;
  141. Real min_complement_limit = (std::max)(tools::min_value<Real>(), Real(tools::min_value<Real>() / diff));
  142. if (left_min_complement < min_complement_limit)
  143. left_min_complement = min_complement_limit;
  144. Real right_min_complement = avg_over_diff_p1 - float_prior(avg_over_diff_p1);
  145. if (right_min_complement < min_complement_limit)
  146. right_min_complement = min_complement_limit;
  147. //
  148. // These asserts will fail only if rounding errors on
  149. // type Real have accumulated so much error that it's
  150. // broken our internal logic. Should that prove to be
  151. // a persistent issue, we might need to add a bit of fudge
  152. // factor to move left_min_complement and right_min_complement
  153. // further from the end points of the range.
  154. //
  155. BOOST_ASSERT((left_min_complement * diff + a) > a);
  156. BOOST_ASSERT((b - right_min_complement * diff) < b);
  157. auto u = [&](Real z, Real zc)->result_type
  158. {
  159. Real position;
  160. if (z < -0.5)
  161. {
  162. if(have_small_left)
  163. return f(diff * (avg_over_diff_m1 - zc));
  164. position = a - diff * zc;
  165. }
  166. if (z > 0.5)
  167. {
  168. if(have_small_right)
  169. return f(diff * (avg_over_diff_p1 - zc));
  170. position = b - diff * zc;
  171. }
  172. else
  173. position = avg + diff*z;
  174. BOOST_ASSERT(position != a);
  175. BOOST_ASSERT(position != b);
  176. return f(position);
  177. };
  178. result_type Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
  179. if (L1)
  180. {
  181. *L1 *= diff;
  182. }
  183. return Q;
  184. }
  185. }
  186. return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
  187. }
  188. template<class Real, class Policy>
  189. template<class F>
  190. 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
  191. {
  192. BOOST_MATH_STD_USING
  193. using boost::math::constants::half;
  194. using boost::math::quadrature::detail::tanh_sinh_detail;
  195. static const char* function = "tanh_sinh<%1%>::integrate";
  196. if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
  197. {
  198. if (b <= a)
  199. {
  200. return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
  201. }
  202. auto u = [&](Real z, Real zc)->Real
  203. {
  204. if (z < 0)
  205. return f((a - b) * zc / 2 + a, (b - a) * zc / 2);
  206. else
  207. return f((a - b) * zc / 2 + b, (b - a) * zc / 2);
  208. };
  209. Real diff = (b - a)*half<Real>();
  210. Real left_min_complement = tools::min_value<Real>() * 4;
  211. Real right_min_complement = tools::min_value<Real>() * 4;
  212. Real Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
  213. if (L1)
  214. {
  215. *L1 *= diff;
  216. }
  217. return Q;
  218. }
  219. return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
  220. }
  221. template<class Real, class Policy>
  222. template<class F>
  223. 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
  224. {
  225. using boost::math::quadrature::detail::tanh_sinh_detail;
  226. static const char* function = "tanh_sinh<%1%>::integrate";
  227. Real min_complement = tools::epsilon<Real>();
  228. return m_imp->integrate([&](const Real& arg, const Real&) { return f(arg); }, error, L1, function, min_complement, min_complement, tolerance, levels);
  229. }
  230. template<class Real, class Policy>
  231. template<class F>
  232. 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
  233. {
  234. using boost::math::quadrature::detail::tanh_sinh_detail;
  235. static const char* function = "tanh_sinh<%1%>::integrate";
  236. Real min_complement = tools::min_value<Real>() * 4;
  237. return m_imp->integrate(f, error, L1, function, min_complement, min_complement, tolerance, levels);
  238. }
  239. }
  240. }
  241. }
  242. #endif