Skip to main content
Clorch exposes tensor indexing through a single function, 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.

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

Use nil 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


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 is true.

Helper Pattern: Tensor to Clojure Vector

When you need to work with slice results in pure Clojure, convert with item-float and tseq.

Real-World Patterns


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.
Ellipsis quoting. The bare symbol ... is a valid Clojure symbol but may be read differently at the REPL versus in compiled source. Always use (quote ...) or the '... reader macro inside scripts to guarantee correct behavior.
Run the full example file to verify every slice operation in one pass: