You are viewing an older version (2.4.3). Go to latest (2.4.4)
{ } Raw JSON

bundles / numpy 2.4.3 / numpy / tril_indices

function

numpy:tril_indices

source: /numpy/lib/_twodim_base_impl.py :907

Signature

def   tril_indices ( n k = 0 m = None )

Summary

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

Parameters

n : int

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

k : int, optional

Diagonal offset (see tril for details).

m : int, optional

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

Returns

inds : tuple of arrays

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.

Examples

import numpy as np
Compute two different sets of indices to access 4x4 arrays, one for the lower triangular part starting at the main diagonal, and one starting two diagonals further right:
il1 = np.tril_indices(4)
il1
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
Both for indexing:
a[il1]
And for assigning values:
a[il1] = -1
a
These cover almost the whole array (two diagonals right of the main one):
il2 = np.tril_indices(4, 2)
a[il2] = -10
a

See also

mask_indices

generic function accepting an arbitrary mask function.

tril
triu
triu_indices

similar function, for upper-triangular.

Aliases

  • numpy.tril_indices