13 if (x.
size() != n ||
b.size() != n)
14 throw std::invalid_argument(
"Dimension mismatch in GMRES");
19 std::vector<Vector> V;
21 std::vector<std::vector<real>> H(
restart, std::vector<real>(
restart + 1, 0.0));
24 std::vector<real>
g(
restart + 1, 0.0);
44 for (
auto& col : H) std::fill(col.begin(), col.end(), 0.0);
45 std::fill(
cs.begin(),
cs.end(), 0.0);
46 std::fill(
sn.begin(),
sn.end(), 0.0);
47 std::fill(
g.begin(),
g.end(), 0.0);
59 for (
idx k = 0;
k < n; ++
k) h +=
w[
k] * V[
i][
k];
61 for (
idx k = 0;
k < n; ++
k)
w[
k] -= h * V[
i][
k];
90 result.residual = std::abs(
g[
j + 1]);
95 std::vector<real> y(
m, 0.0);
104 for (
idx k = 0;
k < n; ++
k) x[
k] += y[
i] * V[
i][
k];
106 if (
result.converged)
break;
115 if (
A.n_rows() !=
A.n_cols())
116 throw std::invalid_argument(
"GMRES requires a square matrix");
128 if (
A.rows() !=
A.cols())
129 throw std::invalid_argument(
"GMRES requires a square matrix");
constexpr idx size() const noexcept
Dense row-major matrix with optional GPU storage.
Sparse matrix in Compressed Sparse Row (CSR) format.
Restarted GMRES – a Krylov subspace solver for general Ax = b.
Backend
Selects which backend handles a linalg operation.
real beta(real a, real b)
B(a, b) – beta function.
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
void sparse_matvec(const SparseMatrix &A, const Vector &x, Vector &y)
y = A * x
SolverResult gmres(MatVecFn matvec, idx n, const Vector &b, Vector &x, real tol=1e-6, idx max_iter=1000, idx restart=30)
Restarted GMRES(restart) – matrix-free interface.
BasicVector< real > Vector
Real-valued dense vector with full backend dispatch (CPU + GPU)
std::function< void(const Vector &, Vector &)> MatVecFn
Callable type for matrix-free matvec: computes y = A*x.