triu_indices#

ivy.triu_indices(n_rows, n_cols=None, k=0, /, *, device=None)[source]#

Return the indices of the upper triangular part of a row by col matrix in a 2-by-N shape (tuple of two N dimensional arrays), where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns. The upper triangular part of the matrix is defined as the elements on and above the diagonal. The argument k controls which diagonal to consider. If k = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)} for i∈[0,min{n_rows, n_cols}−1].

Notes

Primary purpose of this function is to slice an array of shape (n,m). See https://numpy.org/doc/stable/reference/generated/numpy.triu_indices.html for examples

Tensorflow does not support slicing 2-D tensor with tuple of tensor of indices

Parameters:
  • n_rows (int) – number of rows in the 2-d matrix.

  • n_cols (Optional[int], default: None) – number of columns in the 2-d matrix. If None n_cols will be the same as n_rows

  • k (int, default: 0) – number of shifts from the main diagonal. k = 0 includes main diagonal, k > 0 moves upwards and k < 0 moves downwards

  • device (Optional[Union[Device, NativeDevice]], default: None) – device on which to place the created array. Default: None.

Return type:

Tuple[Array]

Returns:

  • ret – an 2xN shape, tuple of two N dimensional, where first subarray (i.e. ret[0]) contains row coordinates of all indices and the second subarray (i.e ret[1]) contains columns indices.

  • Function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

>>> x = ivy.triu_indices(4,4,0)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]),
ivy.array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
>>> x = ivy.triu_indices(4,4,1)
>>> print(x)
(ivy.array([0, 0, 0, 1, 1, 2]),
ivy.array([1, 2, 3, 2, 3, 3]))
>>> x = ivy.triu_indices(4,4,-2)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]),
ivy.array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3]))
>>> x = ivy.triu_indices(4,2,0)
>>> print(x)
(ivy.array([0, 0, 1]),
ivy.array([0, 1, 1]))
>>> x = ivy.triu_indices(2,4,0)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1]),
ivy.array([0, 1, 2, 3, 1, 2, 3]))
>>> x = ivy.triu_indices(4,-4,0)
>>> print(x)
(ivy.array([]), ivy.array([]))
>>> x = ivy.triu_indices(4,4,100)
>>> print(x)
(ivy.array([]), ivy.array([]))
>>> x = ivy.triu_indices(2,4,-100)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 1]), ivy.array([0, 1, 2, 3, 0, 1, 2, 3]))
Container.triu_indices(self, n_rows, n_cols=None, k=0, /, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, device=None, out=None)[source]#
Return type:

Container