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