diff --git a/gtsam/linear/tests/powerMethodExample.h b/gtsam/linear/tests/powerMethodExample.h new file mode 100644 index 000000000..f80299386 --- /dev/null +++ b/gtsam/linear/tests/powerMethodExample.h @@ -0,0 +1,67 @@ +/* ---------------------------------------------------------------------------- + + * GTSAM Copyright 2010-2019, Georgia Tech Research Corporation, + * Atlanta, Georgia 30332-0415 + * All Rights Reserved + * Authors: Frank Dellaert, et al. (see THANKS for the full author list) + + * See LICENSE for the license information + + * -------------------------------------------------------------------------- */ + +/** + * powerMethodExample.h + * + * @file powerMethodExample.h + * @date Nov 2020 + * @author Jing Wu + * @brief Create sparse and dense factor graph for + * PowerMethod/AcceleratedPowerMethod + */ + +#include + +#include + + +namespace gtsam { +namespace linear { +namespace test { +namespace example { + +/* ************************************************************************* */ +inline GaussianFactorGraph createSparseGraph() { + using symbol_shorthand::X; + // Let's make a scalar synchronization graph with 4 nodes + GaussianFactorGraph fg; + auto model = noiseModel::Unit::Create(1); + for (size_t j = 0; j < 3; j++) { + fg.add(X(j), -I_1x1, X(j + 1), I_1x1, Vector1::Zero(), model); + } + fg.add(X(3), -I_1x1, X(0), I_1x1, Vector1::Zero(), model); // extra row + + return fg; +} + +/* ************************************************************************* */ +inline GaussianFactorGraph createDenseGraph() { + using symbol_shorthand::X; + // Let's make a scalar synchronization graph with 10 nodes + GaussianFactorGraph fg; + auto model = noiseModel::Unit::Create(1); + // Iterate over nodes + for (size_t j = 0; j < 10; j++) { + // Each node has an edge with all the others + for (size_t i = 1; i < 10; i++) + fg.add(X(j), -I_1x1, X((j + i) % 10), I_1x1, Vector1::Zero(), model); + } + + return fg; +} + +/* ************************************************************************* */ + +} // namespace example +} // namespace test +} // namespace linear +} // namespace gtsam diff --git a/gtsam/linear/tests/testAcceleratedPowerMethod.cpp b/gtsam/linear/tests/testAcceleratedPowerMethod.cpp index c7c8e8a55..7c4a90936 100644 --- a/gtsam/linear/tests/testAcceleratedPowerMethod.cpp +++ b/gtsam/linear/tests/testAcceleratedPowerMethod.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -33,7 +34,6 @@ using namespace std; using namespace gtsam; -using symbol_shorthand::X; /* ************************************************************************* */ TEST(AcceleratedPowerMethod, acceleratedPowerIteration) { @@ -65,12 +65,7 @@ TEST(AcceleratedPowerMethod, acceleratedPowerIteration) { /* ************************************************************************* */ TEST(AcceleratedPowerMethod, useFactorGraphSparse) { // Let's make a scalar synchronization graph with 4 nodes - GaussianFactorGraph fg; - auto model = noiseModel::Unit::Create(1); - for (size_t j = 0; j < 3; j++) { - fg.add(X(j), -I_1x1, X(j + 1), I_1x1, Vector1::Zero(), model); - } - fg.add(X(3), -I_1x1, X(0), I_1x1, Vector1::Zero(), model); // extra row + GaussianFactorGraph fg = gtsam::linear::test::example::createSparseGraph(); // Get eigenvalues and eigenvectors with Eigen auto L = fg.hessian(); @@ -105,20 +100,7 @@ TEST(AcceleratedPowerMethod, useFactorGraphSparse) { /* ************************************************************************* */ TEST(AcceleratedPowerMethod, useFactorGraphDense) { // Let's make a scalar synchronization graph with 10 nodes - GaussianFactorGraph fg; - auto model = noiseModel::Unit::Create(1); - // Each node has an edge with all the others - for (size_t j = 0; j < 10; j++) { - fg.add(X(j), -I_1x1, X((j + 1)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 2)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 3)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 4)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 5)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 6)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 7)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 8)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 9)%10 ), I_1x1, Vector1::Zero(), model); - } + GaussianFactorGraph fg = gtsam::linear::test::example::createDenseGraph(); // Get eigenvalues and eigenvectors with Eigen auto L = fg.hessian(); diff --git a/gtsam/linear/tests/testPowerMethod.cpp b/gtsam/linear/tests/testPowerMethod.cpp index 7adfd0aa5..54d4c720d 100644 --- a/gtsam/linear/tests/testPowerMethod.cpp +++ b/gtsam/linear/tests/testPowerMethod.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -33,7 +34,6 @@ using namespace std; using namespace gtsam; -using symbol_shorthand::X; /* ************************************************************************* */ TEST(PowerMethod, powerIteration) { @@ -63,12 +63,7 @@ TEST(PowerMethod, powerIteration) { /* ************************************************************************* */ TEST(PowerMethod, useFactorGraphSparse) { // Let's make a scalar synchronization graph with 4 nodes - GaussianFactorGraph fg; - auto model = noiseModel::Unit::Create(1); - for (size_t j = 0; j < 3; j++) { - fg.add(X(j), -I_1x1, X(j + 1), I_1x1, Vector1::Zero(), model); - } - fg.add(X(3), -I_1x1, X(0), I_1x1, Vector1::Zero(), model); // extra row + GaussianFactorGraph fg = gtsam::linear::test::example::createSparseGraph(); // Get eigenvalues and eigenvectors with Eigen auto L = fg.hessian(); @@ -96,20 +91,7 @@ TEST(PowerMethod, useFactorGraphSparse) { /* ************************************************************************* */ TEST(PowerMethod, useFactorGraphDense) { // Let's make a scalar synchronization graph with 10 nodes - GaussianFactorGraph fg; - auto model = noiseModel::Unit::Create(1); - // Each node has an edge with all the others - for (size_t j = 0; j < 10; j++) { - fg.add(X(j), -I_1x1, X((j + 1)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 2)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 3)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 4)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 5)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 6)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 7)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 8)%10 ), I_1x1, Vector1::Zero(), model); - fg.add(X(j), -I_1x1, X((j + 9)%10 ), I_1x1, Vector1::Zero(), model); - } + GaussianFactorGraph fg = gtsam::linear::test::example::createDenseGraph(); // Get eigenvalues and eigenvectors with Eigen auto L = fg.hessian();