blob: 4bb6685aa69206eb398ae2fea89888dc81fb1b51 [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: sameeragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/triplet_sparse_matrix.h"
32
33#include <algorithm>
Austin Schuh3de38b02024-06-25 18:25:10 -070034#include <memory>
35#include <random>
Austin Schuh70cc9552019-01-21 19:46:48 -080036
Austin Schuh3de38b02024-06-25 18:25:10 -070037#include "ceres/compressed_row_sparse_matrix.h"
38#include "ceres/crs_matrix.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080039#include "ceres/internal/eigen.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070040#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080041#include "ceres/types.h"
42#include "glog/logging.h"
43
Austin Schuh3de38b02024-06-25 18:25:10 -070044namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080045
46TripletSparseMatrix::TripletSparseMatrix()
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080047 : num_rows_(0), num_cols_(0), max_num_nonzeros_(0), num_nonzeros_(0) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080048
Austin Schuh3de38b02024-06-25 18:25:10 -070049TripletSparseMatrix::~TripletSparseMatrix() = default;
Austin Schuh70cc9552019-01-21 19:46:48 -080050
51TripletSparseMatrix::TripletSparseMatrix(int num_rows,
52 int num_cols,
53 int max_num_nonzeros)
54 : num_rows_(num_rows),
55 num_cols_(num_cols),
56 max_num_nonzeros_(max_num_nonzeros),
57 num_nonzeros_(0) {
58 // All the sizes should at least be zero
59 CHECK_GE(num_rows, 0);
60 CHECK_GE(num_cols, 0);
61 CHECK_GE(max_num_nonzeros, 0);
62 AllocateMemory();
63}
64
65TripletSparseMatrix::TripletSparseMatrix(const int num_rows,
66 const int num_cols,
67 const std::vector<int>& rows,
68 const std::vector<int>& cols,
69 const std::vector<double>& values)
70 : num_rows_(num_rows),
71 num_cols_(num_cols),
72 max_num_nonzeros_(values.size()),
73 num_nonzeros_(values.size()) {
74 // All the sizes should at least be zero
75 CHECK_GE(num_rows, 0);
76 CHECK_GE(num_cols, 0);
77 CHECK_EQ(rows.size(), cols.size());
78 CHECK_EQ(rows.size(), values.size());
79 AllocateMemory();
80 std::copy(rows.begin(), rows.end(), rows_.get());
81 std::copy(cols.begin(), cols.end(), cols_.get());
82 std::copy(values.begin(), values.end(), values_.get());
83}
84
85TripletSparseMatrix::TripletSparseMatrix(const TripletSparseMatrix& orig)
86 : SparseMatrix(),
87 num_rows_(orig.num_rows_),
88 num_cols_(orig.num_cols_),
89 max_num_nonzeros_(orig.max_num_nonzeros_),
90 num_nonzeros_(orig.num_nonzeros_) {
91 AllocateMemory();
92 CopyData(orig);
93}
94
95TripletSparseMatrix& TripletSparseMatrix::operator=(
96 const TripletSparseMatrix& rhs) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080097 if (this == &rhs) {
98 return *this;
99 }
Austin Schuh70cc9552019-01-21 19:46:48 -0800100 num_rows_ = rhs.num_rows_;
101 num_cols_ = rhs.num_cols_;
102 num_nonzeros_ = rhs.num_nonzeros_;
103 max_num_nonzeros_ = rhs.max_num_nonzeros_;
104 AllocateMemory();
105 CopyData(rhs);
106 return *this;
107}
108
109bool TripletSparseMatrix::AllTripletsWithinBounds() const {
110 for (int i = 0; i < num_nonzeros_; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800111 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800112 if ((rows_[i] < 0) || (rows_[i] >= num_rows_) ||
Austin Schuh3de38b02024-06-25 18:25:10 -0700113 (cols_[i] < 0) || (cols_[i] >= num_cols_)) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800114 return false;
Austin Schuh3de38b02024-06-25 18:25:10 -0700115 }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800116 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800117 }
118 return true;
119}
120
121void TripletSparseMatrix::Reserve(int new_max_num_nonzeros) {
122 CHECK_LE(num_nonzeros_, new_max_num_nonzeros)
123 << "Reallocation will cause data loss";
124
125 // Nothing to do if we have enough space already.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800126 if (new_max_num_nonzeros <= max_num_nonzeros_) return;
Austin Schuh70cc9552019-01-21 19:46:48 -0800127
Austin Schuh3de38b02024-06-25 18:25:10 -0700128 std::unique_ptr<int[]> new_rows =
129 std::make_unique<int[]>(new_max_num_nonzeros);
130 std::unique_ptr<int[]> new_cols =
131 std::make_unique<int[]>(new_max_num_nonzeros);
132 std::unique_ptr<double[]> new_values =
133 std::make_unique<double[]>(new_max_num_nonzeros);
Austin Schuh70cc9552019-01-21 19:46:48 -0800134
135 for (int i = 0; i < num_nonzeros_; ++i) {
136 new_rows[i] = rows_[i];
137 new_cols[i] = cols_[i];
138 new_values[i] = values_[i];
139 }
140
Austin Schuh3de38b02024-06-25 18:25:10 -0700141 rows_ = std::move(new_rows);
142 cols_ = std::move(new_cols);
143 values_ = std::move(new_values);
Austin Schuh70cc9552019-01-21 19:46:48 -0800144 max_num_nonzeros_ = new_max_num_nonzeros;
145}
146
147void TripletSparseMatrix::SetZero() {
148 std::fill(values_.get(), values_.get() + max_num_nonzeros_, 0.0);
149 num_nonzeros_ = 0;
150}
151
152void TripletSparseMatrix::set_num_nonzeros(int num_nonzeros) {
153 CHECK_GE(num_nonzeros, 0);
154 CHECK_LE(num_nonzeros, max_num_nonzeros_);
155 num_nonzeros_ = num_nonzeros;
156}
157
158void TripletSparseMatrix::AllocateMemory() {
Austin Schuh3de38b02024-06-25 18:25:10 -0700159 rows_ = std::make_unique<int[]>(max_num_nonzeros_);
160 cols_ = std::make_unique<int[]>(max_num_nonzeros_);
161 values_ = std::make_unique<double[]>(max_num_nonzeros_);
Austin Schuh70cc9552019-01-21 19:46:48 -0800162}
163
164void TripletSparseMatrix::CopyData(const TripletSparseMatrix& orig) {
165 for (int i = 0; i < num_nonzeros_; ++i) {
166 rows_[i] = orig.rows_[i];
167 cols_[i] = orig.cols_[i];
168 values_[i] = orig.values_[i];
169 }
170}
171
Austin Schuh3de38b02024-06-25 18:25:10 -0700172void TripletSparseMatrix::RightMultiplyAndAccumulate(const double* x,
173 double* y) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800174 for (int i = 0; i < num_nonzeros_; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800175 y[rows_[i]] += values_[i] * x[cols_[i]];
Austin Schuh70cc9552019-01-21 19:46:48 -0800176 }
177}
178
Austin Schuh3de38b02024-06-25 18:25:10 -0700179void TripletSparseMatrix::LeftMultiplyAndAccumulate(const double* x,
180 double* y) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800181 for (int i = 0; i < num_nonzeros_; ++i) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800182 y[cols_[i]] += values_[i] * x[rows_[i]];
Austin Schuh70cc9552019-01-21 19:46:48 -0800183 }
184}
185
186void TripletSparseMatrix::SquaredColumnNorm(double* x) const {
187 CHECK(x != nullptr);
188 VectorRef(x, num_cols_).setZero();
189 for (int i = 0; i < num_nonzeros_; ++i) {
190 x[cols_[i]] += values_[i] * values_[i];
191 }
192}
193
194void TripletSparseMatrix::ScaleColumns(const double* scale) {
195 CHECK(scale != nullptr);
196 for (int i = 0; i < num_nonzeros_; ++i) {
197 values_[i] = values_[i] * scale[cols_[i]];
198 }
199}
200
Austin Schuh3de38b02024-06-25 18:25:10 -0700201void TripletSparseMatrix::ToCRSMatrix(CRSMatrix* crs_matrix) const {
202 CompressedRowSparseMatrix::FromTripletSparseMatrix(*this)->ToCRSMatrix(
203 crs_matrix);
204}
205
Austin Schuh70cc9552019-01-21 19:46:48 -0800206void TripletSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
207 dense_matrix->resize(num_rows_, num_cols_);
208 dense_matrix->setZero();
209 Matrix& m = *dense_matrix;
210 for (int i = 0; i < num_nonzeros_; ++i) {
211 m(rows_[i], cols_[i]) += values_[i];
212 }
213}
214
215void TripletSparseMatrix::AppendRows(const TripletSparseMatrix& B) {
216 CHECK_EQ(B.num_cols(), num_cols_);
217 Reserve(num_nonzeros_ + B.num_nonzeros_);
218 for (int i = 0; i < B.num_nonzeros_; ++i) {
219 rows_.get()[num_nonzeros_] = B.rows()[i] + num_rows_;
220 cols_.get()[num_nonzeros_] = B.cols()[i];
221 values_.get()[num_nonzeros_++] = B.values()[i];
222 }
223 num_rows_ = num_rows_ + B.num_rows();
224}
225
226void TripletSparseMatrix::AppendCols(const TripletSparseMatrix& B) {
227 CHECK_EQ(B.num_rows(), num_rows_);
228 Reserve(num_nonzeros_ + B.num_nonzeros_);
229 for (int i = 0; i < B.num_nonzeros_; ++i, ++num_nonzeros_) {
230 rows_.get()[num_nonzeros_] = B.rows()[i];
231 cols_.get()[num_nonzeros_] = B.cols()[i] + num_cols_;
232 values_.get()[num_nonzeros_] = B.values()[i];
233 }
234 num_cols_ = num_cols_ + B.num_cols();
235}
236
Austin Schuh70cc9552019-01-21 19:46:48 -0800237void TripletSparseMatrix::Resize(int new_num_rows, int new_num_cols) {
238 if ((new_num_rows >= num_rows_) && (new_num_cols >= num_cols_)) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800239 num_rows_ = new_num_rows;
Austin Schuh70cc9552019-01-21 19:46:48 -0800240 num_cols_ = new_num_cols;
241 return;
242 }
243
244 num_rows_ = new_num_rows;
245 num_cols_ = new_num_cols;
246
247 int* r_ptr = rows_.get();
248 int* c_ptr = cols_.get();
249 double* v_ptr = values_.get();
250
251 int dropped_terms = 0;
252 for (int i = 0; i < num_nonzeros_; ++i) {
253 if ((r_ptr[i] < num_rows_) && (c_ptr[i] < num_cols_)) {
254 if (dropped_terms) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800255 r_ptr[i - dropped_terms] = r_ptr[i];
256 c_ptr[i - dropped_terms] = c_ptr[i];
257 v_ptr[i - dropped_terms] = v_ptr[i];
Austin Schuh70cc9552019-01-21 19:46:48 -0800258 }
259 } else {
260 ++dropped_terms;
261 }
262 }
263 num_nonzeros_ -= dropped_terms;
264}
265
Austin Schuh3de38b02024-06-25 18:25:10 -0700266std::unique_ptr<TripletSparseMatrix>
267TripletSparseMatrix::CreateSparseDiagonalMatrix(const double* values,
268 int num_rows) {
269 std::unique_ptr<TripletSparseMatrix> m =
270 std::make_unique<TripletSparseMatrix>(num_rows, num_rows, num_rows);
Austin Schuh70cc9552019-01-21 19:46:48 -0800271 for (int i = 0; i < num_rows; ++i) {
272 m->mutable_rows()[i] = i;
273 m->mutable_cols()[i] = i;
274 m->mutable_values()[i] = values[i];
275 }
276 m->set_num_nonzeros(num_rows);
277 return m;
278}
279
280void TripletSparseMatrix::ToTextFile(FILE* file) const {
281 CHECK(file != nullptr);
282 for (int i = 0; i < num_nonzeros_; ++i) {
283 fprintf(file, "% 10d % 10d %17f\n", rows_[i], cols_[i], values_[i]);
284 }
285}
286
Austin Schuh3de38b02024-06-25 18:25:10 -0700287std::unique_ptr<TripletSparseMatrix> TripletSparseMatrix::CreateFromTextFile(
288 FILE* file) {
289 CHECK(file != nullptr);
290 int num_rows = 0;
291 int num_cols = 0;
292 std::vector<int> rows;
293 std::vector<int> cols;
294 std::vector<double> values;
295 while (true) {
296 int row, col;
297 double value;
298 if (fscanf(file, "%d %d %lf", &row, &col, &value) != 3) {
299 break;
300 }
301 rows.push_back(row);
302 cols.push_back(col);
303 values.push_back(value);
304 num_rows = std::max(num_rows, row + 1);
305 num_cols = std::max(num_cols, col + 1);
306 }
307 VLOG(1) << "Read " << rows.size() << " nonzeros from file.";
308 return std::make_unique<TripletSparseMatrix>(
309 num_rows, num_cols, rows, cols, values);
310}
311
312std::unique_ptr<TripletSparseMatrix> TripletSparseMatrix::CreateRandomMatrix(
313 const TripletSparseMatrix::RandomMatrixOptions& options,
314 std::mt19937& prng) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800315 CHECK_GT(options.num_rows, 0);
316 CHECK_GT(options.num_cols, 0);
317 CHECK_GT(options.density, 0.0);
318 CHECK_LE(options.density, 1.0);
319
320 std::vector<int> rows;
321 std::vector<int> cols;
322 std::vector<double> values;
Austin Schuh3de38b02024-06-25 18:25:10 -0700323 std::uniform_real_distribution<double> uniform01(0.0, 1.0);
324 std::normal_distribution<double> standard_normal;
Austin Schuh70cc9552019-01-21 19:46:48 -0800325 while (rows.empty()) {
326 rows.clear();
327 cols.clear();
328 values.clear();
329 for (int r = 0; r < options.num_rows; ++r) {
330 for (int c = 0; c < options.num_cols; ++c) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700331 if (uniform01(prng) <= options.density) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800332 rows.push_back(r);
333 cols.push_back(c);
Austin Schuh3de38b02024-06-25 18:25:10 -0700334 values.push_back(standard_normal(prng));
Austin Schuh70cc9552019-01-21 19:46:48 -0800335 }
336 }
337 }
338 }
339
Austin Schuh3de38b02024-06-25 18:25:10 -0700340 return std::make_unique<TripletSparseMatrix>(
Austin Schuh70cc9552019-01-21 19:46:48 -0800341 options.num_rows, options.num_cols, rows, cols, values);
342}
343
Austin Schuh3de38b02024-06-25 18:25:10 -0700344} // namespace ceres::internal