blob: 15b96749de14f9e45e73f8661bdbb490e3ed558d [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: sameeragarwal@google.com (Sameer Agarwal)
30
31#include "ceres/triplet_sparse_matrix.h"
32
33#include <algorithm>
34#include <cstddef>
35
36#include "ceres/internal/eigen.h"
37#include "ceres/internal/port.h"
38#include "ceres/random.h"
39#include "ceres/types.h"
40#include "glog/logging.h"
41
42namespace ceres {
43namespace internal {
44
45TripletSparseMatrix::TripletSparseMatrix()
46 : num_rows_(0),
47 num_cols_(0),
48 max_num_nonzeros_(0),
49 num_nonzeros_(0) {}
50
51
52TripletSparseMatrix::~TripletSparseMatrix() {}
53
54TripletSparseMatrix::TripletSparseMatrix(int num_rows,
55 int num_cols,
56 int max_num_nonzeros)
57 : num_rows_(num_rows),
58 num_cols_(num_cols),
59 max_num_nonzeros_(max_num_nonzeros),
60 num_nonzeros_(0) {
61 // All the sizes should at least be zero
62 CHECK_GE(num_rows, 0);
63 CHECK_GE(num_cols, 0);
64 CHECK_GE(max_num_nonzeros, 0);
65 AllocateMemory();
66}
67
68TripletSparseMatrix::TripletSparseMatrix(const int num_rows,
69 const int num_cols,
70 const std::vector<int>& rows,
71 const std::vector<int>& cols,
72 const std::vector<double>& values)
73 : num_rows_(num_rows),
74 num_cols_(num_cols),
75 max_num_nonzeros_(values.size()),
76 num_nonzeros_(values.size()) {
77 // All the sizes should at least be zero
78 CHECK_GE(num_rows, 0);
79 CHECK_GE(num_cols, 0);
80 CHECK_EQ(rows.size(), cols.size());
81 CHECK_EQ(rows.size(), values.size());
82 AllocateMemory();
83 std::copy(rows.begin(), rows.end(), rows_.get());
84 std::copy(cols.begin(), cols.end(), cols_.get());
85 std::copy(values.begin(), values.end(), values_.get());
86}
87
88TripletSparseMatrix::TripletSparseMatrix(const TripletSparseMatrix& orig)
89 : SparseMatrix(),
90 num_rows_(orig.num_rows_),
91 num_cols_(orig.num_cols_),
92 max_num_nonzeros_(orig.max_num_nonzeros_),
93 num_nonzeros_(orig.num_nonzeros_) {
94 AllocateMemory();
95 CopyData(orig);
96}
97
98TripletSparseMatrix& TripletSparseMatrix::operator=(
99 const TripletSparseMatrix& rhs) {
100 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) {
111 if ((rows_[i] < 0) || (rows_[i] >= num_rows_) ||
112 (cols_[i] < 0) || (cols_[i] >= num_cols_))
113 return false;
114 }
115 return true;
116}
117
118void TripletSparseMatrix::Reserve(int new_max_num_nonzeros) {
119 CHECK_LE(num_nonzeros_, new_max_num_nonzeros)
120 << "Reallocation will cause data loss";
121
122 // Nothing to do if we have enough space already.
123 if (new_max_num_nonzeros <= max_num_nonzeros_)
124 return;
125
126 int* new_rows = new int[new_max_num_nonzeros];
127 int* new_cols = new int[new_max_num_nonzeros];
128 double* new_values = new double[new_max_num_nonzeros];
129
130 for (int i = 0; i < num_nonzeros_; ++i) {
131 new_rows[i] = rows_[i];
132 new_cols[i] = cols_[i];
133 new_values[i] = values_[i];
134 }
135
136 rows_.reset(new_rows);
137 cols_.reset(new_cols);
138 values_.reset(new_values);
139
140 max_num_nonzeros_ = new_max_num_nonzeros;
141}
142
143void TripletSparseMatrix::SetZero() {
144 std::fill(values_.get(), values_.get() + max_num_nonzeros_, 0.0);
145 num_nonzeros_ = 0;
146}
147
148void TripletSparseMatrix::set_num_nonzeros(int num_nonzeros) {
149 CHECK_GE(num_nonzeros, 0);
150 CHECK_LE(num_nonzeros, max_num_nonzeros_);
151 num_nonzeros_ = num_nonzeros;
152}
153
154void TripletSparseMatrix::AllocateMemory() {
155 rows_.reset(new int[max_num_nonzeros_]);
156 cols_.reset(new int[max_num_nonzeros_]);
157 values_.reset(new double[max_num_nonzeros_]);
158}
159
160void TripletSparseMatrix::CopyData(const TripletSparseMatrix& orig) {
161 for (int i = 0; i < num_nonzeros_; ++i) {
162 rows_[i] = orig.rows_[i];
163 cols_[i] = orig.cols_[i];
164 values_[i] = orig.values_[i];
165 }
166}
167
168void TripletSparseMatrix::RightMultiply(const double* x, double* y) const {
169 for (int i = 0; i < num_nonzeros_; ++i) {
170 y[rows_[i]] += values_[i]*x[cols_[i]];
171 }
172}
173
174void TripletSparseMatrix::LeftMultiply(const double* x, double* y) const {
175 for (int i = 0; i < num_nonzeros_; ++i) {
176 y[cols_[i]] += values_[i]*x[rows_[i]];
177 }
178}
179
180void TripletSparseMatrix::SquaredColumnNorm(double* x) const {
181 CHECK(x != nullptr);
182 VectorRef(x, num_cols_).setZero();
183 for (int i = 0; i < num_nonzeros_; ++i) {
184 x[cols_[i]] += values_[i] * values_[i];
185 }
186}
187
188void TripletSparseMatrix::ScaleColumns(const double* scale) {
189 CHECK(scale != nullptr);
190 for (int i = 0; i < num_nonzeros_; ++i) {
191 values_[i] = values_[i] * scale[cols_[i]];
192 }
193}
194
195void TripletSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
196 dense_matrix->resize(num_rows_, num_cols_);
197 dense_matrix->setZero();
198 Matrix& m = *dense_matrix;
199 for (int i = 0; i < num_nonzeros_; ++i) {
200 m(rows_[i], cols_[i]) += values_[i];
201 }
202}
203
204void TripletSparseMatrix::AppendRows(const TripletSparseMatrix& B) {
205 CHECK_EQ(B.num_cols(), num_cols_);
206 Reserve(num_nonzeros_ + B.num_nonzeros_);
207 for (int i = 0; i < B.num_nonzeros_; ++i) {
208 rows_.get()[num_nonzeros_] = B.rows()[i] + num_rows_;
209 cols_.get()[num_nonzeros_] = B.cols()[i];
210 values_.get()[num_nonzeros_++] = B.values()[i];
211 }
212 num_rows_ = num_rows_ + B.num_rows();
213}
214
215void TripletSparseMatrix::AppendCols(const TripletSparseMatrix& B) {
216 CHECK_EQ(B.num_rows(), num_rows_);
217 Reserve(num_nonzeros_ + B.num_nonzeros_);
218 for (int i = 0; i < B.num_nonzeros_; ++i, ++num_nonzeros_) {
219 rows_.get()[num_nonzeros_] = B.rows()[i];
220 cols_.get()[num_nonzeros_] = B.cols()[i] + num_cols_;
221 values_.get()[num_nonzeros_] = B.values()[i];
222 }
223 num_cols_ = num_cols_ + B.num_cols();
224}
225
226
227void TripletSparseMatrix::Resize(int new_num_rows, int new_num_cols) {
228 if ((new_num_rows >= num_rows_) && (new_num_cols >= num_cols_)) {
229 num_rows_ = new_num_rows;
230 num_cols_ = new_num_cols;
231 return;
232 }
233
234 num_rows_ = new_num_rows;
235 num_cols_ = new_num_cols;
236
237 int* r_ptr = rows_.get();
238 int* c_ptr = cols_.get();
239 double* v_ptr = values_.get();
240
241 int dropped_terms = 0;
242 for (int i = 0; i < num_nonzeros_; ++i) {
243 if ((r_ptr[i] < num_rows_) && (c_ptr[i] < num_cols_)) {
244 if (dropped_terms) {
245 r_ptr[i-dropped_terms] = r_ptr[i];
246 c_ptr[i-dropped_terms] = c_ptr[i];
247 v_ptr[i-dropped_terms] = v_ptr[i];
248 }
249 } else {
250 ++dropped_terms;
251 }
252 }
253 num_nonzeros_ -= dropped_terms;
254}
255
256TripletSparseMatrix* TripletSparseMatrix::CreateSparseDiagonalMatrix(
257 const double* values, int num_rows) {
258 TripletSparseMatrix* m =
259 new TripletSparseMatrix(num_rows, num_rows, num_rows);
260 for (int i = 0; i < num_rows; ++i) {
261 m->mutable_rows()[i] = i;
262 m->mutable_cols()[i] = i;
263 m->mutable_values()[i] = values[i];
264 }
265 m->set_num_nonzeros(num_rows);
266 return m;
267}
268
269void TripletSparseMatrix::ToTextFile(FILE* file) const {
270 CHECK(file != nullptr);
271 for (int i = 0; i < num_nonzeros_; ++i) {
272 fprintf(file, "% 10d % 10d %17f\n", rows_[i], cols_[i], values_[i]);
273 }
274}
275
276TripletSparseMatrix* TripletSparseMatrix::CreateRandomMatrix(
277 const TripletSparseMatrix::RandomMatrixOptions& options) {
278 CHECK_GT(options.num_rows, 0);
279 CHECK_GT(options.num_cols, 0);
280 CHECK_GT(options.density, 0.0);
281 CHECK_LE(options.density, 1.0);
282
283 std::vector<int> rows;
284 std::vector<int> cols;
285 std::vector<double> values;
286 while (rows.empty()) {
287 rows.clear();
288 cols.clear();
289 values.clear();
290 for (int r = 0; r < options.num_rows; ++r) {
291 for (int c = 0; c < options.num_cols; ++c) {
292 if (RandDouble() <= options.density) {
293 rows.push_back(r);
294 cols.push_back(c);
295 values.push_back(RandNormal());
296 }
297 }
298 }
299 }
300
301 return new TripletSparseMatrix(
302 options.num_rows, options.num_cols, rows, cols, values);
303}
304
305} // namespace internal
306} // namespace ceres