torch.trapezoid¶

torch.
trapezoid
(y, x=None, *, dx=None, dim= 1) → Tensor¶ Computes the trapezoidal rule along
dim
. By default the spacing between elements is assumed to be 1, butdx
can be used to specify a different constant spacing, andx
can be used to specify arbitrary spacing alongdim
.Assuming
y
is a onedimensional tensor with elements ${y_0, y_1, ..., y_n}$, the default computation is$\begin{aligned} \sum_{i = 1}^{n1} \frac{1}{2} (y_i + y_{i1}) \end{aligned}$When
dx
is specified the computation becomes$\begin{aligned} \sum_{i = 1}^{n1} \frac{\Delta x}{2} (y_i + y_{i1}) \end{aligned}$effectively multiplying the result by
dx
. Whenx
is specified, assumingx
is also a onedimensional tensor with elements ${x_0, x_1, ..., x_n}$, the computation becomes$\begin{aligned} \sum_{i = 1}^{n1} \frac{(x_i  x_{i1})}{2} (y_i + y_{i1}) \end{aligned}$When
x
andy
have the same size, the computation is as described above and no broadcasting is needed. The broadcasting behavior of this function is as follows when their sizes are different. For bothx
andy
, the function computes the difference between consecutive elements along dimensiondim
. This effectively creates two tensors, x_diff and y_diff, that have the same shape as the original tensors except their lengths along the dimensiondim
is reduced by 1. After that, those two tensors are broadcast together to compute final output as part of the trapezoidal rule. See the examples below for details.Note
The trapezoidal rule is a technique for approximating the definite integral of a function by averaging its left and right Riemann sums. The approximation becomes more accurate as the resolution of the partition increases.
 Parameters
 Keyword Arguments
Examples:
>>> # Computes the trapezoidal rule in 1D, spacing is implicitly 1 >>> y = torch.tensor([1, 5, 10]) >>> torch.trapezoid(y) tensor(10.5) >>> # Computes the same trapezoidal rule directly to verify >>> (1 + 10 + 10) / 2 10.5 >>> # Computes the trapezoidal rule in 1D with constant spacing of 2 >>> # NOTE: the result is the same as before, but multiplied by 2 >>> torch.trapezoid(y, dx=2) 21.0 >>> # Computes the trapezoidal rule in 1D with arbitrary spacing >>> x = torch.tensor([1, 3, 6]) >>> torch.trapezoid(y, x) 28.5 >>> # Computes the same trapezoidal rule directly to verify >>> ((3  1) * (1 + 5) + (6  3) * (5 + 10)) / 2 28.5 >>> # Computes the trapezoidal rule for each row of a 3x3 matrix >>> y = torch.arange(9).reshape(3, 3) tensor([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) >>> torch.trapezoid(y) tensor([ 2., 8., 14.]) >>> # Computes the trapezoidal rule for each column of the matrix >>> torch.trapezoid(y, dim=0) tensor([ 6., 8., 10.]) >>> # Computes the trapezoidal rule for each row of a 3x3 ones matrix >>> # with the same arbitrary spacing >>> y = torch.ones(3, 3) >>> x = torch.tensor([1, 3, 6]) >>> torch.trapezoid(y, x) array([5., 5., 5.]) >>> # Computes the trapezoidal rule for each row of a 3x3 ones matrix >>> # with different arbitrary spacing per row >>> y = torch.ones(3, 3) >>> x = torch.tensor([[1, 2, 3], [1, 3, 5], [1, 4, 7]]) >>> torch.trapezoid(y, x) array([2., 4., 6.])