Theoretica
A C++ numerical and automatic 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
29
32
33
35 phasor() : phase(0), modulus(0) {}
36
37
40 this->phase = phase;
41 this->modulus = modulus;
42 }
43
44
47 phase = other.phase;
48 modulus = other.modulus;
49 }
50
52 template<typename T>
54 phase = z.arg();
55 modulus = z.norm();
56 }
57
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 Type inverse() const {
130
131 if(modulus < MACH_EPSILON) {
132 TH_MATH_ERROR("phasor::inverse", modulus, DIV_BY_ZERO);
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, DIV_BY_ZERO);
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
201 template<typename T>
202 inline phasor operator*(const complex<T>& other) const {
203 return phasor(
204 modulus * other.norm(),
205 phase + other.arg());
206 }
207
208
211 template<typename T>
212 inline friend phasor operator*(const complex<T>& z, const phasor& w) {
213 return phasor(
214 z.norm() * w.modulus,
215 z.arg() + w.phase
216 );
217 }
218
219
221 inline phasor operator/(const phasor& other) const {
222
223 if(abs(other.modulus) < MACH_EPSILON) {
224 TH_MATH_ERROR("phasor::operator/", other.modulus, DIV_BY_ZERO);
225 return phasor(nan(), nan());
226 }
227
228
229 return phasor(
230 modulus / other.modulus,
231 phase - other.phase);
232 }
233
234
236 inline phasor& operator+=(const phasor& other) {
237
238 if(phase == other.phase) {
240 } else {
241 *this = phasor(to_complex() + other.to_complex());
242 }
243
244 return *this;
245 }
246
247
249 inline phasor& operator-=(const phasor& other) {
250
251 if(phase == other.phase)
253 else
254 *this = phasor(to_complex() - other.to_complex());
255
256 return *this;
257 }
258
259
261 inline phasor& operator*=(const phasor& other) {
262
264 phase += other.phase;
265
266 return *this;
267 }
268
269
271 inline phasor& operator/=(const phasor& other) {
272
273 if(abs(other.modulus) < MACH_EPSILON) {
274 TH_MATH_ERROR("phasor::operator/=", other.modulus, DIV_BY_ZERO);
275 modulus = nan();
276 phase = nan();
277 return *this;
278 }
279
280 modulus /= other.modulus;
281 phase -= other.phase;
282
283 return *this;
284 }
285
286
288 inline bool operator==(const phasor& z) const {
289 return (modulus == z.modulus) && (phase == z.phase);
290 }
291
292
294 inline bool operator!=(const phasor& z) const {
295 return !(*this == z);
296 }
297
298
300 template<typename T = Type>
302 return complex<T>(
303 modulus * cos(phase),
304 modulus * sin(phase));
305 }
306
307
309 template<typename T>
310 inline operator complex<T> () {
311 return to_complex<T>();
312 }
313
314
317 inline static phasor rotor(Type rad) {
318 return phasor((Type) 1.0, rad);
319 }
320
321
323 inline static phasor i() {
324 return phasor(1, PI / 2.0);
325 }
326
327
328 // Friend operators to enable equations of the form
329 // (real) op. (phasor)
330
331 inline friend phasor operator+(Type r, const phasor& z) {
332 return z + phasor(r);
333 }
334
335 inline friend phasor operator-(Type r, const phasor& z) {
336 return phasor(z.modulus, z.phase + PI) + phasor(r);
337 }
338
339 inline friend phasor operator*(Type r, const phasor& z) {
340 return z * phasor(r);
341 }
342
343 inline friend phasor operator/(Type r, const phasor& z) {
344 return phasor(r) / z;
345 }
346
347
348#ifndef THEORETICA_NO_PRINT
349
351 inline std::string to_string(const std::string& separator = ", ") const {
352
353 std::stringstream res;
354 res << modulus << "/" << phase;
355 return res.str();
356 }
357
358
360 inline operator std::string() {
361 return to_string();
362 }
363
364
367 inline friend std::ostream& operator<<(std::ostream& out, const phasor& obj) {
368 return out << obj.to_string();
369 }
370
371#endif
372
373 };
374
375}
376
377#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:271
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
Type inverse() const
Compute the inverse of the complex number.
Definition phasor.h:129
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:323
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:221
phasor & operator+=(const phasor &other)
Add a phasor to this one.
Definition phasor.h:236
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:301
static phasor rotor(Type rad)
Construct a phasor representing a rotation of <rad> radians in 2 dimensions.
Definition phasor.h:317
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:202
phasor & operator-=(const phasor &other)
Subtract a phasor from this one.
Definition phasor.h:249
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:212
std::string to_string(const std::string &separator=", ") const
Convert the phasor to string representation.
Definition phasor.h:351
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:367
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:261
bool operator!=(const phasor &z) const
Check whether two phasors are not the same.
Definition phasor.h:294
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:288
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:225
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
std::remove_reference_t< decltype(std::declval< Structure >()[0])> vector_element_t
Extract the type of a vector (or any indexable container) from its operator[].
Definition core_traits.h:134
TH_CONSTEXPR real nan()
Return a quiet NaN number in floating point representation.
Definition error.h:54
dual2 cos(dual2 x)
Compute the cosine of a second order dual number.
Definition dual2_functions.h:86
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