summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichie Cotton <richierocks@gmail.com>2023-01-17 01:21:56 -0500
committerGitHub <noreply@github.com>2023-01-16 22:21:56 -0800
commitb511719a25d3437081dfe6976e64448221c33001 (patch)
treed4cc36828e187b7c5e30c1023da9cdf007803a4b
parent5e9630109965fad0efd177cf0952c87cc7fed95c (diff)
downloadnumpy-b511719a25d3437081dfe6976e64448221c33001.tar.gz
DOC: #22266 Add examples for tril_indices_from(), triu_indices_from() (#22562)
* DOC: #22266 Add examples for tri[lu]_indices_from() * DOC: see also for tri[lu]_indices_from() * DOC: Fix triu_indices_from example and minor updates. * incides -> indices * Update wording surrounding . Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
-rw-r--r--numpy/lib/twodim_base.py71
1 files changed, 69 insertions, 2 deletions
diff --git a/numpy/lib/twodim_base.py b/numpy/lib/twodim_base.py
index 654ee4cf5..dcb4ed46c 100644
--- a/numpy/lib/twodim_base.py
+++ b/numpy/lib/twodim_base.py
@@ -995,9 +995,42 @@ def tril_indices_from(arr, k=0):
k : int, optional
Diagonal offset (see `tril` for details).
+ Examples
+ --------
+
+ Create a 4 by 4 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]])
+
+ Pass the array to get the indices of the lower triangular elements.
+
+ >>> trili = np.tril_indices_from(a)
+ >>> trili
+ (array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))
+
+ >>> a[trili]
+ array([ 0, 4, 5, 8, 9, 10, 12, 13, 14, 15])
+
+ This is syntactic sugar for tril_indices().
+
+ >>> np.tril_indices(a.shape[0])
+ (array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))
+
+ Use the `k` parameter to return the indices for the lower triangular array
+ up to the k-th diagonal.
+
+ >>> trili1 = np.tril_indices_from(a, k=1)
+ >>> a[trili1]
+ array([ 0, 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15])
+
See Also
--------
- tril_indices, tril
+ tril_indices, tril, triu_indices_from
Notes
-----
@@ -1114,9 +1147,43 @@ def triu_indices_from(arr, k=0):
triu_indices_from : tuple, shape(2) of ndarray, shape(N)
Indices for the upper-triangle of `arr`.
+ Examples
+ --------
+
+ Create a 4 by 4 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]])
+
+ 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])
+
+
See Also
--------
- triu_indices, triu
+ triu_indices, triu, tril_indices_from
Notes
-----