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 _GLIBCXX_BEGIN_NAMESPACE(std)
00050 
00051   /**
00052    * @defgroup complex_numbers Complex Numbers
00053    * @ingroup numerics
00054    *
00055    * Classes and functions for complex numbers.
00056    * @{
00057    */
00058 
00059   // Forward declarations.
00060   template<typename _Tp> class complex;
00061   template<> class complex<float>;
00062   template<> class complex<double>;
00063   template<> class complex<long double>;
00064 
00065   ///  Return magnitude of @a z.
00066   template<typename _Tp> _Tp abs(const complex<_Tp>&);
00067   ///  Return phase angle of @a z.
00068   template<typename _Tp> _Tp arg(const complex<_Tp>&);
00069   ///  Return @a z magnitude squared.
00070   template<typename _Tp> _Tp norm(const complex<_Tp>&);
00071 
00072   ///  Return complex conjugate of @a z.
00073   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
00074   ///  Return complex with magnitude @a rho and angle @a theta.
00075   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
00076 
00077   // Transcendentals:
00078   /// Return complex cosine of @a z.
00079   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
00080   /// Return complex hyperbolic cosine of @a z.
00081   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
00082   /// Return complex base e exponential of @a z.
00083   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
00084   /// Return complex natural logarithm of @a z.
00085   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
00086   /// Return complex base 10 logarithm of @a z.
00087   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
00088 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00089   // DR 844.
00090   /// Return @a x to the @a y'th power.
00091   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
00092 #endif
00093   /// Return @a x to the @a y'th power.
00094   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
00095   /// Return @a x to the @a y'th power.
00096   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
00097                                           const complex<_Tp>&);
00098   /// Return @a x to the @a y'th power.
00099   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
00100   /// Return complex sine of @a z.
00101   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
00102   /// Return complex hyperbolic sine of @a z.
00103   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
00104   /// Return complex square root of @a z.
00105   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
00106   /// Return complex tangent of @a z.
00107   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
00108   /// Return complex hyperbolic tangent of @a z.
00109   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
00110     
00111     
00112   // 26.2.2  Primary template class complex
00113   /**
00114    *  Template to represent complex numbers.
00115    *
00116    *  Specializations for float, double, and long double are part of the
00117    *  library.  Results with any other type are not guaranteed.
00118    *
00119    *  @param  Tp  Type of real and imaginary values.
00120   */
00121   template<typename _Tp>
00122     struct complex
00123     {
00124       /// Value typedef.
00125       typedef _Tp value_type;
00126       
00127       ///  Default constructor.  First parameter is x, second parameter is y.
00128       ///  Unspecified parameters default to 0.
00129       complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
00130       : _M_real(__r), _M_imag(__i) { }
00131 
00132       // Lets the compiler synthesize the copy constructor   
00133       // complex (const complex<_Tp>&);
00134       ///  Copy constructor.
00135       template<typename _Up>
00136         complex(const complex<_Up>& __z)
00137     : _M_real(__z.real()), _M_imag(__z.imag()) { }
00138 
00139 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00140       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00141       // DR 387. std::complex over-encapsulated.
00142       _Tp real() const
00143       { return _M_real; }
00144 
00145       _Tp imag() const
00146       { return _M_imag; }
00147 #else
00148       ///  Return real part of complex number.
00149       _Tp& real()
00150       { return _M_real; }
00151 
00152       ///  Return real part of complex number.
00153       const _Tp& real() const
00154       { return _M_real; }
00155 
00156       ///  Return imaginary part of complex number.
00157       _Tp& imag()
00158       { return _M_imag; }
00159 
00160       ///  Return imaginary part of complex number.
00161       const _Tp& imag() const
00162       { return _M_imag; }
00163 #endif
00164 
00165       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00166       // DR 387. std::complex over-encapsulated.
00167       void real(_Tp __val)
00168       { _M_real = __val; }
00169 
00170       void imag(_Tp __val)
00171       { _M_imag = __val; }
00172 
00173       /// Assign this complex number to scalar @a t.
00174       complex<_Tp>& operator=(const _Tp&);
00175       
00176       /// Add @a t to this complex number.
00177       // 26.2.5/1
00178       complex<_Tp>&
00179       operator+=(const _Tp& __t)
00180       {
00181     _M_real += __t;
00182     return *this;
00183       }
00184 
00185       /// Subtract @a t from this complex number.
00186       // 26.2.5/3
00187       complex<_Tp>&
00188       operator-=(const _Tp& __t)
00189       {
00190     _M_real -= __t;
00191     return *this;
00192       }
00193 
00194       /// Multiply this complex number by @a t.
00195       complex<_Tp>& operator*=(const _Tp&);
00196       /// Divide this complex number by @a t.
00197       complex<_Tp>& operator/=(const _Tp&);
00198 
00199       // Lets the compiler synthesize the
00200       // copy and assignment operator
00201       // complex<_Tp>& operator= (const complex<_Tp>&);
00202       /// Assign this complex number to complex @a z.
00203       template<typename _Up>
00204         complex<_Tp>& operator=(const complex<_Up>&);
00205       /// Add @a z to this complex number.
00206       template<typename _Up>
00207         complex<_Tp>& operator+=(const complex<_Up>&);
00208       /// Subtract @a z from this complex number.
00209       template<typename _Up>
00210         complex<_Tp>& operator-=(const complex<_Up>&);
00211       /// Multiply this complex number by @a z.
00212       template<typename _Up>
00213         complex<_Tp>& operator*=(const complex<_Up>&);
00214       /// Divide this complex number by @a z.
00215       template<typename _Up>
00216         complex<_Tp>& operator/=(const complex<_Up>&);
00217 
00218       const complex& __rep() const
00219       { return *this; }
00220 
00221     private:
00222       _Tp _M_real;
00223       _Tp _M_imag;
00224     };
00225 
00226   template<typename _Tp>
00227     complex<_Tp>&
00228     complex<_Tp>::operator=(const _Tp& __t)
00229     {
00230      _M_real = __t;
00231      _M_imag = _Tp();
00232      return *this;
00233     } 
00234 
00235   // 26.2.5/5
00236   template<typename _Tp>
00237     complex<_Tp>&
00238     complex<_Tp>::operator*=(const _Tp& __t)
00239     {
00240       _M_real *= __t;
00241       _M_imag *= __t;
00242       return *this;
00243     }
00244 
00245   // 26.2.5/7
00246   template<typename _Tp>
00247     complex<_Tp>&
00248     complex<_Tp>::operator/=(const _Tp& __t)
00249     {
00250       _M_real /= __t;
00251       _M_imag /= __t;
00252       return *this;
00253     }
00254 
00255   template<typename _Tp>
00256     template<typename _Up>
00257     complex<_Tp>&
00258     complex<_Tp>::operator=(const complex<_Up>& __z)
00259     {
00260       _M_real = __z.real();
00261       _M_imag = __z.imag();
00262       return *this;
00263     }
00264 
00265   // 26.2.5/9
00266   template<typename _Tp>
00267     template<typename _Up>
00268     complex<_Tp>&
00269     complex<_Tp>::operator+=(const complex<_Up>& __z)
00270     {
00271       _M_real += __z.real();
00272       _M_imag += __z.imag();
00273       return *this;
00274     }
00275 
00276   // 26.2.5/11
00277   template<typename _Tp>
00278     template<typename _Up>
00279     complex<_Tp>&
00280     complex<_Tp>::operator-=(const complex<_Up>& __z)
00281     {
00282       _M_real -= __z.real();
00283       _M_imag -= __z.imag();
00284       return *this;
00285     }
00286 
00287   // 26.2.5/13
00288   // XXX: This is a grammar school implementation.
00289   template<typename _Tp>
00290     template<typename _Up>
00291     complex<_Tp>&
00292     complex<_Tp>::operator*=(const complex<_Up>& __z)
00293     {
00294       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
00295       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
00296       _M_real = __r;
00297       return *this;
00298     }
00299 
00300   // 26.2.5/15
00301   // XXX: This is a grammar school implementation.
00302   template<typename _Tp>
00303     template<typename _Up>
00304     complex<_Tp>&
00305     complex<_Tp>::operator/=(const complex<_Up>& __z)
00306     {
00307       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
00308       const _Tp __n = std::norm(__z);
00309       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
00310       _M_real = __r / __n;
00311       return *this;
00312     }
00313     
00314   // Operators:
00315   //@{
00316   ///  Return new complex value @a x plus @a y.
00317   template<typename _Tp>
00318     inline complex<_Tp>
00319     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
00320     {
00321       complex<_Tp> __r = __x;
00322       __r += __y;
00323       return __r;
00324     }
00325 
00326   template<typename _Tp>
00327     inline complex<_Tp>
00328     operator+(const complex<_Tp>& __x, const _Tp& __y)
00329     {
00330       complex<_Tp> __r = __x;
00331       __r += __y;
00332       return __r;
00333     }
00334 
00335   template<typename _Tp>
00336     inline complex<_Tp>
00337     operator+(const _Tp& __x, const complex<_Tp>& __y)
00338     {
00339       complex<_Tp> __r = __y;
00340       __r += __x;
00341       return __r;
00342     }
00343   //@}
00344 
00345   //@{
00346   ///  Return new complex value @a x minus @a y.
00347   template<typename _Tp>
00348     inline complex<_Tp>
00349     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
00350     {
00351       complex<_Tp> __r = __x;
00352       __r -= __y;
00353       return __r;
00354     }
00355     
00356   template<typename _Tp>
00357     inline complex<_Tp>
00358     operator-(const complex<_Tp>& __x, const _Tp& __y)
00359     {
00360       complex<_Tp> __r = __x;
00361       __r -= __y;
00362       return __r;
00363     }
00364 
00365   template<typename _Tp>
00366     inline complex<_Tp>
00367     operator-(const _Tp& __x, const complex<_Tp>& __y)
00368     {
00369       complex<_Tp> __r(__x, -__y.imag());
00370       __r -= __y.real();
00371       return __r;
00372     }
00373   //@}
00374 
00375   //@{
00376   ///  Return new complex value @a x times @a y.
00377   template<typename _Tp>
00378     inline complex<_Tp>
00379     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
00380     {
00381       complex<_Tp> __r = __x;
00382       __r *= __y;
00383       return __r;
00384     }
00385 
00386   template<typename _Tp>
00387     inline complex<_Tp>
00388     operator*(const complex<_Tp>& __x, const _Tp& __y)
00389     {
00390       complex<_Tp> __r = __x;
00391       __r *= __y;
00392       return __r;
00393     }
00394 
00395   template<typename _Tp>
00396     inline complex<_Tp>
00397     operator*(const _Tp& __x, const complex<_Tp>& __y)
00398     {
00399       complex<_Tp> __r = __y;
00400       __r *= __x;
00401       return __r;
00402     }
00403   //@}
00404 
00405   //@{
00406   ///  Return new complex value @a x divided by @a y.
00407   template<typename _Tp>
00408     inline complex<_Tp>
00409     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
00410     {
00411       complex<_Tp> __r = __x;
00412       __r /= __y;
00413       return __r;
00414     }
00415     
00416   template<typename _Tp>
00417     inline complex<_Tp>
00418     operator/(const complex<_Tp>& __x, const _Tp& __y)
00419     {
00420       complex<_Tp> __r = __x;
00421       __r /= __y;
00422       return __r;
00423     }
00424 
00425   template<typename _Tp>
00426     inline complex<_Tp>
00427     operator/(const _Tp& __x, const complex<_Tp>& __y)
00428     {
00429       complex<_Tp> __r = __x;
00430       __r /= __y;
00431       return __r;
00432     }
00433   //@}
00434 
00435   ///  Return @a x.
00436   template<typename _Tp>
00437     inline complex<_Tp>
00438     operator+(const complex<_Tp>& __x)
00439     { return __x; }
00440 
00441   ///  Return complex negation of @a x.
00442   template<typename _Tp>
00443     inline complex<_Tp>
00444     operator-(const complex<_Tp>& __x)
00445     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
00446 
00447   //@{
00448   ///  Return true if @a x is equal to @a y.
00449   template<typename _Tp>
00450     inline bool
00451     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
00452     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
00453 
00454   template<typename _Tp>
00455     inline bool
00456     operator==(const complex<_Tp>& __x, const _Tp& __y)
00457     { return __x.real() == __y && __x.imag() == _Tp(); }
00458 
00459   template<typename _Tp>
00460     inline bool
00461     operator==(const _Tp& __x, const complex<_Tp>& __y)
00462     { return __x == __y.real() && _Tp() == __y.imag(); }
00463   //@}
00464 
00465   //@{
00466   ///  Return false if @a x is equal to @a y.
00467   template<typename _Tp>
00468     inline bool
00469     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
00470     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
00471 
00472   template<typename _Tp>
00473     inline bool
00474     operator!=(const complex<_Tp>& __x, const _Tp& __y)
00475     { return __x.real() != __y || __x.imag() != _Tp(); }
00476 
00477   template<typename _Tp>
00478     inline bool
00479     operator!=(const _Tp& __x, const complex<_Tp>& __y)
00480     { return __x != __y.real() || _Tp() != __y.imag(); }
00481   //@}
00482 
00483   ///  Extraction operator for complex values.
00484   template<typename _Tp, typename _CharT, class _Traits>
00485     basic_istream<_CharT, _Traits>&
00486     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
00487     {
00488       _Tp __re_x, __im_x;
00489       _CharT __ch;
00490       __is >> __ch;
00491       if (__ch == '(') 
00492     {
00493       __is >> __re_x >> __ch;
00494       if (__ch == ',') 
00495         {
00496           __is >> __im_x >> __ch;
00497           if (__ch == ')') 
00498         __x = complex<_Tp>(__re_x, __im_x);
00499           else
00500         __is.setstate(ios_base::failbit);
00501         }
00502       else if (__ch == ')') 
00503         __x = __re_x;
00504       else
00505         __is.setstate(ios_base::failbit);
00506     }
00507       else 
00508     {
00509       __is.putback(__ch);
00510       __is >> __re_x;
00511       __x = __re_x;
00512     }
00513       return __is;
00514     }
00515 
00516   ///  Insertion operator for complex values.
00517   template<typename _Tp, typename _CharT, class _Traits>
00518     basic_ostream<_CharT, _Traits>&
00519     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
00520     {
00521       basic_ostringstream<_CharT, _Traits> __s;
00522       __s.flags(__os.flags());
00523       __s.imbue(__os.getloc());
00524       __s.precision(__os.precision());
00525       __s << '(' << __x.real() << ',' << __x.imag() << ')';
00526       return __os << __s.str();
00527     }
00528 
00529   // Values
00530 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00531   template<typename _Tp>
00532     inline _Tp
00533     real(const complex<_Tp>& __z)
00534     { return __z.real(); }
00535     
00536   template<typename _Tp>
00537     inline _Tp
00538     imag(const complex<_Tp>& __z)
00539     { return __z.imag(); }
00540 #else
00541   template<typename _Tp>
00542     inline _Tp&
00543     real(complex<_Tp>& __z)
00544     { return __z.real(); }
00545     
00546   template<typename _Tp>
00547     inline const _Tp&
00548     real(const complex<_Tp>& __z)
00549     { return __z.real(); }
00550     
00551   template<typename _Tp>
00552     inline _Tp&
00553     imag(complex<_Tp>& __z)
00554     { return __z.imag(); }
00555     
00556   template<typename _Tp>
00557     inline const _Tp&
00558     imag(const complex<_Tp>& __z)
00559     { return __z.imag(); }
00560 #endif
00561 
00562   // 26.2.7/3 abs(__z):  Returns the magnitude of __z.
00563   template<typename _Tp>
00564     inline _Tp
00565     __complex_abs(const complex<_Tp>& __z)
00566     {
00567       _Tp __x = __z.real();
00568       _Tp __y = __z.imag();
00569       const _Tp __s = std::max(abs(__x), abs(__y));
00570       if (__s == _Tp())  // well ...
00571         return __s;
00572       __x /= __s; 
00573       __y /= __s;
00574       return __s * sqrt(__x * __x + __y * __y);
00575     }
00576 
00577 #if _GLIBCXX_USE_C99_COMPLEX
00578   inline float
00579   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
00580 
00581   inline double
00582   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
00583 
00584   inline long double
00585   __complex_abs(const __complex__ long double& __z)
00586   { return __builtin_cabsl(__z); }
00587 
00588   template<typename _Tp>
00589     inline _Tp
00590     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
00591 #else
00592   template<typename _Tp>
00593     inline _Tp
00594     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
00595 #endif  
00596 
00597 
00598   // 26.2.7/4: arg(__z): Returns the phase angle of __z.
00599   template<typename _Tp>
00600     inline _Tp
00601     __complex_arg(const complex<_Tp>& __z)
00602     { return  atan2(__z.imag(), __z.real()); }
00603 
00604 #if _GLIBCXX_USE_C99_COMPLEX
00605   inline float
00606   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
00607 
00608   inline double
00609   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
00610 
00611   inline long double
00612   __complex_arg(const __complex__ long double& __z)
00613   { return __builtin_cargl(__z); }
00614 
00615   template<typename _Tp>
00616     inline _Tp
00617     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
00618 #else
00619   template<typename _Tp>
00620     inline _Tp
00621     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
00622 #endif
00623 
00624   // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
00625   //     As defined, norm() is -not- a norm is the common mathematical
00626   //     sens used in numerics.  The helper class _Norm_helper<> tries to
00627   //     distinguish between builtin floating point and the rest, so as
00628   //     to deliver an answer as close as possible to the real value.
00629   template<bool>
00630     struct _Norm_helper
00631     {
00632       template<typename _Tp>
00633         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00634         {
00635           const _Tp __x = __z.real();
00636           const _Tp __y = __z.imag();
00637           return __x * __x + __y * __y;
00638         }
00639     };
00640 
00641   template<>
00642     struct _Norm_helper<true>
00643     {
00644       template<typename _Tp>
00645         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00646         {
00647           _Tp __res = std::abs(__z);
00648           return __res * __res;
00649         }
00650     };
00651   
00652   template<typename _Tp>
00653     inline _Tp
00654     norm(const complex<_Tp>& __z)
00655     {
00656       return _Norm_helper<__is_floating<_Tp>::__value 
00657     && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
00658     }
00659 
00660   template<typename _Tp>
00661     inline complex<_Tp>
00662     polar(const _Tp& __rho, const _Tp& __theta)
00663     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
00664 
00665   template<typename _Tp>
00666     inline complex<_Tp>
00667     conj(const complex<_Tp>& __z)
00668     { return complex<_Tp>(__z.real(), -__z.imag()); }
00669   
00670   // Transcendentals
00671 
00672   // 26.2.8/1 cos(__z):  Returns the cosine of __z.
00673   template<typename _Tp>
00674     inline complex<_Tp>
00675     __complex_cos(const complex<_Tp>& __z)
00676     {
00677       const _Tp __x = __z.real();
00678       const _Tp __y = __z.imag();
00679       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
00680     }
00681 
00682 #if _GLIBCXX_USE_C99_COMPLEX
00683   inline __complex__ float
00684   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
00685 
00686   inline __complex__ double
00687   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
00688 
00689   inline __complex__ long double
00690   __complex_cos(const __complex__ long double& __z)
00691   { return __builtin_ccosl(__z); }
00692 
00693   template<typename _Tp>
00694     inline complex<_Tp>
00695     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
00696 #else
00697   template<typename _Tp>
00698     inline complex<_Tp>
00699     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
00700 #endif
00701 
00702   // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
00703   template<typename _Tp>
00704     inline complex<_Tp>
00705     __complex_cosh(const complex<_Tp>& __z)
00706     {
00707       const _Tp __x = __z.real();
00708       const _Tp __y = __z.imag();
00709       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
00710     }
00711 
00712 #if _GLIBCXX_USE_C99_COMPLEX
00713   inline __complex__ float
00714   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
00715 
00716   inline __complex__ double
00717   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
00718 
00719   inline __complex__ long double
00720   __complex_cosh(const __complex__ long double& __z)
00721   { return __builtin_ccoshl(__z); }
00722 
00723   template<typename _Tp>
00724     inline complex<_Tp>
00725     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
00726 #else
00727   template<typename _Tp>
00728     inline complex<_Tp>
00729     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
00730 #endif
00731 
00732   // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
00733   template<typename _Tp>
00734     inline complex<_Tp>
00735     __complex_exp(const complex<_Tp>& __z)
00736     { return std::polar(exp(__z.real()), __z.imag()); }
00737 
00738 #if _GLIBCXX_USE_C99_COMPLEX
00739   inline __complex__ float
00740   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
00741 
00742   inline __complex__ double
00743   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
00744 
00745   inline __complex__ long double
00746   __complex_exp(const __complex__ long double& __z)
00747   { return __builtin_cexpl(__z); }
00748 
00749   template<typename _Tp>
00750     inline complex<_Tp>
00751     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
00752 #else
00753   template<typename _Tp>
00754     inline complex<_Tp>
00755     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
00756 #endif
00757 
00758   // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
00759   //                    The branch cut is along the negative axis.
00760   template<typename _Tp>
00761     inline complex<_Tp>
00762     __complex_log(const complex<_Tp>& __z)
00763     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
00764 
00765 #if _GLIBCXX_USE_C99_COMPLEX
00766   inline __complex__ float
00767   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
00768 
00769   inline __complex__ double
00770   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
00771 
00772   inline __complex__ long double
00773   __complex_log(const __complex__ long double& __z)
00774   { return __builtin_clogl(__z); }
00775 
00776   template<typename _Tp>
00777     inline complex<_Tp>
00778     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
00779 #else
00780   template<typename _Tp>
00781     inline complex<_Tp>
00782     log(const complex<_Tp>& __z) { return __complex_log(__z); }
00783 #endif
00784 
00785   template<typename _Tp>
00786     inline complex<_Tp>
00787     log10(const complex<_Tp>& __z)
00788     { return std::log(__z) / log(_Tp(10.0)); }
00789 
00790   // 26.2.8/10 sin(__z): Returns the sine of __z.
00791   template<typename _Tp>
00792     inline complex<_Tp>
00793     __complex_sin(const complex<_Tp>& __z)
00794     {
00795       const _Tp __x = __z.real();
00796       const _Tp __y = __z.imag();
00797       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
00798     }
00799 
00800 #if _GLIBCXX_USE_C99_COMPLEX
00801   inline __complex__ float
00802   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
00803 
00804   inline __complex__ double
00805   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
00806 
00807   inline __complex__ long double
00808   __complex_sin(const __complex__ long double& __z)
00809   { return __builtin_csinl(__z); }
00810 
00811   template<typename _Tp>
00812     inline complex<_Tp>
00813     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
00814 #else
00815   template<typename _Tp>
00816     inline complex<_Tp>
00817     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
00818 #endif
00819 
00820   // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
00821   template<typename _Tp>
00822     inline complex<_Tp>
00823     __complex_sinh(const complex<_Tp>& __z)
00824     {
00825       const _Tp __x = __z.real();
00826       const _Tp  __y = __z.imag();
00827       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
00828     }
00829 
00830 #if _GLIBCXX_USE_C99_COMPLEX
00831   inline __complex__ float
00832   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
00833 
00834   inline __complex__ double
00835   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
00836 
00837   inline __complex__ long double
00838   __complex_sinh(const __complex__ long double& __z)
00839   { return __builtin_csinhl(__z); }      
00840 
00841   template<typename _Tp>
00842     inline complex<_Tp>
00843     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
00844 #else
00845   template<typename _Tp>
00846     inline complex<_Tp>
00847     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
00848 #endif
00849 
00850   // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
00851   //                     The branch cut is on the negative axis.
00852   template<typename _Tp>
00853     complex<_Tp>
00854     __complex_sqrt(const complex<_Tp>& __z)
00855     {
00856       _Tp __x = __z.real();
00857       _Tp __y = __z.imag();
00858 
00859       if (__x == _Tp())
00860         {
00861           _Tp __t = sqrt(abs(__y) / 2);
00862           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
00863         }
00864       else
00865         {
00866           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
00867           _Tp __u = __t / 2;
00868           return __x > _Tp()
00869             ? complex<_Tp>(__u, __y / __t)
00870             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
00871         }
00872     }
00873 
00874 #if _GLIBCXX_USE_C99_COMPLEX
00875   inline __complex__ float
00876   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
00877 
00878   inline __complex__ double
00879   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
00880 
00881   inline __complex__ long double
00882   __complex_sqrt(const __complex__ long double& __z)
00883   { return __builtin_csqrtl(__z); }
00884 
00885   template<typename _Tp>
00886     inline complex<_Tp>
00887     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
00888 #else
00889   template<typename _Tp>
00890     inline complex<_Tp>
00891     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
00892 #endif
00893 
00894   // 26.2.8/14 tan(__z):  Return the complex tangent of __z.
00895   
00896   template<typename _Tp>
00897     inline complex<_Tp>
00898     __complex_tan(const complex<_Tp>& __z)
00899     { return std::sin(__z) / std::cos(__z); }
00900 
00901 #if _GLIBCXX_USE_C99_COMPLEX
00902   inline __complex__ float
00903   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
00904 
00905   inline __complex__ double
00906   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
00907 
00908   inline __complex__ long double
00909   __complex_tan(const __complex__ long double& __z)
00910   { return __builtin_ctanl(__z); }
00911 
00912   template<typename _Tp>
00913     inline complex<_Tp>
00914     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
00915 #else
00916   template<typename _Tp>
00917     inline complex<_Tp>
00918     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
00919 #endif
00920 
00921 
00922   // 26.2.8/15 tanh(__z):  Returns the hyperbolic tangent of __z.
00923   
00924   template<typename _Tp>
00925     inline complex<_Tp>
00926     __complex_tanh(const complex<_Tp>& __z)
00927     { return std::sinh(__z) / std::cosh(__z); }
00928 
00929 #if _GLIBCXX_USE_C99_COMPLEX
00930   inline __complex__ float
00931   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
00932 
00933   inline __complex__ double
00934   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
00935 
00936   inline __complex__ long double
00937   __complex_tanh(const __complex__ long double& __z)
00938   { return __builtin_ctanhl(__z); }
00939 
00940   template<typename _Tp>
00941     inline complex<_Tp>
00942     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
00943 #else
00944   template<typename _Tp>
00945     inline complex<_Tp>
00946     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
00947 #endif
00948 
00949 
00950   // 26.2.8/9  pow(__x, __y): Returns the complex power base of __x
00951   //                          raised to the __y-th power.  The branch
00952   //                          cut is on the negative axis.
00953 #ifndef __GXX_EXPERIMENTAL_CXX0X__
00954   template<typename _Tp>
00955     complex<_Tp>
00956     __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
00957     {
00958       complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
00959 
00960       while (__n >>= 1)
00961         {
00962           __x *= __x;
00963           if (__n % 2)
00964             __y *= __x;
00965         }
00966 
00967       return __y;
00968     }
00969 
00970   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00971   // DR 844. complex pow return type is ambiguous.
00972   template<typename _Tp>
00973     inline complex<_Tp>
00974     pow(const complex<_Tp>& __z, int __n)
00975     {
00976       return __n < 0
00977         ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -__n)
00978         : std::__complex_pow_unsigned(__z, __n);
00979     }
00980 #endif
00981 
00982   template<typename _Tp>
00983     complex<_Tp>
00984     pow(const complex<_Tp>& __x, const _Tp& __y)
00985     {
00986 #ifndef _GLIBCXX_USE_C99_COMPLEX
00987       if (__x == _Tp())
00988     return _Tp();
00989 #endif
00990       if (__x.imag() == _Tp() && __x.real() > _Tp())
00991         return pow(__x.real(), __y);
00992 
00993       complex<_Tp> __t = std::log(__x);
00994       return std::polar(exp(__y * __t.real()), __y * __t.imag());
00995     }
00996 
00997   template<typename _Tp>
00998     inline complex<_Tp>
00999     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01000     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
01001 
01002 #if _GLIBCXX_USE_C99_COMPLEX
01003   inline __complex__ float
01004   __complex_pow(__complex__ float __x, __complex__ float __y)
01005   { return __builtin_cpowf(__x, __y); }
01006 
01007   inline __complex__ double
01008   __complex_pow(__complex__ double __x, __complex__ double __y)
01009   { return __builtin_cpow(__x, __y); }
01010 
01011   inline __complex__ long double
01012   __complex_pow(const __complex__ long double& __x,
01013         const __complex__ long double& __y)
01014   { return __builtin_cpowl(__x, __y); }
01015 
01016   template<typename _Tp>
01017     inline complex<_Tp>
01018     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01019     { return __complex_pow(__x.__rep(), __y.__rep()); }
01020 #else
01021   template<typename _Tp>
01022     inline complex<_Tp>
01023     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
01024     { return __complex_pow(__x, __y); }
01025 #endif
01026 
01027   template<typename _Tp>
01028     inline complex<_Tp>
01029     pow(const _Tp& __x, const complex<_Tp>& __y)
01030     {
01031       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
01032                       __y.imag() * log(__x))
01033                      : std::pow(complex<_Tp>(__x), __y);
01034     }
01035 
01036   // 26.2.3  complex specializations
01037   // complex<float> specialization
01038   template<>
01039     struct complex<float>
01040     {
01041       typedef float value_type;
01042       typedef __complex__ float _ComplexT;
01043 
01044       complex(_ComplexT __z) : _M_value(__z) { }
01045 
01046       complex(float __r = 0.0f, float __i = 0.0f)
01047       {
01048     __real__ _M_value = __r;
01049     __imag__ _M_value = __i;
01050       }
01051 
01052       explicit complex(const complex<double>&);
01053       explicit complex(const complex<long double>&);    
01054 
01055 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01056       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01057       // DR 387. std::complex over-encapsulated.
01058       float real() const
01059       { return __real__ _M_value; }
01060 
01061       float imag() const
01062       { return __imag__ _M_value; }
01063 #else
01064       float& real()
01065       { return __real__ _M_value; }
01066 
01067       const float& real() const
01068       { return __real__ _M_value; }      
01069 
01070       float& imag()
01071       { return __imag__ _M_value; }
01072 
01073       const float& imag() const
01074       { return __imag__ _M_value; }
01075 #endif
01076 
01077       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01078       // DR 387. std::complex over-encapsulated.
01079       void real(float __val)
01080       { __real__ _M_value = __val; }
01081 
01082       void imag(float __val)
01083       { __imag__ _M_value = __val; }
01084 
01085       complex&
01086       operator=(float __f)
01087       {
01088     _M_value = __f;
01089     return *this;
01090       }
01091 
01092       complex&
01093       operator+=(float __f)
01094       {
01095     _M_value += __f;
01096     return *this;
01097       }
01098 
01099       complex&
01100       operator-=(float __f)
01101       {
01102     _M_value -= __f;
01103     return *this;
01104       }
01105 
01106       complex&
01107       operator*=(float __f)
01108       {
01109     _M_value *= __f;
01110     return *this;
01111       }
01112 
01113       complex&
01114       operator/=(float __f)
01115       {
01116     _M_value /= __f;
01117     return *this;
01118       }
01119 
01120       // Let the compiler synthesize the copy and assignment
01121       // operator.  It always does a pretty good job.
01122       // complex& operator=(const complex&);
01123 
01124       template<typename _Tp>
01125         complex&
01126         operator=(const complex<_Tp>&  __z)
01127     {
01128       __real__ _M_value = __z.real();
01129       __imag__ _M_value = __z.imag();
01130       return *this;
01131     }
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<class _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       _ComplexT __t;
01156       __real__ __t = __z.real();
01157       __imag__ __t = __z.imag();
01158       _M_value *= __t;
01159       return *this;
01160     }
01161 
01162       template<class _Tp>
01163         complex&
01164         operator/=(const complex<_Tp>& __z)
01165     {
01166       _ComplexT __t;
01167       __real__ __t = __z.real();
01168       __imag__ __t = __z.imag();
01169       _M_value /= __t;
01170       return *this;
01171     }
01172 
01173       const _ComplexT& __rep() const { return _M_value; }
01174 
01175     private:
01176       _ComplexT _M_value;
01177     };
01178 
01179   // 26.2.3  complex specializations
01180   // complex<double> specialization
01181   template<>
01182     struct complex<double>
01183     {
01184       typedef double value_type;
01185       typedef __complex__ double _ComplexT;
01186 
01187       complex(_ComplexT __z) : _M_value(__z) { }
01188 
01189       complex(double __r = 0.0, double __i = 0.0)
01190       {
01191     __real__ _M_value = __r;
01192     __imag__ _M_value = __i;
01193       }
01194 
01195       complex(const complex<float>& __z)
01196       : _M_value(__z.__rep()) { }
01197 
01198       explicit complex(const complex<long double>&);    
01199 
01200 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01201       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01202       // DR 387. std::complex over-encapsulated.
01203       double real() const
01204       { return __real__ _M_value; }
01205 
01206       double imag() const
01207       { return __imag__ _M_value; }
01208 #else
01209       double& real()
01210       { return __real__ _M_value; }
01211 
01212       const double& real() const
01213       { return __real__ _M_value; }
01214 
01215       double& imag()
01216       { return __imag__ _M_value; }
01217 
01218       const double& imag() const
01219       { return __imag__ _M_value; }
01220 #endif
01221 
01222       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01223       // DR 387. std::complex over-encapsulated.
01224       void real(double __val)
01225       { __real__ _M_value = __val; }
01226 
01227       void imag(double __val)
01228       { __imag__ _M_value = __val; }
01229 
01230       complex&
01231       operator=(double __d)
01232       {
01233     _M_value = __d;
01234     return *this;
01235       }
01236 
01237       complex&
01238       operator+=(double __d)
01239       {
01240     _M_value += __d;
01241     return *this;
01242       }
01243     
01244       complex&
01245       operator-=(double __d)
01246       {
01247     _M_value -= __d;
01248     return *this;
01249       }
01250 
01251       complex&
01252       operator*=(double __d)
01253       {
01254     _M_value *= __d;
01255     return *this;
01256       }
01257 
01258       complex&
01259       operator/=(double __d)
01260       {
01261     _M_value /= __d;
01262     return *this;
01263       }
01264 
01265       // The compiler will synthesize this, efficiently.
01266       // complex& operator=(const complex&);
01267 
01268       template<typename _Tp>
01269         complex&
01270         operator=(const complex<_Tp>& __z)
01271     {
01272       __real__ _M_value = __z.real();
01273       __imag__ _M_value = __z.imag();
01274       return *this;
01275     }
01276 
01277       template<typename _Tp>
01278         complex&
01279         operator+=(const complex<_Tp>& __z)
01280     {
01281       __real__ _M_value += __z.real();
01282       __imag__ _M_value += __z.imag();
01283       return *this;
01284     }
01285 
01286       template<typename _Tp>
01287         complex&
01288         operator-=(const complex<_Tp>& __z)
01289     {
01290       __real__ _M_value -= __z.real();
01291       __imag__ _M_value -= __z.imag();
01292       return *this;
01293     }
01294 
01295       template<typename _Tp>
01296         complex&
01297         operator*=(const complex<_Tp>& __z)
01298     {
01299       _ComplexT __t;
01300       __real__ __t = __z.real();
01301       __imag__ __t = __z.imag();
01302       _M_value *= __t;
01303       return *this;
01304     }
01305 
01306       template<typename _Tp>
01307         complex&
01308         operator/=(const complex<_Tp>& __z)
01309     {
01310       _ComplexT __t;
01311       __real__ __t = __z.real();
01312       __imag__ __t = __z.imag();
01313       _M_value /= __t;
01314       return *this;
01315     }
01316 
01317       const _ComplexT& __rep() const { return _M_value; }
01318 
01319     private:
01320       _ComplexT _M_value;
01321     };
01322 
01323   // 26.2.3  complex specializations
01324   // complex<long double> specialization
01325   template<>
01326     struct complex<long double>
01327     {
01328       typedef long double value_type;
01329       typedef __complex__ long double _ComplexT;
01330 
01331       complex(_ComplexT __z) : _M_value(__z) { }
01332 
01333       complex(long double __r = 0.0L, long double __i = 0.0L)
01334       {
01335     __real__ _M_value = __r;
01336     __imag__ _M_value = __i;
01337       }
01338 
01339       complex(const complex<float>& __z)
01340       : _M_value(__z.__rep()) { }
01341 
01342       complex(const complex<double>& __z)
01343       : _M_value(__z.__rep()) { }
01344 
01345 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01346       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01347       // DR 387. std::complex over-encapsulated.
01348       long double real() const
01349       { return __real__ _M_value; }
01350 
01351       long double imag() const
01352       { return __imag__ _M_value; }
01353 #else
01354       long double& real()
01355       { return __real__ _M_value; }
01356 
01357       const long double& real() const
01358       { return __real__ _M_value; }
01359 
01360       long double& imag()
01361       { return __imag__ _M_value; }
01362 
01363       const long double& imag() const
01364       { return __imag__ _M_value; }
01365 #endif
01366 
01367       // _GLIBCXX_RESOLVE_LIB_DEFECTS
01368       // DR 387. std::complex over-encapsulated.
01369       void real(long double __val)
01370       { __real__ _M_value = __val; }
01371 
01372       void imag(long double __val)
01373       { __imag__ _M_value = __val; }
01374 
01375       complex&
01376       operator=(long double __r)
01377       {
01378     _M_value = __r;
01379     return *this;
01380       }
01381 
01382       complex&
01383       operator+=(long double __r)
01384       {
01385     _M_value += __r;
01386     return *this;
01387       }
01388 
01389       complex&
01390       operator-=(long double __r)
01391       {
01392     _M_value -= __r;
01393     return *this;
01394       }
01395 
01396       complex&
01397       operator*=(long double __r)
01398       {
01399     _M_value *= __r;
01400     return *this;
01401       }
01402 
01403       complex&
01404       operator/=(long double __r)
01405       {
01406     _M_value /= __r;
01407     return *this;
01408       }
01409 
01410       // The compiler knows how to do this efficiently
01411       // complex& operator=(const complex&);
01412 
01413       template<typename _Tp>
01414         complex&
01415         operator=(const complex<_Tp>& __z)
01416     {
01417       __real__ _M_value = __z.real();
01418       __imag__ _M_value = __z.imag();
01419       return *this;
01420     }
01421 
01422       template<typename _Tp>
01423         complex&
01424     operator+=(const complex<_Tp>& __z)
01425     {
01426       __real__ _M_value += __z.real();
01427       __imag__ _M_value += __z.imag();
01428       return *this;
01429     }
01430 
01431       template<typename _Tp>
01432         complex&
01433     operator-=(const complex<_Tp>& __z)
01434     {
01435       __real__ _M_value -= __z.real();
01436       __imag__ _M_value -= __z.imag();
01437       return *this;
01438     }
01439 
01440       template<typename _Tp>
01441         complex&
01442     operator*=(const complex<_Tp>& __z)
01443     {
01444       _ComplexT __t;
01445       __real__ __t = __z.real();
01446       __imag__ __t = __z.imag();
01447       _M_value *= __t;
01448       return *this;
01449     }
01450 
01451       template<typename _Tp>
01452         complex&
01453     operator/=(const complex<_Tp>& __z)
01454     {
01455       _ComplexT __t;
01456       __real__ __t = __z.real();
01457       __imag__ __t = __z.imag();
01458       _M_value /= __t;
01459       return *this;
01460     }
01461 
01462       const _ComplexT& __rep() const { return _M_value; }
01463 
01464     private:
01465       _ComplexT _M_value;
01466     };
01467 
01468   // These bits have to be at the end of this file, so that the
01469   // specializations have all been defined.
01470   inline
01471   complex<float>::complex(const complex<double>& __z)
01472   : _M_value(__z.__rep()) { }
01473 
01474   inline
01475   complex<float>::complex(const complex<long double>& __z)
01476   : _M_value(__z.__rep()) { }
01477 
01478   inline
01479   complex<double>::complex(const complex<long double>& __z)
01480   : _M_value(__z.__rep()) { }
01481 
01482   // Inhibit implicit instantiations for required instantiations,
01483   // which are defined via explicit instantiations elsewhere.
01484   // NB:  This syntax is a GNU extension.
01485 #if _GLIBCXX_EXTERN_TEMPLATE
01486   extern template istream& operator>>(istream&, complex<float>&);
01487   extern template ostream& operator<<(ostream&, const complex<float>&);
01488   extern template istream& operator>>(istream&, complex<double>&);
01489   extern template ostream& operator<<(ostream&, const complex<double>&);
01490   extern template istream& operator>>(istream&, complex<long double>&);
01491   extern template ostream& operator<<(ostream&, const complex<long double>&);
01492 
01493 #ifdef _GLIBCXX_USE_WCHAR_T
01494   extern template wistream& operator>>(wistream&, complex<float>&);
01495   extern template wostream& operator<<(wostream&, const complex<float>&);
01496   extern template wistream& operator>>(wistream&, complex<double>&);
01497   extern template wostream& operator<<(wostream&, const complex<double>&);
01498   extern template wistream& operator>>(wistream&, complex<long double>&);
01499   extern template wostream& operator<<(wostream&, const complex<long double>&);
01500 #endif
01501 #endif
01502 
01503   // @} group complex_numbers
01504 
01505 _GLIBCXX_END_NAMESPACE
01506 
01507 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
01508 
01509   // See ext/type_traits.h for the primary template.
01510   template<typename _Tp, typename _Up>
01511     struct __promote_2<std::complex<_Tp>, _Up>
01512     {
01513     public:
01514       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01515     };
01516 
01517   template<typename _Tp, typename _Up>
01518     struct __promote_2<_Tp, std::complex<_Up> >
01519     {
01520     public:
01521       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01522     };
01523   
01524   template<typename _Tp, typename _Up>
01525     struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
01526     {
01527     public:
01528       typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
01529     };
01530 
01531 _GLIBCXX_END_NAMESPACE
01532 
01533 #ifdef __GXX_EXPERIMENTAL_CXX0X__
01534 #  if defined(_GLIBCXX_INCLUDE_AS_TR1)
01535 #    error C++0x header cannot be included from TR1 header
01536 #  endif
01537 #  if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
01538 #    include <tr1_impl/complex>
01539 #  else
01540 #    define _GLIBCXX_INCLUDE_AS_CXX0X
01541 #    define _GLIBCXX_BEGIN_NAMESPACE_TR1
01542 #    define _GLIBCXX_END_NAMESPACE_TR1
01543 #    define _GLIBCXX_TR1
01544 #    include <tr1_impl/complex>
01545 #    undef _GLIBCXX_TR1
01546 #    undef _GLIBCXX_END_NAMESPACE_TR1
01547 #    undef _GLIBCXX_BEGIN_NAMESPACE_TR1
01548 #    undef _GLIBCXX_INCLUDE_AS_CXX0X
01549 #  endif
01550 
01551 _GLIBCXX_BEGIN_NAMESPACE(std)
01552 
01553   // Forward declarations.
01554   // DR 781.
01555   template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
01556 
01557   template<typename _Tp>
01558     std::complex<_Tp>
01559     __complex_proj(const std::complex<_Tp>& __z)
01560     {
01561       const _Tp __den = (__z.real() * __z.real()
01562              + __z.imag() * __z.imag() + _Tp(1.0));
01563 
01564       return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
01565                    (_Tp(2.0) * __z.imag()) / __den);
01566     }
01567 
01568 #if _GLIBCXX_USE_C99_COMPLEX
01569   inline __complex__ float
01570   __complex_proj(__complex__ float __z)
01571   { return __builtin_cprojf(__z); }
01572 
01573   inline __complex__ double
01574   __complex_proj(__complex__ double __z)
01575   { return __builtin_cproj(__z); }
01576 
01577   inline __complex__ long double
01578   __complex_proj(const __complex__ long double& __z)
01579   { return __builtin_cprojl(__z); }
01580 
01581   template<typename _Tp>
01582     inline std::complex<_Tp>
01583     proj(const std::complex<_Tp>& __z)
01584     { return __complex_proj(__z.__rep()); }
01585 #else
01586   template<typename _Tp>
01587     inline std::complex<_Tp>
01588     proj(const std::complex<_Tp>& __z)
01589     { return __complex_proj(__z); }
01590 #endif
01591 
01592   // DR 1137.
01593   template<typename _Tp>
01594     inline typename __gnu_cxx::__promote<_Tp>::__type
01595     proj(_Tp __x)
01596     { return __x; }
01597 
01598   template<typename _Tp>
01599     inline typename __gnu_cxx::__promote<_Tp>::__type
01600     conj(_Tp __x)
01601     { return __x; }
01602 
01603 _GLIBCXX_END_NAMESPACE
01604 
01605 #endif
01606 
01607 #endif  /* _GLIBCXX_COMPLEX */