paddle. trapezoid ( y, x=None, dx=None, axis=- 1, name=None ) [source]

Integrate along the given axis using the composite trapezoidal rule. Use the sum method.

  • y (Tensor) – Input tensor to integrate. It’s data type should be float16, float32, float64.

  • x (Tensor, optional) – The sample points corresponding to the y values, the same type as y. It is known that the size of y is [d_1, d_2, … , d_n] and \(axis=k\), then the size of x can only be [d_k] or [d_1, d_2, … , d_n ]. If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.

  • dx (float, optional) – The spacing between sample points when x is None. If neither x nor dx is provided then the default is \(dx = 1\).

  • axis (int, optional) – The axis along which to integrate. The default is -1.

  • name (str, optional) – Name for the operation (optional, default is None). For more information, please refer to Name.


Tensor, Definite integral of y is N-D tensor as approximated along a single axis by the trapezoidal rule. If y is a 1D tensor, then the result is a float. If N is greater than 1, then the result is an (N-1)-D tensor.


>>> import paddle

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

>>> paddle.trapezoid(y)
Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,

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

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

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

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

>>> paddle.trapezoid(y, x)
Tensor(shape=[], dtype=float64, place=Place(cpu), stop_gradient=True,
>>> y = paddle.arange(6).reshape((2, 3)).astype('float32')

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