Mode of #cas

nullSpace

A basis of the null space of a matrix with rational entries, with a theorem that it spans every solution.

#cas [nullSpace] A
gives a basis of the null space of the matrix A with rational entries, the vectors x with Ax=0, and proves that it is a basis.

nullSpace gives a basis for the solutions of Ax=0, and applied to A−λI it gives an eigenspace. The theorem says three things about the printed vectors Vi, over ℚ: each one solves Ax=0, they are linearly independent, and every solution is a combination of them. You could check the first by multiplying. The other two are what make the list a basis and not only a list of solutions, and they are the ones I want a proof of.

Details and Options

  • A is a matrix literal !![a, b; c, d] or a list of rows [[a, b], [c, d]] of any shape. The entries are closed rational expressions such as -3/2, 0.25 or 1/10^15.
  • The theorem is A.mulVec (V i) = 0 for each i, LinearIndependent ℚ V, and ∀ x, A.mulVec x = 0 → ∃ c, x = ∑ i, c i • V i. When only the zero vector solves the equation, the output is [] (only the zero vector) and the theorem is ∀ x, A.mulVec x = 0 → x = 0.
  • The vectors come from the free columns of the reduced row echelon form: each has a 1 at its own free column and 0 at the other free ones. The printed dimension is the number of columns minus the rank.
  • linarith proves the statement about every x from the coordinates of A.mulVec x, and the kernel evaluates the rest.
  • The tactic cas proves goals A.mulVec v = 0 for literals over ℚ.
  • There are no options.

Examples (12)#

Basic Examples (2)

The two rows say the same thing, x+2y+3z=0, so the solutions form a plane:

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

basis of the null space, dimension 2

[(−210), (−301)]
[![-2, 1, 0], ![-3, 0, 1]]
Domain
ℚ^(2×3)
Input (matrix)
!![1, 2, 3; 2, 4, 6]

Checked by Lean’s kernel: Basis of the null space

(∀ (i : Fin (Nat.succ 1)), !![1, 2, 3; 2, 4, 6].mulVec (![![-2, 1, 0], ![-3, 0, 1]] i) = 0) ∧
  LinearIndependent ℚ ![![-2, 1, 0], ![-3, 0, 1]] ∧
    ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 2, 4, 6].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-2, 1, 0], ![-3, 0, 1]] i
The certificate: 1 checked step
  1. Basis of the null space

    (∀ (i : Fin (Nat.succ 1)), !![1, 2, 3; 2, 4, 6].mulVec (![![-2, 1, 0], ![-3, 0, 1]] i) = 0) ∧
      LinearIndependent ℚ ![![-2, 1, 0], ![-3, 0, 1]] ∧
        ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 2, 4, 6].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-2, 1, 0], ![-3, 0, 1]] i

    Checked by Lean’s kernel


Only the zero vector solves Ax=0 for an invertible A:

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

basis of the null space, dimension 0

[] (only the zero vector)
Domain
ℚ^(2×2)
Input (matrix)
!![1, 2; 3, 4]

Checked by Lean’s kernel: Basis of the null space

∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = 0 → x = 0
The certificate: 1 checked step
  1. Basis of the null space

    ∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = 0 → x = 0

    Checked by Lean’s kernel

Scope (3)

A single nonzero column:

In[3]:=
#cas [nullSpace] !![1; 2; 3]
Out[3]=

basis of the null space, dimension 0

[] (only the zero vector)
Domain
ℚ^(3×1)
Input (matrix)
!![1; 2; 3]

Checked by Lean’s kernel: Basis of the null space

∀ (x : Fin 1 → ℚ), !![1; 2; 3].mulVec x = 0 → x = 0
The certificate: 1 checked step
  1. Basis of the null space

    ∀ (x : Fin 1 → ℚ), !![1; 2; 3].mulVec x = 0 → x = 0

    Checked by Lean’s kernel


A singular square matrix has a null space of dimension at least 1:

In[4]:=
#cas [nullSpace] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
Out[4]=

basis of the null space, dimension 1

[(1−21)]
[![1, -2, 1]]
Domain
ℚ^(3×3)
Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 9]

Checked by Lean’s kernel: Basis of the null space

(∀ (i : Fin (Nat.succ 0)), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec (![![1, -2, 1]] i) = 0) ∧
  LinearIndependent ℚ ![![1, -2, 1]] ∧
    ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1, -2, 1]] i
The certificate: 1 checked step
  1. Basis of the null space

    (∀ (i : Fin (Nat.succ 0)), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec (![![1, -2, 1]] i) = 0) ∧
      LinearIndependent ℚ ![![1, -2, 1]] ∧
        ∀ (x : Fin 3 → ℚ), !![1, 2, 3; 4, 5, 6; 7, 8, 9].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1, -2, 1]] i

    Checked by Lean’s kernel


A row with fractions, written as a list of rows:

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

basis of the null space, dimension 2

[(−2310), (−1301)]
[![-2/3, 1, 0], ![-1/3, 0, 1]]
Domain
ℚ^(1×3)
Input (matrix)
!![1/2, 1/3, 1/6]

Checked by Lean’s kernel: Basis of the null space

(∀ (i : Fin (Nat.succ 1)),
    !![1 / 2, 1 / 3, 1 / 6].mulVec (![![-(2 / 3), 1, 0], ![-(1 / 3), 0, 1]] i) = 0) ∧
  LinearIndependent ℚ ![![-(2 / 3), 1, 0], ![-(1 / 3), 0, 1]] ∧
    ∀ (x : Fin 3 → ℚ),
      !![1 / 2, 1 / 3, 1 / 6].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-(2 / 3), 1, 0], ![-(1 / 3), 0, 1]] i
The certificate: 1 checked step
  1. Basis of the null space

    (∀ (i : Fin (Nat.succ 1)),
        !![1 / 2, 1 / 3, 1 / 6].mulVec (![![-(2 / 3), 1, 0], ![-(1 / 3), 0, 1]] i) = 0) ∧
      LinearIndependent ℚ ![![-(2 / 3), 1, 0], ![-(1 / 3), 0, 1]] ∧
        ∀ (x : Fin 3 → ℚ),
          !![1 / 2, 1 / 3, 1 / 6].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-(2 / 3), 1, 0], ![-(1 / 3), 0, 1]] i

    Checked by Lean’s kernel

Applications (3)

A vector of the null space as a theorem. Only the three standard axioms are used:

In[6]:=
theorem nullSpace_vector : !![(1 : ℚ), 2, 3; 2, 4, 6].mulVec ![-2, 1, 0] = 0 := by
  cas

#print axioms nullSpace_vector
Out[6]=

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


The eigenvectors of (4123) for the eigenvalue 2 form the null space of A−2I. eigenvectors gives the same vector:

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

basis of the null space, dimension 1

[(−121)]
[![-1/2, 1]]
Domain
ℚ^(2×2)
Input (matrix)
!![2, 1; 2, 1]

Checked by Lean’s kernel: Basis of the null space

(∀ (i : Fin (Nat.succ 0)), !![2, 1; 2, 1].mulVec (![![-(1 / 2), 1]] i) = 0) ∧
  LinearIndependent ℚ ![![-(1 / 2), 1]] ∧
    ∀ (x : Fin 2 → ℚ), !![2, 1; 2, 1].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-(1 / 2), 1]] i
The certificate: 1 checked step
  1. Basis of the null space

    (∀ (i : Fin (Nat.succ 0)), !![2, 1; 2, 1].mulVec (![![-(1 / 2), 1]] i) = 0) ∧
      LinearIndependent ℚ ![![-(1 / 2), 1]] ∧
        ∀ (x : Fin 2 → ℚ), !![2, 1; 2, 1].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-(1 / 2), 1]] i

    Checked by Lean’s kernel

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

eigenvalue ↦ basis of its eigenspace

[2↦[(−121)], 5↦[(11)]]
[2 ↦ [![-1/2, 1]], 5 ↦ [![1, 1]]]
Domain
ℚ^(2×2)
Input (matrix)
!![4, 1; 2, 3]

Checked by Lean’s kernel: Characteristic polynomial factored over ℚ, eigenspaces

((∀ (i : Fin (Nat.succ 0)), (!![4, 1; 2, 3] - 2 • 1).mulVec (![![-(1 / 2), 1]] i) = 0) ∧
    LinearIndependent ℚ ![![-(1 / 2), 1]] ∧
      ∀ (x : Fin 2 → ℚ), (!![4, 1; 2, 3] - 2 • 1).mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-(1 / 2), 1]] i) ∧
  ((∀ (i : Fin (Nat.succ 0)), (!![4, 1; 2, 3] - 5 • 1).mulVec (![![1, 1]] i) = 0) ∧
      LinearIndependent ℚ ![![1, 1]] ∧
        ∀ (x : Fin 2 → ℚ), (!![4, 1; 2, 3] - 5 • 1).mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1, 1]] i) ∧
    !![4, 1; 2, 3].charpoly = (Polynomial.X - 2) * (Polynomial.X - 5)
The certificate: 1 checked step
  1. Characteristic polynomial factored over ℚ, eigenspaces

    ((∀ (i : Fin (Nat.succ 0)), (!![4, 1; 2, 3] - 2 • 1).mulVec (![![-(1 / 2), 1]] i) = 0) ∧
        LinearIndependent ℚ ![![-(1 / 2), 1]] ∧
          ∀ (x : Fin 2 → ℚ), (!![4, 1; 2, 3] - 2 • 1).mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-(1 / 2), 1]] i) ∧
      ((∀ (i : Fin (Nat.succ 0)), (!![4, 1; 2, 3] - 5 • 1).mulVec (![![1, 1]] i) = 0) ∧
          LinearIndependent ℚ ![![1, 1]] ∧
            ∀ (x : Fin 2 → ℚ), (!![4, 1; 2, 3] - 5 • 1).mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1, 1]] i) ∧
        !![4, 1; 2, 3].charpoly = (Polynomial.X - 2) * (Polynomial.X - 5)

    Checked by Lean’s kernel


To balance aCH4+bO2→cCO2+dH2O, I count each element on both sides: carbon gives a=c, hydrogen 4a=2d and oxygen 2b=2c+d. Twice the null vector is the balanced equation CH4+2O2→CO2+2H2O:

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

basis of the null space, dimension 1

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

Checked by Lean’s kernel: Basis of the null space

(∀ (i : Fin (Nat.succ 0)),
    !![1, 0, -1, 0; 4, 0, 0, -2; 0, 2, -2, -1].mulVec (![![1 / 2, 1, 1 / 2, 1]] i) = 0) ∧
  LinearIndependent ℚ ![![1 / 2, 1, 1 / 2, 1]] ∧
    ∀ (x : Fin 4 → ℚ),
      !![1, 0, -1, 0; 4, 0, 0, -2; 0, 2, -2, -1].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1 / 2, 1, 1 / 2, 1]] i
The certificate: 1 checked step
  1. Basis of the null space

    (∀ (i : Fin (Nat.succ 0)),
        !![1, 0, -1, 0; 4, 0, 0, -2; 0, 2, -2, -1].mulVec (![![1 / 2, 1, 1 / 2, 1]] i) = 0) ∧
      LinearIndependent ℚ ![![1 / 2, 1, 1 / 2, 1]] ∧
        ∀ (x : Fin 4 → ℚ),
          !![1, 0, -1, 0; 4, 0, 0, -2; 0, 2, -2, -1].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![1 / 2, 1, 1 / 2, 1]] i

    Checked by Lean’s kernel

Properties and Relations (2)

The dimension is the number of columns minus the rank. For the singular matrix in Scope it is 3−2=1:

In[10]:=
#cas [matrixRank] !![1, 2, 3; 4, 5, 6; 7, 8, 9]
Out[10]=

rank

2
Domain
ℚ^(3×3)
Input (matrix)
!![1, 2, 3; 4, 5, 6; 7, 8, 9]

Checked by Lean’s kernel: Rank from a factorization and a nonsingular minor

!![1, 2, 3; 4, 5, 6; 7, 8, 9].rank = 2
The certificate: 1 checked step
  1. Rank from a factorization and a nonsingular minor

    !![1, 2, 3; 4, 5, 6; 7, 8, 9].rank = 2

    Checked by Lean’s kernel


Every solution of Ax=b is one solution plus a vector of the null space. linearSolve prints the same basis:

In[11]:=
#cas [linearSolve] (!![1, 2, 3; 2, 4, 6], ![1, 2])
Out[11]=

all solutions

![1, 0, 0] + span of [![-2, 1, 0], ![-3, 0, 1]]
Domain
ℚ^(2×3)
Input (linear system)
!![1, 2, 3; 2, 4, 6] . x = ![1, 2]

Checked by Lean’s kernel: Solution set

!![1, 2, 3; 2, 4, 6].mulVec ![1, 0, 0] = ![1, 2] ∧
  LinearIndependent ℚ ![![-2, 1, 0], ![-3, 0, 1]] ∧
    ∀ (x : Fin 3 → ℚ),
      !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 2] ↔ ∃ c, x = ![1, 0, 0] + ∑ i, c i • ![![-2, 1, 0], ![-3, 0, 1]] i
The certificate: 1 checked step
  1. Solution set

    !![1, 2, 3; 2, 4, 6].mulVec ![1, 0, 0] = ![1, 2] ∧
      LinearIndependent ℚ ![![-2, 1, 0], ![-3, 0, 1]] ∧
        ∀ (x : Fin 3 → ℚ),
          !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 2] ↔ ∃ c, x = ![1, 0, 0] + ∑ i, c i • ![![-2, 1, 0], ![-3, 0, 1]] i

    Checked by Lean’s kernel

Possible Issues (1)

The mode takes the matrix alone. For a right side, use linearSolve:

In[12]:=
#cas [nullSpace] (!![1, 2; 3, 4], ![0, 0])
Out[12]=

Error

expected a matrix `!![a, b; c, d]` or `[[a, b], [c, d]]`

Neat Examples (1)

The rows hold the values of 1, n and n2 at n=1,2,3,4. The null space is spanned by the third difference (−1,3,−3,1), so −p(1)+3p(2)−3p(3)+p(4)=0 for every polynomial p of degree at most 2:

In[13]:=
#cas [nullSpace] !![1, 1, 1, 1; 1, 2, 3, 4; 1, 4, 9, 16]
Out[13]=

basis of the null space, dimension 1

[(−13−31)]
[![-1, 3, -3, 1]]
Domain
ℚ^(3×4)
Input (matrix)
!![1, 1, 1, 1; 1, 2, 3, 4; 1, 4, 9, 16]

Checked by Lean’s kernel: Basis of the null space

(∀ (i : Fin (Nat.succ 0)),
    !![1, 1, 1, 1; 1, 2, 3, 4; 1, 4, 9, 16].mulVec (![![-1, 3, -3, 1]] i) = 0) ∧
  LinearIndependent ℚ ![![-1, 3, -3, 1]] ∧
    ∀ (x : Fin 4 → ℚ),
      !![1, 1, 1, 1; 1, 2, 3, 4; 1, 4, 9, 16].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-1, 3, -3, 1]] i
The certificate: 1 checked step
  1. Basis of the null space

    (∀ (i : Fin (Nat.succ 0)),
        !![1, 1, 1, 1; 1, 2, 3, 4; 1, 4, 9, 16].mulVec (![![-1, 3, -3, 1]] i) = 0) ∧
      LinearIndependent ℚ ![![-1, 3, -3, 1]] ∧
        ∀ (x : Fin 4 → ℚ),
          !![1, 1, 1, 1; 1, 2, 3, 4; 1, 4, 9, 16].mulVec x = 0 → ∃ c, x = ∑ i, c i • ![![-1, 3, -3, 1]] i

    Checked by Lean’s kernel

See Also

linearSolve · matrixRank · rowReduce · eigenvectors