MCPcopy Index your code
hub / github.com/numpy/numpy / tril_indices

Function tril_indices

numpy/lib/_twodim_base_impl.py:935–1016  ·  view source on GitHub ↗

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

(n, k=0, m=None)

Source from the content-addressed store, hash-verified

933
934@set_module('numpy')
935def tril_indices(n, k=0, m=None):
936 """
937 Return the indices for the lower-triangle of an (n, m) array.
938
939 Parameters
940 ----------
941 n : int
942 The row dimension of the arrays for which the returned
943 indices will be valid.
944 k : int, optional
945 Diagonal offset (see `tril` for details).
946 m : int, optional
947 The column dimension of the arrays for which the returned
948 arrays will be valid.
949 By default `m` is taken equal to `n`.
950
951
952 Returns
953 -------
954 inds : tuple of arrays
955 The row and column indices, respectively. The row indices are sorted
956 in non-decreasing order, and the corresponding column indices are
957 strictly increasing for each row.
958
959 See also
960 --------
961 triu_indices : similar function, for upper-triangular.
962 mask_indices : generic function accepting an arbitrary mask function.
963 tril, triu
964
965 Examples
966 --------
967 >>> import numpy as np
968
969 Compute two different sets of indices to access 4x4 arrays, one for the
970 lower triangular part starting at the main diagonal, and one starting two
971 diagonals further right:
972
973 >>> il1 = np.tril_indices(4)
974 >>> il1
975 (array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]), array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))
976
977 Note that row indices (first array) are non-decreasing, and the corresponding
978 column indices (second array) are strictly increasing for each row.
979 Here is how they can be used with a sample array:
980
981 >>> a = np.arange(16).reshape(4, 4)
982 >>> a
983 array([[ 0, 1, 2, 3],
984 [ 4, 5, 6, 7],
985 [ 8, 9, 10, 11],
986 [12, 13, 14, 15]])
987
988 Both for indexing:
989
990 >>> a[il1]
991 array([ 0, 4, 5, ..., 13, 14, 15])
992

Callers 2

test_tril_indicesFunction · 0.90
tril_indices_fromFunction · 0.85

Calls 3

broadcast_toFunction · 0.90
indicesFunction · 0.90
triFunction · 0.85

Tested by 1

test_tril_indicesFunction · 0.72

Used in the wild real call sites across dependent graphs

searching dependent graphs…