Mode of #cas

dot

The product of two matrices, or of a matrix and a vector, with rational entries and a theorem that it is right.

#cas [dot] (A, B)
gives the product of the matrices A and B, and proves A * B = C.
#cas [dot] (A, v)
gives the product of the matrix A and the vector v, and proves A.mulVec v = w.

dot is the matrix product, like Mathematica’s Dot. I use it to check things: that a vector is an eigenvector, that a proposed inverse is one, that a solution solves the system. The theorem is an equation between Mathlib matrices or vectors over ℚ, which the kernel decides by evaluating both sides.

Details and Options

  • A is a matrix literal !![a, b; c, d] or a list of rows [[a, b], [c, d]]. The second factor is a matrix in the same forms, or a vector ![a, b] or [a, b].
  • The second factor has as many rows, or entries, as A has columns. The entries are closed rational expressions such as -3/2, 0.25 or 1/10^15.
  • For a matrix the theorem is A * B = C, and for a vector it is A.mulVec v = w, Mathlib’s product of a matrix and a vector. The kernel decides it with decide +kernel.
  • The tactic cas proves the same equations in a proof.
  • There are no options.

Examples (14)#

Basic Examples (2)

The product of two matrices. The swap matrix on the right swaps the columns:

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

product

(2143)
!![2, 1; 4, 3]
Domain
ℚ^(2×2)
Input (product)
!![1, 2; 3, 4] . !![0, 1; 1, 0]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2; 3, 4] * !![0, 1; 1, 0] = !![2, 1; 4, 3]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 2; 3, 4] * !![0, 1; 1, 0] = !![2, 1; 4, 3]

    Checked by Lean’s kernel


A matrix times a vector. With all ones, the product is the vector of row sums:

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

product

(615)
![6, 15]
Domain
ℚ^(2×3)
Input (product)
!![1, 2, 3; 4, 5, 6] . ![1, 1, 1]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2, 3; 4, 5, 6].mulVec ![1, 1, 1] = ![6, 15]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 2, 3; 4, 5, 6].mulVec ![1, 1, 1] = ![6, 15]

    Checked by Lean’s kernel

Scope (3)

Shapes 2×3 and 3×2 give a 2×2 product:

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

product

(451011)
!![4, 5; 10, 11]
Domain
ℚ^(2×3)
Input (product)
!![1, 2, 3; 4, 5, 6] . !![1, 0; 0, 1; 1, 1]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2, 3; 4, 5, 6] * !![1, 0; 0, 1; 1, 1] = !![4, 5; 10, 11]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 2, 3; 4, 5, 6] * !![1, 0; 0, 1; 1, 1] = !![4, 5; 10, 11]

    Checked by Lean’s kernel


For the dot product of two vectors, write the first one as a row:

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

product

(11)
![11]
Domain
ℚ^(1×2)
Input (product)
!![1, 2] . ![3, 4]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2].mulVec ![3, 4] = ![11]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 2].mulVec ![3, 4] = ![11]

    Checked by Lean’s kernel


A vector can also be a list:

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

product

(37)
![3, 7]
Domain
ℚ^(2×2)
Input (product)
!![1, 2; 3, 4] . ![1, 1]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2; 3, 4].mulVec ![1, 1] = ![3, 7]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 2; 3, 4].mulVec ![1, 1] = ![3, 7]

    Checked by Lean’s kernel

Applications (3)

The product as a theorem. Only the three standard axioms are used:

In[6]:=
theorem dot_swap : !![(1 : ℚ), 2; 3, 4] * !![0, 1; 1, 0] = !![2, 1; 4, 3] := by
  cas

#print axioms dot_swap
Out[6]=

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


A check that v=(1,1) is an eigenvector with eigenvalue 5, as eigenvectors says:

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

product

(55)
![5, 5]
Domain
ℚ^(2×2)
Input (product)
!![4, 1; 2, 3] . ![1, 1]

Checked by Lean’s kernel: Decided by kernel evaluation

!![4, 1; 2, 3].mulVec ![1, 1] = ![5, 5]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![4, 1; 2, 3].mulVec ![1, 1] = ![5, 5]

    Checked by Lean’s kernel


A check of the inverse that inverse prints. The product is the identity:

In[8]:=
#cas [dot] (!![1, 2; 3, 4], !![-2, 1; 3/2, -1/2])
Out[8]=

product

(1001)
!![1, 0; 0, 1]
Domain
ℚ^(2×2)
Input (product)
!![1, 2; 3, 4] . !![-2, 1; 3/2, -1/2]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 2; 3, 4] * !![-2, 1; 3 / 2, -(1 / 2)] = !![1, 0; 0, 1]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 2; 3, 4] * !![-2, 1; 3 / 2, -(1 / 2)] = !![1, 0; 0, 1]

    Checked by Lean’s kernel

Properties and Relations (2)

The product depends on the order. On the left, the swap matrix swaps the rows:

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

product

(3412)
!![3, 4; 1, 2]
Domain
ℚ^(2×2)
Input (product)
!![0, 1; 1, 0] . !![1, 2; 3, 4]

Checked by Lean’s kernel: Decided by kernel evaluation

!![0, 1; 1, 0] * !![1, 2; 3, 4] = !![3, 4; 1, 2]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![0, 1; 1, 0] * !![1, 2; 3, 4] = !![3, 4; 1, 2]

    Checked by Lean’s kernel


A matrix times itself is its square, which matrixPower computes too:

In[10]:=
#cas [dot] (!![1, 1; 1, 0], !![1, 1; 1, 0])
Out[10]=

product

(2111)
!![2, 1; 1, 1]
Domain
ℚ^(2×2)
Input (product)
!![1, 1; 1, 0] . !![1, 1; 1, 0]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 1; 1, 0] * !![1, 1; 1, 0] = !![2, 1; 1, 1]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 1; 1, 0] * !![1, 1; 1, 0] = !![2, 1; 1, 1]

    Checked by Lean’s kernel

In[11]:=
#cas [matrixPower 2] !![1, 1; 1, 0]
Out[11]=

power

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

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 1; 1, 0] ^ 2 = !![2, 1; 1, 1]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 1; 1, 0] ^ 2 = !![2, 1; 1, 1]

    Checked by Lean’s kernel

Possible Issues (3)

The sizes have to match:

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

Error

dot: the sizes do not match

The first factor is a matrix. For two vectors, write the first one as a row, as in the Scope example:

In[13]:=
#cas [dot] (![1, 2], ![3, 4])
Out[13]=

Error

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

dot takes a pair:

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

Error

dot: expected a pair `(A, B)`

Neat Examples (1)

The rows of a Hadamard matrix are orthogonal, so HHT=4I. This H is symmetric, so HT=H:

In[15]:=
#cas [dot] (!![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1], !![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1])
Out[15]=

product

(4000040000400004)
!![4, 0, 0, 0; 0, 4, 0, 0; 0, 0, 4, 0; 0, 0, 0, 4]
Domain
ℚ^(4×4)
Input (product)
!![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1] . !![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1]

Checked by Lean’s kernel: Decided by kernel evaluation

!![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1] *
    !![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1] =
  !![4, 0, 0, 0; 0, 4, 0, 0; 0, 0, 4, 0; 0, 0, 0, 4]
The certificate: 1 checked step
  1. Decided by kernel evaluation

    !![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1] *
        !![1, 1, 1, 1; 1, -1, 1, -1; 1, 1, -1, -1; 1, -1, -1, 1] =
      !![4, 0, 0, 0; 0, 4, 0, 0; 0, 0, 4, 0; 0, 0, 0, 4]

    Checked by Lean’s kernel

See Also

matrixPower · transpose · linearSolve · inverse · eigenvectors