Tensor is a small, composable Rust crate for working with fixed-dimension multidimensional arrays (tensors). Its API is inspired by the standard library's Iterator: operations such as map, zip, reorder or tile produce lightweight adapters that can be composed and evaluated lazily.
- Tensor: a view over N-dimensional data with a fixed number of dimensions.
- Sources: concrete tensor backends (e.g. dense vector or sparse map).
- Adapters: lazy, composable operations that return new tensor views (map, zip, reorder, tile, filter, ...).
- Collection: adapters are evaluated when you collect them into a concrete source.
Creating a dense 2D tensor and mapping values:
use tensor::{vector, shape, Tensor};
fn example_map() {
// create a 2×3 tensor with values 0..6 (row-major)
let a = vector(0..6, [2usize, 3]);
// lazy adapter: add 1 to each element
let b = a.map(|x| x + 1);
// collect into a concrete tensor source and verify result
let collected = b.collect();
assert_eq!(collected, vector(1..7, [2usize, 3]));
}Using mutable assignment (TensorMut):
use tensor::{vector, empty, TensorMut};
fn example_assign() {
let src = vector(0..12, [4usize, 3]);
let mut dst = empty([4usize, 3]);
// assign copies of values from src into dst
dst.assign(&src);
assert_eq!(src, dst);
}Composing adapters:
use tensor::{vector, Tensor};
fn example_compose() {
let t = vector(0..8, [2usize, 2, 2]);
// reorder axes, then map, then tile the result
let r = t.reorder([2, 0, 1]).map(|x| x * 2).tile([4usize, 2, 2]);
let out = r.collect();
// expected result after reorder([2,0,1]), map(|x| x * 2) and tile([4,2,2])
assert_eq!(out, vector([
0, 8, 0, 8,
2, 10, 2, 10,
4, 12, 4, 12,
6, 14, 6, 14,
], [4usize, 2, 2]));
}Common adapters
- map: apply a function to every element.
- zip: combine two tensors elementwise with a function.
- reorder: change axis ordering without copying data.
- tile: repeat the tensor along axes to form a larger shape.
- filter: select elements (using a boolean tensor) — yields a filtered view.
Documentation and tests
- See crate documentation (if generated) for API details and trait documentation (Tensor, TensorMut, etc.).
- Examples and intended behavior are demonstrated in the repository tests.
Tests
- See
tests/tensor.rsfor runnable examples and assertions that reflect expected behavior.
- Adapters are lazy: no computation happens until you collect or mutate.
- The crate focuses on composability and small, explicit primitives rather than implicit broadcasting rules.
- API is experimental and subject to change.