PyTorch torch.cumulative_trapezoid Function


Pytorch torch 参考手册PyTorch torch Reference Manual

torch.cumulative_trapezoidIt is a function in PyTorch used to compute the cumulative trapezoidal integral of a function. It uses the trapezoidal rule to calculate 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 size is used.
  • dx(float, optional): The step size between x coordinates. Default is 1.0.
  • dim(int, optional): The dimension along which to integrate. Default is -1.

Return Value:

  • torch.Tensor: Returns the cumulative integral result.

Usage Example

Example

import torch

# Create function values
y = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])

# Cumulative trapezoidal integral
result = torch.cumulative_trapezoid(y)

print("Function values y:", y)
print("Cumulative integral 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)
print("Cumulative integral result:", result)

Pytorch torch 参考手册PyTorch torch Reference Manual

Other Extensions