Rate this Page

torch.trapezoid#

torch.trapezoid(y,x=None,*,dx=None,dim=-1)Tensor#

Computes thetrapezoidal rule alongdim. By default the spacing between elements is assumed to be 1, butdx can be used to specify a different constant spacing, andx can beused to specify arbitrary spacing alongdim. Only one ofx ordx should be specified.

Assumingy is a one-dimensional tensor with elementsy0,y1,...,yn{y_0, y_1, ..., y_n},the default computation is

i=1n12(yi+yi1)\begin{aligned} \sum_{i = 1}^{n} \frac{1}{2} (y_i + y_{i-1})\end{aligned}

Whendx is specified the computation becomes

i=1nΔx2(yi+yi1)\begin{aligned} \sum_{i = 1}^{n} \frac{\Delta x}{2} (y_i + y_{i-1})\end{aligned}

effectively multiplying the result bydx. Whenx is specified,assumingx is also a one-dimensional tensor withelementsx0,x1,...,xn{x_0, x_1, ..., x_n}, the computation becomes

i=1n(xixi1)2(yi+yi1)\begin{aligned} \sum_{i = 1}^{n} \frac{(x_i - x_{i-1})}{2} (y_i + y_{i-1})\end{aligned}

Whenx 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 bothxandy, the function computes the difference between consecutive elements alongdimensiondim. This effectively creates two tensors,x_diff andy_diff, that havethe 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 functionby averaging its left and right Riemann sums. The approximation becomes more accurate asthe resolution of the partition increases.

Parameters
  • y (Tensor) – Values to use when computing the trapezoidal rule.

  • x (Tensor) – If specified, defines spacing between values as specified above.

Keyword Arguments
  • dx (float) – constant spacing between values. If neitherx ordxare specified then this defaults to 1. Effectively multiplies the result by its value.

  • dim (int) – 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)/210.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))/228.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.])