aboutsummaryrefslogtreecommitdiff
path: root/src/complex.rs
blob: e49823478a78c0205c7910d151f336f3e912ebae (plain)
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
use std::num::ParseFloatError;
use std::str::FromStr;
use std::f64::consts::E;

#[derive(Clone, Copy, Default, Debug)]
pub struct Complex {
    pub real: f64,
    pub imag: f64,
}

impl Complex {
    pub fn new(real: f64, imag: f64) -> Self {
        Self { real, imag }
    }

    pub fn div(&mut self, other: &Self) {
        let z = other.real * other.real + other.imag * other.imag;
        self.real = self.real * other.real + self.imag * other.imag;
        self.real /= z;
        self.imag = self.imag * other.real - self.real * other.imag;
        self.imag /= z;
    }

    pub fn abs(&self) -> f64 {
        (self.real.powi(2) + self.imag.powi(2)).sqrt()
    }

    pub fn pow(&self, rhs: Self) -> Self {
        let r = self.abs();
        let x = (self.imag / self.real).atan();
        let lnr = r.ln();
        let alpha = lnr * rhs.imag + x * rhs.real;
        let e_exp = E.powf(lnr * rhs.real - rhs.imag * x);
        Complex::new(e_exp * alpha.cos(), e_exp * alpha.sin())
    }

    pub fn sqrt(&self) -> Self {
        self.pow(Complex::new(0.5, 0.0))
    }

    pub fn cos(&self) -> Self {
        let e : Complex = E.into();
        let i = Complex::new(0.0, 1.0);

        Complex::new(e.pow(&i * self).real, 0.0)
    }

    pub fn sin(&self) -> Self {
        let e : Complex = E.into();
        let i = Complex::new(0.0, 1.0);

        Complex::new(e.pow(&i * self).imag, 0.0)
    }

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

impl std::fmt::Display for Complex {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        if self.imag == 0.0 {
            write!(f, "{}", self.real)
        } else if self.real == 0.0 {
            if self.imag == 1.0 {
                write!(f, "i")
            } else {
                write!(f, "{}i", self.imag)
            }
        } else {
            if self.imag == 1.0 {
                write!(f, "{} + i", self.real)
            } else {
                write!(f, "{} + {}i", self.real, self.imag)
            }
        }
    }
}

#[derive(Debug)]
pub struct ComplexParseError;

impl From<ParseFloatError> for ComplexParseError {
    fn from(_: ParseFloatError) -> Self {
        Self {}
    }
}

impl FromStr for Complex {
    type Err = ComplexParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut c = Complex::default();
        let s = s.replace(' ', "");
        if s.contains('+') {
            let (a, b) = s.split_once('+').unwrap();
            if a.contains('i') {
                c.imag = a[..a.len() - 1].parse()?;
                c.real = b.parse()?;
            } else {
                c.real = a.parse()?;
                c.imag = b[..b.len() - 1].parse()?;
            }
        } else {
            if s.contains('i') {
                if s.len() == 1 {
                    c.imag = 1.0;
                } else {
                    c.imag = s[..s.len() - 1].parse()?;
                }
            } else {
                c.real = s.parse()?;
            }
        }

        Ok(c)
    }
}

impl Into<Complex> for f64 {
    fn into(self) -> Complex {
        Complex::new(self, 0.0)
    }
}

impl std::ops::Add for &Complex {
    type Output = Complex;

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

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

    fn add(self, rhs: Self) -> Self::Output {
        &self + &rhs
    }
}

impl std::ops::Sub for &Complex {
    type Output = Complex;

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

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

    fn sub(self, rhs: Self) -> Self::Output {
        &self - &rhs
    }
}

impl std::ops::Mul for &Complex {
    type Output = Complex;

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

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

    fn mul(self, rhs: Self) -> Self::Output {
        &self * &rhs
    }
}

impl std::ops::Div for &Complex {
    type Output = Complex;

    fn div(self, rhs: Self) -> Self::Output {
        let z = rhs.real * rhs.real + rhs.imag * rhs.imag;
        Complex::new(
            (self.real * rhs.real + self.imag * rhs.imag) / z,
            (self.imag * rhs.real - self.real * rhs.imag) / z,
        )
    }
}

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

    fn div(self, rhs: Self) -> Self::Output {
        &self / &rhs
    }
}