impl/complex

Go to the documentation of this file.
00001 // TR1 complex -*- C++ -*-
00002 
00003 // Copyright (C) 2007 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 2, 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 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 /** @file tr1_impl/complex
00031  *  This is an internal header file, included by other library headers.
00032  *  You should not attempt to use it directly.
00033  */
00034 
00035 namespace std
00036 {
00037 _GLIBCXX_BEGIN_NAMESPACE_TR1
00038 
00039   // Forward declarations.
00040   template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
00041   template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
00042   template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
00043 
00044   template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
00045   template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
00046   template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
00047 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
00048   // DR 595.
00049   template<typename _Tp> _Tp               fabs(const std::complex<_Tp>&);
00050 #else
00051   template<typename _Tp> std::complex<_Tp> fabs(const std::complex<_Tp>&);
00052 #endif
00053 
00054   /// acos(__z) [8.1.2].
00055   //  Effects:  Behaves the same as C99 function cacos, defined
00056   //            in subclause 7.3.5.1.
00057   template<typename _Tp>
00058     inline std::complex<_Tp>
00059     __complex_acos(const std::complex<_Tp>& __z)
00060     {
00061       const std::complex<_Tp> __t = std::_GLIBCXX_TR1 asin(__z);
00062       const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
00063       return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
00064     }
00065 
00066 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00067   inline __complex__ float
00068   __complex_acos(__complex__ float __z)
00069   { return __builtin_cacosf(__z); }
00070 
00071   inline __complex__ double
00072   __complex_acos(__complex__ double __z)
00073   { return __builtin_cacos(__z); }
00074 
00075   inline __complex__ long double
00076   __complex_acos(const __complex__ long double& __z)
00077   { return __builtin_cacosl(__z); }
00078 
00079   template<typename _Tp>
00080     inline std::complex<_Tp>
00081     acos(const std::complex<_Tp>& __z)
00082     { return __complex_acos(__z.__rep()); }
00083 #else
00084   template<typename _Tp>
00085     inline std::complex<_Tp>
00086     acos(const std::complex<_Tp>& __z)
00087     { return __complex_acos(__z); }
00088 #endif
00089 
00090   /// asin(__z) [8.1.3].
00091   //  Effects:  Behaves the same as C99 function casin, defined
00092   //            in subclause 7.3.5.2.
00093   template<typename _Tp>
00094     inline std::complex<_Tp>
00095     __complex_asin(const std::complex<_Tp>& __z)
00096     {
00097       std::complex<_Tp> __t(-__z.imag(), __z.real());
00098       __t = std::_GLIBCXX_TR1 asinh(__t);
00099       return std::complex<_Tp>(__t.imag(), -__t.real());
00100     }
00101 
00102 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00103   inline __complex__ float
00104   __complex_asin(__complex__ float __z)
00105   { return __builtin_casinf(__z); }
00106 
00107   inline __complex__ double
00108   __complex_asin(__complex__ double __z)
00109   { return __builtin_casin(__z); }
00110 
00111   inline __complex__ long double
00112   __complex_asin(const __complex__ long double& __z)
00113   { return __builtin_casinl(__z); }
00114 
00115   template<typename _Tp>
00116     inline std::complex<_Tp>
00117     asin(const std::complex<_Tp>& __z)
00118     { return __complex_asin(__z.__rep()); }
00119 #else
00120   template<typename _Tp>
00121     inline std::complex<_Tp>
00122     asin(const std::complex<_Tp>& __z)
00123     { return __complex_asin(__z); }
00124 #endif
00125   
00126   /// atan(__z) [8.1.4].
00127   //  Effects:  Behaves the same as C99 function catan, defined
00128   //            in subclause 7.3.5.3.
00129   template<typename _Tp>
00130     std::complex<_Tp>
00131     __complex_atan(const std::complex<_Tp>& __z)
00132     {
00133       const _Tp __r2 = __z.real() * __z.real();
00134       const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
00135 
00136       _Tp __num = __z.imag() + _Tp(1.0);
00137       _Tp __den = __z.imag() - _Tp(1.0);
00138 
00139       __num = __r2 + __num * __num;
00140       __den = __r2 + __den * __den;
00141 
00142       return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
00143                    _Tp(0.25) * log(__num / __den));
00144     }
00145 
00146 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00147   inline __complex__ float
00148   __complex_atan(__complex__ float __z)
00149   { return __builtin_catanf(__z); }
00150 
00151   inline __complex__ double
00152   __complex_atan(__complex__ double __z)
00153   { return __builtin_catan(__z); }
00154 
00155   inline __complex__ long double
00156   __complex_atan(const __complex__ long double& __z)
00157   { return __builtin_catanl(__z); }
00158 
00159   template<typename _Tp>
00160     inline std::complex<_Tp>
00161     atan(const std::complex<_Tp>& __z)
00162     { return __complex_atan(__z.__rep()); }
00163 #else
00164   template<typename _Tp>
00165     inline std::complex<_Tp>
00166     atan(const std::complex<_Tp>& __z)
00167     { return __complex_atan(__z); }
00168 #endif
00169 
00170   /// acosh(__z) [8.1.5].
00171   //  Effects:  Behaves the same as C99 function cacosh, defined
00172   //            in subclause 7.3.6.1.
00173   template<typename _Tp>
00174     std::complex<_Tp>
00175     __complex_acosh(const std::complex<_Tp>& __z)
00176     {
00177       std::complex<_Tp> __t((__z.real() - __z.imag())
00178                 * (__z.real() + __z.imag()) - _Tp(1.0),
00179                 _Tp(2.0) * __z.real() * __z.imag());
00180       __t = std::sqrt(__t);
00181 
00182       return std::log(__t + __z);
00183     }
00184 
00185 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00186   inline __complex__ float
00187   __complex_acosh(__complex__ float __z)
00188   { return __builtin_cacoshf(__z); }
00189 
00190   inline __complex__ double
00191   __complex_acosh(__complex__ double __z)
00192   { return __builtin_cacosh(__z); }
00193 
00194   inline __complex__ long double
00195   __complex_acosh(const __complex__ long double& __z)
00196   { return __builtin_cacoshl(__z); }
00197 
00198   template<typename _Tp>
00199     inline std::complex<_Tp>
00200     acosh(const std::complex<_Tp>& __z)
00201     { return __complex_acosh(__z.__rep()); }
00202 #else
00203   template<typename _Tp>
00204     inline std::complex<_Tp>
00205     acosh(const std::complex<_Tp>& __z)
00206     { return __complex_acosh(__z); }
00207 #endif
00208 
00209   /// asinh(__z) [8.1.6].
00210   //  Effects:  Behaves the same as C99 function casin, defined
00211   //            in subclause 7.3.6.2.
00212   template<typename _Tp>
00213     std::complex<_Tp>
00214     __complex_asinh(const std::complex<_Tp>& __z)
00215     {
00216       std::complex<_Tp> __t((__z.real() - __z.imag())
00217                 * (__z.real() + __z.imag()) + _Tp(1.0),
00218                 _Tp(2.0) * __z.real() * __z.imag());
00219       __t = std::sqrt(__t);
00220 
00221       return std::log(__t + __z);
00222     }
00223 
00224 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00225   inline __complex__ float
00226   __complex_asinh(__complex__ float __z)
00227   { return __builtin_casinhf(__z); }
00228 
00229   inline __complex__ double
00230   __complex_asinh(__complex__ double __z)
00231   { return __builtin_casinh(__z); }
00232 
00233   inline __complex__ long double
00234   __complex_asinh(const __complex__ long double& __z)
00235   { return __builtin_casinhl(__z); }
00236 
00237   template<typename _Tp>
00238     inline std::complex<_Tp>
00239     asinh(const std::complex<_Tp>& __z)
00240     { return __complex_asinh(__z.__rep()); }
00241 #else
00242   template<typename _Tp>
00243     inline std::complex<_Tp>
00244     asinh(const std::complex<_Tp>& __z)
00245     { return __complex_asinh(__z); }
00246 #endif
00247 
00248   /// atanh(__z) [8.1.7].
00249   //  Effects:  Behaves the same as C99 function catanh, defined
00250   //            in subclause 7.3.6.3.
00251   template<typename _Tp>
00252     std::complex<_Tp>
00253     __complex_atanh(const std::complex<_Tp>& __z)
00254     {
00255       const _Tp __i2 = __z.imag() * __z.imag();
00256       const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
00257 
00258       _Tp __num = _Tp(1.0) + __z.real();
00259       _Tp __den = _Tp(1.0) - __z.real();
00260 
00261       __num = __i2 + __num * __num;
00262       __den = __i2 + __den * __den;
00263 
00264       return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
00265                    _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
00266     }
00267 
00268 #if _GLIBCXX_USE_C99_COMPLEX_TR1
00269   inline __complex__ float
00270   __complex_atanh(__complex__ float __z)
00271   { return __builtin_catanhf(__z); }
00272 
00273   inline __complex__ double
00274   __complex_atanh(__complex__ double __z)
00275   { return __builtin_catanh(__z); }
00276 
00277   inline __complex__ long double
00278   __complex_atanh(const __complex__ long double& __z)
00279   { return __builtin_catanhl(__z); }
00280 
00281   template<typename _Tp>
00282     inline std::complex<_Tp>
00283     atanh(const std::complex<_Tp>& __z)
00284     { return __complex_atanh(__z.__rep()); }
00285 #else
00286   template<typename _Tp>
00287     inline std::complex<_Tp>
00288     atanh(const std::complex<_Tp>& __z)
00289     { return __complex_atanh(__z); }
00290 #endif
00291 
00292   /// fabs(__z) [8.1.8].
00293   //  Effects:  Behaves the same as C99 function cabs, defined
00294   //            in subclause 7.3.8.1.
00295   template<typename _Tp>
00296 #ifdef _GLIBCXX_INCLUDE_AS_CXX0X
00297     inline _Tp
00298 #else
00299     inline std::complex<_Tp>
00300 #endif
00301     fabs(const std::complex<_Tp>& __z)
00302     { return std::abs(__z); }
00303 
00304 
00305 #if (defined(_GLIBCXX_INCLUDE_AS_CXX0X) \
00306      || (defined(_GLIBCXX_INCLUDE_AS_TR1) \
00307      && !defined(__GXX_EXPERIMENTAL_CXX0X__)))
00308 
00309   /// Additional overloads [8.1.9].
00310   template<typename _Tp>
00311     inline typename __gnu_cxx::__promote<_Tp>::__type
00312     arg(_Tp __x)
00313     {
00314       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
00315       return std::arg(std::complex<__type>(__x));
00316     }
00317 
00318   template<typename _Tp>
00319     inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
00320     conj(_Tp __x)
00321     { return __x; }
00322 
00323   template<typename _Tp>
00324     inline typename __gnu_cxx::__promote<_Tp>::__type
00325     imag(_Tp)
00326     { return _Tp(); }
00327 
00328   template<typename _Tp>
00329     inline typename __gnu_cxx::__promote<_Tp>::__type
00330     norm(_Tp __x)
00331     {
00332       typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
00333       return __type(__x) * __type(__x);
00334     }
00335 
00336   template<typename _Tp, typename _Up>
00337     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00338     polar(const _Tp& __rho, const _Up& __theta)
00339     {
00340       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00341       return std::polar(__type(__rho), __type(__theta));
00342     }
00343   
00344   template<typename _Tp, typename _Up>
00345     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00346     pow(const std::complex<_Tp>& __x, const _Up& __y)
00347     {
00348       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00349       return std::pow(std::complex<__type>(__x), __type(__y));
00350     }
00351 
00352   template<typename _Tp, typename _Up>
00353     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00354     pow(const _Tp& __x, const std::complex<_Up>& __y)
00355     {
00356       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00357       return std::pow(__type(__x), std::complex<__type>(__y));
00358     }
00359 
00360   template<typename _Tp, typename _Up>
00361     inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
00362     pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
00363     {
00364       typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
00365       return std::pow(std::complex<__type>(__x),
00366               std::complex<__type>(__y));
00367     }
00368 
00369   template<typename _Tp>
00370     inline typename __gnu_cxx::__promote<_Tp>::__type
00371     real(_Tp __x)
00372     { return __x; }
00373 
00374 #endif
00375 
00376 _GLIBCXX_END_NAMESPACE_TR1
00377 }

Generated on Sat Dec 12 09:40:09 2009 for libstdc++ by  doxygen 1.5.6