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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use core::ops::{Add, Div, Mul, Neg, Sub};
use core::ops::{AddAssign, SubAssign, MulAssign, DivAssign};
use std::{fmt, cmp::Ordering};

pub use crate::traits::{Number, Signed, Zero, One};

pub struct Complex<T> {
    /// Real part of the complex number
    pub real: T,
    /// Imaginary part of the complex number
    pub imag: T,
}

pub type Cmplx = Complex<f64>;

impl<T> Complex<T> {
    /// Create a new complex number ( z = x + iy )
    #[inline]
    pub const fn new(real: T, imag: T) -> Self {
        Complex { real, imag }
    }
}

impl<T: Clone + Signed> Complex<T> {
    /// Return the complex conjugate ( z.conj() = x - iy )
    #[inline]
    pub fn conj(&self) -> Self {
        Self::new(self.real.clone(), -self.imag.clone())
    }
}

impl<T: Clone> Clone for Complex<T> {
    /// Clone the complex number
    #[inline]
    fn clone(&self) -> Self {
        Self::new(self.real.clone(), self.imag.clone())
    }
}

impl<T: Clone + Signed> Neg for Complex<T> {
    type Output = Self;
    /// Return the unary negation ( unary - )
    /// - ( a + ib ) = -a - ib
    #[inline]
    fn neg(self) -> Self::Output {
        Self::Output::new( -self.real, -self.imag )
    }
}

impl<T: Clone + Number> Add<Complex<T>> for Complex<T> {
    type Output = Self;
    /// Add two complex numbers together ( binary + )
    /// ( a + ib ) + ( c + id ) = ( a + c ) + i( b + d )
    #[inline]
    fn add(self, plus: Self) -> Self::Output {
        Self::Output::new(self.real + plus.real, self.imag + plus.imag)
    }
}

impl<T: Clone + Number> Sub<Complex<T>> for Complex<T> {
    type Output = Self;
    /// Subtract one complex number from another ( binary - )
    /// ( a + ib ) - ( c + id ) = ( a - c ) + i( b - d )
    #[inline]
    fn sub(self, minus: Self) -> Self::Output {
        Self::Output::new(self.real - minus.real, self.imag - minus.imag)
    }
}

impl<T: Clone + Number> Mul<Complex<T>> for Complex<T> {
    type Output = Self;
    /// Multiply two complex numbers together ( binary * )
    /// ( a + ib ) * ( c + id ) = ( ac - bd ) + i( ad + bc )
    #[inline]
    fn mul(self, times: Self) -> Self::Output {
        let real = self.real.clone() * times.real.clone() - self.imag.clone() * times.imag.clone();  
        let imag = self.real * times.imag + self.imag * times.real;
        Self::Output::new( real, imag )
    }
}

impl<T: Clone + Number> Div<Complex<T>> for Complex<T> {
    type Output = Self;
    /// Divide one complex number by another ( binary / )
    /// ( a + ib ) / ( c + id ) = [( ac + bd ) + i( bc - ad )] / ( c^2 + d^2 )
    #[inline]
    fn div(self, divisor: Self) -> Self::Output {
        let denominator = divisor.real.clone() * divisor.real.clone() + divisor.imag.clone() * divisor.imag.clone();
        let real = self.real.clone() * divisor.real.clone() + self.imag.clone() * divisor.imag.clone();  
        let imag = self.imag * divisor.real - self.real * divisor.imag;
        Self::Output::new( real / denominator.clone(), imag / denominator )
    }
}

impl<T: Number> Add<T> for Complex<T> {
    type Output = Complex<T>;
    /// Add a complex number to a real number 
    /// ( a + ib ) + c = ( a + c ) + ib 
    fn add(self, plus: T) -> Self::Output {
        Self::Output::new( self.real + plus, self.imag )
    }
}

impl<T: Number> Sub<T> for Complex<T> {
    type Output = Complex<T>;
    /// Subtract a real number from a complex number 
    /// ( a + ib ) - c = ( a - c ) + ib 
    fn sub(self, minus: T) -> Self::Output {
        Self::Output::new( self.real - minus, self.imag )
    }
}

impl<T: Clone + Number> Mul<T> for Complex<T>  {
    type Output = Complex<T>;
    /// Multiply a complex number by a real scalar 
    /// ( a + ib ) * r = (a*r) + i(b*r) 
    fn mul(self, scalar: T) -> Self::Output {
        Self::Output::new( self.real * scalar.clone(), self.imag * scalar )
    }
}

impl<T: Clone + Number> Div<T> for Complex<T> {
    type Output = Complex<T>;
    /// Divide a complex number by a real scalar 
    /// ( a + ib ) / r = (a/r) + i(b/r) 
    fn div(self, scalar: T) -> Self::Output {
        Self::Output::new( self.real / scalar.clone(), self.imag / scalar )
    }
}

impl<T: Number> AddAssign for Complex<T> {
    /// Add a complex number to a mutable complex variable and assign the
    /// result to that variable ( += )
    fn add_assign(&mut self, rhs: Self) {
        self.real += rhs.real;
        self.imag += rhs.imag;
    }
}

impl<T: Number> SubAssign for Complex<T> {
    /// Subtract a complex number from a mutable complex variable and assign the
    /// result to that variable ( -= )
    fn sub_assign(&mut self, rhs: Self) {
        self.real -= rhs.real;
        self.imag -= rhs.imag;
    }
}

impl<T: Clone + Number> MulAssign for Complex<T> {
    /// Multipy a mutable complex variable by a complex number and assign the
    /// result to that variable ( *= )
    fn mul_assign(&mut self, rhs: Self) {
        let a = self.real.clone();

        self.real *= rhs.real.clone();
        self.real -= self.imag.clone() * rhs.imag.clone();

        self.imag *= rhs.real;
        self.imag += a * rhs.imag;
    }
}

impl<T: Clone + Number> DivAssign for Complex<T> {
    /// Divide a mutable complex variable by a complex number and assign the
    /// result to that variable ( *= )
    fn div_assign(&mut self, rhs: Self) {
        let a = self.real.clone();
        let denominator = rhs.real.clone() * rhs.real.clone() + rhs.imag.clone() * rhs.imag.clone();
        
        self.real *= rhs.real.clone();
        self.real += self.imag.clone() * rhs.imag.clone();
        self.real /= denominator.clone();

        self.imag *= rhs.real;
        self.imag -= a * rhs.imag;
        self.imag /= denominator;
    }
}

impl<T: Number> AddAssign<T> for Complex<T> {
    /// Add a real number to a mutable complex variable and assign the
    /// result to that variable ( += )
    fn add_assign(&mut self, rhs: T) {
        self.real += rhs;
    }
}

impl<T: Number> SubAssign<T> for Complex<T> {
    /// Subtract a real number from a mutable complex variable and assign the
    /// result to that variable ( += )
    fn sub_assign(&mut self, rhs: T) {
        self.real -= rhs;
    }
}

impl<T: Clone + Number> MulAssign<T> for Complex<T> {
    /// Multiply a mutable complex variable by a real number and assign the
    /// result to that variable ( *= )
    fn mul_assign(&mut self, rhs: T) {
        self.real *= rhs.clone();
        self.imag *= rhs;
    }
}

impl<T: Clone + Number> DivAssign<T> for Complex<T> {
    /// Divide a mutable complex variable by a real number and assign the
    /// result to that variable ( /= )
    fn div_assign(&mut self, rhs: T) {
        self.real /= rhs.clone();
        self.imag /= rhs;
    }
}

impl<T: Clone + Number> Zero for Complex<T> {
    /// Return the additive identity z = 0 + 0i
    fn zero() -> Self {
        Self::new(Zero::zero(), Zero::zero())
    }
}

impl<T: Clone + Number> One for Complex<T> {
    /// Return the multiplicative identity z = 1 + 0i
    fn one() -> Self {
        Self::new(One::one(), Zero::zero())
    }
}

impl<T: Clone + Number> PartialEq for Complex<T> {
    /// Implement trait for equality 
    fn eq(&self, other: &Complex<T>) -> bool {
        self.real == other.real && self.imag == other.imag
    }
}

impl<T: Clone + Number + std::cmp::PartialOrd> PartialOrd for Complex<T> {
    /// Implement trait for ordering 
    fn partial_cmp(&self, other: &Complex<T>) -> Option<Ordering> {
        if self.real != other.real {
            self.real.partial_cmp( &other.real )
        } else {
            self.imag.partial_cmp( &other.imag )
        }
    }
}

impl<T: Clone + Number> Complex<T> {
    /// Return the absolute value squared ( |z|^2 = z * z.conj() )
    #[inline]
    pub fn abs_sqr(&self) -> T {
        self.real.clone() * self.real.clone() + self.imag.clone() * self.imag.clone()
    }
}

impl Complex::<f64> {
    /// Return the absolute value ( |z| = sqrt( z * z.conj() ) )
    #[inline]
    pub fn abs(&self) -> f64 {
        libm::sqrt( self.abs_sqr() )
    }
}

impl Complex::<f64> {
    /// Return the phase angle in radians
    #[inline]
    pub fn arg(&self) -> f64 {
        self.imag.atan2(self.real)
    }
}

impl<T> fmt::Display for Complex<T> where
    T: fmt::Display
{
    /// Format the output ( z = x + iy = ( x, y ) )
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "( {}, {} )", self.real, self.imag )
    }
} 

impl<T> fmt::Debug for Complex<T> where
    T: fmt::Debug
{
    /// Format the output ( z = x + iy = ( x, y ) )
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "( {:?}, {:?} )", self.real, self.imag )
    }
} 

impl<T: Number + Clone> Number for Complex<T> {

}

impl Signed for Complex<f64> {
    fn abs(&self) -> Self {
        //let real = self.abs();
        let real: f64 = self.abs();
        let imag: f64 = 0.0;
        Complex { real, imag }
    }
}

impl Copy for Complex<f64> {

}