libstdc++
|
00001 // The template and inlines for the -*- C++ -*- complex number classes. 00002 00003 // Copyright (C) 1997-2015 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; 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 00515 { 00516 __is.putback(__ch); 00517 __is >> __re_x; 00518 __x = __re_x; 00519 } 00520 return __is; 00521 } 00522 00523 /// Insertion operator for complex values. 00524 template<typename _Tp, typename _CharT, class _Traits> 00525 basic_ostream<_CharT, _Traits>& 00526 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) 00527 { 00528 basic_ostringstream<_CharT, _Traits> __s; 00529 __s.flags(__os.flags()); 00530 __s.imbue(__os.getloc()); 00531 __s.precision(__os.precision()); 00532 __s << '(' << __x.real() << ',' << __x.imag() << ')'; 00533 return __os << __s.str(); 00534 } 00535 00536 // Values 00537 #if __cplusplus >= 201103L 00538 template<typename _Tp> 00539 constexpr _Tp 00540 real(const complex<_Tp>& __z) 00541 { return __z.real(); } 00542 00543 template<typename _Tp> 00544 constexpr _Tp 00545 imag(const complex<_Tp>& __z) 00546 { return __z.imag(); } 00547 #else 00548 template<typename _Tp> 00549 inline _Tp& 00550 real(complex<_Tp>& __z) 00551 { return __z.real(); } 00552 00553 template<typename _Tp> 00554 inline const _Tp& 00555 real(const complex<_Tp>& __z) 00556 { return __z.real(); } 00557 00558 template<typename _Tp> 00559 inline _Tp& 00560 imag(complex<_Tp>& __z) 00561 { return __z.imag(); } 00562 00563 template<typename _Tp> 00564 inline const _Tp& 00565 imag(const complex<_Tp>& __z) 00566 { return __z.imag(); } 00567 #endif 00568 00569 // 26.2.7/3 abs(__z): Returns the magnitude of __z. 00570 template<typename _Tp> 00571 inline _Tp 00572 __complex_abs(const complex<_Tp>& __z) 00573 { 00574 _Tp __x = __z.real(); 00575 _Tp __y = __z.imag(); 00576 const _Tp __s = std::max(abs(__x), abs(__y)); 00577 if (__s == _Tp()) // well ... 00578 return __s; 00579 __x /= __s; 00580 __y /= __s; 00581 return __s * sqrt(__x * __x + __y * __y); 00582 } 00583 00584 #if _GLIBCXX_USE_C99_COMPLEX 00585 inline float 00586 __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } 00587 00588 inline double 00589 __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } 00590 00591 inline long double 00592 __complex_abs(const __complex__ long double& __z) 00593 { return __builtin_cabsl(__z); } 00594 00595 template<typename _Tp> 00596 inline _Tp 00597 abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); } 00598 #else 00599 template<typename _Tp> 00600 inline _Tp 00601 abs(const complex<_Tp>& __z) { return __complex_abs(__z); } 00602 #endif 00603 00604 00605 // 26.2.7/4: arg(__z): Returns the phase angle of __z. 00606 template<typename _Tp> 00607 inline _Tp 00608 __complex_arg(const complex<_Tp>& __z) 00609 { return atan2(__z.imag(), __z.real()); } 00610 00611 #if _GLIBCXX_USE_C99_COMPLEX 00612 inline float 00613 __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } 00614 00615 inline double 00616 __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } 00617 00618 inline long double 00619 __complex_arg(const __complex__ long double& __z) 00620 { return __builtin_cargl(__z); } 00621 00622 template<typename _Tp> 00623 inline _Tp 00624 arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); } 00625 #else 00626 template<typename _Tp> 00627 inline _Tp 00628 arg(const complex<_Tp>& __z) { return __complex_arg(__z); } 00629 #endif 00630 00631 // 26.2.7/5: norm(__z) returns the squared magnitude of __z. 00632 // As defined, norm() is -not- a norm is the common mathematical 00633 // sense used in numerics. The helper class _Norm_helper<> tries to 00634 // distinguish between builtin floating point and the rest, so as 00635 // to deliver an answer as close as possible to the real value. 00636 template<bool> 00637 struct _Norm_helper 00638 { 00639 template<typename _Tp> 00640 static inline _Tp _S_do_it(const complex<_Tp>& __z) 00641 { 00642 const _Tp __x = __z.real(); 00643 const _Tp __y = __z.imag(); 00644 return __x * __x + __y * __y; 00645 } 00646 }; 00647 00648 template<> 00649 struct _Norm_helper<true> 00650 { 00651 template<typename _Tp> 00652 static inline _Tp _S_do_it(const complex<_Tp>& __z) 00653 { 00654 _Tp __res = std::abs(__z); 00655 return __res * __res; 00656 } 00657 }; 00658 00659 template<typename _Tp> 00660 inline _Tp 00661 norm(const complex<_Tp>& __z) 00662 { 00663 return _Norm_helper<__is_floating<_Tp>::__value 00664 && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); 00665 } 00666 00667 template<typename _Tp> 00668 inline complex<_Tp> 00669 polar(const _Tp& __rho, const _Tp& __theta) 00670 { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } 00671 00672 template<typename _Tp> 00673 inline complex<_Tp> 00674 conj(const complex<_Tp>& __z) 00675 { return complex<_Tp>(__z.real(), -__z.imag()); } 00676 00677 // Transcendentals 00678 00679 // 26.2.8/1 cos(__z): Returns the cosine of __z. 00680 template<typename _Tp> 00681 inline complex<_Tp> 00682 __complex_cos(const complex<_Tp>& __z) 00683 { 00684 const _Tp __x = __z.real(); 00685 const _Tp __y = __z.imag(); 00686 return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); 00687 } 00688 00689 #if _GLIBCXX_USE_C99_COMPLEX 00690 inline __complex__ float 00691 __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } 00692 00693 inline __complex__ double 00694 __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } 00695 00696 inline __complex__ long double 00697 __complex_cos(const __complex__ long double& __z) 00698 { return __builtin_ccosl(__z); } 00699 00700 template<typename _Tp> 00701 inline complex<_Tp> 00702 cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); } 00703 #else 00704 template<typename _Tp> 00705 inline complex<_Tp> 00706 cos(const complex<_Tp>& __z) { return __complex_cos(__z); } 00707 #endif 00708 00709 // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. 00710 template<typename _Tp> 00711 inline complex<_Tp> 00712 __complex_cosh(const complex<_Tp>& __z) 00713 { 00714 const _Tp __x = __z.real(); 00715 const _Tp __y = __z.imag(); 00716 return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); 00717 } 00718 00719 #if _GLIBCXX_USE_C99_COMPLEX 00720 inline __complex__ float 00721 __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } 00722 00723 inline __complex__ double 00724 __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } 00725 00726 inline __complex__ long double 00727 __complex_cosh(const __complex__ long double& __z) 00728 { return __builtin_ccoshl(__z); } 00729 00730 template<typename _Tp> 00731 inline complex<_Tp> 00732 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); } 00733 #else 00734 template<typename _Tp> 00735 inline complex<_Tp> 00736 cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); } 00737 #endif 00738 00739 // 26.2.8/3 exp(__z): Returns the complex base e exponential of x 00740 template<typename _Tp> 00741 inline complex<_Tp> 00742 __complex_exp(const complex<_Tp>& __z) 00743 { return std::polar<_Tp>(exp(__z.real()), __z.imag()); } 00744 00745 #if _GLIBCXX_USE_C99_COMPLEX 00746 inline __complex__ float 00747 __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } 00748 00749 inline __complex__ double 00750 __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } 00751 00752 inline __complex__ long double 00753 __complex_exp(const __complex__ long double& __z) 00754 { return __builtin_cexpl(__z); } 00755 00756 template<typename _Tp> 00757 inline complex<_Tp> 00758 exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); } 00759 #else 00760 template<typename _Tp> 00761 inline complex<_Tp> 00762 exp(const complex<_Tp>& __z) { return __complex_exp(__z); } 00763 #endif 00764 00765 // 26.2.8/5 log(__z): Returns the natural complex logarithm of __z. 00766 // The branch cut is along the negative axis. 00767 template<typename _Tp> 00768 inline complex<_Tp> 00769 __complex_log(const complex<_Tp>& __z) 00770 { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } 00771 00772 #if _GLIBCXX_USE_C99_COMPLEX 00773 inline __complex__ float 00774 __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } 00775 00776 inline __complex__ double 00777 __complex_log(__complex__ double __z) { return __builtin_clog(__z); } 00778 00779 inline __complex__ long double 00780 __complex_log(const __complex__ long double& __z) 00781 { return __builtin_clogl(__z); } 00782 00783 template<typename _Tp> 00784 inline complex<_Tp> 00785 log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); } 00786 #else 00787 template<typename _Tp> 00788 inline complex<_Tp> 00789 log(const complex<_Tp>& __z) { return __complex_log(__z); } 00790 #endif 00791 00792 template<typename _Tp> 00793 inline complex<_Tp> 00794 log10(const complex<_Tp>& __z) 00795 { return std::log(__z) / log(_Tp(10.0)); } 00796 00797 // 26.2.8/10 sin(__z): Returns the sine of __z. 00798 template<typename _Tp> 00799 inline complex<_Tp> 00800 __complex_sin(const complex<_Tp>& __z) 00801 { 00802 const _Tp __x = __z.real(); 00803 const _Tp __y = __z.imag(); 00804 return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); 00805 } 00806 00807 #if _GLIBCXX_USE_C99_COMPLEX 00808 inline __complex__ float 00809 __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } 00810 00811 inline __complex__ double 00812 __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } 00813 00814 inline __complex__ long double 00815 __complex_sin(const __complex__ long double& __z) 00816 { return __builtin_csinl(__z); } 00817 00818 template<typename _Tp> 00819 inline complex<_Tp> 00820 sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); } 00821 #else 00822 template<typename _Tp> 00823 inline complex<_Tp> 00824 sin(const complex<_Tp>& __z) { return __complex_sin(__z); } 00825 #endif 00826 00827 // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. 00828 template<typename _Tp> 00829 inline complex<_Tp> 00830 __complex_sinh(const complex<_Tp>& __z) 00831 { 00832 const _Tp __x = __z.real(); 00833 const _Tp __y = __z.imag(); 00834 return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); 00835 } 00836 00837 #if _GLIBCXX_USE_C99_COMPLEX 00838 inline __complex__ float 00839 __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } 00840 00841 inline __complex__ double 00842 __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } 00843 00844 inline __complex__ long double 00845 __complex_sinh(const __complex__ long double& __z) 00846 { return __builtin_csinhl(__z); } 00847 00848 template<typename _Tp> 00849 inline complex<_Tp> 00850 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); } 00851 #else 00852 template<typename _Tp> 00853 inline complex<_Tp> 00854 sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); } 00855 #endif 00856 00857 // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. 00858 // The branch cut is on the negative axis. 00859 template<typename _Tp> 00860 complex<_Tp> 00861 __complex_sqrt(const complex<_Tp>& __z) 00862 { 00863 _Tp __x = __z.real(); 00864 _Tp __y = __z.imag(); 00865 00866 if (__x == _Tp()) 00867 { 00868 _Tp __t = sqrt(abs(__y) / 2); 00869 return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); 00870 } 00871 else 00872 { 00873 _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); 00874 _Tp __u = __t / 2; 00875 return __x > _Tp() 00876 ? complex<_Tp>(__u, __y / __t) 00877 : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); 00878 } 00879 } 00880 00881 #if _GLIBCXX_USE_C99_COMPLEX 00882 inline __complex__ float 00883 __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } 00884 00885 inline __complex__ double 00886 __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } 00887 00888 inline __complex__ long double 00889 __complex_sqrt(const __complex__ long double& __z) 00890 { return __builtin_csqrtl(__z); } 00891 00892 template<typename _Tp> 00893 inline complex<_Tp> 00894 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); } 00895 #else 00896 template<typename _Tp> 00897 inline complex<_Tp> 00898 sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); } 00899 #endif 00900 00901 // 26.2.8/14 tan(__z): Return the complex tangent of __z. 00902 00903 template<typename _Tp> 00904 inline complex<_Tp> 00905 __complex_tan(const complex<_Tp>& __z) 00906 { return std::sin(__z) / std::cos(__z); } 00907 00908 #if _GLIBCXX_USE_C99_COMPLEX 00909 inline __complex__ float 00910 __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } 00911 00912 inline __complex__ double 00913 __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } 00914 00915 inline __complex__ long double 00916 __complex_tan(const __complex__ long double& __z) 00917 { return __builtin_ctanl(__z); } 00918 00919 template<typename _Tp> 00920 inline complex<_Tp> 00921 tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); } 00922 #else 00923 template<typename _Tp> 00924 inline complex<_Tp> 00925 tan(const complex<_Tp>& __z) { return __complex_tan(__z); } 00926 #endif 00927 00928 00929 // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. 00930 00931 template<typename _Tp> 00932 inline complex<_Tp> 00933 __complex_tanh(const complex<_Tp>& __z) 00934 { return std::sinh(__z) / std::cosh(__z); } 00935 00936 #if _GLIBCXX_USE_C99_COMPLEX 00937 inline __complex__ float 00938 __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } 00939 00940 inline __complex__ double 00941 __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } 00942 00943 inline __complex__ long double 00944 __complex_tanh(const __complex__ long double& __z) 00945 { return __builtin_ctanhl(__z); } 00946 00947 template<typename _Tp> 00948 inline complex<_Tp> 00949 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); } 00950 #else 00951 template<typename _Tp> 00952 inline complex<_Tp> 00953 tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); } 00954 #endif 00955 00956 00957 // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x 00958 // raised to the __y-th power. The branch 00959 // cut is on the negative axis. 00960 template<typename _Tp> 00961 complex<_Tp> 00962 __complex_pow_unsigned(complex<_Tp> __x, unsigned __n) 00963 { 00964 complex<_Tp> __y = __n % 2 ? __x : complex<_Tp>(1); 00965 00966 while (__n >>= 1) 00967 { 00968 __x *= __x; 00969 if (__n % 2) 00970 __y *= __x; 00971 } 00972 00973 return __y; 00974 } 00975 00976 // In C++11 mode we used to implement the resolution of 00977 // DR 844. complex pow return type is ambiguous. 00978 // thus the following overload was disabled in that mode. However, doing 00979 // that causes all sorts of issues, see, for example: 00980 // http://gcc.gnu.org/ml/libstdc++/2013-01/msg00058.html 00981 // and also PR57974. 00982 template<typename _Tp> 00983 inline complex<_Tp> 00984 pow(const complex<_Tp>& __z, int __n) 00985 { 00986 return __n < 0 00987 ? complex<_Tp>(1) / std::__complex_pow_unsigned(__z, -(unsigned)__n) 00988 : std::__complex_pow_unsigned(__z, __n); 00989 } 00990 00991 template<typename _Tp> 00992 complex<_Tp> 00993 pow(const complex<_Tp>& __x, const _Tp& __y) 00994 { 00995 #ifndef _GLIBCXX_USE_C99_COMPLEX 00996 if (__x == _Tp()) 00997 return _Tp(); 00998 #endif 00999 if (__x.imag() == _Tp() && __x.real() > _Tp()) 01000 return pow(__x.real(), __y); 01001 01002 complex<_Tp> __t = std::log(__x); 01003 return std::polar<_Tp>(exp(__y * __t.real()), __y * __t.imag()); 01004 } 01005 01006 template<typename _Tp> 01007 inline complex<_Tp> 01008 __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01009 { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } 01010 01011 #if _GLIBCXX_USE_C99_COMPLEX 01012 inline __complex__ float 01013 __complex_pow(__complex__ float __x, __complex__ float __y) 01014 { return __builtin_cpowf(__x, __y); } 01015 01016 inline __complex__ double 01017 __complex_pow(__complex__ double __x, __complex__ double __y) 01018 { return __builtin_cpow(__x, __y); } 01019 01020 inline __complex__ long double 01021 __complex_pow(const __complex__ long double& __x, 01022 const __complex__ long double& __y) 01023 { return __builtin_cpowl(__x, __y); } 01024 01025 template<typename _Tp> 01026 inline complex<_Tp> 01027 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01028 { return __complex_pow(__x.__rep(), __y.__rep()); } 01029 #else 01030 template<typename _Tp> 01031 inline complex<_Tp> 01032 pow(const complex<_Tp>& __x, const complex<_Tp>& __y) 01033 { return __complex_pow(__x, __y); } 01034 #endif 01035 01036 template<typename _Tp> 01037 inline complex<_Tp> 01038 pow(const _Tp& __x, const complex<_Tp>& __y) 01039 { 01040 return __x > _Tp() ? std::polar<_Tp>(pow(__x, __y.real()), 01041 __y.imag() * log(__x)) 01042 : std::pow(complex<_Tp>(__x), __y); 01043 } 01044 01045 /// 26.2.3 complex specializations 01046 /// complex<float> specialization 01047 template<> 01048 struct complex<float> 01049 { 01050 typedef float value_type; 01051 typedef __complex__ float _ComplexT; 01052 01053 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01054 01055 _GLIBCXX_CONSTEXPR complex(float __r = 0.0f, float __i = 0.0f) 01056 #if __cplusplus >= 201103L 01057 : _M_value{ __r, __i } { } 01058 #else 01059 { 01060 __real__ _M_value = __r; 01061 __imag__ _M_value = __i; 01062 } 01063 #endif 01064 01065 explicit _GLIBCXX_CONSTEXPR complex(const complex<double>&); 01066 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01067 01068 #if __cplusplus >= 201103L 01069 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01070 // DR 387. std::complex over-encapsulated. 01071 __attribute ((__abi_tag__ ("cxx11"))) 01072 constexpr float 01073 real() const { return __real__ _M_value; } 01074 01075 __attribute ((__abi_tag__ ("cxx11"))) 01076 constexpr float 01077 imag() const { return __imag__ _M_value; } 01078 #else 01079 float& 01080 real() { return __real__ _M_value; } 01081 01082 const float& 01083 real() const { return __real__ _M_value; } 01084 01085 float& 01086 imag() { return __imag__ _M_value; } 01087 01088 const float& 01089 imag() const { return __imag__ _M_value; } 01090 #endif 01091 01092 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01093 // DR 387. std::complex over-encapsulated. 01094 void 01095 real(float __val) { __real__ _M_value = __val; } 01096 01097 void 01098 imag(float __val) { __imag__ _M_value = __val; } 01099 01100 complex& 01101 operator=(float __f) 01102 { 01103 _M_value = __f; 01104 return *this; 01105 } 01106 01107 complex& 01108 operator+=(float __f) 01109 { 01110 _M_value += __f; 01111 return *this; 01112 } 01113 01114 complex& 01115 operator-=(float __f) 01116 { 01117 _M_value -= __f; 01118 return *this; 01119 } 01120 01121 complex& 01122 operator*=(float __f) 01123 { 01124 _M_value *= __f; 01125 return *this; 01126 } 01127 01128 complex& 01129 operator/=(float __f) 01130 { 01131 _M_value /= __f; 01132 return *this; 01133 } 01134 01135 // Let the compiler synthesize the copy and assignment 01136 // operator. It always does a pretty good job. 01137 // complex& operator=(const complex&); 01138 01139 template<typename _Tp> 01140 complex& 01141 operator=(const complex<_Tp>& __z) 01142 { 01143 __real__ _M_value = __z.real(); 01144 __imag__ _M_value = __z.imag(); 01145 return *this; 01146 } 01147 01148 template<typename _Tp> 01149 complex& 01150 operator+=(const complex<_Tp>& __z) 01151 { 01152 __real__ _M_value += __z.real(); 01153 __imag__ _M_value += __z.imag(); 01154 return *this; 01155 } 01156 01157 template<class _Tp> 01158 complex& 01159 operator-=(const complex<_Tp>& __z) 01160 { 01161 __real__ _M_value -= __z.real(); 01162 __imag__ _M_value -= __z.imag(); 01163 return *this; 01164 } 01165 01166 template<class _Tp> 01167 complex& 01168 operator*=(const complex<_Tp>& __z) 01169 { 01170 _ComplexT __t; 01171 __real__ __t = __z.real(); 01172 __imag__ __t = __z.imag(); 01173 _M_value *= __t; 01174 return *this; 01175 } 01176 01177 template<class _Tp> 01178 complex& 01179 operator/=(const complex<_Tp>& __z) 01180 { 01181 _ComplexT __t; 01182 __real__ __t = __z.real(); 01183 __imag__ __t = __z.imag(); 01184 _M_value /= __t; 01185 return *this; 01186 } 01187 01188 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01189 01190 private: 01191 _ComplexT _M_value; 01192 }; 01193 01194 /// 26.2.3 complex specializations 01195 /// complex<double> specialization 01196 template<> 01197 struct complex<double> 01198 { 01199 typedef double value_type; 01200 typedef __complex__ double _ComplexT; 01201 01202 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01203 01204 _GLIBCXX_CONSTEXPR complex(double __r = 0.0, double __i = 0.0) 01205 #if __cplusplus >= 201103L 01206 : _M_value{ __r, __i } { } 01207 #else 01208 { 01209 __real__ _M_value = __r; 01210 __imag__ _M_value = __i; 01211 } 01212 #endif 01213 01214 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01215 : _M_value(__z.__rep()) { } 01216 01217 explicit _GLIBCXX_CONSTEXPR complex(const complex<long double>&); 01218 01219 #if __cplusplus >= 201103L 01220 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01221 // DR 387. std::complex over-encapsulated. 01222 __attribute ((__abi_tag__ ("cxx11"))) 01223 constexpr double 01224 real() const { return __real__ _M_value; } 01225 01226 __attribute ((__abi_tag__ ("cxx11"))) 01227 constexpr double 01228 imag() const { return __imag__ _M_value; } 01229 #else 01230 double& 01231 real() { return __real__ _M_value; } 01232 01233 const double& 01234 real() const { return __real__ _M_value; } 01235 01236 double& 01237 imag() { return __imag__ _M_value; } 01238 01239 const double& 01240 imag() const { return __imag__ _M_value; } 01241 #endif 01242 01243 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01244 // DR 387. std::complex over-encapsulated. 01245 void 01246 real(double __val) { __real__ _M_value = __val; } 01247 01248 void 01249 imag(double __val) { __imag__ _M_value = __val; } 01250 01251 complex& 01252 operator=(double __d) 01253 { 01254 _M_value = __d; 01255 return *this; 01256 } 01257 01258 complex& 01259 operator+=(double __d) 01260 { 01261 _M_value += __d; 01262 return *this; 01263 } 01264 01265 complex& 01266 operator-=(double __d) 01267 { 01268 _M_value -= __d; 01269 return *this; 01270 } 01271 01272 complex& 01273 operator*=(double __d) 01274 { 01275 _M_value *= __d; 01276 return *this; 01277 } 01278 01279 complex& 01280 operator/=(double __d) 01281 { 01282 _M_value /= __d; 01283 return *this; 01284 } 01285 01286 // The compiler will synthesize this, efficiently. 01287 // complex& operator=(const complex&); 01288 01289 template<typename _Tp> 01290 complex& 01291 operator=(const complex<_Tp>& __z) 01292 { 01293 __real__ _M_value = __z.real(); 01294 __imag__ _M_value = __z.imag(); 01295 return *this; 01296 } 01297 01298 template<typename _Tp> 01299 complex& 01300 operator+=(const complex<_Tp>& __z) 01301 { 01302 __real__ _M_value += __z.real(); 01303 __imag__ _M_value += __z.imag(); 01304 return *this; 01305 } 01306 01307 template<typename _Tp> 01308 complex& 01309 operator-=(const complex<_Tp>& __z) 01310 { 01311 __real__ _M_value -= __z.real(); 01312 __imag__ _M_value -= __z.imag(); 01313 return *this; 01314 } 01315 01316 template<typename _Tp> 01317 complex& 01318 operator*=(const complex<_Tp>& __z) 01319 { 01320 _ComplexT __t; 01321 __real__ __t = __z.real(); 01322 __imag__ __t = __z.imag(); 01323 _M_value *= __t; 01324 return *this; 01325 } 01326 01327 template<typename _Tp> 01328 complex& 01329 operator/=(const complex<_Tp>& __z) 01330 { 01331 _ComplexT __t; 01332 __real__ __t = __z.real(); 01333 __imag__ __t = __z.imag(); 01334 _M_value /= __t; 01335 return *this; 01336 } 01337 01338 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01339 01340 private: 01341 _ComplexT _M_value; 01342 }; 01343 01344 /// 26.2.3 complex specializations 01345 /// complex<long double> specialization 01346 template<> 01347 struct complex<long double> 01348 { 01349 typedef long double value_type; 01350 typedef __complex__ long double _ComplexT; 01351 01352 _GLIBCXX_CONSTEXPR complex(_ComplexT __z) : _M_value(__z) { } 01353 01354 _GLIBCXX_CONSTEXPR complex(long double __r = 0.0L, 01355 long double __i = 0.0L) 01356 #if __cplusplus >= 201103L 01357 : _M_value{ __r, __i } { } 01358 #else 01359 { 01360 __real__ _M_value = __r; 01361 __imag__ _M_value = __i; 01362 } 01363 #endif 01364 01365 _GLIBCXX_CONSTEXPR complex(const complex<float>& __z) 01366 : _M_value(__z.__rep()) { } 01367 01368 _GLIBCXX_CONSTEXPR complex(const complex<double>& __z) 01369 : _M_value(__z.__rep()) { } 01370 01371 #if __cplusplus >= 201103L 01372 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01373 // DR 387. std::complex over-encapsulated. 01374 __attribute ((__abi_tag__ ("cxx11"))) 01375 constexpr long double 01376 real() const { return __real__ _M_value; } 01377 01378 __attribute ((__abi_tag__ ("cxx11"))) 01379 constexpr long double 01380 imag() const { return __imag__ _M_value; } 01381 #else 01382 long double& 01383 real() { return __real__ _M_value; } 01384 01385 const long double& 01386 real() const { return __real__ _M_value; } 01387 01388 long double& 01389 imag() { return __imag__ _M_value; } 01390 01391 const long double& 01392 imag() const { return __imag__ _M_value; } 01393 #endif 01394 01395 // _GLIBCXX_RESOLVE_LIB_DEFECTS 01396 // DR 387. std::complex over-encapsulated. 01397 void 01398 real(long double __val) { __real__ _M_value = __val; } 01399 01400 void 01401 imag(long double __val) { __imag__ _M_value = __val; } 01402 01403 complex& 01404 operator=(long double __r) 01405 { 01406 _M_value = __r; 01407 return *this; 01408 } 01409 01410 complex& 01411 operator+=(long double __r) 01412 { 01413 _M_value += __r; 01414 return *this; 01415 } 01416 01417 complex& 01418 operator-=(long double __r) 01419 { 01420 _M_value -= __r; 01421 return *this; 01422 } 01423 01424 complex& 01425 operator*=(long double __r) 01426 { 01427 _M_value *= __r; 01428 return *this; 01429 } 01430 01431 complex& 01432 operator/=(long double __r) 01433 { 01434 _M_value /= __r; 01435 return *this; 01436 } 01437 01438 // The compiler knows how to do this efficiently 01439 // complex& operator=(const complex&); 01440 01441 template<typename _Tp> 01442 complex& 01443 operator=(const complex<_Tp>& __z) 01444 { 01445 __real__ _M_value = __z.real(); 01446 __imag__ _M_value = __z.imag(); 01447 return *this; 01448 } 01449 01450 template<typename _Tp> 01451 complex& 01452 operator+=(const complex<_Tp>& __z) 01453 { 01454 __real__ _M_value += __z.real(); 01455 __imag__ _M_value += __z.imag(); 01456 return *this; 01457 } 01458 01459 template<typename _Tp> 01460 complex& 01461 operator-=(const complex<_Tp>& __z) 01462 { 01463 __real__ _M_value -= __z.real(); 01464 __imag__ _M_value -= __z.imag(); 01465 return *this; 01466 } 01467 01468 template<typename _Tp> 01469 complex& 01470 operator*=(const complex<_Tp>& __z) 01471 { 01472 _ComplexT __t; 01473 __real__ __t = __z.real(); 01474 __imag__ __t = __z.imag(); 01475 _M_value *= __t; 01476 return *this; 01477 } 01478 01479 template<typename _Tp> 01480 complex& 01481 operator/=(const complex<_Tp>& __z) 01482 { 01483 _ComplexT __t; 01484 __real__ __t = __z.real(); 01485 __imag__ __t = __z.imag(); 01486 _M_value /= __t; 01487 return *this; 01488 } 01489 01490 _GLIBCXX_CONSTEXPR _ComplexT __rep() const { return _M_value; } 01491 01492 private: 01493 _ComplexT _M_value; 01494 }; 01495 01496 // These bits have to be at the end of this file, so that the 01497 // specializations have all been defined. 01498 inline _GLIBCXX_CONSTEXPR 01499 complex<float>::complex(const complex<double>& __z) 01500 : _M_value(__z.__rep()) { } 01501 01502 inline _GLIBCXX_CONSTEXPR 01503 complex<float>::complex(const complex<long double>& __z) 01504 : _M_value(__z.__rep()) { } 01505 01506 inline _GLIBCXX_CONSTEXPR 01507 complex<double>::complex(const complex<long double>& __z) 01508 : _M_value(__z.__rep()) { } 01509 01510 // Inhibit implicit instantiations for required instantiations, 01511 // which are defined via explicit instantiations elsewhere. 01512 // NB: This syntax is a GNU extension. 01513 #if _GLIBCXX_EXTERN_TEMPLATE 01514 extern template istream& operator>>(istream&, complex<float>&); 01515 extern template ostream& operator<<(ostream&, const complex<float>&); 01516 extern template istream& operator>>(istream&, complex<double>&); 01517 extern template ostream& operator<<(ostream&, const complex<double>&); 01518 extern template istream& operator>>(istream&, complex<long double>&); 01519 extern template ostream& operator<<(ostream&, const complex<long double>&); 01520 01521 #ifdef _GLIBCXX_USE_WCHAR_T 01522 extern template wistream& operator>>(wistream&, complex<float>&); 01523 extern template wostream& operator<<(wostream&, const complex<float>&); 01524 extern template wistream& operator>>(wistream&, complex<double>&); 01525 extern template wostream& operator<<(wostream&, const complex<double>&); 01526 extern template wistream& operator>>(wistream&, complex<long double>&); 01527 extern template wostream& operator<<(wostream&, const complex<long double>&); 01528 #endif 01529 #endif 01530 01531 // @} group complex_numbers 01532 01533 _GLIBCXX_END_NAMESPACE_VERSION 01534 } // namespace 01535 01536 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 01537 { 01538 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01539 01540 // See ext/type_traits.h for the primary template. 01541 template<typename _Tp, typename _Up> 01542 struct __promote_2<std::complex<_Tp>, _Up> 01543 { 01544 public: 01545 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01546 }; 01547 01548 template<typename _Tp, typename _Up> 01549 struct __promote_2<_Tp, std::complex<_Up> > 01550 { 01551 public: 01552 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01553 }; 01554 01555 template<typename _Tp, typename _Up> 01556 struct __promote_2<std::complex<_Tp>, std::complex<_Up> > 01557 { 01558 public: 01559 typedef std::complex<typename __promote_2<_Tp, _Up>::__type> __type; 01560 }; 01561 01562 _GLIBCXX_END_NAMESPACE_VERSION 01563 } // namespace 01564 01565 #if __cplusplus >= 201103L 01566 01567 namespace std _GLIBCXX_VISIBILITY(default) 01568 { 01569 _GLIBCXX_BEGIN_NAMESPACE_VERSION 01570 01571 // Forward declarations. 01572 template<typename _Tp> std::complex<_Tp> acos(const std::complex<_Tp>&); 01573 template<typename _Tp> std::complex<_Tp> asin(const std::complex<_Tp>&); 01574 template<typename _Tp> std::complex<_Tp> atan(const std::complex<_Tp>&); 01575 01576 template<typename _Tp> std::complex<_Tp> acosh(const std::complex<_Tp>&); 01577 template<typename _Tp> std::complex<_Tp> asinh(const std::complex<_Tp>&); 01578 template<typename _Tp> std::complex<_Tp> atanh(const std::complex<_Tp>&); 01579 // DR 595. 01580 template<typename _Tp> _Tp fabs(const std::complex<_Tp>&); 01581 01582 template<typename _Tp> 01583 inline std::complex<_Tp> 01584 __complex_acos(const std::complex<_Tp>& __z) 01585 { 01586 const std::complex<_Tp> __t = std::asin(__z); 01587 const _Tp __pi_2 = 1.5707963267948966192313216916397514L; 01588 return std::complex<_Tp>(__pi_2 - __t.real(), -__t.imag()); 01589 } 01590 01591 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01592 inline __complex__ float 01593 __complex_acos(__complex__ float __z) 01594 { return __builtin_cacosf(__z); } 01595 01596 inline __complex__ double 01597 __complex_acos(__complex__ double __z) 01598 { return __builtin_cacos(__z); } 01599 01600 inline __complex__ long double 01601 __complex_acos(const __complex__ long double& __z) 01602 { return __builtin_cacosl(__z); } 01603 01604 template<typename _Tp> 01605 inline std::complex<_Tp> 01606 acos(const std::complex<_Tp>& __z) 01607 { return __complex_acos(__z.__rep()); } 01608 #else 01609 /// acos(__z) [8.1.2]. 01610 // Effects: Behaves the same as C99 function cacos, defined 01611 // in subclause 7.3.5.1. 01612 template<typename _Tp> 01613 inline std::complex<_Tp> 01614 acos(const std::complex<_Tp>& __z) 01615 { return __complex_acos(__z); } 01616 #endif 01617 01618 template<typename _Tp> 01619 inline std::complex<_Tp> 01620 __complex_asin(const std::complex<_Tp>& __z) 01621 { 01622 std::complex<_Tp> __t(-__z.imag(), __z.real()); 01623 __t = std::asinh(__t); 01624 return std::complex<_Tp>(__t.imag(), -__t.real()); 01625 } 01626 01627 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01628 inline __complex__ float 01629 __complex_asin(__complex__ float __z) 01630 { return __builtin_casinf(__z); } 01631 01632 inline __complex__ double 01633 __complex_asin(__complex__ double __z) 01634 { return __builtin_casin(__z); } 01635 01636 inline __complex__ long double 01637 __complex_asin(const __complex__ long double& __z) 01638 { return __builtin_casinl(__z); } 01639 01640 template<typename _Tp> 01641 inline std::complex<_Tp> 01642 asin(const std::complex<_Tp>& __z) 01643 { return __complex_asin(__z.__rep()); } 01644 #else 01645 /// asin(__z) [8.1.3]. 01646 // Effects: Behaves the same as C99 function casin, defined 01647 // in subclause 7.3.5.2. 01648 template<typename _Tp> 01649 inline std::complex<_Tp> 01650 asin(const std::complex<_Tp>& __z) 01651 { return __complex_asin(__z); } 01652 #endif 01653 01654 template<typename _Tp> 01655 std::complex<_Tp> 01656 __complex_atan(const std::complex<_Tp>& __z) 01657 { 01658 const _Tp __r2 = __z.real() * __z.real(); 01659 const _Tp __x = _Tp(1.0) - __r2 - __z.imag() * __z.imag(); 01660 01661 _Tp __num = __z.imag() + _Tp(1.0); 01662 _Tp __den = __z.imag() - _Tp(1.0); 01663 01664 __num = __r2 + __num * __num; 01665 __den = __r2 + __den * __den; 01666 01667 return std::complex<_Tp>(_Tp(0.5) * atan2(_Tp(2.0) * __z.real(), __x), 01668 _Tp(0.25) * log(__num / __den)); 01669 } 01670 01671 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01672 inline __complex__ float 01673 __complex_atan(__complex__ float __z) 01674 { return __builtin_catanf(__z); } 01675 01676 inline __complex__ double 01677 __complex_atan(__complex__ double __z) 01678 { return __builtin_catan(__z); } 01679 01680 inline __complex__ long double 01681 __complex_atan(const __complex__ long double& __z) 01682 { return __builtin_catanl(__z); } 01683 01684 template<typename _Tp> 01685 inline std::complex<_Tp> 01686 atan(const std::complex<_Tp>& __z) 01687 { return __complex_atan(__z.__rep()); } 01688 #else 01689 /// atan(__z) [8.1.4]. 01690 // Effects: Behaves the same as C99 function catan, defined 01691 // in subclause 7.3.5.3. 01692 template<typename _Tp> 01693 inline std::complex<_Tp> 01694 atan(const std::complex<_Tp>& __z) 01695 { return __complex_atan(__z); } 01696 #endif 01697 01698 template<typename _Tp> 01699 std::complex<_Tp> 01700 __complex_acosh(const std::complex<_Tp>& __z) 01701 { 01702 // Kahan's formula. 01703 return _Tp(2.0) * std::log(std::sqrt(_Tp(0.5) * (__z + _Tp(1.0))) 01704 + std::sqrt(_Tp(0.5) * (__z - _Tp(1.0)))); 01705 } 01706 01707 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01708 inline __complex__ float 01709 __complex_acosh(__complex__ float __z) 01710 { return __builtin_cacoshf(__z); } 01711 01712 inline __complex__ double 01713 __complex_acosh(__complex__ double __z) 01714 { return __builtin_cacosh(__z); } 01715 01716 inline __complex__ long double 01717 __complex_acosh(const __complex__ long double& __z) 01718 { return __builtin_cacoshl(__z); } 01719 01720 template<typename _Tp> 01721 inline std::complex<_Tp> 01722 acosh(const std::complex<_Tp>& __z) 01723 { return __complex_acosh(__z.__rep()); } 01724 #else 01725 /// acosh(__z) [8.1.5]. 01726 // Effects: Behaves the same as C99 function cacosh, defined 01727 // in subclause 7.3.6.1. 01728 template<typename _Tp> 01729 inline std::complex<_Tp> 01730 acosh(const std::complex<_Tp>& __z) 01731 { return __complex_acosh(__z); } 01732 #endif 01733 01734 template<typename _Tp> 01735 std::complex<_Tp> 01736 __complex_asinh(const std::complex<_Tp>& __z) 01737 { 01738 std::complex<_Tp> __t((__z.real() - __z.imag()) 01739 * (__z.real() + __z.imag()) + _Tp(1.0), 01740 _Tp(2.0) * __z.real() * __z.imag()); 01741 __t = std::sqrt(__t); 01742 01743 return std::log(__t + __z); 01744 } 01745 01746 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01747 inline __complex__ float 01748 __complex_asinh(__complex__ float __z) 01749 { return __builtin_casinhf(__z); } 01750 01751 inline __complex__ double 01752 __complex_asinh(__complex__ double __z) 01753 { return __builtin_casinh(__z); } 01754 01755 inline __complex__ long double 01756 __complex_asinh(const __complex__ long double& __z) 01757 { return __builtin_casinhl(__z); } 01758 01759 template<typename _Tp> 01760 inline std::complex<_Tp> 01761 asinh(const std::complex<_Tp>& __z) 01762 { return __complex_asinh(__z.__rep()); } 01763 #else 01764 /// asinh(__z) [8.1.6]. 01765 // Effects: Behaves the same as C99 function casin, defined 01766 // in subclause 7.3.6.2. 01767 template<typename _Tp> 01768 inline std::complex<_Tp> 01769 asinh(const std::complex<_Tp>& __z) 01770 { return __complex_asinh(__z); } 01771 #endif 01772 01773 template<typename _Tp> 01774 std::complex<_Tp> 01775 __complex_atanh(const std::complex<_Tp>& __z) 01776 { 01777 const _Tp __i2 = __z.imag() * __z.imag(); 01778 const _Tp __x = _Tp(1.0) - __i2 - __z.real() * __z.real(); 01779 01780 _Tp __num = _Tp(1.0) + __z.real(); 01781 _Tp __den = _Tp(1.0) - __z.real(); 01782 01783 __num = __i2 + __num * __num; 01784 __den = __i2 + __den * __den; 01785 01786 return std::complex<_Tp>(_Tp(0.25) * (log(__num) - log(__den)), 01787 _Tp(0.5) * atan2(_Tp(2.0) * __z.imag(), __x)); 01788 } 01789 01790 #if _GLIBCXX_USE_C99_COMPLEX_TR1 01791 inline __complex__ float 01792 __complex_atanh(__complex__ float __z) 01793 { return __builtin_catanhf(__z); } 01794 01795 inline __complex__ double 01796 __complex_atanh(__complex__ double __z) 01797 { return __builtin_catanh(__z); } 01798 01799 inline __complex__ long double 01800 __complex_atanh(const __complex__ long double& __z) 01801 { return __builtin_catanhl(__z); } 01802 01803 template<typename _Tp> 01804 inline std::complex<_Tp> 01805 atanh(const std::complex<_Tp>& __z) 01806 { return __complex_atanh(__z.__rep()); } 01807 #else 01808 /// atanh(__z) [8.1.7]. 01809 // Effects: Behaves the same as C99 function catanh, defined 01810 // in subclause 7.3.6.3. 01811 template<typename _Tp> 01812 inline std::complex<_Tp> 01813 atanh(const std::complex<_Tp>& __z) 01814 { return __complex_atanh(__z); } 01815 #endif 01816 01817 template<typename _Tp> 01818 inline _Tp 01819 /// fabs(__z) [8.1.8]. 01820 // Effects: Behaves the same as C99 function cabs, defined 01821 // in subclause 7.3.8.1. 01822 fabs(const std::complex<_Tp>& __z) 01823 { return std::abs(__z); } 01824 01825 /// Additional overloads [8.1.9]. 01826 template<typename _Tp> 01827 inline typename __gnu_cxx::__promote<_Tp>::__type 01828 arg(_Tp __x) 01829 { 01830 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01831 #if (_GLIBCXX_USE_C99_MATH && !_GLIBCXX_USE_C99_FP_MACROS_DYNAMIC) 01832 return std::signbit(__x) ? __type(3.1415926535897932384626433832795029L) 01833 : __type(); 01834 #else 01835 return std::arg(std::complex<__type>(__x)); 01836 #endif 01837 } 01838 01839 template<typename _Tp> 01840 inline typename __gnu_cxx::__promote<_Tp>::__type 01841 imag(_Tp) 01842 { return _Tp(); } 01843 01844 template<typename _Tp> 01845 inline typename __gnu_cxx::__promote<_Tp>::__type 01846 norm(_Tp __x) 01847 { 01848 typedef typename __gnu_cxx::__promote<_Tp>::__type __type; 01849 return __type(__x) * __type(__x); 01850 } 01851 01852 template<typename _Tp> 01853 inline typename __gnu_cxx::__promote<_Tp>::__type 01854 real(_Tp __x) 01855 { return __x; } 01856 01857 template<typename _Tp, typename _Up> 01858 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01859 pow(const std::complex<_Tp>& __x, const _Up& __y) 01860 { 01861 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01862 return std::pow(std::complex<__type>(__x), __type(__y)); 01863 } 01864 01865 template<typename _Tp, typename _Up> 01866 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01867 pow(const _Tp& __x, const std::complex<_Up>& __y) 01868 { 01869 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01870 return std::pow(__type(__x), std::complex<__type>(__y)); 01871 } 01872 01873 template<typename _Tp, typename _Up> 01874 inline std::complex<typename __gnu_cxx::__promote_2<_Tp, _Up>::__type> 01875 pow(const std::complex<_Tp>& __x, const std::complex<_Up>& __y) 01876 { 01877 typedef typename __gnu_cxx::__promote_2<_Tp, _Up>::__type __type; 01878 return std::pow(std::complex<__type>(__x), 01879 std::complex<__type>(__y)); 01880 } 01881 01882 // Forward declarations. 01883 // DR 781. 01884 template<typename _Tp> std::complex<_Tp> proj(const std::complex<_Tp>&); 01885 01886 template<typename _Tp> 01887 std::complex<_Tp> 01888 __complex_proj(const std::complex<_Tp>& __z) 01889 { 01890 const _Tp __den = (__z.real() * __z.real() 01891 + __z.imag() * __z.imag() + _Tp(1.0)); 01892 01893 return std::complex<_Tp>((_Tp(2.0) * __z.real()) / __den, 01894 (_Tp(2.0) * __z.imag()) / __den); 01895 } 01896 01897 #if _GLIBCXX_USE_C99_COMPLEX 01898 inline __complex__ float 01899 __complex_proj(__complex__ float __z) 01900 { return __builtin_cprojf(__z); } 01901 01902 inline __complex__ double 01903 __complex_proj(__complex__ double __z) 01904 { return __builtin_cproj(__z); } 01905 01906 inline __complex__ long double 01907 __complex_proj(const __complex__ long double& __z) 01908 { return __builtin_cprojl(__z); } 01909 01910 template<typename _Tp> 01911 inline std::complex<_Tp> 01912 proj(const std::complex<_Tp>& __z) 01913 { return __complex_proj(__z.__rep()); } 01914 #else 01915 template<typename _Tp> 01916 inline std::complex<_Tp> 01917 proj(const std::complex<_Tp>& __z) 01918 { return __complex_proj(__z); } 01919 #endif 01920 01921 // DR 1137. 01922 template<typename _Tp> 01923 inline typename __gnu_cxx::__promote<_Tp>::__type 01924 proj(_Tp __x) 01925 { return __x; } 01926 01927 template<typename _Tp> 01928 inline typename __gnu_cxx::__promote<_Tp>::__type 01929 conj(_Tp __x) 01930 { return __x; } 01931 01932 #if __cplusplus > 201103L 01933 01934 inline namespace literals { 01935 inline namespace complex_literals { 01936 01937 #define __cpp_lib_complex_udls 201309 01938 01939 constexpr std::complex<float> 01940 operator""if(long double __num) 01941 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 01942 01943 constexpr std::complex<float> 01944 operator""if(unsigned long long __num) 01945 { return std::complex<float>{0.0F, static_cast<float>(__num)}; } 01946 01947 constexpr std::complex<double> 01948 operator""i(long double __num) 01949 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 01950 01951 constexpr std::complex<double> 01952 operator""i(unsigned long long __num) 01953 { return std::complex<double>{0.0, static_cast<double>(__num)}; } 01954 01955 constexpr std::complex<long double> 01956 operator""il(long double __num) 01957 { return std::complex<long double>{0.0L, __num}; } 01958 01959 constexpr std::complex<long double> 01960 operator""il(unsigned long long __num) 01961 { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; } 01962 01963 } // inline namespace complex_literals 01964 } // inline namespace literals 01965 01966 #endif // C++14 01967 01968 _GLIBCXX_END_NAMESPACE_VERSION 01969 } // namespace 01970 01971 #endif // C++11 01972 01973 #endif /* _GLIBCXX_COMPLEX */