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
//! Complex numbers
use std::ops::{Add, Sub, Mul, Div, Neg};

/// Complex number
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Complex {
    /// Real component of complex number
    pub re: f64,
    /// Imaginary component of complex number
    pub im: f64
}
impl Complex {
    /// Create a new complex number
    pub fn new(re: f64, im: f64) -> Complex {
        Complex { re: re, im: im }
    }
    /// Compute the complex conjugate of a number
    pub fn conj(self) -> Complex {
        Complex { re: self.re, im: -self.im }
    }
}

impl Add for Complex {
    type Output = Complex;
    fn add(self, rhs: Complex) -> Complex {
        Complex::new(self.re + rhs.re, self.im + rhs.im)
    }
}
impl Sub for Complex {
    type Output = Complex;
    fn sub(self, rhs: Complex) -> Complex {
        Complex::new(self.re - rhs.re, self.im - rhs.im)
    }
}
impl Mul for Complex {
    type Output = Complex;
    fn mul(self, rhs: Complex) -> Complex {
        let (a, b, c, d) = (self.re, self.im, rhs.re, rhs.im);
        Complex::new(
            a * c - b * d,
            b * c + a * d
        )
    }
}
impl Div for Complex {
    type Output = Complex;
    fn div(self, rhs: Complex) -> Complex {
        let (a, b, c, d) = (self.re, self.im, rhs.re, rhs.im);
        let denom = c * c + d * d;
        Complex::new(
            (a * c + b * d) / denom,
            (b * c - a * d) / denom
        )
    }
}
impl Neg for Complex {
    type Output = Complex;
    fn neg(self) -> Complex {
        Complex::new(-self.re, -self.im)
    }
}