Fixed denseHessian bug - was only returning upper triangle, now returns full matrix

release/4.3a0
Richard Roberts 2012-01-18 15:42:00 +00:00
parent 269ac46bd9
commit b031996bbc
3 changed files with 39 additions and 4 deletions

View File

@ -346,9 +346,12 @@ namespace gtsam {
} }
dims.push_back(1); dims.push_back(1);
// combine all factors // combine all factors and get upper-triangular part of Hessian
HessianFactor combined(*this, dims, scatter); HessianFactor combined(*this, dims, scatter);
return combined.info(); Matrix result = combined.info();
// Fill in lower-triangular part of Hessian
result.triangularView<Eigen::StrictlyLower>() = result.transpose();
return result;
} }
/* ************************************************************************* */ /* ************************************************************************* */

View File

@ -226,7 +226,10 @@ namespace gtsam {
/** Return the number of columns and rows of the Hessian matrix */ /** Return the number of columns and rows of the Hessian matrix */
size_t rows() const { return info_.rows(); } size_t rows() const { return info_.rows(); }
/** Return a view of the block at (j1,j2) of the information matrix \f$ H \f$, no data is copied. /** Return a view of the block at (j1,j2) of the <emph>upper-triangular part</emph> of the
* information matrix \f$ H \f$, no data is copied. See HessianFactor class documentation
* above to explain that only the upper-triangular part of the information matrix is stored
* and returned by this function.
* @param j1 Which block row to get, as an iterator pointing to the slot in this factor. You can * @param j1 Which block row to get, as an iterator pointing to the slot in this factor. You can
* use, for example, begin() + 2 to get the 3rd variable in this factor. * use, for example, begin() + 2 to get the 3rd variable in this factor.
* @param j2 Which block column to get, as an iterator pointing to the slot in this factor. You can * @param j2 Which block column to get, as an iterator pointing to the slot in this factor. You can
@ -235,7 +238,10 @@ namespace gtsam {
*/ */
constBlock info(const_iterator j1, const_iterator j2) const { return info_(j1-begin(), j2-begin()); } constBlock info(const_iterator j1, const_iterator j2) const { return info_(j1-begin(), j2-begin()); }
/** Return the full *augmented* information matrix, as described above */ /** Return the <emph>upper-triangular part</emph> of the full *augmented* information matrix,
* as described above. See HessianFactor class documentation above to explain that only the
* upper-triangular part of the information matrix is stored and returned by this function.
*/
constBlock info() const { return info_.full(); } constBlock info() const { return info_.full(); }
/** Return the constant term \f$ f \f$ as described above /** Return the constant term \f$ f \f$ as described above

View File

@ -610,6 +610,32 @@ TEST(GaussianFactorGraph, sparseJacobian) {
EXPECT(assert_equal(expected, actual)); EXPECT(assert_equal(expected, actual));
} }
/* ************************************************************************* */
TEST(GaussianFactorGraph, denseHessian) {
// Create factor graph:
// x1 x2 x3 x4 x5 b
// 1 2 3 0 0 4
// 5 6 7 0 0 8
// 9 10 0 11 12 13
// 0 0 0 14 15 16
GaussianFactorGraph gfg;
SharedDiagonal model = sharedUnit(2);
gfg.add(0, Matrix_(2,3, 1., 2., 3., 5., 6., 7.), Vector_(2, 4., 8.), model);
gfg.add(0, Matrix_(2,3, 9.,10., 0., 0., 0., 0.), 1, Matrix_(2,2, 11., 12., 14., 15.), Vector_(2, 13.,16.), model);
Matrix jacobian(4,6);
jacobian <<
1, 2, 3, 0, 0, 4,
5, 6, 7, 0, 0, 8,
9,10, 0,11,12,13,
0, 0, 0,14,15,16;
Matrix expectedHessian = jacobian.transpose() * jacobian;
Matrix actualHessian = gfg.denseHessian();
EXPECT(assert_equal(expectedHessian, actualHessian));
}
/* ************************************************************************* */ /* ************************************************************************* */
int main() { TestResult tr; return TestRegistry::runAllTests(tr);} int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
/* ************************************************************************* */ /* ************************************************************************* */