11#ifndef EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
12#define EIGEN_LEAST_SQUARE_CONJUGATE_GRADIENT_H
15#include "./InternalHeaderCheck.h"
26template <
typename RealScalar>
27RealScalar least_square_power_of_two_at_most(
const RealScalar& v) {
30 RealScalar factors[32];
32 for (RealScalar f = RealScalar(0.5); count < 32 && f > v; f = RealScalar(f * f)) factors[count++] = f;
34 for (
int k = count - 1; k >= 0; --k) {
35 const RealScalar t = RealScalar(s * factors[k]);
38 return RealScalar(s * RealScalar(0.5));
43template <
typename RealScalar,
typename MatrixType>
44auto least_square_dot_length(
const MatrixType& mat,
int) ->
decltype(RealScalar(mat.nonZeros())) {
45 return numext::maxi(RealScalar(1),
46 RealScalar(RealScalar(mat.nonZeros()) / RealScalar(numext::maxi(mat.cols(), Index(1)))));
48template <
typename RealScalar,
typename MatrixType>
49RealScalar least_square_dot_length(
const MatrixType& mat,
long) {
50 return RealScalar(mat.rows());
65template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
66EIGEN_DONT_INLINE
bool least_square_conjugate_gradient(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
67 const Preconditioner& precond, Index& iters,
68 typename Dest::RealScalar& tol_error) {
69 using RealScalar =
typename Dest::RealScalar;
70 using Scalar =
typename Dest::Scalar;
71 using VectorType = Matrix<Scalar, Dynamic, 1>;
73 RealScalar tol = tol_error;
74 Index maxIters = iters;
76 Index m = mat.rows(), n = mat.cols();
78 VectorType residual = rhs - mat * x;
79 VectorType normal_residual = mat.adjoint() * residual;
80 VectorType normal_rhs = mat.adjoint() * rhs;
82 RealScalar rhsNorm = normal_rhs.stableNorm();
86 RealScalar rhsScale(1);
87 if (rhsNorm < (std::numeric_limits<RealScalar>::min)()) {
88 const RealScalar bNorm = rhs.stableNorm();
89 if (bNorm > RealScalar(0) && bNorm < RealScalar(1)) {
90 const RealScalar scale = least_square_power_of_two_at_most(bNorm);
91 const VectorType scaledGuess = x / scale;
92 if (scaledGuess.allFinite()) {
95 residual = rhs / rhsScale - mat * x;
96 normal_residual = mat.adjoint() * residual;
97 normal_rhs = mat.adjoint() * (rhs / rhsScale);
98 rhsNorm = normal_rhs.stableNorm();
102 const auto restoreScale = [&]() {
103 if (rhsScale != RealScalar(1)) x *= rhsScale;
111 RealScalar threshold = tol * rhsNorm;
112 RealScalar residualNorm = normal_residual.stableNorm();
113 if (residualNorm == 0 || residualNorm < threshold) {
116 tol_error = residualNorm / rhsNorm;
121 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
122 normal_residual /= residualScale;
123 residualNorm /= residualScale;
126 threshold = internal::iterative_solver_threshold(tol, RealScalar(rhsNorm / residualScale),
127 internal::iterative_solver_residual_floor<RealScalar>());
130 p = precond.solve(normal_residual);
132 VectorType z(n), tmp(m);
133 RealScalar absNew = numext::real(normal_residual.dot(p));
140 const RealScalar eps = NumTraits<RealScalar>::epsilon();
141 const RealScalar sqrtLength = numext::sqrt(internal::least_square_dot_length<RealScalar>(mat, 0));
143 const auto gradientFloor = [&](RealScalar apNorm) {
144 const RealScalar pNorm2 = p.squaredNorm();
145 if (pNorm2 > RealScalar(0) && (numext::isfinite)(pNorm2))
146 normA = numext::maxi(normA, RealScalar(apNorm / numext::sqrt(pNorm2)));
147 const RealScalar rNorm2 = (residual / residualScale).squaredNorm();
148 const RealScalar floor = RealScalar(sqrtLength * eps * normA * numext::sqrt(rNorm2));
149 return (numext::isfinite)(floor) ? floor : RealScalar(0);
151 bool atFloor =
false;
152 while (i < maxIters) {
153 tmp.noalias() = mat * p;
156 const RealScalar tmpNorm2 = tmp.squaredNorm();
157 RealScalar apNorm = numext::sqrt(tmpNorm2);
158 RealScalar step = RealScalar(residualScale * (absNew / tmpNorm2));
159 if (!(tmpNorm2 >= (std::numeric_limits<RealScalar>::min)() && (numext::isfinite)(tmpNorm2)) ||
160 !(numext::isfinite)(step)) {
163 apNorm = tmp.stableNorm();
164 step = RealScalar(absNew / apNorm);
165 const RealScalar tail = RealScalar(residualScale / apNorm);
166 if (!(numext::isfinite)(step) || !(numext::isfinite)(tail) || !(numext::isfinite)(apNorm)) {
169 atFloor = residualNorm <= gradientFloor(apNorm);
172 x += step * (tail * p);
173 residual -= step * (tail * tmp);
176 residual -= step * tmp;
178 normal_residual.noalias() = mat.adjoint() * residual;
179 normal_residual /= residualScale;
181 residualNorm = normal_residual.stableNorm();
182 if (residualNorm < threshold)
break;
185 if (residualNorm <= gradientFloor(apNorm)) {
190 z = precond.solve(normal_residual);
192 RealScalar absOld = absNew;
193 absNew = numext::real(normal_residual.dot(z));
194 RealScalar beta = absNew / absOld;
199 tol_error = residualNorm / (rhsNorm / residualScale);
201 return tol_error <= tol || atFloor;
206template <
typename MatrixType_,
212template <
typename MatrixType_,
typename Preconditioner_>
213struct traits<LeastSquaresConjugateGradient<MatrixType_, Preconditioner_> > {
214 using MatrixType = MatrixType_;
215 using Preconditioner = Preconditioner_;
264template <
typename MatrixType_,
typename Preconditioner_>
266 :
public IterativeSolverBase<LeastSquaresConjugateGradient<MatrixType_, Preconditioner_> > {
271 using Base::m_isInitialized;
272 using Base::m_iterations;
276 using MatrixType = MatrixType_;
277 using Scalar =
typename MatrixType::Scalar;
278 using RealScalar =
typename MatrixType::RealScalar;
279 using Preconditioner = Preconditioner_;
295 template <
typename MatrixDerived>
299 template <
typename Rhs,
typename Dest>
300 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
302 m_error = Base::m_tolerance;
304 const bool converged =
305 internal::least_square_conjugate_gradient(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
IterativeSolverBase()
Definition IterativeSolverBase.h:194
Index maxIterations() const
Definition IterativeSolverBase.h:307
Jacobi preconditioner for LeastSquaresConjugateGradient.
Definition BasicPreconditioners.h:122
A conjugate gradient solver for sparse (or dense) least-square problems.
Definition LeastSquareConjugateGradient.h:266
LeastSquaresConjugateGradient()
Definition LeastSquareConjugateGradient.h:283
LeastSquaresConjugateGradient(const EigenBase< MatrixDerived > &A)
Definition LeastSquareConjugateGradient.h:296
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34