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