Skip to content

Commit 90bf601

Browse files
make RealAlgebraicNumber::pow simpler to use
1 parent d8508e5 commit 90bf601

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

src/algebraic_numbers.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@ use std::ops::RemAssign;
3939
use std::ops::Sub;
4040
use std::ops::SubAssign;
4141

42+
pub trait IntoRationalExponent {
43+
fn into_rational_exponent(self) -> Ratio<BigInt>;
44+
}
45+
46+
impl<T: IntoRationalExponent + Clone> IntoRationalExponent for &'_ T {
47+
fn into_rational_exponent(self) -> Ratio<BigInt> {
48+
(*self).clone().into_rational_exponent()
49+
}
50+
}
51+
52+
impl<N: Into<BigInt>, D: Into<BigInt>> IntoRationalExponent for (N, D) {
53+
fn into_rational_exponent(self) -> Ratio<BigInt> {
54+
let (numer, denom) = self;
55+
Ratio::new(numer.into(), denom.into())
56+
}
57+
}
58+
4259
#[derive(Clone)]
4360
pub struct RealAlgebraicNumberData {
4461
pub minimal_polynomial: Polynomial<BigInt>,
@@ -111,6 +128,12 @@ macro_rules! impl_from_int_or_ratio {
111128
}
112129
}
113130

131+
impl IntoRationalExponent for $t {
132+
fn into_rational_exponent(self) -> Ratio<BigInt> {
133+
BigInt::from(self).into()
134+
}
135+
}
136+
114137
impl From<Ratio<$t>> for RealAlgebraicNumber {
115138
fn from(value: Ratio<$t>) -> Self {
116139
let (numer, denom) = value.into();
@@ -127,6 +150,13 @@ macro_rules! impl_from_int_or_ratio {
127150
)
128151
}
129152
}
153+
154+
impl IntoRationalExponent for Ratio<$t> {
155+
fn into_rational_exponent(self) -> Ratio<BigInt> {
156+
let (numer, denom) = self.into();
157+
Ratio::new_raw(numer.into(), denom.into())
158+
}
159+
}
130160
};
131161
}
132162

@@ -766,11 +796,11 @@ impl RealAlgebraicNumber {
766796
)
767797
}
768798
}
769-
pub fn checked_into_pow<E: Into<Ratio<BigInt>>>(self, exponent: E) -> Option<Self> {
770-
Self::checked_pow_impl(Cow::Owned(self), exponent.into())
799+
pub fn checked_into_pow<E: IntoRationalExponent>(self, exponent: E) -> Option<Self> {
800+
Self::checked_pow_impl(Cow::Owned(self), exponent.into_rational_exponent())
771801
}
772-
pub fn checked_pow<E: Into<Ratio<BigInt>>>(&self, exponent: E) -> Option<Self> {
773-
Self::checked_pow_impl(Cow::Borrowed(self), exponent.into())
802+
pub fn checked_pow<E: IntoRationalExponent>(&self, exponent: E) -> Option<Self> {
803+
Self::checked_pow_impl(Cow::Borrowed(self), exponent.into_rational_exponent())
774804
}
775805
}
776806

@@ -1409,17 +1439,19 @@ impl<'a, 'b> Rem<&'a RealAlgebraicNumber> for &'b RealAlgebraicNumber {
14091439
}
14101440
}
14111441

1412-
impl<E: Into<Ratio<BigInt>>> Pow<E> for RealAlgebraicNumber {
1442+
impl<E: IntoRationalExponent> Pow<E> for RealAlgebraicNumber {
14131443
type Output = RealAlgebraicNumber;
14141444
fn pow(self, exponent: E) -> RealAlgebraicNumber {
1415-
self.checked_into_pow(exponent).expect("checked_pow failed")
1445+
self.checked_into_pow(exponent.into_rational_exponent())
1446+
.expect("checked_pow failed")
14161447
}
14171448
}
14181449

1419-
impl<E: Into<Ratio<BigInt>>> Pow<E> for &'_ RealAlgebraicNumber {
1450+
impl<E: IntoRationalExponent> Pow<E> for &'_ RealAlgebraicNumber {
14201451
type Output = RealAlgebraicNumber;
14211452
fn pow(self, exponent: E) -> RealAlgebraicNumber {
1422-
self.checked_pow(exponent).expect("checked_pow failed")
1453+
self.checked_pow(exponent.into_rational_exponent())
1454+
.expect("checked_pow failed")
14231455
}
14241456
}
14251457

0 commit comments

Comments
 (0)