MathLib.h
Go to the documentation of this file.
00001 #ifndef MathLib_H
00002 #define MathLib_H
00003 
00004 #include <algorithm>
00005 #include <cstdlib>
00006 #include <cmath>
00007 
00008 #include "mdpCassandra.h"
00009 #include "pomdpCassandraWrapper.h"
00010 
00011 #include "SparseVector.h"
00012 #include "SparseMatrix.h"
00013 #include "DenseVector.h"
00014 
00015 using namespace std;
00016 using namespace momdp;
00017 namespace momdp 
00018 {
00019 
00020         class MathLib
00021         {
00022         public:
00023                 MathLib(void);
00024                 ~MathLib(void);
00025         };
00026 
00027 
00028         inline REAL_VALUE unit_rand(void)
00029         {
00030                 return ((REAL_VALUE)rand())/RAND_MAX;
00031         }
00032 
00033         inline REAL_VALUE BeliefEntryTruncate(REAL_VALUE input)
00034         {
00035                 input *= 10^BELIEF_DECIMAL_PRECISION;
00036                 input += 0.5;
00037                 input = floor(input);
00038                 return input;
00039         }
00040 
00041         struct kmatrix_entry {
00042                 unsigned int r, c;
00043                 double value;
00044 
00045                 kmatrix_entry(void) {}
00046                 kmatrix_entry(unsigned int _r,
00047                         unsigned int _c,
00048                         double _value) :
00049                 r(_r),
00050                         c(_c),
00051                         value(_value)
00052                 {}
00053         };
00054 
00055         struct kmatrix
00056         {
00057                 unsigned int size1_, size2_;
00058                 std::vector< kmatrix_entry > data;
00059 
00060                 kmatrix(void) : size1_(0), size2_(0) {}
00061                 kmatrix(unsigned int _size1, unsigned int _size2) { resize(_size1,_size2); }
00062 
00063                 double operator()(unsigned int r, unsigned int c) const;
00064 
00065                 unsigned int size1(void) const { return size1_; }
00066                 unsigned int size2(void) const { return size2_; }
00067                 unsigned int filled(void) const { return data.size(); }
00068                 void clear(void);
00069                 void resize(unsigned int _size1, unsigned int _size2, double value = 0.0);
00070                 void push_back(unsigned int r, unsigned int c, double value);
00071                 void canonicalize(void);
00072 
00073                 void read(std::istream& in);
00074         };
00075 
00076 
00077         // result = x
00078         inline void copy(SparseVector& result, const SparseVector& x) 
00079         {
00080                 result.copyFrom(x);
00081         }
00082         inline void copy(DenseVector& result, const DenseVector& x) 
00083         { 
00084                 // copy all the data
00085                 result.copyFrom(x);
00086         }
00087 
00088         // result = x
00089         void copy(DenseVector& result, const SparseVector& x);
00090 
00091         // result = x
00092         void copy(SparseVector& result, const DenseVector& x);
00093 
00094         // result = A (side-effect: canonicalizes A)
00095         void copy(SparseMatrix& result, kmatrix& A);
00096 
00097         // result = A(.,c)
00098         void copy_from_column(SparseVector& result, const SparseMatrix& A, unsigned int c);
00099 
00100         // result = A(:,c)
00101         void copy_from_column(DenseVector& result, const SparseMatrix& A, unsigned int c);
00102         // A(r,c) = v
00103         void kmatrix_set_entry(kmatrix& A, unsigned int r, unsigned int c,
00104                 double v);
00105 
00106         // A = A'
00107         void kmatrix_transpose_in_place(kmatrix& A);
00108 
00109 
00110         // result = A * x
00111         void mult(DenseVector& result, const SparseMatrix& A, const SparseVector& x);
00112 
00113         void mult(DenseVector& result, const SparseMatrix& A, const DenseVector& x);
00114 
00115 
00116         void mult(SparseVector& result, const SparseMatrix& A, const SparseVector& x);
00117 
00118 
00119         // result = x * A
00120         void mult(DenseVector& result, const DenseVector& x, const SparseMatrix& A);
00121 
00122 
00123 
00124         void mult(DenseVector& result, const SparseVector& x, const SparseMatrix& A);
00125 
00126         void mult(SparseVector& result, const SparseVector& x, const SparseMatrix& A);
00127 
00128 
00129         // result = x .* y [for all i, result(i) = x(i) * y(i)]
00130         void emult(DenseVector& result, const DenseVector& x, const DenseVector& y);
00131 
00132 
00133         // result = x .* y [for all i, result(i) = x(i) * y(i)]
00134         void emult(SparseVector& result, const SparseVector& x, const SparseVector& y);
00135 
00136         // result = A(:,c) .* x
00137         void emult_column(SparseVector& result, const SparseMatrix& A, unsigned int c, const SparseVector& x);
00138 
00139         // result = x .* y
00140         void emult(DenseVector& result, const DenseVector& x, const SparseVector& y);
00141 
00142         // result = A(:,c) .* x
00143         void emult_column(DenseVector& result, const SparseMatrix& A, unsigned int c, const DenseVector& x);
00144 
00145         // return x' * y
00146         double inner_prod(const DenseVector& x, const SparseVector& y);
00147 
00148         // return x' * y
00149         double inner_prod(const SparseVector& x, const SparseVector& y);
00150 
00151         double inner_prod_binary(const SparseVector& x, const SparseVector& y);
00152         int binarySearch(const SparseVector &x, int key);
00153         int quickLog2(int n);
00154 
00155         // result = max(result,x)
00156         void max_assign(DenseVector& result, const DenseVector& x);
00157         // return A(:,c)' * y
00158         double inner_prod_column(const SparseMatrix& A, unsigned int c, const SparseVector& y);
00159 
00160 
00161         // return true if for all i: x(i) >= y(i) - eps
00162         bool dominates(const DenseVector& x, const DenseVector& y, double eps);
00163 
00164         // return true if for all i: x(i) >= y(i) - eps
00165         bool dominates(const SparseVector& x, const SparseVector& y, double eps);
00166 
00167 
00168         // result = A
00169         void copy(SparseMatrix& result, CassandraMatrix A, int numColumns);
00170 
00171         // result = A
00172         void copy(kmatrix& result, CassandraMatrix A, int numColumns);
00173 
00174         /**********************************************************************
00175         * FUNCTIONS
00176         **********************************************************************/
00177 
00178         // result = A
00179         inline void copy(SparseMatrix& result, CassandraMatrix A, int numColumns)
00180         {
00181                 kmatrix B;
00182                 copy(B, A, numColumns);
00183                 copy(result, B);
00184         }
00185 
00186         // result = A
00187         inline void copy(kmatrix& result, CassandraMatrix A, int numColumns)
00188         {
00189                 result.resize(A->num_rows, numColumns);
00190                 FOR (r, A->num_rows) {
00191                         int rowOffset = A->row_start[r];
00192                         int rowSize = A->row_length[r];
00193                         FOR (i, rowSize) {
00194                                 int j = rowOffset + i;
00195                                 int c = A->col[j];
00196                                 double val = A->mat_val[j];
00197                                 result.push_back(r, c, val);
00198                         }
00199                 }
00200                 result.canonicalize();
00201         }
00202 
00203 
00204         // Set all entries to zero.
00205         //void set_to_zero(dmatrix& M);
00206         void set_to_zero(kmatrix& M);
00207         void set_to_zero(SparseMatrix& M);
00208         void set_to_zero(DenseVector& v);
00209         void set_to_zero(SparseVector& v);
00210 
00211         /**********************************************************************
00212         * FUNCTIONS
00213         **********************************************************************/
00214 
00215         // Set all entries to zero.
00216         /*inline void set_to_zero(dmatrix& M)
00217         {
00218         M.resize( M.size1(), M.size2() );
00219         }*/
00220 
00221         inline void set_to_zero(kmatrix& M)
00222         {
00223                 M.resize( M.size1(), M.size2() );
00224         }
00225 
00226         inline void set_to_zero(SparseMatrix& M)
00227         {
00228                 M.resize( M.size1(), M.size2() );
00229         }
00230 
00231         inline void set_to_zero(DenseVector& v)
00232         {
00233                 v.resize( v.size() );
00234         }
00235 
00236         inline void set_to_zero(SparseVector& v)
00237         {
00238                 v.resize( v.size() );
00239         }
00240 
00241         // Index of maximum element of a vector
00242         int argmax_elt(const DenseVector& v);
00243         int argmax_elt(const SparseVector& v);
00244 
00245         // b represents a discrete probability distribution Pr(outcome = i) = b(i).
00246         // Chooses an outcome according to the distribution.
00247         inline int chooseFromDistribution(const DenseVector& b) 
00248         {
00249                 double r = unit_rand();
00250                 FOR (i, b.size())
00251                 {
00252                         r -= b(i);
00253                         if (r <= 0) 
00254                         {
00255                                 return i;
00256                         }
00257                 }
00258                 return b.data.size()-1;
00259         }
00260 
00261         inline int chooseFromDistribution(const SparseVector& b) {
00262                 double r = unit_rand();
00263                 int lastIndex = 0;
00264                 FOR_CV(b) 
00265                 {
00266                         r -= CV_VAL(b);
00267                         lastIndex = CV_INDEX(b);
00268                         if (r <= 0)
00269                         {
00270                                 return lastIndex ;
00271                         }
00272                 }
00273                 return lastIndex;
00274         }
00275 
00276         inline int chooseFromDistribution(const SparseVector& b, double r)
00277         {
00278                 int lastIndex = 0;
00279                 FOR_CV(b) 
00280                 {
00281                         r -= CV_VAL(b);
00282                         lastIndex = CV_INDEX(b);
00283                         if (r <= 0)
00284                         {
00285                                 return lastIndex;
00286                         }
00287                 }
00288                 return lastIndex;
00289         }
00290 
00291 
00292         // TODO :: Remove kmatrix implementation
00293 }
00294 
00295 #endif
00296 
00297 


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