gtm Namespace Reference

Classes

class  TMatrix
class  TVector

Functions

double round (double n)



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

Function Documentation

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

Performs a matricial addition.

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

Definition at line 449 of file vmfuncs.h.

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

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

Allocates memory for a matrix.

Parameters:
n Columns.
m Rows.

Definition at line 325 of file vmfuncs.h.

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

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

Copies.

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

Definition at line 350 of file vmfuncs.h.

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

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

Scalar assignation.

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

Definition at line 386 of file vmfuncs.h.

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

template<class T >
void gtm::MatrixCofactor ( T **  ma,
T **  ma_o,
uint  i,
uint  j,
uint  n,
uint  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 536 of file vmfuncs.h.

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

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

Allocates & copies.

Parameters:
ma Matrix.
n Columns.
m Rows.

Definition at line 368 of file vmfuncs.h.

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

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

Gets the matrix determinant (square matrices only).

Parameters:
ma Matrix.
n Dimension.

Definition at line 564 of file vmfuncs.h.

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

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

Frees matrix memory.

Parameters:
ma Matrix.
n Columns.

Definition at line 424 of file vmfuncs.h.

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

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

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

Parameters:
ma Result.
ma_o Matrix.
n Dimension.

Definition at line 617 of file vmfuncs.h.

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

template<class T >
void gtm::MatrixInverseGauss ( T **  ma,
T **  ma_o,
uint  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 652 of file vmfuncs.h.

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

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

Load a square matrix with the identity.

Parameters:
ma Matrix.
n Dimension.

Definition at line 719 of file vmfuncs.h.

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

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

Functions for matrices.

Stream out function for matrices.

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

Definition at line 303 of file vmfuncs.h.

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

template<class T >
void gtm::MatrixProduct ( T **  ma,
T **  ma_l,
T **  ma_r,
uint  n,
uint  m,
uint  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 490 of file vmfuncs.h.

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

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

Performs a scalar-matrix product.

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

Definition at line 515 of file vmfuncs.h.

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

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

Performs a matricial substraction.

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

Definition at line 469 of file vmfuncs.h.

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

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

Gets the transpose matrix.

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

Definition at line 702 of file vmfuncs.h.

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

template<class T >
void gtm::MatrixVectorCast ( T *  v,
T **  ma,
uint  n,
uint  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 407 of file vmfuncs.h.

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

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

Rounds a double number.

Parameters:
n Number.

Definition at line 55 of file mathdefs.h.

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

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

Performs a vectorial addition.

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

Definition at line 175 of file vmfuncs.h.

00176     {
00177         uint i;
00178 
00179         for( i = 0; i < n; v[ i ] = v_l[ i ] + v_r[ i ], i++ );
00180 
00181     }

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

Allocation function.

Parameters:
n Cardinality.

Definition at line 75 of file vmfuncs.h.

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

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

Copy.

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

Definition at line 106 of file vmfuncs.h.

00107     {
00108         memcpy( v, v_o, sizeof( T ) * n );
00109 
00110     }

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

Scalar assignation.

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

Definition at line 137 of file vmfuncs.h.

00138     {
00139         uint i;
00140 
00141         for( i = 0; i < n; v[ i ] = s, i++ );
00142 
00143     }

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

Allocation & copy.

Parameters:
v Vector to be copied.
n Cardinality.

Definition at line 120 of file vmfuncs.h.

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

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 210 of file vmfuncs.h.

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

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

Performs a vectorial dot product.

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

Definition at line 228 of file vmfuncs.h.

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

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

Frees vector's memory.

Parameters:
v Vector.

Definition at line 91 of file vmfuncs.h.

00092     {
00093         if( v ) delete [] v;
00094 
00095     }

template<class T >
void gtm::VectorMatrixCast ( T **  ma,
T *  v,
uint  n,
uint  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 156 of file vmfuncs.h.

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

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

Calculates vectorial L2 norm.

Parameters:
v Vector.
n Cardinality.

Definition at line 264 of file vmfuncs.h.

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

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

Calculates a normal vector, usign the L2 norm.

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

Definition at line 279 of file vmfuncs.h.

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

template<class T >
void gtm::VectorPrint ( std::ostream &  o,
T *  v,
uint  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 50 of file vmfuncs.h.

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

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

Performs a vector-scalar product.

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

Definition at line 248 of file vmfuncs.h.

00249     {
00250         uint i;
00251 
00252         for( i = 0; i < n; v[ i ] = v_l[ i ] * s, i++ );
00253 
00254     }

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

Performs a vectorial substraction.

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

Definition at line 193 of file vmfuncs.h.

00194     {
00195         uint i;
00196 
00197         for( i = 0; i < n; v[ i ] = v_l[ i ] - v_r[ i ], i++ );
00198 
00199     }


Generated on 18 Mar 2010 for creaMaracasVisu_lib by  doxygen 1.6.1