DenseVector.cpp
Go to the documentation of this file.
00001 #include "DenseVector.h"
00002 #include "SparseVector.h"
00003 #include "SparseMatrix.h"
00004 
00005 #include <iostream>
00006 #include <cassert>
00007 #include <sstream>
00008 #include "MathLib.h"
00009 
00010 using namespace std;
00011 using namespace momdp;
00012 namespace momdp
00013 {
00014 DenseVector::DenseVector(void)
00015 {
00016 }
00017 DenseVector::DenseVector(int _size)
00018 { 
00019         resize(_size); 
00020 }
00021 DenseVector::~DenseVector(void)
00022 {
00023 }
00024 
00025 // Arithmetic
00026 
00027 // TODO dead code, should be ok to remove
00028 //DenseVector* DenseVector::mult( SparseMatrix& A)
00029         //{
00030                 //vector<SparseVector_Entry>::const_iterator  Ai, col_end;
00031                 //assert( size() == A.size1() );
00032                 
00033                 //DenseVector* result = new DenseVector( A.size2() );
00034 
00035                 //FOR (c, A.size2()) 
00036                 //{
00037                         //col_end = A.data.begin() + A.col_starts[c+1];
00038                         //for (Ai = A.data.begin() + A.col_starts[c];
00039                                 //Ai != col_end;
00040                                 //Ai++) 
00041                         //{
00042                                         //(*result)(c) += (*this)(Ai->index) * Ai->value;
00043                         //}
00044                 //}
00045 
00046                 //return result;
00047         //}
00048 
00049 #define DenseVector_Operator( OP )                                                      \
00050         void DenseVector::operator OP(const DenseVector& x)                     \
00051 {                                                                                                                       \
00052         vector<REAL_VALUE>::const_iterator  xi;                 \
00053         assert( size() == x.size() );                                                   \
00054         xi = x.data.begin();                                                                    \
00055         FOREACH_NOCONST(REAL_VALUE, di,  data) {                    \
00056         (*di) OP (*xi);                                                                         \
00057         xi++;                                                                                           \
00058 }                                                                                                               \
00059 }
00060 
00061 
00062 DenseVector_Operator( += );
00063 DenseVector_Operator( -= );
00064 
00065 // TODO: += -= for sparse vector
00066 
00067 void DenseVector::operator*=(REAL_VALUE s)
00068 {
00069         FOREACH_NOCONST(REAL_VALUE, di,  data) {
00070                 (*di) *= s;
00071         }
00072 }
00073 
00074 int DenseVector::argSampleDist()
00075 {
00076         REAL_VALUE randNumber = unit_rand();
00077         REAL_VALUE sum = 0.0;
00078         int state = 0;
00079         FOREACH(REAL_VALUE, entry,  data) 
00080         {
00081                 sum += *entry;
00082                 if(randNumber < sum )
00083                 {
00084                         return state; 
00085                 }
00086                 state ++;
00087         }
00088         return state-1;
00089 }
00090 
00091 
00092 
00093 // capacity
00094 void DenseVector::resize(int _size, REAL_VALUE value)
00095 {
00096         assert(0 == value);
00097         data.resize( _size );
00098         FOREACH_NOCONST(double, di,  data) 
00099         {
00100                 (*di) = value;
00101         }
00102         
00103 }
00104 
00105 void DenseVector::resize(int _size)
00106 {
00107         resize(_size, 0.0);
00108 }
00109 
00110 // IO
00111 
00112 void DenseVector::read(istream& in)
00113 {
00114         int num_entries;
00115 
00116         in >> num_entries;
00117         resize( num_entries );
00118         FOR (i, num_entries) 
00119         {
00120                 in >> data[i];
00121         }
00122 
00123 #if 0
00124         std::cout << "dense read: size = " << data.size() << std::endl;
00125         FOR (i, data.size()) {
00126                 if (i == 10) {
00127                         std::cout << "..." << std::endl;
00128                         break;
00129                 }
00130                 std::cout << i << " " << data[i] << std::endl;
00131         }
00132 #endif
00133 }
00134 
00135 ostream& DenseVector::write(ostream& out) const
00136 {
00137         out << size() << std::endl;
00138         FOREACH(REAL_VALUE, x,  data) 
00139         {
00140                 out << (*x) << " ";
00141         }
00142         return out;
00143 }
00144 
00145 
00146 string DenseVector::ToString() const
00147 {
00148         stringstream out;
00149         out << "[";
00150         FOR(i, data.size())
00151         {
00152                 out << data[i];
00153                 if ( i != data.size()-1 )
00154                         out << ",";
00155         }
00156         out << "]";
00157         return out.str();
00158 }
00159 
00160 }
00161 


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