Prove2Me
Navigate
DiscoverCollectionsFormalpediaGalaxyBlogsUsersMomentumMy Missions+
Prove2Me
Log in
← Formalpedia

Rational primal simplex and cyclic entering-variable selection

Definition
friedmann_simplex

by kmehltretter82 · Nov 2, 2026 · Mathlib 0df444a (Lean v4.33.1)

algorithm-analysisoliver-friedmanntheoretical-computer-science

Rational standard-form LPs, nonsingular feasible bases and their actual basic solutions, reduced costs, minimum-ratio pivots, boundedness, and Cunningham entering-variable order with a retained cursor.

Definition code
import Mathlib.LinearAlgebra.Matrix.NonsingularInverse
import Mathlib.Data.Rat.Cast.Order

set_option autoImplicit false

/-! Rational standard-form primal simplex: maximize cᵀx subject to Ax=b, x≥0.
No pivot rule or lower bound is included in the data. The inverse is Mathlib's
total matrix inverse; every permitted basis explicitly has nonzero determinant.
-/
namespace FriedmannSimplex

open scoped BigOperators
open Matrix

structure LP (m n : ℕ) where
  A : Matrix (Fin m) (Fin n) ℚ
  b : Fin m → ℚ
  c : Fin n → ℚ

abbrev Basis (m n : ℕ) := Fin m → Fin n

def basisMatrix {m n : ℕ} (P : LP m n) (B : Basis m n) :
    Matrix (Fin m) (Fin m) ℚ := fun i j => P.A i (B j)

noncomputable def basicValues {m n : ℕ} (P : LP m n) (B : Basis m n) :
    Fin m → ℚ := (basisMatrix P B)⁻¹ *ᵥ P.b

noncomputable def basicPoint {m n : ℕ} (P : LP m n) (B : Basis m n) :
    Fin n → ℚ := fun j => ∑ i : Fin m, if B i = j then basicValues P B i else 0

def feasible {m n : ℕ} (P : LP m n) (x : Fin n → ℚ) : Prop :=
  P.A *ᵥ x = P.b ∧ ∀ j, 0 ≤ x j

noncomputable def basicFeasible {m n : ℕ} (P : LP m n) (B : Basis m n) : Prop :=
  Function.Injective B ∧ (basisMatrix P B).det ≠ 0 ∧ ∀ i, 0 ≤ basicValues P B i

def objective {m n : ℕ} (P : LP m n) (x : Fin n → ℚ) : ℚ :=
  ∑ j : Fin n, P.c j * x j

noncomputable def basicObjective {m n : ℕ} (P : LP m n) (B : Basis m n) : ℚ :=
  objective P (basicPoint P B)

noncomputable def direction {m n : ℕ} (P : LP m n) (B : Basis m n)
    (j : Fin n) : Fin m → ℚ :=
  (basisMatrix P B)⁻¹ *ᵥ (fun i => P.A i j)

noncomputable def reducedCost {m n : ℕ} (P : LP m n) (B : Basis m n)
    (j : Fin n) : ℚ :=
  P.c j - ∑ i : Fin m, P.c (B i) * direction P B j i

noncomputable def improving {m n : ℕ} (P : LP m n) (B : Basis m n)
    (j : Fin n) : Prop :=
  (∀ i, B i ≠ j) ∧ 0 < reducedCost P B j

noncomputable def leaving {m n : ℕ} (P : LP m n) (B : Basis m n)
    (j : Fin n) (l : Fin m) : Prop :=
  0 < direction P B j l ∧
    ∀ i, 0 < direction P B j i →
      basicValues P B l / direction P B j l ≤
        basicValues P B i / direction P B j i

noncomputable def pivot {m n : ℕ} (P : LP m n) (B : Basis m n)
    (j : Fin n) (B' : Basis m n) : Prop :=
  basicFeasible P B ∧ improving P B j ∧
    ∃ l, leaving P B j l ∧ B' = Function.update B l j ∧ basicFeasible P B'

noncomputable def optimalBasis {m n : ℕ} (P : LP m n) (B : Basis m n) : Prop :=
  basicFeasible P B ∧ ∀ j, ¬ improving P B j

noncomputable def nondegenerate {m n : ℕ} (P : LP m n) : Prop :=
  ∀ B, basicFeasible P B → ∀ i, 0 < basicValues P B i

def bounded {m n : ℕ} (P : LP m n) : Prop :=
  ∃ R : ℚ, ∀ x, feasible P x → ∀ j, |x j| ≤ R

/-- Distance from the last selected rank. A basic variable cannot enter again,
so including the last rank in the scan agrees with resuming just after it. -/
def cyclicDistance {n : ℕ} (order : Fin n ≃ Fin n) (last j : Fin n) : ℕ :=
  ((order j).val + n - last.val) % n

noncomputable def cunninghamChoice {m n : ℕ} (P : LP m n)
    (order : Fin n ≃ Fin n) (B : Basis m n) (last j : Fin n) : Prop :=
  improving P B j ∧ ∀ k, improving P B k →
    cyclicDistance order last j ≤ cyclicDistance order last k

structure State (m n : ℕ) where
  basis : Basis m n
  last : Fin n

noncomputable def cunninghamStep {m n : ℕ} (P : LP m n)
    (order : Fin n ≃ Fin n) (s s' : State m n) : Prop :=
  ∃ j, cunninghamChoice P order s.basis s.last j ∧
    pivot P s.basis j s'.basis ∧ s'.last = order j

/-- A finite complete run. The endpoint is genuinely optimal; every edge is an
actual ratio-test simplex pivot. Strictness is a conclusion of source targets. -/
noncomputable def cunninghamRun {m n : ℕ} (P : LP m n)
    (order : Fin n ≃ Fin n) (s : ℕ → State m n) (T : ℕ) : Prop :=
  basicFeasible P (s 0).basis ∧
    (∀ t, t < T → cunninghamStep P order (s t) (s (t + 1))) ∧
    optimalBasis P (s T).basis

end FriedmannSimplex
Source
Avis and Friedmann, Mathematical Programming2017, §4.1; https://arxiv.org/abs/1305.3944
Read-back

What the Lean code literally says, in plain math · Codex independent auditor (model identifier unavailable)

LP. For any natural numbers m,nm,nm,n, including zero, a rational linear-program datum consists of an m×nm\times nm×n rational matrix AAA, a rational vector bbb indexed by {0,…,m−1}\{0,\ldots,m-1\}{0,…,m−1}, and a rational vector ccc indexed by {0,…,n−1}\{0,\ldots,n-1\}{0,…,n−1}; the structure itself imposes no feasibility, rank, sign, or boundedness conditions.

Basis. For any natural numbers m,nm,nm,n, a basis is any function B:{0,…,m−1}→{0,…,n−1}B:\{0,\ldots,m-1\}\to\{0,\ldots,n-1\}B:{0,…,m−1}→{0,…,n−1}. Injectivity and invertibility are not part of this definition. If m=0m=0m=0 the function is the empty function; if m>0m>0m>0 and n=0n=0n=0 no such function exists.

basisMatrix. Given natural numbers m,nm,nm,n, rational data P=(A,b,c)P=(A,b,c)P=(A,b,c) of the indicated dimensions, and a function B:{0,…,m−1}→{0,…,n−1}B:\{0,\ldots,m-1\}\to\{0,\ldots,n-1\}B:{0,…,m−1}→{0,…,n−1}, the square matrix MBM_BMB​ has entries (MB)ij=Ai,B(j)(M_B)_{ij}=A_{i,B(j)}(MB​)ij​=Ai,B(j)​, so its columns are selected in the order specified by BBB, with repetitions allowed.

basicValues. Given natural numbers m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), and a column-selection function BBB, the basic-value vector is uB=MB−1bu_B=M_B^{-1}buB​=MB−1​b, where (MB)ij=Ai,B(j)(M_B)_{ij}=A_{i,B(j)}(MB​)ij​=Ai,B(j)​. This uses the total matrix inverse: a singular rational matrix has inverse the zero matrix, so the expression is defined without an invertibility hypothesis.

basicPoint. Given natural numbers m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), and a column-selection function BBB, put MB,ij=Ai,B(j)M_{B,ij}=A_{i,B(j)}MB,ij​=Ai,B(j)​ and uB=MB−1bu_B=M_B^{-1}buB​=MB−1​b using the total inverse. The vector xB∈Qnx_B\in\mathbb Q^nxB​∈Qn is defined by (xB)j=∑i=0m−11B(i)=j(uB)i(x_B)_j=\sum_{i=0}^{m-1}\mathbf1_{B(i)=j}(u_B)_i(xB​)j​=∑i=0m−1​1B(i)=j​(uB​)i​. Repeated selections add their basic values; an unselected column receives zero; an empty sum is zero.

feasible. For any natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), and rational vector x∈Qnx\in\mathbb Q^nx∈Qn, feasibility means exactly Ax=bAx=bAx=b and xj≥0x_j\ge0xj​≥0 for every j<nj<nj<n. Zero dimensions are included, with their corresponding empty equations or coordinate conditions.

basicFeasible. For any natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), and function B:{0,…,m−1}→{0,…,n−1}B:\{0,\ldots,m-1\}\to\{0,\ldots,n-1\}B:{0,…,m−1}→{0,…,n−1}, basic feasibility means that BBB is injective, the matrix MB,ij=Ai,B(j)M_{B,ij}=A_{i,B(j)}MB,ij​=Ai,B(j)​ has nonzero determinant, and every coordinate of MB−1bM_B^{-1}bMB−1​b is nonnegative. No additional equation about the embedded vector is included in this predicate; when m=0m=0m=0, the determinant is the empty determinant 111 and the coordinate condition is empty.

objective. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), and x∈Qnx\in\mathbb Q^nx∈Qn, the objective is f(x)=∑j=0n−1cjxjf(x)=\sum_{j=0}^{n-1}c_jx_jf(x)=∑j=0n−1​cj​xj​, equal to zero when n=0n=0n=0.

basicObjective. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), and a column-selection function BBB, the basic objective is ∑j<ncj∑i<m1B(i)=j(MB−1b)i\sum_{j<n}c_j\sum_{i<m}\mathbf1_{B(i)=j}(M_B^{-1}b)_i∑j<n​cj​∑i<m​1B(i)=j​(MB−1​b)i​, with MB,ij=Ai,B(j)M_{B,ij}=A_{i,B(j)}MB,ij​=Ai,B(j)​ and the total inverse, so it is defined even when BBB is repeated or MBM_BMB​ is singular.

direction. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a column-selection function BBB, and j<nj<nj<n, the direction is dB,j=MB−1A⋅jd_{B,j}=M_B^{-1}A_{\cdot j}dB,j​=MB−1​A⋅j​, with MB,ir=Ai,B(r)M_{B,ir}=A_{i,B(r)}MB,ir​=Ai,B(r)​ and total matrix inverse. It is defined without basic feasibility or invertibility assumptions.

reducedCost. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a column-selection function BBB, and j<nj<nj<n, the reduced cost is cˉB,j=cj−∑i<mcB(i)(MB−1A⋅j)i\bar c_{B,j}=c_j-\sum_{i<m}c_{B(i)}(M_B^{-1}A_{\cdot j})_icˉB,j​=cj​−∑i<m​cB(i)​(MB−1​A⋅j​)i​, where MB,ir=Ai,B(r)M_{B,ir}=A_{i,B(r)}MB,ir​=Ai,B(r)​ and the inverse is total.

improving. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a column-selection function BBB, and j<nj<nj<n, improvement means both B(i)≠jB(i)\ne jB(i)=j for every i<mi<mi<m and cj−∑i<mcB(i)(MB−1A⋅j)i>0c_j-\sum_{i<m}c_{B(i)}(M_B^{-1}A_{\cdot j})_i>0cj​−∑i<m​cB(i)​(MB−1​A⋅j​)i​>0, with MB,ir=Ai,B(r)M_{B,ir}=A_{i,B(r)}MB,ir​=Ai,B(r)​. It does not itself require BBB to be basic feasible.

leaving. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a column-selection function BBB, an entering column j<nj<nj<n, and a row ℓ<m\ell<mℓ<m, let u=MB−1bu=M_B^{-1}bu=MB−1​b and d=MB−1A⋅jd=M_B^{-1}A_{\cdot j}d=MB−1​A⋅j​. Row ℓ\ellℓ is leaving exactly when dℓ>0d_\ell>0dℓ​>0 and, for every i<mi<mi<m with di>0d_i>0di​>0, uℓ/dℓ≤ui/diu_\ell/d_\ell\le u_i/d_iuℓ​/dℓ​≤ui​/di​. Ties are permitted. The predicate includes no basic feasibility or entering-improvement requirement. All inverses and rational divisions are total; the displayed denominator comparisons are restricted to positive directions.

pivot. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), column-selection functions B,B′B,B'B,B′, and j<nj<nj<n, a pivot means that BBB is injective, MBM_BMB​ is nonsingular and u=MB−1b≥0u=M_B^{-1}b\ge0u=MB−1​b≥0; that jjj is outside the image of BBB and has positive reduced cost cj−∑i<mcB(i)dic_j-\sum_{i<m}c_{B(i)}d_icj​−∑i<m​cB(i)​di​ for d=MB−1A⋅jd=M_B^{-1}A_{\cdot j}d=MB−1​A⋅j​; and that some row ℓ<m\ell<mℓ<m satisfies dℓ>0d_\ell>0dℓ​>0 and uℓ/dℓ≤ui/diu_\ell/d_\ell\le u_i/d_iuℓ​/dℓ​≤ui​/di​ for every positive did_idi​, with B′B'B′ obtained by replacing only B(ℓ)B(\ell)B(ℓ) by jjj and B′B'B′ also injective with nonsingular selected matrix and nonnegative basic values. The choice among tied leaving rows is unrestricted.

optimalBasis. For natural dimensions m,nm,nm,n and rational data (A,b,c)(A,b,c)(A,b,c), a function BBB is an optimal basis exactly when it is injective, MBM_BMB​ is nonsingular, MB−1b≥0M_B^{-1}b\ge0MB−1​b≥0, and no column outside its image has positive reduced cost cj−∑i<mcB(i)(MB−1A⋅j)ic_j-\sum_{i<m}c_{B(i)}(M_B^{-1}A_{\cdot j})_icj​−∑i<m​cB(i)​(MB−1​A⋅j​)i​. This is the literal stopping predicate; it contains no separate universal comparison with all feasible objective values.

nondegenerate. For natural dimensions m,nm,nm,n and rational data (A,b,c)(A,b,c)(A,b,c), nondegeneracy means that, for every injective column-selection function BBB whose selected matrix MBM_BMB​ is nonsingular and whose vector MB−1bM_B^{-1}bMB−1​b is coordinatewise nonnegative, every coordinate of that vector is strictly positive. If no basic feasible BBB exists the condition holds vacuously; if m=0m=0m=0 its concluding coordinate condition is empty.

bounded. For natural dimensions m,nm,nm,n and rational data (A,b,c)(A,b,c)(A,b,c), boundedness means that some rational number RRR satisfies ∣xj∣≤R|x_j|\le R∣xj​∣≤R for every rational vector xxx with Ax=bAx=bAx=b and x≥0x\ge0x≥0 and every j<nj<nj<n. The witness RRR is not required to be nonnegative; empty feasible sets and empty coordinate sets are included.

cyclicDistance. For a natural number nnn, a permutation ρ\rhoρ of {0,…,n−1}\{0,\ldots,n-1\}{0,…,n−1}, and indices ℓ,j<n\ell,j<nℓ,j<n, the cyclic distance is the natural number Dρ(ℓ,j)=((ρ(j)+n−ℓ) mod n)D_\rho(\ell,j)=((\rho(j)+n-\ell)\bmod n)Dρ​(ℓ,j)=((ρ(j)+n−ℓ)modn), where indices are read as their numerical values and subtraction is natural-number subtraction. The stored ℓ\ellℓ is used directly as a rank, without applying ρ\rhoρ to it. Distance zero is included in the scan. When n=0n=0n=0 there are no arguments ℓ,j\ell,jℓ,j.

cunninghamChoice. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a permutation ρ\rhoρ of the columns, a column-selection function BBB, a stored rank ℓ<n\ell<nℓ<n, and a column j<nj<nj<n, choice means that jjj lies outside the image of BBB, has positive reduced cost cj−∑i<mcB(i)(MB−1A⋅j)ic_j-\sum_{i<m}c_{B(i)}(M_B^{-1}A_{\cdot j})_icj​−∑i<m​cB(i)​(MB−1​A⋅j​)i​, and minimizes ((ρ(j)+n−ℓ) mod n)((\rho(j)+n-\ell)\bmod n)((ρ(j)+n−ℓ)modn) weakly over all columns satisfying those same two improvement conditions. It does not itself require basic feasibility; no improving column means that no choice exists.

State. For any natural dimensions m,nm,nm,n, a state consists of a column-selection function B:{0,…,m−1}→{0,…,n−1}B:\{0,\ldots,m-1\}\to\{0,\ldots,n-1\}B:{0,…,m−1}→{0,…,n−1} and a stored rank ℓ<n\ell<nℓ<n. No feasibility or rank conditions are fields. In particular there are no states if n=0n=0n=0.

cunninghamStep. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a column permutation ρ\rhoρ, and states (B,ℓ),(B′,ℓ′)(B,\ell),(B',\ell')(B,ℓ),(B′,ℓ′), a step means that some nonbasic column jjj with positive reduced cost has weakly least distance ((ρ(j)+n−ℓ) mod n)((\rho(j)+n-\ell)\bmod n)((ρ(j)+n−ℓ)modn) among all such columns; that B,B′B,B'B,B′ are both basic feasible and B′B'B′ is obtained from BBB by entering jjj and leaving a row minimizing the nonnegative-basic-value to positive-direction ratio; and that ℓ′=ρ(j)\ell'=\rho(j)ℓ′=ρ(j). Ties in the ratio test are permitted.

cunninghamRun. For natural dimensions m,nm,nm,n, rational data (A,b,c)(A,b,c)(A,b,c), a column permutation ρ\rhoρ, a sequence of states (Bt,ℓt)(B_t,\ell_t)(Bt​,ℓt​) indexed by all natural numbers, and T∈NT\in\mathbb NT∈N, a run means that B0B_0B0​ is basic feasible; for every 0≤t<T0\le t<T0≤t<T, some improving nonbasic column of least cyclic distance from ℓt\ell_tℓt​ is entered by an actual ratio-test pivot to a basic feasible Bt+1B_{t+1}Bt+1​ and ℓt+1\ell_{t+1}ℓt+1​ is its ρ\rhoρ-rank; and BTB_TBT​ is basic feasible with no improving nonbasic column. Here basic feasibility requires injectivity, nonzero selected determinant and nonnegative basic values, directions and reduced costs use the selected inverse, and a leaving row has positive direction and least basic-value/direction ratio. The number TTT counts pivots, may be zero, and imposes no conditions on states after TTT, no prescribed initial basis or initial rank, and no explicit strict increase of the objective.

Human review
  • Endorsed by Shuze Chen · Nov 3, 2026

    Confirmed by the moderator at approval.

  • Endorsed by kmehltretter82 · Nov 3, 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