-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex.hpp
234 lines (167 loc) · 5.27 KB
/
complex.hpp
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
#ifndef COMPLEX_HPP
#define COMPLEX_HPP
#ifndef FERR
#define FERR 1e-10
#endif
namespace jmt
{
#ifndef M_PI
#define M_PI 3.141592653589793238L
#endif
struct complex
{
double re, im;
complex(double re, double im) : re(re), im(im) {}
// not allow implicit conversion
explicit complex(double re) : complex(re, 0.0) {}
complex() : complex(0.0, 0.0) {}
complex(const complex &c) : re(c.re), im(c.im) {}
complex& operator=(const complex &c)
{ re = c.re; im = c.im; return *this; }
complex& operator=(double d)
{ re = d; im = 0.0; return *this; }
inline complex operator+=(const complex &c)
{ re += c.re; im += c.im; return *this; }
inline complex operator+=(double d)
{ re += d; return *this; }
inline complex operator-=(const complex &c)
{ re -= c.re; im -= c.im; return *this; }
inline complex operator-=(double d)
{ re -= d; return *this; }
inline complex operator*=(const complex &c)
{
double rr = re * c.re - im * c.im, ri = re * c.im + im * c.re;
re = rr; im = ri;
return *this;
}
inline complex operator*=(double d)
{ re *= d; return *this; }
inline complex operator/=(double d)
{ re /= d; im /= d; return *this; }
inline complex conj()
{ return complex(re, -im); }
inline double sq_norm() const
{ return re * re + im * im; }
inline double norm() const
{ return std::sqrt(this->sq_norm()); }
};
const complex M_I(0.0, 1.0);
inline complex euler(double norm, double arg)
{ return complex(norm * std::cos(arg), norm * std::sin(arg)); }
inline complex exp(const complex &c)
{
return euler(std::exp(c.re), c.im);
}
inline double sq_norm(double d)
{
return d * d;
}
inline double sq_norm(const complex &c)
{
return c.re * c.re + c.im * c.im;
}
inline double norm(double d)
{ return d; }
inline double norm(const complex &c)
{ return sqrt(sq_norm(c)); }
inline complex normalize(const complex &c)
{ return complex(c.re / norm(c), c.im / norm(c)); }
inline complex conjugate(const complex &c)
{ return complex(c.re, -c.im); }
inline double conjugate(double c)
{ return c; }
inline complex operator+(const complex &c1, const complex &c2)
{ return complex(c1.re + c2.re, c1.im + c2.im); }
inline complex operator+(const complex &c1, double d)
{ return complex(c1.re + d); }
inline complex operator+(double d, const complex &c)
{ return c + d; }
inline complex operator-(const complex &c1, const complex &c2)
{ return complex(c1.re - c2.re, c1.im - c2.im); }
inline complex operator-(const complex &c1, double d)
{ return complex(c1.re - d); }
inline complex operator-(double d, const complex &c)
{ return complex(d - c.re, -c.im); }
inline complex operator*(const complex &c1, const complex &c2)
{ return complex(c1.re * c2.re - c1.im * c2.im, c1.re * c2.im + c1.im * c2.re);}
inline complex operator*(const complex &c, double d)
{ return complex(c.re * d, c.im * d); }
inline complex operator*(double d, const complex &c)
{ return c * d; }
inline complex operator/(const complex &c, const complex &d)
{ return c * complex(d.re / sq_norm(d), -d.im / sq_norm(d)); }
inline complex operator/(double c, const complex &d)
{ return c * complex(d.re / sq_norm(d), -d.im / sq_norm(d)); }
inline complex operator/(const complex &c, double d)
{ return complex(c.re / d, c.im / d); }
inline bool operator==(const complex &c1, const complex &c2)
{ return (c1.re == c2.re) && (c1.im == c2.im); }
inline bool operator<(const complex &c1, const complex &c2)
{ return sq_norm(c1) < sq_norm(c2); }
inline bool operator<(const complex &c, double d)
{ return sq_norm(c) < d; }
inline bool operator<(double d, const complex &c)
{ return c < d; }
inline bool operator<=(const complex &c1, const complex &c2)
{ return sq_norm(c1) <= sq_norm(c2); }
inline bool operator<=(const complex &c, double d)
{ return sq_norm(c) <= d; }
inline bool operator<=(double d, const complex &c)
{ return c <= d; }
inline bool operator>(const complex &c1, const complex &c2)
{ return sq_norm(c1) > sq_norm(c2); }
inline bool operator>(const complex &c, double d)
{ return sq_norm(c) > d; }
inline bool operator>(double d, complex &c)
{ return c > d; }
inline bool operator>=(const complex &c1, const complex &c2)
{ return sq_norm(c1) >= sq_norm(c2); }
inline bool operator>=(const complex &c, double d)
{ return sq_norm(c) >= d; }
inline bool operator>=(double d, const complex &c)
{ return c >= d; }
inline complex operator+(const complex &c)
{ return c; }
inline complex operator-(const complex &c)
{ return complex(-c.re, -c.im); }
inline double fabs(const complex &c)
{
return norm(c);
}
inline double fabs(double c)
{
return std::fabs(c);
}
// to be done
inline double sqrt(const complex &c)
{
return norm(c);
}
inline double sqrt(double c)
{
return std::sqrt(c);
}
inline double copysign(double c, double d)
{ return std::copysign(c, d);}
inline bool feq(const complex &l, const complex &r)
{ return (fabs(l - r) < FERR); }
inline bool feq(double l, double r)
{ return (fabs(l - r) < FERR); }
inline double real(const complex &c)
{ return c.re; }
inline double real(complex &c)
{ return c.re; }
inline double real(const double &d)
{ return d; }
inline double real(double &d)
{ return d; }
inline double im(const double d)
{ return 0.0; }
inline double im(const complex &c)
{ return c.im; }
inline double im(complex &c)
{ return c.im; }
inline complex c(double re, double im)
{ return complex(re, im); }
} // namespace
#endif