1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570 |
- // -*- C++ -*-
- //===----------------------------------------------------------------------===//
- //
- // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
- // See https://llvm.org/LICENSE.txt for license information.
- // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
- //
- //===----------------------------------------------------------------------===//
- #ifndef _LIBCPP_COMPLEX
- #define _LIBCPP_COMPLEX
- /*
- complex synopsis
- namespace std
- {
- template<class T>
- class complex
- {
- public:
- typedef T value_type;
- complex(const T& re = T(), const T& im = T()); // constexpr in C++14
- complex(const complex&); // constexpr in C++14
- template<class X> complex(const complex<X>&); // constexpr in C++14
- T real() const; // constexpr in C++14
- T imag() const; // constexpr in C++14
- void real(T); // constexpr in C++20
- void imag(T); // constexpr in C++20
- complex<T>& operator= (const T&); // constexpr in C++20
- complex<T>& operator+=(const T&); // constexpr in C++20
- complex<T>& operator-=(const T&); // constexpr in C++20
- complex<T>& operator*=(const T&); // constexpr in C++20
- complex<T>& operator/=(const T&); // constexpr in C++20
- complex& operator=(const complex&); // constexpr in C++20
- template<class X> complex<T>& operator= (const complex<X>&); // constexpr in C++20
- template<class X> complex<T>& operator+=(const complex<X>&); // constexpr in C++20
- template<class X> complex<T>& operator-=(const complex<X>&); // constexpr in C++20
- template<class X> complex<T>& operator*=(const complex<X>&); // constexpr in C++20
- template<class X> complex<T>& operator/=(const complex<X>&); // constexpr in C++20
- };
- template<>
- class complex<float>
- {
- public:
- typedef float value_type;
- constexpr complex(float re = 0.0f, float im = 0.0f);
- explicit constexpr complex(const complex<double>&);
- explicit constexpr complex(const complex<long double>&);
- constexpr float real() const;
- void real(float); // constexpr in C++20
- constexpr float imag() const;
- void imag(float); // constexpr in C++20
- complex<float>& operator= (float); // constexpr in C++20
- complex<float>& operator+=(float); // constexpr in C++20
- complex<float>& operator-=(float); // constexpr in C++20
- complex<float>& operator*=(float); // constexpr in C++20
- complex<float>& operator/=(float); // constexpr in C++20
- complex<float>& operator=(const complex<float>&); // constexpr in C++20
- template<class X> complex<float>& operator= (const complex<X>&); // constexpr in C++20
- template<class X> complex<float>& operator+=(const complex<X>&); // constexpr in C++20
- template<class X> complex<float>& operator-=(const complex<X>&); // constexpr in C++20
- template<class X> complex<float>& operator*=(const complex<X>&); // constexpr in C++20
- template<class X> complex<float>& operator/=(const complex<X>&); // constexpr in C++20
- };
- template<>
- class complex<double>
- {
- public:
- typedef double value_type;
- constexpr complex(double re = 0.0, double im = 0.0);
- constexpr complex(const complex<float>&);
- explicit constexpr complex(const complex<long double>&);
- constexpr double real() const;
- void real(double); // constexpr in C++20
- constexpr double imag() const;
- void imag(double); // constexpr in C++20
- complex<double>& operator= (double); // constexpr in C++20
- complex<double>& operator+=(double); // constexpr in C++20
- complex<double>& operator-=(double); // constexpr in C++20
- complex<double>& operator*=(double); // constexpr in C++20
- complex<double>& operator/=(double); // constexpr in C++20
- complex<double>& operator=(const complex<double>&); // constexpr in C++20
- template<class X> complex<double>& operator= (const complex<X>&); // constexpr in C++20
- template<class X> complex<double>& operator+=(const complex<X>&); // constexpr in C++20
- template<class X> complex<double>& operator-=(const complex<X>&); // constexpr in C++20
- template<class X> complex<double>& operator*=(const complex<X>&); // constexpr in C++20
- template<class X> complex<double>& operator/=(const complex<X>&); // constexpr in C++20
- };
- template<>
- class complex<long double>
- {
- public:
- typedef long double value_type;
- constexpr complex(long double re = 0.0L, long double im = 0.0L);
- constexpr complex(const complex<float>&);
- constexpr complex(const complex<double>&);
- constexpr long double real() const;
- void real(long double); // constexpr in C++20
- constexpr long double imag() const;
- void imag(long double); // constexpr in C++20
- complex<long double>& operator=(const complex<long double>&); // constexpr in C++20
- complex<long double>& operator= (long double); // constexpr in C++20
- complex<long double>& operator+=(long double); // constexpr in C++20
- complex<long double>& operator-=(long double); // constexpr in C++20
- complex<long double>& operator*=(long double); // constexpr in C++20
- complex<long double>& operator/=(long double); // constexpr in C++20
- template<class X> complex<long double>& operator= (const complex<X>&); // constexpr in C++20
- template<class X> complex<long double>& operator+=(const complex<X>&); // constexpr in C++20
- template<class X> complex<long double>& operator-=(const complex<X>&); // constexpr in C++20
- template<class X> complex<long double>& operator*=(const complex<X>&); // constexpr in C++20
- template<class X> complex<long double>& operator/=(const complex<X>&); // constexpr in C++20
- };
- // 26.3.6 operators:
- template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator+(const complex<T>&, const T&); // constexpr in C++20
- template<class T> complex<T> operator+(const T&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator-(const complex<T>&, const T&); // constexpr in C++20
- template<class T> complex<T> operator-(const T&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator*(const complex<T>&, const T&); // constexpr in C++20
- template<class T> complex<T> operator*(const T&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator/(const complex<T>&, const T&); // constexpr in C++20
- template<class T> complex<T> operator/(const T&, const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator+(const complex<T>&); // constexpr in C++20
- template<class T> complex<T> operator-(const complex<T>&); // constexpr in C++20
- template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
- template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
- template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14
- template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14
- template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14
- template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14
- template<class T, class charT, class traits>
- basic_istream<charT, traits>&
- operator>>(basic_istream<charT, traits>&, complex<T>&);
- template<class T, class charT, class traits>
- basic_ostream<charT, traits>&
- operator<<(basic_ostream<charT, traits>&, const complex<T>&);
- // 26.3.7 values:
- template<class T> T real(const complex<T>&); // constexpr in C++14
- long double real(long double); // constexpr in C++14
- double real(double); // constexpr in C++14
- template<Integral T> double real(T); // constexpr in C++14
- float real(float); // constexpr in C++14
- template<class T> T imag(const complex<T>&); // constexpr in C++14
- long double imag(long double); // constexpr in C++14
- double imag(double); // constexpr in C++14
- template<Integral T> double imag(T); // constexpr in C++14
- float imag(float); // constexpr in C++14
- template<class T> T abs(const complex<T>&);
- template<class T> T arg(const complex<T>&);
- long double arg(long double);
- double arg(double);
- template<Integral T> double arg(T);
- float arg(float);
- template<class T> T norm(const complex<T>&); // constexpr in C++20
- long double norm(long double); // constexpr in C++20
- double norm(double); // constexpr in C++20
- template<Integral T> double norm(T); // constexpr in C++20
- float norm(float); // constexpr in C++20
- template<class T> complex<T> conj(const complex<T>&); // constexpr in C++20
- complex<long double> conj(long double); // constexpr in C++20
- complex<double> conj(double); // constexpr in C++20
- template<Integral T> complex<double> conj(T); // constexpr in C++20
- complex<float> conj(float); // constexpr in C++20
- template<class T> complex<T> proj(const complex<T>&);
- complex<long double> proj(long double);
- complex<double> proj(double);
- template<Integral T> complex<double> proj(T);
- complex<float> proj(float);
- template<class T> complex<T> polar(const T&, const T& = T());
- // 26.3.8 transcendentals:
- template<class T> complex<T> acos(const complex<T>&);
- template<class T> complex<T> asin(const complex<T>&);
- template<class T> complex<T> atan(const complex<T>&);
- template<class T> complex<T> acosh(const complex<T>&);
- template<class T> complex<T> asinh(const complex<T>&);
- template<class T> complex<T> atanh(const complex<T>&);
- template<class T> complex<T> cos (const complex<T>&);
- template<class T> complex<T> cosh (const complex<T>&);
- template<class T> complex<T> exp (const complex<T>&);
- template<class T> complex<T> log (const complex<T>&);
- template<class T> complex<T> log10(const complex<T>&);
- template<class T> complex<T> pow(const complex<T>&, const T&);
- template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
- template<class T> complex<T> pow(const T&, const complex<T>&);
- template<class T> complex<T> sin (const complex<T>&);
- template<class T> complex<T> sinh (const complex<T>&);
- template<class T> complex<T> sqrt (const complex<T>&);
- template<class T> complex<T> tan (const complex<T>&);
- template<class T> complex<T> tanh (const complex<T>&);
- } // std
- */
- #include <__assert> // all public C++ headers provide the assertion handler
- #include <__config>
- #include <cmath>
- #include <iosfwd>
- #include <stdexcept>
- #include <type_traits>
- #include <version>
- #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
- # include <sstream> // for std::basic_ostringstream
- #endif
- #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
- # pragma GCC system_header
- #endif
- _LIBCPP_BEGIN_NAMESPACE_STD
- template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
- template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
- template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
- template<class _Tp>
- class _LIBCPP_TEMPLATE_VIS complex
- {
- public:
- typedef _Tp value_type;
- private:
- value_type __re_;
- value_type __im_;
- public:
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- complex(const value_type& __re = value_type(), const value_type& __im = value_type())
- : __re_(__re), __im_(__im) {}
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- complex(const complex<_Xp>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const {return __re_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const {return __im_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const value_type& __re)
- {__re_ = __re; __im_ = value_type(); return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
- {
- __re_ = __c.real();
- __im_ = __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
- {
- __re_ += __c.real();
- __im_ += __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
- {
- __re_ -= __c.real();
- __im_ -= __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
- {
- *this = *this * complex(__c.real(), __c.imag());
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
- {
- *this = *this / complex(__c.real(), __c.imag());
- return *this;
- }
- };
- template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
- template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
- template<>
- class _LIBCPP_TEMPLATE_VIS complex<float>
- {
- float __re_;
- float __im_;
- public:
- typedef float value_type;
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
- : __re_(__re), __im_(__im) {}
- _LIBCPP_INLINE_VISIBILITY
- explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
- _LIBCPP_INLINE_VISIBILITY
- explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (float __re)
- {__re_ = __re; __im_ = value_type(); return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {__re_ += __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {__re_ -= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
- {
- __re_ = __c.real();
- __im_ = __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
- {
- __re_ += __c.real();
- __im_ += __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
- {
- __re_ -= __c.real();
- __im_ -= __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
- {
- *this = *this * complex(__c.real(), __c.imag());
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
- {
- *this = *this / complex(__c.real(), __c.imag());
- return *this;
- }
- };
- template<>
- class _LIBCPP_TEMPLATE_VIS complex<double>
- {
- double __re_;
- double __im_;
- public:
- typedef double value_type;
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
- : __re_(__re), __im_(__im) {}
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
- _LIBCPP_INLINE_VISIBILITY
- explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (double __re)
- {__re_ = __re; __im_ = value_type(); return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {__re_ += __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {__re_ -= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
- {
- __re_ = __c.real();
- __im_ = __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
- {
- __re_ += __c.real();
- __im_ += __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
- {
- __re_ -= __c.real();
- __im_ -= __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
- {
- *this = *this * complex(__c.real(), __c.imag());
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
- {
- *this = *this / complex(__c.real(), __c.imag());
- return *this;
- }
- };
- template<>
- class _LIBCPP_TEMPLATE_VIS complex<long double>
- {
- long double __re_;
- long double __im_;
- public:
- typedef long double value_type;
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
- : __re_(__re), __im_(__im) {}
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
- _LIBCPP_INLINE_VISIBILITY
- _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (long double __re)
- {__re_ = __re; __im_ = value_type(); return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {__re_ += __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {__re_ -= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
- _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
- {
- __re_ = __c.real();
- __im_ = __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
- {
- __re_ += __c.real();
- __im_ += __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
- {
- __re_ -= __c.real();
- __im_ -= __c.imag();
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
- {
- *this = *this * complex(__c.real(), __c.imag());
- return *this;
- }
- template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
- {
- *this = *this / complex(__c.real(), __c.imag());
- return *this;
- }
- };
- inline
- _LIBCPP_CONSTEXPR
- complex<float>::complex(const complex<double>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- inline
- _LIBCPP_CONSTEXPR
- complex<float>::complex(const complex<long double>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- inline
- _LIBCPP_CONSTEXPR
- complex<double>::complex(const complex<float>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- inline
- _LIBCPP_CONSTEXPR
- complex<double>::complex(const complex<long double>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- inline
- _LIBCPP_CONSTEXPR
- complex<long double>::complex(const complex<float>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- inline
- _LIBCPP_CONSTEXPR
- complex<long double>::complex(const complex<double>& __c)
- : __re_(__c.real()), __im_(__c.imag()) {}
- // 26.3.6 operators:
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
- {
- complex<_Tp> __t(__x);
- __t += __y;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator+(const complex<_Tp>& __x, const _Tp& __y)
- {
- complex<_Tp> __t(__x);
- __t += __y;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator+(const _Tp& __x, const complex<_Tp>& __y)
- {
- complex<_Tp> __t(__y);
- __t += __x;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
- {
- complex<_Tp> __t(__x);
- __t -= __y;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator-(const complex<_Tp>& __x, const _Tp& __y)
- {
- complex<_Tp> __t(__x);
- __t -= __y;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator-(const _Tp& __x, const complex<_Tp>& __y)
- {
- complex<_Tp> __t(-__y);
- __t += __x;
- return __t;
- }
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
- operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
- {
- _Tp __a = __z.real();
- _Tp __b = __z.imag();
- _Tp __c = __w.real();
- _Tp __d = __w.imag();
- // Avoid floating point operations that are invalid during constant evaluation
- if (__libcpp_is_constant_evaluated()) {
- bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
- bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
- bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
- bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
- bool __z_nan = !__z_inf && (
- (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
- || (std::__constexpr_isnan(__a) && __b == _Tp(0))
- || (__a == _Tp(0) && std::__constexpr_isnan(__b))
- );
- bool __w_nan = !__w_inf && (
- (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
- || (std::__constexpr_isnan(__c) && __d == _Tp(0))
- || (__c == _Tp(0) && std::__constexpr_isnan(__d))
- );
- if (__z_nan || __w_nan) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
- }
- if (__z_inf || __w_inf) {
- if (__z_zero || __w_zero) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
- }
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
- }
- bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
- bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
- if (__z_nonzero_nan || __w_nonzero_nan) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
- }
- }
- _Tp __ac = __a * __c;
- _Tp __bd = __b * __d;
- _Tp __ad = __a * __d;
- _Tp __bc = __b * __c;
- _Tp __x = __ac - __bd;
- _Tp __y = __ad + __bc;
- if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
- {
- bool __recalc = false;
- if (std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b))
- {
- __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
- __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
- if (std::__constexpr_isnan(__c))
- __c = std::__constexpr_copysign(_Tp(0), __c);
- if (std::__constexpr_isnan(__d))
- __d = std::__constexpr_copysign(_Tp(0), __d);
- __recalc = true;
- }
- if (std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d))
- {
- __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
- __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
- if (std::__constexpr_isnan(__a))
- __a = std::__constexpr_copysign(_Tp(0), __a);
- if (std::__constexpr_isnan(__b))
- __b = std::__constexpr_copysign(_Tp(0), __b);
- __recalc = true;
- }
- if (!__recalc && (std::__constexpr_isinf(__ac) || std::__constexpr_isinf(__bd) ||
- std::__constexpr_isinf(__ad) || std::__constexpr_isinf(__bc)))
- {
- if (std::__constexpr_isnan(__a))
- __a = std::__constexpr_copysign(_Tp(0), __a);
- if (std::__constexpr_isnan(__b))
- __b = std::__constexpr_copysign(_Tp(0), __b);
- if (std::__constexpr_isnan(__c))
- __c = std::__constexpr_copysign(_Tp(0), __c);
- if (std::__constexpr_isnan(__d))
- __d = std::__constexpr_copysign(_Tp(0), __d);
- __recalc = true;
- }
- if (__recalc)
- {
- __x = _Tp(INFINITY) * (__a * __c - __b * __d);
- __y = _Tp(INFINITY) * (__a * __d + __b * __c);
- }
- }
- return complex<_Tp>(__x, __y);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator*(const complex<_Tp>& __x, const _Tp& __y)
- {
- complex<_Tp> __t(__x);
- __t *= __y;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator*(const _Tp& __x, const complex<_Tp>& __y)
- {
- complex<_Tp> __t(__y);
- __t *= __x;
- return __t;
- }
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
- operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
- {
- int __ilogbw = 0;
- _Tp __a = __z.real();
- _Tp __b = __z.imag();
- _Tp __c = __w.real();
- _Tp __d = __w.imag();
- _Tp __logbw = std::__constexpr_logb(std::__constexpr_fmax(std::__constexpr_fabs(__c), std::__constexpr_fabs(__d)));
- if (std::__constexpr_isfinite(__logbw))
- {
- __ilogbw = static_cast<int>(__logbw);
- __c = std::__constexpr_scalbn(__c, -__ilogbw);
- __d = std::__constexpr_scalbn(__d, -__ilogbw);
- }
- // Avoid floating point operations that are invalid during constant evaluation
- if (__libcpp_is_constant_evaluated()) {
- bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
- bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
- bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
- bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
- bool __z_nan = !__z_inf && (
- (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
- || (std::__constexpr_isnan(__a) && __b == _Tp(0))
- || (__a == _Tp(0) && std::__constexpr_isnan(__b))
- );
- bool __w_nan = !__w_inf && (
- (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
- || (std::__constexpr_isnan(__c) && __d == _Tp(0))
- || (__c == _Tp(0) && std::__constexpr_isnan(__d))
- );
- if ((__z_nan || __w_nan) || (__z_inf && __w_inf)) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
- }
- bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
- bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
- if (__z_nonzero_nan || __w_nonzero_nan) {
- if (__w_zero) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
- }
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
- }
- if (__w_inf) {
- return complex<_Tp>(_Tp(0), _Tp(0));
- }
- if (__z_inf) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
- }
- if (__w_zero) {
- if (__z_zero) {
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
- }
- return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
- }
- }
- _Tp __denom = __c * __c + __d * __d;
- _Tp __x = std::__constexpr_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
- _Tp __y = std::__constexpr_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
- if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
- {
- if ((__denom == _Tp(0)) && (!std::__constexpr_isnan(__a) || !std::__constexpr_isnan(__b)))
- {
- __x = std::__constexpr_copysign(_Tp(INFINITY), __c) * __a;
- __y = std::__constexpr_copysign(_Tp(INFINITY), __c) * __b;
- } else if ((std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b)) && std::__constexpr_isfinite(__c) &&
- std::__constexpr_isfinite(__d)) {
- __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
- __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
- __x = _Tp(INFINITY) * (__a * __c + __b * __d);
- __y = _Tp(INFINITY) * (__b * __c - __a * __d);
- } else if (std::__constexpr_isinf(__logbw) && __logbw > _Tp(0) && std::__constexpr_isfinite(__a) &&
- std::__constexpr_isfinite(__b)) {
- __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
- __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
- __x = _Tp(0) * (__a * __c + __b * __d);
- __y = _Tp(0) * (__b * __c - __a * __d);
- }
- }
- return complex<_Tp>(__x, __y);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator/(const complex<_Tp>& __x, const _Tp& __y)
- {
- return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator/(const _Tp& __x, const complex<_Tp>& __y)
- {
- complex<_Tp> __t(__x);
- __t /= __y;
- return __t;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator+(const complex<_Tp>& __x)
- {
- return __x;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- operator-(const complex<_Tp>& __x)
- {
- return complex<_Tp>(-__x.real(), -__x.imag());
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- bool
- operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
- {
- return __x.real() == __y.real() && __x.imag() == __y.imag();
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- bool
- operator==(const complex<_Tp>& __x, const _Tp& __y)
- {
- return __x.real() == __y && __x.imag() == 0;
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- bool
- operator==(const _Tp& __x, const complex<_Tp>& __y)
- {
- return __x == __y.real() && 0 == __y.imag();
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- bool
- operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
- {
- return !(__x == __y);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- bool
- operator!=(const complex<_Tp>& __x, const _Tp& __y)
- {
- return !(__x == __y);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- bool
- operator!=(const _Tp& __x, const complex<_Tp>& __y)
- {
- return !(__x == __y);
- }
- // 26.3.7 values:
- template <class _Tp, bool = is_integral<_Tp>::value,
- bool = is_floating_point<_Tp>::value
- >
- struct __libcpp_complex_overload_traits {};
- // Integral Types
- template <class _Tp>
- struct __libcpp_complex_overload_traits<_Tp, true, false>
- {
- typedef double _ValueType;
- typedef complex<double> _ComplexType;
- };
- // Floating point types
- template <class _Tp>
- struct __libcpp_complex_overload_traits<_Tp, false, true>
- {
- typedef _Tp _ValueType;
- typedef complex<_Tp> _ComplexType;
- };
- // real
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- _Tp
- real(const complex<_Tp>& __c)
- {
- return __c.real();
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- typename __libcpp_complex_overload_traits<_Tp>::_ValueType
- real(_Tp __re)
- {
- return __re;
- }
- // imag
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- _Tp
- imag(const complex<_Tp>& __c)
- {
- return __c.imag();
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
- typename __libcpp_complex_overload_traits<_Tp>::_ValueType
- imag(_Tp)
- {
- return 0;
- }
- // abs
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _Tp
- abs(const complex<_Tp>& __c)
- {
- return std::hypot(__c.real(), __c.imag());
- }
- // arg
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- _Tp
- arg(const complex<_Tp>& __c)
- {
- return std::atan2(__c.imag(), __c.real());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if<
- is_same<_Tp, long double>::value,
- long double
- >::type
- arg(_Tp __re)
- {
- return std::atan2l(0.L, __re);
- }
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_integral<_Tp>::value || is_same<_Tp, double>::value,
- double
- >::type
- arg(_Tp __re)
- {
- return std::atan2(0., __re);
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if<
- is_same<_Tp, float>::value,
- float
- >::type
- arg(_Tp __re)
- {
- return std::atan2f(0.F, __re);
- }
- // norm
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- _Tp
- norm(const complex<_Tp>& __c)
- {
- if (std::__constexpr_isinf(__c.real()))
- return std::abs(__c.real());
- if (std::__constexpr_isinf(__c.imag()))
- return std::abs(__c.imag());
- return __c.real() * __c.real() + __c.imag() * __c.imag();
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- typename __libcpp_complex_overload_traits<_Tp>::_ValueType
- norm(_Tp __re)
- {
- typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
- return static_cast<_ValueType>(__re) * __re;
- }
- // conj
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- complex<_Tp>
- conj(const complex<_Tp>& __c)
- {
- return complex<_Tp>(__c.real(), -__c.imag());
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
- typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
- conj(_Tp __re)
- {
- typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
- return _ComplexType(__re);
- }
- // proj
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<_Tp>
- proj(const complex<_Tp>& __c)
- {
- complex<_Tp> __r = __c;
- if (std::__constexpr_isinf(__c.real()) || std::__constexpr_isinf(__c.imag()))
- __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
- return __r;
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_floating_point<_Tp>::value,
- typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
- >::type
- proj(_Tp __re)
- {
- if (std::__constexpr_isinf(__re))
- __re = std::abs(__re);
- return complex<_Tp>(__re);
- }
- template <class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_integral<_Tp>::value,
- typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
- >::type
- proj(_Tp __re)
- {
- typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
- return _ComplexType(__re);
- }
- // polar
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- polar(const _Tp& __rho, const _Tp& __theta = _Tp())
- {
- if (std::__constexpr_isnan(__rho) || std::signbit(__rho))
- return complex<_Tp>(_Tp(NAN), _Tp(NAN));
- if (std::__constexpr_isnan(__theta))
- {
- if (std::__constexpr_isinf(__rho))
- return complex<_Tp>(__rho, __theta);
- return complex<_Tp>(__theta, __theta);
- }
- if (std::__constexpr_isinf(__theta))
- {
- if (std::__constexpr_isinf(__rho))
- return complex<_Tp>(__rho, _Tp(NAN));
- return complex<_Tp>(_Tp(NAN), _Tp(NAN));
- }
- _Tp __x = __rho * std::cos(__theta);
- if (std::__constexpr_isnan(__x))
- __x = 0;
- _Tp __y = __rho * std::sin(__theta);
- if (std::__constexpr_isnan(__y))
- __y = 0;
- return complex<_Tp>(__x, __y);
- }
- // log
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<_Tp>
- log(const complex<_Tp>& __x)
- {
- return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
- }
- // log10
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<_Tp>
- log10(const complex<_Tp>& __x)
- {
- return std::log(__x) / std::log(_Tp(10));
- }
- // sqrt
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- sqrt(const complex<_Tp>& __x)
- {
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(_Tp(INFINITY), __x.imag());
- if (std::__constexpr_isinf(__x.real()))
- {
- if (__x.real() > _Tp(0))
- return complex<_Tp>(__x.real(), std::__constexpr_isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
- return complex<_Tp>(std::__constexpr_isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
- }
- return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
- }
- // exp
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- exp(const complex<_Tp>& __x)
- {
- _Tp __i = __x.imag();
- if (__i == 0) {
- return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
- }
- if (std::__constexpr_isinf(__x.real()))
- {
- if (__x.real() < _Tp(0))
- {
- if (!std::__constexpr_isfinite(__i))
- __i = _Tp(1);
- }
- else if (__i == 0 || !std::__constexpr_isfinite(__i))
- {
- if (std::__constexpr_isinf(__i))
- __i = _Tp(NAN);
- return complex<_Tp>(__x.real(), __i);
- }
- }
- _Tp __e = std::exp(__x.real());
- return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
- }
- // pow
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<_Tp>
- pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
- {
- return std::exp(__y * std::log(__x));
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<typename __promote<_Tp, _Up>::type>
- pow(const complex<_Tp>& __x, const complex<_Up>& __y)
- {
- typedef complex<typename __promote<_Tp, _Up>::type> result_type;
- return _VSTD::pow(result_type(__x), result_type(__y));
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_arithmetic<_Up>::value,
- complex<typename __promote<_Tp, _Up>::type>
- >::type
- pow(const complex<_Tp>& __x, const _Up& __y)
- {
- typedef complex<typename __promote<_Tp, _Up>::type> result_type;
- return _VSTD::pow(result_type(__x), result_type(__y));
- }
- template<class _Tp, class _Up>
- inline _LIBCPP_INLINE_VISIBILITY
- typename enable_if
- <
- is_arithmetic<_Tp>::value,
- complex<typename __promote<_Tp, _Up>::type>
- >::type
- pow(const _Tp& __x, const complex<_Up>& __y)
- {
- typedef complex<typename __promote<_Tp, _Up>::type> result_type;
- return _VSTD::pow(result_type(__x), result_type(__y));
- }
- // __sqr, computes pow(x, 2)
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<_Tp>
- __sqr(const complex<_Tp>& __x)
- {
- return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
- _Tp(2) * __x.real() * __x.imag());
- }
- // asinh
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- asinh(const complex<_Tp>& __x)
- {
- const _Tp __pi(atan2(+0., -0.));
- if (std::__constexpr_isinf(__x.real()))
- {
- if (std::__constexpr_isnan(__x.imag()))
- return __x;
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
- return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
- }
- if (std::__constexpr_isnan(__x.real()))
- {
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(__x.imag(), __x.real());
- if (__x.imag() == 0)
- return __x;
- return complex<_Tp>(__x.real(), __x.real());
- }
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
- complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
- return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
- }
- // acosh
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- acosh(const complex<_Tp>& __x)
- {
- const _Tp __pi(atan2(+0., -0.));
- if (std::__constexpr_isinf(__x.real()))
- {
- if (std::__constexpr_isnan(__x.imag()))
- return complex<_Tp>(std::abs(__x.real()), __x.imag());
- if (std::__constexpr_isinf(__x.imag()))
- {
- if (__x.real() > 0)
- return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
- else
- return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
- }
- if (__x.real() < 0)
- return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
- return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
- }
- if (std::__constexpr_isnan(__x.real()))
- {
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(std::abs(__x.imag()), __x.real());
- return complex<_Tp>(__x.real(), __x.real());
- }
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi/_Tp(2), __x.imag()));
- complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
- return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
- }
- // atanh
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- atanh(const complex<_Tp>& __x)
- {
- const _Tp __pi(atan2(+0., -0.));
- if (std::__constexpr_isinf(__x.imag()))
- {
- return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
- }
- if (std::__constexpr_isnan(__x.imag()))
- {
- if (std::__constexpr_isinf(__x.real()) || __x.real() == 0)
- return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
- return complex<_Tp>(__x.imag(), __x.imag());
- }
- if (std::__constexpr_isnan(__x.real()))
- {
- return complex<_Tp>(__x.real(), __x.real());
- }
- if (std::__constexpr_isinf(__x.real()))
- {
- return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
- }
- if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
- {
- return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
- }
- complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
- return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
- }
- // sinh
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- sinh(const complex<_Tp>& __x)
- {
- if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
- return complex<_Tp>(__x.real(), _Tp(NAN));
- if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
- return complex<_Tp>(__x.real(), _Tp(NAN));
- if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
- return __x;
- return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
- }
- // cosh
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- cosh(const complex<_Tp>& __x)
- {
- if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
- return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
- if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
- return complex<_Tp>(_Tp(NAN), __x.real());
- if (__x.real() == 0 && __x.imag() == 0)
- return complex<_Tp>(_Tp(1), __x.imag());
- if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
- return complex<_Tp>(std::abs(__x.real()), __x.imag());
- return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
- }
- // tanh
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- tanh(const complex<_Tp>& __x)
- {
- if (std::__constexpr_isinf(__x.real()))
- {
- if (!std::__constexpr_isfinite(__x.imag()))
- return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
- return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
- }
- if (std::__constexpr_isnan(__x.real()) && __x.imag() == 0)
- return __x;
- _Tp __2r(_Tp(2) * __x.real());
- _Tp __2i(_Tp(2) * __x.imag());
- _Tp __d(std::cosh(__2r) + std::cos(__2i));
- _Tp __2rsh(std::sinh(__2r));
- if (std::__constexpr_isinf(__2rsh) && std::__constexpr_isinf(__d))
- return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
- __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
- return complex<_Tp>(__2rsh/__d, std::sin(__2i)/__d);
- }
- // asin
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- asin(const complex<_Tp>& __x)
- {
- complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
- return complex<_Tp>(__z.imag(), -__z.real());
- }
- // acos
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- acos(const complex<_Tp>& __x)
- {
- const _Tp __pi(atan2(+0., -0.));
- if (std::__constexpr_isinf(__x.real()))
- {
- if (std::__constexpr_isnan(__x.imag()))
- return complex<_Tp>(__x.imag(), __x.real());
- if (std::__constexpr_isinf(__x.imag()))
- {
- if (__x.real() < _Tp(0))
- return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
- return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
- }
- if (__x.real() < _Tp(0))
- return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
- return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
- }
- if (std::__constexpr_isnan(__x.real()))
- {
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(__x.real(), -__x.imag());
- return complex<_Tp>(__x.real(), __x.real());
- }
- if (std::__constexpr_isinf(__x.imag()))
- return complex<_Tp>(__pi/_Tp(2), -__x.imag());
- // Somehow isnan can be a macro, so we use __constexpr_isnan
- if (__x.real() == 0 && (__x.imag() == 0 || std::__constexpr_isnan(__x.imag())))
- return complex<_Tp>(__pi/_Tp(2), -__x.imag());
- complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
- if (std::signbit(__x.imag()))
- return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
- return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
- }
- // atan
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- atan(const complex<_Tp>& __x)
- {
- complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
- return complex<_Tp>(__z.imag(), -__z.real());
- }
- // sin
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- sin(const complex<_Tp>& __x)
- {
- complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
- return complex<_Tp>(__z.imag(), -__z.real());
- }
- // cos
- template<class _Tp>
- inline _LIBCPP_INLINE_VISIBILITY
- complex<_Tp>
- cos(const complex<_Tp>& __x)
- {
- return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
- }
- // tan
- template<class _Tp>
- _LIBCPP_HIDE_FROM_ABI complex<_Tp>
- tan(const complex<_Tp>& __x)
- {
- complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
- return complex<_Tp>(__z.imag(), -__z.real());
- }
- #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
- template<class _Tp, class _CharT, class _Traits>
- _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
- operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
- {
- if (__is.good())
- {
- std::ws(__is);
- if (__is.peek() == _CharT('('))
- {
- __is.get();
- _Tp __r;
- __is >> __r;
- if (!__is.fail())
- {
- std::ws(__is);
- _CharT __c = __is.peek();
- if (__c == _CharT(','))
- {
- __is.get();
- _Tp __i;
- __is >> __i;
- if (!__is.fail())
- {
- std::ws(__is);
- __c = __is.peek();
- if (__c == _CharT(')'))
- {
- __is.get();
- __x = complex<_Tp>(__r, __i);
- }
- else
- __is.setstate(__is.failbit);
- }
- else
- __is.setstate(__is.failbit);
- }
- else if (__c == _CharT(')'))
- {
- __is.get();
- __x = complex<_Tp>(__r, _Tp(0));
- }
- else
- __is.setstate(__is.failbit);
- }
- else
- __is.setstate(__is.failbit);
- }
- else
- {
- _Tp __r;
- __is >> __r;
- if (!__is.fail())
- __x = complex<_Tp>(__r, _Tp(0));
- else
- __is.setstate(__is.failbit);
- }
- }
- else
- __is.setstate(__is.failbit);
- return __is;
- }
- template<class _Tp, class _CharT, class _Traits>
- _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
- operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
- {
- basic_ostringstream<_CharT, _Traits> __s;
- __s.flags(__os.flags());
- __s.imbue(__os.getloc());
- __s.precision(__os.precision());
- __s << '(' << __x.real() << ',' << __x.imag() << ')';
- return __os << __s.str();
- }
- #endif // !_LIBCPP_HAS_NO_LOCALIZATION
- #if _LIBCPP_STD_VER > 11
- // Literal suffix for complex number literals [complex.literals]
- inline namespace literals
- {
- inline namespace complex_literals
- {
- _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(long double __im)
- {
- return { 0.0l, __im };
- }
- _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(unsigned long long __im)
- {
- return { 0.0l, static_cast<long double>(__im) };
- }
- _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(long double __im)
- {
- return { 0.0, static_cast<double>(__im) };
- }
- _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(unsigned long long __im)
- {
- return { 0.0, static_cast<double>(__im) };
- }
- #if !defined(__CUDACC__)
- _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(long double __im)
- {
- return { 0.0f, static_cast<float>(__im) };
- }
- _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(unsigned long long __im)
- {
- return { 0.0f, static_cast<float>(__im) };
- }
- #endif
- } // namespace complex_literals
- } // namespace literals
- #endif
- _LIBCPP_END_NAMESPACE_STD
- #endif // _LIBCPP_COMPLEX
|