blob: e8bff2bf62dadf9ccb93c0bc42c8536e94c989e6 [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Sameer Agarwal31730ef2013-02-28 11:20:28 -08002// Copyright 2010, 2011, 2012, 2013 Google Inc. All rights reserved.
Keir Mierle8ebb0732012-04-30 23:09:08 -07003// 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
Keir Mierle8ebb0732012-04-30 23:09:08 -070037#include "ceres/casts.h"
38#include "ceres/linear_least_squares_problems.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070039#include "ceres/triplet_sparse_matrix.h"
40#include "ceres/internal/eigen.h"
41#include "ceres/internal/scoped_ptr.h"
Sameer Agarwala1eaa262013-05-09 10:02:24 -070042#include "glog/logging.h"
43#include "gtest/gtest.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070044
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
Keir Mierle8ebb0732012-04-30 23:09:08 -0700158TEST_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
Keir Mierle8ebb0732012-04-30 23:09:08 -0700168} // namespace internal
169} // namespace ceres