aboutsummaryrefslogtreecommitdiff
path: root/libpsi-core/src/maths/complex.rs
blob: 86e4788f8dbbab52f2193d4904af28e7fe9c475a (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
use crate::Float;
use core::{fmt, ops};

#[macro_export]
macro_rules! complex {
    ($real:expr, $imaginary:expr) => {
        $crate::Complex::new($real, $imaginary)
    };
}

macro_rules! impl_ops {
    ($trait:ident, $method:ident, $op:tt) => {
        impl<T: Float> ops::$trait for Complex<T> {
            type Output = Complex<T>;

            fn $method(self, other: Complex<T>) -> Complex<T> {
                Complex {
                    real: self.real $op other.real,
                    imaginary: self.imaginary $op other.imaginary,
                }
            }
        }
    };

    ($trait:ident, $method:ident, $op:tt, real) => {
        impl<T: Float> ops::$trait<T> for Complex<T> {
            type Output = Complex<T>;

            fn $method(self, other: T) -> Complex<T> {
                Complex {
                    real: self.real $op other,
                    imaginary: self.imaginary,
                }
            }
        }
    };

    ($trait_assign:ident, $method_assign:ident, $op:tt, assign) => {
        impl<T: Float> ops::$trait_assign for Complex<T> {
            fn $method_assign(&mut self, other: Complex<T>) {
                self.real = self.real $op other.real;
                self.imaginary = self.imaginary $op other.imaginary;
            }
        }
    };

    ($trait_assign:ident, $method_assign:ident, $op:tt, assign_real) => {
        impl<T: Float> ops::$trait_assign<T> for Complex<T> {
            fn $method_assign(&mut self, other: T) {
                self.real = self.real $op other;
            }
        }
    };
}

#[derive(Copy, Clone, PartialOrd, PartialEq)]
pub struct Complex<T: Float> {
    pub real: T,
    pub imaginary: T,
}

impl<T: Float + fmt::Debug> fmt::Debug for Complex<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Complex {{ real: {:?}, imaginary: {:?} }}",
            self.real, self.imaginary
        )
    }
}

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

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

    fn neg(self) -> Complex<T> {
        Complex {
            real: -self.real,
            imaginary: -self.imaginary,
        }
    }
}

impl<T: Float> From<T> for Complex<T> {
    fn from(real: T) -> Complex<T> {
        Complex {
            real,
            imaginary: T::zero(),
        }
    }
}

impl<T: Float> Complex<T> {
    pub fn new(real: T, imaginary: T) -> Complex<T> {
        Complex { real, imaginary }
    }

    pub fn get_conjugate(&self) -> Complex<T> {
        Complex {
            real: self.real,
            imaginary: -self.imaginary,
        }
    }

    pub fn conjugate(&mut self) {
        self.imaginary = -self.imaginary;
    }

    pub fn phase(&self) -> T {
        T::atan2(self.imaginary, self.real)
    }

    pub fn norm2(&self) -> T {
        self.real * self.real + self.imaginary * self.imaginary
    }

    pub fn abs(&self) -> T {
        T::sqrt(self.norm2())
    }
}

impl_ops!(Add, add, +);
impl_ops!(Sub, sub, -);
impl_ops!(Mul, mul, *);
impl_ops!(Div, div, /);

impl_ops!(AddAssign, add_assign, +, assign);
impl_ops!(SubAssign, sub_assign, -, assign);
impl_ops!(MulAssign, mul_assign, *, assign);
impl_ops!(DivAssign, div_assign, /, assign);

impl_ops!(Add, add, +, real);
impl_ops!(Sub, sub, -, real);
impl_ops!(Mul, mul, *, real);
impl_ops!(Div, div, /, real);