Mode of #cas

linearSolve

All solutions of a linear system Ax=b with rational entries, with a theorem that there are no others.

#cas [linearSolve] (A, b)
finds all solutions x of Ax=b for the matrix A and the vector b with rational entries, and proves that there are no others.

linearSolve takes a linear system in matrix form and describes every rational solution of Ax=b: a single vector, one solution plus the span of a basis of the null space, or no solution. The theorem is about Mathlib’s Matrix.mulVec over ℚ and proves that this description is complete, so it tells you what all the solutions are, not only that some vector works.

Details and Options

  • A is a matrix literal !![a, b; c, d] or a list of rows [[a, b], [c, d]] of any shape. b is a vector ![a, b] or [a, b] with one entry for each row of A. The entries are closed rational expressions such as -3/2, 0.25 or 1/10^15.
  • The theorem is one of three statements over ℚ:
    • one solution x0: ∀ x, A.mulVec x = b ↔ x = x₀;
    • one solution x0 plus the span of independent vectors Vi: A.mulVec x₀ = b ∧ LinearIndependent ℚ V ∧ ∀ x, A.mulVec x = b ↔ ∃ c, x = x₀ + ∑ i, c i • V i;
    • no solution: ¬∃ x, A.mulVec x = b.
  • The kernel decides the equations between literals, and linarith proves the statements about every x from the coordinates of A.mulVec x.
  • For equations in named variables, such as 2*x + y = 1 ∧ x - y = 2, the mode solve takes the system as you write it.
  • The tactic cas proves goals A.mulVec v = b for literals over ℚ.
  • There are no options.

Examples (12)#

Basic Examples (2)

The system x+2y=5, 3x+4y=6 has one solution:

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

all solutions

(−492)
![-4, 9/2]
Domain
ℚ^(2×2)
Input (linear system)
!![1, 2; 3, 4] . x = ![5, 6]

Checked by Lean’s kernel: Solution set

∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = ![5, 6] ↔ x = ![-4, 9 / 2]
The certificate: 1 checked step
  1. Solution set

    ∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = ![5, 6] ↔ x = ![-4, 9 / 2]

    Checked by Lean’s kernel


Here the second equation is twice the first, and the solutions form a plane: one solution plus the span of two vectors:

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

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

Scope (3)

With another right side the two equations contradict each other, and the theorem says that no x solves them:

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

all solutions

no solution
Domain
ℚ^(2×3)
Input (linear system)
!![1, 2, 3; 2, 4, 6] . x = ![1, 3]

Checked by Lean’s kernel: Solution set

¬∃ x, !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 3]
The certificate: 1 checked step
  1. Solution set

    ¬∃ x, !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 3]

    Checked by Lean’s kernel


A square system with a line of solutions. The third equation is the sum of the first two:

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

all solutions

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

Checked by Lean’s kernel: Solution set

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

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

    Checked by Lean’s kernel


Fractions in the matrix, and the right side as a list:

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

all solutions

(−815)
![-8, 15]
Domain
ℚ^(2×2)
Input (linear system)
!![1/2, 1/3; 1/4, 1/5] . x = ![1, 1]

Checked by Lean’s kernel: Solution set

∀ (x : Fin 2 → ℚ), !![1 / 2, 1 / 3; 1 / 4, 1 / 5].mulVec x = ![1, 1] ↔ x = ![-8, 15]
The certificate: 1 checked step
  1. Solution set

    ∀ (x : Fin 2 → ℚ), !![1 / 2, 1 / 3; 1 / 4, 1 / 5].mulVec x = ![1, 1] ↔ x = ![-8, 15]

    Checked by Lean’s kernel

Applications (2)

A solution as a theorem. Only the three standard axioms are used:

In[6]:=
theorem linearSolve_check : !![(1 : ℚ), 2; 3, 4].mulVec ![-4, 9 / 2] = ![5, 6] := by
  cas

#print axioms linearSolve_check
Out[6]=

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


The numbers a, b in 1(x−1)(x+1)=ax−1+bx+1 solve a+b=0 and a−b=1, the coefficients of a(x+1)+b(x−1)=1. apart finds the same decomposition:

In[7]:=
#cas [linearSolve] (!![1, 1; 1, -1], ![0, 1])
Out[7]=

all solutions

(12−12)
![1/2, -1/2]
Domain
ℚ^(2×2)
Input (linear system)
!![1, 1; 1, -1] . x = ![0, 1]

Checked by Lean’s kernel: Solution set

∀ (x : Fin 2 → ℚ), !![1, 1; 1, -1].mulVec x = ![0, 1] ↔ x = ![1 / 2, -(1 / 2)]
The certificate: 1 checked step
  1. Solution set

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

    Checked by Lean’s kernel

In[8]:=
#cas [apart] (1/((x - 1)*(x + 1)))
Out[8]=

partial fractions

12(x−1)−12(x+1)
1/(2*(x - 1)) - 1/(2*(x + 1))
Input (rational function)
(1/((x - 1)*(x + 1)))
Conditions
(x−1)(x+1)≠0

Checked by Lean’s kernel: Identity with the partial fraction decomposition

∀ (x : ℝ), (x - 1) * (x + 1) ≠ 0 → 1 / ((x - 1) * (x + 1)) = 1 / (2 * (x - 1)) - 1 / (2 * (x + 1))
The certificate: 1 checked step
  1. Identity with the partial fraction decomposition

    ∀ (x : ℝ), (x - 1) * (x + 1) ≠ 0 → 1 / ((x - 1) * (x + 1)) = 1 / (2 * (x - 1)) - 1 / (2 * (x + 1))

    Checked by Lean’s kernel

Properties and Relations (2)

The span in the second example is the null space of the matrix:

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

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


solve takes a system in named variables, and proves its answer for every real assignment:

In[10]:=
#cas [solve x, y] (2*x + y = 1 ∧ x - y = 2)
Out[10]=

unique solution

x=1∧y=−1
x = 1 ∧ y = -1
Input (linear system)
(2*x + y = 1 ∧ x - y = 2)

Checked by Lean’s kernel: Equivalence with the solved form for every real assignment

∀ (x y : ℝ), 2 * x + y = 1 ∧ x - y = 2 ↔ x = 1 ∧ y = -1
The certificate: 1 checked step
  1. Equivalence with the solved form for every real assignment

    ∀ (x y : ℝ), 2 * x + y = 1 ∧ x - y = 2 ↔ x = 1 ∧ y = -1

    Checked by Lean’s kernel

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

all solutions

(1−1)
![1, -1]
Domain
ℚ^(2×2)
Input (linear system)
!![2, 1; 1, -1] . x = ![1, 2]

Checked by Lean’s kernel: Solution set

∀ (x : Fin 2 → ℚ), !![2, 1; 1, -1].mulVec x = ![1, 2] ↔ x = ![1, -1]
The certificate: 1 checked step
  1. Solution set

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

    Checked by Lean’s kernel

Possible Issues (2)

The right side needs one entry for each row of the matrix:

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

Error

linearSolve: the right side has 3 entries, the matrix 2 rows

The mode takes the pair (A, b). For Ax=0, nullSpace takes the matrix alone:

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

Error

linearSolve: expected a pair `(A, b)`

Neat Examples (1)

The sum 12+22+…+n2 is a cubic an+bn2+cn3. Its values 1, 5, 14 at n=1,2,3 give the coefficients of n6+n22+n33:

In[14]:=
#cas [linearSolve] (!![1, 1, 1; 2, 4, 8; 3, 9, 27], ![1, 5, 14])
Out[14]=

all solutions

(161213)
![1/6, 1/2, 1/3]
Domain
ℚ^(3×3)
Input (linear system)
!![1, 1, 1; 2, 4, 8; 3, 9, 27] . x = ![1, 5, 14]

Checked by Lean’s kernel: Solution set

∀ (x : Fin 3 → ℚ),
  !![1, 1, 1; 2, 4, 8; 3, 9, 27].mulVec x = ![1, 5, 14] ↔ x = ![1 / 6, 1 / 2, 1 / 3]
The certificate: 1 checked step
  1. Solution set

    ∀ (x : Fin 3 → ℚ),
      !![1, 1, 1; 2, 4, 8; 3, 9, 27].mulVec x = ![1, 5, 14] ↔ x = ![1 / 6, 1 / 2, 1 / 3]

    Checked by Lean’s kernel

See Also

nullSpace · rowReduce · inverse · solve · dot