00001 #ifndef __RComplex_H__ 00002 #define __RComplex_H__ 00003 00004 00005 00024 #include <stdio.h> 00025 #include <math.h> 00026 00027 class RComplex 00028 { 00029 public: 00030 00031 double a; // real 00032 double b; // imaginar 00033 00034 public: 00035 00036 RComplex(){ a=0.0; b=0.0; } 00037 RComplex(double real, double img=0.0){ a=real; b=img; } 00038 RComplex(double root, double theta, int sign){ a=root*cos(sign*theta); b=root*sin(sign*theta); } 00039 RComplex(const RComplex& x){ a=x.a; b=x.b; } 00040 RComplex& operator=(const RComplex& x){ a=x.a; b=x.b; return *this; } 00041 00042 ~RComplex(){} 00043 00044 inline double getReal(){return a;} 00045 inline double getImg(){return b;} 00046 00047 double getRoot(){ return ::sqrt( a*a + b*b ); } 00048 double getAbs(){ return ::sqrt( a*a + b*b ); } 00049 double getArg(); 00050 00051 inline RComplex conjugate(){ return RComplex(a, -b); } 00052 00053 00054 // math complex op 00055 RComplex sqrt(); 00056 RComplex pow(double pow); 00057 RComplex sqr(){ RComplex x(a*a-b*b, 2.0*a*b); return x; } 00058 RComplex exp(){ double ex= ::exp(a); RComplex x( ex*cos(b), ex*sin(b) ); return x; } 00059 RComplex log(); 00060 00061 00062 // real operations 00063 void operator+=(double value){ a += value; } 00064 void operator-=(double value){ a -= value; } 00065 void operator*=(double value){ a *= value; b *= value; } 00066 void operator/=(double value){ a /= value; b /= value; } 00067 00068 // complex operations 00069 void operator+=(const RComplex& v){ a += v.a; b += v.b; } 00070 void operator-=(const RComplex& v){ a -= v.a; b -= v.b; } 00071 void operator*=(const RComplex& v){ RComplex x(a*v.a-b*v.b, a*v.b + b*v.a); *this=x; } 00072 void operator/=(const RComplex& v){ RComplex x(a*v.a+b*v.b, a*v.b - b*v.a); x/=(v.a*v.a+v.b*v.b); *this=x; } 00073 00074 // friends 00075 friend RComplex operator+(const RComplex& v, double a); // v<op>a 00076 friend RComplex operator+(double a, const RComplex& v); // a<op>v 00077 friend RComplex operator-(const RComplex& v, double a); // v<op>a 00078 friend RComplex operator-(double a, const RComplex& v); // a<op>v 00079 friend RComplex operator*( const RComplex& v, double a); // v<op>a 00080 friend RComplex operator*(double a,const RComplex& v); // a<op>v 00081 friend RComplex operator/(const RComplex& v, double a); // v<op>a 00082 00083 friend RComplex operator+(const RComplex& v1, const RComplex& v2); // v1<op>v2 00084 friend RComplex operator-(const RComplex& v1, const RComplex& v2); // v1<op>v2 00085 friend RComplex operator*(const RComplex& v1, const RComplex& v2); // v1<op>v2 00086 friend RComplex operator/(const RComplex& v1, const RComplex& v2); // v1<op>v2 00087 00088 void output(){printf("(%f, i*%f)", a, b);} 00089 void output(FILE* file){fprintf(file, "(%f, i*%f)", a, b);} 00090 }; 00091 00092 #endif 00093 00094