complex 44 KB

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