Clorch exposes tensor indexing through a single function,Documentation Index
Fetch the complete documentation index at: https://mintlify.com/antlobach/clorch/llms.txt
Use this file to discover all available pages before exploring further.
torch/ix, that accepts a tensor followed by one indexer per dimension. Every PyTorch indexing pattern — from a simple integer access to a reversed multi-dimensional slice — has a direct ix equivalent. This guide provides a structured translation reference so you can convert Python notebooks and research implementations into idiomatic Clorch code without guessing.
Quick Reference
The table below maps common PyTorch expressions to their Clorch equivalents.| PyTorch | Clorch | Description |
|---|---|---|
t[0] | (ix t 0) | Single element |
t[-1] | (ix t -1) | Last element |
t[0, 1] | (ix t 0 1) | Multi-dimensional |
t[:, :] | (ix t :all :all) | Select all dimensions |
t[0:5] | (ix t [0 5]) | Range slice |
t[:5] | (ix t [nil 5]) | From start |
t[5:] | (ix t [5 nil]) | To end |
t[::2] | (ix t [nil nil 2]) | Every 2nd element |
t[1:8:2] | (ix t [1 8 2]) | Step slice |
t[::-1] | (ix t [nil nil -1]) | Reverse entire tensor |
t[5::-1] | (ix t [5 nil -1]) | Reverse from index 5 |
t[..., 0] | (ix t (quote ...) 0) | Ellipsis |
t[0, ...] | (ix t 0 (quote ...)) | Ellipsis at end |
Basic Indexing
1D Tensors
Integer indexing extracts a single scalar. Negative indices count from the end. All scalar results are returned as floats by default.Multi-dimensional Tensors
Pass one indexer per dimension. Providing fewer indexers than dimensions selects the full remaining dimensions.Slicing
Basic Ranges
A two-element vector[start stop] selects elements from start (inclusive) to stop (exclusive), matching Python’s exclusive-stop convention.
Open-Ended Ranges
Usenil in place of a bound to leave it open. [start nil] runs to the end; [nil stop] starts from the beginning.
Step Slices
A three-element vector[start stop step] applies a stride. Either or both of start and stop may be nil to leave that bound open.
Negative Step Slicing (Reversing)
A negative step reverses traversal order.[nil nil -1] is the full reversal; [start nil -1] reverses from a given index down to zero.
2D Tensor Examples
Multi-dimensional slicing combines one indexer per axis, separated by spaces.Reversing Rows and Columns
The ix Function: Indexer Type Reference
torch/ix accepts a tensor and any number of indexers — one per dimension you want to slice.
Indexer Syntax Summary
| Clorch Syntax | Meaning |
|---|---|
0, 1, -1 | Integer index — extracts a scalar, reduces dimensionality |
:all | Select entire dimension |
:_ | Select entire dimension (alternative spelling) |
(quote ...) | Ellipsis — fills all remaining unspecified dimensions |
[start stop] | Slice from start (inclusive) to stop (exclusive) |
[start stop step] | Slice with positive step |
[nil stop] | From index 0 to stop |
[start nil] | From start to end of dimension |
[nil nil step] | Every step-th element, full dimension |
[nil nil -1] | Reverse entire dimension |
[nil nil -2] | Reverse every 2nd element |
[start nil -1] | Reverse from start down to index 0 |
Advanced Indexing
Ellipsis
The ellipsis fills all unspecified dimensions between explicitly indexed ones. Use(quote ...) or the reader shorthand '... in scripts.
Select-All Identity
:all and :_ are interchangeable. Using them on every dimension returns the tensor unchanged.
Integer Tensor (Fancy) Indexing
Pass a tensor with dtype:int64 as an indexer to perform gather-style selection.
Boolean Mask Indexing
Pass a boolean tensor to select elements where the mask istrue.
Helper Pattern: Tensor to Clojure Vector
When you need to work with slice results in pure Clojure, convert withitem-float and tseq.
Real-World Patterns
Train/test split
Train/test split
Extract mini-batches
Extract mini-batches
Sliding window sequences
Sliding window sequences
Important Notes
Float results. Scalar extractions via integer indexing return
double values. Use torch/item-float to extract a Clojure number from a 0-dimensional tensor result.nil vs ::. Slice vectors use nil to denote open bounds, not Clojure’s auto-qualified :: keywords, which would conflict with the keyword indexer namespace.