PLearn 0.1
|
#include <SparseVMatrix.h>
Public Member Functions | |
SparseVMatrix () | |
SparseVMatrix (VMat m) | |
This builds a sparse representation in memory of the VMat m passed as argument. | |
SparseVMatrix (const string &filename) | |
This reloads a previously saved sparse VMatrix. | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SparseVMatrix * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Simply calls inherited::build() then build_(). | |
virtual real | dot (int i1, int i2, int inputsize) const |
Returns the dot product between row i1 and row i2 (considering only the inputsize first elements). | |
virtual real | dot (int i, const Vec &v) const |
Returns the result of the dot product between row i and the given vec (only v.length() first elements of row i are considered). | |
virtual void | save (const PPath &filename) const |
calls write | |
virtual | ~SparseVMatrix () |
Static Public Member Functions | |
static string | _classname_ () |
SparseVMatrix. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
virtual void | getNewRow (int i, const Vec &v) const |
This is the only method requiring implementation in subclasses. | |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declare this class' options. | |
Protected Attributes | |
int | nelements |
total number of non-zero elements in the VMatrix | |
unsigned short * | positions |
float * | values |
SparseVMatrixRow * | rows |
Private Types | |
typedef RowBufferedVMatrix | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 68 of file SparseVMatrix.h.
typedef RowBufferedVMatrix PLearn::SparseVMatrix::inherited [private] |
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 70 of file SparseVMatrix.h.
PLearn::SparseVMatrix::SparseVMatrix | ( | ) | [inline] |
PLearn::SparseVMatrix::SparseVMatrix | ( | VMat | m | ) |
This builds a sparse representation in memory of the VMat m passed as argument.
The original fieldinfos are copied as-is.
Definition at line 58 of file SparseVMatrix.cc.
References PLearn::fast_exact_is_equal(), PLearn::VMatrix::fieldinfos, i, j, PLearn::VMatrix::length(), PLearn::VMat::length(), PLearn::SparseVMatrixRow::nelements, nelements, PLearn::VMFieldStat::nmissing(), PLearn::VMFieldStat::nnegative(), PLearn::VMFieldStat::npositive(), PLERROR, positions, PLearn::SparseVMatrixRow::row_startpos, rows, PLearn::VMatrix::updateMtime(), values, and PLearn::VMat::width().
: inherited(m.length(),m.width()), nelements(0), positions(0), values(0), rows(0) { fieldinfos = m->getFieldInfos(); // Copy the field infos updateMtime(m); if(m.width()>USHRT_MAX) PLERROR("In SparseVMatrix constructor: m.width()=%d can't be greater than USHRT_MAX=%d",m.width(),USHRT_MAX); Vec v(m.width()); real* vptr = v.data(); // First count nelements nelements = 0; if(m->hasStats()) // use the stats! { for(int j=0; j<m.width(); j++) { const VMFieldStat& st = m->fieldStat(j); nelements += st.nmissing() + st.npositive() + st.nnegative(); } } else // let's count them ourself { for(int i=0; i<m.length(); i++) { m->getRow(i,v); for(int j=0; j<v.length(); j++) if(!fast_exact_is_equal(vptr[j], 0.)) nelements++; } } // Now allocate space for those elements if(nelements>0) { positions = new unsigned short[nelements]; values = new float[nelements]; int l=length(); rows = new SparseVMatrixRow[l]; int pos = 0; // Fill the representation for(int i=0; i<m.length(); i++) { m->getRow(i,v); SparseVMatrixRow& r = rows[i]; r.row_startpos = pos; int nelem = 0; for(int j=0; j<v.length(); j++) if(!fast_exact_is_equal(vptr[j], 0.)) { positions[pos] = j; values[pos] = (float)vptr[j]; pos++; nelem++; } r.nelements = nelem; } } }
PLearn::SparseVMatrix::SparseVMatrix | ( | const string & | filename | ) |
This reloads a previously saved sparse VMatrix.
Definition at line 51 of file SparseVMatrix.cc.
References PLearn::Object::load(), and PLearn::VMatrix::updateMtime().
PLearn::SparseVMatrix::~SparseVMatrix | ( | ) | [virtual] |
string PLearn::SparseVMatrix::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 49 of file SparseVMatrix.cc.
OptionList & PLearn::SparseVMatrix::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 49 of file SparseVMatrix.cc.
RemoteMethodMap & PLearn::SparseVMatrix::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 49 of file SparseVMatrix.cc.
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 49 of file SparseVMatrix.cc.
Object * PLearn::SparseVMatrix::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 49 of file SparseVMatrix.cc.
StaticInitializer SparseVMatrix::_static_initializer_ & PLearn::SparseVMatrix::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 49 of file SparseVMatrix.cc.
void PLearn::SparseVMatrix::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::VMatrix.
Definition at line 121 of file SparseVMatrix.cc.
References PLearn::VMatrix::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::SparseVMatrix::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::VMatrix.
Definition at line 128 of file SparseVMatrix.cc.
Referenced by build().
{
// TODO
}
string PLearn::SparseVMatrix::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 49 of file SparseVMatrix.cc.
void PLearn::SparseVMatrix::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declare this class' options.
Reimplemented from PLearn::VMatrix.
Definition at line 134 of file SparseVMatrix.cc.
References PLearn::VMatrix::declareOptions().
{ inherited::declareOptions(ol); }
static const PPath& PLearn::SparseVMatrix::declaringFile | ( | ) | [inline, static] |
SparseVMatrix * PLearn::SparseVMatrix::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 49 of file SparseVMatrix.cc.
Returns the result of the dot product between row i and the given vec (only v.length() first elements of row i are considered).
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 226 of file SparseVMatrix.cc.
References PLearn::TVec< T >::data(), i, PLearn::TVec< T >::length(), PLearn::VMatrix::length(), n, PLearn::SparseVMatrixRow::nelements, nelements, PLERROR, positions, PLearn::SparseVMatrixRow::row_startpos, rows, values, and PLearn::VMatrix::width().
{ #ifdef BOUNDCHECK if(i<0 || i>=length() || v.length()>width()) PLERROR("IN SparseVMatrix::dot OUT OF BOUNDS"); #endif if(nelements==0) return 0.; SparseVMatrixRow row_i = rows[i]; float* valueptr = values + row_i.row_startpos; unsigned short* positionptr = positions + row_i.row_startpos; int n = row_i.nelements; real* vdata = v.data(); real res = 0.; while(n--) { int nextpos = (int) *positionptr++; real nextval = (real) *valueptr++; if(nextpos>=v.length()) break; res += nextval*vdata[nextpos]; } return res; }
Returns the dot product between row i1 and row i2 (considering only the inputsize first elements).
The default version in VMatrix is somewhat inefficient, as it repeatedly calls get(i,j) The default version in RowBufferedVMatrix is a little better as it buffers the 2 Vecs between calls in case one of them is needed again. But the real strength of this method is for specialised and efficient versions in subbclasses. This method is typically used by SmartKernels so that they can compute kernel values between input samples efficiently.
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 173 of file SparseVMatrix.cc.
References PLearn::VMatrix::length(), PLearn::SparseVMatrixRow::nelements, nelements, PLERROR, positions, PLearn::SparseVMatrixRow::row_startpos, rows, values, and PLearn::VMatrix::width().
{ #ifdef BOUNDCHECK if(i1<0 || i1>=length() || i2<0 || i2>=length() || inputsize>width()) PLERROR("IN SparseVMatrix::dot OUT OF BOUNDS"); #endif if(nelements==0) return 0.; SparseVMatrixRow row_1 = rows[i1]; float* valueptr_1 = values + row_1.row_startpos; unsigned short* positionptr_1 = positions + row_1.row_startpos; int n_1 = row_1.nelements; SparseVMatrixRow row_2 = rows[i2]; float* valueptr_2 = values + row_2.row_startpos; unsigned short* positionptr_2 = positions + row_2.row_startpos; int n_2 = row_2.nelements; real res = 0.; while(n_1 && n_2) { if(*positionptr_1>=inputsize) break; if(*positionptr_1==*positionptr_2) { res += (*valueptr_1)*(*valueptr_2); positionptr_1++; valueptr_1++; n_1--; positionptr_2++; valueptr_2++; n_2--; } else if(*positionptr_1<*positionptr_2) { positionptr_1++; valueptr_1++; n_1--; } else { positionptr_2++; valueptr_2++; n_2--; } } return res; }
This is the only method requiring implementation in subclasses.
Implements PLearn::RowBufferedVMatrix.
Definition at line 139 of file SparseVMatrix.cc.
References PLearn::TVec< T >::clear(), PLearn::TVec< T >::data(), i, j, PLearn::VMatrix::length(), PLearn::TVec< T >::length(), n, nelements, PLearn::SparseVMatrixRow::nelements, PLERROR, positions, PLearn::SparseVMatrixRow::row_startpos, rows, values, and PLearn::VMatrix::width().
{ #ifdef BOUNDCHECK if(i<0 || i>=length()) PLERROR("In SparseVMatrix::getNewRow, row number i=%d OUT OF BOUNDS (matrix is %dx%d)",i,length(),width()); if(v.length()!=width()) PLERROR("In SparseVMatrix::getNewRow, length of v (%d) is different from width of VMatris (%d)",v.length(),width()); #endif if(nelements==0) v.clear(); else { SparseVMatrixRow row_i = rows[i]; float* valueptr = values + row_i.row_startpos; unsigned short* positionptr = positions + row_i.row_startpos; int n = row_i.nelements; real* vdata = v.data(); int j = 0; while(n--) { int nextpos = (int) *positionptr++; real nextval = (real) *valueptr++; while(j<nextpos) vdata[j++] = 0.; vdata[j++] = nextval; } while(j<v.length()) vdata[j++] = 0.; } }
OptionList & PLearn::SparseVMatrix::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 49 of file SparseVMatrix.cc.
OptionMap & PLearn::SparseVMatrix::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 49 of file SparseVMatrix.cc.
RemoteMethodMap & PLearn::SparseVMatrix::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 49 of file SparseVMatrix.cc.
virtual void PLearn::SparseVMatrix::save | ( | const PPath & | filename | ) | const [inline, virtual] |
calls write
Reimplemented from PLearn::VMatrix.
Definition at line 109 of file SparseVMatrix.h.
References PLearn::save().
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 95 of file SparseVMatrix.h.
int PLearn::SparseVMatrix::nelements [protected] |
total number of non-zero elements in the VMatrix
Definition at line 73 of file SparseVMatrix.h.
Referenced by dot(), getNewRow(), SparseVMatrix(), and ~SparseVMatrix().
unsigned short* PLearn::SparseVMatrix::positions [protected] |
Definition at line 74 of file SparseVMatrix.h.
Referenced by dot(), getNewRow(), SparseVMatrix(), and ~SparseVMatrix().
SparseVMatrixRow* PLearn::SparseVMatrix::rows [protected] |
Definition at line 77 of file SparseVMatrix.h.
Referenced by dot(), getNewRow(), SparseVMatrix(), and ~SparseVMatrix().
float* PLearn::SparseVMatrix::values [protected] |
Definition at line 75 of file SparseVMatrix.h.
Referenced by dot(), getNewRow(), SparseVMatrix(), and ~SparseVMatrix().