mindspore.ops.trapz

mindspore.ops.trapz(y, x=None, *, dx=1.0, dim=- 1)[source]

Integrates y(x) along given dim using trapezoidal rule. By default x-dim distances between points will be 1.0, alternatively they can be provided with x array or with dx scalar.

\[\mathop{ \int }\nolimits_{{}}^{{}}{y}{ \left( {x} \right) } \text{d} x\]
Parameters
  • y (Tensor) – Input tensor to integrate.

  • x (Tensor, optional) – The sample points corresponding to the y values. If x is None, the sample points are assumed to be evenly spaced dx apart. Default: None . If x is not None, after subtracting 1 from the axis specified by dim, the shape of x should be same as y or can broadcast to y.

Keyword Arguments
  • dx (float, optional) – The spacing between sample points when x is None. If x is specified, dx does not take effect. Default: 1.0 .

  • dim (int, optional) – The dim along which to integrate. Default: -1 .

Returns

Tensor of float, definite integral as approximated by trapezoidal rule. If y is a one-dimensional array, the result is a floating-point number. If y is an n-dimensional array, the result is an N-1 dimensional array.

Raises
  • RuntimeError – If dim of x is 1, and x.shape[0] is not equal to y.shape[dim].

  • ValueError – If dim is out of range of \([-y.ndim, y.ndim)\).

  • TypeError – If y is not a Tensor.

  • TypeError – If x is not None and is not a Tensor.

  • TypeError – If dx is not a float number.

  • TypeError – If dim is not a Integer.

Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> y = Tensor(np.array([[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]).astype(np.float32))
>>> x = Tensor(np.array([[1, 2, 3], [1, 3, 5], [1, 4, 7]]).astype(np.float32))
>>> output = ops.trapz(y, x)
>>> print(output)
[2. 4. 6.]