File size: 2,894 Bytes
f18e7de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import OAI.LinearAlgebra.MatrixMultiplication.Tensor.ComplexTensorRestrictionComposition
import Mathlib.Tactic.SplitIfs

namespace OAI

/-! Finite coefficient tensors and their algebraic transformations. -/

open scoped BigOperators
open MatrixMultiplication.Foundation

namespace MatrixMultiplication.Replication

variable {K I R X Y Z X' Y' Z' : Type*} [CommSemiring K]

theorem restrict_directSum
    [Fintype I] [DecidableEq I] [Fintype X] [Fintype Y] [Fintype Z]
    (T : I → Tensor K X Y Z)
    (a : I → X' → X → K) (b : I → Y' → Y → K) (c : I → Z' → Z → K) :
    Tensor.restrict
      (fun x s => if x.1 = s.1 then a x.1 x.2 s.2 else 0)
      (fun y s => if y.1 = s.1 then b y.1 y.2 s.2 else 0)
      (fun z s => if z.1 = s.1 then c z.1 z.2 s.2 else 0)
      (Tensor.directSum T) = Tensor.directSum (fun i => Tensor.restrict (a i) (b i) (c i) (T i)) := by
  classical
  funext x y z
  rcases x with ⟨i, x⟩
  rcases y with ⟨j, y⟩
  rcases z with ⟨k, z⟩
  by_cases hij : i = j
  · subst j
    by_cases hik : i = k
    · subst k
      simp [Tensor.restrict, Tensor.directSum, Fintype.sum_prod_type,
        ite_and, ite_mul, mul_ite]
    · simp [Tensor.restrict, Tensor.directSum, Fintype.sum_prod_type,
        ite_and, ite_mul, mul_ite, hik]
  · simp [Tensor.restrict, Tensor.directSum, Fintype.sum_prod_type,
      ite_and, ite_mul, mul_ite, hij]
    intro hjk hik
    exact (hij (hik.trans hjk.symm)).elim

theorem swap_replication [DecidableEq I] [DecidableEq R]
    (T : I → Tensor K X Y Z) :
    Tensor.pullback
      (fun x : I × (R × X) => (x.2.1, (x.1, x.2.2)))
      (fun y : I × (R × Y) => (y.2.1, (y.1, y.2.2)))
      (fun z : I × (R × Z) => (z.2.1, (z.1, z.2.2)))
      (Tensor.directSum (fun _ : R => Tensor.directSum T)) =
      Tensor.directSum (fun i => Tensor.directSum (fun _ : R => T i)) := by
  funext x y z
  simp only [Tensor.pullback, Tensor.directSum]
  split_ifs <;> simp_all

theorem repair_branches
    [Fintype I] [DecidableEq I] [Fintype R] [DecidableEq R]
    [Fintype X] [Fintype Y] [Fintype Z]
    (T : I → Tensor K X Y Z) (Q : I → Tensor K X' Y' Z')
    (a : I → X' → (R × X) → K) (b : I → Y' → (R × Y) → K)
    (c : I → Z' → (R × Z) → K)
    (h : ∀ i, Tensor.restrict (a i) (b i) (c i)
      (Tensor.directSum (fun _ : R => T i)) = Q i) :
    ∃ (A : (I × X') → (R × (I × X)) → K)
      (B : (I × Y') → (R × (I × Y)) → K)
      (C : (I × Z') → (R × (I × Z)) → K),
      Tensor.restrict A B C (Tensor.directSum (fun _ : R => Tensor.directSum T)) =
        Tensor.directSum Q := by
  classical
  have hb := restrict_directSum (fun i => Tensor.directSum (fun _ : R => T i)) a b c
  simp_rw [h] at hb
  rw [← swap_replication T, Tensor.pullback_eq_restrict, Tensor.restrict_restrict] at hb
  exact ⟨_, _, _, hb⟩

end MatrixMultiplication.Replication

end OAI