17#ifdef NUMERICS_HAS_OMP
18 #pragma omp parallel for reduction(+:sum) schedule(static) if(backend == Backend::omp)
20 for (
idx i = 1;
i < n; ++
i)
22 return h * (0.5 * (
f(
a) +
f(
b)) +
sum);
27 throw std::invalid_argument(
"simpson: n must be even");
30#ifdef NUMERICS_HAS_OMP
31 #pragma omp parallel for reduction(+:sum) schedule(static) if(backend == Backend::omp)
33 for (
idx i = 1;
i < n; ++
i)
34 sum +=
f(
a +
i * h) * (
i % 2 == 0 ? 2.0 : 4.0);
40static constexpr real GL_NODES[5][5] = {
42 {-0.5773502691896257, 0.5773502691896257, 0, 0, 0},
43 {-0.7745966692414834, 0.0, 0.7745966692414834, 0, 0},
44 {-0.8611363115940526, -0.3399810435848563,
45 0.3399810435848563, 0.8611363115940526, 0},
46 {-0.9061798459386640, -0.5384693101056831, 0.0,
47 0.5384693101056831, 0.9061798459386640}
49static constexpr real GL_WEIGHTS[5][5] = {
52 {0.5555555555555556, 0.8888888888888889, 0.5555555555555556, 0, 0},
53 {0.3478548451374538, 0.6521451548625461,
54 0.6521451548625461, 0.3478548451374538, 0},
55 {0.2369268850561891, 0.4786286704993665, 0.5688888888888889,
56 0.4786286704993665, 0.2369268850561891}
61 throw std::invalid_argument(
"gauss_legendre: p must be 1..5");
65 for (
idx i = 0;
i < p; ++
i)
66 sum += GL_WEIGHTS[p - 1][
i] *
f(
mid +
half * GL_NODES[p - 1][
i]);
100 R[0][0] = 0.5 * (
b -
a) * (
f(
a) +
f(
b));
106 for (
idx k = 1;
k < n;
k += 2)
108 R[
i][0] = 0.5 * R[
i - 1][0] + h *
sum;
114 R[
i][
j] = R[
i][
j - 1] + (R[
i][
j - 1] - R[
i - 1][
j - 1]) / (factor - 1.0);
117 if (
i > 0 && std::abs(R[
i][
i] - R[
i - 1][
i - 1]) <
tol)
Common function type aliases for the analysis module.
real trapz(ScalarFn f, real a, real b, idx n=100, Backend backend=Backend::seq)
Trapezoidal rule with n panels.
Backend
Selects which backend handles a linalg operation.
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
real gauss_legendre(ScalarFn f, real a, real b, idx p=5)
Gauss-Legendre quadrature (exact for polynomials up to degree 2p-1)
real adaptive_simpson(ScalarFn f, real a, real b, real tol=1e-8, idx max_depth=50)
Adaptive Simpson quadrature.
real simpson(ScalarFn f, real a, real b, idx n=100, Backend backend=Backend::seq)
Simpson's 1/3 rule with n panels (n must be even)
std::function< real(real)> ScalarFn
Real-valued scalar function f(x)
real romberg(ScalarFn f, real a, real b, real tol=1e-10, idx max_levels=12)
Romberg integration (Richardson extrapolation on trapezoidal rule)
Numerical integration (quadrature) on [a, b].