blob: 0e50b627bce9987168151971e7801e00f64382b4 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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
Austin Schuh3de38b02024-06-25 18:25:10 -070046namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080047
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048static void CompareMatrices(const SparseMatrix* a, const SparseMatrix* b) {
Austin Schuh70cc9552019-01-21 19:46:48 -080049 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
Austin Schuh3de38b02024-06-25 18:25:10 -070062 a->RightMultiplyAndAccumulate(x.data(), y_a.data());
63 b->RightMultiplyAndAccumulate(x.data(), y_b.data());
Austin Schuh70cc9552019-01-21 19:46:48 -080064
65 EXPECT_EQ((y_a - y_b).norm(), 0);
66 }
67}
68
69class DenseSparseMatrixTest : public ::testing::Test {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080070 protected:
71 void SetUp() final {
Austin Schuh3de38b02024-06-25 18:25:10 -070072 std::unique_ptr<LinearLeastSquaresProblem> problem =
73 CreateLinearLeastSquaresProblemFromId(1);
Austin Schuh70cc9552019-01-21 19:46:48 -080074
75 CHECK(problem != nullptr);
76
77 tsm.reset(down_cast<TripletSparseMatrix*>(problem->A.release()));
Austin Schuh3de38b02024-06-25 18:25:10 -070078 dsm = std::make_unique<DenseSparseMatrix>(*tsm);
Austin Schuh70cc9552019-01-21 19:46:48 -080079
80 num_rows = tsm->num_rows();
81 num_cols = tsm->num_cols();
82 }
83
84 int num_rows;
85 int num_cols;
86
87 std::unique_ptr<TripletSparseMatrix> tsm;
88 std::unique_ptr<DenseSparseMatrix> dsm;
89};
90
Austin Schuh3de38b02024-06-25 18:25:10 -070091TEST_F(DenseSparseMatrixTest, RightMultiplyAndAccumulate) {
Austin Schuh70cc9552019-01-21 19:46:48 -080092 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
Austin Schuh3de38b02024-06-25 18:25:10 -0700103 tsm->RightMultiplyAndAccumulate(a.data(), b1.data());
104 dsm->RightMultiplyAndAccumulate(a.data(), b2.data());
Austin Schuh70cc9552019-01-21 19:46:48 -0800105
106 EXPECT_EQ((b1 - b2).norm(), 0);
107}
108
Austin Schuh3de38b02024-06-25 18:25:10 -0700109TEST_F(DenseSparseMatrixTest, LeftMultiplyAndAccumulate) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800110 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
Austin Schuh3de38b02024-06-25 18:25:10 -0700117 tsm->LeftMultiplyAndAccumulate(a.data(), b1.data());
118 dsm->LeftMultiplyAndAccumulate(a.data(), b2.data());
Austin Schuh70cc9552019-01-21 19:46:48 -0800119
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
Austin Schuh3de38b02024-06-25 18:25:10 -0700132 tsm->LeftMultiplyAndAccumulate(a.data(), b1.data());
133 dsm->LeftMultiplyAndAccumulate(a.data(), b2.data());
Austin Schuh70cc9552019-01-21 19:46:48 -0800134
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
158TEST_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
Austin Schuh3de38b02024-06-25 18:25:10 -0700168} // namespace ceres::internal