mindspore.ops.diag

mindspore.ops.diag(input)[source]

Constructs a diagonal tensor with a given diagonal values.

Assume input has dimensions (D1,...Dk) , the output is a tensor of rank 2k with dimensions (D1,...,Dk,D1,...,Dk) where: output[i1,...,ik,i1,...,ik]=input[i1,...,ik] and 0 everywhere else.

Warning

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

Parameters

input (Tensor) – The input tensor.

Returns

Tensor, has the same dtype as the input.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> from mindspore import Tensor, ops
>>> input = Tensor([1, 2, 3, 4]).astype('int32')
>>> output = ops.diag(input)
>>> print(output)
[[1 0 0 0]
 [0 2 0 0]
 [0 0 3 0]
 [0 0 0 4]]