mindspore.ops.MatrixDiagPartV3

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

Returns the diagonal part of a tensor.

Warning

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

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

Parameters

align (str, optional) – An optional string from: “RIGHT_LEFT”(default), “LEFT_RIGHT”, “LEFT_LEFT”, “RIGHT_RIGHT”. It specifies how superdiagonals and subdiagonals should be aligned, respectively. “RIGHT_LEFT” aligns superdiagonals to the right (left-pads the row) and subdiagonals to the left (right-pads the row).

Inputs:
  • x (Tensor) - Rank r, where r >= 2.

  • k (Tensor) - A Tensor of type int32. Diagonal offset(s). 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 of k has restructions, meaning value of k must be in (-x.shape[-2], x.shape[-1]).

  • padding_value (Tensor) - A Tensor. Have the same dtype as x. The number to fill the area outside the specified diagonal band with. There must be only one value.

Outputs:

A Tensor. Has the same type as x. Assume x has r dimensions \((I, J, ..., M, N)\) . Let max_diag_len be the maximum length among all diagonals to be extracted, \(max\_diag\_len = min(M + min(k[1], 0), N + min(-k[0], 0))\) Let num_diags be the number of diagonals to extract, \(num\_diags = k[1] - k[0] + 1\). If \(num\_diags == 1\), the output tensor is of rank r - 1 with shape \((I, J, ..., L, max\_diag\_len)\) Otherwise, the output tensor has rank r with dimensions \((I, J, ..., L, num\_diags, max\_diag\_len)\) .

Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> x = Tensor(np.array([[1, 2, 3, 4],
...                      [5, 6, 7, 8],
...                      [9, 8, 7, 6]]), mindspore.float32)
>>> k =Tensor(np.array([1, 3]), mindspore.int32)
>>> padding_value = Tensor(np.array(9), mindspore.float32)
>>> matrix_diag_part_v3 = ops.MatrixDiagPartV3(align='RIGHT_LEFT')
>>> output = matrix_diag_part_v3(x, k, padding_value)
>>> print(output)
[[9. 9. 4.]
 [9. 3. 8.]
 [2. 7. 6.]]
>>> print(output.shape)
(3, 3)