dpnp.triu_indices

dpnp.triu_indices(n, k=0, m=None, device=None, usm_type='device', sycl_queue=None)[source]

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

For full documentation refer to numpy.triu_indices.

Parameters:
nint

The size of the arrays for which the returned indices will be valid.

kint, optional

Diagonal offset (see dpnp.triu for details).

Default: 0.

mint, optional

The column dimension of the arrays for which the returned arrays will be valid. By default m is taken equal to n.

Default: None.

device{None, string, SyclDevice, SyclQueue, Device}, optional

An array API concept of device where the output array is created. device can be None, a oneAPI filter selector string, an instance of dpctl.SyclDevice corresponding to a non-partitioned SYCL device, an instance of dpctl.SyclQueue, or a dpnp.tensor.Device object returned by dpnp.ndarray.device.

Default: None.

usm_type{"device", "shared", "host"}, optional

The type of SYCL USM allocation for the output array.

Default: None.

sycl_queue{None, SyclQueue}, optional

A SYCL queue to use for output array allocation and copying. The sycl_queue can be passed as None (the default), which means to get the SYCL queue from device keyword if present or to use a default queue.

Default: None.

Returns:
indstuple of dpnp.ndarray

The row and column indices, respectively. The row indices are sorted in non-decreasing order, and the corresponding column indices are strictly increasing for each row.

See also

dpnp.tril_indices

similar function, for lower-triangular.

dpnp.mask_indices

generic function accepting an arbitrary mask function.

dpnp.tril

Return lower triangle of an array.

dpnp.triu

Return upper triangle of an array.

Examples

Compute two different sets of indices to access 4x4 arrays, one for the upper triangular part starting at the main diagonal, and one starting two diagonals further right:

>>> import dpnp as np
>>> iu1 = np.triu_indices(4)
>>> iu1
(array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]),
 array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))

Note that row indices (first array) are non-decreasing, and the corresponding column indices (second array) are strictly increasing for each row.

Here is how they can be used with a sample array:

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

Both for indexing:

>>> a[iu1]
array([ 0,  1,  2, ..., 10, 11, 15])

And for assigning values:

>>> a[iu1] = -1
>>> a
array([[-1, -1, -1, -1],
       [ 4, -1, -1, -1],
       [ 8,  9, -1, -1],
       [12, 13, 14, -1]])

These cover only a small part of the whole array (two diagonals right of the main one):

>>> iu2 = np.triu_indices(4, 2)
>>> a[iu2] = -10
>>> a
array([[ -1,  -1, -10, -10],
       [  4,  -1,  -1, -10],
       [  8,   9,  -1,  -1],
       [ 12,  13,  14,  -1]])