roseking's picture
Add lean source and binary files (part 84)
cdec422 verified
Raw History Blame Contribute Delete
6.54 kB
import OAI.LinearAlgebra.MatrixMultiplication.ComplexArithmetic.Programs
namespace OAI
/-! Complex arithmetic programs and asymptotic matrix multiplication costs. -/
noncomputable section
namespace MatrixMultiplication.Foundation.Arithmetic
namespace Gate
variable {Input Input' Register : Type*}
def mapInput (f : Input → Input') : Gate Input Register → Gate Input' Register
| .constant z => .constant z
| .input i => .input (f i)
| .add i j => .add i j
| .sub i j => .sub i j
| .mul i j => .mul i j
@[simp] theorem eval_mapInput (f : Input → Input') (g : Gate Input Register)
(inputs : Input' → ℂ) (registers : Register → ℂ) :
(g.mapInput f).eval inputs registers = g.eval (inputs ∘ f) registers := by
cases g <;> rfl
@[simp] theorem cost_mapInput (f : Input → Input') (g : Gate Input Register) :
(g.mapInput f).cost = g.cost := by
cases g <;> rfl
def mapSource (f : Input → Input' ⊕ ℂ) : Gate Input Register → Gate Input' Register
| .constant z => .constant z
| .input i => match f i with
| .inl j => .input j
| .inr z => .constant z
| .add i j => .add i j
| .sub i j => .sub i j
| .mul i j => .mul i j
@[simp] theorem eval_mapSource (f : Input → Input' ⊕ ℂ) (g : Gate Input Register)
(inputs : Input' → ℂ) (registers : Register → ℂ) :
(g.mapSource f).eval inputs registers =
g.eval (fun i => Sum.elim inputs id (f i)) registers := by
cases g with
| input i => cases h : f i <;> simp [mapSource, h, eval]
| constant => rfl
| add => rfl
| sub => rfl
| mul => rfl
@[simp] theorem cost_mapSource (f : Input → Input' ⊕ ℂ) (g : Gate Input Register) :
(g.mapSource f).cost = g.cost := by
cases g with
| input i => cases h : f i <;> simp [mapSource, h, cost]
| constant => rfl
| add => rfl
| sub => rfl
| mul => rfl
end Gate
namespace Program
variable {Input Input' Mid : Type*}
def mapInput (f : Input → Input') : {r : ℕ} → Program Input r → Program Input' r
| 0, .nil => .nil
| _ + 1, .step p g => (p.mapInput f).step (g.mapInput f)
@[simp] theorem eval_mapInput (f : Input → Input') {r : ℕ} (p : Program Input r)
(inputs : Input' → ℂ) :
∀ i : Fin r, (p.mapInput f).eval inputs i = p.eval (inputs ∘ f) i := by
induction p with
| nil => intro i; exact Fin.elim0 i
| @step r p g ih =>
intro i
refine Fin.cases ?_ (fun j => ?_) i
· simp only [mapInput, eval_step_zero, Gate.eval_mapInput]
rw [funext ih]
· exact ih j
@[simp] theorem cost_mapInput (f : Input → Input') {r : ℕ} (p : Program Input r) :
(p.mapInput f).cost = p.cost := by
induction p with
| nil => rfl
| step p g ih => simp only [mapInput, cost_step, Gate.cost_mapInput, ih]
def mapSource (f : Input → Input' ⊕ ℂ) : {r : ℕ} → Program Input r → Program Input' r
| 0, .nil => .nil
| _ + 1, .step p g => (p.mapSource f).step (g.mapSource f)
@[simp] theorem eval_mapSource (f : Input → Input' ⊕ ℂ) {r : ℕ}
(p : Program Input r) (inputs : Input' → ℂ) :
∀ i : Fin r, (p.mapSource f).eval inputs i =
p.eval (fun j => Sum.elim inputs id (f j)) i := by
induction p with
| nil => intro i; exact Fin.elim0 i
| @step r p g ih =>
intro i
refine Fin.cases ?_ (fun j => ?_) i
· simp only [mapSource, eval_step_zero, Gate.eval_mapSource]
rw [funext ih]
· exact ih j
@[simp] theorem cost_mapSource (f : Input → Input' ⊕ ℂ) {r : ℕ}
(p : Program Input r) : (p.mapSource f).cost = p.cost := by
induction p with
| nil => rfl
| step p g ih => simp only [mapSource, cost_step, Gate.cost_mapSource, ih]
structure Substituted {r s : ℕ} (p : Program Input r) (q : Program Mid s)
(wires : Mid → Fin r) where
registers : ℕ
program : Program Input registers
oldOutput : Fin r → Fin registers
output : Fin s → Fin registers
correctOld : ∀ inputs i, program.eval inputs (oldOutput i) = p.eval inputs i
correct : ∀ inputs i, program.eval inputs (output i) =
q.eval (fun j => p.eval inputs (wires j)) i
cost_eq : program.cost = p.cost + q.cost
namespace Substituted
def step {r s : ℕ} {p : Program Input r} {q : Program Mid s}
{wires : Mid → Fin r} (a : Substituted p q wires)
(g : Gate Mid (Fin s)) (h : Gate Input (Fin a.registers))
(heval : ∀ inputs, h.eval inputs (a.program.eval inputs) =
g.eval (fun j => p.eval inputs (wires j))
(q.eval (fun j => p.eval inputs (wires j))))
(hcost : h.cost = g.cost) : Substituted p (q.step g) wires where
registers := a.registers + 1
program := a.program.step h
oldOutput := fun i => (a.oldOutput i).succ
output := Fin.cases 0 (fun i => (a.output i).succ)
correctOld := by
intro inputs i
exact a.correctOld inputs i
correct := by
intro inputs i
refine Fin.cases ?_ (fun j => ?_) i
· exact heval inputs
· exact a.correct inputs j
cost_eq := by
simp only [cost_step, a.cost_eq, hcost, Nat.add_assoc]
end Substituted
def substitute {r : ℕ} (p : Program Input r) :
{s : ℕ} → (q : Program Mid s) → (wires : Mid → Fin r) → Substituted p q wires
| 0, .nil, wires =>
{ registers := r
program := p
oldOutput := id
output := Fin.elim0
correctOld := fun _ _ => rfl
correct := fun _ i => Fin.elim0 i
cost_eq := by simp only [cost, Nat.add_zero] }
| _ + 1, .step q g, wires =>
let a := substitute p q wires
match g with
| .input j =>
{ registers := a.registers
program := a.program
oldOutput := a.oldOutput
output := Fin.cases (a.oldOutput (wires j)) a.output
correctOld := a.correctOld
correct := by
intro inputs i
refine Fin.cases ?_ (fun k => ?_) i
· exact a.correctOld inputs (wires j)
· exact a.correct inputs k
cost_eq := by
simpa only [cost_step, Gate.cost, Nat.add_zero] using a.cost_eq }
| .constant z =>
a.step (.constant z) (.constant z) (fun _ => rfl) rfl
| .add i j =>
a.step (.add i j) (.add (a.output i) (a.output j))
(by intro inputs; simp only [Gate.eval, a.correct]) rfl
| .sub i j =>
a.step (.sub i j) (.sub (a.output i) (a.output j))
(by intro inputs; simp only [Gate.eval, a.correct]) rfl
| .mul i j =>
a.step (.mul i j) (.mul (a.output i) (a.output j))
(by intro inputs; simp only [Gate.eval, a.correct]) rfl
end Program
end MatrixMultiplication.Foundation.Arithmetic
end
end OAI