Added >> stream operator to Matrix to easily read from input streams
parent
5fa1b4b00e
commit
b7a13c7061
|
@ -248,11 +248,12 @@ istream& operator>>(istream& inputStream, Matrix& destinationMatrix) {
|
||||||
coeffs.push_back(vector<double>());
|
coeffs.push_back(vector<double>());
|
||||||
if(!first)
|
if(!first)
|
||||||
coeffs.back().reserve(width);
|
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()));
|
back_insert_iterator<vector<double> >(coeffs.back()));
|
||||||
if(first)
|
if(first)
|
||||||
width = coeffs.back().size();
|
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");
|
throw runtime_error("Error reading matrix from input stream, inconsistent numbers of elements in rows");
|
||||||
++ height;
|
++ height;
|
||||||
}
|
}
|
||||||
|
@ -262,7 +263,10 @@ istream& operator>>(istream& inputStream, Matrix& destinationMatrix) {
|
||||||
int row = 0;
|
int row = 0;
|
||||||
BOOST_FOREACH(const vector<double>& rowVec, coeffs) {
|
BOOST_FOREACH(const vector<double>& rowVec, coeffs) {
|
||||||
destinationMatrix.row(row) = Eigen::Map<const Eigen::RowVectorXd>(&rowVec[0], width);
|
destinationMatrix.row(row) = Eigen::Map<const Eigen::RowVectorXd>(&rowVec[0], width);
|
||||||
|
++ row;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return inputStream;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************* */
|
/* ************************************************************************* */
|
||||||
|
|
|
@ -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
|
* tab-, space-, or comma-separated, similar to the format read by the MATLAB
|
||||||
* dlmread command.
|
* 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
|
* extract submatrix, slice semantics, i.e. range = [i1,i2[ excluding i2
|
||||||
|
|
Loading…
Reference in New Issue