/* * * This file and its contents are the property of The MathWorks, Inc. * * This file contains confidential proprietary information. * The reproduction, distribution, utilization or the communication * of this file or any part thereof is strictly prohibited. * Offenders will be held liable for the payment of damages. * * Copyright 1999-2012 The MathWorks, Inc. * */ #ifndef PST_STL_COMPLEX #define PST_STL_COMPLEX #include <__polyspace__math.h> #include #ifdef PST_VISUAL #pragma pack(push, 8) #endif namespace std { // 26.2.2 Primary template class complex template<__ps_class T> class complex { public: typedef T value_type; complex(const T& re = T(), const T & im = T()) { _pst_real = re ; _pst_imag = im ; } complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } template friend class complex ; template complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } T real() const { return _pst_real ; } T imag() const { return _pst_imag ; } #ifdef PST_VISUAL // visual 6 & 7.1 T real(const T& v) { return (_pst_real = v) ; } T imag(const T& v) { return (_pst_imag = v) ; } #endif complex& operator=(const T& t) { _pst_real = t; _pst_imag = T() ; return *this ; } complex& operator+=(const T& t) { _pst_real += t ; return *this ; } complex& operator-=(const T& t) { _pst_real -= t ; return *this ; } complex& operator*=(const T& t) { _pst_real *= t ; _pst_imag *= t ; return *this ; } complex& operator/=(const T& t) { _pst_real /= t ; _pst_imag /= t ; return *this ; } complex& operator= (const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; return *this ; } template complex& operator= (const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; return *this ; } template complex& operator+= (const complex& src) { _pst_real += src._pst_real ; _pst_imag += src._pst_imag ; return *this ; } template complex& operator-= (const complex& src) { _pst_real -= src._pst_real ; _pst_imag -= src._pst_imag ; return *this ; } template complex& operator*= (const complex& src) { const T res = _pst_real * src._pst_real - _pst_imag * src._pst_imag ; _pst_imag = _pst_real * src._pst_imag + _pst_imag * src._pst_real ; _pst_real = res ; return *this; } template complex& operator/= (const complex& src) { const T tmp = _pst_real * src._pst_real + _pst_imag * src._pst_imag ; const T n = src._pst_real * src._pst_real + src._pst_imag * src._pst_imag ; // norm2(src) _pst_imag = (_pst_imag * src._pst_real - _pst_real * src._pst_imag) / n; _pst_real = tmp / n; return *this; } // made public so various functions can access data without selector T _pst_real, _pst_imag ; }; template<> class complex; template<> class complex; template<> class complex; // 26.2.3 Specialization template<> class complex { public: typedef float value_type; complex(float re = 0.0f, float im = 0.0f) { _pst_real = re ; _pst_imag = im ; } friend class complex ; friend class complex ; __ps_explicit complex(const complex& src) ; // defined after specializations. __ps_explicit complex(const complex& src) ; // defined after specializations. float real() const { return _pst_real ; } float imag() const { return _pst_imag ; } #ifdef PST_VISUAL // visual 6 & 7.1 float real(float v) { return (_pst_real = v) ; } float imag(float v) { return (_pst_imag = v) ; } #endif complex& operator=(float t) { _pst_real = t; _pst_imag = 0.0f ; return *this ; } complex& operator+=(float t) { _pst_real += t ; return *this ; } complex& operator-=(float t) { _pst_real -= t ; return *this ; } complex& operator*=(float t) { _pst_real *= t ; _pst_imag *= t ; return *this ; } complex& operator/=(float t) { _pst_real /= t ; _pst_imag /= t ; return *this ; } complex& operator= (const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; return *this ; } template complex& operator= (const complex& src) { _pst_real = (float)(src._pst_real) ; _pst_imag = (float)(src._pst_imag) ; return *this ; } template complex& operator+= (const complex& src) { _pst_real += (float)(src._pst_real) ; _pst_imag += (float)(src._pst_imag) ; return *this ; } template complex& operator-= (const complex& src) { _pst_real -= (float)(src._pst_real) ; _pst_imag -= (float)(src._pst_imag) ; return *this ; } template complex& operator*= (const complex& src) { const float res = _pst_real * src._pst_real - _pst_imag * src._pst_imag ; _pst_imag = _pst_real * src._pst_imag + _pst_imag * src._pst_real ; _pst_real = res ; return *this; } template complex& operator/= (const complex& src) { const float tmp = _pst_real * src._pst_real + _pst_imag * src._pst_imag ; const float n = (float)(src._pst_real * src._pst_real + src._pst_imag * src._pst_imag) ; // norm2(src) _pst_imag = (_pst_imag * src._pst_real - _pst_real * src._pst_imag) / n; _pst_real = tmp / n; return *this; } // made public so various functions can access data without selector float _pst_real, _pst_imag ; }; template<> class complex { public: typedef double value_type; complex(double re = 0.0, double im = 0.0) { _pst_real = re ; _pst_imag = im ; } friend class complex ; friend class complex ; complex(const complex& src) ; __ps_explicit complex(const complex& src) ; double real() const { return _pst_real ; } double imag() const { return _pst_imag ; } #ifdef PST_VISUAL // visual 6 & 7.1 double real(double v) { return (_pst_real = v) ; } double imag(double v) { return (_pst_imag = v) ; } #endif complex& operator=(double t) { _pst_real = t; _pst_imag = 0.0 ; return *this ; } complex& operator+=(double t) { _pst_real += t ; return *this ; } complex& operator-=(double t) { _pst_real -= t ; return *this ; } complex& operator*=(double t) { _pst_real *= t ; _pst_imag *= t ; return *this ; } complex& operator/=(double t) { _pst_real /= t ; _pst_imag /= t ; return *this ; } complex& operator= (const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; return *this ; } template complex& operator= (const complex& src) { _pst_real = (double)(src._pst_real) ; _pst_imag = (double)(src._pst_imag) ; return *this ; } template complex& operator+= (const complex& src) { _pst_real += (double)(src._pst_real) ; _pst_imag += (double)(src._pst_imag) ; return *this ; } template complex& operator-= (const complex& src) { _pst_real -= (double)(src._pst_real) ; _pst_imag -= (double)(src._pst_imag) ; return *this ; } template complex& operator*= (const complex& src) { const double res = _pst_real * src._pst_real - _pst_imag * src._pst_imag ; _pst_imag = _pst_real * src._pst_imag + _pst_imag * src._pst_real ; _pst_real = res ; return *this; } template complex& operator/= (const complex& src) { const double tmp = _pst_real * src._pst_real + _pst_imag * src._pst_imag ; const double n = (double)(src._pst_real * src._pst_real + src._pst_imag * src._pst_imag) ; // norm2(src) _pst_imag = (_pst_imag * src._pst_real - _pst_real * src._pst_imag) / n; _pst_real = tmp / n; return *this; } // made public so various functions can access data without selector double _pst_real, _pst_imag ; }; template<> class complex { public: typedef long double value_type; complex(long double re = 0.0L, long double im = 0.0L) { _pst_real = re ; _pst_imag = im ; } friend class complex ; friend class complex ; complex(const complex& src) ; complex(const complex& src) ; long double real() const { return _pst_real ; } long double imag() const { return _pst_imag ; } #ifdef PST_VISUAL // visual 6 long double real(long double v) { return (_pst_real = v) ; } long double imag(long double v) { return (_pst_imag = v) ; } #endif complex& operator=(long double t) { _pst_real = t; _pst_imag = 0.0L ; return *this ; } complex& operator+=(long double t) { _pst_real += t ; return *this ; } complex& operator-=(long double t) { _pst_real -= t ; return *this ; } complex& operator*=(long double t) { _pst_real *= t ; _pst_imag *= t ; return *this ; } complex& operator/=(long double t) { _pst_real /= t ; _pst_imag /= t ; return *this ; } complex& operator= (const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; return *this ; } template complex& operator= (const complex& src) { _pst_real = (long double)(src._pst_real) ; _pst_imag = (long double)(src._pst_imag) ; return *this ; } template complex& operator+= (const complex& src) { _pst_real += (long double)(src._pst_real) ; _pst_imag += (long double)(src._pst_imag) ; return *this ; } template complex& operator-= (const complex& src) { _pst_real -= (long double)(src._pst_real) ; _pst_imag -= (long double)(src._pst_imag) ; return *this ; } template complex& operator*= (const complex& src) { const long double res = _pst_real * src._pst_real - _pst_imag * src._pst_imag ; _pst_imag = _pst_real * src._pst_imag + _pst_imag * src._pst_real ; _pst_real = res ; return *this; } template complex& operator/= (const complex& src) { const long double tmp = _pst_real * src._pst_real + _pst_imag * src._pst_imag ; const long double n = (long double)(src._pst_real * src._pst_real + src._pst_imag * src._pst_imag) ; // norm2(src) _pst_imag = (_pst_imag * src._pst_real - _pst_real * src._pst_imag) / n; _pst_real = tmp / n; return *this; } // made public so various functions can access data without selector long double _pst_real, _pst_imag ; }; inline complex::complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } inline complex::complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } inline complex::complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } inline complex::complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } inline complex::complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } inline complex::complex(const complex& src) { _pst_real = src._pst_real ; _pst_imag = src._pst_imag ; } // 26.2.6 operators : template complex operator+(const complex& x, const complex& y) { return complex(x._pst_real + y._pst_real, x._pst_imag + y._pst_imag) ; } template complex operator+(const complex& x, const T& t) { return complex(x._pst_real + t, x._pst_imag) ; } template complex operator+(const T& t, const complex& y) { return complex(t + y._pst_real, y._pst_imag) ; } template complex operator-(const complex& x, const complex& y) { return complex(x._pst_real - y._pst_real, x._pst_imag - y._pst_imag) ; } template complex operator-(const complex& x, const T& t) { return complex(x._pst_real - t, x._pst_imag) ; } template complex operator-(const T& t, const complex& y) { return complex(t - y._pst_real, -y._pst_imag) ; } template complex operator*(const complex& x, const complex& y) { return complex(x._pst_real * y._pst_real - x._pst_imag * y._pst_imag, x._pst_real * y._pst_imag + x._pst_imag * y._pst_real) ; } template complex operator*(const complex& x, const T& t) { return complex(x._pst_real * t, x._pst_imag * t) ; } template complex operator*(const T& t, const complex& y) { return complex(t * y._pst_real, t * y._pst_imag) ; } template complex operator/(const complex& x, const complex& y) { const T n = y._pst_real * y._pst_real + y._pst_imag * y._pst_imag ; // norm2(y) return complex( (x._pst_real * y._pst_real + x._pst_imag * y._pst_imag)/n, (x._pst_imag * y._pst_real - x._pst_real * y._pst_imag)/n) ; } template complex operator/(const complex& x, const T& t) { return complex(x._pst_real / t, x._pst_imag / t) ; } template complex operator/(const T& t, const complex& y) { const T n = y._pst_real * y._pst_real + y._pst_imag * y._pst_imag ; // norm2(y) return complex(t * y._pst_real / n , - t * y._pst_imag / n) ; } template complex operator+(const complex& x) { return x ; } template complex operator-(const complex& x) { return complex(-x._pst_real, -x._pst_imag) ; } template __ps_bool operator==(const complex& x, const complex& y) { return (x._pst_real==y._pst_real)&&(x._pst_imag==y._pst_imag) ; } template __ps_bool operator==(const complex& x, const T& t) { return (x._pst_real==t)&&(x._pst_imag==T()) ; } template __ps_bool operator==(const T& t, const complex& y) { return (t==y._pst_real)&&(y._pst_imag==T()) ; } template __ps_bool operator!=(const complex& x, const complex& y) { return (x._pst_real!=y._pst_real)||(x._pst_imag!=y._pst_imag) ; } template __ps_bool operator!=(const complex& x, const T& t) { return (x._pst_real!=t)||(x._pst_imag!=T()) ; } template __ps_bool operator!=(const T& t, const complex& y) { return (t!=y._pst_real)||(y._pst_imag!=T()) ; } template basic_istream& operator>>(basic_istream& in, complex& x) { volatile T t = T() ; x._pst_imag = t ; // randon init x._pst_real = t ; return in ; } template basic_ostream& operator<<(basic_ostream& out, const complex& x) { T t = x._pst_real ; // simulate reading t = x._pst_imag ; return out ; } // 26.2.7 values template T real(const complex& x) { return x._pst_real ; } template T imag(const complex& x) { return x._pst_imag ; } template T abs(const complex& x) { return sqrt(x._pst_real * x._pst_real + x._pst_imag * x._pst_imag) ; } template T arg(const complex& x) { return atan2(x._pst_imag, x._pst_real) ; } template T norm(const complex& x) { return x._pst_real * x._pst_real + x._pst_imag * x._pst_imag ; } template complex conj(const complex& x) { return complex(x._pst_real, -x._pst_imag) ; } template complex polar(const T& rho, const T& theta = 0) // = 0 added in TC1 { return complex(rho * cos(theta), rho*sin(theta)) ; } // 26.2.8 : complex transcendental template complex cos(const complex& x) { return complex(cos(x._pst_real)*cosh(x._pst_imag), -sin(x._pst_real) * sinh(x._pst_imag)) ; } template complex cosh(const complex& x) { return complex(cosh(x._pst_real)*cos(x._pst_imag), sinh(x._pst_real) * sin(x._pst_imag)) ; } template complex exp(const complex& x) { const T rho = exp(x._pst_real) ; return complex(rho * cos(x._pst_imag), rho*sin(x._pst_imag)) ; } template complex log(const complex& x) { const T abs = sqrt(x._pst_real * x._pst_real + x._pst_imag * x._pst_imag) ; const T arg = atan2(x._pst_imag, x._pst_real) ; return complex(log(abs), arg) ; } template complex log10(const complex& x) { const T abs = sqrt(x._pst_real * x._pst_real + x._pst_imag * x._pst_imag) ; const T arg = atan2(x._pst_imag, x._pst_real) ; const T log10 = log(T(10.0)) ; return complex(log(abs)/log10, arg/log10) ; } template complex sin(const complex& x) { return complex(sin(x._pst_real)*cosh(x._pst_imag), cos(x._pst_real) * sinh(x._pst_imag)) ; } template complex sinh(const complex& x) { return complex(sinh(x._pst_real)*cos(x._pst_imag), cosh(x._pst_real) * sin(x._pst_imag)) ; } template complex sqrt(const complex& x) { const T abs = sqrt(x._pst_real * x._pst_real + x._pst_imag * x._pst_imag) ; if (abs==T(0)) { return complex(T(0), T(0)) ; } else if (x._pst_real > 0) { const T nr = sqrt(0.5 * (abs + x._pst_real)) ; return complex(nr, x._pst_imag / (2*nr)) ; } else { T ni = sqrt(0.5 * (abs - x._pst_real)) ; return (x._pst_imag < 0) ? complex(x._pst_imag / (-2*ni), -ni) : complex(x._pst_imag / (2*ni), ni) ; } } template complex tan(const complex& x) { return sin(x)/cos(x) ; } template complex tanh(const complex& x) { return sinh(x)/cosh(x) ; } template complex pow(const complex& x, int n) { if (n<0) return T(1) / pow(x, -n) ; if (n==0) return T(1) ; complex ret = x ; for (int i=1; i complex pow(const complex& x, const T& t) { complex tmp = log(x); volatile int x_is_null = 0 ; // Cast the result of exp() to the type 'T' in order to avoid an error in // case exp() is not overloaded for a type other than 'double'. return x_is_null ? complex(0, 0) : polar((T) exp(t * tmp._pst_real), t * tmp._pst_imag); } template complex pow(const complex& x, const complex& y) { volatile int x_is_null = 0 ; return x_is_null ? complex(0, 0) : exp(y * log(x)); } template complex pow(const T& t, const complex& y) { // Cast the result of pow() to the type 'T' in order to avoid an error in // case pow() is not overloaded for a type other than 'double'. return (t == T()) ? complex(0, 0): polar((T) pow(t, y._pst_real), (T) (y._pst_imag * log(t))); } } //namespace std ; #ifdef __PST_IMPLICIT_USING_STD /* Implicitly include a using directive for the STD namespace when this preprocessing flag is TRUE. */ using namespace std; #endif /* ifdef __PST_IMPLICIT_USING_STD */ #ifdef PST_VISUAL #pragma pack(pop) /* pop back to previous value */ #endif #endif /* PST_STL_COMPLEX */