exp_sinh.hpp 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 exp-sinh quadrature on half infinite intervals.
  8. *
  9. * References:
  10. *
  11. * 1) Tanaka, Ken'ichiro, et al. "Function classes for double exponential integration formulas." Numerische Mathematik 111.4 (2009): 631-655.
  12. */
  13. #ifndef BOOST_MATH_QUADRATURE_EXP_SINH_HPP
  14. #define BOOST_MATH_QUADRATURE_EXP_SINH_HPP
  15. #include <cmath>
  16. #include <limits>
  17. #include <memory>
  18. #include <boost/math/quadrature/detail/exp_sinh_detail.hpp>
  19. namespace boost{ namespace math{ namespace quadrature {
  20. template<class Real, class Policy = policies::policy<> >
  21. class exp_sinh
  22. {
  23. public:
  24. exp_sinh(size_t max_refinements = 9)
  25. : m_imp(std::make_shared<detail::exp_sinh_detail<Real, Policy>>(max_refinements)) {}
  26. template<class F>
  27. auto integrate(const F& f, Real a, Real b, Real tol = boost::math::tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr)->decltype(std::declval<F>()(std::declval<Real>())) const;
  28. template<class F>
  29. auto integrate(const F& f, Real tol = boost::math::tools::root_epsilon<Real>(), Real* error = nullptr, Real* L1 = nullptr, std::size_t* levels = nullptr)->decltype(std::declval<F>()(std::declval<Real>())) const;
  30. private:
  31. std::shared_ptr<detail::exp_sinh_detail<Real, Policy>> m_imp;
  32. };
  33. template<class Real, class Policy>
  34. template<class F>
  35. auto exp_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
  36. {
  37. typedef decltype(f(a)) K;
  38. using std::abs;
  39. using boost::math::constants::half;
  40. using boost::math::quadrature::detail::exp_sinh_detail;
  41. static const char* function = "boost::math::quadrature::exp_sinh<%1%>::integrate";
  42. // Neither limit may be a NaN:
  43. if((boost::math::isnan)(a) || (boost::math::isnan)(b))
  44. {
  45. return static_cast<K>(policies::raise_domain_error(function, "NaN supplied as one limit of integration - sorry I don't know what to do", a, Policy()));
  46. }
  47. // Right limit is infinite:
  48. if ((boost::math::isfinite)(a) && (b >= boost::math::tools::max_value<Real>()))
  49. {
  50. // If a = 0, don't use an additional level of indirection:
  51. if (a == (Real) 0)
  52. {
  53. return m_imp->integrate(f, error, L1, function, tolerance, levels);
  54. }
  55. const auto u = [&](Real t)->Real { return f(t + a); };
  56. return m_imp->integrate(u, error, L1, function, tolerance, levels);
  57. }
  58. if ((boost::math::isfinite)(b) && a <= -boost::math::tools::max_value<Real>())
  59. {
  60. const auto u = [&](Real t)->Real { return f(b-t);};
  61. return m_imp->integrate(u, error, L1, function, tolerance, levels);
  62. }
  63. // Infinite limits:
  64. if ((a <= -boost::math::tools::max_value<Real>()) && (b >= boost::math::tools::max_value<Real>()))
  65. {
  66. return static_cast<K>(policies::raise_domain_error(function, "Use sinh_sinh quadrature for integration over the whole real line; exp_sinh is for half infinite integrals.", a, Policy()));
  67. }
  68. // If we get to here then both ends must necessarily be finite:
  69. return static_cast<K>(policies::raise_domain_error(function, "Use tanh_sinh quadrature for integration over finite domains; exp_sinh is for half infinite integrals.", a, Policy()));
  70. }
  71. template<class Real, class Policy>
  72. template<class F>
  73. auto exp_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
  74. {
  75. static const char* function = "boost::math::quadrature::exp_sinh<%1%>::integrate";
  76. return m_imp->integrate(f, error, L1, function, tolerance, levels);
  77. }
  78. }}}
  79. #endif