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/CPLX.HH

Go to the documentation of this file.
00001 #ifndef __J2K__JCOMPLEX_HH__
00002 #define __J2K__JCOMPLEX_HH__
00003 
00004 /////////////////////////////////////////////////////////////////////////////
00005 // Since most calculations are done as double,                             //
00006 // why do we need a template to create:  char, int, float, long            //
00007 // Complex class, that will anyway do it as double                         //
00008 // and then rip off the decimal values !                                   //
00009 /////////////////////////////////////////////////////////////////////////////
00010 // This part is derived from the Complex STL template for double type.     //
00011 /////////////////////////////////////////////////////////////////////////////
00012 
00013 // You are forced to choose and take the same kind
00014 // of JComplex for your project. Just take it as double, folks !
00015 typedef double      CplxNumber;
00016 
00017 class JComplex {
00018 public:                                                                    
00019 /////////////////////////////////////////////////////////////////////////////
00020 /// Constructors and Destructors                                          ///
00021 /////////////////////////////////////////////////////////////////////////////
00022    // Constructors and Destructors                                         
00023    inline JComplex()                                                       
00024      : Real( 0 ), Imag( 0 ) { }                                            
00025                                                                            
00026    inline JComplex( const CplxNumber& Re )                                 
00027      : Real( Re ), Imag( 0 ) { }                                           
00028                                                                            
00029    inline JComplex( const CplxNumber& Re, const CplxNumber& Im )           
00030      : Real( Re ), Imag( Im ) { }                                          
00031                                                                            
00032    inline JComplex( const JComplex& c )                                    
00033      : Real( c.Real ), Imag( c.Imag ) { }                                  
00034                                                                            
00035    inline virtual ~JComplex() { }                                          
00036 
00037 /////////////////////////////////////////////////////////////////////////////
00038 /// Utilities                                                             ///
00039 /////////////////////////////////////////////////////////////////////////////
00040    CplxNumber Real(const CplxNumber& x) {                                  
00041       return (Real = x);                                                   
00042    }                                                                       
00043                                                                            
00044    CplxNumber Imag(const CplxNumber& y) {                                  
00045       return (Imag = y);                                                   
00046    }                                                                       
00047                                                                            
00048    CplxNumber Real() const {                                               
00049       return (Real);                                                       
00050    }                                                                       
00051                                                                            
00052    CplxNumber Imag() const {                                               
00053       return (Imag);                                                       
00054    }                                                                       
00055                                                                            
00056    // Arithmetic Operations
00057    JComplex& operator=( const CplxNumber& x ) {                            
00058       Real = x;                                                            
00059       Imag = 0;                                                            
00060       return (*this);                                                      
00061    }                                                                       
00062                                                                            
00063    JComplex& operator=( const JComplex& c ) {
00064       Real = c.Real;                                                       
00065       Imag = c.Imag;                                                       
00066       return (*this);                                                      
00067    }                                                                       
00068                                                                            
00069    JComplex& operator+=(const CplxNumber& x ) {                            
00070       Real = Real + x;                                                     
00071       return (*this);                                                      
00072    }                                                                       
00073                                                                            
00074    JComplex& operator+=( JComplex& x, const JComplex& y ) {                
00075      x.Real = x.Real + y.Real;
00076      x.Imag = y.Imag + y.Imag;
00077      return (x);                                                           
00078    }                                                                       
00079                                                                            
00080    JComplex& operator-=(const CplxNumber& x) {                             
00081       Real = Real - x;                                                     
00082       return (*this);                                                      
00083    }                                                                       
00084                                                                            
00085    JComplex& operator-=( JComplex& x, const JComplex& y ) {                
00086      x.Real = x.Real - y.Real;
00087      x.Imag = y.Imag - y.Imag;
00088      return (x);                                                           
00089    }                                                                       
00090                                                                            
00091    JComplex& operator*=(const CplxNumber& x) {                             
00092       Real = Real * x;                                                     
00093       Imag = Imag * x;                                                     
00094       return (*this);                                                      
00095    }                                                                       
00096                                                                            
00097    JComplex& operator/=(const CplxNumber& x) {                             
00098       Real = Real / x;                                                     
00099       Imag = Imag / x;                                                     
00100       return (*this);                                                      
00101    }                                                                       
00102                                                                            
00103 protected:                                                                 
00104    CplxNumber Real;                                                        
00105    CplxNumber Imag;                                                        
00106                                                                            
00107 public:                                                                    
00108                                                                            
00109   static BOOL Compare( const JComplex& l, const JComplex& r ) {            
00110     return ( (l.Real == r.Real) && (l.Imag == r.Imag) );                   
00111   }                                                                        
00112                                                                            
00113   inline friend BOOL operator==( const JComplex& l, const JComplex& r ) {  
00114     return Compare( l, r );                                                
00115   }                                                                        
00116                                                                            
00117   inline friend BOOL operator!=( const Number& l, const Number& r ) {      
00118     return !Compare( l, r );                                               
00119   }                                                                        
00120                                                                            
00121                                                                            
00122 
00123    JComplex& operator*=( JComplex& x, const JComplex& y ) {
00124      // Reminder: (a+bj)(c+dj) = (ac + bcj + adj + bdjj)
00125      // Result:   (ac - bd) + ( bc + ad )j
00126      // jj = -1, by definition.
00127 
00128      CplxNumber Temp_Re = (x.Real * y.Real) - (x.Imag * y.Imag);
00129      CplxNumber Temp_Im = (x.Imag * y.Real) + (x.Real * y.Imag);
00130 
00131      x.Real = Temp_Re;
00132      x.Imag = Temp_Im;
00133 
00134      return (x);
00135    }
00136 
00137    JComplex& operator/=( JComplex& x, const JComplex& y ) {
00138      // Reminder: (a+bj)/(c+dj) = (a+bj)(c-dj) / [ (c+dj)(c-dj) ]
00139      // = (ac + bcj - adj - bdjj) / (cc - ddjj)
00140      // = [ (ac + bd) + ( bc - ad )j ] / [ cc + dd ]
00141      // jj = -1, by definition.
00142 
00143      // Doesn't handle Divide by Zero, but STL does it.
00144 
00145      CplxNumber Temp_delta = (y.Real * y.Real) + (y.Imag * y.Imag);
00146 
00147      CplxNumber Temp_Re = (x.Real * y.Real) + (x.Imag * y.Imag);
00148      CplxNumber Temp_Im = (x.Imag * y.Real) - (x.Real * y.Imag);
00149 
00150      x.Real = Temp_Re / Temp_delta;
00151      x.Imag = Temp_Im / Temp_delta;
00152 
00153      return (x);
00154    }
00155 
00156    static CplxNumber _Infv(CplxNumber) {                                   
00157      return ( _Inf._D );                                                   
00158    }                                                                       
00159                                                                            
00160    static bool _Isinf(CplxNumber x) {                                      
00161      double temp = (double)x;                                              
00162      return (_Dtest(&temp) == _INFCODE);                                   
00163    }                                                                       
00164                                                                            
00165    static bool _Isnan(CplxNumber x) {                                      
00166      double temp = (double)x;                                              
00167      return (_Dtest(&temp) == _NANCODE);                                   
00168    }                                                                       
00169                                                                            
00170    static CplxNumber _Nanv(CplxNumber) {                                   
00171      return (_Nan._D);                                                     
00172    }                                                                       
00173 
00174 /* STL content
00175 // It use Complex double as temp to verify for NaN, Inf
00176 // Might use Double for that.
00177 {
00178    if ( _Isnan( Temp_re ) || _Isnan( Temp_im ) ) {
00179      x.Real( _Nanv( Temp_re )), x.Imag( x.Real );
00180    } else if (
00181    (Temp_im < 0 ? -Temp_im : +Temp_im) < (Temp_re < 0 ? -Temp_re : +Temp_re)
00182    ) {
00183      CplxNumber _Wr = Temp_im / Temp_re;
00184      CplxNumber _Wd = Temp_re + _Wr * Temp_im;
00185 
00186      if (_Isnan(_Wd) || _Wd == 0) {
00187        x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00188      } else {
00189        CplxNumber temp = (x.Real() + x.Imag() * _Wr) / _Wd;
00190        x.Imag((x.Imag() - x.Real() * _Wr) / _Wd);
00191        x.Real(temp);
00192      }
00193 
00194    } else if (Temp_im == 0) {
00195       x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00196    } else {
00197       CplxNumber _Wr = Temp_re / Temp_im;
00198       CplxNumber _Wd = Temp_im + _Wr * Temp_re;
00199 
00200       if (_Isnan(_Wd) || _Wd == 0) {
00201          x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00202       } else {
00203          CplxNumber temp = (x.Real() * _Wr + x.Imag()) / _Wd;
00204          x.Imag((x.Imag() * _Wr - x.Real()) / _Wd);
00205          x.Real(temp);
00206       }
00207    }
00208    return (x);
00209 }
00210 */
00211 
00212 /*
00213 
00214 // TEMPLATE FUNCTION operator>>
00215 template<class Exponent, class _Tr, class _U> inline
00216    basic_istream<Exponent, _Tr>&  operator>>(
00217       basic_istream<Exponent, _Tr>& Im, JComplex& x)
00218 {
00219    Exponent _Ch;
00220    long double Real, Imag;
00221    if (Im >> _Ch && _Ch != '(')
00222       Im.putback(_Ch), Im >> Real, Imag = 0;
00223    else if (Im >> Real >> _Ch && _Ch != ',')
00224       if (_Ch == ')')
00225          Imag = 0;
00226       else
00227          Im.putback(_Ch), Im.setstate(ios_base::failbit);
00228    else if (Im >> Imag >> _Ch && _Ch != ')')
00229          Im.putback(_Ch), Im.setstate(ios_base::failbit);
00230    if (!Im.fail())
00231       x = JComplex((_U)Real, (_U)Imag);
00232    return (Im);
00233 }
00234 
00235 // TEMPLATE FUNCTION operator<<
00236 template<class Exponent, class _Tr, class _U> inline
00237    basic_ostream<Exponent, _Tr>&  operator<<(
00238       basic_ostream<Exponent, _Tr>& _O, const JComplex& x)
00239    {basic_ostringstream<Exponent, _Tr, allocator<Exponent> > _S;
00240    _S.flags(_O.flags());
00241    _S.imbue(_O.getloc());
00242    _S.precision(_O.precision());
00243    _S << '(' << Real(x) << ',' << Imag(x) << ')';
00244    return (_O << _S.str().c_str()); }
00245  #include <xcomplex>
00246 */
00247 
00248 #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