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