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
use core::ops::{BitAnd, Shr};

use num::{complex::ComplexFloat, pow::Pow, traits::Inv, Integer, One};

use crate::{ComplexOp, MaybeList, ToZpk, Zpk};


impl<T, Z, P, K, I> Pow<I> for Zpk<T, Z, P, K>
where
    T: ComplexFloat + ComplexOp<T, Output = T>,
    Z: MaybeList<T>,
    P: MaybeList<T>,
    K: ComplexFloat<Real = T::Real> + ComplexOp<K, Output = K>,
    I: Integer + BitAnd<I, Output = I> + Shr<usize, Output = I> + Copy,
    Self: ToZpk<T, Vec<T>, Vec<T>, K, (), ()> + Inv<Output: ToZpk<T, Vec<T>, Vec<T>, K, (), ()>>
{
    type Output = Zpk<T, Vec<T>, Vec<T>, K>;

    fn pow(self, mut n: I) -> Self::Output
    {
        let mut x = if n < I::zero()
        {
            self.inv()
                .to_zpk((), ())
        }
        else
        {
            self.to_zpk((), ())
        };
        let mut r = if (n & I::one()) == I::one()
        {
            x.clone()
        }
        else
        {
            One::one()
        };
    
        let two = I::one() + I::one();
        loop
        {
            n = n/two;
            if n == I::zero()
            {
                break;
            }
            x = x.as_view()*x.as_view();
            if (n & I::one()) == I::one()
            {
                r = r*x.as_view();
            }
        }
    
        r
    }
}