complex 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588
  1. // The template and inlines for the -*- C++ -*- complex number classes.
  2. // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. // 2006, 2007, 2008, 2009
  4. // Free Software Foundation, Inc.
  5. //
  6. // This file is part of the GNU ISO C++ Library. This library is free
  7. // software; you can redistribute it and/or modify it under the
  8. // terms of the GNU General Public License as published by the
  9. // Free Software Foundation; either version 3, or (at your option)
  10. // any later version.
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License and
  19. // a copy of the GCC Runtime Library Exception along with this program;
  20. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  21. // <http://www.gnu.org/licenses/>.
  22. /** @file include/complex
  23. * This is a Standard C++ Library header.
  24. */
  25. //
  26. // ISO C++ 14882: 26.2 Complex Numbers
  27. // Note: this is not a conforming implementation.
  28. // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
  29. // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
  30. //
  31. #ifndef _GLIBCXX_COMPLEX
  32. #define _GLIBCXX_COMPLEX 1
  33. #pragma GCC system_header
  34. #include <bits/c++config.h>
  35. #include <bits/cpp_type_traits.h>
  36. #include <ext/type_traits.h>
  37. #include <cmath>
  38. #include <sstream>
  39. _GLIBCXX_BEGIN_NAMESPACE(std)
  40. /**
  41. * @defgroup complex_numbers Complex Numbers
  42. * @ingroup numerics
  43. *
  44. * Classes and functions for complex numbers.
  45. * @{
  46. */
  47. // Forward declarations.
  48. template<typename _Tp> class complex;
  49. template<> class complex<float>;
  50. template<> class complex<double>;
  51. template<> class complex<long double>;
  52. /// Return magnitude of @a z.
  53. template<typename _Tp> _Tp abs(const complex<_Tp>&);
  54. /// Return phase angle of @a z.
  55. template<typename _Tp> _Tp arg(const complex<_Tp>&);
  56. /// Return @a z magnitude squared.
  57. template<typename _Tp> _Tp norm(const complex<_Tp>&);
  58. /// Return complex conjugate of @a z.
  59. template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
  60. /// Return complex with magnitude @a rho and angle @a theta.
  61. template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
  62. // Transcendentals:
  63. /// Return complex cosine of @a z.
  64. template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
  65. /// Return complex hyperbolic cosine of @a z.
  66. template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
  67. /// Return complex base e exponential of @a z.
  68. template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
  69. /// Return complex natural logarithm of @a z.
  70. template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
  71. /// Return complex base 10 logarithm of @a z.
  72. template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
  73. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  74. // DR 844.
  75. /// Return @a x to the @a y'th power.
  76. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
  77. #endif
  78. /// Return @a x to the @a y'th power.
  79. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
  80. /// Return @a x to the @a y'th power.
  81. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
  82. const complex<_Tp>&);
  83. /// Return @a x to the @a y'th power.
  84. template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
  85. /// Return complex sine of @a z.
  86. template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
  87. /// Return complex hyperbolic sine of @a z.
  88. template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
  89. /// Return complex square root of @a z.
  90. template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
  91. /// Return complex tangent of @a z.
  92. template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
  93. /// Return complex hyperbolic tangent of @a z.
  94. template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
  95. // 26.2.2 Primary template class complex
  96. /**
  97. * Template to represent complex numbers.
  98. *
  99. * Specializations for float, double, and long double are part of the
  100. * library. Results with any other type are not guaranteed.
  101. *
  102. * @param Tp Type of real and imaginary values.
  103. */
  104. template<typename _Tp>
  105. struct complex
  106. {
  107. /// Value typedef.
  108. typedef _Tp value_type;
  109. /// Default constructor. First parameter is x, second parameter is y.
  110. /// Unspecified parameters default to 0.
  111. complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
  112. : _M_real(__r), _M_imag(__i) { }
  113. // Lets the compiler synthesize the copy constructor
  114. // complex (const complex<_Tp>&);
  115. /// Copy constructor.
  116. template<typename _Up>
  117. complex(const complex<_Up>& __z)
  118. : _M_real(__z.real()), _M_imag(__z.imag()) { }
  119. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  120. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  121. // DR 387. std::complex over-encapsulated.
  122. _Tp real() const
  123. { return _M_real; }
  124. _Tp imag() const
  125. { return _M_imag; }
  126. #else
  127. /// Return real part of complex number.
  128. _Tp& real()
  129. { return _M_real; }
  130. /// Return real part of complex number.
  131. const _Tp& real() const
  132. { return _M_real; }
  133. /// Return imaginary part of complex number.
  134. _Tp& imag()
  135. { return _M_imag; }
  136. /// Return imaginary part of complex number.
  137. const _Tp& imag() const
  138. { return _M_imag; }
  139. #endif
  140. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  141. // DR 387. std::complex over-encapsulated.
  142. void real(_Tp __val)
  143. { _M_real = __val; }
  144. void imag(_Tp __val)
  145. { _M_imag = __val; }
  146. /// Assign this complex number to scalar @a t.
  147. complex<_Tp>& operator=(const _Tp&);
  148. /// Add @a t to this complex number.
  149. // 26.2.5/1
  150. complex<_Tp>&
  151. operator+=(const _Tp& __t)
  152. {
  153. _M_real += __t;
  154. return *this;
  155. }
  156. /// Subtract @a t from this complex number.
  157. // 26.2.5/3
  158. complex<_Tp>&
  159. operator-=(const _Tp& __t)
  160. {
  161. _M_real -= __t;
  162. return *this;
  163. }
  164. /// Multiply this complex number by @a t.
  165. complex<_Tp>& operator*=(const _Tp&);
  166. /// Divide this complex number by @a t.
  167. complex<_Tp>& operator/=(const _Tp&);
  168. // Lets the compiler synthesize the
  169. // copy and assignment operator
  170. // complex<_Tp>& operator= (const complex<_Tp>&);
  171. /// Assign this complex number to complex @a z.
  172. template<typename _Up>
  173. complex<_Tp>& operator=(const complex<_Up>&);
  174. /// Add @a z to this complex number.
  175. template<typename _Up>
  176. complex<_Tp>& operator+=(const complex<_Up>&);
  177. /// Subtract @a z from this complex number.
  178. template<typename _Up>
  179. complex<_Tp>& operator-=(const complex<_Up>&);
  180. /// Multiply this complex number by @a z.
  181. template<typename _Up>
  182. complex<_Tp>& operator*=(const complex<_Up>&);
  183. /// Divide this complex number by @a z.
  184. template<typename _Up>
  185. complex<_Tp>& operator/=(const complex<_Up>&);
  186. const complex& __rep() const
  187. { return *this; }
  188. private:
  189. _Tp _M_real;
  190. _Tp _M_imag;
  191. };
  192. template<typename _Tp>
  193. complex<_Tp>&
  194. complex<_Tp>::operator=(const _Tp& __t)
  195. {
  196. _M_real = __t;
  197. _M_imag = _Tp();
  198. return *this;
  199. }
  200. // 26.2.5/5
  201. template<typename _Tp>
  202. complex<_Tp>&
  203. complex<_Tp>::operator*=(const _Tp& __t)
  204. {
  205. _M_real *= __t;
  206. _M_imag *= __t;
  207. return *this;
  208. }
  209. // 26.2.5/7
  210. template<typename _Tp>
  211. complex<_Tp>&
  212. complex<_Tp>::operator/=(const _Tp& __t)
  213. {
  214. _M_real /= __t;
  215. _M_imag /= __t;
  216. return *this;
  217. }
  218. template<typename _Tp>
  219. template<typename _Up>
  220. complex<_Tp>&
  221. complex<_Tp>::operator=(const complex<_Up>& __z)
  222. {
  223. _M_real = __z.real();
  224. _M_imag = __z.imag();
  225. return *this;
  226. }
  227. // 26.2.5/9
  228. template<typename _Tp>
  229. template<typename _Up>
  230. complex<_Tp>&
  231. complex<_Tp>::operator+=(const complex<_Up>& __z)
  232. {
  233. _M_real += __z.real();
  234. _M_imag += __z.imag();
  235. return *this;
  236. }
  237. // 26.2.5/11
  238. template<typename _Tp>
  239. template<typename _Up>
  240. complex<_Tp>&
  241. complex<_Tp>::operator-=(const complex<_Up>& __z)
  242. {
  243. _M_real -= __z.real();
  244. _M_imag -= __z.imag();
  245. return *this;
  246. }
  247. // 26.2.5/13
  248. // XXX: This is a grammar school implementation.
  249. template<typename _Tp>
  250. template<typename _Up>
  251. complex<_Tp>&
  252. complex<_Tp>::operator*=(const complex<_Up>& __z)
  253. {
  254. const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
  255. _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
  256. _M_real = __r;
  257. return *this;
  258. }
  259. // 26.2.5/15
  260. // XXX: This is a grammar school implementation.
  261. template<typename _Tp>
  262. template<typename _Up>
  263. complex<_Tp>&
  264. complex<_Tp>::operator/=(const complex<_Up>& __z)
  265. {
  266. const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
  267. const _Tp __n = std::norm(__z);
  268. _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
  269. _M_real = __r / __n;
  270. return *this;
  271. }
  272. // Operators:
  273. //@{
  274. /// Return new complex value @a x plus @a y.
  275. template<typename _Tp>
  276. inline complex<_Tp>
  277. operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
  278. {
  279. complex<_Tp> __r = __x;
  280. __r += __y;
  281. return __r;
  282. }
  283. template<typename _Tp>
  284. inline complex<_Tp>
  285. operator+(const complex<_Tp>& __x, const _Tp& __y)
  286. {
  287. complex<_Tp> __r = __x;
  288. __r += __y;
  289. return __r;
  290. }
  291. template<typename _Tp>
  292. inline complex<_Tp>
  293. operator+(const _Tp& __x, const complex<_Tp>& __y)
  294. {
  295. complex<_Tp> __r = __y;
  296. __r += __x;
  297. return __r;
  298. }
  299. //@}
  300. //@{
  301. /// Return new complex value @a x minus @a y.
  302. template<typename _Tp>
  303. inline complex<_Tp>
  304. operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
  305. {
  306. complex<_Tp> __r = __x;
  307. __r -= __y;
  308. return __r;
  309. }
  310. template<typename _Tp>
  311. inline complex<_Tp>
  312. operator-(const complex<_Tp>& __x, const _Tp& __y)
  313. {
  314. complex<_Tp> __r = __x;
  315. __r -= __y;
  316. return __r;
  317. }
  318. template<typename _Tp>
  319. inline complex<_Tp>
  320. operator-(const _Tp& __x, const complex<_Tp>& __y)
  321. {
  322. complex<_Tp> __r(__x, -__y.imag());
  323. __r -= __y.real();
  324. return __r;
  325. }
  326. //@}
  327. //@{
  328. /// Return new complex value @a x times @a y.
  329. template<typename _Tp>
  330. inline complex<_Tp>
  331. operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
  332. {
  333. complex<_Tp> __r = __x;
  334. __r *= __y;
  335. return __r;
  336. }
  337. template<typename _Tp>
  338. inline complex<_Tp>
  339. operator*(const complex<_Tp>& __x, const _Tp& __y)
  340. {
  341. complex<_Tp> __r = __x;
  342. __r *= __y;
  343. return __r;
  344. }
  345. template<typename _Tp>
  346. inline complex<_Tp>
  347. operator*(const _Tp& __x, const complex<_Tp>& __y)
  348. {
  349. complex<_Tp> __r = __y;
  350. __r *= __x;
  351. return __r;
  352. }
  353. //@}
  354. //@{
  355. /// Return new complex value @a x divided by @a y.
  356. template<typename _Tp>
  357. inline complex<_Tp>
  358. operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
  359. {
  360. complex<_Tp> __r = __x;
  361. __r /= __y;
  362. return __r;
  363. }
  364. template<typename _Tp>
  365. inline complex<_Tp>
  366. operator/(const complex<_Tp>& __x, const _Tp& __y)
  367. {
  368. complex<_Tp> __r = __x;
  369. __r /= __y;
  370. return __r;
  371. }
  372. template<typename _Tp>
  373. inline complex<_Tp>
  374. operator/(const _Tp& __x, const complex<_Tp>& __y)
  375. {
  376. complex<_Tp> __r = __x;
  377. __r /= __y;
  378. return __r;
  379. }
  380. //@}
  381. /// Return @a x.
  382. template<typename _Tp>
  383. inline complex<_Tp>
  384. operator+(const complex<_Tp>& __x)
  385. { return __x; }
  386. /// Return complex negation of @a x.
  387. template<typename _Tp>
  388. inline complex<_Tp>
  389. operator-(const complex<_Tp>& __x)
  390. { return complex<_Tp>(-__x.real(), -__x.imag()); }
  391. //@{
  392. /// Return true if @a x is equal to @a y.
  393. template<typename _Tp>
  394. inline bool
  395. operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
  396. { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
  397. template<typename _Tp>
  398. inline bool
  399. operator==(const complex<_Tp>& __x, const _Tp& __y)
  400. { return __x.real() == __y && __x.imag() == _Tp(); }
  401. template<typename _Tp>
  402. inline bool
  403. operator==(const _Tp& __x, const complex<_Tp>& __y)
  404. { return __x == __y.real() && _Tp() == __y.imag(); }
  405. //@}
  406. //@{
  407. /// Return false if @a x is equal to @a y.
  408. template<typename _Tp>
  409. inline bool
  410. operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
  411. { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
  412. template<typename _Tp>
  413. inline bool
  414. operator!=(const complex<_Tp>& __x, const _Tp& __y)
  415. { return __x.real() != __y || __x.imag() != _Tp(); }
  416. template<typename _Tp>
  417. inline bool
  418. operator!=(const _Tp& __x, const complex<_Tp>& __y)
  419. { return __x != __y.real() || _Tp() != __y.imag(); }
  420. //@}
  421. /// Extraction operator for complex values.
  422. template<typename _Tp, typename _CharT, class _Traits>
  423. basic_istream<_CharT, _Traits>&
  424. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
  425. {
  426. _Tp __re_x, __im_x;
  427. _CharT __ch;
  428. __is >> __ch;
  429. if (__ch == '(')
  430. {
  431. __is >> __re_x >> __ch;
  432. if (__ch == ',')
  433. {
  434. __is >> __im_x >> __ch;
  435. if (__ch == ')')
  436. __x = complex<_Tp>(__re_x, __im_x);
  437. else
  438. __is.setstate(ios_base::failbit);
  439. }
  440. else if (__ch == ')')
  441. __x = __re_x;
  442. else
  443. __is.setstate(ios_base::failbit);
  444. }
  445. else
  446. {
  447. __is.putback(__ch);
  448. __is >> __re_x;
  449. __x = __re_x;
  450. }
  451. return __is;
  452. }
  453. /// Insertion operator for complex values.
  454. template<typename _Tp, typename _CharT, class _Traits>
  455. basic_ostream<_CharT, _Traits>&
  456. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
  457. {
  458. basic_ostringstream<_CharT, _Traits> __s;
  459. __s.flags(__os.flags());
  460. __s.imbue(__os.getloc());
  461. __s.precision(__os.precision());
  462. __s << '(' << __x.real() << ',' << __x.imag() << ')';
  463. return __os << __s.str();
  464. }
  465. // Values
  466. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  467. template<typename _Tp>
  468. inline _Tp
  469. real(const complex<_Tp>& __z)
  470. { return __z.real(); }
  471. template<typename _Tp>
  472. inline _Tp
  473. imag(const complex<_Tp>& __z)
  474. { return __z.imag(); }
  475. #else
  476. template<typename _Tp>
  477. inline _Tp&
  478. real(complex<_Tp>& __z)
  479. { return __z.real(); }
  480. template<typename _Tp>
  481. inline const _Tp&
  482. real(const complex<_Tp>& __z)
  483. { return __z.real(); }
  484. template<typename _Tp>
  485. inline _Tp&
  486. imag(complex<_Tp>& __z)
  487. { return __z.imag(); }
  488. template<typename _Tp>
  489. inline const _Tp&
  490. imag(const complex<_Tp>& __z)
  491. { return __z.imag(); }
  492. #endif
  493. // 26.2.7/3 abs(__z): Returns the magnitude of __z.
  494. template<typename _Tp>
  495. inline _Tp
  496. __complex_abs(const complex<_Tp>& __z)
  497. {
  498. _Tp __x = __z.real();
  499. _Tp __y = __z.imag();
  500. const _Tp __s = std::max(abs(__x), abs(__y));
  501. if (__s == _Tp()) // well ...
  502. return __s;
  503. __x /= __s;
  504. __y /= __s;
  505. return __s * sqrt(__x * __x + __y * __y);
  506. }
  507. #if _GLIBCXX_USE_C99_COMPLEX
  508. inline float
  509. __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
  510. inline double
  511. __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
  512. inline long double
  513. __complex_abs(const __complex__ long double& __z)
  514. { return __builtin_cabsl(__z); }
  515. template<typename _Tp>
  516. inline _Tp
  517. abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
  518. #else
  519. template<typename _Tp>
  520. inline _Tp
  521. abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
  522. #endif
  523. // 26.2.7/4: arg(__z): Returns the phase angle of __z.
  524. template<typename _Tp>
  525. inline _Tp
  526. __complex_arg(const complex<_Tp>& __z)
  527. { return atan2(__z.imag(), __z.real()); }
  528. #if _GLIBCXX_USE_C99_COMPLEX
  529. inline float
  530. __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
  531. inline double
  532. __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
  533. inline long double
  534. __complex_arg(const __complex__ long double& __z)
  535. { return __builtin_cargl(__z); }
  536. template<typename _Tp>
  537. inline _Tp
  538. arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
  539. #else
  540. template<typename _Tp>
  541. inline _Tp
  542. arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
  543. #endif
  544. // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
  545. // As defined, norm() is -not- a norm is the common mathematical
  546. // sens used in numerics. The helper class _Norm_helper<> tries to
  547. // distinguish between builtin floating point and the rest, so as
  548. // to deliver an answer as close as possible to the real value.
  549. template<bool>
  550. struct _Norm_helper
  551. {
  552. template<typename _Tp>
  553. static inline _Tp _S_do_it(const complex<_Tp>& __z)
  554. {
  555. const _Tp __x = __z.real();
  556. const _Tp __y = __z.imag();
  557. return __x * __x + __y * __y;
  558. }
  559. };
  560. template<>
  561. struct _Norm_helper<true>
  562. {
  563. template<typename _Tp>
  564. static inline _Tp _S_do_it(const complex<_Tp>& __z)
  565. {
  566. _Tp __res = std::abs(__z);
  567. return __res * __res;
  568. }
  569. };
  570. template<typename _Tp>
  571. inline _Tp
  572. norm(const complex<_Tp>& __z)
  573. {
  574. return _Norm_helper<__is_floating<_Tp>::__value
  575. && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
  576. }
  577. template<typename _Tp>
  578. inline complex<_Tp>
  579. polar(const _Tp& __rho, const _Tp& __theta)
  580. { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); }
  581. template<typename _Tp>
  582. inline complex<_Tp>
  583. conj(const complex<_Tp>& __z)
  584. { return complex<_Tp>(__z.real(), -__z.imag()); }
  585. // Transcendentals
  586. // 26.2.8/1 cos(__z): Returns the cosine of __z.
  587. template<typename _Tp>
  588. inline complex<_Tp>
  589. __complex_cos(const complex<_Tp>& __z)
  590. {
  591. const _Tp __x = __z.real();
  592. const _Tp __y = __z.imag();
  593. return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
  594. }
  595. #if _GLIBCXX_USE_C99_COMPLEX
  596. inline __complex__ float
  597. __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
  598. inline __complex__ double
  599. __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
  600. inline __complex__ long double
  601. __complex_cos(const __complex__ long double& __z)
  602. { return __builtin_ccosl(__z); }
  603. template<typename _Tp>
  604. inline complex<_Tp>
  605. cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
  606. #else
  607. template<typename _Tp>
  608. inline complex<_Tp>
  609. cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
  610. #endif
  611. // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
  612. template<typename _Tp>
  613. inline complex<_Tp>
  614. __complex_cosh(const complex<_Tp>& __z)
  615. {
  616. const _Tp __x = __z.real();
  617. const _Tp __y = __z.imag();
  618. return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
  619. }
  620. #if _GLIBCXX_USE_C99_COMPLEX
  621. inline __complex__ float
  622. __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
  623. inline __complex__ double
  624. __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
  625. inline __complex__ long double
  626. __complex_cosh(const __complex__ long double& __z)
  627. { return __builtin_ccoshl(__z); }
  628. template<typename _Tp>
  629. inline complex<_Tp>
  630. cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
  631. #else
  632. template<typename _Tp>
  633. inline complex<_Tp>
  634. cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
  635. #endif
  636. // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
  637. template<typename _Tp>
  638. inline complex<_Tp>
  639. __complex_exp(const complex<_Tp>& __z)
  640. { return std::polar(exp(__z.real()), __z.imag()); }
  641. #if _GLIBCXX_USE_C99_COMPLEX
  642. inline __complex__ float
  643. __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
  644. inline __complex__ double
  645. __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
  646. inline __complex__ long double
  647. __complex_exp(const __complex__ long double& __z)
  648. { return __builtin_cexpl(__z); }
  649. template<typename _Tp>
  650. inline complex<_Tp>
  651. exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
  652. #else
  653. template<typename _Tp>
  654. inline complex<_Tp>
  655. exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
  656. #endif
  657. // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
  658. // The branch cut is along the negative axis.
  659. template<typename _Tp>
  660. inline complex<_Tp>
  661. __complex_log(const complex<_Tp>& __z)
  662. { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
  663. #if _GLIBCXX_USE_C99_COMPLEX
  664. inline __complex__ float
  665. __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
  666. inline __complex__ double
  667. __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
  668. inline __complex__ long double
  669. __complex_log(const __complex__ long double& __z)
  670. { return __builtin_clogl(__z); }
  671. template<typename _Tp>
  672. inline complex<_Tp>
  673. log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
  674. #else
  675. template<typename _Tp>
  676. inline complex<_Tp>
  677. log(const complex<_Tp>& __z) { return __complex_log(__z); }
  678. #endif
  679. template<typename _Tp>
  680. inline complex<_Tp>
  681. log10(const complex<_Tp>& __z)
  682. { return std::log(__z) / log(_Tp(10.0)); }
  683. // 26.2.8/10 sin(__z): Returns the sine of __z.
  684. template<typename _Tp>
  685. inline complex<_Tp>
  686. __complex_sin(const complex<_Tp>& __z)
  687. {
  688. const _Tp __x = __z.real();
  689. const _Tp __y = __z.imag();
  690. return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
  691. }
  692. #if _GLIBCXX_USE_C99_COMPLEX
  693. inline __complex__ float
  694. __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
  695. inline __complex__ double
  696. __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
  697. inline __complex__ long double
  698. __complex_sin(const __complex__ long double& __z)
  699. { return __builtin_csinl(__z); }
  700. template<typename _Tp>
  701. inline complex<_Tp>
  702. sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
  703. #else
  704. template<typename _Tp>
  705. inline complex<_Tp>
  706. sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
  707. #endif
  708. // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
  709. template<typename _Tp>
  710. inline complex<_Tp>
  711. __complex_sinh(const complex<_Tp>& __z)
  712. {
  713. const _Tp __x = __z.real();
  714. const _Tp __y = __z.imag();
  715. return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
  716. }
  717. #if _GLIBCXX_USE_C99_COMPLEX
  718. inline __complex__ float
  719. __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
  720. inline __complex__ double
  721. __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
  722. inline __complex__ long double
  723. __complex_sinh(const __complex__ long double& __z)
  724. { return __builtin_csinhl(__z); }
  725. template<typename _Tp>
  726. inline complex<_Tp>
  727. sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
  728. #else
  729. template<typename _Tp>
  730. inline complex<_Tp>
  731. sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
  732. #endif
  733. // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
  734. // The branch cut is on the negative axis.
  735. template<typename _Tp>
  736. complex<_Tp>
  737. __complex_sqrt(const complex<_Tp>& __z)
  738. {
  739. _Tp __x = __z.real();
  740. _Tp __y = __z.imag();
  741. if (__x == _Tp())
  742. {
  743. _Tp __t = sqrt(abs(__y) / 2);
  744. return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
  745. }
  746. else
  747. {
  748. _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
  749. _Tp __u = __t / 2;
  750. return __x > _Tp()
  751. ? complex<_Tp>(__u, __y / __t)
  752. : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
  753. }
  754. }
  755. #if _GLIBCXX_USE_C99_COMPLEX
  756. inline __complex__ float
  757. __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
  758. inline __complex__ double
  759. __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
  760. inline __complex__ long double
  761. __complex_sqrt(const __complex__ long double& __z)
  762. { return __builtin_csqrtl(__z); }
  763. template<typename _Tp>
  764. inline complex<_Tp>
  765. sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
  766. #else
  767. template<typename _Tp>
  768. inline complex<_Tp>
  769. sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
  770. #endif
  771. // 26.2.8/14 tan(__z): Return the complex tangent of __z.
  772. template<typename _Tp>
  773. inline complex<_Tp>
  774. __complex_tan(const complex<_Tp>& __z)
  775. { return std::sin(__z) / std::cos(__z); }
  776. #if _GLIBCXX_USE_C99_COMPLEX
  777. inline __complex__ float
  778. __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
  779. inline __complex__ double
  780. __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
  781. inline __complex__ long double
  782. __complex_tan(const __complex__ long double& __z)
  783. { return __builtin_ctanl(__z); }
  784. template<typename _Tp>
  785. inline complex<_Tp>
  786. tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
  787. #else
  788. template<typename _Tp>
  789. inline complex<_Tp>
  790. tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
  791. #endif
  792. // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
  793. template<typename _Tp>
  794. inline complex<_Tp>
  795. __complex_tanh(const complex<_Tp>& __z)
  796. { return std::sinh(__z) / std::cosh(__z); }
  797. #if _GLIBCXX_USE_C99_COMPLEX
  798. inline __complex__ float
  799. __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
  800. inline __complex__ double
  801. __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
  802. inline __complex__ long double
  803. __complex_tanh(const __complex__ long double& __z)
  804. { return __builtin_ctanhl(__z); }
  805. template<typename _Tp>
  806. inline complex<_Tp>
  807. tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
  808. #else
  809. template<typename _Tp>
  810. inline complex<_Tp>
  811. tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
  812. #endif
  813. // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
  814. // raised to the __y-th power. The branch
  815. // cut is on the negative axis.
  816. #ifndef __GXX_EXPERIMENTAL_CXX0X__
  817. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  818. // DR 844. complex pow return type is ambiguous.
  819. template<typename _Tp>
  820. inline complex<_Tp>
  821. pow(const complex<_Tp>& __z, int __n)
  822. { return std::__pow_helper(__z, __n); }
  823. #endif
  824. template<typename _Tp>
  825. complex<_Tp>
  826. pow(const complex<_Tp>& __x, const _Tp& __y)
  827. {
  828. #ifndef _GLIBCXX_USE_C99_COMPLEX
  829. if (__x == _Tp())
  830. return _Tp();
  831. #endif
  832. if (__x.imag() == _Tp() && __x.real() > _Tp())
  833. return pow(__x.real(), __y);
  834. complex<_Tp> __t = std::log(__x);
  835. return std::polar(exp(__y * __t.real()), __y * __t.imag());
  836. }
  837. template<typename _Tp>
  838. inline complex<_Tp>
  839. __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  840. { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
  841. #if _GLIBCXX_USE_C99_COMPLEX
  842. inline __complex__ float
  843. __complex_pow(__complex__ float __x, __complex__ float __y)
  844. { return __builtin_cpowf(__x, __y); }
  845. inline __complex__ double
  846. __complex_pow(__complex__ double __x, __complex__ double __y)
  847. { return __builtin_cpow(__x, __y); }
  848. inline __complex__ long double
  849. __complex_pow(const __complex__ long double& __x,
  850. const __complex__ long double& __y)
  851. { return __builtin_cpowl(__x, __y); }
  852. template<typename _Tp>
  853. inline complex<_Tp>
  854. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  855. { return __complex_pow(__x.__rep(), __y.__rep()); }
  856. #else
  857. template<typename _Tp>
  858. inline complex<_Tp>
  859. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  860. { return __complex_pow(__x, __y); }
  861. #endif
  862. template<typename _Tp>
  863. inline complex<_Tp>
  864. pow(const _Tp& __x, const complex<_Tp>& __y)
  865. {
  866. return __x > _Tp() ? std::polar(pow(__x, __y.real()),
  867. __y.imag() * log(__x))
  868. : std::pow(complex<_Tp>(__x), __y);
  869. }
  870. // 26.2.3 complex specializations
  871. // complex<float> specialization
  872. template<>
  873. struct complex<float>
  874. {
  875. typedef float value_type;
  876. typedef __complex__ float _ComplexT;
  877. complex(_ComplexT __z) : _M_value(__z) { }
  878. complex(float __r = 0.0f, float __i = 0.0f)
  879. {
  880. __real__ _M_value = __r;
  881. __imag__ _M_value = __i;
  882. }
  883. explicit complex(const complex<double>&);
  884. explicit complex(const complex<long double>&);
  885. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  886. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  887. // DR 387. std::complex over-encapsulated.
  888. float real() const
  889. { return __real__ _M_value; }
  890. float imag() const
  891. { return __imag__ _M_value; }
  892. #else
  893. float& real()
  894. { return __real__ _M_value; }
  895. const float& real() const
  896. { return __real__ _M_value; }
  897. float& imag()
  898. { return __imag__ _M_value; }
  899. const float& imag() const
  900. { return __imag__ _M_value; }
  901. #endif
  902. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  903. // DR 387. std::complex over-encapsulated.
  904. void real(float __val)
  905. { __real__ _M_value = __val; }
  906. void imag(float __val)
  907. { __imag__ _M_value = __val; }
  908. complex<float>&
  909. operator=(float __f)
  910. {
  911. __real__ _M_value = __f;
  912. __imag__ _M_value = 0.0f;
  913. return *this;
  914. }
  915. complex<float>&
  916. operator+=(float __f)
  917. {
  918. __real__ _M_value += __f;
  919. return *this;
  920. }
  921. complex<float>&
  922. operator-=(float __f)
  923. {
  924. __real__ _M_value -= __f;
  925. return *this;
  926. }
  927. complex<float>&
  928. operator*=(float __f)
  929. {
  930. _M_value *= __f;
  931. return *this;
  932. }
  933. complex<float>&
  934. operator/=(float __f)
  935. {
  936. _M_value /= __f;
  937. return *this;
  938. }
  939. // Let the compiler synthesize the copy and assignment
  940. // operator. It always does a pretty good job.
  941. // complex& operator=(const complex&);
  942. template<typename _Tp>
  943. complex<float>&
  944. operator=(const complex<_Tp>& __z)
  945. {
  946. __real__ _M_value = __z.real();
  947. __imag__ _M_value = __z.imag();
  948. return *this;
  949. }
  950. template<typename _Tp>
  951. complex<float>&
  952. operator+=(const complex<_Tp>& __z)
  953. {
  954. __real__ _M_value += __z.real();
  955. __imag__ _M_value += __z.imag();
  956. return *this;
  957. }
  958. template<class _Tp>
  959. complex<float>&
  960. operator-=(const complex<_Tp>& __z)
  961. {
  962. __real__ _M_value -= __z.real();
  963. __imag__ _M_value -= __z.imag();
  964. return *this;
  965. }
  966. template<class _Tp>
  967. complex<float>&
  968. operator*=(const complex<_Tp>& __z)
  969. {
  970. _ComplexT __t;
  971. __real__ __t = __z.real();
  972. __imag__ __t = __z.imag();
  973. _M_value *= __t;
  974. return *this;
  975. }
  976. template<class _Tp>
  977. complex<float>&
  978. operator/=(const complex<_Tp>& __z)
  979. {
  980. _ComplexT __t;
  981. __real__ __t = __z.real();
  982. __imag__ __t = __z.imag();
  983. _M_value /= __t;
  984. return *this;
  985. }
  986. const _ComplexT& __rep() const { return _M_value; }
  987. private:
  988. _ComplexT _M_value;
  989. };
  990. // 26.2.3 complex specializations
  991. // complex<double> specialization
  992. template<>
  993. struct complex<double>
  994. {
  995. typedef double value_type;
  996. typedef __complex__ double _ComplexT;
  997. complex(_ComplexT __z) : _M_value(__z) { }
  998. complex(double __r = 0.0, double __i = 0.0)
  999. {
  1000. __real__ _M_value = __r;
  1001. __imag__ _M_value = __i;
  1002. }
  1003. complex(const complex<float>& __z)
  1004. : _M_value(__z.__rep()) { }
  1005. explicit complex(const complex<long double>&);
  1006. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  1007. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1008. // DR 387. std::complex over-encapsulated.
  1009. double real() const
  1010. { return __real__ _M_value; }
  1011. double imag() const
  1012. { return __imag__ _M_value; }
  1013. #else
  1014. double& real()
  1015. { return __real__ _M_value; }
  1016. const double& real() const
  1017. { return __real__ _M_value; }
  1018. double& imag()
  1019. { return __imag__ _M_value; }
  1020. const double& imag() const
  1021. { return __imag__ _M_value; }
  1022. #endif
  1023. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1024. // DR 387. std::complex over-encapsulated.
  1025. void real(double __val)
  1026. { __real__ _M_value = __val; }
  1027. void imag(double __val)
  1028. { __imag__ _M_value = __val; }
  1029. complex<double>&
  1030. operator=(double __d)
  1031. {
  1032. __real__ _M_value = __d;
  1033. __imag__ _M_value = 0.0;
  1034. return *this;
  1035. }
  1036. complex<double>&
  1037. operator+=(double __d)
  1038. {
  1039. __real__ _M_value += __d;
  1040. return *this;
  1041. }
  1042. complex<double>&
  1043. operator-=(double __d)
  1044. {
  1045. __real__ _M_value -= __d;
  1046. return *this;
  1047. }
  1048. complex<double>&
  1049. operator*=(double __d)
  1050. {
  1051. _M_value *= __d;
  1052. return *this;
  1053. }
  1054. complex<double>&
  1055. operator/=(double __d)
  1056. {
  1057. _M_value /= __d;
  1058. return *this;
  1059. }
  1060. // The compiler will synthesize this, efficiently.
  1061. // complex& operator=(const complex&);
  1062. template<typename _Tp>
  1063. complex<double>&
  1064. operator=(const complex<_Tp>& __z)
  1065. {
  1066. __real__ _M_value = __z.real();
  1067. __imag__ _M_value = __z.imag();
  1068. return *this;
  1069. }
  1070. template<typename _Tp>
  1071. complex<double>&
  1072. operator+=(const complex<_Tp>& __z)
  1073. {
  1074. __real__ _M_value += __z.real();
  1075. __imag__ _M_value += __z.imag();
  1076. return *this;
  1077. }
  1078. template<typename _Tp>
  1079. complex<double>&
  1080. operator-=(const complex<_Tp>& __z)
  1081. {
  1082. __real__ _M_value -= __z.real();
  1083. __imag__ _M_value -= __z.imag();
  1084. return *this;
  1085. }
  1086. template<typename _Tp>
  1087. complex<double>&
  1088. operator*=(const complex<_Tp>& __z)
  1089. {
  1090. _ComplexT __t;
  1091. __real__ __t = __z.real();
  1092. __imag__ __t = __z.imag();
  1093. _M_value *= __t;
  1094. return *this;
  1095. }
  1096. template<typename _Tp>
  1097. complex<double>&
  1098. operator/=(const complex<_Tp>& __z)
  1099. {
  1100. _ComplexT __t;
  1101. __real__ __t = __z.real();
  1102. __imag__ __t = __z.imag();
  1103. _M_value /= __t;
  1104. return *this;
  1105. }
  1106. const _ComplexT& __rep() const { return _M_value; }
  1107. private:
  1108. _ComplexT _M_value;
  1109. };
  1110. // 26.2.3 complex specializations
  1111. // complex<long double> specialization
  1112. template<>
  1113. struct complex<long double>
  1114. {
  1115. typedef long double value_type;
  1116. typedef __complex__ long double _ComplexT;
  1117. complex(_ComplexT __z) : _M_value(__z) { }
  1118. complex(long double __r = 0.0L, long double __i = 0.0L)
  1119. {
  1120. __real__ _M_value = __r;
  1121. __imag__ _M_value = __i;
  1122. }
  1123. complex(const complex<float>& __z)
  1124. : _M_value(__z.__rep()) { }
  1125. complex(const complex<double>& __z)
  1126. : _M_value(__z.__rep()) { }
  1127. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  1128. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1129. // DR 387. std::complex over-encapsulated.
  1130. long double real() const
  1131. { return __real__ _M_value; }
  1132. long double imag() const
  1133. { return __imag__ _M_value; }
  1134. #else
  1135. long double& real()
  1136. { return __real__ _M_value; }
  1137. const long double& real() const
  1138. { return __real__ _M_value; }
  1139. long double& imag()
  1140. { return __imag__ _M_value; }
  1141. const long double& imag() const
  1142. { return __imag__ _M_value; }
  1143. #endif
  1144. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1145. // DR 387. std::complex over-encapsulated.
  1146. void real(long double __val)
  1147. { __real__ _M_value = __val; }
  1148. void imag(long double __val)
  1149. { __imag__ _M_value = __val; }
  1150. complex<long double>&
  1151. operator=(long double __r)
  1152. {
  1153. __real__ _M_value = __r;
  1154. __imag__ _M_value = 0.0L;
  1155. return *this;
  1156. }
  1157. complex<long double>&
  1158. operator+=(long double __r)
  1159. {
  1160. __real__ _M_value += __r;
  1161. return *this;
  1162. }
  1163. complex<long double>&
  1164. operator-=(long double __r)
  1165. {
  1166. __real__ _M_value -= __r;
  1167. return *this;
  1168. }
  1169. complex<long double>&
  1170. operator*=(long double __r)
  1171. {
  1172. _M_value *= __r;
  1173. return *this;
  1174. }
  1175. complex<long double>&
  1176. operator/=(long double __r)
  1177. {
  1178. _M_value /= __r;
  1179. return *this;
  1180. }
  1181. // The compiler knows how to do this efficiently
  1182. // complex& operator=(const complex&);
  1183. template<typename _Tp>
  1184. complex<long double>&
  1185. operator=(const complex<_Tp>& __z)
  1186. {
  1187. __real__ _M_value = __z.real();
  1188. __imag__ _M_value = __z.imag();
  1189. return *this;
  1190. }
  1191. template<typename _Tp>
  1192. complex<long double>&
  1193. operator+=(const complex<_Tp>& __z)
  1194. {
  1195. __real__ _M_value += __z.real();
  1196. __imag__ _M_value += __z.imag();
  1197. return *this;
  1198. }
  1199. template<typename _Tp>
  1200. complex<long double>&
  1201. operator-=(const complex<_Tp>& __z)
  1202. {
  1203. __real__ _M_value -= __z.real();
  1204. __imag__ _M_value -= __z.imag();
  1205. return *this;
  1206. }
  1207. template<typename _Tp>
  1208. complex<long double>&
  1209. operator*=(const complex<_Tp>& __z)
  1210. {
  1211. _ComplexT __t;
  1212. __real__ __t = __z.real();
  1213. __imag__ __t = __z.imag();
  1214. _M_value *= __t;
  1215. return *this;
  1216. }
  1217. template<typename _Tp>
  1218. complex<long double>&
  1219. operator/=(const complex<_Tp>& __z)
  1220. {
  1221. _ComplexT __t;
  1222. __real__ __t = __z.real();
  1223. __imag__ __t = __z.imag();
  1224. _M_value /= __t;
  1225. return *this;
  1226. }
  1227. const _ComplexT& __rep() const { return _M_value; }
  1228. private:
  1229. _ComplexT _M_value;
  1230. };
  1231. // These bits have to be at the end of this file, so that the
  1232. // specializations have all been defined.
  1233. inline
  1234. complex<float>::complex(const complex<double>& __z)
  1235. : _M_value(__z.__rep()) { }
  1236. inline
  1237. complex<float>::complex(const complex<long double>& __z)
  1238. : _M_value(__z.__rep()) { }
  1239. inline
  1240. complex<double>::complex(const complex<long double>& __z)
  1241. : _M_value(__z.__rep()) { }
  1242. // Inhibit implicit instantiations for required instantiations,
  1243. // which are defined via explicit instantiations elsewhere.
  1244. // NB: This syntax is a GNU extension.
  1245. #if _GLIBCXX_EXTERN_TEMPLATE
  1246. extern template istream& operator>>(istream&, complex<float>&);
  1247. extern template ostream& operator<<(ostream&, const complex<float>&);
  1248. extern template istream& operator>>(istream&, complex<double>&);
  1249. extern template ostream& operator<<(ostream&, const complex<double>&);
  1250. extern template istream& operator>>(istream&, complex<long double>&);
  1251. extern template ostream& operator<<(ostream&, const complex<long double>&);
  1252. #ifdef _GLIBCXX_USE_WCHAR_T
  1253. extern template wistream& operator>>(wistream&, complex<float>&);
  1254. extern template wostream& operator<<(wostream&, const complex<float>&);
  1255. extern template wistream& operator>>(wistream&, complex<double>&);
  1256. extern template wostream& operator<<(wostream&, const complex<double>&);
  1257. extern template wistream& operator>>(wistream&, complex<long double>&);
  1258. extern template wostream& operator<<(wostream&, const complex<long double>&);
  1259. #endif
  1260. #endif
  1261. // @} group complex_numbers
  1262. _GLIBCXX_END_NAMESPACE
  1263. _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx)
  1264. // See ext/type_traits.h for the primary template.
  1265. template<typename _Tp, typename _Up>
  1266. struct __promote_2<std::complex<_Tp>, _Up>
  1267. {
  1268. public:
  1269. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1270. };
  1271. template<typename _Tp, typename _Up>
  1272. struct __promote_2<_Tp, std::complex<_Up> >
  1273. {
  1274. public:
  1275. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1276. };
  1277. template<typename _Tp, typename _Up>
  1278. struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
  1279. {
  1280. public:
  1281. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1282. };
  1283. _GLIBCXX_END_NAMESPACE
  1284. #ifdef __GXX_EXPERIMENTAL_CXX0X__
  1285. # if defined(_GLIBCXX_INCLUDE_AS_TR1)
  1286. # error C++0x header cannot be included from TR1 header
  1287. # endif
  1288. # if defined(_GLIBCXX_INCLUDE_AS_CXX0X)
  1289. # include <tr1_impl/complex>
  1290. # else
  1291. # define _GLIBCXX_INCLUDE_AS_CXX0X
  1292. # define _GLIBCXX_BEGIN_NAMESPACE_TR1
  1293. # define _GLIBCXX_END_NAMESPACE_TR1
  1294. # define _GLIBCXX_TR1
  1295. # include <tr1_impl/complex>
  1296. # undef _GLIBCXX_TR1
  1297. # undef _GLIBCXX_END_NAMESPACE_TR1
  1298. # undef _GLIBCXX_BEGIN_NAMESPACE_TR1
  1299. # undef _GLIBCXX_INCLUDE_AS_CXX0X
  1300. # endif
  1301. _GLIBCXX_BEGIN_NAMESPACE(std)
  1302. // Forward declarations.
  1303. // DR 781.
  1304. template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
  1305. template<typename _Tp>
  1306. std::complex<_Tp>
  1307. __complex_proj(const std::complex<_Tp>& __z)
  1308. {
  1309. const _Tp __den = (__z.real() * __z.real()
  1310. + __z.imag() * __z.imag() + _Tp(1.0));
  1311. return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
  1312. (_Tp(2.0) * __z.imag()) / __den);
  1313. }
  1314. #if _GLIBCXX_USE_C99_COMPLEX
  1315. inline __complex__ float
  1316. __complex_proj(__complex__ float __z)
  1317. { return __builtin_cprojf(__z); }
  1318. inline __complex__ double
  1319. __complex_proj(__complex__ double __z)
  1320. { return __builtin_cproj(__z); }
  1321. inline __complex__ long double
  1322. __complex_proj(const __complex__ long double& __z)
  1323. { return __builtin_cprojl(__z); }
  1324. template<typename _Tp>
  1325. inline std::complex<_Tp>
  1326. proj(const std::complex<_Tp>& __z)
  1327. { return __complex_proj(__z.__rep()); }
  1328. #else
  1329. template<typename _Tp>
  1330. inline std::complex<_Tp>
  1331. proj(const std::complex<_Tp>& __z)
  1332. { return __complex_proj(__z); }
  1333. #endif
  1334. template<typename _Tp>
  1335. inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  1336. proj(_Tp __x)
  1337. {
  1338. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1339. return std::proj(std::complex<__type>(__x));
  1340. }
  1341. _GLIBCXX_END_NAMESPACE
  1342. #endif
  1343. #endif /* _GLIBCXX_COMPLEX */