PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Attributes
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, T nullelem=0)
 RowMapSparseMatrix (const Mat &m, bool fill_all=true, T nullelem=0)
 RowMapSparseMatrix (const SparseMatrix &sm, int n_rows, int n_cols, T nullelem=0)
 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
get (int i, int j) const
void set (int i, int j, T v) const
bool exists (int i, int j) const
map< int, T > & operator() (int i)
map< int, T > & getRow (int i)
int size () const
 NOTE THIS IS A BIT EXPENSIVE!
int length () const
int width () const
void copyRowFrom (int i, const map< int, T > &from_row, bool clear_rest=true)
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 addToRows (Vec row, bool only_on_non_zeros=true)
 add vector to each row; by default do it only on the non-zero elements
void addToColumns (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

static void transpose (RowMapSparseMatrix< T > &src, RowMapSparseMatrix< T > &dest)
 D = S'.
static real multiplyVecs (map< int, T > &map1, map< int, T > &map2)
 Sparse vectors mutliplication.
static real euclidianDistance (map< int, real > &map1, map< int, real > &map2)
static void substractVecs (map< int, real > &map1, map< int, real > &map2, Vec &dest)
 Vec dest[i] = map1[i] - map2[i].
static 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
null_elem

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,
nullelem = 0 
) [inline]

Definition at line 79 of file RowMapSparseMatrix.h.

        : rows(n_rows), _width(n_columns), save_binary(true), null_elem(nullelem) {}
template<class T>
PLearn::RowMapSparseMatrix< T >::RowMapSparseMatrix ( const Mat m,
bool  fill_all = true,
nullelem = 0 
) [inline]

Definition at line 84 of file RowMapSparseMatrix.h.

                                                                       : rows(m.length()), _width(m.width()), 
                                                                         save_binary(true), null_elem(nullelem)
    {
        if (fill_all)
            for (int i=0;i<length();i++)
            {
                real* r=m[i];
                map<int,T>& row_i=rows[i];
                for (int j=0;j<width();j++)
                    row_i[j]=T(r[j]);
            }
        else
            for (int i=0;i<length();i++)
            {
                real* r=m[i];
                map<int,T>& row_i=rows[i];
                for (int j=0;j<width();j++){
                    if(T(r[j])!=0)
                        row_i[j]=T(r[j]);
                }
            }
    }
template<class T>
PLearn::RowMapSparseMatrix< T >::RowMapSparseMatrix ( const SparseMatrix sm,
int  n_rows,
int  n_cols,
nullelem = 0 
) [inline]

Accepts a FORTRAN formatted sparse matrix as an initializer.

Definition at line 108 of file RowMapSparseMatrix.h.

                                                                                     : 
        rows(n_rows), _width(n_cols), save_binary(false), null_elem(nullelem) {

        for (int j = 0; j < n_cols; j++) {
            int bcol_j = (int)sm.beginRow[j];
            int ecol_j = (int)sm.endRow[j];
            for (int row_i = bcol_j; row_i <= ecol_j; row_i++) {
                int i = (int)sm.row[row_i];
                (*this)(i, j) = (T)sm.values[row_i];
            }
        }

    }

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 572 of file RowMapSparseMatrix.h.

    {
        if (!only_on_non_zeros)
            PLERROR("RowMapSparseMatrix::add(real,bool) works only with bool=true");
        for (int i=0;i<length();i++)
        {
            map<int,T>& row_i = rows[i];
            typename map<int,T>::iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                it->second += scalar;
        }
    }
template<class T>
void PLearn::RowMapSparseMatrix< T >::addToColumns ( 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 552 of file RowMapSparseMatrix.h.

Referenced by PLearn::addToColumns().

    {
#ifdef BOUNDCHECK
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::add2Columns works only when null_elem=0, but was %g\n",null_elem);
#endif
        if (!only_on_non_zeros)
            PLERROR("RowMapSparseMatrix::add2Columns(Vec,bool) works only with bool=true");
        for (int i=0;i<length();i++)
        {
            real col_i=col[i];
            map<int,T>& row_i = rows[i];
            typename map<int,T>::iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                it->second += col_i;
        }
    }

Here is the caller graph for this function:

template<class T>
void PLearn::RowMapSparseMatrix< T >::addToRows ( 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 532 of file RowMapSparseMatrix.h.

Referenced by PLearn::addToRows().

    {
#ifdef BOUNDCHECK
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::add2Rows works only when null_elem=0, but was %g\n",null_elem);
#endif
        if (!only_on_non_zeros)
            PLERROR("RowMapSparseMatrix::add2Rows(Vec,bool) works only with bool=true");
        real* r=row.data();
        for (int i=0;i<length();i++)
        {
            map<int,T>& row_i = rows[i];
            typename map<int,T>::iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                it->second += r[it->first];
        }
    }

Here is the caller graph for this function:

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 592 of file RowMapSparseMatrix.h.

Referenced by PLearn::averageAcrossRowsAndColumns().

    {
        if (!only_on_non_zeros)
            PLERROR("RowMapSparseMatrix::averageAcrossRowsAndColumns works only with only_on_non_zeros=true");
        avg_across_rows.resize(width());
        avg_across_columns.resize(length());
        avg_across_rows.clear();
        avg_across_columns.clear();
        TVec<int> column_counts(width());
        for (int i=0;i<length();i++)
        {
            real& avg_cols_i=avg_across_columns[i];
            real* avg_rows = avg_across_rows.data();
            map<int,T>& row_i = rows[i];
            typename map<int,T>::const_iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            int n=0;
            for (;it!=end;++it)
            {
                avg_cols_i += it->second;
                int j=it->first;
                avg_rows[j] += it->second;
                n++;
                column_counts[j]++;
            }
            if (n>0)
                avg_cols_i /= n;
        }
        for (int j=0;j<width();j++)
            if (column_counts[j]>0)
                avg_across_rows[j] /= column_counts[j];
    }

Here is the caller graph for this function:

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 130 of file RowMapSparseMatrix.h.

Referenced by PLearn::ProbabilitySparseMatrix::clear(), PLearn::ProbabilitySparseMatrix::removeRow(), and PLearn::RowMapSparseMatrix< real >::transpose().

                 {
        int s=rows.size();
        for (int i=0;i<s;i++)
            rows[i].clear();
    }

Here is the caller graph for this function:

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

Definition at line 136 of file RowMapSparseMatrix.h.

    {
        rows[i].clear();
    }
template<class T>
void PLearn::RowMapSparseMatrix< T >::copyRowFrom ( int  i,
const map< int, T > &  from_row,
bool  clear_rest = true 
) [inline]

Definition at line 216 of file RowMapSparseMatrix.h.

    {
        map<int,T>& row = rows[i];
        if (clear_rest)
            clearRow(i);
        typename map<int,T>::const_iterator it = from_row.begin();
        typename map<int,T>::const_iterator end = from_row.end();
        for (;it!=end;++it)
            row[it->first]=it->second;
    }
template<class T>
real PLearn::RowMapSparseMatrix< T >::density ( ) [inline]

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

Definition at line 865 of file RowMapSparseMatrix.h.

    {
        return size() / real(length() * width());
    }
template<class T>
void PLearn::RowMapSparseMatrix< T >::diag ( Vec d) [inline]

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

Definition at line 432 of file RowMapSparseMatrix.h.

    {
        real* d_=d.data();
        for (int i=0;i<length();i++)
            d_[i] = (*this)(i,i);
    }
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 441 of file RowMapSparseMatrix.h.

    {
        real* d_=d.data();
        for (int i=0;i<length();i++)
        {
            real sum2=0;
            map<int,T>& row_i = rows[i];
            typename map<int,T>::const_iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                sum2 += it->second * it->second;
            d_[i] = sum2;
        }
    }
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 477 of file RowMapSparseMatrix.h.

    {
#ifdef BOUNDCHECK
        if (v.length()!=length())
            PLERROR("RowMapSparseMatrix::dotColumn(%d,v), v.length_=%d != matrix length=%d",
                    j,v.length(),length());
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::dotColumn works only when null_elem=0, but was %g\n",null_elem);
#endif
        PLWARNING("RowMapSparseMatrix is not appropriate to perform dotColumn operations");
        real s=0;
        real* v_=v.data();
        for (int i=0;i<v.length();i++)
            s += (*this)(i,j) * v_[i];
        return s;
    }
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 457 of file RowMapSparseMatrix.h.

    {
#ifdef BOUNDCHECK
        if (v.length()!=width())
            PLERROR("RowMapSparseMatrix::dotRow(%d,v), v.length_=%d != matrix width=%d",
                    i,v.length(),width());
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::dotRow works only when null_elem=0, but was %g\n",null_elem);
#endif
        real s = 0;
        real* v_=v.data();
        map<int,T>& row_i = rows[i];
        typename map<int,T>::const_iterator it = row_i.begin();
        typename map<int,T>::const_iterator end = row_i.end();
        for (;it!=end;++it)
            s += it->second * v_[it->first];
        return s;
    }
template<class T>
static 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 723 of file RowMapSparseMatrix.h.

    {
        if (map1.size() == 0 || map2.size() == 0)
            return 0;
        map<int, real>::iterator beg1 = map1.begin();
        map<int, real>::iterator beg2 = map2.begin();
        map<int, real>::iterator end1 = map1.end();
        map<int, real>::iterator end2 = map2.end();
        int col1, col2;
        real val1, val2, diff, sum = 0;
        bool fend1 = (beg1 == end1), fend2 = (beg2 == end2);
        int OUT = getMaxColumnIndex(map1, map2) + 1;
        
        while (!fend1 || !fend2) 
        {
            if (!fend1)
                col1 = beg1->first;
            else
                col1 = OUT;
            if (!fend2)
                col2 = beg2->first;
            else
                col2 = OUT;
            val1 = beg1->second;
            val2 = beg2->second;
            if (col1 == col2) 
            {
                diff = val1 - val2;
                sum += (diff * diff);
                beg1++;
                if (beg1 == end1) fend1 = true;
                beg2++;
                if (beg2 == end2) fend2 = true;
            } else if (col1 < col2) 
            {
                diff = val1;
                sum += (diff * diff);
                beg1++;
                if (beg1 == end1) fend1 = true;
            } else if (col1 > col2) 
            {
                diff = val2;
                sum += (diff * diff);
                beg2++;
                if (beg2 == end2) fend2 = true;
            }
        }
        //return sqrt(sum);
        return sum;
    }
template<class T>
bool PLearn::RowMapSparseMatrix< T >::exists ( int  i,
int  j 
) const [inline]

Definition at line 180 of file RowMapSparseMatrix.h.

                                    { 
#ifdef BOUNDCHECK      
        if (i<0 || i>=length() || j<0 || j>=width())
            PLERROR("RowMapSparseMatrix: out-of-bound access to (%d,%d), dims=(%d,%d)",
                    i,j,length(),width());
#endif
        map<int,T>& row_i = rows[i];
        typename map<int,T>::const_iterator it = row_i.find(j);
        return (it!=row_i.end());
    }
template<class T>
void PLearn::RowMapSparseMatrix< T >::exportToMatlabReadableFormat ( string  filename) [inline]

Export as matlab readable [i, j , v] format to file 'filename' 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 835 of file RowMapSparseMatrix.h.

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

    {
        ofstream out(filename.c_str());
#ifdef USEDOUBLE
        out.precision(20);
#else
        out.precision(6);
#endif
        for (unsigned int i = 0; i < rows.size(); i++)
        {
            typename map<int, T>::iterator beg = rows[i].begin();
            typename map<int, T>::iterator end = rows[i].end();
            while (beg != end)
            {
                out << i + 1 << " " << beg->first + 1 << " " << beg->second << endl;
                beg++;
            }
        }
        int l = length();
        int w = width();
        l--; w--;
        map<int, T>& row_l=rows[l];
        if (row_l.find(w) == row_l.end())
            out << l + 1 << " " << w + 1 << " " << 0 << endl;
    }

Here is the caller graph for this function:

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 403 of file RowMapSparseMatrix.h.

    {
        for (int i=0;i<length();i++)
        {
            map<int,T>& row_i = rows[i];
            typename map<int,T>::const_iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
            {
                int j=it->first;
                if (j!=i)
                {
                    map<int,T>& row_j= rows[j];
                    typename map<int,T>::iterator symm_it =row_j.find(i);
                    if (symm_it==row_j.end())
                        row_j[i]=it->second;
                    else
                        if (fabs(symm_it->second - it->second)>tolerance)
                            return false;
                }
            }
        }
        return true;
    }
template<class T>
T PLearn::RowMapSparseMatrix< T >::get ( int  i,
int  j 
) const [inline]

Definition at line 168 of file RowMapSparseMatrix.h.

{ return (*this)(i,j); }
template<class T>
static 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 822 of file RowMapSparseMatrix.h.

    {
        map<int, real>::iterator end1 = map1.end();
        map<int, real>::iterator end2 = map2.end();
        --end1;
        --end2;
        return MAX(end1->first, end2->first);
    }
template<class T>
map<int,T>& PLearn::RowMapSparseMatrix< T >::getRow ( int  i) [inline]

Definition at line 204 of file RowMapSparseMatrix.h.

{ return rows[i]; }
template<class T>
bool PLearn::RowMapSparseMatrix< T >::isSymmetric ( real  tolerance = 0) [inline]

Definition at line 378 of file RowMapSparseMatrix.h.

    {
        for (int i=0;i<length();i++)
        {
            map<int,T>& row_i = rows[i];
            typename map<int,T>::const_iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                if (it->first!=i)
                {
                    T& other_guy = (*this)(it->first,i);
                    if (fabs(other_guy - it->second)>tolerance)
                        return false;
                }
        }
        return true;
    }
template<class T>
int PLearn::RowMapSparseMatrix< T >::length ( ) const [inline]
template<class T>
static real PLearn::RowMapSparseMatrix< T >::multiplyVecs ( map< int, T > &  map1,
map< int, T > &  map2 
) [inline, static]

Sparse vectors mutliplication.

Definition at line 680 of file RowMapSparseMatrix.h.

    {
        if (map1.size() == 0 || map2.size() == 0)
            return 0;
        typename map<int, T>::iterator beg1 = map1.begin();
        typename map<int, T>::iterator beg2 = map2.begin();
        typename map<int, T>::iterator end1 = map1.end();
        typename map<int, T>::iterator end2 = map2.end();
        int col1, col2;
        T val1, val2, sum = 0;
        bool fend1 = (beg1 == end1), fend2 = (beg2 == end2);
        int OUT = getMaxColumnIndex(map1, map2) + 1;
      
        while (!fend1 || !fend2) {
            if (!fend1)
                col1 = beg1->first;
            else
                col1 = OUT;
            if (!fend2)
                col2 = beg2->first;
            else
                col2 = OUT;
            val1 = beg1->second;
            val2 = beg2->second;
            if (col1 == col2) {
                sum += (val1 * val2);
                ++beg1;
                if (beg1 == end1) fend1 = true;
                ++beg2;
                if (beg2 == end2) fend2 = true;
            } else if (col1 < col2) {
                //sum += (val1 * val2);
                ++beg1;
                if (beg1 == end1) fend1 = true;
            } else if (col1 > col2) {
                //sum += (val1 * val2);
                ++beg2;
                if (beg2 == end2) fend2 = true;
            }
        }
        return sum;
    }
template<class T>
T& PLearn::RowMapSparseMatrix< T >::operator() ( int  i,
int  j 
) [inline]

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 141 of file RowMapSparseMatrix.h.

                                { 
#ifdef BOUNDCHECK      
        if (i<0 || i>=length() || j<0 || j>=width())
            PLERROR("RowMapSparseMatrix: out-of-bound access to (%d,%d), dims=(%d,%d)",
                    i,j,length(),width());
#endif
        map<int,T>& row_i = rows[i];
        typename map<int,T>::iterator it = row_i.find(j);
        if (it==row_i.end())
            return null_elem;
        return it->second;
    }
template<class T>
const T& PLearn::RowMapSparseMatrix< T >::operator() ( int  i,
int  j 
) const [inline]

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 155 of file RowMapSparseMatrix.h.

                                            { 
#ifdef BOUNDCHECK      
        if (i<0 || i>=length() || j<0 || j>=width())
            PLERROR("RowMapSparseMatrix: out-of-bound access to (%d,%d), dims=(%d,%d)",
                    i,j,length(),width());
#endif
        map<int,T>& row_i = rows[i];
        typename map<int,T>::const_iterator it = row_i.find(j);
        if (it==row_i.end())
            return null_elem;
        return it->second;
    }
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 203 of file RowMapSparseMatrix.h.

{ return rows[i]; }
template<class T>
void PLearn::RowMapSparseMatrix< T >::operator*= ( real  scalar) [inline]

multiply each (non-zero) element

Definition at line 644 of file RowMapSparseMatrix.h.

    {
#ifdef BOUNDCHECK
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::operator* works only when null_elem=0, but was %g\n",null_elem);
#endif
        for (int i=0;i<length();i++)
        {
            map<int,T>& row_i = rows[i];
            typename map<int,T>::iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                it->second *= scalar;
        }
    }
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 339 of file RowMapSparseMatrix.h.

Referenced by PLearn::product().

                                       {
        real* _y=y.data();
        real* _x=x.data();
#ifdef BOUNDCHECK
        if (y.length()!=length() || x.length()!=width())
            PLERROR("RowMapSparseMatrix::product: inconsistent arguments (%d,%d), dims=(%d,%d)",
                    x.length(),y.length(),length(),width());
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::product works only when null_elem=0, but was %g\n",null_elem);
#endif
        int s=rows.size();
        for (int i=0;i<s;i++)
        {
            real res=0;
            map<int,T>& row_i = rows[i];
            typename map<int,T>::const_iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            for (;it!=end;++it)
                res += it->second * _x[it->first];
            _y[i] = res;
        }
    }

Here is the caller graph for this function:

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

THIS ALSO CLEARS THE MATRIX.

Definition at line 123 of file RowMapSparseMatrix.h.

Referenced by PLearn::ProbabilitySparseMatrix::resize().

                                           {
        rows.resize(n_rows);
        _width=n_columns;
        clear();
    }

Here is the caller graph for this function:

template<class T>
void PLearn::RowMapSparseMatrix< T >::set ( int  i,
int  j,
v 
) const [inline]

Definition at line 170 of file RowMapSparseMatrix.h.

                                      { 
#ifdef BOUNDCHECK      
        if (i<0 || i>=length() || j<0 || j>=width())
            PLERROR("RowMapSparseMatrix: out-of-bound access to (%d,%d), dims=(%d,%d)",
                    i,j,length(),width());
#endif
        map<int,T>& row_i = rows[i];
        row_i[j] = v;
    }
template<class T>
int PLearn::RowMapSparseMatrix< T >::size ( ) const [inline]

NOTE THIS IS A BIT EXPENSIVE!

Definition at line 207 of file RowMapSparseMatrix.h.

Referenced by PLearn::ProbabilitySparseMatrix::getAsFullVector(), PLearn::ProbabilitySparseMatrix::getAsMaxSizedVectors(), and PLearn::ProbabilitySparseMatrix::size().

                     { 
        int _size=0;
        for (unsigned int i=0;i<rows.size();i++)
            _size += rows[i].size();
        return _size;
    }

Here is the caller graph for this function:

template<class T>
static 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 776 of file RowMapSparseMatrix.h.

    {
        map<int, real>::iterator beg1 = map1.begin();
        map<int, real>::iterator beg2 = map2.begin();
        map<int, real>::iterator end1 = map1.end();
        map<int, real>::iterator end2 = map2.end();
        int col1, col2;
        real val1, val2;
        bool fend1 = (beg1 == end1), fend2 = (beg2 == end2);
        int OUT = getMaxColumnIndex(map1, map2) + 1;
      
        while (!fend1 || !fend2) 
        {
            if (!fend1)
                col1 = beg1->first;
            else
                col1 = OUT;
            if (!fend2)
                col2 = beg2->first;
            else
                col2 = OUT;
            val1 = beg1->second;
            val2 = beg2->second;
            if (col1 == col2) 
            {
                dest[col1] = val1 - val2;
                beg1++;
                if (beg1 == end1) fend1 = true;
                beg2++;
                if (beg2 == end2) fend2 = true;
            } else if (col1 < col2) 
            {
                dest[col1] = val1;
                beg1++;
                if (beg1 == end1) fend1 = true;
            } else if (col1 > col2) 
            {
                dest[col2] = 0 - val2;
                beg2++;
                if (beg2 == end2) fend2 = true;
            }
        }
    } 
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 631 of file RowMapSparseMatrix.h.

    {
        real s=0;
        map<int,T>& row_i = rows[i];
        typename map<int,T>::const_iterator it = row_i.begin();
        typename map<int,T>::const_iterator end = row_i.end();
        for (;it!=end;++it)
            s += it->second;
        return s;
    }
template<class T>
Mat PLearn::RowMapSparseMatrix< T >::toMat ( ) [inline]

Reimplemented in PLearn::RowMapSparseValueMatrix< T >.

Definition at line 362 of file RowMapSparseMatrix.h.

    {
        Mat res(length(),width());
        if (null_elem!=0) res.fill(null_elem);
        for (int i=0;i<length();i++)
        {
            map<int,T>& row_i = rows[i];
            typename map<int,T>::const_iterator it = row_i.begin();
            typename map<int,T>::const_iterator end = row_i.end();
            real* res_i=res[i];
            for (;it!=end;++it)
                res_i[it->first] = it->second;
        }
        return res;
    }
template<class T>
static void PLearn::RowMapSparseMatrix< T >::transpose ( RowMapSparseMatrix< T > &  src,
RowMapSparseMatrix< T > &  dest 
) [inline, static]

D = S'.

Definition at line 661 of file RowMapSparseMatrix.h.

    {
        dest.clear();
        for (int i = 0; i < src.length(); i++)
        {
            map<int, T>& row = src(i);
            typename map<int, T>::iterator beg = row.begin();
            typename map<int, T>::iterator end = row.end();
            while (beg != end)
            {
                int col = beg->first;
                T val = beg->second;
                dest(col, i) = val;
                ++beg;
            }
        }
    }
template<class T>
void PLearn::RowMapSparseMatrix< T >::transposeProduct ( RowMapSparseMatrix< T > &  m,
bool  verbose = false 
) [inline]

M = A' * A.

Definition at line 495 of file RowMapSparseMatrix.h.

    {
#ifdef BOUNDCHECK
        if (null_elem!=0)
            PLERROR("RowMapSparseMatrix::dotColumn works only when null_elem=0, but was %g\n",null_elem);
#endif
        RowMapSparseMatrix<T>& self = (*this);
        int n = self.length();
        RowMapSparseMatrix<T> mt(n, n);
        transpose(m, mt);
        int nnz = 0;
        for (int i = 0; i < n; i++)
        {
            if (verbose)
            {
                if (i % 10 == 0 && i != 0) { 
                    cout << "[" << i << "]" << " ";
                    if (i % 100 == 0) cout << endl;
                    else cout.flush();
                }
            }
            for (int j = 0; j < n; j++)
            {
                T val =  multiplyVecs(mt(i), mt(j));
                if (val != 0)
                {
                    nnz++;
                    self(i, j) = val;
                }
            }
        }
        if (verbose) 
            cout << endl;
    }
template<class T>
int PLearn::RowMapSparseMatrix< T >::width ( ) const [inline]

Definition at line 214 of file RowMapSparseMatrix.h.

Referenced by PLearn::print().

{ return _width; }

Here is the caller graph for this function:


Member Data Documentation

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

Definition at line 73 of file RowMapSparseMatrix.h.

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

Definition at line 77 of file RowMapSparseMatrix.h.

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

Definition at line 71 of file RowMapSparseMatrix.h.

Referenced by PLearn::columnSum(), and PLearn::doubleCentering().

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:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines