dpnp.triu_indices_from

dpnp.triu_indices_from(arr, k=0)[source]

Return the indices for the lower-triangle of arr.

For full documentation refer to numpy.triu_indices_from.

Parameters:
  • arr ({dpnp.ndarray, usm_ndarray}) -- The indices will be valid for square arrays whose dimensions are the same as arr.

  • k (int, optional) -- Diagonal offset (see dpnp.triu for details). Default: 0.

Returns:

inds -- The indices for the triangle. The returned tuple contains two arrays, each with the indices along one dimension of the array. Can be used to slice a ndarray of shape(n, n).

Return type:

tuple of dpnp.ndarray

See also

dpnp.triu_indices

Return the indices for the upper-triangle of an (n, m) array.

dpnp.triu

Return upper triangle of an array.

dpnp.tril_indices_from

similar function, for lower-triangular.

Examples

Create a 4 by 4 array.

>>> import dpnp as np
>>> a = np.arange(16).reshape(4, 4)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

Pass the array to get the indices of the upper triangular elements.

>>> triui = np.triu_indices_from(a)
>>> triui
(array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]),
 array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
>>> a[triui]
array([ 0,  1,  2,  3,  5,  6,  7, 10, 11, 15])

This is syntactic sugar for triu_indices().

>>> np.triu_indices(a.shape[0])
(array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]),
 array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))

Use the k parameter to return the indices for the upper triangular array from the k-th diagonal.

>>> triuim1 = np.triu_indices_from(a, k=1)
>>> a[triuim1]
array([ 1,  2,  3,  6,  7, 11])