SOMのプログラムでデータを扱うためのクラス
テキストファイルのデータをboost::numeric::ublas::matrix
ファイル名と列の終点を引数としたり,引数に始点を含めることも可能.
ついでに,与えられたstd::list
特定のプログラム用に作ったので,改良すればもう少し使える幅が広がるかもしれない.
トークナイザーって言葉はかっこいい響きだと思う.
#include <iostream> #include <fstream> #include <list> #include <boost/numeric/ublas/matrix.hpp> #include <boost/numeric/ublas/matrix_proxy.hpp> #include <boost/tokenizer.hpp> #include <boost/lexical_cast.hpp> using namespace boost::numeric::ublas; typedef boost::char_separator<char> csc; typedef boost::tokenizer<csc> tknzr; class data{ private: public: static matrix<double> generator(void); static matrix<double> generator(std::string, int); static matrix<double> generator(std::string, int, int); static bool write_list(std::string, std::list<double>); static bool write_mat(std::string, matrix<double>); }; matrix<double> data::generator(){ matrix<double> x(100, 100); int x1 = 0, x2 = 1; for(int i = 0; i < 100; ++i){ for(int j = 0; j < 2; ++j){ if(j == 0){ x(i, j) = x1; }else{ x(i, j) = x2++; } } if(((i + 1) % 10) == 0){ x1++; x2 = 1; } } return x; } matrix<double> data::generator(std::string fname, int column){ int x_row = 0, x_col = 0; std::string line; std::ifstream f(fname, std::ios::in); std::list<double> num_token; std::list<double>::iterator nt_it; matrix<double> x; csc sep(", \t "); while(getline(f, line)){ tknzr line_tknzr(line, sep); tknzr::iterator it = line_tknzr.begin(); ++x_row; for(int i = 0;(i < column) && (it != line_tknzr.end()); ++i, ++it){ num_token.push_back(boost::lexical_cast<double>(*it)); } } x_col = num_token.size() / x_row; x.resize(x_row, x_col); nt_it = num_token.begin(); for(int i = 0; i < x_row; ++i){ for(int j = 0; j < x_col; ++j){ x(i, j ) = *nt_it; ++nt_it; } } return x; } matrix<double> data::generator(std::string fname, int begin_column, int end_column){ int x_row = 0, x_col = 0; std::string line; std::ifstream f(fname, std::ios::in); std::list<double> num_token; std::list<double>::iterator nt_it; matrix<double> x; csc sep(", \t"); while(getline(f, line)){ tknzr line_tknzr(line, sep); tknzr::iterator it = line_tknzr.begin(); ++x_row; for(int i = 0;(i < end_column) && (it != line_tknzr.end()); ++i, ++it){ if(i >= (begin_column - 1)){ num_token.push_back(boost::lexical_cast<double>(*it)); } } } x_col = num_token.size() / x_row; x.resize(x_row, x_col); nt_it = num_token.begin(); for(int i = 0; i < x_row; ++i){ for(int j = 0; j < x_col; ++j){ x(i, j ) = *nt_it; ++nt_it; } } return x; } bool data::write_list(std::string fname, std::list<double> lst){ std::ofstream f(fname, std::ios::out); for(std::list<double>::iterator lst_it = lst.begin(); lst_it != lst.end(); ++lst_it){ f << *lst_it << std::endl; } return true; } bool data::write_mat(std::string fname, matrix<double> mat){ std::ofstream f(fname, std::ios::out); for(int i = 0; i < (int)mat.size1(); ++i){ for(int j = 0; j < (int)mat.size2(); ++j){ f << mat(i, j) << " "; } f << std::endl; } return true; }