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