Theoretica
Mathematical Library
Loading...
Searching...
No Matches
phasor.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_PHASOR
7#define THEORETICA_PHASOR
8
9#ifndef THEORETICA_NO_PRINT
10#include <sstream>
11#include <ostream>
12#endif
13
14#include "../core/real_analysis.h"
15#include "./complex.h"
16
17
18namespace theoretica {
19
20
23 template<typename Type = real>
24 class phasor {
25 public:
26
28 Type modulus;
29
31 Type phase;
32
33
35 phasor() : phase(0), modulus(0) {}
36
37
39 phasor(Type modulus, Type phase) {
40 this->phase = phase;
41 this->modulus = modulus;
42 }
43
44
46 phasor(const phasor& other) {
47 phase = other.phase;
48 modulus = other.modulus;
49 }
50
52 template<typename T>
53 phasor(const complex<T>& z) {
54 phase = z.arg();
55 modulus = z.norm();
56 }
57
59 phasor(Type r) {
60 modulus = abs(r);
61 phase = (r >= 0 ? 0 : PI);
62 }
63
64
66 inline phasor& operator=(const phasor& z) {
67 phase = z.phase;
68 modulus = z.modulus;
69 return *this;
70 }
71
72
75 template<typename T>
76 inline phasor& operator=(const std::array<T, 2>& v) {
77 modulus = v[0];
78 phase = v[1];
79 return *this;
80 }
81
82
83 ~phasor() = default;
84
85
87 inline Type Re() const {
88 return modulus * cos(phase);
89 }
90
91
93 inline friend Type Re(const phasor& z) {
94 return z.Re();
95 }
96
97
99 inline Type Im() const {
100 return modulus * sin(phase);
101 }
102
103
105 inline friend Type Im(const phasor& z) {
106 return z.Im();
107 }
108
109
111 inline phasor conjugate() const {
112 return phasor(modulus, -phase);
113 }
114
115
117 inline Type sqr_norm() const {
118 return modulus * modulus;
119 }
120
121
123 inline Type norm() const {
124 return modulus;
125 }
126
127
129 inline phasor inverse() const {
130
131 if(modulus < MACH_EPSILON) {
132 TH_MATH_ERROR("phasor::inverse", modulus, MathError::DivByZero);
133 return (Type) nan();
134 }
135
136 return phasor(
137 ((Type) 1.0) / modulus,
138 -phase
139 );
140 }
141
142
144 inline phasor& invert() {
145
146 if(modulus < MACH_EPSILON) {
147 TH_MATH_ERROR("phasor::invert", modulus, MathError::DivByZero);
148 modulus = (Type) nan();
149 phase = (Type) nan();
150 return *this;
151 }
152
153 phase = phase * -1;
154 modulus = ((Type) 1.0) / modulus;
155 return *this;
156 }
157
158
160 inline Type arg() const {
161 return phase;
162 }
163
164
169 inline phasor operator+(const phasor& other) const {
170
171 if(abs(phase - other.phase) < MACH_EPSILON)
172 return phasor(modulus + other.modulus, phase);
173
174 return phasor(to_complex() + other.to_complex());
175 }
176
177
182 inline phasor operator-(const phasor& other) const {
183
184 if(abs(phase - other.phase) < MACH_EPSILON)
185 return phasor(modulus - other.modulus, phase);
186
187 return phasor(to_complex() - other.to_complex());
188 }
189
190
192 inline phasor operator*(const phasor& other) const {
193 return phasor(
194 modulus * other.modulus,
195 phase + other.phase);
196 }
197
198
200 template<typename T>
201 inline phasor operator*(const complex<T>& other) const {
202 return phasor(
203 modulus * other.norm(),
204 phase + other.arg());
205 }
206
207
209 template<typename T>
210 inline friend phasor operator*(const complex<T>& z, const phasor& w) {
211 return phasor(
212 z.norm() * w.modulus,
213 z.arg() + w.phase
214 );
215 }
216
217
219 inline phasor operator/(const phasor& other) const {
220
221 if(abs(other.modulus) < MACH_EPSILON) {
222 TH_MATH_ERROR("phasor::operator/", other.modulus, MathError::DivByZero);
223 return phasor(nan(), nan());
224 }
225
226
227 return phasor(
228 modulus / other.modulus,
229 phase - other.phase);
230 }
231
232
234 inline phasor& operator+=(const phasor& other) {
235
236 if(phase == other.phase) {
237 modulus += other.modulus;
238 } else {
239 *this = phasor(to_complex() + other.to_complex());
240 }
241
242 return *this;
243 }
244
245
247 inline phasor& operator-=(const phasor& other) {
248
249 if(phase == other.phase)
250 modulus -= other.modulus;
251 else
252 *this = phasor(to_complex() - other.to_complex());
253
254 return *this;
255 }
256
257
259 inline phasor& operator*=(const phasor& other) {
260
261 modulus *= other.modulus;
262 phase += other.phase;
263
264 return *this;
265 }
266
267
269 inline phasor& operator/=(const phasor& other) {
270
271 if(abs(other.modulus) < MACH_EPSILON) {
272 TH_MATH_ERROR("phasor::operator/=", other.modulus, MathError::DivByZero);
273 modulus = nan();
274 phase = nan();
275 return *this;
276 }
277
278 modulus /= other.modulus;
279 phase -= other.phase;
280
281 return *this;
282 }
283
284
286 inline bool operator==(const phasor& z) const {
287 return (modulus == z.modulus) && (phase == z.phase);
288 }
289
290
292 inline bool operator!=(const phasor& z) const {
293 return !(*this == z);
294 }
295
296
298 template<typename T = Type>
299 complex<T> to_complex() const {
300 return complex<T>(
301 modulus * cos(phase),
302 modulus * sin(phase));
303 }
304
305
307 template<typename T>
308 inline operator complex<T> () {
309 return to_complex<T>();
310 }
311
312
315 inline static phasor rotor(Type rad) {
316 return phasor((Type) 1.0, rad);
317 }
318
319
321 inline static phasor i() {
322 return phasor(1, PI / 2.0);
323 }
324
325
326 // Friend operators to enable equations of the form
327 // (real) op. (phasor)
328
329 inline friend phasor operator+(Type r, const phasor& z) {
330 return z + phasor(r);
331 }
332
333 inline friend phasor operator-(Type r, const phasor& z) {
334 return phasor(z.modulus, z.phase + PI) + phasor(r);
335 }
336
337 inline friend phasor operator*(Type r, const phasor& z) {
338 return z * phasor(r);
339 }
340
341 inline friend phasor operator/(Type r, const phasor& z) {
342 return phasor(r) / z;
343 }
344
345
346#ifndef THEORETICA_NO_PRINT
347
349 inline std::string to_string(const std::string& separator = ", ") const {
350
351 std::stringstream res;
352 res << modulus << "/" << phase;
353 return res.str();
354 }
355
356
358 inline operator std::string() {
359 return to_string();
360 }
361
362
365 inline friend std::ostream& operator<<(std::ostream& out, const phasor& obj) {
366 return out << obj.to_string();
367 }
368
369#endif
370
371 };
372
373}
374
375#endif
Complex number in exponential form .
Definition phasor.h:24
phasor operator+(const phasor &other) const
Add two phasors.
Definition phasor.h:169
phasor & operator/=(const phasor &other)
Divide this phasor by another one.
Definition phasor.h:269
Type modulus
Modulus of the complex number.
Definition phasor.h:28
Type Re() const
Get the real part of the complex number.
Definition phasor.h:87
phasor conjugate() const
Compute the conjugate of the complex number.
Definition phasor.h:111
Type sqr_norm() const
Compute the square norm of the complex number.
Definition phasor.h:117
friend Type Re(const phasor &z)
Extract the real part of the complex number.
Definition phasor.h:93
static phasor i()
Imaginary unit in exponential form.
Definition phasor.h:321
phasor inverse() const
Compute the inverse of the complex number.
Definition phasor.h:129
Type Im() const
Get the imaginary part of the complex number.
Definition phasor.h:99
phasor operator/(const phasor &other) const
Divide two phasors.
Definition phasor.h:219
phasor & operator+=(const phasor &other)
Add a phasor to this one.
Definition phasor.h:234
phasor & operator=(const std::array< T, 2 > &v)
Assignment operator from a 2D array as {modulus, phase}.
Definition phasor.h:76
phasor operator-(const phasor &other) const
Subtract two phasors.
Definition phasor.h:182
phasor(Type modulus, Type phase)
Initialize from two real numbers.
Definition phasor.h:39
phasor & operator=(const phasor &z)
Assignment operator.
Definition phasor.h:66
friend Type Im(const phasor &z)
Extract the imaginary part of the complex number.
Definition phasor.h:105
Type phase
Phase of the complex number.
Definition phasor.h:31
phasor(Type r)
Construct a phasor from a real number.
Definition phasor.h:59
phasor & invert()
Invert the complex number.
Definition phasor.h:144
phasor operator*(const phasor &other) const
Multiply two phasors.
Definition phasor.h:192
complex< T > to_complex() const
Transform a phasor to a complex number.
Definition phasor.h:299
static phasor rotor(Type rad)
Construct a phasor representing a rotation of <rad> radians in 2 dimensions.
Definition phasor.h:315
phasor(const complex< T > &z)
Construct a phasor from a complex number.
Definition phasor.h:53
phasor operator*(const complex< T > &other) const
Multiply a complex number in algebraic form and a phasor.
Definition phasor.h:201
phasor & operator-=(const phasor &other)
Subtract a phasor from this one.
Definition phasor.h:247
Type arg() const
Get the argument of the complex number.
Definition phasor.h:160
friend phasor operator*(const complex< T > &z, const phasor &w)
Multiply a complex number in algebraic form and a phasor.
Definition phasor.h:210
std::string to_string(const std::string &separator=", ") const
Convert the phasor to string representation.
Definition phasor.h:349
friend std::ostream & operator<<(std::ostream &out, const phasor &obj)
Stream the phasor in string representation to an output stream (std::ostream)
Definition phasor.h:365
Type norm() const
Compute the norm of the complex number.
Definition phasor.h:123
phasor(const phasor &other)
Initialize from another phasor.
Definition phasor.h:46
phasor & operator*=(const phasor &other)
Multiply this phasor by another one.
Definition phasor.h:259
bool operator!=(const phasor &z) const
Check whether two phasors are not the same.
Definition phasor.h:292
phasor()
Initialize as 0/0.
Definition phasor.h:35
bool operator==(const phasor &z) const
Check whether two phasors are the same.
Definition phasor.h:286
Complex number class.
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compiling opti...
Definition error.h:238
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
dual2 abs(dual2 x)
Compute the absolute value of a second order dual number.
Definition dual2_functions.h:198
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:78
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
@ DivByZero
Division by zero.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:207
dual2 sin(dual2 x)
Compute the sine of a second order dual number.
Definition dual2_functions.h:72
constexpr real PI
The Pi mathematical constant.
Definition constants.h:216