Shape-prefix and sequence operations #
The prefix combinators act on arbitrary leading shapes. Sequence traversal records its length in the result type.
Backward loop of mapAccumRight: visit i - 1, ..., 0 and prepend each result.
Instances For
Traverse the indices 0, ..., n - 1, threading a state and collecting one result per index.
Unlike a list fold, the result records in its type that the traversal produced exactly n values.
The indexed step is useful when the source is already represented as Fin n → α. Results are
accumulated in one array and packed into the tensor once, so the traversal is linear in n.
Instances For
Traverse Fin n from right to left while returning results in their original index order.
This is the state-threading pattern used by reverse-mode passes through a fixed-length sequence.
Instances For
Apply a function independently at every index of a leading shape.
Example:
-- Write the per-sample function and let the leading axis take care of itself: `[5, 3]` in,
-- `[5, 1]` out, with no loop over the batch.
def firstFeature (batch : Tensor Float [5, 3]) : Tensor Float [5, 1] :=
Tensor.mapLeading [5] (fun row => Tensor.take row 0 1) batch
Instances For
Zip two tensors pointwise across the same leading shape.