1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2011-2014 Gael Guennebaud <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2012 Désiré Nuentsa-Wakam <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li
11*bf2c3715SXin Li #ifndef EIGEN_BICGSTAB_H
12*bf2c3715SXin Li #define EIGEN_BICGSTAB_H
13*bf2c3715SXin Li
14*bf2c3715SXin Li namespace Eigen {
15*bf2c3715SXin Li
16*bf2c3715SXin Li namespace internal {
17*bf2c3715SXin Li
18*bf2c3715SXin Li /** \internal Low-level bi conjugate gradient stabilized algorithm
19*bf2c3715SXin Li * \param mat The matrix A
20*bf2c3715SXin Li * \param rhs The right hand side vector b
21*bf2c3715SXin Li * \param x On input and initial solution, on output the computed solution.
22*bf2c3715SXin Li * \param precond A preconditioner being able to efficiently solve for an
23*bf2c3715SXin Li * approximation of Ax=b (regardless of b)
24*bf2c3715SXin Li * \param iters On input the max number of iteration, on output the number of performed iterations.
25*bf2c3715SXin Li * \param tol_error On input the tolerance error, on output an estimation of the relative error.
26*bf2c3715SXin Li * \return false in the case of numerical issue, for example a break down of BiCGSTAB.
27*bf2c3715SXin Li */
28*bf2c3715SXin Li template<typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
bicgstab(const MatrixType & mat,const Rhs & rhs,Dest & x,const Preconditioner & precond,Index & iters,typename Dest::RealScalar & tol_error)29*bf2c3715SXin Li bool bicgstab(const MatrixType& mat, const Rhs& rhs, Dest& x,
30*bf2c3715SXin Li const Preconditioner& precond, Index& iters,
31*bf2c3715SXin Li typename Dest::RealScalar& tol_error)
32*bf2c3715SXin Li {
33*bf2c3715SXin Li using std::sqrt;
34*bf2c3715SXin Li using std::abs;
35*bf2c3715SXin Li typedef typename Dest::RealScalar RealScalar;
36*bf2c3715SXin Li typedef typename Dest::Scalar Scalar;
37*bf2c3715SXin Li typedef Matrix<Scalar,Dynamic,1> VectorType;
38*bf2c3715SXin Li RealScalar tol = tol_error;
39*bf2c3715SXin Li Index maxIters = iters;
40*bf2c3715SXin Li
41*bf2c3715SXin Li Index n = mat.cols();
42*bf2c3715SXin Li VectorType r = rhs - mat * x;
43*bf2c3715SXin Li VectorType r0 = r;
44*bf2c3715SXin Li
45*bf2c3715SXin Li RealScalar r0_sqnorm = r0.squaredNorm();
46*bf2c3715SXin Li RealScalar rhs_sqnorm = rhs.squaredNorm();
47*bf2c3715SXin Li if(rhs_sqnorm == 0)
48*bf2c3715SXin Li {
49*bf2c3715SXin Li x.setZero();
50*bf2c3715SXin Li return true;
51*bf2c3715SXin Li }
52*bf2c3715SXin Li Scalar rho = 1;
53*bf2c3715SXin Li Scalar alpha = 1;
54*bf2c3715SXin Li Scalar w = 1;
55*bf2c3715SXin Li
56*bf2c3715SXin Li VectorType v = VectorType::Zero(n), p = VectorType::Zero(n);
57*bf2c3715SXin Li VectorType y(n), z(n);
58*bf2c3715SXin Li VectorType kt(n), ks(n);
59*bf2c3715SXin Li
60*bf2c3715SXin Li VectorType s(n), t(n);
61*bf2c3715SXin Li
62*bf2c3715SXin Li RealScalar tol2 = tol*tol*rhs_sqnorm;
63*bf2c3715SXin Li RealScalar eps2 = NumTraits<Scalar>::epsilon()*NumTraits<Scalar>::epsilon();
64*bf2c3715SXin Li Index i = 0;
65*bf2c3715SXin Li Index restarts = 0;
66*bf2c3715SXin Li
67*bf2c3715SXin Li while ( r.squaredNorm() > tol2 && i<maxIters )
68*bf2c3715SXin Li {
69*bf2c3715SXin Li Scalar rho_old = rho;
70*bf2c3715SXin Li
71*bf2c3715SXin Li rho = r0.dot(r);
72*bf2c3715SXin Li if (abs(rho) < eps2*r0_sqnorm)
73*bf2c3715SXin Li {
74*bf2c3715SXin Li // The new residual vector became too orthogonal to the arbitrarily chosen direction r0
75*bf2c3715SXin Li // Let's restart with a new r0:
76*bf2c3715SXin Li r = rhs - mat * x;
77*bf2c3715SXin Li r0 = r;
78*bf2c3715SXin Li rho = r0_sqnorm = r.squaredNorm();
79*bf2c3715SXin Li if(restarts++ == 0)
80*bf2c3715SXin Li i = 0;
81*bf2c3715SXin Li }
82*bf2c3715SXin Li Scalar beta = (rho/rho_old) * (alpha / w);
83*bf2c3715SXin Li p = r + beta * (p - w * v);
84*bf2c3715SXin Li
85*bf2c3715SXin Li y = precond.solve(p);
86*bf2c3715SXin Li
87*bf2c3715SXin Li v.noalias() = mat * y;
88*bf2c3715SXin Li
89*bf2c3715SXin Li alpha = rho / r0.dot(v);
90*bf2c3715SXin Li s = r - alpha * v;
91*bf2c3715SXin Li
92*bf2c3715SXin Li z = precond.solve(s);
93*bf2c3715SXin Li t.noalias() = mat * z;
94*bf2c3715SXin Li
95*bf2c3715SXin Li RealScalar tmp = t.squaredNorm();
96*bf2c3715SXin Li if(tmp>RealScalar(0))
97*bf2c3715SXin Li w = t.dot(s) / tmp;
98*bf2c3715SXin Li else
99*bf2c3715SXin Li w = Scalar(0);
100*bf2c3715SXin Li x += alpha * y + w * z;
101*bf2c3715SXin Li r = s - w * t;
102*bf2c3715SXin Li ++i;
103*bf2c3715SXin Li }
104*bf2c3715SXin Li tol_error = sqrt(r.squaredNorm()/rhs_sqnorm);
105*bf2c3715SXin Li iters = i;
106*bf2c3715SXin Li return true;
107*bf2c3715SXin Li }
108*bf2c3715SXin Li
109*bf2c3715SXin Li }
110*bf2c3715SXin Li
111*bf2c3715SXin Li template< typename _MatrixType,
112*bf2c3715SXin Li typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
113*bf2c3715SXin Li class BiCGSTAB;
114*bf2c3715SXin Li
115*bf2c3715SXin Li namespace internal {
116*bf2c3715SXin Li
117*bf2c3715SXin Li template< typename _MatrixType, typename _Preconditioner>
118*bf2c3715SXin Li struct traits<BiCGSTAB<_MatrixType,_Preconditioner> >
119*bf2c3715SXin Li {
120*bf2c3715SXin Li typedef _MatrixType MatrixType;
121*bf2c3715SXin Li typedef _Preconditioner Preconditioner;
122*bf2c3715SXin Li };
123*bf2c3715SXin Li
124*bf2c3715SXin Li }
125*bf2c3715SXin Li
126*bf2c3715SXin Li /** \ingroup IterativeLinearSolvers_Module
127*bf2c3715SXin Li * \brief A bi conjugate gradient stabilized solver for sparse square problems
128*bf2c3715SXin Li *
129*bf2c3715SXin Li * This class allows to solve for A.x = b sparse linear problems using a bi conjugate gradient
130*bf2c3715SXin Li * stabilized algorithm. The vectors x and b can be either dense or sparse.
131*bf2c3715SXin Li *
132*bf2c3715SXin Li * \tparam _MatrixType the type of the sparse matrix A, can be a dense or a sparse matrix.
133*bf2c3715SXin Li * \tparam _Preconditioner the type of the preconditioner. Default is DiagonalPreconditioner
134*bf2c3715SXin Li *
135*bf2c3715SXin Li * \implsparsesolverconcept
136*bf2c3715SXin Li *
137*bf2c3715SXin Li * The maximal number of iterations and tolerance value can be controlled via the setMaxIterations()
138*bf2c3715SXin Li * and setTolerance() methods. The defaults are the size of the problem for the maximal number of iterations
139*bf2c3715SXin Li * and NumTraits<Scalar>::epsilon() for the tolerance.
140*bf2c3715SXin Li *
141*bf2c3715SXin Li * The tolerance corresponds to the relative residual error: |Ax-b|/|b|
142*bf2c3715SXin Li *
143*bf2c3715SXin Li * \b Performance: when using sparse matrices, best performance is achied for a row-major sparse matrix format.
144*bf2c3715SXin Li * Moreover, in this case multi-threading can be exploited if the user code is compiled with OpenMP enabled.
145*bf2c3715SXin Li * See \ref TopicMultiThreading for details.
146*bf2c3715SXin Li *
147*bf2c3715SXin Li * This class can be used as the direct solver classes. Here is a typical usage example:
148*bf2c3715SXin Li * \include BiCGSTAB_simple.cpp
149*bf2c3715SXin Li *
150*bf2c3715SXin Li * By default the iterations start with x=0 as an initial guess of the solution.
151*bf2c3715SXin Li * One can control the start using the solveWithGuess() method.
152*bf2c3715SXin Li *
153*bf2c3715SXin Li * BiCGSTAB can also be used in a matrix-free context, see the following \link MatrixfreeSolverExample example \endlink.
154*bf2c3715SXin Li *
155*bf2c3715SXin Li * \sa class SimplicialCholesky, DiagonalPreconditioner, IdentityPreconditioner
156*bf2c3715SXin Li */
157*bf2c3715SXin Li template< typename _MatrixType, typename _Preconditioner>
158*bf2c3715SXin Li class BiCGSTAB : public IterativeSolverBase<BiCGSTAB<_MatrixType,_Preconditioner> >
159*bf2c3715SXin Li {
160*bf2c3715SXin Li typedef IterativeSolverBase<BiCGSTAB> Base;
161*bf2c3715SXin Li using Base::matrix;
162*bf2c3715SXin Li using Base::m_error;
163*bf2c3715SXin Li using Base::m_iterations;
164*bf2c3715SXin Li using Base::m_info;
165*bf2c3715SXin Li using Base::m_isInitialized;
166*bf2c3715SXin Li public:
167*bf2c3715SXin Li typedef _MatrixType MatrixType;
168*bf2c3715SXin Li typedef typename MatrixType::Scalar Scalar;
169*bf2c3715SXin Li typedef typename MatrixType::RealScalar RealScalar;
170*bf2c3715SXin Li typedef _Preconditioner Preconditioner;
171*bf2c3715SXin Li
172*bf2c3715SXin Li public:
173*bf2c3715SXin Li
174*bf2c3715SXin Li /** Default constructor. */
175*bf2c3715SXin Li BiCGSTAB() : Base() {}
176*bf2c3715SXin Li
177*bf2c3715SXin Li /** Initialize the solver with matrix \a A for further \c Ax=b solving.
178*bf2c3715SXin Li *
179*bf2c3715SXin Li * This constructor is a shortcut for the default constructor followed
180*bf2c3715SXin Li * by a call to compute().
181*bf2c3715SXin Li *
182*bf2c3715SXin Li * \warning this class stores a reference to the matrix A as well as some
183*bf2c3715SXin Li * precomputed values that depend on it. Therefore, if \a A is changed
184*bf2c3715SXin Li * this class becomes invalid. Call compute() to update it with the new
185*bf2c3715SXin Li * matrix A, or modify a copy of A.
186*bf2c3715SXin Li */
187*bf2c3715SXin Li template<typename MatrixDerived>
188*bf2c3715SXin Li explicit BiCGSTAB(const EigenBase<MatrixDerived>& A) : Base(A.derived()) {}
189*bf2c3715SXin Li
190*bf2c3715SXin Li ~BiCGSTAB() {}
191*bf2c3715SXin Li
192*bf2c3715SXin Li /** \internal */
193*bf2c3715SXin Li template<typename Rhs,typename Dest>
194*bf2c3715SXin Li void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const
195*bf2c3715SXin Li {
196*bf2c3715SXin Li m_iterations = Base::maxIterations();
197*bf2c3715SXin Li m_error = Base::m_tolerance;
198*bf2c3715SXin Li
199*bf2c3715SXin Li bool ret = internal::bicgstab(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error);
200*bf2c3715SXin Li
201*bf2c3715SXin Li m_info = (!ret) ? NumericalIssue
202*bf2c3715SXin Li : m_error <= Base::m_tolerance ? Success
203*bf2c3715SXin Li : NoConvergence;
204*bf2c3715SXin Li }
205*bf2c3715SXin Li
206*bf2c3715SXin Li protected:
207*bf2c3715SXin Li
208*bf2c3715SXin Li };
209*bf2c3715SXin Li
210*bf2c3715SXin Li } // end namespace Eigen
211*bf2c3715SXin Li
212*bf2c3715SXin Li #endif // EIGEN_BICGSTAB_H
213