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());
51 const RealScalar threshold(tol_error * rhsNorm);
55 VectorType v(VectorType::Zero(N));
56 VectorType v_new(rhs - mat * x);
57 RealScalar residualNorm(v_new.stableNorm());
58 if (residualNorm == 0 || residualNorm < threshold) {
60 tol_error = residualNorm / rhsNorm;
65 const RealScalar residualScale = internal::iterative_solver_scaling_factor(residualNorm);
66 v_new /= residualScale;
68 VectorType w_new(precond.solve(v_new));
69 RealScalar beta_new2(v_new.dot(w_new));
70 eigen_assert(beta_new2 >= 0.0 &&
"PRECONDITIONER IS NOT POSITIVE DEFINITE");
71 RealScalar beta_new(numext::sqrt(beta_new2));
72 const RealScalar beta_one(beta_new);
75 RealScalar c_old(1.0);
77 RealScalar s_old(0.0);
79 VectorType p_old(VectorType::Zero(N));
84 while (iters < maxIters) {
95 const RealScalar beta(beta_new);
101 v_new.noalias() = mat * w - beta * v_old;
102 const RealScalar alpha = v_new.dot(w);
104 w_new = precond.solve(v_new);
105 beta_new2 = v_new.dot(w_new);
106 eigen_assert(beta_new2 >= 0.0 &&
"PRECONDITIONER IS NOT POSITIVE DEFINITE");
107 beta_new = numext::sqrt(beta_new2);
110 const RealScalar r2 = s * alpha + c * c_old * beta;
111 const RealScalar r3 = s_old * beta;
112 const RealScalar r1_hat = c * alpha - c_old * s * beta;
113 const RealScalar r1 = numext::hypot(r1_hat, beta_new);
122 p = (w - r2 * p_old - r3 * p_oold) / r1;
123 x += (residualScale * beta_one * c * eta) * p;
127 residualNorm *= numext::abs(s);
129 if (residualNorm < threshold) {
139 tol_error = residualNorm / rhsNorm;
144template <
typename MatrixType_,
int UpLo_ = Lower,
typename Preconditioner_ = IdentityPreconditioner>
149template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
150struct traits<MINRES<MatrixType_, UpLo_, Preconditioner_> > {
151 using MatrixType = MatrixType_;
152 using Preconditioner = Preconditioner_;
195template <
typename MatrixType_,
int UpLo_,
typename Preconditioner_>
201 using Base::m_isInitialized;
202 using Base::m_iterations;
206 using Base::_solve_impl;
207 using MatrixType = MatrixType_;
208 using Scalar =
typename MatrixType::Scalar;
209 using RealScalar =
typename MatrixType::RealScalar;
210 using Preconditioner = Preconditioner_;
212 enum { UpLo = UpLo_ };
228 template <
typename MatrixDerived>
232 template <
typename Rhs,
typename Dest>
233 void _solve_vector_with_guess_impl(
const Rhs& b, Dest& x)
const {
235 using ActualMatrixType =
typename Base::ActualMatrixType;
237 TransposeInput = (!MatrixWrapper::MatrixFree) && (UpLo == (
Lower |
Upper)) && (!MatrixType::IsRowMajor) &&
238 (!NumTraits<Scalar>::IsComplex)
240 using RowMajorWrapper =
241 std::conditional_t<TransposeInput, Transpose<const ActualMatrixType>, ActualMatrixType
const&>;
242 EIGEN_STATIC_ASSERT(internal::check_implication(MatrixWrapper::MatrixFree, UpLo == (
Lower |
Upper)),
243 MATRIX_FREE_CONJUGATE_GRADIENT_IS_COMPATIBLE_WITH_UPPER_UNION_LOWER_MODE_ONLY);
244 using SelfAdjointWrapper =
245 std::conditional_t<UpLo == (
Lower |
Upper), RowMajorWrapper,
246 typename MatrixWrapper::template ConstSelfAdjointViewReturnType<UpLo>::Type>;
249 m_error = Base::m_tolerance;
250 RowMajorWrapper row_mat(matrix());
251 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:246
A minimal residual solver for sparse symmetric problems.
Definition MINRES.h:196
MINRES()
Definition MINRES.h:216
MINRES(const EigenBase< MatrixDerived > &A)
Definition MINRES.h:229
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