#include <matrix.h>
Public Member Functions | |
void | copySubBlock (int startRow, int startCol, int rows, int cols, const Matrix &m, int startMRow, int startMCol) |
Copies a block of the matrix m (dense or sparse) into a block of this matrix. | |
virtual const double & | elem (int m, int n) const |
Not inlined as it is overloaded in SparseMatrix. | |
virtual double & | elem (int m, int n) |
virtual void | getData (std::vector< double > *data) const |
Returns a copy of the data as a column major vector of doubles. | |
virtual std::auto_ptr< double > | getDataCopy () const |
Returns an auto_ptr to a copy of the data. | |
virtual double * | getDataPointer () |
There is no data pointer for a sparse matrix, just dies. | |
virtual double | getDefault () const |
Returns the default value for the elements of this matrix. | |
virtual Type | getType () const |
virtual bool | nextSequentialElement (int &i, int &j, double &val) const |
Returns the next element, in sequential access. | |
virtual int | numElements () const |
Returns the number of elements set explicitly, the size of the sparse matrix. | |
virtual void | resize (int m, int n) |
Resizes the matrix. All current data is lost. | |
virtual void | sequentialReset () const |
Resets the sequential access to the matrix. | |
virtual void | setAllElements (double val) |
SparseMatrix (const SparseMatrix &SM) | |
Copy constructor just copies the map. | |
SparseMatrix (int m, int n, double defaultValue=0.0) | |
Sets up an empty sparse matrix. Copy a dense sub-matrix into it to populate it. | |
virtual void | transpose () |
Transposes this matrix in place. | |
~SparseMatrix () | |
Static Public Member Functions | |
static SparseMatrix | EYE (int m, int n) |
An identity matrix of the given size. | |
static SparseMatrix | NEGEYE (int m, int n) |
A negated identity matrix of the given size. | |
Private Member Functions | |
int | key (int m, int n) const |
Creates a single integer key from a row and column index. | |
void | reverseKey (int k, int &m, int &n) const |
Given a key, computes back the row and column that created it. | |
Private Attributes | |
double | mDefaultValue |
The default value for the non-specified elements of this matrix. | |
double | mFoo |
A ghost value used for hacking. | |
std::map< int, double > ::const_iterator | mSequentialIt |
std::map< int, double > | mSparseData |
This hold the actual values. |
This is just a stump of a Sparse Matrix class. Some of its functionality is still incomplete and untested!
A Sparse matrix has a key restriction: its elements can be individually accessed ONLY of the matrix is const. This means that many operations that work on the Matrix class will die as soon as they try to access elements in the sparse class. Examples include eye(), etc. At some point, I hope to overload these function in the SparseMatrix class to do the right thing. Still, somebody might then extend the Matrix class with some function that will not work on the SparseMatrix; that would be a problem.
A sparse matrix can be created with useful values by copying a dense matrix into it, or as a block of dense matrices.
This is really just a hack for using sparse matrices; I have found that a nice design where dense and sparse matrices can be used inter-changeably and the user is not allowed to mess things up is really really hard. Ideally, there should be a Matrix pure abstract class and then DenseMatrix and SparseMatrix implementations, but that turned out to be more difficult than I thought.
Probably the way forward is to replace the inner data storage of these classes with Boost matrices.
For sequential access to non-zero elements in linear time, see the documentation of the parent Matrix class.
Definition at line 242 of file matrix.h.
SparseMatrix::SparseMatrix | ( | int | m, | |
int | n, | |||
double | defaultValue = 0.0 | |||
) | [inline] |
SparseMatrix::SparseMatrix | ( | const SparseMatrix & | SM | ) |
Copy constructor just copies the map.
Definition at line 112 of file matrix.cpp.
void SparseMatrix::copySubBlock | ( | int | startRow, | |
int | startCol, | |||
int | rows, | |||
int | cols, | |||
const Matrix & | m, | |||
int | startMRow, | |||
int | startMCol | |||
) | [virtual] |
Copies a block of the matrix m (dense or sparse) into a block of this matrix.
Reimplemented from Matrix.
Definition at line 509 of file matrix.cpp.
const double & SparseMatrix::elem | ( | int | m, | |
int | n | |||
) | const [virtual] |
Not inlined as it is overloaded in SparseMatrix.
Reimplemented from Matrix.
Definition at line 159 of file matrix.cpp.
double & SparseMatrix::elem | ( | int | m, | |
int | n | |||
) | [virtual] |
This is not implemented yet, as the sparse matrix can not return a non-const reference to one of ots elements; that element might not exist anywhere in memory, precisely because it is a sparse matrix.
Reimplemented from Matrix.
Definition at line 150 of file matrix.cpp.
SparseMatrix SparseMatrix::EYE | ( | int | m, | |
int | n | |||
) | [static] |
An identity matrix of the given size.
Reimplemented from Matrix.
Definition at line 215 of file matrix.cpp.
void SparseMatrix::getData | ( | std::vector< double > * | data | ) | const [virtual] |
Returns a copy of the data as a column major vector of doubles.
Reimplemented from Matrix.
Definition at line 182 of file matrix.cpp.
std::auto_ptr< double > SparseMatrix::getDataCopy | ( | ) | const [virtual] |
Returns an auto_ptr to a copy of the data.
Reimplemented from Matrix.
Definition at line 167 of file matrix.cpp.
double * SparseMatrix::getDataPointer | ( | ) | [virtual] |
There is no data pointer for a sparse matrix, just dies.
Reimplemented from Matrix.
Definition at line 196 of file matrix.cpp.
virtual double SparseMatrix::getDefault | ( | ) | const [inline, virtual] |
virtual Type SparseMatrix::getType | ( | ) | const [inline, virtual] |
int SparseMatrix::key | ( | int | m, | |
int | n | |||
) | const [inline, private] |
SparseMatrix SparseMatrix::NEGEYE | ( | int | m, | |
int | n | |||
) | [static] |
A negated identity matrix of the given size.
Reimplemented from Matrix.
Definition at line 239 of file matrix.cpp.
bool SparseMatrix::nextSequentialElement | ( | int & | i, | |
int & | j, | |||
double & | val | |||
) | const [virtual] |
Returns the next element, in sequential access.
Reimplemented from Matrix.
Definition at line 439 of file matrix.cpp.
virtual int SparseMatrix::numElements | ( | ) | const [inline, virtual] |
void SparseMatrix::resize | ( | int | m, | |
int | n | |||
) | [virtual] |
Resizes the matrix. All current data is lost.
Reimplemented from Matrix.
Definition at line 121 of file matrix.cpp.
void SparseMatrix::reverseKey | ( | int | k, | |
int & | m, | |||
int & | n | |||
) | const [inline, private] |
void SparseMatrix::sequentialReset | ( | ) | const [virtual] |
Resets the sequential access to the matrix.
Reimplemented from Matrix.
Definition at line 433 of file matrix.cpp.
virtual void SparseMatrix::setAllElements | ( | double | val | ) | [inline, virtual] |
void SparseMatrix::transpose | ( | ) | [virtual] |
Transposes this matrix in place.
Reimplemented from Matrix.
Definition at line 565 of file matrix.cpp.
double SparseMatrix::mDefaultValue [private] |
double SparseMatrix::mFoo [private] |
std::map<int, double>::const_iterator SparseMatrix::mSequentialIt [mutable, private] |
std::map<int, double> SparseMatrix::mSparseData [private] |