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