mindspore.ops.triu_indices

mindspore.ops.triu_indices(row, col, offset=0, *, dtype=mstype.int64)[source]

Calculates the indices of the upper triangular elements in a row * col matrix and returns them as a 2-by-N Tensor. The first row of the Tensor contains row coordinates, and the second row contains column coordinates. The coordinates are sorted by row and then by column.

The upper triangular part of the matrix consists of all elements on and above the diagonal.

Note

When running on CUDA, row * col must be less than 2^59 to prevent overflow during calculation.

Parameters
  • row (int) – number of rows in the 2-D matrix.

  • col (int) – number of columns in the 2-D matrix.

  • offset (int, optional) – diagonal offset from the main diagonal. Default: 0 .

Keyword Arguments

dtype (mindspore.dtype, optional) – The specified type of output tensor. An optional data type of mindspore.int32 and mindspore.int64. Default: mstype.int64.

Returns

  • y (Tensor) - indices of the elements in upper triangular part of matrix. The type is specified by dtype. The shape of output is \((2, triu\_size)\), where \(triu\_size\) is the number of elements in the upper triangular matrix.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import mindspore
>>> from mindspore import ops
>>> output = ops.triu_indices(4, 4, 2, dtype=mindspore.int64)
>>> print(output)
[[0 0 1]
 [2 3 3]]
>>> print(output.dtype)
Int64