Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/ComplexArithmetic/Complexity.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 4.57 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/ComplexArithmetic/Complexity.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/ComplexArithmetic/Complexity.lean
-
curl -L -o Complexity.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/ComplexArithmetic/Complexity.lean
4.57 kB
| 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 | |