1 // The template and inlines for the -*- C++ -*- complex number classes. 2 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4 // 2006, 2007, 2008, 2009 5 // Free Software Foundation, Inc. 6 // 7 // This file is part of the GNU ISO C++ Library. This library is free 8 // software; you can redistribute it and/or modify it under the 9 // terms of the GNU General Public License as published by the 10 // Free Software Foundation; either version 3, or (at your option) 11 // any later version. 12 13 // This library is distributed in the hope that it will be useful, 14 // but WITHOUT ANY WARRANTY; without even the implied warranty of 15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 // GNU General Public License for more details. 17 18 // Under Section 7 of GPL version 3, you are granted additional 19 // permissions described in the GCC Runtime Library Exception, version 20 // 3.1, as published by the Free Software Foundation. 21 22 // You should have received a copy of the GNU General Public License and 23 // a copy of the GCC Runtime Library Exception along with this program; 24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25 // <http://www.gnu.org/licenses/>. 26 27 /** @file include/complex 28 * This is a Standard C++ Library header. 29 */ 30 31 // 32 // ISO C++ 14882: 26.2 Complex Numbers 33 // Note: this is not a conforming implementation. 34 // Initially implemented by Ulrich Drepper <drepper (a] cygnus.com> 35 // Improved by Gabriel Dos Reis <dosreis (a] cmla.ens-cachan.fr> 36 // 37 38 #ifndef _GLIBCXX_COMPLEX 39 #define _GLIBCXX_COMPLEX 1 40 41 #pragma GCC system_header 42 43 #include <bits/c++config.h> 44 #include <bits/cpp_type_traits.h> 45 #include <ext/type_traits.h> 46 #include <cmath> 47 #include <sstream> 48 49 _GLIBCXX_BEGIN_NAMESPACE(std) 50 51 /** 52 * @defgroup complex_numbers Complex Numbers 53 * @ingroup numerics 54 * 55 * Classes and functions for complex numbers. 56 * @{ 57 */ 58 59 // Forward declarations. 60 template<typename _Tp> class complex; 61 template<> class complex<float>; 62 template<> class complex<double>; 63 template<> class complex<long double>; 64 65 /// Return magnitude of @a z. 66 template<typename _Tp> _Tp abs(const complex<_Tp>&); 67 /// Return phase angle of @a z. 68 template<typename _Tp> _Tp arg(const complex<_Tp>&); 69 /// Return @a z magnitude squared. 70 template<typename _Tp> _Tp norm(const complex<_Tp>&); 71 72 /// Return complex conjugate of @a z. 73 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); 74 /// Return complex with magnitude @a rho and angle @a theta. 75 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 76 77 // Transcendentals: 78 /// Return complex cosine of @a z. 79 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 80 /// Return complex hyperbolic cosine of @a z. 81 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 82 /// Return complex base e exponential of @a z. 83 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 84 /// Return complex natural logarithm of @a z. 85 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 86 /// Return complex base 10 logarithm of @a z. 87 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 88 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 89 // DR 844. 90 /// Return @a x to the @a y'th power. 91 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 92 #endif 93 /// Return @a x to the @a y'th power. 94 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 95 /// Return @a x to the @a y'th power. 96 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 97 const complex<_Tp>&); 98 /// Return @a x to the @a y'th power. 99 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 100 /// Return complex sine of @a z. 101 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 102 /// Return complex hyperbolic sine of @a z. 103 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 104 /// Return complex square root of @a z. 105 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 106 /// Return complex tangent of @a z. 107 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 108 /// Return complex hyperbolic tangent of @a z. 109 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 110 111 112 // 26.2.2 Primary template class complex 113 /** 114 * Template to represent complex numbers. 115 * 116 * Specializations for float, double, and long double are part of the 117 * library. Results with any other type are not guaranteed. 118 * 119 * @param Tp Type of real and imaginary values. 120 */ 121 template<typename _Tp> 122 struct complex 123 { 124 /// Value typedef. 125 typedef _Tp value_type; 126 127 /// Default constructor. First parameter is x, second parameter is y. 128 /// Unspecified parameters default to 0. 129 complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) 130 : _M_real(__r), _M_imag(__i) { } 131 132 // Lets the compiler synthesize the copy constructor 133 // complex (const complex<_Tp>&); 134 /// Copy constructor. 135 template<typename _Up> 136 complex(const complex<_Up>& __z) 137 : _M_real(__z.real()), _M_imag(__z.imag()) { } 138 139 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 140 // _GLIBCXX_RESOLVE_LIB_DEFECTS 141 // DR 387. std::complex over-encapsulated. 142 _Tp real() const 143 { return _M_real; } 144 145 _Tp imag() const 146 { return _M_imag; } 147 #else 148 /// Return real part of complex number. 149 _Tp& real() 150 { return _M_real; } 151 152 /// Return real part of complex number. 153 const _Tp& real() const 154 { return _M_real; } 155 156 /// Return imaginary part of complex number. 157 _Tp& imag() 158 { return _M_imag; } 159 160 /// Return imaginary part of complex number. 161 const _Tp& imag() const 162 { return _M_imag; } 163 #endif 164 165 // _GLIBCXX_RESOLVE_LIB_DEFECTS 166 // DR 387. std::complex over-encapsulated. 167 void real(_Tp __val) 168 { _M_real = __val; } 169 170 void imag(_Tp __val) 171 { _M_imag = __val; } 172 173 /// Assign this complex number to scalar @a t. 174 complex<_Tp>& operator=(const _Tp&); 175 176 /// Add @a t to this complex number. 177 // 26.2.5/1 178 complex<_Tp>& 179 operator+=(const _Tp& __t) 180 { 181 _M_real += __t; 182 return *this; 183 } 184 185 /// Subtract @a t from this complex number. 186 // 26.2.5/3 187 complex<_Tp>& 188 operator-=(const _Tp& __t) 189 { 190 _M_real -= __t; 191 return *this; 192 } 193 194 /// Multiply this complex number by @a t. 195 complex<_Tp>& operator*=(const _Tp&); 196 /// Divide this complex number by @a t. 197 complex<_Tp>& operator/=(const _Tp&); 198 199 // Lets the compiler synthesize the 200 // copy and assignment operator 201 // complex<_Tp>& operator= (const complex<_Tp>&); 202 /// Assign this complex number to complex @a z. 203 template<typename _Up> 204 complex<_Tp>& operator=(const complex<_Up>&); 205 /// Add @a z to this complex number. 206 template<typename _Up> 207 complex<_Tp>& operator+=(const complex<_Up>&); 208 /// Subtract @a z from this complex number. 209 template<typename _Up> 210 complex<_Tp>& operator-=(const complex<_Up>&); 211 /// Multiply this complex number by @a z. 212 template<typename _Up> 213 complex<_Tp>& operator*=(const complex<_Up>&); 214 /// Divide this complex number by @a z. 215 template<typename _Up> 216 complex<_Tp>& operator/=(const complex<_Up>&); 217 218 const complex& __rep() const 219 { return *this; } 220 221 private: 222 _Tp _M_real; 223 _Tp _M_imag; 224 }; 225 226 template<typename _Tp> 227 complex<_Tp>& 228 complex<_Tp>::operator=(const _Tp& __t) 229 { 230 _M_real = __t; 231 _M_imag = _Tp(); 232 return *this; 233 } 234 235 // 26.2.5/5 236 template<typename _Tp> 237 complex<_Tp>& 238 complex<_Tp>::operator*=(const _Tp& __t) 239 { 240 _M_real *= __t; 241 _M_imag *= __t; 242 return *this; 243 } 244 245 // 26.2.5/7 246 template<typename _Tp> 247 complex<_Tp>& 248 complex<_Tp>::operator/=(const _Tp& __t) 249 { 250 _M_real /= __t; 251 _M_imag /= __t; 252 return *this; 253 } 254 255 template<typename _Tp> 256 template<typename _Up> 257 complex<_Tp>& 258 complex<_Tp>::operator=(const complex<_Up>& __z) 259 { 260 _M_real = __z.real(); 261 _M_imag = __z.imag(); 262 return *this; 263 } 264 265 // 26.2.5/9 266 template<typename _Tp> 267 template<typename _Up> 268 complex<_Tp>& 269 complex<_Tp>::operator+=(const complex<_Up>& __z) 270 { 271 _M_real += __z.real(); 272 _M_imag += __z.imag(); 273 return *this; 274 } 275 276 // 26.2.5/11 277 template<typename _Tp> 278 template<typename _Up> 279 complex<_Tp>& 280 complex<_Tp>::operator-=(const complex<_Up>& __z) 281 { 282 _M_real -= __z.real(); 283 _M_imag -= __z.imag(); 284 return *this; 285 } 286 287 // 26.2.5/13 288 // XXX: This is a grammar school implementation. 289 template<typename _Tp> 290 template<typename _Up> 291 complex<_Tp>& 292 complex<_Tp>::operator*=(const complex<_Up>& __z) 293 { 294 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 295 _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 296 _M_real = __r; 297 return *this; 298 } 299 300 // 26.2.5/15 301 // XXX: This is a grammar school implementation. 302 template<typename _Tp> 303 template<typename _Up> 304 complex<_Tp>& 305 complex<_Tp>::operator/=(const complex<_Up>& __z) 306 { 307 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 308 const _Tp __n = std::norm(__z); 309 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 310 _M_real = __r / __n; 311 return *this; 312 } 313 314 // Operators: 315 //@{ 316 /// Return new complex value @a x plus @a y. 317 template<typename _Tp> 318 inline complex<_Tp> 319 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 320 { 321 complex<_Tp> __r = __x; 322 __r += __y; 323 return __r; 324 } 325 326 template<typename _Tp> 327 inline complex<_Tp> 328 operator+(const complex<_Tp>& __x, const _Tp& __y) 329 { 330 complex<_Tp> __r = __x; 331 __r += __y; 332 return __r; 333 } 334 335 template<typename _Tp> 336 inline complex<_Tp> 337 operator+(const _Tp& __x, const complex<_Tp>& __y) 338 { 339 complex<_Tp> __r = __y; 340 __r += __x; 341 return __r; 342 } 343 //@} 344 345 //@{ 346 /// Return new complex value @a x minus @a y. 347 template<typename _Tp> 348 inline complex<_Tp> 349 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 350 { 351 complex<_Tp> __r = __x; 352 __r -= __y; 353 return __r; 354 } 355 356 template<typename _Tp> 357 inline complex<_Tp> 358 operator-(const complex<_Tp>& __x, const _Tp& __y) 359 { 360 complex<_Tp> __r = __x; 361 __r -= __y; 362 return __r; 363 } 364 365 template<typename _Tp> 366 inline complex<_Tp> 367 operator-(const _Tp& __x, const complex<_Tp>& __y) 368 { 369 complex<_Tp> __r(__x, -__y.imag()); 370 __r -= __y.real(); 371 return __r; 372 } 373 //@} 374 375 //@{ 376 /// Return new complex value @a x times @a y. 377 template<typename _Tp> 378 inline complex<_Tp> 379 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 380 { 381 complex<_Tp> __r = __x; 382 __r *= __y; 383 return __r; 384 } 385 386 template<typename _Tp> 387 inline complex<_Tp> 388 operator*(const complex<_Tp>& __x, const _Tp& __y) 389 { 390 complex<_Tp> __r = __x; 391 __r *= __y; 392 return __r; 393 } 394 395 template<typename _Tp> 396 inline complex<_Tp> 397 operator*(const _Tp& __x, const complex<_Tp>& __y) 398 { 399 complex<_Tp> __r = __y; 400 __r *= __x; 401 return __r; 402 } 403 //@} 404 405 //@{ 406 /// Return new complex value @a x divided by @a y. 407 template<typename _Tp> 408 inline complex<_Tp> 409 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 410 { 411 complex<_Tp> __r = __x; 412 __r /= __y; 413 return __r; 414 } 415 416 template<typename _Tp> 417 inline complex<_Tp> 418 operator/(const complex<_Tp>& __x, const _Tp& __y) 419 { 420 complex<_Tp> __r = __x; 421 __r /= __y; 422 return __r; 423 } 424 425 template<typename _Tp> 426 inline complex<_Tp> 427 operator/(const _Tp& __x, const complex<_Tp>& __y) 428 { 429 complex<_Tp> __r = __x; 430 __r /= __y; 431 return __r; 432 } 433 //@} 434 435 /// Return @a x. 436 template<typename _Tp> 437 inline complex<_Tp> 438 operator+(const complex<_Tp>& __x) 439 { return __x; } 440 441 /// Return complex negation of @a x. 442 template<typename _Tp> 443 inline complex<_Tp> 444 operator-(const complex<_Tp>& __x) 445 { return complex<_Tp>(-__x.real(), -__x.imag()); } 446 447 //@{ 448 /// Return true if @a x is equal to @a y. 449 template<typename _Tp> 450 inline bool 451 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 452 { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 453 454 template<typename _Tp> 455 inline bool 456 operator==(const complex<_Tp>& __x, const _Tp& __y) 457 { return __x.real() == __y && __x.imag() == _Tp(); } 458 459 template<typename _Tp> 460 inline bool 461 operator==(const _Tp& __x, const complex<_Tp>& __y) 462 { return __x == __y.real() && _Tp() == __y.imag(); } 463 //@} 464 465 //@{ 466 /// Return false if @a x is equal to @a y. 467 template<typename _Tp> 468 inline 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 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 bool 479 operator!=(const _Tp& __x, const complex<_Tp>& __y) 480 { return __x != __y.real() || _Tp() != __y.imag(); } 481 //@} 482 483 /// Extraction operator for complex values. 484 template<typename _Tp, typename _CharT, class _Traits> 485 basic_istream<_CharT, _Traits>& 486 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 487 { 488 _Tp __re_x, __im_x; 489 _CharT __ch; 490 __is >> __ch; 491 if (__ch == '(') 492 { 493 __is >> __re_x >> __ch; 494 if (__ch == ',') 495 { 496 __is >> __im_x >> __ch; 497 if (__ch == ')') 498 __x = complex<_Tp>(__re_x, __im_x); 499 else 500 __is.setstate(ios_base::failbit); 501 } 502 else if (__ch == ')') 503 __x = __re_x; 504 else 505 __is.setstate(ios_base::failbit); 506 } 507 else 508 { 509 __is.putback(__ch); 510 __is >> __re_x; 511 __x = __re_x; 512 } 513 return __is; 514 } 515 516 /// Insertion operator for complex values. 517 template<typename _Tp, typename _CharT, class _Traits> 518 basic_ostream<_CharT, _Traits>& 519 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 520 { 521 basic_ostringstream<_CharT, _Traits> __s; 522 __s.flags(__os.flags()); 523 __s.imbue(__os.getloc()); 524 __s.precision(__os.precision()); 525 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 526 return __os << __s.str(); 527 } 528 529 // Values 530 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 531 template<typename _Tp> 532 inline _Tp 533 real(const complex<_Tp>& __z) 534 { return __z.real(); } 535 536 template<typename _Tp> 537 inline _Tp 538 imag(const complex<_Tp>& __z) 539 { return __z.imag(); } 540 #else 541 template<typename _Tp> 542 inline _Tp& 543 real(complex<_Tp>& __z) 544 { return __z.real(); } 545 546 template<typename _Tp> 547 inline const _Tp& 548 real(const complex<_Tp>& __z) 549 { return __z.real(); } 550 551 template<typename _Tp> 552 inline _Tp& 553 imag(complex<_Tp>& __z) 554 { return __z.imag(); } 555 556 template<typename _Tp> 557 inline const _Tp& 558 imag(const complex<_Tp>& __z) 559 { return __z.imag(); } 560 #endif 561 562 // 26.2.7/3 abs(__z): Returns the magnitude of __z. 563 template<typename _Tp> 564 inline _Tp 565 __complex_abs(const complex<_Tp>& __z) 566 { 567 _Tp __x = __z.real(); 568 _Tp __y = __z.imag(); 569 const _Tp __s = std::max(abs(__x), abs(__y)); 570 if (__s == _Tp()) // well ... 571 return __s; 572 __x /= __s; 573 __y /= __s; 574 return __s * sqrt(__x * __x + __y * __y); 575 } 576 577 #if _GLIBCXX_USE_C99_COMPLEX 578 inline float 579 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } 580 581 inline double 582 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } 583 584 inline long double 585 __complex_abs(const __complex__ long double& __z) 586 { return __builtin_cabsl(__z); } 587 588 template<typename _Tp> 589 inline _Tp 590 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 591 #else 592 template<typename _Tp> 593 inline _Tp 594 abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 595 #endif 596 597 598 // 26.2.7/4: arg(__z): Returns the phase angle of __z. 599 template<typename _Tp> 600 inline _Tp 601 __complex_arg(const complex<_Tp>& __z) 602 { return atan2(__z.imag(), __z.real()); } 603 604 #if _GLIBCXX_USE_C99_COMPLEX 605 inline float 606 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 607 608 inline double 609 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 610 611 inline long double 612 __complex_arg(const __complex__ long double& __z) 613 { return __builtin_cargl(__z); } 614 615 template<typename _Tp> 616 inline _Tp 617 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 618 #else 619 template<typename _Tp> 620 inline _Tp 621 arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 622 #endif 623 624 // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 625 // As defined, norm() is -not- a norm is the common mathematical 626 // sens used in numerics. The helper class _Norm_helper<> tries to 627 // distinguish between builtin floating point and the rest, so as 628 // to deliver an answer as close as possible to the real value. 629 template<bool> 630 struct _Norm_helper 631 { 632 template<typename _Tp> 633 static inline _Tp _S_do_it(const complex<_Tp>& __z) 634 { 635 const _Tp __x = __z.real(); 636 const _Tp __y = __z.imag(); 637 return __x * __x + __y * __y; 638 } 639 }; 640 641 template<> 642 struct _Norm_helper<true> 643 { 644 template<typename _Tp> 645 static inline _Tp _S_do_it(const complex<_Tp>& __z) 646 { 647 _Tp __res = std::abs(__z); 648 return __res * __res; 649 } 650 }; 651 652 template<typename _Tp> 653 inline _Tp 654 norm(const complex<_Tp>& __z) 655 { 656 return _Norm_helper<__is_floating<_Tp>::__value 657 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 658 } 659 660 template<typename _Tp> 661 inline complex<_Tp> 662 polar(const _Tp& __rho, const _Tp& __theta) 663 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } 664 665 template<typename _Tp> 666 inline complex<_Tp> 667 conj(const complex<_Tp>& __z) 668 { return complex<_Tp>(__z.real(), -__z.imag()); } 669 670 // Transcendentals 671 672 // 26.2.8/1 cos(__z): Returns the cosine of __z. 673 template<typename _Tp> 674 inline complex<_Tp> 675 __complex_cos(const complex<_Tp>& __z) 676 { 677 const _Tp __x = __z.real(); 678 const _Tp __y = __z.imag(); 679 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 680 } 681 682 #if _GLIBCXX_USE_C99_COMPLEX 683 inline __complex__ float 684 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 685 686 inline __complex__ double 687 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 688 689 inline __complex__ long double 690 __complex_cos(const __complex__ long double& __z) 691 { return __builtin_ccosl(__z); } 692 693 template<typename _Tp> 694 inline complex<_Tp> 695 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 696 #else 697 template<typename _Tp> 698 inline complex<_Tp> 699 cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 700 #endif 701 702 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 703 template<typename _Tp> 704 inline complex<_Tp> 705 __complex_cosh(const complex<_Tp>& __z) 706 { 707 const _Tp __x = __z.real(); 708 const _Tp __y = __z.imag(); 709 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 710 } 711 712 #if _GLIBCXX_USE_C99_COMPLEX 713 inline __complex__ float 714 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 715 716 inline __complex__ double 717 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 718 719 inline __complex__ long double 720 __complex_cosh(const __complex__ long double& __z) 721 { return __builtin_ccoshl(__z); } 722 723 template<typename _Tp> 724 inline complex<_Tp> 725 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 726 #else 727 template<typename _Tp> 728 inline complex<_Tp> 729 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 730 #endif 731 732 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 733 template<typename _Tp> 734 inline complex<_Tp> 735 __complex_exp(const complex<_Tp>& __z) 736 { return std::polar(exp(__z.real()), __z.imag()); } 737 738 #if _GLIBCXX_USE_C99_COMPLEX 739 inline __complex__ float 740 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 741 742 inline __complex__ double 743 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 744 745 inline __complex__ long double 746 __complex_exp(const __complex__ long double& __z) 747 { return __builtin_cexpl(__z); } 748 749 template<typename _Tp> 750 inline complex<_Tp> 751 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 752 #else 753 template<typename _Tp> 754 inline complex<_Tp> 755 exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 756 #endif 757 758 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 759 // The branch cut is along the negative axis. 760 template<typename _Tp> 761 inline complex<_Tp> 762 __complex_log(const complex<_Tp>& __z) 763 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 764 765 #if _GLIBCXX_USE_C99_COMPLEX 766 inline __complex__ float 767 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 768 769 inline __complex__ double 770 __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 771 772 inline __complex__ long double 773 __complex_log(const __complex__ long double& __z) 774 { return __builtin_clogl(__z); } 775 776 template<typename _Tp> 777 inline complex<_Tp> 778 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 779 #else 780 template<typename _Tp> 781 inline complex<_Tp> 782 log(const complex<_Tp>& __z) { return __complex_log(__z); } 783 #endif 784 785 template<typename _Tp> 786 inline complex<_Tp> 787 log10(const complex<_Tp>& __z) 788 { return std::log(__z) / log(_Tp(10.0)); } 789 790 // 26.2.8/10 sin(__z): Returns the sine of __z. 791 template<typename _Tp> 792 inline complex<_Tp> 793 __complex_sin(const complex<_Tp>& __z) 794 { 795 const _Tp __x = __z.real(); 796 const _Tp __y = __z.imag(); 797 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 798 } 799 800 #if _GLIBCXX_USE_C99_COMPLEX 801 inline __complex__ float 802 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 803 804 inline __complex__ double 805 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 806 807 inline __complex__ long double 808 __complex_sin(const __complex__ long double& __z) 809 { return __builtin_csinl(__z); } 810 811 template<typename _Tp> 812 inline complex<_Tp> 813 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 814 #else 815 template<typename _Tp> 816 inline complex<_Tp> 817 sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 818 #endif 819 820 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 821 template<typename _Tp> 822 inline complex<_Tp> 823 __complex_sinh(const complex<_Tp>& __z) 824 { 825 const _Tp __x = __z.real(); 826 const _Tp __y = __z.imag(); 827 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 828 } 829 830 #if _GLIBCXX_USE_C99_COMPLEX 831 inline __complex__ float 832 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 833 834 inline __complex__ double 835 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 836 837 inline __complex__ long double 838 __complex_sinh(const __complex__ long double& __z) 839 { return __builtin_csinhl(__z); } 840 841 template<typename _Tp> 842 inline complex<_Tp> 843 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 844 #else 845 template<typename _Tp> 846 inline complex<_Tp> 847 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 848 #endif 849 850 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 851 // The branch cut is on the negative axis. 852 template<typename _Tp> 853 complex<_Tp> 854 __complex_sqrt(const complex<_Tp>& __z) 855 { 856 _Tp __x = __z.real(); 857 _Tp __y = __z.imag(); 858 859 if (__x == _Tp()) 860 { 861 _Tp __t = sqrt(abs(__y) / 2); 862 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 863 } 864 else 865 { 866 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 867 _Tp __u = __t / 2; 868 return __x > _Tp() 869 ? complex<_Tp>(__u, __y / __t) 870 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 871 } 872 } 873 874 #if _GLIBCXX_USE_C99_COMPLEX 875 inline __complex__ float 876 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 877 878 inline __complex__ double 879 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 880 881 inline __complex__ long double 882 __complex_sqrt(const __complex__ long double& __z) 883 { return __builtin_csqrtl(__z); } 884 885 template<typename _Tp> 886 inline complex<_Tp> 887 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 888 #else 889 template<typename _Tp> 890 inline complex<_Tp> 891 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 892 #endif 893 894 // 26.2.8/14 tan(__z): Return the complex tangent of __z. 895 896 template<typename _Tp> 897 inline complex<_Tp> 898 __complex_tan(const complex<_Tp>& __z) 899 { return std::sin(__z) / std::cos(__z); } 900 901 #if _GLIBCXX_USE_C99_COMPLEX 902 inline __complex__ float 903 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 904 905 inline __complex__ double 906 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 907 908 inline __complex__ long double 909 __complex_tan(const __complex__ long double& __z) 910 { return __builtin_ctanl(__z); } 911 912 template<typename _Tp> 913 inline complex<_Tp> 914 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 915 #else 916 template<typename _Tp> 917 inline complex<_Tp> 918 tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 919 #endif 920 921 922 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 923 924 template<typename _Tp> 925 inline complex<_Tp> 926 __complex_tanh(const complex<_Tp>& __z) 927 { return std::sinh(__z) / std::cosh(__z); } 928 929 #if _GLIBCXX_USE_C99_COMPLEX 930 inline __complex__ float 931 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 932 933 inline __complex__ double 934 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 935 936 inline __complex__ long double 937 __complex_tanh(const __complex__ long double& __z) 938 { return __builtin_ctanhl(__z); } 939 940 template<typename _Tp> 941 inline complex<_Tp> 942 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 943 #else 944 template<typename _Tp> 945 inline complex<_Tp> 946 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 947 #endif 948 949 950 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 951 // raised to the __y-th power. The branch 952 // cut is on the negative axis. 953 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 954 // _GLIBCXX_RESOLVE_LIB_DEFECTS 955 // DR 844. complex pow return type is ambiguous. 956 template<typename _Tp> 957 inline complex<_Tp> 958 pow(const complex<_Tp>& __z, int __n) 959 { return std::__pow_helper(__z, __n); } 960 #endif 961 962 template<typename _Tp> 963 complex<_Tp> 964 pow(const complex<_Tp>& __x, const _Tp& __y) 965 { 966 #ifndef _GLIBCXX_USE_C99_COMPLEX 967 if (__x == _Tp()) 968 return _Tp(); 969 #endif 970 if (__x.imag() == _Tp() && __x.real() > _Tp()) 971 return pow(__x.real(), __y); 972 973 complex<_Tp> __t = std::log(__x); 974 return std::polar(exp(__y * __t.real()), __y * __t.imag()); 975 } 976 977 template<typename _Tp> 978 inline complex<_Tp> 979 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 980 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 981 982 #if _GLIBCXX_USE_C99_COMPLEX 983 inline __complex__ float 984 __complex_pow(__complex__ float __x, __complex__ float __y) 985 { return __builtin_cpowf(__x, __y); } 986 987 inline __complex__ double 988 __complex_pow(__complex__ double __x, __complex__ double __y) 989 { return __builtin_cpow(__x, __y); } 990 991 inline __complex__ long double 992 __complex_pow(const __complex__ long double& __x, 993 const __complex__ long double& __y) 994 { return __builtin_cpowl(__x, __y); } 995 996 template<typename _Tp> 997 inline complex<_Tp> 998 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 999 { return __complex_pow(__x.__rep(), __y.__rep()); } 1000 #else 1001 template<typename _Tp> 1002 inline complex<_Tp> 1003 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 1004 { return __complex_pow(__x, __y); } 1005 #endif 1006 1007 template<typename _Tp> 1008 inline complex<_Tp> 1009 pow(const _Tp& __x, const complex<_Tp>& __y) 1010 { 1011 return __x > _Tp() ? std::polar(pow(__x, __y.real()), 1012 __y.imag() * log(__x)) 1013 : std::pow(complex<_Tp>(__x), __y); 1014 } 1015 1016 // 26.2.3 complex specializations 1017 // complex<float> specialization 1018 template<> 1019 struct complex<float> 1020 { 1021 typedef float value_type; 1022 typedef __complex__ float _ComplexT; 1023 1024 complex(_ComplexT __z) : _M_value(__z) { } 1025 1026 complex(float __r = 0.0f, float __i = 0.0f) 1027 { 1028 __real__ _M_value = __r; 1029 __imag__ _M_value = __i; 1030 } 1031 1032 explicit complex(const complex<double>&); 1033 explicit complex(const complex<long double>&); 1034 1035 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1036 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1037 // DR 387. std::complex over-encapsulated. 1038 float real() const 1039 { return __real__ _M_value; } 1040 1041 float imag() const 1042 { return __imag__ _M_value; } 1043 #else 1044 float& real() 1045 { return __real__ _M_value; } 1046 1047 const float& real() const 1048 { return __real__ _M_value; } 1049 1050 float& imag() 1051 { return __imag__ _M_value; } 1052 1053 const float& imag() const 1054 { return __imag__ _M_value; } 1055 #endif 1056 1057 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1058 // DR 387. std::complex over-encapsulated. 1059 void real(float __val) 1060 { __real__ _M_value = __val; } 1061 1062 void imag(float __val) 1063 { __imag__ _M_value = __val; } 1064 1065 complex<float>& 1066 operator=(float __f) 1067 { 1068 __real__ _M_value = __f; 1069 __imag__ _M_value = 0.0f; 1070 return *this; 1071 } 1072 1073 complex<float>& 1074 operator+=(float __f) 1075 { 1076 __real__ _M_value += __f; 1077 return *this; 1078 } 1079 1080 complex<float>& 1081 operator-=(float __f) 1082 { 1083 __real__ _M_value -= __f; 1084 return *this; 1085 } 1086 1087 complex<float>& 1088 operator*=(float __f) 1089 { 1090 _M_value *= __f; 1091 return *this; 1092 } 1093 1094 complex<float>& 1095 operator/=(float __f) 1096 { 1097 _M_value /= __f; 1098 return *this; 1099 } 1100 1101 // Let the compiler synthesize the copy and assignment 1102 // operator. It always does a pretty good job. 1103 // complex& operator=(const complex&); 1104 1105 template<typename _Tp> 1106 complex<float>& 1107 operator=(const complex<_Tp>& __z) 1108 { 1109 __real__ _M_value = __z.real(); 1110 __imag__ _M_value = __z.imag(); 1111 return *this; 1112 } 1113 1114 template<typename _Tp> 1115 complex<float>& 1116 operator+=(const complex<_Tp>& __z) 1117 { 1118 __real__ _M_value += __z.real(); 1119 __imag__ _M_value += __z.imag(); 1120 return *this; 1121 } 1122 1123 template<class _Tp> 1124 complex<float>& 1125 operator-=(const complex<_Tp>& __z) 1126 { 1127 __real__ _M_value -= __z.real(); 1128 __imag__ _M_value -= __z.imag(); 1129 return *this; 1130 } 1131 1132 template<class _Tp> 1133 complex<float>& 1134 operator*=(const complex<_Tp>& __z) 1135 { 1136 _ComplexT __t; 1137 __real__ __t = __z.real(); 1138 __imag__ __t = __z.imag(); 1139 _M_value *= __t; 1140 return *this; 1141 } 1142 1143 template<class _Tp> 1144 complex<float>& 1145 operator/=(const complex<_Tp>& __z) 1146 { 1147 _ComplexT __t; 1148 __real__ __t = __z.real(); 1149 __imag__ __t = __z.imag(); 1150 _M_value /= __t; 1151 return *this; 1152 } 1153 1154 const _ComplexT& __rep() const { return _M_value; } 1155 1156 private: 1157 _ComplexT _M_value; 1158 }; 1159 1160 // 26.2.3 complex specializations 1161 // complex<double> specialization 1162 template<> 1163 struct complex<double> 1164 { 1165 typedef double value_type; 1166 typedef __complex__ double _ComplexT; 1167 1168 complex(_ComplexT __z) : _M_value(__z) { } 1169 1170 complex(double __r = 0.0, double __i = 0.0) 1171 { 1172 __real__ _M_value = __r; 1173 __imag__ _M_value = __i; 1174 } 1175 1176 complex(const complex<float>& __z) 1177 : _M_value(__z.__rep()) { } 1178 1179 explicit complex(const complex<long double>&); 1180 1181 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1182 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1183 // DR 387. std::complex over-encapsulated. 1184 double real() const 1185 { return __real__ _M_value; } 1186 1187 double imag() const 1188 { return __imag__ _M_value; } 1189 #else 1190 double& real() 1191 { return __real__ _M_value; } 1192 1193 const double& real() const 1194 { return __real__ _M_value; } 1195 1196 double& imag() 1197 { return __imag__ _M_value; } 1198 1199 const double& imag() const 1200 { return __imag__ _M_value; } 1201 #endif 1202 1203 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1204 // DR 387. std::complex over-encapsulated. 1205 void real(double __val) 1206 { __real__ _M_value = __val; } 1207 1208 void imag(double __val) 1209 { __imag__ _M_value = __val; } 1210 1211 complex<double>& 1212 operator=(double __d) 1213 { 1214 __real__ _M_value = __d; 1215 __imag__ _M_value = 0.0; 1216 return *this; 1217 } 1218 1219 complex<double>& 1220 operator+=(double __d) 1221 { 1222 __real__ _M_value += __d; 1223 return *this; 1224 } 1225 1226 complex<double>& 1227 operator-=(double __d) 1228 { 1229 __real__ _M_value -= __d; 1230 return *this; 1231 } 1232 1233 complex<double>& 1234 operator*=(double __d) 1235 { 1236 _M_value *= __d; 1237 return *this; 1238 } 1239 1240 complex<double>& 1241 operator/=(double __d) 1242 { 1243 _M_value /= __d; 1244 return *this; 1245 } 1246 1247 // The compiler will synthesize this, efficiently. 1248 // complex& operator=(const complex&); 1249 1250 template<typename _Tp> 1251 complex<double>& 1252 operator=(const complex<_Tp>& __z) 1253 { 1254 __real__ _M_value = __z.real(); 1255 __imag__ _M_value = __z.imag(); 1256 return *this; 1257 } 1258 1259 template<typename _Tp> 1260 complex<double>& 1261 operator+=(const complex<_Tp>& __z) 1262 { 1263 __real__ _M_value += __z.real(); 1264 __imag__ _M_value += __z.imag(); 1265 return *this; 1266 } 1267 1268 template<typename _Tp> 1269 complex<double>& 1270 operator-=(const complex<_Tp>& __z) 1271 { 1272 __real__ _M_value -= __z.real(); 1273 __imag__ _M_value -= __z.imag(); 1274 return *this; 1275 } 1276 1277 template<typename _Tp> 1278 complex<double>& 1279 operator*=(const complex<_Tp>& __z) 1280 { 1281 _ComplexT __t; 1282 __real__ __t = __z.real(); 1283 __imag__ __t = __z.imag(); 1284 _M_value *= __t; 1285 return *this; 1286 } 1287 1288 template<typename _Tp> 1289 complex<double>& 1290 operator/=(const complex<_Tp>& __z) 1291 { 1292 _ComplexT __t; 1293 __real__ __t = __z.real(); 1294 __imag__ __t = __z.imag(); 1295 _M_value /= __t; 1296 return *this; 1297 } 1298 1299 const _ComplexT& __rep() const { return _M_value; } 1300 1301 private: 1302 _ComplexT _M_value; 1303 }; 1304 1305 // 26.2.3 complex specializations 1306 // complex<long double> specialization 1307 template<> 1308 struct complex<long double> 1309 { 1310 typedef long double value_type; 1311 typedef __complex__ long double _ComplexT; 1312 1313 complex(_ComplexT __z) : _M_value(__z) { } 1314 1315 complex(long double __r = 0.0L, long double __i = 0.0L) 1316 { 1317 __real__ _M_value = __r; 1318 __imag__ _M_value = __i; 1319 } 1320 1321 complex(const complex<float>& __z) 1322 : _M_value(__z.__rep()) { } 1323 1324 complex(const complex<double>& __z) 1325 : _M_value(__z.__rep()) { } 1326 1327 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1328 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1329 // DR 387. std::complex over-encapsulated. 1330 long double real() const 1331 { return __real__ _M_value; } 1332 1333 long double imag() const 1334 { return __imag__ _M_value; } 1335 #else 1336 long double& real() 1337 { return __real__ _M_value; } 1338 1339 const long double& real() const 1340 { return __real__ _M_value; } 1341 1342 long double& imag() 1343 { return __imag__ _M_value; } 1344 1345 const long double& imag() const 1346 { return __imag__ _M_value; } 1347 #endif 1348 1349 // _GLIBCXX_RESOLVE_LIB_DEFECTS 1350 // DR 387. std::complex over-encapsulated. 1351 void real(long double __val) 1352 { __real__ _M_value = __val; } 1353 1354 void imag(long double __val) 1355 { __imag__ _M_value = __val; } 1356 1357 complex<long double>& 1358 operator=(long double __r) 1359 { 1360 __real__ _M_value = __r; 1361 __imag__ _M_value = 0.0L; 1362 return *this; 1363 } 1364 1365 complex<long double>& 1366 operator+=(long double __r) 1367 { 1368 __real__ _M_value += __r; 1369 return *this; 1370 } 1371 1372 complex<long double>& 1373 operator-=(long double __r) 1374 { 1375 __real__ _M_value -= __r; 1376 return *this; 1377 } 1378 1379 complex<long double>& 1380 operator*=(long double __r) 1381 { 1382 _M_value *= __r; 1383 return *this; 1384 } 1385 1386 complex<long double>& 1387 operator/=(long double __r) 1388 { 1389 _M_value /= __r; 1390 return *this; 1391 } 1392 1393 // The compiler knows how to do this efficiently 1394 // complex& operator=(const complex&); 1395 1396 template<typename _Tp> 1397 complex<long double>& 1398 operator=(const complex<_Tp>& __z) 1399 { 1400 __real__ _M_value = __z.real(); 1401 __imag__ _M_value = __z.imag(); 1402 return *this; 1403 } 1404 1405 template<typename _Tp> 1406 complex<long double>& 1407 operator+=(const complex<_Tp>& __z) 1408 { 1409 __real__ _M_value += __z.real(); 1410 __imag__ _M_value += __z.imag(); 1411 return *this; 1412 } 1413 1414 template<typename _Tp> 1415 complex<long double>& 1416 operator-=(const complex<_Tp>& __z) 1417 { 1418 __real__ _M_value -= __z.real(); 1419 __imag__ _M_value -= __z.imag(); 1420 return *this; 1421 } 1422 1423 template<typename _Tp> 1424 complex<long double>& 1425 operator*=(const complex<_Tp>& __z) 1426 { 1427 _ComplexT __t; 1428 __real__ __t = __z.real(); 1429 __imag__ __t = __z.imag(); 1430 _M_value *= __t; 1431 return *this; 1432 } 1433 1434 template<typename _Tp> 1435 complex<long double>& 1436 operator/=(const complex<_Tp>& __z) 1437 { 1438 _ComplexT __t; 1439 __real__ __t = __z.real(); 1440 __imag__ __t = __z.imag(); 1441 _M_value /= __t; 1442 return *this; 1443 } 1444 1445 const _ComplexT& __rep() const { return _M_value; } 1446 1447 private: 1448 _ComplexT _M_value; 1449 }; 1450 1451 // These bits have to be at the end of this file, so that the 1452 // specializations have all been defined. 1453 inline 1454 complex<float>::complex(const complex<double>& __z) 1455 : _M_value(__z.__rep()) { } 1456 1457 inline 1458 complex<float>::complex(const complex<long double>& __z) 1459 : _M_value(__z.__rep()) { } 1460 1461 inline 1462 complex<double>::complex(const complex<long double>& __z) 1463 : _M_value(__z.__rep()) { } 1464 1465 // Inhibit implicit instantiations for required instantiations, 1466 // which are defined via explicit instantiations elsewhere. 1467 // NB: This syntax is a GNU extension. 1468 #if _GLIBCXX_EXTERN_TEMPLATE 1469 extern template istream& operator>>(istream&, complex<float>&); 1470 extern template ostream& operator<<(ostream&, const complex<float>&); 1471 extern template istream& operator>>(istream&, complex<double>&); 1472 extern template ostream& operator<<(ostream&, const complex<double>&); 1473 extern template istream& operator>>(istream&, complex<long double>&); 1474 extern template ostream& operator<<(ostream&, const complex<long double>&); 1475 1476 #ifdef _GLIBCXX_USE_WCHAR_T 1477 extern template wistream& operator>>(wistream&, complex<float>&); 1478 extern template wostream& operator<<(wostream&, const complex<float>&); 1479 extern template wistream& operator>>(wistream&, complex<double>&); 1480 extern template wostream& operator<<(wostream&, const complex<double>&); 1481 extern template wistream& operator>>(wistream&, complex<long double>&); 1482 extern template wostream& operator<<(wostream&, const complex<long double>&); 1483 #endif 1484 #endif 1485 1486 // @} group complex_numbers 1487 1488 _GLIBCXX_END_NAMESPACE 1489 1490 _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) 1491 1492 // See ext/type_traits.h for the primary template. 1493 template<typename _Tp, typename _Up> 1494 struct __promote_2<std::complex<_Tp>, _Up> 1495 { 1496 public: 1497 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1498 }; 1499 1500 template<typename _Tp, typename _Up> 1501 struct __promote_2<_Tp, std::complex<_Up> > 1502 { 1503 public: 1504 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1505 }; 1506 1507 template<typename _Tp, typename _Up> 1508 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 1509 { 1510 public: 1511 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 1512 }; 1513 1514 _GLIBCXX_END_NAMESPACE 1515 1516 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 1517 # if defined(_GLIBCXX_INCLUDE_AS_TR1) 1518 # error C++0x header cannot be included from TR1 header 1519 # endif 1520 # if defined(_GLIBCXX_INCLUDE_AS_CXX0X) 1521 # include <tr1_impl/complex> 1522 # else 1523 # define _GLIBCXX_INCLUDE_AS_CXX0X 1524 # define _GLIBCXX_BEGIN_NAMESPACE_TR1 1525 # define _GLIBCXX_END_NAMESPACE_TR1 1526 # define _GLIBCXX_TR1 1527 # include <tr1_impl/complex> 1528 # undef _GLIBCXX_TR1 1529 # undef _GLIBCXX_END_NAMESPACE_TR1 1530 # undef _GLIBCXX_BEGIN_NAMESPACE_TR1 1531 # undef _GLIBCXX_INCLUDE_AS_CXX0X 1532 # endif 1533 1534 _GLIBCXX_BEGIN_NAMESPACE(std) 1535 1536 // Forward declarations. 1537 // DR 781. 1538 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 1539 1540 template<typename _Tp> 1541 std::complex<_Tp> 1542 __complex_proj(const std::complex<_Tp>& __z) 1543 { 1544 const _Tp __den = (__z.real() * __z.real() 1545 + __z.imag() * __z.imag() + _Tp(1.0)); 1546 1547 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 1548 (_Tp(2.0) * __z.imag()) / __den); 1549 } 1550 1551 #if _GLIBCXX_USE_C99_COMPLEX 1552 inline __complex__ float 1553 __complex_proj(__complex__ float __z) 1554 { return __builtin_cprojf(__z); } 1555 1556 inline __complex__ double 1557 __complex_proj(__complex__ double __z) 1558 { return __builtin_cproj(__z); } 1559 1560 inline __complex__ long double 1561 __complex_proj(const __complex__ long double& __z) 1562 { return __builtin_cprojl(__z); } 1563 1564 template<typename _Tp> 1565 inline std::complex<_Tp> 1566 proj(const std::complex<_Tp>& __z) 1567 { return __complex_proj(__z.__rep()); } 1568 #else 1569 template<typename _Tp> 1570 inline std::complex<_Tp> 1571 proj(const std::complex<_Tp>& __z) 1572 { return __complex_proj(__z); } 1573 #endif 1574 1575 template<typename _Tp> 1576 inline std::complex<typename __gnu_cxx::__promote<_Tp>::__type> 1577 proj(_Tp __x) 1578 { 1579 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 1580 return std::proj(std::complex<__type>(__x)); 1581 } 1582 1583 _GLIBCXX_END_NAMESPACE 1584 1585 #endif 1586 1587 #endif /* _GLIBCXX_COMPLEX */ 1588