/rust/registry/src/index.crates.io-1949cf8c6b5b557f/num-complex-0.4.6/src/pow.rs
Line | Count | Source |
1 | | use super::Complex; |
2 | | |
3 | | use core::ops::Neg; |
4 | | #[cfg(any(feature = "std", feature = "libm"))] |
5 | | use num_traits::Float; |
6 | | use num_traits::{Num, One, Pow}; |
7 | | |
8 | | macro_rules! pow_impl { |
9 | | ($U:ty, $S:ty) => { |
10 | | impl<'a, T: Clone + Num> Pow<$U> for &'a Complex<T> { |
11 | | type Output = Complex<T>; |
12 | | |
13 | | #[inline] |
14 | 0 | fn pow(self, mut exp: $U) -> Self::Output { |
15 | 0 | if exp == 0 { |
16 | 0 | return Complex::one(); |
17 | 0 | } |
18 | 0 | let mut base = self.clone(); |
19 | | |
20 | 0 | while exp & 1 == 0 { |
21 | 0 | base = base.clone() * base; |
22 | 0 | exp >>= 1; |
23 | 0 | } |
24 | | |
25 | 0 | if exp == 1 { |
26 | 0 | return base; |
27 | 0 | } |
28 | | |
29 | 0 | let mut acc = base.clone(); |
30 | 0 | while exp > 1 { |
31 | 0 | exp >>= 1; |
32 | 0 | base = base.clone() * base; |
33 | 0 | if exp & 1 == 1 { |
34 | 0 | acc = acc * base.clone(); |
35 | 0 | } |
36 | | } |
37 | 0 | acc |
38 | 0 | } Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<u8>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<u16>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<u32>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<u64>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<usize>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<u128>>::pow |
39 | | } |
40 | | |
41 | | impl<'a, 'b, T: Clone + Num> Pow<&'b $U> for &'a Complex<T> { |
42 | | type Output = Complex<T>; |
43 | | |
44 | | #[inline] |
45 | 0 | fn pow(self, exp: &$U) -> Self::Output { |
46 | 0 | self.pow(*exp) |
47 | 0 | } Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&u8>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&u16>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&u32>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&u64>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&usize>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&u128>>::pow |
48 | | } |
49 | | |
50 | | impl<'a, T: Clone + Num + Neg<Output = T>> Pow<$S> for &'a Complex<T> { |
51 | | type Output = Complex<T>; |
52 | | |
53 | | #[inline] |
54 | 0 | fn pow(self, exp: $S) -> Self::Output { |
55 | 0 | if exp < 0 { |
56 | 0 | Pow::pow(&self.inv(), exp.wrapping_neg() as $U) |
57 | | } else { |
58 | 0 | Pow::pow(self, exp as $U) |
59 | | } |
60 | 0 | } Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<i8>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<i16>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<i32>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<i64>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<isize>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<i128>>::pow |
61 | | } |
62 | | |
63 | | impl<'a, 'b, T: Clone + Num + Neg<Output = T>> Pow<&'b $S> for &'a Complex<T> { |
64 | | type Output = Complex<T>; |
65 | | |
66 | | #[inline] |
67 | 0 | fn pow(self, exp: &$S) -> Self::Output { |
68 | 0 | self.pow(*exp) |
69 | 0 | } Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&i8>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&i16>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&i32>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&i64>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&isize>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&i128>>::pow |
70 | | } |
71 | | }; |
72 | | } |
73 | | |
74 | | pow_impl!(u8, i8); |
75 | | pow_impl!(u16, i16); |
76 | | pow_impl!(u32, i32); |
77 | | pow_impl!(u64, i64); |
78 | | pow_impl!(usize, isize); |
79 | | pow_impl!(u128, i128); |
80 | | |
81 | | // Note: we can't add `impl<T: Float> Pow<T> for Complex<T>` because new blanket impls are a |
82 | | // breaking change. Someone could already have their own `F` and `impl Pow<F> for Complex<F>` |
83 | | // which would conflict. We can't even do this in a new semantic version, because we have to |
84 | | // gate it on the "std" feature, and features can't add breaking changes either. |
85 | | |
86 | | macro_rules! powf_impl { |
87 | | ($F:ty) => { |
88 | | #[cfg(any(feature = "std", feature = "libm"))] |
89 | | impl<'a, T: Float> Pow<$F> for &'a Complex<T> |
90 | | where |
91 | | $F: Into<T>, |
92 | | { |
93 | | type Output = Complex<T>; |
94 | | |
95 | | #[inline] |
96 | 0 | fn pow(self, exp: $F) -> Self::Output { |
97 | 0 | self.powf(exp.into()) |
98 | 0 | } Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<f32>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<f64>>::pow |
99 | | } |
100 | | |
101 | | #[cfg(any(feature = "std", feature = "libm"))] |
102 | | impl<'a, 'b, T: Float> Pow<&'b $F> for &'a Complex<T> |
103 | | where |
104 | | $F: Into<T>, |
105 | | { |
106 | | type Output = Complex<T>; |
107 | | |
108 | | #[inline] |
109 | 0 | fn pow(self, &exp: &$F) -> Self::Output { |
110 | 0 | self.powf(exp.into()) |
111 | 0 | } Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&f32>>::pow Unexecuted instantiation: <&num_complex::Complex<_> as num_traits::pow::Pow<&f64>>::pow |
112 | | } |
113 | | |
114 | | #[cfg(any(feature = "std", feature = "libm"))] |
115 | | impl<T: Float> Pow<$F> for Complex<T> |
116 | | where |
117 | | $F: Into<T>, |
118 | | { |
119 | | type Output = Complex<T>; |
120 | | |
121 | | #[inline] |
122 | 0 | fn pow(self, exp: $F) -> Self::Output { |
123 | 0 | self.powf(exp.into()) |
124 | 0 | } Unexecuted instantiation: <num_complex::Complex<_> as num_traits::pow::Pow<f32>>::pow Unexecuted instantiation: <num_complex::Complex<_> as num_traits::pow::Pow<f64>>::pow |
125 | | } |
126 | | |
127 | | #[cfg(any(feature = "std", feature = "libm"))] |
128 | | impl<'b, T: Float> Pow<&'b $F> for Complex<T> |
129 | | where |
130 | | $F: Into<T>, |
131 | | { |
132 | | type Output = Complex<T>; |
133 | | |
134 | | #[inline] |
135 | 0 | fn pow(self, &exp: &$F) -> Self::Output { |
136 | 0 | self.powf(exp.into()) |
137 | 0 | } Unexecuted instantiation: <num_complex::Complex<_> as num_traits::pow::Pow<&f32>>::pow Unexecuted instantiation: <num_complex::Complex<_> as num_traits::pow::Pow<&f64>>::pow |
138 | | } |
139 | | }; |
140 | | } |
141 | | |
142 | | powf_impl!(f32); |
143 | | powf_impl!(f64); |
144 | | |
145 | | // These blanket impls are OK, because both the target type and the trait parameter would be |
146 | | // foreign to anyone else trying to implement something that would overlap, raising E0117. |
147 | | |
148 | | #[cfg(any(feature = "std", feature = "libm"))] |
149 | | impl<'a, T: Float> Pow<Complex<T>> for &'a Complex<T> { |
150 | | type Output = Complex<T>; |
151 | | |
152 | | #[inline] |
153 | 0 | fn pow(self, exp: Complex<T>) -> Self::Output { |
154 | 0 | self.powc(exp) |
155 | 0 | } |
156 | | } |
157 | | |
158 | | #[cfg(any(feature = "std", feature = "libm"))] |
159 | | impl<'a, 'b, T: Float> Pow<&'b Complex<T>> for &'a Complex<T> { |
160 | | type Output = Complex<T>; |
161 | | |
162 | | #[inline] |
163 | 0 | fn pow(self, &exp: &'b Complex<T>) -> Self::Output { |
164 | 0 | self.powc(exp) |
165 | 0 | } |
166 | | } |
167 | | |
168 | | #[cfg(any(feature = "std", feature = "libm"))] |
169 | | impl<T: Float> Pow<Complex<T>> for Complex<T> { |
170 | | type Output = Complex<T>; |
171 | | |
172 | | #[inline] |
173 | 0 | fn pow(self, exp: Complex<T>) -> Self::Output { |
174 | 0 | self.powc(exp) |
175 | 0 | } |
176 | | } |
177 | | |
178 | | #[cfg(any(feature = "std", feature = "libm"))] |
179 | | impl<'b, T: Float> Pow<&'b Complex<T>> for Complex<T> { |
180 | | type Output = Complex<T>; |
181 | | |
182 | | #[inline] |
183 | 0 | fn pow(self, &exp: &'b Complex<T>) -> Self::Output { |
184 | 0 | self.powc(exp) |
185 | 0 | } |
186 | | } |