ellalgo/conjugate_gradient.hpp file

Conjugate gradient method for solving linear systems.

This header provides classes and functions for solving linear systems using the conjugate gradient method. It includes a simple Vector and Matrix class for demonstration purposes.

Classes

class Vector0
A simple vector class for conjugate gradient calculations.
class Matrix0
A simple matrix class for conjugate gradient calculations.

Functions

auto operator+(Vector0 lhs, const Vector0& rhs) -> Vector0
Add two vectors.
auto operator-(Vector0 lhs, const Vector0& rhs) -> Vector0
Subtract two vectors.
auto operator*(Vector0 v, double scalar) -> Vector0
Multiply a vector by a scalar.
auto operator*(double scalar, Vector0 v) -> Vector0
Multiply a scalar by a vector.
auto conjugate_gradient(const Matrix0& A, const Vector0& b, const Vector0* x0 = nullptr, double tol = 1e-5, int max_iter = 1000) -> Vector0

Function documentation

Vector0 operator+(Vector0 lhs, const Vector0& rhs)

Add two vectors.

Parameters
lhs in Left-hand side vector
rhs in Right-hand side vector
Returns The resulting vector

Vector0 operator-(Vector0 lhs, const Vector0& rhs)

Subtract two vectors.

Parameters
lhs in Left-hand side vector
rhs in Right-hand side vector
Returns The resulting vector

Vector0 operator*(Vector0 v, double scalar)

Multiply a vector by a scalar.

Parameters
in The vector to multiply
scalar in The scalar value
Returns The resulting vector

Vector0 operator*(double scalar, Vector0 v)

Multiply a scalar by a vector.

Parameters
scalar in The scalar value
in The vector to multiply
Returns The resulting vector

Vector0 conjugate_gradient(const Matrix0& A, const Vector0& b, const Vector0* x0 = nullptr, double tol = 1e-5, int max_iter = 1000)

Parameters
A The matrix A in the linear system Ax = b.
b The right-hand side vector b in the linear system Ax = b.
x0 An optional initial guess for the solution vector.
tol The tolerance for the residual norm, used as the stopping criterion.
max_iter The maximum number of iterations to perform.
Returns The solution vector.
Exceptions
std::runtime_error if the conjugate gradient method does not converge after the maximum number of iterations.

Solves the linear system Ax = b using the conjugate gradient method.