Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/Deprecated/Complex.old/JComplex.old

Go to the documentation of this file.
00001 #ifndef __J2K__JCOMPLEX_HH__
00002 #define __J2K__JCOMPLEX_HH__
00003 
00004 // complex standard header
00005 #include <ymath.h>
00006 #include <cmath>
00007 #include <sstream>
00008 #include <xutility>
00009 
00010 /////////////////////////////////////////////////////////////////////////////
00011 // Since most calculations are done as double,                             //
00012 // why do we need a template to create:  char, int, float, long            //
00013 // Complex class, that will anyway do it as double                         //
00014 // and then rip off the decimal values !                                   //
00015 /////////////////////////////////////////////////////////////////////////////
00016 // This part is derived from the Complex STL template for double type.     //
00017 /////////////////////////////////////////////////////////////////////////////
00018 
00019 // You are forced to choose and take the same kind
00020 // of JComplex for your project. Just take it as double, folks !
00021 typedef double      CplxNumber;
00022 
00023 class JComplex {
00024 public:                                                                    
00025    // Constructors and Destructors                                         
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    // Utilities                                                            
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    // Arithmetic Operations
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 // Notice about STL crapt removed:                                         //
00144 /////////////////////////////////////////////////////////////////////////////
00145 // I rip off shit like Complex of a Complex Real and Complex Imag          //
00146 // added with another kind of Complex of a Complex Real and Complex Imag ! //
00147 // I didn't know that we can have a Complex with a Real part Complex ?!?   //
00148 // It's not to suppose to be Real ?  Real can have a Complex part ?        //
00149 // Perhaps, when I look at linear algebra, I don't see such a thing ?!?    //
00150 /////////////////////////////////////////////////////////////////////////////
00151 // DAMN. Why folks try to do it so complicated, when it so simple !        //
00152 // You don't believe it ?  Take a look at the original STL Complex class ! //
00153 // I could believe that it was suppose to be a Safety Net, but I don't     //
00154 // think that futuristic Maths will include a Complex part in a Real !     //
00155 // That's why it's called Real not Complex !  =)                           //
00156 // The second i
00157 /////////////////////////////////////////////////////////////////////////////
00158 // If your code was more Readable in the first place such stupidity        //
00159 // wouldn't be there !  That doesn't mean that it doesn't work, it's just  //
00160 // extra work for nothing ! Anyway, mostly Complex class I saw,            //
00161 // are double or float, so why bothering, take it as double.               //
00162 // If you need long double then change the first line of this header !     //
00163 /////////////////////////////////////////////////////////////////////////////
00164 // Notice to STL folks:                                                    //
00165 // It's not necessary to follow the 1001 rules of unreadability            //
00166 // when you write code that should be reusable !                           //
00167 // Put more spaces (not TABS) and more meaningful variable names           //
00168 // and a bit more comments about what it's suppose to do.                  //
00169 /////////////////////////////////////////////////////////////////////////////
00170 
00171    JComplex& operator*=( JComplex& x, const JComplex& y ) {
00172      // Reminder: (a+bj)(c+dj) = (ac + bcj + adj + bdjj)
00173      // Result:   (ac - bd) + ( bc + ad )j
00174      // jj = -1, by definition.
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      // Reminder: (a+bj)/(c+dj) = (a+bj)(c-dj) / [ (c+dj)(c-dj) ]
00187      // = (ac + bcj - adj - bdjj) / (cc - ddjj)
00188      // = [ (ac + bd) + ( bc - ad )j ] / [ cc + dd ]
00189      // jj = -1, by definition.
00190 
00191      // Doesn't handle Divide by Zero, but STL does it.
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 /* STL content
00204 // It use Complex double as temp to verify for NaN, Inf
00205 // Might use Double for that.
00206 {
00207    if ( _Isnan( Temp_re ) || _Isnan( Temp_im ) ) {
00208      x.Real( _Nanv( Temp_re )), x.Imag( x.Real );
00209    } else if (
00210    (Temp_im < 0 ? -Temp_im : +Temp_im) < (Temp_re < 0 ? -Temp_re : +Temp_re)
00211    ) {
00212      CplxNumber _Wr = Temp_im / Temp_re;
00213      CplxNumber _Wd = Temp_re + _Wr * Temp_im;
00214 
00215      if (_Isnan(_Wd) || _Wd == 0) {
00216        x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00217      } else {
00218        CplxNumber temp = (x.Real() + x.Imag() * _Wr) / _Wd;
00219        x.Imag((x.Imag() - x.Real() * _Wr) / _Wd);
00220        x.Real(temp);
00221      }
00222 
00223    } else if (Temp_im == 0) {
00224       x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00225    } else {
00226       CplxNumber _Wr = Temp_re / Temp_im;
00227       CplxNumber _Wd = Temp_im + _Wr * Temp_re;
00228 
00229       if (_Isnan(_Wd) || _Wd == 0) {
00230          x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00231       } else {
00232          CplxNumber temp = (x.Real() * _Wr + x.Imag()) / _Wd;
00233          x.Imag((x.Imag() * _Wr - x.Real()) / _Wd);
00234          x.Real(temp);
00235       }
00236    }
00237    return (x);
00238 }
00239 */
00240 
00241 /*
00242 
00243 // TEMPLATE FUNCTION operator>>
00244 template<class Exponent, class _Tr, class _U> inline
00245    basic_istream<Exponent, _Tr>&  operator>>(
00246       basic_istream<Exponent, _Tr>& Im, JComplex& x)
00247 {
00248    Exponent _Ch;
00249    long double Real, Imag;
00250    if (Im >> _Ch && _Ch != '(')
00251       Im.putback(_Ch), Im >> Real, Imag = 0;
00252    else if (Im >> Real >> _Ch && _Ch != ',')
00253       if (_Ch == ')')
00254          Imag = 0;
00255       else
00256          Im.putback(_Ch), Im.setstate(ios_base::failbit);
00257    else if (Im >> Imag >> _Ch && _Ch != ')')
00258          Im.putback(_Ch), Im.setstate(ios_base::failbit);
00259    if (!Im.fail())
00260       x = JComplex((_U)Real, (_U)Imag);
00261    return (Im);
00262 }
00263 
00264 // TEMPLATE FUNCTION operator<<
00265 template<class Exponent, class _Tr, class _U> inline
00266    basic_ostream<Exponent, _Tr>&  operator<<(
00267       basic_ostream<Exponent, _Tr>& _O, const JComplex& x)
00268    {basic_ostringstream<Exponent, _Tr, allocator<Exponent> > _S;
00269    _S.flags(_O.flags());
00270    _S.imbue(_O.getloc());
00271    _S.precision(_O.precision());
00272    _S << '(' << Real(x) << ',' << Imag(x) << ')';
00273    return (_O << _S.str().c_str()); }
00274  #include <xcomplex>
00275 */
00276 
00277 #endif

Generated on Sun Oct 14 18:46:19 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001