Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
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 <[email protected]>
5// Copyright (C) 2012 Désiré Nuentsa-Wakam <[email protected]>
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
11#ifndef EIGEN_BICGSTAB_H
12#define EIGEN_BICGSTAB_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
31template <typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
32bool bicgstab(const MatrixType& mat, const Rhs& rhs, Dest& x, const Preconditioner& precond, Index& iters,
33 typename Dest::RealScalar& tol_error) {
34 using std::abs;
35 using std::sqrt;
36 typedef typename Dest::RealScalar RealScalar;
37 typedef typename Dest::Scalar Scalar;
38 typedef Matrix<Scalar, Dynamic, 1> VectorType;
39 RealScalar tol = tol_error;
40 Index maxIters = iters;
41
42 Index n = mat.cols();
43 VectorType r = rhs - mat * x;
44 VectorType r0 = r;
45
46 RealScalar r0_sqnorm = r0.squaredNorm();
47 RealScalar rhs_sqnorm = rhs.squaredNorm();
48 if (rhs_sqnorm == 0) {
49 x.setZero();
50 return true;
51 }
52 Scalar rho(1);
53 Scalar alpha(1);
54 Scalar w(1);
55
56 VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
57 VectorType y(n), z(n);
58 VectorType kt(n), ks(n);
59
60 VectorType s(n), t(n);
61
62 RealScalar tol2 = tol * tol * rhs_sqnorm;
63 RealScalar eps2 = NumTraits<Scalar>::epsilon() * NumTraits<Scalar>::epsilon();
64 Index i = 0;
65 Index restarts = 0;
66
67 while (r.squaredNorm() > tol2 && i < maxIters) {
68 Scalar rho_old = rho;
69
70 rho = r0.dot(r);
71 if (abs(rho) < eps2 * r0_sqnorm) {
72 // The new residual vector became too orthogonal to the arbitrarily chosen direction r0
73 // Let's restart with a new r0:
74 r = rhs - mat * x;
75 r0 = r;
76 rho = r0_sqnorm = r.squaredNorm();
77 if (restarts++ == 0) i = 0;
78 }
79 Scalar beta = (rho / rho_old) * (alpha / w);
80 p = r + beta * (p - w * v);
81
82 y = precond.solve(p);
83
84 v.noalias() = mat * y;
85
86 alpha = rho / r0.dot(v);
87 s = r - alpha * v;
88
89 z = precond.solve(s);
90 t.noalias() = mat * z;
91
92 RealScalar tmp = t.squaredNorm();
93 if (tmp > RealScalar(0))
94 w = t.dot(s) / tmp;
95 else
96 w = Scalar(0);
97 x += alpha * y + w * z;
98 r = s - w * t;
99 ++i;
100 }
101 tol_error = sqrt(r.squaredNorm() / rhs_sqnorm);
102 iters = i;
103 return true;
104}
105
106} // namespace internal
107
108template <typename MatrixType_, typename Preconditioner_ = DiagonalPreconditioner<typename MatrixType_::Scalar> >
109class BiCGSTAB;
110
111namespace internal {
112
113template <typename MatrixType_, typename Preconditioner_>
114struct traits<BiCGSTAB<MatrixType_, Preconditioner_> > {
115 typedef MatrixType_ MatrixType;
116 typedef Preconditioner_ Preconditioner;
117};
118
119} // namespace internal
120
152template <typename MatrixType_, typename Preconditioner_>
153class BiCGSTAB : public IterativeSolverBase<BiCGSTAB<MatrixType_, Preconditioner_> > {
155 using Base::m_error;
156 using Base::m_info;
157 using Base::m_isInitialized;
158 using Base::m_iterations;
159 using Base::matrix;
160
161 public:
162 typedef MatrixType_ MatrixType;
163 typedef typename MatrixType::Scalar Scalar;
164 typedef typename MatrixType::RealScalar RealScalar;
165 typedef Preconditioner_ Preconditioner;
166
167 public:
169 BiCGSTAB() : Base() {}
170
181 template <typename MatrixDerived>
182 explicit BiCGSTAB(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
183
184 ~BiCGSTAB() {}
185
187 template <typename Rhs, typename Dest>
188 void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const {
189 m_iterations = Base::maxIterations();
190 m_error = Base::m_tolerance;
191
192 bool ret = internal::bicgstab(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
193
194 m_info = (!ret) ? NumericalIssue : m_error <= Base::m_tolerance ? Success : NoConvergence;
195 }
196
197 protected:
198};
199
200} // end namespace Eigen
201
202#endif // EIGEN_BICGSTAB_H
A bi conjugate gradient stabilized solver for sparse square problems.
Definition BiCGSTAB.h:153
BiCGSTAB(const EigenBase< MatrixDerived > &A)
Definition BiCGSTAB.h:182
BiCGSTAB()
Definition BiCGSTAB.h:169
Base class for linear iterative solvers.
Definition IterativeSolverBase.h:124
Index maxIterations() const
Definition IterativeSolverBase.h:251
@ NumericalIssue
Definition Constants.h:442
@ Success
Definition Constants.h:440
@ NoConvergence
Definition Constants.h:444
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_sqrt_op< typename Derived::Scalar >, const Derived > sqrt(const Eigen::ArrayBase< Derived > &x)
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)
Definition EigenBase.h:33