matrix.h

Go to the documentation of this file.
00001 
00002 // matrix.h
00003 // Creation : 19/03/2000
00004 // Author   : Leonardo FLOREZ VALENCIA
00005 //               l-florez@uniandes.edu.co
00006 //               lflorez@creatis.insa-lyon.fr
00007 // Copyright (C) 2000-2002 Leonardo FLOREZ VALENCIA
00008 //
00009 // This program is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU General Public License
00011 // as published by the Free Software Foundation; either version 2
00012 // of the License, or (at your option) any later version.
00013 //
00014 // This program is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with this program; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 #ifndef GTMLIB__MATH__MATRIX__HXX
00025 #define GTMLIB__MATH__MATRIX__HXX
00026 
00027 #include "mathdefs.h"
00028 #include "vmfuncs.h"
00029 #include "vector.h"
00030 
00031 namespace gtm
00032 {
00038     template< class T >
00039     class TMatrix
00040     {
00041     public:
00042         
00052 
00053         TMatrix( uint N = 3, uint M = 3, T data = ( T )0 );
00055         TMatrix( const TMatrix< T >& r );
00057         TMatrix( T** block, uint N, uint M );
00059 
00061         ~TMatrix( ) {
00062             MatrixFreeMemory< T >( _matrix, _N );
00063         };
00064 
00070 
00071         TMatrix< T >& operator=( const TMatrix< T >& r );
00073         TMatrix< T >& operator=( TVector< T >& r );
00075         TMatrix< T >& operator=( T r );
00077         
00083 
00084         bool operator==( const TMatrix< T >& r );
00086         bool operator!=( const TMatrix< T >& r );
00088 
00090         T& operator()( uint i, uint j ) {
00091             return( _matrix[ i ][ j ] );
00092         };
00094         uint GetN( ) {
00095             return( _N );
00096         };
00098         uint GetM( ) {
00099             return( _M );
00100         };
00102         T** GetAnsiRef( ) {
00103             return( _matrix );
00104         };
00106         T Det( ) {
00107             return( MatrixDeterminant< T >( _matrix, _N ) );
00108         };
00110         void LoadIdentity( ) {
00111             MatrixLoadIdentity< T >( _matrix, GTM_MIN( _N, _M ) );
00112         };
00113 
00119 
00120         TMatrix< T > operator+( const TMatrix< T >& r );
00122         TMatrix< T > operator-( const TMatrix< T >& r );
00124         TMatrix< T > operator*( const TMatrix< T >& r );
00126         TMatrix< T > operator*( TVector< T >& r );
00128         TMatrix< T > operator*( T r );
00130 
00136 
00137         TMatrix< T >& operator+=( const TMatrix< T >& r ) {
00138             *this = *this + r;
00139             return( *this );
00140         };
00142         TMatrix< T >& operator-=( const TMatrix< T >& r ) {
00143             *this = *this - r;
00144             return( *this );
00145         };
00147         TMatrix< T >& operator*=( const TMatrix< T >& r ) {
00148             *this = *this * r;
00149             return( *this );
00150         };
00152         TMatrix< T >& operator*=( TVector< T >& r ) {
00153             *this = *this * r;
00154             return( *this );
00155         };
00157         TMatrix< T >& operator*=( T r ) {
00158             *this = *this * r;
00159             return( *this );
00160         };
00162 
00166 
00167         TMatrix< T > operator-( );
00169         TMatrix< T > operator!( );
00171         TMatrix< T > operator~( );
00173 
00174     private:
00175 
00177 
00178 
00179         T** _matrix;
00181         uint _N;
00183         uint _M;
00185 
00186     };
00187 
00188 // -----------------------------------------------------------------------------
00189     template< class T >
00190     TVector< T >& TVector< T >::operator=( TMatrix< T >& r )
00191     {
00192         uint i, j, k, min;
00193 
00194         // This min calc. avoids to reserve temporary memory, so, be careful.
00195         min = GTM_MIN( r.GetN( ) * r.GetM( ), _N );
00196         _type = ( r.GetN( ) == 1 )? COL_VECTOR: ROW_VECTOR;
00197         for( i = 0, k = 0; i < r.GetN( ) && k < min; i++ )
00198             for( j = 0; j < r.GetM( ) && k < min; _vector[ k++ ] = r( i, j ), j++ );
00199         return( *this );
00200 
00201     }
00202 
00203 // -----------------------------------------------------------------------------
00204     template< class T >
00205     TMatrix< T > TVector< T >::operator*( TMatrix< T >& r )
00206     {
00207         TMatrix< T > m = *this;
00208         return( m * r );
00209 
00210     }
00211 
00212 // -----------------------------------------------------------------------------
00213     template< class T >
00214     TMatrix< T >::TMatrix( uint N, uint M, T data )
00215     {
00216         _N = N;
00217         _M = M;
00218         _matrix = MatrixAllocateMemory< T >( _N, _M );
00219         MatrixAssignScalar< T >( _matrix, data, _N, _M );
00220 
00221     }
00222 
00223 // -----------------------------------------------------------------------------
00224     template< class T >
00225     TMatrix< T >::TMatrix( const TMatrix< T >& r )
00226     {
00227         _N = r._N;
00228         _M = r._M;
00229         _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
00230 
00231     }
00232 
00233 // -----------------------------------------------------------------------------
00234     template< class T >
00235     TMatrix< T >::TMatrix( T** block, uint N, uint M )
00236     {
00237         _N = N;
00238         _M = M;
00239         _matrix = MatrixCopyMemory< T >( block, N, M );
00240 
00241     }
00242 
00243 // -----------------------------------------------------------------------------
00244     template< class T >
00245     TMatrix< T >& TMatrix< T >::operator=( const TMatrix< T >& r )
00246     {
00247         if( _N != r._N || _M != r._M ) {
00248 
00249             MatrixFreeMemory< T >( _matrix, _N );
00250             _N = r._N;
00251             _M = r._M;
00252             _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
00253 
00254         } else MatrixAssignMemory< T >( _matrix, r._matrix, _N, _M );
00255         return( *this );
00256   
00257     }
00258 
00259 // -----------------------------------------------------------------------------
00260     template< class T >
00261     TMatrix< T >& TMatrix< T >::operator=( TVector< T >& r )
00262     {
00263         uint i;
00264         uint n = r.GetN( );
00265         bool column = ( r.GetType( ) == COL_VECTOR );
00266 
00267         MatrixFreeMemory< T >( _matrix, _N );
00268         _N = ( column )? 1: n;
00269         _M = ( column )? n: 1;
00270         _matrix = MatrixAllocateMemory< T >( _N, _M );
00271         for( i = 0; i < n; _matrix[ ( column )? 0: i ][ ( column )? i: 0 ] = r( i ), i++ );
00272         return( *this );
00273   
00274     }
00275 
00276 // -----------------------------------------------------------------------------
00277     template< class T >
00278     TMatrix< T >& TMatrix< T >::operator=( T r )
00279     {
00280         MatrixAssignScalar< T >( _matrix, r, _N, _M );
00281         return( *this );
00282 
00283     }
00284 
00285 // -----------------------------------------------------------------------------
00286     template< class T >
00287     bool TMatrix< T >::operator==( const TMatrix< T >& r )
00288     {
00289         uint i, j;
00290         bool ret;
00291 
00292         for(
00293             i = 0, ret = ( _N == r._N && _M == r._M );
00294             i < _N && ret;
00295             i++
00296             ) for(
00297                 j = 0;
00298                 j < _M && ret;
00299                 ret &= ( _matrix[ i ][ j ] == r._matrix[ i ][ j ] ), j++
00300                 );
00301         return( ret );
00302 
00303     }
00304 
00305 // -----------------------------------------------------------------------------
00306     template< class T >
00307     bool TMatrix< T >::operator!=( const TMatrix< T >& r )
00308     {
00309         uint i, j;
00310         bool ret;
00311 
00312         for(
00313             i = 0, ret = ( _N != r._N || _M != r._M );
00314             i < _N && !ret;
00315             i++
00316             ) for(
00317                 j = 0;
00318                 j < _M && !ret;
00319                 ret |= ( _matrix[ i ][ j ] != r._matrix[ i ][ j ] ), j++
00320                 );
00321         return( ret );
00322 
00323     }
00324 
00325 // -----------------------------------------------------------------------------
00326     template< class T >
00327     TMatrix< T > TMatrix< T >::operator+( const TMatrix< T >& r )
00328     {
00329         TMatrix< T > ret( _N, _M );
00330 
00331         MatrixAdd< T >(
00332             ret._matrix,
00333             _matrix,
00334             r._matrix,
00335             GTM_MIN( _N, r._N ),
00336             GTM_MIN( _M, r._M )
00337             );
00338         return( ret );
00339 
00340     }
00341 
00342 // -----------------------------------------------------------------------------
00343     template< class T >
00344     TMatrix< T > TMatrix< T >::operator-( const TMatrix< T >& r )
00345     {
00346         TMatrix< T > ret( _N, _M );
00347   
00348         MatrixSubtract< T >(
00349             ret._matrix,
00350             _matrix,
00351             r._matrix,
00352             GTM_MIN( _N, r._N ),
00353             GTM_MIN( _M, r._M )
00354             );
00355         return( ret );
00356 
00357     }
00358 
00359 // -----------------------------------------------------------------------------
00360     template< class T >
00361     TMatrix< T > TMatrix< T >::operator*( const TMatrix< T >& r )
00362     {
00363         TMatrix< T > ret( r._N, _M );
00364 
00365         MatrixProduct< T >( ret._matrix, _matrix, r._matrix, _N, _M, r._N );
00366         return( ret );
00367 
00368     }
00369 
00370 // -----------------------------------------------------------------------------
00371     template< class T >
00372     TMatrix< T > TMatrix< T >::operator*( T r )
00373     {
00374         TMatrix< T > ret( _N, _M );
00375 
00376         MatrixScalarProduct< T >( ret._matrix, _matrix, r, _N, _M );
00377         return( ret );
00378 
00379     }
00380 
00381 // -----------------------------------------------------------------------------
00382     template< class T >
00383     TMatrix< T > TMatrix< T >::operator*( TVector< T >& r )
00384     {
00385         TMatrix< T > m;
00386         m = r;
00387         return( *this * m );
00388 
00389     }
00390 
00391 // -----------------------------------------------------------------------------
00392     template< class T >
00393     TMatrix< T > TMatrix< T >::operator-( )
00394     {
00395         TMatrix< T > ret( _N, _M );
00396         uint i, j;
00397   
00398         for( i = 0; i < _N; i++ )
00399             for( j = 0; j < _M; ret._matrix[ i ][ j ] = ( T )0 - _matrix[ i ][ j ], j++ );
00400         return( ret );
00401   
00402     }
00403 
00404 // -----------------------------------------------------------------------------
00405     template< class T >
00406     TMatrix< T > TMatrix< T >::operator!( )
00407     {
00408         TMatrix< T > ret( _N, _N );
00409 
00410         if( _N <= 4 ) MatrixInverseAdjoint< T >( ret._matrix, _matrix, _N );
00411         else          MatrixInverseGauss< T >( ret._matrix, _matrix, _N );
00412         return( ret );
00413 
00414     }
00415 
00416 // -----------------------------------------------------------------------------
00417     template< class T >
00418     TMatrix< T > TMatrix< T >::operator~( )
00419     {
00420         TMatrix< T > ret( _M, _N );
00421 
00422         MatrixTranspose< T >( ret._matrix, _matrix, _N, _M );
00423         return( ret );
00424 
00425     }
00426 
00427 } // namespace
00428 
00429 #endif // GTMLIB__MATH__MATRIX__HXX
00430 
00431 // EOF - matrix.h

Generated on Wed Jul 29 16:35:25 2009 for creaMaracasVisu_lib by  doxygen 1.5.3