MTK++ Latest version: 0.2.0

table_boost.h
Go to the documentation of this file.
00001 
00033 #ifndef TABLE_H
00034 #define TABLE_H
00035 
00036 #include <iostream>
00037 #include <string>
00038 #include <vector>
00039 #include <fstream>
00040 #include <iomanip>
00041 #include <stdlib.h>
00042 #include <sstream>
00043 #include <math.h>
00044 
00045 #include "Diagnostics/MTKException.h"
00046 
00047 // - BOOST - //
00048 #include <boost/numeric/ublas/symmetric.hpp>
00049 #include <boost/numeric/ublas/matrix.hpp>
00050 #include <boost/numeric/ublas/io.hpp>
00051 #include <boost/numeric/ublas/banded.hpp> // for diagonal matrix
00052 #include <boost/numeric/bindings/blas/blas3.hpp>
00053 #include "boost/numeric/bindings/traits/ublas_matrix.hpp"
00054 #include "boost/numeric/bindings/traits/ublas_vector.hpp"
00055 
00056 namespace ublas  = boost::numeric::ublas;
00057 namespace blas   = boost::numeric::bindings::blas;
00058 
00059 namespace MTKpp
00060 {
00061 // ============================================================
00062 // Class : table()
00063 // ------------------------------------------------------------
00070 // ============================================================
00071 template <class T> class table
00072 {
00073 public:
00077     table() {
00078       this->itsName = "";
00079       this->nColumns = 0;
00080       this->nRows = 0;
00081     }
00082 
00084     virtual ~table() {}
00085 
00090     void setName(std::string n) {
00091       this->itsName = n;
00092     }
00093 
00098     std::string getName() {
00099       return this->itsName;
00100     }
00101 
00106     void setType(std::string n) {
00107       this->itsType = n;
00108     }
00109 
00114     std::string getType() {
00115       return this->itsType;
00116     }
00117 
00122     void setNumColumns(const int& i) {
00123       this->nColumns = i;
00124     }
00125 
00130     int getNumColumns() {
00131       return this->nColumns;
00132     }
00133 
00139     void setColumnLabel(const int& i, std::string s) {
00140       if (i > this->nColumns-1) {
00141         std::stringstream ss;
00142         ss << " Out of bounds error: i = " << i+1
00143                   << " > " << this->nColumns << std::endl;
00144         std::cout << ss.str();
00145         throw MTKException(ss.str());
00146       }
00147       this->columnLabels[i] = s;
00148     }
00149 
00154     void setColumnLabels(std::vector<std::string> v) {
00155       if (v.size() >= this->columnLabels.size()) {
00156         for (unsigned int i = 0; i < columnLabels.size(); i++) {
00157           this->columnLabels[i] = v[i];
00158         }
00159       }
00160     }
00161 
00167     std::string getColumnLabel(const int& i) {
00168       if (i < static_cast<int>(this->columnLabels.size())) {
00169         return this->columnLabels[i];
00170       }
00171       return "";
00172     }
00173 
00178     std::vector<std::string> getColumnLabels() {
00179       return this->columnLabels;
00180     }
00181 
00186     void setRowLabels(std::vector<std::string> v) {
00187       if (v.size() == this->rowLabels.size()) {
00188         for (unsigned int i = 0; i < v.size(); i++) {
00189           this->rowLabels[i] = v[i];
00190         }
00191       }
00192     }
00193 
00199     std::string getRowLabel(const int& i) {
00200       if (i < static_cast<int>(this->rowLabels.size())) {
00201         return this->rowLabels[i];
00202       }
00203       return "";
00204     }
00205 
00210     std::vector<std::string> getRowLabels() {
00211       return this->rowLabels;
00212     }
00213 
00218     void setNumRows(const int& i) {
00219       this->nRows = i;
00220     }
00221 
00226     int getNumRows() {
00227       return this->nRows;
00228     }
00229 
00235     void setRowLabel(const int& i, std::string s) {
00236       if (i > this->nRows-1) {
00237         std::stringstream ss;
00238         ss << " Out of bounds error: i = " << i+1
00239                   << " > " << this->nRows << std::endl;
00240         std::cout << ss.str();
00241         throw MTKException(ss.str());
00242       }
00243       this->rowLabels[i] = s;
00244     }
00245 
00251     void setSizes(const int& i, const int& j) {
00252       this->nRows = i;
00253       this->nColumns = j;
00254       this->setSize(i, j);
00255     }
00256 
00260     void setup() {
00261       if (this->nColumns > 0 and this->nRows > 0) {
00262         this->setSize(this->nRows, this->nColumns);
00263       }
00264       else {
00265         std::cout << " ERROR " << std::endl;
00266         throw MTKException(" table::setup ERROR ");
00267       }
00268     }
00269 
00276     void setCellValue(const int& i, const int& j, T v) {
00277       if ((i > this->nRows-1) or (j > this->nColumns-1)) {
00278         std::stringstream ss;
00279         ss << " Out of bounds error: i = " << i << " j = " << j << " value = " << v
00280                   << " " << this->nRows << " " << this->nColumns << std::endl;
00281         std::cout << ss.str();
00282         throw MTKException(ss.str());
00283       }
00284       else {
00285         this->itsMatrix(i,j) = v;
00286       }
00287     }
00288 
00295     T getCellValue(const int& i, const int& j) {
00296       if ((i > this->nRows-1) or (j > this->nColumns-1)) {
00297         std::stringstream ss;
00298         ss << " Out of bounds error: i = " << i << " j = " << j
00299                   << " " << this->nRows << " " << this->nColumns << std::endl;
00300         std::cout << ss.str();
00301         throw MTKException(ss.str());
00302       }
00303       else {
00304         return this->itsMatrix(i,j);
00305       }
00306       return 0;
00307     }
00308 
00312     void print() {
00313       std::cout << " \n " << this->getName() << std::endl;
00314       std::cout << "- ";
00315       for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00316         std::cout << this->columnLabels[j] << " ";
00317       }
00318       std::cout << " " << std::endl;
00319 
00320       for (unsigned i = 0; i < this->itsMatrix.size1(); ++ i) {
00321         std::cout << this->rowLabels[i] << " ";
00322         for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00323           std::cout << std::showpoint << this->itsMatrix(i,j) << " ";
00324         }
00325         std::cout << " " << std::endl;
00326       }
00327     }
00328 
00332     void printRow(const int& r) {
00333       for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00334         std::cout << this->columnLabels[j] << " ";
00335       }
00336       std::cout << " " << std::endl;
00337       if (r > static_cast<int>(this->itsMatrix.size1())) {
00338         std::cout << " Error in table " << std::endl;
00339       }
00340       std::cout << this->rowLabels[r] << " ";
00341       for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00342         std::cout << std::showpoint << this->itsMatrix(r,j) << " ";
00343       }
00344       std::cout << " " << std::endl;
00345     }
00346 
00350     void printMatrix() {
00351       for (unsigned i = 0; i < this->itsMatrix.size1 (); ++ i) {
00352         for (unsigned j = 0; j < this->itsMatrix.size2 (); ++ j) {
00353           std::cout << std::showpoint << this->itsMatrix(i,j) << " ";
00354         }
00355         std::cout << " " << std::endl;
00356       }
00357     }
00358 
00362     ublas::matrix<T>& getMatrix() {
00363       return this->itsMatrix;
00364     }
00365 
00371     void sortByColumn(int j, int order) {
00372 
00373       ublas::matrix<T> bkupMatrix;
00374       bkupMatrix.resize(this->nRows, this->nColumns);
00375       for (unsigned i = 0; i < this->itsMatrix.size1(); ++i) {
00376         for (unsigned k = 0; k < this->itsMatrix.size2(); ++k) {
00377           bkupMatrix(i,k) = this->itsMatrix(i,k);
00378         }
00379       }
00380 
00381       int size = nRows;
00382       //int size2 = nColumns;
00383       ublas::vector<T> column;
00384       ublas::vector<int> columnIndices;
00385       column.resize(size);
00386       columnIndices.resize(size);
00387 
00388       int k = 0;
00389       double p = 0.0;
00390       std::string label = "";
00391 
00392       for (int i = 0; i < size; i++) {
00393         column(i) = itsMatrix(i,j);
00394         columnIndices(i) = i;
00395       }
00396 
00397       for (int i = 0; i < size; ++i) {
00398         k = i;
00399         p = column(i);
00400         label = rowLabels[i];
00401 
00402         for (int j = i+1; j < size; ++j) {
00403           if (!order) { // Ascending
00404             if (column(j) < p) {
00405               k = j;
00406               p = column(j);
00407               label = rowLabels[j];
00408             }
00409           }
00410           else { // Descending
00411             if (column(j) > p) {
00412               k = j;
00413               p = column(j);
00414               label = rowLabels[j];
00415             }
00416           }
00417         }
00418         if ( k != i ) {
00419           column(k) = column(i);
00420           column(i) = p;
00421           int temK = columnIndices(k);
00422           columnIndices(k) = columnIndices(i);
00423           columnIndices(i) = temK;
00424           rowLabels[k] = rowLabels[i];
00425           rowLabels[i] = label;
00426 /*
00427           for (int m = 0; m < size2; ++m) {
00428             p = itsMatrix(i,m);
00429             itsMatrix(i,m) = itsMatrix(k,m);
00430             itsMatrix(k,m) = p;
00431           }
00432 */
00433         }
00434       }
00435       for (unsigned i = 0; i < this->itsMatrix.size1(); ++i) {
00436         for (unsigned k = 0; k < this->itsMatrix.size2(); ++k) {
00437           this->itsMatrix(i,k) = bkupMatrix(columnIndices(i), k);
00438         }
00439       }
00440     }
00441 
00445     void initialize(double t) {
00446       for (unsigned i = 0; i < this->itsMatrix.size1(); ++ i) {
00447         for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00448           this->itsMatrix(i,j) = t;
00449         }
00450       }
00451     }
00452 
00456     void initialize(int t) {
00457       for (unsigned i = 0; i < this->itsMatrix.size1(); ++ i) {
00458         for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00459           this->itsMatrix(i,j) = t;
00460         }
00461       }
00462     }
00463 
00467     void initialize(std::string t) {
00468       for (unsigned i = 0; i < this->itsMatrix.size1(); ++ i) {
00469         for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00470           this->itsMatrix(i,j) = t;
00471         }
00472       }
00473     };
00474 
00475 protected: // Functions
00479     void setSize(const int& i, const int& j) {
00480       try {
00481         this->itsMatrix.resize(i,j);
00482         this->rowLabels.resize(i);
00483         this->columnLabels.resize(j);
00484         this->initializeAll();
00485       }
00486       catch (boost::numeric::ublas::bad_size) {
00487         std::cout << " ERROR IN TABLE ... EXITING " << std::endl;
00488         throw MTKException(" ERROR IN TABLE ... EXITING ");
00489       }
00490     }
00491 
00492     void initializeAll() {
00493       for (unsigned i = 0; i < this->itsMatrix.size1(); ++ i) {
00494         this->rowLabels[i] = "";
00495       }
00496       for (unsigned j = 0; j < this->itsMatrix.size2(); ++ j) {
00497         this->columnLabels[j] = "";
00498       }
00499     }
00500 
00501 protected: // Data
00503     std::string itsName;
00504 
00506     std::string itsType;
00507 
00509     int nColumns;
00510 
00512     std::vector<std::string> columnLabels;
00513 
00515     int nRows;
00516 
00518     std::vector<std::string> rowLabels;
00519 
00521     ublas::matrix<T> itsMatrix;
00522 };
00523 
00524 }
00525 
00526 #endif // TABLE_H
00527 

Generated on Fri Dec 23 2011 09:28:52 for MTK++ by Doxygen 1.7.5.1