10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// An iterative solver for solving the Schur complement/reduced camera
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// linear system that arise in SfM problems.
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/linear_operator.h"
3879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/linear_solver.h"
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/partitioned_matrix_view.h"
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/eigen.h"
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/scoped_ptr.h"
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h"
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass BlockSparseMatrix;
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// This class implements various linear algebraic operations related
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// to the Schur complement without explicitly forming it.
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Given a reactangular linear system Ax = b, where
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   A = [E F]
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The normal equations are given by
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   A'Ax = A'b
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//  |E'E E'F||y| = |E'b|
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//  |F'E F'F||z|   |F'b|
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and the Schur complement system is given by
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//  [F'F - F'E (E'E)^-1 E'F] z = F'b - F'E (E'E)^-1 E'b
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Now if we wish to solve Ax = b in the least squares sense, one way
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// is to form this Schur complement system and solve it using
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Preconditioned Conjugate Gradients.
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The key operation in a conjugate gradient solver is the evaluation of the
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// matrix vector product with the Schur complement
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   S = F'F - F'E (E'E)^-1 E'F
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// It is straightforward to see that matrix vector products with S can
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// be evaluated without storing S in memory. Instead, given (E'E)^-1
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// (which for our purposes is an easily inverted block diagonal
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// matrix), it can be done in terms of matrix vector products with E,
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// F and (E'E)^-1. This class implements this functionality and other
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// auxilliary bits needed to implement a CG solver on the Schur
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// complement using the PartitionedMatrixView object.
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THREAD SAFETY: This class is nqot thread safe. In particular, the
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// RightMultiply (and the LeftMultiply) methods are not thread safe as
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// they depend on mutable arrays used for the temporaries needed to
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// compute the product y += Sx;
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass ImplicitSchurComplement : public LinearOperator {
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // num_eliminate_blocks is the number of E blocks in the matrix
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // A.
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // preconditioner indicates whether the inverse of the matrix F'F
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // should be computed or not as a preconditioner for the Schur
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Complement.
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // TODO(sameeragarwal): Get rid of the two bools below and replace
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // them with enums.
10079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  ImplicitSchurComplement(const LinearSolver::Options& options);
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual ~ImplicitSchurComplement();
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Initialize the Schur complement for a linear least squares
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // problem of the form
1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //   |A      | x = |b|
1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //   |diag(D)|     |0|
1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  //
1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // If D is null, then it is treated as a zero dimensional matrix. It
1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // is important that the matrix A have a BlockStructure object
1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // associated with it and has a block structure that is compatible
1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // with the SchurComplement solver.
1131d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  void Init(const BlockSparseMatrix& A, const double* D, const double* b);
1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // y += Sx, where S is the Schur complement.
1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual void RightMultiply(const double* x, double* y) const;
1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // The Schur complement is a symmetric positive definite matrix,
1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // thus the left and right multiply operators are the same.
1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual void LeftMultiply(const double* x, double* y) const {
1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    RightMultiply(x, y);
1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // y = (E'E)^-1 (E'b - E'F x). Given an estimate of the solution to
1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // the Schur complement system, this method computes the value of
1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // the e_block variables that were eliminated to form the Schur
1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // complement.
1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void BackSubstitute(const double* x, double* y);
1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual int num_rows() const { return A_->num_cols_f(); }
1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual int num_cols() const { return A_->num_cols_f(); }
1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const Vector& rhs()    const { return rhs_;             }
1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const BlockSparseMatrix* block_diagonal_EtE_inverse() const {
1350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    return block_diagonal_EtE_inverse_.get();
1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const BlockSparseMatrix* block_diagonal_FtF_inverse() const {
1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    return block_diagonal_FtF_inverse_.get();
1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private:
1430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void AddDiagonalAndInvert(const double* D, BlockSparseMatrix* matrix);
1440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void UpdateRhs();
1450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
14679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  const LinearSolver::Options& options_;
1470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
14879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  scoped_ptr<PartitionedMatrixViewBase> A_;
1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const double* D_;
1500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  const double* b_;
1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  scoped_ptr<BlockSparseMatrix> block_diagonal_EtE_inverse_;
1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  scoped_ptr<BlockSparseMatrix> block_diagonal_FtF_inverse_;
1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Vector rhs_;
1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Temporary storage vectors used to implement RightMultiply.
1580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  mutable Vector tmp_rows_;
1590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  mutable Vector tmp_e_cols_;
1600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  mutable Vector tmp_e_cols_2_;
1610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  mutable Vector tmp_f_cols_;
1620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
1630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
1650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
1660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
168