3个版本
0.1.2 | 2023年1月22日 |
---|---|
0.1.1 | 2023年1月22日 |
0.1.0 | 2023年1月22日 |
#1808 in 数据结构
110KB
3K SLoC
Tensorism
一个小型的实验性库,用于操作具有多个索引的数组。它旨在
- 简洁:可以使用特定的宏轻松表达与相关数学表达式类似的形式变换。
- 类型安全:维度的兼容性可以在编译时进行检查。
概述
Tensorism分为两个同级的crate
- tensorism (此库)包含类型和特质。
- tensorism-gen 包含用于高效编写公式的宏。
示例
- 计算矩阵的迹
use tensorism_gen::make;
use std::iter::Sum;
let mM: Tensor2<StaticDimTag<4>, StaticDimTag<4>, f64> = …;
let tau = make!(<f64>::sum(i $ mM[i, i])); // Or equivalently: `make!((i $ mM[i, i]).sum())`
$$\tau \leftarrow \sum_{i=0}^3 M_{i, i}$$
- 计算两个矩阵的乘积
use tensorism_gen::make;
use std::iter::Sum;
let mA: Tensor2<StaticDimTag<7>, StaticDimTag<4>, Complex64> = …;
let mB: Tensor2<StaticDimTag<4>, StaticDimTag<5>, Complex64> = …;
let mC = make!(i k $ <Complex64>::sum(j $ mA[i, j] * mB[j, k]));
$$\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}$$
- 根据给定的"轴"找到最大值(这里指瞬间)
use tensorism_gen::make;
use datetime::Instant;
fn maximum_of(it: impl Iterator<Item=Instant>) -> Instant { … }
let tD: Tensor3<StaticDimTag<10>, StaticDimTag<25>, StaticDimTag<3>, Instant> = …;
let mX = make!(i j $ maximum_of(k $ tD[i, j, k])); // : Tensor2<StaticDimTag<10>, StaticDimTag<25>, Instant>
let mY = make!(k i $ maximum_of(j $ tD[i, j, k])); // : Tensor2<StaticDimTag<3>, StaticDimTag<10>, Instant>
let v = make!(j $ maximum_of(i k $ tD[i, j, k])); // : Tensor1<StaticDimTag<25>, Instant>
let d = make!(maximum_of(i j k $ tD[i, j, k])); // : 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}$$
- 计算交集
use tensorism_gen::make;
use std::string::String;
use std::collections::HashSet;
fn intersection_of<'a>(it: impl Iterator<Item=&'a HashSet<String>>) -> HashSet<String> { … }
let mA: Tensor2<StaticDimTag<4>, StaticDimTag<3>, HashSet<String>> = …;
let u = make!(i $ intersection_of(j $ &mA[i, j])); // : Tensor1<StaticDimTag<4>, HashSet<String>>
let v = make!(j $ intersection_of(i $ &mA[i, j])); // : 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}$$
- 计算逻辑合取和析取
use tensorism_gen::make;
fn forall(it: impl Iterator<Item=bool>) -> bool { … }
fn exists(it: impl Iterator<Item=bool>) -> bool { … }
let q: Tensor3<StaticDimTag<3>, StaticDimTag<9>, StaticDimTag<7>, bool> = …;
let p = make!(k $ forall(i $ exists(j $ q[i, j, k])));
$$\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)$$
- 任意组合
use tensorism_gen::make;
let q: Tensor3<StaticDimTag<3>, StaticDimTag<9>, StaticDimTag<7>, bool> = …;
let mA: Tensor2<StaticDimTag<3>, StaticDimTag<13>, f64> = …;
let mB: Tensor2<StaticDimTag<7>, StaticDimTag<13>, f64> = …;
let mLambda: Tensor2<StaticDimTag<7>, StaticDimTag<13>, f64> = …;
fn product(it: impl Iterator<Item=f64>) -> f64 { … }
let mZ = make!(i k $ if exists(j $ q[i, j, k] && 0f64 <= mA[i, j]) {
<f64>::sum(l $ mLambda[k, l] * sin(2 * PI * mA[i, l]))
} else {
product(m $ mA[i, m] + mB[k, m])
});
$$\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}$$
依赖项
~10KB