import YM.Slice.Calendar /-! Cyclic gap arithmetic. Put moving point first; at least two on circle. -/ set_option autoImplicit false noncomputable section namespace YM.Slice.Time open Function Set MeasureTheory Lattice Wilson open scoped Topology BigOperators Matrix.Norms.Frobenius variable {a : Axis} {G : Gauge} {m : ℕ} {n : Side} lemma success {B : Fin (m+1) → Brick a G n} {Y : Fin (m+1) → ℕ} {b : Fin (m+1) → Per n} (h : ∀ i : Fin m, b i.succ = b i.castSucc + (B i.castSucc).width + Y i.castSucc) (U : Config G n) : Time.word (drive B Y) (b 0) U = ∏ i, (B i).value (b i) U := by induction m with | zero => rw [welded,distributed] simp only [Time.read,crossing,mul_one]; simp [drive] | succ k ih => rw [welded,Time.distributed,Time.read,crossing,mul_one] have hp (i : Fin k) : Fin.tail b i.succ = Fin.tail b i.castSucc + (Fin.tail B i.castSucc).width + Fin.tail Y i.castSucc := h i.succ have ht : b 0 + ((B 0).width : Per n) + (Y 0:Per n) = Fin.tail b 0 := (h 0).symm rw [starts,duration_temp,Nat.cast_add, ← add_assoc,ht,ih hp] conv_rhs => rw [Fin.prod_univ_succ] rfl /-- Arithmetic natural distances on sorted circle. -/ def minutes (x : Fin (m+1)→Per n) (i : Fin (m+1)) := (x i-x 0).val def sectionGap (x : Fin (m+1) → Per n) (i : Fin m) := minutes x i.succ - minutes x i.castSucc def finalGap (x : Fin (m+1)→ Per n) := n.val - minutes x (Fin.last m) def cyclicGaps (x : Fin (m+1)→ Per n) := (Fin.snoc (α := fun _ => ℕ) (sectionGap x) (finalGap x)) lemma section_rel {x : Fin (m+1) → Per n} (h : StrictMono (minutes x)) (i : Fin m) : minutes x i.succ = minutes x i.castSucc + sectionGap x i := by have he : i.castSucc < i.succ := Fin.castSucc_lt_succ have hh := h he; unfold sectionGap; omega lemma section_duration {x : Fin (m+1) → Per n} (h : StrictMono (minutes x)) (i : Fin m) : sectionGap x i=(x i.succ - x i.castSucc).val := by have hx : sectionGap x i ∏ i, (B i).value (X i) U) - moment β (fun U => ∏ i, (B i).value (x i) U)‖ ≤ (∏ i, K i)/r*(l:ℝ) := by intro X let Y := fun i => cyclicGaps x i - w have hh (i) : (w:ℝ)+2*r < cyclicGaps x i := by refine Fin.lastCases ?_ (fun i => ?_) i · rw [cyclicGaps,Fin.snoc_last,last_val D N]; exact he _ _ (ne_of_gt (by simpa [Fin.lt_def])) rw [cyclicGaps, Fin.snoc_castSucc, section_duration D]; exact he _ _ (Fin.castSucc_lt_succ).ne have hw (i) : w + Y i = cyclicGaps x i := by apply Nat.add_sub_of_le; apply Nat.le_of_lt; exact Nat.cast_lt.mp (lt_trans (by linarith) (hh i)) have hv (i) : 2*r < Y i := by have hh := hh i; rw [← hw,Nat.cast_add] at hh;linarith let j := Fin.last m let Z (i : Fin (m+1)) := if i=j then Y j + l else if i=0 then Y 0-l else Y i have hj : j≠0 := ne_of_gt (by unfold j;simpa [Fin.lt_def]) have hO : (l:ℝ) w+Y i from funext fun i => (hw i).symm] -- rhs Y unfolds? simp [Finset.sum_add_distrib] have hZ : (m+1)*w+∑ i, Z i=n.val := by suffices e : ∑ i, Z i=∑ i, Y i by rw [e,hY] have he i : Z i + (if i=0 then l else 0)=Y i+(if i=j then l else 0) := by by_cases h:i=0 · subst i; rw [if_pos rfl,if_neg hj.symm,hx0];simp by_cases e:i=j · subst i; simp [h,hxj] simp [h,e,hx] have hv := Finset.sum_congr (rfl : Finset.univ = _) fun i _ => he i simpa [Finset.sum_add_distrib] using hv have hf (i : Fin m) : x i.succ=x i.castSucc + ↑(B i.castSucc).width+(Y i.castSucc:Per n) := by rw [hB,add_assoc,← Nat.cast_add,hw,cyclicGaps, Fin.snoc_castSucc,section_duration D];simp [Per] have hF (i : Fin m) : X i.succ=X i.castSucc + ↑(B i.castSucc).width+(Z i.castSucc:Per n) := by rw [show X i.succ=x i.succ by simp [X],hf] by_cases H:i.castSucc=0 · rw [H,← hx0,show X 0=x 0+l by simp [X],Nat.cast_add]; ring rw [hx i.castSucc (show i.castSucc≠j from (Fin.castSucc_lt_last i).ne) H, show X i.castSucc=x i.castSucc by simp [X,H]] have hg : 0 (hx i hi h).symm) r hr hv hL (X 0) (x 0) _ K hK end YM.Slice.Time