Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (c) 1999 |
michael@0 | 3 | * Silicon Graphics Computer Systems, Inc. |
michael@0 | 4 | * |
michael@0 | 5 | * Copyright (c) 1999 |
michael@0 | 6 | * Boris Fomitchev |
michael@0 | 7 | * |
michael@0 | 8 | * This material is provided "as is", with absolutely no warranty expressed |
michael@0 | 9 | * or implied. Any use is at your own risk. |
michael@0 | 10 | * |
michael@0 | 11 | * Permission to use or copy this software for any purpose is hereby granted |
michael@0 | 12 | * without fee, provided the above notices are retained on all copies. |
michael@0 | 13 | * Permission to modify the code and to distribute modified code is granted, |
michael@0 | 14 | * provided the above notices are retained, and a notice that the code was |
michael@0 | 15 | * modified is included with the above copyright notice. |
michael@0 | 16 | * |
michael@0 | 17 | */ |
michael@0 | 18 | |
michael@0 | 19 | #include "stlport_prefix.h" |
michael@0 | 20 | |
michael@0 | 21 | #include <numeric> |
michael@0 | 22 | #include <cmath> |
michael@0 | 23 | #include <complex> |
michael@0 | 24 | |
michael@0 | 25 | #if defined (_STLP_MSVC_LIB) && (_STLP_MSVC_LIB >= 1400) |
michael@0 | 26 | // hypot is deprecated. |
michael@0 | 27 | # if defined (_STLP_MSVC) |
michael@0 | 28 | # pragma warning (disable : 4996) |
michael@0 | 29 | # elif defined (__ICL) |
michael@0 | 30 | # pragma warning (disable : 1478) |
michael@0 | 31 | # endif |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | _STLP_BEGIN_NAMESPACE |
michael@0 | 35 | |
michael@0 | 36 | // Complex division and square roots. |
michael@0 | 37 | |
michael@0 | 38 | // Absolute value |
michael@0 | 39 | _STLP_TEMPLATE_NULL |
michael@0 | 40 | _STLP_DECLSPEC float _STLP_CALL abs(const complex<float>& __z) |
michael@0 | 41 | { return ::hypot(__z._M_re, __z._M_im); } |
michael@0 | 42 | _STLP_TEMPLATE_NULL |
michael@0 | 43 | _STLP_DECLSPEC double _STLP_CALL abs(const complex<double>& __z) |
michael@0 | 44 | { return ::hypot(__z._M_re, __z._M_im); } |
michael@0 | 45 | |
michael@0 | 46 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 47 | _STLP_TEMPLATE_NULL |
michael@0 | 48 | _STLP_DECLSPEC long double _STLP_CALL abs(const complex<long double>& __z) |
michael@0 | 49 | { return ::hypot(__z._M_re, __z._M_im); } |
michael@0 | 50 | #endif |
michael@0 | 51 | |
michael@0 | 52 | // Phase |
michael@0 | 53 | |
michael@0 | 54 | _STLP_TEMPLATE_NULL |
michael@0 | 55 | _STLP_DECLSPEC float _STLP_CALL arg(const complex<float>& __z) |
michael@0 | 56 | { return ::atan2(__z._M_im, __z._M_re); } |
michael@0 | 57 | |
michael@0 | 58 | _STLP_TEMPLATE_NULL |
michael@0 | 59 | _STLP_DECLSPEC double _STLP_CALL arg(const complex<double>& __z) |
michael@0 | 60 | { return ::atan2(__z._M_im, __z._M_re); } |
michael@0 | 61 | |
michael@0 | 62 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 63 | _STLP_TEMPLATE_NULL |
michael@0 | 64 | _STLP_DECLSPEC long double _STLP_CALL arg(const complex<long double>& __z) |
michael@0 | 65 | { return ::atan2(__z._M_im, __z._M_re); } |
michael@0 | 66 | #endif |
michael@0 | 67 | |
michael@0 | 68 | // Construct a complex number from polar representation |
michael@0 | 69 | _STLP_TEMPLATE_NULL |
michael@0 | 70 | _STLP_DECLSPEC complex<float> _STLP_CALL polar(const float& __rho, const float& __phi) |
michael@0 | 71 | { return complex<float>(__rho * ::cos(__phi), __rho * ::sin(__phi)); } |
michael@0 | 72 | _STLP_TEMPLATE_NULL |
michael@0 | 73 | _STLP_DECLSPEC complex<double> _STLP_CALL polar(const double& __rho, const double& __phi) |
michael@0 | 74 | { return complex<double>(__rho * ::cos(__phi), __rho * ::sin(__phi)); } |
michael@0 | 75 | |
michael@0 | 76 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 77 | _STLP_TEMPLATE_NULL |
michael@0 | 78 | _STLP_DECLSPEC complex<long double> _STLP_CALL polar(const long double& __rho, const long double& __phi) |
michael@0 | 79 | { return complex<long double>(__rho * ::cos(__phi), __rho * ::sin(__phi)); } |
michael@0 | 80 | #endif |
michael@0 | 81 | |
michael@0 | 82 | // Division |
michael@0 | 83 | template <class _Tp> |
michael@0 | 84 | static void _divT(const _Tp& __z1_r, const _Tp& __z1_i, |
michael@0 | 85 | const _Tp& __z2_r, const _Tp& __z2_i, |
michael@0 | 86 | _Tp& __res_r, _Tp& __res_i) { |
michael@0 | 87 | _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r; |
michael@0 | 88 | _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i; |
michael@0 | 89 | |
michael@0 | 90 | if (__ar <= __ai) { |
michael@0 | 91 | _Tp __ratio = __z2_r / __z2_i; |
michael@0 | 92 | _Tp __denom = __z2_i * (1 + __ratio * __ratio); |
michael@0 | 93 | __res_r = (__z1_r * __ratio + __z1_i) / __denom; |
michael@0 | 94 | __res_i = (__z1_i * __ratio - __z1_r) / __denom; |
michael@0 | 95 | } |
michael@0 | 96 | else { |
michael@0 | 97 | _Tp __ratio = __z2_i / __z2_r; |
michael@0 | 98 | _Tp __denom = __z2_r * (1 + __ratio * __ratio); |
michael@0 | 99 | __res_r = (__z1_r + __z1_i * __ratio) / __denom; |
michael@0 | 100 | __res_i = (__z1_i - __z1_r * __ratio) / __denom; |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | template <class _Tp> |
michael@0 | 105 | static void _divT(const _Tp& __z1_r, |
michael@0 | 106 | const _Tp& __z2_r, const _Tp& __z2_i, |
michael@0 | 107 | _Tp& __res_r, _Tp& __res_i) { |
michael@0 | 108 | _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r; |
michael@0 | 109 | _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i; |
michael@0 | 110 | |
michael@0 | 111 | if (__ar <= __ai) { |
michael@0 | 112 | _Tp __ratio = __z2_r / __z2_i; |
michael@0 | 113 | _Tp __denom = __z2_i * (1 + __ratio * __ratio); |
michael@0 | 114 | __res_r = (__z1_r * __ratio) / __denom; |
michael@0 | 115 | __res_i = - __z1_r / __denom; |
michael@0 | 116 | } |
michael@0 | 117 | else { |
michael@0 | 118 | _Tp __ratio = __z2_i / __z2_r; |
michael@0 | 119 | _Tp __denom = __z2_r * (1 + __ratio * __ratio); |
michael@0 | 120 | __res_r = __z1_r / __denom; |
michael@0 | 121 | __res_i = - (__z1_r * __ratio) / __denom; |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void _STLP_CALL |
michael@0 | 126 | complex<float>::_div(const float& __z1_r, const float& __z1_i, |
michael@0 | 127 | const float& __z2_r, const float& __z2_i, |
michael@0 | 128 | float& __res_r, float& __res_i) |
michael@0 | 129 | { _divT(__z1_r, __z1_i, __z2_r, __z2_i, __res_r, __res_i); } |
michael@0 | 130 | |
michael@0 | 131 | void _STLP_CALL |
michael@0 | 132 | complex<float>::_div(const float& __z1_r, |
michael@0 | 133 | const float& __z2_r, const float& __z2_i, |
michael@0 | 134 | float& __res_r, float& __res_i) |
michael@0 | 135 | { _divT(__z1_r, __z2_r, __z2_i, __res_r, __res_i); } |
michael@0 | 136 | |
michael@0 | 137 | |
michael@0 | 138 | void _STLP_CALL |
michael@0 | 139 | complex<double>::_div(const double& __z1_r, const double& __z1_i, |
michael@0 | 140 | const double& __z2_r, const double& __z2_i, |
michael@0 | 141 | double& __res_r, double& __res_i) |
michael@0 | 142 | { _divT(__z1_r, __z1_i, __z2_r, __z2_i, __res_r, __res_i); } |
michael@0 | 143 | |
michael@0 | 144 | void _STLP_CALL |
michael@0 | 145 | complex<double>::_div(const double& __z1_r, |
michael@0 | 146 | const double& __z2_r, const double& __z2_i, |
michael@0 | 147 | double& __res_r, double& __res_i) |
michael@0 | 148 | { _divT(__z1_r, __z2_r, __z2_i, __res_r, __res_i); } |
michael@0 | 149 | |
michael@0 | 150 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 151 | void _STLP_CALL |
michael@0 | 152 | complex<long double>::_div(const long double& __z1_r, const long double& __z1_i, |
michael@0 | 153 | const long double& __z2_r, const long double& __z2_i, |
michael@0 | 154 | long double& __res_r, long double& __res_i) |
michael@0 | 155 | { _divT(__z1_r, __z1_i, __z2_r, __z2_i, __res_r, __res_i); } |
michael@0 | 156 | |
michael@0 | 157 | void _STLP_CALL |
michael@0 | 158 | complex<long double>::_div(const long double& __z1_r, |
michael@0 | 159 | const long double& __z2_r, const long double& __z2_i, |
michael@0 | 160 | long double& __res_r, long double& __res_i) |
michael@0 | 161 | { _divT(__z1_r, __z2_r, __z2_i, __res_r, __res_i); } |
michael@0 | 162 | #endif |
michael@0 | 163 | |
michael@0 | 164 | //---------------------------------------------------------------------- |
michael@0 | 165 | // Square root |
michael@0 | 166 | template <class _Tp> |
michael@0 | 167 | static complex<_Tp> sqrtT(const complex<_Tp>& z) { |
michael@0 | 168 | _Tp re = z._M_re; |
michael@0 | 169 | _Tp im = z._M_im; |
michael@0 | 170 | _Tp mag = ::hypot(re, im); |
michael@0 | 171 | complex<_Tp> result; |
michael@0 | 172 | |
michael@0 | 173 | if (mag == 0.f) { |
michael@0 | 174 | result._M_re = result._M_im = 0.f; |
michael@0 | 175 | } else if (re > 0.f) { |
michael@0 | 176 | result._M_re = ::sqrt(0.5f * (mag + re)); |
michael@0 | 177 | result._M_im = im/result._M_re/2.f; |
michael@0 | 178 | } else { |
michael@0 | 179 | result._M_im = ::sqrt(0.5f * (mag - re)); |
michael@0 | 180 | if (im < 0.f) |
michael@0 | 181 | result._M_im = - result._M_im; |
michael@0 | 182 | result._M_re = im/result._M_im/2.f; |
michael@0 | 183 | } |
michael@0 | 184 | return result; |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | complex<float> _STLP_CALL |
michael@0 | 188 | sqrt(const complex<float>& z) { return sqrtT(z); } |
michael@0 | 189 | |
michael@0 | 190 | complex<double> _STLP_CALL |
michael@0 | 191 | sqrt(const complex<double>& z) { return sqrtT(z); } |
michael@0 | 192 | |
michael@0 | 193 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 194 | complex<long double> _STLP_CALL |
michael@0 | 195 | sqrt(const complex<long double>& z) { return sqrtT(z); } |
michael@0 | 196 | #endif |
michael@0 | 197 | |
michael@0 | 198 | // exp, log, pow for complex<float>, complex<double>, and complex<long double> |
michael@0 | 199 | //---------------------------------------------------------------------- |
michael@0 | 200 | // exp |
michael@0 | 201 | template <class _Tp> |
michael@0 | 202 | static complex<_Tp> expT(const complex<_Tp>& z) { |
michael@0 | 203 | _Tp expx = ::exp(z._M_re); |
michael@0 | 204 | return complex<_Tp>(expx * ::cos(z._M_im), |
michael@0 | 205 | expx * ::sin(z._M_im)); |
michael@0 | 206 | } |
michael@0 | 207 | _STLP_DECLSPEC complex<float> _STLP_CALL exp(const complex<float>& z) |
michael@0 | 208 | { return expT(z); } |
michael@0 | 209 | |
michael@0 | 210 | _STLP_DECLSPEC complex<double> _STLP_CALL exp(const complex<double>& z) |
michael@0 | 211 | { return expT(z); } |
michael@0 | 212 | |
michael@0 | 213 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 214 | _STLP_DECLSPEC complex<long double> _STLP_CALL exp(const complex<long double>& z) |
michael@0 | 215 | { return expT(z); } |
michael@0 | 216 | #endif |
michael@0 | 217 | |
michael@0 | 218 | //---------------------------------------------------------------------- |
michael@0 | 219 | // log10 |
michael@0 | 220 | template <class _Tp> |
michael@0 | 221 | static complex<_Tp> log10T(const complex<_Tp>& z, const _Tp& ln10_inv) { |
michael@0 | 222 | complex<_Tp> r; |
michael@0 | 223 | |
michael@0 | 224 | r._M_im = ::atan2(z._M_im, z._M_re) * ln10_inv; |
michael@0 | 225 | r._M_re = ::log10(::hypot(z._M_re, z._M_im)); |
michael@0 | 226 | return r; |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | _STLP_DECLSPEC complex<float> _STLP_CALL log10(const complex<float>& z) |
michael@0 | 230 | { |
michael@0 | 231 | const float LN10_INVF = 1.f / ::log(10.f); |
michael@0 | 232 | return log10T(z, LN10_INVF); |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | _STLP_DECLSPEC complex<double> _STLP_CALL log10(const complex<double>& z) |
michael@0 | 236 | { |
michael@0 | 237 | const double LN10_INV = 1. / ::log10(10.); |
michael@0 | 238 | return log10T(z, LN10_INV); |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 242 | _STLP_DECLSPEC complex<long double> _STLP_CALL log10(const complex<long double>& z) |
michael@0 | 243 | { |
michael@0 | 244 | const long double LN10_INVL = 1.l / ::log(10.l); |
michael@0 | 245 | return log10T(z, LN10_INVL); |
michael@0 | 246 | } |
michael@0 | 247 | #endif |
michael@0 | 248 | |
michael@0 | 249 | //---------------------------------------------------------------------- |
michael@0 | 250 | // log |
michael@0 | 251 | template <class _Tp> |
michael@0 | 252 | static complex<_Tp> logT(const complex<_Tp>& z) { |
michael@0 | 253 | complex<_Tp> r; |
michael@0 | 254 | |
michael@0 | 255 | r._M_im = ::atan2(z._M_im, z._M_re); |
michael@0 | 256 | r._M_re = ::log(::hypot(z._M_re, z._M_im)); |
michael@0 | 257 | return r; |
michael@0 | 258 | } |
michael@0 | 259 | _STLP_DECLSPEC complex<float> _STLP_CALL log(const complex<float>& z) |
michael@0 | 260 | { return logT(z); } |
michael@0 | 261 | |
michael@0 | 262 | _STLP_DECLSPEC complex<double> _STLP_CALL log(const complex<double>& z) |
michael@0 | 263 | { return logT(z); } |
michael@0 | 264 | |
michael@0 | 265 | #ifndef _STLP_NO_LONG_DOUBLE |
michael@0 | 266 | _STLP_DECLSPEC complex<long double> _STLP_CALL log(const complex<long double>& z) |
michael@0 | 267 | { return logT(z); } |
michael@0 | 268 | # endif |
michael@0 | 269 | |
michael@0 | 270 | //---------------------------------------------------------------------- |
michael@0 | 271 | // pow |
michael@0 | 272 | template <class _Tp> |
michael@0 | 273 | static complex<_Tp> powT(const _Tp& a, const complex<_Tp>& b) { |
michael@0 | 274 | _Tp logr = ::log(a); |
michael@0 | 275 | _Tp x = ::exp(logr * b._M_re); |
michael@0 | 276 | _Tp y = logr * b._M_im; |
michael@0 | 277 | |
michael@0 | 278 | return complex<_Tp>(x * ::cos(y), x * ::sin(y)); |
michael@0 | 279 | } |
michael@0 | 280 | |
michael@0 | 281 | template <class _Tp> |
michael@0 | 282 | static complex<_Tp> powT(const complex<_Tp>& z_in, int n) { |
michael@0 | 283 | complex<_Tp> z = z_in; |
michael@0 | 284 | z = _STLP_PRIV __power(z, (n < 0 ? -n : n), multiplies< complex<_Tp> >()); |
michael@0 | 285 | if (n < 0) |
michael@0 | 286 | return _Tp(1.0) / z; |
michael@0 | 287 | else |
michael@0 | 288 | return z; |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | template <class _Tp> |
michael@0 | 292 | static complex<_Tp> powT(const complex<_Tp>& a, const _Tp& b) { |
michael@0 | 293 | _Tp logr = ::log(::hypot(a._M_re,a._M_im)); |
michael@0 | 294 | _Tp logi = ::atan2(a._M_im, a._M_re); |
michael@0 | 295 | _Tp x = ::exp(logr * b); |
michael@0 | 296 | _Tp y = logi * b; |
michael@0 | 297 | |
michael@0 | 298 | return complex<_Tp>(x * ::cos(y), x * ::sin(y)); |
michael@0 | 299 | } |
michael@0 | 300 | |
michael@0 | 301 | template <class _Tp> |
michael@0 | 302 | static complex<_Tp> powT(const complex<_Tp>& a, const complex<_Tp>& b) { |
michael@0 | 303 | _Tp logr = ::log(::hypot(a._M_re,a._M_im)); |
michael@0 | 304 | _Tp logi = ::atan2(a._M_im, a._M_re); |
michael@0 | 305 | _Tp x = ::exp(logr * b._M_re - logi * b._M_im); |
michael@0 | 306 | _Tp y = logr * b._M_im + logi * b._M_re; |
michael@0 | 307 | |
michael@0 | 308 | return complex<_Tp>(x * ::cos(y), x * ::sin(y)); |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | _STLP_DECLSPEC complex<float> _STLP_CALL pow(const float& a, const complex<float>& b) |
michael@0 | 312 | { return powT(a, b); } |
michael@0 | 313 | |
michael@0 | 314 | _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& z_in, int n) |
michael@0 | 315 | { return powT(z_in, n); } |
michael@0 | 316 | |
michael@0 | 317 | _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& a, const float& b) |
michael@0 | 318 | { return powT(a, b); } |
michael@0 | 319 | |
michael@0 | 320 | _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>& a, const complex<float>& b) |
michael@0 | 321 | { return powT(a, b); } |
michael@0 | 322 | |
michael@0 | 323 | _STLP_DECLSPEC complex<double> _STLP_CALL pow(const double& a, const complex<double>& b) |
michael@0 | 324 | { return powT(a, b); } |
michael@0 | 325 | |
michael@0 | 326 | _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& z_in, int n) |
michael@0 | 327 | { return powT(z_in, n); } |
michael@0 | 328 | |
michael@0 | 329 | _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& a, const double& b) |
michael@0 | 330 | { return powT(a, b); } |
michael@0 | 331 | |
michael@0 | 332 | _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>& a, const complex<double>& b) |
michael@0 | 333 | { return powT(a, b); } |
michael@0 | 334 | |
michael@0 | 335 | #if !defined (_STLP_NO_LONG_DOUBLE) |
michael@0 | 336 | _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const long double& a, |
michael@0 | 337 | const complex<long double>& b) |
michael@0 | 338 | { return powT(a, b); } |
michael@0 | 339 | |
michael@0 | 340 | |
michael@0 | 341 | _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& z_in, int n) |
michael@0 | 342 | { return powT(z_in, n); } |
michael@0 | 343 | |
michael@0 | 344 | _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& a, |
michael@0 | 345 | const long double& b) |
michael@0 | 346 | { return powT(a, b); } |
michael@0 | 347 | |
michael@0 | 348 | _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>& a, |
michael@0 | 349 | const complex<long double>& b) |
michael@0 | 350 | { return powT(a, b); } |
michael@0 | 351 | #endif |
michael@0 | 352 | |
michael@0 | 353 | _STLP_END_NAMESPACE |