Eigen  5.0.1
 
Loading...
Searching...
No Matches
LeastSquareConjugateGradient.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
12#define EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
30template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
31EIGEN_DONT_INLINE void least_square_conjugate_gradient(const MatrixType& mat, const Rhs& rhs, Dest& x,
32 const Preconditioner& precond, Index& iters,
33 typename Dest::RealScalar& tol_error) {
34 using std::sqrt;
35 using RealScalar = typename Dest::RealScalar;
36 using Scalar = typename Dest::Scalar;
37 using VectorType = Matrix<Scalar, Dynamic, 1>;
38
39 RealScalar tol = tol_error;
40 Index maxIters = iters;
41
42 Index m = mat.rows(), n = mat.cols();
43
44 VectorType residual = rhs - mat * x;
45 VectorType normal_residual = mat.adjoint() * residual;
46 VectorType normal_rhs = mat.adjoint() * rhs;
47
48 RealScalar rhsNorm = normal_rhs.stableNorm();
49 if (rhsNorm == 0) {
50 x.setZero();
51 iters = 0;
52 tol_error = 0;
53 return;
54 }
55 RealScalar threshold = tol * rhsNorm;
56 RealScalar residualNorm = normal_residual.stableNorm();
57 if (residualNorm == 0 || residualNorm < threshold) {
58 iters = 0;
59 tol_error = residualNorm / rhsNorm;
60 return;
61 }
62
63 // Keep the quadratic recurrence terms representable for very small or large residuals.
64 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
65 normal_residual /= residualScale;
66 threshold /= residualScale;
67
68 VectorType p(n);
69 p = precond.solve(normal_residual); // initial search direction
70
71 VectorType z(n), tmp(m);
72 RealScalar absNew = numext::real(normal_residual.dot(p)); // the square of the absolute value of r scaled by invM
73 Index i = 0;
74 while (i < maxIters) {
75 tmp.noalias() = mat * p;
76
77 Scalar alpha = absNew / tmp.squaredNorm(); // the amount we travel on dir
78 x += (residualScale * alpha) * p; // update solution
79 residual -= (residualScale * alpha) * tmp; // update residual
80 normal_residual.noalias() = mat.adjoint() * residual; // update residual of the normal equation
81 normal_residual /= residualScale;
82
83 residualNorm = normal_residual.stableNorm();
84 if (residualNorm < threshold) break;
85
86 z = precond.solve(normal_residual); // approximately solve for "A'A z = normal_residual"
87
88 RealScalar absOld = absNew;
89 absNew = numext::real(normal_residual.dot(z)); // update the absolute value of r
90 RealScalar beta = absNew / absOld; // calculate the Gram-Schmidt value used to create the new search direction
91 p = z + beta * p; // update search direction
92 i++;
93 }
94 tol_error = residualNorm / (rhsNorm / residualScale);
95 iters = i;
96}
97
98} // namespace internal
99
100template <typename MatrixType_,
103
104namespace internal {
105
106template <typename MatrixType_, typename Preconditioner_>
107struct traits<LeastSquaresConjugateGradient<MatrixType_, Preconditioner_> > {
108 using MatrixType = MatrixType_;
109 using Preconditioner = Preconditioner_;
110};
111
112} // namespace internal
113
152template <typename MatrixType_, typename Preconditioner_>
154 : public IterativeSolverBase<LeastSquaresConjugateGradient<MatrixType_, Preconditioner_> > {
155 protected:
157 using Base::m_error;
158 using Base::m_info;
159 using Base::m_isInitialized;
160 using Base::m_iterations;
161 using Base::matrix;
162
163 public:
164 using MatrixType = MatrixType_;
165 using Scalar = typename MatrixType::Scalar;
166 using RealScalar = typename MatrixType::RealScalar;
167 using Preconditioner = Preconditioner_;
168
169 public:
172
183 template <typename MatrixDerived>
184 explicit LeastSquaresConjugateGradient(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
185
187 template <typename Rhs, typename Dest>
188 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
189 m_iterations = Base::maxIterations();
190 m_error = Base::m_tolerance;
191
192 internal::least_square_conjugate_gradient(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
193 m_info = m_error <= Base::m_tolerance ? Success : NoConvergence;
194 }
195};
196
197} // end namespace Eigen
198
199#endif // EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
Index maxIterations() const
Definition IterativeSolverBase.h:245
Jacobi preconditioner for LeastSquaresConjugateGradient.
Definition BasicPreconditioners.h:122
A conjugate gradient solver for sparse (or dense) least-square problems.
Definition LeastSquareConjugateGradient.h:154
LeastSquaresConjugateGradient()
Definition LeastSquareConjugateGradient.h:171
LeastSquaresConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition LeastSquareConjugateGradient.h:184
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34