Added >> stream operator to Matrix to easily read from input streams

release/4.3a0
Richard Roberts 2012-06-22 22:51:35 +00:00
parent 5fa1b4b00e
commit b7a13c7061
2 changed files with 7 additions and 3 deletions

View File

@ -248,11 +248,12 @@ istream& operator>>(istream& inputStream, Matrix& destinationMatrix) {
coeffs.push_back(vector<double>());
if(!first)
coeffs.back().reserve(width);
std::copy(istream_iterator<double>(stringstream(line)), istream_iterator<double>(),
stringstream lineStream(line);
std::copy(istream_iterator<double>(lineStream), istream_iterator<double>(),
back_insert_iterator<vector<double> >(coeffs.back()));
if(first)
width = coeffs.back().size();
if(coeffs.size() != width)
if(coeffs.back().size() != width)
throw runtime_error("Error reading matrix from input stream, inconsistent numbers of elements in rows");
++ height;
}
@ -262,7 +263,10 @@ istream& operator>>(istream& inputStream, Matrix& destinationMatrix) {
int row = 0;
BOOST_FOREACH(const vector<double>& rowVec, coeffs) {
destinationMatrix.row(row) = Eigen::Map<const Eigen::RowVectorXd>(&rowVec[0], width);
++ row;
}
return inputStream;
}
/* ************************************************************************* */

View File

@ -188,7 +188,7 @@ void save(const Matrix& A, const std::string &s, const std::string& filename);
* tab-, space-, or comma-separated, similar to the format read by the MATLAB
* dlmread command.
*/
istream& operator>>(istream& inputStream, Matrix& destinationMatrix);
std::istream& operator>>(std::istream& inputStream, Matrix& destinationMatrix);
/**
* extract submatrix, slice semantics, i.e. range = [i1,i2[ excluding i2