SparseMatrix.h
Go to the documentation of this file.
00001 #ifndef SparseMatrix_H
00002 #define SparseMatrix_H
00003 
00004 #include <vector>
00005 #include <string>
00006 #include <iostream>
00007 #include <cassert>
00008 
00009 #include "MObject.h"
00010 #include "SparseVector.h"
00011 
00012 
00013 using namespace std;
00014 using namespace momdp;
00015 namespace momdp 
00016 {
00017 
00018         class DenseVector;
00019 
00020         class SparseCol 
00021         {
00022         public:
00023             typedef vector<SparseVector_Entry>::const_iterator iterator;
00024             inline SparseCol(): _begin(NULL), _end(NULL) {}
00025             inline SparseCol(iterator b, iterator e): _begin(b), _end(e) {}
00026             inline iterator begin() { return _begin; }
00027             inline iterator end() { return _end; }
00028             inline bool empty() { return _begin == _end; }
00029         private:
00030             iterator _begin;
00031             iterator _end;
00032         };
00033 
00034         class SparseMatrix : public MObject
00035         {
00036                 friend class SparseVector;
00037                 friend class DenseVector;
00038         public:
00039                 vector< SparseVector_Entry > data;
00040                 int size1_, size2_;
00041                 vector< int > cols_start;
00042                 vector< int > cols;
00043 
00044         private:
00045                 int colEnd(int index) const;
00046 
00047 
00048 
00049         public:
00050                 SparseMatrix(void) : size1_(0), size2_(0) 
00051                 {}
00052 
00053                 SparseMatrix(int _size1, int _size2) 
00054                 {
00055                         resize(_size1,_size2); 
00056                 }
00057                 virtual ~SparseMatrix(void)
00058                 {
00059                 }
00060 
00061                 REAL_VALUE operator()(int r, int c) const;
00062 
00063                 SparseCol col(int c) const;
00064 
00065                 int size1(void) const { return size1_; }
00066                 int size2(void) const { return size2_; }
00067                 int filled(void) const { return data.size(); }
00068 
00069                 void resize(int _size1, int _size2);
00070 
00071                 void push_back(int row, int col, REAL_VALUE value);
00072 
00073                 // if resize()/push_back() are used to initialize, you must call
00074                 // canonicalize() before performing any operations with the matrix
00075 
00076                 void canonicalize(void);
00077 
00078                 const vector<int>& nonEmptyColumns() const;
00079                 bool isColumnEmpty(int c) const;
00080 
00081                 // Arithmetic
00082                 REAL_VALUE getMaxValue();
00083 
00084                 // result = A * x
00085                 void mult(const DenseVector& x, DenseVector& result) const;
00086                 void mult(const SparseVector& x, DenseVector& result) const;
00087                 DenseVector* mult(const SparseVector& x) const;
00088                 DenseVector* mult(const DenseVector& x) const;
00089 
00090                 // result = x * A
00091                 void leftMult(const DenseVector& x, DenseVector& result) const;
00092                 void leftMult(const SparseVector& x, DenseVector& result) const;
00093 
00094                 // IO
00095                 void read(std::istream& in);
00096                 ostream& write(std::ostream& out) const;
00097 
00098         };
00099 }
00100 
00101 #endif
00102 


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