/* Copyright (C) 2004 Garrett A. Kajmowicz This file is part of the uClibc++ Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include #include #ifndef __STD_HEADER_COMPLEX #define __STD_HEADER_COMPLEX 1 namespace std { // class complex; // class complex; // class complex; template class _UCXXEXPORT complex{ public: typedef T value_type; complex(const T& re = T(), const T& im = T()) : r(re), i(im) { } complex(const complex& c): r(c.r), i(c.i){ } template complex(const complex& c): r(c.r), i(c.i){ } inline T real() const{ return r; } inline T imag() const{ return i; } complex& operator= (const T& v){ r = v; i = 0; return *this; } complex& operator+=(const T& v){ r +=v; return *this; } complex& operator-=(const T& v){ r -=v; return *this; } complex& operator*=(const T& v){ r*=v; i*=v; return *this; } complex& operator/=(const T& v){ r/=v; i/=v; return *this; } complex& operator=(const complex& v){ if(&v != this){ r = v.r; i = v.i; } return *this; } template complex& operator= (const complex& v){ r = v.r; i = v.i; return *this; } template complex& operator+=(const complex& v){ r+=v.r; i+=v.i; return *this; } template complex& operator-=(const complex& v){ r-=v.r; i-=v.i; return *this; } template complex& operator*=(const complex& v){ T tempr = r*v.r - i*v.i; T tempi = r*v.i + i*v.r; r = tempr; i = tempi; return *this; } template complex& operator/=(const complex& v){ T tempr = (r*v.r + i*v.i) / (v.r*v.r + v.i*v.i); T tempi = (i*v.r - r*v.i) / (v.r*v.r + v.i*v.i); r = tempr; i = tempi; return *this; } private: T r; T i; }; template _UCXXEXPORT complex operator+(const complex& ls, const complex& rs){ complex retval(ls); retval += rs; return retval; } template _UCXXEXPORT complex operator+(const complex& ls, const T& rs){ complex retval(ls); ls += rs; return retval; } template _UCXXEXPORT inline complex operator+(const T& ls, const complex& rs){ return rs + ls; } template _UCXXEXPORT complex operator-(const complex& ls, const complex& rs){ complex retval(ls); retval -= rs; return retval; } template _UCXXEXPORT complex operator-(const complex& ls, const T& rs){ complex retval(ls); retval -= rs; return retval; } template _UCXXEXPORT complex operator-(const T& ls, const complex& rs){ complex retval(ls); retval -= rs; return retval; } template _UCXXEXPORT complex operator*(const complex& ls, const complex& rs){ complex retval(ls); retval *= rs; return retval; } template _UCXXEXPORT complex operator*(const complex& ls, const T& rs){ complex retval(ls); retval *= rs; return retval; } template _UCXXEXPORT complex operator*(const T& ls, const complex& rs){ complex retval(ls); retval *=rs; return retval; } template _UCXXEXPORT complex operator/(const complex& ls, const complex& rs){ complex retval(ls); retval/=rs; return retval; } template _UCXXEXPORT complex operator/(const complex& ls, const T& rs){ complex retval(ls); retval/=rs; return retval; } template _UCXXEXPORT complex operator/(const T& ls, const complex& rs){ complex retval(ls); retval/=rs; return retval; } template _UCXXEXPORT complex operator+(const complex& v){ return v; } template _UCXXEXPORT complex operator-(const complex& v){ return complex (-v.real(), -v.imag()); } template _UCXXEXPORT bool operator==(const complex& ls, const complex& rs){ if( ls.real() == rs.real() && ls.imag() == rs.image()){ return true; } return false; } template _UCXXEXPORT bool operator==(const complex& ls, const T& rs){ if(ls.real() == rs && ls.imag() == T()){ return true; } return false; } template _UCXXEXPORT bool operator==(const T& ls, const complex& rs){ if(ls == rs.real() && rs.imag() == T()){ return true; } return false; } template _UCXXEXPORT bool operator!=(const complex& ls, const complex& rs){ if(ls == rs){ return false; } return true; } template _UCXXEXPORT bool operator!=(const complex& ls, const T& rs){ if(ls == rs){ return false; } return true; } template _UCXXEXPORT bool operator!=(const T& ls, const complex& rs){ if(ls == rs){ return false; } return true; } template _UCXXEXPORT basic_istream& operator>>(basic_istream& is, complex& v) { T tempr; T tempi; is >> tempr; is.get(); is >> tempi; v = complex(tempr, tempi); return is; } template _UCXXEXPORT basic_ostream& operator<<(basic_ostream& os, const complex&v) { os << v.real() << ", " << v.imag(); return os; } template _UCXXEXPORT T real(const complex& v){ return v.real(); } template _UCXXEXPORT T imag(const complex& v){ return v.imag(); } template _UCXXEXPORT T norm(const complex& v){ return (v.real() * v.real() + v.imag() * v.imag()); } template _UCXXEXPORT complex conj(const complex& v){ return complex(v.real(), -v.imag()); } #ifdef __UCLIBCXX_SUPPORT_MATH__ //Can we link with libm? template _UCXXEXPORT T abs(const complex& v){ return sqrt(v.real() * v.real() + v.imag() * v.imag()); } template _UCXXEXPORT T arg(const complex& v){ return atan2(v.imag(), v.real()); } template _UCXXEXPORT complex polar(const T& rho, const T& theta){ return complex(rho * cos(theta), rho * sin(theta)); } template _UCXXEXPORT complex cos (const complex& v){ return complex(cos(v.real()) * cosh(v.imag()), -sin(v.real()) * sinh(v.imag())); } template _UCXXEXPORT complex cosh (const complex& v){ return complex(cosh(v.real()) * cos(v.imag()), sinh(v.real()) * sin(v.imag())); } template _UCXXEXPORT complex exp (const complex& v){ return polar(exp(v.real()), v.imag()); } template _UCXXEXPORT complex log (const complex& v){ return complex(log(abs(v)), arg(v)); } template _UCXXEXPORT complex log10(const complex& v){ return (log(v) / log(T(10.0))); } template _UCXXEXPORT complex pow(const complex& v, int p){ T rho = pow(abs(v), p); T theta = arg(v); return complex(rho * cos(p * theta), rho * sin(p * theta) ); } template _UCXXEXPORT complex pow(const complex& v, const T& p){ return polar( pow(abs(v),p), arg(v)*p ); } template _UCXXEXPORT complex pow(const complex& v, const complex& p){ if(v == T()){ //We are using "0" as the value return T(); } return exp(p * log(v)); } template _UCXXEXPORT complex pow(const T& v, const complex& p){ if(v == T()){ return T(); } return polar(pow(v,p.real()), y.imag() * log(x) ); } template _UCXXEXPORT complex sin (const complex& v){ return complex(sin(v.real()) * cosh(v.imag()), cosh(v.real()) * sin(v.imag())); } template _UCXXEXPORT complex sinh (const complex& v){ return complext(sinh(v.real()) * cos(v.imag()), cosh(v.real()) * sin(v.imag()) ); } template _UCXXEXPORT complex sqrt (const complex&); template _UCXXEXPORT complex tan (const complex& v){ return sin(v) / cos(v); } template _UCXXEXPORT complex tanh (const complex& v){ return sinh(v) / cosh(v); } #endif } #endif