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
/*
   Appellation: math <traits>
   Contrib: FL03 <jo3mccain@icloud.com>
*/
use nd::{Array, Dimension};
use num::complex::Complex;
use num::{Float, Num, Signed, Zero};

pub trait AsComplex {
    type Real;

    fn as_complex(&self, real: bool) -> Complex<Self::Real>;

    fn as_re(&self) -> Complex<Self::Real> {
        self.as_complex(true)
    }

    fn as_im(&self) -> Complex<Self::Real> {
        self.as_complex(false)
    }
}

pub trait IntoComplex: AsComplex {
    fn into_complex(self, real: bool) -> Complex<Self::Real>
    where
        Self: Sized,
    {
        self.as_complex(real)
    }

    fn into_re(self) -> Complex<Self::Real>
    where
        Self: Sized,
    {
        self.as_complex(true)
    }
}

pub trait Conjugate {
    type Output;

    fn conj(&self) -> Self::Output;
}

pub trait FloorDiv<Rhs = Self> {
    type Output;

    fn floor_div(self, rhs: Rhs) -> Self::Output;
}

pub trait RoundTo {
    fn round_to(&self, places: usize) -> Self;
}

pub trait SquareRoot {
    fn sqrt(self) -> Self;
}

/*
 ********* Implementations *********
*/
impl<T> AsComplex for T
where
    T: Copy + Num,
{
    type Real = T;

    fn as_complex(&self, real: bool) -> Complex<Self> {
        match real {
            true => Complex::new(*self, Self::zero()),
            false => Complex::new(Self::zero(), *self),
        }
    }
}

impl<T> IntoComplex for T where T: AsComplex {}

macro_rules! impl_conj {
    ($($t:ident<$res:ident>),*) => {
        $(
            impl_conj!(@impl $t<$res>);
        )*
    };
    (@impl $t:ident<$res:ident>) => {
        impl Conjugate for $t {
            type Output = $res<$t>;

            fn conj(&self) -> Self::Output {
                Complex { re: *self, im: -$t::zero() }
            }
        }
    };
}

impl_conj!(f32<Complex>, f64<Complex>);

impl<T> Conjugate for Complex<T>
where
    T: Clone + Signed,
{
    type Output = Complex<T>;

    fn conj(&self) -> Self {
        Complex::<T>::conj(self)
    }
}

impl<T, D> Conjugate for Array<T, D>
where
    D: Dimension,
    T: Clone + num::complex::ComplexFloat,
{
    type Output = Array<T, D>;
    fn conj(&self) -> Self::Output {
        self.mapv(|x| x.conj())
    }
}

impl<T> FloorDiv for T
where
    T: Copy + Num,
{
    type Output = T;

    fn floor_div(self, rhs: Self) -> Self::Output {
        crate::floor_div(self, rhs)
    }
}

// impl<A, B, C> Pow<B> for A where A: num::traits::Pow<B, Output = C> {
//     type Output = C;

//     fn pow(self, rhs: B) -> Self::Output {
//         num::traits::Pow::pow(self, rhs)
//     }
// }

// impl<D, A, B, C> Pow<i32> for Array<A, D> where A: Clone + num::traits::Pow<B, Output = C>, D: Dimension  {
//     type Output = Array<C, D>;

//     fn pow(self, rhs: B) -> Self::Output {
//         self.mapv(|x| x.pow(rhs))
//     }
// }

impl<T> RoundTo for T
where
    T: Float,
{
    fn round_to(&self, places: usize) -> Self {
        crate::round_to(*self, places)
    }
}

impl SquareRoot for f32 {
    fn sqrt(self) -> Self {
        f32::sqrt(self)
    }
}

impl SquareRoot for f64 {
    fn sqrt(self) -> Self {
        f64::sqrt(self)
    }
}

impl<T> SquareRoot for Complex<T>
where
    T: Float,
{
    fn sqrt(self) -> Self {
        Complex::<T>::sqrt(self)
    }
}

impl<T, D> SquareRoot for Array<T, D>
where
    D: Dimension,
    T: Float,
{
    fn sqrt(self) -> Self {
        self.mapv(|x| x.sqrt())
    }
}