libstdc++
complex
Go to the documentation of this file.
00001 // The template and inlines for the -*- C++ -*- complex number classes.
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2008, 2009, 2010
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file include/complex
00028  *  This is a Standard C++ Library header.
00029  */
00030 
00031 //
00032 // ISO C++ 14882: 26.2  Complex Numbers
00033 // Note: this is not a conforming implementation.
00034 // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
00035 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
00036 //
00037 
00038 #ifndef _GLIBCXX_COMPLEX
00039 #define _GLIBCXX_COMPLEX 1
00040 
00041 #pragma GCC system_header
00042 
00043 #include <bits/c++config.h>
00044 #include <bits/cpp_type_traits.h>
00045 #include <ext/type_traits.h>
00046 #include <cmath>
00047 #include <sstream>
00048 
00049 namespace std _GLIBCXX_VISIBILITY(default)
00050 {
00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00052 
00053   /**
00054    * @defgroup complex_numbers Complex Numbers
00055    * @ingroup numerics
00056    *
00057    * Classes and functions for complex numbers.
00058    * @{
00059    */
00060 
00061   // Forward declarations.
00062   template<typename _Tp> class complex;
00063   template<> class complex<float>;
00064   template<> class complex<double>;
00065   template<> class complex<long double>;
00066 
00067   ///  Return magnitude of @a z.
00068   template<typename _Tp> _Tp abs(const complex<_Tp>&);
00069   ///  Return phase angle of @a z.
00070   template<typename _Tp> _Tp arg(const complex<_Tp>&);
00071   ///  Return @a z magnitude squared.
00072   template<typename _Tp> _Tp norm(const complex<_Tp>&);
00073 
00074   ///  Return complex conjugate of @a z.
00075   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
00076   ///  Return complex with magnitude @a rho and angle @a theta.
00077   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
00078 
00079   // Transcendentals:
00080   /// Return complex cosine of @a z.
00081   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
00082   /// Return complex hyperbolic cosine of @a z.
00083   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
00084   /// Return complex base e exponential of @a z.
00085   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
00086   /// Return complex natural logarithm of @a z.
00087   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
00088   /// Return complex base 10 logarithm of @a z.
00089   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
00090 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00091   // DR 844.
00092   /// Return @a x to the @a y'th power.
00093   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
00094 #endif
00095   /// Return @a x to the @a y'th power.
00096   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
00097   /// Return @a x to the @a y'th power.
00098   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
00099                                           const complex<_Tp>&);
00100   /// Return @a x to the @a y'th power.
00101   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
00102   /// Return complex sine of @a z.
00103   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
00104   /// Return complex hyperbolic sine of @a z.
00105   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
00106   /// Return complex square root of @a z.
00107   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
00108   /// Return complex tangent of @a z.
00109   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
00110   /// Return complex hyperbolic tangent of @a z.
00111   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
00112     
00113     
00114   // 26.2.2  Primary template class complex
00115   /**
00116    *  Template to represent complex numbers.
00117    *
00118    *  Specializations for float, double, and long double are part of the
00119    *  library.  Results with any other type are not guaranteed.
00120    *
00121    *  @param  Tp  Type of real and imaginary values.
00122   */
00123   template<typename _Tp>
00124     struct complex
00125     {
00126       /// Value typedef.
00127       typedef _Tp value_type;
00128       
00129       ///  Default constructor.  First parameter is x, second parameter is y.
00130       ///  Unspecified parameters default to 0.
00131       _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
00132       : _M_real(__r), _M_imag(__i) { }
00133 
00134       // Lets the compiler synthesize the copy constructor   
00135       // complex (const complex<_Tp>&);
00136       ///  Copy constructor.
00137       template<typename _Up>
00138         _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
00139     : _M_real(__z.real()), _M_imag(__z.imag()) { }
00140 
00141 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00142       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00143       // DR 387. std::complex over-encapsulated.
00144       constexpr _Tp 
00145       real() const { return _M_real; }
00146 
00147       constexpr _Tp 
00148       imag() const { return _M_imag; }
00149 #else
00150       ///  Return real part of complex number.
00151       _Tp& 
00152       real() { return _M_real; }
00153 
00154       ///  Return real part of complex number.
00155       const _Tp& 
00156       real() const { return _M_real; }
00157 
00158       ///  Return imaginary part of complex number.
00159       _Tp& 
00160       imag() { return _M_imag; }
00161 
00162       ///  Return imaginary part of complex number.
00163       const _Tp& 
00164       imag() const { return _M_imag; }
00165 #endif
00166 
00167       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00168       // DR 387. std::complex over-encapsulated.
00169       void 
00170       real(_Tp __val) { _M_real = __val; }
00171 
00172       void 
00173       imag(_Tp __val) { _M_imag = __val; }
00174 
00175       /// Assign this complex number to scalar @a t.
00176       complex<_Tp>& operator=(const _Tp&);
00177       
00178       /// Add @a t to this complex number.
00179       // 26.2.5/1
00180       complex<_Tp>&
00181       operator+=(const _Tp& __t)
00182       {
00183     _M_real += __t;
00184     return *this;
00185       }
00186 
00187       /// Subtract @a t from this complex number.
00188       // 26.2.5/3
00189       complex<_Tp>&
00190       operator-=(const _Tp& __t)
00191       {
00192     _M_real -= __t;
00193     return *this;
00194       }
00195 
00196       /// Multiply this complex number by @a t.
00197       complex<_Tp>& operator*=(const _Tp&);
00198       /// Divide this complex number by @a t.
00199       complex<_Tp>& operator/=(const _Tp&);
00200 
00201       // Lets the compiler synthesize the
00202       // copy and assignment operator
00203       // complex<_Tp>& operator= (const complex<_Tp>&);
00204       /// Assign this complex number to complex @a z.
00205       template<typename _Up>
00206         complex<_Tp>& operator=(const complex<_Up>&);
00207       /// Add @a z to this complex number.
00208       template<typename _Up>
00209         complex<_Tp>& operator+=(const complex<_Up>&);
00210       /// Subtract @a z from this complex number.
00211       template<typename _Up>
00212         complex<_Tp>& operator-=(const complex<_Up>&);
00213       /// Multiply this complex number by @a z.
00214       template<typename _Up>
00215         complex<_Tp>& operator*=(const complex<_Up>&);
00216       /// Divide this complex number by @a z.
00217       template<typename _Up>
00218         complex<_Tp>& operator/=(const complex<_Up>&);
00219 
00220       _GLIBCXX_USE_CONSTEXPR complex __rep() const
00221       { return *this; }
00222 
00223     private:
00224       _Tp _M_real;
00225       _Tp _M_imag;
00226     };
00227 
00228   template<typename _Tp>
00229     complex<_Tp>&
00230     complex<_Tp>::operator=(const _Tp& __t)
00231     {
00232      _M_real = __t;
00233      _M_imag = _Tp();
00234      return *this;
00235     } 
00236 
00237   // 26.2.5/5
00238   template<typename _Tp>
00239     complex<_Tp>&
00240     complex<_Tp>::operator*=(const _Tp& __t)
00241     {
00242       _M_real *= __t;
00243       _M_imag *= __t;
00244       return *this;
00245     }
00246 
00247   // 26.2.5/7
00248   template<typename _Tp>
00249     complex<_Tp>&
00250     complex<_Tp>::operator/=(const _Tp& __t)
00251     {
00252       _M_real /= __t;
00253       _M_imag /= __t;
00254       return *this;
00255     }
00256 
00257   template<typename _Tp>
00258     template<typename _Up>
00259     complex<_Tp>&
00260     complex<_Tp>::operator=(const complex<_Up>& __z)
00261     {
00262       _M_real = __z.real();
00263       _M_imag = __z.imag();
00264       return *this;
00265     }
00266 
00267   // 26.2.5/9
00268   template<typename _Tp>
00269     template<typename _Up>
00270     complex<_Tp>&
00271     complex<_Tp>::operator+=(const complex<_Up>& __z)
00272     {
00273       _M_real += __z.real();
00274       _M_imag += __z.imag();
00275       return *this;
00276     }
00277 
00278   // 26.2.5/11
00279   template<typename _Tp>
00280     template<typename _Up>
00281     complex<_Tp>&
00282     complex<_Tp>::operator-=(const complex<_Up>& __z)
00283     {
00284       _M_real -= __z.real();
00285       _M_imag -= __z.imag();
00286       return *this;
00287     }
00288 
00289   // 26.2.5/13
00290   // XXX: This is a grammar school implementation.
00291   template<typename _Tp>
00292     template<typename _Up>
00293     complex<_Tp>&
00294     complex<_Tp>::operator*=(const complex<_Up>& __z)
00295     {
00296       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
00297       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
00298       _M_real = __r;
00299       return *this;
00300     }
00301 
00302   // 26.2.5/15
00303   // XXX: This is a grammar school implementation.
00304   template<typename _Tp>
00305     template<typename _Up>
00306     complex<_Tp>&
00307     complex<_Tp>::operator/=(const complex<_Up>& __z)
00308     {
00309       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
00310       const _Tp __n = std::norm(__z);
00311       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
00312       _M_real = __r / __n;
00313       return *this;
00314     }
00315     
00316   // Operators:
00317   //@{
00318   ///  Return new complex value @a x plus @a y.
00319   template<typename _Tp>
00320     inline complex<_Tp>
00321     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
00322     {
00323       complex<_Tp> __r = __x;
00324       __r += __y;
00325       return __r;
00326     }
00327 
00328   template<typename _Tp>
00329     inline complex<_Tp>
00330     operator+(const complex<_Tp>& __x, const _Tp& __y)
00331     {
00332       complex<_Tp> __r = __x;
00333       __r += __y;
00334       return __r;
00335     }
00336 
00337   template<typename _Tp>
00338     inline complex<_Tp>
00339     operator+(const _Tp& __x, const complex<_Tp>& __y)
00340     {
00341       complex<_Tp> __r = __y;
00342       __r += __x;
00343       return __r;
00344     }
00345   //@}
00346 
00347   //@{
00348   ///  Return new complex value @a x minus @a y.
00349   template<typename _Tp>
00350     inline complex<_Tp>
00351     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
00352     {
00353       complex<_Tp> __r = __x;
00354       __r -= __y;
00355       return __r;
00356     }
00357     
00358   template<typename _Tp>
00359     inline complex<_Tp>
00360     operator-(const complex<_Tp>& __x, const _Tp& __y)
00361     {
00362       complex<_Tp> __r = __x;
00363       __r -= __y;
00364       return __r;
00365     }
00366 
00367   template<typename _Tp>
00368     inline complex<_Tp>
00369     operator-(const _Tp& __x, const complex<_Tp>& __y)
00370     {
00371       complex<_Tp> __r(__x, -__y.imag());
00372       __r -= __y.real();
00373       return __r;
00374     }
00375   //@}
00376 
00377   //@{
00378   ///  Return new complex value @a x times @a y.
00379   template<typename _Tp>
00380     inline complex<_Tp>
00381     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
00382     {
00383       complex<_Tp> __r = __x;
00384       __r *= __y;
00385       return __r;
00386     }
00387 
00388   template<typename _Tp>
00389     inline complex<_Tp>
00390     operator*(const complex<_Tp>& __x, const _Tp& __y)
00391     {
00392       complex<_Tp> __r = __x;
00393       __r *= __y;
00394       return __r;
00395     }
00396 
00397   template<typename _Tp>
00398     inline complex<_Tp>
00399     operator*(const _Tp& __x, const complex<_Tp>& __y)
00400     {
00401       complex<_Tp> __r = __y;
00402       __r *= __x;
00403       return __r;
00404     }
00405   //@}
00406 
00407   //@{
00408   ///  Return new complex value @a x divided by @a y.
00409   template<typename _Tp>
00410     inline complex<_Tp>
00411     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
00412     {
00413       complex<_Tp> __r = __x;
00414       __r /= __y;
00415       return __r;
00416     }
00417     
00418   template<typename _Tp>
00419     inline complex<_Tp>
00420     operator/(const complex<_Tp>& __x, const _Tp& __y)
00421     {
00422       complex<_Tp> __r = __x;
00423       __r /= __y;
00424       return __r;
00425     }
00426 
00427   template<typename _Tp>
00428     inline complex<_Tp>
00429     operator/(const _Tp& __x, const complex<_Tp>& __y)
00430     {
00431       complex<_Tp> __r = __x;
00432       __r /= __y;
00433       return __r;
00434     }
00435   //@}
00436 
00437   ///  Return @a x.
00438   template<typename _Tp>
00439     inline complex<_Tp>
00440     operator+(const complex<_Tp>& __x)
00441     { return __x; }
00442 
00443   ///  Return complex negation of @a x.
00444   template<typename _Tp>
00445     inline complex<_Tp>
00446     operator-(const complex<_Tp>& __x)
00447     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
00448 
00449   //@{
00450   ///  Return true if @a x is equal to @a y.
00451   template<typename _Tp>
00452     inline _GLIBCXX_CONSTEXPR bool
00453     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
00454     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
00455 
00456   template<typename _Tp>
00457     inline _GLIBCXX_CONSTEXPR bool
00458     operator==(const complex<_Tp>& __x, const _Tp& __y)
00459     { return __x.real() == __y && __x.imag() == _Tp(); }
00460 
00461   template<typename _Tp>
00462     inline _GLIBCXX_CONSTEXPR bool
00463     operator==(const _Tp& __x, const complex<_Tp>& __y)
00464     { return __x == __y.real() && _Tp() == __y.imag(); }
00465   //@}
00466 
00467   //@{
00468   ///  Return false if @a x is equal to @a y.
00469   template<typename _Tp>
00470     inline _GLIBCXX_CONSTEXPR bool
00471     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
00472     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
00473 
00474   template<typename _Tp>
00475     inline _GLIBCXX_CONSTEXPR bool
00476     operator!=(const complex<_Tp>& __x, const _Tp& __y)
00477     { return __x.real() != __y || __x.imag() != _Tp(); }
00478 
00479   template<typename _Tp>
00480     inline _GLIBCXX_CONSTEXPR bool
00481     operator!=(const _Tp& __x, const complex<_Tp>& __y)
00482     { return __x != __y.real() || _Tp() != __y.imag(); }
00483   //@}
00484 
00485   ///  Extraction operator for complex values.
00486   template<typename _Tp, typename _CharT, class _Traits>
00487     basic_istream<_CharT, _Traits>&
00488     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
00489     {
00490       _Tp __re_x, __im_x;
00491       _CharT __ch;
00492       __is >> __ch;
00493       if (__ch == '(') 
00494     {
00495       __is >> __re_x >> __ch;
00496       if (__ch == ',') 
00497         {
00498           __is >> __im_x >> __ch;
00499           if (__ch == ')') 
00500         __x = complex<_Tp>(__re_x, __im_x);
00501           else
00502         __is.setstate(ios_base::failbit);
00503         }
00504       else if (__ch == ')') 
00505         __x = __re_x;
00506       else
00507         __is.setstate(ios_base::failbit);
00508     }
00509       else 
00510     {
00511       __is.putback(__ch);
00512       __is >> __re_x;
00513       __x = __re_x;
00514     }
00515       return __is;
00516     }
00517 
00518   ///  Insertion operator for complex values.
00519   template<typename _Tp, typename _CharT, class _Traits>
00520     basic_ostream<_CharT, _Traits>&
00521     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
00522     {
00523       basic_ostringstream<_CharT, _Traits> __s;
00524       __s.flags(__os.flags());
00525       __s.imbue(__os.getloc());
00526       __s.precision(__os.precision());
00527       __s << '(' << __x.real() << ',' << __x.imag() << ')';
00528       return __os << __s.str();
00529     }
00530 
00531   // Values
00532 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00533   template<typename _Tp>
00534     inline constexpr _Tp
00535     real(const complex<_Tp>& __z)
00536     { return __z.real(); }
00537     
00538   template<typename _Tp>
00539     inline constexpr _Tp
00540     imag(const complex<_Tp>& __z)
00541     { return __z.imag(); }
00542 #else
00543   template<typename _Tp>
00544     inline _Tp&
00545     real(complex<_Tp>& __z)
00546     { return __z.real(); }
00547     
00548   template<typename _Tp>
00549     inline const _Tp&
00550     real(const complex<_Tp>& __z)
00551     { return __z.real(); }
00552     
00553   template<typename _Tp>
00554     inline _Tp&
00555     imag(complex<_Tp>& __z)
00556     { return __z.imag(); }
00557     
00558   template<typename _Tp>
00559     inline const _Tp&
00560     imag(const complex<_Tp>& __z)
00561     { return __z.imag(); }
00562 #endif
00563 
00564   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
00565   template<typename _Tp>
00566     inline _Tp
00567     __complex_abs(const complex<_Tp>& __z)
00568     {
00569       _Tp __x = __z.real();
00570       _Tp __y = __z.imag();
00571       const _Tp __s = std::max(abs(__x), abs(__y));
00572       if (__s == _Tp())  // well ...
00573         return __s;
00574       __x /= __s; 
00575       __y /= __s;
00576       return __s * sqrt(__x * __x + __y * __y);
00577     }
00578 
00579 #if _GLIBCXX_USE_C99_COMPLEX
00580   inline float
00581   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
00582 
00583   inline double
00584   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
00585 
00586   inline long double
00587   __complex_abs(const __complex__ long double& __z)
00588   { return __builtin_cabsl(__z); }
00589 
00590   template<typename _Tp>
00591     inline _Tp
00592     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
00593 #else
00594   template<typename _Tp>
00595     inline _Tp
00596     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
00597 #endif  
00598 
00599 
00600   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
00601   template<typename _Tp>
00602     inline _Tp
00603     __complex_arg(const complex<_Tp>& __z)
00604     { return  atan2(__z.imag(), __z.real()); }
00605 
00606 #if _GLIBCXX_USE_C99_COMPLEX
00607   inline float
00608   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
00609 
00610   inline double
00611   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
00612 
00613   inline long double
00614   __complex_arg(const __complex__ long double& __z)
00615   { return __builtin_cargl(__z); }
00616 
00617   template<typename _Tp>
00618     inline _Tp
00619     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
00620 #else
00621   template<typename _Tp>
00622     inline _Tp
00623     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
00624 #endif
00625 
00626   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
00627   //     As defined, norm() is -not- a norm is the common mathematical
00628   //     sens used in numerics.  The helper class _Norm_helper<> tries to
00629   //     distinguish between builtin floating point and the rest, so as
00630   //     to deliver an answer as close as possible to the real value.
00631   template<bool>
00632     struct _Norm_helper
00633     {
00634       template<typename _Tp>
00635         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00636         {
00637           const _Tp __x = __z.real();
00638           const _Tp __y = __z.imag();
00639           return __x * __x + __y * __y;
00640         }
00641     };
00642 
00643   template<>
00644     struct _Norm_helper<true>
00645     {
00646       template<typename _Tp>
00647         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00648         {
00649           _Tp __res = std::abs(__z);
00650           return __res * __res;
00651         }
00652     };
00653   
00654   template<typename _Tp>
00655     inline _Tp
00656     norm(const complex<_Tp>& __z)
00657     {
00658       return _Norm_helper<__is_floating<_Tp>::__value 
00659     && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
00660     }
00661 
00662   template<typename _Tp>
00663     inline complex<_Tp>
00664     polar(const _Tp& __rho, const _Tp& __theta)
00665     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
00666 
00667   template<typename _Tp>
00668     inline complex<_Tp>
00669     conj(const complex<_Tp>& __z)
00670     { return complex<_Tp>(__z.real(), -__z.imag()); }
00671   
00672   // Transcendentals
00673 
00674   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
00675   template<typename _Tp>
00676     inline complex<_Tp>
00677     __complex_cos(const complex<_Tp>& __z)
00678     {
00679       const _Tp __x = __z.real();
00680       const _Tp __y = __z.imag();
00681       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
00682     }
00683 
00684 #if _GLIBCXX_USE_C99_COMPLEX
00685   inline __complex__ float
00686   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
00687 
00688   inline __complex__ double
00689   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
00690 
00691   inline __complex__ long double
00692   __complex_cos(const __complex__ long double& __z)
00693   { return __builtin_ccosl(__z); }
00694 
00695   template<typename _Tp>
00696     inline complex<_Tp>
00697     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
00698 #else
00699   template<typename _Tp>
00700     inline complex<_Tp>
00701     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
00702 #endif
00703 
00704   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
00705   template<typename _Tp>
00706     inline complex<_Tp>
00707     __complex_cosh(const complex<_Tp>& __z)
00708     {
00709       const _Tp __x = __z.real();
00710       const _Tp __y = __z.imag();
00711       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
00712     }
00713 
00714 #if _GLIBCXX_USE_C99_COMPLEX
00715   inline __complex__ float
00716   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
00717 
00718   inline __complex__ double
00719   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
00720 
00721   inline __complex__ long double
00722   __complex_cosh(const __complex__ long double& __z)
00723   { return __builtin_ccoshl(__z); }
00724 
00725   template<typename _Tp>
00726     inline complex<_Tp>
00727     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
00728 #else
00729   template<typename _Tp>
00730     inline complex<_Tp>
00731     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
00732 #endif
00733 
00734   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
00735   template<typename _Tp>
00736     inline complex<_Tp>
00737     __complex_exp(const complex<_Tp>& __z)
00738     { return std::polar(exp(__z.real()), __z.imag()); }
00739 
00740 #if _GLIBCXX_USE_C99_COMPLEX
00741   inline __complex__ float
00742   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
00743 
00744   inline __complex__ double
00745   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
00746 
00747   inline __complex__ long double
00748   __complex_exp(const __complex__ long double& __z)
00749   { return __builtin_cexpl(__z); }
00750 
00751   template<typename _Tp>
00752     inline complex<_Tp>
00753     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
00754 #else
00755   template<typename _Tp>
00756     inline complex<_Tp>
00757     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
00758 #endif
00759 
00760   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
00761   //                    The branch cut is along the negative axis.
00762   template<typename _Tp>
00763     inline complex<_Tp>
00764     __complex_log(const complex<_Tp>& __z)
00765     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
00766 
00767 #if _GLIBCXX_USE_C99_COMPLEX
00768   inline __complex__ float
00769   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
00770 
00771   inline __complex__ double
00772   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
00773 
00774   inline __complex__ long double
00775   __complex_log(const __complex__ long double& __z)
00776   { return __builtin_clogl(__z); }
00777 
00778   template<typename _Tp>
00779     inline complex<_Tp>
00780     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
00781 #else
00782   template<typename _Tp>
00783     inline complex<_Tp>
00784     log(const complex<_Tp>& __z) { return __complex_log(__z); }
00785 #endif
00786 
00787   template<typename _Tp>
00788     inline complex<_Tp>
00789     log10(const complex<_Tp>& __z)
00790     { return std::log(__z) / log(_Tp(10.0)); }
00791 
00792   // 26.2.8/10 sin(__z): Returns the sine of __z.
00793   template<typename _Tp>
00794     inline complex<_Tp>
00795     __complex_sin(const complex<_Tp>& __z)
00796     {
00797       const _Tp __x = __z.real();
00798       const _Tp __y = __z.imag();
00799       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
00800     }
00801 
00802 #if _GLIBCXX_USE_C99_COMPLEX
00803   inline __complex__ float
00804   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
00805 
00806   inline __complex__ double
00807   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
00808 
00809   inline __complex__ long double
00810   __complex_sin(const __complex__ long double& __z)
00811   { return __builtin_csinl(__z); }
00812 
00813   template<typename _Tp>
00814     inline complex<_Tp>
00815     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
00816 #else
00817   template<typename _Tp>
00818     inline complex<_Tp>
00819     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
00820 #endif
00821 
00822   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
00823   template<typename _Tp>
00824     inline complex<_Tp>
00825     __complex_sinh(const complex<_Tp>& __z)
00826     {
00827       const _Tp __x = __z.real();
00828       const _Tp  __y = __z.imag();
00829       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
00830     }
00831 
00832 #if _GLIBCXX_USE_C99_COMPLEX
00833   inline __complex__ float
00834   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
00835 
00836   inline __complex__ double
00837   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
00838 
00839   inline __complex__ long double
00840   __complex_sinh(const __complex__ long double& __z)
00841   { return __builtin_csinhl(__z); }      
00842 
00843   template<typename _Tp>
00844     inline complex<_Tp>
00845     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
00846 #else
00847   template<typename _Tp>
00848     inline complex<_Tp>
00849     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
00850 #endif
00851 
00852   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
00853   //                     The branch cut is on the negative axis.
00854   template<typename _Tp>
00855     complex<_Tp>
00856     __complex_sqrt(const complex<_Tp>& __z)
00857     {
00858       _Tp __x = __z.real();
00859       _Tp __y = __z.imag();
00860 
00861       if (__x == _Tp())
00862         {
00863           _Tp __t = sqrt(abs(__y) / 2);
00864           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
00865         }
00866       else
00867         {
00868           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
00869           _Tp __u = __t / 2;
00870           return __x > _Tp()
00871             ? complex<_Tp>(__u, __y / __t)
00872             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
00873         }
00874     }
00875 
00876 #if _GLIBCXX_USE_C99_COMPLEX
00877   inline __complex__ float
00878   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
00879 
00880   inline __complex__ double
00881   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
00882 
00883   inline __complex__ long double
00884   __complex_sqrt(const __complex__ long double& __z)
00885   { return __builtin_csqrtl(__z); }
00886 
00887   template<typename _Tp>
00888     inline complex<_Tp>
00889     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
00890 #else
00891   template<typename _Tp>
00892     inline complex<_Tp>
00893     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
00894 #endif
00895 
00896   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
00897   
00898   template<typename _Tp>
00899     inline complex<_Tp>
00900     __complex_tan(const complex<_Tp>& __z)
00901     { return std::sin(__z) / std::cos(__z); }
00902 
00903 #if _GLIBCXX_USE_C99_COMPLEX
00904   inline __complex__ float
00905   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
00906 
00907   inline __complex__ double
00908   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
00909 
00910   inline __complex__ long double
00911   __complex_tan(const __complex__ long double& __z)
00912   { return __builtin_ctanl(__z); }
00913 
00914   template<typename _Tp>
00915     inline complex<_Tp>
00916     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
00917 #else
00918   template<typename _Tp>
00919     inline complex<_Tp>
00920     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
00921 #endif
00922 
00923 
00924   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
00925   
00926   template<typename _Tp>
00927     inline complex<_Tp>
00928     __complex_tanh(const complex<_Tp>& __z)
00929     { return std::sinh(__z) / std::cosh(__z); }
00930 
00931 #if _GLIBCXX_USE_C99_COMPLEX
00932   inline __complex__ float
00933   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
00934 
00935   inline __complex__ double
00936   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
00937 
00938   inline __complex__ long double
00939   __complex_tanh(const __complex__ long double& __z)
00940   { return __builtin_ctanhl(__z); }
00941 
00942   template<typename _Tp>
00943     inline complex<_Tp>
00944     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
00945 #else
00946   template<typename _Tp>
00947     inline complex<_Tp>
00948     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
00949 #endif
00950 
00951 
00952   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
00953   //                          raised to the __y-th power.  The branch
00954   //                          cut is on the negative axis.
00955 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00956   template<typename _Tp>
00957     complex<_Tp>
00958     __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
00959     {
00960       complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
00961 
00962       while (__n >>= 1)
00963         {
00964           __x *= __x;
00965           if (__n % 2)
00966             __y *= __x;
00967         }
00968 
00969       return __y;
00970     }
00971 
00972   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00973   // DR 844. complex pow return type is ambiguous.
00974   template<typename _Tp>
00975     inline complex<_Tp>
00976     pow(const complex<_Tp>& __z, int __n)
00977     {
00978       return __n < 0
00979         ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -__n)
00980         : std::__complex_pow_unsigned(__z, __n);
00981     }
00982 #endif
00983 
00984   template<typename _Tp>
00985     complex<_Tp>
00986     pow(const complex<_Tp>& __x, const _Tp& __y)
00987     {
00988 #ifndef _GLIBCXX_USE_C99_COMPLEX
00989       if (__x == _Tp())
00990     return _Tp();
00991 #endif
00992       if (__x.imag() == _Tp() && __x.real() > _Tp())
00993         return pow(__x.real(), __y);
00994 
00995       complex<_Tp> __t = std::log(__x);
00996       return std::polar(exp(__y * __t.real()), __y * __t.imag());
00997     }
00998 
00999   template<typename _Tp>
01000     inline complex<_Tp>
01001     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01002     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
01003 
01004 #if _GLIBCXX_USE_C99_COMPLEX
01005   inline __complex__ float
01006   __complex_pow(__complex__ float __x, __complex__ float __y)
01007   { return __builtin_cpowf(__x, __y); }
01008 
01009   inline __complex__ double
01010   __complex_pow(__complex__ double __x, __complex__ double __y)
01011   { return __builtin_cpow(__x, __y); }
01012 
01013   inline __complex__ long double
01014   __complex_pow(const __complex__ long double& __x,
01015         const __complex__ long double& __y)
01016   { return __builtin_cpowl(__x, __y); }
01017 
01018   template<typename _Tp>
01019     inline complex<_Tp>
01020     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01021     { return __complex_pow(__x.__rep(), __y.__rep()); }
01022 #else
01023   template<typename _Tp>
01024     inline complex<_Tp>
01025     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01026     { return __complex_pow(__x, __y); }
01027 #endif
01028 
01029   template<typename _Tp>
01030     inline complex<_Tp>
01031     pow(const _Tp& __x, const complex<_Tp>& __y)
01032     {
01033       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
01034                       __y.imag() * log(__x))
01035                      : std::pow(complex<_Tp>(__x), __y);
01036     }
01037 
01038   // 26.2.3  complex specializations
01039   // complex<float> specialization
01040   template<>
01041     struct complex<float>
01042     {
01043       typedef float value_type;
01044       typedef __complex__ float _ComplexT;
01045 
01046       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
01047 
01048       _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
01049 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01050       // The list-initialization extension to __complex__ types is
01051       // not available in GCC 4.6.  Thus libstdc++/48760 cannot be
01052       // fixed in C++0x mode, unfortunately.
01053       : _M_value(__r + __i * 1.0fi) { }
01054 #else
01055       {
01056     __real__ _M_value = __r;
01057     __imag__ _M_value = __i;
01058       }
01059 #endif
01060 
01061       explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
01062       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 
01063 
01064 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01065       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01066       // DR 387. std::complex over-encapsulated.
01067       constexpr float 
01068       real() const { return __real__ _M_value; }
01069 
01070       constexpr float 
01071       imag() const { return __imag__ _M_value; }
01072 #else
01073       float& 
01074       real() { return __real__ _M_value; }
01075 
01076       const float& 
01077       real() const { return __real__ _M_value; }      
01078 
01079       float& 
01080       imag() { return __imag__ _M_value; }
01081 
01082       const float& 
01083       imag() const { return __imag__ _M_value; }
01084 #endif
01085 
01086       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01087       // DR 387. std::complex over-encapsulated.
01088       void 
01089       real(float __val) { __real__ _M_value = __val; }
01090 
01091       void 
01092       imag(float __val) { __imag__ _M_value = __val; }
01093 
01094       complex&
01095       operator=(float __f)
01096       {
01097     _M_value = __f;
01098     return *this;
01099       }
01100 
01101       complex&
01102       operator+=(float __f)
01103       {
01104     _M_value += __f;
01105     return *this;
01106       }
01107 
01108       complex&
01109       operator-=(float __f)
01110       {
01111     _M_value -= __f;
01112     return *this;
01113       }
01114 
01115       complex&
01116       operator*=(float __f)
01117       {
01118     _M_value *= __f;
01119     return *this;
01120       }
01121 
01122       complex&
01123       operator/=(float __f)
01124       {
01125     _M_value /= __f;
01126     return *this;
01127       }
01128 
01129       // Let the compiler synthesize the copy and assignment
01130       // operator.  It always does a pretty good job.
01131       // complex& operator=(const complex&);
01132 
01133       template<typename _Tp>
01134         complex&
01135         operator=(const complex<_Tp>&  __z)
01136     {
01137       __real__ _M_value = __z.real();
01138       __imag__ _M_value = __z.imag();
01139       return *this;
01140     }
01141 
01142       template<typename _Tp>
01143         complex&
01144         operator+=(const complex<_Tp>& __z)
01145     {
01146       __real__ _M_value += __z.real();
01147       __imag__ _M_value += __z.imag();
01148       return *this;
01149     }
01150 
01151       template<class _Tp>
01152         complex&
01153         operator-=(const complex<_Tp>& __z)
01154     {
01155       __real__ _M_value -= __z.real();
01156       __imag__ _M_value -= __z.imag();
01157       return *this;
01158     }
01159 
01160       template<class _Tp>
01161         complex&
01162         operator*=(const complex<_Tp>& __z)
01163     {
01164       _ComplexT __t;
01165       __real__ __t = __z.real();
01166       __imag__ __t = __z.imag();
01167       _M_value *= __t;
01168       return *this;
01169     }
01170 
01171       template<class _Tp>
01172         complex&
01173         operator/=(const complex<_Tp>& __z)
01174     {
01175       _ComplexT __t;
01176       __real__ __t = __z.real();
01177       __imag__ __t = __z.imag();
01178       _M_value /= __t;
01179       return *this;
01180     }
01181 
01182       _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
01183 
01184     private:
01185       _ComplexT _M_value;
01186     };
01187 
01188   // 26.2.3  complex specializations
01189   // complex<double> specialization
01190   template<>
01191     struct complex<double>
01192     {
01193       typedef double value_type;
01194       typedef __complex__ double _ComplexT;
01195 
01196       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
01197 
01198       _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
01199 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01200       // The list-initialization extension to __complex__ types is
01201       // not available in GCC 4.6.  Thus libstdc++/48760 cannot be
01202       // fixed in C++0x mode, unfortunately.
01203       : _M_value(__r + __i * 1.0i) { }
01204 #else
01205       {
01206     __real__ _M_value = __r;
01207     __imag__ _M_value = __i;
01208       }
01209 #endif
01210 
01211       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
01212       : _M_value(__z.__rep()) { }
01213 
01214       explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 
01215 
01216 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01217       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01218       // DR 387. std::complex over-encapsulated.
01219       constexpr double 
01220       real() const { return __real__ _M_value; }
01221 
01222       constexpr double 
01223       imag() const { return __imag__ _M_value; }
01224 #else
01225       double& 
01226       real() { return __real__ _M_value; }
01227 
01228       const double& 
01229       real() const { return __real__ _M_value; }
01230 
01231       double& 
01232       imag() { return __imag__ _M_value; }
01233 
01234       const double& 
01235       imag() const { return __imag__ _M_value; }
01236 #endif
01237 
01238       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01239       // DR 387. std::complex over-encapsulated.
01240       void 
01241       real(double __val) { __real__ _M_value = __val; }
01242 
01243       void 
01244       imag(double __val) { __imag__ _M_value = __val; }
01245 
01246       complex&
01247       operator=(double __d)
01248       {
01249     _M_value = __d;
01250     return *this;
01251       }
01252 
01253       complex&
01254       operator+=(double __d)
01255       {
01256     _M_value += __d;
01257     return *this;
01258       }
01259     
01260       complex&
01261       operator-=(double __d)
01262       {
01263     _M_value -= __d;
01264     return *this;
01265       }
01266 
01267       complex&
01268       operator*=(double __d)
01269       {
01270     _M_value *= __d;
01271     return *this;
01272       }
01273 
01274       complex&
01275       operator/=(double __d)
01276       {
01277     _M_value /= __d;
01278     return *this;
01279       }
01280 
01281       // The compiler will synthesize this, efficiently.
01282       // complex& operator=(const complex&);
01283 
01284       template<typename _Tp>
01285         complex&
01286         operator=(const complex<_Tp>& __z)
01287     {
01288       __real__ _M_value = __z.real();
01289       __imag__ _M_value = __z.imag();
01290       return *this;
01291     }
01292 
01293       template<typename _Tp>
01294         complex&
01295         operator+=(const complex<_Tp>& __z)
01296     {
01297       __real__ _M_value += __z.real();
01298       __imag__ _M_value += __z.imag();
01299       return *this;
01300     }
01301 
01302       template<typename _Tp>
01303         complex&
01304         operator-=(const complex<_Tp>& __z)
01305     {
01306       __real__ _M_value -= __z.real();
01307       __imag__ _M_value -= __z.imag();
01308       return *this;
01309     }
01310 
01311       template<typename _Tp>
01312         complex&
01313         operator*=(const complex<_Tp>& __z)
01314     {
01315       _ComplexT __t;
01316       __real__ __t = __z.real();
01317       __imag__ __t = __z.imag();
01318       _M_value *= __t;
01319       return *this;
01320     }
01321 
01322       template<typename _Tp>
01323         complex&
01324         operator/=(const complex<_Tp>& __z)
01325     {
01326       _ComplexT __t;
01327       __real__ __t = __z.real();
01328       __imag__ __t = __z.imag();
01329       _M_value /= __t;
01330       return *this;
01331     }
01332 
01333       _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
01334 
01335     private:
01336       _ComplexT _M_value;
01337     };
01338 
01339   // 26.2.3  complex specializations
01340   // complex<long double> specialization
01341   template<>
01342     struct complex<long double>
01343     {
01344       typedef long double value_type;
01345       typedef __complex__ long double _ComplexT;
01346 
01347       _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
01348 
01349       _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 
01350                  long double __i = 0.0L)
01351 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01352       // The list-initialization extension to __complex__ types is
01353       // not available in GCC 4.6.  Thus libstdc++/48760 cannot be
01354       // fixed in C++0x mode, unfortunately.
01355       : _M_value(__r + __i * 1.0Li) { }
01356 #else
01357       {
01358     __real__ _M_value = __r;
01359     __imag__ _M_value = __i;
01360       }
01361 #endif
01362 
01363       _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
01364       : _M_value(__z.__rep()) { }
01365 
01366       _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
01367       : _M_value(__z.__rep()) { }
01368 
01369 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01370       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01371       // DR 387. std::complex over-encapsulated.
01372       constexpr long double 
01373       real() const { return __real__ _M_value; }
01374 
01375       constexpr long double 
01376       imag() const { return __imag__ _M_value; }
01377 #else
01378       long double& 
01379       real() { return __real__ _M_value; }
01380 
01381       const long double& 
01382       real() const { return __real__ _M_value; }
01383 
01384       long double& 
01385       imag() { return __imag__ _M_value; }
01386 
01387       const long double& 
01388       imag() const { return __imag__ _M_value; }
01389 #endif
01390 
01391       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01392       // DR 387. std::complex over-encapsulated.
01393       void 
01394       real(long double __val) { __real__ _M_value = __val; }
01395 
01396       void 
01397       imag(long double __val) { __imag__ _M_value = __val; }
01398 
01399       complex&
01400       operator=(long double __r)
01401       {
01402     _M_value = __r;
01403     return *this;
01404       }
01405 
01406       complex&
01407       operator+=(long double __r)
01408       {
01409     _M_value += __r;
01410     return *this;
01411       }
01412 
01413       complex&
01414       operator-=(long double __r)
01415       {
01416     _M_value -= __r;
01417     return *this;
01418       }
01419 
01420       complex&
01421       operator*=(long double __r)
01422       {
01423     _M_value *= __r;
01424     return *this;
01425       }
01426 
01427       complex&
01428       operator/=(long double __r)
01429       {
01430     _M_value /= __r;
01431     return *this;
01432       }
01433 
01434       // The compiler knows how to do this efficiently
01435       // complex& operator=(const complex&);
01436 
01437       template<typename _Tp>
01438         complex&
01439         operator=(const complex<_Tp>& __z)
01440     {
01441       __real__ _M_value = __z.real();
01442       __imag__ _M_value = __z.imag();
01443       return *this;
01444     }
01445 
01446       template<typename _Tp>
01447         complex&
01448     operator+=(const complex<_Tp>& __z)
01449     {
01450       __real__ _M_value += __z.real();
01451       __imag__ _M_value += __z.imag();
01452       return *this;
01453     }
01454 
01455       template<typename _Tp>
01456         complex&
01457     operator-=(const complex<_Tp>& __z)
01458     {
01459       __real__ _M_value -= __z.real();
01460       __imag__ _M_value -= __z.imag();
01461       return *this;
01462     }
01463 
01464       template<typename _Tp>
01465         complex&
01466     operator*=(const complex<_Tp>& __z)
01467     {
01468       _ComplexT __t;
01469       __real__ __t = __z.real();
01470       __imag__ __t = __z.imag();
01471       _M_value *= __t;
01472       return *this;
01473     }
01474 
01475       template<typename _Tp>
01476         complex&
01477     operator/=(const complex<_Tp>& __z)
01478     {
01479       _ComplexT __t;
01480       __real__ __t = __z.real();
01481       __imag__ __t = __z.imag();
01482       _M_value /= __t;
01483       return *this;
01484     }
01485 
01486       _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; }
01487 
01488     private:
01489       _ComplexT _M_value;
01490     };
01491 
01492   // These bits have to be at the end of this file, so that the
01493   // specializations have all been defined.
01494   inline _GLIBCXX_CONSTEXPR
01495   complex<float>::complex(const complex<double>& __z)
01496   : _M_value(__z.__rep()) { }
01497 
01498   inline _GLIBCXX_CONSTEXPR
01499   complex<float>::complex(const complex<long double>& __z)
01500   : _M_value(__z.__rep()) { }
01501 
01502   inline _GLIBCXX_CONSTEXPR
01503   complex<double>::complex(const complex<long double>& __z)
01504   : _M_value(__z.__rep()) { }
01505 
01506   // Inhibit implicit instantiations for required instantiations,
01507   // which are defined via explicit instantiations elsewhere.
01508   // NB:  This syntax is a GNU extension.
01509 #if _GLIBCXX_EXTERN_TEMPLATE
01510   extern template istream& operator>>(istream&, complex<float>&);
01511   extern template ostream& operator<<(ostream&, const complex<float>&);
01512   extern template istream& operator>>(istream&, complex<double>&);
01513   extern template ostream& operator<<(ostream&, const complex<double>&);
01514   extern template istream& operator>>(istream&, complex<long double>&);
01515   extern template ostream& operator<<(ostream&, const complex<long double>&);
01516 
01517 #ifdef _GLIBCXX_USE_WCHAR_T
01518   extern template wistream& operator>>(wistream&, complex<float>&);
01519   extern template wostream& operator<<(wostream&, const complex<float>&);
01520   extern template wistream& operator>>(wistream&, complex<double>&);
01521   extern template wostream& operator<<(wostream&, const complex<double>&);
01522   extern template wistream& operator>>(wistream&, complex<long double>&);
01523   extern template wostream& operator<<(wostream&, const complex<long double>&);
01524 #endif
01525 #endif
01526 
01527   // @} group complex_numbers
01528 
01529 _GLIBCXX_END_NAMESPACE_VERSION
01530 } // namespace
01531 
01532 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
01533 {
01534 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01535 
01536   // See ext/type_traits.h for the primary template.
01537   template<typename _Tp, typename _Up>
01538     struct __promote_2<std::complex<_Tp>, _Up>
01539     {
01540     public:
01541       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01542     };
01543 
01544   template<typename _Tp, typename _Up>
01545     struct __promote_2<_Tp, std::complex<_Up> >
01546     {
01547     public:
01548       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01549     };
01550   
01551   template<typename _Tp, typename _Up>
01552     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
01553     {
01554     public:
01555       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01556     };
01557 
01558 _GLIBCXX_END_NAMESPACE_VERSION
01559 } // namespace
01560 
01561 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01562 
01563 namespace std _GLIBCXX_VISIBILITY(default)
01564 {
01565 _GLIBCXX_BEGIN_NAMESPACE_VERSION
01566 
01567   // Forward declarations.
01568   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
01569   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
01570   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
01571 
01572   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
01573   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
01574   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
01575   // DR 595.
01576   template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
01577 
01578   template<typename _Tp>
01579     inline std::complex<_Tp>
01580     __complex_acos(const std::complex<_Tp>& __z)
01581     {
01582       const std::complex<_Tp> __t = std::asin(__z);
01583       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
01584       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
01585     }
01586 
01587 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01588   inline __complex__ float
01589   __complex_acos(__complex__ float __z)
01590   { return __builtin_cacosf(__z); }
01591 
01592   inline __complex__ double
01593   __complex_acos(__complex__ double __z)
01594   { return __builtin_cacos(__z); }
01595 
01596   inline __complex__ long double
01597   __complex_acos(const __complex__ long double& __z)
01598   { return __builtin_cacosl(__z); }
01599 
01600   template<typename _Tp>
01601     inline std::complex<_Tp>
01602     acos(const std::complex<_Tp>& __z)
01603     { return __complex_acos(__z.__rep()); }
01604 #else
01605   /// acos(__z) [8.1.2].
01606   //  Effects:  Behaves the same as C99 function cacos, defined
01607   //            in subclause 7.3.5.1.
01608   template<typename _Tp>
01609     inline std::complex<_Tp>
01610     acos(const std::complex<_Tp>& __z)
01611     { return __complex_acos(__z); }
01612 #endif
01613 
01614   template<typename _Tp>
01615     inline std::complex<_Tp>
01616     __complex_asin(const std::complex<_Tp>& __z)
01617     {
01618       std::complex<_Tp> __t(-__z.imag(), __z.real());
01619       __t = std::asinh(__t);
01620       return std::complex<_Tp>(__t.imag(), -__t.real());
01621     }
01622 
01623 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01624   inline __complex__ float
01625   __complex_asin(__complex__ float __z)
01626   { return __builtin_casinf(__z); }
01627 
01628   inline __complex__ double
01629   __complex_asin(__complex__ double __z)
01630   { return __builtin_casin(__z); }
01631 
01632   inline __complex__ long double
01633   __complex_asin(const __complex__ long double& __z)
01634   { return __builtin_casinl(__z); }
01635 
01636   template<typename _Tp>
01637     inline std::complex<_Tp>
01638     asin(const std::complex<_Tp>& __z)
01639     { return __complex_asin(__z.__rep()); }
01640 #else
01641   /// asin(__z) [8.1.3].
01642   //  Effects:  Behaves the same as C99 function casin, defined
01643   //            in subclause 7.3.5.2.
01644   template<typename _Tp>
01645     inline std::complex<_Tp>
01646     asin(const std::complex<_Tp>& __z)
01647     { return __complex_asin(__z); }
01648 #endif
01649   
01650   template<typename _Tp>
01651     std::complex<_Tp>
01652     __complex_atan(const std::complex<_Tp>& __z)
01653     {
01654       const _Tp __r2 = __z.real() * __z.real();
01655       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
01656 
01657       _Tp __num = __z.imag() + _Tp(1.0);
01658       _Tp __den = __z.imag() - _Tp(1.0);
01659 
01660       __num = __r2 + __num * __num;
01661       __den = __r2 + __den * __den;
01662 
01663       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
01664                    _Tp(0.25) * log(__num / __den));
01665     }
01666 
01667 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01668   inline __complex__ float
01669   __complex_atan(__complex__ float __z)
01670   { return __builtin_catanf(__z); }
01671 
01672   inline __complex__ double
01673   __complex_atan(__complex__ double __z)
01674   { return __builtin_catan(__z); }
01675 
01676   inline __complex__ long double
01677   __complex_atan(const __complex__ long double& __z)
01678   { return __builtin_catanl(__z); }
01679 
01680   template<typename _Tp>
01681     inline std::complex<_Tp>
01682     atan(const std::complex<_Tp>& __z)
01683     { return __complex_atan(__z.__rep()); }
01684 #else
01685   /// atan(__z) [8.1.4].
01686   //  Effects:  Behaves the same as C99 function catan, defined
01687   //            in subclause 7.3.5.3.
01688   template<typename _Tp>
01689     inline std::complex<_Tp>
01690     atan(const std::complex<_Tp>& __z)
01691     { return __complex_atan(__z); }
01692 #endif
01693 
01694   template<typename _Tp>
01695     std::complex<_Tp>
01696     __complex_acosh(const std::complex<_Tp>& __z)
01697     {
01698       std::complex<_Tp> __t((__z.real() - __z.imag())
01699                 * (__z.real() + __z.imag()) - _Tp(1.0),
01700                 _Tp(2.0) * __z.real() * __z.imag());
01701       __t = std::sqrt(__t);
01702 
01703       return std::log(__t + __z);
01704     }
01705 
01706 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01707   inline __complex__ float
01708   __complex_acosh(__complex__ float __z)
01709   { return __builtin_cacoshf(__z); }
01710 
01711   inline __complex__ double
01712   __complex_acosh(__complex__ double __z)
01713   { return __builtin_cacosh(__z); }
01714 
01715   inline __complex__ long double
01716   __complex_acosh(const __complex__ long double& __z)
01717   { return __builtin_cacoshl(__z); }
01718 
01719   template<typename _Tp>
01720     inline std::complex<_Tp>
01721     acosh(const std::complex<_Tp>& __z)
01722     { return __complex_acosh(__z.__rep()); }
01723 #else
01724   /// acosh(__z) [8.1.5].
01725   //  Effects:  Behaves the same as C99 function cacosh, defined
01726   //            in subclause 7.3.6.1.
01727   template<typename _Tp>
01728     inline std::complex<_Tp>
01729     acosh(const std::complex<_Tp>& __z)
01730     { return __complex_acosh(__z); }
01731 #endif
01732 
01733   template<typename _Tp>
01734     std::complex<_Tp>
01735     __complex_asinh(const std::complex<_Tp>& __z)
01736     {
01737       std::complex<_Tp> __t((__z.real() - __z.imag())
01738                 * (__z.real() + __z.imag()) + _Tp(1.0),
01739                 _Tp(2.0) * __z.real() * __z.imag());
01740       __t = std::sqrt(__t);
01741 
01742       return std::log(__t + __z);
01743     }
01744 
01745 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01746   inline __complex__ float
01747   __complex_asinh(__complex__ float __z)
01748   { return __builtin_casinhf(__z); }
01749 
01750   inline __complex__ double
01751   __complex_asinh(__complex__ double __z)
01752   { return __builtin_casinh(__z); }
01753 
01754   inline __complex__ long double
01755   __complex_asinh(const __complex__ long double& __z)
01756   { return __builtin_casinhl(__z); }
01757 
01758   template<typename _Tp>
01759     inline std::complex<_Tp>
01760     asinh(const std::complex<_Tp>& __z)
01761     { return __complex_asinh(__z.__rep()); }
01762 #else
01763   /// asinh(__z) [8.1.6].
01764   //  Effects:  Behaves the same as C99 function casin, defined
01765   //            in subclause 7.3.6.2.
01766   template<typename _Tp>
01767     inline std::complex<_Tp>
01768     asinh(const std::complex<_Tp>& __z)
01769     { return __complex_asinh(__z); }
01770 #endif
01771 
01772   template<typename _Tp>
01773     std::complex<_Tp>
01774     __complex_atanh(const std::complex<_Tp>& __z)
01775     {
01776       const _Tp __i2 = __z.imag() * __z.imag();
01777       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
01778 
01779       _Tp __num = _Tp(1.0) + __z.real();
01780       _Tp __den = _Tp(1.0) - __z.real();
01781 
01782       __num = __i2 + __num * __num;
01783       __den = __i2 + __den * __den;
01784 
01785       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
01786                    _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
01787     }
01788 
01789 #if _GLIBCXX_USE_C99_COMPLEX_TR1
01790   inline __complex__ float
01791   __complex_atanh(__complex__ float __z)
01792   { return __builtin_catanhf(__z); }
01793 
01794   inline __complex__ double
01795   __complex_atanh(__complex__ double __z)
01796   { return __builtin_catanh(__z); }
01797 
01798   inline __complex__ long double
01799   __complex_atanh(const __complex__ long double& __z)
01800   { return __builtin_catanhl(__z); }
01801 
01802   template<typename _Tp>
01803     inline std::complex<_Tp>
01804     atanh(const std::complex<_Tp>& __z)
01805     { return __complex_atanh(__z.__rep()); }
01806 #else
01807   /// atanh(__z) [8.1.7].
01808   //  Effects:  Behaves the same as C99 function catanh, defined
01809   //            in subclause 7.3.6.3.
01810   template<typename _Tp>
01811     inline std::complex<_Tp>
01812     atanh(const std::complex<_Tp>& __z)
01813     { return __complex_atanh(__z); }
01814 #endif
01815 
01816   template<typename _Tp>
01817     inline _Tp
01818     /// fabs(__z) [8.1.8].
01819     //  Effects:  Behaves the same as C99 function cabs, defined
01820     //            in subclause 7.3.8.1.
01821     fabs(const std::complex<_Tp>& __z)
01822     { return std::abs(__z); }
01823 
01824   /// Additional overloads [8.1.9].
01825   template<typename _Tp>
01826     inline typename __gnu_cxx::__promote<_Tp>::__type
01827     arg(_Tp __x)
01828     {
01829       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
01830 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
01831       return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
01832                            : __type();
01833 #else
01834       return std::arg(std::complex<__type>(__x));
01835 #endif
01836     }
01837 
01838   template<typename _Tp>
01839     inline typename __gnu_cxx::__promote<_Tp>::__type
01840     imag(_Tp)
01841     { return _Tp(); }
01842 
01843   template<typename _Tp>
01844     inline typename __gnu_cxx::__promote<_Tp>::__type
01845     norm(_Tp __x)
01846     {
01847       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
01848       return __type(__x) * __type(__x);
01849     }
01850 
01851   template<typename _Tp>
01852     inline typename __gnu_cxx::__promote<_Tp>::__type
01853     real(_Tp __x)
01854     { return __x; }
01855 
01856   template<typename _Tp, typename _Up>
01857     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
01858     pow(const std::complex<_Tp>& __x, const _Up& __y)
01859     {
01860       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
01861       return std::pow(std::complex<__type>(__x), __type(__y));
01862     }
01863 
01864   template<typename _Tp, typename _Up>
01865     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
01866     pow(const _Tp& __x, const std::complex<_Up>& __y)
01867     {
01868       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
01869       return std::pow(__type(__x), std::complex<__type>(__y));
01870     }
01871 
01872   template<typename _Tp, typename _Up>
01873     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
01874     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
01875     {
01876       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
01877       return std::pow(std::complex<__type>(__x),
01878               std::complex<__type>(__y));
01879     }
01880 
01881   // Forward declarations.
01882   // DR 781.
01883   template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
01884 
01885   template<typename _Tp>
01886     std::complex<_Tp>
01887     __complex_proj(const std::complex<_Tp>& __z)
01888     {
01889       const _Tp __den = (__z.real() * __z.real()
01890              + __z.imag() * __z.imag() + _Tp(1.0));
01891 
01892       return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
01893                    (_Tp(2.0) * __z.imag()) / __den);
01894     }
01895 
01896 #if _GLIBCXX_USE_C99_COMPLEX
01897   inline __complex__ float
01898   __complex_proj(__complex__ float __z)
01899   { return __builtin_cprojf(__z); }
01900 
01901   inline __complex__ double
01902   __complex_proj(__complex__ double __z)
01903   { return __builtin_cproj(__z); }
01904 
01905   inline __complex__ long double
01906   __complex_proj(const __complex__ long double& __z)
01907   { return __builtin_cprojl(__z); }
01908 
01909   template<typename _Tp>
01910     inline std::complex<_Tp>
01911     proj(const std::complex<_Tp>& __z)
01912     { return __complex_proj(__z.__rep()); }
01913 #else
01914   template<typename _Tp>
01915     inline std::complex<_Tp>
01916     proj(const std::complex<_Tp>& __z)
01917     { return __complex_proj(__z); }
01918 #endif
01919 
01920   // DR 1137.
01921   template<typename _Tp>
01922     inline typename __gnu_cxx::__promote<_Tp>::__type
01923     proj(_Tp __x)
01924     { return __x; }
01925 
01926   template<typename _Tp>
01927     inline typename __gnu_cxx::__promote<_Tp>::__type
01928     conj(_Tp __x)
01929     { return __x; }
01930 
01931 _GLIBCXX_END_NAMESPACE_VERSION
01932 } // namespace
01933 
01934 #endif  // __GXX_EXPERIMENTAL_CXX0X__
01935 
01936 #endif  /* _GLIBCXX_COMPLEX */