-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of: #155
- Loading branch information
Showing
3 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,15 @@ | ||
mod boolean; | ||
mod expectation; | ||
mod finitefield; | ||
mod rational; | ||
mod realsemiring; | ||
mod semiring_traits; | ||
mod tropical; | ||
|
||
pub use self::boolean::*; | ||
pub use self::expectation::*; | ||
pub use self::finitefield::*; | ||
pub use self::rational::*; | ||
pub use self::realsemiring::*; | ||
pub use self::semiring_traits::*; | ||
pub use self::tropical::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use rational::Rational; | ||
use std::{fmt::Display, ops}; | ||
|
||
use super::semiring_traits::Semiring; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)] | ||
pub struct RationalSemiring(Rational); | ||
|
||
impl Semiring for RationalSemiring { | ||
fn one() -> Self { | ||
RationalSemiring(Rational::new(1, 1)) | ||
} | ||
|
||
fn zero() -> Self { | ||
RationalSemiring(Rational::new(0, 1)) | ||
} | ||
} | ||
|
||
impl Display for RationalSemiring { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}/{}", self.0.numerator(), self.0.denominator()) | ||
} | ||
} | ||
|
||
impl ops::Add<RationalSemiring> for RationalSemiring { | ||
type Output = RationalSemiring; | ||
|
||
fn add(self, rhs: RationalSemiring) -> Self::Output { | ||
RationalSemiring(self.0 + rhs.0) | ||
} | ||
} | ||
|
||
impl ops::Mul<RationalSemiring> for RationalSemiring { | ||
type Output = RationalSemiring; | ||
|
||
fn mul(self, rhs: RationalSemiring) -> Self::Output { | ||
RationalSemiring(self.0 * rhs.0) | ||
} | ||
} |