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