From 46a4916b7770623bbd7538f2ee697c5498a5860d Mon Sep 17 00:00:00 2001 From: Tim Leach <4410524+TimLeach635@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:02:30 +0100 Subject: [PATCH 1/3] refactor: fix auto-fixable warnings --- src/algebraic_numbers.rs | 6 +++--- src/array2d.rs | 8 ++++---- src/mod_int.rs | 12 ++++++------ src/polynomial.rs | 2 +- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/algebraic_numbers.rs b/src/algebraic_numbers.rs index 406c84d..7c7c05b 100644 --- a/src/algebraic_numbers.rs +++ b/src/algebraic_numbers.rs @@ -481,7 +481,7 @@ impl RealAlgebraicNumber { pub fn interval(&self) -> &DyadicFractionInterval { &self.data().interval } - fn interval_shrinker(&mut self) -> IntervalShrinker { + fn interval_shrinker(&mut self) -> IntervalShrinker<'_> { let RealAlgebraicNumberData { minimal_polynomial, interval, @@ -609,7 +609,7 @@ impl RealAlgebraicNumber { } /// shrinks the interval till it doesn't contain zero #[must_use] - fn remove_zero_from_interval(&mut self) -> Option<(Sign, IntervalShrinker)> { + fn remove_zero_from_interval(&mut self) -> Option<(Sign, IntervalShrinker<'_>)> { let sign = match self.cmp_with_zero() { Ordering::Equal => return None, Ordering::Less => Sign::Negative, @@ -956,7 +956,7 @@ struct ResultFactor { } impl ResultFactor { - fn factor(&self) -> Cow> { + fn factor(&self) -> Cow<'_, Polynomial> { self.primitive_sturm_sequence .get(0) .map(Cow::Borrowed) diff --git a/src/array2d.rs b/src/array2d.rs index 365f69a..e73cd8e 100644 --- a/src/array2d.rs +++ b/src/array2d.rs @@ -277,7 +277,7 @@ impl Array2DBase { &self, x_bound: XB, y_bound: YB, - ) -> Array2DSlice { + ) -> Array2DSlice<'_, Data::Element> { let Array2DSliceData { x_size, y_size, @@ -294,7 +294,7 @@ impl Array2DBase { &mut self, x_bound: XB, y_bound: YB, - ) -> Array2DMutSlice + ) -> Array2DMutSlice<'_, Data::Element> where Data: BorrowMut<[::Element]>, { @@ -313,14 +313,14 @@ impl Array2DBase { pub(crate) fn positions(&self) -> Positions { Positions::new(self.x_size, self.y_size) } - pub(crate) fn iter(&self) -> Iter { + pub(crate) fn iter(&self) -> Iter<'_, Data::Element> { Iter { positions: self.positions(), stride: self.stride(), data: self.data.borrow(), } } - pub(crate) fn iter_mut(&mut self) -> IterMut + pub(crate) fn iter_mut(&mut self) -> IterMut<'_, Data::Element> where Data: BorrowMut<[::Element]>, { diff --git a/src/mod_int.rs b/src/mod_int.rs index f80c53f..c836b61 100644 --- a/src/mod_int.rs +++ b/src/mod_int.rs @@ -171,7 +171,7 @@ pub trait ModularReducePow: ModularReduce { } pub trait Modulus: Clone + Eq { - fn to_modulus(&self) -> Cow; + fn to_modulus(&self) -> Cow<'_, Value>; #[inline] fn into_modulus(self) -> Value { self.to_modulus().into_owned() @@ -180,7 +180,7 @@ pub trait Modulus: Clone + Eq { impl, Value: Clone + Eq> Modulus for &'_ T { #[inline] - fn to_modulus(&self) -> Cow { + fn to_modulus(&self) -> Cow<'_, Value> { (**self).to_modulus() } } @@ -244,7 +244,7 @@ impl Deref for KnownPrime { impl, Value: Eq + Clone> Modulus for KnownPrime { #[inline] - fn to_modulus(&self) -> Cow { + fn to_modulus(&self) -> Cow<'_, Value> { self.0.to_modulus() } #[inline] @@ -313,7 +313,7 @@ impl Deref for KnownOdd { impl, Value: Eq + Clone> Modulus for KnownOdd { #[inline] - fn to_modulus(&self) -> Cow { + fn to_modulus(&self) -> Cow<'_, Value> { self.0.to_modulus() } #[inline] @@ -350,7 +350,7 @@ macro_rules! impl_int_modulus { ($t:ty, $wide:ty, $to_wide:expr, $from_wide:expr, $from_bigint:ident) => { impl Modulus for $t { #[inline] - fn to_modulus(&self) -> Cow { + fn to_modulus(&self) -> Cow<'_, Self> { Cow::Borrowed(self) } #[inline] @@ -516,7 +516,7 @@ macro_rules! impl_static_modulus { $( impl Modulus<$t> for $n { #[inline] - fn to_modulus(&self) -> Cow<$t> { + fn to_modulus(&self) -> Cow<'_, $t> { Cow::Owned($n.into_modulus()) } #[inline] diff --git a/src/polynomial.rs b/src/polynomial.rs index ded5912..de6fc78 100644 --- a/src/polynomial.rs +++ b/src/polynomial.rs @@ -1190,7 +1190,7 @@ impl Polynomial { pub fn split_out_divisor(self) -> (Vec, T::Divisor) { self.into() } - pub fn iter(&self) -> Iter { + pub fn iter(&self) -> Iter<'_, T> { Iter { elements: self.elements.iter(), divisor: &self.divisor, From 0c162280f5c2f22956a119b2c1cb7f5ee38865f0 Mon Sep 17 00:00:00 2001 From: Tim Leach <4410524+TimLeach635@users.noreply.github.com> Date: Fri, 10 Jul 2026 20:05:30 +0100 Subject: [PATCH 2/3] refactor: fix auto-fixable clippy warnings --- src/algebraic_numbers.rs | 31 ++++------ src/array2d.rs | 20 ++---- src/mod_int.rs | 62 +++++++++---------- src/polynomial.rs | 18 +++--- src/polynomial/add_sub.rs | 28 ++++----- src/polynomial/div_rem.rs | 28 ++++----- src/polynomial/factorization_over_integers.rs | 2 +- src/polynomial/mul.rs | 14 ++--- src/quadratic_numbers.rs | 4 +- src/util.rs | 15 ++--- 10 files changed, 102 insertions(+), 120 deletions(-) diff --git a/src/algebraic_numbers.rs b/src/algebraic_numbers.rs index 7c7c05b..1794792 100644 --- a/src/algebraic_numbers.rs +++ b/src/algebraic_numbers.rs @@ -95,7 +95,7 @@ pub struct RealAlgebraicNumber { impl fmt::Debug for RealAlgebraicNumber { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - debug_real_algebraic_number(&self.data(), f, "RealAlgebraicNumber") + debug_real_algebraic_number(self.data(), f, "RealAlgebraicNumber") } } @@ -251,7 +251,7 @@ impl<'a> IntervalAndSignChanges<'a> { if let Some(sign_changes) = *get_sign_changes(self) { sign_changes } else { - let at = get_interval_bound(&self.interval); + let at = get_interval_bound(self.interval); let sign_changes = sign_changes_at(primitive_sturm_sequence, ValueOrInfinity::Value(&at)); *get_sign_changes(self) = Some(sign_changes); @@ -297,22 +297,18 @@ impl<'a> IntervalAndSignChanges<'a> { impl Deref for IntervalAndSignChanges<'_> { type Target = DyadicFractionInterval; fn deref(&self) -> &DyadicFractionInterval { - &self.interval + self.interval } } impl DerefMut for IntervalAndSignChanges<'_> { fn deref_mut(&mut self) -> &mut DyadicFractionInterval { - &mut self.interval + self.interval } } fn distance(a: usize, b: usize) -> usize { - if a < b { - b - a - } else { - a - b - } + b.abs_diff(a) } #[derive(Debug)] @@ -957,8 +953,7 @@ struct ResultFactor { impl ResultFactor { fn factor(&self) -> Cow<'_, Polynomial> { - self.primitive_sturm_sequence - .get(0) + self.primitive_sturm_sequence.first() .map(Cow::Borrowed) .unwrap_or_else(|| Cow::Owned(Polynomial::zero())) } @@ -1192,7 +1187,7 @@ impl Add for &'_ RealAlgebraicNumber { } } -impl<'a, 'b> Add<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber { +impl Add<&RealAlgebraicNumber> for &RealAlgebraicNumber { type Output = RealAlgebraicNumber; fn add(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber { self.clone().add(rhs) @@ -1260,7 +1255,7 @@ impl Sub for &'_ RealAlgebraicNumber { } } -impl<'a, 'b> Sub<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber { +impl Sub<&RealAlgebraicNumber> for &RealAlgebraicNumber { type Output = RealAlgebraicNumber; fn sub(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber { self.clone().sub(rhs) @@ -1432,7 +1427,7 @@ impl Mul for &'_ RealAlgebraicNumber { } } -impl<'a, 'b> Mul<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber { +impl Mul<&RealAlgebraicNumber> for &RealAlgebraicNumber { type Output = RealAlgebraicNumber; fn mul(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber { self.clone().mul(rhs) @@ -1455,7 +1450,7 @@ impl ExactDivAssign<&'_ RealAlgebraicNumber> for RealAlgebraicNumber { impl AlwaysExactDiv for RealAlgebraicNumber {} impl AlwaysExactDiv<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {} impl AlwaysExactDiv for &'_ RealAlgebraicNumber {} -impl<'a, 'b> AlwaysExactDiv<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {} +impl AlwaysExactDiv<&RealAlgebraicNumber> for &RealAlgebraicNumber {} impl AlwaysExactDivAssign for RealAlgebraicNumber {} impl AlwaysExactDivAssign<&'_ RealAlgebraicNumber> for RealAlgebraicNumber {} @@ -1482,7 +1477,7 @@ impl ExactDiv for &'_ RealAlgebraicNumber { } } -impl<'a, 'b> ExactDiv<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber { +impl ExactDiv<&RealAlgebraicNumber> for &RealAlgebraicNumber { type Output = RealAlgebraicNumber; fn checked_exact_div(self, rhs: &RealAlgebraicNumber) -> Option { self.clone().checked_exact_div(rhs) @@ -1522,7 +1517,7 @@ impl Div<&'_ RealAlgebraicNumber> for RealAlgebraicNumber { } } -impl<'a, 'b> Div<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber { +impl Div<&RealAlgebraicNumber> for &RealAlgebraicNumber { type Output = RealAlgebraicNumber; fn div(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber { self.exact_div(rhs) @@ -1564,7 +1559,7 @@ impl Rem<&'_ RealAlgebraicNumber> for RealAlgebraicNumber { } } -impl<'a, 'b> Rem<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber { +impl Rem<&RealAlgebraicNumber> for &RealAlgebraicNumber { type Output = RealAlgebraicNumber; fn rem(self, rhs: &RealAlgebraicNumber) -> RealAlgebraicNumber { self.clone().rem(rhs) diff --git a/src/array2d.rs b/src/array2d.rs index e73cd8e..979684c 100644 --- a/src/array2d.rs +++ b/src/array2d.rs @@ -19,8 +19,8 @@ mod private { pub(crate) trait SealedData {} impl SealedData for Vec {} - impl<'a, T> SealedData for &'a [T] {} - impl<'a, T> SealedData for &'a mut [T] {} + impl SealedData for &[T] {} + impl SealedData for &mut [T] {} } pub(crate) trait Array2DData: @@ -382,10 +382,10 @@ impl Array2DBase> { self.data } pub(crate) fn data(&self) -> &[T] { - &*self.data + &self.data } pub(crate) fn data_mut(&mut self) -> &mut [T] { - &mut *self.data + &mut self.data } } @@ -498,6 +498,7 @@ pub(crate) type Array2DMutSlice<'a, T> = Array2DBase<&'a mut [T]>; /// column-major 2D positions iterator #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +#[derive(Default)] pub(crate) struct Positions { x: usize, y: usize, @@ -506,17 +507,6 @@ pub(crate) struct Positions { after_rev_y: usize, } -impl Default for Positions { - fn default() -> Self { - Self { - x: 0, - y: 0, - y_size: 0, - rev_x: 0, - after_rev_y: 0, - } - } -} impl Positions { pub(crate) fn new(x_size: usize, y_size: usize) -> Self { diff --git a/src/mod_int.rs b/src/mod_int.rs index c836b61..6273bd0 100644 --- a/src/mod_int.rs +++ b/src/mod_int.rs @@ -664,9 +664,9 @@ impl ModularInteger { } } -impl Into<(V, M)> for ModularInteger { - fn into(self) -> (V, M) { - (self.value, self.modulus) +impl From> for (V, M) { + fn from(val: ModularInteger) -> Self { + (val.value, val.modulus) } } @@ -761,16 +761,16 @@ impl> AddAssign for ModularInteger { } } -impl<'r, V: ModularReduce + Eq, M: Modulus> AddAssign<&'r ModularInteger> +impl> AddAssign<&ModularInteger> for ModularInteger { fn add_assign(&mut self, rhs: &Self) { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); self.value.modular_add_ref_assign(&rhs.value, &self.modulus); } } -impl<'r, V: ModularReduce + Eq, M: Modulus> Add<&'r ModularInteger> +impl> Add<&ModularInteger> for ModularInteger { type Output = ModularInteger; @@ -783,8 +783,8 @@ impl<'r, V: ModularReduce + Eq, M: Modulus> Add<&'r ModularInteger> } } -impl<'l, V: ModularReduce + Eq, M: Modulus> Add> - for &'l ModularInteger +impl> Add> + for &ModularInteger { type Output = ModularInteger; fn add(self, rhs: ModularInteger) -> Self::Output { @@ -796,8 +796,8 @@ impl<'l, V: ModularReduce + Eq, M: Modulus> Add> } } -impl<'l, 'r, V: ModularReduce + Eq, M: Modulus> Add<&'r ModularInteger> - for &'l ModularInteger +impl> Add<&ModularInteger> + for &ModularInteger { type Output = ModularInteger; fn add(self, rhs: &ModularInteger) -> Self::Output { @@ -829,7 +829,7 @@ impl> Neg for ModularInteger { } } -impl<'a, V: ModularReduce + Eq, M: Modulus> Neg for &'a ModularInteger { +impl> Neg for &ModularInteger { type Output = ModularInteger; fn neg(self) -> Self::Output { let value = self.value.modular_neg_ref(&self.modulus); @@ -851,12 +851,12 @@ impl> Sub> for Modular } } -impl<'r, V: ModularReduce + Eq, M: Modulus> Sub<&'r ModularInteger> +impl> Sub<&ModularInteger> for ModularInteger { type Output = ModularInteger; fn sub(self, rhs: &ModularInteger) -> Self::Output { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); ModularInteger { value: self.value.modular_sub_move_ref(&rhs.value, &self.modulus), modulus: self.modulus, @@ -864,8 +864,8 @@ impl<'r, V: ModularReduce + Eq, M: Modulus> Sub<&'r ModularInteger> } } -impl<'l, V: ModularReduce + Eq, M: Modulus> Sub> - for &'l ModularInteger +impl> Sub> + for &ModularInteger { type Output = ModularInteger; fn sub(self, rhs: ModularInteger) -> Self::Output { @@ -877,12 +877,12 @@ impl<'l, V: ModularReduce + Eq, M: Modulus> Sub> } } -impl<'l, 'r, V: ModularReduce + Eq, M: Modulus> Sub<&'r ModularInteger> - for &'l ModularInteger +impl> Sub<&ModularInteger> + for &ModularInteger { type Output = ModularInteger; fn sub(self, rhs: &ModularInteger) -> Self::Output { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); ModularInteger { value: self.value.modular_sub_ref_ref(&rhs.value, &self.modulus), modulus: self.modulus.clone(), @@ -899,11 +899,11 @@ impl> SubAssign> } } -impl<'r, V: ModularReduce + Eq, M: Modulus> SubAssign<&'r ModularInteger> +impl> SubAssign<&ModularInteger> for ModularInteger { fn sub_assign(&mut self, rhs: &Self) { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); self.value.modular_sub_ref_assign(&rhs.value, &self.modulus); } } @@ -938,21 +938,21 @@ impl> MulAssign for ModularInteger { } } -impl<'r, V: ModularReduce + Eq, M: Modulus> MulAssign<&'r ModularInteger> +impl> MulAssign<&ModularInteger> for ModularInteger { fn mul_assign(&mut self, rhs: &Self) { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); self.value.modular_mul_ref_assign(&rhs.value, &self.modulus); } } -impl<'r, V: ModularReduce + Eq, M: Modulus> Mul<&'r ModularInteger> +impl> Mul<&ModularInteger> for ModularInteger { type Output = ModularInteger; fn mul(self, rhs: &ModularInteger) -> Self::Output { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); ModularInteger { value: self.value.modular_mul_move_ref(&rhs.value, &self.modulus), modulus: self.modulus, @@ -960,8 +960,8 @@ impl<'r, V: ModularReduce + Eq, M: Modulus> Mul<&'r ModularInteger> } } -impl<'l, V: ModularReduce + Eq, M: Modulus> Mul> - for &'l ModularInteger +impl> Mul> + for &ModularInteger { type Output = ModularInteger; fn mul(self, rhs: ModularInteger) -> Self::Output { @@ -973,12 +973,12 @@ impl<'l, V: ModularReduce + Eq, M: Modulus> Mul> } } -impl<'l, 'r, V: ModularReduce + Eq, M: Modulus> Mul<&'r ModularInteger> - for &'l ModularInteger +impl> Mul<&ModularInteger> + for &ModularInteger { type Output = ModularInteger; fn mul(self, rhs: &ModularInteger) -> Self::Output { - self.require_matching_moduli(&rhs); + self.require_matching_moduli(rhs); ModularInteger { value: self.value.modular_mul_ref_ref(&rhs.value, &self.modulus), modulus: self.modulus.clone(), @@ -1061,8 +1061,8 @@ impl + ExtendedGCD, M: Modu } } -impl<'a, 'b, V: ModularReduce + Eq + One + Zero + GCD + ExtendedGCD, M: Modulus> - Div<&'a ModularInteger> for &'b ModularInteger +impl + ExtendedGCD, M: Modulus> + Div<&ModularInteger> for &ModularInteger { type Output = ModularInteger; fn div(self, rhs: &ModularInteger) -> ModularInteger { diff --git a/src/polynomial.rs b/src/polynomial.rs index de6fc78..e3b76f2 100644 --- a/src/polynomial.rs +++ b/src/polynomial.rs @@ -139,7 +139,7 @@ pub trait PolynomialCoefficient: coefficient } fn negate_element(element: &mut Self::Element) { - let zero = Self::make_zero_element(Cow::Borrowed(&element)); + let zero = Self::make_zero_element(Cow::Borrowed(element)); *element = -mem::replace(element, zero); } fn mul_element_by_usize(element: Cow, multiplier: usize) -> Self::Element; @@ -167,7 +167,7 @@ pub trait PolynomialCoefficient: } let mut retval = None; loop { - if exponent % 2 != 0 { + if !exponent.is_multiple_of(2) { match &mut retval { None => retval = Some(base.clone()), Some(retval) => *retval *= &base, @@ -187,7 +187,7 @@ pub trait PolynomialCoefficient: } let mut retval = None; loop { - if exponent % 2 != 0 { + if !exponent.is_multiple_of(2) { match &mut retval { None => retval = Some(base.clone()), Some(retval) => *retval *= &base, @@ -207,7 +207,7 @@ pub trait PolynomialCoefficient: } let mut retval = None; loop { - if exponent % 2 != 0 { + if !exponent.is_multiple_of(2) { match &mut retval { None => retval = Some(base.clone()), Some(retval) => *retval *= &base, @@ -494,7 +494,7 @@ impl Mul for &DivisorIsOne { } } -impl<'a, 'b> Mul<&'a DivisorIsOne> for &'b DivisorIsOne { +impl Mul<&DivisorIsOne> for &DivisorIsOne { type Output = DivisorIsOne; fn mul(self, _rhs: &DivisorIsOne) -> DivisorIsOne { DivisorIsOne @@ -545,7 +545,7 @@ impl ExactDiv for &DivisorIsOne { } } -impl<'a, 'b> ExactDiv<&'a DivisorIsOne> for &'b DivisorIsOne { +impl ExactDiv<&DivisorIsOne> for &DivisorIsOne { type Output = DivisorIsOne; fn checked_exact_div(self, _rhs: &DivisorIsOne) -> Option { Some(DivisorIsOne) @@ -1136,9 +1136,9 @@ impl From<(Vec, T::Divisor)> for Polynomia } } -impl Into<(Vec, T::Divisor)> for Polynomial { - fn into(self) -> (Vec, T::Divisor) { - (self.elements, self.divisor) +impl From> for (Vec, T::Divisor) { + fn from(val: Polynomial) -> Self { + (val.elements, val.divisor) } } diff --git a/src/polynomial/add_sub.rs b/src/polynomial/add_sub.rs index a7bc3e2..3408dcb 100644 --- a/src/polynomial/add_sub.rs +++ b/src/polynomial/add_sub.rs @@ -79,7 +79,7 @@ impl AddAssign for Polynomial { } } -impl<'a, T: PolynomialCoefficient> AddAssign<&'a Polynomial> for Polynomial { +impl AddAssign<&Polynomial> for Polynomial { fn add_assign(&mut self, rhs: &Polynomial) { add_sub_assign(self, rhs, AddAssign::::add_assign); } @@ -91,7 +91,7 @@ impl AddAssign for Polynomial { } } -impl<'a, T: PolynomialCoefficient> AddAssign<&'a T> for Polynomial { +impl AddAssign<&T> for Polynomial { fn add_assign(&mut self, rhs: &T) { add_sub_assign_single( self, @@ -109,7 +109,7 @@ impl Add for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Add<&'a Polynomial> for Polynomial { +impl Add<&Polynomial> for Polynomial { type Output = Polynomial; fn add(mut self, rhs: &Polynomial) -> Self::Output { self += rhs; @@ -117,7 +117,7 @@ impl<'a, T: PolynomialCoefficient> Add<&'a Polynomial> for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Add> for &'a Polynomial { +impl Add> for &Polynomial { type Output = Polynomial; fn add(self, mut rhs: Polynomial) -> Self::Output { rhs += self; @@ -125,7 +125,7 @@ impl<'a, T: PolynomialCoefficient> Add> for &'a Polynomial { } } -impl<'a, T: PolynomialCoefficient> Add for &'a Polynomial { +impl Add for &Polynomial { type Output = Polynomial; fn add(self, rhs: Self) -> Self::Output { let mut retval = self.clone(); @@ -142,7 +142,7 @@ impl Add for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Add<&'a T> for Polynomial { +impl Add<&T> for Polynomial { type Output = Polynomial; fn add(mut self, rhs: &T) -> Self::Output { self += rhs; @@ -150,7 +150,7 @@ impl<'a, T: PolynomialCoefficient> Add<&'a T> for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Add for &'a Polynomial { +impl Add for &Polynomial { type Output = Polynomial; fn add(self, rhs: T) -> Self::Output { let mut retval = self.clone(); @@ -193,7 +193,7 @@ impl SubAssign for Polynomial { } } -impl<'a, T: PolynomialCoefficient> SubAssign<&'a Polynomial> for Polynomial { +impl SubAssign<&Polynomial> for Polynomial { fn sub_assign(&mut self, rhs: &Polynomial) { add_sub_assign(self, rhs, SubAssign::::sub_assign); } @@ -205,7 +205,7 @@ impl SubAssign for Polynomial { } } -impl<'a, T: PolynomialCoefficient> SubAssign<&'a T> for Polynomial { +impl SubAssign<&T> for Polynomial { fn sub_assign(&mut self, rhs: &T) { add_sub_assign_single( self, @@ -223,7 +223,7 @@ impl Sub for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Sub<&'a Polynomial> for Polynomial { +impl Sub<&Polynomial> for Polynomial { type Output = Polynomial; fn sub(mut self, rhs: &Polynomial) -> Self::Output { self -= rhs; @@ -231,7 +231,7 @@ impl<'a, T: PolynomialCoefficient> Sub<&'a Polynomial> for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Sub> for &'a Polynomial { +impl Sub> for &Polynomial { type Output = Polynomial; fn sub(self, rhs: Polynomial) -> Self::Output { let mut lhs = self.clone(); @@ -240,7 +240,7 @@ impl<'a, T: PolynomialCoefficient> Sub> for &'a Polynomial { } } -impl<'a, T: PolynomialCoefficient> Sub for &'a Polynomial { +impl Sub for &Polynomial { type Output = Polynomial; fn sub(self, rhs: Self) -> Self::Output { let mut lhs = self.clone(); @@ -257,7 +257,7 @@ impl Sub for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Sub<&'a T> for Polynomial { +impl Sub<&T> for Polynomial { type Output = Polynomial; fn sub(mut self, rhs: &T) -> Self::Output { self -= rhs; @@ -265,7 +265,7 @@ impl<'a, T: PolynomialCoefficient> Sub<&'a T> for Polynomial { } } -impl<'a, T: PolynomialCoefficient> Sub for &'a Polynomial { +impl Sub for &Polynomial { type Output = Polynomial; fn sub(self, rhs: T) -> Self::Output { let mut lhs = self.clone(); diff --git a/src/polynomial/div_rem.rs b/src/polynomial/div_rem.rs index 503832f..204bb5f 100644 --- a/src/polynomial/div_rem.rs +++ b/src/polynomial/div_rem.rs @@ -165,7 +165,7 @@ impl CheckedDiv for Polynomial { let PseudoDivRem { quotient, factor, .. } = self.clone().checked_pseudo_div_rem(rhs)?; - Some(quotient.checked_exact_div(factor)?) + quotient.checked_exact_div(factor) } } @@ -174,7 +174,7 @@ impl CheckedRem for Polynomial { let PseudoDivRem { remainder, factor, .. } = self.clone().checked_pseudo_div_rem(rhs)?; - Some(remainder.checked_exact_div(factor)?) + remainder.checked_exact_div(factor) } } @@ -219,7 +219,7 @@ impl_div_rem!(Polynomial, identity, Polynomial); impl_div_rem!(Polynomial, identity, &'_ Polynomial); impl_div_rem!(&'_ Polynomial, Clone::clone, Polynomial); -impl<'a, 'b, T: PolynomialDivSupported> Div<&'a Polynomial> for &'b Polynomial { +impl Div<&Polynomial> for &Polynomial { type Output = Polynomial; fn div(self, rhs: &Polynomial) -> Polynomial { let PseudoDivRem { @@ -229,7 +229,7 @@ impl<'a, 'b, T: PolynomialDivSupported> Div<&'a Polynomial> for &'b Polynomia } } -impl<'a, 'b, T: PolynomialDivSupported> Rem<&'a Polynomial> for &'b Polynomial { +impl Rem<&Polynomial> for &Polynomial { type Output = Polynomial; fn rem(self, rhs: &Polynomial) -> Polynomial { let PseudoDivRem { @@ -239,8 +239,8 @@ impl<'a, 'b, T: PolynomialDivSupported> Rem<&'a Polynomial> for &'b Polynomia } } -impl<'l, 'r, T: PolynomialCoefficient + for<'a> ExactDiv<&'a T, Output = T>> - ExactDiv<&'r Polynomial> for &'l Polynomial +impl ExactDiv<&'a T, Output = T>> + ExactDiv<&Polynomial> for &Polynomial { type Output = Polynomial; fn exact_div(self, rhs: &Polynomial) -> Polynomial { @@ -359,8 +359,8 @@ impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> Div<&'a } } -impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> Div - for &'a Polynomial +impl ExactDiv<&'b T, Output = T>> Div + for &Polynomial { type Output = Polynomial; fn div(self, rhs: T) -> Polynomial { @@ -368,7 +368,7 @@ impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> Div } } -impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> Div<&'a T> +impl ExactDiv<&'b T, Output = T>> Div<&T> for Polynomial { type Output = Polynomial; @@ -392,7 +392,7 @@ impl ExactDiv<&'a T, Output = T>> DivAssign ExactDiv<&'b T, Output = T>> DivAssign<&'a T> +impl ExactDiv<&'b T, Output = T>> DivAssign<&T> for Polynomial { fn div_assign(&mut self, rhs: &T) { @@ -412,8 +412,8 @@ impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> ExactDi } } -impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> ExactDiv - for &'a Polynomial +impl ExactDiv<&'b T, Output = T>> ExactDiv + for &Polynomial { type Output = Polynomial; fn exact_div(self, rhs: T) -> Polynomial { @@ -424,7 +424,7 @@ impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> ExactDi } } -impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> ExactDiv<&'a T> +impl ExactDiv<&'b T, Output = T>> ExactDiv<&T> for Polynomial { type Output = Polynomial; @@ -457,7 +457,7 @@ impl ExactDiv<&'a T, Output = T>> ExactDivAss } } -impl<'a, T: PolynomialCoefficient + for<'b> ExactDiv<&'b T, Output = T>> ExactDivAssign<&'a T> +impl ExactDiv<&'b T, Output = T>> ExactDivAssign<&T> for Polynomial { fn exact_div_assign(&mut self, rhs: &T) { diff --git a/src/polynomial/factorization_over_integers.rs b/src/polynomial/factorization_over_integers.rs index 587963a..ba4d45e 100644 --- a/src/polynomial/factorization_over_integers.rs +++ b/src/polynomial/factorization_over_integers.rs @@ -427,7 +427,7 @@ impl Polynomial { let converted_polynomial: Polynomial<_> = self .elements .iter() - .map(|coefficient| ModularInteger::::from_bigint(&coefficient, modulus)) + .map(|coefficient| ModularInteger::::from_bigint(coefficient, modulus)) .collect(); debug_assert_eq!(converted_polynomial.degree(), Some(degree)); if converted_polynomial.is_square_free() { diff --git a/src/polynomial/mul.rs b/src/polynomial/mul.rs index 1242bf1..909bf25 100644 --- a/src/polynomial/mul.rs +++ b/src/polynomial/mul.rs @@ -9,7 +9,7 @@ use std::{ ops::{AddAssign, Mul, MulAssign}, }; -impl<'a, T: PolynomialCoefficient> Mul for &'a Polynomial { +impl Mul for &Polynomial { type Output = Polynomial; #[allow(clippy::suspicious_arithmetic_impl)] fn mul(self, rhs: &Polynomial) -> Polynomial { @@ -35,14 +35,14 @@ impl<'a, T: PolynomialCoefficient> Mul for &'a Polynomial { } } -impl<'a, T: PolynomialCoefficient> Mul> for &'a Polynomial { +impl Mul> for &Polynomial { type Output = Polynomial; fn mul(self, rhs: Polynomial) -> Polynomial { self * &rhs } } -impl<'a, T: PolynomialCoefficient> Mul<&'a Polynomial> for Polynomial { +impl Mul<&Polynomial> for Polynomial { type Output = Polynomial; fn mul(self, rhs: &Polynomial) -> Polynomial { &self * rhs @@ -62,7 +62,7 @@ impl MulAssign for Polynomial { } } -impl<'a, T: PolynomialCoefficient> MulAssign<&'a Polynomial> for Polynomial { +impl MulAssign<&Polynomial> for Polynomial { fn mul_assign(&mut self, rhs: &Polynomial) { *self = &*self * rhs; } @@ -115,14 +115,14 @@ impl<'a, T: PolynomialCoefficient> Mul<&'a T> for &'a Polynomial { } } -impl<'a, T: PolynomialCoefficient> Mul for &'a Polynomial { +impl Mul for &Polynomial { type Output = Polynomial; fn mul(self, rhs: T) -> Polynomial { mul_single(Cow::Borrowed(self), Cow::Owned(rhs)) } } -impl<'a, T: PolynomialCoefficient> Mul<&'a T> for Polynomial { +impl Mul<&T> for Polynomial { type Output = Polynomial; fn mul(self, rhs: &T) -> Polynomial { mul_single(Cow::Owned(self), Cow::Borrowed(rhs)) @@ -142,7 +142,7 @@ impl MulAssign for Polynomial { } } -impl<'a, T: PolynomialCoefficient> MulAssign<&'a T> for Polynomial { +impl MulAssign<&T> for Polynomial { fn mul_assign(&mut self, rhs: &T) { mul_assign_single(self, Cow::Borrowed(rhs)); } diff --git a/src/quadratic_numbers.rs b/src/quadratic_numbers.rs index 6df510d..a5c0388 100644 --- a/src/quadratic_numbers.rs +++ b/src/quadratic_numbers.rs @@ -391,8 +391,8 @@ impl RealQuadraticNumber { } let gcd = self .constant_term() - .gcd(&self.linear_term()) - .gcd(&self.quadratic_term()); + .gcd(self.linear_term()) + .gcd(self.quadratic_term()); // gcd is always positive since quadratic_term is not zero debug_assert!(gcd.is_positive()); self.poly.constant_term /= &gcd; diff --git a/src/util.rs b/src/util.rs index bddddd2..f64c165 100644 --- a/src/util.rs +++ b/src/util.rs @@ -512,7 +512,7 @@ pub fn is_prime_u32(v: u32) -> bool { if v == prime { return true; } - if v % prime == 0 { + if v.is_multiple_of(prime) { return false; } if prime * prime > v { @@ -552,7 +552,7 @@ pub fn next_prime_u32(mut v: u32) -> Option { if v >= 4_294_967_291 { return None; } - if v % 2 == 0 { + if v.is_multiple_of(2) { v += 1; } else { v += 2; @@ -583,7 +583,7 @@ fn is_prime_check_small_divisors(v: u128) -> Option { if v == prime { return Some(true); } - if v % prime == 0 { + if v.is_multiple_of(prime) { return Some(false); } if prime * prime > v { @@ -599,7 +599,7 @@ fn is_pseudo_prime_miller_rabin_test_for_base( r: usize, base: &T, ) -> bool { - let x = base.pow_modular_reduce(d, &n); + let x = base.pow_modular_reduce(d, n); if x.is_one() || (n.clone() - x.clone()).is_one() { return false; } @@ -770,8 +770,10 @@ pub trait IsPseudoPrimePower: IsPseudoPrime + FloorLog2 { } #[derive(Copy, Clone, Eq, PartialEq, Debug, Hash)] +#[derive(Default)] pub enum Sign { Negative, + #[default] Positive, } @@ -785,11 +787,6 @@ impl Sign { } } -impl Default for Sign { - fn default() -> Sign { - Sign::Positive - } -} impl Neg for Sign { type Output = Sign; From 1b5921c54031a7eb8107726eea3f52c5d2791256 Mon Sep 17 00:00:00 2001 From: Tim Leach <4410524+TimLeach635@users.noreply.github.com> Date: Fri, 10 Jul 2026 21:19:59 +0100 Subject: [PATCH 3/3] refactor: manually fix remaining warnings --- src/algebraic_numbers.rs | 13 +- src/array2d.rs | 2 +- src/interval_arithmetic.rs | 868 +++++++++--------- src/lattice.rs | 2 +- src/mod_int.rs | 8 +- src/polynomial.rs | 25 +- .../distinct_degree_factorization.rs | 2 +- src/polynomial/div_rem.rs | 15 +- src/polynomial/factorization_over_integers.rs | 8 +- src/polynomial/gcd.rs | 4 - src/polynomial/same_degree_factorization.rs | 2 +- src/python.rs | 8 +- src/traits.rs | 31 +- src/util.rs | 5 +- 14 files changed, 503 insertions(+), 490 deletions(-) diff --git a/src/algebraic_numbers.rs b/src/algebraic_numbers.rs index 1794792..aaaa795 100644 --- a/src/algebraic_numbers.rs +++ b/src/algebraic_numbers.rs @@ -4,7 +4,8 @@ use crate::{ interval_arithmetic::DyadicFractionInterval, polynomial::Polynomial, - traits::{AlwaysExactDiv, AlwaysExactDivAssign, CeilLog2, ExactDiv, ExactDivAssign, FloorLog2}, + traits::{AlwaysExactDiv, AlwaysExactDivAssign, CeilLog2, ExactDiv, ExactDivAssign, + ExactDivAssignError, FloorLog2}, util::{DebugAsDisplay, Sign}, }; use num_bigint::{BigInt, BigUint}; @@ -313,7 +314,6 @@ fn distance(a: usize, b: usize) -> usize { #[derive(Debug)] struct IntervalShrinker<'a> { - minimal_polynomial: &'a Polynomial, primitive_sturm_sequence: Cow<'a, [Polynomial]>, interval: IntervalAndSignChanges<'a>, } @@ -327,12 +327,10 @@ enum IntervalShrinkResult { impl<'a> IntervalShrinker<'a> { #[inline] fn with_primitive_sturm_sequence( - minimal_polynomial: &'a Polynomial, primitive_sturm_sequence: Cow<'a, [Polynomial]>, interval: &'a mut DyadicFractionInterval, ) -> Self { IntervalShrinker { - minimal_polynomial, primitive_sturm_sequence, interval: IntervalAndSignChanges::new(interval), } @@ -342,7 +340,6 @@ impl<'a> IntervalShrinker<'a> { interval: &'a mut DyadicFractionInterval, ) -> Self { Self::with_primitive_sturm_sequence( - minimal_polynomial, Cow::Owned(minimal_polynomial.to_primitive_sturm_sequence()), interval, ) @@ -1435,14 +1432,14 @@ impl Mul<&RealAlgebraicNumber> for &RealAlgebraicNumber { } impl ExactDivAssign for RealAlgebraicNumber { - fn checked_exact_div_assign(&mut self, rhs: RealAlgebraicNumber) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: RealAlgebraicNumber) -> Result<(), ExactDivAssignError> { self.checked_exact_div_assign(&rhs) } } impl ExactDivAssign<&'_ RealAlgebraicNumber> for RealAlgebraicNumber { - fn checked_exact_div_assign(&mut self, rhs: &RealAlgebraicNumber) -> Result<(), ()> { - self.mul_assign(&rhs.checked_recip().ok_or(())?); + fn checked_exact_div_assign(&mut self, rhs: &RealAlgebraicNumber) -> Result<(), ExactDivAssignError> { + self.mul_assign(&rhs.checked_recip().ok_or(ExactDivAssignError)?); Ok(()) } } diff --git a/src/array2d.rs b/src/array2d.rs index 979684c..0bff4e5 100644 --- a/src/array2d.rs +++ b/src/array2d.rs @@ -859,7 +859,7 @@ mod tests { #[test] fn test_iter_mut() { let mut array = Array2DOwned::new_with_positions(2, 3, |x, y| x * 10 + y); - let expected_list: Vec<_> = array.data().iter().copied().collect(); + let expected_list: Vec<_> = array.data().to_vec(); for operation_mask in 0..(1 << expected_list.len()) { println!(); let mut expected = expected_list.iter().copied(); diff --git a/src/interval_arithmetic.rs b/src/interval_arithmetic.rs index 6065524..892f7ac 100644 --- a/src/interval_arithmetic.rs +++ b/src/interval_arithmetic.rs @@ -3,8 +3,8 @@ use crate::{ traits::{ - AlwaysExactDiv, AlwaysExactDivAssign, CeilLog2, ExactDiv, ExactDivAssign, FloorLog2, - IntervalUnion, IntervalUnionAssign, + AlwaysExactDiv, AlwaysExactDivAssign, CeilLog2, ExactDiv, ExactDivAssign, + ExactDivAssignError, FloorLog2, IntervalUnion, IntervalUnionAssign, }, util::DebugAsDisplay, }; @@ -408,12 +408,12 @@ impl DyadicFractionInterval { }, ); } - fn do_checked_div_assign(&mut self, rhs: Cow) -> Result<(), ()> { + fn do_checked_div_assign(&mut self, rhs: Cow) -> Result<(), ExactDivAssignError> { if let Some(recip) = rhs.checked_recip() { *self *= recip; Ok(()) } else { - Err(()) + Err(ExactDivAssignError) } } fn do_div_assign(&mut self, rhs: Cow) { @@ -1003,6 +1003,7 @@ impl AddAssign<&'_ DyadicFractionInterval> for DyadicFractionInterval { impl AddAssign for DyadicFractionInterval { fn add_assign(&mut self, mut rhs: BigInt) { + #![allow(clippy::suspicious_op_assign_impl)] rhs <<= self.log2_denom; self.lower_bound_numer.add_assign(&rhs); self.upper_bound_numer.add_assign(rhs); @@ -1049,6 +1050,7 @@ impl SubAssign<&'_ DyadicFractionInterval> for DyadicFractionInterval { impl SubAssign for DyadicFractionInterval { fn sub_assign(&mut self, mut rhs: BigInt) { + #![allow(clippy::suspicious_op_assign_impl)] rhs <<= self.log2_denom; self.lower_bound_numer.sub_assign(&rhs); self.upper_bound_numer.sub_assign(rhs); @@ -1165,7 +1167,7 @@ impl ExactDivAssign for DyadicFractionInterval { fn exact_div_assign(&mut self, rhs: DyadicFractionInterval) { self.do_div_assign(Cow::Owned(rhs)) } - fn checked_exact_div_assign(&mut self, rhs: DyadicFractionInterval) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: DyadicFractionInterval) -> Result<(), ExactDivAssignError> { self.do_checked_div_assign(Cow::Owned(rhs)) } } @@ -1174,7 +1176,7 @@ impl ExactDivAssign<&'_ DyadicFractionInterval> for DyadicFractionInterval { fn exact_div_assign(&mut self, rhs: &DyadicFractionInterval) { self.do_div_assign(Cow::Borrowed(rhs)) } - fn checked_exact_div_assign(&mut self, rhs: &DyadicFractionInterval) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: &DyadicFractionInterval) -> Result<(), ExactDivAssignError> { self.do_checked_div_assign(Cow::Borrowed(rhs)) } } @@ -1312,31 +1314,31 @@ mod tests { ops::{Deref, DerefMut}, }; - type DFI = DyadicFractionInterval; + type Dfi = DyadicFractionInterval; #[derive(Clone)] - struct SameWrapper>(T); + struct SameWrapper>(T); - impl> Deref for SameWrapper { - type Target = DFI; - fn deref(&self) -> &DFI { + impl> Deref for SameWrapper { + type Target = Dfi; + fn deref(&self) -> &Dfi { self.0.borrow() } } - impl> DerefMut for SameWrapper { - fn deref_mut(&mut self) -> &mut DFI { + impl> DerefMut for SameWrapper { + fn deref_mut(&mut self) -> &mut Dfi { self.0.borrow_mut() } } - impl> PartialEq for SameWrapper { + impl> PartialEq for SameWrapper { fn eq(&self, rhs: &Self) -> bool { self.is_same(rhs) } } - impl> fmt::Debug for SameWrapper { + impl> fmt::Debug for SameWrapper { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Debug::fmt(&**self, f) } @@ -1369,190 +1371,190 @@ mod tests { #[test] fn test_from_ratio_range() { assert_same!( - DFI::from_ratio_range(r(2, 3), r(5, 7), 8), - DFI::new(bi(170), bi(183), 8) + Dfi::from_ratio_range(r(2, 3), r(5, 7), 8), + Dfi::new(bi(170), bi(183), 8) ); assert_same!( - DFI::from_ratio_range(ri(-1), r(-5, 7), 8), - DFI::new(bi(-256), bi(-182), 8) + Dfi::from_ratio_range(ri(-1), r(-5, 7), 8), + Dfi::new(bi(-256), bi(-182), 8) ); assert_same!( - DFI::from_ratio_range(r(5, 32), r(45, 32), 5), - DFI::new(bi(5), bi(45), 5) + Dfi::from_ratio_range(r(5, 32), r(45, 32), 5), + Dfi::new(bi(5), bi(45), 5) ); assert_same!( - DFI::from_ratio_range(r(7, 32), r(8, 32), 5), - DFI::new(bi(7), bi(8), 5) + Dfi::from_ratio_range(r(7, 32), r(8, 32), 5), + Dfi::new(bi(7), bi(8), 5) ); } #[test] fn test_from_ratio() { - assert_same!(DFI::from_ratio(r(2, 3), 8), DFI::new(bi(170), bi(171), 8)); + assert_same!(Dfi::from_ratio(r(2, 3), 8), Dfi::new(bi(170), bi(171), 8)); assert_same!( - DFI::from_ratio(r(-2, 3), 8), - DFI::new(bi(-171), bi(-170), 8) + Dfi::from_ratio(r(-2, 3), 8), + Dfi::new(bi(-171), bi(-170), 8) ); - assert_same!(DFI::from_ratio(r(1, 8), 8), DFI::new(bi(32), bi(32), 8)); + assert_same!(Dfi::from_ratio(r(1, 8), 8), Dfi::new(bi(32), bi(32), 8)); } #[test] fn test_convert_log2_denom() { assert_same!( - DFI::new(bi(1), bi(2), 0).into_converted_log2_denom(2), - DFI::new(bi(4), bi(8), 2) + Dfi::new(bi(1), bi(2), 0).into_converted_log2_denom(2), + Dfi::new(bi(4), bi(8), 2) ); assert_same!( - DFI::new(bi(-2), bi(-1), 0).into_converted_log2_denom(2), - DFI::new(bi(-8), bi(-4), 2) + Dfi::new(bi(-2), bi(-1), 0).into_converted_log2_denom(2), + Dfi::new(bi(-8), bi(-4), 2) ); assert_same!( - DFI::new(bi(4), bi(8), 2).into_converted_log2_denom(0), - DFI::new(bi(1), bi(2), 0) + Dfi::new(bi(4), bi(8), 2).into_converted_log2_denom(0), + Dfi::new(bi(1), bi(2), 0) ); assert_same!( - DFI::new(bi(7), bi(7), 2).into_converted_log2_denom(0), - DFI::new(bi(1), bi(2), 0) + Dfi::new(bi(7), bi(7), 2).into_converted_log2_denom(0), + Dfi::new(bi(1), bi(2), 0) ); assert_same!( - DFI::new(bi(6), bi(6), 2).into_converted_log2_denom(0), - DFI::new(bi(1), bi(2), 0) + Dfi::new(bi(6), bi(6), 2).into_converted_log2_denom(0), + Dfi::new(bi(1), bi(2), 0) ); assert_same!( - DFI::new(bi(5), bi(5), 2).into_converted_log2_denom(0), - DFI::new(bi(1), bi(2), 0) + Dfi::new(bi(5), bi(5), 2).into_converted_log2_denom(0), + Dfi::new(bi(1), bi(2), 0) ); assert_same!( - DFI::new(bi(4), bi(4), 2).into_converted_log2_denom(0), - DFI::new(bi(1), bi(1), 0) + Dfi::new(bi(4), bi(4), 2).into_converted_log2_denom(0), + Dfi::new(bi(1), bi(1), 0) ); assert_same!( - DFI::new(bi(8), bi(8), 2).into_converted_log2_denom(0), - DFI::new(bi(2), bi(2), 0) + Dfi::new(bi(8), bi(8), 2).into_converted_log2_denom(0), + Dfi::new(bi(2), bi(2), 0) ); assert_same!( - DFI::new(bi(-8), bi(-4), 2).into_converted_log2_denom(0), - DFI::new(bi(-2), bi(-1), 0) + Dfi::new(bi(-8), bi(-4), 2).into_converted_log2_denom(0), + Dfi::new(bi(-2), bi(-1), 0) ); assert_same!( - DFI::new(bi(-7), bi(-7), 2).into_converted_log2_denom(0), - DFI::new(bi(-2), bi(-1), 0) + Dfi::new(bi(-7), bi(-7), 2).into_converted_log2_denom(0), + Dfi::new(bi(-2), bi(-1), 0) ); assert_same!( - DFI::new(bi(-6), bi(-6), 2).into_converted_log2_denom(0), - DFI::new(bi(-2), bi(-1), 0) + Dfi::new(bi(-6), bi(-6), 2).into_converted_log2_denom(0), + Dfi::new(bi(-2), bi(-1), 0) ); assert_same!( - DFI::new(bi(-5), bi(-5), 2).into_converted_log2_denom(0), - DFI::new(bi(-2), bi(-1), 0) + Dfi::new(bi(-5), bi(-5), 2).into_converted_log2_denom(0), + Dfi::new(bi(-2), bi(-1), 0) ); assert_same!( - DFI::new(bi(-4), bi(-4), 2).into_converted_log2_denom(0), - DFI::new(bi(-1), bi(-1), 0) + Dfi::new(bi(-4), bi(-4), 2).into_converted_log2_denom(0), + Dfi::new(bi(-1), bi(-1), 0) ); assert_same!( - DFI::new(bi(-8), bi(-8), 2).into_converted_log2_denom(0), - DFI::new(bi(-2), bi(-2), 0) + Dfi::new(bi(-8), bi(-8), 2).into_converted_log2_denom(0), + Dfi::new(bi(-2), bi(-2), 0) ); } #[test] fn test_square() { assert_same!( - DFI::new(bi(1), bi(2), 0).into_square(), - DFI::new(bi(1), bi(4), 0) + Dfi::new(bi(1), bi(2), 0).into_square(), + Dfi::new(bi(1), bi(4), 0) ); assert_same!( - DFI::new(bi(4), bi(5), 0).into_square(), - DFI::new(bi(16), bi(25), 0) + Dfi::new(bi(4), bi(5), 0).into_square(), + Dfi::new(bi(16), bi(25), 0) ); assert_same!( - DFI::new(bi(1), bi(1), 4).into_square(), - DFI::new(bi(0), bi(1), 4) + Dfi::new(bi(1), bi(1), 4).into_square(), + Dfi::new(bi(0), bi(1), 4) ); assert_same!( - DFI::new(bi(16), bi(16), 4).into_square(), - DFI::new(bi(16), bi(16), 4) + Dfi::new(bi(16), bi(16), 4).into_square(), + Dfi::new(bi(16), bi(16), 4) ); assert_same!( - DFI::new(bi(15), bi(15), 4).into_square(), - DFI::new(bi(14), bi(15), 4) + Dfi::new(bi(15), bi(15), 4).into_square(), + Dfi::new(bi(14), bi(15), 4) ); assert_same!( - DFI::new(bi(15), bi(15), 4).into_square(), - DFI::new(bi(14), bi(15), 4) + Dfi::new(bi(15), bi(15), 4).into_square(), + Dfi::new(bi(14), bi(15), 4) ); assert_same!( - DFI::new(bi(-16), bi(16), 4).into_square(), - DFI::new(bi(0), bi(16), 4) + Dfi::new(bi(-16), bi(16), 4).into_square(), + Dfi::new(bi(0), bi(16), 4) ); assert_same!( - DFI::new(bi(-4), bi(5), 0).into_square(), - DFI::new(bi(0), bi(25), 0) + Dfi::new(bi(-4), bi(5), 0).into_square(), + Dfi::new(bi(0), bi(25), 0) ); assert_same!( - DFI::new(bi(-5), bi(4), 0).into_square(), - DFI::new(bi(0), bi(25), 0) + Dfi::new(bi(-5), bi(4), 0).into_square(), + Dfi::new(bi(0), bi(25), 0) ); assert_same!( - DFI::new(bi(-16), bi(-16), 4).into_square(), - DFI::new(bi(16), bi(16), 4) + Dfi::new(bi(-16), bi(-16), 4).into_square(), + Dfi::new(bi(16), bi(16), 4) ); assert_same!( - DFI::new(bi(-5), bi(-4), 0).into_square(), - DFI::new(bi(16), bi(25), 0) + Dfi::new(bi(-5), bi(-4), 0).into_square(), + Dfi::new(bi(16), bi(25), 0) ); } #[test] fn test_sqrt() { assert_same!( - DFI::new(bi(0), bi(1), 8).into_sqrt(), - DFI::new(bi(0), bi(16), 8) + Dfi::new(bi(0), bi(1), 8).into_sqrt(), + Dfi::new(bi(0), bi(16), 8) ); assert_same!( - DFI::new(bi(1), bi(2), 8).into_sqrt(), - DFI::new(bi(16), bi(23), 8) + Dfi::new(bi(1), bi(2), 8).into_sqrt(), + Dfi::new(bi(16), bi(23), 8) ); assert_same!( - DFI::new(bi(2), bi(3), 8).into_sqrt(), - DFI::new(bi(22), bi(28), 8) + Dfi::new(bi(2), bi(3), 8).into_sqrt(), + Dfi::new(bi(22), bi(28), 8) ); assert_same!( - DFI::new(bi(3), bi(4), 8).into_sqrt(), - DFI::new(bi(27), bi(32), 8) + Dfi::new(bi(3), bi(4), 8).into_sqrt(), + Dfi::new(bi(27), bi(32), 8) ); assert_same!( - DFI::new(bi(4), bi(5), 8).into_sqrt(), - DFI::new(bi(32), bi(36), 8) + Dfi::new(bi(4), bi(5), 8).into_sqrt(), + Dfi::new(bi(32), bi(36), 8) ); assert_same!( - DFI::new(bi(512), bi(512), 8).into_sqrt(), - DFI::new(bi(362), bi(363), 8) + Dfi::new(bi(512), bi(512), 8).into_sqrt(), + Dfi::new(bi(362), bi(363), 8) ); } #[test] fn test_arithmetic_geometric_mean() { assert_same!( - DFI::new(bi(0), bi(1), 8).into_arithmetic_geometric_mean(DFI::new(bi(0), bi(1), 8)), - DFI::new(bi(0), bi(1), 8), + Dfi::new(bi(0), bi(1), 8).into_arithmetic_geometric_mean(Dfi::new(bi(0), bi(1), 8)), + Dfi::new(bi(0), bi(1), 8), ); assert_same!( - DFI::new(bi(256), bi(256), 8).into_arithmetic_geometric_mean(DFI::new( + Dfi::new(bi(256), bi(256), 8).into_arithmetic_geometric_mean(Dfi::new( bi(512), bi(512), 8 )), - DFI::new(bi(372), bi(374), 8), + Dfi::new(bi(372), bi(374), 8), ); assert_same!( - DFI::new(bi(1) << 64, bi(1) << 64, 64).into_arithmetic_geometric_mean(DFI::new( + Dfi::new(bi(1) << 64, bi(1) << 64, 64).into_arithmetic_geometric_mean(Dfi::new( bi(2) << 64, bi(2) << 64, 64 )), - DFI::new( + Dfi::new( bi(26_873_051_318_597_756_702), bi(26_873_051_318_597_756_707), 64 @@ -1563,11 +1565,11 @@ mod tests { #[test] fn test_debug() { assert_eq!( - &format!("{:?}", DFI::new(bi(-123), bi(456), 789)), + &format!("{:?}", Dfi::new(bi(-123), bi(456), 789)), "DyadicFractionInterval { lower_bound_numer: -123, upper_bound_numer: 456, log2_denom: 789 }", ); assert_eq!( - &format!("{:#?}", DFI::new(bi(-123), bi(456), 789)), + &format!("{:#?}", Dfi::new(bi(-123), bi(456), 789)), "DyadicFractionInterval {\n lower_bound_numer: -123,\n upper_bound_numer: 456,\n log2_denom: 789,\n}", ); } @@ -1575,14 +1577,14 @@ mod tests { #[test] fn test_display() { assert_eq!( - &format!("{}", DFI::new(bi(-123), bi(456), 789)), + &format!("{}", Dfi::new(bi(-123), bi(456), 789)), "[-123 / 2^789, 456 / 2^789]", ); } #[test] fn test_interval_union() { - fn test_case(lhs: DFI, rhs: DFI, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Dfi, expected: Dfi) { test_op_helper( SameWrapper(lhs.clone()), SameWrapper(rhs.clone()), @@ -1607,60 +1609,60 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(3), bi(97), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(3), bi(97), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(3), bi(194), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(3), bi(194), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(6), bi(97), 1), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(6), bi(97), 1), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(3), bi(97), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(3), bi(97), 1), ); test_case( - DFI::new(bi(-3), bi(5), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-3), bi(97), 0), + Dfi::new(bi(-3), bi(5), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-3), bi(97), 0), ); test_case( - DFI::new(bi(-5), bi(3), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-5), bi(97), 0), + Dfi::new(bi(-5), bi(3), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-5), bi(97), 0), ); test_case( - DFI::new(bi(-5), bi(-3), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-5), bi(97), 0), + Dfi::new(bi(-5), bi(-3), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-5), bi(97), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(-17), bi(97), 1), - DFI::new(bi(-17), bi(97), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(-17), bi(97), 1), + Dfi::new(bi(-17), bi(97), 1), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(-97), bi(17), 1), - DFI::new(bi(-97), bi(17), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(-97), bi(17), 1), + Dfi::new(bi(-97), bi(17), 1), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(-97), bi(-17), 1), - DFI::new(bi(-97), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(-97), bi(-17), 1), + Dfi::new(bi(-97), bi(5), 1), ); } #[test] fn test_add() { - fn test_case(lhs: DFI, rhs: DFI, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Dfi, expected: Dfi) { test_op_helper( SameWrapper(lhs.clone()), SameWrapper(rhs.clone()), @@ -1685,30 +1687,30 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(20), bi(102), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(20), bi(102), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(37), bi(199), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(37), bi(199), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(23), bi(107), 1), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(23), bi(107), 1), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(20), bi(102), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(20), bi(102), 1), ); } #[test] fn test_add_int() { - fn test_case(lhs: DFI, rhs: BigInt, expected: DFI) { + fn test_case(lhs: Dfi, rhs: BigInt, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -1722,20 +1724,20 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), bi(23), - DFI::new(bi(26), bi(28), 0), + Dfi::new(bi(26), bi(28), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), bi(23), - DFI::new(bi(49), bi(51), 1), + Dfi::new(bi(49), bi(51), 1), ); } #[test] fn test_add_ratio() { - fn test_case(lhs: DFI, rhs: Ratio, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Ratio, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -1749,30 +1751,30 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(7, 5), - DFI::new(bi(4), bi(7), 0), + Dfi::new(bi(4), bi(7), 0), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(-7, 5), - DFI::new(bi(1), bi(4), 0), + Dfi::new(bi(1), bi(4), 0), ); test_case( - DFI::new(bi(3), bi(5), 8), + Dfi::new(bi(3), bi(5), 8), r(7, 5), - DFI::new(bi(361), bi(364), 8), + Dfi::new(bi(361), bi(364), 8), ); test_case( - DFI::new(bi(3), bi(5), 8), + Dfi::new(bi(3), bi(5), 8), r(-7, 5), - DFI::new(bi(-356), bi(-353), 8), + Dfi::new(bi(-356), bi(-353), 8), ); } #[test] fn test_sub() { - fn test_case(lhs: DFI, rhs: DFI, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Dfi, expected: Dfi) { test_op_helper( SameWrapper(lhs.clone()), SameWrapper(rhs.clone()), @@ -1797,30 +1799,30 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-94), bi(-12), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-94), bi(-12), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-191), bi(-29), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-191), bi(-29), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(-91), bi(-7), 1), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(-91), bi(-7), 1), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(-94), bi(-12), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(-94), bi(-12), 1), ); } #[test] fn test_sub_int() { - fn test_case(lhs: DFI, rhs: BigInt, expected: DFI) { + fn test_case(lhs: Dfi, rhs: BigInt, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -1834,20 +1836,20 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), bi(23), - DFI::new(bi(-20), bi(-18), 0), + Dfi::new(bi(-20), bi(-18), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), bi(23), - DFI::new(bi(-43), bi(-41), 1), + Dfi::new(bi(-43), bi(-41), 1), ); } #[test] fn test_sub_ratio() { - fn test_case(lhs: DFI, rhs: Ratio, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Ratio, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -1861,30 +1863,30 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(7, 5), - DFI::new(bi(1), bi(4), 0), + Dfi::new(bi(1), bi(4), 0), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(-7, 5), - DFI::new(bi(4), bi(7), 0), + Dfi::new(bi(4), bi(7), 0), ); test_case( - DFI::new(bi(3), bi(5), 8), + Dfi::new(bi(3), bi(5), 8), r(7, 5), - DFI::new(bi(-356), bi(-353), 8), + Dfi::new(bi(-356), bi(-353), 8), ); test_case( - DFI::new(bi(3), bi(5), 8), + Dfi::new(bi(3), bi(5), 8), r(-7, 5), - DFI::new(bi(361), bi(364), 8), + Dfi::new(bi(361), bi(364), 8), ); } #[test] fn test_mul() { - fn test_case(lhs: DFI, rhs: DFI, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Dfi, expected: Dfi) { test_op_helper( SameWrapper(lhs.clone()), SameWrapper(rhs.clone()), @@ -1909,105 +1911,105 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(51), bi(485), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(51), bi(485), 0), ); test_case( - DFI::new(bi(-3), bi(5), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-291), bi(485), 0), + Dfi::new(bi(-3), bi(5), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-291), bi(485), 0), ); test_case( - DFI::new(bi(-5), bi(3), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-485), bi(291), 0), + Dfi::new(bi(-5), bi(3), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-485), bi(291), 0), ); test_case( - DFI::new(bi(-5), bi(-3), 0), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(-485), bi(-51), 0), + Dfi::new(bi(-5), bi(-3), 0), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(-485), bi(-51), 0), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(-17), bi(97), 0), - DFI::new(bi(-85), bi(485), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(-17), bi(97), 0), + Dfi::new(bi(-85), bi(485), 0), ); test_case( - DFI::new(bi(-3), bi(5), 0), - DFI::new(bi(-17), bi(97), 0), - DFI::new(bi(-291), bi(485), 0), + Dfi::new(bi(-3), bi(5), 0), + Dfi::new(bi(-17), bi(97), 0), + Dfi::new(bi(-291), bi(485), 0), ); test_case( - DFI::new(bi(-5), bi(3), 0), - DFI::new(bi(-17), bi(97), 0), - DFI::new(bi(-485), bi(291), 0), + Dfi::new(bi(-5), bi(3), 0), + Dfi::new(bi(-17), bi(97), 0), + Dfi::new(bi(-485), bi(291), 0), ); test_case( - DFI::new(bi(-5), bi(-3), 0), - DFI::new(bi(-17), bi(97), 0), - DFI::new(bi(-485), bi(85), 0), + Dfi::new(bi(-5), bi(-3), 0), + Dfi::new(bi(-17), bi(97), 0), + Dfi::new(bi(-485), bi(85), 0), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(-97), bi(17), 0), - DFI::new(bi(-485), bi(85), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(-97), bi(17), 0), + Dfi::new(bi(-485), bi(85), 0), ); test_case( - DFI::new(bi(-3), bi(5), 0), - DFI::new(bi(-97), bi(17), 0), - DFI::new(bi(-485), bi(291), 0), + Dfi::new(bi(-3), bi(5), 0), + Dfi::new(bi(-97), bi(17), 0), + Dfi::new(bi(-485), bi(291), 0), ); test_case( - DFI::new(bi(-5), bi(3), 0), - DFI::new(bi(-97), bi(17), 0), - DFI::new(bi(-291), bi(485), 0), + Dfi::new(bi(-5), bi(3), 0), + Dfi::new(bi(-97), bi(17), 0), + Dfi::new(bi(-291), bi(485), 0), ); test_case( - DFI::new(bi(-5), bi(-3), 0), - DFI::new(bi(-97), bi(17), 0), - DFI::new(bi(-85), bi(485), 0), + Dfi::new(bi(-5), bi(-3), 0), + Dfi::new(bi(-97), bi(17), 0), + Dfi::new(bi(-85), bi(485), 0), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(-97), bi(-17), 0), - DFI::new(bi(-485), bi(-51), 0), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(-97), bi(-17), 0), + Dfi::new(bi(-485), bi(-51), 0), ); test_case( - DFI::new(bi(-3), bi(5), 0), - DFI::new(bi(-97), bi(-17), 0), - DFI::new(bi(-485), bi(291), 0), + Dfi::new(bi(-3), bi(5), 0), + Dfi::new(bi(-97), bi(-17), 0), + Dfi::new(bi(-485), bi(291), 0), ); test_case( - DFI::new(bi(-5), bi(3), 0), - DFI::new(bi(-97), bi(-17), 0), - DFI::new(bi(-291), bi(485), 0), + Dfi::new(bi(-5), bi(3), 0), + Dfi::new(bi(-97), bi(-17), 0), + Dfi::new(bi(-291), bi(485), 0), ); test_case( - DFI::new(bi(-5), bi(-3), 0), - DFI::new(bi(-97), bi(-17), 0), - DFI::new(bi(51), bi(485), 0), + Dfi::new(bi(-5), bi(-3), 0), + Dfi::new(bi(-97), bi(-17), 0), + Dfi::new(bi(51), bi(485), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 0), - DFI::new(bi(51), bi(485), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 0), + Dfi::new(bi(51), bi(485), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(51), bi(485), 1), + Dfi::new(bi(3), bi(5), 0), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(51), bi(485), 1), ); test_case( - DFI::new(bi(3), bi(5), 1), - DFI::new(bi(17), bi(97), 1), - DFI::new(bi(25), bi(243), 1), + Dfi::new(bi(3), bi(5), 1), + Dfi::new(bi(17), bi(97), 1), + Dfi::new(bi(25), bi(243), 1), ); } #[test] fn test_mul_int() { - fn test_case(lhs: DFI, rhs: BigInt, expected: DFI) { + fn test_case(lhs: Dfi, rhs: BigInt, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -2021,30 +2023,30 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), bi(23), - DFI::new(bi(69), bi(115), 0), + Dfi::new(bi(69), bi(115), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), bi(23), - DFI::new(bi(69), bi(115), 1), + Dfi::new(bi(69), bi(115), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), bi(-23), - DFI::new(bi(-115), bi(-69), 0), + Dfi::new(bi(-115), bi(-69), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), bi(-23), - DFI::new(bi(-115), bi(-69), 1), + Dfi::new(bi(-115), bi(-69), 1), ); } #[test] fn test_mul_ratio() { - fn test_case(lhs: DFI, rhs: Ratio, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Ratio, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -2058,36 +2060,36 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(23, 7), - DFI::new(bi(9), bi(17), 0), + Dfi::new(bi(9), bi(17), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), r(23, 7), - DFI::new(bi(9), bi(17), 1), + Dfi::new(bi(9), bi(17), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(-23, 7), - DFI::new(bi(-17), bi(-9), 0), + Dfi::new(bi(-17), bi(-9), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), r(-23, 7), - DFI::new(bi(-17), bi(-9), 1), + Dfi::new(bi(-17), bi(-9), 1), ); - test_case(DFI::new(bi(3), bi(5), 0), ri(3), DFI::new(bi(9), bi(15), 0)); + test_case(Dfi::new(bi(3), bi(5), 0), ri(3), Dfi::new(bi(9), bi(15), 0)); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), ri(-3), - DFI::new(bi(-15), bi(-9), 0), + Dfi::new(bi(-15), bi(-9), 0), ); } #[test] fn test_div() { - fn test_case(lhs: DFI, rhs: DFI, expected: Option) { + fn test_case(lhs: Dfi, rhs: Dfi, expected: Option) { test_checked_op_helper( SameWrapper(lhs), SameWrapper(rhs), @@ -2101,89 +2103,89 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 8), - DFI::new(bi(17), bi(97), 8), - Some(DFI::new(bi(7), bi(76), 8)), + Dfi::new(bi(3), bi(5), 8), + Dfi::new(bi(17), bi(97), 8), + Some(Dfi::new(bi(7), bi(76), 8)), ); test_case( - DFI::new(bi(-3), bi(5), 8), - DFI::new(bi(17), bi(97), 8), - Some(DFI::new(bi(-46), bi(76), 8)), + Dfi::new(bi(-3), bi(5), 8), + Dfi::new(bi(17), bi(97), 8), + Some(Dfi::new(bi(-46), bi(76), 8)), ); test_case( - DFI::new(bi(-5), bi(3), 8), - DFI::new(bi(17), bi(97), 8), - Some(DFI::new(bi(-76), bi(46), 8)), + Dfi::new(bi(-5), bi(3), 8), + Dfi::new(bi(17), bi(97), 8), + Some(Dfi::new(bi(-76), bi(46), 8)), ); test_case( - DFI::new(bi(-5), bi(-3), 8), - DFI::new(bi(17), bi(97), 8), - Some(DFI::new(bi(-76), bi(-7), 8)), + Dfi::new(bi(-5), bi(-3), 8), + Dfi::new(bi(17), bi(97), 8), + Some(Dfi::new(bi(-76), bi(-7), 8)), ); test_case( - DFI::new(bi(3), bi(5), 8), - DFI::new(bi(-17), bi(97), 8), + Dfi::new(bi(3), bi(5), 8), + Dfi::new(bi(-17), bi(97), 8), None, ); test_case( - DFI::new(bi(-3), bi(5), 8), - DFI::new(bi(-17), bi(97), 8), + Dfi::new(bi(-3), bi(5), 8), + Dfi::new(bi(-17), bi(97), 8), None, ); test_case( - DFI::new(bi(-5), bi(3), 8), - DFI::new(bi(-17), bi(97), 8), + Dfi::new(bi(-5), bi(3), 8), + Dfi::new(bi(-17), bi(97), 8), None, ); test_case( - DFI::new(bi(-5), bi(-3), 8), - DFI::new(bi(-17), bi(97), 8), + Dfi::new(bi(-5), bi(-3), 8), + Dfi::new(bi(-17), bi(97), 8), None, ); test_case( - DFI::new(bi(3), bi(5), 8), - DFI::new(bi(-97), bi(17), 8), + Dfi::new(bi(3), bi(5), 8), + Dfi::new(bi(-97), bi(17), 8), None, ); test_case( - DFI::new(bi(-3), bi(5), 8), - DFI::new(bi(-97), bi(17), 8), + Dfi::new(bi(-3), bi(5), 8), + Dfi::new(bi(-97), bi(17), 8), None, ); test_case( - DFI::new(bi(-5), bi(3), 8), - DFI::new(bi(-97), bi(17), 8), + Dfi::new(bi(-5), bi(3), 8), + Dfi::new(bi(-97), bi(17), 8), None, ); test_case( - DFI::new(bi(-5), bi(-3), 8), - DFI::new(bi(-97), bi(17), 8), + Dfi::new(bi(-5), bi(-3), 8), + Dfi::new(bi(-97), bi(17), 8), None, ); test_case( - DFI::new(bi(3), bi(5), 8), - DFI::new(bi(-97), bi(-17), 8), - Some(DFI::new(bi(-76), bi(-7), 8)), + Dfi::new(bi(3), bi(5), 8), + Dfi::new(bi(-97), bi(-17), 8), + Some(Dfi::new(bi(-76), bi(-7), 8)), ); test_case( - DFI::new(bi(-3), bi(5), 8), - DFI::new(bi(-97), bi(-17), 8), - Some(DFI::new(bi(-76), bi(46), 8)), + Dfi::new(bi(-3), bi(5), 8), + Dfi::new(bi(-97), bi(-17), 8), + Some(Dfi::new(bi(-76), bi(46), 8)), ); test_case( - DFI::new(bi(-5), bi(3), 8), - DFI::new(bi(-97), bi(-17), 8), - Some(DFI::new(bi(-46), bi(76), 8)), + Dfi::new(bi(-5), bi(3), 8), + Dfi::new(bi(-97), bi(-17), 8), + Some(Dfi::new(bi(-46), bi(76), 8)), ); test_case( - DFI::new(bi(-5), bi(-3), 8), - DFI::new(bi(-97), bi(-17), 8), - Some(DFI::new(bi(7), bi(76), 8)), + Dfi::new(bi(-5), bi(-3), 8), + Dfi::new(bi(-97), bi(-17), 8), + Some(Dfi::new(bi(7), bi(76), 8)), ); test_case( - DFI::from_int(bi(1), 64), - DFI::from_int(bi(3), 64), - Some(DFI::new( + Dfi::from_int(bi(1), 64), + Dfi::from_int(bi(3), 64), + Some(Dfi::new( bi(6_148_914_691_236_517_205), bi(6_148_914_691_236_517_206), 64, @@ -2193,7 +2195,7 @@ mod tests { #[test] fn test_div_int() { - fn test_case(lhs: DFI, rhs: BigInt, expected: DFI) { + fn test_case(lhs: Dfi, rhs: BigInt, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -2207,40 +2209,40 @@ mod tests { ); } test_case( - DFI::new(bi(35), bi(72), 0), + Dfi::new(bi(35), bi(72), 0), bi(11), - DFI::new(bi(3), bi(7), 0), + Dfi::new(bi(3), bi(7), 0), ); test_case( - DFI::new(bi(35), bi(72), 1), + Dfi::new(bi(35), bi(72), 1), bi(11), - DFI::new(bi(3), bi(7), 1), + Dfi::new(bi(3), bi(7), 1), ); test_case( - DFI::new(bi(35), bi(72), 0), + Dfi::new(bi(35), bi(72), 0), bi(-11), - DFI::new(bi(-7), bi(-3), 0), + Dfi::new(bi(-7), bi(-3), 0), ); test_case( - DFI::new(bi(35), bi(72), 1), + Dfi::new(bi(35), bi(72), 1), bi(-11), - DFI::new(bi(-7), bi(-3), 1), + Dfi::new(bi(-7), bi(-3), 1), ); test_case( - DFI::new(bi(35), bi(72), 0), + Dfi::new(bi(35), bi(72), 0), bi(3), - DFI::new(bi(11), bi(24), 0), + Dfi::new(bi(11), bi(24), 0), ); test_case( - DFI::new(bi(35), bi(72), 0), + Dfi::new(bi(35), bi(72), 0), bi(5), - DFI::new(bi(7), bi(15), 0), + Dfi::new(bi(7), bi(15), 0), ); } #[test] fn test_div_ratio() { - fn test_case(lhs: DFI, rhs: Ratio, expected: DFI) { + fn test_case(lhs: Dfi, rhs: Ratio, expected: Dfi) { test_op_helper( SameWrapper(lhs), rhs, @@ -2254,40 +2256,40 @@ mod tests { ); } test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(7, 23), - DFI::new(bi(9), bi(17), 0), + Dfi::new(bi(9), bi(17), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), r(7, 23), - DFI::new(bi(9), bi(17), 1), + Dfi::new(bi(9), bi(17), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(-7, 23), - DFI::new(bi(-17), bi(-9), 0), + Dfi::new(bi(-17), bi(-9), 0), ); test_case( - DFI::new(bi(3), bi(5), 1), + Dfi::new(bi(3), bi(5), 1), r(-7, 23), - DFI::new(bi(-17), bi(-9), 1), + Dfi::new(bi(-17), bi(-9), 1), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(1, 3), - DFI::new(bi(9), bi(15), 0), + Dfi::new(bi(9), bi(15), 0), ); test_case( - DFI::new(bi(3), bi(5), 0), + Dfi::new(bi(3), bi(5), 0), r(-1, 3), - DFI::new(bi(-15), bi(-9), 0), + Dfi::new(bi(-15), bi(-9), 0), ); } #[test] fn test_pow() { - fn test_case(lhs: DFI, rhs: i64, expected: DFI) { + fn test_case(lhs: Dfi, rhs: i64, expected: Dfi) { test_unary_op_helper( SameWrapper(lhs), &SameWrapper(expected), @@ -2295,104 +2297,104 @@ mod tests { |SameWrapper(a)| SameWrapper(a.pow(rhs)), ); } - test_case(DFI::new(bi(5), bi(7), 0), 0, DFI::new(bi(1), bi(1), 0)); - test_case(DFI::new(bi(5), bi(7), 0), 1, DFI::new(bi(5), bi(7), 0)); - test_case(DFI::new(bi(5), bi(7), 0), 2, DFI::new(bi(25), bi(49), 0)); - test_case(DFI::new(bi(5), bi(7), 0), 3, DFI::new(bi(125), bi(343), 0)); - test_case(DFI::new(bi(5), bi(7), 0), 4, DFI::new(bi(625), bi(2401), 0)); + test_case(Dfi::new(bi(5), bi(7), 0), 0, Dfi::new(bi(1), bi(1), 0)); + test_case(Dfi::new(bi(5), bi(7), 0), 1, Dfi::new(bi(5), bi(7), 0)); + test_case(Dfi::new(bi(5), bi(7), 0), 2, Dfi::new(bi(25), bi(49), 0)); + test_case(Dfi::new(bi(5), bi(7), 0), 3, Dfi::new(bi(125), bi(343), 0)); + test_case(Dfi::new(bi(5), bi(7), 0), 4, Dfi::new(bi(625), bi(2401), 0)); test_case( - DFI::new(bi(5), bi(7), 0), + Dfi::new(bi(5), bi(7), 0), 5, - DFI::new(bi(3125), bi(16807), 0), + Dfi::new(bi(3125), bi(16807), 0), ); test_case( - DFI::new(bi(255), bi(257), 8), + Dfi::new(bi(255), bi(257), 8), 0, - DFI::new(bi(256), bi(256), 8), + Dfi::new(bi(256), bi(256), 8), ); test_case( - DFI::new(bi(255), bi(257), 8), + Dfi::new(bi(255), bi(257), 8), 1, - DFI::new(bi(255), bi(257), 8), + Dfi::new(bi(255), bi(257), 8), ); test_case( - DFI::new(bi(255), bi(257), 8), + Dfi::new(bi(255), bi(257), 8), 2, - DFI::new(bi(254), bi(259), 8), + Dfi::new(bi(254), bi(259), 8), ); test_case( - DFI::new(bi(255), bi(257), 8), + Dfi::new(bi(255), bi(257), 8), 3, - DFI::new(bi(253), bi(261), 8), + Dfi::new(bi(253), bi(261), 8), ); - test_case(DFI::new(bi(-5), bi(7), 0), 0, DFI::new(bi(1), bi(1), 0)); - test_case(DFI::new(bi(-5), bi(7), 0), 1, DFI::new(bi(-5), bi(7), 0)); - test_case(DFI::new(bi(-5), bi(7), 0), 2, DFI::new(bi(0), bi(49), 0)); + test_case(Dfi::new(bi(-5), bi(7), 0), 0, Dfi::new(bi(1), bi(1), 0)); + test_case(Dfi::new(bi(-5), bi(7), 0), 1, Dfi::new(bi(-5), bi(7), 0)); + test_case(Dfi::new(bi(-5), bi(7), 0), 2, Dfi::new(bi(0), bi(49), 0)); test_case( - DFI::new(bi(-5), bi(7), 0), + Dfi::new(bi(-5), bi(7), 0), 3, - DFI::new(bi(-125), bi(343), 0), + Dfi::new(bi(-125), bi(343), 0), ); - test_case(DFI::new(bi(-5), bi(7), 0), 4, DFI::new(bi(0), bi(2401), 0)); + test_case(Dfi::new(bi(-5), bi(7), 0), 4, Dfi::new(bi(0), bi(2401), 0)); test_case( - DFI::new(bi(-5), bi(7), 0), + Dfi::new(bi(-5), bi(7), 0), 5, - DFI::new(bi(-3125), bi(16807), 0), + Dfi::new(bi(-3125), bi(16807), 0), ); - test_case(DFI::new(bi(-7), bi(5), 0), 0, DFI::new(bi(1), bi(1), 0)); - test_case(DFI::new(bi(-7), bi(5), 0), 1, DFI::new(bi(-7), bi(5), 0)); - test_case(DFI::new(bi(-7), bi(5), 0), 2, DFI::new(bi(0), bi(49), 0)); + test_case(Dfi::new(bi(-7), bi(5), 0), 0, Dfi::new(bi(1), bi(1), 0)); + test_case(Dfi::new(bi(-7), bi(5), 0), 1, Dfi::new(bi(-7), bi(5), 0)); + test_case(Dfi::new(bi(-7), bi(5), 0), 2, Dfi::new(bi(0), bi(49), 0)); test_case( - DFI::new(bi(-7), bi(5), 0), + Dfi::new(bi(-7), bi(5), 0), 3, - DFI::new(bi(-343), bi(125), 0), + Dfi::new(bi(-343), bi(125), 0), ); - test_case(DFI::new(bi(-7), bi(5), 0), 4, DFI::new(bi(0), bi(2401), 0)); + test_case(Dfi::new(bi(-7), bi(5), 0), 4, Dfi::new(bi(0), bi(2401), 0)); test_case( - DFI::new(bi(-7), bi(5), 0), + Dfi::new(bi(-7), bi(5), 0), 5, - DFI::new(bi(-16807), bi(3125), 0), + Dfi::new(bi(-16807), bi(3125), 0), ); - test_case(DFI::new(bi(-7), bi(-5), 0), 0, DFI::new(bi(1), bi(1), 0)); - test_case(DFI::new(bi(-7), bi(-5), 0), 1, DFI::new(bi(-7), bi(-5), 0)); - test_case(DFI::new(bi(-7), bi(-5), 0), 2, DFI::new(bi(25), bi(49), 0)); + test_case(Dfi::new(bi(-7), bi(-5), 0), 0, Dfi::new(bi(1), bi(1), 0)); + test_case(Dfi::new(bi(-7), bi(-5), 0), 1, Dfi::new(bi(-7), bi(-5), 0)); + test_case(Dfi::new(bi(-7), bi(-5), 0), 2, Dfi::new(bi(25), bi(49), 0)); test_case( - DFI::new(bi(-7), bi(-5), 0), + Dfi::new(bi(-7), bi(-5), 0), 3, - DFI::new(bi(-343), bi(-125), 0), + Dfi::new(bi(-343), bi(-125), 0), ); test_case( - DFI::new(bi(-7), bi(-5), 0), + Dfi::new(bi(-7), bi(-5), 0), 4, - DFI::new(bi(625), bi(2401), 0), + Dfi::new(bi(625), bi(2401), 0), ); test_case( - DFI::new(bi(-7), bi(-5), 0), + Dfi::new(bi(-7), bi(-5), 0), 5, - DFI::new(bi(-16807), bi(-3125), 0), + Dfi::new(bi(-16807), bi(-3125), 0), ); - test_case(DFI::from_int(bi(3), 8), -1, DFI::new(bi(85), bi(86), 8)); - test_case(DFI::from_int(bi(3), 8), -2, DFI::new(bi(28), bi(29), 8)); - test_case(DFI::from_int(bi(3), 8), -3, DFI::new(bi(9), bi(10), 8)); - test_case(DFI::from_int(bi(3), 8), -4, DFI::new(bi(3), bi(4), 8)); - test_case(DFI::from_int(bi(3), 8), -5, DFI::new(bi(0), bi(2), 8)); - test_case(DFI::from_int(bi(3), 8), -6, DFI::new(bi(0), bi(1), 8)); + test_case(Dfi::from_int(bi(3), 8), -1, Dfi::new(bi(85), bi(86), 8)); + test_case(Dfi::from_int(bi(3), 8), -2, Dfi::new(bi(28), bi(29), 8)); + test_case(Dfi::from_int(bi(3), 8), -3, Dfi::new(bi(9), bi(10), 8)); + test_case(Dfi::from_int(bi(3), 8), -4, Dfi::new(bi(3), bi(4), 8)); + test_case(Dfi::from_int(bi(3), 8), -5, Dfi::new(bi(0), bi(2), 8)); + test_case(Dfi::from_int(bi(3), 8), -6, Dfi::new(bi(0), bi(1), 8)); } #[test] fn test_natural_log_of_2() { for _ in 0..5 { assert_same!( - DFI::natural_log_of_2(64), - DFI::new( + Dfi::natural_log_of_2(64), + Dfi::new( bi(12_786_308_645_202_655_659), bi(12_786_308_645_202_655_660), 64 ) ); assert_same!( - DFI::natural_log_of_2(128), - DFI::new( + Dfi::natural_log_of_2(128), + Dfi::new( BigInt::from(235_865_763_225_513_294_137_944_142_764_154_484_399u128), BigInt::from(235_865_763_225_513_294_137_944_142_764_154_484_400u128), 128 @@ -2404,72 +2406,72 @@ mod tests { #[test] fn test_log_core() { assert_same!( - DFI::from_ratio(ri(2), 64).log_core(), // calculate log(3) - DFI::new( + Dfi::from_ratio(ri(2), 64).log_core(), // calculate log(3) + Dfi::new( bi(20_265_819_725_292_939_638), bi(20_265_819_725_292_939_639), 64 ) ); assert_same!( - DFI::from_ratio(ri(2), 128).log_core(), // calculate log(3) - DFI::new( + Dfi::from_ratio(ri(2), 128).log_core(), // calculate log(3) + Dfi::new( "373838389916413667603494184660470824117".parse().unwrap(), "373838389916413667603494184660470824118".parse().unwrap(), 128 ) ); assert_same!( - DFI::from_ratio(ri(3), 64).log_core(), // calculate log(2) - DFI::new( + Dfi::from_ratio(ri(3), 64).log_core(), // calculate log(2) + Dfi::new( bi(12_786_308_645_202_655_659), bi(12_786_308_645_202_655_660), 64 ) ); assert_same!( - DFI::from_ratio(ri(3), 128).log_core(), // calculate log(2) - DFI::new( + Dfi::from_ratio(ri(3), 128).log_core(), // calculate log(2) + Dfi::new( BigInt::from(235_865_763_225_513_294_137_944_142_764_154_484_399u128), BigInt::from(235_865_763_225_513_294_137_944_142_764_154_484_400u128), 128 ) ); assert_same!( - DFI::from_ratio(ri(9), 64).log_core(), // calculate log(5 / 4) - DFI::new( + Dfi::from_ratio(ri(9), 64).log_core(), // calculate log(5 / 4) + Dfi::new( bi(4_116_271_982_791_902_040), bi(4_116_271_982_791_902_041), 64 ) ); assert_same!( - DFI::from_ratio(ri(9), 128).log_core(), // calculate log(5 / 4) - DFI::new( + Dfi::from_ratio(ri(9), 128).log_core(), // calculate log(5 / 4) + Dfi::new( BigInt::from(75_931_815_804_343_184_391_506_054_410_983_916_693u128), BigInt::from(75_931_815_804_343_184_391_506_054_410_983_916_694u128), 128 ) ); assert_same!( - DFI::from_ratio(r(13, 3), 64).log_core(), // calculate log(8 / 5) - DFI::new( + Dfi::from_ratio(r(13, 3), 64).log_core(), // calculate log(8 / 5) + Dfi::new( bi(8_670_036_662_410_753_619), bi(8_670_036_662_410_753_620), 64 ) ); assert_same!( - DFI::from_ratio(r(13, 3), 80).log_core(), // calculate log(8 / 5) - DFI::new( + Dfi::from_ratio(r(13, 3), 80).log_core(), // calculate log(8 / 5) + Dfi::new( bi(568_199_522_707_751_149_207_091), bi(568_199_522_707_751_149_207_092), 80 ) ); assert_same!( - DFI::from_ratio(r(13, 3), 128).log_core(), // calculate log(8 / 5) - DFI::new( + Dfi::from_ratio(r(13, 3), 128).log_core(), // calculate log(8 / 5) + Dfi::new( BigInt::from(159_933_947_421_170_109_746_438_088_353_170_567_705u128), BigInt::from(159_933_947_421_170_109_746_438_088_353_170_567_706u128), 128 @@ -2480,48 +2482,48 @@ mod tests { #[test] fn test_log() { assert_same!( - DFI::from_ratio_range(r(4, 5), r(123, 45), 64).log(), - DFI::new( + Dfi::from_ratio_range(r(4, 5), r(123, 45), 64).log(), + Dfi::new( bi(-4_116_271_982_791_902_042), bi(18_548_604_515_280_868_688), 64 ) ); assert_same!( - DFI::from_ratio_range(r(4, 5), r(123, 45), 127).log(), - DFI::new( + Dfi::from_ratio_range(r(4, 5), r(123, 45), 127).log(), + Dfi::new( bi(-37_965_907_902_171_592_195_753_027_205_491_958_348), BigInt::from(171_080_680_208_919_797_346_165_683_845_098_625_899u128), 127 ) ); assert_same!( - DFI::from_ratio(r(12345, 2), 64).log(), - DFI::new( + Dfi::from_ratio(r(12345, 2), 64).log(), + Dfi::new( bi(161_000_585_365_199_022_398), bi(161_000_585_365_199_022_399), 64 ) ); for i in 2..30 { - fn do_test(i: usize, input: DFI) { + fn do_test(i: usize, input: Dfi) { assert!(input.log2_denom < 50); println!("i = {}", i); dbg!(&input); let input_denom = 2.0f64.powi(input.log2_denom.try_into().unwrap()); let input_lower_bound = input.lower_bound_numer.to_f64().unwrap() / input_denom; let input_upper_bound = input.upper_bound_numer.to_f64().unwrap() / input_denom; - let expected = DFI::from_ratio_range( + let expected = Dfi::from_ratio_range( Ratio::::from_float(input_lower_bound.ln()).unwrap(), Ratio::::from_float(input_upper_bound.ln()).unwrap(), input.log2_denom, ); assert_same!(input.log(), expected); } - do_test(i, DFI::from_int(BigInt::from(i), 32)); + do_test(i, Dfi::from_int(BigInt::from(i), 32)); do_test( i, - DFI::from_ratio(Ratio::new(BigInt::one(), BigInt::from(i)), 32), + Dfi::from_ratio(Ratio::new(BigInt::one(), BigInt::from(i)), 32), ); } } @@ -2529,56 +2531,56 @@ mod tests { #[test] fn test_exp() { assert_same!( - DFI::from_int_range(bi(0), bi(0), 64).exp(), - DFI::new( + Dfi::from_int_range(bi(0), bi(0), 64).exp(), + Dfi::new( bi(18_446_744_073_709_551_616), bi(18_446_744_073_709_551_616), 64 ) ); assert_same!( - DFI::from_int_range(bi(0), bi(1), 64).exp(), - DFI::new( + Dfi::from_int_range(bi(0), bi(1), 64).exp(), + Dfi::new( bi(18_446_744_073_709_551_616), bi(50_143_449_209_799_256_683), 64 ) ); assert_same!( - DFI::from_int_range(bi(1), bi(1), 64).exp(), - DFI::new( + Dfi::from_int_range(bi(1), bi(1), 64).exp(), + Dfi::new( bi(50_143_449_209_799_256_682), bi(50_143_449_209_799_256_683), 64 ) ); assert_same!( - DFI::from_int_range(bi(1), bi(1), 120).exp(), - DFI::new( + Dfi::from_int_range(bi(1), bi(1), 120).exp(), + Dfi::new( bi(3_613_216_306_821_173_191_995_746_233_763_034_355), bi(3_613_216_306_821_173_191_995_746_233_763_034_356), 120 ) ); assert_same!( - DFI::from_ratio_range(r(1, 16), r(1, 16), 64).exp(), - DFI::new( + Dfi::from_ratio_range(r(1, 16), r(1, 16), 64).exp(), + Dfi::new( bi(19_636_456_851_539_679_189), bi(19_636_456_851_539_679_190), 64 ) ); assert_same!( - DFI::from_ratio_range(r(1, 16), r(5, 11), 64).exp(), - DFI::new( + Dfi::from_ratio_range(r(1, 16), r(5, 11), 64).exp(), + Dfi::new( bi(19_636_456_851_539_679_189), bi(29_062_053_985_348_969_808), 64 ) ); assert_same!( - DFI::from_ratio(r(12345, 20), 120).exp(), - DFI::new( + Dfi::from_ratio(r(12345, 20), 120).exp(), + Dfi::new( "15554943374427623479586295616005556713817814660873673636717540447365020\ 241010687483038422716174364856604938959636391301874371790367372240576361413166\ 945168943074910853869056633171038088299306405677231299055607307283566273703681\ @@ -2597,19 +2599,19 @@ mod tests { // check that we don't overflow where we don't need to assert_same!( - DFI::from_int(bi(-99_999_999_999_999_999_999_999_999), 120).exp(), - DFI::new(bi(0), bi(1), 120) + Dfi::from_int(bi(-99_999_999_999_999_999_999_999_999), 120).exp(), + Dfi::new(bi(0), bi(1), 120) ); for i in (1..30).rev() { - fn do_test(i: i64, input: DFI) { + fn do_test(i: i64, input: Dfi) { assert!(input.log2_denom < 50); println!("i = {}", i); dbg!(&input); let input_denom = 2.0f64.powi(input.log2_denom.try_into().unwrap()); let input_lower_bound = input.lower_bound_numer.to_f64().unwrap() / input_denom; let input_upper_bound = input.upper_bound_numer.to_f64().unwrap() / input_denom; - let expected = DFI::from_ratio_range( + let expected = Dfi::from_ratio_range( Ratio::::from_float(input_lower_bound.exp()).unwrap(), Ratio::::from_float(input_upper_bound.exp()).unwrap(), input.log2_denom, @@ -2618,16 +2620,16 @@ mod tests { } if i <= 3 { // f64::exp loses precision really fast, skip in cases where f64::exp is not precise enough - do_test(i, DFI::from_int(BigInt::from(i), 32)); + do_test(i, Dfi::from_int(BigInt::from(i), 32)); } do_test( i, - DFI::from_ratio(Ratio::new(BigInt::one(), BigInt::from(i)), 32), + Dfi::from_ratio(Ratio::new(BigInt::one(), BigInt::from(i)), 32), ); - do_test(i, DFI::from_int(BigInt::from(-i), 32)); + do_test(i, Dfi::from_int(BigInt::from(-i), 32)); do_test( i, - DFI::from_ratio(Ratio::new(BigInt::one(), BigInt::from(-i)), 32), + Dfi::from_ratio(Ratio::new(BigInt::one(), BigInt::from(-i)), 32), ); } } diff --git a/src/lattice.rs b/src/lattice.rs index b152825..4fa6cf6 100644 --- a/src/lattice.rs +++ b/src/lattice.rs @@ -338,7 +338,7 @@ mod tests { BigInt::zero() } } else { - -(&sin_pi_7_approximation) + -sin_pi_7_approximation .pow(x as i32) .mul(&multiplier) .round() diff --git a/src/mod_int.rs b/src/mod_int.rs index 6273bd0..c072dc6 100644 --- a/src/mod_int.rs +++ b/src/mod_int.rs @@ -4,8 +4,8 @@ use crate::{ polynomial::{DivisorIsOne, PolynomialCoefficient, PolynomialReducingFactorSupported}, traits::{ - AlwaysExactDiv, AlwaysExactDivAssign, ExactDiv, ExactDivAssign, ExtendedGCD, - ExtendedGCDResult, GCD, + AlwaysExactDiv, AlwaysExactDivAssign, ExactDiv, ExactDivAssign, ExactDivAssignError, + ExtendedGCD, ExtendedGCDResult, GCD, }, util::BaseAndExponent, }; @@ -1110,13 +1110,13 @@ macro_rules! impl_exact_div { fn exact_div_assign(&mut self, rhs: $rhs) { self.div_assign(rhs); } - fn checked_exact_div_assign(&mut self, rhs: $rhs) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: $rhs) -> Result<(), ExactDivAssignError> { (&*self) .checked_exact_div(rhs) .map(|v| { *self = v; }) - .ok_or(()) + .ok_or(ExactDivAssignError) } } diff --git a/src/polynomial.rs b/src/polynomial.rs index e3b76f2..171c97e 100644 --- a/src/polynomial.rs +++ b/src/polynomial.rs @@ -1,8 +1,8 @@ // SPDX-License-Identifier: LGPL-2.1-or-later // See Notices.txt for copyright information use crate::traits::{ - AlwaysExactDivAssign, CharacteristicZero, ExactDiv, ExactDivAssign, GCDAndLCM, - RingCharacteristic, GCD, + AlwaysExactDivAssign, CharacteristicZero, ExactDiv, ExactDivAssign, ExactDivAssignError, + GCDAndLCM, RingCharacteristic, GCD, }; use num_bigint::BigInt; use num_integer::Integer; @@ -502,14 +502,14 @@ impl Mul<&DivisorIsOne> for &DivisorIsOne { } impl ExactDivAssign for DivisorIsOne { - fn checked_exact_div_assign(&mut self, _rhs: DivisorIsOne) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, _rhs: DivisorIsOne) -> Result<(), ExactDivAssignError> { Ok(()) } fn exact_div_assign(&mut self, _rhs: DivisorIsOne) {} } impl ExactDivAssign<&DivisorIsOne> for DivisorIsOne { - fn checked_exact_div_assign(&mut self, _rhs: &DivisorIsOne) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, _rhs: &DivisorIsOne) -> Result<(), ExactDivAssignError> { Ok(()) } fn exact_div_assign(&mut self, _rhs: &DivisorIsOne) {} @@ -1486,9 +1486,9 @@ impl Polynomial { T::make_zero_coefficient_from_coefficient(Cow::Borrowed(at)), ) } - pub fn set_one_if_nonzero(&mut self) -> Result<(), ()> { + pub fn set_one_if_nonzero(&mut self) -> Result<(), EmptyPolynomial> { if self.elements.is_empty() { - Err(()) + Err(EmptyPolynomial) } else { self.elements.drain(1..); self.divisor = One::one(); @@ -1499,7 +1499,7 @@ impl Polynomial { pub fn into_one_if_nonzero(mut self) -> Result { match self.set_one_if_nonzero() { Ok(()) => Ok(self), - Err(()) => Err(self), + Err(EmptyPolynomial) => Err(self), } } #[must_use] @@ -1748,6 +1748,17 @@ impl From for std::io::Error { } } +#[derive(Debug)] +pub struct EmptyPolynomial; + +impl fmt::Display for EmptyPolynomial { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Empty polynomial") + } +} + +impl std::error::Error for EmptyPolynomial {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/polynomial/distinct_degree_factorization.rs b/src/polynomial/distinct_degree_factorization.rs index ed97feb..5f2d786 100644 --- a/src/polynomial/distinct_degree_factorization.rs +++ b/src/polynomial/distinct_degree_factorization.rs @@ -61,7 +61,7 @@ mod tests { let poly = make_poly(poly, modulus); let expected_factors: Vec<_> = expected_factors .iter() - .map(|poly| make_poly(*poly, modulus)) + .map(|poly| make_poly(poly, modulus)) .collect(); println!("poly: {}", poly); println!("expected_factors:"); diff --git a/src/polynomial/div_rem.rs b/src/polynomial/div_rem.rs index 204bb5f..6066fab 100644 --- a/src/polynomial/div_rem.rs +++ b/src/polynomial/div_rem.rs @@ -5,8 +5,7 @@ use crate::{ polynomial::{ Polynomial, PolynomialCoefficient, PolynomialCoefficientElement, PolynomialDivSupported, PseudoDivRem, - }, - traits::{ExactDiv, ExactDivAssign}, + }, traits::{ExactDiv, ExactDivAssign, ExactDivAssignError}, }; use num_integer::Integer; use num_traits::{CheckedDiv, CheckedRem, Zero}; @@ -276,13 +275,13 @@ macro_rules! impl_div_rem_eq { let lhs = mem::replace(self, Zero::zero()); *self = lhs.exact_div(rhs); } - fn checked_exact_div_assign(&mut self, rhs: $r) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: $r) -> Result<(), ExactDivAssignError> { (&*self) .checked_exact_div(rhs) .map(|v| { *self = v; }) - .ok_or(()) + .ok_or(ExactDivAssignError) } } }; @@ -345,8 +344,8 @@ fn checked_div_single ExactDiv<&'a T, Output fn checked_div_assign_single ExactDiv<&'a T, Output = T>>( lhs: &mut Polynomial, rhs: &T, -) -> Result<(), ()> { - *lhs = checked_div_single(Cow::Owned(mem::replace(lhs, Zero::zero())), rhs).ok_or(())?; +) -> Result<(), ExactDivAssignError> { + *lhs = checked_div_single(Cow::Owned(mem::replace(lhs, Zero::zero())), rhs).ok_or(ExactDivAssignError)?; Ok(()) } @@ -452,7 +451,7 @@ impl ExactDiv<&'a T, Output = T>> ExactDivAss fn exact_div_assign(&mut self, rhs: T) { div_assign_single(self, &rhs); } - fn checked_exact_div_assign(&mut self, rhs: T) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: T) -> Result<(), ExactDivAssignError> { checked_div_assign_single(self, &rhs) } } @@ -463,7 +462,7 @@ impl ExactDiv<&'b T, Output = T>> ExactDivAss fn exact_div_assign(&mut self, rhs: &T) { div_assign_single(self, rhs); } - fn checked_exact_div_assign(&mut self, rhs: &T) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: &T) -> Result<(), ExactDivAssignError> { checked_div_assign_single(self, rhs) } } diff --git a/src/polynomial/factorization_over_integers.rs b/src/polynomial/factorization_over_integers.rs index ba4d45e..2a98f04 100644 --- a/src/polynomial/factorization_over_integers.rs +++ b/src/polynomial/factorization_over_integers.rs @@ -542,13 +542,9 @@ impl Polynomial { modular_factors.retain(|factor| factor.degree() != Some(0)); - debug_assert!(modular_factors + debug_assert!(!modular_factors .iter() - .position(|factor| match factor.degree() { - None | Some(0) => true, - _ => false, - }) - .is_none()); + .any(|factor| matches!(factor.degree(), None | Some(0)))); // println!("modular_factors:"); // for factor in &modular_factors { diff --git a/src/polynomial/gcd.rs b/src/polynomial/gcd.rs index 256ca32..2c62003 100644 --- a/src/polynomial/gcd.rs +++ b/src/polynomial/gcd.rs @@ -49,9 +49,6 @@ impl ExactDiv<&'a T, Output = T>> Polynomial< assert!(!self.is_zero()); let one = T::make_one_coefficient_from_element(Cow::Borrowed(&self.elements[0])); - #[allow(unused_variables)] - let mut i = 3usize; - let mut f_i_2 = self; // f[i-2] let mut n_i_2 = f_i_2.degree().expect("f_i_2 is known to be non-zero"); let mut a_i_2 = one.clone(); @@ -84,7 +81,6 @@ impl ExactDiv<&'a T, Output = T>> Polynomial< ); // step to next iteration - i += 1; f_i_2 = f_i_1; f_i_1 = f_i; n_i_2 = n_i_1; diff --git a/src/polynomial/same_degree_factorization.rs b/src/polynomial/same_degree_factorization.rs index 5655509..1cf0777 100644 --- a/src/polynomial/same_degree_factorization.rs +++ b/src/polynomial/same_degree_factorization.rs @@ -117,7 +117,7 @@ mod tests { let poly = make_poly(poly, modulus); let expected_factors: HashSet<_> = expected_factors .iter() - .map(|poly| make_poly(*poly, modulus)) + .map(|poly| make_poly(poly, modulus)) .collect(); println!("poly: {}", poly); println!("factor_degree: {}", factor_degree); diff --git a/src/python.rs b/src/python.rs index 21bf589..19f9a64 100644 --- a/src/python.rs +++ b/src/python.rs @@ -115,7 +115,7 @@ impl RealAlgebraicNumberPy { self.value.is_integer() } fn recip<'py>(&self, py: Python<'py>) -> PyResult { - py.allow_threads(|| Some(self.value.checked_recip()?)) + py.allow_threads(|| self.value.checked_recip()) .ok_or_else(get_div_by_zero_error) } /// returns `floor(log2(self))` @@ -246,10 +246,10 @@ impl RealAlgebraicNumberPy { // Unary arithmetic fn __neg__(&self, py: Python) -> PyResult { - Ok(py.allow_threads(|| (-&self.value).into())) + Ok(py.allow_threads(|| -&self.value)) } fn __abs__(&self, py: Python) -> PyResult { - Ok(py.allow_threads(|| self.value.abs().into())) + Ok(py.allow_threads(|| self.value.abs())) } } @@ -274,7 +274,7 @@ fn try_arithmetic_helper< ) -> PyResult { let rhs = Bound::::try_from(rhs)?; let rhs = rhs.get(); - py.allow_threads(|| Ok(f(&lhs.value, &rhs.value)?.into())) + py.allow_threads(|| f(&lhs.value, &rhs.value)) .map_err(map_err) } diff --git a/src/traits.rs b/src/traits.rs index 560848c..a6fb4e8 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -205,7 +205,7 @@ impl Mul<&'a T, Output = T>> ExtendedGCD f /// # Ok::<(), ParseRatioError>(()) /// ``` // FIXME: unignore doctest when pub -pub(crate) trait DivRemNearest: Sized { +pub trait DivRemNearest: Sized { type DivOutput; type RemOutput; fn checked_div_rem_nearest(&self, rhs: &Rhs) -> Option<(Self::DivOutput, Self::RemOutput)>; @@ -474,7 +474,7 @@ impl TrailingZeros for BigInt { } } -pub(crate) trait IsolatedRealRoot { +pub trait IsolatedRealRoot { fn root_polynomial(&self) -> &Polynomial; fn multiplicity(&self) -> usize; fn lower_bound(&self) -> &Ratio; @@ -550,13 +550,24 @@ pub trait ExactDiv: Sized { fn checked_exact_div(self, rhs: Rhs) -> Option; } +#[derive(Debug)] +pub struct ExactDivAssignError; + +impl fmt::Display for ExactDivAssignError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "exact division failed") + } +} + +impl std::error::Error for ExactDivAssignError {} + pub trait ExactDivAssign: ExactDiv { fn exact_div_assign(&mut self, rhs: Rhs) { - if let Err(()) = self.checked_exact_div_assign(rhs) { + if let Err(ExactDivAssignError) = self.checked_exact_div_assign(rhs) { panic!("exact division failed"); } } - fn checked_exact_div_assign(&mut self, rhs: Rhs) -> Result<(), ()>; + fn checked_exact_div_assign(&mut self, rhs: Rhs) -> Result<(), ExactDivAssignError>; } /// division always produces exact results except for division by zero, overflow, or similar @@ -610,9 +621,9 @@ macro_rules! impl_exact_div_for_ratio { fn exact_div_assign(&mut self, rhs: $rhs) { self.div_assign(rhs); } - fn checked_exact_div_assign(&mut self, rhs: $rhs) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: $rhs) -> Result<(), ExactDivAssignError> { if rhs.is_zero() { - Err(()) + Err(ExactDivAssignError) } else { self.div_assign(rhs); Ok(()) @@ -703,13 +714,13 @@ macro_rules! impl_exact_div_for_int { fn exact_div_assign(&mut self, rhs: &$t) { *self = (&*self).exact_div(rhs); } - fn checked_exact_div_assign(&mut self, rhs: &$t) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: &$t) -> Result<(), ExactDivAssignError> { (&*self) .checked_exact_div(rhs) .map(|v| { *self = v; }) - .ok_or(()) + .ok_or(ExactDivAssignError) } } @@ -717,13 +728,13 @@ macro_rules! impl_exact_div_for_int { fn exact_div_assign(&mut self, rhs: $t) { *self = (&*self).exact_div(rhs); } - fn checked_exact_div_assign(&mut self, rhs: $t) -> Result<(), ()> { + fn checked_exact_div_assign(&mut self, rhs: $t) -> Result<(), ExactDivAssignError> { (&*self) .checked_exact_div(rhs) .map(|v| { *self = v; }) - .ok_or(()) + .ok_or(ExactDivAssignError) } } }; diff --git a/src/util.rs b/src/util.rs index f64c165..a6955d0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1141,8 +1141,9 @@ pub(crate) mod tests { pub(crate) fn test_checked_op_helper< T: Clone + PartialEq + fmt::Debug, R: Clone, - OpEqMove: Fn(&mut T, R) -> Result<(), ()>, - OpEqRef: Fn(&mut T, &R) -> Result<(), ()>, + Err, + OpEqMove: Fn(&mut T, R) -> Result<(), Err>, + OpEqRef: Fn(&mut T, &R) -> Result<(), Err>, OpRefRef: Fn(&T, &R) -> Option, OpMoveRef: Fn(T, &R) -> Option, OpRefMove: Fn(&T, R) -> Option,