complex 55 KB

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