consteig
Compile-time eigenvalue and eigenvector computation for C++17
Loading...
Searching...
No Matches
complex.hpp
1#ifndef COMPLEX_HPP
2#define COMPLEX_HPP
3
4#include "../consteig_options.hpp"
5
6#include "functions/utilities.hpp"
7
8namespace consteig
9{
10
25template <typename T> struct Complex
26{
29
31 constexpr Complex(T r = 0, T i = 0) : real(r), imag(i)
32 {
33 }
34
36 constexpr Complex operator+(const Complex &other) const
37 {
38 return {real + other.real, imag + other.imag};
39 }
40
42 constexpr Complex &operator+=(const Complex &other)
43 {
44 real += other.real;
45 imag += other.imag;
46 return *this;
47 }
48
50 constexpr Complex operator-(const Complex &other) const
51 {
52 return {real - other.real, imag - other.imag};
53 }
54
56 constexpr Complex operator*(const Complex &other) const
57 {
58 return {real * other.real - imag * other.imag,
59 real * other.imag + imag * other.real};
60 }
61
63 constexpr Complex operator/(const Complex &other) const
64 {
65 T denom = other.real * other.real + other.imag * other.imag;
66 return {(real * other.real + imag * other.imag) / denom,
67 (imag * other.real - real * other.imag) / denom};
68 }
69
72 constexpr bool operator==(const Complex &other) const
73 {
74 return real == other.real && imag == other.imag;
75 }
76
78 constexpr bool operator!=(const Complex &other) const
79 {
80 return !(*this == other);
81 }
82};
83
86template <typename T>
87constexpr Complex<T> operator+(const T &scalar, const Complex<T> &c)
88{
89 return {scalar + c.real, c.imag};
90}
91
94template <typename T>
95constexpr Complex<T> operator+(const Complex<T> &c, const T &scalar)
96{
97 return {c.real + scalar, c.imag};
98}
99
102template <typename T>
103constexpr Complex<T> operator-(const T &scalar, const Complex<T> &c)
104{
105 return {scalar - c.real, -c.imag};
106}
107
110template <typename T>
111constexpr Complex<T> operator-(const Complex<T> &c, const T &scalar)
112{
113 return {c.real - scalar, c.imag};
114}
115
118template <typename T>
119constexpr Complex<T> operator*(const T &scalar, const Complex<T> &c)
120{
121 return {scalar * c.real, scalar * c.imag};
122}
123
126template <typename T>
127constexpr Complex<T> operator*(const Complex<T> &c, const T &scalar)
128{
129 return scalar * c;
130}
131
136template <typename T> constexpr Complex<T> conj(const Complex<T> &c)
137{
138 return {c.real, -c.imag};
139}
140
141} // namespace consteig
142
143#endif
constexpr T epsilon()
Machine epsilon for type T.
Definition utilities.hpp:82
Constexpr complex number type.
Definition complex.hpp:26
T real
Real part.
Definition complex.hpp:27
constexpr Complex operator*(const Complex &other) const
Complex multiplication: (a+bi)(c+di) = (ac-bd) + (ad+bc)i.
Definition complex.hpp:56
constexpr Complex operator-(const Complex &other) const
Complex subtraction.
Definition complex.hpp:50
constexpr bool operator==(const Complex &other) const
Exact equality (no tolerance). Prefer equalWithin for floats.
Definition complex.hpp:72
constexpr Complex operator+(const Complex &other) const
Complex addition.
Definition complex.hpp:36
constexpr bool operator!=(const Complex &other) const
Inequality.
Definition complex.hpp:78
constexpr Complex(T r=0, T i=0)
Construct from real and imaginary parts (default: 0 + 0i).
Definition complex.hpp:31
constexpr Complex & operator+=(const Complex &other)
Complex addition in-place.
Definition complex.hpp:42
constexpr Complex operator/(const Complex &other) const
Complex division: divides by |other|^2.
Definition complex.hpp:63
T imag
Imaginary part.
Definition complex.hpp:28