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 798// real 799 800template<class _Tp> 801inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 802_Tp 803real(const complex<_Tp>& __c) 804{ 805 return __c.real(); 806} 807 808inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 809long double 810real(long double __re) 811{ 812 return __re; 813} 814 815inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 816double 817real(double __re) 818{ 819 return __re; 820} 821 822template<class _Tp> 823inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 824typename enable_if 825< 826 is_integral<_Tp>::value, 827 double 828>::type 829real(_Tp __re) 830{ 831 return __re; 832} 833 834inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 835float 836real(float __re) 837{ 838 return __re; 839} 840 841// imag 842 843template<class _Tp> 844inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 845_Tp 846imag(const complex<_Tp>& __c) 847{ 848 return __c.imag(); 849} 850 851inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 852long double 853imag(long double __re) 854{ 855 return 0; 856} 857 858inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 859double 860imag(double __re) 861{ 862 return 0; 863} 864 865template<class _Tp> 866inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 867typename enable_if 868< 869 is_integral<_Tp>::value, 870 double 871>::type 872imag(_Tp __re) 873{ 874 return 0; 875} 876 877inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 878float 879imag(float __re) 880{ 881 return 0; 882} 883 884// abs 885 886template<class _Tp> 887inline _LIBCPP_INLINE_VISIBILITY 888_Tp 889abs(const complex<_Tp>& __c) 890{ 891 return hypot(__c.real(), __c.imag()); 892} 893 894// arg 895 896template<class _Tp> 897inline _LIBCPP_INLINE_VISIBILITY 898_Tp 899arg(const complex<_Tp>& __c) 900{ 901 return atan2(__c.imag(), __c.real()); 902} 903 904inline _LIBCPP_INLINE_VISIBILITY 905long double 906arg(long double __re) 907{ 908 return atan2l(0.L, __re); 909} 910 911inline _LIBCPP_INLINE_VISIBILITY 912double 913arg(double __re) 914{ 915 return atan2(0., __re); 916} 917 918template<class _Tp> 919inline _LIBCPP_INLINE_VISIBILITY 920typename enable_if 921< 922 is_integral<_Tp>::value, 923 double 924>::type 925arg(_Tp __re) 926{ 927 return atan2(0., __re); 928} 929 930inline _LIBCPP_INLINE_VISIBILITY 931float 932arg(float __re) 933{ 934 return atan2f(0.F, __re); 935} 936 937// norm 938 939template<class _Tp> 940inline _LIBCPP_INLINE_VISIBILITY 941_Tp 942norm(const complex<_Tp>& __c) 943{ 944 if (isinf(__c.real())) 945 return abs(__c.real()); 946 if (isinf(__c.imag())) 947 return abs(__c.imag()); 948 return __c.real() * __c.real() + __c.imag() * __c.imag(); 949} 950 951inline _LIBCPP_INLINE_VISIBILITY 952long double 953norm(long double __re) 954{ 955 return __re * __re; 956} 957 958inline _LIBCPP_INLINE_VISIBILITY 959double 960norm(double __re) 961{ 962 return __re * __re; 963} 964 965template<class _Tp> 966inline _LIBCPP_INLINE_VISIBILITY 967typename enable_if 968< 969 is_integral<_Tp>::value, 970 double 971>::type 972norm(_Tp __re) 973{ 974 return (double)__re * __re; 975} 976 977inline _LIBCPP_INLINE_VISIBILITY 978float 979norm(float __re) 980{ 981 return __re * __re; 982} 983 984// conj 985 986template<class _Tp> 987inline _LIBCPP_INLINE_VISIBILITY 988complex<_Tp> 989conj(const complex<_Tp>& __c) 990{ 991 return complex<_Tp>(__c.real(), -__c.imag()); 992} 993 994inline _LIBCPP_INLINE_VISIBILITY 995complex<long double> 996conj(long double __re) 997{ 998 return complex<long double>(__re); 999} 1000 1001inline _LIBCPP_INLINE_VISIBILITY 1002complex<double> 1003conj(double __re) 1004{ 1005 return complex<double>(__re); 1006} 1007 1008template<class _Tp> 1009inline _LIBCPP_INLINE_VISIBILITY 1010typename enable_if 1011< 1012 is_integral<_Tp>::value, 1013 complex<double> 1014>::type 1015conj(_Tp __re) 1016{ 1017 return complex<double>(__re); 1018} 1019 1020inline _LIBCPP_INLINE_VISIBILITY 1021complex<float> 1022conj(float __re) 1023{ 1024 return complex<float>(__re); 1025} 1026 1027// proj 1028 1029template<class _Tp> 1030inline _LIBCPP_INLINE_VISIBILITY 1031complex<_Tp> 1032proj(const complex<_Tp>& __c) 1033{ 1034 std::complex<_Tp> __r = __c; 1035 if (isinf(__c.real()) || isinf(__c.imag())) 1036 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag())); 1037 return __r; 1038} 1039 1040inline _LIBCPP_INLINE_VISIBILITY 1041complex<long double> 1042proj(long double __re) 1043{ 1044 if (isinf(__re)) 1045 __re = abs(__re); 1046 return complex<long double>(__re); 1047} 1048 1049inline _LIBCPP_INLINE_VISIBILITY 1050complex<double> 1051proj(double __re) 1052{ 1053 if (isinf(__re)) 1054 __re = abs(__re); 1055 return complex<double>(__re); 1056} 1057 1058template<class _Tp> 1059inline _LIBCPP_INLINE_VISIBILITY 1060typename enable_if 1061< 1062 is_integral<_Tp>::value, 1063 complex<double> 1064>::type 1065proj(_Tp __re) 1066{ 1067 return complex<double>(__re); 1068} 1069 1070inline _LIBCPP_INLINE_VISIBILITY 1071complex<float> 1072proj(float __re) 1073{ 1074 if (isinf(__re)) 1075 __re = abs(__re); 1076 return complex<float>(__re); 1077} 1078 1079// polar 1080 1081template<class _Tp> 1082complex<_Tp> 1083polar(const _Tp& __rho, const _Tp& __theta = _Tp(0)) 1084{ 1085 if (isnan(__rho) || signbit(__rho)) 1086 return complex<_Tp>(_Tp(NAN), _Tp(NAN)); 1087 if (isnan(__theta)) 1088 { 1089 if (isinf(__rho)) 1090 return complex<_Tp>(__rho, __theta); 1091 return complex<_Tp>(__theta, __theta); 1092 } 1093 if (isinf(__theta)) 1094 { 1095 if (isinf(__rho)) 1096 return complex<_Tp>(__rho, _Tp(NAN)); 1097 return complex<_Tp>(_Tp(NAN), _Tp(NAN)); 1098 } 1099 _Tp __x = __rho * cos(__theta); 1100 if (isnan(__x)) 1101 __x = 0; 1102 _Tp __y = __rho * sin(__theta); 1103 if (isnan(__y)) 1104 __y = 0; 1105 return complex<_Tp>(__x, __y); 1106} 1107 1108// log 1109 1110template<class _Tp> 1111inline _LIBCPP_INLINE_VISIBILITY 1112complex<_Tp> 1113log(const complex<_Tp>& __x) 1114{ 1115 return complex<_Tp>(log(abs(__x)), arg(__x)); 1116} 1117 1118// log10 1119 1120template<class _Tp> 1121inline _LIBCPP_INLINE_VISIBILITY 1122complex<_Tp> 1123log10(const complex<_Tp>& __x) 1124{ 1125 return log(__x) / log(_Tp(10)); 1126} 1127 1128// sqrt 1129 1130template<class _Tp> 1131complex<_Tp> 1132sqrt(const complex<_Tp>& __x) 1133{ 1134 if (isinf(__x.imag())) 1135 return complex<_Tp>(_Tp(INFINITY), __x.imag()); 1136 if (isinf(__x.real())) 1137 { 1138 if (__x.real() > _Tp(0)) 1139 return complex<_Tp>(__x.real(), isnan(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag())); 1140 return complex<_Tp>(isnan(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag())); 1141 } 1142 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2)); 1143} 1144 1145// exp 1146 1147template<class _Tp> 1148complex<_Tp> 1149exp(const complex<_Tp>& __x) 1150{ 1151 _Tp __i = __x.imag(); 1152 if (isinf(__x.real())) 1153 { 1154 if (__x.real() < _Tp(0)) 1155 { 1156 if (!isfinite(__i)) 1157 __i = _Tp(1); 1158 } 1159 else if (__i == 0 || !isfinite(__i)) 1160 { 1161 if (isinf(__i)) 1162 __i = _Tp(NAN); 1163 return complex<_Tp>(__x.real(), __i); 1164 } 1165 } 1166 else if (isnan(__x.real()) && __x.imag() == 0) 1167 return __x; 1168 _Tp __e = exp(__x.real()); 1169 return complex<_Tp>(__e * cos(__i), __e * sin(__i)); 1170} 1171 1172// pow 1173 1174template<class _Tp> 1175inline _LIBCPP_INLINE_VISIBILITY 1176complex<_Tp> 1177pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1178{ 1179 return exp(__y * log(__x)); 1180} 1181 1182template<class _Tp, class _Up> 1183inline _LIBCPP_INLINE_VISIBILITY 1184complex<typename __promote<_Tp, _Up>::type> 1185pow(const complex<_Tp>& __x, const complex<_Up>& __y) 1186{ 1187 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1188 return _VSTD::pow(result_type(__x), result_type(__y)); 1189} 1190 1191template<class _Tp, class _Up> 1192inline _LIBCPP_INLINE_VISIBILITY 1193typename enable_if 1194< 1195 is_arithmetic<_Up>::value, 1196 complex<typename __promote<_Tp, _Up>::type> 1197>::type 1198pow(const complex<_Tp>& __x, const _Up& __y) 1199{ 1200 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1201 return _VSTD::pow(result_type(__x), result_type(__y)); 1202} 1203 1204template<class _Tp, class _Up> 1205inline _LIBCPP_INLINE_VISIBILITY 1206typename enable_if 1207< 1208 is_arithmetic<_Tp>::value, 1209 complex<typename __promote<_Tp, _Up>::type> 1210>::type 1211pow(const _Tp& __x, const complex<_Up>& __y) 1212{ 1213 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1214 return _VSTD::pow(result_type(__x), result_type(__y)); 1215} 1216 1217// asinh 1218 1219template<class _Tp> 1220complex<_Tp> 1221asinh(const complex<_Tp>& __x) 1222{ 1223 const _Tp __pi(atan2(+0., -0.)); 1224 if (isinf(__x.real())) 1225 { 1226 if (isnan(__x.imag())) 1227 return __x; 1228 if (isinf(__x.imag())) 1229 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); 1230 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); 1231 } 1232 if (isnan(__x.real())) 1233 { 1234 if (isinf(__x.imag())) 1235 return complex<_Tp>(__x.imag(), __x.real()); 1236 if (__x.imag() == 0) 1237 return __x; 1238 return complex<_Tp>(__x.real(), __x.real()); 1239 } 1240 if (isinf(__x.imag())) 1241 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1242 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) + _Tp(1))); 1243 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); 1244} 1245 1246// acosh 1247 1248template<class _Tp> 1249complex<_Tp> 1250acosh(const complex<_Tp>& __x) 1251{ 1252 const _Tp __pi(atan2(+0., -0.)); 1253 if (isinf(__x.real())) 1254 { 1255 if (isnan(__x.imag())) 1256 return complex<_Tp>(abs(__x.real()), __x.imag()); 1257 if (isinf(__x.imag())) 1258 { 1259 if (__x.real() > 0) 1260 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); 1261 else 1262 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag())); 1263 } 1264 if (__x.real() < 0) 1265 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag())); 1266 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); 1267 } 1268 if (isnan(__x.real())) 1269 { 1270 if (isinf(__x.imag())) 1271 return complex<_Tp>(abs(__x.imag()), __x.real()); 1272 return complex<_Tp>(__x.real(), __x.real()); 1273 } 1274 if (isinf(__x.imag())) 1275 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag())); 1276 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1))); 1277 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag())); 1278} 1279 1280// atanh 1281 1282template<class _Tp> 1283complex<_Tp> 1284atanh(const complex<_Tp>& __x) 1285{ 1286 const _Tp __pi(atan2(+0., -0.)); 1287 if (isinf(__x.imag())) 1288 { 1289 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1290 } 1291 if (isnan(__x.imag())) 1292 { 1293 if (isinf(__x.real()) || __x.real() == 0) 1294 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag()); 1295 return complex<_Tp>(__x.imag(), __x.imag()); 1296 } 1297 if (isnan(__x.real())) 1298 { 1299 return complex<_Tp>(__x.real(), __x.real()); 1300 } 1301 if (isinf(__x.real())) 1302 { 1303 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1304 } 1305 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) 1306 { 1307 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag())); 1308 } 1309 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2); 1310 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); 1311} 1312 1313// sinh 1314 1315template<class _Tp> 1316complex<_Tp> 1317sinh(const complex<_Tp>& __x) 1318{ 1319 if (isinf(__x.real()) && !isfinite(__x.imag())) 1320 return complex<_Tp>(__x.real(), _Tp(NAN)); 1321 if (__x.real() == 0 && !isfinite(__x.imag())) 1322 return complex<_Tp>(__x.real(), _Tp(NAN)); 1323 if (__x.imag() == 0 && !isfinite(__x.real())) 1324 return __x; 1325 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag())); 1326} 1327 1328// cosh 1329 1330template<class _Tp> 1331complex<_Tp> 1332cosh(const complex<_Tp>& __x) 1333{ 1334 if (isinf(__x.real()) && !isfinite(__x.imag())) 1335 return complex<_Tp>(abs(__x.real()), _Tp(NAN)); 1336 if (__x.real() == 0 && !isfinite(__x.imag())) 1337 return complex<_Tp>(_Tp(NAN), __x.real()); 1338 if (__x.real() == 0 && __x.imag() == 0) 1339 return complex<_Tp>(_Tp(1), __x.imag()); 1340 if (__x.imag() == 0 && !isfinite(__x.real())) 1341 return complex<_Tp>(abs(__x.real()), __x.imag()); 1342 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag())); 1343} 1344 1345// tanh 1346 1347template<class _Tp> 1348complex<_Tp> 1349tanh(const complex<_Tp>& __x) 1350{ 1351 if (isinf(__x.real())) 1352 { 1353 if (!isfinite(__x.imag())) 1354 return complex<_Tp>(_Tp(1), _Tp(0)); 1355 return complex<_Tp>(_Tp(1), copysign(_Tp(0), sin(_Tp(2) * __x.imag()))); 1356 } 1357 if (isnan(__x.real()) && __x.imag() == 0) 1358 return __x; 1359 _Tp __2r(_Tp(2) * __x.real()); 1360 _Tp __2i(_Tp(2) * __x.imag()); 1361 _Tp __d(cosh(__2r) + cos(__2i)); 1362 _Tp __2rsh(sinh(__2r)); 1363 if (isinf(__2rsh) && isinf(__d)) 1364 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), 1365 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.)); 1366 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d); 1367} 1368 1369// asin 1370 1371template<class _Tp> 1372complex<_Tp> 1373asin(const complex<_Tp>& __x) 1374{ 1375 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real())); 1376 return complex<_Tp>(__z.imag(), -__z.real()); 1377} 1378 1379// acos 1380 1381template<class _Tp> 1382complex<_Tp> 1383acos(const complex<_Tp>& __x) 1384{ 1385 const _Tp __pi(atan2(+0., -0.)); 1386 if (isinf(__x.real())) 1387 { 1388 if (isnan(__x.imag())) 1389 return complex<_Tp>(__x.imag(), __x.real()); 1390 if (isinf(__x.imag())) 1391 { 1392 if (__x.real() < _Tp(0)) 1393 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag()); 1394 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag()); 1395 } 1396 if (__x.real() < _Tp(0)) 1397 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real()); 1398 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real()); 1399 } 1400 if (isnan(__x.real())) 1401 { 1402 if (isinf(__x.imag())) 1403 return complex<_Tp>(__x.real(), -__x.imag()); 1404 return complex<_Tp>(__x.real(), __x.real()); 1405 } 1406 if (isinf(__x.imag())) 1407 return complex<_Tp>(__pi/_Tp(2), -__x.imag()); 1408 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag()))) 1409 return complex<_Tp>(__pi/_Tp(2), -__x.imag()); 1410 complex<_Tp> __z = log(__x + sqrt(pow(__x, _Tp(2)) - _Tp(1))); 1411 if (signbit(__x.imag())) 1412 return complex<_Tp>(abs(__z.imag()), abs(__z.real())); 1413 return complex<_Tp>(abs(__z.imag()), -abs(__z.real())); 1414} 1415 1416// atan 1417 1418template<class _Tp> 1419complex<_Tp> 1420atan(const complex<_Tp>& __x) 1421{ 1422 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real())); 1423 return complex<_Tp>(__z.imag(), -__z.real()); 1424} 1425 1426// sin 1427 1428template<class _Tp> 1429complex<_Tp> 1430sin(const complex<_Tp>& __x) 1431{ 1432 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real())); 1433 return complex<_Tp>(__z.imag(), -__z.real()); 1434} 1435 1436// cos 1437 1438template<class _Tp> 1439inline _LIBCPP_INLINE_VISIBILITY 1440complex<_Tp> 1441cos(const complex<_Tp>& __x) 1442{ 1443 return cosh(complex<_Tp>(-__x.imag(), __x.real())); 1444} 1445 1446// tan 1447 1448template<class _Tp> 1449complex<_Tp> 1450tan(const complex<_Tp>& __x) 1451{ 1452 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real())); 1453 return complex<_Tp>(__z.imag(), -__z.real()); 1454} 1455 1456template<class _Tp, class _CharT, class _Traits> 1457basic_istream<_CharT, _Traits>& 1458operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 1459{ 1460 if (__is.good()) 1461 { 1462 ws(__is); 1463 if (__is.peek() == _CharT('(')) 1464 { 1465 __is.get(); 1466 _Tp __r; 1467 __is >> __r; 1468 if (!__is.fail()) 1469 { 1470 ws(__is); 1471 _CharT __c = __is.peek(); 1472 if (__c == _CharT(',')) 1473 { 1474 __is.get(); 1475 _Tp __i; 1476 __is >> __i; 1477 if (!__is.fail()) 1478 { 1479 ws(__is); 1480 __c = __is.peek(); 1481 if (__c == _CharT(')')) 1482 { 1483 __is.get(); 1484 __x = complex<_Tp>(__r, __i); 1485 } 1486 else 1487 __is.setstate(ios_base::failbit); 1488 } 1489 else 1490 __is.setstate(ios_base::failbit); 1491 } 1492 else if (__c == _CharT(')')) 1493 { 1494 __is.get(); 1495 __x = complex<_Tp>(__r, _Tp(0)); 1496 } 1497 else 1498 __is.setstate(ios_base::failbit); 1499 } 1500 else 1501 __is.setstate(ios_base::failbit); 1502 } 1503 else 1504 { 1505 _Tp __r; 1506 __is >> __r; 1507 if (!__is.fail()) 1508 __x = complex<_Tp>(__r, _Tp(0)); 1509 else 1510 __is.setstate(ios_base::failbit); 1511 } 1512 } 1513 else 1514 __is.setstate(ios_base::failbit); 1515 return __is; 1516} 1517 1518template<class _Tp, class _CharT, class _Traits> 1519basic_ostream<_CharT, _Traits>& 1520operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 1521{ 1522 basic_ostringstream<_CharT, _Traits> __s; 1523 __s.flags(__os.flags()); 1524 __s.imbue(__os.getloc()); 1525 __s.precision(__os.precision()); 1526 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 1527 return __os << __s.str(); 1528} 1529 1530#if _LIBCPP_STD_VER > 11 1531// Literal suffix for complex number literals [complex.literals] 1532inline namespace literals 1533{ 1534 inline namespace complex_literals 1535 { 1536 constexpr complex<long double> operator""il(long double __im) 1537 { 1538 return { 0.0l, __im }; 1539 } 1540 1541 constexpr complex<long double> operator""il(unsigned long long __im) 1542 { 1543 return { 0.0l, static_cast<long double>(__im) }; 1544 } 1545 1546 1547 constexpr complex<double> operator""i(long double __im) 1548 { 1549 return { 0.0, static_cast<double>(__im) }; 1550 } 1551 1552 constexpr complex<double> operator""i(unsigned long long __im) 1553 { 1554 return { 0.0, static_cast<double>(__im) }; 1555 } 1556 1557 1558 constexpr complex<float> operator""if(long double __im) 1559 { 1560 return { 0.0f, static_cast<float>(__im) }; 1561 } 1562 1563 constexpr complex<float> operator""if(unsigned long long __im) 1564 { 1565 return { 0.0f, static_cast<float>(__im) }; 1566 } 1567 } 1568} 1569#endif 1570 1571_LIBCPP_END_NAMESPACE_STD 1572 1573#endif // _LIBCPP_COMPLEX 1574