libstdc++
|
00001 // The template and inlines for the -*- C++ -*- complex number classes. 00002 00003 // Copyright (C) 1997-2014 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file include/complex 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 // 00030 // ISO C++ 14882: 26.2 Complex Numbers 00031 // Note: this is not a conforming implementation. 00032 // Initially implemented by Ulrich Drepper <drepper@cygnus.com> 00033 // Improved by Gabriel Dos Reis <dosreis@cmla.ens-cachan.fr> 00034 // 00035 00036 #ifndef _GLIBCXX_COMPLEX 00037 #define _GLIBCXX_COMPLEX 1 00038 00039 #pragma GCC system_header 00040 00041 #include <bits/c++config.h> 00042 #include <bits/cpp_type_traits.h> 00043 #include <ext/type_traits.h> 00044 #include <cmath> 00045 #include <sstream> 00046 00047 // Get rid of a macro possibly defined in <complex.h> 00048 #undef complex 00049 00050 namespace std _GLIBCXX_VISIBILITY(default) 00051 { 00052 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00053 00054 /** 00055 * @defgroup complex_numbers Complex Numbers 00056 * @ingroup numerics 00057 * 00058 * Classes and functions for complex numbers. 00059 * @{ 00060 */ 00061 00062 // Forward declarations. 00063 template<typename _Tp> class complex; 00064 template<> class complex<float>; 00065 template<> class complex<double>; 00066 template<> class complex<long double>; 00067 00068 /// Return magnitude of @a z. 00069 template<typename _Tp> _Tp abs(const complex<_Tp>&); 00070 /// Return phase angle of @a z. 00071 template<typename _Tp> _Tp arg(const complex<_Tp>&); 00072 /// Return @a z magnitude squared. 00073 template<typename _Tp> _Tp norm(const complex<_Tp>&); 00074 00075 /// Return complex conjugate of @a z. 00076 template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); 00077 /// Return complex with magnitude @a rho and angle @a theta. 00078 template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); 00079 00080 // Transcendentals: 00081 /// Return complex cosine of @a z. 00082 template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); 00083 /// Return complex hyperbolic cosine of @a z. 00084 template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); 00085 /// Return complex base e exponential of @a z. 00086 template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); 00087 /// Return complex natural logarithm of @a z. 00088 template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); 00089 /// Return complex base 10 logarithm of @a z. 00090 template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); 00091 /// Return @a x to the @a y'th power. 00092 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); 00093 /// Return @a x to the @a y'th power. 00094 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); 00095 /// Return @a x to the @a y'th power. 00096 template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, 00097 const complex<_Tp>&); 00098 /// Return @a x to the @a y'th power. 00099 template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); 00100 /// Return complex sine of @a z. 00101 template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); 00102 /// Return complex hyperbolic sine of @a z. 00103 template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); 00104 /// Return complex square root of @a z. 00105 template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); 00106 /// Return complex tangent of @a z. 00107 template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); 00108 /// Return complex hyperbolic tangent of @a z. 00109 template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); 00110 00111 00112 // 26.2.2 Primary template class complex 00113 /** 00114 * Template to represent complex numbers. 00115 * 00116 * Specializations for float, double, and long double are part of the 00117 * library. Results with any other type are not guaranteed. 00118 * 00119 * @param Tp Type of real and imaginary values. 00120 */ 00121 template<typename _Tp> 00122 struct complex 00123 { 00124 /// Value typedef. 00125 typedef _Tp value_type; 00126 00127 /// Default constructor. First parameter is x, second parameter is y. 00128 /// Unspecified parameters default to 0. 00129 _GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp()) 00130 : _M_real(__r), _M_imag(__i) { } 00131 00132 // Lets the compiler synthesize the copy constructor 00133 // complex (const complex<_Tp>&); 00134 /// Copy constructor. 00135 template<typename _Up> 00136 _GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z) 00137 : _M_real(__z.real()), _M_imag(__z.imag()) { } 00138 00139 #if __cplusplus >= 201103L 00140 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00141 // DR 387. std::complex over-encapsulated. 00142 _GLIBCXX_ABI_TAG_CXX11 00143 constexpr _Tp 00144 real() { return _M_real; } 00145 00146 _GLIBCXX_ABI_TAG_CXX11 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 #if __cplusplus >= 201103L 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 template<typename _Tp> 00956 complex<_Tp> 00957 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 00958 { 00959 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 00960 00961 while (__n >>= 1) 00962 { 00963 __x *= __x; 00964 if (__n % 2) 00965 __y *= __x; 00966 } 00967 00968 return __y; 00969 } 00970 00971 // In C++11 mode we used to implement the resolution of 00972 // DR 844. complex pow return type is ambiguous. 00973 // thus the following overload was disabled in that mode. However, doing 00974 // that causes all sorts of issues, see, for example: 00975 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html 00976 // and also PR57974. 00977 template<typename _Tp> 00978 inline complex<_Tp> 00979 pow(const complex<_Tp>& __z, int __n) 00980 { 00981 return __n < 0 00982 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 00983 : std::__complex_pow_unsigned(__z, __n); 00984 } 00985 00986 template<typename _Tp> 00987 complex<_Tp> 00988 pow(const complex<_Tp>& __x, const _Tp& __y) 00989 { 00990 #ifndef _GLIBCXX_USE_C99_COMPLEX 00991 if (__x == _Tp()) 00992 return _Tp(); 00993 #endif 00994 if (__x.imag() == _Tp() && __x.real() > _Tp()) 00995 return pow(__x.real(), __y); 00996 00997 complex<_Tp> __t = std::log(__x); 00998 return std::polar(exp(__y * __t.real()), __y * __t.imag()); 00999 } 01000 01001 template<typename _Tp> 01002 inline complex<_Tp> 01003 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01004 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 01005 01006 #if _GLIBCXX_USE_C99_COMPLEX 01007 inline __complex__ float 01008 __complex_pow(__complex__ float __x, __complex__ float __y) 01009 { return __builtin_cpowf(__x, __y); } 01010 01011 inline __complex__ double 01012 __complex_pow(__complex__ double __x, __complex__ double __y) 01013 { return __builtin_cpow(__x, __y); } 01014 01015 inline __complex__ long double 01016 __complex_pow(const __complex__ long double& __x, 01017 const __complex__ long double& __y) 01018 { return __builtin_cpowl(__x, __y); } 01019 01020 template<typename _Tp> 01021 inline complex<_Tp> 01022 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01023 { return __complex_pow(__x.__rep(), __y.__rep()); } 01024 #else 01025 template<typename _Tp> 01026 inline complex<_Tp> 01027 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01028 { return __complex_pow(__x, __y); } 01029 #endif 01030 01031 template<typename _Tp> 01032 inline complex<_Tp> 01033 pow(const _Tp& __x, const complex<_Tp>& __y) 01034 { 01035 return __x > _Tp() ? std::polar(pow(__x, __y.real()), 01036 __y.imag() * log(__x)) 01037 : std::pow(complex<_Tp>(__x), __y); 01038 } 01039 01040 /// 26.2.3 complex specializations 01041 /// complex<float> specialization 01042 template<> 01043 struct complex<float> 01044 { 01045 typedef float value_type; 01046 typedef __complex__ float _ComplexT; 01047 01048 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01049 01050 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 01051 #if __cplusplus >= 201103L 01052 : _M_value{ __r, __i } { } 01053 #else 01054 { 01055 __real__ _M_value = __r; 01056 __imag__ _M_value = __i; 01057 } 01058 #endif 01059 01060 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 01061 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01062 01063 #if __cplusplus >= 201103L 01064 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01065 // DR 387. std::complex over-encapsulated. 01066 __attribute ((__abi_tag__ ("cxx11"))) 01067 constexpr float 01068 real() const { return __real__ _M_value; } 01069 01070 __attribute ((__abi_tag__ ("cxx11"))) 01071 constexpr float 01072 imag() const { return __imag__ _M_value; } 01073 #else 01074 float& 01075 real() { return __real__ _M_value; } 01076 01077 const float& 01078 real() const { return __real__ _M_value; } 01079 01080 float& 01081 imag() { return __imag__ _M_value; } 01082 01083 const float& 01084 imag() const { return __imag__ _M_value; } 01085 #endif 01086 01087 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01088 // DR 387. std::complex over-encapsulated. 01089 void 01090 real(float __val) { __real__ _M_value = __val; } 01091 01092 void 01093 imag(float __val) { __imag__ _M_value = __val; } 01094 01095 complex& 01096 operator=(float __f) 01097 { 01098 _M_value = __f; 01099 return *this; 01100 } 01101 01102 complex& 01103 operator+=(float __f) 01104 { 01105 _M_value += __f; 01106 return *this; 01107 } 01108 01109 complex& 01110 operator-=(float __f) 01111 { 01112 _M_value -= __f; 01113 return *this; 01114 } 01115 01116 complex& 01117 operator*=(float __f) 01118 { 01119 _M_value *= __f; 01120 return *this; 01121 } 01122 01123 complex& 01124 operator/=(float __f) 01125 { 01126 _M_value /= __f; 01127 return *this; 01128 } 01129 01130 // Let the compiler synthesize the copy and assignment 01131 // operator. It always does a pretty good job. 01132 // complex& operator=(const complex&); 01133 01134 template<typename _Tp> 01135 complex& 01136 operator=(const complex<_Tp>& __z) 01137 { 01138 __real__ _M_value = __z.real(); 01139 __imag__ _M_value = __z.imag(); 01140 return *this; 01141 } 01142 01143 template<typename _Tp> 01144 complex& 01145 operator+=(const complex<_Tp>& __z) 01146 { 01147 __real__ _M_value += __z.real(); 01148 __imag__ _M_value += __z.imag(); 01149 return *this; 01150 } 01151 01152 template<class _Tp> 01153 complex& 01154 operator-=(const complex<_Tp>& __z) 01155 { 01156 __real__ _M_value -= __z.real(); 01157 __imag__ _M_value -= __z.imag(); 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 template<class _Tp> 01173 complex& 01174 operator/=(const complex<_Tp>& __z) 01175 { 01176 _ComplexT __t; 01177 __real__ __t = __z.real(); 01178 __imag__ __t = __z.imag(); 01179 _M_value /= __t; 01180 return *this; 01181 } 01182 01183 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01184 01185 private: 01186 _ComplexT _M_value; 01187 }; 01188 01189 /// 26.2.3 complex specializations 01190 /// complex<double> specialization 01191 template<> 01192 struct complex<double> 01193 { 01194 typedef double value_type; 01195 typedef __complex__ double _ComplexT; 01196 01197 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01198 01199 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 01200 #if __cplusplus >= 201103L 01201 : _M_value{ __r, __i } { } 01202 #else 01203 { 01204 __real__ _M_value = __r; 01205 __imag__ _M_value = __i; 01206 } 01207 #endif 01208 01209 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01210 : _M_value(__z.__rep()) { } 01211 01212 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01213 01214 #if __cplusplus >= 201103L 01215 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01216 // DR 387. std::complex over-encapsulated. 01217 __attribute ((__abi_tag__ ("cxx11"))) 01218 constexpr double 01219 real() const { return __real__ _M_value; } 01220 01221 __attribute ((__abi_tag__ ("cxx11"))) 01222 constexpr double 01223 imag() const { return __imag__ _M_value; } 01224 #else 01225 double& 01226 real() { return __real__ _M_value; } 01227 01228 const double& 01229 real() const { return __real__ _M_value; } 01230 01231 double& 01232 imag() { return __imag__ _M_value; } 01233 01234 const double& 01235 imag() const { return __imag__ _M_value; } 01236 #endif 01237 01238 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01239 // DR 387. std::complex over-encapsulated. 01240 void 01241 real(double __val) { __real__ _M_value = __val; } 01242 01243 void 01244 imag(double __val) { __imag__ _M_value = __val; } 01245 01246 complex& 01247 operator=(double __d) 01248 { 01249 _M_value = __d; 01250 return *this; 01251 } 01252 01253 complex& 01254 operator+=(double __d) 01255 { 01256 _M_value += __d; 01257 return *this; 01258 } 01259 01260 complex& 01261 operator-=(double __d) 01262 { 01263 _M_value -= __d; 01264 return *this; 01265 } 01266 01267 complex& 01268 operator*=(double __d) 01269 { 01270 _M_value *= __d; 01271 return *this; 01272 } 01273 01274 complex& 01275 operator/=(double __d) 01276 { 01277 _M_value /= __d; 01278 return *this; 01279 } 01280 01281 // The compiler will synthesize this, efficiently. 01282 // complex& operator=(const complex&); 01283 01284 template<typename _Tp> 01285 complex& 01286 operator=(const complex<_Tp>& __z) 01287 { 01288 __real__ _M_value = __z.real(); 01289 __imag__ _M_value = __z.imag(); 01290 return *this; 01291 } 01292 01293 template<typename _Tp> 01294 complex& 01295 operator+=(const complex<_Tp>& __z) 01296 { 01297 __real__ _M_value += __z.real(); 01298 __imag__ _M_value += __z.imag(); 01299 return *this; 01300 } 01301 01302 template<typename _Tp> 01303 complex& 01304 operator-=(const complex<_Tp>& __z) 01305 { 01306 __real__ _M_value -= __z.real(); 01307 __imag__ _M_value -= __z.imag(); 01308 return *this; 01309 } 01310 01311 template<typename _Tp> 01312 complex& 01313 operator*=(const complex<_Tp>& __z) 01314 { 01315 _ComplexT __t; 01316 __real__ __t = __z.real(); 01317 __imag__ __t = __z.imag(); 01318 _M_value *= __t; 01319 return *this; 01320 } 01321 01322 template<typename _Tp> 01323 complex& 01324 operator/=(const complex<_Tp>& __z) 01325 { 01326 _ComplexT __t; 01327 __real__ __t = __z.real(); 01328 __imag__ __t = __z.imag(); 01329 _M_value /= __t; 01330 return *this; 01331 } 01332 01333 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01334 01335 private: 01336 _ComplexT _M_value; 01337 }; 01338 01339 /// 26.2.3 complex specializations 01340 /// complex<long double> specialization 01341 template<> 01342 struct complex<long double> 01343 { 01344 typedef long double value_type; 01345 typedef __complex__ long double _ComplexT; 01346 01347 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01348 01349 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 01350 long double __i = 0.0L) 01351 #if __cplusplus >= 201103L 01352 : _M_value{ __r, __i } { } 01353 #else 01354 { 01355 __real__ _M_value = __r; 01356 __imag__ _M_value = __i; 01357 } 01358 #endif 01359 01360 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01361 : _M_value(__z.__rep()) { } 01362 01363 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 01364 : _M_value(__z.__rep()) { } 01365 01366 #if __cplusplus >= 201103L 01367 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01368 // DR 387. std::complex over-encapsulated. 01369 __attribute ((__abi_tag__ ("cxx11"))) 01370 constexpr long double 01371 real() const { return __real__ _M_value; } 01372 01373 __attribute ((__abi_tag__ ("cxx11"))) 01374 constexpr long double 01375 imag() const { return __imag__ _M_value; } 01376 #else 01377 long double& 01378 real() { return __real__ _M_value; } 01379 01380 const long double& 01381 real() const { return __real__ _M_value; } 01382 01383 long double& 01384 imag() { return __imag__ _M_value; } 01385 01386 const long double& 01387 imag() const { return __imag__ _M_value; } 01388 #endif 01389 01390 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01391 // DR 387. std::complex over-encapsulated. 01392 void 01393 real(long double __val) { __real__ _M_value = __val; } 01394 01395 void 01396 imag(long double __val) { __imag__ _M_value = __val; } 01397 01398 complex& 01399 operator=(long double __r) 01400 { 01401 _M_value = __r; 01402 return *this; 01403 } 01404 01405 complex& 01406 operator+=(long double __r) 01407 { 01408 _M_value += __r; 01409 return *this; 01410 } 01411 01412 complex& 01413 operator-=(long double __r) 01414 { 01415 _M_value -= __r; 01416 return *this; 01417 } 01418 01419 complex& 01420 operator*=(long double __r) 01421 { 01422 _M_value *= __r; 01423 return *this; 01424 } 01425 01426 complex& 01427 operator/=(long double __r) 01428 { 01429 _M_value /= __r; 01430 return *this; 01431 } 01432 01433 // The compiler knows how to do this efficiently 01434 // complex& operator=(const complex&); 01435 01436 template<typename _Tp> 01437 complex& 01438 operator=(const complex<_Tp>& __z) 01439 { 01440 __real__ _M_value = __z.real(); 01441 __imag__ _M_value = __z.imag(); 01442 return *this; 01443 } 01444 01445 template<typename _Tp> 01446 complex& 01447 operator+=(const complex<_Tp>& __z) 01448 { 01449 __real__ _M_value += __z.real(); 01450 __imag__ _M_value += __z.imag(); 01451 return *this; 01452 } 01453 01454 template<typename _Tp> 01455 complex& 01456 operator-=(const complex<_Tp>& __z) 01457 { 01458 __real__ _M_value -= __z.real(); 01459 __imag__ _M_value -= __z.imag(); 01460 return *this; 01461 } 01462 01463 template<typename _Tp> 01464 complex& 01465 operator*=(const complex<_Tp>& __z) 01466 { 01467 _ComplexT __t; 01468 __real__ __t = __z.real(); 01469 __imag__ __t = __z.imag(); 01470 _M_value *= __t; 01471 return *this; 01472 } 01473 01474 template<typename _Tp> 01475 complex& 01476 operator/=(const complex<_Tp>& __z) 01477 { 01478 _ComplexT __t; 01479 __real__ __t = __z.real(); 01480 __imag__ __t = __z.imag(); 01481 _M_value /= __t; 01482 return *this; 01483 } 01484 01485 _GLIBCXX_USE_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01486 01487 private: 01488 _ComplexT _M_value; 01489 }; 01490 01491 // These bits have to be at the end of this file, so that the 01492 // specializations have all been defined. 01493 inline _GLIBCXX_CONSTEXPR 01494 complex<float>::complex(const complex<double>& __z) 01495 : _M_value(__z.__rep()) { } 01496 01497 inline _GLIBCXX_CONSTEXPR 01498 complex<float>::complex(const complex<long double>& __z) 01499 : _M_value(__z.__rep()) { } 01500 01501 inline _GLIBCXX_CONSTEXPR 01502 complex<double>::complex(const complex<long double>& __z) 01503 : _M_value(__z.__rep()) { } 01504 01505 // Inhibit implicit instantiations for required instantiations, 01506 // which are defined via explicit instantiations elsewhere. 01507 // NB: This syntax is a GNU extension. 01508 #if _GLIBCXX_EXTERN_TEMPLATE 01509 extern template istream& operator>>(istream&, complex<float>&); 01510 extern template ostream& operator<<(ostream&, const complex<float>&); 01511 extern template istream& operator>>(istream&, complex<double>&); 01512 extern template ostream& operator<<(ostream&, const complex<double>&); 01513 extern template istream& operator>>(istream&, complex<long double>&); 01514 extern template ostream& operator<<(ostream&, const complex<long double>&); 01515 01516 #ifdef _GLIBCXX_USE_WCHAR_T 01517 extern template wistream& operator>>(wistream&, complex<float>&); 01518 extern template wostream& operator<<(wostream&, const complex<float>&); 01519 extern template wistream& operator>>(wistream&, complex<double>&); 01520 extern template wostream& operator<<(wostream&, const complex<double>&); 01521 extern template wistream& operator>>(wistream&, complex<long double>&); 01522 extern template wostream& operator<<(wostream&, const complex<long double>&); 01523 #endif 01524 #endif 01525 01526 // @} group complex_numbers 01527 01528 _GLIBCXX_END_NAMESPACE_VERSION 01529 } // namespace 01530 01531 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 01532 { 01533 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01534 01535 // See ext/type_traits.h for the primary template. 01536 template<typename _Tp, typename _Up> 01537 struct __promote_2<std::complex<_Tp>, _Up> 01538 { 01539 public: 01540 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01541 }; 01542 01543 template<typename _Tp, typename _Up> 01544 struct __promote_2<_Tp, std::complex<_Up> > 01545 { 01546 public: 01547 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01548 }; 01549 01550 template<typename _Tp, typename _Up> 01551 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 01552 { 01553 public: 01554 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01555 }; 01556 01557 _GLIBCXX_END_NAMESPACE_VERSION 01558 } // namespace 01559 01560 #if __cplusplus >= 201103L 01561 01562 namespace std _GLIBCXX_VISIBILITY(default) 01563 { 01564 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01565 01566 // Forward declarations. 01567 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 01568 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 01569 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 01570 01571 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 01572 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 01573 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 01574 // DR 595. 01575 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 01576 01577 template<typename _Tp> 01578 inline std::complex<_Tp> 01579 __complex_acos(const std::complex<_Tp>& __z) 01580 { 01581 const std::complex<_Tp> __t = std::asin(__z); 01582 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 01583 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 01584 } 01585 01586 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01587 inline __complex__ float 01588 __complex_acos(__complex__ float __z) 01589 { return __builtin_cacosf(__z); } 01590 01591 inline __complex__ double 01592 __complex_acos(__complex__ double __z) 01593 { return __builtin_cacos(__z); } 01594 01595 inline __complex__ long double 01596 __complex_acos(const __complex__ long double& __z) 01597 { return __builtin_cacosl(__z); } 01598 01599 template<typename _Tp> 01600 inline std::complex<_Tp> 01601 acos(const std::complex<_Tp>& __z) 01602 { return __complex_acos(__z.__rep()); } 01603 #else 01604 /// acos(__z) [8.1.2]. 01605 // Effects: Behaves the same as C99 function cacos, defined 01606 // in subclause 7.3.5.1. 01607 template<typename _Tp> 01608 inline std::complex<_Tp> 01609 acos(const std::complex<_Tp>& __z) 01610 { return __complex_acos(__z); } 01611 #endif 01612 01613 template<typename _Tp> 01614 inline std::complex<_Tp> 01615 __complex_asin(const std::complex<_Tp>& __z) 01616 { 01617 std::complex<_Tp> __t(-__z.imag(), __z.real()); 01618 __t = std::asinh(__t); 01619 return std::complex<_Tp>(__t.imag(), -__t.real()); 01620 } 01621 01622 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01623 inline __complex__ float 01624 __complex_asin(__complex__ float __z) 01625 { return __builtin_casinf(__z); } 01626 01627 inline __complex__ double 01628 __complex_asin(__complex__ double __z) 01629 { return __builtin_casin(__z); } 01630 01631 inline __complex__ long double 01632 __complex_asin(const __complex__ long double& __z) 01633 { return __builtin_casinl(__z); } 01634 01635 template<typename _Tp> 01636 inline std::complex<_Tp> 01637 asin(const std::complex<_Tp>& __z) 01638 { return __complex_asin(__z.__rep()); } 01639 #else 01640 /// asin(__z) [8.1.3]. 01641 // Effects: Behaves the same as C99 function casin, defined 01642 // in subclause 7.3.5.2. 01643 template<typename _Tp> 01644 inline std::complex<_Tp> 01645 asin(const std::complex<_Tp>& __z) 01646 { return __complex_asin(__z); } 01647 #endif 01648 01649 template<typename _Tp> 01650 std::complex<_Tp> 01651 __complex_atan(const std::complex<_Tp>& __z) 01652 { 01653 const _Tp __r2 = __z.real() * __z.real(); 01654 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 01655 01656 _Tp __num = __z.imag() + _Tp(1.0); 01657 _Tp __den = __z.imag() - _Tp(1.0); 01658 01659 __num = __r2 + __num * __num; 01660 __den = __r2 + __den * __den; 01661 01662 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 01663 _Tp(0.25) * log(__num / __den)); 01664 } 01665 01666 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01667 inline __complex__ float 01668 __complex_atan(__complex__ float __z) 01669 { return __builtin_catanf(__z); } 01670 01671 inline __complex__ double 01672 __complex_atan(__complex__ double __z) 01673 { return __builtin_catan(__z); } 01674 01675 inline __complex__ long double 01676 __complex_atan(const __complex__ long double& __z) 01677 { return __builtin_catanl(__z); } 01678 01679 template<typename _Tp> 01680 inline std::complex<_Tp> 01681 atan(const std::complex<_Tp>& __z) 01682 { return __complex_atan(__z.__rep()); } 01683 #else 01684 /// atan(__z) [8.1.4]. 01685 // Effects: Behaves the same as C99 function catan, defined 01686 // in subclause 7.3.5.3. 01687 template<typename _Tp> 01688 inline std::complex<_Tp> 01689 atan(const std::complex<_Tp>& __z) 01690 { return __complex_atan(__z); } 01691 #endif 01692 01693 template<typename _Tp> 01694 std::complex<_Tp> 01695 __complex_acosh(const std::complex<_Tp>& __z) 01696 { 01697 // Kahan's formula. 01698 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 01699 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 01700 } 01701 01702 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01703 inline __complex__ float 01704 __complex_acosh(__complex__ float __z) 01705 { return __builtin_cacoshf(__z); } 01706 01707 inline __complex__ double 01708 __complex_acosh(__complex__ double __z) 01709 { return __builtin_cacosh(__z); } 01710 01711 inline __complex__ long double 01712 __complex_acosh(const __complex__ long double& __z) 01713 { return __builtin_cacoshl(__z); } 01714 01715 template<typename _Tp> 01716 inline std::complex<_Tp> 01717 acosh(const std::complex<_Tp>& __z) 01718 { return __complex_acosh(__z.__rep()); } 01719 #else 01720 /// acosh(__z) [8.1.5]. 01721 // Effects: Behaves the same as C99 function cacosh, defined 01722 // in subclause 7.3.6.1. 01723 template<typename _Tp> 01724 inline std::complex<_Tp> 01725 acosh(const std::complex<_Tp>& __z) 01726 { return __complex_acosh(__z); } 01727 #endif 01728 01729 template<typename _Tp> 01730 std::complex<_Tp> 01731 __complex_asinh(const std::complex<_Tp>& __z) 01732 { 01733 std::complex<_Tp> __t((__z.real() - __z.imag()) 01734 * (__z.real() + __z.imag()) + _Tp(1.0), 01735 _Tp(2.0) * __z.real() * __z.imag()); 01736 __t = std::sqrt(__t); 01737 01738 return std::log(__t + __z); 01739 } 01740 01741 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01742 inline __complex__ float 01743 __complex_asinh(__complex__ float __z) 01744 { return __builtin_casinhf(__z); } 01745 01746 inline __complex__ double 01747 __complex_asinh(__complex__ double __z) 01748 { return __builtin_casinh(__z); } 01749 01750 inline __complex__ long double 01751 __complex_asinh(const __complex__ long double& __z) 01752 { return __builtin_casinhl(__z); } 01753 01754 template<typename _Tp> 01755 inline std::complex<_Tp> 01756 asinh(const std::complex<_Tp>& __z) 01757 { return __complex_asinh(__z.__rep()); } 01758 #else 01759 /// asinh(__z) [8.1.6]. 01760 // Effects: Behaves the same as C99 function casin, defined 01761 // in subclause 7.3.6.2. 01762 template<typename _Tp> 01763 inline std::complex<_Tp> 01764 asinh(const std::complex<_Tp>& __z) 01765 { return __complex_asinh(__z); } 01766 #endif 01767 01768 template<typename _Tp> 01769 std::complex<_Tp> 01770 __complex_atanh(const std::complex<_Tp>& __z) 01771 { 01772 const _Tp __i2 = __z.imag() * __z.imag(); 01773 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 01774 01775 _Tp __num = _Tp(1.0) + __z.real(); 01776 _Tp __den = _Tp(1.0) - __z.real(); 01777 01778 __num = __i2 + __num * __num; 01779 __den = __i2 + __den * __den; 01780 01781 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 01782 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 01783 } 01784 01785 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01786 inline __complex__ float 01787 __complex_atanh(__complex__ float __z) 01788 { return __builtin_catanhf(__z); } 01789 01790 inline __complex__ double 01791 __complex_atanh(__complex__ double __z) 01792 { return __builtin_catanh(__z); } 01793 01794 inline __complex__ long double 01795 __complex_atanh(const __complex__ long double& __z) 01796 { return __builtin_catanhl(__z); } 01797 01798 template<typename _Tp> 01799 inline std::complex<_Tp> 01800 atanh(const std::complex<_Tp>& __z) 01801 { return __complex_atanh(__z.__rep()); } 01802 #else 01803 /// atanh(__z) [8.1.7]. 01804 // Effects: Behaves the same as C99 function catanh, defined 01805 // in subclause 7.3.6.3. 01806 template<typename _Tp> 01807 inline std::complex<_Tp> 01808 atanh(const std::complex<_Tp>& __z) 01809 { return __complex_atanh(__z); } 01810 #endif 01811 01812 template<typename _Tp> 01813 inline _Tp 01814 /// fabs(__z) [8.1.8]. 01815 // Effects: Behaves the same as C99 function cabs, defined 01816 // in subclause 7.3.8.1. 01817 fabs(const std::complex<_Tp>& __z) 01818 { return std::abs(__z); } 01819 01820 /// Additional overloads [8.1.9]. 01821 template<typename _Tp> 01822 inline typename __gnu_cxx::__promote<_Tp>::__type 01823 arg(_Tp __x) 01824 { 01825 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01826 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 01827 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 01828 : __type(); 01829 #else 01830 return std::arg(std::complex<__type>(__x)); 01831 #endif 01832 } 01833 01834 template<typename _Tp> 01835 inline typename __gnu_cxx::__promote<_Tp>::__type 01836 imag(_Tp) 01837 { return _Tp(); } 01838 01839 template<typename _Tp> 01840 inline typename __gnu_cxx::__promote<_Tp>::__type 01841 norm(_Tp __x) 01842 { 01843 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01844 return __type(__x) * __type(__x); 01845 } 01846 01847 template<typename _Tp> 01848 inline typename __gnu_cxx::__promote<_Tp>::__type 01849 real(_Tp __x) 01850 { return __x; } 01851 01852 template<typename _Tp, typename _Up> 01853 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01854 pow(const std::complex<_Tp>& __x, const _Up& __y) 01855 { 01856 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01857 return std::pow(std::complex<__type>(__x), __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 _Tp& __x, const std::complex<_Up>& __y) 01863 { 01864 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01865 return std::pow(__type(__x), std::complex<__type>(__y)); 01866 } 01867 01868 template<typename _Tp, typename _Up> 01869 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01870 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 01871 { 01872 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01873 return std::pow(std::complex<__type>(__x), 01874 std::complex<__type>(__y)); 01875 } 01876 01877 // Forward declarations. 01878 // DR 781. 01879 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 01880 01881 template<typename _Tp> 01882 std::complex<_Tp> 01883 __complex_proj(const std::complex<_Tp>& __z) 01884 { 01885 const _Tp __den = (__z.real() * __z.real() 01886 + __z.imag() * __z.imag() + _Tp(1.0)); 01887 01888 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 01889 (_Tp(2.0) * __z.imag()) / __den); 01890 } 01891 01892 #if _GLIBCXX_USE_C99_COMPLEX 01893 inline __complex__ float 01894 __complex_proj(__complex__ float __z) 01895 { return __builtin_cprojf(__z); } 01896 01897 inline __complex__ double 01898 __complex_proj(__complex__ double __z) 01899 { return __builtin_cproj(__z); } 01900 01901 inline __complex__ long double 01902 __complex_proj(const __complex__ long double& __z) 01903 { return __builtin_cprojl(__z); } 01904 01905 template<typename _Tp> 01906 inline std::complex<_Tp> 01907 proj(const std::complex<_Tp>& __z) 01908 { return __complex_proj(__z.__rep()); } 01909 #else 01910 template<typename _Tp> 01911 inline std::complex<_Tp> 01912 proj(const std::complex<_Tp>& __z) 01913 { return __complex_proj(__z); } 01914 #endif 01915 01916 // DR 1137. 01917 template<typename _Tp> 01918 inline typename __gnu_cxx::__promote<_Tp>::__type 01919 proj(_Tp __x) 01920 { return __x; } 01921 01922 template<typename _Tp> 01923 inline typename __gnu_cxx::__promote<_Tp>::__type 01924 conj(_Tp __x) 01925 { return __x; } 01926 01927 #if __cplusplus > 201103L 01928 01929 inline namespace literals { 01930 inline namespace complex_literals { 01931 01932 constexpr std::complex<float> 01933 operator""if(long double __num) 01934 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 01935 01936 constexpr std::complex<float> 01937 operator""if(unsigned long long __num) 01938 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 01939 01940 constexpr std::complex<double> 01941 operator""i(long double __num) 01942 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 01943 01944 constexpr std::complex<double> 01945 operator""i(unsigned long long __num) 01946 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 01947 01948 constexpr std::complex<long double> 01949 operator""il(long double __num) 01950 { return std::complex<long double>{0.0L, __num}; } 01951 01952 constexpr std::complex<long double> 01953 operator""il(unsigned long long __num) 01954 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } 01955 01956 } // inline namespace complex_literals 01957 } // inline namespace literals 01958 01959 #endif // C++14 01960 01961 _GLIBCXX_END_NAMESPACE_VERSION 01962 } // namespace 01963 01964 #endif // C++11 01965 01966 #endif /* _GLIBCXX_COMPLEX */