1 ////////////////////////////////////////////////////////////////////////////////
3 // Creation : 19/03/2000
4 // Author : Leonardo FLOREZ VALENCIA
5 // l-florez@uniandes.edu.co
6 // lflorez@creatis.insa-lyon.fr
7 // Copyright (C) 2000-2002 Leonardo FLOREZ VALENCIA
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License
11 // as published by the Free Software Foundation; either version 2
12 // of the License, or (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ////////////////////////////////////////////////////////////////////////////////
24 #ifndef GTMLIB__MATH__MATRIX__HXX
25 #define GTMLIB__MATH__MATRIX__HXX
35 * This class defines a C++ template to use mathematical matrices of NxM.
47 * @param data Initial data.
48 * @param r Copy object (matrix or vector).
49 * @param block Memory block.
52 /// Default constructor.
53 TMatrix( uint N = 3, uint M = 3, T data = ( T )0 );
55 TMatrix( const TMatrix< T >& r );
56 /// ANSI casting constructor.
57 TMatrix( T** block, uint N, uint M );
62 MatrixFreeMemory< T >( _matrix, _N );
65 /** Assignation operators.
67 * @param r Right object (matrix, vector or scalar).
70 /// Natural assignation.
71 TMatrix< T >& operator=( const TMatrix< T >& r );
72 /// Vector assignation.
73 TMatrix< T >& operator=( TVector< T >& r );
74 /// Scalar assignation.
75 TMatrix< T >& operator=( T r );
78 /** Comparation operators.
80 * @param Right matrix.
84 bool operator==( const TMatrix< T >& r );
86 bool operator!=( const TMatrix< T >& r );
89 /// Reference operator.
90 T& operator()( uint i, uint j ) {
91 return( _matrix[ i ][ j ] );
101 /// Returns the ANSI (C/C++) reference.
107 return( MatrixDeterminant< T >( _matrix, _N ) );
110 void LoadIdentity( ) {
111 MatrixLoadIdentity< T >( _matrix, GTM_MIN( _N, _M ) );
114 /** Binary operators.
116 * @param r Right objet (matrix, vector or scalar).
120 TMatrix< T > operator+( const TMatrix< T >& r );
122 TMatrix< T > operator-( const TMatrix< T >& r );
124 TMatrix< T > operator*( const TMatrix< T >& r );
125 /// Product (vector).
126 TMatrix< T > operator*( TVector< T >& r );
128 TMatrix< T > operator*( T r );
131 /** Self-assignation binary operators.
133 * @param r Right object (matrix, vector or scalar).
137 TMatrix< T >& operator+=( const TMatrix< T >& r ) {
142 TMatrix< T >& operator-=( const TMatrix< T >& r ) {
147 TMatrix< T >& operator*=( const TMatrix< T >& r ) {
151 /// Product (vector).
152 TMatrix< T >& operator*=( TVector< T >& r ) {
157 TMatrix< T >& operator*=( T r ) {
166 /// Additive inverse.
167 TMatrix< T > operator-( );
169 TMatrix< T > operator!( );
170 /// Matrix transpose.
171 TMatrix< T > operator~( );
176 /// Matrix' internal state.
188 // -----------------------------------------------------------------------------
190 TVector< T >& TVector< T >::operator=( TMatrix< T >& r )
194 // This min calc. avoids to reserve temporary memory, so, be careful.
195 min = GTM_MIN( r.GetN( ) * r.GetM( ), _N );
196 _type = ( r.GetN( ) == 1 )? COL_VECTOR: ROW_VECTOR;
197 for( i = 0, k = 0; i < r.GetN( ) && k < min; i++ )
198 for( j = 0; j < r.GetM( ) && k < min; _vector[ k++ ] = r( i, j ), j++ );
203 // -----------------------------------------------------------------------------
205 TMatrix< T > TVector< T >::operator*( TMatrix< T >& r )
207 TMatrix< T > m = *this;
212 // -----------------------------------------------------------------------------
214 TMatrix< T >::TMatrix( uint N, uint M, T data )
218 _matrix = MatrixAllocateMemory< T >( _N, _M );
219 MatrixAssignScalar< T >( _matrix, data, _N, _M );
223 // -----------------------------------------------------------------------------
225 TMatrix< T >::TMatrix( const TMatrix< T >& r )
229 _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
233 // -----------------------------------------------------------------------------
235 TMatrix< T >::TMatrix( T** block, uint N, uint M )
239 _matrix = MatrixCopyMemory< T >( block, N, M );
243 // -----------------------------------------------------------------------------
245 TMatrix< T >& TMatrix< T >::operator=( const TMatrix< T >& r )
247 if( _N != r._N || _M != r._M ) {
249 MatrixFreeMemory< T >( _matrix, _N );
252 _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
254 } else MatrixAssignMemory< T >( _matrix, r._matrix, _N, _M );
259 // -----------------------------------------------------------------------------
261 TMatrix< T >& TMatrix< T >::operator=( TVector< T >& r )
265 bool column = ( r.GetType( ) == COL_VECTOR );
267 MatrixFreeMemory< T >( _matrix, _N );
268 _N = ( column )? 1: n;
269 _M = ( column )? n: 1;
270 _matrix = MatrixAllocateMemory< T >( _N, _M );
271 for( i = 0; i < n; _matrix[ ( column )? 0: i ][ ( column )? i: 0 ] = r( i ), i++ );
276 // -----------------------------------------------------------------------------
278 TMatrix< T >& TMatrix< T >::operator=( T r )
280 MatrixAssignScalar< T >( _matrix, r, _N, _M );
285 // -----------------------------------------------------------------------------
287 bool TMatrix< T >::operator==( const TMatrix< T >& r )
293 i = 0, ret = ( _N == r._N && _M == r._M );
299 ret &= ( _matrix[ i ][ j ] == r._matrix[ i ][ j ] ), j++
305 // -----------------------------------------------------------------------------
307 bool TMatrix< T >::operator!=( const TMatrix< T >& r )
313 i = 0, ret = ( _N != r._N || _M != r._M );
319 ret |= ( _matrix[ i ][ j ] != r._matrix[ i ][ j ] ), j++
325 // -----------------------------------------------------------------------------
327 TMatrix< T > TMatrix< T >::operator+( const TMatrix< T >& r )
329 TMatrix< T > ret( _N, _M );
342 // -----------------------------------------------------------------------------
344 TMatrix< T > TMatrix< T >::operator-( const TMatrix< T >& r )
346 TMatrix< T > ret( _N, _M );
359 // -----------------------------------------------------------------------------
361 TMatrix< T > TMatrix< T >::operator*( const TMatrix< T >& r )
363 TMatrix< T > ret( r._N, _M );
365 MatrixProduct< T >( ret._matrix, _matrix, r._matrix, _N, _M, r._N );
370 // -----------------------------------------------------------------------------
372 TMatrix< T > TMatrix< T >::operator*( T r )
374 TMatrix< T > ret( _N, _M );
376 MatrixScalarProduct< T >( ret._matrix, _matrix, r, _N, _M );
381 // -----------------------------------------------------------------------------
383 TMatrix< T > TMatrix< T >::operator*( TVector< T >& r )
391 // -----------------------------------------------------------------------------
393 TMatrix< T > TMatrix< T >::operator-( )
395 TMatrix< T > ret( _N, _M );
398 for( i = 0; i < _N; i++ )
399 for( j = 0; j < _M; ret._matrix[ i ][ j ] = ( T )0 - _matrix[ i ][ j ], j++ );
404 // -----------------------------------------------------------------------------
406 TMatrix< T > TMatrix< T >::operator!( )
408 TMatrix< T > ret( _N, _N );
410 if( _N <= 4 ) MatrixInverseAdjoint< T >( ret._matrix, _matrix, _N );
411 else MatrixInverseGauss< T >( ret._matrix, _matrix, _N );
416 // -----------------------------------------------------------------------------
418 TMatrix< T > TMatrix< T >::operator~( )
420 TMatrix< T > ret( _M, _N );
422 MatrixTranspose< T >( ret._matrix, _matrix, _N, _M );
429 #endif // GTMLIB__MATH__MATRIX__HXX