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