DGtal  0.6.devel
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Types | Public Member Functions | Static Public Attributes | Private Attributes
DGtal::SimpleMatrix< TComponent, TM, TN > Class Template Reference

#include <SimpleMatrix.h>

Public Types

typedef TComponent Component
typedef PointVector< N, ComponentRowVector
typedef PointVector< M, ComponentColumnVector
typedef SimpleMatrix
< Component, TM, TN > 
Self

Public Member Functions

 BOOST_CONCEPT_ASSERT ((CEuclideanRing< TComponent >))
 BOOST_STATIC_ASSERT (TM > 0)
 BOOST_STATIC_ASSERT (TM > 0)
 SimpleMatrix ()
 SimpleMatrix (const Self &other)
void clear ()
void constant (const Component &aScalar)
void identity ()
RowVector row (const DGtal::Dimension i) const
ColumnVector column (const DGtal::Dimension j) const
void setComponent (const DGtal::Dimension i, const DGtal::Dimension j, const Component &aValue)
Component operator() (const DGtal::Dimension i, const DGtal::Dimension j) const
bool operator== (const Self &aMatrix) const
template<typename TComponentOther >
Selfoperator= (const SimpleMatrix< TComponentOther, M, N > &aMatrix)
Self operator+ (const Self &aMatrix) const
Selfoperator+= (const Self &aMatrix)
Self operator- (const Self &aMatrix) const
Selfoperator-= (const Self &aMatrix)
Self operator* (const Component &aScalar) const
Selfoperator*= (const Component &aScalar)
Self operator/ (const Component &aScalar) const
Selfoperator/= (const Component &aScalar)
SimpleMatrix< Component, TM, TM > operator* (const SimpleMatrix< Component, N, M > &aMatrix) const
ColumnVector operator* (const RowVector &aVector) const
SimpleMatrix< Component, TN, TM > transpose () const
Component cofactor (const DGtal::Dimension i, const DGtal::Dimension j) const
Self cofactor () const
Component minorDeterminant (const DGtal::Dimension i, const DGtal::Dimension j) const
Component determinant () const
SimpleMatrix< Component, TM, TN > inverse () const
 ~SimpleMatrix ()
void selfDisplay (std::ostream &out) const
bool isValid () const
template<typename TC >
DGtal::SimpleMatrix< T, TM, TN > & operator= (const SimpleMatrix< TC, M, N > &other)

Static Public Attributes

static const DGtal::Dimension M = TM
static const DGtal::Dimension N = TN

Private Attributes

boost::array< Component, M *NmyValues
boost::array< Component, M *NmyCofactorCoefs

Detailed Description

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
class DGtal::SimpleMatrix< TComponent, TM, TN >

Aim: implements basic MxN Matrix services (M,N>=1).

Description of template class 'SimpleMatrix'

This class defines simple matrix with static size. Computations are performed in the euclidean ring associated with the type TComponent.

This class also defines types for row and column vectors as specialized PointVector types.

Class inspired by Anis Benyoub (INSA-Lyon).

Template Parameters:
TComponentany model of CEuclideanRing
TMnumber of rows of the matrix
TNnumber of columns of the matrix

Definition at line 72 of file SimpleMatrix.h.


Member Typedef Documentation

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
typedef PointVector<M,Component> DGtal::SimpleMatrix< TComponent, TM, TN >::ColumnVector

Definition at line 81 of file SimpleMatrix.h.

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
typedef TComponent DGtal::SimpleMatrix< TComponent, TM, TN >::Component

Definition at line 76 of file SimpleMatrix.h.

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
typedef PointVector<N,Component> DGtal::SimpleMatrix< TComponent, TM, TN >::RowVector

Definition at line 80 of file SimpleMatrix.h.

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
typedef SimpleMatrix<Component,TM,TN> DGtal::SimpleMatrix< TComponent, TM, TN >::Self

Definition at line 83 of file SimpleMatrix.h.


Constructor & Destructor Documentation

template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN >::SimpleMatrix ( )
inline

Create a static mxn matrix.

SimpleMatrix values are all set to the zero value associated to Component type.

Constructor.

Definition at line 56 of file SimpleMatrix.ih.

{
for ( DGtal::Dimension i = 0; i < TM*TN; ++i )
myValues[ i ] = NumberTraits<Component>::ZERO;
//Cofactor coefs computation
for (DGtal::Dimension i=0; i<TM; i++)
for (DGtal::Dimension j=0; j<TN; j++)
myCofactorCoefs[i*N+j] = static_cast<T>(std::pow(-1.0f, (float)i+j));
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN >::SimpleMatrix ( const Self other)
inline

Copy constructor.

Parameters:
otherthe object to clone. Forbidden by default.

Definition at line 70 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myCofactorCoefs, and DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
for ( DGtal::Dimension i = 0; i < M*N; ++i )
{
myValues[ i ] = other.myValues[i];
myCofactorCoefs[ i ] = other.myCofactorCoefs[i];
}
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN >::~SimpleMatrix ( )
inline

Destructor.

Definition at line 46 of file SimpleMatrix.ih.

{
}

Member Function Documentation

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< TComponent, TM, TN >::BOOST_CONCEPT_ASSERT ( (CEuclideanRing< TComponent >)  )
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< TComponent, TM, TN >::BOOST_STATIC_ASSERT ( TM  ,
 
)
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< TComponent, TM, TN >::BOOST_STATIC_ASSERT ( TM  ,
 
)
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
void DGtal::SimpleMatrix< T, TM, TN >::clear ( )
inline

Clear matrix values

Definition at line 83 of file SimpleMatrix.ih.

{
for ( DGtal::Dimension i = 0; i < M*N; ++i )
myValues[ i ] = NumberTraits<T>::ZERO;
}
template<typename T , DGtal::Dimension M, DGtal::Dimension N>
T DGtal::SimpleMatrix< T, M, N >::cofactor ( const DGtal::Dimension  i,
const DGtal::Dimension  j 
) const
inline

Cofactor of the matrix at position (i,j).

Precondition:
the matrix is a square one.
Returns:
the cofactor at (i,j).

Definition at line 170 of file SimpleMatrix.ih.

template<typename T , DGtal::Dimension M, DGtal::Dimension N>
DGtal::SimpleMatrix< T, M, N > DGtal::SimpleMatrix< T, M, N >::cofactor ( ) const
inline

Cofactor matrix computation.

Precondition:
the matrix is a square one.
Returns:
the cofactor matrix.

Definition at line 180 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::setComponent().

{
for (DGtal::Dimension i=0; i<M; i++)
for (DGtal::Dimension j=0; j<M; j++)
mat.setComponent( i, j , cofactor(i,j));
return mat;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN >::ColumnVector DGtal::SimpleMatrix< T, TM, TN >::column ( const DGtal::Dimension  j) const
inline

Get column vector.

Parameters:
jthe column index.
Returns:
the j-th column

Definition at line 125 of file SimpleMatrix.ih.

{
ASSERT(j<N);
for ( DGtal::Dimension i = 0; i < M; ++i )
v[ i ] = this->operator()(i,j);
return v;
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
void DGtal::SimpleMatrix< T, TM, TN >::constant ( const Component aScalar)
inline

Set a constant scalar to each matrix component.

Parameters:
aScalarthe scalar

Definition at line 92 of file SimpleMatrix.ih.

{
for ( DGtal::Dimension i = 0; i < M*N; ++i )
myValues[ i ] = aSc;
}
template<typename T , DGtal::Dimension M, DGtal::Dimension N>
T DGtal::SimpleMatrix< T, M, N >::determinant ( ) const
inline

Returns the determinant of square matrix. Slow method for large matrices.

Precondition:
this must be NxN

Specialized methods exist for 2x2 and 3x3 matrices.

Returns:
the determinant.

Definition at line 204 of file SimpleMatrix.ih.

template<typename T , DGtal::Dimension M, DGtal::Dimension N>
void DGtal::SimpleMatrix< T, M, N >::identity ( )
inline

Set values to create identity matrix

Precondition:
the matrix is a square one.

Definition at line 101 of file SimpleMatrix.ih.

{
//fast clear
this->clear();
for (DGtal::Dimension i=0; i<M; i++)
this->setComponent( i, i , NumberTraits<T>::ONE);
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN > DGtal::SimpleMatrix< T, TM, TN >::inverse ( ) const
inline

Compute the inverse of the matrix.

Precondition:
the matrix is a square one.
Returns:
the inverse matrix.

Definition at line 213 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::transpose().

{
SimpleMatrix<T,TM,TM> r = cofactor().transpose();
//determinant
T det = determinant();
ASSERT(det != 0);
return r/det;
}
template<typename T , DGtal::Dimension M, DGtal::Dimension N>
bool DGtal::SimpleMatrix< T, M, N >::isValid ( ) const
inline

Checks the validity/consistency of the object.

Returns:
'true' if the object is valid, 'false' otherwise.

Definition at line 402 of file SimpleMatrix.ih.

{
return true;
}
template<typename T , DGtal::Dimension M, DGtal::Dimension N>
T DGtal::SimpleMatrix< T, M, N >::minorDeterminant ( const DGtal::Dimension  i,
const DGtal::Dimension  j 
) const
inline

Return the minor determinant (i,j) of the current matrix

Specialized methods exist for 2x2 and 3x3 matrices.

Precondition:
the matrix is a square one.
Parameters:
irow index
jcolumn index
Returns:
the minor (i,j)

Definition at line 195 of file SimpleMatrix.ih.

template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
T DGtal::SimpleMatrix< T, TM, TN >::operator() ( const DGtal::Dimension  i,
const DGtal::Dimension  j 
) const
inline

Get a value at position (i,j).

Parameters:
irow index
jcolumn index

Definition at line 364 of file SimpleMatrix.ih.

{
ASSERT(i<M);
ASSERT(j<N);
return myValues[i*N + j];
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
Self DGtal::SimpleMatrix< TComponent, TM, TN >::operator* ( const Component aScalar) const

Product between the matrix 'this' and a scalar

Parameters:
aScalarthe scalar coefficient
Returns:
the resulting matrix
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
SimpleMatrix<Component,TM,TM> DGtal::SimpleMatrix< TComponent, TM, TN >::operator* ( const SimpleMatrix< Component, N, M > &  aMatrix) const

Product between the matrix 'this' and aMatrix.

Note:
the product is O(N^3) for NxN matrices.
Parameters:
aMatrixthe NxM matrix to multiply
Returns:
the product MxM matrix
template<typename T , DGtal::Dimension M, DGtal::Dimension N>
DGtal::SimpleMatrix< T, M, N >::ColumnVector DGtal::SimpleMatrix< T, M, N >::operator* ( const RowVector aVector) const
inline

Product between the matrix and a Column vector.

Note:
the product is O(N^3) for NxN matrices.
Parameters:
aMatrixthe NxM matrix to multiply
Returns:
the product MxM matrix

Definition at line 271 of file SimpleMatrix.ih.

{
for (DGtal::Dimension i=0; i<M; i++)
for (DGtal::Dimension k=0; k<N; k++)
res[i] += this->operator()(i, k )*other[k];
return res;
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, M, N > & DGtal::SimpleMatrix< T, M, N >::operator*= ( const Component aScalar)
inline

Product between the matrix 'this' and a scalar

Parameters:
aScalarthe scalar coefficient
Returns:
the resulting a reference to the matrix

Definition at line 328 of file SimpleMatrix.ih.

{
for (DGtal::Dimension i=0; i<M*N; i++)
this->myValues[i] *= other;
return *this;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN > DGtal::SimpleMatrix< T, TM, TN >::operator+ ( const Self aMatrix) const
inline

Addition between the matrix 'this' and aMatrix.

Parameters:
aMatrixthe matrix to add to self.
Returns:
the summed matrix

Definition at line 149 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
SimpleMatrix<T,TM,TN> res;
for ( DGtal::Dimension i = 0; i < M*N; ++i )
res.myValues[ i ] = this->myValues[i] + other.myValues[i];
return res;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN > & DGtal::SimpleMatrix< T, TM, TN >::operator+= ( const Self aMatrix)
inline

Addition and assignment between the matrix 'this' and aMatrix.

Parameters:
aMatrixthe matrix to add to self.
Returns:
a reference to the result

Definition at line 160 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
for ( DGtal::Dimension i = 0; i < M*N; ++i )
myValues[ i ] += other.myValues[i];
return *this;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN > DGtal::SimpleMatrix< T, TM, TN >::operator- ( const Self aMatrix) const
inline

Substract between the matrix 'this' and aMatrix.

Parameters:
aMatrixthe matrix to substract to self.
Returns:
the difference matrix

Definition at line 229 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
SimpleMatrix<T,TM,TN> res;
for ( DGtal::Dimension i = 0; i < M*N; ++i )
res.myValues[ i ] = this->myValues[i] - other.myValues[i];
return res;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN > & DGtal::SimpleMatrix< T, TM, TN >::operator-= ( const Self aMatrix)
inline

Substract and assignment between the matrix 'this' and aMatrix.

Parameters:
aMatrixthe matrix to substract to self.
Returns:
a reference to the result

Definition at line 240 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
for ( DGtal::Dimension i = 0; i < M*N; ++i )
myValues[ i ] -= other.myValues[i];
return *this;
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, M, N > DGtal::SimpleMatrix< T, M, N >::operator/ ( const Component aScalar) const
inline

Division of a matrix by a scalar.

Parameters:
aScalarthe scalar value
Returns:
the resulting matrix

Definition at line 316 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
Self resultat;
for (DGtal::Dimension i=0; i<M*N; i++)
resultat.myValues[i] = myValues[i]/other;
return resultat;
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, M, N > & DGtal::SimpleMatrix< T, M, N >::operator/= ( const Component aScalar)
inline

Division of a matrix by a scalar.

Parameters:
aScalarthe scalar value
Returns:
a reference to the updated matrix

Definition at line 306 of file SimpleMatrix.ih.

{
for (DGtal::Dimension i=0; i<M*N; i++)
this->myValues[i] /= other;
return *this;
}//------------------------------------------------------------------------------
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
template<typename TC >
DGtal::SimpleMatrix<T, TM, TN>& DGtal::SimpleMatrix< TComponent, TM, TN >::operator= ( const SimpleMatrix< TC, M, N > &  other)
inline

Definition at line 138 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
for ( DGtal::Dimension i = 0; i < M*N; ++i )
myValues[ i ] = static_cast<T>(other.myValues[i]);
return *this;
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
template<typename TComponentOther >
Self& DGtal::SimpleMatrix< TComponent, TM, TN >::operator= ( const SimpleMatrix< TComponentOther, M, N > &  aMatrix)

Assignment operator from another matrix. Note: a static_cast from TComponentOther to Component is performed.

Template Parameters:
TComponentOtheranother Component type.
Parameters:
aMatrixthe matrix to copy.
Returns:
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
bool DGtal::SimpleMatrix< T, TM, TN >::operator== ( const Self aMatrix) const
inline

SimpleMatrix comparison.

Parameters:
anothermatrix.
Returns:
true if aMatrix equals this

Definition at line 250 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::myValues.

{
return myValues == other.myValues;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TM, TN >::RowVector DGtal::SimpleMatrix< T, TM, TN >::row ( const DGtal::Dimension  i) const
inline

Get row vector.

Parameters:
ithe row index
Returns:
the i-th row

Definition at line 114 of file SimpleMatrix.ih.

{
ASSERT(i<M);
for ( DGtal::Dimension j = 0; j < N; ++j )
v[ j ] = this->operator()(i,j);
return v;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
void DGtal::SimpleMatrix< T, TM, TN >::selfDisplay ( std::ostream &  out) const
inline

Writes/Displays the object on an output stream.

Parameters:
outthe output stream where the object is written.

Definition at line 382 of file SimpleMatrix.ih.

{
out << "[SimpleMatrix] "<<M<<"x"<<N<< " [";
for(DGtal::Dimension i = 0; i < M; ++i)
{
out<<"[";
for(DGtal::Dimension j = 0; j < N; ++j)
out<< this->operator()(i,j)<<" ";
out<<"]";
}
out<<"]";
}
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
void DGtal::SimpleMatrix< T, TM, TN >::setComponent ( const DGtal::Dimension  i,
const DGtal::Dimension  j,
const Component aValue 
)
inline

Set a value at position (i,j).

Parameters:
irow index
jcolumn index
aValuea component

Definition at line 352 of file SimpleMatrix.ih.

Referenced by DGtal::SimpleMatrix< TComponent, TM, TN >::cofactor(), and DGtal::SimpleMatrix< TComponent, TM, TN >::transpose().

{
ASSERT(i<M);
ASSERT(j<N);
myValues[i*N + j] = aValue;
}
template<typename T , DGtal::Dimension TM, DGtal::Dimension TN>
DGtal::SimpleMatrix< T, TN, TM > DGtal::SimpleMatrix< T, TM, TN >::transpose ( ) const
inline

Transpose the matrix.

Returns:
the transposted NxM matrix

Definition at line 259 of file SimpleMatrix.ih.

References DGtal::SimpleMatrix< TComponent, TM, TN >::setComponent().

Referenced by DGtal::SimpleMatrix< TComponent, TM, TN >::inverse().

{
for (DGtal::Dimension i=0; i<M; i++)
for (DGtal::Dimension j=0; j<N; j++)
res.setComponent(j,i, this->operator()(i,j));
return res;
}

Field Documentation

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
const DGtal::Dimension DGtal::SimpleMatrix< TComponent, TM, TN >::M = TM
static

Definition at line 77 of file SimpleMatrix.h.

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
boost::array< Component, M*N> DGtal::SimpleMatrix< TComponent, TM, TN >::myCofactorCoefs
private

Static computation of cofactor coefficients

Todo:
should be static

Definition at line 384 of file SimpleMatrix.h.

Referenced by DGtal::SimpleMatrix< TComponent, TM, TN >::SimpleMatrix().

template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
boost::array< Component, M*N> DGtal::SimpleMatrix< TComponent, TM, TN >::myValues
private
template<typename TComponent, DGtal::Dimension TM, DGtal::Dimension TN>
const DGtal::Dimension DGtal::SimpleMatrix< TComponent, TM, TN >::N = TN
static

Definition at line 78 of file SimpleMatrix.h.


The documentation for this class was generated from the following files: