1// The template and inlines for the -*- C++ -*- complex number classes. 2 3// Copyright (C) 1997-2022 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25/** @file include/complex 26 * This is a Standard C++ Library header. 27 */ 28 29// 30// ISO C++ 14882: 26.2 Complex Numbers 31// Note: this is not a conforming implementation. 32// Initially implemented by Ulrich Drepper <drepper@cygnus.com> 33// Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> 34// 35 36#ifndef _GLIBCXX_COMPLEX 37#define _GLIBCXX_COMPLEX 1 38 39#pragma GCC system_header 40 41#include <bits/c++config.h> 42#include <bits/cpp_type_traits.h> 43#include <ext/type_traits.h> 44#include <cmath> 45#include <sstream> 46 47#if _GLIBCXX_USE_C99_COMPLEX 48// This is disgusting; we can't include ccomplex because that requires c++11 49// and we can't use the builtins because those point to the wrong 50// ABI-wise cabs/cabsf so we manually declare those here and use 51// them directly. 52extern "C" float __c99_cabsf(_Complex float); 53extern "C" double __c99_cabs(_Complex double); 54extern "C" long double __c99_cabsl(_Complex long double); 55#endif 56 57// Get rid of a macro possibly defined in <complex.h> 58#undef complex 59 60#if __cplusplus > 201703L 61# define __cpp_lib_constexpr_complex 201711L 62#endif 63 64namespace std _GLIBCXX_VISIBILITY(default) 65{ 66_GLIBCXX_BEGIN_NAMESPACE_VERSION 67 68 /** 69 * @defgroup complex_numbers Complex Numbers 70 * @ingroup numerics 71 * 72 * Classes and functions for complex numbers. 73 * @{ 74 */ 75 76 // Forward declarations. 77 template<typename _Tp> class complex; 78 template<> class complex<float>; 79 template<> class complex<double>; 80 template<> class complex<long double>; 81 82 /// Return magnitude of @a z. 83 template<typename _Tp> _Tp abs(const complex<_Tp>&); 84 /// Return phase angle of @a z. 85 template<typename _Tp> _Tp arg(const complex<_Tp>&); 86 /// Return @a z magnitude squared. 87 template<typename _Tp> _Tp _GLIBCXX20_CONSTEXPR norm(const complex<_Tp>&); 88 89 /// Return complex conjugate of @a z. 90 template<typename _Tp> 91 _GLIBCXX20_CONSTEXPR complex<_Tp> conj(const complex<_Tp>&); 92 /// Return complex with magnitude @a rho and angle @a theta. 93 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 94 95 // Transcendentals: 96 /// Return complex cosine of @a z. 97 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 98 /// Return complex hyperbolic cosine of @a z. 99 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 100 /// Return complex base e exponential of @a z. 101 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 102 /// Return complex natural logarithm of @a z. 103 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 104 /// Return complex base 10 logarithm of @a z. 105 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 106 /// Return @a x to the @a y'th power. 107 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 108 /// Return @a x to the @a y'th power. 109 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 110 /// Return @a x to the @a y'th power. 111 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 112 const complex<_Tp>&); 113 /// Return @a x to the @a y'th power. 114 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 115 /// Return complex sine of @a z. 116 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 117 /// Return complex hyperbolic sine of @a z. 118 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 119 /// Return complex square root of @a z. 120 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 121 /// Return complex tangent of @a z. 122 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 123 /// Return complex hyperbolic tangent of @a z. 124 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 125 126 127 // 26.2.2 Primary template class complex 128 /** 129 * Template to represent complex numbers. 130 * 131 * Specializations for float, double, and long double are part of the 132 * library. Results with any other type are not guaranteed. 133 * 134 * @param Tp Type of real and imaginary values. 135 */ 136 template<typename _Tp> 137 class complex 138 { 139 public: 140 /// Value typedef. 141 typedef _Tp value_type; 142 143 /// Default constructor. First parameter is x, second parameter is y. 144 /// Unspecified parameters default to 0. 145 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) 146 : _M_real(__r), _M_imag(__i) { } 147 148 // Let the compiler synthesize the copy constructor 149#if __cplusplus >= 201103L 150 constexpr complex(const complex&) = default; 151#endif 152 153 /// Converting constructor. 154 template<typename _Up> 155 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) 156 : _M_real(__z.real()), _M_imag(__z.imag()) { } 157 158#if __cplusplus >= 201103L 159 // _GLIBCXX_RESOLVE_LIB_DEFECTS 160 // DR 387. std::complex over-encapsulated. 161 _GLIBCXX_ABI_TAG_CXX11 162 constexpr _Tp 163 real() const { return _M_real; } 164 165 _GLIBCXX_ABI_TAG_CXX11 166 constexpr _Tp 167 imag() const { return _M_imag; } 168#else 169 /// Return real part of complex number. 170 _Tp& 171 real() { return _M_real; } 172 173 /// Return real part of complex number. 174 const _Tp& 175 real() const { return _M_real; } 176 177 /// Return imaginary part of complex number. 178 _Tp& 179 imag() { return _M_imag; } 180 181 /// Return imaginary part of complex number. 182 const _Tp& 183 imag() const { return _M_imag; } 184#endif 185 186 // _GLIBCXX_RESOLVE_LIB_DEFECTS 187 // DR 387. std::complex over-encapsulated. 188 _GLIBCXX20_CONSTEXPR void 189 real(_Tp __val) { _M_real = __val; } 190 191 _GLIBCXX20_CONSTEXPR void 192 imag(_Tp __val) { _M_imag = __val; } 193 194 /// Assign a scalar to this complex number. 195 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const _Tp&); 196 197 /// Add a scalar to this complex number. 198 // 26.2.5/1 199 _GLIBCXX20_CONSTEXPR complex<_Tp>& 200 operator+=(const _Tp& __t) 201 { 202 _M_real += __t; 203 return *this; 204 } 205 206 /// Subtract a scalar from this complex number. 207 // 26.2.5/3 208 _GLIBCXX20_CONSTEXPR complex<_Tp>& 209 operator-=(const _Tp& __t) 210 { 211 _M_real -= __t; 212 return *this; 213 } 214 215 /// Multiply this complex number by a scalar. 216 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const _Tp&); 217 /// Divide this complex number by a scalar. 218 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const _Tp&); 219 220 // Let the compiler synthesize the copy assignment operator 221#if __cplusplus >= 201103L 222 _GLIBCXX20_CONSTEXPR complex& operator=(const complex&) = default; 223#endif 224 225 /// Assign another complex number to this one. 226 template<typename _Up> 227 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator=(const complex<_Up>&); 228 /// Add another complex number to this one. 229 template<typename _Up> 230 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator+=(const complex<_Up>&); 231 /// Subtract another complex number from this one. 232 template<typename _Up> 233 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator-=(const complex<_Up>&); 234 /// Multiply this complex number by another. 235 template<typename _Up> 236 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator*=(const complex<_Up>&); 237 /// Divide this complex number by another. 238 template<typename _Up> 239 _GLIBCXX20_CONSTEXPR complex<_Tp>& operator/=(const complex<_Up>&); 240 241 _GLIBCXX_CONSTEXPR complex __rep() const 242 { return *this; } 243 244 private: 245 _Tp _M_real; 246 _Tp _M_imag; 247 }; 248 249 template<typename _Tp> 250 _GLIBCXX20_CONSTEXPR complex<_Tp>& 251 complex<_Tp>::operator=(const _Tp& __t) 252 { 253 _M_real = __t; 254 _M_imag = _Tp(); 255 return *this; 256 } 257 258 // 26.2.5/5 259 template<typename _Tp> 260 _GLIBCXX20_CONSTEXPR complex<_Tp>& 261 complex<_Tp>::operator*=(const _Tp& __t) 262 { 263 _M_real *= __t; 264 _M_imag *= __t; 265 return *this; 266 } 267 268 // 26.2.5/7 269 template<typename _Tp> 270 _GLIBCXX20_CONSTEXPR complex<_Tp>& 271 complex<_Tp>::operator/=(const _Tp& __t) 272 { 273 _M_real /= __t; 274 _M_imag /= __t; 275 return *this; 276 } 277 278 template<typename _Tp> 279 template<typename _Up> 280 _GLIBCXX20_CONSTEXPR complex<_Tp>& 281 complex<_Tp>::operator=(const complex<_Up>& __z) 282 { 283 _M_real = __z.real(); 284 _M_imag = __z.imag(); 285 return *this; 286 } 287 288 // 26.2.5/9 289 template<typename _Tp> 290 template<typename _Up> 291 _GLIBCXX20_CONSTEXPR complex<_Tp>& 292 complex<_Tp>::operator+=(const complex<_Up>& __z) 293 { 294 _M_real += __z.real(); 295 _M_imag += __z.imag(); 296 return *this; 297 } 298 299 // 26.2.5/11 300 template<typename _Tp> 301 template<typename _Up> 302 _GLIBCXX20_CONSTEXPR complex<_Tp>& 303 complex<_Tp>::operator-=(const complex<_Up>& __z) 304 { 305 _M_real -= __z.real(); 306 _M_imag -= __z.imag(); 307 return *this; 308 } 309 310 // 26.2.5/13 311 // XXX: This is a grammar school implementation. 312 template<typename _Tp> 313 template<typename _Up> 314 _GLIBCXX20_CONSTEXPR complex<_Tp>& 315 complex<_Tp>::operator*=(const complex<_Up>& __z) 316 { 317 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 318 _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 319 _M_real = __r; 320 return *this; 321 } 322 323 // 26.2.5/15 324 // XXX: This is a grammar school implementation. 325 template<typename _Tp> 326 template<typename _Up> 327 _GLIBCXX20_CONSTEXPR complex<_Tp>& 328 complex<_Tp>::operator/=(const complex<_Up>& __z) 329 { 330 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 331 const _Tp __n = std::norm(__z); 332 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 333 _M_real = __r / __n; 334 return *this; 335 } 336 337 // Operators: 338 ///@{ 339 /// Return new complex value @a x plus @a y. 340 template<typename _Tp> 341 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 342 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 343 { 344 complex<_Tp> __r = __x; 345 __r += __y; 346 return __r; 347 } 348 349 template<typename _Tp> 350 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 351 operator+(const complex<_Tp>& __x, const _Tp& __y) 352 { 353 complex<_Tp> __r = __x; 354 __r += __y; 355 return __r; 356 } 357 358 template<typename _Tp> 359 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 360 operator+(const _Tp& __x, const complex<_Tp>& __y) 361 { 362 complex<_Tp> __r = __y; 363 __r += __x; 364 return __r; 365 } 366 ///@} 367 368 ///@{ 369 /// Return new complex value @a x minus @a y. 370 template<typename _Tp> 371 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 372 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 373 { 374 complex<_Tp> __r = __x; 375 __r -= __y; 376 return __r; 377 } 378 379 template<typename _Tp> 380 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 381 operator-(const complex<_Tp>& __x, const _Tp& __y) 382 { 383 complex<_Tp> __r = __x; 384 __r -= __y; 385 return __r; 386 } 387 388 template<typename _Tp> 389 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 390 operator-(const _Tp& __x, const complex<_Tp>& __y) 391 { 392 complex<_Tp> __r = -__y; 393 __r += __x; 394 return __r; 395 } 396 ///@} 397 398 ///@{ 399 /// Return new complex value @a x times @a y. 400 template<typename _Tp> 401 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 402 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 403 { 404 complex<_Tp> __r = __x; 405 __r *= __y; 406 return __r; 407 } 408 409 template<typename _Tp> 410 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 411 operator*(const complex<_Tp>& __x, const _Tp& __y) 412 { 413 complex<_Tp> __r = __x; 414 __r *= __y; 415 return __r; 416 } 417 418 template<typename _Tp> 419 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 420 operator*(const _Tp& __x, const complex<_Tp>& __y) 421 { 422 complex<_Tp> __r = __y; 423 __r *= __x; 424 return __r; 425 } 426 ///@} 427 428 ///@{ 429 /// Return new complex value @a x divided by @a y. 430 template<typename _Tp> 431 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 432 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 433 { 434 complex<_Tp> __r = __x; 435 __r /= __y; 436 return __r; 437 } 438 439 template<typename _Tp> 440 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 441 operator/(const complex<_Tp>& __x, const _Tp& __y) 442 { 443 complex<_Tp> __r = __x; 444 __r /= __y; 445 return __r; 446 } 447 448 template<typename _Tp> 449 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 450 operator/(const _Tp& __x, const complex<_Tp>& __y) 451 { 452 complex<_Tp> __r = __x; 453 __r /= __y; 454 return __r; 455 } 456 ///@} 457 458 /// Return @a x. 459 template<typename _Tp> 460 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 461 operator+(const complex<_Tp>& __x) 462 { return __x; } 463 464 /// Return complex negation of @a x. 465 template<typename _Tp> 466 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 467 operator-(const complex<_Tp>& __x) 468 { return complex<_Tp>(-__x.real(), -__x.imag()); } 469 470 ///@{ 471 /// Return true if @a x is equal to @a y. 472 template<typename _Tp> 473 inline _GLIBCXX_CONSTEXPR bool 474 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 475 { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 476 477 template<typename _Tp> 478 inline _GLIBCXX_CONSTEXPR bool 479 operator==(const complex<_Tp>& __x, const _Tp& __y) 480 { return __x.real() == __y && __x.imag() == _Tp(); } 481 482#if !(__cpp_impl_three_way_comparison >= 201907L) 483 template<typename _Tp> 484 inline _GLIBCXX_CONSTEXPR bool 485 operator==(const _Tp& __x, const complex<_Tp>& __y) 486 { return __x == __y.real() && _Tp() == __y.imag(); } 487 ///@} 488 489 ///@{ 490 /// Return false if @a x is equal to @a y. 491 template<typename _Tp> 492 inline _GLIBCXX_CONSTEXPR bool 493 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) 494 { return __x.real() != __y.real() || __x.imag() != __y.imag(); } 495 496 template<typename _Tp> 497 inline _GLIBCXX_CONSTEXPR bool 498 operator!=(const complex<_Tp>& __x, const _Tp& __y) 499 { return __x.real() != __y || __x.imag() != _Tp(); } 500 501 template<typename _Tp> 502 inline _GLIBCXX_CONSTEXPR bool 503 operator!=(const _Tp& __x, const complex<_Tp>& __y) 504 { return __x != __y.real() || _Tp() != __y.imag(); } 505#endif 506 ///@} 507 508 /// Extraction operator for complex values. 509 template<typename _Tp, typename _CharT, class _Traits> 510 basic_istream<_CharT, _Traits>& 511 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 512 { 513 bool __fail = true; 514 _CharT __ch; 515 if (__is >> __ch) 516 { 517 if (_Traits::eq(__ch, __is.widen('('))) 518 { 519 _Tp __u; 520 if (__is >> __u >> __ch) 521 { 522 const _CharT __rparen = __is.widen(')'); 523 if (_Traits::eq(__ch, __rparen)) 524 { 525 __x = __u; 526 __fail = false; 527 } 528 else if (_Traits::eq(__ch, __is.widen(','))) 529 { 530 _Tp __v; 531 if (__is >> __v >> __ch) 532 { 533 if (_Traits::eq(__ch, __rparen)) 534 { 535 __x = complex<_Tp>(__u, __v); 536 __fail = false; 537 } 538 else 539 __is.putback(__ch); 540 } 541 } 542 else 543 __is.putback(__ch); 544 } 545 } 546 else 547 { 548 __is.putback(__ch); 549 _Tp __u; 550 if (__is >> __u) 551 { 552 __x = __u; 553 __fail = false; 554 } 555 } 556 } 557 if (__fail) 558 __is.setstate(ios_base::failbit); 559 return __is; 560 } 561 562 /// Insertion operator for complex values. 563 template<typename _Tp, typename _CharT, class _Traits> 564 basic_ostream<_CharT, _Traits>& 565 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 566 { 567 basic_ostringstream<_CharT, _Traits> __s; 568 __s.flags(__os.flags()); 569 __s.imbue(__os.getloc()); 570 __s.precision(__os.precision()); 571 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 572 return __os << __s.str(); 573 } 574 575 // Values 576#if __cplusplus >= 201103L 577 template<typename _Tp> 578 constexpr _Tp 579 real(const complex<_Tp>& __z) 580 { return __z.real(); } 581 582 template<typename _Tp> 583 constexpr _Tp 584 imag(const complex<_Tp>& __z) 585 { return __z.imag(); } 586#else 587 template<typename _Tp> 588 inline _Tp& 589 real(complex<_Tp>& __z) 590 { return __z.real(); } 591 592 template<typename _Tp> 593 inline const _Tp& 594 real(const complex<_Tp>& __z) 595 { return __z.real(); } 596 597 template<typename _Tp> 598 inline _Tp& 599 imag(complex<_Tp>& __z) 600 { return __z.imag(); } 601 602 template<typename _Tp> 603 inline const _Tp& 604 imag(const complex<_Tp>& __z) 605 { return __z.imag(); } 606#endif 607 608 // 26.2.7/3 abs(__z): Returns the magnitude of __z. 609 template<typename _Tp> 610 inline _Tp 611 __complex_abs(const complex<_Tp>& __z) 612 { 613 _Tp __x = __z.real(); 614 _Tp __y = __z.imag(); 615 const _Tp __s = std::max(abs(__x), abs(__y)); 616 if (__s == _Tp()) // well ... 617 return __s; 618 __x /= __s; 619 __y /= __s; 620 return __s * sqrt(__x * __x + __y * __y); 621 } 622 623#if _GLIBCXX_USE_C99_COMPLEX 624 // XXX: We can't use __builtin_cabs* because they are broken 625 inline float 626 __complex_abs(__complex__ float __z) { return __c99_cabsf(__z); } 627 628 inline double 629 __complex_abs(__complex__ double __z) { return __c99_cabs(__z); } 630 631 inline long double 632 __complex_abs(const __complex__ long double& __z) 633 { return __c99_cabsl(__z); } 634 635 template<typename _Tp> 636 inline _Tp 637 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 638#else 639 template<typename _Tp> 640 inline _Tp 641 abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 642#endif 643 644 645 // 26.2.7/4: arg(__z): Returns the phase angle of __z. 646 template<typename _Tp> 647 inline _Tp 648 __complex_arg(const complex<_Tp>& __z) 649 { return atan2(__z.imag(), __z.real()); } 650 651#if _GLIBCXX_USE_C99_COMPLEX 652 inline float 653 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 654 655 inline double 656 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 657 658 inline long double 659 __complex_arg(const __complex__ long double& __z) 660 { return __builtin_cargl(__z); } 661 662 template<typename _Tp> 663 inline _Tp 664 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 665#else 666 template<typename _Tp> 667 inline _Tp 668 arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 669#endif 670 671 // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 672 // As defined, norm() is -not- a norm is the common mathematical 673 // sense used in numerics. The helper class _Norm_helper<> tries to 674 // distinguish between builtin floating point and the rest, so as 675 // to deliver an answer as close as possible to the real value. 676 template<bool> 677 struct _Norm_helper 678 { 679 template<typename _Tp> 680 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z) 681 { 682 const _Tp __x = __z.real(); 683 const _Tp __y = __z.imag(); 684 return __x * __x + __y * __y; 685 } 686 }; 687 688 template<> 689 struct _Norm_helper<true> 690 { 691 template<typename _Tp> 692 static inline _GLIBCXX20_CONSTEXPR _Tp _S_do_it(const complex<_Tp>& __z) 693 { 694 //_Tp __res = std::abs(__z); 695 //return __res * __res; 696 const _Tp __x = __z.real(); 697 const _Tp __y = __z.imag(); 698 return __x * __x + __y * __y; 699 } 700 }; 701 702 template<typename _Tp> 703 inline _GLIBCXX20_CONSTEXPR _Tp 704 norm(const complex<_Tp>& __z) 705 { 706 return _Norm_helper<__is_floating<_Tp>::__value 707 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 708 } 709 710 template<typename _Tp> 711 inline complex<_Tp> 712 polar(const _Tp& __rho, const _Tp& __theta) 713 { 714 __glibcxx_assert( __rho >= 0 ); 715 return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); 716 } 717 718 template<typename _Tp> 719 inline _GLIBCXX20_CONSTEXPR complex<_Tp> 720 conj(const complex<_Tp>& __z) 721 { return complex<_Tp>(__z.real(), -__z.imag()); } 722 723 // Transcendentals 724 725 // 26.2.8/1 cos(__z): Returns the cosine of __z. 726 template<typename _Tp> 727 inline complex<_Tp> 728 __complex_cos(const complex<_Tp>& __z) 729 { 730 const _Tp __x = __z.real(); 731 const _Tp __y = __z.imag(); 732 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 733 } 734 735#if _GLIBCXX_USE_C99_COMPLEX 736 inline __complex__ float 737 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 738 739 inline __complex__ double 740 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 741 742 inline __complex__ long double 743 __complex_cos(const __complex__ long double& __z) 744 { return __builtin_ccosl(__z); } 745 746 template<typename _Tp> 747 inline complex<_Tp> 748 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 749#else 750 template<typename _Tp> 751 inline complex<_Tp> 752 cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 753#endif 754 755 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 756 template<typename _Tp> 757 inline complex<_Tp> 758 __complex_cosh(const complex<_Tp>& __z) 759 { 760 const _Tp __x = __z.real(); 761 const _Tp __y = __z.imag(); 762 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 763 } 764 765#if _GLIBCXX_USE_C99_COMPLEX 766 inline __complex__ float 767 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 768 769 inline __complex__ double 770 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 771 772 inline __complex__ long double 773 __complex_cosh(const __complex__ long double& __z) 774 { return __builtin_ccoshl(__z); } 775 776 template<typename _Tp> 777 inline complex<_Tp> 778 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 779#else 780 template<typename _Tp> 781 inline complex<_Tp> 782 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 783#endif 784 785 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 786 template<typename _Tp> 787 inline complex<_Tp> 788 __complex_exp(const complex<_Tp>& __z) 789 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } 790 791#if _GLIBCXX_USE_C99_COMPLEX 792 inline __complex__ float 793 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 794 795 inline __complex__ double 796 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 797 798 inline __complex__ long double 799 __complex_exp(const __complex__ long double& __z) 800 { return __builtin_cexpl(__z); } 801 802 template<typename _Tp> 803 inline complex<_Tp> 804 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 805#else 806 template<typename _Tp> 807 inline complex<_Tp> 808 exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 809#endif 810 811 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 812 // The branch cut is along the negative axis. 813 template<typename _Tp> 814 inline complex<_Tp> 815 __complex_log(const complex<_Tp>& __z) 816 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 817 818#if _GLIBCXX_USE_C99_COMPLEX 819 inline __complex__ float 820 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 821 822 inline __complex__ double 823 __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 824 825 inline __complex__ long double 826 __complex_log(const __complex__ long double& __z) 827 { return __builtin_clogl(__z); } 828 829 template<typename _Tp> 830 inline complex<_Tp> 831 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 832#else 833 template<typename _Tp> 834 inline complex<_Tp> 835 log(const complex<_Tp>& __z) { return __complex_log(__z); } 836#endif 837 838 template<typename _Tp> 839 inline complex<_Tp> 840 log10(const complex<_Tp>& __z) 841 { return std::log(__z) / log(_Tp(10.0)); } 842 843 // 26.2.8/10 sin(__z): Returns the sine of __z. 844 template<typename _Tp> 845 inline complex<_Tp> 846 __complex_sin(const complex<_Tp>& __z) 847 { 848 const _Tp __x = __z.real(); 849 const _Tp __y = __z.imag(); 850 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 851 } 852 853#if _GLIBCXX_USE_C99_COMPLEX 854 inline __complex__ float 855 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 856 857 inline __complex__ double 858 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 859 860 inline __complex__ long double 861 __complex_sin(const __complex__ long double& __z) 862 { return __builtin_csinl(__z); } 863 864 template<typename _Tp> 865 inline complex<_Tp> 866 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 867#else 868 template<typename _Tp> 869 inline complex<_Tp> 870 sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 871#endif 872 873 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 874 template<typename _Tp> 875 inline complex<_Tp> 876 __complex_sinh(const complex<_Tp>& __z) 877 { 878 const _Tp __x = __z.real(); 879 const _Tp __y = __z.imag(); 880 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 881 } 882 883#if _GLIBCXX_USE_C99_COMPLEX 884 inline __complex__ float 885 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 886 887 inline __complex__ double 888 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 889 890 inline __complex__ long double 891 __complex_sinh(const __complex__ long double& __z) 892 { return __builtin_csinhl(__z); } 893 894 template<typename _Tp> 895 inline complex<_Tp> 896 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 897#else 898 template<typename _Tp> 899 inline complex<_Tp> 900 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 901#endif 902 903 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 904 // The branch cut is on the negative axis. 905 template<typename _Tp> 906 complex<_Tp> 907 __complex_sqrt(const complex<_Tp>& __z) 908 { 909 _Tp __x = __z.real(); 910 _Tp __y = __z.imag(); 911 912 if (__x == _Tp()) 913 { 914 _Tp __t = sqrt(abs(__y) / 2); 915 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 916 } 917 else 918 { 919 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 920 _Tp __u = __t / 2; 921 return __x > _Tp() 922 ? complex<_Tp>(__u, __y / __t) 923 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 924 } 925 } 926 927#if _GLIBCXX_USE_C99_COMPLEX 928 inline __complex__ float 929 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 930 931 inline __complex__ double 932 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 933 934 inline __complex__ long double 935 __complex_sqrt(const __complex__ long double& __z) 936 { return __builtin_csqrtl(__z); } 937 938 template<typename _Tp> 939 inline complex<_Tp> 940 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 941#else 942 template<typename _Tp> 943 inline complex<_Tp> 944 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 945#endif 946 947 // 26.2.8/14 tan(__z): Return the complex tangent of __z. 948 949 template<typename _Tp> 950 inline complex<_Tp> 951 __complex_tan(const complex<_Tp>& __z) 952 { return std::sin(__z) / std::cos(__z); } 953 954#if _GLIBCXX_USE_C99_COMPLEX 955 inline __complex__ float 956 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 957 958 inline __complex__ double 959 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 960 961 inline __complex__ long double 962 __complex_tan(const __complex__ long double& __z) 963 { return __builtin_ctanl(__z); } 964 965 template<typename _Tp> 966 inline complex<_Tp> 967 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 968#else 969 template<typename _Tp> 970 inline complex<_Tp> 971 tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 972#endif 973 974 975 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 976 977 template<typename _Tp> 978 inline complex<_Tp> 979 __complex_tanh(const complex<_Tp>& __z) 980 { return std::sinh(__z) / std::cosh(__z); } 981 982#if _GLIBCXX_USE_C99_COMPLEX 983 inline __complex__ float 984 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 985 986 inline __complex__ double 987 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 988 989 inline __complex__ long double 990 __complex_tanh(const __complex__ long double& __z) 991 { return __builtin_ctanhl(__z); } 992 993 template<typename _Tp> 994 inline complex<_Tp> 995 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 996#else 997 template<typename _Tp> 998 inline complex<_Tp> 999 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 1000#endif 1001 1002 1003 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 1004 // raised to the __y-th power. The branch 1005 // cut is on the negative axis. 1006 template<typename _Tp> 1007 complex<_Tp> 1008 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 1009 { 1010 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 1011 1012 while (__n >>= 1) 1013 { 1014 __x *= __x; 1015 if (__n % 2) 1016 __y *= __x; 1017 } 1018 1019 return __y; 1020 } 1021 1022 // In C++11 mode we used to implement the resolution of 1023 // DR 844. complex pow return type is ambiguous. 1024 // thus the following overload was disabled in that mode. However, doing 1025 // that causes all sorts of issues, see, for example: 1026 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html 1027 // and also PR57974. 1028 template<typename _Tp> 1029 inline complex<_Tp> 1030 pow(const complex<_Tp>& __z, int __n) 1031 { 1032 return __n < 0 1033 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 1034 : std::__complex_pow_unsigned(__z, __n); 1035 } 1036 1037 template<typename _Tp> 1038 complex<_Tp> 1039 pow(const complex<_Tp>& __x, const _Tp& __y) 1040 { 1041#if ! _GLIBCXX_USE_C99_COMPLEX 1042 if (__x == _Tp()) 1043 return _Tp(); 1044#endif 1045 if (__x.imag() == _Tp() && __x.real() > _Tp()) 1046 return pow(__x.real(), __y); 1047 1048 complex<_Tp> __t = std::log(__x); 1049 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); 1050 } 1051 1052 template<typename _Tp> 1053 inline complex<_Tp> 1054 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1055 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 1056 1057#if _GLIBCXX_USE_C99_COMPLEX 1058 inline __complex__ float 1059 __complex_pow(__complex__ float __x, __complex__ float __y) 1060 { return __builtin_cpowf(__x, __y); } 1061 1062 inline __complex__ double 1063 __complex_pow(__complex__ double __x, __complex__ double __y) 1064 { return __builtin_cpow(__x, __y); } 1065 1066 inline __complex__ long double 1067 __complex_pow(const __complex__ long double& __x, 1068 const __complex__ long double& __y) 1069 { return __builtin_cpowl(__x, __y); } 1070 1071 template<typename _Tp> 1072 inline complex<_Tp> 1073 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1074 { return __complex_pow(__x.__rep(), __y.__rep()); } 1075#else 1076 template<typename _Tp> 1077 inline complex<_Tp> 1078 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1079 { return __complex_pow(__x, __y); } 1080#endif 1081 1082 template<typename _Tp> 1083 inline complex<_Tp> 1084 pow(const _Tp& __x, const complex<_Tp>& __y) 1085 { 1086 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), 1087 __y.imag() * log(__x)) 1088 : std::pow(complex<_Tp>(__x), __y); 1089 } 1090 1091 /// 26.2.3 complex specializations 1092 /// complex<float> specialization 1093 template<> 1094 class complex<float> 1095 { 1096 public: 1097 typedef float value_type; 1098 typedef __complex__ float _ComplexT; 1099 1100 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1101 1102 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 1103#if __cplusplus >= 201103L 1104 : _M_value{ __r, __i } { } 1105#else 1106 { 1107 __real__ _M_value = __r; 1108 __imag__ _M_value = __i; 1109 } 1110#endif 1111 1112 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 1113 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1114 1115#if __cplusplus >= 201103L 1116 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1117 // DR 387. std::complex over-encapsulated. 1118 __attribute ((__abi_tag__ ("cxx11"))) 1119 constexpr float 1120 real() const { return __real__ _M_value; } 1121 1122 __attribute ((__abi_tag__ ("cxx11"))) 1123 constexpr float 1124 imag() const { return __imag__ _M_value; } 1125#else 1126 float& 1127 real() { return __real__ _M_value; } 1128 1129 const float& 1130 real() const { return __real__ _M_value; } 1131 1132 float& 1133 imag() { return __imag__ _M_value; } 1134 1135 const float& 1136 imag() const { return __imag__ _M_value; } 1137#endif 1138 1139 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1140 // DR 387. std::complex over-encapsulated. 1141 _GLIBCXX20_CONSTEXPR void 1142 real(float __val) { __real__ _M_value = __val; } 1143 1144 _GLIBCXX20_CONSTEXPR void 1145 imag(float __val) { __imag__ _M_value = __val; } 1146 1147 _GLIBCXX20_CONSTEXPR complex& 1148 operator=(float __f) 1149 { 1150 _M_value = __f; 1151 return *this; 1152 } 1153 1154 _GLIBCXX20_CONSTEXPR complex& 1155 operator+=(float __f) 1156 { 1157 _M_value += __f; 1158 return *this; 1159 } 1160 1161 _GLIBCXX20_CONSTEXPR complex& 1162 operator-=(float __f) 1163 { 1164 _M_value -= __f; 1165 return *this; 1166 } 1167 1168 _GLIBCXX20_CONSTEXPR complex& 1169 operator*=(float __f) 1170 { 1171 _M_value *= __f; 1172 return *this; 1173 } 1174 1175 _GLIBCXX20_CONSTEXPR complex& 1176 operator/=(float __f) 1177 { 1178 _M_value /= __f; 1179 return *this; 1180 } 1181 1182 // Let the compiler synthesize the copy and assignment 1183 // operator. It always does a pretty good job. 1184#if __cplusplus >= 201103L 1185 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; 1186#endif 1187 1188 template<typename _Tp> 1189 _GLIBCXX20_CONSTEXPR complex& 1190 operator=(const complex<_Tp>& __z) 1191 { 1192 __real__ _M_value = __z.real(); 1193 __imag__ _M_value = __z.imag(); 1194 return *this; 1195 } 1196 1197 template<typename _Tp> 1198 _GLIBCXX20_CONSTEXPR complex& 1199 operator+=(const complex<_Tp>& __z) 1200 { 1201 _M_value += __z.__rep(); 1202 return *this; 1203 } 1204 1205 template<class _Tp> 1206 _GLIBCXX20_CONSTEXPR complex& 1207 operator-=(const complex<_Tp>& __z) 1208 { 1209 _M_value -= __z.__rep(); 1210 return *this; 1211 } 1212 1213 template<class _Tp> 1214 _GLIBCXX20_CONSTEXPR complex& 1215 operator*=(const complex<_Tp>& __z) 1216 { 1217 const _ComplexT __t = __z.__rep(); 1218 _M_value *= __t; 1219 return *this; 1220 } 1221 1222 template<class _Tp> 1223 _GLIBCXX20_CONSTEXPR complex& 1224 operator/=(const complex<_Tp>& __z) 1225 { 1226 const _ComplexT __t = __z.__rep(); 1227 _M_value /= __t; 1228 return *this; 1229 } 1230 1231 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1232 1233 private: 1234 _ComplexT _M_value; 1235 }; 1236 1237 /// 26.2.3 complex specializations 1238 /// complex<double> specialization 1239 template<> 1240 class complex<double> 1241 { 1242 public: 1243 typedef double value_type; 1244 typedef __complex__ double _ComplexT; 1245 1246 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1247 1248 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 1249#if __cplusplus >= 201103L 1250 : _M_value{ __r, __i } { } 1251#else 1252 { 1253 __real__ _M_value = __r; 1254 __imag__ _M_value = __i; 1255 } 1256#endif 1257 1258 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1259 : _M_value(__z.__rep()) { } 1260 1261 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 1262 1263#if __cplusplus >= 201103L 1264 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1265 // DR 387. std::complex over-encapsulated. 1266 __attribute ((__abi_tag__ ("cxx11"))) 1267 constexpr double 1268 real() const { return __real__ _M_value; } 1269 1270 __attribute ((__abi_tag__ ("cxx11"))) 1271 constexpr double 1272 imag() const { return __imag__ _M_value; } 1273#else 1274 double& 1275 real() { return __real__ _M_value; } 1276 1277 const double& 1278 real() const { return __real__ _M_value; } 1279 1280 double& 1281 imag() { return __imag__ _M_value; } 1282 1283 const double& 1284 imag() const { return __imag__ _M_value; } 1285#endif 1286 1287 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1288 // DR 387. std::complex over-encapsulated. 1289 _GLIBCXX20_CONSTEXPR void 1290 real(double __val) { __real__ _M_value = __val; } 1291 1292 _GLIBCXX20_CONSTEXPR void 1293 imag(double __val) { __imag__ _M_value = __val; } 1294 1295 _GLIBCXX20_CONSTEXPR complex& 1296 operator=(double __d) 1297 { 1298 _M_value = __d; 1299 return *this; 1300 } 1301 1302 _GLIBCXX20_CONSTEXPR complex& 1303 operator+=(double __d) 1304 { 1305 _M_value += __d; 1306 return *this; 1307 } 1308 1309 _GLIBCXX20_CONSTEXPR complex& 1310 operator-=(double __d) 1311 { 1312 _M_value -= __d; 1313 return *this; 1314 } 1315 1316 _GLIBCXX20_CONSTEXPR complex& 1317 operator*=(double __d) 1318 { 1319 _M_value *= __d; 1320 return *this; 1321 } 1322 1323 _GLIBCXX20_CONSTEXPR complex& 1324 operator/=(double __d) 1325 { 1326 _M_value /= __d; 1327 return *this; 1328 } 1329 1330 // The compiler will synthesize this, efficiently. 1331#if __cplusplus >= 201103L 1332 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; 1333#endif 1334 1335 template<typename _Tp> 1336 _GLIBCXX20_CONSTEXPR complex& 1337 operator=(const complex<_Tp>& __z) 1338 { 1339 _M_value = __z.__rep(); 1340 return *this; 1341 } 1342 1343 template<typename _Tp> 1344 _GLIBCXX20_CONSTEXPR complex& 1345 operator+=(const complex<_Tp>& __z) 1346 { 1347 _M_value += __z.__rep(); 1348 return *this; 1349 } 1350 1351 template<typename _Tp> 1352 _GLIBCXX20_CONSTEXPR complex& 1353 operator-=(const complex<_Tp>& __z) 1354 { 1355 _M_value -= __z.__rep(); 1356 return *this; 1357 } 1358 1359 template<typename _Tp> 1360 _GLIBCXX20_CONSTEXPR complex& 1361 operator*=(const complex<_Tp>& __z) 1362 { 1363 const _ComplexT __t = __z.__rep(); 1364 _M_value *= __t; 1365 return *this; 1366 } 1367 1368 template<typename _Tp> 1369 _GLIBCXX20_CONSTEXPR complex& 1370 operator/=(const complex<_Tp>& __z) 1371 { 1372 const _ComplexT __t = __z.__rep(); 1373 _M_value /= __t; 1374 return *this; 1375 } 1376 1377 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1378 1379 private: 1380 _ComplexT _M_value; 1381 }; 1382 1383 /// 26.2.3 complex specializations 1384 /// complex<long double> specialization 1385 template<> 1386 class complex<long double> 1387 { 1388 public: 1389 typedef long double value_type; 1390 typedef __complex__ long double _ComplexT; 1391 1392 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 1393 1394 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 1395 long double __i = 0.0L) 1396#if __cplusplus >= 201103L 1397 : _M_value{ __r, __i } { } 1398#else 1399 { 1400 __real__ _M_value = __r; 1401 __imag__ _M_value = __i; 1402 } 1403#endif 1404 1405 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 1406 : _M_value(__z.__rep()) { } 1407 1408 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 1409 : _M_value(__z.__rep()) { } 1410 1411#if __cplusplus >= 201103L 1412 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1413 // DR 387. std::complex over-encapsulated. 1414 __attribute ((__abi_tag__ ("cxx11"))) 1415 constexpr long double 1416 real() const { return __real__ _M_value; } 1417 1418 __attribute ((__abi_tag__ ("cxx11"))) 1419 constexpr long double 1420 imag() const { return __imag__ _M_value; } 1421#else 1422 long double& 1423 real() { return __real__ _M_value; } 1424 1425 const long double& 1426 real() const { return __real__ _M_value; } 1427 1428 long double& 1429 imag() { return __imag__ _M_value; } 1430 1431 const long double& 1432 imag() const { return __imag__ _M_value; } 1433#endif 1434 1435 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1436 // DR 387. std::complex over-encapsulated. 1437 _GLIBCXX20_CONSTEXPR void 1438 real(long double __val) { __real__ _M_value = __val; } 1439 1440 _GLIBCXX20_CONSTEXPR void 1441 imag(long double __val) { __imag__ _M_value = __val; } 1442 1443 _GLIBCXX20_CONSTEXPR complex& 1444 operator=(long double __r) 1445 { 1446 _M_value = __r; 1447 return *this; 1448 } 1449 1450 _GLIBCXX20_CONSTEXPR complex& 1451 operator+=(long double __r) 1452 { 1453 _M_value += __r; 1454 return *this; 1455 } 1456 1457 _GLIBCXX20_CONSTEXPR complex& 1458 operator-=(long double __r) 1459 { 1460 _M_value -= __r; 1461 return *this; 1462 } 1463 1464 _GLIBCXX20_CONSTEXPR complex& 1465 operator*=(long double __r) 1466 { 1467 _M_value *= __r; 1468 return *this; 1469 } 1470 1471 _GLIBCXX20_CONSTEXPR complex& 1472 operator/=(long double __r) 1473 { 1474 _M_value /= __r; 1475 return *this; 1476 } 1477 1478 // The compiler knows how to do this efficiently 1479#if __cplusplus >= 201103L 1480 _GLIBCXX14_CONSTEXPR complex& operator=(const complex&) = default; 1481#endif 1482 1483 template<typename _Tp> 1484 _GLIBCXX20_CONSTEXPR complex& 1485 operator=(const complex<_Tp>& __z) 1486 { 1487 _M_value = __z.__rep(); 1488 return *this; 1489 } 1490 1491 template<typename _Tp> 1492 _GLIBCXX20_CONSTEXPR complex& 1493 operator+=(const complex<_Tp>& __z) 1494 { 1495 _M_value += __z.__rep(); 1496 return *this; 1497 } 1498 1499 template<typename _Tp> 1500 _GLIBCXX20_CONSTEXPR complex& 1501 operator-=(const complex<_Tp>& __z) 1502 { 1503 _M_value -= __z.__rep(); 1504 return *this; 1505 } 1506 1507 template<typename _Tp> 1508 _GLIBCXX20_CONSTEXPR complex& 1509 operator*=(const complex<_Tp>& __z) 1510 { 1511 const _ComplexT __t = __z.__rep(); 1512 _M_value *= __t; 1513 return *this; 1514 } 1515 1516 template<typename _Tp> 1517 _GLIBCXX20_CONSTEXPR complex& 1518 operator/=(const complex<_Tp>& __z) 1519 { 1520 const _ComplexT __t = __z.__rep(); 1521 _M_value /= __t; 1522 return *this; 1523 } 1524 1525 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 1526 1527 private: 1528 _ComplexT _M_value; 1529 }; 1530 1531 // These bits have to be at the end of this file, so that the 1532 // specializations have all been defined. 1533 inline _GLIBCXX_CONSTEXPR 1534 complex<float>::complex(const complex<double>& __z) 1535 : _M_value(__z.__rep()) { } 1536 1537 inline _GLIBCXX_CONSTEXPR 1538 complex<float>::complex(const complex<long double>& __z) 1539 : _M_value(__z.__rep()) { } 1540 1541 inline _GLIBCXX_CONSTEXPR 1542 complex<double>::complex(const complex<long double>& __z) 1543 : _M_value(__z.__rep()) { } 1544 1545 // Inhibit implicit instantiations for required instantiations, 1546 // which are defined via explicit instantiations elsewhere. 1547 // NB: This syntax is a GNU extension. 1548#if _GLIBCXX_EXTERN_TEMPLATE 1549 extern template istream& operator>>(istream&, complex<float>&); 1550 extern template ostream& operator<<(ostream&, const complex<float>&); 1551 extern template istream& operator>>(istream&, complex<double>&); 1552 extern template ostream& operator<<(ostream&, const complex<double>&); 1553 extern template istream& operator>>(istream&, complex<long double>&); 1554 extern template ostream& operator<<(ostream&, const complex<long double>&); 1555 1556#ifdef _GLIBCXX_USE_WCHAR_T 1557 extern template wistream& operator>>(wistream&, complex<float>&); 1558 extern template wostream& operator<<(wostream&, const complex<float>&); 1559 extern template wistream& operator>>(wistream&, complex<double>&); 1560 extern template wostream& operator<<(wostream&, const complex<double>&); 1561 extern template wistream& operator>>(wistream&, complex<long double>&); 1562 extern template wostream& operator<<(wostream&, const complex<long double>&); 1563#endif 1564#endif 1565 1566 /// @} group complex_numbers 1567 1568_GLIBCXX_END_NAMESPACE_VERSION 1569} // namespace 1570 1571#if __cplusplus >= 201103L 1572 1573namespace std _GLIBCXX_VISIBILITY(default) 1574{ 1575_GLIBCXX_BEGIN_NAMESPACE_VERSION 1576 1577 // Forward declarations. 1578 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 1579 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 1580 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 1581 1582 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 1583 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 1584 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 1585 // DR 595. 1586 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 1587 1588 template<typename _Tp> 1589 inline std::complex<_Tp> 1590 __complex_acos(const std::complex<_Tp>& __z) 1591 { 1592 const std::complex<_Tp> __t = std::asin(__z); 1593 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 1594 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 1595 } 1596 1597#if _GLIBCXX_USE_C99_COMPLEX_TR1 1598 inline __complex__ float 1599 __complex_acos(__complex__ float __z) 1600 { return __builtin_cacosf(__z); } 1601 1602 inline __complex__ double 1603 __complex_acos(__complex__ double __z) 1604 { return __builtin_cacos(__z); } 1605 1606 inline __complex__ long double 1607 __complex_acos(const __complex__ long double& __z) 1608 { return __builtin_cacosl(__z); } 1609 1610 template<typename _Tp> 1611 inline std::complex<_Tp> 1612 acos(const std::complex<_Tp>& __z) 1613 { return __complex_acos(__z.__rep()); } 1614#else 1615 /// acos(__z) [8.1.2]. 1616 // Effects: Behaves the same as C99 function cacos, defined 1617 // in subclause 7.3.5.1. 1618 template<typename _Tp> 1619 inline std::complex<_Tp> 1620 acos(const std::complex<_Tp>& __z) 1621 { return __complex_acos(__z); } 1622#endif 1623 1624 template<typename _Tp> 1625 inline std::complex<_Tp> 1626 __complex_asin(const std::complex<_Tp>& __z) 1627 { 1628 std::complex<_Tp> __t(-__z.imag(), __z.real()); 1629 __t = std::asinh(__t); 1630 return std::complex<_Tp>(__t.imag(), -__t.real()); 1631 } 1632 1633#if _GLIBCXX_USE_C99_COMPLEX_TR1 1634 inline __complex__ float 1635 __complex_asin(__complex__ float __z) 1636 { return __builtin_casinf(__z); } 1637 1638 inline __complex__ double 1639 __complex_asin(__complex__ double __z) 1640 { return __builtin_casin(__z); } 1641 1642 inline __complex__ long double 1643 __complex_asin(const __complex__ long double& __z) 1644 { return __builtin_casinl(__z); } 1645 1646 template<typename _Tp> 1647 inline std::complex<_Tp> 1648 asin(const std::complex<_Tp>& __z) 1649 { return __complex_asin(__z.__rep()); } 1650#else 1651 /// asin(__z) [8.1.3]. 1652 // Effects: Behaves the same as C99 function casin, defined 1653 // in subclause 7.3.5.2. 1654 template<typename _Tp> 1655 inline std::complex<_Tp> 1656 asin(const std::complex<_Tp>& __z) 1657 { return __complex_asin(__z); } 1658#endif 1659 1660 template<typename _Tp> 1661 std::complex<_Tp> 1662 __complex_atan(const std::complex<_Tp>& __z) 1663 { 1664 const _Tp __r2 = __z.real() * __z.real(); 1665 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 1666 1667 _Tp __num = __z.imag() + _Tp(1.0); 1668 _Tp __den = __z.imag() - _Tp(1.0); 1669 1670 __num = __r2 + __num * __num; 1671 __den = __r2 + __den * __den; 1672 1673 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 1674 _Tp(0.25) * log(__num / __den)); 1675 } 1676 1677#if _GLIBCXX_USE_C99_COMPLEX_TR1 1678 inline __complex__ float 1679 __complex_atan(__complex__ float __z) 1680 { return __builtin_catanf(__z); } 1681 1682 inline __complex__ double 1683 __complex_atan(__complex__ double __z) 1684 { return __builtin_catan(__z); } 1685 1686 inline __complex__ long double 1687 __complex_atan(const __complex__ long double& __z) 1688 { return __builtin_catanl(__z); } 1689 1690 template<typename _Tp> 1691 inline std::complex<_Tp> 1692 atan(const std::complex<_Tp>& __z) 1693 { return __complex_atan(__z.__rep()); } 1694#else 1695 /// atan(__z) [8.1.4]. 1696 // Effects: Behaves the same as C99 function catan, defined 1697 // in subclause 7.3.5.3. 1698 template<typename _Tp> 1699 inline std::complex<_Tp> 1700 atan(const std::complex<_Tp>& __z) 1701 { return __complex_atan(__z); } 1702#endif 1703 1704 template<typename _Tp> 1705 std::complex<_Tp> 1706 __complex_acosh(const std::complex<_Tp>& __z) 1707 { 1708 // Kahan's formula. 1709 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 1710 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 1711 } 1712 1713#if _GLIBCXX_USE_C99_COMPLEX_TR1 1714 inline __complex__ float 1715 __complex_acosh(__complex__ float __z) 1716 { return __builtin_cacoshf(__z); } 1717 1718 inline __complex__ double 1719 __complex_acosh(__complex__ double __z) 1720 { return __builtin_cacosh(__z); } 1721 1722 inline __complex__ long double 1723 __complex_acosh(const __complex__ long double& __z) 1724 { return __builtin_cacoshl(__z); } 1725 1726 template<typename _Tp> 1727 inline std::complex<_Tp> 1728 acosh(const std::complex<_Tp>& __z) 1729 { return __complex_acosh(__z.__rep()); } 1730#else 1731 /// acosh(__z) [8.1.5]. 1732 // Effects: Behaves the same as C99 function cacosh, defined 1733 // in subclause 7.3.6.1. 1734 template<typename _Tp> 1735 inline std::complex<_Tp> 1736 acosh(const std::complex<_Tp>& __z) 1737 { return __complex_acosh(__z); } 1738#endif 1739 1740 template<typename _Tp> 1741 std::complex<_Tp> 1742 __complex_asinh(const std::complex<_Tp>& __z) 1743 { 1744 std::complex<_Tp> __t((__z.real() - __z.imag()) 1745 * (__z.real() + __z.imag()) + _Tp(1.0), 1746 _Tp(2.0) * __z.real() * __z.imag()); 1747 __t = std::sqrt(__t); 1748 1749 return std::log(__t + __z); 1750 } 1751 1752#if _GLIBCXX_USE_C99_COMPLEX_TR1 1753 inline __complex__ float 1754 __complex_asinh(__complex__ float __z) 1755 { return __builtin_casinhf(__z); } 1756 1757 inline __complex__ double 1758 __complex_asinh(__complex__ double __z) 1759 { return __builtin_casinh(__z); } 1760 1761 inline __complex__ long double 1762 __complex_asinh(const __complex__ long double& __z) 1763 { return __builtin_casinhl(__z); } 1764 1765 template<typename _Tp> 1766 inline std::complex<_Tp> 1767 asinh(const std::complex<_Tp>& __z) 1768 { return __complex_asinh(__z.__rep()); } 1769#else 1770 /// asinh(__z) [8.1.6]. 1771 // Effects: Behaves the same as C99 function casin, defined 1772 // in subclause 7.3.6.2. 1773 template<typename _Tp> 1774 inline std::complex<_Tp> 1775 asinh(const std::complex<_Tp>& __z) 1776 { return __complex_asinh(__z); } 1777#endif 1778 1779 template<typename _Tp> 1780 std::complex<_Tp> 1781 __complex_atanh(const std::complex<_Tp>& __z) 1782 { 1783 const _Tp __i2 = __z.imag() * __z.imag(); 1784 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 1785 1786 _Tp __num = _Tp(1.0) + __z.real(); 1787 _Tp __den = _Tp(1.0) - __z.real(); 1788 1789 __num = __i2 + __num * __num; 1790 __den = __i2 + __den * __den; 1791 1792 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 1793 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 1794 } 1795 1796#if _GLIBCXX_USE_C99_COMPLEX_TR1 1797 inline __complex__ float 1798 __complex_atanh(__complex__ float __z) 1799 { return __builtin_catanhf(__z); } 1800 1801 inline __complex__ double 1802 __complex_atanh(__complex__ double __z) 1803 { return __builtin_catanh(__z); } 1804 1805 inline __complex__ long double 1806 __complex_atanh(const __complex__ long double& __z) 1807 { return __builtin_catanhl(__z); } 1808 1809 template<typename _Tp> 1810 inline std::complex<_Tp> 1811 atanh(const std::complex<_Tp>& __z) 1812 { return __complex_atanh(__z.__rep()); } 1813#else 1814 /// atanh(__z) [8.1.7]. 1815 // Effects: Behaves the same as C99 function catanh, defined 1816 // in subclause 7.3.6.3. 1817 template<typename _Tp> 1818 inline std::complex<_Tp> 1819 atanh(const std::complex<_Tp>& __z) 1820 { return __complex_atanh(__z); } 1821#endif 1822 1823 template<typename _Tp> 1824 inline _Tp 1825 /// fabs(__z) [8.1.8]. 1826 // Effects: Behaves the same as C99 function cabs, defined 1827 // in subclause 7.3.8.1. 1828 fabs(const std::complex<_Tp>& __z) 1829 { return std::abs(__z); } 1830 1831 /// Additional overloads [8.1.9]. 1832 template<typename _Tp> 1833 inline typename __gnu_cxx::__promote<_Tp>::__type 1834 arg(_Tp __x) 1835 { 1836 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1837#if (_GLIBCXX11_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 1838 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 1839 : __type(); 1840#else 1841 return std::arg(std::complex<__type>(__x)); 1842#endif 1843 } 1844 1845 template<typename _Tp> 1846 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1847 imag(_Tp) 1848 { return _Tp(); } 1849 1850 template<typename _Tp> 1851 _GLIBCXX20_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1852 norm(_Tp __x) 1853 { 1854 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1855 return __type(__x) * __type(__x); 1856 } 1857 1858 template<typename _Tp> 1859 _GLIBCXX_CONSTEXPR inline typename __gnu_cxx::__promote<_Tp>::__type 1860 real(_Tp __x) 1861 { return __x; } 1862 1863 template<typename _Tp, typename _Up> 1864 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1865 pow(const std::complex<_Tp>& __x, const _Up& __y) 1866 { 1867 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1868 return std::pow(std::complex<__type>(__x), __type(__y)); 1869 } 1870 1871 template<typename _Tp, typename _Up> 1872 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1873 pow(const _Tp& __x, const std::complex<_Up>& __y) 1874 { 1875 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1876 return std::pow(__type(__x), std::complex<__type>(__y)); 1877 } 1878 1879 template<typename _Tp, typename _Up> 1880 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 1881 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 1882 { 1883 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 1884 return std::pow(std::complex<__type>(__x), 1885 std::complex<__type>(__y)); 1886 } 1887 1888 // Forward declarations. 1889 // DR 781. 1890 template<typename _Tp> 1891 std::complex<_Tp> proj(const std::complex<_Tp>&); 1892 1893 // Generic implementation of std::proj, does not work for infinities. 1894 template<typename _Tp> 1895 inline std::complex<_Tp> 1896 __complex_proj(const std::complex<_Tp>& __z) 1897 { return __z; } 1898 1899#if _GLIBCXX_USE_C99_COMPLEX 1900 inline complex<float> 1901 __complex_proj(const complex<float>& __z) 1902 { return __builtin_cprojf(__z.__rep()); } 1903 1904 inline complex<double> 1905 __complex_proj(const complex<double>& __z) 1906 { return __builtin_cproj(__z.__rep()); } 1907 1908 inline complex<long double> 1909 __complex_proj(const complex<long double>& __z) 1910 { return __builtin_cprojl(__z.__rep()); } 1911#elif defined _GLIBCXX_USE_C99_MATH_TR1 1912 inline complex<float> 1913 __complex_proj(const complex<float>& __z) 1914 { 1915 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) 1916 return complex<float>(__builtin_inff(), 1917 __builtin_copysignf(0.0f, __z.imag())); 1918 return __z; 1919 } 1920 1921 inline complex<double> 1922 __complex_proj(const complex<double>& __z) 1923 { 1924 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) 1925 return complex<double>(__builtin_inf(), 1926 __builtin_copysign(0.0, __z.imag())); 1927 return __z; 1928 } 1929 1930 inline complex<long double> 1931 __complex_proj(const complex<long double>& __z) 1932 { 1933 if (__builtin_isinf(__z.real()) || __builtin_isinf(__z.imag())) 1934 return complex<long double>(__builtin_infl(), 1935 __builtin_copysignl(0.0l, __z.imag())); 1936 return __z; 1937 } 1938#endif 1939 1940 template<typename _Tp> 1941 inline std::complex<_Tp> 1942 proj(const std::complex<_Tp>& __z) 1943 { return __complex_proj(__z); } 1944 1945 // Overload for scalars 1946 template<typename _Tp> 1947 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1948 proj(_Tp __x) 1949 { 1950 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1951 return std::proj(std::complex<__type>(__x)); 1952 } 1953 1954 template<typename _Tp> 1955 inline _GLIBCXX20_CONSTEXPR 1956 std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1957 conj(_Tp __x) 1958 { 1959 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1960 return std::complex<__type>(__x, -__type()); 1961 } 1962 1963#if __cplusplus > 201103L 1964 1965inline namespace literals { 1966inline namespace complex_literals { 1967#pragma GCC diagnostic push 1968#pragma GCC diagnostic ignored "-Wliteral-suffix" 1969#define __cpp_lib_complex_udls 201309L 1970 1971 constexpr std::complex<float> 1972 operator""if(long double __num) 1973 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1974 1975 constexpr std::complex<float> 1976 operator""if(unsigned long long __num) 1977 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 1978 1979 constexpr std::complex<double> 1980 operator""i(long double __num) 1981 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1982 1983 constexpr std::complex<double> 1984 operator""i(unsigned long long __num) 1985 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 1986 1987 constexpr std::complex<long double> 1988 operator""il(long double __num) 1989 { return std::complex<long double>{0.0L, __num}; } 1990 1991 constexpr std::complex<long double> 1992 operator""il(unsigned long long __num) 1993 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } 1994 1995#pragma GCC diagnostic pop 1996} // inline namespace complex_literals 1997} // inline namespace literals 1998 1999#endif // C++14 2000 2001_GLIBCXX_END_NAMESPACE_VERSION 2002} // namespace 2003 2004#endif // C++11 2005 2006#endif /* _GLIBCXX_COMPLEX */ 2007