11 #ifndef EIGEN_BICGSTAB_H
12 #define EIGEN_BICGSTAB_H
28 template<
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
29 bool bicgstab(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
30 const Preconditioner& precond,
int& iters,
31 typename Dest::RealScalar& tol_error)
35 typedef typename Dest::RealScalar RealScalar;
36 typedef typename Dest::Scalar Scalar;
37 typedef Matrix<Scalar,Dynamic,1> VectorType;
38 RealScalar tol = tol_error;
42 VectorType r = rhs - mat * x;
45 RealScalar r0_sqnorm = r0.squaredNorm();
50 VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
51 VectorType y(n), z(n);
52 VectorType kt(n), ks(n);
54 VectorType s(n), t(n);
56 RealScalar tol2 = tol*tol;
59 while ( r.squaredNorm()/r0_sqnorm > tol2 && i<maxIters )
64 if (rho == Scalar(0))
return false;
65 Scalar beta = (rho/rho_old) * (alpha / w);
66 p = r + beta * (p - w * v);
70 v.noalias() = mat * y;
72 alpha = rho / r0.dot(v);
76 t.noalias() = mat * z;
78 w = t.dot(s) / t.squaredNorm();
79 x += alpha * y + w * z;
83 tol_error = sqrt(r.squaredNorm()/r0_sqnorm);
90 template<
typename _MatrixType,
91 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
96 template<
typename _MatrixType,
typename _Preconditioner>
97 struct traits<BiCGSTAB<_MatrixType,_Preconditioner> >
99 typedef _MatrixType MatrixType;
100 typedef _Preconditioner Preconditioner;
151 template<
typename _MatrixType,
typename _Preconditioner>
155 using Base::mp_matrix;
157 using Base::m_iterations;
159 using Base::m_isInitialized;
161 typedef _MatrixType MatrixType;
162 typedef typename MatrixType::Scalar Scalar;
163 typedef typename MatrixType::Index Index;
164 typedef typename MatrixType::RealScalar RealScalar;
165 typedef _Preconditioner Preconditioner;
191 template<
typename Rhs,
typename Guess>
192 inline const internal::solve_retval_with_guess<BiCGSTAB, Rhs, Guess>
195 eigen_assert(m_isInitialized &&
"BiCGSTAB is not initialized.");
196 eigen_assert(Base::rows()==b.rows()
197 &&
"BiCGSTAB::solve(): invalid number of rows of the right hand side matrix b");
198 return internal::solve_retval_with_guess
199 <
BiCGSTAB, Rhs, Guess>(*
this, b.derived(), x0);
203 template<
typename Rhs,
typename Dest>
204 void _solveWithGuess(
const Rhs& b, Dest& x)
const
207 for(
int j=0; j<b.cols(); ++j)
210 m_error = Base::m_tolerance;
212 typename Dest::ColXpr xj(x,j);
213 if(!internal::bicgstab(*mp_matrix, b.col(j), xj, Base::m_preconditioner, m_iterations, m_error))
217 : m_error <= Base::m_tolerance ?
Success
219 m_isInitialized =
true;
223 template<
typename Rhs,
typename Dest>
224 void _solve(
const Rhs& b, Dest& x)
const
227 _solveWithGuess(b,x);
237 template<
typename _MatrixType,
typename _Preconditioner,
typename Rhs>
238 struct solve_retval<BiCGSTAB<_MatrixType, _Preconditioner>, Rhs>
239 : solve_retval_base<BiCGSTAB<_MatrixType, _Preconditioner>, Rhs>
241 typedef BiCGSTAB<_MatrixType, _Preconditioner> Dec;
242 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
244 template<typename Dest>
void evalTo(Dest& dst)
const
246 dec()._solve(rhs(),dst);
254 #endif // EIGEN_BICGSTAB_H