PLearn 0.1
Public Member Functions | Public Attributes
PLearn::RowMapSparseValueMatrix< T > Class Template Reference

#include <RowMapSparseValueMatrix.h>

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

List of all members.

Public Member Functions

 RowMapSparseValueMatrix (T value_=0, int n_rows=0, int n_columns=0)
 RowMapSparseValueMatrix (T value_, string filename)
 RowMapSparseValueMatrix (T value_, const Mat &m, int fill_mode=0)
 RowMapSparseValueMatrix (T value_, const SparseMatrix &sm, int n_rows, int n_cols)
 Accepts a FORTRAN formatted sparse matrix as an initializer.
Mat toMat ()
T & operator() (int i, int j)
const T & operator() (int i, int j) const
map< int, T > & operator() (int i)
void averageAcrossRowsAndColumns (Vec avg_across_rows, Vec avg_across_columns, bool only_on_non_value=false)
real euclidianDistance (map< int, real > &map1, map< int, real > &map2)

Public Attributes

value

Detailed Description

template<class T>
class PLearn::RowMapSparseValueMatrix< T >

Warning **: this class inherits methods from RowMapSparseMatrix<T> that are not necessarily correct when "value" is different from 0. The following methods are correct (plus those redefined below):

The value of elements that is not specified is given by the field "value".

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_value_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 75 of file RowMapSparseValueMatrix.h.


Constructor & Destructor Documentation

template<class T >
PLearn::RowMapSparseValueMatrix< T >::RowMapSparseValueMatrix ( value_ = 0,
int  n_rows = 0,
int  n_columns = 0 
) [inline]

Definition at line 80 of file RowMapSparseValueMatrix.h.

        : RowMapSparseMatrix<T>(n_rows, n_columns), value(value_)
    {}
template<class T >
PLearn::RowMapSparseValueMatrix< T >::RowMapSparseValueMatrix ( value_,
string  filename 
) [inline]

Definition at line 84 of file RowMapSparseValueMatrix.h.

        : RowMapSparseMatrix<T>(filename), value(value_)
    {}
template<class T >
PLearn::RowMapSparseValueMatrix< T >::RowMapSparseValueMatrix ( value_,
const Mat m,
int  fill_mode = 0 
) [inline]

Definition at line 88 of file RowMapSparseValueMatrix.h.

References i, j, and PLERROR.

        :  RowMapSparseMatrix<T>(m.length(), m.width()), value(value_)
    {
        switch(fill_mode){
        case 0:
            //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]);
            }
            break;
        case 1:
            //fill only if entry != value 
            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])!=value_)
                        row_i[j]=T(r[j]);
                }
            }
            break;
        case 2:
            //fill only if entry < value 
            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])<value_)
                        row_i[j]=T(r[j]);
                }
            }
            break;
        default:
            PLERROR("RowMapSparseValueMatrix: fill_mode must be 0, 1 or 2.");
        }
    }
template<class T >
PLearn::RowMapSparseValueMatrix< T >::RowMapSparseValueMatrix ( value_,
const SparseMatrix sm,
int  n_rows,
int  n_cols 
) [inline]

Accepts a FORTRAN formatted sparse matrix as an initializer.

Definition at line 132 of file RowMapSparseValueMatrix.h.

        : RowMapSparseMatrix<T>(sm, n_rows, n_cols), value(value_)
    {}

Member Function Documentation

template<class T >
void PLearn::RowMapSparseValueMatrix< T >::averageAcrossRowsAndColumns ( Vec  avg_across_rows,
Vec  avg_across_columns,
bool  only_on_non_value = false 
) [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 ones not equal to "value") or across everything.

Reimplemented from PLearn::RowMapSparseMatrix< T >.

Definition at line 195 of file RowMapSparseValueMatrix.h.

References PLearn::TVec< T >::clear(), PLearn::TVec< T >::data(), i, j, n, and PLearn::TVec< T >::resize().

                                                                  {
        avg_across_rows.resize(width());
        avg_across_columns.resize(length());
        avg_across_rows.clear();
        avg_across_columns.clear();
        TVec<int> column_counts(width());

        if (only_on_non_value){
            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]++;
                }
                avg_cols_i /= n;
            }
            for (int j=0;j<width();j++)
                avg_across_rows[j] /= column_counts[j];
        }
        else {
            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]++;
                }
                avg_cols_i += value*(width()-n);
                avg_cols_i /= width(); //store average of ith row
            }
            //compute average of each column
            for (int j=0;j<width();j++){
                avg_across_rows[j] += value*(length() - column_counts[j]);
                avg_across_rows[j] /= length();
            }
        }
    }

Here is the call graph for this function:

template<class T >
real PLearn::RowMapSparseValueMatrix< T >::euclidianDistance ( map< int, real > &  map1,
map< int, real > &  map2 
) [inline]

This is not a "true" euclidian distance

Reimplemented from PLearn::RowMapSparseMatrix< T >.

Definition at line 253 of file RowMapSparseValueMatrix.h.

References PLearn::diff(), and PLearn::sum().

                                                                       {
        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 - value;
                sum += (diff * diff);
                beg1++;
                if (beg1 == end1) fend1 = true;
            } else if (col1 > col2) 
            {
                diff = value - val2;
                sum += (diff * diff);
                beg2++;
                if (beg2 == end2) fend2 = true;
            }
        }
        //return sqrt(sum);
        return sum;
    }

Here is the call graph for this function:

template<class T >
map<int,T>& PLearn::RowMapSparseValueMatrix< 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 from PLearn::RowMapSparseMatrix< T >.

Definition at line 186 of file RowMapSparseValueMatrix.h.

References i.

{ return rows[i]; }
template<class T >
T& PLearn::RowMapSparseValueMatrix< T >::operator() ( int  i,
int  j 
) [inline]

Reimplemented from PLearn::RowMapSparseMatrix< T >.

Definition at line 152 of file RowMapSparseValueMatrix.h.

References i, j, and PLERROR.

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

Reimplemented from PLearn::RowMapSparseMatrix< T >.

Definition at line 161 of file RowMapSparseValueMatrix.h.

References i, and PLERROR.

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

Reimplemented from PLearn::RowMapSparseMatrix< T >.

Definition at line 136 of file RowMapSparseValueMatrix.h.

References i.

    {
        Mat res(length(),width(),value);
        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;
    }

Member Data Documentation

template<class T >
T PLearn::RowMapSparseValueMatrix< T >::value

Definition at line 78 of file RowMapSparseValueMatrix.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines