rs_sci/
complex.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
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
use std::fmt::{Display, Formatter, Result as FmtResult};
use std::ops::{Add, Div, Mul, Neg, Sub};

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Complex<T> {
    re: T,
    im: T,
}

impl<T> Complex<T> {
    /// creates new complex number from real and imaginary parts
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(3.0, 4.0); // 3 + 4i
    /// ```
    /// ---
    /// basic constructor for complex numbers
    pub fn new(re: T, im: T) -> Self {
        Self { re, im }
    }
}

impl<T: Copy> Complex<T> {
    pub fn re(&self) -> T {
        self.re
    }

    pub fn im(&self) -> T {
        self.im
    }
}

impl Complex<f64> {
    /// calculates absolute value (modulus) of complex number
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(3.0, 4.0);
    /// assert_eq!(z.modulus(), 5.0);
    /// ```
    /// ---
    /// computes √(re² + im²)
    pub fn modulus(&self) -> f64 {
        (self.re * self.re + self.im * self.im).sqrt()
    }

    /// calculates argument (phase) of complex number
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(1.0, 1.0);
    /// assert_eq!(z.argument(), std::f64::consts::PI/4.0);
    /// ```
    /// ---
    /// returns angle in radians from positive real axis

    pub fn argument(&self) -> f64 {
        self.im.atan2(self.re)
    }
    /// returns complex conjugate
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(3.0, 4.0);
    /// let conj = z.conjugate(); // 3 - 4i
    /// ```
    /// ---
    /// negates imaginary part while keeping real part

    pub fn conjugate(&self) -> Self {
        Self::new(self.re, -self.im)
    }
    /// computes exponential of complex number
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::I * std::f64::consts::PI;
    /// let exp_z = z.exp(); // ≈ -1 + 0i
    /// ```
    /// ---
    /// uses euler's formula: e^(a+bi) = e^a(cos(b) + i*sin(b))
    pub fn exp(&self) -> Self {
        let r = self.re.exp();
        Self::new(r * self.im.cos(), r * self.im.sin())
    }
    /// computes natural logarithm of complex number
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(1.0, 0.0);
    /// let ln_z = z.ln(); // 0 + 0i
    /// ```
    /// ---
    /// returns ln|z| + i*arg(z)
    pub fn ln(&self) -> Self {
        Complex::new(self.modulus().ln(), self.argument())
    }
    /// raises complex number to real power
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(1.0, 1.0);
    /// let z_squared = z.pow(2.0);
    /// ```
    /// ---
    /// uses polar form for computation
    pub fn pow(&self, n: f64) -> Self {
        let r = self.modulus().powf(n);
        let theta = self.argument() * n;
        Self::new(r * theta.cos(), r * theta.sin())
    }
    /// calculates square root of complex number
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(-1.0, 0.0);
    /// let sqrt_z = z.sqrt(); // 0 + 1i
    /// ```
    /// ---
    /// returns principal square root
    pub fn sqrt(&self) -> Self {
        let r = self.modulus().sqrt();
        let theta = self.argument() / 2.0;
        Self::new(r * theta.cos(), r * theta.sin())
    }

    pub fn sin(&self) -> Self {
        Self::new(
            self.re.sin() * self.im.cosh(),
            self.re.cos() * self.im.sinh(),
        )
    }

    pub fn cos(&self) -> Self {
        Self::new(
            self.re.cos() * self.im.cosh(),
            -self.re.sin() * self.im.sinh(),
        )
    }

    pub fn tan(&self) -> Self {
        self.sin() / self.cos()
    }

    pub fn sinh(&self) -> Self {
        Self::new(
            self.re.sinh() * self.im.cos(),
            self.re.cosh() * self.im.sin(),
        )
    }

    pub fn cosh(&self) -> Self {
        Self::new(
            self.re.cosh() * self.im.cos(),
            self.re.sinh() * self.im.sin(),
        )
    }

    pub fn tanh(&self) -> Self {
        self.sinh() / self.cosh()
    }
}
impl<T: Copy + Add<Output = T>> Add for Complex<T> {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re + rhs.re,
            im: self.im + rhs.im,
        }
    }
}

impl<T: Copy + Sub<Output = T>> Sub for Complex<T> {
    type Output = Self;

    fn sub(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re - rhs.re,
            im: self.im - rhs.im,
        }
    }
}

impl<T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T>> Mul for Complex<T> {
    type Output = Self;

    fn mul(self, rhs: Self) -> Self::Output {
        Self {
            re: self.re * rhs.re - self.im * rhs.im,
            im: self.re * rhs.im + self.im * rhs.re,
        }
    }
}

impl<T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>> Div
    for Complex<T>
{
    type Output = Self;

    fn div(self, rhs: Self) -> Self::Output {
        let denom = rhs.re * rhs.re + rhs.im * rhs.im;
        Self {
            re: (self.re * rhs.re + self.im * rhs.im) / denom,
            im: (self.im * rhs.re - self.re * rhs.im) / denom,
        }
    }
}

impl<T: Neg<Output = T>> Neg for Complex<T> {
    type Output = Self;

    fn neg(self) -> Self::Output {
        Self::new(-self.re, -self.im)
    }
}

impl<T: Display> Display for Complex<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "{}+{}i", self.re, self.im)
    }
}

impl From<f64> for Complex<f64> {
    fn from(x: f64) -> Self {
        Self::new(x, 0.0)
    }
}

impl From<i32> for Complex<f64> {
    fn from(x: i32) -> Self {
        Self::new(x as f64, 0.0)
    }
}

impl Complex<f64> {
    pub const I: Complex<f64> = Complex { re: 0.0, im: 1.0 };
    pub const ONE: Complex<f64> = Complex { re: 1.0, im: 0.0 };
    pub const ZERO: Complex<f64> = Complex { re: 0.0, im: 0.0 };
}

impl Complex<f64> {
    /// creates complex number from polar coordinates
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::from_polar(2.0, std::f64::consts::PI/4.0);
    /// ```
    /// ---
    /// converts (r,θ) to x + yi form
    pub fn from_polar(r: f64, theta: f64) -> Self {
        Self::new(r * theta.cos(), r * theta.sin())
    }
    
    /// converts to polar coordinates
    ///
    /// #### Example
    /// ```txt
    /// let z = Complex::new(1.0, 1.0);
    /// let (r, theta) = z.to_polar();
    /// ```
    /// ---
    /// returns tuple of (modulus, argument)
    pub fn to_polar(&self) -> (f64, f64) {
        (self.modulus(), self.argument())
    }
}