blob: 8f50f3561e2e063dfe04b9171bdfe441131f33f0 [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/block_sparse_matrix.h"
32
33#include <cstddef>
34#include <algorithm>
35#include <vector>
36#include "ceres/block_structure.h"
37#include "ceres/internal/eigen.h"
38#include "ceres/random.h"
39#include "ceres/small_blas.h"
40#include "ceres/triplet_sparse_matrix.h"
41#include "glog/logging.h"
42
43namespace ceres {
44namespace internal {
45
46using std::vector;
47
48BlockSparseMatrix::~BlockSparseMatrix() {}
49
50BlockSparseMatrix::BlockSparseMatrix(
51 CompressedRowBlockStructure* block_structure)
52 : num_rows_(0),
53 num_cols_(0),
54 num_nonzeros_(0),
55 block_structure_(block_structure) {
56 CHECK(block_structure_ != nullptr);
57
58 // Count the number of columns in the matrix.
59 for (int i = 0; i < block_structure_->cols.size(); ++i) {
60 num_cols_ += block_structure_->cols[i].size;
61 }
62
63 // Count the number of non-zero entries and the number of rows in
64 // the matrix.
65 for (int i = 0; i < block_structure_->rows.size(); ++i) {
66 int row_block_size = block_structure_->rows[i].block.size;
67 num_rows_ += row_block_size;
68
69 const vector<Cell>& cells = block_structure_->rows[i].cells;
70 for (int j = 0; j < cells.size(); ++j) {
71 int col_block_id = cells[j].block_id;
72 int col_block_size = block_structure_->cols[col_block_id].size;
73 num_nonzeros_ += col_block_size * row_block_size;
74 }
75 }
76
77 CHECK_GE(num_rows_, 0);
78 CHECK_GE(num_cols_, 0);
79 CHECK_GE(num_nonzeros_, 0);
80 VLOG(2) << "Allocating values array with "
81 << num_nonzeros_ * sizeof(double) << " bytes."; // NOLINT
82 values_.reset(new double[num_nonzeros_]);
83 max_num_nonzeros_ = num_nonzeros_;
84 CHECK(values_ != nullptr);
85}
86
87void BlockSparseMatrix::SetZero() {
88 std::fill(values_.get(), values_.get() + num_nonzeros_, 0.0);
89}
90
91void BlockSparseMatrix::RightMultiply(const double* x, double* y) const {
92 CHECK(x != nullptr);
93 CHECK(y != nullptr);
94
95 for (int i = 0; i < block_structure_->rows.size(); ++i) {
96 int row_block_pos = block_structure_->rows[i].block.position;
97 int row_block_size = block_structure_->rows[i].block.size;
98 const vector<Cell>& cells = block_structure_->rows[i].cells;
99 for (int j = 0; j < cells.size(); ++j) {
100 int col_block_id = cells[j].block_id;
101 int col_block_size = block_structure_->cols[col_block_id].size;
102 int col_block_pos = block_structure_->cols[col_block_id].position;
103 MatrixVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
104 values_.get() + cells[j].position, row_block_size, col_block_size,
105 x + col_block_pos,
106 y + row_block_pos);
107 }
108 }
109}
110
111void BlockSparseMatrix::LeftMultiply(const double* x, double* y) const {
112 CHECK(x != nullptr);
113 CHECK(y != nullptr);
114
115 for (int i = 0; i < block_structure_->rows.size(); ++i) {
116 int row_block_pos = block_structure_->rows[i].block.position;
117 int row_block_size = block_structure_->rows[i].block.size;
118 const vector<Cell>& cells = block_structure_->rows[i].cells;
119 for (int j = 0; j < cells.size(); ++j) {
120 int col_block_id = cells[j].block_id;
121 int col_block_size = block_structure_->cols[col_block_id].size;
122 int col_block_pos = block_structure_->cols[col_block_id].position;
123 MatrixTransposeVectorMultiply<Eigen::Dynamic, Eigen::Dynamic, 1>(
124 values_.get() + cells[j].position, row_block_size, col_block_size,
125 x + row_block_pos,
126 y + col_block_pos);
127 }
128 }
129}
130
131void BlockSparseMatrix::SquaredColumnNorm(double* x) const {
132 CHECK(x != nullptr);
133 VectorRef(x, num_cols_).setZero();
134 for (int i = 0; i < block_structure_->rows.size(); ++i) {
135 int row_block_size = block_structure_->rows[i].block.size;
136 const vector<Cell>& cells = block_structure_->rows[i].cells;
137 for (int j = 0; j < cells.size(); ++j) {
138 int col_block_id = cells[j].block_id;
139 int col_block_size = block_structure_->cols[col_block_id].size;
140 int col_block_pos = block_structure_->cols[col_block_id].position;
141 const MatrixRef m(values_.get() + cells[j].position,
142 row_block_size, col_block_size);
143 VectorRef(x + col_block_pos, col_block_size) += m.colwise().squaredNorm();
144 }
145 }
146}
147
148void BlockSparseMatrix::ScaleColumns(const double* scale) {
149 CHECK(scale != nullptr);
150
151 for (int i = 0; i < block_structure_->rows.size(); ++i) {
152 int row_block_size = block_structure_->rows[i].block.size;
153 const vector<Cell>& cells = block_structure_->rows[i].cells;
154 for (int j = 0; j < cells.size(); ++j) {
155 int col_block_id = cells[j].block_id;
156 int col_block_size = block_structure_->cols[col_block_id].size;
157 int col_block_pos = block_structure_->cols[col_block_id].position;
158 MatrixRef m(values_.get() + cells[j].position,
159 row_block_size, col_block_size);
160 m *= ConstVectorRef(scale + col_block_pos, col_block_size).asDiagonal();
161 }
162 }
163}
164
165void BlockSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
166 CHECK(dense_matrix != nullptr);
167
168 dense_matrix->resize(num_rows_, num_cols_);
169 dense_matrix->setZero();
170 Matrix& m = *dense_matrix;
171
172 for (int i = 0; i < block_structure_->rows.size(); ++i) {
173 int row_block_pos = block_structure_->rows[i].block.position;
174 int row_block_size = block_structure_->rows[i].block.size;
175 const vector<Cell>& cells = block_structure_->rows[i].cells;
176 for (int j = 0; j < cells.size(); ++j) {
177 int col_block_id = cells[j].block_id;
178 int col_block_size = block_structure_->cols[col_block_id].size;
179 int col_block_pos = block_structure_->cols[col_block_id].position;
180 int jac_pos = cells[j].position;
181 m.block(row_block_pos, col_block_pos, row_block_size, col_block_size)
182 += MatrixRef(values_.get() + jac_pos, row_block_size, col_block_size);
183 }
184 }
185}
186
187void BlockSparseMatrix::ToTripletSparseMatrix(
188 TripletSparseMatrix* matrix) const {
189 CHECK(matrix != nullptr);
190
191 matrix->Reserve(num_nonzeros_);
192 matrix->Resize(num_rows_, num_cols_);
193 matrix->SetZero();
194
195 for (int i = 0; i < block_structure_->rows.size(); ++i) {
196 int row_block_pos = block_structure_->rows[i].block.position;
197 int row_block_size = block_structure_->rows[i].block.size;
198 const vector<Cell>& cells = block_structure_->rows[i].cells;
199 for (int j = 0; j < cells.size(); ++j) {
200 int col_block_id = cells[j].block_id;
201 int col_block_size = block_structure_->cols[col_block_id].size;
202 int col_block_pos = block_structure_->cols[col_block_id].position;
203 int jac_pos = cells[j].position;
204 for (int r = 0; r < row_block_size; ++r) {
205 for (int c = 0; c < col_block_size; ++c, ++jac_pos) {
206 matrix->mutable_rows()[jac_pos] = row_block_pos + r;
207 matrix->mutable_cols()[jac_pos] = col_block_pos + c;
208 matrix->mutable_values()[jac_pos] = values_[jac_pos];
209 }
210 }
211 }
212 }
213 matrix->set_num_nonzeros(num_nonzeros_);
214}
215
216// Return a pointer to the block structure. We continue to hold
217// ownership of the object though.
218const CompressedRowBlockStructure* BlockSparseMatrix::block_structure()
219 const {
220 return block_structure_.get();
221}
222
223void BlockSparseMatrix::ToTextFile(FILE* file) const {
224 CHECK(file != nullptr);
225 for (int i = 0; i < block_structure_->rows.size(); ++i) {
226 const int row_block_pos = block_structure_->rows[i].block.position;
227 const int row_block_size = block_structure_->rows[i].block.size;
228 const vector<Cell>& cells = block_structure_->rows[i].cells;
229 for (int j = 0; j < cells.size(); ++j) {
230 const int col_block_id = cells[j].block_id;
231 const int col_block_size = block_structure_->cols[col_block_id].size;
232 const int col_block_pos = block_structure_->cols[col_block_id].position;
233 int jac_pos = cells[j].position;
234 for (int r = 0; r < row_block_size; ++r) {
235 for (int c = 0; c < col_block_size; ++c) {
236 fprintf(file, "% 10d % 10d %17f\n",
237 row_block_pos + r,
238 col_block_pos + c,
239 values_[jac_pos++]);
240 }
241 }
242 }
243 }
244}
245
246BlockSparseMatrix* BlockSparseMatrix::CreateDiagonalMatrix(
247 const double* diagonal, const std::vector<Block>& column_blocks) {
248 // Create the block structure for the diagonal matrix.
249 CompressedRowBlockStructure* bs = new CompressedRowBlockStructure();
250 bs->cols = column_blocks;
251 int position = 0;
252 bs->rows.resize(column_blocks.size(), CompressedRow(1));
253 for (int i = 0; i < column_blocks.size(); ++i) {
254 CompressedRow& row = bs->rows[i];
255 row.block = column_blocks[i];
256 Cell& cell = row.cells[0];
257 cell.block_id = i;
258 cell.position = position;
259 position += row.block.size * row.block.size;
260 }
261
262 // Create the BlockSparseMatrix with the given block structure.
263 BlockSparseMatrix* matrix = new BlockSparseMatrix(bs);
264 matrix->SetZero();
265
266 // Fill the values array of the block sparse matrix.
267 double* values = matrix->mutable_values();
268 for (int i = 0; i < column_blocks.size(); ++i) {
269 const int size = column_blocks[i].size;
270 for (int j = 0; j < size; ++j) {
271 // (j + 1) * size is compact way of accessing the (j,j) entry.
272 values[j * (size + 1)] = diagonal[j];
273 }
274 diagonal += size;
275 values += size * size;
276 }
277
278 return matrix;
279}
280
281void BlockSparseMatrix::AppendRows(const BlockSparseMatrix& m) {
282 CHECK_EQ(m.num_cols(), num_cols());
283 const CompressedRowBlockStructure* m_bs = m.block_structure();
284 CHECK_EQ(m_bs->cols.size(), block_structure_->cols.size());
285
286 const int old_num_nonzeros = num_nonzeros_;
287 const int old_num_row_blocks = block_structure_->rows.size();
288 block_structure_->rows.resize(old_num_row_blocks + m_bs->rows.size());
289
290 for (int i = 0; i < m_bs->rows.size(); ++i) {
291 const CompressedRow& m_row = m_bs->rows[i];
292 CompressedRow& row = block_structure_->rows[old_num_row_blocks + i];
293 row.block.size = m_row.block.size;
294 row.block.position = num_rows_;
295 num_rows_ += m_row.block.size;
296 row.cells.resize(m_row.cells.size());
297 for (int c = 0; c < m_row.cells.size(); ++c) {
298 const int block_id = m_row.cells[c].block_id;
299 row.cells[c].block_id = block_id;
300 row.cells[c].position = num_nonzeros_;
301 num_nonzeros_ += m_row.block.size * m_bs->cols[block_id].size;
302 }
303 }
304
305 if (num_nonzeros_ > max_num_nonzeros_) {
306 double* new_values = new double[num_nonzeros_];
307 std::copy(values_.get(), values_.get() + old_num_nonzeros, new_values);
308 values_.reset(new_values);
309 max_num_nonzeros_ = num_nonzeros_;
310 }
311
312 std::copy(m.values(),
313 m.values() + m.num_nonzeros(),
314 values_.get() + old_num_nonzeros);
315}
316
317void BlockSparseMatrix::DeleteRowBlocks(const int delta_row_blocks) {
318 const int num_row_blocks = block_structure_->rows.size();
319 int delta_num_nonzeros = 0;
320 int delta_num_rows = 0;
321 const std::vector<Block>& column_blocks = block_structure_->cols;
322 for (int i = 0; i < delta_row_blocks; ++i) {
323 const CompressedRow& row = block_structure_->rows[num_row_blocks - i - 1];
324 delta_num_rows += row.block.size;
325 for (int c = 0; c < row.cells.size(); ++c) {
326 const Cell& cell = row.cells[c];
327 delta_num_nonzeros += row.block.size * column_blocks[cell.block_id].size;
328 }
329 }
330 num_nonzeros_ -= delta_num_nonzeros;
331 num_rows_ -= delta_num_rows;
332 block_structure_->rows.resize(num_row_blocks - delta_row_blocks);
333}
334
335BlockSparseMatrix* BlockSparseMatrix::CreateRandomMatrix(
336 const BlockSparseMatrix::RandomMatrixOptions& options) {
337 CHECK_GT(options.num_row_blocks, 0);
338 CHECK_GT(options.min_row_block_size, 0);
339 CHECK_GT(options.max_row_block_size, 0);
340 CHECK_LE(options.min_row_block_size, options.max_row_block_size);
341 CHECK_GT(options.block_density, 0.0);
342 CHECK_LE(options.block_density, 1.0);
343
344 CompressedRowBlockStructure* bs = new CompressedRowBlockStructure();
345 if (options.col_blocks.empty()) {
346 CHECK_GT(options.num_col_blocks, 0);
347 CHECK_GT(options.min_col_block_size, 0);
348 CHECK_GT(options.max_col_block_size, 0);
349 CHECK_LE(options.min_col_block_size, options.max_col_block_size);
350
351 // Generate the col block structure.
352 int col_block_position = 0;
353 for (int i = 0; i < options.num_col_blocks; ++i) {
354 // Generate a random integer in [min_col_block_size, max_col_block_size]
355 const int delta_block_size =
356 Uniform(options.max_col_block_size - options.min_col_block_size);
357 const int col_block_size = options.min_col_block_size + delta_block_size;
358 bs->cols.push_back(Block(col_block_size, col_block_position));
359 col_block_position += col_block_size;
360 }
361 } else {
362 bs->cols = options.col_blocks;
363 }
364
365 bool matrix_has_blocks = false;
366 while (!matrix_has_blocks) {
367 VLOG(1) << "Clearing";
368 bs->rows.clear();
369 int row_block_position = 0;
370 int value_position = 0;
371 for (int r = 0; r < options.num_row_blocks; ++r) {
372
373 const int delta_block_size =
374 Uniform(options.max_row_block_size - options.min_row_block_size);
375 const int row_block_size = options.min_row_block_size + delta_block_size;
376 bs->rows.push_back(CompressedRow());
377 CompressedRow& row = bs->rows.back();
378 row.block.size = row_block_size;
379 row.block.position = row_block_position;
380 row_block_position += row_block_size;
381 for (int c = 0; c < bs->cols.size(); ++c) {
382 if (RandDouble() > options.block_density) continue;
383
384 row.cells.push_back(Cell());
385 Cell& cell = row.cells.back();
386 cell.block_id = c;
387 cell.position = value_position;
388 value_position += row_block_size * bs->cols[c].size;
389 matrix_has_blocks = true;
390 }
391 }
392 }
393
394 BlockSparseMatrix* matrix = new BlockSparseMatrix(bs);
395 double* values = matrix->mutable_values();
396 for (int i = 0; i < matrix->num_nonzeros(); ++i) {
397 values[i] = RandNormal();
398 }
399
400 return matrix;
401}
402
403} // namespace internal
404} // namespace ceres