Short answer: Conjugate gradient solves Ax = b when A is symmetric positive definite. It builds A-orthogonal search directions so that each new direction does not undo the minimization along earlier ones. In exact arithmetic it reaches the solution in at most n steps for an n by n system, and it often reaches a useful residual much sooner when the eigenvalues are clustered. Each iteration is one matrix-vector product, so a sparse A stays sparse. The memory is a handful of vectors.
What the problem actually asks
Gaussian elimination factors a sparse matrix and fills in new nonzeros. Gradient descent on the quadratic (1/2)x·Ax − b·x uses only matrix-vector products and takes small steps in a narrow valley, because successive gradients are not conjugate. Conjugate gradient chooses the next direction as the residual plus a multiple of the previous direction, with the multiple chosen so the directions are A-orthogonal. The line search along that direction is then a closed-form step length.
Positive definiteness makes the quadratic bowl have a unique minimum at the solution of Ax = b. A symmetric indefinite matrix breaks the line-search guarantees. Those systems want MINRES or a factorization.
What the algorithm guarantees
In exact arithmetic, the iterate after at most n steps is the solution, and the residual is orthogonal to the Krylov subspace generated so far. The error measured in the A-norm decreases. Time is O(n) iterations times the cost of a matrix-vector product in the worst case, and far fewer iterations when the condition number is moderate or a preconditioner clusters the eigenvalues. Extra memory is a few n-vectors. Floating-point loss of conjugacy is real on ill-conditioned systems. Restarting or, better, preconditioning is the practical response.
The algorithm does not form A inverse. It does not need A stored as a dense matrix. A preconditioner M approximates A and is applied by solving M z = r each iteration. The preconditioner has to be symmetric positive definite too if the standard convergence story is kept.
The working steps
- Set x to an initial guess, r = b − Ax, and the first direction d = r.
- The step length is (r·r) / (d·A·d). Update x by adding that length times d, and update r by subtracting that length times A·d.
- The next direction is the new residual plus β times d, where β = (new r · new r) / (old r · old r) in the Fletcher-Reeves form used by the classical algorithm.
- Stop when the residual norm is small relative to b, or after a budget. In exact arithmetic the residual becomes zero by step n.
- If A is ill-conditioned, apply a preconditioner rather than expecting the n-step bound to be a fast floating-point run.
The n-step exact termination needs exact arithmetic and a symmetric positive definite matrix. It is not a general optimizer of a nonquadratic loss. Nonlinear conjugate gradient borrows the direction update and loses the finite termination. A dense well-conditioned system may still be faster to factor. Conjugate gradient wins when the matrix-vector product is cheap and a modest number of iterations suffices.
Approaches that look simpler and fail
| Approach | Why it fails when the matrix is only symmetric |
|---|---|
| Run the algorithm on a symmetric indefinite system | The step-length denominator d·A·d can be zero or negative. The quadratic is not a bowl, and the iteration is not the right Krylov method. |
| Replace the matrix-vector product by an explicit inverse | The method exists to avoid the inverse. Forming it is a dense factorization. |
| Expect n floating-point iterations to produce a zero residual on a badly conditioned matrix | Conjugacy erodes. The residual may stall above the exact-arithmetic zero. Precondition or iterate with a residual check, not a promise of step n. |
| Use the linear update formulas on a general neural-net loss and expect termination in n steps | Those formulas used the quadratic model. Nonlinear conjugate gradient is a heuristic cousin. |
A dense factorization is Gaussian elimination. A general smooth minimization is gradient descent or a Newton method. Conjugate gradient is the Krylov solver for symmetric positive definite linear systems.
A working example
This Python fragment takes Fletcher–Reeves steps on a symmetric positive-definite matrix. For A = [[4, 1], [1, 3]] and b = [1, 2] the solution is x = 1/11, y = 7/11. It prints 0.0909 0.6364.
A = [[4.0, 1.0], [1.0, 3.0]]
b = [1.0, 2.0]
def matvec(v):
return [A[i][0] * v[0] + A[i][1] * v[1] for i in range(2)]
x = [0.0, 0.0]
r = b[:]
d = r[:]
rr = r[0] * r[0] + r[1] * r[1]
for _ in range(2):
Ad = matvec(d)
alpha = rr / (d[0] * Ad[0] + d[1] * Ad[1])
x = [x[i] + alpha * d[i] for i in range(2)]
r = [r[i] - alpha * Ad[i] for i in range(2)]
rr_new = r[0] * r[0] + r[1] * r[1]
if rr_new < 1e-18:
break
beta = rr_new / rr
d = [r[i] + beta * d[i] for i in range(2)]
rr = rr_new
Ax = matvec(x)
assert abs(Ax[0] - b[0]) < 1e-8 and abs(Ax[1] - b[1]) < 1e-8
print(f"{x[0]:.4f}", f"{x[1]:.4f}")
Where it is used
PETSc, MATLAB pcg, and finite-element codes solve a large symmetric positive-definite system with conjugate gradient. Each step is a matrix-vector product, so a sparse stiffness matrix stays sparse. Gaussian elimination is the dense, direct alternative.
When you can drop this algorithm
Drop conjugate gradient when A is not symmetric positive definite. Use a solver that matches the matrix. Keep it, with a preconditioner, when A is a large sparse SPD system and matrix-vector products are the natural operation.
Why this stays published
We publish this so a search for conjugate gradient returns the A-orthogonal directions and the requirement that A be symmetric positive definite. Conjugate gradient solves a symmetric positive definite linear system with one matrix-vector product per iteration, reaching the exact solution in at most n steps in exact arithmetic.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
