| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 | // Ceres Solver - A fast non-linear least squares minimizer// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.// http://code.google.com/p/ceres-solver///// Redistribution and use in source and binary forms, with or without// modification, are permitted provided that the following conditions are met://// * Redistributions of source code must retain the above copyright notice,//   this list of conditions and the following disclaimer.// * Redistributions in binary form must reproduce the above copyright notice,//   this list of conditions and the following disclaimer in the documentation//   and/or other materials provided with the distribution.// * Neither the name of Google Inc. nor the names of its contributors may be//   used to endorse or promote products derived from this software without//   specific prior written permission.//// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE// POSSIBILITY OF SUCH DAMAGE.//// Author: sameeragarwal@google.com (Sameer Agarwal)#include "ceres/compressed_row_sparse_matrix.h"#include <algorithm>#include <numeric>#include <vector>#include "ceres/crs_matrix.h"#include "ceres/internal/port.h"#include "ceres/triplet_sparse_matrix.h"#include "glog/logging.h"namespace ceres {namespace internal {namespace {// Helper functor used by the constructor for reordering the contents// of a TripletSparseMatrix. This comparator assumes thay there are no// duplicates in the pair of arrays rows and cols, i.e., there is no// indices i and j (not equal to each other) s.t.////  rows[i] == rows[j] && cols[i] == cols[j]//// If this is the case, this functor will not be a StrictWeakOrdering.struct RowColLessThan {  RowColLessThan(const int* rows, const int* cols)      : rows(rows), cols(cols) {  }  bool operator()(const int x, const int y) const {    if (rows[x] == rows[y]) {      return (cols[x] < cols[y]);    }    return (rows[x] < rows[y]);  }  const int* rows;  const int* cols;};}  // namespace// This constructor gives you a semi-initialized CompressedRowSparseMatrix.CompressedRowSparseMatrix::CompressedRowSparseMatrix(int num_rows,                                                     int num_cols,                                                     int max_num_nonzeros) {  num_rows_ = num_rows;  num_cols_ = num_cols;  rows_.resize(num_rows + 1, 0);  cols_.resize(max_num_nonzeros, 0);  values_.resize(max_num_nonzeros, 0.0);  VLOG(1) << "# of rows: " << num_rows_          << " # of columns: " << num_cols_          << " max_num_nonzeros: " << cols_.size()          << ". Allocating " << (num_rows_ + 1) * sizeof(int) +  // NOLINT      cols_.size() * sizeof(int) +  // NOLINT      cols_.size() * sizeof(double);  // NOLINT}CompressedRowSparseMatrix::CompressedRowSparseMatrix(    const TripletSparseMatrix& m) {  num_rows_ = m.num_rows();  num_cols_ = m.num_cols();  rows_.resize(num_rows_ + 1, 0);  cols_.resize(m.num_nonzeros(), 0);  values_.resize(m.max_num_nonzeros(), 0.0);  // index is the list of indices into the TripletSparseMatrix m.  vector<int> index(m.num_nonzeros(), 0);  for (int i = 0; i < m.num_nonzeros(); ++i) {    index[i] = i;  }  // Sort index such that the entries of m are ordered by row and ties  // are broken by column.  sort(index.begin(), index.end(), RowColLessThan(m.rows(), m.cols()));  VLOG(1) << "# of rows: " << num_rows_          << " # of columns: " << num_cols_          << " max_num_nonzeros: " << cols_.size()          << ". Allocating "          << ((num_rows_ + 1) * sizeof(int) +  // NOLINT              cols_.size() * sizeof(int) +     // NOLINT              cols_.size() * sizeof(double));  // NOLINT  // Copy the contents of the cols and values array in the order given  // by index and count the number of entries in each row.  for (int i = 0; i < m.num_nonzeros(); ++i) {    const int idx = index[i];    ++rows_[m.rows()[idx] + 1];    cols_[i] = m.cols()[idx];    values_[i] = m.values()[idx];  }  // Find the cumulative sum of the row counts.  for (int i = 1; i < num_rows_ + 1; ++i) {    rows_[i] += rows_[i-1];  }  CHECK_EQ(num_nonzeros(), m.num_nonzeros());}CompressedRowSparseMatrix::CompressedRowSparseMatrix(const double* diagonal,                                                     int num_rows) {  CHECK_NOTNULL(diagonal);  num_rows_ = num_rows;  num_cols_ = num_rows;  rows_.resize(num_rows + 1);  cols_.resize(num_rows);  values_.resize(num_rows);  rows_[0] = 0;  for (int i = 0; i < num_rows_; ++i) {    cols_[i] = i;    values_[i] = diagonal[i];    rows_[i + 1] = i + 1;  }  CHECK_EQ(num_nonzeros(), num_rows);}CompressedRowSparseMatrix::~CompressedRowSparseMatrix() {}void CompressedRowSparseMatrix::SetZero() {  fill(values_.begin(), values_.end(), 0);}void CompressedRowSparseMatrix::RightMultiply(const double* x,                                              double* y) const {  CHECK_NOTNULL(x);  CHECK_NOTNULL(y);  for (int r = 0; r < num_rows_; ++r) {    for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {      y[r] += values_[idx] * x[cols_[idx]];    }  }}void CompressedRowSparseMatrix::LeftMultiply(const double* x, double* y) const {  CHECK_NOTNULL(x);  CHECK_NOTNULL(y);  for (int r = 0; r < num_rows_; ++r) {    for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {      y[cols_[idx]] += values_[idx] * x[r];    }  }}void CompressedRowSparseMatrix::SquaredColumnNorm(double* x) const {  CHECK_NOTNULL(x);  fill(x, x + num_cols_, 0.0);  for (int idx = 0; idx < rows_[num_rows_]; ++idx) {    x[cols_[idx]] += values_[idx] * values_[idx];  }}void CompressedRowSparseMatrix::ScaleColumns(const double* scale) {  CHECK_NOTNULL(scale);  for (int idx = 0; idx < rows_[num_rows_]; ++idx) {    values_[idx] *= scale[cols_[idx]];  }}void CompressedRowSparseMatrix::ToDenseMatrix(Matrix* dense_matrix) const {  CHECK_NOTNULL(dense_matrix);  dense_matrix->resize(num_rows_, num_cols_);  dense_matrix->setZero();  for (int r = 0; r < num_rows_; ++r) {    for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {      (*dense_matrix)(r, cols_[idx]) = values_[idx];    }  }}void CompressedRowSparseMatrix::DeleteRows(int delta_rows) {  CHECK_GE(delta_rows, 0);  CHECK_LE(delta_rows, num_rows_);  num_rows_ -= delta_rows;  rows_.resize(num_rows_ + 1);  // Walk the list of row blocks untill we reach the new number of  // rows and then drop the rest of the row blocks.  int num_row_blocks = 0;  int num_rows = 0;  while (num_row_blocks < row_blocks_.size() && num_rows < num_rows_) {    num_rows += row_blocks_[num_row_blocks];    ++num_row_blocks;  }  row_blocks_.resize(num_row_blocks);}void CompressedRowSparseMatrix::AppendRows(const CompressedRowSparseMatrix& m) {  CHECK_EQ(m.num_cols(), num_cols_);  CHECK(row_blocks_.size() == 0 || m.row_blocks().size() !=0)      << "Cannot append a matrix with row blocks to one without and vice versa."      << "This matrix has : " << row_blocks_.size() << " row blocks."      << "The matrix being appended has: " << m.row_blocks().size()      << " row blocks.";  if (cols_.size() < num_nonzeros() + m.num_nonzeros()) {    cols_.resize(num_nonzeros() + m.num_nonzeros());    values_.resize(num_nonzeros() + m.num_nonzeros());  }  // Copy the contents of m into this matrix.  copy(m.cols(), m.cols() + m.num_nonzeros(), &cols_[num_nonzeros()]);  copy(m.values(), m.values() + m.num_nonzeros(), &values_[num_nonzeros()]);  rows_.resize(num_rows_ + m.num_rows() + 1);  // new_rows = [rows_, m.row() + rows_[num_rows_]]  fill(rows_.begin() + num_rows_,       rows_.begin() + num_rows_ + m.num_rows() + 1,       rows_[num_rows_]);  for (int r = 0; r < m.num_rows() + 1; ++r) {    rows_[num_rows_ + r] += m.rows()[r];  }  num_rows_ += m.num_rows();  row_blocks_.insert(row_blocks_.end(), m.row_blocks().begin(), m.row_blocks().end());}void CompressedRowSparseMatrix::ToTextFile(FILE* file) const {  CHECK_NOTNULL(file);  for (int r = 0; r < num_rows_; ++r) {    for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {      fprintf(file,              "% 10d % 10d %17f\n",              r,              cols_[idx],              values_[idx]);    }  }}void CompressedRowSparseMatrix::ToCRSMatrix(CRSMatrix* matrix) const {  matrix->num_rows = num_rows_;  matrix->num_cols = num_cols_;  matrix->rows = rows_;  matrix->cols = cols_;  matrix->values = values_;  // Trim.  matrix->rows.resize(matrix->num_rows + 1);  matrix->cols.resize(matrix->rows[matrix->num_rows]);  matrix->values.resize(matrix->rows[matrix->num_rows]);}void CompressedRowSparseMatrix::SolveLowerTriangularInPlace(    double* solution) const {  for (int r = 0; r < num_rows_; ++r) {    for (int idx = rows_[r]; idx < rows_[r + 1] - 1; ++idx) {      solution[r] -= values_[idx] * solution[cols_[idx]];    }    solution[r] /=  values_[rows_[r + 1] - 1];  }}void CompressedRowSparseMatrix::SolveLowerTriangularTransposeInPlace(    double* solution) const {  for (int r = num_rows_ - 1; r >= 0; --r) {    solution[r] /= values_[rows_[r + 1] - 1];    for (int idx = rows_[r + 1] - 2; idx >= rows_[r]; --idx) {      solution[cols_[idx]] -= values_[idx] * solution[r];    }  }}CompressedRowSparseMatrix* CompressedRowSparseMatrix::CreateBlockDiagonalMatrix(    const double* diagonal,    const vector<int>& blocks) {  int num_rows = 0;  int num_nonzeros = 0;  for (int i = 0; i < blocks.size(); ++i) {    num_rows += blocks[i];    num_nonzeros += blocks[i] * blocks[i];  }  CompressedRowSparseMatrix* matrix =      new CompressedRowSparseMatrix(num_rows, num_rows, num_nonzeros);  int* rows = matrix->mutable_rows();  int* cols = matrix->mutable_cols();  double* values = matrix->mutable_values();  fill(values, values + num_nonzeros, 0.0);  int idx_cursor = 0;  int col_cursor = 0;  for (int i = 0; i < blocks.size(); ++i) {    const int block_size = blocks[i];    for (int r = 0; r < block_size; ++r) {      *(rows++) = idx_cursor;      values[idx_cursor + r] = diagonal[col_cursor + r];      for (int c = 0; c < block_size; ++c, ++idx_cursor) {        *(cols++) = col_cursor + c;      }    }    col_cursor += block_size;  }  *rows = idx_cursor;  *matrix->mutable_row_blocks() = blocks;  *matrix->mutable_col_blocks() = blocks;  CHECK_EQ(idx_cursor, num_nonzeros);  CHECK_EQ(col_cursor, num_rows);  return matrix;}CompressedRowSparseMatrix* CompressedRowSparseMatrix::Transpose() const {  CompressedRowSparseMatrix* transpose =      new CompressedRowSparseMatrix(num_cols_, num_rows_, num_nonzeros());  int* transpose_rows = transpose->mutable_rows();  int* transpose_cols = transpose->mutable_cols();  double* transpose_values = transpose->mutable_values();  for (int idx = 0; idx < num_nonzeros(); ++idx) {    ++transpose_rows[cols_[idx] + 1];  }  for (int i = 1; i < transpose->num_rows() + 1; ++i) {    transpose_rows[i] += transpose_rows[i - 1];  }  for (int r = 0; r < num_rows(); ++r) {    for (int idx = rows_[r]; idx < rows_[r + 1]; ++idx) {      const int c = cols_[idx];      const int transpose_idx = transpose_rows[c]++;      transpose_cols[transpose_idx] = r;      transpose_values[transpose_idx] = values_[idx];    }  }  for (int i = transpose->num_rows() - 1; i > 0 ; --i) {    transpose_rows[i] = transpose_rows[i - 1];  }  transpose_rows[0] = 0;  return transpose;}}  // namespace internal}  // namespace ceres
 |