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) {
36 using RealScalar =
typename Dest::RealScalar;
37 using Scalar =
typename Dest::Scalar;
38 using VectorType = Matrix<Scalar, Dynamic, 1>;
41 const RealScalar rhsNorm(rhs.stableNorm());
50 const Index maxIters(iters);
51 const Index N(mat.cols());
52 const RealScalar threshold(tol_error * rhsNorm);
56 VectorType v(VectorType::Zero(N));
57 VectorType v_new(rhs - mat * x);
58 RealScalar residualNorm(v_new.stableNorm());
59 if (residualNorm == 0 || residualNorm < threshold) {
61 tol_error = residualNorm / rhsNorm;
66 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
67 v_new /= residualScale;
69 VectorType w_new(precond.solve(v_new));
70 RealScalar beta_new2(v_new.dot(w_new));
71 eigen_assert(beta_new2 >= 0.0 &&
"PRECONDITIONER IS NOT POSITIVE DEFINITE");
72 RealScalar beta_new(sqrt(beta_new2));
73 const RealScalar beta_one(beta_new);
76 RealScalar c_old(1.0);
78 RealScalar s_old(0.0);
80 VectorType p_old(VectorType::Zero(N));
85 while (iters < maxIters) {
96 const RealScalar beta(beta_new);
102 v_new.noalias() = mat * w - beta * v_old;
103 const RealScalar alpha = v_new.dot(w);
105 w_new = precond.solve(v_new);
106 beta_new2 = v_new.dot(w_new);
107 eigen_assert(beta_new2 >= 0.0 &&
"PRECONDITIONER IS NOT POSITIVE DEFINITE");
108 beta_new = sqrt(beta_new2);
111 const RealScalar r2 = s * alpha + c * c_old * beta;
112 const RealScalar r3 = s_old * beta;
113 const RealScalar r1_hat = c * alpha - c_old * s * beta;
114 const RealScalar r1 = numext::hypot(r1_hat, beta_new);
123 p = (w - r2 * p_old - r3 * p_oold) / r1;
124 x += (residualScale * beta_one * c * eta) * p;
128 residualNorm *= numext::abs(s);
130 if (residualNorm < threshold) {
140 tol_error = residualNorm / rhsNorm;
145template <
typename MatrixType_,
int UpLo_ = Lower,
typename Preconditioner_ = IdentityPreconditioner>
150template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
151struct traits<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
152 using MatrixType = MatrixType_;
153 using Preconditioner = Preconditioner_;
196template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
202 using Base::m_isInitialized;
203 using Base::m_iterations;
207 using Base::_solve_impl;
208 using MatrixType = MatrixType_;
209 using Scalar =
typename MatrixType::Scalar;
210 using RealScalar =
typename MatrixType::RealScalar;
211 using Preconditioner = Preconditioner_;
213 enum { UpLo = UpLo_ };
229 template <
typename MatrixDerived>
233 template <
typename Rhs,
typename Dest>
234 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
236 using ActualMatrixType =
typename Base::ActualMatrixType;
238 TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (
Lower |
Upper)) && (!MatrixType::IsRowMajor) &&
239 (!NumTraits<Scalar>::IsComplex)
241 using RowMajorWrapper =
242 std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType
const&>;
243 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (
Lower |
Upper)),
244 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
245 using SelfAdjointWrapper =
246 std::conditional_t<UpLo == (
Lower |
Upper), RowMajorWrapper,
247 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>;
250 m_error = Base::m_tolerance;
251 RowMajorWrapper row_mat(matrix());
252 internal::minres(SelfAdjointWrapper(row_mat), b, x, Base::m_preconditioner, m_iterations, m_error);
IterativeSolverBase()
Definition IterativeSolverBase.h:136
Index maxIterations() const
Definition IterativeSolverBase.h:245
A minimal residual solver for sparse symmetric problems.
Definition MINRES.h:197
MINRES()
Definition MINRES.h:217
MINRES(const EigenBase< MatrixDerived > &A)
Definition MINRES.h:230
Expression of an array as a mathematical vector or matrix.
Definition ArrayWrapper.h:120
@ Lower
Definition Constants.h:212
@ Upper
Definition Constants.h:214
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34