Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
IDRS.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2020 Chris Schoutrop <c.e.m.schoutrop@tue.nl>
5// Copyright (C) 2020 Jens Wehner <j.wehner@esciencecenter.nl>
6// Copyright (C) 2020 Jan van Dijk <j.v.dijk@tue.nl>
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_IDRS_H
14#define EIGEN_IDRS_H
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
36template <typename Vector, typename RealScalar>
37typename Vector::Scalar omega(const Vector& t, const Vector& s, RealScalar angle) {
38 using Scalar = typename Vector::Scalar;
39 const RealScalar ns = s.stableNorm();
40 const RealScalar nt = t.stableNorm();
41 const Scalar ts = t.dot(s);
42 const RealScalar rho = numext::abs(ts / (nt * ns));
43
44 if (rho < angle) {
45 if (ts == Scalar(0)) {
46 return Scalar(0);
47 }
48 // Original relation for om is given by
49 // om = om * angle / rho;
50 // To alleviate potential (near) division by zero this can be rewritten as
51 // om = angle * (ns / nt) * (ts / abs(ts)) = angle * (ns / nt) * sgn(ts)
52 return angle * (ns / nt) * (ts / numext::abs(ts));
53 }
54 return ts / (nt * nt);
55}
56
57template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
58bool idrs_unscaled(const MatrixType& A, const Rhs& b, Dest& x, const Preconditioner& precond, Index& iter,
59 typename Dest::RealScalar& relres, Index S, bool smoothing, typename Dest::RealScalar angle,
60 bool replacement) {
61 using RealScalar = typename Dest::RealScalar;
62 using Scalar = typename Dest::Scalar;
63 using VectorType = Matrix<Scalar, Dynamic, 1>;
64 using DenseMatrixType = Matrix<Scalar, Dynamic, Dynamic, ColMajor>;
65 const Index N = b.size();
66 S = numext::mini(S, x.rows());
67 const RealScalar tol = relres;
68 const Index maxit = iter;
69
70 bool trueres = false;
71
72 FullPivLU<DenseMatrixType> lu_solver;
73
74 DenseMatrixType P = internal::random_orthonormal_basis<DenseMatrixType>(N, S);
75
76 const RealScalar normb = b.stableNorm();
77
78 if (normb == RealScalar(0)) {
79 // Solution is the zero vector
80 x.setZero();
81 iter = 0;
82 relres = 0;
83 return true;
84 }
85 // from http://homepage.tudelft.nl/1w5b5/IDRS/manual.pdf
86 // A peak in the residual is considered dangerously high if‖ri‖/‖b‖> C(tol/epsilon).
87 // With epsilon the relative machine precision. The factor tol/epsilon corresponds
88 // to the size of a finite precision number that is so large that the absolute
89 // round-off error in this number, when propagated through the process, makes it
90 // impossible to achieve the required accuracy. The factor C accounts for the
91 // accumulation of round-off errors. This parameter has been set to 10^{-3}.
92 // mp is epsilon/C 10^3 * eps is very conservative, so normally no residual
93 // replacements will take place. It only happens if things go very wrong. Too many
94 // restarts may ruin the convergence.
95 const RealScalar mp = RealScalar(1e3) * NumTraits<Scalar>::epsilon();
96
97 // Compute initial residual
98 // Relative tolerance, at least the residual floor so that tol = 0 stops at a vanishing residual
99 const RealScalar tolb =
100 internal::iterative_solver_threshold(tol, normb, internal::iterative_solver_residual_floor<RealScalar>());
101 VectorType r = b - A * x;
102
103 VectorType x_s, r_s;
104
105 if (smoothing) {
106 x_s = x;
107 r_s = r;
108 }
109
110 RealScalar normr = r.stableNorm();
111
112 if (normr <= tolb) {
113 // Initial guess is a good enough solution
114 iter = 0;
115 relres = normr / normb;
116 return true;
117 }
118
119 DenseMatrixType G = DenseMatrixType::Zero(N, S);
120 DenseMatrixType U = DenseMatrixType::Zero(N, S);
121 DenseMatrixType M = DenseMatrixType::Identity(S, S);
122 VectorType t(N), v(N);
123 Scalar om = 1.;
124
125 const auto restart = [&]() {
126 G.setZero();
127 U.setZero();
128 M.setIdentity();
129 };
130
131 // r and x follow separate recurrences, and A U(:,k) = G(:,k) holds only up to the rounding error of the
132 // bi-orthogonalization, which grows with cond(A) and with the cancellation in G(:,k). The recurrence residual can
133 // then fall below the tolerance while b - A x does not. Accept convergence only when b - A x confirms it, and restart
134 // from the current iterate otherwise, unless no iteration is left. A restart counts as an iteration, which bounds
135 // their number by maxit. A residual that is not finite ends the iteration, and the caller reports NoConvergence.
136 // Stagnation exit: when |b - A x| cannot reach the threshold (a tolerance below the rounding error of b - A x), the
137 // confirmations fail again and again. A failed confirmation at least 5 iterations after |b - A x| last dropped to
138 // half of its reference value, without such a drop, ends the iteration instead of restarting until maxit.
139 // residualIsTrue is set when normr is the computed |b - A x| at the end of the iteration.
140 bool residualIsTrue = false;
141 RealScalar referenceNorm = (std::numeric_limits<RealScalar>::max)();
142 Index referenceIter = 0;
143 const auto done = [&]() {
144 if (!(numext::isfinite)(normr)) {
145 return true;
146 }
147 if (normr > tolb) {
148 return false;
149 }
150 if (smoothing) {
151 x = x_s;
152 }
153 r.noalias() = b - A * x;
154 normr = r.stableNorm();
155 if (normr <= tolb || iter >= maxit) {
156 residualIsTrue = true;
157 return true;
158 }
159 if (normr <= RealScalar(0.5) * referenceNorm) {
160 referenceNorm = normr;
161 referenceIter = iter;
162 } else if (iter - referenceIter >= 5) {
163 residualIsTrue = true;
164 return true;
165 }
166 if (smoothing) {
167 r_s = r;
168 }
169 restart();
170 ++iter;
171 return false;
172 };
173
174 // Main iteration loop, build G-spaces:
175 iter = 0;
176
177 while (!done() && iter < maxit) {
178 // New right hand size for small system:
179 VectorType f = (r.adjoint() * P).adjoint();
180
181 for (Index k = 0; k < S; ++k) {
182 // Solve small system and make v orthogonal to P:
183 // c = M(k:s,k:s)\f(k:s);
184 lu_solver.compute(M.block(k, k, S - k, S - k));
185 VectorType c = lu_solver.solve(f.segment(k, S - k));
186 // v = r - G(:,k:s)*c;
187 v.noalias() = r - G.rightCols(S - k) * c;
188 // Preconditioning
189 v = precond.solve(v);
190
191 // Compute new U(:,k) and G(:,k), G(:,k) is in space G_j
192 U.col(k) = U.rightCols(S - k) * c + om * v;
193 G.col(k).noalias() = A * U.col(k);
194
195 // Bi-Orthogonalise the new basis vectors:
196 const RealScalar normg = G.col(k).stableNorm();
197 for (Index i = 0; i < k; ++i) {
198 // alpha = ( P(:,i)'*G(:,k) )/M(i,i);
199 Scalar alpha = P.col(i).dot(G.col(k)) / M(i, i);
200 G.col(k) = G.col(k) - alpha * G.col(i);
201 U.col(k) = U.col(k) - alpha * U.col(i);
202 }
203
204 if (k > 0 && G.col(k).stableNorm() <= mp * normg) {
205 // G(:,k) was in the span of the earlier directions: the Krylov space is exhausted. What is left of G(:,k) is
206 // the rounding error of the loop above, for which A U(:,k) = G(:,k) no longer holds. Restart from the current
207 // iterate with the omega step below.
208 restart();
209 break;
210 }
211
212 // New column of M = P'*G (first k-1 entries are zero)
213 // M(k:s,k) = (G(:,k)'*P(:,k:s))';
214 M.block(k, k, S - k, 1) = (G.col(k).adjoint() * P.rightCols(S - k)).adjoint();
215
216 if (M(k, k) == Scalar(0)) {
217 return false;
218 }
219
220 // Make r orthogonal to q_i, i = 0..k-1
221 Scalar beta = f(k) / M(k, k);
222 r = r - beta * G.col(k);
223 x = x + beta * U.col(k);
224 normr = r.stableNorm();
225
226 if (replacement && normr > tolb / mp) {
227 trueres = true;
228 }
229
230 // Smoothing:
231 if (smoothing) {
232 t = r_s - r;
233 // gamma is a Scalar, but the conversion is not allowed
234 Scalar gamma = t.dot(r_s) / t.squaredNorm();
235 r_s = r_s - gamma * t;
236 x_s = x_s - gamma * (x_s - x);
237 normr = r_s.stableNorm();
238 }
239
240 if (normr < tolb || iter == maxit) {
241 break;
242 }
243
244 // New f = P'*r (first k components are zero)
245 if (k < S - 1) {
246 f.segment(k + 1, S - (k + 1)) = f.segment(k + 1, S - (k + 1)) - beta * M.block(k + 1, k, S - (k + 1), 1);
247 }
248 } // end for
249
250 if (normr < tolb) {
251 continue;
252 }
253
254 // Now we have sufficient vectors in G_j to compute residual in G_j+1
255 // Note: r is already perpendicular to P so v = r
256 // Preconditioning
257 v = r;
258 v = precond.solve(v);
259
260 // Matrix-vector multiplication:
261 t.noalias() = A * v;
262
263 // Computation of a new omega
264 om = internal::omega(t, r, angle);
265
266 if (om == RealScalar(0.0)) {
267 return false;
268 }
269
270 r = r - om * t;
271 x = x + om * v;
272 normr = r.stableNorm();
273
274 if (replacement && normr > tolb / mp) {
275 trueres = true;
276 }
277
278 // Residual replacement?
279 if (trueres && normr < normb) {
280 r.noalias() = b - A * x;
281 trueres = false;
282 }
283
284 // Smoothing:
285 if (smoothing) {
286 t = r_s - r;
287 Scalar gamma = t.dot(r_s) / t.squaredNorm();
288 r_s = r_s - gamma * t;
289 x_s = x_s - gamma * (x_s - x);
290 normr = r_s.stableNorm();
291 }
292
293 iter++;
294
295 } // end while
296
297 if (smoothing) {
298 x = x_s;
299 }
300 if (!residualIsTrue && (numext::isfinite)(normr)) {
301 // The loop ended at maxit with the recurrence residual: report the computed b - A x, which relres documents.
302 normr = (b - A * x).stableNorm();
303 }
304 relres = normr / normb;
305 return true;
306}
307
308template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
309bool idrs(const MatrixType& A, const Rhs& b, Dest& x, const Preconditioner& precond, Index& iter,
310 typename Dest::RealScalar& relres, Index S, bool smoothing, typename Dest::RealScalar angle,
311 bool replacement) {
312 // The products of omega() are of the order of the residual squared, see iterative_solver_on_scaled_problem().
313 return iterative_solver_on_scaled_problem(b, x, [&](const auto& scaledRhs, auto& scaledGuess) {
314 return idrs_unscaled(A, scaledRhs, scaledGuess, precond, iter, relres, S, smoothing, angle, replacement);
315 });
316}
317
318} // namespace internal
319
320template <typename MatrixType_, typename Preconditioner_ = DiagonalPreconditioner<typename MatrixType_::Scalar> >
321class IDRS;
322
323namespace internal {
324
325template <typename MatrixType_, typename Preconditioner_>
326struct traits<Eigen::IDRS<MatrixType_, Preconditioner_> > {
327 using MatrixType = MatrixType_;
328 using Preconditioner = Preconditioner_;
329};
330
331} // namespace internal
332
376template <typename MatrixType_, typename Preconditioner_>
377class IDRS : public IterativeSolverBase<IDRS<MatrixType_, Preconditioner_> > {
378 public:
379 using MatrixType = MatrixType_;
380 using Scalar = typename MatrixType::Scalar;
381 using RealScalar = typename MatrixType::RealScalar;
382 using Preconditioner = Preconditioner_;
383
384 private:
385 using Base = IterativeSolverBase<IDRS>;
386 using Base::m_error;
387 using Base::m_info;
388 using Base::m_isInitialized;
389 using Base::m_iterations;
390 using Base::matrix;
391 Index m_S = 4;
392 bool m_smoothing = false;
393 RealScalar m_angle = RealScalar(0.7);
394 bool m_residual = false;
395
396 public:
398 IDRS() { Base::m_tolerance = RealScalar(-1); }
399
410 template <typename MatrixDerived>
411 explicit IDRS(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {
412 Base::m_tolerance = RealScalar(-1);
413 }
414
428 RealScalar tolerance() const { return Base::toleranceOrEps(); }
429
435 template <typename Rhs, typename Dest>
436 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
437 m_iterations = Base::maxIterations();
438 const RealScalar tol = tolerance();
439 m_error = tol;
440
441 bool ret = internal::idrs(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error, m_S, m_smoothing, m_angle,
442 m_residual);
443
444 m_info = (!ret) ? NumericalIssue : Base::infoAtDefaultOrSetTolerance(m_error, tol);
445 }
446
448 void setS(Index S) {
449 if (S < 1) {
450 S = 4;
451 }
452
453 m_S = S;
454 }
455
462 void setSmoothing(bool smoothing) { m_smoothing = smoothing; }
463
474 void setAngle(RealScalar angle) { m_angle = angle; }
475
479 void setResidualUpdate(bool update) { m_residual = update; }
480};
481
482} // namespace Eigen
483
484#endif /* EIGEN_IDRS_H */
The Induced Dimension Reduction method (IDR(s)) is a short-recurrences Krylov method for sparse squar...
Definition IDRS.h:377
void setResidualUpdate(bool update)
Definition IDRS.h:479
IDRS()
Definition IDRS.h:398
RealScalar tolerance() const
Definition IDRS.h:428
IDRS(const EigenBase< MatrixDerived > &A)
Definition IDRS.h:411
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition IDRS.h:436
void setSmoothing(bool smoothing)
Definition IDRS.h:462
void setS(Index S)
Definition IDRS.h:448
void setAngle(RealScalar angle)
Definition IDRS.h:474
Index maxIterations() const
Definition IterativeSolverBase.h:307
@ NumericalIssue
Definition Constants.h:459
Matrix< Type, Size, 1 > Vector
Size×1 vector of type Type.
Definition Matrix.h:532
Definition EigenBase.h:34