testRegularHessianFactor.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
21 
23 
24 #include <boost/assign/std/vector.hpp>
25 #include <boost/assign/std/map.hpp>
26 #include <boost/assign/list_of.hpp>
27 
28 using namespace std;
29 using namespace gtsam;
30 using namespace boost::assign;
31 
32 /* ************************************************************************* */
33 TEST(RegularHessianFactor, Constructors)
34 {
35  // First construct a regular JacobianFactor
36  // 0.5*|x0 + x1 + x3 - [1;2]|^2 = 0.5*|A*x-b|^2, with A=[I I I]
37  Matrix A1 = I_2x2, A2 = I_2x2, A3 = I_2x2;
38  Vector2 b(1,2);
39  vector<pair<Key, Matrix> > terms;
40  terms += make_pair(0, A1), make_pair(1, A2), make_pair(3, A3);
41  RegularJacobianFactor<2> jf(terms, b);
42 
43  // Test conversion from JacobianFactor
44  RegularHessianFactor<2> factor(jf);
45 
46  // 0.5*|A*x-b|^2 = 0.5*(Ax-b)'*(Ax-b) = 0.5*x'*A'A*x - x'*A'b + 0.5*b'*b
47  // Compare with comment in HessianFactor: E(x) = 0.5 x^T G x - x^T g + 0.5 f
48  // Hence G = I6, g A'*b = [b;b;b], and f = b'*b = 1+4 = 5
49  Matrix G11 = I_2x2;
50  Matrix G12 = I_2x2;
51  Matrix G13 = I_2x2;
52 
53  Matrix G22 = I_2x2;
54  Matrix G23 = I_2x2;
55 
56  Matrix G33 = I_2x2;
57 
58  Vector2 g1 = b, g2 = b, g3 = b;
59 
60  double f = 5;
61 
62  // Test ternary constructor
63  RegularHessianFactor<2> factor2(0, 1, 3, G11, G12, G13, g1, G22, G23, g2, G33, g3, f);
64  EXPECT(assert_equal(factor,factor2));
65 
66  // Test n-way constructor
67  KeyVector keys {0, 1, 3};
68  vector<Matrix> Gs; Gs += G11, G12, G13, G22, G23, G33;
69  vector<Vector> gs; gs += g1, g2, g3;
71  EXPECT(assert_equal(factor, factor3));
72 
73  // Test constructor from Gaussian Factor Graph
75  gfg += jf;
76  RegularHessianFactor<2> factor4(gfg);
77  EXPECT(assert_equal(factor, factor4));
79  gfg2 += factor;
80  RegularHessianFactor<2> factor5(gfg);
81  EXPECT(assert_equal(factor, factor5));
82 
83  // Test constructor from Information matrix
84  Matrix info = factor.augmentedInformation();
85  vector<size_t> dims; dims += 2, 2, 2;
86  SymmetricBlockMatrix sym(dims, info, true);
87  RegularHessianFactor<2> factor6(keys, sym);
88  EXPECT(assert_equal(factor, factor6));
89 
90  // multiplyHessianAdd:
91  {
92  // brute force
93  Matrix AtA = factor.information();
94  HessianFactor::const_iterator i1 = factor.begin();
95  HessianFactor::const_iterator i2 = i1 + 1;
96  Vector X(6); X << 1,2,3,4,5,6;
97  Vector Y(6); Y << 9, 12, 9, 12, 9, 12;
98  EXPECT(assert_equal(Y,AtA*X));
99 
100  VectorValues x = map_list_of<Key, Vector>
101  (0, Vector2(1,2))
102  (1, Vector2(3,4))
103  (3, Vector2(5,6));
104 
106  expected.insert(0, Y.segment<2>(0));
107  expected.insert(1, Y.segment<2>(2));
108  expected.insert(3, Y.segment<2>(4));
109 
110  // VectorValues version
111  double alpha = 1.0;
112  VectorValues actualVV;
113  actualVV.insert(0, Vector2::Zero());
114  actualVV.insert(1, Vector2::Zero());
115  actualVV.insert(3, Vector2::Zero());
116  factor.multiplyHessianAdd(alpha, x, actualVV);
117  EXPECT(assert_equal(expected, actualVV));
118 
119  // RAW ACCESS
120  Vector expected_y(8); expected_y << 9, 12, 9, 12, 0, 0, 9, 12;
121  Vector fast_y = Vector8::Zero();
122  double xvalues[8] = {1,2,3,4,0,0,5,6};
123  factor.multiplyHessianAdd(alpha, xvalues, fast_y.data());
124  EXPECT(assert_equal(expected_y, fast_y));
125 
126  // now, do it with non-zero y
127  factor.multiplyHessianAdd(alpha, xvalues, fast_y.data());
128  EXPECT(assert_equal(2*expected_y, fast_y));
129 
130  // check some expressions
131  EXPECT(assert_equal(G12,factor.info().aboveDiagonalBlock(i1 - factor.begin(), i2 - factor.begin())));
132  EXPECT(assert_equal(G22,factor.info().diagonalBlock(i2 - factor.begin())));
133  }
134 }
135 
136 /* ************************************************************************* */
137 int main() { TestResult tr; return TestRegistry::runAllTests(tr);}
138 /* ************************************************************************* */
Scalar * b
Definition: benchVecAdd.cpp:17
static int runAllTests(TestResult &result)
Matrix expected
Definition: testMatrix.cpp:974
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Definition: Half.h:150
JacobianFactor factor3(keyY, A32, keyZ, A33, b3, noiseModel::Isotropic::Sigma(2, sigma3))
Matrix augmentedInformation() const override
const SymmetricBlockMatrix & info() const
Return underlying information matrix.
else if n * info
Factor Graph Values.
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:86
Eigen::VectorXd Vector
Definition: Vector.h:38
const_iterator begin() const
Definition: Factor.h:127
#define EXPECT(condition)
Definition: Test.h:151
Matrix information() const override
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
RealScalar alpha
HessianFactor class with constant sized blocks.
Linear Factor Graph where all factors are Gaussians.
TEST(RegularHessianFactor, Constructors)
constBlock aboveDiagonalBlock(DenseIndex I, DenseIndex J) const
Get block above the diagonal (I, J).
traits
Definition: chartTesting.h:28
bool assert_equal(const Matrix &expected, const Matrix &actual, double tol)
Definition: Matrix.cpp:42
Eigen::Vector2d Vector2
Definition: Vector.h:42
Pose3 g1(Rot3(), Point3(100.0, 0.0, 300.0))
JacobianFactor factor2(keyX, A21, keyY, A22, b2, noiseModel::Isotropic::Sigma(2, sigma2))
Eigen::SelfAdjointView< Block, Eigen::Upper > diagonalBlock(DenseIndex J)
Return the J&#39;th diagonal block as a self adjoint view.
const KeyVector keys
#define X
Definition: icosphere.cpp:20
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Pose3 g2(g1.expmap(h *V1_g1))
iterator insert(const std::pair< Key, Vector > &key_value)
void multiplyHessianAdd(double alpha, const VectorValues &x, VectorValues &y) const override


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:49:17