-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplex.cpp
317 lines (284 loc) · 6.95 KB
/
complex.cpp
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "complex.h"
complex::complex() //default constructor
{
real = 0;
img = 0;
}
complex::complex(float R, float I)//constructor
{
real = R;
img = I;
}
void complex::operator= (complex scnd)
{
real = scnd.real;
img = scnd.img;
}
void complex::operator+=(complex scnd)
{
(*this) = ((*this) + scnd);
}
void complex::operator-=(complex scnd)
{
(*this) = ((*this) - scnd);
}
void complex::operator*=(complex scnd)
{
(*this) = ((*this) * scnd);
}
void complex::operator/=(complex scnd)
{
(*this) = ((*this) / scnd);
}
bool complex::operator==(complex scnd)const
{
return (real == scnd.real) && (img == scnd.img);
}
bool complex::operator!=(complex scnd)const
{
return (real != scnd.real) || (img != scnd.img);
}
complex complex::operator+(complex scnd)const
{
return complex(real + scnd.real, img + scnd.img);
}
complex complex::operator-(complex scnd)const
{
return complex(real - scnd.real, img - scnd.img);
}
complex complex::operator*(complex scnd)const
{
return complex(real*scnd.real - img*scnd.img, real*scnd.img + img*scnd.real);
}
complex complex::operator/(complex scnd)const
{
if (scnd.real == 0 && scnd.img == 0)
return complex(nanf(""), nanf(""));
return complex(((*this)*scnd.cconjugate()).real / (scnd*scnd.cconjugate()).real, ((*this)*scnd.cconjugate()).img / (scnd*scnd.cconjugate()).real);
}
complex complex::operator^(float powr)const
{
complex temp;
if (powr == 0)
{
return complex(1, 0);
}
else if (abs((powr - (int)powr)) < Range) //almost an integer
{
temp = (*this);
for (int n = 1; n < abs(round(powr)); n++)
temp *= (*this);
if (powr > 0)
return temp;
else
temp = complex(1, 0) / temp;
}
else //rational power
{
float denom_pow = (abs((powr - (int)powr)));
temp = (*this);
for (int n = 1; n < round((int)powr*(1 / denom_pow)) + 1; n++) //power 12.5 = 25/2
temp *= (*this); //done with the intgeral part of the power
complex A(pow(temp.get_magnitude(), (denom_pow)), 0);
complex B(cos(denom_pow*temp.get_phase('R')), sin(denom_pow*temp.get_phase('R')));
temp = A*B; //now (*this) will have the value of the first^abs(power)
if (powr > 0)
return temp;
else
temp = complex(1, 0) / temp;
}
return temp;
}
void complex::set_real(float realIn)
{
real = realIn;
}
void complex::set_img(float imgIn)
{
img = imgIn;
}
float complex::get_real(void)const
{
return real;
}
float complex::get_imaginary(void)const
{
return img;
}
float complex::get_phase(char format)const //'R' for rads , 'D' for degrees
{
switch (format)
{
case 'd':
case 'D':
return atan2(img, real) * 180 / PI;
case'r':
case'R':
return atan2(img, real);
}
}
float complex::get_magnitude(void)const
{
return sqrt(pow(real, 2) + pow(img, 2));
}
void complex::print(void)const //note that there must be a member of the class calling the function (e.g it must be like x.print_complex())
{
if (real < Range && real > -Range) //equivalent to real == 0 with a little range precision
{
if (img < Range && img > -Range) //equivalent to img == 0 with a little range precision
cout << 0;
else
{
if (img < Range + 1 && img > -Range + 1)
cout << "i";
else
{
if (img < Range + round(img) && img > -Range + round(img))
cout << round(img) << "i";
else
cout << img << "i";
}
}
}
else
{
if (img < Range && img > -Range)
{
if (real < Range + round(real) && real > -Range + round(real))
cout << round(real);
else
cout << real;
}
else
{
if (img < Range + 1 && img > -Range + 1)
{
if (real < Range + round(real) && real > -Range + round(real))
cout << round(real);
else
cout << real;
cout << "+i";
}
else
{
if (img < Range - 1 && img > -Range - 1)
{
if (real < Range + round(real) && real > -Range + round(real))
cout << round(real);
else
cout << real;
cout << "-i";
}
else
{
if (img > 0)
{
if (real < Range + round(real) && real > -Range + round(real))
cout << round(real);
else
cout << real;
cout << "+";
if (img < Range + round(img) && img > -Range + round(img))
cout << round(img);
else
cout << img;
cout << "i";
}
else
{
if (real < Range + round(real) && real > -Range + round(real))
cout << round(real);
else
cout << real;
if (img < Range + round(img) && img > -Range + round(img))
cout << round(img);
else
cout << img;
cout << "i";
}
}
}
}
}
}
void complex::print(int null)const //note that there must be a member of the class calling the function (e.g it must be like x.print_complex())
{
if (has_NaN())
{
cout << "Sorry Number is Out of Precision Range, But You Can Still Use It";
return;
}
if (real == 0)
{
if (img == 0)
cout << 0;
else
{
if (img == 1)
cout << "i";
else
cout << img << "i";
}
}
else
{
if (img == 0)
cout << real;
else
{
if (img == 1)
cout << real << "+i";
else
{
if (img == -1)
cout << real << "-i";
else
{
if (img < 0)
cout << real << img << "i";
else
cout << real << "+" << img << "i";
}
}
}
}
} //not used //precision controlled print when needed : swap parameters of this and the other print
void complex::conjugate(complex var)
{
real = var.real;
img = var.img*(-1);
}
int complex::divide(complex first, complex second)
{
if (second.real == 0 && second.img == 0)
(*this) = complex(nanf(0), nanf(0));
(*this) = complex(((first*second.cconjugate())).real / (second*second.cconjugate()).real, ((first*second.cconjugate())).img / (second*second.cconjugate()).real);
return 0;
}
complex complex::p_r(float magnitude, float phase)const //polar to rectangular just for internal use
{
//use real as magnitude , img as phase
return complex(magnitude*cos(phase), magnitude*sin(phase));
}
complex complex::cconjugate(void)const
{
return complex(real, -img);
} //complex conjugate
bool complex::has_NaN(void)const
{
if (isnan(real) || isnan(img))
return true;
else
return false;
}
//not used
complex complex::r_p() //rectangular to polar just for internal use
{
//use real as magnitude , img as phase
return complex(sqrt(pow(real, 2) + pow(img, 2)), (atan2(img, real) * 180));
}
complex complex::p_r() //polar to rectangular just for internal use
{
//use real as magnitude , img as phase
return complex(real*cos(img), real*sin(img));
}