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