-
Notifications
You must be signed in to change notification settings - Fork 14
/
complex.hh
155 lines (133 loc) · 3.28 KB
/
complex.hh
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
/*
Fast complex math
Copyright 2018 Ahmet Inan <[email protected]>
*/
#pragma once
namespace DSP {
template <typename T>
class Complex
{
T re, im;
public:
typedef T value_type;
constexpr Complex() : re(0), im(0) {}
constexpr Complex(T r) : re(r), im(0) {}
constexpr Complex(T r, T i) : re(r), im(i) {}
constexpr T real() const { return re; }
constexpr T imag() const { return im; }
inline void real(T r) { re = r; }
inline void imag(T i) { im = i; }
inline Complex<T> operator = (T a)
{
re = a;
im = 0;
return *this;
}
inline Complex<T> operator += (Complex<T> a)
{
return *this = a + *this;
}
inline Complex<T> operator -= (Complex<T> a)
{
return *this = *this - a;
}
inline Complex<T> operator *= (Complex<T> a)
{
return *this = a * *this;
}
inline Complex<T> operator *= (T a)
{
return *this = a * *this;
}
inline Complex<T> operator /= (T a)
{
return *this = *this / a;
}
inline Complex<T> operator /= (Complex<T> a)
{
return *this = *this / a;
}
};
template <typename T>
static constexpr bool operator == (Complex<T> a, Complex<T> b)
{
return a.real() == b.real() && a.imag() == b.imag();
}
template <typename T>
static constexpr bool operator != (Complex<T> a, Complex<T> b)
{
return a.real() != b.real() || a.imag() != b.imag();
}
template <typename T>
static constexpr Complex<T> operator + (Complex<T> a, Complex<T> b)
{
return Complex<T>(a.real() + b.real(), a.imag() + b.imag());
}
template <typename T>
static constexpr Complex<T> operator + (Complex<T> a)
{
return a;
}
template <typename T>
static constexpr Complex<T> operator - (Complex<T> a, Complex<T> b)
{
return Complex<T>(a.real() - b.real(), a.imag() - b.imag());
}
template <typename T>
static constexpr Complex<T> operator - (Complex<T> a)
{
return Complex<T>(-a.real(), -a.imag());
}
template <typename T>
static constexpr Complex<T> operator * (T a, Complex<T> b)
{
return Complex<T>(a * b.real(), a * b.imag());
}
template <typename T>
static constexpr Complex<T> operator / (Complex<T> a, T b)
{
return Complex<T>(a.real() / b, a.imag() / b);
}
template <typename T>
static constexpr Complex<T> operator * (Complex<T> a, Complex<T> b)
{
return Complex<T>(a.real() * b.real() - a.imag() * b.imag(), a.real() * b.imag() + a.imag() * b.real());
}
template <typename T>
static constexpr Complex<T> operator / (T a, Complex<T> b)
{
return Complex<T>((a * b.real()) / (b.real() * b.real() + b.imag() * b.imag()),
- (a * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()));
}
template <typename T>
static constexpr Complex<T> operator / (Complex<T> a, Complex<T> b)
{
return Complex<T>((a.real() * b.real() + a.imag() * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()),
(a.imag() * b.real() - a.real() * b.imag()) / (b.real() * b.real() + b.imag() * b.imag()));
}
template <typename T>
static constexpr Complex<T> conj(Complex<T> a)
{
return Complex<T>(a.real(), -a.imag());
}
template <typename T>
static constexpr T norm(Complex<T> a)
{
return a.real() * a.real() + a.imag() * a.imag();
}
template <typename T>
static constexpr T abs(Complex<T> a)
{
return sqrt(norm(a));
}
template <typename T>
static constexpr T arg(Complex<T> a)
{
return atan2(a.imag(), a.real());
}
template <typename T>
static constexpr Complex<T> polar(T a, T b)
{
return Complex<T>(a * cos(b), a * sin(b));
}
}