tanh_sinh.hpp 11 KB

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