1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::Ratio;

use core::cmp;
use num_integer::Integer;
use num_traits::{One, Pow};

macro_rules! pow_unsigned_impl {
    (@ $exp:ty) => {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: $exp) -> Ratio<T> {
            Ratio::new_raw(self.numer.pow(expon), self.denom.pow(expon))
        }
    };
    ($exp:ty) => {
        impl<T: Clone + Integer + Pow<$exp, Output = T>> Pow<$exp> for Ratio<T> {
            pow_unsigned_impl!(@ $exp);
        }
        impl<'a, T: Clone + Integer> Pow<$exp> for &'a Ratio<T>
        where
            &'a T: Pow<$exp, Output = T>,
        {
            pow_unsigned_impl!(@ $exp);
        }
        impl<'b, T: Clone + Integer + Pow<$exp, Output = T>> Pow<&'b $exp> for Ratio<T> {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
        impl<'a, 'b, T: Clone + Integer> Pow<&'b $exp> for &'a Ratio<T>
        where
            &'a T: Pow<$exp, Output = T>,
        {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
    };
}
pow_unsigned_impl!(u8);
pow_unsigned_impl!(u16);
pow_unsigned_impl!(u32);
pow_unsigned_impl!(u64);
pow_unsigned_impl!(u128);
pow_unsigned_impl!(usize);

macro_rules! pow_signed_impl {
    (@ &'b BigInt, BigUint) => {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: &'b BigInt) -> Ratio<T> {
            match expon.sign() {
                Sign::NoSign => One::one(),
                Sign::Minus => {
                    Pow::pow(self, expon.magnitude()).into_recip()
                }
                Sign::Plus => Pow::pow(self, expon.magnitude()),
            }
        }
    };
    (@ $exp:ty, $unsigned:ty) => {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: $exp) -> Ratio<T> {
            match expon.cmp(&0) {
                cmp::Ordering::Equal => One::one(),
                cmp::Ordering::Less => {
                    let expon = expon.wrapping_abs() as $unsigned;
                    Pow::pow(self, expon).into_recip()
                }
                cmp::Ordering::Greater => Pow::pow(self, expon as $unsigned),
            }
        }
    };
    ($exp:ty, $unsigned:ty) => {
        impl<T: Clone + Integer + Pow<$unsigned, Output = T>> Pow<$exp> for Ratio<T> {
            pow_signed_impl!(@ $exp, $unsigned);
        }
        impl<'a, T: Clone + Integer> Pow<$exp> for &'a Ratio<T>
        where
            &'a T: Pow<$unsigned, Output = T>,
        {
            pow_signed_impl!(@ $exp, $unsigned);
        }
        impl<'b, T: Clone + Integer + Pow<$unsigned, Output = T>> Pow<&'b $exp> for Ratio<T> {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
        impl<'a, 'b, T: Clone + Integer> Pow<&'b $exp> for &'a Ratio<T>
        where
            &'a T: Pow<$unsigned, Output = T>,
        {
            type Output = Ratio<T>;
            #[inline]
            fn pow(self, expon: &'b $exp) -> Ratio<T> {
                Pow::pow(self, *expon)
            }
        }
    };
}
pow_signed_impl!(i8, u8);
pow_signed_impl!(i16, u16);
pow_signed_impl!(i32, u32);
pow_signed_impl!(i64, u64);
pow_signed_impl!(i128, u128);
pow_signed_impl!(isize, usize);

#[cfg(feature = "num-bigint")]
mod bigint {
    use super::*;
    use num_bigint::{BigInt, BigUint, Sign};

    impl<T: Clone + Integer + for<'b> Pow<&'b BigUint, Output = T>> Pow<BigUint> for Ratio<T> {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigUint) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'a, T: Clone + Integer> Pow<BigUint> for &'a Ratio<T>
    where
        &'a T: for<'b> Pow<&'b BigUint, Output = T>,
    {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigUint) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'b, T: Clone + Integer + Pow<&'b BigUint, Output = T>> Pow<&'b BigUint> for Ratio<T> {
        pow_unsigned_impl!(@ &'b BigUint);
    }
    impl<'a, 'b, T: Clone + Integer> Pow<&'b BigUint> for &'a Ratio<T>
    where
        &'a T: Pow<&'b BigUint, Output = T>,
    {
        pow_unsigned_impl!(@ &'b BigUint);
    }

    impl<T: Clone + Integer + for<'b> Pow<&'b BigUint, Output = T>> Pow<BigInt> for Ratio<T> {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigInt) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'a, T: Clone + Integer> Pow<BigInt> for &'a Ratio<T>
    where
        &'a T: for<'b> Pow<&'b BigUint, Output = T>,
    {
        type Output = Ratio<T>;
        #[inline]
        fn pow(self, expon: BigInt) -> Ratio<T> {
            Pow::pow(self, &expon)
        }
    }
    impl<'b, T: Clone + Integer + Pow<&'b BigUint, Output = T>> Pow<&'b BigInt> for Ratio<T> {
        pow_signed_impl!(@ &'b BigInt, BigUint);
    }
    impl<'a, 'b, T: Clone + Integer> Pow<&'b BigInt> for &'a Ratio<T>
    where
        &'a T: Pow<&'b BigUint, Output = T>,
    {
        pow_signed_impl!(@ &'b BigInt, BigUint);
    }
}