Tensorism
A small experimental library for manipulating arrays with multiple indexes. It is meant to be:
- Concise: Specific macros can be used to easily express transformations in a form similar to the related mathematical expressions.
- Type-safe: Compatibility of dimensions can be checked at compilation time.
Overview
Tensorism is divided into two sibling crates:
- tensorism (this library) contains types and traits.
- tensorism-gen contains macros to efficiently write formulas.
Examples
- Computing the trace of a matrix:
use make;
use Sum;
let mM: = …;
let tau = make!; // Or equivalently: `make!((i $ mM[i, i]).sum())`
$$\tau \leftarrow \sum_{i=0}^3 M_{i, i}$$
- Multiplying two matrices:
use make;
use Sum;
let mA: = …;
let mB: = …;
let mC = make!;
$$\forall i \in 0 .. 7,\quad \forall k \in 0 .. 5,\quad C_{i, k} \leftarrow \sum_{0 \leq j < 4} A_{i, j} \cdot B_{j, k}$$
- Finding the maximum values (here almong instants) according to given "axes":
use make;
use Instant;
let tD: = …;
let mX = make!; // : Tensor2<StaticDimTag<10>, StaticDimTag<25>, Instant>
let mY = make!; // : Tensor2<StaticDimTag<3>, StaticDimTag<10>, Instant>
let v = make!; // : Tensor1<StaticDimTag<25>, Instant>
let d = make!; // : Instant
$$\forall i \in 0 .. 10,\quad \forall j \in 0 .. 25,\quad X_{i, j} \leftarrow \max_{0 \leq k < 3} D_{i, j, k}$$
$$\forall k \in 0 .. 3,\quad \forall i \in 0 .. 10,\quad Y_{k, i} \leftarrow \max_{0 \leq j < 25} D_{i, j, k}$$
$$\forall j \in 0 .. 25,\quad v_j \leftarrow \underset{0 \leq k < 3}{\max_{0 \leq i < 10}} D_{i, j, k}$$
$$d \leftarrow \underset{0 \leq k < 3}{\underset{0 \leq j < 25}{\max_{0 \leq i < 10}}} D_{i, j, k}$$
- Computing intersections:
use make;
use String;
use HashSet;
let mA: = …;
let u = make!; // : Tensor1<StaticDimTag<4>, HashSet<String>>
let v = make!; // : Tensor1<StaticDimTag<3>, HashSet<String>>
$$\forall i \in 0 .. 4,\quad u_j \leftarrow \bigcap_{0 \leq j < 3} A_{i, j}$$
$$\forall j \in 0 .. 3,\quad v_j \leftarrow \bigcap_{0 \leq i < 4} A_{i, j}$$
- Computing logical conjunctions and disjunctions:
use make;
let q: = …;
let p = make!;
$$\forall k \in 0 .. 7,\quad p_k \leftarrow \Big( \forall i \in 0 .. 3, \ \exists j \in 0 .. 9, \ q_{i, j, k} \Big)$$
- Any combination:
use make;
let q: = …;
let mA: = …;
let mB: = …;
let mLambda: = …;
let mZ = make!;
$$\forall i \in 0 .. 3,\quad \forall k \in 0 .. 7,\quad Z_{i, k} \leftarrow \begin{cases} \sum_{0 \leq l < 13} \Lambda_{k, l} \cdot \sin\left(2 \cdot \pi \cdot A_{i, l} \right) & \mathrm{if} & \exists j \in 0 .. 9, \ q_{i, j, k} \wedge 0 \leq A_{i, j}\ \prod_{0 \leq m < 13} (A_{i, m} + B_{k, m}) & \mathrm{else} \end{cases}$$