Skip to content

SENATOROVAI/conjugate-gradient-sparse-cg-solver-course

Conjugate Gradient & Sparse CG Solver Course

License Python Website PRs Welcome DOI Code Style Pre-commit

πŸš€ Professional implementation and mathematical explanation of Conjugate Gradient (CG) and Sparse Conjugate Gradient methods for large-scale linear systems.


πŸ”₯ Project Overview

This repository provides a complete course-style implementation of:

  • Conjugate Gradient (CG) algorithm
  • Preconditioned Conjugate Gradient
  • Sparse Conjugate Gradient
  • Large-scale linear system solving
  • Numerical stability analysis
  • Optimization perspective of CG

Keywords


conjugate gradient
conjugate gradient method
sparse conjugate gradient
pcg solver
cg solver python
large scale linear systems
numerical linear algebra
iterative solver
preconditioned conjugate gradient
optimization solver
python cg implementation


πŸ“š Mathematical Background

Linear System Problem

We solve:

$$ Ax = b $$

Where:

  • $$A$$ β€” symmetric positive definite matrix
  • $$x$$ β€” unknown vector
  • $$b$$ β€” right-hand side

πŸ”΅ Conjugate Gradient Method

CG minimizes quadratic function:

$$ f(x) = \frac{1}{2}x^T A x - b^T x $$

Update rule:

$$ x_{k+1} = x_k + \alpha_k p_k $$

Where:

  • $$p_k$$ β€” conjugate direction
  • $$\alpha_k$$ β€” optimal step size

Step size:

$$ \alpha_k = \frac{r_k^T r_k} {p_k^T A p_k} $$


Residual Update

$$ r_k = b - Ax_k $$

Direction update:

$$ p_{k+1} = r_{k+1} + \beta_k p_k $$

Where:

$$ \beta_k = \frac{r_{k+1}^T r_{k+1}} {r_k^T r_k} $$


⚑ Sparse Conjugate Gradient

For sparse matrices:

  • Store matrix in CSR/CSC format
  • Avoid dense multiplication
  • Reduce memory complexity

Advantages:

βœ… Memory efficient
βœ… Faster computation
βœ… Scalable to large systems


🧠 Why This Project Is Important

CG is used in:

  • Finite element methods
  • Physics simulations
  • Machine learning
  • PDE solvers
  • Large sparse systems
  • Scientific computing

It is one of the most important iterative solvers.


πŸ— Project Structure


conjugate-gradient-sparse-cg-solver-course/
β”‚
β”œβ”€β”€ README.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ CITATION.cff
β”œβ”€β”€ requirements.txt
β”‚
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ cg_solver.py
β”‚   β”œβ”€β”€ sparse_cg.py
β”‚   β”œβ”€β”€ preconditioner.py
β”‚
β”œβ”€β”€ examples/
β”‚   └── demo.py
β”‚
β”œβ”€β”€ docs/
β”‚   β”œβ”€β”€ theory.md
β”‚   β”œβ”€β”€ convergence.md
β”‚
β”œβ”€β”€ images/
β”‚   └── convergence_plot.png
β”‚
└── index.html

Clean structure improves:

βœ” Search ranking
βœ” Professional appearance
βœ” Research credibility


🐍 Example β€” Basic Conjugate Gradient Implementation

import numpy as np

def conjugate_gradient(A, b, x0=None, tol=1e-8, max_iter=1000):
    n = len(b)
    x = np.zeros(n) if x0 is None else x0

    r = b - A @ x
    p = r.copy()

    for _ in range(max_iter):
        Ap = A @ p
        alpha = (r @ r) / (p @ Ap)
        x = x + alpha * p

        r_new = r - alpha * Ap

        if np.linalg.norm(r_new) < tol:
            break

        beta = (r_new @ r_new) / (r @ r)
        p = r_new + beta * p
        r = r_new

    return x

πŸš€ Installation

pip install -r requirements.txt

Run example:

python examples/demo.py

πŸ“Š Visualization (Highly Recommended)

Add:

  • Residual norm vs iteration
  • Convergence curve
  • Sparse matrix structure plot

Example:

import matplotlib.pyplot as plt

plt.plot(residual_history)
plt.xlabel("Iteration")
plt.ylabel("Residual Norm")
plt.title("CG Convergence")
plt.show()

About

The Conjugate Gradient (CG) method is an efficient iterative algorithm for solving large, sparse systems of linear equations where the matrix is symmetric and positive-definite. It finds the minimum of a quadratic function by generating conjugate search directions, ensuring convergence in at most steps for an matrix.Solver

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors