• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // 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: sameeragarwal@google.com (Sameer Agarwal)
30 
31 #include "ceres/compressed_row_sparse_matrix.h"
32 
33 #include "ceres/casts.h"
34 #include "ceres/crs_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/matrix_proto.h"
39 #include "ceres/triplet_sparse_matrix.h"
40 #include "gtest/gtest.h"
41 
42 namespace ceres {
43 namespace internal {
44 
CompareMatrices(const SparseMatrix * a,const SparseMatrix * b)45 void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) {
46   EXPECT_EQ(a->num_rows(), b->num_rows());
47   EXPECT_EQ(a->num_cols(), b->num_cols());
48 
49   int num_rows = a->num_rows();
50   int num_cols = a->num_cols();
51 
52   for (int i = 0; i < num_cols; ++i) {
53     Vector x = Vector::Zero(num_cols);
54     x(i) = 1.0;
55 
56     Vector y_a = Vector::Zero(num_rows);
57     Vector y_b = Vector::Zero(num_rows);
58 
59     a->RightMultiply(x.data(), y_a.data());
60     b->RightMultiply(x.data(), y_b.data());
61 
62     EXPECT_EQ((y_a - y_b).norm(), 0);
63   }
64 }
65 
66 class CompressedRowSparseMatrixTest : public ::testing::Test {
67  protected :
SetUp()68   virtual void SetUp() {
69     scoped_ptr<LinearLeastSquaresProblem> problem(
70         CreateLinearLeastSquaresProblemFromId(1));
71 
72     CHECK_NOTNULL(problem.get());
73 
74     tsm.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
75     crsm.reset(new CompressedRowSparseMatrix(*tsm));
76 
77     num_rows = tsm->num_rows();
78     num_cols = tsm->num_cols();
79   }
80 
81   int num_rows;
82   int num_cols;
83 
84   scoped_ptr<TripletSparseMatrix> tsm;
85   scoped_ptr<CompressedRowSparseMatrix> crsm;
86 };
87 
TEST_F(CompressedRowSparseMatrixTest,RightMultiply)88 TEST_F(CompressedRowSparseMatrixTest, RightMultiply) {
89   CompareMatrices(tsm.get(), crsm.get());
90 }
91 
TEST_F(CompressedRowSparseMatrixTest,LeftMultiply)92 TEST_F(CompressedRowSparseMatrixTest, LeftMultiply) {
93   for (int i = 0; i < num_rows; ++i) {
94     Vector a = Vector::Zero(num_rows);
95     a(i) = 1.0;
96 
97     Vector b1 = Vector::Zero(num_cols);
98     Vector b2 = Vector::Zero(num_cols);
99 
100     tsm->LeftMultiply(a.data(), b1.data());
101     crsm->LeftMultiply(a.data(), b2.data());
102 
103     EXPECT_EQ((b1 - b2).norm(), 0);
104   }
105 }
106 
TEST_F(CompressedRowSparseMatrixTest,ColumnNorm)107 TEST_F(CompressedRowSparseMatrixTest, ColumnNorm) {
108   Vector b1 = Vector::Zero(num_cols);
109   Vector b2 = Vector::Zero(num_cols);
110 
111   tsm->SquaredColumnNorm(b1.data());
112   crsm->SquaredColumnNorm(b2.data());
113 
114   EXPECT_EQ((b1 - b2).norm(), 0);
115 }
116 
TEST_F(CompressedRowSparseMatrixTest,Scale)117 TEST_F(CompressedRowSparseMatrixTest, Scale) {
118   Vector scale(num_cols);
119   for (int i = 0; i < num_cols; ++i) {
120     scale(i) = i + 1;
121   }
122 
123   tsm->ScaleColumns(scale.data());
124   crsm->ScaleColumns(scale.data());
125   CompareMatrices(tsm.get(), crsm.get());
126 }
127 
TEST_F(CompressedRowSparseMatrixTest,DeleteRows)128 TEST_F(CompressedRowSparseMatrixTest, DeleteRows) {
129   for (int i = 0; i < num_rows; ++i) {
130     tsm->Resize(num_rows - i, num_cols);
131     crsm->DeleteRows(crsm->num_rows() - tsm->num_rows());
132     CompareMatrices(tsm.get(), crsm.get());
133   }
134 }
135 
TEST_F(CompressedRowSparseMatrixTest,AppendRows)136 TEST_F(CompressedRowSparseMatrixTest, AppendRows) {
137   for (int i = 0; i < num_rows; ++i) {
138     TripletSparseMatrix tsm_appendage(*tsm);
139     tsm_appendage.Resize(i, num_cols);
140 
141     tsm->AppendRows(tsm_appendage);
142     CompressedRowSparseMatrix crsm_appendage(tsm_appendage);
143     crsm->AppendRows(crsm_appendage);
144 
145     CompareMatrices(tsm.get(), crsm.get());
146   }
147 }
148 
149 #ifndef CERES_NO_PROTOCOL_BUFFERS
TEST_F(CompressedRowSparseMatrixTest,Serialization)150 TEST_F(CompressedRowSparseMatrixTest, Serialization) {
151   SparseMatrixProto proto;
152   crsm->ToProto(&proto);
153 
154   CompressedRowSparseMatrix n(proto);
155   ASSERT_EQ(n.num_rows(), crsm->num_rows());
156   ASSERT_EQ(n.num_cols(), crsm->num_cols());
157   ASSERT_EQ(n.num_nonzeros(), crsm->num_nonzeros());
158 
159   for (int i = 0; i < n.num_rows() + 1; ++i) {
160     ASSERT_EQ(crsm->rows()[i], proto.compressed_row_matrix().rows(i));
161     ASSERT_EQ(crsm->rows()[i], n.rows()[i]);
162   }
163 
164   for (int i = 0; i < crsm->num_nonzeros(); ++i) {
165     ASSERT_EQ(crsm->cols()[i], proto.compressed_row_matrix().cols(i));
166     ASSERT_EQ(crsm->cols()[i], n.cols()[i]);
167     ASSERT_EQ(crsm->values()[i], proto.compressed_row_matrix().values(i));
168     ASSERT_EQ(crsm->values()[i], n.values()[i]);
169   }
170 }
171 #endif
172 
TEST_F(CompressedRowSparseMatrixTest,ToDenseMatrix)173 TEST_F(CompressedRowSparseMatrixTest, ToDenseMatrix) {
174   Matrix tsm_dense;
175   Matrix crsm_dense;
176 
177   tsm->ToDenseMatrix(&tsm_dense);
178   crsm->ToDenseMatrix(&crsm_dense);
179 
180   EXPECT_EQ((tsm_dense - crsm_dense).norm(), 0.0);
181 }
182 
TEST_F(CompressedRowSparseMatrixTest,ToCRSMatrix)183 TEST_F(CompressedRowSparseMatrixTest, ToCRSMatrix) {
184   CRSMatrix crs_matrix;
185   crsm->ToCRSMatrix(&crs_matrix);
186   EXPECT_EQ(crsm->num_rows(), crs_matrix.num_rows);
187   EXPECT_EQ(crsm->num_cols(), crs_matrix.num_cols);
188   EXPECT_EQ(crsm->num_rows() + 1, crs_matrix.rows.size());
189   EXPECT_EQ(crsm->num_nonzeros(), crs_matrix.cols.size());
190   EXPECT_EQ(crsm->num_nonzeros(), crs_matrix.values.size());
191 
192   for (int i = 0; i < crsm->num_rows() + 1; ++i) {
193     EXPECT_EQ(crsm->rows()[i], crs_matrix.rows[i]);
194   }
195 
196   for (int i = 0; i < crsm->num_nonzeros(); ++i) {
197     EXPECT_EQ(crsm->cols()[i], crs_matrix.cols[i]);
198     EXPECT_EQ(crsm->values()[i], crs_matrix.values[i]);
199   }
200 }
201 
202 }  // namespace internal
203 }  // namespace ceres
204