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 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c); 336 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); 337 338 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;} 339 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;} 340 341 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} 342 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} 343 344 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) 345 {__re_ = __re; __im_ = value_type(); return *this;} 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 * complex(__c.real(), __c.imag()); 372 return *this; 373 } 374 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) 375 { 376 *this = *this / complex(__c.real(), __c.imag()); 377 return *this; 378 } 379}; 380 381template<> 382class _LIBCPP_TYPE_VIS_ONLY complex<double> 383{ 384 double __re_; 385 double __im_; 386public: 387 typedef double value_type; 388 389 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) 390 : __re_(__re), __im_(__im) {} 391 _LIBCPP_CONSTEXPR complex(const complex<float>& __c); 392 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); 393 394 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;} 395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;} 396 397 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} 398 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} 399 400 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) 401 {__re_ = __re; __im_ = value_type(); return *this;} 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 * complex(__c.real(), __c.imag()); 428 return *this; 429 } 430 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) 431 { 432 *this = *this / complex(__c.real(), __c.imag()); 433 return *this; 434 } 435}; 436 437template<> 438class _LIBCPP_TYPE_VIS_ONLY complex<long double> 439{ 440 long double __re_; 441 long double __im_; 442public: 443 typedef long double value_type; 444 445 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L) 446 : __re_(__re), __im_(__im) {} 447 _LIBCPP_CONSTEXPR complex(const complex<float>& __c); 448 _LIBCPP_CONSTEXPR complex(const complex<double>& __c); 449 450 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;} 451 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;} 452 453 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} 454 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} 455 456 _LIBCPP_INLINE_VISIBILITY complex& operator= (long double __re) 457 {__re_ = __re; __im_ = value_type(); return *this;} 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 * complex(__c.real(), __c.imag()); 484 return *this; 485 } 486 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) 487 { 488 *this = *this / complex(__c.real(), __c.imag()); 489 return *this; 490 } 491}; 492 493inline _LIBCPP_INLINE_VISIBILITY 494_LIBCPP_CONSTEXPR 495complex<float>::complex(const complex<double>& __c) 496 : __re_(__c.real()), __im_(__c.imag()) {} 497 498inline _LIBCPP_INLINE_VISIBILITY 499_LIBCPP_CONSTEXPR 500complex<float>::complex(const complex<long double>& __c) 501 : __re_(__c.real()), __im_(__c.imag()) {} 502 503inline _LIBCPP_INLINE_VISIBILITY 504_LIBCPP_CONSTEXPR 505complex<double>::complex(const complex<float>& __c) 506 : __re_(__c.real()), __im_(__c.imag()) {} 507 508inline _LIBCPP_INLINE_VISIBILITY 509_LIBCPP_CONSTEXPR 510complex<double>::complex(const complex<long double>& __c) 511 : __re_(__c.real()), __im_(__c.imag()) {} 512 513inline _LIBCPP_INLINE_VISIBILITY 514_LIBCPP_CONSTEXPR 515complex<long double>::complex(const complex<float>& __c) 516 : __re_(__c.real()), __im_(__c.imag()) {} 517 518inline _LIBCPP_INLINE_VISIBILITY 519_LIBCPP_CONSTEXPR 520complex<long double>::complex(const complex<double>& __c) 521 : __re_(__c.real()), __im_(__c.imag()) {} 522 523// 26.3.6 operators: 524 525template<class _Tp> 526inline _LIBCPP_INLINE_VISIBILITY 527complex<_Tp> 528operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 529{ 530 complex<_Tp> __t(__x); 531 __t += __y; 532 return __t; 533} 534 535template<class _Tp> 536inline _LIBCPP_INLINE_VISIBILITY 537complex<_Tp> 538operator+(const complex<_Tp>& __x, const _Tp& __y) 539{ 540 complex<_Tp> __t(__x); 541 __t += __y; 542 return __t; 543} 544 545template<class _Tp> 546inline _LIBCPP_INLINE_VISIBILITY 547complex<_Tp> 548operator+(const _Tp& __x, const complex<_Tp>& __y) 549{ 550 complex<_Tp> __t(__y); 551 __t += __x; 552 return __t; 553} 554 555template<class _Tp> 556inline _LIBCPP_INLINE_VISIBILITY 557complex<_Tp> 558operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 559{ 560 complex<_Tp> __t(__x); 561 __t -= __y; 562 return __t; 563} 564 565template<class _Tp> 566inline _LIBCPP_INLINE_VISIBILITY 567complex<_Tp> 568operator-(const complex<_Tp>& __x, const _Tp& __y) 569{ 570 complex<_Tp> __t(__x); 571 __t -= __y; 572 return __t; 573} 574 575template<class _Tp> 576inline _LIBCPP_INLINE_VISIBILITY 577complex<_Tp> 578operator-(const _Tp& __x, const complex<_Tp>& __y) 579{ 580 complex<_Tp> __t(-__y); 581 __t += __x; 582 return __t; 583} 584 585template<class _Tp> 586complex<_Tp> 587operator*(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 644template<class _Tp> 645inline _LIBCPP_INLINE_VISIBILITY 646complex<_Tp> 647operator*(const complex<_Tp>& __x, const _Tp& __y) 648{ 649 complex<_Tp> __t(__x); 650 __t *= __y; 651 return __t; 652} 653 654template<class _Tp> 655inline _LIBCPP_INLINE_VISIBILITY 656complex<_Tp> 657operator*(const _Tp& __x, const complex<_Tp>& __y) 658{ 659 complex<_Tp> __t(__y); 660 __t *= __x; 661 return __t; 662} 663 664template<class _Tp> 665complex<_Tp> 666operator/(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 708template<class _Tp> 709inline _LIBCPP_INLINE_VISIBILITY 710complex<_Tp> 711operator/(const complex<_Tp>& __x, const _Tp& __y) 712{ 713 return complex<_Tp>(__x.real() / __y, __x.imag() / __y); 714} 715 716template<class _Tp> 717inline _LIBCPP_INLINE_VISIBILITY 718complex<_Tp> 719operator/(const _Tp& __x, const complex<_Tp>& __y) 720{ 721 complex<_Tp> __t(__x); 722 __t /= __y; 723 return __t; 724} 725 726template<class _Tp> 727inline _LIBCPP_INLINE_VISIBILITY 728complex<_Tp> 729operator+(const complex<_Tp>& __x) 730{ 731 return __x; 732} 733 734template<class _Tp> 735inline _LIBCPP_INLINE_VISIBILITY 736complex<_Tp> 737operator-(const complex<_Tp>& __x) 738{ 739 return complex<_Tp>(-__x.real(), -__x.imag()); 740} 741 742template<class _Tp> 743inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 744bool 745operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 746{ 747 return __x.real() == __y.real() && __x.imag() == __y.imag(); 748} 749 750template<class _Tp> 751inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 752bool 753operator==(const complex<_Tp>& __x, const _Tp& __y) 754{ 755 return __x.real() == __y && __x.imag() == 0; 756} 757 758template<class _Tp> 759inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 760bool 761operator==(const _Tp& __x, const complex<_Tp>& __y) 762{ 763 return __x == __y.real() && 0 == __y.imag(); 764} 765 766template<class _Tp> 767inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 768bool 769operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) 770{ 771 return !(__x == __y); 772} 773 774template<class _Tp> 775inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 776bool 777operator!=(const complex<_Tp>& __x, const _Tp& __y) 778{ 779 return !(__x == __y); 780} 781 782template<class _Tp> 783inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 784bool 785operator!=(const _Tp& __x, const complex<_Tp>& __y) 786{ 787 return !(__x == __y); 788} 789 790// 26.3.7 values: 791 792// real 793 794template<class _Tp> 795inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 796_Tp 797real(const complex<_Tp>& __c) 798{ 799 return __c.real(); 800} 801 802inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 803long double 804real(long double __re) 805{ 806 return __re; 807} 808 809inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 810double 811real(double __re) 812{ 813 return __re; 814} 815 816template<class _Tp> 817inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 818typename enable_if 819< 820 is_integral<_Tp>::value, 821 double 822>::type 823real(_Tp __re) 824{ 825 return __re; 826} 827 828inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 829float 830real(float __re) 831{ 832 return __re; 833} 834 835// imag 836 837template<class _Tp> 838inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 839_Tp 840imag(const complex<_Tp>& __c) 841{ 842 return __c.imag(); 843} 844 845inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 846long double 847imag(long double __re) 848{ 849 return 0; 850} 851 852inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 853double 854imag(double __re) 855{ 856 return 0; 857} 858 859template<class _Tp> 860inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 861typename enable_if 862< 863 is_integral<_Tp>::value, 864 double 865>::type 866imag(_Tp __re) 867{ 868 return 0; 869} 870 871inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 872float 873imag(float __re) 874{ 875 return 0; 876} 877 878// abs 879 880template<class _Tp> 881inline _LIBCPP_INLINE_VISIBILITY 882_Tp 883abs(const complex<_Tp>& __c) 884{ 885 return hypot(__c.real(), __c.imag()); 886} 887 888// arg 889 890template<class _Tp> 891inline _LIBCPP_INLINE_VISIBILITY 892_Tp 893arg(const complex<_Tp>& __c) 894{ 895 return atan2(__c.imag(), __c.real()); 896} 897 898inline _LIBCPP_INLINE_VISIBILITY 899long double 900arg(long double __re) 901{ 902 return atan2l(0.L, __re); 903} 904 905inline _LIBCPP_INLINE_VISIBILITY 906double 907arg(double __re) 908{ 909 return atan2(0., __re); 910} 911 912template<class _Tp> 913inline _LIBCPP_INLINE_VISIBILITY 914typename enable_if 915< 916 is_integral<_Tp>::value, 917 double 918>::type 919arg(_Tp __re) 920{ 921 return atan2(0., __re); 922} 923 924inline _LIBCPP_INLINE_VISIBILITY 925float 926arg(float __re) 927{ 928 return atan2f(0.F, __re); 929} 930 931// norm 932 933template<class _Tp> 934inline _LIBCPP_INLINE_VISIBILITY 935_Tp 936norm(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 945inline _LIBCPP_INLINE_VISIBILITY 946long double 947norm(long double __re) 948{ 949 return __re * __re; 950} 951 952inline _LIBCPP_INLINE_VISIBILITY 953double 954norm(double __re) 955{ 956 return __re * __re; 957} 958 959template<class _Tp> 960inline _LIBCPP_INLINE_VISIBILITY 961typename enable_if 962< 963 is_integral<_Tp>::value, 964 double 965>::type 966norm(_Tp __re) 967{ 968 return (double)__re * __re; 969} 970 971inline _LIBCPP_INLINE_VISIBILITY 972float 973norm(float __re) 974{ 975 return __re * __re; 976} 977 978// conj 979 980template<class _Tp> 981inline _LIBCPP_INLINE_VISIBILITY 982complex<_Tp> 983conj(const complex<_Tp>& __c) 984{ 985 return complex<_Tp>(__c.real(), -__c.imag()); 986} 987 988inline _LIBCPP_INLINE_VISIBILITY 989complex<long double> 990conj(long double __re) 991{ 992 return complex<long double>(__re); 993} 994 995inline _LIBCPP_INLINE_VISIBILITY 996complex<double> 997conj(double __re) 998{ 999 return complex<double>(__re); 1000} 1001 1002template<class _Tp> 1003inline _LIBCPP_INLINE_VISIBILITY 1004typename enable_if 1005< 1006 is_integral<_Tp>::value, 1007 complex<double> 1008>::type 1009conj(_Tp __re) 1010{ 1011 return complex<double>(__re); 1012} 1013 1014inline _LIBCPP_INLINE_VISIBILITY 1015complex<float> 1016conj(float __re) 1017{ 1018 return complex<float>(__re); 1019} 1020 1021// proj 1022 1023template<class _Tp> 1024inline _LIBCPP_INLINE_VISIBILITY 1025complex<_Tp> 1026proj(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 1034inline _LIBCPP_INLINE_VISIBILITY 1035complex<long double> 1036proj(long double __re) 1037{ 1038 if (isinf(__re)) 1039 __re = abs(__re); 1040 return complex<long double>(__re); 1041} 1042 1043inline _LIBCPP_INLINE_VISIBILITY 1044complex<double> 1045proj(double __re) 1046{ 1047 if (isinf(__re)) 1048 __re = abs(__re); 1049 return complex<double>(__re); 1050} 1051 1052template<class _Tp> 1053inline _LIBCPP_INLINE_VISIBILITY 1054typename enable_if 1055< 1056 is_integral<_Tp>::value, 1057 complex<double> 1058>::type 1059proj(_Tp __re) 1060{ 1061 return complex<double>(__re); 1062} 1063 1064inline _LIBCPP_INLINE_VISIBILITY 1065complex<float> 1066proj(float __re) 1067{ 1068 if (isinf(__re)) 1069 __re = abs(__re); 1070 return complex<float>(__re); 1071} 1072 1073// polar 1074 1075template<class _Tp> 1076complex<_Tp> 1077polar(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 1104template<class _Tp> 1105inline _LIBCPP_INLINE_VISIBILITY 1106complex<_Tp> 1107log(const complex<_Tp>& __x) 1108{ 1109 return complex<_Tp>(log(abs(__x)), arg(__x)); 1110} 1111 1112// log10 1113 1114template<class _Tp> 1115inline _LIBCPP_INLINE_VISIBILITY 1116complex<_Tp> 1117log10(const complex<_Tp>& __x) 1118{ 1119 return log(__x) / log(_Tp(10)); 1120} 1121 1122// sqrt 1123 1124template<class _Tp> 1125complex<_Tp> 1126sqrt(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 1141template<class _Tp> 1142complex<_Tp> 1143exp(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 1168template<class _Tp> 1169inline _LIBCPP_INLINE_VISIBILITY 1170complex<_Tp> 1171pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1172{ 1173 return exp(__y * log(__x)); 1174} 1175 1176template<class _Tp, class _Up> 1177inline _LIBCPP_INLINE_VISIBILITY 1178complex<typename __promote<_Tp, _Up>::type> 1179pow(const complex<_Tp>& __x, const complex<_Up>& __y) 1180{ 1181 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1182 return _VSTD::pow(result_type(__x), result_type(__y)); 1183} 1184 1185template<class _Tp, class _Up> 1186inline _LIBCPP_INLINE_VISIBILITY 1187typename enable_if 1188< 1189 is_arithmetic<_Up>::value, 1190 complex<typename __promote<_Tp, _Up>::type> 1191>::type 1192pow(const complex<_Tp>& __x, const _Up& __y) 1193{ 1194 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1195 return _VSTD::pow(result_type(__x), result_type(__y)); 1196} 1197 1198template<class _Tp, class _Up> 1199inline _LIBCPP_INLINE_VISIBILITY 1200typename enable_if 1201< 1202 is_arithmetic<_Tp>::value, 1203 complex<typename __promote<_Tp, _Up>::type> 1204>::type 1205pow(const _Tp& __x, const complex<_Up>& __y) 1206{ 1207 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1208 return _VSTD::pow(result_type(__x), result_type(__y)); 1209} 1210 1211// asinh 1212 1213template<class _Tp> 1214complex<_Tp> 1215asinh(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 1242template<class _Tp> 1243complex<_Tp> 1244acosh(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 { 1253 if (__x.real() > 0) 1254 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); 1255 else 1256 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag())); 1257 } 1258 if (__x.real() < 0) 1259 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag())); 1260 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); 1261 } 1262 if (isnan(__x.real())) 1263 { 1264 if (isinf(__x.imag())) 1265 return complex<_Tp>(abs(__x.imag()), __x.real()); 1266 return complex<_Tp>(__x.real(), __x.real()); 1267 } 1268 if (isinf(__x.imag())) 1269 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag())); 1270 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1))); 1271 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag())); 1272} 1273 1274// atanh 1275 1276template<class _Tp> 1277complex<_Tp> 1278atanh(const complex<_Tp>& __x) 1279{ 1280 const _Tp __pi(atan2(+0., -0.)); 1281 if (isinf(__x.imag())) 1282 { 1283 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1284 } 1285 if (isnan(__x.imag())) 1286 { 1287 if (isinf(__x.real()) || __x.real() == 0) 1288 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag()); 1289 return complex<_Tp>(__x.imag(), __x.imag()); 1290 } 1291 if (isnan(__x.real())) 1292 { 1293 return complex<_Tp>(__x.real(), __x.real()); 1294 } 1295 if (isinf(__x.real())) 1296 { 1297 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1298 } 1299 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) 1300 { 1301 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag())); 1302 } 1303 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2); 1304 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); 1305} 1306 1307// sinh 1308 1309template<class _Tp> 1310complex<_Tp> 1311sinh(const complex<_Tp>& __x) 1312{ 1313 if (isinf(__x.real()) && !isfinite(__x.imag())) 1314 return complex<_Tp>(__x.real(), _Tp(NAN)); 1315 if (__x.real() == 0 && !isfinite(__x.imag())) 1316 return complex<_Tp>(__x.real(), _Tp(NAN)); 1317 if (__x.imag() == 0 && !isfinite(__x.real())) 1318 return __x; 1319 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag())); 1320} 1321 1322// cosh 1323 1324template<class _Tp> 1325complex<_Tp> 1326cosh(const complex<_Tp>& __x) 1327{ 1328 if (isinf(__x.real()) && !isfinite(__x.imag())) 1329 return complex<_Tp>(abs(__x.real()), _Tp(NAN)); 1330 if (__x.real() == 0 && !isfinite(__x.imag())) 1331 return complex<_Tp>(_Tp(NAN), __x.real()); 1332 if (__x.real() == 0 && __x.imag() == 0) 1333 return complex<_Tp>(_Tp(1), __x.imag()); 1334 if (__x.imag() == 0 && !isfinite(__x.real())) 1335 return complex<_Tp>(abs(__x.real()), __x.imag()); 1336 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag())); 1337} 1338 1339// tanh 1340 1341template<class _Tp> 1342complex<_Tp> 1343tanh(const complex<_Tp>& __x) 1344{ 1345 if (isinf(__x.real())) 1346 { 1347 if (!isfinite(__x.imag())) 1348 return complex<_Tp>(_Tp(1), _Tp(0)); 1349 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag()))); 1350 } 1351 if (isnan(__x.real()) && __x.imag() == 0) 1352 return __x; 1353 _Tp __2r(_Tp(2) * __x.real()); 1354 _Tp __2i(_Tp(2) * __x.imag()); 1355 _Tp __d(cosh(__2r) + cos(__2i)); 1356 _Tp __2rsh(sinh(__2r)); 1357 if (isinf(__2rsh) && isinf(__d)) 1358 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), 1359 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.)); 1360 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d); 1361} 1362 1363// asin 1364 1365template<class _Tp> 1366complex<_Tp> 1367asin(const complex<_Tp>& __x) 1368{ 1369 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real())); 1370 return complex<_Tp>(__z.imag(), -__z.real()); 1371} 1372 1373// acos 1374 1375template<class _Tp> 1376complex<_Tp> 1377acos(const complex<_Tp>& __x) 1378{ 1379 const _Tp __pi(atan2(+0., -0.)); 1380 if (isinf(__x.real())) 1381 { 1382 if (isnan(__x.imag())) 1383 return complex<_Tp>(__x.imag(), __x.real()); 1384 if (isinf(__x.imag())) 1385 { 1386 if (__x.real() < _Tp(0)) 1387 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag()); 1388 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag()); 1389 } 1390 if (__x.real() < _Tp(0)) 1391 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real()); 1392 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real()); 1393 } 1394 if (isnan(__x.real())) 1395 { 1396 if (isinf(__x.imag())) 1397 return complex<_Tp>(__x.real(), -__x.imag()); 1398 return complex<_Tp>(__x.real(), __x.real()); 1399 } 1400 if (isinf(__x.imag())) 1401 return complex<_Tp>(__pi/_Tp(2), -__x.imag()); 1402 if (__x.real() == 0) 1403 return complex<_Tp>(__pi/_Tp(2), -__x.imag()); 1404 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1))); 1405 if (signbit(__x.imag())) 1406 return complex<_Tp>(abs(__z.imag()), abs(__z.real())); 1407 return complex<_Tp>(abs(__z.imag()), -abs(__z.real())); 1408} 1409 1410// atan 1411 1412template<class _Tp> 1413complex<_Tp> 1414atan(const complex<_Tp>& __x) 1415{ 1416 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real())); 1417 return complex<_Tp>(__z.imag(), -__z.real()); 1418} 1419 1420// sin 1421 1422template<class _Tp> 1423complex<_Tp> 1424sin(const complex<_Tp>& __x) 1425{ 1426 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real())); 1427 return complex<_Tp>(__z.imag(), -__z.real()); 1428} 1429 1430// cos 1431 1432template<class _Tp> 1433inline _LIBCPP_INLINE_VISIBILITY 1434complex<_Tp> 1435cos(const complex<_Tp>& __x) 1436{ 1437 return cosh(complex<_Tp>(-__x.imag(), __x.real())); 1438} 1439 1440// tan 1441 1442template<class _Tp> 1443complex<_Tp> 1444tan(const complex<_Tp>& __x) 1445{ 1446 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real())); 1447 return complex<_Tp>(__z.imag(), -__z.real()); 1448} 1449 1450template<class _Tp, class _CharT, class _Traits> 1451basic_istream<_CharT, _Traits>& 1452operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 1453{ 1454 if (__is.good()) 1455 { 1456 ws(__is); 1457 if (__is.peek() == _CharT('(')) 1458 { 1459 __is.get(); 1460 _Tp __r; 1461 __is >> __r; 1462 if (!__is.fail()) 1463 { 1464 ws(__is); 1465 _CharT __c = __is.peek(); 1466 if (__c == _CharT(',')) 1467 { 1468 __is.get(); 1469 _Tp __i; 1470 __is >> __i; 1471 if (!__is.fail()) 1472 { 1473 ws(__is); 1474 __c = __is.peek(); 1475 if (__c == _CharT(')')) 1476 { 1477 __is.get(); 1478 __x = complex<_Tp>(__r, __i); 1479 } 1480 else 1481 __is.setstate(ios_base::failbit); 1482 } 1483 else 1484 __is.setstate(ios_base::failbit); 1485 } 1486 else if (__c == _CharT(')')) 1487 { 1488 __is.get(); 1489 __x = complex<_Tp>(__r, _Tp(0)); 1490 } 1491 else 1492 __is.setstate(ios_base::failbit); 1493 } 1494 else 1495 __is.setstate(ios_base::failbit); 1496 } 1497 else 1498 { 1499 _Tp __r; 1500 __is >> __r; 1501 if (!__is.fail()) 1502 __x = complex<_Tp>(__r, _Tp(0)); 1503 else 1504 __is.setstate(ios_base::failbit); 1505 } 1506 } 1507 else 1508 __is.setstate(ios_base::failbit); 1509 return __is; 1510} 1511 1512template<class _Tp, class _CharT, class _Traits> 1513basic_ostream<_CharT, _Traits>& 1514operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 1515{ 1516 basic_ostringstream<_CharT, _Traits> __s; 1517 __s.flags(__os.flags()); 1518 __s.imbue(__os.getloc()); 1519 __s.precision(__os.precision()); 1520 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 1521 return __os << __s.str(); 1522} 1523 1524#if _LIBCPP_STD_VER > 11 1525// Literal suffix for complex number literals [complex.literals] 1526inline namespace literals 1527{ 1528 inline namespace complex_literals 1529 { 1530 constexpr complex<long double> operator""il(long double __im) 1531 { 1532 return { 0.0l, __im }; 1533 } 1534 1535 constexpr complex<long double> operator""il(unsigned long long __im) 1536 { 1537 return { 0.0l, static_cast<long double>(__im) }; 1538 } 1539 1540 1541 constexpr complex<double> operator""i(long double __im) 1542 { 1543 return { 0.0, static_cast<double>(__im) }; 1544 } 1545 1546 constexpr complex<double> operator""i(unsigned long long __im) 1547 { 1548 return { 0.0, static_cast<double>(__im) }; 1549 } 1550 1551 1552 constexpr complex<float> operator""if(long double __im) 1553 { 1554 return { 0.0f, static_cast<float>(__im) }; 1555 } 1556 1557 constexpr complex<float> operator""if(unsigned long long __im) 1558 { 1559 return { 0.0f, static_cast<float>(__im) }; 1560 } 1561 } 1562} 1563#endif 1564 1565_LIBCPP_END_NAMESPACE_STD 1566 1567#endif // _LIBCPP_COMPLEX 1568