17#include "./InternalHeaderCheck.h"
36template <
typename Vector,
typename RealScalar>
37typename Vector::Scalar omega(
const Vector& t,
const Vector& s, RealScalar angle) {
38 using Scalar =
typename Vector::Scalar;
39 const RealScalar ns = s.stableNorm();
40 const RealScalar nt = t.stableNorm();
41 const Scalar ts = t.dot(s);
42 const RealScalar rho = numext::abs(ts / (nt * ns));
45 if (ts == Scalar(0)) {
52 return angle * (ns / nt) * (ts / numext::abs(ts));
54 return ts / (nt * nt);
57template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
58bool idrs_unscaled(
const MatrixType& A,
const Rhs& b, Dest& x,
const Preconditioner& precond, Index& iter,
59 typename Dest::RealScalar& relres, Index S,
bool smoothing,
typename Dest::RealScalar angle,
61 using RealScalar =
typename Dest::RealScalar;
62 using Scalar =
typename Dest::Scalar;
63 using VectorType = Matrix<Scalar, Dynamic, 1>;
64 using DenseMatrixType = Matrix<Scalar, Dynamic, Dynamic, ColMajor>;
65 const Index N = b.size();
66 S = numext::mini(S, x.rows());
67 const RealScalar tol = relres;
68 const Index maxit = iter;
72 FullPivLU<DenseMatrixType> lu_solver;
74 DenseMatrixType P = internal::random_orthonormal_basis<DenseMatrixType>(N, S);
76 const RealScalar normb = b.stableNorm();
78 if (normb == RealScalar(0)) {
95 const RealScalar mp = RealScalar(1e3) * NumTraits<Scalar>::epsilon();
99 const RealScalar tolb =
100 internal::iterative_solver_threshold(tol, normb, internal::iterative_solver_residual_floor<RealScalar>());
101 VectorType r = b - A * x;
110 RealScalar normr = r.stableNorm();
115 relres = normr / normb;
119 DenseMatrixType G = DenseMatrixType::Zero(N, S);
120 DenseMatrixType U = DenseMatrixType::Zero(N, S);
121 DenseMatrixType M = DenseMatrixType::Identity(S, S);
122 VectorType t(N), v(N);
125 const auto restart = [&]() {
140 bool residualIsTrue =
false;
141 RealScalar referenceNorm = (std::numeric_limits<RealScalar>::max)();
142 Index referenceIter = 0;
143 const auto done = [&]() {
144 if (!(numext::isfinite)(normr)) {
153 r.noalias() = b - A * x;
154 normr = r.stableNorm();
155 if (normr <= tolb || iter >= maxit) {
156 residualIsTrue =
true;
159 if (normr <= RealScalar(0.5) * referenceNorm) {
160 referenceNorm = normr;
161 referenceIter = iter;
162 }
else if (iter - referenceIter >= 5) {
163 residualIsTrue =
true;
177 while (!done() && iter < maxit) {
179 VectorType f = (r.adjoint() * P).adjoint();
181 for (Index k = 0; k < S; ++k) {
184 lu_solver.compute(M.block(k, k, S - k, S - k));
185 VectorType c = lu_solver.solve(f.segment(k, S - k));
187 v.noalias() = r - G.rightCols(S - k) * c;
189 v = precond.solve(v);
192 U.col(k) = U.rightCols(S - k) * c + om * v;
193 G.col(k).noalias() = A * U.col(k);
196 const RealScalar normg = G.col(k).stableNorm();
197 for (Index i = 0; i < k; ++i) {
199 Scalar alpha = P.col(i).dot(G.col(k)) / M(i, i);
200 G.col(k) = G.col(k) - alpha * G.col(i);
201 U.col(k) = U.col(k) - alpha * U.col(i);
204 if (k > 0 && G.col(k).stableNorm() <= mp * normg) {
214 M.block(k, k, S - k, 1) = (G.col(k).adjoint() * P.rightCols(S - k)).adjoint();
216 if (M(k, k) == Scalar(0)) {
221 Scalar beta = f(k) / M(k, k);
222 r = r - beta * G.col(k);
223 x = x + beta * U.col(k);
224 normr = r.stableNorm();
226 if (replacement && normr > tolb / mp) {
234 Scalar gamma = t.dot(r_s) / t.squaredNorm();
235 r_s = r_s - gamma * t;
236 x_s = x_s - gamma * (x_s - x);
237 normr = r_s.stableNorm();
240 if (normr < tolb || iter == maxit) {
246 f.segment(k + 1, S - (k + 1)) = f.segment(k + 1, S - (k + 1)) - beta * M.block(k + 1, k, S - (k + 1), 1);
258 v = precond.solve(v);
264 om = internal::omega(t, r, angle);
266 if (om == RealScalar(0.0)) {
272 normr = r.stableNorm();
274 if (replacement && normr > tolb / mp) {
279 if (trueres && normr < normb) {
280 r.noalias() = b - A * x;
287 Scalar gamma = t.dot(r_s) / t.squaredNorm();
288 r_s = r_s - gamma * t;
289 x_s = x_s - gamma * (x_s - x);
290 normr = r_s.stableNorm();
300 if (!residualIsTrue && (numext::isfinite)(normr)) {
302 normr = (b - A * x).stableNorm();
304 relres = normr / normb;
308template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
309bool idrs(
const MatrixType& A,
const Rhs& b, Dest& x,
const Preconditioner& precond, Index& iter,
310 typename Dest::RealScalar& relres, Index S,
bool smoothing,
typename Dest::RealScalar angle,
313 return iterative_solver_on_scaled_problem(b, x, [&](
const auto& scaledRhs,
auto& scaledGuess) {
314 return idrs_unscaled(A, scaledRhs, scaledGuess, precond, iter, relres, S, smoothing, angle, replacement);
320template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar> >
325template <
typename MatrixType_,
typename Preconditioner_>
326struct traits<Eigen::IDRS<MatrixType_, Preconditioner_> > {
327 using MatrixType = MatrixType_;
328 using Preconditioner = Preconditioner_;
376template <
typename MatrixType_,
typename Preconditioner_>
379 using MatrixType = MatrixType_;
380 using Scalar =
typename MatrixType::Scalar;
381 using RealScalar =
typename MatrixType::RealScalar;
382 using Preconditioner = Preconditioner_;
388 using Base::m_isInitialized;
389 using Base::m_iterations;
392 bool m_smoothing =
false;
393 RealScalar m_angle = RealScalar(0.7);
394 bool m_residual =
false;
398 IDRS() { Base::m_tolerance = RealScalar(-1); }
410 template <
typename MatrixDerived>
412 Base::m_tolerance = RealScalar(-1);
428 RealScalar
tolerance()
const {
return Base::toleranceOrEps(); }
435 template <
typename Rhs,
typename Dest>
441 bool ret = internal::idrs(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error, m_S, m_smoothing, m_angle,
444 m_info = (!ret) ?
NumericalIssue : Base::infoAtDefaultOrSetTolerance(m_error, tol);
474 void setAngle(RealScalar angle) { m_angle = angle; }
The Induced Dimension Reduction method (IDR(s)) is a short-recurrences Krylov method for sparse squar...
Definition IDRS.h:377
void setResidualUpdate(bool update)
Definition IDRS.h:479
IDRS()
Definition IDRS.h:398
RealScalar tolerance() const
Definition IDRS.h:428
IDRS(const EigenBase< MatrixDerived > &A)
Definition IDRS.h:411
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition IDRS.h:436
void setSmoothing(bool smoothing)
Definition IDRS.h:462
void setS(Index S)
Definition IDRS.h:448
void setAngle(RealScalar angle)
Definition IDRS.h:474
IterativeSolverBase()
Definition IterativeSolverBase.h:194
Index maxIterations() const
Definition IterativeSolverBase.h:307
@ NumericalIssue
Definition Constants.h:459
Matrix< Type, Size, 1 > Vector
Size×1 vector of type Type.
Definition Matrix.h:532
Definition EigenBase.h:34