scijit.integrate.trapezoid

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

Composite trapezoidal rule over samples.

Takes NO callback of either style: it integrates samples, not a function. For a function use fixed_quad() or quad().

Parameters:
yarray_like

Samples to integrate, of any rank. A list or a tuple is converted, an integer or boolean array is promoted to float64, and a complex one stays complex.

xarray_like or None, optional

Sample positions, which may be non-uniform. Either 1-D along axis, or of y’s shape. 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.

Returns:
totalfloat or ndarray

The integral. A scalar for a 1-D y, otherwise an array of y’s rank less one. Fewer than two samples gives 0.0.

Raises:
IndexError

axis outside y’s rank.

ValueError

The spacings and the sample pairs do not broadcast along axis, with numpy’s own message; x neither 1-D nor of y’s shape.

See also

scipy.integrate.trapezoid

The scipy routine this mirrors.

Notes

The number of spacings and the number of sample pairs do not have to agree. The spacings x[1:] - x[:-1] are multiplied against the pairs y[1:] + y[:-1] and broadcast, so either may be 1 and repeat against the other: len(x) == 2 with len(y) == 4 integrates with one repeated spacing, and len(y) == 1 with len(x) == 3 raises.

np.trapezoid computes the same value inside @njit but takes no axis argument.

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.trapezoid(np.sin(x), x)
>>> run(x)
1.9995983886400375