pflm.utils.trapz#
- trapz(y: ndarray, x: ndarray) ndarray | float[source][source]#
Compute the integrated area using the trapezoidal rule.
For a single curve
yof lengthnandxof the same length:\[T(y, x) = \sum_{i=0}^{n-2} (x_{i+1}-x_i) \, \frac{y_i + y_{i+1}}{2}.\]For a matrix
Yof shape(m, n)withlen(x) = n(integrate along axis 1):\[[T(Y, x)]_k = \sum_{i=0}^{n-2} (x_{i+1}-x_i) \, \frac{Y_{k,i} + Y_{k,i+1}}{2}, \quad k=0,\dots,m-1.\]If instead
len(x) = m(integrate along axis 0):\[[T(Y, x)]_k = \sum_{i=0}^{m-2} (x_{i+1}-x_i) \, \frac{Y_{i,k} + Y_{i+1,k}}{2}, \quad k=0,\dots,n-1.\]- Parameters:
- yarray_like
1D or 2D array of function values with respect to x.
- Accepted shapes:
(n,) for a single curve.
(m, n) for multiple curves.
If y is 1D, it is treated as a single row (1, n).
- xarray_like of shape (n,)
1D array of x-coordinates corresponding to the function values.
- Returns:
- np.ndarray or float
If y was 1D, returns a scalar float. If y was 2D, returns a 1D array of shape (m,) with the integral per row of y.
- Raises:
- ValueError
If the number of points in x does not match either the number of rows or the number of columns of y.
See also
numpy.trapzReference implementation for simple cases.
Notes
The implementation dispatches to a float32/float64 optimized backend.
x is coerced to the dtype of y to avoid unintended up/down-casts.