10 #ifndef EIGEN_DGMRES_H 11 #define EIGEN_DGMRES_H 13 #include <Eigen/Eigenvalues> 17 template<
typename _MatrixType,
18 typename _Preconditioner = DiagonalPreconditioner<typename _MatrixType::Scalar> >
23 template<
typename _MatrixType,
typename _Preconditioner>
26 typedef _MatrixType MatrixType;
27 typedef _Preconditioner Preconditioner;
38 template <
typename VectorType,
typename IndexType>
41 eigen_assert(vec.size() == perm.size());
45 for (Index k = 0; k < ncut; k++)
48 for (Index j = 0; j < vec.size()-1; j++)
50 if ( vec(perm(j)) < vec(perm(j+1)) )
52 std::swap(perm(j),perm(j+1));
100 template<
typename _MatrixType,
typename _Preconditioner>
104 using Base::mp_matrix;
106 using Base::m_iterations;
108 using Base::m_isInitialized;
109 using Base::m_tolerance;
113 typedef typename MatrixType::Index Index;
114 typedef typename MatrixType::RealScalar RealScalar;
115 typedef _Preconditioner Preconditioner;
124 DGMRES() : Base(),m_restart(30),m_neig(0),m_r(0),m_maxNeig(5),m_isDeflAllocated(false),m_isDeflInitialized(false) {}
136 template<
typename MatrixDerived>
137 explicit DGMRES(
const EigenBase<MatrixDerived>& A) : Base(A.derived()), m_restart(30),m_neig(0),m_r(0),m_maxNeig(5),m_isDeflAllocated(false),m_isDeflInitialized(false) {}
146 template<
typename Rhs,
typename Guess>
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);
158 template<
typename Rhs,
typename Dest>
159 void _solveWithGuess(
const Rhs& b, Dest& x)
const 162 for(
int j=0; j<b.cols(); ++j)
164 m_iterations = Base::maxIterations();
165 m_error = Base::m_tolerance;
167 typename Dest::ColXpr xj(x,j);
168 dgmres(*mp_matrix, b.col(j), xj, Base::m_preconditioner);
171 : m_error <= Base::m_tolerance ?
Success 173 m_isInitialized =
true;
177 template<
typename Rhs,
typename Dest>
178 void _solve(
const Rhs& b, Dest& x)
const 181 _solveWithGuess(b,x);
199 if (neig+1 > m_maxNeig) m_maxNeig = neig+1;
214 template<
typename Rhs,
typename Dest>
215 void dgmres(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
const Preconditioner& precond)
const;
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;
220 int dgmresComputeDeflationData(
const MatrixType& mat,
const Preconditioner& precond,
const Index& it, Index& neig)
const;
222 template<
typename RhsType,
typename DestType>
223 int dgmresApplyDeflation(
const RhsType& In, DestType& Out)
const;
227 void dgmresInitDeflation(Index& rows)
const;
228 mutable DenseMatrix m_V;
229 mutable DenseMatrix m_H;
230 mutable DenseMatrix m_Hes;
231 mutable Index m_restart;
232 mutable DenseMatrix m_U;
233 mutable DenseMatrix m_MU;
234 mutable DenseMatrix m_T;
238 mutable int m_maxNeig;
239 mutable RealScalar m_lambdaN;
240 mutable bool m_isDeflAllocated;
241 mutable bool m_isDeflInitialized;
244 mutable RealScalar m_smv;
245 mutable bool m_force;
254 template<
typename _MatrixType,
typename _Preconditioner>
255 template<
typename Rhs,
typename Dest>
257 const Preconditioner& precond)
const 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);
267 x = precond.solve(x);
269 RealScalar beta = r0.norm();
270 RealScalar normRhs = rhs.norm();
271 m_error = beta/normRhs;
272 if(m_error < m_tolerance)
280 dgmresCycle(mat, precond, x, r0, beta, normRhs, nbIts);
298 template<
typename _MatrixType,
typename _Preconditioner>
299 template<
typename Dest>
306 m_V.col(0) = r0/beta;
308 std::vector<JacobiRotation<Scalar> >gr(m_restart);
312 while (m_info ==
NoConvergence && it < m_restart && nbIts < m_iterations)
315 if (m_isDeflInitialized )
317 dgmresApplyDeflation(m_V.col(it), tv1);
318 tv2 = precond.solve(tv1);
322 tv2 = precond.solve(m_V.col(it));
328 for (
int i = 0; i <= it; ++i)
330 coef = tv1.dot(m_V.col(i));
331 tv1 = tv1 - coef * m_V.col(i);
337 m_V.col(it+1) = tv1/coef;
338 m_H(it+1, it) = coef;
344 for (
int i = 1; i <= it; ++i)
346 m_H.col(it).applyOnTheLeft(i-1,i,gr[i-1].adjoint());
349 gr[it].makeGivens(m_H(it, it), m_H(it+1,it));
351 m_H.col(it).applyOnTheLeft(it,it+1,gr[it].adjoint());
352 g.applyOnTheLeft(it,it+1, gr[it].adjoint());
354 beta = std::abs(g(it+1));
355 m_error = beta/normRhs;
356 std::cerr << nbIts <<
" Relative Residual Norm " << m_error << std::endl;
359 if (m_error < m_tolerance)
371 nrs = m_H.topLeftCorner(it,it).template triangularView<Upper>().solve(g.head(it));
374 if (m_isDeflInitialized)
376 tv1 = m_V.leftCols(it) * nrs;
377 dgmresApplyDeflation(tv1, tv2);
378 x = x + precond.solve(tv2);
381 x = x + precond.solve(m_V.leftCols(it) * nrs);
384 if(nbIts < m_iterations && m_info == NoConvergence && m_neig > 0 && (m_r+m_neig) < m_maxNeig)
385 dgmresComputeDeflationData(mat, precond, it, m_neig);
391 template<
typename _MatrixType,
typename _Preconditioner>
394 m_U.resize(rows, m_maxNeig);
395 m_MU.resize(rows, m_maxNeig);
396 m_T.resize(m_maxNeig, m_maxNeig);
398 m_isDeflAllocated =
true;
401 template<
typename _MatrixType,
typename _Preconditioner>
404 return schurofH.
matrixT().diagonal();
407 template<
typename _MatrixType,
typename _Preconditioner>
410 typedef typename MatrixType::Index Index;
419 eig(j) = std::complex<RealScalar>(T(j,j),RealScalar(0));
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));
429 if (j < it-1) eig(j) = std::complex<RealScalar>(T(j,j),RealScalar(0));
433 template<
typename _MatrixType,
typename _Preconditioner>
438 bool computeU =
true;
440 matrixQ.setIdentity();
441 schurofH.computeFromHessenberg(m_Hes.topLeftCorner(it,it), matrixQ, computeU);
445 eig = this->schurValues(schurofH);
449 for (
int j=0; j<it; ++j) modulEig(j) = std::abs(eig(j));
450 perm.setLinSpaced(it,0,it-1);
455 m_lambdaN = (std::max)(modulEig.maxCoeff(), m_lambdaN);
459 while (nbrEig < neig)
461 if(eig(perm(it-nbrEig-1)).imag() == RealScalar(0)) nbrEig++;
467 for (
int j = 0; j < nbrEig; j++)
469 Sr.col(j) = schurofH.matrixU().col(perm(it-j-1));
474 X = m_V.leftCols(it) * Sr;
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);
484 Index m = m_V.rows();
485 if (!m_isDeflAllocated)
486 dgmresInitDeflation(m);
489 for (
int j = 0; j < nbrEig; j++)
491 tv1 = mat * X.col(j);
492 MX.col(j) = precond.solve(tv1);
496 m_T.block(m_r, m_r, nbrEig, nbrEig) = X.transpose() * MX;
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);
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);
510 m_luT.compute(m_T.topLeftCorner(m_r, m_r));
513 m_isDeflInitialized =
true;
516 template<
typename _MatrixType,
typename _Preconditioner>
517 template<
typename RhsType,
typename DestType>
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);
527 template<
typename _MatrixType,
typename _Preconditioner,
typename Rhs>
532 EIGEN_MAKE_SOLVE_HELPERS(Dec,Rhs)
534 template<
typename Dest>
void evalTo(Dest& dst)
const 536 dec()._solve(rhs(),dst);
const internal::solve_retval_with_guess< DGMRES, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Definition: DGMRES.h:148
Definition: gtest_unittest.cc:5031
Definition: ForwardDeclarations.h:124
int restart()
Get the restart value.
Definition: DGMRES.h:186
A Restarted GMRES with deflation.
Definition: DGMRES.h:19
Definition: RealSchur.h:54
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
int deflSize()
Get the size of the deflation subspace size.
Definition: DGMRES.h:205
LU decomposition of a matrix with partial pivoting, and related features.
Definition: ForwardDeclarations.h:217
iterative scaling algorithm to equilibrate rows and column norms in matrices
Definition: TestIMU_Common.h:87
void sortWithPermutation(VectorType &vec, IndexType &perm, typename IndexType::Scalar &ncut)
Computes a permutation vector to have a sorted sequence.
Definition: DGMRES.h:39
Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor Matrix...
Definition: EigenBase.h:26
The provided data did not satisfy the prerequisites.
Definition: Constants.h:378
Definition: SparseSolve.h:86
Derived & setZero(Index size)
Resizes to the given size, and sets all coefficients in this expression to zero.
Definition: CwiseNullaryOp.h:515
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
Definition: RealSchur.h:143
Computation was successful.
Definition: Constants.h:376
DGMRES()
Default constructor.
Definition: DGMRES.h:124
Definition: BandTriangularSolver.h:13
void set_restart(const int restart)
Set the restart value (default is 30)
Definition: DGMRES.h:191
void setEigenv(const int neig)
Set the number of eigenvalues to deflate at each restart.
Definition: DGMRES.h:196
Definition: ForwardDeclarations.h:125
const int Dynamic
This value means that a positive quantity (e.g., a size) is not known at compile-time, and that instead the value is stored in some runtime variable.
Definition: Constants.h:21
Definition: ComplexSchur.h:51
Base class for linear iterative solvers.
Definition: IterativeSolverBase.h:21
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
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
void setMaxEigenv(const int maxNeig)
Set the maximum size of the deflation subspace.
Definition: DGMRES.h:210
Iterative procedure did not converge.
Definition: Constants.h:380
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Definition: ComplexSchur.h:161
Definition: ForwardDeclarations.h:17
double Scalar
Common scalar type.
Definition: FlexibleKalmanBase.h:48
DGMRES(const EigenBase< MatrixDerived > &A)
Initialize the solver with matrix A for further Ax=b solving.
Definition: DGMRES.h:137