complex
7.4 KB
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
#pragma once
#include <cmath>
#define INFINITY (__builtin_inff())
namespace std {
// Taken from libc++
template <class _Tp>
class complex {
public:
typedef _Tp value_type;
private:
value_type __re_;
value_type __im_;
public:
complex(const value_type &__re = value_type(), const value_type &__im = value_type())
: __re_(__re), __im_(__im) {}
template <class _Xp>
complex(const complex<_Xp> &__c)
: __re_(__c.real()), __im_(__c.imag()) {}
value_type real() const { return __re_; }
value_type imag() const { return __im_; }
void real(value_type __re) { __re_ = __re; }
void imag(value_type __im) { __im_ = __im; }
complex &operator=(const value_type &__re) {
__re_ = __re;
__im_ = value_type();
return *this;
}
complex &operator+=(const value_type &__re) {
__re_ += __re;
return *this;
}
complex &operator-=(const value_type &__re) {
__re_ -= __re;
return *this;
}
complex &operator*=(const value_type &__re) {
__re_ *= __re;
__im_ *= __re;
return *this;
}
complex &operator/=(const value_type &__re) {
__re_ /= __re;
__im_ /= __re;
return *this;
}
template <class _Xp>
complex &operator=(const complex<_Xp> &__c) {
__re_ = __c.real();
__im_ = __c.imag();
return *this;
}
template <class _Xp>
complex &operator+=(const complex<_Xp> &__c) {
__re_ += __c.real();
__im_ += __c.imag();
return *this;
}
template <class _Xp>
complex &operator-=(const complex<_Xp> &__c) {
__re_ -= __c.real();
__im_ -= __c.imag();
return *this;
}
template <class _Xp>
complex &operator*=(const complex<_Xp> &__c) {
*this = *this * complex(__c.real(), __c.imag());
return *this;
}
template <class _Xp>
complex &operator/=(const complex<_Xp> &__c) {
*this = *this / complex(__c.real(), __c.imag());
return *this;
}
};
template <class _Tp>
inline complex<_Tp>
operator+(const complex<_Tp> &__x, const complex<_Tp> &__y) {
complex<_Tp> __t(__x);
__t += __y;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator+(const complex<_Tp> &__x, const _Tp &__y) {
complex<_Tp> __t(__x);
__t += __y;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator+(const _Tp &__x, const complex<_Tp> &__y) {
complex<_Tp> __t(__y);
__t += __x;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator-(const complex<_Tp> &__x, const complex<_Tp> &__y) {
complex<_Tp> __t(__x);
__t -= __y;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator-(const complex<_Tp> &__x, const _Tp &__y) {
complex<_Tp> __t(__x);
__t -= __y;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator-(const _Tp &__x, const complex<_Tp> &__y) {
complex<_Tp> __t(-__y);
__t += __x;
return __t;
}
template <class _Tp>
complex<_Tp>
operator*(const complex<_Tp> &__z, const complex<_Tp> &__w) {
_Tp __a = __z.real();
_Tp __b = __z.imag();
_Tp __c = __w.real();
_Tp __d = __w.imag();
_Tp __ac = __a * __c;
_Tp __bd = __b * __d;
_Tp __ad = __a * __d;
_Tp __bc = __b * __c;
_Tp __x = __ac - __bd;
_Tp __y = __ad + __bc;
if (std::isnan(__x) && std::isnan(__y)) {
bool __recalc = false;
if (std::isinf(__a) || std::isinf(__b)) {
__a = copysign(std::isinf(__a) ? _Tp(1) : _Tp(0), __a);
__b = copysign(std::isinf(__b) ? _Tp(1) : _Tp(0), __b);
if (std::isnan(__c))
__c = copysign(_Tp(0), __c);
if (std::isnan(__d))
__d = copysign(_Tp(0), __d);
__recalc = true;
}
if (std::isinf(__c) || std::isinf(__d)) {
__c = copysign(std::isinf(__c) ? _Tp(1) : _Tp(0), __c);
__d = copysign(std::isinf(__d) ? _Tp(1) : _Tp(0), __d);
if (std::isnan(__a))
__a = copysign(_Tp(0), __a);
if (std::isnan(__b))
__b = copysign(_Tp(0), __b);
__recalc = true;
}
if (!__recalc && (std::isinf(__ac) || std::isinf(__bd) ||
std::isinf(__ad) || std::isinf(__bc))) {
if (std::isnan(__a))
__a = copysign(_Tp(0), __a);
if (std::isnan(__b))
__b = copysign(_Tp(0), __b);
if (std::isnan(__c))
__c = copysign(_Tp(0), __c);
if (std::isnan(__d))
__d = copysign(_Tp(0), __d);
__recalc = true;
}
if (__recalc) {
__x = _Tp(INFINITY) * (__a * __c - __b * __d);
__y = _Tp(INFINITY) * (__a * __d + __b * __c);
}
}
return complex<_Tp>(__x, __y);
}
template <class _Tp>
inline complex<_Tp>
operator*(const complex<_Tp> &__x, const _Tp &__y) {
complex<_Tp> __t(__x);
__t *= __y;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator*(const _Tp &__x, const complex<_Tp> &__y) {
complex<_Tp> __t(__y);
__t *= __x;
return __t;
}
template <class _Tp>
complex<_Tp>
operator/(const complex<_Tp> &__z, const complex<_Tp> &__w) {
int __ilogbw = 0;
_Tp __a = __z.real();
_Tp __b = __z.imag();
_Tp __c = __w.real();
_Tp __d = __w.imag();
_Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
if (std::isfinite(__logbw)) {
__ilogbw = static_cast<int>(__logbw);
__c = scalbn(__c, -__ilogbw);
__d = scalbn(__d, -__ilogbw);
}
_Tp __denom = __c * __c + __d * __d;
_Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
_Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
if (std::isnan(__x) && std::isnan(__y)) {
if ((__denom == _Tp(0)) && (!std::isnan(__a) || !std::isnan(__b))) {
__x = copysign(_Tp(INFINITY), __c) * __a;
__y = copysign(_Tp(INFINITY), __c) * __b;
} else if ((std::isinf(__a) || std::isinf(__b)) && std::isfinite(__c) && std::isfinite(__d)) {
__a = copysign(std::isinf(__a) ? _Tp(1) : _Tp(0), __a);
__b = copysign(std::isinf(__b) ? _Tp(1) : _Tp(0), __b);
__x = _Tp(INFINITY) * (__a * __c + __b * __d);
__y = _Tp(INFINITY) * (__b * __c - __a * __d);
} else if (std::isinf(__logbw) && __logbw > _Tp(0) && std::isfinite(__a) && std::isfinite(__b)) {
__c = copysign(std::isinf(__c) ? _Tp(1) : _Tp(0), __c);
__d = copysign(std::isinf(__d) ? _Tp(1) : _Tp(0), __d);
__x = _Tp(0) * (__a * __c + __b * __d);
__y = _Tp(0) * (__b * __c - __a * __d);
}
}
return complex<_Tp>(__x, __y);
}
template <class _Tp>
inline complex<_Tp>
operator/(const complex<_Tp> &__x, const _Tp &__y) {
return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
}
template <class _Tp>
inline complex<_Tp>
operator/(const _Tp &__x, const complex<_Tp> &__y) {
complex<_Tp> __t(__x);
__t /= __y;
return __t;
}
template <class _Tp>
inline complex<_Tp>
operator+(const complex<_Tp> &__x) {
return __x;
}
template <class _Tp>
inline complex<_Tp>
operator-(const complex<_Tp> &__x) {
return complex<_Tp>(-__x.real(), -__x.imag());
}
template <class _Tp>
inline bool
operator==(const complex<_Tp> &__x, const complex<_Tp> &__y) {
return __x.real() == __y.real() && __x.imag() == __y.imag();
}
template <class _Tp>
inline bool
operator==(const complex<_Tp> &__x, const _Tp &__y) {
return __x.real() == __y && __x.imag() == 0;
}
template <class _Tp>
inline bool
operator==(const _Tp &__x, const complex<_Tp> &__y) {
return __x == __y.real() && 0 == __y.imag();
}
template <class _Tp>
inline bool
operator!=(const complex<_Tp> &__x, const complex<_Tp> &__y) {
return !(__x == __y);
}
template <class _Tp>
inline bool
operator!=(const complex<_Tp> &__x, const _Tp &__y) {
return !(__x == __y);
}
template <class _Tp>
inline bool
operator!=(const _Tp &__x, const complex<_Tp> &__y) {
return !(__x == __y);
}
} // namespace std