DGMRES.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2012 Désiré Nuentsa-Wakam <desire.nuentsa_wakam@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_DGMRES_H
11 #define EIGEN_DGMRES_H
12 
13 #include <Eigen/Eigenvalues>
14 
15 namespace Eigen {
16 
17 template< typename _MatrixType,
18  typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
19 class DGMRES;
20 
21 namespace internal {
22 
23 template< typename _MatrixType, typename _Preconditioner>
24 struct traits<DGMRES<_MatrixType,_Preconditioner> >
25 {
26  typedef _MatrixType MatrixType;
27  typedef _Preconditioner Preconditioner;
28 };
29 
38 template <typename VectorType, typename IndexType>
39 void sortWithPermutation (VectorType& vec, IndexType& perm, typename IndexType::Scalar& ncut)
40 {
41  eigen_assert(vec.size() == perm.size());
42  typedef typename IndexType::Scalar Index;
43  typedef typename VectorType::Scalar Scalar;
44  bool flag;
45  for (Index k = 0; k < ncut; k++)
46  {
47  flag = false;
48  for (Index j = 0; j < vec.size()-1; j++)
49  {
50  if ( vec(perm(j)) < vec(perm(j+1)) )
51  {
52  std::swap(perm(j),perm(j+1));
53  flag = true;
54  }
55  if (!flag) break; // The vector is in sorted order
56  }
57  }
58 }
59 
60 }
100 template< typename _MatrixType, typename _Preconditioner>
101 class DGMRES : public IterativeSolverBase<DGMRES<_MatrixType,_Preconditioner> >
102 {
104  using Base::mp_matrix;
105  using Base::m_error;
106  using Base::m_iterations;
107  using Base::m_info;
108  using Base::m_isInitialized;
109  using Base::m_tolerance;
110  public:
111  typedef _MatrixType MatrixType;
112  typedef typename MatrixType::Scalar Scalar;
113  typedef typename MatrixType::Index Index;
114  typedef typename MatrixType::RealScalar RealScalar;
115  typedef _Preconditioner Preconditioner;
121 
122 
124  DGMRES() : Base(),m_restart(30),m_neig(0),m_r(0),m_maxNeig(5),m_isDeflAllocated(false),m_isDeflInitialized(false) {}
125 
136  DGMRES(const MatrixType& A) : Base(A),m_restart(30),m_neig(0),m_r(0),m_maxNeig(5),m_isDeflAllocated(false),m_isDeflInitialized(false)
137  {}
138 
139  ~DGMRES() {}
140 
146  template<typename Rhs,typename Guess>
148  solveWithGuess(const MatrixBase<Rhs>& b, const Guess& x0) const
149  {
150  eigen_assert(m_isInitialized && "DGMRES is not initialized.");
151  eigen_assert(Base::rows()==b.rows()
152  && "DGMRES::solve(): invalid number of rows of the right hand side matrix b");
154  <DGMRES, Rhs, Guess>(*this, b.derived(), x0);
155  }
156 
158  template<typename Rhs,typename Dest>
159  void _solveWithGuess(const Rhs& b, Dest& x) const
160  {
161  bool failed = false;
162  for(int j=0; j<b.cols(); ++j)
163  {
164  m_iterations = Base::maxIterations();
165  m_error = Base::m_tolerance;
166 
167  typename Dest::ColXpr xj(x,j);
168  dgmres(*mp_matrix, b.col(j), xj, Base::m_preconditioner);
169  }
170  m_info = failed ? NumericalIssue
171  : m_error <= Base::m_tolerance ? Success
172  : NoConvergence;
173  m_isInitialized = true;
174  }
175 
177  template<typename Rhs,typename Dest>
178  void _solve(const Rhs& b, Dest& x) const
179  {
180  x = b;
181  _solveWithGuess(b,x);
182  }
186  int restart() { return m_restart; }
187 
191  void set_restart(const int restart) { m_restart=restart; }
192 
196  void setEigenv(const int neig)
197  {
198  m_neig = neig;
199  if (neig+1 > m_maxNeig) m_maxNeig = neig+1; // To allow for complex conjugates
200  }
201 
205  int deflSize() {return m_r; }
206 
210  void setMaxEigenv(const int maxNeig) { m_maxNeig = maxNeig; }
211 
212  protected:
213  // DGMRES algorithm
214  template<typename Rhs, typename Dest>
215  void dgmres(const MatrixType& mat,const Rhs& rhs, Dest& x, const Preconditioner& precond) const;
216  // Perform one cycle of GMRES
217  template<typename Dest>
218  int dgmresCycle(const MatrixType& mat, const Preconditioner& precond, Dest& x, DenseVector& r0, RealScalar& beta, const RealScalar& normRhs, int& nbIts) const;
219  // Compute data to use for deflation
220  int dgmresComputeDeflationData(const MatrixType& mat, const Preconditioner& precond, const Index& it, Index& neig) const;
221  // Apply deflation to a vector
222  template<typename RhsType, typename DestType>
223  int dgmresApplyDeflation(const RhsType& In, DestType& Out) const;
224  ComplexVector schurValues(const ComplexSchur<DenseMatrix>& schurofH) const;
225  ComplexVector schurValues(const RealSchur<DenseMatrix>& schurofH) const;
226  // Init data for deflation
227  void dgmresInitDeflation(Index& rows) const;
228  mutable DenseMatrix m_V; // Krylov basis vectors
229  mutable DenseMatrix m_H; // Hessenberg matrix
230  mutable DenseMatrix m_Hes; // Initial hessenberg matrix wihout Givens rotations applied
231  mutable Index m_restart; // Maximum size of the Krylov subspace
232  mutable DenseMatrix m_U; // Vectors that form the basis of the invariant subspace
233  mutable DenseMatrix m_MU; // matrix operator applied to m_U (for next cycles)
234  mutable DenseMatrix m_T; /* T=U^T*M^{-1}*A*U */
235  mutable PartialPivLU<DenseMatrix> m_luT; // LU factorization of m_T
236  mutable int m_neig; //Number of eigenvalues to extract at each restart
237  mutable int m_r; // Current number of deflated eigenvalues, size of m_U
238  mutable int m_maxNeig; // Maximum number of eigenvalues to deflate
239  mutable RealScalar m_lambdaN; //Modulus of the largest eigenvalue of A
240  mutable bool m_isDeflAllocated;
241  mutable bool m_isDeflInitialized;
242 
243  //Adaptive strategy
244  mutable RealScalar m_smv; // Smaller multiple of the remaining number of steps allowed
245  mutable bool m_force; // Force the use of deflation at each restart
246 
247 };
254 template< typename _MatrixType, typename _Preconditioner>
255 template<typename Rhs, typename Dest>
256 void DGMRES<_MatrixType, _Preconditioner>::dgmres(const MatrixType& mat,const Rhs& rhs, Dest& x,
257  const Preconditioner& precond) const
258 {
259  //Initialization
260  int n = mat.rows();
261  DenseVector r0(n);
262  int nbIts = 0;
263  m_H.resize(m_restart+1, m_restart);
264  m_Hes.resize(m_restart, m_restart);
265  m_V.resize(n,m_restart+1);
266  //Initial residual vector and intial norm
267  x = precond.solve(x);
268  r0 = rhs - mat * x;
269  RealScalar beta = r0.norm();
270  RealScalar normRhs = rhs.norm();
271  m_error = beta/normRhs;
272  if(m_error < m_tolerance)
273  m_info = Success;
274  else
275  m_info = NoConvergence;
276 
277  // Iterative process
278  while (nbIts < m_iterations && m_info == NoConvergence)
279  {
280  dgmresCycle(mat, precond, x, r0, beta, normRhs, nbIts);
281 
282  // Compute the new residual vector for the restart
283  if (nbIts < m_iterations && m_info == NoConvergence)
284  r0 = rhs - mat * x;
285  }
286 }
287 
298 template< typename _MatrixType, typename _Preconditioner>
299 template<typename Dest>
300 int DGMRES<_MatrixType, _Preconditioner>::dgmresCycle(const MatrixType& mat, const Preconditioner& precond, Dest& x, DenseVector& r0, RealScalar& beta, const RealScalar& normRhs, int& nbIts) const
301 {
302  //Initialization
303  DenseVector g(m_restart+1); // Right hand side of the least square problem
304  g.setZero();
305  g(0) = Scalar(beta);
306  m_V.col(0) = r0/beta;
307  m_info = NoConvergence;
308  std::vector<JacobiRotation<Scalar> >gr(m_restart); // Givens rotations
309  int it = 0; // Number of inner iterations
310  int n = mat.rows();
311  DenseVector tv1(n), tv2(n); //Temporary vectors
312  while (m_info == NoConvergence && it < m_restart && nbIts < m_iterations)
313  {
314  // Apply preconditioner(s) at right
315  if (m_isDeflInitialized )
316  {
317  dgmresApplyDeflation(m_V.col(it), tv1); // Deflation
318  tv2 = precond.solve(tv1);
319  }
320  else
321  {
322  tv2 = precond.solve(m_V.col(it)); // User's selected preconditioner
323  }
324  tv1 = mat * tv2;
325 
326  // Orthogonalize it with the previous basis in the basis using modified Gram-Schmidt
327  Scalar coef;
328  for (int i = 0; i <= it; ++i)
329  {
330  coef = tv1.dot(m_V.col(i));
331  tv1 = tv1 - coef * m_V.col(i);
332  m_H(i,it) = coef;
333  m_Hes(i,it) = coef;
334  }
335  // Normalize the vector
336  coef = tv1.norm();
337  m_V.col(it+1) = tv1/coef;
338  m_H(it+1, it) = coef;
339 // m_Hes(it+1,it) = coef;
340 
341  // FIXME Check for happy breakdown
342 
343  // Update Hessenberg matrix with Givens rotations
344  for (int i = 1; i <= it; ++i)
345  {
346  m_H.col(it).applyOnTheLeft(i-1,i,gr[i-1].adjoint());
347  }
348  // Compute the new plane rotation
349  gr[it].makeGivens(m_H(it, it), m_H(it+1,it));
350  // Apply the new rotation
351  m_H.col(it).applyOnTheLeft(it,it+1,gr[it].adjoint());
352  g.applyOnTheLeft(it,it+1, gr[it].adjoint());
353 
354  beta = std::abs(g(it+1));
355  m_error = beta/normRhs;
356  std::cerr << nbIts << " Relative Residual Norm " << m_error << std::endl;
357  it++; nbIts++;
358 
359  if (m_error < m_tolerance)
360  {
361  // The method has converged
362  m_info = Success;
363  break;
364  }
365  }
366 
367  // Compute the new coefficients by solving the least square problem
368 // it++;
369  //FIXME Check first if the matrix is singular ... zero diagonal
370  DenseVector nrs(m_restart);
371  nrs = m_H.topLeftCorner(it,it).template triangularView<Upper>().solve(g.head(it));
372 
373  // Form the new solution
374  if (m_isDeflInitialized)
375  {
376  tv1 = m_V.leftCols(it) * nrs;
377  dgmresApplyDeflation(tv1, tv2);
378  x = x + precond.solve(tv2);
379  }
380  else
381  x = x + precond.solve(m_V.leftCols(it) * nrs);
382 
383  // Go for a new cycle and compute data for deflation
384  if(nbIts < m_iterations && m_info == NoConvergence && m_neig > 0 && (m_r+m_neig) < m_maxNeig)
385  dgmresComputeDeflationData(mat, precond, it, m_neig);
386  return 0;
387 
388 }
389 
390 
391 template< typename _MatrixType, typename _Preconditioner>
393 {
394  m_U.resize(rows, m_maxNeig);
395  m_MU.resize(rows, m_maxNeig);
396  m_T.resize(m_maxNeig, m_maxNeig);
397  m_lambdaN = 0.0;
398  m_isDeflAllocated = true;
399 }
400 
401 template< typename _MatrixType, typename _Preconditioner>
403 {
404  return schurofH.matrixT().diagonal();
405 }
406 
407 template< typename _MatrixType, typename _Preconditioner>
409 {
410  typedef typename MatrixType::Index Index;
411  const DenseMatrix& T = schurofH.matrixT();
412  Index it = T.rows();
413  ComplexVector eig(it);
414  Index j = 0;
415  while (j < it-1)
416  {
417  if (T(j+1,j) ==Scalar(0))
418  {
419  eig(j) = std::complex<RealScalar>(T(j,j),RealScalar(0));
420  j++;
421  }
422  else
423  {
424  eig(j) = std::complex<RealScalar>(T(j,j),T(j+1,j));
425  eig(j+1) = std::complex<RealScalar>(T(j,j+1),T(j+1,j+1));
426  j++;
427  }
428  }
429  if (j < it-1) eig(j) = std::complex<RealScalar>(T(j,j),RealScalar(0));
430  return eig;
431 }
432 
433 template< typename _MatrixType, typename _Preconditioner>
435 {
436  // First, find the Schur form of the Hessenberg matrix H
438  bool computeU = true;
439  DenseMatrix matrixQ(it,it);
440  matrixQ.setIdentity();
441  schurofH.computeFromHessenberg(m_Hes.topLeftCorner(it,it), matrixQ, computeU);
442 
443  ComplexVector eig(it);
444  Matrix<Index,Dynamic,1>perm(it);
445  eig = this->schurValues(schurofH);
446 
447  // Reorder the absolute values of Schur values
448  DenseRealVector modulEig(it);
449  for (int j=0; j<it; ++j) modulEig(j) = std::abs(eig(j));
450  perm.setLinSpaced(it,0,it-1);
451  internal::sortWithPermutation(modulEig, perm, neig);
452 
453  if (!m_lambdaN)
454  {
455  m_lambdaN = (std::max)(modulEig.maxCoeff(), m_lambdaN);
456  }
457  //Count the real number of extracted eigenvalues (with complex conjugates)
458  int nbrEig = 0;
459  while (nbrEig < neig)
460  {
461  if(eig(perm(it-nbrEig-1)).imag() == RealScalar(0)) nbrEig++;
462  else nbrEig += 2;
463  }
464  // Extract the Schur vectors corresponding to the smallest Ritz values
465  DenseMatrix Sr(it, nbrEig);
466  Sr.setZero();
467  for (int j = 0; j < nbrEig; j++)
468  {
469  Sr.col(j) = schurofH.matrixU().col(perm(it-j-1));
470  }
471 
472  // Form the Schur vectors of the initial matrix using the Krylov basis
473  DenseMatrix X;
474  X = m_V.leftCols(it) * Sr;
475  if (m_r)
476  {
477  // Orthogonalize X against m_U using modified Gram-Schmidt
478  for (int j = 0; j < nbrEig; j++)
479  for (int k =0; k < m_r; k++)
480  X.col(j) = X.col(j) - (m_U.col(k).dot(X.col(j)))*m_U.col(k);
481  }
482 
483  // Compute m_MX = A * M^-1 * X
484  Index m = m_V.rows();
485  if (!m_isDeflAllocated)
486  dgmresInitDeflation(m);
487  DenseMatrix MX(m, nbrEig);
488  DenseVector tv1(m);
489  for (int j = 0; j < nbrEig; j++)
490  {
491  tv1 = mat * X.col(j);
492  MX.col(j) = precond.solve(tv1);
493  }
494 
495  //Update m_T = [U'MU U'MX; X'MU X'MX]
496  m_T.block(m_r, m_r, nbrEig, nbrEig) = X.transpose() * MX;
497  if(m_r)
498  {
499  m_T.block(0, m_r, m_r, nbrEig) = m_U.leftCols(m_r).transpose() * MX;
500  m_T.block(m_r, 0, nbrEig, m_r) = X.transpose() * m_MU.leftCols(m_r);
501  }
502 
503  // Save X into m_U and m_MX in m_MU
504  for (int j = 0; j < nbrEig; j++) m_U.col(m_r+j) = X.col(j);
505  for (int j = 0; j < nbrEig; j++) m_MU.col(m_r+j) = MX.col(j);
506  // Increase the size of the invariant subspace
507  m_r += nbrEig;
508 
509  // Factorize m_T into m_luT
510  m_luT.compute(m_T.topLeftCorner(m_r, m_r));
511 
512  //FIXME CHeck if the factorization was correctly done (nonsingular matrix)
513  m_isDeflInitialized = true;
514  return 0;
515 }
516 template<typename _MatrixType, typename _Preconditioner>
517 template<typename RhsType, typename DestType>
518 int DGMRES<_MatrixType, _Preconditioner>::dgmresApplyDeflation(const RhsType &x, DestType &y) const
519 {
520  DenseVector x1 = m_U.leftCols(m_r).transpose() * x;
521  y = x + m_U.leftCols(m_r) * ( m_lambdaN * m_luT.solve(x1) - x1);
522  return 0;
523 }
524 
525 namespace internal {
526 
527  template<typename _MatrixType, typename _Preconditioner, typename Rhs>
528 struct solve_retval<DGMRES<_MatrixType, _Preconditioner>, Rhs>
529  : solve_retval_base<DGMRES<_MatrixType, _Preconditioner>, Rhs>
530 {
532  EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
533 
534  template<typename Dest> void evalTo(Dest& dst) const
535  {
536  dec()._solve(rhs(),dst);
537  }
538 };
539 } // end namespace internal
540 
541 } // end namespace Eigen
542 #endif
int dgmresApplyDeflation(const RhsType &In, DestType &Out) const
Definition: DGMRES.h:518
int restart()
Definition: DGMRES.h:186
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
A Restarted GMRES with deflation. This class implements a modification of the GMRES solver for sparse...
Definition: DGMRES.h:19
DenseMatrix m_H
Definition: DGMRES.h:229
Performs a real Schur decomposition of a square matrix.
Definition: RealSchur.h:54
bool m_isDeflAllocated
Definition: DGMRES.h:240
DenseMatrix m_U
Definition: DGMRES.h:232
int dgmresComputeDeflationData(const MatrixType &mat, const Preconditioner &precond, const Index &it, Index &neig) const
Definition: DGMRES.h:434
int deflSize()
Definition: DGMRES.h:205
LU decomposition of a matrix with partial pivoting, and related features.
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: matrix.hpp:471
Block< Derived, internal::traits< Derived >::RowsAtCompileTime, 1,!IsRowMajor > ColXpr
Definition: BlockMethods.h:15
void dgmresInitDeflation(Index &rows) const
Definition: DGMRES.h:392
MatrixType::Index Index
Definition: DGMRES.h:113
MatrixType::RealScalar RealScalar
Definition: DGMRES.h:114
void _solveWithGuess(const Rhs &b, Dest &x) const
Definition: DGMRES.h:159
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition: RealSchur.h:143
int m_neig
Definition: DGMRES.h:236
void _solve(const Rhs &b, Dest &x) const
Definition: DGMRES.h:178
void sortWithPermutation(VectorType &vec, IndexType &perm, typename IndexType::Scalar &ncut)
Computes a permutation vector to have a sorted sequence.
Definition: DGMRES.h:39
DGMRES(const MatrixType &A)
Definition: DGMRES.h:136
EIGEN_STRONG_INLINE Index rows() const
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
MatrixType::Scalar Scalar
Definition: DGMRES.h:112
bool m_isDeflInitialized
Definition: DGMRES.h:241
ComplexVector schurValues(const ComplexSchur< DenseMatrix > &schurofH) const
Definition: DGMRES.h:402
DenseMatrix m_T
Definition: DGMRES.h:234
Matrix< RealScalar, Dynamic, 1 > DenseRealVector
Definition: DGMRES.h:119
PartialPivLU< DenseMatrix > m_luT
Definition: DGMRES.h:235
int dgmresCycle(const MatrixType &mat, const Preconditioner &precond, Dest &x, DenseVector &r0, RealScalar &beta, const RealScalar &normRhs, int &nbIts) const
Perform one restart cycle of DGMRES.
Definition: DGMRES.h:300
Matrix< RealScalar, Dynamic, Dynamic > DenseRealMatrix
Definition: DGMRES.h:117
DenseMatrix m_V
Definition: DGMRES.h:228
IterativeSolverBase< DGMRES > Base
Definition: DGMRES.h:103
_Preconditioner Preconditioner
Definition: DGMRES.h:115
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Definition: ComplexSchur.h:161
Derived & setZero(Index size)
Matrix< std::complex< RealScalar >, Dynamic, 1 > ComplexVector
Definition: DGMRES.h:120
Index m_restart
Definition: DGMRES.h:231
_MatrixType MatrixType
Definition: DGMRES.h:111
void rhs(const real_t *x, real_t *f)
bool m_force
Definition: DGMRES.h:245
void set_restart(const int restart)
Definition: DGMRES.h:191
void setEigenv(const int neig)
Definition: DGMRES.h:196
const internal::solve_retval_with_guess< DGMRES, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: DGMRES.h:148
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Definition: DGMRES.h:116
int m_maxNeig
Definition: DGMRES.h:238
DenseMatrix m_Hes
Definition: DGMRES.h:230
RealScalar m_lambdaN
Definition: DGMRES.h:239
#define EIGEN_MAKE_SOLVE_HELPERS(DecompositionType, Rhs)
Definition: Solve.h:61
RealScalar m_smv
Definition: DGMRES.h:244
Performs a complex Schur decomposition of a real or complex square matrix.
Definition: ComplexSchur.h:51
#define eigen_assert(x)
Base class for linear iterative solvers.
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
DenseMatrix m_MU
Definition: DGMRES.h:233
void setMaxEigenv(const int maxNeig)
Definition: DGMRES.h:210
Matrix< Scalar, Dynamic, 1 > DenseVector
Definition: DGMRES.h:118
void dgmres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond) const
Perform several cycles of restarted GMRES with modified Gram Schmidt,.
Definition: DGMRES.h:256


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:32