All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines
Public Types | Public Member Functions
QGpCoreTools::SparseMatrix< T > Class Template Reference

Sparse matrix storage based on rows. More...

#include <SparseMatrix.h>

List of all members.

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

RowaddRow (int row)
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

Detailed Description

template<typename T>
class QGpCoreTools::SparseMatrix< T >

Sparse matrix storage based on rows.


Member Typedef Documentation

template<typename T>
typedef Row::Iterator QGpCoreTools::SparseMatrix< T >::ColumnIterator
template<typename T>
typedef Row::ConstIterator QGpCoreTools::SparseMatrix< T >::ConstColumnIterator
template<typename T>
typedef QHash<int, Row>::ConstIterator QGpCoreTools::SparseMatrix< T >::ConstRowIterator
template<typename T>
typedef QHash<int, T> QGpCoreTools::SparseMatrix< T >::Row
template<typename T>
typedef QHash<int, Row>::Iterator QGpCoreTools::SparseMatrix< T >::RowIterator

Constructor & Destructor Documentation

template<typename T >
QGpCoreTools::SparseMatrix< T >::SparseMatrix ( )
  {
    _nrow=0;
    _ncol=0;
  }
template<typename T >
QGpCoreTools::SparseMatrix< T >::SparseMatrix ( int  nrow,
int  ncol 
)
  {
    _nrow=nrow;
    _ncol=ncol;
  }

Member Function Documentation

template<typename T >
SparseMatrix< T >::Row & QGpCoreTools::SparseMatrix< T >::addRow ( int  row)

Referenced by QGpCoreTools::operator>>().

  {
    RowIterator itr=_values.insert(row, QHash<int, T>());
    return itr.value();
  }
template<typename T >
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;
    }
  }
template<typename T >
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();
    }
  }
template<typename T>
ConstRowIterator QGpCoreTools::SparseMatrix< T >::begin ( ) const [inline]

Referenced by QGpCoreTools::operator<<().

{return _values.begin();}
template<typename T>
RowIterator QGpCoreTools::SparseMatrix< T >::begin ( ) [inline]
{return _values.begin();}
template<typename T >
void QGpCoreTools::SparseMatrix< T >::clear ( )

Referenced by QGpCoreTools::operator>>().

  {
    _nrow=0;
    _ncol=0;
    _values.clear();
  }
template<typename T>
int QGpCoreTools::SparseMatrix< T >::columnCount ( ) const [inline]
{return _ncol;}
template<typename T>
ConstRowIterator QGpCoreTools::SparseMatrix< T >::end ( ) const [inline]

Referenced by QGpCoreTools::operator<<().

{return _values.end();}
template<typename T>
RowIterator QGpCoreTools::SparseMatrix< T >::end ( ) [inline]
{return _values.end();}
template<typename T >
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;
  }
template<typename T >
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;
  }
template<typename T >
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;
  }
template<typename T>
int QGpCoreTools::SparseMatrix< T >::realRowCount ( ) const [inline]
{return _values.count();}
template<typename T>
int QGpCoreTools::SparseMatrix< T >::rowCount ( ) const [inline]
{return _nrow;}
template<typename T >
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());
        }
      }
    }
  }
template<typename T >
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;
        }
      }
    }
  }
template<typename T >
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());
      }
    }
  }
template<typename T >
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;
  }
template<typename T >
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;
  }

The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines