Wm4BandedMatrix.inl

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.0 (2006/06/28)
00016 
00017 //----------------------------------------------------------------------------
00018 namespace Wm4
00019 {
00020 
00021 template <class Real>
00022 BandedMatrix<Real>::BandedMatrix (int iSize, int iLBands, int iUBands)
00023 {
00024     assert(iSize > 0 && iLBands >= 0 && iUBands >= 0);
00025     assert(iLBands < iSize && iUBands < iSize);
00026 
00027     m_iSize = iSize;
00028     m_iLBands = iLBands;
00029     m_iUBands = iUBands;
00030     Allocate();
00031 }
00032 //----------------------------------------------------------------------------
00033 template <class Real>
00034 BandedMatrix<Real>::BandedMatrix (const BandedMatrix& rkM)
00035 {
00036     m_afDBand = 0;
00037     m_aafLBand = 0;
00038     m_aafUBand = 0;
00039     *this = rkM;
00040 }
00041 //----------------------------------------------------------------------------
00042 template <class Real>
00043 BandedMatrix<Real>::~BandedMatrix ()
00044 {
00045     Deallocate();
00046 }
00047 //----------------------------------------------------------------------------
00048 template <class Real>
00049 BandedMatrix<Real>& BandedMatrix<Real>::operator= (const BandedMatrix& rkM)
00050 {
00051     Deallocate();
00052     m_iSize = rkM.m_iSize;
00053     m_iLBands = rkM.m_iLBands;
00054     m_iUBands = rkM.m_iUBands;
00055     Allocate();
00056 
00057     size_t uiSize = m_iSize*sizeof(Real);
00058     System::Memcpy(m_afDBand,uiSize,rkM.m_afDBand,uiSize);
00059 
00060     int i;
00061     for (i = 0; i < m_iLBands; i++)
00062     {
00063         uiSize = (m_iSize-1-i)*sizeof(Real);
00064         System::Memcpy(m_aafLBand[i],uiSize,rkM.m_aafLBand[i],uiSize);
00065     }
00066 
00067     for (i = 0; i < m_iUBands; i++)
00068     {
00069         uiSize = (m_iSize-1-i)*sizeof(Real);
00070         System::Memcpy(m_aafUBand[i],uiSize,rkM.m_aafUBand[i],uiSize);
00071     }
00072 
00073     return *this;
00074 }
00075 //----------------------------------------------------------------------------
00076 template <class Real>
00077 int BandedMatrix<Real>::GetSize () const
00078 {
00079     return m_iSize;
00080 }
00081 //----------------------------------------------------------------------------
00082 template <class Real>
00083 int BandedMatrix<Real>::GetLBands () const
00084 {
00085     return m_iLBands;
00086 }
00087 //----------------------------------------------------------------------------
00088 template <class Real>
00089 int BandedMatrix<Real>::GetUBands () const
00090 {
00091     return m_iUBands;
00092 }
00093 //----------------------------------------------------------------------------
00094 template <class Real>
00095 Real* BandedMatrix<Real>::GetDBand ()
00096 {
00097     return m_afDBand;
00098 }
00099 //----------------------------------------------------------------------------
00100 template <class Real>
00101 const Real* BandedMatrix<Real>::GetDBand () const
00102 {
00103     return m_afDBand;
00104 }
00105 //----------------------------------------------------------------------------
00106 template <class Real>
00107 int BandedMatrix<Real>::GetLBandMax (int i) const
00108 {
00109     assert(0 <= i && i < m_iLBands);
00110     return m_iSize-1-i;
00111 }
00112 //----------------------------------------------------------------------------
00113 template <class Real>
00114 Real* BandedMatrix<Real>::GetLBand (int i)
00115 {
00116     if ( m_aafLBand )
00117     {
00118         assert(0 <= i && i < m_iLBands);
00119         return m_aafLBand[i];
00120     }
00121     return 0;
00122 }
00123 //----------------------------------------------------------------------------
00124 template <class Real>
00125 const Real* BandedMatrix<Real>::GetLBand (int i) const
00126 {
00127     if (m_aafLBand)
00128     {
00129         assert(0 <= i && i < m_iLBands);
00130         return m_aafLBand[i];
00131     }
00132     return 0;
00133 }
00134 //----------------------------------------------------------------------------
00135 template <class Real>
00136 int BandedMatrix<Real>::GetUBandMax (int i) const
00137 {
00138     assert(0 <= i && i < m_iUBands);
00139     return m_iSize-1-i;
00140 }
00141 //----------------------------------------------------------------------------
00142 template <class Real>
00143 Real* BandedMatrix<Real>::GetUBand (int i)
00144 {
00145     if (m_aafUBand)
00146     {
00147         assert(0 <= i && i < m_iUBands);
00148         return m_aafUBand[i];
00149     }
00150     return 0;
00151 }
00152 //----------------------------------------------------------------------------
00153 template <class Real>
00154 const Real* BandedMatrix<Real>::GetUBand (int i) const
00155 {
00156     if (m_aafUBand)
00157     {
00158         assert(0 <= i && i < m_iUBands);
00159         return m_aafUBand[i];
00160     }
00161     return 0;
00162 }
00163 //----------------------------------------------------------------------------
00164 template <class Real>
00165 Real& BandedMatrix<Real>::operator() (int iRow, int iCol)
00166 {
00167     assert(0 <= iRow && iRow < m_iSize && 0 <= iCol && iCol < m_iSize);
00168 
00169     int iBand = iCol - iRow;
00170     if (iBand > 0)
00171     {
00172         if (--iBand < m_iUBands && iRow < m_iSize-1-iBand)
00173         {
00174             return m_aafUBand[iBand][iRow];
00175         }
00176     }
00177     else if ( iBand < 0 )
00178     {
00179         iBand = -iBand;
00180         if (--iBand < m_iLBands && iCol < m_iSize-1-iBand)
00181         {
00182             return m_aafLBand[iBand][iCol];
00183         }
00184     }
00185     else
00186     {
00187         return m_afDBand[iRow];
00188     }
00189 
00190     static Real s_fDummy = (Real)0.0;
00191     return s_fDummy;
00192 }
00193 //----------------------------------------------------------------------------
00194 template <class Real>
00195 Real BandedMatrix<Real>::operator() (int iRow, int iCol) const
00196 {
00197     assert(0 <= iRow && iRow < m_iSize && 0 <= iCol && iCol < m_iSize);
00198 
00199     int iBand = iCol - iRow;
00200     if (iBand > 0)
00201     {
00202         if (--iBand < m_iUBands && iRow < m_iSize-1-iBand)
00203         {
00204             return m_aafUBand[iBand][iRow];
00205         }
00206     }
00207     else if ( iBand < 0 )
00208     {
00209         iBand = -iBand;
00210         if (--iBand < m_iLBands && iCol < m_iSize-1-iBand)
00211         {
00212             return m_aafLBand[iBand][iCol];
00213         }
00214     }
00215     else
00216     {
00217         return m_afDBand[iRow];
00218     }
00219 
00220     return (Real)0.0;
00221 }
00222 //----------------------------------------------------------------------------
00223 template <class Real>
00224 void BandedMatrix<Real>::SetZero ()
00225 {
00226     assert(m_iSize > 0);
00227 
00228     memset(m_afDBand,0,m_iSize*sizeof(Real));
00229 
00230     int i;
00231     for (i = 0; i < m_iLBands; i++)
00232     {
00233         memset(m_aafLBand[i],0,(m_iSize-1-i)*sizeof(Real));
00234     }
00235 
00236     for (i = 0; i < m_iUBands; i++)
00237     {
00238         memset(m_aafUBand[i],0,(m_iSize-1-i)*sizeof(Real));
00239     }
00240 }
00241 //----------------------------------------------------------------------------
00242 template <class Real>
00243 void BandedMatrix<Real>::SetIdentity ()
00244 {
00245     assert(m_iSize > 0);
00246 
00247     int i;
00248     for (i = 0; i < m_iSize; i++)
00249     {
00250         m_afDBand[i] = (Real)1.0;
00251     }
00252 
00253     for (i = 0; i < m_iLBands; i++)
00254     {
00255         memset(m_aafLBand[i],0,(m_iSize-1-i)*sizeof(Real));
00256     }
00257 
00258     for (i = 0; i < m_iUBands; i++)
00259     {
00260         memset(m_aafUBand[i],0,(m_iSize-1-i)*sizeof(Real));
00261     }
00262 }
00263 //----------------------------------------------------------------------------
00264 template <class Real>
00265 void BandedMatrix<Real>::Allocate ()
00266 {
00267     // assert:  m_iSize, m_iLBands, m_iRBandQuantity already set
00268     // assert:  m_afDBand, m_aafLBand, m_aafUBand all null
00269 
00270     m_afDBand = WM4_NEW Real[m_iSize];
00271     memset(m_afDBand,0,m_iSize*sizeof(Real));
00272 
00273     if (m_iLBands > 0)
00274     {
00275         m_aafLBand = WM4_NEW Real*[m_iLBands];
00276     }
00277     else
00278     {
00279         m_aafLBand = 0;
00280     }
00281 
00282     if (m_iUBands > 0)
00283     {
00284         m_aafUBand = WM4_NEW Real*[m_iUBands];
00285     }
00286     else
00287     {
00288         m_aafUBand = 0;
00289     }
00290 
00291     int i;
00292     for (i = 0; i < m_iLBands; i++)
00293     {
00294         m_aafLBand[i] = WM4_NEW Real[m_iSize-1-i];
00295         memset(m_aafLBand[i],0,(m_iSize-1-i)*sizeof(Real));
00296     }
00297 
00298     for (i = 0; i < m_iUBands; i++)
00299     {
00300         m_aafUBand[i] = WM4_NEW Real[m_iSize-1-i];
00301         memset(m_aafUBand[i],0,(m_iSize-1-i)*sizeof(Real));
00302     }
00303 }
00304 //----------------------------------------------------------------------------
00305 template <class Real>
00306 void BandedMatrix<Real>::Deallocate ()
00307 {
00308     WM4_DELETE[] m_afDBand;
00309 
00310     int i;
00311 
00312     if (m_aafLBand)
00313     {
00314         for (i = 0; i < m_iLBands; i++)
00315         {
00316             WM4_DELETE[] m_aafLBand[i];
00317         }
00318 
00319         WM4_DELETE[] m_aafLBand;
00320         m_aafLBand = 0;
00321     }
00322 
00323     if (m_aafUBand)
00324     {
00325         for (i = 0; i < m_iUBands; i++)
00326         {
00327             WM4_DELETE[] m_aafUBand[i];
00328         }
00329 
00330         WM4_DELETE[] m_aafUBand;
00331         m_aafUBand = 0;
00332     }
00333 }
00334 //----------------------------------------------------------------------------
00335 } // namespace Wm4

Generated on Wed Nov 23 19:01:02 2011 for FreeCAD by  doxygen 1.6.1