scijit.integrate.cumulative_trapezoid

scijit.integrate.cumulative_trapezoid(y, x=None, dx=1.0, axis=-1, initial=None)

Running integral by the composite trapezoid rule.

Takes NO callback of either style: it integrates samples, not a function.

Parameters:
yarray_like

Samples to integrate, of any rank. An integer or boolean array is promoted to float64 and a complex one stays complex. Copied to a contiguous buffer, so strided views are safe.

xarray_like or None, optional

Sample positions, which may be non-uniform. Either 1-D along axis, or of y’s rank. None (the default) means equal spacing dx.

dxfloat, optional

Spacing used when x is None. Default 1.0.

axisint, optional

Axis to integrate along. Default -1, the last axis.

initialfloat or None, optional

Value prepended to the result along axis, which then has the same length there as y, and added to every element. None (the default) returns the n - 1 running integrals. Inside @njit the choice between None and a float fixes the length, so it must not be a variable that is sometimes one and sometimes the other.

Returns:
resndarray

Running integral, of y’s rank. Along axis its length is n - 1, the integral evaluated at the samples after the first, or n when a leading value is prepended.

Raises:
IndexError

axis outside y’s rank, from indexing y.shape.

ValueError

No samples along axis; x neither 1-D nor of y’s rank; x and y of different lengths along axis; initial neither None nor a scalar.

See also

scipy.integrate.cumulative_trapezoid

The scipy routine this mirrors.

Notes

A non-zero initial is accepted, prepended and added to every element. scipy.integrate.cumulative_trapezoid raises ValueError: `initial` must be `None` or `0`. and accepts a non-zero initial only on cumulative_simpson.

Pure @njit, no state, so prange-safe.

Examples

>>> import numpy as np
>>> from numba import njit
>>> import scijit.integrate as si
>>> x = np.linspace(0.0, np.pi, 65)
>>> @njit
... def run(x):
...     return si.cumulative_trapezoid(np.sin(x), x, 1.0, -1, 0.0)[-1]
>>> run(x)
1.9995983886400375

A 2-D y, integrated down the rows:

>>> si.cumulative_trapezoid(np.arange(6.0).reshape(2, 3), None, 1.0, 0)
array([[1.5, 2.5, 3.5]])