complex 46 KB

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