cgma
|
#include <CubitSparseMatrix.hpp>
Public Member Functions | |
CubitSparseMatrix () | |
CubitSparseMatrix (int numRows, int numCols, std::vector< int > &is, std::vector< int > &js, std::vector< double > &es) | |
~CubitSparseMatrix () | |
void | reset (int numRows, int numCols, std::vector< int > &is, std::vector< int > &js, std::vector< double > &es) |
void | clear () |
void | print (char *filename=0) const |
void | add (int row, int col, double data) |
int | num_non_zeros (void) const |
void | num_non_zeros_per_row (double &ave, int &max, int &min) const |
int | num_rows (void) const |
int | num_cols (void) const |
const std::map< int, double > * | get_row (int row) const |
void | sub_matrix (const std::vector< bool > &rows_to_include, const std::vector< bool > &cols_to_include, CubitSparseMatrix &submatrix) |
std::vector< double > | operator* (const std::vector< double > &vec) const |
void | plus_identity () |
Private Member Functions | |
void | delete_data (void) |
Private Attributes | |
std::map< int, std::map< int, double > * > | matrixData |
int | numRows |
int | numCols |
Definition at line 18 of file CubitSparseMatrix.hpp.
Definition at line 49 of file CubitSparseMatrix.cpp.
CubitSparseMatrix::CubitSparseMatrix | ( | int | numRows, |
int | numCols, | ||
std::vector< int > & | is, | ||
std::vector< int > & | js, | ||
std::vector< double > & | es | ||
) |
Definition at line 141 of file CubitSparseMatrix.cpp.
{ delete_data(); }
void CubitSparseMatrix::add | ( | int | row, |
int | col, | ||
double | data | ||
) |
Definition at line 107 of file CubitSparseMatrix.cpp.
{ if ( row+1 > numRows || col+1 > numCols ) { throw std::invalid_argument( "The dimensions of the 2 matrices must be the same." ); } map<int,double> *submap = NULL; map<int,map<int,double>*>::iterator iter = matrixData.find( row ); if ( iter == matrixData.end() ) { submap = new map<int,double>; matrixData.insert( std::pair<int, map<int,double>*>(row, submap) ); } else { submap = iter->second; map<int,double>::iterator iter2 = submap->find( col ); if ( iter2 != submap->end() ) { iter2->second += data; return; } } submap->insert( std::pair<int, double>( col, data ) ); }
void CubitSparseMatrix::clear | ( | ) |
Definition at line 94 of file CubitSparseMatrix.cpp.
{ numRows = 0; numCols = 0; delete_data(); }
void CubitSparseMatrix::delete_data | ( | void | ) | [private] |
Definition at line 152 of file CubitSparseMatrix.cpp.
{ while ( !matrixData.empty() ) { map<int,double> *submap = matrixData.begin()->second; matrixData.begin()->second = NULL; matrixData.erase( matrixData.begin() ); delete submap; } }
const map< int, double > * CubitSparseMatrix::get_row | ( | int | row | ) | const |
Definition at line 300 of file CubitSparseMatrix.cpp.
{ if ( row < 0 || row >= numRows ) { throw std::invalid_argument( "The row index must be greater than or equal to 0, and less than the number of rows in the matrix." ); } map<int, map<int,double>*>::const_iterator iter = matrixData.find( row ); if ( iter == matrixData.end() ) return NULL; return iter->second; }
int CubitSparseMatrix::num_cols | ( | void | ) | const [inline] |
Definition at line 47 of file CubitSparseMatrix.hpp.
{ return numCols; };
int CubitSparseMatrix::num_non_zeros | ( | void | ) | const |
Definition at line 418 of file CubitSparseMatrix.cpp.
{ int num_nz = 0; map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin(); while ( iter1 != matrixData.end() ) { //iter1->first; map<int,double>*submap = iter1->second; if ( submap ) num_nz += submap->size(); iter1++; } return num_nz; }
void CubitSparseMatrix::num_non_zeros_per_row | ( | double & | ave, |
int & | max, | ||
int & | min | ||
) | const |
Definition at line 370 of file CubitSparseMatrix.cpp.
{ max = 0; min = numCols; ave = 0.0; map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin(); while ( iter1 != matrixData.end() ) { //iter1->first; map<int,double>*submap = iter1->second; iter1++; int nz = submap->size(); ave += nz; if ( nz > max ) max = nz; if ( nz < min ) min = nz; } if ( numCols > 0 ) ave /= numCols; }
int CubitSparseMatrix::num_rows | ( | void | ) | const [inline] |
Definition at line 46 of file CubitSparseMatrix.hpp.
{ return numRows; };
vector< double > CubitSparseMatrix::operator* | ( | const std::vector< double > & | vec | ) | const |
Definition at line 259 of file CubitSparseMatrix.cpp.
{ if ( (int) vec.size() != numCols ) { throw std::invalid_argument( "The length of the input vector must be the same as the number of colums in the matrix." ); } vector<double> ans( numRows ); int i; for ( i = 0; i < numRows; i++ ) { ans[i] = 0.0; } map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin(); while ( iter1 != matrixData.end() ) { int row = iter1->first; map<int,double>*submap = iter1->second; iter1++; map<int,double>::iterator iter2 = submap->begin(); while ( iter2 != submap->end() ) { int col = iter2->first; double data = iter2->second; iter2++; ans[row] += data * vec[col]; } } return ans; }
void CubitSparseMatrix::plus_identity | ( | ) |
Definition at line 403 of file CubitSparseMatrix.cpp.
void CubitSparseMatrix::print | ( | char * | filename = 0 | ) | const |
Definition at line 319 of file CubitSparseMatrix.cpp.
{ printf( "CubitSparseMatrix::numRows: %d\n", numRows ); printf( "CubitSparseMatrix::numCols: %d\n", numCols ); int min, max; double ave; num_non_zeros_per_row( ave, max, min ); printf( "CubitSparseMatrix::Average # Non Zeros per row: %f\n", ave ); printf( "CubitSparseMatrix::Minimum # Non Zeros per row: %d\n", min ); printf( "CubitSparseMatrix::Maximum # Non Zeros per row: %d\n", max ); CubitFile fp; if ( filename ) fp.open( filename, "w" ); map<int, map<int,double>*>::const_iterator iter1 = matrixData.begin(); while ( iter1 != matrixData.end() ) { int row = iter1->first; map<int,double>*submap = iter1->second; iter1++; map<int,double>::iterator iter2 = submap->begin(); while ( iter2 != submap->end() ) { int col = iter2->first; double data = iter2->second; iter2++; if ( fp.file() ) { fprintf( fp.file(), "%d %d %f\n", row, col, data ); } else { printf( "CubitSparseMatrix::(%d,%d) : %f\n", row, col, data ); } } } }
void CubitSparseMatrix::reset | ( | int | numRows, |
int | numCols, | ||
std::vector< int > & | is, | ||
std::vector< int > & | js, | ||
std::vector< double > & | es | ||
) |
Definition at line 61 of file CubitSparseMatrix.cpp.
void CubitSparseMatrix::sub_matrix | ( | const std::vector< bool > & | rows_to_include, |
const std::vector< bool > & | cols_to_include, | ||
CubitSparseMatrix & | submatrix | ||
) |
Definition at line 172 of file CubitSparseMatrix.cpp.
{ if ( (int) rows_to_include.size() != numRows ) { throw std::invalid_argument( "The length of rows_to_include must the the same as the number of rows in the matrix." ); } if ( (int) cols_to_include.size() != numCols ) { throw std::invalid_argument( "The length of cols_to_include must the the same as the number of colums in the matrix." ); } vector<int> is; vector<int> js; vector<double> es; int i; int *row_indices = new int[numRows]; int *col_indices = new int[numCols]; int num_rows = 0; int num_cols = 0; for ( i = 0; i < numRows; i++ ) { if ( rows_to_include[i] ) { row_indices[i] = num_rows; num_rows++; } else { row_indices[i] = 0; } } for ( i = 0; i < numCols; i++ ) { if ( cols_to_include[i] ) { col_indices[i] = num_cols; num_cols++; } else { col_indices[i] = 0; } } map<int, map<int,double>*>::iterator iter1 = matrixData.begin(); while ( iter1 != matrixData.end() ) { int row = iter1->first; map<int,double>*submap = iter1->second; iter1++; if ( !rows_to_include[row] ) continue; map<int,double>::iterator iter2 = submap->begin(); while ( iter2 != submap->end() ) { int col = iter2->first; double data = iter2->second; iter2++; if ( !cols_to_include[col] ) continue; is.push_back( row_indices[row] ); js.push_back( col_indices[col] ); es.push_back( data ); } } delete [] col_indices; delete [] row_indices; submatrix.reset( num_rows, num_cols, is, js, es ); }
std::map< int, std::map< int, double> * > CubitSparseMatrix::matrixData [private] |
Definition at line 67 of file CubitSparseMatrix.hpp.
int CubitSparseMatrix::numCols [private] |
Definition at line 70 of file CubitSparseMatrix.hpp.
int CubitSparseMatrix::numRows [private] |
Definition at line 69 of file CubitSparseMatrix.hpp.