complex 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495
  1. // -*- C++ -*-
  2. //===--------------------------- complex ----------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_COMPLEX
  10. #define _LIBCPP_COMPLEX
  11. /*
  12. complex synopsis
  13. namespace std
  14. {
  15. template<class T>
  16. class complex
  17. {
  18. public:
  19. typedef T value_type;
  20. complex(const T& re = T(), const T& im = T()); // constexpr in C++14
  21. complex(const complex&); // constexpr in C++14
  22. template<class X> complex(const complex<X>&); // constexpr in C++14
  23. T real() const; // constexpr in C++14
  24. T imag() const; // constexpr in C++14
  25. void real(T);
  26. void imag(T);
  27. complex<T>& operator= (const T&);
  28. complex<T>& operator+=(const T&);
  29. complex<T>& operator-=(const T&);
  30. complex<T>& operator*=(const T&);
  31. complex<T>& operator/=(const T&);
  32. complex& operator=(const complex&);
  33. template<class X> complex<T>& operator= (const complex<X>&);
  34. template<class X> complex<T>& operator+=(const complex<X>&);
  35. template<class X> complex<T>& operator-=(const complex<X>&);
  36. template<class X> complex<T>& operator*=(const complex<X>&);
  37. template<class X> complex<T>& operator/=(const complex<X>&);
  38. };
  39. template<>
  40. class complex<float>
  41. {
  42. public:
  43. typedef float value_type;
  44. constexpr complex(float re = 0.0f, float im = 0.0f);
  45. explicit constexpr complex(const complex<double>&);
  46. explicit constexpr complex(const complex<long double>&);
  47. constexpr float real() const;
  48. void real(float);
  49. constexpr float imag() const;
  50. void imag(float);
  51. complex<float>& operator= (float);
  52. complex<float>& operator+=(float);
  53. complex<float>& operator-=(float);
  54. complex<float>& operator*=(float);
  55. complex<float>& operator/=(float);
  56. complex<float>& operator=(const complex<float>&);
  57. template<class X> complex<float>& operator= (const complex<X>&);
  58. template<class X> complex<float>& operator+=(const complex<X>&);
  59. template<class X> complex<float>& operator-=(const complex<X>&);
  60. template<class X> complex<float>& operator*=(const complex<X>&);
  61. template<class X> complex<float>& operator/=(const complex<X>&);
  62. };
  63. template<>
  64. class complex<double>
  65. {
  66. public:
  67. typedef double value_type;
  68. constexpr complex(double re = 0.0, double im = 0.0);
  69. constexpr complex(const complex<float>&);
  70. explicit constexpr complex(const complex<long double>&);
  71. constexpr double real() const;
  72. void real(double);
  73. constexpr double imag() const;
  74. void imag(double);
  75. complex<double>& operator= (double);
  76. complex<double>& operator+=(double);
  77. complex<double>& operator-=(double);
  78. complex<double>& operator*=(double);
  79. complex<double>& operator/=(double);
  80. complex<double>& operator=(const complex<double>&);
  81. template<class X> complex<double>& operator= (const complex<X>&);
  82. template<class X> complex<double>& operator+=(const complex<X>&);
  83. template<class X> complex<double>& operator-=(const complex<X>&);
  84. template<class X> complex<double>& operator*=(const complex<X>&);
  85. template<class X> complex<double>& operator/=(const complex<X>&);
  86. };
  87. template<>
  88. class complex<long double>
  89. {
  90. public:
  91. typedef long double value_type;
  92. constexpr complex(long double re = 0.0L, long double im = 0.0L);
  93. constexpr complex(const complex<float>&);
  94. constexpr complex(const complex<double>&);
  95. constexpr long double real() const;
  96. void real(long double);
  97. constexpr long double imag() const;
  98. void imag(long double);
  99. complex<long double>& operator=(const complex<long double>&);
  100. complex<long double>& operator= (long double);
  101. complex<long double>& operator+=(long double);
  102. complex<long double>& operator-=(long double);
  103. complex<long double>& operator*=(long double);
  104. complex<long double>& operator/=(long double);
  105. template<class X> complex<long double>& operator= (const complex<X>&);
  106. template<class X> complex<long double>& operator+=(const complex<X>&);
  107. template<class X> complex<long double>& operator-=(const complex<X>&);
  108. template<class X> complex<long double>& operator*=(const complex<X>&);
  109. template<class X> complex<long double>& operator/=(const complex<X>&);
  110. };
  111. // 26.3.6 operators:
  112. template<class T> complex<T> operator+(const complex<T>&, const complex<T>&);
  113. template<class T> complex<T> operator+(const complex<T>&, const T&);
  114. template<class T> complex<T> operator+(const T&, const complex<T>&);
  115. template<class T> complex<T> operator-(const complex<T>&, const complex<T>&);
  116. template<class T> complex<T> operator-(const complex<T>&, const T&);
  117. template<class T> complex<T> operator-(const T&, const complex<T>&);
  118. template<class T> complex<T> operator*(const complex<T>&, const complex<T>&);
  119. template<class T> complex<T> operator*(const complex<T>&, const T&);
  120. template<class T> complex<T> operator*(const T&, const complex<T>&);
  121. template<class T> complex<T> operator/(const complex<T>&, const complex<T>&);
  122. template<class T> complex<T> operator/(const complex<T>&, const T&);
  123. template<class T> complex<T> operator/(const T&, const complex<T>&);
  124. template<class T> complex<T> operator+(const complex<T>&);
  125. template<class T> complex<T> operator-(const complex<T>&);
  126. template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
  127. template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
  128. template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
  129. template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
  130. template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
  131. template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
  132. template<class T, class charT, class traits>
  133. basic_istream<charT, traits>&
  134. operator>>(basic_istream<charT, traits>&, complex<T>&);
  135. template<class T, class charT, class traits>
  136. basic_ostream<charT, traits>&
  137. operator<<(basic_ostream<charT, traits>&, const complex<T>&);
  138. // 26.3.7 values:
  139. template<class T> T real(const complex<T>&); // constexpr in C++14
  140. long double real(long double); // constexpr in C++14
  141. double real(double); // constexpr in C++14
  142. template<Integral T> double real(T); // constexpr in C++14
  143. float real(float); // constexpr in C++14
  144. template<class T> T imag(const complex<T>&); // constexpr in C++14
  145. long double imag(long double); // constexpr in C++14
  146. double imag(double); // constexpr in C++14
  147. template<Integral T> double imag(T); // constexpr in C++14
  148. float imag(float); // constexpr in C++14
  149. template<class T> T abs(const complex<T>&);
  150. template<class T> T arg(const complex<T>&);
  151. long double arg(long double);
  152. double arg(double);
  153. template<Integral T> double arg(T);
  154. float arg(float);
  155. template<class T> T norm(const complex<T>&);
  156. long double norm(long double);
  157. double norm(double);
  158. template<Integral T> double norm(T);
  159. float norm(float);
  160. template<class T> complex<T> conj(const complex<T>&);
  161. complex<long double> conj(long double);
  162. complex<double> conj(double);
  163. template<Integral T> complex<double> conj(T);
  164. complex<float> conj(float);
  165. template<class T> complex<T> proj(const complex<T>&);
  166. complex<long double> proj(long double);
  167. complex<double> proj(double);
  168. template<Integral T> complex<double> proj(T);
  169. complex<float> proj(float);
  170. template<class T> complex<T> polar(const T&, const T& = T());
  171. // 26.3.8 transcendentals:
  172. template<class T> complex<T> acos(const complex<T>&);
  173. template<class T> complex<T> asin(const complex<T>&);
  174. template<class T> complex<T> atan(const complex<T>&);
  175. template<class T> complex<T> acosh(const complex<T>&);
  176. template<class T> complex<T> asinh(const complex<T>&);
  177. template<class T> complex<T> atanh(const complex<T>&);
  178. template<class T> complex<T> cos (const complex<T>&);
  179. template<class T> complex<T> cosh (const complex<T>&);
  180. template<class T> complex<T> exp (const complex<T>&);
  181. template<class T> complex<T> log (const complex<T>&);
  182. template<class T> complex<T> log10(const complex<T>&);
  183. template<class T> complex<T> pow(const complex<T>&, const T&);
  184. template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
  185. template<class T> complex<T> pow(const T&, const complex<T>&);
  186. template<class T> complex<T> sin (const complex<T>&);
  187. template<class T> complex<T> sinh (const complex<T>&);
  188. template<class T> complex<T> sqrt (const complex<T>&);
  189. template<class T> complex<T> tan (const complex<T>&);
  190. template<class T> complex<T> tanh (const complex<T>&);
  191. template<class T, class charT, class traits>
  192. basic_istream<charT, traits>&
  193. operator>>(basic_istream<charT, traits>& is, complex<T>& x);
  194. template<class T, class charT, class traits>
  195. basic_ostream<charT, traits>&
  196. operator<<(basic_ostream<charT, traits>& o, const complex<T>& x);
  197. } // std
  198. */
  199. #include <__config>
  200. #include <type_traits>
  201. #include <stdexcept>
  202. #include <cmath>
  203. #include <sstream>
  204. #include <version>
  205. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  206. #pragma GCC system_header
  207. #endif
  208. _LIBCPP_BEGIN_NAMESPACE_STD
  209. template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
  210. template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
  211. template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
  212. template<class _Tp>
  213. class _LIBCPP_TEMPLATE_VIS complex
  214. {
  215. public:
  216. typedef _Tp value_type;
  217. private:
  218. value_type __re_;
  219. value_type __im_;
  220. public:
  221. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  222. complex(const value_type& __re = value_type(), const value_type& __im = value_type())
  223. : __re_(__re), __im_(__im) {}
  224. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  225. complex(const complex<_Xp>& __c)
  226. : __re_(__c.real()), __im_(__c.imag()) {}
  227. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;}
  228. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;}
  229. _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
  230. _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
  231. _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re)
  232. {__re_ = __re; __im_ = value_type(); return *this;}
  233. _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
  234. _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
  235. _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
  236. _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
  237. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
  238. {
  239. __re_ = __c.real();
  240. __im_ = __c.imag();
  241. return *this;
  242. }
  243. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
  244. {
  245. __re_ += __c.real();
  246. __im_ += __c.imag();
  247. return *this;
  248. }
  249. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
  250. {
  251. __re_ -= __c.real();
  252. __im_ -= __c.imag();
  253. return *this;
  254. }
  255. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
  256. {
  257. *this = *this * complex(__c.real(), __c.imag());
  258. return *this;
  259. }
  260. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
  261. {
  262. *this = *this / complex(__c.real(), __c.imag());
  263. return *this;
  264. }
  265. };
  266. template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
  267. template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
  268. template<>
  269. class _LIBCPP_TEMPLATE_VIS complex<float>
  270. {
  271. float __re_;
  272. float __im_;
  273. public:
  274. typedef float value_type;
  275. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
  276. : __re_(__re), __im_(__im) {}
  277. _LIBCPP_INLINE_VISIBILITY
  278. explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
  279. _LIBCPP_INLINE_VISIBILITY
  280. explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
  281. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
  282. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
  283. _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
  284. _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
  285. _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re)
  286. {__re_ = __re; __im_ = value_type(); return *this;}
  287. _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;}
  288. _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;}
  289. _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
  290. _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
  291. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
  292. {
  293. __re_ = __c.real();
  294. __im_ = __c.imag();
  295. return *this;
  296. }
  297. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
  298. {
  299. __re_ += __c.real();
  300. __im_ += __c.imag();
  301. return *this;
  302. }
  303. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
  304. {
  305. __re_ -= __c.real();
  306. __im_ -= __c.imag();
  307. return *this;
  308. }
  309. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
  310. {
  311. *this = *this * complex(__c.real(), __c.imag());
  312. return *this;
  313. }
  314. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
  315. {
  316. *this = *this / complex(__c.real(), __c.imag());
  317. return *this;
  318. }
  319. };
  320. template<>
  321. class _LIBCPP_TEMPLATE_VIS complex<double>
  322. {
  323. double __re_;
  324. double __im_;
  325. public:
  326. typedef double value_type;
  327. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
  328. : __re_(__re), __im_(__im) {}
  329. _LIBCPP_INLINE_VISIBILITY
  330. _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
  331. _LIBCPP_INLINE_VISIBILITY
  332. explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
  333. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
  334. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
  335. _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
  336. _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
  337. _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re)
  338. {__re_ = __re; __im_ = value_type(); return *this;}
  339. _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;}
  340. _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;}
  341. _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
  342. _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
  343. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
  344. {
  345. __re_ = __c.real();
  346. __im_ = __c.imag();
  347. return *this;
  348. }
  349. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
  350. {
  351. __re_ += __c.real();
  352. __im_ += __c.imag();
  353. return *this;
  354. }
  355. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
  356. {
  357. __re_ -= __c.real();
  358. __im_ -= __c.imag();
  359. return *this;
  360. }
  361. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
  362. {
  363. *this = *this * complex(__c.real(), __c.imag());
  364. return *this;
  365. }
  366. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
  367. {
  368. *this = *this / complex(__c.real(), __c.imag());
  369. return *this;
  370. }
  371. };
  372. template<>
  373. class _LIBCPP_TEMPLATE_VIS complex<long double>
  374. {
  375. long double __re_;
  376. long double __im_;
  377. public:
  378. typedef long double value_type;
  379. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
  380. : __re_(__re), __im_(__im) {}
  381. _LIBCPP_INLINE_VISIBILITY
  382. _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
  383. _LIBCPP_INLINE_VISIBILITY
  384. _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
  385. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
  386. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
  387. _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;}
  388. _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;}
  389. _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re)
  390. {__re_ = __re; __im_ = value_type(); return *this;}
  391. _LIBCPP_INLINE_VISIBILITY complex& operator+=(long double __re) {__re_ += __re; return *this;}
  392. _LIBCPP_INLINE_VISIBILITY complex& operator-=(long double __re) {__re_ -= __re; return *this;}
  393. _LIBCPP_INLINE_VISIBILITY complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
  394. _LIBCPP_INLINE_VISIBILITY complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
  395. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c)
  396. {
  397. __re_ = __c.real();
  398. __im_ = __c.imag();
  399. return *this;
  400. }
  401. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c)
  402. {
  403. __re_ += __c.real();
  404. __im_ += __c.imag();
  405. return *this;
  406. }
  407. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c)
  408. {
  409. __re_ -= __c.real();
  410. __im_ -= __c.imag();
  411. return *this;
  412. }
  413. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c)
  414. {
  415. *this = *this * complex(__c.real(), __c.imag());
  416. return *this;
  417. }
  418. template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c)
  419. {
  420. *this = *this / complex(__c.real(), __c.imag());
  421. return *this;
  422. }
  423. };
  424. inline
  425. _LIBCPP_CONSTEXPR
  426. complex<float>::complex(const complex<double>& __c)
  427. : __re_(__c.real()), __im_(__c.imag()) {}
  428. inline
  429. _LIBCPP_CONSTEXPR
  430. complex<float>::complex(const complex<long double>& __c)
  431. : __re_(__c.real()), __im_(__c.imag()) {}
  432. inline
  433. _LIBCPP_CONSTEXPR
  434. complex<double>::complex(const complex<float>& __c)
  435. : __re_(__c.real()), __im_(__c.imag()) {}
  436. inline
  437. _LIBCPP_CONSTEXPR
  438. complex<double>::complex(const complex<long double>& __c)
  439. : __re_(__c.real()), __im_(__c.imag()) {}
  440. inline
  441. _LIBCPP_CONSTEXPR
  442. complex<long double>::complex(const complex<float>& __c)
  443. : __re_(__c.real()), __im_(__c.imag()) {}
  444. inline
  445. _LIBCPP_CONSTEXPR
  446. complex<long double>::complex(const complex<double>& __c)
  447. : __re_(__c.real()), __im_(__c.imag()) {}
  448. // 26.3.6 operators:
  449. template<class _Tp>
  450. inline _LIBCPP_INLINE_VISIBILITY
  451. complex<_Tp>
  452. operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
  453. {
  454. complex<_Tp> __t(__x);
  455. __t += __y;
  456. return __t;
  457. }
  458. template<class _Tp>
  459. inline _LIBCPP_INLINE_VISIBILITY
  460. complex<_Tp>
  461. operator+(const complex<_Tp>& __x, const _Tp& __y)
  462. {
  463. complex<_Tp> __t(__x);
  464. __t += __y;
  465. return __t;
  466. }
  467. template<class _Tp>
  468. inline _LIBCPP_INLINE_VISIBILITY
  469. complex<_Tp>
  470. operator+(const _Tp& __x, const complex<_Tp>& __y)
  471. {
  472. complex<_Tp> __t(__y);
  473. __t += __x;
  474. return __t;
  475. }
  476. template<class _Tp>
  477. inline _LIBCPP_INLINE_VISIBILITY
  478. complex<_Tp>
  479. operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
  480. {
  481. complex<_Tp> __t(__x);
  482. __t -= __y;
  483. return __t;
  484. }
  485. template<class _Tp>
  486. inline _LIBCPP_INLINE_VISIBILITY
  487. complex<_Tp>
  488. operator-(const complex<_Tp>& __x, const _Tp& __y)
  489. {
  490. complex<_Tp> __t(__x);
  491. __t -= __y;
  492. return __t;
  493. }
  494. template<class _Tp>
  495. inline _LIBCPP_INLINE_VISIBILITY
  496. complex<_Tp>
  497. operator-(const _Tp& __x, const complex<_Tp>& __y)
  498. {
  499. complex<_Tp> __t(-__y);
  500. __t += __x;
  501. return __t;
  502. }
  503. template<class _Tp>
  504. complex<_Tp>
  505. operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
  506. {
  507. _Tp __a = __z.real();
  508. _Tp __b = __z.imag();
  509. _Tp __c = __w.real();
  510. _Tp __d = __w.imag();
  511. _Tp __ac = __a * __c;
  512. _Tp __bd = __b * __d;
  513. _Tp __ad = __a * __d;
  514. _Tp __bc = __b * __c;
  515. _Tp __x = __ac - __bd;
  516. _Tp __y = __ad + __bc;
  517. if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
  518. {
  519. bool __recalc = false;
  520. if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b))
  521. {
  522. __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
  523. __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
  524. if (__libcpp_isnan_or_builtin(__c))
  525. __c = copysign(_Tp(0), __c);
  526. if (__libcpp_isnan_or_builtin(__d))
  527. __d = copysign(_Tp(0), __d);
  528. __recalc = true;
  529. }
  530. if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d))
  531. {
  532. __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
  533. __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
  534. if (__libcpp_isnan_or_builtin(__a))
  535. __a = copysign(_Tp(0), __a);
  536. if (__libcpp_isnan_or_builtin(__b))
  537. __b = copysign(_Tp(0), __b);
  538. __recalc = true;
  539. }
  540. if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) ||
  541. __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc)))
  542. {
  543. if (__libcpp_isnan_or_builtin(__a))
  544. __a = copysign(_Tp(0), __a);
  545. if (__libcpp_isnan_or_builtin(__b))
  546. __b = copysign(_Tp(0), __b);
  547. if (__libcpp_isnan_or_builtin(__c))
  548. __c = copysign(_Tp(0), __c);
  549. if (__libcpp_isnan_or_builtin(__d))
  550. __d = copysign(_Tp(0), __d);
  551. __recalc = true;
  552. }
  553. if (__recalc)
  554. {
  555. __x = _Tp(INFINITY) * (__a * __c - __b * __d);
  556. __y = _Tp(INFINITY) * (__a * __d + __b * __c);
  557. }
  558. }
  559. return complex<_Tp>(__x, __y);
  560. }
  561. template<class _Tp>
  562. inline _LIBCPP_INLINE_VISIBILITY
  563. complex<_Tp>
  564. operator*(const complex<_Tp>& __x, const _Tp& __y)
  565. {
  566. complex<_Tp> __t(__x);
  567. __t *= __y;
  568. return __t;
  569. }
  570. template<class _Tp>
  571. inline _LIBCPP_INLINE_VISIBILITY
  572. complex<_Tp>
  573. operator*(const _Tp& __x, const complex<_Tp>& __y)
  574. {
  575. complex<_Tp> __t(__y);
  576. __t *= __x;
  577. return __t;
  578. }
  579. template<class _Tp>
  580. complex<_Tp>
  581. operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
  582. {
  583. int __ilogbw = 0;
  584. _Tp __a = __z.real();
  585. _Tp __b = __z.imag();
  586. _Tp __c = __w.real();
  587. _Tp __d = __w.imag();
  588. _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
  589. if (__libcpp_isfinite_or_builtin(__logbw))
  590. {
  591. __ilogbw = static_cast<int>(__logbw);
  592. __c = scalbn(__c, -__ilogbw);
  593. __d = scalbn(__d, -__ilogbw);
  594. }
  595. _Tp __denom = __c * __c + __d * __d;
  596. _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
  597. _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
  598. if (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y))
  599. {
  600. if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b)))
  601. {
  602. __x = copysign(_Tp(INFINITY), __c) * __a;
  603. __y = copysign(_Tp(INFINITY), __c) * __b;
  604. }
  605. else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d))
  606. {
  607. __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a);
  608. __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b);
  609. __x = _Tp(INFINITY) * (__a * __c + __b * __d);
  610. __y = _Tp(INFINITY) * (__b * __c - __a * __d);
  611. }
  612. else if (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b))
  613. {
  614. __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c);
  615. __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d);
  616. __x = _Tp(0) * (__a * __c + __b * __d);
  617. __y = _Tp(0) * (__b * __c - __a * __d);
  618. }
  619. }
  620. return complex<_Tp>(__x, __y);
  621. }
  622. template<class _Tp>
  623. inline _LIBCPP_INLINE_VISIBILITY
  624. complex<_Tp>
  625. operator/(const complex<_Tp>& __x, const _Tp& __y)
  626. {
  627. return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
  628. }
  629. template<class _Tp>
  630. inline _LIBCPP_INLINE_VISIBILITY
  631. complex<_Tp>
  632. operator/(const _Tp& __x, const complex<_Tp>& __y)
  633. {
  634. complex<_Tp> __t(__x);
  635. __t /= __y;
  636. return __t;
  637. }
  638. template<class _Tp>
  639. inline _LIBCPP_INLINE_VISIBILITY
  640. complex<_Tp>
  641. operator+(const complex<_Tp>& __x)
  642. {
  643. return __x;
  644. }
  645. template<class _Tp>
  646. inline _LIBCPP_INLINE_VISIBILITY
  647. complex<_Tp>
  648. operator-(const complex<_Tp>& __x)
  649. {
  650. return complex<_Tp>(-__x.real(), -__x.imag());
  651. }
  652. template<class _Tp>
  653. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  654. bool
  655. operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
  656. {
  657. return __x.real() == __y.real() && __x.imag() == __y.imag();
  658. }
  659. template<class _Tp>
  660. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  661. bool
  662. operator==(const complex<_Tp>& __x, const _Tp& __y)
  663. {
  664. return __x.real() == __y && __x.imag() == 0;
  665. }
  666. template<class _Tp>
  667. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  668. bool
  669. operator==(const _Tp& __x, const complex<_Tp>& __y)
  670. {
  671. return __x == __y.real() && 0 == __y.imag();
  672. }
  673. template<class _Tp>
  674. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  675. bool
  676. operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
  677. {
  678. return !(__x == __y);
  679. }
  680. template<class _Tp>
  681. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  682. bool
  683. operator!=(const complex<_Tp>& __x, const _Tp& __y)
  684. {
  685. return !(__x == __y);
  686. }
  687. template<class _Tp>
  688. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  689. bool
  690. operator!=(const _Tp& __x, const complex<_Tp>& __y)
  691. {
  692. return !(__x == __y);
  693. }
  694. // 26.3.7 values:
  695. template <class _Tp, bool = is_integral<_Tp>::value,
  696. bool = is_floating_point<_Tp>::value
  697. >
  698. struct __libcpp_complex_overload_traits {};
  699. // Integral Types
  700. template <class _Tp>
  701. struct __libcpp_complex_overload_traits<_Tp, true, false>
  702. {
  703. typedef double _ValueType;
  704. typedef complex<double> _ComplexType;
  705. };
  706. // Floating point types
  707. template <class _Tp>
  708. struct __libcpp_complex_overload_traits<_Tp, false, true>
  709. {
  710. typedef _Tp _ValueType;
  711. typedef complex<_Tp> _ComplexType;
  712. };
  713. // real
  714. template<class _Tp>
  715. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  716. _Tp
  717. real(const complex<_Tp>& __c)
  718. {
  719. return __c.real();
  720. }
  721. template <class _Tp>
  722. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  723. typename __libcpp_complex_overload_traits<_Tp>::_ValueType
  724. real(_Tp __re)
  725. {
  726. return __re;
  727. }
  728. // imag
  729. template<class _Tp>
  730. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  731. _Tp
  732. imag(const complex<_Tp>& __c)
  733. {
  734. return __c.imag();
  735. }
  736. template <class _Tp>
  737. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
  738. typename __libcpp_complex_overload_traits<_Tp>::_ValueType
  739. imag(_Tp)
  740. {
  741. return 0;
  742. }
  743. // abs
  744. template<class _Tp>
  745. inline _LIBCPP_INLINE_VISIBILITY
  746. _Tp
  747. abs(const complex<_Tp>& __c)
  748. {
  749. return hypot(__c.real(), __c.imag());
  750. }
  751. // arg
  752. template<class _Tp>
  753. inline _LIBCPP_INLINE_VISIBILITY
  754. _Tp
  755. arg(const complex<_Tp>& __c)
  756. {
  757. return atan2(__c.imag(), __c.real());
  758. }
  759. template <class _Tp>
  760. inline _LIBCPP_INLINE_VISIBILITY
  761. typename enable_if<
  762. is_same<_Tp, long double>::value,
  763. long double
  764. >::type
  765. arg(_Tp __re)
  766. {
  767. return atan2l(0.L, __re);
  768. }
  769. template<class _Tp>
  770. inline _LIBCPP_INLINE_VISIBILITY
  771. typename enable_if
  772. <
  773. is_integral<_Tp>::value || is_same<_Tp, double>::value,
  774. double
  775. >::type
  776. arg(_Tp __re)
  777. {
  778. return atan2(0., __re);
  779. }
  780. template <class _Tp>
  781. inline _LIBCPP_INLINE_VISIBILITY
  782. typename enable_if<
  783. is_same<_Tp, float>::value,
  784. float
  785. >::type
  786. arg(_Tp __re)
  787. {
  788. return atan2f(0.F, __re);
  789. }
  790. // norm
  791. template<class _Tp>
  792. inline _LIBCPP_INLINE_VISIBILITY
  793. _Tp
  794. norm(const complex<_Tp>& __c)
  795. {
  796. if (__libcpp_isinf_or_builtin(__c.real()))
  797. return abs(__c.real());
  798. if (__libcpp_isinf_or_builtin(__c.imag()))
  799. return abs(__c.imag());
  800. return __c.real() * __c.real() + __c.imag() * __c.imag();
  801. }
  802. template <class _Tp>
  803. inline _LIBCPP_INLINE_VISIBILITY
  804. typename __libcpp_complex_overload_traits<_Tp>::_ValueType
  805. norm(_Tp __re)
  806. {
  807. typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
  808. return static_cast<_ValueType>(__re) * __re;
  809. }
  810. // conj
  811. template<class _Tp>
  812. inline _LIBCPP_INLINE_VISIBILITY
  813. complex<_Tp>
  814. conj(const complex<_Tp>& __c)
  815. {
  816. return complex<_Tp>(__c.real(), -__c.imag());
  817. }
  818. template <class _Tp>
  819. inline _LIBCPP_INLINE_VISIBILITY
  820. typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
  821. conj(_Tp __re)
  822. {
  823. typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
  824. return _ComplexType(__re);
  825. }
  826. // proj
  827. template<class _Tp>
  828. inline _LIBCPP_INLINE_VISIBILITY
  829. complex<_Tp>
  830. proj(const complex<_Tp>& __c)
  831. {
  832. std::complex<_Tp> __r = __c;
  833. if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag()))
  834. __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag()));
  835. return __r;
  836. }
  837. template <class _Tp>
  838. inline _LIBCPP_INLINE_VISIBILITY
  839. typename enable_if
  840. <
  841. is_floating_point<_Tp>::value,
  842. typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
  843. >::type
  844. proj(_Tp __re)
  845. {
  846. if (__libcpp_isinf_or_builtin(__re))
  847. __re = abs(__re);
  848. return complex<_Tp>(__re);
  849. }
  850. template <class _Tp>
  851. inline _LIBCPP_INLINE_VISIBILITY
  852. typename enable_if
  853. <
  854. is_integral<_Tp>::value,
  855. typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
  856. >::type
  857. proj(_Tp __re)
  858. {
  859. typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
  860. return _ComplexType(__re);
  861. }
  862. // polar
  863. template<class _Tp>
  864. complex<_Tp>
  865. polar(const _Tp& __rho, const _Tp& __theta = _Tp())
  866. {
  867. if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho))
  868. return complex<_Tp>(_Tp(NAN), _Tp(NAN));
  869. if (__libcpp_isnan_or_builtin(__theta))
  870. {
  871. if (__libcpp_isinf_or_builtin(__rho))
  872. return complex<_Tp>(__rho, __theta);
  873. return complex<_Tp>(__theta, __theta);
  874. }
  875. if (__libcpp_isinf_or_builtin(__theta))
  876. {
  877. if (__libcpp_isinf_or_builtin(__rho))
  878. return complex<_Tp>(__rho, _Tp(NAN));
  879. return complex<_Tp>(_Tp(NAN), _Tp(NAN));
  880. }
  881. _Tp __x = __rho * cos(__theta);
  882. if (__libcpp_isnan_or_builtin(__x))
  883. __x = 0;
  884. _Tp __y = __rho * sin(__theta);
  885. if (__libcpp_isnan_or_builtin(__y))
  886. __y = 0;
  887. return complex<_Tp>(__x, __y);
  888. }
  889. // log
  890. template<class _Tp>
  891. inline _LIBCPP_INLINE_VISIBILITY
  892. complex<_Tp>
  893. log(const complex<_Tp>& __x)
  894. {
  895. return complex<_Tp>(log(abs(__x)), arg(__x));
  896. }
  897. // log10
  898. template<class _Tp>
  899. inline _LIBCPP_INLINE_VISIBILITY
  900. complex<_Tp>
  901. log10(const complex<_Tp>& __x)
  902. {
  903. return log(__x) / log(_Tp(10));
  904. }
  905. // sqrt
  906. template<class _Tp>
  907. complex<_Tp>
  908. sqrt(const complex<_Tp>& __x)
  909. {
  910. if (__libcpp_isinf_or_builtin(__x.imag()))
  911. return complex<_Tp>(_Tp(INFINITY), __x.imag());
  912. if (__libcpp_isinf_or_builtin(__x.real()))
  913. {
  914. if (__x.real() > _Tp(0))
  915. return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag()));
  916. return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag()));
  917. }
  918. return polar(sqrt(abs(__x)), arg(__x) / _Tp(2));
  919. }
  920. // exp
  921. template<class _Tp>
  922. complex<_Tp>
  923. exp(const complex<_Tp>& __x)
  924. {
  925. _Tp __i = __x.imag();
  926. if (__libcpp_isinf_or_builtin(__x.real()))
  927. {
  928. if (__x.real() < _Tp(0))
  929. {
  930. if (!__libcpp_isfinite_or_builtin(__i))
  931. __i = _Tp(1);
  932. }
  933. else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i))
  934. {
  935. if (__libcpp_isinf_or_builtin(__i))
  936. __i = _Tp(NAN);
  937. return complex<_Tp>(__x.real(), __i);
  938. }
  939. }
  940. else if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
  941. return __x;
  942. _Tp __e = exp(__x.real());
  943. return complex<_Tp>(__e * cos(__i), __e * sin(__i));
  944. }
  945. // pow
  946. template<class _Tp>
  947. inline _LIBCPP_INLINE_VISIBILITY
  948. complex<_Tp>
  949. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  950. {
  951. return exp(__y * log(__x));
  952. }
  953. template<class _Tp, class _Up>
  954. inline _LIBCPP_INLINE_VISIBILITY
  955. complex<typename __promote<_Tp, _Up>::type>
  956. pow(const complex<_Tp>& __x, const complex<_Up>& __y)
  957. {
  958. typedef complex<typename __promote<_Tp, _Up>::type> result_type;
  959. return _VSTD::pow(result_type(__x), result_type(__y));
  960. }
  961. template<class _Tp, class _Up>
  962. inline _LIBCPP_INLINE_VISIBILITY
  963. typename enable_if
  964. <
  965. is_arithmetic<_Up>::value,
  966. complex<typename __promote<_Tp, _Up>::type>
  967. >::type
  968. pow(const complex<_Tp>& __x, const _Up& __y)
  969. {
  970. typedef complex<typename __promote<_Tp, _Up>::type> result_type;
  971. return _VSTD::pow(result_type(__x), result_type(__y));
  972. }
  973. template<class _Tp, class _Up>
  974. inline _LIBCPP_INLINE_VISIBILITY
  975. typename enable_if
  976. <
  977. is_arithmetic<_Tp>::value,
  978. complex<typename __promote<_Tp, _Up>::type>
  979. >::type
  980. pow(const _Tp& __x, const complex<_Up>& __y)
  981. {
  982. typedef complex<typename __promote<_Tp, _Up>::type> result_type;
  983. return _VSTD::pow(result_type(__x), result_type(__y));
  984. }
  985. // __sqr, computes pow(x, 2)
  986. template<class _Tp>
  987. inline _LIBCPP_INLINE_VISIBILITY
  988. complex<_Tp>
  989. __sqr(const complex<_Tp>& __x)
  990. {
  991. return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
  992. _Tp(2) * __x.real() * __x.imag());
  993. }
  994. // asinh
  995. template<class _Tp>
  996. complex<_Tp>
  997. asinh(const complex<_Tp>& __x)
  998. {
  999. const _Tp __pi(atan2(+0., -0.));
  1000. if (__libcpp_isinf_or_builtin(__x.real()))
  1001. {
  1002. if (__libcpp_isnan_or_builtin(__x.imag()))
  1003. return __x;
  1004. if (__libcpp_isinf_or_builtin(__x.imag()))
  1005. return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
  1006. return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
  1007. }
  1008. if (__libcpp_isnan_or_builtin(__x.real()))
  1009. {
  1010. if (__libcpp_isinf_or_builtin(__x.imag()))
  1011. return complex<_Tp>(__x.imag(), __x.real());
  1012. if (__x.imag() == 0)
  1013. return __x;
  1014. return complex<_Tp>(__x.real(), __x.real());
  1015. }
  1016. if (__libcpp_isinf_or_builtin(__x.imag()))
  1017. return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
  1018. complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1)));
  1019. return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
  1020. }
  1021. // acosh
  1022. template<class _Tp>
  1023. complex<_Tp>
  1024. acosh(const complex<_Tp>& __x)
  1025. {
  1026. const _Tp __pi(atan2(+0., -0.));
  1027. if (__libcpp_isinf_or_builtin(__x.real()))
  1028. {
  1029. if (__libcpp_isnan_or_builtin(__x.imag()))
  1030. return complex<_Tp>(abs(__x.real()), __x.imag());
  1031. if (__libcpp_isinf_or_builtin(__x.imag()))
  1032. {
  1033. if (__x.real() > 0)
  1034. return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag()));
  1035. else
  1036. return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag()));
  1037. }
  1038. if (__x.real() < 0)
  1039. return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag()));
  1040. return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag()));
  1041. }
  1042. if (__libcpp_isnan_or_builtin(__x.real()))
  1043. {
  1044. if (__libcpp_isinf_or_builtin(__x.imag()))
  1045. return complex<_Tp>(abs(__x.imag()), __x.real());
  1046. return complex<_Tp>(__x.real(), __x.real());
  1047. }
  1048. if (__libcpp_isinf_or_builtin(__x.imag()))
  1049. return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag()));
  1050. complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
  1051. return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag()));
  1052. }
  1053. // atanh
  1054. template<class _Tp>
  1055. complex<_Tp>
  1056. atanh(const complex<_Tp>& __x)
  1057. {
  1058. const _Tp __pi(atan2(+0., -0.));
  1059. if (__libcpp_isinf_or_builtin(__x.imag()))
  1060. {
  1061. return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
  1062. }
  1063. if (__libcpp_isnan_or_builtin(__x.imag()))
  1064. {
  1065. if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0)
  1066. return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag());
  1067. return complex<_Tp>(__x.imag(), __x.imag());
  1068. }
  1069. if (__libcpp_isnan_or_builtin(__x.real()))
  1070. {
  1071. return complex<_Tp>(__x.real(), __x.real());
  1072. }
  1073. if (__libcpp_isinf_or_builtin(__x.real()))
  1074. {
  1075. return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag()));
  1076. }
  1077. if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
  1078. {
  1079. return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag()));
  1080. }
  1081. complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
  1082. return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag()));
  1083. }
  1084. // sinh
  1085. template<class _Tp>
  1086. complex<_Tp>
  1087. sinh(const complex<_Tp>& __x)
  1088. {
  1089. if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
  1090. return complex<_Tp>(__x.real(), _Tp(NAN));
  1091. if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
  1092. return complex<_Tp>(__x.real(), _Tp(NAN));
  1093. if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
  1094. return __x;
  1095. return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag()));
  1096. }
  1097. // cosh
  1098. template<class _Tp>
  1099. complex<_Tp>
  1100. cosh(const complex<_Tp>& __x)
  1101. {
  1102. if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag()))
  1103. return complex<_Tp>(abs(__x.real()), _Tp(NAN));
  1104. if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag()))
  1105. return complex<_Tp>(_Tp(NAN), __x.real());
  1106. if (__x.real() == 0 && __x.imag() == 0)
  1107. return complex<_Tp>(_Tp(1), __x.imag());
  1108. if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real()))
  1109. return complex<_Tp>(abs(__x.real()), __x.imag());
  1110. return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag()));
  1111. }
  1112. // tanh
  1113. template<class _Tp>
  1114. complex<_Tp>
  1115. tanh(const complex<_Tp>& __x)
  1116. {
  1117. if (__libcpp_isinf_or_builtin(__x.real()))
  1118. {
  1119. if (!__libcpp_isfinite_or_builtin(__x.imag()))
  1120. return complex<_Tp>(_Tp(1), _Tp(0));
  1121. return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag())));
  1122. }
  1123. if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0)
  1124. return __x;
  1125. _Tp __2r(_Tp(2) * __x.real());
  1126. _Tp __2i(_Tp(2) * __x.imag());
  1127. _Tp __d(cosh(__2r) + cos(__2i));
  1128. _Tp __2rsh(sinh(__2r));
  1129. if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d))
  1130. return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
  1131. __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
  1132. return complex<_Tp>(__2rsh/__d, sin(__2i)/__d);
  1133. }
  1134. // asin
  1135. template<class _Tp>
  1136. complex<_Tp>
  1137. asin(const complex<_Tp>& __x)
  1138. {
  1139. complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real()));
  1140. return complex<_Tp>(__z.imag(), -__z.real());
  1141. }
  1142. // acos
  1143. template<class _Tp>
  1144. complex<_Tp>
  1145. acos(const complex<_Tp>& __x)
  1146. {
  1147. const _Tp __pi(atan2(+0., -0.));
  1148. if (__libcpp_isinf_or_builtin(__x.real()))
  1149. {
  1150. if (__libcpp_isnan_or_builtin(__x.imag()))
  1151. return complex<_Tp>(__x.imag(), __x.real());
  1152. if (__libcpp_isinf_or_builtin(__x.imag()))
  1153. {
  1154. if (__x.real() < _Tp(0))
  1155. return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
  1156. return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
  1157. }
  1158. if (__x.real() < _Tp(0))
  1159. return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real());
  1160. return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real());
  1161. }
  1162. if (__libcpp_isnan_or_builtin(__x.real()))
  1163. {
  1164. if (__libcpp_isinf_or_builtin(__x.imag()))
  1165. return complex<_Tp>(__x.real(), -__x.imag());
  1166. return complex<_Tp>(__x.real(), __x.real());
  1167. }
  1168. if (__libcpp_isinf_or_builtin(__x.imag()))
  1169. return complex<_Tp>(__pi/_Tp(2), -__x.imag());
  1170. if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag())))
  1171. return complex<_Tp>(__pi/_Tp(2), -__x.imag());
  1172. complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1)));
  1173. if (signbit(__x.imag()))
  1174. return complex<_Tp>(abs(__z.imag()), abs(__z.real()));
  1175. return complex<_Tp>(abs(__z.imag()), -abs(__z.real()));
  1176. }
  1177. // atan
  1178. template<class _Tp>
  1179. complex<_Tp>
  1180. atan(const complex<_Tp>& __x)
  1181. {
  1182. complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real()));
  1183. return complex<_Tp>(__z.imag(), -__z.real());
  1184. }
  1185. // sin
  1186. template<class _Tp>
  1187. complex<_Tp>
  1188. sin(const complex<_Tp>& __x)
  1189. {
  1190. complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real()));
  1191. return complex<_Tp>(__z.imag(), -__z.real());
  1192. }
  1193. // cos
  1194. template<class _Tp>
  1195. inline _LIBCPP_INLINE_VISIBILITY
  1196. complex<_Tp>
  1197. cos(const complex<_Tp>& __x)
  1198. {
  1199. return cosh(complex<_Tp>(-__x.imag(), __x.real()));
  1200. }
  1201. // tan
  1202. template<class _Tp>
  1203. complex<_Tp>
  1204. tan(const complex<_Tp>& __x)
  1205. {
  1206. complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real()));
  1207. return complex<_Tp>(__z.imag(), -__z.real());
  1208. }
  1209. template<class _Tp, class _CharT, class _Traits>
  1210. basic_istream<_CharT, _Traits>&
  1211. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
  1212. {
  1213. if (__is.good())
  1214. {
  1215. ws(__is);
  1216. if (__is.peek() == _CharT('('))
  1217. {
  1218. __is.get();
  1219. _Tp __r;
  1220. __is >> __r;
  1221. if (!__is.fail())
  1222. {
  1223. ws(__is);
  1224. _CharT __c = __is.peek();
  1225. if (__c == _CharT(','))
  1226. {
  1227. __is.get();
  1228. _Tp __i;
  1229. __is >> __i;
  1230. if (!__is.fail())
  1231. {
  1232. ws(__is);
  1233. __c = __is.peek();
  1234. if (__c == _CharT(')'))
  1235. {
  1236. __is.get();
  1237. __x = complex<_Tp>(__r, __i);
  1238. }
  1239. else
  1240. __is.setstate(ios_base::failbit);
  1241. }
  1242. else
  1243. __is.setstate(ios_base::failbit);
  1244. }
  1245. else if (__c == _CharT(')'))
  1246. {
  1247. __is.get();
  1248. __x = complex<_Tp>(__r, _Tp(0));
  1249. }
  1250. else
  1251. __is.setstate(ios_base::failbit);
  1252. }
  1253. else
  1254. __is.setstate(ios_base::failbit);
  1255. }
  1256. else
  1257. {
  1258. _Tp __r;
  1259. __is >> __r;
  1260. if (!__is.fail())
  1261. __x = complex<_Tp>(__r, _Tp(0));
  1262. else
  1263. __is.setstate(ios_base::failbit);
  1264. }
  1265. }
  1266. else
  1267. __is.setstate(ios_base::failbit);
  1268. return __is;
  1269. }
  1270. template<class _Tp, class _CharT, class _Traits>
  1271. basic_ostream<_CharT, _Traits>&
  1272. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
  1273. {
  1274. basic_ostringstream<_CharT, _Traits> __s;
  1275. __s.flags(__os.flags());
  1276. __s.imbue(__os.getloc());
  1277. __s.precision(__os.precision());
  1278. __s << '(' << __x.real() << ',' << __x.imag() << ')';
  1279. return __os << __s.str();
  1280. }
  1281. #if _LIBCPP_STD_VER > 11
  1282. // Literal suffix for complex number literals [complex.literals]
  1283. inline namespace literals
  1284. {
  1285. inline namespace complex_literals
  1286. {
  1287. constexpr complex<long double> operator""il(long double __im)
  1288. {
  1289. return { 0.0l, __im };
  1290. }
  1291. constexpr complex<long double> operator""il(unsigned long long __im)
  1292. {
  1293. return { 0.0l, static_cast<long double>(__im) };
  1294. }
  1295. constexpr complex<double> operator""i(long double __im)
  1296. {
  1297. return { 0.0, static_cast<double>(__im) };
  1298. }
  1299. constexpr complex<double> operator""i(unsigned long long __im)
  1300. {
  1301. return { 0.0, static_cast<double>(__im) };
  1302. }
  1303. constexpr complex<float> operator""if(long double __im)
  1304. {
  1305. return { 0.0f, static_cast<float>(__im) };
  1306. }
  1307. constexpr complex<float> operator""if(unsigned long long __im)
  1308. {
  1309. return { 0.0f, static_cast<float>(__im) };
  1310. }
  1311. }
  1312. }
  1313. #endif
  1314. _LIBCPP_END_NAMESPACE_STD
  1315. #endif // _LIBCPP_COMPLEX