39#ifndef EIGEN_IDRSTABL_H
40#define EIGEN_IDRSTABL_H
43#include "./InternalHeaderCheck.h"
49template <
typename MatrixType,
typename Rhs,
typename Dest,
typename Preconditioner>
50bool idrstabl(
const MatrixType &mat,
const Rhs &rhs, Dest &x,
const Preconditioner &precond, Index &iters,
51 typename Dest::RealScalar &tol_error, Index L, Index S) {
55 using Scalar =
typename Dest::Scalar;
56 using RealScalar =
typename Dest::RealScalar;
57 using VectorType = Matrix<Scalar, Dynamic, 1>;
58 using DenseMatrixType = Matrix<Scalar, Dynamic, Dynamic, ColMajor>;
60 const Index N = x.rows();
63 const Index maxIters = iters;
65 const RealScalar rhs_norm = rhs.stableNorm();
66 const RealScalar tol = internal::iterative_solver_threshold(tol_error, rhs_norm,
67 internal::iterative_solver_residual_floor<RealScalar>());
80 FullPivLU<DenseMatrixType> lu_solver;
82 if (S >= N || L >= N) {
87 lu_solver.compute(DenseMatrixType(mat));
88 x = lu_solver.solve(rhs);
90 tol_error = (rhs - mat * x).stableNorm() / rhs_norm;
95 DenseMatrixType u(N, L + 1);
96 DenseMatrixType r(N, L + 1);
98 DenseMatrixType V(N * (L + 1), S);
102 VectorType update(N);
109 r.col(0).noalias() = rhs - mat * x;
112 tol_error = r.col(0).stableNorm();
115 DenseMatrixType h_FOM = DenseMatrixType::Zero(S, S - 1);
118 DenseMatrixType U(N * (L + 1), S);
119 for (Index col_index = 0; col_index < S; ++col_index) {
124 if (col_index != 0) {
128 VectorType w = mat * precond.solve(u.col(0));
129 for (Index i = 0; i < col_index; ++i) {
130 auto v = U.col(i).head(N);
131 h_FOM(i, col_index - 1) = v.dot(w);
132 w -= h_FOM(i, col_index - 1) * v;
135 h_FOM(col_index, col_index - 1) = u.col(0).stableNorm();
137 if (numext::abs(h_FOM(col_index, col_index - 1)) != RealScalar(0)) {
162 u.col(0) /= h_FOM(col_index, col_index - 1);
166 u.col(0).normalize();
169 U.col(col_index).head(N) = u.col(0);
174 Scalar beta = r.col(0).stableNorm();
175 VectorType e1 = VectorType::Zero(S - 1);
177 lu_solver.compute(h_FOM.topLeftCorner(S - 1, S - 1));
178 VectorType y = lu_solver.solve(e1);
179 VectorType x2 = x + U.topLeftCorner(N, S - 1) * y;
183 RealScalar FOM_residual = (h_FOM(S - 1, S - 2) * y(S - 2) * U.col(S - 1).head(N)).stableNorm();
185 if (FOM_residual < tol) {
189 x = precond.solve(x2);
192 tol_error = FOM_residual / rhs_norm;
209 DenseMatrixType R_T = internal::random_orthonormal_basis<DenseMatrixType>(N, S).adjoint();
210 DenseMatrixType AR_T = DenseMatrixType(R_T * mat);
213 DenseMatrixType sigma(S, S);
215 bool reset_while =
false;
217 while (k < maxIters) {
218 for (Index j = 1; j <= L; ++j) {
223 for (Index i = 0; i < S; ++i) {
224 sigma.col(i).noalias() = AR_T * precond.solve(U.block(N * (j - 1), i, N, 1));
227 lu_solver.compute(sigma);
231 alpha = lu_solver.solve(R_T * r.col(0));
234 alpha = lu_solver.solve(AR_T * precond.solve(r.col(j - 2)));
238 update.noalias() = U.topRows(N) * alpha;
239 r.col(0).noalias() -= mat * precond.solve(update);
242 for (Index i = 1; i <= j - 2; ++i) {
244 r.col(i).noalias() -= U.block(N * (i + 1), 0, N, S) * alpha;
248 r.col(j - 1).noalias() = mat * precond.solve(r.col(j - 2));
250 tol_error = r.col(0).stableNorm();
252 if (tol_error < tol) {
258 bool break_normalization =
false;
259 for (Index q = 1; q <= S; ++q) {
262 u.leftCols(j) = r.leftCols(j);
265 u.leftCols(j) = u.middleCols(1, j);
270 u.leftCols(j).reshaped() -= U.topRows(N * j) * lu_solver.solve(AR_T * precond.solve(u.col(j - 1)));
273 u.col(j).noalias() = mat * precond.solve(u.col(j - 1));
284 for (Index i = 0; i <= q - 2; ++i) {
285 auto v = V.col(i).segment(N * j, N);
286 Scalar h = v.squaredNorm();
287 h = v.dot(u.col(j)) / h;
288 u.leftCols(j + 1).reshaped() -= h * V.col(i).head(N * (j + 1));
292 Scalar normalization_constant = u.col(j).stableNorm();
295 if (normalization_constant == RealScalar(0.0)) {
296 break_normalization =
true;
299 u.leftCols(j + 1) /= normalization_constant;
302 V.col(q - 1).head(N * (j + 1)) = u.leftCols(j + 1).reshaped();
305 if (!break_normalization) {
306 U.topRows(N * (j + 1)) = V.topRows(N * (j + 1));
310 U.middleRows(N * j, N).setZero();
318 r.col(L).noalias() = mat * precond.solve(r.col(L - 1));
323 ColPivHouseholderQR<DenseMatrixType> qr_solver(r.rightCols(L));
324 gamma = qr_solver.solve(r.col(0));
327 update.noalias() = r.leftCols(L) * gamma;
329 r.col(0).noalias() -= mat * precond.solve(update);
333 tol_error = r.col(0).stableNorm();
335 if (tol_error < tol) {
348 for (Index i = 1; i <= L; ++i) {
349 U.topRows(N) -= U.block(N * i, 0, N, S) * gamma(i - 1);
358 x = precond.solve(x);
361 tol_error = tol_error / rhs_norm;
367template <
typename MatrixType_,
typename Preconditioner_ = DiagonalPreconditioner<
typename MatrixType_::Scalar>>
372template <
typename MatrixType_,
typename Preconditioner_>
373struct traits<IDRSTABL<MatrixType_, Preconditioner_>> {
374 using MatrixType = MatrixType_;
375 using Preconditioner = Preconditioner_;
421template <
typename MatrixType_,
typename Preconditioner_>
427 using Base::m_isInitialized;
428 using Base::m_iterations;
434 using MatrixType = MatrixType_;
435 using Scalar =
typename MatrixType::Scalar;
436 using RealScalar =
typename MatrixType::RealScalar;
437 using Preconditioner = Preconditioner_;
453 template <
typename MatrixDerived>
462 template <
typename Rhs,
typename Dest>
465 m_error = Base::m_tolerance;
466 bool ret = internal::idrstabl(matrix(), b, x, Base::m_preconditioner, m_iterations, m_error, m_L, m_S);
474 eigen_assert(L >= 1 &&
"L needs to be positive");
480 eigen_assert(S >= 1 &&
"S needs to be positive");
The IDR(s)STAB(l) is a combination of IDR(s) and BiCGSTAB(l). It is a short-recurrences Krylov method...
Definition IDRSTABL.h:422
void setL(Index L)
Definition IDRSTABL.h:473
void setS(Index S)
Definition IDRSTABL.h:479
IDRSTABL(const EigenBase< MatrixDerived > &A)
Definition IDRSTABL.h:454
void _solve_vector_with_guess_impl(const Rhs &b, Dest &x) const
Definition IDRSTABL.h:463
IterativeSolverBase()
Definition IterativeSolverBase.h:194
Index maxIterations() const
Definition IterativeSolverBase.h:307
@ NumericalIssue
Definition Constants.h:459
@ Success
Definition Constants.h:457
@ NoConvergence
Definition Constants.h:461
Definition EigenBase.h:34