00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _VRCOMPLEX_H_
00021 #define _VRCOMPLEX_H_
00022
00023 #include <cmath>
00024 #include <iostream>
00025 using std::istream;
00026 using std::ostream;
00027
00028
00029 class VrComplex {
00030 public:
00031 VrComplex(float r,float i) :re(r),im(i) {}
00032 VrComplex(float r) :re(r),im(0) {}
00033 VrComplex() :re(0),im(0) {}
00034 VrComplex& operator = (const VrComplex&);
00035 VrComplex& operator = (const float&);
00036 VrComplex& operator = (const double&);
00037 VrComplex& operator = (const int&);
00038 VrComplex& operator += (const VrComplex&);
00039 VrComplex& operator -= (const VrComplex&);
00040 VrComplex& operator *= (const VrComplex&);
00041 VrComplex& operator /= (const VrComplex&);
00042 float real () const { return re; }
00043 float imag () const { return im; }
00044 void real(float x) {re = x;}
00045 void imag(float x) {im = x;}
00046 private:
00047 float re, im;
00048
00049
00050
00051 friend float real (const VrComplex&);
00052 friend float imag (const VrComplex&);
00053 friend VrComplex operator + (const VrComplex&, const VrComplex&);
00054 friend VrComplex operator + (const VrComplex&, float);
00055 friend VrComplex operator + (float, const VrComplex&);
00056 friend VrComplex operator - (const VrComplex&, const VrComplex&);
00057 friend VrComplex operator - (const VrComplex&, float);
00058 friend VrComplex operator - (float, const VrComplex&);
00059 friend VrComplex operator * (const VrComplex&, const VrComplex&);
00060 friend VrComplex operator * (const VrComplex&, float);
00061 friend VrComplex operator * (float, const VrComplex&);
00062 friend VrComplex operator / (const VrComplex&, const VrComplex&);
00063 friend VrComplex operator / (const VrComplex&, float);
00064 friend VrComplex operator / (float, const VrComplex&);
00065 friend bool operator == (const VrComplex&, const VrComplex&);
00066 friend bool operator == (const VrComplex&, float);
00067 friend bool operator == (float, const VrComplex&);
00068 friend bool operator != (const VrComplex&, const VrComplex&);
00069 friend bool operator != (const VrComplex&, float);
00070 friend bool operator != (float, const VrComplex&);
00071 friend VrComplex polar (float, float);
00072 friend VrComplex pow (const VrComplex&, const VrComplex&);
00073 friend VrComplex pow (const VrComplex&, float);
00074 friend VrComplex pow (const VrComplex&, int);
00075 friend VrComplex pow (float, const VrComplex&);
00076
00077
00078 };
00079
00080 inline VrComplex&
00081 VrComplex::operator = (const VrComplex& r)
00082 {
00083 re = r.re;
00084 im = r.im;
00085 return *this;
00086 }
00087
00088 inline VrComplex&
00089 VrComplex::operator = (const float& r)
00090 {
00091 re = r;
00092 im = 0;
00093 return *this;
00094 }
00095
00096 inline VrComplex&
00097 VrComplex::operator = (const double& r)
00098 {
00099 re = r;
00100 im = 0;
00101 return *this;
00102 }
00103
00104 inline VrComplex&
00105 VrComplex::operator = (const int& r)
00106 {
00107 re = r;
00108 im = 0;
00109 return *this;
00110 }
00111
00112
00113 inline VrComplex&
00114 VrComplex::operator += (const VrComplex& r)
00115 {
00116 re += r.re;
00117 im += r.im;
00118 return *this;
00119 }
00120
00121 inline VrComplex&
00122 VrComplex::operator -= (const VrComplex& r)
00123 {
00124 re -= r.re;
00125 im -= r.im;
00126 return *this;
00127 }
00128
00129 inline VrComplex&
00130 VrComplex::operator *= (const VrComplex& r)
00131 {
00132 float f = re * r.re - im * r.im;
00133 im = re * r.im + im * r.re;
00134 re = f;
00135 return *this;
00136 }
00137
00138 inline float
00139 imag (const VrComplex& x)
00140 {
00141 return x.imag ();
00142 }
00143
00144 inline float
00145 real (const VrComplex& x)
00146 {
00147 return x.real ();
00148 }
00149
00150 inline VrComplex
00151 operator + (const VrComplex& x, const VrComplex& y)
00152 {
00153 return VrComplex (real (x) + real (y), imag (x) + imag (y));
00154 }
00155
00156 inline VrComplex
00157 operator + (const VrComplex& x, float y)
00158 {
00159 return VrComplex (real (x) + y, imag (x));
00160 }
00161
00162 inline VrComplex
00163 operator + (float x, const VrComplex& y)
00164 {
00165 return VrComplex (x + real (y), imag (y));
00166 }
00167
00168 inline VrComplex
00169 operator - (const VrComplex& x, const VrComplex& y)
00170 {
00171 return VrComplex (real (x) - real (y), imag (x) - imag (y));
00172 }
00173
00174 inline VrComplex
00175 operator - (const VrComplex& x, float y)
00176 {
00177 return VrComplex (real (x) - y, imag (x));
00178 }
00179
00180 inline VrComplex
00181 operator - (float x, const VrComplex& y)
00182 {
00183 return VrComplex (x - real (y), - imag (y));
00184 }
00185
00186 inline VrComplex
00187 operator * (const VrComplex& x, const VrComplex& y)
00188 {
00189 return VrComplex (real (x) * real (y) - imag (x) * imag (y),
00190 real (x) * imag (y) + imag (x) * real (y));
00191 }
00192
00193 inline VrComplex
00194 operator * (const VrComplex& x, float y)
00195 {
00196 return VrComplex (real (x) * y, imag (x) * y);
00197 }
00198
00199 inline VrComplex
00200 operator * (float x, const VrComplex& y)
00201 {
00202 return VrComplex (x * real (y), x * imag (y));
00203 }
00204
00205 inline VrComplex
00206 operator / (const VrComplex& x, float y)
00207 {
00208 return VrComplex (real (x) / y, imag (x) / y);
00209 }
00210
00211 inline VrComplex
00212 operator + (const VrComplex& x)
00213 {
00214 return x;
00215 }
00216
00217 inline VrComplex
00218 operator - (const VrComplex& x)
00219 {
00220 return VrComplex (-real (x), -imag (x));
00221 }
00222
00223 inline bool
00224 operator == (const VrComplex& x, const VrComplex& y)
00225 {
00226 return real (x) == real (y) && imag (x) == imag (y);
00227 }
00228
00229 inline bool
00230 operator == (const VrComplex& x, float y)
00231 {
00232 return real (x) == y && imag (x) == 0;
00233 }
00234
00235 inline bool
00236 operator == (float x, const VrComplex& y)
00237 {
00238 return x == real (y) && imag (y) == 0;
00239 }
00240
00241 inline bool
00242 operator != (const VrComplex& x, const VrComplex& y)
00243 {
00244 return real (x) != real (y) || imag (x) != imag (y);
00245 }
00246
00247 inline bool
00248 operator != (const VrComplex& x, float y)
00249 {
00250 return real (x) != y || imag (x) != 0;
00251 }
00252
00253 inline bool
00254 operator != (float x, const VrComplex& y)
00255 {
00256 return x != real (y) || imag (y) != 0;
00257 }
00258
00259
00260 extern "C" double hypot (double, double);
00261
00262 inline float
00263 abs (const VrComplex& x)
00264 {
00265 return hypot (real (x), imag (x));
00266 }
00267
00268 inline float
00269 arg (const VrComplex& x)
00270 {
00271 return atan2 (imag (x), real (x));
00272 }
00273
00274 inline VrComplex
00275 polar (float r, float t)
00276 {
00277 return VrComplex (r * cos (t), r * sin (t));
00278 }
00279
00280 inline VrComplex
00281 conj (const VrComplex& x)
00282 {
00283 return VrComplex (real (x), -imag (x));
00284 }
00285
00286 inline float
00287 norm (const VrComplex& x)
00288 {
00289 return real (x) * real (x) + imag (x) * imag (x);
00290 }
00291
00292
00293 VrComplex operator / (const VrComplex& x, const VrComplex& y);
00294 VrComplex operator / (float x, const VrComplex& y);
00295 VrComplex cos (const VrComplex&);
00296 VrComplex cosh (const VrComplex&);
00297 VrComplex exp (const VrComplex&);
00298 VrComplex log (const VrComplex&);
00299 VrComplex pow (const VrComplex&, const VrComplex&);
00300 VrComplex pow (const VrComplex&, float);
00301 VrComplex pow (const VrComplex&, int);
00302 VrComplex pow (float, const VrComplex&);
00303 VrComplex sin (const VrComplex&);
00304 VrComplex sinh (const VrComplex&);
00305 VrComplex sqrt (const VrComplex&);
00306
00307
00308 inline char real(const char& c) {return c;}
00309 inline char imag(const char&) {return 0;}
00310
00311
00312 inline short real(const short& s) {return s;}
00313 inline short imag(const short&) {return 0;}
00314 inline int real(const int& i) {return i;}
00315 inline int imag(const int&) {return 0;}
00316 inline float real(const float& f) {return f;}
00317 inline float imag(const float&) {return 0;}
00318
00319
00320
00321 istream& operator >> (istream&, VrComplex&);
00322 ostream& operator << (ostream&, const VrComplex&);
00323
00324 #endif