complex 55 KB

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