scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
complex_number.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_MECA_NUMBER_COMPLEX_NUMBER_HPP_INCLUDED
2#define SCIFIR_UNITS_MECA_NUMBER_COMPLEX_NUMBER_HPP_INCLUDED
3
4#include "./angle.hpp"
5#include "../util/is_number.hpp"
6#include "../units/scalar_unit.hpp"
7#include "../util/types.hpp"
8
9#include "boost/algorithm/string.hpp"
10
11#include <cmath>
12#include <string>
13
14using namespace std;
15
16namespace scifir
17{
18 template<class T>
20 {
21 public:
24
27
28 complex_number(complex_number<T>&& x) : real(std::move(x.real)),imaginary(std::move(x.imaginary))
29 {}
30
31 explicit complex_number(const scalar_unit& x,const scalar_unit& y) : real(x),imaginary(y)
32 {}
33
34 explicit complex_number(const string& x,const string& y) : real(T(x)),imaginary(T(y))
35 {}
36
38 {
39 if (init_complex_number.find("+") != string::npos or init_complex_number.find("-") != string::npos)
40 {
42 boost::split(numbers,init_complex_number,boost::is_any_of("+-"));
43 if (numbers.size() == 2)
44 {
45 boost::trim(numbers[0]);
46 boost::trim(numbers[1]);
47 if (numbers[1].substr(numbers[1].length() - 3) == "(i)")
48 {
49 real = T(numbers[0]);
50 imaginary = T(numbers[1].substr(0,numbers[1].length() - 3));
51 if (init_complex_number.find("-") != string::npos)
52 {
53 imaginary *= -1;
54 }
55 }
56 else
57 {
58 real = T();
59 imaginary = T();
60 }
61 }
62 }
63 else
64 {
65 real = T();
66 imaginary = T();
67 }
68 }
69
71 {
72 real = x.real;
73 imaginary = x.imaginary;
74 return *this;
75 }
76
78 {
79 real = std::move(x.real);
80 imaginary = std::move(x.imaginary);
81 return *this;
82 }
83
84 template<typename U>
86 {
87 return complex_number<T>(T(real + x.real),T(imaginary + x.imaginary));
88 }
89
90 template<typename U>
92 {
93 return complex_number<T>(T(real - x.real),T(imaginary - x.imaginary));
94 }
95
96 template<typename U>
98 {
99 return complex_number<scalar_unit>(real * x.real - imaginary * x.imaginary,real * x.imaginary + imaginary * x.real);
100 }
101
102 template<typename U>
104 {
105 scalar_unit new_real = (real * x.real + x.imaginary * imaginary) / ((real^2) + (imaginary^2));
106 scalar_unit new_imaginary = (x.imaginary * real - x.real * imaginary) / ((real^2) + (imaginary^2));
108 }
109
110 template<typename U>
112 {
113 real += x.real;
114 imaginary += x.imaginary;
115 }
116
117 template<typename U>
119 {
120 real -= x.real;
121 imaginary -= x.imaginary;
122 }
123
125 {
126 return complex_number<T>(real,imaginary * -1);
127 }
128
129 T get_r() const
130 {
132 }
133
135 {
136 if (imaginary != 0 and real > 0)
137 {
139 }
140 else if (real < 0 and imaginary == 0)
141 {
142 return angle(180.0f);
143 }
144 else
145 {
146 return angle();
147 }
148 }
149
156
157 string display(int number_of_decimals = 2) const
158 {
160 output << real.display(number_of_decimals);
161 if (imaginary >= 0)
162 {
163 output << " + ";
164 }
165 else
166 {
167 output << " - ";
168 }
169 output << display_float(std::abs(imaginary.get_value()),number_of_decimals) << " " << imaginary.display_dimensions() << "(i)";
170 return output.str();
171 }
172
175 };
176
177 template<typename T>
179 {
180 return x.display(2);
181 }
182
183 bool is_complex(const string& init_complex_number);
184
185 template<typename T>
187 {
188 return T(scifir::sqrt(scifir::pow(x.real,2) + scifir::pow(x.imaginary,2)));
189 }
190
191 template<typename T>
193 {
194 int sgn_value;
195 if (x.imaginary > 0)
196 {
197 sgn_value = 1;
198 }
199 else if (x.imaginary < 0)
200 {
201 sgn_value = -1;
202 }
203 else
204 {
205 sgn_value = 0;
206 }
207 scalar_unit new_real = scifir::sqrt(x.real + scifir::sqrt((x.real^2) + (x.imaginary^2)));
208 scalar_unit new_imaginary = sgn_value * scifir::sqrt(((-1) * x.real + scifir::sqrt((x.real^2) + (x.imaginary^2))) / 2);
210 }
211
212 /*template<typename T>
213 T sin(complex_number<T> x)
214 {
215 return std::sin(float(x.real));
216 }*/
217}
218
219template<typename T,typename U>
221{
222 if (x.real == y.real and x.imaginary == y.imaginary)
223 {
224 return true;
225 }
226 else
227 {
228 return false;
229 }
230}
231
232template<typename T,typename U>
234{
235 return !(x == y);
236}
237
238template<typename T>
239bool operator ==(const scifir::complex_number<T>& x, const string& init_complex_number)
240{
241 return (x == scifir::complex_number<T>(init_complex_number));
242}
243
244template<typename T>
245bool operator !=(const scifir::complex_number<T>& x, const string& init_complex_number)
246{
247 return !(x == init_complex_number);
248}
249
250template<typename T>
251bool operator ==(const string& init_complex_number, const scifir::complex_number<T>& x)
252{
253 return (scifir::complex_number<T>(init_complex_number) == x);
254}
255
256template<typename T>
257bool operator !=(const string& init_complex_number, const scifir::complex_number<T>& x)
258{
259 return !(init_complex_number == x);
260}
261
262template<typename T>
263void operator +=(string& x, const scifir::complex_number<T>& y)
264{
265 x += to_string(y);
266}
267
268template<typename T>
269string operator +(const string& x, const scifir::complex_number<T>& y)
270{
271 return x + to_string(y);
272}
273
274template<typename T>
275string operator +(const scifir::complex_number<T>& x, const string& y)
276{
277 return to_string(x) + y;
278}
279
280template<typename T>
281ostream& operator <<(ostream& os, const scifir::complex_number<T>& x)
282{
283 return os << to_string(x);
284}
285
286template<typename T>
287istream& operator >>(istream& is, scifir::complex_number<T>& x)
288{
289 char a[256];
290 is.getline(a, 256);
291 string b(a);
293 return is;
294}
295
296#endif // SCIFIR_UNITS_MECA_NUMBER_COMPLEX_NUMBER_HPP_INCLUDED
Class that allows to work with angles. Each angle sizes 4 bytes. Initialization string example: "20°"...
Definition angle.hpp:77
complex_number(const scalar_unit &x, const scalar_unit &y)
complex_number< scalar_unit > operator/(const complex_number< U > &x) const
complex_number(const string &init_complex_number)
complex_number< scalar_unit > get_reciprocal() const
complex_number(const string &x, const string &y)
string display(int number_of_decimals=2) const
void operator-=(const complex_number< U > &x)
complex_number< T > & operator=(const complex_number< T > &x)
void operator+=(const complex_number< U > &x)
complex_number< T > operator+(const complex_number< U > &x) const
complex_number(const complex_number< T > &x)
complex_number< T > get_conjugate() const
complex_number(complex_number< T > &&x)
complex_number< T > operator-(const complex_number< U > &x) const
complex_number< scalar_unit > operator*(const complex_number< U > &x) const
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
bool operator!=(const scifir::complex_number< T > &x, const scifir::complex_number< U > &y)
istream & operator>>(istream &is, scifir::complex_number< T > &x)
ostream & operator<<(ostream &os, const scifir::complex_number< T > &x)
bool operator==(const scifir::complex_number< T > &x, const scifir::complex_number< U > &y)
void operator+=(string &x, const scifir::complex_number< T > &y)
string operator+(const string &x, const scifir::complex_number< T > &y)
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
T abs(const complex_number< T > &x)
scalar_unit pow(const scalar_unit &x, int exponent)
Exponentiates a scalar_unit to some numeric type, the dimensions are also exponentiated.
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
angle atan(float x)
Calculates the atan of some value x and returns the result as angle in degrees.
Definition angle.cpp:451
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
bool is_complex(const string &init_complex_number)
angle sqrt(const angle &x)
Calculates the square root of the angle x and returns that new angle.
Definition angle.cpp:416