blob: 9308861eb26c84baa3702266adf47b38ccf8680d [file] [log] [blame]
Sameer Agarwal0e2743e2013-10-23 14:51:07 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2013 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30
31#include <limits>
32#include <vector>
33
34#include "ceres/block_random_access_diagonal_matrix.h"
35#include "ceres/internal/eigen.h"
36#include "glog/logging.h"
37#include "gtest/gtest.h"
Sameer Agarwal4ad91492014-09-24 23:54:18 -070038#include "Eigen/Cholesky"
Sameer Agarwal0e2743e2013-10-23 14:51:07 -070039
40namespace ceres {
41namespace internal {
42
Sameer Agarwal4ad91492014-09-24 23:54:18 -070043class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test {
44 public:
45 void SetUp() {
Sameer Agarwalbcc865f2014-12-17 07:35:09 -080046 std::vector<int> blocks;
Sameer Agarwal4ad91492014-09-24 23:54:18 -070047 blocks.push_back(3);
48 blocks.push_back(4);
49 blocks.push_back(5);
50 const int num_rows = 3 + 4 + 5;
51 num_nonzeros_ = 3 * 3 + 4 * 4 + 5 * 5;
Sameer Agarwal0e2743e2013-10-23 14:51:07 -070052
Sameer Agarwal4ad91492014-09-24 23:54:18 -070053 m_.reset(new BlockRandomAccessDiagonalMatrix(blocks));
Sameer Agarwal0e2743e2013-10-23 14:51:07 -070054
Sameer Agarwal4ad91492014-09-24 23:54:18 -070055 EXPECT_EQ(m_->num_rows(), num_rows);
56 EXPECT_EQ(m_->num_cols(), num_rows);
Sameer Agarwal0e2743e2013-10-23 14:51:07 -070057
Sameer Agarwal4ad91492014-09-24 23:54:18 -070058 for (int i = 0; i < blocks.size(); ++i) {
59 const int row_block_id = i;
60 int col_block_id;
61 int row;
62 int col;
63 int row_stride;
64 int col_stride;
65
66 for (int j = 0; j < blocks.size(); ++j) {
67 col_block_id = j;
68 CellInfo* cell = m_->GetCell(row_block_id, col_block_id,
69 &row, &col,
70 &row_stride, &col_stride);
71 // Off diagonal entries are not present.
72 if (i != j) {
73 EXPECT_TRUE(cell == NULL);
74 continue;
75 }
76
77 EXPECT_TRUE(cell != NULL);
78 EXPECT_EQ(row, 0);
79 EXPECT_EQ(col, 0);
80 EXPECT_EQ(row_stride, blocks[row_block_id]);
81 EXPECT_EQ(col_stride, blocks[col_block_id]);
82
83 // Write into the block
84 MatrixRef(cell->values, row_stride, col_stride).block(
85 row, col, blocks[row_block_id], blocks[col_block_id]) =
86 (row_block_id + 1) * (col_block_id +1) *
87 Matrix::Ones(blocks[row_block_id], blocks[col_block_id])
88 + Matrix::Identity(blocks[row_block_id], blocks[row_block_id]);
Sameer Agarwal0e2743e2013-10-23 14:51:07 -070089 }
Sameer Agarwal0e2743e2013-10-23 14:51:07 -070090 }
91 }
92
Sameer Agarwal4ad91492014-09-24 23:54:18 -070093 protected:
94 int num_nonzeros_;
95 scoped_ptr<BlockRandomAccessDiagonalMatrix> m_;
96};
97
98TEST_F(BlockRandomAccessDiagonalMatrixTest, MatrixContents) {
99 const TripletSparseMatrix* tsm = m_->matrix();
100 EXPECT_EQ(tsm->num_nonzeros(), num_nonzeros_);
101 EXPECT_EQ(tsm->max_num_nonzeros(), num_nonzeros_);
Sameer Agarwal0e2743e2013-10-23 14:51:07 -0700102
103 Matrix dense;
104 tsm->ToDenseMatrix(&dense);
105
106 double kTolerance = 1e-14;
107
108 // (0,0)
Sameer Agarwal4ad91492014-09-24 23:54:18 -0700109 EXPECT_NEAR((dense.block(0, 0, 3, 3) -
110 (Matrix::Ones(3, 3) + Matrix::Identity(3, 3))).norm(),
Sameer Agarwal0e2743e2013-10-23 14:51:07 -0700111 0.0,
112 kTolerance);
113
114 // (1,1)
Sameer Agarwal4ad91492014-09-24 23:54:18 -0700115 EXPECT_NEAR((dense.block(3, 3, 4, 4) -
116 (2 * 2 * Matrix::Ones(4, 4) + Matrix::Identity(4, 4))).norm(),
Sameer Agarwal0e2743e2013-10-23 14:51:07 -0700117 0.0,
118 kTolerance);
119
120 // (1,1)
Sameer Agarwal4ad91492014-09-24 23:54:18 -0700121 EXPECT_NEAR((dense.block(7, 7, 5, 5) -
122 (3 * 3 * Matrix::Ones(5, 5) + Matrix::Identity(5, 5))).norm(),
Sameer Agarwal0e2743e2013-10-23 14:51:07 -0700123 0.0,
124 kTolerance);
125
126 // There is nothing else in the matrix besides these four blocks.
Sameer Agarwal4ad91492014-09-24 23:54:18 -0700127 EXPECT_NEAR(dense.norm(),
128 sqrt(6 * 1.0 + 3 * 4.0 +
129 12 * 16.0 + 4 * 25.0 +
130 20 * 81.0 + 5 * 100.0), kTolerance);
131}
132
133TEST_F(BlockRandomAccessDiagonalMatrixTest, RightMultiply) {
134 double kTolerance = 1e-14;
135 const TripletSparseMatrix* tsm = m_->matrix();
136 Matrix dense;
137 tsm->ToDenseMatrix(&dense);
138 Vector x = Vector::Random(dense.rows());
139 Vector expected_y = dense * x;
140 Vector actual_y = Vector::Zero(dense.rows());
141 m_->RightMultiply(x.data(), actual_y.data());
142 EXPECT_NEAR((expected_y - actual_y).norm(), 0, kTolerance);
143}
144
145TEST_F(BlockRandomAccessDiagonalMatrixTest, Invert) {
146 double kTolerance = 1e-14;
147 const TripletSparseMatrix* tsm = m_->matrix();
148 Matrix dense;
149 tsm->ToDenseMatrix(&dense);
150 Matrix expected_inverse =
151 dense.llt().solve(Matrix::Identity(dense.rows(), dense.rows()));
152
153 m_->Invert();
154 tsm->ToDenseMatrix(&dense);
155
156 EXPECT_NEAR((expected_inverse - dense).norm(), 0.0, kTolerance);
Sameer Agarwal0e2743e2013-10-23 14:51:07 -0700157}
158
159} // namespace internal
160} // namespace ceres