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