complex 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. // The template and inlines for the -*- C++ -*- complex number classes.
  2. // Copyright (C) 1997-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/complex
  21. * This is a Standard C++ Library header.
  22. */
  23. //
  24. // ISO C++ 14882: 26.2 Complex Numbers
  25. // Note: this is not a conforming implementation.
  26. // Initially implemented by Ulrich Drepper <drepper@cygnus.com>
  27. // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr>
  28. //
  29. #ifndef _GLIBCXX_COMPLEX
  30. #define _GLIBCXX_COMPLEX 1
  31. #pragma GCC system_header
  32. #include <bits/c++config.h>
  33. #include <bits/cpp_type_traits.h>
  34. #include <ext/type_traits.h>
  35. #include <cmath>
  36. #include <sstream>
  37. // Get rid of a macro possibly defined in <complex.h>
  38. #undef complex
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. /**
  43. * @defgroup complex_numbers Complex Numbers
  44. * @ingroup numerics
  45. *
  46. * Classes and functions for complex numbers.
  47. * @{
  48. */
  49. // Forward declarations.
  50. template<typename _Tp> class complex;
  51. template<> class complex<float>;
  52. template<> class complex<double>;
  53. template<> class complex<long double>;
  54. /// Return magnitude of @a z.
  55. template<typename _Tp> _Tp abs(const complex<_Tp>&);
  56. /// Return phase angle of @a z.
  57. template<typename _Tp> _Tp arg(const complex<_Tp>&);
  58. /// Return @a z magnitude squared.
  59. template<typename _Tp> _Tp norm(const complex<_Tp>&);
  60. /// Return complex conjugate of @a z.
  61. template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&);
  62. /// Return complex with magnitude @a rho and angle @a theta.
  63. template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0);
  64. // Transcendentals:
  65. /// Return complex cosine of @a z.
  66. template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&);
  67. /// Return complex hyperbolic cosine of @a z.
  68. template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&);
  69. /// Return complex base e exponential of @a z.
  70. template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&);
  71. /// Return complex natural logarithm of @a z.
  72. template<typename _Tp> complex<_Tp> log(const complex<_Tp>&);
  73. /// Return complex base 10 logarithm of @a z.
  74. template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&);
  75. /// Return @a x to the @a y'th power.
  76. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int);
  77. /// Return @a x to the @a y'th power.
  78. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&);
  79. /// Return @a x to the @a y'th power.
  80. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&,
  81. const complex<_Tp>&);
  82. /// Return @a x to the @a y'th power.
  83. template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&);
  84. /// Return complex sine of @a z.
  85. template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&);
  86. /// Return complex hyperbolic sine of @a z.
  87. template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&);
  88. /// Return complex square root of @a z.
  89. template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&);
  90. /// Return complex tangent of @a z.
  91. template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&);
  92. /// Return complex hyperbolic tangent of @a z.
  93. template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&);
  94. // 26.2.2 Primary template class complex
  95. /**
  96. * Template to represent complex numbers.
  97. *
  98. * Specializations for float, double, and long double are part of the
  99. * library. Results with any other type are not guaranteed.
  100. *
  101. * @param Tp Type of real and imaginary values.
  102. */
  103. template<typename _Tp>
  104. struct complex
  105. {
  106. /// Value typedef.
  107. typedef _Tp value_type;
  108. /// Default constructor. First parameter is x, second parameter is y.
  109. /// Unspecified parameters default to 0.
  110. _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
  111. : _M_real(__r), _M_imag(__i) { }
  112. // Let the compiler synthesize the copy constructor
  113. #if __cplusplus >= 201103L
  114. constexpr complex(const complex&) = default;
  115. #endif
  116. /// Converting constructor.
  117. template<typename _Up>
  118. _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
  119. : _M_real(__z.real()), _M_imag(__z.imag()) { }
  120. #if __cplusplus >= 201103L
  121. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  122. // DR 387. std::complex over-encapsulated.
  123. _GLIBCXX_ABI_TAG_CXX11
  124. constexpr _Tp
  125. real() const { return _M_real; }
  126. _GLIBCXX_ABI_TAG_CXX11
  127. constexpr _Tp
  128. imag() const { return _M_imag; }
  129. #else
  130. /// Return real part of complex number.
  131. _Tp&
  132. real() { return _M_real; }
  133. /// Return real part of complex number.
  134. const _Tp&
  135. real() const { return _M_real; }
  136. /// Return imaginary part of complex number.
  137. _Tp&
  138. imag() { return _M_imag; }
  139. /// Return imaginary part of complex number.
  140. const _Tp&
  141. imag() const { return _M_imag; }
  142. #endif
  143. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  144. // DR 387. std::complex over-encapsulated.
  145. void
  146. real(_Tp __val) { _M_real = __val; }
  147. void
  148. imag(_Tp __val) { _M_imag = __val; }
  149. /// Assign a scalar to this complex number.
  150. complex<_Tp>& operator=(const _Tp&);
  151. /// Add a scalar to this complex number.
  152. // 26.2.5/1
  153. complex<_Tp>&
  154. operator+=(const _Tp& __t)
  155. {
  156. _M_real += __t;
  157. return *this;
  158. }
  159. /// Subtract a scalar from this complex number.
  160. // 26.2.5/3
  161. complex<_Tp>&
  162. operator-=(const _Tp& __t)
  163. {
  164. _M_real -= __t;
  165. return *this;
  166. }
  167. /// Multiply this complex number by a scalar.
  168. complex<_Tp>& operator*=(const _Tp&);
  169. /// Divide this complex number by a scalar.
  170. complex<_Tp>& operator/=(const _Tp&);
  171. // Let the compiler synthesize the copy assignment operator
  172. #if __cplusplus >= 201103L
  173. complex& operator=(const complex&) = default;
  174. #endif
  175. /// Assign another complex number to this one.
  176. template<typename _Up>
  177. complex<_Tp>& operator=(const complex<_Up>&);
  178. /// Add another complex number to this one.
  179. template<typename _Up>
  180. complex<_Tp>& operator+=(const complex<_Up>&);
  181. /// Subtract another complex number from this one.
  182. template<typename _Up>
  183. complex<_Tp>& operator-=(const complex<_Up>&);
  184. /// Multiply this complex number by another.
  185. template<typename _Up>
  186. complex<_Tp>& operator*=(const complex<_Up>&);
  187. /// Divide this complex number by another.
  188. template<typename _Up>
  189. complex<_Tp>& operator/=(const complex<_Up>&);
  190. _GLIBCXX_CONSTEXPR complex __rep() const
  191. { return *this; }
  192. private:
  193. _Tp _M_real;
  194. _Tp _M_imag;
  195. };
  196. template<typename _Tp>
  197. complex<_Tp>&
  198. complex<_Tp>::operator=(const _Tp& __t)
  199. {
  200. _M_real = __t;
  201. _M_imag = _Tp();
  202. return *this;
  203. }
  204. // 26.2.5/5
  205. template<typename _Tp>
  206. complex<_Tp>&
  207. complex<_Tp>::operator*=(const _Tp& __t)
  208. {
  209. _M_real *= __t;
  210. _M_imag *= __t;
  211. return *this;
  212. }
  213. // 26.2.5/7
  214. template<typename _Tp>
  215. complex<_Tp>&
  216. complex<_Tp>::operator/=(const _Tp& __t)
  217. {
  218. _M_real /= __t;
  219. _M_imag /= __t;
  220. return *this;
  221. }
  222. template<typename _Tp>
  223. template<typename _Up>
  224. complex<_Tp>&
  225. complex<_Tp>::operator=(const complex<_Up>& __z)
  226. {
  227. _M_real = __z.real();
  228. _M_imag = __z.imag();
  229. return *this;
  230. }
  231. // 26.2.5/9
  232. template<typename _Tp>
  233. template<typename _Up>
  234. complex<_Tp>&
  235. complex<_Tp>::operator+=(const complex<_Up>& __z)
  236. {
  237. _M_real += __z.real();
  238. _M_imag += __z.imag();
  239. return *this;
  240. }
  241. // 26.2.5/11
  242. template<typename _Tp>
  243. template<typename _Up>
  244. complex<_Tp>&
  245. complex<_Tp>::operator-=(const complex<_Up>& __z)
  246. {
  247. _M_real -= __z.real();
  248. _M_imag -= __z.imag();
  249. return *this;
  250. }
  251. // 26.2.5/13
  252. // XXX: This is a grammar school implementation.
  253. template<typename _Tp>
  254. template<typename _Up>
  255. complex<_Tp>&
  256. complex<_Tp>::operator*=(const complex<_Up>& __z)
  257. {
  258. const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag();
  259. _M_imag = _M_real * __z.imag() + _M_imag * __z.real();
  260. _M_real = __r;
  261. return *this;
  262. }
  263. // 26.2.5/15
  264. // XXX: This is a grammar school implementation.
  265. template<typename _Tp>
  266. template<typename _Up>
  267. complex<_Tp>&
  268. complex<_Tp>::operator/=(const complex<_Up>& __z)
  269. {
  270. const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag();
  271. const _Tp __n = std::norm(__z);
  272. _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n;
  273. _M_real = __r / __n;
  274. return *this;
  275. }
  276. // Operators:
  277. //@{
  278. /// Return new complex value @a x plus @a y.
  279. template<typename _Tp>
  280. inline complex<_Tp>
  281. operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
  282. {
  283. complex<_Tp> __r = __x;
  284. __r += __y;
  285. return __r;
  286. }
  287. template<typename _Tp>
  288. inline complex<_Tp>
  289. operator+(const complex<_Tp>& __x, const _Tp& __y)
  290. {
  291. complex<_Tp> __r = __x;
  292. __r += __y;
  293. return __r;
  294. }
  295. template<typename _Tp>
  296. inline complex<_Tp>
  297. operator+(const _Tp& __x, const complex<_Tp>& __y)
  298. {
  299. complex<_Tp> __r = __y;
  300. __r += __x;
  301. return __r;
  302. }
  303. //@}
  304. //@{
  305. /// Return new complex value @a x minus @a y.
  306. template<typename _Tp>
  307. inline complex<_Tp>
  308. operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
  309. {
  310. complex<_Tp> __r = __x;
  311. __r -= __y;
  312. return __r;
  313. }
  314. template<typename _Tp>
  315. inline complex<_Tp>
  316. operator-(const complex<_Tp>& __x, const _Tp& __y)
  317. {
  318. complex<_Tp> __r = __x;
  319. __r -= __y;
  320. return __r;
  321. }
  322. template<typename _Tp>
  323. inline complex<_Tp>
  324. operator-(const _Tp& __x, const complex<_Tp>& __y)
  325. {
  326. complex<_Tp> __r(__x, -__y.imag());
  327. __r -= __y.real();
  328. return __r;
  329. }
  330. //@}
  331. //@{
  332. /// Return new complex value @a x times @a y.
  333. template<typename _Tp>
  334. inline complex<_Tp>
  335. operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
  336. {
  337. complex<_Tp> __r = __x;
  338. __r *= __y;
  339. return __r;
  340. }
  341. template<typename _Tp>
  342. inline complex<_Tp>
  343. operator*(const complex<_Tp>& __x, const _Tp& __y)
  344. {
  345. complex<_Tp> __r = __x;
  346. __r *= __y;
  347. return __r;
  348. }
  349. template<typename _Tp>
  350. inline complex<_Tp>
  351. operator*(const _Tp& __x, const complex<_Tp>& __y)
  352. {
  353. complex<_Tp> __r = __y;
  354. __r *= __x;
  355. return __r;
  356. }
  357. //@}
  358. //@{
  359. /// Return new complex value @a x divided by @a y.
  360. template<typename _Tp>
  361. inline complex<_Tp>
  362. operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
  363. {
  364. complex<_Tp> __r = __x;
  365. __r /= __y;
  366. return __r;
  367. }
  368. template<typename _Tp>
  369. inline complex<_Tp>
  370. operator/(const complex<_Tp>& __x, const _Tp& __y)
  371. {
  372. complex<_Tp> __r = __x;
  373. __r /= __y;
  374. return __r;
  375. }
  376. template<typename _Tp>
  377. inline complex<_Tp>
  378. operator/(const _Tp& __x, const complex<_Tp>& __y)
  379. {
  380. complex<_Tp> __r = __x;
  381. __r /= __y;
  382. return __r;
  383. }
  384. //@}
  385. /// Return @a x.
  386. template<typename _Tp>
  387. inline complex<_Tp>
  388. operator+(const complex<_Tp>& __x)
  389. { return __x; }
  390. /// Return complex negation of @a x.
  391. template<typename _Tp>
  392. inline complex<_Tp>
  393. operator-(const complex<_Tp>& __x)
  394. { return complex<_Tp>(-__x.real(), -__x.imag()); }
  395. //@{
  396. /// Return true if @a x is equal to @a y.
  397. template<typename _Tp>
  398. inline _GLIBCXX_CONSTEXPR bool
  399. operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
  400. { return __x.real() == __y.real() && __x.imag() == __y.imag(); }
  401. template<typename _Tp>
  402. inline _GLIBCXX_CONSTEXPR bool
  403. operator==(const complex<_Tp>& __x, const _Tp& __y)
  404. { return __x.real() == __y && __x.imag() == _Tp(); }
  405. template<typename _Tp>
  406. inline _GLIBCXX_CONSTEXPR bool
  407. operator==(const _Tp& __x, const complex<_Tp>& __y)
  408. { return __x == __y.real() && _Tp() == __y.imag(); }
  409. //@}
  410. //@{
  411. /// Return false if @a x is equal to @a y.
  412. template<typename _Tp>
  413. inline _GLIBCXX_CONSTEXPR bool
  414. operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
  415. { return __x.real() != __y.real() || __x.imag() != __y.imag(); }
  416. template<typename _Tp>
  417. inline _GLIBCXX_CONSTEXPR bool
  418. operator!=(const complex<_Tp>& __x, const _Tp& __y)
  419. { return __x.real() != __y || __x.imag() != _Tp(); }
  420. template<typename _Tp>
  421. inline _GLIBCXX_CONSTEXPR bool
  422. operator!=(const _Tp& __x, const complex<_Tp>& __y)
  423. { return __x != __y.real() || _Tp() != __y.imag(); }
  424. //@}
  425. /// Extraction operator for complex values.
  426. template<typename _Tp, typename _CharT, class _Traits>
  427. basic_istream<_CharT, _Traits>&
  428. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
  429. {
  430. bool __fail = true;
  431. _CharT __ch;
  432. if (__is >> __ch)
  433. {
  434. if (_Traits::eq(__ch, __is.widen('(')))
  435. {
  436. _Tp __u;
  437. if (__is >> __u >> __ch)
  438. {
  439. const _CharT __rparen = __is.widen(')');
  440. if (_Traits::eq(__ch, __rparen))
  441. {
  442. __x = __u;
  443. __fail = false;
  444. }
  445. else if (_Traits::eq(__ch, __is.widen(',')))
  446. {
  447. _Tp __v;
  448. if (__is >> __v >> __ch)
  449. {
  450. if (_Traits::eq(__ch, __rparen))
  451. {
  452. __x = complex<_Tp>(__u, __v);
  453. __fail = false;
  454. }
  455. else
  456. __is.putback(__ch);
  457. }
  458. }
  459. else
  460. __is.putback(__ch);
  461. }
  462. }
  463. else
  464. {
  465. __is.putback(__ch);
  466. _Tp __u;
  467. if (__is >> __u)
  468. {
  469. __x = __u;
  470. __fail = false;
  471. }
  472. }
  473. }
  474. if (__fail)
  475. __is.setstate(ios_base::failbit);
  476. return __is;
  477. }
  478. /// Insertion operator for complex values.
  479. template<typename _Tp, typename _CharT, class _Traits>
  480. basic_ostream<_CharT, _Traits>&
  481. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
  482. {
  483. basic_ostringstream<_CharT, _Traits> __s;
  484. __s.flags(__os.flags());
  485. __s.imbue(__os.getloc());
  486. __s.precision(__os.precision());
  487. __s << '(' << __x.real() << ',' << __x.imag() << ')';
  488. return __os << __s.str();
  489. }
  490. // Values
  491. #if __cplusplus >= 201103L
  492. template<typename _Tp>
  493. constexpr _Tp
  494. real(const complex<_Tp>& __z)
  495. { return __z.real(); }
  496. template<typename _Tp>
  497. constexpr _Tp
  498. imag(const complex<_Tp>& __z)
  499. { return __z.imag(); }
  500. #else
  501. template<typename _Tp>
  502. inline _Tp&
  503. real(complex<_Tp>& __z)
  504. { return __z.real(); }
  505. template<typename _Tp>
  506. inline const _Tp&
  507. real(const complex<_Tp>& __z)
  508. { return __z.real(); }
  509. template<typename _Tp>
  510. inline _Tp&
  511. imag(complex<_Tp>& __z)
  512. { return __z.imag(); }
  513. template<typename _Tp>
  514. inline const _Tp&
  515. imag(const complex<_Tp>& __z)
  516. { return __z.imag(); }
  517. #endif
  518. // 26.2.7/3 abs(__z): Returns the magnitude of __z.
  519. template<typename _Tp>
  520. inline _Tp
  521. __complex_abs(const complex<_Tp>& __z)
  522. {
  523. _Tp __x = __z.real();
  524. _Tp __y = __z.imag();
  525. const _Tp __s = std::max(abs(__x), abs(__y));
  526. if (__s == _Tp()) // well ...
  527. return __s;
  528. __x /= __s;
  529. __y /= __s;
  530. return __s * sqrt(__x * __x + __y * __y);
  531. }
  532. #if _GLIBCXX_USE_C99_COMPLEX
  533. inline float
  534. __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); }
  535. inline double
  536. __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); }
  537. inline long double
  538. __complex_abs(const __complex__ long double& __z)
  539. { return __builtin_cabsl(__z); }
  540. template<typename _Tp>
  541. inline _Tp
  542. abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }
  543. #else
  544. template<typename _Tp>
  545. inline _Tp
  546. abs(const complex<_Tp>& __z) { return __complex_abs(__z); }
  547. #endif
  548. // 26.2.7/4: arg(__z): Returns the phase angle of __z.
  549. template<typename _Tp>
  550. inline _Tp
  551. __complex_arg(const complex<_Tp>& __z)
  552. { return atan2(__z.imag(), __z.real()); }
  553. #if _GLIBCXX_USE_C99_COMPLEX
  554. inline float
  555. __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); }
  556. inline double
  557. __complex_arg(__complex__ double __z) { return __builtin_carg(__z); }
  558. inline long double
  559. __complex_arg(const __complex__ long double& __z)
  560. { return __builtin_cargl(__z); }
  561. template<typename _Tp>
  562. inline _Tp
  563. arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }
  564. #else
  565. template<typename _Tp>
  566. inline _Tp
  567. arg(const complex<_Tp>& __z) { return __complex_arg(__z); }
  568. #endif
  569. // 26.2.7/5: norm(__z) returns the squared magnitude of __z.
  570. // As defined, norm() is -not- a norm is the common mathematical
  571. // sense used in numerics. The helper class _Norm_helper<> tries to
  572. // distinguish between builtin floating point and the rest, so as
  573. // to deliver an answer as close as possible to the real value.
  574. template<bool>
  575. struct _Norm_helper
  576. {
  577. template<typename _Tp>
  578. static inline _Tp _S_do_it(const complex<_Tp>& __z)
  579. {
  580. const _Tp __x = __z.real();
  581. const _Tp __y = __z.imag();
  582. return __x * __x + __y * __y;
  583. }
  584. };
  585. template<>
  586. struct _Norm_helper<true>
  587. {
  588. template<typename _Tp>
  589. static inline _Tp _S_do_it(const complex<_Tp>& __z)
  590. {
  591. _Tp __res = std::abs(__z);
  592. return __res * __res;
  593. }
  594. };
  595. template<typename _Tp>
  596. inline _Tp
  597. norm(const complex<_Tp>& __z)
  598. {
  599. return _Norm_helper<__is_floating<_Tp>::__value
  600. && !_GLIBCXX_FAST_MATH>::_S_do_it(__z);
  601. }
  602. template<typename _Tp>
  603. inline complex<_Tp>
  604. polar(const _Tp& __rho, const _Tp& __theta)
  605. {
  606. __glibcxx_assert( __rho >= 0 );
  607. return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta));
  608. }
  609. template<typename _Tp>
  610. inline complex<_Tp>
  611. conj(const complex<_Tp>& __z)
  612. { return complex<_Tp>(__z.real(), -__z.imag()); }
  613. // Transcendentals
  614. // 26.2.8/1 cos(__z): Returns the cosine of __z.
  615. template<typename _Tp>
  616. inline complex<_Tp>
  617. __complex_cos(const complex<_Tp>& __z)
  618. {
  619. const _Tp __x = __z.real();
  620. const _Tp __y = __z.imag();
  621. return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y));
  622. }
  623. #if _GLIBCXX_USE_C99_COMPLEX
  624. inline __complex__ float
  625. __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); }
  626. inline __complex__ double
  627. __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); }
  628. inline __complex__ long double
  629. __complex_cos(const __complex__ long double& __z)
  630. { return __builtin_ccosl(__z); }
  631. template<typename _Tp>
  632. inline complex<_Tp>
  633. cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }
  634. #else
  635. template<typename _Tp>
  636. inline complex<_Tp>
  637. cos(const complex<_Tp>& __z) { return __complex_cos(__z); }
  638. #endif
  639. // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z.
  640. template<typename _Tp>
  641. inline complex<_Tp>
  642. __complex_cosh(const complex<_Tp>& __z)
  643. {
  644. const _Tp __x = __z.real();
  645. const _Tp __y = __z.imag();
  646. return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y));
  647. }
  648. #if _GLIBCXX_USE_C99_COMPLEX
  649. inline __complex__ float
  650. __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); }
  651. inline __complex__ double
  652. __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); }
  653. inline __complex__ long double
  654. __complex_cosh(const __complex__ long double& __z)
  655. { return __builtin_ccoshl(__z); }
  656. template<typename _Tp>
  657. inline complex<_Tp>
  658. cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }
  659. #else
  660. template<typename _Tp>
  661. inline complex<_Tp>
  662. cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }
  663. #endif
  664. // 26.2.8/3 exp(__z): Returns the complex base e exponential of x
  665. template<typename _Tp>
  666. inline complex<_Tp>
  667. __complex_exp(const complex<_Tp>& __z)
  668. { return std::polar<_Tp>(exp(__z.real()), __z.imag()); }
  669. #if _GLIBCXX_USE_C99_COMPLEX
  670. inline __complex__ float
  671. __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); }
  672. inline __complex__ double
  673. __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); }
  674. inline __complex__ long double
  675. __complex_exp(const __complex__ long double& __z)
  676. { return __builtin_cexpl(__z); }
  677. template<typename _Tp>
  678. inline complex<_Tp>
  679. exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }
  680. #else
  681. template<typename _Tp>
  682. inline complex<_Tp>
  683. exp(const complex<_Tp>& __z) { return __complex_exp(__z); }
  684. #endif
  685. // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z.
  686. // The branch cut is along the negative axis.
  687. template<typename _Tp>
  688. inline complex<_Tp>
  689. __complex_log(const complex<_Tp>& __z)
  690. { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }
  691. #if _GLIBCXX_USE_C99_COMPLEX
  692. inline __complex__ float
  693. __complex_log(__complex__ float __z) { return __builtin_clogf(__z); }
  694. inline __complex__ double
  695. __complex_log(__complex__ double __z) { return __builtin_clog(__z); }
  696. inline __complex__ long double
  697. __complex_log(const __complex__ long double& __z)
  698. { return __builtin_clogl(__z); }
  699. template<typename _Tp>
  700. inline complex<_Tp>
  701. log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }
  702. #else
  703. template<typename _Tp>
  704. inline complex<_Tp>
  705. log(const complex<_Tp>& __z) { return __complex_log(__z); }
  706. #endif
  707. template<typename _Tp>
  708. inline complex<_Tp>
  709. log10(const complex<_Tp>& __z)
  710. { return std::log(__z) / log(_Tp(10.0)); }
  711. // 26.2.8/10 sin(__z): Returns the sine of __z.
  712. template<typename _Tp>
  713. inline complex<_Tp>
  714. __complex_sin(const complex<_Tp>& __z)
  715. {
  716. const _Tp __x = __z.real();
  717. const _Tp __y = __z.imag();
  718. return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y));
  719. }
  720. #if _GLIBCXX_USE_C99_COMPLEX
  721. inline __complex__ float
  722. __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); }
  723. inline __complex__ double
  724. __complex_sin(__complex__ double __z) { return __builtin_csin(__z); }
  725. inline __complex__ long double
  726. __complex_sin(const __complex__ long double& __z)
  727. { return __builtin_csinl(__z); }
  728. template<typename _Tp>
  729. inline complex<_Tp>
  730. sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }
  731. #else
  732. template<typename _Tp>
  733. inline complex<_Tp>
  734. sin(const complex<_Tp>& __z) { return __complex_sin(__z); }
  735. #endif
  736. // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z.
  737. template<typename _Tp>
  738. inline complex<_Tp>
  739. __complex_sinh(const complex<_Tp>& __z)
  740. {
  741. const _Tp __x = __z.real();
  742. const _Tp __y = __z.imag();
  743. return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y));
  744. }
  745. #if _GLIBCXX_USE_C99_COMPLEX
  746. inline __complex__ float
  747. __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); }
  748. inline __complex__ double
  749. __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); }
  750. inline __complex__ long double
  751. __complex_sinh(const __complex__ long double& __z)
  752. { return __builtin_csinhl(__z); }
  753. template<typename _Tp>
  754. inline complex<_Tp>
  755. sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }
  756. #else
  757. template<typename _Tp>
  758. inline complex<_Tp>
  759. sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }
  760. #endif
  761. // 26.2.8/13 sqrt(__z): Returns the complex square root of __z.
  762. // The branch cut is on the negative axis.
  763. template<typename _Tp>
  764. complex<_Tp>
  765. __complex_sqrt(const complex<_Tp>& __z)
  766. {
  767. _Tp __x = __z.real();
  768. _Tp __y = __z.imag();
  769. if (__x == _Tp())
  770. {
  771. _Tp __t = sqrt(abs(__y) / 2);
  772. return complex<_Tp>(__t, __y < _Tp() ? -__t : __t);
  773. }
  774. else
  775. {
  776. _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x)));
  777. _Tp __u = __t / 2;
  778. return __x > _Tp()
  779. ? complex<_Tp>(__u, __y / __t)
  780. : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u);
  781. }
  782. }
  783. #if _GLIBCXX_USE_C99_COMPLEX
  784. inline __complex__ float
  785. __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); }
  786. inline __complex__ double
  787. __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); }
  788. inline __complex__ long double
  789. __complex_sqrt(const __complex__ long double& __z)
  790. { return __builtin_csqrtl(__z); }
  791. template<typename _Tp>
  792. inline complex<_Tp>
  793. sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }
  794. #else
  795. template<typename _Tp>
  796. inline complex<_Tp>
  797. sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }
  798. #endif
  799. // 26.2.8/14 tan(__z): Return the complex tangent of __z.
  800. template<typename _Tp>
  801. inline complex<_Tp>
  802. __complex_tan(const complex<_Tp>& __z)
  803. { return std::sin(__z) / std::cos(__z); }
  804. #if _GLIBCXX_USE_C99_COMPLEX
  805. inline __complex__ float
  806. __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); }
  807. inline __complex__ double
  808. __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); }
  809. inline __complex__ long double
  810. __complex_tan(const __complex__ long double& __z)
  811. { return __builtin_ctanl(__z); }
  812. template<typename _Tp>
  813. inline complex<_Tp>
  814. tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }
  815. #else
  816. template<typename _Tp>
  817. inline complex<_Tp>
  818. tan(const complex<_Tp>& __z) { return __complex_tan(__z); }
  819. #endif
  820. // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z.
  821. template<typename _Tp>
  822. inline complex<_Tp>
  823. __complex_tanh(const complex<_Tp>& __z)
  824. { return std::sinh(__z) / std::cosh(__z); }
  825. #if _GLIBCXX_USE_C99_COMPLEX
  826. inline __complex__ float
  827. __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); }
  828. inline __complex__ double
  829. __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); }
  830. inline __complex__ long double
  831. __complex_tanh(const __complex__ long double& __z)
  832. { return __builtin_ctanhl(__z); }
  833. template<typename _Tp>
  834. inline complex<_Tp>
  835. tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }
  836. #else
  837. template<typename _Tp>
  838. inline complex<_Tp>
  839. tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }
  840. #endif
  841. // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x
  842. // raised to the __y-th power. The branch
  843. // cut is on the negative axis.
  844. template<typename _Tp>
  845. complex<_Tp>
  846. __complex_pow_unsigned(complex<_Tp> __x, unsigned __n)
  847. {
  848. complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1);
  849. while (__n >>= 1)
  850. {
  851. __x *= __x;
  852. if (__n % 2)
  853. __y *= __x;
  854. }
  855. return __y;
  856. }
  857. // In C++11 mode we used to implement the resolution of
  858. // DR 844. complex pow return type is ambiguous.
  859. // thus the following overload was disabled in that mode. However, doing
  860. // that causes all sorts of issues, see, for example:
  861. // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html
  862. // and also PR57974.
  863. template<typename _Tp>
  864. inline complex<_Tp>
  865. pow(const complex<_Tp>& __z, int __n)
  866. {
  867. return __n < 0
  868. ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n)
  869. : std::__complex_pow_unsigned(__z, __n);
  870. }
  871. template<typename _Tp>
  872. complex<_Tp>
  873. pow(const complex<_Tp>& __x, const _Tp& __y)
  874. {
  875. #if ! _GLIBCXX_USE_C99_COMPLEX
  876. if (__x == _Tp())
  877. return _Tp();
  878. #endif
  879. if (__x.imag() == _Tp() && __x.real() > _Tp())
  880. return pow(__x.real(), __y);
  881. complex<_Tp> __t = std::log(__x);
  882. return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag());
  883. }
  884. template<typename _Tp>
  885. inline complex<_Tp>
  886. __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  887. { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }
  888. #if _GLIBCXX_USE_C99_COMPLEX
  889. inline __complex__ float
  890. __complex_pow(__complex__ float __x, __complex__ float __y)
  891. { return __builtin_cpowf(__x, __y); }
  892. inline __complex__ double
  893. __complex_pow(__complex__ double __x, __complex__ double __y)
  894. { return __builtin_cpow(__x, __y); }
  895. inline __complex__ long double
  896. __complex_pow(const __complex__ long double& __x,
  897. const __complex__ long double& __y)
  898. { return __builtin_cpowl(__x, __y); }
  899. template<typename _Tp>
  900. inline complex<_Tp>
  901. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  902. { return __complex_pow(__x.__rep(), __y.__rep()); }
  903. #else
  904. template<typename _Tp>
  905. inline complex<_Tp>
  906. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  907. { return __complex_pow(__x, __y); }
  908. #endif
  909. template<typename _Tp>
  910. inline complex<_Tp>
  911. pow(const _Tp& __x, const complex<_Tp>& __y)
  912. {
  913. return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()),
  914. __y.imag() * log(__x))
  915. : std::pow(complex<_Tp>(__x), __y);
  916. }
  917. /// 26.2.3 complex specializations
  918. /// complex<float> specialization
  919. template<>
  920. struct complex<float>
  921. {
  922. typedef float value_type;
  923. typedef __complex__ float _ComplexT;
  924. _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
  925. _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f)
  926. #if __cplusplus >= 201103L
  927. : _M_value{ __r, __i } { }
  928. #else
  929. {
  930. __real__ _M_value = __r;
  931. __imag__ _M_value = __i;
  932. }
  933. #endif
  934. explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&);
  935. explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
  936. #if __cplusplus >= 201103L
  937. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  938. // DR 387. std::complex over-encapsulated.
  939. __attribute ((__abi_tag__ ("cxx11")))
  940. constexpr float
  941. real() const { return __real__ _M_value; }
  942. __attribute ((__abi_tag__ ("cxx11")))
  943. constexpr float
  944. imag() const { return __imag__ _M_value; }
  945. #else
  946. float&
  947. real() { return __real__ _M_value; }
  948. const float&
  949. real() const { return __real__ _M_value; }
  950. float&
  951. imag() { return __imag__ _M_value; }
  952. const float&
  953. imag() const { return __imag__ _M_value; }
  954. #endif
  955. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  956. // DR 387. std::complex over-encapsulated.
  957. void
  958. real(float __val) { __real__ _M_value = __val; }
  959. void
  960. imag(float __val) { __imag__ _M_value = __val; }
  961. complex&
  962. operator=(float __f)
  963. {
  964. _M_value = __f;
  965. return *this;
  966. }
  967. complex&
  968. operator+=(float __f)
  969. {
  970. _M_value += __f;
  971. return *this;
  972. }
  973. complex&
  974. operator-=(float __f)
  975. {
  976. _M_value -= __f;
  977. return *this;
  978. }
  979. complex&
  980. operator*=(float __f)
  981. {
  982. _M_value *= __f;
  983. return *this;
  984. }
  985. complex&
  986. operator/=(float __f)
  987. {
  988. _M_value /= __f;
  989. return *this;
  990. }
  991. // Let the compiler synthesize the copy and assignment
  992. // operator. It always does a pretty good job.
  993. // complex& operator=(const complex&);
  994. template<typename _Tp>
  995. complex&
  996. operator=(const complex<_Tp>& __z)
  997. {
  998. __real__ _M_value = __z.real();
  999. __imag__ _M_value = __z.imag();
  1000. return *this;
  1001. }
  1002. template<typename _Tp>
  1003. complex&
  1004. operator+=(const complex<_Tp>& __z)
  1005. {
  1006. __real__ _M_value += __z.real();
  1007. __imag__ _M_value += __z.imag();
  1008. return *this;
  1009. }
  1010. template<class _Tp>
  1011. complex&
  1012. operator-=(const complex<_Tp>& __z)
  1013. {
  1014. __real__ _M_value -= __z.real();
  1015. __imag__ _M_value -= __z.imag();
  1016. return *this;
  1017. }
  1018. template<class _Tp>
  1019. complex&
  1020. operator*=(const complex<_Tp>& __z)
  1021. {
  1022. _ComplexT __t;
  1023. __real__ __t = __z.real();
  1024. __imag__ __t = __z.imag();
  1025. _M_value *= __t;
  1026. return *this;
  1027. }
  1028. template<class _Tp>
  1029. complex&
  1030. operator/=(const complex<_Tp>& __z)
  1031. {
  1032. _ComplexT __t;
  1033. __real__ __t = __z.real();
  1034. __imag__ __t = __z.imag();
  1035. _M_value /= __t;
  1036. return *this;
  1037. }
  1038. _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
  1039. private:
  1040. _ComplexT _M_value;
  1041. };
  1042. /// 26.2.3 complex specializations
  1043. /// complex<double> specialization
  1044. template<>
  1045. struct complex<double>
  1046. {
  1047. typedef double value_type;
  1048. typedef __complex__ double _ComplexT;
  1049. _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
  1050. _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0)
  1051. #if __cplusplus >= 201103L
  1052. : _M_value{ __r, __i } { }
  1053. #else
  1054. {
  1055. __real__ _M_value = __r;
  1056. __imag__ _M_value = __i;
  1057. }
  1058. #endif
  1059. _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
  1060. : _M_value(__z.__rep()) { }
  1061. explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&);
  1062. #if __cplusplus >= 201103L
  1063. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1064. // DR 387. std::complex over-encapsulated.
  1065. __attribute ((__abi_tag__ ("cxx11")))
  1066. constexpr double
  1067. real() const { return __real__ _M_value; }
  1068. __attribute ((__abi_tag__ ("cxx11")))
  1069. constexpr double
  1070. imag() const { return __imag__ _M_value; }
  1071. #else
  1072. double&
  1073. real() { return __real__ _M_value; }
  1074. const double&
  1075. real() const { return __real__ _M_value; }
  1076. double&
  1077. imag() { return __imag__ _M_value; }
  1078. const double&
  1079. imag() const { return __imag__ _M_value; }
  1080. #endif
  1081. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1082. // DR 387. std::complex over-encapsulated.
  1083. void
  1084. real(double __val) { __real__ _M_value = __val; }
  1085. void
  1086. imag(double __val) { __imag__ _M_value = __val; }
  1087. complex&
  1088. operator=(double __d)
  1089. {
  1090. _M_value = __d;
  1091. return *this;
  1092. }
  1093. complex&
  1094. operator+=(double __d)
  1095. {
  1096. _M_value += __d;
  1097. return *this;
  1098. }
  1099. complex&
  1100. operator-=(double __d)
  1101. {
  1102. _M_value -= __d;
  1103. return *this;
  1104. }
  1105. complex&
  1106. operator*=(double __d)
  1107. {
  1108. _M_value *= __d;
  1109. return *this;
  1110. }
  1111. complex&
  1112. operator/=(double __d)
  1113. {
  1114. _M_value /= __d;
  1115. return *this;
  1116. }
  1117. // The compiler will synthesize this, efficiently.
  1118. // complex& operator=(const complex&);
  1119. template<typename _Tp>
  1120. complex&
  1121. operator=(const complex<_Tp>& __z)
  1122. {
  1123. __real__ _M_value = __z.real();
  1124. __imag__ _M_value = __z.imag();
  1125. return *this;
  1126. }
  1127. template<typename _Tp>
  1128. complex&
  1129. operator+=(const complex<_Tp>& __z)
  1130. {
  1131. __real__ _M_value += __z.real();
  1132. __imag__ _M_value += __z.imag();
  1133. return *this;
  1134. }
  1135. template<typename _Tp>
  1136. complex&
  1137. operator-=(const complex<_Tp>& __z)
  1138. {
  1139. __real__ _M_value -= __z.real();
  1140. __imag__ _M_value -= __z.imag();
  1141. return *this;
  1142. }
  1143. template<typename _Tp>
  1144. complex&
  1145. operator*=(const complex<_Tp>& __z)
  1146. {
  1147. _ComplexT __t;
  1148. __real__ __t = __z.real();
  1149. __imag__ __t = __z.imag();
  1150. _M_value *= __t;
  1151. return *this;
  1152. }
  1153. template<typename _Tp>
  1154. complex&
  1155. operator/=(const complex<_Tp>& __z)
  1156. {
  1157. _ComplexT __t;
  1158. __real__ __t = __z.real();
  1159. __imag__ __t = __z.imag();
  1160. _M_value /= __t;
  1161. return *this;
  1162. }
  1163. _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
  1164. private:
  1165. _ComplexT _M_value;
  1166. };
  1167. /// 26.2.3 complex specializations
  1168. /// complex<long double> specialization
  1169. template<>
  1170. struct complex<long double>
  1171. {
  1172. typedef long double value_type;
  1173. typedef __complex__ long double _ComplexT;
  1174. _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { }
  1175. _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L,
  1176. long double __i = 0.0L)
  1177. #if __cplusplus >= 201103L
  1178. : _M_value{ __r, __i } { }
  1179. #else
  1180. {
  1181. __real__ _M_value = __r;
  1182. __imag__ _M_value = __i;
  1183. }
  1184. #endif
  1185. _GLIBCXX_CONSTEXPR complex(const complex<float>& __z)
  1186. : _M_value(__z.__rep()) { }
  1187. _GLIBCXX_CONSTEXPR complex(const complex<double>& __z)
  1188. : _M_value(__z.__rep()) { }
  1189. #if __cplusplus >= 201103L
  1190. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1191. // DR 387. std::complex over-encapsulated.
  1192. __attribute ((__abi_tag__ ("cxx11")))
  1193. constexpr long double
  1194. real() const { return __real__ _M_value; }
  1195. __attribute ((__abi_tag__ ("cxx11")))
  1196. constexpr long double
  1197. imag() const { return __imag__ _M_value; }
  1198. #else
  1199. long double&
  1200. real() { return __real__ _M_value; }
  1201. const long double&
  1202. real() const { return __real__ _M_value; }
  1203. long double&
  1204. imag() { return __imag__ _M_value; }
  1205. const long double&
  1206. imag() const { return __imag__ _M_value; }
  1207. #endif
  1208. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  1209. // DR 387. std::complex over-encapsulated.
  1210. void
  1211. real(long double __val) { __real__ _M_value = __val; }
  1212. void
  1213. imag(long double __val) { __imag__ _M_value = __val; }
  1214. complex&
  1215. operator=(long double __r)
  1216. {
  1217. _M_value = __r;
  1218. return *this;
  1219. }
  1220. complex&
  1221. operator+=(long double __r)
  1222. {
  1223. _M_value += __r;
  1224. return *this;
  1225. }
  1226. complex&
  1227. operator-=(long double __r)
  1228. {
  1229. _M_value -= __r;
  1230. return *this;
  1231. }
  1232. complex&
  1233. operator*=(long double __r)
  1234. {
  1235. _M_value *= __r;
  1236. return *this;
  1237. }
  1238. complex&
  1239. operator/=(long double __r)
  1240. {
  1241. _M_value /= __r;
  1242. return *this;
  1243. }
  1244. // The compiler knows how to do this efficiently
  1245. // complex& operator=(const complex&);
  1246. template<typename _Tp>
  1247. complex&
  1248. operator=(const complex<_Tp>& __z)
  1249. {
  1250. __real__ _M_value = __z.real();
  1251. __imag__ _M_value = __z.imag();
  1252. return *this;
  1253. }
  1254. template<typename _Tp>
  1255. complex&
  1256. operator+=(const complex<_Tp>& __z)
  1257. {
  1258. __real__ _M_value += __z.real();
  1259. __imag__ _M_value += __z.imag();
  1260. return *this;
  1261. }
  1262. template<typename _Tp>
  1263. complex&
  1264. operator-=(const complex<_Tp>& __z)
  1265. {
  1266. __real__ _M_value -= __z.real();
  1267. __imag__ _M_value -= __z.imag();
  1268. return *this;
  1269. }
  1270. template<typename _Tp>
  1271. complex&
  1272. operator*=(const complex<_Tp>& __z)
  1273. {
  1274. _ComplexT __t;
  1275. __real__ __t = __z.real();
  1276. __imag__ __t = __z.imag();
  1277. _M_value *= __t;
  1278. return *this;
  1279. }
  1280. template<typename _Tp>
  1281. complex&
  1282. operator/=(const complex<_Tp>& __z)
  1283. {
  1284. _ComplexT __t;
  1285. __real__ __t = __z.real();
  1286. __imag__ __t = __z.imag();
  1287. _M_value /= __t;
  1288. return *this;
  1289. }
  1290. _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; }
  1291. private:
  1292. _ComplexT _M_value;
  1293. };
  1294. // These bits have to be at the end of this file, so that the
  1295. // specializations have all been defined.
  1296. inline _GLIBCXX_CONSTEXPR
  1297. complex<float>::complex(const complex<double>& __z)
  1298. : _M_value(__z.__rep()) { }
  1299. inline _GLIBCXX_CONSTEXPR
  1300. complex<float>::complex(const complex<long double>& __z)
  1301. : _M_value(__z.__rep()) { }
  1302. inline _GLIBCXX_CONSTEXPR
  1303. complex<double>::complex(const complex<long double>& __z)
  1304. : _M_value(__z.__rep()) { }
  1305. // Inhibit implicit instantiations for required instantiations,
  1306. // which are defined via explicit instantiations elsewhere.
  1307. // NB: This syntax is a GNU extension.
  1308. #if _GLIBCXX_EXTERN_TEMPLATE
  1309. extern template istream& operator>>(istream&, complex<float>&);
  1310. extern template ostream& operator<<(ostream&, const complex<float>&);
  1311. extern template istream& operator>>(istream&, complex<double>&);
  1312. extern template ostream& operator<<(ostream&, const complex<double>&);
  1313. extern template istream& operator>>(istream&, complex<long double>&);
  1314. extern template ostream& operator<<(ostream&, const complex<long double>&);
  1315. #ifdef _GLIBCXX_USE_WCHAR_T
  1316. extern template wistream& operator>>(wistream&, complex<float>&);
  1317. extern template wostream& operator<<(wostream&, const complex<float>&);
  1318. extern template wistream& operator>>(wistream&, complex<double>&);
  1319. extern template wostream& operator<<(wostream&, const complex<double>&);
  1320. extern template wistream& operator>>(wistream&, complex<long double>&);
  1321. extern template wostream& operator<<(wostream&, const complex<long double>&);
  1322. #endif
  1323. #endif
  1324. // @} group complex_numbers
  1325. _GLIBCXX_END_NAMESPACE_VERSION
  1326. } // namespace
  1327. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  1328. {
  1329. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1330. // See ext/type_traits.h for the primary template.
  1331. template<typename _Tp, typename _Up>
  1332. struct __promote_2<std::complex<_Tp>, _Up>
  1333. {
  1334. public:
  1335. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1336. };
  1337. template<typename _Tp, typename _Up>
  1338. struct __promote_2<_Tp, std::complex<_Up> >
  1339. {
  1340. public:
  1341. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1342. };
  1343. template<typename _Tp, typename _Up>
  1344. struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
  1345. {
  1346. public:
  1347. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1348. };
  1349. _GLIBCXX_END_NAMESPACE_VERSION
  1350. } // namespace
  1351. #if __cplusplus >= 201103L
  1352. namespace std _GLIBCXX_VISIBILITY(default)
  1353. {
  1354. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1355. // Forward declarations.
  1356. template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
  1357. template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
  1358. template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
  1359. template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
  1360. template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
  1361. template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
  1362. // DR 595.
  1363. template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
  1364. template<typename _Tp>
  1365. inline std::complex<_Tp>
  1366. __complex_acos(const std::complex<_Tp>& __z)
  1367. {
  1368. const std::complex<_Tp> __t = std::asin(__z);
  1369. const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
  1370. return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
  1371. }
  1372. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1373. inline __complex__ float
  1374. __complex_acos(__complex__ float __z)
  1375. { return __builtin_cacosf(__z); }
  1376. inline __complex__ double
  1377. __complex_acos(__complex__ double __z)
  1378. { return __builtin_cacos(__z); }
  1379. inline __complex__ long double
  1380. __complex_acos(const __complex__ long double& __z)
  1381. { return __builtin_cacosl(__z); }
  1382. template<typename _Tp>
  1383. inline std::complex<_Tp>
  1384. acos(const std::complex<_Tp>& __z)
  1385. { return __complex_acos(__z.__rep()); }
  1386. #else
  1387. /// acos(__z) [8.1.2].
  1388. // Effects: Behaves the same as C99 function cacos, defined
  1389. // in subclause 7.3.5.1.
  1390. template<typename _Tp>
  1391. inline std::complex<_Tp>
  1392. acos(const std::complex<_Tp>& __z)
  1393. { return __complex_acos(__z); }
  1394. #endif
  1395. template<typename _Tp>
  1396. inline std::complex<_Tp>
  1397. __complex_asin(const std::complex<_Tp>& __z)
  1398. {
  1399. std::complex<_Tp> __t(-__z.imag(), __z.real());
  1400. __t = std::asinh(__t);
  1401. return std::complex<_Tp>(__t.imag(), -__t.real());
  1402. }
  1403. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1404. inline __complex__ float
  1405. __complex_asin(__complex__ float __z)
  1406. { return __builtin_casinf(__z); }
  1407. inline __complex__ double
  1408. __complex_asin(__complex__ double __z)
  1409. { return __builtin_casin(__z); }
  1410. inline __complex__ long double
  1411. __complex_asin(const __complex__ long double& __z)
  1412. { return __builtin_casinl(__z); }
  1413. template<typename _Tp>
  1414. inline std::complex<_Tp>
  1415. asin(const std::complex<_Tp>& __z)
  1416. { return __complex_asin(__z.__rep()); }
  1417. #else
  1418. /// asin(__z) [8.1.3].
  1419. // Effects: Behaves the same as C99 function casin, defined
  1420. // in subclause 7.3.5.2.
  1421. template<typename _Tp>
  1422. inline std::complex<_Tp>
  1423. asin(const std::complex<_Tp>& __z)
  1424. { return __complex_asin(__z); }
  1425. #endif
  1426. template<typename _Tp>
  1427. std::complex<_Tp>
  1428. __complex_atan(const std::complex<_Tp>& __z)
  1429. {
  1430. const _Tp __r2 = __z.real() * __z.real();
  1431. const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
  1432. _Tp __num = __z.imag() + _Tp(1.0);
  1433. _Tp __den = __z.imag() - _Tp(1.0);
  1434. __num = __r2 + __num * __num;
  1435. __den = __r2 + __den * __den;
  1436. return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
  1437. _Tp(0.25) * log(__num / __den));
  1438. }
  1439. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1440. inline __complex__ float
  1441. __complex_atan(__complex__ float __z)
  1442. { return __builtin_catanf(__z); }
  1443. inline __complex__ double
  1444. __complex_atan(__complex__ double __z)
  1445. { return __builtin_catan(__z); }
  1446. inline __complex__ long double
  1447. __complex_atan(const __complex__ long double& __z)
  1448. { return __builtin_catanl(__z); }
  1449. template<typename _Tp>
  1450. inline std::complex<_Tp>
  1451. atan(const std::complex<_Tp>& __z)
  1452. { return __complex_atan(__z.__rep()); }
  1453. #else
  1454. /// atan(__z) [8.1.4].
  1455. // Effects: Behaves the same as C99 function catan, defined
  1456. // in subclause 7.3.5.3.
  1457. template<typename _Tp>
  1458. inline std::complex<_Tp>
  1459. atan(const std::complex<_Tp>& __z)
  1460. { return __complex_atan(__z); }
  1461. #endif
  1462. template<typename _Tp>
  1463. std::complex<_Tp>
  1464. __complex_acosh(const std::complex<_Tp>& __z)
  1465. {
  1466. // Kahan's formula.
  1467. return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
  1468. + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
  1469. }
  1470. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1471. inline __complex__ float
  1472. __complex_acosh(__complex__ float __z)
  1473. { return __builtin_cacoshf(__z); }
  1474. inline __complex__ double
  1475. __complex_acosh(__complex__ double __z)
  1476. { return __builtin_cacosh(__z); }
  1477. inline __complex__ long double
  1478. __complex_acosh(const __complex__ long double& __z)
  1479. { return __builtin_cacoshl(__z); }
  1480. template<typename _Tp>
  1481. inline std::complex<_Tp>
  1482. acosh(const std::complex<_Tp>& __z)
  1483. { return __complex_acosh(__z.__rep()); }
  1484. #else
  1485. /// acosh(__z) [8.1.5].
  1486. // Effects: Behaves the same as C99 function cacosh, defined
  1487. // in subclause 7.3.6.1.
  1488. template<typename _Tp>
  1489. inline std::complex<_Tp>
  1490. acosh(const std::complex<_Tp>& __z)
  1491. { return __complex_acosh(__z); }
  1492. #endif
  1493. template<typename _Tp>
  1494. std::complex<_Tp>
  1495. __complex_asinh(const std::complex<_Tp>& __z)
  1496. {
  1497. std::complex<_Tp> __t((__z.real() - __z.imag())
  1498. * (__z.real() + __z.imag()) + _Tp(1.0),
  1499. _Tp(2.0) * __z.real() * __z.imag());
  1500. __t = std::sqrt(__t);
  1501. return std::log(__t + __z);
  1502. }
  1503. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1504. inline __complex__ float
  1505. __complex_asinh(__complex__ float __z)
  1506. { return __builtin_casinhf(__z); }
  1507. inline __complex__ double
  1508. __complex_asinh(__complex__ double __z)
  1509. { return __builtin_casinh(__z); }
  1510. inline __complex__ long double
  1511. __complex_asinh(const __complex__ long double& __z)
  1512. { return __builtin_casinhl(__z); }
  1513. template<typename _Tp>
  1514. inline std::complex<_Tp>
  1515. asinh(const std::complex<_Tp>& __z)
  1516. { return __complex_asinh(__z.__rep()); }
  1517. #else
  1518. /// asinh(__z) [8.1.6].
  1519. // Effects: Behaves the same as C99 function casin, defined
  1520. // in subclause 7.3.6.2.
  1521. template<typename _Tp>
  1522. inline std::complex<_Tp>
  1523. asinh(const std::complex<_Tp>& __z)
  1524. { return __complex_asinh(__z); }
  1525. #endif
  1526. template<typename _Tp>
  1527. std::complex<_Tp>
  1528. __complex_atanh(const std::complex<_Tp>& __z)
  1529. {
  1530. const _Tp __i2 = __z.imag() * __z.imag();
  1531. const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
  1532. _Tp __num = _Tp(1.0) + __z.real();
  1533. _Tp __den = _Tp(1.0) - __z.real();
  1534. __num = __i2 + __num * __num;
  1535. __den = __i2 + __den * __den;
  1536. return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
  1537. _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
  1538. }
  1539. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1540. inline __complex__ float
  1541. __complex_atanh(__complex__ float __z)
  1542. { return __builtin_catanhf(__z); }
  1543. inline __complex__ double
  1544. __complex_atanh(__complex__ double __z)
  1545. { return __builtin_catanh(__z); }
  1546. inline __complex__ long double
  1547. __complex_atanh(const __complex__ long double& __z)
  1548. { return __builtin_catanhl(__z); }
  1549. template<typename _Tp>
  1550. inline std::complex<_Tp>
  1551. atanh(const std::complex<_Tp>& __z)
  1552. { return __complex_atanh(__z.__rep()); }
  1553. #else
  1554. /// atanh(__z) [8.1.7].
  1555. // Effects: Behaves the same as C99 function catanh, defined
  1556. // in subclause 7.3.6.3.
  1557. template<typename _Tp>
  1558. inline std::complex<_Tp>
  1559. atanh(const std::complex<_Tp>& __z)
  1560. { return __complex_atanh(__z); }
  1561. #endif
  1562. template<typename _Tp>
  1563. inline _Tp
  1564. /// fabs(__z) [8.1.8].
  1565. // Effects: Behaves the same as C99 function cabs, defined
  1566. // in subclause 7.3.8.1.
  1567. fabs(const std::complex<_Tp>& __z)
  1568. { return std::abs(__z); }
  1569. /// Additional overloads [8.1.9].
  1570. template<typename _Tp>
  1571. inline typename __gnu_cxx::__promote<_Tp>::__type
  1572. arg(_Tp __x)
  1573. {
  1574. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1575. #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
  1576. return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
  1577. : __type();
  1578. #else
  1579. return std::arg(std::complex<__type>(__x));
  1580. #endif
  1581. }
  1582. template<typename _Tp>
  1583. _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  1584. imag(_Tp)
  1585. { return _Tp(); }
  1586. template<typename _Tp>
  1587. inline typename __gnu_cxx::__promote<_Tp>::__type
  1588. norm(_Tp __x)
  1589. {
  1590. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1591. return __type(__x) * __type(__x);
  1592. }
  1593. template<typename _Tp>
  1594. _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  1595. real(_Tp __x)
  1596. { return __x; }
  1597. template<typename _Tp, typename _Up>
  1598. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  1599. pow(const std::complex<_Tp>& __x, const _Up& __y)
  1600. {
  1601. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  1602. return std::pow(std::complex<__type>(__x), __type(__y));
  1603. }
  1604. template<typename _Tp, typename _Up>
  1605. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  1606. pow(const _Tp& __x, const std::complex<_Up>& __y)
  1607. {
  1608. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  1609. return std::pow(__type(__x), std::complex<__type>(__y));
  1610. }
  1611. template<typename _Tp, typename _Up>
  1612. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  1613. pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
  1614. {
  1615. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  1616. return std::pow(std::complex<__type>(__x),
  1617. std::complex<__type>(__y));
  1618. }
  1619. // Forward declarations.
  1620. // DR 781.
  1621. template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&);
  1622. template<typename _Tp>
  1623. std::complex<_Tp>
  1624. __complex_proj(const std::complex<_Tp>& __z)
  1625. {
  1626. const _Tp __den = (__z.real() * __z.real()
  1627. + __z.imag() * __z.imag() + _Tp(1.0));
  1628. return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den,
  1629. (_Tp(2.0) * __z.imag()) / __den);
  1630. }
  1631. #if _GLIBCXX_USE_C99_COMPLEX
  1632. inline __complex__ float
  1633. __complex_proj(__complex__ float __z)
  1634. { return __builtin_cprojf(__z); }
  1635. inline __complex__ double
  1636. __complex_proj(__complex__ double __z)
  1637. { return __builtin_cproj(__z); }
  1638. inline __complex__ long double
  1639. __complex_proj(const __complex__ long double& __z)
  1640. { return __builtin_cprojl(__z); }
  1641. template<typename _Tp>
  1642. inline std::complex<_Tp>
  1643. proj(const std::complex<_Tp>& __z)
  1644. { return __complex_proj(__z.__rep()); }
  1645. #else
  1646. template<typename _Tp>
  1647. inline std::complex<_Tp>
  1648. proj(const std::complex<_Tp>& __z)
  1649. { return __complex_proj(__z); }
  1650. #endif
  1651. template<typename _Tp>
  1652. inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  1653. proj(_Tp __x)
  1654. {
  1655. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1656. return std::proj(std::complex<__type>(__x));
  1657. }
  1658. template<typename _Tp>
  1659. inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  1660. conj(_Tp __x)
  1661. {
  1662. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1663. return std::complex<__type>(__x, -__type());
  1664. }
  1665. #if __cplusplus > 201103L
  1666. inline namespace literals {
  1667. inline namespace complex_literals {
  1668. #pragma GCC diagnostic push
  1669. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  1670. #define __cpp_lib_complex_udls 201309
  1671. constexpr std::complex<float>
  1672. operator""if(long double __num)
  1673. { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
  1674. constexpr std::complex<float>
  1675. operator""if(unsigned long long __num)
  1676. { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
  1677. constexpr std::complex<double>
  1678. operator""i(long double __num)
  1679. { return std::complex<double>{0.0, static_cast<double>(__num)}; }
  1680. constexpr std::complex<double>
  1681. operator""i(unsigned long long __num)
  1682. { return std::complex<double>{0.0, static_cast<double>(__num)}; }
  1683. constexpr std::complex<long double>
  1684. operator""il(long double __num)
  1685. { return std::complex<long double>{0.0L, __num}; }
  1686. constexpr std::complex<long double>
  1687. operator""il(unsigned long long __num)
  1688. { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
  1689. #pragma GCC diagnostic pop
  1690. } // inline namespace complex_literals
  1691. } // inline namespace literals
  1692. #endif // C++14
  1693. _GLIBCXX_END_NAMESPACE_VERSION
  1694. } // namespace
  1695. #endif // C++11
  1696. #endif /* _GLIBCXX_COMPLEX */