_complex.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. #ifndef _STLP_INTERNAL_COMPLEX
  19. #define _STLP_INTERNAL_COMPLEX
  20. // This header declares the template class complex, as described in
  21. // in the draft C++ standard. Single-precision complex numbers
  22. // are complex<float>, double-precision are complex<double>, and
  23. // quad precision are complex<long double>.
  24. // Note that the template class complex is declared within namespace
  25. // std, as called for by the draft C++ standard.
  26. #ifndef _STLP_INTERNAL_CMATH
  27. # include <stl/_cmath.h>
  28. #endif
  29. _STLP_BEGIN_NAMESPACE
  30. template <class _Tp>
  31. struct complex {
  32. typedef _Tp value_type;
  33. typedef complex<_Tp> _Self;
  34. // Constructors, destructor, assignment operator.
  35. complex() : _M_re(0), _M_im(0) {}
  36. complex(const value_type& __x)
  37. : _M_re(__x), _M_im(0) {}
  38. complex(const value_type& __x, const value_type& __y)
  39. : _M_re(__x), _M_im(__y) {}
  40. complex(const _Self& __z)
  41. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  42. _Self& operator=(const _Self& __z) {
  43. _M_re = __z._M_re;
  44. _M_im = __z._M_im;
  45. return *this;
  46. }
  47. #if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  48. template <class _Tp2>
  49. explicit complex(const complex<_Tp2>& __z)
  50. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  51. template <class _Tp2>
  52. _Self& operator=(const complex<_Tp2>& __z) {
  53. _M_re = __z._M_re;
  54. _M_im = __z._M_im;
  55. return *this;
  56. }
  57. #endif /* _STLP_MEMBER_TEMPLATES */
  58. // Element access.
  59. value_type real() const { return _M_re; }
  60. value_type imag() const { return _M_im; }
  61. // Arithmetic op= operations involving one real argument.
  62. _Self& operator= (const value_type& __x) {
  63. _M_re = __x;
  64. _M_im = 0;
  65. return *this;
  66. }
  67. _Self& operator+= (const value_type& __x) {
  68. _M_re += __x;
  69. return *this;
  70. }
  71. _Self& operator-= (const value_type& __x) {
  72. _M_re -= __x;
  73. return *this;
  74. }
  75. _Self& operator*= (const value_type& __x) {
  76. _M_re *= __x;
  77. _M_im *= __x;
  78. return *this;
  79. }
  80. _Self& operator/= (const value_type& __x) {
  81. _M_re /= __x;
  82. _M_im /= __x;
  83. return *this;
  84. }
  85. // Arithmetic op= operations involving two complex arguments.
  86. static void _STLP_CALL _div(const value_type& __z1_r, const value_type& __z1_i,
  87. const value_type& __z2_r, const value_type& __z2_i,
  88. value_type& __res_r, value_type& __res_i);
  89. static void _STLP_CALL _div(const value_type& __z1_r,
  90. const value_type& __z2_r, const value_type& __z2_i,
  91. value_type& __res_r, value_type& __res_i);
  92. #if defined (_STLP_MEMBER_TEMPLATES) // && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  93. template <class _Tp2> _Self& operator+= (const complex<_Tp2>& __z) {
  94. _M_re += __z._M_re;
  95. _M_im += __z._M_im;
  96. return *this;
  97. }
  98. template <class _Tp2> _Self& operator-= (const complex<_Tp2>& __z) {
  99. _M_re -= __z._M_re;
  100. _M_im -= __z._M_im;
  101. return *this;
  102. }
  103. template <class _Tp2> _Self& operator*= (const complex<_Tp2>& __z) {
  104. value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  105. value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  106. _M_re = __r;
  107. _M_im = __i;
  108. return *this;
  109. }
  110. template <class _Tp2> _Self& operator/= (const complex<_Tp2>& __z) {
  111. value_type __r;
  112. value_type __i;
  113. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  114. _M_re = __r;
  115. _M_im = __i;
  116. return *this;
  117. }
  118. #endif /* _STLP_MEMBER_TEMPLATES */
  119. _Self& operator+= (const _Self& __z) {
  120. _M_re += __z._M_re;
  121. _M_im += __z._M_im;
  122. return *this;
  123. }
  124. _Self& operator-= (const _Self& __z) {
  125. _M_re -= __z._M_re;
  126. _M_im -= __z._M_im;
  127. return *this;
  128. }
  129. _Self& operator*= (const _Self& __z) {
  130. value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  131. value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  132. _M_re = __r;
  133. _M_im = __i;
  134. return *this;
  135. }
  136. _Self& operator/= (const _Self& __z) {
  137. value_type __r;
  138. value_type __i;
  139. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  140. _M_re = __r;
  141. _M_im = __i;
  142. return *this;
  143. }
  144. // Data members.
  145. value_type _M_re;
  146. value_type _M_im;
  147. };
  148. // Explicit specializations for float, double, long double. The only
  149. // reason for these specializations is to enable automatic conversions
  150. // from complex<float> to complex<double>, and complex<double> to
  151. // complex<long double>.
  152. _STLP_TEMPLATE_NULL
  153. struct _STLP_CLASS_DECLSPEC complex<float> {
  154. typedef float value_type;
  155. typedef complex<float> _Self;
  156. // Constructors, destructor, assignment operator.
  157. complex(value_type __x = 0.0f, value_type __y = 0.0f)
  158. : _M_re(__x), _M_im(__y) {}
  159. complex(const complex<float>& __z) : _M_re(__z._M_re), _M_im(__z._M_im) {}
  160. inline explicit complex(const complex<double>& __z);
  161. #ifndef _STLP_NO_LONG_DOUBLE
  162. inline explicit complex(const complex<long double>& __z);
  163. #endif
  164. // Element access.
  165. value_type real() const { return _M_re; }
  166. value_type imag() const { return _M_im; }
  167. // Arithmetic op= operations involving one real argument.
  168. _Self& operator= (value_type __x) {
  169. _M_re = __x;
  170. _M_im = 0.0f;
  171. return *this;
  172. }
  173. _Self& operator+= (value_type __x) {
  174. _M_re += __x;
  175. return *this;
  176. }
  177. _Self& operator-= (value_type __x) {
  178. _M_re -= __x;
  179. return *this;
  180. }
  181. _Self& operator*= (value_type __x) {
  182. _M_re *= __x;
  183. _M_im *= __x;
  184. return *this;
  185. }
  186. _Self& operator/= (value_type __x) {
  187. _M_re /= __x;
  188. _M_im /= __x;
  189. return *this;
  190. }
  191. // Arithmetic op= operations involving two complex arguments.
  192. static void _STLP_CALL _div(const float& __z1_r, const float& __z1_i,
  193. const float& __z2_r, const float& __z2_i,
  194. float& __res_r, float& __res_i);
  195. static void _STLP_CALL _div(const float& __z1_r,
  196. const float& __z2_r, const float& __z2_i,
  197. float& __res_r, float& __res_i);
  198. #if defined (_STLP_MEMBER_TEMPLATES)
  199. template <class _Tp2>
  200. complex<float>& operator=(const complex<_Tp2>& __z) {
  201. _M_re = __z._M_re;
  202. _M_im = __z._M_im;
  203. return *this;
  204. }
  205. template <class _Tp2>
  206. complex<float>& operator+= (const complex<_Tp2>& __z) {
  207. _M_re += __z._M_re;
  208. _M_im += __z._M_im;
  209. return *this;
  210. }
  211. template <class _Tp2>
  212. complex<float>& operator-= (const complex<_Tp2>& __z) {
  213. _M_re -= __z._M_re;
  214. _M_im -= __z._M_im;
  215. return *this;
  216. }
  217. template <class _Tp2>
  218. complex<float>& operator*= (const complex<_Tp2>& __z) {
  219. float __r = _M_re * __z._M_re - _M_im * __z._M_im;
  220. float __i = _M_re * __z._M_im + _M_im * __z._M_re;
  221. _M_re = __r;
  222. _M_im = __i;
  223. return *this;
  224. }
  225. template <class _Tp2>
  226. complex<float>& operator/= (const complex<_Tp2>& __z) {
  227. float __r;
  228. float __i;
  229. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  230. _M_re = __r;
  231. _M_im = __i;
  232. return *this;
  233. }
  234. #endif /* _STLP_MEMBER_TEMPLATES */
  235. _Self& operator=(const _Self& __z) {
  236. _M_re = __z._M_re;
  237. _M_im = __z._M_im;
  238. return *this;
  239. }
  240. _Self& operator+= (const _Self& __z) {
  241. _M_re += __z._M_re;
  242. _M_im += __z._M_im;
  243. return *this;
  244. }
  245. _Self& operator-= (const _Self& __z) {
  246. _M_re -= __z._M_re;
  247. _M_im -= __z._M_im;
  248. return *this;
  249. }
  250. _Self& operator*= (const _Self& __z) {
  251. value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  252. value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  253. _M_re = __r;
  254. _M_im = __i;
  255. return *this;
  256. }
  257. _Self& operator/= (const _Self& __z) {
  258. value_type __r;
  259. value_type __i;
  260. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  261. _M_re = __r;
  262. _M_im = __i;
  263. return *this;
  264. }
  265. // Data members.
  266. value_type _M_re;
  267. value_type _M_im;
  268. };
  269. _STLP_TEMPLATE_NULL
  270. struct _STLP_CLASS_DECLSPEC complex<double> {
  271. typedef double value_type;
  272. typedef complex<double> _Self;
  273. // Constructors, destructor, assignment operator.
  274. complex(value_type __x = 0.0, value_type __y = 0.0)
  275. : _M_re(__x), _M_im(__y) {}
  276. complex(const complex<double>& __z)
  277. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  278. inline complex(const complex<float>& __z);
  279. #if !defined (_STLP_NO_LONG_DOUBLE)
  280. explicit inline complex(const complex<long double>& __z);
  281. #endif
  282. // Element access.
  283. value_type real() const { return _M_re; }
  284. value_type imag() const { return _M_im; }
  285. // Arithmetic op= operations involving one real argument.
  286. _Self& operator= (value_type __x) {
  287. _M_re = __x;
  288. _M_im = 0.0;
  289. return *this;
  290. }
  291. _Self& operator+= (value_type __x) {
  292. _M_re += __x;
  293. return *this;
  294. }
  295. _Self& operator-= (value_type __x) {
  296. _M_re -= __x;
  297. return *this;
  298. }
  299. _Self& operator*= (value_type __x) {
  300. _M_re *= __x;
  301. _M_im *= __x;
  302. return *this;
  303. }
  304. _Self& operator/= (value_type __x) {
  305. _M_re /= __x;
  306. _M_im /= __x;
  307. return *this;
  308. }
  309. // Arithmetic op= operations involving two complex arguments.
  310. static void _STLP_CALL _div(const double& __z1_r, const double& __z1_i,
  311. const double& __z2_r, const double& __z2_i,
  312. double& __res_r, double& __res_i);
  313. static void _STLP_CALL _div(const double& __z1_r,
  314. const double& __z2_r, const double& __z2_i,
  315. double& __res_r, double& __res_i);
  316. #if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  317. template <class _Tp2>
  318. complex<double>& operator=(const complex<_Tp2>& __z) {
  319. _M_re = __z._M_re;
  320. _M_im = __z._M_im;
  321. return *this;
  322. }
  323. template <class _Tp2>
  324. complex<double>& operator+= (const complex<_Tp2>& __z) {
  325. _M_re += __z._M_re;
  326. _M_im += __z._M_im;
  327. return *this;
  328. }
  329. template <class _Tp2>
  330. complex<double>& operator-= (const complex<_Tp2>& __z) {
  331. _M_re -= __z._M_re;
  332. _M_im -= __z._M_im;
  333. return *this;
  334. }
  335. template <class _Tp2>
  336. complex<double>& operator*= (const complex<_Tp2>& __z) {
  337. double __r = _M_re * __z._M_re - _M_im * __z._M_im;
  338. double __i = _M_re * __z._M_im + _M_im * __z._M_re;
  339. _M_re = __r;
  340. _M_im = __i;
  341. return *this;
  342. }
  343. template <class _Tp2>
  344. complex<double>& operator/= (const complex<_Tp2>& __z) {
  345. double __r;
  346. double __i;
  347. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  348. _M_re = __r;
  349. _M_im = __i;
  350. return *this;
  351. }
  352. #endif /* _STLP_MEMBER_TEMPLATES */
  353. _Self& operator=(const _Self& __z) {
  354. _M_re = __z._M_re;
  355. _M_im = __z._M_im;
  356. return *this;
  357. }
  358. _Self& operator+= (const _Self& __z) {
  359. _M_re += __z._M_re;
  360. _M_im += __z._M_im;
  361. return *this;
  362. }
  363. _Self& operator-= (const _Self& __z) {
  364. _M_re -= __z._M_re;
  365. _M_im -= __z._M_im;
  366. return *this;
  367. }
  368. _Self& operator*= (const _Self& __z) {
  369. value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  370. value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  371. _M_re = __r;
  372. _M_im = __i;
  373. return *this;
  374. }
  375. _Self& operator/= (const _Self& __z) {
  376. value_type __r;
  377. value_type __i;
  378. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  379. _M_re = __r;
  380. _M_im = __i;
  381. return *this;
  382. }
  383. // Data members.
  384. value_type _M_re;
  385. value_type _M_im;
  386. };
  387. #if !defined (_STLP_NO_LONG_DOUBLE)
  388. _STLP_TEMPLATE_NULL
  389. struct _STLP_CLASS_DECLSPEC complex<long double> {
  390. typedef long double value_type;
  391. typedef complex<long double> _Self;
  392. // Constructors, destructor, assignment operator.
  393. complex(value_type __x = 0.0l, value_type __y = 0.0l)
  394. : _M_re(__x), _M_im(__y) {}
  395. complex(const complex<long double>& __z)
  396. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  397. inline complex(const complex<float>& __z);
  398. inline complex(const complex<double>& __z);
  399. // Element access.
  400. value_type real() const { return _M_re; }
  401. value_type imag() const { return _M_im; }
  402. // Arithmetic op= operations involving one real argument.
  403. _Self& operator= (value_type __x) {
  404. _M_re = __x;
  405. _M_im = 0.0l;
  406. return *this;
  407. }
  408. _Self& operator+= (value_type __x) {
  409. _M_re += __x;
  410. return *this;
  411. }
  412. _Self& operator-= (value_type __x) {
  413. _M_re -= __x;
  414. return *this;
  415. }
  416. _Self& operator*= (value_type __x) {
  417. _M_re *= __x;
  418. _M_im *= __x;
  419. return *this;
  420. }
  421. _Self& operator/= (value_type __x) {
  422. _M_re /= __x;
  423. _M_im /= __x;
  424. return *this;
  425. }
  426. // Arithmetic op= operations involving two complex arguments.
  427. static void _STLP_CALL _div(const long double& __z1_r, const long double& __z1_i,
  428. const long double& __z2_r, const long double& __z2_i,
  429. long double& __res_r, long double& __res_i);
  430. static void _STLP_CALL _div(const long double& __z1_r,
  431. const long double& __z2_r, const long double& __z2_i,
  432. long double& __res_r, long double& __res_i);
  433. # if defined (_STLP_MEMBER_TEMPLATES) && defined (_STLP_FUNCTION_TMPL_PARTIAL_ORDER)
  434. template <class _Tp2>
  435. complex<long double>& operator=(const complex<_Tp2>& __z) {
  436. _M_re = __z._M_re;
  437. _M_im = __z._M_im;
  438. return *this;
  439. }
  440. template <class _Tp2>
  441. complex<long double>& operator+= (const complex<_Tp2>& __z) {
  442. _M_re += __z._M_re;
  443. _M_im += __z._M_im;
  444. return *this;
  445. }
  446. template <class _Tp2>
  447. complex<long double>& operator-= (const complex<_Tp2>& __z) {
  448. _M_re -= __z._M_re;
  449. _M_im -= __z._M_im;
  450. return *this;
  451. }
  452. template <class _Tp2>
  453. complex<long double>& operator*= (const complex<_Tp2>& __z) {
  454. long double __r = _M_re * __z._M_re - _M_im * __z._M_im;
  455. long double __i = _M_re * __z._M_im + _M_im * __z._M_re;
  456. _M_re = __r;
  457. _M_im = __i;
  458. return *this;
  459. }
  460. template <class _Tp2>
  461. complex<long double>& operator/= (const complex<_Tp2>& __z) {
  462. long double __r;
  463. long double __i;
  464. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  465. _M_re = __r;
  466. _M_im = __i;
  467. return *this;
  468. }
  469. # endif /* _STLP_MEMBER_TEMPLATES */
  470. _Self& operator=(const _Self& __z) {
  471. _M_re = __z._M_re;
  472. _M_im = __z._M_im;
  473. return *this;
  474. }
  475. _Self& operator+= (const _Self& __z) {
  476. _M_re += __z._M_re;
  477. _M_im += __z._M_im;
  478. return *this;
  479. }
  480. _Self& operator-= (const _Self& __z) {
  481. _M_re -= __z._M_re;
  482. _M_im -= __z._M_im;
  483. return *this;
  484. }
  485. _Self& operator*= (const _Self& __z) {
  486. value_type __r = _M_re * __z._M_re - _M_im * __z._M_im;
  487. value_type __i = _M_re * __z._M_im + _M_im * __z._M_re;
  488. _M_re = __r;
  489. _M_im = __i;
  490. return *this;
  491. }
  492. _Self& operator/= (const _Self& __z) {
  493. value_type __r;
  494. value_type __i;
  495. _div(_M_re, _M_im, __z._M_re, __z._M_im, __r, __i);
  496. _M_re = __r;
  497. _M_im = __i;
  498. return *this;
  499. }
  500. // Data members.
  501. value_type _M_re;
  502. value_type _M_im;
  503. };
  504. #endif /* _STLP_NO_LONG_DOUBLE */
  505. // Converting constructors from one of these three specialized types
  506. // to another.
  507. inline complex<float>::complex(const complex<double>& __z)
  508. : _M_re((float)__z._M_re), _M_im((float)__z._M_im) {}
  509. inline complex<double>::complex(const complex<float>& __z)
  510. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  511. #ifndef _STLP_NO_LONG_DOUBLE
  512. inline complex<float>::complex(const complex<long double>& __z)
  513. : _M_re((float)__z._M_re), _M_im((float)__z._M_im) {}
  514. inline complex<double>::complex(const complex<long double>& __z)
  515. : _M_re((double)__z._M_re), _M_im((double)__z._M_im) {}
  516. inline complex<long double>::complex(const complex<float>& __z)
  517. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  518. inline complex<long double>::complex(const complex<double>& __z)
  519. : _M_re(__z._M_re), _M_im(__z._M_im) {}
  520. #endif
  521. // Unary non-member arithmetic operators.
  522. template <class _Tp>
  523. inline complex<_Tp> _STLP_CALL operator+(const complex<_Tp>& __z)
  524. { return __z; }
  525. template <class _Tp>
  526. inline complex<_Tp> _STLP_CALL operator-(const complex<_Tp>& __z)
  527. { return complex<_Tp>(-__z._M_re, -__z._M_im); }
  528. // Non-member arithmetic operations involving one real argument.
  529. template <class _Tp>
  530. inline complex<_Tp> _STLP_CALL operator+(const _Tp& __x, const complex<_Tp>& __z)
  531. { return complex<_Tp>(__x + __z._M_re, __z._M_im); }
  532. template <class _Tp>
  533. inline complex<_Tp> _STLP_CALL operator+(const complex<_Tp>& __z, const _Tp& __x)
  534. { return complex<_Tp>(__z._M_re + __x, __z._M_im); }
  535. template <class _Tp>
  536. inline complex<_Tp> _STLP_CALL operator-(const _Tp& __x, const complex<_Tp>& __z)
  537. { return complex<_Tp>(__x - __z._M_re, -__z._M_im); }
  538. template <class _Tp>
  539. inline complex<_Tp> _STLP_CALL operator-(const complex<_Tp>& __z, const _Tp& __x)
  540. { return complex<_Tp>(__z._M_re - __x, __z._M_im); }
  541. template <class _Tp>
  542. inline complex<_Tp> _STLP_CALL operator*(const _Tp& __x, const complex<_Tp>& __z)
  543. { return complex<_Tp>(__x * __z._M_re, __x * __z._M_im); }
  544. template <class _Tp>
  545. inline complex<_Tp> _STLP_CALL operator*(const complex<_Tp>& __z, const _Tp& __x)
  546. { return complex<_Tp>(__z._M_re * __x, __z._M_im * __x); }
  547. template <class _Tp>
  548. inline complex<_Tp> _STLP_CALL operator/(const _Tp& __x, const complex<_Tp>& __z) {
  549. complex<_Tp> __result;
  550. complex<_Tp>::_div(__x,
  551. __z._M_re, __z._M_im,
  552. __result._M_re, __result._M_im);
  553. return __result;
  554. }
  555. template <class _Tp>
  556. inline complex<_Tp> _STLP_CALL operator/(const complex<_Tp>& __z, const _Tp& __x)
  557. { return complex<_Tp>(__z._M_re / __x, __z._M_im / __x); }
  558. // Non-member arithmetic operations involving two complex arguments
  559. template <class _Tp>
  560. inline complex<_Tp> _STLP_CALL
  561. operator+(const complex<_Tp>& __z1, const complex<_Tp>& __z2)
  562. { return complex<_Tp>(__z1._M_re + __z2._M_re, __z1._M_im + __z2._M_im); }
  563. template <class _Tp>
  564. inline complex<_Tp> _STLP_CALL
  565. operator-(const complex<_Tp>& __z1, const complex<_Tp>& __z2)
  566. { return complex<_Tp>(__z1._M_re - __z2._M_re, __z1._M_im - __z2._M_im); }
  567. template <class _Tp>
  568. inline complex<_Tp> _STLP_CALL
  569. operator*(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  570. return complex<_Tp>(__z1._M_re * __z2._M_re - __z1._M_im * __z2._M_im,
  571. __z1._M_re * __z2._M_im + __z1._M_im * __z2._M_re);
  572. }
  573. template <class _Tp>
  574. inline complex<_Tp> _STLP_CALL
  575. operator/(const complex<_Tp>& __z1, const complex<_Tp>& __z2) {
  576. complex<_Tp> __result;
  577. complex<_Tp>::_div(__z1._M_re, __z1._M_im,
  578. __z2._M_re, __z2._M_im,
  579. __result._M_re, __result._M_im);
  580. return __result;
  581. }
  582. // Comparison operators.
  583. template <class _Tp>
  584. inline bool _STLP_CALL operator==(const complex<_Tp>& __z1, const complex<_Tp>& __z2)
  585. { return __z1._M_re == __z2._M_re && __z1._M_im == __z2._M_im; }
  586. template <class _Tp>
  587. inline bool _STLP_CALL operator==(const complex<_Tp>& __z, const _Tp& __x)
  588. { return __z._M_re == __x && __z._M_im == 0; }
  589. template <class _Tp>
  590. inline bool _STLP_CALL operator==(const _Tp& __x, const complex<_Tp>& __z)
  591. { return __x == __z._M_re && 0 == __z._M_im; }
  592. //04/27/04 dums: removal of this check, if it is restablish
  593. //please explain why the other operators are not macro guarded
  594. //#ifdef _STLP_FUNCTION_TMPL_PARTIAL_ORDER
  595. template <class _Tp>
  596. inline bool _STLP_CALL operator!=(const complex<_Tp>& __z1, const complex<_Tp>& __z2)
  597. { return __z1._M_re != __z2._M_re || __z1._M_im != __z2._M_im; }
  598. //#endif /* _STLP_FUNCTION_TMPL_PARTIAL_ORDER */
  599. template <class _Tp>
  600. inline bool _STLP_CALL operator!=(const complex<_Tp>& __z, const _Tp& __x)
  601. { return __z._M_re != __x || __z._M_im != 0; }
  602. template <class _Tp>
  603. inline bool _STLP_CALL operator!=(const _Tp& __x, const complex<_Tp>& __z)
  604. { return __x != __z._M_re || 0 != __z._M_im; }
  605. // Other basic arithmetic operations
  606. template <class _Tp>
  607. inline _Tp _STLP_CALL real(const complex<_Tp>& __z)
  608. { return __z._M_re; }
  609. template <class _Tp>
  610. inline _Tp _STLP_CALL imag(const complex<_Tp>& __z)
  611. { return __z._M_im; }
  612. template <class _Tp>
  613. _Tp _STLP_CALL abs(const complex<_Tp>& __z);
  614. template <class _Tp>
  615. _Tp _STLP_CALL arg(const complex<_Tp>& __z);
  616. template <class _Tp>
  617. inline _Tp _STLP_CALL norm(const complex<_Tp>& __z)
  618. { return __z._M_re * __z._M_re + __z._M_im * __z._M_im; }
  619. template <class _Tp>
  620. inline complex<_Tp> _STLP_CALL conj(const complex<_Tp>& __z)
  621. { return complex<_Tp>(__z._M_re, -__z._M_im); }
  622. template <class _Tp>
  623. complex<_Tp> _STLP_CALL polar(const _Tp& __rho)
  624. { return complex<_Tp>(__rho, 0); }
  625. template <class _Tp>
  626. complex<_Tp> _STLP_CALL polar(const _Tp& __rho, const _Tp& __phi);
  627. _STLP_TEMPLATE_NULL
  628. _STLP_DECLSPEC float _STLP_CALL abs(const complex<float>&);
  629. _STLP_TEMPLATE_NULL
  630. _STLP_DECLSPEC double _STLP_CALL abs(const complex<double>&);
  631. _STLP_TEMPLATE_NULL
  632. _STLP_DECLSPEC float _STLP_CALL arg(const complex<float>&);
  633. _STLP_TEMPLATE_NULL
  634. _STLP_DECLSPEC double _STLP_CALL arg(const complex<double>&);
  635. _STLP_TEMPLATE_NULL
  636. _STLP_DECLSPEC complex<float> _STLP_CALL polar(const float& __rho, const float& __phi);
  637. _STLP_TEMPLATE_NULL
  638. _STLP_DECLSPEC complex<double> _STLP_CALL polar(const double& __rho, const double& __phi);
  639. template <class _Tp>
  640. _Tp _STLP_CALL abs(const complex<_Tp>& __z)
  641. { return _Tp(abs(complex<double>(double(__z.real()), double(__z.imag())))); }
  642. template <class _Tp>
  643. _Tp _STLP_CALL arg(const complex<_Tp>& __z)
  644. { return _Tp(arg(complex<double>(double(__z.real()), double(__z.imag())))); }
  645. template <class _Tp>
  646. complex<_Tp> _STLP_CALL polar(const _Tp& __rho, const _Tp& __phi) {
  647. complex<double> __tmp = polar(double(__rho), double(__phi));
  648. return complex<_Tp>(_Tp(__tmp.real()), _Tp(__tmp.imag()));
  649. }
  650. #if !defined (_STLP_NO_LONG_DOUBLE)
  651. _STLP_TEMPLATE_NULL
  652. _STLP_DECLSPEC long double _STLP_CALL arg(const complex<long double>&);
  653. _STLP_TEMPLATE_NULL
  654. _STLP_DECLSPEC long double _STLP_CALL abs(const complex<long double>&);
  655. _STLP_TEMPLATE_NULL
  656. _STLP_DECLSPEC complex<long double> _STLP_CALL polar(const long double&, const long double&);
  657. #endif
  658. #if !defined (_STLP_USE_NO_IOSTREAMS)
  659. _STLP_END_NAMESPACE
  660. # ifndef _STLP_INTERNAL_IOSFWD
  661. # include <stl/_iosfwd.h>
  662. # endif
  663. _STLP_BEGIN_NAMESPACE
  664. // Complex output, in the form (re,im). We use a two-step process
  665. // involving stringstream so that we get the padding right.
  666. template <class _Tp, class _CharT, class _Traits>
  667. basic_ostream<_CharT, _Traits>& _STLP_CALL
  668. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z);
  669. template <class _Tp, class _CharT, class _Traits>
  670. basic_istream<_CharT, _Traits>& _STLP_CALL
  671. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z);
  672. // Specializations for narrow characters; lets us avoid widen.
  673. _STLP_OPERATOR_TEMPLATE
  674. _STLP_DECLSPEC basic_istream<char, char_traits<char> >& _STLP_CALL
  675. operator>>(basic_istream<char, char_traits<char> >& __is, complex<float>& __z);
  676. _STLP_OPERATOR_TEMPLATE
  677. _STLP_DECLSPEC basic_istream<char, char_traits<char> >& _STLP_CALL
  678. operator>>(basic_istream<char, char_traits<char> >& __is, complex<double>& __z);
  679. _STLP_OPERATOR_TEMPLATE
  680. _STLP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL
  681. operator<<(basic_ostream<char, char_traits<char> >& __is, const complex<float>& __z);
  682. _STLP_OPERATOR_TEMPLATE
  683. _STLP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL
  684. operator<<(basic_ostream<char, char_traits<char> >& __is, const complex<double>& __z);
  685. # if !defined (_STLP_NO_LONG_DOUBLE)
  686. _STLP_OPERATOR_TEMPLATE
  687. _STLP_DECLSPEC basic_istream<char, char_traits<char> >& _STLP_CALL
  688. operator>>(basic_istream<char, char_traits<char> >& __is, complex<long double>& __z);
  689. _STLP_OPERATOR_TEMPLATE
  690. _STLP_DECLSPEC basic_ostream<char, char_traits<char> >& _STLP_CALL
  691. operator<<(basic_ostream<char, char_traits<char> >& __is, const complex<long double>& __z);
  692. # endif
  693. # if defined (_STLP_USE_TEMPLATE_EXPORT) && ! defined (_STLP_NO_WCHAR_T)
  694. _STLP_EXPORT_TEMPLATE basic_istream<wchar_t, char_traits<wchar_t> >& _STLP_CALL
  695. operator>>(basic_istream<wchar_t, char_traits<wchar_t> >&, complex<double>&);
  696. _STLP_EXPORT_TEMPLATE basic_ostream<wchar_t, char_traits<wchar_t> >& _STLP_CALL
  697. operator<<(basic_ostream<wchar_t, char_traits<wchar_t> >&, const complex<double>&);
  698. _STLP_EXPORT_TEMPLATE basic_istream<wchar_t, char_traits<wchar_t> >& _STLP_CALL
  699. operator>>(basic_istream<wchar_t, char_traits<wchar_t> >&, complex<float>&);
  700. _STLP_EXPORT_TEMPLATE basic_ostream<wchar_t, char_traits<wchar_t> >& _STLP_CALL
  701. operator<<(basic_ostream<wchar_t, char_traits<wchar_t> >&, const complex<float>&);
  702. # if !defined (_STLP_NO_LONG_DOUBLE)
  703. _STLP_EXPORT_TEMPLATE basic_istream<wchar_t, char_traits<wchar_t> >& _STLP_CALL
  704. operator>>(basic_istream<wchar_t, char_traits<wchar_t> >&, complex<long double>&);
  705. _STLP_EXPORT_TEMPLATE basic_ostream<wchar_t, char_traits<wchar_t> >& _STLP_CALL
  706. operator<<(basic_ostream<wchar_t, char_traits<wchar_t> >&, const complex<long double>&);
  707. # endif
  708. # endif
  709. #endif
  710. // Transcendental functions. These are defined only for float,
  711. // double, and long double. (Sqrt isn't transcendental, of course,
  712. // but it's included in this section anyway.)
  713. _STLP_DECLSPEC complex<float> _STLP_CALL sqrt(const complex<float>&);
  714. _STLP_DECLSPEC complex<float> _STLP_CALL exp(const complex<float>&);
  715. _STLP_DECLSPEC complex<float> _STLP_CALL log(const complex<float>&);
  716. _STLP_DECLSPEC complex<float> _STLP_CALL log10(const complex<float>&);
  717. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>&, int);
  718. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>&, const float&);
  719. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const float&, const complex<float>&);
  720. _STLP_DECLSPEC complex<float> _STLP_CALL pow(const complex<float>&, const complex<float>&);
  721. _STLP_DECLSPEC complex<float> _STLP_CALL sin(const complex<float>&);
  722. _STLP_DECLSPEC complex<float> _STLP_CALL cos(const complex<float>&);
  723. _STLP_DECLSPEC complex<float> _STLP_CALL tan(const complex<float>&);
  724. _STLP_DECLSPEC complex<float> _STLP_CALL sinh(const complex<float>&);
  725. _STLP_DECLSPEC complex<float> _STLP_CALL cosh(const complex<float>&);
  726. _STLP_DECLSPEC complex<float> _STLP_CALL tanh(const complex<float>&);
  727. _STLP_DECLSPEC complex<double> _STLP_CALL sqrt(const complex<double>&);
  728. _STLP_DECLSPEC complex<double> _STLP_CALL exp(const complex<double>&);
  729. _STLP_DECLSPEC complex<double> _STLP_CALL log(const complex<double>&);
  730. _STLP_DECLSPEC complex<double> _STLP_CALL log10(const complex<double>&);
  731. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>&, int);
  732. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>&, const double&);
  733. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const double&, const complex<double>&);
  734. _STLP_DECLSPEC complex<double> _STLP_CALL pow(const complex<double>&, const complex<double>&);
  735. _STLP_DECLSPEC complex<double> _STLP_CALL sin(const complex<double>&);
  736. _STLP_DECLSPEC complex<double> _STLP_CALL cos(const complex<double>&);
  737. _STLP_DECLSPEC complex<double> _STLP_CALL tan(const complex<double>&);
  738. _STLP_DECLSPEC complex<double> _STLP_CALL sinh(const complex<double>&);
  739. _STLP_DECLSPEC complex<double> _STLP_CALL cosh(const complex<double>&);
  740. _STLP_DECLSPEC complex<double> _STLP_CALL tanh(const complex<double>&);
  741. #if !defined (_STLP_NO_LONG_DOUBLE)
  742. _STLP_DECLSPEC complex<long double> _STLP_CALL sqrt(const complex<long double>&);
  743. _STLP_DECLSPEC complex<long double> _STLP_CALL exp(const complex<long double>&);
  744. _STLP_DECLSPEC complex<long double> _STLP_CALL log(const complex<long double>&);
  745. _STLP_DECLSPEC complex<long double> _STLP_CALL log10(const complex<long double>&);
  746. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>&, int);
  747. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>&, const long double&);
  748. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const long double&, const complex<long double>&);
  749. _STLP_DECLSPEC complex<long double> _STLP_CALL pow(const complex<long double>&,
  750. const complex<long double>&);
  751. _STLP_DECLSPEC complex<long double> _STLP_CALL sin(const complex<long double>&);
  752. _STLP_DECLSPEC complex<long double> _STLP_CALL cos(const complex<long double>&);
  753. _STLP_DECLSPEC complex<long double> _STLP_CALL tan(const complex<long double>&);
  754. _STLP_DECLSPEC complex<long double> _STLP_CALL sinh(const complex<long double>&);
  755. _STLP_DECLSPEC complex<long double> _STLP_CALL cosh(const complex<long double>&);
  756. _STLP_DECLSPEC complex<long double> _STLP_CALL tanh(const complex<long double>&);
  757. #endif
  758. _STLP_END_NAMESPACE
  759. #ifndef _STLP_LINK_TIME_INSTANTIATION
  760. # include <stl/_complex.c>
  761. #endif
  762. #endif
  763. // Local Variables:
  764. // mode:C++
  765. // End: