File size: 3,678 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
import OAI.LinearAlgebra.MatrixMultiplication.Completion.Scripts
import Mathlib.Data.Fintype.Card
import Mathlib.Tactic.FieldSimp

namespace OAI

/-! Readable tensor completion and its finite arithmetic realization. -/

noncomputable section

namespace MatrixMultiplication.CompletionPartitions

open MatrixMultiplication.Foundation RecursiveCompletion
attribute [local instance 10000] Classical.propDecidable Classical.decEq
attribute [local instance 11000] instDecidableEqFin

variable {X Y Z : Type*} [Fintype X]

def activeCount (S : FlaggedTensor X Y Z) : ℕ := Fintype.card {x // S.flagB x}
def inactiveCount (S : FlaggedTensor X Y Z) : ℕ := Fintype.card {x // ¬ S.flagB x}

theorem partition_count (S : FlaggedTensor X Y Z) :
    activeCount S + inactiveCount S = Fintype.card X := by
  rw [activeCount, inactiveCount, Fintype.card_subtype_compl]
  exact Nat.add_sub_of_le (Fintype.card_subtype_le S.flagB)

theorem card_all (p : X → Prop) (m : ℕ) :
    Fintype.card {x : Fin m → X // ∀ i, p (x i)} = Fintype.card {x // p x} ^ m := by
  calc
    _ = Fintype.card (Fin m → {x // p x}) :=
      Fintype.card_congr Equiv.subtypePiEquivPi
    _ = _ := by simp

theorem activeCount_minus (S : FlaggedTensor X Y Z) (m : ℕ) :
    activeCount (complete S .B m) = activeCount S ^ m := by
  simpa [activeCount, complete, flagB, raisedFlag] using card_all S.flagB m

theorem inactiveCount_plus (S : FlaggedTensor X Y Z) (center : Color)
    (hc : center ≠ .B) (m : ℕ) :
    inactiveCount (complete S center m) = inactiveCount S ^ m := by
  change Fintype.card {x : Fin m → X // ¬ raisedFlag center .B (fun i => S.flagB (x i))} = _
  calc
    _ = Fintype.card {x : Fin m → X // ∀ i, ¬ S.flagB (x i)} :=
      Fintype.card_congr (Equiv.subtypeEquivRight fun x => by simp [raisedFlag, hc])
    _ = Fintype.card (Fin m → {x // ¬ S.flagB x}) :=
      Fintype.card_congr
        { toFun := fun w i => ⟨w.val i, w.property i⟩
          invFun := fun w => ⟨fun i => (w i).val, fun i => (w i).property⟩
          left_inv := fun _ => rfl
          right_inv := fun _ => rfl }
    _ = _ := by simp [inactiveCount]

def activeFraction (S : FlaggedTensor X Y Z) : ℝ :=
  (activeCount S : ℝ) / Fintype.card X

def inactiveFraction (S : FlaggedTensor X Y Z) : ℝ :=
  (inactiveCount S : ℝ) / Fintype.card X

theorem fraction_sum [Nonempty X] (S : FlaggedTensor X Y Z) :
    activeFraction S + inactiveFraction S = 1 := by
  have hc : (Fintype.card X : ℝ) ≠ 0 := (Nat.cast_pos.mpr Fintype.card_pos).ne'
  rw [activeFraction, inactiveFraction, ← add_div, ← Nat.cast_add, partition_count]
  exact div_self hc

theorem activeFraction_minus (S : FlaggedTensor X Y Z) (m : ℕ) :
    activeFraction (complete S .B m) = activeFraction S ^ m := by
  simp only [activeFraction, activeCount_minus, Fintype.card_fun,
    Fintype.card_fin, Nat.cast_pow, div_pow]

theorem inactiveFraction_plus (S : FlaggedTensor X Y Z) (center : Color)
    (hc : center ≠ .B) (m : ℕ) :
    inactiveFraction (complete S center m) = inactiveFraction S ^ m := by
  simp only [inactiveFraction, inactiveCount_plus S center hc m,
    Fintype.card_fun, Fintype.card_fin, Nat.cast_pow, div_pow]

theorem activeFraction_plus [Nonempty X] (S : FlaggedTensor X Y Z)
    (center : Color) (hc : center ≠ .B) (m : ℕ) :
    activeFraction (complete S center m) = 1 - (1 - activeFraction S) ^ m := by
  have h := fraction_sum (complete S center m)
  rw [inactiveFraction_plus S center hc m] at h
  have hold : inactiveFraction S = 1 - activeFraction S := by
    linarith [fraction_sum S]
  rw [hold] at h
  linarith

end MatrixMultiplication.CompletionPartitions

end

end OAI