|
1 | | -# Conjugate-Gradient-stepik |
| 1 | +# stepik: https://stepik.org/a/260000 |
| 2 | + |
| 3 | + |
| 4 | +# Conjugate Gradient & Sparse CG Solver Course |
| 5 | + |
| 6 | +> 🚀 Professional implementation and mathematical explanation of **Conjugate Gradient (CG)** and **Sparse Conjugate Gradient** methods for large-scale linear systems. |
| 7 | +
|
| 8 | +--- |
| 9 | + |
| 10 | +## 🔥 Project Overview |
| 11 | + |
| 12 | +This repository provides a complete course-style implementation of: |
| 13 | + |
| 14 | +- Conjugate Gradient (CG) algorithm |
| 15 | +- Preconditioned Conjugate Gradient |
| 16 | +- Sparse Conjugate Gradient |
| 17 | +- Large-scale linear system solving |
| 18 | +- Numerical stability analysis |
| 19 | +- Optimization perspective of CG |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## Keywords |
| 24 | + |
| 25 | +``` |
| 26 | +
|
| 27 | +conjugate gradient |
| 28 | +conjugate gradient method |
| 29 | +sparse conjugate gradient |
| 30 | +pcg solver |
| 31 | +cg solver python |
| 32 | +large scale linear systems |
| 33 | +numerical linear algebra |
| 34 | +iterative solver |
| 35 | +preconditioned conjugate gradient |
| 36 | +optimization solver |
| 37 | +python cg implementation |
| 38 | +
|
| 39 | +``` |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 📚 Mathematical Background |
| 44 | + |
| 45 | +### Linear System Problem |
| 46 | + |
| 47 | +We solve: |
| 48 | + |
| 49 | +$$ |
| 50 | +Ax = b |
| 51 | +$$ |
| 52 | + |
| 53 | +Where: |
| 54 | + |
| 55 | +- $$A$$ — symmetric positive definite matrix |
| 56 | +- $$x$$ — unknown vector |
| 57 | +- $$b$$ — right-hand side |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 🔵 Conjugate Gradient Method |
| 62 | + |
| 63 | +CG minimizes quadratic function: |
| 64 | + |
| 65 | +$$ |
| 66 | +f(x) = \frac{1}{2}x^T A x - b^T x |
| 67 | +$$ |
| 68 | + |
| 69 | +Update rule: |
| 70 | + |
| 71 | +$$ |
| 72 | +x_{k+1} = x_k + \alpha_k p_k |
| 73 | +$$ |
| 74 | + |
| 75 | +Where: |
| 76 | + |
| 77 | +- $$p_k$$ — conjugate direction |
| 78 | +- $$\alpha_k$$ — optimal step size |
| 79 | + |
| 80 | +Step size: |
| 81 | + |
| 82 | +$$ |
| 83 | +\alpha_k = |
| 84 | +\frac{r_k^T r_k} |
| 85 | +{p_k^T A p_k} |
| 86 | +$$ |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +### Residual Update |
| 91 | + |
| 92 | +$$ |
| 93 | +r_k = b - Ax_k |
| 94 | +$$ |
| 95 | + |
| 96 | +Direction update: |
| 97 | + |
| 98 | +$$ |
| 99 | +p_{k+1} = r_{k+1} + \beta_k p_k |
| 100 | +$$ |
| 101 | + |
| 102 | +Where: |
| 103 | + |
| 104 | +$$ |
| 105 | +\beta_k = |
| 106 | +\frac{r_{k+1}^T r_{k+1}} |
| 107 | +{r_k^T r_k} |
| 108 | +$$ |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +## ⚡ Sparse Conjugate Gradient |
| 113 | + |
| 114 | +For sparse matrices: |
| 115 | + |
| 116 | +- Store matrix in CSR/CSC format |
| 117 | +- Avoid dense multiplication |
| 118 | +- Reduce memory complexity |
| 119 | + |
| 120 | +Advantages: |
| 121 | + |
| 122 | +✅ Memory efficient |
| 123 | +✅ Faster computation |
| 124 | +✅ Scalable to large systems |
| 125 | + |
| 126 | +--- |
| 127 | + |
| 128 | +## 🧠 Why This Project Is Important |
| 129 | + |
| 130 | +CG is used in: |
| 131 | + |
| 132 | +- Finite element methods |
| 133 | +- Physics simulations |
| 134 | +- Machine learning |
| 135 | +- PDE solvers |
| 136 | +- Large sparse systems |
| 137 | +- Scientific computing |
| 138 | + |
| 139 | +It is one of the most important iterative solvers. |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## 🏗 Project Structure |
| 144 | + |
| 145 | +``` |
| 146 | +
|
| 147 | +conjugate-gradient-sparse-cg-solver-course/ |
| 148 | +│ |
| 149 | +├── README.md |
| 150 | +├── LICENSE |
| 151 | +├── CITATION.cff |
| 152 | +├── requirements.txt |
| 153 | +│ |
| 154 | +├── src/ |
| 155 | +│ ├── cg_solver.py |
| 156 | +│ ├── sparse_cg.py |
| 157 | +│ ├── preconditioner.py |
| 158 | +│ |
| 159 | +├── examples/ |
| 160 | +│ └── demo.py |
| 161 | +│ |
| 162 | +├── docs/ |
| 163 | +│ ├── theory.md |
| 164 | +│ ├── convergence.md |
| 165 | +│ |
| 166 | +├── images/ |
| 167 | +│ └── convergence_plot.png |
| 168 | +│ |
| 169 | +└── index.html |
| 170 | +
|
| 171 | +```` |
| 172 | +
|
| 173 | +Clean structure improves: |
| 174 | +
|
| 175 | +✔ Search ranking |
| 176 | +✔ Professional appearance |
| 177 | +✔ Research credibility |
| 178 | +
|
| 179 | +--- |
| 180 | +
|
| 181 | +## 🐍 Example — Basic Conjugate Gradient Implementation |
| 182 | +
|
| 183 | +```python |
| 184 | +import numpy as np |
| 185 | +
|
| 186 | +def conjugate_gradient(A, b, x0=None, tol=1e-8, max_iter=1000): |
| 187 | + n = len(b) |
| 188 | + x = np.zeros(n) if x0 is None else x0 |
| 189 | +
|
| 190 | + r = b - A @ x |
| 191 | + p = r.copy() |
| 192 | +
|
| 193 | + for _ in range(max_iter): |
| 194 | + Ap = A @ p |
| 195 | + alpha = (r @ r) / (p @ Ap) |
| 196 | + x = x + alpha * p |
| 197 | +
|
| 198 | + r_new = r - alpha * Ap |
| 199 | +
|
| 200 | + if np.linalg.norm(r_new) < tol: |
| 201 | + break |
| 202 | +
|
| 203 | + beta = (r_new @ r_new) / (r @ r) |
| 204 | + p = r_new + beta * p |
| 205 | + r = r_new |
| 206 | +
|
| 207 | + return x |
| 208 | +```` |
| 209 | +
|
| 210 | +--- |
| 211 | +
|
| 212 | +## 🚀 Installation |
| 213 | +
|
| 214 | +```bash id="install-cg" |
| 215 | +pip install -r requirements.txt |
| 216 | +``` |
| 217 | + |
| 218 | +Run example: |
| 219 | + |
| 220 | +```bash id="run-cg" |
| 221 | +python examples/demo.py |
| 222 | +``` |
| 223 | + |
| 224 | +--- |
| 225 | + |
| 226 | +## 📊 Visualization (Highly Recommended) |
| 227 | + |
| 228 | +Add: |
| 229 | + |
| 230 | +* Residual norm vs iteration |
| 231 | +* Convergence curve |
| 232 | +* Sparse matrix structure plot |
| 233 | + |
| 234 | +Example: |
| 235 | + |
| 236 | +```python id="plot-cg" |
| 237 | +import matplotlib.pyplot as plt |
| 238 | + |
| 239 | +plt.plot(residual_history) |
| 240 | +plt.xlabel("Iteration") |
| 241 | +plt.ylabel("Residual Norm") |
| 242 | +plt.title("CG Convergence") |
| 243 | +plt.show() |
| 244 | +``` |
| 245 | + |
| 246 | + |
| 247 | + |
0 commit comments