From 28658490e87c1e565dd143fd0f26fcaff3f71610 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Tue, 10 Jan 2023 14:36:58 -0800 Subject: [PATCH] Fix sign issue and added test --- gtsam/linear/GaussianBayesNet.cpp | 2 +- gtsam/linear/tests/testGaussianBayesNet.cpp | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/gtsam/linear/GaussianBayesNet.cpp b/gtsam/linear/GaussianBayesNet.cpp index 2887e6d3a..2e62d2d42 100644 --- a/gtsam/linear/GaussianBayesNet.cpp +++ b/gtsam/linear/GaussianBayesNet.cpp @@ -122,7 +122,7 @@ namespace gtsam { /* ************************************************************************* */ double GaussianBayesNet::evaluate(const VectorValues& x) const { - return exp(-logProbability(x)); + return exp(logProbability(x)); } /* ************************************************************************* */ diff --git a/gtsam/linear/tests/testGaussianBayesNet.cpp b/gtsam/linear/tests/testGaussianBayesNet.cpp index 7b55d00c3..7f0641dbf 100644 --- a/gtsam/linear/tests/testGaussianBayesNet.cpp +++ b/gtsam/linear/tests/testGaussianBayesNet.cpp @@ -78,9 +78,13 @@ TEST(GaussianBayesNet, Evaluate1) { // which at the mean is 1.0! So, the only thing we need to calculate is // the normalization constant 1.0/sqrt((2*pi*Sigma).det()). // The covariance matrix inv(Sigma) = R'*R, so the determinant is - const double expected = sqrt((invSigma / (2 * M_PI)).determinant()); + const double constant = sqrt((invSigma / (2 * M_PI)).determinant()); + EXPECT_DOUBLES_EQUAL(log(constant), + smallBayesNet.at(0)->logNormalizationConstant() + + smallBayesNet.at(1)->logNormalizationConstant(), + 1e-9); const double actual = smallBayesNet.evaluate(mean); - EXPECT_DOUBLES_EQUAL(expected, actual, 1e-9); + EXPECT_DOUBLES_EQUAL(constant, actual, 1e-9); } // Check the evaluate with non-unit noise. @@ -89,9 +93,9 @@ TEST(GaussianBayesNet, Evaluate2) { const VectorValues mean = noisyBayesNet.optimize(); const Matrix R = noisyBayesNet.matrix().first; const Matrix invSigma = R.transpose() * R; - const double expected = sqrt((invSigma / (2 * M_PI)).determinant()); + const double constant = sqrt((invSigma / (2 * M_PI)).determinant()); const double actual = noisyBayesNet.evaluate(mean); - EXPECT_DOUBLES_EQUAL(expected, actual, 1e-9); + EXPECT_DOUBLES_EQUAL(constant, actual, 1e-9); } /* ************************************************************************* */