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: richie.stebbing@gmail.com (Richard Stebbing)
31 #include "ceres/dynamic_compressed_row_sparse_matrix.h"
33 #include "ceres/casts.h"
34 #include "ceres/compressed_row_sparse_matrix.h"
35 #include "ceres/internal/eigen.h"
36 #include "ceres/internal/scoped_ptr.h"
37 #include "ceres/linear_least_squares_problems.h"
38 #include "ceres/triplet_sparse_matrix.h"
39 #include "gtest/gtest.h"
47 class DynamicCompressedRowSparseMatrixTest : public ::testing::Test {
49 virtual void SetUp() {
53 // The number of additional elements reserved when `Finalize` is called
54 // should have no effect on the number of rows, columns or nonzeros.
55 // Set this to some nonzero value to be sure.
56 num_additional_elements = 13;
58 expected_num_nonzeros = num_rows * num_cols - std::min(num_rows, num_cols);
60 InitialiseDenseReference();
61 InitialiseSparseMatrixReferences();
63 dcrsm.reset(new DynamicCompressedRowSparseMatrix(num_rows,
69 dcrsm->Finalize(num_additional_elements);
72 void InitialiseDenseReference() {
73 dense.resize(num_rows, num_cols);
76 for (int i = 0; i < (num_rows * num_cols); ++i) {
77 const int r = i / num_cols, c = i % num_cols;
83 ASSERT_EQ(num_nonzeros, expected_num_nonzeros);
86 void InitialiseSparseMatrixReferences() {
87 vector<int> rows, cols;
88 vector<double> values;
89 for (int i = 0; i < (num_rows * num_cols); ++i) {
90 const int r = i / num_cols, c = i % num_cols;
94 values.push_back(i + 1);
97 ASSERT_EQ(values.size(), expected_num_nonzeros);
99 tsm.reset(new TripletSparseMatrix(num_rows,
101 expected_num_nonzeros));
102 copy(rows.begin(), rows.end(), tsm->mutable_rows());
103 copy(cols.begin(), cols.end(), tsm->mutable_cols());
104 copy(values.begin(), values.end(), tsm->mutable_values());
105 tsm->set_num_nonzeros(values.size());
107 Matrix dense_from_tsm;
108 tsm->ToDenseMatrix(&dense_from_tsm);
109 ASSERT_TRUE((dense.array() == dense_from_tsm.array()).all());
111 crsm.reset(CompressedRowSparseMatrix::FromTripletSparseMatrix(*tsm));
112 Matrix dense_from_crsm;
113 crsm->ToDenseMatrix(&dense_from_crsm);
114 ASSERT_TRUE((dense.array() == dense_from_crsm.array()).all());
117 void InsertNonZeroEntriesFromDenseReference() {
118 for (int r = 0; r < num_rows; ++r) {
119 for (int c = 0; c < num_cols; ++c) {
120 const double& v = dense(r, c);
122 dcrsm->InsertEntry(r, c, v);
129 EXPECT_EQ(dcrsm->num_rows(), num_rows);
130 EXPECT_EQ(dcrsm->num_cols(), num_cols);
131 EXPECT_EQ(dcrsm->num_nonzeros(), 0);
133 Matrix dense_from_dcrsm;
134 dcrsm->ToDenseMatrix(&dense_from_dcrsm);
135 EXPECT_EQ(dense_from_dcrsm.rows(), num_rows);
136 EXPECT_EQ(dense_from_dcrsm.cols(), num_cols);
137 EXPECT_TRUE((dense_from_dcrsm.array() == 0.0).all());
140 void ExpectEqualToDenseReference() {
141 Matrix dense_from_dcrsm;
142 dcrsm->ToDenseMatrix(&dense_from_dcrsm);
143 EXPECT_TRUE((dense.array() == dense_from_dcrsm.array()).all());
146 void ExpectEqualToCompressedRowSparseMatrixReference() {
147 typedef Eigen::Map<const Eigen::VectorXi> ConstIntVectorRef;
149 ConstIntVectorRef crsm_rows(crsm->rows(), crsm->num_rows() + 1);
150 ConstIntVectorRef dcrsm_rows(dcrsm->rows(), dcrsm->num_rows() + 1);
151 EXPECT_TRUE((crsm_rows.array() == dcrsm_rows.array()).all());
153 ConstIntVectorRef crsm_cols(crsm->cols(), crsm->num_nonzeros());
154 ConstIntVectorRef dcrsm_cols(dcrsm->cols(), dcrsm->num_nonzeros());
155 EXPECT_TRUE((crsm_cols.array() == dcrsm_cols.array()).all());
157 ConstVectorRef crsm_values(crsm->values(), crsm->num_nonzeros());
158 ConstVectorRef dcrsm_values(dcrsm->values(), dcrsm->num_nonzeros());
159 EXPECT_TRUE((crsm_values.array() == dcrsm_values.array()).all());
165 int num_additional_elements;
167 int expected_num_nonzeros;
170 scoped_ptr<TripletSparseMatrix> tsm;
171 scoped_ptr<CompressedRowSparseMatrix> crsm;
173 scoped_ptr<DynamicCompressedRowSparseMatrix> dcrsm;
176 TEST_F(DynamicCompressedRowSparseMatrixTest, Initialization) {
183 TEST_F(DynamicCompressedRowSparseMatrixTest, InsertEntryAndFinalize) {
184 InsertNonZeroEntriesFromDenseReference();
188 ExpectEqualToDenseReference();
189 ExpectEqualToCompressedRowSparseMatrixReference();
192 TEST_F(DynamicCompressedRowSparseMatrixTest, ClearRows) {
193 InsertNonZeroEntriesFromDenseReference();
195 ExpectEqualToDenseReference();
196 ExpectEqualToCompressedRowSparseMatrixReference();
198 dcrsm->ClearRows(0, 0);
200 ExpectEqualToDenseReference();
201 ExpectEqualToCompressedRowSparseMatrixReference();
203 dcrsm->ClearRows(0, num_rows);
204 ExpectEqualToCompressedRowSparseMatrixReference();
209 InsertNonZeroEntriesFromDenseReference();
210 dcrsm->ClearRows(1, 2);
212 dense.block(1, 0, 2, num_cols).setZero();
213 ExpectEqualToDenseReference();
215 InitialiseDenseReference();
218 } // namespace internal