tanh_sinh.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. if (error)
  103. {
  104. *error *= 2;
  105. }
  106. return Q;
  107. }
  108. if ((boost::math::isfinite)(b) && (a <= -tools::max_value<Real>()))
  109. {
  110. auto v = [&](const Real& t, const Real& tc)->result_type
  111. {
  112. Real z;
  113. if (t > -0.5)
  114. z = 1 / (t + 1);
  115. else
  116. z = -1 / tc;
  117. Real arg;
  118. if (t < 0.5)
  119. arg = 2 * z - 1;
  120. else
  121. arg = tc / (2 - tc);
  122. return f(b - arg) * z * z;
  123. };
  124. Real left_limit = sqrt(tools::min_value<Real>()) * 4;
  125. result_type Q = Real(2) * m_imp->integrate(v, error, L1, function, left_limit, tools::min_value<Real>(), tolerance, levels);
  126. if (L1)
  127. {
  128. *L1 *= 2;
  129. }
  130. if (error)
  131. {
  132. *error *= 2;
  133. }
  134. return Q;
  135. }
  136. if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
  137. {
  138. if (a == b)
  139. {
  140. return result_type(0);
  141. }
  142. if (b < a)
  143. {
  144. return -this->integrate(f, b, a, tolerance, error, L1, levels);
  145. }
  146. Real avg = (a + b)*half<Real>();
  147. Real diff = (b - a)*half<Real>();
  148. Real avg_over_diff_m1 = a / diff;
  149. Real avg_over_diff_p1 = b / diff;
  150. bool have_small_left = fabs(a) < 0.5f;
  151. bool have_small_right = fabs(b) < 0.5f;
  152. Real left_min_complement = float_next(avg_over_diff_m1) - avg_over_diff_m1;
  153. Real min_complement_limit = (std::max)(tools::min_value<Real>(), Real(tools::min_value<Real>() / diff));
  154. if (left_min_complement < min_complement_limit)
  155. left_min_complement = min_complement_limit;
  156. Real right_min_complement = avg_over_diff_p1 - float_prior(avg_over_diff_p1);
  157. if (right_min_complement < min_complement_limit)
  158. right_min_complement = min_complement_limit;
  159. //
  160. // These asserts will fail only if rounding errors on
  161. // type Real have accumulated so much error that it's
  162. // broken our internal logic. Should that prove to be
  163. // a persistent issue, we might need to add a bit of fudge
  164. // factor to move left_min_complement and right_min_complement
  165. // further from the end points of the range.
  166. //
  167. BOOST_ASSERT((left_min_complement * diff + a) > a);
  168. BOOST_ASSERT((b - right_min_complement * diff) < b);
  169. auto u = [&](Real z, Real zc)->result_type
  170. {
  171. Real position;
  172. if (z < -0.5)
  173. {
  174. if(have_small_left)
  175. return f(diff * (avg_over_diff_m1 - zc));
  176. position = a - diff * zc;
  177. }
  178. else if (z > 0.5)
  179. {
  180. if(have_small_right)
  181. return f(diff * (avg_over_diff_p1 - zc));
  182. position = b - diff * zc;
  183. }
  184. else
  185. position = avg + diff*z;
  186. BOOST_ASSERT(position != a);
  187. BOOST_ASSERT(position != b);
  188. return f(position);
  189. };
  190. result_type Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
  191. if (L1)
  192. {
  193. *L1 *= diff;
  194. }
  195. if (error)
  196. {
  197. *error *= diff;
  198. }
  199. return Q;
  200. }
  201. }
  202. return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
  203. }
  204. template<class Real, class Policy>
  205. template<class F>
  206. 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
  207. {
  208. BOOST_MATH_STD_USING
  209. using boost::math::constants::half;
  210. using boost::math::quadrature::detail::tanh_sinh_detail;
  211. static const char* function = "tanh_sinh<%1%>::integrate";
  212. if ((boost::math::isfinite)(a) && (boost::math::isfinite)(b))
  213. {
  214. if (b <= a)
  215. {
  216. return policies::raise_domain_error(function, "Arguments to integrate are in wrong order; integration over [a,b] must have b > a.", a, Policy());
  217. }
  218. auto u = [&](Real z, Real zc)->Real
  219. {
  220. if (z < 0)
  221. return f((a - b) * zc / 2 + a, (b - a) * zc / 2);
  222. else
  223. return f((a - b) * zc / 2 + b, (b - a) * zc / 2);
  224. };
  225. Real diff = (b - a)*half<Real>();
  226. Real left_min_complement = tools::min_value<Real>() * 4;
  227. Real right_min_complement = tools::min_value<Real>() * 4;
  228. Real Q = diff*m_imp->integrate(u, error, L1, function, left_min_complement, right_min_complement, tolerance, levels);
  229. if (L1)
  230. {
  231. *L1 *= diff;
  232. }
  233. if (error)
  234. {
  235. *error *= diff;
  236. }
  237. return Q;
  238. }
  239. return policies::raise_domain_error(function, "The domain of integration is not sensible; please check the bounds.", a, Policy());
  240. }
  241. template<class Real, class Policy>
  242. template<class F>
  243. 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
  244. {
  245. using boost::math::quadrature::detail::tanh_sinh_detail;
  246. static const char* function = "tanh_sinh<%1%>::integrate";
  247. Real min_complement = tools::epsilon<Real>();
  248. return m_imp->integrate([&](const Real& arg, const Real&) { return f(arg); }, error, L1, function, min_complement, min_complement, tolerance, levels);
  249. }
  250. template<class Real, class Policy>
  251. template<class F>
  252. 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
  253. {
  254. using boost::math::quadrature::detail::tanh_sinh_detail;
  255. static const char* function = "tanh_sinh<%1%>::integrate";
  256. Real min_complement = tools::min_value<Real>() * 4;
  257. return m_imp->integrate(f, error, L1, function, min_complement, min_complement, tolerance, levels);
  258. }
  259. }
  260. }
  261. }
  262. #endif