Documentation

CslibFrontier.Algorithms.LinearAlgebra.LDLCertificate

Exact LDLᵀ certificates for rational matrices, checked by the kernel #

A symmetric rational matrix M is positive definite when it factors as M = L * diagonal d * Lᵀ with L unit lower triangular and every d i > 0, and positive semidefinite when every d i ≥ 0 (Matrix.posDef_mul_diagonal_mul_conjTranspose_iff). An external tool, such as SymPy's LDLdecomposition, finds L and d; this module checks them exactly, by a Boolean function that the kernel evaluates (decide +kernel), and turns acceptance into Mathlib's Matrix.PosDef or Matrix.PosSemidef of M cast to ℝ. A singular positive semidefinite M has a witness with some d i = 0, and the checker never divides.

The checkers are also complete: a matrix given by n rows of n entries has an accepted witness exactly when it is positive (semi)definite over ℝ. Symmetric elimination without pivoting (Matrix.exists_LDL_of_posSemidef, Matrix.exists_LDL_of_posDef) stays in ℚ and supplies one. A rejected witness says nothing about the matrix, but the absence of any accepted witness refutes ⪰ 0 (≻ 0).

Representation #

A matrix is given by its list of rows and read as Mathlib.Tactic.Matrix.ofLists m n rows, the m × n matrix of the first m rows and their first n entries, padded with zeros; for a list literal this is the !![…] matrix up to definitional unfolding. The dimensions are explicit, so the theorems concern Matrix (Fin m) (Fin n) ℚ and their casts to ℝ. The checkers do not rely on the padding: Cslib.LinearAlgebra.LDLCert.factors rejects a matrix unless it has exactly n rows of exactly n entries (Cslib.LinearAlgebra.isShape).

Matrices are combined on rows by Mathlib.Tactic.Matrix.ListMatrix.transpose, Cslib.LinearAlgebra.rowsMul (a product the kernel evaluates; Mathlib's ListMatrix.mul seals its dot product), Cslib.LinearAlgebra.rowsMap₂ (entrywise sums, differences, negations and scalings) and Cslib.LinearAlgebra.rowsTrace, each interpreted by an ofLists lemma without any shape hypothesis. A trace of a product, such as the Gramian energy tr (Bᵀ * W * B) of #8322, is rowsTrace of rowsMul (Cslib.LinearAlgebra.trace_mul_ofLists). A positive semidefinite residual N - M ⪰ 0 is checked on rowsMap₂ (· - ·) n n N M, and the Lyapunov client Cslib.LinearAlgebra.LyapunovCert recomputes -(Aᵀ * P + P * A) from A and P instead of taking it as input, in integers (Cslib.LinearAlgebra.scaledLyapunovResidual, which agrees with Cslib.LinearAlgebra.lyapunovResidual by Cslib.LinearAlgebra.map_ofLists_scaledLyapunovResidual).

A certificate Cslib.LinearAlgebra.LDLCert lists the rows of L and the entries of d. Row i of lower holds L i 0, …, L i i, so triangularity is the length i + 1 of the row and the unit diagonal is its last entry 1. A certificate for n × n matrices is rejected unless lower has exactly n rows of these lengths, each ending in 1, and diag has exactly n entries. The empty dimension n = 0 has the empty certificate, and the empty matrix is positive definite.

Certificate layout #

A certificate is ordinary Lean source: the rows of M (or A and P), lower and diag as list literals of rational literals. Write every non-integer entry with its type, as (5/4 : ℚ). Bare 5/4 entries are elaborated as untyped binop% trees whose type is settled only when the whole literal is, and elaborating a 48 × 48 matrix of them did not finish in 20 minutes; with the ascription elaboration is linear in the size of the literal (about 5 s for a 48 × 48 certificate). The ascribed and the bare literal elaborate to the same term, so the kernel cost is unchanged.

Kernel cost #

The identity M = L * diagonal d * Lᵀ is checked in integer arithmetic. Row i of L is scaled to integers by the least common multiple sᵢ of its denominators, and d by t (Cslib.LinearAlgebra.denLcm, Cslib.LinearAlgebra.scaleRow), and entry (i, j) is checked as M i j * (sᵢ * sⱼ * t) = ∑ₖ aᵢₖ eₖ aⱼₖ for the scaled rows a and e, with the rational M i j compared through its numerator and denominator. Rational + and * normalize by a gcd at every step; in the kernel the integer form is the faster one on the benchmarks of #8319, increasingly so as the coefficients grow. Only the entries on and below the diagonal are checked (Cslib.LinearAlgebra.LDLCert.scaledLowerCheck), at j + 1 integer products for entry (i, j), and the rest follow from the symmetry of M, which factors checks by comparing the rows with their transpose. The Lyapunov residual is computed from A and P scaled by the lcm of all their denominators, so its two products are integer products, and its symmetry is not checked but proved from that of the accepted P. All recursion is structural, on lists or on the dimensions, and the kernel's GMP-accelerated Nat operations do the arithmetic.

Main definitions #

Main results #

Kernel-reducible operations on lists of rows #

def Cslib.LinearAlgebra.rowDot {α : Type u_1} [Mul α] [Add α] [Zero α] :
List α → List α → α

The sum of the products of corresponding entries of two lists, over the positions where both have one. Unlike Mathlib's sealed Mathlib.Tactic.Matrix.ListMatrix.dotProduct, the kernel evaluates it.

theorem Cslib.LinearAlgebra.rowDot_eq_sum {α : Type u_1} [NonUnitalNonAssocSemiring α] {n : ℕ} {r : List α} (s : List α) (hr : r.length ≤ n) :
rowDot r s = ∑ k : Fin n, r.getD (↑k) 0 * s.getD (↑k) 0

rowDot r s is the dot product of the length-n vectors read from r and s, padded with zeros, whenever r has at most n entries.

def Cslib.LinearAlgebra.rowsMul {α : Type u_1} [Mul α] [Add α] [Zero α] (l m n : ℕ) (A B : List (List α)) :
List (List α)

The product of A, read as l × m, and B, read as m × n, as l rows of n entries: Mathlib's Mathlib.Tactic.Matrix.ListMatrix.mul with rowDot in place of its sealed dot product, so that the kernel evaluates it.

rowsMul computes the matrix product, with no shape hypothesis on A or B.

def Cslib.LinearAlgebra.rowMap₂ {α : Type u_1} {β : Type u_2} {γ : Type u_3} [Zero α] [Zero β] (f : α → β → γ) :
ℕ → List α → List β → List γ

f applied to corresponding entries of the first n entries of two lists, padded with zeros.

def Cslib.LinearAlgebra.rowsMap₂ {α : Type u_1} {β : Type u_2} {γ : Type u_3} [Zero α] [Zero β] (f : α → β → γ) :
ℕ → ℕ → List (List α) → List (List β) → List (List γ)

f applied entrywise to A and B, both read as m × n matrices, as m rows of n entries.

theorem Cslib.LinearAlgebra.ofLists_rowsMap₂ {α : Type u_1} {β : Type u_2} {γ : Type u_3} [Zero α] [Zero β] [Zero γ] (f : α → β → γ) (m n : ℕ) (A : List (List α)) (B : List (List β)) :

rowsMap₂ f computes f entrywise, with no shape hypothesis on A or B.

def Cslib.LinearAlgebra.rowsTrace {α : Type u_1} [Add α] [Zero α] :
ℕ → List (List α) → α

The sum of the first n diagonal entries of rows, read as an n × n matrix padded with zeros: entry (0, 0), then the trace of the rows below with their first entries dropped.

theorem Cslib.LinearAlgebra.trace_ofLists {α : Type u_1} [AddCommMonoid α] (n : ℕ) (rows : List (List α)) :

rowsTrace computes the trace, with no shape hypothesis on rows.

The trace of a product computed on rows, such as tr (Bᵀ * W * B): rowsTrace of rowsMul.

theorem Cslib.LinearAlgebra.getD_zipWith_mul {α : Type u_1} [MulZeroClass α] (a b : List α) (k : ℕ) :
(List.zipWith (fun (x1 x2 : α) => x1 * x2) a b).getD k 0 = a.getD k 0 * b.getD k 0

List.zipWith (· * ·), read entrywise with zero padding.

def Cslib.LinearAlgebra.isShape {α : Type u_1} (m n : ℕ) (rows : List (List α)) :

Whether rows has exactly m rows of exactly n entries.

Integer scaling of rational rows #

The least common multiple of the denominators of a list of rationals.

The integer list s * q for the entries q of r, exact when every q.den divides s.

The denominators' lcm is positive.

theorem Cslib.LinearAlgebra.den_dvd_denLcm {r : List ℚ} {q : ℚ} (hq : q ∈ r) :

Every denominator divides denLcm.

theorem Cslib.LinearAlgebra.cast_getD_scaleRow {s : ℕ} {r : List ℚ} (hs : ∀ q ∈ r, q.den ∣ s) (k : ℕ) :
↑((scaleRow s r).getD k 0) = ↑s * r.getD k 0

scaleRow s r is s times r, entrywise and with zero padding, when every denominator of r divides s.

Scaling every row of A by a common multiple s of its denominators scales its matrix.

LDLᵀ certificates #

An LDLᵀ witness for an n × n rational matrix: L unit lower triangular and d the diagonal. Row i of lower lists L i 0, …, L i i, ending in the diagonal entry 1.

The factor L as an n × n matrix: the rows of c.lower, padded with zeros.

The diagonal factor d as an n-vector, padded with zeros.

Whether row k of rows has i + k + 1 entries ending in 1, for every k: the rows i, i + 1, … of a unit lower-triangular factor.

def Cslib.LinearAlgebra.LDLCert.scaledLowerCheck {β : Type u_1} (num : β → ℤ) (den : β → ℕ) (rows : List (List β)) (lower : List (List ℚ)) (diag : List ℚ) :

Whether v = L * diagonal d * Lᵀ holds on and below the diagonal, for v given by rows of entries x standing for num x / den x, checked in integers: row i of L is scaled by sᵢ = denLcm (row i) and d by t = denLcm d, and entry (i, j) with j ≤ i is checked as num x * (sᵢ * sⱼ * t) = (∑ₖ aᵢₖ eₖ aⱼₖ) * den x for the scaled rows a and e. Row i of L has i + 1 entries, so the zip with the first i + 1 entries of row i of v stops at the diagonal.

def Cslib.LinearAlgebra.LDLCert.factorsLower (c : LDLCert) {β : Type u_1} (num : β → ℤ) (den : β → ℕ) (n : ℕ) (rows : List (List β)) :

Whether c factors a symmetric n × n matrix given by rows of entries x standing for num x / den x, on and below the diagonal: rows has n rows of n entries, c.lower has n rows, row i with i + 1 entries ending in 1, c.diag has n entries, and scaledLowerCheck accepts. Symmetry is not checked here.

Whether c is an LDLᵀ factorization of the n × n matrix with rows rows: rows is symmetric, it has n rows of n entries, c.lower has n rows, row i with i + 1 entries ending in 1, c.diag has n entries, and ofLists n n rows = c.L n * diagonal (c.d n) * (c.L n)ᵀ on and below the diagonal, hence everywhere.

Whether c certifies ofLists n n rows ≻ 0: it factors the matrix and every d i is positive.

Whether c certifies ofLists n n rows ⪰ 0: it factors the matrix and every d i is nonnegative.

The product L * diagonal d * Lᵀ is symmetric.

theorem Cslib.LinearAlgebra.LDLCert.eq_of_factors (c : LDLCert) {n : ℕ} {rows : List (List ℚ)} (h : c.factors n rows = true) :

An accepted witness factors the matrix: M = L * diagonal d * Lᵀ over ℚ.

theorem Cslib.LinearAlgebra.LDLCert.map_eq_of_eq (c : LDLCert) {n : ℕ} {v : Matrix (Fin n) (Fin n) ℚ} (hlen : c.lower.length = n) (hdlen : c.diag.length = n) (hl : unitLowerFrom 0 c.lower = true) (hM : v = c.L n * Matrix.diagonal (c.d n) * (c.L n).transpose) :
(∀ (i : Fin n), c.d n i ∈ c.diag) ∧ ∃ (L : Matrix (Fin n) (Fin n) ℝ), L.IsLowerTriangular ∧ (∀ (i : Fin n), L i i = 1) ∧ v.map Rat.cast = (L * Matrix.diagonal fun (i : Fin n) => ↑(c.d n i)) * L.conjTranspose

The real form of a factorization by a well-formed witness, with L unit lower triangular.

theorem Cslib.LinearAlgebra.LDLCert.posDef_of_eq (c : LDLCert) {n : ℕ} {v : Matrix (Fin n) (Fin n) ℚ} (hlen : c.lower.length = n) (hdlen : c.diag.length = n) (hl : unitLowerFrom 0 c.lower = true) (hd : (c.diag.all fun (x : ℚ) => decide (0 < x)) = true) (hM : v = c.L n * Matrix.diagonal (c.d n) * (c.L n).transpose) :

A well-formed witness with positive diagonal proves the matrix it factors positive definite over ℝ.

theorem Cslib.LinearAlgebra.LDLCert.posSemidef_of_eq (c : LDLCert) {n : ℕ} {v : Matrix (Fin n) (Fin n) ℚ} (hlen : c.lower.length = n) (hdlen : c.diag.length = n) (hl : unitLowerFrom 0 c.lower = true) (hd : (c.diag.all fun (x : ℚ) => decide (0 ≤ x)) = true) (hM : v = c.L n * Matrix.diagonal (c.d n) * (c.L n).transpose) :

A well-formed witness with nonnegative diagonal proves the matrix it factors positive semidefinite over ℝ.

Soundness for ≻ 0. An accepted witness proves the matrix positive definite over ℝ.

Soundness for ⪰ 0. An accepted witness proves the matrix positive semidefinite over ℝ, including a singular one, whose witnesses have some d i = 0.

Completeness #

Completeness for ⪰ 0. Some witness is accepted exactly when rows lists n rows of n entries and the matrix is positive semidefinite over ℝ, singular or not (Matrix.exists_LDL_of_posSemidef). A rejected witness therefore says nothing about the matrix, but the absence of any accepted witness for a well-shaped rows refutes ⪰ 0.

Completeness for ≻ 0. Some witness is accepted exactly when rows lists n rows of n entries and the matrix is positive definite over ℝ (Matrix.exists_LDL_of_posDef).

Lyapunov residuals #

def Cslib.LinearAlgebra.lyapunovResidual {α : Type u_1} [Ring α] (n : ℕ) (A P : List (List α)) :
List (List α)

The rows of -(Aᵀ * P + P * A) for the n × n matrices with rows A and P.

The common scale denLcm A * denLcm P of the fraction-free Lyapunov residual.

The fraction-free Lyapunov residual: -(Âᵀ * P̂ + P̂ * Â) in integers, for  and P̂ the rows of A and P scaled by the lcm of all their denominators. It is lyapunovScale A P • -(Aᵀ * P + P * A) (map_ofLists_scaledLyapunovResidual).

Whether c factors the recomputed residual -(Aᵀ * P + P * A) of the n × n matrices with rows A and P, on and below the diagonal and in integers (scaledLyapunovResidual). The residual's symmetry is not checked: it follows from that of P (LDLCert.eq_of_factorsLyapunovResidual).

For a symmetric P, an accepted residual check factors -(Aᵀ * P + P * A) over ℚ, and c is well formed.

Residual soundness for ≻ 0. For a symmetric P, an accepted residual check with positive diagonal proves -(Aᵀ * P + P * A) ≻ 0 over ℝ.

Residual soundness for ⪰ 0. For a symmetric P, an accepted residual check with nonnegative diagonal proves -(Aᵀ * P + P * A) ⪰ 0 over ℝ.

Witnesses for the two inequalities of a quadratic Lyapunov certificate for ẋ = A x: P ≻ 0 and -(Aᵀ * P + P * A) ≻ 0.

  • An LDLᵀ witness for P.

  • An LDLᵀ witness for -(Aᵀ * P + P * A).

Whether c certifies P ≻ 0 and -(Aᵀ * P + P * A) ≻ 0 for the n × n matrices with rows A and P. The residual is recomputed from A and P, in integers (scaledLyapunovResidual), and checked on and below the diagonal: it is symmetric because the accepted P is.

Soundness. An accepted Lyapunov witness proves P ≻ 0 and -(Aᵀ * P + P * A) ≻ 0 for the real matrices A and P.

Instability certificates #

The rows of the scalar matrix ε • 1 of size n × n.

The rows of A - ε • 1 for the n × n matrix with rows A.

shiftRows computes A - ε • 1, with no shape hypothesis on A.

The quadratic form xᵀ P x of the n × n matrix with rows P at the n-vector x, both padded with zeros, computed on lists.

quadForm computes x ⬝ᵥ P *ᵥ x, with no shape hypothesis.

theorem Cslib.LinearAlgebra.map_ratCast_sub_smul_one {ι : Type u_1} [DecidableEq ι] (M : Matrix ι ι ℚ) (ε : ℚ) :
(M - ε • 1).map Rat.cast = M.map Rat.cast - ↑ε • 1

The real cast of M - ε • 1 is M - ε • 1 over ℝ.

theorem Cslib.LinearAlgebra.dotProduct_mulVec_ratCast_neg {ι : Type u_1} [Fintype ι] {M : Matrix ι ι ℚ} {x : ι → ℚ} (h : x ⬝ᵥ M.mulVec x < 0) :
((fun (i : ι) => ↑(x i)) ⬝ᵥ (M.map Rat.cast).mulVec fun (i : ι) => ↑(x i)) < 0

A rational quadratic form that is negative at x stays negative over ℝ.

An instability (or slow-decay) certificate for ẋ = A x with the symmetric matrix P, which need not be definite: a rational rate ε of either sign, an initial vector x₀ with x₀ᵀ P x₀ < 0, and an LDLᵀ witness for -((A - ε • 1)ᵀ * P + P * (A - ε • 1)) ⪰ 0.

  • rate : ℚ

    The certified rate ε: some trajectory decays no faster than exp (ε t).

  • x₀ : List ℚ

    An initial vector of negative energy x₀ᵀ P x₀ < 0.

  • An LDLᵀ witness for -((A - ε • 1)ᵀ * P + P * (A - ε • 1)), with d ≥ 0.

Whether c certifies, for the n × n matrices with rows A and P, that P is symmetric, c.x₀ ⬝ᵥ P *ᵥ c.x₀ < 0 and -((A - ε • 1)ᵀ * P + P * (A - ε • 1)) ⪰ 0 for ε = c.rate. The residual is recomputed from shiftRows n ε A and P, in integers.

Soundness. An accepted instability certificate proves, over ℝ, that x₀ ⬝ᵥ P *ᵥ x₀ < 0 and -((A - ε • 1)ᵀ * P + P * (A - ε • 1)) ⪰ 0 for ε = c.rate.