Blame internal/ceres/implicit_schur_complement.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
// An iterative solver for solving the Schur complement/reduced camera
Packit ea1746
// linear system that arise in SfM problems.
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
Packit ea1746
#define CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
Packit ea1746
Packit ea1746
#include "ceres/linear_operator.h"
Packit ea1746
#include "ceres/linear_solver.h"
Packit ea1746
#include "ceres/partitioned_matrix_view.h"
Packit ea1746
#include "ceres/internal/eigen.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class BlockSparseMatrix;
Packit ea1746
Packit ea1746
// This class implements various linear algebraic operations related
Packit ea1746
// to the Schur complement without explicitly forming it.
Packit ea1746
//
Packit ea1746
//
Packit ea1746
// Given a reactangular linear system Ax = b, where
Packit ea1746
//
Packit ea1746
//   A = [E F]
Packit ea1746
//
Packit ea1746
// The normal equations are given by
Packit ea1746
//
Packit ea1746
//   A'Ax = A'b
Packit ea1746
//
Packit ea1746
//  |E'E E'F||y| = |E'b|
Packit ea1746
//  |F'E F'F||z|   |F'b|
Packit ea1746
//
Packit ea1746
// and the Schur complement system is given by
Packit ea1746
//
Packit ea1746
//  [F'F - F'E (E'E)^-1 E'F] z = F'b - F'E (E'E)^-1 E'b
Packit ea1746
//
Packit ea1746
// Now if we wish to solve Ax = b in the least squares sense, one way
Packit ea1746
// is to form this Schur complement system and solve it using
Packit ea1746
// Preconditioned Conjugate Gradients.
Packit ea1746
//
Packit ea1746
// The key operation in a conjugate gradient solver is the evaluation of the
Packit ea1746
// matrix vector product with the Schur complement
Packit ea1746
//
Packit ea1746
//   S = F'F - F'E (E'E)^-1 E'F
Packit ea1746
//
Packit ea1746
// It is straightforward to see that matrix vector products with S can
Packit ea1746
// be evaluated without storing S in memory. Instead, given (E'E)^-1
Packit ea1746
// (which for our purposes is an easily inverted block diagonal
Packit ea1746
// matrix), it can be done in terms of matrix vector products with E,
Packit ea1746
// F and (E'E)^-1. This class implements this functionality and other
Packit ea1746
// auxilliary bits needed to implement a CG solver on the Schur
Packit ea1746
// complement using the PartitionedMatrixView object.
Packit ea1746
//
Packit ea1746
// THREAD SAFETY: This class is nqot thread safe. In particular, the
Packit ea1746
// RightMultiply (and the LeftMultiply) methods are not thread safe as
Packit ea1746
// they depend on mutable arrays used for the temporaries needed to
Packit ea1746
// compute the product y += Sx;
Packit ea1746
class ImplicitSchurComplement : public LinearOperator {
Packit ea1746
 public:
Packit ea1746
  // num_eliminate_blocks is the number of E blocks in the matrix
Packit ea1746
  // A.
Packit ea1746
  //
Packit ea1746
  // preconditioner indicates whether the inverse of the matrix F'F
Packit ea1746
  // should be computed or not as a preconditioner for the Schur
Packit ea1746
  // Complement.
Packit ea1746
  //
Packit ea1746
  // TODO(sameeragarwal): Get rid of the two bools below and replace
Packit ea1746
  // them with enums.
Packit ea1746
  explicit ImplicitSchurComplement(const LinearSolver::Options& options);
Packit ea1746
  virtual ~ImplicitSchurComplement();
Packit ea1746
Packit ea1746
  // Initialize the Schur complement for a linear least squares
Packit ea1746
  // problem of the form
Packit ea1746
  //
Packit ea1746
  //   |A      | x = |b|
Packit ea1746
  //   |diag(D)|     |0|
Packit ea1746
  //
Packit ea1746
  // If D is null, then it is treated as a zero dimensional matrix. It
Packit ea1746
  // is important that the matrix A have a BlockStructure object
Packit ea1746
  // associated with it and has a block structure that is compatible
Packit ea1746
  // with the SchurComplement solver.
Packit ea1746
  void Init(const BlockSparseMatrix& A, const double* D, const double* b);
Packit ea1746
Packit ea1746
  // y += Sx, where S is the Schur complement.
Packit ea1746
  virtual void RightMultiply(const double* x, double* y) const;
Packit ea1746
Packit ea1746
  // The Schur complement is a symmetric positive definite matrix,
Packit ea1746
  // thus the left and right multiply operators are the same.
Packit ea1746
  virtual void LeftMultiply(const double* x, double* y) const {
Packit ea1746
    RightMultiply(x, y);
Packit ea1746
  }
Packit ea1746
Packit ea1746
  // y = (E'E)^-1 (E'b - E'F x). Given an estimate of the solution to
Packit ea1746
  // the Schur complement system, this method computes the value of
Packit ea1746
  // the e_block variables that were eliminated to form the Schur
Packit ea1746
  // complement.
Packit ea1746
  void BackSubstitute(const double* x, double* y);
Packit ea1746
Packit ea1746
  virtual int num_rows() const { return A_->num_cols_f(); }
Packit ea1746
  virtual int num_cols() const { return A_->num_cols_f(); }
Packit ea1746
  const Vector& rhs()    const { return rhs_;             }
Packit ea1746
Packit ea1746
  const BlockSparseMatrix* block_diagonal_EtE_inverse() const {
Packit ea1746
    return block_diagonal_EtE_inverse_.get();
Packit ea1746
  }
Packit ea1746
Packit ea1746
  const BlockSparseMatrix* block_diagonal_FtF_inverse() const {
Packit ea1746
    return block_diagonal_FtF_inverse_.get();
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  void AddDiagonalAndInvert(const double* D, BlockSparseMatrix* matrix);
Packit ea1746
  void UpdateRhs();
Packit ea1746
Packit ea1746
  const LinearSolver::Options& options_;
Packit ea1746
Packit ea1746
  scoped_ptr<PartitionedMatrixViewBase> A_;
Packit ea1746
  const double* D_;
Packit ea1746
  const double* b_;
Packit ea1746
Packit ea1746
  scoped_ptr<BlockSparseMatrix> block_diagonal_EtE_inverse_;
Packit ea1746
  scoped_ptr<BlockSparseMatrix> block_diagonal_FtF_inverse_;
Packit ea1746
Packit ea1746
  Vector rhs_;
Packit ea1746
Packit ea1746
  // Temporary storage vectors used to implement RightMultiply.
Packit ea1746
  mutable Vector tmp_rows_;
Packit ea1746
  mutable Vector tmp_e_cols_;
Packit ea1746
  mutable Vector tmp_e_cols_2_;
Packit ea1746
  mutable Vector tmp_f_cols_;
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_