Skip to content

Commit

Permalink
Merge pull request numpy#26398 from ajayjanapareddi/main
Browse files Browse the repository at this point in the history
DOC: order of indices returned in tril_indices and triu_indices
  • Loading branch information
mattip authored Sep 15, 2024
2 parents b307a8c + 730ba49 commit 4206d09
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions numpy/lib/_twodim_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,9 @@ def tril_indices(n, k=0, m=None):
Returns
-------
inds : tuple of arrays
The indices for the triangle. The returned tuple contains two arrays,
each with the indices along one dimension of the array.
The row and column indices, respectively. The row indices are sorted
in non-decreasing order, and the correspdonding column indices are
strictly increasing for each row.
See also
--------
Expand All @@ -944,8 +945,11 @@ def tril_indices(n, k=0, m=None):
diagonals further right:
>>> il1 = np.tril_indices(4)
>>> il2 = np.tril_indices(4, 2)
>>> il1
(array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 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)
Expand All @@ -971,6 +975,7 @@ def tril_indices(n, k=0, m=None):
These cover almost the whole array (two diagonals right of the main one):
>>> il2 = np.tril_indices(4, 2)
>>> a[il2] = -10
>>> a
array([[-10, -10, -10, 3],
Expand Down Expand Up @@ -1075,9 +1080,9 @@ def triu_indices(n, k=0, m=None):
Returns
-------
inds : tuple, shape(2) of ndarrays, shape(`n`)
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`).
The row and column indices, respectively. The row indices are sorted
in non-decreasing order, and the correspdonding column indices are
strictly increasing for each row.
See also
--------
Expand All @@ -1098,7 +1103,11 @@ def triu_indices(n, k=0, m=None):
diagonals further right:
>>> iu1 = np.triu_indices(4)
>>> iu2 = np.triu_indices(4, 2)
>>> 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:
Expand Down Expand Up @@ -1126,6 +1135,7 @@ def triu_indices(n, k=0, m=None):
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],
Expand Down

0 comments on commit 4206d09

Please sign in to comment.