complex_cmath.h
12.1 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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//===------------------------- __complex_cmath.h --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// std::complex header copied from the libcxx source and simplified for use in
// OpenMP target offload regions.
//
//===----------------------------------------------------------------------===//
#ifndef _OPENMP
#error "This file is for OpenMP compilation only."
#endif
#ifndef __cplusplus
#error "This file is for C++ compilation only."
#endif
#ifndef _LIBCPP_COMPLEX
#define _LIBCPP_COMPLEX
#include <cmath>
#include <type_traits>
#define __DEVICE__ static constexpr __attribute__((nothrow))
namespace std {
// abs
template <class _Tp> __DEVICE__ _Tp abs(const std::complex<_Tp> &__c) {
return hypot(__c.real(), __c.imag());
}
// arg
template <class _Tp> __DEVICE__ _Tp arg(const std::complex<_Tp> &__c) {
return atan2(__c.imag(), __c.real());
}
template <class _Tp>
typename enable_if<is_integral<_Tp>::value || is_same<_Tp, double>::value,
double>::type
arg(_Tp __re) {
return atan2(0., __re);
}
template <class _Tp>
typename enable_if<is_same<_Tp, float>::value, float>::type arg(_Tp __re) {
return atan2f(0.F, __re);
}
// norm
template <class _Tp> __DEVICE__ _Tp norm(const std::complex<_Tp> &__c) {
if (std::isinf(__c.real()))
return abs(__c.real());
if (std::isinf(__c.imag()))
return abs(__c.imag());
return __c.real() * __c.real() + __c.imag() * __c.imag();
}
// conj
template <class _Tp> std::complex<_Tp> conj(const std::complex<_Tp> &__c) {
return std::complex<_Tp>(__c.real(), -__c.imag());
}
// proj
template <class _Tp> std::complex<_Tp> proj(const std::complex<_Tp> &__c) {
std::complex<_Tp> __r = __c;
if (std::isinf(__c.real()) || std::isinf(__c.imag()))
__r = std::complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
return __r;
}
// polar
template <class _Tp>
complex<_Tp> polar(const _Tp &__rho, const _Tp &__theta = _Tp()) {
if (std::isnan(__rho) || signbit(__rho))
return std::complex<_Tp>(_Tp(NAN), _Tp(NAN));
if (std::isnan(__theta)) {
if (std::isinf(__rho))
return std::complex<_Tp>(__rho, __theta);
return std::complex<_Tp>(__theta, __theta);
}
if (std::isinf(__theta)) {
if (std::isinf(__rho))
return std::complex<_Tp>(__rho, _Tp(NAN));
return std::complex<_Tp>(_Tp(NAN), _Tp(NAN));
}
_Tp __x = __rho * cos(__theta);
if (std::isnan(__x))
__x = 0;
_Tp __y = __rho * sin(__theta);
if (std::isnan(__y))
__y = 0;
return std::complex<_Tp>(__x, __y);
}
// log
template <class _Tp> std::complex<_Tp> log(const std::complex<_Tp> &__x) {
return std::complex<_Tp>(log(abs(__x)), arg(__x));
}
// log10
template <class _Tp> std::complex<_Tp> log10(const std::complex<_Tp> &__x) {
return log(__x) / log(_Tp(10));
}
// sqrt
template <class _Tp>
__DEVICE__ std::complex<_Tp> sqrt(const std::complex<_Tp> &__x) {
if (std::isinf(__x.imag()))
return std::complex<_Tp>(_Tp(INFINITY), __x.imag());
if (std::isinf(__x.real())) {
if (__x.real() > _Tp(0))
return std::complex<_Tp>(__x.real(), std::isnan(__x.imag())
? __x.imag()
: copysign(_Tp(0), __x.imag()));
return std::complex<_Tp>(std::isnan(__x.imag()) ? __x.imag() : _Tp(0),
copysign(__x.real(), __x.imag()));
}
return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
}
// exp
template <class _Tp>
__DEVICE__ std::complex<_Tp> exp(const std::complex<_Tp> &__x) {
_Tp __i = __x.imag();
if (std::isinf(__x.real())) {
if (__x.real() < _Tp(0)) {
if (!std::isfinite(__i))
__i = _Tp(1);
} else if (__i == 0 || !std::isfinite(__i)) {
if (std::isinf(__i))
__i = _Tp(NAN);
return std::complex<_Tp>(__x.real(), __i);
}
} else if (std::isnan(__x.real()) && __x.imag() == 0)
return __x;
_Tp __e = exp(__x.real());
return std::complex<_Tp>(__e * cos(__i), __e * sin(__i));
}
// pow
template <class _Tp>
std::complex<_Tp> pow(const std::complex<_Tp> &__x,
const std::complex<_Tp> &__y) {
return exp(__y * log(__x));
}
// __sqr, computes pow(x, 2)
template <class _Tp> std::complex<_Tp> __sqr(const std::complex<_Tp> &__x) {
return std::complex<_Tp>((__x.real() - __x.imag()) *
(__x.real() + __x.imag()),
_Tp(2) * __x.real() * __x.imag());
}
// asinh
template <class _Tp>
__DEVICE__ std::complex<_Tp> asinh(const std::complex<_Tp> &__x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.real())) {
if (std::isnan(__x.imag()))
return __x;
if (std::isinf(__x.imag()))
return std::complex<_Tp>(__x.real(),
copysign(__pi * _Tp(0.25), __x.imag()));
return std::complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
}
if (std::isnan(__x.real())) {
if (std::isinf(__x.imag()))
return std::complex<_Tp>(__x.imag(), __x.real());
if (__x.imag() == 0)
return __x;
return std::complex<_Tp>(__x.real(), __x.real());
}
if (std::isinf(__x.imag()))
return std::complex<_Tp>(copysign(__x.imag(), __x.real()),
copysign(__pi / _Tp(2), __x.imag()));
std::complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
return std::complex<_Tp>(copysign(__z.real(), __x.real()),
copysign(__z.imag(), __x.imag()));
}
// acosh
template <class _Tp>
__DEVICE__ std::complex<_Tp> acosh(const std::complex<_Tp> &__x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.real())) {
if (std::isnan(__x.imag()))
return std::complex<_Tp>(abs(__x.real()), __x.imag());
if (std::isinf(__x.imag())) {
if (__x.real() > 0)
return std::complex<_Tp>(__x.real(),
copysign(__pi * _Tp(0.25), __x.imag()));
else
return std::complex<_Tp>(-__x.real(),
copysign(__pi * _Tp(0.75), __x.imag()));
}
if (__x.real() < 0)
return std::complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
return std::complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
}
if (std::isnan(__x.real())) {
if (std::isinf(__x.imag()))
return std::complex<_Tp>(abs(__x.imag()), __x.real());
return std::complex<_Tp>(__x.real(), __x.real());
}
if (std::isinf(__x.imag()))
return std::complex<_Tp>(abs(__x.imag()),
copysign(__pi / _Tp(2), __x.imag()));
std::complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
return std::complex<_Tp>(copysign(__z.real(), _Tp(0)),
copysign(__z.imag(), __x.imag()));
}
// atanh
template <class _Tp>
__DEVICE__ std::complex<_Tp> atanh(const std::complex<_Tp> &__x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.imag())) {
return std::complex<_Tp>(copysign(_Tp(0), __x.real()),
copysign(__pi / _Tp(2), __x.imag()));
}
if (std::isnan(__x.imag())) {
if (std::isinf(__x.real()) || __x.real() == 0)
return std::complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
return std::complex<_Tp>(__x.imag(), __x.imag());
}
if (std::isnan(__x.real())) {
return std::complex<_Tp>(__x.real(), __x.real());
}
if (std::isinf(__x.real())) {
return std::complex<_Tp>(copysign(_Tp(0), __x.real()),
copysign(__pi / _Tp(2), __x.imag()));
}
if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) {
return std::complex<_Tp>(copysign(_Tp(INFINITY), __x.real()),
copysign(_Tp(0), __x.imag()));
}
std::complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
return std::complex<_Tp>(copysign(__z.real(), __x.real()),
copysign(__z.imag(), __x.imag()));
}
// sinh
template <class _Tp>
__DEVICE__ std::complex<_Tp> sinh(const std::complex<_Tp> &__x) {
if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
return std::complex<_Tp>(__x.real(), _Tp(NAN));
if (__x.real() == 0 && !std::isfinite(__x.imag()))
return std::complex<_Tp>(__x.real(), _Tp(NAN));
if (__x.imag() == 0 && !std::isfinite(__x.real()))
return __x;
return std::complex<_Tp>(sinh(__x.real()) * cos(__x.imag()),
cosh(__x.real()) * sin(__x.imag()));
}
// cosh
template <class _Tp>
__DEVICE__ std::complex<_Tp> cosh(const std::complex<_Tp> &__x) {
if (std::isinf(__x.real()) && !std::isfinite(__x.imag()))
return std::complex<_Tp>(abs(__x.real()), _Tp(NAN));
if (__x.real() == 0 && !std::isfinite(__x.imag()))
return std::complex<_Tp>(_Tp(NAN), __x.real());
if (__x.real() == 0 && __x.imag() == 0)
return std::complex<_Tp>(_Tp(1), __x.imag());
if (__x.imag() == 0 && !std::isfinite(__x.real()))
return std::complex<_Tp>(abs(__x.real()), __x.imag());
return std::complex<_Tp>(cosh(__x.real()) * cos(__x.imag()),
sinh(__x.real()) * sin(__x.imag()));
}
// tanh
template <class _Tp>
__DEVICE__ std::complex<_Tp> tanh(const std::complex<_Tp> &__x) {
if (std::isinf(__x.real())) {
if (!std::isfinite(__x.imag()))
return std::complex<_Tp>(_Tp(1), _Tp(0));
return std::complex<_Tp>(_Tp(1),
copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
}
if (std::isnan(__x.real()) && __x.imag() == 0)
return __x;
_Tp __2r(_Tp(2) * __x.real());
_Tp __2i(_Tp(2) * __x.imag());
_Tp __d(cosh(__2r) + cos(__2i));
_Tp __2rsh(sinh(__2r));
if (std::isinf(__2rsh) && std::isinf(__d))
return std::complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
__2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
return std::complex<_Tp>(__2rsh / __d, sin(__2i) / __d);
}
// asin
template <class _Tp>
__DEVICE__ std::complex<_Tp> asin(const std::complex<_Tp> &__x) {
std::complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
return std::complex<_Tp>(__z.imag(), -__z.real());
}
// acos
template <class _Tp>
__DEVICE__ std::complex<_Tp> acos(const std::complex<_Tp> &__x) {
const _Tp __pi(atan2(+0., -0.));
if (std::isinf(__x.real())) {
if (std::isnan(__x.imag()))
return std::complex<_Tp>(__x.imag(), __x.real());
if (std::isinf(__x.imag())) {
if (__x.real() < _Tp(0))
return std::complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
return std::complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
}
if (__x.real() < _Tp(0))
return std::complex<_Tp>(__pi,
signbit(__x.imag()) ? -__x.real() : __x.real());
return std::complex<_Tp>(_Tp(0),
signbit(__x.imag()) ? __x.real() : -__x.real());
}
if (std::isnan(__x.real())) {
if (std::isinf(__x.imag()))
return std::complex<_Tp>(__x.real(), -__x.imag());
return std::complex<_Tp>(__x.real(), __x.real());
}
if (std::isinf(__x.imag()))
return std::complex<_Tp>(__pi / _Tp(2), -__x.imag());
if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
return std::complex<_Tp>(__pi / _Tp(2), -__x.imag());
std::complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
if (signbit(__x.imag()))
return std::complex<_Tp>(abs(__z.imag()), abs(__z.real()));
return std::complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
}
// atan
template <class _Tp>
__DEVICE__ std::complex<_Tp> atan(const std::complex<_Tp> &__x) {
std::complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
return std::complex<_Tp>(__z.imag(), -__z.real());
}
// sin
template <class _Tp>
__DEVICE__ std::complex<_Tp> sin(const std::complex<_Tp> &__x) {
std::complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
return std::complex<_Tp>(__z.imag(), -__z.real());
}
// cos
template <class _Tp> std::complex<_Tp> cos(const std::complex<_Tp> &__x) {
return cosh(complex<_Tp>(-__x.imag(), __x.real()));
}
// tan
template <class _Tp>
__DEVICE__ std::complex<_Tp> tan(const std::complex<_Tp> &__x) {
std::complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
return std::complex<_Tp>(__z.imag(), -__z.real());
}
} // namespace std
#endif