Skip to content

Commit

Permalink
Merge branch 'main' into nested_evaluate
Browse files Browse the repository at this point in the history
  • Loading branch information
benruijl committed Jul 28, 2024
2 parents b9b43dc + 45cf1fc commit 8edf402
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/coefficient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,70 @@ impl Add<i64> for CoefficientView<'_> {
}
}

impl TryFrom<Atom> for i64 {
type Error = &'static str;

fn try_from(value: Atom) -> Result<Self, Self::Error> {
value.as_view().try_into()
}
}

impl TryFrom<&Atom> for i64 {
type Error = &'static str;

fn try_from(value: &Atom) -> Result<Self, Self::Error> {
value.as_view().try_into()
}
}

impl<'a> TryFrom<AtomView<'a>> for i64 {
type Error = &'static str;

fn try_from(value: AtomView<'a>) -> Result<Self, Self::Error> {
if let AtomView::Num(n) = value {
if let CoefficientView::Natural(n, 1) = n.get_coeff_view() {
return Ok(n);
} else {
Err("Not an i64")
}
} else {
Err("Not a number")
}
}
}

impl TryFrom<Atom> for Rational {
type Error = &'static str;

fn try_from(value: Atom) -> Result<Self, Self::Error> {
value.as_view().try_into()
}
}

impl TryFrom<&Atom> for Rational {
type Error = &'static str;

fn try_from(value: &Atom) -> Result<Self, Self::Error> {
value.as_view().try_into()
}
}

impl<'a> TryFrom<AtomView<'a>> for Rational {
type Error = &'static str;

fn try_from(value: AtomView<'a>) -> Result<Self, Self::Error> {
if let AtomView::Num(n) = value {
match n.get_coeff_view() {
CoefficientView::Natural(n, d) => Ok(Rational::from_unchecked(n, d)),
CoefficientView::Large(r) => Ok(r.to_rat()),
_ => Err("Not a rational"),
}
} else {
Err("Not a number")
}
}
}

impl Atom {
/// Set the coefficient ring to the multivariate rational polynomial with `vars` variables.
pub fn set_coefficient_ring(&self, vars: &Arc<Vec<Variable>>) -> Atom {
Expand Down
5 changes: 5 additions & 0 deletions src/poly/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ impl<R: Ring, E: Exponent> PolynomialRing<R, E> {
_phantom_exp: PhantomData,
}
}

/// Get the coefficient ring.
pub fn coefficient_ring(&self) -> &R {
&self.ring
}
}

impl<R: Ring, E: Exponent> std::fmt::Display for PolynomialRing<R, E> {
Expand Down

0 comments on commit 8edf402

Please sign in to comment.