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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use super::Complex;

use core::ops::Neg;
#[cfg(feature = "std")]
use traits::Float;
use traits::{Num, One, Pow};

macro_rules! pow_impl {
    ($U:ty, $S:ty) => {
        impl<'a, T: Clone + Num> Pow<$U> for &'a Complex<T> {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, mut exp: $U) -> Self::Output {
                if exp == 0 {
                    return Complex::one();
                }
                let mut base = self.clone();

                while exp & 1 == 0 {
                    base = base.clone() * base;
                    exp >>= 1;
                }

                if exp == 1 {
                    return base;
                }

                let mut acc = base.clone();
                while exp > 1 {
                    exp >>= 1;
                    base = base.clone() * base;
                    if exp & 1 == 1 {
                        acc = acc * base.clone();
                    }
                }
                acc
            }
        }

        impl<'a, 'b, T: Clone + Num> Pow<&'b $U> for &'a Complex<T> {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, exp: &$U) -> Self::Output {
                self.pow(*exp)
            }
        }

        impl<'a, T: Clone + Num + Neg<Output = T>> Pow<$S> for &'a Complex<T> {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, exp: $S) -> Self::Output {
                if exp < 0 {
                    Pow::pow(&self.inv(), exp.wrapping_neg() as $U)
                } else {
                    Pow::pow(self, exp as $U)
                }
            }
        }

        impl<'a, 'b, T: Clone + Num + Neg<Output = T>> Pow<&'b $S> for &'a Complex<T> {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, exp: &$S) -> Self::Output {
                self.pow(*exp)
            }
        }
    };
}

pow_impl!(u8, i8);
pow_impl!(u16, i16);
pow_impl!(u32, i32);
pow_impl!(u64, i64);
pow_impl!(usize, isize);
#[cfg(has_i128)]
pow_impl!(u128, i128);

// Note: we can't add `impl<T: Float> Pow<T> for Complex<T>` because new blanket impls are a
// breaking change.  Someone could already have their own `F` and `impl Pow<F> for Complex<F>`
// which would conflict.  We can't even do this in a new semantic version, because we have to
// gate it on the "std" feature, and features can't add breaking changes either.

macro_rules! powf_impl {
    ($F:ty) => {
        #[cfg(feature = "std")]
        impl<'a, T: Float> Pow<$F> for &'a Complex<T>
        where
            $F: Into<T>,
        {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, exp: $F) -> Self::Output {
                self.powf(exp.into())
            }
        }

        #[cfg(feature = "std")]
        impl<'a, 'b, T: Float> Pow<&'b $F> for &'a Complex<T>
        where
            $F: Into<T>,
        {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, &exp: &$F) -> Self::Output {
                self.powf(exp.into())
            }
        }

        #[cfg(feature = "std")]
        impl<T: Float> Pow<$F> for Complex<T>
        where
            $F: Into<T>,
        {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, exp: $F) -> Self::Output {
                self.powf(exp.into())
            }
        }

        #[cfg(feature = "std")]
        impl<'b, T: Float> Pow<&'b $F> for Complex<T>
        where
            $F: Into<T>,
        {
            type Output = Complex<T>;

            #[inline]
            fn pow(self, &exp: &$F) -> Self::Output {
                self.powf(exp.into())
            }
        }
    };
}

powf_impl!(f32);
powf_impl!(f64);

// These blanket impls are OK, because both the target type and the trait parameter would be
// foreign to anyone else trying to implement something that would overlap, raising E0117.

#[cfg(feature = "std")]
impl<'a, T: Float> Pow<Complex<T>> for &'a Complex<T> {
    type Output = Complex<T>;

    #[inline]
    fn pow(self, exp: Complex<T>) -> Self::Output {
        self.powc(exp)
    }
}

#[cfg(feature = "std")]
impl<'a, 'b, T: Float> Pow<&'b Complex<T>> for &'a Complex<T> {
    type Output = Complex<T>;

    #[inline]
    fn pow(self, &exp: &'b Complex<T>) -> Self::Output {
        self.powc(exp)
    }
}

#[cfg(feature = "std")]
impl<T: Float> Pow<Complex<T>> for Complex<T> {
    type Output = Complex<T>;

    #[inline]
    fn pow(self, exp: Complex<T>) -> Self::Output {
        self.powc(exp)
    }
}

#[cfg(feature = "std")]
impl<'b, T: Float> Pow<&'b Complex<T>> for Complex<T> {
    type Output = Complex<T>;

    #[inline]
    fn pow(self, &exp: &'b Complex<T>) -> Self::Output {
        self.powc(exp)
    }
}