Skip to main content

sim_lib_discrete_graph/
intring.rs

1//! A plain integer ring as a [`Semiring`], for signed structural matrices.
2//!
3//! The algebra spine's tropical / boolean / counting semirings cannot represent
4//! the signed entries of incidence and Laplacian matrices (no subtraction, no
5//! negatives). `IntRing` is the ordinary ring of `i64` under `+` and `*`; it is
6//! a valid semiring (every ring is), with no Kleene `star`.
7
8use sim_lib_discrete_algebra::Semiring;
9
10/// The ring of `i64` under saturating `+` and `*`.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub struct IntRing(pub i64);
13
14impl Semiring for IntRing {
15    fn zero() -> Self {
16        IntRing(0)
17    }
18    fn one() -> Self {
19        IntRing(1)
20    }
21    fn add(&self, other: &Self) -> Self {
22        IntRing(self.0.saturating_add(other.0))
23    }
24    fn mul(&self, other: &Self) -> Self {
25        IntRing(self.0.saturating_mul(other.0))
26    }
27    fn is_zero(&self) -> bool {
28        self.0 == 0
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35
36    #[test]
37    fn ring_basics() {
38        assert_eq!(IntRing(3).add(&IntRing(-5)), IntRing(-2));
39        assert_eq!(IntRing(4).mul(&IntRing(-2)), IntRing(-8));
40        assert!(IntRing::zero().is_zero());
41        assert_eq!(IntRing(7).star(), None);
42    }
43}