ToPS
|
00001 #ifndef SPARSE_MATRIX_HPP 00002 #define SPARSE_MATRIX_HPP 00003 00004 #include "crossplatform.hpp" 00005 #include "util.hpp" 00006 #include <vector> 00007 #include <map> 00008 #include <boost/shared_ptr.hpp> 00009 #include <fstream> 00010 #include <sstream> 00011 00012 using namespace std; 00013 00014 namespace tops{ 00015 00016 class DLLEXPORT SparseMatrix{ 00017 00018 private: 00019 static float postProbs_thresh; 00020 static float log_postProbs_thresh; 00021 vector< map<int,float> > M; 00022 int _ncolumns; 00023 int _nrows; 00024 int nextr; 00025 map<int,float>::iterator nextc; 00026 00027 public: 00028 typedef boost::shared_ptr<SparseMatrix> SparseMatrixPtr; 00029 00030 SparseMatrix(){} 00031 SparseMatrix(int nrows, int ncols){ 00032 M.resize(nrows); 00033 _nrows = nrows; 00034 _ncolumns = ncols; 00035 nextr = 0; 00036 nextc = M[0].begin(); 00037 } 00038 00039 SparseMatrix(int nrows, int ncols, fMatrix postProbs){ 00040 M.resize(nrows); 00041 _nrows = nrows; 00042 _ncolumns = ncols; 00043 for(int i = 0; i < nrows; i++){ 00044 for(int j = 0; j < ncols; j++){ 00045 if(postProbs(i,j) >= postProbs_thresh){ 00046 M[i][j] = postProbs(i,j); 00047 } 00048 } 00049 } 00050 nextr = 0; 00051 nextc = M[0].begin(); 00052 } 00053 00054 SparseMatrix(fMatrix postProbs, SparseMatrixPtr reference, float n){ 00055 M.resize(reference->nrows()); 00056 _nrows = reference->nrows(); 00057 _ncolumns = reference->ncols(); 00058 map<int,float>::iterator it; 00059 for(int i = 0; i < _nrows; i++){ 00060 for(it = reference->lineBegin(i); it != reference->lineEnd(i); it++){ 00061 if((postProbs(i,it->first)/n) >= postProbs_thresh){ 00062 M[i][it->first] = postProbs(i,it->first)/n; 00063 } 00064 } 00065 } 00066 nextr = 0; 00067 nextc = M[0].begin(); 00068 } 00069 00070 SparseMatrix(SparseMatrixPtr reference){ 00071 M.resize(reference->nrows()); 00072 _nrows = reference->nrows(); 00073 _ncolumns = reference->ncols(); 00074 map<int,float>::iterator it; 00075 for(int i = 0; i < _nrows; i++){ 00076 for(it = reference->lineBegin(i); it != reference->lineEnd(i); it++){ 00077 M[i][it->first] = it->second; 00078 } 00079 } 00080 nextr = 0; 00081 nextc = M[0].begin(); 00082 } 00083 00084 static void setppthresh(float n){ 00085 postProbs_thresh = n; 00086 log_postProbs_thresh = log(n); 00087 } 00088 00089 void applyPrediction(SparseMatrixPtr predMatrix1, SparseMatrixPtr predMatrix2); 00090 void buildPredMatrix(int nrows, int ncols, fMatrix &postProbs); 00091 void getfMatrixTimesX(fMatrix &fM, float x); 00092 void getfMatrixPred(fMatrix &fM); 00093 void leftXright(SparseMatrixPtr &N, fMatrix &OUT, float n); 00094 void leftTransXright(SparseMatrixPtr &N, fMatrix &OUT, float n); 00095 void leftXright(SparseMatrixPtr &N, fMatrix &OUT); 00096 void leftTransXright(SparseMatrixPtr &N, fMatrix &OUT); 00097 void transposeOf(SparseMatrixPtr &A); 00098 void resize(int nrows, int ncols); 00099 int nrows(); 00100 int ncols(); 00101 bool next(int *i, int *j, float *prob); 00102 vector< map<int,float> > &matrix(); 00103 map<int,float>::iterator lineBegin(int i); 00104 map<int,float>::iterator lineEnd(int i); 00105 void printMatrix(); 00106 void removeLastLine(); 00107 void removeLastColumn(); 00108 void addGaps(SparseMatrixPtr ppGap1, SparseMatrixPtr ppGap2); 00109 }; 00110 typedef boost::shared_ptr<SparseMatrix> SparseMatrixPtr; 00111 }; 00112 #endif 00113 00114