Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / block_random_access_diagonal_matrix_test.cc
1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
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"
38 #include "Eigen/Cholesky"
39
40 namespace ceres {
41 namespace internal {
42
43 class BlockRandomAccessDiagonalMatrixTest : public ::testing::Test {
44  public:
45   void SetUp() {
46     std::vector<int> blocks;
47     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;
52
53     m_.reset(new BlockRandomAccessDiagonalMatrix(blocks));
54
55     EXPECT_EQ(m_->num_rows(), num_rows);
56     EXPECT_EQ(m_->num_cols(), num_rows);
57
58     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]);
89       }
90     }
91   }
92
93  protected:
94   int num_nonzeros_;
95   scoped_ptr<BlockRandomAccessDiagonalMatrix> m_;
96 };
97
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_);
102
103   Matrix dense;
104   tsm->ToDenseMatrix(&dense);
105
106   double kTolerance = 1e-14;
107
108   // (0,0)
109   EXPECT_NEAR((dense.block(0, 0, 3, 3) -
110                (Matrix::Ones(3, 3) + Matrix::Identity(3, 3))).norm(),
111               0.0,
112               kTolerance);
113
114   // (1,1)
115   EXPECT_NEAR((dense.block(3, 3, 4, 4) -
116                (2 * 2 * Matrix::Ones(4, 4) + Matrix::Identity(4, 4))).norm(),
117               0.0,
118               kTolerance);
119
120   // (1,1)
121   EXPECT_NEAR((dense.block(7, 7, 5, 5) -
122                (3 * 3 * Matrix::Ones(5, 5) + Matrix::Identity(5, 5))).norm(),
123               0.0,
124               kTolerance);
125
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);
131 }
132
133 TEST_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
145 TEST_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);
157 }
158
159 }  // namespace internal
160 }  // namespace ceres