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