Mode of #cas

rowReduce

The reduced row echelon form of a matrix with rational entries, with the row operations that prove it.

#cas [rowReduce] A
gives the reduced row echelon form R of the matrix A with rational entries and its pivot columns, and proves E * A = R ∧ E.det ≠ 0 ∧ R.IsReducedRowEchelon.

I like row reduction because the reduced form shows so much at once: the pivots, the free columns and the relations between the columns. rowReduce gives you that form, with a theorem about Mathlib’s Matrix.IsReducedRowEchelon. The theorem gives an invertible matrix E of row operations with EA=R, and since the reduced form of a matrix is unique, that pins R down.

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 E * A = R ∧ E.det ≠ 0 ∧ R.IsReducedRowEchelon over ℚ, where E records the row operations of an exact Gauss-Jordan elimination. The kernel evaluates the product and the conditions of the echelon form, and norm_det proves E.det ≠ 0.
  • The report also prints the pivot columns, numbered from 0. Their number is the rank.
  • The mode takes one matrix. For a system Ax=b, reduce the augmented matrix, or solve it with linearSolve.
  • There are no options.

Examples (11)#

Basic Examples (2)

The reduced form of a singular 3×3 matrix. The pivots are in the first two columns, and the last row is zero:

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

reduced row echelon form, pivot columns [0, 1]

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

Checked by Lean’s kernel: Row operations E with E * A = R

!![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1] * !![1, 2, 3; 4, 5, 6; 7, 8, 9] =
    !![1, 0, -1; 0, 1, 2; 0, 0, 0] ∧
  !![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1].det ≠ 0 ∧
    !![1, 0, -1; 0, 1, 2; 0, 0, 0].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1] * !![1, 2, 3; 4, 5, 6; 7, 8, 9] =
        !![1, 0, -1; 0, 1, 2; 0, 0, 0] ∧
      !![-(5 / 3), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1].det ≠ 0 ∧
        !![1, 0, -1; 0, 1, 2; 0, 0, 0].IsReducedRowEchelon

    Checked by Lean’s kernel


The first entry is 0, so the elimination swaps two rows, and E records the swap:

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

reduced row echelon form, pivot columns [0, 1]

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

Checked by Lean’s kernel: Row operations E with E * A = R

!![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1] * !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3] =
    !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0] ∧
  !![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1].det ≠ 0 ∧
    !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1] * !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3] =
        !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0] ∧
      !![-(1 / 2), 1, 0; 1 / 2, 0, 0; -1, -2, 1].det ≠ 0 ∧
        !![1, 0, -1, 1 / 2; 0, 1, 2, 1 / 2; 0, 0, 0, 0].IsReducedRowEchelon

    Checked by Lean’s kernel

Scope (2)

A row whose first entry is 1 is already reduced:

In[3]:=
#cas [rowReduce] !![1, 2, 3]
Out[3]=

reduced row echelon form, pivot columns [0]

(123)
!![1, 2, 3]
Domain
ℚ^(1×3)
Input (matrix)
!![1, 2, 3]

Checked by Lean’s kernel: Row operations E with E * A = R

!![1] * !![1, 2, 3] = !![1, 2, 3] ∧ !![1].det ≠ 0 ∧ !![1, 2, 3].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![1] * !![1, 2, 3] = !![1, 2, 3] ∧ !![1].det ≠ 0 ∧ !![1, 2, 3].IsReducedRowEchelon

    Checked by Lean’s kernel


An invertible matrix reduces to the identity. A list of rows works as well as a matrix literal:

In[4]:=
#cas [rowReduce] [[2, 4], [1, 3]]
Out[4]=

reduced row echelon form, pivot columns [0, 1]

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

Checked by Lean’s kernel: Row operations E with E * A = R

!![3 / 2, -2; -(1 / 2), 1] * !![2, 4; 1, 3] = !![1, 0; 0, 1] ∧
  !![3 / 2, -2; -(1 / 2), 1].det ≠ 0 ∧ !![1, 0; 0, 1].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![3 / 2, -2; -(1 / 2), 1] * !![2, 4; 1, 3] = !![1, 0; 0, 1] ∧
      !![3 / 2, -2; -(1 / 2), 1].det ≠ 0 ∧ !![1, 0; 0, 1].IsReducedRowEchelon

    Checked by Lean’s kernel

Applications (3)

The theorem of the first report contains its matrix E. Kernel evaluation, the proof behind the report, checks the product again, and only the three standard axioms are used:

In[5]:=
theorem rowReduce_certificate :
    !![(-(5 / 3) : ℚ), 2 / 3, 0; 4 / 3, -(1 / 3), 0; 1, -2, 1] * !![1, 2, 3; 4, 5, 6; 7, 8, 9] =
      !![1, 0, -1; 0, 1, 2; 0, 0, 0] := by
  decide +kernel

#print axioms rowReduce_certificate
Out[5]=

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


The system x+2y=5, 3x+4y=6 as an augmented matrix. The last column of the reduced form is the solution x=−4, y=92, which linearSolve finds too:

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

reduced row echelon form, pivot columns [0, 1]

(10−40192)
!![1, 0, -4; 0, 1, 9/2]
Domain
ℚ^(2×3)
Input (matrix)
!![1, 2, 5; 3, 4, 6]

Checked by Lean’s kernel: Row operations E with E * A = R

!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 5; 3, 4, 6] = !![1, 0, -4; 0, 1, 9 / 2] ∧
  !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -4; 0, 1, 9 / 2].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 5; 3, 4, 6] = !![1, 0, -4; 0, 1, 9 / 2] ∧
      !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -4; 0, 1, 9 / 2].IsReducedRowEchelon

    Checked by Lean’s kernel

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

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


For an invertible A, reducing [A∣I] gives [I∣A−1]. The right half is the inverse that inverse prints:

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

reduced row echelon form, pivot columns [0, 1]

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

Checked by Lean’s kernel: Row operations E with E * A = R

!![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 1, 0; 3, 4, 0, 1] = !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)] ∧
  !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![-2, 1; 3 / 2, -(1 / 2)] * !![1, 2, 1, 0; 3, 4, 0, 1] = !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)] ∧
      !![-2, 1; 3 / 2, -(1 / 2)].det ≠ 0 ∧ !![1, 0, -2, 1; 0, 1, 3 / 2, -(1 / 2)].IsReducedRowEchelon

    Checked by Lean’s kernel

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

inverse

(−2132−12)
!![-2, 1; 3/2, -1/2]
Domain
ℚ^(2×2)
Input (matrix)
!![1, 2; 3, 4]

Checked by Lean’s kernel: Inverse from the product B * A = 1

!![1, 2; 3, 4]⁻¹ = !![-2, 1; 3 / 2, -(1 / 2)]
The certificate: 1 checked step
  1. Inverse from the product B * A = 1

    !![1, 2; 3, 4]⁻¹ = !![-2, 1; 3 / 2, -(1 / 2)]

    Checked by Lean’s kernel

Properties and Relations (2)

The number of pivot columns is the rank. The second example has two:

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

rank

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

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

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

    !![0, 2, 4, 1; 1, 1, 1, 1; 2, 4, 6, 3].rank = 2

    Checked by Lean’s kernel


Each column without a pivot gives a vector of the null space. For the first example the reduced form says x1=x3 and x2=−2x3:

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

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

Possible Issues (1)

The mode takes one matrix, not a system. Reduce the augmented matrix as above, or use linearSolve:

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

Error

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

Neat Examples (1)

Dürer’s magic square from Melencolia I is singular. The reduced form says that its last column is c1−3c2+3c3:

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

reduced row echelon form, pivot columns [0, 1, 2]

(1001010−300130000)
!![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0]
Domain
ℚ^(4×4)
Input (matrix)
!![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1]

Checked by Lean’s kernel: Row operations E with E * A = R

!![1 / 34, -(9 / 136), 13 / 136, 0;
        8 / 17, 47 / 68, -(83 / 68), 0;
        -(15 / 34), -(69 / 136), 145 / 136, 0;
        -1, -3, 3, 1] *
      !![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1] =
    !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0] ∧
  !![1 / 34, -(9 / 136), 13 / 136, 0;
          8 / 17, 47 / 68, -(83 / 68), 0;
          -(15 / 34), -(69 / 136), 145 / 136, 0;
          -1, -3, 3, 1].det ≠
      0 ∧
    !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0].IsReducedRowEchelon
The certificate: 1 checked step
  1. Row operations E with E * A = R

    !![1 / 34, -(9 / 136), 13 / 136, 0;
            8 / 17, 47 / 68, -(83 / 68), 0;
            -(15 / 34), -(69 / 136), 145 / 136, 0;
            -1, -3, 3, 1] *
          !![16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1] =
        !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0] ∧
      !![1 / 34, -(9 / 136), 13 / 136, 0;
              8 / 17, 47 / 68, -(83 / 68), 0;
              -(15 / 34), -(69 / 136), 145 / 136, 0;
              -1, -3, 3, 1].det ≠
          0 ∧
        !![1, 0, 0, 1; 0, 1, 0, -3; 0, 0, 1, 3; 0, 0, 0, 0].IsReducedRowEchelon

    Checked by Lean’s kernel

See Also

matrixRank · nullSpace · linearSolve · inverse · det