libstdc++
complex
Go to the documentation of this file.
00001 // TR1 complex -*- C++ -*-
00002 
00003 // Copyright (C) 2006-2015 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 tr1/complex
00026  *  This is a TR1 C++ Library header. 
00027  */
00028 
00029 #ifndef _GLIBCXX_TR1_COMPLEX
00030 #define _GLIBCXX_TR1_COMPLEX 1
00031 
00032 #pragma GCC system_header
00033 
00034 #include <complex>
00035 
00036 namespace std _GLIBCXX_VISIBILITY(default)
00037 {
00038 namespace tr1
00039 {
00040 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00041 
00042   /**
00043    * @addtogroup complex_numbers
00044    * @{
00045    */
00046 
00047 #if __cplusplus >= 201103L
00048   using std::acos;
00049   using std::asin;
00050   using std::atan;
00051 #else
00052   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
00053   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
00054   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
00055 #endif
00056 
00057   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
00058   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
00059   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
00060 
00061   // The std::fabs return type in C++0x mode is different (just _Tp).
00062   template<typename _Tp> std::complex<_Tp> fabs(const std::complex<_Tp>&);
00063 
00064 #if __cplusplus < 201103L
00065   template<typename _Tp>
00066     inline std::complex<_Tp>
00067     __complex_acos(const std::complex<_Tp>& __z)
00068     {
00069       const std::complex<_Tp> __t = std::tr1::asin(__z);
00070       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
00071       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
00072     }
00073 
00074 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00075   inline __complex__ float
00076   __complex_acos(__complex__ float __z)
00077   { return __builtin_cacosf(__z); }
00078 
00079   inline __complex__ double
00080   __complex_acos(__complex__ double __z)
00081   { return __builtin_cacos(__z); }
00082 
00083   inline __complex__ long double
00084   __complex_acos(const __complex__ long double& __z)
00085   { return __builtin_cacosl(__z); }
00086 
00087   template<typename _Tp>
00088     inline std::complex<_Tp>
00089     acos(const std::complex<_Tp>& __z)
00090     { return __complex_acos(__z.__rep()); }
00091 #else
00092   /// acos(__z) [8.1.2].
00093   //  Effects:  Behaves the same as C99 function cacos, defined
00094   //            in subclause 7.3.5.1.
00095   template<typename _Tp>
00096     inline std::complex<_Tp>
00097     acos(const std::complex<_Tp>& __z)
00098     { return __complex_acos(__z); }
00099 #endif
00100 
00101   template<typename _Tp>
00102     inline std::complex<_Tp>
00103     __complex_asin(const std::complex<_Tp>& __z)
00104     {
00105       std::complex<_Tp> __t(-__z.imag(), __z.real());
00106       __t = std::tr1::asinh(__t);
00107       return std::complex<_Tp>(__t.imag(), -__t.real());
00108     }
00109 
00110 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00111   inline __complex__ float
00112   __complex_asin(__complex__ float __z)
00113   { return __builtin_casinf(__z); }
00114 
00115   inline __complex__ double
00116   __complex_asin(__complex__ double __z)
00117   { return __builtin_casin(__z); }
00118 
00119   inline __complex__ long double
00120   __complex_asin(const __complex__ long double& __z)
00121   { return __builtin_casinl(__z); }
00122 
00123   template<typename _Tp>
00124     inline std::complex<_Tp>
00125     asin(const std::complex<_Tp>& __z)
00126     { return __complex_asin(__z.__rep()); }
00127 #else
00128   /// asin(__z) [8.1.3].
00129   //  Effects:  Behaves the same as C99 function casin, defined
00130   //            in subclause 7.3.5.2.
00131   template<typename _Tp>
00132     inline std::complex<_Tp>
00133     asin(const std::complex<_Tp>& __z)
00134     { return __complex_asin(__z); }
00135 #endif
00136   
00137   template<typename _Tp>
00138     std::complex<_Tp>
00139     __complex_atan(const std::complex<_Tp>& __z)
00140     {
00141       const _Tp __r2 = __z.real() * __z.real();
00142       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
00143 
00144       _Tp __num = __z.imag() + _Tp(1.0);
00145       _Tp __den = __z.imag() - _Tp(1.0);
00146 
00147       __num = __r2 + __num * __num;
00148       __den = __r2 + __den * __den;
00149 
00150       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
00151                                _Tp(0.25) * log(__num / __den));
00152     }
00153 
00154 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00155   inline __complex__ float
00156   __complex_atan(__complex__ float __z)
00157   { return __builtin_catanf(__z); }
00158 
00159   inline __complex__ double
00160   __complex_atan(__complex__ double __z)
00161   { return __builtin_catan(__z); }
00162 
00163   inline __complex__ long double
00164   __complex_atan(const __complex__ long double& __z)
00165   { return __builtin_catanl(__z); }
00166 
00167   template<typename _Tp>
00168     inline std::complex<_Tp>
00169     atan(const std::complex<_Tp>& __z)
00170     { return __complex_atan(__z.__rep()); }
00171 #else
00172   /// atan(__z) [8.1.4].
00173   //  Effects:  Behaves the same as C99 function catan, defined
00174   //            in subclause 7.3.5.3.
00175   template<typename _Tp>
00176     inline std::complex<_Tp>
00177     atan(const std::complex<_Tp>& __z)
00178     { return __complex_atan(__z); }
00179 #endif
00180 
00181 #endif // C++11
00182 
00183   template<typename _Tp>
00184     std::complex<_Tp>
00185     __complex_acosh(const std::complex<_Tp>& __z)
00186     {
00187       // Kahan's formula.
00188       return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
00189                                  + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
00190     }
00191 
00192 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00193   inline __complex__ float
00194   __complex_acosh(__complex__ float __z)
00195   { return __builtin_cacoshf(__z); }
00196 
00197   inline __complex__ double
00198   __complex_acosh(__complex__ double __z)
00199   { return __builtin_cacosh(__z); }
00200 
00201   inline __complex__ long double
00202   __complex_acosh(const __complex__ long double& __z)
00203   { return __builtin_cacoshl(__z); }
00204 
00205   template<typename _Tp>
00206     inline std::complex<_Tp>
00207     acosh(const std::complex<_Tp>& __z)
00208     { return __complex_acosh(__z.__rep()); }
00209 #else
00210   /// acosh(__z) [8.1.5].
00211   //  Effects:  Behaves the same as C99 function cacosh, defined
00212   //            in subclause 7.3.6.1.
00213   template<typename _Tp>
00214     inline std::complex<_Tp>
00215     acosh(const std::complex<_Tp>& __z)
00216     { return __complex_acosh(__z); }
00217 #endif
00218 
00219   template<typename _Tp>
00220     std::complex<_Tp>
00221     __complex_asinh(const std::complex<_Tp>& __z)
00222     {
00223       std::complex<_Tp> __t((__z.real() - __z.imag())
00224                             * (__z.real() + __z.imag()) + _Tp(1.0),
00225                             _Tp(2.0) * __z.real() * __z.imag());
00226       __t = std::sqrt(__t);
00227 
00228       return std::log(__t + __z);
00229     }
00230 
00231 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00232   inline __complex__ float
00233   __complex_asinh(__complex__ float __z)
00234   { return __builtin_casinhf(__z); }
00235 
00236   inline __complex__ double
00237   __complex_asinh(__complex__ double __z)
00238   { return __builtin_casinh(__z); }
00239 
00240   inline __complex__ long double
00241   __complex_asinh(const __complex__ long double& __z)
00242   { return __builtin_casinhl(__z); }
00243 
00244   template<typename _Tp>
00245     inline std::complex<_Tp>
00246     asinh(const std::complex<_Tp>& __z)
00247     { return __complex_asinh(__z.__rep()); }
00248 #else
00249   /// asinh(__z) [8.1.6].
00250   //  Effects:  Behaves the same as C99 function casin, defined
00251   //            in subclause 7.3.6.2.
00252   template<typename _Tp>
00253     inline std::complex<_Tp>
00254     asinh(const std::complex<_Tp>& __z)
00255     { return __complex_asinh(__z); }
00256 #endif
00257 
00258   template<typename _Tp>
00259     std::complex<_Tp>
00260     __complex_atanh(const std::complex<_Tp>& __z)
00261     {
00262       const _Tp __i2 = __z.imag() * __z.imag();
00263       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
00264 
00265       _Tp __num = _Tp(1.0) + __z.real();
00266       _Tp __den = _Tp(1.0) - __z.real();
00267 
00268       __num = __i2 + __num * __num;
00269       __den = __i2 + __den * __den;
00270 
00271       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
00272                                _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
00273     }
00274 
00275 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00276   inline __complex__ float
00277   __complex_atanh(__complex__ float __z)
00278   { return __builtin_catanhf(__z); }
00279 
00280   inline __complex__ double
00281   __complex_atanh(__complex__ double __z)
00282   { return __builtin_catanh(__z); }
00283 
00284   inline __complex__ long double
00285   __complex_atanh(const __complex__ long double& __z)
00286   { return __builtin_catanhl(__z); }
00287 
00288   template<typename _Tp>
00289     inline std::complex<_Tp>
00290     atanh(const std::complex<_Tp>& __z)
00291     { return __complex_atanh(__z.__rep()); }
00292 #else
00293   /// atanh(__z) [8.1.7].
00294   //  Effects:  Behaves the same as C99 function catanh, defined
00295   //            in subclause 7.3.6.3.
00296   template<typename _Tp>
00297     inline std::complex<_Tp>
00298     atanh(const std::complex<_Tp>& __z)
00299     { return __complex_atanh(__z); }
00300 #endif
00301 
00302   template<typename _Tp>
00303     inline std::complex<_Tp>
00304     /// fabs(__z) [8.1.8].
00305     //  Effects:  Behaves the same as C99 function cabs, defined
00306     //            in subclause 7.3.8.1.
00307     fabs(const std::complex<_Tp>& __z)
00308     { return std::abs(__z); }
00309 
00310   /// Additional overloads [8.1.9].
00311 #if __cplusplus < 201103L
00312 
00313   template<typename _Tp>
00314     inline typename __gnu_cxx::__promote<_Tp>::__type
00315     arg(_Tp __x)
00316     {
00317       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
00318 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
00319       return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
00320                                : __type();
00321 #else
00322       return std::arg(std::complex<__type>(__x));
00323 #endif
00324     }
00325 
00326   template<typename _Tp>
00327     inline typename __gnu_cxx::__promote<_Tp>::__type
00328     imag(_Tp)
00329     { return _Tp(); }
00330 
00331   template<typename _Tp>
00332     inline typename __gnu_cxx::__promote<_Tp>::__type
00333     norm(_Tp __x)
00334     {
00335       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
00336       return __type(__x) * __type(__x);
00337     }
00338 
00339   template<typename _Tp>
00340     inline typename __gnu_cxx::__promote<_Tp>::__type
00341     real(_Tp __x)
00342     { return __x; }
00343 
00344 #endif
00345 
00346   template<typename _Tp, typename _Up>
00347     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00348     pow(const std::complex<_Tp>& __x, const _Up& __y)
00349     {
00350       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00351       return std::pow(std::complex<__type>(__x), __type(__y));
00352     }
00353 
00354   template<typename _Tp, typename _Up>
00355     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00356     pow(const _Tp& __x, const std::complex<_Up>& __y)
00357     {
00358       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00359       return std::pow(__type(__x), std::complex<__type>(__y));
00360     }
00361 
00362   template<typename _Tp, typename _Up>
00363     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00364     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
00365     {
00366       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00367       return std::pow(std::complex<__type>(__x),
00368                       std::complex<__type>(__y));
00369     }
00370 
00371   using std::arg;
00372 
00373   template<typename _Tp>
00374     inline std::complex<_Tp>
00375     conj(const std::complex<_Tp>& __z)
00376     { return std::conj(__z); }  
00377 
00378   template<typename _Tp>
00379     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
00380     conj(_Tp __x)
00381     { return __x; }
00382 
00383   using std::imag;
00384   using std::norm;
00385   using std::polar;
00386 
00387   template<typename _Tp, typename _Up>
00388     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00389     polar(const _Tp& __rho, const _Up& __theta)
00390     {
00391       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00392       return std::polar(__type(__rho), __type(__theta));
00393     }
00394 
00395   using std::real;
00396 
00397   template<typename _Tp>
00398     inline std::complex<_Tp>
00399     pow(const std::complex<_Tp>& __x, const _Tp& __y)
00400     { return std::pow(__x, __y); }
00401 
00402   template<typename _Tp>
00403     inline std::complex<_Tp>
00404     pow(const _Tp& __x, const std::complex<_Tp>& __y)
00405     { return std::pow(__x, __y); }
00406 
00407   template<typename _Tp>
00408     inline std::complex<_Tp>
00409     pow(const std::complex<_Tp>& __x, const std::complex<_Tp>& __y)
00410     { return std::pow(__x, __y); }
00411 
00412 // @} group complex_numbers
00413 
00414 _GLIBCXX_END_NAMESPACE_VERSION
00415 }
00416 }
00417 
00418 #endif // _GLIBCXX_TR1_COMPLEX