Download data/lean/OAI/LinearAlgebra/MatrixFields/Arithmetic/RecursiveBlockPrograms.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 28.4 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixFields/Arithmetic/RecursiveBlockPrograms.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixFields/Arithmetic/RecursiveBlockPrograms.lean
-
curl -L -o RecursiveBlockPrograms.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixFields/Arithmetic/RecursiveBlockPrograms.lean
28.4 kB
| import Mathlib | |
| import OAI.LinearAlgebra.MatrixFields.Construction.ExpressionFamily | |
| import OAI.LinearAlgebra.MatrixFields.Tensors.TerminalRelabel | |
| namespace OAI | |
| namespace MatrixAllFields | |
| open scoped BigOperators Topology Polynomial | |
| section | |
| noncomputable section | |
| namespace MatrixMultiplication.Arithmetic | |
| variable {F : Type*} [Field F] | |
| namespace Gate | |
| variable {Input Input' Register : Type*} | |
| def mapInput (f : Input β Input') : Gate F Input Register β Gate F 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 F Input Register) | |
| (inputs : Input' β F) (registers : Register β F) : | |
| (g.mapInput f).eval inputs registers = g.eval (inputs β f) registers := by | |
| cases g <;> rfl | |
| omit [Field F] in | |
| @[simp] theorem cost_mapInput (f : Input β Input') (g : Gate F Input Register) : | |
| (g.mapInput f).cost = g.cost := by | |
| cases g <;> rfl | |
| def mapSource (f : Input β Input' β F) : Gate F Input Register β Gate F 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' β F) (g : Gate F Input Register) | |
| (inputs : Input' β F) (registers : Register β F) : | |
| (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 | |
| omit [Field F] in | |
| @[simp] theorem cost_mapSource (f : Input β Input' β F) (g : Gate F 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 F Input r β Program F 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 F Input r) | |
| (inputs : Input' β F) : | |
| β 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 | |
| omit [Field F] in | |
| @[simp] theorem cost_mapInput (f : Input β Input') {r : β} (p : Program F 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' β F) : {r : β} β Program F Input r β Program F Input' r | |
| | 0, .nil => .nil | |
| | _ + 1, .step p g => (p.mapSource f).step (g.mapSource f) | |
| @[simp] theorem eval_mapSource (f : Input β Input' β F) {r : β} | |
| (p : Program F Input r) (inputs : Input' β F) : | |
| β 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 | |
| omit [Field F] in | |
| @[simp] theorem cost_mapSource (f : Input β Input' β F) {r : β} | |
| (p : Program F 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 F Input r) (q : Program F Mid s) | |
| (wires : Mid β Fin r) where | |
| registers : β | |
| program : Program F 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 F Input r} {q : Program F Mid s} | |
| {wires : Mid β Fin r} (a : Substituted p q wires) | |
| (g : Gate F Mid (Fin s)) (h : Gate F 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 F Input r) : | |
| {s : β} β (q : Program F 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.Arithmetic | |
| end | |
| noncomputable section | |
| open scoped BigOperators | |
| namespace MatrixMultiplication.Arithmetic | |
| variable {F : Type*} [Field F] | |
| def zeroPadMatrix {a b : β} (A B : β) (M : Matrix (Fin a) (Fin b) F) : | |
| Matrix (Fin A) (Fin B) F := | |
| fun i j => if hi : i.val < a then | |
| if hj : j.val < b then M β¨i.val, hiβ© β¨j.val, hjβ© else 0 | |
| else 0 | |
| @[simp] theorem zeroPadMatrix_castLE {a b A B : β} (ha : a β€ A) (hb : b β€ B) | |
| (M : Matrix (Fin a) (Fin b) F) (i : Fin a) (j : Fin b) : | |
| zeroPadMatrix A B M (Fin.castLE ha i) (Fin.castLE hb j) = M i j := by | |
| simp [zeroPadMatrix, i.is_lt, j.is_lt] | |
| theorem sum_fin_castLE_of_zero {n m : β} (h : n β€ m) (f : Fin m β F) | |
| (hf : β j : Fin m, n β€ j.val β f j = 0) : | |
| (β j : Fin m, f j) = β j : Fin n, f (Fin.castLE h j) := by | |
| symm | |
| refine Fintype.sum_of_injective (Fin.castLE h) ?_ | |
| (fun j : Fin n => f (Fin.castLE h j)) f ?_ (fun _ => rfl) | |
| Β· intro i j hij | |
| exact Fin.ext (congrArg (fun v : Fin m => v.val) hij) | |
| Β· intro j hj | |
| exact hf j (le_of_not_gt fun hsmall => hj β¨β¨j.val, hsmallβ©, Fin.ext rflβ©) | |
| theorem zeroPadMatrix_mul {a b c A B C : β} (ha : a β€ A) (hb : b β€ B) (hc : c β€ C) | |
| (M : Matrix (Fin a) (Fin b) F) (N : Matrix (Fin b) (Fin c) F) | |
| (i : Fin a) (k : Fin c) : | |
| (zeroPadMatrix A B M * zeroPadMatrix B C N) (Fin.castLE ha i) (Fin.castLE hc k) = | |
| (M * N) i k := by | |
| rw [Matrix.mul_apply, Matrix.mul_apply] | |
| calc | |
| (β j : Fin B, | |
| zeroPadMatrix A B M (Fin.castLE ha i) j * | |
| zeroPadMatrix B C N j (Fin.castLE hc k)) = | |
| β j : Fin b, | |
| zeroPadMatrix A B M (Fin.castLE ha i) (Fin.castLE hb j) * | |
| zeroPadMatrix B C N (Fin.castLE hb j) (Fin.castLE hc k) := by | |
| apply sum_fin_castLE_of_zero hb | |
| intro j hj | |
| simp [zeroPadMatrix, not_lt_of_ge hj] | |
| _ = β j : Fin b, M i j * N j k := by simp | |
| def padSource {a b c A B C : β} : MatrixInput A B C β MatrixInput a b c β F | |
| | .inl (i, j) => if hi : i.val < a then | |
| if hj : j.val < b then .inl (.inl (β¨i.val, hiβ©, β¨j.val, hjβ©)) else .inr 0 | |
| else .inr 0 | |
| | .inr (j, k) => if hj : j.val < b then | |
| if hk : k.val < c then .inl (.inr (β¨j.val, hjβ©, β¨k.val, hkβ©)) else .inr 0 | |
| else .inr 0 | |
| theorem padSource_eval {a b c A B C : β} | |
| (M : Matrix (Fin a) (Fin b) F) (N : Matrix (Fin b) (Fin c) F) : | |
| (fun x : MatrixInput A B C => Sum.elim (matrixInputs M N) id (padSource x)) = | |
| matrixInputs (zeroPadMatrix A B M) (zeroPadMatrix B C N) := by | |
| funext x | |
| rcases x with β¨i, jβ© | β¨j, kβ© | |
| Β· by_cases hi : i.val < a <;> by_cases hj : j.val < b <;> | |
| simp [padSource, matrixInputs, zeroPadMatrix, hi, hj] | |
| Β· by_cases hj : j.val < b <;> by_cases hk : k.val < c <;> | |
| simp [padSource, matrixInputs, zeroPadMatrix, hj, hk] | |
| namespace MatrixAlgorithm | |
| def pad {a b c A B C : β} (ha : a β€ A) (_hb : b β€ B) (hc : c β€ C) | |
| (P : MatrixAlgorithm F A B C) : MatrixAlgorithm F a b c where | |
| registers := P.registers | |
| program := P.program.mapSource (padSource (a := a) (b := b) (c := c)) | |
| output := fun i k => P.output (Fin.castLE ha i) (Fin.castLE hc k) | |
| theorem pad_correct {a b c A B C : β} (ha : a β€ A) (hb : b β€ B) (hc : c β€ C) | |
| {P : MatrixAlgorithm F A B C} (hP : P.Correct) : (pad ha hb hc P).Correct := by | |
| intro M N | |
| funext i k | |
| change (P.program.mapSource (padSource (a := a) (b := b) (c := c))).eval | |
| (matrixInputs M N) (P.output (Fin.castLE ha i) (Fin.castLE hc k)) = (M * N) i k | |
| rw [Program.eval_mapSource, padSource_eval] | |
| have hentry := congrFun (congrFun | |
| (hP (zeroPadMatrix A B M) (zeroPadMatrix B C N)) (Fin.castLE ha i)) (Fin.castLE hc k) | |
| exact hentry.trans (zeroPadMatrix_mul ha hb hc M N i k) | |
| @[simp] theorem pad_cost_eq {a b c A B C : β} (ha : a β€ A) (hb : b β€ B) (hc : c β€ C) | |
| (P : MatrixAlgorithm F A B C) : (pad ha hb hc P).cost = P.cost := | |
| Program.cost_mapSource (padSource (a := a) (b := b) (c := c)) P.program | |
| theorem exists_restrict {a b c A B C : β} (ha : a β€ A) (hb : b β€ B) (hc : c β€ C) | |
| (P : MatrixAlgorithm F A B C) (hP : P.Correct) : | |
| β Q : MatrixAlgorithm F a b c, Q.Correct β§ Q.cost β€ P.cost := | |
| β¨pad ha hb hc P, pad_correct ha hb hc hP, (pad_cost_eq ha hb hc P).leβ© | |
| end MatrixAlgorithm | |
| theorem RectangularAdmissibleExponent.mono_aspect {k l Ο : β} | |
| (h : RectangularAdmissibleExponent F l Ο) (hkl : k β€ l) : | |
| RectangularAdmissibleExponent F k Ο := by | |
| intro Ξ΅ hΞ΅ | |
| obtain β¨C, hC, hboundβ© := h Ξ΅ hΞ΅ | |
| refine β¨C, hC, ?_β© | |
| intro n hn | |
| obtain β¨P, hP, hcostβ© := hbound n hn | |
| obtain β¨Q, hQ, hcostQβ© := MatrixAlgorithm.exists_restrict (le_refl n) | |
| (innerSize_mono hn hkl) (le_refl n) P hP | |
| exact β¨Q, hQ, (Nat.cast_le.mpr hcostQ).trans hcostβ© | |
| end MatrixMultiplication.Arithmetic | |
| namespace MatrixMultiplication.Arithmetic.RecursiveBlock | |
| open MatrixMultiplication.Foundation | |
| variable {F : Type*} [Field F] | |
| namespace LinearExpression | |
| variable {Input : Type*} | |
| def sumFin : (n : β) β (Fin n β Expression F Input) β Expression F Input | |
| | 0, _ => .constant 0 | |
| | n + 1, f => .add (f 0) (sumFin n (fun i => f i.succ)) | |
| theorem eval_sumFin (n : β) (f : Fin n β Expression F Input) (inputs : Input β F) : | |
| (sumFin n f).eval inputs = β i, (f i).eval inputs := by | |
| induction n with | |
| | zero => simp [sumFin, Expression.eval] | |
| | succ n ih => | |
| simp only [sumFin, Expression.eval, ih, Fin.sum_univ_succ] | |
| theorem cost_sumFin (n : β) (f : Fin n β Expression F Input) : | |
| (sumFin n f).cost = (β i, (f i).cost) + n := by | |
| induction n with | |
| | zero => simp [sumFin, Expression.cost] | |
| | succ n ih => | |
| simp only [sumFin, Expression.cost, ih, Fin.sum_univ_succ] | |
| omega | |
| def linear {Term : Type*} [Fintype Term] (a : Term β F) (wire : Term β Input) : | |
| Expression F Input := | |
| sumFin (Fintype.card Term) (fun i => | |
| .mul (.constant (a ((Fintype.equivFin Term).symm i))) | |
| (.input (wire ((Fintype.equivFin Term).symm i)))) | |
| theorem eval_linear {Term : Type*} [Fintype Term] | |
| (a : Term β F) (wire : Term β Input) (inputs : Input β F) : | |
| (linear a wire).eval inputs = β i, a i * inputs (wire i) := by | |
| rw [linear, eval_sumFin] | |
| exact (Fintype.equivFin Term).symm.sum_comp (fun i => a i * inputs (wire i)) | |
| theorem cost_linear {Term : Type*} [Fintype Term] | |
| (a : Term β F) (wire : Term β Input) : | |
| (linear a wire).cost = 2 * Fintype.card Term := by | |
| simp [linear, cost_sumFin, Expression.cost, two_mul] | |
| end LinearExpression | |
| structure Family (F Input Output : Type*) where | |
| registers : β | |
| program : Program F Input registers | |
| output : Output β Fin registers | |
| namespace Family | |
| variable {Input Mid Output : Type*} | |
| def eval (p : Family F Input Output) (inputs : Input β F) (o : Output) : F := | |
| p.program.eval inputs (p.output o) | |
| def cost (p : Family F Input Output) : β := p.program.cost | |
| def expressions [Fintype Output] (e : Output β Expression F Input) : Family F Input Output := | |
| let p := Expression.compileFamily e | |
| β¨p.registers, p.program, p.outputβ© | |
| theorem eval_expressions [Fintype Output] (e : Output β Expression F Input) | |
| (inputs : Input β F) (o : Output) : | |
| (expressions e).eval inputs o = (e o).eval inputs := | |
| (Expression.compileFamily e).correct inputs o | |
| theorem cost_expressions [Fintype Output] (e : Output β Expression F Input) : | |
| (expressions e).cost = β o, (e o).cost := | |
| (Expression.compileFamily e).cost_eq | |
| def compose (p : Family F Input Mid) (q : Family F Mid Output) : Family F Input Output := | |
| let s := p.program.substitute q.program p.output | |
| β¨s.registers, s.program, fun o => s.output (q.output o)β© | |
| theorem eval_compose (p : Family F Input Mid) (q : Family F Mid Output) | |
| (inputs : Input β F) (o : Output) : | |
| (compose p q).eval inputs o = q.eval (p.eval inputs) o := | |
| (p.program.substitute q.program p.output).correct inputs (q.output o) | |
| theorem cost_compose (p : Family F Input Mid) (q : Family F Mid Output) : | |
| (compose p q).cost = p.cost + q.cost := | |
| (p.program.substitute q.program p.output).cost_eq | |
| def mapInputs {NewInput : Type*} (f : Input β NewInput) (p : Family F Input Output) : | |
| Family F NewInput Output := | |
| β¨p.registers, p.program.mapInput f, p.outputβ© | |
| theorem eval_mapInputs {NewInput : Type*} (f : Input β NewInput) | |
| (p : Family F Input Output) (inputs : NewInput β F) (o : Output) : | |
| (p.mapInputs f).eval inputs o = p.eval (fun i => inputs (f i)) o := by | |
| exact Program.eval_mapInput f p.program inputs (p.output o) | |
| omit [Field F] in | |
| theorem cost_mapInputs {NewInput : Type*} (f : Input β NewInput) | |
| (p : Family F Input Output) : (p.mapInputs f).cost = p.cost := by | |
| exact Program.cost_mapInput f p.program | |
| def parallel : (r : β) β (Fin r β Family F Input Output) β Family F Input (Fin r Γ Output) | |
| | 0, _ => β¨0, Program.nil, fun o => Fin.elim0 o.1β© | |
| | r + 1, p => | |
| let head := p 0 | |
| let tail := parallel r (fun i => p i.succ) | |
| { registers := head.registers + tail.registers | |
| program := head.program.append tail.program | |
| output := fun o => Fin.cases | |
| (Program.oldIndex _ _ (head.output o.2)) | |
| (fun i => Program.newIndex _ _ (tail.output (i, o.2))) o.1 } | |
| theorem eval_parallel (r : β) (p : Fin r β Family F Input Output) | |
| (inputs : Input β F) (o : Fin r Γ Output) : | |
| (parallel r p).eval inputs o = (p o.1).eval inputs o.2 := by | |
| induction r with | |
| | zero => exact Fin.elim0 o.1 | |
| | succ r ih => | |
| rcases o with β¨i, oβ© | |
| refine Fin.cases ?_ (fun j => ?_) i | |
| Β· exact Program.eval_append_old _ _ inputs _ | |
| Β· exact (Program.eval_append_new _ _ inputs _).trans | |
| (ih (fun j => p j.succ) (j, o)) | |
| omit [Field F] in | |
| theorem cost_parallel (r : β) (p : Fin r β Family F Input Output) : | |
| (parallel r p).cost = β i, (p i).cost := by | |
| induction r with | |
| | zero => simp [parallel, cost, Program.cost] | |
| | succ r ih => | |
| change ((p 0).program.append (parallel r (fun i => p i.succ)).program).cost = _ | |
| rw [Program.cost_append] | |
| change (p 0).cost + (parallel r (fun i => p i.succ)).cost = _ | |
| rw [ih, Fin.sum_univ_succ] | |
| def copies (r : β) (p : Family F Input Output) : | |
| Family F (Fin r Γ Input) (Fin r Γ Output) := | |
| parallel r (fun i => p.mapInputs (fun x => (i, x))) | |
| theorem eval_copies (r : β) (p : Family F Input Output) | |
| (inputs : Fin r Γ Input β F) (o : Fin r Γ Output) : | |
| (copies r p).eval inputs o = p.eval (fun x => inputs (o.1, x)) o.2 := by | |
| rw [copies, eval_parallel, eval_mapInputs] | |
| omit [Field F] in | |
| theorem cost_copies (r : β) (p : Family F Input Output) : | |
| (copies r p).cost = r * p.cost := by | |
| simp [copies, cost_parallel, cost_mapInputs] | |
| def ofMatrix {m l q : β} (p : MatrixAlgorithm F m l q) : | |
| Family F (MatrixInput m l q) (Fin m Γ Fin q) := | |
| β¨p.registers, p.program, fun o => p.output o.1 o.2β© | |
| omit [Field F] in | |
| @[simp] theorem cost_ofMatrix {m l q : β} (p : MatrixAlgorithm F m l q) : | |
| (ofMatrix p).cost = p.cost := rfl | |
| theorem eval_ofMatrix {m l q : β} (p : MatrixAlgorithm F m l q) (hp : p.Correct) | |
| (inputs : MatrixInput m l q β F) (o : Fin m Γ Fin q) : | |
| (ofMatrix p).eval inputs o = | |
| β j, inputs (.inl (o.1, j)) * inputs (.inr (j, o.2)) := by | |
| have h := (MatrixAlgorithm.correct_iff_entries p).mp hp | |
| (fun i j => inputs (.inl (i, j))) (fun j k => inputs (.inr (j, k))) o.1 o.2 | |
| have hi : matrixInputs (fun i j => inputs (.inl (i, j))) | |
| (fun j k => inputs (.inr (j, k))) = inputs := by | |
| funext x | |
| cases x <;> rfl | |
| rw [hi] at h | |
| exact h | |
| end Family | |
| section BlockIdentities | |
| variable {nβ nβ nβ r mβ mβ mβ : β} | |
| def join {n m : β} (i : Fin n) (j : Fin m) : Fin (n * m) := finProdFinEquiv (i, j) | |
| theorem rank_bilinear_identity | |
| (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) | |
| (c : Fin r β Fin nβ Γ Fin nβ β F) | |
| (h : Tensor.matrixCoefficients (K := F) (Fin nβ) (Fin nβ) (Fin nβ) = | |
| fun x y z => β q, Tensor.rankOne (a q) (b q) (c q) x y z) | |
| (left : Fin nβ Γ Fin nβ β F) (right : Fin nβ Γ Fin nβ β F) | |
| (i : Fin nβ) (k : Fin nβ) : | |
| (β q, c q (k, i) * (β x, a q x * left x) * (β y, b q y * right y)) = | |
| β j, left (i, j) * right (j, k) := by | |
| rw [β Tensor.contract_matrixCoefficients left right i k] | |
| rw [h] | |
| simp only [Tensor.contract, Tensor.rankOne, Finset.sum_mul, Finset.mul_sum] | |
| calc | |
| (β q, β y, β x, c q (k, i) * (a q x * left x) * (b q y * right y)) = | |
| β q, β x, β y, c q (k, i) * (a q x * left x) * (b q y * right y) := by | |
| apply Finset.sum_congr rfl | |
| intro q hq | |
| exact Finset.sum_comm | |
| _ = β x, β q, β y, c q (k, i) * (a q x * left x) * (b q y * right y) := | |
| Finset.sum_comm | |
| _ = β x, β y, β q, a q x * b q y * c q (k, i) * left x * right y := by | |
| apply Finset.sum_congr rfl | |
| intro x hx | |
| rw [Finset.sum_comm] | |
| apply Finset.sum_congr rfl | |
| intro y hy | |
| apply Finset.sum_congr rfl | |
| intro q hq | |
| ring | |
| def leftBlock (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (A : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) (q : Fin r) : | |
| Matrix (Fin mβ) (Fin mβ) F := | |
| fun s t => β x, a q x * A (join x.1 s) (join x.2 t) | |
| def rightBlock (b : Fin r β Fin nβ Γ Fin nβ β F) | |
| (B : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) (q : Fin r) : | |
| Matrix (Fin mβ) (Fin mβ) F := | |
| fun t u => β y, b q y * B (join y.1 t) (join y.2 u) | |
| theorem block_identity | |
| (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) | |
| (c : Fin r β Fin nβ Γ Fin nβ β F) | |
| (h : Tensor.matrixCoefficients (K := F) (Fin nβ) (Fin nβ) (Fin nβ) = | |
| fun x y z => β q, Tensor.rankOne (a q) (b q) (c q) x y z) | |
| (A : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) | |
| (B : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) | |
| (i : Fin nβ) (k : Fin nβ) (s : Fin mβ) (u : Fin mβ) : | |
| (β q, c q (k, i) * ((leftBlock a A q) * (rightBlock b B q)) s u) = | |
| (A * B) (join i s) (join k u) := by | |
| simp only [Matrix.mul_apply, Finset.mul_sum] | |
| rw [Finset.sum_comm] | |
| have hlocal (t : Fin mβ) := rank_bilinear_identity a b c h | |
| (fun x => A (join x.1 s) (join x.2 t)) | |
| (fun y => B (join y.1 t) (join y.2 u)) i k | |
| simp only [leftBlock, rightBlock] | |
| simp_rw [β mul_assoc, hlocal] | |
| rw [Finset.sum_comm] | |
| exact (Fintype.sum_prod_type _).symm.trans | |
| (finProdFinEquiv.sum_comp (fun j => A (join i s) j * B j (join k u))) | |
| end BlockIdentities | |
| section BlockPrograms | |
| variable {nβ nβ nβ r mβ mβ mβ : β} | |
| def inputExpressions (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) : | |
| Fin r Γ MatrixInput mβ mβ mβ β | |
| Expression F (MatrixInput (nβ * mβ) (nβ * mβ) (nβ * mβ)) | |
| | (q, .inl (s, t)) => LinearExpression.linear (a q) | |
| (fun x => .inl (join x.1 s, join x.2 t)) | |
| | (q, .inr (t, u)) => LinearExpression.linear (b q) | |
| (fun y => .inr (join y.1 t, join y.2 u)) | |
| def inputFamily (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) : | |
| Family F (MatrixInput (nβ * mβ) (nβ * mβ) (nβ * mβ)) | |
| (Fin r Γ MatrixInput mβ mβ mβ) := | |
| Family.expressions (inputExpressions a b) | |
| theorem inputFamily_eval (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) | |
| (A : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) | |
| (B : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) | |
| (q : Fin r) (x : MatrixInput mβ mβ mβ) : | |
| (inputFamily a b).eval (matrixInputs A B) (q, x) = | |
| matrixInputs (leftBlock a A q) (rightBlock b B q) x := by | |
| cases x with | |
| | inl x => | |
| simp only [inputFamily, Family.eval_expressions, inputExpressions, | |
| LinearExpression.eval_linear, matrixInputs, leftBlock] | |
| | inr x => | |
| simp only [inputFamily, Family.eval_expressions, inputExpressions, | |
| LinearExpression.eval_linear, matrixInputs, rightBlock] | |
| theorem inputFamily_cost (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) : | |
| (inputFamily (mβ := mβ) (mβ := mβ) (mβ := mβ) a b).cost = | |
| 2 * r * (nβ * nβ * mβ * mβ + nβ * nβ * mβ * mβ) := by | |
| rw [inputFamily, Family.cost_expressions] | |
| simp only [Fintype.sum_prod_type, Fintype.sum_sum_type, | |
| inputExpressions, LinearExpression.cost_linear, Fintype.card_prod, | |
| Fintype.card_fin, Finset.sum_const, Finset.card_univ, smul_eq_mul] | |
| ring | |
| def recursiveProducts (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) (p : MatrixAlgorithm F mβ mβ mβ) : | |
| Family F (MatrixInput (nβ * mβ) (nβ * mβ) (nβ * mβ)) | |
| (Fin r Γ (Fin mβ Γ Fin mβ)) := | |
| (inputFamily a b).compose (Family.copies r (Family.ofMatrix p)) | |
| theorem recursiveProducts_eval (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) | |
| (p : MatrixAlgorithm F mβ mβ mβ) (hp : p.Correct) | |
| (A : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) | |
| (B : Matrix (Fin (nβ * mβ)) (Fin (nβ * mβ)) F) | |
| (q : Fin r) (s : Fin mβ) (u : Fin mβ) : | |
| (recursiveProducts a b p).eval (matrixInputs A B) (q, (s, u)) = | |
| ((leftBlock a A q) * (rightBlock b B q)) s u := by | |
| rw [recursiveProducts, Family.eval_compose, Family.eval_copies, | |
| Family.eval_ofMatrix _ hp] | |
| simp only [inputFamily_eval, matrixInputs, Matrix.mul_apply] | |
| theorem recursiveProducts_cost (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) (p : MatrixAlgorithm F mβ mβ mβ) : | |
| (recursiveProducts a b p).cost = | |
| 2 * r * (nβ * nβ * mβ * mβ + nβ * nβ * mβ * mβ) + r * p.cost := by | |
| rw [recursiveProducts, Family.cost_compose, inputFamily_cost, | |
| Family.cost_copies, Family.cost_ofMatrix] | |
| def outputExpressions (c : Fin r β Fin nβ Γ Fin nβ β F) : | |
| Fin (nβ * mβ) Γ Fin (nβ * mβ) β Expression F (Fin r Γ (Fin mβ Γ Fin mβ)) := | |
| fun o => LinearExpression.linear | |
| (fun q => c q ((finProdFinEquiv.symm o.2).1, (finProdFinEquiv.symm o.1).1)) | |
| (fun q => (q, ((finProdFinEquiv.symm o.1).2, (finProdFinEquiv.symm o.2).2))) | |
| def outputFamily (c : Fin r β Fin nβ Γ Fin nβ β F) : | |
| Family F (Fin r Γ (Fin mβ Γ Fin mβ)) (Fin (nβ * mβ) Γ Fin (nβ * mβ)) := | |
| Family.expressions (outputExpressions c) | |
| theorem outputFamily_eval (c : Fin r β Fin nβ Γ Fin nβ β F) | |
| (inputs : Fin r Γ (Fin mβ Γ Fin mβ) β F) | |
| (i : Fin nβ) (k : Fin nβ) (s : Fin mβ) (u : Fin mβ) : | |
| (outputFamily c).eval inputs (join i s, join k u) = | |
| β q, c q (k, i) * inputs (q, (s, u)) := by | |
| simp only [outputFamily, Family.eval_expressions, outputExpressions, | |
| LinearExpression.eval_linear, join, Equiv.symm_apply_apply] | |
| theorem outputFamily_cost (c : Fin r β Fin nβ Γ Fin nβ β F) : | |
| (outputFamily (mβ := mβ) (mβ := mβ) c).cost = 2 * r * nβ * nβ * mβ * mβ := by | |
| simp only [outputFamily, Family.cost_expressions, outputExpressions, | |
| LinearExpression.cost_linear, Fintype.card_fin, Finset.sum_const, | |
| Finset.card_univ, Fintype.card_prod, smul_eq_mul] | |
| ring | |
| def algorithm (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) (c : Fin r β Fin nβ Γ Fin nβ β F) | |
| (p : MatrixAlgorithm F mβ mβ mβ) : | |
| MatrixAlgorithm F (nβ * mβ) (nβ * mβ) (nβ * mβ) := | |
| let f := (recursiveProducts a b p).compose (outputFamily c) | |
| β¨f.registers, f.program, fun i k => f.output (i, k)β© | |
| theorem algorithm_correct | |
| (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) (c : Fin r β Fin nβ Γ Fin nβ β F) | |
| (h : Tensor.matrixCoefficients (K := F) (Fin nβ) (Fin nβ) (Fin nβ) = | |
| fun x y z => β q, Tensor.rankOne (a q) (b q) (c q) x y z) | |
| (p : MatrixAlgorithm F mβ mβ mβ) (hp : p.Correct) : (algorithm a b c p).Correct := by | |
| intro A B | |
| funext i k | |
| obtain β¨β¨i, sβ©, rflβ© := finProdFinEquiv.surjective i | |
| obtain β¨β¨k, uβ©, rflβ© := finProdFinEquiv.surjective k | |
| change ((recursiveProducts a b p).compose (outputFamily c)).eval | |
| (matrixInputs A B) (join i s, join k u) = _ | |
| rw [Family.eval_compose, outputFamily_eval] | |
| simp_rw [recursiveProducts_eval a b p hp A B] | |
| exact block_identity a b c h A B i k s u | |
| theorem algorithm_cost (a : Fin r β Fin nβ Γ Fin nβ β F) | |
| (b : Fin r β Fin nβ Γ Fin nβ β F) (c : Fin r β Fin nβ Γ Fin nβ β F) | |
| (p : MatrixAlgorithm F mβ mβ mβ) : | |
| (algorithm a b c p).cost = r * p.cost + | |
| 2 * r * (nβ * nβ * mβ * mβ + nβ * nβ * mβ * mβ + nβ * nβ * mβ * mβ) := by | |
| change ((recursiveProducts a b p).compose (outputFamily c)).cost = _ | |
| rw [Family.cost_compose, recursiveProducts_cost, outputFamily_cost] | |
| ring | |
| theorem rank_block_step | |
| (h : Tensor.RankAtMost (Tensor.matrixCoefficients (K := F) (Fin nβ) (Fin nβ) (Fin nβ)) r) | |
| (p : MatrixAlgorithm F mβ mβ mβ) (hp : p.Correct) : | |
| β q : MatrixAlgorithm F (nβ * mβ) (nβ * mβ) (nβ * mβ), q.Correct β§ | |
| q.cost = r * p.cost + | |
| 2 * r * (nβ * nβ * mβ * mβ + nβ * nβ * mβ * mβ + nβ * nβ * mβ * mβ) := by | |
| rcases h with β¨a, b, c, hβ© | |
| exact β¨algorithm a b c p, algorithm_correct a b c h p hp, algorithm_cost a b c pβ© | |
| end BlockPrograms | |
| end MatrixMultiplication.Arithmetic.RecursiveBlock | |
| end | |
| end | |
| end MatrixAllFields | |
| end OAI | |