blob: 354357fedb1a54fad08725dee33cd8df096f5fa0 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 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: keir@google.com (Keir Mierle)
30//
31// TODO(keir): Implement a generic "compare sparse matrix implementations" test
32// suite that can compare all the implementations. Then this file would shrink
33// in size.
34
35#include "ceres/dense_sparse_matrix.h"
36
37#include "gtest/gtest.h"
38#include "ceres/casts.h"
39#include "ceres/linear_least_squares_problems.h"
40#include "ceres/matrix_proto.h"
41#include "ceres/triplet_sparse_matrix.h"
42#include "ceres/internal/eigen.h"
43#include "ceres/internal/scoped_ptr.h"
44
45namespace ceres {
46namespace internal {
47
48void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) {
49 EXPECT_EQ(a->num_rows(), b->num_rows());
50 EXPECT_EQ(a->num_cols(), b->num_cols());
51
52 int num_rows = a->num_rows();
53 int num_cols = a->num_cols();
54
55 for (int i = 0; i < num_cols; ++i) {
56 Vector x = Vector::Zero(num_cols);
57 x(i) = 1.0;
58
59 Vector y_a = Vector::Zero(num_rows);
60 Vector y_b = Vector::Zero(num_rows);
61
62 a->RightMultiply(x.data(), y_a.data());
63 b->RightMultiply(x.data(), y_b.data());
64
65 EXPECT_EQ((y_a - y_b).norm(), 0);
66 }
67}
68
69class DenseSparseMatrixTest : public ::testing::Test {
70 protected :
71 virtual void SetUp() {
72 scoped_ptr<LinearLeastSquaresProblem> problem(
73 CreateLinearLeastSquaresProblemFromId(1));
74
75 CHECK_NOTNULL(problem.get());
76
77 tsm.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
78 dsm.reset(new DenseSparseMatrix(*tsm));
79
80 num_rows = tsm->num_rows();
81 num_cols = tsm->num_cols();
82 }
83
84 int num_rows;
85 int num_cols;
86
87 scoped_ptr<TripletSparseMatrix> tsm;
88 scoped_ptr<DenseSparseMatrix> dsm;
89};
90
91TEST_F(DenseSparseMatrixTest, RightMultiply) {
92 CompareMatrices(tsm.get(), dsm.get());
93
94 // Try with a not entirely zero vector to verify column interactions, which
95 // could be masked by a subtle bug when using the elementary vectors.
96 Vector a(num_cols);
97 for (int i = 0; i < num_cols; i++) {
98 a(i) = i;
99 }
100 Vector b1 = Vector::Zero(num_rows);
101 Vector b2 = Vector::Zero(num_rows);
102
103 tsm->RightMultiply(a.data(), b1.data());
104 dsm->RightMultiply(a.data(), b2.data());
105
106 EXPECT_EQ((b1 - b2).norm(), 0);
107}
108
109TEST_F(DenseSparseMatrixTest, LeftMultiply) {
110 for (int i = 0; i < num_rows; ++i) {
111 Vector a = Vector::Zero(num_rows);
112 a(i) = 1.0;
113
114 Vector b1 = Vector::Zero(num_cols);
115 Vector b2 = Vector::Zero(num_cols);
116
117 tsm->LeftMultiply(a.data(), b1.data());
118 dsm->LeftMultiply(a.data(), b2.data());
119
120 EXPECT_EQ((b1 - b2).norm(), 0);
121 }
122
123 // Try with a not entirely zero vector to verify column interactions, which
124 // could be masked by a subtle bug when using the elementary vectors.
125 Vector a(num_rows);
126 for (int i = 0; i < num_rows; i++) {
127 a(i) = i;
128 }
129 Vector b1 = Vector::Zero(num_cols);
130 Vector b2 = Vector::Zero(num_cols);
131
132 tsm->LeftMultiply(a.data(), b1.data());
133 dsm->LeftMultiply(a.data(), b2.data());
134
135 EXPECT_EQ((b1 - b2).norm(), 0);
136}
137
138TEST_F(DenseSparseMatrixTest, ColumnNorm) {
139 Vector b1 = Vector::Zero(num_cols);
140 Vector b2 = Vector::Zero(num_cols);
141
142 tsm->SquaredColumnNorm(b1.data());
143 dsm->SquaredColumnNorm(b2.data());
144
145 EXPECT_EQ((b1 - b2).norm(), 0);
146}
147
148TEST_F(DenseSparseMatrixTest, Scale) {
149 Vector scale(num_cols);
150 for (int i = 0; i < num_cols; ++i) {
151 scale(i) = i + 1;
152 }
153 tsm->ScaleColumns(scale.data());
154 dsm->ScaleColumns(scale.data());
155 CompareMatrices(tsm.get(), dsm.get());
156}
157
Sameer Agarwaldd2b17d2012-08-16 19:34:57 -0700158#ifndef CERES_NO_PROTOCOL_BUFFERS
Keir Mierle8ebb0732012-04-30 23:09:08 -0700159TEST_F(DenseSparseMatrixTest, Serialization) {
160 SparseMatrixProto proto;
161 dsm->ToProto(&proto);
162
163 DenseSparseMatrix n(proto);
164 ASSERT_EQ(dsm->num_rows(), n.num_rows());
165 ASSERT_EQ(dsm->num_cols(), n.num_cols());
166 ASSERT_EQ(dsm->num_nonzeros(), n.num_nonzeros());
167
168 for (int i = 0; i < n.num_rows() + 1; ++i) {
169 ASSERT_EQ(dsm->values()[i], proto.dense_matrix().values(i));
170 }
171}
172#endif
173
174TEST_F(DenseSparseMatrixTest, ToDenseMatrix) {
175 Matrix tsm_dense;
176 Matrix dsm_dense;
177
178 tsm->ToDenseMatrix(&tsm_dense);
179 dsm->ToDenseMatrix(&dsm_dense);
180
181 EXPECT_EQ((tsm_dense - dsm_dense).norm(), 0.0);
182}
183
184// TODO(keir): Make this work without protocol buffers.
Sameer Agarwaldd2b17d2012-08-16 19:34:57 -0700185#ifndef CERES_NO_PROTOCOL_BUFFERS
Keir Mierle8ebb0732012-04-30 23:09:08 -0700186TEST_F(DenseSparseMatrixTest, AppendDiagonal) {
187 DenseSparseMatrixProto proto;
188 proto.set_num_rows(3);
189 proto.set_num_cols(3);
190 for (int i = 0; i < 9; ++i) {
191 proto.add_values(i);
192 }
193 SparseMatrixProto outer_proto;
194 *outer_proto.mutable_dense_matrix() = proto;
195
196 DenseSparseMatrix dsm(outer_proto);
197
198 double diagonal[] = { 10, 11, 12 };
199 dsm.AppendDiagonal(diagonal);
200
201 // Verify the diagonal got added.
202 Matrix m = dsm.matrix();
203 EXPECT_EQ(6, m.rows());
204 EXPECT_EQ(3, m.cols());
205 for (int i = 0; i < 3; ++i) {
206 for (int j = 0; j < 3; ++j) {
207 EXPECT_EQ(3 * i + j, m(i, j));
208 if (i == j) {
209 EXPECT_EQ(10 + i, m(i + 3, j));
210 } else {
211 EXPECT_EQ(0, m(i + 3, j));
212 }
213 }
214 }
215
216 // Verify the diagonal gets removed.
217 dsm.RemoveDiagonal();
218 m = dsm.matrix();
219
220 EXPECT_EQ(3, m.rows());
221 EXPECT_EQ(3, m.cols());
222
223 for (int i = 0; i < 3; ++i) {
224 for (int j = 0; j < 3; ++j) {
225 EXPECT_EQ(3 * i + j, m(i, j));
226 }
227 }
228}
229#endif
230
231} // namespace internal
232} // namespace ceres