MCPcopy Create free account
hub / github.com/numpy/numpy / triu_indices_from

Function triu_indices_from

numpy/lib/_twodim_base_impl.py:1183–1242  ·  view source on GitHub ↗

Return the indices for the upper-triangle of arr. See `triu_indices` for full details. Parameters ---------- arr : ndarray, shape(N, N) The indices will be valid for square arrays. k : int, optional Diagonal offset (see `triu` for details). Returns

(arr, k=0)

Source from the content-addressed store, hash-verified

1181
1182@array_function_dispatch(_trilu_indices_form_dispatcher)
1183def triu_indices_from(arr, k=0):
1184 """
1185 Return the indices for the upper-triangle of arr.
1186
1187 See `triu_indices` for full details.
1188
1189 Parameters
1190 ----------
1191 arr : ndarray, shape(N, N)
1192 The indices will be valid for square arrays.
1193 k : int, optional
1194 Diagonal offset (see `triu` for details).
1195
1196 Returns
1197 -------
1198 triu_indices_from : tuple, shape(2) of ndarray, shape(N)
1199 Indices for the upper-triangle of `arr`.
1200
1201 Examples
1202 --------
1203 >>> import numpy as np
1204
1205 Create a 4 by 4 array
1206
1207 >>> a = np.arange(16).reshape(4, 4)
1208 >>> a
1209 array([[ 0, 1, 2, 3],
1210 [ 4, 5, 6, 7],
1211 [ 8, 9, 10, 11],
1212 [12, 13, 14, 15]])
1213
1214 Pass the array to get the indices of the upper triangular elements.
1215
1216 >>> triui = np.triu_indices_from(a)
1217 >>> triui
1218 (array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
1219
1220 >>> a[triui]
1221 array([ 0, 1, 2, 3, 5, 6, 7, 10, 11, 15])
1222
1223 This is syntactic sugar for triu_indices().
1224
1225 >>> np.triu_indices(a.shape[0])
1226 (array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]), array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
1227
1228 Use the `k` parameter to return the indices for the upper triangular array
1229 from the k-th diagonal.
1230
1231 >>> triuim1 = np.triu_indices_from(a, k=1)
1232 >>> a[triuim1]
1233 array([ 1, 2, 3, 6, 7, 11])
1234
1235
1236 See Also
1237 --------
1238 triu_indices, triu, tril_indices_from
1239 """
1240 if arr.ndim != 2:

Callers

nothing calls this directly

Calls 1

triu_indicesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…