PyTorch torch.cumulative_trapezoid function
PyTorch torch reference manual
torch.cumulative_trapezoid is a PyTorch function for computing the cumulative trapezoidal integral of a function. It uses the trapezoidal rule to compute the cumulative integral of the function at given points.
Function definition
torch.cumulative_trapezoid(y, x=None, dx=1.0, dim=-1)
Parameters:
y(Tensor): The function values to be integrated.x(Tensor, optional): The x coordinates corresponding to the function. If None, the dx step is used.dx(float, optional): The step between x coordinates. Defaults to 1.0.dim(int, optional): The dimension along which to integrate. Defaults to -1.
Return Value:
torch.Tensor: Returns the cumulative integration result.
Usage examples
Example
import
torch
Create function values
y
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
,
4.0
,
5.0
]
)
Cumulative trapezoidal integration
result
=
torch.
cumulative_trapezoid
(
y
)
(
"Function values y:"
,
y
)
(
"Cumulative integration result:"
,
result
)
The output is:
函数值 y: tensor([1., 2., 3., 4., 5.])
累积积分结果: tensor([0.0000, 1.5000, 3.5000, 6.0000, 9.0000])
Example - Specifying x coordinates
import
torch
y
=
torch.
tensor
(
[
1.0
,
2.0
,
3.0
,
4.0
,
5.0
]
)
x
=
torch.
tensor
(
[
0.0
,
0.5
,
1.0
,
1.5
,
2.0
]
)
result
=
torch.
cumulative_trapezoid
(
y
,
x
)
(
"Cumulative integration result:"
,
result
)