complex.cpp 11 KB

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