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)
| 882 | |
| 883 | @set_module('numpy') |
| 884 | def tril_indices(n, k=0, m=None): |
| 885 | """ |
| 886 | Return the indices for the lower-triangle of an (n, m) array. |
| 887 | |
| 888 | Parameters |
| 889 | ---------- |
| 890 | n : int |
| 891 | The row dimension of the arrays for which the returned |
| 892 | indices will be valid. |
| 893 | k : int, optional |
| 894 | Diagonal offset (see `tril` for details). |
| 895 | m : int, optional |
| 896 | .. versionadded:: 1.9.0 |
| 897 | |
| 898 | The column dimension of the arrays for which the returned |
| 899 | arrays will be valid. |
| 900 | By default `m` is taken equal to `n`. |
| 901 | |
| 902 | |
| 903 | Returns |
| 904 | ------- |
| 905 | inds : tuple of arrays |
| 906 | The indices for the triangle. The returned tuple contains two arrays, |
| 907 | each with the indices along one dimension of the array. |
| 908 | |
| 909 | See also |
| 910 | -------- |
| 911 | triu_indices : similar function, for upper-triangular. |
| 912 | mask_indices : generic function accepting an arbitrary mask function. |
| 913 | tril, triu |
| 914 | |
| 915 | Notes |
| 916 | ----- |
| 917 | .. versionadded:: 1.4.0 |
| 918 | |
| 919 | Examples |
| 920 | -------- |
| 921 | Compute two different sets of indices to access 4x4 arrays, one for the |
| 922 | lower triangular part starting at the main diagonal, and one starting two |
| 923 | diagonals further right: |
| 924 | |
| 925 | >>> il1 = np.tril_indices(4) |
| 926 | >>> il2 = np.tril_indices(4, 2) |
| 927 | |
| 928 | Here is how they can be used with a sample array: |
| 929 | |
| 930 | >>> a = np.arange(16).reshape(4, 4) |
| 931 | >>> a |
| 932 | array([[ 0, 1, 2, 3], |
| 933 | [ 4, 5, 6, 7], |
| 934 | [ 8, 9, 10, 11], |
| 935 | [12, 13, 14, 15]]) |
| 936 | |
| 937 | Both for indexing: |
| 938 | |
| 939 | >>> a[il1] |
| 940 | array([ 0, 4, 5, ..., 13, 14, 15]) |
| 941 |