trapezoidal.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright Nick Thompson, 2017
  3. * Use, modification and distribution are subject to the
  4. * Boost Software License, Version 1.0. (See accompanying file
  5. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. *
  7. * Use the adaptive trapezoidal rule to estimate the integral of periodic functions over a period,
  8. * or to integrate a function whose derivative vanishes at the endpoints.
  9. *
  10. * If your function does not satisfy these conditions, and instead is simply continuous and bounded
  11. * over the whole interval, then this routine will still converge, albeit slowly. However, there
  12. * are much more efficient methods in this case, including Romberg, Simpson, and double exponential quadrature.
  13. */
  14. #ifndef BOOST_MATH_QUADRATURE_TRAPEZOIDAL_HPP
  15. #define BOOST_MATH_QUADRATURE_TRAPEZOIDAL_HPP
  16. #include <cmath>
  17. #include <limits>
  18. #include <utility>
  19. #include <stdexcept>
  20. #include <boost/math/constants/constants.hpp>
  21. #include <boost/math/special_functions/fpclassify.hpp>
  22. #include <boost/math/policies/error_handling.hpp>
  23. #include <boost/math/tools/cxx03_warn.hpp>
  24. namespace boost{ namespace math{ namespace quadrature {
  25. template<class F, class Real, class Policy>
  26. auto trapezoidal(F f, Real a, Real b, Real tol, std::size_t max_refinements, Real* error_estimate, Real* L1, const Policy& pol)->decltype(std::declval<F>()(std::declval<Real>()))
  27. {
  28. static const char* function = "boost::math::quadrature::trapezoidal<%1%>(F, %1%, %1%, %1%)";
  29. using std::abs;
  30. using boost::math::constants::half;
  31. // In many math texts, K represents the field of real or complex numbers.
  32. // Too bad we can't put blackboard bold into C++ source!
  33. typedef decltype(f(a)) K;
  34. static_assert(!std::is_integral<K>::value,
  35. "The return type cannot be integral, it must be either a real or complex floating point type.");
  36. if (!(boost::math::isfinite)(a))
  37. {
  38. return static_cast<K>(boost::math::policies::raise_domain_error(function, "Left endpoint of integration must be finite for adaptive trapezoidal integration but got a = %1%.\n", a, pol));
  39. }
  40. if (!(boost::math::isfinite)(b))
  41. {
  42. return static_cast<K>(boost::math::policies::raise_domain_error(function, "Right endpoint of integration must be finite for adaptive trapezoidal integration but got b = %1%.\n", b, pol));
  43. }
  44. if (a == b)
  45. {
  46. return static_cast<K>(0);
  47. }
  48. if(a > b)
  49. {
  50. return -trapezoidal(f, b, a, tol, max_refinements, error_estimate, L1, pol);
  51. }
  52. K ya = f(a);
  53. K yb = f(b);
  54. Real h = (b - a)*half<Real>();
  55. K I0 = (ya + yb)*h;
  56. Real IL0 = (abs(ya) + abs(yb))*h;
  57. K yh = f(a + h);
  58. K I1;
  59. I1 = I0*half<Real>() + yh*h;
  60. Real IL1 = IL0*half<Real>() + abs(yh)*h;
  61. // The recursion is:
  62. // I_k = 1/2 I_{k-1} + 1/2^k \sum_{j=1; j odd, j < 2^k} f(a + j(b-a)/2^k)
  63. std::size_t k = 2;
  64. // We want to go through at least 5 levels so we have sampled the function at least 20 times.
  65. // Otherwise, we could terminate prematurely and miss essential features.
  66. // This is of course possible anyway, but 20 samples seems to be a reasonable compromise.
  67. Real error = abs(I0 - I1);
  68. // I take k < 5, rather than k < 4, or some other smaller minimum number,
  69. // because I hit a truly exceptional bug where the k = 2 and k =3 refinement were bitwise equal,
  70. // but the quadrature had not yet converged.
  71. while (k < 5 || (k < max_refinements && error > tol*IL1) )
  72. {
  73. I0 = I1;
  74. IL0 = IL1;
  75. I1 = I0*half<Real>();
  76. IL1 = IL0*half<Real>();
  77. std::size_t p = static_cast<std::size_t>(1u) << k;
  78. h *= half<Real>();
  79. K sum = 0;
  80. Real absum = 0;
  81. for(std::size_t j = 1; j < p; j += 2)
  82. {
  83. K y = f(a + j*h);
  84. sum += y;
  85. absum += abs(y);
  86. }
  87. I1 += sum*h;
  88. IL1 += absum*h;
  89. ++k;
  90. error = abs(I0 - I1);
  91. }
  92. if (error_estimate)
  93. {
  94. *error_estimate = error;
  95. }
  96. if (L1)
  97. {
  98. *L1 = IL1;
  99. }
  100. return static_cast<K>(I1);
  101. }
  102. template<class F, class Real>
  103. auto trapezoidal(F f, Real a, Real b, Real tol = boost::math::tools::root_epsilon<Real>(), std::size_t max_refinements = 12, Real* error_estimate = nullptr, Real* L1 = nullptr)->decltype(std::declval<F>()(std::declval<Real>()))
  104. {
  105. return trapezoidal(f, a, b, tol, max_refinements, error_estimate, L1, boost::math::policies::policy<>());
  106. }
  107. }}}
  108. #endif