testSymmetricBlockMatrix.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 
20 #include <boost/assign/list_of.hpp>
21 
22 using namespace std;
23 using namespace gtsam;
24 using boost::assign::list_of;
25 
27  list_of(3)(2)(1),
28  (Matrix(6, 6) <<
29  1, 2, 3, 4, 5, 6,
30  2, 8, 9, 10, 11, 12,
31  3, 9, 15, 16, 17, 18,
32  4, 10, 16, 22, 23, 24,
33  5, 11, 17, 23, 29, 30,
34  6, 12, 18, 24, 30, 36).finished());
35 
36 /* ************************************************************************* */
38 {
39  // On the diagonal
40  Matrix expected1 = (Matrix(2, 2) <<
41  22, 23,
42  23, 29).finished();
44  EXPECT(assert_equal(expected1, actual1));
45 
46  // Above the diagonal
47  Matrix expected2 = (Matrix(3, 2) <<
48  4, 5,
49  10, 11,
50  16, 17).finished();
52  EXPECT(assert_equal(expected2, actual2));
53 }
54 
55 /* ************************************************************************* */
57 {
58  // On the diagonal
59  Matrix expected1 = testBlockMatrix.diagonalBlock(1);
60  SymmetricBlockMatrix bm1 = SymmetricBlockMatrix::LikeActiveViewOf(testBlockMatrix);
61 
62  bm1.setDiagonalBlock(1, expected1);
63  Matrix actual1 = bm1.diagonalBlock(1);
64  EXPECT(assert_equal(expected1, actual1));
65 
66  // Above the diagonal
67  Matrix expected2 = testBlockMatrix.aboveDiagonalBlock(0, 1);
68  SymmetricBlockMatrix bm2 = SymmetricBlockMatrix::LikeActiveViewOf(testBlockMatrix);
69  bm2.setOffDiagonalBlock(0, 1, expected2);
70  Matrix actual2 = bm2.aboveDiagonalBlock(0, 1);
71  EXPECT(assert_equal(expected2, actual2));
72 
73  // Below the diagonal
74  Matrix expected3 = testBlockMatrix.aboveDiagonalBlock(0, 1).transpose();
75  SymmetricBlockMatrix bm3 = SymmetricBlockMatrix::LikeActiveViewOf(testBlockMatrix);
76  bm3.setOffDiagonalBlock(1, 0, expected3);
77  Matrix actual3 = bm3.aboveDiagonalBlock(0, 1).transpose();
78  EXPECT(assert_equal(expected3, actual3));
79 }
80 
81 /* ************************************************************************* */
83 {
84  // On the diagonal
85  Matrix expected1 = (Matrix(3, 3) <<
86  22, 23, 24,
87  23, 29, 30,
88  24, 30, 36).finished();
89  Matrix actual1 = testBlockMatrix.selfadjointView(1, 3);
90  EXPECT(assert_equal(expected1, actual1));
91 
92  // Above the diagonal
93  Matrix expected2 = (Matrix(3, 3) <<
94  4, 5, 6,
95  10, 11, 12,
96  16, 17, 18).finished();
97  Matrix actual2 = testBlockMatrix.aboveDiagonalRange(0, 1, 1, 3);
98  EXPECT(assert_equal(expected2, actual2));
99 }
100 
101 /* ************************************************************************* */
103 {
104  SymmetricBlockMatrix expected1(list_of(2)(3)(1), (Matrix(6, 6) <<
105  0, 0, 0, 0, 0, 0,
106  0, 0, 0, 0, 0, 0,
107  0, 0, 4, 6, 8, 0,
108  0, 0, 0, 9, 12, 0,
109  0, 0, 0, 0, 16, 0,
110  0, 0, 0, 0, 0, 0).finished());
111 
112  SymmetricBlockMatrix expected2(list_of(2)(3)(1), (Matrix(6, 6) <<
113  0, 0, 10, 15, 20, 0,
114  0, 0, 12, 18, 24, 0,
115  0, 0, 0, 0, 0, 0,
116  0, 0, 0, 0, 0, 0,
117  0, 0, 0, 0, 0, 0,
118  0, 0, 0, 0, 0, 0).finished());
119 
120  Matrix a = (Matrix(1, 3) << 2, 3, 4).finished();
121  Matrix b = (Matrix(1, 2) << 5, 6).finished();
122 
123  SymmetricBlockMatrix bm1(list_of(2)(3)(1));
124  bm1.setZero();
125  bm1.diagonalBlock(1).rankUpdate(a.transpose());
127 
128  SymmetricBlockMatrix bm2(list_of(2)(3)(1));
129  bm2.setZero();
130  bm2.updateOffDiagonalBlock(0, 1, b.transpose() * a);
132 
133  SymmetricBlockMatrix bm3(list_of(2)(3)(1));
134  bm3.setZero();
135  bm3.updateOffDiagonalBlock(1, 0, a.transpose() * b);
137 
138  SymmetricBlockMatrix bm4(list_of(2)(3)(1));
139  bm4.setZero();
140  bm4.updateDiagonalBlock(1, expected1.diagonalBlock(1));
142 
143  SymmetricBlockMatrix bm5(list_of(2)(3)(1));
144  bm5.setZero();
145  bm5.updateOffDiagonalBlock(0, 1, expected2.aboveDiagonalBlock(0, 1));
147 
148  SymmetricBlockMatrix bm6(list_of(2)(3)(1));
149  bm6.setZero();
150  bm6.updateOffDiagonalBlock(1, 0, expected2.aboveDiagonalBlock(0, 1).transpose());
152 }
153 
154 /* ************************************************************************* */
155 TEST(SymmetricBlockMatrix, inverseInPlace) {
156  // generate an invertible matrix
157  const Vector3 a(1.0, 0.2, 2.0), b(0.3, 0.8, -1.0), c(0.1, 0.2, 0.7);
158  Matrix inputMatrix(3, 3);
159  inputMatrix.setZero();
160  inputMatrix += a * a.transpose();
161  inputMatrix += b * b.transpose();
162  inputMatrix += c * c.transpose();
163  const Matrix expectedInverse = inputMatrix.inverse();
164 
165  SymmetricBlockMatrix symmMatrix(list_of(2)(1), inputMatrix);
166  // invert in place
167  symmMatrix.invertInPlace();
168  EXPECT(assert_equal(expectedInverse, symmMatrix.selfadjointView()));
169 }
170 
171 /* ************************************************************************* */
172 int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
173 /* ************************************************************************* */
174 
Scalar * b
Definition: benchVecAdd.cpp:17
static int runAllTests(TestResult &result)
Eigen::Vector3d Vector3
Definition: Vector.h:43
Access to matrices via blocks of pre-defined sizes. Used in GaussianFactor and GaussianConditional.
void setOffDiagonalBlock(DenseIndex I, DenseIndex J, const XprType &xpr)
Set an off-diagonal block. Only the upper triangular portion of xpr is evaluated. ...
Eigen::MatrixXd Matrix
Definition: base/Matrix.h:43
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
TEST(SymmetricBlockMatrix, ReadBlocks)
Definition: Half.h:150
Array33i a
void invertInPlace()
Invert the entire active matrix in place.
void setDiagonalBlock(DenseIndex I, const XprType &xpr)
Set a diagonal block. Only the upper triangular portion of xpr is evaluated.
static SymmetricBlockMatrix testBlockMatrix(list_of(3)(2)(1),(Matrix(6, 6)<< 1, 2, 3, 4, 5, 6, 2, 8, 9, 10, 11, 12, 3, 9, 15, 16, 17, 18, 4, 10, 16, 22, 23, 24, 5, 11, 17, 23, 29, 30, 6, 12, 18, 24, 30, 36).finished())
#define EXPECT(condition)
Definition: Test.h:151
void updateDiagonalBlock(DenseIndex I, const XprType &xpr)
Increment the diagonal block by the values in xpr. Only reads the upper triangular part of xpr...
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
constBlock aboveDiagonalRange(DenseIndex i_startBlock, DenseIndex i_endBlock, DenseIndex j_startBlock, DenseIndex j_endBlock) const
Get a range [i,j) from the matrix. Indices are in block units.
void setZero()
Set the entire active matrix zero.
Eigen::SelfAdjointView< constBlock, Eigen::Upper > selfadjointView(DenseIndex I, DenseIndex J) const
Return the square sub-matrix that contains blocks(i:j, i:j).
void updateOffDiagonalBlock(DenseIndex I, DenseIndex J, const XprType &xpr)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > Matrix
Eigen::SelfAdjointView< Block, Eigen::Upper > diagonalBlock(DenseIndex J)
Return the J&#39;th diagonal block as a self adjoint view.


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