GClasses
GClasses::GSparseMatrix Class Reference

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.

#include <GSparseMatrix.h>

Public Types

typedef SparseVec::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.) More...
 
 GSparseMatrix (const GDomNode *pNode)
 Deserializes a sparse matrix. More...
 
 ~GSparseMatrix ()
 
void clear ()
 Empties the contents of this matrix. More...
 
size_t cols () const
 Returns the number of columns (as if this matrix were dense) More...
 
void copyFrom (const GSparseMatrix *that)
 Copies values from "that" into "this". Keeps values in "this" that are not overwritten by "that". 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. More...
 
void copyFrom (const 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. More...
 
void copyRow (SparseVec &row)
 Adds a new row to this matrix by copying the parameter row. More...
 
double defaultValue ()
 Returns the default value–the common value that is not stored. More...
 
void deleteLastRow ()
 Delete the last row in this sparse matrix. (Note that you can call swapRows to move any row you want into the last position before you call this method.) More...
 
GMatrixfirstPrincipalComponents (size_t k, GRand &rand)
 Returns a k-row dense matrix containing the first k principal components of this sparse matrix. More...
 
void fullRow (GVec &outFullRow, size_t row)
 Copies a row into a non-sparse vector. More...
 
double get (size_t row, size_t col) const
 Returns the value at the specified position in the matrix. Returns the default value if no element is stored at that position. More...
 
void multiply (double scalar)
 Multiplies the matrix by a scalar value. More...
 
GMatrixmultiply (GMatrix *pThat, bool transposeThat)
 Multiplies this sparse matrix by pThat dense matrix, and returns the resulting dense matrix. If transposeThat is true, then it multiplies by the transpose of pThat. More...
 
void newRow ()
 Adds a new empty row to this matrix. More...
 
void newRows (size_t n)
 Adds n new empty rows to this matrix. More...
 
void principalComponentAboutOrigin (GVec &outVector, GRand *pRand)
 Computes the first principal component about the origin. (This method expects the default value to be 0.0.) The size of pOutVector will be the number of columns in this matrix. (To compute the next principal component, call RemoveComponent, then call this method again.) More...
 
void removeComponentAboutOrigin (const GVec &component)
 Removes the specified component, assuming the mean is at the origin. More...
 
void resize (size_t rows, size_t cols)
 Modifies this matrix such that it now has dimensions 'rows' x 'cols'. More...
 
SparseVecrow (size_t i)
 Returns the specified sparse row. More...
 
Iter rowBegin (size_t i) const
 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. More...
 
Iter rowEnd (size_t i) const
 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. More...
 
size_t rowNonDefValues (size_t i)
 Returns the number of non-default-valued elements in the specified row. More...
 
size_t rows () const
 Returns the number of rows (as if this matrix were dense) More...
 
GDomNodeserialize (GDom *pDoc) const
 Serializes this object. More...
 
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.) More...
 
void shuffle (GRand *pRand, GMatrix *pLabels=NULL)
 Shuffles the rows in this matrix. If pLabels is non-NULL, then it will also be shuffled in a manner that preserves corresponding rows with this sparse matrix. More...
 
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. More...
 
GSparseMatrixsubMatrix (size_t row, size_t col, size_t height, size_t width)
 Returns a sub-matrix of this matrix. More...
 
void swapColumns (size_t a, size_t b)
 Swaps the two specified columns. (This method is a lot slower than swapRows.) More...
 
void swapRows (size_t a, size_t b)
 Swaps the two specified rows. (This method is a lot faster than swapColumns.) More...
 
GMatrixtoFullMatrix ()
 Converts to a full matrix. More...
 
GSparseMatrixtranspose ()
 Returns the transpose of this matrix. More...
 

Static Public Member Functions

static void test ()
 

Protected Member Functions

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

Protected Attributes

size_t m_cols
 
double m_defaultValue
 
std::vector< SparseVecm_rows
 

Member Typedef Documentation

typedef SparseVec::const_iterator GClasses::GSparseMatrix::Iter

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 ( const GDomNode pNode)

Deserializes a sparse matrix.

GClasses::GSparseMatrix::~GSparseMatrix ( )

Member Function Documentation

void GClasses::GSparseMatrix::clear ( )

Empties the contents of this matrix.

size_t GClasses::GSparseMatrix::cols ( ) const
inline

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

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

Copies values from "that" into "this". Keeps values in "this" that are not overwritten by "that". 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 ( const 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 ( SparseVec 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::deleteLastRow ( )

Delete the last row in this sparse matrix. (Note that you can call swapRows to move any row you want into the last position before you call this method.)

GMatrix* GClasses::GSparseMatrix::firstPrincipalComponents ( size_t  k,
GRand rand 
)

Returns a k-row dense matrix containing the first k principal components of this sparse matrix.

void GClasses::GSparseMatrix::fullRow ( GVec outFullRow,
size_t  row 
)

Copies a row into a non-sparse vector.

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

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.

GMatrix* GClasses::GSparseMatrix::multiply ( GMatrix pThat,
bool  transposeThat 
)

Multiplies this sparse matrix by pThat dense matrix, and returns the resulting dense matrix. If transposeThat is true, then it multiplies by the transpose of pThat.

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.

void GClasses::GSparseMatrix::principalComponentAboutOrigin ( GVec outVector,
GRand pRand 
)

Computes the first principal component about the origin. (This method expects the default value to be 0.0.) The size of pOutVector will be the number of columns in this matrix. (To compute the next principal component, call RemoveComponent, then call this method again.)

void GClasses::GSparseMatrix::removeComponentAboutOrigin ( const GVec component)

Removes the specified component, assuming the mean is at the origin.

void GClasses::GSparseMatrix::resize ( size_t  rows,
size_t  cols 
)

Modifies this matrix such that it now has dimensions 'rows' x 'cols'.

SparseVec& GClasses::GSparseMatrix::row ( size_t  i)
inline

Returns the specified sparse row.

Iter GClasses::GSparseMatrix::rowBegin ( size_t  i) const
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) const
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 ( ) const
inline

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

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

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 also 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
double GClasses::GSparseMatrix::m_defaultValue
protected
std::vector<SparseVec> GClasses::GSparseMatrix::m_rows
protected