📄 std_complex.h
字号:
else __is.setstate(ios_base::failbit); } else if (__ch == ')') __x = __re_x; else __is.setstate(ios_base::failbit); } else { __is.putback(__ch); __is >> __re_x; __x = __re_x; } return __is; } /// Insertion operator for complex values. template<typename _Tp, typename _CharT, class _Traits> basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x) { basic_ostringstream<_CharT, _Traits> __s; __s.flags(__os.flags()); __s.imbue(__os.getloc()); __s.precision(__os.precision()); __s << '(' << __x.real() << ',' << __x.imag() << ')'; return __os << __s.str(); } // Values template<typename _Tp> inline _Tp& real(complex<_Tp>& __z) { return __z.real(); } template<typename _Tp> inline const _Tp& real(const complex<_Tp>& __z) { return __z.real(); } template<typename _Tp> inline _Tp& imag(complex<_Tp>& __z) { return __z.imag(); } template<typename _Tp> inline const _Tp& imag(const complex<_Tp>& __z) { return __z.imag(); } // 26.2.7/3 abs(__z): Returns the magnitude of __z. template<typename _Tp> inline _Tp __complex_abs(const complex<_Tp>& __z) { _Tp __x = __z.real(); _Tp __y = __z.imag(); const _Tp __s = std::max(abs(__x), abs(__y)); if (__s == _Tp()) // well ... return __s; __x /= __s; __y /= __s; return __s * sqrt(__x * __x + __y * __y); }#if _GLIBCXX_USE_C99_COMPLEX inline float __complex_abs(__complex__ float __z) { return __builtin_cabsf(__z); } inline double __complex_abs(__complex__ double __z) { return __builtin_cabs(__z); } inline long double __complex_abs(const __complex__ long double& __z) { return __builtin_cabsl(__z); } template<typename _Tp> inline _Tp abs(const complex<_Tp>& __z) { return __complex_abs(__z.__rep()); }#else template<typename _Tp> inline _Tp abs(const complex<_Tp>& __z) { return __complex_abs(__z); }#endif // 26.2.7/4: arg(__z): Returns the phase angle of __z. template<typename _Tp> inline _Tp __complex_arg(const complex<_Tp>& __z) { return atan2(__z.imag(), __z.real()); }#if _GLIBCXX_USE_C99_COMPLEX inline float __complex_arg(__complex__ float __z) { return __builtin_cargf(__z); } inline double __complex_arg(__complex__ double __z) { return __builtin_carg(__z); } inline long double __complex_arg(const __complex__ long double& __z) { return __builtin_cargl(__z); } template<typename _Tp> inline _Tp arg(const complex<_Tp>& __z) { return __complex_arg(__z.__rep()); }#else template<typename _Tp> inline _Tp arg(const complex<_Tp>& __z) { return __complex_arg(__z); }#endif // 26.2.7/5: norm(__z) returns the squared magintude of __z. // As defined, norm() is -not- a norm is the common mathematical // sens used in numerics. The helper class _Norm_helper<> tries to // distinguish between builtin floating point and the rest, so as // to deliver an answer as close as possible to the real value. template<bool> struct _Norm_helper { template<typename _Tp> static inline _Tp _S_do_it(const complex<_Tp>& __z) { const _Tp __x = __z.real(); const _Tp __y = __z.imag(); return __x * __x + __y * __y; } }; template<> struct _Norm_helper<true> { template<typename _Tp> static inline _Tp _S_do_it(const complex<_Tp>& __z) { _Tp __res = std::abs(__z); return __res * __res; } }; template<typename _Tp> inline _Tp norm(const complex<_Tp>& __z) { return _Norm_helper<__is_floating<_Tp>::__value && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); } template<typename _Tp> inline complex<_Tp> polar(const _Tp& __rho, const _Tp& __theta) { return complex<_Tp>(__rho * cos(__theta), __rho * sin(__theta)); } template<typename _Tp> inline complex<_Tp> conj(const complex<_Tp>& __z) { return complex<_Tp>(__z.real(), -__z.imag()); } // Transcendentals // 26.2.8/1 cos(__z): Returns the cosine of __z. template<typename _Tp> inline complex<_Tp> __complex_cos(const complex<_Tp>& __z) { const _Tp __x = __z.real(); const _Tp __y = __z.imag(); return complex<_Tp>(cos(__x) * cosh(__y), -sin(__x) * sinh(__y)); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_cos(__complex__ float __z) { return __builtin_ccosf(__z); } inline __complex__ double __complex_cos(__complex__ double __z) { return __builtin_ccos(__z); } inline __complex__ long double __complex_cos(const __complex__ long double& __z) { return __builtin_ccosl(__z); } template<typename _Tp> inline complex<_Tp> cos(const complex<_Tp>& __z) { return __complex_cos(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> cos(const complex<_Tp>& __z) { return __complex_cos(__z); }#endif // 26.2.8/2 cosh(__z): Returns the hyperbolic cosine of __z. template<typename _Tp> inline complex<_Tp> __complex_cosh(const complex<_Tp>& __z) { const _Tp __x = __z.real(); const _Tp __y = __z.imag(); return complex<_Tp>(cosh(__x) * cos(__y), sinh(__x) * sin(__y)); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_cosh(__complex__ float __z) { return __builtin_ccoshf(__z); } inline __complex__ double __complex_cosh(__complex__ double __z) { return __builtin_ccosh(__z); } inline __complex__ long double __complex_cosh(const __complex__ long double& __z) { return __builtin_ccoshl(__z); } template<typename _Tp> inline complex<_Tp> cosh(const complex<_Tp>& __z) { return __complex_cosh(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> cosh(const complex<_Tp>& __z) { return __complex_cosh(__z); }#endif // 26.2.8/3 exp(__z): Returns the complex base e exponential of x template<typename _Tp> inline complex<_Tp> __complex_exp(const complex<_Tp>& __z) { return std::polar(exp(__z.real()), __z.imag()); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_exp(__complex__ float __z) { return __builtin_cexpf(__z); } inline __complex__ double __complex_exp(__complex__ double __z) { return __builtin_cexp(__z); } inline __complex__ long double __complex_exp(const __complex__ long double& __z) { return __builtin_cexpl(__z); } template<typename _Tp> inline complex<_Tp> exp(const complex<_Tp>& __z) { return __complex_exp(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> exp(const complex<_Tp>& __z) { return __complex_exp(__z); }#endif // 26.2.8/5 log(__z): Reurns the natural complex logaritm of __z. // The branch cut is along the negative axis. template<typename _Tp> inline complex<_Tp> __complex_log(const complex<_Tp>& __z) { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_log(__complex__ float __z) { return __builtin_clogf(__z); } inline __complex__ double __complex_log(__complex__ double __z) { return __builtin_clog(__z); } inline __complex__ long double __complex_log(const __complex__ long double& __z) { return __builtin_clogl(__z); } template<typename _Tp> inline complex<_Tp> log(const complex<_Tp>& __z) { return __complex_log(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> log(const complex<_Tp>& __z) { return __complex_log(__z); }#endif template<typename _Tp> inline complex<_Tp> log10(const complex<_Tp>& __z) { return std::log(__z) / log(_Tp(10.0)); } // 26.2.8/10 sin(__z): Returns the sine of __z. template<typename _Tp> inline complex<_Tp> __complex_sin(const complex<_Tp>& __z) { const _Tp __x = __z.real(); const _Tp __y = __z.imag(); return complex<_Tp>(sin(__x) * cosh(__y), cos(__x) * sinh(__y)); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_sin(__complex__ float __z) { return __builtin_csinf(__z); } inline __complex__ double __complex_sin(__complex__ double __z) { return __builtin_csin(__z); } inline __complex__ long double __complex_sin(const __complex__ long double& __z) { return __builtin_csinl(__z); } template<typename _Tp> inline complex<_Tp> sin(const complex<_Tp>& __z) { return __complex_sin(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> sin(const complex<_Tp>& __z) { return __complex_sin(__z); }#endif // 26.2.8/11 sinh(__z): Returns the hyperbolic sine of __z. template<typename _Tp> inline complex<_Tp> __complex_sinh(const complex<_Tp>& __z) { const _Tp __x = __z.real(); const _Tp __y = __z.imag(); return complex<_Tp>(sinh(__x) * cos(__y), cosh(__x) * sin(__y)); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_sinh(__complex__ float __z) { return __builtin_csinhf(__z); } inline __complex__ double __complex_sinh(__complex__ double __z) { return __builtin_csinh(__z); } inline __complex__ long double __complex_sinh(const __complex__ long double& __z) { return __builtin_csinhl(__z); } template<typename _Tp> inline complex<_Tp> sinh(const complex<_Tp>& __z) { return __complex_sinh(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> sinh(const complex<_Tp>& __z) { return __complex_sinh(__z); }#endif // 26.2.8/13 sqrt(__z): Returns the complex square root of __z. // The branch cut is on the negative axis. template<typename _Tp> complex<_Tp> __complex_sqrt(const complex<_Tp>& __z) { _Tp __x = __z.real(); _Tp __y = __z.imag(); if (__x == _Tp()) { _Tp __t = sqrt(abs(__y) / 2); return complex<_Tp>(__t, __y < _Tp() ? -__t : __t); } else { _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); _Tp __u = __t / 2; return __x > _Tp() ? complex<_Tp>(__u, __y / __t) : complex<_Tp>(abs(__y) / __t, __y < _Tp() ? -__u : __u); } }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_sqrt(__complex__ float __z) { return __builtin_csqrtf(__z); } inline __complex__ double __complex_sqrt(__complex__ double __z) { return __builtin_csqrt(__z); } inline __complex__ long double __complex_sqrt(const __complex__ long double& __z) { return __builtin_csqrtl(__z); } template<typename _Tp> inline complex<_Tp> sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> sqrt(const complex<_Tp>& __z) { return __complex_sqrt(__z); }#endif // 26.2.8/14 tan(__z): Return the complex tangent of __z. template<typename _Tp> inline complex<_Tp> __complex_tan(const complex<_Tp>& __z) { return std::sin(__z) / std::cos(__z); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_tan(__complex__ float __z) { return __builtin_ctanf(__z); } inline __complex__ double __complex_tan(__complex__ double __z) { return __builtin_ctan(__z); } inline __complex__ long double __complex_tan(const __complex__ long double& __z) { return __builtin_ctanl(__z); } template<typename _Tp> inline complex<_Tp> tan(const complex<_Tp>& __z) { return __complex_tan(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> tan(const complex<_Tp>& __z) { return __complex_tan(__z); }#endif // 26.2.8/15 tanh(__z): Returns the hyperbolic tangent of __z. template<typename _Tp> inline complex<_Tp> __complex_tanh(const complex<_Tp>& __z) { return std::sinh(__z) / std::cosh(__z); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_tanh(__complex__ float __z) { return __builtin_ctanhf(__z); } inline __complex__ double __complex_tanh(__complex__ double __z) { return __builtin_ctanh(__z); } inline __complex__ long double __complex_tanh(const __complex__ long double& __z) { return __builtin_ctanhl(__z); } template<typename _Tp> inline complex<_Tp> tanh(const complex<_Tp>& __z) { return __complex_tanh(__z.__rep()); }#else template<typename _Tp> inline complex<_Tp> tanh(const complex<_Tp>& __z) { return __complex_tanh(__z); }#endif // 26.2.8/9 pow(__x, __y): Returns the complex power base of __x // raised to the __y-th power. The branch // cut is on the negative axis. template<typename _Tp> inline complex<_Tp> pow(const complex<_Tp>& __z, int __n) { return std::__pow_helper(__z, __n); } template<typename _Tp> complex<_Tp> pow(const complex<_Tp>& __x, const _Tp& __y) {#ifndef _GLIBCXX_USE_C99_COMPLEX if (__x == _Tp()) return _Tp();#endif if (__x.imag() == _Tp() && __x.real() > _Tp()) return pow(__x.real(), __y); complex<_Tp> __t = std::log(__x); return std::polar(exp(__y * __t.real()), __y * __t.imag()); } template<typename _Tp> inline complex<_Tp> __complex_pow(const complex<_Tp>& __x, const complex<_Tp>& __y) { return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); }#if _GLIBCXX_USE_C99_COMPLEX inline __complex__ float __complex_pow(__complex__ float __x, __complex__ float __y) { return __builtin_cpowf(__x, __y); } inline __complex__ double __complex_pow(__complex__ double __x, __complex__ double __y) { return __builtin_cpow(__x, __y); } inline __complex__ long double __complex_pow(const __complex__ long double& __x, const __complex__ long double& __y) { return __builtin_cpowl(__x, __y); } template<typename _Tp> inline complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) { return __complex_pow(__x.__rep(), __y.__rep()); }#else template<typename _Tp> inline complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) { return __complex_pow(__x, __y); }#endif template<typename _Tp> inline complex<_Tp> pow(const _Tp& __x, const complex<_Tp>& __y) { return __x > _Tp() ? std::polar(pow(__x, __y.real()),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -