BiCGSTAB.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
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 namespace Eigen {
15 
16 namespace internal {
17 
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)
32 {
33  using std::sqrt;
34  using std::abs;
35  typedef typename Dest::RealScalar RealScalar;
36  typedef typename Dest::Scalar Scalar;
37  typedef Matrix<Scalar,Dynamic,1> VectorType;
38  RealScalar tol = tol_error;
39  int maxIters = iters;
40 
41  int n = mat.cols();
42  VectorType r = rhs - mat * x;
43  VectorType r0 = r;
44 
45  RealScalar r0_sqnorm = r0.squaredNorm();
46  Scalar rho = 1;
47  Scalar alpha = 1;
48  Scalar w = 1;
49 
50  VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
51  VectorType y(n), z(n);
52  VectorType kt(n), ks(n);
53 
54  VectorType s(n), t(n);
55 
56  RealScalar tol2 = tol*tol;
57  int i = 0;
58 
59  while ( r.squaredNorm()/r0_sqnorm > tol2 && i<maxIters )
60  {
61  Scalar rho_old = rho;
62 
63  rho = r0.dot(r);
64  if (rho == Scalar(0)) return false; /* New search directions cannot be found */
65  Scalar beta = (rho/rho_old) * (alpha / w);
66  p = r + beta * (p - w * v);
67 
68  y = precond.solve(p);
69 
70  v.noalias() = mat * y;
71 
72  alpha = rho / r0.dot(v);
73  s = r - alpha * v;
74 
75  z = precond.solve(s);
76  t.noalias() = mat * z;
77 
78  w = t.dot(s) / t.squaredNorm();
79  x += alpha * y + w * z;
80  r = s - w * t;
81  ++i;
82  }
83  tol_error = sqrt(r.squaredNorm()/r0_sqnorm);
84  iters = i;
85  return true;
86 }
87 
88 }
89 
90 template< typename _MatrixType,
91  typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
92 class BiCGSTAB;
93 
94 namespace internal {
95 
96 template< typename _MatrixType, typename _Preconditioner>
97 struct traits<BiCGSTAB<_MatrixType,_Preconditioner> >
98 {
99  typedef _MatrixType MatrixType;
100  typedef _Preconditioner Preconditioner;
101 };
102 
103 }
104 
151 template< typename _MatrixType, typename _Preconditioner>
152 class BiCGSTAB : public IterativeSolverBase<BiCGSTAB<_MatrixType,_Preconditioner> >
153 {
154  typedef IterativeSolverBase<BiCGSTAB> Base;
155  using Base::mp_matrix;
156  using Base::m_error;
157  using Base::m_iterations;
158  using Base::m_info;
159  using Base::m_isInitialized;
160 public:
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;
166 
167 public:
168 
170  BiCGSTAB() : Base() {}
171 
182  BiCGSTAB(const MatrixType& A) : Base(A) {}
183 
184  ~BiCGSTAB() {}
185 
191  template<typename Rhs,typename Guess>
192  inline const internal::solve_retval_with_guess<BiCGSTAB, Rhs, Guess>
193  solveWithGuess(const MatrixBase<Rhs>& b, const Guess& x0) const
194  {
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);
200  }
201 
203  template<typename Rhs,typename Dest>
204  void _solveWithGuess(const Rhs& b, Dest& x) const
205  {
206  bool failed = false;
207  for(int j=0; j<b.cols(); ++j)
208  {
209  m_iterations = Base::maxIterations();
210  m_error = Base::m_tolerance;
211 
212  typename Dest::ColXpr xj(x,j);
213  if(!internal::bicgstab(*mp_matrix, b.col(j), xj, Base::m_preconditioner, m_iterations, m_error))
214  failed = true;
215  }
216  m_info = failed ? NumericalIssue
217  : m_error <= Base::m_tolerance ? Success
218  : NoConvergence;
219  m_isInitialized = true;
220  }
221 
223  template<typename Rhs,typename Dest>
224  void _solve(const Rhs& b, Dest& x) const
225  {
226  x.setZero();
227  _solveWithGuess(b,x);
228  }
229 
230 protected:
231 
232 };
233 
234 
235 namespace internal {
236 
237  template<typename _MatrixType, typename _Preconditioner, typename Rhs>
238 struct solve_retval<BiCGSTAB<_MatrixType, _Preconditioner>, Rhs>
239  : solve_retval_base<BiCGSTAB<_MatrixType, _Preconditioner>, Rhs>
240 {
241  typedef BiCGSTAB<_MatrixType, _Preconditioner> Dec;
242  EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
243 
244  template<typename Dest> void evalTo(Dest& dst) const
245  {
246  dec()._solve(rhs(),dst);
247  }
248 };
249 
250 } // end namespace internal
251 
252 } // end namespace Eigen
253 
254 #endif // EIGEN_BICGSTAB_H