diff --git a/cpp/LinearFactor.cpp b/cpp/LinearFactor.cpp index 7f73e6993..6237ac022 100644 --- a/cpp/LinearFactor.cpp +++ b/cpp/LinearFactor.cpp @@ -183,6 +183,27 @@ pair LinearFactor::matrix(const Ordering& ordering) const { return make_pair(A, t*b_); } +/* ************************************************************************* */ +Matrix LinearFactor::matrix_augmented(const Ordering& ordering) const { + // get pointers to the matrices + vector matrices; + BOOST_FOREACH(string j, ordering) { + const Matrix& Aj = get_A(j); + matrices.push_back(&Aj); + } + + // load b into a matrix + Matrix B_mat(numberOfRows(), 1); + for (int i=0; i matrix(const Ordering& ordering) const; + /** + * Return (dense) matrix associated with factor + * The returned system is an augmented matrix: [A b] + * @param ordering of variables needed for matrix column order + */ + Matrix matrix_augmented(const Ordering& ordering) const; + /* ************************************************************************* */ // MUTABLE functions. FD:on the path to being eradicated /* ************************************************************************* */ diff --git a/cpp/Matrix.h b/cpp/Matrix.h index 66937d440..577558834 100644 --- a/cpp/Matrix.h +++ b/cpp/Matrix.h @@ -172,9 +172,17 @@ void householder(Matrix& A, size_t k); Vector backsubstitution(const Matrix& R, const Vector& b); /** - * create a matrix by stacking other matrices + * create a matrix by stacking other matrices + * Given a set of matrices: A1, A2, A3... + * @return combined matrix [A1; A2; A3] */ Matrix stack(size_t nrMatrices, ...); + +/** + * create a matrix by concatenating + * Given a set of matrices: A1, A2, A3... + * @return combined matrix [A1 A2 A3] + */ Matrix collect(std::vector matrices); Matrix collect(size_t nrMatrices, ...); diff --git a/cpp/testLinearFactor.cpp b/cpp/testLinearFactor.cpp index 6aaf388e6..75b743c94 100644 --- a/cpp/testLinearFactor.cpp +++ b/cpp/testLinearFactor.cpp @@ -497,9 +497,9 @@ TEST( LinearFactor, matrix ) // get the factor "f2" from the factor graph LinearFactor::shared_ptr lf = fg[1]; - // render with a given ordering - Ordering ord; - ord += "x1","x2"; + // render with a given ordering + Ordering ord; + ord += "x1","x2"; Matrix A; Vector b; boost::tie(A,b) = lf->matrix(ord); @@ -513,6 +513,29 @@ TEST( LinearFactor, matrix ) EQUALITY(b,b1); } +/* ************************************************************************* */ +TEST( LinearFactor, matrix_aug ) +{ + // create a small linear factor graph + LinearFactorGraph fg = createLinearFactorGraph(); + + // get the factor "f2" from the factor graph + LinearFactor::shared_ptr lf = fg[1]; + + // render with a given ordering + Ordering ord; + ord += "x1","x2"; + + Matrix Ab; + Ab = lf->matrix_augmented(ord); + + Matrix Ab1 = Matrix_(2,5, + -10.0, 0.0, 10.0, 0.0, 2.0, + 000.0,-10.0, 0.0, 10.0, -1.0 ); + + EQUALITY(Ab,Ab1); +} + /* ************************************************************************* */ TEST( LinearFactor, size ) {