cumulative_trapezoid

paddle. cumulative_trapezoid ( y, x=None, dx=None, axis=- 1, name=None ) [源代码]

在指定维度上对输入实现 trapezoid rule 算法。与 trapezoid 的区别是,所用累积求和函数为 cumsum。

参数

  • y (Tensor) - 输入多维 Tensor,可选的数据类型为 float16、float32、float64。

  • x (Tensor,可选) - y 中数值对应的浮点数所组成的 Tensor,类型与 y 相同,形状与 y 的形状相匹配;若 x 有输入,已知 y 的尺寸为 [d_1, d_2, ... , d_n]axis=k,则 x 的尺寸只能为 [d_k][d_1, d_2, ... , d_n];如果 x 为 None,则假定采样点均匀分布 dx

  • dx (float,可选) - 相邻采样点之间的常数间隔;当 xdx 均未指定时,dx 默认为 1.0。

  • axis (int,可选) - 计算 trapezoid rule 时 y 的维度。默认值 -1。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

Tensor,按 trapezoidal rule 计算出 y 等于 N 维张量时的定积分,结果为 N 维张量。

代码示例

>>> import paddle

>>> y = paddle.to_tensor([4, 5, 6], dtype='float32')

>>> paddle.cumulative_trapezoid(y)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[4.50000000, 10.       ])

>>> paddle.cumulative_trapezoid(y, dx=2.)
>>> # Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
>>> #        [9. , 20.])

>>> y = paddle.to_tensor([4, 5, 6], dtype='float32')
>>> x = paddle.to_tensor([1, 2, 3], dtype='float32')

>>> paddle.cumulative_trapezoid(y, x)
Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
[4.50000000, 10.       ])

>>> y = paddle.to_tensor([1, 2, 3], dtype='float64')
>>> x = paddle.to_tensor([8, 6, 4], dtype='float64')

>>> paddle.cumulative_trapezoid(y, x)
Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
[-3., -8.])

>>> y = paddle.arange(6).reshape((2, 3)).astype('float32')

>>> paddle.cumulative_trapezoid(y, axis=0)
Tensor(shape=[1, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[1.50000000, 2.50000000, 3.50000000]])
>>> paddle.cumulative_trapezoid(y, axis=1)
Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.50000000, 2.        ],
 [3.50000000, 8.        ]])