numerics
Loading...
Searching...
No Matches
cg.hpp
Go to the documentation of this file.
1/// @file cg.hpp
2/// @brief Conjugate gradient solvers (dense and matrix-free)
3#pragma once
5#include "core/matrix.hpp"
6#include "core/vector.hpp"
7#include "core/policy.hpp"
8#include <functional>
9
10namespace num {
11
12/// @brief Callable type for matrix-free matvec: computes y = A*x
13using MatVecFn = std::function<void(const Vector&, Vector&)>;
14
15/// @brief Conjugate gradient solver for Ax = b
16/// @param A Symmetric positive definite matrix
17/// @param b Right-hand side vector
18/// @param x Solution vector (initial guess on input, solution on output)
19/// @param tol Convergence tolerance on residual norm (default 1e-10)
20/// @param max_iter Maximum iterations (default 1000)
21/// @param backend Backend for internal matvec/dot/axpy/scale (default: default_backend)
22/// @return SolverResult with convergence info
23SolverResult cg(const Matrix& A, const Vector& b, Vector& x,
24 real tol = 1e-10, idx max_iter = 1000,
25 Backend backend = default_backend);
26
27/// @brief Matrix-free conjugate gradient for Ax = b where A is SPD
28/// @param matvec Callable computing y = A*x
29/// @param b Right-hand side
30/// @param x Initial guess (modified in-place -> solution)
31/// @param tol Convergence tolerance on residual norm
32/// @param max_iter Maximum CG iterations
34 real tol = 1e-6, idx max_iter = 1000);
35
36} // namespace num
Dense row-major matrix with optional GPU storage.
Definition matrix.hpp:12
Matrix operations.
double real
Definition types.hpp:10
Backend
Selects which backend handles a linalg operation.
Definition policy.hpp:19
constexpr T ipow(T x) noexcept
Compute x^N at compile time via repeated squaring.
std::size_t idx
Definition types.hpp:11
void matvec(const Matrix &A, const Vector &x, Vector &y, Backend b=default_backend)
y = A * x
Definition matrix.cpp:94
constexpr real e
Definition math.hpp:41
constexpr Backend default_backend
Definition policy.hpp:57
SolverResult cg_matfree(MatVecFn matvec, const Vector &b, Vector &x, real tol=1e-6, idx max_iter=1000)
Matrix-free conjugate gradient for Ax = b where A is SPD.
Definition cg.cpp:66
SolverResult cg(const Matrix &A, const Vector &b, Vector &x, real tol=1e-10, idx max_iter=1000, Backend backend=default_backend)
Conjugate gradient solver for Ax = b.
Definition cg.cpp:8
std::function< void(const Vector &, Vector &)> MatVecFn
Callable type for matrix-free matvec: computes y = A*x.
Definition cg.hpp:13
Backend enum for linear algebra operations.
Common result type shared by all iterative solvers.
Vector operations.