💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.8 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_MATH_QUADRATURE_HPP
  20. #define ANTKEEPER_MATH_QUADRATURE_HPP
  21. #include <iterator>
  22. #include <type_traits>
  23. namespace math {
  24. /// Numerical integration functions.
  25. namespace quadrature {
  26. /**
  27. * Approximates the definite integral of a function using Simpson's 1/3 rule.
  28. *
  29. * @param f Unary function object to integrate.
  30. * @param first,last Range of sample points on `[first, last)`.
  31. * @return Approximated integral of @p f.
  32. *
  33. * @see https://en.wikipedia.org/wiki/Simpson%27s_rule
  34. */
  35. template<class UnaryOp, class InputIt>
  36. typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  37. simpson(UnaryOp f, InputIt first, InputIt last);
  38. /**
  39. * Approximates the definite integral of a function using the trapezoidal rule.
  40. *
  41. * @param f Unary function object to integrate.
  42. * @param first,last Range of sample points on `[first, last)`.
  43. * @return Approximated integral of @p f.
  44. *
  45. * @see https://en.wikipedia.org/wiki/Trapezoidal_rule
  46. */
  47. template<class UnaryOp, class InputIt>
  48. typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  49. trapezoid(UnaryOp f, InputIt first, InputIt last);
  50. template<class UnaryOp, class InputIt>
  51. typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  52. simpson(UnaryOp f, InputIt first, InputIt last)
  53. {
  54. typedef typename std::iterator_traits<InputIt>::value_type input_type;
  55. typedef typename std::invoke_result<UnaryOp, input_type>::type output_type;
  56. typedef decltype(*last - *first) difference_type;
  57. if (first == last)
  58. return output_type{0};
  59. output_type f_a = f(*first);
  60. InputIt second = first;
  61. ++second;
  62. if (second == last)
  63. return f_a;
  64. difference_type h = *second - *first;
  65. output_type f_b = f(*first + h / difference_type(2));
  66. output_type f_c = f(*second);
  67. output_type sum = (f_a + f_b * difference_type(4) + f_c) * h;
  68. for (first = second++; second != last; first = second++)
  69. {
  70. h = *second - *first;
  71. f_a = f_c;
  72. f_c = f(*second);
  73. f_b = f(*first + h / difference_type(2));
  74. sum += (f_a + f_b * difference_type(4) + f_c) * h;
  75. }
  76. return sum / difference_type(6);
  77. }
  78. template<class UnaryOp, class InputIt>
  79. typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  80. trapezoid(UnaryOp f, InputIt first, InputIt last)
  81. {
  82. typedef typename std::iterator_traits<InputIt>::value_type input_type;
  83. typedef typename std::invoke_result<UnaryOp, input_type>::type output_type;
  84. typedef decltype(*last - *first) difference_type;
  85. if (first == last)
  86. return output_type{0};
  87. output_type f_a = f(*first);
  88. InputIt second = first;
  89. ++second;
  90. if (second == last)
  91. return f_a;
  92. output_type f_b = f(*second);
  93. output_type sum = (f_a + f_b) * (*second - *first);
  94. for (first = second++; second != last; first = second++)
  95. {
  96. f_a = f_b;
  97. f_b = f(*second);
  98. sum += (f_a + f_b) * (*second - *first);
  99. }
  100. return sum / difference_type(2);
  101. }
  102. } // namespace quadrature
  103. } // namespace math
  104. #endif // ANTKEEPER_MATH_QUADRATURE_HPP