ToPS
SparseMatrix.cpp
00001 #include "SparseMatrix.hpp"
00002 #include <iostream>
00003 
00004 using namespace std;
00005 
00006 namespace tops{
00007 
00008   float SparseMatrix::postProbs_thresh = 0.001;
00009   float SparseMatrix::log_postProbs_thresh = log(0.001);
00010 
00011   void SparseMatrix::resize(int nrows, int ncols){
00012     M.resize(nrows);
00013     _nrows = nrows;
00014     _ncolumns = ncols;
00015   }
00016 
00017   int SparseMatrix::nrows(){
00018     return _nrows;
00019   }
00020 
00021   int SparseMatrix::ncols(){
00022     return _ncolumns;
00023   }
00024 
00025   void SparseMatrix::buildPredMatrix(int nrows, int ncols, fMatrix &postProbs){
00026     _nrows = nrows+1;
00027     _ncolumns = ncols;
00028     M.resize(_nrows);
00029     for(int i = 0; i < nrows; i++){
00030       for(int j = 0; j < ncols; j++){
00031         if(postProbs(i,j) >= postProbs_thresh)
00032           M[i+1][j] = postProbs(i,j);
00033       }
00034     }
00035     nextr = 0;
00036     nextc = M[0].begin();
00037   }
00038 
00039   void SparseMatrix::applyPrediction(SparseMatrixPtr predMatrix1, SparseMatrixPtr predMatrix2){
00040     if(_nrows != predMatrix1->nrows() || _ncolumns != predMatrix2->nrows()){
00041       cerr << "Tried to apply prediction to alignment matrix with wrong sized matrices!" << endl;
00042       exit(-1);
00043     }
00044 
00045     map<int,float>::iterator it1, it2, it3;
00046     
00047     for(int i = 0; i < _nrows; i++){
00048       for(it1 = lineBegin(i); it1 != lineEnd(i); it1++){
00049         it2 = predMatrix1->lineBegin(i);
00050         it3 = predMatrix2->lineBegin(it1->first);
00051         float sum = 0.0;
00052         while(it2 != predMatrix1->lineEnd(i) && it3 != predMatrix2->lineEnd(it1->first)){
00053           if(it2->first == it3->first){
00054             sum += it2->second * it3->second;
00055             it2++;
00056             it3++;
00057           }
00058           else if(it2->first > it3->first)
00059             it3++;
00060           else
00061             it2++;
00062         }
00063         it1->second *= sum;
00064       }
00065     }
00066   }
00067 
00068   bool SparseMatrix::next(int *i, int *j, float *prob){
00069     while(M[nextr].empty()){
00070       nextr++;
00071       nextc = M[nextr].begin();
00072       if(nextr == _nrows)
00073         return false;
00074     }
00075     *i = nextr;
00076     *j = nextc->first;
00077     *prob = nextc->second;
00078     nextc++;
00079 
00080     if(nextc == M[nextr].end() && nextr == _nrows-1){
00081       nextr = 0;
00082       nextc = M[0].begin();
00083       return false;
00084     }
00085     if(nextc == M[nextr].end() && nextr != _nrows-1){
00086       nextr++;
00087       nextc = M[nextr].begin();
00088     }
00089     return true;
00090   }
00091 
00092   vector< map<int,float> > &SparseMatrix::matrix(){
00093     return M;
00094   }
00095 
00096   map<int,float>::iterator SparseMatrix::lineBegin(int i){
00097     return M[i].begin();
00098   }
00099   map<int,float>::iterator SparseMatrix::lineEnd(int i){
00100     return M[i].end();
00101   }
00102 
00103   void SparseMatrix::getfMatrixTimesX(fMatrix &fM, float x){
00104     fM.resize(_nrows,_ncolumns);
00105     for(int i = 0; i < _nrows; i++){
00106       for(int j = 0; j < _ncolumns; j++){
00107         fM(i,j) = 0;
00108       }
00109     }
00110     for(int i = 0; i < _nrows; i++){
00111       for(map<int,float>::iterator it = M[i].begin(); it != M[i].end(); it++){
00112         fM(i,it->first) = x * it->second;
00113       }
00114     }
00115   }
00116 
00117   void SparseMatrix::getfMatrixPred(fMatrix &fM){
00118     fM.resize(_nrows-1,_ncolumns);
00119     for(int i = 1; i < _nrows; i++){
00120       for(int j = 0; j < _ncolumns; j++){
00121         fM(i-1,j) = 0;
00122       }
00123     }
00124     for(int i = 1; i < _nrows; i++){
00125       for(map<int,float>::iterator it = M[i].begin(); it != M[i].end(); it++){
00126         fM(i-1,it->first) = it->second;
00127       }
00128     }
00129   }
00130 
00131 
00132   void SparseMatrix::removeLastLine(){
00133     M.pop_back();
00134     _nrows--;
00135     nextr = 0;
00136     nextc = M[0].begin();
00137   }
00138 
00139   void SparseMatrix::removeLastColumn(){
00140     for(int i = 0; i < _nrows; i++){
00141       M[i].erase(_ncolumns-1);
00142     }
00143     _ncolumns--;
00144     nextr = 0;
00145     nextc = M[0].begin();
00146   }
00147 
00148   void SparseMatrix::leftXright(SparseMatrixPtr &N, fMatrix &OUT, float n){
00149     if(ncols() != N->nrows()){
00150       cout << "Cannot compute the product. Wrong number of rows or columns! (prod matrices)" << endl;
00151       exit(-1);
00152     }
00153    
00154     map<int,float>::iterator it1;
00155     map<int,float>::iterator it2;
00156     
00157     for(int i = 0; i < nrows(); i++){
00158       for(it1 = M[i].begin(); it1 != M[i].end(); it1++){
00159         for(it2 = N->lineBegin(it1->first); it2 != N->lineEnd(it1->first); it2++){
00160           OUT(i, it2->first) += it1->second * it2->second * n;
00161         }
00162       }
00163     }
00164   }
00165 
00166   void SparseMatrix::leftTransXright(SparseMatrixPtr &N, fMatrix &OUT, float n){
00167     if(nrows() != N->nrows()){
00168       cout << "Cannot compute the product. Wrong number of rows or columns! (prod matrices)" << endl;
00169       exit(-1);
00170     }
00171    
00172     map<int,float>::iterator it1;
00173     map<int,float>::iterator it2;
00174     
00175     for(int i = 0; i < nrows(); i++){
00176       for(it1 = M[i].begin(); it1 != M[i].end(); it1++){
00177         for(it2 = N->lineBegin(i); it2 != N->lineEnd(i); it2++){
00178           OUT(it1->first, it2->first) += it1->second * it2->second * n;
00179         }
00180       }
00181     }
00182   }
00183 
00184   void SparseMatrix::leftXright(SparseMatrixPtr &N, fMatrix &OUT){
00185     if(ncols() != N->nrows()){
00186       cout << "Cannot compute the product. Wrong number of rows or columns! (prod matrices)" << endl;
00187       exit(-1);
00188     }
00189    
00190     map<int,float>::iterator it1;
00191     map<int,float>::iterator it2;
00192     
00193     for(int i = 0; i < nrows(); i++){
00194       for(it1 = M[i].begin(); it1 != M[i].end(); it1++){
00195         for(it2 = N->lineBegin(it1->first); it2 != N->lineEnd(it1->first); it2++){
00196           OUT(i, it2->first) += it1->second * it2->second;
00197         }
00198       }
00199     }
00200   }
00201 
00202   void SparseMatrix::leftTransXright(SparseMatrixPtr &N, fMatrix &OUT){
00203     if(nrows() != N->nrows()){
00204       cout << "Cannot compute the product. Wrong number of rows or columns! (prod matrices)" << endl;
00205       exit(-1);
00206     }
00207    
00208     map<int,float>::iterator it1;
00209     map<int,float>::iterator it2;
00210     
00211     for(int i = 0; i < nrows(); i++){
00212       for(it1 = M[i].begin(); it1 != M[i].end(); it1++){
00213         for(it2 = N->lineBegin(i); it2 != N->lineEnd(i); it2++){
00214           OUT(it1->first, it2->first) += it1->second * it2->second;
00215         }
00216       }
00217     }
00218   }
00219 
00220   void SparseMatrix::transposeOf(SparseMatrixPtr &A){
00221     M.resize(A->ncols());
00222     _ncolumns = A->nrows();
00223     _nrows = A->ncols();
00224     
00225     map<int,float>::iterator it;
00226     for(int i = 0; i < A->nrows(); i++){
00227       for(it = A->lineBegin(i); it != A->lineEnd(i); it++){
00228         M[it->first][i] = it->second;
00229       }
00230     }
00231     nextr = 0;
00232     nextc = M[0].begin();
00233   }
00234 
00235   void SparseMatrix::addGaps(SparseMatrixPtr ppGap1, SparseMatrixPtr ppGap2){
00236     vector<float> aux (_ncolumns, 0.0);
00237     map<int,float>::iterator it;
00238     resize(_nrows+1, _ncolumns+1);
00239     for(int i = 0; i < ppGap1->nrows(); i++){
00240       for(it = ppGap1->lineBegin(i); it != ppGap1->lineEnd(i); it++){
00241           aux[it->first] += it->second;
00242       }
00243     }
00244     for(int i = 0; i < (int)aux.size(); i++)
00245       M[_nrows-1][i] = aux[i];
00246     
00247     for(int i = 0; i < ppGap2->nrows(); i++){
00248       float sum = 0.0;
00249       for(it = ppGap2->lineBegin(i); it != ppGap2->lineEnd(i); it++){
00250           sum += it->second;
00251       }
00252       M[i][_ncolumns-1] = sum;
00253     }
00254     nextr = 0;
00255     nextc = M[0].begin();
00256   }
00257 
00258   void SparseMatrix::printMatrix(){
00259     for(int i = 0; i < _nrows; i++){
00260       map<int,float>::iterator it;
00261       for(it = M[i].begin(); it != M[i].end(); it++)
00262         cout << i << "-" << it->first << "-" << it->second << " ";
00263     }
00264   }
00265 }
00266