blob: 2fa7216caf5a989944a9a74d51b1c034e2536417 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
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 <memory>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/casts.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040#include "ceres/internal/eigen.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/linear_least_squares_problems.h"
42#include "ceres/triplet_sparse_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080043#include "glog/logging.h"
44#include "gtest/gtest.h"
45
46namespace ceres {
47namespace internal {
48
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080049static void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) {
Austin Schuh70cc9552019-01-21 19:46:48 -080050 EXPECT_EQ(a->num_rows(), b->num_rows());
51 EXPECT_EQ(a->num_cols(), b->num_cols());
52
53 int num_rows = a->num_rows();
54 int num_cols = a->num_cols();
55
56 for (int i = 0; i < num_cols; ++i) {
57 Vector x = Vector::Zero(num_cols);
58 x(i) = 1.0;
59
60 Vector y_a = Vector::Zero(num_rows);
61 Vector y_b = Vector::Zero(num_rows);
62
63 a->RightMultiply(x.data(), y_a.data());
64 b->RightMultiply(x.data(), y_b.data());
65
66 EXPECT_EQ((y_a - y_b).norm(), 0);
67 }
68}
69
70class DenseSparseMatrixTest : public ::testing::Test {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080071 protected:
72 void SetUp() final {
Austin Schuh70cc9552019-01-21 19:46:48 -080073 std::unique_ptr<LinearLeastSquaresProblem> problem(
74 CreateLinearLeastSquaresProblemFromId(1));
75
76 CHECK(problem != nullptr);
77
78 tsm.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
79 dsm.reset(new DenseSparseMatrix(*tsm));
80
81 num_rows = tsm->num_rows();
82 num_cols = tsm->num_cols();
83 }
84
85 int num_rows;
86 int num_cols;
87
88 std::unique_ptr<TripletSparseMatrix> tsm;
89 std::unique_ptr<DenseSparseMatrix> dsm;
90};
91
92TEST_F(DenseSparseMatrixTest, RightMultiply) {
93 CompareMatrices(tsm.get(), dsm.get());
94
95 // Try with a not entirely zero vector to verify column interactions, which
96 // could be masked by a subtle bug when using the elementary vectors.
97 Vector a(num_cols);
98 for (int i = 0; i < num_cols; i++) {
99 a(i) = i;
100 }
101 Vector b1 = Vector::Zero(num_rows);
102 Vector b2 = Vector::Zero(num_rows);
103
104 tsm->RightMultiply(a.data(), b1.data());
105 dsm->RightMultiply(a.data(), b2.data());
106
107 EXPECT_EQ((b1 - b2).norm(), 0);
108}
109
110TEST_F(DenseSparseMatrixTest, LeftMultiply) {
111 for (int i = 0; i < num_rows; ++i) {
112 Vector a = Vector::Zero(num_rows);
113 a(i) = 1.0;
114
115 Vector b1 = Vector::Zero(num_cols);
116 Vector b2 = Vector::Zero(num_cols);
117
118 tsm->LeftMultiply(a.data(), b1.data());
119 dsm->LeftMultiply(a.data(), b2.data());
120
121 EXPECT_EQ((b1 - b2).norm(), 0);
122 }
123
124 // Try with a not entirely zero vector to verify column interactions, which
125 // could be masked by a subtle bug when using the elementary vectors.
126 Vector a(num_rows);
127 for (int i = 0; i < num_rows; i++) {
128 a(i) = i;
129 }
130 Vector b1 = Vector::Zero(num_cols);
131 Vector b2 = Vector::Zero(num_cols);
132
133 tsm->LeftMultiply(a.data(), b1.data());
134 dsm->LeftMultiply(a.data(), b2.data());
135
136 EXPECT_EQ((b1 - b2).norm(), 0);
137}
138
139TEST_F(DenseSparseMatrixTest, ColumnNorm) {
140 Vector b1 = Vector::Zero(num_cols);
141 Vector b2 = Vector::Zero(num_cols);
142
143 tsm->SquaredColumnNorm(b1.data());
144 dsm->SquaredColumnNorm(b2.data());
145
146 EXPECT_EQ((b1 - b2).norm(), 0);
147}
148
149TEST_F(DenseSparseMatrixTest, Scale) {
150 Vector scale(num_cols);
151 for (int i = 0; i < num_cols; ++i) {
152 scale(i) = i + 1;
153 }
154 tsm->ScaleColumns(scale.data());
155 dsm->ScaleColumns(scale.data());
156 CompareMatrices(tsm.get(), dsm.get());
157}
158
159TEST_F(DenseSparseMatrixTest, ToDenseMatrix) {
160 Matrix tsm_dense;
161 Matrix dsm_dense;
162
163 tsm->ToDenseMatrix(&tsm_dense);
164 dsm->ToDenseMatrix(&dsm_dense);
165
166 EXPECT_EQ((tsm_dense - dsm_dense).norm(), 0.0);
167}
168
169} // namespace internal
170} // namespace ceres