Blame internal/ceres/compressed_col_sparse_matrix_utils.h

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
#ifndef CERES_INTERNAL_COMPRESSED_COL_SPARSE_MATRIX_UTILS_H_
Packit ea1746
#define CERES_INTERNAL_COMPRESSED_COL_SPARSE_MATRIX_UTILS_H_
Packit ea1746
Packit ea1746
#include <vector>
Packit ea1746
#include "ceres/internal/port.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
// Extract the block sparsity pattern of the scalar compressed columns
Packit ea1746
// matrix and return it in compressed column form. The compressed
Packit ea1746
// column form is stored in two vectors block_rows, and block_cols,
Packit ea1746
// which correspond to the row and column arrays in a compressed
Packit ea1746
// column sparse matrix.
Packit ea1746
//
Packit ea1746
// If c_ij is the block in the matrix A corresponding to row block i
Packit ea1746
// and column block j, then it is expected that A contains at least
Packit ea1746
// one non-zero entry corresponding to the top left entry of c_ij,
Packit ea1746
// as that entry is used to detect the presence of a non-zero c_ij.
Packit ea1746
void CompressedColumnScalarMatrixToBlockMatrix(
Packit ea1746
    const int* scalar_rows,
Packit ea1746
    const int* scalar_cols,
Packit ea1746
    const std::vector<int>& row_blocks,
Packit ea1746
    const std::vector<int>& col_blocks,
Packit ea1746
    std::vector<int>* block_rows,
Packit ea1746
    std::vector<int>* block_cols);
Packit ea1746
Packit ea1746
// Given a set of blocks and a permutation of these blocks, compute
Packit ea1746
// the corresponding "scalar" ordering, where the scalar ordering of
Packit ea1746
// size sum(blocks).
Packit ea1746
void BlockOrderingToScalarOrdering(
Packit ea1746
    const std::vector<int>& blocks,
Packit ea1746
    const std::vector<int>& block_ordering,
Packit ea1746
    std::vector<int>* scalar_ordering);
Packit ea1746
Packit ea1746
// Solve the linear system
Packit ea1746
//
Packit ea1746
//   R * solution = rhs
Packit ea1746
//
Packit ea1746
// Where R is an upper triangular compressed column sparse matrix.
Packit ea1746
template <typename IntegerType>
Packit ea1746
void SolveUpperTriangularInPlace(IntegerType num_cols,
Packit ea1746
                                 const IntegerType* rows,
Packit ea1746
                                 const IntegerType* cols,
Packit ea1746
                                 const double* values,
Packit ea1746
                                 double* rhs_and_solution) {
Packit ea1746
  for (IntegerType c = num_cols - 1; c >= 0; --c) {
Packit ea1746
    rhs_and_solution[c] /= values[cols[c + 1] - 1];
Packit ea1746
    for (IntegerType idx = cols[c]; idx < cols[c + 1] - 1; ++idx) {
Packit ea1746
      const IntegerType r = rows[idx];
Packit ea1746
      const double v = values[idx];
Packit ea1746
      rhs_and_solution[r] -= v * rhs_and_solution[c];
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
// Solve the linear system
Packit ea1746
//
Packit ea1746
//   R' * solution = rhs
Packit ea1746
//
Packit ea1746
// Where R is an upper triangular compressed column sparse matrix.
Packit ea1746
template <typename IntegerType>
Packit ea1746
void SolveUpperTriangularTransposeInPlace(IntegerType num_cols,
Packit ea1746
                                          const IntegerType* rows,
Packit ea1746
                                          const IntegerType* cols,
Packit ea1746
                                          const double* values,
Packit ea1746
                                          double* rhs_and_solution) {
Packit ea1746
  for (IntegerType c = 0; c < num_cols; ++c) {
Packit ea1746
    for (IntegerType idx = cols[c]; idx < cols[c + 1] - 1; ++idx) {
Packit ea1746
      const IntegerType r = rows[idx];
Packit ea1746
      const double v = values[idx];
Packit ea1746
      rhs_and_solution[c] -= v * rhs_and_solution[r];
Packit ea1746
    }
Packit ea1746
    rhs_and_solution[c] =  rhs_and_solution[c] / values[cols[c + 1] - 1];
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
// Given a upper triangular matrix R in compressed column form, solve
Packit ea1746
// the linear system,
Packit ea1746
//
Packit ea1746
//  R'R x = b
Packit ea1746
//
Packit ea1746
// Where b is all zeros except for rhs_nonzero_index, where it is
Packit ea1746
// equal to one.
Packit ea1746
//
Packit ea1746
// The function exploits this knowledge to reduce the number of
Packit ea1746
// floating point operations.
Packit ea1746
template <typename IntegerType>
Packit ea1746
void SolveRTRWithSparseRHS(IntegerType num_cols,
Packit ea1746
                           const IntegerType* rows,
Packit ea1746
                           const IntegerType* cols,
Packit ea1746
                           const double* values,
Packit ea1746
                           const int rhs_nonzero_index,
Packit ea1746
                           double* solution) {
Packit ea1746
  std::fill(solution, solution + num_cols, 0.0);
Packit ea1746
  solution[rhs_nonzero_index] = 1.0 / values[cols[rhs_nonzero_index + 1] - 1];
Packit ea1746
Packit ea1746
  for (IntegerType c = rhs_nonzero_index + 1; c < num_cols; ++c) {
Packit ea1746
    for (IntegerType idx = cols[c]; idx < cols[c + 1] - 1; ++idx) {
Packit ea1746
      const IntegerType r = rows[idx];
Packit ea1746
      if (r < rhs_nonzero_index) continue;
Packit ea1746
      const double v = values[idx];
Packit ea1746
      solution[c] -= v * solution[r];
Packit ea1746
    }
Packit ea1746
    solution[c] =  solution[c] / values[cols[c + 1] - 1];
Packit ea1746
  }
Packit ea1746
Packit ea1746
  SolveUpperTriangularInPlace(num_cols, rows, cols, values, solution);
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_COMPRESSED_COL_SPARSE_MATRIX_UTILS_H_