PyTorch torch.trapz function
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
y: The function values to integratex: The values of the integration variable (optional)dx: The sampling interval (used when x is not provided)
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
)
(
"Trapezoidal integration result (trapz):"
,
result
)
Equivalent to trapezoid
result2
=
torch.
trapezoid
(
y
,
dx
=
1.0
)
(
"Trapezoidal integration result (trapezoid):"
,
result2
)
The output is:
梯形积分结果 (trapz): tensor(7.5000)
梯形积分结果 (trapezoid): tensor(7.5000)