]> Creatis software - creaMaracasVisu.git/blob - lib/maracasVisuLib/include/matrix.h
9689f5968abecca9da9951b55c0f06cd2c008790
[creaMaracasVisu.git] / lib / maracasVisuLib / include / matrix.h
1 ////////////////////////////////////////////////////////////////////////////////
2 // matrix.h
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
8 //
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.
13 //
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.
18 //
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 ////////////////////////////////////////////////////////////////////////////////
23
24 #ifndef GTMLIB__MATH__MATRIX__HXX
25 #define GTMLIB__MATH__MATRIX__HXX
26
27 #include "mathdefs.h"
28 #include "vmfuncs.h"
29 #include "vector.h"
30
31 namespace gtm
32 {
33     /** TMatrix class.
34      *
35      *  This class defines a C++ template to use mathematical matrices of NxM.
36      *  @see TVector
37      */
38     template< class T >
39     class TMatrix
40     {
41     public:
42         
43         /** Constructors.
44          *
45          *  @param N Columns.
46          *  @param M Rows.
47          *  @param data Initial data.
48          *  @param r Copy object (matrix or vector).
49          *  @param block Memory block.
50          */
51         //@{
52         /// Default constructor.
53         TMatrix( uint N = 3, uint M = 3, T data = ( T )0 );
54         /// Copy constructor.
55         TMatrix( const TMatrix< T >& r );
56         /// ANSI casting constructor.
57         TMatrix( T** block, uint N, uint M );
58         //@}
59
60         /// Destructor.
61         ~TMatrix( ) {
62             MatrixFreeMemory< T >( _matrix, _N );
63         };
64
65         /** Assignation operators.
66          *
67          *  @param r Right object (matrix, vector or scalar).
68          */
69         //@{
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 );
76         //@}
77         
78         /** Comparation operators.
79          *
80          *  @param Right matrix.
81          */
82         //@{
83         /// Equality.
84         bool operator==( const TMatrix< T >& r );
85         /// Inequality.
86         bool operator!=( const TMatrix< T >& r );
87         //@}
88
89         /// Reference operator.
90         T& operator()( uint i, uint j ) {
91             return( _matrix[ i ][ j ] );
92         };
93         /// Columns
94         uint GetN( ) {
95             return( _N );
96         };
97         /// Rows
98         uint GetM( ) {
99             return( _M );
100         };
101         /// Returns the ANSI (C/C++) reference.
102         T** GetAnsiRef( ) {
103             return( _matrix );
104         };
105         /// Determinant.
106         T Det( ) {
107             return( MatrixDeterminant< T >( _matrix, _N ) );
108         };
109         /// Loads identity.
110         void LoadIdentity( ) {
111             MatrixLoadIdentity< T >( _matrix, GTM_MIN( _N, _M ) );
112         };
113
114         /** Binary operators.
115          *
116          *  @param r Right objet (matrix, vector or scalar).
117          */
118         //@{
119         /// Addition.
120         TMatrix< T > operator+( const TMatrix< T >& r );
121         /// Substraction.
122         TMatrix< T > operator-( const TMatrix< T >& r );
123         /// Product.
124         TMatrix< T > operator*( const TMatrix< T >& r );
125         /// Product (vector).
126         TMatrix< T > operator*( TVector< T >& r );
127         /// Scalar product
128         TMatrix< T > operator*( T r );
129         //@}
130
131         /** Self-assignation binary operators.
132          *
133          *  @param r Right object (matrix, vector or scalar).
134          */
135         //@{
136         /// Addition.
137         TMatrix< T >& operator+=( const TMatrix< T >& r ) {
138             *this = *this + r;
139             return( *this );
140         };
141         /// Substraction.
142         TMatrix< T >& operator-=( const TMatrix< T >& r ) {
143             *this = *this - r;
144             return( *this );
145         };
146         /// Product.
147         TMatrix< T >& operator*=( const TMatrix< T >& r ) {
148             *this = *this * r;
149             return( *this );
150         };
151         /// Product (vector).
152         TMatrix< T >& operator*=( TVector< T >& r ) {
153             *this = *this * r;
154             return( *this );
155         };
156         /// Scalar product.
157         TMatrix< T >& operator*=( T r ) {
158             *this = *this * r;
159             return( *this );
160         };
161         //@}
162
163         /** Unary operators.
164          */
165         //@{
166         /// Additive inverse.
167         TMatrix< T > operator-( );
168         /// Matrix inverse.
169         TMatrix< T > operator!( );
170         /// Matrix transpose.
171         TMatrix< T > operator~( );
172         //@}
173
174     private:
175
176         /// Matrix' internal state.
177         //@{
178         /// Memory block.
179         T** _matrix;
180         /// Columns.
181         uint _N;
182         /// Rows.
183         uint _M;
184         //@}
185
186     };
187
188 // -----------------------------------------------------------------------------
189     template< class T >
190     TVector< T >& TVector< T >::operator=( TMatrix< T >& r )
191     {
192         uint i, j, k, min;
193
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++ );
199         return( *this );
200
201     }
202
203 // -----------------------------------------------------------------------------
204     template< class T >
205     TMatrix< T > TVector< T >::operator*( TMatrix< T >& r )
206     {
207         TMatrix< T > m = *this;
208         return( m * r );
209
210     }
211
212 // -----------------------------------------------------------------------------
213     template< class T >
214     TMatrix< T >::TMatrix( uint N, uint M, T data )
215     {
216         _N = N;
217         _M = M;
218         _matrix = MatrixAllocateMemory< T >( _N, _M );
219         MatrixAssignScalar< T >( _matrix, data, _N, _M );
220
221     }
222
223 // -----------------------------------------------------------------------------
224     template< class T >
225     TMatrix< T >::TMatrix( const TMatrix< T >& r )
226     {
227         _N = r._N;
228         _M = r._M;
229         _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
230
231     }
232
233 // -----------------------------------------------------------------------------
234     template< class T >
235     TMatrix< T >::TMatrix( T** block, uint N, uint M )
236     {
237         _N = N;
238         _M = M;
239         _matrix = MatrixCopyMemory< T >( block, N, M );
240
241     }
242
243 // -----------------------------------------------------------------------------
244     template< class T >
245     TMatrix< T >& TMatrix< T >::operator=( const TMatrix< T >& r )
246     {
247         if( _N != r._N || _M != r._M ) {
248
249             MatrixFreeMemory< T >( _matrix, _N );
250             _N = r._N;
251             _M = r._M;
252             _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
253
254         } else MatrixAssignMemory< T >( _matrix, r._matrix, _N, _M );
255         return( *this );
256   
257     }
258
259 // -----------------------------------------------------------------------------
260     template< class T >
261     TMatrix< T >& TMatrix< T >::operator=( TVector< T >& r )
262     {
263         uint i;
264         uint n = r.GetN( );
265         bool column = ( r.GetType( ) == COL_VECTOR );
266
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++ );
272         return( *this );
273   
274     }
275
276 // -----------------------------------------------------------------------------
277     template< class T >
278     TMatrix< T >& TMatrix< T >::operator=( T r )
279     {
280         MatrixAssignScalar< T >( _matrix, r, _N, _M );
281         return( *this );
282
283     }
284
285 // -----------------------------------------------------------------------------
286     template< class T >
287     bool TMatrix< T >::operator==( const TMatrix< T >& r )
288     {
289         uint i, j;
290         bool ret;
291
292         for(
293             i = 0, ret = ( _N == r._N && _M == r._M );
294             i < _N && ret;
295             i++
296             ) for(
297                 j = 0;
298                 j < _M && ret;
299                 ret &= ( _matrix[ i ][ j ] == r._matrix[ i ][ j ] ), j++
300                 );
301         return( ret );
302
303     }
304
305 // -----------------------------------------------------------------------------
306     template< class T >
307     bool TMatrix< T >::operator!=( const TMatrix< T >& r )
308     {
309         uint i, j;
310         bool ret;
311
312         for(
313             i = 0, ret = ( _N != r._N || _M != r._M );
314             i < _N && !ret;
315             i++
316             ) for(
317                 j = 0;
318                 j < _M && !ret;
319                 ret |= ( _matrix[ i ][ j ] != r._matrix[ i ][ j ] ), j++
320                 );
321         return( ret );
322
323     }
324
325 // -----------------------------------------------------------------------------
326     template< class T >
327     TMatrix< T > TMatrix< T >::operator+( const TMatrix< T >& r )
328     {
329         TMatrix< T > ret( _N, _M );
330
331         MatrixAdd< T >(
332             ret._matrix,
333             _matrix,
334             r._matrix,
335             GTM_MIN( _N, r._N ),
336             GTM_MIN( _M, r._M )
337             );
338         return( ret );
339
340     }
341
342 // -----------------------------------------------------------------------------
343     template< class T >
344     TMatrix< T > TMatrix< T >::operator-( const TMatrix< T >& r )
345     {
346         TMatrix< T > ret( _N, _M );
347   
348         MatrixSubtract< T >(
349             ret._matrix,
350             _matrix,
351             r._matrix,
352             GTM_MIN( _N, r._N ),
353             GTM_MIN( _M, r._M )
354             );
355         return( ret );
356
357     }
358
359 // -----------------------------------------------------------------------------
360     template< class T >
361     TMatrix< T > TMatrix< T >::operator*( const TMatrix< T >& r )
362     {
363         TMatrix< T > ret( r._N, _M );
364
365         MatrixProduct< T >( ret._matrix, _matrix, r._matrix, _N, _M, r._N );
366         return( ret );
367
368     }
369
370 // -----------------------------------------------------------------------------
371     template< class T >
372     TMatrix< T > TMatrix< T >::operator*( T r )
373     {
374         TMatrix< T > ret( _N, _M );
375
376         MatrixScalarProduct< T >( ret._matrix, _matrix, r, _N, _M );
377         return( ret );
378
379     }
380
381 // -----------------------------------------------------------------------------
382     template< class T >
383     TMatrix< T > TMatrix< T >::operator*( TVector< T >& r )
384     {
385         TMatrix< T > m;
386         m = r;
387         return( *this * m );
388
389     }
390
391 // -----------------------------------------------------------------------------
392     template< class T >
393     TMatrix< T > TMatrix< T >::operator-( )
394     {
395         TMatrix< T > ret( _N, _M );
396         uint i, j;
397   
398         for( i = 0; i < _N; i++ )
399             for( j = 0; j < _M; ret._matrix[ i ][ j ] = ( T )0 - _matrix[ i ][ j ], j++ );
400         return( ret );
401   
402     }
403
404 // -----------------------------------------------------------------------------
405     template< class T >
406     TMatrix< T > TMatrix< T >::operator!( )
407     {
408         TMatrix< T > ret( _N, _N );
409
410         if( _N <= 4 ) MatrixInverseAdjoint< T >( ret._matrix, _matrix, _N );
411         else          MatrixInverseGauss< T >( ret._matrix, _matrix, _N );
412         return( ret );
413
414     }
415
416 // -----------------------------------------------------------------------------
417     template< class T >
418     TMatrix< T > TMatrix< T >::operator~( )
419     {
420         TMatrix< T > ret( _M, _N );
421
422         MatrixTranspose< T >( ret._matrix, _matrix, _N, _M );
423         return( ret );
424
425     }
426
427 } // namespace
428
429 #endif // GTMLIB__MATH__MATRIX__HXX
430
431 // EOF - matrix.h