mindspore.mint.diag
- mindspore.mint.diag(input, diagonal=0)[source]
If input is a vector (1-D tensor), then returns a 2-D square tensor with the elements of input as the diagonal.
If input is a matrix (2-D tensor), then returns a 1-D tensor with the diagonal elements of input.
The argument diagonal controls which diagonal to consider:
If diagonal = 0, it is the main diagonal.
If diagonal > 0, it is above the main diagonal.
If diagonal < 0, it is below the main diagonal.
Warning
This is an experimental API that is subject to change or deletion.
- Parameters
- Returns
Tensor, has the same dtype as the input, its shape is up to diagonal.
If input shape is \((x_0)\) : then output shape is \((x_0 + \left | diagonal \right | , x_0 + \left | diagonal \right | )\) 2-D tensor.
If input shape is \((x_0, x_1)\) : then output shape is main diagonal to move \((\left | diagonal \right |)\) elements remaining elements' length 1-D tensor.
- Raises
ValueError – If shape of input is not 1-D or 2-D.
- Supported Platforms:
Ascend
Examples
>>> import mindspore >>> input = mindspore.tensor([1,2,3,4]) >>> mindspore.mint.diag(input) Tensor(shape=[4, 4], dtype=Int64, value= [[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0], [0, 0, 0, 4]]) >>> mindspore.mint.diag(input, diagonal=1) Tensor(shape=[5, 5], dtype=Int64, value= [[0, 1, 0, 0, 0], [0, 0, 2, 0, 0], [0, 0, 0, 3, 0], [0, 0, 0, 0, 4], [0, 0, 0, 0, 0]]) >>> mindspore.mint.diag(input, diagonal=-1) Tensor(shape=[5, 5], dtype=Int64, value= [[0, 0, 0, 0, 0], [1, 0, 0, 0, 0], [0, 2, 0, 0, 0], [0, 0, 3, 0, 0], [0, 0, 0, 4, 0]]) >>> input = mindspore.tensor([[1,2],[3,4]]) >>> mindspore.mint.diag(input) Tensor(shape=[2], dtype=Int64, value= [1, 4]) >>> mindspore.mint.diag(input, diagonal=1) Tensor(shape=[1], dtype=Int64, value= [2]) >>> mindspore.mint.diag(input, diagonal=-1) Tensor(shape=[1], dtype=Int64, value= [3])