complex 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. // The template and inlines for the -*- C++ -*- complex number classes.
  2. // Copyright (C) 1997-2021 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. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  1327. {
  1328. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1329. // See ext/type_traits.h for the primary template.
  1330. template<typename _Tp, typename _Up>
  1331. struct __promote_2<std::complex<_Tp>, _Up>
  1332. {
  1333. public:
  1334. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1335. };
  1336. template<typename _Tp, typename _Up>
  1337. struct __promote_2<_Tp, std::complex<_Up> >
  1338. {
  1339. public:
  1340. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1341. };
  1342. template<typename _Tp, typename _Up>
  1343. struct __promote_2<std::complex<_Tp>, std::complex<_Up> >
  1344. {
  1345. public:
  1346. typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type;
  1347. };
  1348. _GLIBCXX_END_NAMESPACE_VERSION
  1349. } // namespace
  1350. #if __cplusplus >= 201103L
  1351. namespace std _GLIBCXX_VISIBILITY(default)
  1352. {
  1353. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  1354. // Forward declarations.
  1355. template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&);
  1356. template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&);
  1357. template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&);
  1358. template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&);
  1359. template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&);
  1360. template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&);
  1361. // DR 595.
  1362. template<typename _Tp> _Tp fabs(const std::complex<_Tp>&);
  1363. template<typename _Tp>
  1364. inline std::complex<_Tp>
  1365. __complex_acos(const std::complex<_Tp>& __z)
  1366. {
  1367. const std::complex<_Tp> __t = std::asin(__z);
  1368. const _Tp __pi_2 = 1.5707963267948966192313216916397514L;
  1369. return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag());
  1370. }
  1371. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1372. inline __complex__ float
  1373. __complex_acos(__complex__ float __z)
  1374. { return __builtin_cacosf(__z); }
  1375. inline __complex__ double
  1376. __complex_acos(__complex__ double __z)
  1377. { return __builtin_cacos(__z); }
  1378. inline __complex__ long double
  1379. __complex_acos(const __complex__ long double& __z)
  1380. { return __builtin_cacosl(__z); }
  1381. template<typename _Tp>
  1382. inline std::complex<_Tp>
  1383. acos(const std::complex<_Tp>& __z)
  1384. { return __complex_acos(__z.__rep()); }
  1385. #else
  1386. /// acos(__z) [8.1.2].
  1387. // Effects: Behaves the same as C99 function cacos, defined
  1388. // in subclause 7.3.5.1.
  1389. template<typename _Tp>
  1390. inline std::complex<_Tp>
  1391. acos(const std::complex<_Tp>& __z)
  1392. { return __complex_acos(__z); }
  1393. #endif
  1394. template<typename _Tp>
  1395. inline std::complex<_Tp>
  1396. __complex_asin(const std::complex<_Tp>& __z)
  1397. {
  1398. std::complex<_Tp> __t(-__z.imag(), __z.real());
  1399. __t = std::asinh(__t);
  1400. return std::complex<_Tp>(__t.imag(), -__t.real());
  1401. }
  1402. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1403. inline __complex__ float
  1404. __complex_asin(__complex__ float __z)
  1405. { return __builtin_casinf(__z); }
  1406. inline __complex__ double
  1407. __complex_asin(__complex__ double __z)
  1408. { return __builtin_casin(__z); }
  1409. inline __complex__ long double
  1410. __complex_asin(const __complex__ long double& __z)
  1411. { return __builtin_casinl(__z); }
  1412. template<typename _Tp>
  1413. inline std::complex<_Tp>
  1414. asin(const std::complex<_Tp>& __z)
  1415. { return __complex_asin(__z.__rep()); }
  1416. #else
  1417. /// asin(__z) [8.1.3].
  1418. // Effects: Behaves the same as C99 function casin, defined
  1419. // in subclause 7.3.5.2.
  1420. template<typename _Tp>
  1421. inline std::complex<_Tp>
  1422. asin(const std::complex<_Tp>& __z)
  1423. { return __complex_asin(__z); }
  1424. #endif
  1425. template<typename _Tp>
  1426. std::complex<_Tp>
  1427. __complex_atan(const std::complex<_Tp>& __z)
  1428. {
  1429. const _Tp __r2 = __z.real() * __z.real();
  1430. const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag();
  1431. _Tp __num = __z.imag() + _Tp(1.0);
  1432. _Tp __den = __z.imag() - _Tp(1.0);
  1433. __num = __r2 + __num * __num;
  1434. __den = __r2 + __den * __den;
  1435. return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x),
  1436. _Tp(0.25) * log(__num / __den));
  1437. }
  1438. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1439. inline __complex__ float
  1440. __complex_atan(__complex__ float __z)
  1441. { return __builtin_catanf(__z); }
  1442. inline __complex__ double
  1443. __complex_atan(__complex__ double __z)
  1444. { return __builtin_catan(__z); }
  1445. inline __complex__ long double
  1446. __complex_atan(const __complex__ long double& __z)
  1447. { return __builtin_catanl(__z); }
  1448. template<typename _Tp>
  1449. inline std::complex<_Tp>
  1450. atan(const std::complex<_Tp>& __z)
  1451. { return __complex_atan(__z.__rep()); }
  1452. #else
  1453. /// atan(__z) [8.1.4].
  1454. // Effects: Behaves the same as C99 function catan, defined
  1455. // in subclause 7.3.5.3.
  1456. template<typename _Tp>
  1457. inline std::complex<_Tp>
  1458. atan(const std::complex<_Tp>& __z)
  1459. { return __complex_atan(__z); }
  1460. #endif
  1461. template<typename _Tp>
  1462. std::complex<_Tp>
  1463. __complex_acosh(const std::complex<_Tp>& __z)
  1464. {
  1465. // Kahan's formula.
  1466. return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0)))
  1467. + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0))));
  1468. }
  1469. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1470. inline __complex__ float
  1471. __complex_acosh(__complex__ float __z)
  1472. { return __builtin_cacoshf(__z); }
  1473. inline __complex__ double
  1474. __complex_acosh(__complex__ double __z)
  1475. { return __builtin_cacosh(__z); }
  1476. inline __complex__ long double
  1477. __complex_acosh(const __complex__ long double& __z)
  1478. { return __builtin_cacoshl(__z); }
  1479. template<typename _Tp>
  1480. inline std::complex<_Tp>
  1481. acosh(const std::complex<_Tp>& __z)
  1482. { return __complex_acosh(__z.__rep()); }
  1483. #else
  1484. /// acosh(__z) [8.1.5].
  1485. // Effects: Behaves the same as C99 function cacosh, defined
  1486. // in subclause 7.3.6.1.
  1487. template<typename _Tp>
  1488. inline std::complex<_Tp>
  1489. acosh(const std::complex<_Tp>& __z)
  1490. { return __complex_acosh(__z); }
  1491. #endif
  1492. template<typename _Tp>
  1493. std::complex<_Tp>
  1494. __complex_asinh(const std::complex<_Tp>& __z)
  1495. {
  1496. std::complex<_Tp> __t((__z.real() - __z.imag())
  1497. * (__z.real() + __z.imag()) + _Tp(1.0),
  1498. _Tp(2.0) * __z.real() * __z.imag());
  1499. __t = std::sqrt(__t);
  1500. return std::log(__t + __z);
  1501. }
  1502. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1503. inline __complex__ float
  1504. __complex_asinh(__complex__ float __z)
  1505. { return __builtin_casinhf(__z); }
  1506. inline __complex__ double
  1507. __complex_asinh(__complex__ double __z)
  1508. { return __builtin_casinh(__z); }
  1509. inline __complex__ long double
  1510. __complex_asinh(const __complex__ long double& __z)
  1511. { return __builtin_casinhl(__z); }
  1512. template<typename _Tp>
  1513. inline std::complex<_Tp>
  1514. asinh(const std::complex<_Tp>& __z)
  1515. { return __complex_asinh(__z.__rep()); }
  1516. #else
  1517. /// asinh(__z) [8.1.6].
  1518. // Effects: Behaves the same as C99 function casin, defined
  1519. // in subclause 7.3.6.2.
  1520. template<typename _Tp>
  1521. inline std::complex<_Tp>
  1522. asinh(const std::complex<_Tp>& __z)
  1523. { return __complex_asinh(__z); }
  1524. #endif
  1525. template<typename _Tp>
  1526. std::complex<_Tp>
  1527. __complex_atanh(const std::complex<_Tp>& __z)
  1528. {
  1529. const _Tp __i2 = __z.imag() * __z.imag();
  1530. const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real();
  1531. _Tp __num = _Tp(1.0) + __z.real();
  1532. _Tp __den = _Tp(1.0) - __z.real();
  1533. __num = __i2 + __num * __num;
  1534. __den = __i2 + __den * __den;
  1535. return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)),
  1536. _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x));
  1537. }
  1538. #if _GLIBCXX_USE_C99_COMPLEX_TR1
  1539. inline __complex__ float
  1540. __complex_atanh(__complex__ float __z)
  1541. { return __builtin_catanhf(__z); }
  1542. inline __complex__ double
  1543. __complex_atanh(__complex__ double __z)
  1544. { return __builtin_catanh(__z); }
  1545. inline __complex__ long double
  1546. __complex_atanh(const __complex__ long double& __z)
  1547. { return __builtin_catanhl(__z); }
  1548. template<typename _Tp>
  1549. inline std::complex<_Tp>
  1550. atanh(const std::complex<_Tp>& __z)
  1551. { return __complex_atanh(__z.__rep()); }
  1552. #else
  1553. /// atanh(__z) [8.1.7].
  1554. // Effects: Behaves the same as C99 function catanh, defined
  1555. // in subclause 7.3.6.3.
  1556. template<typename _Tp>
  1557. inline std::complex<_Tp>
  1558. atanh(const std::complex<_Tp>& __z)
  1559. { return __complex_atanh(__z); }
  1560. #endif
  1561. template<typename _Tp>
  1562. inline _Tp
  1563. /// fabs(__z) [8.1.8].
  1564. // Effects: Behaves the same as C99 function cabs, defined
  1565. // in subclause 7.3.8.1.
  1566. fabs(const std::complex<_Tp>& __z)
  1567. { return std::abs(__z); }
  1568. /// Additional overloads [8.1.9].
  1569. template<typename _Tp>
  1570. inline typename __gnu_cxx::__promote<_Tp>::__type
  1571. arg(_Tp __x)
  1572. {
  1573. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1574. #if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC)
  1575. return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L)
  1576. : __type();
  1577. #else
  1578. return std::arg(std::complex<__type>(__x));
  1579. #endif
  1580. }
  1581. template<typename _Tp>
  1582. _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  1583. imag(_Tp)
  1584. { return _Tp(); }
  1585. template<typename _Tp>
  1586. _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  1587. norm(_Tp __x)
  1588. {
  1589. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1590. return __type(__x) * __type(__x);
  1591. }
  1592. template<typename _Tp>
  1593. _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type
  1594. real(_Tp __x)
  1595. { return __x; }
  1596. template<typename _Tp, typename _Up>
  1597. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  1598. pow(const std::complex<_Tp>& __x, const _Up& __y)
  1599. {
  1600. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  1601. return std::pow(std::complex<__type>(__x), __type(__y));
  1602. }
  1603. template<typename _Tp, typename _Up>
  1604. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  1605. pow(const _Tp& __x, const std::complex<_Up>& __y)
  1606. {
  1607. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  1608. return std::pow(__type(__x), std::complex<__type>(__y));
  1609. }
  1610. template<typename _Tp, typename _Up>
  1611. inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type>
  1612. pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y)
  1613. {
  1614. typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type;
  1615. return std::pow(std::complex<__type>(__x),
  1616. std::complex<__type>(__y));
  1617. }
  1618. // Forward declarations.
  1619. // DR 781.
  1620. template<typename _Tp>
  1621. std::complex<_Tp> proj(const std::complex<_Tp>&);
  1622. // Generic implementation of std::proj, does not work for infinities.
  1623. template<typename _Tp>
  1624. inline std::complex<_Tp>
  1625. __complex_proj(const std::complex<_Tp>& __z)
  1626. { return __z; }
  1627. #if _GLIBCXX_USE_C99_COMPLEX
  1628. inline complex<float>
  1629. __complex_proj(const complex<float>& __z)
  1630. { return __builtin_cprojf(__z.__rep()); }
  1631. inline complex<double>
  1632. __complex_proj(const complex<double>& __z)
  1633. { return __builtin_cproj(__z.__rep()); }
  1634. inline complex<long double>
  1635. __complex_proj(const complex<long double>& __z)
  1636. { return __builtin_cprojl(__z.__rep()); }
  1637. #elif defined _GLIBCXX_USE_C99_MATH_TR1
  1638. inline complex<float>
  1639. __complex_proj(const complex<float>& __z)
  1640. {
  1641. if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
  1642. return complex<float>(__builtin_inff(),
  1643. __builtin_copysignf(0.0f, __z.imag()));
  1644. return __z;
  1645. }
  1646. inline complex<double>
  1647. __complex_proj(const complex<double>& __z)
  1648. {
  1649. if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
  1650. return complex<double>(__builtin_inf(),
  1651. __builtin_copysign(0.0, __z.imag()));
  1652. return __z;
  1653. }
  1654. inline complex<long double>
  1655. __complex_proj(const complex<long double>& __z)
  1656. {
  1657. if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag()))
  1658. return complex<long double>(__builtin_infl(),
  1659. __builtin_copysignl(0.0l, __z.imag()));
  1660. return __z;
  1661. }
  1662. #endif
  1663. template<typename _Tp>
  1664. inline std::complex<_Tp>
  1665. proj(const std::complex<_Tp>& __z)
  1666. { return __complex_proj(__z); }
  1667. // Overload for scalars
  1668. template<typename _Tp>
  1669. inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  1670. proj(_Tp __x)
  1671. {
  1672. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1673. return std::proj(std::complex<__type>(__x));
  1674. }
  1675. template<typename _Tp>
  1676. inline _GLIBCXX20_CONSTEXPR
  1677. std::complex<typename __gnu_cxx::__promote<_Tp>::__type>
  1678. conj(_Tp __x)
  1679. {
  1680. typedef typename __gnu_cxx::__promote<_Tp>::__type __type;
  1681. return std::complex<__type>(__x, -__type());
  1682. }
  1683. #if __cplusplus > 201103L
  1684. inline namespace literals {
  1685. inline namespace complex_literals {
  1686. #pragma GCC diagnostic push
  1687. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  1688. #define __cpp_lib_complex_udls 201309
  1689. constexpr std::complex<float>
  1690. operator""if(long double __num)
  1691. { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
  1692. constexpr std::complex<float>
  1693. operator""if(unsigned long long __num)
  1694. { return std::complex<float>{0.0F, static_cast<float>(__num)}; }
  1695. constexpr std::complex<double>
  1696. operator""i(long double __num)
  1697. { return std::complex<double>{0.0, static_cast<double>(__num)}; }
  1698. constexpr std::complex<double>
  1699. operator""i(unsigned long long __num)
  1700. { return std::complex<double>{0.0, static_cast<double>(__num)}; }
  1701. constexpr std::complex<long double>
  1702. operator""il(long double __num)
  1703. { return std::complex<long double>{0.0L, __num}; }
  1704. constexpr std::complex<long double>
  1705. operator""il(unsigned long long __num)
  1706. { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
  1707. #pragma GCC diagnostic pop
  1708. } // inline namespace complex_literals
  1709. } // inline namespace literals
  1710. #endif // C++14
  1711. _GLIBCXX_END_NAMESPACE_VERSION
  1712. } // namespace
  1713. #endif // C++11
  1714. #endif /* _GLIBCXX_COMPLEX */