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