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 | |
---|
18 | /** \internal Low-level bi conjugate gradient stabilized algorithm |
---|
19 | * \param mat The matrix A |
---|
20 | * \param rhs The right hand side vector b |
---|
21 | * \param x On input and initial solution, on output the computed solution. |
---|
22 | * \param precond A preconditioner being able to efficiently solve for an |
---|
23 | * approximation of Ax=b (regardless of b) |
---|
24 | * \param iters On input the max number of iteration, on output the number of performed iterations. |
---|
25 | * \param tol_error On input the tolerance error, on output an estimation of the relative error. |
---|
26 | * \return false in the case of numerical issue, for example a break down of BiCGSTAB. |
---|
27 | */ |
---|
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 | |
---|
105 | /** \ingroup IterativeLinearSolvers_Module |
---|
106 | * \brief A bi conjugate gradient stabilized solver for sparse square problems |
---|
107 | * |
---|
108 | * This class allows to solve for A.x = b sparse linear problems using a bi conjugate gradient |
---|
109 | * stabilized algorithm. The vectors x and b can be either dense or sparse. |
---|
110 | * |
---|
111 | * \tparam _MatrixType the type of the sparse matrix A, can be a dense or a sparse matrix. |
---|
112 | * \tparam _Preconditioner the type of the preconditioner. Default is DiagonalPreconditioner |
---|
113 | * |
---|
114 | * The maximal number of iterations and tolerance value can be controlled via the setMaxIterations() |
---|
115 | * and setTolerance() methods. The defaults are the size of the problem for the maximal number of iterations |
---|
116 | * and NumTraits<Scalar>::epsilon() for the tolerance. |
---|
117 | * |
---|
118 | * This class can be used as the direct solver classes. Here is a typical usage example: |
---|
119 | * \code |
---|
120 | * int n = 10000; |
---|
121 | * VectorXd x(n), b(n); |
---|
122 | * SparseMatrix<double> A(n,n); |
---|
123 | * // fill A and b |
---|
124 | * BiCGSTAB<SparseMatrix<double> > solver; |
---|
125 | * solver(A); |
---|
126 | * x = solver.solve(b); |
---|
127 | * std::cout << "#iterations: " << solver.iterations() << std::endl; |
---|
128 | * std::cout << "estimated error: " << solver.error() << std::endl; |
---|
129 | * // update b, and solve again |
---|
130 | * x = solver.solve(b); |
---|
131 | * \endcode |
---|
132 | * |
---|
133 | * By default the iterations start with x=0 as an initial guess of the solution. |
---|
134 | * One can control the start using the solveWithGuess() method. Here is a step by |
---|
135 | * step execution example starting with a random guess and printing the evolution |
---|
136 | * of the estimated error: |
---|
137 | * * \code |
---|
138 | * x = VectorXd::Random(n); |
---|
139 | * solver.setMaxIterations(1); |
---|
140 | * int i = 0; |
---|
141 | * do { |
---|
142 | * x = solver.solveWithGuess(b,x); |
---|
143 | * std::cout << i << " : " << solver.error() << std::endl; |
---|
144 | * ++i; |
---|
145 | * } while (solver.info()!=Success && i<100); |
---|
146 | * \endcode |
---|
147 | * Note that such a step by step excution is slightly slower. |
---|
148 | * |
---|
149 | * \sa class SimplicialCholesky, DiagonalPreconditioner, IdentityPreconditioner |
---|
150 | */ |
---|
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 | |
---|
169 | /** Default constructor. */ |
---|
170 | BiCGSTAB() : Base() {} |
---|
171 | |
---|
172 | /** Initialize the solver with matrix \a A for further \c Ax=b solving. |
---|
173 | * |
---|
174 | * This constructor is a shortcut for the default constructor followed |
---|
175 | * by a call to compute(). |
---|
176 | * |
---|
177 | * \warning this class stores a reference to the matrix A as well as some |
---|
178 | * precomputed values that depend on it. Therefore, if \a A is changed |
---|
179 | * this class becomes invalid. Call compute() to update it with the new |
---|
180 | * matrix A, or modify a copy of A. |
---|
181 | */ |
---|
182 | BiCGSTAB(const MatrixType& A) : Base(A) {} |
---|
183 | |
---|
184 | ~BiCGSTAB() {} |
---|
185 | |
---|
186 | /** \returns the solution x of \f$ A x = b \f$ using the current decomposition of A |
---|
187 | * \a x0 as an initial solution. |
---|
188 | * |
---|
189 | * \sa compute() |
---|
190 | */ |
---|
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 | |
---|
202 | /** \internal */ |
---|
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 | |
---|
222 | /** \internal */ |
---|
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 |
---|