00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00042 #ifndef _GLIBCXX_COMPLEX
00043 #define _GLIBCXX_COMPLEX 1
00044 
00045 #pragma GCC system_header
00046 
00047 #include <bits/c++config.h>
00048 #include <bits/cpp_type_traits.h>
00049 #include <cmath>
00050 #include <sstream>
00051 
00052 namespace std
00053 {
00054   
00055   template<typename _Tp> class complex;
00056   template<> class complex<float>;
00057   template<> class complex<double>;
00058   template<> class complex<long double>;
00059 
00061   template<typename _Tp> _Tp abs(const complex<_Tp>&);
00063   template<typename _Tp> _Tp arg(const complex<_Tp>&);
00065   template<typename _Tp> _Tp norm(const complex<_Tp>&);
00066 
00068   template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
00070   template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
00071 
00072   
00074   template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
00076   template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
00078   template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
00080   template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
00082   template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
00084   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
00086   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
00088   template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 
00089                                           const complex<_Tp>&);
00091   template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
00093   template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
00095   template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
00097   template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
00099   template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
00101   template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
00103     
00104     
00105   
00114   template<typename _Tp>
00115     struct complex
00116     {
00118       typedef _Tp value_type;
00119       
00122       complex(const _Tp& = _Tp(), const _Tp & = _Tp());
00123 
00124       
00125       
00127       template<typename _Up>
00128         complex(const complex<_Up>&);
00129 
00131       _Tp& real(); 
00133       const _Tp& real() const;
00135       _Tp& imag();
00137       const _Tp& imag() const;
00138 
00140       complex<_Tp>& operator=(const _Tp&);
00142       complex<_Tp>& operator+=(const _Tp&);
00144       complex<_Tp>& operator-=(const _Tp&);
00146       complex<_Tp>& operator*=(const _Tp&);
00148       complex<_Tp>& operator/=(const _Tp&);
00149 
00150       
00151       
00152       
00154       template<typename _Up>
00155         complex<_Tp>& operator=(const complex<_Up>&);
00157       template<typename _Up>
00158         complex<_Tp>& operator+=(const complex<_Up>&);
00160       template<typename _Up>
00161         complex<_Tp>& operator-=(const complex<_Up>&);
00163       template<typename _Up>
00164         complex<_Tp>& operator*=(const complex<_Up>&);
00166       template<typename _Up>
00167         complex<_Tp>& operator/=(const complex<_Up>&);
00168 
00169       const complex& __rep() const;
00170 
00171     private:
00172       _Tp _M_real;
00173       _Tp _M_imag;
00174     };
00175 
00176   template<typename _Tp>
00177     inline _Tp&
00178     complex<_Tp>::real() { return _M_real; }
00179 
00180   template<typename _Tp>
00181     inline const _Tp&
00182     complex<_Tp>::real() const { return _M_real; }
00183 
00184   template<typename _Tp>
00185     inline _Tp&
00186     complex<_Tp>::imag() { return _M_imag; }
00187 
00188   template<typename _Tp>
00189     inline const _Tp&
00190     complex<_Tp>::imag() const { return _M_imag; }
00191 
00192   template<typename _Tp>
00193     inline 
00194     complex<_Tp>::complex(const _Tp& __r, const _Tp& __i)
00195     : _M_real(__r), _M_imag(__i) { }
00196 
00197   template<typename _Tp>
00198     template<typename _Up>
00199     inline 
00200     complex<_Tp>::complex(const complex<_Up>& __z)
00201     : _M_real(__z.real()), _M_imag(__z.imag()) { }
00202         
00203   template<typename _Tp>
00204     complex<_Tp>&
00205     complex<_Tp>::operator=(const _Tp& __t)
00206     {
00207      _M_real = __t;
00208      _M_imag = _Tp();
00209      return *this;
00210     } 
00211 
00212   
00213   template<typename _Tp>
00214     inline complex<_Tp>&
00215     complex<_Tp>::operator+=(const _Tp& __t)
00216     {
00217       _M_real += __t;
00218       return *this;
00219     }
00220 
00221   
00222   template<typename _Tp>
00223     inline complex<_Tp>&
00224     complex<_Tp>::operator-=(const _Tp& __t)
00225     {
00226       _M_real -= __t;
00227       return *this;
00228     }
00229 
00230   
00231   template<typename _Tp>
00232     complex<_Tp>&
00233     complex<_Tp>::operator*=(const _Tp& __t)
00234     {
00235       _M_real *= __t;
00236       _M_imag *= __t;
00237       return *this;
00238     }
00239 
00240   
00241   template<typename _Tp>
00242     complex<_Tp>&
00243     complex<_Tp>::operator/=(const _Tp& __t)
00244     {
00245       _M_real /= __t;
00246       _M_imag /= __t;
00247       return *this;
00248     }
00249 
00250   template<typename _Tp>
00251     template<typename _Up>
00252     complex<_Tp>&
00253     complex<_Tp>::operator=(const complex<_Up>& __z)
00254     {
00255       _M_real = __z.real();
00256       _M_imag = __z.imag();
00257       return *this;
00258     }
00259 
00260   
00261   template<typename _Tp>
00262     template<typename _Up>
00263     complex<_Tp>&
00264     complex<_Tp>::operator+=(const complex<_Up>& __z)
00265     {
00266       _M_real += __z.real();
00267       _M_imag += __z.imag();
00268       return *this;
00269     }
00270 
00271   
00272   template<typename _Tp>
00273     template<typename _Up>
00274     complex<_Tp>&
00275     complex<_Tp>::operator-=(const complex<_Up>& __z)
00276     {
00277       _M_real -= __z.real();
00278       _M_imag -= __z.imag();
00279       return *this;
00280     }
00281 
00282   
00283   
00284   template<typename _Tp>
00285     template<typename _Up>
00286     complex<_Tp>&
00287     complex<_Tp>::operator*=(const complex<_Up>& __z)
00288     {
00289       const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
00290       _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
00291       _M_real = __r;
00292       return *this;
00293     }
00294 
00295   
00296   
00297   template<typename _Tp>
00298     template<typename _Up>
00299     complex<_Tp>&
00300     complex<_Tp>::operator/=(const complex<_Up>& __z)
00301     {
00302       const _Tp __r =  _M_real * __z.real() + _M_imag * __z.imag();
00303       const _Tp __n = std::norm(__z);
00304       _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
00305       _M_real = __r / __n;
00306       return *this;
00307     }
00308 
00309   template<typename _Tp>
00310     inline const complex<_Tp>&
00311     complex<_Tp>::__rep() const { return *this; }
00312     
00313   
00315 
00316   template<typename _Tp>
00317     inline complex<_Tp>
00318     operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
00319     {
00320       complex<_Tp> __r = __x;
00321       __r += __y;
00322       return __r;
00323     }
00324 
00325   template<typename _Tp>
00326     inline complex<_Tp>
00327     operator+(const complex<_Tp>& __x, const _Tp& __y)
00328     {
00329       complex<_Tp> __r = __x;
00330       __r.real() += __y;
00331       return __r;
00332     }
00333 
00334   template<typename _Tp>
00335     inline complex<_Tp>
00336     operator+(const _Tp& __x, const complex<_Tp>& __y)
00337     {
00338       complex<_Tp> __r = __y;
00339       __r.real() += __x;
00340       return __r;
00341     }
00343 
00345 
00346   template<typename _Tp>
00347     inline complex<_Tp>
00348     operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
00349     {
00350       complex<_Tp> __r = __x;
00351       __r -= __y;
00352       return __r;
00353     }
00354     
00355   template<typename _Tp>
00356     inline complex<_Tp>
00357     operator-(const complex<_Tp>& __x, const _Tp& __y)
00358     {
00359       complex<_Tp> __r = __x;
00360       __r.real() -= __y;
00361       return __r;
00362     }
00363 
00364   template<typename _Tp>
00365     inline complex<_Tp>
00366     operator-(const _Tp& __x, const complex<_Tp>& __y)
00367     {
00368       complex<_Tp> __r(__x, -__y.imag());
00369       __r.real() -= __y.real();
00370       return __r;
00371     }
00373 
00375 
00376   template<typename _Tp>
00377     inline complex<_Tp>
00378     operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
00379     {
00380       complex<_Tp> __r = __x;
00381       __r *= __y;
00382       return __r;
00383     }
00384 
00385   template<typename _Tp>
00386     inline complex<_Tp>
00387     operator*(const complex<_Tp>& __x, const _Tp& __y)
00388     {
00389       complex<_Tp> __r = __x;
00390       __r *= __y;
00391       return __r;
00392     }
00393 
00394   template<typename _Tp>
00395     inline complex<_Tp>
00396     operator*(const _Tp& __x, const complex<_Tp>& __y)
00397     {
00398       complex<_Tp> __r = __y;
00399       __r *= __x;
00400       return __r;
00401     }
00403 
00405 
00406   template<typename _Tp>
00407     inline complex<_Tp>
00408     operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
00409     {
00410       complex<_Tp> __r = __x;
00411       __r /= __y;
00412       return __r;
00413     }
00414     
00415   template<typename _Tp>
00416     inline complex<_Tp>
00417     operator/(const complex<_Tp>& __x, const _Tp& __y)
00418     {
00419       complex<_Tp> __r = __x;
00420       __r /= __y;
00421       return __r;
00422     }
00423 
00424   template<typename _Tp>
00425     inline complex<_Tp>
00426     operator/(const _Tp& __x, const complex<_Tp>& __y)
00427     {
00428       complex<_Tp> __r = __x;
00429       __r /= __y;
00430       return __r;
00431     }
00433 
00435   template<typename _Tp>
00436     inline complex<_Tp>
00437     operator+(const complex<_Tp>& __x)
00438     { return __x; }
00439 
00441   template<typename _Tp>
00442     inline complex<_Tp>
00443     operator-(const complex<_Tp>& __x)
00444     {  return complex<_Tp>(-__x.real(), -__x.imag()); }
00445 
00447 
00448   template<typename _Tp>
00449     inline bool
00450     operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
00451     { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
00452 
00453   template<typename _Tp>
00454     inline bool
00455     operator==(const complex<_Tp>& __x, const _Tp& __y)
00456     { return __x.real() == __y && __x.imag() == _Tp(); }
00457 
00458   template<typename _Tp>
00459     inline bool
00460     operator==(const _Tp& __x, const complex<_Tp>& __y)
00461     { return __x == __y.real() && _Tp() == __y.imag(); }
00463 
00465 
00466   template<typename _Tp>
00467     inline bool
00468     operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
00469     { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
00470 
00471   template<typename _Tp>
00472     inline bool
00473     operator!=(const complex<_Tp>& __x, const _Tp& __y)
00474     { return __x.real() != __y || __x.imag() != _Tp(); }
00475 
00476   template<typename _Tp>
00477     inline bool
00478     operator!=(const _Tp& __x, const complex<_Tp>& __y)
00479     { return __x != __y.real() || _Tp() != __y.imag(); }
00481 
00483   template<typename _Tp, typename _CharT, class _Traits>
00484     basic_istream<_CharT, _Traits>&
00485     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
00486     {
00487       _Tp __re_x, __im_x;
00488       _CharT __ch;
00489       __is >> __ch;
00490       if (__ch == '(') 
00491     {
00492       __is >> __re_x >> __ch;
00493       if (__ch == ',') 
00494         {
00495           __is >> __im_x >> __ch;
00496           if (__ch == ')') 
00497         __x = complex<_Tp>(__re_x, __im_x);
00498           else
00499         __is.setstate(ios_base::failbit);
00500         }
00501       else if (__ch == ')') 
00502         __x = __re_x;
00503       else
00504         __is.setstate(ios_base::failbit);
00505     }
00506       else 
00507     {
00508       __is.putback(__ch);
00509       __is >> __re_x;
00510       __x = __re_x;
00511     }
00512       return __is;
00513     }
00514 
00516   template<typename _Tp, typename _CharT, class _Traits>
00517     basic_ostream<_CharT, _Traits>&
00518     operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
00519     {
00520       basic_ostringstream<_CharT, _Traits> __s;
00521       __s.flags(__os.flags());
00522       __s.imbue(__os.getloc());
00523       __s.precision(__os.precision());
00524       __s << '(' << __x.real() << ',' << __x.imag() << ')';
00525       return __os << __s.str();
00526     }
00527 
00528   
00529   template<typename _Tp>
00530     inline _Tp&
00531     real(complex<_Tp>& __z)
00532     { return __z.real(); }
00533     
00534   template<typename _Tp>
00535     inline const _Tp&
00536     real(const complex<_Tp>& __z)
00537     { return __z.real(); }
00538     
00539   template<typename _Tp>
00540     inline _Tp&
00541     imag(complex<_Tp>& __z)
00542     { return __z.imag(); }
00543     
00544   template<typename _Tp>
00545     inline const _Tp&
00546     imag(const complex<_Tp>& __z)
00547     { return __z.imag(); }
00548 
00549   
00550   template<typename _Tp>
00551     inline _Tp
00552     __complex_abs(const complex<_Tp>& __z)
00553     {
00554       _Tp __x = __z.real();
00555       _Tp __y = __z.imag();
00556       const _Tp __s = std::max(abs(__x), abs(__y));
00557       if (__s == _Tp())  
00558         return __s;
00559       __x /= __s; 
00560       __y /= __s;
00561       return __s * sqrt(__x * __x + __y * __y);
00562     }
00563 
00564 #if _GLIBCXX_USE_C99_COMPLEX
00565   inline float
00566   __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
00567 
00568   inline double
00569   __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
00570 
00571   inline long double
00572   __complex_abs(const __complex__ long double& __z)
00573   { return __builtin_cabsl(__z); }
00574 
00575   template<typename _Tp>
00576     inline _Tp
00577     abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
00578 #else
00579   template<typename _Tp>
00580     inline _Tp
00581     abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
00582 #endif  
00583 
00584 
00585   
00586   template<typename _Tp>
00587     inline _Tp
00588     __complex_arg(const complex<_Tp>& __z)
00589     { return  atan2(__z.imag(), __z.real()); }
00590 
00591 #if _GLIBCXX_USE_C99_COMPLEX
00592   inline float
00593   __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
00594 
00595   inline double
00596   __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
00597 
00598   inline long double
00599   __complex_arg(const __complex__ long double& __z)
00600   { return __builtin_cargl(__z); }
00601 
00602   template<typename _Tp>
00603     inline _Tp
00604     arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
00605 #else
00606   template<typename _Tp>
00607     inline _Tp
00608     arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
00609 #endif
00610 
00611   
00612   
00613   
00614   
00615   
00616   template<bool>
00617     struct _Norm_helper
00618     {
00619       template<typename _Tp>
00620         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00621         {
00622           const _Tp __x = __z.real();
00623           const _Tp __y = __z.imag();
00624           return __x * __x + __y * __y;
00625         }
00626     };
00627 
00628   template<>
00629     struct _Norm_helper<true>
00630     {
00631       template<typename _Tp>
00632         static inline _Tp _S_do_it(const complex<_Tp>& __z)
00633         {
00634           _Tp __res = std::abs(__z);
00635           return __res * __res;
00636         }
00637     };
00638   
00639   template<typename _Tp>
00640     inline _Tp
00641     norm(const complex<_Tp>& __z)
00642     {
00643       return _Norm_helper<__is_floating<_Tp>::__value 
00644     && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
00645     }
00646 
00647   template<typename _Tp>
00648     inline complex<_Tp>
00649     polar(const _Tp& __rho, const _Tp& __theta)
00650     { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
00651 
00652   template<typename _Tp>
00653     inline complex<_Tp>
00654     conj(const complex<_Tp>& __z)
00655     { return complex<_Tp>(__z.real(), -__z.imag()); }
00656   
00657   
00658 
00659   
00660   template<typename _Tp>
00661     inline complex<_Tp>
00662     __complex_cos(const complex<_Tp>& __z)
00663     {
00664       const _Tp __x = __z.real();
00665       const _Tp __y = __z.imag();
00666       return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
00667     }
00668 
00669 #if _GLIBCXX_USE_C99_COMPLEX
00670   inline __complex__ float
00671   __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
00672 
00673   inline __complex__ double
00674   __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
00675 
00676   inline __complex__ long double
00677   __complex_cos(const __complex__ long double& __z)
00678   { return __builtin_ccosl(__z); }
00679 
00680   template<typename _Tp>
00681     inline complex<_Tp>
00682     cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
00683 #else
00684   template<typename _Tp>
00685     inline complex<_Tp>
00686     cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
00687 #endif
00688 
00689   
00690   template<typename _Tp>
00691     inline complex<_Tp>
00692     __complex_cosh(const complex<_Tp>& __z)
00693     {
00694       const _Tp __x = __z.real();
00695       const _Tp __y = __z.imag();
00696       return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
00697     }
00698 
00699 #if _GLIBCXX_USE_C99_COMPLEX
00700   inline __complex__ float
00701   __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
00702 
00703   inline __complex__ double
00704   __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
00705 
00706   inline __complex__ long double
00707   __complex_cosh(const __complex__ long double& __z)
00708   { return __builtin_ccoshl(__z); }
00709 
00710   template<typename _Tp>
00711     inline complex<_Tp>
00712     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
00713 #else
00714   template<typename _Tp>
00715     inline complex<_Tp>
00716     cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
00717 #endif
00718 
00719   
00720   template<typename _Tp>
00721     inline complex<_Tp>
00722     __complex_exp(const complex<_Tp>& __z)
00723     { return std::polar(exp(__z.real()), __z.imag()); }
00724 
00725 #if _GLIBCXX_USE_C99_COMPLEX
00726   inline __complex__ float
00727   __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
00728 
00729   inline __complex__ double
00730   __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
00731 
00732   inline __complex__ long double
00733   __complex_exp(const __complex__ long double& __z)
00734   { return __builtin_cexpl(__z); }
00735 
00736   template<typename _Tp>
00737     inline complex<_Tp>
00738     exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
00739 #else
00740   template<typename _Tp>
00741     inline complex<_Tp>
00742     exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
00743 #endif
00744 
00745   
00746   
00747   template<typename _Tp>
00748     inline complex<_Tp>
00749     __complex_log(const complex<_Tp>& __z)
00750     { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
00751 
00752 #if _GLIBCXX_USE_C99_COMPLEX
00753   inline __complex__ float
00754   __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
00755 
00756   inline __complex__ double
00757   __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
00758 
00759   inline __complex__ long double
00760   __complex_log(const __complex__ long double& __z)
00761   { return __builtin_clogl(__z); }
00762 
00763   template<typename _Tp>
00764     inline complex<_Tp>
00765     log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
00766 #else
00767   template<typename _Tp>
00768     inline complex<_Tp>
00769     log(const complex<_Tp>& __z) { return __complex_log(__z); }
00770 #endif
00771 
00772   template<typename _Tp>
00773     inline complex<_Tp>
00774     log10(const complex<_Tp>& __z)
00775     { return std::log(__z) / log(_Tp(10.0)); }
00776 
00777   
00778   template<typename _Tp>
00779     inline complex<_Tp>
00780     __complex_sin(const complex<_Tp>& __z)
00781     {
00782       const _Tp __x = __z.real();
00783       const _Tp __y = __z.imag();
00784       return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 
00785     }
00786 
00787 #if _GLIBCXX_USE_C99_COMPLEX
00788   inline __complex__ float
00789   __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
00790 
00791   inline __complex__ double
00792   __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
00793 
00794   inline __complex__ long double
00795   __complex_sin(const __complex__ long double& __z)
00796   { return __builtin_csinl(__z); }
00797 
00798   template<typename _Tp>
00799     inline complex<_Tp>
00800     sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
00801 #else
00802   template<typename _Tp>
00803     inline complex<_Tp>
00804     sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
00805 #endif
00806 
00807   
00808   template<typename _Tp>
00809     inline complex<_Tp>
00810     __complex_sinh(const complex<_Tp>& __z)
00811     {
00812       const _Tp __x = __z.real();
00813       const _Tp  __y = __z.imag();
00814       return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
00815     }
00816 
00817 #if _GLIBCXX_USE_C99_COMPLEX
00818   inline __complex__ float
00819   __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }      
00820 
00821   inline __complex__ double
00822   __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }      
00823 
00824   inline __complex__ long double
00825   __complex_sinh(const __complex__ long double& __z)
00826   { return __builtin_csinhl(__z); }      
00827 
00828   template<typename _Tp>
00829     inline complex<_Tp>
00830     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
00831 #else
00832   template<typename _Tp>
00833     inline complex<_Tp>
00834     sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
00835 #endif
00836 
00837   
00838   
00839   template<typename _Tp>
00840     complex<_Tp>
00841     __complex_sqrt(const complex<_Tp>& __z)
00842     {
00843       _Tp __x = __z.real();
00844       _Tp __y = __z.imag();
00845 
00846       if (__x == _Tp())
00847         {
00848           _Tp __t = sqrt(abs(__y) / 2);
00849           return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
00850         }
00851       else
00852         {
00853           _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
00854           _Tp __u = __t / 2;
00855           return __x > _Tp()
00856             ? complex<_Tp>(__u, __y / __t)
00857             : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
00858         }
00859     }
00860 
00861 #if _GLIBCXX_USE_C99_COMPLEX
00862   inline __complex__ float
00863   __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
00864 
00865   inline __complex__ double
00866   __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
00867 
00868   inline __complex__ long double
00869   __complex_sqrt(const __complex__ long double& __z)
00870   { return __builtin_csqrtl(__z); }
00871 
00872   template<typename _Tp>
00873     inline complex<_Tp>
00874     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
00875 #else
00876   template<typename _Tp>
00877     inline complex<_Tp>
00878     sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
00879 #endif
00880 
00881   
00882   
00883   template<typename _Tp>
00884     inline complex<_Tp>
00885     __complex_tan(const complex<_Tp>& __z)
00886     { return std::sin(__z) / std::cos(__z); }
00887 
00888 #if _GLIBCXX_USE_C99_COMPLEX
00889   inline __complex__ float
00890   __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
00891 
00892   inline __complex__ double
00893   __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
00894 
00895   inline __complex__ long double
00896   __complex_tan(const __complex__ long double& __z)
00897   { return __builtin_ctanl(__z); }
00898 
00899   template<typename _Tp>
00900     inline complex<_Tp>
00901     tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
00902 #else
00903   template<typename _Tp>
00904     inline complex<_Tp>
00905     tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
00906 #endif
00907 
00908 
00909   
00910   
00911   template<typename _Tp>
00912     inline complex<_Tp>
00913     __complex_tanh(const complex<_Tp>& __z)
00914     { return std::sinh(__z) / std::cosh(__z); }
00915 
00916 #if _GLIBCXX_USE_C99_COMPLEX
00917   inline __complex__ float
00918   __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
00919 
00920   inline __complex__ double
00921   __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
00922 
00923   inline __complex__ long double
00924   __complex_tanh(const __complex__ long double& __z)
00925   { return __builtin_ctanhl(__z); }
00926 
00927   template<typename _Tp>
00928     inline complex<_Tp>
00929     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
00930 #else
00931   template<typename _Tp>
00932     inline complex<_Tp>
00933     tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
00934 #endif
00935 
00936 
00937   
00938   
00939   
00940   template<typename _Tp>
00941     inline complex<_Tp>
00942     pow(const complex<_Tp>& __z, int __n)
00943     { return std::__pow_helper(__z, __n); }
00944 
00945   template<typename _Tp>
00946     complex<_Tp>
00947     pow(const complex<_Tp>& __x, const _Tp& __y)
00948     {
00949 #ifndef _GLIBCXX_USE_C99_COMPLEX
00950       if (__x == _Tp())
00951     return _Tp();
00952 #endif
00953       if (__x.imag() == _Tp() && __x.real() > _Tp())
00954         return pow(__x.real(), __y);
00955 
00956       complex<_Tp> __t = std::log(__x);
00957       return std::polar(exp(__y * __t.real()), __y * __t.imag());
00958     }
00959 
00960   template<typename _Tp>
00961     inline complex<_Tp>
00962     __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
00963     { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
00964 
00965 #if _GLIBCXX_USE_C99_COMPLEX
00966   inline __complex__ float
00967   __complex_pow(__complex__ float __x, __complex__ float __y)
00968   { return __builtin_cpowf(__x, __y); }
00969 
00970   inline __complex__ double
00971   __complex_pow(__complex__ double __x, __complex__ double __y)
00972   { return __builtin_cpow(__x, __y); }
00973 
00974   inline __complex__ long double
00975   __complex_pow(const __complex__ long double& __x,
00976         const __complex__ long double& __y)
00977   { return __builtin_cpowl(__x, __y); }
00978 
00979   template<typename _Tp>
00980     inline complex<_Tp>
00981     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
00982     { return __complex_pow(__x.__rep(), __y.__rep()); }
00983 #else
00984   template<typename _Tp>
00985     inline complex<_Tp>
00986     pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
00987     { return __complex_pow(__x, __y); }
00988 #endif
00989 
00990   template<typename _Tp>
00991     inline complex<_Tp>
00992     pow(const _Tp& __x, const complex<_Tp>& __y)
00993     {
00994       return __x > _Tp() ? std::polar(pow(__x, __y.real()),
00995                       __y.imag() * log(__x))
00996                      : std::pow(complex<_Tp>(__x, _Tp()), __y);
00997     }
00998 
00999   
01000   
01001   template<>
01002     struct complex<float>
01003     {
01004       typedef float value_type;
01005       typedef __complex__ float _ComplexT;
01006 
01007       complex(_ComplexT __z) : _M_value(__z) { }
01008 
01009       complex(float = 0.0f, float = 0.0f);
01010 
01011       explicit complex(const complex<double>&);
01012       explicit complex(const complex<long double>&);
01013 
01014       float& real();
01015       const float& real() const;
01016       float& imag();
01017       const float& imag() const;
01018 
01019       complex<float>& operator=(float);
01020       complex<float>& operator+=(float);
01021       complex<float>& operator-=(float);
01022       complex<float>& operator*=(float);
01023       complex<float>& operator/=(float);
01024 
01025       
01026       
01027       
01028       template<typename _Tp>
01029         complex<float>&operator=(const complex<_Tp>&);
01030       template<typename _Tp>
01031         complex<float>& operator+=(const complex<_Tp>&);
01032       template<class _Tp>
01033         complex<float>& operator-=(const complex<_Tp>&);
01034       template<class _Tp>
01035         complex<float>& operator*=(const complex<_Tp>&);
01036       template<class _Tp>
01037         complex<float>&operator/=(const complex<_Tp>&);
01038 
01039       const _ComplexT& __rep() const { return _M_value; }
01040 
01041     private:
01042       _ComplexT _M_value;
01043     };
01044 
01045   inline float&
01046   complex<float>::real()
01047   { return __real__ _M_value; }
01048 
01049   inline const float&
01050   complex<float>::real() const
01051   { return __real__ _M_value; }
01052 
01053   inline float&
01054   complex<float>::imag()
01055   { return __imag__ _M_value; }
01056 
01057   inline const float&
01058   complex<float>::imag() const
01059   { return __imag__ _M_value; }
01060 
01061   inline
01062   complex<float>::complex(float r, float i)
01063   {
01064     __real__ _M_value = r;
01065     __imag__ _M_value = i;
01066   }
01067 
01068   inline complex<float>&
01069   complex<float>::operator=(float __f)
01070   {
01071     __real__ _M_value = __f;
01072     __imag__ _M_value = 0.0f;
01073     return *this;
01074   }
01075 
01076   inline complex<float>&
01077   complex<float>::operator+=(float __f)
01078   {
01079     __real__ _M_value += __f;
01080     return *this;
01081   }
01082 
01083   inline complex<float>&
01084   complex<float>::operator-=(float __f)
01085   {
01086     __real__ _M_value -= __f;
01087     return *this;
01088   }
01089 
01090   inline complex<float>&
01091   complex<float>::operator*=(float __f)
01092   {
01093     _M_value *= __f;
01094     return *this;
01095   }
01096 
01097   inline complex<float>&
01098   complex<float>::operator/=(float __f)
01099   {
01100     _M_value /= __f;
01101     return *this;
01102   }
01103 
01104   template<typename _Tp>
01105   inline complex<float>&
01106   complex<float>::operator=(const complex<_Tp>& __z)
01107   {
01108     __real__ _M_value = __z.real();
01109     __imag__ _M_value = __z.imag();
01110     return *this;
01111   }
01112 
01113   template<typename _Tp>
01114   inline complex<float>&
01115   complex<float>::operator+=(const complex<_Tp>& __z)
01116   {
01117     __real__ _M_value += __z.real();
01118     __imag__ _M_value += __z.imag();
01119     return *this;
01120   }
01121     
01122   template<typename _Tp>
01123     inline complex<float>&
01124     complex<float>::operator-=(const complex<_Tp>& __z)
01125     {
01126      __real__ _M_value -= __z.real();
01127      __imag__ _M_value -= __z.imag();
01128      return *this;
01129     } 
01130 
01131   template<typename _Tp>
01132     inline complex<float>&
01133     complex<float>::operator*=(const complex<_Tp>& __z)
01134     {
01135       _ComplexT __t;
01136       __real__ __t = __z.real();
01137       __imag__ __t = __z.imag();
01138       _M_value *= __t;
01139       return *this;
01140     }
01141 
01142   template<typename _Tp>
01143     inline complex<float>&
01144     complex<float>::operator/=(const complex<_Tp>& __z)
01145     {
01146       _ComplexT __t;
01147       __real__ __t = __z.real();
01148       __imag__ __t = __z.imag();
01149       _M_value /= __t;
01150       return *this;
01151     }
01152 
01153   
01154   
01155   template<>
01156     struct complex<double>
01157     {
01158       typedef double value_type;
01159       typedef __complex__ double _ComplexT;
01160 
01161       complex(_ComplexT __z) : _M_value(__z) { }
01162 
01163       complex(double = 0.0, double = 0.0);
01164 
01165       complex(const complex<float>&);
01166       explicit complex(const complex<long double>&);
01167 
01168       double& real();
01169       const double& real() const;
01170       double& imag();
01171       const double& imag() const;
01172 
01173       complex<double>& operator=(double);
01174       complex<double>& operator+=(double);
01175       complex<double>& operator-=(double);
01176       complex<double>& operator*=(double);
01177       complex<double>& operator/=(double);
01178 
01179       
01180       
01181       template<typename _Tp>
01182         complex<double>& operator=(const complex<_Tp>&);
01183       template<typename _Tp>
01184         complex<double>& operator+=(const complex<_Tp>&);
01185       template<typename _Tp>
01186         complex<double>& operator-=(const complex<_Tp>&);
01187       template<typename _Tp>
01188         complex<double>& operator*=(const complex<_Tp>&);
01189       template<typename _Tp>
01190         complex<double>& operator/=(const complex<_Tp>&);
01191 
01192       const _ComplexT& __rep() const { return _M_value; }
01193 
01194     private:
01195       _ComplexT _M_value;
01196     };
01197 
01198   inline double&
01199   complex<double>::real()
01200   { return __real__ _M_value; }
01201 
01202   inline const double&
01203   complex<double>::real() const
01204   { return __real__ _M_value; }
01205 
01206   inline double&
01207   complex<double>::imag()
01208   { return __imag__ _M_value; }
01209 
01210   inline const double&
01211   complex<double>::imag() const
01212   { return __imag__ _M_value; }
01213 
01214   inline
01215   complex<double>::complex(double __r, double __i)
01216   {
01217     __real__ _M_value = __r;
01218     __imag__ _M_value = __i;
01219   }
01220 
01221   inline complex<double>&
01222   complex<double>::operator=(double __d)
01223   {
01224     __real__ _M_value = __d;
01225     __imag__ _M_value = 0.0;
01226     return *this;
01227   }
01228 
01229   inline complex<double>&
01230   complex<double>::operator+=(double __d)
01231   {
01232     __real__ _M_value += __d;
01233     return *this;
01234   }
01235 
01236   inline complex<double>&
01237   complex<double>::operator-=(double __d)
01238   {
01239     __real__ _M_value -= __d;
01240     return *this;
01241   }
01242 
01243   inline complex<double>&
01244   complex<double>::operator*=(double __d)
01245   {
01246     _M_value *= __d;
01247     return *this;
01248   }
01249 
01250   inline complex<double>&
01251   complex<double>::operator/=(double __d)
01252   {
01253     _M_value /= __d;
01254     return *this;
01255   }
01256 
01257   template<typename _Tp>
01258     inline complex<double>&
01259     complex<double>::operator=(const complex<_Tp>& __z)
01260     {
01261       __real__ _M_value = __z.real();
01262       __imag__ _M_value = __z.imag();
01263       return *this;
01264     }
01265     
01266   template<typename _Tp>
01267     inline complex<double>&
01268     complex<double>::operator+=(const complex<_Tp>& __z)
01269     {
01270       __real__ _M_value += __z.real();
01271       __imag__ _M_value += __z.imag();
01272       return *this;
01273     }
01274 
01275   template<typename _Tp>
01276     inline complex<double>&
01277     complex<double>::operator-=(const complex<_Tp>& __z)
01278     {
01279       __real__ _M_value -= __z.real();
01280       __imag__ _M_value -= __z.imag();
01281       return *this;
01282     }
01283 
01284   template<typename _Tp>
01285     inline complex<double>&
01286     complex<double>::operator*=(const complex<_Tp>& __z)
01287     {
01288       _ComplexT __t;
01289       __real__ __t = __z.real();
01290       __imag__ __t = __z.imag();
01291       _M_value *= __t;
01292       return *this;
01293     }
01294 
01295   template<typename _Tp>
01296     inline complex<double>&
01297     complex<double>::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   
01307   
01308   template<>
01309     struct complex<long double>
01310     {
01311       typedef long double value_type;
01312       typedef __complex__ long double _ComplexT;
01313 
01314       complex(_ComplexT __z) : _M_value(__z) { }
01315 
01316       complex(long double = 0.0L, long double = 0.0L);
01317 
01318       complex(const complex<float>&);
01319       complex(const complex<double>&);
01320 
01321       long double& real();
01322       const long double& real() const;
01323       long double& imag();
01324       const long double& imag() const;
01325 
01326       complex<long double>& operator= (long double);
01327       complex<long double>& operator+= (long double);
01328       complex<long double>& operator-= (long double);
01329       complex<long double>& operator*= (long double);
01330       complex<long double>& operator/= (long double);
01331 
01332       
01333       
01334       template<typename _Tp>
01335         complex<long double>& operator=(const complex<_Tp>&);
01336       template<typename _Tp>
01337         complex<long double>& operator+=(const complex<_Tp>&);
01338       template<typename _Tp>
01339         complex<long double>& operator-=(const complex<_Tp>&);
01340       template<typename _Tp>
01341         complex<long double>& operator*=(const complex<_Tp>&);
01342       template<typename _Tp>
01343         complex<long double>& operator/=(const complex<_Tp>&);
01344 
01345       const _ComplexT& __rep() const { return _M_value; }
01346 
01347     private:
01348       _ComplexT _M_value;
01349     };
01350 
01351   inline
01352   complex<long double>::complex(long double __r, long double __i)
01353   {
01354     __real__ _M_value = __r;
01355     __imag__ _M_value = __i;
01356   }
01357 
01358   inline long double&
01359   complex<long double>::real()
01360   { return __real__ _M_value; }
01361 
01362   inline const long double&
01363   complex<long double>::real() const
01364   { return __real__ _M_value; }
01365 
01366   inline long double&
01367   complex<long double>::imag()
01368   { return __imag__ _M_value; }
01369 
01370   inline const long double&
01371   complex<long double>::imag() const
01372   { return __imag__ _M_value; }
01373 
01374   inline complex<long double>&   
01375   complex<long double>::operator=(long double __r)
01376   {
01377     __real__ _M_value = __r;
01378     __imag__ _M_value = 0.0L;
01379     return *this;
01380   }
01381 
01382   inline complex<long double>&
01383   complex<long double>::operator+=(long double __r)
01384   {
01385     __real__ _M_value += __r;
01386     return *this;
01387   }
01388 
01389   inline complex<long double>&
01390   complex<long double>::operator-=(long double __r)
01391   {
01392     __real__ _M_value -= __r;
01393     return *this;
01394   }
01395 
01396   inline complex<long double>&
01397   complex<long double>::operator*=(long double __r)
01398   {
01399     _M_value *= __r;
01400     return *this;
01401   }
01402 
01403   inline complex<long double>&
01404   complex<long double>::operator/=(long double __r)
01405   {
01406     _M_value /= __r;
01407     return *this;
01408   }
01409 
01410   template<typename _Tp>
01411     inline complex<long double>&
01412     complex<long double>::operator=(const complex<_Tp>& __z)
01413     {
01414       __real__ _M_value = __z.real();
01415       __imag__ _M_value = __z.imag();
01416       return *this;
01417     }
01418 
01419   template<typename _Tp>
01420     inline complex<long double>&
01421     complex<long double>::operator+=(const complex<_Tp>& __z)
01422     {
01423       __real__ _M_value += __z.real();
01424       __imag__ _M_value += __z.imag();
01425       return *this;
01426     }
01427 
01428   template<typename _Tp>
01429     inline complex<long double>&
01430     complex<long double>::operator-=(const complex<_Tp>& __z)
01431     {
01432       __real__ _M_value -= __z.real();
01433       __imag__ _M_value -= __z.imag();
01434       return *this;
01435     }
01436     
01437   template<typename _Tp>
01438     inline complex<long double>&
01439     complex<long double>::operator*=(const complex<_Tp>& __z)
01440     {
01441       _ComplexT __t;
01442       __real__ __t = __z.real();
01443       __imag__ __t = __z.imag();
01444       _M_value *= __t;
01445       return *this;
01446     }
01447 
01448   template<typename _Tp>
01449     inline complex<long double>&
01450     complex<long double>::operator/=(const complex<_Tp>& __z)
01451     {
01452       _ComplexT __t;
01453       __real__ __t = __z.real();
01454       __imag__ __t = __z.imag();
01455       _M_value /= __t;
01456       return *this;
01457     }
01458 
01459   
01460   
01461   
01462   
01463   inline
01464   complex<float>::complex(const complex<double>& __z)
01465   : _M_value(__z.__rep()) { }
01466 
01467   inline
01468   complex<float>::complex(const complex<long double>& __z)
01469   : _M_value(__z.__rep()) { }
01470 
01471   inline
01472   complex<double>::complex(const complex<float>& __z) 
01473   : _M_value(__z.__rep()) { }
01474 
01475   inline
01476   complex<double>::complex(const complex<long double>& __z)
01477     : _M_value(__z.__rep()) { }
01478 
01479   inline
01480   complex<long double>::complex(const complex<float>& __z)
01481   : _M_value(__z.__rep()) { }
01482 
01483   inline
01484   complex<long double>::complex(const complex<double>& __z)
01485   : _M_value(__z.__rep()) { }
01486 } 
01487 
01488 #endif