golib  0.5
goSparseMatrix Class Reference

Basic sparse matrix class with Matlab mxArray support. More...

#include <gosparsematrix.h>

Public Types

enum  { UNSORTED, ROW_WISE, COLUMN_WISE }
 

Public Member Functions

 goSparseMatrix (int rows=0, int cols=0)
 
void init ()
 
void setSize (int rows, int cols)
 
void set (int row, int col, double value)
 Not implemented. More...
 
int getElementCount () const
 Get the total number of nonzero elements. More...
 
int getSortType () const
 
bool fillBegin (int elementCount)
 Begin filling elementCount elements in this matrix. More...
 
int fillNext (int row, int col, double value)
 Fills the next element in this matrix. More...
 
void fillEnd (int sortType=ROW_WISE)
 Ends filling the matrix. More...
 
bool matrixVectorMult (goArray< goDouble > &ret, const goArray< goDouble > &v)
 (this) * v More...
 
template<class Tv >
bool matrixVectorMult (goMath::Vector< Tv > &ret, const goMath::Vector< Tv > &v)
 (this) * v More...
 
bool vectorMatrixMult (goArray< goDouble > &ret, const goArray< goDouble > &v)
 v' * (this) More...
 
bool matrixVectorMult (goSparseMatrix &ret, const goArray< goDouble > &v)
 (this) * v More...
 
template<class Tv >
bool matrixVectorMult (goSparseMatrix &ret, const goMath::Vector< Tv > &v)
 (this) * v More...
 
bool vectorMatrixMult (goSparseMatrix &ret, const goArray< goDouble > &v)
 v' * (this) More...
 
bool matrixMatrixMult (goSparseMatrix &ret, goSparseMatrix &m)
 
bool matrixMatrixAdd (goSparseMatrix &ret, goSparseMatrix &m)
 
bool matrixMatrixSubtract (goSparseMatrix &ret, goSparseMatrix &m)
 
goSparseMatrixoperator*= (goDouble scalar)
 
goSparseMatrix operator* (const goSparseMatrix &other) const
 
template<class Tv >
goMath::Vector< Tv > operator* (const goMath::Vector< Tv > &v) const
 
goSparseMatrix operator* (goDouble s) const
 
goSparseMatrix operator+ (const goSparseMatrix &m) const
 
goSparseMatrix operator- (const goSparseMatrix &m) const
 
goIndex_t row (goIndex_t elementIndex) const
 Returns the row of the element at elementIndex.
 
goIndex_t column (goIndex_t elementIndex) const
 Returns the row of the element at elementIndex.
 
goDouble value (goIndex_t elementIndex) const
 Returns the value of the element at elementIndex.
 
int getRowCount () const
 Returns the number of rows.
 
int getColumnCount () const
 Returns the number of columns.
 
const goArray< goIndex_t > & getRowStart () const
 For row-sorted matrices.
 
const goArray< goIndex_t > & getColStart () const
 For column-sorted matrices.
 
void sortRows (bool sort_columns=false)
 Sorts in ascending row order. More...
 
void sortColumns (bool sort_rows=false)
 Sorts in ascending column order. More...
 
void findRows ()
 
void findColumns ()
 
bool appendRow (const goSparseMatrix &r)
 Appends the row matrix r to this matrix. More...
 
bool appendRows (const goSparseMatrix &m)
 
void transpose ()
 
goArray< goIndex_t > & getColIndex ()
 
const goArray< goIndex_t > & getColIndex () const
 
goArray< goIndex_t > & getRowIndex ()
 
const goArray< goIndex_t > & getRowIndex () const
 
goArray< goDouble > & getValues ()
 
const goArray< goDouble > & getValues () const
 

Protected Member Functions

void setSortType (int t)
 

Detailed Description

Basic sparse matrix class with Matlab mxArray support.

Provides facilities to fill and read a sparse matrix without the pain in the $#! caused by Matlab's C interface.

However, there are a few things to remember:

  • NEVER fill in two values at the same coordinate. The data you fill in may not contain doubles.
  • If you don't like this/need something more forgiving, please extend this class. Please do not add double-checks in fillNext() since I fear this will slow it down too much.

It is not too well tested but worked so far for me. There are some more or less limited multiplication functions for goArray and other goSparseMatrix objects. Extend as needed.

To fill a mxArray with 3 values, you could do:

#include "gosparsematrix.h"
...
mxArray* myArray; // myArray must exist and be allocated in the right size.
...
goSparseMatrix m (100,100); // Say myArray is 100x100.
m.fillBegin (3); // Fill in 3 values
m.fillNext (1,2,6.0);
m.fillNext (5,2,4.0);
m.fillNext (4,5,4.0);
m.fillEnd (); // Done.
if (goGetMatlabSparse (myArray, m))
printf ("All is well!\n");
// myArray should now contain the above values.
...
Todo:

Make a "get all nonzero elements" method.

TEST ADD / MULT / SUBTRACT METHODS THOROUGHLY!

Bug:
See todo, do tests.
Author
Christian Gosch

The documentation for this class was generated from the following file: