mindspore.ops.matrix_diag
- mindspore.ops.matrix_diag(x, k=0, num_rows=- 1, num_cols=- 1, padding_value=0, align='RIGHT_LEFT')[source]
Return a tensor with the contents in x as k[0]-th to k[1]-th diagonals of a matrix, with everything else padded with padding_value .
num_rows and num_cols are tensors type of int32 with only one value, which is -1, indicating that the innermost matrix of the output tensor is a square.
- Parameters
x (Tensor) – The input tensor.
k (Union[int, Tensor], optional) – Diagonal offsets. Positive value means superdiagonal, and negative value means subdiagonals. When k is a pair of integers specifying the low and high ends of a matrix band. Default
0
.num_rows (Union[int, Tensor], optional) – The number of rows of the output tensor. Default
-1
.num_cols (Union[int, Tensor], optional) – The number of columns of the output tensor. Default
-1
.padding_value (Union[int, float, Tensor], optional) – The number to fill the area outside the specified diagonal band. Default
0
.align (str, optional) –
specifies how superdiagonals and subdiagonals should be aligned. Supported values
"RIGHT_LEFT"
,"LEFT_RIGHT"
,"LEFT_LEFT"
,"RIGHT_RIGHT"
. Default"RIGHT_LEFT"
.When set to "RIGHT_LEFT", the alignment of superdiagonals will be towards the right side (padding the row on the left), while subdiagonals will be towards the left side (padding the row on the right)
When set to "LEFT_RIGHT", the alignment of superdiagonals will be towards the left side (padding the row on the right), while subdiagonals will be towards the right side (padding the row on the left)
When set to "LEFT_LEFT", the alignment of both superdiagonals and subdiagonals will be towards the left side(padding the row on the right).
When set to "RIGHT_RIGHT", the alignment of both superdiagonals and subdiagonals will be towards the right side(padding the row on the left).
- Returns
Tensor
- Supported Platforms:
Ascend
GPU
CPU
Examples
>>> import mindspore >>> x = mindspore.tensor([[8., 9., 0.], ... [1., 2., 3.], ... [0., 4., 5.]]) >>> k = mindspore.tensor([-1, 1], mindspore.int32) >>> padding_value = mindspore.tensor(11.) >>> num_rows = mindspore.tensor(3, mindspore.int32) >>> num_cols = mindspore.tensor(3, mindspore.int32) >>> output = mindspore.ops.matrix_diag(x, k, num_rows, num_cols, padding_value, align='LEFT_RIGHT') >>> print(output) [[ 1. 8. 11.] [ 4. 2. 9.] [11. 5. 3.]] >>> print(output.shape) (3, 3)