Matrix multiplication algorithms
DefinitionRectangular matrix accumulation#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication, and let \(A\), \(B\), \(C\) be \(p \times q\), \(q \times r\) and \(p \times r\) matrices over \(\alpha\), stored as vectors of rows. Rectangular matrix accumulation adds the product \(AB\) to the accumulator \(C\): for \(i < p\), then \(j < r\), then \(k < q\), each in increasing order, it replaces \(C_{ij}\) by \(C_{ij} + A_{ik} B_{kj}\). It returns the final matrix together with a cost pair (scalar multiplications, scalar additions), charging one of each for every such update.
public def rectangularMatrixAccumulate {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α r) p) :=
scanRows A B p 0 (by omega) (pure C)
@[no_expose] private def scanRows {α : Type u} [Add α] [Mul α] {p q r : Nat}
(A : Vector (Vector α q) p) (B : Vector (Vector α r) q) :
(fuel i : Nat) → i + fuel ≤ p →
TimeM (Nat × Nat) (Vector (Vector α r) p) →
TimeM (Nat × Nat) (Vector (Vector α r) p)
| 0, _, _, state => state
| fuel + 1, i, h, state =>
scanRows A B fuel (i + 1) (by omega) (do
let C ← state
let row ← scanJ (A.get ⟨i, by omega⟩) B r 0 (by omega)
(pure (C.get ⟨i, by omega⟩))
pure (C.set i row))
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:90
Used by 9
DefinitionSquare matrix accumulation#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication, and let \(A\), \(B\), \(C\) be \(n \times n\) matrices over \(\alpha\), stored as vectors of rows. Square matrix accumulation on \(A\), \(B\), \(C\) is rectangular matrix accumulation on \(A\), \(B\), \(C\) with all three dimensions equal to \(n\): it adds \(AB\) to the accumulator \(C\) and returns the result with its cost pair (scalar multiplications, scalar additions).
public def matrixAccumulate {α : Type u} [Add α] [Mul α] {n : Nat}
(A B C : _root_.Vector (_root_.Vector α n) n) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α n) n) :=
rectangularMatrixAccumulate A B C
public def rectangularMatrixAccumulate {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α r) p) :=
scanRows A B p 0 (by omega) (pure C)
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:279
Uses 1 · Used by 11
TheoremEntries of rectangular matrix accumulation#✓fully proved
Let \(\alpha\) be a commutative additive monoid with a multiplication, and let \(A\), \(B\), \(C\) be \(p \times q\), \(q \times r\) and \(p \times r\) matrices over \(\alpha\), stored as vectors of rows. For all \(i < p\) and \(j < r\), the \((i, j)\) entry of the matrix returned by rectangular matrix accumulation on \(A\), \(B\), \(C\) is \(C_{ij} + \sum_{k < q} A_{ik} B_{kj}\).
theorem rectangularMatrixAccumulate_apply {α : Type u} [AddCommMonoid α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p)
(i : Fin p) (j : Fin r) :
((rectangularMatrixAccumulate A B C).ret.get i).get j =
(C.get i).get j + ∑ k : Fin q, (A.get i).get k * (B.get k).get j
public def rectangularMatrixAccumulate {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α r) p) :=
scanRows A B p 0 (by omega) (pure C)
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:279
Uses 1 · Used by 2
TheoremRectangular matrix accumulation computes \(C + AB\)#✓fully proved
Let \(\alpha\) be a commutative additive monoid with a multiplication, and let \(A\), \(B\), \(C\) be \(p \times q\), \(q \times r\) and \(p \times r\) matrices over \(\alpha\), stored as vectors of rows. The matrix returned by rectangular matrix accumulation on \(A\), \(B\), \(C\) equals \(C + AB\), where \(AB\) is the matrix product, \((AB)_{ij} = \sum_{k < q} A_{ik} B_{kj}\).
theorem rectangularMatrixAccumulate_ret {α : Type u} [AddCommMonoid α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
Matrix.of (fun i j => ((rectangularMatrixAccumulate A B C).ret.get i).get j) =
Matrix.of (fun i j => (C.get i).get j) +
Matrix.of (fun i k => (A.get i).get k) * Matrix.of (fun k j => (B.get k).get j)
public def rectangularMatrixAccumulate {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α r) p) :=
scanRows A B p 0 (by omega) (pure C)
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:279
Uses 2 · Used by 1
TheoremOperation count of rectangular matrix accumulation#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication, and let \(A\), \(B\), \(C\) be \(p \times q\), \(q \times r\) and \(p \times r\) matrices over \(\alpha\). Rectangular matrix accumulation on \(A\), \(B\), \(C\) has cost \((pqr, pqr)\): it charges exactly \(pqr\) scalar multiplications and exactly \(pqr\) scalar additions.
theorem rectangularMatrixAccumulate_time {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
(rectangularMatrixAccumulate A B C).time = (p * q * r, p * q * r)
public def rectangularMatrixAccumulate {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α r) p) :=
scanRows A B p 0 (by omega) (pure C)
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:279
Uses 1 · Used by 2
TheoremRectangular matrix accumulation with zero inner dimension#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication. If \(A\) is a \(p \times 0\) matrix, \(B\) a \(0 \times r\) matrix and \(C\) a \(p \times r\) matrix over \(\alpha\), then rectangular matrix accumulation on \(A\), \(B\), \(C\) returns \(C\) unchanged at cost \((0, 0)\).
theorem rectangularMatrixAccumulate_zero_inner {α : Type u} [Add α] [Mul α]
{p r : Nat} (A : _root_.Vector (_root_.Vector α 0) p)
(B : _root_.Vector (_root_.Vector α r) 0) (C : _root_.Vector (_root_.Vector α r) p) :
rectangularMatrixAccumulate A B C = (pure C : TimeM (Nat × Nat) _)
public def rectangularMatrixAccumulate {α : Type u} [Add α] [Mul α]
{p q r : Nat} (A : _root_.Vector (_root_.Vector α q) p)
(B : _root_.Vector (_root_.Vector α r) q) (C : _root_.Vector (_root_.Vector α r) p) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α r) p) :=
scanRows A B p 0 (by omega) (pure C)
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:279
Uses 1
TheoremSquare matrix accumulation computes \(C + AB\)#✓fully proved
Let \(\alpha\) be a commutative additive monoid with a multiplication, and let \(A\), \(B\), \(C\) be \(n \times n\) matrices over \(\alpha\), stored as vectors of rows. The matrix returned by square matrix accumulation on \(A\), \(B\), \(C\) equals \(C + AB\), where \(AB\) is the matrix product, \((AB)_{ij} = \sum_{k < n} A_{ik} B_{kj}\).
theorem matrixAccumulate_ret {α : Type u} [AddCommMonoid α] [Mul α] {n : Nat}
(A B C : _root_.Vector (_root_.Vector α n) n) :
Matrix.of (fun i j => ((matrixAccumulate A B C).ret.get i).get j) =
Matrix.of (fun i j => (C.get i).get j) +
Matrix.of (fun i k => (A.get i).get k) * Matrix.of (fun k j => (B.get k).get j)
public def matrixAccumulate {α : Type u} [Add α] [Mul α] {n : Nat}
(A B C : _root_.Vector (_root_.Vector α n) n) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α n) n) :=
rectangularMatrixAccumulate A B C
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:286
Uses 2 · Used by 4
TheoremOperation count of square matrix accumulation#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication, and let \(A\), \(B\), \(C\) be \(n \times n\) matrices over \(\alpha\). Square matrix accumulation on \(A\), \(B\), \(C\) has cost \((n^3, n^3)\): it charges exactly \(n^3\) scalar multiplications and exactly \(n^3\) scalar additions.
theorem matrixAccumulate_time {α : Type u} [Add α] [Mul α] {n : Nat}
(A B C : _root_.Vector (_root_.Vector α n) n) :
(matrixAccumulate A B C).time = (n ^ 3, n ^ 3)
public def matrixAccumulate {α : Type u} [Add α] [Mul α] {n : Nat}
(A B C : _root_.Vector (_root_.Vector α n) n) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α n) n) :=
rectangularMatrixAccumulate A B C
CSLibExt/Algorithms/Lean/Matrix/Multiply.lean:286
Uses 3 · Used by 2
DefinitionRecursive matrix multiply-accumulate#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication, let \(k\) be a natural number and \(n = 2^k\), and let \(A\), \(B\), \(C\) be \(n \times n\) matrices over \(\alpha\), stored as vectors of rows. The procedure adds the product of \(A\) and \(B\) to the accumulator \(C\) by the eight-call recursion of CLRS. When \(n = 1\), writing \(a\), \(b\), \(c\) for the single entries of \(A\), \(B\), \(C\), it replaces \(c\) by \(c + ab\). Otherwise it splits each matrix \(M\) into four \((n/2) \times (n/2)\) quadrants \(M_{11}\) (top left), \(M_{12}\) (top right), \(M_{21}\) (bottom left) and \(M_{22}\) (bottom right), and updates, in this order, \(C_{11} \gets C_{11} + A_{11} B_{11}\), \(C_{12} \gets C_{12} + A_{11} B_{12}\), \(C_{21} \gets C_{21} + A_{21} B_{11}\), \(C_{22} \gets C_{22} + A_{21} B_{12}\), then \(C_{11} \gets C_{11} + A_{12} B_{21}\), \(C_{12} \gets C_{12} + A_{12} B_{22}\), \(C_{21} \gets C_{21} + A_{22} B_{21}\), \(C_{22} \gets C_{22} + A_{22} B_{22}\), each by a recursive call on the accumulator left by the previous one. It returns the final accumulator together with two counters: the number of scalar multiplications and the number of accumulator additions.
Only these two operations are charged; calls, indexing and allocation are free.
public def recursiveMatrixAccumulate {α : Type u} [Add α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :=
accumulateBlock A B k 0 0 0 (by simp) (by simp) (by simp) C
@[no_expose] private def accumulateBlock {α : Type u} [Add α] [Mul α] {n : Nat}
(A B : _root_.Vector (_root_.Vector α n) n) :
(depth r t c : Nat) → r + 2 ^ depth ≤ n → t + 2 ^ depth ≤ n →
c + 2 ^ depth ≤ n → _root_.Vector (_root_.Vector α n) n →
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α n) n)
| 0, r, t, c, hr, ht, hc, C =>
updateEntry A B C ⟨r, by simp only [pow_zero] at hr; omega⟩
⟨t, by simp only [pow_zero] at ht; omega⟩
⟨c, by simp only [pow_zero] at hc; omega⟩
| depth + 1, r, t, c, hr, ht, hc, C => do
have hpow : 0 < (2 ^ depth : Nat) := Nat.pow_pos (by decide)
have hr' : r + 2 ^ depth + 2 ^ depth ≤ n := by
⋯
CSLibExt/Algorithms/Lean/Matrix/Recursive.lean:64
TheoremEntries of the recursive matrix multiply-accumulate#✓fully proved
Let \(\alpha\) be an additive commutative monoid with a multiplication, let \(k\) be a natural number and \(n = 2^k\), and let \(A = (a_{ij})\), \(B = (b_{ij})\), \(C = (c_{ij})\) be \(n \times n\) matrices over \(\alpha\), with indices running from \(0\) to \(n - 1\). For all indices \(i, j < n\), entry \((i, j)\) of the matrix returned by the recursive multiply-accumulate procedure on \(A\), \(B\), \(C\) equals \(c_{ij} + \sum_{t < n} a_{it} b_{tj}\).
theorem recursiveMatrixAccumulate_apply {α : Type u} [AddCommMonoid α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) (i j : Fin (2 ^ k)) :
((recursiveMatrixAccumulate k A B C).ret.get i).get j =
(C.get i).get j + ∑ t : Fin (2 ^ k), (A.get i).get t * (B.get t).get j
public def recursiveMatrixAccumulate {α : Type u} [Add α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :=
accumulateBlock A B k 0 0 0 (by simp) (by simp) (by simp) C
CSLibExt/Algorithms/Lean/Matrix/Recursive.lean:185
Uses 1 · Used by 1
TheoremCorrectness of the recursive matrix multiply-accumulate#✓fully proved
Let \(\alpha\) be an additive commutative monoid with a multiplication, let \(k\) be a natural number and \(n = 2^k\), and let \(A = (a_{ij})\), \(B = (b_{ij})\), \(C\) be \(n \times n\) matrices over \(\alpha\), with indices running from \(0\) to \(n - 1\). Viewed as a matrix, the result returned by the recursive multiply-accumulate procedure on \(A\), \(B\), \(C\) equals \(C + AB\), where \(AB\) is the matrix product with entries \((AB)_{ij} = \sum_{t < n} a_{it} b_{tj}\).
theorem recursiveMatrixAccumulate_ret {α : Type u} [AddCommMonoid α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :
_root_.Matrix.of (fun i j => ((recursiveMatrixAccumulate k A B C).ret.get i).get j) =
_root_.Matrix.of (fun i j => (C.get i).get j) +
_root_.Matrix.of (fun i j => (A.get i).get j) *
_root_.Matrix.of (fun i j => (B.get i).get j)
public def recursiveMatrixAccumulate {α : Type u} [Add α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :=
accumulateBlock A B k 0 0 0 (by simp) (by simp) (by simp) C
CSLibExt/Algorithms/Lean/Matrix/Recursive.lean:185
Uses 2
TheoremOperation count of the recursive matrix multiply-accumulate#✓fully proved
Let \(\alpha\) be a type with an addition and a multiplication, let \(k\) be a natural number and \(n = 2^k\), and let \(A\), \(B\), \(C\) be \(n \times n\) matrices over \(\alpha\). The recursive multiply-accumulate procedure on \(A\), \(B\), \(C\) performs exactly \(8^k\) scalar multiplications and exactly \(8^k\) accumulator additions. Since \(8^k = n^3\), each count is the cube of the dimension.
Calls, indexing and allocation are uncharged.
theorem recursiveMatrixAccumulate_time {α : Type u} [Add α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :
(recursiveMatrixAccumulate k A B C).time = (8 ^ k, 8 ^ k)
public def recursiveMatrixAccumulate {α : Type u} [Add α] [Mul α] (k : Nat)
(A B C : _root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :
TimeM (Nat × Nat) (_root_.Vector (_root_.Vector α (2 ^ k)) (2 ^ k)) :=
accumulateBlock A B k 0 0 0 (by simp) (by simp) (by simp) C
CSLibExt/Algorithms/Lean/Matrix/Recursive.lean:185
Uses 1
Loading dependency explorer…
The dependency explorer needs JavaScript and its graph data.