Shortcuts

torch.trapz

torch.trapz(y, x, *, dim=-1) → Tensor

Estimate ydx\int y\,dx along dim, using the trapezoid rule.

Parameters
  • y (Tensor) – The values of the function to integrate

  • x (Tensor) – The points at which the function y is sampled. If x is not in ascending order, intervals on which it is decreasing contribute negatively to the estimated integral (i.e., the convention abf=baf\int_a^b f = -\int_b^a f is followed).

  • dim (int) – The dimension along which to integrate. By default, use the last dimension.

Returns

A Tensor with the same shape as the input, except with dim removed. Each element of the returned tensor represents the estimated integral ydx\int y\,dx along dim.

Example:

>>> y = torch.randn((2, 3))
>>> y
tensor([[-2.1156,  0.6857, -0.2700],
        [-1.2145,  0.5540,  2.0431]])
>>> x = torch.tensor([[1, 3, 4], [1, 2, 3]])
>>> torch.trapz(y, x)
tensor([-1.2220,  0.9683])
torch.trapz(y, *, dx=1, dim=-1) → Tensor

As above, but the sample points are spaced uniformly at a distance of dx.

Parameters

y (Tensor) – The values of the function to integrate

Keyword Arguments
  • dx (float) – The distance between points at which y is sampled.

  • dim (int) – The dimension along which to integrate. By default, use the last dimension.

Returns

A Tensor with the same shape as the input, except with dim removed. Each element of the returned tensor represents the estimated integral ydx\int y\,dx along dim.

Docs

Access comprehensive developer documentation for PyTorch

View Docs

Tutorials

Get in-depth tutorials for beginners and advanced developers

View Tutorials

Resources

Find development resources and get your questions answered

View Resources