]>
Commit | Line | Data |
---|---|---|
e1e0016d | 1 | // The template and inlines for the -*- C++ -*- complex number classes. |
01dd2c6c | 2 | |
f1717362 | 3 | // Copyright (C) 1997-2016 Free Software Foundation, Inc. |
01dd2c6c | 4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
01dd2c6c | 9 | // any later version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
6bc9506f | 16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
01dd2c6c | 19 | |
6bc9506f | 20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
01dd2c6c | 24 | |
c785a1c4 | 25 | /** @file include/complex |
944beac5 | 26 | * This is a Standard C++ Library header. |
27 | */ | |
28 | ||
e1e0016d | 29 | // |
30 | // ISO C++ 14882: 26.2 Complex Numbers | |
31 | // Note: this is not a conforming implementation. | |
32 | // Initially implemented by Ulrich Drepper <drepper@cygnus.com> | |
33 | // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> | |
34 | // | |
35 | ||
a438d9ac | 36 | #ifndef _GLIBCXX_COMPLEX |
37 | #define _GLIBCXX_COMPLEX 1 | |
e1e0016d | 38 | |
39 | #pragma GCC system_header | |
40 | ||
41 | #include <bits/c++config.h> | |
42 | #include <bits/cpp_type_traits.h> | |
c17b0a1c | 43 | #include <ext/type_traits.h> |
e1e0016d | 44 | #include <cmath> |
45 | #include <sstream> | |
46 | ||
79bd723e | 47 | // Get rid of a macro possibly defined in <complex.h> |
48 | #undef complex | |
49 | ||
2948dd21 | 50 | namespace std _GLIBCXX_VISIBILITY(default) |
51 | { | |
52 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
1069247d | 53 | |
97a32de0 | 54 | /** |
55 | * @defgroup complex_numbers Complex Numbers | |
56 | * @ingroup numerics | |
57 | * | |
58 | * Classes and functions for complex numbers. | |
59 | * @{ | |
60 | */ | |
61 | ||
9253fc43 | 62 | // Forward declarations. |
e1e0016d | 63 | template<typename _Tp> class complex; |
64 | template<> class complex<float>; | |
65 | template<> class complex<double>; | |
66 | template<> class complex<long double>; | |
67 | ||
ed73ad37 | 68 | /// Return magnitude of @a z. |
e1e0016d | 69 | template<typename _Tp> _Tp abs(const complex<_Tp>&); |
ed73ad37 | 70 | /// Return phase angle of @a z. |
e1e0016d | 71 | template<typename _Tp> _Tp arg(const complex<_Tp>&); |
ed73ad37 | 72 | /// Return @a z magnitude squared. |
e1e0016d | 73 | template<typename _Tp> _Tp norm(const complex<_Tp>&); |
74 | ||
ed73ad37 | 75 | /// Return complex conjugate of @a z. |
e1e0016d | 76 | template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); |
ed73ad37 | 77 | /// Return complex with magnitude @a rho and angle @a theta. |
e1e0016d | 78 | template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); |
79 | ||
80 | // Transcendentals: | |
ed73ad37 | 81 | /// Return complex cosine of @a z. |
e1e0016d | 82 | template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); |
ed73ad37 | 83 | /// Return complex hyperbolic cosine of @a z. |
e1e0016d | 84 | template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); |
ed73ad37 | 85 | /// Return complex base e exponential of @a z. |
e1e0016d | 86 | template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); |
ed73ad37 | 87 | /// Return complex natural logarithm of @a z. |
e1e0016d | 88 | template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); |
ed73ad37 | 89 | /// Return complex base 10 logarithm of @a z. |
e1e0016d | 90 | template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); |
2ec89d4f | 91 | /// Return @a x to the @a y'th power. |
e1e0016d | 92 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); |
ed73ad37 | 93 | /// Return @a x to the @a y'th power. |
e1e0016d | 94 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); |
ed73ad37 | 95 | /// Return @a x to the @a y'th power. |
e1e0016d | 96 | template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, |
ad25b344 | 97 | const complex<_Tp>&); |
ed73ad37 | 98 | /// Return @a x to the @a y'th power. |
e1e0016d | 99 | template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); |
ed73ad37 | 100 | /// Return complex sine of @a z. |
e1e0016d | 101 | template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); |
ed73ad37 | 102 | /// Return complex hyperbolic sine of @a z. |
e1e0016d | 103 | template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); |
ed73ad37 | 104 | /// Return complex square root of @a z. |
e1e0016d | 105 | template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); |
ed73ad37 | 106 | /// Return complex tangent of @a z. |
e1e0016d | 107 | template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); |
ed73ad37 | 108 | /// Return complex hyperbolic tangent of @a z. |
e1e0016d | 109 | template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); |
110 | ||
111 | ||
112 | // 26.2.2 Primary template class complex | |
ed73ad37 | 113 | /** |
114 | * Template to represent complex numbers. | |
115 | * | |
116 | * Specializations for float, double, and long double are part of the | |
117 | * library. Results with any other type are not guaranteed. | |
118 | * | |
119 | * @param Tp Type of real and imaginary values. | |
120 | */ | |
e1e0016d | 121 | template<typename _Tp> |
ad25b344 | 122 | struct complex |
e1e0016d | 123 | { |
ed73ad37 | 124 | /// Value typedef. |
e1e0016d | 125 | typedef _Tp value_type; |
126 | ||
ed73ad37 | 127 | /// Default constructor. First parameter is x, second parameter is y. |
128 | /// Unspecified parameters default to 0. | |
c0000147 | 129 | _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) |
253c3a91 | 130 | : _M_real(__r), _M_imag(__i) { } |
e1e0016d | 131 | |
c44802ee | 132 | // Let the compiler synthesize the copy constructor |
133 | #if __cplusplus >= 201103L | |
134 | constexpr complex(const complex&) = default; | |
135 | #endif | |
136 | ||
137 | /// Converting constructor. | |
e1e0016d | 138 | template<typename _Up> |
c0000147 | 139 | _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) |
253c3a91 | 140 | : _M_real(__z.real()), _M_imag(__z.imag()) { } |
dcd7c3cb | 141 | |
0c8766b1 | 142 | #if __cplusplus >= 201103L |
8c449bbb | 143 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
144 | // DR 387. std::complex over-encapsulated. | |
8544d95d | 145 | _GLIBCXX_ABI_TAG_CXX11 |
c0000147 | 146 | constexpr _Tp |
931e6774 | 147 | real() const { return _M_real; } |
8c449bbb | 148 | |
8544d95d | 149 | _GLIBCXX_ABI_TAG_CXX11 |
c0000147 | 150 | constexpr _Tp |
931e6774 | 151 | imag() const { return _M_imag; } |
8c449bbb | 152 | #else |
ed73ad37 | 153 | /// Return real part of complex number. |
c0000147 | 154 | _Tp& |
155 | real() { return _M_real; } | |
253c3a91 | 156 | |
ed73ad37 | 157 | /// Return real part of complex number. |
c0000147 | 158 | const _Tp& |
159 | real() const { return _M_real; } | |
253c3a91 | 160 | |
ed73ad37 | 161 | /// Return imaginary part of complex number. |
c0000147 | 162 | _Tp& |
163 | imag() { return _M_imag; } | |
253c3a91 | 164 | |
ed73ad37 | 165 | /// Return imaginary part of complex number. |
c0000147 | 166 | const _Tp& |
167 | imag() const { return _M_imag; } | |
8c449bbb | 168 | #endif |
169 | ||
170 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
171 | // DR 387. std::complex over-encapsulated. | |
c0000147 | 172 | void |
173 | real(_Tp __val) { _M_real = __val; } | |
8c449bbb | 174 | |
c0000147 | 175 | void |
176 | imag(_Tp __val) { _M_imag = __val; } | |
e1e0016d | 177 | |
c44802ee | 178 | /// Assign a scalar to this complex number. |
e1e0016d | 179 | complex<_Tp>& operator=(const _Tp&); |
253c3a91 | 180 | |
c44802ee | 181 | /// Add a scalar to this complex number. |
253c3a91 | 182 | // 26.2.5/1 |
183 | complex<_Tp>& | |
184 | operator+=(const _Tp& __t) | |
185 | { | |
186 | _M_real += __t; | |
187 | return *this; | |
188 | } | |
189 | ||
c44802ee | 190 | /// Subtract a scalar from this complex number. |
253c3a91 | 191 | // 26.2.5/3 |
192 | complex<_Tp>& | |
193 | operator-=(const _Tp& __t) | |
194 | { | |
195 | _M_real -= __t; | |
196 | return *this; | |
197 | } | |
198 | ||
c44802ee | 199 | /// Multiply this complex number by a scalar. |
e1e0016d | 200 | complex<_Tp>& operator*=(const _Tp&); |
c44802ee | 201 | /// Divide this complex number by a scalar. |
e1e0016d | 202 | complex<_Tp>& operator/=(const _Tp&); |
203 | ||
c44802ee | 204 | // Let the compiler synthesize the copy assignment operator |
205 | #if __cplusplus >= 201103L | |
206 | complex& operator=(const complex&) = default; | |
207 | #endif | |
208 | ||
209 | /// Assign another complex number to this one. | |
e1e0016d | 210 | template<typename _Up> |
211 | complex<_Tp>& operator=(const complex<_Up>&); | |
c44802ee | 212 | /// Add another complex number to this one. |
e1e0016d | 213 | template<typename _Up> |
214 | complex<_Tp>& operator+=(const complex<_Up>&); | |
c44802ee | 215 | /// Subtract another complex number from this one. |
e1e0016d | 216 | template<typename _Up> |
217 | complex<_Tp>& operator-=(const complex<_Up>&); | |
c44802ee | 218 | /// Multiply this complex number by another. |
e1e0016d | 219 | template<typename _Up> |
220 | complex<_Tp>& operator*=(const complex<_Up>&); | |
c44802ee | 221 | /// Divide this complex number by another. |
e1e0016d | 222 | template<typename _Up> |
223 | complex<_Tp>& operator/=(const complex<_Up>&); | |
224 | ||
6b688037 | 225 | _GLIBCXX_CONSTEXPR complex __rep() const |
253c3a91 | 226 | { return *this; } |
ad25b344 | 227 | |
e1e0016d | 228 | private: |
dcd7c3cb | 229 | _Tp _M_real; |
230 | _Tp _M_imag; | |
e1e0016d | 231 | }; |
232 | ||
e1e0016d | 233 | template<typename _Tp> |
234 | complex<_Tp>& | |
235 | complex<_Tp>::operator=(const _Tp& __t) | |
236 | { | |
237 | _M_real = __t; | |
238 | _M_imag = _Tp(); | |
239 | return *this; | |
240 | } | |
241 | ||
e1e0016d | 242 | // 26.2.5/5 |
243 | template<typename _Tp> | |
244 | complex<_Tp>& | |
245 | complex<_Tp>::operator*=(const _Tp& __t) | |
246 | { | |
247 | _M_real *= __t; | |
248 | _M_imag *= __t; | |
249 | return *this; | |
250 | } | |
251 | ||
252 | // 26.2.5/7 | |
253 | template<typename _Tp> | |
254 | complex<_Tp>& | |
255 | complex<_Tp>::operator/=(const _Tp& __t) | |
256 | { | |
257 | _M_real /= __t; | |
258 | _M_imag /= __t; | |
259 | return *this; | |
260 | } | |
261 | ||
262 | template<typename _Tp> | |
263 | template<typename _Up> | |
264 | complex<_Tp>& | |
265 | complex<_Tp>::operator=(const complex<_Up>& __z) | |
266 | { | |
267 | _M_real = __z.real(); | |
268 | _M_imag = __z.imag(); | |
269 | return *this; | |
270 | } | |
271 | ||
272 | // 26.2.5/9 | |
273 | template<typename _Tp> | |
274 | template<typename _Up> | |
275 | complex<_Tp>& | |
276 | complex<_Tp>::operator+=(const complex<_Up>& __z) | |
277 | { | |
278 | _M_real += __z.real(); | |
279 | _M_imag += __z.imag(); | |
280 | return *this; | |
281 | } | |
282 | ||
283 | // 26.2.5/11 | |
284 | template<typename _Tp> | |
285 | template<typename _Up> | |
286 | complex<_Tp>& | |
287 | complex<_Tp>::operator-=(const complex<_Up>& __z) | |
288 | { | |
289 | _M_real -= __z.real(); | |
290 | _M_imag -= __z.imag(); | |
291 | return *this; | |
292 | } | |
293 | ||
294 | // 26.2.5/13 | |
295 | // XXX: This is a grammar school implementation. | |
296 | template<typename _Tp> | |
297 | template<typename _Up> | |
298 | complex<_Tp>& | |
299 | complex<_Tp>::operator*=(const complex<_Up>& __z) | |
300 | { | |
301 | const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); | |
302 | _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); | |
303 | _M_real = __r; | |
304 | return *this; | |
305 | } | |
306 | ||
307 | // 26.2.5/15 | |
308 | // XXX: This is a grammar school implementation. | |
309 | template<typename _Tp> | |
310 | template<typename _Up> | |
311 | complex<_Tp>& | |
312 | complex<_Tp>::operator/=(const complex<_Up>& __z) | |
313 | { | |
314 | const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); | |
9927a571 | 315 | const _Tp __n = std::norm(__z); |
e1e0016d | 316 | _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; |
317 | _M_real = __r / __n; | |
318 | return *this; | |
319 | } | |
320 | ||
321 | // Operators: | |
ed73ad37 | 322 | //@{ |
323 | /// Return new complex value @a x plus @a y. | |
e1e0016d | 324 | template<typename _Tp> |
325 | inline complex<_Tp> | |
326 | operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 327 | { |
328 | complex<_Tp> __r = __x; | |
329 | __r += __y; | |
330 | return __r; | |
331 | } | |
e1e0016d | 332 | |
333 | template<typename _Tp> | |
334 | inline complex<_Tp> | |
335 | operator+(const complex<_Tp>& __x, const _Tp& __y) | |
dcd7c3cb | 336 | { |
337 | complex<_Tp> __r = __x; | |
3f012259 | 338 | __r += __y; |
dcd7c3cb | 339 | return __r; |
340 | } | |
e1e0016d | 341 | |
342 | template<typename _Tp> | |
343 | inline complex<_Tp> | |
344 | operator+(const _Tp& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 345 | { |
346 | complex<_Tp> __r = __y; | |
3f012259 | 347 | __r += __x; |
dcd7c3cb | 348 | return __r; |
349 | } | |
ed73ad37 | 350 | //@} |
e1e0016d | 351 | |
ed73ad37 | 352 | //@{ |
353 | /// Return new complex value @a x minus @a y. | |
e1e0016d | 354 | template<typename _Tp> |
355 | inline complex<_Tp> | |
356 | operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 357 | { |
358 | complex<_Tp> __r = __x; | |
359 | __r -= __y; | |
360 | return __r; | |
361 | } | |
e1e0016d | 362 | |
363 | template<typename _Tp> | |
364 | inline complex<_Tp> | |
365 | operator-(const complex<_Tp>& __x, const _Tp& __y) | |
dcd7c3cb | 366 | { |
367 | complex<_Tp> __r = __x; | |
3f012259 | 368 | __r -= __y; |
dcd7c3cb | 369 | return __r; |
370 | } | |
e1e0016d | 371 | |
372 | template<typename _Tp> | |
373 | inline complex<_Tp> | |
374 | operator-(const _Tp& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 375 | { |
376 | complex<_Tp> __r(__x, -__y.imag()); | |
3f012259 | 377 | __r -= __y.real(); |
dcd7c3cb | 378 | return __r; |
379 | } | |
ed73ad37 | 380 | //@} |
e1e0016d | 381 | |
ed73ad37 | 382 | //@{ |
383 | /// Return new complex value @a x times @a y. | |
e1e0016d | 384 | template<typename _Tp> |
385 | inline complex<_Tp> | |
386 | operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 387 | { |
388 | complex<_Tp> __r = __x; | |
389 | __r *= __y; | |
390 | return __r; | |
391 | } | |
e1e0016d | 392 | |
393 | template<typename _Tp> | |
394 | inline complex<_Tp> | |
395 | operator*(const complex<_Tp>& __x, const _Tp& __y) | |
dcd7c3cb | 396 | { |
397 | complex<_Tp> __r = __x; | |
398 | __r *= __y; | |
399 | return __r; | |
400 | } | |
e1e0016d | 401 | |
402 | template<typename _Tp> | |
403 | inline complex<_Tp> | |
404 | operator*(const _Tp& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 405 | { |
406 | complex<_Tp> __r = __y; | |
407 | __r *= __x; | |
408 | return __r; | |
409 | } | |
ed73ad37 | 410 | //@} |
e1e0016d | 411 | |
ed73ad37 | 412 | //@{ |
413 | /// Return new complex value @a x divided by @a y. | |
e1e0016d | 414 | template<typename _Tp> |
415 | inline complex<_Tp> | |
416 | operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 417 | { |
418 | complex<_Tp> __r = __x; | |
419 | __r /= __y; | |
420 | return __r; | |
421 | } | |
e1e0016d | 422 | |
423 | template<typename _Tp> | |
424 | inline complex<_Tp> | |
425 | operator/(const complex<_Tp>& __x, const _Tp& __y) | |
dcd7c3cb | 426 | { |
427 | complex<_Tp> __r = __x; | |
428 | __r /= __y; | |
429 | return __r; | |
430 | } | |
e1e0016d | 431 | |
432 | template<typename _Tp> | |
433 | inline complex<_Tp> | |
434 | operator/(const _Tp& __x, const complex<_Tp>& __y) | |
dcd7c3cb | 435 | { |
436 | complex<_Tp> __r = __x; | |
437 | __r /= __y; | |
438 | return __r; | |
439 | } | |
ed73ad37 | 440 | //@} |
e1e0016d | 441 | |
ed73ad37 | 442 | /// Return @a x. |
e1e0016d | 443 | template<typename _Tp> |
444 | inline complex<_Tp> | |
445 | operator+(const complex<_Tp>& __x) | |
446 | { return __x; } | |
447 | ||
ed73ad37 | 448 | /// Return complex negation of @a x. |
e1e0016d | 449 | template<typename _Tp> |
450 | inline complex<_Tp> | |
451 | operator-(const complex<_Tp>& __x) | |
452 | { return complex<_Tp>(-__x.real(), -__x.imag()); } | |
453 | ||
ed73ad37 | 454 | //@{ |
455 | /// Return true if @a x is equal to @a y. | |
e1e0016d | 456 | template<typename _Tp> |
43757f70 | 457 | inline _GLIBCXX_CONSTEXPR bool |
e1e0016d | 458 | operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) |
459 | { return __x.real() == __y.real() && __x.imag() == __y.imag(); } | |
460 | ||
461 | template<typename _Tp> | |
43757f70 | 462 | inline _GLIBCXX_CONSTEXPR bool |
e1e0016d | 463 | operator==(const complex<_Tp>& __x, const _Tp& __y) |
464 | { return __x.real() == __y && __x.imag() == _Tp(); } | |
465 | ||
466 | template<typename _Tp> | |
43757f70 | 467 | inline _GLIBCXX_CONSTEXPR bool |
e1e0016d | 468 | operator==(const _Tp& __x, const complex<_Tp>& __y) |
469 | { return __x == __y.real() && _Tp() == __y.imag(); } | |
ed73ad37 | 470 | //@} |
e1e0016d | 471 | |
ed73ad37 | 472 | //@{ |
473 | /// Return false if @a x is equal to @a y. | |
e1e0016d | 474 | template<typename _Tp> |
43757f70 | 475 | inline _GLIBCXX_CONSTEXPR bool |
e1e0016d | 476 | operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) |
477 | { return __x.real() != __y.real() || __x.imag() != __y.imag(); } | |
478 | ||
479 | template<typename _Tp> | |
43757f70 | 480 | inline _GLIBCXX_CONSTEXPR bool |
e1e0016d | 481 | operator!=(const complex<_Tp>& __x, const _Tp& __y) |
482 | { return __x.real() != __y || __x.imag() != _Tp(); } | |
483 | ||
484 | template<typename _Tp> | |
43757f70 | 485 | inline _GLIBCXX_CONSTEXPR bool |
e1e0016d | 486 | operator!=(const _Tp& __x, const complex<_Tp>& __y) |
487 | { return __x != __y.real() || _Tp() != __y.imag(); } | |
ed73ad37 | 488 | //@} |
e1e0016d | 489 | |
ed73ad37 | 490 | /// Extraction operator for complex values. |
e1e0016d | 491 | template<typename _Tp, typename _CharT, class _Traits> |
492 | basic_istream<_CharT, _Traits>& | |
493 | operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) | |
494 | { | |
495 | _Tp __re_x, __im_x; | |
496 | _CharT __ch; | |
497 | __is >> __ch; | |
498 | if (__ch == '(') | |
499 | { | |
500 | __is >> __re_x >> __ch; | |
501 | if (__ch == ',') | |
502 | { | |
503 | __is >> __im_x >> __ch; | |
504 | if (__ch == ')') | |
505 | __x = complex<_Tp>(__re_x, __im_x); | |
506 | else | |
507 | __is.setstate(ios_base::failbit); | |
508 | } | |
509 | else if (__ch == ')') | |
dcd7c3cb | 510 | __x = __re_x; |
e1e0016d | 511 | else |
512 | __is.setstate(ios_base::failbit); | |
513 | } | |
514 | else | |
515 | { | |
516 | __is.putback(__ch); | |
517 | __is >> __re_x; | |
dcd7c3cb | 518 | __x = __re_x; |
e1e0016d | 519 | } |
520 | return __is; | |
521 | } | |
522 | ||
ed73ad37 | 523 | /// Insertion operator for complex values. |
e1e0016d | 524 | template<typename _Tp, typename _CharT, class _Traits> |
525 | basic_ostream<_CharT, _Traits>& | |
526 | operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) | |
527 | { | |
528 | basic_ostringstream<_CharT, _Traits> __s; | |
529 | __s.flags(__os.flags()); | |
530 | __s.imbue(__os.getloc()); | |
531 | __s.precision(__os.precision()); | |
a48ffaa8 | 532 | __s << '(' << __x.real() << ',' << __x.imag() << ')'; |
e1e0016d | 533 | return __os << __s.str(); |
534 | } | |
535 | ||
536 | // Values | |
0c8766b1 | 537 | #if __cplusplus >= 201103L |
8c449bbb | 538 | template<typename _Tp> |
d07bb4f7 | 539 | constexpr _Tp |
8c449bbb | 540 | real(const complex<_Tp>& __z) |
541 | { return __z.real(); } | |
d07bb4f7 | 542 | |
8c449bbb | 543 | template<typename _Tp> |
d07bb4f7 | 544 | constexpr _Tp |
8c449bbb | 545 | imag(const complex<_Tp>& __z) |
546 | { return __z.imag(); } | |
547 | #else | |
e1e0016d | 548 | template<typename _Tp> |
dcd7c3cb | 549 | inline _Tp& |
550 | real(complex<_Tp>& __z) | |
551 | { return __z.real(); } | |
552 | ||
553 | template<typename _Tp> | |
554 | inline const _Tp& | |
e1e0016d | 555 | real(const complex<_Tp>& __z) |
556 | { return __z.real(); } | |
557 | ||
558 | template<typename _Tp> | |
dcd7c3cb | 559 | inline _Tp& |
560 | imag(complex<_Tp>& __z) | |
561 | { return __z.imag(); } | |
562 | ||
563 | template<typename _Tp> | |
564 | inline const _Tp& | |
e1e0016d | 565 | imag(const complex<_Tp>& __z) |
566 | { return __z.imag(); } | |
8c449bbb | 567 | #endif |
e1e0016d | 568 | |
ad25b344 | 569 | // 26.2.7/3 abs(__z): Returns the magnitude of __z. |
e1e0016d | 570 | template<typename _Tp> |
571 | inline _Tp | |
ad25b344 | 572 | __complex_abs(const complex<_Tp>& __z) |
e1e0016d | 573 | { |
574 | _Tp __x = __z.real(); | |
575 | _Tp __y = __z.imag(); | |
1a2ae8d8 | 576 | const _Tp __s = std::max(abs(__x), abs(__y)); |
e1e0016d | 577 | if (__s == _Tp()) // well ... |
578 | return __s; | |
579 | __x /= __s; | |
580 | __y /= __s; | |
9253fc43 | 581 | return __s * sqrt(__x * __x + __y * __y); |
e1e0016d | 582 | } |
583 | ||
3ebe371a | 584 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 585 | inline float |
586 | __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } | |
587 | ||
588 | inline double | |
589 | __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } | |
590 | ||
591 | inline long double | |
592 | __complex_abs(const __complex__ long double& __z) | |
9253fc43 | 593 | { return __builtin_cabsl(__z); } |
594 | ||
ad25b344 | 595 | template<typename _Tp> |
596 | inline _Tp | |
597 | abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } | |
9253fc43 | 598 | #else |
599 | template<typename _Tp> | |
600 | inline _Tp | |
601 | abs(const complex<_Tp>& __z) { return __complex_abs(__z); } | |
602 | #endif | |
ad25b344 | 603 | |
604 | ||
605 | // 26.2.7/4: arg(__z): Returns the phase angle of __z. | |
e1e0016d | 606 | template<typename _Tp> |
607 | inline _Tp | |
ad25b344 | 608 | __complex_arg(const complex<_Tp>& __z) |
9253fc43 | 609 | { return atan2(__z.imag(), __z.real()); } |
ad25b344 | 610 | |
3ebe371a | 611 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 612 | inline float |
613 | __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } | |
614 | ||
615 | inline double | |
616 | __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } | |
617 | ||
618 | inline long double | |
619 | __complex_arg(const __complex__ long double& __z) | |
620 | { return __builtin_cargl(__z); } | |
621 | ||
622 | template<typename _Tp> | |
623 | inline _Tp | |
624 | arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } | |
9253fc43 | 625 | #else |
626 | template<typename _Tp> | |
627 | inline _Tp | |
628 | arg(const complex<_Tp>& __z) { return __complex_arg(__z); } | |
629 | #endif | |
e1e0016d | 630 | |
9fc1117c | 631 | // 26.2.7/5: norm(__z) returns the squared magnitude of __z. |
e1e0016d | 632 | // As defined, norm() is -not- a norm is the common mathematical |
c44802ee | 633 | // sense used in numerics. The helper class _Norm_helper<> tries to |
e1e0016d | 634 | // distinguish between builtin floating point and the rest, so as |
635 | // to deliver an answer as close as possible to the real value. | |
636 | template<bool> | |
637 | struct _Norm_helper | |
638 | { | |
639 | template<typename _Tp> | |
640 | static inline _Tp _S_do_it(const complex<_Tp>& __z) | |
641 | { | |
642 | const _Tp __x = __z.real(); | |
643 | const _Tp __y = __z.imag(); | |
644 | return __x * __x + __y * __y; | |
645 | } | |
646 | }; | |
647 | ||
648 | template<> | |
649 | struct _Norm_helper<true> | |
650 | { | |
651 | template<typename _Tp> | |
652 | static inline _Tp _S_do_it(const complex<_Tp>& __z) | |
653 | { | |
9927a571 | 654 | _Tp __res = std::abs(__z); |
e1e0016d | 655 | return __res * __res; |
656 | } | |
657 | }; | |
658 | ||
659 | template<typename _Tp> | |
660 | inline _Tp | |
661 | norm(const complex<_Tp>& __z) | |
662 | { | |
6e8739c9 | 663 | return _Norm_helper<__is_floating<_Tp>::__value |
9253fc43 | 664 | && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); |
e1e0016d | 665 | } |
666 | ||
667 | template<typename _Tp> | |
668 | inline complex<_Tp> | |
669 | polar(const _Tp& __rho, const _Tp& __theta) | |
c32e3727 | 670 | { |
6b5e6f09 | 671 | __glibcxx_assert( __rho >= 0 ); |
c32e3727 | 672 | return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); |
673 | } | |
e1e0016d | 674 | |
675 | template<typename _Tp> | |
676 | inline complex<_Tp> | |
677 | conj(const complex<_Tp>& __z) | |
678 | { return complex<_Tp>(__z.real(), -__z.imag()); } | |
679 | ||
680 | // Transcendentals | |
ad25b344 | 681 | |
682 | // 26.2.8/1 cos(__z): Returns the cosine of __z. | |
e1e0016d | 683 | template<typename _Tp> |
684 | inline complex<_Tp> | |
ad25b344 | 685 | __complex_cos(const complex<_Tp>& __z) |
e1e0016d | 686 | { |
687 | const _Tp __x = __z.real(); | |
688 | const _Tp __y = __z.imag(); | |
1a2ae8d8 | 689 | return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); |
e1e0016d | 690 | } |
691 | ||
3ebe371a | 692 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 693 | inline __complex__ float |
694 | __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } | |
695 | ||
696 | inline __complex__ double | |
697 | __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } | |
698 | ||
699 | inline __complex__ long double | |
700 | __complex_cos(const __complex__ long double& __z) | |
701 | { return __builtin_ccosl(__z); } | |
9253fc43 | 702 | |
e1e0016d | 703 | template<typename _Tp> |
704 | inline complex<_Tp> | |
ad25b344 | 705 | cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } |
9253fc43 | 706 | #else |
707 | template<typename _Tp> | |
708 | inline complex<_Tp> | |
709 | cos(const complex<_Tp>& __z) { return __complex_cos(__z); } | |
710 | #endif | |
ad25b344 | 711 | |
712 | // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. | |
713 | template<typename _Tp> | |
714 | inline complex<_Tp> | |
715 | __complex_cosh(const complex<_Tp>& __z) | |
1ae1c5d9 | 716 | { |
717 | const _Tp __x = __z.real(); | |
718 | const _Tp __y = __z.imag(); | |
719 | return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); | |
720 | } | |
ad25b344 | 721 | |
3ebe371a | 722 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 723 | inline __complex__ float |
724 | __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } | |
725 | ||
726 | inline __complex__ double | |
727 | __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } | |
728 | ||
729 | inline __complex__ long double | |
730 | __complex_cosh(const __complex__ long double& __z) | |
731 | { return __builtin_ccoshl(__z); } | |
e1e0016d | 732 | |
733 | template<typename _Tp> | |
734 | inline complex<_Tp> | |
ad25b344 | 735 | cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } |
9253fc43 | 736 | #else |
737 | template<typename _Tp> | |
738 | inline complex<_Tp> | |
739 | cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } | |
740 | #endif | |
ad25b344 | 741 | |
742 | // 26.2.8/3 exp(__z): Returns the complex base e exponential of x | |
743 | template<typename _Tp> | |
744 | inline complex<_Tp> | |
745 | __complex_exp(const complex<_Tp>& __z) | |
753c4bad | 746 | { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } |
e1e0016d | 747 | |
3ebe371a | 748 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 749 | inline __complex__ float |
750 | __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } | |
751 | ||
752 | inline __complex__ double | |
753 | __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } | |
754 | ||
755 | inline __complex__ long double | |
1ae1c5d9 | 756 | __complex_exp(const __complex__ long double& __z) |
757 | { return __builtin_cexpl(__z); } | |
9253fc43 | 758 | |
e1e0016d | 759 | template<typename _Tp> |
760 | inline complex<_Tp> | |
ad25b344 | 761 | exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } |
9253fc43 | 762 | #else |
763 | template<typename _Tp> | |
764 | inline complex<_Tp> | |
765 | exp(const complex<_Tp>& __z) { return __complex_exp(__z); } | |
766 | #endif | |
ad25b344 | 767 | |
9fc1117c | 768 | // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. |
ad25b344 | 769 | // The branch cut is along the negative axis. |
770 | template<typename _Tp> | |
771 | inline complex<_Tp> | |
772 | __complex_log(const complex<_Tp>& __z) | |
1a2ae8d8 | 773 | { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } |
e1e0016d | 774 | |
24079773 | 775 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 776 | inline __complex__ float |
777 | __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } | |
778 | ||
779 | inline __complex__ double | |
780 | __complex_log(__complex__ double __z) { return __builtin_clog(__z); } | |
781 | ||
782 | inline __complex__ long double | |
783 | __complex_log(const __complex__ long double& __z) | |
24079773 | 784 | { return __builtin_clogl(__z); } |
ad25b344 | 785 | |
24079773 | 786 | template<typename _Tp> |
787 | inline complex<_Tp> | |
788 | log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } | |
789 | #else | |
ad25b344 | 790 | template<typename _Tp> |
791 | inline complex<_Tp> | |
44e413b3 | 792 | log(const complex<_Tp>& __z) { return __complex_log(__z); } |
24079773 | 793 | #endif |
ad25b344 | 794 | |
e1e0016d | 795 | template<typename _Tp> |
796 | inline complex<_Tp> | |
797 | log10(const complex<_Tp>& __z) | |
1a2ae8d8 | 798 | { return std::log(__z) / log(_Tp(10.0)); } |
e1e0016d | 799 | |
ad25b344 | 800 | // 26.2.8/10 sin(__z): Returns the sine of __z. |
e1e0016d | 801 | template<typename _Tp> |
802 | inline complex<_Tp> | |
ad25b344 | 803 | __complex_sin(const complex<_Tp>& __z) |
e1e0016d | 804 | { |
805 | const _Tp __x = __z.real(); | |
806 | const _Tp __y = __z.imag(); | |
1a2ae8d8 | 807 | return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); |
e1e0016d | 808 | } |
809 | ||
3ebe371a | 810 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 811 | inline __complex__ float |
812 | __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } | |
813 | ||
814 | inline __complex__ double | |
815 | __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } | |
816 | ||
817 | inline __complex__ long double | |
818 | __complex_sin(const __complex__ long double& __z) | |
819 | { return __builtin_csinl(__z); } | |
820 | ||
e1e0016d | 821 | template<typename _Tp> |
822 | inline complex<_Tp> | |
1ae1c5d9 | 823 | sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } |
9253fc43 | 824 | #else |
825 | template<typename _Tp> | |
826 | inline complex<_Tp> | |
827 | sin(const complex<_Tp>& __z) { return __complex_sin(__z); } | |
828 | #endif | |
ad25b344 | 829 | |
830 | // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. | |
831 | template<typename _Tp> | |
832 | inline complex<_Tp> | |
833 | __complex_sinh(const complex<_Tp>& __z) | |
e1e0016d | 834 | { |
835 | const _Tp __x = __z.real(); | |
836 | const _Tp __y = __z.imag(); | |
1a2ae8d8 | 837 | return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); |
e1e0016d | 838 | } |
839 | ||
3ebe371a | 840 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 841 | inline __complex__ float |
842 | __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } | |
843 | ||
844 | inline __complex__ double | |
845 | __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } | |
846 | ||
847 | inline __complex__ long double | |
848 | __complex_sinh(const __complex__ long double& __z) | |
849 | { return __builtin_csinhl(__z); } | |
850 | ||
851 | template<typename _Tp> | |
852 | inline complex<_Tp> | |
853 | sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } | |
9253fc43 | 854 | #else |
855 | template<typename _Tp> | |
856 | inline complex<_Tp> | |
857 | sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } | |
858 | #endif | |
ad25b344 | 859 | |
860 | // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. | |
861 | // The branch cut is on the negative axis. | |
e1e0016d | 862 | template<typename _Tp> |
863 | complex<_Tp> | |
ad25b344 | 864 | __complex_sqrt(const complex<_Tp>& __z) |
e1e0016d | 865 | { |
866 | _Tp __x = __z.real(); | |
867 | _Tp __y = __z.imag(); | |
868 | ||
869 | if (__x == _Tp()) | |
870 | { | |
1a2ae8d8 | 871 | _Tp __t = sqrt(abs(__y) / 2); |
e1e0016d | 872 | return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); |
873 | } | |
874 | else | |
875 | { | |
1a2ae8d8 | 876 | _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); |
e1e0016d | 877 | _Tp __u = __t / 2; |
878 | return __x > _Tp() | |
879 | ? complex<_Tp>(__u, __y / __t) | |
1a2ae8d8 | 880 | : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); |
e1e0016d | 881 | } |
882 | } | |
883 | ||
3ebe371a | 884 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 885 | inline __complex__ float |
886 | __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } | |
887 | ||
888 | inline __complex__ double | |
889 | __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } | |
890 | ||
891 | inline __complex__ long double | |
892 | __complex_sqrt(const __complex__ long double& __z) | |
893 | { return __builtin_csqrtl(__z); } | |
894 | ||
e1e0016d | 895 | template<typename _Tp> |
896 | inline complex<_Tp> | |
ad25b344 | 897 | sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } |
9253fc43 | 898 | #else |
899 | template<typename _Tp> | |
900 | inline complex<_Tp> | |
901 | sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } | |
902 | #endif | |
e1e0016d | 903 | |
ad25b344 | 904 | // 26.2.8/14 tan(__z): Return the complex tangent of __z. |
905 | ||
e1e0016d | 906 | template<typename _Tp> |
907 | inline complex<_Tp> | |
ad25b344 | 908 | __complex_tan(const complex<_Tp>& __z) |
909 | { return std::sin(__z) / std::cos(__z); } | |
910 | ||
3ebe371a | 911 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 912 | inline __complex__ float |
913 | __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } | |
914 | ||
915 | inline __complex__ double | |
916 | __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } | |
917 | ||
918 | inline __complex__ long double | |
919 | __complex_tan(const __complex__ long double& __z) | |
920 | { return __builtin_ctanl(__z); } | |
921 | ||
922 | template<typename _Tp> | |
923 | inline complex<_Tp> | |
924 | tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } | |
9253fc43 | 925 | #else |
926 | template<typename _Tp> | |
927 | inline complex<_Tp> | |
928 | tan(const complex<_Tp>& __z) { return __complex_tan(__z); } | |
929 | #endif | |
930 | ||
ad25b344 | 931 | |
932 | // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. | |
933 | ||
934 | template<typename _Tp> | |
935 | inline complex<_Tp> | |
936 | __complex_tanh(const complex<_Tp>& __z) | |
937 | { return std::sinh(__z) / std::cosh(__z); } | |
938 | ||
3ebe371a | 939 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 940 | inline __complex__ float |
941 | __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } | |
942 | ||
943 | inline __complex__ double | |
944 | __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } | |
945 | ||
946 | inline __complex__ long double | |
947 | __complex_tanh(const __complex__ long double& __z) | |
948 | { return __builtin_ctanhl(__z); } | |
e1e0016d | 949 | |
ad25b344 | 950 | template<typename _Tp> |
951 | inline complex<_Tp> | |
952 | tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } | |
9253fc43 | 953 | #else |
954 | template<typename _Tp> | |
955 | inline complex<_Tp> | |
956 | tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } | |
957 | #endif | |
958 | ||
ad25b344 | 959 | |
960 | // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x | |
961 | // raised to the __y-th power. The branch | |
962 | // cut is on the negative axis. | |
86e067d2 | 963 | template<typename _Tp> |
964 | complex<_Tp> | |
965 | __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) | |
966 | { | |
967 | complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); | |
968 | ||
969 | while (__n >>= 1) | |
970 | { | |
971 | __x *= __x; | |
972 | if (__n % 2) | |
973 | __y *= __x; | |
974 | } | |
975 | ||
976 | return __y; | |
977 | } | |
978 | ||
cd13c6c8 | 979 | // In C++11 mode we used to implement the resolution of |
2ec89d4f | 980 | // DR 844. complex pow return type is ambiguous. |
cd13c6c8 | 981 | // thus the following overload was disabled in that mode. However, doing |
982 | // that causes all sorts of issues, see, for example: | |
983 | // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html | |
984 | // and also PR57974. | |
e1e0016d | 985 | template<typename _Tp> |
986 | inline complex<_Tp> | |
987 | pow(const complex<_Tp>& __z, int __n) | |
86e067d2 | 988 | { |
989 | return __n < 0 | |
3b168595 | 990 | ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) |
86e067d2 | 991 | : std::__complex_pow_unsigned(__z, __n); |
992 | } | |
e1e0016d | 993 | |
994 | template<typename _Tp> | |
ab6501ef | 995 | complex<_Tp> |
e1e0016d | 996 | pow(const complex<_Tp>& __x, const _Tp& __y) |
997 | { | |
bdb62e6a | 998 | #if ! _GLIBCXX_USE_C99_COMPLEX |
72ecee27 | 999 | if (__x == _Tp()) |
1000 | return _Tp(); | |
1001 | #endif | |
d8928427 | 1002 | if (__x.imag() == _Tp() && __x.real() > _Tp()) |
1a2ae8d8 | 1003 | return pow(__x.real(), __y); |
ab6501ef | 1004 | |
bf945d40 | 1005 | complex<_Tp> __t = std::log(__x); |
753c4bad | 1006 | return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); |
e1e0016d | 1007 | } |
1008 | ||
ad25b344 | 1009 | template<typename _Tp> |
1010 | inline complex<_Tp> | |
1011 | __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
1012 | { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } | |
1013 | ||
3ebe371a | 1014 | #if _GLIBCXX_USE_C99_COMPLEX |
ad25b344 | 1015 | inline __complex__ float |
1016 | __complex_pow(__complex__ float __x, __complex__ float __y) | |
1017 | { return __builtin_cpowf(__x, __y); } | |
1018 | ||
1019 | inline __complex__ double | |
1020 | __complex_pow(__complex__ double __x, __complex__ double __y) | |
1021 | { return __builtin_cpow(__x, __y); } | |
1022 | ||
1023 | inline __complex__ long double | |
59505270 | 1024 | __complex_pow(const __complex__ long double& __x, |
1025 | const __complex__ long double& __y) | |
ad25b344 | 1026 | { return __builtin_cpowl(__x, __y); } |
9253fc43 | 1027 | |
59505270 | 1028 | template<typename _Tp> |
1029 | inline complex<_Tp> | |
1030 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
1031 | { return __complex_pow(__x.__rep(), __y.__rep()); } | |
1032 | #else | |
e1e0016d | 1033 | template<typename _Tp> |
1034 | inline complex<_Tp> | |
1035 | pow(const complex<_Tp>& __x, const complex<_Tp>& __y) | |
ad25b344 | 1036 | { return __complex_pow(__x, __y); } |
59505270 | 1037 | #endif |
e1e0016d | 1038 | |
1039 | template<typename _Tp> | |
1040 | inline complex<_Tp> | |
1041 | pow(const _Tp& __x, const complex<_Tp>& __y) | |
1042 | { | |
753c4bad | 1043 | return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), |
1044 | __y.imag() * log(__x)) | |
253c3a91 | 1045 | : std::pow(complex<_Tp>(__x), __y); |
e1e0016d | 1046 | } |
1047 | ||
db3b35cb | 1048 | /// 26.2.3 complex specializations |
1049 | /// complex<float> specialization | |
ad25b344 | 1050 | template<> |
1051 | struct complex<float> | |
1052 | { | |
1053 | typedef float value_type; | |
1054 | typedef __complex__ float _ComplexT; | |
1055 | ||
c0000147 | 1056 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
ad25b344 | 1057 | |
c0000147 | 1058 | _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) |
0c8766b1 | 1059 | #if __cplusplus >= 201103L |
09a2ff97 | 1060 | : _M_value{ __r, __i } { } |
1061 | #else | |
1062 | { | |
1063 | __real__ _M_value = __r; | |
1064 | __imag__ _M_value = __i; | |
1065 | } | |
1066 | #endif | |
356ae535 | 1067 | |
c0000147 | 1068 | explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); |
1069 | explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); | |
253c3a91 | 1070 | |
0c8766b1 | 1071 | #if __cplusplus >= 201103L |
8c449bbb | 1072 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1073 | // DR 387. std::complex over-encapsulated. | |
96ac5968 | 1074 | __attribute ((__abi_tag__ ("cxx11"))) |
c0000147 | 1075 | constexpr float |
5c285588 | 1076 | real() const { return __real__ _M_value; } |
8c449bbb | 1077 | |
96ac5968 | 1078 | __attribute ((__abi_tag__ ("cxx11"))) |
c0000147 | 1079 | constexpr float |
5c285588 | 1080 | imag() const { return __imag__ _M_value; } |
8c449bbb | 1081 | #else |
c0000147 | 1082 | float& |
1083 | real() { return __real__ _M_value; } | |
253c3a91 | 1084 | |
c0000147 | 1085 | const float& |
1086 | real() const { return __real__ _M_value; } | |
253c3a91 | 1087 | |
c0000147 | 1088 | float& |
1089 | imag() { return __imag__ _M_value; } | |
253c3a91 | 1090 | |
c0000147 | 1091 | const float& |
1092 | imag() const { return __imag__ _M_value; } | |
8c449bbb | 1093 | #endif |
1094 | ||
1095 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1096 | // DR 387. std::complex over-encapsulated. | |
c0000147 | 1097 | void |
1098 | real(float __val) { __real__ _M_value = __val; } | |
8c449bbb | 1099 | |
c0000147 | 1100 | void |
1101 | imag(float __val) { __imag__ _M_value = __val; } | |
253c3a91 | 1102 | |
59fa4003 | 1103 | complex& |
253c3a91 | 1104 | operator=(float __f) |
1105 | { | |
59fa4003 | 1106 | _M_value = __f; |
253c3a91 | 1107 | return *this; |
1108 | } | |
1109 | ||
59fa4003 | 1110 | complex& |
253c3a91 | 1111 | operator+=(float __f) |
1112 | { | |
59fa4003 | 1113 | _M_value += __f; |
253c3a91 | 1114 | return *this; |
1115 | } | |
1116 | ||
59fa4003 | 1117 | complex& |
253c3a91 | 1118 | operator-=(float __f) |
1119 | { | |
59fa4003 | 1120 | _M_value -= __f; |
253c3a91 | 1121 | return *this; |
1122 | } | |
1123 | ||
59fa4003 | 1124 | complex& |
253c3a91 | 1125 | operator*=(float __f) |
1126 | { | |
1127 | _M_value *= __f; | |
1128 | return *this; | |
1129 | } | |
1130 | ||
59fa4003 | 1131 | complex& |
253c3a91 | 1132 | operator/=(float __f) |
1133 | { | |
1134 | _M_value /= __f; | |
1135 | return *this; | |
1136 | } | |
ad25b344 | 1137 | |
9fc1117c | 1138 | // Let the compiler synthesize the copy and assignment |
ad25b344 | 1139 | // operator. It always does a pretty good job. |
253c3a91 | 1140 | // complex& operator=(const complex&); |
1141 | ||
ad25b344 | 1142 | template<typename _Tp> |
59fa4003 | 1143 | complex& |
253c3a91 | 1144 | operator=(const complex<_Tp>& __z) |
1145 | { | |
1146 | __real__ _M_value = __z.real(); | |
1147 | __imag__ _M_value = __z.imag(); | |
1148 | return *this; | |
1149 | } | |
1150 | ||
ad25b344 | 1151 | template<typename _Tp> |
59fa4003 | 1152 | complex& |
253c3a91 | 1153 | operator+=(const complex<_Tp>& __z) |
1154 | { | |
1155 | __real__ _M_value += __z.real(); | |
1156 | __imag__ _M_value += __z.imag(); | |
1157 | return *this; | |
1158 | } | |
1159 | ||
ad25b344 | 1160 | template<class _Tp> |
59fa4003 | 1161 | complex& |
253c3a91 | 1162 | operator-=(const complex<_Tp>& __z) |
1163 | { | |
1164 | __real__ _M_value -= __z.real(); | |
1165 | __imag__ _M_value -= __z.imag(); | |
1166 | return *this; | |
1167 | } | |
1168 | ||
ad25b344 | 1169 | template<class _Tp> |
59fa4003 | 1170 | complex& |
253c3a91 | 1171 | operator*=(const complex<_Tp>& __z) |
1172 | { | |
1173 | _ComplexT __t; | |
1174 | __real__ __t = __z.real(); | |
1175 | __imag__ __t = __z.imag(); | |
1176 | _M_value *= __t; | |
1177 | return *this; | |
1178 | } | |
1179 | ||
ad25b344 | 1180 | template<class _Tp> |
59fa4003 | 1181 | complex& |
253c3a91 | 1182 | operator/=(const complex<_Tp>& __z) |
1183 | { | |
1184 | _ComplexT __t; | |
1185 | __real__ __t = __z.real(); | |
1186 | __imag__ __t = __z.imag(); | |
1187 | _M_value /= __t; | |
1188 | return *this; | |
1189 | } | |
ad25b344 | 1190 | |
6b688037 | 1191 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
ad25b344 | 1192 | |
1193 | private: | |
1194 | _ComplexT _M_value; | |
1195 | }; | |
e1e0016d | 1196 | |
db3b35cb | 1197 | /// 26.2.3 complex specializations |
1198 | /// complex<double> specialization | |
ad25b344 | 1199 | template<> |
1200 | struct complex<double> | |
1201 | { | |
1202 | typedef double value_type; | |
1203 | typedef __complex__ double _ComplexT; | |
1204 | ||
c0000147 | 1205 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
e1e0016d | 1206 | |
c0000147 | 1207 | _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) |
0c8766b1 | 1208 | #if __cplusplus >= 201103L |
09a2ff97 | 1209 | : _M_value{ __r, __i } { } |
1210 | #else | |
1211 | { | |
1212 | __real__ _M_value = __r; | |
1213 | __imag__ _M_value = __i; | |
1214 | } | |
1215 | #endif | |
253c3a91 | 1216 | |
c0000147 | 1217 | _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) |
253c3a91 | 1218 | : _M_value(__z.__rep()) { } |
1219 | ||
c0000147 | 1220 | explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); |
253c3a91 | 1221 | |
0c8766b1 | 1222 | #if __cplusplus >= 201103L |
8c449bbb | 1223 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1224 | // DR 387. std::complex over-encapsulated. | |
96ac5968 | 1225 | __attribute ((__abi_tag__ ("cxx11"))) |
c0000147 | 1226 | constexpr double |
5c285588 | 1227 | real() const { return __real__ _M_value; } |
8c449bbb | 1228 | |
96ac5968 | 1229 | __attribute ((__abi_tag__ ("cxx11"))) |
c0000147 | 1230 | constexpr double |
5c285588 | 1231 | imag() const { return __imag__ _M_value; } |
8c449bbb | 1232 | #else |
c0000147 | 1233 | double& |
1234 | real() { return __real__ _M_value; } | |
253c3a91 | 1235 | |
c0000147 | 1236 | const double& |
1237 | real() const { return __real__ _M_value; } | |
253c3a91 | 1238 | |
c0000147 | 1239 | double& |
1240 | imag() { return __imag__ _M_value; } | |
253c3a91 | 1241 | |
c0000147 | 1242 | const double& |
1243 | imag() const { return __imag__ _M_value; } | |
8c449bbb | 1244 | #endif |
1245 | ||
1246 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1247 | // DR 387. std::complex over-encapsulated. | |
c0000147 | 1248 | void |
1249 | real(double __val) { __real__ _M_value = __val; } | |
8c449bbb | 1250 | |
c0000147 | 1251 | void |
1252 | imag(double __val) { __imag__ _M_value = __val; } | |
253c3a91 | 1253 | |
59fa4003 | 1254 | complex& |
253c3a91 | 1255 | operator=(double __d) |
1256 | { | |
59fa4003 | 1257 | _M_value = __d; |
253c3a91 | 1258 | return *this; |
1259 | } | |
1260 | ||
59fa4003 | 1261 | complex& |
253c3a91 | 1262 | operator+=(double __d) |
1263 | { | |
59fa4003 | 1264 | _M_value += __d; |
253c3a91 | 1265 | return *this; |
1266 | } | |
1267 | ||
59fa4003 | 1268 | complex& |
253c3a91 | 1269 | operator-=(double __d) |
1270 | { | |
59fa4003 | 1271 | _M_value -= __d; |
253c3a91 | 1272 | return *this; |
1273 | } | |
1274 | ||
59fa4003 | 1275 | complex& |
253c3a91 | 1276 | operator*=(double __d) |
1277 | { | |
1278 | _M_value *= __d; | |
1279 | return *this; | |
1280 | } | |
1281 | ||
59fa4003 | 1282 | complex& |
253c3a91 | 1283 | operator/=(double __d) |
1284 | { | |
1285 | _M_value /= __d; | |
1286 | return *this; | |
1287 | } | |
ad25b344 | 1288 | |
9fc1117c | 1289 | // The compiler will synthesize this, efficiently. |
253c3a91 | 1290 | // complex& operator=(const complex&); |
1291 | ||
ad25b344 | 1292 | template<typename _Tp> |
59fa4003 | 1293 | complex& |
253c3a91 | 1294 | operator=(const complex<_Tp>& __z) |
1295 | { | |
1296 | __real__ _M_value = __z.real(); | |
1297 | __imag__ _M_value = __z.imag(); | |
1298 | return *this; | |
1299 | } | |
1300 | ||
ad25b344 | 1301 | template<typename _Tp> |
59fa4003 | 1302 | complex& |
253c3a91 | 1303 | operator+=(const complex<_Tp>& __z) |
1304 | { | |
1305 | __real__ _M_value += __z.real(); | |
1306 | __imag__ _M_value += __z.imag(); | |
1307 | return *this; | |
1308 | } | |
1309 | ||
ad25b344 | 1310 | template<typename _Tp> |
59fa4003 | 1311 | complex& |
253c3a91 | 1312 | operator-=(const complex<_Tp>& __z) |
1313 | { | |
1314 | __real__ _M_value -= __z.real(); | |
1315 | __imag__ _M_value -= __z.imag(); | |
1316 | return *this; | |
1317 | } | |
1318 | ||
ad25b344 | 1319 | template<typename _Tp> |
59fa4003 | 1320 | complex& |
253c3a91 | 1321 | operator*=(const complex<_Tp>& __z) |
1322 | { | |
1323 | _ComplexT __t; | |
1324 | __real__ __t = __z.real(); | |
1325 | __imag__ __t = __z.imag(); | |
1326 | _M_value *= __t; | |
1327 | return *this; | |
1328 | } | |
1329 | ||
ad25b344 | 1330 | template<typename _Tp> |
59fa4003 | 1331 | complex& |
253c3a91 | 1332 | operator/=(const complex<_Tp>& __z) |
1333 | { | |
1334 | _ComplexT __t; | |
1335 | __real__ __t = __z.real(); | |
1336 | __imag__ __t = __z.imag(); | |
1337 | _M_value /= __t; | |
1338 | return *this; | |
1339 | } | |
dcd7c3cb | 1340 | |
6b688037 | 1341 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
ad25b344 | 1342 | |
1343 | private: | |
1344 | _ComplexT _M_value; | |
1345 | }; | |
e1e0016d | 1346 | |
db3b35cb | 1347 | /// 26.2.3 complex specializations |
1348 | /// complex<long double> specialization | |
ad25b344 | 1349 | template<> |
1350 | struct complex<long double> | |
1351 | { | |
1352 | typedef long double value_type; | |
1353 | typedef __complex__ long double _ComplexT; | |
e1e0016d | 1354 | |
c0000147 | 1355 | _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } |
ad25b344 | 1356 | |
c0000147 | 1357 | _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, |
1358 | long double __i = 0.0L) | |
0c8766b1 | 1359 | #if __cplusplus >= 201103L |
09a2ff97 | 1360 | : _M_value{ __r, __i } { } |
1361 | #else | |
1362 | { | |
1363 | __real__ _M_value = __r; | |
1364 | __imag__ _M_value = __i; | |
1365 | } | |
1366 | #endif | |
253c3a91 | 1367 | |
c0000147 | 1368 | _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) |
253c3a91 | 1369 | : _M_value(__z.__rep()) { } |
1370 | ||
c0000147 | 1371 | _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) |
253c3a91 | 1372 | : _M_value(__z.__rep()) { } |
1373 | ||
0c8766b1 | 1374 | #if __cplusplus >= 201103L |
8c449bbb | 1375 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1376 | // DR 387. std::complex over-encapsulated. | |
96ac5968 | 1377 | __attribute ((__abi_tag__ ("cxx11"))) |
c0000147 | 1378 | constexpr long double |
5c285588 | 1379 | real() const { return __real__ _M_value; } |
8c449bbb | 1380 | |
96ac5968 | 1381 | __attribute ((__abi_tag__ ("cxx11"))) |
c0000147 | 1382 | constexpr long double |
5c285588 | 1383 | imag() const { return __imag__ _M_value; } |
8c449bbb | 1384 | #else |
c0000147 | 1385 | long double& |
1386 | real() { return __real__ _M_value; } | |
253c3a91 | 1387 | |
c0000147 | 1388 | const long double& |
1389 | real() const { return __real__ _M_value; } | |
253c3a91 | 1390 | |
c0000147 | 1391 | long double& |
1392 | imag() { return __imag__ _M_value; } | |
253c3a91 | 1393 | |
c0000147 | 1394 | const long double& |
1395 | imag() const { return __imag__ _M_value; } | |
8c449bbb | 1396 | #endif |
1397 | ||
1398 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
1399 | // DR 387. std::complex over-encapsulated. | |
c0000147 | 1400 | void |
1401 | real(long double __val) { __real__ _M_value = __val; } | |
8c449bbb | 1402 | |
c0000147 | 1403 | void |
1404 | imag(long double __val) { __imag__ _M_value = __val; } | |
253c3a91 | 1405 | |
59fa4003 | 1406 | complex& |
253c3a91 | 1407 | operator=(long double __r) |
1408 | { | |
59fa4003 | 1409 | _M_value = __r; |
253c3a91 | 1410 | return *this; |
1411 | } | |
1412 | ||
59fa4003 | 1413 | complex& |
253c3a91 | 1414 | operator+=(long double __r) |
1415 | { | |
59fa4003 | 1416 | _M_value += __r; |
253c3a91 | 1417 | return *this; |
1418 | } | |
1419 | ||
59fa4003 | 1420 | complex& |
253c3a91 | 1421 | operator-=(long double __r) |
1422 | { | |
59fa4003 | 1423 | _M_value -= __r; |
253c3a91 | 1424 | return *this; |
1425 | } | |
1426 | ||
59fa4003 | 1427 | complex& |
253c3a91 | 1428 | operator*=(long double __r) |
1429 | { | |
1430 | _M_value *= __r; | |
1431 | return *this; | |
1432 | } | |
1433 | ||
59fa4003 | 1434 | complex& |
253c3a91 | 1435 | operator/=(long double __r) |
1436 | { | |
1437 | _M_value /= __r; | |
1438 | return *this; | |
1439 | } | |
ad25b344 | 1440 | |
1441 | // The compiler knows how to do this efficiently | |
253c3a91 | 1442 | // complex& operator=(const complex&); |
1443 | ||
ad25b344 | 1444 | template<typename _Tp> |
59fa4003 | 1445 | complex& |
253c3a91 | 1446 | operator=(const complex<_Tp>& __z) |
1447 | { | |
1448 | __real__ _M_value = __z.real(); | |
1449 | __imag__ _M_value = __z.imag(); | |
1450 | return *this; | |
1451 | } | |
1452 | ||
ad25b344 | 1453 | template<typename _Tp> |
59fa4003 | 1454 | complex& |
253c3a91 | 1455 | operator+=(const complex<_Tp>& __z) |
1456 | { | |
1457 | __real__ _M_value += __z.real(); | |
1458 | __imag__ _M_value += __z.imag(); | |
1459 | return *this; | |
1460 | } | |
1461 | ||
ad25b344 | 1462 | template<typename _Tp> |
59fa4003 | 1463 | complex& |
253c3a91 | 1464 | operator-=(const complex<_Tp>& __z) |
1465 | { | |
1466 | __real__ _M_value -= __z.real(); | |
1467 | __imag__ _M_value -= __z.imag(); | |
1468 | return *this; | |
1469 | } | |
1470 | ||
ad25b344 | 1471 | template<typename _Tp> |
59fa4003 | 1472 | complex& |
253c3a91 | 1473 | operator*=(const complex<_Tp>& __z) |
1474 | { | |
1475 | _ComplexT __t; | |
1476 | __real__ __t = __z.real(); | |
1477 | __imag__ __t = __z.imag(); | |
1478 | _M_value *= __t; | |
1479 | return *this; | |
1480 | } | |
1481 | ||
ad25b344 | 1482 | template<typename _Tp> |
59fa4003 | 1483 | complex& |
253c3a91 | 1484 | operator/=(const complex<_Tp>& __z) |
1485 | { | |
1486 | _ComplexT __t; | |
1487 | __real__ __t = __z.real(); | |
1488 | __imag__ __t = __z.imag(); | |
1489 | _M_value /= __t; | |
1490 | return *this; | |
1491 | } | |
ad25b344 | 1492 | |
6b688037 | 1493 | _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } |
ad25b344 | 1494 | |
1495 | private: | |
1496 | _ComplexT _M_value; | |
1497 | }; | |
e1e0016d | 1498 | |
e1e0016d | 1499 | // These bits have to be at the end of this file, so that the |
1500 | // specializations have all been defined. | |
c0000147 | 1501 | inline _GLIBCXX_CONSTEXPR |
e1e0016d | 1502 | complex<float>::complex(const complex<double>& __z) |
ad25b344 | 1503 | : _M_value(__z.__rep()) { } |
e1e0016d | 1504 | |
c0000147 | 1505 | inline _GLIBCXX_CONSTEXPR |
e1e0016d | 1506 | complex<float>::complex(const complex<long double>& __z) |
ad25b344 | 1507 | : _M_value(__z.__rep()) { } |
e1e0016d | 1508 | |
c0000147 | 1509 | inline _GLIBCXX_CONSTEXPR |
e1e0016d | 1510 | complex<double>::complex(const complex<long double>& __z) |
41819b5f | 1511 | : _M_value(__z.__rep()) { } |
e1e0016d | 1512 | |
7b2e5f56 | 1513 | // Inhibit implicit instantiations for required instantiations, |
1514 | // which are defined via explicit instantiations elsewhere. | |
1515 | // NB: This syntax is a GNU extension. | |
1516 | #if _GLIBCXX_EXTERN_TEMPLATE | |
1517 | extern template istream& operator>>(istream&, complex<float>&); | |
1518 | extern template ostream& operator<<(ostream&, const complex<float>&); | |
1519 | extern template istream& operator>>(istream&, complex<double>&); | |
1520 | extern template ostream& operator<<(ostream&, const complex<double>&); | |
1521 | extern template istream& operator>>(istream&, complex<long double>&); | |
1522 | extern template ostream& operator<<(ostream&, const complex<long double>&); | |
1523 | ||
1524 | #ifdef _GLIBCXX_USE_WCHAR_T | |
1525 | extern template wistream& operator>>(wistream&, complex<float>&); | |
1526 | extern template wostream& operator<<(wostream&, const complex<float>&); | |
1527 | extern template wistream& operator>>(wistream&, complex<double>&); | |
1528 | extern template wostream& operator<<(wostream&, const complex<double>&); | |
1529 | extern template wistream& operator>>(wistream&, complex<long double>&); | |
1530 | extern template wostream& operator<<(wostream&, const complex<long double>&); | |
1531 | #endif | |
1532 | #endif | |
1533 | ||
97a32de0 | 1534 | // @} group complex_numbers |
1535 | ||
2948dd21 | 1536 | _GLIBCXX_END_NAMESPACE_VERSION |
1537 | } // namespace | |
e1e0016d | 1538 | |
2948dd21 | 1539 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) |
1540 | { | |
1541 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
c17b0a1c | 1542 | |
1543 | // See ext/type_traits.h for the primary template. | |
1544 | template<typename _Tp, typename _Up> | |
1545 | struct __promote_2<std::complex<_Tp>, _Up> | |
1546 | { | |
1547 | public: | |
1548 | typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; | |
1549 | }; | |
1550 | ||
1551 | template<typename _Tp, typename _Up> | |
1552 | struct __promote_2<_Tp, std::complex<_Up> > | |
1553 | { | |
1554 | public: | |
1555 | typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; | |
1556 | }; | |
1557 | ||
1558 | template<typename _Tp, typename _Up> | |
1559 | struct __promote_2<std::complex<_Tp>, std::complex<_Up> > | |
1560 | { | |
1561 | public: | |
1562 | typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; | |
1563 | }; | |
1564 | ||
2948dd21 | 1565 | _GLIBCXX_END_NAMESPACE_VERSION |
1566 | } // namespace | |
c17b0a1c | 1567 | |
0c8766b1 | 1568 | #if __cplusplus >= 201103L |
df2fcd57 | 1569 | |
2948dd21 | 1570 | namespace std _GLIBCXX_VISIBILITY(default) |
1571 | { | |
1572 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
df2fcd57 | 1573 | |
5022cf3f | 1574 | // Forward declarations. |
1575 | template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); | |
1576 | template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); | |
1577 | template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); | |
1578 | ||
1579 | template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); | |
1580 | template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); | |
1581 | template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); | |
1582 | // DR 595. | |
1583 | template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); | |
1584 | ||
1585 | template<typename _Tp> | |
1586 | inline std::complex<_Tp> | |
1587 | __complex_acos(const std::complex<_Tp>& __z) | |
1588 | { | |
1589 | const std::complex<_Tp> __t = std::asin(__z); | |
1590 | const _Tp __pi_2 = 1.5707963267948966192313216916397514L; | |
1591 | return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); | |
1592 | } | |
1593 | ||
1594 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1595 | inline __complex__ float | |
1596 | __complex_acos(__complex__ float __z) | |
1597 | { return __builtin_cacosf(__z); } | |
1598 | ||
1599 | inline __complex__ double | |
1600 | __complex_acos(__complex__ double __z) | |
1601 | { return __builtin_cacos(__z); } | |
1602 | ||
1603 | inline __complex__ long double | |
1604 | __complex_acos(const __complex__ long double& __z) | |
1605 | { return __builtin_cacosl(__z); } | |
1606 | ||
1607 | template<typename _Tp> | |
1608 | inline std::complex<_Tp> | |
1609 | acos(const std::complex<_Tp>& __z) | |
1610 | { return __complex_acos(__z.__rep()); } | |
1611 | #else | |
1612 | /// acos(__z) [8.1.2]. | |
1613 | // Effects: Behaves the same as C99 function cacos, defined | |
1614 | // in subclause 7.3.5.1. | |
1615 | template<typename _Tp> | |
1616 | inline std::complex<_Tp> | |
1617 | acos(const std::complex<_Tp>& __z) | |
1618 | { return __complex_acos(__z); } | |
1619 | #endif | |
1620 | ||
1621 | template<typename _Tp> | |
1622 | inline std::complex<_Tp> | |
1623 | __complex_asin(const std::complex<_Tp>& __z) | |
1624 | { | |
1625 | std::complex<_Tp> __t(-__z.imag(), __z.real()); | |
1626 | __t = std::asinh(__t); | |
1627 | return std::complex<_Tp>(__t.imag(), -__t.real()); | |
1628 | } | |
1629 | ||
1630 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1631 | inline __complex__ float | |
1632 | __complex_asin(__complex__ float __z) | |
1633 | { return __builtin_casinf(__z); } | |
1634 | ||
1635 | inline __complex__ double | |
1636 | __complex_asin(__complex__ double __z) | |
1637 | { return __builtin_casin(__z); } | |
1638 | ||
1639 | inline __complex__ long double | |
1640 | __complex_asin(const __complex__ long double& __z) | |
1641 | { return __builtin_casinl(__z); } | |
1642 | ||
1643 | template<typename _Tp> | |
1644 | inline std::complex<_Tp> | |
1645 | asin(const std::complex<_Tp>& __z) | |
1646 | { return __complex_asin(__z.__rep()); } | |
1647 | #else | |
1648 | /// asin(__z) [8.1.3]. | |
1649 | // Effects: Behaves the same as C99 function casin, defined | |
1650 | // in subclause 7.3.5.2. | |
1651 | template<typename _Tp> | |
1652 | inline std::complex<_Tp> | |
1653 | asin(const std::complex<_Tp>& __z) | |
1654 | { return __complex_asin(__z); } | |
1655 | #endif | |
1656 | ||
1657 | template<typename _Tp> | |
1658 | std::complex<_Tp> | |
1659 | __complex_atan(const std::complex<_Tp>& __z) | |
1660 | { | |
1661 | const _Tp __r2 = __z.real() * __z.real(); | |
1662 | const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); | |
1663 | ||
1664 | _Tp __num = __z.imag() + _Tp(1.0); | |
1665 | _Tp __den = __z.imag() - _Tp(1.0); | |
1666 | ||
1667 | __num = __r2 + __num * __num; | |
1668 | __den = __r2 + __den * __den; | |
1669 | ||
1670 | return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), | |
1671 | _Tp(0.25) * log(__num / __den)); | |
1672 | } | |
1673 | ||
1674 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1675 | inline __complex__ float | |
1676 | __complex_atan(__complex__ float __z) | |
1677 | { return __builtin_catanf(__z); } | |
1678 | ||
1679 | inline __complex__ double | |
1680 | __complex_atan(__complex__ double __z) | |
1681 | { return __builtin_catan(__z); } | |
1682 | ||
1683 | inline __complex__ long double | |
1684 | __complex_atan(const __complex__ long double& __z) | |
1685 | { return __builtin_catanl(__z); } | |
1686 | ||
1687 | template<typename _Tp> | |
1688 | inline std::complex<_Tp> | |
1689 | atan(const std::complex<_Tp>& __z) | |
1690 | { return __complex_atan(__z.__rep()); } | |
1691 | #else | |
1692 | /// atan(__z) [8.1.4]. | |
1693 | // Effects: Behaves the same as C99 function catan, defined | |
1694 | // in subclause 7.3.5.3. | |
1695 | template<typename _Tp> | |
1696 | inline std::complex<_Tp> | |
1697 | atan(const std::complex<_Tp>& __z) | |
1698 | { return __complex_atan(__z); } | |
1699 | #endif | |
1700 | ||
1701 | template<typename _Tp> | |
1702 | std::complex<_Tp> | |
1703 | __complex_acosh(const std::complex<_Tp>& __z) | |
1704 | { | |
c1aeb0dd | 1705 | // Kahan's formula. |
1706 | return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) | |
1707 | + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); | |
5022cf3f | 1708 | } |
1709 | ||
1710 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1711 | inline __complex__ float | |
1712 | __complex_acosh(__complex__ float __z) | |
1713 | { return __builtin_cacoshf(__z); } | |
1714 | ||
1715 | inline __complex__ double | |
1716 | __complex_acosh(__complex__ double __z) | |
1717 | { return __builtin_cacosh(__z); } | |
1718 | ||
1719 | inline __complex__ long double | |
1720 | __complex_acosh(const __complex__ long double& __z) | |
1721 | { return __builtin_cacoshl(__z); } | |
1722 | ||
1723 | template<typename _Tp> | |
1724 | inline std::complex<_Tp> | |
1725 | acosh(const std::complex<_Tp>& __z) | |
1726 | { return __complex_acosh(__z.__rep()); } | |
1727 | #else | |
1728 | /// acosh(__z) [8.1.5]. | |
1729 | // Effects: Behaves the same as C99 function cacosh, defined | |
1730 | // in subclause 7.3.6.1. | |
1731 | template<typename _Tp> | |
1732 | inline std::complex<_Tp> | |
1733 | acosh(const std::complex<_Tp>& __z) | |
1734 | { return __complex_acosh(__z); } | |
1735 | #endif | |
1736 | ||
1737 | template<typename _Tp> | |
1738 | std::complex<_Tp> | |
1739 | __complex_asinh(const std::complex<_Tp>& __z) | |
1740 | { | |
1741 | std::complex<_Tp> __t((__z.real() - __z.imag()) | |
1742 | * (__z.real() + __z.imag()) + _Tp(1.0), | |
1743 | _Tp(2.0) * __z.real() * __z.imag()); | |
1744 | __t = std::sqrt(__t); | |
1745 | ||
1746 | return std::log(__t + __z); | |
1747 | } | |
1748 | ||
1749 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1750 | inline __complex__ float | |
1751 | __complex_asinh(__complex__ float __z) | |
1752 | { return __builtin_casinhf(__z); } | |
1753 | ||
1754 | inline __complex__ double | |
1755 | __complex_asinh(__complex__ double __z) | |
1756 | { return __builtin_casinh(__z); } | |
1757 | ||
1758 | inline __complex__ long double | |
1759 | __complex_asinh(const __complex__ long double& __z) | |
1760 | { return __builtin_casinhl(__z); } | |
1761 | ||
1762 | template<typename _Tp> | |
1763 | inline std::complex<_Tp> | |
1764 | asinh(const std::complex<_Tp>& __z) | |
1765 | { return __complex_asinh(__z.__rep()); } | |
1766 | #else | |
1767 | /// asinh(__z) [8.1.6]. | |
1768 | // Effects: Behaves the same as C99 function casin, defined | |
1769 | // in subclause 7.3.6.2. | |
1770 | template<typename _Tp> | |
1771 | inline std::complex<_Tp> | |
1772 | asinh(const std::complex<_Tp>& __z) | |
1773 | { return __complex_asinh(__z); } | |
1774 | #endif | |
1775 | ||
1776 | template<typename _Tp> | |
1777 | std::complex<_Tp> | |
1778 | __complex_atanh(const std::complex<_Tp>& __z) | |
1779 | { | |
1780 | const _Tp __i2 = __z.imag() * __z.imag(); | |
1781 | const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); | |
1782 | ||
1783 | _Tp __num = _Tp(1.0) + __z.real(); | |
1784 | _Tp __den = _Tp(1.0) - __z.real(); | |
1785 | ||
1786 | __num = __i2 + __num * __num; | |
1787 | __den = __i2 + __den * __den; | |
1788 | ||
1789 | return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), | |
1790 | _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); | |
1791 | } | |
1792 | ||
1793 | #if _GLIBCXX_USE_C99_COMPLEX_TR1 | |
1794 | inline __complex__ float | |
1795 | __complex_atanh(__complex__ float __z) | |
1796 | { return __builtin_catanhf(__z); } | |
1797 | ||
1798 | inline __complex__ double | |
1799 | __complex_atanh(__complex__ double __z) | |
1800 | { return __builtin_catanh(__z); } | |
1801 | ||
1802 | inline __complex__ long double | |
1803 | __complex_atanh(const __complex__ long double& __z) | |
1804 | { return __builtin_catanhl(__z); } | |
1805 | ||
1806 | template<typename _Tp> | |
1807 | inline std::complex<_Tp> | |
1808 | atanh(const std::complex<_Tp>& __z) | |
1809 | { return __complex_atanh(__z.__rep()); } | |
1810 | #else | |
1811 | /// atanh(__z) [8.1.7]. | |
1812 | // Effects: Behaves the same as C99 function catanh, defined | |
1813 | // in subclause 7.3.6.3. | |
1814 | template<typename _Tp> | |
1815 | inline std::complex<_Tp> | |
1816 | atanh(const std::complex<_Tp>& __z) | |
1817 | { return __complex_atanh(__z); } | |
1818 | #endif | |
1819 | ||
1820 | template<typename _Tp> | |
1821 | inline _Tp | |
1822 | /// fabs(__z) [8.1.8]. | |
1823 | // Effects: Behaves the same as C99 function cabs, defined | |
1824 | // in subclause 7.3.8.1. | |
1825 | fabs(const std::complex<_Tp>& __z) | |
1826 | { return std::abs(__z); } | |
1827 | ||
1828 | /// Additional overloads [8.1.9]. | |
1829 | template<typename _Tp> | |
1830 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1831 | arg(_Tp __x) | |
1832 | { | |
1833 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; | |
bdb62e6a | 1834 | #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) |
5022cf3f | 1835 | return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) |
1836 | : __type(); | |
1837 | #else | |
1838 | return std::arg(std::complex<__type>(__x)); | |
1839 | #endif | |
1840 | } | |
1841 | ||
1842 | template<typename _Tp> | |
1843 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1844 | imag(_Tp) | |
1845 | { return _Tp(); } | |
1846 | ||
1847 | template<typename _Tp> | |
1848 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1849 | norm(_Tp __x) | |
1850 | { | |
1851 | typedef typename __gnu_cxx::__promote<_Tp>::__type __type; | |
1852 | return __type(__x) * __type(__x); | |
1853 | } | |
1854 | ||
1855 | template<typename _Tp> | |
1856 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1857 | real(_Tp __x) | |
1858 | { return __x; } | |
1859 | ||
1860 | template<typename _Tp, typename _Up> | |
1861 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> | |
1862 | pow(const std::complex<_Tp>& __x, const _Up& __y) | |
1863 | { | |
1864 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; | |
1865 | return std::pow(std::complex<__type>(__x), __type(__y)); | |
1866 | } | |
1867 | ||
1868 | template<typename _Tp, typename _Up> | |
1869 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> | |
1870 | pow(const _Tp& __x, const std::complex<_Up>& __y) | |
1871 | { | |
1872 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; | |
1873 | return std::pow(__type(__x), std::complex<__type>(__y)); | |
1874 | } | |
1875 | ||
1876 | template<typename _Tp, typename _Up> | |
1877 | inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> | |
1878 | pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) | |
1879 | { | |
1880 | typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; | |
1881 | return std::pow(std::complex<__type>(__x), | |
1882 | std::complex<__type>(__y)); | |
1883 | } | |
1884 | ||
df2fcd57 | 1885 | // Forward declarations. |
1886 | // DR 781. | |
1887 | template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); | |
1888 | ||
1889 | template<typename _Tp> | |
1890 | std::complex<_Tp> | |
1891 | __complex_proj(const std::complex<_Tp>& __z) | |
1892 | { | |
1893 | const _Tp __den = (__z.real() * __z.real() | |
1894 | + __z.imag() * __z.imag() + _Tp(1.0)); | |
1895 | ||
1896 | return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, | |
1897 | (_Tp(2.0) * __z.imag()) / __den); | |
1898 | } | |
1899 | ||
1900 | #if _GLIBCXX_USE_C99_COMPLEX | |
1901 | inline __complex__ float | |
1902 | __complex_proj(__complex__ float __z) | |
1903 | { return __builtin_cprojf(__z); } | |
1904 | ||
1905 | inline __complex__ double | |
1906 | __complex_proj(__complex__ double __z) | |
1907 | { return __builtin_cproj(__z); } | |
1908 | ||
1909 | inline __complex__ long double | |
1910 | __complex_proj(const __complex__ long double& __z) | |
1911 | { return __builtin_cprojl(__z); } | |
1912 | ||
1913 | template<typename _Tp> | |
1914 | inline std::complex<_Tp> | |
1915 | proj(const std::complex<_Tp>& __z) | |
1916 | { return __complex_proj(__z.__rep()); } | |
1917 | #else | |
1918 | template<typename _Tp> | |
1919 | inline std::complex<_Tp> | |
1920 | proj(const std::complex<_Tp>& __z) | |
1921 | { return __complex_proj(__z); } | |
1922 | #endif | |
1923 | ||
64e9b516 | 1924 | // DR 1137. |
df2fcd57 | 1925 | template<typename _Tp> |
64e9b516 | 1926 | inline typename __gnu_cxx::__promote<_Tp>::__type |
df2fcd57 | 1927 | proj(_Tp __x) |
64e9b516 | 1928 | { return __x; } |
1929 | ||
1930 | template<typename _Tp> | |
1931 | inline typename __gnu_cxx::__promote<_Tp>::__type | |
1932 | conj(_Tp __x) | |
1933 | { return __x; } | |
df2fcd57 | 1934 | |
9d4f199c | 1935 | #if __cplusplus > 201103L |
1936 | ||
1937 | inline namespace literals { | |
1938 | inline namespace complex_literals { | |
1939 | ||
f6751ff2 | 1940 | #define __cpp_lib_complex_udls 201309 |
1941 | ||
9d4f199c | 1942 | constexpr std::complex<float> |
1943 | operator""if(long double __num) | |
1944 | { return std::complex<float>{0.0F, static_cast<float>(__num)}; } | |
1945 | ||
1946 | constexpr std::complex<float> | |
1947 | operator""if(unsigned long long __num) | |
1948 | { return std::complex<float>{0.0F, static_cast<float>(__num)}; } | |
1949 | ||
1950 | constexpr std::complex<double> | |
1951 | operator""i(long double __num) | |
1952 | { return std::complex<double>{0.0, static_cast<double>(__num)}; } | |
1953 | ||
1954 | constexpr std::complex<double> | |
1955 | operator""i(unsigned long long __num) | |
1956 | { return std::complex<double>{0.0, static_cast<double>(__num)}; } | |
1957 | ||
1958 | constexpr std::complex<long double> | |
1959 | operator""il(long double __num) | |
1960 | { return std::complex<long double>{0.0L, __num}; } | |
1961 | ||
1962 | constexpr std::complex<long double> | |
1963 | operator""il(unsigned long long __num) | |
1964 | { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } | |
1965 | ||
1966 | } // inline namespace complex_literals | |
1967 | } // inline namespace literals | |
1968 | ||
1969 | #endif // C++14 | |
1970 | ||
2948dd21 | 1971 | _GLIBCXX_END_NAMESPACE_VERSION |
1972 | } // namespace | |
df2fcd57 | 1973 | |
0c8766b1 | 1974 | #endif // C++11 |
bec9a462 | 1975 | |
5022cf3f | 1976 | #endif /* _GLIBCXX_COMPLEX */ |