import OAI.LinearAlgebra.MatrixMultiplication.Tensor.ComplexTensor import Mathlib.Analysis.SpecialFunctions.Pow.Real import Mathlib.Data.Matrix.Mul namespace OAI /-! Complex arithmetic programs and asymptotic matrix multiplication costs. -/ noncomputable section open scoped BigOperators namespace MatrixMultiplication.Foundation namespace Arithmetic inductive Gate (Input Register : Type*) where | constant : ℂ → Gate Input Register | input : Input → Gate Input Register | add : Register → Register → Gate Input Register | sub : Register → Register → Gate Input Register | mul : Register → Register → Gate Input Register namespace Gate variable {Input Register : Type*} def eval (inputs : Input → ℂ) (registers : Register → ℂ) : Gate Input Register → ℂ | .constant z => z | .input i => inputs i | .add i j => registers i + registers j | .sub i j => registers i - registers j | .mul i j => registers i * registers j def cost : Gate Input Register → ℕ | .constant _ => 0 | .input _ => 0 | .add _ _ => 1 | .sub _ _ => 1 | .mul _ _ => 1 end Gate inductive Program (Input : Type*) : ℕ → Type _ where | nil : Program Input 0 | step {r : ℕ} : Program Input r → Gate Input (Fin r) → Program Input (r + 1) namespace Program variable {Input : Type*} def eval : {r : ℕ} → Program Input r → (Input → ℂ) → Fin r → ℂ | 0, .nil, _ => Fin.elim0 | _ + 1, .step p g, inputs => Fin.cases (g.eval inputs (p.eval inputs)) (p.eval inputs) def cost : {r : ℕ} → Program Input r → ℕ | 0, .nil => 0 | _ + 1, .step p g => p.cost + g.cost @[simp] theorem eval_step_zero {r : ℕ} (p : Program Input r) (g : Gate Input (Fin r)) (inputs : Input → ℂ) : (p.step g).eval inputs 0 = g.eval inputs (p.eval inputs) := rfl @[simp] theorem eval_step_succ {r : ℕ} (p : Program Input r) (g : Gate Input (Fin r)) (inputs : Input → ℂ) (i : Fin r) : (p.step g).eval inputs i.succ = p.eval inputs i := rfl @[simp] theorem cost_step {r : ℕ} (p : Program Input r) (g : Gate Input (Fin r)) : (p.step g).cost = p.cost + g.cost := rfl end Program abbrev MatrixInput (n : ℕ) := (Fin n × Fin n) ⊕ (Fin n × Fin n) def matrixInputs {n : ℕ} (A B : Matrix (Fin n) (Fin n) ℂ) : MatrixInput n → ℂ | .inl (i, j) => A i j | .inr (j, k) => B j k structure MatrixAlgorithm (n : ℕ) where registers : ℕ program : Program (MatrixInput n) registers output : Fin n → Fin n → Fin registers namespace MatrixAlgorithm def eval {n : ℕ} (P : MatrixAlgorithm n) (A B : Matrix (Fin n) (Fin n) ℂ) : Matrix (Fin n) (Fin n) ℂ := fun i k => P.program.eval (matrixInputs A B) (P.output i k) def Correct {n : ℕ} (P : MatrixAlgorithm n) : Prop := ∀ A B : Matrix (Fin n) (Fin n) ℂ, P.eval A B = A * B def cost {n : ℕ} (P : MatrixAlgorithm n) : ℕ := P.program.cost theorem correct_iff_entries {n : ℕ} (P : MatrixAlgorithm n) : P.Correct ↔ ∀ (A B : Matrix (Fin n) (Fin n) ℂ) (i k : Fin n), P.program.eval (matrixInputs A B) (P.output i k) = ∑ j, A i j * B j k := by constructor · intro h A B i k exact congrFun (congrFun (h A B) i) k · intro h A B funext i k exact h A B i k end MatrixAlgorithm def scalarAlgorithm : MatrixAlgorithm 1 where registers := 3 program := ((Program.nil.step (.input (.inl (0, 0)))).step (.input (.inr (0, 0)))).step (.mul 1 0) output := fun _ _ => 0 theorem scalarAlgorithm_correct : scalarAlgorithm.Correct := by intro A B funext i k have hi : i = 0 := Subsingleton.elim _ _ have hk : k = 0 := Subsingleton.elim _ _ subst i subst k change A 0 0 * B 0 0 = (A * B) 0 0 simp [Matrix.mul_apply] @[simp] theorem scalarAlgorithm_cost : scalarAlgorithm.cost = 1 := rfl def AdmissibleExponent (τ : ℝ) : Prop := ∀ ε : ℝ, 0 < ε → ∃ C : ℝ, 0 < C ∧ ∀ n : ℕ, 1 ≤ n → ∃ P : MatrixAlgorithm n, P.Correct ∧ (P.cost : ℝ) ≤ C * (n : ℝ) ^ (τ + ε) noncomputable def omega : ℝ := sInf {τ : ℝ | AdmissibleExponent τ} end Arithmetic namespace Tensor def matrixMultiplication (a b c : ℕ) : Tensor ℂ (Fin a × Fin b) (Fin b × Fin c) (Fin c × Fin a) := fun x y z => if x.2 = y.1 ∧ y.2 = z.1 ∧ z.2 = x.1 then 1 else 0 @[simp] theorem matrixMultiplication_matching (a b c : ℕ) (i : Fin a) (j : Fin b) (k : Fin c) : matrixMultiplication a b c (i, j) (j, k) (k, i) = 1 := by simp [matrixMultiplication] end Tensor end MatrixMultiplication.Foundation end end OAI