File size: 3,695 Bytes
cdec422 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | import OAI.LinearAlgebra.MatrixMultiplication.Polynomial.ComplexExpressionFamily
import Mathlib.Tactic.NormNum
namespace OAI
/-! Complex arithmetic programs and asymptotic matrix multiplication costs. -/
noncomputable section
open scoped BigOperators
namespace MatrixMultiplication.Foundation.Arithmetic
namespace Expression
variable {Input : Type*}
def sumFin : {n : ℕ} → (Fin n → Expression Input) → Expression Input
| 0, _ => .constant 0
| n + 1, f => .add (f 0) (sumFin (fun i : Fin n => f i.succ))
theorem sumFin_eval {n : ℕ} (f : Fin n → Expression Input) (inputs : Input → ℂ) :
(sumFin f).eval inputs = ∑ i, (f i).eval inputs := by
induction n with
| zero => simp [sumFin, eval]
| succ n ih => simp [sumFin, eval, ih, Fin.sum_univ_succ]
theorem sumFin_cost {n : ℕ} (f : Fin n → Expression Input) :
(sumFin f).cost = (∑ i, (f i).cost) + n := by
induction n with
| zero => simp [sumFin, cost]
| succ n ih =>
simp only [sumFin, cost, ih, Fin.sum_univ_succ]
omega
end Expression
def naiveEntry {n : ℕ} (i k : Fin n) : Expression (MatrixInput n) :=
Expression.sumFin fun j : Fin n =>
.mul (.input (.inl (i, j))) (.input (.inr (j, k)))
theorem naiveEntry_eval {n : ℕ} (i k : Fin n)
(A B : Matrix (Fin n) (Fin n) ℂ) :
(naiveEntry i k).eval (matrixInputs A B) = ∑ j, A i j * B j k := by
simp [naiveEntry, Expression.sumFin_eval, Expression.eval, matrixInputs]
@[simp] theorem naiveEntry_cost {n : ℕ} (i k : Fin n) :
(naiveEntry i k).cost = 2 * n := by
simp [naiveEntry, Expression.sumFin_cost, Expression.cost, two_mul]
def naiveAlgorithm (n : ℕ) : MatrixAlgorithm n :=
let p := Expression.compileFamily (fun ik : Fin n × Fin n => naiveEntry ik.1 ik.2)
{ registers := p.registers
program := p.program
output := fun i k => p.output (i, k) }
theorem naiveAlgorithm_correct (n : ℕ) : (naiveAlgorithm n).Correct := by
apply (MatrixAlgorithm.correct_iff_entries _).2
intro A B i k
let p := Expression.compileFamily (fun ik : Fin n × Fin n => naiveEntry ik.1 ik.2)
change p.program.eval (matrixInputs A B) (p.output (i, k)) = _
rw [p.correct]
exact naiveEntry_eval i k A B
@[simp] theorem naiveAlgorithm_cost (n : ℕ) : (naiveAlgorithm n).cost = 2 * n ^ 3 := by
let p := Expression.compileFamily (fun ik : Fin n × Fin n => naiveEntry ik.1 ik.2)
change p.program.cost = _
rw [p.cost_eq]
simp only [naiveEntry_cost, Finset.sum_const, Finset.card_univ,
Fintype.card_prod, Fintype.card_fin, nsmul_eq_mul, Nat.cast_id]
ring
theorem naiveAlgorithm_cost_le (n : ℕ) : (naiveAlgorithm n).cost ≤ 2 * n ^ 3 :=
(naiveAlgorithm_cost n).le
theorem admissibleExponent_three : AdmissibleExponent 3 := by
intro ε hε
refine ⟨2, by norm_num, ?_⟩
intro n hn
refine ⟨naiveAlgorithm n, naiveAlgorithm_correct n, ?_⟩
rw [naiveAlgorithm_cost]
have hnR : (1 : ℝ) ≤ (n : ℝ) := by exact_mod_cast hn
have hpow : (n : ℝ) ^ (3 : ℝ) ≤ (n : ℝ) ^ (3 + ε) :=
Real.rpow_le_rpow_of_exponent_le hnR (le_add_of_nonneg_right hε.le)
calc
((2 * n ^ 3 : ℕ) : ℝ) = 2 * (n : ℝ) ^ (3 : ℝ) := by
calc
((2 * n ^ 3 : ℕ) : ℝ) = 2 * (n : ℝ) ^ (3 : ℕ) := by
simp only [Nat.cast_mul, Nat.cast_pow, Nat.cast_ofNat]
_ = 2 * (n : ℝ) ^ (3 : ℝ) :=
congrArg (fun t : ℝ => 2 * t) (Real.rpow_natCast (n : ℝ) 3).symm
_ ≤ 2 * (n : ℝ) ^ (3 + ε) := mul_le_mul_of_nonneg_left hpow (by norm_num)
theorem admissibleExponent_nonempty : Set.Nonempty {τ : ℝ | AdmissibleExponent τ} :=
⟨3, admissibleExponent_three⟩
end MatrixMultiplication.Foundation.Arithmetic
end
end OAI
|