Basic Image AlgorithmS Library
2.8.0
|
Implementation of sparse matrix operations. More...
#include <Base/Math/SparseMatrix.hh>
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... | |
SparseMatrix & | operator= (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... | |
Implementation of sparse matrix operations.
Definition at line 51 of file SparseMatrix.hh.
|
inline |
Default constructor for zero sparse matrix.
Definition at line 56 of file SparseMatrix.hh.
|
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().
|
inline |
Default destructor.
Definition at line 66 of file SparseMatrix.hh.
void SparseMatrix::AddToDiagonal | ( | const double & | value | ) |
Runs across diagonal and adds value to diagonal elements.
Definition at line 440 of file SparseMatrix.cpp.
void SparseMatrix::AppendMatrixBottom | ( | SparseMatrix & | H | ) |
void SparseMatrix::AppendMatrixRight | ( | SparseMatrix & | H | ) |
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_!
Definition at line 1209 of file SparseMatrix.cpp.
|
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.
rows | Returns row indices of elements. |
cols | Returns column indices of elements. |
values | Returns 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.
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().
|
inline |
Returns the maximum number of columns (max non-zero entry).
Definition at line 74 of file SparseMatrix.hh.
Referenced by operator=().
|
inline |
Returns the total number of columns (matrix size).
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.
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...
row | Returns row index of maximum (at least 0). |
Definition at line 387 of file SparseMatrix.cpp.
|
inline |
Definition at line 186 of file SparseMatrix.hh.
double SparseMatrix::GetMaxDiagonalElement | ( | unsigned int & | index | ) | const |
Returns maximum element at diagonal (at least zero).
row | Returns diagonal index of maximum (at least 0). |
Definition at line 336 of file SparseMatrix.cpp.
|
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).
col | Returns column index of maximum (at least 0). |
Definition at line 364 of file SparseMatrix.cpp.
|
inline |
Definition at line 177 of file SparseMatrix.hh.
|
inline |
Returns the maximum number of rows (max non-zero entry).
Definition at line 71 of file SparseMatrix.hh.
Referenced by operator=().
|
inline |
Returns the total number of rows (matrix size).
Definition at line 77 of file SparseMatrix.hh.
Referenced by operator=().
|
protected |
Insert an element.
Definition at line 221 of file SparseMatrix.cpp.
int SparseMatrix::Invert | ( | BIAS::Matrix< double > & | inverse | ) |
Returns inverse matrix as dense matrix.
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 | ) |
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.
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.
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.
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.
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.
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.
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).
Definition at line 190 of file SparseMatrix.cpp.
References maxCol_, maxRow_, numCol_, numRow_, and rows_.
Referenced by Multiply().
|
protected |
Definition at line 231 of file SparseMatrix.hh.
Referenced by AppendMatrixBottom(), AppendMatrixRight(), Invert(), InvertAndSolve(), MultiplyWithTransposeOf(), and Transpose().
|
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().
|
protected |
Definition at line 235 of file SparseMatrix.hh.
Referenced by AppendMatrixBottom(), AppendMatrixRight(), Invert(), Multiply(), MultiplyWithTransposeOf(), and Transpose().
|
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().
|
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().