SparseVector.h
Go to the documentation of this file.
00001 #ifndef SparseVector_H
00002 #define SparseVector_H
00003 
00004 #include <vector>
00005 #include <string>
00006 #include <cmath>
00007 
00008 #include "MObject.h"
00009 #include "Const.h"
00010 #include "md5wrapper.h"
00011 #include "md5.h"
00012 //#include "Belief.h"  // Commented out during merge on 02102009
00013 
00014 using namespace std;
00015 using namespace momdp;
00016 
00017 namespace momdp 
00018 {
00019         // SparseVector_Entry should not inherit from MObject, becuase MObject's size is very significant compared to SparseVector_Entry
00020         class SparseVector_Entry
00021         {
00022         public:
00023                 int index;
00024                 REAL_VALUE value;
00025 
00026                 SparseVector_Entry(void) {}
00027                 SparseVector_Entry(int _index, REAL_VALUE _value) :     index(_index), value(_value)
00028                 {}
00029         };
00030 
00031         class SparseVector : public MObject
00032         {
00033                 friend class SparseMatrix;
00034                 friend class DenseVector;
00035 
00036         public:
00037                 void copyFrom(const SparseVector& x)
00038                 {
00039                         this->data = x.data;
00040                         this->logicalSize = x.logicalSize;
00041                         this->md5hash = x.md5hash;
00042                 }
00043                 vector< SparseVector_Entry > data;
00044                 void push_back( int index, REAL_VALUE value);
00045 
00046         protected:
00047 
00048                 int logicalSize; // the logical length of this sparse vector 
00049 
00050 
00051 
00052                 void add(SparseVector& result,  const SparseVector& x,  const SparseVector& y);
00053                 void subtract(SparseVector& result,     const SparseVector& x, const SparseVector& y);
00054 
00055 
00056                 // Hashing 
00057                 string md5hash;
00058 
00059         public:
00060                 SparseVector(void);
00061                 SparseVector(int _size) { resize(_size); }
00062                 virtual ~SparseVector(void);
00063 
00064 
00065                 // Sealing
00066                 void finalize();
00067 
00068 
00069                 // Arithmetc
00070                 REAL_VALUE getEntropy();
00071 
00072                 int argmax();
00073                 REAL_VALUE operator()( int index) const;
00074                 REAL_VALUE maskedSum( vector<int> mask);
00075                 void copyIndex( vector<int>& result);
00076                 void copyValue( vector<REAL_VALUE>& result);
00077 
00078 
00079                 void operator+=(const SparseVector& x);
00080                 void operator-=(const SparseVector& x);
00081 
00082                 void operator*=(REAL_VALUE s);
00083                 bool operator==(const SparseVector& x) const;
00084 
00085 
00086                 REAL_VALUE delta(const SparseVector& x) const;
00087                 REAL_VALUE totalDifference(const SparseVector& x) const;
00088                 REAL_VALUE totalHashDifference(const SparseVector& x) const;
00089 
00090                 int sampleState() const;
00091                 int argSampleDist() const;
00092 
00093                 double norm_inf()
00094                 {
00095                         double val, max = 0.0;
00096                         FOREACH(SparseVector_Entry, xi,  data) 
00097                         {
00098                                 val = fabs(xi->value);
00099                                 if (val > max) max = val;
00100                         }
00101                         return max;
00102                 }
00103 
00104                 REAL_VALUE norm_1()
00105                 {
00106                         double sum = 0.0;
00107                         FOREACH(SparseVector_Entry, xi,  data) 
00108                         {
00109                                 sum += fabs(xi->value);
00110                         }
00111                         return sum;
00112                 }
00113 
00114                 double norm_2()
00115                 {
00116                         double sum = 0.0;
00117                         FOREACH(SparseVector_Entry, xi,  data) 
00118                         {
00119                                 sum += (xi->value)*(xi->value);
00120                         }
00121                         return sum;
00122                 }
00123 
00124 
00125                 bool isDifferentByAtLeastSingleEntry(const SparseVector& x, const REAL_VALUE& threshold) const;
00126 
00127                 // Capacity
00128                 int size(void) const { return logicalSize; }
00129                 int filled(void) const { return data.size(); }
00130 
00131                 void resize( int _size);
00132 
00133                 //void push_back( int index, REAL_VALUE value);
00134                 //void canonicalize(void) {}
00135 
00136 
00137                 // IO
00138                 void read(std::istream& in);
00139                 std::ostream& write(std::ostream& out) const;
00140 
00141                 //void dumpSparse(std::ostream& out) const;
00142                 //void dumpDense(std::ostream& out, int numStates) const;
00143 
00144 
00145 
00146 
00147                 string md5HashValue();
00148                 string ToString() const;
00149 
00150                 string convToString(unsigned char *bytes);
00151                 string getHashFromSparseVectorTruncated(SparseVector& x);
00152                 string getHashFromCVector(SparseVector& x);
00153 
00154         };
00155 
00156         // result = x .* y [for all i, result(i) = x(i) * y(i)]
00157         template <class T, class U> double inner_prod_SparseVector_internal(T xbegin, T xend, U ybegin, U yend)
00158         {
00159                 double sum = 0.0;
00160                 U yi = ybegin;
00161                 for (T xi = xbegin; xi != xend; xi++) {
00162                         while (1) {
00163                                 if (yi == yend) return sum;
00164                                 if (yi->index >= xi->index) {
00165                                         if (yi->index == xi->index) {
00166                                                 sum += xi->value * yi->value;
00167                                         }
00168                                         break;
00169                                 }
00170                                 yi++;
00171                         }
00172                 }
00173                 return sum;
00174         }
00175 
00176 }
00177 
00178 
00179 #endif


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29