Blame internal/ceres/compressed_col_sparse_matrix_utils.cc

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 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_col_sparse_matrix_utils.h"
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include <algorithm>
Packit ea1746
#include "ceres/internal/port.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
void CompressedColumnScalarMatrixToBlockMatrix(
Packit ea1746
    const int* scalar_rows,
Packit ea1746
    const int* scalar_cols,
Packit ea1746
    const vector<int>& row_blocks,
Packit ea1746
    const vector<int>& col_blocks,
Packit ea1746
    vector<int>* block_rows,
Packit ea1746
    vector<int>* block_cols) {
Packit ea1746
  CHECK_NOTNULL(block_rows)->clear();
Packit ea1746
  CHECK_NOTNULL(block_cols)->clear();
Packit ea1746
  const int num_row_blocks = row_blocks.size();
Packit ea1746
  const int num_col_blocks = col_blocks.size();
Packit ea1746
Packit ea1746
  vector<int> row_block_starts(num_row_blocks);
Packit ea1746
  for (int i = 0, cursor = 0; i < num_row_blocks; ++i) {
Packit ea1746
    row_block_starts[i] = cursor;
Packit ea1746
    cursor += row_blocks[i];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // This loop extracts the block sparsity of the scalar sparse matrix
Packit ea1746
  // It does so by iterating over the columns, but only considering
Packit ea1746
  // the columns corresponding to the first element of each column
Packit ea1746
  // block. Within each column, the inner loop iterates over the rows,
Packit ea1746
  // and detects the presence of a row block by checking for the
Packit ea1746
  // presence of a non-zero entry corresponding to its first element.
Packit ea1746
  block_cols->push_back(0);
Packit ea1746
  int c = 0;
Packit ea1746
  for (int col_block = 0; col_block < num_col_blocks; ++col_block) {
Packit ea1746
    int column_size = 0;
Packit ea1746
    for (int idx = scalar_cols[c]; idx < scalar_cols[c + 1]; ++idx) {
Packit ea1746
      vector<int>::const_iterator it =
Packit ea1746
          std::lower_bound(row_block_starts.begin(),
Packit ea1746
                           row_block_starts.end(),
Packit ea1746
                           scalar_rows[idx]);
Packit ea1746
      // Since we are using lower_bound, it will return the row id
Packit ea1746
      // where the row block starts. For everything but the first row
Packit ea1746
      // of the block, where these values will be the same, we can
Packit ea1746
      // skip, as we only need the first row to detect the presence of
Packit ea1746
      // the block.
Packit ea1746
      //
Packit ea1746
      // For rows all but the first row in the last row block,
Packit ea1746
      // lower_bound will return row_block_starts.end(), but those can
Packit ea1746
      // be skipped like the rows in other row blocks too.
Packit ea1746
      if (it == row_block_starts.end() || *it != scalar_rows[idx]) {
Packit ea1746
        continue;
Packit ea1746
      }
Packit ea1746
Packit ea1746
      block_rows->push_back(it - row_block_starts.begin());
Packit ea1746
      ++column_size;
Packit ea1746
    }
Packit ea1746
    block_cols->push_back(block_cols->back() + column_size);
Packit ea1746
    c += col_blocks[col_block];
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
void BlockOrderingToScalarOrdering(const vector<int>& blocks,
Packit ea1746
                                   const vector<int>& block_ordering,
Packit ea1746
                                   vector<int>* scalar_ordering) {
Packit ea1746
  CHECK_EQ(blocks.size(), block_ordering.size());
Packit ea1746
  const int num_blocks = blocks.size();
Packit ea1746
Packit ea1746
  // block_starts = [0, block1, block1 + block2 ..]
Packit ea1746
  vector<int> block_starts(num_blocks);
Packit ea1746
  for (int i = 0, cursor = 0; i < num_blocks ; ++i) {
Packit ea1746
    block_starts[i] = cursor;
Packit ea1746
    cursor += blocks[i];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  scalar_ordering->resize(block_starts.back() + blocks.back());
Packit ea1746
  int cursor = 0;
Packit ea1746
  for (int i = 0; i < num_blocks; ++i) {
Packit ea1746
    const int block_id = block_ordering[i];
Packit ea1746
    const int block_size = blocks[block_id];
Packit ea1746
    int block_position = block_starts[block_id];
Packit ea1746
    for (int j = 0; j < block_size; ++j) {
Packit ea1746
      (*scalar_ordering)[cursor++] = block_position++;
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres