Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
BIAS::SparseMatrix Class Reference

Implementation of sparse matrix operations. More...

#include <Base/Math/SparseMatrix.hh>

+ Collaboration diagram for BIAS::SparseMatrix:

Public Member Functions

void AddToDiagonal (const double &value)
 Runs across diagonal and adds value to diagonal elements. More...
 
void AppendMatrixBottom (SparseMatrix &H)
 
void AppendMatrixRight (SparseMatrix &H)
 
bool CheckConsistency ()
 Checks if this sparse matrix is still consistent, i.e. More...
 
void GetAllElements (std::vector< unsigned int > &rows, std::vector< unsigned int > &cols, std::vector< double > &values) const
 Return rows, columns and values for all elements in matrix. More...
 
void GetAsDense (BIAS::Matrix< double > &M) const
 Returns this matrix as dense BIAS matrix in M. More...
 
void GetAsDense (unsigned int row, unsigned int col, unsigned int numRows, unsigned int numCols, BIAS::Matrix< double > &M) const
 Returns submatrix as dense BIAS matrix in M. More...
 
unsigned int GetCols () const
 Returns the maximum number of columns (max non-zero entry). More...
 
unsigned int GetColsNum () const
 Returns the total number of columns (matrix size). More...
 
double GetElement (unsigned int row, unsigned int col) const
 Returns value of an element. More...
 
double GetMaxColumnElement (unsigned int col, unsigned int &row) const
 Returns maximum element at given column (at least zero). More...
 
double GetMaxColumnElement (unsigned int col) const
 
double GetMaxDiagonalElement (unsigned int &index) const
 Returns maximum element at diagonal (at least zero). More...
 
double GetMaxDiagonalElement () const
 
double GetMaxRowElement (unsigned int row, unsigned int &col) const
 Returns maximum element at given row (at least zero). More...
 
double GetMaxRowElement (unsigned int row) const
 
unsigned int GetRows () const
 Returns the maximum number of rows (max non-zero entry). More...
 
unsigned int GetRowsNum () const
 Returns the total number of rows (matrix size). More...
 
int Invert (BIAS::Matrix< double > &inverse)
 Returns inverse matrix as dense matrix. More...
 
int Invert (SparseMatrix &inverse)
 Returns inverse matrix as sparse matrix. More...
 
int InvertAndSolve (BIAS::Vector< double > &b, BIAS::Matrix< double > &inverse, BIAS::Vector< double > &result)
 Inverts matrix and returns inverse (as dense matrix) and solution of linear equation system A^(-1)*x = b in result. More...
 
int InvertAndSolve (BIAS::Vector< double > &b, SparseMatrix &inverse, BIAS::Vector< double > &result)
 Inverts matrix and returns inverse (as sparse matrix) and solution of linear equation system A^(-1)*x = b in result. More...
 
void Multiply (SparseMatrix &A, SparseMatrix &result)
 Multiplies with A and returns result M*A. More...
 
void Multiply (BIAS::Vector< double > &x, BIAS::Vector< double > &result)
 Multiplies with vector x and returns result A*x. More...
 
void MultiplyDiagonalBy (const double &value)
 Runs across diagonal and multiplies diagonal element with value. More...
 
void MultiplyIP (const double multiplier)
 Multiplies this elementwise with 'multiplier'. More...
 
void MultiplyWithTransposeOf (SparseMatrix &A, SparseMatrix &result)
 Multiplies with transpose of A and returns result M*A'. More...
 
SparseMatrixoperator= (const SparseMatrix &S)
 
void Print (std::string const &name, std::ostream &stream=std::cout) const
 Prints the contents of this matrix. More...
 
int PseudoInverse (BIAS::Matrix< double > &inverse)
 computes pseudo inverse and returns result in inverse More...
 
int PseudoInverse (BIAS::SparseMatrix &inverse)
 computes pseudo inverse and returns result in inverse More...
 
void Reinit (unsigned rows, unsigned cols)
 clears old matrix and reinits with given size More...
 
void SetElement (unsigned int row, unsigned int col, double val)
 Sets a new element or changes the value of an existing element. More...
 
void SetSubMatrix (unsigned int row, unsigned int col, BIAS::Matrix< double > const &A, unsigned int startRowInA, unsigned int startColInA, unsigned int row_count, unsigned int col_count)
 Sets a submatrix beginning at (row,col) from a dense matrix. More...
 
int Solve (BIAS::Vector< double > &b, BIAS::Vector< double > &result)
 Returns solution of linear equation system A*x = b. More...
 
 SparseMatrix ()
 Default constructor for zero sparse matrix. More...
 
 SparseMatrix (unsigned int numRow, unsigned int numCol)
 Default constructor for zero sparse matrix. More...
 
 SparseMatrix (BIAS::Matrix< double > const &A)
 Constructor from BIAS matrix. More...
 
void Transpose (SparseMatrix &result)
 Returns transposed matrix in result (as sparse matrix). More...
 
 ~SparseMatrix ()
 Default destructor. More...
 

Protected Member Functions

int GaussJordan_ ()
 Performs Gauss-Jordan algorithm on sparse matrix. More...
 
void InsertElement_ (unsigned int row, unsigned int col, double value)
 Insert an element. More...
 

Protected Attributes

unsigned int maxCol_
 
unsigned int maxRow_
 Current max. More...
 
unsigned int numCol_
 
unsigned int numRow_
 Indicates the current number of rows and clumns. More...
 
std::map< unsigned int,
std::list< std::pair< unsigned
int, double > > > 
rows_
 Maps row indices to mappings from column indices to values. More...
 

Detailed Description

Implementation of sparse matrix operations.

Author
beder, rework by esquivel
Date
01/2007, rework 11/2008,
Note
This implementation of a sparse matrix uses a flexible structure that may be extended by further elements, without the need to reallocate the data manually. This structure is realized using a list of pairs.
Examples:
ExampleSparseMatrix.cpp, and ExampleSparseMatrix2.cpp.

Definition at line 51 of file SparseMatrix.hh.

Constructor & Destructor Documentation

BIAS::SparseMatrix::SparseMatrix ( )
inline

Default constructor for zero sparse matrix.

Definition at line 56 of file SparseMatrix.hh.

BIAS::SparseMatrix::SparseMatrix ( unsigned int  numRow,
unsigned int  numCol 
)
inline

Default constructor for zero sparse matrix.

Definition at line 59 of file SparseMatrix.hh.

SparseMatrix::SparseMatrix ( BIAS::Matrix< double > const &  A)

Constructor from BIAS matrix.

Definition at line 39 of file SparseMatrix.cpp.

References BIAS::Matrix< T >::GetCols(), and BIAS::Matrix< T >::GetRows().

BIAS::SparseMatrix::~SparseMatrix ( )
inline

Default destructor.

Definition at line 66 of file SparseMatrix.hh.

Member Function Documentation

void SparseMatrix::AddToDiagonal ( const double &  value)

Runs across diagonal and adds value to diagonal elements.

Note
If diagonal elements does not exist, it is created.
Attention
Implemented only for square matrices!
Author
koeser 04/2007

Definition at line 440 of file SparseMatrix.cpp.

void SparseMatrix::AppendMatrixBottom ( SparseMatrix H)

Definition at line 590 of file SparseMatrix.cpp.

References maxCol_, maxRow_, numCol_, numRow_, and rows_.

void SparseMatrix::AppendMatrixRight ( SparseMatrix H)

Definition at line 629 of file SparseMatrix.cpp.

References maxCol_, maxRow_, numCol_, numRow_, and rows_.

bool SparseMatrix::CheckConsistency ( )

Checks if this sparse matrix is still consistent, i.e.

   order of rows and columns is strictly ascending, and there
   are no empty rows, no values below epsilon and no values
   outside of the matrix boundaries given by maxRow_, maxCol_!
Returns
Returns true if matrix is consistent, false otherwise.
Author
esquivel 11/2008

Definition at line 1209 of file SparseMatrix.cpp.

int SparseMatrix::GaussJordan_ ( )
protected

Performs Gauss-Jordan algorithm on sparse matrix.

Definition at line 1007 of file SparseMatrix.cpp.

void SparseMatrix::GetAllElements ( std::vector< unsigned int > &  rows,
std::vector< unsigned int > &  cols,
std::vector< double > &  values 
) const

Return rows, columns and values for all elements in matrix.

Parameters
rowsReturns row indices of elements.
colsReturns column indices of elements.
valuesReturns values of elements.

Definition at line 1274 of file SparseMatrix.cpp.

void SparseMatrix::GetAsDense ( BIAS::Matrix< double > &  M) const

Returns this matrix as dense BIAS matrix in M.

Definition at line 314 of file SparseMatrix.cpp.

References BIAS::Matrix< T >::GetCols(), BIAS::Matrix< T >::GetRows(), TNT::Matrix< T >::newsize(), and BIAS::Matrix< T >::SetZero().

Referenced by PseudoInverse().

void SparseMatrix::GetAsDense ( unsigned int  row,
unsigned int  col,
unsigned int  numRows,
unsigned int  numCols,
BIAS::Matrix< double > &  M 
) const

Returns submatrix as dense BIAS matrix in M.

Todo:
Not efficient because of row->column mapping...

Definition at line 494 of file SparseMatrix.cpp.

References BIAS::Matrix< T >::GetCols(), BIAS::Matrix< T >::GetRows(), TNT::Matrix< T >::newsize(), and BIAS::Matrix< T >::SetZero().

unsigned int BIAS::SparseMatrix::GetCols ( ) const
inline

Returns the maximum number of columns (max non-zero entry).

Examples:
ExampleSparseMatrix2.cpp.

Definition at line 74 of file SparseMatrix.hh.

Referenced by operator=().

unsigned int BIAS::SparseMatrix::GetColsNum ( ) const
inline

Returns the total number of columns (matrix size).

Examples:
ExampleSparseMatrix2.cpp.

Definition at line 80 of file SparseMatrix.hh.

Referenced by operator=().

double SparseMatrix::GetElement ( unsigned int  row,
unsigned int  col 
) const

Returns value of an element.

Note
Not very efficient since it has to be tested first if the element has been set already!
Author
esquivel 11/2008

Definition at line 99 of file SparseMatrix.cpp.

double SparseMatrix::GetMaxColumnElement ( unsigned int  col,
unsigned int &  row 
) const

Returns maximum element at given column (at least zero).

TODO Not efficient because of row->column mapping...

Parameters
rowReturns row index of maximum (at least 0).
Todo:
Not efficient because of row->column mapping...
Author
esquivel 11/2008

Definition at line 387 of file SparseMatrix.cpp.

double BIAS::SparseMatrix::GetMaxColumnElement ( unsigned int  col) const
inline

Definition at line 186 of file SparseMatrix.hh.

double SparseMatrix::GetMaxDiagonalElement ( unsigned int &  index) const

Returns maximum element at diagonal (at least zero).

Parameters
rowReturns diagonal index of maximum (at least 0).
Author
koeser 04/2007

Definition at line 336 of file SparseMatrix.cpp.

double BIAS::SparseMatrix::GetMaxDiagonalElement ( ) const
inline

Definition at line 169 of file SparseMatrix.hh.

double SparseMatrix::GetMaxRowElement ( unsigned int  row,
unsigned int &  col 
) const

Returns maximum element at given row (at least zero).

Parameters
colReturns column index of maximum (at least 0).
Author
esquivel 11/2008

Definition at line 364 of file SparseMatrix.cpp.

double BIAS::SparseMatrix::GetMaxRowElement ( unsigned int  row) const
inline

Definition at line 177 of file SparseMatrix.hh.

unsigned int BIAS::SparseMatrix::GetRows ( ) const
inline

Returns the maximum number of rows (max non-zero entry).

Examples:
ExampleSparseMatrix2.cpp.

Definition at line 71 of file SparseMatrix.hh.

Referenced by operator=().

unsigned int BIAS::SparseMatrix::GetRowsNum ( ) const
inline

Returns the total number of rows (matrix size).

Examples:
ExampleSparseMatrix2.cpp.

Definition at line 77 of file SparseMatrix.hh.

Referenced by operator=().

void SparseMatrix::InsertElement_ ( unsigned int  row,
unsigned int  col,
double  value 
)
protected

Insert an element.

Note
The element must NOT have been set before!

Definition at line 221 of file SparseMatrix.cpp.

int SparseMatrix::Invert ( BIAS::Matrix< double > &  inverse)

Returns inverse matrix as dense matrix.

Attention
Valid only for square matrices!
Examples:
ExampleSparseMatrix.cpp, and ExampleSparseMatrix2.cpp.

Definition at line 897 of file SparseMatrix.cpp.

References TNT::Matrix< T >::newsize(), and BIAS::Matrix< T >::SetZero().

Referenced by PseudoInverse().

int SparseMatrix::Invert ( SparseMatrix inverse)

Returns inverse matrix as sparse matrix.

Attention
Valid only for square matrices!

Definition at line 949 of file SparseMatrix.cpp.

References maxCol_, maxRow_, numCol_, numRow_, and rows_.

int SparseMatrix::InvertAndSolve ( BIAS::Vector< double > &  b,
BIAS::Matrix< double > &  inverse,
BIAS::Vector< double > &  result 
)

Inverts matrix and returns inverse (as dense matrix) and solution of linear equation system A^(-1)*x = b in result.

Attention
Valid only for square matrices!

Definition at line 672 of file SparseMatrix.cpp.

References TNT::Vector< T >::newsize(), TNT::Matrix< T >::newsize(), BIAS::Vector< T >::SetZero(), BIAS::Matrix< T >::SetZero(), and BIAS::Vector< T >::Size().

int SparseMatrix::InvertAndSolve ( BIAS::Vector< double > &  b,
SparseMatrix inverse,
BIAS::Vector< double > &  result 
)

Inverts matrix and returns inverse (as sparse matrix) and solution of linear equation system A^(-1)*x = b in result.

Attention
Valid only for square matrices!

Definition at line 735 of file SparseMatrix.cpp.

References maxCol_, maxRow_, TNT::Vector< T >::newsize(), rows_, BIAS::Vector< T >::SetZero(), and BIAS::Vector< T >::Size().

void SparseMatrix::Multiply ( SparseMatrix A,
SparseMatrix result 
)

Multiplies with A and returns result M*A.

Examples:
ExampleSparseMatrix.cpp, and ExampleSparseMatrix2.cpp.

Definition at line 247 of file SparseMatrix.cpp.

References numCol_, numRow_, and Transpose().

Referenced by PseudoInverse().

void SparseMatrix::Multiply ( BIAS::Vector< double > &  x,
BIAS::Vector< double > &  result 
)

Multiplies with vector x and returns result A*x.

Definition at line 567 of file SparseMatrix.cpp.

References TNT::Vector< T >::newsize(), and BIAS::Vector< T >::SetZero().

void SparseMatrix::MultiplyDiagonalBy ( const double &  value)

Runs across diagonal and multiplies diagonal element with value.

Author
koeser 04/2007

Definition at line 414 of file SparseMatrix.cpp.

void SparseMatrix::MultiplyIP ( const double  multiplier)

Multiplies this elementwise with 'multiplier'.

Definition at line 234 of file SparseMatrix.cpp.

void SparseMatrix::MultiplyWithTransposeOf ( SparseMatrix A,
SparseMatrix result 
)

Multiplies with transpose of A and returns result M*A'.

Definition at line 258 of file SparseMatrix.cpp.

References maxCol_, maxRow_, numCol_, numRow_, and rows_.

Referenced by PseudoInverse().

SparseMatrix & SparseMatrix::operator= ( const SparseMatrix S)

Definition at line 67 of file SparseMatrix.cpp.

References GetCols(), GetColsNum(), GetRows(), GetRowsNum(), and rows_.

void SparseMatrix::Print ( std::string const &  name,
std::ostream &  stream = std::cout 
) const

Prints the contents of this matrix.

Definition at line 546 of file SparseMatrix.cpp.

Referenced by BIAS::operator<<().

int SparseMatrix::PseudoInverse ( BIAS::Matrix< double > &  inverse)

computes pseudo inverse and returns result in inverse

Definition at line 850 of file SparseMatrix.cpp.

References GetAsDense().

int SparseMatrix::PseudoInverse ( BIAS::SparseMatrix inverse)

computes pseudo inverse and returns result in inverse

Definition at line 870 of file SparseMatrix.cpp.

References Invert(), Multiply(), and MultiplyWithTransposeOf().

void SparseMatrix::Reinit ( unsigned  rows,
unsigned  cols 
)

clears old matrix and reinits with given size

Definition at line 860 of file SparseMatrix.cpp.

void SparseMatrix::SetElement ( unsigned int  row,
unsigned int  col,
double  val 
)

Sets a new element or changes the value of an existing element.

Note
Existing elements are removed by setting value to 0!
Author
esquivel 11/2008

Definition at line 117 of file SparseMatrix.cpp.

void SparseMatrix::SetSubMatrix ( unsigned int  row,
unsigned int  col,
BIAS::Matrix< double > const &  A,
unsigned int  startRowInA,
unsigned int  startColInA,
unsigned int  row_count,
unsigned int  col_count 
)

Sets a submatrix beginning at (row,col) from a dense matrix.

NO part of the specified submatrix must have been set before!

Definition at line 150 of file SparseMatrix.cpp.

References BIAS::Matrix< T >::GetCols(), and BIAS::Matrix< T >::GetRows().

int SparseMatrix::Solve ( BIAS::Vector< double > &  b,
BIAS::Vector< double > &  result 
)

Returns solution of linear equation system A*x = b.

Examples:
ExampleSparseMatrix2.cpp.

Definition at line 803 of file SparseMatrix.cpp.

References TNT::Vector< T >::newsize(), BIAS::Vector< T >::SetZero(), and BIAS::Vector< T >::Size().

void SparseMatrix::Transpose ( SparseMatrix result)

Returns transposed matrix in result (as sparse matrix).

Examples:
ExampleSparseMatrix.cpp, and ExampleSparseMatrix2.cpp.

Definition at line 190 of file SparseMatrix.cpp.

References maxCol_, maxRow_, numCol_, numRow_, and rows_.

Referenced by Multiply().

Member Data Documentation

unsigned int BIAS::SparseMatrix::maxCol_
protected
unsigned int BIAS::SparseMatrix::maxRow_
protected

Current max.

row and column containing non-zero elements.

Definition at line 231 of file SparseMatrix.hh.

Referenced by AppendMatrixBottom(), AppendMatrixRight(), Invert(), InvertAndSolve(), MultiplyWithTransposeOf(), and Transpose().

unsigned int BIAS::SparseMatrix::numCol_
protected
unsigned int BIAS::SparseMatrix::numRow_
protected

Indicates the current number of rows and clumns.

values are negative, if the dimensions of the matrix are unknown.

Definition at line 235 of file SparseMatrix.hh.

Referenced by AppendMatrixBottom(), AppendMatrixRight(), Invert(), Multiply(), MultiplyWithTransposeOf(), and Transpose().

std::map<unsigned int, std::list<std::pair<unsigned int, double> > > BIAS::SparseMatrix::rows_
protected

Maps row indices to mappings from column indices to values.

Definition at line 228 of file SparseMatrix.hh.

Referenced by AppendMatrixBottom(), AppendMatrixRight(), Invert(), InvertAndSolve(), MultiplyWithTransposeOf(), operator=(), and Transpose().


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