NumCpp  1.0
A C++ implementation of the Python Numpy library
trapazoidal.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 trapazoidal(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 trapezoidal_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  trapezoidal_integral += 0.5 * (x2 - x1) * (f(x1) + f(x2));
66  }
67 
68  return trapezoidal_integral;
69  }
70  }
71 }
nc::uint32
std::uint32_t uint32
Definition: Types.hpp:41
nc
Definition: Coordinate.hpp:45
nc::integrate::trapazoidal
double trapazoidal(const double low, const double high, const uint32 n, const std::function< double(double)> &f) noexcept
Definition: trapazoidal.hpp:54
Types.hpp
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition: f.hpp:58