-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcomplex.h
More file actions
211 lines (191 loc) · 4.86 KB
/
Copy pathcomplex.h
File metadata and controls
211 lines (191 loc) · 4.86 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright Heikki Berg 2017 - 2018
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file ../../LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#if !defined(CDSP_COMPLEX)
#define CDSP_COMPLEX
#include "dsp_types.h"
#include "dsp_math.h"
namespace cdsp
{
/// Complex numbers
template<typename T>
class complex
{
public:
/// Constructor taking a real and imaginary parts
explicit complex(T real = T{}, T imag = T{}) :
m_real(real),
m_imag(imag)
{
};
/// Copy constuctor
complex(complex const&) = default;
/// Move constructor
complex(complex&& value) noexcept :
m_real(std::move(value.m_real)),
m_imag(std::move(value.m_imag))
{
};
/// Copy constructor from different template parameter
template<typename FromT>
complex(complex<FromT> const& value);
/// Assignment operator
complex& operator=(complex const&) = default;
/// Move assignment
complex& operator=(complex && rhs) noexcept
{
m_real = std::move(rhs.m_real);
m_imag = std::move(rhs.m_imag);
return *this;
};
/// Assignment from different template parameter
template<typename FromT>
complex& operator=(complex<FromT> const& rhs);
/// Addition
complex& operator+=(complex const& rhs);
/// Subtraction
complex& operator-=(complex const& rhs);
/// Multiplication
complex& operator*=(complex const& rhs);
/// Division
complex& operator/=(complex const& rhs);
/// Absolute value
T abs() const;
/// Conjugation
void conj();
/// Destructor
~complex() = default;
/// Get the real part
T real() const { return m_real; };
/// Get the imaginary part
T imag() const { return m_imag; };
private:
/// Real part
T m_real;
/// Imaginary part
T m_imag;
};
/// Conjugation
template<typename T>
complex<T> conj(complex<T> const& value);
/// Absolute value
template<typename T>
T abs(complex<T> const& value);
/// Addition
template<typename T>
complex<T> operator+(complex<T> const& lhs, complex<T> const& rhs);
/// Subtraction
template<typename T>
complex<T> operator-(complex<T> const& lhs, complex<T> const& rhs);
/// Multiplication
template<typename T>
complex<T> operator*(complex<T> const& lhs, complex<T> const& rhs);
/// Division
template<typename T>
complex<T> operator/(complex<T> const& lhs, complex<T> const& rhs);
///////////////////////////////
/////// IMPLEMENTATIONS ///////
///////////////////////////////
template<typename T>
template<typename FromT>
complex<T>::complex(complex<FromT> const& value) :
m_real(static_cast<T>(value.real())),
m_imag(static_cast<T>(value.imag()))
{
}
template<typename T>
template<typename FromT>
complex<T>& complex<T>::operator=(complex<FromT> const& rhs)
{
m_real = static_cast<T>(rhs.real());
m_imag = static_cast<T>(rhs.imag());
return *this;
}
template<typename T>
complex<T>& complex<T>::operator+=(complex<T> const& rhs)
{
m_real += rhs.m_real;
m_imag += rhs.m_imag;
return *this;
}
template<typename T>
complex<T>& complex<T>::operator-=(complex<T> const& rhs)
{
m_real -= rhs.m_real;
m_imag -= rhs.m_imag;
return *this;
}
template<typename T>
complex<T>& complex<T>::operator*=(complex<T> const& rhs)
{
T real = m_real * rhs.m_real - m_imag * rhs.m_imag;
T imag = m_imag * rhs.m_real + m_real * rhs.m_imag;
m_real = real;
m_imag = imag;
return *this;
}
template<typename T>
complex<T>& complex<T>::operator/=(complex<T> const& rhs)
{
T real = m_real * rhs.m_real + m_imag * rhs.m_imag;
T imag = m_imag * rhs.m_real - m_real * rhs.m_imag;
T div = rhs.m_real * rhs.m_real + rhs.m_imag * rhs.m_imag;
m_real = cdsp::math::divides<T,T>()(real, div);
m_imag = cdsp::math::divides<T,T>()(imag, div);
return *this;
}
template<typename T>
complex<T> operator+(complex<T> const& lhs, complex<T> const& rhs)
{
complex<T> res(lhs);
res += rhs;
return res;
}
template<typename T>
complex<T> operator-(complex<T> const& lhs, complex<T> const& rhs)
{
complex<T> res(lhs);
res -= rhs;
return res;
}
template<typename T>
complex<T> operator*(complex<T> const& lhs, complex<T> const& rhs)
{
complex<T> res(lhs);
res *= rhs;
return res;
}
template<typename T>
complex<T> operator/(complex<T> const& lhs, complex<T> const& rhs)
{
complex<T> res(lhs);
res /= rhs;
return res;
}
template<typename T>
T complex<T>::abs() const
{
T val = m_real * m_real + m_imag * m_imag;
T res = cdsp::math::sqrt(val);
return res;
}
template<typename T>
T abs(complex<T> const& value)
{
return value.abs();
}
template<typename T>
void complex<T>::conj()
{
m_imag = -m_imag;
}
template<typename T>
complex<T> conj(complex<T> const& value)
{
complex<T> ret(value);
ret.conj();
return ret;
}
} // namespace cdsp
#endif //CDSP_COMPLEX