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_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;
41 VectorType r = rhs - mat * x;
44 RealScalar r0_norm = r0.stableNorm();
45 RealScalar r_norm = r0_norm;
46 RealScalar rhs_norm = rhs.stableNorm();
54 RealScalar tol = internal::iterative_solver_threshold(tol_error, rhs_norm,
55 internal::iterative_solver_residual_floor<RealScalar>());
61 VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
62 VectorType y(n), z(n);
64 VectorType s(n), t(n);
66 RealScalar eps = NumTraits<Scalar>::epsilon();
70 while (r_norm > tol && i < maxIters) {
73 if (Eigen::numext::abs(rho) / Eigen::numext::maxi(r0_norm, r_norm) < eps * Eigen::numext::mini(r0_norm, r_norm)) {
76 r.noalias() = rhs - mat * x;
78 rho = r.squaredNorm();
79 r0_norm = r.stableNorm();
82 if (restarts++ == 0) i = 0;
84 Scalar beta = (rho / rho_old) * (alpha / w);
85 p = r + beta * (p - w * v);
89 v.noalias() = mat * y;
90 Scalar theta = r0.dot(v);
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;
96 r0_norm = r0.stableNorm();
100 if (restarts++ == 0) i = 0;
106 z = precond.solve(s);
107 t.noalias() = mat * z;
109 RealScalar tmp = t.squaredNorm();
110 if (tmp > RealScalar(0)) {
115 x += alpha * y + w * z;
117 r_norm = r.stableNorm();
121 tol_error = r_norm / rhs_norm;
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) {
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);
138template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar> >
143template <
typename MatrixType_,
typename Preconditioner_>
144struct traits<BiCGSTAB<MatrixType_, Preconditioner_> > {
145 using MatrixType = MatrixType_;
146 using Preconditioner = Preconditioner_;
182template <
typename MatrixType_,
typename Preconditioner_>
188 using Base::m_isInitialized;
189 using Base::m_iterations;
193 using MatrixType = MatrixType_;
194 using Scalar =
typename MatrixType::Scalar;
195 using RealScalar =
typename MatrixType::RealScalar;
196 using Preconditioner = Preconditioner_;
212 template <
typename MatrixDerived>
216 template <
typename Rhs,
typename Dest>
217 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
219 m_error = Base::m_tolerance;
221 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:183
BiCGSTAB(const EigenBase< MatrixDerived > &A)
Definition BiCGSTAB.h:213
BiCGSTAB()
Definition BiCGSTAB.h:200
IterativeSolverBase()
Definition IterativeSolverBase.h:194
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