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;
44 for (Index k = 0; k < ncut; k++)
47 for (Index j = 0; j < vec.size()-1; j++)
49 if ( vec(perm(j)) < vec(perm(j+1)) )
101 template<
typename _MatrixType,
typename _Preconditioner>
107 using Base::m_iterations;
109 using Base::m_isInitialized;
110 using Base::m_tolerance;
112 using Base::_solve_impl;
114 typedef typename MatrixType::Scalar
Scalar;
127 DGMRES() : Base(),m_restart(30),m_neig(0),m_r(0),m_maxNeig(5),m_isDeflAllocated(false),m_isDeflInitialized(false) {}
139 template<
typename MatrixDerived>
140 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) {}
145 template<
typename Rhs,
typename Dest>
149 for(
int j=0; j<b.cols(); ++j)
151 m_iterations = Base::maxIterations();
152 m_error = Base::m_tolerance;
155 dgmres(matrix(), b.col(j), xj, Base::m_preconditioner);
158 : m_error <= Base::m_tolerance ?
Success 160 m_isInitialized =
true;
164 template<
typename Rhs,
typename Dest>
168 _solve_with_guess_impl(b,x.derived());
186 if (neig+1 > m_maxNeig) m_maxNeig = neig+1;
201 template<
typename Rhs,
typename Dest>
202 void dgmres(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
const Preconditioner& precond)
const;
204 template<
typename Dest>
205 int dgmresCycle(
const MatrixType& mat,
const Preconditioner& precond, Dest& x, DenseVector& r0, RealScalar& beta,
const RealScalar& normRhs,
int& nbIts)
const;
207 int dgmresComputeDeflationData(
const MatrixType& mat,
const Preconditioner& precond,
const Index& it, StorageIndex& neig)
const;
209 template<
typename RhsType,
typename DestType>
210 int dgmresApplyDeflation(
const RhsType& In, DestType& Out)
const;
214 void dgmresInitDeflation(Index& rows)
const;
241 template<
typename _MatrixType,
typename _Preconditioner>
242 template<
typename Rhs,
typename Dest>
250 m_H.resize(m_restart+1, m_restart);
251 m_Hes.resize(m_restart, m_restart);
252 m_V.resize(n,m_restart+1);
254 x = precond.solve(x);
258 m_error = beta/normRhs;
259 if(m_error < m_tolerance)
267 dgmresCycle(mat, precond, x, r0, beta, normRhs, nbIts);
285 template<
typename _MatrixType,
typename _Preconditioner>
286 template<
typename Dest>
293 m_V.col(0) = r0/beta;
295 std::vector<JacobiRotation<Scalar> >gr(m_restart);
299 while (m_info ==
NoConvergence && it < m_restart && nbIts < m_iterations)
302 if (m_isDeflInitialized )
304 dgmresApplyDeflation(m_V.col(it), tv1);
305 tv2 = precond.solve(tv1);
309 tv2 = precond.solve(m_V.col(it));
315 for (
int i = 0; i <= it; ++i)
317 coef = tv1.dot(m_V.col(i));
318 tv1 = tv1 - coef * m_V.col(i);
324 m_V.col(it+1) = tv1/coef;
325 m_H(it+1, it) = coef;
331 for (
int i = 1; i <= it; ++i)
333 m_H.col(it).applyOnTheLeft(i-1,i,gr[i-1].adjoint());
336 gr[it].makeGivens(m_H(it, it), m_H(it+1,it));
338 m_H.col(it).applyOnTheLeft(it,it+1,gr[it].adjoint());
339 g.applyOnTheLeft(it,it+1, gr[it].adjoint());
342 m_error = beta/normRhs;
346 if (m_error < m_tolerance)
358 nrs = m_H.topLeftCorner(it,it).template triangularView<Upper>().solve(g.head(it));
361 if (m_isDeflInitialized)
363 tv1 = m_V.leftCols(it) * nrs;
364 dgmresApplyDeflation(tv1, tv2);
365 x = x + precond.solve(tv2);
368 x = x + precond.solve(m_V.leftCols(it) * nrs);
371 if(nbIts < m_iterations && m_info == NoConvergence && m_neig > 0 && (m_r+m_neig) < m_maxNeig)
372 dgmresComputeDeflationData(mat, precond, it, m_neig);
378 template<
typename _MatrixType,
typename _Preconditioner>
381 m_U.resize(rows, m_maxNeig);
382 m_MU.resize(rows, m_maxNeig);
383 m_T.resize(m_maxNeig, m_maxNeig);
385 m_isDeflAllocated =
true;
388 template<
typename _MatrixType,
typename _Preconditioner>
391 return schurofH.
matrixT().diagonal();
394 template<
typename _MatrixType,
typename _Preconditioner>
406 eig(j) = std::complex<RealScalar>(T(j,j),
RealScalar(0));
411 eig(j) = std::complex<RealScalar>(T(j,j),T(j+1,j));
412 eig(j+1) = std::complex<RealScalar>(T(j,j+1),T(j+1,j+1));
416 if (j < it-1) eig(j) = std::complex<RealScalar>(T(j,j),
RealScalar(0));
420 template<
typename _MatrixType,
typename _Preconditioner>
425 bool computeU =
true;
427 matrixQ.setIdentity();
428 schurofH.computeFromHessenberg(m_Hes.topLeftCorner(it,it), matrixQ, computeU);
432 eig = this->schurValues(schurofH);
436 for (
int j=0; j<it; ++j) modulEig(j) =
std::abs(eig(j));
437 perm.setLinSpaced(it,0,it-1);
442 m_lambdaN = (
std::max)(modulEig.maxCoeff(), m_lambdaN);
446 while (nbrEig < neig)
448 if(eig(perm(it-nbrEig-1)).imag() ==
RealScalar(0)) nbrEig++;
454 for (
int j = 0; j < nbrEig; j++)
456 Sr.col(j) = schurofH.matrixU().col(perm(it-j-1));
461 X = m_V.leftCols(it) * Sr;
465 for (
int j = 0; j < nbrEig; j++)
466 for (
int k =0; k < m_r; k++)
467 X.col(j) = X.col(j) - (m_U.col(k).dot(X.col(j)))*m_U.col(k);
472 if (!m_isDeflAllocated)
473 dgmresInitDeflation(m);
476 for (
int j = 0; j < nbrEig; j++)
478 tv1 = mat * X.col(j);
479 MX.col(j) = precond.solve(tv1);
483 m_T.block(m_r, m_r, nbrEig, nbrEig) = X.transpose() * MX;
486 m_T.block(0, m_r, m_r, nbrEig) = m_U.leftCols(m_r).transpose() * MX;
487 m_T.block(m_r, 0, nbrEig, m_r) = X.transpose() * m_MU.leftCols(m_r);
491 for (
int j = 0; j < nbrEig; j++) m_U.col(m_r+j) = X.col(j);
492 for (
int j = 0; j < nbrEig; j++) m_MU.col(m_r+j) = MX.col(j);
497 m_luT.compute(m_T.topLeftCorner(m_r, m_r));
500 m_isDeflInitialized =
true;
503 template<
typename _MatrixType,
typename _Preconditioner>
504 template<
typename RhsType,
typename DestType>
507 DenseVector x1 = m_U.leftCols(m_r).transpose() * x;
508 y = x + m_U.leftCols(m_r) * ( m_lambdaN * m_luT.solve(x1) - x1);
int dgmresApplyDeflation(const RhsType &In, DestType &Out) const
EIGEN_DEVICE_FUNC Derived & setZero(Index size)
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.
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Index rows() const
_Preconditioner Preconditioner
LU decomposition of a matrix with partial pivoting, and related features.
Block< Derived, internal::traits< Derived >::RowsAtCompileTime, 1,!IsRowMajor > ColXpr
void dgmresInitDeflation(Index &rows) const
int dgmresComputeDeflationData(const MatrixType &mat, const Preconditioner &precond, const Index &it, StorageIndex &neig) const
MatrixType::RealScalar RealScalar
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const AbsReturnType abs() const
const MatrixType & matrixT() const
Returns the quasi-triangular matrix in the Schur decomposition.
void sortWithPermutation(VectorType &vec, IndexType &perm, typename IndexType::Scalar &ncut)
Computes a permutation vector to have a sorted sequence.
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC half() max(const half &a, const half &b)
MatrixType::Scalar Scalar
ComplexVector schurValues(const ComplexSchur< DenseMatrix > &schurofH) const
Matrix< RealScalar, Dynamic, 1 > DenseRealVector
void _solve_impl(const Rhs &b, MatrixBase< Dest > &x) const
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
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.
Matrix< std::complex< RealScalar >, Dynamic, 1 > ComplexVector
MatrixType::StorageIndex StorageIndex
void set_restart(const int restart)
void setEigenv(const int neig)
Matrix< Scalar, Dynamic, Dynamic > DenseMatrix
Performs a complex Schur decomposition of a real or complex square matrix.
Base class for linear iterative solvers.
EIGEN_DEVICE_FUNC const Scalar & b
Base class for all dense matrices, vectors, and expressions.
void setMaxEigenv(const int maxNeig)
void swap(mpfr::mpreal &x, mpfr::mpreal &y)
void _solve_with_guess_impl(const Rhs &b, Dest &x) const
Matrix< Scalar, Dynamic, 1 > DenseVector
DGMRES(const EigenBase< MatrixDerived > &A)
void dgmres(const MatrixType &mat, const Rhs &rhs, Dest &x, const Preconditioner &precond) const
Perform several cycles of restarted GMRES with modified Gram Schmidt,.