Expand description
Tropical semiring tensor operations for the tenferro workspace.
This crate extends the tenferro algebra-parameterized architecture with three tropical semirings:
| Semiring | ⊕ (add) | ⊗ (mul) | Zero | One | Use case |
|---|---|---|---|---|---|
| MaxPlus | max | + | −∞ | 0 | Shortest path, optimal alignment |
| MinPlus | min | + | +∞ | 0 | Shortest path (Dijkstra) |
| MaxMul | max | × | 0 | 1 | Viterbi, max-probability paths |
§Architecture
The crate provides:
-
Scalar wrappers (
MaxPlus<T>,MinPlus<T>,MaxMul<T>):#[repr(transparent)]newtypes that redefineAdd/Mulwith tropical semantics. Satisfy theScalarblanket impl. -
Algebra markers (
MaxPlusAlgebra,MinPlusAlgebra,MaxMulAlgebra): Zero-sized types used as the algebra parameterAinTensorPrims<A>. -
TensorPrimsimplementations:impl TensorPrims<MaxPlusAlgebra> for CpuBackendetc. Orphan rule compatible because algebra markers are defined locally. -
ArgmaxTracker: Records winner indices during tropical forward pass for use in automatic differentiation.
§Examples
§Scalar arithmetic
ⓘ
use tenferro_tropical::MaxPlus;
let a = MaxPlus(3.0_f64);
let b = MaxPlus(5.0_f64);
let c = a + b; // MaxPlus(5.0) — tropical add = max
let d = a * b; // MaxPlus(8.0) — tropical mul = ordinary +§Algebra dispatch
ⓘ
use tenferro_algebra::HasAlgebra;
use tenferro_tropical::{MaxPlus, MaxPlusAlgebra};
// MaxPlus<f64> automatically maps to MaxPlusAlgebra
fn check<T: HasAlgebra<Algebra = MaxPlusAlgebra>>() {}
check::<MaxPlus<f64>>();§Plan-based tropical contraction
ⓘ
use tenferro_prims::{CpuBackend, TensorPrims, PrimDescriptor};
use tenferro_tropical::MaxPlusAlgebra;
let desc = PrimDescriptor::BatchedGemm {
batch_dims: vec![], m: 3, n: 5, k: 4,
};
// Under MaxPlusAlgebra, GEMM computes:
// C[i,j] = max_k (A[i,k] + B[k,j])
let plan = <CpuBackend as TensorPrims<MaxPlusAlgebra>>::plan::<f64>(
&desc, &[&[3, 4], &[4, 5], &[3, 5]],
).unwrap();Re-exports§
pub use algebra::MaxMulAlgebra;pub use algebra::MaxPlusAlgebra;pub use algebra::MinPlusAlgebra;pub use argmax::ArgmaxTracker;pub use prims::TropicalPlan;pub use scalar::MaxMul;pub use scalar::MaxPlus;pub use scalar::MinPlus;