17#include "./InternalHeaderCheck.h"
32template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
33EIGEN_DONT_INLINE
void minres(
const MatrixType& mat,
const Rhs& rhs, Dest& x,
const Preconditioner& precond,
34 Index& iters,
typename Dest::RealScalar& tol_error) {
35 using RealScalar =
typename Dest::RealScalar;
36 using Scalar =
typename Dest::Scalar;
37 using VectorType = Matrix<Scalar, Dynamic, 1>;
40 const RealScalar rhsNorm(rhs.stableNorm());
49 const Index maxIters(iters);
50 const Index N(mat.cols());
53 const RealScalar threshold(
54 internal::iterative_solver_threshold(tol_error, rhsNorm, RealScalar((std::numeric_limits<RealScalar>::min)())));
58 VectorType v(VectorType::Zero(N));
59 VectorType v_new(rhs - mat * x);
60 RealScalar residualNorm(v_new.stableNorm());
61 if (residualNorm == 0 || residualNorm < threshold) {
63 tol_error = residualNorm / rhsNorm;
68 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
69 v_new /= residualScale;
71 VectorType w_new(precond.solve(v_new));
72 RealScalar beta_new2(v_new.dot(w_new));
73 eigen_assert(beta_new2 >= RealScalar(0) &&
"PRECONDITIONER IS NOT POSITIVE DEFINITE");
74 RealScalar beta_new(numext::sqrt(beta_new2));
75 const RealScalar beta_one(beta_new);
78 RealScalar c_old(1.0);
80 RealScalar s_old(0.0);
82 VectorType p_old(VectorType::Zero(N));
92 bool residualIsTrue =
true;
93 RealScalar referenceResidualNorm = (std::numeric_limits<RealScalar>::max)();
94 Index referenceIters = 0;
97 while (iters < maxIters) {
108 const RealScalar beta(beta_new);
114 v_new.noalias() = mat * w - beta * v_old;
115 const RealScalar alpha = v_new.dot(w);
117 w_new = precond.solve(v_new);
118 beta_new2 = v_new.dot(w_new);
119 eigen_assert(beta_new2 >= RealScalar(0) &&
"PRECONDITIONER IS NOT POSITIVE DEFINITE");
120 beta_new = numext::sqrt(beta_new2);
123 const RealScalar r2 = s * alpha + c * c_old * beta;
124 const RealScalar r3 = s_old * beta;
125 const RealScalar r1_hat = c * alpha - c_old * s * beta;
126 const RealScalar r1 = numext::hypot(r1_hat, beta_new);
135 p = (w - r2 * p_old - r3 * p_oold) / r1;
136 x += (residualScale * beta_one * c * eta) * p;
141 residualNorm *= numext::abs(s);
142 residualIsTrue =
false;
143 const bool krylovExhausted = beta_new == RealScalar(0);
144 if (residualNorm < threshold || krylovExhausted) {
145 residualNorm = (rhs - mat * x).stableNorm();
146 residualIsTrue =
true;
147 if (residualNorm < threshold || krylovExhausted) {
150 if (residualNorm <= RealScalar(0.5) * referenceResidualNorm) {
151 referenceResidualNorm = residualNorm;
152 referenceIters = iters;
153 }
else if (iters - referenceIters >= 5) {
162 if (!residualIsTrue) {
163 residualNorm = (rhs - mat * x).stableNorm();
165 tol_error = residualNorm / rhsNorm;
170template <
typename MatrixType_,
int UpLo_ = Lower,
typename Preconditioner_ = IdentityPreconditioner>
175template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
176struct traits<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
177 using MatrixType = MatrixType_;
178 using Preconditioner = Preconditioner_;
225template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
231 using Base::m_isInitialized;
232 using Base::m_iterations;
236 using Base::_solve_impl;
237 using MatrixType = MatrixType_;
238 using Scalar =
typename MatrixType::Scalar;
239 using RealScalar =
typename MatrixType::RealScalar;
240 using Preconditioner = Preconditioner_;
242 enum { UpLo = UpLo_ };
246 MINRES() : Base() { Base::m_tolerance = RealScalar(-1); }
258 template <
typename MatrixDerived>
260 Base::m_tolerance = RealScalar(-1);
279 RealScalar
tolerance()
const {
return Base::toleranceOrEps(); }
282 template <
typename Rhs,
typename Dest>
283 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
285 using ActualMatrixType =
typename Base::ActualMatrixType;
287 TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (
Lower |
Upper)) && (!MatrixType::IsRowMajor) &&
288 (!NumTraits<Scalar>::IsComplex)
290 using RowMajorWrapper =
291 std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType
const&>;
292 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (
Lower |
Upper)),
293 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
294 using SelfAdjointWrapper =
295 std::conditional_t<UpLo == (
Lower |
Upper), RowMajorWrapper,
296 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>;
301 RowMajorWrapper row_mat(matrix());
302 internal::minres(SelfAdjointWrapper(row_mat), b, x, Base::m_preconditioner, m_iterations, m_error);
303 m_info = Base::infoAtDefaultOrSetTolerance(m_error, tol);
IterativeSolverBase()
Definition IterativeSolverBase.h:194
Index maxIterations() const
Definition IterativeSolverBase.h:307
A minimal residual solver for sparse symmetric problems.
Definition MINRES.h:226
MINRES()
Definition MINRES.h:246
MINRES(const EigenBase< MatrixDerived > &A)
Definition MINRES.h:259
RealScalar tolerance() const
Definition MINRES.h:279
Expression of an array as a mathematical vector or matrix.
Definition ArrayWrapper.h:120
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
Definition EigenBase.h:34