diff --git a/unsupported/Eigen/Complex b/unsupported/Eigen/Complex new file mode 100644 index 000000000..f8d9af25c --- /dev/null +++ b/unsupported/Eigen/Complex @@ -0,0 +1,142 @@ +#ifndef EIGEN_COMPLEX_H +#define EIGEN_COMPLEX_H + +#include + +namespace Eigen { + +template +struct castable_pointer +{ + castable_pointer(_NativePtr ptr) : _ptr(ptr) {} + operator _NativePtr () {return _ptr;} + operator _PunnedPtr () {return reinterpret_cast<_PunnedPtr>(_ptr);} + private: + _NativePtr _ptr; +}; + +template +struct Complex +{ + typedef typename std::complex StandardComplex; + typedef T value_type; + + Complex(const T& re = T(), const T& im = T()) : _re(re),_im(im) { } + Complex(const Complex&other ): _re(other.real()) ,_im(other.imag()) {} + + template + Complex(const Complex&other): _re(other.real()) ,_im(other.imag()) {} + template + Complex(const std::complex&other): _re(other.real()) ,_im(other.imag()) {} + + typedef castable_pointer< Complex*, StandardComplex* > pointer_type; + typedef castable_pointer< const Complex*, const StandardComplex* > const_pointer_type; + + pointer_type operator & () {return pointer_type(this);} + const_pointer_type operator & () const {return const_pointer_type(this);} + + StandardComplex std_type() const {return StandardComplex(real(),imag());} + StandardComplex & std_type() {return *(StandardComplex*)(&(*this));} + + operator StandardComplex () const {return std_type();} + operator StandardComplex & () {return std_type();} + + T & real() {return _re;} + T & imag() {return _im;} + const T & real() const {return _re;} + const T & imag() const {return _im;} + + // *** complex member functions: *** + Complex& operator= (const T& val) { _re=val;_im=0;return *this; } + Complex& operator+= (const T& val) {_re+=val;return *this;} + Complex& operator-= (const T& val) {_re-=val;return *this;} + Complex& operator*= (const T& val) {_re*=val;_im*=val;return *this; } + Complex& operator/= (const T& val) {_re/=val;_im/=val;return *this; } + + Complex& operator= (const Complex& rhs) {_re=rhs._re;_im=rhs._im;return *this;} + Complex& operator= (const StandardComplex& rhs) {_re=rhs.real();_im=rhs.imag();return *this;} + + template Complex& operator= (const Complex& rhs) { _re=rhs._re;_im=rhs._im;return *this;} + template Complex& operator+= (const Complex& rhs) { _re+=rhs._re;_im+=rhs._im;return *this;} + template Complex& operator-= (const Complex& rhs) { _re-=rhs._re;_im-=rhs._im;return *this;} + template Complex& operator*= (const Complex& rhs) { this->std_type() *= rhs.std_type(); return *this; } + template Complex& operator/= (const Complex& rhs) { this->std_type() /= rhs.std_type(); return *this; } + + private: + T _re; + T _im; +}; + +template +T ei_to_std( const T & x) {return x;} + +template +std::complex ei_to_std( const Complex & x) {return x.std_type();} + +// 26.2.6 operators +template Complex operator+(const Complex& rhs) {return rhs;} +template Complex operator-(const Complex& rhs) {return -ei_to_std(rhs);} + +template Complex operator+(const Complex& lhs, const Complex& rhs) { return ei_to_std(lhs) + ei_to_std(rhs);} +template Complex operator-(const Complex& lhs, const Complex& rhs) { return ei_to_std(lhs) - ei_to_std(rhs);} +template Complex operator*(const Complex& lhs, const Complex& rhs) { return ei_to_std(lhs) * ei_to_std(rhs);} +template Complex operator/(const Complex& lhs, const Complex& rhs) { return ei_to_std(lhs) / ei_to_std(rhs);} +template bool operator==(const Complex& lhs, const Complex& rhs) { return ei_to_std(lhs) == ei_to_std(rhs);} +template bool operator!=(const Complex& lhs, const Complex& rhs) { return ei_to_std(lhs) != ei_to_std(rhs);} + +template Complex operator+(const Complex& lhs, const T& rhs) {return ei_to_std(lhs) + ei_to_std(rhs); } +template Complex operator-(const Complex& lhs, const T& rhs) {return ei_to_std(lhs) - ei_to_std(rhs); } +template Complex operator*(const Complex& lhs, const T& rhs) {return ei_to_std(lhs) * ei_to_std(rhs); } +template Complex operator/(const Complex& lhs, const T& rhs) {return ei_to_std(lhs) / ei_to_std(rhs); } +template bool operator==(const Complex& lhs, const T& rhs) {return ei_to_std(lhs) == ei_to_std(rhs); } +template bool operator!=(const Complex& lhs, const T& rhs) {return ei_to_std(lhs) != ei_to_std(rhs); } + +template Complex operator+(const T& lhs, const Complex& rhs) {return ei_to_std(lhs) + ei_to_std(rhs); } +template Complex operator-(const T& lhs, const Complex& rhs) {return ei_to_std(lhs) - ei_to_std(rhs); } +template Complex operator*(const T& lhs, const Complex& rhs) {return ei_to_std(lhs) * ei_to_std(rhs); } +template Complex operator/(const T& lhs, const Complex& rhs) {return ei_to_std(lhs) / ei_to_std(rhs); } +template bool operator==(const T& lhs, const Complex& rhs) {return ei_to_std(lhs) == ei_to_std(rhs); } +template bool operator!=(const T& lhs, const Complex& rhs) {return ei_to_std(lhs) != ei_to_std(rhs); } + +template +std::basic_istream& + operator>> (std::basic_istream& istr, Complex& rhs) +{ + return istr >> rhs.std_type(); +} + +template +std::basic_ostream& +operator<< (std::basic_ostream& ostr, const Complex& rhs) +{ + return ostr << rhs.std_type(); +} + + // 26.2.7 values: + template T real(const Complex&x) {return real(ei_to_std(x));} + template T abs(const Complex&x) {return abs(ei_to_std(x));} + template T arg(const Complex&x) {return arg(ei_to_std(x));} + template T norm(const Complex&x) {return norm(ei_to_std(x));} + + template Complex conj(const Complex&x) { return conj(ei_to_std(x));} + template Complex polar(const T& x, const T&y) {return polar(ei_to_std(x),ei_to_std(y));} + // 26.2.8 transcendentals: + template Complex cos (const Complex&x){return cos(ei_to_std(x));} + template Complex cosh (const Complex&x){return cosh(ei_to_std(x));} + template Complex exp (const Complex&x){return exp(ei_to_std(x));} + template Complex log (const Complex&x){return log(ei_to_std(x));} + template Complex log10 (const Complex&x){return log10(ei_to_std(x));} + + template Complex pow(const Complex&x, int p) {return pow(ei_to_std(x),ei_to_std(p));} + template Complex pow(const Complex&x, const T&p) {return pow(ei_to_std(x),ei_to_std(p));} + template Complex pow(const Complex&x, const Complex&p) {return pow(ei_to_std(x),ei_to_std(p));} + template Complex pow(const T&x, const Complex&p) {return pow(ei_to_std(x),ei_to_std(p));} + + template Complex sin (const Complex&x){return sin(ei_to_std(x));} + template Complex sinh (const Complex&x){return sinh(ei_to_std(x));} + template Complex sqrt (const Complex&x){return sqrt(ei_to_std(x));} + template Complex tan (const Complex&x){return tan(ei_to_std(x));} + template Complex tanh (const Complex&x){return tanh(ei_to_std(x));} +} + +#endif