Documentation

CslibFrontier.Foundations.LinearAlgebra.Matrix.GaussJordan

Staged Gauss–Jordan elimination and matrix rank #

This file computes the rank of a matrix over a field by Gauss–Jordan elimination without row swaps, one stage per column. Stage j looks for the first row that is zero on the columns before j and nonzero at j (Matrix.GaussJordan.pivotRow). If there is one, it subtracts multiples of that row from every other row to clear column j (Matrix.GaussJordan.stage); otherwise it leaves the matrix unchanged. Rows are never swapped, so every stage is a fixed function of the matrix.

Each stage multiplies on the left by an invertible matrix, so it preserves the rank. After all c stages, every nonzero row owns at least one column where it is nonzero and every other row is zero (Matrix.GaussJordan.exists_pivot_of_reduce_ne_zero); distinct rows own distinct columns, since an owned column has only one nonzero entry. The rank is therefore the number of nonzero rows (Matrix.GaussJordan.rank_eq_card_reduce_ne_zero). A row may own several columns: !![1, 1] is already reduced and has rank 1. The rows of the output are not sorted into row echelon form, so Matrix.IsPivotedBy does not apply directly.

Over ZMod 2 a stage needs no division: the pivot entry is 1, and the stage adds the pivot row to every other row with a 1 in column j (Matrix.GaussJordan.stage_apply_zmod_two). Each entry of a stage is then an XOR of ANDs of entries and pivot indicators, and the pivot is a prefix condition on column j (Matrix.GaussJordan.pivotRow_eq_some_iff). A Boolean circuit with unbounded fan-in AND and OR gates can evaluate one stage in constant depth; the full reduction chains c stages, so it has polynomial size and depth O(c).

Main definitions #

Main results #

References #

Gauss–Jordan elimination is standard. Computing a GF(2) rank this way is the constructivity step of the rank property in Razborov and Rudich, §3.2.

def Matrix.GaussJordan.IsFreeBefore {m : Type u_1} {c : ℕ} {K : Type u_2} [Zero K] (A : Matrix m (Fin c) K) (k : ℕ) (i : m) :

Row i of A is zero on the columns before k.

Instances For
    @[instance_reducible]

    Elimination with a fixed pivot row #

    def Matrix.GaussJordan.eliminate {m : Type u_1} {c : ℕ} {K : Type u_2} [DecidableEq m] [Field K] (A : Matrix m (Fin c) K) (j : Fin c) (p : m) :
    Matrix m (Fin c) K

    Clear column j using pivot row p: every row i ≠ p loses A i j / A p j times row p.

    theorem Matrix.GaussJordan.eliminate_apply_self {m : Type u_1} {c : ℕ} {K : Type u_2} [DecidableEq m] [Field K] (A : Matrix m (Fin c) K) (j : Fin c) (p : m) (l : Fin c) :
    eliminate A j p p l = A p l
    theorem Matrix.GaussJordan.eliminate_apply_of_ne {m : Type u_1} {c : ℕ} {K : Type u_2} [DecidableEq m] [Field K] {A : Matrix m (Fin c) K} {j : Fin c} {p i : m} (h : i ≠ p) (l : Fin c) :
    eliminate A j p i l = A i l - A i j / A p j * A p l
    theorem Matrix.GaussJordan.eliminate_apply_of_eq_zero {m : Type u_1} {c : ℕ} {K : Type u_2} [DecidableEq m] [Field K] {A : Matrix m (Fin c) K} {j l : Fin c} {p : m} (h : A p l = 0) (i : m) :
    eliminate A j p i l = A i l

    Elimination with a pivot row that is zero in column l leaves column l unchanged.

    theorem Matrix.GaussJordan.eliminate_apply_of_ne_of_ne_zero {m : Type u_1} {c : ℕ} {K : Type u_2} [DecidableEq m] [Field K] {A : Matrix m (Fin c) K} {j : Fin c} {p i : m} (hp : A p j ≠ 0) (h : i ≠ p) :
    eliminate A j p i j = 0

    Elimination with a pivot nonzero in column j clears column j outside the pivot row.

    theorem Matrix.GaussJordan.rank_eliminate {m : Type u_1} {c : ℕ} {K : Type u_2} [DecidableEq m] [Field K] [Finite m] (A : Matrix m (Fin c) K) (j : Fin c) (p : m) :
    (eliminate A j p).rank = A.rank

    Elimination preserves the rank. It is left multiplication by 1 - D, where D is supported on column p off row p; then D * D = 0, so 1 + D inverts 1 - D.

    The stages #

    def Matrix.GaussJordan.pivotCandidates {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) (j : Fin c) :

    The candidate pivot rows for column j: the rows that are zero before j and nonzero at j.

    theorem Matrix.GaussJordan.mem_pivotCandidates {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [Field K] [DecidableEq K] {A : Matrix m (Fin c) K} {j : Fin c} {i : m} :
    i ∈ pivotCandidates A j ↔ IsFreeBefore A (↑j) i ∧ A i j ≠ 0
    def Matrix.GaussJordan.pivotRow {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) (j : Fin c) :

    The pivot row for column j: the first row that is zero on the columns before j and nonzero at j, or none if there is no such row.

    def Matrix.GaussJordan.stage {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) (j : Fin c) :
    Matrix m (Fin c) K

    One stage of Gauss–Jordan elimination at column j: if column j has a pivot row, clear column j in every other row; otherwise leave the matrix unchanged.

    def Matrix.GaussJordan.reduceUpTo {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) :
    ℕ → Matrix m (Fin c) K

    The stages for the columns 0, …, k - 1, in order; indices from c on are skipped.

    def Matrix.GaussJordan.reduce {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) :
    Matrix m (Fin c) K

    Gauss–Jordan elimination: the stages for all columns, in order.

    theorem Matrix.GaussJordan.pivotRow_eq_some_iff {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] {A : Matrix m (Fin c) K} {j : Fin c} {p : m} :
    pivotRow A j = some p ↔ IsFreeBefore A (↑j) p ∧ A p j ≠ 0 ∧ ∀ i < p, IsFreeBefore A (↑j) i → A i j = 0

    The pivot row is the first candidate: it is free before j and nonzero at j, and every earlier row that is free before j is zero at j.

    theorem Matrix.GaussJordan.pivotRow_eq_none_iff {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] {A : Matrix m (Fin c) K} {j : Fin c} :
    pivotRow A j = none ↔ ∀ (i : m), IsFreeBefore A (↑j) i → A i j = 0

    Column j has no pivot row exactly when every row that is free before j is zero at j.

    theorem Matrix.GaussJordan.stage_of_pivotRow_eq_none {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] {A : Matrix m (Fin c) K} {j : Fin c} (h : pivotRow A j = none) :
    stage A j = A
    theorem Matrix.GaussJordan.stage_of_pivotRow_eq_some {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] {A : Matrix m (Fin c) K} {j : Fin c} {p : m} (h : pivotRow A j = some p) :
    stage A j = eliminate A j p
    theorem Matrix.GaussJordan.reduceUpTo_zero {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) :
    theorem Matrix.GaussJordan.reduceUpTo_succ_of_lt {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) {k : ℕ} (h : k < c) :
    reduceUpTo A (k + 1) = stage (reduceUpTo A k) ⟨k, h⟩
    theorem Matrix.GaussJordan.reduceUpTo_succ_of_le {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) {k : ℕ} (h : c ≤ k) :
    reduceUpTo A (k + 1) = reduceUpTo A k

    Rank #

    theorem Matrix.GaussJordan.rank_stage {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) (j : Fin c) :
    (stage A j).rank = A.rank

    A stage preserves the rank.

    theorem Matrix.GaussJordan.rank_reduceUpTo {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) (k : ℕ) :
    theorem Matrix.GaussJordan.rank_reduce {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) :

    Gauss–Jordan elimination preserves the rank.

    theorem Matrix.GaussJordan.exists_pivot_of_reduce_ne_zero {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) {i : m} (hi : (reduce A).row i ≠ 0) :
    ∃ (l : Fin c), reduce A i l ≠ 0 ∧ ∀ (i' : m), i' ≠ i → reduce A i' l = 0

    Every nonzero row of reduce A owns a column: it is nonzero there and every other row is zero there. Distinct rows own distinct columns, but one row may own several.

    theorem Matrix.GaussJordan.rank_eq_card_reduce_ne_zero {m : Type u_1} {c : ℕ} {K : Type u_2} [Fintype m] [LinearOrder m] [Field K] [DecidableEq K] (A : Matrix m (Fin c) K) :
    A.rank = {i : m | (reduce A).row i ≠ 0}.card

    Rank by Gauss–Jordan elimination: the rank of A is the number of nonzero rows of reduce A. Each nonzero row owns a column where every other row is zero (exists_pivot_of_reduce_ne_zero), and these give a diagonal submatrix of full size.

    Over ZMod 2 #

    theorem Matrix.GaussJordan.stage_apply_zmod_two {m : Type u_1} {c : ℕ} [Fintype m] [LinearOrder m] (A : Matrix m (Fin c) (ZMod 2)) (j : Fin c) (i : m) (l : Fin c) :
    stage A j i l = A i l + ∑ p : m, if pivotRow A j = some p ∧ p ≠ i then A i j * A p l else 0

    Over ZMod 2 a stage needs no division. The pivot entry is 1, and the stage adds the pivot row p to every other row i with A i j = 1. Each entry is a sum of products of entries and pivot indicators, which a Boolean circuit evaluates with XOR and AND gates.