1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2010, 2011, 2012, 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: 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 "ceres/casts.h"
38 #include "ceres/linear_least_squares_problems.h"
39 #include "ceres/triplet_sparse_matrix.h"
40 #include "ceres/internal/eigen.h"
41 #include "ceres/internal/scoped_ptr.h"
42 #include "glog/logging.h"
43 #include "gtest/gtest.h"
44
45 namespace ceres {
46 namespace internal {
47
CompareMatrices(const SparseMatrix * a,const SparseMatrix * b)48 void 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
69 class DenseSparseMatrixTest : public ::testing::Test {
70 protected :
SetUp()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
TEST_F(DenseSparseMatrixTest,RightMultiply)91 TEST_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
TEST_F(DenseSparseMatrixTest,LeftMultiply)109 TEST_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
TEST_F(DenseSparseMatrixTest,ColumnNorm)138 TEST_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
TEST_F(DenseSparseMatrixTest,Scale)148 TEST_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
TEST_F(DenseSparseMatrixTest,ToDenseMatrix)158 TEST_F(DenseSparseMatrixTest, ToDenseMatrix) {
159 Matrix tsm_dense;
160 Matrix dsm_dense;
161
162 tsm->ToDenseMatrix(&tsm_dense);
163 dsm->ToDenseMatrix(&dsm_dense);
164
165 EXPECT_EQ((tsm_dense - dsm_dense).norm(), 0.0);
166 }
167
168 } // namespace internal
169 } // namespace ceres
170