blob: 03bfcb6091f9d7c2a50212eb551c1d1591481a0a [file] [log] [blame]
Carlos Hernandez79397c22014-08-07 17:51:38 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2014 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: richie.stebbing@gmail.com (Richard Stebbing)
30
31#include "ceres/dynamic_compressed_row_sparse_matrix.h"
32
33#include "ceres/casts.h"
34#include "ceres/compressed_row_sparse_matrix.h"
35#include "ceres/casts.h"
36#include "ceres/internal/eigen.h"
37#include "ceres/internal/scoped_ptr.h"
38#include "ceres/linear_least_squares_problems.h"
39#include "ceres/triplet_sparse_matrix.h"
40#include "gtest/gtest.h"
41
42namespace ceres {
43namespace internal {
44
45class DynamicCompressedRowSparseMatrixTest : public ::testing::Test {
46 protected:
47 virtual void SetUp() {
48 num_rows = 7;
49 num_cols = 4;
50
51 // The number of additional elements reserved when `Finalize` is called
52 // should have no effect on the number of rows, columns or nonzeros.
53 // Set this to some nonzero value to be sure.
54 num_additional_elements = 13;
55
56 expected_num_nonzeros = num_rows * num_cols - min(num_rows, num_cols);
57
58 InitialiseDenseReference();
59 InitialiseSparseMatrixReferences();
60
61 dcrsm.reset(new DynamicCompressedRowSparseMatrix(num_rows,
62 num_cols,
63 0));
64 }
65
66 void Finalize() {
67 dcrsm->Finalize(num_additional_elements);
68 }
69
70 void InitialiseDenseReference() {
71 dense.resize(num_rows, num_cols);
72 dense.setZero();
73 int num_nonzeros = 0;
74 for (int i = 0; i < (num_rows * num_cols); ++i) {
75 const int r = i / num_cols, c = i % num_cols;
76 if (r != c) {
77 dense(r, c) = i + 1;
78 ++num_nonzeros;
79 }
80 }
81 ASSERT_EQ(num_nonzeros, expected_num_nonzeros);
82 }
83
84 void InitialiseSparseMatrixReferences() {
85 std::vector<int> rows, cols;
86 std::vector<double> values;
87 for (int i = 0; i < (num_rows * num_cols); ++i) {
88 const int r = i / num_cols, c = i % num_cols;
89 if (r != c) {
90 rows.push_back(r);
91 cols.push_back(c);
92 values.push_back(i + 1);
93 }
94 }
95 ASSERT_EQ(values.size(), expected_num_nonzeros);
96
97 tsm.reset(new TripletSparseMatrix(num_rows,
98 num_cols,
99 expected_num_nonzeros));
100 std::copy(rows.begin(), rows.end(), tsm->mutable_rows());
101 std::copy(cols.begin(), cols.end(), tsm->mutable_cols());
102 std::copy(values.begin(), values.end(), tsm->mutable_values());
103 tsm->set_num_nonzeros(values.size());
104
105 Matrix dense_from_tsm;
106 tsm->ToDenseMatrix(&dense_from_tsm);
107 ASSERT_TRUE((dense.array() == dense_from_tsm.array()).all());
108
109 crsm.reset(new CompressedRowSparseMatrix(*tsm));
110 Matrix dense_from_crsm;
111 crsm->ToDenseMatrix(&dense_from_crsm);
112 ASSERT_TRUE((dense.array() == dense_from_crsm.array()).all());
113 }
114
115 void InsertNonZeroEntriesFromDenseReference() {
116 for (int r = 0; r < num_rows; ++r) {
117 for (int c = 0; c < num_cols; ++c) {
118 const double& v = dense(r, c);
119 if (v != 0.0) {
120 dcrsm->InsertEntry(r, c, v);
121 }
122 }
123 }
124 }
125
126 void ExpectEmpty() {
127 EXPECT_EQ(dcrsm->num_rows(), num_rows);
128 EXPECT_EQ(dcrsm->num_cols(), num_cols);
129 EXPECT_EQ(dcrsm->num_nonzeros(), 0);
130
131 Matrix dense_from_dcrsm;
132 dcrsm->ToDenseMatrix(&dense_from_dcrsm);
133 EXPECT_EQ(dense_from_dcrsm.rows(), num_rows);
134 EXPECT_EQ(dense_from_dcrsm.cols(), num_cols);
135 EXPECT_TRUE((dense_from_dcrsm.array() == 0.0).all());
136 }
137
138 void ExpectEqualToDenseReference() {
139 Matrix dense_from_dcrsm;
140 dcrsm->ToDenseMatrix(&dense_from_dcrsm);
141 EXPECT_TRUE((dense.array() == dense_from_dcrsm.array()).all());
142 }
143
144 void ExpectEqualToCompressedRowSparseMatrixReference() {
145 typedef Eigen::Map<const Eigen::VectorXi> ConstIntVectorRef;
146
147 ConstIntVectorRef crsm_rows(crsm->rows(), crsm->num_rows() + 1);
148 ConstIntVectorRef dcrsm_rows(dcrsm->rows(), dcrsm->num_rows() + 1);
149 EXPECT_TRUE((crsm_rows.array() == dcrsm_rows.array()).all());
150
151 ConstIntVectorRef crsm_cols(crsm->cols(), crsm->num_nonzeros());
152 ConstIntVectorRef dcrsm_cols(dcrsm->cols(), dcrsm->num_nonzeros());
153 EXPECT_TRUE((crsm_cols.array() == dcrsm_cols.array()).all());
154
155 ConstVectorRef crsm_values(crsm->values(), crsm->num_nonzeros());
156 ConstVectorRef dcrsm_values(dcrsm->values(), dcrsm->num_nonzeros());
157 EXPECT_TRUE((crsm_values.array() == dcrsm_values.array()).all());
158 }
159
160 int num_rows;
161 int num_cols;
162
163 int num_additional_elements;
164
165 int expected_num_nonzeros;
166
167 Matrix dense;
168 scoped_ptr<TripletSparseMatrix> tsm;
169 scoped_ptr<CompressedRowSparseMatrix> crsm;
170
171 scoped_ptr<DynamicCompressedRowSparseMatrix> dcrsm;
172};
173
174TEST_F(DynamicCompressedRowSparseMatrixTest, Initialization) {
175 ExpectEmpty();
176
177 Finalize();
178 ExpectEmpty();
179}
180
181TEST_F(DynamicCompressedRowSparseMatrixTest, InsertEntryAndFinalize) {
182 InsertNonZeroEntriesFromDenseReference();
183 ExpectEmpty();
184
185 Finalize();
186 ExpectEqualToDenseReference();
187 ExpectEqualToCompressedRowSparseMatrixReference();
188}
189
190TEST_F(DynamicCompressedRowSparseMatrixTest, ClearRows) {
191 InsertNonZeroEntriesFromDenseReference();
192 Finalize();
193 ExpectEqualToDenseReference();
194 ExpectEqualToCompressedRowSparseMatrixReference();
195
196 dcrsm->ClearRows(0, 0);
197 Finalize();
198 ExpectEqualToDenseReference();
199 ExpectEqualToCompressedRowSparseMatrixReference();
200
201 dcrsm->ClearRows(0, num_rows);
202 ExpectEqualToCompressedRowSparseMatrixReference();
203
204 Finalize();
205 ExpectEmpty();
206
207 InsertNonZeroEntriesFromDenseReference();
208 dcrsm->ClearRows(1, 2);
209 Finalize();
210 dense.block(1, 0, 2, num_cols).setZero();
211 ExpectEqualToDenseReference();
212
213 InitialiseDenseReference();
214}
215
216} // namespace internal
217} // namespace ceres