💿🐜 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.

126 lines
3.4 KiB

  1. /*
  2. * Copyright (C) 2023 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. *
  32. * @return Approximated integral of @p f.
  33. *
  34. * @see https://en.wikipedia.org/wiki/Simpson%27s_rule
  35. */
  36. template<class UnaryOp, class InputIt>
  37. [[nodiscard]] typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  38. simpson(UnaryOp f, InputIt first, InputIt last)
  39. {
  40. typedef typename std::iterator_traits<InputIt>::value_type input_type;
  41. typedef typename std::invoke_result<UnaryOp, input_type>::type output_type;
  42. typedef decltype(*last - *first) difference_type;
  43. if (first == last)
  44. return output_type{0};
  45. output_type f_a = f(*first);
  46. InputIt second = first;
  47. ++second;
  48. if (second == last)
  49. return f_a;
  50. difference_type h = *second - *first;
  51. output_type f_b = f(*first + h / difference_type(2));
  52. output_type f_c = f(*second);
  53. output_type sum = (f_a + f_b * difference_type(4) + f_c) * h;
  54. for (first = second++; second != last; first = second++)
  55. {
  56. h = *second - *first;
  57. f_a = f_c;
  58. f_c = f(*second);
  59. f_b = f(*first + h / difference_type(2));
  60. sum += (f_a + f_b * difference_type(4) + f_c) * h;
  61. }
  62. return sum / difference_type(6);
  63. }
  64. /**
  65. * Approximates the definite integral of a function using the trapezoidal rule.
  66. *
  67. * @param f Unary function object to integrate.
  68. * @param first,last Range of sample points on `[first, last)`.
  69. *
  70. * @return Approximated integral of @p f.
  71. *
  72. * @see https://en.wikipedia.org/wiki/Trapezoidal_rule
  73. */
  74. template<class UnaryOp, class InputIt>
  75. [[nodiscard]] typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  76. trapezoid(UnaryOp f, InputIt first, InputIt last)
  77. {
  78. typedef typename std::iterator_traits<InputIt>::value_type input_type;
  79. typedef typename std::invoke_result<UnaryOp, input_type>::type output_type;
  80. typedef decltype(*last - *first) difference_type;
  81. if (first == last)
  82. return output_type{0};
  83. output_type f_a = f(*first);
  84. InputIt second = first;
  85. ++second;
  86. if (second == last)
  87. return f_a;
  88. output_type f_b = f(*second);
  89. output_type sum = (f_a + f_b) * (*second - *first);
  90. for (first = second++; second != last; first = second++)
  91. {
  92. f_a = f_b;
  93. f_b = f(*second);
  94. sum += (f_a + f_b) * (*second - *first);
  95. }
  96. return sum / difference_type(2);
  97. }
  98. } // namespace quadrature
  99. } // namespace math
  100. #endif // ANTKEEPER_MATH_QUADRATURE_HPP