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