Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
MINRES.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2012 Giacomo Po <gpo@ucla.edu>
5// Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
6// Copyright (C) 2018 David Hyde <dabh@stanford.edu>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11// SPDX-License-Identifier: MPL-2.0
12
13#ifndef EIGEN_MINRES_H
14#define EIGEN_MINRES_H
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
22
32template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
33EIGEN_DONT_INLINE void minres(const MatrixType& mat, const Rhs& rhs, Dest& x, const Preconditioner& precond,
34 Index& iters, typename Dest::RealScalar& tol_error) {
35 using RealScalar = typename Dest::RealScalar;
36 using Scalar = typename Dest::Scalar;
37 using VectorType = Matrix<Scalar, Dynamic, 1>;
38
39 // Check for zero rhs
40 const RealScalar rhsNorm(rhs.stableNorm());
41 if (rhsNorm == 0) {
42 x.setZero();
43 iters = 0;
44 tol_error = 0;
45 return;
46 }
47
48 // initialize
49 const Index maxIters(iters); // initialize maxIters to iters
50 const Index N(mat.cols()); // the size of the matrix
51 // Convergence threshold (compared to residualNorm), raised to at most the smallest normal number so that
52 // tol_error = 0 stops.
53 const RealScalar threshold(
54 internal::iterative_solver_threshold(tol_error, rhsNorm, RealScalar((std::numeric_limits<RealScalar>::min)())));
55
56 // Initialize preconditioned Lanczos
57 VectorType v_old(N); // will be initialized inside loop
58 VectorType v(VectorType::Zero(N)); // initialize v
59 VectorType v_new(rhs - mat * x); // initialize v_new
60 RealScalar residualNorm(v_new.stableNorm());
61 if (residualNorm == 0 || residualNorm < threshold) {
62 iters = 0;
63 tol_error = residualNorm / rhsNorm;
64 return;
65 }
66
67 // Keep the quadratic Lanczos terms representable for very small or large residuals.
68 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
69 v_new /= residualScale;
70 VectorType w(N); // will be initialized inside loop
71 VectorType w_new(precond.solve(v_new)); // initialize w_new
72 RealScalar beta_new2(v_new.dot(w_new));
73 eigen_assert(beta_new2 >= RealScalar(0) && "PRECONDITIONER IS NOT POSITIVE DEFINITE");
74 RealScalar beta_new(numext::sqrt(beta_new2));
75 const RealScalar beta_one(beta_new);
76 // Initialize other variables
77 RealScalar c(1.0); // the cosine of the Givens rotation
78 RealScalar c_old(1.0);
79 RealScalar s(0.0); // the sine of the Givens rotation
80 RealScalar s_old(0.0); // the sine of the Givens rotation
81 VectorType p_oold(N); // will be initialized in loop
82 VectorType p_old(VectorType::Zero(N)); // initialize p_old=0
83 VectorType p(p_old); // initialize p=0
84 RealScalar eta(1.0);
85
86 // The recurrence below estimates the residual in the M^{-1}-norm, which MINRES minimizes. With a preconditioner it is
87 // not the 2-norm |b - A x| that error() reports, so convergence is accepted only when b - A x confirms it, and the
88 // estimate restarts from b - A x otherwise. residualNorm is a computed |b - A x| exactly when residualIsTrue is set.
89 // Stagnation exit: a failed confirmation at least 5 iterations after |b - A x| last dropped to half of its reference
90 // value, without such a drop, ends the iteration. Otherwise a tolerance below the rounding error of b - A x would
91 // fail confirmations until maxIterations().
92 bool residualIsTrue = true;
93 RealScalar referenceResidualNorm = (std::numeric_limits<RealScalar>::max)();
94 Index referenceIters = 0;
95
96 iters = 0; // reset iters
97 while (iters < maxIters) {
98 // Preconditioned Lanczos
99 /* Note that there are 4 variants on the Lanczos algorithm. These are
100 * described in Paige, C. C. (1972). Computational variants of
101 * the Lanczos method for the eigenproblem. IMA Journal of Applied
102 * Mathematics, 10(3), 373-381. The current implementation corresponds
103 * to the case A(2,7) in the paper. It also corresponds to
104 * algorithm 6.14 in Y. Saad, Iterative Methods for Sparse Linear
105 * Systems, 2003 p.173. For the preconditioned version see
106 * A. Greenbaum, Iterative Methods for Solving Linear Systems, SIAM (1987).
107 */
108 const RealScalar beta(beta_new);
109 v_old = v; // update: at first time step, this makes v_old = 0 so value of beta doesn't matter
110 v_new /= beta_new; // overwrite v_new for next iteration
111 w_new /= beta_new; // overwrite w_new for next iteration
112 v = v_new; // update
113 w = w_new; // update
114 v_new.noalias() = mat * w - beta * v_old; // compute v_new
115 const RealScalar alpha = v_new.dot(w);
116 v_new -= alpha * v; // overwrite v_new
117 w_new = precond.solve(v_new); // overwrite w_new
118 beta_new2 = v_new.dot(w_new); // compute beta_new
119 eigen_assert(beta_new2 >= RealScalar(0) && "PRECONDITIONER IS NOT POSITIVE DEFINITE");
120 beta_new = numext::sqrt(beta_new2); // compute beta_new
121
122 // Givens rotation
123 const RealScalar r2 = s * alpha + c * c_old * beta; // s, s_old, c and c_old are still from previous iteration
124 const RealScalar r3 = s_old * beta; // s, s_old, c and c_old are still from previous iteration
125 const RealScalar r1_hat = c * alpha - c_old * s * beta;
126 const RealScalar r1 = numext::hypot(r1_hat, beta_new);
127 c_old = c; // store for next iteration
128 s_old = s; // store for next iteration
129 c = r1_hat / r1; // new cosine
130 s = beta_new / r1; // new sine
131
132 // Update solution
133 p_oold = p_old;
134 p_old = p;
135 p = (w - r2 * p_old - r3 * p_oold) / r1;
136 x += (residualScale * beta_one * c * eta) * p;
137 iters++; // increment iteration number (for output purposes)
138
139 // Update the estimated residual norm, then confirm convergence with b - A x. beta_new == 0 means the Krylov space
140 // is exhausted, and the Lanczos recurrence cannot continue.
141 residualNorm *= numext::abs(s);
142 residualIsTrue = false;
143 const bool krylovExhausted = beta_new == RealScalar(0);
144 if (residualNorm < threshold || krylovExhausted) {
145 residualNorm = (rhs - mat * x).stableNorm();
146 residualIsTrue = true;
147 if (residualNorm < threshold || krylovExhausted) {
148 break;
149 }
150 if (residualNorm <= RealScalar(0.5) * referenceResidualNorm) {
151 referenceResidualNorm = residualNorm;
152 referenceIters = iters;
153 } else if (iters - referenceIters >= 5) {
154 break;
155 }
156 }
157
158 eta = -s * eta; // update eta
159 }
160
161 // error() reports |b - A x| / |b|
162 if (!residualIsTrue) {
163 residualNorm = (rhs - mat * x).stableNorm();
164 }
165 tol_error = residualNorm / rhsNorm;
166}
167
168} // namespace internal
169
170template <typename MatrixType_, int UpLo_ = Lower, typename Preconditioner_ = IdentityPreconditioner>
171class MINRES;
172
173namespace internal {
174
175template <typename MatrixType_, int UpLo_, typename Preconditioner_>
176struct traits<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
177 using MatrixType = MatrixType_;
178 using Preconditioner = Preconditioner_;
179};
180
181} // namespace internal
182
225template <typename MatrixType_, int UpLo_, typename Preconditioner_>
226class MINRES : public IterativeSolverBase<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
227 protected:
228 using Base = IterativeSolverBase<MINRES>;
229 using Base::m_error;
230 using Base::m_info;
231 using Base::m_isInitialized;
232 using Base::m_iterations;
233 using Base::matrix;
234
235 public:
236 using Base::_solve_impl;
237 using MatrixType = MatrixType_;
238 using Scalar = typename MatrixType::Scalar;
239 using RealScalar = typename MatrixType::RealScalar;
240 using Preconditioner = Preconditioner_;
241
242 enum { UpLo = UpLo_ };
243
244 public:
246 MINRES() : Base() { Base::m_tolerance = RealScalar(-1); }
247
258 template <typename MatrixDerived>
259 explicit MINRES(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {
260 Base::m_tolerance = RealScalar(-1);
261 }
262
279 RealScalar tolerance() const { return Base::toleranceOrEps(); }
280
282 template <typename Rhs, typename Dest>
283 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
284 using MatrixWrapper = typename Base::MatrixWrapper;
285 using ActualMatrixType = typename Base::ActualMatrixType;
286 enum {
287 TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (Lower | Upper)) && (!MatrixType::IsRowMajor) &&
288 (!NumTraits<Scalar>::IsComplex)
289 };
290 using RowMajorWrapper =
291 std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType const&>;
292 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (Lower | Upper)),
293 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
294 using SelfAdjointWrapper =
295 std::conditional_t<UpLo == (Lower | Upper), RowMajorWrapper,
296 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>;
297
298 m_iterations = Base::maxIterations();
299 const RealScalar tol = tolerance();
300 m_error = tol;
301 RowMajorWrapper row_mat(matrix());
302 internal::minres(SelfAdjointWrapper(row_mat), b, x, Base::m_preconditioner, m_iterations, m_error);
303 m_info = Base::infoAtDefaultOrSetTolerance(m_error, tol);
304 }
305
306 protected:
307};
308
309} // end namespace Eigen
310
311#endif // EIGEN_MINRES_H
Index maxIterations() const
Definition IterativeSolverBase.h:307
A minimal residual solver for sparse symmetric problems.
Definition MINRES.h:226
MINRES()
Definition MINRES.h:246
MINRES(const EigenBase< MatrixDerived > &A)
Definition MINRES.h:259
RealScalar tolerance() const
Definition MINRES.h:279
Expression of an array as a mathematical vector or matrix.
Definition ArrayWrapper.h:120
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
Definition EigenBase.h:34