-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcomplex.cpp
executable file
·330 lines (261 loc) · 7.38 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
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
* complex.cpp
* Implementation file for complex number class
* Michael F. Hutt
* Jan. 1999
*
* Copyright (c) 1999 Michael F. Hutt
* Released under the MIT License
*
*/
#include "complex.h"
static Complex j = Complex(0.0,1.0);
static Complex i = Complex(0.0,1.0);
// default constructor
Complex::Complex(void) {real=0.0; imag=0.0;}
// constructor
Complex::Complex(double r) {real=r; imag=0.0;}
// constructor
Complex::Complex(double r,double i) {real=r; imag=i;}
// return the real part
double Complex::re(void) { return(this->real); }
double re(Complex z) { return z.re(); };
// return the imagimary part
double Complex::im(void) { return(this->imag); }
double im(Complex z) { return z.im(); }
// return the complex conjugate
Complex Complex::conj(void) { return Complex(this->real,-this->imag); }
Complex conj(Complex z) { return Complex(z.re(),-z.im()); }
// return the magnitude
double Complex::abs(void) { return sqrt(real*real+imag*imag); }
double abs(Complex z) { return sqrt(z.re()*z.re()+z.im()*z.im()); }
// return the phase argument
double Complex::arg(void) { return atan2(this->imag,this->real); }
double arg(Complex z) { return atan2(z.im(),z.re()); }
// return the norm
double Complex::norm(void) { return real*real+imag*imag; }
double norm(Complex z) { return z.re()*z.re()+z.im()*z.im(); }
// overload the + operator to add 2 complex numbers
Complex Complex::operator+(Complex z)
{
return Complex(this->real+z.real,this->imag+z.imag);
}
// overload the + operator to add complex and a double
Complex Complex::operator+(double a)
{
return Complex(this->real+a,this->imag);
}
// This is a friend function
// overload the + operator to add a double and a complex
Complex operator+(double a, Complex z)
{
return Complex(a+z.re(),z.im());
}
// overload the - operator to subtract 2 complex numbers
Complex Complex::operator-(Complex z)
{
return Complex(this->real-z.real,this->imag-z.imag);
}
// overload the - operator to subtract a double from a complex
Complex Complex::operator-(double a)
{
return Complex(this->real-a,this->imag);
}
// overload the - operator to subtract a complex from a double
Complex operator-(double a, Complex z)
{
return Complex(a-z.re(),-z.im());
}
// overload the - operator to take the negative of a complex
Complex operator-(Complex z)
{
return Complex(-z.real,-z.imag);
}
// overload the * operator to multiply two complex numbers
Complex Complex::operator*(Complex z)
{
return Complex(this->real*z.real-this->imag*z.imag,
this->real*z.imag+this->imag*z.real);
}
// overload the * operator to multiply a complex by a double
Complex Complex::operator*(double a)
{
return Complex(real*a,imag*a);
}
// overload the * operator to multiply a double by a complex
Complex operator*(double a, Complex z)
{
return Complex(a*z.re(),a*z.im());
}
// overload the / operator to divide two complex numbers
Complex Complex::operator/(Complex z)
{
Complex top((*this)*z.conj());
double bottom(z.norm());
Complex quo(top/bottom);
return quo;
}
// overload the / operator to divide a complex number by a double
Complex Complex::operator/(double a)
{
return Complex(this->real/a,this->imag/a);
}
// overload the / operator to divide a double by a complex
Complex operator/(double a, Complex z)
{
Complex top((a)*z.conj());
double bottom(z.norm());
Complex quo(top/bottom);
return quo;
}
// overload the += operator
const Complex& Complex::operator+=(const Complex& z)
{
this->real+=z.real;
this->imag+=z.imag;
return *this;
}
// overload the -= operator
const Complex& Complex::operator-=(const Complex& z)
{
this->real-=z.real;
this->imag-=z.imag;
return *this;
}
// overload the == operator
int Complex::operator==(Complex z)
{
if (this->real == z.re() && this->imag == z.im()) {
return 1;
}
else {
return 0;
}
}
// take the square root of a complex number
Complex sqrt(Complex z)
{
double zsqre,zsqim;
zsqre = sqrt(0.5*(z.abs()+z.re()));
zsqim = sqrt(0.5*(z.abs()-z.re()));
if (z.im() >= 0.0) {
return Complex(zsqre,zsqim);
}
else {
return Complex(zsqre,-zsqim);
}
}
// take the natural log of a complex number
Complex log(Complex z)
{
if (z.re() < 0 && z.im() == 0.0) {
return log(z.abs())+j*PI;
}
else {
return log(z.abs())+j*z.arg();
}
}
// raise e to a complex number
Complex exp(Complex z)
{
return exp(z.re())*(cos(z.im())+j*sin(z.im()));
}
// raise a complex number to a double
Complex pow(Complex z, double c)
{
return exp(c*log(z));
}
// take the sin of a complex number
Complex sin(Complex z)
{
return 0.5*(-j)*exp(j*z)-0.5*(-j)*exp(-j*z);
}
// take the cos of a complex number
Complex cos(Complex z)
{
return 0.5*exp(j*z)+0.5*exp(-j*z);
}
// take the tan of a complex number
Complex tan(Complex z) { return sin(z)/cos(z); }
// take the sec of a complex number
Complex sec(Complex z) { return 1/cos(z); }
// take the csc of a complex number
Complex csc(Complex z) { return 1/sin(z); }
// take the cot of a complex number
Complex cot(Complex z) { return cos(z)/sin(z); }
// take the sinh of a complex number
Complex sinh(Complex z) { return (exp(z)-exp(-z))/2.0; }
// take the cosh of a complex number
Complex cosh(Complex z) { return (exp(z)+exp(-z))/2.0; }
// take the tanh of a complex number
Complex tanh(Complex z) { return sinh(z)/cosh(z); }
// take the sech of a complex number
Complex sech(Complex z) { return 1/cosh(z); }
// take the csch of a complex number
Complex csch(Complex z) { return 1/sinh(z); }
// take the coth of a complex number
Complex coth(Complex z) { return cosh(z)/sinh(z); }
// take the asin of a complex number
Complex asin(Complex z) { return -j*log(j*z+sqrt(1.0-z*z)); }
// take the acos of a complex number
Complex acos(Complex z) { return -j*log(z+sqrt(z*z-1.0)); }
// take the atan of a complex number
Complex atan(Complex z) { return (0.5*j)*log((j+z)/(j-z)); }
// take the asinh of a complex number
Complex asinh(Complex z) { return log(z+sqrt(z*z+1.0)); }
// take the acosh of a complex number
Complex acosh(Complex z) { return log(z+sqrt(z*z-1.0)); }
// take the atanh of a complex number
Complex atanh(Complex z) { return 0.5*log((1.0+z)/(1.0-z)); }
// create an inserter function so
// complex types work with cout
std::ostream& operator<<(std::ostream& stream, Complex z)
{
if (z.im() < 0) {
stream << z.re();
stream << z.im() <<"j";
}
else {
stream << z.re() <<"+";
stream << z.im() <<"j";
}
return stream;
}
// round a complex number
Complex Complex::rnd(int precision)
{
double rnum,inum;
int tnum;
rnum = this->real*pow(10,precision);
tnum = (int)(rnum < 0 ? rnum-0.5 : rnum + 0.5);
rnum = tnum/pow(10,precision);
inum = this->imag*pow(10,precision);
tnum = (int)(inum < 0 ? inum-0.5 : inum + 0.5);
inum = tnum/pow(10,precision);
return Complex(rnum,inum);
}
Complex rnd(Complex z, int precision)
{
double rnum,inum;
int tnum;
rnum = z.re()*pow(10,precision);
tnum = (int)(rnum < 0 ? rnum-0.5 : rnum + 0.5);
rnum = tnum/pow(10,precision);
inum = z.im()*pow(10,precision);
tnum = (int)(inum < 0 ? inum-0.5 : inum + 0.5);
inum = tnum/pow(10,precision);
return Complex(rnum,inum);
}
// end of Complex class
//=========================================================================
// round a number
double rnd(double num, int precision)
{
double rnum;
int tnum;
rnum = num*pow(10,precision);
tnum = (int)(rnum < 0 ? rnum-0.5 : rnum + 0.5);
rnum = tnum/pow(10,precision);
return rnum;
}