sl@0: /*
sl@0:  * Copyright (c) 1999
sl@0:  * Silicon Graphics Computer Systems, Inc.
sl@0:  *
sl@0:  * Copyright (c) 1999
sl@0:  * Boris Fomitchev
sl@0:  *
sl@0:  * This material is provided "as is", with absolutely no warranty expressed
sl@0:  * or implied. Any use is at your own risk.
sl@0:  *
sl@0:  * Permission to use or copy this software for any purpose is hereby granted
sl@0:  * without fee, provided the above notices are retained on all copies.
sl@0:  * Permission to modify the code and to distribute modified code is granted,
sl@0:  * provided the above notices are retained, and a notice that the code was
sl@0:  * modified is included with the above copyright notice.
sl@0:  *
sl@0:  */
sl@0: 
sl@0: #include "stlport_prefix.h"
sl@0: 
sl@0: #include <numeric>
sl@0: #include <cmath>
sl@0: #include <complex>
sl@0: 
sl@0: #if defined (_STLP_MSVC_LIB) && (_STLP_MSVC_LIB >= 1400)
sl@0: // hypot is deprecated.
sl@0: #  if defined (_STLP_MSVC)
sl@0: #    pragma warning (disable : 4996)
sl@0: #  elif defined (__ICL)
sl@0: #    pragma warning (disable : 1478)
sl@0: #  endif
sl@0: #endif
sl@0: 
sl@0: _STLP_BEGIN_NAMESPACE
sl@0: 
sl@0: // Complex division and square roots.
sl@0: 
sl@0: // Absolute value
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC float _STLP_CALL abs(const complex<float>& __z)
sl@0: { return ::hypot(__z._M_re, __z._M_im); }
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC double _STLP_CALL abs(const complex<double>& __z)
sl@0: { return ::hypot(__z._M_re, __z._M_im); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC long double _STLP_CALL abs(const complex<long double>& __z)
sl@0: { return ::hypot(__z._M_re, __z._M_im); }
sl@0: #endif
sl@0: 
sl@0: // Phase
sl@0: 
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC float _STLP_CALL arg(const complex<float>& __z)
sl@0: { return ::atan2(__z._M_im, __z._M_re); }
sl@0: 
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC double _STLP_CALL arg(const complex<double>& __z)
sl@0: { return ::atan2(__z._M_im, __z._M_re); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC long double _STLP_CALL arg(const complex<long double>& __z)
sl@0: { return ::atan2(__z._M_im, __z._M_re); }
sl@0: #endif
sl@0: 
sl@0: // Construct a complex number from polar representation
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL polar(const float& __rho, const float& __phi)
sl@0: { return complex<float>(__rho * ::cos(__phi), __rho * ::sin(__phi)); }
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL polar(const double& __rho, const double& __phi)
sl@0: { return complex<double>(__rho * ::cos(__phi), __rho * ::sin(__phi)); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_TEMPLATE_NULL
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL polar(const long double& __rho, const long double& __phi)
sl@0: { return complex<long double>(__rho * ::cos(__phi), __rho * ::sin(__phi)); }
sl@0: #endif
sl@0: 
sl@0: // Division
sl@0: template <class _Tp>
sl@0: static void _divT(const _Tp& __z1_r, const _Tp& __z1_i,
sl@0:                   const _Tp& __z2_r, const _Tp& __z2_i,
sl@0:                   _Tp& __res_r, _Tp& __res_i) {
sl@0:   _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
sl@0:   _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
sl@0: 
sl@0:   if (__ar <= __ai) {
sl@0:     _Tp __ratio = __z2_r / __z2_i;
sl@0:     _Tp __denom = __z2_i * (1 + __ratio * __ratio);
sl@0:     __res_r = (__z1_r * __ratio + __z1_i) / __denom;
sl@0:     __res_i = (__z1_i * __ratio - __z1_r) / __denom;
sl@0:   }
sl@0:   else {
sl@0:     _Tp __ratio = __z2_i / __z2_r;
sl@0:     _Tp __denom = __z2_r * (1 + __ratio * __ratio);
sl@0:     __res_r = (__z1_r + __z1_i * __ratio) / __denom;
sl@0:     __res_i = (__z1_i - __z1_r * __ratio) / __denom;
sl@0:   }
sl@0: }
sl@0: 
sl@0: template <class _Tp>
sl@0: static void _divT(const _Tp& __z1_r,
sl@0:                   const _Tp& __z2_r, const _Tp& __z2_i,
sl@0:                   _Tp& __res_r, _Tp& __res_i) {
sl@0:   _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
sl@0:   _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
sl@0: 
sl@0:   if (__ar <= __ai) {
sl@0:     _Tp __ratio = __z2_r / __z2_i;
sl@0:     _Tp __denom = __z2_i * (1 + __ratio * __ratio);
sl@0:     __res_r = (__z1_r * __ratio) / __denom;
sl@0:     __res_i = - __z1_r / __denom;
sl@0:   }
sl@0:   else {
sl@0:     _Tp __ratio = __z2_i / __z2_r;
sl@0:     _Tp __denom = __z2_r * (1 + __ratio * __ratio);
sl@0:     __res_r = __z1_r / __denom;
sl@0:     __res_i = - (__z1_r * __ratio) / __denom;
sl@0:   }
sl@0: }
sl@0: 
sl@0: _STLP_DECLSPEC void _STLP_CALL
sl@0: complex<float>::_div(const float& __z1_r, const float& __z1_i,
sl@0:                      const float& __z2_r, const float& __z2_i,
sl@0:                      float& __res_r, float& __res_i)
sl@0: { _divT(__z1_r, __z1_i, __z2_r, __z2_i, __res_r, __res_i); }
sl@0: 
sl@0: _STLP_DECLSPEC void _STLP_CALL
sl@0: complex<float>::_div(const float& __z1_r,
sl@0:                      const float& __z2_r, const float& __z2_i,
sl@0:                      float& __res_r, float& __res_i)
sl@0: { _divT(__z1_r, __z2_r, __z2_i, __res_r, __res_i); }
sl@0: 
sl@0: 
sl@0: _STLP_DECLSPEC void _STLP_CALL
sl@0: complex<double>::_div(const double& __z1_r, const double& __z1_i,
sl@0:                       const double& __z2_r, const double& __z2_i,
sl@0:                       double& __res_r, double& __res_i)
sl@0: { _divT(__z1_r, __z1_i, __z2_r, __z2_i, __res_r, __res_i); }
sl@0: 
sl@0: _STLP_DECLSPEC void _STLP_CALL
sl@0: complex<double>::_div(const double& __z1_r,
sl@0:                       const double& __z2_r, const double& __z2_i,
sl@0:                       double& __res_r, double& __res_i)
sl@0: { _divT(__z1_r, __z2_r, __z2_i, __res_r, __res_i); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_DECLSPEC void _STLP_CALL
sl@0: complex<long double>::_div(const long double& __z1_r, const long double& __z1_i,
sl@0:                            const long double& __z2_r, const long double& __z2_i,
sl@0:                            long double& __res_r, long double& __res_i)
sl@0: { _divT(__z1_r, __z1_i, __z2_r, __z2_i, __res_r, __res_i); }
sl@0: 
sl@0: _STLP_DECLSPEC void _STLP_CALL
sl@0: complex<long double>::_div(const long double& __z1_r,
sl@0:                            const long double& __z2_r, const long double& __z2_i,
sl@0:                            long double& __res_r, long double& __res_i)
sl@0: { _divT(__z1_r, __z2_r, __z2_i, __res_r, __res_i); }
sl@0: #endif
sl@0: 
sl@0: //----------------------------------------------------------------------
sl@0: // Square root
sl@0: template <class _Tp>
sl@0: static complex<_Tp> sqrtT(const complex<_Tp>& z) {
sl@0:   _Tp re = z._M_re;
sl@0:   _Tp im = z._M_im;
sl@0:   _Tp mag = ::hypot(re, im);
sl@0:   complex<_Tp> result;
sl@0: 
sl@0:   if (mag == 0.f) {
sl@0:     result._M_re = result._M_im = 0.f;
sl@0:   } else if (re > 0.f) {
sl@0:     result._M_re = ::sqrt(0.5f * (mag + re));
sl@0:     result._M_im = im/result._M_re/2.f;
sl@0:   } else {
sl@0:     result._M_im = ::sqrt(0.5f * (mag - re));
sl@0:     if (im < 0.f)
sl@0:       result._M_im = - result._M_im;
sl@0:     result._M_re = im/result._M_im/2.f;
sl@0:   }
sl@0:   return result;
sl@0: }
sl@0: 
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL
sl@0: sqrt(const complex<float>& z) { return sqrtT(z); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double>  _STLP_CALL
sl@0: sqrt(const complex<double>& z) { return sqrtT(z); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL
sl@0: sqrt(const complex<long double>& z) { return sqrtT(z); }
sl@0: #endif
sl@0: 
sl@0: // exp, log, pow for complex<float>, complex<double>, and complex<long double>
sl@0: //----------------------------------------------------------------------
sl@0: // exp
sl@0: template <class _Tp>
sl@0: static complex<_Tp> expT(const complex<_Tp>& z) {
sl@0:   _Tp expx = ::exp(z._M_re);
sl@0:   return complex<_Tp>(expx * ::cos(z._M_im),
sl@0:                       expx * ::sin(z._M_im));
sl@0: }
sl@0: _STLP_DECLSPEC complex<float>  _STLP_CALL exp(const complex<float>& z)
sl@0: { return expT(z); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL exp(const complex<double>& z)
sl@0: { return expT(z); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL exp(const complex<long double>& z)
sl@0: { return expT(z); }
sl@0: #endif
sl@0: 
sl@0: //----------------------------------------------------------------------
sl@0: // log10
sl@0: template <class _Tp>
sl@0: static complex<_Tp> log10T(const complex<_Tp>& z, const _Tp& ln10_inv) {
sl@0:   complex<_Tp> r;
sl@0: 
sl@0:   r._M_im = ::atan2(z._M_im, z._M_re) * ln10_inv;
sl@0:   r._M_re = ::log10(::hypot(z._M_re, z._M_im));
sl@0:   return r;
sl@0: }
sl@0: 
sl@0: static const float LN10_INVF = 1.f / ::log(10.f);
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL log10(const complex<float>& z)
sl@0: { return log10T(z, LN10_INVF); }
sl@0: 
sl@0: static const double LN10_INV = 1. / ::log10(10.);
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL log10(const complex<double>& z)
sl@0: { return log10T(z, LN10_INV); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: static const long double LN10_INVL = 1.l / ::log(10.l);
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL log10(const complex<long double>& z)
sl@0: { return log10T(z, LN10_INVL); }
sl@0: #endif
sl@0: 
sl@0: //----------------------------------------------------------------------
sl@0: // log
sl@0: template <class _Tp>
sl@0: static complex<_Tp> logT(const complex<_Tp>& z) {
sl@0:   complex<_Tp> r;
sl@0: 
sl@0:   r._M_im = ::atan2(z._M_im, z._M_re);
sl@0:   r._M_re = ::log(::hypot(z._M_re, z._M_im));
sl@0:   return r;
sl@0: }
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL log(const complex<float>& z)
sl@0: { return logT(z); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL log(const complex<double>& z)
sl@0: { return logT(z); }
sl@0: 
sl@0: #ifndef _STLP_NO_LONG_DOUBLE
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL log(const complex<long double>& z)
sl@0: { return logT(z); }
sl@0: # endif
sl@0: 
sl@0: //----------------------------------------------------------------------
sl@0: // pow
sl@0: template <class _Tp>
sl@0: static complex<_Tp> powT(const _Tp& a, const complex<_Tp>& b) {
sl@0:   _Tp logr = ::log(a);
sl@0:   _Tp x = ::exp(logr * b._M_re);
sl@0:   _Tp y = logr * b._M_im;
sl@0: 
sl@0:   return complex<_Tp>(x * ::cos(y), x * ::sin(y));
sl@0: }
sl@0: 
sl@0: template <class _Tp>
sl@0: static complex<_Tp> powT(const complex<_Tp>& z_in, int n) {
sl@0:   complex<_Tp> z = z_in;
sl@0:   z = _STLP_PRIV __power(z, (n < 0 ? -n : n), multiplies< complex<_Tp> >());
sl@0:   if (n < 0)
sl@0:     return _Tp(1.0) / z;
sl@0:   else
sl@0:     return z;
sl@0: }
sl@0: 
sl@0: template <class _Tp>
sl@0: static complex<_Tp> powT(const complex<_Tp>& a, const _Tp& b) {
sl@0:   _Tp logr = ::log(::hypot(a._M_re,a._M_im));
sl@0:   _Tp logi = ::atan2(a._M_im, a._M_re);
sl@0:   _Tp x = ::exp(logr * b);
sl@0:   _Tp y = logi * b;
sl@0: 
sl@0:   return complex<_Tp>(x * ::cos(y), x * ::sin(y));
sl@0: }
sl@0: 
sl@0: template <class _Tp>
sl@0: static complex<_Tp> powT(const complex<_Tp>& a, const complex<_Tp>& b) {
sl@0:   _Tp logr = ::log(::hypot(a._M_re,a._M_im));
sl@0:   _Tp logi = ::atan2(a._M_im, a._M_re);
sl@0:   _Tp x = ::exp(logr * b._M_re - logi * b._M_im);
sl@0:   _Tp y = logr * b._M_im + logi * b._M_re;
sl@0: 
sl@0:   return complex<_Tp>(x * ::cos(y), x * ::sin(y));
sl@0: }
sl@0: 
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL pow(const float& a, const complex<float>& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& z_in, int n)
sl@0: { return powT(z_in, n); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& a, const float& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& a, const complex<float>& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL pow(const double& a, const complex<double>& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& z_in, int n)
sl@0: { return powT(z_in, n); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& a, const double& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& a, const complex<double>& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: #if !defined (_STLP_NO_LONG_DOUBLE)
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const long double& a,
sl@0:                                                    const complex<long double>& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: 
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& z_in, int n)
sl@0: { return powT(z_in, n); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& a,
sl@0:                                                    const long double& b)
sl@0: { return powT(a, b); }
sl@0: 
sl@0: _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& a,
sl@0:                                                    const complex<long double>& b)
sl@0: { return powT(a, b); }
sl@0: #endif
sl@0: 
sl@0: _STLP_END_NAMESPACE