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, 2011 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() { return _M_real; } 00146 00147 constexpr _Tp 00148 imag() { 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 constexpr _Tp 00535 real(const complex<_Tp>& __z) 00536 { return __z.real(); } 00537 00538 template<typename _Tp> 00539 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 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01050 : _M_value{ __r, __i } { } 01051 #else 01052 { 01053 __real__ _M_value = __r; 01054 __imag__ _M_value = __i; 01055 } 01056 #endif 01057 01058 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 01059 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01060 01061 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01062 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01063 // DR 387. std::complex over-encapsulated. 01064 constexpr float 01065 real() { return __real__ _M_value; } 01066 01067 constexpr float 01068 imag() { return __imag__ _M_value; } 01069 #else 01070 float& 01071 real() { return __real__ _M_value; } 01072 01073 const float& 01074 real() const { return __real__ _M_value; } 01075 01076 float& 01077 imag() { return __imag__ _M_value; } 01078 01079 const float& 01080 imag() const { return __imag__ _M_value; } 01081 #endif 01082 01083 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01084 // DR 387. std::complex over-encapsulated. 01085 void 01086 real(float __val) { __real__ _M_value = __val; } 01087 01088 void 01089 imag(float __val) { __imag__ _M_value = __val; } 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 complex& 01120 operator/=(float __f) 01121 { 01122 _M_value /= __f; 01123 return *this; 01124 } 01125 01126 // Let the compiler synthesize the copy and assignment 01127 // operator. It always does a pretty good job. 01128 // complex& operator=(const complex&); 01129 01130 template<typename _Tp> 01131 complex& 01132 operator=(const complex<_Tp>& __z) 01133 { 01134 __real__ _M_value = __z.real(); 01135 __imag__ _M_value = __z.imag(); 01136 return *this; 01137 } 01138 01139 template<typename _Tp> 01140 complex& 01141 operator+=(const complex<_Tp>& __z) 01142 { 01143 __real__ _M_value += __z.real(); 01144 __imag__ _M_value += __z.imag(); 01145 return *this; 01146 } 01147 01148 template<class _Tp> 01149 complex& 01150 operator-=(const complex<_Tp>& __z) 01151 { 01152 __real__ _M_value -= __z.real(); 01153 __imag__ _M_value -= __z.imag(); 01154 return *this; 01155 } 01156 01157 template<class _Tp> 01158 complex& 01159 operator*=(const complex<_Tp>& __z) 01160 { 01161 _ComplexT __t; 01162 __real__ __t = __z.real(); 01163 __imag__ __t = __z.imag(); 01164 _M_value *= __t; 01165 return *this; 01166 } 01167 01168 template<class _Tp> 01169 complex& 01170 operator/=(const complex<_Tp>& __z) 01171 { 01172 _ComplexT __t; 01173 __real__ __t = __z.real(); 01174 __imag__ __t = __z.imag(); 01175 _M_value /= __t; 01176 return *this; 01177 } 01178 01179 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01180 01181 private: 01182 _ComplexT _M_value; 01183 }; 01184 01185 /// 26.2.3 complex specializations 01186 /// complex<double> specialization 01187 template<> 01188 struct complex<double> 01189 { 01190 typedef double value_type; 01191 typedef __complex__ double _ComplexT; 01192 01193 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01194 01195 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 01196 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01197 : _M_value{ __r, __i } { } 01198 #else 01199 { 01200 __real__ _M_value = __r; 01201 __imag__ _M_value = __i; 01202 } 01203 #endif 01204 01205 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01206 : _M_value(__z.__rep()) { } 01207 01208 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01209 01210 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01211 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01212 // DR 387. std::complex over-encapsulated. 01213 constexpr double 01214 real() { return __real__ _M_value; } 01215 01216 constexpr double 01217 imag() { return __imag__ _M_value; } 01218 #else 01219 double& 01220 real() { return __real__ _M_value; } 01221 01222 const double& 01223 real() const { return __real__ _M_value; } 01224 01225 double& 01226 imag() { return __imag__ _M_value; } 01227 01228 const double& 01229 imag() const { return __imag__ _M_value; } 01230 #endif 01231 01232 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01233 // DR 387. std::complex over-encapsulated. 01234 void 01235 real(double __val) { __real__ _M_value = __val; } 01236 01237 void 01238 imag(double __val) { __imag__ _M_value = __val; } 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 complex& 01262 operator*=(double __d) 01263 { 01264 _M_value *= __d; 01265 return *this; 01266 } 01267 01268 complex& 01269 operator/=(double __d) 01270 { 01271 _M_value /= __d; 01272 return *this; 01273 } 01274 01275 // The compiler will synthesize this, efficiently. 01276 // complex& operator=(const complex&); 01277 01278 template<typename _Tp> 01279 complex& 01280 operator=(const complex<_Tp>& __z) 01281 { 01282 __real__ _M_value = __z.real(); 01283 __imag__ _M_value = __z.imag(); 01284 return *this; 01285 } 01286 01287 template<typename _Tp> 01288 complex& 01289 operator+=(const complex<_Tp>& __z) 01290 { 01291 __real__ _M_value += __z.real(); 01292 __imag__ _M_value += __z.imag(); 01293 return *this; 01294 } 01295 01296 template<typename _Tp> 01297 complex& 01298 operator-=(const complex<_Tp>& __z) 01299 { 01300 __real__ _M_value -= __z.real(); 01301 __imag__ _M_value -= __z.imag(); 01302 return *this; 01303 } 01304 01305 template<typename _Tp> 01306 complex& 01307 operator*=(const complex<_Tp>& __z) 01308 { 01309 _ComplexT __t; 01310 __real__ __t = __z.real(); 01311 __imag__ __t = __z.imag(); 01312 _M_value *= __t; 01313 return *this; 01314 } 01315 01316 template<typename _Tp> 01317 complex& 01318 operator/=(const complex<_Tp>& __z) 01319 { 01320 _ComplexT __t; 01321 __real__ __t = __z.real(); 01322 __imag__ __t = __z.imag(); 01323 _M_value /= __t; 01324 return *this; 01325 } 01326 01327 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01328 01329 private: 01330 _ComplexT _M_value; 01331 }; 01332 01333 /// 26.2.3 complex specializations 01334 /// complex<long double> specialization 01335 template<> 01336 struct complex<long double> 01337 { 01338 typedef long double value_type; 01339 typedef __complex__ long double _ComplexT; 01340 01341 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01342 01343 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 01344 long double __i = 0.0L) 01345 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01346 : _M_value{ __r, __i } { } 01347 #else 01348 { 01349 __real__ _M_value = __r; 01350 __imag__ _M_value = __i; 01351 } 01352 #endif 01353 01354 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01355 : _M_value(__z.__rep()) { } 01356 01357 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 01358 : _M_value(__z.__rep()) { } 01359 01360 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01361 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01362 // DR 387. std::complex over-encapsulated. 01363 constexpr long double 01364 real() { return __real__ _M_value; } 01365 01366 constexpr long double 01367 imag() { return __imag__ _M_value; } 01368 #else 01369 long double& 01370 real() { return __real__ _M_value; } 01371 01372 const long double& 01373 real() const { return __real__ _M_value; } 01374 01375 long double& 01376 imag() { return __imag__ _M_value; } 01377 01378 const long double& 01379 imag() const { return __imag__ _M_value; } 01380 #endif 01381 01382 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01383 // DR 387. std::complex over-encapsulated. 01384 void 01385 real(long double __val) { __real__ _M_value = __val; } 01386 01387 void 01388 imag(long double __val) { __imag__ _M_value = __val; } 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 complex& 01405 operator-=(long double __r) 01406 { 01407 _M_value -= __r; 01408 return *this; 01409 } 01410 01411 complex& 01412 operator*=(long double __r) 01413 { 01414 _M_value *= __r; 01415 return *this; 01416 } 01417 01418 complex& 01419 operator/=(long double __r) 01420 { 01421 _M_value /= __r; 01422 return *this; 01423 } 01424 01425 // The compiler knows how to do this efficiently 01426 // complex& operator=(const complex&); 01427 01428 template<typename _Tp> 01429 complex& 01430 operator=(const complex<_Tp>& __z) 01431 { 01432 __real__ _M_value = __z.real(); 01433 __imag__ _M_value = __z.imag(); 01434 return *this; 01435 } 01436 01437 template<typename _Tp> 01438 complex& 01439 operator+=(const complex<_Tp>& __z) 01440 { 01441 __real__ _M_value += __z.real(); 01442 __imag__ _M_value += __z.imag(); 01443 return *this; 01444 } 01445 01446 template<typename _Tp> 01447 complex& 01448 operator-=(const complex<_Tp>& __z) 01449 { 01450 __real__ _M_value -= __z.real(); 01451 __imag__ _M_value -= __z.imag(); 01452 return *this; 01453 } 01454 01455 template<typename _Tp> 01456 complex& 01457 operator*=(const complex<_Tp>& __z) 01458 { 01459 _ComplexT __t; 01460 __real__ __t = __z.real(); 01461 __imag__ __t = __z.imag(); 01462 _M_value *= __t; 01463 return *this; 01464 } 01465 01466 template<typename _Tp> 01467 complex& 01468 operator/=(const complex<_Tp>& __z) 01469 { 01470 _ComplexT __t; 01471 __real__ __t = __z.real(); 01472 __imag__ __t = __z.imag(); 01473 _M_value /= __t; 01474 return *this; 01475 } 01476 01477 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01478 01479 private: 01480 _ComplexT _M_value; 01481 }; 01482 01483 // These bits have to be at the end of this file, so that the 01484 // specializations have all been defined. 01485 inline _GLIBCXX_CONSTEXPR 01486 complex<float>::complex(const complex<double>& __z) 01487 : _M_value(__z.__rep()) { } 01488 01489 inline _GLIBCXX_CONSTEXPR 01490 complex<float>::complex(const complex<long double>& __z) 01491 : _M_value(__z.__rep()) { } 01492 01493 inline _GLIBCXX_CONSTEXPR 01494 complex<double>::complex(const complex<long double>& __z) 01495 : _M_value(__z.__rep()) { } 01496 01497 // Inhibit implicit instantiations for required instantiations, 01498 // which are defined via explicit instantiations elsewhere. 01499 // NB: This syntax is a GNU extension. 01500 #if _GLIBCXX_EXTERN_TEMPLATE 01501 extern template istream& operator>>(istream&, complex<float>&); 01502 extern template ostream& operator<<(ostream&, const complex<float>&); 01503 extern template istream& operator>>(istream&, complex<double>&); 01504 extern template ostream& operator<<(ostream&, const complex<double>&); 01505 extern template istream& operator>>(istream&, complex<long double>&); 01506 extern template ostream& operator<<(ostream&, const complex<long double>&); 01507 01508 #ifdef _GLIBCXX_USE_WCHAR_T 01509 extern template wistream& operator>>(wistream&, complex<float>&); 01510 extern template wostream& operator<<(wostream&, const complex<float>&); 01511 extern template wistream& operator>>(wistream&, complex<double>&); 01512 extern template wostream& operator<<(wostream&, const complex<double>&); 01513 extern template wistream& operator>>(wistream&, complex<long double>&); 01514 extern template wostream& operator<<(wostream&, const complex<long double>&); 01515 #endif 01516 #endif 01517 01518 // @} group complex_numbers 01519 01520 _GLIBCXX_END_NAMESPACE_VERSION 01521 } // namespace 01522 01523 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 01524 { 01525 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01526 01527 // See ext/type_traits.h for the primary template. 01528 template<typename _Tp, typename _Up> 01529 struct __promote_2<std::complex<_Tp>, _Up> 01530 { 01531 public: 01532 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01533 }; 01534 01535 template<typename _Tp, typename _Up> 01536 struct __promote_2<_Tp, std::complex<_Up> > 01537 { 01538 public: 01539 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01540 }; 01541 01542 template<typename _Tp, typename _Up> 01543 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 01544 { 01545 public: 01546 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01547 }; 01548 01549 _GLIBCXX_END_NAMESPACE_VERSION 01550 } // namespace 01551 01552 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 01553 01554 namespace std _GLIBCXX_VISIBILITY(default) 01555 { 01556 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01557 01558 // Forward declarations. 01559 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 01560 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 01561 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 01562 01563 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 01564 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 01565 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 01566 // DR 595. 01567 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 01568 01569 template<typename _Tp> 01570 inline std::complex<_Tp> 01571 __complex_acos(const std::complex<_Tp>& __z) 01572 { 01573 const std::complex<_Tp> __t = std::asin(__z); 01574 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 01575 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 01576 } 01577 01578 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01579 inline __complex__ float 01580 __complex_acos(__complex__ float __z) 01581 { return __builtin_cacosf(__z); } 01582 01583 inline __complex__ double 01584 __complex_acos(__complex__ double __z) 01585 { return __builtin_cacos(__z); } 01586 01587 inline __complex__ long double 01588 __complex_acos(const __complex__ long double& __z) 01589 { return __builtin_cacosl(__z); } 01590 01591 template<typename _Tp> 01592 inline std::complex<_Tp> 01593 acos(const std::complex<_Tp>& __z) 01594 { return __complex_acos(__z.__rep()); } 01595 #else 01596 /// acos(__z) [8.1.2]. 01597 // Effects: Behaves the same as C99 function cacos, defined 01598 // in subclause 7.3.5.1. 01599 template<typename _Tp> 01600 inline std::complex<_Tp> 01601 acos(const std::complex<_Tp>& __z) 01602 { return __complex_acos(__z); } 01603 #endif 01604 01605 template<typename _Tp> 01606 inline std::complex<_Tp> 01607 __complex_asin(const std::complex<_Tp>& __z) 01608 { 01609 std::complex<_Tp> __t(-__z.imag(), __z.real()); 01610 __t = std::asinh(__t); 01611 return std::complex<_Tp>(__t.imag(), -__t.real()); 01612 } 01613 01614 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01615 inline __complex__ float 01616 __complex_asin(__complex__ float __z) 01617 { return __builtin_casinf(__z); } 01618 01619 inline __complex__ double 01620 __complex_asin(__complex__ double __z) 01621 { return __builtin_casin(__z); } 01622 01623 inline __complex__ long double 01624 __complex_asin(const __complex__ long double& __z) 01625 { return __builtin_casinl(__z); } 01626 01627 template<typename _Tp> 01628 inline std::complex<_Tp> 01629 asin(const std::complex<_Tp>& __z) 01630 { return __complex_asin(__z.__rep()); } 01631 #else 01632 /// asin(__z) [8.1.3]. 01633 // Effects: Behaves the same as C99 function casin, defined 01634 // in subclause 7.3.5.2. 01635 template<typename _Tp> 01636 inline std::complex<_Tp> 01637 asin(const std::complex<_Tp>& __z) 01638 { return __complex_asin(__z); } 01639 #endif 01640 01641 template<typename _Tp> 01642 std::complex<_Tp> 01643 __complex_atan(const std::complex<_Tp>& __z) 01644 { 01645 const _Tp __r2 = __z.real() * __z.real(); 01646 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 01647 01648 _Tp __num = __z.imag() + _Tp(1.0); 01649 _Tp __den = __z.imag() - _Tp(1.0); 01650 01651 __num = __r2 + __num * __num; 01652 __den = __r2 + __den * __den; 01653 01654 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 01655 _Tp(0.25) * log(__num / __den)); 01656 } 01657 01658 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01659 inline __complex__ float 01660 __complex_atan(__complex__ float __z) 01661 { return __builtin_catanf(__z); } 01662 01663 inline __complex__ double 01664 __complex_atan(__complex__ double __z) 01665 { return __builtin_catan(__z); } 01666 01667 inline __complex__ long double 01668 __complex_atan(const __complex__ long double& __z) 01669 { return __builtin_catanl(__z); } 01670 01671 template<typename _Tp> 01672 inline std::complex<_Tp> 01673 atan(const std::complex<_Tp>& __z) 01674 { return __complex_atan(__z.__rep()); } 01675 #else 01676 /// atan(__z) [8.1.4]. 01677 // Effects: Behaves the same as C99 function catan, defined 01678 // in subclause 7.3.5.3. 01679 template<typename _Tp> 01680 inline std::complex<_Tp> 01681 atan(const std::complex<_Tp>& __z) 01682 { return __complex_atan(__z); } 01683 #endif 01684 01685 template<typename _Tp> 01686 std::complex<_Tp> 01687 __complex_acosh(const std::complex<_Tp>& __z) 01688 { 01689 // Kahan's formula. 01690 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 01691 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 01692 } 01693 01694 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01695 inline __complex__ float 01696 __complex_acosh(__complex__ float __z) 01697 { return __builtin_cacoshf(__z); } 01698 01699 inline __complex__ double 01700 __complex_acosh(__complex__ double __z) 01701 { return __builtin_cacosh(__z); } 01702 01703 inline __complex__ long double 01704 __complex_acosh(const __complex__ long double& __z) 01705 { return __builtin_cacoshl(__z); } 01706 01707 template<typename _Tp> 01708 inline std::complex<_Tp> 01709 acosh(const std::complex<_Tp>& __z) 01710 { return __complex_acosh(__z.__rep()); } 01711 #else 01712 /// acosh(__z) [8.1.5]. 01713 // Effects: Behaves the same as C99 function cacosh, defined 01714 // in subclause 7.3.6.1. 01715 template<typename _Tp> 01716 inline std::complex<_Tp> 01717 acosh(const std::complex<_Tp>& __z) 01718 { return __complex_acosh(__z); } 01719 #endif 01720 01721 template<typename _Tp> 01722 std::complex<_Tp> 01723 __complex_asinh(const std::complex<_Tp>& __z) 01724 { 01725 std::complex<_Tp> __t((__z.real() - __z.imag()) 01726 * (__z.real() + __z.imag()) + _Tp(1.0), 01727 _Tp(2.0) * __z.real() * __z.imag()); 01728 __t = std::sqrt(__t); 01729 01730 return std::log(__t + __z); 01731 } 01732 01733 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01734 inline __complex__ float 01735 __complex_asinh(__complex__ float __z) 01736 { return __builtin_casinhf(__z); } 01737 01738 inline __complex__ double 01739 __complex_asinh(__complex__ double __z) 01740 { return __builtin_casinh(__z); } 01741 01742 inline __complex__ long double 01743 __complex_asinh(const __complex__ long double& __z) 01744 { return __builtin_casinhl(__z); } 01745 01746 template<typename _Tp> 01747 inline std::complex<_Tp> 01748 asinh(const std::complex<_Tp>& __z) 01749 { return __complex_asinh(__z.__rep()); } 01750 #else 01751 /// asinh(__z) [8.1.6]. 01752 // Effects: Behaves the same as C99 function casin, defined 01753 // in subclause 7.3.6.2. 01754 template<typename _Tp> 01755 inline std::complex<_Tp> 01756 asinh(const std::complex<_Tp>& __z) 01757 { return __complex_asinh(__z); } 01758 #endif 01759 01760 template<typename _Tp> 01761 std::complex<_Tp> 01762 __complex_atanh(const std::complex<_Tp>& __z) 01763 { 01764 const _Tp __i2 = __z.imag() * __z.imag(); 01765 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 01766 01767 _Tp __num = _Tp(1.0) + __z.real(); 01768 _Tp __den = _Tp(1.0) - __z.real(); 01769 01770 __num = __i2 + __num * __num; 01771 __den = __i2 + __den * __den; 01772 01773 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 01774 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 01775 } 01776 01777 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01778 inline __complex__ float 01779 __complex_atanh(__complex__ float __z) 01780 { return __builtin_catanhf(__z); } 01781 01782 inline __complex__ double 01783 __complex_atanh(__complex__ double __z) 01784 { return __builtin_catanh(__z); } 01785 01786 inline __complex__ long double 01787 __complex_atanh(const __complex__ long double& __z) 01788 { return __builtin_catanhl(__z); } 01789 01790 template<typename _Tp> 01791 inline std::complex<_Tp> 01792 atanh(const std::complex<_Tp>& __z) 01793 { return __complex_atanh(__z.__rep()); } 01794 #else 01795 /// atanh(__z) [8.1.7]. 01796 // Effects: Behaves the same as C99 function catanh, defined 01797 // in subclause 7.3.6.3. 01798 template<typename _Tp> 01799 inline std::complex<_Tp> 01800 atanh(const std::complex<_Tp>& __z) 01801 { return __complex_atanh(__z); } 01802 #endif 01803 01804 template<typename _Tp> 01805 inline _Tp 01806 /// fabs(__z) [8.1.8]. 01807 // Effects: Behaves the same as C99 function cabs, defined 01808 // in subclause 7.3.8.1. 01809 fabs(const std::complex<_Tp>& __z) 01810 { return std::abs(__z); } 01811 01812 /// Additional overloads [8.1.9]. 01813 template<typename _Tp> 01814 inline typename __gnu_cxx::__promote<_Tp>::__type 01815 arg(_Tp __x) 01816 { 01817 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01818 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 01819 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 01820 : __type(); 01821 #else 01822 return std::arg(std::complex<__type>(__x)); 01823 #endif 01824 } 01825 01826 template<typename _Tp> 01827 inline typename __gnu_cxx::__promote<_Tp>::__type 01828 imag(_Tp) 01829 { return _Tp(); } 01830 01831 template<typename _Tp> 01832 inline typename __gnu_cxx::__promote<_Tp>::__type 01833 norm(_Tp __x) 01834 { 01835 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01836 return __type(__x) * __type(__x); 01837 } 01838 01839 template<typename _Tp> 01840 inline typename __gnu_cxx::__promote<_Tp>::__type 01841 real(_Tp __x) 01842 { return __x; } 01843 01844 template<typename _Tp, typename _Up> 01845 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01846 pow(const std::complex<_Tp>& __x, const _Up& __y) 01847 { 01848 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01849 return std::pow(std::complex<__type>(__x), __type(__y)); 01850 } 01851 01852 template<typename _Tp, typename _Up> 01853 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01854 pow(const _Tp& __x, const std::complex<_Up>& __y) 01855 { 01856 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01857 return std::pow(__type(__x), std::complex<__type>(__y)); 01858 } 01859 01860 template<typename _Tp, typename _Up> 01861 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01862 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 01863 { 01864 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01865 return std::pow(std::complex<__type>(__x), 01866 std::complex<__type>(__y)); 01867 } 01868 01869 // Forward declarations. 01870 // DR 781. 01871 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 01872 01873 template<typename _Tp> 01874 std::complex<_Tp> 01875 __complex_proj(const std::complex<_Tp>& __z) 01876 { 01877 const _Tp __den = (__z.real() * __z.real() 01878 + __z.imag() * __z.imag() + _Tp(1.0)); 01879 01880 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 01881 (_Tp(2.0) * __z.imag()) / __den); 01882 } 01883 01884 #if _GLIBCXX_USE_C99_COMPLEX 01885 inline __complex__ float 01886 __complex_proj(__complex__ float __z) 01887 { return __builtin_cprojf(__z); } 01888 01889 inline __complex__ double 01890 __complex_proj(__complex__ double __z) 01891 { return __builtin_cproj(__z); } 01892 01893 inline __complex__ long double 01894 __complex_proj(const __complex__ long double& __z) 01895 { return __builtin_cprojl(__z); } 01896 01897 template<typename _Tp> 01898 inline std::complex<_Tp> 01899 proj(const std::complex<_Tp>& __z) 01900 { return __complex_proj(__z.__rep()); } 01901 #else 01902 template<typename _Tp> 01903 inline std::complex<_Tp> 01904 proj(const std::complex<_Tp>& __z) 01905 { return __complex_proj(__z); } 01906 #endif 01907 01908 // DR 1137. 01909 template<typename _Tp> 01910 inline typename __gnu_cxx::__promote<_Tp>::__type 01911 proj(_Tp __x) 01912 { return __x; } 01913 01914 template<typename _Tp> 01915 inline typename __gnu_cxx::__promote<_Tp>::__type 01916 conj(_Tp __x) 01917 { return __x; } 01918 01919 _GLIBCXX_END_NAMESPACE_VERSION 01920 } // namespace 01921 01922 #endif // __GXX_EXPERIMENTAL_CXX0X__ 01923 01924 #endif /* _GLIBCXX_COMPLEX */