implicit_schur_complement.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // 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. #ifndef CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
  34. #define CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_
  35. #include <memory>
  36. #include "ceres/internal/disable_warnings.h"
  37. #include "ceres/internal/eigen.h"
  38. #include "ceres/internal/export.h"
  39. #include "ceres/linear_operator.h"
  40. #include "ceres/linear_solver.h"
  41. #include "ceres/partitioned_matrix_view.h"
  42. #include "ceres/types.h"
  43. namespace ceres::internal {
  44. class BlockSparseMatrix;
  45. // This class implements various linear algebraic operations related
  46. // to the Schur complement without explicitly forming it.
  47. //
  48. //
  49. // Given a reactangular linear system Ax = b, where
  50. //
  51. // A = [E F]
  52. //
  53. // The normal equations are given by
  54. //
  55. // A'Ax = A'b
  56. //
  57. // |E'E E'F||y| = |E'b|
  58. // |F'E F'F||z| |F'b|
  59. //
  60. // and the Schur complement system is given by
  61. //
  62. // [F'F - F'E (E'E)^-1 E'F] z = F'b - F'E (E'E)^-1 E'b
  63. //
  64. // Now if we wish to solve Ax = b in the least squares sense, one way
  65. // is to form this Schur complement system and solve it using
  66. // Preconditioned Conjugate Gradients.
  67. //
  68. // The key operation in a conjugate gradient solver is the evaluation of the
  69. // matrix vector product with the Schur complement
  70. //
  71. // S = F'F - F'E (E'E)^-1 E'F
  72. //
  73. // It is straightforward to see that matrix vector products with S can
  74. // be evaluated without storing S in memory. Instead, given (E'E)^-1
  75. // (which for our purposes is an easily inverted block diagonal
  76. // matrix), it can be done in terms of matrix vector products with E,
  77. // F and (E'E)^-1. This class implements this functionality and other
  78. // auxiliary bits needed to implement a CG solver on the Schur
  79. // complement using the PartitionedMatrixView object.
  80. //
  81. // THREAD SAFETY: This class is not thread safe. In particular, the
  82. // RightMultiplyAndAccumulate (and the LeftMultiplyAndAccumulate) methods are
  83. // not thread safe as they depend on mutable arrays used for the temporaries
  84. // needed to compute the product y += Sx;
  85. class CERES_NO_EXPORT ImplicitSchurComplement final : public LinearOperator {
  86. public:
  87. // num_eliminate_blocks is the number of E blocks in the matrix
  88. // A.
  89. //
  90. // preconditioner indicates whether the inverse of the matrix F'F
  91. // should be computed or not as a preconditioner for the Schur
  92. // Complement.
  93. //
  94. // TODO(sameeragarwal): Get rid of the two bools below and replace
  95. // them with enums.
  96. explicit ImplicitSchurComplement(const LinearSolver::Options& options);
  97. // Initialize the Schur complement for a linear least squares
  98. // problem of the form
  99. //
  100. // |A | x = |b|
  101. // |diag(D)| |0|
  102. //
  103. // If D is null, then it is treated as a zero dimensional matrix. It
  104. // is important that the matrix A have a BlockStructure object
  105. // associated with it and has a block structure that is compatible
  106. // with the SchurComplement solver.
  107. void Init(const BlockSparseMatrix& A, const double* D, const double* b);
  108. // y += Sx, where S is the Schur complement.
  109. void RightMultiplyAndAccumulate(const double* x, double* y) const final;
  110. // The Schur complement is a symmetric positive definite matrix,
  111. // thus the left and right multiply operators are the same.
  112. void LeftMultiplyAndAccumulate(const double* x, double* y) const final {
  113. RightMultiplyAndAccumulate(x, y);
  114. }
  115. // Following is useful for approximation of S^-1 via power series expansion.
  116. // Z = (F'F)^-1 F'E (E'E)^-1 E'F
  117. // y += Zx
  118. void InversePowerSeriesOperatorRightMultiplyAccumulate(const double* x,
  119. double* y) const;
  120. // y = (E'E)^-1 (E'b - E'F x). Given an estimate of the solution to
  121. // the Schur complement system, this method computes the value of
  122. // the e_block variables that were eliminated to form the Schur
  123. // complement.
  124. void BackSubstitute(const double* x, double* y);
  125. int num_rows() const final { return A_->num_cols_f(); }
  126. int num_cols() const final { return A_->num_cols_f(); }
  127. const Vector& rhs() const { return rhs_; }
  128. const BlockSparseMatrix* block_diagonal_EtE_inverse() const {
  129. return block_diagonal_EtE_inverse_.get();
  130. }
  131. const BlockSparseMatrix* block_diagonal_FtF_inverse() const {
  132. CHECK(compute_ftf_inverse_);
  133. return block_diagonal_FtF_inverse_.get();
  134. }
  135. private:
  136. void AddDiagonalAndInvert(const double* D, BlockSparseMatrix* matrix);
  137. void UpdateRhs();
  138. const LinearSolver::Options& options_;
  139. bool compute_ftf_inverse_ = false;
  140. std::unique_ptr<PartitionedMatrixViewBase> A_;
  141. const double* D_ = nullptr;
  142. const double* b_ = nullptr;
  143. std::unique_ptr<BlockSparseMatrix> block_diagonal_EtE_inverse_;
  144. std::unique_ptr<BlockSparseMatrix> block_diagonal_FtF_inverse_;
  145. Vector rhs_;
  146. // Temporary storage vectors used to implement RightMultiplyAndAccumulate.
  147. mutable Vector tmp_rows_;
  148. mutable Vector tmp_e_cols_;
  149. mutable Vector tmp_e_cols_2_;
  150. mutable Vector tmp_f_cols_;
  151. };
  152. } // namespace ceres::internal
  153. #include "ceres/internal/reenable_warnings.h"
  154. #endif // CERES_INTERNAL_IMPLICIT_SCHUR_COMPLEMENT_H_