00001 #ifndef __J2K__JCOMPLEX_HH__
00002 #define __J2K__JCOMPLEX_HH__
00003
00004
00005 #include <ymath.h>
00006 #include <cmath>
00007 #include <sstream>
00008 #include <xutility>
00009
00010
00011
00012
00013
00014
00015
00016 // This part is derived from the Complex STL template for double type. //
00017
00018
00019
00020
00021 typedef double CplxNumber;
00022
00023 class JComplex {
00024 public:
00025
00026 inline JComplex()
00027 : Real( 0 ), Imag( 0 ) { }
00028
00029 inline JComplex( const CplxNumber& Re )
00030 : Real( Re ), Imag( 0 ) { }
00031
00032 inline JComplex( const CplxNumber& Re, const CplxNumber& Im )
00033 : Real( Re ), Imag( Im ) { }
00034
00035 inline JComplex( const JComplex& c )
00036 : Real( c.Real ), Imag( c.Imag ) { }
00037
00038 inline virtual ~JComplex() { }
00039
00040
00041 CplxNumber Real(const CplxNumber& x) {
00042 return (Real = x);
00043 }
00044
00045 CplxNumber Imag(const CplxNumber& y) {
00046 return (Imag = y);
00047 }
00048
00049 CplxNumber Real() const {
00050 return (Real);
00051 }
00052
00053 CplxNumber Imag() const {
00054 return (Imag);
00055 }
00056
00057
00058 JComplex& operator=( const CplxNumber& x ) {
00059 Real = x;
00060 Imag = 0;
00061 return (*this);
00062 }
00063
00064 JComplex& operator=( const JComplex& c ) {
00065 Real = c.Real;
00066 Imag = c.Imag;
00067 return (*this);
00068 }
00069
00070 JComplex& operator+=(const CplxNumber& x ) {
00071 Real = Real + x;
00072 return (*this);
00073 }
00074
00075 JComplex& operator+=( JComplex& x, const JComplex& y ) {
00076 x.Real = x.Real + y.Real;
00077 x.Imag = y.Imag + y.Imag;
00078 return (x);
00079 }
00080
00081 JComplex& operator-=(const CplxNumber& x) {
00082 Real = Real - x;
00083 return (*this);
00084 }
00085
00086 JComplex& operator-=( JComplex& x, const JComplex& y ) {
00087 x.Real = x.Real - y.Real;
00088 x.Imag = y.Imag - y.Imag;
00089 return (x);
00090 }
00091
00092 JComplex& operator*=(const CplxNumber& x) {
00093 Real = Real * x;
00094 Imag = Imag * x;
00095 return (*this);
00096 }
00097
00098 JComplex& operator/=(const CplxNumber& x) {
00099 Real = Real / x;
00100 Imag = Imag / x;
00101 return (*this);
00102 }
00103
00104 protected:
00105 CplxNumber Real;
00106 CplxNumber Imag;
00107
00108 public:
00109
00110 static BOOL Compare( const JComplex& l, const JComplex& r ) {
00111 return ( (l.Real == r.Real) && (l.Imag == r.Imag) );
00112 }
00113
00114 inline friend BOOL operator==( const JComplex& l, const JComplex& r ) {
00115 return Compare( l, r );
00116 }
00117
00118 inline friend BOOL operator!=( const Number& l, const Number& r ) {
00119 return !Compare( l, r );
00120 }
00121
00122
00123 static CplxNumber _Infv(CplxNumber) {
00124 return ( _Inf._D );
00125 }
00126
00127 static bool _Isinf(CplxNumber x) {
00128 double temp = (double)x;
00129 return (_Dtest(&temp) == _INFCODE);
00130 }
00131
00132 static bool _Isnan(CplxNumber x) {
00133 double temp = (double)x;
00134 return (_Dtest(&temp) == _NANCODE);
00135 }
00136
00137 static CplxNumber _Nanv(CplxNumber) {
00138 return (_Nan._D);
00139 }
00140
00141
00142
00143
00144
00145 // I rip off shit like Complex of a Complex Real and Complex Imag //
00146
00147
00148
00149
00150
00151 // DAMN. Why folks try to do it so complicated, when it so simple ! //
00152
00153
00154
00155
00156
00157
00158 // If your code was more Readable in the first place such stupidity //
00159
00160
00161
00162
00163
00164 // Notice to STL folks: //
00165
00166
00167
00168
00169
00170
00171 JComplex& operator*=( JComplex& x, const JComplex& y ) {
00172
00173
00174
00175
00176 CplxNumber Temp_Re = (x.Real * y.Real) - (x.Imag * y.Imag);
00177 CplxNumber Temp_Im = (x.Imag * y.Real) + (x.Real * y.Imag);
00178
00179 x.Real = Temp_Re;
00180 x.Imag = Temp_Im;
00181
00182 return (x);
00183 }
00184
00185 JComplex& operator/=( JComplex& x, const JComplex& y ) {
00186
00187
00188
00189
00190
00191
00192
00193 CplxNumber Temp_delta = (y.Real * y.Real) + (y.Imag * y.Imag);
00194
00195 CplxNumber Temp_Re = (x.Real * y.Real) + (x.Imag * y.Imag);
00196 CplxNumber Temp_Im = (x.Imag * y.Real) - (x.Real * y.Imag);
00197
00198 x.Real = Temp_Re / Temp_delta;
00199 x.Imag = Temp_Im / Temp_delta;
00200
00201 return (x);
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 #endif