Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / block_random_access_sparse_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 #include "ceres/block_random_access_sparse_matrix.h"
34 #include "ceres/internal/eigen.h"
35 #include "glog/logging.h"
36 #include "gtest/gtest.h"
37
38 namespace ceres {
39 namespace internal {
40
41 using std::make_pair;
42 using std::pair;
43 using std::set;
44 using std::vector;
45
46 TEST(BlockRandomAccessSparseMatrix, GetCell) {
47   vector<int> blocks;
48   blocks.push_back(3);
49   blocks.push_back(4);
50   blocks.push_back(5);
51   const int num_rows = 3 + 4 + 5;
52
53   set< pair<int, int> > block_pairs;
54   int num_nonzeros = 0;
55   block_pairs.insert(make_pair(0, 0));
56   num_nonzeros += blocks[0] * blocks[0];
57
58   block_pairs.insert(make_pair(1, 1));
59   num_nonzeros += blocks[1] * blocks[1];
60
61   block_pairs.insert(make_pair(1, 2));
62   num_nonzeros += blocks[1] * blocks[2];
63
64   block_pairs.insert(make_pair(0, 2));
65   num_nonzeros += blocks[2] * blocks[0];
66
67   BlockRandomAccessSparseMatrix m(blocks, block_pairs);
68   EXPECT_EQ(m.num_rows(), num_rows);
69   EXPECT_EQ(m.num_cols(), num_rows);
70
71   for (set<pair<int, int> >::const_iterator it = block_pairs.begin();
72        it != block_pairs.end();
73        ++it) {
74     const int row_block_id = it->first;
75     const int col_block_id = it->second;
76     int row;
77     int col;
78     int row_stride;
79     int col_stride;
80     CellInfo* cell =  m.GetCell(row_block_id, col_block_id,
81                                 &row, &col,
82                                 &row_stride, &col_stride);
83     EXPECT_TRUE(cell != NULL);
84     EXPECT_EQ(row, 0);
85     EXPECT_EQ(col, 0);
86     EXPECT_EQ(row_stride, blocks[row_block_id]);
87     EXPECT_EQ(col_stride, blocks[col_block_id]);
88
89     // Write into the block
90     MatrixRef(cell->values, row_stride, col_stride).block(
91         row, col, blocks[row_block_id], blocks[col_block_id]) =
92         (row_block_id + 1) * (col_block_id +1) *
93         Matrix::Ones(blocks[row_block_id], blocks[col_block_id]);
94   }
95
96   const TripletSparseMatrix* tsm = m.matrix();
97   EXPECT_EQ(tsm->num_nonzeros(), num_nonzeros);
98   EXPECT_EQ(tsm->max_num_nonzeros(), num_nonzeros);
99
100   Matrix dense;
101   tsm->ToDenseMatrix(&dense);
102
103   double kTolerance = 1e-14;
104
105   // (0, 0)
106   EXPECT_NEAR((dense.block(0, 0, 3, 3) - Matrix::Ones(3, 3)).norm(),
107               0.0,
108               kTolerance);
109   // (1, 1)
110   EXPECT_NEAR((dense.block(3, 3, 4, 4) - 2 * 2 * Matrix::Ones(4, 4)).norm(),
111               0.0,
112               kTolerance);
113   // (1, 2)
114   EXPECT_NEAR((dense.block(3, 3 + 4, 4, 5) - 2 * 3 * Matrix::Ones(4, 5)).norm(),
115               0.0,
116               kTolerance);
117   // (0, 2)
118   EXPECT_NEAR((dense.block(0, 3 + 4, 3, 5) - 3 * 1 * Matrix::Ones(3, 5)).norm(),
119               0.0,
120               kTolerance);
121
122   // There is nothing else in the matrix besides these four blocks.
123   EXPECT_NEAR(dense.norm(), sqrt(9. + 16. * 16. + 36. * 20. + 9. * 15.),
124               kTolerance);
125
126   Vector x = Vector::Ones(dense.rows());
127   Vector actual_y = Vector::Zero(dense.rows());
128   Vector expected_y = Vector::Zero(dense.rows());
129
130   expected_y += dense.selfadjointView<Eigen::Upper>() * x;
131   m.SymmetricRightMultiply(x.data(), actual_y.data());
132   EXPECT_NEAR((expected_y - actual_y).norm(), 0.0, kTolerance)
133       << "actual: " << actual_y.transpose() << "\n"
134       << "expected: " << expected_y.transpose()
135       << "matrix: \n " << dense;
136 }
137
138 // IntPairToLong is private, thus this fixture is needed to access and
139 // test it.
140 class BlockRandomAccessSparseMatrixTest : public ::testing::Test {
141  public:
142   virtual void SetUp() {
143     vector<int> blocks;
144     blocks.push_back(1);
145     set< pair<int, int> > block_pairs;
146     block_pairs.insert(make_pair(0, 0));
147     m_.reset(new BlockRandomAccessSparseMatrix(blocks, block_pairs));
148   }
149
150   void CheckIntPairToLong(int a, int b) {
151     int64 value = m_->IntPairToLong(a, b);
152     EXPECT_GT(value, 0) << "Overflow a = " << a << " b = " << b;
153     EXPECT_GT(value, a) << "Overflow a = " << a << " b = " << b;
154     EXPECT_GT(value, b) << "Overflow a = " << a << " b = " << b;
155   }
156
157   void CheckLongToIntPair() {
158     uint64 max_rows =  m_->kMaxRowBlocks;
159     for (int row = max_rows - 10; row < max_rows; ++row) {
160       for (int col = 0; col < 10; ++col) {
161         int row_computed;
162         int col_computed;
163         m_->LongToIntPair(m_->IntPairToLong(row, col),
164                           &row_computed,
165                           &col_computed);
166         EXPECT_EQ(row, row_computed);
167         EXPECT_EQ(col, col_computed);
168       }
169     }
170   }
171
172  private:
173   scoped_ptr<BlockRandomAccessSparseMatrix> m_;
174 };
175
176 TEST_F(BlockRandomAccessSparseMatrixTest, IntPairToLongOverflow) {
177   CheckIntPairToLong(std::numeric_limits<int>::max(),
178                      std::numeric_limits<int>::max());
179 }
180
181 TEST_F(BlockRandomAccessSparseMatrixTest, LongToIntPair) {
182   CheckLongToIntPair();
183 }
184
185 }  // namespace internal
186 }  // namespace ceres