NumCpp
2.4.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 "
NumCpp/Core/Types.hpp
"
35
36
#include <functional>
37
38
namespace
nc
39
{
40
namespace
integrate
41
{
42
//============================================================================
43
// Method Description:
53
inline
double
simpson
(
const
double
low,
const
double
high,
const
uint32
n,
54
const
std::function<
double
(
double
)>&
f
) noexcept
55
{
56
const
double
width = (high - low) /
static_cast<
double
>
(n);
57
58
double
simpson_integral = 0.0;
59
for
(
uint32
step = 0; step < n; ++step)
60
{
61
const
double
x1 = low +
static_cast<
double
>
(step) * width;
62
const
double
x2 = low +
static_cast<
double
>
(step + 1) * width;
63
64
simpson_integral += (x2 - x1) / 6.0 * (
f
(x1) + 4.0 *
f
(0.5 * (x1 + x2)) +
f
(x2));
65
}
66
67
return
simpson_integral;
68
}
69
}
// namespace integrate
70
}
// namespace nc
nc::uint32
std::uint32_t uint32
Definition:
Types.hpp:40
nc
Definition:
Coordinate.hpp:44
Types.hpp
nc::random::f
dtype f(dtype inDofN, dtype inDofD)
Definition:
f.hpp:56
nc::integrate::simpson
double simpson(const double low, const double high, const uint32 n, const std::function< double(double)> &f) noexcept
Definition:
simpson.hpp:53
include
NumCpp
Integrate
simpson.hpp
Generated by
1.8.17