Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
BiCGSTAB.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
5// Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
6//
7// This Source Code Form is subject to the terms of the Mozilla
8// Public License v. 2.0. If a copy of the MPL was not distributed
9// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10// SPDX-License-Identifier: MPL-2.0
11
12#ifndef EIGEN_BICGSTAB_H
13#define EIGEN_BICGSTAB_H
14
15// IWYU pragma: private
16#include "./InternalHeaderCheck.h"
17
18namespace Eigen {
19
20namespace internal {
21
32template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
33bool bicgstab_unscaled(const MatrixType& mat, const Rhs& rhs, Dest& x, const Preconditioner& precond, Index& iters,
34 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 Index maxIters = iters;
39
40 Index n = mat.cols();
41 VectorType r = rhs - mat * x;
42 VectorType r0 = r;
43
44 RealScalar r0_norm = r0.stableNorm();
45 RealScalar r_norm = r0_norm;
46 RealScalar rhs_norm = rhs.stableNorm();
47 if (rhs_norm == 0) {
48 x.setZero();
49 iters = 0;
50 tol_error = 0;
51 return true;
52 }
53
54 RealScalar tol = internal::iterative_solver_threshold(tol_error, rhs_norm,
55 internal::iterative_solver_residual_floor<RealScalar>());
56
57 Scalar rho(1);
58 Scalar alpha(0);
59 Scalar w(1);
60
61 VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
62 VectorType y(n), z(n);
63
64 VectorType s(n), t(n);
65
66 RealScalar eps = NumTraits<Scalar>::epsilon();
67 Index i = 0;
68 Index restarts = 0;
69
70 while (r_norm > tol && i < maxIters) {
71 Scalar rho_old = rho;
72 rho = r0.dot(r);
73 if (Eigen::numext::abs(rho) / Eigen::numext::maxi(r0_norm, r_norm) < eps * Eigen::numext::mini(r0_norm, r_norm)) {
74 // The new residual vector became too orthogonal to the arbitrarily chosen direction r0
75 // Let's restart with a new r0:
76 r.noalias() = rhs - mat * x;
77 r0 = r;
78 rho = r.squaredNorm();
79 r0_norm = r.stableNorm();
80 alpha = Scalar(0);
81 w = Scalar(1);
82 if (restarts++ == 0) i = 0;
83 }
84 Scalar beta = (rho / rho_old) * (alpha / w);
85 p = r + beta * (p - w * v);
86
87 y = precond.solve(p);
88
89 v.noalias() = mat * y;
90 Scalar theta = r0.dot(v);
91 // For small angles ∠(r0, v) < eps, random restart.
92 RealScalar v_norm = v.stableNorm();
93 if (Eigen::numext::abs(theta) / Eigen::numext::maxi(r0_norm, v_norm) < eps * Eigen::numext::mini(r0_norm, v_norm)) {
94 r.noalias() = rhs - mat * x;
95 r0.setRandom();
96 r0_norm = r0.stableNorm();
97 rho = Scalar(1);
98 alpha = Scalar(0);
99 w = Scalar(1);
100 if (restarts++ == 0) i = 0;
101 continue;
102 }
103 alpha = rho / theta;
104 s = r - alpha * v;
105
106 z = precond.solve(s);
107 t.noalias() = mat * z;
108
109 RealScalar tmp = t.squaredNorm();
110 if (tmp > RealScalar(0)) {
111 w = t.dot(s) / tmp;
112 } else {
113 w = Scalar(0);
114 }
115 x += alpha * y + w * z;
116 r = s - w * t;
117 r_norm = r.stableNorm();
118 ++i;
119 }
120
121 tol_error = r_norm / rhs_norm;
122 iters = i;
123 return true;
124}
125
126template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
127bool bicgstab(const MatrixType& mat, const Rhs& rhs, Dest& x, const Preconditioner& precond, Index& iters,
128 typename Dest::RealScalar& tol_error) {
129 // The inner products and squared norms are of the order of the residual squared, see
130 // iterative_solver_on_scaled_problem().
131 return iterative_solver_on_scaled_problem(rhs, x, [&](const auto& scaledRhs, auto& scaledGuess) {
132 return bicgstab_unscaled(mat, scaledRhs, scaledGuess, precond, iters, tol_error);
133 });
134}
135
136} // namespace internal
137
138template <typename MatrixType_, typename Preconditioner_ = DiagonalPreconditioner<typename MatrixType_::Scalar> >
139class BiCGSTAB;
140
141namespace internal {
142
143template <typename MatrixType_, typename Preconditioner_>
144struct traits<BiCGSTAB<MatrixType_, Preconditioner_> > {
145 using MatrixType = MatrixType_;
146 using Preconditioner = Preconditioner_;
147};
148
149} // namespace internal
150
182template <typename MatrixType_, typename Preconditioner_>
183class BiCGSTAB : public IterativeSolverBase<BiCGSTAB<MatrixType_, Preconditioner_> > {
184 protected:
186 using Base::m_error;
187 using Base::m_info;
188 using Base::m_isInitialized;
189 using Base::m_iterations;
190 using Base::matrix;
191
192 public:
193 using MatrixType = MatrixType_;
194 using Scalar = typename MatrixType::Scalar;
195 using RealScalar = typename MatrixType::RealScalar;
196 using Preconditioner = Preconditioner_;
197
198 public:
200 BiCGSTAB() : Base() {}
201
212 template <typename MatrixDerived>
213 explicit BiCGSTAB(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
214
216 template <typename Rhs, typename Dest>
217 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
218 m_iterations = Base::maxIterations();
219 m_error = Base::m_tolerance;
220
221 bool ret = internal::bicgstab(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
222
223 m_info = (!ret) ? NumericalIssue : m_error <= Base::m_tolerance ? Success : NoConvergence;
224 }
225};
226
227} // end namespace Eigen
228
229#endif // EIGEN_BICGSTAB_H
A bi conjugate gradient stabilized solver for sparse square problems.
Definition BiCGSTAB.h:183
BiCGSTAB(const EigenBase< MatrixDerived > &A)
Definition BiCGSTAB.h:213
BiCGSTAB()
Definition BiCGSTAB.h:200
Index maxIterations() const
Definition IterativeSolverBase.h:307
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34