From 4ee4014d7a345fc410461c1d2855ef296b8820ba Mon Sep 17 00:00:00 2001 From: jingwuOUO Date: Mon, 26 Oct 2020 15:41:01 -0400 Subject: [PATCH] Refined unittest --- .../tests/testAcceleratedPowerMethod.cpp | 23 +++++++++++++------ gtsam/linear/tests/testPowerMethod.cpp | 15 +++++++----- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/gtsam/linear/tests/testAcceleratedPowerMethod.cpp b/gtsam/linear/tests/testAcceleratedPowerMethod.cpp index 6179d6ca6..228ce157c 100644 --- a/gtsam/linear/tests/testAcceleratedPowerMethod.cpp +++ b/gtsam/linear/tests/testAcceleratedPowerMethod.cpp @@ -45,17 +45,19 @@ TEST(AcceleratedPowerMethod, acceleratedPowerIteration) { A.coeffRef(3, 3) = 3; A.coeffRef(4, 4) = 2; A.coeffRef(5, 5) = 1; - Vector initial = Vector6::Random(); - const Vector6 x1 = (Vector(6) << 1.0, 0.0, 0.0, 0.0, 0.0, 0.0).finished(); + Vector initial = (Vector(6) << 0.24434602, 0.22829942, 0.70094486, 0.15463092, 0.55871359, + 0.2465342).finished(); const double ev1 = 6.0; // test accelerated power iteration AcceleratedPowerMethod apf(A, initial); - apf.compute(50, 1e-4); + apf.compute(100, 1e-5); EXPECT_LONGS_EQUAL(6, apf.eigenvector().rows()); Vector6 actual1 = apf.eigenvector(); - EXPECT(assert_equal(x1, actual1, 1e-4)); + const double ritzValue = actual1.dot(A * actual1); + const double ritzResidual = (A * actual1 - ritzValue * actual1).norm(); + EXPECT_DOUBLES_EQUAL(0, ritzResidual, 1e-5); EXPECT_DOUBLES_EQUAL(ev1, apf.eigenvalue(), 1e-5); } @@ -79,6 +81,7 @@ TEST(AcceleratedPowerMethod, useFactorGraph) { auto v0 = solver.eigenvectors().col(0); for (size_t j = 0; j < 3; j++) EXPECT_DOUBLES_EQUAL(-0.5, v0[j].real(), 1e-9); + // find the index of the max eigenvalue size_t maxIdx = 0; for (auto i = 0; i < solver.eigenvalues().rows(); ++i) { if (solver.eigenvalues()(i).real() >= solver.eigenvalues()(maxIdx).real()) @@ -86,7 +89,6 @@ TEST(AcceleratedPowerMethod, useFactorGraph) { } // Store the max eigenvalue and its according eigenvector const auto ev1 = solver.eigenvalues()(maxIdx).real(); - auto ev2 = solver.eigenvectors().col(maxIdx).real(); Vector disturb = Vector4::Random(); disturb.normalize(); @@ -94,9 +96,16 @@ TEST(AcceleratedPowerMethod, useFactorGraph) { double magnitude = initial.norm(); initial += 0.03 * magnitude * disturb; AcceleratedPowerMethod apf(L.first, initial); - apf.compute(50, 1e-4); + apf.compute(100, 1e-5); + // Check if the eigenvalue is the maximum eigen value EXPECT_DOUBLES_EQUAL(ev1, apf.eigenvalue(), 1e-8); - EXPECT(assert_equal(ev2, apf.eigenvector(), 4e-5)); + + // Check if the according ritz residual converged to the threshold + Vector actual1 = apf.eigenvector(); + const double ritzValue = actual1.dot(L.first * actual1); + const double ritzResidual = (L.first * actual1 - ritzValue * actual1).norm(); + EXPECT_DOUBLES_EQUAL(0, ritzResidual, 1e-5); + // Check } /* ************************************************************************* */ diff --git a/gtsam/linear/tests/testPowerMethod.cpp b/gtsam/linear/tests/testPowerMethod.cpp index 7f6d1efa7..4c96c5bca 100644 --- a/gtsam/linear/tests/testPowerMethod.cpp +++ b/gtsam/linear/tests/testPowerMethod.cpp @@ -45,14 +45,16 @@ TEST(PowerMethod, powerIteration) { A.coeffRef(3, 3) = 3; A.coeffRef(4, 4) = 2; A.coeffRef(5, 5) = 1; - Vector initial = Vector6::Random(); + Vector initial = (Vector(6) << 0.24434602, 0.22829942, 0.70094486, 0.15463092, 0.55871359, + 0.2465342).finished(); PowerMethod pf(A, initial); - pf.compute(50, 1e-4); + pf.compute(100, 1e-5); EXPECT_LONGS_EQUAL(6, pf.eigenvector().rows()); - const Vector6 x1 = (Vector(6) << 1.0, 0.0, 0.0, 0.0, 0.0, 0.0).finished(); - Vector6 actual0 = pf.eigenvector(); - EXPECT(assert_equal(x1, actual0, 1e-4)); + Vector6 actual1 = pf.eigenvector(); + const double ritzValue = actual1.dot(A * actual1); + const double ritzResidual = (A * actual1 - ritzValue * actual1).norm(); + EXPECT_DOUBLES_EQUAL(0, ritzResidual, 1e-5); const double ev1 = 6.0; EXPECT_DOUBLES_EQUAL(ev1, pf.eigenvalue(), 1e-5); @@ -77,6 +79,7 @@ TEST(PowerMethod, useFactorGraph) { auto v0 = solver.eigenvectors().col(0); for (size_t j = 0; j < 3; j++) EXPECT_DOUBLES_EQUAL(-0.5, v0[j].real(), 1e-9); + // find the index of the max eigenvalue size_t maxIdx = 0; for (auto i = 0; i < solver.eigenvalues().rows(); ++i) { if (solver.eigenvalues()(i).real() >= solver.eigenvalues()(maxIdx).real()) @@ -91,7 +94,7 @@ TEST(PowerMethod, useFactorGraph) { pf.compute(50, 1e-4); EXPECT_DOUBLES_EQUAL(ev1, pf.eigenvalue(), 1e-8); auto actual2 = pf.eigenvector(); - EXPECT(assert_equal(-ev2, actual2, 3e-5)); + EXPECT(assert_equal(ev2, actual2, 3e-5)); } /* ************************************************************************* */