Mode of #cas
linearSolve
All solutions of a linear system with rational entries, with a theorem that there are no others.
#cas [linearSolve] (A, b)linearSolve takes a linear system in matrix form and describes every rational solution of : 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.
Examples (12)#
Basic Examples (2)
The system , has one solution:
#cas [linearSolve] (!![1, 2; 3, 4], ![5, 6])
all solutions
![-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
Solution set
∀ (x : Fin 2 → ℚ), !![1, 2; 3, 4].mulVec x = ![5, 6] ↔ x = ![-4, 9 / 2]
Here the second equation is twice the first, and the solutions form a plane: one solution plus the span of two vectors:
#cas [linearSolve] (!![1, 2, 3; 2, 4, 6], ![1, 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
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
Scope (3)
With another right side the two equations contradict each other, and the theorem says that no solves them:
#cas [linearSolve] (!![1, 2, 3; 2, 4, 6], ![1, 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
Solution set
¬∃ x, !![1, 2, 3; 2, 4, 6].mulVec x = ![1, 3]
A square system with a line of solutions. The third equation is the sum of the first two:
#cas [linearSolve] (!![1, 1, 1; 1, -1, 2; 2, 0, 3], ![6, 5, 11])
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
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
Fractions in the matrix, and the right side as a list:
#cas [linearSolve] (!![1/2, 1/3; 1/4, 1/5], [1, 1])
all solutions
![-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
Solution set
∀ (x : Fin 2 → ℚ), !![1 / 2, 1 / 3; 1 / 4, 1 / 5].mulVec x = ![1, 1] ↔ x = ![-8, 15]
Applications (2)
A solution as a theorem. Only the three standard axioms are used:
theorem linearSolve_check : !![(1 : ℚ), 2; 3, 4].mulVec ![-4, 9 / 2] = ![5, 6] := by cas #print axioms linearSolve_check
linearSolve_check depends on the axioms propext, Classical.choice, Quot.sound.
The numbers , in solve and , the coefficients of . apart finds the same decomposition:
#cas [linearSolve] (!![1, 1; 1, -1], ![0, 1])
all solutions
![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
Solution set
∀ (x : Fin 2 → ℚ), !![1, 1; 1, -1].mulVec x = ![0, 1] ↔ x = ![1 / 2, -(1 / 2)]
#cas [apart] (1/((x - 1)*(x + 1)))
partial fractions
1/(2*(x - 1)) - 1/(2*(x + 1))- Input (rational function)
(1/((x - 1)*(x + 1)))- Conditions
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
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))
Properties and Relations (2)
The span in the second example is the null space of the matrix:
#cas [nullSpace] !![1, 2, 3; 2, 4, 6]
basis of the null space, dimension 2
[![-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
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
solve takes a system in named variables, and proves its answer for every real assignment:
#cas [solve x, y] (2*x + y = 1 ∧ x - y = 2)
unique solution
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
Equivalence with the solved form for every real assignment
∀ (x y : ℝ), 2 * x + y = 1 ∧ x - y = 2 ↔ x = 1 ∧ y = -1
#cas [linearSolve] (!![2, 1; 1, -1], ![1, 2])
all solutions
![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
Solution set
∀ (x : Fin 2 → ℚ), !![2, 1; 1, -1].mulVec x = ![1, 2] ↔ x = ![1, -1]
Possible Issues (2)
The right side needs one entry for each row of the matrix:
#cas [linearSolve] (!![1, 2; 3, 4], ![5, 6, 7])
Error
linearSolve: the right side has 3 entries, the matrix 2 rows
The mode takes the pair (A, b). For , nullSpace takes the matrix alone:
#cas [linearSolve] !![1, 2; 3, 4]
Error
linearSolve: expected a pair `(A, b)`
Neat Examples (1)
The sum is a cubic . Its values , , at give the coefficients of :
#cas [linearSolve] (!![1, 1, 1; 2, 4, 8; 3, 9, 27], ![1, 5, 14])
all solutions
![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
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]
