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