Machine Learning Library
Public Member Functions | Protected Attributes | Friends | List of all members
CMatrix< Type > Class Template Reference

Template object implementing a matrix of single and double precision elements. More...

#include <CDenseVector.h>

Inheritance diagram for CMatrix< Type >:
CObject< Type >

Public Member Functions

 CMatrix (int iRows=0, int iColumns=0, bool bInitialize=false)
 
 CMatrix (const CMatrix< Type > &rtMatrix)
 
 ~CMatrix ()
 
CMatrix< Type > & operator= (const CMatrix< Type > &rtSrc)
 
void resize (int iRows, int iCols, bool bPreserveData=false)
 
int rows () const
 
virtual string className () const
 Returns the class name. More...
 
int columns () const
 
const Type & getElement (int i, int j) const
 
Type & operator() (int i, int j)
 
CMatrix< Type > operator() (int i, int j, int rn, int cn) const
 
void insert (int i, int j, const CMatrix< Type > &rtSubmatrix)
 
void setElement (int i, int j, const Type &tValue)
 
Type determinant () const
 
Type trace () const
 
Type sumElements () const
 
CDenseVector< Type > getRow (int iRow) const
 
CDenseVector< Type > getColumn (int iColumn) const
 
CDenseVector< Type > getDiagonal () const
 
Type minElement (int &rtX, int &rtY) const
 
Type maxElement (int &rtX, int &rtY) const
 
void operator*= (Type rScalar)
 
void operator+= (Type rScalar)
 
void operator-= (Type rScalar)
 
void operator/= (Type rScalar)
 
void operator+= (const CMatrix< Type > &tM)
 
void operator-= (const CMatrix< Type > &tM)
 
void setRow (int iRow, const CDenseVector< Type > &tVec)
 
void setColumn (int iIndex, CDenseVector< Type > &tVec)
 
void setDiagonal (Type tValue)
 
void setDiagonal (CDenseVector< Type > &tDiag)
 
Type frobeniusNorm ()
 
CMatrix< Type > transpose ()
 
bool isNaN () const
 
CMatrix< Type > invert () const
 
void dump (ostream &stream=cout)
 
bool serialize (fstream &stream, IO_MODE=READ)
 Read/write from binary stream. More...
 
bool serialize2 (CArchiv &tArchiv)
 
void zeros ()
 
void setData (Type *ptData, bool bColumnMajorOrder=false)
 
bool exportToMatFile (const char *acPath, const char *acVarame, bool bAppend=true)
 
int importFromMatFile (const char *acPath, const char *acVarname)
 
void replicateMatrix (int iX, int iY, const CMatrix< Type > &tMat)
 
Type * _data ()
 
int eig (CDenseVector< Type > &rtEigenvalues, CMatrix< Type > &rtEigenvectors, int iNum=0, TMatrixType tType=SYMETRIC)
 
int calcLUFactorization (CMatrix< Type > &rtL, CMatrix< Type > &rtU, CDenseVector< int > &rtIdx) const
 
- Public Member Functions inherited from CObject< Type >
 CObject ()
 Constructor. More...
 
virtual ~CObject ()
 Destructor. More...
 
void setVerbose (VERBOSE_LEVEL tVerbose)
 Set the verbose level. More...
 
VERBOSE_LEVEL verbose (void) const
 Return current verbose level. More...
 
virtual bool isA (const char *acClass) const
 Check if the object is an instance of the class with given name. More...
 
DATATYPE dataType () const
 Returns the template data type. More...
 

Protected Attributes

int iRows
 
int iCols
 
Type * ptData
 
- Protected Attributes inherited from CObject< Type >
unsigned char ucVerbose
 

Friends

CMatrix< Type > operator+ (const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
 
CMatrix< Type > operator- (const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
 
CMatrix< Type > elementMul (const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
 
CMatrix< Type > elementDiv (const CMatrix< Type > &rtMat1, const CMatrix< Type > &rtMat2)
 
CMatrix< Type > operator* (const CMatrix< Type > &tMat1, const CMatrix< Type > &tMat2)
 
CMatrix< Type > operator+ (const CMatrix< Type > &rtMat, const Type &rtScalar)
 
CMatrix< Type > operator- (const CMatrix< Type > &rtMat, const Type &rtScalar)
 
CMatrix< Type > operator* (const CMatrix< Type > &rtMat, const Type &rtScalar)
 
CMatrix< Type > operator/ (const CMatrix< Type > &rtMat, const Type &rtScalar)
 
CDenseVector< Type > operator* (const CMatrix< Type > &rtMat, const CVector< Type > &rtVec)
 
void load (CMatrix< Type > &, char *acString)
 
void save (const CMatrix< Type > &, char *acString)
 

Detailed Description

template<class Type>
class CMatrix< Type >

Template object implementing a matrix of single and double precision elements.

The elements are stored in a C array in column-major order for easy utilization of LAPACK or CBLAS packages.

Constructor & Destructor Documentation

template<class Type>
CMatrix< Type >::CMatrix ( int  iRows = 0,
int  iColumns = 0,
bool  bInitialize = false 
)

Constructor – Create a new instance of a matrix object with given size.

Parameters
iRowsNumber of rows (default 0).
iColumnsNumber of columns (default 0).
bInitialzeIf true, matrix will be initialized with 0 (default: false).
template<class Type>
CMatrix< Type >::CMatrix ( const CMatrix< Type > &  rtMatrix)

Constructor – Create a new instance of a matrix object which is a clone of a second matrix.

Parameters
Referenceto matrix which will be cloned
template<class Type>
CMatrix< Type >::~CMatrix ( )

Destructor

Member Function Documentation

template<class Type>
Type* CMatrix< Type >::_data ( )
inline
template<class Type>
int CMatrix< Type >::calcLUFactorization ( CMatrix< Type > &  rtL,
CMatrix< Type > &  rtU,
CDenseVector< int > &  rtIdx 
) const

LU factorization of matrix

template<class Type>
virtual string CMatrix< Type >::className ( ) const
inlinevirtual

Returns the class name.

Reimplemented from CObject< Type >.

template<class Type>
int CMatrix< Type >::columns ( ) const
inline

Return the number of columns of the matrix.

Returns
Number of columns.

References CMatrix< Type >::iCols.

template<class Type>
Type CMatrix< Type >::determinant ( ) const

Calculate the determinant of the matrix

Returns
Matrix determinant
template<class Type>
void CMatrix< Type >::dump ( ostream &  stream = cout)

Dump matrix to stream

template<class Type>
int CMatrix< Type >::eig ( CDenseVector< Type > &  rtEigenvalues,
CMatrix< Type > &  rtEigenvectors,
int  iNum = 0,
TMatrixType  tType = SYMETRIC 
)

Calculate all eigenvalues and eigenvectors of the current matrix. Speifying the type of the matrix can speed-up the calculation significantly. Additionally, only the first iNum vectors can be calculated.

Parameters
rtEigenvaluesVector to be filled with eigenvalues in ascending order
rtEigenvectorsMatrix to be filled with eigenvectors
iNumDirectionsNumber of eigenvectors and eigenvalue to calculate
tTypeMatrix type
Returns
0 for success, otherwise for error
template<class Type>
bool CMatrix< Type >::exportToMatFile ( const char *  acPath,
const char *  acVarame,
bool  bAppend = true 
)

Export matrix to file MAT-file (Level 1.0)

Parameters
acPathPath to MAType file
acVarnameVariable name in MAType file
bAppendIf the file already exists, the data is append to the file (Default: true)
Returns
True on success, false oterwise
template<class Type>
Type CMatrix< Type >::frobeniusNorm ( )

Calculates the frobenius norm

\[ \parallel A \parallel_F = \sqrt { \sum_i \sum_j a_{ij}^2 } \]

Returns
Transposed matrix.
template<class Type>
CDenseVector<Type> CMatrix< Type >::getColumn ( int  iColumn) const

Return a column as a CDenseVector instance.

Parameters
iColumnIndex of column to return.
Returns
CDenseVector containing column.
template<class Type>
CDenseVector<Type> CMatrix< Type >::getDiagonal ( ) const

Returns the diagonal of the matrix as a CDenseVector instance.

Returns
CDenseVector containing the elements of the matrix diagonal.
template<class Type>
const Type& CMatrix< Type >::getElement ( int  i,
int  j 
) const

Return the value of the element in row i column j.

Parameters
iRow index.
jColumn index,
Returns
Value of element.
template<class Type>
CDenseVector<Type> CMatrix< Type >::getRow ( int  iRow) const

Return a row as a CDenseVector instance.

Parameters
iRowIndex of row to return.
Returns
CDenseVector containing row.
template<class Type>
int CMatrix< Type >::importFromMatFile ( const char *  acPath,
const char *  acVarname 
)
template<class Type>
void CMatrix< Type >::insert ( int  i,
int  j,
const CMatrix< Type > &  rtSubmatrix 
)

Insert the submatrix starting at row i column j.

Parameters
iRow offset.
jColumn offset.
rtSubmatrix.
template<class Type>
CMatrix<Type> CMatrix< Type >::invert ( ) const

Calculates the inverse matrix using LU decomposition.

Returns
Inverse matrix
template<class Type>
bool CMatrix< Type >::isNaN ( ) const

Look for NaN elements

Returns
True if matrix contains NaN elements, false otherwise
template<class Type>
Type CMatrix< Type >::maxElement ( int &  rtX,
int &  rtY 
) const

Get index of max element

Parameters
rtXrow index of maximal element
rtYcolumn index of maximal element
Returns
Maximal element value
template<class Type>
Type CMatrix< Type >::minElement ( int &  rtX,
int &  rtY 
) const

Get index of min element

Parameters
rtXrow index of minimal element
rtYcolumn index of minimal element
Returns
Minimal element value
template<class Type>
Type& CMatrix< Type >::operator() ( int  i,
int  j 
)

Return a reference to the element in row i column j.

Parameters
iRow index.
jColumn index,
Returns
Value of element.
template<class Type>
CMatrix<Type> CMatrix< Type >::operator() ( int  i,
int  j,
int  rn,
int  cn 
) const

Return the submatrix starting at row i column j with extend rn and cn.

Parameters
iRow offset.
jColumn offset.
rnRow extend
cnColumn extend
Returns
Submatrix.
template<class Type>
void CMatrix< Type >::operator*= ( Type  rScalar)

Multiply each element by a scalar

 @param tScalar.
template<class Type>
void CMatrix< Type >::operator+= ( Type  rScalar)

Add scalar to each element

Parameters
tScalar.
template<class Type>
void CMatrix< Type >::operator+= ( const CMatrix< Type > &  tM)

Add matrix

Parameters
Matrix.
template<class Type>
void CMatrix< Type >::operator-= ( Type  rScalar)

Subtract from to each element

Parameters
tScalar.
template<class Type>
void CMatrix< Type >::operator-= ( const CMatrix< Type > &  tM)

Subtract matrix

Parameters
Matrix.
template<class Type>
void CMatrix< Type >::operator/= ( Type  rScalar)

Divied each element by tScalar

Parameters
tScalar.
template<class Type>
CMatrix<Type>& CMatrix< Type >::operator= ( const CMatrix< Type > &  rtSrc)

Assign matrix instance to a matrix variable.

Parameters
Matrix.
template<class Type>
void CMatrix< Type >::replicateMatrix ( int  iX,
int  iY,
const CMatrix< Type > &  tMat 
)

Fill the matrix with a iX x iY mosaic of replicates of the matrix tMat

Parameters
iXX size of mosaic
iYY size of mosaic
tMatSingle mosaic block
template<class Type>
void CMatrix< Type >::resize ( int  iRows,
int  iCols,
bool  bPreserveData = false 
)

Resize matrix.

Parameters
iRowsNumber of rows.
iColsNumber of columns.
Iftrue, current matrix data is preserved
template<class Type>
int CMatrix< Type >::rows ( ) const
inline

Return the number of rows of the matrix.

Returns
Number of rows.

References CMatrix< Type >::iRows.

template<class Type>
bool CMatrix< Type >::serialize ( fstream &  stream,
IO_MODE  mode = READ 
)
virtual

Read/write from binary stream.

The functions handles different data types e.g for reading float objects in a double instance, etc.

Parameters
fstreamReference to binary stream
modeSwitches between reading and writing
Returns
True if successful, false otherwise

Reimplemented from CObject< Type >.

template<class Type>
bool CMatrix< Type >::serialize2 ( CArchiv tArchiv)
virtual

Reimplemented from CObject< Type >.

template<class Type>
void CMatrix< Type >::setColumn ( int  iIndex,
CDenseVector< Type > &  tVec 
)

Fill column iColumn with data given by a vector

Parameters
iColumnIndex of column to fill.
Vectorcontaining the new column data.
template<class Type>
void CMatrix< Type >::setData ( Type *  ptData,
bool  bColumnMajorOrder = false 
)

Fill matrix with data fromm array.

Parameters
ptDataArray of size rows() * cols() with data
bColumnMajorOrderIf true, data is assumed to be in column-major order, row-major order otherwise (default: false)
template<class Type>
void CMatrix< Type >::setDiagonal ( Type  tValue)

Set each elements of the diagonal to tValue

Parameters
tValueValue.
template<class Type>
void CMatrix< Type >::setDiagonal ( CDenseVector< Type > &  tDiag)

Fill matrix dagonal with data from vector

Parameters
Referenceto vector with new diagonal elements
template<class Type>
void CMatrix< Type >::setElement ( int  i,
int  j,
const Type &  tValue 
)

Assign a new value to the element in row i column j.

Parameters
iRow index.
jColumn index,
tValueNew value of element.
template<class Type>
void CMatrix< Type >::setRow ( int  iRow,
const CDenseVector< Type > &  tVec 
)

Fill row iRow with data given by a vector

Parameters
iRowIndex of row to fill.
Vectorcontaining the new row data.

Referenced by CDataset< Type >::getInputMatrix(), and CDataset< Type >::getOutputMatrix().

template<class Type>
Type CMatrix< Type >::sumElements ( ) const

Sum elements

Returns
Sum of elements.
template<class Type>
Type CMatrix< Type >::trace ( ) const

Calculate the trace of the matrix.

Returns
Trace of matrix.
template<class Type>
CMatrix<Type> CMatrix< Type >::transpose ( )

Transposes the matrix.

Returns
Transposed matrix.
template<class Type>
void CMatrix< Type >::zeros ( void  )
inline

Friends And Related Function Documentation

template<class Type>
CMatrix<Type> elementDiv ( const CMatrix< Type > &  rtMat1,
const CMatrix< Type > &  rtMat2 
)
friend

Elementwise division of two matrices with returned result.

template<class Type>
CMatrix<Type> elementMul ( const CMatrix< Type > &  rtMat1,
const CMatrix< Type > &  rtMat2 
)
friend

Elementwise multiplication of two matrices with returned result.

template<class Type>
void load ( CMatrix< Type > &  ,
char *  acString 
)
friend

Load matrix

template<class Type>
CMatrix<Type> operator* ( const CMatrix< Type > &  tMat1,
const CMatrix< Type > &  tMat2 
)
friend

Multiplication of two matrices with returned result.

template<class Type>
CMatrix<Type> operator* ( const CMatrix< Type > &  rtMat,
const Type &  rtScalar 
)
friend

Multiplication of a scalar with each element with returned result.

template<class Type>
CDenseVector<Type> operator* ( const CMatrix< Type > &  rtMat,
const CVector< Type > &  rtVec 
)
friend

Vector matrix multiplication

template<class Type>
CMatrix<Type> operator+ ( const CMatrix< Type > &  rtMat1,
const CMatrix< Type > &  rtMat2 
)
friend

Elementwise addition of two matrices with returned result.

template<class Type>
CMatrix<Type> operator+ ( const CMatrix< Type > &  rtMat,
const Type &  rtScalar 
)
friend

Addition of a scalar to each element with returned result. ^

template<class Type>
CMatrix<Type> operator- ( const CMatrix< Type > &  rtMat1,
const CMatrix< Type > &  rtMat2 
)
friend

Elementwise subtraction of two matrices with returned result.

template<class Type>
CMatrix<Type> operator- ( const CMatrix< Type > &  rtMat,
const Type &  rtScalar 
)
friend

Subtraction of a scalar from each element with returned result.

template<class Type>
CMatrix<Type> operator/ ( const CMatrix< Type > &  rtMat,
const Type &  rtScalar 
)
friend

Division of each element by a scalar with returned result.

template<class Type>
void save ( const CMatrix< Type > &  ,
char *  acString 
)
friend

Save matrix

Member Data Documentation

template<class Type>
int CMatrix< Type >::iCols
protected

Number of columns

Referenced by CMatrix< Type >::columns(), and CMatrix< Type >::zeros().

template<class Type>
int CMatrix< Type >::iRows
protected

Number of rows

Referenced by CMatrix< Type >::rows(), and CMatrix< Type >::zeros().

template<class Type>
Type* CMatrix< Type >::ptData
protected

Data storage / column-major order

Referenced by CMatrix< Type >::_data(), and CMatrix< Type >::zeros().


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