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>
38 template <
typename VectorType,
typename IndexType>
42 typedef typename IndexType::Scalar Index;
43 typedef typename VectorType::Scalar Scalar;
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;
112 typedef typename MatrixType::Scalar
Scalar;
113 typedef typename MatrixType::Index
Index;
124 DGMRES() : Base(),m_restart(30),m_neig(0),m_r(0),m_maxNeig(5),m_isDeflAllocated(false),m_isDeflInitialized(false) {}
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)
146 template<
typename Rhs,
typename Guess>
150 eigen_assert(m_isInitialized &&
"DGMRES is not initialized.");
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>
162 for(
int j=0; j<b.cols(); ++j)
164 m_iterations = Base::maxIterations();
165 m_error = Base::m_tolerance;
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>
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;
254 template<
typename _MatrixType,
typename _Preconditioner>
255 template<
typename Rhs,
typename Dest>
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);
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());
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);
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>
534 template<typename Dest>
void evalTo(Dest& dst)
const 536 dec()._solve(
rhs(),dst);
int dgmresApplyDeflation(const RhsType &In, DestType &Out) const
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
A Restarted GMRES with deflation. This class implements a modification of the GMRES solver for sparse...
Performs a real Schur decomposition of a square matrix.
int dgmresComputeDeflationData(const MatrixType &mat, const Preconditioner &precond, const Index &it, Index &neig) const
_Preconditioner Preconditioner
LU decomposition of a matrix with partial pivoting, and related features.
iterative scaling algorithm to equilibrate rows and column norms in matrices
Block< Derived, internal::traits< Derived >::RowsAtCompileTime, 1,!IsRowMajor > ColXpr
void dgmresInitDeflation(Index &rows) const
MatrixType::RealScalar RealScalar
void _solveWithGuess(const Rhs &b, Dest &x) const
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
void _solve(const Rhs &b, Dest &x) const
void sortWithPermutation(VectorType &vec, IndexType &perm, typename IndexType::Scalar &ncut)
Computes a permutation vector to have a sorted sequence.
DGMRES(const MatrixType &A)
EIGEN_STRONG_INLINE Index rows() const
EIGEN_STRONG_INLINE const CwiseUnaryOp< internal::scalar_abs_op< Scalar >, const Derived > abs() const
MatrixType::Scalar Scalar
ComplexVector schurValues(const ComplexSchur< DenseMatrix > &schurofH) const
Matrix< RealScalar, Dynamic, 1 > DenseRealVector
PartialPivLU< DenseMatrix > m_luT
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.
Matrix< RealScalar, Dynamic, Dynamic > DenseRealMatrix
IterativeSolverBase< DGMRES > Base
_Preconditioner Preconditioner
const ComplexMatrixType & matrixT() const
Returns the triangular matrix in the Schur decomposition.
Derived & setZero(Index size)
Matrix< std::complex< RealScalar >, Dynamic, 1 > ComplexVector
void rhs(const real_t *x, real_t *f)
void set_restart(const int restart)
void setEigenv(const int neig)
const internal::solve_retval_with_guess< DGMRES, Rhs, Guess > solveWithGuess(const MatrixBase< Rhs > &b, const Guess &x0) const
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
#define EIGEN_MAKE_SOLVE_HELPERS(DecompositionType, Rhs)
Performs a complex Schur decomposition of a real or complex square matrix.
Base class for linear iterative solvers.
DGMRES< _MatrixType, _Preconditioner > Dec
Base class for all dense matrices, vectors, and expressions.
void setMaxEigenv(const int maxNeig)
Matrix< Scalar, Dynamic, 1 > DenseVector
void dgmres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond) const
Perform several cycles of restarted GMRES with modified Gram Schmidt,.