bundles / scipy latest / scipy / integrate / _quadrature / cumulative_simpson
function
scipy.integrate._quadrature:cumulative_simpson
Signature
def cumulative_simpson ( y , * , x = None , dx = 1.0 , axis = -1 , initial = None ) Summary
Cumulatively integrate y(x) using the composite Simpson's 1/3 rule. The integral of the samples at every point is calculated by assuming a quadratic relationship between each point and the two adjacent points.
Parameters
y: array_likeValues to integrate. Requires at least one point along
axis. If two or fewer points are provided alongaxis, Simpson's integration is not possible and the result is calculated with cumulative_trapezoid.x: array_like, optionalThe coordinate to integrate along. Must have the same shape as
yor must be 1D with the same length asyalongaxis.xmust also be strictly increasing alongaxis. Ifxis None (default), integration is performed using spacingdxbetween consecutive elements iny.dx: scalar or array_like, optionalSpacing between elements of
y. Only used ifxis None. Can either be a float, or an array with the same shape asy, but of length one alongaxis. Default is 1.0.axis: int, optionalSpecifies the axis to integrate along. Default is -1 (last axis).
initial: scalar or array_like, optionalIf given, insert this value at the beginning of the returned result, and add it to the rest of the result. Default is None, which means no value at
x[0]is returned and res has one element less thanyalong the axis of integration. Can either be a float, or an array with the same shape asy, but of length one alongaxis.
Returns
res: ndarrayThe result of cumulative integration of
yalongaxis. Ifinitialis None, the shape is such that the axis of integration has one less value thany. Ifinitialis given, the shape is equal to that ofy.
Notes
The composite Simpson's 1/3 method can be used to approximate the definite integral of a sampled input function [1]. The method assumes a quadratic relationship over the interval containing any three consecutive sampled points.
Consider three consecutive points: .
Assuming a quadratic relationship over the three points, the integral over the subinterval between and is given by formula (8) of [2]:
The integral between and is given by swapping appearances of and . The integral is estimated separately for each subinterval and then cumulatively summed to obtain the final result.
For samples that are equally spaced, the result is exact if the function is a polynomial of order three or less [1] and the number of subintervals is even. Otherwise, the integral is exact for polynomials of order two or less.
Array API Standard Support
cumulative_simpson has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ✅ PyTorch ✅ ✅ JAX ⛔ ⛔ Dask ⚠️ computes graph n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
from scipy import integrate import numpy as np import matplotlib.pyplot as plt x = np.linspace(-2, 2, num=20) y = x**2 y_int = integrate.cumulative_simpson(y, x=x, initial=0) fig, ax = plt.subplots()✓
ax.plot(x, y_int, 'ro', x, x**3/3 - (x[0])**3/3, 'b-')
✗ax.grid() plt.show()✓

def cumulative_simpson_reference(y, x): return np.asarray([integrate.simpson(y[:i], x=x[:i]) for i in range(2, len(y) + 1)]) rng = np.random.default_rng(354673834679465) x, y = rng.random(size=(2, 10)) x.sort() res = integrate.cumulative_simpson(y, x=x) ref = cumulative_simpson_reference(y, x) equal = np.abs(res - ref) < 1e-15 equal # not equal when `simpson` has even number of subintervals✓
See also
- cumulative_trapezoid
cumulative integration using the composite trapezoidal rule
- numpy.cumsum
- simpson
integrator for sampled data using the Composite Simpson's Rule
Aliases
-
scipy.integrate.cumulative_simpson