1 // 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"
38
39 namespace ceres {
40 namespace internal {
41
TEST(BlockRandomAccessDiagonalMatrix,GetCell)42 TEST(BlockRandomAccessDiagonalMatrix, GetCell) {
43 vector<int> blocks;
44 blocks.push_back(3);
45 blocks.push_back(4);
46 blocks.push_back(5);
47 const int num_rows = 3 + 4 + 5;
48 const int num_nonzeros = 3 * 3 + 4 * 4 + 5 * 5;
49
50 BlockRandomAccessDiagonalMatrix m(blocks);
51 EXPECT_EQ(m.num_rows(), num_rows);
52 EXPECT_EQ(m.num_cols(), num_rows);
53
54 for (int i = 0; i < blocks.size(); ++i) {
55 const int row_block_id = i;
56 int col_block_id;
57 int row;
58 int col;
59 int row_stride;
60 int col_stride;
61
62 for (int j = 0; j < blocks.size(); ++j) {
63 col_block_id = j;
64 CellInfo* cell = m.GetCell(row_block_id, col_block_id,
65 &row, &col,
66 &row_stride, &col_stride);
67 // Off diagonal entries are not present.
68 if (i != j) {
69 EXPECT_TRUE(cell == NULL);
70 continue;
71 }
72
73 EXPECT_TRUE(cell != NULL);
74 EXPECT_EQ(row, 0);
75 EXPECT_EQ(col, 0);
76 EXPECT_EQ(row_stride, blocks[row_block_id]);
77 EXPECT_EQ(col_stride, blocks[col_block_id]);
78
79 // Write into the block
80 MatrixRef(cell->values, row_stride, col_stride).block(
81 row, col, blocks[row_block_id], blocks[col_block_id]) =
82 (row_block_id + 1) * (col_block_id +1) *
83 Matrix::Ones(blocks[row_block_id], blocks[col_block_id]);
84 }
85 }
86
87 const TripletSparseMatrix* tsm = m.matrix();
88 EXPECT_EQ(tsm->num_nonzeros(), num_nonzeros);
89 EXPECT_EQ(tsm->max_num_nonzeros(), num_nonzeros);
90
91 Matrix dense;
92 tsm->ToDenseMatrix(&dense);
93
94 double kTolerance = 1e-14;
95
96 // (0,0)
97 EXPECT_NEAR((dense.block(0, 0, 3, 3) - Matrix::Ones(3, 3)).norm(),
98 0.0,
99 kTolerance);
100
101 // (1,1)
102 EXPECT_NEAR((dense.block(3, 3, 4, 4) - 2 * 2 * Matrix::Ones(4, 4)).norm(),
103 0.0,
104 kTolerance);
105
106 // (1,1)
107 EXPECT_NEAR((dense.block(7, 7, 5, 5) - 3 * 3 * Matrix::Ones(5, 5)).norm(),
108 0.0,
109 kTolerance);
110
111 // There is nothing else in the matrix besides these four blocks.
112 EXPECT_NEAR(dense.norm(), sqrt(9.0 + 16. * 16. + 81.0 * 25.), kTolerance);
113 }
114
115 } // namespace internal
116 } // namespace ceres
117