mindspore.ops.Trace

class mindspore.ops.Trace[source]

Returns a new tensor that is the sum of the input trace.

Note

Input must be matrix, and complex number is not supported at present.

Warning

This is an experimental API that is subject to change or deletion.

Inputs:
  • x (Tensor) - A matrix to be calculated. The matrix must be two dimensional.

Outputs:

Tensor, 0D Tensor with 1 element, it has the same data type as input x.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), mindspore.float32)
>>> trace = ops.Trace()
>>> output = trace(x)
>>> print(output)
15.0
>>> x = Tensor(np.arange(1, 13).reshape(3, 4), mindspore.float32)
>>> trace = ops.Trace()
>>> output = trace(x)
>>> print(output)
18.0
>>> x = Tensor(np.arange(12, 0, -1).reshape(4, 3), mindspore.float32)
>>> trace = ops.Trace()
>>> output = trace(x)
>>> print(output)
24.0