tenferro_tropical/
algebra.rs

1//! Tropical algebra markers, [`HasAlgebra`], and [`Semiring`] implementations.
2//!
3//! Each zero-sized struct identifies a tropical algebra for use with
4//! [`TensorPrims<A>`](tenferro_prims::TensorPrims). The orphan rule is
5//! satisfied because the algebra markers are defined in this crate.
6//!
7//! | Algebra marker | Scalar wrapper | ⊕ | ⊗ |
8//! |----------------|---------------|---|---|
9//! | [`MaxPlusAlgebra`] | [`MaxPlus<T>`](crate::MaxPlus) | max | + |
10//! | [`MinPlusAlgebra`] | [`MinPlus<T>`](crate::MinPlus) | min | + |
11//! | [`MaxMulAlgebra`] | [`MaxMul<T>`](crate::MaxMul) | max | × |
12
13use tenferro_algebra::{HasAlgebra, Semiring};
14
15use crate::scalar::{MaxMul, MaxPlus, MinPlus};
16
17/// Algebra marker for the max-plus tropical semiring (⊕ = max, ⊗ = +).
18///
19/// Used as the algebra parameter `A` in
20/// [`TensorPrims<MaxPlusAlgebra>`](tenferro_prims::TensorPrims).
21///
22/// # Examples
23///
24/// ```ignore
25/// use tenferro_tropical::MaxPlusAlgebra;
26/// use tenferro_prims::{CpuBackend, TensorPrims};
27///
28/// // Check extension support
29/// let has_contract = CpuBackend::has_extension_for::<f64>(
30///     tenferro_prims::Extension::Contract,
31/// );
32/// ```
33pub struct MaxPlusAlgebra;
34
35/// Algebra marker for the min-plus tropical semiring (⊕ = min, ⊗ = +).
36///
37/// # Examples
38///
39/// ```ignore
40/// use tenferro_tropical::MinPlusAlgebra;
41/// use tenferro_prims::{CpuBackend, TensorPrims};
42///
43/// let has_contract = CpuBackend::has_extension_for::<f64>(
44///     tenferro_prims::Extension::Contract,
45/// );
46/// ```
47pub struct MinPlusAlgebra;
48
49/// Algebra marker for the max-times tropical semiring (⊕ = max, ⊗ = ×).
50///
51/// # Examples
52///
53/// ```ignore
54/// use tenferro_tropical::MaxMulAlgebra;
55/// use tenferro_prims::{CpuBackend, TensorPrims};
56///
57/// let has_contract = CpuBackend::has_extension_for::<f64>(
58///     tenferro_prims::Extension::Contract,
59/// );
60/// ```
61pub struct MaxMulAlgebra;
62
63// ---------------------------------------------------------------------------
64// HasAlgebra: scalar → algebra mapping
65// ---------------------------------------------------------------------------
66
67impl HasAlgebra for MaxPlus<f32> {
68    type Algebra = MaxPlusAlgebra;
69}
70
71impl HasAlgebra for MaxPlus<f64> {
72    type Algebra = MaxPlusAlgebra;
73}
74
75impl HasAlgebra for MinPlus<f32> {
76    type Algebra = MinPlusAlgebra;
77}
78
79impl HasAlgebra for MinPlus<f64> {
80    type Algebra = MinPlusAlgebra;
81}
82
83impl HasAlgebra for MaxMul<f32> {
84    type Algebra = MaxMulAlgebra;
85}
86
87impl HasAlgebra for MaxMul<f64> {
88    type Algebra = MaxMulAlgebra;
89}
90
91// ---------------------------------------------------------------------------
92// Semiring implementations (f64 only for POC)
93// ---------------------------------------------------------------------------
94
95/// Max-plus semiring over `MaxPlus<f64>`.
96///
97/// - `zero()` = MaxPlus(−∞)
98/// - `one()` = MaxPlus(0.0)
99/// - `add(a, b)` = max(a, b)
100/// - `mul(a, b)` = a + b (ordinary addition)
101impl Semiring for MaxPlusAlgebra {
102    type Scalar = MaxPlus<f64>;
103
104    fn zero() -> Self::Scalar {
105        todo!()
106    }
107
108    fn one() -> Self::Scalar {
109        todo!()
110    }
111
112    fn add(_a: Self::Scalar, _b: Self::Scalar) -> Self::Scalar {
113        todo!()
114    }
115
116    fn mul(_a: Self::Scalar, _b: Self::Scalar) -> Self::Scalar {
117        todo!()
118    }
119}
120
121/// Min-plus semiring over `MinPlus<f64>`.
122///
123/// - `zero()` = MinPlus(+∞)
124/// - `one()` = MinPlus(0.0)
125/// - `add(a, b)` = min(a, b)
126/// - `mul(a, b)` = a + b (ordinary addition)
127impl Semiring for MinPlusAlgebra {
128    type Scalar = MinPlus<f64>;
129
130    fn zero() -> Self::Scalar {
131        todo!()
132    }
133
134    fn one() -> Self::Scalar {
135        todo!()
136    }
137
138    fn add(_a: Self::Scalar, _b: Self::Scalar) -> Self::Scalar {
139        todo!()
140    }
141
142    fn mul(_a: Self::Scalar, _b: Self::Scalar) -> Self::Scalar {
143        todo!()
144    }
145}
146
147/// Max-times semiring over `MaxMul<f64>`.
148///
149/// - `zero()` = MaxMul(0.0)
150/// - `one()` = MaxMul(1.0)
151/// - `add(a, b)` = max(a, b)
152/// - `mul(a, b)` = a × b (ordinary multiplication)
153impl Semiring for MaxMulAlgebra {
154    type Scalar = MaxMul<f64>;
155
156    fn zero() -> Self::Scalar {
157        todo!()
158    }
159
160    fn one() -> Self::Scalar {
161        todo!()
162    }
163
164    fn add(_a: Self::Scalar, _b: Self::Scalar) -> Self::Scalar {
165        todo!()
166    }
167
168    fn mul(_a: Self::Scalar, _b: Self::Scalar) -> Self::Scalar {
169        todo!()
170    }
171}