blob: b4eb0b072aa5089c875ad45e5e1a0a54d77f5e90 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// http://ceres-solver.org/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: sameeragarwal@google.com (Sameer Agarwal)
30//
31// An iterative solver for solving the Schur complement/reduced camera
32// linear system that arise in SfM problems.
33
34#ifndef CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
35#define CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
36
37#include <memory>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080038
Austin Schuh3de38b02024-06-25 18:25:10 -070039#include "ceres/internal/disable_warnings.h"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080040#include "ceres/internal/eigen.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070041#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include "ceres/linear_operator.h"
43#include "ceres/linear_solver.h"
44#include "ceres/partitioned_matrix_view.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080045#include "ceres/types.h"
46
Austin Schuh3de38b02024-06-25 18:25:10 -070047namespace ceres::internal {
Austin Schuh70cc9552019-01-21 19:46:48 -080048
49class BlockSparseMatrix;
50
51// This class implements various linear algebraic operations related
52// to the Schur complement without explicitly forming it.
53//
54//
55// Given a reactangular linear system Ax = b, where
56//
57// A = [E F]
58//
59// The normal equations are given by
60//
61// A'Ax = A'b
62//
63// |E'E E'F||y| = |E'b|
64// |F'E F'F||z| |F'b|
65//
66// and the Schur complement system is given by
67//
68// [F'F - F'E (E'E)^-1 E'F] z = F'b - F'E (E'E)^-1 E'b
69//
70// Now if we wish to solve Ax = b in the least squares sense, one way
71// is to form this Schur complement system and solve it using
72// Preconditioned Conjugate Gradients.
73//
74// The key operation in a conjugate gradient solver is the evaluation of the
75// matrix vector product with the Schur complement
76//
77// S = F'F - F'E (E'E)^-1 E'F
78//
79// It is straightforward to see that matrix vector products with S can
80// be evaluated without storing S in memory. Instead, given (E'E)^-1
81// (which for our purposes is an easily inverted block diagonal
82// matrix), it can be done in terms of matrix vector products with E,
83// F and (E'E)^-1. This class implements this functionality and other
Austin Schuh3de38b02024-06-25 18:25:10 -070084// auxiliary bits needed to implement a CG solver on the Schur
Austin Schuh70cc9552019-01-21 19:46:48 -080085// complement using the PartitionedMatrixView object.
86//
Austin Schuh3de38b02024-06-25 18:25:10 -070087// THREAD SAFETY: This class is not thread safe. In particular, the
88// RightMultiplyAndAccumulate (and the LeftMultiplyAndAccumulate) methods are
89// not thread safe as they depend on mutable arrays used for the temporaries
90// needed to compute the product y += Sx;
91class CERES_NO_EXPORT ImplicitSchurComplement final : public LinearOperator {
Austin Schuh70cc9552019-01-21 19:46:48 -080092 public:
93 // num_eliminate_blocks is the number of E blocks in the matrix
94 // A.
95 //
96 // preconditioner indicates whether the inverse of the matrix F'F
97 // should be computed or not as a preconditioner for the Schur
98 // Complement.
99 //
100 // TODO(sameeragarwal): Get rid of the two bools below and replace
101 // them with enums.
102 explicit ImplicitSchurComplement(const LinearSolver::Options& options);
Austin Schuh70cc9552019-01-21 19:46:48 -0800103
104 // Initialize the Schur complement for a linear least squares
105 // problem of the form
106 //
107 // |A | x = |b|
108 // |diag(D)| |0|
109 //
110 // If D is null, then it is treated as a zero dimensional matrix. It
111 // is important that the matrix A have a BlockStructure object
112 // associated with it and has a block structure that is compatible
113 // with the SchurComplement solver.
114 void Init(const BlockSparseMatrix& A, const double* D, const double* b);
115
116 // y += Sx, where S is the Schur complement.
Austin Schuh3de38b02024-06-25 18:25:10 -0700117 void RightMultiplyAndAccumulate(const double* x, double* y) const final;
Austin Schuh70cc9552019-01-21 19:46:48 -0800118
119 // The Schur complement is a symmetric positive definite matrix,
120 // thus the left and right multiply operators are the same.
Austin Schuh3de38b02024-06-25 18:25:10 -0700121 void LeftMultiplyAndAccumulate(const double* x, double* y) const final {
122 RightMultiplyAndAccumulate(x, y);
Austin Schuh70cc9552019-01-21 19:46:48 -0800123 }
124
Austin Schuh3de38b02024-06-25 18:25:10 -0700125 // Following is useful for approximation of S^-1 via power series expansion.
126 // Z = (F'F)^-1 F'E (E'E)^-1 E'F
127 // y += Zx
128 void InversePowerSeriesOperatorRightMultiplyAccumulate(const double* x,
129 double* y) const;
130
Austin Schuh70cc9552019-01-21 19:46:48 -0800131 // y = (E'E)^-1 (E'b - E'F x). Given an estimate of the solution to
132 // the Schur complement system, this method computes the value of
133 // the e_block variables that were eliminated to form the Schur
134 // complement.
135 void BackSubstitute(const double* x, double* y);
136
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800137 int num_rows() const final { return A_->num_cols_f(); }
138 int num_cols() const final { return A_->num_cols_f(); }
139 const Vector& rhs() const { return rhs_; }
Austin Schuh70cc9552019-01-21 19:46:48 -0800140
141 const BlockSparseMatrix* block_diagonal_EtE_inverse() const {
142 return block_diagonal_EtE_inverse_.get();
143 }
144
145 const BlockSparseMatrix* block_diagonal_FtF_inverse() const {
Austin Schuh3de38b02024-06-25 18:25:10 -0700146 CHECK(compute_ftf_inverse_);
Austin Schuh70cc9552019-01-21 19:46:48 -0800147 return block_diagonal_FtF_inverse_.get();
148 }
149
150 private:
151 void AddDiagonalAndInvert(const double* D, BlockSparseMatrix* matrix);
152 void UpdateRhs();
153
154 const LinearSolver::Options& options_;
Austin Schuh3de38b02024-06-25 18:25:10 -0700155 bool compute_ftf_inverse_ = false;
Austin Schuh70cc9552019-01-21 19:46:48 -0800156 std::unique_ptr<PartitionedMatrixViewBase> A_;
Austin Schuh3de38b02024-06-25 18:25:10 -0700157 const double* D_ = nullptr;
158 const double* b_ = nullptr;
Austin Schuh70cc9552019-01-21 19:46:48 -0800159
160 std::unique_ptr<BlockSparseMatrix> block_diagonal_EtE_inverse_;
161 std::unique_ptr<BlockSparseMatrix> block_diagonal_FtF_inverse_;
162
163 Vector rhs_;
164
Austin Schuh3de38b02024-06-25 18:25:10 -0700165 // Temporary storage vectors used to implement RightMultiplyAndAccumulate.
Austin Schuh70cc9552019-01-21 19:46:48 -0800166 mutable Vector tmp_rows_;
167 mutable Vector tmp_e_cols_;
168 mutable Vector tmp_e_cols_2_;
169 mutable Vector tmp_f_cols_;
170};
171
Austin Schuh3de38b02024-06-25 18:25:10 -0700172} // namespace ceres::internal
173
174#include "ceres/internal/reenable_warnings.h"
Austin Schuh70cc9552019-01-21 19:46:48 -0800175
176#endif // CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_