Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/eigen3/unsupported/Eigen/src/IterativeSolvers/GMRES.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of Eigen, a lightweight C++ template library
0002 // for linear algebra.
0003 //
0004 // Copyright (C) 2011 Gael Guennebaud <gael.guennebaud@inria.fr>
0005 // Copyright (C) 2012, 2014 Kolja Brix <brix@igpm.rwth-aaachen.de>
0006 //
0007 // This Source Code Form is subject to the terms of the Mozilla
0008 // Public License v. 2.0. If a copy of the MPL was not distributed
0009 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
0010 
0011 #ifndef EIGEN_GMRES_H
0012 #define EIGEN_GMRES_H
0013 
0014 namespace Eigen {
0015 
0016 namespace internal {
0017 
0018 /**
0019 * Generalized Minimal Residual Algorithm based on the
0020 * Arnoldi algorithm implemented with Householder reflections.
0021 *
0022 * Parameters:
0023 *  \param mat       matrix of linear system of equations
0024 *  \param rhs       right hand side vector of linear system of equations
0025 *  \param x         on input: initial guess, on output: solution
0026 *  \param precond   preconditioner used
0027 *  \param iters     on input: maximum number of iterations to perform
0028 *                   on output: number of iterations performed
0029 *  \param restart   number of iterations for a restart
0030 *  \param tol_error on input: relative residual tolerance
0031 *                   on output: residuum achieved
0032 *
0033 * \sa IterativeMethods::bicgstab()
0034 *
0035 *
0036 * For references, please see:
0037 *
0038 * Saad, Y. and Schultz, M. H.
0039 * GMRES: A Generalized Minimal Residual Algorithm for Solving Nonsymmetric Linear Systems.
0040 * SIAM J.Sci.Stat.Comp. 7, 1986, pp. 856 - 869.
0041 *
0042 * Saad, Y.
0043 * Iterative Methods for Sparse Linear Systems.
0044 * Society for Industrial and Applied Mathematics, Philadelphia, 2003.
0045 *
0046 * Walker, H. F.
0047 * Implementations of the GMRES method.
0048 * Comput.Phys.Comm. 53, 1989, pp. 311 - 320.
0049 *
0050 * Walker, H. F.
0051 * Implementation of the GMRES Method using Householder Transformations.
0052 * SIAM J.Sci.Stat.Comp. 9, 1988, pp. 152 - 163.
0053 *
0054 */
0055 template<typename MatrixType, typename Rhs, typename Dest, typename Preconditioner>
0056 bool gmres(const MatrixType & mat, const Rhs & rhs, Dest & x, const Preconditioner & precond,
0057     Index &iters, const Index &restart, typename Dest::RealScalar & tol_error) {
0058 
0059   using std::sqrt;
0060   using std::abs;
0061 
0062   typedef typename Dest::RealScalar RealScalar;
0063   typedef typename Dest::Scalar Scalar;
0064   typedef Matrix < Scalar, Dynamic, 1 > VectorType;
0065   typedef Matrix < Scalar, Dynamic, Dynamic, ColMajor> FMatrixType;
0066 
0067   const RealScalar considerAsZero = (std::numeric_limits<RealScalar>::min)();
0068 
0069   if(rhs.norm() <= considerAsZero) 
0070   {
0071     x.setZero();
0072     tol_error = 0;
0073     return true;
0074   }
0075 
0076   RealScalar tol = tol_error;
0077   const Index maxIters = iters;
0078   iters = 0;
0079 
0080   const Index m = mat.rows();
0081 
0082   // residual and preconditioned residual
0083   VectorType p0 = rhs - mat*x;
0084   VectorType r0 = precond.solve(p0);
0085 
0086   const RealScalar r0Norm = r0.norm();
0087 
0088   // is initial guess already good enough?
0089   if(r0Norm == 0)
0090   {
0091     tol_error = 0;
0092     return true;
0093   }
0094 
0095   // storage for Hessenberg matrix and Householder data
0096   FMatrixType H   = FMatrixType::Zero(m, restart + 1);
0097   VectorType w    = VectorType::Zero(restart + 1);
0098   VectorType tau  = VectorType::Zero(restart + 1);
0099 
0100   // storage for Jacobi rotations
0101   std::vector < JacobiRotation < Scalar > > G(restart);
0102   
0103   // storage for temporaries
0104   VectorType t(m), v(m), workspace(m), x_new(m);
0105 
0106   // generate first Householder vector
0107   Ref<VectorType> H0_tail = H.col(0).tail(m - 1);
0108   RealScalar beta;
0109   r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
0110   w(0) = Scalar(beta);
0111   
0112   for (Index k = 1; k <= restart; ++k)
0113   {
0114     ++iters;
0115 
0116     v = VectorType::Unit(m, k - 1);
0117 
0118     // apply Householder reflections H_{1} ... H_{k-1} to v
0119     // TODO: use a HouseholderSequence
0120     for (Index i = k - 1; i >= 0; --i) {
0121       v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
0122     }
0123 
0124     // apply matrix M to v:  v = mat * v;
0125     t.noalias() = mat * v;
0126     v = precond.solve(t);
0127 
0128     // apply Householder reflections H_{k-1} ... H_{1} to v
0129     // TODO: use a HouseholderSequence
0130     for (Index i = 0; i < k; ++i) {
0131       v.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
0132     }
0133 
0134     if (v.tail(m - k).norm() != 0.0)
0135     {
0136       if (k <= restart)
0137       {
0138         // generate new Householder vector
0139         Ref<VectorType> Hk_tail = H.col(k).tail(m - k - 1);
0140         v.tail(m - k).makeHouseholder(Hk_tail, tau.coeffRef(k), beta);
0141 
0142         // apply Householder reflection H_{k} to v
0143         v.tail(m - k).applyHouseholderOnTheLeft(Hk_tail, tau.coeffRef(k), workspace.data());
0144       }
0145     }
0146 
0147     if (k > 1)
0148     {
0149       for (Index i = 0; i < k - 1; ++i)
0150       {
0151         // apply old Givens rotations to v
0152         v.applyOnTheLeft(i, i + 1, G[i].adjoint());
0153       }
0154     }
0155 
0156     if (k<m && v(k) != (Scalar) 0)
0157     {
0158       // determine next Givens rotation
0159       G[k - 1].makeGivens(v(k - 1), v(k));
0160 
0161       // apply Givens rotation to v and w
0162       v.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
0163       w.applyOnTheLeft(k - 1, k, G[k - 1].adjoint());
0164     }
0165 
0166     // insert coefficients into upper matrix triangle
0167     H.col(k-1).head(k) = v.head(k);
0168 
0169     tol_error = abs(w(k)) / r0Norm;
0170     bool stop = (k==m || tol_error < tol || iters == maxIters);
0171 
0172     if (stop || k == restart)
0173     {
0174       // solve upper triangular system
0175       Ref<VectorType> y = w.head(k);
0176       H.topLeftCorner(k, k).template triangularView <Upper>().solveInPlace(y);
0177 
0178       // use Horner-like scheme to calculate solution vector
0179       x_new.setZero();
0180       for (Index i = k - 1; i >= 0; --i)
0181       {
0182         x_new(i) += y(i);
0183         // apply Householder reflection H_{i} to x_new
0184         x_new.tail(m - i).applyHouseholderOnTheLeft(H.col(i).tail(m - i - 1), tau.coeffRef(i), workspace.data());
0185       }
0186 
0187       x += x_new;
0188 
0189       if(stop)
0190       {
0191         return true;
0192       }
0193       else
0194       {
0195         k=0;
0196 
0197         // reset data for restart
0198         p0.noalias() = rhs - mat*x;
0199         r0 = precond.solve(p0);
0200 
0201         // clear Hessenberg matrix and Householder data
0202         H.setZero();
0203         w.setZero();
0204         tau.setZero();
0205 
0206         // generate first Householder vector
0207         r0.makeHouseholder(H0_tail, tau.coeffRef(0), beta);
0208         w(0) = Scalar(beta);
0209       }
0210     }
0211   }
0212 
0213   return false;
0214 
0215 }
0216 
0217 }
0218 
0219 template< typename _MatrixType,
0220           typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
0221 class GMRES;
0222 
0223 namespace internal {
0224 
0225 template< typename _MatrixType, typename _Preconditioner>
0226 struct traits<GMRES<_MatrixType,_Preconditioner> >
0227 {
0228   typedef _MatrixType MatrixType;
0229   typedef _Preconditioner Preconditioner;
0230 };
0231 
0232 }
0233 
0234 /** \ingroup IterativeLinearSolvers_Module
0235   * \brief A GMRES solver for sparse square problems
0236   *
0237   * This class allows to solve for A.x = b sparse linear problems using a generalized minimal
0238   * residual method. The vectors x and b can be either dense or sparse.
0239   *
0240   * \tparam _MatrixType the type of the sparse matrix A, can be a dense or a sparse matrix.
0241   * \tparam _Preconditioner the type of the preconditioner. Default is DiagonalPreconditioner
0242   *
0243   * The maximal number of iterations and tolerance value can be controlled via the setMaxIterations()
0244   * and setTolerance() methods. The defaults are the size of the problem for the maximal number of iterations
0245   * and NumTraits<Scalar>::epsilon() for the tolerance.
0246   *
0247   * This class can be used as the direct solver classes. Here is a typical usage example:
0248   * \code
0249   * int n = 10000;
0250   * VectorXd x(n), b(n);
0251   * SparseMatrix<double> A(n,n);
0252   * // fill A and b
0253   * GMRES<SparseMatrix<double> > solver(A);
0254   * x = solver.solve(b);
0255   * std::cout << "#iterations:     " << solver.iterations() << std::endl;
0256   * std::cout << "estimated error: " << solver.error()      << std::endl;
0257   * // update b, and solve again
0258   * x = solver.solve(b);
0259   * \endcode
0260   *
0261   * By default the iterations start with x=0 as an initial guess of the solution.
0262   * One can control the start using the solveWithGuess() method.
0263   * 
0264   * GMRES can also be used in a matrix-free context, see the following \link MatrixfreeSolverExample example \endlink.
0265   *
0266   * \sa class SimplicialCholesky, DiagonalPreconditioner, IdentityPreconditioner
0267   */
0268 template< typename _MatrixType, typename _Preconditioner>
0269 class GMRES : public IterativeSolverBase<GMRES<_MatrixType,_Preconditioner> >
0270 {
0271   typedef IterativeSolverBase<GMRES> Base;
0272   using Base::matrix;
0273   using Base::m_error;
0274   using Base::m_iterations;
0275   using Base::m_info;
0276   using Base::m_isInitialized;
0277 
0278 private:
0279   Index m_restart;
0280 
0281 public:
0282   using Base::_solve_impl;
0283   typedef _MatrixType MatrixType;
0284   typedef typename MatrixType::Scalar Scalar;
0285   typedef typename MatrixType::RealScalar RealScalar;
0286   typedef _Preconditioner Preconditioner;
0287 
0288 public:
0289 
0290   /** Default constructor. */
0291   GMRES() : Base(), m_restart(30) {}
0292 
0293   /** Initialize the solver with matrix \a A for further \c Ax=b solving.
0294     *
0295     * This constructor is a shortcut for the default constructor followed
0296     * by a call to compute().
0297     *
0298     * \warning this class stores a reference to the matrix A as well as some
0299     * precomputed values that depend on it. Therefore, if \a A is changed
0300     * this class becomes invalid. Call compute() to update it with the new
0301     * matrix A, or modify a copy of A.
0302     */
0303   template<typename MatrixDerived>
0304   explicit GMRES(const EigenBase<MatrixDerived>& A) : Base(A.derived()), m_restart(30) {}
0305 
0306   ~GMRES() {}
0307 
0308   /** Get the number of iterations after that a restart is performed.
0309     */
0310   Index get_restart() { return m_restart; }
0311 
0312   /** Set the number of iterations after that a restart is performed.
0313     *  \param restart   number of iterations for a restarti, default is 30.
0314     */
0315   void set_restart(const Index restart) { m_restart=restart; }
0316 
0317   /** \internal */
0318   template<typename Rhs,typename Dest>
0319   void _solve_vector_with_guess_impl(const Rhs& b, Dest& x) const
0320   {
0321     m_iterations = Base::maxIterations();
0322     m_error = Base::m_tolerance;
0323     bool ret = internal::gmres(matrix(), b, x, Base::m_preconditioner, m_iterations, m_restart, m_error);
0324     m_info = (!ret) ? NumericalIssue
0325           : m_error <= Base::m_tolerance ? Success
0326           : NoConvergence;
0327   }
0328 
0329 protected:
0330 
0331 };
0332 
0333 } // end namespace Eigen
0334 
0335 #endif // EIGEN_GMRES_H