diff options
author | kan <kan@FreeBSD.org> | 2004-07-28 03:12:05 +0000 |
---|---|---|
committer | kan <kan@FreeBSD.org> | 2004-07-28 03:12:05 +0000 |
commit | 96bad46eee8bf907dceb152bbb9d128bed5a4956 (patch) | |
tree | 75ef0e6da73746d6849e25a0996ae34e1aeff51d /contrib/libstdc++/include/std/std_complex.h | |
parent | 5e00ec74d8ce58f99801200d4d3d0412c7cc1b28 (diff) | |
download | FreeBSD-src-96bad46eee8bf907dceb152bbb9d128bed5a4956.zip FreeBSD-src-96bad46eee8bf907dceb152bbb9d128bed5a4956.tar.gz |
Gcc 3.4.2 20040728 C++ support bits.
Diffstat (limited to 'contrib/libstdc++/include/std/std_complex.h')
-rw-r--r-- | contrib/libstdc++/include/std/std_complex.h | 291 |
1 files changed, 229 insertions, 62 deletions
diff --git a/contrib/libstdc++/include/std/std_complex.h b/contrib/libstdc++/include/std/std_complex.h index 252070b..e1027f6 100644 --- a/contrib/libstdc++/include/std/std_complex.h +++ b/contrib/libstdc++/include/std/std_complex.h @@ -1,6 +1,6 @@ // The template and inlines for the -*- C++ -*- complex number classes. -// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 +// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 // Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free @@ -40,8 +40,8 @@ * in your programs, rather than any of the "st[dl]_*.h" implementation files. */ -#ifndef _CPP_COMPLEX -#define _CPP_COMPLEX 1 +#ifndef _GLIBCXX_COMPLEX +#define _GLIBCXX_COMPLEX 1 #pragma GCC system_header @@ -58,78 +58,135 @@ namespace std template<> class complex<double>; template<> class complex<long double>; + /// Return magnitude of @a z. template<typename _Tp> _Tp abs(const complex<_Tp>&); + /// Return phase angle of @a z. template<typename _Tp> _Tp arg(const complex<_Tp>&); + /// Return @a z magnitude squared. template<typename _Tp> _Tp norm(const complex<_Tp>&); + /// Return complex conjugate of @a z. template<typename _Tp> complex<_Tp> conj(const complex<_Tp>&); + /// Return complex with magnitude @a rho and angle @a theta. template<typename _Tp> complex<_Tp> polar(const _Tp&, const _Tp& = 0); // Transcendentals: + /// Return complex cosine of @a z. template<typename _Tp> complex<_Tp> cos(const complex<_Tp>&); + /// Return complex hyperbolic cosine of @a z. template<typename _Tp> complex<_Tp> cosh(const complex<_Tp>&); + /// Return complex base e exponential of @a z. template<typename _Tp> complex<_Tp> exp(const complex<_Tp>&); + /// Return complex natural logarithm of @a z. template<typename _Tp> complex<_Tp> log(const complex<_Tp>&); + /// Return complex base 10 logarithm of @a z. template<typename _Tp> complex<_Tp> log10(const complex<_Tp>&); + /// Return complex cosine of @a z. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, int); + /// Return @a x to the @a y'th power. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const _Tp&); + /// Return @a x to the @a y'th power. template<typename _Tp> complex<_Tp> pow(const complex<_Tp>&, const complex<_Tp>&); + /// Return @a x to the @a y'th power. template<typename _Tp> complex<_Tp> pow(const _Tp&, const complex<_Tp>&); + /// Return complex sine of @a z. template<typename _Tp> complex<_Tp> sin(const complex<_Tp>&); + /// Return complex hyperbolic sine of @a z. template<typename _Tp> complex<_Tp> sinh(const complex<_Tp>&); + /// Return complex square root of @a z. template<typename _Tp> complex<_Tp> sqrt(const complex<_Tp>&); + /// Return complex tangent of @a z. template<typename _Tp> complex<_Tp> tan(const complex<_Tp>&); + /// Return complex hyperbolic tangent of @a z. template<typename _Tp> complex<_Tp> tanh(const complex<_Tp>&); + //@} // 26.2.2 Primary template class complex + /** + * Template to represent complex numbers. + * + * Specializations for float, double, and long double are part of the + * library. Results with any other type are not guaranteed. + * + * @param Tp Type of real and imaginary values. + */ template<typename _Tp> class complex { public: + /// Value typedef. typedef _Tp value_type; + /// Default constructor. First parameter is x, second parameter is y. + /// Unspecified parameters default to 0. complex(const _Tp& = _Tp(), const _Tp & = _Tp()); - // Let's the compiler synthetize the copy constructor + // Lets the compiler synthesize the copy constructor // complex (const complex<_Tp>&); + /// Copy constructor. template<typename _Up> complex(const complex<_Up>&); - - _Tp real() const; - _Tp imag() const; + /// Return real part of complex number. + _Tp& real(); + /// Return real part of complex number. + const _Tp& real() const; + /// Return imaginary part of complex number. + _Tp& imag(); + /// Return imaginary part of complex number. + const _Tp& imag() const; + + /// Assign this complex number to scalar @a t. complex<_Tp>& operator=(const _Tp&); + /// Add @a t to this complex number. complex<_Tp>& operator+=(const _Tp&); + /// Subtract @a t from this complex number. complex<_Tp>& operator-=(const _Tp&); + /// Multiply this complex number by @a t. complex<_Tp>& operator*=(const _Tp&); + /// Divide this complex number by @a t. complex<_Tp>& operator/=(const _Tp&); - // Let's the compiler synthetize the + // Lets the compiler synthesize the // copy and assignment operator // complex<_Tp>& operator= (const complex<_Tp>&); + /// Assign this complex number to complex @a z. template<typename _Up> complex<_Tp>& operator=(const complex<_Up>&); + /// Add @a z to this complex number. template<typename _Up> complex<_Tp>& operator+=(const complex<_Up>&); + /// Subtract @a z from this complex number. template<typename _Up> complex<_Tp>& operator-=(const complex<_Up>&); + /// Multiply this complex number by @a z. template<typename _Up> complex<_Tp>& operator*=(const complex<_Up>&); + /// Divide this complex number by @a z. template<typename _Up> complex<_Tp>& operator/=(const complex<_Up>&); private: - _Tp _M_real, _M_imag; + _Tp _M_real; + _Tp _M_imag; }; template<typename _Tp> - inline _Tp + inline _Tp& + complex<_Tp>::real() { return _M_real; } + + template<typename _Tp> + inline const _Tp& complex<_Tp>::real() const { return _M_real; } template<typename _Tp> - inline _Tp + inline _Tp& + complex<_Tp>::imag() { return _M_imag; } + + template<typename _Tp> + inline const _Tp& complex<_Tp>::imag() const { return _M_imag; } template<typename _Tp> @@ -243,83 +300,147 @@ namespace std complex<_Tp>::operator/=(const complex<_Up>& __z) { const _Tp __r = _M_real * __z.real() + _M_imag * __z.imag(); - const _Tp __n = norm(__z); + const _Tp __n = std::norm(__z); _M_imag = (_M_imag * __z.real() - _M_real * __z.imag()) / __n; _M_real = __r / __n; return *this; } // Operators: + //@{ + /// Return new complex value @a x plus @a y. template<typename _Tp> inline complex<_Tp> operator+(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) += __y; } + { + complex<_Tp> __r = __x; + __r += __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator+(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) += __y; } + { + complex<_Tp> __r = __x; + __r.real() += __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator+(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__y) += __x; } + { + complex<_Tp> __r = __y; + __r.real() += __x; + return __r; + } + //@} + //@{ + /// Return new complex value @a x minus @a y. template<typename _Tp> inline complex<_Tp> operator-(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) -= __y; } + { + complex<_Tp> __r = __x; + __r -= __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator-(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) -= __y; } + { + complex<_Tp> __r = __x; + __r.real() -= __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator-(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) -= __y; } + { + complex<_Tp> __r(__x, -__y.imag()); + __r.real() -= __y.real(); + return __r; + } + //@} + //@{ + /// Return new complex value @a x times @a y. template<typename _Tp> inline complex<_Tp> operator*(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) *= __y; } + { + complex<_Tp> __r = __x; + __r *= __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator*(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) *= __y; } + { + complex<_Tp> __r = __x; + __r *= __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator*(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__y) *= __x; } + { + complex<_Tp> __r = __y; + __r *= __x; + return __r; + } + //@} + //@{ + /// Return new complex value @a x divided by @a y. template<typename _Tp> inline complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) /= __y; } + { + complex<_Tp> __r = __x; + __r /= __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator/(const complex<_Tp>& __x, const _Tp& __y) - { return complex<_Tp> (__x) /= __y; } + { + complex<_Tp> __r = __x; + __r /= __y; + return __r; + } template<typename _Tp> inline complex<_Tp> operator/(const _Tp& __x, const complex<_Tp>& __y) - { return complex<_Tp> (__x) /= __y; } + { + complex<_Tp> __r = __x; + __r /= __y; + return __r; + } + //@} + /// Return @a x. template<typename _Tp> inline complex<_Tp> operator+(const complex<_Tp>& __x) { return __x; } + /// Return complex negation of @a x. template<typename _Tp> inline complex<_Tp> operator-(const complex<_Tp>& __x) { return complex<_Tp>(-__x.real(), -__x.imag()); } + //@{ + /// Return true if @a x is equal to @a y. template<typename _Tp> inline bool operator==(const complex<_Tp>& __x, const complex<_Tp>& __y) @@ -334,7 +455,10 @@ namespace std inline bool operator==(const _Tp& __x, const complex<_Tp>& __y) { return __x == __y.real() && _Tp() == __y.imag(); } + //@} + //@{ + /// Return false if @a x is equal to @a y. template<typename _Tp> inline bool operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y) @@ -349,7 +473,9 @@ namespace std inline bool operator!=(const _Tp& __x, const complex<_Tp>& __y) { return __x != __y.real() || _Tp() != __y.imag(); } + //@} + /// Extraction operator for complex values. template<typename _Tp, typename _CharT, class _Traits> basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) @@ -369,7 +495,7 @@ namespace std __is.setstate(ios_base::failbit); } else if (__ch == ')') - __x = complex<_Tp>(__re_x, _Tp(0)); + __x = __re_x; else __is.setstate(ios_base::failbit); } @@ -377,11 +503,12 @@ namespace std { __is.putback(__ch); __is >> __re_x; - __x = complex<_Tp>(__re_x, _Tp(0)); + __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) @@ -396,12 +523,22 @@ namespace std // Values template<typename _Tp> - inline _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 + inline _Tp& + imag(complex<_Tp>& __z) + { return __z.imag(); } + + template<typename _Tp> + inline const _Tp& imag(const complex<_Tp>& __z) { return __z.imag(); } @@ -411,7 +548,7 @@ namespace std { _Tp __x = __z.real(); _Tp __y = __z.imag(); - const _Tp __s = max(abs(__x), abs(__y)); + const _Tp __s = std::max(abs(__x), abs(__y)); if (__s == _Tp()) // well ... return __s; __x /= __s; @@ -447,7 +584,7 @@ namespace std template<typename _Tp> static inline _Tp _S_do_it(const complex<_Tp>& __z) { - _Tp __res = abs(__z); + _Tp __res = std::abs(__z); return __res * __res; } }; @@ -456,7 +593,7 @@ namespace std inline _Tp norm(const complex<_Tp>& __z) { - return _Norm_helper<__is_floating<_Tp>::_M_type && !_GLIBCPP_FAST_MATH>::_S_do_it(__z); + return _Norm_helper<__is_floating<_Tp>::_M_type && !_GLIBCXX_FAST_MATH>::_S_do_it(__z); } template<typename _Tp> @@ -491,17 +628,17 @@ namespace std template<typename _Tp> inline complex<_Tp> exp(const complex<_Tp>& __z) - { return polar(exp(__z.real()), __z.imag()); } + { return std::polar(exp(__z.real()), __z.imag()); } template<typename _Tp> inline complex<_Tp> log(const complex<_Tp>& __z) - { return complex<_Tp>(log(abs(__z)), arg(__z)); } + { return complex<_Tp>(log(std::abs(__z)), std::arg(__z)); } template<typename _Tp> inline complex<_Tp> log10(const complex<_Tp>& __z) - { return log(__z) / log(_Tp(10.0)); } + { return std::log(__z) / log(_Tp(10.0)); } template<typename _Tp> inline complex<_Tp> @@ -535,7 +672,7 @@ namespace std } else { - _Tp __t = sqrt(2 * (abs(__z) + abs(__x))); + _Tp __t = sqrt(2 * (std::abs(__z) + abs(__x))); _Tp __u = __t / 2; return __x > _Tp() ? complex<_Tp>(__u, __y / __t) @@ -547,48 +684,48 @@ namespace std inline complex<_Tp> tan(const complex<_Tp>& __z) { - return sin(__z) / cos(__z); + return std::sin(__z) / std::cos(__z); } template<typename _Tp> inline complex<_Tp> tanh(const complex<_Tp>& __z) { - return sinh(__z) / cosh(__z); + return std::sinh(__z) / std::cosh(__z); } template<typename _Tp> inline complex<_Tp> pow(const complex<_Tp>& __z, int __n) { - return __pow_helper(__z, __n); + return std::__pow_helper(__z, __n); } template<typename _Tp> complex<_Tp> pow(const complex<_Tp>& __x, const _Tp& __y) { - if (__x.imag() == _Tp()) + if (__x.imag() == _Tp() && __x.real() > _Tp()) return pow(__x.real(), __y); - complex<_Tp> __t = log(__x); - return polar(exp(__y * __t.real()), __y * __t.imag()); + complex<_Tp> __t = std::log(__x); + return std::polar(exp(__y * __t.real()), __y * __t.imag()); } template<typename _Tp> inline complex<_Tp> pow(const complex<_Tp>& __x, const complex<_Tp>& __y) { - return __x == _Tp() ? _Tp() : exp(__y * log(__x)); + return __x == _Tp() ? _Tp() : std::exp(__y * std::log(__x)); } template<typename _Tp> inline complex<_Tp> pow(const _Tp& __x, const complex<_Tp>& __y) { - return __x == _Tp() - ? _Tp() - : polar(pow(__x, __y.real()), __y.imag() * log(__x)); + return __x > _Tp() ? std::polar(pow(__x, __y.real()), + __y.imag() * log(__x)) + : std::pow(complex<_Tp>(__x, _Tp()), __y); } // 26.2.3 complex specializations @@ -599,14 +736,16 @@ namespace std typedef float value_type; complex(float = 0.0f, float = 0.0f); -#ifdef _GLIBCPP_BUGGY_COMPLEX +#ifdef _GLIBCXX_BUGGY_COMPLEX complex(const complex& __z) : _M_value(__z._M_value) { } #endif explicit complex(const complex<double>&); explicit complex(const complex<long double>&); - float real() const; - float imag() const; + float& real(); + const float& real() const; + float& imag(); + const float& imag() const; complex<float>& operator=(float); complex<float>& operator+=(float); @@ -638,11 +777,19 @@ namespace std friend class complex<long double>; }; - inline float + inline float& + complex<float>::real() + { return __real__ _M_value; } + + inline const float& complex<float>::real() const { return __real__ _M_value; } - inline float + inline float& + complex<float>::imag() + { return __imag__ _M_value; } + + inline const float& complex<float>::imag() const { return __imag__ _M_value; } @@ -746,14 +893,16 @@ namespace std typedef double value_type; complex(double =0.0, double =0.0); -#ifdef _GLIBCPP_BUGGY_COMPLEX +#ifdef _GLIBCXX_BUGGY_COMPLEX complex(const complex& __z) : _M_value(__z._M_value) { } #endif complex(const complex<float>&); explicit complex(const complex<long double>&); - - double real() const; - double imag() const; + + double& real(); + const double& real() const; + double& imag(); + const double& imag() const; complex<double>& operator=(double); complex<double>& operator+=(double); @@ -784,11 +933,19 @@ namespace std friend class complex<long double>; }; - inline double + inline double& + complex<double>::real() + { return __real__ _M_value; } + + inline const double& complex<double>::real() const { return __real__ _M_value; } - inline double + inline double& + complex<double>::imag() + { return __imag__ _M_value; } + + inline const double& complex<double>::imag() const { return __imag__ _M_value; } @@ -892,14 +1049,16 @@ namespace std typedef long double value_type; complex(long double = 0.0L, long double = 0.0L); -#ifdef _GLIBCPP_BUGGY_COMPLEX +#ifdef _GLIBCXX_BUGGY_COMPLEX complex(const complex& __z) : _M_value(__z._M_value) { } #endif complex(const complex<float>&); complex(const complex<double>&); - long double real() const; - long double imag() const; + long double& real(); + const long double& real() const; + long double& imag(); + const long double& imag() const; complex<long double>& operator= (long double); complex<long double>& operator+= (long double); @@ -937,11 +1096,19 @@ namespace std __imag__ _M_value = __i; } - inline long double + inline long double& + complex<long double>::real() + { return __real__ _M_value; } + + inline const long double& complex<long double>::real() const { return __real__ _M_value; } - inline long double + inline long double& + complex<long double>::imag() + { return __imag__ _M_value; } + + inline const long double& complex<long double>::imag() const { return __imag__ _M_value; } @@ -1062,4 +1229,4 @@ namespace std : _M_value(_ComplexT(__z._M_value)) { } } // namespace std -#endif /* _CPP_COMPLEX */ +#endif /* _GLIBCXX_COMPLEX */ |