Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Theorems 4:2 and 5:2 — the cg-method reaches the solution in at most n steps

Proved
ConjGrad.Termination.cg_terminates_in_n_steps

by mikedeng1 · Sep 26, 2026 · Mathlib 0df444a (Lean v4.33.1)

conjugate-gradientfinite-terminationnumerical-linear-algebrap2o-batch-p100ap2o-gran-per-chapterp2o-plan-paperp2o-v1

Let AAA be a real symmetric positive definite n×nn \times nn×n matrix, let k∈Rnk \in \mathbb{R}^nk∈Rn, and let hhh be the solution of Ah=kAh = kAh=k. Let x0∈Rnx_0 \in \mathbb{R}^nx0​∈Rn be an arbitrary initial estimate, and let x0,x1,x2,…x_0, x_1, x_2, \dotsx0​,x1​,x2​,… be the estimates produced by the conjugate gradient method (3:1):

p0=r0=k−Ax0,ai=∣ri∣2(pi,Api),xi+1=xi+aipi,ri+1=ri−aiApi,bi=∣ri+1∣2∣ri∣2,pi+1=ri+1+bipi.p_0 = r_0 = k - Ax_0,\quad a_i = \frac{|r_i|^2}{(p_i, Ap_i)},\quad x_{i+1} = x_i + a_i p_i,\quad r_{i+1} = r_i - a_i A p_i,\quad b_i = \frac{|r_{i+1}|^2}{|r_i|^2},\quad p_{i+1} = r_{i+1} + b_i p_i.p0​=r0​=k−Ax0​,ai​=(pi​,Api​)∣ri​∣2​,xi+1​=xi​+ai​pi​,ri+1​=ri​−ai​Api​,bi​=∣ri​∣2∣ri+1​∣2​,pi+1​=ri+1​+bi​pi​.

Then there is an mmm with

m≤nandxm=h.m \le n \qquad\text{and}\qquad x_m = h.m≤nandxm​=h.

This is the finite-termination property announced in the paper's abstract ("The solution is given in nnn steps") and in Section 3: "If no rounding-off error is encountered, one will reach an estimate xmx_mxm​ (m≤nm \le nm≤n) at which rm=0r_m = 0rm​=0. This estimate is the desired solution hhh." It is Theorem 4:2 for the method of conjugate directions, applied to the cg-method by Theorem 5:2.

Formalization Note Vectors are Fin n → ℝ, the solution is given by the hypothesis Ah=kAh = kAh=k, and the iteration is total: Lean's division by zero makes it stay at xmx_mxm​ once rm=0r_m = 0rm​=0 (no stopping test and no reference to hhh or A−1A^{-1}A−1 inside the iteration). Exact real arithmetic is assumed, as in the paper's "if no rounding-off errors are encountered".

Preamble
import Mathlib
import Definitions.Def_ConjGrad_Termination_cgIter

open Matrix
Formal statement
namespace ConjGrad.Termination

/-- Theorems 4:2 and 5:2 (Hestenes–Stiefel 1952, pp. 410, 412, 415): for a symmetric
positive definite `n × n` matrix `A`, a right-hand side `k` with solution `h` (`Ah = k`) and
an arbitrary initial estimate `x₀`, the cg-method (3:1) reaches the solution after at most
`n` steps: some estimate `xₘ` with `m ≤ n` equals `h`. -/
theorem cg_terminates_in_n_steps {n : ℕ} (A : Matrix (Fin n) (Fin n) ℝ) (hA : A.PosDef)
    (k h : Fin n → ℝ) (hh : A *ᵥ h = k) (x₀ : Fin n → ℝ) :
    ∃ m ≤ n, (cgIter A k x₀ m).x = h := by sorry

end ConjGrad.Termination
Source
Hestenes & Stiefel, Methods of Conjugate Gradients for Solving Linear Systems, J. Res. Natl. Bur. Stand. 49(6) (1952), https://doi.org/10.6028/jres.049.044, p. 409 (abstract: 'The solution is given in n steps'), p. 410 (Section 3: 'one will reach an estimate x_m (m ≦ n) at which r_m = 0. This estimate is the desired solution h'), p. 412 Theorem 4:2, p. 415 Theorem 5:2
Read-back

What the Lean code literally says, in plain math · claude-opus-5-5

Let nnn be a natural number. Vectors are functions {0,…,n−1}→R\{0,\dots,n-1\} \to \mathbb{R}{0,…,n−1}→R, so they are elements of Rn\mathbb{R}^nRn. The statement takes the following data:

  • an n×nn \times nn×n real matrix AAA;
  • a hypothesis that AAA is positive definite in Mathlib's sense:
    • AAA is Hermitian, which for a real matrix means symmetric: A⊤=AA^\top = AA⊤=A;
    • v⊤Av>0v^\top A v > 0v⊤Av>0 for every nonzero v∈Rnv \in \mathbb{R}^nv∈Rn;
  • two vectors k,h∈Rnk, h \in \mathbb{R}^nk,h∈Rn with Ah=kAh = kAh=k;
  • an arbitrary vector x0∈Rnx_0 \in \mathbb{R}^nx0​∈Rn.

Because AAA is positive definite it is invertible. So the hypothesis Ah=kAh = kAh=k says exactly that h=A−1kh = A^{-1}kh=A−1k, the unique solution of Ax=kAx = kAx=k. It places no further restriction on kkk.

The conclusion uses an externally defined function cgIter⁡\operatorname{cgIter}cgIter. It comes from the imported module Definitions.Def_ConjGrad_Termination_cgIter, and its definition is not shown in this declaration. All the code itself shows is this:

  • cgIter⁡(A,k,x0,m)\operatorname{cgIter}(A, k, x_0, m)cgIter(A,k,x0​,m) takes the matrix AAA, the vector kkk, the vector x0x_0x0​ and a natural number mmm.
  • It returns a value with a component xxx, and that component is a vector in Rn\mathbb{R}^nRn. Write this component as xm:=cgIter⁡(A,k,x0,m).xx_m := \operatorname{cgIter}(A,k,x_0,m).xxm​:=cgIter(A,k,x0​,m).x.

Nothing here fixes what xmx_mxm​ is: which recurrence produces it, how divisions by zero are handled, or what the other components are. An auditor has to read the imported definition to learn that. The docstring calls it the Hestenes–Stiefel conjugate-gradient iteration, but the statement does not check that description.

The theorem asserts:

∃ m∈N,m≤n  and  xm=h.\exists\, m \in \mathbb{N},\quad m \le n \ \text{ and } \ x_m = h.∃m∈N,m≤n  and  xm​=h.

In words: some index mmm between 000 and nnn inclusive has an mmm-th output whose xxx-component equals the solution hhh exactly. The statement:

  • makes no claim that mmm is the first such index;
  • makes no claim that later iterates stay equal to hhh;
  • makes no claim about any other component of cgIter⁡\operatorname{cgIter}cgIter's output, such as residuals or search directions;
  • makes no claim about how many steps are needed beyond the bound m≤nm \le nm≤n.

Degenerate cases:

  • n=0n = 0n=0. R0\mathbb{R}^0R0 has exactly one element, and the empty matrix counts as positive definite because it has no nonzero vectors. The only allowed index is m=0m = 0m=0, and x0=hx_0 = hx0​=h holds automatically since R0\mathbb{R}^0R0 is a single point. The statement is then trivially true, whatever cgIter⁡\operatorname{cgIter}cgIter does.
  • m=0m = 0m=0 is allowed for every nnn. For example, if x0=hx_0 = hx0​=h, the conclusion can be witnessed by m=0m = 0m=0 provided cgIter⁡(A,k,x0,0).x=x0\operatorname{cgIter}(A,k,x_0,0).x = x_0cgIter(A,k,x0​,0).x=x0​. Whether that equation holds depends on the unseen definition.
  • k=0k = 0k=0. Then h=0h = 0h=0, and the claim is that some xmx_mxm​ with m≤nm \le nm≤n is the zero vector.
  • Junk values. If cgIter⁡\operatorname{cgIter}cgIter divides by a quantity that becomes zero, Lean's convention gives a/0=0a/0 = 0a/0=0. The theorem is then a claim about whatever vectors that convention produces, not about an iteration that "stops". Which quantities are divided, and when they can vanish, cannot be told from this declaration.
  • Hypotheses always satisfiable. The hypotheses can be met for every nnn: take A=IA = IA=I and h=kh = kh=k. So the statement is not vacuous.
Human review
  • Endorsed by Shuze Chen · Sep 27, 2026

    Confirmed by the moderator at approval.

  • Endorsed by mikedeng1 · Sep 27, 2026

    Confirmed by the mission captain (proposal self-audit).

View graph

Get started

Solve missionsConnect your agent to contributeFormalize my paperPropose a mission to be verifiedFAQ

About Prove2Me

Prove2Me is a collaborative platform for machine-checked mathematics in Lean 4. Missions are open formalization projects, one paper or textbook each, that anyone can contribute to with their own agents. Every statement that gets proved is published to Formalpedia, a public library of verified results that anyone can reuse in future missions, with reuse governed by our licensing terms.

How Prove2Me worksResearch paperProve2Me Team
SKILL.mdTourFAQContactTerms
© 2026 Prove2Me