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

C:/temp/src/j2k/Beta/Math/Complex/CplxMult.cpp

Go to the documentation of this file.
00001 #ifndef __J2K__JComplex_Multiplication_Division_CPP__
00002 #define __J2K__JComplex_Multiplication_Division_CPP__
00003 
00004 /////// TO BE FINISH !
00005 
00006 /////////////////////////////////////////////////////////////////////////////
00007 /// Scaling operations                                                    ///
00008 /////////////////////////////////////////////////////////////////////////////
00009 inline JComplex& JComplex::operator*=( const CplxDouble r ) {
00010   Real = Real * r;
00011   Imag = Imag * r;
00012   return (*this);
00013 }
00014 
00015 inline JComplex& JComplex::operator/=( const CplxDouble r ) {
00016   Real = Real / r;
00017   Imag = Imag / r;
00018   return (*this);
00019 }
00020 
00021 /////////////////////////////////////////////////////////////////////////////
00022 /// Multiplication and Division operations                                ///
00023 /////////////////////////////////////////////////////////////////////////////
00024 JComplex JComplex::Multiply( JComplex& x, const JComplex& y ) {
00025    // Reminder: (a+bj)(c+dj) = (ac + bcj + adj + bdjj)
00026    // Result:   (ac - bd) + ( bc + ad )j
00027    // jj = -1, by definition.
00028 
00029    CplxDouble Temp_Re = (x.Real * y.Real) - (x.Imag * y.Imag);
00030    CplxDouble Temp_Im = (x.Imag * y.Real) + (x.Real * y.Imag);
00031 
00032    return JComplex( Temp_Re, Temp_Im );
00033 }
00034 
00035 JComplex JComplex::Divide( JComplex& x, const JComplex& y ) {
00036    // Reminder: (a+bj)/(c+dj) = (a+bj)(c-dj) / [ (c+dj)(c-dj) ]
00037    // = (ac + bcj - adj - bdjj) / (cc - ddjj)
00038    // = [ (ac + bd) + ( bc - ad )j ] / [ cc + dd ]
00039    // jj = -1, by definition.
00040 
00041    // Doesn't handle Divide by Zero, but STL does it.
00042 
00043    CplxDouble Temp_delta = (y.Real * y.Real) + (y.Imag * y.Imag);
00044    CplxDouble Temp_Re    = (x.Real * y.Real) + (x.Imag * y.Imag);
00045    CplxDouble Temp_Im    = (x.Imag * y.Real) - (x.Real * y.Imag);
00046 
00047    return JComplex( Temp_Re / Temp_delta, Temp_Im / Temp_delta );
00048 }
00049 
00050 inline void JComplex::operator*=( const JComplex& c ) {
00051   this = Multiply( this, c );
00052 }
00053 
00054 inline void JComplex::operator/=( const JComplex& c ) {
00055   this = Divide( this, c );
00056 }
00057 
00058 inline JComplex JComplex::operator*( const JComplex& l, const JComplex& r) {
00059   return Multiply( l, r );
00060 }
00061 
00062 inline JComplex JComplex::operator/( const JComplex& l, const JComplex& r) {
00063   return Divide( l, r );
00064 }
00065 
00066 /* STL content
00067 // It use Complex double as temp to verify for NaN, Inf
00068 // Might use Double for that.
00069 {
00070    if ( _Isnan( Temp_re ) || _Isnan( Temp_im ) ) {
00071      x.Real( _Nanv( Temp_re )), x.Imag( x.Real );
00072    } else if (
00073    (Temp_im < 0 ? -Temp_im : +Temp_im) < (Temp_re < 0 ? -Temp_re : +Temp_re)
00074    ) {
00075      CplxDouble _Wr = Temp_im / Temp_re;
00076      CplxDouble _Wd = Temp_re + _Wr * Temp_im;
00077 
00078      if (_Isnan(_Wd) || _Wd == 0) {
00079        x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00080      } else {
00081        CplxDouble temp = (x.Real() + x.Imag() * _Wr) / _Wd;
00082        x.Imag((x.Imag() - x.Real() * _Wr) / _Wd);
00083        x.Real(temp);
00084      }
00085 
00086    } else if (Temp_im == 0) {
00087       x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00088    } else {
00089       CplxDouble _Wr = Temp_re / Temp_im;
00090       CplxDouble _Wd = Temp_im + _Wr * Temp_re;
00091 
00092       if (_Isnan(_Wd) || _Wd == 0) {
00093          x.Real(_Nanv(Temp_re)), x.Imag(x.Real());
00094       } else {
00095          CplxDouble temp = (x.Real() * _Wr + x.Imag()) / _Wd;
00096          x.Imag((x.Imag() * _Wr - x.Real()) / _Wd);
00097          x.Real(temp);
00098       }
00099    }
00100    return (x);
00101 }
00102 */
00103 
00104 /*
00105 
00106 // TEMPLATE FUNCTION operator>>
00107 template<class Exponent, class _Tr, class _U> inline
00108    basic_istream<Exponent, _Tr>&  operator>>(
00109       basic_istream<Exponent, _Tr>& Im, JComplex& x)
00110 {
00111    Exponent _Ch;
00112    long double Real, Imag;
00113    if (Im >> _Ch && _Ch != '(')
00114       Im.putback(_Ch), Im >> Real, Imag = 0;
00115    else if (Im >> Real >> _Ch && _Ch != ',')
00116       if (_Ch == ')')
00117          Imag = 0;
00118       else
00119          Im.putback(_Ch), Im.setstate(ios_base::failbit);
00120    else if (Im >> Imag >> _Ch && _Ch != ')')
00121          Im.putback(_Ch), Im.setstate(ios_base::failbit);
00122    if (!Im.fail())
00123       x = JComplex((_U)Real, (_U)Imag);
00124    return (Im);
00125 }
00126 
00127 // TEMPLATE FUNCTION operator<<
00128 template<class Exponent, class _Tr, class _U> inline
00129    basic_ostream<Exponent, _Tr>&  operator<<(
00130       basic_ostream<Exponent, _Tr>& _O, const JComplex& x)
00131    {basic_ostringstream<Exponent, _Tr, allocator<Exponent> > _S;
00132    _S.flags(_O.flags());
00133    _S.imbue(_O.getloc());
00134    _S.precision(_O.precision());
00135    _S << '(' << Real(x) << ',' << Imag(x) << ')';
00136    return (_O << _S.str().c_str()); }
00137  #include <xcomplex>
00138 */
00139 
00140 #endif

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