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
// Symphonia
// Copyright (c) 2019-2022 The Project Symphonia Developers.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! The `complex` module implements a 32-bit floating point complex number.

/// A complex number.
#[derive(Copy, Clone, Default, Debug, PartialEq)]
#[repr(C)]
pub struct Complex {
    /// The real component.
    pub re: f32,
    /// The imaginary component.
    pub im: f32,
}

impl Complex {
    /// Create a new complex number.
    #[inline(always)]
    pub fn new(re: f32, im: f32) -> Self {
        Self { re, im }
    }

    /// Create a complex number with a value of `0 + j1`.
    #[inline(always)]
    pub fn j() -> Self {
        Self { re: 0.0, im: 1.0 }
    }

    /// Scale the complex number.
    #[inline(always)]
    pub fn scale(&self, scale: f32) -> Self {
        Self { re: self.re * scale, im: self.im * scale }
    }

    /// Take the complex conjugate of `self`.
    ///
    /// For a complex number defined as `a + jb` the complex conjugate is defined to be `a - jb`.
    #[inline(always)]
    pub fn conj(&self) -> Self {
        Self { re: self.re, im: -self.im }
    }
}

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

    #[inline(always)]
    fn add(self, rhs: Self) -> Self::Output {
        Self::Output { re: self.re + rhs.re, im: self.im + rhs.im }
    }
}

impl core::ops::AddAssign for Complex {
    #[inline(always)]
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

impl core::ops::Sub for Complex {
    type Output = Complex;

    #[inline(always)]
    fn sub(self, rhs: Self) -> Self::Output {
        Self::Output { re: self.re - rhs.re, im: self.im - rhs.im }
    }
}

impl core::ops::SubAssign for Complex {
    #[inline(always)]
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

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

    #[inline(always)]
    fn mul(self, rhs: Self) -> Self::Output {
        Self::Output {
            re: (self.re * rhs.re) - (self.im * rhs.im),
            im: (self.re * rhs.im) + (self.im * rhs.re),
        }
    }
}

impl core::ops::MulAssign for Complex {
    #[inline(always)]
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

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

    #[inline(always)]
    fn div(self, rhs: Self) -> Self::Output {
        let denom = rhs.re * rhs.re + rhs.im * rhs.im;

        Self::Output {
            re: (self.re * rhs.re + self.im * rhs.im) / denom,
            im: (self.im * rhs.re - self.re * rhs.im) / denom,
        }
    }
}

impl core::ops::DivAssign for Complex {
    #[inline(always)]
    fn div_assign(&mut self, rhs: Self) {
        *self = *self / rhs;
    }
}

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

    #[inline(always)]
    fn mul(self, rhs: f32) -> Self::Output {
        Self::Output { re: self.re * rhs, im: self.im * rhs }
    }
}

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

    #[inline(always)]
    fn div(self, rhs: f32) -> Self::Output {
        Self::Output { re: self.re / rhs, im: self.im / rhs }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn verify_complex() {
        assert_eq!(Complex::j(), Complex::new(0.0, 1.0));

        // Conjugate
        assert_eq!(Complex::new(1.0, 10.0).conj(), Complex::new(1.0, -10.0));

        // Scale
        assert_eq!(Complex::new(5.0, 2.0).scale(3.0), Complex::new(15.0, 6.0));

        // Addition
        assert_eq!(Complex::new(3.0, 13.0) + Complex::new(7.0, 17.0), Complex::new(10.0, 30.0));

        // Subtraction
        assert_eq!(Complex::new(3.0, 13.0) - Complex::new(7.0, 17.0), Complex::new(-4.0, -4.0));

        // Multiplication
        assert_eq!(Complex::new(3.0, 13.0) * Complex::new(7.0, 17.0), Complex::new(-200.0, 142.0));

        // Division
        assert_eq!(
            Complex::new(3.0, 13.0) / Complex::new(7.0, 17.0),
            Complex::new(121.0 / 169.0, 20.0 / 169.0)
        );

        // Scalar Multiplication
        assert_eq!(Complex::new(5.0, 2.0) * 3.0, Complex::new(15.0, 6.0));

        // Scalar Division
        assert_eq!(Complex::new(4.0, 2.0) / 2.0, Complex::new(2.0, 1.0));
    }
}