PLearn 0.1
|
#include <ProbabilitySparseMatrix.h>
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, real > | getPYxCopy (int x, bool dont_raise_error=false) |
map< int, real > | getPyXCopy (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) |
real * | getAsFullVector () |
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< real > | x2y |
RowMapSparseMatrix< real > | y2x |
Definition at line 25 of file ProbabilitySparseMatrix.h.
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; }
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().
void PLearn::ProbabilitySparseMatrix::clearElements | ( | ) | [inline] |
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; }
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; }
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(); }
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; } }
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; }
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; } }
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; }
Definition at line 122 of file ProbabilitySparseMatrix.h.
References y2x.
Referenced by removeExtra(), and PLearn::samePos().
{ return y2x(y); }
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; }
Definition at line 135 of file ProbabilitySparseMatrix.h.
References y2x.
{ map<int, real> pyX = y2x(y); return pyX; }
Definition at line 100 of file ProbabilitySparseMatrix.h.
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); } }
void PLearn::ProbabilitySparseMatrix::load | ( | string | filename | ) | [inline] |
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(); }
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(); }
void PLearn::ProbabilitySparseMatrix::removeColumn | ( | int | x | ) | [inline] |
Definition at line 172 of file ProbabilitySparseMatrix.h.
References x.
Referenced by removeExtra().
{ set(y, x, 0.0, true); }
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); } } }
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); } }
void PLearn::ProbabilitySparseMatrix::removeRow | ( | int | y | ) | [inline] |
void PLearn::ProbabilitySparseMatrix::rename | ( | string | new_name | ) | [inline] |
Definition at line 43 of file ProbabilitySparseMatrix.h.
References PLearn::RowMapSparseMatrix< T >::resize(), x2y, and y2x.
Referenced by ProbabilitySparseMatrix().
void PLearn::ProbabilitySparseMatrix::save | ( | string | filename | ) | [inline] |
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); } } }
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); }
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.
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; }
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; }
Definition at line 214 of file ProbabilitySparseMatrix.h.
References y2x.
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; }
Definition at line 203 of file ProbabilitySparseMatrix.h.
References x2y.
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<<().
Definition at line 33 of file ProbabilitySparseMatrix.h.
Referenced by PLearn::check_prob(), computeX(), PLearn::update(), and PLearn::updateAndClearCounts().
Definition at line 28 of file ProbabilitySparseMatrix.h.
Referenced by clear(), clearElements(), exists(), fullPrint(), get(), getPYx(), getPYxCopy(), incr(), load(), nx(), removeColumn(), removeRow(), resize(), save(), set(), size(), and sumPYx().
Definition at line 32 of file ProbabilitySparseMatrix.h.
Referenced by computeY(), PLearn::samePos(), PLearn::update(), and PLearn::updateAndClearCounts().
Definition at line 29 of file ProbabilitySparseMatrix.h.
Referenced by clear(), clearElements(), fullPrint(), getAsFullVector(), getAsMaxSizedVectors(), getPyX(), getPyXCopy(), load(), ny(), removeColumn(), removeRow(), resize(), save(), set(), size(), sumOfElements(), and sumPyX().