NumCpp  2.9.0
A Templatized Header Only C++ Implementation of the Python NumPy Library
simpson.hpp
Go to the documentation of this file.
1 #pragma once
33 
34 #include <functional>
35 
36 #include "NumCpp/Core/Types.hpp"
37 
38 namespace nc
39 {
40  namespace integrate
41  {
42  //============================================================================
43  // Method Description:
53  inline double simpson(const double low,
54  const double high,
55  const uint32 n,
56  const std::function<double(double)>& f) noexcept
57  {
58  const double width = (high - low) / static_cast<double>(n);
59 
60  double simpson_integral = 0.;
61  for (uint32 step = 0; step < n; ++step)
62  {
63  const double x1 = low + static_cast<double>(step) * width;
64  const double x2 = low + static_cast<double>(step + 1) * width;
65 
66  simpson_integral += (x2 - x1) / 6. * (f(x1) + 4. * f(0.5 * (x1 + x2)) + f(x2));
67  }
68 
69  return simpson_integral;
70  }
71  } // namespace integrate
72 } // namespace nc
double simpson(const double low, const double high, const uint32 n, const std::function< double(double)> &f) noexcept
Definition: simpson.hpp:53
dtype f(GeneratorType &generator, dtype inDofN, dtype inDofD)
Definition: f.hpp:58
Definition: Coordinate.hpp:45
std::uint32_t uint32
Definition: Types.hpp:40