forked from dealias/fftwpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Complex.h
321 lines (263 loc) · 6.69 KB
/
Complex.h
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
/*
Copyright (C) 1988 Free Software Foundation
written by Doug Lea ([email protected])
This file is part of the GNU C++ Library. This library is free
software; you can redistribute it and/or modify it under the terms of
the GNU Library General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your
option) any later version. This library is distributed in the hope
that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#ifndef __Complex_h__
#define __Complex_h__ 1
#define __ATT_complex__
#include <iostream>
#include <cmath>
using std::istream;
using std::ostream;
using std::ws;
class Complex
{
#ifdef __ATT_complex__
public:
#else
protected:
#endif
double re;
double im;
public:
Complex() {}
Complex(double r, double i=0) : re(r), im(i) {}
Complex(const Complex& y) : re(y.re), im(y.im) {}
~Complex() {}
double real() const {return re;}
double imag() const {return im;}
const Complex& operator = (const Complex& y);
const Complex& operator += (const Complex& y);
const Complex& operator += (double y);
const Complex& operator -= (const Complex& y);
const Complex& operator -= (double y);
const Complex& operator *= (const Complex& y);
const Complex& operator *= (double y);
const Complex& operator /= (const Complex& y);
const Complex& operator /= (double y);
void error(char* msg) const;
};
// inline members
inline const Complex& Complex::operator = (const Complex& y)
{
re = y.re; im = y.im; return *this;
}
inline const Complex& Complex::operator += (const Complex& y)
{
re += y.re; im += y.im; return *this;
}
inline const Complex& Complex::operator += (double y)
{
re += y; return *this;
}
inline const Complex& Complex::operator -= (const Complex& y)
{
re -= y.re; im -= y.im; return *this;
}
inline const Complex& Complex::operator -= (double y)
{
re -= y; return *this;
}
inline const Complex& Complex::operator *= (const Complex& y)
{
double r = re * y.re - im * y.im;
im = re * y.im + im * y.re;
re = r;
return *this;
}
inline const Complex& Complex::operator *= (double y)
{
re *= y; im *= y; return *this;
}
inline const Complex& Complex::operator /= (const Complex& y)
{
register double t1,t2,t3;
t2=1.0/(y.re*y.re+y.im*y.im);
t1=t2*y.re; t2 *= y.im; t3=re;
re *= t1; re += im*t2;
im *= t1; im -= t3*t2;
return *this;
}
inline const Complex& Complex::operator /= (double y)
{
re /= y;
im /= y;
return *this;
}
// functions
inline int operator == (const Complex& x, const Complex& y)
{
return x.re == y.re && x.im == y.im;
}
inline int operator == (const Complex& x, double y)
{
return x.im == 0.0 && x.re == y;
}
inline int operator != (const Complex& x, const Complex& y)
{
return x.re != y.re || x.im != y.im;
}
inline int operator != (const Complex& x, double y)
{
return x.im != 0.0 || x.re != y;
}
inline Complex operator - (const Complex& x)
{
return Complex(-x.re, -x.im);
}
inline Complex conj(const Complex& x)
{
return Complex(x.re, -x.im);
}
inline Complex operator + (const Complex& x, const Complex& y)
{
return Complex(x.re+y.re, x.im+y.im);
}
inline Complex operator + (const Complex& x, double y)
{
return Complex(x.re+y, x.im);
}
inline Complex operator + (double x, const Complex& y)
{
return Complex(x+y.re, y.im);
}
inline Complex operator - (const Complex& x, const Complex& y)
{
return Complex(x.re-y.re, x.im-y.im);
}
inline Complex operator - (const Complex& x, double y)
{
return Complex(x.re-y, x.im);
}
inline Complex operator - (double x, const Complex& y)
{
return Complex(x-y.re, -y.im);
}
inline Complex operator * (const Complex& x, const Complex& y)
{
return Complex(x.re*y.re-x.im*y.im, x.re*y.im+x.im*y.re);
}
inline Complex multconj(const Complex& x, const Complex& y)
{
return Complex(x.re*y.re+x.im*y.im,x.im*y.re-x.re*y.im);
}
inline Complex operator * (const Complex& x, double y)
{
return Complex(x.re*y, x.im*y);
}
inline Complex operator * (double x, const Complex& y)
{
return Complex(x*y.re, x*y.im);
}
inline Complex operator / (const Complex& x, const Complex& y)
{
register double t1,t2;
t2=1.0/(y.re*y.re+y.im*y.im);
t1=t2*y.re; t2 *= y.im;
return Complex(x.im*t2+x.re*t1, x.im*t1-x.re*t2);
}
inline Complex operator / (const Complex& x, double y)
{
return Complex(x.re/y,x.im/y);
}
inline Complex operator / (double x, const Complex& y)
{
register double factor;
factor=1.0/(y.re*y.re+y.im*y.im);
return Complex(x*y.re*factor,-x*y.im*factor);
}
inline double real(const Complex& x)
{
return x.re;
}
inline double imag(const Complex& x)
{
return x.im;
}
inline double abs2(const Complex& x)
{
return x.re*x.re+x.im*x.im;
}
inline double abs(const Complex& x)
{
return sqrt(abs2(x));
}
inline double arg(const Complex& x)
{
return x.im != 0.0 ? atan2(x.im, x.re) : 0.0;
}
// Return the principal branch of the square root (non-negative real part).
inline Complex sqrt(const Complex& x)
{
double mag=abs(x);
if(mag == 0.0) return Complex(0.0,0.0);
else if(x.re > 0) {
double re=sqrt(0.5*(mag+x.re));
return Complex(re,0.5*x.im/re);
} else {
double im=sqrt(0.5*(mag-x.re));
if(x.im < 0) im=-im;
return Complex(0.5*x.im/im,im);
}
}
inline Complex polar(double r, double t)
{
return Complex(r*cos(t), r*sin(t));
}
// Complex exponentiation
inline Complex pow(const Complex& z, const Complex& w)
{
double u=w.re;
double v=w.im;
if(z == 0.0) return w == 0.0 ? 1.0 : 0.0;
double logr=0.5*log(abs2(z));
double th=arg(z);
double phi=logr*v+th*u;
return exp(logr*u-th*v)*Complex(cos(phi),sin(phi));
}
inline Complex pow(const Complex& z, double u)
{
if(z == 0.0) return u == 0.0 ? 1.0 : 0.0;
double logr=0.5*log(abs2(z));
double theta=u*arg(z);
return exp(logr*u)*Complex(cos(theta),sin(theta));
}
inline istream& operator >> (istream& s, Complex& y)
{
char c;
s >> ws >> c;
if(c == '(') {
s >> y.re >> c;
if(c == ',') s >> y.im >> c;
else y.im=0.0;
} else {
s.putback(c);
s >> y.re; y.im=0.0;
}
return s;
}
inline ostream& operator << (ostream& s, const Complex& y)
{
s << "(" << y.re << "," << y.im << ")";
return s;
}
inline bool isfinite(const Complex& z)
{
#ifdef _WIN32
return _finite(z.re) && _finite(z.im);
#else
return !(std::isinf(z.re) || std::isnan(z.re) || std::isinf(z.im) || std::isnan(z.im));
#endif
}
#endif