Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PyTorch torch.trapz function

PyTorch torch.trapz function


PyTorch torch reference manual PyTorch torch reference manual

torch.trapz is a function in PyTorch used to compute the trapezoidal integral; it is an torch.trapezoid alias of the function, and both have the same functionality.

Function definition

torch.trapz(y, x, dx)

Parameter Description


Usage examples

Example

import

torch

Trapezoidal integration using dx

y

=

torch.

tensor

(

[

1.0

,

2.0

,

3.0

,

4.0

]

)

dx=1.0 means a uniform sampling interval

result

=

torch.

trapz

(

y

,

dx

=

1.0

)

print

(

"Trapezoidal integration result (trapz):"

,

result

)

Equivalent to trapezoid

result2

=

torch.

trapezoid

(

y

,

dx

=

1.0

)

print

(

"Trapezoidal integration result (trapezoid):"

,

result2

)

The output is:

梯形积分结果 (trapz): tensor(7.5000)

梯形积分结果 (trapezoid): tensor(7.5000)


PyTorch torch reference manual PyTorch torch reference manual