torch.tril_indices
function tril_indices(row: number, col: number, options?: IndicesOptions): [Tensor, Tensor]function tril_indices(row: number, col: number, offset: number, options?: IndicesOptions): [Tensor, Tensor]Returns the row and column indices of the lower triangular part of a matrix.
Generates two 1-D tensors containing the row and column indices of all elements in the lower triangular region (on and below specified diagonal). Essential for:
- Sparse tensor construction: Creating indices for sparse lower triangular matrices
- Element selection: Extracting lower triangular elements via advanced indexing
- Structured sparsity: Defining sparsity patterns (e.g., triangular solvers)
- Matrix visualization: Plotting or printing only lower triangular structure
- Batch operations: Processing multiple triangular subproblems efficiently
- Memory optimization: Storing only non-zero lower triangular elements
Returns two 1-D tensors: (row_indices, col_indices) where row_indices[i] and col_indices[i] give the position of the i-th lower triangular element.
- Data type: Returns 1-D tensors, typically 'int32'
- Symmetry: Complementary to triu_indices
Parameters
rownumber- Number of rows in matrix
colnumber- Number of columns in matrix
optionsIndicesOptionsoptional- Tensor options (dtype: 'int32' or 'float32', device, etc.)
Returns
Examples
// Get indices for 3×3 lower triangle
const [rows, cols] = torch.tril_indices(3, 3);
// rows: [0, 1, 1, 2, 2, 2], cols: [0, 0, 1, 0, 1, 2]
// Include one diagonal above main
const [r, c] = torch.tril_indices(3, 3, { offset: 1 });
// rows: [0, 0, 1, 1, 1, 2, 2, 2], cols: [0, 1, 0, 1, 2, 0, 1, 2]See Also
- PyTorch torch.tril_indices()
- triu_indices - Get upper triangular indices
- tril - Extract lower triangular part directly
- index_select - Select elements by indices
- gather - Gather elements (alternative indexing)