Mode of #cas

positiveDefinite

Whether a square matrix with rational entries is positive definite, with a theorem about Mathlib’s Matrix.PosDef either way.

#cas [positiveDefinite] A
decides whether the square matrix A with rational entries is positive definite over ℝ, and proves the answer.

For a symmetric matrix, positive definiteness means xTAx>0 for every nonzero x. It is the condition behind the second-derivative test and many quadratic-form estimates. positiveDefinite proves its answer either way, and the two proofs are of quite different kinds. True comes with an exact factorization A=LDLT over ℚ with positive pivots. False comes with a rational vector x≠0 such that xTAx≤0, which you can check by hand, or with an entry that breaks the symmetry.

Details and Options

  • A is a square matrix literal !![a, b; c, d] or a list of rows [[a, b], [c, d]]. The entries are closed rational expressions such as -3/2, 0.25 or 1/10^15.
  • The theorem is M.PosDef or ¬M.PosDef for the matrix M : Matrix (Fin n) (Fin n) ℝ with the entries of A. Mathlib’s Matrix.PosDef asks that M be Hermitian, which over ℝ means symmetric, and that xTMx>0 for every x≠0.
  • True comes from the symmetric elimination A=LDLT over ℚ, with all pivots in D positive. The report prints the pivots, and the kernel checks the factorization entry by entry and the signs of the pivots (CAS.Analysis.Matrix.posDef_of_ldlCheck).
  • False names a rational vector x≠0 with xTAx≤0, and the kernel checks its quadratic form (CAS.Analysis.Matrix.not_posDef_of_quadFormCheck). For a matrix that is not symmetric, the theorem comes from one entry with Aij≠Aji.
  • The elimination is exact, so there is no tolerance: a pivot of 10−15 counts as positive.
  • There are no options.

Examples (14)#

Basic Examples (2)

A symmetric matrix with the pivots 2 and 32:

In[1]:=
#cas [positiveDefinite] !![2, 1; 1, 2]
Out[1]=

Boolean

True
Domain
2 × 2 real matrix
Input (matrix)
!![2, 1; 1, 2]

Checked by Lean’s kernel: The matrix is positive definite

!![2, 1; 1, 2].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![2, 1; 1, 2].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [2, 3/2], all positive

For this one the vector x=(−2,1) gives xTAx=−3:

In[2]:=
#cas [positiveDefinite] !![1, 2; 2, 1]
Out[2]=

Boolean

False (xᵀAx ≤ 0 for x = [-2, 1])
Domain
2 × 2 real matrix
Input (matrix)
!![1, 2; 2, 1]

Checked by Lean’s kernel: A vector with a nonpositive quadratic form

¬!![1, 2; 2, 1].PosDef
The certificate: 1 checked step
  1. A vector with a nonpositive quadratic form

    ¬!![1, 2; 2, 1].PosDef

    Checked by Lean’s kernel

Scope (4)

Mathlib’s PosDef includes symmetry, so a matrix that is not symmetric is never positive definite:

In[3]:=
#cas [positiveDefinite] !![1, 2; 3, 4]
Out[3]=

Boolean

False (not symmetric)
Domain
2 × 2 real matrix
Input (matrix)
!![1, 2; 3, 4]

Checked by Lean’s kernel: The matrix is not Hermitian

¬!![1, 2; 3, 4].PosDef
The certificate: 1 checked step
  1. The matrix is not Hermitian

    ¬!![1, 2; 3, 4].PosDef

    Checked by Lean’s kernel


The matrix (1111+10−12) has the pivots 1 and 10−12:

In[4]:=
#cas [positiveDefinite] !![1, 1; 1, 1000000000001/1000000000000]
Out[4]=

Boolean

True
Domain
2 × 2 real matrix
Input (matrix)
!![1, 1; 1, 1000000000001/1000000000000]

Checked by Lean’s kernel: The matrix is positive definite

!![1, 1; 1, 1000000000001 / 1000000000000].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![1, 1; 1, 1000000000001 / 1000000000000].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [1, 1/1000000000000], all positive

Tiny entries are fine:

In[5]:=
#cas [positiveDefinite] !![1/10^15, 0; 0, 1/10^15]
Out[5]=

Boolean

True
Domain
2 × 2 real matrix
Input (matrix)
!![1/10^15, 0; 0, 1/10^15]

Checked by Lean’s kernel: The matrix is positive definite

!![1 / 1000000000000000, 0; 0, 1 / 1000000000000000].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![1 / 1000000000000000, 0; 0, 1 / 1000000000000000].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [1/1000000000000000, 1/1000000000000000], all positive

A list of rows with fractions:

In[6]:=
#cas [positiveDefinite] [[1, 1/2], [1/2, 1/3]]
Out[6]=

Boolean

True
Domain
2 × 2 real matrix
Input (matrix)
[[1, 1/2], [1/2, 1/3]]

Checked by Lean’s kernel: The matrix is positive definite

!![1, 1 / 2; 1 / 2, 1 / 3].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![1, 1 / 2; 1 / 2, 1 / 3].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [1, 1/12], all positive

Applications (2)

A proof can take the printed theorem as a hypothesis and use Mathlib’s consequences of positive definiteness, here Matrix.PosDef.det_pos:

In[7]:=
theorem posDef_det_pos (h : (!![2, 1; 1, 2] : Matrix (Fin 2) (Fin 2) ℝ).PosDef) :
    0 < (!![2, 1; 1, 2] : Matrix (Fin 2) (Fin 2) ℝ).det :=
  h.det_pos

#print axioms posDef_det_pos
Out[7]=

posDef_det_pos depends on the axioms propext, Classical.choice, Quot.sound.


The quadratic form f(x,y,z)=2x2+32y2+z2+xy+yz has this Hessian. It is positive definite, so f is positive away from 0 and has a strict minimum there:

In[8]:=
#cas [positiveDefinite] !![4, 1, 0; 1, 3, 1; 0, 1, 2]
Out[8]=

Boolean

True
Domain
3 × 3 real matrix
Input (matrix)
!![4, 1, 0; 1, 3, 1; 0, 1, 2]

Checked by Lean’s kernel: The matrix is positive definite

!![4, 1, 0; 1, 3, 1; 0, 1, 2].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![4, 1, 0; 1, 3, 1; 0, 1, 2].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [4, 11/4, 18/11], all positive

Properties and Relations (2)

A symmetric matrix is positive definite exactly when its smallest eigenvalue is positive. For the Hessian above it is 3−3≈1.268, and eigenvalueBounds proves the lower bound 1.267949:

In[9]:=
#cas [eigenvalueBounds] !![4, 1, 0; 1, 3, 1; 0, 1, 2]
Out[9]=

Rayleigh bounds

1.267949≤λmin,λmax≤4.732051
1.267949 ≤ λ_min, λ_max ≤ 4.732051
Domain
3 × 3 symmetric real matrix
Input (matrix)
!![4, 1, 0; 1, 3, 1; 0, 1, 2]

Checked by Lean’s kernel: Rayleigh bounds, sharp to 10^-6

(∀ (x : Fin 3 → ℝ),
    1.267949 * ∑ i, x i ^ 2 ≤ ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j ∧
      ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j ≤ 4.732051 * ∑ i, x i ^ 2) ∧
  (∃ x, ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j < 1.267950 * ∑ i, x i ^ 2) ∧
    ∃ x, 4.732050 * ∑ i, x i ^ 2 < ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j
The certificate: 1 checked step
  1. Rayleigh bounds, sharp to 10^-6

    (∀ (x : Fin 3 → ℝ),
        1.267949 * ∑ i, x i ^ 2 ≤ ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j ∧
          ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j ≤ 4.732051 * ∑ i, x i ^ 2) ∧
      (∃ x, ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j < 1.267950 * ∑ i, x i ^ 2) ∧
        ∃ x, 4.732050 * ∑ i, x i ^ 2 < ∑ i, ∑ j, !![4, 1, 0; 1, 3, 1; 0, 1, 2] i j * x i * x j

    Checked by Lean’s kernel

The computation
  • Certificate

    A - cI = L D Lᵀ and μI - A = L' D' L'ᵀ over ℚ, with nonnegative pivots
    x, y over ℚ with xᵀAx < (c + 10^-6) |x|² and yᵀAy > (μ - 10^-6) |y|²

The pivots are the ratios of the leading principal minors, so they are all positive exactly when the minors are (Sylvester’s criterion). For the Hessian the pivots are 4, 114 and 1811, and the minors are 4, 11 and the determinant 18:

In[10]:=
#cas [det] !![4, 1, 0; 1, 3, 1; 0, 1, 2]
Out[10]=

determinant

18
Domain
ℚ^(3×3)
Input (matrix)
!![4, 1, 0; 1, 3, 1; 0, 1, 2]

Checked by Lean’s kernel: Determinant

!![4, 1, 0; 1, 3, 1; 0, 1, 2].det = 18
The certificate: 1 checked step
  1. Determinant

    !![4, 1, 0; 1, 3, 1; 0, 1, 2].det = 18

    Checked by Lean’s kernel

Possible Issues (3)

A positive semidefinite matrix that is singular is not positive definite. The vector x=(−1,1) has Ax=0:

In[11]:=
#cas [positiveDefinite] !![1, 1; 1, 1]
Out[11]=

Boolean

False (xᵀAx ≤ 0 for x = [-1, 1])
Domain
2 × 2 real matrix
Input (matrix)
!![1, 1; 1, 1]

Checked by Lean’s kernel: A vector with a nonpositive quadratic form

¬!![1, 1; 1, 1].PosDef
The certificate: 1 checked step
  1. A vector with a nonpositive quadratic form

    ¬!![1, 1; 1, 1].PosDef

    Checked by Lean’s kernel


The quadratic form of the first matrix is x12+x22, positive for every x≠0, but the matrix is not symmetric, so it is not PosDef. Its symmetric part 12(A+AT) has the same quadratic form, and here it is the identity:

In[12]:=
#cas [positiveDefinite] !![1, 1; -1, 1]
Out[12]=

Boolean

False (not symmetric)
Domain
2 × 2 real matrix
Input (matrix)
!![1, 1; -1, 1]

Checked by Lean’s kernel: The matrix is not Hermitian

¬!![1, 1; -1, 1].PosDef
The certificate: 1 checked step
  1. The matrix is not Hermitian

    ¬!![1, 1; -1, 1].PosDef

    Checked by Lean’s kernel

In[13]:=
#cas [positiveDefinite] !![1, 0; 0, 1]
Out[13]=

Boolean

True
Domain
2 × 2 real matrix
Input (matrix)
!![1, 0; 0, 1]

Checked by Lean’s kernel: The matrix is positive definite

!![1, 0; 0, 1].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![1, 0; 0, 1].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [1, 1], all positive

The matrix has to be square:

In[14]:=
#cas [positiveDefinite] !![1, 2, 3; 4, 5, 6]
Out[14]=

Error

positiveDefinite: the matrix must be square

Neat Examples (1)

The 10×10 Hilbert matrix is the Gram matrix of 1,x,…,x9 on [0,1], so it is positive definite, and it is close to singular. Its last pivot is about 2.2·10−11, and the exact elimination proves it positive all the same:

In[15]:=
#cas [positiveDefinite] !![1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10;
  1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11;
  1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12;
  1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13;
  1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14;
  1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15;
  1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16;
  1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17;
  1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18;
  1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19]
Out[15]=

Boolean

True
Domain
10 × 10 real matrix
Input (matrix)
!![1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10; 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11; 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12; 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13; 1/5, 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14; 1/6, 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15; 1/7, 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16; 1/8, 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17; 1/9, 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18; 1/10, 1/11, 1/12, 1/13, 1/14, 1/15, 1/16, 1/17, 1/18, 1/19]

Checked by Lean’s kernel: The matrix is positive definite

!![1, 1 / 2, 1 / 3, 1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10;
  1 / 2, 1 / 3, 1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11;
  1 / 3, 1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12;
  1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13;
  1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14;
  1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15;
  1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16;
  1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16, 1 / 17;
  1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16, 1 / 17, 1 / 18;
  1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16, 1 / 17, 1 / 18, 1 / 19].PosDef
The certificate: 1 checked step
  1. The matrix is positive definite

    !![1, 1 / 2, 1 / 3, 1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10;
      1 / 2, 1 / 3, 1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11;
      1 / 3, 1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12;
      1 / 4, 1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13;
      1 / 5, 1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14;
      1 / 6, 1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15;
      1 / 7, 1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16;
      1 / 8, 1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16, 1 / 17;
      1 / 9, 1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16, 1 / 17, 1 / 18;
      1 / 10, 1 / 11, 1 / 12, 1 / 13, 1 / 14, 1 / 15, 1 / 16, 1 / 17, 1 / 18, 1 / 19].PosDef

    Checked by Lean’s kernel

The computation
  • Certificate

    A = L D Lᵀ over ℚ with pivots D = [1, 1/12, 1/180, 1/2800, 1/44100, 1/698544, 1/11099088, 1/176679360, 1/2815827300, 1/44914183600], all positive

See Also

eigenvalueBounds · eigenvalues · transpose · det