12#ifndef EIGEN_BICGSTAB_H
13#define EIGEN_BICGSTAB_H
16#include "./InternalHeaderCheck.h"
32template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
33bool bicgstab(
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;
41 VectorType r = rhs - mat * x;
44 RealScalar r0_norm = r0.stableNorm();
45 RealScalar r_norm = r0_norm;
46 RealScalar rhs_norm = rhs.stableNorm();
52 RealScalar tol = tol_error * rhs_norm;
58 VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
59 VectorType y(n), z(n);
61 VectorType s(n), t(n);
63 RealScalar eps = NumTraits<Scalar>::epsilon();
67 while (r_norm > tol && i < maxIters) {
70 if (Eigen::numext::abs(rho) / Eigen::numext::maxi(r0_norm, r_norm) < eps * Eigen::numext::mini(r0_norm, r_norm)) {
73 r.noalias() = rhs - mat * x;
75 rho = r.squaredNorm();
76 r0_norm = r.stableNorm();
79 if (restarts++ == 0) i = 0;
81 Scalar beta = (rho / rho_old) * (alpha / w);
82 p = r + beta * (p - w * v);
86 v.noalias() = mat * y;
87 Scalar theta = r0.dot(v);
89 RealScalar v_norm = v.stableNorm();
90 if (Eigen::numext::abs(theta) / Eigen::numext::maxi(r0_norm, v_norm) < eps * Eigen::numext::mini(r0_norm, v_norm)) {
91 r.noalias() = rhs - mat * x;
93 r0_norm = r0.stableNorm();
97 if (restarts++ == 0) i = 0;
103 z = precond.solve(s);
104 t.noalias() = mat * z;
106 RealScalar tmp = t.squaredNorm();
107 if (tmp > RealScalar(0)) {
112 x += alpha * y + w * z;
114 r_norm = r.stableNorm();
118 tol_error = r_norm / rhs_norm;
125template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar> >
130template <
typename MatrixType_,
typename Preconditioner_>
131struct traits<BiCGSTAB<MatrixType_, Preconditioner_> > {
132 using MatrixType = MatrixType_;
133 using Preconditioner = Preconditioner_;
169template <
typename MatrixType_,
typename Preconditioner_>
175 using Base::m_isInitialized;
176 using Base::m_iterations;
180 using MatrixType = MatrixType_;
181 using Scalar =
typename MatrixType::Scalar;
182 using RealScalar =
typename MatrixType::RealScalar;
183 using Preconditioner = Preconditioner_;
199 template <
typename MatrixDerived>
203 template <
typename Rhs,
typename Dest>
204 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
206 m_error = Base::m_tolerance;
208 bool ret = internal::bicgstab(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
A bi conjugate gradient stabilized solver for sparse square problems.
Definition BiCGSTAB.h:170
BiCGSTAB(const EigenBase< MatrixDerived > &A)
Definition BiCGSTAB.h:200
BiCGSTAB()
Definition BiCGSTAB.h:187
IterativeSolverBase()
Definition IterativeSolverBase.h:136
Index maxIterations() const
Definition IterativeSolverBase.h:245
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34