1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Author: sameeragarwal@google.com (Sameer Agarwal)
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"
38 #include "Eigen/Cholesky"
43 class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test {
46 std::vector<int> blocks;
50 const int num_rows = 3 + 4 + 5;
51 num_nonzeros_ = 3 * 3 + 4 * 4 + 5 * 5;
53 m_.reset(new BlockRandomAccessDiagonalMatrix(blocks));
55 EXPECT_EQ(m_->num_rows(), num_rows);
56 EXPECT_EQ(m_->num_cols(), num_rows);
58 for (int i = 0; i < blocks.size(); ++i) {
59 const int row_block_id = i;
66 for (int j = 0; j < blocks.size(); ++j) {
68 CellInfo* cell = m_->GetCell(row_block_id, col_block_id,
70 &row_stride, &col_stride);
71 // Off diagonal entries are not present.
73 EXPECT_TRUE(cell == NULL);
77 EXPECT_TRUE(cell != NULL);
80 EXPECT_EQ(row_stride, blocks[row_block_id]);
81 EXPECT_EQ(col_stride, blocks[col_block_id]);
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]);
95 scoped_ptr<BlockRandomAccessDiagonalMatrix> m_;
98 TEST_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_);
104 tsm->ToDenseMatrix(&dense);
106 double kTolerance = 1e-14;
109 EXPECT_NEAR((dense.block(0, 0, 3, 3) -
110 (Matrix::Ones(3, 3) + Matrix::Identity(3, 3))).norm(),
115 EXPECT_NEAR((dense.block(3, 3, 4, 4) -
116 (2 * 2 * Matrix::Ones(4, 4) + Matrix::Identity(4, 4))).norm(),
121 EXPECT_NEAR((dense.block(7, 7, 5, 5) -
122 (3 * 3 * Matrix::Ones(5, 5) + Matrix::Identity(5, 5))).norm(),
126 // There is nothing else in the matrix besides these four blocks.
127 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);
133 TEST_F(BlockRandomAccessDiagonalMatrixTest, RightMultiply) {
134 double kTolerance = 1e-14;
135 const TripletSparseMatrix* tsm = m_->matrix();
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);
145 TEST_F(BlockRandomAccessDiagonalMatrixTest, Invert) {
146 double kTolerance = 1e-14;
147 const TripletSparseMatrix* tsm = m_->matrix();
149 tsm->ToDenseMatrix(&dense);
150 Matrix expected_inverse =
151 dense.llt().solve(Matrix::Identity(dense.rows(), dense.rows()));
154 tsm->ToDenseMatrix(&dense);
156 EXPECT_NEAR((expected_inverse - dense).norm(), 0.0, kTolerance);
159 } // namespace internal