|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2017 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/compressed_row_sparse_matrix.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <algorithm>
|
|
Packit |
ea1746 |
#include <numeric>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
#include "ceres/crs_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/port.h"
|
|
Packit |
ea1746 |
#include "ceres/random.h"
|
|
Packit |
ea1746 |
#include "ceres/triplet_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
using std::vector;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Helper functor used by the constructor for reordering the contents
|
|
Packit |
ea1746 |
// of a TripletSparseMatrix. This comparator assumes thay there are no
|
|
Packit |
ea1746 |
// duplicates in the pair of arrays rows and cols, i.e., there is no
|
|
Packit |
ea1746 |
// indices i and j (not equal to each other) s.t.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// rows[i] == rows[j] && cols[i] == cols[j]
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// If this is the case, this functor will not be a StrictWeakOrdering.
|
|
Packit |
ea1746 |
struct RowColLessThan {
|
|
Packit |
ea1746 |
RowColLessThan(const int* rows, const int* cols) : rows(rows), cols(cols) {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
bool operator()(const int x, const int y) const {
|
|
Packit |
ea1746 |
if (rows[x] == rows[y]) {
|
|
Packit |
ea1746 |
return (cols[x] < cols[y]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
return (rows[x] < rows[y]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int* rows;
|
|
Packit |
ea1746 |
const int* cols;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void TransposeForCompressedRowSparseStructure(const int num_rows,
|
|
Packit |
ea1746 |
const int num_cols,
|
|
Packit |
ea1746 |
const int num_nonzeros,
|
|
Packit |
ea1746 |
const int* rows,
|
|
Packit |
ea1746 |
const int* cols,
|
|
Packit |
ea1746 |
const double* values,
|
|
Packit |
ea1746 |
int* transpose_rows,
|
|
Packit |
ea1746 |
int* transpose_cols,
|
|
Packit |
ea1746 |
double* transpose_values) {
|
|
Packit |
ea1746 |
// Explicitly zero out transpose_rows.
|
|
Packit |
ea1746 |
std::fill(transpose_rows, transpose_rows + num_cols + 1, 0);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Count the number of entries in each column of the original matrix
|
|
Packit |
ea1746 |
// and assign to transpose_rows[col + 1].
|
|
Packit |
ea1746 |
for (int idx = 0; idx < num_nonzeros; ++idx) {
|
|
Packit |
ea1746 |
++transpose_rows[cols[idx] + 1];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Compute the starting position for each row in the transpose by
|
|
Packit |
ea1746 |
// computing the cumulative sum of the entries of transpose_rows.
|
|
Packit |
ea1746 |
for (int i = 1; i < num_cols + 1; ++i) {
|
|
Packit |
ea1746 |
transpose_rows[i] += transpose_rows[i - 1];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Populate transpose_cols and (optionally) transpose_values by
|
|
Packit |
ea1746 |
// walking the entries of the source matrices. For each entry that
|
|
Packit |
ea1746 |
// is added, the value of transpose_row is incremented allowing us
|
|
Packit |
ea1746 |
// to keep track of where the next entry for that row should go.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// As a result transpose_row is shifted to the left by one entry.
|
|
Packit |
ea1746 |
for (int r = 0; r < num_rows; ++r) {
|
|
Packit |
ea1746 |
for (int idx = rows[r]; idx < rows[r + 1]; ++idx) {
|
|
Packit |
ea1746 |
const int c = cols[idx];
|
|
Packit |
ea1746 |
const int transpose_idx = transpose_rows[c]++;
|
|
Packit |
ea1746 |
transpose_cols[transpose_idx] = r;
|
|
Packit |
ea1746 |
if (values != NULL && transpose_values != NULL) {
|
|
Packit |
ea1746 |
transpose_values[transpose_idx] = values[idx];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This loop undoes the left shift to transpose_rows introduced by
|
|
Packit |
ea1746 |
// the previous loop.
|
|
Packit |
ea1746 |
for (int i = num_cols - 1; i > 0; --i) {
|
|
Packit |
ea1746 |
transpose_rows[i] = transpose_rows[i - 1];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
transpose_rows[0] = 0;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void AddRandomBlock(const int num_rows,
|
|
Packit |
ea1746 |
const int num_cols,
|
|
Packit |
ea1746 |
const int row_block_begin,
|
|
Packit |
ea1746 |
const int col_block_begin,
|
|
Packit |
ea1746 |
std::vector<int>* rows,
|
|
Packit |
ea1746 |
std::vector<int>* cols,
|
|
Packit |
ea1746 |
std::vector<double>* values) {
|
|
Packit |
ea1746 |
for (int r = 0; r < num_rows; ++r) {
|
|
Packit |
ea1746 |
for (int c = 0; c < num_cols; ++c) {
|
|
Packit |
ea1746 |
rows->push_back(row_block_begin + r);
|
|
Packit |
ea1746 |
cols->push_back(col_block_begin + c);
|
|
Packit |
ea1746 |
values->push_back(RandNormal());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// This constructor gives you a semi-initialized CompressedRowSparseMatrix.
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::CompressedRowSparseMatrix(int num_rows,
|
|
Packit |
ea1746 |
int num_cols,
|
|
Packit |
ea1746 |
int max_num_nonzeros) {
|
|
Packit |
ea1746 |
num_rows_ = num_rows;
|
|
Packit |
ea1746 |
num_cols_ = num_cols;
|
|
Packit |
ea1746 |
storage_type_ = UNSYMMETRIC;
|
|
Packit |
ea1746 |
rows_.resize(num_rows + 1, 0);
|
|
Packit |
ea1746 |
cols_.resize(max_num_nonzeros, 0);
|
|
Packit |
ea1746 |
values_.resize(max_num_nonzeros, 0.0);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
VLOG(1) << "# of rows: " << num_rows_ << " # of columns: " << num_cols_
|
|
Packit |
ea1746 |
<< " max_num_nonzeros: " << cols_.size() << ". Allocating "
|
|
Packit |
ea1746 |
<< (num_rows_ + 1) * sizeof(int) + // NOLINT
|
|
Packit |
ea1746 |
cols_.size() * sizeof(int) + // NOLINT
|
|
Packit |
ea1746 |
cols_.size() * sizeof(double); // NOLINT
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* CompressedRowSparseMatrix::FromTripletSparseMatrix(
|
|
Packit |
ea1746 |
const TripletSparseMatrix& input) {
|
|
Packit |
ea1746 |
return CompressedRowSparseMatrix::FromTripletSparseMatrix(input, false);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix*
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::FromTripletSparseMatrixTransposed(
|
|
Packit |
ea1746 |
const TripletSparseMatrix& input) {
|
|
Packit |
ea1746 |
return CompressedRowSparseMatrix::FromTripletSparseMatrix(input, true);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* CompressedRowSparseMatrix::FromTripletSparseMatrix(
|
|
Packit |
ea1746 |
const TripletSparseMatrix& input, bool transpose) {
|
|
Packit |
ea1746 |
int num_rows = input.num_rows();
|
|
Packit |
ea1746 |
int num_cols = input.num_cols();
|
|
Packit |
ea1746 |
const int* rows = input.rows();
|
|
Packit |
ea1746 |
const int* cols = input.cols();
|
|
Packit |
ea1746 |
const double* values = input.values();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (transpose) {
|
|
Packit |
ea1746 |
std::swap(num_rows, num_cols);
|
|
Packit |
ea1746 |
std::swap(rows, cols);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// index is the list of indices into the TripletSparseMatrix input.
|
|
Packit |
ea1746 |
vector<int> index(input.num_nonzeros(), 0);
|
|
Packit |
ea1746 |
for (int i = 0; i < input.num_nonzeros(); ++i) {
|
|
Packit |
ea1746 |
index[i] = i;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Sort index such that the entries of m are ordered by row and ties
|
|
Packit |
ea1746 |
// are broken by column.
|
|
Packit |
ea1746 |
std::sort(index.begin(), index.end(), RowColLessThan(rows, cols));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
VLOG(1) << "# of rows: " << num_rows << " # of columns: " << num_cols
|
|
Packit |
ea1746 |
<< " num_nonzeros: " << input.num_nonzeros() << ". Allocating "
|
|
Packit |
ea1746 |
<< ((num_rows + 1) * sizeof(int) + // NOLINT
|
|
Packit |
ea1746 |
input.num_nonzeros() * sizeof(int) + // NOLINT
|
|
Packit |
ea1746 |
input.num_nonzeros() * sizeof(double)); // NOLINT
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* output =
|
|
Packit |
ea1746 |
new CompressedRowSparseMatrix(num_rows, num_cols, input.num_nonzeros());
|
|
Packit |
ea1746 |
|
|
Packit Service |
89d5ee |
if (num_rows == 0) {
|
|
Packit Service |
89d5ee |
// No data to copy.
|
|
Packit Service |
89d5ee |
return output;
|
|
Packit Service |
89d5ee |
}
|
|
Packit Service |
89d5ee |
|
|
Packit |
ea1746 |
// Copy the contents of the cols and values array in the order given
|
|
Packit |
ea1746 |
// by index and count the number of entries in each row.
|
|
Packit |
ea1746 |
int* output_rows = output->mutable_rows();
|
|
Packit |
ea1746 |
int* output_cols = output->mutable_cols();
|
|
Packit |
ea1746 |
double* output_values = output->mutable_values();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
output_rows[0] = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < index.size(); ++i) {
|
|
Packit |
ea1746 |
const int idx = index[i];
|
|
Packit |
ea1746 |
++output_rows[rows[idx] + 1];
|
|
Packit |
ea1746 |
output_cols[i] = cols[idx];
|
|
Packit |
ea1746 |
output_values[i] = values[idx];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Find the cumulative sum of the row counts.
|
|
Packit |
ea1746 |
for (int i = 1; i < num_rows + 1; ++i) {
|
|
Packit |
ea1746 |
output_rows[i] += output_rows[i - 1];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CHECK_EQ(output->num_nonzeros(), input.num_nonzeros());
|
|
Packit |
ea1746 |
return output;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::CompressedRowSparseMatrix(const double* diagonal,
|
|
Packit |
ea1746 |
int num_rows) {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(diagonal);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
num_rows_ = num_rows;
|
|
Packit |
ea1746 |
num_cols_ = num_rows;
|
|
Packit |
ea1746 |
storage_type_ = UNSYMMETRIC;
|
|
Packit |
ea1746 |
rows_.resize(num_rows + 1);
|
|
Packit |
ea1746 |
cols_.resize(num_rows);
|
|
Packit |
ea1746 |
values_.resize(num_rows);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
rows_[0] = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < num_rows_; ++i) {
|
|
Packit |
ea1746 |
cols_[i] = i;
|
|
Packit |
ea1746 |
values_[i] = diagonal[i];
|
|
Packit |
ea1746 |
rows_[i + 1] = i + 1;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CHECK_EQ(num_nonzeros(), num_rows);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::~CompressedRowSparseMatrix() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::SetZero() {
|
|
Packit |
ea1746 |
std::fill(values_.begin(), values_.end(), 0);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::RightMultiply(const double* x,
|
|
Packit |
ea1746 |
double* y) const {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(x);
|
|
Packit |
ea1746 |
CHECK_NOTNULL(y);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int r = 0; r < num_rows_; ++r) {
|
|
Packit |
ea1746 |
for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
|
|
Packit |
ea1746 |
y[r] += values_[idx] * x[cols_[idx]];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::LeftMultiply(const double* x, double* y) const {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(x);
|
|
Packit |
ea1746 |
CHECK_NOTNULL(y);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int r = 0; r < num_rows_; ++r) {
|
|
Packit |
ea1746 |
for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
|
|
Packit |
ea1746 |
y[cols_[idx]] += values_[idx] * x[r];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::SquaredColumnNorm(double* x) const {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(x);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
std::fill(x, x + num_cols_, 0.0);
|
|
Packit |
ea1746 |
for (int idx = 0; idx < rows_[num_rows_]; ++idx) {
|
|
Packit |
ea1746 |
x[cols_[idx]] += values_[idx] * values_[idx];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::ScaleColumns(const double* scale) {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(scale);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int idx = 0; idx < rows_[num_rows_]; ++idx) {
|
|
Packit |
ea1746 |
values_[idx] *= scale[cols_[idx]];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(dense_matrix);
|
|
Packit |
ea1746 |
dense_matrix->resize(num_rows_, num_cols_);
|
|
Packit |
ea1746 |
dense_matrix->setZero();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int r = 0; r < num_rows_; ++r) {
|
|
Packit |
ea1746 |
for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
|
|
Packit |
ea1746 |
(*dense_matrix)(r, cols_[idx]) = values_[idx];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::DeleteRows(int delta_rows) {
|
|
Packit |
ea1746 |
CHECK_GE(delta_rows, 0);
|
|
Packit |
ea1746 |
CHECK_LE(delta_rows, num_rows_);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
num_rows_ -= delta_rows;
|
|
Packit |
ea1746 |
rows_.resize(num_rows_ + 1);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The rest of the code updates the block information. Immediately
|
|
Packit |
ea1746 |
// return in case of no block information.
|
|
Packit |
ea1746 |
if (row_blocks_.empty()) {
|
|
Packit |
ea1746 |
return;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Walk the list of row blocks until we reach the new number of rows
|
|
Packit |
ea1746 |
// and the drop the rest of the row blocks.
|
|
Packit |
ea1746 |
int num_row_blocks = 0;
|
|
Packit |
ea1746 |
int num_rows = 0;
|
|
Packit |
ea1746 |
while (num_row_blocks < row_blocks_.size() && num_rows < num_rows_) {
|
|
Packit |
ea1746 |
num_rows += row_blocks_[num_row_blocks];
|
|
Packit |
ea1746 |
++num_row_blocks;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
row_blocks_.resize(num_row_blocks);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::AppendRows(const CompressedRowSparseMatrix& m) {
|
|
Packit |
ea1746 |
CHECK_EQ(m.num_cols(), num_cols_);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CHECK((row_blocks_.empty() && m.row_blocks().empty()) ||
|
|
Packit |
ea1746 |
(!row_blocks_.empty() && !m.row_blocks().empty()))
|
|
Packit |
ea1746 |
<< "Cannot append a matrix with row blocks to one without and vice versa."
|
|
Packit |
ea1746 |
<< "This matrix has : " << row_blocks_.size() << " row blocks."
|
|
Packit |
ea1746 |
<< "The matrix being appended has: " << m.row_blocks().size()
|
|
Packit |
ea1746 |
<< " row blocks.";
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (m.num_rows() == 0) {
|
|
Packit |
ea1746 |
return;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (cols_.size() < num_nonzeros() + m.num_nonzeros()) {
|
|
Packit |
ea1746 |
cols_.resize(num_nonzeros() + m.num_nonzeros());
|
|
Packit |
ea1746 |
values_.resize(num_nonzeros() + m.num_nonzeros());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Copy the contents of m into this matrix.
|
|
Packit |
ea1746 |
DCHECK_LT(num_nonzeros(), cols_.size());
|
|
Packit |
ea1746 |
if (m.num_nonzeros() > 0) {
|
|
Packit |
ea1746 |
std::copy(m.cols(), m.cols() + m.num_nonzeros(), &cols_[num_nonzeros()]);
|
|
Packit |
ea1746 |
std::copy(
|
|
Packit |
ea1746 |
m.values(), m.values() + m.num_nonzeros(), &values_[num_nonzeros()]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
rows_.resize(num_rows_ + m.num_rows() + 1);
|
|
Packit |
ea1746 |
// new_rows = [rows_, m.row() + rows_[num_rows_]]
|
|
Packit |
ea1746 |
std::fill(rows_.begin() + num_rows_,
|
|
Packit |
ea1746 |
rows_.begin() + num_rows_ + m.num_rows() + 1,
|
|
Packit |
ea1746 |
rows_[num_rows_]);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int r = 0; r < m.num_rows() + 1; ++r) {
|
|
Packit |
ea1746 |
rows_[num_rows_ + r] += m.rows()[r];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
num_rows_ += m.num_rows();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The rest of the code updates the block information. Immediately
|
|
Packit |
ea1746 |
// return in case of no block information.
|
|
Packit |
ea1746 |
if (row_blocks_.empty()) {
|
|
Packit |
ea1746 |
return;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
row_blocks_.insert(
|
|
Packit |
ea1746 |
row_blocks_.end(), m.row_blocks().begin(), m.row_blocks().end());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::ToTextFile(FILE* file) const {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(file);
|
|
Packit |
ea1746 |
for (int r = 0; r < num_rows_; ++r) {
|
|
Packit |
ea1746 |
for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {
|
|
Packit |
ea1746 |
fprintf(file, "% 10d % 10d %17f\n", r, cols_[idx], values_[idx]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::ToCRSMatrix(CRSMatrix* matrix) const {
|
|
Packit |
ea1746 |
matrix->num_rows = num_rows_;
|
|
Packit |
ea1746 |
matrix->num_cols = num_cols_;
|
|
Packit |
ea1746 |
matrix->rows = rows_;
|
|
Packit |
ea1746 |
matrix->cols = cols_;
|
|
Packit |
ea1746 |
matrix->values = values_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Trim.
|
|
Packit |
ea1746 |
matrix->rows.resize(matrix->num_rows + 1);
|
|
Packit |
ea1746 |
matrix->cols.resize(matrix->rows[matrix->num_rows]);
|
|
Packit |
ea1746 |
matrix->values.resize(matrix->rows[matrix->num_rows]);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowSparseMatrix::SetMaxNumNonZeros(int num_nonzeros) {
|
|
Packit |
ea1746 |
CHECK_GE(num_nonzeros, 0);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
cols_.resize(num_nonzeros);
|
|
Packit |
ea1746 |
values_.resize(num_nonzeros);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateBlockDiagonalMatrix(
|
|
Packit |
ea1746 |
const double* diagonal, const vector<int>& blocks) {
|
|
Packit |
ea1746 |
int num_rows = 0;
|
|
Packit |
ea1746 |
int num_nonzeros = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < blocks.size(); ++i) {
|
|
Packit |
ea1746 |
num_rows += blocks[i];
|
|
Packit |
ea1746 |
num_nonzeros += blocks[i] * blocks[i];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* matrix =
|
|
Packit |
ea1746 |
new CompressedRowSparseMatrix(num_rows, num_rows, num_nonzeros);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int* rows = matrix->mutable_rows();
|
|
Packit |
ea1746 |
int* cols = matrix->mutable_cols();
|
|
Packit |
ea1746 |
double* values = matrix->mutable_values();
|
|
Packit |
ea1746 |
std::fill(values, values + num_nonzeros, 0.0);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int idx_cursor = 0;
|
|
Packit |
ea1746 |
int col_cursor = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < blocks.size(); ++i) {
|
|
Packit |
ea1746 |
const int block_size = blocks[i];
|
|
Packit |
ea1746 |
for (int r = 0; r < block_size; ++r) {
|
|
Packit |
ea1746 |
*(rows++) = idx_cursor;
|
|
Packit |
ea1746 |
values[idx_cursor + r] = diagonal[col_cursor + r];
|
|
Packit |
ea1746 |
for (int c = 0; c < block_size; ++c, ++idx_cursor) {
|
|
Packit |
ea1746 |
*(cols++) = col_cursor + c;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
col_cursor += block_size;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
*rows = idx_cursor;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
*matrix->mutable_row_blocks() = blocks;
|
|
Packit |
ea1746 |
*matrix->mutable_col_blocks() = blocks;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CHECK_EQ(idx_cursor, num_nonzeros);
|
|
Packit |
ea1746 |
CHECK_EQ(col_cursor, num_rows);
|
|
Packit |
ea1746 |
return matrix;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* CompressedRowSparseMatrix::Transpose() const {
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* transpose =
|
|
Packit |
ea1746 |
new CompressedRowSparseMatrix(num_cols_, num_rows_, num_nonzeros());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
switch (storage_type_) {
|
|
Packit |
ea1746 |
case UNSYMMETRIC:
|
|
Packit |
ea1746 |
transpose->set_storage_type(UNSYMMETRIC);
|
|
Packit |
ea1746 |
break;
|
|
Packit |
ea1746 |
case LOWER_TRIANGULAR:
|
|
Packit |
ea1746 |
transpose->set_storage_type(UPPER_TRIANGULAR);
|
|
Packit |
ea1746 |
break;
|
|
Packit |
ea1746 |
case UPPER_TRIANGULAR:
|
|
Packit |
ea1746 |
transpose->set_storage_type(LOWER_TRIANGULAR);
|
|
Packit |
ea1746 |
break;
|
|
Packit |
ea1746 |
default:
|
|
Packit |
ea1746 |
LOG(FATAL) << "Unknown storage type: " << storage_type_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TransposeForCompressedRowSparseStructure(num_rows(),
|
|
Packit |
ea1746 |
num_cols(),
|
|
Packit |
ea1746 |
num_nonzeros(),
|
|
Packit |
ea1746 |
rows(),
|
|
Packit |
ea1746 |
cols(),
|
|
Packit |
ea1746 |
values(),
|
|
Packit |
ea1746 |
transpose->mutable_rows(),
|
|
Packit |
ea1746 |
transpose->mutable_cols(),
|
|
Packit |
ea1746 |
transpose->mutable_values());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The rest of the code updates the block information. Immediately
|
|
Packit |
ea1746 |
// return in case of no block information.
|
|
Packit |
ea1746 |
if (row_blocks_.empty()) {
|
|
Packit |
ea1746 |
return transpose;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
*(transpose->mutable_row_blocks()) = col_blocks_;
|
|
Packit |
ea1746 |
*(transpose->mutable_col_blocks()) = row_blocks_;
|
|
Packit |
ea1746 |
return transpose;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateRandomMatrix(
|
|
Packit |
ea1746 |
const CompressedRowSparseMatrix::RandomMatrixOptions& options) {
|
|
Packit |
ea1746 |
CHECK_GT(options.num_row_blocks, 0);
|
|
Packit |
ea1746 |
CHECK_GT(options.min_row_block_size, 0);
|
|
Packit |
ea1746 |
CHECK_GT(options.max_row_block_size, 0);
|
|
Packit |
ea1746 |
CHECK_LE(options.min_row_block_size, options.max_row_block_size);
|
|
Packit |
ea1746 |
CHECK_GT(options.num_col_blocks, 0);
|
|
Packit |
ea1746 |
CHECK_GT(options.min_col_block_size, 0);
|
|
Packit |
ea1746 |
CHECK_GT(options.max_col_block_size, 0);
|
|
Packit |
ea1746 |
CHECK_LE(options.min_col_block_size, options.max_col_block_size);
|
|
Packit |
ea1746 |
CHECK_GT(options.block_density, 0.0);
|
|
Packit |
ea1746 |
CHECK_LE(options.block_density, 1.0);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
vector<int> row_blocks;
|
|
Packit |
ea1746 |
vector<int> col_blocks;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Generate the row block structure.
|
|
Packit |
ea1746 |
for (int i = 0; i < options.num_row_blocks; ++i) {
|
|
Packit |
ea1746 |
// Generate a random integer in [min_row_block_size, max_row_block_size]
|
|
Packit |
ea1746 |
const int delta_block_size =
|
|
Packit |
ea1746 |
Uniform(options.max_row_block_size - options.min_row_block_size);
|
|
Packit |
ea1746 |
row_blocks.push_back(options.min_row_block_size + delta_block_size);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Generate the col block structure.
|
|
Packit |
ea1746 |
for (int i = 0; i < options.num_col_blocks; ++i) {
|
|
Packit |
ea1746 |
// Generate a random integer in [min_col_block_size, max_col_block_size]
|
|
Packit |
ea1746 |
const int delta_block_size =
|
|
Packit |
ea1746 |
Uniform(options.max_col_block_size - options.min_col_block_size);
|
|
Packit |
ea1746 |
col_blocks.push_back(options.min_col_block_size + delta_block_size);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
vector<int> tsm_rows;
|
|
Packit |
ea1746 |
vector<int> tsm_cols;
|
|
Packit |
ea1746 |
vector<double> tsm_values;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// For ease of construction, we are going to generate the
|
|
Packit |
ea1746 |
// CompressedRowSparseMatrix by generating it as a
|
|
Packit |
ea1746 |
// TripletSparseMatrix and then converting it to a
|
|
Packit |
ea1746 |
// CompressedRowSparseMatrix.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// It is possible that the random matrix is empty which is likely
|
|
Packit |
ea1746 |
// not what the user wants, so do the matrix generation till we have
|
|
Packit |
ea1746 |
// at least one non-zero entry.
|
|
Packit |
ea1746 |
while (tsm_values.empty()) {
|
|
Packit |
ea1746 |
tsm_rows.clear();
|
|
Packit |
ea1746 |
tsm_cols.clear();
|
|
Packit |
ea1746 |
tsm_values.clear();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int row_block_begin = 0;
|
|
Packit |
ea1746 |
for (int r = 0; r < options.num_row_blocks; ++r) {
|
|
Packit |
ea1746 |
int col_block_begin = 0;
|
|
Packit |
ea1746 |
for (int c = 0; c < options.num_col_blocks; ++c) {
|
|
Packit |
ea1746 |
// Randomly determine if this block is present or not.
|
|
Packit |
ea1746 |
if (RandDouble() <= options.block_density) {
|
|
Packit |
ea1746 |
AddRandomBlock(row_blocks[r],
|
|
Packit |
ea1746 |
col_blocks[c],
|
|
Packit |
ea1746 |
row_block_begin,
|
|
Packit |
ea1746 |
col_block_begin,
|
|
Packit |
ea1746 |
&tsm_rows,
|
|
Packit |
ea1746 |
&tsm_cols,
|
|
Packit |
ea1746 |
&tsm_values);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
col_block_begin += col_blocks[c];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
row_block_begin += row_blocks[r];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int num_rows = std::accumulate(row_blocks.begin(), row_blocks.end(), 0);
|
|
Packit |
ea1746 |
const int num_cols = std::accumulate(col_blocks.begin(), col_blocks.end(), 0);
|
|
Packit |
ea1746 |
const bool kDoNotTranspose = false;
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* matrix =
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix::FromTripletSparseMatrix(
|
|
Packit |
ea1746 |
TripletSparseMatrix(
|
|
Packit |
ea1746 |
num_rows, num_cols, tsm_rows, tsm_cols, tsm_values),
|
|
Packit |
ea1746 |
kDoNotTranspose);
|
|
Packit |
ea1746 |
(*matrix->mutable_row_blocks()) = row_blocks;
|
|
Packit |
ea1746 |
(*matrix->mutable_col_blocks()) = col_blocks;
|
|
Packit |
ea1746 |
matrix->set_storage_type(CompressedRowSparseMatrix::UNSYMMETRIC);
|
|
Packit |
ea1746 |
return matrix;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|