00001
00018 #ifndef _COMPLEX_H
00019 #define _COMPLEX_H
00020
00021 typedef enum {I='i',J='j',A='d', R='r'} CNOTATION;
00022 #define CNOTATION_DEFAULT J
00023 #define PI 3.1415926535897932384626433832795
00024 #define E 2.71828182845905
00025
00026
00027 #ifdef __cplusplus
00028 #include <math.h>
00029 #include "platform.h"
00030 class complex {
00031 private:
00032 double r;
00033 double i;
00034 CNOTATION f;
00035 public:
00037 inline complex()
00038 {
00039 r = 0;
00040 i = 0;
00041 f = CNOTATION_DEFAULT;
00042 };
00043 inline complex(double re)
00044 {
00045 r = re;
00046 i = 0;
00047 f = CNOTATION_DEFAULT;
00048 };
00049 inline complex(double re, double im, CNOTATION nf=CNOTATION_DEFAULT)
00050 {
00051 f = nf;
00052
00053
00054
00055
00056
00057
00058 r = re;
00059 i = im;
00060
00061 };
00062
00063
00064 inline complex &operator = (complex x)
00065 {
00066 r = x.r;
00067 i = x.i;
00068 f = x.f;
00069 return *this;
00070 };
00071 inline complex &operator = (double x)
00072 {
00073 r = x;
00074 i = 0;
00075 f = CNOTATION_DEFAULT;
00076 return *this;
00077 };
00078
00079
00080 inline double & Re(void)
00081 {
00082 return r;
00083 };
00084 inline double & Im(void)
00085 {
00086 return i;
00087 };
00088 inline CNOTATION & Notation(void)
00089 {
00090 return f;
00091 };
00092 inline double Mag(void) const
00093 {
00094 return sqrt(r*r+i*i);
00095 };
00096 inline double Mag(double m)
00097 {
00098 double old = sqrt(r*r+i*i);
00099 r *= m/old;
00100 i *= m/old;
00101 return m;
00102 };
00103 inline double Arg(void) const
00104 {
00105 if (r==0)
00106 {
00107 if (i>0)
00108 return PI/2;
00109 else if (i==0)
00110 return 0;
00111 else
00112 return -PI/2;
00113 }
00114 else if (r>0)
00115 return atan(i/r);
00116 else
00117 return PI+atan(i/r);
00118 };
00119 inline double Arg(double a)
00120 {
00121 SetPolar(Mag(),a,f);
00122 return a;
00123 };
00124 inline complex Log(void) const
00125 {
00126 return complex(log(Mag()),Arg(),f);
00127 };
00128 inline void SetReal(double v)
00129 {
00130 r = v;
00131 };
00132 inline void SetImag(double v)
00133 {
00134 i = v;
00135 };
00136 inline void SetNotation(CNOTATION nf)
00137 {
00138 f = nf;
00139 }
00140 inline void SetRect(double rp, double ip, CNOTATION nf=CNOTATION_DEFAULT)
00141 {
00142 r = rp;
00143 i = ip;
00144 f = nf;
00145 };
00146 inline void SetPolar(double m, double a, CNOTATION nf=A)
00147 {
00148 r = (m*cos(a));
00149 i = (m*sin(a));
00150 f = nf;
00151 };
00152
00153 #if 0
00154
00155
00156
00157
00158 #endif
00159
00160 inline complex operator - (void)
00161 {
00162 return complex(-r,-i,f);
00163 };
00164 inline complex operator ~ (void)
00165 {
00166 return complex(r,-i,f);
00167 };
00168
00169
00170 inline complex &operator += (double x)
00171 {
00172 r += x;
00173 return *this;
00174 };
00175 inline complex &operator -= (double x)
00176 {
00177 r -= x;
00178 return *this;
00179 };
00180 inline complex &operator *= (double x)
00181 {
00182 r *= x;
00183 i *= x;
00184 return *this;
00185 };
00186 inline complex &operator /= (double x)
00187 {
00188 r /= x;
00189 i /= x;
00190 return *this;
00191 };
00192 inline complex &operator ^= (double x)
00193 {
00194 double lm = log(Mag()), a = Arg(), b = exp(x*lm), c = x*a;
00195 r = (b*cos(c));
00196 i = (b*sin(c));
00197 return *this;
00198 };
00199 inline complex &operator += (complex x)
00200 {
00201 r += x.r;
00202 i += x.i;
00203 return *this;
00204 };
00205 inline complex &operator -= (complex x)
00206 {
00207 r -= x.r;
00208 i -= x.i;
00209 return *this;
00210 };
00211 inline complex &operator *= (complex x)
00212 {
00213 double pr=r;
00214 r = pr * x.r - i * x.i;
00215 i = pr * x.i + i * x.r;
00216 return *this;
00217 };
00218 inline complex &operator /= (complex y)
00219 {
00220 double xr=r;
00221 double a = y.r*y.r+y.i*y.i;
00222 r = (xr*y.r+i*y.i)/a;
00223 i = (i*y.r-xr*y.i)/a;
00224 return *this;
00225 };
00226 inline complex &operator ^= (complex x)
00227 {
00228 double lm = log(Mag()), a = Arg(), b = exp(x.r*lm-x.i*a), c = x.r*a+x.i*lm;
00229 r = (b*cos(c));
00230 i = (b*sin(c));
00231 return *this;
00232 };
00233
00234
00235 inline complex operator + (double y)
00236 {
00237 complex x(*this);
00238 return x+=y;
00239 };
00240 inline complex operator - (double y)
00241 {
00242 complex x(*this);
00243 return x-=y;
00244 };
00245 inline complex operator * (double y)
00246 {
00247 complex x(*this);
00248 return x*=y;
00249 };
00250 inline complex operator / (double y)
00251 {
00252 complex x(*this);
00253 return x/=y;
00254 };
00255 inline complex operator ^ (double y)
00256 {
00257 complex x(*this);
00258 return x^=y;
00259 };
00260 inline complex operator + (complex y)
00261 {
00262 complex x(*this);
00263 return x+=y;
00264 };
00265 inline complex operator - (complex y)
00266 {
00267 complex x(*this);
00268 return x-=y;
00269 };
00270 inline complex operator * (complex y)
00271 {
00272 complex x(*this);
00273 return x*=y;
00274 };
00275 inline complex operator / (complex y)
00276 {
00277 complex x(*this);
00278 return x/=y;
00279 };
00280 inline complex operator ^ (complex y)
00281 {
00282 complex x(*this);
00283 return x^=y;
00284 };
00285
00287 inline complex SetPowerFactor(double mag,
00288 double pf,
00289 CNOTATION n=J)
00290 {
00291 SetPolar(mag/pf, acos(pf),n);
00292 return *this;
00293 }
00294
00295
00296
00297 inline bool IsZero(double err=0.0)
00298 {
00299 return Mag()<=err;
00300 }
00301
00302
00303 inline bool operator == (double m) { return Mag()==m; };
00304 inline bool operator != (double m) { return Mag()!=m; };
00305 inline bool operator < (double m) { return Mag()<m; };
00306 inline bool operator <= (double m) { return Mag()<=m; };
00307 inline bool operator > (double m) { return Mag()>m; };
00308 inline bool operator >= (double m) { return Mag()>=m; };
00309
00310
00311 inline bool operator == (complex y) { return fmod(y.Arg()-Arg(),2*PI)==0.0;};
00312 inline bool operator != (complex y) { return fmod(y.Arg()-Arg(),2*PI)!=0.0;};
00313 inline bool operator < (complex y) { return fmod(y.Arg()-Arg(),2*PI)<PI;};
00314 inline bool operator <= (complex y) { return fmod(y.Arg()-Arg(),2*PI)<=PI;};
00315 inline bool operator > (complex y) { return fmod(y.Arg()-Arg(),2*PI)>PI;};
00316 inline bool operator >= (complex y) { return fmod(y.Arg()-Arg(),2*PI)>=PI;};
00317 inline bool IsFinite(void) { return isfinite(r) && isfinite(i); };
00318 };
00319 #else
00320 typedef struct {
00321 double r;
00322 double i;
00323 CNOTATION f;
00324 } complex;
00325 #define complex_set_polar(X,M,A) ((X).r=((M)*cos(A)),(X).i=((M)*sin(A)),(X))
00326 #define complex_set_power_factor(X,M,P) complex_set_polar((X),(M)/(P),acos(P))
00327 #endif
00328
00329 #endif
00330