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, but
dx can be used to specify a different constant spacing, and x can be
used to specify arbitrary spacing along dim. Only one of x or dx should be specified.
Assuming y is a one-dimensional tensor with elements ,
the default computation is
When dx is specified the computation becomes
effectively multiplying the result by dx. When x is specified,
assuming x is also a one-dimensional tensor with
elements , the computation becomes
When x and y 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 both x
and y, the function computes the difference between consecutive elements along
dimension dim. This effectively creates two tensors, x_diff and y_diff, that have
the same shape as the original tensors except their lengths along the dimension dim 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.
dx (float) – constant spacing between values. If neither x or dx
are specified then this defaults to 1. Effectively multiplies the result by its value.
dim () – The dimension along which to compute the trapezoidal rule. The last (inner-most) dimension by default.
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) /