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();
54 RealScalar tol = tol_error * rhs_norm;
60 VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
61 VectorType y(n), z(n);
63 VectorType s(n), t(n);
65 RealScalar eps = NumTraits<Scalar>::epsilon();
69 while (r_norm > tol && i < maxIters) {
72 if (Eigen::numext::abs(rho) / Eigen::numext::maxi(r0_norm, r_norm) < eps * Eigen::numext::mini(r0_norm, r_norm)) {
75 r.noalias() = rhs - mat * x;
77 rho = r.squaredNorm();
78 r0_norm = r.stableNorm();
81 if (restarts++ == 0) i = 0;
83 Scalar beta = (rho / rho_old) * (alpha / w);
84 p = r + beta * (p - w * v);
88 v.noalias() = mat * y;
89 Scalar theta = r0.dot(v);
91 RealScalar v_norm = v.stableNorm();
92 if (Eigen::numext::abs(theta) / Eigen::numext::maxi(r0_norm, v_norm) < eps * Eigen::numext::mini(r0_norm, v_norm)) {
93 r.noalias() = rhs - mat * x;
95 r0_norm = r0.stableNorm();
99 if (restarts++ == 0) i = 0;
105 z = precond.solve(s);
106 t.noalias() = mat * z;
108 RealScalar tmp = t.squaredNorm();
109 if (tmp > RealScalar(0)) {
114 x += alpha * y + w * z;
116 r_norm = r.stableNorm();
120 tol_error = r_norm / rhs_norm;
127template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar> >
132template <
typename MatrixType_,
typename Preconditioner_>
133struct traits<BiCGSTAB<MatrixType_, Preconditioner_> > {
134 using MatrixType = MatrixType_;
135 using Preconditioner = Preconditioner_;
171template <
typename MatrixType_,
typename Preconditioner_>
177 using Base::m_isInitialized;
178 using Base::m_iterations;
182 using MatrixType = MatrixType_;
183 using Scalar =
typename MatrixType::Scalar;
184 using RealScalar =
typename MatrixType::RealScalar;
185 using Preconditioner = Preconditioner_;
201 template <
typename MatrixDerived>
205 template <
typename Rhs,
typename Dest>
206 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
208 m_error = Base::m_tolerance;
210 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:172
BiCGSTAB(const EigenBase< MatrixDerived > &A)
Definition BiCGSTAB.h:202
BiCGSTAB()
Definition BiCGSTAB.h:189
IterativeSolverBase()
Definition IterativeSolverBase.h:136
Index maxIterations() const
Definition IterativeSolverBase.h:246
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34