Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
LSMR.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// This Source Code Form is subject to the terms of the Mozilla
5// Public License v. 2.0. If a copy of the MPL was not distributed
6// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
7// SPDX-FileCopyrightText: The Eigen Authors
8// SPDX-License-Identifier: MPL-2.0
9
10#ifndef EIGEN_LSMR_H
11#define EIGEN_LSMR_H
12
13// LSMR is an iterative algorithm for least-squares problems min ||A x - b||,
14// based on the Golub-Kahan bidiagonalization process. It is analytically
15// equivalent to applying MINRES to the normal equation A^T A x = A^T b, so the
16// quantity ||A^T r_k|| decreases monotonically (where r_k = b - A x_k). In
17// practice ||r_k|| also decreases monotonically, which makes LSMR safer than
18// LSQR to stop early. With a damping parameter lambda > 0 it instead minimizes
19// || (A; lambda I) x - (b; 0) ||, i.e. it solves the regularized (Tikhonov)
20// least-squares problem.
21//
22// This implementation follows the published algorithm:
23//
24// D. C.-L. Fong and M. A. Saunders, "LSMR: An Iterative Algorithm for Sparse
25// Least-Squares Problems", SIAM J. Sci. Comput. 33(5):2950-2971, 2011.
26// https://web.stanford.edu/group/SOL/software/lsmr/
27//
28// The scalar recurrences, the cheap estimates of ||r||, ||A^T r||, ||A|| and
29// cond(A), and the stopping rules reproduce the reference Fortran 90
30// implementation by the same authors (distributed under the BSD / Common Public
31// License at the URL above). No source code from that implementation is
32// reproduced here; only the mathematical algorithm is implemented, in Eigen's
33// own idiom.
34
35// IWYU pragma: private
36#include "./InternalHeaderCheck.h"
37
38namespace Eigen {
39
40namespace internal {
41
76template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
77EIGEN_DONT_INLINE Index lsmr(const MatrixType& mat, const Rhs& rhs, Dest& x, const Preconditioner& precond,
78 Index& iters, typename Dest::RealScalar& tol_error, const typename Dest::RealScalar& atol,
79 const typename Dest::RealScalar& btol, const typename Dest::RealScalar& lambda,
80 const typename Dest::RealScalar& conlim) {
81 using RealScalar = typename Dest::RealScalar;
82 using Scalar = typename Dest::Scalar;
83 using VectorType = Matrix<Scalar, Dynamic, 1>;
84
85 const RealScalar zero(0);
86 const RealScalar one(1);
87
88 const Index n = mat.cols();
89 const Index maxIters = iters;
90
91 // n-vectors needed before the early-return below. u is the only m-vector.
92 VectorType v(n), Atu(n);
93
94 // The damped bidiagonalization below starts from (b - A x0; 0), which is the stacked residual of
95 // min ||A x - b||^2 + lambda^2 ||x - x0||^2 rather than of the documented problem, whose stacked residual
96 // (b - A x0; -lambda x0) would need M x0 as well. The damped minimizer is unique and does not depend on x0, so
97 // start from zero.
98 if (lambda > zero) x.setZero();
99
100 // Set up the first vectors u and v for the bidiagonalization. These satisfy
101 // beta*u = b - A*x0 and alpha*v = M^{-1} A^T u.
102 VectorType u = rhs - mat * x; // working residual r0 of the initial guess
103 RealScalar alpha = zero;
104 RealScalar beta = u.stableNorm();
105 if (beta > zero) {
106 u /= beta;
107 Atu.noalias() = mat.adjoint() * u;
108 v = precond.solve(Atu);
109 alpha = v.stableNorm();
110 if (alpha > zero) v /= alpha;
111 }
112
113 iters = 0;
114 // If b - A*x0 = 0 or A^T(b - A*x0) = 0 then the current x already solves the
115 // (least-squares) problem: no correction is needed.
116 if (alpha * beta == zero) {
117 tol_error = zero;
118 return 0; // istop = 0
119 }
120
121 if (maxIters <= 0) {
122 // No iteration is allowed: x is returned as given, with no reduction of ||A^T r|| / (||A|| ||r||) to report.
123 tol_error = one;
124 return 7;
125 }
126
127 // dx accumulates the correction (in z-space when a preconditioner is used).
128 // The remaining n-vectors are only needed once we start iterating.
129 VectorType dx = VectorType::Zero(n);
130 VectorType h = v;
131 VectorType hbar = VectorType::Zero(n);
132 VectorType t(n);
133
134 // Quantities driving the two plane rotations.
135 RealScalar alphabar = alpha;
136 RealScalar zetabar = alpha * beta;
137 RealScalar rho = one;
138 RealScalar rhobar = one;
139 RealScalar cbar = one;
140 RealScalar sbar = zero;
141
142 // Quantities for the running estimate of ||r||.
143 RealScalar betadd = beta;
144 RealScalar betad = zero;
145 RealScalar rhodold = one;
146 RealScalar tautildeold = zero;
147 RealScalar thetatilde = zero;
148 RealScalar zeta = zero;
149 RealScalar d = zero;
150
151 // Quantities for the running estimates of ||A|| and cond(A).
152 RealScalar normA2 = alpha * alpha;
153 RealScalar maxrbar = zero;
154 RealScalar minrbar = NumTraits<RealScalar>::highest();
155
156 const RealScalar normb = beta;
157 const RealScalar ctol = conlim > zero ? one / conlim : zero;
158 RealScalar test2 = zero; // recomputed each iteration; also read after the loop for tol_error
159
160 Index istop = 0;
161 Index itn = 0;
162 while (istop == 0) {
163 ++itn;
164
165 // Perform the next step of the bidiagonalization to obtain the next
166 // beta, u, alpha, v. These satisfy
167 // beta*u = A*(M^{-1} v) - alpha*u,
168 // alpha*v = M^{-1} A^T u - beta*v.
169 t = precond.solve(v);
170 u *= -alpha;
171 u.noalias() += mat * t;
172 beta = u.stableNorm();
173 if (beta > zero) {
174 u /= beta;
175 Atu.noalias() = mat.adjoint() * u;
176 t = precond.solve(Atu);
177 v = t - beta * v;
178 alpha = v.stableNorm();
179 if (alpha > zero) v /= alpha;
180 }
181
182 // Construct rotation Qhat_{k} that folds in the damping.
183 const RealScalar alphahat = numext::hypot(alphabar, lambda);
184 const RealScalar chat = alphabar / alphahat;
185 const RealScalar shat = lambda / alphahat;
186
187 // Use a plane rotation Q_{k} to turn B_{k} into R_{k}.
188 const RealScalar rhoold = rho;
189 rho = numext::hypot(alphahat, beta);
190 const RealScalar c = alphahat / rho;
191 const RealScalar s = beta / rho;
192 const RealScalar thetanew = s * alpha;
193 alphabar = c * alpha;
194
195 // Use a plane rotation Qbar_{k} to turn R_{k}^T into Rbar_{k}.
196 const RealScalar rhobarold = rhobar;
197 const RealScalar zetaold = zeta;
198 const RealScalar thetabar = sbar * rho;
199 const RealScalar rhotemp = cbar * rho;
200 rhobar = numext::hypot(cbar * rho, thetanew);
201 cbar = cbar * rho / rhobar;
202 sbar = thetanew / rhobar;
203 zeta = cbar * zetabar;
204 zetabar = -sbar * zetabar;
205
206 // Update h, hbar and the (correction) solution dx.
207 hbar = h - (thetabar * rho / (rhoold * rhobarold)) * hbar;
208 dx += (zeta / (rho * rhobar)) * hbar;
209 h = v - (thetanew / rho) * h;
210
211 // Estimate ||r||. Apply rotation Qhat_{k}, then Q_{k}, then Qtilde_{k-1}.
212 const RealScalar betaacute = chat * betadd;
213 const RealScalar betacheck = -shat * betadd;
214 const RealScalar betahat = c * betaacute;
215 betadd = -s * betaacute;
216
217 const RealScalar thetatildeold = thetatilde;
218 const RealScalar rhotildeold = numext::hypot(rhodold, thetabar);
219 const RealScalar ctildeold = rhodold / rhotildeold;
220 const RealScalar stildeold = thetabar / rhotildeold;
221 thetatilde = stildeold * rhobar;
222 rhodold = ctildeold * rhobar;
223 betad = -stildeold * betad + ctildeold * betahat;
224
225 tautildeold = (zetaold - thetatildeold * tautildeold) / rhotildeold;
226 const RealScalar taud = (zeta - thetatilde * tautildeold) / rhodold;
227 d += betacheck * betacheck;
228 const RealScalar normr = numext::sqrt<RealScalar>(d + numext::abs2(betad - taud) + numext::abs2(betadd));
229
230 // Estimate ||A||.
231 normA2 += beta * beta;
232 const RealScalar normA = numext::sqrt(normA2);
233 normA2 += alpha * alpha;
234
235 // Estimate cond(A).
236 maxrbar = numext::maxi(maxrbar, rhobarold);
237 if (itn > 1) minrbar = numext::mini(minrbar, rhobarold);
238 const RealScalar condA = numext::maxi(maxrbar, rhotemp) / numext::mini(minrbar, rhotemp);
239
240 // Compute the norms needed for the stopping rules.
241 const RealScalar normAr = numext::abs(zetabar);
242 const RealScalar normx = dx.stableNorm();
243
244 const RealScalar test1 = normr / normb;
245 test2 = (normA * normr > zero) ? normAr / (normA * normr) : zero;
246 const RealScalar test3 = one / condA;
247 const RealScalar t1 = test1 / (one + normA * normx / normb);
248 const RealScalar rtol = btol + atol * normA * normx / normb;
249
250 // The "1 + test <= 1" guards trigger near machine precision and make the
251 // method behave as if atol = btol = eps and conlim = 1/eps even when the
252 // user passed 0 for any of them. The user tolerances are tested first so a
253 // genuine convergence (istop 1/2/3) takes priority over the machine limits.
254 // (istop 4 uses t1 rather than test1, matching the reference algorithm.)
255 if (test1 <= rtol)
256 istop = 1;
257 else if (test2 <= atol)
258 istop = 2;
259 else if (test3 <= ctol)
260 istop = 3;
261 else if (one + t1 <= one)
262 istop = 4;
263 else if (one + test2 <= one)
264 istop = 5;
265 else if (one + test3 <= one)
266 istop = 6;
267 else if (itn >= maxIters)
268 istop = 7;
269 }
270
271 // Recover the solution: x <- x0 + M^{-1} dx.
272 t = precond.solve(dx);
273 x += t;
274
275 iters = itn;
276 tol_error = test2;
277 return istop;
278}
279
280} // namespace internal
281
282template <typename MatrixType_, typename Preconditioner_ = IdentityPreconditioner>
283class LSMR;
284
285namespace internal {
286
287template <typename MatrixType_, typename Preconditioner_>
288struct traits<LSMR<MatrixType_, Preconditioner_> > {
289 using MatrixType = MatrixType_;
290 using Preconditioner = Preconditioner_;
291};
292
293} // namespace internal
294
363template <typename MatrixType_, typename Preconditioner_>
364class LSMR : public IterativeSolverBase<LSMR<MatrixType_, Preconditioner_> > {
365 protected:
366 using Base = IterativeSolverBase<LSMR>;
367 using Base::m_error;
368 using Base::m_info;
369 using Base::m_isInitialized;
370 using Base::m_iterations;
371 using Base::matrix;
372
373 public:
374 using MatrixType = MatrixType_;
375 using Scalar = typename MatrixType::Scalar;
376 using RealScalar = typename MatrixType::RealScalar;
377 using Preconditioner = Preconditioner_;
378
380 LSMR() : Base() {}
381
392 template <typename MatrixDerived>
393 explicit LSMR(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
394
407 LSMR& setDamping(const RealScalar& lambda) {
408 m_lambda = lambda;
409 return *this;
410 }
411
413 RealScalar damping() const { return m_lambda; }
414
419 LSMR& setConditionLimit(const RealScalar& conlim) {
420 m_conditionLimit = conlim;
421 return *this;
422 }
423
425 RealScalar conditionLimit() const { return m_conditionLimit; }
426
431 LSMR& setToleranceA(const RealScalar& atol) {
432 m_atol = atol;
433 return *this;
434 }
435
438 RealScalar toleranceA() const { return m_atol >= RealScalar(0) ? m_atol : Base::m_tolerance; }
439
444 LSMR& setToleranceB(const RealScalar& btol) {
445 m_btol = btol;
446 return *this;
447 }
448
451 RealScalar toleranceB() const { return m_btol >= RealScalar(0) ? m_btol : Base::m_tolerance; }
452
454 template <typename Rhs, typename Dest>
455 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
456 m_iterations = Base::maxIterations();
457
458 Index istop = internal::lsmr(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error, toleranceA(),
459 toleranceB(), m_lambda, m_conditionLimit);
460 // istop in {0,1,2,4,5}: the (least-squares) solution was found, possibly
461 // only to within machine precision (4,5). istop in {3,6,7}: stopped on the
462 // condition-number limit or the iteration limit without meeting the
463 // requested tolerance.
464 m_info = (istop == 3 || istop == 6 || istop == 7) ? NoConvergence : Success;
465 }
466
467 protected:
468 RealScalar m_lambda = RealScalar(0);
469 RealScalar m_conditionLimit = RealScalar(0);
470 // Negative means "unset": toleranceA()/toleranceB() then fall back to tolerance().
471 RealScalar m_atol = RealScalar(-1);
472 RealScalar m_btol = RealScalar(-1);
473};
474
475} // end namespace Eigen
476
477#endif // EIGEN_LSMR_H
Index maxIterations() const
Definition IterativeSolverBase.h:307
An LSMR solver for sparse (or dense) least-squares problems.
Definition LSMR.h:364
LSMR(const EigenBase< MatrixDerived > &A)
Definition LSMR.h:393
LSMR()
Definition LSMR.h:380
LSMR & setConditionLimit(const RealScalar &conlim)
Definition LSMR.h:419
LSMR & setDamping(const RealScalar &lambda)
Definition LSMR.h:407
LSMR & setToleranceB(const RealScalar &btol)
Definition LSMR.h:444
RealScalar conditionLimit() const
Definition LSMR.h:425
RealScalar damping() const
Definition LSMR.h:413
RealScalar toleranceB() const
Definition LSMR.h:451
LSMR & setToleranceA(const RealScalar &atol)
Definition LSMR.h:431
RealScalar toleranceA() const
Definition LSMR.h:438
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34