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

131 lines
3.7 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. if (first == last)
  57. return output_type(0);
  58. output_type f_a = f(*first);
  59. InputIt second = first;
  60. ++second;
  61. if (second == last)
  62. return f_a;
  63. output_type f_c = f(*second);
  64. input_type h = *second - *first;
  65. output_type f_b = f(*first + h / input_type(2));
  66. output_type sum = ((f_a + f_b * input_type(4) + f_c) / input_type(6)) * h;
  67. for (first = second++; second != last; first = second++)
  68. {
  69. h = *second - *first;
  70. f_a = f_c;
  71. f_c = f(*second);
  72. f_b = f(*first + h / input_type(2));
  73. sum += ((f_a + f_b * input_type(4) + f_c) / input_type(6)) * h;
  74. }
  75. return sum;
  76. }
  77. template<class UnaryOp, class InputIt>
  78. typename std::invoke_result<UnaryOp, typename std::iterator_traits<InputIt>::value_type>::type
  79. trapezoid(UnaryOp f, InputIt first, InputIt last)
  80. {
  81. typedef typename std::iterator_traits<InputIt>::value_type input_type;
  82. typedef typename std::invoke_result<UnaryOp, input_type>::type output_type;
  83. if (first == last)
  84. return output_type(0);
  85. output_type f_a = f(*first);
  86. InputIt second = first;
  87. ++second;
  88. if (second == last)
  89. return f_a;
  90. output_type f_b = f(*second);
  91. output_type sum = ((f_a + f_b) / input_type(2)) * (*second - *first);
  92. for (first = second++; second != last; first = second++)
  93. {
  94. f_a = f_b;
  95. f_b = f(*second);
  96. sum += ((f_a + f_b) / input_type(2)) * (*second - *first);
  97. }
  98. return sum;
  99. }
  100. } // namespace quadrature
  101. } // namespace math
  102. #endif // ANTKEEPER_MATH_QUADRATURE_HPP