tenferro_tropical/scalar.rs
1//! Tropical scalar wrapper types.
2//!
3//! Each newtype redefines `Add` and `Mul` so that the [`Scalar`](tenferro_algebra::Scalar)
4//! blanket impl is satisfied with tropical semantics:
5//!
6//! | Type | `+` (⊕) | `*` (⊗) | Zero | One |
7//! |------|---------|---------|------|-----|
8//! | [`MaxPlus<T>`] | max | + | −∞ | 0 |
9//! | [`MinPlus<T>`] | min | + | +∞ | 0 |
10//! | [`MaxMul<T>`] | max | × | 0 | 1 |
11//!
12//! All arithmetic bodies use `todo!()` (POC skeleton).
13
14use std::fmt;
15use std::ops;
16
17/// Max-plus tropical scalar: ⊕ = max, ⊗ = +.
18///
19/// The most common tropical semiring, used for shortest-path and
20/// optimal-alignment problems. Wraps any ordered numeric type `T`.
21///
22/// # Examples
23///
24/// ```ignore
25/// use tenferro_tropical::MaxPlus;
26///
27/// let a = MaxPlus(3.0_f64);
28/// let b = MaxPlus(5.0_f64);
29///
30/// // Tropical addition = max
31/// let c = a + b; // MaxPlus(5.0)
32///
33/// // Tropical multiplication = ordinary addition
34/// let d = a * b; // MaxPlus(8.0)
35/// ```
36#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
37#[repr(transparent)]
38pub struct MaxPlus<T>(pub T);
39
40/// Min-plus tropical scalar: ⊕ = min, ⊗ = +.
41///
42/// Used for shortest-path problems (Dijkstra, Bellman–Ford).
43///
44/// # Examples
45///
46/// ```ignore
47/// use tenferro_tropical::MinPlus;
48///
49/// let a = MinPlus(3.0_f64);
50/// let b = MinPlus(5.0_f64);
51///
52/// // Tropical addition = min
53/// let c = a + b; // MinPlus(3.0)
54///
55/// // Tropical multiplication = ordinary addition
56/// let d = a * b; // MinPlus(8.0)
57/// ```
58#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
59#[repr(transparent)]
60pub struct MinPlus<T>(pub T);
61
62/// Max-times tropical scalar: ⊕ = max, ⊗ = ×.
63///
64/// Used for probabilistic models where you want the most likely
65/// path (Viterbi algorithm with probabilities in [0, 1]).
66///
67/// # Examples
68///
69/// ```ignore
70/// use tenferro_tropical::MaxMul;
71///
72/// let a = MaxMul(0.3_f64);
73/// let b = MaxMul(0.7_f64);
74///
75/// // Tropical addition = max
76/// let c = a + b; // MaxMul(0.7)
77///
78/// // Tropical multiplication = ordinary multiplication
79/// let d = a * b; // MaxMul(0.21)
80/// ```
81#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
82#[repr(transparent)]
83pub struct MaxMul<T>(pub T);
84
85// ---------------------------------------------------------------------------
86// Display
87// ---------------------------------------------------------------------------
88
89impl<T: fmt::Display> fmt::Display for MaxPlus<T> {
90 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
91 write!(f, "MaxPlus({})", self.0)
92 }
93}
94
95impl<T: fmt::Display> fmt::Display for MinPlus<T> {
96 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97 write!(f, "MinPlus({})", self.0)
98 }
99}
100
101impl<T: fmt::Display> fmt::Display for MaxMul<T> {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 write!(f, "MaxMul({})", self.0)
104 }
105}
106
107// ---------------------------------------------------------------------------
108// MaxPlus<T>: Add (= max), Mul (= +), Zero (= −∞), One (= 0)
109// ---------------------------------------------------------------------------
110
111impl<T: Copy + PartialOrd> ops::Add for MaxPlus<T> {
112 type Output = Self;
113
114 /// Tropical addition: max(a, b).
115 fn add(self, _rhs: Self) -> Self {
116 todo!()
117 }
118}
119
120impl<T: Copy + ops::Add<Output = T>> ops::Mul for MaxPlus<T> {
121 type Output = Self;
122
123 /// Tropical multiplication: a + b (ordinary addition).
124 fn mul(self, _rhs: Self) -> Self {
125 todo!()
126 }
127}
128
129impl<T: num_traits::Float> num_traits::Zero for MaxPlus<T> {
130 /// Additive identity: −∞ (negative infinity).
131 fn zero() -> Self {
132 todo!()
133 }
134
135 fn is_zero(&self) -> bool {
136 todo!()
137 }
138}
139
140impl<T: num_traits::Float> num_traits::One for MaxPlus<T> {
141 /// Multiplicative identity: 0.
142 fn one() -> Self {
143 todo!()
144 }
145}
146
147// ---------------------------------------------------------------------------
148// MinPlus<T>: Add (= min), Mul (= +), Zero (= +∞), One (= 0)
149// ---------------------------------------------------------------------------
150
151impl<T: Copy + PartialOrd> ops::Add for MinPlus<T> {
152 type Output = Self;
153
154 /// Tropical addition: min(a, b).
155 fn add(self, _rhs: Self) -> Self {
156 todo!()
157 }
158}
159
160impl<T: Copy + ops::Add<Output = T>> ops::Mul for MinPlus<T> {
161 type Output = Self;
162
163 /// Tropical multiplication: a + b (ordinary addition).
164 fn mul(self, _rhs: Self) -> Self {
165 todo!()
166 }
167}
168
169impl<T: num_traits::Float> num_traits::Zero for MinPlus<T> {
170 /// Additive identity: +∞ (positive infinity).
171 fn zero() -> Self {
172 todo!()
173 }
174
175 fn is_zero(&self) -> bool {
176 todo!()
177 }
178}
179
180impl<T: num_traits::Float> num_traits::One for MinPlus<T> {
181 /// Multiplicative identity: 0.
182 fn one() -> Self {
183 todo!()
184 }
185}
186
187// ---------------------------------------------------------------------------
188// MaxMul<T>: Add (= max), Mul (= ×), Zero (= 0), One (= 1)
189// ---------------------------------------------------------------------------
190
191impl<T: Copy + PartialOrd> ops::Add for MaxMul<T> {
192 type Output = Self;
193
194 /// Tropical addition: max(a, b).
195 fn add(self, _rhs: Self) -> Self {
196 todo!()
197 }
198}
199
200impl<T: Copy + ops::Mul<Output = T>> ops::Mul for MaxMul<T> {
201 type Output = Self;
202
203 /// Tropical multiplication: a × b (ordinary multiplication).
204 fn mul(self, _rhs: Self) -> Self {
205 todo!()
206 }
207}
208
209impl<T: num_traits::Float> num_traits::Zero for MaxMul<T> {
210 /// Additive identity: 0 (for max, 0 is the identity when values are non-negative).
211 fn zero() -> Self {
212 todo!()
213 }
214
215 fn is_zero(&self) -> bool {
216 todo!()
217 }
218}
219
220impl<T: num_traits::Float> num_traits::One for MaxMul<T> {
221 /// Multiplicative identity: 1.
222 fn one() -> Self {
223 todo!()
224 }
225}
226
227// ---------------------------------------------------------------------------
228// Unsafe marker traits — MaxPlus/MinPlus/MaxMul are Send + Sync if T is.
229// These are automatically derived from the inner T via #[repr(transparent)].
230// ---------------------------------------------------------------------------
231
232// Send and Sync are auto-derived since the wrapper is transparent over T.