Sparse matrix storage based on rows. More...
#include <SparseMatrix.h>
Public Types | |
typedef Row::Iterator | ColumnIterator |
typedef Row::ConstIterator | ConstColumnIterator |
typedef QHash< int, Row > ::ConstIterator | ConstRowIterator |
typedef QHash< int, T > | Row |
typedef QHash< int, Row >::Iterator | RowIterator |
Public Member Functions | |
Row & | addRow (int row) |
T | at (int row, int col) const |
T & | at (int row, int col) |
ConstRowIterator | begin () const |
RowIterator | begin () |
void | clear () |
int | columnCount () const |
ConstRowIterator | end () const |
RowIterator | end () |
Matrix< T > | operator* (const Matrix< T > &m) const |
bool | operator== (const Matrix< T > &m) const |
Matrix< T > | plainMatrix () const |
int | realRowCount () const |
int | rowCount () const |
void | setColumnCount (int ncol) |
void | setPlainMatrix (const Matrix< T > &m) |
void | setRowCount (int nrow) |
SparseMatrix () | |
SparseMatrix (int nrow, int ncol) | |
QString | toUserString (int precision=6, char format='g') const |
int | valueCount () const |
Sparse matrix storage based on rows.
typedef Row::Iterator QGpCoreTools::SparseMatrix< T >::ColumnIterator |
typedef Row::ConstIterator QGpCoreTools::SparseMatrix< T >::ConstColumnIterator |
typedef QHash<int, Row>::ConstIterator QGpCoreTools::SparseMatrix< T >::ConstRowIterator |
typedef QHash<int, T> QGpCoreTools::SparseMatrix< T >::Row |
typedef QHash<int, Row>::Iterator QGpCoreTools::SparseMatrix< T >::RowIterator |
QGpCoreTools::SparseMatrix< T >::SparseMatrix | ( | ) |
{ _nrow=0; _ncol=0; }
QGpCoreTools::SparseMatrix< T >::SparseMatrix | ( | int | nrow, |
int | ncol | ||
) |
{ _nrow=nrow; _ncol=ncol; }
SparseMatrix< T >::Row & QGpCoreTools::SparseMatrix< T >::addRow | ( | int | row | ) |
Referenced by QGpCoreTools::operator>>().
{ RowIterator itr=_values.insert(row, QHash<int, T>()); return itr.value(); }
T QGpCoreTools::SparseMatrix< T >::at | ( | int | row, |
int | col | ||
) | const |
{ ConstRowIterator itr=_values.constFind(row); if(itr!=_values.end()) { const Row& rowHash=itr.value(); ConstColumnIterator itc=rowHash.constFind(col); if(itc!=rowHash.end()) { return itc.value(); } else { return 0.0; } } else { return 0.0; } }
T & QGpCoreTools::SparseMatrix< T >::at | ( | int | row, |
int | col | ||
) |
{ RowIterator itr=_values.find(row); if(itr!=_values.end()) { Row& rowHash=itr.value(); ColumnIterator itc=rowHash.find(col); if(itc!=rowHash.end()) { return itc.value(); } else { ColumnIterator itc=rowHash.insert(col, 0.0); return itc.value(); } } else { Row& rowHash=addRow(row); ColumnIterator itc=rowHash.insert(col, 0.0); return itc.value(); } }
ConstRowIterator QGpCoreTools::SparseMatrix< T >::begin | ( | ) | const [inline] |
Referenced by QGpCoreTools::operator<<().
{return _values.begin();}
RowIterator QGpCoreTools::SparseMatrix< T >::begin | ( | ) | [inline] |
{return _values.begin();}
void QGpCoreTools::SparseMatrix< T >::clear | ( | ) |
Referenced by QGpCoreTools::operator>>().
{ _nrow=0; _ncol=0; _values.clear(); }
int QGpCoreTools::SparseMatrix< T >::columnCount | ( | ) | const [inline] |
{return _ncol;}
ConstRowIterator QGpCoreTools::SparseMatrix< T >::end | ( | ) | const [inline] |
Referenced by QGpCoreTools::operator<<().
{return _values.end();}
RowIterator QGpCoreTools::SparseMatrix< T >::end | ( | ) | [inline] |
{return _values.end();}
Matrix< T > QGpCoreTools::SparseMatrix< T >::operator* | ( | const Matrix< T > & | m | ) | const |
References QGpCoreTools::Matrix< T >::at(), and QGpCoreTools::Matrix< T >::columnCount().
{ Matrix<T> r(_nrow, m.columnCount()); for(int ir=0; ir<_nrow; ir++) { ConstRowIterator itr=_values.constFind(ir); if(itr!=_values.end()) { const Row& rowHash=itr.value(); for(int ic=0; ic<m.columnCount(); ic++) { T s=0.0; for(ConstColumnIterator itc=rowHash.begin(); itc!=rowHash.end(); itc++) { s+=itc.value()*m.at(itc.key(), ic); } r.at(ir, ic)=s; } } else { for(int ic=0; ic<r.columnCount(); ic++) { r.at(ir,ic)=0.0; } } } return r; }
bool QGpCoreTools::SparseMatrix< T >::operator== | ( | const Matrix< T > & | m | ) | const |
References QGpCoreTools::Matrix< T >::at(), QGpCoreTools::Matrix< T >::columnCount(), and QGpCoreTools::Matrix< T >::rowCount().
{ if(_nrow!=m.rowCount() || _ncol!=m.columnCount()) { return false; } for(int ir=0; ir<_nrow; ir++) { for(int ic=0; ic<_ncol; ic++) { const T& v=m.at(ir, ic); if(v!=0.0 && at(ir, ic)!=v) { return false; } } } return true; }
Matrix< T > QGpCoreTools::SparseMatrix< T >::plainMatrix | ( | ) | const |
References QGpCoreTools::Matrix< T >::at().
{ Matrix<T> m(_nrow, _ncol); for(int ir=0; ir<_nrow; ir++) { ConstRowIterator itr=_values.constFind(ir); if(itr!=_values.end()) { const Row& rowHash=itr.value(); for(int ic=0; ic<_ncol; ic++) { ConstColumnIterator itc=rowHash.constFind(ic); if(itc!=rowHash.end()) { m.at(ir, ic)=itc.value(); } else { m.at(ir, ic)=0.0; } } } else { for(int ic=0; ic<_ncol; ic++) { m.at(ir,ic)=0.0; } } } return m; }
int QGpCoreTools::SparseMatrix< T >::realRowCount | ( | ) | const [inline] |
{return _values.count();}
int QGpCoreTools::SparseMatrix< T >::rowCount | ( | ) | const [inline] |
{return _nrow;}
void QGpCoreTools::SparseMatrix< T >::setColumnCount | ( | int | ncol | ) |
Referenced by QGpCoreTools::operator>>().
{ _ncol=ncol; for(RowIterator itr=_values.begin(); itr!=_values.end(); itr++) { Row& rowHash=itr.value(); for(ColumnIterator itc=rowHash.begin(); itc!=rowHash.end(); itc++) { if(itc.key()>=ncol) { rowHash.remove(itc.key()); } } } }
void QGpCoreTools::SparseMatrix< T >::setPlainMatrix | ( | const Matrix< T > & | m | ) |
References QGpCoreTools::Matrix< T >::at(), QGpCoreTools::Matrix< T >::columnCount(), and QGpCoreTools::Matrix< T >::rowCount().
{ clear(); setRowCount(m.rowCount()); setColumnCount(m.columnCount()); for(int ir=0; ir<_nrow; ir++) { for(int ic=0; ic<_ncol; ic++) { const T& v=m.at(ir, ic); if(v!=0.0) { at(ir, ic)=v; } } } }
void QGpCoreTools::SparseMatrix< T >::setRowCount | ( | int | nrow | ) |
Referenced by QGpCoreTools::operator>>().
{ _nrow=nrow; for(RowIterator itr=_values.begin(); itr!=_values.end(); itr++) { if(itr.key()>=nrow) { _values.remove(itr.key()); } } }
QString QGpCoreTools::SparseMatrix< T >::toUserString | ( | int | precision = 6 , |
char | format = 'g' |
||
) | const |
References str, and QGpCoreTools::Number::toDouble().
{ QString str(" | "); for(int j=0; j<_ncol; j++) { str+=QString(" %1 |").arg(j, 12, 10, QChar(' ')); } str+="\n----|-"; for(int j=0; j<_ncol; j++) { str+="--------------|"; } str+="\n"; for(int i=0; i<_nrow; i++) { str+=QString("%1 | ").arg(i, 3, 10, QChar(' ')); for(int j=0; j<_ncol; j++) { str+=QString(" %1 |").arg(Number::toDouble(at(i, j)), 12, format, precision, QChar(' ')); } str+="\n"; } str+="----|-"; for(int j=0; j<_ncol; j++) { str+="--------------|"; } str+="\n"; return str; }
int QGpCoreTools::SparseMatrix< T >::valueCount | ( | ) | const |
{ int n=0; for(ConstRowIterator itr=_values.begin(); itr!=_values.end(); itr++) { const Row& rowHash=itr.value(); n+=rowHash.count(); } return n; }