torch.cumulative_trapezoid
- torch.cumulative_trapezoid(y, x=None, *, dx=None, dim=-1) → Tensor
Cumulatively 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.
For more details, please read torch.trapezoid(). The difference between torch.trapezoid()
and this function is that, torch.trapezoid() returns a value for each integration,
where as this function returns a cumulative value for every spacing within the integration. This
is analogous to how .sum returns a value and .cumsum returns a cumulative sum.
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:
>>> # Cumulatively computes the trapezoidal rule in 1D, spacing is implicitly 1.
>>> y = torch.tensor([1, 5, 10])
>>> torch.cumulative_trapezoid(y)
tensor([3., 10.5])
>>> # Computes the same trapezoidal rule directly up to each element to verify
>>> (1 + 5) / 2
3.0