mindspore.ops.MatrixDiagV3

class mindspore.ops.MatrixDiagV3(align='RIGHT_LEFT')[source]

Constructs a diagonal matrix or a batch of diagonal matrices from a given input Tensor.

Warning

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

Refer to mindspore.ops.matrix_diag() for more details.

Parameters

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).

Inputs:
  • x (Tensor) - The diagonal Tensor.

  • k (Union[int, Tensor], optional) - Diagonal offsets. A Tensor of type int32. Positive value means superdiagonal, 0 refers to the main diagonal, and negative value means subdiagonals. k can be a single integer (for a single diagonal) or a pair of integers specifying the low and high ends of a matrix band. k[0] must not be larger than k[1]. The value must be in the range of given or derivated num_rows and num_cols, meaning value of k must be in (-num_rows, num_cols). Default: 0.

  • num_rows (Union[int, Tensor], optional) - The number of rows of the output Tensor. A Tensor of type int32 with only one value. If num_rows is -1, indicating that the innermost matrix of the output Tensor is a square matrix, and the real number of rows will be derivated by other inputs. That is \(num\_rows = x.shape[-1] - min(k[1], 0)\). Otherwise, the value must be equal or greater than \(x.shape[-1] - min(k[1], 0)\). Default: -1.

  • num_cols (Union[int, Tensor], optional) - The number of columns of the output Tensor. A Tensor of type int32 with only one value. If num_cols is -1, indicating that the innermost matrix of the output Tensor is a square matrix, and the real number of columns will be derivated by other inputs. That is \(num\_cols = x.shape[-1] + max(k[0], 0)\). Otherwise, the value must be equal or greater than \(x.shape[-1] - min(k[1], 0)\). Default: -1.

  • padding_value (Union[int, float, Tensor], optional) - The number to fill the area outside the specified diagonal band. A Tensor with only one value. Have the same dtype as x. Default: 0.

Outputs:

A Tensor. Has the same type as x. Suppose x has r dimensions with shape \((I, J, ..., M, N)\) . The output Tensor has rank r + 1 with shape \((I, J, ..., M, num_rows, num_cols)\) when only one diagonal is given (k is an integer or k[0] == k[1]). Otherwise, it has rank r with shape \((I, J, ..., num_rows, num_cols)\) .

Supported Platforms:

Ascend GPU CPU

Examples

>>> x = Tensor(np.array([[8, 9, 0],
...                      [1, 2, 3],
...                      [0, 4, 5]]), mindspore.float32)
>>> k =Tensor(np.array([-1, 1]), mindspore.int32)
>>> num_rows = Tensor(np.array(3), mindspore.int32)
>>> num_cols = Tensor(np.array(3), mindspore.int32)
>>> padding_value = Tensor(np.array(11), mindspore.float32)
>>> matrix_diag_v3 = ops.MatrixDiagV3(align='LEFT_RIGHT')
>>> output = matrix_diag_v3(x, k, num_rows, num_cols, padding_value)
>>> print(output)
[[ 1.  8. 11.]
 [ 4.  2.  9.]
 [11.  5.  3.]]
>>> print(output.shape)
(3, 3)