Line 77: |
Line 77: |
| #include <boost/lexical_cast.hpp> | | #include <boost/lexical_cast.hpp> |
| #include <boost/foreach.hpp> | | #include <boost/foreach.hpp> |
| + | |
| + | #include <Eigen/Core> |
| | | |
| // a generic class for Matrix | | // a generic class for Matrix |
Line 90: |
Line 92: |
| // default constructor | | // default constructor |
| Matrix615() : nr(0), nc(0), hasMissing(false) {} | | Matrix615() : nr(0), nc(0), hasMissing(false) {} |
| + | Matrix615(const char* filename) : nr(0), nc(0), hasMissing(false) { |
| + | readFromFile(filename); |
| + | } |
| + | Matrix615(const T value, const char* str) : nr(0), nc(0) { |
| + | enableMissingValue(value, str); |
| + | } |
| | | |
| // Allow missing value as a pair of actual value and string value | | // Allow missing value as a pair of actual value and string value |
− | void enableMissingValue(const T value, const char* string) { | + | void enableMissingValue(const T value, const char* str) { |
| hasMissing = true; | | hasMissing = true; |
| valueMissing = value; | | valueMissing = value; |
− | strMissing = string; | + | strMissing = str; |
| } | | } |
| | | |
Line 108: |
Line 116: |
| void fill(T defaultValue) { | | void fill(T defaultValue) { |
| std::fill( data.begin(), data.end(), defaultValue ); | | std::fill( data.begin(), data.end(), defaultValue ); |
| + | } |
| + | |
| + | void copyTo(Eigen::Matrix<T,Eigen::Dynamic,Eigen::Dynamic>& m) { |
| + | m.resize(nr,nc); |
| + | for(int j=0; j < nc; ++j) { |
| + | for(int i=0; i < nr; ++i) { |
| + | m(i,j) = data[i*nc+j]; |
| + | } |
| + | } |
| } | | } |
| | | |
Line 120: |
Line 137: |
| for(int i=0; i < nr; ++i) { | | for(int i=0; i < nr; ++i) { |
| for(int j=0; j < nc; ++j) { | | for(int j=0; j < nc; ++j) { |
− | if ( j > 0 ) o << "\t";
| + | if ( j > 0 ) o << "\t"; |
− | if ( hasMissing && ( valueMissing == at(i,j) ) )
| + | if ( hasMissing && ( valueMissing == at(i,j) ) ) |
− | o << strMissing;
| + | o << strMissing; |
− | else
| + | else |
− | o << at(i,j);
| + | o << at(i,j); |
| } | | } |
| o << std::endl; | | o << std::endl; |
Line 141: |
Line 158: |
| boost::char_separator<char> sep(" \t"); | | boost::char_separator<char> sep(" \t"); |
| typedef boost::tokenizer< boost::char_separator<char> > wsTokenizer; | | typedef boost::tokenizer< boost::char_separator<char> > wsTokenizer; |
− | | + | |
| + | data.clear(); |
| nr = nc = 0; | | nr = nc = 0; |
| while( std::getline(ifs, line) ) { | | while( std::getline(ifs, line) ) { |
| wsTokenizer t(line,sep); | | wsTokenizer t(line,sep); |
| for(wsTokenizer::iterator i=t.begin(); i != t.end(); ++i) { | | for(wsTokenizer::iterator i=t.begin(); i != t.end(); ++i) { |
− | // if hasMissing is set, convert string "Missing" into special value for Missing
| + | // if hasMissing is set, convert string "Missing" into special value for Missing |
− | if ( hasMissing && ( i->compare(strMissing) == 0 ) )
| + | if ( hasMissing && ( i->compare(strMissing) == 0 ) ) |
− | data.push_back(valueMissing);
| + | data.push_back(valueMissing); |
− | // Otherwise, convert the string to a particular type
| + | // Otherwise, convert the string to a particular type |
− | else
| + | else |
− | data.push_back(boost::lexical_cast<T>(i->c_str()));
| + | data.push_back(boost::lexical_cast<T>(i->c_str())); |
− | if ( nr == 0 ) ++nc; // count # of columns at the first row
| + | if ( nr == 0 ) ++nc; // count # of columns at the first row |
| } | | } |
| ++nr; | | ++nr; |
| // when reading each line, make sure that the # of columns match to expectation; | | // when reading each line, make sure that the # of columns match to expectation; |
| if ( (int)data.size() != nr*nc ) { | | if ( (int)data.size() != nr*nc ) { |
− | std::cerr << "The input file is not rectangle at line " << nr << std::endl;
| + | std::cerr << "The input file is not rectangle at line " << nr << std::endl; |
− | abort();
| + | abort(); |
| } | | } |
| } | | } |
| } | | } |
| }; | | }; |
| + | |
| #endif | | #endif |
| | | |