GClasses

GClasses::GSparseMatrix Class Reference

This class stores a row-compressed sparse matrix. That is, each row consists of a map from a column-index to a value. More...

#include <GSparseMatrix.h>

List of all members.

Public Types

typedef Map::const_iterator Iter

Public Member Functions

 GSparseMatrix (size_t rows, size_t cols, double defaultValue=0.0)
 Construct a sparse matrix with the specified number of rows and columns. defaultValue specifies the common value that is not stored. (Typically, defaultValue is 0, but for some applications it may make more sense to set it to UNKNOWN_REAL_VALUE.)
 GSparseMatrix (GDomNode *pNode)
 Deserializes a sparse matrix.
 ~GSparseMatrix ()
GDomNodeserialize (GDom *pDoc)
 Serializes this object.
double defaultValue ()
 Returns the default value--the common value that is not stored.
size_t rows ()
 Returns the number of rows (as if this matrix were dense)
size_t cols ()
 Returns the number of columns (as if this matrix were dense)
void fullRow (double *pOutFullRow, size_t row)
 Copies a row into a non-sparse vector.
Iter rowBegin (size_t i)
 Returns a const_iterator to the beginning of a row. The iterator references a pair, such that first is the column, and second is the value.
Iter rowEnd (size_t i)
 Returns a const_iterator to the end of a row. The iterator references a pair, such that first is the column, and second is the value.
Maprow (size_t i)
 Returns the specified sparse row.
size_t rowNonDefValues (size_t i)
 Returns the number of non-default-valued elements in the specified row.
double get (size_t row, size_t col)
 Returns the value at the specified position in the matrix. Returns the default value if no element is stored at that position.
void set (size_t row, size_t col, double val)
 Sets a value at the specified position in the matrix. (If val is the default value, it removes the element from the matrix.)
void copyFrom (GSparseMatrix *that)
 Copies values from "that" into "this". Any default-valued elements in that will be left the same. Any non-default-valued elements will be copied over the value in this. If the matrices are different sizes, any non-overlapping elements will be left at the default value, no-matter what value it has in that.
void copyFrom (GMatrix *that)
 Copies values from "that" into "this". If the matrices are different sizes, any non-overlapping elements will be left at the default value.
void newRow ()
 Adds a new empty row to this matrix.
void newRows (size_t n)
 Adds n new empty rows to this matrix.
void copyRow (Map &row)
 Adds a new row to this matrix by copying the parameter row.
GMatrixtoFullMatrix ()
 Converts to a full matrix.
void multiply (double scalar)
 Multiplies the matrix by a scalar value.
void swapColumns (size_t a, size_t b)
 Swaps the two specified columns. (This method is a lot slower than swapRows.)
void swapRows (size_t a, size_t b)
 Swaps the two specified rows. (This method is a lot faster than swapColumns.)
void shuffle (GRand *pRand, GMatrix *pLabels=NULL)
 Shuffles the rows in this matrix. If pLabels is non-NULL, then it will be shuffled in a manner that preserves corresponding rows with this sparse matrix.
GSparseMatrixsubMatrix (size_t row, size_t col, size_t height, size_t width)
 Returns a sub-matrix of this matrix.
GSparseMatrixtranspose ()
 Returns the transpose of this matrix.
void singularValueDecomposition (GSparseMatrix **ppU, double **ppDiag, GSparseMatrix **ppV, bool throwIfNoConverge=false, size_t maxIters=80)
 Performs singular value decomposition. (Takes advantage of sparsity to perform the decomposition efficiently.) Throws an exception if the default value is not 0.0.

Static Public Member Functions

static void test ()

Protected Types

typedef std::map< size_t, double > Map

Protected Member Functions

void singularValueDecompositionHelper (GSparseMatrix **ppU, double **ppDiag, GSparseMatrix **ppV, bool throwIfNoConverge, size_t maxIters)

Protected Attributes

size_t m_cols
std::vector< Mapm_rows
double m_defaultValue

Detailed Description

This class stores a row-compressed sparse matrix. That is, each row consists of a map from a column-index to a value.


Member Typedef Documentation

typedef Map::const_iterator GClasses::GSparseMatrix::Iter
typedef std::map<size_t,double> GClasses::GSparseMatrix::Map [protected]

Constructor & Destructor Documentation

GClasses::GSparseMatrix::GSparseMatrix ( size_t  rows,
size_t  cols,
double  defaultValue = 0.0 
)

Construct a sparse matrix with the specified number of rows and columns. defaultValue specifies the common value that is not stored. (Typically, defaultValue is 0, but for some applications it may make more sense to set it to UNKNOWN_REAL_VALUE.)

GClasses::GSparseMatrix::GSparseMatrix ( GDomNode pNode)

Deserializes a sparse matrix.

GClasses::GSparseMatrix::~GSparseMatrix ( )

Member Function Documentation

size_t GClasses::GSparseMatrix::cols ( ) [inline]

Returns the number of columns (as if this matrix were dense)

void GClasses::GSparseMatrix::copyFrom ( GSparseMatrix that)

Copies values from "that" into "this". Any default-valued elements in that will be left the same. Any non-default-valued elements will be copied over the value in this. If the matrices are different sizes, any non-overlapping elements will be left at the default value, no-matter what value it has in that.

void GClasses::GSparseMatrix::copyFrom ( GMatrix that)

Copies values from "that" into "this". If the matrices are different sizes, any non-overlapping elements will be left at the default value.

void GClasses::GSparseMatrix::copyRow ( Map row)

Adds a new row to this matrix by copying the parameter row.

double GClasses::GSparseMatrix::defaultValue ( ) [inline]

Returns the default value--the common value that is not stored.

void GClasses::GSparseMatrix::fullRow ( double *  pOutFullRow,
size_t  row 
)

Copies a row into a non-sparse vector.

double GClasses::GSparseMatrix::get ( size_t  row,
size_t  col 
)

Returns the value at the specified position in the matrix. Returns the default value if no element is stored at that position.

void GClasses::GSparseMatrix::multiply ( double  scalar)

Multiplies the matrix by a scalar value.

void GClasses::GSparseMatrix::newRow ( )

Adds a new empty row to this matrix.

void GClasses::GSparseMatrix::newRows ( size_t  n)

Adds n new empty rows to this matrix.

Map& GClasses::GSparseMatrix::row ( size_t  i) [inline]

Returns the specified sparse row.

Iter GClasses::GSparseMatrix::rowBegin ( size_t  i) [inline]

Returns a const_iterator to the beginning of a row. The iterator references a pair, such that first is the column, and second is the value.

Iter GClasses::GSparseMatrix::rowEnd ( size_t  i) [inline]

Returns a const_iterator to the end of a row. The iterator references a pair, such that first is the column, and second is the value.

size_t GClasses::GSparseMatrix::rowNonDefValues ( size_t  i) [inline]

Returns the number of non-default-valued elements in the specified row.

size_t GClasses::GSparseMatrix::rows ( ) [inline]

Returns the number of rows (as if this matrix were dense)

GDomNode* GClasses::GSparseMatrix::serialize ( GDom pDoc)

Serializes this object.

void GClasses::GSparseMatrix::set ( size_t  row,
size_t  col,
double  val 
)

Sets a value at the specified position in the matrix. (If val is the default value, it removes the element from the matrix.)

void GClasses::GSparseMatrix::shuffle ( GRand pRand,
GMatrix pLabels = NULL 
)

Shuffles the rows in this matrix. If pLabels is non-NULL, then it will be shuffled in a manner that preserves corresponding rows with this sparse matrix.

void GClasses::GSparseMatrix::singularValueDecomposition ( GSparseMatrix **  ppU,
double **  ppDiag,
GSparseMatrix **  ppV,
bool  throwIfNoConverge = false,
size_t  maxIters = 80 
)

Performs singular value decomposition. (Takes advantage of sparsity to perform the decomposition efficiently.) Throws an exception if the default value is not 0.0.

void GClasses::GSparseMatrix::singularValueDecompositionHelper ( GSparseMatrix **  ppU,
double **  ppDiag,
GSparseMatrix **  ppV,
bool  throwIfNoConverge,
size_t  maxIters 
) [protected]
GSparseMatrix* GClasses::GSparseMatrix::subMatrix ( size_t  row,
size_t  col,
size_t  height,
size_t  width 
)

Returns a sub-matrix of this matrix.

void GClasses::GSparseMatrix::swapColumns ( size_t  a,
size_t  b 
)

Swaps the two specified columns. (This method is a lot slower than swapRows.)

void GClasses::GSparseMatrix::swapRows ( size_t  a,
size_t  b 
)

Swaps the two specified rows. (This method is a lot faster than swapColumns.)

static void GClasses::GSparseMatrix::test ( ) [static]
GMatrix* GClasses::GSparseMatrix::toFullMatrix ( )

Converts to a full matrix.

GSparseMatrix* GClasses::GSparseMatrix::transpose ( )

Returns the transpose of this matrix.


Member Data Documentation

size_t GClasses::GSparseMatrix::m_cols [protected]
std::vector<Map> GClasses::GSparseMatrix::m_rows [protected]