os/ossrv/stdcpp/src/complex_exp.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2  * © Portions copyright (c) 2006-2007 Nokia Corporation.  All rights reserved.
     3  * Copyright (c) 1999
     4  * Silicon Graphics Computer Systems, Inc.
     5  *
     6  * Copyright (c) 1999 
     7  * Boris Fomitchev
     8  *
     9  * This material is provided "as is", with absolutely no warranty expressed
    10  * or implied. Any use is at your own risk.
    11  *
    12  * Permission to use or copy this software for any purpose is hereby granted 
    13  * without fee, provided the above notices are retained on all copies.
    14  * Permission to modify the code and to distribute modified code is granted,
    15  * provided the above notices are retained, and a notice that the code was
    16  * modified is included with the above copyright notice.
    17  *
    18  */ 
    19 
    20 # include "stlport_prefix.h"
    21 // exp, log, pow for complex<float>, complex<double>, and complex<long double>
    22 
    23 #include <numeric>
    24 #include "complex_impl.h"
    25 
    26 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
    27 #include "libstdcppwsd.h"
    28 # endif
    29 
    30 _STLP_BEGIN_NAMESPACE
    31 
    32 //----------------------------------------------------------------------
    33 // exp
    34 
    35 _STLP_EXP_DECLSPEC complex<float>  _STLP_CALL
    36 exp(const complex<float>& z)
    37 {
    38   float expx = _STLP_EXPF(z._M_re);
    39   return complex<float>(expx * _STLP_COSF(z._M_im),
    40                         expx * _STLP_SINF(z._M_im));
    41 }
    42 
    43 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL exp(const complex<double>& z)
    44 {
    45   double expx = _STLP_EXP(z._M_re);
    46   return complex<double>(expx * _STLP_COS(z._M_im),
    47                          expx * _STLP_SIN(z._M_im));
    48 }
    49 
    50 # ifndef _STLP_NO_LONG_DOUBLE
    51 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL exp(const complex<long double>& z)
    52 {
    53   long double expx = _STLP_EXPL(z._M_re);
    54   return complex<long double>(expx * _STLP_COSL(z._M_im),
    55                               expx * _STLP_SINL(z._M_im));
    56 }
    57 # endif
    58 
    59 //----------------------------------------------------------------------
    60 // log10
    61 
    62 _STLP_EXP_DECLSPEC complex<float> _STLP_CALL log10(const complex<float>& z)
    63 {
    64   complex<float> r;
    65   
    66 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
    67   get_complex_exp_float_ln10_inv() = 1.f / _STLP_LOGF(10.f);  
    68   r._M_im = _STLP_ATAN2F(z._M_im, z._M_re) * get_complex_exp_float_ln10_inv();  	
    69 # else
    70   static float ln10_inv = 1.f / _STLP_LOGF(10.f);
    71   r._M_im = _STLP_ATAN2F(z._M_im, z._M_re) * ln10_inv;  
    72 # endif //__LIBSTD_CPP_SYMBIAN32_WSD__
    73   
    74   r._M_re = _STLP_LOG10F(_STLP_HYPOTF(z._M_re, z._M_im));
    75   return r;
    76 }
    77 
    78 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL log10(const complex<double>& z)
    79 {
    80   complex<double> r;
    81 
    82 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
    83 	get_complex_exp_double_ln10_inv() = 1. / _STLP_LOG(10.);
    84 	r._M_im = _STLP_ATAN2(z._M_im, z._M_re) * get_complex_exp_double_ln10_inv();
    85 # else
    86 	static double ln10_inv = 1. / _STLP_LOG(10.);
    87 	r._M_im = _STLP_ATAN2(z._M_im, z._M_re) * ln10_inv;
    88 # endif //__LIBSTD_CPP_SYMBIAN32_WSD__
    89   
    90   r._M_re = _STLP_LOG10(_STLP_HYPOT(z._M_re, z._M_im));
    91   return r;
    92 }
    93 
    94 #ifndef _STLP_NO_LONG_DOUBLE
    95 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL log10(const complex<long double>& z)
    96 {
    97   complex<long double> result;
    98 #if defined(__LIBSTD_CPP_SYMBIAN32_WSD__) || defined(_STLP_LIBSTD_CPP_NO_STATIC_VAR_)
    99   get_complex_exp_long_double_ln10_inv() = 1.l / _STLP_LOGL(10.l);
   100   result._M_im = _STLP_ATAN2L(z._M_im, z._M_re) * get_complex_exp_long_double_ln10_inv();
   101 # else
   102   static long double ln10_inv = 1.l / _STLP_LOGL(10.l);
   103   result._M_im = _STLP_ATAN2L(z._M_im, z._M_re) * ln10_inv;
   104 # endif //__LIBSTD_CPP_SYMBIAN32_WSD__
   105  
   106     result._M_re = _STLP_LOG10L(_STLP_HYPOTL(z._M_re, z._M_im));
   107   return result;
   108 }
   109 # endif
   110 
   111 //----------------------------------------------------------------------
   112 // log
   113 
   114 _STLP_EXP_DECLSPEC complex<float> _STLP_CALL log(const complex<float>& z)
   115 {
   116   complex<float> r;
   117 
   118   r._M_im = _STLP_ATAN2F(z._M_im, z._M_re);
   119   r._M_re = _STLP_LOGF(_STLP_HYPOTF(z._M_re, z._M_im));
   120   return r;
   121 }
   122 
   123 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL log(const complex<double>& z)
   124 {
   125   complex<double> r;
   126 
   127   r._M_im = _STLP_ATAN2(z._M_im, z._M_re);
   128   r._M_re = _STLP_LOG(_STLP_HYPOT(z._M_re, z._M_im));
   129   return r;
   130 }
   131 
   132 #ifndef _STLP_NO_LONG_DOUBLE
   133 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL log(const complex<long double>& z)
   134 {
   135   complex<long double> result;
   136 
   137   result._M_im = _STLP_ATAN2L(z._M_im, z._M_re);
   138   result._M_re = _STLP_LOGL(_STLP_HYPOTL(z._M_re, z._M_im));
   139   return result;
   140 }
   141 # endif
   142 
   143 //----------------------------------------------------------------------
   144 // pow
   145 
   146 _STLP_EXP_DECLSPEC complex<float> _STLP_CALL pow(const float& a, const complex<float>& b) {
   147   float logr = _STLP_LOGF(a);
   148   float x = _STLP_EXPF(logr*b._M_re);
   149   float y = logr*b._M_im;
   150 
   151   return complex<float>(x * _STLP_COSF(y), x * _STLP_SINF(y));
   152 }
   153 
   154 _STLP_EXP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& z_in, int n) {
   155   complex<float> z = z_in;
   156   z = __power(z, (n < 0 ? -n : n), multiplies< complex<float> >());
   157   if (n < 0)
   158     return 1.f / z;
   159   else
   160     return z;
   161 }
   162 
   163 _STLP_EXP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& a, const float& b) {
   164   float logr = _STLP_LOGF(_STLP_HYPOTF(a._M_re,a._M_im));
   165   float logi = _STLP_ATAN2F(a._M_im, a._M_re);
   166   float x = _STLP_EXPF(logr * b);
   167   float y = logi * b;
   168 
   169   return complex<float>(x * _STLP_COSF(y), x * _STLP_SINF(y));
   170 }  
   171 
   172 _STLP_EXP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& a, const complex<float>& b) {
   173   float logr = _STLP_LOGF(_STLP_HYPOTF(a._M_re,a._M_im));
   174   float logi = _STLP_ATAN2F(a._M_im, a._M_re);
   175   float x = _STLP_EXPF(logr*b._M_re - logi*b._M_im);
   176   float y = logr*b._M_im + logi*b._M_re;
   177 
   178   return complex<float>(x * _STLP_COSF(y), x * _STLP_SINF(y));
   179 }
   180 
   181 
   182 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL pow(const double& a, const complex<double>& b) {
   183   double logr = _STLP_LOG(a);
   184   double x = _STLP_EXP(logr*b._M_re);
   185   double y = logr*b._M_im;
   186 
   187   return complex<double>(x * _STLP_COS(y), x * _STLP_SIN(y));
   188 }
   189 
   190 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& z_in, int n) {
   191   complex<double> z = z_in;
   192   z = __power(z, (n < 0 ? -n : n), multiplies< complex<double> >());
   193   if (n < 0)
   194 #if !defined(__SC__)			//*TY 04/15/2000 - 
   195     return 1. / z;
   196 #else							//*TY 04/15/2000 - added workaround for SCpp compiler
   197 	return double(1.0) / z;		//*TY 04/15/2000 - it incorrectly assign long double attribute to floating point literals
   198 #endif							//*TY 04/15/2000 - 
   199   else
   200     return z;
   201 }
   202 
   203 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& a, const double& b) {
   204   double logr = _STLP_LOG(_STLP_HYPOT(a._M_re,a._M_im));
   205   double logi = _STLP_ATAN2(a._M_im, a._M_re);
   206   double x = _STLP_EXP(logr * b);
   207   double y = logi * b;
   208 
   209   return complex<double>(x * _STLP_COS(y), x * _STLP_SIN(y));
   210 }  
   211 
   212 _STLP_EXP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& a, const complex<double>& b) {
   213   double logr = _STLP_LOG(_STLP_HYPOT(a._M_re,a._M_im));
   214   double logi = _STLP_ATAN2(a._M_im, a._M_re);
   215   double x = _STLP_EXP(logr*b._M_re - logi*b._M_im);
   216   double y = logr*b._M_im + logi*b._M_re;
   217 
   218   return complex<double>(x * _STLP_COS(y), x * _STLP_SIN(y));
   219 }
   220 
   221 
   222 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL pow(const long double& a,
   223                                                    const complex<long double>& b) {
   224   long double logr = _STLP_LOGL(a);
   225   long double x = _STLP_EXPL(logr*b._M_re);
   226   long double y = logr*b._M_im;
   227 
   228   return complex<long double>(x * _STLP_COSL(y), x * _STLP_SINL(y));
   229 }
   230 
   231 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& z_in, int n) {
   232   complex<long double> z = z_in;
   233   z = __power(z, (n < 0 ? -n : n), multiplies< complex<long double> >());
   234   if (n < 0)
   235     return 1.l / z;
   236   else
   237     return z;
   238 }
   239 
   240 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& a,
   241                          const long double& b) {
   242   long double logr = _STLP_LOGL(_STLP_HYPOTL(a._M_re,a._M_im));
   243   long double logi = _STLP_ATAN2L(a._M_im, a._M_re);
   244   long double x = _STLP_EXPL(logr * b);
   245   long double y = logi * b;
   246 
   247   return complex<long double>(x * _STLP_COSL(y), x * _STLP_SINL(y));
   248 }  
   249 
   250 _STLP_EXP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& a,
   251                          const complex<long double>& b) {
   252   long double logr = _STLP_LOGL(_STLP_HYPOTL(a._M_re,a._M_im));
   253   long double logi = _STLP_ATAN2L(a._M_im, a._M_re);
   254   long double x = _STLP_EXPL(logr*b._M_re - logi*b._M_im);
   255   long double y = logr*b._M_im + logi*b._M_re;
   256 
   257   return complex<long double>(x * _STLP_COSL(y), x * _STLP_SINL(y));
   258 }
   259 
   260 _STLP_END_NAMESPACE
   261