Eigen  5.0.1
 
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 std::sqrt;
36 using RealScalar = typename Dest::RealScalar;
37 using Scalar = typename Dest::Scalar;
38 using VectorType = Matrix<Scalar, Dynamic, 1>;
39
40 // Check for zero rhs
41 const RealScalar rhsNorm(rhs.stableNorm());
42 if (rhsNorm == 0) {
43 x.setZero();
44 iters = 0;
45 tol_error = 0;
46 return;
47 }
48
49 // initialize
50 const Index maxIters(iters); // initialize maxIters to iters
51 const Index N(mat.cols()); // the size of the matrix
52 const RealScalar threshold(tol_error * rhsNorm); // convergence threshold (compared to residualNorm)
53
54 // Initialize preconditioned Lanczos
55 VectorType v_old(N); // will be initialized inside loop
56 VectorType v(VectorType::Zero(N)); // initialize v
57 VectorType v_new(rhs - mat * x); // initialize v_new
58 RealScalar residualNorm(v_new.stableNorm());
59 if (residualNorm == 0 || residualNorm < threshold) {
60 iters = 0;
61 tol_error = residualNorm / rhsNorm;
62 return;
63 }
64
65 // Keep the quadratic Lanczos terms representable for very small or large residuals.
66 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
67 v_new /= residualScale;
68 VectorType w(N); // will be initialized inside loop
69 VectorType w_new(precond.solve(v_new)); // initialize w_new
70 RealScalar beta_new2(v_new.dot(w_new));
71 eigen_assert(beta_new2 >= 0.0 && "PRECONDITIONER IS NOT POSITIVE DEFINITE");
72 RealScalar beta_new(sqrt(beta_new2));
73 const RealScalar beta_one(beta_new);
74 // Initialize other variables
75 RealScalar c(1.0); // the cosine of the Givens rotation
76 RealScalar c_old(1.0);
77 RealScalar s(0.0); // the sine of the Givens rotation
78 RealScalar s_old(0.0); // the sine of the Givens rotation
79 VectorType p_oold(N); // will be initialized in loop
80 VectorType p_old(VectorType::Zero(N)); // initialize p_old=0
81 VectorType p(p_old); // initialize p=0
82 RealScalar eta(1.0);
83
84 iters = 0; // reset iters
85 while (iters < maxIters) {
86 // Preconditioned Lanczos
87 /* Note that there are 4 variants on the Lanczos algorithm. These are
88 * described in Paige, C. C. (1972). Computational variants of
89 * the Lanczos method for the eigenproblem. IMA Journal of Applied
90 * Mathematics, 10(3), 373-381. The current implementation corresponds
91 * to the case A(2,7) in the paper. It also corresponds to
92 * algorithm 6.14 in Y. Saad, Iterative Methods for Sparse Linear
93 * Systems, 2003 p.173. For the preconditioned version see
94 * A. Greenbaum, Iterative Methods for Solving Linear Systems, SIAM (1987).
95 */
96 const RealScalar beta(beta_new);
97 v_old = v; // update: at first time step, this makes v_old = 0 so value of beta doesn't matter
98 v_new /= beta_new; // overwrite v_new for next iteration
99 w_new /= beta_new; // overwrite w_new for next iteration
100 v = v_new; // update
101 w = w_new; // update
102 v_new.noalias() = mat * w - beta * v_old; // compute v_new
103 const RealScalar alpha = v_new.dot(w);
104 v_new -= alpha * v; // overwrite v_new
105 w_new = precond.solve(v_new); // overwrite w_new
106 beta_new2 = v_new.dot(w_new); // compute beta_new
107 eigen_assert(beta_new2 >= 0.0 && "PRECONDITIONER IS NOT POSITIVE DEFINITE");
108 beta_new = sqrt(beta_new2); // compute beta_new
109
110 // Givens rotation
111 const RealScalar r2 = s * alpha + c * c_old * beta; // s, s_old, c and c_old are still from previous iteration
112 const RealScalar r3 = s_old * beta; // s, s_old, c and c_old are still from previous iteration
113 const RealScalar r1_hat = c * alpha - c_old * s * beta;
114 const RealScalar r1 = numext::hypot(r1_hat, beta_new);
115 c_old = c; // store for next iteration
116 s_old = s; // store for next iteration
117 c = r1_hat / r1; // new cosine
118 s = beta_new / r1; // new sine
119
120 // Update solution
121 p_oold = p_old;
122 p_old = p;
123 p = (w - r2 * p_old - r3 * p_oold) / r1;
124 x += (residualScale * beta_one * c * eta) * p;
125
126 /* Update the estimated residual norm. Note that this is the estimated
127 residual; the real residual |Ax-b| may be slightly larger. */
128 residualNorm *= numext::abs(s);
129
130 if (residualNorm < threshold) {
131 break;
132 }
133
134 eta = -s * eta; // update eta
135 iters++; // increment iteration number (for output purposes)
136 }
137
138 /* Compute error. Note that this is the estimated error. The real
139 error |Ax-b|/|b| may be slightly larger */
140 tol_error = residualNorm / rhsNorm;
141}
142
143} // namespace internal
144
145template <typename MatrixType_, int UpLo_ = Lower, typename Preconditioner_ = IdentityPreconditioner>
146class MINRES;
147
148namespace internal {
149
150template <typename MatrixType_, int UpLo_, typename Preconditioner_>
151struct traits<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
152 using MatrixType = MatrixType_;
153 using Preconditioner = Preconditioner_;
154};
155
156} // namespace internal
157
196template <typename MatrixType_, int UpLo_, typename Preconditioner_>
197class MINRES : public IterativeSolverBase<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
198 protected:
199 using Base = IterativeSolverBase<MINRES>;
200 using Base::m_error;
201 using Base::m_info;
202 using Base::m_isInitialized;
203 using Base::m_iterations;
204 using Base::matrix;
205
206 public:
207 using Base::_solve_impl;
208 using MatrixType = MatrixType_;
209 using Scalar = typename MatrixType::Scalar;
210 using RealScalar = typename MatrixType::RealScalar;
211 using Preconditioner = Preconditioner_;
212
213 enum { UpLo = UpLo_ };
214
215 public:
217 MINRES() : Base() {}
218
229 template <typename MatrixDerived>
230 explicit MINRES(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
231
233 template <typename Rhs, typename Dest>
234 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
235 using MatrixWrapper = typename Base::MatrixWrapper;
236 using ActualMatrixType = typename Base::ActualMatrixType;
237 enum {
238 TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (Lower | Upper)) && (!MatrixType::IsRowMajor) &&
239 (!NumTraits<Scalar>::IsComplex)
240 };
241 using RowMajorWrapper =
242 std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType const&>;
243 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (Lower | Upper)),
244 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
245 using SelfAdjointWrapper =
246 std::conditional_t<UpLo == (Lower | Upper), RowMajorWrapper,
247 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>;
248
249 m_iterations = Base::maxIterations();
250 m_error = Base::m_tolerance;
251 RowMajorWrapper row_mat(matrix());
252 internal::minres(SelfAdjointWrapper(row_mat), b, x, Base::m_preconditioner, m_iterations, m_error);
253 m_info = m_error <= Base::m_tolerance ? Success : NoConvergence;
254 }
255
256 protected:
257};
258
259} // end namespace Eigen
260
261#endif // EIGEN_MINRES_H
Index maxIterations() const
Definition IterativeSolverBase.h:245
A minimal residual solver for sparse symmetric problems.
Definition MINRES.h:197
MINRES()
Definition MINRES.h:217
MINRES(const EigenBase< MatrixDerived > &A)
Definition MINRES.h:230
Expression of an array as a mathematical vector or matrix.
Definition ArrayWrapper.h:120
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34