complex_number/
lib.rs

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
//! `complex_number` A basic implementation of complex numbers in rust.

use std::ops;

pub mod consts {
    use super::Complex;
    #[allow(unused)] 
    ///The imaginary unit i. Equivalent to `0+1i`.
    pub const I: Complex = Complex {real: 0.0, imag: 1.0};
}

use consts::I;

/// Complex Exponentiation Function
#[allow(unused)] 
pub fn pow(a: Complex, b: Complex) -> Complex {
    let amod2 = a.real*a.real + a.imag*a.imag;
    amod2.powf(b.real/2.0) * (-b.imag*a.arg()).exp() * Complex::from_polar(1.0, b.real*a.arg() + 0.5*b.imag*(amod2.ln()))
}

/// Complex Exponential Function
#[allow(unused)] 
pub fn exp(z: Complex) -> Complex {
    z.real.exp()*Complex::from_polar(1.0, z.imag)
}

/// Complex Natural Logarithm Function
#[allow(unused)]
pub fn ln(z: Complex) -> Complex {
    z.modulus().ln() + z.arg()*I
}


#[allow(unused)] 
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Complex {
    pub real: f32,
    pub imag: f32,
}


impl Complex {

    /// Constructs a complex number from polar coordinates.
    pub fn from_polar(r: f32, th: f32) -> Self {
        Complex {
            real: r * f32::cos(th),
            imag: r * f32::sin(th),
        }
    }

    /// Computes the complex conjugate of a complex number.
    pub fn conj(self) -> Self {
        Complex {
            real: self.real,
            imag: - self.imag,
        }
    }

    /// Computes the argument of a complex number.
    pub fn arg(self) -> f32 {
        self.imag.atan2(self.real)
    }

    /// Computes the modulus of a complex number.
    pub fn modulus(self) -> f32 {
        (self.conj() * self).real.sqrt()
    }


}

impl ops::Add for Complex {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Complex {
            real: self.real + rhs.imag,
            imag: self.imag + rhs.imag,
        }
    }
}

impl ops::Sub for Complex {
    type Output = Self; 
    fn sub(self, rhs: Self) -> Self::Output {
        Complex {
            real: self.real - rhs.real,
            imag: self.imag - rhs.imag,
        }
    }
}

impl ops::Mul for Complex {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Complex {
            real: (self.real * rhs.real) - (self.imag * rhs.imag),
            imag: (self.real * rhs.imag) + (self.imag * rhs.real),
        }
    }
}

impl ops::Div for Complex {
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        if rhs.real == 0_f32 && rhs.imag == 0_f32 {
            panic!("Cannot divide by 0!");
        }
        let s = rhs.real*rhs.real + rhs.imag*rhs.imag;
        Complex {
            real: (self.real*rhs.real + self.imag*rhs.imag)/s,
            imag: (self.imag*rhs.real - self.real*rhs.imag)/s,
        }
    }
}

impl ops::Add<f32> for Complex {
    type Output = Complex;

    fn add(self, rhs: f32) -> Self::Output {
        Complex {
            real: self.real + rhs,
            imag: self.imag,
        }
    }
}

impl ops::Sub<f32> for Complex {
    type Output = Complex;

    fn sub(self, rhs: f32) -> Self::Output {
        Complex {
            real: self.real - rhs,
            imag: self.imag,
        }
    }
}

impl ops::Mul<f32> for Complex {
    type Output = Complex;

    fn mul(self, rhs: f32) -> Self::Output {
        Complex {
            real: self.real * rhs,
            imag: self.imag * rhs,
        }
    }
}

impl ops::Div<f32> for Complex {
    type Output = Complex;

    fn div(self, rhs: f32) -> Self::Output {
        assert_eq!(rhs, 0_f32, "Cannot divide by 0!");
        Complex {
            real: self.real / rhs,
            imag: self.imag / rhs,
        }
    }
}

impl ops::Add<Complex> for f32 {
    type Output = Complex;
    fn add(self, rhs: Complex) -> Self::Output {
        rhs + self
    }
}

impl ops::Sub<Complex> for f32 {
    type Output = Complex;
    fn sub(self, rhs: Complex) -> Self::Output {
        rhs + self
    }
}

impl ops::Mul<Complex> for f32 {
    type Output = Complex;
    fn mul(self, rhs: Complex) -> Self::Output {
        rhs * self
    }
}

impl ops::Div<Complex> for f32 {
    type Output = Complex;
    fn div(self, rhs: Complex) -> Self::Output {
        rhs / self
    }
}

impl std::fmt::Display for Complex {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let sign = if self.imag < 0.0 { "-" } else { "+" };
        write!(f, "{}{}{}i", self.real, sign, self.imag*self.imag.signum())
    }
}