MTK++ Latest version: 0.2.0

table.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 #include <Eigen/Dense>
00048 using namespace Eigen;
00049 
00050 namespace MTKpp
00051 {
00052 // ============================================================
00053 // Class : table()
00054 // ------------------------------------------------------------
00061 // ============================================================
00062 template <class T> class table
00063 {
00064 public:
00068     table() {
00069       this->itsName = "";
00070       this->nColumns = 0;
00071       this->nRows = 0;
00072     }
00073 
00075     virtual ~table() {}
00076 
00081     void setName(std::string n) {
00082       this->itsName = n;
00083     }
00084 
00089     std::string getName() {
00090       return this->itsName;
00091     }
00092 
00097     void setType(std::string n) {
00098       this->itsType = n;
00099     }
00100 
00105     std::string getType() {
00106       return this->itsType;
00107     }
00108 
00113     void setNumColumns(const int& i) {
00114       this->nColumns = i;
00115     }
00116 
00121     int getNumColumns() {
00122       return this->nColumns;
00123     }
00124 
00130     void setColumnLabel(const int& i, std::string s) {
00131       if (i > this->nColumns-1) {
00132         std::stringstream ss;
00133         ss << " Out of bounds error: i = " << i+1
00134                   << " > " << this->nColumns << std::endl;
00135         std::cout << ss.str();
00136         throw MTKException(ss.str());
00137       }
00138       this->columnLabels[i] = s;
00139     }
00140 
00145     void setColumnLabels(std::vector<std::string> v) {
00146       if (v.size() >= this->columnLabels.size()) {
00147         for (unsigned int i = 0; i < columnLabels.size(); i++) {
00148           this->columnLabels[i] = v[i];
00149         }
00150       }
00151     }
00152 
00158     std::string getColumnLabel(const int& i) {
00159       if (i < static_cast<int>(this->columnLabels.size())) {
00160         return this->columnLabels[i];
00161       }
00162       return "";
00163     }
00164 
00169     std::vector<std::string> getColumnLabels() {
00170       return this->columnLabels;
00171     }
00172 
00177     void setRowLabels(std::vector<std::string> v) {
00178       if (v.size() == this->rowLabels.size()) {
00179         for (unsigned int i = 0; i < v.size(); i++) {
00180           this->rowLabels[i] = v[i];
00181         }
00182       }
00183     }
00184 
00190     std::string getRowLabel(const int& i) {
00191       if (i < static_cast<int>(this->rowLabels.size())) {
00192         return this->rowLabels[i];
00193       }
00194       return "";
00195     }
00196 
00201     std::vector<std::string> getRowLabels() {
00202       return this->rowLabels;
00203     }
00204 
00209     void setNumRows(const int& i) {
00210       this->nRows = i;
00211     }
00212 
00217     int getNumRows() {
00218       return this->nRows;
00219     }
00220 
00226     void setRowLabel(const int& i, std::string s) {
00227       if (i > this->nRows-1) {
00228         std::stringstream ss;
00229         ss << " Out of bounds error: i = " << i+1
00230                   << " > " << this->nRows << std::endl;
00231         std::cout << ss.str();
00232         throw MTKException(ss.str());
00233       }
00234       this->rowLabels[i] = s;
00235     }
00236 
00242     void setSizes(const int& i, const int& j) {
00243       this->nRows = i;
00244       this->nColumns = j;
00245       this->setSize(i, j);
00246     }
00247 
00251     void setup() {
00252       if (this->nColumns > 0 and this->nRows > 0) {
00253         this->setSize(this->nRows, this->nColumns);
00254       }
00255       else {
00256         std::cout << " ERROR " << std::endl;
00257         throw MTKException(" table::setup ERROR ");
00258       }
00259     }
00260 
00267     void setCellValue(const int& i, const int& j, T v) {
00268       if ((i > this->nRows-1) or (j > this->nColumns-1)) {
00269         std::stringstream ss;
00270         ss << " Out of bounds error: i = " << i << " j = " << j << " value = " << v
00271                   << " " << this->nRows << " " << this->nColumns << std::endl;
00272         std::cout << ss.str();
00273         throw MTKException(ss.str());
00274       }
00275       else {
00276         this->itsMatrix(i,j) = v;
00277       }
00278     }
00279 
00286     T getCellValue(const int& i, const int& j) {
00287       if ((i > this->nRows-1) or (j > this->nColumns-1)) {
00288         std::stringstream ss;
00289         ss << " Out of bounds error: i = " << i << " j = " << j
00290                   << " " << this->nRows << " " << this->nColumns << std::endl;
00291         std::cout << ss.str();
00292         throw MTKException(ss.str());
00293       }
00294       else {
00295         return this->itsMatrix(i,j);
00296       }
00297       return 0;
00298     }
00299 
00303     void print() {
00304       std::cout << " \n " << this->getName() << std::endl;
00305       std::cout << "- ";
00306       for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00307         std::cout << this->columnLabels[j] << " ";
00308       }
00309       std::cout << " " << std::endl;
00310 
00311       for (unsigned i = 0; i < this->itsMatrix.rows(); ++ i) {
00312         std::cout << this->rowLabels[i] << " ";
00313         for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00314           std::cout << std::showpoint << this->itsMatrix(i,j) << " ";
00315         }
00316         std::cout << " " << std::endl;
00317       }
00318     }
00319 
00323     void printRow(const int& r) {
00324       for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00325         std::cout << this->columnLabels[j] << " ";
00326       }
00327       std::cout << " " << std::endl;
00328       if (r > static_cast<int>(this->itsMatrix.rows())) {
00329         std::cout << " Error in table " << std::endl;
00330       }
00331       std::cout << this->rowLabels[r] << " ";
00332       for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00333         std::cout << std::showpoint << this->itsMatrix(r,j) << " ";
00334       }
00335       std::cout << " " << std::endl;
00336     }
00337 
00341     void printMatrix() {
00342       for (unsigned i = 0; i < this->itsMatrix.rows(); ++ i) {
00343         for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00344           std::cout << std::showpoint << this->itsMatrix(i,j) << " ";
00345         }
00346         std::cout << " " << std::endl;
00347       }
00348     }
00349 
00353     Eigen::Matrix<T, Dynamic, Dynamic>& getMatrix() {
00354       return this->itsMatrix;
00355     }
00356 
00362     void sortByColumn(int j, int order) {
00363 
00364       Eigen::Matrix<T, Dynamic, Dynamic> bkupMatrix;
00365       bkupMatrix.resize(this->nRows, this->nColumns);
00366 
00367       for (unsigned i = 0; i < this->itsMatrix.rows(); ++i) {
00368         for (unsigned k = 0; k < this->itsMatrix.cols(); ++k) {
00369           bkupMatrix(i,k) = this->itsMatrix(i,k);
00370         }
00371       }
00372 
00373       int size = nRows;
00374       //int size2 = nColumns;
00375 
00376 //      Eigen::Vector<T> column;
00377 //      Eigen::Vector<int> columnIndices;
00378 
00379       Eigen::Matrix<T, Dynamic, 1> column;
00380       Eigen::Matrix<int, Dynamic, 1> columnIndices;
00381 
00382       column.resize(size,1);
00383       columnIndices.resize(size, 1);
00384 
00385       int k = 0;
00386       double p = 0.0;
00387       std::string label = "";
00388 
00389       for (int i = 0; i < size; i++) {
00390         column(i, 0) = itsMatrix(i,j);
00391         columnIndices(i, 0) = i;
00392       }
00393 
00394       for (int i = 0; i < size; ++i) {
00395         k = i;
00396         p = column(i, 0);
00397         label = rowLabels[i];
00398 
00399         for (int j = i+1; j < size; ++j) {
00400           if (!order) { // Ascending
00401             if (column(j, 0) < p) {
00402               k = j;
00403               p = column(j, 0);
00404               label = rowLabels[j];
00405             }
00406           }
00407           else { // Descending
00408             if (column(j,0) > p) {
00409               k = j;
00410               p = column(j,0);
00411               label = rowLabels[j];
00412             }
00413           }
00414         }
00415         if ( k != i ) {
00416           column(k,0) = column(i,0);
00417           column(i,0) = p;
00418           int temK = columnIndices(k,0);
00419           columnIndices(k,0) = columnIndices(i,0);
00420           columnIndices(i,0) = temK;
00421           rowLabels[k] = rowLabels[i];
00422           rowLabels[i] = label;
00423 /*
00424           for (int m = 0; m < size2; ++m) {
00425             p = itsMatrix(i,m);
00426             itsMatrix(i,m) = itsMatrix(k,m);
00427             itsMatrix(k,m) = p;
00428           }
00429 */
00430         }
00431       }
00432       for (unsigned i = 0; i < this->itsMatrix.rows(); ++i) {
00433         for (unsigned k = 0; k < this->itsMatrix.cols(); ++k) {
00434           this->itsMatrix(i,k) = bkupMatrix(columnIndices(i,0), k);
00435         }
00436       }
00437     }
00438 
00442     void initialize(double t) {
00443       for (unsigned i = 0; i < this->itsMatrix.rows(); ++ i) {
00444         for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00445           this->itsMatrix(i,j) = t;
00446         }
00447       }
00448     }
00449 
00453     void initialize(int t) {
00454       for (unsigned i = 0; i < this->itsMatrix.rows(); ++ i) {
00455         for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00456           this->itsMatrix(i,j) = t;
00457         }
00458       }
00459     }
00460 
00464     void initialize(std::string t) {
00465       for (unsigned i = 0; i < this->itsMatrix.rows(); ++ i) {
00466         for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00467           this->itsMatrix(i,j) = t;
00468         }
00469       }
00470     };
00471 
00472 protected: // Functions
00476     void setSize(const int& i, const int& j) {
00477 //      try {
00478         this->itsMatrix.resize(i,j);
00479         this->rowLabels.resize(i);
00480         this->columnLabels.resize(j);
00481         this->initializeAll();
00482 /*
00483       }
00484       catch (boost::numeric::ublas::bad_size) {
00485         std::cout << " ERROR IN TABLE ... EXITING " << std::endl;
00486         throw MTKException(" ERROR IN TABLE ... EXITING ");
00487       }
00488 */
00489     }
00490 
00491     void initializeAll() {
00492       for (unsigned i = 0; i < this->itsMatrix.rows(); ++ i) {
00493         this->rowLabels[i] = "";
00494       }
00495       for (unsigned j = 0; j < this->itsMatrix.cols(); ++ j) {
00496         this->columnLabels[j] = "";
00497       }
00498     }
00499 
00500 protected: // Data
00502     std::string itsName;
00503 
00505     std::string itsType;
00506 
00508     int nColumns;
00509 
00511     std::vector<std::string> columnLabels;
00512 
00514     int nRows;
00515 
00517     std::vector<std::string> rowLabels;
00518 
00520     Eigen::Matrix<T, Dynamic, Dynamic> itsMatrix;
00521 };
00522 
00523 }
00524 
00525 #endif // TABLE_H

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