PLearn 0.1
Public Member Functions | Public Attributes | Private Attributes
PLearn::ProbabilitySparseMatrix Class Reference

#include <ProbabilitySparseMatrix.h>

Inheritance diagram for PLearn::ProbabilitySparseMatrix:
Inheritance graph
[legend]
Collaboration diagram for PLearn::ProbabilitySparseMatrix:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ProbabilitySparseMatrix (int ny=0, int nx=0, string pname="pYX")
void resize (int ny, int nx)
int nx ()
int ny ()
void rename (string new_name)
Set computeX ()
Set computeY ()
real get (int y, int x)
bool exists (int y, int x)
real operator() (int y, int x)
void incr (int y, int x, real increment=1)
const map< int, real > & getPYx (int x, bool dont_raise_error=false)
const map< int, real > & getPyX (int y)
map< int, realgetPYxCopy (int x, bool dont_raise_error=false)
map< int, realgetPyXCopy (int y)
void setPYx (int x, const map< int, real > &pYx)
void setPyX (int y, const map< int, real > &pyX)
void set (int y, int x, real v, bool dont_warn_for_zero=false)
void removeElem (int y, int x)
real sumPYx (int x, Set Y)
real sumPyX (int y, Set X)
real sumPYx (int x)
real sumPyX (int y)
void clearElements ()
void clear ()
void removeRow (int y, Set X)
void removeRow (int y)
void removeColumn (int x)
int size ()
void removeExtra (ProbabilitySparseMatrix &m)
void fullPrint ()
void save (string filename)
void load (string filename)
realgetAsFullVector ()
void getAsMaxSizedVectors (int max_size, vector< pair< real *, int > > &vectors)
void add (real *full_vector, int n_elems)
void set (real *full_vector, int n_elems)
real sumOfElements ()

Public Attributes

Set Y
Set X
bool raise_error
string name

Private Attributes

RowMapSparseMatrix< realx2y
RowMapSparseMatrix< realy2x

Detailed Description

Definition at line 25 of file ProbabilitySparseMatrix.h.


Constructor & Destructor Documentation

PLearn::ProbabilitySparseMatrix::ProbabilitySparseMatrix ( int  ny = 0,
int  nx = 0,
string  pname = "pYX" 
) [inline]

Definition at line 38 of file ProbabilitySparseMatrix.h.

References name, nx(), ny(), and resize().

                                                                      : raise_error(true)
    {
        resize(ny,nx);
        name = pname;
    }

Here is the call graph for this function:


Member Function Documentation

void PLearn::ProbabilitySparseMatrix::add ( real full_vector,
int  n_elems 
) [inline]

Definition at line 418 of file ProbabilitySparseMatrix.h.

References i, and incr().

    {
        for (int i = 0; i < n_elems; i += 3)
            incr((int)full_vector[i], (int)full_vector[i + 1], full_vector[i + 2]);
    }

Here is the call graph for this function:

void PLearn::ProbabilitySparseMatrix::clear ( ) [inline]

Definition at line 240 of file ProbabilitySparseMatrix.h.

References PLearn::RowMapSparseMatrix< T >::clear(), x2y, and y2x.

Referenced by set(), PLearn::update(), and PLearn::updateAndClearCounts().

{ x2y.clear(); y2x.clear(); }

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::ProbabilitySparseMatrix::clearElements ( ) [inline]

Definition at line 227 of file ProbabilitySparseMatrix.h.

References nx(), x, x2y, and y2x.

                         {
        for (int x=0;x<nx();x++)
        {
            map<int,real>& r = x2y(x);
            for (map<int,real>::iterator it = r.begin(); it!=r.end(); ++it)
            {
                it->second=0;
                y2x(it->first,x)=0;
            }
        }
    }

Here is the call graph for this function:

Set PLearn::ProbabilitySparseMatrix::computeX ( ) [inline]

Definition at line 52 of file ProbabilitySparseMatrix.h.

References nx(), sumPYx(), and X.

Referenced by PLearn::update(), and PLearn::updateAndClearCounts().

    {
        X.clear();
        for (int xx = 0; xx < nx(); xx++)
        {
            if (sumPYx(xx) > 0)
            {
                X.insert(xx);
            }
        }
        return X;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

Set PLearn::ProbabilitySparseMatrix::computeY ( ) [inline]

Definition at line 65 of file ProbabilitySparseMatrix.h.

References ny(), sumPyX(), and Y.

Referenced by PLearn::update(), and PLearn::updateAndClearCounts().

    {
        Y.clear();
        for (int yy = 0; yy < ny(); yy++)
        {
            if (sumPyX(yy) > 0)
            {
                Y.insert(yy);
            }
        }
        return Y;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

bool PLearn::ProbabilitySparseMatrix::exists ( int  y,
int  x 
) [inline]

Definition at line 92 of file ProbabilitySparseMatrix.h.

References x2y.

Referenced by removeExtra(), and PLearn::samePos().

                              { 
        map<int,real>& mx = x2y(x);
        if (mx.size()==0) return false;
        return mx.find(y)!=mx.end();
    }

Here is the caller graph for this function:

void PLearn::ProbabilitySparseMatrix::fullPrint ( ) [inline]

Definition at line 310 of file ProbabilitySparseMatrix.h.

References PLearn::endl(), nx(), ny(), x, x2y, and y2x.

    {
        cout << "y2x" << endl;
        for (int y = 0; y < ny(); y++)
        {
            for (int x = 0; x < nx(); x++)
            {
                cout << y2x(y, x) << " ";
            }
            cout << endl;
        }
        cout << "x2y" << endl;
        for (int x = 0; x < nx(); x++)
        {
            for (int y = 0; y < ny(); y++)
            {
                cout << x2y(x, y) << " ";
            }
            cout << endl;
        }
    }

Here is the call graph for this function:

real PLearn::ProbabilitySparseMatrix::get ( int  y,
int  x 
) [inline]

Definition at line 78 of file ProbabilitySparseMatrix.h.

References name, PLWARNING, raise_error, x, and x2y.

                          { 
        map<int,real>& mx = x2y(x);
        if (mx.size()==0)
        {
            if (raise_error)
                //PLERROR("trying to access an invalid probability at P(%d|%d) in %s",y,x, name);
                PLWARNING("trying to access an invalid probability at P(%d|%d) in %s",y,x, name.c_str());
            return 0; 
        }
        if (mx.find(y)!=mx.end()) 
            return mx[y]; 
        return 0;
    }
real* PLearn::ProbabilitySparseMatrix::getAsFullVector ( ) [inline]

Definition at line 344 of file ProbabilitySparseMatrix.h.

References i, j, ny(), PLERROR, PLearn::RowMapSparseMatrix< T >::size(), and y2x.

    {
        // a vector of triples : (row, col, value)
        int vector_size = y2x.size() * 3;
        real* full_vector = new real[vector_size];
        int pos = 0;
        for (int i = 0; i < ny(); i++)
        {
            map<int, real>& row_i = y2x(i);
            for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
            {
                int j = it->first;
                real value = it->second;
                full_vector[pos++] = (real)i;
                full_vector[pos++] = (real)j;
                full_vector[pos++] = value;
            }
        }
        if (pos != vector_size)
            PLERROR("weird");
        return full_vector;
    }

Here is the call graph for this function:

void PLearn::ProbabilitySparseMatrix::getAsMaxSizedVectors ( int  max_size,
vector< pair< real *, int > > &  vectors 
) [inline]

Definition at line 367 of file ProbabilitySparseMatrix.h.

References i, j, ny(), PLWARNING, PLearn::RowMapSparseMatrix< T >::size(), and y2x.

    {
        if ((max_size % 3) != 0) PLWARNING("dangerous vector size (max_size mod 3 must equal 0)");

        int n_elems = y2x.size() * 3;
        int n_vecs = n_elems / max_size;
        int remaining = n_elems % max_size;
        if (remaining > 0) // padding to get sure that last block size (= remaining) is moddable by 3
        {
            n_vecs += 1;
            int mod3 = remaining % 3;
            if (mod3 != 0)
                remaining += (3 - mod3);
        }
        vectors.resize(n_vecs);
        //cerr << "(" << name << ") size = " << n_elems << endl;
        for (int i = 0; i < n_vecs; i++)
        {
            if (i == (n_vecs - 1) && remaining > 0)
            {
                vectors[i].first = new real[remaining];
                vectors[i].second = remaining;
                //cerr << "(" << name << ") i = " << i << ", size = " << remaining << endl;
            } else
            {
                vectors[i].first = new real[max_size];
                vectors[i].second = max_size;
                //cerr << "(" << name << ") i = " << i << ", size = " << max_size << endl;
            }
        }
        int pos = 0;
        for (int i = 0; i < ny(); i++)
        {
            map<int, real>& row_i = y2x(i);
            for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
            {
                int j = it->first;
                real value = it->second;
                vectors[pos / max_size].first[pos++ % max_size] = i;
                vectors[pos / max_size].first[pos++ % max_size] = j;
                vectors[pos / max_size].first[pos++ % max_size] = value;
            }
        }
        while (pos < n_elems) // pad with (0, 0, 0)
        {
            vectors[pos / max_size].first[pos++ % max_size] = 0;
            vectors[pos / max_size].first[pos++ % max_size] = 0;
            vectors[pos / max_size].first[pos++ % max_size] = 0;
        }
    }

Here is the call graph for this function:

const map<int,real>& PLearn::ProbabilitySparseMatrix::getPYx ( int  x,
bool  dont_raise_error = false 
) [inline]

Definition at line 115 of file ProbabilitySparseMatrix.h.

References PLERROR, raise_error, and x2y.

Referenced by PLearn::check_prob().

    { 
        const map<int,real>& PYx = x2y(x);
        if (raise_error && !dont_raise_error && PYx.size()==0)
            PLERROR("ProbabilitySparseMatrix::getPyx: accessing an empty column at X=%d",x);
        return PYx;
    }

Here is the caller graph for this function:

const map<int,real>& PLearn::ProbabilitySparseMatrix::getPyX ( int  y) [inline]

Definition at line 122 of file ProbabilitySparseMatrix.h.

References y2x.

Referenced by removeExtra(), and PLearn::samePos().

    { 
        return y2x(y); 
    }

Here is the caller graph for this function:

map<int,real> PLearn::ProbabilitySparseMatrix::getPYxCopy ( int  x,
bool  dont_raise_error = false 
) [inline]

Definition at line 127 of file ProbabilitySparseMatrix.h.

References PLERROR, raise_error, and x2y.

    { 
        map<int,real> PYx = x2y(x);
        if (raise_error && !dont_raise_error && PYx.size()==0)
            PLERROR("ProbabilitySparseMatrix::getPyx: accessing an empty column at X=%d",x);
        return PYx;
    }
map<int,real> PLearn::ProbabilitySparseMatrix::getPyXCopy ( int  y) [inline]

Definition at line 135 of file ProbabilitySparseMatrix.h.

References y2x.

    { 
        map<int, real> pyX = y2x(y);
        return pyX; 
    }
void PLearn::ProbabilitySparseMatrix::incr ( int  y,
int  x,
real  increment = 1 
) [inline]

Definition at line 100 of file ProbabilitySparseMatrix.h.

References x, and x2y.

Referenced by add().

    { 
        if (increment!=0)
        {
            real current_value = 0;
            map<int,real>& mx = x2y(x);
            if (mx.size()!=0)
            {
                map<int,real>::const_iterator it = mx.find(y);
                if (it!=mx.end())
                    current_value = it->second;
            }
            set(y,x,current_value+increment); 
        }
    }

Here is the caller graph for this function:

void PLearn::ProbabilitySparseMatrix::load ( string  filename) [inline]

Definition at line 338 of file ProbabilitySparseMatrix.h.

References x2y, and y2x.

    {
        y2x.load(filename + ".y2x");
        x2y.load(filename + ".x2y");
    }
int PLearn::ProbabilitySparseMatrix::nx ( ) [inline]

Definition at line 47 of file ProbabilitySparseMatrix.h.

References PLearn::RowMapSparseMatrix< T >::length(), and x2y.

Referenced by clearElements(), computeX(), fullPrint(), PLearn::operator<<(), and ProbabilitySparseMatrix().

{ return x2y.length(); }

Here is the call graph for this function:

Here is the caller graph for this function:

int PLearn::ProbabilitySparseMatrix::ny ( ) [inline]

Definition at line 48 of file ProbabilitySparseMatrix.h.

References PLearn::RowMapSparseMatrix< T >::length(), and y2x.

Referenced by computeY(), fullPrint(), getAsFullVector(), getAsMaxSizedVectors(), PLearn::operator<<(), ProbabilitySparseMatrix(), removeExtra(), and sumOfElements().

{ return y2x.length(); }

Here is the call graph for this function:

Here is the caller graph for this function:

real PLearn::ProbabilitySparseMatrix::operator() ( int  y,
int  x 
) [inline]

Definition at line 99 of file ProbabilitySparseMatrix.h.

References x.

{ return get(y,x); }
void PLearn::ProbabilitySparseMatrix::removeColumn ( int  x) [inline]

Definition at line 266 of file ProbabilitySparseMatrix.h.

References x2y, and y2x.

    {
        map<int, real>& col = x2y(x);
        for (map<int, real>::iterator it = col.begin(); it != col.end(); ++it)
        {
            int y = it->first;
            map<int,real>& yX = y2x(y);
            if (yX.size()>0)
                yX.erase(x);
        }
        col.clear();
    }
void PLearn::ProbabilitySparseMatrix::removeElem ( int  y,
int  x 
) [inline]

Definition at line 172 of file ProbabilitySparseMatrix.h.

References x.

Referenced by removeExtra().

    {
        set(y, x, 0.0, true);
    }

Here is the caller graph for this function:

void PLearn::ProbabilitySparseMatrix::removeExtra ( ProbabilitySparseMatrix m) [inline]

Definition at line 286 of file ProbabilitySparseMatrix.h.

References PLearn::Set::begin(), PLearn::Set::clear(), PLearn::Set::end(), exists(), getPyX(), PLearn::Set::insert(), ny(), removeElem(), and x.

    {
        //for (SetIterator yit = Y.begin(); yit != Y.end(); ++yit)
        int _ny = ny();
        Set x_to_remove;
        for (int y = 0; y < _ny; y++)
        {
            //int y = *yit;
            ConstSparseVec& yX = getPyX(y);
            x_to_remove.clear();
            for (SparseVec::const_iterator xit = yX.begin(); xit != yX.end(); ++xit)
            {
                int x = xit->first;
                if (!m.exists(y, x))
                    x_to_remove.insert(x);
            }
            for (SetIterator xit = x_to_remove.begin(); xit != x_to_remove.end(); ++xit)
            {
                int x = *xit;
                removeElem(y, x);
            }
        }
    }

Here is the call graph for this function:

void PLearn::ProbabilitySparseMatrix::removeRow ( int  y,
Set  X 
) [inline]

Definition at line 242 of file ProbabilitySparseMatrix.h.

References PLearn::Set::begin(), PLearn::RowMapSparseMatrix< T >::clear(), PLearn::Set::end(), x, x2y, and y2x.

                                 {
        y2x(y).clear();
        for (SetIterator it=X.begin();it!=X.end();++it)
        {
            int x = *it;
            map<int,real>& pYx = x2y(x);
            if (pYx.size()>0)
                pYx.erase(y);
        }
    }

Here is the call graph for this function:

void PLearn::ProbabilitySparseMatrix::removeRow ( int  y) [inline]

Definition at line 253 of file ProbabilitySparseMatrix.h.

References x, x2y, and y2x.

    {
        map<int, real>& row = y2x(y);
        for (map<int, real>::iterator it = row.begin(); it != row.end(); ++it)
        {
            int x = it->first;
            map<int,real>& Yx = x2y(x);
            if (Yx.size()>0)
                Yx.erase(y);
        }
        row.clear();
    }
void PLearn::ProbabilitySparseMatrix::rename ( string  new_name) [inline]

Definition at line 50 of file ProbabilitySparseMatrix.h.

References name.

{ name = new_name; }
void PLearn::ProbabilitySparseMatrix::resize ( int  ny,
int  nx 
) [inline]

Definition at line 43 of file ProbabilitySparseMatrix.h.

References PLearn::RowMapSparseMatrix< T >::resize(), x2y, and y2x.

Referenced by ProbabilitySparseMatrix().

                                { 
        x2y.resize(nx,ny); 
        y2x.resize(ny,nx);
    }

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::ProbabilitySparseMatrix::save ( string  filename) [inline]

Definition at line 332 of file ProbabilitySparseMatrix.h.

References x2y, and y2x.

    {
        y2x.save(filename + ".y2x");
        x2y.save(filename + ".x2y");
    }
void PLearn::ProbabilitySparseMatrix::set ( real full_vector,
int  n_elems 
) [inline]

Definition at line 424 of file ProbabilitySparseMatrix.h.

References clear(), and i.

    {
        clear();
        for (int i = 0; i < n_elems; i += 3)
            set((int)full_vector[i], (int)full_vector[i + 1], full_vector[i + 2]);
    }

Here is the call graph for this function:

void PLearn::ProbabilitySparseMatrix::set ( int  y,
int  x,
real  v,
bool  dont_warn_for_zero = false 
) [inline]

Definition at line 153 of file ProbabilitySparseMatrix.h.

References name, PLWARNING, x, x2y, and y2x.

Referenced by PLearn::update(), and PLearn::updateAndClearCounts().

                                                                  { 
        if (v!=0)
        {
            x2y(x,y)=v;
            y2x(y,x)=v;
        } else 
        {
            if (!dont_warn_for_zero)
                PLWARNING("setting something to 0 in ProbabilitySparseMatrix %s", name.c_str());
            map<int,real>& PYx = x2y(x);
            if (PYx.find(y)!=PYx.end())
            {
                PYx.erase(y);
                map<int,real>& PyX = y2x(y);
                PyX.erase(x);
            }
        }
    }

Here is the caller graph for this function:

void PLearn::ProbabilitySparseMatrix::setPyX ( int  y,
const map< int, real > &  pyX 
) [inline]

Definition at line 147 of file ProbabilitySparseMatrix.h.

    {
        for (map<int, real>::const_iterator it = pyX.begin(); it != pyX.end(); ++it)
            set(y, it->first, it->second);
    }
void PLearn::ProbabilitySparseMatrix::setPYx ( int  x,
const map< int, real > &  pYx 
) [inline]

Definition at line 141 of file ProbabilitySparseMatrix.h.

    {
        for (map<int, real>::const_iterator it = pYx.begin(); it != pYx.end(); ++it)
            set(it->first, x, it->second);
    }
int PLearn::ProbabilitySparseMatrix::size ( ) [inline]

Definition at line 279 of file ProbabilitySparseMatrix.h.

References PLWARNING, PLearn::RowMapSparseMatrix< T >::size(), x2y, and y2x.

    {
        if (x2y.size() != y2x.size())
            PLWARNING("x2y and y2x sizes dont match");
        return y2x.size();
    }

Here is the call graph for this function:

real PLearn::ProbabilitySparseMatrix::sumOfElements ( ) [inline]

Definition at line 431 of file ProbabilitySparseMatrix.h.

References i, j, ny(), PLearn::sum(), and y2x.

    {
        real sum = 0.0;
        for (int i = 0; i < ny(); i++)
        {
            map<int, real>& row_i = y2x(i);
            for (map<int, real>::iterator it = row_i.begin(); it != row_i.end(); ++it)
            {
                int j = it->first;
                sum += get(i, j);
            }
        }
        return sum;
    }

Here is the call graph for this function:

real PLearn::ProbabilitySparseMatrix::sumPyX ( int  y,
Set  X 
) [inline]

Definition at line 190 of file ProbabilitySparseMatrix.h.

References PLearn::Set::contains(), x, and y2x.

Referenced by computeY().

    {
        real sum_pyX = 0.0;
        map<int, real>& row = y2x(y);
        for (map<int, real>::const_iterator xit = row.begin(); xit != row.end(); ++xit)
        {
            int x = xit->first;
            if (X.contains(x))
                sum_pyX += xit->second;
        }
        return sum_pyX;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

real PLearn::ProbabilitySparseMatrix::sumPyX ( int  y) [inline]

Definition at line 214 of file ProbabilitySparseMatrix.h.

References y2x.

    {
        real sum_pyX = 0.0;
        map<int, real>& row = y2x(y);
        for (map<int, real>::const_iterator xit = row.begin(); xit != row.end(); ++xit)
        {
            sum_pyX += xit->second;
        }
        return sum_pyX;
    }
real PLearn::ProbabilitySparseMatrix::sumPYx ( int  x,
Set  Y 
) [inline]

Definition at line 177 of file ProbabilitySparseMatrix.h.

References PLearn::Set::contains(), and x2y.

Referenced by computeX(), PLearn::update(), and PLearn::updateAndClearCounts().

    {
        real sum_pYx = 0.0;
        map<int, real>& col = x2y(x);
        for (map<int, real>::const_iterator yit = col.begin(); yit != col.end(); ++yit)
        {
            int y = yit->first;
            if (Y.contains(y))
                sum_pYx += yit->second;
        }
        return sum_pYx;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

real PLearn::ProbabilitySparseMatrix::sumPYx ( int  x) [inline]

Definition at line 203 of file ProbabilitySparseMatrix.h.

References x2y.

    {
        real sum_pYx = 0.0;
        map<int, real>& col = x2y(x);
        for (map<int, real>::const_iterator yit = col.begin(); yit != col.end(); ++yit)
        {
            sum_pYx += yit->second;
        }
        return sum_pYx;
    }

Member Data Documentation

Definition at line 36 of file ProbabilitySparseMatrix.h.

Referenced by get(), ProbabilitySparseMatrix(), rename(), and set().

Definition at line 35 of file ProbabilitySparseMatrix.h.

Referenced by get(), getPYx(), getPYxCopy(), and PLearn::operator<<().


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