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