yang
This project has not described itself yet.
Brought by drakestapleton
yang / proofs / DyadicEnergyReplay.lean
| 1 | import DyadicPolarFlux |
| 2 | import Mathlib.Algebra.BigOperators.Intervals |
| 3 | |
| 4 | open YangMills.DyadicExp YangMills.DyadicPartition YangMills.DyadicPolarFlux |
| 5 | |
| 6 | namespace YangMills.DyadicEnergyReplay |
| 7 | |
| 8 | /-- Materialize the angular data once for the two sweeps. -/ |
| 9 | @[noinline] def cache {α : Type} (f : ℕ → α) : Array α := |
| 10 | Array.ofFn (fun i : Fin 2048 => f i) |
| 11 | |
| 12 | def atIndex {α : Type} [Inhabited α] (a : Array α) (i : ℕ) := a[i]?.getD default |
| 13 | |
| 14 | theorem cache_eq {α : Type} [Inhabited α] (f : ℕ → α) {i : ℕ} (hi : i < 2048) : |
| 15 | atIndex (cache f) i = f i := by simp [atIndex,cache,hi] |
| 16 | |
| 17 | def total (f : ℕ → ℤ) : ℕ → ℤ |
| 18 | | 0 => 0 |
| 19 | | n+1 => total f n + f n |
| 20 | |
| 21 | theorem total_eq (f : ℕ → ℤ) (n : ℕ) : total f n = ∑ i ∈ Finset.range n, f i := by |
| 22 | induction n with |
| 23 | | zero => simp [total] |
| 24 | | succ n ih => simp [total,ih,Finset.sum_range_succ] |
| 25 | |
| 26 | structure Row where |
| 27 | cell : Cell |
| 28 | wl : ℤ |
| 29 | wh : ℤ |
| 30 | deriving Inhabited |
| 31 | |
| 32 | def row (c : Cell) (rl rh : ℤ) : Row := ⟨c,weightLo c rl rh,weightHi c rl rh⟩ |
| 33 | |
| 34 | structure Scan where |
| 35 | lo : ℤ |
| 36 | hi : ℤ |
| 37 | cost : ℤ |
| 38 | |
| 39 | def sweep (read : ℕ → Row) (back : Bool) (ml mh : ℤ) : ℕ → Scan |
| 40 | | 0 => ⟨0,0,0⟩ |
| 41 | | j+1 => |
| 42 | let s := sweep read back ml mh j |
| 43 | let a := read (index back j) |
| 44 | let dl := lowerSlope back a.cell ml mh a.wl a.wh |
| 45 | let dh := upperSlope back a.cell ml mh a.wl a.wh |
| 46 | ⟨s.lo+dl,s.hi+dh,s.cost + cellCost j a.wh a.wl (fluxCap s.lo s.hi dl dh)⟩ |
| 47 | |
| 48 | theorem sweep_eq (read : ℕ → Row) (back : Bool) (rl rh : ℤ) |
| 49 | (lookup : ∀ i < 2048, read i = row (angleCell i) rl rh) {n : ℕ} (hn : n ≤ 1024) : |
| 50 | sweep read back (meanLo rl rh) (meanHi rl rh) n = |
| 51 | ⟨runningSlope true back n rl rh,runningSlope false back n rl rh, |
| 52 | ∑ j ∈ Finset.range n, energyCell back j rl rh⟩ := by |
| 53 | induction n with |
| 54 | | zero => simp [sweep,runningSlope] |
| 55 | | succ n ih => |
| 56 | have hi : index back n < 2048 := by cases back <;> simp [index] <;> omega |
| 57 | simp only [sweep,ih (by omega),lookup _ hi,row,runningSlope,Finset.sum_range_succ, |
| 58 | increment, if_true, Bool.false_eq_true, if_false, energyCell] |
| 59 | |
| 60 | private theorem split_cost (rl rh : ℤ) : |
| 61 | (∑ i ∈ Finset.range 2048, gridCost i rl rh) = |
| 62 | (∑ j ∈ Finset.range 1024, energyCell false j rl rh) + |
| 63 | (∑ j ∈ Finset.range 1024, energyCell true j rl rh) := by |
| 64 | rw [show 2048=1024+1024 by decide, Finset.sum_range_add] |
| 65 | apply congrArg₂ (· + ·) |
| 66 | · apply Finset.sum_congr rfl |
| 67 | intro i hi |
| 68 | simp [gridCost,Finset.mem_range.mp hi] |
| 69 | · rw [← Finset.sum_range_reflect (fun j => energyCell true j rl rh) 1024] |
| 70 | apply Finset.sum_congr rfl |
| 71 | intro i hi |
| 72 | simp only [gridCost,show ¬1024+i<1024 by omega,if_false] |
| 73 | rw [show 2047 - (1024+i) = 1024-1-i by omega] |
| 74 | |
| 75 | /-- Both printed integers, including the rounded normalization and π² factor. -/ |
| 76 | def evaluate (cells : Array Cell) (rl rh : ℤ) : ℤ × ℤ := |
| 77 | let rows := cache (fun i => row (atIndex cells i) rl rh) |
| 78 | let read := atIndex rows |
| 79 | let zl := total (fun i => (read i).wl) 2048 / 2048 |
| 80 | let zh := ceilDiv (total (fun i => (read i).wh) 2048) 2048 |
| 81 | let ml := ratioLo (total (fun i => loMul (read i).cell.cosLo (read i).wl (read i).wh) 2048 / 2048) zl zh |
| 82 | let mh := ratioHi (ceilDiv (total (fun i => hiMul (read i).cell.cosHi (read i).wl (read i).wh) 2048) 2048) zl zh |
| 83 | let trans := ceilDiv (total (fun i => ceilDiv (transverseCap (read i).cell rl rh * (read i).wh) q) 2048) 2048 |
| 84 | let energy := (sweep read false ml mh 1024).cost + (sweep read true ml mh 1024).cost |
| 85 | (ceilDiv (ceilDiv (ceilDiv energy 2048*q) zl * 986961) 100000,ceilDiv (trans*q) zl) |
| 86 | |
| 87 | theorem evaluate_eq (rl rh : ℤ) : |
| 88 | evaluate (cache angleCell) rl rh = |
| 89 | (parallelCap rl rh, ceilDiv (averageHi weightedCap rl rh*q) (averageLo weightLo rl rh)) := by |
| 90 | let read := atIndex (cache (fun i => row (atIndex (cache angleCell) i) rl rh)) |
| 91 | have lookup (i : ℕ) (hi : i < 2048) : read i = row (angleCell i) rl rh := by |
| 92 | simp [read,cache_eq _ hi] |
| 93 | have sum (f : Row → ℤ) : |
| 94 | total (fun i => f (read i)) 2048 = ∑ i ∈ Finset.range 2048, f (row (angleCell i) rl rh) := by |
| 95 | rw [total_eq]; apply Finset.sum_congr rfl |
| 96 | intro i hi; rw [lookup i (Finset.mem_range.mp hi)] |
| 97 | have zl := sum Row.wl |
| 98 | have zh := sum Row.wh |
| 99 | have ml := sum (fun a => loMul a.cell.cosLo a.wl a.wh) |
| 100 | have mh := sum (fun a => hiMul a.cell.cosHi a.wl a.wh) |
| 101 | have tr := sum (fun a => ceilDiv (transverseCap a.cell rl rh*a.wh) q) |
| 102 | change _ = ∑ i ∈ Finset.range 2048, momentLo (angleCell i) rl rh at ml |
| 103 | change _ = ∑ i ∈ Finset.range 2048, momentHi (angleCell i) rl rh at mh |
| 104 | change _ = ∑ i ∈ Finset.range 2048, weightedCap (angleCell i) rl rh at tr |
| 105 | let zl' := averageLo weightLo rl rh |
| 106 | let zh' := averageHi weightHi rl rh |
| 107 | have eqzl : total (fun i => (read i).wl) 2048 / 2048 = zl' := congrArg (fun z : ℤ => z/2048) zl |
| 108 | have eqzh : ceilDiv (total (fun i => (read i).wh) 2048) 2048 = zh' := congrArg (fun z => ceilDiv z 2048) zh |
| 109 | have eqml : ratioLo (total (fun i => loMul (read i).cell.cosLo (read i).wl (read i).wh) 2048 / 2048) zl' zh' = |
| 110 | meanLo rl rh := congrArg (fun z : ℤ => ratioLo (z/2048) zl' zh') ml |
| 111 | have eqmh : ratioHi (ceilDiv (total (fun i => hiMul (read i).cell.cosHi (read i).wl (read i).wh) 2048) 2048) zl' zh' = |
| 112 | meanHi rl rh := congrArg (fun z : ℤ => ratioHi (ceilDiv z 2048) zl' zh') mh |
| 113 | have eqtr : ceilDiv (total (fun i => ceilDiv (transverseCap (read i).cell rl rh * (read i).wh) q) 2048) 2048 = |
| 114 | averageHi weightedCap rl rh := congrArg (fun z => ceilDiv z 2048) tr |
| 115 | have a := sweep_eq read false rl rh lookup (n:=1024) le_rfl |
| 116 | have b := sweep_eq read true rl rh lookup (n:=1024) le_rfl |
| 117 | dsimp only [evaluate] |
| 118 | rw [← (show read = atIndex (cache (fun i => row (atIndex (cache angleCell) i) rl rh)) from rfl)] |
| 119 | rw [eqzl,eqzh,eqml,eqmh,eqtr] |
| 120 | rw [a,b] |
| 121 | simp only |
| 122 | rw [← split_cost] |
| 123 | rfl |
| 124 | |
| 125 | def fieldLo (b k : ℤ) := 6*b*q*k/1280000 |
| 126 | def fieldHi (b k : ℤ) := ceilDiv (6*b*q*(k+1)) 1280000 |
| 127 | |
| 128 | open YangMills.OneLinkTransport |
| 129 | |
| 130 | /-- One of the printed pairs bounds both real trial energies throughout its field box. |
| 131 | The endpoints of adjacent boxes may overlap by one integer rounding unit. -/ |
| 132 | theorem field_bounds {b k : ℤ} (hb : 0 ≤ b) (hb' : b ≤ 2880) (hk : 0 ≤ k) (hk' : k < 128) |
| 133 | {r : ℝ} (left : 6*(b:ℝ)*k/1280000 ≤ r) (right : r ≤ 6*(b:ℝ)*(k+1)/1280000) : |
| 134 | let v := evaluate (cache angleCell) (fieldLo b k) (fieldHi b k) |
| 135 | parallelEnergy r ≤ (v.1:ℝ)/q ∧ transverseEnergy r ≤ (v.2:ℝ)/q := by |
| 136 | have hh := mul_le_mul hb' (show k+1 ≤ 128 by omega) (by omega) (by decide : (0:ℤ) ≤ 2880) |
| 137 | have l0 : 0 ≤ fieldLo b k := by unfold fieldLo q; apply Int.ediv_nonneg <;> positivity |
| 138 | have lnum := floor_mul_bound (6*b*q*k) (show (0:ℤ)<1280000 by decide) |
| 139 | have hnum := ceil_mul_bound (6*b*q*(k+1)) (show (0:ℤ)<1280000 by decide) |
| 140 | change (fieldLo b k:ℝ)*_ ≤ _ at lnum |
| 141 | change _ ≤ (fieldHi b k:ℝ)*_ at hnum |
| 142 | push_cast at lnum hnum |
| 143 | have low : (fieldLo b k:ℝ)/q ≤ r := by norm_num [q] at lnum ⊢; linarith |
| 144 | have high : r ≤ (fieldHi b k:ℝ)/q := by norm_num [q] at hnum ⊢; linarith |
| 145 | have ord : fieldLo b k ≤ fieldHi b k := by |
| 146 | have pos : (0:ℝ)<q := by norm_num [q] |
| 147 | exact_mod_cast (div_le_div_iff_of_pos_right pos).mp (low.trans high) |
| 148 | have up : fieldHi b k ≤ 2*q := by |
| 149 | unfold fieldHi ceilDiv |
| 150 | have bound : 6*b*q*(k+1) ≤ 2*q*1280000 := by norm_num [q] at *; nlinarith |
| 151 | omega |
| 152 | have hr : 0 ≤ r := (div_nonneg (by exact_mod_cast l0) (by norm_num [q])).trans low |
| 153 | rw [evaluate_eq] |
| 154 | refine ⟨normalized_parallel hr low high l0 ord up,?_⟩ |
| 155 | have cost (t : ℝ) : transverseCost r (Real.cos t) = transverseTrial r t := by |
| 156 | unfold transverseCost transverseTrial |
| 157 | rw [Real.sin_sq]; ring |
| 158 | unfold transverseEnergy |
| 159 | simp only [cost] |
| 160 | exact normalized_transverse hr low high l0 ord up |
| 161 | |
| 162 | #print axioms field_bounds |
| 163 | #print axioms evaluate_eq |
| 164 | |
| 165 | end YangMills.DyadicEnergyReplay |