complex 46 KB

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