Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

PLearn::RowMapSparseMatrix< T > Class Template Reference

#include <RowMapSparseMatrix.h>

Inheritance diagram for PLearn::RowMapSparseMatrix< T >:

Inheritance graph
[legend]
Collaboration diagram for PLearn::RowMapSparseMatrix< T >:

Collaboration graph
[legend]
List of all members.

Public Member Functions

 RowMapSparseMatrix (int n_rows=0, int n_columns=0)
 RowMapSparseMatrix (string filename)
 RowMapSparseMatrix (const Mat &m, bool fill_all=true)
 RowMapSparseMatrix (const SparseMatrix &sm, int n_rows, int n_cols)
 Accepts a FORTRAN formatted sparse matrix as an initializer.

void resize (int n_rows, int n_columns)
 THIS ALSO CLEARS THE MATRIX.

void clear ()
 this is equivalent to setting all values to "0" (the default value of T)

void clearRow (int i)
T & operator() (int i, int j)
const T & operator() (int i, int j) const
map< int, T > & operator() (int i)
int size () const
 NOTE THIS IS A BIT EXPENSIVE!

int length () const
int width () const
void save (string filename) const
void saveAscii (string filename)
void load (string filename)
 determines automatically whether file is binary or ascii

void read (istream &in)
void write (ostream &out) const
 mode used to write depends on save_binary flag

void saveNonZeroElements (string filename) const
void product (const Vec &x, Vec &y)
 multiply a sparse matrix by a full vector and set resulting vector y = matrix * x

Mat toMat ()
bool isSymmetric (real tolerance=0)
bool fillSymmetricPart (real tolerance=0)
void diag (Vec &d)
 set d[i] = A[i,i]

void diagonalOfSquare (Vec &d)
 d = diagonal(A*A'), i.e.

real dotRow (int i, Vec v)
 return dot product of i-th row with vector v

real dotColumn (int j, Vec v)
 return dot product of j-th column with vector v

void transposeProduct (RowMapSparseMatrix &m, bool verbose=false)
 M = A' * A.

void add2Rows (Vec row, bool only_on_non_zeros=true)
 add vector to each row; by default do it only on the non-zero elements

void add2Columns (Vec col, bool only_on_non_zeros=true)
 add vector to each column; by default do it only on the non-zero elements

void add (real scalar, bool only_on_non_zeros=true)
 add a scalar everywhere; by default do it only on the non-zero elements

void averageAcrossRowsAndColumns (Vec avg_across_rows, Vec avg_across_columns, bool only_on_non_zeros=true)
real sumRow (int i)
void operator *= (real scalar)
 multiply each (non-zero) element

void exportToMatlabReadableFormat (string filename)
real density ()
 Compute the matrix density, that is : number_of_nonzero_elements / (length*width).


Static Public Member Functions

void transpose (RowMapSparseMatrix< T > &src, RowMapSparseMatrix< T > &dest)
 D = S'.

real multiplyVecs (map< int, T > &map1, map< int, T > &map2)
 Sparse vectors mutliplication.

real euclidianDistance (map< int, real > &map1, map< int, real > &map2)
void substractVecs (map< int, real > &map1, map< int, real > &map2, Vec &dest)
 Vec dest[i] = map1[i] - map2[i].

int getMaxColumnIndex (map< int, T > &map1, map< int, T > &map2)
 Return the last non-zero position of a sparse vector (used for sparse operations that need to know the "end" of a vector).


Public Attributes

vector< map< int, T > > rows
bool save_binary

Protected Attributes

int _width

Detailed Description

template<class T>
class PLearn::RowMapSparseMatrix< T >

Sparse matrices implemented with STL maps.

We assume that there are elements in each ROW.

We associate an STL map to each row: column index --> value

Space used is about O( size_of_elements * number_of_non_zero_elements )

Random access time is O(log(number_of_elements_per_row))

Row-wise iterations can be done in constant time per access.

Binary or ascii load/save streaming are available. Recommended filename extensions are .armsm and .brmsm respectively for Ascii Row Map Sparse Matrix or Binary Row Map Sparse Matrix.

Definition at line 69 of file RowMapSparseMatrix.h.


Constructor & Destructor Documentation

template<class T>
PLearn::RowMapSparseMatrix< T >::RowMapSparseMatrix int  n_rows = 0,
int  n_columns = 0
[inline]
 

Definition at line 78 of file RowMapSparseMatrix.h.

template<class T>
PLearn::RowMapSparseMatrix< T >::RowMapSparseMatrix string  filename  )  [inline]
 

Definition at line 81 of file RowMapSparseMatrix.h.

template<class T>
PLearn::RowMapSparseMatrix< T >::RowMapSparseMatrix const Mat m,
bool  fill_all = true
[inline]
 

Definition at line 83 of file RowMapSparseMatrix.h.

template<class T>
PLearn::RowMapSparseMatrix< T >::RowMapSparseMatrix const SparseMatrix sm,
int  n_rows,
int  n_cols
[inline]
 

Accepts a FORTRAN formatted sparse matrix as an initializer.

Definition at line 106 of file RowMapSparseMatrix.h.


Member Function Documentation

template<class T>
void PLearn::RowMapSparseMatrix< T >::add real  scalar,
bool  only_on_non_zeros = true
[inline]
 

add a scalar everywhere; by default do it only on the non-zero elements

Definition at line 507 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::add2Columns Vec  col,
bool  only_on_non_zeros = true
[inline]
 

add vector to each column; by default do it only on the non-zero elements

Definition at line 491 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::add2Rows Vec  row,
bool  only_on_non_zeros = true
[inline]
 

add vector to each row; by default do it only on the non-zero elements

Definition at line 475 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::averageAcrossRowsAndColumns Vec  avg_across_rows,
Vec  avg_across_columns,
bool  only_on_non_zeros = true
[inline]
 

average across rows on one hand, and in parallel average across columns (thus getting two averages). The boolean argument specifies whether the average is across the explicit elements (the non-zeros) or across everything (currently unsupported).

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 526 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::clear  )  [inline]
 

this is equivalent to setting all values to "0" (the default value of T)

Definition at line 127 of file RowMapSparseMatrix.h.

Referenced by PLearn::RowMapSparseMatrix< real >::resize(), and PLearn::RowMapSparseMatrix< real >::transpose().

template<class T>
void PLearn::RowMapSparseMatrix< T >::clearRow int  i  )  [inline]
 

Definition at line 133 of file RowMapSparseMatrix.h.

template<class T>
real PLearn::RowMapSparseMatrix< T >::density  )  [inline]
 

Compute the matrix density, that is : number_of_nonzero_elements / (length*width).

Definition at line 793 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::diag Vec d  )  [inline]
 

set d[i] = A[i,i]

Definition at line 383 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::diagonalOfSquare Vec d  )  [inline]
 

d = diagonal(A*A'), i.e.

d[i] = |A[i]|^2 where A[i] is i-th row

Definition at line 392 of file RowMapSparseMatrix.h.

template<class T>
real PLearn::RowMapSparseMatrix< T >::dotColumn int  j,
Vec  v
[inline]
 

return dot product of j-th column with vector v

Definition at line 426 of file RowMapSparseMatrix.h.

template<class T>
real PLearn::RowMapSparseMatrix< T >::dotRow int  i,
Vec  v
[inline]
 

return dot product of i-th row with vector v

Definition at line 408 of file RowMapSparseMatrix.h.

template<class T>
real PLearn::RowMapSparseMatrix< T >::euclidianDistance map< int, real > &  map1,
map< int, real > &  map2
[inline, static]
 

This is not a "true" euclidian distance

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 651 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::exportToMatlabReadableFormat string  filename  )  [inline]
 

Export as matlab readable [i, j , v] format to file <out> Matlab : (1) load <out> (2) A = spconvert(out) (note: the file extension must be '.dat')

Add the bottom right corner, to make sure that the sparse matrix has the right dimensions

Definition at line 763 of file RowMapSparseMatrix.h.

Referenced by PLearn::MatlabInterface::eigs_r11(), and PLearn::matlabR11eigs().

template<class T>
bool PLearn::RowMapSparseMatrix< T >::fillSymmetricPart real  tolerance = 0  )  [inline]
 

if A(i,j) is specified and not A(j,i) then set A(j,i)=A(i,j). if both were specified but different, up to the tolerance (in abs. value difference), then abort the operation and return false (there is a problem!). Otherwise return true.

see if entry i exists in row j

not found, then set A(j,i) to A(i,j)

check if they are equal

Definition at line 354 of file RowMapSparseMatrix.h.

template<class T>
int PLearn::RowMapSparseMatrix< T >::getMaxColumnIndex map< int, T > &  map1,
map< int, T > &  map2
[inline, static]
 

Return the last non-zero position of a sparse vector (used for sparse operations that need to know the "end" of a vector).

Definition at line 750 of file RowMapSparseMatrix.h.

template<class T>
bool PLearn::RowMapSparseMatrix< T >::isSymmetric real  tolerance = 0  )  [inline]
 

Definition at line 329 of file RowMapSparseMatrix.h.

template<class T>
int PLearn::RowMapSparseMatrix< T >::length  )  const [inline]
 

Definition at line 181 of file RowMapSparseMatrix.h.

Referenced by PLearn::RowMapSparseMatrix< real >::add(), PLearn::RowMapSparseMatrix< real >::add2Columns(), PLearn::RowMapSparseMatrix< real >::add2Rows(), PLearn::RowMapSparseValueMatrix< T >::averageAcrossRowsAndColumns(), PLearn::RowMapSparseMatrix< real >::averageAcrossRowsAndColumns(), PLearn::RowMapSparseMatrix< real >::density(), PLearn::RowMapSparseMatrix< real >::diag(), PLearn::RowMapSparseMatrix< real >::diagonalOfSquare(), PLearn::RowMapSparseMatrix< real >::dotColumn(), PLearn::MatlabInterface::eigs_r11(), PLearn::RowMapSparseMatrix< real >::exportToMatlabReadableFormat(), PLearn::RowMapSparseMatrix< real >::fillSymmetricPart(), PLearn::RowMapSparseMatrix< real >::isSymmetric(), PLearn::matlabR11eigs(), PLearn::RowMapSparseMatrix< real >::operator *=(), PLearn::RowMapSparseValueMatrix< T >::operator()(), PLearn::RowMapSparseMatrix< real >::operator()(), PLearn::print(), PLearn::RowMapSparseMatrix< real >::product(), PLearn::RowMapSparseMatrix< real >::RowMapSparseMatrix(), PLearn::RowMapSparseValueMatrix< T >::RowMapSparseValueMatrix(), PLearn::RowMapSparseMatrix< real >::saveNonZeroElements(), PLearn::RowMapSparseValueMatrix< T >::toMat(), PLearn::RowMapSparseMatrix< real >::toMat(), and PLearn::RowMapSparseMatrix< real >::transpose().

template<class T>
void PLearn::RowMapSparseMatrix< T >::load string  filename  )  [inline]
 

determines automatically whether file is binary or ascii

Definition at line 193 of file RowMapSparseMatrix.h.

template<class T>
real PLearn::RowMapSparseMatrix< T >::multiplyVecs map< int, T > &  map1,
map< int, T > &  map2
[inline, static]
 

Sparse vectors mutliplication.

Definition at line 608 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::operator *= real  scalar  )  [inline]
 

multiply each (non-zero) element

Definition at line 576 of file RowMapSparseMatrix.h.

template<class T>
map<int,T>& PLearn::RowMapSparseMatrix< T >::operator() int  i  )  [inline]
 

Get i-th row. Exemple to iterate on i-th row:

map<int,T>& row_i = A(i); < note very important: row_i is a reference (&) map<int,T>::const_iterator it = row_i.begin(); map<int,T>::const_iterator end = row_i.end(); for (;it!=end;++it) { int j = it->first; T Aij = it->second; ... }

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 172 of file RowMapSparseMatrix.h.

template<class T>
const T& PLearn::RowMapSparseMatrix< T >::operator() int  i,
int  j
const [inline]
 

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 147 of file RowMapSparseMatrix.h.

template<class T>
T& PLearn::RowMapSparseMatrix< T >::operator() int  i,
int  j
[inline]
 

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 138 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::product const Vec x,
Vec y
[inline]
 

multiply a sparse matrix by a full vector and set resulting vector y = matrix * x

Definition at line 293 of file RowMapSparseMatrix.h.

Referenced by PLearn::product().

template<class T>
void PLearn::RowMapSparseMatrix< T >::read istream &  in  )  [inline]
 

Definition at line 194 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::resize int  n_rows,
int  n_columns
[inline]
 

THIS ALSO CLEARS THE MATRIX.

Definition at line 120 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::save string  filename  )  const [inline]
 

Definition at line 184 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::saveAscii string  filename  )  [inline]
 

Definition at line 187 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::saveNonZeroElements string  filename  )  const [inline]
 

saves the non-zero elements in an ascii file with the following simple format: first line: <length> <width> <numbernonzero> subsequent lines: <row> <column> <value>

Definition at line 277 of file RowMapSparseMatrix.h.

template<class T>
int PLearn::RowMapSparseMatrix< T >::size  )  const [inline]
 

NOTE THIS IS A BIT EXPENSIVE!

Definition at line 175 of file RowMapSparseMatrix.h.

Referenced by PLearn::RowMapSparseMatrix< real >::density(), and PLearn::RowMapSparseMatrix< real >::saveNonZeroElements().

template<class T>
void PLearn::RowMapSparseMatrix< T >::substractVecs map< int, real > &  map1,
map< int, real > &  map2,
Vec dest
[inline, static]
 

Vec dest[i] = map1[i] - map2[i].

Definition at line 704 of file RowMapSparseMatrix.h.

template<class T>
real PLearn::RowMapSparseMatrix< T >::sumRow int  i  )  [inline]
 

average across rows on one hand, and in parallel average across columns (thus getting two averages). The boolean argument specifies whether the average is across the explicit elements (the non-zeros) or across everything (currently unsupported).

Definition at line 563 of file RowMapSparseMatrix.h.

template<class T>
Mat PLearn::RowMapSparseMatrix< T >::toMat  )  [inline]
 

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 314 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::transpose RowMapSparseMatrix< T > &  src,
RowMapSparseMatrix< T > &  dest
[inline, static]
 

D = S'.

Definition at line 589 of file RowMapSparseMatrix.h.

template<class T>
void PLearn::RowMapSparseMatrix< T >::transposeProduct RowMapSparseMatrix< T > &  m,
bool  verbose = false
[inline]
 

M = A' * A.

Definition at line 442 of file RowMapSparseMatrix.h.

template<class T>
int PLearn::RowMapSparseMatrix< T >::width  )  const [inline]
 

Definition at line 182 of file RowMapSparseMatrix.h.

Referenced by PLearn::RowMapSparseValueMatrix< T >::averageAcrossRowsAndColumns(), PLearn::RowMapSparseMatrix< real >::averageAcrossRowsAndColumns(), PLearn::RowMapSparseMatrix< real >::density(), PLearn::RowMapSparseMatrix< real >::dotRow(), PLearn::RowMapSparseMatrix< real >::exportToMatlabReadableFormat(), PLearn::RowMapSparseValueMatrix< T >::operator()(), PLearn::RowMapSparseMatrix< real >::operator()(), PLearn::print(), PLearn::RowMapSparseMatrix< real >::product(), PLearn::RowMapSparseMatrix< real >::RowMapSparseMatrix(), PLearn::RowMapSparseValueMatrix< T >::RowMapSparseValueMatrix(), PLearn::RowMapSparseMatrix< real >::saveNonZeroElements(), PLearn::RowMapSparseValueMatrix< T >::toMat(), and PLearn::RowMapSparseMatrix< real >::toMat().

template<class T>
void PLearn::RowMapSparseMatrix< T >::write ostream &  out  )  const [inline]
 

mode used to write depends on save_binary flag

Definition at line 237 of file RowMapSparseMatrix.h.


Member Data Documentation

template<class T>
int PLearn::RowMapSparseMatrix< T >::_width [protected]
 

Definition at line 73 of file RowMapSparseMatrix.h.

template<class T>
vector< map<int,T> > PLearn::RowMapSparseMatrix< T >::rows
 

Definition at line 71 of file RowMapSparseMatrix.h.

template<class T>
bool PLearn::RowMapSparseMatrix< T >::save_binary
 

Definition at line 76 of file RowMapSparseMatrix.h.


The documentation for this class was generated from the following file:
Generated on Tue Aug 17 16:23:54 2004 for PLearn by doxygen 1.3.7