1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5// See https://llvm.org/LICENSE.txt for license information. 6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef _LIBCPP_COMPLEX 11#define _LIBCPP_COMPLEX 12 13/* 14 complex synopsis 15 16namespace std 17{ 18 19template<class T> 20class complex 21{ 22public: 23 typedef T value_type; 24 25 complex(const T& re = T(), const T& im = T()); // constexpr in C++14 26 complex(const complex&); // constexpr in C++14 27 template<class X> complex(const complex<X>&); // constexpr in C++14 28 29 T real() const; // constexpr in C++14 30 T imag() const; // constexpr in C++14 31 32 void real(T); 33 void imag(T); 34 35 complex<T>& operator= (const T&); 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 41 complex& operator=(const complex&); 42 template<class X> complex<T>& operator= (const complex<X>&); 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}; 48 49template<> 50class complex<float> 51{ 52public: 53 typedef float value_type; 54 55 constexpr complex(float re = 0.0f, float im = 0.0f); 56 explicit constexpr complex(const complex<double>&); 57 explicit constexpr complex(const complex<long double>&); 58 59 constexpr float real() const; 60 void real(float); 61 constexpr float imag() const; 62 void imag(float); 63 64 complex<float>& operator= (float); 65 complex<float>& operator+=(float); 66 complex<float>& operator-=(float); 67 complex<float>& operator*=(float); 68 complex<float>& operator/=(float); 69 70 complex<float>& operator=(const complex<float>&); 71 template<class X> complex<float>& operator= (const complex<X>&); 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}; 77 78template<> 79class complex<double> 80{ 81public: 82 typedef double value_type; 83 84 constexpr complex(double re = 0.0, double im = 0.0); 85 constexpr complex(const complex<float>&); 86 explicit constexpr complex(const complex<long double>&); 87 88 constexpr double real() const; 89 void real(double); 90 constexpr double imag() const; 91 void imag(double); 92 93 complex<double>& operator= (double); 94 complex<double>& operator+=(double); 95 complex<double>& operator-=(double); 96 complex<double>& operator*=(double); 97 complex<double>& operator/=(double); 98 complex<double>& operator=(const complex<double>&); 99 100 template<class X> complex<double>& operator= (const complex<X>&); 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}; 106 107template<> 108class complex<long double> 109{ 110public: 111 typedef long double value_type; 112 113 constexpr complex(long double re = 0.0L, long double im = 0.0L); 114 constexpr complex(const complex<float>&); 115 constexpr complex(const complex<double>&); 116 117 constexpr long double real() const; 118 void real(long double); 119 constexpr long double imag() const; 120 void imag(long double); 121 122 complex<long double>& operator=(const complex<long double>&); 123 complex<long double>& operator= (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 129 template<class X> complex<long double>& operator= (const complex<X>&); 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}; 135 136// 26.3.6 operators: 137template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); 138template<class T> complex<T> operator+(const complex<T>&, const T&); 139template<class T> complex<T> operator+(const T&, const complex<T>&); 140template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); 141template<class T> complex<T> operator-(const complex<T>&, const T&); 142template<class T> complex<T> operator-(const T&, const complex<T>&); 143template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); 144template<class T> complex<T> operator*(const complex<T>&, const T&); 145template<class T> complex<T> operator*(const T&, const complex<T>&); 146template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); 147template<class T> complex<T> operator/(const complex<T>&, const T&); 148template<class T> complex<T> operator/(const T&, const complex<T>&); 149template<class T> complex<T> operator+(const complex<T>&); 150template<class T> complex<T> operator-(const complex<T>&); 151template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14 152template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14 153template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14 154template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14 155template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14 156template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14 157 158template<class T, class charT, class traits> 159 basic_istream<charT, traits>& 160 operator>>(basic_istream<charT, traits>&, complex<T>&); 161template<class T, class charT, class traits> 162 basic_ostream<charT, traits>& 163 operator<<(basic_ostream<charT, traits>&, const complex<T>&); 164 165// 26.3.7 values: 166 167template<class T> T real(const complex<T>&); // constexpr in C++14 168 long double real(long double); // constexpr in C++14 169 double real(double); // constexpr in C++14 170template<Integral T> double real(T); // constexpr in C++14 171 float real(float); // constexpr in C++14 172 173template<class T> T imag(const complex<T>&); // constexpr in C++14 174 long double imag(long double); // constexpr in C++14 175 double imag(double); // constexpr in C++14 176template<Integral T> double imag(T); // constexpr in C++14 177 float imag(float); // constexpr in C++14 178 179template<class T> T abs(const complex<T>&); 180 181template<class T> T arg(const complex<T>&); 182 long double arg(long double); 183 double arg(double); 184template<Integral T> double arg(T); 185 float arg(float); 186 187template<class T> T norm(const complex<T>&); 188 long double norm(long double); 189 double norm(double); 190template<Integral T> double norm(T); 191 float norm(float); 192 193template<class T> complex<T> conj(const complex<T>&); 194 complex<long double> conj(long double); 195 complex<double> conj(double); 196template<Integral T> complex<double> conj(T); 197 complex<float> conj(float); 198 199template<class T> complex<T> proj(const complex<T>&); 200 complex<long double> proj(long double); 201 complex<double> proj(double); 202template<Integral T> complex<double> proj(T); 203 complex<float> proj(float); 204 205template<class T> complex<T> polar(const T&, const T& = T()); 206 207// 26.3.8 transcendentals: 208template<class T> complex<T> acos(const complex<T>&); 209template<class T> complex<T> asin(const complex<T>&); 210template<class T> complex<T> atan(const complex<T>&); 211template<class T> complex<T> acosh(const complex<T>&); 212template<class T> complex<T> asinh(const complex<T>&); 213template<class T> complex<T> atanh(const complex<T>&); 214template<class T> complex<T> cos (const complex<T>&); 215template<class T> complex<T> cosh (const complex<T>&); 216template<class T> complex<T> exp (const complex<T>&); 217template<class T> complex<T> log (const complex<T>&); 218template<class T> complex<T> log10(const complex<T>&); 219 220template<class T> complex<T> pow(const complex<T>&, const T&); 221template<class T> complex<T> pow(const complex<T>&, const complex<T>&); 222template<class T> complex<T> pow(const T&, const complex<T>&); 223 224template<class T> complex<T> sin (const complex<T>&); 225template<class T> complex<T> sinh (const complex<T>&); 226template<class T> complex<T> sqrt (const complex<T>&); 227template<class T> complex<T> tan (const complex<T>&); 228template<class T> complex<T> tanh (const complex<T>&); 229 230} // std 231 232*/ 233 234#include <__assert> // all public C++ headers provide the assertion handler 235#include <__config> 236#include <cmath> 237#include <iosfwd> 238#include <stdexcept> 239#include <type_traits> 240#include <version> 241 242#if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 243# include <sstream> // for std::basic_ostringstream 244#endif 245 246#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 247# pragma GCC system_header 248#endif 249 250_LIBCPP_BEGIN_NAMESPACE_STD 251 252template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex; 253 254template<class _Tp> complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w); 255template<class _Tp> complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y); 256 257template<class _Tp> 258class _LIBCPP_TEMPLATE_VIS complex 259{ 260public: 261 typedef _Tp value_type; 262private: 263 value_type __re_; 264 value_type __im_; 265public: 266 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 267 complex(const value_type& __re = value_type(), const value_type& __im = value_type()) 268 : __re_(__re), __im_(__im) {} 269 template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 270 complex(const complex<_Xp>& __c) 271 : __re_(__c.real()), __im_(__c.imag()) {} 272 273 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type real() const {return __re_;} 274 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 value_type imag() const {return __im_;} 275 276 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} 277 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} 278 279 _LIBCPP_INLINE_VISIBILITY complex& operator= (const value_type& __re) 280 {__re_ = __re; __im_ = value_type(); return *this;} 281 _LIBCPP_INLINE_VISIBILITY complex& operator+=(const value_type& __re) {__re_ += __re; return *this;} 282 _LIBCPP_INLINE_VISIBILITY complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;} 283 _LIBCPP_INLINE_VISIBILITY complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;} 284 _LIBCPP_INLINE_VISIBILITY complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;} 285 286 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) 287 { 288 __re_ = __c.real(); 289 __im_ = __c.imag(); 290 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 *this = *this * complex(__c.real(), __c.imag()); 307 return *this; 308 } 309 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) 310 { 311 *this = *this / complex(__c.real(), __c.imag()); 312 return *this; 313 } 314}; 315 316template<> class _LIBCPP_TEMPLATE_VIS complex<double>; 317template<> class _LIBCPP_TEMPLATE_VIS complex<long double>; 318 319template<> 320class _LIBCPP_TEMPLATE_VIS complex<float> 321{ 322 float __re_; 323 float __im_; 324public: 325 typedef float value_type; 326 327 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f) 328 : __re_(__re), __im_(__im) {} 329 _LIBCPP_INLINE_VISIBILITY 330 explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c); 331 _LIBCPP_INLINE_VISIBILITY 332 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); 333 334 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;} 335 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;} 336 337 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} 338 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} 339 340 _LIBCPP_INLINE_VISIBILITY complex& operator= (float __re) 341 {__re_ = __re; __im_ = value_type(); return *this;} 342 _LIBCPP_INLINE_VISIBILITY complex& operator+=(float __re) {__re_ += __re; return *this;} 343 _LIBCPP_INLINE_VISIBILITY complex& operator-=(float __re) {__re_ -= __re; return *this;} 344 _LIBCPP_INLINE_VISIBILITY complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;} 345 _LIBCPP_INLINE_VISIBILITY complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;} 346 347 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) 348 { 349 __re_ = __c.real(); 350 __im_ = __c.imag(); 351 return *this; 352 } 353 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator+=(const complex<_Xp>& __c) 354 { 355 __re_ += __c.real(); 356 __im_ += __c.imag(); 357 return *this; 358 } 359 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator-=(const complex<_Xp>& __c) 360 { 361 __re_ -= __c.real(); 362 __im_ -= __c.imag(); 363 return *this; 364 } 365 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator*=(const complex<_Xp>& __c) 366 { 367 *this = *this * complex(__c.real(), __c.imag()); 368 return *this; 369 } 370 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) 371 { 372 *this = *this / complex(__c.real(), __c.imag()); 373 return *this; 374 } 375}; 376 377template<> 378class _LIBCPP_TEMPLATE_VIS complex<double> 379{ 380 double __re_; 381 double __im_; 382public: 383 typedef double value_type; 384 385 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0) 386 : __re_(__re), __im_(__im) {} 387 _LIBCPP_INLINE_VISIBILITY 388 _LIBCPP_CONSTEXPR complex(const complex<float>& __c); 389 _LIBCPP_INLINE_VISIBILITY 390 explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c); 391 392 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;} 393 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;} 394 395 _LIBCPP_INLINE_VISIBILITY void real(value_type __re) {__re_ = __re;} 396 _LIBCPP_INLINE_VISIBILITY void imag(value_type __im) {__im_ = __im;} 397 398 _LIBCPP_INLINE_VISIBILITY complex& operator= (double __re) 399 {__re_ = __re; __im_ = value_type(); return *this;} 400 _LIBCPP_INLINE_VISIBILITY complex& operator+=(double __re) {__re_ += __re; return *this;} 401 _LIBCPP_INLINE_VISIBILITY complex& operator-=(double __re) {__re_ -= __re; return *this;} 402 _LIBCPP_INLINE_VISIBILITY complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;} 403 _LIBCPP_INLINE_VISIBILITY complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;} 404 405 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator= (const complex<_Xp>& __c) 406 { 407 __re_ = __c.real(); 408 __im_ = __c.imag(); 409 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 *this = *this * complex(__c.real(), __c.imag()); 426 return *this; 427 } 428 template<class _Xp> _LIBCPP_INLINE_VISIBILITY complex& operator/=(const complex<_Xp>& __c) 429 { 430 *this = *this / complex(__c.real(), __c.imag()); 431 return *this; 432 } 433}; 434 435template<> 436class _LIBCPP_TEMPLATE_VIS complex<long double> 437{ 438 long double __re_; 439 long double __im_; 440public: 441 typedef long double value_type; 442 443 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L) 444 : __re_(__re), __im_(__im) {} 445 _LIBCPP_INLINE_VISIBILITY 446 _LIBCPP_CONSTEXPR complex(const complex<float>& __c); 447 _LIBCPP_INLINE_VISIBILITY 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 494_LIBCPP_CONSTEXPR 495complex<float>::complex(const complex<double>& __c) 496 : __re_(__c.real()), __im_(__c.imag()) {} 497 498inline 499_LIBCPP_CONSTEXPR 500complex<float>::complex(const complex<long double>& __c) 501 : __re_(__c.real()), __im_(__c.imag()) {} 502 503inline 504_LIBCPP_CONSTEXPR 505complex<double>::complex(const complex<float>& __c) 506 : __re_(__c.real()), __im_(__c.imag()) {} 507 508inline 509_LIBCPP_CONSTEXPR 510complex<double>::complex(const complex<long double>& __c) 511 : __re_(__c.real()), __im_(__c.imag()) {} 512 513inline 514_LIBCPP_CONSTEXPR 515complex<long double>::complex(const complex<float>& __c) 516 : __re_(__c.real()), __im_(__c.imag()) {} 517 518inline 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 (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y)) 600 { 601 bool __recalc = false; 602 if (__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) 603 { 604 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a); 605 __b = copysign(__libcpp_isinf_or_builtin(__b) ? _Tp(1) : _Tp(0), __b); 606 if (__libcpp_isnan_or_builtin(__c)) 607 __c = copysign(_Tp(0), __c); 608 if (__libcpp_isnan_or_builtin(__d)) 609 __d = copysign(_Tp(0), __d); 610 __recalc = true; 611 } 612 if (__libcpp_isinf_or_builtin(__c) || __libcpp_isinf_or_builtin(__d)) 613 { 614 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c); 615 __d = copysign(__libcpp_isinf_or_builtin(__d) ? _Tp(1) : _Tp(0), __d); 616 if (__libcpp_isnan_or_builtin(__a)) 617 __a = copysign(_Tp(0), __a); 618 if (__libcpp_isnan_or_builtin(__b)) 619 __b = copysign(_Tp(0), __b); 620 __recalc = true; 621 } 622 if (!__recalc && (__libcpp_isinf_or_builtin(__ac) || __libcpp_isinf_or_builtin(__bd) || 623 __libcpp_isinf_or_builtin(__ad) || __libcpp_isinf_or_builtin(__bc))) 624 { 625 if (__libcpp_isnan_or_builtin(__a)) 626 __a = copysign(_Tp(0), __a); 627 if (__libcpp_isnan_or_builtin(__b)) 628 __b = copysign(_Tp(0), __b); 629 if (__libcpp_isnan_or_builtin(__c)) 630 __c = copysign(_Tp(0), __c); 631 if (__libcpp_isnan_or_builtin(__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 (__libcpp_isfinite_or_builtin(__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 (__libcpp_isnan_or_builtin(__x) && __libcpp_isnan_or_builtin(__y)) 684 { 685 if ((__denom == _Tp(0)) && (!__libcpp_isnan_or_builtin(__a) || !__libcpp_isnan_or_builtin(__b))) 686 { 687 __x = copysign(_Tp(INFINITY), __c) * __a; 688 __y = copysign(_Tp(INFINITY), __c) * __b; 689 } 690 else if ((__libcpp_isinf_or_builtin(__a) || __libcpp_isinf_or_builtin(__b)) && __libcpp_isfinite_or_builtin(__c) && __libcpp_isfinite_or_builtin(__d)) 691 { 692 __a = copysign(__libcpp_isinf_or_builtin(__a) ? _Tp(1) : _Tp(0), __a); 693 __b = copysign(__libcpp_isinf_or_builtin(__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 (__libcpp_isinf_or_builtin(__logbw) && __logbw > _Tp(0) && __libcpp_isfinite_or_builtin(__a) && __libcpp_isfinite_or_builtin(__b)) 698 { 699 __c = copysign(__libcpp_isinf_or_builtin(__c) ? _Tp(1) : _Tp(0), __c); 700 __d = copysign(__libcpp_isinf_or_builtin(__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 792template <class _Tp, bool = is_integral<_Tp>::value, 793 bool = is_floating_point<_Tp>::value 794 > 795struct __libcpp_complex_overload_traits {}; 796 797// Integral Types 798template <class _Tp> 799struct __libcpp_complex_overload_traits<_Tp, true, false> 800{ 801 typedef double _ValueType; 802 typedef complex<double> _ComplexType; 803}; 804 805// Floating point types 806template <class _Tp> 807struct __libcpp_complex_overload_traits<_Tp, false, true> 808{ 809 typedef _Tp _ValueType; 810 typedef complex<_Tp> _ComplexType; 811}; 812 813// real 814 815template<class _Tp> 816inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 817_Tp 818real(const complex<_Tp>& __c) 819{ 820 return __c.real(); 821} 822 823template <class _Tp> 824inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 825typename __libcpp_complex_overload_traits<_Tp>::_ValueType 826real(_Tp __re) 827{ 828 return __re; 829} 830 831// imag 832 833template<class _Tp> 834inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 835_Tp 836imag(const complex<_Tp>& __c) 837{ 838 return __c.imag(); 839} 840 841template <class _Tp> 842inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 843typename __libcpp_complex_overload_traits<_Tp>::_ValueType 844imag(_Tp) 845{ 846 return 0; 847} 848 849// abs 850 851template<class _Tp> 852inline _LIBCPP_INLINE_VISIBILITY 853_Tp 854abs(const complex<_Tp>& __c) 855{ 856 return hypot(__c.real(), __c.imag()); 857} 858 859// arg 860 861template<class _Tp> 862inline _LIBCPP_INLINE_VISIBILITY 863_Tp 864arg(const complex<_Tp>& __c) 865{ 866 return atan2(__c.imag(), __c.real()); 867} 868 869template <class _Tp> 870inline _LIBCPP_INLINE_VISIBILITY 871typename enable_if< 872 is_same<_Tp, long double>::value, 873 long double 874>::type 875arg(_Tp __re) 876{ 877 return atan2l(0.L, __re); 878} 879 880template<class _Tp> 881inline _LIBCPP_INLINE_VISIBILITY 882typename enable_if 883< 884 is_integral<_Tp>::value || is_same<_Tp, double>::value, 885 double 886>::type 887arg(_Tp __re) 888{ 889 return atan2(0., __re); 890} 891 892template <class _Tp> 893inline _LIBCPP_INLINE_VISIBILITY 894typename enable_if< 895 is_same<_Tp, float>::value, 896 float 897>::type 898arg(_Tp __re) 899{ 900 return atan2f(0.F, __re); 901} 902 903// norm 904 905template<class _Tp> 906inline _LIBCPP_INLINE_VISIBILITY 907_Tp 908norm(const complex<_Tp>& __c) 909{ 910 if (__libcpp_isinf_or_builtin(__c.real())) 911 return abs(__c.real()); 912 if (__libcpp_isinf_or_builtin(__c.imag())) 913 return abs(__c.imag()); 914 return __c.real() * __c.real() + __c.imag() * __c.imag(); 915} 916 917template <class _Tp> 918inline _LIBCPP_INLINE_VISIBILITY 919typename __libcpp_complex_overload_traits<_Tp>::_ValueType 920norm(_Tp __re) 921{ 922 typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType; 923 return static_cast<_ValueType>(__re) * __re; 924} 925 926// conj 927 928template<class _Tp> 929inline _LIBCPP_INLINE_VISIBILITY 930complex<_Tp> 931conj(const complex<_Tp>& __c) 932{ 933 return complex<_Tp>(__c.real(), -__c.imag()); 934} 935 936template <class _Tp> 937inline _LIBCPP_INLINE_VISIBILITY 938typename __libcpp_complex_overload_traits<_Tp>::_ComplexType 939conj(_Tp __re) 940{ 941 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; 942 return _ComplexType(__re); 943} 944 945 946 947// proj 948 949template<class _Tp> 950inline _LIBCPP_INLINE_VISIBILITY 951complex<_Tp> 952proj(const complex<_Tp>& __c) 953{ 954 complex<_Tp> __r = __c; 955 if (__libcpp_isinf_or_builtin(__c.real()) || __libcpp_isinf_or_builtin(__c.imag())) 956 __r = complex<_Tp>(INFINITY, copysign(_Tp(0), __c.imag())); 957 return __r; 958} 959 960template <class _Tp> 961inline _LIBCPP_INLINE_VISIBILITY 962typename enable_if 963< 964 is_floating_point<_Tp>::value, 965 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType 966>::type 967proj(_Tp __re) 968{ 969 if (__libcpp_isinf_or_builtin(__re)) 970 __re = abs(__re); 971 return complex<_Tp>(__re); 972} 973 974template <class _Tp> 975inline _LIBCPP_INLINE_VISIBILITY 976typename enable_if 977< 978 is_integral<_Tp>::value, 979 typename __libcpp_complex_overload_traits<_Tp>::_ComplexType 980>::type 981proj(_Tp __re) 982{ 983 typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType; 984 return _ComplexType(__re); 985} 986 987// polar 988 989template<class _Tp> 990complex<_Tp> 991polar(const _Tp& __rho, const _Tp& __theta = _Tp()) 992{ 993 if (__libcpp_isnan_or_builtin(__rho) || signbit(__rho)) 994 return complex<_Tp>(_Tp(NAN), _Tp(NAN)); 995 if (__libcpp_isnan_or_builtin(__theta)) 996 { 997 if (__libcpp_isinf_or_builtin(__rho)) 998 return complex<_Tp>(__rho, __theta); 999 return complex<_Tp>(__theta, __theta); 1000 } 1001 if (__libcpp_isinf_or_builtin(__theta)) 1002 { 1003 if (__libcpp_isinf_or_builtin(__rho)) 1004 return complex<_Tp>(__rho, _Tp(NAN)); 1005 return complex<_Tp>(_Tp(NAN), _Tp(NAN)); 1006 } 1007 _Tp __x = __rho * cos(__theta); 1008 if (__libcpp_isnan_or_builtin(__x)) 1009 __x = 0; 1010 _Tp __y = __rho * sin(__theta); 1011 if (__libcpp_isnan_or_builtin(__y)) 1012 __y = 0; 1013 return complex<_Tp>(__x, __y); 1014} 1015 1016// log 1017 1018template<class _Tp> 1019inline _LIBCPP_INLINE_VISIBILITY 1020complex<_Tp> 1021log(const complex<_Tp>& __x) 1022{ 1023 return complex<_Tp>(log(abs(__x)), arg(__x)); 1024} 1025 1026// log10 1027 1028template<class _Tp> 1029inline _LIBCPP_INLINE_VISIBILITY 1030complex<_Tp> 1031log10(const complex<_Tp>& __x) 1032{ 1033 return log(__x) / log(_Tp(10)); 1034} 1035 1036// sqrt 1037 1038template<class _Tp> 1039complex<_Tp> 1040sqrt(const complex<_Tp>& __x) 1041{ 1042 if (__libcpp_isinf_or_builtin(__x.imag())) 1043 return complex<_Tp>(_Tp(INFINITY), __x.imag()); 1044 if (__libcpp_isinf_or_builtin(__x.real())) 1045 { 1046 if (__x.real() > _Tp(0)) 1047 return complex<_Tp>(__x.real(), __libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : copysign(_Tp(0), __x.imag())); 1048 return complex<_Tp>(__libcpp_isnan_or_builtin(__x.imag()) ? __x.imag() : _Tp(0), copysign(__x.real(), __x.imag())); 1049 } 1050 return polar(sqrt(abs(__x)), arg(__x) / _Tp(2)); 1051} 1052 1053// exp 1054 1055template<class _Tp> 1056complex<_Tp> 1057exp(const complex<_Tp>& __x) 1058{ 1059 _Tp __i = __x.imag(); 1060 if (__i == 0) { 1061 return complex<_Tp>(exp(__x.real()), copysign(_Tp(0), __x.imag())); 1062 } 1063 if (__libcpp_isinf_or_builtin(__x.real())) 1064 { 1065 if (__x.real() < _Tp(0)) 1066 { 1067 if (!__libcpp_isfinite_or_builtin(__i)) 1068 __i = _Tp(1); 1069 } 1070 else if (__i == 0 || !__libcpp_isfinite_or_builtin(__i)) 1071 { 1072 if (__libcpp_isinf_or_builtin(__i)) 1073 __i = _Tp(NAN); 1074 return complex<_Tp>(__x.real(), __i); 1075 } 1076 } 1077 _Tp __e = exp(__x.real()); 1078 return complex<_Tp>(__e * cos(__i), __e * sin(__i)); 1079} 1080 1081// pow 1082 1083template<class _Tp> 1084inline _LIBCPP_INLINE_VISIBILITY 1085complex<_Tp> 1086pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1087{ 1088 return exp(__y * log(__x)); 1089} 1090 1091template<class _Tp, class _Up> 1092inline _LIBCPP_INLINE_VISIBILITY 1093complex<typename __promote<_Tp, _Up>::type> 1094pow(const complex<_Tp>& __x, const complex<_Up>& __y) 1095{ 1096 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1097 return _VSTD::pow(result_type(__x), result_type(__y)); 1098} 1099 1100template<class _Tp, class _Up> 1101inline _LIBCPP_INLINE_VISIBILITY 1102typename enable_if 1103< 1104 is_arithmetic<_Up>::value, 1105 complex<typename __promote<_Tp, _Up>::type> 1106>::type 1107pow(const complex<_Tp>& __x, const _Up& __y) 1108{ 1109 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1110 return _VSTD::pow(result_type(__x), result_type(__y)); 1111} 1112 1113template<class _Tp, class _Up> 1114inline _LIBCPP_INLINE_VISIBILITY 1115typename enable_if 1116< 1117 is_arithmetic<_Tp>::value, 1118 complex<typename __promote<_Tp, _Up>::type> 1119>::type 1120pow(const _Tp& __x, const complex<_Up>& __y) 1121{ 1122 typedef complex<typename __promote<_Tp, _Up>::type> result_type; 1123 return _VSTD::pow(result_type(__x), result_type(__y)); 1124} 1125 1126// __sqr, computes pow(x, 2) 1127 1128template<class _Tp> 1129inline _LIBCPP_INLINE_VISIBILITY 1130complex<_Tp> 1131__sqr(const complex<_Tp>& __x) 1132{ 1133 return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()), 1134 _Tp(2) * __x.real() * __x.imag()); 1135} 1136 1137// asinh 1138 1139template<class _Tp> 1140complex<_Tp> 1141asinh(const complex<_Tp>& __x) 1142{ 1143 const _Tp __pi(atan2(+0., -0.)); 1144 if (__libcpp_isinf_or_builtin(__x.real())) 1145 { 1146 if (__libcpp_isnan_or_builtin(__x.imag())) 1147 return __x; 1148 if (__libcpp_isinf_or_builtin(__x.imag())) 1149 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); 1150 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); 1151 } 1152 if (__libcpp_isnan_or_builtin(__x.real())) 1153 { 1154 if (__libcpp_isinf_or_builtin(__x.imag())) 1155 return complex<_Tp>(__x.imag(), __x.real()); 1156 if (__x.imag() == 0) 1157 return __x; 1158 return complex<_Tp>(__x.real(), __x.real()); 1159 } 1160 if (__libcpp_isinf_or_builtin(__x.imag())) 1161 return complex<_Tp>(copysign(__x.imag(), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1162 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) + _Tp(1))); 1163 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); 1164} 1165 1166// acosh 1167 1168template<class _Tp> 1169complex<_Tp> 1170acosh(const complex<_Tp>& __x) 1171{ 1172 const _Tp __pi(atan2(+0., -0.)); 1173 if (__libcpp_isinf_or_builtin(__x.real())) 1174 { 1175 if (__libcpp_isnan_or_builtin(__x.imag())) 1176 return complex<_Tp>(abs(__x.real()), __x.imag()); 1177 if (__libcpp_isinf_or_builtin(__x.imag())) 1178 { 1179 if (__x.real() > 0) 1180 return complex<_Tp>(__x.real(), copysign(__pi * _Tp(0.25), __x.imag())); 1181 else 1182 return complex<_Tp>(-__x.real(), copysign(__pi * _Tp(0.75), __x.imag())); 1183 } 1184 if (__x.real() < 0) 1185 return complex<_Tp>(-__x.real(), copysign(__pi, __x.imag())); 1186 return complex<_Tp>(__x.real(), copysign(_Tp(0), __x.imag())); 1187 } 1188 if (__libcpp_isnan_or_builtin(__x.real())) 1189 { 1190 if (__libcpp_isinf_or_builtin(__x.imag())) 1191 return complex<_Tp>(abs(__x.imag()), __x.real()); 1192 return complex<_Tp>(__x.real(), __x.real()); 1193 } 1194 if (__libcpp_isinf_or_builtin(__x.imag())) 1195 return complex<_Tp>(abs(__x.imag()), copysign(__pi/_Tp(2), __x.imag())); 1196 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1))); 1197 return complex<_Tp>(copysign(__z.real(), _Tp(0)), copysign(__z.imag(), __x.imag())); 1198} 1199 1200// atanh 1201 1202template<class _Tp> 1203complex<_Tp> 1204atanh(const complex<_Tp>& __x) 1205{ 1206 const _Tp __pi(atan2(+0., -0.)); 1207 if (__libcpp_isinf_or_builtin(__x.imag())) 1208 { 1209 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1210 } 1211 if (__libcpp_isnan_or_builtin(__x.imag())) 1212 { 1213 if (__libcpp_isinf_or_builtin(__x.real()) || __x.real() == 0) 1214 return complex<_Tp>(copysign(_Tp(0), __x.real()), __x.imag()); 1215 return complex<_Tp>(__x.imag(), __x.imag()); 1216 } 1217 if (__libcpp_isnan_or_builtin(__x.real())) 1218 { 1219 return complex<_Tp>(__x.real(), __x.real()); 1220 } 1221 if (__libcpp_isinf_or_builtin(__x.real())) 1222 { 1223 return complex<_Tp>(copysign(_Tp(0), __x.real()), copysign(__pi/_Tp(2), __x.imag())); 1224 } 1225 if (abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0)) 1226 { 1227 return complex<_Tp>(copysign(_Tp(INFINITY), __x.real()), copysign(_Tp(0), __x.imag())); 1228 } 1229 complex<_Tp> __z = log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2); 1230 return complex<_Tp>(copysign(__z.real(), __x.real()), copysign(__z.imag(), __x.imag())); 1231} 1232 1233// sinh 1234 1235template<class _Tp> 1236complex<_Tp> 1237sinh(const complex<_Tp>& __x) 1238{ 1239 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag())) 1240 return complex<_Tp>(__x.real(), _Tp(NAN)); 1241 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag())) 1242 return complex<_Tp>(__x.real(), _Tp(NAN)); 1243 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real())) 1244 return __x; 1245 return complex<_Tp>(sinh(__x.real()) * cos(__x.imag()), cosh(__x.real()) * sin(__x.imag())); 1246} 1247 1248// cosh 1249 1250template<class _Tp> 1251complex<_Tp> 1252cosh(const complex<_Tp>& __x) 1253{ 1254 if (__libcpp_isinf_or_builtin(__x.real()) && !__libcpp_isfinite_or_builtin(__x.imag())) 1255 return complex<_Tp>(abs(__x.real()), _Tp(NAN)); 1256 if (__x.real() == 0 && !__libcpp_isfinite_or_builtin(__x.imag())) 1257 return complex<_Tp>(_Tp(NAN), __x.real()); 1258 if (__x.real() == 0 && __x.imag() == 0) 1259 return complex<_Tp>(_Tp(1), __x.imag()); 1260 if (__x.imag() == 0 && !__libcpp_isfinite_or_builtin(__x.real())) 1261 return complex<_Tp>(abs(__x.real()), __x.imag()); 1262 return complex<_Tp>(cosh(__x.real()) * cos(__x.imag()), sinh(__x.real()) * sin(__x.imag())); 1263} 1264 1265// tanh 1266 1267template<class _Tp> 1268complex<_Tp> 1269tanh(const complex<_Tp>& __x) 1270{ 1271 if (__libcpp_isinf_or_builtin(__x.real())) 1272 { 1273 if (!__libcpp_isfinite_or_builtin(__x.imag())) 1274 return complex<_Tp>(copysign(_Tp(1), __x.real()), _Tp(0)); 1275 return complex<_Tp>(copysign(_Tp(1), __x.real()), copysign(_Tp(0), sin(_Tp(2) * __x.imag()))); 1276 } 1277 if (__libcpp_isnan_or_builtin(__x.real()) && __x.imag() == 0) 1278 return __x; 1279 _Tp __2r(_Tp(2) * __x.real()); 1280 _Tp __2i(_Tp(2) * __x.imag()); 1281 _Tp __d(cosh(__2r) + cos(__2i)); 1282 _Tp __2rsh(sinh(__2r)); 1283 if (__libcpp_isinf_or_builtin(__2rsh) && __libcpp_isinf_or_builtin(__d)) 1284 return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1), 1285 __2i > _Tp(0) ? _Tp(0) : _Tp(-0.)); 1286 return complex<_Tp>(__2rsh/__d, sin(__2i)/__d); 1287} 1288 1289// asin 1290 1291template<class _Tp> 1292complex<_Tp> 1293asin(const complex<_Tp>& __x) 1294{ 1295 complex<_Tp> __z = asinh(complex<_Tp>(-__x.imag(), __x.real())); 1296 return complex<_Tp>(__z.imag(), -__z.real()); 1297} 1298 1299// acos 1300 1301template<class _Tp> 1302complex<_Tp> 1303acos(const complex<_Tp>& __x) 1304{ 1305 const _Tp __pi(atan2(+0., -0.)); 1306 if (__libcpp_isinf_or_builtin(__x.real())) 1307 { 1308 if (__libcpp_isnan_or_builtin(__x.imag())) 1309 return complex<_Tp>(__x.imag(), __x.real()); 1310 if (__libcpp_isinf_or_builtin(__x.imag())) 1311 { 1312 if (__x.real() < _Tp(0)) 1313 return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag()); 1314 return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag()); 1315 } 1316 if (__x.real() < _Tp(0)) 1317 return complex<_Tp>(__pi, signbit(__x.imag()) ? -__x.real() : __x.real()); 1318 return complex<_Tp>(_Tp(0), signbit(__x.imag()) ? __x.real() : -__x.real()); 1319 } 1320 if (__libcpp_isnan_or_builtin(__x.real())) 1321 { 1322 if (__libcpp_isinf_or_builtin(__x.imag())) 1323 return complex<_Tp>(__x.real(), -__x.imag()); 1324 return complex<_Tp>(__x.real(), __x.real()); 1325 } 1326 if (__libcpp_isinf_or_builtin(__x.imag())) 1327 return complex<_Tp>(__pi/_Tp(2), -__x.imag()); 1328 if (__x.real() == 0 && (__x.imag() == 0 || isnan(__x.imag()))) 1329 return complex<_Tp>(__pi/_Tp(2), -__x.imag()); 1330 complex<_Tp> __z = log(__x + sqrt(__sqr(__x) - _Tp(1))); 1331 if (signbit(__x.imag())) 1332 return complex<_Tp>(abs(__z.imag()), abs(__z.real())); 1333 return complex<_Tp>(abs(__z.imag()), -abs(__z.real())); 1334} 1335 1336// atan 1337 1338template<class _Tp> 1339complex<_Tp> 1340atan(const complex<_Tp>& __x) 1341{ 1342 complex<_Tp> __z = atanh(complex<_Tp>(-__x.imag(), __x.real())); 1343 return complex<_Tp>(__z.imag(), -__z.real()); 1344} 1345 1346// sin 1347 1348template<class _Tp> 1349complex<_Tp> 1350sin(const complex<_Tp>& __x) 1351{ 1352 complex<_Tp> __z = sinh(complex<_Tp>(-__x.imag(), __x.real())); 1353 return complex<_Tp>(__z.imag(), -__z.real()); 1354} 1355 1356// cos 1357 1358template<class _Tp> 1359inline _LIBCPP_INLINE_VISIBILITY 1360complex<_Tp> 1361cos(const complex<_Tp>& __x) 1362{ 1363 return cosh(complex<_Tp>(-__x.imag(), __x.real())); 1364} 1365 1366// tan 1367 1368template<class _Tp> 1369complex<_Tp> 1370tan(const complex<_Tp>& __x) 1371{ 1372 complex<_Tp> __z = tanh(complex<_Tp>(-__x.imag(), __x.real())); 1373 return complex<_Tp>(__z.imag(), -__z.real()); 1374} 1375 1376template<class _Tp, class _CharT, class _Traits> 1377basic_istream<_CharT, _Traits>& 1378operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 1379{ 1380 if (__is.good()) 1381 { 1382 ws(__is); 1383 if (__is.peek() == _CharT('(')) 1384 { 1385 __is.get(); 1386 _Tp __r; 1387 __is >> __r; 1388 if (!__is.fail()) 1389 { 1390 ws(__is); 1391 _CharT __c = __is.peek(); 1392 if (__c == _CharT(',')) 1393 { 1394 __is.get(); 1395 _Tp __i; 1396 __is >> __i; 1397 if (!__is.fail()) 1398 { 1399 ws(__is); 1400 __c = __is.peek(); 1401 if (__c == _CharT(')')) 1402 { 1403 __is.get(); 1404 __x = complex<_Tp>(__r, __i); 1405 } 1406 else 1407 __is.setstate(__is.failbit); 1408 } 1409 else 1410 __is.setstate(__is.failbit); 1411 } 1412 else if (__c == _CharT(')')) 1413 { 1414 __is.get(); 1415 __x = complex<_Tp>(__r, _Tp(0)); 1416 } 1417 else 1418 __is.setstate(__is.failbit); 1419 } 1420 else 1421 __is.setstate(__is.failbit); 1422 } 1423 else 1424 { 1425 _Tp __r; 1426 __is >> __r; 1427 if (!__is.fail()) 1428 __x = complex<_Tp>(__r, _Tp(0)); 1429 else 1430 __is.setstate(__is.failbit); 1431 } 1432 } 1433 else 1434 __is.setstate(__is.failbit); 1435 return __is; 1436} 1437 1438#if !defined(_LIBCPP_HAS_NO_LOCALIZATION) 1439template<class _Tp, class _CharT, class _Traits> 1440basic_ostream<_CharT, _Traits>& 1441operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 1442{ 1443 basic_ostringstream<_CharT, _Traits> __s; 1444 __s.flags(__os.flags()); 1445 __s.imbue(__os.getloc()); 1446 __s.precision(__os.precision()); 1447 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 1448 return __os << __s.str(); 1449} 1450#endif // !_LIBCPP_HAS_NO_LOCALIZATION 1451 1452#if _LIBCPP_STD_VER > 11 1453// Literal suffix for complex number literals [complex.literals] 1454inline namespace literals 1455{ 1456 inline namespace complex_literals 1457 { 1458 constexpr complex<long double> operator""il(long double __im) 1459 { 1460 return { 0.0l, __im }; 1461 } 1462 1463 constexpr complex<long double> operator""il(unsigned long long __im) 1464 { 1465 return { 0.0l, static_cast<long double>(__im) }; 1466 } 1467 1468 1469 constexpr complex<double> operator""i(long double __im) 1470 { 1471 return { 0.0, static_cast<double>(__im) }; 1472 } 1473 1474 constexpr complex<double> operator""i(unsigned long long __im) 1475 { 1476 return { 0.0, static_cast<double>(__im) }; 1477 } 1478 1479 1480 constexpr complex<float> operator""if(long double __im) 1481 { 1482 return { 0.0f, static_cast<float>(__im) }; 1483 } 1484 1485 constexpr complex<float> operator""if(unsigned long long __im) 1486 { 1487 return { 0.0f, static_cast<float>(__im) }; 1488 } 1489 } // namespace complex_literals 1490} // namespace literals 1491#endif 1492 1493_LIBCPP_END_NAMESPACE_STD 1494 1495#endif // _LIBCPP_COMPLEX 1496