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