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