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

release/4.3a0
Richard Roberts 2012-06-22 22:51:32 +00:00
parent 073369d666
commit 0e46b02374
3 changed files with 50 additions and 22 deletions

View File

@ -19,6 +19,7 @@
#include <gtsam/base/Matrix.h> #include <gtsam/base/Matrix.h>
#include <gtsam/base/timing.h> #include <gtsam/base/timing.h>
#include <gtsam/base/Vector.h> #include <gtsam/base/Vector.h>
#include <gtsam/base/FastList.h>
#include <gtsam/3rdparty/Eigen/Eigen/Dense> #include <gtsam/3rdparty/Eigen/Eigen/Dense>
#include <gtsam/3rdparty/Eigen/Eigen/SVD> #include <gtsam/3rdparty/Eigen/Eigen/SVD>
@ -236,27 +237,33 @@ void save(const Matrix& A, const string &s, const string& filename) {
} }
/* ************************************************************************* */ /* ************************************************************************* */
//istream& operator>>(istream& inputStream, Matrix& destinationMatrix) { istream& operator>>(istream& inputStream, Matrix& destinationMatrix) {
// destinationMatrix.resize(0,0); string line;
// string line; FastList<vector<double> > coeffs;
// bool first = true; bool first = true;
// while(getline(inputStream, line)) { size_t width = 0;
// // Read coefficients from file size_t height = 0;
// vector<double> coeffs; while(getline(inputStream, line)) {
// std::copy(istream_iterator<double>(stringstream(line)), istream_iterator<double>(), coeffs.end()); // Read coefficients from file
// if(first) { coeffs.push_back(vector<double>());
// destinationMatrix.resize(1, if(!first)
// } coeffs.back().reserve(width);
// if(coeffs.size() != dimLatent()) { std::copy(istream_iterator<double>(stringstream(line)), istream_iterator<double>(),
// cout << "Error reading motion file, latent variable dimension does not match file" << endl; back_insert_iterator<vector<double> >(coeffs.back()));
// exit(1); if(first)
// } width = coeffs.back().size();
// if(coeffs.size() != width)
// // Copy coefficients to alignment matrix throw runtime_error("Error reading matrix from input stream, inconsistent numbers of elements in rows");
// alignment_.conservativeResize(alignment_.rows() + 1, dimLatent()); ++ height;
// alignment_.row(alignment_.rows() - 1) = Eigen::Map<Vector>(&coeffs[0], dimLatent()).transpose(); }
// }
//} // Copy coefficients to matrix
destinationMatrix.resize(height, width);
int row = 0;
BOOST_FOREACH(const vector<double>& rowVec, coeffs) {
destinationMatrix.row(row) = Eigen::Map<const Eigen::RowVectorXd>(&rowVec[0], width);
}
}
/* ************************************************************************* */ /* ************************************************************************* */
void insertSub(Matrix& fullMatrix, const Matrix& subMatrix, size_t i, size_t j) { void insertSub(Matrix& fullMatrix, const Matrix& subMatrix, size_t i, size_t j) {

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 * tab-, space-, or comma-separated, similar to the format read by the MATLAB
* dlmread command. * dlmread command.
*/ */
//istream& operator>>(istream& inputStream, Matrix& destinationMatrix); istream& operator>>(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

View File

@ -17,6 +17,7 @@
**/ **/
#include <iostream> #include <iostream>
#include <sstream>
#include <CppUnitLite/TestHarness.h> #include <CppUnitLite/TestHarness.h>
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
@ -261,6 +262,26 @@ TEST( matrix, insert_sub )
EXPECT(assert_equal(expected, big)); EXPECT(assert_equal(expected, big));
} }
/* ************************************************************************* */
TEST( matrix, stream_read ) {
Matrix expected = Matrix_(3,4,
1.1, 2.3, 4.2, 7.6,
-0.3, -8e-2, 5.1, 9.0,
1.2, 3.4, 4.5, 6.7);
string matrixAsString =
"1.1 2.3 4.2 7.6\n"
"-0.3 -8e-2 5.1 9.0\n\r" // Test extra spaces and windows newlines
"1.2 \t 3.4 4.5 6.7"; // Test tab as separator
stringstream asStream(matrixAsString, ios::in);
Matrix actual;
asStream >> actual;
EXPECT(assert_equal(expected, actual));
}
/* ************************************************************************* */ /* ************************************************************************* */
TEST( matrix, scale_columns ) TEST( matrix, scale_columns )
{ {