|
libstdc++
|
00001 // The template and inlines for the -*- C++ -*- complex number classes. 00002 00003 // Copyright (C) 1997-2013 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/complex 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 // 00030 // ISO C++ 14882: 26.2 Complex Numbers 00031 // Note: this is not a conforming implementation. 00032 // Initially implemented by Ulrich Drepper <drepper@cygnus.com> 00033 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> 00034 // 00035 00036 #ifndef _GLIBCXX_COMPLEX 00037 #define _GLIBCXX_COMPLEX 1 00038 00039 #pragma GCC system_header 00040 00041 #include <bits/c++config.h> 00042 #include <bits/cpp_type_traits.h> 00043 #include <ext/type_traits.h> 00044 #include <cmath> 00045 #include <sstream> 00046 00047 namespace std _GLIBCXX_VISIBILITY(default) 00048 { 00049 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00050 00051 /** 00052 * @defgroup complex_numbers Complex Numbers 00053 * @ingroup numerics 00054 * 00055 * Classes and functions for complex numbers. 00056 * @{ 00057 */ 00058 00059 // Forward declarations. 00060 template<typename _Tp> class complex; 00061 template<> class complex<float>; 00062 template<> class complex<double>; 00063 template<> class complex<long double>; 00064 00065 /// Return magnitude of @a z. 00066 template<typename _Tp> _Tp abs(const complex<_Tp>&); 00067 /// Return phase angle of @a z. 00068 template<typename _Tp> _Tp arg(const complex<_Tp>&); 00069 /// Return @a z magnitude squared. 00070 template<typename _Tp> _Tp norm(const complex<_Tp>&); 00071 00072 /// Return complex conjugate of @a z. 00073 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); 00074 /// Return complex with magnitude @a rho and angle @a theta. 00075 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 00076 00077 // Transcendentals: 00078 /// Return complex cosine of @a z. 00079 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 00080 /// Return complex hyperbolic cosine of @a z. 00081 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 00082 /// Return complex base e exponential of @a z. 00083 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 00084 /// Return complex natural logarithm of @a z. 00085 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 00086 /// Return complex base 10 logarithm of @a z. 00087 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 00088 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00089 // DR 844. 00090 /// Return @a x to the @a y'th power. 00091 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 00092 #endif 00093 /// Return @a x to the @a y'th power. 00094 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 00095 /// Return @a x to the @a y'th power. 00096 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 00097 const complex<_Tp>&); 00098 /// Return @a x to the @a y'th power. 00099 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 00100 /// Return complex sine of @a z. 00101 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 00102 /// Return complex hyperbolic sine of @a z. 00103 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 00104 /// Return complex square root of @a z. 00105 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 00106 /// Return complex tangent of @a z. 00107 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 00108 /// Return complex hyperbolic tangent of @a z. 00109 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 00110 00111 00112 // 26.2.2 Primary template class complex 00113 /** 00114 * Template to represent complex numbers. 00115 * 00116 * Specializations for float, double, and long double are part of the 00117 * library. Results with any other type are not guaranteed. 00118 * 00119 * @param Tp Type of real and imaginary values. 00120 */ 00121 template<typename _Tp> 00122 struct complex 00123 { 00124 /// Value typedef. 00125 typedef _Tp value_type; 00126 00127 /// Default constructor. First parameter is x, second parameter is y. 00128 /// Unspecified parameters default to 0. 00129 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) 00130 : _M_real(__r), _M_imag(__i) { } 00131 00132 // Lets the compiler synthesize the copy constructor 00133 // complex (const complex<_Tp>&); 00134 /// Copy constructor. 00135 template<typename _Up> 00136 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) 00137 : _M_real(__z.real()), _M_imag(__z.imag()) { } 00138 00139 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00140 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00141 // DR 387. std::complex over-encapsulated. 00142 constexpr _Tp 00143 real() { return _M_real; } 00144 00145 constexpr _Tp 00146 imag() { return _M_imag; } 00147 #else 00148 /// Return real part of complex number. 00149 _Tp& 00150 real() { return _M_real; } 00151 00152 /// Return real part of complex number. 00153 const _Tp& 00154 real() const { return _M_real; } 00155 00156 /// Return imaginary part of complex number. 00157 _Tp& 00158 imag() { return _M_imag; } 00159 00160 /// Return imaginary part of complex number. 00161 const _Tp& 00162 imag() const { return _M_imag; } 00163 #endif 00164 00165 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00166 // DR 387. std::complex over-encapsulated. 00167 void 00168 real(_Tp __val) { _M_real = __val; } 00169 00170 void 00171 imag(_Tp __val) { _M_imag = __val; } 00172 00173 /// Assign this complex number to scalar @a t. 00174 complex<_Tp>& operator=(const _Tp&); 00175 00176 /// Add @a t to this complex number. 00177 // 26.2.5/1 00178 complex<_Tp>& 00179 operator+=(const _Tp& __t) 00180 { 00181 _M_real += __t; 00182 return *this; 00183 } 00184 00185 /// Subtract @a t from this complex number. 00186 // 26.2.5/3 00187 complex<_Tp>& 00188 operator-=(const _Tp& __t) 00189 { 00190 _M_real -= __t; 00191 return *this; 00192 } 00193 00194 /// Multiply this complex number by @a t. 00195 complex<_Tp>& operator*=(const _Tp&); 00196 /// Divide this complex number by @a t. 00197 complex<_Tp>& operator/=(const _Tp&); 00198 00199 // Lets the compiler synthesize the 00200 // copy and assignment operator 00201 // complex<_Tp>& operator= (const complex<_Tp>&); 00202 /// Assign this complex number to complex @a z. 00203 template<typename _Up> 00204 complex<_Tp>& operator=(const complex<_Up>&); 00205 /// Add @a z to this complex number. 00206 template<typename _Up> 00207 complex<_Tp>& operator+=(const complex<_Up>&); 00208 /// Subtract @a z from this complex number. 00209 template<typename _Up> 00210 complex<_Tp>& operator-=(const complex<_Up>&); 00211 /// Multiply this complex number by @a z. 00212 template<typename _Up> 00213 complex<_Tp>& operator*=(const complex<_Up>&); 00214 /// Divide this complex number by @a z. 00215 template<typename _Up> 00216 complex<_Tp>& operator/=(const complex<_Up>&); 00217 00218 _GLIBCXX_USE_CONSTEXPR complex __rep() const 00219 { return *this; } 00220 00221 private: 00222 _Tp _M_real; 00223 _Tp _M_imag; 00224 }; 00225 00226 template<typename _Tp> 00227 complex<_Tp>& 00228 complex<_Tp>::operator=(const _Tp& __t) 00229 { 00230 _M_real = __t; 00231 _M_imag = _Tp(); 00232 return *this; 00233 } 00234 00235 // 26.2.5/5 00236 template<typename _Tp> 00237 complex<_Tp>& 00238 complex<_Tp>::operator*=(const _Tp& __t) 00239 { 00240 _M_real *= __t; 00241 _M_imag *= __t; 00242 return *this; 00243 } 00244 00245 // 26.2.5/7 00246 template<typename _Tp> 00247 complex<_Tp>& 00248 complex<_Tp>::operator/=(const _Tp& __t) 00249 { 00250 _M_real /= __t; 00251 _M_imag /= __t; 00252 return *this; 00253 } 00254 00255 template<typename _Tp> 00256 template<typename _Up> 00257 complex<_Tp>& 00258 complex<_Tp>::operator=(const complex<_Up>& __z) 00259 { 00260 _M_real = __z.real(); 00261 _M_imag = __z.imag(); 00262 return *this; 00263 } 00264 00265 // 26.2.5/9 00266 template<typename _Tp> 00267 template<typename _Up> 00268 complex<_Tp>& 00269 complex<_Tp>::operator+=(const complex<_Up>& __z) 00270 { 00271 _M_real += __z.real(); 00272 _M_imag += __z.imag(); 00273 return *this; 00274 } 00275 00276 // 26.2.5/11 00277 template<typename _Tp> 00278 template<typename _Up> 00279 complex<_Tp>& 00280 complex<_Tp>::operator-=(const complex<_Up>& __z) 00281 { 00282 _M_real -= __z.real(); 00283 _M_imag -= __z.imag(); 00284 return *this; 00285 } 00286 00287 // 26.2.5/13 00288 // XXX: This is a grammar school implementation. 00289 template<typename _Tp> 00290 template<typename _Up> 00291 complex<_Tp>& 00292 complex<_Tp>::operator*=(const complex<_Up>& __z) 00293 { 00294 const _Tp __r = _M_real * __z.real() - _M_imag * __z.imag(); 00295 _M_imag = _M_real * __z.imag() + _M_imag * __z.real(); 00296 _M_real = __r; 00297 return *this; 00298 } 00299 00300 // 26.2.5/15 00301 // XXX: This is a grammar school implementation. 00302 template<typename _Tp> 00303 template<typename _Up> 00304 complex<_Tp>& 00305 complex<_Tp>::operator/=(const complex<_Up>& __z) 00306 { 00307 const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); 00308 const _Tp __n = std::norm(__z); 00309 _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; 00310 _M_real = __r / __n; 00311 return *this; 00312 } 00313 00314 // Operators: 00315 //@{ 00316 /// Return new complex value @a x plus @a y. 00317 template<typename _Tp> 00318 inline complex<_Tp> 00319 operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) 00320 { 00321 complex<_Tp> __r = __x; 00322 __r += __y; 00323 return __r; 00324 } 00325 00326 template<typename _Tp> 00327 inline complex<_Tp> 00328 operator+(const complex<_Tp>& __x, const _Tp& __y) 00329 { 00330 complex<_Tp> __r = __x; 00331 __r += __y; 00332 return __r; 00333 } 00334 00335 template<typename _Tp> 00336 inline complex<_Tp> 00337 operator+(const _Tp& __x, const complex<_Tp>& __y) 00338 { 00339 complex<_Tp> __r = __y; 00340 __r += __x; 00341 return __r; 00342 } 00343 //@} 00344 00345 //@{ 00346 /// Return new complex value @a x minus @a y. 00347 template<typename _Tp> 00348 inline complex<_Tp> 00349 operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) 00350 { 00351 complex<_Tp> __r = __x; 00352 __r -= __y; 00353 return __r; 00354 } 00355 00356 template<typename _Tp> 00357 inline complex<_Tp> 00358 operator-(const complex<_Tp>& __x, const _Tp& __y) 00359 { 00360 complex<_Tp> __r = __x; 00361 __r -= __y; 00362 return __r; 00363 } 00364 00365 template<typename _Tp> 00366 inline complex<_Tp> 00367 operator-(const _Tp& __x, const complex<_Tp>& __y) 00368 { 00369 complex<_Tp> __r(__x, -__y.imag()); 00370 __r -= __y.real(); 00371 return __r; 00372 } 00373 //@} 00374 00375 //@{ 00376 /// Return new complex value @a x times @a y. 00377 template<typename _Tp> 00378 inline complex<_Tp> 00379 operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) 00380 { 00381 complex<_Tp> __r = __x; 00382 __r *= __y; 00383 return __r; 00384 } 00385 00386 template<typename _Tp> 00387 inline complex<_Tp> 00388 operator*(const complex<_Tp>& __x, const _Tp& __y) 00389 { 00390 complex<_Tp> __r = __x; 00391 __r *= __y; 00392 return __r; 00393 } 00394 00395 template<typename _Tp> 00396 inline complex<_Tp> 00397 operator*(const _Tp& __x, const complex<_Tp>& __y) 00398 { 00399 complex<_Tp> __r = __y; 00400 __r *= __x; 00401 return __r; 00402 } 00403 //@} 00404 00405 //@{ 00406 /// Return new complex value @a x divided by @a y. 00407 template<typename _Tp> 00408 inline complex<_Tp> 00409 operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) 00410 { 00411 complex<_Tp> __r = __x; 00412 __r /= __y; 00413 return __r; 00414 } 00415 00416 template<typename _Tp> 00417 inline complex<_Tp> 00418 operator/(const complex<_Tp>& __x, const _Tp& __y) 00419 { 00420 complex<_Tp> __r = __x; 00421 __r /= __y; 00422 return __r; 00423 } 00424 00425 template<typename _Tp> 00426 inline complex<_Tp> 00427 operator/(const _Tp& __x, const complex<_Tp>& __y) 00428 { 00429 complex<_Tp> __r = __x; 00430 __r /= __y; 00431 return __r; 00432 } 00433 //@} 00434 00435 /// Return @a x. 00436 template<typename _Tp> 00437 inline complex<_Tp> 00438 operator+(const complex<_Tp>& __x) 00439 { return __x; } 00440 00441 /// Return complex negation of @a x. 00442 template<typename _Tp> 00443 inline complex<_Tp> 00444 operator-(const complex<_Tp>& __x) 00445 { return complex<_Tp>(-__x.real(), -__x.imag()); } 00446 00447 //@{ 00448 /// Return true if @a x is equal to @a y. 00449 template<typename _Tp> 00450 inline _GLIBCXX_CONSTEXPR bool 00451 operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) 00452 { return __x.real() == __y.real() && __x.imag() == __y.imag(); } 00453 00454 template<typename _Tp> 00455 inline _GLIBCXX_CONSTEXPR bool 00456 operator==(const complex<_Tp>& __x, const _Tp& __y) 00457 { return __x.real() == __y && __x.imag() == _Tp(); } 00458 00459 template<typename _Tp> 00460 inline _GLIBCXX_CONSTEXPR bool 00461 operator==(const _Tp& __x, const complex<_Tp>& __y) 00462 { return __x == __y.real() && _Tp() == __y.imag(); } 00463 //@} 00464 00465 //@{ 00466 /// Return false if @a x is equal to @a y. 00467 template<typename _Tp> 00468 inline _GLIBCXX_CONSTEXPR bool 00469 operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) 00470 { return __x.real() != __y.real() || __x.imag() != __y.imag(); } 00471 00472 template<typename _Tp> 00473 inline _GLIBCXX_CONSTEXPR bool 00474 operator!=(const complex<_Tp>& __x, const _Tp& __y) 00475 { return __x.real() != __y || __x.imag() != _Tp(); } 00476 00477 template<typename _Tp> 00478 inline _GLIBCXX_CONSTEXPR bool 00479 operator!=(const _Tp& __x, const complex<_Tp>& __y) 00480 { return __x != __y.real() || _Tp() != __y.imag(); } 00481 //@} 00482 00483 /// Extraction operator for complex values. 00484 template<typename _Tp, typename _CharT, class _Traits> 00485 basic_istream<_CharT, _Traits>& 00486 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) 00487 { 00488 _Tp __re_x, __im_x; 00489 _CharT __ch; 00490 __is >> __ch; 00491 if (__ch == '(') 00492 { 00493 __is >> __re_x >> __ch; 00494 if (__ch == ',') 00495 { 00496 __is >> __im_x >> __ch; 00497 if (__ch == ')') 00498 __x = complex<_Tp>(__re_x, __im_x); 00499 else 00500 __is.setstate(ios_base::failbit); 00501 } 00502 else if (__ch == ')') 00503 __x = __re_x; 00504 else 00505 __is.setstate(ios_base::failbit); 00506 } 00507 else 00508 { 00509 __is.putback(__ch); 00510 __is >> __re_x; 00511 __x = __re_x; 00512 } 00513 return __is; 00514 } 00515 00516 /// Insertion operator for complex values. 00517 template<typename _Tp, typename _CharT, class _Traits> 00518 basic_ostream<_CharT, _Traits>& 00519 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 00520 { 00521 basic_ostringstream<_CharT, _Traits> __s; 00522 __s.flags(__os.flags()); 00523 __s.imbue(__os.getloc()); 00524 __s.precision(__os.precision()); 00525 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 00526 return __os << __s.str(); 00527 } 00528 00529 // Values 00530 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00531 template<typename _Tp> 00532 constexpr _Tp 00533 real(const complex<_Tp>& __z) 00534 { return __z.real(); } 00535 00536 template<typename _Tp> 00537 constexpr _Tp 00538 imag(const complex<_Tp>& __z) 00539 { return __z.imag(); } 00540 #else 00541 template<typename _Tp> 00542 inline _Tp& 00543 real(complex<_Tp>& __z) 00544 { return __z.real(); } 00545 00546 template<typename _Tp> 00547 inline const _Tp& 00548 real(const complex<_Tp>& __z) 00549 { return __z.real(); } 00550 00551 template<typename _Tp> 00552 inline _Tp& 00553 imag(complex<_Tp>& __z) 00554 { return __z.imag(); } 00555 00556 template<typename _Tp> 00557 inline const _Tp& 00558 imag(const complex<_Tp>& __z) 00559 { return __z.imag(); } 00560 #endif 00561 00562 // 26.2.7/3 abs(__z): Returns the magnitude of __z. 00563 template<typename _Tp> 00564 inline _Tp 00565 __complex_abs(const complex<_Tp>& __z) 00566 { 00567 _Tp __x = __z.real(); 00568 _Tp __y = __z.imag(); 00569 const _Tp __s = std::max(abs(__x), abs(__y)); 00570 if (__s == _Tp()) // well ... 00571 return __s; 00572 __x /= __s; 00573 __y /= __s; 00574 return __s * sqrt(__x * __x + __y * __y); 00575 } 00576 00577 #if _GLIBCXX_USE_C99_COMPLEX 00578 inline float 00579 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } 00580 00581 inline double 00582 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } 00583 00584 inline long double 00585 __complex_abs(const __complex__ long double& __z) 00586 { return __builtin_cabsl(__z); } 00587 00588 template<typename _Tp> 00589 inline _Tp 00590 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 00591 #else 00592 template<typename _Tp> 00593 inline _Tp 00594 abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 00595 #endif 00596 00597 00598 // 26.2.7/4: arg(__z): Returns the phase angle of __z. 00599 template<typename _Tp> 00600 inline _Tp 00601 __complex_arg(const complex<_Tp>& __z) 00602 { return atan2(__z.imag(), __z.real()); } 00603 00604 #if _GLIBCXX_USE_C99_COMPLEX 00605 inline float 00606 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 00607 00608 inline double 00609 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 00610 00611 inline long double 00612 __complex_arg(const __complex__ long double& __z) 00613 { return __builtin_cargl(__z); } 00614 00615 template<typename _Tp> 00616 inline _Tp 00617 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 00618 #else 00619 template<typename _Tp> 00620 inline _Tp 00621 arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 00622 #endif 00623 00624 // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 00625 // As defined, norm() is -not- a norm is the common mathematical 00626 // sens used in numerics. The helper class _Norm_helper<> tries to 00627 // distinguish between builtin floating point and the rest, so as 00628 // to deliver an answer as close as possible to the real value. 00629 template<bool> 00630 struct _Norm_helper 00631 { 00632 template<typename _Tp> 00633 static inline _Tp _S_do_it(const complex<_Tp>& __z) 00634 { 00635 const _Tp __x = __z.real(); 00636 const _Tp __y = __z.imag(); 00637 return __x * __x + __y * __y; 00638 } 00639 }; 00640 00641 template<> 00642 struct _Norm_helper<true> 00643 { 00644 template<typename _Tp> 00645 static inline _Tp _S_do_it(const complex<_Tp>& __z) 00646 { 00647 _Tp __res = std::abs(__z); 00648 return __res * __res; 00649 } 00650 }; 00651 00652 template<typename _Tp> 00653 inline _Tp 00654 norm(const complex<_Tp>& __z) 00655 { 00656 return _Norm_helper<__is_floating<_Tp>::__value 00657 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 00658 } 00659 00660 template<typename _Tp> 00661 inline complex<_Tp> 00662 polar(const _Tp& __rho, const _Tp& __theta) 00663 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } 00664 00665 template<typename _Tp> 00666 inline complex<_Tp> 00667 conj(const complex<_Tp>& __z) 00668 { return complex<_Tp>(__z.real(), -__z.imag()); } 00669 00670 // Transcendentals 00671 00672 // 26.2.8/1 cos(__z): Returns the cosine of __z. 00673 template<typename _Tp> 00674 inline complex<_Tp> 00675 __complex_cos(const complex<_Tp>& __z) 00676 { 00677 const _Tp __x = __z.real(); 00678 const _Tp __y = __z.imag(); 00679 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 00680 } 00681 00682 #if _GLIBCXX_USE_C99_COMPLEX 00683 inline __complex__ float 00684 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 00685 00686 inline __complex__ double 00687 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 00688 00689 inline __complex__ long double 00690 __complex_cos(const __complex__ long double& __z) 00691 { return __builtin_ccosl(__z); } 00692 00693 template<typename _Tp> 00694 inline complex<_Tp> 00695 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 00696 #else 00697 template<typename _Tp> 00698 inline complex<_Tp> 00699 cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 00700 #endif 00701 00702 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 00703 template<typename _Tp> 00704 inline complex<_Tp> 00705 __complex_cosh(const complex<_Tp>& __z) 00706 { 00707 const _Tp __x = __z.real(); 00708 const _Tp __y = __z.imag(); 00709 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 00710 } 00711 00712 #if _GLIBCXX_USE_C99_COMPLEX 00713 inline __complex__ float 00714 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 00715 00716 inline __complex__ double 00717 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 00718 00719 inline __complex__ long double 00720 __complex_cosh(const __complex__ long double& __z) 00721 { return __builtin_ccoshl(__z); } 00722 00723 template<typename _Tp> 00724 inline complex<_Tp> 00725 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 00726 #else 00727 template<typename _Tp> 00728 inline complex<_Tp> 00729 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 00730 #endif 00731 00732 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 00733 template<typename _Tp> 00734 inline complex<_Tp> 00735 __complex_exp(const complex<_Tp>& __z) 00736 { return std::polar(exp(__z.real()), __z.imag()); } 00737 00738 #if _GLIBCXX_USE_C99_COMPLEX 00739 inline __complex__ float 00740 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 00741 00742 inline __complex__ double 00743 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 00744 00745 inline __complex__ long double 00746 __complex_exp(const __complex__ long double& __z) 00747 { return __builtin_cexpl(__z); } 00748 00749 template<typename _Tp> 00750 inline complex<_Tp> 00751 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 00752 #else 00753 template<typename _Tp> 00754 inline complex<_Tp> 00755 exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 00756 #endif 00757 00758 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 00759 // The branch cut is along the negative axis. 00760 template<typename _Tp> 00761 inline complex<_Tp> 00762 __complex_log(const complex<_Tp>& __z) 00763 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 00764 00765 #if _GLIBCXX_USE_C99_COMPLEX 00766 inline __complex__ float 00767 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 00768 00769 inline __complex__ double 00770 __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 00771 00772 inline __complex__ long double 00773 __complex_log(const __complex__ long double& __z) 00774 { return __builtin_clogl(__z); } 00775 00776 template<typename _Tp> 00777 inline complex<_Tp> 00778 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 00779 #else 00780 template<typename _Tp> 00781 inline complex<_Tp> 00782 log(const complex<_Tp>& __z) { return __complex_log(__z); } 00783 #endif 00784 00785 template<typename _Tp> 00786 inline complex<_Tp> 00787 log10(const complex<_Tp>& __z) 00788 { return std::log(__z) / log(_Tp(10.0)); } 00789 00790 // 26.2.8/10 sin(__z): Returns the sine of __z. 00791 template<typename _Tp> 00792 inline complex<_Tp> 00793 __complex_sin(const complex<_Tp>& __z) 00794 { 00795 const _Tp __x = __z.real(); 00796 const _Tp __y = __z.imag(); 00797 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 00798 } 00799 00800 #if _GLIBCXX_USE_C99_COMPLEX 00801 inline __complex__ float 00802 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 00803 00804 inline __complex__ double 00805 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 00806 00807 inline __complex__ long double 00808 __complex_sin(const __complex__ long double& __z) 00809 { return __builtin_csinl(__z); } 00810 00811 template<typename _Tp> 00812 inline complex<_Tp> 00813 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 00814 #else 00815 template<typename _Tp> 00816 inline complex<_Tp> 00817 sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 00818 #endif 00819 00820 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 00821 template<typename _Tp> 00822 inline complex<_Tp> 00823 __complex_sinh(const complex<_Tp>& __z) 00824 { 00825 const _Tp __x = __z.real(); 00826 const _Tp __y = __z.imag(); 00827 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 00828 } 00829 00830 #if _GLIBCXX_USE_C99_COMPLEX 00831 inline __complex__ float 00832 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 00833 00834 inline __complex__ double 00835 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 00836 00837 inline __complex__ long double 00838 __complex_sinh(const __complex__ long double& __z) 00839 { return __builtin_csinhl(__z); } 00840 00841 template<typename _Tp> 00842 inline complex<_Tp> 00843 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 00844 #else 00845 template<typename _Tp> 00846 inline complex<_Tp> 00847 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 00848 #endif 00849 00850 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 00851 // The branch cut is on the negative axis. 00852 template<typename _Tp> 00853 complex<_Tp> 00854 __complex_sqrt(const complex<_Tp>& __z) 00855 { 00856 _Tp __x = __z.real(); 00857 _Tp __y = __z.imag(); 00858 00859 if (__x == _Tp()) 00860 { 00861 _Tp __t = sqrt(abs(__y) / 2); 00862 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 00863 } 00864 else 00865 { 00866 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 00867 _Tp __u = __t / 2; 00868 return __x > _Tp() 00869 ? complex<_Tp>(__u, __y / __t) 00870 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 00871 } 00872 } 00873 00874 #if _GLIBCXX_USE_C99_COMPLEX 00875 inline __complex__ float 00876 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 00877 00878 inline __complex__ double 00879 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 00880 00881 inline __complex__ long double 00882 __complex_sqrt(const __complex__ long double& __z) 00883 { return __builtin_csqrtl(__z); } 00884 00885 template<typename _Tp> 00886 inline complex<_Tp> 00887 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 00888 #else 00889 template<typename _Tp> 00890 inline complex<_Tp> 00891 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 00892 #endif 00893 00894 // 26.2.8/14 tan(__z): Return the complex tangent of __z. 00895 00896 template<typename _Tp> 00897 inline complex<_Tp> 00898 __complex_tan(const complex<_Tp>& __z) 00899 { return std::sin(__z) / std::cos(__z); } 00900 00901 #if _GLIBCXX_USE_C99_COMPLEX 00902 inline __complex__ float 00903 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 00904 00905 inline __complex__ double 00906 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 00907 00908 inline __complex__ long double 00909 __complex_tan(const __complex__ long double& __z) 00910 { return __builtin_ctanl(__z); } 00911 00912 template<typename _Tp> 00913 inline complex<_Tp> 00914 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 00915 #else 00916 template<typename _Tp> 00917 inline complex<_Tp> 00918 tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 00919 #endif 00920 00921 00922 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 00923 00924 template<typename _Tp> 00925 inline complex<_Tp> 00926 __complex_tanh(const complex<_Tp>& __z) 00927 { return std::sinh(__z) / std::cosh(__z); } 00928 00929 #if _GLIBCXX_USE_C99_COMPLEX 00930 inline __complex__ float 00931 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 00932 00933 inline __complex__ double 00934 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 00935 00936 inline __complex__ long double 00937 __complex_tanh(const __complex__ long double& __z) 00938 { return __builtin_ctanhl(__z); } 00939 00940 template<typename _Tp> 00941 inline complex<_Tp> 00942 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 00943 #else 00944 template<typename _Tp> 00945 inline complex<_Tp> 00946 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 00947 #endif 00948 00949 00950 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 00951 // raised to the __y-th power. The branch 00952 // cut is on the negative axis. 00953 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00954 template<typename _Tp> 00955 complex<_Tp> 00956 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 00957 { 00958 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 00959 00960 while (__n >>= 1) 00961 { 00962 __x *= __x; 00963 if (__n % 2) 00964 __y *= __x; 00965 } 00966 00967 return __y; 00968 } 00969 00970 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00971 // DR 844. complex pow return type is ambiguous. 00972 template<typename _Tp> 00973 inline complex<_Tp> 00974 pow(const complex<_Tp>& __z, int __n) 00975 { 00976 return __n < 0 00977 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 00978 : std::__complex_pow_unsigned(__z, __n); 00979 } 00980 #endif 00981 00982 template<typename _Tp> 00983 complex<_Tp> 00984 pow(const complex<_Tp>& __x, const _Tp& __y) 00985 { 00986 #ifndef _GLIBCXX_USE_C99_COMPLEX 00987 if (__x == _Tp()) 00988 return _Tp(); 00989 #endif 00990 if (__x.imag() == _Tp() && __x.real() > _Tp()) 00991 return pow(__x.real(), __y); 00992 00993 complex<_Tp> __t = std::log(__x); 00994 return std::polar(exp(__y * __t.real()), __y * __t.imag()); 00995 } 00996 00997 template<typename _Tp> 00998 inline complex<_Tp> 00999 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01000 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 01001 01002 #if _GLIBCXX_USE_C99_COMPLEX 01003 inline __complex__ float 01004 __complex_pow(__complex__ float __x, __complex__ float __y) 01005 { return __builtin_cpowf(__x, __y); } 01006 01007 inline __complex__ double 01008 __complex_pow(__complex__ double __x, __complex__ double __y) 01009 { return __builtin_cpow(__x, __y); } 01010 01011 inline __complex__ long double 01012 __complex_pow(const __complex__ long double& __x, 01013 const __complex__ long double& __y) 01014 { return __builtin_cpowl(__x, __y); } 01015 01016 template<typename _Tp> 01017 inline complex<_Tp> 01018 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01019 { return __complex_pow(__x.__rep(), __y.__rep()); } 01020 #else 01021 template<typename _Tp> 01022 inline complex<_Tp> 01023 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01024 { return __complex_pow(__x, __y); } 01025 #endif 01026 01027 template<typename _Tp> 01028 inline complex<_Tp> 01029 pow(const _Tp& __x, const complex<_Tp>& __y) 01030 { 01031 return __x > _Tp() ? std::polar(pow(__x, __y.real()), 01032 __y.imag() * log(__x)) 01033 : std::pow(complex<_Tp>(__x), __y); 01034 } 01035 01036 /// 26.2.3 complex specializations 01037 /// complex<float> specialization 01038 template<> 01039 struct complex<float> 01040 { 01041 typedef float value_type; 01042 typedef __complex__ float _ComplexT; 01043 01044 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01045 01046 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 01047 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01048 : _M_value{ __r, __i } { } 01049 #else 01050 { 01051 __real__ _M_value = __r; 01052 __imag__ _M_value = __i; 01053 } 01054 #endif 01055 01056 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 01057 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01058 01059 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01060 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01061 // DR 387. std::complex over-encapsulated. 01062 constexpr float 01063 real() { return __real__ _M_value; } 01064 01065 constexpr float 01066 imag() { return __imag__ _M_value; } 01067 #else 01068 float& 01069 real() { return __real__ _M_value; } 01070 01071 const float& 01072 real() const { return __real__ _M_value; } 01073 01074 float& 01075 imag() { return __imag__ _M_value; } 01076 01077 const float& 01078 imag() const { return __imag__ _M_value; } 01079 #endif 01080 01081 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01082 // DR 387. std::complex over-encapsulated. 01083 void 01084 real(float __val) { __real__ _M_value = __val; } 01085 01086 void 01087 imag(float __val) { __imag__ _M_value = __val; } 01088 01089 complex& 01090 operator=(float __f) 01091 { 01092 _M_value = __f; 01093 return *this; 01094 } 01095 01096 complex& 01097 operator+=(float __f) 01098 { 01099 _M_value += __f; 01100 return *this; 01101 } 01102 01103 complex& 01104 operator-=(float __f) 01105 { 01106 _M_value -= __f; 01107 return *this; 01108 } 01109 01110 complex& 01111 operator*=(float __f) 01112 { 01113 _M_value *= __f; 01114 return *this; 01115 } 01116 01117 complex& 01118 operator/=(float __f) 01119 { 01120 _M_value /= __f; 01121 return *this; 01122 } 01123 01124 // Let the compiler synthesize the copy and assignment 01125 // operator. It always does a pretty good job. 01126 // complex& operator=(const complex&); 01127 01128 template<typename _Tp> 01129 complex& 01130 operator=(const complex<_Tp>& __z) 01131 { 01132 __real__ _M_value = __z.real(); 01133 __imag__ _M_value = __z.imag(); 01134 return *this; 01135 } 01136 01137 template<typename _Tp> 01138 complex& 01139 operator+=(const complex<_Tp>& __z) 01140 { 01141 __real__ _M_value += __z.real(); 01142 __imag__ _M_value += __z.imag(); 01143 return *this; 01144 } 01145 01146 template<class _Tp> 01147 complex& 01148 operator-=(const complex<_Tp>& __z) 01149 { 01150 __real__ _M_value -= __z.real(); 01151 __imag__ _M_value -= __z.imag(); 01152 return *this; 01153 } 01154 01155 template<class _Tp> 01156 complex& 01157 operator*=(const complex<_Tp>& __z) 01158 { 01159 _ComplexT __t; 01160 __real__ __t = __z.real(); 01161 __imag__ __t = __z.imag(); 01162 _M_value *= __t; 01163 return *this; 01164 } 01165 01166 template<class _Tp> 01167 complex& 01168 operator/=(const complex<_Tp>& __z) 01169 { 01170 _ComplexT __t; 01171 __real__ __t = __z.real(); 01172 __imag__ __t = __z.imag(); 01173 _M_value /= __t; 01174 return *this; 01175 } 01176 01177 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01178 01179 private: 01180 _ComplexT _M_value; 01181 }; 01182 01183 /// 26.2.3 complex specializations 01184 /// complex<double> specialization 01185 template<> 01186 struct complex<double> 01187 { 01188 typedef double value_type; 01189 typedef __complex__ double _ComplexT; 01190 01191 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01192 01193 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 01194 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01195 : _M_value{ __r, __i } { } 01196 #else 01197 { 01198 __real__ _M_value = __r; 01199 __imag__ _M_value = __i; 01200 } 01201 #endif 01202 01203 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01204 : _M_value(__z.__rep()) { } 01205 01206 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01207 01208 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01209 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01210 // DR 387. std::complex over-encapsulated. 01211 constexpr double 01212 real() { return __real__ _M_value; } 01213 01214 constexpr double 01215 imag() { return __imag__ _M_value; } 01216 #else 01217 double& 01218 real() { return __real__ _M_value; } 01219 01220 const double& 01221 real() const { return __real__ _M_value; } 01222 01223 double& 01224 imag() { return __imag__ _M_value; } 01225 01226 const double& 01227 imag() const { return __imag__ _M_value; } 01228 #endif 01229 01230 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01231 // DR 387. std::complex over-encapsulated. 01232 void 01233 real(double __val) { __real__ _M_value = __val; } 01234 01235 void 01236 imag(double __val) { __imag__ _M_value = __val; } 01237 01238 complex& 01239 operator=(double __d) 01240 { 01241 _M_value = __d; 01242 return *this; 01243 } 01244 01245 complex& 01246 operator+=(double __d) 01247 { 01248 _M_value += __d; 01249 return *this; 01250 } 01251 01252 complex& 01253 operator-=(double __d) 01254 { 01255 _M_value -= __d; 01256 return *this; 01257 } 01258 01259 complex& 01260 operator*=(double __d) 01261 { 01262 _M_value *= __d; 01263 return *this; 01264 } 01265 01266 complex& 01267 operator/=(double __d) 01268 { 01269 _M_value /= __d; 01270 return *this; 01271 } 01272 01273 // The compiler will synthesize this, efficiently. 01274 // complex& operator=(const complex&); 01275 01276 template<typename _Tp> 01277 complex& 01278 operator=(const complex<_Tp>& __z) 01279 { 01280 __real__ _M_value = __z.real(); 01281 __imag__ _M_value = __z.imag(); 01282 return *this; 01283 } 01284 01285 template<typename _Tp> 01286 complex& 01287 operator+=(const complex<_Tp>& __z) 01288 { 01289 __real__ _M_value += __z.real(); 01290 __imag__ _M_value += __z.imag(); 01291 return *this; 01292 } 01293 01294 template<typename _Tp> 01295 complex& 01296 operator-=(const complex<_Tp>& __z) 01297 { 01298 __real__ _M_value -= __z.real(); 01299 __imag__ _M_value -= __z.imag(); 01300 return *this; 01301 } 01302 01303 template<typename _Tp> 01304 complex& 01305 operator*=(const complex<_Tp>& __z) 01306 { 01307 _ComplexT __t; 01308 __real__ __t = __z.real(); 01309 __imag__ __t = __z.imag(); 01310 _M_value *= __t; 01311 return *this; 01312 } 01313 01314 template<typename _Tp> 01315 complex& 01316 operator/=(const complex<_Tp>& __z) 01317 { 01318 _ComplexT __t; 01319 __real__ __t = __z.real(); 01320 __imag__ __t = __z.imag(); 01321 _M_value /= __t; 01322 return *this; 01323 } 01324 01325 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01326 01327 private: 01328 _ComplexT _M_value; 01329 }; 01330 01331 /// 26.2.3 complex specializations 01332 /// complex<long double> specialization 01333 template<> 01334 struct complex<long double> 01335 { 01336 typedef long double value_type; 01337 typedef __complex__ long double _ComplexT; 01338 01339 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01340 01341 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 01342 long double __i = 0.0L) 01343 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01344 : _M_value{ __r, __i } { } 01345 #else 01346 { 01347 __real__ _M_value = __r; 01348 __imag__ _M_value = __i; 01349 } 01350 #endif 01351 01352 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01353 : _M_value(__z.__rep()) { } 01354 01355 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 01356 : _M_value(__z.__rep()) { } 01357 01358 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01359 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01360 // DR 387. std::complex over-encapsulated. 01361 constexpr long double 01362 real() { return __real__ _M_value; } 01363 01364 constexpr long double 01365 imag() { return __imag__ _M_value; } 01366 #else 01367 long double& 01368 real() { return __real__ _M_value; } 01369 01370 const long double& 01371 real() const { return __real__ _M_value; } 01372 01373 long double& 01374 imag() { return __imag__ _M_value; } 01375 01376 const long double& 01377 imag() const { return __imag__ _M_value; } 01378 #endif 01379 01380 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01381 // DR 387. std::complex over-encapsulated. 01382 void 01383 real(long double __val) { __real__ _M_value = __val; } 01384 01385 void 01386 imag(long double __val) { __imag__ _M_value = __val; } 01387 01388 complex& 01389 operator=(long double __r) 01390 { 01391 _M_value = __r; 01392 return *this; 01393 } 01394 01395 complex& 01396 operator+=(long double __r) 01397 { 01398 _M_value += __r; 01399 return *this; 01400 } 01401 01402 complex& 01403 operator-=(long double __r) 01404 { 01405 _M_value -= __r; 01406 return *this; 01407 } 01408 01409 complex& 01410 operator*=(long double __r) 01411 { 01412 _M_value *= __r; 01413 return *this; 01414 } 01415 01416 complex& 01417 operator/=(long double __r) 01418 { 01419 _M_value /= __r; 01420 return *this; 01421 } 01422 01423 // The compiler knows how to do this efficiently 01424 // complex& operator=(const complex&); 01425 01426 template<typename _Tp> 01427 complex& 01428 operator=(const complex<_Tp>& __z) 01429 { 01430 __real__ _M_value = __z.real(); 01431 __imag__ _M_value = __z.imag(); 01432 return *this; 01433 } 01434 01435 template<typename _Tp> 01436 complex& 01437 operator+=(const complex<_Tp>& __z) 01438 { 01439 __real__ _M_value += __z.real(); 01440 __imag__ _M_value += __z.imag(); 01441 return *this; 01442 } 01443 01444 template<typename _Tp> 01445 complex& 01446 operator-=(const complex<_Tp>& __z) 01447 { 01448 __real__ _M_value -= __z.real(); 01449 __imag__ _M_value -= __z.imag(); 01450 return *this; 01451 } 01452 01453 template<typename _Tp> 01454 complex& 01455 operator*=(const complex<_Tp>& __z) 01456 { 01457 _ComplexT __t; 01458 __real__ __t = __z.real(); 01459 __imag__ __t = __z.imag(); 01460 _M_value *= __t; 01461 return *this; 01462 } 01463 01464 template<typename _Tp> 01465 complex& 01466 operator/=(const complex<_Tp>& __z) 01467 { 01468 _ComplexT __t; 01469 __real__ __t = __z.real(); 01470 __imag__ __t = __z.imag(); 01471 _M_value /= __t; 01472 return *this; 01473 } 01474 01475 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01476 01477 private: 01478 _ComplexT _M_value; 01479 }; 01480 01481 // These bits have to be at the end of this file, so that the 01482 // specializations have all been defined. 01483 inline _GLIBCXX_CONSTEXPR 01484 complex<float>::complex(const complex<double>& __z) 01485 : _M_value(__z.__rep()) { } 01486 01487 inline _GLIBCXX_CONSTEXPR 01488 complex<float>::complex(const complex<long double>& __z) 01489 : _M_value(__z.__rep()) { } 01490 01491 inline _GLIBCXX_CONSTEXPR 01492 complex<double>::complex(const complex<long double>& __z) 01493 : _M_value(__z.__rep()) { } 01494 01495 // Inhibit implicit instantiations for required instantiations, 01496 // which are defined via explicit instantiations elsewhere. 01497 // NB: This syntax is a GNU extension. 01498 #if _GLIBCXX_EXTERN_TEMPLATE 01499 extern template istream& operator>>(istream&, complex<float>&); 01500 extern template ostream& operator<<(ostream&, const complex<float>&); 01501 extern template istream& operator>>(istream&, complex<double>&); 01502 extern template ostream& operator<<(ostream&, const complex<double>&); 01503 extern template istream& operator>>(istream&, complex<long double>&); 01504 extern template ostream& operator<<(ostream&, const complex<long double>&); 01505 01506 #ifdef _GLIBCXX_USE_WCHAR_T 01507 extern template wistream& operator>>(wistream&, complex<float>&); 01508 extern template wostream& operator<<(wostream&, const complex<float>&); 01509 extern template wistream& operator>>(wistream&, complex<double>&); 01510 extern template wostream& operator<<(wostream&, const complex<double>&); 01511 extern template wistream& operator>>(wistream&, complex<long double>&); 01512 extern template wostream& operator<<(wostream&, const complex<long double>&); 01513 #endif 01514 #endif 01515 01516 // @} group complex_numbers 01517 01518 _GLIBCXX_END_NAMESPACE_VERSION 01519 } // namespace 01520 01521 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 01522 { 01523 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01524 01525 // See ext/type_traits.h for the primary template. 01526 template<typename _Tp, typename _Up> 01527 struct __promote_2<std::complex<_Tp>, _Up> 01528 { 01529 public: 01530 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01531 }; 01532 01533 template<typename _Tp, typename _Up> 01534 struct __promote_2<_Tp, std::complex<_Up> > 01535 { 01536 public: 01537 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01538 }; 01539 01540 template<typename _Tp, typename _Up> 01541 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 01542 { 01543 public: 01544 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01545 }; 01546 01547 _GLIBCXX_END_NAMESPACE_VERSION 01548 } // namespace 01549 01550 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01551 01552 namespace std _GLIBCXX_VISIBILITY(default) 01553 { 01554 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01555 01556 // Forward declarations. 01557 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 01558 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 01559 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 01560 01561 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 01562 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 01563 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 01564 // DR 595. 01565 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 01566 01567 template<typename _Tp> 01568 inline std::complex<_Tp> 01569 __complex_acos(const std::complex<_Tp>& __z) 01570 { 01571 const std::complex<_Tp> __t = std::asin(__z); 01572 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 01573 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 01574 } 01575 01576 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01577 inline __complex__ float 01578 __complex_acos(__complex__ float __z) 01579 { return __builtin_cacosf(__z); } 01580 01581 inline __complex__ double 01582 __complex_acos(__complex__ double __z) 01583 { return __builtin_cacos(__z); } 01584 01585 inline __complex__ long double 01586 __complex_acos(const __complex__ long double& __z) 01587 { return __builtin_cacosl(__z); } 01588 01589 template<typename _Tp> 01590 inline std::complex<_Tp> 01591 acos(const std::complex<_Tp>& __z) 01592 { return __complex_acos(__z.__rep()); } 01593 #else 01594 /// acos(__z) [8.1.2]. 01595 // Effects: Behaves the same as C99 function cacos, defined 01596 // in subclause 7.3.5.1. 01597 template<typename _Tp> 01598 inline std::complex<_Tp> 01599 acos(const std::complex<_Tp>& __z) 01600 { return __complex_acos(__z); } 01601 #endif 01602 01603 template<typename _Tp> 01604 inline std::complex<_Tp> 01605 __complex_asin(const std::complex<_Tp>& __z) 01606 { 01607 std::complex<_Tp> __t(-__z.imag(), __z.real()); 01608 __t = std::asinh(__t); 01609 return std::complex<_Tp>(__t.imag(), -__t.real()); 01610 } 01611 01612 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01613 inline __complex__ float 01614 __complex_asin(__complex__ float __z) 01615 { return __builtin_casinf(__z); } 01616 01617 inline __complex__ double 01618 __complex_asin(__complex__ double __z) 01619 { return __builtin_casin(__z); } 01620 01621 inline __complex__ long double 01622 __complex_asin(const __complex__ long double& __z) 01623 { return __builtin_casinl(__z); } 01624 01625 template<typename _Tp> 01626 inline std::complex<_Tp> 01627 asin(const std::complex<_Tp>& __z) 01628 { return __complex_asin(__z.__rep()); } 01629 #else 01630 /// asin(__z) [8.1.3]. 01631 // Effects: Behaves the same as C99 function casin, defined 01632 // in subclause 7.3.5.2. 01633 template<typename _Tp> 01634 inline std::complex<_Tp> 01635 asin(const std::complex<_Tp>& __z) 01636 { return __complex_asin(__z); } 01637 #endif 01638 01639 template<typename _Tp> 01640 std::complex<_Tp> 01641 __complex_atan(const std::complex<_Tp>& __z) 01642 { 01643 const _Tp __r2 = __z.real() * __z.real(); 01644 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 01645 01646 _Tp __num = __z.imag() + _Tp(1.0); 01647 _Tp __den = __z.imag() - _Tp(1.0); 01648 01649 __num = __r2 + __num * __num; 01650 __den = __r2 + __den * __den; 01651 01652 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 01653 _Tp(0.25) * log(__num / __den)); 01654 } 01655 01656 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01657 inline __complex__ float 01658 __complex_atan(__complex__ float __z) 01659 { return __builtin_catanf(__z); } 01660 01661 inline __complex__ double 01662 __complex_atan(__complex__ double __z) 01663 { return __builtin_catan(__z); } 01664 01665 inline __complex__ long double 01666 __complex_atan(const __complex__ long double& __z) 01667 { return __builtin_catanl(__z); } 01668 01669 template<typename _Tp> 01670 inline std::complex<_Tp> 01671 atan(const std::complex<_Tp>& __z) 01672 { return __complex_atan(__z.__rep()); } 01673 #else 01674 /// atan(__z) [8.1.4]. 01675 // Effects: Behaves the same as C99 function catan, defined 01676 // in subclause 7.3.5.3. 01677 template<typename _Tp> 01678 inline std::complex<_Tp> 01679 atan(const std::complex<_Tp>& __z) 01680 { return __complex_atan(__z); } 01681 #endif 01682 01683 template<typename _Tp> 01684 std::complex<_Tp> 01685 __complex_acosh(const std::complex<_Tp>& __z) 01686 { 01687 // Kahan's formula. 01688 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 01689 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 01690 } 01691 01692 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01693 inline __complex__ float 01694 __complex_acosh(__complex__ float __z) 01695 { return __builtin_cacoshf(__z); } 01696 01697 inline __complex__ double 01698 __complex_acosh(__complex__ double __z) 01699 { return __builtin_cacosh(__z); } 01700 01701 inline __complex__ long double 01702 __complex_acosh(const __complex__ long double& __z) 01703 { return __builtin_cacoshl(__z); } 01704 01705 template<typename _Tp> 01706 inline std::complex<_Tp> 01707 acosh(const std::complex<_Tp>& __z) 01708 { return __complex_acosh(__z.__rep()); } 01709 #else 01710 /// acosh(__z) [8.1.5]. 01711 // Effects: Behaves the same as C99 function cacosh, defined 01712 // in subclause 7.3.6.1. 01713 template<typename _Tp> 01714 inline std::complex<_Tp> 01715 acosh(const std::complex<_Tp>& __z) 01716 { return __complex_acosh(__z); } 01717 #endif 01718 01719 template<typename _Tp> 01720 std::complex<_Tp> 01721 __complex_asinh(const std::complex<_Tp>& __z) 01722 { 01723 std::complex<_Tp> __t((__z.real() - __z.imag()) 01724 * (__z.real() + __z.imag()) + _Tp(1.0), 01725 _Tp(2.0) * __z.real() * __z.imag()); 01726 __t = std::sqrt(__t); 01727 01728 return std::log(__t + __z); 01729 } 01730 01731 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01732 inline __complex__ float 01733 __complex_asinh(__complex__ float __z) 01734 { return __builtin_casinhf(__z); } 01735 01736 inline __complex__ double 01737 __complex_asinh(__complex__ double __z) 01738 { return __builtin_casinh(__z); } 01739 01740 inline __complex__ long double 01741 __complex_asinh(const __complex__ long double& __z) 01742 { return __builtin_casinhl(__z); } 01743 01744 template<typename _Tp> 01745 inline std::complex<_Tp> 01746 asinh(const std::complex<_Tp>& __z) 01747 { return __complex_asinh(__z.__rep()); } 01748 #else 01749 /// asinh(__z) [8.1.6]. 01750 // Effects: Behaves the same as C99 function casin, defined 01751 // in subclause 7.3.6.2. 01752 template<typename _Tp> 01753 inline std::complex<_Tp> 01754 asinh(const std::complex<_Tp>& __z) 01755 { return __complex_asinh(__z); } 01756 #endif 01757 01758 template<typename _Tp> 01759 std::complex<_Tp> 01760 __complex_atanh(const std::complex<_Tp>& __z) 01761 { 01762 const _Tp __i2 = __z.imag() * __z.imag(); 01763 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 01764 01765 _Tp __num = _Tp(1.0) + __z.real(); 01766 _Tp __den = _Tp(1.0) - __z.real(); 01767 01768 __num = __i2 + __num * __num; 01769 __den = __i2 + __den * __den; 01770 01771 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 01772 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 01773 } 01774 01775 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01776 inline __complex__ float 01777 __complex_atanh(__complex__ float __z) 01778 { return __builtin_catanhf(__z); } 01779 01780 inline __complex__ double 01781 __complex_atanh(__complex__ double __z) 01782 { return __builtin_catanh(__z); } 01783 01784 inline __complex__ long double 01785 __complex_atanh(const __complex__ long double& __z) 01786 { return __builtin_catanhl(__z); } 01787 01788 template<typename _Tp> 01789 inline std::complex<_Tp> 01790 atanh(const std::complex<_Tp>& __z) 01791 { return __complex_atanh(__z.__rep()); } 01792 #else 01793 /// atanh(__z) [8.1.7]. 01794 // Effects: Behaves the same as C99 function catanh, defined 01795 // in subclause 7.3.6.3. 01796 template<typename _Tp> 01797 inline std::complex<_Tp> 01798 atanh(const std::complex<_Tp>& __z) 01799 { return __complex_atanh(__z); } 01800 #endif 01801 01802 template<typename _Tp> 01803 inline _Tp 01804 /// fabs(__z) [8.1.8]. 01805 // Effects: Behaves the same as C99 function cabs, defined 01806 // in subclause 7.3.8.1. 01807 fabs(const std::complex<_Tp>& __z) 01808 { return std::abs(__z); } 01809 01810 /// Additional overloads [8.1.9]. 01811 template<typename _Tp> 01812 inline typename __gnu_cxx::__promote<_Tp>::__type 01813 arg(_Tp __x) 01814 { 01815 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01816 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 01817 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 01818 : __type(); 01819 #else 01820 return std::arg(std::complex<__type>(__x)); 01821 #endif 01822 } 01823 01824 template<typename _Tp> 01825 inline typename __gnu_cxx::__promote<_Tp>::__type 01826 imag(_Tp) 01827 { return _Tp(); } 01828 01829 template<typename _Tp> 01830 inline typename __gnu_cxx::__promote<_Tp>::__type 01831 norm(_Tp __x) 01832 { 01833 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01834 return __type(__x) * __type(__x); 01835 } 01836 01837 template<typename _Tp> 01838 inline typename __gnu_cxx::__promote<_Tp>::__type 01839 real(_Tp __x) 01840 { return __x; } 01841 01842 template<typename _Tp, typename _Up> 01843 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01844 pow(const std::complex<_Tp>& __x, const _Up& __y) 01845 { 01846 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01847 return std::pow(std::complex<__type>(__x), __type(__y)); 01848 } 01849 01850 template<typename _Tp, typename _Up> 01851 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01852 pow(const _Tp& __x, const std::complex<_Up>& __y) 01853 { 01854 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01855 return std::pow(__type(__x), std::complex<__type>(__y)); 01856 } 01857 01858 template<typename _Tp, typename _Up> 01859 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01860 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 01861 { 01862 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01863 return std::pow(std::complex<__type>(__x), 01864 std::complex<__type>(__y)); 01865 } 01866 01867 // Forward declarations. 01868 // DR 781. 01869 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 01870 01871 template<typename _Tp> 01872 std::complex<_Tp> 01873 __complex_proj(const std::complex<_Tp>& __z) 01874 { 01875 const _Tp __den = (__z.real() * __z.real() 01876 + __z.imag() * __z.imag() + _Tp(1.0)); 01877 01878 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 01879 (_Tp(2.0) * __z.imag()) / __den); 01880 } 01881 01882 #if _GLIBCXX_USE_C99_COMPLEX 01883 inline __complex__ float 01884 __complex_proj(__complex__ float __z) 01885 { return __builtin_cprojf(__z); } 01886 01887 inline __complex__ double 01888 __complex_proj(__complex__ double __z) 01889 { return __builtin_cproj(__z); } 01890 01891 inline __complex__ long double 01892 __complex_proj(const __complex__ long double& __z) 01893 { return __builtin_cprojl(__z); } 01894 01895 template<typename _Tp> 01896 inline std::complex<_Tp> 01897 proj(const std::complex<_Tp>& __z) 01898 { return __complex_proj(__z.__rep()); } 01899 #else 01900 template<typename _Tp> 01901 inline std::complex<_Tp> 01902 proj(const std::complex<_Tp>& __z) 01903 { return __complex_proj(__z); } 01904 #endif 01905 01906 // DR 1137. 01907 template<typename _Tp> 01908 inline typename __gnu_cxx::__promote<_Tp>::__type 01909 proj(_Tp __x) 01910 { return __x; } 01911 01912 template<typename _Tp> 01913 inline typename __gnu_cxx::__promote<_Tp>::__type 01914 conj(_Tp __x) 01915 { return __x; } 01916 01917 _GLIBCXX_END_NAMESPACE_VERSION 01918 } // namespace 01919 01920 #endif // __GXX_EXPERIMENTAL_CXX0X__ 01921 01922 #endif /* _GLIBCXX_COMPLEX */