25 #ifndef __SparseMatrix_hh__
26 #define __SparseMatrix_hh__
28 #include "bias_config.h"
31 #include "Base/Math/Matrix.hh"
32 #include "Base/Math/Vector.hh"
38 #define SPARSE_MATRIX_EPSILON 1e-12
59 SparseMatrix(
unsigned int numRow,
unsigned int numCol): maxRow_(0), maxCol_(0), numRow_(numRow), numCol_(numCol) {}
71 unsigned int GetRows()
const {
return maxRow_+1; }
74 unsigned int GetCols()
const {
return maxCol_+1; }
86 double GetElement(
unsigned int row,
unsigned int col)
const;
91 void SetElement(
unsigned int row,
unsigned int col,
double val);
96 void SetSubMatrix(
unsigned int row,
unsigned int col,
98 unsigned int startRowInA,
unsigned int startColInA,
99 unsigned int row_count,
unsigned int col_count);
105 void MultiplyIP(
const double multiplier);
138 void Reinit(
unsigned rows,
unsigned cols);
159 void GetAsDense(
unsigned int row,
unsigned int col,
unsigned int numRows,
163 void Print(std::string
const &name, std::ostream &stream=std::cout)
const;
168 double GetMaxDiagonalElement(
unsigned int &index)
const;
170 unsigned int index;
return GetMaxDiagonalElement(index);
176 double GetMaxRowElement(
unsigned int row,
unsigned int &col)
const;
178 unsigned int col;
return GetMaxRowElement(row, col);
185 double GetMaxColumnElement(
unsigned int col,
unsigned int &row)
const;
187 unsigned int row;
return GetMaxColumnElement(col, row);
194 void AddToDiagonal(
const double &value);
198 void MultiplyDiagonalBy(
const double &value);
206 bool CheckConsistency();
213 void GetAllElements(std::vector<unsigned int> &rows,
214 std::vector<unsigned int> &cols,
215 std::vector<double> &values)
const;
222 void InsertElement_(
unsigned int row,
unsigned int col,
double value);
228 std::map<unsigned int, std::list<std::pair<unsigned int, double> > >
rows_;
242 matrix.
Print(
"SparseMatrix", stream);
double GetMaxRowElement(unsigned int row) const
void Print(std::string const &name, std::ostream &stream=std::cout) const
Prints the contents of this matrix.
unsigned int maxRow_
Current max.
std::map< unsigned int, std::list< std::pair< unsigned int, double > > > rows_
Maps row indices to mappings from column indices to values.
double GetMaxDiagonalElement() const
SparseMatrix(unsigned int numRow, unsigned int numCol)
Default constructor for zero sparse matrix.
~SparseMatrix()
Default destructor.
unsigned int GetRows() const
Returns the maximum number of rows (max non-zero entry).
Implementation of sparse matrix operations.
unsigned int numRow_
Indicates the current number of rows and clumns.
unsigned int GetCols() const
Returns the maximum number of columns (max non-zero entry).
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
SparseMatrix()
Default constructor for zero sparse matrix.
unsigned int GetRowsNum() const
Returns the total number of rows (matrix size).
double GetMaxColumnElement(unsigned int col) const
unsigned int GetColsNum() const
Returns the total number of columns (matrix size).