gtm Namespace Reference

Classes

class  TMatrix
class  TVector

Functions

double round (double n)



template<class T >
void VectorPrint (std::ostream &o, T *v, uint32_t n, bool col)
 Functions for vectors.
template<class T >
T * VectorAllocateMemory (uint32_t n)
template<class T >
void VectorFreeMemory (T *v)
template<class T >
void VectorAssignMemory (T *v, T *v_o, uint32_t n)
template<class T >
T * VectorCopyMemory (T *v, uint32_t n)
template<class T >
void VectorAssignScalar (T *v, T s, uint32_t n)
template<class T >
void VectorMatrixCast (T **ma, T *v, uint32_t n, uint32_t m, bool col)
template<class T >
void VectorAdd (T *v, T *v_l, T *v_r, uint32_t n)
template<class T >
void VectorSubtract (T *v, T *v_l, T *v_r, uint32_t n)
template<class T >
void VectorCrossProduct (T *v, T *v_l, T *v_r)
template<class T >
VectorDotProduct (T *v_l, T *v_r, uint32_t n)
template<class T >
void VectorScalarProduct (T *v, T *v_l, T s, uint32_t n)
template<class T >
VectorNorm (T *v, uint32_t n)
template<class T >
void VectorNormalize (T *v, T *v_o, uint32_t n)
template<class T >
void MatrixPrint (std::ostream &o, T **ma, uint32_t n, uint32_t m)
 Functions for matrices.
template<class T >
T ** MatrixAllocateMemory (uint32_t n, uint32_t m)
template<class T >
void MatrixAssignMemory (T **ma, T **ma_o, uint32_t n, uint32_t m)
template<class T >
T ** MatrixCopyMemory (T **ma, uint32_t n, uint32_t m)
template<class T >
void MatrixAssignScalar (T **ma, T s, uint32_t n, uint32_t m)
template<class T >
void MatrixVectorCast (T *v, T **ma, uint32_t n, uint32_t m, bool col)
template<class T >
void MatrixFreeMemory (T **ma, uint32_t n)
template<class T >
void MatrixAdd (T **ma, T **ma_l, T **ma_r, uint32_t n, uint32_t m)
template<class T >
void MatrixSubtract (T **ma, T **ma_l, T **ma_r, uint32_t n, uint32_t m)
template<class T >
void MatrixProduct (T **ma, T **ma_l, T **ma_r, uint32_t n, uint32_t m, uint32_t nr)
template<class T >
void MatrixScalarProduct (T **ma, T **ma_l, T s, uint32_t n, uint32_t m)
template<class T >
void MatrixCofactor (T **ma, T **ma_o, uint32_t i, uint32_t j, uint32_t n, uint32_t m)
template<class T >
MatrixDeterminant (T **ma, uint32_t n)
template<class T >
void MatrixInverseAdjoint (T **ma, T **ma_o, uint32_t n)
template<class T >
void MatrixInverseGauss (T **ma, T **ma_o, uint32_t n)
template<class T >
void MatrixTranspose (T **ma, T **ma_o, uint32_t n, uint32_t m)
template<class T >
void MatrixLoadIdentity (T **ma, uint32_t n)

Function Documentation

template<class T >
void gtm::MatrixAdd ( T **  ma,
T **  ma_l,
T **  ma_r,
uint32_t  n,
uint32_t  m 
) [inline]

Performs a matricial addition.

Parameters:
ma Result.
ma_l Left operand.
ma_r Right operand.
n Columns.
m Rows.

Definition at line 451 of file vmfuncs.h.

00452     {
00453         uint32_t i, j;
00454 
00455         for( i = 0; i < n; i++ )
00456             for( j = 0; j < m; ma[ i ][ j ] = ma_l[ i ][ j ] + ma_r[ i ][ j ], j++ );
00457         
00458     }

template<class T >
T** gtm::MatrixAllocateMemory ( uint32_t  n,
uint32_t  m 
) [inline]

Allocates memory for a matrix.

Parameters:
n Columns.
m Rows.

Definition at line 327 of file vmfuncs.h.

00328     {
00329         uint32_t i;
00330         T** ma = new T*[ n ];
00331 
00332         for( i = 0; i < n; i++ ) {
00333 
00334             ma[ i ] = new T[ m ];
00335             memset( ma[ i ], 0, sizeof( T ) * m );
00336 
00337         } // rof
00338         return( ma );
00339 
00340     }

template<class T >
void gtm::MatrixAssignMemory ( T **  ma,
T **  ma_o,
uint32_t  n,
uint32_t  m 
) [inline]

Copies.

Parameters:
ma Target matrix.
ma_o Source matrix.
n Columns.
m Rows.

Definition at line 352 of file vmfuncs.h.

00353     {
00354         uint32_t i;
00355 
00356         for( i = 0; i < n; i++ )
00357             memcpy( ma[ i ], ma_o[ i ], sizeof( T ) * m );
00358 
00359     }

template<class T >
void gtm::MatrixAssignScalar ( T **  ma,
s,
uint32_t  n,
uint32_t  m 
) [inline]

Scalar assignation.

Parameters:
ma Matrix.
s Scalar.
n Columns.
m Rows.

Definition at line 388 of file vmfuncs.h.

00389     {
00390         uint32_t i, j;
00391 
00392         for( i = 0; i < n; i++ )
00393             for( j = 0; j < m; j++ )
00394                 ma[ i ][ j ] = s;
00395 
00396     }

template<class T >
void gtm::MatrixCofactor ( T **  ma,
T **  ma_o,
uint32_t  i,
uint32_t  j,
uint32_t  n,
uint32_t  m 
) [inline]

Gets the matrix cofactor.

Parameters:
ma Result.
ma_o Matrix.
i Column index.
j Row index.
n Columns.
m Rows.

Definition at line 538 of file vmfuncs.h.

00539     {
00540         uint32_t k, l;
00541         
00542         for( k = 0; k < i; k++ ) {
00543             
00544             for( l = 0;     l < j; l++ ) ma[ k ][ l ]     = ma_o[ k ][ l ];
00545             for( l = j + 1; l < m; l++ ) ma[ k ][ l - 1 ] = ma_o[ k ][ l ];
00546             
00547         } // rof
00548         
00549         for( k = i + 1; k < n; k++ ) {
00550             
00551             for( l = 0;     l < j; l++ ) ma[ k - 1 ][ l ]     = ma_o[ k ][ l ];
00552             for( l = j + 1; l < m; l++ ) ma[ k - 1 ][ l - 1 ] = ma_o[ k ][ l ];
00553             
00554         } // rof
00555         
00556     }

template<class T >
T** gtm::MatrixCopyMemory ( T **  ma,
uint32_t  n,
uint32_t  m 
) [inline]

Allocates & copies.

Parameters:
ma Matrix.
n Columns.
m Rows.

Definition at line 370 of file vmfuncs.h.

00371     {
00372         T** n_ma = MatrixAllocateMemory< T >( n, m );
00373         MatrixAssignMemory< T >( n_ma, ma, n, m );
00374         return( n_ma );
00375         
00376     }

template<class T >
T gtm::MatrixDeterminant ( T **  ma,
uint32_t  n 
) [inline]

Gets the matrix determinant (square matrices only).

Parameters:
ma Matrix.
n Dimension.

Definition at line 566 of file vmfuncs.h.

00567     {
00568         uint32_t k;
00569         T** tmp = NULL;
00570         T ret = ( T )0, c;
00571         
00572         //  All these cases are for speed-up calculations.
00573         if( n == 1 ) {
00574 
00575             ret = ma[ 0 ][ 0 ];
00576 
00577         } else if( n == 2 ) {
00578 
00579             ret =
00580                 ( ma[ 0 ][ 0 ] * ma[ 1 ][ 1 ] ) -
00581                 ( ma[ 1 ][ 0 ] * ma[ 0 ][ 1 ] );
00582 
00583         } else if( n == 3 ) {
00584 
00585             ret =
00586                 ( ma[ 0 ][ 0 ] * ma[ 1 ][ 1 ] * ma[ 2 ][ 2 ] ) -
00587                 ( ma[ 0 ][ 0 ] * ma[ 2 ][ 1 ] * ma[ 1 ][ 2 ] ) -
00588                 ( ma[ 1 ][ 0 ] * ma[ 0 ][ 1 ] * ma[ 2 ][ 2 ] ) +
00589                 ( ma[ 1 ][ 0 ] * ma[ 2 ][ 1 ] * ma[ 0 ][ 2 ] ) +
00590                 ( ma[ 2 ][ 0 ] * ma[ 0 ][ 1 ] * ma[ 1 ][ 2 ] ) -
00591                 ( ma[ 2 ][ 0 ] * ma[ 1 ][ 1 ] * ma[ 0 ][ 2 ] );
00592 
00593         } else {
00594             
00595             tmp = MatrixAllocateMemory< T >( n - 1, n - 1 );
00596             for( k = 0, c = ( T )1, ret = ( T )0; k < n; k++ ) {
00597 
00598                 MatrixCofactor< T >( tmp, ma, k, 0, n, n );
00599                 ret = ret + ( c * ( ma[ k ][ 0 ] * MatrixDeterminant< T >( tmp, n - 1 ) ) );
00600                 c = c * ( T )( 0 - 1 );
00601 
00602             } // rof
00603             MatrixFreeMemory< T >( tmp, n - 1 );
00604 
00605         } // fi
00606         return( ret );
00607 
00608     }

template<class T >
void gtm::MatrixFreeMemory ( T **  ma,
uint32_t  n 
) [inline]

Frees matrix memory.

Parameters:
ma Matrix.
n Columns.

Definition at line 426 of file vmfuncs.h.

00427     {
00428         uint32_t i;
00429 
00430         if( ma ) {
00431 
00432             for( i = 0; i < n; i++ )
00433                 if( ma[ i ] ) delete [] ma[ i ];
00434             delete [] ma;
00435 
00436         } // fi
00437 
00438     }

template<class T >
void gtm::MatrixInverseAdjoint ( T **  ma,
T **  ma_o,
uint32_t  n 
) [inline]

Calculates the inverse using the adjoint method (only square matrices).

Parameters:
ma Result.
ma_o Matrix.
n Dimension.

Definition at line 619 of file vmfuncs.h.

00620     {
00621         uint32_t i, j;
00622         T** tmp;
00623         T c;
00624 
00625         tmp = MatrixAllocateMemory< T >( n - 1, n - 1 );
00626         for( i = 0; i < n; i++ ) {
00627 
00628             for( j = 0; j < n; j++ ) {
00629 
00630                 c = ( ( i + j ) % 2 == 0 )? ( T )1: ( T )( 0 - 1 );
00631                 MatrixCofactor< T >( tmp, ma_o, i, j, n, n );
00632                 ma[ j ][ i ] = c * MatrixDeterminant< T >( tmp, n - 1 );
00633 
00634             } // rof
00635 
00636         } // rof
00637         MatrixFreeMemory< T >( tmp, n - 1 );
00638 
00639         c = ( T )1 / MatrixDeterminant< T >( ma_o, n );
00640         MatrixScalarProduct< T >( ma, ma, c, n, n );
00641 
00642     }

template<class T >
void gtm::MatrixInverseGauss ( T **  ma,
T **  ma_o,
uint32_t  n 
) [inline]

Calculates the inverse using the gauss method (only square matrices).

Parameters:
ma Result.
ma_o Matrix.
n Dimension.
Warning:
Adapted from a java-implementation: http://www.nauticom.net/www/jdtaft/JavaMatrix.htm

Definition at line 654 of file vmfuncs.h.

00655     {
00656         uint32_t i, j, k, n2 = 2 * n;
00657         T** ma_a = MatrixAllocateMemory< T >( n2, n );
00658         T a, b;
00659 
00660         //  Augmented matrix initialization
00661         for( i = 0; i < n; i++ )
00662             for( j = 0; j < n; ma_a[ i ][ j ] = ma_o[ i ][ j ], j++ );
00663         for( i = n; i < n2; i++ )
00664             for( j = 0; j < n; ma_a[ i ][ j ] = ( i - n == j )? ( T )1: ( T )0, j++ );
00665 
00666         //  Reduction
00667         for( j = 0; j < n; j++ ) {
00668 
00669             a = ma_a[ j ][ j ];
00670             if( a != 0 ) for( i = 0; i < n2; ma_a[ i ][ j ] = ma_a[ i ][ j ] / a, i++ );
00671             for( k = 0; k < n; k++ ) {
00672 
00673                 if( ( k - j ) != 0 ) {
00674 
00675                     b = ma_a[ j ][ k ];
00676                     for(
00677                         i = 0;
00678                         i < n2;
00679                         ma_a[ i ][ k ] = ma_a[ i ][ k ] - ( b * ma_a[ i ][ j ] ), i++
00680                         );
00681 
00682                 } // fi
00683 
00684             } // rof
00685 
00686         } // rof
00687 
00688         //  Result assignation
00689         MatrixAssignMemory< T >( ma, ma_a, n, n );
00690         MatrixFreeMemory< T >( ma_a, n2 );
00691 
00692     }

template<class T >
void gtm::MatrixLoadIdentity ( T **  ma,
uint32_t  n 
) [inline]

Load a square matrix with the identity.

Parameters:
ma Matrix.
n Dimension.

Definition at line 721 of file vmfuncs.h.

00722     {
00723         uint32_t i, j;
00724 
00725         for( i = 0; i < n; i++ )
00726             for( j = 0; j < n; ma[ i ][ j ] = ( i == j )? ( T )1: ( T )0, j++ );
00727 
00728     }

template<class T >
void gtm::MatrixPrint ( std::ostream &  o,
T **  ma,
uint32_t  n,
uint32_t  m 
) [inline]

Functions for matrices.

Stream out function for matrices.

Parameters:
o Output stream.
ma Matrix.
n Columns.
m Rows.

Definition at line 305 of file vmfuncs.h.

00306     {
00307         uint32_t i, j;
00308 
00309         o << n << " x " << m << endl;
00310         for( j = 0; j < m; j++ ) {
00311 
00312             for( i = 0; i < n; o << ma[ i ][ j ] << " ", i++ );
00313             o << endl;
00314 
00315         } // rof
00316 
00317     }

template<class T >
void gtm::MatrixProduct ( T **  ma,
T **  ma_l,
T **  ma_r,
uint32_t  n,
uint32_t  m,
uint32_t  nr 
) [inline]

Performs a matricial product.

Parameters:
ma Result.
ma_l Left operand.
ma_r Right operand.
n Left columns.
m Left rows/right columns.
nr Right columns.

Definition at line 492 of file vmfuncs.h.

00493     {
00494         unsigned i, j, k;
00495 
00496         for( i = 0; i < nr; i++ )
00497             for( j = 0; j < m; j++ )
00498                 for(
00499                     k = 0, ma[ i ][ j ] = ( T )0;
00500                     k < n;
00501                     ma[ i ][ j ] = ma[ i ][ j ] + ( ma_l[ k ][ j ] * ma_r[ i ][ k ] ), k++
00502                     );
00503 
00504     }

template<class T >
void gtm::MatrixScalarProduct ( T **  ma,
T **  ma_l,
s,
uint32_t  n,
uint32_t  m 
) [inline]

Performs a scalar-matrix product.

Parameters:
ma Result.
ma_l Left operand.
s Scalar.
n Columns.
m Rows.

Definition at line 517 of file vmfuncs.h.

00518     {
00519         uint32_t i, j;
00520 
00521         for( i = 0; i < n; i++ )
00522             for( j = 0; j < m; ma[ i ][ j ] = ma_l[ i ][ j ] * s, j++ );
00523 
00524     }

template<class T >
void gtm::MatrixSubtract ( T **  ma,
T **  ma_l,
T **  ma_r,
uint32_t  n,
uint32_t  m 
) [inline]

Performs a matricial substraction.

Parameters:
ma Result.
ma_l Left operand.
ma_r Right operand.
n Columns.
m Rows.

Definition at line 471 of file vmfuncs.h.

00472     {
00473         uint32_t i, j;
00474 
00475         for( i = 0; i < n; i++ )
00476             for( j = 0; j < m; ma[ i ][ j ] = ma_l[ i ][ j ] - ma_r[ i ][ j ], j++ );
00477 
00478     }

template<class T >
void gtm::MatrixTranspose ( T **  ma,
T **  ma_o,
uint32_t  n,
uint32_t  m 
) [inline]

Gets the transpose matrix.

Parameters:
ma Matrix result.
ma_o Matrix.
n Columns.
m Rows.

Definition at line 704 of file vmfuncs.h.

00705     {
00706         uint32_t i, j;
00707 
00708         for( i = 0; i < m; i++ )
00709             for( j = 0; j < n; ma[ i ][ j ] = ma_o[ j ][ i ], j++ );
00710         
00711     }

template<class T >
void gtm::MatrixVectorCast ( T *  v,
T **  ma,
uint32_t  n,
uint32_t  m,
bool  col 
) [inline]

Converts a matrix in a vector.

Parameters:
v Vector.
ma Matrix.
n Columns.
m Rows.
col Convert to a column vector?

Definition at line 409 of file vmfuncs.h.

00410     {
00411         uint32_t i;
00412 
00413         for( i = 0; i < ( ( col )? m: n ); i++ )
00414             v[ i ] = ma[ ( col )? n: i ][ ( col )? i: m ];
00415 
00416     }

double gtm::round ( double  n  )  [inline]

Rounds a double number.

Parameters:
n Number.

Definition at line 56 of file mathdefs.h.

00057     {
00058         double tmp;
00059         tmp = floor( n );
00060         if ( ( n - tmp ) < 0.5 )
00061             return( tmp );
00062         else
00063             return( ceil( n ) );
00064     }

template<class T >
void gtm::VectorAdd ( T *  v,
T *  v_l,
T *  v_r,
uint32_t  n 
) [inline]

Performs a vectorial addition.

Parameters:
v Result.
v_l Left operand.
v_r Right operand.
n Cardinality.

Definition at line 177 of file vmfuncs.h.

00178     {
00179         uint32_t i;
00180 
00181         for( i = 0; i < n; v[ i ] = v_l[ i ] + v_r[ i ], i++ );
00182 
00183     }

template<class T >
T* gtm::VectorAllocateMemory ( uint32_t  n  )  [inline]

Allocation function.

Parameters:
n Cardinality.

Definition at line 77 of file vmfuncs.h.

00078     {
00079         T* v = new T[ n ];
00080 
00081         memset( v, 0, sizeof( T ) * n );
00082         return( v );
00083 
00084     }

template<class T >
void gtm::VectorAssignMemory ( T *  v,
T *  v_o,
uint32_t  n 
) [inline]

Copy.

Parameters:
v Target vector.
v_o Source vector.
n Cardinality.

Definition at line 108 of file vmfuncs.h.

00109     {
00110         memcpy( v, v_o, sizeof( T ) * n );
00111 
00112     }

template<class T >
void gtm::VectorAssignScalar ( T *  v,
s,
uint32_t  n 
) [inline]

Scalar assignation.

Parameters:
v Vector.
s Scalar value.
n Cardinality.

Definition at line 139 of file vmfuncs.h.

00140     {
00141         uint32_t i;
00142 
00143         for( i = 0; i < n; v[ i ] = s, i++ );
00144 
00145     }

template<class T >
T* gtm::VectorCopyMemory ( T *  v,
uint32_t  n 
) [inline]

Allocation & copy.

Parameters:
v Vector to be copied.
n Cardinality.

Definition at line 122 of file vmfuncs.h.

00123     {
00124         T* n_v = VectorAllocateMemory< T >( n );
00125         VectorAssignMemory< T >( n_v, v, n );
00126         return( n_v );
00127 
00128     }

template<class T >
void gtm::VectorCrossProduct ( T *  v,
T *  v_l,
T *  v_r 
) [inline]

Performs a vectorial cross product.

Parameters:
v Result.
v_l Left operand.
v_r Right operand.

Definition at line 212 of file vmfuncs.h.

00213     {
00214         v[ 0 ] = ( v_l[ 1 ] * v_r[ 2 ] ) - ( v_l[ 2 ] * v_r[ 1 ] );
00215         v[ 1 ] = ( v_l[ 2 ] * v_r[ 0 ] ) - ( v_l[ 0 ] * v_r[ 2 ] );
00216         v[ 2 ] = ( v_l[ 0 ] * v_r[ 1 ] ) - ( v_l[ 1 ] * v_r[ 0 ] );
00217 
00218     }

template<class T >
T gtm::VectorDotProduct ( T *  v_l,
T *  v_r,
uint32_t  n 
) [inline]

Performs a vectorial dot product.

Parameters:
v_l Left operand.
v_r Right operand.
n Cardinality.
Returns:
Dot product.

Definition at line 230 of file vmfuncs.h.

00231     {
00232         T ret;
00233         uint32_t i;
00234 
00235         for( i = 0, ret = ( T )0; i < n; ret = ret + ( v_l[ i ] * v_r[ i ] ), i++ );
00236         return( ret );
00237 
00238     }

template<class T >
void gtm::VectorFreeMemory ( T *  v  )  [inline]

Frees vector's memory.

Parameters:
v Vector.

Definition at line 93 of file vmfuncs.h.

00094     {
00095         if( v ) delete [] v;
00096 
00097     }

template<class T >
void gtm::VectorMatrixCast ( T **  ma,
T *  v,
uint32_t  n,
uint32_t  m,
bool  col 
) [inline]

Convert a vector in a matrix.

Parameters:
ma Matrix.
v Vector.
n Columns.
m Rows.
col Is it a column vector?

Definition at line 158 of file vmfuncs.h.

00159     {
00160         uint32_t i;
00161 
00162         for( i = 0; i < ( ( col )? m: n ); i++ )
00163             ma[ ( col )? n: i ][ ( col )? i: m ] = v[ i ];
00164 
00165     }

template<class T >
T gtm::VectorNorm ( T *  v,
uint32_t  n 
) [inline]

Calculates vectorial L2 norm.

Parameters:
v Vector.
n Cardinality.

Definition at line 266 of file vmfuncs.h.

00267     {
00268         return( ( T )( sqrt( ( double )( VectorDotProduct< T >( v, v, n ) ) ) ) );
00269 
00270     }

template<class T >
void gtm::VectorNormalize ( T *  v,
T *  v_o,
uint32_t  n 
) [inline]

Calculates a normal vector, usign the L2 norm.

Parameters:
v Result.
v_o Original vector.
n Cardinality.

Definition at line 281 of file vmfuncs.h.

00282     {
00283         uint32_t i;
00284         T norm = VectorNorm< T >( v_o, n );
00285 
00286         norm = ( norm == ( T )0 )? ( T )1: norm;
00287         for( i = 0; i < n; v[ i ] = v_o[ i ] / norm, i++ );
00288 
00289     }

template<class T >
void gtm::VectorPrint ( std::ostream &  o,
T *  v,
uint32_t  n,
bool  col 
) [inline]

Functions for vectors.

Stream out function for vectors.

Parameters:
o Output stream.
v ANSI array.
n Cardinality.
col Should I print it in column format?

Definition at line 52 of file vmfuncs.h.

00053     {
00054         uint32_t i;
00055         
00056         o << n << ": [";
00057         if( col ) o << endl;
00058         else      o << " ";
00059         for( uint32_t i = 0; i < n; i++ ) {
00060             
00061             o << v[ i ];
00062             if( col ) o << endl;
00063             else      o << " ";
00064             
00065         } // rof
00066         o << "]" << endl;
00067         
00068     }

template<class T >
void gtm::VectorScalarProduct ( T *  v,
T *  v_l,
s,
uint32_t  n 
) [inline]

Performs a vector-scalar product.

Parameters:
v Result.
v_l Left operand.
s Scalar.
n Cardinality.

Definition at line 250 of file vmfuncs.h.

00251     {
00252         uint32_t i;
00253 
00254         for( i = 0; i < n; v[ i ] = v_l[ i ] * s, i++ );
00255 
00256     }

template<class T >
void gtm::VectorSubtract ( T *  v,
T *  v_l,
T *  v_r,
uint32_t  n 
) [inline]

Performs a vectorial substraction.

Parameters:
v Result.
v_l Left operand.
v_r Right operand.
n Cardinality.

Definition at line 195 of file vmfuncs.h.

00196     {
00197         uint32_t i;
00198 
00199         for( i = 0; i < n; v[ i ] = v_l[ i ] - v_r[ i ], i++ );
00200 
00201     }


Generated on 20 Oct 2010 for creaMaracasVisu_lib by  doxygen 1.6.1