EllAlgo 1.6.13
Loading...
Searching...
No Matches
Classes | Functions
conjugate_gradient.hpp File Reference

Conjugate gradient method for solving linear systems. More...

#include <cmath>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
Include dependency graph for conjugate_gradient.hpp:

Go to the source code of this file.

Classes

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

Functions

Vector0 operator+ (Vector0 lhs, const Vector0 &rhs)
 Element-wise vector addition.
 
Vector0 operator- (Vector0 lhs, const Vector0 &rhs)
 Element-wise vector subtraction.
 
Vector0 operator* (Vector0 v, double scalar)
 Scalar-vector multiplication (vector * scalar)
 
Vector0 operator* (double scalar, Vector0 v)
 Scalar-vector multiplication (scalar * vector)
 
template<typename Matrix0 , typename Vector0 >
Vector0 conjugate_gradient (const Matrix0 &A, const Vector0 &b, const Vector0 *x0=nullptr, double tol=1e-5, int max_iter=1000)
 Solve Ax = b using the conjugate gradient method.
 

Detailed Description

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.

Function Documentation

◆ conjugate_gradient()

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

Solve Ax = b using the conjugate gradient method.

The Conjugate Gradient method iteratively solves \(Ax = b\) where \(A\) is symmetric positive-definite.

The update at each iteration \(k\) is:

\[ \alpha_k = \frac{r_k^T r_k}{d_k^T A d_k}, \qquad x_{k+1} = x_k + \alpha_k d_k \]

\[ r_{k+1} = r_k - \alpha_k A d_k, \qquad \beta_k = \frac{r_{k+1}^T r_{k+1}}{r_k^T r_k}, \qquad d_{k+1} = r_{k+1} + \beta_k d_k \]

Template Parameters
Matrix0The matrix type, which must support matrix-vector multiplication.
Vector0The vector type, which must support vector operations.
Parameters
AThe matrix A in the linear system Ax = b.
bThe right-hand side vector b in the linear system Ax = b.
x0An optional initial guess for the solution vector.
tolThe tolerance for the residual norm, used as the stopping criterion.
max_iterThe maximum number of iterations to perform.
Returns
The solution vector.
Exceptions
std::runtime_errorif the conjugate gradient method does not converge after the maximum number of iterations.

◆ operator*() [1/2]

Vector0 operator* ( double  scalar,
Vector0  v 
)
inline

Scalar-vector multiplication (scalar * vector)

Parameters
[in]scalarThe scalar value
[in]vThe vector to multiply
Returns
The resulting vector

◆ operator*() [2/2]

Vector0 operator* ( Vector0  v,
double  scalar 
)
inline

Scalar-vector multiplication (vector * scalar)

Parameters
[in]vThe vector to multiply
[in]scalarThe scalar value
Returns
The resulting vector

◆ operator+()

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

Element-wise vector addition.

Parameters
[in]lhsLeft-hand side vector
[in]rhsRight-hand side vector
Returns
The resulting vector

◆ operator-()

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

Element-wise vector subtraction.

Parameters
[in]lhsLeft-hand side vector
[in]rhsRight-hand side vector
Returns
The resulting vector