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)A * B = C.#cas [dot] (A, v)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.
Examples (14)#
Basic Examples (2)
The product of two matrices. The swap matrix on the right swaps the columns:
#cas [dot] (!![1, 2; 3, 4], !![0, 1; 1, 0])
product
!![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
Decided by kernel evaluation
!![1, 2; 3, 4] * !![0, 1; 1, 0] = !![2, 1; 4, 3]
A matrix times a vector. With all ones, the product is the vector of row sums:
#cas [dot] (!![1, 2, 3; 4, 5, 6], ![1, 1, 1])
product
![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
Decided by kernel evaluation
!![1, 2, 3; 4, 5, 6].mulVec ![1, 1, 1] = ![6, 15]
Scope (3)
Shapes and give a product:
#cas [dot] (!![1, 2, 3; 4, 5, 6], !![1, 0; 0, 1; 1, 1])
product
!![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
Decided by kernel evaluation
!![1, 2, 3; 4, 5, 6] * !![1, 0; 0, 1; 1, 1] = !![4, 5; 10, 11]
For the dot product of two vectors, write the first one as a row:
#cas [dot] (!![1, 2], ![3, 4])
product
![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
Decided by kernel evaluation
!![1, 2].mulVec ![3, 4] = ![11]
A vector can also be a list:
#cas [dot] (!![1, 2; 3, 4], [1, 1])
product
![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
Decided by kernel evaluation
!![1, 2; 3, 4].mulVec ![1, 1] = ![3, 7]
Applications (3)
The product as a theorem. Only the three standard axioms are used:
theorem dot_swap : !![(1 : ℚ), 2; 3, 4] * !![0, 1; 1, 0] = !![2, 1; 4, 3] := by cas #print axioms dot_swap
dot_swap depends on the axioms propext, Classical.choice, Quot.sound.
A check that is an eigenvector with eigenvalue , as eigenvectors says:
#cas [dot] (!![4, 1; 2, 3], ![1, 1])
product
![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
Decided by kernel evaluation
!![4, 1; 2, 3].mulVec ![1, 1] = ![5, 5]
A check of the inverse that inverse prints. The product is the identity:
#cas [dot] (!![1, 2; 3, 4], !![-2, 1; 3/2, -1/2])
product
!![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
Decided by kernel evaluation
!![1, 2; 3, 4] * !![-2, 1; 3 / 2, -(1 / 2)] = !![1, 0; 0, 1]
Properties and Relations (2)
The product depends on the order. On the left, the swap matrix swaps the rows:
#cas [dot] (!![0, 1; 1, 0], !![1, 2; 3, 4])
product
!![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
Decided by kernel evaluation
!![0, 1; 1, 0] * !![1, 2; 3, 4] = !![3, 4; 1, 2]
A matrix times itself is its square, which matrixPower computes too:
#cas [dot] (!![1, 1; 1, 0], !![1, 1; 1, 0])
product
!![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
Decided by kernel evaluation
!![1, 1; 1, 0] * !![1, 1; 1, 0] = !![2, 1; 1, 1]
#cas [matrixPower 2] !![1, 1; 1, 0]
power
!![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
Decided by kernel evaluation
!![1, 1; 1, 0] ^ 2 = !![2, 1; 1, 1]
Possible Issues (3)
The sizes have to match:
#cas [dot] (!![1, 2; 3, 4], ![1, 2, 3])
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:
#cas [dot] (![1, 2], ![3, 4])
Error
expected a matrix `!![a, b; c, d]` or `[[a, b], [c, d]]`
dot takes a pair:
#cas [dot] !![1, 2; 3, 4]
Error
dot: expected a pair `(A, B)`
Neat Examples (1)
The rows of a Hadamard matrix are orthogonal, so . This is symmetric, so :
#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])
product
!![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
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]
See Also
matrixPower · transpose · linearSolve · inverse · eigenvectors
