cfahlgren1's picture
download
raw
5.18 kB
import OAI.LinearAlgebra.MatrixMultiplication.ComplexArithmetic.Complexity
import OAI.LinearAlgebra.MatrixMultiplication.Tensor.ComplexTensorBatching
import Mathlib.Tactic.SplitIfs
import Mathlib.Algebra.BigOperators.GroupWithZero.Finset
namespace OAI
/-! Finite coefficient tensors and their algebraic transformations. -/
open scoped BigOperators
namespace MatrixMultiplication.Foundation
namespace Tensor
variable {K A B C D E F : Type*} [CommSemiring K]
def matrixCoefficients (A B C : Type*) [DecidableEq A] [DecidableEq B] [DecidableEq C] :
Tensor K (A × B) (B × C) (C × A) :=
fun x y z => if x.2 = y.1 ∧ y.2 = z.1 ∧ z.2 = x.1 then 1 else 0
theorem matrixMultiplication_eq_matrixCoefficients (a b c : ℕ) :
matrixMultiplication a b c = matrixCoefficients (K := ℂ) (Fin a) (Fin b) (Fin c) := rfl
theorem contract_matrixCoefficients [Fintype A] [Fintype B] [Fintype C]
[DecidableEq A] [DecidableEq B] [DecidableEq C]
(left : A × B → K) (right : B × C → K) (i : A) (k : C) :
contract (matrixCoefficients A B C) left right (k, i) =
∑ j, left (i, j) * right (j, k) := by
simp [contract, matrixCoefficients, Fintype.sum_prod_type, ite_and, ite_mul]
theorem matrixCoefficients_product [DecidableEq A] [DecidableEq B] [DecidableEq C]
[DecidableEq D] [DecidableEq E] [DecidableEq F]
(x : (A × D) × (B × E)) (y : (B × E) × (C × F)) (z : (C × F) × (A × D)) :
product (matrixCoefficients (K := K) A B C) (matrixCoefficients D E F)
((x.1.1, x.2.1), (x.1.2, x.2.2))
((y.1.1, y.2.1), (y.1.2, y.2.2))
((z.1.1, z.2.1), (z.1.2, z.2.2)) =
matrixCoefficients (A × D) (B × E) (C × F) x y z := by
simp only [product, matrixCoefficients, Prod.ext_iff]
split_ifs <;> simp_all
theorem matrixCoefficients_cyclic [DecidableEq A] [DecidableEq B] [DecidableEq C]
(x : B × C) (y : C × A) (z : A × B) :
matrixCoefficients (K := K) A B C z x y = matrixCoefficients B C A x y z := by
have h : (z.2 = x.1 ∧ x.2 = y.1 ∧ y.2 = z.1) ↔
(x.2 = y.1 ∧ y.2 = z.1 ∧ z.2 = x.1) :=
⟨fun h => ⟨h.2.1, h.2.2, h.1⟩, fun h => ⟨h.2.2, h.1, h.2.1⟩⟩
simp only [matrixCoefficients, h]
theorem matrixCoefficients_rank_of_product [DecidableEq A] [DecidableEq B] [DecidableEq C]
[DecidableEq D] [DecidableEq E] [DecidableEq F] {r : ℕ}
(hproduct : RankAtMost (product (matrixCoefficients (K := K) A B C)
(matrixCoefficients D E F)) r) :
RankAtMost (matrixCoefficients (K := K) (A × D) (B × E) (C × F)) r := by
have h := hproduct.pullback
(fun x : (A × D) × (B × E) => ((x.1.1, x.2.1), (x.1.2, x.2.2)))
(fun y : (B × E) × (C × F) => ((y.1.1, y.2.1), (y.1.2, y.2.2)))
(fun z : (C × F) × (A × D) => ((z.1.1, z.2.1), (z.1.2, z.2.2)))
have heq : pullback
(fun x : (A × D) × (B × E) => ((x.1.1, x.2.1), (x.1.2, x.2.2)))
(fun y : (B × E) × (C × F) => ((y.1.1, y.2.1), (y.1.2, y.2.2)))
(fun z : (C × F) × (A × D) => ((z.1.1, z.2.1), (z.1.2, z.2.2)))
(product (matrixCoefficients (K := K) A B C) (matrixCoefficients D E F)) =
matrixCoefficients (K := K) (A × D) (B × E) (C × F) := by
funext x y z
exact matrixCoefficients_product x y z
rw [heq] at h
exact h
theorem matrixCoefficients_product_rank [DecidableEq A] [DecidableEq B] [DecidableEq C]
[DecidableEq D] [DecidableEq E] [DecidableEq F] {r s : ℕ}
(hT : RankAtMost (matrixCoefficients (K := K) A B C) r)
(hS : RankAtMost (matrixCoefficients (K := K) D E F) s) :
RankAtMost (matrixCoefficients (K := K) (A × D) (B × E) (C × F)) (r * s) :=
matrixCoefficients_rank_of_product (hT.product hS)
theorem matrixCoefficients_batch_rank [DecidableEq A] [DecidableEq B] [Fintype B]
{R k r batches : ℕ}
(houter : RankAtMost (matrixCoefficients (K := K) A A A) R)
(hbatch : RankAtMost (directSum (fun _ : Fin k => matrixCoefficients (K := K) B B B)) r)
(hfit : R ≤ batches * k) :
RankAtMost (matrixCoefficients (K := K) (A × B) (A × B) (A × B)) (batches * r) :=
matrixCoefficients_rank_of_product (houter.batch_product hbatch hfit)
theorem matrixCoefficients_power [DecidableEq A] [DecidableEq B] [DecidableEq C]
(n : ℕ) (x : (Fin n → A) × (Fin n → B))
(y : (Fin n → B) × (Fin n → C)) (z : (Fin n → C) × (Fin n → A)) :
Tensor.power (matrixCoefficients (K := K) A B C) n
(fun i => (x.1 i, x.2 i)) (fun i => (y.1 i, y.2 i)) (fun i => (z.1 i, z.2 i)) =
matrixCoefficients (Fin n → A) (Fin n → B) (Fin n → C) x y z := by
simp only [Tensor.power, matrixCoefficients, Fintype.prod_boole, forall_and, ← funext_iff]
theorem matrixCoefficients_cyclic_rank [DecidableEq A] [DecidableEq B] [DecidableEq C]
{r : ℕ} (h : RankAtMost (matrixCoefficients (K := K) A B C) r) :
RankAtMost (matrixCoefficients (K := K) B C A) r := by
rcases h with ⟨a, b, c, heq⟩
refine ⟨b, c, a, ?_⟩
funext x y z
rw [← matrixCoefficients_cyclic (K := K) x y z]
rw [congrFun (congrFun (congrFun heq z) x) y]
apply Finset.sum_congr rfl
intro i hi
simp only [rankOne]
ring
end Tensor
end MatrixMultiplication.Foundation
end OAI

Xet Storage Details

Size:
5.18 kB
·
Xet hash:
2cd69b35f8cd1c7a60648272508ade14b979163564eee906280f7b71c67b5547

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.