00001 #ifndef JMATH_UBLAS_DATA_FILE_HPP
00002 #define JMATH_UBLAS_DATA_FILE_HPP
00003
00004 #include "boost/numeric/ublas/vector.hpp"
00005 #include "boost/numeric/ublas/matrix.hpp"
00006
00007 #include "kernel/csvFile.hpp"
00008 #include "jmath/jmathException.hpp"
00009
00010 namespace jafar {
00011
00012 namespace jmath {
00018 template<typename T>
00019 class matrix_file : public jafar::kernel::CSVFileSaveLoad {
00020 public:
00021 boost::numeric::ublas::matrix<T> data;
00023 matrix_file() : jafar::kernel::CSVFileSaveLoad(), data(0,0) {}
00025 matrix_file(const boost::numeric::ublas::matrix<T> &_data) :
00026 jafar::kernel::CSVFileSaveLoad(), data(_data) {}
00027 protected:
00028 void loadCSVFile(jafar::kernel::CSVFile& csvFile) {
00029 data.resize(csvFile.nbOfLines(), csvFile.nbOfColumns());
00030 JFR_PRED_ERROR((data.size1() == csvFile.nbOfLines()) &&
00031 (data.size2() == csvFile.nbOfColumns()),
00032 JmathException,
00033 JmathException::RESIZE_FAIL,
00034 "resizing data to "<<csvFile.nbOfLines()
00035 <<"x"<<csvFile.nbOfColumns()
00036 <<" failed, abort")
00037 for(unsigned int row = 0; row < csvFile.nbOfLines(); row++)
00038 for(unsigned int col = 0; col < csvFile.nbOfColumns(); col++)
00039 csvFile.getItem(row, col, data(row,col));
00040 }
00041 void saveCSVFile(jafar::kernel::CSVFile& csvFile) {
00042 size_t rows = data.size1();
00043 size_t cols = data.size2();
00044 for(unsigned int row = 0; row < rows; row++)
00045 for(unsigned int col = 0; col < cols; col++)
00046 csvFile.setItem(row, col, data(row,col));
00047 }
00048 };
00049
00055 template<typename T>
00056 class vector_file : public jafar::kernel::CSVFileSaveLoad {
00057 public:
00058 boost::numeric::ublas::vector<T> data;
00059 typedef enum {VERTICAL, HORIZONTAL} storage;
00060 storage storage_type;
00062 vector_file(storage type = VERTICAL) :
00063 jafar::kernel::CSVFileSaveLoad(), data(0), storage_type(type) {}
00065 vector_file(const boost::numeric::ublas::vector<T>& _data, storage type = VERTICAL) :
00066 jafar::kernel::CSVFileSaveLoad(), data(_data), storage_type(type) {}
00067 protected:
00068 void loadCSVFile(jafar::kernel::CSVFile& csvFile) {
00069 JFR_PRED_ERROR((csvFile.nbOfLines() == 1) || (csvFile.nbOfColumns() == 1),
00070 JmathException,
00071 JmathException::WRONG_SIZE,
00072 "data should be on single column or line")
00073 size_t length = std::max(csvFile.nbOfLines(), csvFile.nbOfColumns());
00074 data.resize(length);
00075 JFR_PRED_ERROR(data.size() == length,
00076 JmathException,
00077 JmathException::RESIZE_FAIL,
00078 "resizing data to "<<length<<" failed, abort")
00079 if(length == csvFile.nbOfLines())
00080 for(unsigned int row = 0; row < length; row++)
00081 csvFile.getItem(row, 0, data[row]);
00082 else
00083 for(unsigned int col = 0; col < length; col++)
00084 csvFile.getItem(0, col, data[col]);
00085 }
00086 void saveCSVFile(jafar::kernel::CSVFile& csvFile) {
00087 size_t length = data.size();
00088 switch(storage_type){
00089 case VERTICAL : {
00090 for(unsigned int row = 0; row < length; row++)
00091 csvFile.setItem(row, 0, data[row]);
00092 }; break;
00093 case HORIZONTAL : {
00094 for(unsigned int col = 0; col < length; col++)
00095 csvFile.setItem(0, col, data[col]);
00096 }; break;
00097 default : {
00098 JFR_ERROR(JmathException,
00099 JmathException::WRONG_TYPE,
00100 "can not handle this kind of storage direction");
00101 }
00102 }
00103 }
00104 };
00105 }
00106 }
00107
00108 #endif