EllAlgo 1.6.13
Loading...
Searching...
No Matches
Functions
conjugate_gradient2.hpp File Reference

Alternative conjugate gradient method for solving linear systems. More...

#include <cmath>
#include <stdexcept>
#include <string>
Include dependency graph for conjugate_gradient2.hpp:

Go to the source code of this file.

Functions

template<typename Matrix , typename Vector >
Vector conjugate_gradient2 (const Matrix &A, const Vector &b, Vector &x_vector, double tol=1e-5, int max_iter=1000)
 

Detailed Description

Alternative conjugate gradient method for solving linear systems.

This header provides an alternative implementation of the conjugate gradient method using operator overloading instead of method calls.

Function Documentation

◆ conjugate_gradient2()

Vector conjugate_gradient2 ( const Matrix A,
const Vector b,
Vector x_vector,
double  tol = 1e-5,
int  max_iter = 1000 
)

Solves a system of linear equations \(Ax = b\) using the Conjugate Gradient method.

The update at each iteration \(k\):

\[ \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
MatrixThe type of the matrix A.
VectorThe type of the vectors b and x_vector.
Parameters
AThe matrix A in the system of linear equations Ax = b.
bThe right-hand side vector b in the system of linear equations Ax = b.
x_vectorThe initial guess for the solution vector. This vector will be updated in-place.
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.