1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 ////////////////////////////////////////////////////////////////////////////////
28 // Creation : 19/03/2000
29 // Author : Leonardo FLOREZ VALENCIA
30 // l-florez@uniandes.edu.co
31 // lflorez@creatis.insa-lyon.fr
32 // Copyright (C) 2000-2002 Leonardo FLOREZ VALENCIA
34 // This program is free software; you can redistribute it and/or
35 // modify it under the terms of the GNU General Public License
36 // as published by the Free Software Foundation; either version 2
37 // of the License, or (at your option) any later version.
39 // This program is distributed in the hope that it will be useful,
40 // but WITHOUT ANY WARRANTY; without even the implied warranty of
41 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
42 // GNU General Public License for more details.
44 // You should have received a copy of the GNU General Public License
45 // along with this program; if not, write to the Free Software
46 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
47 ////////////////////////////////////////////////////////////////////////////////
49 #ifndef GTMLIB__MATH__MATRIX__HXX
50 #define GTMLIB__MATH__MATRIX__HXX
60 * This class defines a C++ template to use mathematical matrices of NxM.
72 * @param data Initial data.
73 * @param r Copy object (matrix or vector).
74 * @param block Memory block.
77 /// Default constructor.
78 TMatrix( uint32_t N = 3, uint32_t M = 3, T data = ( T )0 );
80 TMatrix( const TMatrix< T >& r );
81 /// ANSI casting constructor.
82 TMatrix( T** block, uint32_t N, uint32_t M );
87 MatrixFreeMemory< T >( _matrix, _N );
90 /** Assignation operators.
92 * @param r Right object (matrix, vector or scalar).
95 /// Natural assignation.
96 TMatrix< T >& operator=( const TMatrix< T >& r );
97 /// Vector assignation.
98 TMatrix< T >& operator=( TVector< T >& r );
99 /// Scalar assignation.
100 TMatrix< T >& operator=( T r );
103 /** Comparation operators.
105 * @param Right matrix.
109 bool operator==( const TMatrix< T >& r );
111 bool operator!=( const TMatrix< T >& r );
114 /// Reference operator.
115 T& operator()( uint32_t i, uint32_t j ) {
116 return( _matrix[ i ][ j ] );
126 /// Returns the ANSI (C/C++) reference.
132 return( MatrixDeterminant< T >( _matrix, _N ) );
135 void LoadIdentity( ) {
136 MatrixLoadIdentity< T >( _matrix, GTM_MIN( _N, _M ) );
139 /** Binary operators.
141 * @param r Right objet (matrix, vector or scalar).
145 TMatrix< T > operator+( const TMatrix< T >& r );
147 TMatrix< T > operator-( const TMatrix< T >& r );
149 TMatrix< T > operator*( const TMatrix< T >& r );
150 /// Product (vector).
151 TMatrix< T > operator*( TVector< T >& r );
153 TMatrix< T > operator*( T r );
156 /** Self-assignation binary operators.
158 * @param r Right object (matrix, vector or scalar).
162 TMatrix< T >& operator+=( const TMatrix< T >& r ) {
167 TMatrix< T >& operator-=( const TMatrix< T >& r ) {
172 TMatrix< T >& operator*=( const TMatrix< T >& r ) {
176 /// Product (vector).
177 TMatrix< T >& operator*=( TVector< T >& r ) {
182 TMatrix< T >& operator*=( T r ) {
191 /// Additive inverse.
192 TMatrix< T > operator-( );
194 TMatrix< T > operator!( );
195 /// Matrix transpose.
196 TMatrix< T > operator~( );
201 /// Matrix' internal state.
213 // -----------------------------------------------------------------------------
215 TVector< T >& TVector< T >::operator=( TMatrix< T >& r )
217 uint32_t i, j, k, min;
219 // This min calc. avoids to reserve temporary memory, so, be careful.
220 min = GTM_MIN( r.GetN( ) * r.GetM( ), _N );
221 _type = ( r.GetN( ) == 1 )? COL_VECTOR: ROW_VECTOR;
222 for( i = 0, k = 0; i < r.GetN( ) && k < min; i++ )
223 for( j = 0; j < r.GetM( ) && k < min; _vector[ k++ ] = r( i, j ), j++ );
228 // -----------------------------------------------------------------------------
230 TMatrix< T > TVector< T >::operator*( TMatrix< T >& r )
232 TMatrix< T > m = *this;
237 // -----------------------------------------------------------------------------
239 TMatrix< T >::TMatrix( uint32_t N, uint32_t M, T data )
243 _matrix = MatrixAllocateMemory< T >( _N, _M );
244 MatrixAssignScalar< T >( _matrix, data, _N, _M );
248 // -----------------------------------------------------------------------------
250 TMatrix< T >::TMatrix( const TMatrix< T >& r )
254 _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
258 // -----------------------------------------------------------------------------
260 TMatrix< T >::TMatrix( T** block, uint32_t N, uint32_t M )
264 _matrix = MatrixCopyMemory< T >( block, N, M );
268 // -----------------------------------------------------------------------------
270 TMatrix< T >& TMatrix< T >::operator=( const TMatrix< T >& r )
272 if( _N != r._N || _M != r._M ) {
274 MatrixFreeMemory< T >( _matrix, _N );
277 _matrix = MatrixCopyMemory< T >( r._matrix, _N, _M );
279 } else MatrixAssignMemory< T >( _matrix, r._matrix, _N, _M );
284 // -----------------------------------------------------------------------------
286 TMatrix< T >& TMatrix< T >::operator=( TVector< T >& r )
289 uint32_t n = r.GetN( );
290 bool column = ( r.GetType( ) == COL_VECTOR );
292 MatrixFreeMemory< T >( _matrix, _N );
293 _N = ( column )? 1: n;
294 _M = ( column )? n: 1;
295 _matrix = MatrixAllocateMemory< T >( _N, _M );
296 for( i = 0; i < n; _matrix[ ( column )? 0: i ][ ( column )? i: 0 ] = r( i ), i++ );
301 // -----------------------------------------------------------------------------
303 TMatrix< T >& TMatrix< T >::operator=( T r )
305 MatrixAssignScalar< T >( _matrix, r, _N, _M );
310 // -----------------------------------------------------------------------------
312 bool TMatrix< T >::operator==( const TMatrix< T >& r )
318 i = 0, ret = ( _N == r._N && _M == r._M );
324 ret &= ( _matrix[ i ][ j ] == r._matrix[ i ][ j ] ), j++
330 // -----------------------------------------------------------------------------
332 bool TMatrix< T >::operator!=( const TMatrix< T >& r )
338 i = 0, ret = ( _N != r._N || _M != r._M );
344 ret |= ( _matrix[ i ][ j ] != r._matrix[ i ][ j ] ), j++
350 // -----------------------------------------------------------------------------
352 TMatrix< T > TMatrix< T >::operator+( const TMatrix< T >& r )
354 TMatrix< T > ret( _N, _M );
367 // -----------------------------------------------------------------------------
369 TMatrix< T > TMatrix< T >::operator-( const TMatrix< T >& r )
371 TMatrix< T > ret( _N, _M );
384 // -----------------------------------------------------------------------------
386 TMatrix< T > TMatrix< T >::operator*( const TMatrix< T >& r )
388 TMatrix< T > ret( r._N, _M );
390 MatrixProduct< T >( ret._matrix, _matrix, r._matrix, _N, _M, r._N );
395 // -----------------------------------------------------------------------------
397 TMatrix< T > TMatrix< T >::operator*( T r )
399 TMatrix< T > ret( _N, _M );
401 MatrixScalarProduct< T >( ret._matrix, _matrix, r, _N, _M );
406 // -----------------------------------------------------------------------------
408 TMatrix< T > TMatrix< T >::operator*( TVector< T >& r )
416 // -----------------------------------------------------------------------------
418 TMatrix< T > TMatrix< T >::operator-( )
420 TMatrix< T > ret( _N, _M );
423 for( i = 0; i < _N; i++ )
424 for( j = 0; j < _M; ret._matrix[ i ][ j ] = ( T )0 - _matrix[ i ][ j ], j++ );
429 // -----------------------------------------------------------------------------
431 TMatrix< T > TMatrix< T >::operator!( )
433 TMatrix< T > ret( _N, _N );
435 if( _N <= 4 ) MatrixInverseAdjoint< T >( ret._matrix, _matrix, _N );
436 else MatrixInverseGauss< T >( ret._matrix, _matrix, _N );
441 // -----------------------------------------------------------------------------
443 TMatrix< T > TMatrix< T >::operator~( )
445 TMatrix< T > ret( _M, _N );
447 MatrixTranspose< T >( ret._matrix, _matrix, _N, _M );
454 #endif // GTMLIB__MATH__MATRIX__HXX