diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWalkFamily.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWalkFamily.lean new file mode 100644 index 0000000000000000000000000000000000000000..5ecd8f9e04d7643b8e9714277805a295c066ffef --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWalkFamily.lean @@ -0,0 +1,145 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.ClosedWords + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave +section TraceBridge +variable {V : Type*} [Fintype V] [DecidableEq V] +variable (G : SimpleGraph V) [DecidableRel G.Adj] + +abbrev closedWalkFamily (r : ℕ) := Σ v : V, {p : G.Walk v v // p.length = r} + +omit [Fintype V] [DecidableEq V] [DecidableRel G.Adj] in + +lemma wordOfWalk_adj {r : ℕ} {a : V} (p : G.Walk a a) (hp : p.length = r) + (i j : Fin r) (hij : (SimpleGraph.cycleGraph r).Adj i j) : + G.Adj (p.getVert i.val) (p.getVert j.val) := by + obtain ⟨x,hx⟩ := i + obtain ⟨y,hy⟩ := j + have hne : x ≠ y := by + intro h + subst y + exact hij.ne rfl + wlog hgt : x > y generalizing x y + · exact (this y hy x hx hij.symm hne.symm (by omega)).symm + rcases SimpleGraph.cycleGraph_adj'.mp hij with h | h + · rw [Fin.sub_val_of_le (show (⟨y, hy⟩ : Fin r) ≤ ⟨x, hx⟩ from hgt.le)] at h + dsimp only at h + have heq : x = y+1 := by omega + subst x + exact (p.adj_getVert_succ (by omega : y < p.length)).symm + · rw [Fin.coe_sub_iff_lt.mpr hgt] at h + dsimp only at h + have hx' : x = r-1 := by omega + have hy' : y = 0 := by omega + subst x; subst y + have hlast := p.adj_getVert_succ (by omega : r-1 < p.length) + simpa only [show r-1+1 = p.length by omega, SimpleGraph.Walk.getVert_length, + SimpleGraph.Walk.getVert_zero] using hlast + +noncomputable def wordOfWalk {r : ℕ} (p : closedWalkFamily G r) : closedWords G r := + ⟨fun i => p.2.1.getVert i.val, by + classical + simp only [closedWords, Finset.mem_filter, Finset.mem_univ, true_and] + exact wordOfWalk_adj G p.2.1 p.2.2⟩ + +omit [DecidableEq V] in +lemma wordOfWalk_injective {r : ℕ} (hr : 0 < r) : + Function.Injective (wordOfWalk (G := G) (r := r)) := by + intro p q hpq + obtain ⟨a,p,hp⟩ := p + obtain ⟨b,q,hq⟩ := q + have hword : (fun i : Fin r => p.getVert i.val) = (fun i : Fin r => q.getVert i.val) := + Subtype.ext_iff.mp hpq + have hab : a = b := by simpa using congrFun hword ⟨0,hr⟩ + subst b + apply congrArg (Sigma.mk a) + apply Subtype.ext + apply SimpleGraph.Walk.ext_getVert_le_length (hp.trans hq.symm) + intro k hk + by_cases hkr : k < r + · exact congrFun hword ⟨k,hkr⟩ + · have heq : k = r := by omega + subst k + simpa only [hp, hq] using p.getVert_length.trans q.getVert_length.symm + +noncomputable def walkOfWord (n : ℕ) (f : closedWords G (n+3)) : + closedWalkFamily G (n+3) := by + let h : SimpleGraph.cycleGraph (n+3) →g G := ⟨f.val, by + have hf := f.property + simpa only [closedWords, Finset.mem_filter, Finset.mem_univ, true_and] using hf⟩ + exact ⟨f.val 0, (SimpleGraph.cycleGraph.cycle n).map h, by + change ((SimpleGraph.cycleGraph.cycle n).map h).length = n+3 + rw [SimpleGraph.Walk.length_map, SimpleGraph.cycleGraph.length_cycle]⟩ + +omit [DecidableEq V] in +lemma walkOfWord_injective (n : ℕ) : Function.Injective (walkOfWord G n) := by + intro f g hfg + apply Subtype.ext + funext i + let k := (SimpleGraph.cycleGraph.cycle n).support.idxOf i + have hk := (SimpleGraph.cycleGraph.cycle n).getVert_support_idxOf + (canonical_cycle_spanning n i) + have hget := congrArg (fun w : closedWalkFamily G (n+3) => w.2.1.getVert k) hfg + simpa [walkOfWord, SimpleGraph.Walk.getVert_map, k, hk] using hget + +theorem trace_adjMatrix_eq_closedWords (n : ℕ) : + Matrix.trace (G.adjMatrix ℕ ^ (n+3)) = (closedWords G (n+3)).card := by + have hcard : Fintype.card (closedWalkFamily G (n+3)) = + Fintype.card (closedWords G (n+3)) := le_antisymm + (Fintype.card_le_of_injective _ (wordOfWalk_injective G (by omega))) + (Fintype.card_le_of_injective _ (walkOfWord_injective G n)) + rw [Fintype.card_coe] at hcard + rw [← hcard, Fintype.card_sigma] + simp only [Matrix.trace, Matrix.diag_apply, SimpleGraph.adjMatrix_pow_apply_eq_card_walk, Nat.cast_id] + apply Finset.sum_congr rfl + intro i hi + exact Fintype.card_congr (Equiv.refl _) + +lemma closedWords_card_eq (r : ℕ) : + (closedWords G r).card = (graphEmbeddings (SimpleGraph.cycleGraph r) G).card + + (nonsimpleWords G r).card := by + classical + have hfun : ((closedWords G r).filter Function.Injective) = + (graphEmbeddings (SimpleGraph.cycleGraph r) G).image (fun (τ : Fin r ↪ V) => (τ : Fin r → V)) := by + ext f + simp only [Finset.mem_filter, closedWords, Finset.mem_univ, true_and, Finset.mem_image] + constructor + · rintro ⟨hf, hi⟩ + exact ⟨⟨f,hi⟩, (mem_graphEmbeddings _).mpr hf, rfl⟩ + · rintro ⟨τ,hτ,rfl⟩ + exact ⟨(mem_graphEmbeddings τ).mp hτ, τ.injective⟩ + have hc := Finset.card_filter_add_card_filter_not (s := closedWords G r) Function.Injective + rw [hfun, Finset.card_image_of_injective _ (fun _ _ h => Function.Embedding.ext (congrFun h))] at hc + exact hc.symm + +theorem link_trace_count_bound (n : ℕ) (Δ C : ℕ) (hΔ0 : 1 ≤ Δ) + (hΔ : ∀ x : V, (Finset.univ.filter (G.Adj x)).card ≤ Δ) + (hcyc : ∀ b, 3 ≤ b → b < n+3 → + (graphEmbeddings (SimpleGraph.cycleGraph b) G).card ≤ C * Δ ^ b) : + Matrix.trace (G.adjMatrix ℕ ^ (n+3)) - + (graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card ≤ + (n+3) ^ (n+4) * (Fintype.card V * Δ ^ ((n+3)/2) + C * Δ ^ (n+2)) := by + rw [trace_adjMatrix_eq_closedWords, closedWords_card_eq, Nat.add_sub_cancel_left] + exact nonsimpleWords_card_le G n Δ C hΔ0 hΔ hcyc + +end TraceBridge +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWalkPattern.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWalkPattern.lean new file mode 100644 index 0000000000000000000000000000000000000000..76bdb0c1bf8718317404cec40f09a83bd1f5bbdd --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWalkPattern.lean @@ -0,0 +1,120 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.GraphEmbeddingsRestriction +import OAI.Combinatorics.TriangleRemoval.Spectral.CountClosedWalk + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave + +theorem canonical_cycle_spanning (n : ℕ) (v : Fin (n+3)) : + v ∈ (SimpleGraph.cycleGraph.cycle n).support := by + let f : Fin (n+3) → Fin (n+3) := fun i => (SimpleGraph.cycleGraph.cycle n).getVert i + have hf : Function.Injective f := by + intro i j hij + apply Fin.ext + exact SimpleGraph.cycleGraph.isCycle_cycle.getVert_injOn' + (by simp only [Set.mem_ofPred_eq, SimpleGraph.cycleGraph.length_cycle]; omega) + (by simp only [Set.mem_ofPred_eq, SimpleGraph.cycleGraph.length_cycle]; omega) hij + obtain ⟨i, hi⟩ := Finite.surjective_of_injective hf v + exact hi ▸ (SimpleGraph.cycleGraph.cycle n).getVert_mem_support i + +section WalkPatterns +variable {r k : ℕ} + +def closedWalkPattern (σ : Fin r → Fin k) : SimpleGraph (Fin k) := + (SimpleGraph.cycleGraph r).map σ + +def PatternProper (σ : Fin r → Fin k) : Prop := + ∀ i j, (SimpleGraph.cycleGraph r).Adj i j → σ i ≠ σ j + +noncomputable def patternHom (σ : Fin r → Fin k) (hσ : PatternProper σ) : + SimpleGraph.cycleGraph r →g closedWalkPattern σ := + SimpleGraph.Hom.map σ _ (fun hij => hσ _ _ hij) + +@[simp] lemma patternHom_apply (σ : Fin r → Fin k) (hσ : PatternProper σ) (i : Fin r) : + patternHom σ hσ i = σ i := rfl + +lemma pattern_preconnected (σ : Fin r → Fin k) (hσ : PatternProper σ) + (hs : Function.Surjective σ) : (closedWalkPattern σ).Preconnected := + SimpleGraph.Preconnected.map (patternHom σ hσ) hs SimpleGraph.cycleGraph_preconnected + +theorem pattern_spanning_walk {n k : ℕ} (σ : Fin (n+3) → Fin k) + (hσ : PatternProper σ) (hs : Function.Surjective σ) : + ∃ a : Fin k, ∃ p : (closedWalkPattern σ).Walk a a, + p.length = n+3 ∧ ∀ v, v ∈ p.support := by + let p := (SimpleGraph.cycleGraph.cycle n).map (patternHom σ hσ) + refine ⟨σ 0, p, ?_, ?_⟩ + · change ((SimpleGraph.cycleGraph.cycle n).map (patternHom σ hσ)).length = n+3 + rw [SimpleGraph.Walk.length_map, SimpleGraph.cycleGraph.length_cycle] + · intro v + obtain ⟨i, rfl⟩ := hs v + change σ i ∈ ((SimpleGraph.cycleGraph.cycle n).map (patternHom σ hσ)).support + rw [SimpleGraph.Walk.support_map] + exact List.mem_map.mpr ⟨i, canonical_cycle_spanning n i, rfl⟩ + +end WalkPatterns + +section PatternCounting +variable {A V : Type*} [Fintype A] [DecidableEq A] [Fintype V] [DecidableEq V] +variable (H : SimpleGraph A) (G : SimpleGraph V) [DecidableRel G.Adj] + +theorem noninjective_pattern_count (r : ℕ) (hH : H.Preconnected) + (hsize : Fintype.card A < r) (a : A) (p : H.Walk a a) + (hlength : p.length = r) (hspan : ∀ v, v ∈ p.support) + (Δ C : ℕ) (hΔ0 : 1 ≤ Δ) + (hΔ : ∀ x : V, (Finset.univ.filter (G.Adj x)).card ≤ Δ) + (hcyc : ∀ b, 3 ≤ b → b < r → + (graphEmbeddings (SimpleGraph.cycleGraph b) G).card ≤ C * Δ ^ b) : + (graphEmbeddings H G).card ≤ + Fintype.card V * Δ ^ (r / 2) + C * Δ ^ (r-1) := by + classical + by_cases htree : H.IsAcyclic + · have hv := acyclic_spanning_walk_vertices p htree hspan + rw [hlength] at hv + calc + _ ≤ Fintype.card V * Δ ^ (Fintype.card A - 1) := + connected_embedding_count H G hH a Δ hΔ + _ ≤ Fintype.card V * Δ ^ (r/2) := + Nat.mul_le_mul_left _ (Nat.pow_le_pow_right hΔ0 hv) + _ ≤ _ := Nat.le_add_right _ _ + · obtain ⟨v, c, hc⟩ : ∃ v : A, ∃ c : H.Walk v v, c.IsCycle := by + simpa only [SimpleGraph.IsAcyclic, not_forall, not_not] using htree + have hb : 3 ≤ c.length := hc.three_le_length + obtain ⟨f⟩ := (SimpleGraph.cycleGraph_isContained_iff (by omega : 2 < c.length)).mpr + ⟨v, c, hc, rfl⟩ + have hcard : c.length ≤ Fintype.card A := by + simpa using Fintype.card_le_of_injective f.toEmbedding f.injective + have hh := cyclic_seed_embedding_count H G hH (SimpleGraph.cycleGraph c.length) + f.toEmbedding (by exact Fin.pos_iff_nonempty.mp (by omega)) + (fun i j hij => f.toHom.map_adj hij) Δ hΔ + have hp : Δ ^ c.length * Δ ^ (Fintype.card A - c.length) = + Δ ^ Fintype.card A := by rw [← pow_add, Nat.add_sub_of_le hcard] + calc + _ ≤ (graphEmbeddings (SimpleGraph.cycleGraph c.length) G).card * + Δ ^ (Fintype.card A-c.length) := by simpa using hh + _ ≤ (C * Δ ^ c.length) * Δ ^ (Fintype.card A-c.length) := + Nat.mul_le_mul_right _ (hcyc _ hb (hcard.trans_lt hsize)) + _ = C * Δ ^ Fintype.card A := by rw [mul_assoc, hp] + _ ≤ C * Δ ^ (r-1) := Nat.mul_le_mul_left _ + (Nat.pow_le_pow_right hΔ0 (by omega)) + _ ≤ _ := Nat.le_add_left _ _ + +end PatternCounting +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWords.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWords.lean new file mode 100644 index 0000000000000000000000000000000000000000..1afa04078eb1d3e4987e4c37905475032916c6d2 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ClosedWords.lean @@ -0,0 +1,123 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.ClosedWalkPattern + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave +section ClosedWords +variable {V : Type*} [Fintype V] [DecidableEq V] +variable (G : SimpleGraph V) [DecidableRel G.Adj] + +noncomputable def closedWords (r : ℕ) : Finset (Fin r → V) := by + classical + exact Finset.univ.filter (fun f => ∀ i j, (SimpleGraph.cycleGraph r).Adj i j → + G.Adj (f i) (f j)) + +noncomputable def nonsimpleWords (r : ℕ) : Finset (Fin r → V) := by + classical + exact (closedWords G r).filter (fun f => ¬Function.Injective f) + +theorem nonsimple_word_factor {r : ℕ} {f : Fin r → V} + (hf : f ∈ nonsimpleWords G r) : + ∃ k : Fin r, ∃ σ : Fin r → Fin k.val, + PatternProper σ ∧ Function.Surjective σ ∧ + ∃ τ ∈ graphEmbeddings (closedWalkPattern σ) G, (fun i => τ (σ i)) = f := by + classical + have hfadj : ∀ i j, (SimpleGraph.cycleGraph r).Adj i j → G.Adj (f i) (f j) := by + simpa only [nonsimpleWords, closedWords, Finset.mem_filter, Finset.mem_univ, + true_and] using (Finset.mem_filter.mp hf).1 + have hnot := (Finset.mem_filter.mp hf).2 + let S := Finset.univ.image f + have hcard : S.card < r := by + have hle : S.card ≤ r := by simpa [S] using (Finset.card_image_le (s := Finset.univ) (f := f)) + refine lt_of_le_of_ne hle ?_ + intro heq + apply hnot + have hi : Set.InjOn f (↑(Finset.univ : Finset (Fin r))) := + Finset.card_image_iff.mp (by simpa [S] using heq) + exact fun i j hij => hi (by simp) (by simp) hij + let e : S ≃ Fin S.card := (Fintype.equivFin S).trans (finCongr (Fintype.card_coe S)) + let σ : Fin r → Fin S.card := fun i => e ⟨f i, Finset.mem_image.mpr ⟨i, by simp, rfl⟩⟩ + let τ : Fin S.card ↪ V := e.symm.toEmbedding.trans (Function.Embedding.subtype _) + have hτσ : ∀ i, τ (σ i) = f i := by intro i; simp [τ, σ] + have hs : Function.Surjective σ := by + intro a + obtain ⟨i, _, hi⟩ := Finset.mem_image.mp (e.symm a).property + refine ⟨i, ?_⟩ + apply e.symm.injective + apply Subtype.ext + simpa [σ] using hi + have hp : PatternProper σ := by + intro i j hij heq + exact (hfadj i j hij).ne (by rw [← hτσ i, ← hτσ j, heq]) + refine ⟨⟨S.card, hcard⟩, σ, hp, hs, τ, ?_, funext hτσ⟩ + apply (mem_graphEmbeddings τ).mpr + intro a b hab + obtain ⟨_, i, j, hij, hi, hj⟩ := hab + subst a; subst b + simpa only [hτσ] using hfadj i j hij + +theorem nonsimpleWords_card_le (n : ℕ) (Δ C : ℕ) (hΔ0 : 1 ≤ Δ) + (hΔ : ∀ x : V, (Finset.univ.filter (G.Adj x)).card ≤ Δ) + (hcyc : ∀ b, 3 ≤ b → b < n+3 → + (graphEmbeddings (SimpleGraph.cycleGraph b) G).card ≤ C * Δ ^ b) : + (nonsimpleWords G (n+3)).card ≤ + (n+3) ^ (n+4) * (Fintype.card V * Δ ^ ((n+3)/2) + C * Δ ^ (n+2)) := by + classical + let t : (k : Fin (n+3)) → Finset (Fin (n+3) → Fin k.val) := fun _ => + Finset.univ.filter (fun σ => PatternProper σ ∧ Function.Surjective σ) + let R := fun (k : Fin (n+3)) (σ : Fin (n+3) → Fin k.val) => + (graphEmbeddings (closedWalkPattern σ) G).image (fun τ i => τ (σ i)) + have hsub : nonsimpleWords G (n+3) ⊆ Finset.univ.biUnion (fun k => (t k).biUnion (R k)) := by + intro f hf + obtain ⟨k, σ, hp, hs, τ, hτ, rfl⟩ := nonsimple_word_factor G hf + exact Finset.mem_biUnion.mpr ⟨k, by simp, Finset.mem_biUnion.mpr + ⟨σ, by simp [t, hp, hs], Finset.mem_image.mpr ⟨τ, hτ, rfl⟩⟩⟩ + let B := Fintype.card V * Δ ^ ((n+3)/2) + C * Δ ^ (n+2) + have hR : ∀ k, ∀ σ ∈ t k, (R k σ).card ≤ B := by + intro k σ hσ + have hh : PatternProper σ ∧ Function.Surjective σ := by simpa only + [t, Finset.mem_filter, Finset.mem_univ, true_and] using hσ + obtain ⟨a, p, hp, hs⟩ := pattern_spanning_walk σ hh.1 hh.2 + exact (Finset.card_image_le).trans (by + simpa [B] using noninjective_pattern_count (closedWalkPattern σ) G (n+3) + (pattern_preconnected σ hh.1 hh.2) (by simpa only [Fintype.card_fin] using k.isLt) + a p hp hs Δ C hΔ0 hΔ hcyc) + calc + _ ≤ (Finset.univ.biUnion (fun k => (t k).biUnion (R k))).card := Finset.card_le_card hsub + _ ≤ ∑ k : Fin (n+3), ∑ σ ∈ t k, (R k σ).card := by + exact (Finset.card_biUnion_le).trans (Finset.sum_le_sum (fun k _ => Finset.card_biUnion_le)) + _ ≤ ∑ k : Fin (n+3), (t k).card * B := by + apply Finset.sum_le_sum + intro k hk + simpa using Finset.sum_le_sum (hR k) + _ ≤ ∑ _k : Fin (n+3), (n+3)^(n+3) * B := by + apply Finset.sum_le_sum + intro k hk + apply Nat.mul_le_mul_right B + calc + (t k).card ≤ Fintype.card (Fin (n+3) → Fin k.val) := Finset.card_le_univ _ + _ = k.val^(n+3) := by simp + _ ≤ (n+3)^(n+3) := Nat.pow_le_pow_left k.isLt.le _ + _ = (n+3)^(n+4) * B := by simp only [Finset.sum_const, Finset.card_univ, + Fintype.card_fin, smul_eq_mul]; rw [show n+4 = (n+3)+1 by omega, pow_succ]; ring + +end ClosedWords +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/CountClosedWalk.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/CountClosedWalk.lean new file mode 100644 index 0000000000000000000000000000000000000000..888ba2f528c3e53ee5c3fd2abfd08763b0de8469 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/CountClosedWalk.lean @@ -0,0 +1,99 @@ +import Mathlib + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave + +variable {V : Type*} [DecidableEq V] {G : SimpleGraph V} + +theorem bridge_count_closedWalk {a : V} (p : G.Walk a a) {e : Sym2 V} + (he : e ∈ p.edges) (hb : G.IsBridge e) : 2 ≤ p.edges.count e := by + classical + induction e using Sym2.inductionOn with + | _ x y => + obtain ⟨i, hi, heq⟩ := (p.mk_mem_edges_iff_exists).mp he + let q := p.drop i + have hq : ¬ q.Nil := by + rw [← SimpleGraph.Walk.length_eq_zero_iff] + dsimp [q] + rw [SimpleGraph.Walk.drop_length] + omega + have he' : s(p.getVert i, q.snd) = s(x,y) := by + simpa only [q, SimpleGraph.Walk.snd, SimpleGraph.Walk.drop_getVert] using heq + have hmem : s(p.getVert i, q.snd) ∈ (q.tail.append (p.take i)).edges := by + have hh : G.IsBridge s(p.getVert i, q.snd) := he'.symm ▸ hb + have hwalk := (SimpleGraph.isBridge_iff_forall_walk_mem_edges.mp hh) + (q.tail.append (p.take i)).reverse + simpa only [SimpleGraph.Walk.edges_reverse, List.mem_reverse] using hwalk + have hcount : 1 ≤ (q.tail.append (p.take i)).edges.count s(x,y) := by + exact List.one_le_count_iff.mpr (he' ▸ hmem) + have hp : p.edges = (p.take i).edges ++ s(x,y) :: q.tail.edges := by + conv_lhs => rw [← p.append_take_drop_eq i] + rw [SimpleGraph.Walk.edges_append] + change (p.take i).edges ++ q.edges = _ + rw [← q.cons_tail_eq hq, SimpleGraph.Walk.edges_cons, he'] + simp + rw [SimpleGraph.Walk.edges_append, List.count_append] at hcount + rw [hp, List.count_append, List.count_cons_self] + omega + +theorem acyclic_closedWalk_distinct_edges {a : V} (p : G.Walk a a) + (hG : G.IsAcyclic) : 2 * p.edges.toFinset.card ≤ p.length := by + classical + have hh : ∀ e ∈ p.edges.toFinset, 2 ≤ p.edges.count e := by + intro e he + have he' := List.mem_toFinset.mp he + exact bridge_count_closedWalk p he' + ((SimpleGraph.isAcyclic_iff_forall_isBridge.mp hG) (p.edges_subset_edgeSet he')) + calc + _ = ∑ _e ∈ p.edges.toFinset, 2 := by simp [Nat.mul_comm] + _ ≤ ∑ e ∈ p.edges.toFinset, p.edges.count e := Finset.sum_le_sum hh + _ = p.edges.length := by exact List.sum_toFinset_count_eq_length _ + _ = p.length := p.length_edges + +theorem walk_support_card_le_edges {a b : V} (p : G.Walk a b) : + p.support.toFinset.card ≤ p.edges.toFinset.card + 1 := by + classical + induction p with + | nil => simp + | @cons a b c hab p ih => + simp only [SimpleGraph.Walk.support_cons, SimpleGraph.Walk.edges_cons, + List.toFinset_cons] + by_cases ha : a ∈ p.support.toFinset + · rw [Finset.insert_eq_of_mem ha] + exact ih.trans (Nat.add_le_add_right (Finset.card_le_card (Finset.subset_insert _ _)) 1) + · have he : s(a,b) ∉ p.edges.toFinset := by + intro he + exact ha (List.mem_toFinset.mpr + (p.fst_mem_support_of_mem_edges (List.mem_toFinset.mp he))) + rw [Finset.card_insert_of_notMem ha, Finset.card_insert_of_notMem he] + omega + +theorem acyclic_spanning_walk_vertices [Fintype V] {a : V} (p : G.Walk a a) + (hG : G.IsAcyclic) (hspan : ∀ v, v ∈ p.support) : + Fintype.card V - 1 ≤ p.length / 2 := by + have hc := acyclic_closedWalk_distinct_edges p hG + have hv := walk_support_card_le_edges p + have hs : p.support.toFinset = Finset.univ := by + exact Finset.eq_univ_iff_forall.mpr (fun v => List.mem_toFinset.mpr (hspan v)) + rw [hs, Finset.card_univ] at hv + omega + +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/DensityGlobalSpectral.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/DensityGlobalSpectral.lean new file mode 100644 index 0000000000000000000000000000000000000000..9d2ee75f157fa9abe7e16e3a1b2484276a05d7f0 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/DensityGlobalSpectral.lean @@ -0,0 +1,111 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixCodegreeDriftTerm +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixBalancedNoise +import OAI.Combinatorics.TriangleRemoval.Spectral.WheelSpoke +import OAI.Combinatorics.TriangleRemoval.Embeddings.DegreeTemplate + +namespace OAI + +section +noncomputable section +open scoped BigOperators Matrix.Norms.L2Operator +open Filter Classical + +namespace SharpTerminalLeave + +theorem density_global_spectral {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) + (D c : ℝ) (hD : prefixD n ≤ D) (hDmin : 1 ≤ prefixD n) + (hδ : (n : ℝ)^(-c) ≤ 1) (hne : ∀ u, (neighbors G u).Nonempty) + (hrow : ∀ u v : Fin n, {u,v} ∈ G → + |(currentCodegree G u v : ℝ)-D| ≤ (n : ℝ)^(-c)*D) + (hcyc : ∀ j, 3 ≤ j → j ≤ 8000 → ∀ u : Fin n, + |linkCycleCount j G u-D^j| ≤ (n : ℝ)^(-c)*D^j) : + ‖D⁻¹ • globalLinkAdjacency G hG-globalStarAverage G‖ ≤ 2*prefixSpectralError n c := by + have hD1 : 1 ≤ D := hDmin.trans hD + have hD0 : 0 < D := lt_of_lt_of_le zero_lt_one hD1 + have hd0 : 0 ≤ (n : ℝ)^(-c) := Real.rpow_nonneg (Nat.cast_nonneg _) _ + let M (u : Fin n) : Matrix (neighbors G u) (neighbors G u) ℝ := + D⁻¹ • (linkSimpleGraph G hG u).adjMatrix ℝ-averagingProjector + let N (u : Fin n) : Matrix (edgeStar G u) (edgeStar G u) ℝ := + Matrix.reindex (neighborEdgeEquiv G hG u) (neighborEdgeEquiv G hG u) (M u) + have hM : ∀ u, (M u).IsHermitian := by + intro u + exact ((linkSimpleGraph G hG u).isHermitian_adjMatrix ℝ).smul + (isSelfAdjoint_iff.mpr rfl) |>.sub averagingProjector_hermitian + have hlocal : ∀ u, ‖N u‖ ≤ prefixSpectralError n c := by + intro u + have : Nonempty (neighbors G u) := (hne u).to_subtype + apply (hermitian_reindex_norm_le _ _ (hM u)).trans + have hr : ∀ v : neighbors G u, + |((Finset.univ.filter ((linkSimpleGraph G hG u).Adj v)).card : ℝ)-D| ≤ (n : ℝ)^(-c)*D := by + intro v + rw [link_degree_eq G hG u v] + exact hrow u v.val (Finset.mem_filter.mp v.property).2 + have hm := hcyc 8000 (by decide) le_rfl u + rw [linkCycleCount_eq_graphEmbeddings (j := 7998) G hG u] at hm + have hc : ∀ b, 3 ≤ b → b < 8000 → + ((graphEmbeddings (SimpleGraph.cycleGraph b) (linkSimpleGraph G hG u)).card : ℝ) ≤ 2*D^b := by + intro b hb hb' + have hh := (abs_le.mp (hcyc b hb hb'.le u)).2 + have he : b = b-2+2 := by omega + have hcount : linkCycleCount b G u = + ((graphEmbeddings (SimpleGraph.cycleGraph b) (linkSimpleGraph G hG u)).card : ℝ) := by + rw [he] + exact linkCycleCount_eq_graphEmbeddings G hG u + rw [hcount] at hh + have hx := mul_le_mul_of_nonneg_right hδ (pow_nonneg hD0.le b) + nlinarith + have hh := graph_star_approximation (linkSimpleGraph G hG u) 7997 (by decide) + D ((n : ℝ)^(-c)) hD1 hd0 hδ hr hm hc + apply hh.trans + have hn : (Fintype.card (neighbors G u) : ℝ) ≤ n := by + exact_mod_cast (show Fintype.card (neighbors G u) ≤ n by + simpa using (neighbors G u).card_le_univ) + change 4*((8001 : ℝ)*(n : ℝ)^(-c)+(8000 : ℝ)^8001*2^8000* + ((Fintype.card (neighbors G u) : ℝ)/D^4000+1/D))^(1/(8000 : ℝ))+7*(n : ℝ)^(-c) ≤ _ + unfold prefixSpectralError + gcongr + have hsplit : D⁻¹ • globalLinkAdjacency G hG-globalStarAverage G = + ∑ u, extendStar (edgeStar G u) (N u) := by + simp only [globalLinkAdjacency,globalStarAverage,Finset.smul_sum,← Finset.sum_sub_distrib] + apply Finset.sum_congr rfl + intro u _ + have he : N u = D⁻¹ • linkStarMatrix G hG u-averagingProjector := by + ext i j + simp only [N,M,linkStarMatrix,Matrix.reindex_apply,Matrix.submatrix_apply, + Matrix.sub_apply,Matrix.smul_apply,averagingProjector] + rw [Fintype.card_congr (neighborEdgeEquiv G hG u)] + rw [he] + simp [extendStar,Matrix.mul_sub,Matrix.sub_mul,Matrix.smul_mul,Matrix.mul_smul] + rw [hsplit] + exact graph_star_error_norm G hG N (fun u => (Matrix.isHermitian_reindex_iff _).mpr (hM u)) + (prefixSpectralError n c) (prefixSpectralError_nonneg n c (zero_le_one.trans hDmin)) hlocal + +theorem prefix_degree_wheel_tracking : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support → + PrefixRootedUpper 8001 n ω → PrefixBalancedNoise 8001 n ω → + ∀ i ≤ prefixTime n, + historyAlive (fun j => densityEdgeSafe n (earlyDensity n j) (prefixEdgeRadius n)) (prefixTime n) i ω → + (∀ u : Fin n, |(currentDegree (ω (historyIndex (prefixTime n) i)) u : ℝ)/ + ((n : ℝ)*earlyDensity n i)-1| ≤ prefixWheelRadius n/2) ∧ + (∀ j, 3 ≤ j → j ≤ 8000 → ∀ u : Fin n, + |linkCycleCount j (ω (historyIndex (prefixTime n) i)) u/ + ((n : ℝ)*earlyDensity n i^2)^j-1| ≤ prefixWheelRadius n/2) := by + filter_upwards [prefix_balanced_tracking 8001] with n hn + intro ω hω hu hnse i hi hs + have hG : ω (historyIndex (prefixTime n) i) ⊆ completeGraph n := complete_history_valid ω hω i hi + constructor + · intro u + have h := hn ω hω hu hnse i hi hs 2 (by decide) degreeTemplate degreeTemplate_balanced (degreeRoot u) + rwa [degree_rootedCount _ hG,degree_rootedScaling] at h + · intro j hj hj' u + have h := hn ω hω hu hnse i hi hs (j+1) (by omega) + (wheelTemplate j hj) (wheelTemplate_balanced j hj) (wheelRoot hj u) + rwa [wheel_rootedCount hj _ hG,wheel_rootedScaling] at h + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/EdgeStar.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/EdgeStar.lean new file mode 100644 index 0000000000000000000000000000000000000000..e02735ae85528eb1dfe8df9a5dafd44e24e4b695 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/EdgeStar.lean @@ -0,0 +1,96 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixEpsilon +import OAI.Combinatorics.TriangleRemoval.Spectral.StarInclusion + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix InnerProductSpace +open scoped BigOperators +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix + +namespace SharpTerminalLeave + +def edgeStar {n : ℕ} (G : Graph n) (u : Fin n) : Finset G := + Finset.univ.filter (fun e => u ∈ e.val) + +@[simp] theorem mem_edgeStar {n : ℕ} (G : Graph n) (u : Fin n) (e : G) : + e ∈ edgeStar G u ↔ u ∈ e.val := by simp [edgeStar] + +def neighborEdge {n : ℕ} (G : Graph n) (u : Fin n) (v : neighbors G u) : edgeStar G u := + ⟨⟨{u,v.val},(Finset.mem_filter.mp v.property).2⟩,by simp⟩ + +theorem neighborEdge_injective {n : ℕ} (G : Graph n) (u : Fin n) : + Function.Injective (neighborEdge G u) := by + intro v w h + have he : ({u,v.val} : Finset (Fin n)) = {u,w.val} := congrArg (fun e => e.val.val) h + have hv : v.val ∈ ({u,w.val} : Finset (Fin n)) := he ▸ (by simp) + have hw : w.val ∈ ({u,v.val} : Finset (Fin n)) := he.symm ▸ (by simp) + simp only [Finset.mem_insert,Finset.mem_singleton] at hv hw + apply Subtype.ext + rcases hv with hv | hv + · rcases hw with hw | hw + · exact hv.trans hw.symm + · exact hw.symm + · exact hv + +theorem neighborEdge_surjective {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) (u : Fin n) : + Function.Surjective (neighborEdge G u) := by + intro e + obtain ⟨a,b,hab,he⟩ := Finset.card_eq_two.mp (mem_completeGraph.mp (hG e.val.property)) + have hu : u ∈ e.val.val := (mem_edgeStar G u e.val).mp e.property + rw [he] at hu + simp only [Finset.mem_insert,Finset.mem_singleton] at hu + have hm : ({a,b} : Finset (Fin n)) ∈ G := he ▸ e.val.property + rcases hu with rfl | rfl + · refine ⟨⟨b,Finset.mem_filter.mpr ⟨Finset.mem_univ _,hm⟩⟩,?_⟩ + apply Subtype.ext + apply Subtype.ext + exact he.symm + · refine ⟨⟨a,Finset.mem_filter.mpr ⟨Finset.mem_univ _,?_⟩⟩,?_⟩ + · simpa only [Finset.pair_comm] using hm + · apply Subtype.ext + apply Subtype.ext + exact (Finset.pair_comm _ _).trans he.symm + +noncomputable def neighborEdgeEquiv {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) (u : Fin n) : + neighbors G u ≃ edgeStar G u := + Equiv.ofBijective (neighborEdge G u) ⟨neighborEdge_injective G u,neighborEdge_surjective G hG u⟩ + +theorem edgeStar_card {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) (u : Fin n) : + (edgeStar G u).card = currentDegree G u := by + have h := Fintype.card_congr (neighborEdgeEquiv G hG u) + simpa only [Fintype.card_coe,currentDegree] using h.symm + +theorem edgeStar_exact_overlap {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) (e : G) : + (Finset.univ.filter fun u => e ∈ edgeStar G u).card = 2 := by + have he : (Finset.univ.filter fun u => e ∈ edgeStar G u) = e.val := by + ext u + simp + rw [he] + exact mem_completeGraph.mp (hG e.property) + +open scoped Matrix.Norms.L2Operator in + +theorem graph_star_error_norm {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) + (M : (u : Fin n) → Matrix (edgeStar G u) (edgeStar G u) ℝ) + (hM : ∀ u, (M u).IsHermitian) (ε : ℝ) (hε : 0 ≤ ε) (hlocal : ∀ u, ‖M u‖ ≤ ε) : + ‖∑ u, extendStar (edgeStar G u) (M u)‖ ≤ 2*ε := by + apply sum_star_norm_bound _ _ hM ε 2 hε (by norm_num) hlocal + intro e + rw [edgeStar_exact_overlap G hG e] + norm_num + +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ExtendStarApply.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ExtendStarApply.lean new file mode 100644 index 0000000000000000000000000000000000000000..6d5710233c77f68f1c376001bb26dce406025ecf --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ExtendStarApply.lean @@ -0,0 +1,51 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.StarInclusion + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix InnerProductSpace +open scoped BigOperators +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix + +namespace SharpTerminalLeave + +variable {E : Type*} [Fintype E] [DecidableEq E] + +omit [Fintype E] in + +theorem extendStar_apply (S : Finset E) (M : Matrix S S ℝ) (i j : E) : + extendStar S M i j = if hi : i ∈ S then if hj : j ∈ S then + M ⟨i,hi⟩ ⟨j,hj⟩ else 0 else 0 := by + simp only [extendStar,Matrix.mul_apply,Matrix.conjTranspose_apply,starInclusion, + star_trivial] + by_cases hi : i ∈ S + · by_cases hj : j ∈ S + · have hei : ∀ x : S, i = x.val ↔ x = ⟨i,hi⟩ := fun x => by + rw [Subtype.ext_iff] + exact eq_comm + have hej : ∀ x : S, j = x.val ↔ x = ⟨j,hj⟩ := fun x => by + rw [Subtype.ext_iff] + exact eq_comm + simp [hi,hj,hei,hej] + · have hej : ∀ x : S, j ≠ x.val := fun x hx => hj (hx.symm ▸ x.property) + simp [hi,hj,hej] + · have hei : ∀ x : S, i ≠ x.val := fun x hx => hi (hx.symm ▸ x.property) + simp [hi,hei] + +end SharpTerminalLeave + +open scoped BigOperators Topology NNReal Matrix.Norms.Operator +open MeasureTheory + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ExtendStarMulVec.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ExtendStarMulVec.lean new file mode 100644 index 0000000000000000000000000000000000000000..ced4133201fd39758006c753a5a581625920f36f --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/ExtendStarMulVec.lean @@ -0,0 +1,86 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.LinkStarMatrix +import OAI.Combinatorics.TriangleRemoval.Process.CompleteEdge + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Classical Matrix + +namespace SharpTerminalLeave + +lemma extendStar_mulVec_apply {E : Type*} [Fintype E] [DecidableEq E] + (S : Finset E) (M : Matrix S S ℝ) (z : E → ℝ) (e : E) : + (extendStar S M *ᵥ z) e = if he : e ∈ S then + (M *ᵥ fun f : S => z f.val) ⟨e,he⟩ else 0 := by + rw [extendStar,← Matrix.mulVec_mulVec,← Matrix.mulVec_mulVec,starInclusion_mulVec] + simp only [Matrix.mulVec,dotProduct,starInclusion] + by_cases he : e ∈ S + · have hh : ∀ f : S, e = f.val ↔ f = ⟨e,he⟩ := fun f => by rw [Subtype.ext_iff]; exact eq_comm + simp [he,hh] + · have hh : ∀ f : S, e ≠ f.val := fun f hf => he (hf.symm ▸ f.property) + simp [he,hh] + +lemma extendStar_average_apply {E : Type*} [Fintype E] [DecidableEq E] + (S : Finset E) (z : E → ℝ) (e : E) : + (extendStar S averagingProjector *ᵥ z) e = + if e ∈ S then (∑ f ∈ S, z f)/(S.card : ℝ) else 0 := by + rw [extendStar_mulVec_apply] + split_ifs with he + · simp only [Matrix.mulVec,dotProduct,averagingProjector,Fintype.card_coe] + rw [← Finset.mul_sum,Finset.sum_coe_sort] + ring + · rfl + +lemma graphActive_star_sum {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) + (x : CompleteEdge n → ℝ) (u : Fin n) : + (∑ e ∈ (graphActive G).filter (fun e => u ∈ e.val), x e) = + ∑ e ∈ edgeStar G u, graphRestrict G hG x e := by + rw [Finset.sum_filter] + rw [graphActive_sum G hG] + simp only [edgeStar,Finset.sum_filter] + rfl + +lemma graphActive_star_card {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) (u : Fin n) : + ((graphActive G).filter (fun e => u ∈ e.val)).card = (edgeStar G u).card := by + have hh := graphActive_star_sum G hG (fun _ => 1) u + simpa only [graphRestrict,LinearMap.coe_mk,AddHom.coe_mk,Finset.sum_const,nsmul_eq_mul,mul_one, + Nat.cast_inj] using hh + +lemma graph_movingStarMean {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) + (x : CompleteEdge n → ℝ) (u : Fin n) : + movingStarMean (fun e : CompleteEdge n => e.val) (graphActive G) x u = + (∑ e ∈ edgeStar G u, graphRestrict G hG x e)/(edgeStar G u).card := by + unfold movingStarMean movingStarDegree + congr 1 + · convert graphActive_star_sum G hG x u using 1 + congr 1 + ext e + simp + · convert congrArg (Nat.cast : ℕ → ℝ) (graphActive_star_card G hG u) using 1 + congr 2 + ext e + simp + +lemma graph_movingStarLift {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) + (x : CompleteEdge n → ℝ) (e : CompleteEdge n) (he : e ∈ graphActive G) : + movingStarLift (fun e : CompleteEdge n => e.val) + (movingStarMean (fun e : CompleteEdge n => e.val) (graphActive G) x) e = + graphMatrixLift G hG (globalStarAverage G) x e := by + have heG : e.val ∈ G := (mem_graphActive G e).mp he + change (∑ u ∈ e.val, _) = (graphExtend G (globalStarAverage G *ᵥ graphRestrict G hG x)) e + simp only [graphExtend,LinearMap.coe_mk,AddHom.coe_mk,dite_eq_left heG] + rw [globalStarAverage,Matrix.sum_mulVec] + simp only [Finset.sum_apply,extendStar_average_apply,mem_edgeStar] + rw [← Finset.sum_filter] + have heq : Finset.univ.filter (fun u => u ∈ e.val) = e.val := by ext; simp + rw [heq] + exact Finset.sum_congr rfl (fun u _ => graph_movingStarMean G hG x u) + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FallingFactorialNormalizedProduct.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FallingFactorialNormalizedProduct.lean new file mode 100644 index 0000000000000000000000000000000000000000..2639861739c30bfd2b2f3efc8bd629a98acaa816 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FallingFactorialNormalizedProduct.lean @@ -0,0 +1,130 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Coupling.UnitTime + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma fallingFactorial_normalized_product (r a n : ℕ) (hn : r+a ≤ n) (hn0 : 0 < n) : + ((n-r).descFactorial a : ℝ)/(n : ℝ)^a = + ∏ i ∈ Finset.range a, (1-((r+i : ℕ) : ℝ)/(n : ℝ)) := by + have hnR : (0 : ℝ) < n := by exact_mod_cast hn0 + rw [Nat.descFactorial_eq_prod_range,Nat.cast_prod] + have hh : (n : ℝ)^a = ∏ _i ∈ Finset.range a, (n : ℝ) := by simp + rw [hh,← Finset.prod_div_distrib] + apply Finset.prod_congr rfl + intro i hi + have hri : r+i ≤ n := by have := Finset.mem_range.mp hi; omega + rw [Nat.sub_sub,Nat.cast_sub hri] + field_simp + +theorem fallingFactorial_normalized_error (r a n : ℕ) (hn : r+a ≤ n) (hn0 : 0 < n) : + |((n-r).descFactorial a : ℝ)/(n : ℝ)^a-1| ≤ + (a : ℝ)*(r+a : ℕ)/(n : ℝ) := by + have hnR : (0 : ℝ) < n := by exact_mod_cast hn0 + rw [fallingFactorial_normalized_product r a n hn hn0] + let f := fun i : ℕ => 1-((r+i : ℕ) : ℝ)/(n : ℝ) + have hf : ∀ i ∈ Finset.range a, f i ∈ Set.Icc (0 : ℝ) 1 := by + intro i hi + have hri : r+i ≤ n := by have := Finset.mem_range.mp hi; omega + have h0 : 0 ≤ ((r+i : ℕ) : ℝ)/(n : ℝ) := by positivity + have h1 : ((r+i : ℕ) : ℝ)/(n : ℝ) ≤ 1 := by + apply (div_le_one hnR).mpr + exact_mod_cast hri + exact ⟨by dsimp [f]; linarith,by dsimp [f]; linarith⟩ + have hh := abs_prod_sub_prod_le_sum (Finset.range a) f (fun _ => 1) hf + (fun _ _ => ⟨zero_le_one,le_rfl⟩) + simp only [Finset.prod_const_one] at hh + apply hh.trans + calc + (∑ i ∈ Finset.range a, |f i-1|) = + ∑ i ∈ Finset.range a, ((r+i : ℕ) : ℝ)/(n : ℝ) := by + apply Finset.sum_congr rfl + intro i _ + dsimp [f] + have h0 : 0 ≤ ((r+i : ℕ) : ℝ)/(n : ℝ) := by positivity + rw [show 1-((r+i : ℕ) : ℝ)/(n : ℝ)-1 = -(((r+i : ℕ) : ℝ)/(n : ℝ)) by ring, + abs_neg,abs_of_nonneg h0] + _ ≤ ∑ _i ∈ Finset.range a, ((r+a : ℕ) : ℝ)/(n : ℝ) := by + apply Finset.sum_le_sum + intro i hi + apply div_le_div_of_nonneg_right _ hnR.le + exact_mod_cast (by have := Finset.mem_range.mp hi; omega : r+i ≤ r+a) + _ = _ := by simp; ring + +theorem initialDensity_power_error (b n : ℕ) (hn : 1 ≤ n) : + |(1-1/(n : ℝ))^b-1| ≤ (b : ℝ)/(n : ℝ) := by + have hnR : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hp0 : 0 ≤ 1-1/(n : ℝ) := by + have : 1/(n : ℝ) ≤ 1 := by apply (div_le_one (by linarith)).mpr; exact hnR + linarith + have hp1 : 1-1/(n : ℝ) ≤ 1 := by + have : 0 ≤ 1/(n : ℝ) := by positivity + linarith + have hh := abs_prod_sub_prod_le_sum (Finset.range b) (fun _ => 1-1/(n : ℝ)) + (fun _ => 1) (fun _ _ => ⟨hp0,hp1⟩) (fun _ _ => ⟨zero_le_one,le_rfl⟩) + simpa only [Finset.prod_const,Finset.card_range,one_pow, + show 1-1/(n : ℝ)-1 = -(1/(n : ℝ)) by ring,abs_neg, + abs_of_nonneg (by positivity : 0 ≤ 1/(n : ℝ)),Finset.sum_const, + nsmul_eq_mul,mul_one_div] using hh + +theorem initialRootedScaling_error (r a b n : ℕ) (hn : r+a ≤ n) (hn2 : 2 ≤ n) : + |((n-r).descFactorial a : ℝ) / + ((n : ℝ)^a * (1-1/(n : ℝ))^b)-1| ≤ + (2 : ℝ)^b * ((a : ℝ)*(r+a : ℕ)+(b : ℝ))/(n : ℝ) := by + have hnR : (2 : ℝ) ≤ n := by exact_mod_cast hn2 + have hn0 : 0 < n := by omega + have hnR0 : (0 : ℝ) < n := by linarith + have hp : (1/2 : ℝ) ≤ 1-1/(n : ℝ) := by + have hh : 1/(n : ℝ) ≤ 1/2 := (div_le_div_iff₀ hnR0 (by norm_num)).mpr (by linarith) + linarith + have hp0 : 0 < (1-1/(n : ℝ))^b := pow_pos (by linarith) _ + have hi : 1/(1-1/(n : ℝ))^b ≤ (2 : ℝ)^b := by + have hh := pow_le_pow_left₀ (by norm_num : (0 : ℝ) ≤ 1/2) hp b + have hh2 : (1/2 : ℝ)^b = 1/(2 : ℝ)^b := by rw [div_pow,one_pow] + rw [hh2] at hh + exact (div_le_iff₀ hp0).mpr (by + have ht : (0 : ℝ) < (2 : ℝ)^b := by positivity + have hmul := (div_le_iff₀ ht).mp hh + simpa only [mul_comm] using hmul) + have hf := fallingFactorial_normalized_error r a n hn hn0 + have ht := initialDensity_power_error b n (by omega) + have he : |((n-r).descFactorial a : ℝ) / + ((n : ℝ)^a*(1-1/(n : ℝ))^b)-1| = + |((n-r).descFactorial a : ℝ)/(n : ℝ)^a-(1-1/(n : ℝ))^b| / + (1-1/(n : ℝ))^b := by + calc + _ = |(((n-r).descFactorial a : ℝ)/(n : ℝ)^a-(1-1/(n : ℝ))^b) / + (1-1/(n : ℝ))^b| := by + rw [sub_div,div_self (ne_of_gt hp0),div_div] + _ = _ := by rw [abs_div,abs_of_pos hp0] + rw [he,div_eq_mul_inv] + have he2 : |((n-r).descFactorial a : ℝ)/(n : ℝ)^a-(1-1/(n : ℝ))^b| ≤ + ((a : ℝ)*(r+a : ℕ)+(b : ℝ))/(n : ℝ) := by + calc + _ ≤ |((n-r).descFactorial a : ℝ)/(n : ℝ)^a-1| + + |(1-1/(n : ℝ))^b-1| := by + simpa only [abs_sub_comm (1 : ℝ)] using + (abs_sub_le (((n-r).descFactorial a : ℝ)/(n : ℝ)^a) 1 ((1-1/(n : ℝ))^b)) + _ ≤ (a : ℝ)*(r+a : ℕ)/(n : ℝ)+(b : ℝ)/(n : ℝ) := add_le_add hf ht + _ = _ := by ring + calc + _ ≤ (((a : ℝ)*(r+a : ℕ)+(b : ℝ))/(n : ℝ)) * (2 : ℝ)^b := + mul_le_mul he2 (by simpa only [one_div] using hi) (inv_nonneg.mpr hp0.le) + (by positivity) + _ = _ := by ring + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone.lean new file mode 100644 index 0000000000000000000000000000000000000000..afce1ab4fbc41895df5ba467d06a60a82bec2a4b --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone.lean @@ -0,0 +1,49 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.MapOutput + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave.ExposureTree +variable {K V R : Type*} + +theorem freshLog_checkNoneTrace_forall (ν : K → PMF V) (P : R → Prop) + (as : List (ExposureTree K V (Bool × List R))) + (ha : ∀ A ∈ as, ∀ z ∈ (freshLog ν A).support, ∀ c ∈ z.1.2, P c) + {z : (Bool × List R) × List K} + (hz : z ∈ (freshLog ν (checkNoneTrace as)).support) : + ∀ c ∈ z.1.2, P c := by + induction as generalizing z with + | nil => + have heq : z = ((true,[]),[]) := by simpa [checkNoneTrace,freshLog] using hz + simp [heq] + | cons A as ih => + rw [checkNoneTrace, freshLog_bind] at hz + obtain ⟨x,hx,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have hxP := ha A (by simp) x hx + have has (B) (hB : B ∈ as) := ha B (by simp [hB]) + cases hb : x.1.1 + · simp only [hb, Bool.false_eq_true, ↓reduceIte, freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + intro c hc + rcases List.mem_append.mp hc with hc | hc + · exact hxP c hc + · exact ih has hw c hc + · simp only [hb, ↓reduceIte, freshLog, PMF.mem_support_pure_iff] at hy + subst y + exact hxP + +end SharpTerminalLeave.ExposureTree + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone2.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone2.lean new file mode 100644 index 0000000000000000000000000000000000000000..26f96f226eda30ae79644ae8ae4b131dfdb9eba7 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone2.lean @@ -0,0 +1,54 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.FreshLogCheckNone + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave.ExposureTree +variable {K V R : Type*} + +theorem freshLog_checkNoneTrace_pairwise (ν : K → PMF V) (rel : R → R → Prop) + (as : List (ExposureTree K V (Bool × List R))) + (ha : ∀ A ∈ as, ∀ z ∈ (freshLog ν A).support, z.1.2.Pairwise rel) + (hab : as.Pairwise (fun A B => + ∀ x ∈ (freshLog ν A).support, ∀ y ∈ (freshLog ν B).support, + ∀ c ∈ x.1.2, ∀ d ∈ y.1.2, rel c d)) + {z : (Bool × List R) × List K} + (hz : z ∈ (freshLog ν (checkNoneTrace as)).support) : + z.1.2.Pairwise rel := by + induction as generalizing z with + | nil => + have heq : z = ((true,[]),[]) := by simpa [checkNoneTrace,freshLog] using hz + simp [heq] + | cons A as ih => + obtain ⟨hhead,htail⟩ := List.pairwise_cons.mp hab + rw [checkNoneTrace, freshLog_bind] at hz + obtain ⟨x,hx,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have hxR := ha A (by simp) x hx + have has (B) (hB : B ∈ as) := ha B (by simp [hB]) + cases hb : x.1.1 + · simp only [hb, Bool.false_eq_true, ↓reduceIte, freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + apply List.pairwise_append.mpr + refine ⟨hxR, ih has htail hw, ?_⟩ + intro c hc + exact freshLog_checkNoneTrace_forall ν (rel c) as + (fun B hB y hy' d hd => hhead B hB x hx y hy' c hc d hd) hw + · simp only [hb, ↓reduceIte, freshLog, PMF.mem_support_pure_iff] at hy + subst y + exact hxR + +end SharpTerminalLeave.ExposureTree + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone3.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone3.lean new file mode 100644 index 0000000000000000000000000000000000000000..52ba36498022ebde3a664d01c77d42732b2b22f2 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/FreshLogCheckNone3.lean @@ -0,0 +1,47 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.MapOutput + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave.ExposureTree +variable {K V R : Type*} + +theorem freshLog_checkNoneTrace_append (ν : K → PMF V) + (P : List R → Prop) (h0 : P []) + (happend : ∀ a b, P a → P b → P (a ++ b)) + (as : List (ExposureTree K V (Bool × List R))) + (ha : ∀ A ∈ as, ∀ z ∈ (freshLog ν A).support, P z.1.2) + {z : (Bool × List R) × List K} + (hz : z ∈ (freshLog ν (checkNoneTrace as)).support) : P z.1.2 := by + induction as generalizing z with + | nil => + have he : z = ((true,[]),[]) := by simpa [checkNoneTrace,freshLog] using hz + simpa only [he] using h0 + | cons A as ih => + rw [checkNoneTrace, freshLog_bind] at hz + obtain ⟨x,hx,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have hxa := ha A (by simp) x hx + have has (B) (hB : B ∈ as) := ha B (by simp [hB]) + cases hb : x.1.1 + · simp only [hb,Bool.false_eq_true,↓reduceIte,freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + exact happend _ _ hxa (ih has hw) + · simp only [hb,↓reduceIte,freshLog,PMF.mem_support_pure_iff] at hy + subst y + exact hxa + +end SharpTerminalLeave.ExposureTree + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/GlobalLinkAdjacencyHermitian.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/GlobalLinkAdjacencyHermitian.lean new file mode 100644 index 0000000000000000000000000000000000000000..a22f4ed1c7c7d261ad3c66f07c68dcb969b0ae02 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/GlobalLinkAdjacencyHermitian.lean @@ -0,0 +1,85 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.EdgeIncidence +import OAI.Combinatorics.TriangleRemoval.Process.AveragingProjectorInftyOne +import OAI.Combinatorics.TriangleRemoval.Spectral.MatrixEuclideanAlgHom + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory + +namespace SharpTerminalLeave +open scoped Matrix.Norms.Operator + +theorem globalLinkAdjacency_hermitian {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) : + (globalLinkAdjacency G hG).IsHermitian := by + apply Matrix.IsHermitian.ext + intro i j + simp only [globalLinkAdjacency,Matrix.sum_apply] + apply Finset.sum_congr rfl + intro u _ + exact (Matrix.isHermitian_mul_mul_conjTranspose (starInclusion (edgeStar G u)) + ((Matrix.isHermitian_reindex_iff _).mpr + ((linkSimpleGraph G hG u).isHermitian_adjMatrix ℝ))).apply i j + +theorem goodPrefix_semigroup_quantitative {n : ℕ} [NeZero n] {G : Graph n} [Nonempty G] + {c C : ℝ} (h : GoodPrefixGraph n c C G) (hne : ∀ u, (neighbors G u).Nonempty) + (hD : 1 ≤ prefixD n) (hδ : (n : ℝ)^(-c) ≤ 1) + (T : ℝ) (hT : 0 ≤ T) (K : ℕ) (s : ℝ) (hs : s ∈ Set.Icc (0 : ℝ) T) : + ‖NormedSpace.exp ((-s) • ((prefixD n)⁻¹ • globalLinkAdjacency G h.1))‖ ≤ + (∑ j ∈ Finset.range K, ((1+2*T)*6*T)^j*(1+2*T)) + + Real.sqrt G.card * ((2*prefixSpectralError n c*T)^K * + Real.exp (T*(2*prefixSpectralError n c))) := by + let P := globalStarAverage G + let R := (prefixD n)⁻¹ • globalLinkAdjacency G h.1 - P + let ε := 2*prefixSpectralError n c + have hε : 0 ≤ ε := mul_nonneg (by norm_num) + (prefixSpectralError_nonneg n c (le_trans zero_le_one hD)) + have hPR : P+R = (prefixD n)⁻¹ • globalLinkAdjacency G h.1 := by dsimp [R]; abel + have hP : P.PosSemidef := globalStarAverage_posSemidef G + have hR : IsSelfAdjoint R := by + exact ((globalLinkAdjacency_hermitian G h.1).smul + (isSelfAdjoint_iff.mpr rfl) |>.sub hP.isHermitian).isSelfAdjoint + have hR₂ : ‖matrixEuclideanAlgHom R‖ ≤ ε := goodPrefix_global_spectral h hne hD hδ + have hRinf : ‖R‖ ≤ 6 := goodPrefix_residual_infty h hD hδ + have hF : Continuous (matrixEuclideanAlgHom (I := G)) := + (matrixEuclideanAlgHom (I := G)).toLinearMap.continuous_of_finiteDimensional + have hb : ∀ t ∈ Set.Icc (0 : ℝ) T, ‖NormedSpace.exp ((-t) • P)‖ ≤ 1+2*T := by + intro t ht + have hdeg : ∀ u, 0 < currentDegree G u := fun u => Finset.card_pos.mpr (hne u) + exact (graph_star_semigroup_bound G h.1 hdeg t ht.1).trans (by linarith [ht.2]) + have hb₂ : ∀ t ∈ Set.Icc (0 : ℝ) T, + ‖NormedSpace.exp ((-t) • matrixEuclideanAlgHom P)‖ ≤ 1 := + fun t ht => euclidean_posSemidef_exp_bound P hP t ht.1 + have hf₂ : ∀ t ∈ Set.Icc (0 : ℝ) T, + ‖NormedSpace.exp ((-t) • (matrixEuclideanAlgHom P + matrixEuclideanAlgHom R))‖ ≤ + Real.exp (T*ε) := by + intro t ht + apply (euclidean_perturbed_exp_bound P R hP hR t ht.1).trans + apply Real.exp_le_exp.mpr + exact (mul_le_mul_of_nonneg_left hR₂ ht.1).trans + (mul_le_mul_of_nonneg_right ht.2 hε) + let : CompleteSpace (EuclideanSpace ℝ G) := PiLp.completeSpace 2 (fun _ : G => ℝ) + let : CompleteSpace (EuclideanSpace ℝ G →L[ℝ] EuclideanSpace ℝ G) := + ContinuousLinearMap.instCompleteSpace + have hh := perturbation_mixed_norm_bound (matrixEuclideanAlgHom (I := G)) hF P R + T (1+2*T) (Real.exp (T*ε)) (Real.sqrt G.card) hT (by positivity) (by positivity) + (by positivity) (by simpa only [Fintype.card_coe] using + (matrix_infty_le_sqrt_card_euclidean (I := G))) hb hb₂ hf₂ K s hs + rw [hPR] at hh + apply hh.trans + apply add_le_add + · apply Finset.sum_le_sum + intro j _ + gcongr + · gcongr + +end SharpTerminalLeave + +open Filter +open scoped BigOperators Topology + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/GraphStarApproximation.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/GraphStarApproximation.lean new file mode 100644 index 0000000000000000000000000000000000000000..64a4fff072fc1aa5b52f30cdbb7ca202d78f0b31 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/GraphStarApproximation.lean @@ -0,0 +1,92 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.NormalizedOnes +import OAI.Combinatorics.TriangleRemoval.Spectral.RealTraceAdjMatrix + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave +open scoped BigOperators Matrix.Norms.L2Operator + +section GraphStar +variable {V : Type*} [Fintype V] [DecidableEq V] [Nonempty V] +variable (G : SimpleGraph V) [DecidableRel G.Adj] + +theorem graph_star_approximation (n : ℕ) (hr : Even (n+3)) (D δ : ℝ) + (hD : 1 ≤ D) (hδ0 : 0 ≤ δ) (hδ1 : δ ≤ 1) + (hrow : ∀ x : V, |((Finset.univ.filter (G.Adj x)).card : ℝ) - D| ≤ δ*D) + (hmain : |((graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card : ℝ) - D^(n+3)| ≤ + δ * D^(n+3)) + (hcyc : ∀ b, 3 ≤ b → b < n+3 → + ((graphEmbeddings (SimpleGraph.cycleGraph b) G).card : ℝ) ≤ 2 * D^b) : + ‖D⁻¹ • G.adjMatrix ℝ - averagingProjector‖ ≤ + 4 * (((n+4 : ℕ) : ℝ) * δ + ((n+3 : ℕ) : ℝ)^(n+4) * 2^(n+3) * + ((Fintype.card V : ℝ) / D^((n+3)/2) + 1/D)) ^ (1 / ((n+3 : ℕ) : ℝ)) + 7*δ := by + have hD0 : 0 < D := lt_of_lt_of_le zero_lt_one hD + have hDne : D ≠ 0 := ne_of_gt hD0 + let W : ℝ := ((n+3 : ℕ) : ℝ)^(n+4) * 2^(n+3) * + ((Fintype.card V : ℝ) / D^((n+3)/2) + 1/D) + let E : ℝ := ((n+4 : ℕ) : ℝ)*δ + W + let θ : ℝ := E ^ (1 / ((n+3 : ℕ) : ℝ)) + have hW : 0 ≤ W := by dsimp [W]; positivity + have hE : 0 ≤ E := by dsimp [E]; positivity + have hθ : 0 ≤ θ := Real.rpow_nonneg hE _ + have hθpow : θ ^ (n+3) = E := by + dsimp [θ] + rw [← Real.rpow_mul_natCast hE] + have hn : ((n+3 : ℕ) : ℝ) ≠ 0 := by positivity + rw [div_mul_cancel₀ _ hn,Real.rpow_one] + have hdeg : ∀ x : V, ((Finset.univ.filter (G.Adj x)).card : ℝ) ≤ 2*D := by + intro x + have hh := (abs_le.mp (hrow x)).2 + nlinarith + have htrace := normalized_link_trace_bound G n hr D δ hD hdeg hmain hcyc + have hber : 1 - ((n+3 : ℕ) : ℝ)*δ ≤ (1-δ)^(n+3) := by + simpa only [sub_eq_add_neg, mul_neg] using + (one_add_mul_le_pow (a := -δ) (by linarith) (n+3)) + have htrace' : Matrix.trace ((D⁻¹ • G.adjMatrix ℝ)^(n+3)) ≤ (1-δ)^(n+3) + θ^(n+3) := by + rw [hθpow] + have hh := (abs_le.mp htrace).2 + dsimp [E] + change Matrix.trace ((D⁻¹ • G.adjMatrix ℝ)^(n+3)) - 1 ≤ δ + W at hh + push_cast at hber ⊢ + nlinarith + have hrows : ∀ x : V, |(∑ y, (D⁻¹ • G.adjMatrix ℝ) x y) - 1| ≤ δ := by + intro x + have hsum : (∑ y, G.adjMatrix ℝ x y) = + ((Finset.univ.filter (G.Adj x)).card : ℝ) := by + simp only [SimpleGraph.adjMatrix_apply] + exact Finset.sum_boole _ _ + simp only [Matrix.smul_apply,smul_eq_mul,← Finset.mul_sum,hsum] + have heq : D⁻¹ * ((Finset.univ.filter (G.Adj x)).card : ℝ) - 1 = + D⁻¹ * (((Finset.univ.filter (G.Adj x)).card : ℝ) - D) := by + field_simp + rw [heq,abs_mul,abs_of_pos (inv_pos.mpr hD0)] + calc + _ ≤ D⁻¹ * (δ*D) := mul_le_mul_of_nonneg_left (hrow x) (inv_nonneg.mpr hD0.le) + _ = δ := by field_simp + have hpos : ∀ x y, 0 ≤ (D⁻¹ • G.adjMatrix ℝ) x y := by + intro x y + change 0 ≤ D⁻¹ * G.adjMatrix ℝ x y + exact mul_nonneg (inv_nonneg.mpr hD0.le) (by simp [SimpleGraph.adjMatrix_apply]; split_ifs <;> norm_num) + exact star_approximation_from_trace _ ((G.isHermitian_adjMatrix ℝ).smul (isSelfAdjoint_iff.mpr rfl)) + δ θ hδ0 hδ1 hθ hpos hrows (n+3) hr (by omega) htrace' + +end GraphStar +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/LinkStarMatrix.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/LinkStarMatrix.lean new file mode 100644 index 0000000000000000000000000000000000000000..4d08aa025b22e6980706314c5534fc203f639b29 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/LinkStarMatrix.lean @@ -0,0 +1,116 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.GoodPrefixStarApproximation +import OAI.Combinatorics.TriangleRemoval.Process.ReindexQuadratic +import OAI.Combinatorics.TriangleRemoval.Spectral.EdgeStar + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix InnerProductSpace +open scoped BigOperators +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix + +namespace SharpTerminalLeave +open scoped Matrix.Norms.L2Operator + +theorem averagingProjector_reindex {I J : Type*} [Fintype I] [Fintype J] + (e : I ≃ J) : Matrix.reindex e e (averagingProjector : Matrix I I ℝ) = + (averagingProjector : Matrix J J ℝ) := by + ext i j + simp only [Matrix.reindex_apply,Matrix.submatrix_apply,averagingProjector] + rw [Fintype.card_congr e] + +theorem averagingProjector_hermitian {I : Type*} [Fintype I] : + (averagingProjector : Matrix I I ℝ).IsHermitian := by + apply Matrix.IsHermitian.ext + intro i j + simp [averagingProjector] + +theorem averagingProjector_posSemidef {I : Type*} [Fintype I] : + (averagingProjector : Matrix I I ℝ).PosSemidef := by + apply Matrix.PosSemidef.of_dotProduct_mulVec_nonneg averagingProjector_hermitian + intro x + have heq : star x ⬝ᵥ ((averagingProjector : Matrix I I ℝ) *ᵥ x) = + (Fintype.card I : ℝ)⁻¹ * (∑ i, x i)^2 := by + simp [averagingProjector,dotProduct,Matrix.mulVec,← Finset.sum_mul,pow_two, + mul_comm,mul_assoc] + rw [heq] + positivity + +noncomputable def linkStarMatrix {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) (u : Fin n) : + Matrix (edgeStar G u) (edgeStar G u) ℝ := + Matrix.reindex (neighborEdgeEquiv G hG u) (neighborEdgeEquiv G hG u) + ((linkSimpleGraph G hG u).adjMatrix ℝ) + +noncomputable def globalLinkAdjacency {n : ℕ} (G : Graph n) (hG : G ⊆ completeGraph n) : + Matrix G G ℝ := ∑ u, extendStar (edgeStar G u) (linkStarMatrix G hG u) + +noncomputable def globalStarAverage {n : ℕ} (G : Graph n) : Matrix G G ℝ := + ∑ u, extendStar (edgeStar G u) averagingProjector + +theorem globalStarAverage_posSemidef {n : ℕ} (G : Graph n) : + (globalStarAverage G).PosSemidef := by + apply Matrix.posSemidef_sum + intro u _ + exact averagingProjector_posSemidef.mul_mul_conjTranspose_same _ + +noncomputable def prefixSpectralError (n : ℕ) (c : ℝ) : ℝ := + 4 * ((8001 : ℝ) * (n : ℝ)^(-c) + (8000 : ℝ)^8001 * 2^8000 * + ((n : ℝ) / prefixD n^4000 + 1/prefixD n)) ^ (1 / (8000 : ℝ)) + 7*(n : ℝ)^(-c) + +theorem prefixSpectralError_nonneg (n : ℕ) (c : ℝ) (hD : 0 ≤ prefixD n) : + 0 ≤ prefixSpectralError n c := by + unfold prefixSpectralError + positivity + +theorem goodPrefix_global_spectral {n : ℕ} {G : Graph n} {c C : ℝ} + (h : GoodPrefixGraph n c C G) (hne : ∀ u, (neighbors G u).Nonempty) + (hD : 1 ≤ prefixD n) (hδ : (n : ℝ)^(-c) ≤ 1) : + ‖(prefixD n)⁻¹ • globalLinkAdjacency G h.1 - globalStarAverage G‖ ≤ + 2 * prefixSpectralError n c := by + let M (u : Fin n) : Matrix (neighbors G u) (neighbors G u) ℝ := + (prefixD n)⁻¹ • (linkSimpleGraph G h.1 u).adjMatrix ℝ - averagingProjector + let N (u : Fin n) : Matrix (edgeStar G u) (edgeStar G u) ℝ := + Matrix.reindex (neighborEdgeEquiv G h.1 u) (neighborEdgeEquiv G h.1 u) (M u) + have hM : ∀ u, (M u).IsHermitian := by + intro u + exact ((linkSimpleGraph G h.1 u).isHermitian_adjMatrix ℝ).smul + (isSelfAdjoint_iff.mpr rfl) |>.sub averagingProjector_hermitian + have hlocal : ∀ u, ‖N u‖ ≤ prefixSpectralError n c := by + intro u + have : Nonempty (neighbors G u) := (hne u).to_subtype + apply (hermitian_reindex_norm_le _ _ (hM u)).trans + apply (goodPrefix_star_fixed h u hD hδ).trans + have hn : ((neighbors G u).card : ℝ) ≤ n := by + exact_mod_cast (show (neighbors G u).card ≤ n by simpa using (neighbors G u).card_le_univ) + unfold prefixSpectralError + gcongr + have hsplit : (prefixD n)⁻¹ • globalLinkAdjacency G h.1 - globalStarAverage G = + ∑ u, extendStar (edgeStar G u) (N u) := by + simp only [globalLinkAdjacency,globalStarAverage,Finset.smul_sum,← Finset.sum_sub_distrib] + apply Finset.sum_congr rfl + intro u _ + have heq : N u = (prefixD n)⁻¹ • linkStarMatrix G h.1 u - averagingProjector := by + ext i j + simp only [N,M,linkStarMatrix,Matrix.reindex_apply,Matrix.submatrix_apply, + Matrix.sub_apply,Matrix.smul_apply,averagingProjector] + rw [Fintype.card_congr (neighborEdgeEquiv G h.1 u)] + rw [heq] + simp [extendStar,Matrix.mul_sub,Matrix.sub_mul,Matrix.smul_mul,Matrix.mul_smul] + rw [hsplit] + apply graph_star_error_norm G h.1 N (fun u => (Matrix.isHermitian_reindex_iff _).mpr (hM u)) + (prefixSpectralError n c) (prefixSpectralError_nonneg n c (le_trans zero_le_one hD)) hlocal + +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixEuclideanAlgHom.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixEuclideanAlgHom.lean new file mode 100644 index 0000000000000000000000000000000000000000..c51aa15b17b2dd68edf93b8eb4ac87cc338c2841 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixEuclideanAlgHom.lean @@ -0,0 +1,74 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.IncidenceDegree +import OAI.Combinatorics.TriangleRemoval.Stability.Volterra +import OAI.Combinatorics.TriangleRemoval.Spectral.MatrixExpL2Bound + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory + +namespace SharpTerminalLeave + +noncomputable def matrixEuclideanAlgHom {I : Type*} [Fintype I] [DecidableEq I] : + Matrix I I ℝ →ₐ[ℝ] (EuclideanSpace ℝ I →L[ℝ] EuclideanSpace ℝ I) := + (Matrix.toEuclideanCLM (n := I) (𝕜 := ℝ)).toAlgEquiv.toAlgHom + +open scoped Matrix.Norms.L2Operator in + +theorem euclidean_posSemidef_exp_bound {I : Type*} [Fintype I] [DecidableEq I] + (P : Matrix I I ℝ) (hP : P.PosSemidef) (s : ℝ) (hs : 0 ≤ s) : + ‖NormedSpace.exp ((-s) • matrixEuclideanAlgHom P)‖ ≤ 1 := by + have hF : Continuous (matrixEuclideanAlgHom (I := I)) := + (matrixEuclideanAlgHom (I := I)).toLinearMap.continuous_of_finiteDimensional + rw [← map_smul,← continuous_algHom_map_exp (matrixEuclideanAlgHom (I := I)) hF] + exact matrix_posSemidef_exp_l2_le_one P hP s hs + +open scoped Matrix.Norms.L2Operator in + +theorem euclidean_perturbed_exp_bound {I : Type*} [Fintype I] [DecidableEq I] [Nonempty I] + (P R : Matrix I I ℝ) (hP : P.PosSemidef) (hR : IsSelfAdjoint R) + (s : ℝ) (hs : 0 ≤ s) : + ‖NormedSpace.exp ((-s) • (matrixEuclideanAlgHom P + matrixEuclideanAlgHom R))‖ ≤ + Real.exp (s * ‖matrixEuclideanAlgHom R‖) := by + have hF : Continuous (matrixEuclideanAlgHom (I := I)) := + (matrixEuclideanAlgHom (I := I)).toLinearMap.continuous_of_finiteDimensional + rw [← map_add,← map_smul,← continuous_algHom_map_exp (matrixEuclideanAlgHom (I := I)) hF] + exact matrix_perturbed_exp_l2_bound P R hP hR s hs + +open scoped Matrix.Norms.Operator in + +theorem matrix_infty_le_sqrt_card_euclidean {I : Type*} [Fintype I] [DecidableEq I] + (A : Matrix I I ℝ) : ‖A‖ ≤ Real.sqrt (Fintype.card I) * ‖matrixEuclideanAlgHom A‖ := by + apply matrix_norm_le_of_row_abs_sum_le _ _ (by positivity) + intro i + let v : EuclideanSpace ℝ I := WithLp.toLp 2 (fun j => if 0 ≤ A i j then 1 else -1) + have hv : ‖v‖ = Real.sqrt (Fintype.card I) := by + rw [EuclideanSpace.norm_eq] + congr 1 + calc + _ = ∑ _j : I, (1 : ℝ) := by + apply Finset.sum_congr rfl + intro j _ + simp only [v] + split_ifs <;> norm_num + _ = _ := by simp + have heq : (matrixEuclideanAlgHom A v).ofLp i = ∑ j, |A i j| := by + change (Matrix.toEuclideanCLM (n := I) (𝕜 := ℝ) A v).ofLp i = _ + simp only [Matrix.ofLp_toEuclideanCLM,v,Matrix.mulVec,dotProduct] + apply Finset.sum_congr rfl + intro j _ + by_cases h : 0 ≤ A i j + · simp [h,abs_of_nonneg h] + · simp [h,abs_of_neg (lt_of_not_ge h)] + have hp := (PiLp.norm_apply_le (matrixEuclideanAlgHom A v) i).trans + ((matrixEuclideanAlgHom A).le_opNorm v) + rw [heq,Real.norm_eq_abs,abs_of_nonneg (Finset.sum_nonneg fun j _ => abs_nonneg _),hv] at hp + simpa only [mul_comm] using hp + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixExpL2Bound.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixExpL2Bound.lean new file mode 100644 index 0000000000000000000000000000000000000000..42d96fe11bdaf6dc93899765270d0d2ec76c0db1 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixExpL2Bound.lean @@ -0,0 +1,60 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory + +section +open scoped BigOperators Topology Matrix.Norms.L2Operator MatrixOrder + +namespace SharpTerminalLeave + +theorem matrix_exp_l2_bound {ι : Type*} [Fintype ι] [DecidableEq ι] + (A : Matrix ι ι ℝ) (hA : IsSelfAdjoint A) (r s : ℝ) (hs : 0 ≤ s) + (hlower : ∀ x ∈ spectrum ℝ A, -r ≤ x) : + ‖NormedSpace.exp ((-s) • A)‖ ≤ Real.exp (s * r) := by + have heq : NormedSpace.exp ((-s) • A) = + cfc (fun x : ℝ => Real.exp ((-s) * x)) A := by + rw [← CFC.real_exp_eq_normedSpace_exp ((isSelfAdjoint_iff.mpr rfl).smul hA)] + rw [← cfc_smul_id (R := ℝ) (-s) A hA, ← cfc_comp Real.exp (fun x : ℝ => (-s) • x) A hA] + rfl + rw [heq] + apply norm_cfc_le (Real.exp_pos _).le + intro x hx + rw [Real.norm_eq_abs, abs_of_pos (Real.exp_pos _)] + exact Real.exp_le_exp.mpr (by nlinarith [hlower x hx]) + +theorem matrix_posSemidef_exp_l2_le_one {ι : Type*} [Fintype ι] [DecidableEq ι] + (P : Matrix ι ι ℝ) (hP : P.PosSemidef) (s : ℝ) (hs : 0 ≤ s) : + ‖NormedSpace.exp ((-s) • P)‖ ≤ 1 := by + have h : ∀ x ∈ spectrum ℝ P, -(0 : ℝ) ≤ x := by + simpa only [neg_zero] using (algebraMap_le_iff_le_spectrum + (IsSelfAdjoint.of_nonneg hP.nonneg)).mp (show algebraMap ℝ _ 0 ≤ P by simp [hP.nonneg]) + simpa only [mul_zero, Real.exp_zero] using + matrix_exp_l2_bound P (IsSelfAdjoint.of_nonneg hP.nonneg) 0 s hs h + +theorem matrix_perturbed_exp_l2_bound {ι : Type*} [Fintype ι] [DecidableEq ι] + [Nonempty ι] + (P R : Matrix ι ι ℝ) (hP : P.PosSemidef) (hR : IsSelfAdjoint R) + (s : ℝ) (hs : 0 ≤ s) : + ‖NormedSpace.exp ((-s) • (P + R))‖ ≤ Real.exp (s * ‖R‖) := by + have hPR : IsSelfAdjoint (P + R) := (IsSelfAdjoint.of_nonneg hP.nonneg).add hR + have hRlower : algebraMap ℝ (Matrix ι ι ℝ) (-‖R‖) ≤ R := by + apply algebraMap_le_of_le_spectrum (ha := hR) + intro x hx + have hxnorm := spectrum.norm_le_norm_of_mem hx + rw [Real.norm_eq_abs] at hxnorm + exact (abs_le.mp hxnorm).1 + apply matrix_exp_l2_bound (P + R) hPR ‖R‖ s hs + apply (algebraMap_le_iff_le_spectrum hPR).mp + exact hRlower.trans (le_add_of_nonneg_left hP.nonneg) + +end SharpTerminalLeave + +end + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixInfinityAlgHom.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixInfinityAlgHom.lean new file mode 100644 index 0000000000000000000000000000000000000000..4b89b24bb7819aad7d99bdb75d548429e539b580 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixInfinityAlgHom.lean @@ -0,0 +1,42 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Stability.Volterra + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave +noncomputable def matrixInfinityAlgHom {I : Type*} [Fintype I] [DecidableEq I] : + Matrix I I ℝ →ₐ[ℝ] ((I → ℝ) →L[ℝ] (I → ℝ)) := + (Matrix.toLinAlgEquiv'.trans (Module.End.toContinuousLinearMap (I → ℝ))).toAlgHom + +@[simp] theorem matrixInfinityAlgHom_apply {I : Type*} [Fintype I] [DecidableEq I] + (A : Matrix I I ℝ) (z : I → ℝ) (i : I) : + matrixInfinityAlgHom A z i = ∑ j, A i j * z j := rfl + +open scoped Matrix.Norms.Operator in + +@[simp] theorem matrixInfinityAlgHom_norm {I : Type*} [Fintype I] [DecidableEq I] + (A : Matrix I I ℝ) : ‖matrixInfinityAlgHom A‖ = ‖A‖ := + (Matrix.linfty_opNorm_eq_opNorm A).symm + +open scoped Matrix.Norms.Operator in + +theorem matrixInfinityAlgHom_exp_norm {I : Type*} [Fintype I] [DecidableEq I] + (A : Matrix I I ℝ) (s : ℝ) : + ‖NormedSpace.exp ((-s) • matrixInfinityAlgHom A)‖ = + ‖NormedSpace.exp ((-s) • A)‖ := by + have hc : Continuous (matrixInfinityAlgHom (I := I)) := + (matrixInfinityAlgHom (I := I)).toLinearMap.continuous_of_finiteDimensional + rw [← map_smul,← continuous_algHom_map_exp (matrixInfinityAlgHom (I := I)) hc, + matrixInfinityAlgHom_norm] + rfl +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixRowAbsSum.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixRowAbsSum.lean new file mode 100644 index 0000000000000000000000000000000000000000..8d573364f76fdba890d72b67ae8a71c24ccc6ffe --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MatrixRowAbsSum.lean @@ -0,0 +1,89 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.ExtendStarApply +import OAI.Combinatorics.TriangleRemoval.Process.IncidenceDegree + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix InnerProductSpace +open scoped BigOperators +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix +open scoped BigOperators Topology NNReal Matrix.Norms.Operator +open MeasureTheory + +namespace SharpTerminalLeave +open scoped Matrix.Norms.Operator + +theorem matrix_row_abs_sum_le {I J : Type*} [Fintype I] [Fintype J] + (M : Matrix I J ℝ) (i : I) : ∑ j, |M i j| ≤ ‖M‖ := by + rw [Matrix.linfty_opNorm_def] + have hh : ∑ j, ‖M i j‖₊ ≤ Finset.univ.sup (fun k => ∑ j, ‖M k j‖₊) := + Finset.le_sup (f := fun k => ∑ j, ‖M k j‖₊) (Finset.mem_univ i) + have hc := NNReal.coe_le_coe.mpr hh + simpa only [NNReal.coe_sum,coe_nnnorm,Real.norm_eq_abs] using hc + +theorem extendStar_row_abs_sum {E : Type*} [Fintype E] [DecidableEq E] + (S : Finset E) (M : Matrix S S ℝ) (i : E) : + ∑ j, |extendStar S M i j| = + if hi : i ∈ S then ∑ j : S, |M ⟨i,hi⟩ j| else 0 := by + by_cases hi : i ∈ S + · rw [dite_eq_left hi] + have he : ∑ j : E, |extendStar S M i j| = ∑ j ∈ S, |extendStar S M i j| := by + symm + apply Finset.sum_subset (Finset.subset_univ S) + intro j _ hj + simp [extendStar_apply,hj] + rw [he,← Finset.sum_coe_sort S] + apply Finset.sum_congr rfl + intro j _ + simp [extendStar_apply,hi,j.property] + · simp [extendStar_apply,hi] + +theorem sum_star_infty_norm_bound {E U : Type*} [Fintype E] [DecidableEq E] [Fintype U] + (S : U → Finset E) (M : (u : U) → Matrix (S u) (S u) ℝ) + (ε K : ℝ) (hε : 0 ≤ ε) (hK : 0 ≤ K) (hM : ∀ u, ‖M u‖ ≤ ε) + (hoverlap : ∀ e, ((Finset.univ.filter fun u => e ∈ S u).card : ℝ) ≤ K) : + ‖∑ u, extendStar (S u) (M u)‖ ≤ K*ε := by + apply matrix_norm_le_of_row_abs_sum_le _ _ (mul_nonneg hK hε) + intro e + calc + _ ≤ ∑ f, ∑ u, |extendStar (S u) (M u) e f| := by + apply Finset.sum_le_sum + intro f _ + simp only [Matrix.sum_apply] + exact Finset.abs_sum_le_sum_abs _ _ + _ = ∑ u, ∑ f, |extendStar (S u) (M u) e f| := Finset.sum_comm + _ ≤ ∑ u, if e ∈ S u then ε else 0 := by + apply Finset.sum_le_sum + intro u _ + rw [extendStar_row_abs_sum] + by_cases he : e ∈ S u + · simp only [he,↓reduceDIte,↓reduceIte] + exact (matrix_row_abs_sum_le (M u) ⟨e,he⟩).trans (hM u) + · simp [he] + _ = ((Finset.univ.filter fun u => e ∈ S u).card : ℝ) * ε := by + rw [← Finset.sum_filter] + simp + _ ≤ K*ε := mul_le_mul_of_nonneg_right (hoverlap e) hε + +theorem infty_reindex_norm_le {I J : Type*} [Fintype I] [Fintype J] + (e : I ≃ J) (M : Matrix I I ℝ) : ‖Matrix.reindex e e M‖ ≤ ‖M‖ := by + apply matrix_norm_le_of_row_abs_sum_le _ _ (norm_nonneg _) + intro i + simp only [Matrix.reindex_apply,Matrix.submatrix_apply] + rw [e.symm.sum_comp (fun j => |M (e.symm i) j|)] + exact matrix_row_abs_sum_le M (e.symm i) + +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MovingStarDegree.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MovingStarDegree.lean new file mode 100644 index 0000000000000000000000000000000000000000..c134328728b108105cd6e12b505284f3834b7cf3 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/MovingStarDegree.lean @@ -0,0 +1,327 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators + +namespace SharpTerminalLeave + +open Classical + +private lemma moving_abs_sub_bound (x y : ℝ) : |x-y| ≤ |x|+|y| := by + simpa using (abs_sub_le x 0 y) + +noncomputable def movingStarDegree {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (u : V) : ℝ := by + classical + exact ((active.filter (fun e => u ∈ ends e)).card : ℝ) + +noncomputable def movingStarMean {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (x : E → ℝ) (u : V) : ℝ := by + classical + exact (∑ e ∈ active.filter (fun e => u ∈ ends e), x e) / movingStarDegree ends active u + +noncomputable def movingStarLift {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (b : V → ℝ) (e : E) : ℝ := ∑ u ∈ ends e, b u + +noncomputable def movingStarOtherMean {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (b : V → ℝ) (u : V) : ℝ := by + classical + exact (∑ e ∈ active.filter (fun e => u ∈ ends e), ∑ v ∈ (ends e).erase u, b v) / + movingStarDegree ends active u + +lemma movingStarDegree_nonneg {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (u : V) : + 0 ≤ movingStarDegree ends active u := Nat.cast_nonneg _ + +lemma movingStarMean_congr {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) {x y : E → ℝ} + (h : ∀ e ∈ active, x e = y e) : movingStarMean ends active x = movingStarMean ends active y := by + classical + funext u + unfold movingStarMean + congr 1 + apply Finset.sum_congr rfl + intro e he + exact h e (Finset.mem_filter.mp he).1 + +lemma movingStarMean_sub {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (x y : E → ℝ) (u : V) : + movingStarMean ends active (fun e => x e-y e) u = + movingStarMean ends active x u-movingStarMean ends active y u := by + classical + simp only [movingStarMean,Finset.sum_sub_distrib,sub_div] + +lemma movingStarMean_zero_degree {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (x : E → ℝ) (u : V) + (hd : movingStarDegree ends active u = 0) : movingStarMean ends active x u = 0 := by + simp [movingStarMean,hd] + +lemma movingStarMean_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (x : E → ℝ) (u : V) + {M : ℝ} (hM : 0 ≤ M) (hx : ∀ e ∈ active, |x e| ≤ M) : + |movingStarMean ends active x u| ≤ M := by + classical + by_cases hd : movingStarDegree ends active u = 0 + · simpa [movingStarMean_zero_degree ends active x u hd] using hM + · have hp : 0 < movingStarDegree ends active u := + lt_of_le_of_ne (movingStarDegree_nonneg ends active u) (Ne.symm hd) + unfold movingStarMean + rw [abs_div,abs_of_pos hp] + apply (div_le_iff₀ hp).mpr + calc + _ ≤ ∑ e ∈ active.filter (fun e => u ∈ ends e), |x e| := Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ _e ∈ active.filter (fun e => u ∈ ends e), M := by + apply Finset.sum_le_sum + intro e he + exact hx e (Finset.mem_filter.mp he).1 + _ = _ := by simp [movingStarDegree,mul_comm] + +lemma movingStarLift_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) (b : V → ℝ) + {B : ℝ} (hb : ∀ v, |b v| ≤ B) (e : E) : |movingStarLift ends b e| ≤ 2*B := by + classical + unfold movingStarLift + calc + _ ≤ ∑ v ∈ ends e, |b v| := Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ _v ∈ ends e, B := Finset.sum_le_sum (fun v _ => hb v) + _ = _ := by simp [hends] + +lemma movingStarOtherMean_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) (active : Finset E) + (b : V → ℝ) (u : V) {B : ℝ} (hB : 0 ≤ B) (hb : ∀ v, |b v| ≤ B) : + |movingStarOtherMean ends active b u| ≤ B := by + classical + by_cases hd : movingStarDegree ends active u = 0 + · simpa [movingStarOtherMean,hd] using hB + · have hp : 0 < movingStarDegree ends active u := + lt_of_le_of_ne (movingStarDegree_nonneg ends active u) (Ne.symm hd) + unfold movingStarOtherMean + rw [abs_div,abs_of_pos hp] + apply (div_le_iff₀ hp).mpr + calc + _ ≤ ∑ e ∈ active.filter (fun e => u ∈ ends e), |∑ v ∈ (ends e).erase u, b v| := + Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ _e ∈ active.filter (fun e => u ∈ ends e), B := by + apply Finset.sum_le_sum + intro e he + have hu := (Finset.mem_filter.mp he).2 + have hc : ((ends e).erase u).card = 1 := by + rw [Finset.card_erase_of_mem hu,hends] + calc + _ ≤ ∑ v ∈ (ends e).erase u, |b v| := Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ _v ∈ (ends e).erase u, B := Finset.sum_le_sum (fun v _ => hb v) + _ = B := by simp [hc] + _ = _ := by simp [movingStarDegree,mul_comm] + +lemma movingStarMean_lift {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (b : V → ℝ) (u : V) + (hd : movingStarDegree ends active u ≠ 0) : + movingStarMean ends active (movingStarLift ends b) u = + b u + movingStarOtherMean ends active b u := by + classical + have he : (∑ e ∈ active.filter (fun e => u ∈ ends e), movingStarLift ends b e) = + movingStarDegree ends active u*b u + + ∑ e ∈ active.filter (fun e => u ∈ ends e), ∑ v ∈ (ends e).erase u, b v := by + calc + _ = ∑ e ∈ active.filter (fun e => u ∈ ends e), (b u+∑ v ∈ (ends e).erase u, b v) := by + apply Finset.sum_congr rfl + intro e he + exact (Finset.add_sum_erase _ b (Finset.mem_filter.mp he).2).symm + _ = _ := by simp [Finset.sum_add_distrib,movingStarDegree] + unfold movingStarMean movingStarOtherMean + rw [he,add_div,mul_div_cancel_left₀ _ hd] + +theorem movingStar_vertex_step_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) (active : Finset E) + (b : V → ℝ) (M : E → ℝ) {B R a : ℝ} (hB : 0 ≤ B) (hR : 0 ≤ R) + (ha : 0 ≤ a) (ha1 : a ≤ 1) (hb : ∀ u, |b u| ≤ B) + (hM : ∀ e ∈ active, |M e| ≤ R) (u : V) : + |b u + a*movingStarMean ends active (fun e => M e-movingStarLift ends b e) u| ≤ B+a*R := by + classical + rw [movingStarMean_sub] + by_cases hd : movingStarDegree ends active u = 0 + · rw [movingStarMean_zero_degree ends active M u hd, + movingStarMean_zero_degree ends active (movingStarLift ends b) u hd] + simp only [sub_self,mul_zero,add_zero] + exact (hb u).trans (le_add_of_nonneg_right (mul_nonneg ha hR)) + · rw [movingStarMean_lift ends active b u hd] + have hid : b u+a*(movingStarMean ends active M u-(b u+movingStarOtherMean ends active b u)) = + (1-a)*b u-a*movingStarOtherMean ends active b u+a*movingStarMean ends active M u := by ring + rw [hid] + calc + _ ≤ |(1-a)*b u-a*movingStarOtherMean ends active b u|+ + |a*movingStarMean ends active M u| := abs_add_le _ _ + _ ≤ (|(1-a)*b u|+|a*movingStarOtherMean ends active b u|)+ + |a*movingStarMean ends active M u| := by + exact add_le_add (moving_abs_sub_bound _ _) le_rfl + _ = ((1-a)*|b u|+a*|movingStarOtherMean ends active b u|)+ + a*|movingStarMean ends active M u| := by + rw [abs_mul,abs_mul,abs_mul,abs_of_nonneg (sub_nonneg.mpr ha1),abs_of_nonneg ha] + _ ≤ ((1-a)*B+a*B)+a*R := by + gcongr + · exact hb u + · exact movingStarOtherMean_bound ends hends active b u hB hb + · exact movingStarMean_bound ends active M u hR hM + _ = _ := by ring + +noncomputable def movingStarVertices {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : ℕ → Finset E) (a : ℕ → ℝ) + (M : ℕ → E → ℝ) : ℕ → V → ℝ + | 0 => fun _ => 0 + | k+1 => fun u => movingStarVertices ends active a M k u + a k * + movingStarMean ends (active k) + (fun e => M k e-movingStarLift ends (movingStarVertices ends active a M k) e) u + +theorem movingStarVertices_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) + (active : ℕ → Finset E) (a : ℕ → ℝ) (M : ℕ → E → ℝ) (T : ℕ) + {R : ℝ} (hR : 0 ≤ R) (ha : ∀ k < T, 0 ≤ a k ∧ a k ≤ 1) + (hM : ∀ k < T, ∀ e ∈ active k, |M k e| ≤ R) : + ∀ u, |movingStarVertices ends active a M T u| ≤ R*∑ k ∈ Finset.range T, a k := by + induction T with + | zero => intro u; simp [movingStarVertices] + | succ T ih => + have haT := ha T (by omega) + have hb := ih (fun k hk => ha k (by omega)) (fun k hk => hM k (by omega)) + have hB : 0 ≤ R*∑ k ∈ Finset.range T, a k := by + exact mul_nonneg hR (Finset.sum_nonneg (fun k hk => (ha k (by have := Finset.mem_range.mp hk; omega)).1)) + intro u + have hh := movingStar_vertex_step_bound ends hends (active T) + (movingStarVertices ends active a M T) (M T) hB hR haT.1 haT.2 hb (hM T (by omega)) u + simpa only [movingStarVertices,Finset.sum_range_succ,mul_add,mul_comm] using hh + +theorem movingStar_trajectory_representation {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : ℕ → Finset E) (a : ℕ → ℝ) + (M x : ℕ → E → ℝ) (T : ℕ) + (hnest : ∀ k < T, active (k+1) ⊆ active k) + (hinit : ∀ e ∈ active 0, x 0 e = M 0 e) + (hstep : ∀ k < T, ∀ e ∈ active (k+1), x (k+1) e = x k e - + a k*movingStarLift ends (movingStarMean ends (active k) (x k)) e + (M (k+1) e-M k e)) : + ∀ e ∈ active T, x T e = M T e-movingStarLift ends (movingStarVertices ends active a M T) e := by + induction T with + | zero => intro e he; simpa [movingStarVertices,movingStarLift] using hinit e he + | succ T ih => + have hx := ih (fun k hk => hnest k (by omega)) (fun k hk => hstep k (by omega)) + have hm := movingStarMean_congr ends (active T) hx + intro e he + rw [hstep T (by omega) e he,hx e (hnest T (by omega) he),hm] + simp only [movingStarVertices,movingStarLift,Finset.sum_add_distrib,Finset.mul_sum] + ring + +theorem movingStar_trajectory_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) + (active : ℕ → Finset E) (a : ℕ → ℝ) (M x : ℕ → E → ℝ) (T : ℕ) + (hnest : ∀ k < T, active (k+1) ⊆ active k) + (hinit : ∀ e ∈ active 0, x 0 e = M 0 e) + (hstep : ∀ k < T, ∀ e ∈ active (k+1), x (k+1) e = x k e - + a k*movingStarLift ends (movingStarMean ends (active k) (x k)) e + (M (k+1) e-M k e)) + {R : ℝ} (hR : 0 ≤ R) (ha : ∀ k < T, 0 ≤ a k ∧ a k ≤ 1) + (hM : ∀ k ≤ T, ∀ e ∈ active k, |M k e| ≤ R) : + ∀ e ∈ active T, |x T e| ≤ R*(1+2*∑ k ∈ Finset.range T, a k) := by + have hb := movingStarVertices_bound ends hends active a M T hR ha (fun k hk => hM k (by omega)) + intro e he + rw [movingStar_trajectory_representation ends active a M x T hnest hinit hstep e he] + calc + _ ≤ |M T e|+|movingStarLift ends (movingStarVertices ends active a M T) e| := moving_abs_sub_bound _ _ + _ ≤ R+2*(R*∑ k ∈ Finset.range T, a k) := add_le_add (hM T le_rfl e he) + (movingStarLift_bound ends hends _ hb e) + _ = _ := by ring + +lemma movingStar_incidence_swap {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (f : E → V → ℝ) : + (∑ e ∈ active, ∑ u ∈ ends e, f e u) = + ∑ u, ∑ e ∈ active.filter (fun e => u ∈ ends e), f e u := by + classical + have he (e : E) : (∑ u ∈ ends e, f e u) = ∑ u, if u ∈ ends e then f e u else 0 := by simp + simp_rw [he] + rw [Finset.sum_comm] + simp only [Finset.sum_filter] + +lemma movingStarMean_mass {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (x : E → ℝ) (u : V) : + movingStarDegree ends active u*movingStarMean ends active x u = + ∑ e ∈ active.filter (fun e => u ∈ ends e), x e := by + classical + by_cases hd : movingStarDegree ends active u = 0 + · have hc : (active.filter (fun e => u ∈ ends e)).card = 0 := by + exact_mod_cast (show ((active.filter (fun e => u ∈ ends e)).card : ℝ) = 0 from hd) + have he := Finset.card_eq_zero.mp hc + simp [movingStarMean,hd,he] + · unfold movingStarMean + exact mul_div_cancel₀ _ hd + +theorem movingStar_energy_identity {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : Finset E) (x : E → ℝ) : + (∑ e ∈ active, x e*movingStarLift ends (movingStarMean ends active x) e) = + ∑ u, movingStarDegree ends active u*(movingStarMean ends active x u)^2 := by + classical + simp only [movingStarLift,Finset.mul_sum] + rw [movingStar_incidence_swap] + apply Finset.sum_congr rfl + intro u _ + rw [← Finset.sum_mul,← movingStarMean_mass ends active x u] + ring + +lemma movingStar_lift_sq_bound {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) + (active : Finset E) (b : V → ℝ) : + (∑ e ∈ active, (movingStarLift ends b e)^2) ≤ + 2*∑ u, movingStarDegree ends active u*(b u)^2 := by + classical + calc + _ ≤ ∑ e ∈ active, 2*∑ u ∈ ends e, (b u)^2 := by + apply Finset.sum_le_sum + intro e _ + simpa only [movingStarLift,hends,Nat.cast_ofNat] using + (sq_sum_le_card_mul_sum_sq (s := ends e) (f := b)) + _ = _ := by + rw [← Finset.mul_sum,movingStar_incidence_swap] + congr 1 + apply Finset.sum_congr rfl + intro u _ + simp [movingStarDegree] + +theorem movingStar_euler_energy_le {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) + (active : Finset E) (x : E → ℝ) {a : ℝ} (ha : 0 ≤ a) (ha1 : a ≤ 1) : + (∑ e ∈ active, (x e-a*movingStarLift ends (movingStarMean ends active x) e)^2) ≤ + ∑ e ∈ active, (x e)^2 := by + classical + let P := movingStarLift ends (movingStarMean ends active x) + let V := ∑ u, movingStarDegree ends active u*(movingStarMean ends active x u)^2 + have hV : 0 ≤ V := Finset.sum_nonneg (fun u _ => + mul_nonneg (movingStarDegree_nonneg ends active u) (sq_nonneg _)) + have hlin : (∑ e ∈ active, x e*P e) = V := movingStar_energy_identity ends active x + have hsq : (∑ e ∈ active, (P e)^2) ≤ 2*V := + movingStar_lift_sq_bound ends hends active (movingStarMean ends active x) + have hexpand : (∑ e ∈ active, (x e-a*P e)^2) = + (∑ e ∈ active, (x e)^2)-2*a*(∑ e ∈ active, x e*P e)+ + a^2*(∑ e ∈ active, (P e)^2) := by + calc + _ = ∑ e ∈ active, ((x e)^2-2*a*(x e*P e)+a^2*(P e)^2) := by + apply Finset.sum_congr rfl + intro e _ + ring + _ = _ := by simp only [Finset.sum_add_distrib,Finset.sum_sub_distrib,Finset.mul_sum] + change (∑ e ∈ active, (x e-a*P e)^2) ≤ _ + rw [hexpand,hlin] + have hsq' := mul_le_mul_of_nonneg_left hsq (sq_nonneg a) + have hab : a^2 ≤ a := by nlinarith + have hb := mul_le_mul_of_nonneg_right hab hV + nlinarith only [hsq',hb] + +theorem movingStar_euler_energy_subset_le {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) + (active remaining : Finset E) (hsub : remaining ⊆ active) (x : E → ℝ) + {a : ℝ} (ha : 0 ≤ a) (ha1 : a ≤ 1) : + (∑ e ∈ remaining, (x e-a*movingStarLift ends (movingStarMean ends active x) e)^2) ≤ + ∑ e ∈ active, (x e)^2 := by + apply le_trans _ (movingStar_euler_energy_le ends hends active x ha ha1) + exact Finset.sum_le_sum_of_subset_of_nonneg hsub (fun _ _ _ => sq_nonneg _) + +end SharpTerminalLeave +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/NormalizedOnes.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/NormalizedOnes.lean new file mode 100644 index 0000000000000000000000000000000000000000..2de09772a8f6a7840245f2b2a451e0655a4d2fc2 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/NormalizedOnes.lean @@ -0,0 +1,143 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.UnitLineRemainder +import OAI.Combinatorics.TriangleRemoval.Process.EigenProjector +import OAI.Combinatorics.TriangleRemoval.Process.EigenvalueAbsRowsum + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace + +namespace SharpTerminalLeave + +variable {ι : Type*} [Fintype ι] [DecidableEq ι] [Nonempty ι] + +noncomputable def normalizedOnes : EuclideanSpace ℝ ι := + WithLp.toLp 2 (fun _ => (Real.sqrt (Fintype.card ι : ℝ))⁻¹) + +noncomputable def averagingProjector : Matrix ι ι ℝ := + fun _ _ => (Fintype.card ι : ℝ)⁻¹ + +omit [DecidableEq ι] in +theorem normalizedOnes_norm : ‖(normalizedOnes : EuclideanSpace ℝ ι)‖ = 1 := by + have hn : 0 < (Fintype.card ι : ℝ) := Nat.cast_pos.mpr Fintype.card_pos + have hs : (Real.sqrt (Fintype.card ι : ℝ)) ^ 2 = Fintype.card ι := + Real.sq_sqrt (le_of_lt hn) + have hnorm : ‖(normalizedOnes : EuclideanSpace ℝ ι)‖ ^ 2 = 1 := by + rw [EuclideanSpace.norm_sq_eq] + simp only [normalizedOnes, WithLp.ofLp_toLp, Real.norm_eq_abs, sq_abs, + Finset.sum_const, Finset.card_univ, nsmul_eq_mul, inv_pow, hs] + exact mul_inv_cancel₀ (ne_of_gt hn) + nlinarith [norm_nonneg (normalizedOnes : EuclideanSpace ℝ ι)] + +theorem normalizedOnes_residual (A : Matrix ι ι ℝ) (δ : ℝ) (hδ : 0 ≤ δ) + (hrow : ∀ i, |(∑ j, A i j) - 1| ≤ δ) : + ‖(Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) A normalizedOnes - normalizedOnes‖ ≤ δ := by + have hn : 0 < (Fintype.card ι : ℝ) := Nat.cast_pos.mpr Fintype.card_pos + have hs : (Real.sqrt (Fintype.card ι : ℝ)) ^ 2 = Fintype.card ι := + Real.sq_sqrt (le_of_lt hn) + have hcoords : ∀ i, ((Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) A normalizedOnes - normalizedOnes).ofLp i = + ((∑ j, A i j) - 1) * (Real.sqrt (Fintype.card ι : ℝ))⁻¹ := by + intro i + simp [normalizedOnes, Matrix.toEuclideanCLM_toLp, Matrix.mulVec, dotProduct, + Finset.sum_mul, sub_mul] + have hsq : ‖(Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) A normalizedOnes - normalizedOnes‖ ^ 2 ≤ δ ^ 2 := by + rw [EuclideanSpace.norm_sq_eq] + calc + _ = ∑ i, |((∑ j, A i j) - 1)| ^ 2 * (Real.sqrt (Fintype.card ι : ℝ))⁻¹ ^ 2 := by + simp only [hcoords, Real.norm_eq_abs, sq_abs, mul_pow] + _ ≤ ∑ _ : ι, δ ^ 2 * (Real.sqrt (Fintype.card ι : ℝ))⁻¹ ^ 2 := by + apply Finset.sum_le_sum + intro i _ + exact mul_le_mul_of_nonneg_right + (pow_le_pow_left₀ (abs_nonneg _) (hrow i) 2) (sq_nonneg _) + _ = δ ^ 2 := by + simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul, inv_pow, hs] + field_simp + nlinarith [norm_nonneg ((Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) A normalizedOnes - normalizedOnes)] + +omit [Nonempty ι] in +theorem eigenProjector_entry (A : Matrix ι ι ℝ) (hA : A.IsHermitian) (k i j : ι) : + eigenProjector A hA k i j = hA.eigenvectorBasis k i * hA.eigenvectorBasis k j := by + simp [eigenProjector, Unitary.conjStarAlgAut_apply, Matrix.mul_apply, + Matrix.diagonal_apply, Pi.single_apply] + +omit [Nonempty ι] in +theorem eigenProjector_toCLM (A : Matrix ι ι ℝ) (hA : A.IsHermitian) (k : ι) : + (Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) (eigenProjector A hA k) = + rankOne ℝ (hA.eigenvectorBasis k) (hA.eigenvectorBasis k) := by + ext x i + change ((eigenProjector A hA k) *ᵥ x.ofLp) i = _ + simp only [rankOne_apply, PiLp.smul_apply, smul_eq_mul, Matrix.mulVec, dotProduct, + eigenProjector_entry, PiLp.inner_apply, RCLike.inner_apply, conj_trivial] + simp_rw [Finset.sum_mul] + apply Finset.sum_congr rfl + intro j _ + ring + +omit [Nonempty ι] in +theorem averagingProjector_toCLM : + (Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) (averagingProjector : Matrix ι ι ℝ) = + rankOne ℝ (normalizedOnes : EuclideanSpace ℝ ι) normalizedOnes := by + have hn : 0 ≤ (Fintype.card ι : ℝ) := Nat.cast_nonneg _ + have hs : (Real.sqrt (Fintype.card ι : ℝ))⁻¹ ^ 2 = (Fintype.card ι : ℝ)⁻¹ := by + rw [inv_pow, Real.sq_sqrt hn] + ext x i + change (averagingProjector *ᵥ x.ofLp) i = _ + simp only [rankOne_apply, PiLp.smul_apply, smul_eq_mul, Matrix.mulVec, dotProduct, + averagingProjector, PiLp.inner_apply, RCLike.inner_apply, conj_trivial, + normalizedOnes] + rw [Finset.sum_mul] + apply Finset.sum_congr rfl + intro j _ + nlinarith [congrArg (fun t : ℝ => t * x.ofLp j) hs] + +theorem star_approximation_from_trace (A : Matrix ι ι ℝ) (hA : A.IsHermitian) + (δ θ : ℝ) (hδ0 : 0 ≤ δ) (hδ1 : δ ≤ 1) (hθ : 0 ≤ θ) + (hpos : ∀ i j, 0 ≤ A i j) (hrow : ∀ i, |(∑ j, A i j) - 1| ≤ δ) + (r : ℕ) (hr : Even r) (hr0 : r ≠ 0) + (htrace : (A ^ r).trace ≤ (1 - δ) ^ r + θ ^ r) : + ‖A - averagingProjector‖ ≤ 4 * θ + 7 * δ := by + have hrlo : ∀ i, 1 - δ ≤ ∑ j, A i j := fun i => by + have hh := (abs_le.mp (hrow i)).1 + linarith + have hrhi : ∀ i, ∑ j, |A i j| ≤ 1 + δ := fun i => by + simp_rw [abs_of_nonneg (hpos i _)] + have hh := (abs_le.mp (hrow i)).2 + linarith + obtain ⟨k, hklo, hkhi, _⟩ := exists_leading_of_rowsums A hA (1-δ) (1+δ) + (by linarith) hrlo hrhi + have hlam : |hA.eigenvalues k - 1| ≤ δ := by + have hhi := (abs_le.mp hkhi).2 + exact abs_le.mpr ⟨by linarith, by linarith⟩ + have htail := eigenvalue_tail_of_trace A hA k r hr hr0 (1-δ) θ + (by linarith) hθ hklo htrace + have happrox := eigenProjector_approximation A hA k θ hθ htail + have heig : ‖(Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) A - hA.eigenvalues k • + rankOne ℝ (hA.eigenvectorBasis k) (hA.eigenvectorBasis k)‖ ≤ θ := by + rw [← eigenProjector_toCLM A hA k, ← map_smul, ← map_sub, + Matrix.l2_opNorm_toEuclideanCLM] + exact happrox + have hfinal := projector_transfer ((Matrix.toEuclideanCLM (n := ι) (𝕜 := ℝ)) A) + (hA.eigenvectorBasis k) normalizedOnes (hA.eigenvectorBasis.norm_eq_one k) + normalizedOnes_norm (hA.eigenvalues k) θ δ δ heig hlam + (normalizedOnes_residual A δ hδ0 hrow) + rw [← averagingProjector_toCLM, ← map_sub, Matrix.l2_opNorm_toEuclideanCLM] at hfinal + linarith + +end SharpTerminalLeave + +open scoped BigOperators + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/RealTraceAdjMatrix.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/RealTraceAdjMatrix.lean new file mode 100644 index 0000000000000000000000000000000000000000..5a3c7d839a821a069f39b119c1996b96ebf6b6bd --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/RealTraceAdjMatrix.lean @@ -0,0 +1,132 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.ClosedWalkFamily + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave +section RealTrace +variable {V : Type*} [Fintype V] [DecidableEq V] +variable (G : SimpleGraph V) [DecidableRel G.Adj] + +lemma real_trace_adjMatrix_eq (n : ℕ) : + Matrix.trace (G.adjMatrix ℝ ^ (n+3)) = + ((graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card : ℝ) + + (nonsimpleWords G (n+3)).card := by + have hNat := trace_adjMatrix_eq_closedWords G n + rw [closedWords_card_eq] at hNat + have ht : Matrix.trace (G.adjMatrix ℝ ^ (n+3)) = + ((Matrix.trace (G.adjMatrix ℕ ^ (n+3)) : ℕ) : ℝ) := by + simp only [Matrix.trace, Matrix.diag_apply, Nat.cast_sum, + SimpleGraph.adjMatrix_pow_apply_eq_card_walk, Nat.cast_id] + rw [ht, hNat, Nat.cast_add] + +theorem normalized_link_trace_bound (n : ℕ) (hr : Even (n+3)) (D δ : ℝ) + (hD : 1 ≤ D) + (hdeg : ∀ x : V, ((Finset.univ.filter (G.Adj x)).card : ℝ) ≤ 2*D) + (hmain : |((graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card : ℝ) - D^(n+3)| ≤ + δ * D^(n+3)) + (hcyc : ∀ b, 3 ≤ b → b < n+3 → + ((graphEmbeddings (SimpleGraph.cycleGraph b) G).card : ℝ) ≤ 2 * D^b) : + |Matrix.trace (((D⁻¹) • G.adjMatrix ℝ) ^ (n+3)) - 1| ≤ + δ + ((n+3 : ℕ) : ℝ)^(n+4) * 2^(n+3) * + ((Fintype.card V : ℝ) / D^((n+3)/2) + 1/D) := by + classical + have hD0 : 0 < D := lt_of_lt_of_le zero_lt_one hD + have hDne : D ≠ 0 := ne_of_gt hD0 + let Δ : ℕ := ⌊2*D⌋₊ + have hΔlo : D ≤ (Δ : ℝ) := by + have hh := Nat.lt_floor_add_one (2*D) + change 2*D < (Δ : ℝ)+1 at hh + linarith + have hΔhi : (Δ : ℝ) ≤ 2*D := Nat.floor_le (by positivity) + have hΔ0 : 1 ≤ Δ := by exact_mod_cast hD.trans hΔlo + have hdeg' : ∀ x : V, (Finset.univ.filter (G.Adj x)).card ≤ Δ := by + intro x + exact (Nat.le_floor_iff (by positivity : 0 ≤ 2*D)).mpr (hdeg x) + have hcyc' : ∀ b, 3 ≤ b → b < n+3 → + (graphEmbeddings (SimpleGraph.cycleGraph b) G).card ≤ 2 * Δ^b := by + intro b hb hbr + have hh := (hcyc b hb hbr).trans (mul_le_mul_of_nonneg_left + (pow_le_pow_left₀ hD0.le hΔlo b) (by norm_num : (0:ℝ) ≤ 2)) + exact_mod_cast hh + have hcount := nonsimpleWords_card_le G n Δ 2 hΔ0 hdeg' hcyc' + have hcountR : ((nonsimpleWords G (n+3)).card : ℝ) ≤ + ((n+3 : ℕ) : ℝ)^(n+4) * + ((Fintype.card V : ℝ) * (2*D)^((n+3)/2) + 2*(2*D)^(n+2)) := by + have hh : ((nonsimpleWords G (n+3)).card : ℝ) ≤ + ((n+3 : ℕ) : ℝ)^(n+4) * + ((Fintype.card V : ℝ) * (Δ:ℝ)^((n+3)/2) + 2*(Δ:ℝ)^(n+2)) := by + exact_mod_cast hcount + refine hh.trans (mul_le_mul_of_nonneg_left ?_ (by positivity)) + exact add_le_add + (mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (Nat.cast_nonneg Δ) hΔhi _) (Nat.cast_nonneg _)) + (mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (Nat.cast_nonneg Δ) hΔhi _) (by norm_num)) + have hhalf : (n+3)/2 + (n+3)/2 = n+3 := by + obtain ⟨k, hk⟩ := hr + omega + have hhalfle : (n+3)/2 ≤ n+3 := Nat.div_le_self _ _ + have hp2 : (2:ℝ)^((n+3)/2) ≤ 2^(n+3) := pow_le_pow_right₀ (by norm_num) hhalfle + have hpow : D^(n+3) = D^((n+3)/2) * D^((n+3)/2) := by rw [← pow_add, hhalf] + have hpow' : D^(n+3) = D^(n+2) * D := by rw [← pow_succ] + have hratio : ((nonsimpleWords G (n+3)).card : ℝ) / D^(n+3) ≤ + ((n+3 : ℕ) : ℝ)^(n+4) * 2^(n+3) * + ((Fintype.card V : ℝ) / D^((n+3)/2) + 1/D) := by + apply (div_le_iff₀ (pow_pos hD0 _)).mpr + have htree : (Fintype.card V : ℝ) * (2*D)^((n+3)/2) ≤ + 2^(n+3) * ((Fintype.card V : ℝ)/D^((n+3)/2)) * D^(n+3) := by + rw [mul_pow, hpow] + field_simp + nlinarith [mul_le_mul_of_nonneg_right hp2 + (mul_nonneg (Nat.cast_nonneg (Fintype.card V)) (pow_nonneg hD0.le ((n+3)/2)))] + have hcycle : 2*(2*D)^(n+2) = 2^(n+3) * (1/D) * D^(n+3) := by + rw [mul_pow, hpow', show n+3 = (n+2)+1 by omega, pow_succ] + field_simp + ring + calc + _ ≤ ((n+3 : ℕ) : ℝ)^(n+4) * + ((Fintype.card V : ℝ) * (2*D)^((n+3)/2) + 2*(2*D)^(n+2)) := hcountR + _ ≤ ((n+3 : ℕ) : ℝ)^(n+4) * + (2^(n+3) * ((Fintype.card V : ℝ)/D^((n+3)/2)) * D^(n+3) + + 2^(n+3) * (1/D) * D^(n+3)) := mul_le_mul_of_nonneg_left (add_le_add htree hcycle.le) (by positivity) + _ = _ := by ring + have htrace : Matrix.trace (((D⁻¹) • G.adjMatrix ℝ) ^ (n+3)) = + (((graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card : ℝ) + + (nonsimpleWords G (n+3)).card) / D^(n+3) := by + rw [smul_pow, Matrix.trace_smul, real_trace_adjMatrix_eq] + simp only [smul_eq_mul, inv_pow, div_eq_mul_inv, mul_comm] + rw [htrace] + calc + _ = |(((graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card : ℝ) - D^(n+3)) / + D^(n+3) + (nonsimpleWords G (n+3)).card / D^(n+3)| := by + congr 1 + field_simp + ring + _ ≤ |(((graphEmbeddings (SimpleGraph.cycleGraph (n+3)) G).card : ℝ) - D^(n+3)) / + D^(n+3)| + |((nonsimpleWords G (n+3)).card : ℝ) / D^(n+3)| := abs_add_le _ _ + _ ≤ δ + ((n+3 : ℕ) : ℝ)^(n+4) * 2^(n+3) * + ((Fintype.card V : ℝ) / D^((n+3)/2) + 1/D) := by + apply add_le_add + · rw [abs_div, abs_of_pos (pow_pos hD0 _)] + exact (div_le_iff₀ (pow_pos hD0 _)).mpr hmain + · rw [abs_of_nonneg (div_nonneg (Nat.cast_nonneg _) (pow_nonneg hD0.le _))] + exact hratio + +end RealTrace +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/StarInclusion.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/StarInclusion.lean new file mode 100644 index 0000000000000000000000000000000000000000..9193af2c83c76d51780ff79f4153b40c1c76a940 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/StarInclusion.lean @@ -0,0 +1,127 @@ +import Mathlib + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix InnerProductSpace +open scoped BigOperators +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix + +namespace SharpTerminalLeave + +variable {E U : Type*} [Fintype E] [DecidableEq E] [Fintype U] + +def starInclusion (S : Finset E) : Matrix E S ℝ := + fun e f => if e = f.val then 1 else 0 + +noncomputable def extendStar (S : Finset E) (M : Matrix S S ℝ) : Matrix E E ℝ := + starInclusion S * M * (starInclusion S)ᴴ + +theorem starInclusion_mulVec (S : Finset E) (x : E → ℝ) : + (starInclusion S)ᴴ *ᵥ x = fun e : S => x e.val := by + ext e + simp [starInclusion,Matrix.mulVec,dotProduct] + +theorem extendStar_quadratic (S : Finset E) (M : Matrix S S ℝ) (x : E → ℝ) : + x ⬝ᵥ (extendStar S M *ᵥ x) = + (fun e : S => x e.val) ⬝ᵥ (M *ᵥ fun e : S => x e.val) := by + rw [extendStar,← Matrix.mulVec_mulVec,← Matrix.mulVec_mulVec,starInclusion_mulVec, + Matrix.dotProduct_mulVec] + congr 1 + ext e + simp [starInclusion,Matrix.vecMul,dotProduct] + +theorem matrix_quadratic_abs (M : Matrix E E ℝ) (x : E → ℝ) : + |x ⬝ᵥ (M *ᵥ x)| ≤ ‖M‖ * ∑ e, (x e)^2 := by + let y : EuclideanSpace ℝ E := WithLp.toLp 2 x + let T := (Matrix.toEuclideanCLM (n := E) (𝕜 := ℝ)) M + have heq : x ⬝ᵥ (M *ᵥ x) = inner ℝ y (T y) := by + simp [T,y,Matrix.toEuclideanCLM_toLp,EuclideanSpace.inner_eq_star_dotProduct,dotProduct, + mul_comm] + have hn : ‖y‖^2 = ∑ e, (x e)^2 := by + simp [EuclideanSpace.norm_sq_eq,y,Real.norm_eq_abs] + rw [heq] + calc + _ ≤ ‖y‖ * ‖T y‖ := abs_real_inner_le_norm _ _ + _ ≤ ‖y‖ * (‖T‖ * ‖y‖) := mul_le_mul_of_nonneg_left (T.le_opNorm y) (norm_nonneg _) + _ = ‖M‖ * ∑ e, (x e)^2 := by + rw [mul_left_comm,← pow_two,hn] + exact congrArg (fun a : ℝ => a * ∑ e, (x e)^2) (Matrix.l2_opNorm_toEuclideanCLM M) + +theorem hermitian_norm_le_of_quadratic (M : Matrix E E ℝ) (hM : M.IsHermitian) + (a : ℝ) (ha : 0 ≤ a) + (hquad : ∀ x : E → ℝ, |x ⬝ᵥ (M *ᵥ x)| ≤ a * ∑ e, (x e)^2) : ‖M‖ ≤ a := by + have hev : ∀ e, |hM.eigenvalues e| ≤ a := by + intro e + have h := hquad (hM.eigenvectorBasis e).ofLp + have hn : ∑ i, ((hM.eigenvectorBasis e).ofLp i)^2 = 1 := by + have hh := hM.eigenvectorBasis.norm_eq_one e + have hs := EuclideanSpace.norm_sq_eq (hM.eigenvectorBasis e) + simpa [hh,Real.norm_eq_abs] using hs.symm + rw [hn,mul_one] at h + simpa [hM.eigenvalues_eq e] using h + conv_lhs => rw [hM.spectral_theorem] + rw [Unitary.conjStarAlgAut_apply,← Unitary.coe_star,CStarRing.norm_mul_coe_unitary, + CStarRing.norm_coe_unitary_mul,Matrix.l2_opNorm_diagonal] + exact (pi_norm_le_iff_of_nonneg ha).mpr (fun e => hev e) + +theorem sum_star_norm_bound (S : U → Finset E) (M : (u : U) → Matrix (S u) (S u) ℝ) + (hM : ∀ u, (M u).IsHermitian) (ε K : ℝ) (hε : 0 ≤ ε) (hK : 0 ≤ K) + (hlocal : ∀ u, ‖M u‖ ≤ ε) + (hoverlap : ∀ e : E, ((Finset.univ.filter fun u => e ∈ S u).card : ℝ) ≤ K) : + ‖∑ u, extendStar (S u) (M u)‖ ≤ K*ε := by + have hsym : (∑ u, extendStar (S u) (M u)).IsHermitian := by + apply Matrix.IsHermitian.ext + intro i j + simp only [Matrix.sum_apply,star_sum] + apply Finset.sum_congr rfl + intro u _ + exact (Matrix.isHermitian_mul_mul_conjTranspose (starInclusion (S u)) (hM u)).apply i j + apply hermitian_norm_le_of_quadratic _ hsym (K*ε) (mul_nonneg hK hε) + intro x + have hsum : x ⬝ᵥ ((∑ u, extendStar (S u) (M u)) *ᵥ x) = + ∑ u, (fun e : S u => x e.val) ⬝ᵥ (M u *ᵥ fun e : S u => x e.val) := by + simp only [Matrix.sum_mulVec,dotProduct_sum,extendStar_quadratic] + have hcount : (∑ u, ∑ e : S u, (x e.val)^2) ≤ K * ∑ e, (x e)^2 := by + have hrewrite : ∀ u, (∑ e : S u, (x e.val)^2) = + ∑ e : E, if e ∈ S u then (x e)^2 else 0 := by + intro u + rw [← Finset.sum_filter] + exact (Finset.sum_subtype (Finset.univ.filter fun e => e ∈ S u) + (fun e => by simp only [Finset.mem_filter,Finset.mem_univ,true_and]) + (fun e => (x e)^2)).symm + simp_rw [hrewrite] + rw [Finset.sum_comm,Finset.mul_sum] + apply Finset.sum_le_sum + intro e _ + have heq : (∑ u : U, if e ∈ S u then (x e)^2 else 0) = + ((Finset.univ.filter fun u => e ∈ S u).card : ℝ) * (x e)^2 := by + rw [← Finset.sum_filter,Finset.sum_const,nsmul_eq_mul] + rw [heq] + exact mul_le_mul_of_nonneg_right (hoverlap e) (sq_nonneg _) + rw [hsum] + calc + _ ≤ ∑ u, |(fun e : S u => x e.val) ⬝ᵥ (M u *ᵥ fun e : S u => x e.val)| := + Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ u, ε * ∑ e : S u, (x e.val)^2 := by + apply Finset.sum_le_sum + intro u _ + exact (matrix_quadratic_abs _ _).trans + (mul_le_mul_of_nonneg_right (hlocal u) (Finset.sum_nonneg fun _ _ => sq_nonneg _)) + _ = ε * (∑ u, ∑ e : S u, (x e.val)^2) := by rw [Finset.mul_sum] + _ ≤ ε * (K * ∑ e, (x e)^2) := mul_le_mul_of_nonneg_left hcount hε + _ = K * ε * ∑ e, (x e)^2 := by ring + +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQueryAddress.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQueryAddress.lean new file mode 100644 index 0000000000000000000000000000000000000000..ffefcb16e1df150530af6147a27286024b781aac --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQueryAddress.lean @@ -0,0 +1,133 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Asymptotics.FreshLogExposeLabeled +import OAI.Combinatorics.TriangleRemoval.Spectral.FreshLogCheckNone2 + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave +section TraceAddress +variable {ι τ : Type*} [Fintype τ] [DecidableEq ι] [DecidableEq τ] + +theorem tracedGridQuery_address (H : τ → Finset ι) (N d k : ℕ) (c : QueryCall ι τ) + (ν : τ → PMF (Fin N)) {z : (Bool × List (QueryCall ι τ)) × List τ} + (hz : z ∈ (ExposureTree.freshLog ν (tracedGridQuery H N d k c)).support) : + ∀ a ∈ z.1.2, c.address <:+ a.address := by + induction d generalizing k c z with + | zero => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,_,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have heq : y = ((true,[c]),[]) := by simpa [ExposureTree.freshLog] using hy + simp [heq] + | succ d ih => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,_,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + rw [ExposureTree.freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + have hwP := ExposureTree.freshLog_checkNoneTrace_forall ν + (fun a : QueryCall ι τ => c.address <:+ a.address) _ + (by + intro A hA a ha + obtain ⟨p,_,rfl⟩ := List.mem_map.mp hA + split_ifs at ha with hp + · intro b hb + exact (List.suffix_cons p.1 c.address).trans (ih _ _ ha b hb) + · have heq : a = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using ha + simp [heq]) hw + intro a ha + rcases List.mem_cons.mp ha with rfl | ha + · exact ⟨[],rfl⟩ + · exact hwP a ha + +lemma fork_addresses_ne {α : Type*} {p q : α} {base a b : List α} (hpq : p ≠ q) + (ha : p :: base <:+ a) (hb : q :: base <:+ b) : a ≠ b := by + intro hab + subst b + have hh := List.suffix_of_suffix_length_le ha hb (by simp) + have heq := hh.eq_of_length (by simp) + exact hpq (List.cons.inj heq).1 + +lemma child_address_ne {α : Type*} {p : α} {base a : List α} + (ha : p :: base <:+ a) : base ≠ a := by + intro h + have hh := ha.length_le + simp only [List.length_cons, ← h] at hh + omega + +theorem tracedGridQuery_address_nodup (H : τ → Finset ι) (N d k : ℕ) (c : QueryCall ι τ) + (ν : τ → PMF (Fin N)) {z : (Bool × List (QueryCall ι τ)) × List τ} + (hz : z ∈ (ExposureTree.freshLog ν (tracedGridQuery H N d k c)).support) : + (z.1.2.map QueryCall.address).Nodup := by + induction d generalizing k c z with + | zero => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,_,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have heq : y = ((true,[c]),[]) := by simpa [ExposureTree.freshLog] using hy + simp [heq] + | succ d ih => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,hx,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + rw [ExposureTree.freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + have hxvals := (ExposureTree.freshLog_exposeLabeled_support ν Prod.snd _ hx).1 + have hnd : (x.1.map Prod.fst).Nodup := by rw [hxvals]; exact Finset.nodup_toList _ + have hsnd := ((List.mergeSort_perm x.1 (fun a b => a.2.val ≤ b.2.val)).map Prod.fst).nodup_iff.mpr hnd + have hpair : (x.1.mergeSort (fun a b => a.2.val ≤ b.2.val)).Pairwise + (fun a b => a.1 ≠ b.1) := by + simpa only [List.Nodup, List.pairwise_map] using hsnd + have hwR := ExposureTree.freshLog_checkNoneTrace_pairwise ν + (fun a b : QueryCall ι τ => a.address ≠ b.address) _ + (by + intro A hA a ha + obtain ⟨p,_,rfl⟩ := List.mem_map.mp hA + split_ifs at ha with hp + · have hh := ih _ _ ha + simpa only [List.Nodup, List.pairwise_map] using hh + · have heq : a = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using ha + simp [heq]) + (by + apply List.pairwise_map.mpr + apply hpair.imp + intro p q hpq x hx y hy a ha b hb + split_ifs at hx hy with hp hq + · exact fork_addresses_ne hpq + (tracedGridQuery_address H N d _ _ ν hx a ha) + (tracedGridQuery_address H N d _ _ ν hy b hb) + · have heq : y = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using hy + simp [heq] at hb + · have heq : x = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using hx + simp [heq] at ha + · have heq : x = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using hx + simp [heq] at ha) hw + have hwP := ExposureTree.freshLog_checkNoneTrace_forall ν + (fun a : QueryCall ι τ => c.address ≠ a.address) _ + (by + intro A hA a ha + obtain ⟨p,_,rfl⟩ := List.mem_map.mp hA + split_ifs at ha with hp + · intro b hb + exact child_address_ne (tracedGridQuery_address H N d _ _ ν ha b hb) + · have heq : a = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using ha + simp [heq]) hw + simpa only [List.Nodup, List.map_cons, List.pairwise_cons, List.pairwise_map, + List.mem_map, forall_exists_index, and_imp, forall_apply_eq_imp_iff₂] + using (And.intro hwP hwR) + +end TraceAddress +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQueryLog.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQueryLog.lean new file mode 100644 index 0000000000000000000000000000000000000000..154e164db09a8954e3842f39549b1692fe0f233a --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQueryLog.lean @@ -0,0 +1,55 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Asymptotics.FreshLogExposeLabeled + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave +section TraceAccounting +variable {ι τ : Type*} [Fintype τ] [DecidableEq ι] [DecidableEq τ] + +theorem tracedGridQuery_log (H : τ → Finset ι) (N d k : ℕ) (c : QueryCall ι τ) + (ν : τ → PMF (Fin N)) {z : (Bool × List (QueryCall ι τ)) × List τ} + (hz : z ∈ (ExposureTree.freshLog ν (tracedGridQuery H N d k c)).support) : + z.2 = z.1.2.flatMap (QueryCall.keys H) := by + induction d generalizing k c z with + | zero => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,hx,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have heq : y = ((true,[c]),[]) := by + simpa [ExposureTree.freshLog] using hy + have hkeys := (ExposureTree.freshLog_exposeLabeled_support ν Prod.snd _ hx).2 + simp [heq, hkeys, QueryCall.keys] + | succ d ih => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,hx,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + rw [ExposureTree.freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + have hxkeys := (ExposureTree.freshLog_exposeLabeled_support ν Prod.snd _ hx).2 + have hwkeys := ExposureTree.freshLog_checkNoneTrace_support ν (QueryCall.keys H) _ + (by + intro A hA a ha + obtain ⟨p,_,rfl⟩ := List.mem_map.mp hA + split_ifs at ha with hp + · exact ih _ _ ha + · have heq : a = ((false,[]),[]) := by + simpa [ExposureTree.freshLog] using ha + simp [heq]) hw + simp only [List.flatMap_cons, hxkeys, hwkeys, QueryCall.keys] + +end TraceAccounting +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQuerySeparated.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQuerySeparated.lean new file mode 100644 index 0000000000000000000000000000000000000000..40e3525a1181b45aa13d91837f75de5e6bd53803 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/TracedGridQuerySeparated.lean @@ -0,0 +1,88 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.TracedGridQueryLog +import OAI.Combinatorics.TriangleRemoval.Queries.Separated +import OAI.Combinatorics.TriangleRemoval.Spectral.FreshLogCheckNone + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave +section TrueRepeatedCall +variable {ι τ : Type*} [Fintype τ] [DecidableEq ι] [DecidableEq τ] + +theorem tracedGridQuery_separated (H : τ → Finset ι) + (hlin : ∀ T S, T ≠ S → (H T ∩ H S).card ≤ 1) + (N d k : ℕ) (c : QueryCall ι τ) (hc : c.Separated H) + (ν : τ → PMF (Fin N)) {z : (Bool × List (QueryCall ι τ)) × List τ} + (hz : z ∈ (ExposureTree.freshLog ν (tracedGridQuery H N d k c)).support) : + ∀ a ∈ z.1.2, a.Separated H := by + induction d generalizing k c z with + | zero => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,_,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + have heq : y = ((true,[c]),[]) := by simpa [ExposureTree.freshLog] using hy + simpa [heq] using hc + | succ d ih => + rw [tracedGridQuery, ExposureTree.freshLog_bind] at hz + obtain ⟨x,_,hz⟩ := (PMF.mem_support_bind_iff _ _ _).mp hz + obtain ⟨y,hy,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hz + rw [ExposureTree.freshLog_mapOutput] at hy + obtain ⟨w,hw,rfl⟩ := (PMF.mem_support_map_iff _ _ _).mp hy + have hwP := ExposureTree.freshLog_checkNoneTrace_forall ν (fun a => a.Separated H) _ + (by + intro A hA a ha + obtain ⟨p,_,rfl⟩ := List.mem_map.mp hA + split_ifs at ha with hp + · exact ih _ _ (separated_child H hlin _ _ _) ha + · have heq : a = ((false,[]),[]) := by simpa [ExposureTree.freshLog] using ha + simp [heq]) hw + intro a ha + rcases List.mem_cons.mp ha with rfl | ha + · exact hc + · exact hwP a ha + +theorem repeated_flatMap_calls {R K : Type*} [DecidableEq K] + (calls : List R) (keys : R → List K) + (hc : ∀ c ∈ calls, (keys c).Nodup) + (hr : ExposureTree.repeats ∅ (calls.flatMap keys) = true) : + ∃ i j : Fin calls.length, i < j ∧ ∃ T, + T ∈ keys (calls.get i) ∧ T ∈ keys (calls.get j) := by + by_contra hn + have hnd : (calls.flatMap keys).Nodup := by + apply List.nodup_flatMap.mpr + refine ⟨hc, List.pairwise_iff_get.mpr ?_⟩ + intro i j hij + apply List.disjoint_left.mpr + intro T hTi hTj + exact hn ⟨i,j,hij,T,hTi,hTj⟩ + have hf := (ExposureTree.repeats_empty_false_iff _).mpr hnd + rw [hr] at hf + contradiction + +theorem tracedGridQuery_repeat_calls (H : τ → Finset ι) + (hlin : ∀ T S, T ≠ S → (H T ∩ H S).card ≤ 1) + (N d k : ℕ) (c : QueryCall ι τ) (hc : c.Separated H) + (ν : τ → PMF (Fin N)) {z : (Bool × List (QueryCall ι τ)) × List τ} + (hz : z ∈ (ExposureTree.freshLog ν (tracedGridQuery H N d k c)).support) + (hr : ExposureTree.repeats ∅ z.2 = true) : + ∃ i j : Fin z.1.2.length, i < j ∧ ∃ T, + T ∈ (z.1.2.get i).keys H ∧ T ∈ (z.1.2.get j).keys H := by + have hp := tracedGridQuery_separated H hlin N d k c hc ν hz + rw [tracedGridQuery_log H N d k c ν hz] at hr + exact repeated_flatMap_calls _ _ (fun a ha => a.keys_nodup H (hp a ha)) hr + +end TrueRepeatedCall +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Spectral/WheelSpoke.lean b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/WheelSpoke.lean new file mode 100644 index 0000000000000000000000000000000000000000..541edb904b68729c25195577bbc0e79a1e8ed030 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Spectral/WheelSpoke.lean @@ -0,0 +1,246 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.RerootTemplate + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +lemma cycleNext_val {j : ℕ} (i : Fin j) : + (cycleNext i).val = if i.val+1 < j then i.val+1 else 0 := by + unfold cycleNext + dsimp + split_ifs with h + · exact Nat.mod_eq_of_lt h + · have he : i.val+1 = j := by omega + simp [he] + +lemma cycleNext_injective (j : ℕ) : Function.Injective (@cycleNext j) := by + intro i k h + have hh := congrArg Fin.val h + rw [cycleNext_val,cycleNext_val] at hh + apply Fin.ext + split_ifs at hh <;> omega + +lemma cycleNext_ne {j : ℕ} (hj : 2 ≤ j) (i : Fin j) : cycleNext i ≠ i := by + intro h + have hh := congrArg Fin.val h + rw [cycleNext_val] at hh + split_ifs at hh <;> omega + +lemma cycleNext_twice_ne {j : ℕ} (hj : 3 ≤ j) (i : Fin j) : + cycleNext (cycleNext i) ≠ i := by + intro h + have hh := congrArg Fin.val h + have h1 := cycleNext_val i + have h2 := cycleNext_val (cycleNext i) + split_ifs at h1 h2 <;> omega + +def wheelSpoke {j : ℕ} (i : Fin j) : Finset (Fin (j+1)) := {0,i.succ} +def wheelRim {j : ℕ} (i : Fin j) : Finset (Fin (j+1)) := {i.succ,(cycleNext i).succ} + +lemma wheelSpoke_injective (j : ℕ) : Function.Injective (@wheelSpoke j) := by + intro i k h + have hh : i.succ ∈ wheelSpoke k := h ▸ Finset.mem_insert_of_mem (Finset.mem_singleton_self _) + simp only [wheelSpoke,Finset.mem_insert,Finset.mem_singleton] at hh + rcases hh with hh | hh + · exact (Fin.succ_ne_zero i hh).elim + · exact Fin.succ_injective _ hh + +lemma wheelRim_injective {j : ℕ} (hj : 3 ≤ j) : Function.Injective (@wheelRim j) := by + intro i k h + have ha : i.succ ∈ wheelRim k := h ▸ Finset.mem_insert_self _ _ + have hb : (cycleNext i).succ ∈ wheelRim k := h ▸ Finset.mem_insert_of_mem (Finset.mem_singleton_self _) + simp only [wheelRim,Finset.mem_insert,Finset.mem_singleton,Fin.succ_inj] at ha hb + rcases ha with rfl | ha + · rfl + rcases hb with hb | hb + · have hh : cycleNext (cycleNext k) = k := by rw [← ha,hb] + exact (cycleNext_twice_ne hj k hh).elim + · exact ((cycleNext_ne (by omega) i) (hb.trans ha.symm)).elim + +lemma wheelSpoke_ne_rim {j : ℕ} (i k : Fin j) : wheelSpoke i ≠ wheelRim k := by + intro h + have hh : (0 : Fin (j+1)) ∈ wheelRim k := h ▸ Finset.mem_insert_self _ _ + simp only [wheelRim,Finset.mem_insert,Finset.mem_singleton,(Ne.symm (Fin.succ_ne_zero _)),or_self] at hh + +def wheelTemplate (j : ℕ) (hj : 3 ≤ j) : RootedTemplate (j+1) where + edges := Finset.univ.image wheelSpoke ∪ Finset.univ.image wheelRim + roots := {0} + simple := by + intro e he + rcases Finset.mem_union.mp he with he | he + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + simp [wheelSpoke,(Ne.symm (Fin.succ_ne_zero _))] + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + have hne : i.succ ≠ (cycleNext i).succ := by + intro h; exact cycleNext_ne (by omega) i (Fin.succ_injective _ h).symm + simp [wheelRim,hne] + independent := by + intro e he hsub + rcases Finset.mem_union.mp he with he | he + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + have hh := hsub (show i.succ ∈ wheelSpoke i by simp [wheelSpoke]) + simp at hh + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + have hh := hsub (show i.succ ∈ wheelRim i by simp [wheelRim]) + simp at hh + +@[simp] lemma wheelTemplate_roots_card (j : ℕ) (hj : 3 ≤ j) : + (wheelTemplate j hj).roots.card = 1 := Finset.card_singleton _ + +@[simp] lemma wheelTemplate_edges_card (j : ℕ) (hj : 3 ≤ j) : + (wheelTemplate j hj).edges.card = 2*j := by + have hd : Disjoint (Finset.univ.image (@wheelSpoke j)) (Finset.univ.image (@wheelRim j)) := by + apply Finset.disjoint_left.mpr + intro e he hf + obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + obtain ⟨k,_,hk⟩ := Finset.mem_image.mp hf + exact wheelSpoke_ne_rim i k hk.symm + rw [wheelTemplate,Finset.card_union_of_disjoint hd, + Finset.card_image_of_injective _ (wheelSpoke_injective j), + Finset.card_image_of_injective _ (wheelRim_injective hj)] + simp + omega + +lemma wheelTemplate_balanced (j : ℕ) (hj : 3 ≤ j) : RootedTwoBalanced (wheelTemplate j hj) := by + intro J hJ + have h0 : (0 : Fin (j+1)) ∈ J := hJ (Finset.mem_singleton_self _) + let A := Finset.univ.filter (fun i : Fin j => i.succ ∈ J) + have hsub : ((wheelTemplate j hj).edges.filter (· ⊆ J)) ⊆ + A.image wheelSpoke ∪ A.image wheelRim := by + intro e he + obtain ⟨he,hsub⟩ := Finset.mem_filter.mp he + rcases Finset.mem_union.mp he with he | he + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + exact Finset.mem_union_left _ (Finset.mem_image.mpr ⟨i,Finset.mem_filter.mpr + ⟨Finset.mem_univ _,hsub (Finset.mem_insert_of_mem (Finset.mem_singleton_self _))⟩,rfl⟩) + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp he + exact Finset.mem_union_right _ (Finset.mem_image.mpr ⟨i,Finset.mem_filter.mpr + ⟨Finset.mem_univ _,hsub (Finset.mem_insert_self _ _)⟩,rfl⟩) + have hA : A.card ≤ (J.erase 0).card := by + apply Finset.card_le_card_of_injOn Fin.succ + · intro i hi + exact Finset.mem_erase.mpr ⟨Fin.succ_ne_zero i,(Finset.mem_filter.mp hi).2⟩ + · intro a _ b _ hab + exact Fin.succ_injective _ hab + have hc := (Finset.card_le_card hsub).trans (Finset.card_union_le _ _) + have h1 := Finset.card_image_le (s := A) (f := wheelSpoke) + have h2 := Finset.card_image_le (s := A) (f := wheelRim) + rw [Finset.card_erase_of_mem h0] at hA + rw [wheelTemplate_roots_card] + omega + +def wheelRoot {j n : ℕ} (hj : 3 ≤ j) (u : Fin n) : + {v // v ∈ (wheelTemplate j hj).roots} ↪ Fin n where + toFun := fun _ => u + inj' := by + intro a b _ + apply Subtype.ext + have ha : a.val = 0 := Finset.mem_singleton.mp a.property + have hb : b.val = 0 := Finset.mem_singleton.mp b.property + exact ha.trans hb.symm + +lemma wheelRooted_zero {j n : ℕ} (hj : 3 ≤ j) (u : Fin n) + (φ : RootedInjection (wheelTemplate j hj) (wheelRoot hj u)) : φ.val 0 = u := + φ.property ⟨0,Finset.mem_singleton_self _⟩ + +def wheelRestrict {j n : ℕ} (hj : 3 ≤ j) (u : Fin n) + (φ : RootedInjection (wheelTemplate j hj) (wheelRoot hj u)) : Fin j ↪ Fin n := + (Fin.succEmb j).trans φ.val + +lemma wheel_image_subset {j n : ℕ} (hj : 3 ≤ j) (u : Fin n) + (φ : RootedInjection (wheelTemplate j hj) (wheelRoot hj u)) (G : Graph n) : + imageEdges (wheelTemplate j hj) φ.val ⊆ G ↔ + ∀ i, {u,wheelRestrict hj u φ i} ∈ G ∧ + {wheelRestrict hj u φ i,wheelRestrict hj u φ (cycleNext i)} ∈ G := by + have hs (i : Fin j) : (wheelSpoke i).map φ.val = {u,wheelRestrict hj u φ i} := by + simp [wheelSpoke,wheelRestrict,wheelRooted_zero hj u φ] + have hr (i : Fin j) : (wheelRim i).map φ.val = + {wheelRestrict hj u φ i,wheelRestrict hj u φ (cycleNext i)} := by simp [wheelRim,wheelRestrict] + constructor + · intro h i + constructor + · rw [← hs] + exact h (Finset.mem_image.mpr ⟨wheelSpoke i,Finset.mem_union_left _ (Finset.mem_image.mpr + ⟨i,Finset.mem_univ _,rfl⟩),rfl⟩) + · rw [← hr] + exact h (Finset.mem_image.mpr ⟨wheelRim i,Finset.mem_union_right _ (Finset.mem_image.mpr + ⟨i,Finset.mem_univ _,rfl⟩),rfl⟩) + · intro h e he + obtain ⟨f,hf,rfl⟩ := Finset.mem_image.mp he + rcases Finset.mem_union.mp hf with hf | hf + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp hf + rw [hs]; exact (h i).1 + · obtain ⟨i,_,rfl⟩ := Finset.mem_image.mp hf + rw [hr]; exact (h i).2 + +lemma wheelRestrict_injective {j n : ℕ} (hj : 3 ≤ j) (u : Fin n) : + Function.Injective (wheelRestrict hj u) := by + intro φ χ h + apply Subtype.ext + apply Function.Embedding.ext + intro i + refine Fin.cases ?_ (fun i => ?_) i + · exact (wheelRooted_zero hj u φ).trans (wheelRooted_zero hj u χ).symm + · exact congrArg (fun f : Fin j ↪ Fin n => f i) h + +lemma wheelRestrict_surj {j n : ℕ} (hj : 3 ≤ j) (u : Fin n) (φ : Fin j ↪ Fin n) + (havoid : ∀ i, φ i ≠ u) : + ∃ χ : RootedInjection (wheelTemplate j hj) (wheelRoot hj u), wheelRestrict hj u χ = φ := by + let f : Fin (j+1) → Fin n := Fin.cases u φ + have hf : Function.Injective f := by + intro a b h + revert h + refine Fin.cases ?_ (fun a => ?_) a + · refine Fin.cases ?_ (fun b => ?_) b + · intro _; rfl + · intro h; exact (havoid b h.symm).elim + · refine Fin.cases ?_ (fun b => ?_) b + · intro h; exact (havoid a h).elim + · intro h; exact congrArg Fin.succ (φ.injective h) + let χ : RootedInjection (wheelTemplate j hj) (wheelRoot hj u) := + ⟨⟨f,hf⟩,by intro v; have hv : v.val = 0 := Finset.mem_singleton.mp v.property; change f v.val = u; rw [hv]; rfl⟩ + exact ⟨χ,by ext i; rfl⟩ + +theorem wheel_rootedCount {j n : ℕ} (hj : 3 ≤ j) (G : Graph n) (hG : G ⊆ completeGraph n) (u : Fin n) : + rootedCount (wheelTemplate j hj) (wheelRoot hj u) G = linkCycleCount j G u := by + let A := Finset.univ.filter (fun φ : RootedInjection (wheelTemplate j hj) (wheelRoot hj u) => + imageEdges (wheelTemplate j hj) φ.val ⊆ G) + let B := Finset.univ.filter (fun φ : Fin j ↪ Fin n => + ∀ i, {u,φ i} ∈ G ∧ {φ i,φ (cycleNext i)} ∈ G) + have hA : rootedCount (wheelTemplate j hj) (wheelRoot hj u) G = (A.card : ℝ) := by + simp [rootedCount,copyCount,intact,A] + have hB : linkCycleCount j G u = (B.card : ℝ) := by + simp [linkCycleCount,B,Finset.sum_boole] + rw [hA,hB] + congr 1 + apply Finset.card_bij (fun φ _ => wheelRestrict hj u φ) + · intro φ hφ + exact Finset.mem_filter.mpr ⟨Finset.mem_univ _,(wheel_image_subset hj u φ G).mp (Finset.mem_filter.mp hφ).2⟩ + · intro φ _ χ _ h + exact wheelRestrict_injective hj u h + · intro φ hφ + have hp := (Finset.mem_filter.mp hφ).2 + have hav : ∀ i, φ i ≠ u := by + intro i he + have hh := mem_completeGraph.mp (hG (hp i).1) + simp [he] at hh + obtain ⟨χ,hχ⟩ := wheelRestrict_surj hj u φ hav + refine ⟨χ,Finset.mem_filter.mpr ⟨Finset.mem_univ _,?_⟩,hχ⟩ + exact (wheel_image_subset hj u χ G).mpr (hχ.symm ▸ hp) + +@[simp] theorem wheel_rootedScaling {j n : ℕ} (hj : 3 ≤ j) (p : ℝ) : + rootedScaling (wheelTemplate j hj) n p = ((n : ℝ)*p^2)^j := by + simp only [rootedScaling,wheelTemplate_roots_card,wheelTemplate_edges_card,Nat.add_sub_cancel, + mul_pow,← pow_mul] + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/ActiveCavityFactorProduct.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/ActiveCavityFactorProduct.lean new file mode 100644 index 0000000000000000000000000000000000000000..37fea7e9501f62a11c1640f0fa5b39d0c427ba6a --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/ActiveCavityFactorProduct.lean @@ -0,0 +1,129 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.TriangleHypergraphReindex +import OAI.Combinatorics.TriangleRemoval.Stability.CavityWeight +import OAI.Combinatorics.TriangleRemoval.Process.PathRequired + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave + +lemma active_cavity_factor_product {n : ℕ} (G : Graph n) (e : G) (T : triangles G) (t : ℝ) : + (∏ f ∈ (triangleHypergraph G T.val).erase e.val, + cavityLimit (triangleHypergraph G) f (some T.val) t) = + ∏ f ∈ (activeHypergraph G T).erase e, cavityLimit (activeHypergraph G) f (some T) t := by + change (∏ f ∈ (triangleHypergraph G (triangleEmbedding G T)).erase (edgeEmbedding G e), _) = _ + rw [triangleHypergraph_reindex,← Finset.map_erase,Finset.prod_map] + apply Finset.prod_congr rfl + intro f _ + exact active_cavity_eq G f (some T) t + +lemma active_messageIntegral {n : ℕ} (G : Graph n) (e : G) (T : triangles G) (t : ℝ) : + messageIntegral (triangleHypergraph G) (cavityLimit (triangleHypergraph G)) e.val T.val t = + messageIntegral (activeHypergraph G) (cavityLimit (activeHypergraph G)) e T t := by + unfold messageIntegral + apply intervalIntegral.integral_congr + intro s _ + exact active_cavity_factor_product G e T s + +lemma goodPrefix_full_continuous_rows {c : ℝ} (hc : 0 < c) : + ∀ᶠ n : ℕ in atTop, 0 < prefixD n ∧ + ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∀ t ∈ Set.Icc (0 : ℝ) 1, ∀ (e T : Finset (Fin n)), e ∈ triangleHypergraph G T → + messageIntegral (triangleHypergraph G) (cavityLimit (triangleHypergraph G)) e T t ≤ 1/16 ∧ + (∏ f ∈ (triangleHypergraph G T).erase e, + cavityLimit (triangleHypergraph G) f (some T) t) ≤ + (17/16 : ℝ)*cavityWeight (prefixD n) t := by + filter_upwards [goodPrefix_continuous_rows hc] with n hn + refine ⟨hn.1,?_⟩ + intro C G hG t ht e T he + have heG := triangleHypergraph_subset G T he + have hT : T ∈ triangles G := by + by_contra hT + simp only [triangleHypergraph,hT,ite_false,Finset.notMem_empty] at he + let ee : G := ⟨e,heG⟩ + let TT : triangles G := ⟨T,hT⟩ + have heact : ee ∈ activeHypergraph G TT := by + have hh : edgeEmbedding G ee ∈ (activeHypergraph G TT).map (edgeEmbedding G) := by + rw [← triangleHypergraph_reindex] + exact he + exact Finset.mem_map.mp hh |>.elim (fun _ ha => (edgeEmbedding G).injective ha.2 ▸ ha.1) + obtain ⟨h1,h2⟩ := hn.2 C G hG t ht + rw [show e = ee.val from rfl,show T = TT.val from rfl, + active_messageIntegral,active_cavity_factor_product] + exact ⟨h1 ee TT heact,h2 ee TT heact⟩ + +theorem goodPrefix_full_grid_rows {c : ℝ} (hc : 0 < c) : + ∀ᶠ n : ℕ in atTop, ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∃ N₀ : ℕ, ∀ (N : ℕ) [NeZero N], N₀ ≤ N → + GridRowQuality (triangleHypergraph G) N + (fun k => cavityWeight (prefixD n) ((k : ℝ)/N)) := by + filter_upwards [goodPrefix_full_continuous_rows hc] with n hn + intro C G h + let H := triangleHypergraph G + let A : ℝ := (Fintype.card (Finset (Fin n)) : ℝ)*gridUniformError H + + (Fintype.card (Finset (Fin n)) : ℝ)*Fintype.card (Finset (Fin n)) + let B : ℝ := 2*gridUniformError H + have hA : Tendsto (fun N : ℕ => A/(N : ℝ)) atTop (𝓝 0) := + tendsto_const_nhds.div_atTop (tendsto_natCast_atTop_atTop (R := ℝ)) + have hB : Tendsto (fun N : ℕ => B/(N : ℝ)) atTop (𝓝 0) := + tendsto_const_nhds.div_atTop (tendsto_natCast_atTop_atTop (R := ℝ)) + have hsmall : ∀ᶠ N : ℕ in atTop, A/(N : ℝ) ≤ 1/16 ∧ + B/(N : ℝ) ≤ cavityWeight (prefixD n) 1/16 := by + filter_upwards [hA.eventually_le_const (by norm_num : (0 : ℝ) < 1/16), + hB.eventually_le_const (show 0 < cavityWeight (prefixD n) 1/16 from + div_pos (cavityWeight_pos hn.1.le zero_le_one) (by norm_num))] + with N ha hb + exact ⟨ha,hb⟩ + obtain ⟨N₀,hN₀⟩ := eventually_atTop.mp hsmall + refine ⟨N₀,?_⟩ + intro N _ hN + have hNpos : (0 : ℝ) < N := by exact_mod_cast Nat.pos_of_ne_zero (NeZero.ne N) + have ht (k : ℕ) (hk : k ≤ N) : (k : ℝ)/N ∈ Set.Icc (0 : ℝ) 1 := + ⟨by positivity,(div_le_one hNpos).mpr (by exact_mod_cast hk)⟩ + obtain ⟨ha,hb⟩ := hN₀ N hN + constructor + · intro k hk e T he + obtain ⟨hi,_⟩ := hn.2 C G h _ (ht k hk) e T he + have herr := gridCandidateFailure_error H N k hk e T + change |gridCandidateFailure H N k e T - _| ≤ A/(N : ℝ) at herr + have hh := (abs_le.mp herr).1 + dsimp only [H] at hh + linarith + · intro k hk e T he + obtain ⟨_,hp⟩ := hn.2 C G h _ (ht k hk) e T he + have hT : T ∈ triangles G := by + by_contra hh + simp only [triangleHypergraph,hh,ite_false,Finset.notMem_empty] at he + have hcard : ((H T).erase e).card = 2 := by + rw [Finset.card_erase_of_mem he] + simp only [triangleHypergraph,hT,ite_true,Finset.card_powersetCard,(mem_triangles.mp hT).1] + decide + have herr := gridAnswer_uniform_error H N k hk ((H T).erase e) (some T) + rw [hcard] at herr + have herr' := (abs_le.mp herr).2 + have hbl := cavityWeight_lower hn.1.le (ht k hk) + dsimp only [B,H] at hb herr' + linarith + +end SharpTerminalLeave + +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityGenerator.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityGenerator.lean new file mode 100644 index 0000000000000000000000000000000000000000..80871e6aa4f527e7dd99db986a6b9b09ba123278 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityGenerator.lean @@ -0,0 +1,361 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Coupling.UnitTime +import OAI.Combinatorics.TriangleRemoval.Process.TripleFactorBounds +import OAI.Combinatorics.TriangleRemoval.Process.VariationConstants + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter +open scoped BigOperators Topology + +section +open scoped BigOperators Topology BoundedContinuousFunction +open scoped BigOperators +open Filter MeasureTheory +open scoped Topology + +namespace SharpTerminalLeave +section CavityStability +variable {ι τ : Type*} [Fintype ι] [Fintype τ] [DecidableEq ι] [DecidableEq τ] + +noncomputable def cavityGenerator (H : τ → Finset ι) (D : ℝ) : + (ι → ℝ) →L[ℝ] (ι → ℝ) := + ContinuousLinearMap.pi fun e => (1 / D) • + ∑ T ∈ messageCandidates H e none, ∑ f ∈ (H T).erase e, ContinuousLinearMap.proj f + +omit [Fintype ι] in +@[simp] theorem cavityGenerator_apply (H : τ → Finset ι) (D : ℝ) (z : ι → ℝ) (e : ι) : + cavityGenerator H D z e = (1 / D) * + ∑ T ∈ messageCandidates H e none, ∑ f ∈ (H T).erase e, z f := by + simp [cavityGenerator] + +noncomputable def cavityVelocity (H : τ → Finset ι) (D : ℝ) (s : ℝ) (e : ι) : ℝ := + 1 - Real.exp (2 * s) / D * ∑ T ∈ messageCandidates H e none, + (∏ f ∈ (H T).erase e, cavityLimit H f (some T) (cavityTime D s)) / + (1 - cavityMessage H D e T s) + +noncomputable def cavityResidual (H : τ → Finset ι) (D : ℝ) (s : ℝ) : ι → ℝ := + cavityVelocity H D s + cavityGenerator H D (fun e => cavityLog H D e s) + +theorem cavityMessage_derivative_bound (H : τ → Finset ι) {D δ s : ℝ} + (hD : 0 < D) (hδ : δ ≤ 1 / 2) (hcard : ∀ T, (H T).card = 3) + (hz : ∀ f, |cavityLog H D f s| ≤ δ) + (hI : ∀ f T, f ∈ H T → cavityMessage H D f T s ≤ 1 / 2) + (ht : cavityTime D s ∈ Set.Ioo (0 : ℝ) 1) (e : ι) (T : τ) (he : e ∈ H T) : + deriv (cavityMessage H D e T) s ≤ 12 / D := by + rw [(cavityMessage_hasDerivAt H D e T ht).deriv] + obtain ⟨f, g, hfg, hset, hf, hg⟩ := triangle_children (hcard T) he + have heq := cavity_triangle_message_summand H D e T hfg hset hf hg ht.2 + have hprod : 1 / 4 ≤ (1 - cavityMessage H D f T s) * (1 - cavityMessage H D g T s) := by + have h1 : 1 / 2 ≤ 1 - cavityMessage H D f T s := by linarith [hI f T hf] + have h2 : 1 / 2 ≤ 1 - cavityMessage H D g T s := by linarith [hI g T hg] + nlinarith [mul_le_mul h1 h2 (by norm_num : (0 : ℝ) ≤ 1 / 2) (by linarith)] + have he2 : Real.exp (cavityLog H D f s + cavityLog H D g s) ≤ 3 := by + apply (Real.exp_le_exp.mpr (show cavityLog H D f s + cavityLog H D g s ≤ 1 from + by linarith [(abs_le.mp (hz f)).2, (abs_le.mp (hz g)).2])).trans + exact Real.exp_one_lt_d9.le.trans (by norm_num) + have hb : Real.exp (2 * s) * + (∏ u ∈ (H T).erase e, cavityLimit H u (some T) (cavityTime D s)) ≤ 12 := by + rw [heq] + apply (div_le_iff₀ (by linarith : 0 < + (1 - cavityMessage H D f T s) * (1 - cavityMessage H D g T s))).mpr + linarith + calc + _ = (Real.exp (2 * s) * + ∏ u ∈ (H T).erase e, cavityLimit H u (some T) (cavityTime D s)) / D := by ring + _ ≤ _ := div_le_div_of_nonneg_right hb hD.le + +theorem cavityMessage_bound (H : τ → Finset ι) {D δ s : ℝ} + (hD : 0 < D) (hs : 0 ≤ s) (hδ : δ ≤ 1 / 2) (hcard : ∀ T, (H T).card = 3) + (hz : ∀ u ∈ Set.Icc 0 s, ∀ f, |cavityLog H D f u| ≤ δ) + (hI : ∀ u ∈ Set.Icc 0 s, ∀ f T, f ∈ H T → cavityMessage H D f T u ≤ 1 / 2) + (ht : cavityTime D s < 1) (e : ι) (T : τ) (he : e ∈ H T) : + cavityMessage H D e T s ≤ 12 * s / D := by + have hi : ∀ u ∈ Set.Ioo 0 s, cavityTime D u ∈ Set.Ioo (0 : ℝ) 1 := by + intro u hu + exact ⟨cavityTime_pos hD hu.1, (cavityTime_mono hD hu.2.le).trans_lt ht⟩ + have hh := (convex_Icc (0 : ℝ) s).image_sub_le_mul_sub_of_deriv_le + (continuous_cavityMessage H D e T).continuousOn + (fun u hu => ((cavityMessage_hasDerivAt H D e T (hi u (by simpa using hu))).differentiableAt).differentiableWithinAt) + (C := 12 / D) (fun u hu => by + have hu' : u ∈ Set.Ioo 0 s := by simpa only [interior_Icc] using hu + exact cavityMessage_derivative_bound H hD hδ hcard + (hz u ⟨hu'.1.le, hu'.2.le⟩) (hI u ⟨hu'.1.le, hu'.2.le⟩) (hi u hu') e T he) + 0 ⟨le_rfl, hs⟩ s ⟨hs, le_rfl⟩ hs + simpa only [cavityMessage_zero, sub_zero, div_mul_eq_mul_div] using hh + +theorem cavityResidual_bound (H : τ → Finset ι) {D δ ε η s : ℝ} + (hD : 0 < D) (hδ : δ ≤ 1 / 2) (hε : 0 ≤ ε) (hε' : ε ≤ 1 / 2) (hη : 0 ≤ η) + (hcard : ∀ T, (H T).card = 3) + (hdeg : ∀ e, |((messageCandidates H e none).card : ℝ) / D - 1| ≤ η) + (hrow : ∀ e, ((messageCandidates H e none).card : ℝ) / D ≤ 2) + (hz : ∀ e, |cavityLog H D e s| ≤ δ) + (hI : ∀ e T, e ∈ H T → cavityMessage H D e T s ≤ ε) + (ht : cavityTime D s < 1) : + ‖cavityResidual H D s‖ ≤ η + 64 * δ ^ 2 + 96 * ε := by + apply (pi_norm_le_iff_of_nonneg (by positivity)).mpr + intro e + let R : τ → ℝ := fun T => Real.exp (2 * s) * + ((∏ f ∈ (H T).erase e, cavityLimit H f (some T) (cavityTime D s)) / + (1 - cavityMessage H D e T s)) + let L : τ → ℝ := fun T => ∑ f ∈ (H T).erase e, cavityLog H D f s + have hres : cavityResidual H D s e = + 1 - (1 / D) * (∑ T ∈ messageCandidates H e none, R T) + + (1 / D) * (∑ T ∈ messageCandidates H e none, L T) := by + simp only [cavityResidual, Pi.add_apply, cavityVelocity, cavityGenerator_apply, R, L, + ← Finset.mul_sum] + ring + rw [Real.norm_eq_abs, hres] + have hb := cavity_row_error (messageCandidates H e none) R L hD + (show 0 ≤ 32 * δ ^ 2 + 48 * ε by positivity) (hdeg e) (hrow e) (by + intro T hT + have he : e ∈ H T := (Finset.mem_filter.mp hT).2.1 + obtain ⟨f, g, hfg, hset, hf, hg⟩ := triangle_children (hcard T) he + have hR := cavity_triangle_summand H D e T hfg hset hf hg ht + have hL : L T = cavityLog H D f s + cavityLog H D g s := by + simp only [L, hset, Finset.sum_pair hfg] + dsimp only [R] + rw [hR, hL] + convert cavity_summand_taylor hδ (hz f) (hz g) hε' + ⟨cavityMessage_nonneg H D e T s, hI e T he⟩ + ⟨cavityMessage_nonneg H D f T s, hI f T hf⟩ + ⟨cavityMessage_nonneg H D g T s, hI g T hg⟩ using 1 + congr 1 + ring) + nlinarith + +theorem continuousOn_cavityVelocity (H : τ → Finset ι) {D S : ℝ} + (hD : 0 < D) (hS : cavityTime D S < 1) : + ContinuousOn (cavityVelocity H D) (Set.Icc 0 S) := by + apply continuousOn_pi.mpr + intro e + unfold cavityVelocity + apply continuousOn_const.sub + apply ContinuousOn.mul + · exact ((Real.continuous_exp.comp (continuous_const.mul continuous_id)).div_const D).continuousOn + · apply continuousOn_finsetSum + intro T _ + apply ContinuousOn.div + · apply continuousOn_finsetProd + intro f _ + exact ((continuous_cavityLimit H f (some T)).comp (continuous_cavityTime D)).continuousOn + · exact continuousOn_const.sub (continuous_cavityMessage H D e T).continuousOn + · intro s hs + exact (cavityLimit_factor_pos H e T ((cavityTime_mono hD hs.2).trans_lt hS)).ne' + +theorem continuousOn_cavityLog_vector (H : τ → Finset ι) {D S : ℝ} + (hD : 0 < D) (hS : cavityTime D S < 1) : + ContinuousOn (fun s e => cavityLog H D e s) (Set.Icc 0 S) := + continuousOn_pi.mpr (continuousOn_cavityLog H hD hS) + +theorem continuousOn_cavityResidual (H : τ → Finset ι) {D S : ℝ} + (hD : 0 < D) (hS : cavityTime D S < 1) : + ContinuousOn (cavityResidual H D) (Set.Icc 0 S) := + (continuousOn_cavityVelocity H hD hS).add + ((cavityGenerator H D).continuous.comp_continuousOn (continuousOn_cavityLog_vector H hD hS)) + +theorem cavityLog_provisional_bound (H : τ → Finset ι) {D δ η K s : ℝ} + (hD : 0 < D) (hs : 0 ≤ s) (hδ : δ ≤ 1 / 2) (hη : 0 ≤ η) (hK : 0 ≤ K) + (hcard : ∀ T, (H T).card = 3) + (hdeg : ∀ e, |((messageCandidates H e none).card : ℝ) / D - 1| ≤ η) + (hrow : ∀ e, ((messageCandidates H e none).card : ℝ) / D ≤ 2) + (hz : ∀ u ∈ Set.Icc 0 s, ∀ e, |cavityLog H D e u| ≤ δ) + (hI : ∀ u ∈ Set.Icc 0 s, ∀ e T, e ∈ H T → cavityMessage H D e T u ≤ 1 / 2) + (ht : cavityTime D s < 1) (hsmall : 12 * s / D ≤ 1 / 2) + (hsem : ∀ u ∈ Set.Icc 0 s, ‖NormedSpace.exp ((-u) • cavityGenerator H D)‖ ≤ K) : + ‖fun e => cavityLog H D e s‖ ≤ K * (η + 64 * δ ^ 2 + 1152 * s / D) * s := by + have hi : ∀ u ∈ Set.Icc 0 s, ∀ e T, e ∈ H T → + cavityMessage H D e T u ≤ 12 * s / D := by + intro u hu e T he + apply (cavityMessage_bound H hD hu.1 hδ hcard + (fun v hv => hz v ⟨hv.1, hv.2.trans hu.2⟩) + (fun v hv => hI v ⟨hv.1, hv.2.trans hu.2⟩) + ((cavityTime_mono hD hu.2).trans_lt ht) e T he).trans + exact div_le_div_of_nonneg_right (by linarith [hu.2]) hD.le + have hh := forcing_norm_bound (cavityGenerator H D) + (fun u e => cavityLog H D e u) (cavityResidual H D) hs hK + (continuousOn_cavityLog_vector H hD ht) (continuousOn_cavityResidual H hD ht) + (funext (cavityLog_zero H D)) (by + intro u hu + apply hasDerivAt_pi.mpr + intro e + convert cavityLog_hasDerivAt H D e + ⟨cavityTime_pos hD hu.1, (cavityTime_mono hD hu.2.le).trans_lt ht⟩ using 1 + dsimp [cavityResidual, cavityVelocity] + ring) hsem (d := η + 64 * δ ^ 2 + 1152 * s / D) (by + intro u hu + have hr := cavityResidual_bound H hD hδ (by positivity : 0 ≤ 12 * s / D) + hsmall hη hcard hdeg hrow (hz u hu) (hi u hu) + ((cavityTime_mono hD hu.2).trans_lt ht) + convert hr using 1 + ring) + exact hh + +noncomputable def cavityIncidentMessages (H : τ → Finset ι) (D s : ℝ) : ι × τ → ℝ := + fun p => if p.1 ∈ H p.2 then cavityMessage H D p.1 p.2 s else 0 + +theorem continuous_cavityIncidentMessages (H : τ → Finset ι) (D : ℝ) : + Continuous (cavityIncidentMessages H D) := by + apply continuous_pi + intro p + by_cases hp : p.1 ∈ H p.2 + · simpa only [cavityIncidentMessages, ite_eq_left hp] using continuous_cavityMessage H D p.1 p.2 + · simpa only [cavityIncidentMessages, ite_eq_right hp] using (continuous_const : Continuous (fun _ : ℝ => (0 : ℝ))) + +@[simp] theorem cavityIncidentMessages_zero (H : τ → Finset ι) (D : ℝ) : + cavityIncidentMessages H D 0 = 0 := by + ext p + simp [cavityIncidentMessages] + +theorem cavityIncidentMessages_norm_iff (H : τ → Finset ι) (D s b : ℝ) (hb : 0 ≤ b) : + ‖cavityIncidentMessages H D s‖ ≤ b ↔ + ∀ e T, e ∈ H T → cavityMessage H D e T s ≤ b := by + rw [pi_norm_le_iff_of_nonneg hb] + constructor + · intro hh e T he + simpa only [cavityIncidentMessages, ite_eq_left he, Real.norm_eq_abs, + abs_of_nonneg (cavityMessage_nonneg H D e T s)] using hh (e, T) + · intro hh p + by_cases hp : p.1 ∈ H p.2 + · simpa only [cavityIncidentMessages, ite_eq_left hp, Real.norm_eq_abs, + abs_of_nonneg (cavityMessage_nonneg H D p.1 p.2 s)] using hh p.1 p.2 hp + · simpa only [cavityIncidentMessages, ite_eq_right hp, norm_zero] using hb + +theorem cavity_strict_continuation (H : τ → Finset ι) {D S δ η K : ℝ} + (hD : 0 < D) (hS : 0 ≤ S) (hδ : 0 < δ) (hδ' : δ ≤ 1 / 2) + (hη : 0 ≤ η) (hK : 0 ≤ K) (hcard : ∀ T, (H T).card = 3) + (hdeg : ∀ e, |((messageCandidates H e none).card : ℝ) / D - 1| ≤ η) + (hrow : ∀ e, ((messageCandidates H e none).card : ℝ) / D ≤ 2) + (ht : cavityTime D S < 1) + (hsmall : 12 * S / D ≤ 1 / 4) + (himprove : K * (η + 64 * δ ^ 2 + 1152 * S / D) * S ≤ δ / 2) + (hsem : ∀ u ∈ Set.Icc 0 S, ‖NormedSpace.exp ((-u) • cavityGenerator H D)‖ ≤ K) : + ∀ s ∈ Set.Icc 0 S, + (∀ e, |cavityLog H D e s| ≤ δ / 2) ∧ + (∀ e T, e ∈ H T → cavityMessage H D e T s ≤ 12 * s / D) := by + let Q : ℝ → ℝ := fun s => max (‖fun e => cavityLog H D e s‖ / δ) + (2 * ‖cavityIncidentMessages H D s‖) + have hQ : ContinuousOn Q (Set.Icc 0 S) := + ((continuousOn_cavityLog_vector H hD ht).norm.div_const δ).sup + (continuousOn_const.mul (continuous_cavityIncidentMessages H D).continuousOn.norm) + have hzero : Q 0 ≤ 1 := by + simp only [Q, cavityLog_zero, show (fun _ : ι => (0 : ℝ)) = 0 from rfl, + norm_zero, zero_div, cavityIncidentMessages_zero, mul_zero, max_self, zero_le_one] + have hdecode : ∀ s, Q s ≤ 1 → + (∀ e, |cavityLog H D e s| ≤ δ) ∧ + (∀ e T, e ∈ H T → cavityMessage H D e T s ≤ 1 / 2) := by + intro s hh + have hm := max_le_iff.mp hh + constructor + · have hb : ‖fun e => cavityLog H D e s‖ ≤ δ := by + exact (div_le_one hδ).mp hm.1 + simpa only [Real.norm_eq_abs] using (pi_norm_le_iff_of_nonneg hδ.le).mp hb + · apply (cavityIncidentMessages_norm_iff H D s _ (by norm_num)).mp + linarith [hm.2] + have hboot : ∀ s ∈ Set.Icc 0 S, Q s ≤ 1 / 2 := by + apply strict_bootstrap (δ := 1) (by norm_num) hQ hzero + intro s hs hp + have hz := fun u hu => (hdecode u (hp u hu)).1 + have hI := fun u hu => (hdecode u (hp u hu)).2 + have ht' : cavityTime D s < 1 := (cavityTime_mono hD hs.2).trans_lt ht + have hsmall' : 12 * s / D ≤ 1 / 4 := + (div_le_div_of_nonneg_right (by linarith [hs.2]) hD.le).trans hsmall + apply max_le_iff.mpr + constructor + · apply (div_le_iff₀ hδ).mpr + have hh := cavityLog_provisional_bound H hD hs.1 hδ' hη hK hcard hdeg hrow hz hI + ht' (by linarith) (fun u hu => hsem u ⟨hu.1, hu.2.trans hs.2⟩) + have hforce : η + 64 * δ ^ 2 + 1152 * s / D ≤ η + 64 * δ ^ 2 + 1152 * S / D := by + have := div_le_div_of_nonneg_right (by linarith [hs.2] : 1152 * s ≤ 1152 * S) hD.le + linarith + have hcmp : K * (η + 64 * δ ^ 2 + 1152 * s / D) * s ≤ + K * (η + 64 * δ ^ 2 + 1152 * S / D) * S := by + apply mul_le_mul (mul_le_mul_of_nonneg_left hforce hK) hs.2 hs.1 + positivity + linarith [hh.trans hcmp, himprove] + · have hm : ‖cavityIncidentMessages H D s‖ ≤ 12 * s / D := by + apply (cavityIncidentMessages_norm_iff H D s _ + (div_nonneg (mul_nonneg (by norm_num) hs.1) hD.le)).mpr + exact cavityMessage_bound H hD hs.1 hδ' hcard hz hI ht' + linarith + intro s hs + have hpre : ∀ u ∈ Set.Icc 0 s, Q u ≤ 1 := fun u hu => + (hboot u ⟨hu.1, hu.2.trans hs.2⟩).trans (by norm_num) + constructor + · have hh := (max_le_iff.mp (hboot s hs)).1 + have hn : ‖fun e => cavityLog H D e s‖ ≤ δ / 2 := by + have := (div_le_iff₀ hδ).mp hh + linarith + simpa only [Real.norm_eq_abs] using (pi_norm_le_iff_of_nonneg (by positivity)).mp hn + · exact cavityMessage_bound H hD hs.1 hδ' hcard + (fun u hu => (hdecode u (hpre u hu)).1) + (fun u hu => (hdecode u (hpre u hu)).2) + ((cavityTime_mono hD hs.2).trans_lt ht) + +omit [Fintype ι] [Fintype τ] [DecidableEq ι] [DecidableEq τ] in +theorem cavityTime_strictMono {D : ℝ} (hD : 0 < D) : StrictMono (cavityTime D) := by + intro s t hst + apply div_lt_div_of_pos_right _ (by positivity) + exact sub_lt_sub_right (Real.exp_lt_exp.mpr (by linarith)) _ + +theorem cavity_closed_continuation (H : τ → Finset ι) {D S δ η K : ℝ} + (hD : 0 < D) (hS : 0 < S) (hδ : 0 < δ) (hδ' : δ ≤ 1 / 2) + (hη : 0 ≤ η) (hK : 0 ≤ K) (hcard : ∀ T, (H T).card = 3) + (hdeg : ∀ e, |((messageCandidates H e none).card : ℝ) / D - 1| ≤ η) + (hrow : ∀ e, ((messageCandidates H e none).card : ℝ) / D ≤ 2) + (ht : cavityTime D S ≤ 1) + (hsmall : 12 * S / D ≤ 1 / 4) + (himprove : K * (η + 64 * δ ^ 2 + 1152 * S / D) * S ≤ δ / 2) + (hsem : ∀ u ∈ Set.Icc 0 S, ‖NormedSpace.exp ((-u) • cavityGenerator H D)‖ ≤ K) : + ∀ s ∈ Set.Icc 0 S, + (∀ e, Real.exp (-s - δ / 2) ≤ cavityLimit H e none (cavityTime D s) ∧ + cavityLimit H e none (cavityTime D s) ≤ Real.exp (-s + δ / 2)) ∧ + (∀ e T, e ∈ H T → cavityMessage H D e T s ≤ 12 * s / D) := by + have hpre : ∀ s ∈ Set.Ico 0 S, + (∀ e, Real.exp (-s - δ / 2) ≤ cavityLimit H e none (cavityTime D s) ∧ + cavityLimit H e none (cavityTime D s) ≤ Real.exp (-s + δ / 2)) ∧ + (∀ e T, e ∈ H T → cavityMessage H D e T s ≤ 12 * s / D) := by + intro s hs + have ht' : cavityTime D s < 1 := (cavityTime_strictMono hD hs.2).trans_le ht + have hsmall' : 12 * s / D ≤ 1 / 4 := + (div_le_div_of_nonneg_right (by linarith [hs.2]) hD.le).trans hsmall + have hforce : η + 64 * δ ^ 2 + 1152 * s / D ≤ η + 64 * δ ^ 2 + 1152 * S / D := by + linarith [div_le_div_of_nonneg_right (by linarith [hs.2] : 1152 * s ≤ 1152 * S) hD.le] + have hcmp : K * (η + 64 * δ ^ 2 + 1152 * s / D) * s ≤ + K * (η + 64 * δ ^ 2 + 1152 * S / D) * S := by + apply mul_le_mul (mul_le_mul_of_nonneg_left hforce hK) hs.2.le hs.1 + positivity + have hh := cavity_strict_continuation H hD hs.1 hδ hδ' hη hK hcard hdeg hrow ht' + hsmall' (hcmp.trans himprove) (fun u hu => hsem u ⟨hu.1, hu.2.trans hs.2.le⟩) s ⟨hs.1, le_rfl⟩ + refine ⟨?_, hh.2⟩ + intro e + rw [cavityLog_exp H D e ht'] + constructor <;> apply Real.exp_le_exp.mpr <;> linarith [(abs_le.mp (hh.1 e)).1, (abs_le.mp (hh.1 e)).2] + have hclose {P : ℝ → Prop} (hp : IsClosed {s | P s}) + (hh : ∀ s ∈ Set.Ico 0 S, P s) : ∀ s ∈ Set.Icc 0 S, P s := by + have hx : closure (Set.Ico 0 S) ⊆ {s | P s} := closure_minimal hh hp + simpa only [closure_Ico hS.ne, Set.subset_def, Set.mem_ofPred_eq] using hx + intro s hs + constructor + · intro e + have hc := (continuous_cavityLimit H e none).comp (continuous_cavityTime D) + constructor + · exact hclose (isClosed_le (Real.continuous_exp.comp + (continuous_id.neg.sub continuous_const)) hc) (fun u hu => (hpre u hu).1 e |>.1) s hs + · exact hclose (isClosed_le hc (Real.continuous_exp.comp + (continuous_id.neg.add continuous_const))) (fun u hu => (hpre u hu).1 e |>.2) s hs + · intro e T he + exact hclose (isClosed_le (continuous_cavityMessage H D e T) + ((continuous_const.mul continuous_id).div_const D)) (fun u hu => (hpre u hu).2 e T he) s hs + +end CavityStability +end SharpTerminalLeave + +end + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityReference.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityReference.lean new file mode 100644 index 0000000000000000000000000000000000000000..861cf1049b5254450f2f38cc46f5625ca5d34199 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityReference.lean @@ -0,0 +1,183 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.IncidentTriangleThird +import OAI.Combinatorics.TriangleRemoval.Process.ActivePairMembership +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixHorizon + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma goodPrefix_active_degree {n : ℕ} {c C : ℝ} {G : Graph n} + (h : GoodPrefixGraph n c C G) (hD : 0 < prefixD n) (e : G) : + |((messageCandidates (activeHypergraph G) e none).card : ℝ)/prefixD n-1| ≤ + (n : ℝ)^(-c) := by + classical + rw [active_messageCandidates_card] + obtain ⟨u,v,huv,hev⟩ := Finset.card_eq_two.mp (mem_completeGraph.mp (h.1 e.property)) + have heG : ({u,v} : Finset (Fin n)) ∈ G := hev ▸ e.property + rw [hev,triangleDegree_eq_codegree h.1 heG] + have hcode := h.2.2.2.1 u v huv + have heq : (currentCodegree G u v : ℝ)/prefixD n-1 = + ((currentCodegree G u v : ℝ)-prefixD n)/prefixD n := by + rw [sub_div,div_self hD.ne'] + rw [heq,abs_div,abs_of_pos hD] + exact (div_le_iff₀ hD).mpr hcode + +lemma cavityTime_prefixHorizon {n : ℕ} (hD : 0 < prefixD n) : + cavityTime (prefixD n) (prefixHorizon n) = 1 := by + unfold cavityTime prefixHorizon + rw [show 2*(Real.log (1+2*prefixD n)/2)=Real.log (1+2*prefixD n) by ring, + Real.exp_log (by linarith : 0 < 1+2*prefixD n)] + field_simp + ring + +theorem goodPrefix_cavity_continuation {c : ℝ} (hc : 0 < c) : + ∃ a : ℝ, 0 < a ∧ a < min c (1/1000 : ℝ) ∧ ∀ᶠ n : ℕ in atTop, + ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∀ s ∈ Set.Icc (0 : ℝ) (prefixHorizon n), + (∀ e : G, Real.exp (-s-(n : ℝ)^(-a)/2) ≤ + cavityLimit (activeHypergraph G) e none (cavityTime (prefixD n) s) ∧ + cavityLimit (activeHypergraph G) e none (cavityTime (prefixD n) s) ≤ + Real.exp (-s+(n : ℝ)^(-a)/2)) ∧ + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + cavityMessage (activeHypergraph G) (prefixD n) e T s ≤ 12*s/prefixD n) := by + obtain ⟨C₁,hC₁,J,hsem⟩ := goodPrefix_cavity_semigroup hc + obtain ⟨a,ha,hac,hnum⟩ := prefix_bootstrap_numerics hc hC₁ J + refine ⟨a,ha,hac,?_⟩ + filter_upwards [hsem,hnum,prefix_scales_eventually_pos,eventually_ge_atTop (1 : ℕ)] + with n hsem hnum hp hn + intro C G h + have hD : 0 < prefixD n := lt_of_lt_of_le zero_lt_one hp.2 + have hδc : (n : ℝ)^(-c) ≤ (n : ℝ)^(-a) := + Real.rpow_le_rpow_of_exponent_le (by exact_mod_cast hn) + (by linarith [(lt_min_iff.mp hac).1]) + apply cavity_closed_continuation (activeHypergraph G) + (K := C₁*(1+Real.log (n : ℝ))^J) hD hnum.2.2.1 hnum.1 hnum.2.1 + (Real.rpow_nonneg (Nat.cast_nonneg n) _) (by positivity) + (activeHypergraph_card G) (goodPrefix_active_degree h hD) + · intro e + have hh := (abs_le.mp (goodPrefix_active_degree h hD e)).2 + linarith [hnum.2.1] + · exact (cavityTime_prefixHorizon hD).le + · exact hnum.2.2.2.2.1 + · exact hnum.2.2.2.2.2 + · intro s hs + exact hsem C G h s ⟨hs.1,hs.2.trans hnum.2.2.2.1⟩ + +noncomputable def cavityReference (D t : ℝ) : ℝ := (1+2*D*t)^(-(1/2 : ℝ)) +noncomputable def cavityClock (D t : ℝ) : ℝ := Real.log (1+2*D*t)/2 + +lemma cavityClock_mem {D t : ℝ} (hD : 0 < D) (ht : t ∈ Set.Icc (0 : ℝ) 1) : + cavityClock D t ∈ Set.Icc (0 : ℝ) (Real.log (1+2*D)/2) := by + have harg : 1 ≤ 1+2*D*t := by nlinarith [ht.1] + have hlo := Real.log_nonneg harg + have hhi := Real.log_le_log (by linarith : 0 < 1+2*D*t) + (by nlinarith [ht.2] : 1+2*D*t ≤ 1+2*D) + exact ⟨by dsimp [cavityClock]; linarith,by dsimp [cavityClock]; linarith⟩ + +lemma cavityTime_clock {D t : ℝ} (hD : 0 < D) (ht : 0 ≤ t) : + cavityTime D (cavityClock D t) = t := by + unfold cavityTime cavityClock + rw [show 2*(Real.log (1+2*D*t)/2)=Real.log (1+2*D*t) by ring, + Real.exp_log (by nlinarith : 0 < 1+2*D*t)] + field_simp + ring + +lemma cavityReference_exp {D t : ℝ} (hD : 0 < D) (ht : 0 ≤ t) : + cavityReference D t = Real.exp (-cavityClock D t) := by + rw [cavityReference,Real.rpow_def_of_pos (by nlinarith : 0 < 1+2*D*t)] + congr 1 + dsimp [cavityClock] + ring + +lemma cavityReference_pos {D t : ℝ} (hD : 0 < D) (ht : 0 ≤ t) : + 0 < cavityReference D t := by rw [cavityReference_exp hD ht]; positivity + +theorem goodPrefix_cavity_relative {c : ℝ} (hc : 0 < c) : + ∃ a : ℝ, 0 < a ∧ ∀ᶠ n : ℕ in atTop, + let δ := (n : ℝ)^(-a) + let Imax := 12*prefixHorizon n/prefixD n + Imax ≤ 1/4 ∧ ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∀ t ∈ Set.Icc (0 : ℝ) 1, + (∀ e : G, Real.exp (-δ/2) ≤ cavityLimit (activeHypergraph G) e none t / + cavityReference (prefixD n) t ∧ + cavityLimit (activeHypergraph G) e none t / cavityReference (prefixD n) t ≤ + Real.exp (δ/2)) ∧ + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + messageIntegral (activeHypergraph G) (cavityLimit (activeHypergraph G)) e T t ≤ Imax ∧ + Real.exp (-δ/2) ≤ cavityLimit (activeHypergraph G) e (some T) t / + cavityReference (prefixD n) t ∧ + cavityLimit (activeHypergraph G) e (some T) t / cavityReference (prefixD n) t ≤ + Real.exp (δ/2)/(1-Imax)) := by + obtain ⟨a,ha,hac,hcont⟩ := goodPrefix_cavity_continuation hc + obtain ⟨C₁,hC₁,J,_⟩ := goodPrefix_cavity_semigroup hc + obtain ⟨b,hb,_,hnum⟩ := prefix_bootstrap_numerics hc hC₁ J + refine ⟨a,ha,?_⟩ + filter_upwards [hcont,hnum,prefix_scales_eventually_pos] with n hcavity hn hp + dsimp only + have hD : 0 < prefixD n := lt_of_lt_of_le zero_lt_one hp.2 + have hI : 12*prefixHorizon n/prefixD n ≤ 1/4 := hn.2.2.2.2.1 + refine ⟨hI,?_⟩ + intro C G h t ht + let s := cavityClock (prefixD n) t + have hs : s ∈ Set.Icc (0 : ℝ) (prefixHorizon n) := cavityClock_mem hD ht + have hst : cavityTime (prefixD n) s = t := cavityTime_clock hD ht.1 + obtain ⟨hq,hmsg⟩ := hcavity C G h s hs + rw [hst] at hq + have href := cavityReference_exp hD ht.1 + have href0 := cavityReference_pos hD ht.1 + have helo : Real.exp (-s-(n : ℝ)^(-a)/2) = + Real.exp (-(n : ℝ)^(-a)/2)*cavityReference (prefixD n) t := by + rw [href,← Real.exp_add] + congr 1 + dsimp [s] + ring + have hehi : Real.exp (-s+(n : ℝ)^(-a)/2) = + Real.exp ((n : ℝ)^(-a)/2)*cavityReference (prefixD n) t := by + rw [href,← Real.exp_add] + congr 1 + dsimp [s] + ring + have hroot (e : G) := hq e + simp only [helo,hehi] at hroot + constructor + · intro e + exact ⟨(le_div_iff₀ href0).mpr (hroot e).1,(div_le_iff₀ href0).mpr (hroot e).2⟩ + · intro e T he + let I := messageIntegral (activeHypergraph G) (cavityLimit (activeHypergraph G)) e T t + have hI0 : 0 ≤ I := (messageIntegral_mem_unit _ _ (continuous_cavityLimit _) + (cavityLimit_mem_unit _) _ _ _).1 + have hIm : I ≤ 12*prefixHorizon n/prefixD n := by + have hh := hmsg e T he + change messageIntegral _ _ _ _ (cavityTime (prefixD n) s) ≤ _ at hh + rw [hst] at hh + exact hh.trans (div_le_div_of_nonneg_right (by linarith [hs.2]) hD.le) + have hIf : 0 < 1-I := by linarith + have hMax : 0 < 1-12*prefixHorizon n/prefixD n := by linarith + have ho := cavityLimit_omission (activeHypergraph G) e T he t + change cavityLimit (activeHypergraph G) e none t = + cavityLimit (activeHypergraph G) e (some T) t * (1-I) at ho + have hparent : cavityLimit (activeHypergraph G) e (some T) t = + cavityLimit (activeHypergraph G) e none t/(1-I) := (eq_div_iff hIf.ne').mpr ho.symm + refine ⟨hIm,?_,?_⟩ + · apply (le_div_iff₀ href0).mpr + have hpar0 := (cavityLimit_mem_unit (activeHypergraph G) e (some T) t).1 + have hm : cavityLimit (activeHypergraph G) e none t ≤ + cavityLimit (activeHypergraph G) e (some T) t := by rw [ho]; nlinarith + exact (hroot e).1.trans hm + · rw [hparent] + calc + _ = (cavityLimit (activeHypergraph G) e none t/cavityReference (prefixD n) t)/(1-I) := by ring + _ ≤ Real.exp ((n : ℝ)^(-a)/2)/(1-12*prefixHorizon n/prefixD n) := + div_le_div₀ (by positivity) ((div_le_iff₀ href0).mpr (hroot e).2) hMax (by linarith) +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityWeight.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityWeight.lean new file mode 100644 index 0000000000000000000000000000000000000000..b865a6fdba6bc042c74ce3a45a360cf5f079e7cb --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityWeight.lean @@ -0,0 +1,138 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixMessageEnvelopeSmall +import OAI.Combinatorics.TriangleRemoval.Coupling.GridUniformError + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave + +noncomputable def cavityWeight (D t : ℝ) : ℝ := 1/(1+2*D*t) + +lemma cavityReference_sq {D t : ℝ} (hD : 0 ≤ D) (ht : 0 ≤ t) : + cavityReference D t ^ 2 = cavityWeight D t := by + have hx : 0 ≤ 1+2*D*t := by positivity + unfold cavityReference cavityWeight + rw [← Real.rpow_mul_natCast hx] + norm_num [Real.rpow_neg_one] + +lemma cavityWeight_pos {D t : ℝ} (hD : 0 ≤ D) (ht : 0 ≤ t) : + 0 < cavityWeight D t := by unfold cavityWeight; positivity + +lemma cavityWeight_lower {D t : ℝ} (hD : 0 ≤ D) (ht : t ∈ Set.Icc (0 : ℝ) 1) : + cavityWeight D 1 ≤ cavityWeight D t := by + unfold cavityWeight + have ht0 := ht.1 + apply one_div_le_one_div_of_le + · positivity + · nlinarith [ht.2] + +theorem goodPrefix_continuous_rows {c : ℝ} (hc : 0 < c) : + ∀ᶠ n : ℕ in atTop, 0 < prefixD n ∧ + ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∀ t ∈ Set.Icc (0 : ℝ) 1, + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + messageIntegral (activeHypergraph G) (cavityLimit (activeHypergraph G)) e T t ≤ 1/16) ∧ + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + (∏ f ∈ (activeHypergraph G T).erase e, + cavityLimit (activeHypergraph G) f (some T) t) ≤ + (17/16 : ℝ)*cavityWeight (prefixD n) t) := by + obtain ⟨a,ha,hrel⟩ := goodPrefix_cavity_relative hc + filter_upwards [goodPrefix_cavity_stability hc (by norm_num : (0 : ℝ) < 1/64), + hrel,prefix_message_envelope_small (by norm_num : (0 : ℝ) < 1/16), + prefix_scales_eventually_pos] with n hs hr hi hp + have hD : 0 < prefixD n := lt_of_lt_of_le zero_lt_one hp.2 + refine ⟨hD,?_⟩ + intro C G h t ht + refine ⟨fun e T he => ((hr.2 C G h t ht).2 e T he).1.trans hi.2,?_⟩ + intro e T he + have hcard : ((activeHypergraph G T).erase e).card = 2 := by + rw [Finset.card_erase_of_mem he,activeHypergraph_card] + have href := cavityReference_pos hD ht.1 + have hprod : (∏ f ∈ (activeHypergraph G T).erase e, + cavityLimit (activeHypergraph G) f (some T) t) ≤ + ((65/64 : ℝ)*cavityReference (prefixD n) t)^2 := by + calc + _ ≤ ∏ _f ∈ (activeHypergraph G T).erase e, + (65/64 : ℝ)*cavityReference (prefixD n) t := by + apply Finset.prod_le_prod₀ + · intro f _; exact (cavityLimit_mem_unit _ _ _ _).1 + · intro f hf + have hh := (abs_le.mp ((hs C G h t ht).2 f T (Finset.mem_of_mem_erase hf))).2 + apply (div_le_iff₀ href).mp + linarith + _ = _ := by simp [hcard] + calc + _ ≤ ((65/64 : ℝ)*cavityReference (prefixD n) t)^2 := hprod + _ = (65/64 : ℝ)^2*cavityWeight (prefixD n) t := by + rw [mul_pow,cavityReference_sq hD.le ht.1] + _ ≤ _ := mul_le_mul_of_nonneg_right (by norm_num) (cavityWeight_pos hD.le ht.1).le + +theorem goodPrefix_grid_rows {c : ℝ} (hc : 0 < c) : + ∀ᶠ n : ℕ in atTop, ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∃ N₀ : ℕ, ∀ (N : ℕ) [NeZero N], N₀ ≤ N → ∀ k ≤ N, + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + (7/8 : ℝ) ≤ gridCandidateFailure (activeHypergraph G) N k e T) ∧ + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + gridAnswerProbability (activeHypergraph G) N N k + ((activeHypergraph G T).erase e) (some T) ≤ + (9/8 : ℝ)*cavityWeight (prefixD n) ((k : ℝ)/N)) := by + filter_upwards [goodPrefix_continuous_rows hc] with n hn + intro C G h + let H := activeHypergraph G + let A : ℝ := (Fintype.card G : ℝ)*gridUniformError H + + (Fintype.card G : ℝ)*Fintype.card (triangles G) + let B : ℝ := 2*gridUniformError H + have hA : Tendsto (fun N : ℕ => A/(N : ℝ)) atTop (𝓝 0) := + tendsto_const_nhds.div_atTop (tendsto_natCast_atTop_atTop (R := ℝ)) + have hB : Tendsto (fun N : ℕ => B/(N : ℝ)) atTop (𝓝 0) := + tendsto_const_nhds.div_atTop (tendsto_natCast_atTop_atTop (R := ℝ)) + have hsmall : ∀ᶠ N : ℕ in atTop, A/(N : ℝ) ≤ 1/16 ∧ + B/(N : ℝ) ≤ cavityWeight (prefixD n) 1/16 := by + filter_upwards [hA.eventually_le_const (by norm_num : (0 : ℝ) < 1/16), + hB.eventually_le_const (show 0 < cavityWeight (prefixD n) 1/16 from + div_pos (cavityWeight_pos hn.1.le zero_le_one) (by norm_num))] + with N ha hb + exact ⟨ha,hb⟩ + obtain ⟨N₀,hN₀⟩ := eventually_atTop.mp hsmall + refine ⟨N₀,?_⟩ + intro N _ hN k hk + have hNpos : (0 : ℝ) < N := by exact_mod_cast Nat.pos_of_ne_zero (NeZero.ne N) + have ht : (k : ℝ)/N ∈ Set.Icc (0 : ℝ) 1 := + ⟨by positivity,(div_le_one hNpos).mpr (by exact_mod_cast hk)⟩ + obtain ⟨hi,hpair⟩ := hn.2 C G h _ ht + obtain ⟨ha,hb⟩ := hN₀ N hN + constructor + · intro e T he + have herr := gridCandidateFailure_error H N k hk e T + have herr' := (abs_le.mp herr).1 + have hh := hi e T he + change _ ≤ A/(N : ℝ) at herr + dsimp only [A] at ha + dsimp only [H] at herr' + linarith + · intro e T he + have hcard : ((H T).erase e).card = 2 := by + rw [Finset.card_erase_of_mem he,activeHypergraph_card] + have herr := gridAnswer_uniform_error H N k hk ((H T).erase e) (some T) + rw [hcard] at herr + have herr' := (abs_le.mp herr).2 + have hp := hpair e T he + have hbl := cavityWeight_lower hn.1.le ht + dsimp only [B,H] at hb herr' + norm_num only [Nat.cast_ofNat] at herr' + linarith + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityWeightLipschitz.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityWeightLipschitz.lean new file mode 100644 index 0000000000000000000000000000000000000000..a9c545dd1139a22163bb0345bb9742a34c6cdb15 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/CavityWeightLipschitz.lean @@ -0,0 +1,119 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Stability.CavityWeight +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixWeight +import OAI.Combinatorics.TriangleRemoval.Process.WitnessMu + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter +open scoped BigOperators + +namespace SharpTerminalLeave +open Filter MeasureTheory +open scoped BigOperators Topology + +lemma cavityWeight_lipschitz {D : ℝ} (hD : 0 ≤ D) + {x y : ℝ} (hx : 0 ≤ x) (hy : 0 ≤ y) : + |cavityWeight D x - cavityWeight D y| ≤ (2*D)*|x-y| := by + have hx1 : 1 ≤ 1+2*D*x := by nlinarith [mul_nonneg hD hx] + have hy1 : 1 ≤ 1+2*D*y := by nlinarith [mul_nonneg hD hy] + have hx0 : 0 < 1+2*D*x := by linarith + have hy0 : 0 < 1+2*D*y := by linarith + have he : cavityWeight D x - cavityWeight D y = + (2*D*(y-x))/((1+2*D*x)*(1+2*D*y)) := by + unfold cavityWeight + field_simp + ring + rw [he,abs_div,abs_mul,abs_of_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 2) hD), + abs_of_pos (mul_pos hx0 hy0),abs_sub_comm y x] + have hden : 1 ≤ (1+2*D*x)*(1+2*D*y) := by nlinarith + exact (div_le_iff₀ (mul_pos hx0 hy0)).mpr (by + nlinarith [mul_nonneg (mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 2) hD) + (abs_nonneg (x-y))) (sub_nonneg.mpr hden)]) + +lemma continuous_clipped_cavityWeight {D : ℝ} (hD : 0 ≤ D) : + Continuous (fun t => cavityWeight D (unitTime t)) := by + unfold cavityWeight + apply Continuous.div continuous_const + (continuous_const.add (continuous_const.mul continuous_unitTime)) + intro t + change 1 + 2*D*unitTime t ≠ 0 + have ht := mul_nonneg (mul_nonneg (by norm_num : (0 : ℝ) ≤ 2) hD) (unitTime_nonneg t) + linarith + +lemma cavityWeight_integral {D : ℝ} (hD : 0 < D) : + (∫ t in (0 : ℝ)..1, cavityWeight D (unitTime t)) = witnessMu D := by + have hc : 2*D ≠ 0 := by positivity + have he : (∫ t in (0 : ℝ)..1, cavityWeight D (unitTime t)) = + ∫ t in (0 : ℝ)..1, 1/(1+(2*D)*t) := by + apply intervalIntegral.integral_congr + intro t ht + rw [Set.uIcc_of_le (by norm_num : (0 : ℝ) ≤ 1)] at ht + simp only [unitTime_of_mem ht,cavityWeight] + rw [he,intervalIntegral.integral_comp_add_mul (fun x : ℝ => 1/x) hc 1] + simp only [mul_zero,add_zero,mul_one,smul_eq_mul] + rw [integral_one_div_of_pos (by norm_num : (0 : ℝ) < 1) (by linarith : 0 < 1+2*D)] + simp only [div_one,witnessMu] + ring + +theorem gridWitnessWeight_error {D : ℝ} (hD : 0 < D) (N : ℕ) (hN : 0 < N) : + |prefixWeight (fun j => (2/(N : ℝ))*cavityWeight D ((j : ℝ)/N)) N - + 2*witnessMu D| ≤ 4*D/N := by + have hNr : (0 : ℝ) < N := by exact_mod_cast hN + have hh := uniform_grid_quadrature (fun t => cavityWeight D (unitTime t)) + (continuous_clipped_cavityWeight hD.le) (by positivity : (0 : ℝ) ≤ 2*D) + (fun x hx y hy => by + rw [unitTime_of_mem hx,unitTime_of_mem hy] + exact cavityWeight_lipschitz hD.le hx.1 hy.1) N N hN le_rfl + rw [div_self (ne_of_gt hNr),cavityWeight_integral hD] at hh + have hs : (∑ j ∈ Finset.range N, cavityWeight D (unitTime ((j : ℝ)/N))) = + ∑ j ∈ Finset.range N, cavityWeight D ((j : ℝ)/N) := by + apply Finset.sum_congr rfl + intro j hj + rw [unitTime_of_mem ⟨by positivity,(div_le_one hNr).mpr (by exact_mod_cast (Finset.mem_range.mp hj).le)⟩] + rw [hs,abs_sub_comm] at hh + have he : prefixWeight (fun j => (2/(N : ℝ))*cavityWeight D ((j : ℝ)/N)) N - + 2*witnessMu D = + 2*((1/(N : ℝ))*(∑ j ∈ Finset.range N, cavityWeight D ((j : ℝ)/N)) - witnessMu D) := by + unfold prefixWeight + rw [← Finset.mul_sum] + ring + rw [he,abs_mul,abs_of_pos (by norm_num : (0 : ℝ) < 2)] + calc + _ ≤ 2*(2*D/N) := mul_le_mul_of_nonneg_left hh (by norm_num) + _ = _ := by ring + +theorem gridWitnessWeight_tendsto {D : ℝ} (hD : 0 < D) : + Tendsto (fun N : ℕ => prefixWeight + (fun j => (2/((N+1 : ℕ) : ℝ))*cavityWeight D ((j : ℝ)/(N+1 : ℕ))) (N+1)) + atTop (𝓝 (2*witnessMu D)) := by + have hz : Tendsto (fun N : ℕ => 4*D/((N+1 : ℕ) : ℝ)) atTop (𝓝 0) := by + have h := (tendsto_const_nhds (x := 4*D)).mul + (tendsto_one_div_add_atTop_nhds_zero_nat : + Tendsto (fun N : ℕ => (1 : ℝ)/(N+1)) atTop (𝓝 0)) + simpa only [Nat.cast_add,Nat.cast_one,mul_zero,div_eq_mul_inv,one_mul] using h + have hl : Tendsto (fun N : ℕ => 2*witnessMu D - 4*D/((N+1 : ℕ) : ℝ)) + atTop (𝓝 (2*witnessMu D)) := by + simpa only [sub_zero] using (tendsto_const_nhds (x := 2*witnessMu D)).sub hz + have hu : Tendsto (fun N : ℕ => 2*witnessMu D + 4*D/((N+1 : ℕ) : ℝ)) + atTop (𝓝 (2*witnessMu D)) := by + simpa only [add_zero] using (tendsto_const_nhds (x := 2*witnessMu D)).add hz + apply tendsto_of_tendsto_of_tendsto_of_le_of_le hl hu + · intro N + have h := (abs_le.mp (gridWitnessWeight_error hD (N+1) (by omega))).1 + linarith + · intro N + have h := (abs_le.mp (gridWitnessWeight_error hD (N+1) (by omega))).2 + linarith + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/ContinuousCavityWeightClip.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/ContinuousCavityWeightClip.lean new file mode 100644 index 0000000000000000000000000000000000000000..def42354a3ee74b20c4f881a200de55c3e9f4775 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/ContinuousCavityWeightClip.lean @@ -0,0 +1,111 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Stability.CavityWeightLipschitz + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave + +lemma continuous_cavityWeight_clip {D : ℝ} (hD : 0 ≤ D) : + Continuous (fun t => cavityWeight D (unitTime t)) := by + unfold cavityWeight + apply continuous_const.div + (continuous_const.add ((continuous_const.mul continuous_const).mul continuous_unitTime)) + intro t + have ht := unitTime_nonneg t + apply ne_of_gt + change 0 < 1+2*D*unitTime t + have hh : 0 ≤ 2*D*unitTime t := mul_nonneg (mul_nonneg (by norm_num) hD) ht + linarith + +lemma cavityWeight_lipschitz_cell {D x y : ℝ} (hD : 0 ≤ D) + (hx : 0 ≤ x) (hy : 0 ≤ y) : + |cavityWeight D x - cavityWeight D y| ≤ (2*D)*|x-y| := by + have ha : 1 ≤ 1+2*D*x := le_add_of_nonneg_right (mul_nonneg (mul_nonneg (by norm_num) hD) hx) + have hb : 1 ≤ 1+2*D*y := le_add_of_nonneg_right (mul_nonneg (mul_nonneg (by norm_num) hD) hy) + have ha0 : 0 < 1+2*D*x := lt_of_lt_of_le zero_lt_one ha + have hb0 : 0 < 1+2*D*y := lt_of_lt_of_le zero_lt_one hb + have hid : 1/(1+2*D*x)-1/(1+2*D*y) = + (2*D*(y-x))/((1+2*D*x)*(1+2*D*y)) := by + field_simp + ring + unfold cavityWeight + rw [hid,abs_div,abs_mul,abs_of_nonneg (by positivity : 0 ≤ 2*D), + abs_of_pos (mul_pos ha0 hb0),abs_sub_comm y x] + exact div_le_self (by positivity) (one_le_mul_of_one_le_of_one_le ha hb) + +theorem grid_prefixWeight_tendsto {D : ℝ} (hD : 0 < D) : + Tendsto (fun j : ℕ => + prefixWeight (fun t => (2/((j+1 : ℕ) : ℝ))*cavityWeight D ((t : ℝ)/(j+1 : ℕ))) (j+1)) + atTop (𝓝 (2*witnessMu D)) := by + have hquad (j : ℕ) : + |prefixWeight (fun t => (2/((j+1 : ℕ) : ℝ))*cavityWeight D ((t : ℝ)/(j+1 : ℕ))) (j+1) - + 2*witnessMu D| ≤ 4*D/((j+1 : ℕ) : ℝ) := by + have hN : 0 < j+1 := Nat.succ_pos j + have hNr : (0 : ℝ) < (j+1 : ℕ) := by exact_mod_cast hN + have hh := uniform_grid_quadrature (fun t => cavityWeight D (unitTime t)) + (continuous_cavityWeight_clip hD.le) (show 0 ≤ 2*D by positivity) + (fun x hx y hy => by + rw [unitTime_of_mem hx,unitTime_of_mem hy] + exact cavityWeight_lipschitz_cell hD.le hx.1 hy.1) (j+1) (j+1) hN le_rfl + rw [div_self (ne_of_gt hNr),cavityWeight_integral hD] at hh + have heq : (∑ t ∈ Finset.range (j+1), cavityWeight D (unitTime ((t : ℝ)/(j+1 : ℕ)))) = + ∑ t ∈ Finset.range (j+1), cavityWeight D ((t : ℝ)/(j+1 : ℕ)) := by + apply Finset.sum_congr rfl + intro t ht + rw [unitTime_of_mem ⟨by positivity,(div_le_one hNr).mpr + (by exact_mod_cast (Finset.mem_range.mp ht).le)⟩] + rw [heq] at hh + have hb := mul_le_mul_of_nonneg_left hh (by norm_num : (0 : ℝ) ≤ 2) + calc + _ = 2*|witnessMu D - (1/((j+1 : ℕ) : ℝ))* + ∑ t ∈ Finset.range (j+1), cavityWeight D ((t : ℝ)/(j+1 : ℕ))| := by + unfold prefixWeight + rw [← Finset.mul_sum] + rw [show (2 : ℝ) = |(2 : ℝ)| by norm_num, ← abs_mul, ← abs_neg] + congr 1 + ring + _ ≤ _ := by + calc + _ ≤ 2*(2*D/((j+1 : ℕ) : ℝ)) := hb + _ = _ := by ring + have hden : Tendsto (fun j : ℕ => (4*D)/((j+1 : ℕ) : ℝ)) atTop (𝓝 0) := by + have hh := tendsto_one_div_add_atTop_nhds_zero_nat.mul_const (4*D) + simpa only [Nat.cast_add,Nat.cast_one,mul_comm,one_div,div_eq_mul_inv,one_mul,zero_mul] using hh + apply tendsto_iff_norm_sub_tendsto_zero.mpr + exact squeeze_zero (fun _ => norm_nonneg _) (fun j => by rw [Real.norm_eq_abs]; exact hquad j) hden + +lemma collision_exponential_identity {D : ℝ} (hD : 0 < D) : + (1+2*D)^3*Real.exp (45*D*(2*witnessMu D)) = (1+2*D)^48 := by + have he : 45*D*(2*witnessMu D) = 45*Real.log (1+2*D) := by + unfold witnessMu + field_simp + rw [he,show (45 : ℝ) = (45 : ℕ) by norm_num,Real.exp_nat_mul, + Real.exp_log (by linarith : 0 < 1+2*D),← pow_add] + +lemma collision_power_48_bound {D : ℝ} (hD : 1 ≤ D) : + (1+2*D)^48/D^50 ≤ (3 : ℝ)^48/D^2 := by + have hpos : 0 < D := lt_of_lt_of_le zero_lt_one hD + have hh := div_le_div_of_nonneg_right + (pow_le_pow_left₀ (by positivity : 0 ≤ 1+2*D) (by linarith : 1+2*D ≤ 3*D) 48) + (by positivity : 0 ≤ D^50) + apply hh.trans_eq + rw [mul_pow,show 50 = 48+2 by norm_num,pow_add] + field_simp + +end SharpTerminalLeave + +open Filter +open scoped BigOperators Topology + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/EdgeStabilityRate.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/EdgeStabilityRate.lean new file mode 100644 index 0000000000000000000000000000000000000000..849f587dbb8a15d1c7fb3db820d6a35d31589291 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/EdgeStabilityRate.lean @@ -0,0 +1,117 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixEdgeRadius +import OAI.Combinatorics.TriangleRemoval.Process.TriangleIndex + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +noncomputable def edgeStabilityRate (s t Q : ℝ) : ℝ := s^2/(Q*t) +noncomputable def edgeStabilityScalar (s t Q κ : ℝ) : ℝ := s/t-1-2*edgeStabilityRate s t Q+κ*s/(Q*t) + +lemma triangle_relative_inverse_bound {Q S r : ℝ} (hS : 0 < S) (hr : r ≤ 1/2) + (h : |Q/S-1| ≤ r) : 0 < Q ∧ |1-S/Q| ≤ 2*r ∧ S/Q ≤ 2 := by + obtain ⟨hl,hu⟩ := abs_le.mp h + have hlow : S/2 ≤ Q := by + have hh : 1/2 ≤ Q/S := by linarith + have hh' := (le_div_iff₀ hS).mp hh + linarith only [hh'] + have hQ : 0 < Q := lt_of_lt_of_le (by positivity) hlow + have hab : |Q-S| ≤ r*S := by + have he : Q/S-1 = (Q-S)/S := by field_simp + rw [he,abs_div,abs_of_pos hS] at h + exact (div_le_iff₀ hS).mp h + have hratio : S/Q ≤ 2 := (div_le_iff₀ hQ).mpr (by linarith) + have hr0 : 0 ≤ r := (abs_nonneg _).trans h + refine ⟨hQ,?_,hratio⟩ + have he : 1-S/Q = (Q-S)/Q := by field_simp + rw [he,abs_div,abs_of_pos hQ] + calc + _ ≤ r*S/Q := div_le_div_of_nonneg_right hab hQ.le + _ = r*(S/Q) := by ring + _ ≤ _ := by nlinarith only [mul_le_mul_of_nonneg_left hratio hr0] + +lemma edge_stability_coefficients {s t Q S lam r κ : ℝ} + (hs : 0 < s) (ht : 0 < t) (hS : 0 < S) (hl : 0 ≤ lam) + (hscale : t = s*(1-lam)^2) (hratio : s ≤ 2*t) + (hident : s = lam*S) (hr : r ≤ 1/2) (hQerr : |Q/S-1| ≤ r) + (hκ : 0 ≤ κ) (hκ1 : κ ≤ 1) : + (0 ≤ edgeStabilityRate s t Q ∧ edgeStabilityRate s t Q ≤ 4*lam) ∧ + |edgeStabilityScalar s t Q κ| ≤ 8*r*lam+2*lam^2+4*lam/s := by + obtain ⟨hQ,habs,hSQ⟩ := triangle_relative_inverse_bound hS hr hQerr + have hr0 : 0 ≤ r := (abs_nonneg _).trans hQerr + have hsQ : s/Q ≤ 2*lam := by + rw [hident,mul_div_assoc] + simpa only [mul_comm lam 2] using mul_le_mul_of_nonneg_left hSQ hl + have hsQdiff : |lam-s/Q| ≤ 2*r*lam := by + have he : lam-s/Q = lam*(1-S/Q) := by rw [hident]; ring + rw [he,abs_mul,abs_of_nonneg hl] + nlinarith only [mul_le_mul_of_nonneg_left habs hl] + have hsT : s/t ≤ 2 := (div_le_iff₀ ht).mpr hratio + have haeq : edgeStabilityRate s t Q = (s/t)*(s/Q) := by unfold edgeStabilityRate; ring + have hbeq : edgeStabilityScalar s t Q κ = + (s/t)*(2*(lam-s/Q)-lam^2+κ/Q) := by + unfold edgeStabilityScalar edgeStabilityRate + apply (mul_right_cancel₀ ht.ne') + field_simp [hs.ne',ht.ne',hQ.ne'] + rw [hscale] + ring + constructor + · rw [haeq] + exact ⟨by positivity,(mul_le_mul hsT hsQ (by positivity) (by norm_num)).trans_eq (by ring)⟩ + · have hκQ : |κ/Q| ≤ 2*lam/s := by + rw [abs_of_nonneg (div_nonneg hκ hQ.le)] + apply (le_div_iff₀ hs).mpr + have hh := div_le_div_of_nonneg_right hκ1 hQ.le + have hm := mul_le_mul_of_nonneg_right hh hs.le + calc + _ ≤ (1/Q)*s := hm + _ = s/Q := by ring + _ ≤ _ := hsQ + have hb : |2*(lam-s/Q)-lam^2+κ/Q| ≤ 4*r*lam+lam^2+2*lam/s := by + calc + _ ≤ |2*(lam-s/Q)-lam^2|+|κ/Q| := abs_add_le _ _ + _ ≤ |2*(lam-s/Q)|+|lam^2|+|κ/Q| := by linarith only [abs_sub (2*(lam-s/Q)) (lam^2)] + _ ≤ 4*r*lam+lam^2+2*lam/s := by + rw [abs_mul,abs_of_nonneg (by norm_num : (0 : ℝ) ≤ 2),abs_of_nonneg (sq_nonneg lam)] + linarith only [hsQdiff,hκQ] + rw [hbeq,abs_mul,abs_of_pos (div_pos hs ht)] + calc + _ ≤ 2*(4*r*lam+lam^2+2*lam/s) := mul_le_mul hsT hb (abs_nonneg _) (by norm_num) + _ = _ := by ring + +lemma early_codegree_grid {n i : ℕ} (hn : 0 < n) + (hp : 1/(n : ℝ) ≤ prefixDensity n) (hi : i < prefixTime n) : + earlyTemplateScale 1 2 n (i+1) = earlyTemplateScale 1 2 n i* + (1-6/(n : ℝ)^2/earlyDensity n i)^2 ∧ + earlyTemplateScale 1 2 n i = (6/(n : ℝ)^2/earlyDensity n i)*earlyTriangleScale n i ∧ + 0 ≤ 6/(n : ℝ)^2/earlyDensity n i ∧ + 6/(n : ℝ)^2/earlyDensity n i ≤ 6/(n : ℝ) := by + have hnR : (0 : ℝ) < n := by exact_mod_cast hn + have hpi := early_density_positive hn hp hi.le + have hs := earlyDensity_succ n i + have he : earlyDensity n (i+1) = earlyDensity n i*(1-6/(n : ℝ)^2/earlyDensity n i) := by + have he : earlyDensity n (i+1) = earlyDensity n i-6/(n : ℝ)^2 := by linarith only [hs] + rw [he] + field_simp + refine ⟨?_,?_,by positivity,?_⟩ + · simp only [earlyTemplateScale,pow_one,he,mul_pow]; ring + · unfold earlyTriangleScale + simp only [earlyTemplateScale,pow_one] + field_simp + · have hp' := hp.trans (earlyDensity_antitone n hi.le) + apply (div_le_iff₀ hpi).mpr + have hm := mul_le_mul_of_nonneg_left hp' (show 0 ≤ 6/(n : ℝ) by positivity) + convert hm using 1 + field_simp + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/GeometricVolterraIdentity.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/GeometricVolterraIdentity.lean new file mode 100644 index 0000000000000000000000000000000000000000..2d1c9b8313e2f47a333245862199c019dcf50ea8 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/GeometricVolterraIdentity.lean @@ -0,0 +1,123 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave + +theorem geometric_volterra_identity (a c : ℝ) (k : ℕ) : + c + a * (∑ j ∈ Finset.range k, c * (1 + a) ^ j) = c * (1 + a) ^ k := by + induction k with + | zero => simp + | succ k ih => + rw [Finset.sum_range_succ, pow_succ] + nlinarith [ih] + +theorem discrete_volterra_bound (E : ℕ → ℝ) (a c : ℝ) (ha : 0 ≤ a) + (M : ℕ) (hE : ∀ k ≤ M, E k ≤ c + a * ∑ j ∈ Finset.range k, E j) + (k : ℕ) (hk : k ≤ M) : E k ≤ c * (1 + a) ^ k := by + induction k using Nat.strong_induction_on with + | h k ih => + calc + E k ≤ c + a * ∑ j ∈ Finset.range k, E j := hE k hk + _ ≤ c + a * ∑ j ∈ Finset.range k, c * (1 + a) ^ j := by + gcongr with j hj + exact ih j (Finset.mem_range.mp hj) ((Nat.le_of_lt (Finset.mem_range.mp hj)).trans hk) + _ = c * (1 + a) ^ k := geometric_volterra_identity a c k + +theorem discrete_volterra_exp_bound (E : ℕ → ℝ) (A C : ℝ) + (hA : 0 ≤ A) (hC : 0 ≤ C) (N : ℕ) (hN : 0 < N) + (hE : ∀ k ≤ N, E k ≤ C / N + (A / N) * ∑ j ∈ Finset.range k, E j) + (k : ℕ) (hk : k ≤ N) : E k ≤ (C / N) * Real.exp A := by + have hNr : (0 : ℝ) < N := by exact_mod_cast hN + have hd : 0 ≤ A / N := div_nonneg hA hNr.le + have hh := discrete_volterra_bound E (A / N) (C / N) hd N hE k hk + apply hh.trans + apply mul_le_mul_of_nonneg_left _ (div_nonneg hC hNr.le) + calc + (1 + A / N) ^ k ≤ (Real.exp (A / N)) ^ k := by + apply pow_le_pow_left₀ (by positivity) + simpa only [add_comm] using Real.add_one_le_exp (A / N) + _ = Real.exp ((k : ℝ) * (A / N)) := (Real.exp_nat_mul _ _).symm + _ ≤ Real.exp A := by + apply Real.exp_le_exp.mpr + have hkr : (k : ℝ) ≤ N := by exact_mod_cast hk + calc + (k : ℝ) * (A / N) ≤ (N : ℝ) * (A / N) := mul_le_mul_of_nonneg_right hkr hd + _ = A := by field_simp + +theorem integral_cell_error (f : ℝ → ℝ) {a b L : ℝ} (hab : a ≤ b) + (hf : Continuous f) (hL : 0 ≤ L) + (hLip : ∀ x ∈ Set.Icc a b, |f x - f a| ≤ L * (x - a)) : + |(∫ x in a..b, f x) - (b - a) * f a| ≤ L * (b - a) ^ 2 := by + have heq : (∫ x in a..b, f x) - (b - a) * f a = ∫ x in a..b, f x - f a := by + rw [intervalIntegral.integral_sub (hf.intervalIntegrable _ _) (continuous_const.intervalIntegrable _ _)] + simp only [intervalIntegral.integral_const, smul_eq_mul] + rw [heq, ← Real.norm_eq_abs] + have hb := intervalIntegral.norm_integral_le_of_norm_le_const + (a := a) (b := b) (f := fun x => f x - f a) (C := L * (b - a)) (by + intro x hx + rw [Set.uIoc_of_le hab] at hx + rw [Real.norm_eq_abs] + exact (hLip x ⟨hx.1.le, hx.2⟩).trans + (mul_le_mul_of_nonneg_left (sub_le_sub_right hx.2 a) hL)) + rw [abs_of_nonneg (sub_nonneg.mpr hab)] at hb + convert hb using 1 + ring + +theorem uniform_grid_quadrature (f : ℝ → ℝ) (hf : Continuous f) + {L : ℝ} (hL : 0 ≤ L) + (hLip : ∀ x ∈ Set.Icc (0 : ℝ) 1, ∀ y ∈ Set.Icc (0 : ℝ) 1, + |f x - f y| ≤ L * |x - y|) + (N k : ℕ) (hN : 0 < N) (hk : k ≤ N) : + |(∫ x in (0 : ℝ)..((k : ℝ) / N), f x) - + (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, f ((j : ℝ) / N)| ≤ L / N := by + have hNr : (0 : ℝ) < N := by exact_mod_cast hN + have hsum : (∫ x in (0 : ℝ)..((k : ℝ) / N), f x) = + ∑ j ∈ Finset.range k, ∫ x in ((j : ℝ) / N)..(((j + 1 : ℕ) : ℝ) / N), f x := by + simpa only [Nat.cast_zero, zero_div] using + (intervalIntegral.sum_integral_adjacent_intervals + (a := fun j => (j : ℝ) / N) (n := k) (fun _ _ => hf.intervalIntegrable _ _)).symm + rw [hsum, Finset.mul_sum, ← Finset.sum_sub_distrib] + calc + _ ≤ ∑ j ∈ Finset.range k, + |(∫ x in ((j : ℝ) / N)..(((j + 1 : ℕ) : ℝ) / N), f x) - + (1 / N) * f ((j : ℝ) / N)| := Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ _j ∈ Finset.range k, L * (1 / (N : ℝ)) ^ 2 := by + apply Finset.sum_le_sum + intro j hj + have hjk : j < k := Finset.mem_range.mp hj + have hjN : (j : ℝ) + 1 ≤ N := by exact_mod_cast (show j + 1 ≤ N by omega) + have hja : (j : ℝ) / N ≤ ((j + 1 : ℕ) : ℝ) / N := by + gcongr; exact_mod_cast (Nat.le_succ j) + have hdif : (((j + 1 : ℕ) : ℝ) / N) - (j : ℝ) / N = 1 / (N : ℝ) := by + push_cast; ring + have he := integral_cell_error f hja hf hL (by + intro x hx + have ha0 : (0 : ℝ) ≤ (j : ℝ) / N := by positivity + have hb1 : (((j + 1 : ℕ) : ℝ) / N) ≤ 1 := by + rw [div_le_one hNr]; simpa using hjN + have hh := hLip x ⟨ha0.trans hx.1, hx.2.trans hb1⟩ + ((j : ℝ) / N) ⟨ha0, hja.trans hb1⟩ + simpa only [abs_of_nonneg (sub_nonneg.mpr hx.1)] using hh) + simpa only [hdif] using he + _ ≤ L / N := by + simp only [Finset.sum_const, Finset.card_range, nsmul_eq_mul] + have hkr : (k : ℝ) ≤ N := by exact_mod_cast hk + calc + _ ≤ (N : ℝ) * (L * (1 / (N : ℝ)) ^ 2) := by gcongr + _ = L / N := by field_simp + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/GridCavityError.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/GridCavityError.lean new file mode 100644 index 0000000000000000000000000000000000000000..dc3022dc1f1d94300fa0e8f28f97af5ae29ddaf7 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/GridCavityError.lean @@ -0,0 +1,304 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Queries.TreeBind +import OAI.Combinatorics.TriangleRemoval.Stability.GeometricVolterraIdentity + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter + +namespace SharpTerminalLeave +open Filter +open scoped BigOperators Topology + +section GridApproximation +variable {ι τ : Type*} [Fintype ι] [Fintype τ] [DecidableEq ι] [DecidableEq τ] + +theorem messageIntegral_time_lipschitz (H : τ → Finset ι) (e : ι) (T : τ) + {s t : ℝ} (hs : s ∈ Set.Icc (0 : ℝ) 1) (ht : t ∈ Set.Icc (0 : ℝ) 1) : + |messageIntegral H (cavityLimit H) e T t - messageIntegral H (cavityLimit H) e T s| ≤ + |t - s| := by + let f : ℝ → ℝ := fun x => ∏ a ∈ (H T).erase e, cavityLimit H a (some T) x + have hc : Continuous f := continuous_message_integrand H _ (continuous_cavityLimit H) e T + have hf : ∀ x, f x ∈ Set.Icc (0 : ℝ) 1 := by + intro x + exact ⟨Finset.prod_nonneg (fun a _ => (cavityLimit_mem_unit H a (some T) x).1), + Finset.prod_le_one₀ (fun a _ => (cavityLimit_mem_unit H a (some T) x).1) + (fun a _ => (cavityLimit_mem_unit H a (some T) x).2)⟩ + simp only [messageIntegral, unitTime_of_mem hs, unitTime_of_mem ht] + change |(∫ x in (0 : ℝ)..t, f x) - ∫ x in (0 : ℝ)..s, f x| ≤ _ + rw [intervalIntegral.integral_interval_sub_left (hc.intervalIntegrable _ _) (hc.intervalIntegrable _ _), + ← Real.norm_eq_abs] + simpa only [one_mul] using + (intervalIntegral.norm_integral_le_of_norm_le_const (a := s) (b := t) (C := (1 : ℝ)) + (f := f) (fun x _ => by simpa only [Real.norm_eq_abs, abs_of_nonneg (hf x).1] using (hf x).2)) + +theorem cavityLimit_time_lipschitz (H : τ → Finset ι) (e : ι) (p : Option τ) + {s t : ℝ} (hs : s ∈ Set.Icc (0 : ℝ) 1) (ht : t ∈ Set.Icc (0 : ℝ) 1) : + |cavityLimit H e p t - cavityLimit H e p s| ≤ (Fintype.card τ : ℝ) * |t - s| := by + rw [cavityLimit_product H e p t, cavityLimit_product H e p s] + unfold cavityOperator + have hm : ∀ T u, 1 - messageIntegral H (cavityLimit H) e T u ∈ Set.Icc (0 : ℝ) 1 := by + intro T u + have h := messageIntegral_mem_unit H _ (continuous_cavityLimit H) (cavityLimit_mem_unit H) e T u + constructor <;> linarith [h.1, h.2] + calc + _ ≤ ∑ T ∈ messageCandidates H e p, + |(1 - messageIntegral H (cavityLimit H) e T t) - + (1 - messageIntegral H (cavityLimit H) e T s)| := + abs_prod_sub_prod_le_sum _ _ _ (fun T _ => hm T t) (fun T _ => hm T s) + _ ≤ ∑ _T ∈ messageCandidates H e p, |t - s| := by + apply Finset.sum_le_sum + intro T _ + rw [sub_sub_sub_cancel_left, abs_sub_comm] + exact messageIntegral_time_lipschitz H e T hs ht + _ ≤ (Fintype.card τ : ℝ) * |t - s| := by + simp only [Finset.sum_const, nsmul_eq_mul] + gcongr + exact_mod_cast Finset.card_le_univ (messageCandidates H e p) + +theorem cavity_integrand_time_lipschitz (H : τ → Finset ι) (e : ι) (T : τ) + {s t : ℝ} (hs : s ∈ Set.Icc (0 : ℝ) 1) (ht : t ∈ Set.Icc (0 : ℝ) 1) : + |(∏ f ∈ (H T).erase e, cavityLimit H f (some T) t) - + ∏ f ∈ (H T).erase e, cavityLimit H f (some T) s| ≤ + ((Fintype.card ι : ℝ) * Fintype.card τ) * |t - s| := by + calc + _ ≤ ∑ f ∈ (H T).erase e, |cavityLimit H f (some T) t - cavityLimit H f (some T) s| := + abs_prod_sub_prod_le_sum _ _ _ (fun f _ => cavityLimit_mem_unit H f (some T) t) + (fun f _ => cavityLimit_mem_unit H f (some T) s) + _ ≤ ∑ _f ∈ (H T).erase e, (Fintype.card τ : ℝ) * |t - s| := + Finset.sum_le_sum (fun f _ => cavityLimit_time_lipschitz H f (some T) hs ht) + _ ≤ ((Fintype.card ι : ℝ) * Fintype.card τ) * |t - s| := by + simp only [Finset.sum_const, nsmul_eq_mul, mul_assoc] + gcongr + exact_mod_cast Finset.card_le_univ ((H T).erase e) + +omit [Fintype ι] [Fintype τ] [DecidableEq ι] [DecidableEq τ] in +theorem sum_fin_filter_lt_eq_range (N k : ℕ) (hk : k ≤ N) (f : ℕ → ℝ) : + (∑ u ∈ Finset.univ.filter (fun u : Fin N => u.val < k), f u.val) = + ∑ j ∈ Finset.range k, f j := by + rw [Finset.sum_filter] + calc + _ = ∑ j ∈ Finset.range N, if j < k then f j else 0 := + Fin.sum_univ_eq_sum_range (fun j => if j < k then f j else 0) N + _ = + ∑ j ∈ Finset.range k, if j < k then f j else 0 := by + symm + apply Finset.sum_subset (Finset.range_mono hk) + intro j _ hj + simp only [Finset.mem_range] at hj + simp only [hj, ↓reduceIte] + _ = _ := by + apply Finset.sum_congr rfl + intro j hj + simp only [Finset.mem_range.mp hj, ↓reduceIte] + +omit [Fintype ι] in + +theorem gridMessage_exact_range (H : τ → Finset ι) (N : ℕ) [NeZero N] + (k : ℕ) (hk : k ≤ N) (e : ι) (parent : Option τ) : + gridMessage H N N k e parent = + ∏ T ∈ messageCandidates H e parent, + (1 - (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, + ∏ f ∈ (H T).erase e, gridMessage H N N j f (some T)) := by + rw [gridMessage_exact H N k hk] + apply Finset.prod_congr rfl + intro T _ + congr 2 + exact sum_fin_filter_lt_eq_range N k hk + (fun j => ∏ f ∈ (H T).erase e, gridMessage H N N j f (some T)) + +omit [Fintype ι] [Fintype τ] [DecidableEq ι] [DecidableEq τ] in +theorem uniform_subaverage_mem_unit (N k : ℕ) (hN : 0 < N) (hk : k ≤ N) + (f : ℕ → ℝ) (hf : ∀ j < k, f j ∈ Set.Icc (0 : ℝ) 1) : + 1 - (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, f j ∈ Set.Icc (0 : ℝ) 1 := by + have hNr : (0 : ℝ) < N := by exact_mod_cast hN + have hs0 : 0 ≤ (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, f j := + mul_nonneg (by positivity) (Finset.sum_nonneg (fun j hj => (hf j (Finset.mem_range.mp hj)).1)) + have hs1 : (∑ j ∈ Finset.range k, f j) ≤ (N : ℝ) := by + calc + _ ≤ ∑ _j ∈ Finset.range k, (1 : ℝ) := + Finset.sum_le_sum (fun j hj => (hf j (Finset.mem_range.mp hj)).2) + _ = (k : ℝ) := by simp + _ ≤ (N : ℝ) := by exact_mod_cast hk + have hs2 : (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, f j ≤ 1 := by + calc + _ ≤ (1 / (N : ℝ)) * N := mul_le_mul_of_nonneg_left hs1 (by positivity) + _ = 1 := by field_simp + constructor <;> linarith + +omit [Fintype ι] in +theorem gridMessage_mem_unit (H : τ → Finset ι) (N : ℕ) [NeZero N] + (d k : ℕ) (e : ι) (p : Option τ) : gridMessage H N d k e p ∈ Set.Icc (0 : ℝ) 1 := + gridAnswerProbability_mem_unit H N d k {e} p + +noncomputable def gridCavityError (H : τ → Finset ι) (N : ℕ) [NeZero N] (k : ℕ) : ℝ := + ‖(fun ep : ι × Option τ => gridMessage H N N k ep.1 ep.2 - + cavityLimit H ep.1 ep.2 ((k : ℝ) / N))‖ + +theorem gridCavityError_nonneg (H : τ → Finset ι) (N : ℕ) [NeZero N] (k : ℕ) : + 0 ≤ gridCavityError H N k := norm_nonneg _ + +theorem gridCavityError_coord (H : τ → Finset ι) (N : ℕ) [NeZero N] + (k : ℕ) (e : ι) (p : Option τ) : + |gridMessage H N N k e p - cavityLimit H e p ((k : ℝ) / N)| ≤ gridCavityError H N k := by + exact norm_le_pi_norm (fun ep : ι × Option τ => + gridMessage H N N k ep.1 ep.2 - cavityLimit H ep.1 ep.2 ((k : ℝ) / N)) (e, p) + +theorem grid_integrand_error (H : τ → Finset ι) (N : ℕ) [NeZero N] + (k : ℕ) (e : ι) (T : τ) : + |(∏ f ∈ (H T).erase e, gridMessage H N N k f (some T)) - + ∏ f ∈ (H T).erase e, cavityLimit H f (some T) ((k : ℝ) / N)| ≤ + (Fintype.card ι : ℝ) * gridCavityError H N k := by + calc + _ ≤ ∑ f ∈ (H T).erase e, + |gridMessage H N N k f (some T) - cavityLimit H f (some T) ((k : ℝ) / N)| := + abs_prod_sub_prod_le_sum _ _ _ (fun f _ => gridMessage_mem_unit H N N k f (some T)) + (fun f _ => cavityLimit_mem_unit H f (some T) ((k : ℝ) / N)) + _ ≤ ∑ _f ∈ (H T).erase e, gridCavityError H N k := + Finset.sum_le_sum (fun f _ => gridCavityError_coord H N k f (some T)) + _ ≤ (Fintype.card ι : ℝ) * gridCavityError H N k := by + simp only [Finset.sum_const, nsmul_eq_mul] + apply mul_le_mul_of_nonneg_right _ (gridCavityError_nonneg H N k) + exact_mod_cast Finset.card_le_univ ((H T).erase e) + +theorem grid_message_integral_error (H : τ → Finset ι) (N : ℕ) [NeZero N] + (k : ℕ) (hk : k ≤ N) (e : ι) (T : τ) : + |(1 / (N : ℝ)) * (∑ j ∈ Finset.range k, + ∏ f ∈ (H T).erase e, gridMessage H N N j f (some T)) - + messageIntegral H (cavityLimit H) e T ((k : ℝ) / N)| ≤ + ((Fintype.card ι : ℝ) / N) * ∑ j ∈ Finset.range k, gridCavityError H N j + + ((Fintype.card ι : ℝ) * Fintype.card τ) / N := by + have hN : 0 < N := Nat.pos_of_ne_zero (NeZero.ne N) + have hNr : (0 : ℝ) < N := by exact_mod_cast hN + let f : ℝ → ℝ := fun s => ∏ a ∈ (H T).erase e, cavityLimit H a (some T) s + have hc : Continuous f := continuous_message_integrand H _ (continuous_cavityLimit H) e T + have hquad := uniform_grid_quadrature f hc (by positivity : + 0 ≤ (Fintype.card ι : ℝ) * Fintype.card τ) + (fun x hx y hy => cavity_integrand_time_lipschitz H e T hy hx) N k hN hk + have htime : (k : ℝ) / N ∈ Set.Icc (0 : ℝ) 1 := + ⟨by positivity, (div_le_one hNr).mpr (by exact_mod_cast hk)⟩ + have hi : messageIntegral H (cavityLimit H) e T ((k : ℝ) / N) = + ∫ s in (0 : ℝ)..((k : ℝ) / N), f s := by + simp only [messageIntegral, unitTime_of_mem htime, f] + rw [hi] + calc + _ ≤ |(1 / (N : ℝ)) * (∑ j ∈ Finset.range k, + ∏ a ∈ (H T).erase e, gridMessage H N N j a (some T)) - + (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, f ((j : ℝ) / N)| + + |(1 / (N : ℝ)) * (∑ j ∈ Finset.range k, f ((j : ℝ) / N)) - + ∫ s in (0 : ℝ)..((k : ℝ) / N), f s| := abs_sub_le _ _ _ + _ ≤ ((Fintype.card ι : ℝ) / N) * ∑ j ∈ Finset.range k, gridCavityError H N j + + ((Fintype.card ι : ℝ) * Fintype.card τ) / N := by + apply add_le_add _ (by simpa only [abs_sub_comm] using hquad) + rw [← mul_sub, abs_mul, abs_of_nonneg (by positivity : 0 ≤ 1 / (N : ℝ)), + ← Finset.sum_sub_distrib] + calc + _ ≤ (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, + |(∏ a ∈ (H T).erase e, gridMessage H N N j a (some T)) - f ((j : ℝ) / N)| := + mul_le_mul_of_nonneg_left (Finset.abs_sum_le_sum_abs _ _) (by positivity) + _ ≤ (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, + (Fintype.card ι : ℝ) * gridCavityError H N j := by + gcongr with j hj + exact grid_integrand_error H N j e T + _ = _ := by rw [← Finset.mul_sum]; ring + +theorem gridCavityError_volterra (H : τ → Finset ι) (N : ℕ) [NeZero N] + (k : ℕ) (hk : k ≤ N) : + gridCavityError H N k ≤ + ((Fintype.card τ : ℝ) ^ 2 * Fintype.card ι) / N + + ((Fintype.card τ : ℝ) * Fintype.card ι / N) * + ∑ j ∈ Finset.range k, gridCavityError H N j := by + have hN : 0 < N := Nat.pos_of_ne_zero (NeZero.ne N) + have hNr : (0 : ℝ) < N := by exact_mod_cast hN + have hb : 0 ≤ ((Fintype.card ι : ℝ) / N) * ∑ j ∈ Finset.range k, gridCavityError H N j + + ((Fintype.card ι : ℝ) * Fintype.card τ) / N := by + have := Finset.sum_nonneg (s := Finset.range k) (fun j _ => gridCavityError_nonneg H N j) + positivity + unfold gridCavityError + apply (pi_norm_le_iff_of_nonneg (by positivity)).mpr + intro ep + rw [Real.norm_eq_abs, gridMessage_exact_range H N k hk, + cavityLimit_product H ep.1 ep.2 ((k : ℝ) / N)] + unfold cavityOperator + let g : τ → ℝ := fun T => 1 - (1 / (N : ℝ)) * ∑ j ∈ Finset.range k, + ∏ f ∈ (H T).erase ep.1, gridMessage H N N j f (some T) + let q : τ → ℝ := fun T => 1 - messageIntegral H (cavityLimit H) ep.1 T ((k : ℝ) / N) + have hg : ∀ T, g T ∈ Set.Icc (0 : ℝ) 1 := by + intro T + apply uniform_subaverage_mem_unit N k hN hk + intro j _ + exact ⟨Finset.prod_nonneg (fun f _ => (gridMessage_mem_unit H N N j f (some T)).1), + Finset.prod_le_one₀ (fun f _ => (gridMessage_mem_unit H N N j f (some T)).1) + (fun f _ => (gridMessage_mem_unit H N N j f (some T)).2)⟩ + have hq : ∀ T, q T ∈ Set.Icc (0 : ℝ) 1 := by + intro T + have h := messageIntegral_mem_unit H _ (continuous_cavityLimit H) (cavityLimit_mem_unit H) + ep.1 T ((k : ℝ) / N) + change 1 - _ ∈ _ + constructor <;> linarith [h.1, h.2] + change |(∏ T ∈ messageCandidates H ep.1 ep.2, g T) - + ∏ T ∈ messageCandidates H ep.1 ep.2, q T| ≤ _ + calc + _ ≤ ∑ T ∈ messageCandidates H ep.1 ep.2, |g T - q T| := + abs_prod_sub_prod_le_sum _ _ _ (fun T _ => hg T) (fun T _ => hq T) + _ ≤ ∑ _T ∈ messageCandidates H ep.1 ep.2, + (((Fintype.card ι : ℝ) / N) * ∑ j ∈ Finset.range k, gridCavityError H N j + + ((Fintype.card ι : ℝ) * Fintype.card τ) / N) := by + apply Finset.sum_le_sum + intro T _ + dsimp only [g, q] + rw [sub_sub_sub_cancel_left, abs_sub_comm] + exact grid_message_integral_error H N k hk ep.1 T + _ ≤ (Fintype.card τ : ℝ) * + (((Fintype.card ι : ℝ) / N) * ∑ j ∈ Finset.range k, gridCavityError H N j + + ((Fintype.card ι : ℝ) * Fintype.card τ) / N) := by + simp only [Finset.sum_const, nsmul_eq_mul] + apply mul_le_mul_of_nonneg_right _ hb + exact_mod_cast Finset.card_le_univ (messageCandidates H ep.1 ep.2) + _ = _ := by unfold gridCavityError; ring + +theorem gridCavityError_bound (H : τ → Finset ι) (N : ℕ) [NeZero N] + (k : ℕ) (hk : k ≤ N) : + gridCavityError H N k ≤ + (((Fintype.card τ : ℝ) ^ 2 * Fintype.card ι) / N) * + Real.exp ((Fintype.card τ : ℝ) * Fintype.card ι) := by + exact discrete_volterra_exp_bound (gridCavityError H N) + ((Fintype.card τ : ℝ) * Fintype.card ι) + ((Fintype.card τ : ℝ) ^ 2 * Fintype.card ι) (by positivity) (by positivity) + N (Nat.pos_of_ne_zero (NeZero.ne N)) (gridCavityError_volterra H N) k hk + +theorem gridMessage_tendsto_terminal (H : τ → Finset ι) (e : ι) (p : Option τ) : + Tendsto (fun n : ℕ => gridMessage H (n + 1) (n + 1) (n + 1) e p) + atTop (𝓝 (cavityLimit H e p 1)) := by + have hden : Tendsto (fun n : ℕ => (1 : ℝ) / ((n + 1 : ℕ) : ℝ)) atTop (𝓝 0) := by + simpa only [Nat.cast_add, Nat.cast_one] using + (tendsto_one_div_add_atTop_nhds_zero_nat : + Tendsto (fun n : ℕ => (1 : ℝ) / (n + 1)) atTop (𝓝 0)) + let C : ℝ := ((Fintype.card τ : ℝ) ^ 2 * Fintype.card ι) * + Real.exp ((Fintype.card τ : ℝ) * Fintype.card ι) + have hbound : ∀ n : ℕ, + ‖gridMessage H (n + 1) (n + 1) (n + 1) e p - cavityLimit H e p 1‖ ≤ + C * (1 / ((n + 1 : ℕ) : ℝ)) := by + intro n + have hn : ((n + 1 : ℕ) : ℝ) ≠ 0 := by positivity + have hx := (gridCavityError_coord H (n + 1) (n + 1) e p).trans + (gridCavityError_bound H (n + 1) (n + 1) le_rfl) + simpa only [div_self hn, Real.norm_eq_abs, C, div_eq_mul_inv, one_mul, + mul_assoc, mul_comm, mul_left_comm] using hx + apply tendsto_iff_norm_sub_tendsto_zero.mpr + exact squeeze_zero (fun n => norm_nonneg _) hbound + (by simpa using tendsto_const_nhds.mul hden) + +end GridApproximation +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/GridCavityWeightLower.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/GridCavityWeightLower.lean new file mode 100644 index 0000000000000000000000000000000000000000..d3e480396bd16039fade5b1b7586a42d29d43bff --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/GridCavityWeightLower.lean @@ -0,0 +1,131 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Queries.CanonicalCollisionSingletonSum +import OAI.Combinatorics.TriangleRemoval.Stability.ActiveCavityFactorProduct +import OAI.Combinatorics.TriangleRemoval.Stability.ContinuousCavityWeightClip +import OAI.Combinatorics.TriangleRemoval.Probability.PmfMeanLiminfTendsto + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma grid_cavityWeight_lower {D : ℝ} (hD : 0 ≤ D) (N : ℕ) [NeZero N] : + ∀ t ≤ N, 1 ≤ (1+2*D)*cavityWeight D ((t : ℝ)/N) := by + intro t ht + have hNr : (0 : ℝ) < N := by exact_mod_cast Nat.pos_of_ne_zero (NeZero.ne N) + have hmem : (t : ℝ)/N ∈ Set.Icc (0 : ℝ) 1 := + ⟨by positivity,(div_le_one hNr).mpr (by exact_mod_cast ht)⟩ + have hh := mul_le_mul_of_nonneg_left (cavityWeight_lower hD hmem) + (show 0 ≤ 1+2*D by positivity) + have heq : (1+2*D)*cavityWeight D 1 = 1 := by + unfold cavityWeight + rw [mul_one,mul_one_div_cancel (by positivity : 1+2*D ≠ 0)] + rwa [heq] at hh + +lemma grid_collision_majorant_tendsto (K : ℝ) {D : ℝ} (hD : 0 < D) : + Tendsto (fun j : ℕ => (K*(1+2*D)^3/D^50)* + Real.exp (45*D*prefixWeight (fun t => (2/((j+1 : ℕ) : ℝ))* + cavityWeight D ((t : ℝ)/(j+1 : ℕ))) (j+1))) + atTop (𝓝 (K*(1+2*D)^48/D^50)) := by + have hh := (Real.continuous_exp.continuousAt.tendsto.comp + ((grid_prefixWeight_tendsto hD).const_mul (45*D))).const_mul + (K*(1+2*D)^3/D^50) + have he : (K*(1+2*D)^3/D^50)*Real.exp (45*D*(2*witnessMu D)) = + K*(1+2*D)^48/D^50 := by + calc + _ = K*((1+2*D)^3*Real.exp (45*D*(2*witnessMu D)))/D^50 := by ring + _ = _ := by rw [collision_exponential_identity hD] + rwa [he] at hh + +theorem averaged_disjoint_collisionCost (c₀ C₀ : ℝ) (hc₀ : 0 < c₀) : + ∀ᶠ n : ℕ in atTop, ∀ (G : Graph n), GoodPrefixGraph n c₀ C₀ G → + ∀ hne : G.Nonempty, + pmfMean (PMF.uniformOfFinset G hne) (fun e => + pmfMean (PMF.uniformOfFinset G hne) (fun f => if Disjoint e f then + collisionCost (triangleHypergraph G) {e,f} none else 0)) ≤ + 65536*(1+2*prefixD n)^48/prefixD n^50 := by + classical + filter_upwards [averaged_disjoint_grid_collision c₀ C₀ hc₀, + goodPrefix_full_grid_rows hc₀,goodPrefix_full_continuous_rows hc₀] with n hfinite hrows hn + intro G hGood hne + let D := prefixD n + let P := PMF.uniformOfFinset G hne + let PP := P.bind (fun e => P.map (Prod.mk e)) + let f : (Finset (Fin n) × Finset (Fin n)) → ℕ → ℝ := fun ef j => + if Disjoint ef.1 ef.2 then gridCollision (triangleHypergraph G) {ef.1,ef.2} none j else 0 + let g : ℕ → ℝ := fun j => (65536*(1+2*D)^3/D^50)* + Real.exp (45*D*prefixWeight (fun t => (2/((j+1 : ℕ) : ℝ))* + cavityWeight D ((t : ℝ)/(j+1 : ℕ))) (j+1)) + have hbound (ef : Finset (Fin n) × Finset (Fin n)) (j : ℕ) : 0 ≤ f ef j := by + dsimp only [f] + split_ifs <;> first | exact (gridCollision_mem_unit _ _ _ _).1 | rfl + obtain ⟨N₀,hN₀⟩ := hrows C₀ G hGood + have hupper : ∀ᶠ j : ℕ in atTop, pmfMean PP (fun ef => f ef j) ≤ g j := by + filter_upwards [eventually_ge_atTop N₀] with j hj + dsimp only [PP] + rw [pmfMean_bind] + simp_rw [pmfMean_map] + have hh := hfinite G hGood hne (j+1) + (fun t => cavityWeight D ((t : ℝ)/(j+1 : ℕ))) + (fun t => (cavityWeight_pos hn.1.le (by positivity)).le) + (hN₀ (j+1) (by omega)) (1+2*D) + (by dsimp only [D]; linarith [hn.1]) (grid_cavityWeight_lower hn.1.le (j+1)) + (j+1) (j+1) le_rfl le_rfl + simpa only [gridCollision_eq_trace,P,f,g,D] using hh + have hh := pmfMean_liminf_le_of_tendsto_upper PP f hbound g + (grid_collision_majorant_tendsto 65536 hn.1) hupper + dsimp only [PP] at hh + rw [pmfMean_bind] at hh + simp_rw [pmfMean_map] at hh + have hf (e f' : Finset (Fin n)) : Filter.liminf (f (e,f')) atTop = + if Disjoint e f' then collisionCost (triangleHypergraph G) {e,f'} none else 0 := by + by_cases hd : Disjoint e f' + · simp only [f,hd,ite_true,collisionCost] + · simp only [f,hd,ite_false,Filter.liminf_const] + simp_rw [hf] at hh + exact hh + +theorem averaged_singleton_collisionCost (c₀ C₀ : ℝ) (hc₀ : 0 < c₀) : + ∀ᶠ n : ℕ in atTop, ∀ (G : Graph n), GoodPrefixGraph n c₀ C₀ G → + ∀ hne : G.Nonempty, + pmfMean (PMF.uniformOfFinset G hne) (fun e => + collisionCost (triangleHypergraph G) {e} none) ≤ + 8192*(1+2*prefixD n)^48/prefixD n^50 := by + classical + filter_upwards [averaged_singleton_grid_collision c₀ C₀ hc₀, + goodPrefix_full_grid_rows hc₀,goodPrefix_full_continuous_rows hc₀] with n hfinite hrows hn + intro G hGood hne + let D := prefixD n + let P := PMF.uniformOfFinset G hne + let f : Finset (Fin n) → ℕ → ℝ := fun e j => gridCollision (triangleHypergraph G) {e} none j + let g : ℕ → ℝ := fun j => (8192*(1+2*D)^3/D^50)* + Real.exp (45*D*prefixWeight (fun t => (2/((j+1 : ℕ) : ℝ))* + cavityWeight D ((t : ℝ)/(j+1 : ℕ))) (j+1)) + have hbound (e : Finset (Fin n)) (j : ℕ) : 0 ≤ f e j := + (gridCollision_mem_unit _ _ _ _).1 + obtain ⟨N₀,hN₀⟩ := hrows C₀ G hGood + have hupper : ∀ᶠ j : ℕ in atTop, pmfMean P (fun e => f e j) ≤ g j := by + filter_upwards [eventually_ge_atTop N₀] with j hj + have hh := hfinite G hGood hne (j+1) + (fun t => cavityWeight D ((t : ℝ)/(j+1 : ℕ))) + (fun t => (cavityWeight_pos hn.1.le (by positivity)).le) + (hN₀ (j+1) (by omega)) (1+2*D) + (by dsimp only [D]; linarith [hn.1]) (grid_cavityWeight_lower hn.1.le (j+1)) + (j+1) (j+1) le_rfl le_rfl + simpa only [gridCollision_eq_trace,P,f,g,D] using hh + exact pmfMean_liminf_le_of_tendsto_upper P f hbound g + (grid_collision_majorant_tendsto 8192 hn.1) hupper + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/MovingPerturbationFiniteExpansion.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/MovingPerturbationFiniteExpansion.lean new file mode 100644 index 0000000000000000000000000000000000000000..abe15b2c7672ef42ede8c515b03b40088f9972ad --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/MovingPerturbationFiniteExpansion.lean @@ -0,0 +1,96 @@ +import Mathlib + +namespace OAI + +section +noncomputable section +open scoped BigOperators + +namespace SharpTerminalLeave + +theorem moving_perturbation_finite_expansion {X : Type*} [AddCommGroup X] [Module ℝ X] + (V : Module.End ℝ X) (x y : X) (h : x = y + V x) (K : ℕ) : + x = (∑ j ∈ Finset.range K, (V^j) y) + (V^K) x := by + induction K with + | zero => simp + | succ K ih => + rw [Finset.sum_range_succ, pow_succ] + calc + x = (∑ j ∈ Finset.range K, (V^j) y) + (V^K) x := ih + _ = (∑ j ∈ Finset.range K, (V^j) y) + (V^K) (y + V x) := by rw [← h] + _ = _ := by simp [Module.End.mul_apply, add_assoc] + +lemma seminorm_end_pow_le {X : Type*} [AddCommGroup X] [Module ℝ X] + (p : Seminorm ℝ X) (V : Module.End ℝ X) {A : ℝ} (hA : 0 ≤ A) + (hV : ∀ x, p (V x) ≤ A * p x) (K : ℕ) (x : X) : + p ((V^K) x) ≤ A^K * p x := by + induction K with + | zero => simp + | succ K ih => + rw [pow_succ',Module.End.mul_apply,pow_succ'] + exact (hV _).trans (by simpa only [mul_assoc] using mul_le_mul_of_nonneg_left ih hA) + +lemma seminorm_finset_sum_le {X I : Type*} [AddCommGroup X] [Module ℝ X] + (p : Seminorm ℝ X) (s : Finset I) (f : I → X) : + p (∑ i ∈ s, f i) ≤ ∑ i ∈ s, p (f i) := by + classical + induction s using Finset.induction_on with + | empty => simp + | @insert i s hi ih => + simp only [Finset.sum_insert hi] + exact (map_add_le_add p _ _).trans (add_le_add le_rfl ih) + +theorem moving_perturbation_mixed_norm_bound {X : Type*} [AddCommGroup X] [Module ℝ X] + (p q : Seminorm ℝ X) (V : Module.End ℝ X) (x y : X) + {A B C : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) + (hVp : ∀ z, p (V z) ≤ A * p z) (hVq : ∀ z, q (V z) ≤ B * q z) + (hpq : ∀ z, p z ≤ q z) (hqp : ∀ z, q z ≤ C * p z) + (h : x = y + V x) (K : ℕ) : + p x ≤ (∑ j ∈ Finset.range K, A^j) * p y + B^K * C * p x := by + have he := moving_perturbation_finite_expansion V x y h K + calc + p x = p ((∑ j ∈ Finset.range K, (V^j) y) + (V^K) x) := congrArg p he + _ ≤ p (∑ j ∈ Finset.range K, (V^j) y) + p ((V^K) x) := map_add_le_add p _ _ + _ ≤ (∑ j ∈ Finset.range K, p ((V^j) y)) + q ((V^K) x) := + add_le_add (seminorm_finset_sum_le p _ _) (hpq _) + _ ≤ (∑ j ∈ Finset.range K, A^j * p y) + B^K * q x := + add_le_add (Finset.sum_le_sum (fun j _ => seminorm_end_pow_le p V hA hVp j y)) + (seminorm_end_pow_le q V hB hVq K x) + _ ≤ (∑ j ∈ Finset.range K, A^j * p y) + B^K * (C * p x) := + add_le_add le_rfl (mul_le_mul_of_nonneg_left (hqp x) (pow_nonneg hB K)) + _ = _ := by rw [Finset.sum_mul]; ring + +theorem moving_perturbation_mixed_norm_absorb {X : Type*} [AddCommGroup X] [Module ℝ X] + (p q : Seminorm ℝ X) (V : Module.End ℝ X) (x y : X) + {A B C : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) + (hVp : ∀ z, p (V z) ≤ A * p z) (hVq : ∀ z, q (V z) ≤ B * q z) + (hpq : ∀ z, p z ≤ q z) (hqp : ∀ z, q z ≤ C * p z) + (h : x = y + V x) (K : ℕ) (hK : B^K*C ≤ 1/2) : + p x ≤ 2*(∑ j ∈ Finset.range K, A^j)*p y := by + have hb := moving_perturbation_mixed_norm_bound p q V x y hA hB hVp hVq hpq hqp h K + have hp : 0 ≤ p x := apply_nonneg p x + have ht := mul_le_mul_of_nonneg_right hK hp + nlinarith only [hb,ht] + +theorem moving_perturbation_mixed_norm_absorb_modulo {X : Type*} [AddCommGroup X] [Module ℝ X] + (p q : Seminorm ℝ X) (V : Module.End ℝ X) (x y : X) + {A B C : ℝ} (hA : 0 ≤ A) (hB : 0 ≤ B) + (hVp : ∀ z, p (V z) ≤ A * p z) (hVq : ∀ z, q (V z) ≤ B * q z) + (hpq : ∀ z, p z ≤ q z) (hqp : ∀ z, q z ≤ C * p z) + (h : p (x-y-V x) = 0) (K : ℕ) (hK : B^K*C ≤ 1/2) : + p x ≤ 2*(∑ j ∈ Finset.range K, A^j)*p y := by + let z := x-y-V x + have he : x = (y+z)+V x := by dsimp only [z]; abel + have hb := moving_perturbation_mixed_norm_absorb p q V x (y+z) hA hB hVp hVq hpq hqp he K hK + have hy : p (y+z) ≤ p y := by + have hh := map_add_le_add p y z + change p z = 0 at h + linarith only [hh,h] + apply hb.trans + exact mul_le_mul_of_nonneg_left hy (by positivity) + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/MovingResidualForcing.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/MovingResidualForcing.lean new file mode 100644 index 0000000000000000000000000000000000000000..116f93dde12a52522ba1a00cc3fdfd3400e23ff1 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/MovingResidualForcing.lean @@ -0,0 +1,171 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Stability.MovingPerturbationFiniteExpansion +import OAI.Combinatorics.TriangleRemoval.Asymptotics.ActiveL2Map + +namespace OAI + +section +noncomputable section +open scoped BigOperators + +namespace SharpTerminalLeave +open Classical + +lemma moving_active_subset {E : Type*} (active : ℕ → Finset E) (T : ℕ) + (hnest : ∀ k < T, active (k+1) ⊆ active k) {i j : ℕ} (hij : i ≤ j) (hj : j ≤ T) : + active j ⊆ active i := by + induction j, hij using Nat.le_induction with + | base => exact Finset.Subset.refl _ + | succ k hik ih => exact (hnest k (by omega)).trans (ih (by omega)) + +lemma activeLinf_mono {E : Type*} [Fintype E] {s t : Finset E} (h : s ⊆ t) (x : E → ℝ) : + activeLinf s x ≤ activeLinf t x := + activeLinf_bound s x (apply_nonneg _ _) (fun _ he => activeLinf_coordinate t x (h he)) + +noncomputable def movingResidualForcing {E : Type*} [Fintype E] + (a : ℕ → ℝ) (R : ℕ → Module.End ℝ (E → ℝ)) : Module.End ℝ (ℕ → E → ℝ) where + toFun x t := ∑ i ∈ Finset.range t, (-a i) • (R i (x i)) + map_add' x y := by + funext t + simp only [Pi.add_apply,map_add,smul_add,Finset.sum_add_distrib] + map_smul' c x := by + funext t + simp only [Pi.smul_apply,map_smul,smul_comm (-a _) c,Finset.smul_sum,RingHom.id_apply] + +lemma movingResidualForcing_zero {E : Type*} [Fintype E] + (a : ℕ → ℝ) (R : ℕ → Module.End ℝ (E → ℝ)) (x : ℕ → E → ℝ) : + movingResidualForcing a R x 0 = 0 := by simp [movingResidualForcing] + +lemma movingResidualForcing_succ {E : Type*} [Fintype E] + (a : ℕ → ℝ) (R : ℕ → Module.End ℝ (E → ℝ)) (x : ℕ → E → ℝ) (t : ℕ) : + movingResidualForcing a R x (t+1) = movingResidualForcing a R x t-a t • R t (x t) := by + simp [movingResidualForcing,Finset.sum_range_succ,sub_eq_add_neg,add_comm] + +lemma movingResidualForcing_bound {E : Type*} [Fintype E] + (P : ℕ → Seminorm ℝ (E → ℝ)) (a : ℕ → ℝ) (R : ℕ → Module.End ℝ (E → ℝ)) + (T : ℕ) (ha : ∀ i < T, 0 ≤ a i) + (hmono : ∀ i j, i ≤ j → j ≤ T → ∀ x, P j x ≤ P i x) + {B : ℝ} (hB : 0 ≤ B) (hR : ∀ i < T, ∀ x, P i (R i x) ≤ B*P i x) + (x : ℕ → E → ℝ) : + trajectorySeminorm P T (movingResidualForcing a R x) ≤ + (∑ i ∈ Finset.range T, a i)*B*trajectorySeminorm P T x := by + have hS : 0 ≤ ∑ i ∈ Finset.range T, a i := + Finset.sum_nonneg (fun i hi => ha i (Finset.mem_range.mp hi)) + have hx0 := apply_nonneg (trajectorySeminorm P T) x + apply trajectorySeminorm_bound _ _ _ (by positivity) + intro t ht + change P t (∑ i ∈ Finset.range t, (-a i) • R i (x i)) ≤ _ + calc + _ ≤ ∑ i ∈ Finset.range t, P t ((-a i) • R i (x i)) := seminorm_finset_sum_le _ _ _ + _ ≤ ∑ i ∈ Finset.range t, a i*(B*trajectorySeminorm P T x) := by + apply Finset.sum_le_sum + intro i hi + have hit : i < t := Finset.mem_range.mp hi + have hiT : i < T := by omega + rw [map_smul_eq_mul,Real.norm_eq_abs,abs_neg,abs_of_nonneg (ha i hiT)] + apply mul_le_mul_of_nonneg_left _ (ha i hiT) + exact (hmono i t hit.le ht _).trans ((hR i hiT _).trans + (mul_le_mul_of_nonneg_left (trajectorySeminorm_at P T x (by omega)) hB)) + _ ≤ ∑ i ∈ Finset.range T, a i*(B*trajectorySeminorm P T x) := + Finset.sum_le_sum_of_subset_of_nonneg (Finset.range_mono ht) + (fun i hi _ => mul_nonneg (ha i (Finset.mem_range.mp hi)) (mul_nonneg hB hx0)) + _ = _ := by rw [← Finset.sum_mul]; ring + +lemma movingStar_actual_response {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (active : ℕ → Finset E) (a : ℕ → ℝ) + (R : ℕ → Module.End ℝ (E → ℝ)) (M x : ℕ → E → ℝ) (T : ℕ) + (hnest : ∀ k < T, active (k+1) ⊆ active k) + (hinit : ∀ e ∈ active 0, x 0 e = M 0 e) + (hstep : ∀ k < T, ∀ e ∈ active (k+1), x (k+1) e = x k e - + a k*(movingStarLift ends (movingStarMean ends (active k) (x k)) e+R k (x k) e) + + (M (k+1) e-M k e)) : + trajectorySeminorm (fun t => activeLinf (active t)) T + (x-movingStarResponse ends active a M- + movingStarResponse ends active a (movingResidualForcing a R x)) = 0 := by + let F := M+movingResidualForcing a R x + have hi : ∀ e ∈ active 0, x 0 e = F 0 e := by + intro e he + simp only [F,Pi.add_apply,movingResidualForcing_zero,Pi.zero_apply,add_zero] + exact hinit e he + have hs : ∀ k < T, ∀ e ∈ active (k+1), x (k+1) e = x k e - + a k*movingStarLift ends (movingStarMean ends (active k) (x k)) e + (F (k+1) e-F k e) := by + intro k hk e he + rw [hstep k hk e he] + simp only [F,Pi.add_apply,movingResidualForcing_succ,Pi.sub_apply,Pi.smul_apply,smul_eq_mul] + ring + apply le_antisymm _ (apply_nonneg _ _) + apply trajectorySeminorm_bound _ _ _ le_rfl + intro t ht + apply activeLinf_bound _ _ le_rfl + intro e he + have h₁ := movingStar_trajectory_representation ends active a F x t + (fun k hk => hnest k (by omega)) hi (fun k hk => hs k (by omega)) e he + have h₂ := movingStar_trajectory_representation ends active a F + (movingStarSolve ends active a F) t (fun k hk => hnest k (by omega)) + (fun _ _ => rfl) (fun _ _ _ _ => rfl) e he + have h_eq : x t e = movingStarResponse ends active a F t e := h₁.trans h₂.symm + change |x t e-movingStarResponse ends active a M t e- + movingStarResponse ends active a (movingResidualForcing a R x) t e| ≤ 0 + have hadd := congrFun (congrFun (map_add (movingStarResponse ends active a) + M (movingResidualForcing a R x)) t) e + change movingStarResponse ends active a F t e = _ at hadd + rw [h_eq,hadd] + simp + +theorem movingStar_mixed_stability {E V : Type*} [Fintype E] [Fintype V] + (ends : E → Finset V) (hends : ∀ e, (ends e).card = 2) + (active : ℕ → Finset E) (a : ℕ → ℝ) (R : ℕ → Module.End ℝ (E → ℝ)) + (M x : ℕ → E → ℝ) (T K : ℕ) + (hnest : ∀ k < T, active (k+1) ⊆ active k) + (ha : ∀ k < T, 0 ≤ a k ∧ a k ≤ 1) + (hinit : ∀ e ∈ active 0, x 0 e = M 0 e) + (hstep : ∀ k < T, ∀ e ∈ active (k+1), x (k+1) e = x k e - + a k*(movingStarLift ends (movingStarMean ends (active k) (x k)) e+R k (x k) e) + + (M (k+1) e-M k e)) + {B δ : ℝ} (hB : 0 ≤ B) (hδ : 0 ≤ δ) + (hRinf : ∀ k < T, ∀ z, activeLinf (active k) (R k z) ≤ B*activeLinf (active k) z) + (hR₂ : ∀ k < T, ∀ z, activeL2 (active k) (R k z) ≤ δ*activeL2 (active k) z) + (hK : ((1+2*∑ k ∈ Finset.range T, a k)*(∑ k ∈ Finset.range T, a k)*δ)^K* + (Fintype.card E : ℝ) ≤ 1/2) : + trajectorySeminorm (fun t => activeLinf (active t)) T x ≤ + 2*(∑ j ∈ Finset.range K, + ((1+2*∑ k ∈ Finset.range T, a k)*(∑ k ∈ Finset.range T, a k)*B)^j)* + (1+2*∑ k ∈ Finset.range T, a k)*trajectorySeminorm (fun t => activeLinf (active t)) T M := by + let p := trajectorySeminorm (fun t => activeLinf (active t)) T + let q := trajectorySeminorm (fun t => activeL2 (active t)) T + let V₁ := (movingStarResponse ends active a).comp (movingResidualForcing a R) + let S := ∑ k ∈ Finset.range T, a k + let L := 1+2*S + have hS : 0 ≤ S := Finset.sum_nonneg (fun k hk => (ha k (Finset.mem_range.mp hk)).1) + have hL : 0 ≤ L := by dsimp [L]; positivity + have hVp : ∀ z, p (V₁ z) ≤ L*S*B*p z := by + intro z + have hb := movingStarResponse_linf ends hends active a T hnest ha (movingResidualForcing a R z) + have hf := movingResidualForcing_bound (fun t => activeLinf (active t)) a R T + (fun k hk => (ha k hk).1) + (fun i j hij hj z => activeLinf_mono (moving_active_subset active T hnest hij hj) z) hB hRinf z + exact hb.trans (by simpa only [mul_assoc] using mul_le_mul_of_nonneg_left hf hL) + have hVq : ∀ z, q (V₁ z) ≤ L*S*δ*q z := by + intro z + have hb := movingStarResponse_l2 ends hends active a T hnest ha (movingResidualForcing a R z) + have hf := movingResidualForcing_bound (fun t => activeL2 (active t)) a R T + (fun k hk => (ha k hk).1) + (fun i j hij hj z => activeL2_mono (moving_active_subset active T hnest hij hj) z) hδ hR₂ z + exact hb.trans (by simpa only [mul_assoc] using mul_le_mul_of_nonneg_left hf hL) + have hb := moving_perturbation_mixed_norm_absorb_modulo p q V₁ x + (movingStarResponse ends active a M) (by positivity : 0 ≤ L*S*B) (by positivity : 0 ≤ L*S*δ) + hVp hVq (fun z => (trajectory_mixed_seminorms active T z).1) + (fun z => (trajectory_mixed_seminorms active T z).2) + (movingStar_actual_response ends active a R M x T hnest hinit hstep) K hK + have hy := movingStarResponse_linf ends hends active a T hnest ha M + apply hb.trans + calc + _ ≤ 2*(∑ j ∈ Finset.range K, (L*S*B)^j)*(L*p M) := + mul_le_mul_of_nonneg_left hy (by positivity) + _ = _ := by dsimp only [p,L,S]; ring + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/RelativeTemplateResidual.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/RelativeTemplateResidual.lean new file mode 100644 index 0000000000000000000000000000000000000000..968591aa8ab4c170baa44e12009375d0c739cd10 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/RelativeTemplateResidual.lean @@ -0,0 +1,77 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.RelativeScaleBudget +import OAI.Combinatorics.TriangleRemoval.Process.TriangleLiftCount + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def relativeTemplateResidual {n : ℕ} {α : Type*} [Fintype α] + (required : α → Graph n) (s : ℕ → ℝ) (T j : ℕ) + (ω : History (Graph n) T) : ℝ := + copyCount required (ω (historyIndex T j))/s j - + copyCount required (ω (historyIndex T 0))/s 0 - + historyNoise (fun _ => step) (fun k H => copyCount required H/s k) T j ω + + historyCounter (fun k G => triangleLiftCount required G/ + ((triangles G).card*s (k+1))) T j ω - + ∑ k ∈ Finset.range j, + copyCount required (ω (historyIndex T k))*(1/s (k+1)-1/s k) + +lemma relativeTemplateResidual_eq {n : ℕ} {α : Type*} [Fintype α] + (required : α → Graph n) (s : ℕ → ℝ) (T j : ℕ) (hj : j ≤ T) + (ω : History (Graph n) T) : + relativeTemplateResidual required s T j ω = historyCounter + (fun k G => (pmfMean (step G) (copyCount required)-copyCount required G+ + triangleLiftCount required G/(triangles G).card)/s (k+1)) T j ω := by + have hid := history_relative_loss_counter (fun _ => step) (copyCount required) s T j hj ω + have hsum : historyCounter + (fun k G => (pmfMean (step G) (copyCount required)-copyCount required G+ + triangleLiftCount required G/(triangles G).card)/s (k+1)) T j ω = + historyCounter (fun k G => triangleLiftCount required G/ + ((triangles G).card*s (k+1))) T j ω - + historyCounter (fun k G => (copyCount required G- + pmfMean (step G) (copyCount required))/s (k+1)) T j ω := by + unfold historyCounter + rw [← Finset.sum_sub_distrib] + apply Finset.sum_congr rfl + intro k _ + simp only [div_eq_mul_inv,mul_inv_rev] + ring + rw [hsum,hid] + unfold relativeTemplateResidual + ring + +theorem history_relative_template_hierarchy {n : ℕ} {α : Type*} [Fintype α] + (required : α → Graph n) (b : ℕ) (hsize : ∀ a, (required a).card ≤ b) + (s : ℕ → ℝ) (T j : ℕ) (hj : j ≤ T) (hs : ∀ k < j, 0 < s (k+1)) + (ω : History (Graph n) T) : + 0 ≤ relativeTemplateResidual required s T j ω ∧ + relativeTemplateResidual required s T j ω ≤ historyCounter + (fun k G => ((b : ℝ)^2/(triangles G).card*copyCount required G)/s (k+1)) T j ω := by + rw [relativeTemplateResidual_eq required s T j hj] + constructor + · apply Finset.sum_nonneg + intro k hk + have hkj : k < j := by simpa only [Nat.min_eq_left hj,Finset.mem_range] using hk + exact div_nonneg (copyCount_triangleLift_drift_all required + (ω (historyIndex T k)) b hsize).1 (hs k hkj).le + · apply Finset.sum_le_sum + intro k hk + have hkj : k < j := by simpa only [Nat.min_eq_left hj,Finset.mem_range] using hk + exact div_le_div_of_nonneg_right (copyCount_triangleLift_drift_all required + (ω (historyIndex T k)) b hsize).2 (hs k hkj).le + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Stability/Volterra.lean b/lean/OAI/Combinatorics/TriangleRemoval/Stability/Volterra.lean new file mode 100644 index 0000000000000000000000000000000000000000..2313efed0a6bc88d4c967aa7646f95a1cb066193 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Stability/Volterra.lean @@ -0,0 +1,231 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory + +namespace SharpTerminalLeave + +theorem exp_neg_smul_perturbation {A : Type*} [NormedRing A] + [NormedAlgebra ℝ A] [CompleteSpace A] (P R : A) (s : ℝ) : + NormedSpace.exp ((-s) • (P + R)) = NormedSpace.exp ((-s) • P) - + ∫ t in (0 : ℝ)..s, + NormedSpace.exp ((t - s) • P) * R * NormedSpace.exp ((-t) • (P + R)) := by + let f : ℝ → A := fun t => + NormedSpace.exp ((t - s) • P) * NormedSpace.exp ((-t) • (P + R)) + let g : ℝ → A := fun t => + NormedSpace.exp ((t - s) • P) * R * NormedSpace.exp ((-t) • (P + R)) + have hd : ∀ t, HasDerivAt f (-g t) t := by + intro t + have h1 := (hasDerivAt_exp_smul_const (𝕂 := ℝ) P (t - s)).scomp t + ((hasDerivAt_id t).sub_const s) + have h2 := (hasDerivAt_exp_smul_const' (𝕂 := ℝ) (P + R) (-t)).scomp t + ((hasDerivAt_id t).neg) + have hh := h1.mul h2 + simp only [Function.comp_apply, id_eq, one_smul, neg_one_smul] at hh + convert hh using 1 + · rfl + · dsimp [g] + noncomm_ring + have hg : Continuous (fun t => -g t) := by + have hP := (differentiable_exp_smul_const ℝ P).continuous + have hA := (differentiable_exp_smul_const ℝ (P + R)).continuous + exact (((hP.comp (continuous_id.sub continuous_const)).mul continuous_const).mul + (hA.comp continuous_neg)).neg + have hi := intervalIntegral.integral_eq_sub_of_hasDerivAt + (a := 0) (b := s) (fun t _ => hd t) (hg.intervalIntegrable 0 s) + simp only [f, sub_self, zero_smul, NormedSpace.exp_zero, one_mul, + neg_zero, mul_one, zero_sub, intervalIntegral.integral_neg] at hi + change _ = _ - ∫ t in (0 : ℝ)..s, g t + rw [neg_eq_iff_eq_neg.mp hi] + abel + +section Volterra +variable {A : Type*} [NormedRing A] [NormedAlgebra ℝ A] [CompleteSpace A] + +noncomputable def volterra (P R : A) (f : ℝ → A) (s : ℝ) : A := + ∫ t in (0 : ℝ)..s, NormedSpace.exp ((t - s) • P) * R * f t + +noncomputable def volterraIter (P R : A) (f : ℝ → A) : ℕ → ℝ → A + | 0 => f + | k + 1 => volterra P R (volterraIter P R f k) + +theorem continuous_volterra_integrand (P R : A) {f : ℝ → A} + (hf : Continuous f) : + Continuous (fun q : ℝ × ℝ => NormedSpace.exp ((q.2 - q.1) • P) * R * f q.2) := + (((differentiable_exp_smul_const ℝ P).continuous.comp + (continuous_snd.sub continuous_fst)).mul continuous_const).mul (hf.comp continuous_snd) + +theorem continuous_volterra (P R : A) {f : ℝ → A} (hf : Continuous f) : + Continuous (volterra P R f) := + intervalIntegral.continuous_parametric_intervalIntegral_of_continuous + (continuous_volterra_integrand P R hf) continuous_id + +theorem continuous_volterraIter (P R : A) {f : ℝ → A} (hf : Continuous f) (k : ℕ) : + Continuous (volterraIter P R f k) := by + induction k with + | zero => exact hf + | succ k ih => exact continuous_volterra P R ih + +theorem volterra_sub (P R : A) {f g : ℝ → A} (hf : Continuous f) (hg : Continuous g) : + volterra P R (fun t => f t - g t) = fun s => volterra P R f s - volterra P R g s := by + funext s + simp only [volterra, mul_sub] + apply intervalIntegral.integral_sub + · exact ((continuous_volterra_integrand P R hf).comp + (continuous_const.prodMk continuous_id)).intervalIntegrable 0 s + · exact ((continuous_volterra_integrand P R hg).comp + (continuous_const.prodMk continuous_id)).intervalIntegrable 0 s + +theorem volterraIter_sub (P R : A) {f g : ℝ → A} (hf : Continuous f) (hg : Continuous g) + (k : ℕ) : volterraIter P R (fun t => f t - g t) k = + fun s => volterraIter P R f k s - volterraIter P R g k s := by + induction k with + | zero => rfl + | succ k ih => + simp only [volterraIter, ih] + exact volterra_sub P R (continuous_volterraIter P R hf k) + (continuous_volterraIter P R hg k) + +omit [CompleteSpace A] in +theorem volterraIter_shift (P R : A) (f : ℝ → A) (k : ℕ) : + volterraIter P R (volterra P R f) k = volterraIter P R f (k + 1) := by + induction k with + | zero => rfl + | succ k ih => simp only [volterraIter, ih] + +theorem perturbation_iteration (P R : A) (k : ℕ) : + volterraIter P R (fun t => NormedSpace.exp ((-t) • (P + R))) k = + fun s => volterraIter P R (fun t => NormedSpace.exp ((-t) • P)) k s - + volterraIter P R (fun t => NormedSpace.exp ((-t) • (P + R))) (k + 1) s := by + have h0 : Continuous (fun t : ℝ => NormedSpace.exp ((-t) • P)) := + (differentiable_exp_smul_const ℝ P).continuous.comp continuous_neg + have h1 : Continuous (fun t : ℝ => NormedSpace.exp ((-t) • (P + R))) := + (differentiable_exp_smul_const ℝ (P + R)).continuous.comp continuous_neg + have heq : (fun t : ℝ => NormedSpace.exp ((-t) • (P + R))) = + fun s => NormedSpace.exp ((-s) • P) - + volterra P R (fun t => NormedSpace.exp ((-t) • (P + R))) s := by + funext s + exact exp_neg_smul_perturbation P R s + conv_lhs => rw [heq] + rw [volterraIter_sub P R h0 (continuous_volterra P R h1), volterraIter_shift] + +theorem perturbation_finite_expansion (P R : A) (K : ℕ) (s : ℝ) : + NormedSpace.exp ((-s) • (P + R)) = + (∑ j ∈ Finset.range K, (-1 : ℝ) ^ j • + volterraIter P R (fun t => NormedSpace.exp ((-t) • P)) j s) + + (-1 : ℝ) ^ K • + volterraIter P R (fun t => NormedSpace.exp ((-t) • (P + R))) K s := by + induction K with + | zero => simp [volterraIter] + | succ K ih => + rw [ih, Finset.sum_range_succ] + have hi := congrFun (perturbation_iteration P R K) s + rw [hi, smul_sub, pow_succ] + simp only [mul_neg_one, neg_smul] + abel + +omit [CompleteSpace A] in + +theorem volterraIter_norm_le (P R : A) (f : ℝ → A) (T a b : ℝ) + (hT : 0 ≤ T) (ha : 0 ≤ a) (hb : 0 ≤ b) + (hP : ∀ t ∈ Set.Icc (0 : ℝ) T, ‖NormedSpace.exp ((-t) • P)‖ ≤ a) + (hf : ∀ t ∈ Set.Icc (0 : ℝ) T, ‖f t‖ ≤ b) + (k : ℕ) (s : ℝ) (hs : s ∈ Set.Icc (0 : ℝ) T) : + ‖volterraIter P R f k s‖ ≤ (a * ‖R‖ * T) ^ k * b := by + induction k generalizing s with + | zero => simpa [volterraIter] using hf s hs + | succ k ih => + have hn : 0 ≤ a * ‖R‖ * ((a * ‖R‖ * T) ^ k * b) := by positivity + have hi : ‖volterraIter P R f (k + 1) s‖ ≤ + (a * ‖R‖ * ((a * ‖R‖ * T) ^ k * b)) * |s - 0| := by + apply intervalIntegral.norm_integral_le_of_norm_le_const + intro t ht + rw [Set.uIoc_of_le hs.1] at ht + have hst : s - t ∈ Set.Icc (0 : ℝ) T := ⟨sub_nonneg.mpr ht.2, by linarith [ht.1, hs.2]⟩ + have htt : t ∈ Set.Icc (0 : ℝ) T := ⟨ht.1.le, ht.2.trans hs.2⟩ + have hPe : ‖NormedSpace.exp ((t - s) • P)‖ ≤ a := by + simpa only [neg_sub] using hP (s - t) hst + exact (norm_mul_le _ _).trans (mul_le_mul + ((norm_mul_le _ _).trans (mul_le_mul_of_nonneg_right hPe (norm_nonneg R))) + (ih t htt) (norm_nonneg _) (mul_nonneg ha (norm_nonneg R))) + calc + _ ≤ (a * ‖R‖ * ((a * ‖R‖ * T) ^ k * b)) * s := by + simpa only [sub_zero, abs_of_nonneg hs.1] using hi + _ ≤ (a * ‖R‖ * ((a * ‖R‖ * T) ^ k * b)) * T := + mul_le_mul_of_nonneg_left hs.2 hn + _ = (a * ‖R‖ * T) ^ (k + 1) * b := by rw [pow_succ]; ring + +variable {B : Type*} [NormedRing B] [NormedAlgebra ℝ B] [CompleteSpace B] + +omit [CompleteSpace B] in +theorem continuous_algHom_map_exp (F : A →ₐ[ℝ] B) (hF : Continuous F) (x : A) : + F (NormedSpace.exp x) = NormedSpace.exp (F x) := by + apply NormedSpace.map_exp_of_mem_ball (𝕂 := ℝ) F hF + rw [NormedSpace.expSeries_radius_eq_top] + exact edist_lt_top _ _ + +theorem map_volterraIter (F : A →ₐ[ℝ] B) (hF : Continuous F) (P R : A) + {f : ℝ → A} (hf : Continuous f) (k : ℕ) (s : ℝ) : + F (volterraIter P R f k s) = + volterraIter (F P) (F R) (fun t => F (f t)) k s := by + let L : A →L[ℝ] B := ⟨F.toLinearMap, hF⟩ + induction k generalizing s with + | zero => rfl + | succ k ih => + change L (∫ t in (0 : ℝ)..s, + NormedSpace.exp ((t - s) • P) * R * volterraIter P R f k t) = _ + have hint : IntervalIntegrable (fun t => + NormedSpace.exp ((t - s) • P) * R * volterraIter P R f k t) volume 0 s := + ((continuous_volterra_integrand P R (continuous_volterraIter P R hf k)).comp + (continuous_const.prodMk continuous_id)).intervalIntegrable 0 s + rw [← L.intervalIntegral_comp_comm hint] + change (∫ t in (0 : ℝ)..s, + F (NormedSpace.exp ((t - s) • P) * R * volterraIter P R f k t)) = _ + simp only [map_mul, continuous_algHom_map_exp F hF, map_smul, ih] + rfl + +theorem perturbation_mixed_norm_bound (F : A →ₐ[ℝ] B) (hF : Continuous F) + (P R : A) (T a b N : ℝ) (hT : 0 ≤ T) (ha : 0 ≤ a) (hb : 0 ≤ b) (hN : 0 ≤ N) + (hcompare : ∀ x : A, ‖x‖ ≤ N * ‖F x‖) + (hbase : ∀ t ∈ Set.Icc (0 : ℝ) T, ‖NormedSpace.exp ((-t) • P)‖ ≤ a) + (hbase₂ : ∀ t ∈ Set.Icc (0 : ℝ) T, ‖NormedSpace.exp ((-t) • F P)‖ ≤ 1) + (hfull₂ : ∀ t ∈ Set.Icc (0 : ℝ) T, ‖NormedSpace.exp ((-t) • (F P + F R))‖ ≤ b) + (K : ℕ) (s : ℝ) (hs : s ∈ Set.Icc (0 : ℝ) T) : + ‖NormedSpace.exp ((-s) • (P + R))‖ ≤ + (∑ j ∈ Finset.range K, (a * ‖R‖ * T) ^ j * a) + + N * ((‖F R‖ * T) ^ K * b) := by + have hc : Continuous (fun t : ℝ => NormedSpace.exp ((-t) • (P + R))) := + (differentiable_exp_smul_const ℝ (P + R)).continuous.comp continuous_neg + have hr : ‖volterraIter P R (fun t => NormedSpace.exp ((-t) • (P + R))) K s‖ ≤ + N * ((‖F R‖ * T) ^ K * b) := by + apply (hcompare _).trans + apply mul_le_mul_of_nonneg_left _ hN + rw [map_volterraIter F hF P R hc] + simp_rw [continuous_algHom_map_exp F hF, map_smul, map_add] + simpa only [one_mul] using + volterraIter_norm_le (F P) (F R) (fun t => NormedSpace.exp ((-t) • (F P + F R))) + T 1 b hT zero_le_one hb hbase₂ hfull₂ K s hs + rw [perturbation_finite_expansion P R K s] + calc + _ ≤ (∑ j ∈ Finset.range K, + ‖(-1 : ℝ) ^ j • volterraIter P R (fun t => NormedSpace.exp ((-t) • P)) j s‖) + + ‖(-1 : ℝ) ^ K • volterraIter P R (fun t => NormedSpace.exp ((-t) • (P + R))) K s‖ := + (norm_add_le _ _).trans (add_le_add (norm_sum_le _ _) (le_refl _)) + _ = (∑ j ∈ Finset.range K, + ‖volterraIter P R (fun t => NormedSpace.exp ((-t) • P)) j s‖) + + ‖volterraIter P R (fun t => NormedSpace.exp ((-t) • (P + R))) K s‖ := by + simp only [norm_smul, norm_pow, norm_neg, norm_one, one_pow, one_mul] + _ ≤ _ := by + apply add_le_add _ hr + exact Finset.sum_le_sum fun j _ => volterraIter_norm_le P R _ T a a hT ha ha + hbase hbase j s hs + +end Volterra +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/BalancedJump.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/BalancedJump.lean new file mode 100644 index 0000000000000000000000000000000000000000..b4456cce095270b594ae15fa51bc32a0f63df475 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/BalancedJump.lean @@ -0,0 +1,205 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.RootedTemplateEdgesCard +import OAI.Combinatorics.TriangleRemoval.Process.RelativeNoiseExit +import OAI.Combinatorics.TriangleRemoval.Tracking.CodegreeVertexCount +import OAI.Combinatorics.TriangleRemoval.Tracking.CodegreeNoiseRadius +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixEdgeRadius + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +lemma earlyTemplateScale_uniform_regular (H : ℕ) : ∀ᶠ n : ℕ in atTop, + ∀ a ≤ H, ∀ b ≤ H^2, + (∀ i ≤ prefixTime n, 0 < earlyTemplateScale a b n i) ∧ + (∀ i < prefixTime n, earlyTemplateScale a b n (i+1) ≤ earlyTemplateScale a b n i) ∧ + (∀ i < prefixTime n, earlyTemplateScale a b n i ≤ 2*earlyTemplateScale a b n (i+1)) ∧ + relativeScaleBudget (earlyTemplateScale a b n) (prefixTime n) ≤ 2*(b : ℝ)*Real.log n := by + have hh : ∀ z : Fin (H+1) × Fin (H^2+1), ∀ᶠ n : ℕ in atTop, + (∀ i ≤ prefixTime n, 0 < earlyTemplateScale z.1.val z.2.val n i) ∧ + (∀ i < prefixTime n, earlyTemplateScale z.1.val z.2.val n (i+1) ≤ earlyTemplateScale z.1.val z.2.val n i) ∧ + (∀ i < prefixTime n, earlyTemplateScale z.1.val z.2.val n i ≤ 2*earlyTemplateScale z.1.val z.2.val n (i+1)) ∧ + relativeScaleBudget (earlyTemplateScale z.1.val z.2.val n) (prefixTime n) ≤ 2*(z.2.val : ℝ)*Real.log n := by + intro z + filter_upwards [earlyTemplateScale_regular z.1.val z.2.val, + earlyTemplateScale_log_budget z.1.val z.2.val] with n hr hb + exact ⟨hr.1,hr.2.1,hr.2.2,hb⟩ + filter_upwards [Filter.eventually_all.mpr hh] with n hn + intro a ha b hb + exact hn (⟨a,by omega⟩,⟨b,by omega⟩) + +noncomputable def balancedJump (n : ℕ) : ℝ := (n : ℝ)^(-1/2000 : ℝ) +noncomputable def balancedLoad (H : ℕ) {k : ℕ} (T : RootedTemplate k) (n i : ℕ) : ℝ := + 4*(T.edges.card : ℝ)*(1+Real.log (n : ℝ))^(3*H+2)* + rootedScaling T n (earlyDensity n i)/((n : ℝ)*earlyDensity n i^2) + +lemma balanced_jump_budget (H : ℕ) : ∀ᶠ n : ℕ in atTop, + 24*(H : ℝ)^2*(1+Real.log (n : ℝ))^(3*H+2)/prefixD n ≤ balancedJump n := by + have hc : 0 < 1/(24*(H : ℝ)^2+1) := by positivity + filter_upwards [prefixTemplateFactor_subpower ((3*H+2 : ℕ) : ℝ) (by norm_num : (0 : ℝ) < 1/2000) hc, + prefixD_eventual_envelope,eventually_ge_atTop (1 : ℕ)] with n hb hd hn + have hn0 : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hln : 0 ≤ 1+Real.log (n : ℝ) := by have := Real.log_nonneg hn1; linarith + have hb' : (1+Real.log (n : ℝ))^(3*H+2 : ℕ) ≤ + 1/(24*(H : ℝ)^2+1)*(n : ℝ)^(1/2000 : ℝ) := by + simpa only [prefixTemplateFactor,Real.rpow_natCast] using hb + have hcoef : 24*(H : ℝ)^2/(24*(H : ℝ)^2+1) ≤ 1 := by + apply (div_le_one (by positivity)).mpr; linarith + have hnum : 24*(H : ℝ)^2*(1+Real.log (n : ℝ))^(3*H+2) ≤ (n : ℝ)^(1/2000 : ℝ) := by + have hh := mul_le_mul_of_nonneg_left hb' (show 0 ≤ 24*(H : ℝ)^2 by positivity) + have he := mul_le_mul_of_nonneg_right hcoef (Real.rpow_nonneg hn0.le (1/2000 : ℝ)) + ring_nf at hh he ⊢ + exact hh.trans he + calc + _ ≤ (n : ℝ)^(1/2000 : ℝ)/(n : ℝ)^(1/1000 : ℝ) := + div_le_div₀ (by positivity) hnum (by positivity) hd.1 + _ = _ := by unfold balancedJump; rw [← Real.rpow_sub hn0]; norm_num + +lemma rooted_initial_ratio_le_two (H : ℕ) : ∀ᶠ n : ℕ in atTop, + ∀ k ≤ H, ∀ T : RootedTemplate k, ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + rootedCount T ψ (completeGraph n)/rootedScaling T n (earlyDensity n 0) ≤ 2 := by + let C : ℝ := (2 : ℝ)^(H^2)*(2*(H : ℝ)^2) + filter_upwards [tendsto_natCast_atTop_atTop.eventually_ge_atTop C, + eventually_ge_atTop H,eventually_ge_atTop (2 : ℕ)] with n hC hn hn2 + intro k hk T ψ + have hh := rootedCount_initial_uniform_error H hk hn hn2 T ψ + have hnR : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hdiv : C/(n : ℝ) ≤ 1 := (div_le_one hnR).mpr hC + have he : earlyDensity n 0 = 1-1/(n : ℝ) := by simp [earlyDensity] + rw [he] + have hp := (abs_le.mp hh).2 + change _ ≤ C/(n : ℝ) at hp + linarith only [hp,hdiv] + +lemma balanced_noise_exponent_budget (H : ℕ) : ∀ᶠ n : ℕ in atTop, + (Real.log (n : ℝ))^2/4 ≤ codegreeNoiseRadius n^2/ + (4*(balancedJump n*(2+4*(H : ℝ)^2*Real.log n+codegreeNoiseRadius n+balancedJump n)+ + balancedJump n*codegreeNoiseRadius n)) := by + let C : ℝ := 20+16*(H : ℝ)^2 + have hC : 0 < C := by dsimp [C]; positivity + filter_upwards [prefixTemplateFactor_subpower 1 (by norm_num : (0 : ℝ) < 1/10000) (one_div_pos.mpr hC), + prefixTemplateFactor_subpower 2 (by norm_num : (0 : ℝ) < 1/5000) (by norm_num : (0 : ℝ) < 4), + eventually_ge_atTop (1 : ℕ)] with n hp hl hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hlog : 0 ≤ Real.log (n : ℝ) := Real.log_nonneg hn1 + have hr : 0 < codegreeNoiseRadius n := Real.rpow_pos_of_pos hn0 _ + have hc : 0 < balancedJump n := Real.rpow_pos_of_pos hn0 _ + have hr1 : codegreeNoiseRadius n ≤ 1 := Real.rpow_le_one_of_one_le_of_nonpos hn1 (by norm_num) + have hc1 : balancedJump n ≤ 1 := Real.rpow_le_one_of_one_le_of_nonpos hn1 (by norm_num) + let d := 4*(balancedJump n*(2+4*(H : ℝ)^2*Real.log n+codegreeNoiseRadius n+balancedJump n)+ + balancedJump n*codegreeNoiseRadius n) + have hdpos : 0 < d := by dsimp [d]; positivity + have hd : d ≤ (n : ℝ)^(-1/2500 : ℝ) := by + have hp' : 1+Real.log (n : ℝ) ≤ (1/C)*(n : ℝ)^(1/10000 : ℝ) := by + simpa only [prefixTemplateFactor,Real.rpow_one] using hp + have hh := mul_le_mul_of_nonneg_left hp' hC.le + simp only [← mul_assoc,mul_one_div_cancel hC.ne',one_mul] at hh + have hbracket : 4*(2+4*(H : ℝ)^2*Real.log n+codegreeNoiseRadius n+balancedJump n+codegreeNoiseRadius n) ≤ + C*(1+Real.log (n : ℝ)) := by dsimp [C]; nlinarith only [hr1,hc1,hlog,sq_nonneg (H : ℝ)] + have hm := mul_le_mul_of_nonneg_left (hbracket.trans hh) hc.le + have he : balancedJump n*(n : ℝ)^(1/10000 : ℝ) = (n : ℝ)^(-1/2500 : ℝ) := by + unfold balancedJump; rw [← Real.rpow_add hn0]; norm_num + rw [he] at hm + dsimp only [d] + nlinarith only [hm] + have hr2 : codegreeNoiseRadius n^2 = (n : ℝ)^(-1/5000 : ℝ) := by + unfold codegreeNoiseRadius + rw [← Real.rpow_mul_natCast hn0.le]; norm_num + have hratio : (n : ℝ)^(1/5000 : ℝ) ≤ codegreeNoiseRadius n^2/d := by + apply (le_div_iff₀ hdpos).mpr + have hh := mul_le_mul_of_nonneg_left hd (Real.rpow_nonneg hn0.le (1/5000 : ℝ)) + rw [← Real.rpow_add hn0] at hh + rw [hr2] + convert hh using 1 + norm_num + have hl' : (1+Real.log (n : ℝ))^2 ≤ 4*(n : ℝ)^(1/5000 : ℝ) := by + simpa only [prefixTemplateFactor,Real.rpow_two] using hl + exact (show (Real.log (n : ℝ))^2/4 ≤ (n : ℝ)^(1/5000 : ℝ) by nlinarith only [hl',hlog]).trans hratio + +theorem prefix_balanced_noise_single (H : ℕ) : ∀ᶠ n : ℕ in atTop, + ∀ k ≤ H, ∀ T : RootedTemplate k, ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => if RelativeNoiseExit (fun φ : RootedInjection T ψ => imageEdges T φ.val) + (balancedLoad H T n) (earlyTemplateScale (k-T.roots.card) T.edges.card n) 2 + (codegreeNoiseRadius n) (prefixTime n) ω then 1 else 0) ≤ + 2*(prefixTime n+1 : ℝ)*Real.exp (-(Real.log (n : ℝ))^2/4) := by + filter_upwards [earlyTemplateScale_uniform_regular H,balanced_jump_budget H, + rooted_initial_ratio_le_two H,balanced_noise_exponent_budget H, + prefixDensity_eventually_inverse_lower,eventually_ge_atTop (1 : ℕ)] with n hreg hjump hinit hexp hp hn + intro k hk T ψ + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hln : 0 ≤ 1+Real.log (n : ℝ) := by have := Real.log_nonneg hn1; linarith + have hb : T.edges.card ≤ H^2 := (rootedTemplate_edges_card_le_sq T).trans (Nat.pow_le_pow_left hk 2) + have hbR : (T.edges.card : ℝ) ≤ (H : ℝ)^2 := by exact_mod_cast hb + have hr := hreg (k-T.roots.card) ((Nat.sub_le _ _).trans hk) T.edges.card hb + let s := earlyTemplateScale (k-T.roots.card) T.edges.card n + have hD : 0 < prefixD n := by unfold prefixD; have hh : 0 < prefixDensity n := lt_of_lt_of_le (by positivity) hp; positivity + have hc : 0 < balancedJump n := Real.rpow_pos_of_pos hn0 _ + have hnoise : 0 < codegreeNoiseRadius n := Real.rpow_pos_of_pos hn0 _ + have hL : ∀ i < prefixTime n, 0 ≤ balancedLoad H T n i := by + intro i hi + have hpi := early_density_positive (by omega) hp hi.le + unfold balancedLoad rootedScaling + positivity + have hJ : ∀ i < prefixTime n, 3*balancedLoad H T n i/s (i+1) ≤ balancedJump n := by + intro i hi + have hsi := hr.1 (i+1) (by omega) + have hpi := early_density_positive (by omega) hp hi.le + have hDi : prefixD n ≤ (n : ℝ)*earlyDensity n i^2 := by + simpa only [earlyTemplateScale,pow_one] using earlyCodegreeScale_ge_prefixD n i hi.le (le_trans (by positivity) hp) + have hratio := hr.2.2.1 i hi + have hcoeff : 0 ≤ 12*(T.edges.card : ℝ)*(1+Real.log (n : ℝ))^(3*H+2)/((n : ℝ)*earlyDensity n i^2) := by positivity + have hm := mul_le_mul_of_nonneg_left hratio hcoeff + have hstep : 3*balancedLoad H T n i/s (i+1) ≤ + 24*(T.edges.card : ℝ)*(1+Real.log (n : ℝ))^(3*H+2)/((n : ℝ)*earlyDensity n i^2) := by + apply (div_le_iff₀ hsi).mpr + dsimp only [balancedLoad,rootedScaling,s,earlyTemplateScale] at hm ⊢ + convert hm using 1 <;> ring + apply hstep.trans (_root_.le_trans _ hjump) + apply div_le_div₀ (by positivity) _ hD hDi + exact mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hbR (by norm_num)) (by positivity) + have ht := triangle_relative_template_self_bounded_tail + (fun φ : RootedInjection T ψ => imageEdges T φ.val) (completeGraph n) (prefixTime n) + (balancedLoad H T n) s (balancedJump n) 2 hc (by norm_num) hr.1 hr.2.1 hL hJ + (codegreeNoiseRadius n) hnoise + refine (show _ ≤ 2*(prefixTime n+1 : ℝ)*Real.exp + (-codegreeNoiseRadius n^2/(4*(balancedJump n*(copyCount (fun φ : RootedInjection T ψ => imageEdges T φ.val) + (completeGraph n)/s 0+2*relativeScaleBudget s (prefixTime n)+codegreeNoiseRadius n+balancedJump n)+ + balancedJump n*codegreeNoiseRadius n))) from ?_).trans ?_ + · convert ht using 1 + apply congrArg (pmfMean _) + funext ω + unfold RelativeNoiseExit + split_ifs <;> rfl + apply mul_le_mul_of_nonneg_left _ (by positivity) + apply Real.exp_le_exp.mpr + rw [neg_div,neg_div] + apply neg_le_neg + apply hexp.trans + apply div_le_div_of_nonneg_left (sq_nonneg _) + · have hscale : 0 ≤ copyCount (fun φ : RootedInjection T ψ => imageEdges T φ.val) (completeGraph n)/s 0 := + div_nonneg (copyCount_nonneg _ _) (hr.1 0 (Nat.zero_le _)).le + have hbudget := relativeScaleBudget_nonneg s (prefixTime n) hr.1 hr.2.1 + positivity + · have hi := hinit k hk T ψ + change copyCount _ (completeGraph n)/s 0 ≤ 2 at hi + have hbgt : relativeScaleBudget s (prefixTime n) ≤ 2*(H : ℝ)^2*Real.log n := + hr.2.2.2.trans (by nlinarith only [mul_le_mul_of_nonneg_right hbR (Real.log_nonneg hn1)]) + have hh := mul_le_mul_of_nonneg_left (show copyCount (fun φ : RootedInjection T ψ => imageEdges T φ.val) (completeGraph n)/s 0+ + 2*relativeScaleBudget s (prefixTime n)+codegreeNoiseRadius n+balancedJump n ≤ + 2+4*(H : ℝ)^2*Real.log n+codegreeNoiseRadius n+balancedJump n by linarith only [hi,hbgt]) hc.le + linarith only [hh] + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CenteredCodegreeNeighborSum.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CenteredCodegreeNeighborSum.lean new file mode 100644 index 0000000000000000000000000000000000000000..e45b49635b1e19c3de31cd008d4e0066e64ba66b --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CenteredCodegreeNeighborSum.lean @@ -0,0 +1,72 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.HistoryCompensator + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def centeredCodegreeNeighborSum {n : ℕ} (G : Graph n) + (u v : Fin n) (s : ℝ) : ℝ := + ∑ w ∈ commonNeighbors G u v, + ((currentCodegree G u w : ℝ)+currentCodegree G v w-2*s) + +lemma codegree_hazard_centering {n : ℕ} (G : Graph n) (u v : Fin n) (s : ℝ) : + (∑ w ∈ commonNeighbors G u v, + ((currentCodegree G u w : ℝ)+currentCodegree G v w-if {u,v} ∈ G then 1 else 0)) = + (2*s-if {u,v} ∈ G then 1 else 0)*(currentCodegree G u v : ℝ)+ + centeredCodegreeNeighborSum G u v s := by + classical + have he : (∑ w ∈ commonNeighbors G u v, + ((currentCodegree G u w : ℝ)+currentCodegree G v w-if {u,v} ∈ G then 1 else 0)) = + (∑ _w ∈ commonNeighbors G u v, (2*s-if {u,v} ∈ G then 1 else 0))+ + centeredCodegreeNeighborSum G u v s := by + rw [centeredCodegreeNeighborSum,← Finset.sum_add_distrib] + apply Finset.sum_congr rfl + intro w _ + ring + rw [he] + simp only [Finset.sum_const,nsmul_eq_mul,currentCodegree] + ring + +theorem normalized_codegree_centered_drift {n : ℕ} {G : Graph n} + (hG : G ⊆ completeGraph n) (u v : Fin n) (huv : u ≠ v) (s t : ℝ) : + pmfMean (step G) (fun H => (currentCodegree H u v : ℝ)/t)- + (currentCodegree G u v : ℝ)/s = + (currentCodegree G u v : ℝ)*(1/t-1/s)- + ((2*s-if {u,v} ∈ G then 1 else 0)*(currentCodegree G u v : ℝ)+ + centeredCodegreeNeighborSum G u v s)/((triangles G).card*t) := by + by_cases hQ : (triangles G).Nonempty + · have hd := step_mean_codegree hG hQ u v huv + rw [codegree_hazard_centering G u v s] at hd + simp only [div_eq_mul_inv,pmfMean_mul_const] at hd ⊢ + rw [hd,mul_inv_rev] + ring + · have hzero : triangles G = ∅ := Finset.not_nonempty_iff_eq_empty.mp hQ + rw [step,dite_eq_right hQ,pmfMean_pure,hzero,Finset.card_empty,Nat.cast_zero,zero_mul,div_zero] + ring + +theorem prefix_codegree_centered_drift {n : ℕ} {G : Graph n} + (hG : G ⊆ completeGraph n) (u v : Fin n) (huv : u ≠ v) (k : ℕ) : + pmfMean (step G) (prefixNormalizedCodegree u v (k+1))- + prefixNormalizedCodegree u v k G = + (currentCodegree G u v : ℝ)*(1/earlyTemplateScale 1 2 n (k+1)-1/earlyTemplateScale 1 2 n k)- + ((2*earlyTemplateScale 1 2 n k-if {u,v} ∈ G then 1 else 0)* + (currentCodegree G u v : ℝ)+ + centeredCodegreeNeighborSum G u v (earlyTemplateScale 1 2 n k))/ + ((triangles G).card*earlyTemplateScale 1 2 n (k+1)) := + normalized_codegree_centered_drift hG u v huv _ _ + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeNoiseRadius.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeNoiseRadius.lean new file mode 100644 index 0000000000000000000000000000000000000000..bdcdadf1443d9165e1beee33f16d40369e7db9ef --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeNoiseRadius.lean @@ -0,0 +1,79 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixDTendstoTop + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def codegreeNoiseRadius (n : ℕ) : ℝ := (n : ℝ)^(-1/10000 : ℝ) +noncomputable def codegreeNoiseDenominator (n : ℕ) : ℝ := + 4*((6/prefixD n)*(4+8*Real.log n+codegreeNoiseRadius n+6/prefixD n)+ + (6/prefixD n)*codegreeNoiseRadius n) + +theorem codegree_noise_exponent_lower : ∀ᶠ n : ℕ in atTop, + (n : ℝ)^(1/2500 : ℝ) ≤ codegreeNoiseRadius n^2/codegreeNoiseDenominator n := by + have hlog := ((isLittleO_log_rpow_atTop (by norm_num : (0 : ℝ) < 1/2500)).comp_tendsto + tendsto_natCast_atTop_atTop).bound (by norm_num : (0 : ℝ) < 1/384) + have hpow : Tendsto (fun n : ℕ => (n : ℝ)^(1/2500 : ℝ)) atTop atTop := + (tendsto_rpow_atTop (by norm_num : (0 : ℝ) < 1/2500)).comp tendsto_natCast_atTop_atTop + filter_upwards [prefixD_eventual_envelope,prefixD_tendsto_atTop.eventually_ge_atTop 6, + hlog,hpow.eventually_ge_atTop 336,eventually_ge_atTop (1 : ℕ)] with n hD hD6 hl hn336 hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hD0 : 0 < prefixD n := by linarith + have hln : 0 ≤ Real.log (n : ℝ) := Real.log_nonneg hn1 + have hc0 : 0 < 6/prefixD n := by positivity + have hc1 : 6/prefixD n ≤ 1 := (div_le_one hD0).mpr hD6 + have hr0 : 0 < codegreeNoiseRadius n := Real.rpow_pos_of_pos hn0 _ + have hr1 : codegreeNoiseRadius n ≤ 1 := + Real.rpow_le_one_of_one_le_of_nonpos hn1 (by norm_num) + have hc : 6/prefixD n ≤ 6*(n : ℝ)^(-1/1000 : ℝ) := by + have hh := div_le_div_of_nonneg_left (by norm_num : (0 : ℝ) ≤ 6) + (Real.rpow_pos_of_pos hn0 (1/1000 : ℝ)) hD.1 + calc + _ ≤ 6/(n : ℝ)^(1/1000 : ℝ) := hh + _ = _ := by + rw [show (-1/1000 : ℝ) = -(1/1000 : ℝ) by norm_num,Real.rpow_neg hn0.le] + rfl + have hr2 : codegreeNoiseRadius n^2 = (n : ℝ)^(-1/5000 : ℝ) := by + unfold codegreeNoiseRadius + rw [← Real.rpow_natCast,← Real.rpow_mul hn0.le] + norm_num + have hd0 : 0 < codegreeNoiseDenominator n := by unfold codegreeNoiseDenominator; positivity + have hd : codegreeNoiseDenominator n ≤ (n : ℝ)^(-1/1000 : ℝ)*(24*(7+8*Real.log n)) := by + unfold codegreeNoiseDenominator + calc + _ = 4*(6/prefixD n)*(4+8*Real.log n+codegreeNoiseRadius n+6/prefixD n+codegreeNoiseRadius n) := by ring + _ ≤ 4*(6/prefixD n)*(7+8*Real.log n) := by + apply mul_le_mul_of_nonneg_left _ (by positivity) + linarith only [hr1,hc1] + _ ≤ 4*(6*(n : ℝ)^(-1/1000 : ℝ))*(7+8*Real.log n) := by + exact mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left hc (by norm_num)) + (by positivity) + _ = _ := by ring + have hsmall : 24*(7+8*Real.log (n : ℝ)) ≤ (n : ℝ)^(1/2500 : ℝ) := by + dsimp at hl + rw [abs_of_nonneg hln,abs_of_nonneg (Real.rpow_nonneg hn0.le (1/2500 : ℝ))] at hl + linarith only [hl,hn336] + have hd' := hd.trans (mul_le_mul_of_nonneg_left hsmall + (Real.rpow_nonneg hn0.le (-1/1000 : ℝ))) + apply (le_div_iff₀ hd0).mpr + have hh := mul_le_mul_of_nonneg_left hd' (Real.rpow_nonneg hn0.le (1/2500 : ℝ)) + rw [← Real.rpow_add hn0,← Real.rpow_add hn0] at hh + rw [hr2] + convert hh using 1; norm_num + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeRelativeNoiseTail.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeRelativeNoiseTail.lean new file mode 100644 index 0000000000000000000000000000000000000000..f8a86d672896f697a49c427bbe3a64d8fef28550 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeRelativeNoiseTail.lean @@ -0,0 +1,52 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.RelativeCopyNoiseFirst +import OAI.Combinatorics.TriangleRemoval.Embeddings.FixedEndpointPairInjective + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +open Classical in + +theorem codegree_relative_noise_tail {n : ℕ} (u v : Fin n) (G : Graph n) + (T : ℕ) (s : ℕ → ℝ) (c B : ℝ) (hc : 0 < c) (hB : 0 ≤ B) + (hs : ∀ k ≤ T, 0 < s k) (hdec : ∀ k < T, s (k+1) ≤ s k) + (hjump : ∀ k < T, 6/s (k+1) ≤ c) (r : ℝ) (hr : 0 < r) : + pmfMean (historyLaw (PMF.pure G) (fun _ => step) T T) + (fun ω => if ∃ j ≤ T, + (∀ k < j, (currentCodegree (ω (historyIndex T k)) u v : ℝ) ≤ B*s k) ∧ + r ≤ |historyNoise (fun _ => step) + (fun k H => (currentCodegree H u v : ℝ)/s k) T j ω| then 1 else 0) ≤ + 2*(T+1 : ℝ)*Real.exp + (-r^2/(4*(c*((currentCodegree G u v : ℝ)/s 0+B*relativeScaleBudget s T+r+c)+c*r))) := by + have ht := triangle_relative_template_self_bounded_tail (wedgeEdges u v) G T + (fun _ => 2) s c B hc hB hs hdec (fun _ _ => by norm_num) + (fun k hk => by simpa only [show (3 : ℝ)*2 = 6 by norm_num] using hjump k hk) r hr + simp_rw [← currentCodegree_copyCount] at ht + apply le_trans _ ht + apply pmfMean_mono + intro ω _ + by_cases hex : ∃ j ≤ T, + (∀ k < j, (currentCodegree (ω (historyIndex T k)) u v : ℝ) ≤ B*s k) ∧ + r ≤ |historyNoise (fun _ => step) + (fun k H => (currentCodegree H u v : ℝ)/s k) T j ω| + · obtain ⟨j,hj,hcounts,hcross⟩ := hex + rw [ite_eq_left ⟨j,hj,hcounts,hcross⟩, + ite_eq_left ⟨j,hj,wedge_pastRelativeCopyLoadsSafe u v T j ω,hcounts,hcross⟩] + · rw [ite_eq_right hex] + split <;> norm_num + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeVarianceRate.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeVarianceRate.lean new file mode 100644 index 0000000000000000000000000000000000000000..cb1e3c05dcea477e4320ebb5e26301165c80e5b0 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeVarianceRate.lean @@ -0,0 +1,89 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.PowersetCardTriple +import OAI.Combinatorics.TriangleRemoval.Process.History + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter MeasureTheory +open scoped BigOperators ENNReal Classical +open scoped BigOperators +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +theorem pmfMean_centered_sq_le {α : Type*} [Fintype α] + (p : PMF α) (f : α → ℝ) (a : ℝ) : + pmfMean p (fun b => (f b - pmfMean p f)^2) ≤ + pmfMean p (fun b => (f b - a)^2) := by + rw [pmfMean_sq_sub,pmfMean_sq_sub] + nlinarith [sq_nonneg (pmfMean p f - a)] + +theorem codegree_centered_range {n : ℕ} {G H : Graph n} + (hG : G ⊆ completeGraph n) (u v : Fin n) (hH : H ∈ (step G).support) : + |(currentCodegree H u v : ℝ) - + pmfMean (step G) (fun J => (currentCodegree J u v : ℝ))| ≤ 2 := by + have hmlo : (currentCodegree G u v : ℝ) - 2 ≤ + pmfMean (step G) (fun J => (currentCodegree J u v : ℝ)) := by + rw [← pmfMean_const (step G) ((currentCodegree G u v : ℝ) - 2)] + apply pmfMean_mono + intro J hJ + have hJ' := (codegree_step_range hG u v hJ).2 + linarith + have hmhi : pmfMean (step G) (fun J => (currentCodegree J u v : ℝ)) ≤ + (currentCodegree G u v : ℝ) := by + rw [← pmfMean_const (step G) (currentCodegree G u v : ℝ)] + apply pmfMean_mono + intro J hJ + have hJ' := (codegree_step_range hG u v hJ).1 + linarith + obtain ⟨hlo,hhi⟩ := codegree_step_range hG u v hH + exact abs_le.mpr ⟨by linarith,by linarith⟩ + +theorem codegree_centered_variance {n : ℕ} {G : Graph n} + (hG : G ⊆ completeGraph n) (u v : Fin n) : + pmfMean (step G) (fun H => ((currentCodegree H u v : ℝ) - + pmfMean (step G) (fun J => (currentCodegree J u v : ℝ)))^2) ≤ + 2 * ((currentCodegree G u v : ℝ) - + pmfMean (step G) (fun H => (currentCodegree H u v : ℝ))) := by + exact (pmfMean_centered_sq_le _ _ (currentCodegree G u v : ℝ)).trans + (codegree_step_second_moment hG u v) + +@[simp] theorem markovLaw_triangle_step {n : ℕ} (G : Graph n) (k : ℕ) : + markovLaw (PMF.pure G) (fun _ => step) k = evolve G k := by + induction k with + | zero => rfl + | succ k ih => rw [markovLaw,evolve,ih] + +noncomputable def codegreeVarianceRate {n : ℕ} (u v : Fin n) (_k : ℕ) (G : Graph n) : ℝ := + 2 * ((currentCodegree G u v : ℝ) - + pmfMean (step G) (fun H => (currentCodegree H u v : ℝ))) + +theorem triangle_codegree_noise_freedman {n : ℕ} (G : Graph n) + (hG : G ⊆ completeGraph n) (u v : Fin n) (T k : ℕ) + (r V : ℝ) (hr : 0 < r) (hV : 0 ≤ V) : + pmfMean (historyLaw (PMF.pure G) (fun _ => step) T k) + (fun ω => if r ≤ historyNoise (fun _ => step) + (fun _ H => (currentCodegree H u v : ℝ)) T k ω ∧ + historyCounter (codegreeVarianceRate u v) T k ω ≤ V then 1 else 0) ≤ + Real.exp (-r ^ 2 / (4 * (V + 2 * r))) := by + apply history_noise_freedman (PMF.pure G) (fun _ => step) + (fun _ H => (currentCodegree H u v : ℝ)) (codegreeVarianceRate u v) T 2 + (by norm_num) + · intro j _ H hH J hJ + rw [markovLaw_triangle_step] at hH + exact codegree_centered_range ((evolve_support_subset G j hH).trans hG) u v hJ + · intro j _ H hH + rw [markovLaw_triangle_step] at hH + exact codegree_centered_variance ((evolve_support_subset G j hH).trans hG) u v + · exact hr + · exact hV + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeVertexCount.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeVertexCount.lean new file mode 100644 index 0000000000000000000000000000000000000000..41824ee5942662cb8f4262f892ece4089b3c0f5d --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/CodegreeVertexCount.lean @@ -0,0 +1,103 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Asymptotics.EarlyDensity +import OAI.Combinatorics.TriangleRemoval.Tracking.CodegreeRelativeNoiseTail + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma codegree_le_vertex_count {n : ℕ} (G : Graph n) (u v : Fin n) : + currentCodegree G u v ≤ n := by + unfold currentCodegree + exact (Finset.card_le_univ _).trans (by simp) + +lemma earlyCodegreeScale_ge_prefixD (n i : ℕ) (hi : i ≤ prefixTime n) + (hp : 0 ≤ prefixDensity n) : prefixD n ≤ earlyTemplateScale 1 2 n i := by + unfold earlyTemplateScale prefixD + rw [pow_one] + exact mul_le_mul_of_nonneg_left + (pow_le_pow_left₀ hp (earlyDensity_antitone n hi) 2) (Nat.cast_nonneg n) + +lemma initial_codegree_relative_le_four {n : ℕ} (hn : 2 ≤ n) (u v : Fin n) : + (currentCodegree (completeGraph n) u v : ℝ)/earlyTemplateScale 1 2 n 0 ≤ 4 := by + have hn0 : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hn2 : (2 : ℝ) ≤ n := by exact_mod_cast hn + have hp : (1/2 : ℝ) ≤ earlyDensity n 0 := by + have hh : 1/(n : ℝ) ≤ 1/2 := (div_le_iff₀ hn0).mpr (by linarith) + simp only [earlyDensity,Nat.cast_zero,mul_zero,zero_div,sub_zero] + linarith + have hs : (n : ℝ)/4 ≤ earlyTemplateScale 1 2 n 0 := by + have hh := mul_le_mul_of_nonneg_left (pow_le_pow_left₀ (by norm_num : (0 : ℝ) ≤ 1/2) hp 2) + hn0.le + unfold earlyTemplateScale + norm_num only [pow_one,div_pow,one_pow] at hh ⊢ + nlinarith only [hh] + apply (div_le_iff₀ (lt_of_lt_of_le (by positivity) hs)).mpr + have hh : (currentCodegree (completeGraph n) u v : ℝ) ≤ n := by + exact_mod_cast codegree_le_vertex_count (completeGraph n) u v + linarith + +open Classical in + +theorem prefix_codegree_relative_noise_bound : ∀ᶠ n : ℕ in atTop, + ∀ u v : Fin n, ∀ r : ℝ, 0 < r → + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n)) + (fun ω => if ∃ j ≤ prefixTime n, + (∀ k < j, (currentCodegree (ω (historyIndex (prefixTime n) k)) u v : ℝ) ≤ + 2*earlyTemplateScale 1 2 n k) ∧ + r ≤ |historyNoise (fun _ => step) + (fun k H => (currentCodegree H u v : ℝ)/earlyTemplateScale 1 2 n k) + (prefixTime n) j ω| then 1 else 0) ≤ + 2*(prefixTime n+1 : ℝ)*Real.exp + (-r^2/(4*((6/prefixD n)*(4+8*Real.log n+r+6/prefixD n)+(6/prefixD n)*r))) := by + filter_upwards [earlyTemplateScale_regular 1 2,earlyTemplateScale_log_budget 1 2, + prefixDensity_eventually_inverse_lower,eventually_ge_atTop (2 : ℕ)] with n hreg hbudget hp hn + intro u v r hr + have hn0 : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hpp : 0 < prefixDensity n := lt_of_lt_of_le (by positivity) hp + have hD : 0 < prefixD n := by unfold prefixD; positivity + have hc : 0 < 6/prefixD n := by positivity + have hjump : ∀ k < prefixTime n, 6/earlyTemplateScale 1 2 n (k+1) ≤ 6/prefixD n := by + intro k hk + exact div_le_div_of_nonneg_left (by norm_num) hD + (earlyCodegreeScale_ge_prefixD n (k+1) (by omega) hpp.le) + have ht := codegree_relative_noise_tail u v (completeGraph n) (prefixTime n) + (earlyTemplateScale 1 2 n) (6/prefixD n) 2 hc (by norm_num) + hreg.1 hreg.2.1 hjump r hr + apply ht.trans + apply mul_le_mul_of_nonneg_left _ (by positivity) + apply Real.exp_le_exp.mpr + rw [neg_div,neg_div] + apply neg_le_neg + have hinit := initial_codegree_relative_le_four hn u v + have hz := relativeScaleBudget_nonneg (earlyTemplateScale 1 2 n) (prefixTime n) + hreg.1 hreg.2.1 + have hi0 : 0 ≤ (currentCodegree (completeGraph n) u v : ℝ)/earlyTemplateScale 1 2 n 0 := + div_nonneg (Nat.cast_nonneg _) (hreg.1 0 (Nat.zero_le _)).le + have hd0 : 0 < 4*((6/prefixD n)* + ((currentCodegree (completeGraph n) u v : ℝ)/earlyTemplateScale 1 2 n 0+ + 2*relativeScaleBudget (earlyTemplateScale 1 2 n) (prefixTime n)+r+6/prefixD n)+ + (6/prefixD n)*r) := by positivity + apply div_le_div_of_nonneg_left (sq_nonneg r) hd0 + have hh := mul_le_mul_of_nonneg_left (show + (currentCodegree (completeGraph n) u v : ℝ)/earlyTemplateScale 1 2 n 0+ + 2*relativeScaleBudget (earlyTemplateScale 1 2 n) (prefixTime n)+r+6/prefixD n ≤ + 4+8*Real.log n+r+6/prefixD n by norm_num at hbudget; linarith) + hc.le + linarith + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/GoodPrefixStarApproximation.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/GoodPrefixStarApproximation.lean new file mode 100644 index 0000000000000000000000000000000000000000..7e1f9e636b86bd9276c8bbb54d5235d0fabf8c77 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/GoodPrefixStarApproximation.lean @@ -0,0 +1,67 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.GraphStarApproximation +import OAI.Combinatorics.TriangleRemoval.Process.LinkSimpleGraph + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology +open InnerProductSpace +open scoped InnerProductSpace +open scoped BigOperators NNReal +open Matrix +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix InnerProductSpace +open scoped BigOperators + +namespace SharpTerminalLeave +open scoped Matrix.Norms.L2Operator + +theorem goodPrefix_star_approximation {n : ℕ} {G : Graph n} {c C : ℝ} + (h : GoodPrefixGraph n c C G) (u : Fin n) [Nonempty (neighbors G u)] + (j : ℕ) (hj : j+3 ≤ prefixCycleCap) (heven : Even (j+3)) + (hD : 1 ≤ prefixD n) (hδ : (n : ℝ)^(-c) ≤ 1) : + ‖(prefixD n)⁻¹ • (linkSimpleGraph G h.1 u).adjMatrix ℝ - averagingProjector‖ ≤ + 4 * (((j+4 : ℕ) : ℝ) * (n : ℝ)^(-c) + ((j+3 : ℕ) : ℝ)^(j+4) * 2^(j+3) * + ((neighbors G u).card / prefixD n^((j+3)/2) + 1/prefixD n)) ^ + (1 / ((j+3 : ℕ) : ℝ)) + 7*(n : ℝ)^(-c) := by + have hc := h.2.2.2.2.1 + have hmain := hc (j+3) (by omega) hj u + rw [linkCycleCount_eq_graphEmbeddings (j := j+1) G h.1 u] at hmain + have hcyc : ∀ b, 3 ≤ b → b < j+3 → + ((graphEmbeddings (SimpleGraph.cycleGraph b) (linkSimpleGraph G h.1 u)).card : ℝ) ≤ + 2 * prefixD n ^ b := by + intro b hb hbj + have hh := (abs_le.mp (hc b hb (by omega) u)).2 + have heq : b = b-2+2 := by omega + have hcount : linkCycleCount b G u = + ((graphEmbeddings (SimpleGraph.cycleGraph b) (linkSimpleGraph G h.1 u)).card : ℝ) := by + rw [heq] + exact linkCycleCount_eq_graphEmbeddings G h.1 u + rw [hcount] at hh + have hx := mul_le_mul_of_nonneg_right hδ (pow_nonneg (le_trans zero_le_one hD) b) + nlinarith + simpa only [Fintype.card_coe] using graph_star_approximation (linkSimpleGraph G h.1 u) + j heven (prefixD n) ((n : ℝ)^(-c)) hD (Real.rpow_nonneg (Nat.cast_nonneg _) _) hδ + (goodPrefix_link_rows h u) hmain hcyc + +theorem goodPrefix_star_fixed {n : ℕ} {G : Graph n} {c C : ℝ} + (h : GoodPrefixGraph n c C G) (u : Fin n) [Nonempty (neighbors G u)] + (hD : 1 ≤ prefixD n) (hδ : (n : ℝ)^(-c) ≤ 1) : + ‖(prefixD n)⁻¹ • (linkSimpleGraph G h.1 u).adjMatrix ℝ - averagingProjector‖ ≤ + 4 * ((8001 : ℝ) * (n : ℝ)^(-c) + (8000 : ℝ)^8001 * 2^8000 * + ((neighbors G u).card / prefixD n^4000 + 1/prefixD n)) ^ (1 / (8000 : ℝ)) + + 7*(n : ℝ)^(-c) := by + exact goodPrefix_star_approximation h u 7997 (by decide) (by decide) hD hδ + +end SharpTerminalLeave + +open scoped BigOperators Matrix.Norms.L2Operator +open Matrix + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/GoodPrefixSurvivalMoments.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/GoodPrefixSurvivalMoments.lean new file mode 100644 index 0000000000000000000000000000000000000000..61cef9e53d9941003357ca7ba8865ab338b0ad0d --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/GoodPrefixSurvivalMoments.lean @@ -0,0 +1,85 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.FinishSupportSubset +import OAI.Combinatorics.TriangleRemoval.Stability.GridCavityWeightLower +import OAI.Combinatorics.TriangleRemoval.Process.AveragedSingletonSurvivalError + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +theorem goodPrefix_survival_moments (c C : ℝ) (hc : 0 < c) {ε : ℝ} (hε : 0 < ε) : + ∀ᶠ n : ℕ in atTop, ∀ (G : Graph n), GoodPrefixGraph n c C G → + ∀ hne : G.Nonempty, + (|pmfMean (PMF.uniformOfFinset G hne) (fun e => focusSurvival G {e})- + cavityReference (prefixD n) 1| ≤ + 8192*(1+2*prefixD n)^48/prefixD n^50+ε*cavityReference (prefixD n) 1) ∧ + (|pmfMean (PMF.uniformOfFinset G hne) (fun e => + pmfMean (PMF.uniformOfFinset G hne) (fun f => focusSurvival G {e,f}))- + (cavityReference (prefixD n) 1)^2| ≤ + 65536*(1+2*prefixD n)^48/prefixD n^50+8/(n : ℝ)+ + (2*ε+ε^2)*(cavityReference (prefixD n) 1)^2) := by + filter_upwards [goodPrefix_cavity_stability hc hε, + averaged_singleton_collisionCost c C hc,averaged_disjoint_collisionCost c C hc, + goodPrefix_rootOverlap hc,goodPrefix_full_continuous_rows hc] with n hrel hsingle hpair hover hpos + intro G hG hne + have hr := (hrel C G hG 1 ⟨by norm_num,le_rfl⟩).1 + constructor + · exact (averaged_singleton_survival_error G hne (prefixD n) ε hpos.1 hr).trans + (add_le_add (hsingle G hG hne) le_rfl) + · exact (averaged_pair_survival_error G hne hG.1 (prefixD n) ε hpos.1 hε.le hr).trans + (add_le_add (add_le_add (hpair G hG hne) (hover C G hG hne)) le_rfl) + +lemma focusSurvival_singleton_mean {n : ℕ} (G : Graph n) (e : Finset (Fin n)) : + focusSurvival G {e} = pmfMean (finish G) (fun H => if e ∈ H then 1 else 0) := by + classical + unfold focusSurvival + rw [trueProbability_map] + simp only [Finset.singleton_subset_iff,decide_eq_true_eq,pmfMean] + +lemma focusSurvival_pair_mean {n : ℕ} (G : Graph n) (e f : Finset (Fin n)) : + focusSurvival G {e,f} = pmfMean (finish G) (fun H => if e ∈ H ∧ f ∈ H then 1 else 0) := by + classical + unfold focusSurvival + rw [trueProbability_map] + simp only [Finset.insert_subset_iff,Finset.singleton_subset_iff,decide_eq_true_eq,pmfMean] + +theorem terminal_first_focus_moment {n : ℕ} (G : Graph n) (hne : G.Nonempty) : + pmfMean (PMF.uniformOfFinset G hne) (fun e => focusSurvival G {e}) = + pmfMean (finish G) (fun H => (H.card : ℝ))/(G.card : ℝ) := by + simp_rw [focusSurvival_singleton_mean] + rw [pmfMean_swap] + exact terminal_first_edge_moment G hne + +theorem terminal_second_focus_moment {n : ℕ} (G : Graph n) (hne : G.Nonempty) : + pmfMean (PMF.uniformOfFinset G hne) (fun e => + pmfMean (PMF.uniformOfFinset G hne) (fun f => focusSurvival G {e,f})) = + pmfMean (finish G) (fun H => (H.card : ℝ)^2)/(G.card : ℝ)^2 := by + simp_rw [focusSurvival_pair_mean] + calc + _ = pmfMean (PMF.uniformOfFinset G hne) (fun e => + pmfMean (finish G) (fun H => + pmfMean (PMF.uniformOfFinset G hne) (fun f => + if e ∈ H ∧ f ∈ H then 1 else 0))) := by + apply congrArg + funext e + exact pmfMean_swap _ _ _ + _ = pmfMean (finish G) (fun H => + pmfMean (PMF.uniformOfFinset G hne) (fun e => + pmfMean (PMF.uniformOfFinset G hne) (fun f => + if e ∈ H ∧ f ∈ H then 1 else 0))) := pmfMean_swap _ _ _ + _ = _ := terminal_second_edge_moment G hne + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/NormalizedTrackingAbsolute.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/NormalizedTrackingAbsolute.lean new file mode 100644 index 0000000000000000000000000000000000000000..d48c77531b07daad2952dfbe82d85b2b5c7ec9cc --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/NormalizedTrackingAbsolute.lean @@ -0,0 +1,125 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.TriangleNoiseJumpBudget +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixPairClosureBudget + +namespace OAI + +section +noncomputable section +open scoped BigOperators Topology +open Filter Classical + +namespace SharpTerminalLeave + +lemma normalized_tracking_absolute {X s r : ℝ} (hs : 0 < s) (hr : 0 ≤ r) + (h : |X/s-1| ≤ r/2) : |X-s| ≤ r*s := by + rw [show X/s-1 = (X-s)/s by rw [sub_div,div_self hs.ne'],abs_div,abs_of_pos hs] at h + exact ((div_le_iff₀ hs).mp h).trans (mul_le_mul_of_nonneg_right (by linarith only [hr]) hs.le) + +theorem prefix_good_history : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support → + PrefixRootedUpper 8001 n ω → PrefixBalancedNoise 8001 n ω → + PrefixTriangleNoise n ω → PrefixCodegreeNoiseGood n ω → + GoodPrefixGraph n (1/80000) (3*(8001 : ℕ)+2 : ℕ) (ω (historyIndex (prefixTime n) (prefixTime n))) := by + filter_upwards [prefix_present_edge_closure,prefix_all_pair_closure,prefix_degree_wheel_tracking, + prefixDensity_eventually_inverse_lower,eventually_ge_atTop (1 : ℕ)] with n hedge hpair hwheel hp hn + intro ω hω hroot hbalanced htri hnoise + have hn0 : 0 < n := by omega + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hnR : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hedges := hedge ω hω hroot hbalanced htri hnoise + have halive : historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) + (prefixTime n) (prefixTime n) ω := fun i hi => hedges i hi.le + have he := hedges (prefixTime n) le_rfl + have hw := hwheel ω hω hroot hbalanced (prefixTime n) le_rfl halive + have hc := hpair ω hω hroot hbalanced htri hnoise (prefixTime n) le_rfl + have hpp : 0 < prefixDensity n := (div_pos zero_lt_one hnR).trans_le hp + have hr : 0 ≤ prefixWheelRadius n := Real.rpow_nonneg hnR.le _ + have hdeg := mul_pos hnR hpp + have hD : 0 < prefixD n := mul_pos hnR (sq_pos_of_pos hpp) + refine ⟨he.1,?_,?_,?_,?_,?_⟩ + · simpa only [earlyDensity_prefix,prefixM] using he.2.1 + · intro u + have hh := hw.1 u + rw [earlyDensity_prefix] at hh + simpa only [prefixWheelRadius,neg_div] using normalized_tracking_absolute hdeg hr hh + · intro u v huv + have hh := hc u v huv + simp only [prefixNormalizedCodegree,earlyTemplateScale,pow_one,earlyDensity_prefix] at hh + simpa only [prefixD,prefixWheelRadius,neg_div] using normalized_tracking_absolute hD hr hh + · intro j hj hj' u + have hh := hw.2 j hj hj' u + rw [earlyDensity_prefix] at hh + simpa only [prefixD,prefixWheelRadius,neg_div] using normalized_tracking_absolute (pow_pos hD j) hr hh + · intro k hk T ψ + have hh := (hroot (prefixTime n) le_rfl halive k hk T ψ).le + rw [earlyDensity_prefix] at hh + have hfac : (1+Real.log (n : ℝ))^(3*8001+2) = prefixTemplateFactor n (3*(8001 : ℕ)+2 : ℕ) := by + simp only [prefixTemplateFactor,Real.rpow_natCast] + rw [hfac] at hh + have hfac0 : 0 ≤ prefixTemplateFactor n (3*(8001 : ℕ)+2 : ℕ) := Real.rpow_nonneg (by have := Real.log_nonneg hn1; linarith) _ + constructor + · intro hlarge + exact hh.trans (mul_le_mul_of_nonneg_left (rootedCompletionMax_large T hpp.le hlarge) hfac0) + · intro hsmall + exact hh.trans ((mul_le_mul_of_nonneg_left (rootedCompletionMax_small T hsmall) hfac0).trans_eq (mul_one _)) + +lemma four_prefix_margins : ∀ᶠ n : ℕ in atTop, + 4*Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) ≤ + Real.exp (-(Real.log (n : ℝ))^(4/3 : ℝ)) := by + have ht : Tendsto (fun n : ℕ => (Real.log (n : ℝ))^(4/3 : ℝ)) atTop atTop := + (tendsto_rpow_atTop (by norm_num : (0 : ℝ) < 4/3)).comp + (Real.tendsto_log_atTop.comp tendsto_natCast_atTop_atTop) + filter_upwards [ht.eventually (eventually_ge_atTop (Real.log 4))] with n hn + have hh : (4 : ℝ) ≤ Real.exp ((Real.log (n : ℝ))^(4/3 : ℝ)) := by + calc + (4 : ℝ) = Real.exp (Real.log 4) := (Real.exp_log (by norm_num)).symm + _ ≤ _ := Real.exp_le_exp.mpr hn + calc + _ ≤ Real.exp ((Real.log (n : ℝ))^(4/3 : ℝ))*Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := + mul_le_mul_of_nonneg_right hh (Real.exp_pos _).le + _ = _ := by rw [← Real.exp_add]; congr 1; ring + +theorem uniform_prefix_obligation : UniformPrefixObligation := by + refine ⟨1/80000,by norm_num,(3*(8001 : ℕ)+2 : ℕ),?_⟩ + filter_upwards [prefix_good_history,prefix_rooted_upper_failure 8001, + prefix_balanced_noise_failure 8001,prefix_triangle_noise_failure, + prefix_codegree_noise_failure,four_prefix_margins] with n hgood hr hb ht hc hmargin + let μ := historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n) + have hm : μ.map (fun ω => ω (historyIndex (prefixTime n) (prefixTime n))) = prefixLaw n := by + rw [historyLaw_marginal _ _ _ _ le_rfl,markovLaw_triangle_step] + rfl + unfold prefixFailure + rw [← hm,pmfMean_map] + have hpoint : ∀ ω ∈ μ.support, + (if GoodPrefixGraph n (1/80000) (3*(8001 : ℕ)+2 : ℕ) (ω (historyIndex (prefixTime n) (prefixTime n))) then (0 : ℝ) else 1) ≤ + (if PrefixRootedUpper 8001 n ω then 0 else 1)+ + (if PrefixBalancedNoise 8001 n ω then 0 else 1)+ + (if PrefixTriangleNoise n ω then 0 else 1)+ + (if PrefixCodegreeNoiseGood n ω then 0 else 1) := by + intro ω hω + by_cases h₁ : PrefixRootedUpper 8001 n ω <;> + by_cases h₂ : PrefixBalancedNoise 8001 n ω <;> + by_cases h₃ : PrefixTriangleNoise n ω <;> + by_cases h₄ : PrefixCodegreeNoiseGood n ω + all_goals simp only [h₁,h₂,h₃,h₄,ite_true,ite_false] + · rw [ite_eq_left (hgood ω hω h₁ h₂ h₃ h₄)]; norm_num + all_goals split_ifs <;> norm_num + have hs := pmfMean_mono μ hpoint + simp only [pmfMean_add] at hs + apply hs.trans + have hs' := add_le_add (add_le_add (add_le_add hr hb) ht) hc + calc + _ ≤ ((Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ))+ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)))+ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)))+ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := hs' + _ = 4*Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := by ring + _ ≤ _ := hmargin + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PolynomialExponentialPrefixEnvelope.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PolynomialExponentialPrefixEnvelope.lean new file mode 100644 index 0000000000000000000000000000000000000000..487c1506159431924f6a65cfb284db6655528892 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PolynomialExponentialPrefixEnvelope.lean @@ -0,0 +1,47 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +theorem polynomial_exponential_le_prefix_envelope (A K κ : ℝ) (hκ : 0 < κ) : + ∀ᶠ n : ℕ in atTop, + A*(n : ℝ)^K*Real.exp (-(n : ℝ)^κ) ≤ + Real.exp (-(Real.log n)^(4/3 : ℝ)) := by + have ho := ((isLittleO_log_rpow_atTop hκ).const_mul_left (K+1)).add + (isLittleO_log_rpow_rpow_atTop (4/3 : ℝ) hκ) + have hbound := (ho.comp_tendsto tendsto_natCast_atTop_atTop).bound + (by norm_num : (0 : ℝ) < 1) + filter_upwards [hbound, tendsto_natCast_atTop_atTop.eventually_ge_atTop A, + eventually_ge_atTop (1 : ℕ)] with n hn hA hn1 + have hn0 : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hb : (K+1)*Real.log (n : ℝ)+(Real.log n)^(4/3 : ℝ) ≤ (n : ℝ)^κ := by + dsimp at hn + simp only [one_mul] at hn + rw [abs_of_nonneg (Real.rpow_nonneg hn0.le κ)] at hn + exact (le_abs_self _).trans hn + calc + _ ≤ (n : ℝ)*(n : ℝ)^K*Real.exp (-(n : ℝ)^κ) := + mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_right hA + (Real.rpow_nonneg hn0.le K)) (Real.exp_pos _).le + _ = Real.exp (Real.log n*(K+1)-(n : ℝ)^κ) := by + rw [show (n : ℝ)*(n : ℝ)^K = (n : ℝ)^(K+1) by + rw [Real.rpow_add hn0,Real.rpow_one]; ring] + rw [Real.rpow_def_of_pos hn0,← Real.exp_add] + congr 1 + _ ≤ _ := Real.exp_le_exp.mpr (by linarith only [hb]) + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixBalancedNoise.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixBalancedNoise.lean new file mode 100644 index 0000000000000000000000000000000000000000..b35cc16b0dcdfcdfc814b1fdffbba6a08e4edb03 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixBalancedNoise.lean @@ -0,0 +1,206 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.RerootTemplate +import OAI.Combinatorics.TriangleRemoval.Process.PowerEulerError +import OAI.Combinatorics.TriangleRemoval.Tracking.BalancedJump + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +def PrefixBalancedNoise (H n : ℕ) (ω : History (Graph n) (prefixTime n)) : Prop := + ∀ k ≤ H, ∀ T : RootedTemplate k, ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + ¬ RelativeNoiseExit (fun φ : RootedInjection T ψ => imageEdges T φ.val) + (balancedLoad H T n) (earlyTemplateScale (k-T.roots.card) T.edges.card n) 2 + (codegreeNoiseRadius n) (prefixTime n) ω + +theorem prefix_balanced_noise_failure (H : ℕ) : ∀ᶠ n : ℕ in atTop, + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n)) + (fun ω => if PrefixBalancedNoise H n ω then 0 else 1) ≤ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := by + filter_upwards [prefix_balanced_noise_single H, + polynomial_logsquare_le_prefix_margin ((rootedFamilySize H : ℝ)*2^(H+2)) (H+2), + eventually_ge_atTop (1 : ℕ)] with n htail hb hn + let μ := historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n) + let P (i : RootedCountIndex H n) (ω : History (Graph n) (prefixTime n)) := + RelativeNoiseExit (fun φ : RootedInjection i.2.1 i.2.2 => imageEdges i.2.1 φ.val) + (balancedLoad H i.2.1 n) (earlyTemplateScale (i.1.val-i.2.1.roots.card) i.2.1.edges.card n) + 2 (codegreeNoiseRadius n) (prefixTime n) ω + have he (ω : History (Graph n) (prefixTime n)) : + ¬ PrefixBalancedNoise H n ω ↔ ∃ i ∈ Finset.univ, P i ω := by + simp only [PrefixBalancedNoise,not_forall,not_not] + constructor + · rintro ⟨k,hk,T,ψ,h⟩ + exact ⟨⟨⟨k,by omega⟩,T,ψ⟩,Finset.mem_univ _,h⟩ + · rintro ⟨i,_,h⟩ + exact ⟨i.1.val,by omega,i.2.1,i.2.2,h⟩ + have hu := pmfMean_event_union μ Finset.univ P + have hs : (∑ i, pmfMean μ (fun ω => if P i ω then 1 else 0)) ≤ + (Fintype.card (RootedCountIndex H n) : ℝ)*2*(prefixTime n+1)* + Real.exp (-(Real.log (n : ℝ))^2/4) := by + calc + _ ≤ ∑ _i : RootedCountIndex H n, + 2*(prefixTime n+1 : ℝ)*Real.exp (-(Real.log (n : ℝ))^2/4) := by + apply Finset.sum_le_sum + intro i _ + exact htail i.1.val (by omega) i.2.1 i.2.2 + _ = _ := by simp; ring + have hc : (Fintype.card (RootedCountIndex H n) : ℝ)*2*(prefixTime n+1) ≤ + (rootedFamilySize H : ℝ)*2^(H+2)*(n : ℝ)^(H+2 : ℕ) := by + exact_mod_cast rooted_noise_chances_le H n hn + have hev : (n : ℝ)^((H : ℝ)+2) = (n : ℝ)^(H+2 : ℕ) := by + rw [← Real.rpow_natCast]; norm_cast + rw [hev] at hb + have hs' := hs.trans ((mul_le_mul_of_nonneg_right hc (Real.exp_pos _).le).trans hb) + have hf := hu.trans hs' + convert hf using 1 + try rfl + apply congrArg (pmfMean μ) + funext ω + simp only [← he] + split_ifs <;> rfl + +noncomputable def prefixWheelRadius (n : ℕ) : ℝ := (n : ℝ)^(-1/80000 : ℝ) + +lemma balanced_closure_budget (H : ℕ) : ∀ᶠ n : ℕ in atTop, + 0 ≤ prefixEdgeRadius n ∧ prefixEdgeRadius n ≤ 1/2 ∧ + prefixWheelRadius n ≤ 1 ∧ + (2 : ℝ)^(H^2)*(2*(H : ℝ)^2)/(n : ℝ)+codegreeNoiseRadius n+ + (16*(H : ℝ)^2*prefixEdgeRadius n*Real.log n+4*((H : ℝ)^2)^2/prefixD n+ + 24*((H : ℝ)^2)^2*Real.log n/(n : ℝ)) ≤ prefixWheelRadius n/2 := by + let C : ℝ := 2^(H^2)*(2*(H : ℝ)^2)+1+16*(H : ℝ)^2+28*((H : ℝ)^2)^2 + have hC : 0 < C := by dsimp [C]; positivity + have hrad : Tendsto prefixEdgeRadius atTop (nhds 0) := by + change Tendsto (fun n : ℕ => (n : ℝ)^(-1/40000 : ℝ)) atTop (nhds 0) + simpa only [Function.comp_def,prefixEdgeRadius,neg_div] using (tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1/40000)).comp + tendsto_natCast_atTop_atTop + filter_upwards [prefixTemplateFactor_subpower 1 (by norm_num : (0 : ℝ) < 1/80000) + (by positivity : 0 < 1/(2*C)),prefixD_eventual_envelope, + hrad.eventually_le_const (by norm_num : (0 : ℝ) < 1/2), + eventually_ge_atTop (1 : ℕ)] with n hlog hD hrad hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hη : 0 ≤ prefixEdgeRadius n := Real.rpow_nonneg hn0.le _ + have hl : 0 ≤ Real.log (n : ℝ) := Real.log_nonneg hn1 + have hηlog : C*(1+Real.log (n : ℝ))*prefixEdgeRadius n ≤ prefixWheelRadius n/2 := by + have hh := mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_left + (show 1+Real.log (n : ℝ) ≤ 1/(2*C)*(n : ℝ)^(1/80000 : ℝ) by + simpa only [prefixTemplateFactor,Real.rpow_one] using hlog) hC.le) hη + have he : C*(1/(2*C)*(n : ℝ)^(1/80000 : ℝ))*prefixEdgeRadius n = prefixWheelRadius n/2 := by + unfold prefixEdgeRadius prefixWheelRadius + calc + _ = (C/(2*C))*((n : ℝ)^(1/80000 : ℝ)*(n : ℝ)^(-1/40000 : ℝ)) := by ring + _ = _ := by rw [← Real.rpow_add hn0]; norm_num; field_simp + exact hh.trans_eq he + have hnoise : codegreeNoiseRadius n ≤ prefixEdgeRadius n := + Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hinv : 1/(n : ℝ) ≤ prefixEdgeRadius n := by + simpa only [Real.rpow_neg_one,one_div,prefixEdgeRadius] using + (Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num : (-1 : ℝ) ≤ -1/40000)) + have hDinv : 1/prefixD n ≤ prefixEdgeRadius n := by + apply (div_le_div_of_nonneg_left zero_le_one (Real.rpow_pos_of_pos hn0 _) hD.1).trans + rw [one_div,← Real.rpow_neg hn0.le] + exact Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + refine ⟨hη,hrad,Real.rpow_le_one_of_one_le_of_nonpos hn1 (by norm_num),?_⟩ + have hinit := mul_le_mul_of_nonneg_left hinv (show 0 ≤ (2 : ℝ)^(H^2)*(2*(H : ℝ)^2) by positivity) + have hdisc := mul_le_mul_of_nonneg_left hDinv (show 0 ≤ 4*((H : ℝ)^2)^2 by positivity) + have hgrid := mul_le_mul_of_nonneg_left hinv (show 0 ≤ 24*((H : ℝ)^2)^2*Real.log n by positivity) + have hηL : prefixEdgeRadius n ≤ (1+Real.log (n : ℝ))*prefixEdgeRadius n := by nlinarith only [mul_nonneg hl hη] + have hmain := mul_le_mul_of_nonneg_left hηL + (show 0 ≤ (2 : ℝ)^(H^2)*(2*(H : ℝ)^2)+1+4*((H : ℝ)^2)^2 by positivity) + dsimp only [C] at hηlog + simp only [div_eq_mul_inv,one_mul] at hinit hdisc hgrid ⊢ + nlinarith only [hinit,hdisc,hgrid,hnoise,hmain,hηlog, + mul_nonneg (show 0 ≤ 16*(H : ℝ)^2+24*((H : ℝ)^2)^2 by positivity) hη] + +theorem prefix_balanced_tracking (H : ℕ) : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support → + PrefixRootedUpper H n ω → PrefixBalancedNoise H n ω → + ∀ j ≤ prefixTime n, + historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) (prefixTime n) j ω → + ∀ k ≤ H, ∀ T : RootedTemplate k, RootedTwoBalanced T → + ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + |rootedCount T ψ (ω (historyIndex (prefixTime n) j))/rootedScaling T n (earlyDensity n j)-1| ≤ + prefixWheelRadius n/2 := by + filter_upwards [balanced_closure_budget H,earlyTemplateScale_uniform_regular H, + prefixDensity_eventually_inverse_lower,prefixD_eventual_envelope, + eventually_ge_atTop H,eventually_ge_atTop (2 : ℕ)] with n hbud hreg hp hD hnH hn + intro ω hω hupper hnoise j hj hsafe k hk T hT ψ + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast (by omega : 1 ≤ n) + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hln : 0 ≤ Real.log (n : ℝ) := Real.log_nonneg hn1 + have hb : T.edges.card ≤ H^2 := (rootedTemplate_edges_card_le_sq T).trans (Nat.pow_le_pow_left hk 2) + have hbR : (T.edges.card : ℝ) ≤ (H : ℝ)^2 := by exact_mod_cast hb + have hs := hreg (k-T.roots.card) ((Nat.sub_le _ _).trans hk) T.edges.card hb + have hi := (historyLaw_path_support (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n) le_rfl ω hω).1 + have hi' : ω (historyIndex (prefixTime n) 0) = completeGraph n := by simpa only [PMF.mem_support_pure_iff] using hi + induction j using Nat.strong_induction_on with + | h j ih => + have hcap : ∀ i < j, rootedCount T ψ (ω (historyIndex (prefixTime n) i)) ≤ + 2*earlyTemplateScale (k-T.roots.card) T.edges.card n i := by + intro i hij + have htracking := ih i hij (by omega) (fun l hl => hsafe l (hl.trans hij)) + have hbnd : rootedCount T ψ (ω (historyIndex (prefixTime n) i))/ + rootedScaling T n (earlyDensity n i) ≤ 2 := by + have hh := (abs_le.mp htracking).2 + linarith only [hh,hbud.2.2.1] + exact (div_le_iff₀ (hs.1 i (by omega))).mp hbnd + have hloads : pastRelativeCopyLoadsSafe (fun φ : RootedInjection T ψ => imageEdges T φ.val) + (balancedLoad H T n) (prefixTime n) j ω := by + intro i hi + have hij : i < j := lt_of_lt_of_le hi (Nat.min_le_left _ _) + have hiT : i ≤ prefixTime n := by omega + have hDi : 1 ≤ (n : ℝ)*earlyDensity n i^2 := by + have hd := earlyCodegreeScale_ge_prefixD n i hiT (le_trans (by positivity) hp) + have hh := Real.one_le_rpow hn1 (by norm_num : (0 : ℝ) ≤ 1/1000) + exact hh.trans (hD.1.trans (by simpa only [earlyTemplateScale,pow_one] using hd)) + exact balanced_rooted_load_bound T hk hT ψ _ (hsafe i hij).1 + (early_density_positive (by omega) hp hiT) (earlyDensity_le_one n i) (by positivity) hDi + (fun a ha U ξ => (hupper i hiT (fun l hl => hsafe l (hl.trans hij)) a ha U ξ).le) + have hnse : |historyNoise (fun _ => step) + (fun i G => rootedCount T ψ G/earlyTemplateScale (k-T.roots.card) T.edges.card n i) + (prefixTime n) j ω| < codegreeNoiseRadius n := by + exact lt_of_not_ge (fun hcross => hnoise k hk T ψ ⟨j,hj,hloads,hcap,hcross⟩) + have hd := early_relative_drift_bound (fun φ : RootedInjection T ψ => imageEdges T φ.val) + (k-T.roots.card) T.edges.card (fun φ => imageEdges_card T φ.val) + (prefixEdgeRadius n) (by omega) hp hbud.1 hbud.2.1 hs.2.2.1 j hj ω hsafe hcap + have hinit := rootedCount_initial_uniform_error H hk hnH hn T ψ + have he : earlyDensity n 0 = 1-1/(n : ℝ) := by simp [earlyDensity] + rw [← he,← hi'] at hinit + have htri := fun x y z : ℝ => (abs_add_le (x+y) z).trans + (add_le_add (abs_add_le x y) le_rfl) + have htri := htri + (rootedCount T ψ (ω (historyIndex (prefixTime n) j))/rootedScaling T n (earlyDensity n j)- + rootedCount T ψ (ω (historyIndex (prefixTime n) 0))/rootedScaling T n (earlyDensity n 0)- + historyNoise (fun _ => step) (fun i G => rootedCount T ψ G/earlyTemplateScale (k-T.roots.card) T.edges.card n i) + (prefixTime n) j ω) + (historyNoise (fun _ => step) (fun i G => rootedCount T ψ G/earlyTemplateScale (k-T.roots.card) T.edges.card n i) + (prefixTime n) j ω) + (rootedCount T ψ (ω (historyIndex (prefixTime n) 0))/rootedScaling T n (earlyDensity n 0)-1) + have hlarge : 16*(T.edges.card : ℝ)*prefixEdgeRadius n*Real.log n+4*(T.edges.card : ℝ)^2/prefixD n+ + 24*(T.edges.card : ℝ)^2*Real.log n/(n : ℝ) ≤ + 16*(H : ℝ)^2*prefixEdgeRadius n*Real.log n+4*((H : ℝ)^2)^2/prefixD n+ + 24*((H : ℝ)^2)^2*Real.log n/(n : ℝ) := by + have hdpos : 0 ≤ prefixD n := (Real.rpow_nonneg hn0.le _).trans hD.1 + gcongr + exact hbud.1 + change |rootedCount T ψ (ω (historyIndex (prefixTime n) j))/rootedScaling T n (earlyDensity n j)- + rootedCount T ψ (ω (historyIndex (prefixTime n) 0))/rootedScaling T n (earlyDensity n 0)- + historyNoise (fun _ => step) (fun i G => rootedCount T ψ G/earlyTemplateScale (k-T.roots.card) T.edges.card n i) + (prefixTime n) j ω| ≤ _ at hd + have heq : ∀ x y z : ℝ, x-y-z+z+(y-1) = x-1 := by intros; ring + rw [heq] at htri + linarith only [htri,hd,hnse,hinit,hlarge,hbud.2.2.2] + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodeSet.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodeSet.lean new file mode 100644 index 0000000000000000000000000000000000000000..1213e448eee38c97c26351864ce37ec2f3d2baed --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodeSet.lean @@ -0,0 +1,93 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Queries.SplitQueryPaths + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +namespace SharpTerminalLeave + +def prefixCodeSet {A : Type*} [DecidableEq A] : List A → Finset (List A) + | [] => ∅ + | a::p => insert [a] ((prefixCodeSet p).image (List.cons a)) + +lemma nil_not_mem_prefixCodeSet {A : Type*} [DecidableEq A] (p : List A) : + [] ∉ prefixCodeSet p := by + cases p with + | nil => simp [prefixCodeSet] + | cons a p => simp [prefixCodeSet] + +lemma prefixCodeSet_card {A : Type*} [DecidableEq A] (p : List A) : + (prefixCodeSet p).card = p.length := by + induction p with + | nil => rfl + | cons a p ih => + rw [prefixCodeSet,Finset.card_insert_of_notMem,Finset.card_image_of_injective _ (fun _ _ h => (List.cons.inj h).2),ih,List.length_cons] + intro h + obtain ⟨q,hq,h⟩ := Finset.mem_image.mp h + have he : q = [] := List.cons.inj h |>.2 + subst q + exact nil_not_mem_prefixCodeSet p hq + +lemma prefixCodeSet_cons_disjoint {A : Type*} [DecidableEq A] {a b : A} + (hab : a ≠ b) (p q : List A) : + Disjoint (prefixCodeSet (a::p)) (prefixCodeSet (b::q)) := by + apply Finset.disjoint_left.mpr + intro l hl hr + have hhead {x : A} {t : List A} (h : l ∈ prefixCodeSet (x::t)) : l.head? = some x := by + rw [prefixCodeSet] at h + rcases Finset.mem_insert.mp h with rfl | h + · rfl + · obtain ⟨u,_,rfl⟩ := Finset.mem_image.mp h + rfl + exact hab (Option.some.inj ((hhead hl).symm.trans (hhead hr))) + +lemma prefixCodeSet_union_card {A : Type*} [DecidableEq A] (p q : List A) : + (prefixCodeSet p ∪ prefixCodeSet q).card = + (splitQueryPaths p q).1.length + (splitQueryPaths p q).2.1.length + + (splitQueryPaths p q).2.2.length := by + induction p generalizing q with + | nil => simp [prefixCodeSet,splitQueryPaths,prefixCodeSet_card] + | cons a p ih => + cases q with + | nil => simpa only [prefixCodeSet,Finset.union_empty,splitQueryPaths,List.length_nil,zero_add,Nat.add_zero] using prefixCodeSet_card (a::p) + | cons b q => + by_cases hab : a = b + · subst b + have he : prefixCodeSet (a::p) ∪ prefixCodeSet (a::q) = + insert [a] ((prefixCodeSet p ∪ prefixCodeSet q).image (List.cons a)) := by + simp only [prefixCodeSet,Finset.image_union,Finset.insert_union,Finset.union_insert,Finset.insert_idem] + rw [he,Finset.card_insert_of_notMem,Finset.card_image_of_injective _ (fun _ _ h => (List.cons.inj h).2),ih] + · simp only [splitQueryPaths,↓reduceIte,List.length_cons] + omega + · intro h + obtain ⟨t,ht,ht0⟩ := Finset.mem_image.mp h + have he : t = [] := List.cons.inj ht0 |>.2 + subst t + rcases Finset.mem_union.mp ht with h | h + · exact nil_not_mem_prefixCodeSet p h + · exact nil_not_mem_prefixCodeSet q h + · rw [Finset.card_union_of_disjoint (prefixCodeSet_cons_disjoint hab p q), + prefixCodeSet_card,prefixCodeSet_card] + simp only [splitQueryPaths,hab,↓reduceIte,List.length_nil,zero_add] + +lemma prefixCodeSet_append_singleton {A : Type*} [DecidableEq A] (p : List A) (a : A) : + prefixCodeSet (p++[a]) = insert (p++[a]) (prefixCodeSet p) := by + induction p with + | nil => simp [prefixCodeSet] + | cons b p ih => + simp only [List.cons_append,prefixCodeSet,ih,Finset.image_insert] + exact Finset.insert_comm _ _ _ + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeDriftTerm.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeDriftTerm.lean new file mode 100644 index 0000000000000000000000000000000000000000..412e703aa66b83eed0fc7afb03b826027fc7c431 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeDriftTerm.lean @@ -0,0 +1,72 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.CenteredCodegreeNeighborSum +import OAI.Combinatorics.TriangleRemoval.Process.CommonNeighborsComplete + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +theorem complete_history_valid {n T : ℕ} + (ω : History (Graph n) T) + (hω : ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) T T).support) : + ∀ j ≤ T, ω (historyIndex T j) ⊆ completeGraph n := by + obtain ⟨hi,hp⟩ := historyLaw_path_support (PMF.pure (completeGraph n)) + (fun _ => step) T T le_rfl ω hω + have hi' : ω (historyIndex T 0) = completeGraph n := by + simpa only [PMF.mem_support_pure_iff] using hi + intro j + induction j with + | zero => + intro _ + rw [hi'] + | succ j ih => + intro hj + exact (step_support_subset (hp j (Nat.lt_of_succ_le hj))).trans + (ih (Nat.le_of_succ_le hj)) + +noncomputable def prefixCodegreeDriftTerm {n : ℕ} (u v : Fin n) + (k : ℕ) (G : Graph n) : ℝ := + (currentCodegree G u v : ℝ)*(1/earlyTemplateScale 1 2 n (k+1)-1/earlyTemplateScale 1 2 n k)- + ((2*earlyTemplateScale 1 2 n k-if {u,v} ∈ G then 1 else 0)* + (currentCodegree G u v : ℝ)+ + centeredCodegreeNeighborSum G u v (earlyTemplateScale 1 2 n k))/ + ((triangles G).card*earlyTemplateScale 1 2 n (k+1)) + +theorem codegree_predictable_error_explicit {n : ℕ} (hn : 2 ≤ n) + (u v : Fin n) (huv : u ≠ v) + (ω : History (Graph n) (prefixTime n)) + (hω : ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n)).support) + (j : ℕ) (hj : j ≤ prefixTime n) : + prefixCodegreePredictableError u v j ω = -1/((n : ℝ)-1)^2+ + ∑ k ∈ Finset.range j, + prefixCodegreeDriftTerm u v k (ω (historyIndex (prefixTime n) k)) := by + obtain ⟨hi,_⟩ := historyLaw_path_support (PMF.pure (completeGraph n)) + (fun _ => step) (prefixTime n) (prefixTime n) le_rfl ω hω + have hi' : ω (historyIndex (prefixTime n) 0) = completeGraph n := by + simpa only [PMF.mem_support_pure_iff] using hi + unfold prefixCodegreePredictableError + rw [hi'] + change ((currentCodegree (completeGraph n) u v : ℝ)/earlyTemplateScale 1 2 n 0-1)+_ = _ + rw [initial_normalized_codegree_error hn u v huv] + congr 1 + unfold historyCompensator historyCounter + rw [Nat.min_eq_left hj] + apply Finset.sum_congr rfl + intro k hk + exact prefix_codegree_centered_drift + (complete_history_valid ω hω k ((Finset.mem_range.mp hk).le.trans hj)) u v huv k + +end SharpTerminalLeave +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeErrorExit.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeErrorExit.lean new file mode 100644 index 0000000000000000000000000000000000000000..d0fa6c4e6885a045ddc2b314fa5636e94b8508f7 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeErrorExit.lean @@ -0,0 +1,116 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.HistoryCompensator + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +def PrefixCodegreeErrorExit {n : ℕ} (u v : Fin n) + (ω : History (Graph n) (prefixTime n)) : Prop := + ∃ j ≤ prefixTime n, 2*codegreeNoiseRadius n ≤ + |prefixNormalizedCodegree u v j (ω (historyIndex (prefixTime n) j))-1| + +lemma codegreeNoiseRadius_tendsto_zero : Tendsto codegreeNoiseRadius atTop (nhds 0) := by + change Tendsto (fun n : ℕ => (n : ℝ)^(-1/10000 : ℝ)) atTop (nhds 0) + simpa only [Function.comp_def,neg_div] using + ((tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1/10000)).comp + (tendsto_natCast_atTop_atTop (R := ℝ))) + +lemma pmfMean_event_or_upper {α : Type*} [Fintype α] (μ : PMF α) + (P Q A : α → Prop) [DecidablePred P] [DecidablePred Q] [DecidablePred A] + (h : ∀ a, A a → P a ∨ Q a) : + pmfMean μ (fun a => if A a then 1 else 0) ≤ + pmfMean μ (fun a => if P a then 1 else 0)+ + pmfMean μ (fun a => if Q a then 1 else 0) := by + rw [← pmfMean_add] + apply pmfMean_mono + intro a _ + by_cases ha : A a + · rw [ite_eq_left ha] + rcases h a ha with hp | hq + · rw [ite_eq_left hp] + split <;> norm_num + · rw [ite_eq_left hq] + split <;> norm_num + · rw [ite_eq_right ha] + split <;> split <;> norm_num + +lemma any_codegree_exit_noise_or_drift {n : ℕ} + (hs : ∀ k ≤ prefixTime n, 0 < earlyTemplateScale 1 2 n k) + (hr : codegreeNoiseRadius n ≤ 1/2) + (ω : History (Graph n) (prefixTime n)) + (h : ∃ uv : Fin n × Fin n, uv.1 ≠ uv.2 ∧ PrefixCodegreeErrorExit uv.1 uv.2 ω) : + (∃ uv : Fin n × Fin n, PrefixCodegreeNoiseExit uv.1 uv.2 ω) ∨ + (∃ uv : Fin n × Fin n, uv.1 ≠ uv.2 ∧ PrefixCodegreeDriftExit uv.1 uv.2 ω) := by + obtain ⟨uv,hne,hcross⟩ := h + rcases codegree_first_exit_noise_or_drift uv.1 uv.2 hs hr ω hcross with hn | hd + · exact Or.inl ⟨uv,hn⟩ + · exact Or.inr ⟨uv,hne,hd⟩ + +open Classical in + +noncomputable def prefixCodegreeErrorProbability (n : ℕ) : ℝ := + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => if ∃ uv : Fin n × Fin n, uv.1 ≠ uv.2 ∧ PrefixCodegreeErrorExit uv.1 uv.2 ω then 1 else 0) + +open Classical in + +noncomputable def prefixCodegreeDriftProbability (n : ℕ) : ℝ := + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => if ∃ uv : Fin n × Fin n, uv.1 ≠ uv.2 ∧ PrefixCodegreeDriftExit uv.1 uv.2 ω then 1 else 0) + +open Classical in + +noncomputable def prefixCodegreeNoiseProbability (n : ℕ) : ℝ := + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => if ∃ uv : Fin n × Fin n, PrefixCodegreeNoiseExit uv.1 uv.2 ω then 1 else 0) + +lemma prefix_codegree_error_probability_split (n : ℕ) + (hs : ∀ k ≤ prefixTime n, 0 < earlyTemplateScale 1 2 n k) + (hr : codegreeNoiseRadius n ≤ 1/2) : + prefixCodegreeErrorProbability n ≤ prefixCodegreeNoiseProbability n+prefixCodegreeDriftProbability n := by + classical + unfold prefixCodegreeErrorProbability prefixCodegreeNoiseProbability prefixCodegreeDriftProbability + have hm := pmfMean_event_or_upper + (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => ∃ uv : Fin n × Fin n, PrefixCodegreeNoiseExit uv.1 uv.2 ω) + (fun ω => ∃ uv : Fin n × Fin n, uv.1 ≠ uv.2 ∧ PrefixCodegreeDriftExit uv.1 uv.2 ω) + (fun ω => ∃ uv : Fin n × Fin n, uv.1 ≠ uv.2 ∧ PrefixCodegreeErrorExit uv.1 uv.2 ω) + (any_codegree_exit_noise_or_drift hs hr) + convert hm using 1 + +lemma prefix_codegree_noise_probability_bound : ∀ᶠ n : ℕ in atTop, + prefixCodegreeNoiseProbability n ≤ Real.exp (-(Real.log n)^(4/3 : ℝ)) := by + filter_upwards [all_codegree_stopped_noise_envelope] with n hn + unfold prefixCodegreeNoiseProbability + exact hn + +theorem prefix_codegree_error_probability_reduction : ∀ᶠ n : ℕ in atTop, + prefixCodegreeErrorProbability n ≤ Real.exp (-(Real.log n)^(4/3 : ℝ))+ + prefixCodegreeDriftProbability n := by + filter_upwards [prefix_codegree_noise_probability_bound,earlyTemplateScale_regular 1 2, + codegreeNoiseRadius_tendsto_zero.eventually (gt_mem_nhds (by norm_num : (0 : ℝ) < 1/2))] + with n hnoise hscale hr + have hs : prefixCodegreeErrorProbability n ≤ + prefixCodegreeNoiseProbability n+prefixCodegreeDriftProbability n := + prefix_codegree_error_probability_split n hscale.1 hr.le + calc + prefixCodegreeErrorProbability n ≤ + prefixCodegreeNoiseProbability n+prefixCodegreeDriftProbability n := hs + _ ≤ Real.exp (-(Real.log n)^(4/3 : ℝ))+prefixCodegreeDriftProbability n := + add_le_add hnoise (le_refl (prefixCodegreeDriftProbability n)) + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeNoiseExit.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeNoiseExit.lean new file mode 100644 index 0000000000000000000000000000000000000000..b98905fc81dd1caaed9e52c45efcf3a2bd37fdf8 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeNoiseExit.lean @@ -0,0 +1,86 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixTimeSquare +import OAI.Combinatorics.TriangleRemoval.Tracking.CodegreeVertexCount +import OAI.Combinatorics.TriangleRemoval.Tracking.CodegreeNoiseRadius + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +def PrefixCodegreeNoiseExit {n : ℕ} (u v : Fin n) + (ω : History (Graph n) (prefixTime n)) : Prop := + ∃ j ≤ prefixTime n, + (∀ k < j, (currentCodegree (ω (historyIndex (prefixTime n) k)) u v : ℝ) ≤ + 2*earlyTemplateScale 1 2 n k) ∧ + codegreeNoiseRadius n ≤ |historyNoise (fun _ => step) + (fun k H => (currentCodegree H u v : ℝ)/earlyTemplateScale 1 2 n k) + (prefixTime n) j ω| + +open Classical in + +theorem all_codegree_stopped_noise_envelope : ∀ᶠ n : ℕ in atTop, + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n)) + (fun ω => if ∃ uv : Fin n × Fin n, PrefixCodegreeNoiseExit uv.1 uv.2 ω + then 1 else 0) ≤ Real.exp (-(Real.log n)^(4/3 : ℝ)) := by + filter_upwards [prefix_codegree_relative_noise_bound,codegree_noise_exponent_lower, + polynomial_exponential_le_prefix_envelope 4 4 (1/2500) (by norm_num), + eventually_ge_atTop (1 : ℕ)] with n htail hexp hbudget hn + have hn0 : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hr : 0 < codegreeNoiseRadius n := Real.rpow_pos_of_pos hn0 _ + have hh := pmfMean_event_union + (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (Finset.univ : Finset (Fin n × Fin n)) + (fun uv ω => PrefixCodegreeNoiseExit uv.1 uv.2 ω) + simp only [Finset.mem_univ,true_and] at hh + apply hh.trans + calc + _ ≤ ∑ _uv : Fin n × Fin n, 2*(prefixTime n+1 : ℝ)*Real.exp (-(n : ℝ)^(1/2500 : ℝ)) := by + apply Finset.sum_le_sum + intro uv _ + have ht := htail uv.1 uv.2 (codegreeNoiseRadius n) hr + have ht' : pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n)) + (fun ω => if PrefixCodegreeNoiseExit uv.1 uv.2 ω then 1 else 0) ≤ + 2*(prefixTime n+1 : ℝ)*Real.exp + (-(codegreeNoiseRadius n)^2/codegreeNoiseDenominator n) := by + unfold codegreeNoiseDenominator + refine le_trans ?_ ht + apply le_of_eq + congr 1 + funext ω + by_cases hω : PrefixCodegreeNoiseExit uv.1 uv.2 ω <;> + simp_all only [PrefixCodegreeNoiseExit, ite_true, ite_false] + apply ht'.trans + apply mul_le_mul_of_nonneg_left _ (by positivity) + apply Real.exp_le_exp.mpr + rw [neg_div] + exact neg_le_neg hexp + _ = (n : ℝ)^2*2*(prefixTime n+1)*Real.exp (-(n : ℝ)^(1/2500 : ℝ)) := by + simp only [Finset.sum_const,Finset.card_univ,Fintype.card_prod,Fintype.card_fin, + nsmul_eq_mul,Nat.cast_mul] + ring + _ ≤ 4*(n : ℝ)^4*Real.exp (-(n : ℝ)^(1/2500 : ℝ)) := by + have ht : (prefixTime n : ℝ) ≤ (n : ℝ)^2 := by exact_mod_cast prefixTime_le_square n + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn2 : (1 : ℝ) ≤ (n : ℝ)^2 := one_le_pow₀ hn1 + apply mul_le_mul_of_nonneg_right _ (Real.exp_pos _).le + nlinarith [sq_nonneg (n : ℝ)] + _ ≤ _ := by + rw [show (n : ℝ)^(4 : ℝ) = (n : ℝ)^4 from Real.rpow_natCast _ 4] at hbudget + exact hbudget + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeNoiseGood.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeNoiseGood.lean new file mode 100644 index 0000000000000000000000000000000000000000..466400b59638968dda1dcf4830bc39cb7206a74d --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixCodegreeNoiseGood.lean @@ -0,0 +1,80 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixCodegreeNoiseExit +import OAI.Combinatorics.TriangleRemoval.Tracking.RootedCompletionMax + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +def PrefixCodegreeNoiseGood (n : ℕ) (ω : History (Graph n) (prefixTime n)) : Prop := + ∀ u v : Fin n, ¬ PrefixCodegreeNoiseExit u v ω + +theorem prefix_codegree_noise_failure : ∀ᶠ n : ℕ in atTop, + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => if PrefixCodegreeNoiseGood n ω then 0 else 1) ≤ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := by + filter_upwards [prefix_codegree_relative_noise_bound,codegree_noise_exponent_lower, + prefixTemplateFactor_subpower 2 (by norm_num : (0 : ℝ) < 1/2500) (by norm_num : (0 : ℝ) < 1), + polynomial_logsquare_le_prefix_margin 4 4,eventually_ge_atTop (1 : ℕ)] with n ht hexp hlog hb hn + let μ := historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n) + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hr : 0 < codegreeNoiseRadius n := Real.rpow_pos_of_pos hn0 _ + have hexp' : (Real.log (n : ℝ))^2/4 ≤ codegreeNoiseRadius n^2/codegreeNoiseDenominator n := by + have hh : (1+Real.log (n : ℝ))^2 ≤ (n : ℝ)^(1/2500 : ℝ) := by + simpa only [prefixTemplateFactor,Real.rpow_two,one_mul] using hlog + have hln := Real.log_nonneg hn1 + nlinarith only [hh,hln,hexp,sq_nonneg (Real.log (n : ℝ))] + have hsingle (uv : Fin n × Fin n) : + pmfMean μ (fun ω => if PrefixCodegreeNoiseExit uv.1 uv.2 ω then 1 else 0) ≤ + 2*(prefixTime n+1 : ℝ)*Real.exp (-(Real.log (n : ℝ))^2/4) := by + have ht' : pmfMean μ (fun ω => if PrefixCodegreeNoiseExit uv.1 uv.2 ω then 1 else 0) ≤ + 2*(prefixTime n+1 : ℝ)*Real.exp (-(codegreeNoiseRadius n)^2/codegreeNoiseDenominator n) := by + unfold codegreeNoiseDenominator + refine le_trans ?_ (ht uv.1 uv.2 (codegreeNoiseRadius n) hr) + apply le_of_eq + congr 1 + funext ω + by_cases hω : PrefixCodegreeNoiseExit uv.1 uv.2 ω <;> + simp_all only [PrefixCodegreeNoiseExit, ite_true, ite_false] + apply ht'.trans + apply mul_le_mul_of_nonneg_left _ (by positivity) + apply Real.exp_le_exp.mpr + rw [neg_div,neg_div] + exact neg_le_neg hexp' + have hu := pmfMean_event_union μ Finset.univ (fun uv : Fin n × Fin n => PrefixCodegreeNoiseExit uv.1 uv.2) + have hs : (∑ uv : Fin n × Fin n, + pmfMean μ (fun ω => if PrefixCodegreeNoiseExit uv.1 uv.2 ω then 1 else 0)) ≤ + 4*(n : ℝ)^4*Real.exp (-(Real.log (n : ℝ))^2/4) := by + calc + _ ≤ ∑ _uv : Fin n × Fin n, 2*(prefixTime n+1 : ℝ)*Real.exp (-(Real.log (n : ℝ))^2/4) := + Finset.sum_le_sum (fun uv _ => hsingle uv) + _ = (n : ℝ)^2*(2*(prefixTime n+1))*Real.exp (-(Real.log (n : ℝ))^2/4) := by + simp only [Finset.sum_const, Finset.card_univ, Fintype.card_prod, + Fintype.card_fin, nsmul_eq_mul, Nat.cast_mul] + ring + _ ≤ _ := by + apply mul_le_mul_of_nonneg_right _ (Real.exp_pos _).le + have htime : (prefixTime n : ℝ) ≤ (n : ℝ)^2 := by exact_mod_cast prefixTime_le_square n + have hnt : 1 ≤ (n : ℝ)^2 := by nlinarith + nlinarith only [mul_le_mul_of_nonneg_left htime (sq_nonneg (n : ℝ)), + mul_le_mul_of_nonneg_left hnt (sq_nonneg (n : ℝ))] + rw [show (n : ℝ)^(4 : ℝ) = (n : ℝ)^4 from Real.rpow_natCast _ 4] at hb + have hf := hu.trans (hs.trans hb) + convert hf using 1 + apply congrArg (pmfMean μ) + funext ω + have he : (∃ uv ∈ (Finset.univ : Finset (Fin n × Fin n)), PrefixCodegreeNoiseExit uv.1 uv.2 ω) ↔ + ¬ PrefixCodegreeNoiseGood n ω := by simp [PrefixCodegreeNoiseGood,Prod.exists] + simp only [he, ite_not] + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixDTendstoTop.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixDTendstoTop.lean new file mode 100644 index 0000000000000000000000000000000000000000..2a3f71ca2d05703053b8b696f8eb422a2c259c4c --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixDTendstoTop.lean @@ -0,0 +1,135 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.GoodPrefixSurvivalMoments + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma prefixD_tendsto_atTop : Tendsto prefixD atTop atTop := by + apply tendsto_atTop_mono' atTop + (show ∀ᶠ n : ℕ in atTop, (n : ℝ)^(1/1000 : ℝ) ≤ prefixD n from + prefixD_eventual_envelope.mono (fun _ h => h.1)) + exact (tendsto_rpow_atTop (by norm_num : (0 : ℝ) < 1/1000)).comp + tendsto_natCast_atTop_atTop + +lemma prefixD_inv_tendsto : Tendsto (fun n => 1/prefixD n) atTop (𝓝 (0 : ℝ)) := by + simpa only [one_div,Function.comp_def] using tendsto_inv_atTop_zero.comp prefixD_tendsto_atTop + +lemma prefixD_div_n_tendsto : Tendsto (fun n => prefixD n/(n : ℝ)) atTop (𝓝 (0 : ℝ)) := by + have hupper : ∀ᶠ n : ℕ in atTop, + 0 ≤ prefixD n/(n : ℝ) ∧ prefixD n/(n : ℝ) ≤ 4*(n : ℝ)^(-(999/1000 : ℝ)) := by + filter_upwards [prefixD_eventual_envelope,eventually_ge_atTop (1 : ℕ)] with n hn hp + have hn0 : (0 : ℝ) < n := by exact_mod_cast hp + have hp0 : 0 ≤ prefixD n := le_trans (Real.rpow_nonneg (Nat.cast_nonneg n) _) hn.1 + refine ⟨div_nonneg hp0 hn0.le,?_⟩ + calc + _ ≤ (4*(n : ℝ)^(1/1000 : ℝ))/(n : ℝ) := div_le_div_of_nonneg_right hn.2 hn0.le + _ = _ := by + have hid : (n : ℝ)^(1/1000 : ℝ)/(n : ℝ) = + (n : ℝ)^(-(999/1000 : ℝ)) := by + conv_lhs => rhs; rw [← Real.rpow_one (n : ℝ)] + rw [← Real.rpow_sub hn0] + norm_num + rw [mul_div_assoc,hid] + have hl := ((tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 999/1000)).comp + tendsto_natCast_atTop_atTop).const_mul 4 + exact squeeze_zero' (hupper.mono (fun _ h => h.1)) (hupper.mono (fun _ h => h.2)) + (by simpa using hl) + +theorem goodPrefix_relative_l2_bound (c C : ℝ) (hc : 0 < c) {ε : ℝ} (hε : 0 < ε) : + ∀ᶠ n : ℕ in atTop, ∀ (G : Graph n), GoodPrefixGraph n c C G → + pmfMean (finish G) (fun H => + ((H.card : ℝ)/((G.card : ℝ)*cavityReference (prefixD n) 1)-1)^2) ≤ + (81920*(3 : ℝ)^49)/prefixD n+8/(n : ℝ)+16*(prefixD n/(n : ℝ))+ + (4*ε+ε^2) := by + filter_upwards [goodPrefix_survival_moments c C hc hε, + prefixD_tendsto_atTop.eventually (eventually_ge_atTop (1 : ℝ)), + prefix_scales_eventually_pos,eventually_ge_atTop (1 : ℕ)] with n hm hD hsc hn + intro G hG + have hD0 : 0 < prefixD n := lt_of_lt_of_le zero_lt_one hD + have hn0 : (0 : ℝ) < n := by exact_mod_cast hn + have hm0 : 0 < (G.card : ℝ) := by + rw [hG.2.1] + unfold prefixM + have := hsc.1 + positivity + have hne : G.Nonempty := Finset.card_pos.mp (by exact_mod_cast hm0) + obtain ⟨h1,h2⟩ := hm G hG hne + rw [terminal_first_focus_moment] at h1 + rw [terminal_second_focus_moment] at h2 + let q := cavityReference (prefixD n) 1 + let A := (1+2*prefixD n)^48/prefixD n^50 + have hq : 0 < q := cavityReference_pos hD0 (by norm_num) + have hq1 : q ≤ 1 := cavityReference_le_one hD0 (by norm_num) + have hq2 : q^2 = 1/(1+2*prefixD n) := by + dsimp only [q] + rw [cavityReference_sq hD0.le (by norm_num)] + simp only [cavityWeight,mul_one] + have hA : 0 ≤ A := by dsimp only [A]; positivity + have hAbound : A ≤ (3 : ℝ)^48/prefixD n^2 := collision_power_48_bound hD + have h1' : |pmfMean (finish G) (fun H => (H.card : ℝ))/(G.card : ℝ)-q| ≤ + 8192*A+ε*q := by simpa only [A,q,mul_div_assoc] using h1 + have h2' : |pmfMean (finish G) (fun H => (H.card : ℝ)^2)/(G.card : ℝ)^2-q^2| ≤ + 65536*A+8/(n : ℝ)+(2*ε+ε^2)*q^2 := by + simpa only [A,q,mul_div_assoc] using h2 + have heq : pmfMean (finish G) (fun H => + ((H.card : ℝ)/((G.card : ℝ)*q)-1)^2) = + (pmfMean (finish G) (fun H => (H.card : ℝ)^2)/(G.card : ℝ)^2-q^2)/q^2 - + 2*(pmfMean (finish G) (fun H => (H.card : ℝ))/(G.card : ℝ)-q)/q := by + rw [pmfMean_sq_sub] + simp_rw [div_pow,div_eq_mul_inv,pmfMean_mul_const] + field_simp + ring + have hb : pmfMean (finish G) (fun H => + ((H.card : ℝ)/((G.card : ℝ)*q)-1)^2) ≤ + (81920*A+8/(n : ℝ))/q^2+(4*ε+ε^2) := by + rw [heq] + have hfirst := (abs_le.mp h1').1 + have hsecond := (abs_le.mp h2').2 + have haux : (2*(8192*A+ε*q))/q ≤ (16384*A)/q^2+2*ε := by + calc + _ = 16384*A/q+2*ε := by field_simp; ring + _ ≤ _ := add_le_add + (div_le_div_of_nonneg_left (by positivity) (sq_pos_of_pos hq) + (by nlinarith : q^2 ≤ q)) le_rfl + have hlinear : -2*(pmfMean (finish G) (fun H => (H.card : ℝ))/(G.card : ℝ)-q)/q ≤ + (2*(8192*A+ε*q))/q := + div_le_div_of_nonneg_right (by linarith) hq.le + calc + _ ≤ (65536*A+8/(n : ℝ)+(2*ε+ε^2)*q^2)/q^2 + + (2*(8192*A+ε*q))/q := by + apply add_le_add + · exact div_le_div_of_nonneg_right hsecond (sq_nonneg q) + · simpa only [neg_mul,neg_div] using hlinear + _ ≤ (65536*A+8/(n : ℝ)+(2*ε+ε^2)*q^2)/q^2+ + ((16384*A)/q^2+2*ε) := add_le_add le_rfl haux + _ = _ := by field_simp; ring + apply hb.trans + rw [hq2] + have hDA : (81920*A)*(1+2*prefixD n) ≤ (81920*(3 : ℝ)^49)/prefixD n := by + calc + _ ≤ (81920*((3 : ℝ)^48/prefixD n^2))*(3*prefixD n) := + mul_le_mul (mul_le_mul_of_nonneg_left hAbound (by norm_num)) + (by linarith) (by positivity) (by positivity) + _ = _ := by rw [show 49 = 48+1 by norm_num,pow_add,pow_one]; field_simp + have hraw : (81920*A+8/(n : ℝ))/(1/(1+2*prefixD n)) = + (81920*A)*(1+2*prefixD n)+8/(n : ℝ)+16*(prefixD n/(n : ℝ)) := by + field_simp + ring + rw [hraw] + linarith + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeForcingBound.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeForcingBound.lean new file mode 100644 index 0000000000000000000000000000000000000000..b8cceb5e74e905161cbcdcda0c7f878be747ad95 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeForcingBound.lean @@ -0,0 +1,158 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixTriangleRadius + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +noncomputable def prefixEdgeForcingBound (n : ℕ) : ℝ := + 4/(n : ℝ)+codegreeNoiseRadius n+16*prefixTriangleRadius n*Real.log n+ + 4/prefixD n+24*Real.log n/(n : ℝ) + +lemma prefix_edge_forcing_power : ∀ᶠ n : ℕ in atTop, + 0 ≤ prefixEdgeForcingBound n ∧ + prefixEdgeForcingBound n ≤ 49*(1+Real.log (n : ℝ))*prefixTriangleRadius n := by + filter_upwards [prefixD_eventual_envelope,eventually_ge_atTop (1 : ℕ)] with n hD hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have ht : 0 ≤ prefixTriangleRadius n := Real.rpow_nonneg hn0.le _ + have hl := Real.log_nonneg hn1 + have hD0 := lt_of_lt_of_le (Real.rpow_pos_of_pos hn0 _) hD.1 + have hi : 1/(n : ℝ) ≤ prefixTriangleRadius n := by + simpa only [Real.rpow_neg_one,one_div,prefixTriangleRadius] using + (Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num : (-1 : ℝ) ≤ -3/80000)) + have hno0 : 0 ≤ codegreeNoiseRadius n := Real.rpow_nonneg hn0.le _ + have hno : codegreeNoiseRadius n ≤ prefixTriangleRadius n := + Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hdi : 1/prefixD n ≤ prefixTriangleRadius n := by + apply (div_le_div_of_nonneg_left zero_le_one (Real.rpow_pos_of_pos hn0 _) hD.1).trans + rw [one_div,← Real.rpow_neg hn0.le] + exact Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hg := mul_le_mul_of_nonneg_left hi (show 0 ≤ 24*Real.log (n : ℝ) by positivity) + constructor + · unfold prefixEdgeForcingBound + positivity + · unfold prefixEdgeForcingBound + simp only [div_eq_mul_inv,one_mul] at hi hdi hg ⊢ + nlinarith only [hi,hno,hdi,hg,ht,mul_nonneg ht hl] + +lemma edge_stability_log_bounds {L S : ℝ} (hL : 0 ≤ L) (hS : 0 ≤ S) (hS' : S ≤ 4*L) : + 0 ≤ 1+2*S ∧ 1+2*S ≤ 8*(1+L) ∧ + (1+2*S)*S ≤ 32*(1+L)^2 ∧ + (1+2*S)*S*6 ≤ 192*(1+L)^2 := by + have hp := mul_le_mul (show 1+2*S ≤ 8*(1+L) by linarith) hS' + hS (show 0 ≤ 8*(1+L) by positivity) + have hpoly : 8*(1+L)*(4*L) ≤ 32*(1+L)^2 := by nlinarith only [hL] + refine ⟨by positivity,by linarith,hp.trans hpoly,?_⟩ + nlinarith only [hp,hpoly] + +lemma edge_stability_gain_bound {K : ℕ} {L S F r : ℝ} + (hL : 0 ≤ L) (hS : 0 ≤ S) (hS' : S ≤ 4*L) (hF : 0 ≤ F) + (hr : 0 ≤ r) (hF' : F ≤ 49*(1+L)*r) : + 2*(∑ j ∈ Finset.range K, ((1+2*S)*S*6)^j)*(1+2*S)*F ≤ + (784*(K : ℝ)*192^K)*(1+L)^(2*K+2)*r := by + obtain ⟨hB,hB',_,hA⟩ := edge_stability_log_bounds hL hS hS' + have hbase : 1 ≤ 192*(1+L)^2 := by nlinarith only [hL,sq_nonneg L] + have hsum : (∑ j ∈ Finset.range K, ((1+2*S)*S*6)^j) ≤ + (K : ℝ)*(192*(1+L)^2)^K := by + calc + _ ≤ ∑ _j ∈ Finset.range K, (192*(1+L)^2)^K := by + apply Finset.sum_le_sum + intro j hj + exact (pow_le_pow_left₀ (by positivity) hA j).trans + (pow_le_pow_right₀ hbase (Finset.mem_range.mp hj).le) + _ = _ := by simp + have hs0 : 0 ≤ ∑ j ∈ Finset.range K, ((1+2*S)*S*6)^j := by positivity + calc + _ ≤ 2*(∑ j ∈ Finset.range K, ((1+2*S)*S*6)^j)*(1+2*S)*(49*(1+L)*r) := + mul_le_mul le_rfl hF' hF (mul_nonneg (mul_nonneg (by norm_num) hs0) hB) + _ ≤ 2*((K : ℝ)*(192*(1+L)^2)^K)*(8*(1+L))*(49*(1+L)*r) := by + apply mul_le_mul_of_nonneg_right _ (mul_nonneg (by positivity) hr) + gcongr + _ = _ := by rw [mul_pow,← pow_mul,pow_add,pow_succ, pow_succ]; ring + +theorem prefix_edge_stability_budget {γ : ℝ} (hγ : 0 < γ) : + ∃ K : ℕ, 0 < K ∧ ∀ᶠ n : ℕ in atTop, ∀ S : ℝ, + 0 ≤ S → S ≤ 4*Real.log n → + ((1+2*S)*S*(n : ℝ)^(-γ))^K*(n : ℝ)^2 ≤ 1/2 ∧ + 2*(∑ j ∈ Finset.range K, ((1+2*S)*S*6)^j)*(1+2*S)*prefixEdgeForcingBound n ≤ + prefixEdgeRadius n/2 := by + obtain ⟨K,hK⟩ := exists_nat_gt (3/γ) + have hKγ : 3 < γ*(K : ℝ) := by + have hh := (div_lt_iff₀ hγ).mp hK + nlinarith only [hh] + have hK0 : 0 < K := by + have hkR : (0 : ℝ) < K := lt_trans (by positivity) hK + exact_mod_cast hkR + let C : ℝ := 784*(K : ℝ)*192^K + have hC : 0 < C := by dsimp [C]; positivity + have hremexp : 0 < γ*(K : ℝ)-2 := by linarith + refine ⟨K,hK0,?_⟩ + filter_upwards [prefix_edge_forcing_power, + prefixTemplateFactor_subpower (2*K : ℝ) hremexp + (by positivity : (0 : ℝ) < 1/(2*32^K)), + prefixTemplateFactor_subpower (2*K+2 : ℝ) + (by norm_num : (0 : ℝ) < 1/80000) (by positivity : (0 : ℝ) < 1/(2*C)), + eventually_ge_atTop (1 : ℕ)] with n hF hrem hgain hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hl := Real.log_nonneg hn1 + have htr0 : 0 ≤ prefixTriangleRadius n := Real.rpow_nonneg hn0.le _ + have hrem' : (1+Real.log (n : ℝ))^(2*K) ≤ + 1/(2*32^K)*(n : ℝ)^(γ*(K : ℝ)-2) := by + convert hrem using 1 + simp only [prefixTemplateFactor] + rw [← Real.rpow_natCast] + congr 1 + push_cast + ring + have hgain' : (1+Real.log (n : ℝ))^(2*K+2) ≤ + 1/(2*C)*(n : ℝ)^(1/80000 : ℝ) := by + convert hgain using 1 + simp only [prefixTemplateFactor] + rw [← Real.rpow_natCast] + congr 1 + push_cast + ring + intro S hS hS' + obtain ⟨hB,hB',hA,_⟩ := edge_stability_log_bounds hl hS hS' + constructor + · calc + _ ≤ (32*(1+Real.log (n : ℝ))^2*(n : ℝ)^(-γ))^K*(n : ℝ)^2 := by gcongr + _ = 32^K*(1+Real.log (n : ℝ))^(2*K)*(n : ℝ)^(-γ*(K : ℝ)+2) := by + rw [mul_pow,mul_pow,← pow_mul] + have he : ((n : ℝ)^(-γ))^K*(n : ℝ)^2 = (n : ℝ)^(-γ*(K : ℝ)+2) := by + rw [← Real.rpow_natCast ((n : ℝ)^(-γ)) K,← Real.rpow_mul hn0.le, + ← Real.rpow_natCast (n : ℝ) 2,← Real.rpow_add hn0] + norm_num + rw [mul_assoc (32^K*(1+Real.log (n : ℝ))^(2*K)),he] + _ ≤ 32^K*(1/(2*32^K)*(n : ℝ)^(γ*(K : ℝ)-2))*(n : ℝ)^(-γ*(K : ℝ)+2) := by gcongr + _ = 1/2 := by + rw [mul_assoc, mul_assoc,← Real.rpow_add hn0] + simp only [show γ*(K : ℝ)-2+(-γ*(K : ℝ)+2) = 0 by ring,Real.rpow_zero,mul_one] + field_simp + · have hb := edge_stability_gain_bound hl hS hS' hF.1 + htr0 hF.2 (K := K) + apply hb.trans + change C*(1+Real.log (n : ℝ))^(2*K+2)*prefixTriangleRadius n ≤ _ + calc + _ ≤ C*(1/(2*C)*(n : ℝ)^(1/80000 : ℝ))*prefixTriangleRadius n := by gcongr + _ = prefixEdgeRadius n/2 := by + have he : (n : ℝ)^(1/80000 : ℝ)*prefixTriangleRadius n = prefixEdgeRadius n := by + unfold prefixTriangleRadius prefixEdgeRadius + rw [← Real.rpow_add hn0] + norm_num + calc + _ = (C*(1/(2*C)))*((n : ℝ)^(1/80000 : ℝ)*prefixTriangleRadius n) := by ring + _ = prefixEdgeRadius n/2 := by rw [he]; field_simp + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeRadius.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeRadius.lean new file mode 100644 index 0000000000000000000000000000000000000000..d761084ba53ffb79375a1f89f5377391a2fa4370 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeRadius.lean @@ -0,0 +1,145 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Asymptotics.EarlyDensityOne +import OAI.Combinatorics.TriangleRemoval.Asymptotics.DensityEdgeSafe + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter +open Classical + +namespace SharpTerminalLeave + +noncomputable def prefixEdgeRadius (n : ℕ) : ℝ := (n : ℝ)^(-1/40000 : ℝ) + +lemma early_density_positive {n : ℕ} (hn : 0 < n) + (hp : 1/(n : ℝ) ≤ prefixDensity n) {i : ℕ} (hi : i ≤ prefixTime n) : + 0 < earlyDensity n i := by + have hnR : (0 : ℝ) < n := by exact_mod_cast hn + exact lt_of_lt_of_le (by positivity) (hp.trans (earlyDensity_antitone n hi)) + +lemma early_density_log_bound {n i : ℕ} (hn : 0 < n) + (hp : 1/(n : ℝ) ≤ prefixDensity n) (hi : i ≤ prefixTime n) : + -Real.log (earlyDensity n i) ≤ Real.log n := by + have hnR : (0 : ℝ) < n := by exact_mod_cast hn + have hh : Real.log (1/(n : ℝ)) ≤ Real.log (earlyDensity n i) := + Real.log_le_log (by positivity) (hp.trans (earlyDensity_antitone n hi)) + rw [one_div,Real.log_inv] at hh + linarith + +lemma early_density_survival_budget {n i : ℕ} (hn : 2 ≤ n) + (hp : 1/(n : ℝ) ≤ prefixDensity n) (hi : i ≤ prefixTime n) + {b η : ℝ} (hb : 0 ≤ b) (hη : 0 ≤ η) : + b*(1-earlyDensity n 0)/earlyDensity n 0+b*(6/(n : ℝ)^2)/earlyDensity n i+ + 2*b*η*(-Real.log (earlyDensity n i))+b^2/(n*earlyDensity n i^2) ≤ + 8*b/(n : ℝ)+2*b*η*Real.log n+b^2/prefixD n := by + have hnR : (2 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := by linarith + have hp0 : 0 < earlyDensity n 0 := early_density_positive (by omega) hp (Nat.zero_le _) + have hpi := early_density_positive (by omega) hp hi + have hpT : 0 < prefixDensity n := early_density_positive (by omega) hp le_rfl + have hlow : 1/(n : ℝ) ≤ earlyDensity n i := hp.trans (earlyDensity_antitone n hi) + have hab : b*(1-earlyDensity n 0)/earlyDensity n 0 ≤ 2*b/(n : ℝ) := by + have hz : earlyDensity n 0 = 1-1/(n : ℝ) := by simp [earlyDensity] + have hhalf : (1 : ℝ)/2 ≤ earlyDensity n 0 := by + rw [hz] + have hinv : 1/(n : ℝ) ≤ 1/2 := (div_le_div_iff₀ hn0 (by norm_num)).mpr (by linarith) + linarith + apply (div_le_iff₀ hp0).mpr + rw [hz] + have hm := mul_le_mul_of_nonneg_left hhalf (show 0 ≤ 2*b/(n : ℝ) by positivity) + rw [hz] at hm + simp only [div_eq_mul_inv] at hm ⊢ + nlinarith only [hm] + have hdisc : b*(6/(n : ℝ)^2)/earlyDensity n i ≤ 6*b/(n : ℝ) := by + calc + _ ≤ b*(6/(n : ℝ)^2)/(1/(n : ℝ)) := + div_le_div_of_nonneg_left (by positivity) (by positivity) hlow + _ = _ := by field_simp + have hlog : 2*b*η*(-Real.log (earlyDensity n i)) ≤ 2*b*η*Real.log n := + mul_le_mul_of_nonneg_left (early_density_log_bound (by omega) hp hi) (by positivity) + have hlast : b^2/(n*earlyDensity n i^2) ≤ b^2/prefixD n := by + apply div_le_div_of_nonneg_left (sq_nonneg b) (by unfold prefixD; positivity) + exact mul_le_mul_of_nonneg_left + (pow_le_pow_left₀ hpT.le (earlyDensity_antitone n hi) 2) hn0.le + simp only [div_eq_mul_inv] at hab hdisc hlog hlast ⊢ + linarith only [hab,hdisc,hlog,hlast] + +lemma prefix_survival_budget_small : ∀ᶠ n : ℕ in atTop, + 0 ≤ prefixEdgeRadius n ∧ prefixEdgeRadius n ≤ 1/2 ∧ + ∀ b : ℝ, 0 ≤ b → b ≤ (n : ℝ)^(1/200000 : ℝ) → + 8*b/(n : ℝ)+2*b*prefixEdgeRadius n*Real.log n+b^2/prefixD n ≤ 1 := by + have hlim : Tendsto (fun n : ℕ => + 8*(n : ℝ)^(-1/2 : ℝ)+2*(n : ℝ)^(-1/100000 : ℝ)+(n : ℝ)^(-1/2000 : ℝ)) + atTop (nhds 0) := by + have ha := (tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1/2)).comp tendsto_natCast_atTop_atTop + have hb := (tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1/100000)).comp tendsto_natCast_atTop_atTop + have hc := (tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1/2000)).comp tendsto_natCast_atTop_atTop + convert ((ha.const_mul 8).add (hb.const_mul 2)).add hc using 1 <;> norm_num + have hrad : Tendsto prefixEdgeRadius atTop (nhds 0) := by + change Tendsto (fun n : ℕ => (n : ℝ)^(-1/40000 : ℝ)) atTop (nhds 0) + simpa only [Function.comp_def,neg_div] using + (tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1/40000)).comp + (tendsto_natCast_atTop_atTop (R := ℝ)) + filter_upwards [hlim.eventually_le_const (by norm_num : (0 : ℝ) < 1), + hrad.eventually_le_const (by norm_num : (0 : ℝ) < 1/2), + prefixTemplateFactor_subpower 1 (by norm_num : (0 : ℝ) < 1/200000) zero_lt_one, + prefixD_eventual_envelope,eventually_ge_atTop (1 : ℕ)] with n hlim hrad hlog hD hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hlog0 : 0 ≤ Real.log (n : ℝ) := Real.log_nonneg hn1 + have hlog' : Real.log (n : ℝ) ≤ (n : ℝ)^(1/200000 : ℝ) := by + simp only [prefixTemplateFactor,Real.rpow_one,one_mul] at hlog + linarith + refine ⟨Real.rpow_nonneg hn0.le _,hrad,?_⟩ + intro b hb hbhigh + have hfirst : b/(n : ℝ) ≤ (n : ℝ)^(-1/2 : ℝ) := by + calc + _ ≤ (n : ℝ)^(1/200000 : ℝ)/(n : ℝ) := div_le_div_of_nonneg_right hbhigh hn0.le + _ = (n : ℝ)^((1/200000 : ℝ)-1) := by rw [Real.rpow_sub hn0,Real.rpow_one] + _ ≤ _ := Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hmiddle : b*prefixEdgeRadius n*Real.log n ≤ (n : ℝ)^(-1/100000 : ℝ) := by + calc + _ ≤ ((n : ℝ)^(1/200000 : ℝ)*(n : ℝ)^(-1/40000 : ℝ))*(n : ℝ)^(1/200000 : ℝ) := + mul_le_mul (mul_le_mul_of_nonneg_right hbhigh (Real.rpow_nonneg hn0.le _)) + hlog' hlog0 (by positivity) + _ = (n : ℝ)^((1/200000 : ℝ)-1/40000+1/200000) := by + rw [← Real.rpow_add hn0,← Real.rpow_add hn0]; congr 1; norm_num + _ ≤ _ := Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hlast : b^2/prefixD n ≤ (n : ℝ)^(-1/2000 : ℝ) := by + calc + _ ≤ ((n : ℝ)^(1/200000 : ℝ))^2/(n : ℝ)^(1/1000 : ℝ) := + div_le_div₀ (by positivity) (pow_le_pow_left₀ hb hbhigh 2) (by positivity) hD.1 + _ = (n : ℝ)^((1/200000 : ℝ)*2-1/1000) := by + rw [← Real.rpow_mul_natCast hn0.le,Real.rpow_sub hn0]; norm_num + _ ≤ _ := Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + simp only [div_eq_mul_inv] at hfirst hlast ⊢ + nlinarith only [hlim,hfirst,hmiddle,hlast] + +theorem prefix_killed_survival : ∀ᶠ n : ℕ in atTop, + ∀ j ≤ prefixTime n, ∀ F : Graph n, (F.card : ℝ) ≤ (n : ℝ)^(1/200000 : ℝ) → + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) j) + (fun ω => if historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) + (prefixTime n) j ω then intact F (ω (historyIndex (prefixTime n) j)) else 0) ≤ + Real.exp 1 * earlyDensity n j^F.card := by + filter_upwards [prefix_survival_budget_small,prefixDensity_eventually_inverse_lower, + eventually_ge_atTop (2 : ℕ)] with n hbudget hp hn + intro j hj F hF + have hk := density_killed_survival (PMF.pure (completeGraph n)) (earlyDensity n) + (prefixEdgeRadius n) (prefixTime n) j hj (by omega) + (fun i hi => early_density_positive (by omega) hp hi) + (fun i _ => earlyDensity_succ n i) (earlyDensity_le_one n 0) hbudget.1 hbudget.2.1 F + apply hk.trans + rw [mul_comm (Real.exp 1)] + apply mul_le_mul_of_nonneg_left _ (pow_nonneg (early_density_positive (by omega) hp hj).le _) + apply Real.exp_le_exp.mpr + exact (early_density_survival_budget hn hp hj (Nat.cast_nonneg _) hbudget.1).trans + (hbudget.2.2 F.card (Nat.cast_nonneg _) hF) + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeResidualBounds.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeResidualBounds.lean new file mode 100644 index 0000000000000000000000000000000000000000..3331c6731f254356f59d61b2211bb9ea89dda40a --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEdgeResidualBounds.lean @@ -0,0 +1,98 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.DensityGlobalSpectral +import OAI.Combinatorics.TriangleRemoval.Asymptotics.DensityGlobalInfty +import OAI.Combinatorics.TriangleRemoval.Process.HistoryGraphNested + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical Matrix + +namespace SharpTerminalLeave + +theorem prefix_edge_residual_bounds : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ∀ hω : ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support, + PrefixRootedUpper 8001 n ω → PrefixBalancedNoise 8001 n ω → + ∀ j ≤ prefixTime n, + historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) (prefixTime n) j ω → + ∀ i < j, ∀ z : CompleteEdge n → ℝ, + activeLinf (graphActive (ω (historyIndex (prefixTime n) i))) (prefixEdgeResidual ω hω i z) ≤ + 6*activeLinf (graphActive (ω (historyIndex (prefixTime n) i))) z ∧ + activeL2 (graphActive (ω (historyIndex (prefixTime n) i))) (prefixEdgeResidual ω hω i z) ≤ + (2*prefixSpectralError n (1/80000))*activeL2 (graphActive (ω (historyIndex (prefixTime n) i))) z := by + filter_upwards [prefix_degree_wheel_tracking,prefixD_eventual_envelope, + triangle_closure_budget,prefixDensity_eventually_inverse_lower, + eventually_ge_atTop (1 : ℕ)] with n htrack hD hη hp hn + intro ω hω hroot hnoise j hj hsafe i hi z + have hn0 : 0 < n := by omega + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hnR : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hit : i ≤ prefixTime n := by omega + let G := ω (historyIndex (prefixTime n) i) + let D := earlyTemplateScale 1 2 n i + have hG : G ⊆ completeGraph n := history_valid_all ω hω i + have hs : densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n) G := hsafe i hi + have hw := htrack ω hω hroot hnoise i hit (fun k hk => hsafe k (by omega)) + have hpi := early_density_positive hn0 hp hit + have hD0 : 0 < D := by dsimp [D,earlyTemplateScale]; positivity + have hDmin : 1 ≤ prefixD n := (Real.one_le_rpow hn1 (by norm_num : (0 : ℝ) ≤ 1/1000)).trans hD.1 + have hDle : prefixD n ≤ D := by + have hp0 := early_density_positive hn0 hp (le_refl (prefixTime n)) + have hh := mul_le_mul_of_nonneg_left + (pow_le_pow_left₀ hp0.le (earlyDensity_antitone n hit) 2) hnR.le + simpa only [prefixD,D,earlyTemplateScale,pow_one,earlyDensity_prefix] using hh + have hw0 : 0 ≤ prefixWheelRadius n := Real.rpow_nonneg hnR.le _ + have hw1 : prefixWheelRadius n ≤ 1 := Real.rpow_le_one_of_one_le_of_nonpos hn1 (by norm_num) + have hηw : prefixEdgeRadius n ≤ prefixWheelRadius n := + Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hrow : ∀ u v : Fin n, {u,v} ∈ G → + |(currentCodegree G u v : ℝ)-D| ≤ (n : ℝ)^(-(1/80000 : ℝ))*D := by + intro u v he + have hh := hs.2.2 {u,v} he + rw [triangleDegree_eq_codegree hG he] at hh + have hh' : |(currentCodegree G u v : ℝ)-D| ≤ prefixEdgeRadius n*D := by + simpa only [D,earlyTemplateScale,pow_one] using hh + simpa only [prefixWheelRadius,neg_div] using hh'.trans (mul_le_mul_of_nonneg_right hηw hD0.le) + have hcyc : ∀ b, 3 ≤ b → b ≤ 8000 → ∀ u : Fin n, + |linkCycleCount b G u-D^b| ≤ (n : ℝ)^(-(1/80000 : ℝ))*D^b := by + intro b hb hb' u + have hh := hw.2 b hb hb' u + have hh' : |linkCycleCount b G u/D^b-1| ≤ prefixWheelRadius n/2 := by + simpa only [D,earlyTemplateScale,pow_one,G] using hh + have hpow := pow_pos hD0 b + have he : linkCycleCount b G u/D^b-1 = (linkCycleCount b G u-D^b)/D^b := by field_simp + rw [he,abs_div,abs_of_pos hpow] at hh' + have hh'' := (div_le_iff₀ hpow).mp hh' + simpa only [prefixWheelRadius,neg_div] using hh''.trans (mul_le_mul_of_nonneg_right (by linarith only [hw0] : prefixWheelRadius n/2 ≤ prefixWheelRadius n) hpow.le) + have hne : ∀ u : Fin n, (neighbors G u).Nonempty := by + intro u + have hh := (abs_le.mp (hw.1 u)).1 + have hpos : 0 < (n : ℝ)*earlyDensity n i := mul_pos hnR hpi + have hc : (0 : ℝ) < currentDegree G u := by + have hr : 0 < (currentDegree G u : ℝ)/((n : ℝ)*earlyDensity n i) := by linarith only [hh,hw1] + exact (div_pos_iff.mp hr).resolve_right (by intro h; linarith only [h.2,hpos]) |>.1 + apply Finset.card_pos.mp + exact_mod_cast hc + have hrow2 : ∀ u v : Fin n, {u,v} ∈ G → (currentCodegree G u v : ℝ) ≤ 2*D := by + intro u v he + have hp0 : 0 ≤ (n : ℝ)*earlyDensity n i^2 := by positivity + simpa only [D,earlyTemplateScale,pow_one] using densityEdgeSafe_pair_upper hp0 (show prefixEdgeRadius n ≤ 1 by linarith only [hη.2.1]) hs u v he + constructor + · open scoped Matrix.Norms.Operator in + have hh := (graphMatrixLift_linf G hG (D⁻¹ • globalLinkAdjacency G hG-globalStarAverage G) z).trans + (mul_le_mul_of_nonneg_right (density_residual_infty G hG D hD0 hrow2) (apply_nonneg _ _)) + exact hh + · open scoped Matrix.Norms.L2Operator in + have hh := (graphMatrixLift_l2 G hG (D⁻¹ • globalLinkAdjacency G hG-globalStarAverage G) z).trans + (mul_le_mul_of_nonneg_right (density_global_spectral G hG D (1/80000) hDle hDmin (by simpa only [prefixWheelRadius,neg_div] using hw1) hne hrow hcyc) + (apply_nonneg _ _)) + exact hh + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddings.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddings.lean new file mode 100644 index 0000000000000000000000000000000000000000..c2320ae3223c030e5fa9eff8fdb8f1153ad2f4b9 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddings.lean @@ -0,0 +1,150 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.RealCardFiberBound + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +namespace SharpTerminalLeave +section OrderedEmbeddings +variable {V : Type*} [Fintype V] [DecidableEq V] +variable {N : ℕ} (J : SimpleGraph (Fin N)) (G : SimpleGraph V) +variable [DecidableRel J.Adj] [DecidableRel G.Adj] + +noncomputable def prefixEmbeddings (k : ℕ) (hk : k ≤ N) : Finset (Fin k ↪ V) := + Finset.univ.filter (fun φ => ∀ i j : Fin k, + J.Adj (Fin.castLE hk i) (Fin.castLE hk j) → G.Adj (φ i) (φ j)) + +variable {G J} + +omit [DecidableEq V] in +lemma mem_prefixEmbeddings {k : ℕ} {hk : k ≤ N} (φ : Fin k ↪ V) : + φ ∈ prefixEmbeddings J G k hk ↔ ∀ i j : Fin k, + J.Adj (Fin.castLE hk i) (Fin.castLE hk j) → G.Adj (φ i) (φ j) := by + simp only [prefixEmbeddings,Finset.mem_filter,Finset.mem_univ,true_and] + +def dropLastEmbedding {k : ℕ} (φ : Fin (k+1) ↪ V) : Fin k ↪ V := + ⟨fun i => φ i.castSucc,φ.injective.comp (Fin.castSucc_injective k)⟩ + +omit [Fintype V] [DecidableEq V] in +@[simp] lemma dropLastEmbedding_apply {k : ℕ} (φ : Fin (k+1) ↪ V) (i : Fin k) : + dropLastEmbedding φ i = φ i.castSucc := rfl + +omit [DecidableEq V] in +lemma dropLastEmbedding_mem {k : ℕ} (hk : k+1 ≤ N) {φ : Fin (k+1) ↪ V} + (hφ : φ ∈ prefixEmbeddings J G (k+1) hk) : + dropLastEmbedding φ ∈ prefixEmbeddings J G k (by omega) := by + apply (mem_prefixEmbeddings _).mpr + intro i j hij + apply (mem_prefixEmbeddings φ).mp hφ i.castSucc j.castSucc + exact hij + +def compatibleNext (J : SimpleGraph (Fin N)) (G : SimpleGraph V) + [DecidableRel J.Adj] [DecidableRel G.Adj] {k : ℕ} (hk : k+1 ≤ N) + (ψ : Fin k ↪ V) : Finset V := Finset.univ.filter (fun z => + ∀ i : Fin k, J.Adj (Fin.castLE (by omega) i) ⟨k,by omega⟩ → G.Adj (ψ i) z) + +lemma embedding_fiber_le {k : ℕ} (hk : k+1 ≤ N) (ψ : Fin k ↪ V) : + ((prefixEmbeddings J G (k+1) hk).filter (fun φ => dropLastEmbedding φ = ψ)).card ≤ + (compatibleNext J G hk ψ).card := by + let s := (prefixEmbeddings J G (k+1) hk).filter (fun φ => dropLastEmbedding φ = ψ) + have hinj : Set.InjOn (fun φ : Fin (k+1) ↪ V => φ (Fin.last k)) s := by + intro φ hφ χ hχ he + have hφr := (Finset.mem_filter.mp hφ).2 + have hχr := (Finset.mem_filter.mp hχ).2 + apply Function.Embedding.ext + intro i + refine Fin.lastCases he (fun j => ?_) i + change dropLastEmbedding φ j = dropLastEmbedding χ j + rw [hφr,hχr] + calc + s.card = (s.image (fun φ => φ (Fin.last k))).card := (Finset.card_image_iff.mpr hinj).symm + _ ≤ _ := Finset.card_le_card (by + intro z hz + obtain ⟨φ,hφ,rfl⟩ := Finset.mem_image.mp hz + obtain ⟨hφ,hψ⟩ := Finset.mem_filter.mp hφ + apply Finset.mem_filter.mpr + refine ⟨Finset.mem_univ _,?_⟩ + intro i hi + have hh := (mem_prefixEmbeddings φ).mp hφ i.castSucc (Fin.last k) hi + change G.Adj (dropLastEmbedding φ i) _ at hh + rw [hψ] at hh + exact hh) + +theorem prefixEmbeddings_succ_bound {k : ℕ} (hk : k+1 ≤ N) (b : ℝ) + (hb : ∀ ψ ∈ prefixEmbeddings J G k (by omega), + ((compatibleNext J G hk ψ).card : ℝ) ≤ b) : + ((prefixEmbeddings J G (k+1) hk).card : ℝ) ≤ + b * (prefixEmbeddings J G k (by omega)).card := by + apply real_card_le_fiber_bound _ _ dropLastEmbedding + (fun _ hφ => dropLastEmbedding_mem hk hφ) b + intro ψ hψ + exact (Nat.cast_le.mpr (embedding_fiber_le hk ψ)).trans (hb ψ hψ) + +omit [DecidableEq V] in +lemma prefixEmbeddings_zero : (prefixEmbeddings J G 0 (Nat.zero_le N)).card = 1 := by + have h : prefixEmbeddings J G 0 (Nat.zero_le N) = Finset.univ := by + ext φ + simp [prefixEmbeddings] + rw [h,Finset.card_univ] + exact Fintype.card_unique + +theorem prefixEmbeddings_product_bound (b : ℕ → ℝ) (hb0 : ∀ k < N, 0 ≤ b k) + (hb : ∀ k (hk : k+1 ≤ N), ∀ ψ ∈ prefixEmbeddings J G k (by omega), + ((compatibleNext J G hk ψ).card : ℝ) ≤ b k) + (k : ℕ) (hk : k ≤ N) : + ((prefixEmbeddings J G k hk).card : ℝ) ≤ ∏ i ∈ Finset.range k, b i := by + induction k with + | zero => simp only [prefixEmbeddings_zero,Nat.cast_one,Finset.range_zero, + Finset.prod_empty,le_refl] + | succ k ih => + calc + _ ≤ b k * (prefixEmbeddings J G k (by omega)).card := + prefixEmbeddings_succ_bound hk (b k) (hb k hk) + _ ≤ b k * ∏ i ∈ Finset.range k, b i := + mul_le_mul_of_nonneg_left (ih (by omega)) (hb0 k (by omega)) + _ = _ := by rw [Finset.prod_range_succ,mul_comm] + +omit [DecidableEq V] in + +theorem compatibleNext_le_codegree {k : ℕ} (hk : k+1 ≤ N) (ψ : Fin k ↪ V) + (i j : Fin k) (hij : i ≠ j) + (hi : J.Adj (Fin.castLE (by omega) i) ⟨k,by omega⟩) + (hj : J.Adj (Fin.castLE (by omega) j) ⟨k,by omega⟩) + (D : ℝ) (hD : ∀ x y : V, x ≠ y → + ((Finset.univ.filter (fun z => G.Adj x z ∧ G.Adj y z)).card : ℝ) ≤ D) : + ((compatibleNext J G hk ψ).card : ℝ) ≤ D := by + have hs : compatibleNext J G hk ψ ⊆ + Finset.univ.filter (fun z => G.Adj (ψ i) z ∧ G.Adj (ψ j) z) := by + intro z hz + have hh := (Finset.mem_filter.mp hz).2 + exact Finset.mem_filter.mpr ⟨Finset.mem_univ _,hh i hi,hh j hj⟩ + exact (Nat.cast_le.mpr (Finset.card_le_card hs)).trans (hD _ _ (ψ.injective.ne hij)) + +omit [DecidableEq V] in +theorem compatibleNext_le_degree {k : ℕ} (hk : k+1 ≤ N) (ψ : Fin k ↪ V) + (i : Fin k) (hi : J.Adj (Fin.castLE (by omega) i) ⟨k,by omega⟩) + (Δ : ℝ) (hΔ : ∀ x : V, ((Finset.univ.filter (G.Adj x)).card : ℝ) ≤ Δ) : + ((compatibleNext J G hk ψ).card : ℝ) ≤ Δ := by + have hs : compatibleNext J G hk ψ ⊆ Finset.univ.filter (G.Adj (ψ i)) := by + intro z hz + exact Finset.mem_filter.mpr ⟨Finset.mem_univ _,(Finset.mem_filter.mp hz).2 i hi⟩ + exact (Nat.cast_le.mpr (Finset.card_le_card hs)).trans (hΔ _) + +omit [DecidableEq V] in +lemma compatibleNext_le_order {k : ℕ} (hk : k+1 ≤ N) (ψ : Fin k ↪ V) : + (compatibleNext J G hk ψ).card ≤ Fintype.card V := Finset.card_le_univ _ + +end OrderedEmbeddings +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddingsAfterRoots.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddingsAfterRoots.lean new file mode 100644 index 0000000000000000000000000000000000000000..d29c7e66918ddacf02c3f078c771353f0ad13af1 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddingsAfterRoots.lean @@ -0,0 +1,78 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixEmbeddings + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +namespace SharpTerminalLeave +section OrderedBirthCounts +variable {V : Type*} [Fintype V] [DecidableEq V] +variable {N : ℕ} (J : SimpleGraph (Fin N)) (G : SimpleGraph V) +variable [DecidableRel J.Adj] [DecidableRel G.Adj] + +theorem prefixEmbeddings_after_roots (R k : ℕ) (hR : R ≤ k) (hk : k ≤ N) + (D : ℝ) (hD0 : 0 ≤ D) + (hD : ∀ x y : V, x ≠ y → + ((Finset.univ.filter (fun z => G.Adj x z ∧ G.Adj y z)).card : ℝ) ≤ D) + (hborn : ∀ i, R ≤ i → (hi : i+1 ≤ N) → ∃ u v : Fin i, + u ≠ v ∧ J.Adj (Fin.castLE (by omega) u) ⟨i,by omega⟩ ∧ + J.Adj (Fin.castLE (by omega) v) ⟨i,by omega⟩) : + ((prefixEmbeddings J G k hk).card : ℝ) ≤ + ((prefixEmbeddings J G R (hR.trans hk)).card : ℝ) * D ^ (k-R) := by + obtain ⟨d,rfl⟩ := Nat.exists_eq_add_of_le hR + induction d with + | zero => simp + | succ d ih => + have hrec := prefixEmbeddings_succ_bound (J := J) (G := G) hk D (by + intro ψ _ + obtain ⟨u,v,huv,hu,hv⟩ := hborn (R+d) (by omega) hk + exact compatibleNext_le_codegree hk ψ u v huv hu hv D hD) + have hm := mul_le_mul_of_nonneg_left (ih (by omega) (by omega) hborn) hD0 + calc + _ ≤ D * (prefixEmbeddings J G (R+d) (by omega)).card := hrec + _ ≤ D * ((prefixEmbeddings J G R (by omega)).card * D ^ (R+d-R)) := hm + _ = _ := by simp only [Nat.add_sub_cancel_left]; ring + +theorem prefixEmbeddings_matching_roots (r : ℕ) (hr : 2*r ≤ N) + (Δ : ℝ) (hΔ0 : 0 ≤ Δ) + (hΔ : ∀ x : V, ((Finset.univ.filter (G.Adj x)).card : ℝ) ≤ Δ) + (hroot : ∀ i (hi : i < r), J.Adj ⟨2*i,by omega⟩ ⟨2*i+1,by omega⟩) : + ((prefixEmbeddings J G (2*r) hr).card : ℝ) ≤ ((Fintype.card V : ℝ) * Δ) ^ r := by + induction r with + | zero => simp only [Nat.mul_zero,prefixEmbeddings_zero,Nat.cast_one,pow_zero,le_refl] + | succ r ih => + have hr0 : 2*r ≤ N := by omega + have hr1 : 2*r+1 ≤ N := by omega + have hr2 : 2*r+1+1 ≤ N := by omega + have hone := prefixEmbeddings_succ_bound (J := J) (G := G) hr1 + (Fintype.card V : ℝ) (by + intro ψ _ + exact_mod_cast compatibleNext_le_order (J := J) hr1 ψ) + have htwo := prefixEmbeddings_succ_bound (J := J) (G := G) hr2 Δ (by + intro ψ _ + exact compatibleNext_le_degree hr2 ψ ⟨2*r,by omega⟩ (hroot r (by omega)) Δ hΔ) + have hi := ih hr0 (fun i hi => hroot i (by omega)) + have hmul := mul_le_mul_of_nonneg_left + (mul_le_mul_of_nonneg_left hi (Nat.cast_nonneg (Fintype.card V))) hΔ0 + calc + _ = ((prefixEmbeddings J G (2*r+1+1) hr2).card : ℝ) := by congr 3 + _ ≤ Δ * (prefixEmbeddings J G (2*r+1) hr1).card := htwo + _ ≤ Δ * ((Fintype.card V : ℝ) * (prefixEmbeddings J G (2*r) hr0).card) := + mul_le_mul_of_nonneg_left hone hΔ0 + _ ≤ Δ * ((Fintype.card V : ℝ) * ((Fintype.card V : ℝ) * Δ)^r) := hmul + _ = _ := by rw [pow_succ]; ring + +end OrderedBirthCounts +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddingsEdgeCover.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddingsEdgeCover.lean new file mode 100644 index 0000000000000000000000000000000000000000..09ace2d5a632c4b27e5d76dfc3aa732bd5df9197 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEmbeddingsEdgeCover.lean @@ -0,0 +1,47 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.OrientedEdges + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +namespace SharpTerminalLeave +open Classical in + +theorem prefixEmbeddings_edge_cover {n N R : ℕ} (J : SimpleGraph (Fin N)) + (G : Graph n) (hG : ∀ e ∈ G, e.card = 2) (hR : R ≤ N) + (E : Finset (Finset (Fin R))) (hE : ∀ e ∈ E, e.card = 2) + (hcover : ∀ i : Fin R, ∃ e ∈ E, i ∈ e) + (hedge : ∀ e ∈ E, ∀ x ∈ e, ∀ y ∈ e, x ≠ y → + J.Adj (Fin.castLE hR x) (Fin.castLE hR y)) : + (prefixEmbeddings J (lookupGraph G) R hR).card ≤ (2 * G.card)^E.card := by + classical + let e : Fin E.card ≃ ↥E := (Fin.castOrderIso (Fintype.card_coe E).symm).toEquiv.trans + (Fintype.equivFin ↥E).symm + have hh : ∀ j : Fin E.card, ∃ u v : Fin R, u ≠ v ∧ (e j).val = {u,v} := by + intro j + exact Finset.card_eq_two.mp (hE _ (e j).property) + choose u v huv huvE using hh + apply prefixEmbeddings_root_cover J G hG hR u v + · intro i + obtain ⟨f,hf,hif⟩ := hcover i + let j := e.symm ⟨f,hf⟩ + have hej : (e j).val = f := congrArg Subtype.val (e.apply_symm_apply _) + have hmem : i ∈ ({u j,v j} : Finset (Fin R)) := by rwa [← huvE j,hej] + exact ⟨j,by simpa only [Finset.mem_insert,Finset.mem_singleton] using hmem⟩ + · intro j + exact hedge _ (e j).property _ (by rw [huvE j]; simp) _ + (by rw [huvE j]; simp) (huv j) + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEpsilon.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEpsilon.lean new file mode 100644 index 0000000000000000000000000000000000000000..08c486ce13a60ba41c7fa220e0d8e73717dd7058 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixEpsilon.lean @@ -0,0 +1,166 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.RootedTemplates +import OAI.Combinatorics.TriangleRemoval.Process.CommonNeighbors + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def prefixEpsilon : ℝ := 1 / 2000 +def prefixCycleCap : ℕ := 8000 +def prefixSuffixLength : ℕ := 100 +def prefixSuppression : ℕ := 50 +def prefixTemplateCap : ℕ := 8001 + +noncomputable def prefixTargetDensity (n : ℕ) : ℝ := + (n : ℝ) ^ (-(1 / 2 : ℝ) + prefixEpsilon) + +noncomputable def prefixTargetTime (n : ℕ) : ℝ := + (n : ℝ)^2 / 6 * (1 - 1 / (n : ℝ) - prefixTargetDensity n) + +noncomputable def prefixTime (n : ℕ) : ℕ := ⌊prefixTargetTime n⌋₊ + +noncomputable def prefixDensity (n : ℕ) : ℝ := + 1 - 1 / (n : ℝ) - 6 * prefixTime n / (n : ℝ)^2 + +noncomputable def prefixD (n : ℕ) : ℝ := n * prefixDensity n ^ 2 +noncomputable def prefixM (n : ℕ) : ℝ := (n : ℝ)^2 * prefixDensity n / 2 + +noncomputable def prefixLaw (n : ℕ) : PMF (Graph n) := + evolve (completeGraph n) (prefixTime n) + +def neighbors {n : ℕ} (G : Graph n) (u : Fin n) : Finset (Fin n) := + Finset.univ.filter (fun v => {u,v} ∈ G) + +def currentDegree {n : ℕ} (G : Graph n) (u : Fin n) : ℕ := + (neighbors G u).card + +def cycleNext {j : ℕ} (i : Fin j) : Fin j := + ⟨(i.val + 1) % j, Nat.mod_lt _ (Nat.zero_lt_of_lt i.isLt)⟩ + +noncomputable def linkCycleCount {n : ℕ} (j : ℕ) (G : Graph n) (u : Fin n) : ℝ := by + classical + exact ∑ φ : Fin j ↪ Fin n, + if ∀ i, {u, φ i} ∈ G ∧ {φ i, φ (cycleNext i)} ∈ G then 1 else 0 + +def largeTemplateEligible {k : ℕ} (T : RootedTemplate k) (n : ℕ) (p : ℝ) : Prop := + ∀ W : Finset (Fin k), T.roots ⊆ W → + ∀ F : Graph k, F ⊆ T.edges → (∀ e ∈ F, e ⊆ W) → + 1 ≤ (n : ℝ) ^ (W.card - T.roots.card) * p ^ F.card + +def smallTemplateEligible {k : ℕ} (T : RootedTemplate k) (n : ℕ) (p : ℝ) : Prop := + ∀ J : Finset (Fin k), T.roots ⊆ J → + (n : ℝ) ^ (k - J.card) * p ^ (T.edges.card - (T.edges.filter (· ⊆ J)).card) ≤ 1 + +noncomputable def prefixTemplateFactor (n : ℕ) (C : ℝ) : ℝ := + (1 + Real.log n) ^ C + +def GoodPrefixGraph (n : ℕ) (c C : ℝ) (G : Graph n) : Prop := + G ⊆ completeGraph n ∧ + (G.card : ℝ) = prefixM n ∧ + (∀ u, |(currentDegree G u : ℝ) - n * prefixDensity n| ≤ + (n : ℝ)^(-c) * (n * prefixDensity n)) ∧ + (∀ u v, u ≠ v → |(currentCodegree G u v : ℝ) - prefixD n| ≤ + (n : ℝ)^(-c) * prefixD n) ∧ + (∀ j, 3 ≤ j → j ≤ prefixCycleCap → ∀ u, + |linkCycleCount j G u - prefixD n ^ j| ≤ (n : ℝ)^(-c) * prefixD n ^ j) ∧ + (∀ k, k ≤ prefixTemplateCap → ∀ T : RootedTemplate k, + ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + (largeTemplateEligible T n (prefixDensity n) → + rootedCount T ψ G ≤ prefixTemplateFactor n C * rootedScaling T n (prefixDensity n)) ∧ + (smallTemplateEligible T n (prefixDensity n) → + rootedCount T ψ G ≤ prefixTemplateFactor n C)) + +noncomputable def prefixFailure (n : ℕ) (c C : ℝ) : ℝ := by + classical + exact pmfMean (prefixLaw n) (fun G => if GoodPrefixGraph n c C G then 0 else 1) + +def UniformPrefixObligation : Prop := + ∃ c : ℝ, 0 < c ∧ ∃ C : ℝ, ∀ᶠ n : ℕ in atTop, + prefixFailure n c C ≤ Real.exp (- (Real.log n) ^ (4 / 3 : ℝ)) + +theorem prefixDensity_bounds (n : ℕ) (hn : 0 < n) + (ht : 0 ≤ prefixTargetTime n) : + prefixTargetDensity n ≤ prefixDensity n ∧ + prefixDensity n < prefixTargetDensity n + 6 / (n : ℝ)^2 := by + have hnR : 0 < (n : ℝ) := Nat.cast_pos.mpr hn + have hn2 : 0 < (n : ℝ)^2 := sq_pos_of_pos hnR + have hf : (prefixTime n : ℝ) ≤ prefixTargetTime n := Nat.floor_le ht + have hg : prefixTargetTime n < (prefixTime n : ℝ) + 1 := Nat.lt_floor_add_one _ + have hx : prefixTargetTime n * 6 = + (n : ℝ)^2 * (1 - 1 / (n : ℝ) - prefixTargetDensity n) := by + unfold prefixTargetTime + ring + have hp : prefixDensity n * (n : ℝ)^2 = + (1 - 1 / (n : ℝ)) * (n : ℝ)^2 - 6 * prefixTime n := by + unfold prefixDensity + field_simp + constructor + · apply (le_of_mul_le_mul_right (a := (n : ℝ)^2) _ hn2) + nlinarith + · apply (lt_of_mul_lt_mul_right (a := (n : ℝ)^2) _ (le_of_lt hn2)) + have he : (6 / (n : ℝ)^2) * (n : ℝ)^2 = 6 := div_mul_cancel₀ _ (ne_of_gt hn2) + nlinarith + +theorem step_card_mass_lower {n : ℕ} {G H : Graph n} + (h : H ∈ (step G).support) : G.card ≤ H.card + 3 := by + by_cases ha : (triangles G).Nonempty + · exact le_of_eq (step_card_of_active ha h).symm + · simp only [step,dite_eq_right ha,PMF.mem_support_pure_iff] at h + subst H + omega + +theorem evolve_card_mass_lower {n : ℕ} (G : Graph n) (k : ℕ) {H : Graph n} + (h : H ∈ (evolve G k).support) : G.card ≤ H.card + 3*k := by + induction k generalizing H with + | zero => + simp only [evolve,PMF.mem_support_pure_iff] at h + simp [h] + | succ k ih => + obtain ⟨J,hJ,hH⟩ := (PMF.mem_support_bind_iff _ _ _).mp h + have h₁ := ih hJ + have h₂ := step_card_mass_lower hH + omega + +theorem mass_equality_prevents_early_stop {n : ℕ} {G H K : Graph n} + {j k : ℕ} (hjk : j < k) (hK : K ∈ (evolve G j).support) + (hH : H ∈ (evolve K (k-j)).support) + (hm : H.card + 3*k = G.card) : (triangles K).Nonempty := by + by_contra hn + have hKe : triangles K = ∅ := Finset.not_nonempty_iff_eq_empty.mp hn + rw [evolve_absorbing K hKe] at hH + have he : H = K := by simpa only [PMF.mem_support_pure_iff] using hH + subst H + have hb := evolve_card_mass_lower G j hK + omega + +theorem prefixM_mass (n : ℕ) (hn : 0 < n) : + prefixM n + 3 * prefixTime n = (n.choose 2 : ℝ) := by + have hnR : (n : ℝ) ≠ 0 := ne_of_gt (Nat.cast_pos.mpr hn) + rw [Nat.cast_choose_two] + unfold prefixM prefixDensity + field_simp + ring + +theorem goodPrefix_mass {n : ℕ} {c C : ℝ} {G : Graph n} + (hn : 0 < n) (hG : GoodPrefixGraph n c C G) : + G.card + 3 * prefixTime n = (completeGraph n).card := by + have hm := prefixM_mass n hn + rw [← hG.2.1] at hm + rw [card_completeGraph] + exact_mod_cast hm + +end SharpTerminalLeave + +open InnerProductSpace +open scoped InnerProductSpace + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixFailureEnvelopeSuperpolynomial.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixFailureEnvelopeSuperpolynomial.lean new file mode 100644 index 0000000000000000000000000000000000000000..56e53ac24c5cc624072d3d1d039b828a0c28a939 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixFailureEnvelopeSuperpolynomial.lean @@ -0,0 +1,63 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +theorem prefix_failure_envelope_superpolynomial (K : ℝ) : + Tendsto (fun n : ℕ => (n : ℝ)^K * + Real.exp (- (Real.log n)^(4/3 : ℝ))) atTop (𝓝 0) := by + have hlog : Tendsto (fun n : ℕ => Real.log (n : ℝ)) atTop atTop := + Real.tendsto_log_atTop.comp tendsto_natCast_atTop_atTop + have hpow : Tendsto (fun n : ℕ => (Real.log (n : ℝ))^(1/3 : ℝ)) atTop atTop := + (tendsto_rpow_atTop (by norm_num : (0 : ℝ) < 1/3)).comp hlog + have hbound : ∀ᶠ n : ℕ in atTop, (n : ℝ)^K * + Real.exp (- (Real.log n)^(4/3 : ℝ)) ≤ (n : ℝ)^(-1 : ℝ) := by + filter_upwards [hpow.eventually (eventually_ge_atTop (K+1)), + eventually_ge_atTop (2 : ℕ)] with n hn hsize + have hn1 : (1 : ℝ) < n := by exact_mod_cast (by omega : 1 < n) + have hn0 : (0 : ℝ) < n := lt_trans zero_lt_one hn1 + have hl0 : 0 < Real.log (n : ℝ) := Real.log_pos hn1 + have he : (Real.log (n : ℝ))^(4/3 : ℝ) = + Real.log n * (Real.log n)^(1/3 : ℝ) := by + rw [show (4/3 : ℝ) = 1+1/3 by norm_num,Real.rpow_add hl0,Real.rpow_one] + have hle : (K+1)*Real.log n ≤ (Real.log (n : ℝ))^(4/3 : ℝ) := by + rw [he,mul_comm (K+1)] + exact mul_le_mul_of_nonneg_left hn hl0.le + calc + (n : ℝ)^K * Real.exp (- (Real.log n)^(4/3 : ℝ)) ≤ + (n : ℝ)^K * (n : ℝ)^(-(K+1)) := by + apply mul_le_mul_of_nonneg_left _ (Real.rpow_nonneg hn0.le _) + rw [Real.rpow_def_of_pos hn0] + apply Real.exp_le_exp.mpr + nlinarith only [hle] + _ = (n : ℝ)^(-1 : ℝ) := by + rw [← Real.rpow_add hn0] + congr 1 + ring + exact squeeze_zero' (Filter.Eventually.of_forall (fun n => by positivity)) hbound + ((tendsto_rpow_neg_atTop (by norm_num : (0 : ℝ) < 1)).comp + tendsto_natCast_atTop_atTop) + +theorem prefix_failure_l2_envelope_tendsto : + Tendsto (fun n : ℕ => (2*(n : ℝ)+1)* + Real.exp (- (Real.log n)^(4/3 : ℝ))) atTop (𝓝 0) := by + have h1 := prefix_failure_envelope_superpolynomial 1 + have h0 := prefix_failure_envelope_superpolynomial 0 + simpa only [Real.rpow_one,Real.rpow_zero,one_mul,mul_add,add_mul,mul_zero, + add_zero,Function.comp_def,mul_assoc] using (h1.const_mul 2).add h0 + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixHorizon.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixHorizon.lean new file mode 100644 index 0000000000000000000000000000000000000000..6fc1a0146aaff9284be28a7071e4a8f1eafc0ff3 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixHorizon.lean @@ -0,0 +1,127 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Asymptotics.OneAddLogRpow + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def prefixHorizon (n : ℕ) : ℝ := Real.log (1+2*prefixD n)/2 + +lemma prefixHorizon_bounds : ∀ᶠ n : ℕ in atTop, + 0 < prefixHorizon n ∧ prefixHorizon n ≤ Real.log n := by + filter_upwards [prefix_scales_eventually_pos,eventually_ge_atTop (3 : ℕ)] with n hp hn + have hnR : (3 : ℝ) ≤ n := by exact_mod_cast hn + have hD : prefixD n ≤ n := by + have hpl := prefixDensity_le_one n + have hsq : (prefixDensity n)^2 ≤ 1 := by nlinarith [hp.1] + exact (mul_le_mul_of_nonneg_left hsq (Nat.cast_nonneg n)).trans_eq (mul_one _) + have harg : 1+2*prefixD n ≤ (n : ℝ)^2 := by nlinarith + have hl := Real.log_le_log (by linarith [hp.2] : 0 < 1+2*prefixD n) harg + rw [Real.log_pow] at hl + norm_num only [Nat.cast_ofNat] at hl + unfold prefixHorizon + exact ⟨div_pos (Real.log_pos (by linarith [hp.2])) (by norm_num),by linarith⟩ + +theorem prefix_bootstrap_numerics {c C₁ : ℝ} (hc : 0 < c) (hC₁ : 0 < C₁) (J : ℕ) : + ∃ a : ℝ, 0 < a ∧ a < min c (1/1000 : ℝ) ∧ ∀ᶠ n : ℕ in atTop, + let δ := (n : ℝ)^(-a) + let S := prefixHorizon n + let K := C₁*(1+Real.log n)^J + 0 < δ ∧ δ ≤ 1/2 ∧ 0 < S ∧ S ≤ Real.log n ∧ + 12*S/prefixD n ≤ 1/4 ∧ + K*((n : ℝ)^(-c)+64*δ^2+1152*S/prefixD n)*S ≤ δ/2 := by + let a := min c (1/1000 : ℝ)/3 + have ha : 0 < a := div_pos (lt_min hc (by norm_num)) (by norm_num) + have hac : 2*a ≤ c := by dsimp [a]; linarith [min_le_left c (1/1000 : ℝ)] + have had : 2*a ≤ (1/1000 : ℝ) := by dsimp [a]; linarith [min_le_right c (1/1000 : ℝ)] + refine ⟨a,ha,by dsimp [a]; linarith [lt_min hc (by norm_num : (0 : ℝ) < 1/1000)],?_⟩ + have hδ := ((tendsto_rpow_neg_atTop ha).comp + (tendsto_natCast_atTop_atTop (R := ℝ))).eventually_le_const + (by norm_num : (0 : ℝ) < 1/2) + have hb : 0 < 1/(2434*C₁) := by positivity + filter_upwards [prefixHorizon_bounds,prefixD_eventual_envelope,prefix_scales_eventually_pos, + prefixTemplateFactor_subpower (J+2) ha hb, + prefixTemplateFactor_subpower 1 ha (by norm_num : (0 : ℝ) < 1/48), + hδ,eventually_ge_atTop (1 : ℕ)] with n hS hD hp hpol hlog hδ hn + dsimp only + change (n : ℝ)^(-a) ≤ 1/2 at hδ + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := by linarith + have hl0 : 0 ≤ Real.log n := Real.log_nonneg hn1 + have hL : 1 ≤ 1+Real.log n := by linarith + have hna : 0 < (n : ℝ)^a := Real.rpow_pos_of_pos hn0 _ + have hn2a : 0 < (n : ℝ)^(-2*a) := Real.rpow_pos_of_pos hn0 _ + have hDa : (n : ℝ)^a ≤ prefixD n := + (Real.rpow_le_rpow_of_exponent_le hn1 (by linarith : a ≤ (1/1000 : ℝ))).trans hD.1 + have hD2a : (n : ℝ)^(2*a) ≤ prefixD n := + (Real.rpow_le_rpow_of_exponent_le hn1 had).trans hD.1 + have hD0 : 0 < prefixD n := lt_of_lt_of_le zero_lt_one hp.2 + have hη : (n : ℝ)^(-c) ≤ (n : ℝ)^(-2*a) := + Real.rpow_le_rpow_of_exponent_le hn1 (by linarith) + have hδsq : ((n : ℝ)^(-a))^2 = (n : ℝ)^(-2*a) := by + rw [← Real.rpow_natCast,← Real.rpow_mul hn0.le] + congr 1 + ring + have hDinv : 1/prefixD n ≤ (n : ℝ)^(-2*a) := by + calc + _ ≤ 1/(n : ℝ)^(2*a) := div_le_div_of_nonneg_left zero_le_one + (Real.rpow_pos_of_pos hn0 _) hD2a + _ = _ := by rw [one_div,← Real.rpow_neg hn0.le]; congr 1; ring + have hSD : prefixHorizon n/prefixD n ≤ (1+Real.log n)*(n : ℝ)^(-2*a) := by + calc + _ = prefixHorizon n*(1/prefixD n) := by ring + _ ≤ (1+Real.log n)*(n : ℝ)^(-2*a) := + mul_le_mul (by linarith [hS.2]) hDinv (by positivity) (by linarith) + have hsmall : 12*prefixHorizon n/prefixD n ≤ 1/4 := by + have hlog' : 1+Real.log n ≤ (1/48 : ℝ)*(n : ℝ)^a := by + simpa only [prefixTemplateFactor,Real.rpow_one] using hlog + apply (div_le_iff₀ hD0).mpr + nlinarith [hS.2,hDa] + have hforce : (n : ℝ)^(-c)+64*((n : ℝ)^(-a))^2+ + 1152*prefixHorizon n/prefixD n ≤ 1217*(1+Real.log n)*(n : ℝ)^(-2*a) := by + rw [hδsq] + have hhd := mul_le_mul_of_nonneg_left hSD (by norm_num : (0 : ℝ) ≤ 1152) + have hhη := mul_le_mul_of_nonneg_right hL hn2a.le + rw [mul_div_assoc] + nlinarith only [hη,hhη,hhd] + have hpol' : (1+Real.log n)^(J+2) ≤ 1/(2434*C₁)*(n : ℝ)^a := by + unfold prefixTemplateFactor at hpol + rw [show (J : ℝ)+2 = ((J+2 : ℕ) : ℝ) by norm_cast,Real.rpow_natCast] at hpol + exact hpol + have hm : C₁*(1+Real.log n)^J * + ((n : ℝ)^(-c)+64*((n : ℝ)^(-a))^2+1152*prefixHorizon n/prefixD n)* + prefixHorizon n ≤ + 1217*C₁*(1+Real.log n)^(J+2)*(n : ℝ)^(-2*a) := by + calc + _ ≤ C₁*(1+Real.log n)^J * (1217*(1+Real.log n)*(n : ℝ)^(-2*a)) * + (1+Real.log n) := by + apply mul_le_mul (mul_le_mul_of_nonneg_left hforce (by positivity)) + (by linarith [hS.2]) hS.1.le + positivity + _ = _ := by rw [pow_add]; ring + have hident : (1217*C₁)*(1/(2434*C₁)) = (1/2 : ℝ) := by + field_simp + ring + have hexp : (n : ℝ)^a*(n : ℝ)^(-2*a) = (n : ℝ)^(-a) := by + rw [← Real.rpow_add hn0] + congr 1 + ring + refine ⟨Real.rpow_pos_of_pos hn0 _,hδ,hS.1,hS.2,hsmall,?_⟩ + calc + _ ≤ 1217*C₁*(1+Real.log n)^(J+2)*(n : ℝ)^(-2*a) := hm + _ ≤ 1217*C₁*(1/(2434*C₁)*(n : ℝ)^a)*(n : ℝ)^(-2*a) := by + gcongr + _ = (n : ℝ)^(-a)/2 := by + rw [← mul_assoc (1217*C₁),hident,mul_assoc,hexp] + ring +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixMessageEnvelopeSmall.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixMessageEnvelopeSmall.lean new file mode 100644 index 0000000000000000000000000000000000000000..10779821f7d6768b51255e03b8f076a84e42a8ef --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixMessageEnvelopeSmall.lean @@ -0,0 +1,80 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Stability.CavityReference + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma prefix_message_envelope_small {b : ℝ} (hb : 0 < b) : + ∀ᶠ n : ℕ in atTop, 0 ≤ 12*prefixHorizon n/prefixD n ∧ + 12*prefixHorizon n/prefixD n ≤ b := by + have hpoly := prefixTemplateFactor_subpower 1 (by norm_num : (0 : ℝ) < 1/1000) + (div_pos hb (by norm_num : (0 : ℝ) < 12)) + filter_upwards [prefixHorizon_bounds,prefixD_eventual_envelope, + prefix_scales_eventually_pos,hpoly] with n hS hD hp hpoly + have hD0 : 0 < prefixD n := lt_of_lt_of_le zero_lt_one hp.2 + have hpoly' : 1+Real.log n ≤ (b/12)*(n : ℝ)^(1/1000 : ℝ) := by + simpa only [prefixTemplateFactor,Real.rpow_one] using hpoly + refine ⟨div_nonneg (mul_nonneg (by norm_num) hS.1.le) hD0.le,?_⟩ + apply (div_le_iff₀ hD0).mpr + have hbound := mul_le_mul_of_nonneg_left hD.1 hb.le + nlinarith [hS.2] + +lemma prefix_message_envelope_tendsto : + Tendsto (fun n : ℕ => 12*prefixHorizon n/prefixD n) atTop (𝓝 0) := by + apply tendsto_order.mpr + constructor + · intro a ha + filter_upwards [prefix_message_envelope_small zero_lt_one] with n hn + exact ha.trans_le hn.1 + · intro b hb + filter_upwards [prefix_message_envelope_small (show 0 < b/2 by linarith)] with n hn + exact hn.2.trans_lt (by linarith) + +theorem goodPrefix_cavity_stability {c : ℝ} (hc : 0 < c) {ε : ℝ} (hε : 0 < ε) : + ∀ᶠ n : ℕ in atTop, ∀ (C : ℝ) (G : Graph n), GoodPrefixGraph n c C G → + ∀ t ∈ Set.Icc (0 : ℝ) 1, + (∀ e : G, |cavityLimit (activeHypergraph G) e none t / + cavityReference (prefixD n) t - 1| ≤ ε) ∧ + (∀ (e : G) (T : triangles G), e ∈ activeHypergraph G T → + |cavityLimit (activeHypergraph G) e (some T) t / + cavityReference (prefixD n) t - 1| ≤ ε) := by + obtain ⟨a,ha,hrel⟩ := goodPrefix_cavity_relative hc + have hδ := (tendsto_rpow_neg_atTop ha).comp (tendsto_natCast_atTop_atTop (R := ℝ)) + have hlo : Tendsto (fun n : ℕ => Real.exp (-(n : ℝ)^(-a)/2)) atTop (𝓝 1) := by + simpa [Function.comp_def] using Real.continuous_exp.continuousAt.tendsto.comp (hδ.neg.div_const 2) + have hexp : Tendsto (fun n : ℕ => Real.exp ((n : ℝ)^(-a)/2)) atTop (𝓝 1) := by + simpa [Function.comp_def] using Real.continuous_exp.continuousAt.tendsto.comp (hδ.div_const 2) + have hden : Tendsto (fun n : ℕ => 1-12*prefixHorizon n/prefixD n) atTop (𝓝 1) := by + simpa only [sub_zero] using tendsto_const_nhds.sub prefix_message_envelope_tendsto + have hhi : Tendsto (fun n : ℕ => Real.exp ((n : ℝ)^(-a)/2)/ + (1-12*prefixHorizon n/prefixD n)) atTop (𝓝 1) := by + simpa only [Pi.div_def,div_one] using hexp.div hden one_ne_zero + filter_upwards [hrel,hlo.eventually_const_le (by linarith : 1-ε < 1), + hhi.eventually_le_const (by linarith : 1 < 1+ε), + hexp.eventually_le_const (by linarith : 1 < 1+ε)] with n hrel hlo hhi hexp + intro C G h t ht + obtain ⟨hroot,hparent⟩ := hrel.2 C G h t ht + constructor + · intro e + apply abs_le.mpr + obtain ⟨he1,he2⟩ := hroot e + constructor <;> linarith + · intro e T he + obtain ⟨_,he1,he2⟩ := hparent e T he + apply abs_le.mpr + constructor <;> linarith + +end SharpTerminalLeave + +open scoped BigOperators + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixPairClosureBudget.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixPairClosureBudget.lean new file mode 100644 index 0000000000000000000000000000000000000000..e5f8ca93c5f53c6316e40bbdd2261f9c362c8501 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixPairClosureBudget.lean @@ -0,0 +1,115 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.CenteredEdgeVectorTriangle +import OAI.Combinatorics.TriangleRemoval.Process.CenteredNeighborSumBound + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +lemma prefix_pair_closure_budget : ∀ᶠ n : ℕ in atTop, + prefixEdgeForcingBound n+16*prefixEdgeRadius n*Real.log n ≤ prefixWheelRadius n/2 ∧ + prefixWheelRadius n ≤ 1 := by + filter_upwards [prefix_edge_forcing_power, + prefixTemplateFactor_subpower 1 (by norm_num : (0 : ℝ) < 1/80000) + (by norm_num : (0 : ℝ) < 1/130),eventually_ge_atTop (1 : ℕ)] with n hF hlog hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have hl := Real.log_nonneg hn1 + have hη : 0 ≤ prefixEdgeRadius n := Real.rpow_nonneg hn0.le _ + have hr : prefixTriangleRadius n ≤ prefixEdgeRadius n := + Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hlog' : 1+Real.log (n : ℝ) ≤ 1/130*(n : ℝ)^(1/80000 : ℝ) := by + simpa only [prefixTemplateFactor,Real.rpow_one] using hlog + have he : (n : ℝ)^(1/80000 : ℝ)*prefixEdgeRadius n = prefixWheelRadius n := by + unfold prefixEdgeRadius prefixWheelRadius + rw [← Real.rpow_add hn0] + congr 1 + norm_num + refine ⟨?_,Real.rpow_le_one_of_one_le_of_nonpos hn1 (by norm_num)⟩ + calc + _ ≤ 49*(1+Real.log (n : ℝ))*prefixEdgeRadius n+16*prefixEdgeRadius n*Real.log n := by + exact add_le_add (hF.2.trans (mul_le_mul_of_nonneg_left hr (by positivity))) le_rfl + _ ≤ 65*(1+Real.log (n : ℝ))*prefixEdgeRadius n := by nlinarith only [hη] + _ ≤ 65*(1/130*(n : ℝ)^(1/80000 : ℝ))*prefixEdgeRadius n := by gcongr + _ = prefixWheelRadius n/2 := by rw [show 65*(1/130*(n : ℝ)^(1/80000 : ℝ))*prefixEdgeRadius n = + ((n : ℝ)^(1/80000 : ℝ)*prefixEdgeRadius n)/2 by ring,he] + +lemma initial_normalized_pair_bound {n : ℕ} (hn : 2 ≤ n) + (ω : History (Graph n) (prefixTime n)) + (hω : ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support) + (u v : Fin n) (huv : u ≠ v) : + |prefixNormalizedCodegree u v 0 (ω (historyIndex (prefixTime n) 0))-1| ≤ 4/(n : ℝ) := by + have hi := (historyLaw_path_support (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n) le_rfl ω hω).1 + have hi' : ω (historyIndex (prefixTime n) 0) = completeGraph n := by simpa using hi + unfold prefixNormalizedCodegree + rw [hi',initial_normalized_codegree_error hn u v huv, + neg_div,abs_neg,abs_of_nonneg (by positivity : (0 : ℝ) ≤ 1/((n : ℝ)-1)^2)] + have hnR : (2 : ℝ) ≤ n := by exact_mod_cast hn + apply (div_le_div_iff₀ (sq_pos_of_ne_zero (by linarith)) (by linarith : (0 : ℝ) < n)).mpr + nlinarith + +theorem prefix_pair_barrier_improvement : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support → + PrefixTriangleNoise n ω → PrefixCodegreeNoiseGood n ω → ∀ j ≤ prefixTime n, + historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) (prefixTime n) j ω → + ∀ u v : Fin n, u ≠ v → + (∀ i < j, (currentCodegree (ω (historyIndex (prefixTime n) i)) u v : ℝ) ≤ 2*earlyTemplateScale 1 2 n i) → + |prefixNormalizedCodegree u v j (ω (historyIndex (prefixTime n) j))-1| ≤ prefixWheelRadius n/2 := by + filter_upwards [prefix_pair_compensator_bound,prefix_pair_closure_budget, + eventually_ge_atTop (2 : ℕ)] with n hcomp hbudget hn + intro ω hω htri hnoise j hj hsafe u v huv hcap + have hc := hcomp ω hω htri j hj hsafe u v huv hcap + have hnse : |historyNoise (fun _ => step) (prefixNormalizedCodegree u v) (prefixTime n) j ω| < codegreeNoiseRadius n := by + apply lt_of_not_ge + intro hx + exact hnoise u v ⟨j,hj,hcap,hx⟩ + have hi := initial_normalized_pair_bound hn ω hω u v huv + have he := history_noise_compensator (fun _ => step) (prefixNormalizedCodegree u v) (prefixTime n) j hj ω + have he' : prefixNormalizedCodegree u v j (ω (historyIndex (prefixTime n) j))-1 = + (prefixNormalizedCodegree u v 0 (ω (historyIndex (prefixTime n) 0))-1)+ + historyNoise (fun _ => step) (prefixNormalizedCodegree u v) (prefixTime n) j ω+ + historyCompensator (fun _ => step) (prefixNormalizedCodegree u v) (prefixTime n) j ω := by linarith only [he] + rw [he'] + apply le_trans _ hbudget.1 + have hab := (abs_add_le ((prefixNormalizedCodegree u v 0 (ω (historyIndex (prefixTime n) 0))-1)+ + historyNoise (fun _ => step) (prefixNormalizedCodegree u v) (prefixTime n) j ω) + (historyCompensator (fun _ => step) (prefixNormalizedCodegree u v) (prefixTime n) j ω)).trans + (add_le_add (abs_add_le _ _) le_rfl) + apply hab.trans + unfold prefixEdgeForcingBound + linarith only [hi,hnse,hc] + +theorem prefix_all_pair_closure : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support → + PrefixRootedUpper 8001 n ω → PrefixBalancedNoise 8001 n ω → + PrefixTriangleNoise n ω → PrefixCodegreeNoiseGood n ω → + ∀ j ≤ prefixTime n, ∀ u v : Fin n, u ≠ v → + |prefixNormalizedCodegree u v j (ω (historyIndex (prefixTime n) j))-1| ≤ prefixWheelRadius n/2 := by + filter_upwards [prefix_present_edge_closure,prefix_pair_barrier_improvement, + prefix_pair_closure_budget,earlyTemplateScale_regular 1 2] with n hedge hpair hbudget hreg + intro ω hω hroot hbalanced htri hnoise j + have hs := hedge ω hω hroot hbalanced htri hnoise + induction j using Nat.strong_induction_on with + | h j ih => + intro hj u v huv + apply hpair ω hω htri hnoise j hj (fun i hi => hs i (by omega)) u v huv + intro i hi + have hh := (le_abs_self (prefixNormalizedCodegree u v i (ω (historyIndex (prefixTime n) i))-1)).trans + (ih i hi (by omega) u v huv) + have hc : prefixNormalizedCodegree u v i (ω (historyIndex (prefixTime n) i)) ≤ 2 := by + linarith only [hh,hbudget.2] + exact (div_le_iff₀ (hreg.1 i (by omega))).mp hc + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixSpectralErrorPower.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixSpectralErrorPower.lean new file mode 100644 index 0000000000000000000000000000000000000000..342ae80a7b63296d7ea925fe92c88d1b5ed2f22c --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixSpectralErrorPower.lean @@ -0,0 +1,99 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Spectral.LinkStarMatrix +import OAI.Combinatorics.TriangleRemoval.Asymptotics.OneAddLogRpow + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave +open Filter + +theorem prefixSpectralError_power_decay {c : ℝ} (hc : 0 < c) : + ∃ γ : ℝ, 0 < γ ∧ ∀ᶠ n : ℕ in atTop, + 2 * prefixSpectralError n c ≤ (n : ℝ)^(-γ) := by + let a : ℝ := min c (1/1000) + let B : ℝ := (8000 : ℝ)^8001 * 2^8000 + let L : ℝ := 4 * (8001 + 2*B) ^ (1/(8000 : ℝ)) + 7 + have ha : 0 < a := lt_min hc (by norm_num) + have hac : a ≤ c := min_le_left _ _ + have ha1 : a ≤ 1/1000 := min_le_right _ _ + have hB : 0 ≤ B := by dsimp [B]; positivity + have hL : 0 ≤ L := by dsimp [L]; positivity + refine ⟨a/16000,by positivity,?_⟩ + have ht : Tendsto (fun n : ℕ => (n : ℝ)^(a/16000)) atTop atTop := + (tendsto_rpow_atTop (by positivity : 0 < a/16000)).comp tendsto_natCast_atTop_atTop + filter_upwards [prefixD_eventual_envelope, eventually_ge_atTop (1 : ℕ), + ht.eventually_ge_atTop (2*L)] with n hD hn hLr + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := by linarith + have hDr : 0 < (n : ℝ)^(1/1000 : ℝ) := Real.rpow_pos_of_pos hn0 _ + have hD0 : 0 < prefixD n := hDr.trans_le hD.1 + have hδ : (n : ℝ)^(-c) ≤ (n : ℝ)^(-a) := + Real.rpow_le_rpow_of_exponent_le hn1 (by linarith) + have h4000 : ((n : ℝ)^(1/1000 : ℝ))^4000 = (n : ℝ)^(4 : ℝ) := by + rw [← Real.rpow_natCast,← Real.rpow_mul hn0.le] + norm_num + have hq : (n : ℝ)/prefixD n^4000 ≤ (n : ℝ)^(-a) := by + calc + _ ≤ (n : ℝ) / ((n : ℝ)^(1/1000 : ℝ))^4000 := + div_le_div_of_nonneg_left hn0.le (pow_pos hDr _) + (pow_le_pow_left₀ hDr.le hD.1 _) + _ = (n : ℝ)^(-(3 : ℝ)) := by + rw [h4000] + conv_lhs => lhs; rw [← Real.rpow_one (n : ℝ)] + rw [← Real.rpow_sub hn0] + norm_num + _ ≤ _ := Real.rpow_le_rpow_of_exponent_le hn1 (by linarith) + have hi : 1/prefixD n ≤ (n : ℝ)^(-a) := by + calc + _ ≤ 1/(n : ℝ)^(1/1000 : ℝ) := + div_le_div_of_nonneg_left zero_le_one hDr hD.1 + _ = (n : ℝ)^(-(1/1000 : ℝ)) := by rw [Real.rpow_neg hn0.le,one_div] + _ ≤ _ := Real.rpow_le_rpow_of_exponent_le hn1 (by linarith) + have hsum : 8001*(n : ℝ)^(-c) + B*((n : ℝ)/prefixD n^4000 + 1/prefixD n) ≤ + (8001+2*B) * (n : ℝ)^(-a) := by + have hh := mul_le_mul_of_nonneg_left (add_le_add hq hi) hB + calc + _ ≤ 8001*(n : ℝ)^(-a)+B*((n : ℝ)^(-a)+(n : ℝ)^(-a)) := + add_le_add (mul_le_mul_of_nonneg_left hδ (by norm_num)) hh + _ = _ := (show ∀ x y : ℝ, 8001*x+y*(x+x)=(8001+2*y)*x by + intro x y; ring) _ _ + have hrt : (8001*(n : ℝ)^(-c) + B*((n : ℝ)/prefixD n^4000 + 1/prefixD n)) ^ + (1/(8000 : ℝ)) ≤ (8001+2*B)^(1/(8000 : ℝ)) * (n : ℝ)^(-a/8000) := by + calc + _ ≤ ((8001+2*B) * (n : ℝ)^(-a))^(1/(8000 : ℝ)) := + Real.rpow_le_rpow (add_nonneg + (mul_nonneg (by norm_num) (Real.rpow_nonneg hn0.le _)) + (mul_nonneg hB (add_nonneg (div_nonneg hn0.le (pow_nonneg hD0.le _)) + (div_nonneg zero_le_one hD0.le)))) hsum (by norm_num) + _ = _ := by + rw [Real.mul_rpow (by positivity) (by positivity),← Real.rpow_mul hn0.le] + congr 2 + ring + have hδ' : (n : ℝ)^(-c) ≤ (n : ℝ)^(-a/8000) := + Real.rpow_le_rpow_of_exponent_le hn1 (by linarith) + have herr : prefixSpectralError n c ≤ L * (n : ℝ)^(-a/8000) := by + unfold prefixSpectralError + change 4*(8001*(n : ℝ)^(-c)+B*((n : ℝ)/prefixD n^4000+1/prefixD n))^ + (1/(8000 : ℝ))+7*(n : ℝ)^(-c) ≤ _ + dsimp [L] + nlinarith + calc + _ ≤ (2*L)*(n : ℝ)^(-a/8000) := by linarith + _ ≤ (n : ℝ)^(a/16000)*(n : ℝ)^(-a/8000) := + mul_le_mul_of_nonneg_right hLr (by positivity) + _ = (n : ℝ)^(-(a/16000)) := by + rw [← Real.rpow_add hn0] + congr 1 + ring + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixTimeSquare.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixTimeSquare.lean new file mode 100644 index 0000000000000000000000000000000000000000..283241b027520291cdb268f612635b99b4fa8189 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixTimeSquare.lean @@ -0,0 +1,66 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixEpsilon +import OAI.Combinatorics.TriangleRemoval.Tracking.RootedTemplateEncoding +import OAI.Combinatorics.TriangleRemoval.Tracking.PolynomialExponentialPrefixEnvelope + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma prefixTime_le_square (n : ℕ) : prefixTime n ≤ n^2 := by + have hf : prefixTargetTime n ≤ (n : ℝ)^2 := by + unfold prefixTargetTime + have h₁ : 0 ≤ 1/(n : ℝ) := by positivity + have h₂ : 0 ≤ prefixTargetDensity n := Real.rpow_nonneg (Nat.cast_nonneg n) _ + have h₃ := sq_nonneg (n : ℝ) + nlinarith + calc + prefixTime n ≤ ⌊(n : ℝ)^2⌋₊ := Nat.floor_le_floor hf + _ = n^2 := by simp only [← Nat.cast_pow,Nat.floor_natCast] + +theorem rooted_noise_chances_le (H n : ℕ) (hn : 1 ≤ n) : + Fintype.card (RootedCountIndex H n)*2*(prefixTime n+1) ≤ + rootedFamilySize H * 2^(H+2)*n^(H+2) := by + have hpow : (n+1)^H ≤ (2*n)^H := Nat.pow_le_pow_left (by omega) H + have hn2 : 1 ≤ n^2 := one_le_pow₀ hn + have ht : prefixTime n+1 ≤ 2*n^2 := by + have hh := prefixTime_le_square n + omega + calc + _ ≤ (rootedFamilySize H*(n+1)^H)*2*(2*n^2) := by + gcongr + exact rootedCountIndex_card_le H n + _ ≤ (rootedFamilySize H*(2*n)^H)*2*(2*n^2) := by gcongr + _ = _ := by rw [mul_pow,pow_add,pow_add]; ring + +theorem rooted_noise_budget_envelope (H : ℕ) (κ : ℝ) (hκ : 0 < κ) : + ∀ᶠ n : ℕ in atTop, + (Fintype.card (RootedCountIndex H n) : ℝ)*2*(prefixTime n+1)* + Real.exp (-(n : ℝ)^κ) ≤ Real.exp (-(Real.log n)^(4/3 : ℝ)) := by + filter_upwards [polynomial_exponential_le_prefix_envelope + ((rootedFamilySize H : ℝ)*2^(H+2)) (H+2) κ hκ, + eventually_ge_atTop (1 : ℕ)] with n hb hn + apply le_trans _ hb + have hh : (Fintype.card (RootedCountIndex H n) : ℝ)*2*(prefixTime n+1) ≤ + (rootedFamilySize H : ℝ)*2^(H+2)*(n : ℝ)^(H+2) := by + exact_mod_cast rooted_noise_chances_le H n hn + have he : (n : ℝ)^((H : ℝ)+2) = (n : ℝ)^(H+2 : ℕ) := by + rw [← Real.rpow_natCast] + norm_cast + rw [he] + exact mul_le_mul_of_nonneg_right hh (Real.exp_pos _).le + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixTriangleRadius.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixTriangleRadius.lean new file mode 100644 index 0000000000000000000000000000000000000000..0809ceec58d3b4c23a216cd3cac65a72f8338481 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixTriangleRadius.lean @@ -0,0 +1,123 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Asymptotics.DensityEdgeSafeTriangle + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +noncomputable def prefixTriangleRadius (n : ℕ) : ℝ := (n : ℝ)^(-3/80000 : ℝ) + +lemma triangle_closure_budget : ∀ᶠ n : ℕ in atTop, + 0 ≤ prefixEdgeRadius n ∧ prefixEdgeRadius n ≤ 1/2 ∧ + 4/(n : ℝ)+codegreeNoiseRadius n+12*(prefixEdgeRadius n)^2*Real.log n+ + 2/prefixD n+216*Real.log n/(n : ℝ) ≤ prefixTriangleRadius n := by + have hrad : Tendsto prefixEdgeRadius atTop (nhds 0) := by + change Tendsto (fun n : ℕ => (n : ℝ)^(-1/40000 : ℝ)) atTop (nhds 0) + simpa only [Function.comp_def,neg_div] using (tendsto_rpow_neg_atTop + (by norm_num : (0 : ℝ) < 1/40000)).comp tendsto_natCast_atTop_atTop + filter_upwards [prefixTemplateFactor_subpower 1 (by norm_num : (0 : ℝ) < 1/80000) + (by norm_num : (0 : ℝ) < 1/235),prefixD_eventual_envelope, + hrad.eventually_le_const (by norm_num : (0 : ℝ) < 1/2), + eventually_ge_atTop (1 : ℕ)] with n hlog hD hrad hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hn0 : (0 : ℝ) < n := lt_of_lt_of_le zero_lt_one hn1 + have he : (prefixEdgeRadius n)^2 = (n : ℝ)^(-1/20000 : ℝ) := by + unfold prefixEdgeRadius + rw [← Real.rpow_natCast,← Real.rpow_mul hn0.le] + norm_num + have hinv : 1/(n : ℝ) ≤ (prefixEdgeRadius n)^2 := by + rw [he] + simpa only [Real.rpow_neg_one,one_div] using + (Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num : (-1 : ℝ) ≤ -1/20000)) + have hnoise : codegreeNoiseRadius n ≤ (prefixEdgeRadius n)^2 := by + rw [he] + exact Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hdisc : 1/prefixD n ≤ (prefixEdgeRadius n)^2 := by + rw [he] + apply (div_le_div_of_nonneg_left zero_le_one (Real.rpow_pos_of_pos hn0 _) hD.1).trans + rw [one_div,← Real.rpow_neg hn0.le] + exact Real.rpow_le_rpow_of_exponent_le hn1 (by norm_num) + have hl : 0 ≤ Real.log (n : ℝ) := Real.log_nonneg hn1 + have hs := mul_le_mul_of_nonneg_right (show 1+Real.log (n : ℝ) ≤ + 1/235*(n : ℝ)^(1/80000 : ℝ) by + simpa only [prefixTemplateFactor,Real.rpow_one] using hlog) + (show 0 ≤ 235*(prefixEdgeRadius n)^2 by positivity) + have hprod : (n : ℝ)^(1/80000 : ℝ)*(prefixEdgeRadius n)^2 = prefixTriangleRadius n := by + rw [he,← Real.rpow_add hn0] + norm_num [prefixTriangleRadius] + have hscaled : 235*(1+Real.log (n : ℝ))*(prefixEdgeRadius n)^2 ≤ prefixTriangleRadius n := by + nlinarith only [hs,hprod] + refine ⟨Real.rpow_nonneg hn0.le _,hrad,?_⟩ + have hgrid := mul_le_mul_of_nonneg_left hinv (show 0 ≤ 216*Real.log (n : ℝ) by positivity) + simp only [div_eq_mul_inv,one_mul] at hinv hdisc hgrid ⊢ + nlinarith only [hinv,hnoise,hdisc,hgrid,hscaled,sq_nonneg (prefixEdgeRadius n), + mul_nonneg (sq_nonneg (prefixEdgeRadius n)) hl] + +theorem prefix_triangle_tracking : ∀ᶠ n : ℕ in atTop, + ∀ ω : History (Graph n) (prefixTime n), + ω ∈ (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)).support → + PrefixTriangleNoise n ω → ∀ j ≤ prefixTime n, + historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) (prefixTime n) j ω → + |(triangles (ω (historyIndex (prefixTime n) j))).card/earlyTriangleScale n j-1| ≤ + prefixTriangleRadius n := by + filter_upwards [triangle_closure_budget,earlyTriangleScale_regular, + prefixDensity_eventually_inverse_lower,eventually_ge_atTop (2 : ℕ)] with n hb hr hp hn + intro ω hω hnse j hj hsafe + have hn0 : 0 < n := by omega + have hnR : (2 : ℝ) ≤ n := by exact_mod_cast hn + have hi := (historyLaw_path_support (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n) le_rfl ω hω).1 + have hi' : ω (historyIndex (prefixTime n) 0) = completeGraph n := by + simpa only [PMF.mem_support_pure_iff] using hi + have hloads : pastRelativeCopyLoadsSafe (triangleRequired (n := n)) (triangleNoiseLoad n) + (prefixTime n) j ω := by + intro i hit + have hij : i < j := lt_of_lt_of_le hit (Nat.min_le_left _ _) + have hh := densityEdgeSafe_triangle_load (early_density_positive hn0 hp (by omega)).le + (show prefixEdgeRadius n ≤ 1 by linarith only [hb.2.1]) (hsafe i hij) + simpa only [triangleNoiseLoad,earlyTemplateScale,pow_one] using hh + have hcap : ∀ i < j, copyCount (triangleRequired (n := n)) (ω (historyIndex (prefixTime n) i)) ≤ + 2*earlyTriangleScale n i := by + intro i hij + rw [triangle_copyCount] + have hh := (densityEdgeSafe_triangle_bounds (hsafe i hij)).2 + have hS : 0 ≤ earlyTriangleScale n i := (hr.1 i (by omega)).le + change _ ≤ earlyTriangleScale n i*(1+prefixEdgeRadius n) at hh + have ht := mul_le_mul_of_nonneg_right hb.2.1 hS + linarith only [hh,ht,hS] + have hnoise : |historyNoise (fun _ => step) + (fun i G => ((triangles G).card : ℝ)/earlyTriangleScale n i) (prefixTime n) j ω| < codegreeNoiseRadius n := by + apply lt_of_not_ge + intro hcross + apply hnse + refine ⟨j,hj,hloads,hcap,?_⟩ + simpa only [triangle_copyCount] using hcross + have hd := early_triangle_drift_bound (prefixEdgeRadius n) hn0 hp hb.1 hb.2.1 + hr.2.2.1 j hj ω hsafe + have hinit : |(triangles (ω (historyIndex (prefixTime n) 0))).card/earlyTriangleScale n 0-1| ≤ 4/(n : ℝ) := by + rw [hi',initial_triangle_relative_error hn,neg_div,abs_neg,abs_of_nonneg (by positivity : (0 : ℝ) ≤ 1/((n : ℝ)-1)^2)] + apply (div_le_div_iff₀ (sq_pos_of_ne_zero (by linarith)) (by linarith : (0 : ℝ) < n)).mpr + nlinarith + have ht := (abs_add_le + (((triangles (ω (historyIndex (prefixTime n) j))).card : ℝ)/earlyTriangleScale n j- + (triangles (ω (historyIndex (prefixTime n) 0))).card/earlyTriangleScale n 0- + historyNoise (fun _ => step) (fun i G => ((triangles G).card : ℝ)/earlyTriangleScale n i) (prefixTime n) j ω) + (historyNoise (fun _ => step) (fun i G => ((triangles G).card : ℝ)/earlyTriangleScale n i) (prefixTime n) j ω)) + have ht' := abs_add_le + (((triangles (ω (historyIndex (prefixTime n) j))).card : ℝ)/earlyTriangleScale n j- + (triangles (ω (historyIndex (prefixTime n) 0))).card/earlyTriangleScale n 0) + (((triangles (ω (historyIndex (prefixTime n) 0))).card : ℝ)/earlyTriangleScale n 0-1) + simp only [sub_add_cancel] at ht + simp only [sub_add_sub_cancel] at ht' + linarith only [ht,ht',hd,hnoise,hinit,hb.2.2] + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixWeight.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixWeight.lean new file mode 100644 index 0000000000000000000000000000000000000000..b1ad9a30b64320bff5f39f2c0381de8e86d009aa --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/PrefixWeight.lean @@ -0,0 +1,101 @@ +import Mathlib + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter +open scoped BigOperators + +namespace SharpTerminalLeave + +def prefixWeight (w : ℕ → ℝ) (t : ℕ) : ℝ := ∑ i ∈ Finset.range t, w i + +def orderedWeight (w : ℕ → ℝ) : ℕ → ℕ → ℝ + | 0, _ => 1 + | j + 1, t => ∑ i ∈ Finset.range t, w i * orderedWeight w j i + +@[simp] theorem prefixWeight_zero (w : ℕ → ℝ) : prefixWeight w 0 = 0 := by + simp [prefixWeight] + +@[simp] theorem prefixWeight_succ (w : ℕ → ℝ) (t : ℕ) : + prefixWeight w (t + 1) = prefixWeight w t + w t := Finset.sum_range_succ _ _ + +theorem prefixWeight_nonneg {w : ℕ → ℝ} (hw : ∀ i, 0 ≤ w i) (t : ℕ) : + 0 ≤ prefixWeight w t := Finset.sum_nonneg (fun i _ => hw i) + +theorem orderedWeight_nonneg {w : ℕ → ℝ} (hw : ∀ i, 0 ≤ w i) (j t : ℕ) : + 0 ≤ orderedWeight w j t := by + induction j generalizing t with + | zero => exact zero_le_one + | succ j ih => exact Finset.sum_nonneg (fun i _ => mul_nonneg (hw i) (ih i)) + +theorem power_increment_lower (x y : ℝ) (hx : 0 ≤ x) (hy : 0 ≤ y) (j : ℕ) : + ((j + 1 : ℕ) : ℝ) * y * x ^ j ≤ (x + y) ^ (j + 1) - x ^ (j + 1) := by + induction j with + | zero => simp + | succ j ih => + have hmul := mul_le_mul_of_nonneg_left ih hx + have hpow := mul_le_mul_of_nonneg_left + (pow_le_pow_left₀ hx (le_add_of_nonneg_right hy) (j + 1)) hy + simp only [Nat.cast_add, Nat.cast_one] at ih hmul ⊢ + rw [pow_succ (x + y) (j + 1), pow_succ x (j + 1)] + rw [pow_succ x j] at hpow hmul ⊢ + nlinarith + +theorem orderedWeight_factorial {w : ℕ → ℝ} (hw : ∀ i, 0 ≤ w i) (j t : ℕ) : + (j.factorial : ℝ) * orderedWeight w j t ≤ prefixWeight w t ^ j := by + induction j generalizing t with + | zero => simp [orderedWeight] + | succ j ih => + rw [orderedWeight, Nat.factorial_succ, Nat.cast_mul, Finset.mul_sum] + calc + _ ≤ ∑ i ∈ Finset.range t, + ((j + 1 : ℕ) : ℝ) * w i * prefixWeight w i ^ j := by + apply Finset.sum_le_sum + intro i _ + have hh := mul_le_mul_of_nonneg_left (ih i) + (mul_nonneg (Nat.cast_nonneg (j + 1)) (hw i)) + convert hh using 1; ring + _ ≤ ∑ i ∈ Finset.range t, + (prefixWeight w (i + 1) ^ (j + 1) - prefixWeight w i ^ (j + 1)) := by + apply Finset.sum_le_sum + intro i _ + rw [prefixWeight_succ] + exact power_increment_lower _ _ (prefixWeight_nonneg hw i) (hw i) j + _ = prefixWeight w t ^ (j + 1) := by + rw [Finset.sum_range_sub (fun i => prefixWeight w i ^ (j + 1))] + simp + +theorem orderedWeight_le {w : ℕ → ℝ} (hw : ∀ i, 0 ≤ w i) (j t : ℕ) : + orderedWeight w j t ≤ prefixWeight w t ^ j / (j.factorial : ℝ) := by + apply (le_div_iff₀ (Nat.cast_pos.mpr (Nat.factorial_pos j))).mpr + simpa only [mul_comm] using orderedWeight_factorial hw j t + +theorem three_segment_weight_le {w : ℕ → ℝ} (hw : ∀ i, 0 ≤ w i) + (h a b t : ℕ) : + orderedWeight w h t * orderedWeight w a t * orderedWeight w b t ≤ + prefixWeight w t ^ (h + a + b) / + ((h.factorial : ℝ) * (a.factorial : ℝ) * (b.factorial : ℝ)) := by + have hh := orderedWeight_le hw h t + have ha := orderedWeight_le hw a t + have hb := orderedWeight_le hw b t + have hp := mul_le_mul hh ha (orderedWeight_nonneg hw a t) + (div_nonneg (pow_nonneg (prefixWeight_nonneg hw t) _) (Nat.cast_nonneg _)) + have hp' := mul_le_mul hp hb (orderedWeight_nonneg hw b t) + (mul_nonneg + (div_nonneg (pow_nonneg (prefixWeight_nonneg hw t) _) (Nat.cast_nonneg _)) + (div_nonneg (pow_nonneg (prefixWeight_nonneg hw t) _) (Nat.cast_nonneg _))) + calc + _ ≤ _ := hp' + _ = _ := by rw [pow_add, pow_add, div_mul_div_comm, div_mul_div_comm] + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedCompletionMax.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedCompletionMax.lean new file mode 100644 index 0000000000000000000000000000000000000000..f2c4882b5502cf2285d345abde602190f6edc94b --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedCompletionMax.lean @@ -0,0 +1,308 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.RootedTemplateEdgesCard +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixTimeSquare +import OAI.Combinatorics.TriangleRemoval.Process.OverlapVertices +import OAI.Combinatorics.TriangleRemoval.Tracking.PrefixEdgeRadius + +namespace OAI + +section +noncomputable section +open scoped BigOperators +open Filter Classical + +namespace SharpTerminalLeave + +def rootedCompletionMax {k : ℕ} (T : RootedTemplate k) (n : ℕ) (p : ℝ) : ℝ := + Finset.univ.sup' Finset.univ_nonempty (fun J : Finset (Fin k) => + if T.roots ⊆ J then (n : ℝ)^(k-J.card)*p^(T.edges.card-(T.edges.filter (· ⊆ J)).card) else 1) + +lemma rootedCompletionMax_ge_one {k : ℕ} (T : RootedTemplate k) (n : ℕ) (p : ℝ) : + 1 ≤ rootedCompletionMax T n p := by + unfold rootedCompletionMax + apply Finset.le_sup'_of_le _ (Finset.mem_univ (Finset.univ : Finset (Fin k))) + simp + +lemma rootedCompletionMax_bound {k : ℕ} (T : RootedTemplate k) (n : ℕ) (p : ℝ) + (J : Finset (Fin k)) (hJ : T.roots ⊆ J) : + (n : ℝ)^(k-J.card)*p^(T.edges.card-(T.edges.filter (· ⊆ J)).card) ≤ + rootedCompletionMax T n p := by + unfold rootedCompletionMax + apply Finset.le_sup'_of_le _ (Finset.mem_univ J) + simp only [hJ,ite_true,le_refl] + +lemma rootedCompletionMax_large {k n : ℕ} (T : RootedTemplate k) {p : ℝ} + (hp : 0 ≤ p) (hT : largeTemplateEligible T n p) : + rootedCompletionMax T n p ≤ rootedScaling T n p := by + have h1 : 1 ≤ rootedScaling T n p := by + simpa only [rootedScaling,Finset.card_univ,Fintype.card_fin] using + hT Finset.univ (Finset.subset_univ _) T.edges (Finset.Subset.refl _) + (fun _ _ => Finset.subset_univ _) + apply Finset.sup'_le + intro J _ + split_ifs with hJ + · exact largeTemplate_completion_scale T hp hT J hJ + · exact h1 + +lemma rootedCompletionMax_small {k n : ℕ} (T : RootedTemplate k) {p : ℝ} + (hT : smallTemplateEligible T n p) : rootedCompletionMax T n p ≤ 1 := by + apply Finset.sup'_le + intro J _ + split_ifs with hJ + · exact hT J hJ + · exact le_rfl + +theorem rooted_tail_from_survival {k n : ℕ} {Ω : Type*} [Fintype Ω] + (T : RootedTemplate k) (ψ : {v // v ∈ T.roots} ↪ Fin n) + (μ : PMF Ω) (state : Ω → Graph n) (A : Ω → Prop) + (q : ℕ) {p B U : ℝ} (hp : 0 ≤ p) (hp1 : p ≤ 1) (hB : 0 < B) (hU : 0 ≤ U) + (hcoef : 2*(2*((((q+1)*k : ℕ) : ℝ)+1))^k ≤ B) + (hsurvive : ∀ F : Graph n, F.card ≤ q*T.edges.card → + pmfMean μ (fun ω => if A ω then intact F (state ω) else 0) ≤ U*p^F.card) : + pmfMean μ (fun ω => if A ω ∧ B*rootedCompletionMax T n p ≤ rootedCount T ψ (state ω) + then 1 else 0) ≤ U*(1/2 : ℝ)^q := by + let M := rootedCompletionMax T n p + have hM : 0 < M := lt_of_lt_of_le zero_lt_one (rootedCompletionMax_ge_one T n p) + have hm := rooted_count_moment_from_survival T ψ μ state A q hp hp1 hM.le hU + (rootedCompletionMax_bound T n p) hsurvive + have ht := pmfMean_stopped_power_tail μ A (fun ω => rootedCount T ψ (state ω)) + (fun ω => copyCount_nonneg _ _) q (mul_pos hB hM) hm + apply ht.trans + have he : U*((2*((((q+1)*k : ℕ) : ℝ)+1))^k*M)^q/(B*M)^q = + U*((2*((((q+1)*k : ℕ) : ℝ)+1))^k/B)^q := by + rw [mul_div_assoc,← div_pow,mul_div_mul_right _ _ hM.ne'] + rw [he] + apply mul_le_mul_of_nonneg_left _ hU + apply pow_le_pow_left₀ (by positivity) + apply (div_le_iff₀ hB).mpr + linarith only [hcoef] + +noncomputable def rootedMomentOrder (n : ℕ) : ℕ := ⌊(1+Real.log (n : ℝ))^2⌋₊ + +lemma rooted_moment_edge_cap (H : ℕ) : ∀ᶠ n : ℕ in atTop, + ∀ k ≤ H, ∀ T : RootedTemplate k, ((rootedMomentOrder n*T.edges.card : ℕ) : ℝ) ≤ + (n : ℝ)^(1/200000 : ℝ) := by + have hc : (0 : ℝ) < 1/((H : ℝ)^2+1) := by positivity + filter_upwards [prefixTemplateFactor_subpower 2 (by norm_num : (0 : ℝ) < 1/200000) hc, + eventually_ge_atTop (1 : ℕ)] with n hb hn + have hn1 : (1 : ℝ) ≤ n := by exact_mod_cast hn + have hl : 0 ≤ 1+Real.log (n : ℝ) := by have := Real.log_nonneg hn1; linarith + have hq : (rootedMomentOrder n : ℝ) ≤ (1+Real.log (n : ℝ))^2 := Nat.floor_le (sq_nonneg _) + have hpow : (1+Real.log (n : ℝ))^(2 : ℝ) = (1+Real.log (n : ℝ))^2 := Real.rpow_two _ + change (1+Real.log (n : ℝ))^(2 : ℝ) ≤ _ at hb + rw [hpow] at hb + intro k hk T + have he : (T.edges.card : ℝ) ≤ (H : ℝ)^2 := by + exact_mod_cast (rootedTemplate_edges_card_le_sq T).trans (Nat.pow_le_pow_left hk 2) + push_cast + calc + _ ≤ (1+Real.log (n : ℝ))^2*((H : ℝ)^2+1) := + mul_le_mul hq (by linarith only [he]) (Nat.cast_nonneg _) (sq_nonneg _) + _ ≤ (1/((H : ℝ)^2+1)*(n : ℝ)^(1/200000 : ℝ))*((H : ℝ)^2+1) := + mul_le_mul_of_nonneg_right hb (by positivity) + _ = _ := by field_simp + +lemma rooted_moment_coefficient (H : ℕ) : ∀ᶠ n : ℕ in atTop, + 0 < (1+Real.log (n : ℝ))^(3*H+2) ∧ + ∀ k ≤ H, 2*(2*((((rootedMomentOrder n+1)*k : ℕ) : ℝ)+1))^k ≤ + (1+Real.log (n : ℝ))^(3*H+2) := by + have hlog := Real.tendsto_log_atTop.comp (tendsto_natCast_atTop_atTop (R := ℝ)) + filter_upwards [hlog.eventually_ge_atTop (2*(2*(H : ℝ)+1))] with n hn + change 2*(2*(H : ℝ)+1) ≤ Real.log (n : ℝ) at hn + let L : ℝ := 1+Real.log (n : ℝ) + have hL : 2 ≤ L := by dsimp [L]; have := Nat.cast_nonneg (α := ℝ) H; linarith + have hL0 : 0 < L := by linarith + have hL1 : 1 ≤ L := by linarith + have hq : (rootedMomentOrder n : ℝ) ≤ L^2 := Nat.floor_le (sq_nonneg _) + refine ⟨pow_pos hL0 _,?_⟩ + intro k hk + have hkR : (k : ℝ) ≤ H := by exact_mod_cast hk + have hcoeff : 2*((((rootedMomentOrder n+1)*k : ℕ) : ℝ)+1) ≤ L^3 := by + push_cast + have hq' : (rootedMomentOrder n : ℝ)+1 ≤ 2*L^2 := by nlinarith [sq_nonneg (L-1)] + have hmul := mul_le_mul hq' hkR (Nat.cast_nonneg k) (by positivity : (0 : ℝ) ≤ 2*L^2) + have hc : 2*(2*(H : ℝ)+1) ≤ L := by dsimp [L]; linarith + have hhc := mul_le_mul_of_nonneg_right hc (sq_nonneg L) + nlinarith only [hmul,hhc,sq_nonneg (L-1),hL] + have hcoeff0 : 0 ≤ 2*((((rootedMomentOrder n+1)*k : ℕ) : ℝ)+1) := by positivity + have hpow : (2*((((rootedMomentOrder n+1)*k : ℕ) : ℝ)+1))^k ≤ L^(3*H) := by + calc + _ ≤ (L^3)^k := pow_le_pow_left₀ hcoeff0 hcoeff k + _ = L^(3*k) := (pow_mul L 3 k).symm + _ ≤ _ := pow_le_pow_right₀ hL1 (by omega) + change 2*_ ≤ L^(3*H+2) + rw [pow_add] + have htail : 2 ≤ L^2 := by nlinarith + exact mul_le_mul htail hpow (by positivity) (by positivity) |>.trans_eq (mul_comm _ _) + +lemma historyAlive_past {α : Type*} (safe : ℕ → α → Prop) (T j : ℕ) + (ω π : History α T) (h : ∀ i : Fin (T+1), i.val ≤ min j T → ω i = π i) : + historyAlive safe T j ω ↔ historyAlive safe T j π := by + have he (i : ℕ) (hi : i < j) : ω (historyIndex T i) = π (historyIndex T i) := by + apply h + exact min_le_min_right T hi.le + unfold historyAlive + constructor + · intro hA i hi; rw [← he i hi]; exact hA i hi + · intro hA i hi; rw [he i hi]; exact hA i hi + +lemma stopped_state_mean_past {α : Type*} [Fintype α] (initial : PMF α) + (K : ℕ → α → PMF α) (safe : ℕ → α → Prop) (T j : ℕ) (hj : j ≤ T) + (P : α → Prop) : + pmfMean (historyLaw initial K T T) + (fun ω => if historyAlive safe T j ω ∧ P (ω (historyIndex T j)) then 1 else 0) = + pmfMean (historyLaw initial K T j) + (fun ω => if historyAlive safe T j ω ∧ P (ω (historyIndex T j)) then 1 else 0) := by + apply historyLaw_past_mean initial K T j T hj + intro ω π h + have he := h (historyIndex T j) (le_refl _) + rw [he,historyAlive_past safe T j ω π h] + +theorem prefix_rooted_killed_tail (H : ℕ) : ∀ᶠ n : ℕ in atTop, + ∀ j ≤ prefixTime n, ∀ k ≤ H, ∀ T : RootedTemplate k, + ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n)) + (fun ω => if historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) + (prefixTime n) j ω ∧ + (1+Real.log (n : ℝ))^(3*H+2)*rootedCompletionMax T n (earlyDensity n j) ≤ + rootedCount T ψ (ω (historyIndex (prefixTime n) j)) then 1 else 0) ≤ + Real.exp 1*(1/2 : ℝ)^(rootedMomentOrder n) := by + filter_upwards [prefix_killed_survival,rooted_moment_edge_cap H, + rooted_moment_coefficient H,prefixDensity_eventually_inverse_lower, + eventually_ge_atTop (1 : ℕ)] with n hsurv hcap hcoef hp hn + intro j hj k hk T ψ + rw [stopped_state_mean_past _ _ _ _ _ hj + (fun G => (1+Real.log (n : ℝ))^(3*H+2)*rootedCompletionMax T n (earlyDensity n j) ≤ + rootedCount T ψ G)] + apply rooted_tail_from_survival T ψ _ _ _ (rootedMomentOrder n) + (early_density_positive (by omega) hp hj).le (earlyDensity_le_one n j) + hcoef.1 (Real.exp_pos 1).le (hcoef.2 k hk) + intro F hF + apply hsurv j hj F + exact (show (F.card : ℝ) ≤ ((rootedMomentOrder n*T.edges.card : ℕ) : ℝ) by exact_mod_cast hF).trans + (hcap k hk T) + +lemma rooted_moment_tail_logsquare : ∀ᶠ n : ℕ in atTop, + Real.exp 1*(1/2 : ℝ)^(rootedMomentOrder n) ≤ Real.exp (-(Real.log (n : ℝ))^2/4) := by + have hlog : Tendsto (fun n : ℕ => Real.log (n : ℝ)) atTop atTop := + Real.tendsto_log_atTop.comp tendsto_natCast_atTop_atTop + filter_upwards [hlog.eventually_ge_atTop 3] with n hn + have hq : (1+Real.log (n : ℝ))^2 < (rootedMomentOrder n : ℝ)+1 := Nat.lt_floor_add_one _ + have hlog2 : (1 : ℝ)/2 ≤ Real.log 2 := by + have hh := Real.one_sub_inv_le_log_of_pos (by norm_num : (0 : ℝ) < 2) + norm_num at hh ⊢ + exact hh + have he : (1/2 : ℝ)^(rootedMomentOrder n) = + Real.exp (-(rootedMomentOrder n : ℝ)*Real.log 2) := by + rw [neg_mul,Real.exp_neg,Real.exp_nat_mul,Real.exp_log (by norm_num : (0 : ℝ) < 2)] + simp [inv_pow,one_div] + rw [he,← Real.exp_add] + apply Real.exp_le_exp.mpr + have hh := mul_le_mul_of_nonneg_left hlog2 (Nat.cast_nonneg (rootedMomentOrder n)) + nlinarith only [hh,hq,hn,sq_nonneg (Real.log (n : ℝ))] + +theorem polynomial_logsquare_le_prefix_margin (A K : ℝ) : ∀ᶠ n : ℕ in atTop, + A*(n : ℝ)^K*Real.exp (-(Real.log (n : ℝ))^2/4) ≤ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := by + have hlog : Tendsto (fun n : ℕ => Real.log (n : ℝ)) atTop atTop := + Real.tendsto_log_atTop.comp tendsto_natCast_atTop_atTop + have hpow := (tendsto_rpow_atTop (by norm_num : (0 : ℝ) < 2/3)).comp hlog + filter_upwards [hlog.eventually_ge_atTop (8*(K+1)),hlog.eventually_ge_atTop 1, + hpow.eventually_ge_atTop 16,tendsto_natCast_atTop_atTop.eventually_ge_atTop A, + eventually_ge_atTop (1 : ℕ)] with n hlinear hl hpow hA hn + have hn0 : (0 : ℝ) < n := by exact_mod_cast (by omega : 0 < n) + have hl0 : 0 < Real.log (n : ℝ) := by linarith + have hlin := mul_le_mul_of_nonneg_right hlinear hl0.le + have hpow' : 16*(Real.log (n : ℝ))^(4/3 : ℝ) ≤ (Real.log (n : ℝ))^2 := by + have hm := mul_le_mul_of_nonneg_right hpow (Real.rpow_nonneg hl0.le (4/3 : ℝ)) + have he : (Real.log (n : ℝ))^(2/3 : ℝ)*(Real.log (n : ℝ))^(4/3 : ℝ) = + (Real.log (n : ℝ))^2 := by + rw [← Real.rpow_add hl0]; norm_num [Real.rpow_two] + exact hm.trans_eq he + calc + _ ≤ (n : ℝ)*(n : ℝ)^K*Real.exp (-(Real.log (n : ℝ))^2/4) := + mul_le_mul_of_nonneg_right (mul_le_mul_of_nonneg_right hA (Real.rpow_nonneg hn0.le _)) + (Real.exp_pos _).le + _ = Real.exp ((K+1)*Real.log n-(Real.log (n : ℝ))^2/4) := by + rw [show (n : ℝ)*(n : ℝ)^K = (n : ℝ)^(K+1) by + rw [Real.rpow_add hn0,Real.rpow_one]; ring] + rw [Real.rpow_def_of_pos hn0,← Real.exp_add] + congr 1 + ring + _ ≤ _ := Real.exp_le_exp.mpr (by nlinarith only [hlin,hpow']) + +def PrefixRootedUpper (H n : ℕ) (ω : History (Graph n) (prefixTime n)) : Prop := + ∀ j ≤ prefixTime n, + historyAlive (fun i => densityEdgeSafe n (earlyDensity n i) (prefixEdgeRadius n)) + (prefixTime n) j ω → + ∀ k ≤ H, ∀ T : RootedTemplate k, ∀ ψ : {v // v ∈ T.roots} ↪ Fin n, + rootedCount T ψ (ω (historyIndex (prefixTime n) j)) < + (1+Real.log (n : ℝ))^(3*H+2)*rootedCompletionMax T n (earlyDensity n j) + +theorem prefix_rooted_upper_failure (H : ℕ) : ∀ᶠ n : ℕ in atTop, + pmfMean (historyLaw (PMF.pure (completeGraph n)) (fun _ => step) + (prefixTime n) (prefixTime n)) + (fun ω => if PrefixRootedUpper H n ω then 0 else 1) ≤ + Real.exp (-2*(Real.log (n : ℝ))^(4/3 : ℝ)) := by + filter_upwards [prefix_rooted_killed_tail H,rooted_moment_tail_logsquare, + polynomial_logsquare_le_prefix_margin + ((rootedFamilySize H : ℝ)*2^(H+2)) (H+2), + eventually_ge_atTop (1 : ℕ)] with n htail hnum hbudget hn + let μ := historyLaw (PMF.pure (completeGraph n)) (fun _ => step) (prefixTime n) (prefixTime n) + let P (i : Fin (prefixTime n+1) × RootedCountIndex H n) + (ω : History (Graph n) (prefixTime n)) := + historyAlive (fun j => densityEdgeSafe n (earlyDensity n j) (prefixEdgeRadius n)) + (prefixTime n) i.1.val ω ∧ + (1+Real.log (n : ℝ))^(3*H+2)* + rootedCompletionMax i.2.2.1 n (earlyDensity n i.1.val) ≤ + rootedCount i.2.2.1 i.2.2.2 (ω (historyIndex (prefixTime n) i.1.val)) + have he (ω : History (Graph n) (prefixTime n)) : + ¬ PrefixRootedUpper H n ω ↔ ∃ i ∈ Finset.univ, P i ω := by + simp only [PrefixRootedUpper,not_forall,not_lt] + constructor + · rintro ⟨j,hj,ha,k,hk,T,ψ,hbad⟩ + exact ⟨(⟨j,by omega⟩,⟨⟨k,by omega⟩,T,ψ⟩),Finset.mem_univ _,ha,hbad⟩ + · rintro ⟨i,_,ha,hbad⟩ + exact ⟨i.1.val,by omega,ha,i.2.1.val,by omega,i.2.2.1,i.2.2.2,hbad⟩ + have hu := pmfMean_event_union μ Finset.univ P + have hs : (∑ i, pmfMean μ (fun ω => if P i ω then 1 else 0)) ≤ + (Fintype.card (Fin (prefixTime n+1) × RootedCountIndex H n) : ℝ)* + Real.exp (-(Real.log (n : ℝ))^2/4) := by + calc + _ ≤ ∑ _i : Fin (prefixTime n+1) × RootedCountIndex H n, + Real.exp (-(Real.log (n : ℝ))^2/4) := by + apply Finset.sum_le_sum + intro i _ + exact (htail i.1.val (by omega) i.2.1.val (by omega) i.2.2.1 i.2.2.2).trans hnum + _ = _ := by simp + have hc : (Fintype.card (Fin (prefixTime n+1) × RootedCountIndex H n) : ℝ) ≤ + (rootedFamilySize H : ℝ)*2^(H+2)*(n : ℝ)^(H+2 : ℕ) := by + have hh := rooted_noise_chances_le H n hn + have hh' : Fintype.card (Fin (prefixTime n+1) × RootedCountIndex H n) ≤ + rootedFamilySize H*2^(H+2)*n^(H+2) := by + simp only [Fintype.card_prod,Fintype.card_fin] + nlinarith only [hh] + exact_mod_cast hh' + have hev : (n : ℝ)^((H : ℝ)+2) = (n : ℝ)^(H+2 : ℕ) := by + rw [← Real.rpow_natCast]; norm_cast + rw [hev] at hbudget + have hsumfinal := hs.trans ((mul_le_mul_of_nonneg_right hc + (Real.exp_pos _).le).trans hbudget) + have hfinal := hu.trans (by + convert hsumfinal using 1; try rfl + apply Finset.sum_congr rfl + intro i _ + apply congrArg (pmfMean μ) + funext ω + by_cases h : P i ω <;> simp only [h,ite_true,ite_false]) + convert hfinal using 1; try rfl + apply congrArg (pmfMean μ) + funext ω + simp only [← he] + split_ifs <;> rfl + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedCountedTracedRepeat.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedCountedTracedRepeat.lean new file mode 100644 index 0000000000000000000000000000000000000000..ac0a2618f227ffbdd403be1dc9defebfdef5a22a --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedCountedTracedRepeat.lean @@ -0,0 +1,126 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.WitnessEmbeddingBound +import OAI.Combinatorics.TriangleRemoval.Embeddings.EmbeddedWitnessRoots + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open scoped BigOperators Topology +open Filter +open scoped BigOperators + +namespace SharpTerminalLeave + +theorem rooted_counted_traced_repeat_cover (c₀ C₀ : ℝ) (hc₀ : 0 < c₀) : + ∀ᶠ n : ℕ in atTop, ∀ (G : Graph n), GoodPrefixGraph n c₀ C₀ G → + ∀ (focus : Graph n) + (parent : Option (Finset (Fin n))) (_hM : EdgeMatching focus) (_hG : focus ⊆ G) + (_hr : focus.card ≤ 2) + (_hc : (QueryCall.mk [] focus parent).Separated (triangleHypergraph G)) + (N d k : ℕ) (ν : Finset (Fin n) → PMF (Fin N)) + (z : (Bool × List (QueryCall (Finset (Fin n)) (Finset (Fin n)))) × List (Finset (Fin n))) + (_hz : z ∈ (ExposureTree.freshLog ν + (tracedGridQuery (triangleHypergraph G) N d k ⟨[],focus,parent⟩)).support) + (_hrep : ExposureTree.repeats ∅ z.2 = true), + ∃ W : BoundedEmbeddedWitness G (focus.biUnion id).card d, + WitnessEmbeddingBound focus.card W ∧ embeddedWitnessRoots W = focus ∧ + (∃ a ∈ z.1.2, a.address = (embeddedWitnessPaths W).1.reverse) ∧ + (∃ b ∈ z.1.2, b.address = (embeddedWitnessPaths W).2.reverse) := by + classical + filter_upwards [actual_witness_count_roots_eventually c₀ C₀ hc₀] with n hn + intro G hGood focus parent hM hG hr hc N d k ν z hz hrep + let c : QueryCall (Finset (Fin n)) (Finset (Fin n)) := ⟨[],focus,parent⟩ + let F := extractCallForest (triangleHypergraph G) N d k c ν z hz + let A := F.toTriangleGrowth hM hG + have hR : F.rootCount ≤ F.vertexCount := Nat.le_add_right _ _ + obtain ⟨a,b,he,hi⟩ := traced_graph_path_collision_witness G N d k c hM hG hc ν z hz hrep + have hdepth (i : Fin F.size) : (F.call i).address.length ≤ d := by + obtain ⟨p,hp,he⟩ := tracedGridQuery_legal_path (triangleHypergraph G) N d k c ν hz + (F.call i) (List.get_mem z.1.2 i) + rw [he,callAtPath_address] + simpa only [c,List.append_nil,List.length_reverse] using + (mem_legalPaths (triangleHypergraph G) d focus parent p).mp hp |>.2 + let key := indexedTriangleKey (A.pathBirthIndex a b hR) (A.pathAttachments a b hR) + (A.pathAssignment a b hi) + have hs : (A.pathSuffix a b F.rootCount).card ≤ 2*d := by + have h := (A.pathForest a b).two_mark_size key + (suffixProjection (A.path_card_split a b hR) (A.pathIndex a b a (A.left_mem_pathUnion a b))) + (suffixProjection (A.path_card_split a b hR) (A.pathIndex a b b (A.right_mem_pathUnion a b))) + (A.pathWitnessCode a b hR).2.1.property + rw [F.decoded_mark_address hM hG rfl a b hR hi a (A.left_mem_pathUnion a b), + F.decoded_mark_address hM hG rfl a b hR hi b (A.right_mem_pathUnion a b)] at h + have ha := hdepth (F.vertexCall a) + have hb := hdepth (F.vertexCall b) + omega + let q : WitnessDimensions F.rootCount d := + ⟨(⟨(A.pathUnion a b).card,by rw [A.path_card_split a b hR]; omega⟩, + ⟨(A.pathSuffix a b F.rootCount).card,by omega⟩),A.path_card_split a b hR⟩ + have hseed : (A.seed.backEdges A.roots).card ≤ 2 := by + rw [F.growth_seed_card hM hG] + exact hr + let E : {E : Graph (A.pathUnion a b).card // E ∈ seedPatternChoices (A.pathUnion a b).card F.rootCount} := + ⟨A.pathRootEdges a b,A.pathRootEdges_mem_seedPatternChoices a b hR hseed⟩ + let W : BoundedEmbeddedWitness G F.rootCount d := + ⟨q,E,A.pathWitnessCode a b hR,A.pathAssignment a b hi,A.pathAssignment_mem_decoded hR hi he⟩ + have hleft : (embeddedWitnessPaths W).1.reverse = (F.call (F.vertexCall a)).address := by + change (((suffixProjection (A.path_card_split a b hR) + (A.pathIndex a b a (A.left_mem_pathUnion a b))).elim [] + ((A.pathForest a b).address key)).reverse).reverse = _ + rw [List.reverse_reverse] + exact F.decoded_mark_address hM hG rfl a b hR hi a (A.left_mem_pathUnion a b) + have hright : (embeddedWitnessPaths W).2.reverse = (F.call (F.vertexCall b)).address := by + change (((suffixProjection (A.path_card_split a b hR) + (A.pathIndex a b b (A.right_mem_pathUnion a b))).elim [] + ((A.pathForest a b).address key)).reverse).reverse = _ + rw [List.reverse_reverse] + exact F.decoded_mark_address hM hG rfl a b hR hi b (A.right_mem_pathUnion a b) + have hbound : WitnessEmbeddingBound focus.card W := by + have hh := hn G hGood focus.card hr F.vertexCount F.rootCount A hR + (F.rootCount_focus_card hM) (F.growth_seed_card hM hG) a b he + change ((graphEmbeddingSet (decodeWitnessPattern (A.pathWitnessCode a b hR)).2 G).card : ℝ) ≤ + (4*prefixM n)^focus.card*(2*prefixD n)^(A.pathSuffix a b F.rootCount).card/prefixD n^50 + change ((graphEmbeddingSet (insert {A.pathIndex a b a (A.left_mem_pathUnion a b), + A.pathIndex a b b (A.right_mem_pathUnion a b)} + (indexedPatternEdges (A.pathRootEdges a b) (A.pathBirthIndex a b hR) + (A.pathAttachments a b hR))) G).card : ℝ) ≤ _ + rw [← A.pathPattern_edges a b hR] + exact hh + have hroots : embeddedWitnessRoots W = focus := by + change (A.pathRootEdges a b).image (Finset.image (A.pathAssignment a b hi)) = c.focus + exact (A.pathRootEdges_image a b hi).trans (F.growth_seed_image hM hG) + exact ⟨W,hbound,hroots,⟨F.call (F.vertexCall a),List.get_mem z.1.2 _,hleft.symm⟩, + ⟨F.call (F.vertexCall b),List.get_mem z.1.2 _,hright.symm⟩⟩ + +end SharpTerminalLeave + +open scoped BigOperators ENNReal Classical +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +namespace SharpTerminalLeave + +noncomputable instance rootedCountedTracedRepeatIndexedWitnessCodeFintype + {K s : ℕ} (E : Graph K) (birth : Fin s → Fin K) + (mark : Fin K → Option (Fin s)) : Fintype (IndexedWitnessCode E birth mark) := by + classical + unfold IndexedWitnessCode + infer_instance + +end SharpTerminalLeave + +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open Filter MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedExposedCollisionWitness.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedExposedCollisionWitness.lean new file mode 100644 index 0000000000000000000000000000000000000000..139bf78154c7f96e42a91860a67b8aa61057d75b --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedExposedCollisionWitness.lean @@ -0,0 +1,51 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.BirthGraph + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology +open scoped BigOperators + +namespace SharpTerminalLeave.BirthGraph +variable {N : ℕ} (B : BirthGraph N) +variable {V : Type*} [DecidableEq V] (G : SimpleGraph V) (label : Fin N → V) + +theorem rooted_exposed_collision_witness (hrules : B.SpawnRules G label) + (R : ℕ) (hroots : Set.InjOn label {v : Fin N | v.val < R}) + (hnonroot : ∀ v : Fin N, R ≤ v.val → (B.older v).card = 2) + {v w : Fin N} (hne : v ≠ w) {T : Finset V} + (hv : B.ExposedAt G label v T) (hw : B.ExposedAt G label w T) : + ∃ S : Set (Fin N), IsLowerSet S ∧ Set.InjOn label S ∧ + (∀ x : Fin N, x.val < R → x ∈ S) ∧ + ∃ a ∈ S, ∃ b ∈ S, B.ExtraEdge G label a b := by + classical + by_cases hinj : Function.Injective label + · have hex : ∃ a b, B.ExtraEdge G label a b := by + by_contra hn + have hno : ∀ a b, ¬ B.ExtraEdge G label a b := by simpa only [not_exists] using hn + exact B.no_repeated_exposure G label hinj hno hne hv hw + obtain ⟨a,b,he⟩ := hex + exact ⟨Set.univ,isLowerSet_univ,hinj.injOn,fun _ _ => Set.mem_univ _, + a,Set.mem_univ _,b,Set.mem_univ _,he⟩ + · obtain ⟨z,u,huz,hzu,hpre⟩ := exists_first_repeat label hinj + have hz : R ≤ z.val := by + by_contra hn + have hzR : z.val < R := Nat.lt_of_not_ge hn + have huR : u.val < R := lt_trans huz hzR + exact (ne_of_lt huz) (hroots hzR huR hzu).symm + obtain ⟨a,b,ha,hb,he⟩ := + B.first_label_repeat_extra G label hrules (hnonroot z hz) huz hzu + exact ⟨Set.Iio z,isLowerSet_Iio z,hpre,fun x hx => lt_of_lt_of_le hx hz, + a,ha,b,hb,he⟩ + +end SharpTerminalLeave.BirthGraph + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedInjectionEquiv.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedInjectionEquiv.lean new file mode 100644 index 0000000000000000000000000000000000000000..48be641011c3c0c1c0d6d40253adf8024c68250d --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedInjectionEquiv.lean @@ -0,0 +1,90 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.RootedTemplates + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def rootedInjectionEquiv {k n : ℕ} (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + RootedInjection T ψ ≃ + ({v : Fin k // v ∉ T.roots} ↪ {w : Fin n // w ∉ Set.range ψ}) := by + classical + let restrict (f : RootedInjection T ψ) : + {v : Fin k // v ∉ T.roots} ↪ {w : Fin n // w ∉ Set.range ψ} := + ⟨fun v => ⟨f.val v.val, by + rintro ⟨r,hr⟩ + have he : f.val r.val = f.val v.val := (f.property r).trans hr + exact v.property (f.val.injective he ▸ r.property)⟩, + fun a b h => Subtype.ext (f.val.injective (congrArg Subtype.val h))⟩ + let extend (g : {v : Fin k // v ∉ T.roots} ↪ + {w : Fin n // w ∉ Set.range ψ}) : RootedInjection T ψ := + ⟨⟨fun v => if h : v ∈ T.roots then ψ ⟨v,h⟩ else g ⟨v,h⟩, + by + intro a b hab + by_cases ha : a ∈ T.roots <;> by_cases hb : b ∈ T.roots + · simp only [ha,hb,↓reduceDIte] at hab + exact congrArg Subtype.val (ψ.injective hab) + · simp only [ha,hb,↓reduceDIte] at hab + exact False.elim ((g ⟨b,hb⟩).property ⟨⟨a,ha⟩,hab⟩) + · simp only [ha,hb,↓reduceDIte] at hab + exact False.elim ((g ⟨a,ha⟩).property ⟨⟨b,hb⟩,hab.symm⟩) + · simp only [ha,hb,↓reduceDIte] at hab + exact congrArg Subtype.val (g.injective (Subtype.ext hab))⟩, + by + intro v + change (if h : v.val ∈ T.roots then ψ ⟨v.val,h⟩ else (g ⟨v.val,h⟩).val) = ψ v + exact dite_eq_left v.property⟩ + refine ⟨restrict,extend,?_,?_⟩ + · intro f + apply Subtype.ext + apply Function.Embedding.ext + intro v + change (if h : v ∈ T.roots then ψ ⟨v,h⟩ else (restrict f ⟨v,h⟩).val) = f.val v + split_ifs with h + · exact (f.property ⟨v,h⟩).symm + · rfl + · intro g + apply Function.Embedding.ext + intro v + apply Subtype.ext + change (if h : v.val ∈ T.roots then ψ ⟨v.val,h⟩ else (g ⟨v.val,h⟩).val) = _ + simp only [v.property,↓reduceDIte] + +theorem rootedInjection_card {k n : ℕ} (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + Fintype.card (RootedInjection T ψ) = + (n - T.roots.card).descFactorial (k - T.roots.card) := by + classical + rw [Fintype.card_congr (rootedInjectionEquiv T ψ), Fintype.card_embedding_eq] + have hd : Fintype.card {v : Fin k // v ∉ T.roots} = k - T.roots.card := by + simp + have hc : Fintype.card {w : Fin n // w ∉ Set.range ψ} = n - T.roots.card := by + calc + _ = Fintype.card ↑(Set.range ψ)ᶜ := Fintype.card_congr (Equiv.refl _) + _ = _ := by simp only [Fintype.card_compl_set, Fintype.card_fin, + Fintype.card_range, Fintype.card_coe] + rw [hd,hc] + +theorem rootedCount_complete {k n : ℕ} (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + rootedCount T ψ (completeGraph n) = + ((n - T.roots.card).descFactorial (k - T.roots.card) : ℝ) := by + classical + have hh (φ : RootedInjection T ψ) : imageEdges T φ.val ⊆ completeGraph n := by + intro e he + exact mem_completeGraph.mpr (imageEdges_simple T φ.val he) + simp only [rootedCount, copyCount, intact, hh, ↓reduceIte, Finset.sum_const, + Finset.card_univ, nsmul_eq_mul, mul_one, rootedInjection_card] + +end SharpTerminalLeave + +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedInjectionEquivFree.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedInjectionEquivFree.lean new file mode 100644 index 0000000000000000000000000000000000000000..e6bab4f9491f045a7e97c5e0e1bd2497d85758f2 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedInjectionEquivFree.lean @@ -0,0 +1,96 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.RootedTemplates + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +noncomputable def rootedInjectionEquivFree {k n : ℕ} (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + RootedInjection T ψ ≃ + ({v : Fin k // v ∉ T.roots} ↪ {w : Fin n // w ∉ Set.range ψ}) := by + classical + refine { + toFun := fun φ => { + toFun := fun v => ⟨φ.val v,?_⟩ + inj' := ?_ } + invFun := fun f => ⟨{ + toFun := fun v => if hv : v ∈ T.roots then ψ ⟨v,hv⟩ else f ⟨v,hv⟩ + inj' := ?_ },?_⟩ + left_inv := ?_ + right_inv := ?_ } + · intro h + obtain ⟨a,ha⟩ := h + rw [← φ.property a] at ha + have hh : a.val = v.val := φ.val.injective ha + exact v.property (hh ▸ a.property) + · intro a b hab + apply Subtype.ext + exact φ.val.injective (congrArg Subtype.val hab) + · intro a b hab + dsimp at hab + split_ifs at hab with ha hb hb + · exact congrArg Subtype.val (ψ.injective hab) + · exact ((f ⟨b,hb⟩).property ⟨⟨a,ha⟩,hab⟩).elim + · exact ((f ⟨a,ha⟩).property ⟨⟨b,hb⟩,hab.symm⟩).elim + · exact congrArg Subtype.val (f.injective (Subtype.ext hab)) + · intro v + change (if hv : v.val ∈ T.roots then ψ ⟨v.val,hv⟩ else _) = ψ v + simp only [dite_eq_left v.property] + · intro φ + apply Subtype.ext + apply DFunLike.ext + intro v + change (if hv : v ∈ T.roots then ψ ⟨v,hv⟩ else φ.val v) = φ.val v + split_ifs with hv + · exact (φ.property ⟨v,hv⟩).symm + · rfl + · intro f + apply DFunLike.ext + intro v + apply Subtype.ext + change (if hv : v.val ∈ T.roots then ψ ⟨v.val,hv⟩ else (f ⟨v.val,hv⟩).val) = (f v).val + simp only [dite_eq_right v.property] + +theorem card_rootedInjection {k n : ℕ} (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + Fintype.card (RootedInjection T ψ) = + (n-T.roots.card).descFactorial (k-T.roots.card) := by + classical + rw [Fintype.card_congr (rootedInjectionEquivFree T ψ),Fintype.card_embedding_eq] + have hfree : Fintype.card {v : Fin k // v ∉ T.roots} = k-T.roots.card := by + simpa only [Fintype.card_fin,Fintype.card_coe] using + (Fintype.card_subtype_compl (fun v : Fin k => v ∈ T.roots)) + have htarget : Fintype.card {w : Fin n // w ∉ Set.range ψ} = n-T.roots.card := by + calc + _ = Fintype.card (↑((Set.range ψ)ᶜ)) := Fintype.card_congr (Equiv.refl _) + _ = n-T.roots.card := by + rw [Fintype.card_compl_set,Fintype.card_range,Fintype.card_fin,Fintype.card_coe] + rw [hfree,htarget] + +theorem rootedCount_completeGraph {k n : ℕ} (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + rootedCount T ψ (completeGraph n) = + ((n-T.roots.card).descFactorial (k-T.roots.card) : ℝ) := by + classical + unfold rootedCount copyCount + have hcontains : ∀ φ : RootedInjection T ψ, imageEdges T φ.val ⊆ completeGraph n := by + intro φ e he + exact mem_completeGraph.mpr (imageEdges_simple T φ.val he) + simp only [intact,ite_eq_left (hcontains _),Finset.sum_const,Finset.card_univ,nsmul_eq_mul, + mul_one,card_rootedInjection] + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedTemplateEdgesCard.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedTemplateEdgesCard.lean new file mode 100644 index 0000000000000000000000000000000000000000..201f1e904d283e770e40fe23d298bca3f2dec0b3 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedTemplateEdgesCard.lean @@ -0,0 +1,57 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Tracking.RootedInjectionEquivFree +import OAI.Combinatorics.TriangleRemoval.Spectral.FallingFactorialNormalizedProduct + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +lemma rootedTemplate_edges_card_le_sq {k : ℕ} (T : RootedTemplate k) : + T.edges.card ≤ k^2 := by + have hh : T.edges ⊆ completeGraph k := by + intro e he + exact mem_completeGraph.mpr (T.simple e he) + exact (Finset.card_le_card hh).trans (by rw [card_completeGraph]; exact Nat.choose_le_pow k 2) + +theorem rootedCount_initial_uniform_error (H : ℕ) {k n : ℕ} (hk : k ≤ H) + (hn : H ≤ n) (hn2 : 2 ≤ n) (T : RootedTemplate k) + (ψ : {v // v ∈ T.roots} ↪ Fin n) : + |rootedCount T ψ (completeGraph n) / + rootedScaling T n (1-1/(n : ℝ))-1| ≤ + (2 : ℝ)^(H^2) * (2*(H : ℝ)^2)/(n : ℝ) := by + have hr : T.roots.card ≤ k := by simpa using Finset.card_le_univ T.roots + have hs : T.roots.card+(k-T.roots.card) = k := Nat.add_sub_of_le hr + have hb : T.edges.card ≤ H^2 := (rootedTemplate_edges_card_le_sq T).trans + (Nat.pow_le_pow_left hk 2) + rw [rootedCount_completeGraph] + unfold rootedScaling + have hh := initialRootedScaling_error T.roots.card (k-T.roots.card) T.edges.card n + (by rw [hs]; exact hk.trans hn) hn2 + apply hh.trans + apply div_le_div_of_nonneg_right _ (Nat.cast_nonneg n) + apply mul_le_mul + · exact pow_le_pow_right₀ (by norm_num : (1 : ℝ) ≤ 2) hb + · rw [hs] + have ha : (k-T.roots.card : ℕ) ≤ H := (Nat.sub_le _ _).trans hk + have haR : ((k-T.roots.card : ℕ) : ℝ) ≤ H := by exact_mod_cast ha + have hkR : (k : ℝ) ≤ H := by exact_mod_cast hk + have hbR : (T.edges.card : ℝ) ≤ (H : ℝ)^2 := by exact_mod_cast hb + have hmul := mul_le_mul haR hkR (Nat.cast_nonneg k) (Nat.cast_nonneg H) + nlinarith only [hmul,hbR] + · positivity + · positivity + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedTemplateEncoding.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedTemplateEncoding.lean new file mode 100644 index 0000000000000000000000000000000000000000..3aa9ddabd75f0b860afc511221ae26bb00c356c5 --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/RootedTemplateEncoding.lean @@ -0,0 +1,76 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Embeddings.RootedTemplates + +namespace OAI + +section +open scoped BigOperators Topology Matrix.Norms.Operator +open MeasureTheory +open scoped BigOperators +open scoped BigOperators ENNReal Classical +open Filter MeasureTheory +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +def rootedTemplateEncoding {k : ℕ} (T : RootedTemplate k) : + Graph k × Finset (Fin k) := (T.edges,T.roots) + +lemma rootedTemplateEncoding_injective (k : ℕ) : + Function.Injective (rootedTemplateEncoding (k := k)) := by + intro T U h + cases T + cases U + cases h + rfl + +noncomputable instance rootedTemplateFintype (k : ℕ) : Fintype (RootedTemplate k) := + Fintype.ofInjective rootedTemplateEncoding (rootedTemplateEncoding_injective k) + +abbrev RootedCountIndex (H n : ℕ) := + Σ k : Fin (H+1), Σ T : RootedTemplate k.val, ({v // v ∈ T.roots} ↪ Fin n) + +noncomputable def rootedFamilySize (H : ℕ) : ℕ := + ∑ k : Fin (H+1), Fintype.card (RootedTemplate k.val) + +lemma rootInjection_card_le {k n : ℕ} (T : RootedTemplate k) : + Fintype.card ({v // v ∈ T.roots} ↪ Fin n) ≤ n^T.roots.card := by + have hh := Fintype.card_le_of_injective + (fun f : {v // v ∈ T.roots} ↪ Fin n => (f : {v // v ∈ T.roots} → Fin n)) + (fun _ _ h => DFunLike.coe_injective h) + simpa only [Fintype.card_fun,Fintype.card_fin,Fintype.card_coe] using hh + +theorem rootedCountIndex_card_le (H n : ℕ) : + Fintype.card (RootedCountIndex H n) ≤ rootedFamilySize H * (n+1)^H := by + classical + unfold RootedCountIndex + simp only [Fintype.card_sigma] + calc + _ ≤ ∑ k : Fin (H+1), ∑ T : RootedTemplate k.val, (n+1)^H := by + apply Finset.sum_le_sum + intro k _ + apply Finset.sum_le_sum + intro T _ + apply (rootInjection_card_le T).trans + have hr : T.roots.card ≤ k.val := by + simpa using (Finset.card_le_univ T.roots) + calc + n^T.roots.card ≤ (n+1)^T.roots.card := Nat.pow_le_pow_left (Nat.le_succ n) _ + _ ≤ (n+1)^H := Nat.pow_le_pow_right (by omega) (by omega) + _ = _ := by + simp only [Finset.sum_const, Finset.card_univ, nsmul_eq_mul] + rw [← Finset.sum_mul] + rfl + +def rootedIndexRequired {H n : ℕ} (i : RootedCountIndex H n) : + RootedInjection i.2.1 i.2.2 → Graph n := fun φ => imageEdges i.2.1 φ.val + +@[simp] lemma rootedIndex_copyCount {H n : ℕ} (i : RootedCountIndex H n) (G : Graph n) : + copyCount (rootedIndexRequired i) G = rootedCount i.2.1 i.2.2 G := rfl + +end SharpTerminalLeave + +end + +end OAI diff --git a/lean/OAI/Combinatorics/TriangleRemoval/Tracking/TriangleDrift.lean b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/TriangleDrift.lean new file mode 100644 index 0000000000000000000000000000000000000000..0b98917586a548d8fed471ea88a8e93219a4a52a --- /dev/null +++ b/lean/OAI/Combinatorics/TriangleRemoval/Tracking/TriangleDrift.lean @@ -0,0 +1,256 @@ +import Mathlib +import OAI.Combinatorics.TriangleRemoval.Process.TerminalLaw +import OAI.Combinatorics.TriangleRemoval.Probability.FiniteMean + +namespace OAI + +section +section +open Filter +open scoped BigOperators Topology + +namespace SharpTerminalLeave + +def hittingTriangles {n : ℕ} (G F : Graph n) : Finset (Finset (Fin n)) := + (triangles G).filter (fun t => ¬ Disjoint F (t.powersetCard 2)) + +def incidentTriangles {n : ℕ} (G : Graph n) (e : Finset (Fin n)) : + Finset (Finset (Fin n)) := + (triangles G).filter (fun t => e ∈ t.powersetCard 2) + +def triangleDegree {n : ℕ} (G : Graph n) (e : Finset (Fin n)) : ℕ := + (incidentTriangles G e).card + +noncomputable def intact {n : ℕ} (F H : Graph n) : ℝ := + if F ⊆ H then 1 else 0 + +theorem pmfMean_sum {α β : Type*} [Fintype α] [Fintype β] + (p : PMF α) (f : β → α → ℝ) : + pmfMean p (fun a => ∑ b, f b a) = ∑ b, pmfMean p (f b) := by + simp only [pmfMean, Finset.mul_sum] + exact Finset.sum_comm + +theorem step_mean {n : ℕ} (G : Graph n) (f : Graph n → ℝ) + (hG : (triangles G).Nonempty) : + pmfMean (step G) f = + (∑ t ∈ triangles G, f (G \ t.powersetCard 2)) / ((triangles G).card : ℝ) := by + rw [step, dite_eq_left hG, pmfMean_map, pmfMean_uniformOfFinset] + +theorem intact_delete {n : ℕ} {G F : Graph n} (hF : F ⊆ G) + (t : Finset (Fin n)) : + intact F (G \ t.powersetCard 2) = + 1 - if ¬ Disjoint F (t.powersetCard 2) then 1 else 0 := by + classical + simp only [intact, Finset.subset_sdiff, hF, true_and] + split_ifs <;> simp_all + +theorem step_mean_intact {n : ℕ} {G F : Graph n} (hF : F ⊆ G) + (hG : (triangles G).Nonempty) : + pmfMean (step G) (intact F) = + 1 - ((hittingTriangles G F).card : ℝ) / ((triangles G).card : ℝ) := by + classical + rw [step_mean G (intact F) hG] + simp_rw [intact_delete hF] + rw [Finset.sum_sub_distrib] + have hb : (∑ t ∈ triangles G, if ¬ Disjoint F (t.powersetCard 2) then (1 : ℝ) else 0) = + ((hittingTriangles G F).card : ℝ) := by + rw [← Finset.sum_filter] + simp [hittingTriangles] + rw [hb] + simp only [Finset.sum_const, nsmul_eq_mul, mul_one] + rw [sub_div, div_self (by exact_mod_cast hG.card_pos.ne')] + +theorem step_mean_intact_of_not_subset {n : ℕ} {G F : Graph n} + (hF : ¬ F ⊆ G) : pmfMean (step G) (intact F) = 0 := by + rw [← pmfMean_const (step G) 0] + apply pmfMean_congr + intro H hH + have hFH : ¬ F ⊆ H := fun h => hF (h.trans (step_support_subset hH)) + simp [intact, hFH] + +noncomputable def copyCount {n : ℕ} {α : Type*} [Fintype α] + (required : α → Graph n) (G : Graph n) : ℝ := + ∑ a, intact (required a) G + +theorem step_mean_copyCount {n : ℕ} {α : Type*} [Fintype α] + (required : α → Graph n) (G : Graph n) (hG : (triangles G).Nonempty) : + pmfMean (step G) (copyCount required) = copyCount required G - + (∑ a, if required a ⊆ G then ((hittingTriangles G (required a)).card : ℝ) else 0) / + ((triangles G).card : ℝ) := by + classical + unfold copyCount + rw [pmfMean_sum, Finset.sum_div, ← Finset.sum_sub_distrib] + apply Finset.sum_congr rfl + intro a _ + by_cases ha : required a ⊆ G + · rw [step_mean_intact ha hG, ite_eq_left ha, intact, ite_eq_left ha] + · rw [step_mean_intact_of_not_subset ha, ite_eq_right ha, intact, ite_eq_right ha] + simp + +theorem three_le_card_union {α : Type*} [DecidableEq α] + {e f : Finset α} (he : e.card = 2) (hf : f.card = 2) (hef : e ≠ f) : + 3 ≤ (e ∪ f).card := by + have hi : (e ∩ f).card < 2 := by + by_contra h + have hge : 2 ≤ (e ∩ f).card := by omega + have heq : e ∩ f = e := Finset.eq_of_subset_of_card_le + Finset.inter_subset_left (by omega) + have hfq : e ∩ f = f := Finset.eq_of_subset_of_card_le + Finset.inter_subset_right (by omega) + exact hef (heq.symm.trans hfq) + have h := Finset.card_union_add_card_inter e f + omega + +theorem triangle_eq_of_two_edges {α : Type*} [DecidableEq α] + {t u e f : Finset α} (ht : t.card = 3) (hu : u.card = 3) + (he : e.card = 2) (hf : f.card = 2) (hef : e ≠ f) + (het : e ⊆ t) (hft : f ⊆ t) (heu : e ⊆ u) (hfu : f ⊆ u) : t = u := by + have hm := three_le_card_union he hf hef + have ht' : e ∪ f = t := Finset.eq_of_subset_of_card_le + (Finset.union_subset het hft) (by omega) + have hu' : e ∪ f = u := Finset.eq_of_subset_of_card_le + (Finset.union_subset heu hfu) (by omega) + exact ht'.symm.trans hu' + +theorem sum_card_le_card_biUnion_add_pair_overlaps {α β : Type*} + [DecidableEq α] [DecidableEq β] (s : Finset α) (F : α → Finset β) : + (∑ a ∈ s, (F a).card) ≤ (s.biUnion F).card + + ∑ a ∈ s, ∑ b ∈ s.erase a, ((F a) ∩ F b).card := by + classical + induction s using Finset.induction_on with + | empty => simp + | @insert a s ha ih => + + rw [Finset.sum_insert ha, Finset.biUnion_insert] + have hi : ((F a) ∩ s.biUnion F).card ≤ ∑ b ∈ s, ((F a) ∩ F b).card := by + rw [Finset.inter_biUnion] + exact Finset.card_biUnion_le + have hcard := Finset.card_union_add_card_inter (F a) (s.biUnion F) + have hsum : (∑ x ∈ s, ∑ b ∈ s.erase x, ((F x) ∩ F b).card) ≤ + ∑ x ∈ s, ∑ b ∈ (insert a s).erase x, ((F x) ∩ F b).card := by + apply Finset.sum_le_sum + intro x hx + apply Finset.sum_le_sum_of_subset_of_nonneg + · exact Finset.erase_subset_erase x (Finset.subset_insert a s) + · intro i _ _; exact Nat.zero_le _ + rw [Finset.sum_insert ha, Finset.erase_insert ha] + omega + +theorem hittingTriangles_eq_biUnion {n : ℕ} (G F : Graph n) : + hittingTriangles G F = F.biUnion (incidentTriangles G) := by + classical + ext t + simp only [hittingTriangles, Finset.mem_filter, Finset.mem_biUnion, + incidentTriangles, Finset.not_disjoint_iff] + constructor + · rintro ⟨ht, e, heF, het⟩ + exact ⟨e, heF, ht, het⟩ + · rintro ⟨e, heF, ht, het⟩ + exact ⟨ht, e, heF, het⟩ + +theorem incident_pair_card_le_one {n : ℕ} (G : Graph n) + {e f : Finset (Fin n)} (hef : e ≠ f) : + ((incidentTriangles G e) ∩ incidentTriangles G f).card ≤ 1 := by + apply Finset.card_le_one.mpr + intro t ht u hu + simp only [incidentTriangles, Finset.mem_inter, Finset.mem_filter, + Finset.mem_powersetCard] at ht hu + exact triangle_eq_of_two_edges (mem_triangles.mp ht.1.1).1 + (mem_triangles.mp hu.1.1).1 ht.1.2.2 ht.2.2.2 hef + ht.1.2.1 ht.2.2.1 hu.1.2.1 hu.2.2.1 + +theorem hittingTriangles_card_le_degree_sum {n : ℕ} (G F : Graph n) : + (hittingTriangles G F).card ≤ ∑ e ∈ F, triangleDegree G e := by + rw [hittingTriangles_eq_biUnion] + exact Finset.card_biUnion_le + +theorem degree_sum_le_hittingTriangles_add {n : ℕ} (G F : Graph n) : + (∑ e ∈ F, triangleDegree G e) ≤ (hittingTriangles G F).card + F.card ^ 2 := by + rw [hittingTriangles_eq_biUnion] + refine (sum_card_le_card_biUnion_add_pair_overlaps F (incidentTriangles G)).trans ?_ + apply Nat.add_le_add_left + calc + (∑ e ∈ F, ∑ f ∈ F.erase e, ((incidentTriangles G e) ∩ incidentTriangles G f).card) + ≤ ∑ _e ∈ F, F.card := by + apply Finset.sum_le_sum + intro e _ + calc + (∑ f ∈ F.erase e, ((incidentTriangles G e) ∩ incidentTriangles G f).card) + ≤ ∑ _f ∈ F.erase e, 1 := by + apply Finset.sum_le_sum + intro f hf + exact incident_pair_card_le_one G (Finset.ne_of_mem_erase hf).symm + _ = (F.erase e).card := by simp + _ ≤ F.card := Finset.card_le_card (Finset.erase_subset e F) + _ = F.card ^ 2 := by simp [pow_two] + +theorem hittingTriangles_degree_error {n : ℕ} (G F : Graph n) (D δ : ℝ) + (h : ∀ e ∈ F, |(triangleDegree G e : ℝ) - D| ≤ δ) : + |((hittingTriangles G F).card : ℝ) - (F.card : ℝ) * D| ≤ + (F.card : ℝ) * δ + (F.card : ℝ) ^ 2 := by + have hsumlo : (F.card : ℝ) * (D - δ) ≤ ∑ e ∈ F, (triangleDegree G e : ℝ) := by + calc + _ = ∑ _e ∈ F, (D - δ) := by simp [mul_sub] + _ ≤ _ := Finset.sum_le_sum fun e he => by + have hh := (abs_le.mp (h e he)).1 + linarith + have hsumhi : (∑ e ∈ F, (triangleDegree G e : ℝ)) ≤ (F.card : ℝ) * (D + δ) := by + calc + _ ≤ ∑ _e ∈ F, (D + δ) := Finset.sum_le_sum fun e he => by + have hh := (abs_le.mp (h e he)).2 + linarith + _ = _ := by simp [mul_add] + have hhitlo : (∑ e ∈ F, (triangleDegree G e : ℝ)) ≤ + ((hittingTriangles G F).card : ℝ) + (F.card : ℝ) ^ 2 := by + exact_mod_cast degree_sum_le_hittingTriangles_add G F + have hhithi : ((hittingTriangles G F).card : ℝ) ≤ + ∑ e ∈ F, (triangleDegree G e : ℝ) := by + exact_mod_cast hittingTriangles_card_le_degree_sum G F + apply abs_le.mpr + constructor <;> nlinarith [sq_nonneg (F.card : ℝ)] + +theorem copyCount_drift_error {n : ℕ} {α : Type*} [Fintype α] + (required : α → Graph n) (G : Graph n) (b : ℕ) (D δ : ℝ) + (hsize : ∀ a, (required a).card = b) + (hdeg : ∀ e ∈ G, |(triangleDegree G e : ℝ) - D| ≤ δ) + (hG : (triangles G).Nonempty) : + |pmfMean (step G) (copyCount required) - copyCount required G + + ((b : ℝ) * D / (triangles G).card) * copyCount required G| ≤ + (((b : ℝ) * δ + (b : ℝ) ^ 2) / (triangles G).card) * copyCount required G := by + classical + let loss (a : α) : ℝ := + if required a ⊆ G then ((hittingTriangles G (required a)).card : ℝ) else 0 + have ha (a : α) : |loss a - (b : ℝ) * D * intact (required a) G| ≤ + ((b : ℝ) * δ + (b : ℝ) ^ 2) * intact (required a) G := by + by_cases hpres : required a ⊆ G + · have hh := hittingTriangles_degree_error G (required a) D δ + (fun e he => hdeg e (hpres he)) + simpa only [loss, hpres, ite_eq_left, intact, hsize, mul_one] using hh + · simp [loss, intact, hpres] + have hs : |∑ a, (loss a - (b : ℝ) * D * intact (required a) G)| ≤ + ((b : ℝ) * δ + (b : ℝ) ^ 2) * copyCount required G := by + calc + _ ≤ ∑ a, |loss a - (b : ℝ) * D * intact (required a) G| := + Finset.abs_sum_le_sum_abs _ _ + _ ≤ ∑ a, ((b : ℝ) * δ + (b : ℝ) ^ 2) * intact (required a) G := + Finset.sum_le_sum fun a _ => ha a + _ = _ := by rw [← Finset.mul_sum]; rfl + have hq : (0 : ℝ) < (triangles G).card := by exact_mod_cast hG.card_pos + have hid : pmfMean (step G) (copyCount required) - copyCount required G + + ((b : ℝ) * D / (triangles G).card) * copyCount required G = + -(∑ a, (loss a - (b : ℝ) * D * intact (required a) G)) / (triangles G).card := by + rw [step_mean_copyCount required G hG, Finset.sum_sub_distrib, ← Finset.mul_sum] + change copyCount required G - (∑ a, loss a) / _ - copyCount required G + + (_ / _) * copyCount required G = -((∑ a, loss a) - (b : ℝ) * D * copyCount required G) / _ + ring + rw [hid, abs_div, abs_neg, abs_of_pos hq] + calc + _ ≤ (((b : ℝ) * δ + (b : ℝ) ^ 2) * copyCount required G) / (triangles G).card := + div_le_div_of_nonneg_right hs hq.le + _ = _ := by ring + +end SharpTerminalLeave +end +end + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Amplification.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Amplification.lean new file mode 100644 index 0000000000000000000000000000000000000000..d877dea09da637947b65e6927e1c25d72e209545 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Amplification.lean @@ -0,0 +1,289 @@ +import OAI.Combinatorics.TwoWayAutomata.RelationMonoid + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- The exponential lower bound for `2 ≤ h`. -/ +def lossBound (h : ℕ) : ℕ := 2 ^ ((h - 2) / 127) + +/-- The image of a monoid lies in the diagram corner of its image identity. -/ +theorem map_identity_corner {A : Type*} [Monoid A] {m : ℕ} + (ι : A →ₙ* Diagram m) (a : A) : ι a ∈ (ι 1).corner := by + constructor + · change ι 1 * ι a = ι a + rw [← map_mul, one_mul] + · change ι a * ι 1 = ι a + rw [← map_mul, mul_one] + +/-- The cardinality consequence of the uniform transport theorem. -/ +theorem missing_context_card {m : ℕ} (e d u v z : Diagram m) + (he : Diagram.comp e e = e) (hd : Diagram.comp d d = d) + (hcontext : Diagram.comp (Diagram.comp u e) v = d) (hz : z ∈ e.corner) + (hnew : Diagram.comp (Diagram.comp u z) v ∈ d.corner) : + (d.missing (Diagram.comp (Diagram.comp u z) v)).ncard ≤ 2 * (e.missing z).ncard := by + obtain ⟨J', _, hcard, hcover⟩ := Diagram.transport e d u v he hd hcontext + (e.missing z) (fun _ h => h.1) + exact (Set.ncard_le_ncard (hcover z hz Set.Subset.rfl hnew)).trans hcard + +/-- Conjugating by a unit costs at most twice as many missing classes. -/ +theorem conjugate_missing_bound {A : Type*} [Monoid A] {m : ℕ} + (ι : A →ₙ* Diagram m) (p : Aˣ) (a : A) : + ((ι 1).missing (ι (p * a * (↑p⁻¹ : A)))).ncard ≤ + 2 * ((ι 1).missing (ι a)).ncard := by + have he : Diagram.comp (ι 1) (ι 1) = ι 1 := (map_identity_corner ι 1).1 + have hcontext : Diagram.comp (Diagram.comp (ι p) (ι 1)) (ι (↑p⁻¹ : A)) = ι 1 := by + change ι p * ι 1 * ι (↑p⁻¹ : A) = ι 1 + rw [← map_mul, ← map_mul] + simp + have hnew : Diagram.comp (Diagram.comp (ι p) (ι a)) (ι (↑p⁻¹ : A)) ∈ (ι 1).corner := by + change ι p * ι a * ι (↑p⁻¹ : A) ∈ (ι 1).corner + rw [← map_mul, ← map_mul] + exact map_identity_corner ι _ + have h := missing_context_card (ι 1) (ι 1) (ι p) (ι (↑p⁻¹ : A)) (ι a) + he he hcontext (map_identity_corner ι a) hnew + change ((ι 1).missing (ι p * ι a * ι (↑p⁻¹ : A))).ncard ≤ _ at h + simpa only [map_mul] using h + +/-- All pair products share one missing-set bound after sandwiching by an idempotent. -/ +theorem common_pair_budget {A : Type*} [Monoid A] {m t c : ℕ} + (ι : A →ₙ* Diagram m) (f : Bool × Fin t → A) (b0 : A) (hb0 : IsIdempotentElem b0) + (hcost : ∀ p, ((ι 1).missing (ι (f p))).ncard ≤ 2 * c) : + ∃ J0 ⊆ (ι b0).classes, J0.ncard ≤ 8 * t * c ∧ + ∀ i j, (ι b0).missing (ι (b0 * f (false, i) * f (true, j) * b0)) ⊆ J0 := by + classical + let J := ⋃ p, (ι 1).missing (ι (f p)) + have hJ : J ⊆ (ι 1).classes := by + intro C hC + obtain ⟨p, hp⟩ := Set.mem_iUnion.mp hC + exact hp.1 + have hcard : J.ncard ≤ 4 * t * c := by + calc + J.ncard ≤ ∑ p : Bool × Fin t, ((ι 1).missing (ι (f p))).ncard := + Set.ncard_iUnion_le_of_fintype _ + _ ≤ ∑ _ : Bool × Fin t, 2 * c := Finset.sum_le_sum (fun p _ => hcost p) + _ = 4 * t * c := by simp; ring + have he : Diagram.comp (ι 1) (ι 1) = ι 1 := (map_identity_corner ι 1).1 + have hd : Diagram.comp (ι b0) (ι b0) = ι b0 := + (map_mul ι b0 b0).symm.trans (congrArg ι hb0.eq) + have hcontext : Diagram.comp (Diagram.comp (ι b0) (ι 1)) (ι b0) = ι b0 := by + change ι b0 * ι 1 * ι b0 = ι b0 + rw [← map_mul, ← map_mul, mul_one, hb0.eq] + obtain ⟨J0, hJ0, hcard0, hcover⟩ := Diagram.transport (ι 1) (ι b0) (ι b0) (ι b0) + he hd hcontext J hJ + refine ⟨J0, hJ0, by nlinarith, ?_⟩ + intro i j + have hproduct : (ι 1).missing (ι (f (false, i) * f (true, j))) ⊆ J := by + rw [map_mul] + exact (Diagram.missing_comp (ι 1) _ _ he (map_identity_corner ι _) + (map_identity_corner ι _)).trans (Set.union_subset + (Set.subset_iUnion (fun p => (ι 1).missing (ι (f p))) (false, i)) + (Set.subset_iUnion (fun p => (ι 1).missing (ι (f p))) (true, j))) + have hnew : Diagram.comp (Diagram.comp (ι b0) (ι (f (false, i) * f (true, j)))) + (ι b0) ∈ (ι b0).corner := by + constructor + · rw [← Diagram.comp_assoc, ← Diagram.comp_assoc, hd] + · rw [Diagram.comp_assoc, hd] + have h := hcover _ (map_identity_corner ι _) hproduct hnew + change (ι b0).missing (ι b0 * ι (f (false, i) * f (true, j)) * ι b0) ⊆ J0 at h + simpa only [map_mul, mul_assoc] using h + +/-- Disjoint source and target sets prevent two added edges from being concatenated. -/ +theorem bipartite_relation_comp {H : Type*} (F U V : Set H) + (hU : U ⊆ F) (hV : V ⊆ F) (hUV : Disjoint U V) + (E E' : SetRel H H) (hE : E ⊆ U ×ˢ V) (hE' : E' ⊆ U ×ˢ V) : + (restrictedIdentity F ∪ E) ○ (restrictedIdentity F ∪ E') = + restrictedIdentity F ∪ (E ∪ E') := by + ext ⟨x, y⟩ + constructor + · rintro ⟨z, hxz | hxz, hzy | hzy⟩ + · exact Or.inl ⟨hxz.1.trans hzy.1, hxz.2⟩ + · exact Or.inr (Or.inr (hxz.1 ▸ hzy)) + · exact Or.inr (Or.inl (hzy.1 ▸ hxz)) + · exact (Set.disjoint_left.mp hUV (hE' hzy).1 (hE hxz).2).elim + · rintro (hxy | hxy | hxy) + · exact ⟨x, Or.inl ⟨rfl, hxy.2⟩, Or.inl hxy⟩ + · exact ⟨y, Or.inr hxy, Or.inl ⟨rfl, hV (hE hxy).2⟩⟩ + · exact ⟨x, Or.inl ⟨rfl, hU (hE' hxy).1⟩, Or.inr hxy⟩ + +/-- Removing the hub leaves exactly the desired pair between its two neighbours. -/ +theorem hub_pair_relation {H : Type*} (u v z : H) (huz : u ≠ z) (hvz : v ≠ z) : + restrictedIdentity {x | x ≠ z} ○ (SetRel.id ∪ {(u, z)}) ○ + (SetRel.id ∪ {(z, v)}) ○ restrictedIdentity {x | x ≠ z} = + restrictedIdentity {x | x ≠ z} ∪ {(u, v)} := by + ext ⟨a, b⟩ + simp only [SetRel.comp, Set.mem_ofPred_eq, Set.mem_union, Set.mem_singleton_iff, + SetRel.id, restrictedIdentity, Prod.mk.injEq] + aesop + +/-- A permutation and its inverse form a unit in the full relation monoid. -/ +def permutationUnit {H : Type*} (σ : Equiv.Perm H) : (RelationMonoid H)ˣ where + val := .ofRel (Function.graph σ) + inv := .ofRel (Function.graph σ.symm) + val_inv := by + apply RelationMonoid.ext + change Function.graph σ ○ Function.graph σ.symm = SetRel.id + ext ⟨a, b⟩ + simp [SetRel.comp, Function.graph, SetRel.id] + inv_val := by + apply RelationMonoid.ext + change Function.graph σ.symm ○ Function.graph σ = SetRel.id + ext ⟨a, b⟩ + simp [SetRel.comp, Function.graph, SetRel.id] + +/-- Permutation conjugation relabels both endpoints of every relation edge. -/ +theorem permutation_conjugate_relation {H : Type*} (σ : Equiv.Perm H) (R : SetRel H H) : + Function.graph σ ○ R ○ Function.graph σ.symm = {xy | (σ xy.1, σ xy.2) ∈ R} := by + ext ⟨a, b⟩ + constructor + · rintro ⟨c, ⟨d, had, hdc⟩, hcb⟩ + change σ a = d at had + change σ.symm c = b at hcb + have hc : c = σ b := by rw [← hcb, σ.apply_symm_apply] + rwa [← had, hc] at hdc + · intro hab + exact ⟨σ b, ⟨σ a, rfl, hab⟩, σ.symm_apply_apply b⟩ + +/-- Unit lifts move the initial added pair to any other ordered pair of distinct points. -/ +theorem pair_conjugate {A H : Type*} [Monoid A] {m : ℕ} + (ι : A →ₙ* Diagram m) (φ : A →* RelationMonoid H) + (hlift : ∀ σ : Equiv.Perm H, ∃ p : Aˣ, φ p = (permutationUnit σ).val) + (a : A) (ha : IsIdempotentElem a) (x y : H) (hxy : x ≠ y) + (himage : (φ a).toRel = SetRel.id ∪ {(x, y)}) (u v : H) (huv : u ≠ v) : + ∃ s : A, IsIdempotentElem s ∧ (φ s).toRel = SetRel.id ∪ {(u, v)} ∧ + ((ι 1).missing (ι s)).ncard ≤ 2 * ((ι 1).missing (ι a)).ncard := by + have hinj {r s : H} (hrs : r ≠ s) : Function.Injective (fun b : Bool => if b then s else r) := by + intro b c h + cases b <;> cases c <;> simp_all + obtain ⟨σ, hσ⟩ := Equiv.Perm.exists_extending_pair + (fun b : Bool => if b then v else u) (fun b : Bool => if b then y else x) + (hinj huv) (hinj hxy) + have hσu : σ u = x := by simpa using hσ false + have hσv : σ v = y := by simpa using hσ true + obtain ⟨p, hp⟩ := hlift σ + have hunit : Units.map φ p = permutationUnit σ := Units.ext hp + have hinv : φ (↑p⁻¹ : A) = (↑(permutationUnit σ)⁻¹ : RelationMonoid H) := + congrArg (fun q : (RelationMonoid H)ˣ => (↑q⁻¹ : RelationMonoid H)) hunit + refine ⟨p * a * (↑p⁻¹ : A), ?_, ?_, conjugate_missing_bound ι p a⟩ + · change (↑p * a * ↑p⁻¹) * (↑p * a * ↑p⁻¹) = ↑p * a * ↑p⁻¹ + simp only [mul_assoc, Units.inv_mul_cancel_left, ← mul_assoc a a, ha.eq] + · rw [map_mul, map_mul, hp, hinv, RelationMonoid.toRel_mul, RelationMonoid.toRel_mul, himage] + change Function.graph σ ○ (SetRel.id ∪ {(x, y)}) ○ Function.graph σ.symm = _ + rw [permutation_conjugate_relation, ← hσu, ← hσv] + ext ⟨r, s⟩ + simp [SetRel.id, σ.injective.eq_iff] + +/-- Restricting an idempotent sandwich preserves the new pair and supplies all unit lifts. -/ +theorem smaller_relation_instance {A H : Type*} [Monoid A] [Finite A] {m : ℕ} + (ι : A →ₙ* Diagram m) (φ : A →* RelationMonoid H) (hφ : Function.Surjective φ) + (b b' : A) (hb : IsIdempotentElem b) (hbb' : b * b' = b') (hb'b : b' * b = b') + (F : Set H) + (hiPi : restrictedIdentity F ○ (φ b).toRel ○ restrictedIdentity F = restrictedIdentity F) + (x y : F) + (hpair : restrictedIdentity F ○ (φ b').toRel ○ restrictedIdentity F = + restrictedIdentity F ∪ {(x.val, y.val)}) : + ∃ d : hb.Corner, ∃ hd : IsIdempotentElem d, ∃ ψ : hd.Corner →* RelationMonoid F, + Function.Surjective ψ ∧ + (∀ σ : Equiv.Perm F, ∃ p : hd.Cornerˣ, ψ p = (permutationUnit σ).val) ∧ + (∀ w, (ψ w).toRel = {xy : F × F | (xy.1.val, xy.2.val) ∈ (φ w.val.val).toRel}) ∧ + ∃ v : hd.Corner, IsIdempotentElem v ∧ (ψ v).toRel = SetRel.id ∪ {(x, y)} ∧ + ((ι d.val).missing (ι v.val.val)).ncard ≤ 2 * ((ι b).missing (ι b')).ncard := by + let P := (φ b).toRel + let i := restrictedIdentity F + let Q := P ○ i ○ P + have hP : IsIdempotentElem (φ b) := by rw [IsIdempotentElem, ← map_mul, hb.eq] + have hPP : P ○ P = P := congrArg RelationMonoid.toRel hP.eq + obtain ⟨hQ, ρ, hρ⟩ := relation_corner_equiv P F hPP hiPi + have hident := relation_corner_identities P i hPP hiPi + let q : hP.Corner := ⟨.ofRel Q, (Subsemigroup.mem_corner_iff hP).mpr + ⟨RelationMonoid.ext hident.2.1, RelationMonoid.ext hident.2.2.1⟩⟩ + have hq : IsIdempotentElem q := Subtype.ext hQ.eq + let θ := cornerMap φ.toMulHom hb hP rfl + have hθ : Function.Surjective θ := cornerMap_surjective φ.toMulHom hφ hb hP rfl + obtain ⟨d, hd, hθd, hminimal⟩ := minimal_idempotent_lift θ.toMulHom hθ q hq + let γ := cornerMap θ.toMulHom hd hq hθd + let equiv := (cornerFlatten hP q hq).trans ρ + let ψ := equiv.toMonoidHom.comp γ + have hψ : Function.Surjective ψ := + equiv.surjective.comp (cornerMap_surjective θ.toMulHom hθ hd hq hθd) + have hψrel (w : hd.Corner) : + (ψ w).toRel = {xy : F × F | (xy.1.val, xy.2.val) ∈ (φ w.val.val).toRel} := + hρ _ + have hψunits (σ : Equiv.Perm F) : ∃ p : hd.Cornerˣ, ψ p = (permutationUnit σ).val := by + obtain ⟨w, hw⟩ := hψ (permutationUnit σ).val + have hunitψ : IsUnit (ψ w) := hw.symm ▸ (permutationUnit σ).isUnit + have hunitγ : IsUnit (γ w) := by + have h := hunitψ.map equiv.symm.toMonoidHom + simpa only [ψ, MonoidHom.comp_apply, MulEquiv.coe_toMonoidHom, + MulEquiv.symm_apply_apply] using h + obtain ⟨p, hp⟩ := minimal_corner_reflects_units θ.toMulHom hd hq hθd hminimal w hunitγ + exact ⟨p, hp ▸ hw⟩ + have hdimage : φ d.val = RelationMonoid.ofRel Q := congrArg Subtype.val hθd + let b'c : hb.Corner := ⟨b', (Subsemigroup.mem_corner_iff hb).mpr ⟨hbb', hb'b⟩⟩ + let w : hd.Corner := ⟨d * b'c * d, b'c, rfl⟩ + have hwimage : (ψ w).toRel = SetRel.id ∪ {(x, y)} := by + rw [hψrel] + change {xy : F × F | (xy.1.val, xy.2.val) ∈ (φ (d.val * b' * d.val)).toRel} = _ + rw [map_mul, map_mul, hdimage, RelationMonoid.toRel_mul, RelationMonoid.toRel_mul, + RelationMonoid.toRel_ofRel] + have habsorb : P ○ (φ b').toRel ○ P = (φ b').toRel := by + change ((φ b * φ b') * φ b).toRel = _ + rw [← map_mul, ← map_mul, hbb', hb'b] + have hsandwich : i ○ (Q ○ (φ b').toRel ○ Q) ○ i = i ∪ {(x.val, y.val)} := by + calc + i ○ (Q ○ (φ b').toRel ○ Q) ○ i = + (i ○ Q) ○ (φ b').toRel ○ (Q ○ i) := by simp only [SetRel.comp_assoc] + _ = i ○ (P ○ (φ b').toRel ○ P) ○ i := by + rw [hident.2.2.2.1, hident.2.2.2.2.1] + simp only [SetRel.comp_assoc] + _ = i ∪ {(x.val, y.val)} := by rw [habsorb]; exact hpair + ext ⟨r, s⟩ + have hmem : (r.val, s.val) ∈ i ○ (Q ○ (φ b').toRel ○ Q) ○ i ↔ + (r.val, s.val) ∈ Q ○ (φ b').toRel ○ Q := by + simp [i, SetRel.comp, restrictedIdentity, r.property, s.property] + change (r.val, s.val) ∈ Q ○ (φ b').toRel ○ Q ↔ (r, s) ∈ SetRel.id ∪ {(x, y)} + rw [← hmem, hsandwich] + simp [i, restrictedIdentity, SetRel.id, Subtype.ext_iff] + have hiPair : IsIdempotentElem (ψ w) := by + apply RelationMonoid.ext + rw [RelationMonoid.toRel_mul, hwimage] + ext ⟨r, s⟩ + simp only [SetRel.comp, Set.mem_ofPred_eq, Set.mem_union, Set.mem_singleton_iff, + SetRel.id, Prod.mk.injEq] + aesop + obtain ⟨k, hk, hv⟩ := finite_idempotent_pow w + obtain ⟨k, rfl⟩ := Nat.exists_eq_succ_of_ne_zero hk.ne' + refine ⟨d, hd, ψ, hψ, hψunits, hψrel, w ^ (k + 1), hv, ?_, ?_⟩ + · rw [map_pow, hiPair.pow_succ_eq, hwimage] + · let ιb : hb.Corner →ₙ* Diagram m := ι.comp (MulMemClass.subtype _) + let ιd : hd.Corner →ₙ* Diagram m := ιb.comp (MulMemClass.subtype _) + have hid : Diagram.comp (ι d.val) (ι d.val) = ι d.val := + (map_mul ιb d d).symm.trans (congrArg ιb hd.eq) + have hib : Diagram.comp (ι b) (ι b) = ι b := + (map_mul ι b b).symm.trans (congrArg ι hb.eq) + obtain ⟨hbd, hdb⟩ := (Subsemigroup.mem_corner_iff hb).mp d.property + have hcontext : Diagram.comp (Diagram.comp (ι d.val) (ι b)) (ι d.val) = ι d.val := by + change ι d.val * ι b * ι d.val = ι d.val + rw [← map_mul, hdb] + exact hid + have hb' : ι b' ∈ (ι b).corner := + ⟨(map_mul ι b b').symm.trans (congrArg ι hbb'), + (map_mul ι b' b).symm.trans (congrArg ι hb'b)⟩ + have hw : ιd w ∈ (ι d.val).corner := map_identity_corner ιd w + have htransport := missing_context_card (ι b) (ι d.val) (ι d.val) (ι d.val) (ι b') + hib hid hcontext hb' (by + change ι d.val * ι b' * ι d.val ∈ (ι d.val).corner + rw [← map_mul, ← map_mul] + exact hw) + have hpow := Diagram.missing_pow_succ (ι d.val) (ιd w) hid hw k + rw [← map_positive_pow] at hpow + apply (Set.ncard_le_ncard hpow).trans + change ((ι d.val).missing (ι (d.val * b' * d.val))).ncard ≤ _ + rw [map_mul, map_mul] + exact htransport + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Chain.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Chain.lean new file mode 100644 index 0000000000000000000000000000000000000000..e2a70f5879f4588359617d9e3b040e900d3e8d7f --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Chain.lean @@ -0,0 +1,228 @@ +import OAI.Combinatorics.TwoWayAutomata.Network + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Internal vertices of a chain with one distinguished final cell. -/ +def ChainNode (m : ℕ) : ℕ → Type + | 0 => Fin m + | n + 1 => Fin m ⊕ ChainNode m n + +namespace ChainNode + +/-- Zero-based cell coordinate. -/ +def position {m : ℕ} : {n : ℕ} → ChainNode m n → ℕ + | 0, _ => 0 + | _ + 1, .inl _ => 0 + | _ + 1, .inr c => c.position + 1 + +/-- State label carried at a cell. -/ +def label {m : ℕ} : {n : ℕ} → ChainNode m n → Fin m + | 0, q => q + | _ + 1, .inl q => q + | _ + 1, .inr c => c.label + +/-- Every vertex belongs to one of the chain's cells. -/ +theorem position_le {m n : ℕ} (c : ChainNode m n) : c.position ≤ n := by + induction n with + | zero => exact Nat.zero_le _ + | succ n ih => + cases c with + | inl q => exact Nat.zero_le _ + | inr c => exact Nat.succ_le_succ (ih c) + +/-- A cell coordinate and a state label uniquely identify an internal vertex. -/ +theorem coordinates_injective {m n : ℕ} (c d : ChainNode m n) + (hp : c.position = d.position) (hq : c.label = d.label) : c = d := by + induction n with + | zero => exact hq + | succ n ih => + cases c with + | inl q => + cases d with + | inl r => exact congrArg Sum.inl hq + | inr d => exact (Nat.succ_ne_zero _ hp.symm).elim + | inr c => + cases d with + | inl r => exact (Nat.succ_ne_zero _ hp).elim + | inr d => exact congrArg Sum.inr (ih c d (Nat.succ.inj hp) hq) + +/-- Every valid cell coordinate and state label occurs. -/ +theorem coordinates_surjective {m n : ℕ} (q : Fin m) (p : ℕ) (hp : p ≤ n) : + ∃ c : ChainNode m n, c.label = q ∧ c.position = p := by + induction n generalizing p with + | zero => exact ⟨q, rfl, by dsimp [position]; omega⟩ + | succ n ih => + cases p with + | zero => exact ⟨Sum.inl q, rfl, rfl⟩ + | succ p => + obtain ⟨c, hq, hc⟩ := ih p (by omega) + exact ⟨Sum.inr c, hq, congrArg Nat.succ hc⟩ + +end ChainNode + +/-- The symbol at a vertex in a nonempty cell chain. -/ +def chainSymbol {Sigma : Type*} {m : ℕ} (w : List Sigma) (last : Sigma) : + ChainNode m w.length → Sigma := + match w with + | [] => fun _ => last + | a :: tail => fun c => match c with + | .inl _ => a + | .inr d => chainSymbol tail last d + +/-- A single cell retains stays internally and exposes its two moving transitions. -/ +def cellNetwork {m : ℕ} (moves : Fin 3 → SetRel (Fin m) (Fin m)) : Network m (Fin m) where + edge := moves 1 + leftIn := SetRel.id + rightIn := SetRel.id + leftOut := moves 0 + rightOut := moves 2 + +/-- Join a list of cells to a distinguished final cell. -/ +def chainNetwork {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) : + Network m (ChainNode m w.length) := + match w with + | [] => cellNetwork (moves last) + | a :: tail => (cellNetwork (moves a)).join (chainNetwork moves tail last) + +/-- The path summary of a cell chain is the product of its cell diagrams. -/ +theorem chainNetwork_summary {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) : + (chainNetwork moves w last).summary = + wordDiagram (fun a => cellDiagram m (moves a 1) (moves a 2) (moves a 0)) (w ++ [last]) := by + induction w with + | nil => + change (cellNetwork (moves last)).summary = _ + simp [Network.summary, cellNetwork, cellDiagram, wordDiagram, SetRel.id_comp] + | cons a w ih => + change ((cellNetwork (moves a)).join (chainNetwork moves w last)).summary = _ + rw [Network.summary_join, ih] + simp only [Network.summary, cellNetwork, SetRel.id_comp, cellDiagram, wordDiagram, + List.cons_append, List.map_cons, List.prod_cons] + rfl + +/-- Entrances attach exactly to the first and last cells with their unchanged labels. -/ +theorem chainNetwork_entrances {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) + (q : Fin m) (c : ChainNode m w.length) : + ((q, c) ∈ (chainNetwork moves w last).leftIn ↔ c.position = 0 ∧ q = c.label) ∧ + ((q, c) ∈ (chainNetwork moves w last).rightIn ↔ c.position = w.length ∧ q = c.label) := by + induction w with + | nil => + change (q = c ↔ 0 = 0 ∧ q = c) ∧ (q = c ↔ 0 = 0 ∧ q = c) + simp + | cons a w ih => + cases c with + | inl r => + change ((∃ v : Fin m, (Sum.inl r : Fin m ⊕ ChainNode m w.length) = Sum.inl v ∧ + q = v) ↔ 0 = 0 ∧ q = r) ∧ + ((∃ v, (Sum.inl r : Fin m ⊕ ChainNode m w.length) = Sum.inr v ∧ + (q, v) ∈ (chainNetwork moves w last).rightIn) ↔ 0 = w.length + 1 ∧ q = r) + simp [eq_comm] + | inr c => + change ((∃ v : Fin m, (Sum.inr c : Fin m ⊕ ChainNode m w.length) = Sum.inl v ∧ + q = v) ↔ c.position + 1 = 0 ∧ q = c.label) ∧ + ((∃ v, (Sum.inr c : Fin m ⊕ ChainNode m w.length) = Sum.inr v ∧ + (q, v) ∈ (chainNetwork moves w last).rightIn) ↔ + c.position + 1 = w.length + 1 ∧ q = c.label) + simpa using (ih c).2 + +/-- Only the endpoint cells can exit a chain, using the appropriate moving transition. -/ +theorem chainNetwork_exits {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) + (c : ChainNode m w.length) (q : Fin m) : + ((c, q) ∈ (chainNetwork moves w last).leftOut ↔ + c.position = 0 ∧ (c.label, q) ∈ moves (chainSymbol w last c) 0) ∧ + ((c, q) ∈ (chainNetwork moves w last).rightOut ↔ + c.position = w.length ∧ (c.label, q) ∈ moves (chainSymbol w last c) 2) := by + induction w with + | nil => + change ((c, q) ∈ moves last 0 ↔ 0 = 0 ∧ (c, q) ∈ moves last 0) ∧ + ((c, q) ∈ moves last 2 ↔ 0 = 0 ∧ (c, q) ∈ moves last 2) + simp + | cons a w ih => + cases c with + | inl r => + change ((∃ v : Fin m, (Sum.inl r : Fin m ⊕ ChainNode m w.length) = Sum.inl v ∧ + (v, q) ∈ moves a 0) ↔ 0 = 0 ∧ (r, q) ∈ moves a 0) ∧ + ((∃ v, (Sum.inl r : Fin m ⊕ ChainNode m w.length) = Sum.inr v ∧ + (v, q) ∈ (chainNetwork moves w last).rightOut) ↔ + 0 = w.length + 1 ∧ (r, q) ∈ moves a 2) + simp + | inr c => + change ((∃ v : Fin m, (Sum.inr c : Fin m ⊕ ChainNode m w.length) = Sum.inl v ∧ + (v, q) ∈ moves a 0) ↔ c.position + 1 = 0 ∧ + (c.label, q) ∈ moves (chainSymbol w last c) 0) ∧ + ((∃ v, (Sum.inr c : Fin m ⊕ ChainNode m w.length) = Sum.inr v ∧ + (v, q) ∈ (chainNetwork moves w last).rightOut) ↔ + c.position + 1 = w.length + 1 ∧ (c.label, q) ∈ moves (chainSymbol w last c) 2) + simpa using (ih c).2 + +/-- Internal edges are precisely local transitions that stay within the chain. -/ +theorem chainNetwork_edges {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) + (c d : ChainNode m w.length) : + (c, d) ∈ (chainNetwork moves w last).edge ↔ + ∃ move : Fin 3, (c.label, d.label) ∈ moves (chainSymbol w last c) move ∧ + (d.position : ℤ) = (c.position : ℤ) + (move.val : ℤ) - 1 := by + induction w with + | nil => + change (c, d) ∈ moves last 1 ↔ + ∃ move : Fin 3, (c, d) ∈ moves last move ∧ (0 : ℤ) = 0 + (move.val : ℤ) - 1 + constructor + · exact fun h => ⟨1, h, by norm_num⟩ + · rintro ⟨move, h, hp⟩ + have hm : move = 1 := Fin.ext (by norm_num at hp; omega) + simpa only [hm] using h + | cons a w ih => + cases c with + | inl q => + cases d with + | inl r => + change (q, r) ∈ moves a 1 ↔ + ∃ move : Fin 3, (q, r) ∈ moves a move ∧ (0 : ℤ) = 0 + (move.val : ℤ) - 1 + constructor + · exact fun h => ⟨1, h, by norm_num⟩ + · rintro ⟨move, h, hp⟩ + have hm : move = 1 := Fin.ext (by norm_num at hp; omega) + simpa only [hm] using h + | inr d => + change (q, d) ∈ moves a 2 ○ (chainNetwork moves w last).leftIn ↔ _ + constructor + · rintro ⟨r, hqr, hrd⟩ + obtain ⟨hp, hq⟩ := (chainNetwork_entrances moves w last r d).1.mp hrd + exact ⟨2, hq ▸ hqr, by dsimp [ChainNode.position]; rw [hp]; norm_num⟩ + · rintro ⟨move, h, hp⟩ + change ((d.position + 1 : ℕ) : ℤ) = 0 + (move.val : ℤ) - 1 at hp + have hm : move = 2 := Fin.ext (by have := move.isLt; omega) + have hd : d.position = 0 := by have := move.isLt; omega + refine ⟨d.label, ?_, (chainNetwork_entrances moves w last d.label d).1.mpr ⟨hd, rfl⟩⟩ + simpa only [ChainNode.label, chainSymbol, hm] using h + | inr c => + cases d with + | inl r => + change (c, r) ∈ (chainNetwork moves w last).leftOut ○ SetRel.id ↔ _ + rw [SetRel.comp_id] + constructor + · intro h + obtain ⟨hp, hq⟩ := (chainNetwork_exits moves w last c r).1.mp h + exact ⟨0, hq, by dsimp [ChainNode.position]; rw [hp]; norm_num⟩ + · rintro ⟨move, h, hp⟩ + change (0 : ℤ) = ((c.position + 1 : ℕ) : ℤ) + (move.val : ℤ) - 1 at hp + have hm : move = 0 := Fin.ext (by omega) + have hc : c.position = 0 := by omega + refine (chainNetwork_exits moves w last c r).1.mpr ⟨hc, ?_⟩ + simpa only [ChainNode.label, chainSymbol, hm] using h + | inr d => + change (c, d) ∈ (chainNetwork moves w last).edge ↔ _ + rw [ih c d] + simp only [ChainNode.label, chainSymbol, ChainNode.position, Nat.cast_add, Nat.cast_one] + constructor <;> rintro ⟨move, h, hp⟩ <;> exact ⟨move, h, by omega⟩ + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Classes.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Classes.lean new file mode 100644 index 0000000000000000000000000000000000000000..bf606c746a5a92b8b50078f18e81c159406a413a --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Classes.lean @@ -0,0 +1,342 @@ +import OAI.Combinatorics.TwoWayAutomata.Diagram + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +namespace Diagram + +theorem corner_comp {m : ℕ} {e z w : Diagram m} (hz : z ∈ e.corner) + (hw : w ∈ e.corner) : comp z w ∈ e.corner := by + constructor + · rw [← comp_assoc, hz.1] + · rw [comp_assoc, hw.2] + +theorem corner_pow_succ {m : ℕ} {e z : Diagram m} (hz : z ∈ e.corner) (k : ℕ) : + z ^ (k + 1) ∈ e.corner := by + induction k with + | zero => simpa using hz + | succ k ih => exact pow_succ z (k + 1) ▸ corner_comp ih hz + +theorem nesting_refl {m : ℕ} {e : Diagram m} (he : comp e e = e) : + (e, e) ∈ nesting m := ⟨he, he, he, he⟩ + +theorem nesting_trans {m : ℕ} {d e f : Diagram m} + (hde : (d, e) ∈ nesting m) (hef : (e, f) ∈ nesting m) : + (d, f) ∈ nesting m := by + refine ⟨hde.1, hef.2.1, ?_, ?_⟩ + · calc + comp d f = comp (comp d e) f := by rw [hde.2.2.1] + _ = comp d e := by rw [comp_assoc, hef.2.2.1] + _ = d := hde.2.2.1 + · calc + comp f d = comp f (comp e d) := by rw [hde.2.2.2] + _ = comp e d := by rw [← comp_assoc, hef.2.2.2] + _ = d := hde.2.2.2 + +theorem comp_mono {m : ℕ} {a a' b b' : Diagram m} + (ha : (a, a') ∈ inclusion m) (hb : (b, b') ∈ inclusion m) : + (comp a b, comp a' b') ∈ inclusion m := by + obtain ⟨haf, hab, hal, har⟩ := ha + obtain ⟨hbf, hbb, hbl, hbr⟩ := hb + refine ⟨?_, ?_, ?_, ?_⟩ <;> dsimp [comp] <;> gcongr + +theorem through_loop_through {m : ℕ} (e : Diagram m) (he : comp e e = e) + (positive : Bool) : + e.through positive ○ e.loopClosure positive ○ e.through positive = e.through positive := by + cases positive + · exact congrArg backward he + · exact congrArg forward he + +/-- Idempotence of the recurrence relation. -/ +theorem recurrence_idempotent {m : ℕ} (e : Diagram m) (he : comp e e = e) + (positive : Bool) : + e.recurrence positive ○ e.recurrence positive = e.recurrence positive := by + have hK : e.loopClosure positive ○ e.loopClosure positive = e.loopClosure positive := by + cases positive <;> simp [loopClosure] + unfold recurrence + calc + (e.loopClosure positive ○ e.through positive ○ e.loopClosure positive) ○ + (e.loopClosure positive ○ e.through positive ○ e.loopClosure positive) = + e.loopClosure positive ○ + (e.through positive ○ (e.loopClosure positive ○ e.loopClosure positive) ○ + e.through positive) ○ e.loopClosure positive := by simp only [SetRel.comp_assoc] + _ = e.loopClosure positive ○ e.through positive ○ e.loopClosure positive := by + rw [hK, through_loop_through e he positive] + +theorem corner_returns {m : ℕ} (e z : Diagram m) (hz : z ∈ e.corner) : + e.left ⊆ z.left ∧ e.right ⊆ z.right := by + constructor + · rw [← congrArg left hz.1] + exact Set.subset_union_left + · rw [← congrArg right hz.2] + exact Set.subset_union_left + +/-- Positive through-edge absorption in a corner. -/ +theorem corner_forward_absorption {m : ℕ} (e z : Diagram m) (hz : z ∈ e.corner) : + e.forward ○ e.loopClosure true ○ z.forward ⊆ z.forward ∧ + z.forward ○ e.loopClosure true ○ e.forward ⊆ z.forward := by + obtain ⟨hL, hR⟩ := corner_returns e z hz + have hl : e.loopClosure true ⊆ relStar (z.left ○ e.right) := + relStar_mono (SetRel.comp_subset_comp_left hL) + have hr : e.loopClosure true ⊆ relStar (e.left ○ z.right) := + relStar_mono (SetRel.comp_subset_comp_right hR) + constructor + · calc + _ ⊆ e.forward ○ relStar (z.left ○ e.right) ○ z.forward := by gcongr + _ = z.forward := congrArg forward hz.1 + · calc + _ ⊆ z.forward ○ relStar (e.left ○ z.right) ○ e.forward := by gcongr + _ = z.forward := congrArg forward hz.2 + +theorem corner_through_absorption {m : ℕ} (e z : Diagram m) (hz : z ∈ e.corner) + (positive : Bool) : + e.through positive ○ e.loopClosure positive ○ z.through positive ⊆ z.through positive ∧ + z.through positive ○ e.loopClosure positive ○ e.through positive ⊆ z.through positive := by + cases positive + · obtain ⟨hL, hR⟩ := corner_returns e z hz + have hl : e.loopClosure false ⊆ relStar (e.right ○ z.left) := + relStar_mono (SetRel.comp_subset_comp_right hL) + have hr : e.loopClosure false ⊆ relStar (z.right ○ e.left) := + relStar_mono (SetRel.comp_subset_comp_left hR) + constructor + · calc + _ ⊆ e.backward ○ relStar (z.right ○ e.left) ○ z.backward := by + dsimp [through]; gcongr + _ = z.backward := congrArg backward hz.2 + · calc + _ ⊆ z.backward ○ relStar (e.right ○ z.left) ○ e.backward := by + dsimp [through]; gcongr + _ = z.backward := congrArg backward hz.1 + · exact corner_forward_absorption e z hz + +@[simp] +theorem loopClosure_comp_self {m : ℕ} (e : Diagram m) (positive : Bool) : + e.loopClosure positive ○ e.loopClosure positive = e.loopClosure positive := + relStar_comp_self _ + +theorem loopClosure_comp {m : ℕ} (e : Diagram m) (positive : Bool) + (R : SetRel (Fin m) (Fin m)) : + e.loopClosure positive ○ (e.loopClosure positive ○ R) = e.loopClosure positive ○ R := by + rw [← SetRel.comp_assoc, loopClosure_comp_self] + +theorem surviving_absorption {m : ℕ} (e z : Diagram m) (hz : z ∈ e.corner) + (positive : Bool) : + e.recurrence positive ○ e.surviving z positive ⊆ e.surviving z positive ∧ + e.surviving z positive ○ e.recurrence positive ⊆ e.surviving z positive := by + obtain ⟨hl, hr⟩ := corner_through_absorption e z hz positive + constructor + · calc + _ = e.loopClosure positive ○ + (e.through positive ○ e.loopClosure positive ○ z.through positive) ○ + e.loopClosure positive := by + simp only [recurrence, surviving, SetRel.comp_assoc, loopClosure_comp] + _ ⊆ e.surviving z positive := by unfold surviving; gcongr + · calc + _ = e.loopClosure positive ○ + (z.through positive ○ e.loopClosure positive ○ e.through positive) ○ + e.loopClosure positive := by + simp only [recurrence, surviving, SetRel.comp_assoc, loopClosure_comp] + _ ⊆ e.surviving z positive := by unfold surviving; gcongr + +theorem recurrence_trans {m : ℕ} (e : Diagram m) (he : comp e e = e) + {positive : Bool} {x y z : Fin m} + (hxy : (x, y) ∈ e.recurrence positive) (hyz : (y, z) ∈ e.recurrence positive) : + (x, z) ∈ e.recurrence positive := by + rw [← recurrence_idempotent e he positive] + exact ⟨y, hxy, hyz⟩ + +theorem class_nonempty {m : ℕ} {e : Diagram m} {c : Bool × Set (Fin m)} + (hc : c ∈ e.classes) : c.2.Nonempty := by + obtain ⟨x, hx, hxc⟩ := hc + exact ⟨x, hxc ▸ ⟨hx, hx⟩⟩ + +theorem class_related {m : ℕ} (e : Diagram m) (he : comp e e = e) + {c : Bool × Set (Fin m)} (hc : c ∈ e.classes) {x y : Fin m} + (hx : x ∈ c.2) (hy : y ∈ c.2) : + (x, y) ∈ e.recurrence c.1 ∧ (y, x) ∈ e.recurrence c.1 := by + obtain ⟨p, _, hC⟩ := hc + rw [hC] at hx hy + exact ⟨recurrence_trans e he hx.2 hy.1, recurrence_trans e he hy.2 hx.1⟩ + +theorem class_eq_of_mem {m : ℕ} (e : Diagram m) (he : comp e e = e) + {c : Bool × Set (Fin m)} (hc : c ∈ e.classes) {x : Fin m} (hx : x ∈ c.2) : + c.2 = {y | (x, y) ∈ e.recurrence c.1 ∧ (y, x) ∈ e.recurrence c.1} := by + ext y + constructor + · exact class_related e he hc hx + · intro hy + obtain ⟨p, _, hC⟩ := hc + rw [hC] at hx ⊢ + exact ⟨recurrence_trans e he hx.1 hy.1, recurrence_trans e he hy.2 hx.2⟩ + +theorem class_eq_of_common {m : ℕ} (e : Diagram m) (he : comp e e = e) + {c d : Bool × Set (Fin m)} (hc : c ∈ e.classes) (hd : d ∈ e.classes) + (hsign : c.1 = d.1) {x : Fin m} (hx : x ∈ c.2) (hxd : x ∈ d.2) : c = d := by + apply Prod.ext hsign + rw [class_eq_of_mem e he hc hx, class_eq_of_mem e he hd hxd, hsign] + +theorem surviving_loop_iff {m : ℕ} (e z : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) {c : Bool × Set (Fin m)} (hc : c ∈ e.classes) + {x y : Fin m} (hx : x ∈ c.2) (hy : y ∈ c.2) : + (x, x) ∈ e.surviving z c.1 ↔ (y, y) ∈ e.surviving z c.1 := by + obtain ⟨hxy, hyx⟩ := class_related e he hc hx hy + obtain ⟨hl, hr⟩ := surviving_absorption e z hz c.1 + exact ⟨fun h => hr ⟨x, hl ⟨x, hyx, h⟩, hxy⟩, + fun h => hr ⟨y, hl ⟨y, hxy, h⟩, hyx⟩⟩ + +theorem not_mem_missing_iff {m : ℕ} (e z : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) {c : Bool × Set (Fin m)} (hc : c ∈ e.classes) + {x : Fin m} (hx : x ∈ c.2) : + c ∉ e.missing z ↔ (x, x) ∈ e.surviving z c.1 := by + classical + change ¬ (c ∈ e.classes ∧ _) ↔ _ + simp only [hc, true_and, not_forall, not_not] + exact ⟨fun ⟨y, hy, hyy⟩ => (surviving_loop_iff e z he hz hc hy hx).mp hyy, + fun hxx => ⟨x, hx, hxx⟩⟩ + +@[simp] +theorem missing_self {m : ℕ} (e : Diagram m) : e.missing e = ∅ := by + apply Set.eq_empty_iff_forall_notMem.mpr + rintro c ⟨⟨x, hx, hxc⟩, hmissing⟩ + exact hmissing x (hxc ▸ ⟨hx, hx⟩) hx + +theorem surviving_comp {m : ℕ} (e z w : Diagram m) + (hz : z ∈ e.corner) (hw : w ∈ e.corner) (positive : Bool) : + (if positive then e.surviving z positive ○ e.surviving w positive + else e.surviving w positive ○ e.surviving z positive) ⊆ + e.surviving (comp z w) positive := by + obtain ⟨hzL, hzR⟩ := corner_returns e z hz + obtain ⟨hwL, hwR⟩ := corner_returns e w hw + have hplus : e.loopClosure true ⊆ relStar (w.left ○ z.right) := + relStar_mono (SetRel.comp_subset_comp hwL hzR) + have hminus : e.loopClosure false ⊆ relStar (z.right ○ w.left) := + relStar_mono (SetRel.comp_subset_comp hzR hwL) + cases positive <;> simp only [Bool.false_eq_true, ite_false, ite_true] + · calc + _ = e.loopClosure false ○ + (w.backward ○ e.loopClosure false ○ z.backward) ○ e.loopClosure false := by + simp only [surviving, through, Bool.false_eq_true, ite_false, + SetRel.comp_assoc, loopClosure_comp] + _ ⊆ e.surviving (comp z w) false := by + dsimp [surviving, through, comp]; gcongr + · calc + _ = e.loopClosure true ○ + (z.forward ○ e.loopClosure true ○ w.forward) ○ e.loopClosure true := by + simp only [surviving, through, ite_true, + SetRel.comp_assoc, loopClosure_comp] + _ ⊆ e.surviving (comp z w) true := by + dsimp [surviving, through, comp]; gcongr + +/-- A class can disappear from a product only if it disappears from a factor. -/ +theorem missing_comp {m : ℕ} (e z w : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) (hw : w ∈ e.corner) : + e.missing (comp z w) ⊆ e.missing z ∪ e.missing w := by + classical + intro c hc + by_contra h + obtain ⟨hnz, hnw⟩ := not_or.mp h + obtain ⟨x, hx⟩ := class_nonempty hc.1 + have hzz := (not_mem_missing_iff e z he hz hc.1 hx).mp hnz + have hww := (not_mem_missing_iff e w he hw hc.1 hx).mp hnw + apply hc.2 x hx + apply surviving_comp e z w hz hw c.1 + cases hs : c.1 <;> simp only [hs] at hzz hww ⊢ <;> + simp only [Bool.false_eq_true, ite_false, ite_true] + · exact ⟨x, hww, hzz⟩ + · exact ⟨x, hzz, hww⟩ + +theorem missing_pow_succ {m : ℕ} (e z : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) (k : ℕ) : e.missing (z ^ (k + 1)) ⊆ e.missing z := by + induction k with + | zero => simp + | succ k ih => + rw [pow_succ] + exact (missing_comp e _ z he (corner_pow_succ hz k) hz).trans + (Set.union_subset ih Set.Subset.rfl) + +/-- There are at most two signed recurrent classes per boundary label. -/ +theorem class_count {m : ℕ} (e : Diagram m) : e.classes.ncard ≤ 2 * m := by + let f : Bool × Fin m → Bool × Set (Fin m) := fun p => + (p.1, {y | (p.2, y) ∈ e.recurrence p.1 ∧ (y, p.2) ∈ e.recurrence p.1}) + have hsub : e.classes ⊆ f '' Set.univ := by + rintro ⟨positive, C⟩ ⟨x, _, hC⟩ + exact ⟨(positive, x), Set.mem_univ _, Prod.ext rfl hC.symm⟩ + calc + e.classes.ncard ≤ (f '' Set.univ).ncard := Set.ncard_le_ncard hsub + _ ≤ (Set.univ : Set (Bool × Fin m)).ncard := Set.ncard_image_le + _ = 2 * m := by simp + +theorem through_comp_recurrence {m : ℕ} (e : Diagram m) (he : comp e e = e) + (positive : Bool) : + e.through positive ○ e.recurrence positive = e.through positive ○ e.loopClosure positive := by + simpa only [recurrence, SetRel.comp_assoc] using + congrArg (· ○ e.loopClosure positive) (through_loop_through e he positive) + +theorem recurrence_comp_through {m : ℕ} (e : Diagram m) (he : comp e e = e) + (positive : Bool) : + e.recurrence positive ○ e.through positive = e.loopClosure positive ○ e.through positive := by + simpa only [recurrence, SetRel.comp_assoc] using + congrArg (e.loopClosure positive ○ ·) (through_loop_through e he positive) + +theorem rectangle_subset {m : ℕ} (e : Diagram m) (he : comp e e = e) + (positive : Bool) (p : Fin m) : e.rectangle positive p ⊆ e.through positive := by + rintro ⟨u, v⟩ ⟨⟨a, hua, hap⟩, ⟨b, hpb, hbv⟩⟩ + rw [← through_loop_through e he positive] + exact ⟨b, ⟨a, hua, hap.trans hpb⟩, hbv⟩ + +theorem rectangle_eq_of_related {m : ℕ} (e : Diagram m) (he : comp e e = e) + {positive : Bool} {p q : Fin m} (hpq : (p, q) ∈ e.recurrence positive) + (hqp : (q, p) ∈ e.recurrence positive) : + e.rectangle positive p = e.rectangle positive q := by + have hleft : (e.through positive ○ e.loopClosure positive) ○ e.recurrence positive = + e.through positive ○ e.loopClosure positive := by + calc + _ = e.through positive ○ e.recurrence positive := by + simp only [recurrence, SetRel.comp_assoc, loopClosure_comp] + _ = _ := through_comp_recurrence e he positive + have hright : e.recurrence positive ○ (e.loopClosure positive ○ e.through positive) = + e.loopClosure positive ○ e.through positive := by + calc + _ = e.recurrence positive ○ e.through positive := by + simp only [recurrence, SetRel.comp_assoc, loopClosure_comp] + _ = _ := recurrence_comp_through e he positive + ext ⟨u, v⟩ + exact ⟨fun ⟨hup, hpv⟩ => + ⟨hleft ▸ (show (u, q) ∈ _ ○ e.recurrence positive from ⟨p, hup, hpq⟩), + hright ▸ (show (q, v) ∈ e.recurrence positive ○ _ from ⟨p, hqp, hpv⟩)⟩, + fun ⟨huq, hqv⟩ => + ⟨hleft ▸ (show (u, p) ∈ _ ○ e.recurrence positive from ⟨q, huq, hqp⟩), + hright ▸ (show (p, v) ∈ e.recurrence positive ○ _ from ⟨q, hpq, hqv⟩)⟩⟩ + +/-- The recurrent rectangles cover every through edge, including for an empty boundary. -/ +theorem rectangle_cover {m : ℕ} (e : Diagram m) (he : comp e e = e) + {positive : Bool} {u v : Fin m} (huv : (u, v) ∈ e.through positive) : + ∃ p, (p, p) ∈ e.recurrence positive ∧ (u, v) ∈ e.rectangle positive p := by + have hFPF : e.through positive ○ e.recurrence positive ○ e.through positive = + e.through positive := by + rw [through_comp_recurrence e he positive, through_loop_through e he positive] + obtain ⟨b, ⟨a, hua, hab⟩, hbv⟩ := hFPF.symm ▸ huv + obtain ⟨p, hap, hpp, hpb⟩ := + idempotent_relation_factor _ (recurrence_idempotent e he positive) hab + refine ⟨p, hpp, ?_, ?_⟩ + · rw [← through_comp_recurrence e he positive] + exact ⟨a, hua, hap⟩ + · rw [← recurrence_comp_through e he positive] + exact ⟨b, hpb, hbv⟩ + +theorem rectangle_survives {m : ℕ} (e z : Diagram m) (hz : z ∈ e.corner) + {positive : Bool} {p : Fin m} (hp : (p, p) ∈ e.surviving z positive) : + e.rectangle positive p ⊆ z.through positive := by + obtain ⟨hl, hr⟩ := corner_through_absorption e z hz positive + rintro ⟨u, v⟩ ⟨⟨a, hua, hap⟩, ⟨b, hpb, hbv⟩⟩ + obtain ⟨d, ⟨c, hpc, hcd⟩, hdp⟩ := hp + exact hr ⟨b, ⟨d, hl ⟨c, ⟨a, hua, hap.trans hpc⟩, hcd⟩, hdp.trans hpb⟩, hbv⟩ + +end Diagram + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Corners.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Corners.lean new file mode 100644 index 0000000000000000000000000000000000000000..29922e127f35288be4b88028db1e1c96721286c4 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Corners.lean @@ -0,0 +1,208 @@ +import OAI.Combinatorics.TwoWayAutomata.Nesting + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- A finite nonempty subsemigroup contains an idempotent. -/ +theorem finite_subsemigroup_idempotent {A : Type*} [Semigroup A] [Finite A] + (S : Set A) (hne : S.Nonempty) (hmul : ∀ x ∈ S, ∀ y ∈ S, x * y ∈ S) : + ∃ e ∈ S, IsIdempotentElem e := by + let : TopologicalSpace A := ⊥ + let : DiscreteTopology A := ⟨rfl⟩ + exact exists_idempotent_in_compact_subsemigroup (fun _ => continuous_of_discreteTopology) + S hne (Set.toFinite S).isCompact hmul + +/-- Every element of a finite monoid has an idempotent positive power. -/ +theorem finite_idempotent_pow {A : Type*} [Monoid A] [Finite A] (a : A) : + ∃ k : ℕ, 0 < k ∧ IsIdempotentElem (a ^ k) := by + obtain ⟨_, ⟨k, rfl⟩, hk⟩ := finite_subsemigroup_idempotent + (Set.range (fun k : ℕ => a ^ (k + 1))) ⟨a, 0, by simp⟩ (by + rintro _ ⟨i, rfl⟩ _ ⟨j, rfl⟩ + exact ⟨i + j + 1, by simp only [← pow_add, Nat.add_assoc, Nat.add_left_comm, Nat.add_comm]⟩) + exact ⟨k + 1, Nat.succ_pos _, hk⟩ + +/-- A multiplicative map preserves positive powers without any unitality assumption. -/ +theorem map_positive_pow {A B : Type*} [Monoid A] [Monoid B] + (f : A →ₙ* B) (a : A) (k : ℕ) : f (a ^ (k + 1)) = f a ^ (k + 1) := by + induction k with + | zero => simp + | succ k ih => rw [pow_succ, map_mul, ih, ← pow_succ] + +/-- A nested chain with a common missing-set budget. -/ +theorem exists_nested_chain {A : Type*} [Monoid A] [Finite A] {m k : ℕ} + (ι : A →ₙ* Diagram m) (b0 : A) (hb0 : IsIdempotentElem b0) (g : ℕ → A) + (J : Set (Bool × Set (Fin m))) + (hg : ∀ j < k, ι (g j) ∈ (ι b0).corner ∧ (ι b0).missing (ι (g j)) ⊆ J) : + ∃ b : ℕ → A, b 0 = b0 ∧ + (∀ j, IsIdempotentElem (b j)) ∧ + (∀ j, ∃ n : ℕ, b (j + 1) = (b j * g j * b j) ^ (n + 1)) ∧ + (∀ j, (ι (b (j + 1)), ι (b j)) ∈ Diagram.nesting m) ∧ + (∀ j ≤ k, (ι b0).missing (ι (b j)) ⊆ J) ∧ + ∑ j ∈ Finset.range k, ((ι (b j)).missing (ι (b (j + 1)))).ncard ≤ 2 * J.ncard := by + classical + choose power hpower using fun a : A => finite_idempotent_pow a + let b : ℕ → A := Nat.rec b0 (fun j prev => (prev * g j * prev) ^ power (prev * g j * prev)) + have hzero : b 0 = b0 := rfl + have hsucc (j : ℕ) : b (j + 1) = (b j * g j * b j) ^ power (b j * g j * b j) := rfl + have hidemp (j : ℕ) : IsIdempotentElem (b j) := by + cases j with + | zero => exact hb0 + | succ j => exact (hpower _).2 + have hstep (j : ℕ) : ∃ n : ℕ, b (j + 1) = (b j * g j * b j) ^ (n + 1) := by + obtain ⟨n, hn⟩ := Nat.exists_eq_succ_of_ne_zero (hpower (b j * g j * b j)).1.ne' + exact ⟨n, by rw [hsucc, hn]⟩ + have hmapidemp (j : ℕ) : Diagram.comp (ι (b j)) (ι (b j)) = ι (b j) := by + exact (map_mul ι (b j) (b j)).symm.trans (congrArg ι (hidemp j).eq) + have hnest (j : ℕ) : (ι (b (j + 1)), ι (b j)) ∈ Diagram.nesting m := by + obtain ⟨n, hn⟩ := hstep j + have hcorner : ι (b j * g j * b j) ∈ (ι (b j)).corner := by + constructor + · change ι (b j) * ι (b j * g j * b j) = _ + rw [← map_mul, ← mul_assoc, ← mul_assoc, (hidemp j).eq] + · change ι (b j * g j * b j) * ι (b j) = _ + rw [← map_mul, mul_assoc, (hidemp j).eq] + have hpow := Diagram.corner_pow_succ hcorner n + rw [← map_positive_pow, ← hn] at hpow + exact ⟨hmapidemp (j + 1), hmapidemp j, hpow.2, hpow.1⟩ + have hbase (j : ℕ) : (ι (b j), ι b0) ∈ Diagram.nesting m := by + induction j with + | zero => exact Diagram.nesting_refl (hmapidemp 0) + | succ j ih => exact Diagram.nesting_trans (hnest j) ih + have hbudget : ∀ j ≤ k, (ι b0).missing (ι (b j)) ⊆ J := by + intro j + induction j with + | zero => intro _; rw [hzero, Diagram.missing_self]; exact Set.empty_subset _ + | succ j ih => + intro hj + obtain ⟨n, hn⟩ := hstep j + have hb : ι (b j) ∈ (ι b0).corner := ⟨(hbase j).2.2.2, (hbase j).2.2.1⟩ + have hgj := (hg j (by omega)).1 + have hbg := Diagram.corner_comp hb hgj + have hpow := Diagram.missing_pow_succ (ι b0) (ι (b j * g j * b j)) + (hmapidemp 0) (by rw [map_mul, map_mul]; exact Diagram.corner_comp hbg hb) n + rw [← map_positive_pow, ← hn] at hpow + apply hpow.trans + have hfirst := Diagram.missing_comp (ι b0) (ι (b j)) (ι (g j)) (hmapidemp 0) hb hgj + have hsecond := Diagram.missing_comp (ι b0) (ι (b j) * ι (g j)) (ι (b j)) + (hmapidemp 0) hbg hb + rw [map_mul, map_mul] + exact hsecond.trans + (Set.union_subset (hfirst.trans (Set.union_subset (ih (by omega)) (hg j (by omega)).2)) + (ih (by omega))) + exact ⟨b, hzero, hidemp, hstep, hnest, hbudget, + Diagram.nested_chain_bound (fun j => ι (b j)) (fun j _ => hmapidemp j) + (fun j _ => hnest j) J (by simpa only [hzero] using hbudget)⟩ + +/-- An idempotent has a lift minimal in the nesting order. -/ +theorem minimal_idempotent_lift {A M : Type*} [Semigroup A] [Finite A] [Semigroup M] + (θ : A →ₙ* M) (hθ : Function.Surjective θ) (q : M) (hq : IsIdempotentElem q) : + ∃ d, IsIdempotentElem d ∧ θ d = q ∧ + ∀ e, IsIdempotentElem e → θ e = q → e * d = e → d * e = e → e = d := by + obtain ⟨a, ha⟩ := hθ q + obtain ⟨d0, hd0q, hd0⟩ := finite_subsemigroup_idempotent + {x | θ x = q} ⟨a, ha⟩ (by + intro x hx y hy + change θ (x * y) = q + rw [map_mul, hx, hy, hq.eq]) + obtain ⟨d, ⟨hd, hdq⟩, hminimal⟩ := Set.exists_min_image + {d | IsIdempotentElem d ∧ θ d = q} + (fun d => (Subsemigroup.corner d : Set A).ncard) (Set.toFinite _) ⟨d0, hd0, hd0q⟩ + refine ⟨d, hd, hdq, ?_⟩ + intro e he heq hed hde + have hsub : (Subsemigroup.corner e : Set A) ⊆ Subsemigroup.corner d := by + rintro x ⟨y, rfl⟩ + apply (Subsemigroup.mem_corner_iff hd).mpr + exact ⟨by simp only [← mul_assoc, hde], by simp only [mul_assoc, hed]⟩ + have heqcorner := Set.eq_of_subset_of_ncard_le hsub (hminimal e ⟨he, heq⟩) + have hdin : d ∈ Subsemigroup.corner e := by + change d ∈ (Subsemigroup.corner e : Set A) + rw [heqcorner] + exact (Subsemigroup.mem_corner_iff hd).mpr ⟨hd, hd⟩ + exact hed.symm.trans ((Subsemigroup.mem_corner_iff he).mp hdin).1 + +/-- Mathlib's semigroup corner, equipped with its idempotent as identity. -/ +instance cornerMonoid {A : Type*} [Semigroup A] {e : A} (he : IsIdempotentElem e) : + Monoid he.Corner where + __ : Semigroup he.Corner := inferInstanceAs (Semigroup (Subsemigroup.corner e)) + one := ⟨e, (Subsemigroup.mem_corner_iff he).mpr ⟨he, he⟩⟩ + one_mul x := Subtype.ext ((Subsemigroup.mem_corner_iff he).mp x.property).1 + mul_one x := Subtype.ext ((Subsemigroup.mem_corner_iff he).mp x.property).2 + +@[instance] +theorem finite_corner {A : Type*} [Semigroup A] [Finite A] {e : A} + (he : IsIdempotentElem e) : Finite he.Corner := + inferInstanceAs (Finite (Subsemigroup.corner e)) + +/-- A multiplicative map restricts to a unital map between matching idempotent corners. -/ +def cornerMap {A M : Type*} [Semigroup A] [Semigroup M] (θ : A →ₙ* M) + {d : A} {q : M} (hd : IsIdempotentElem d) (hq : IsIdempotentElem q) (hθd : θ d = q) : + hd.Corner →* hq.Corner where + toFun x := ⟨θ x.val, (Subsemigroup.mem_corner_iff hq).mpr (by + obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hd).mp x.property + exact ⟨by rw [← hθd, ← map_mul, hl], by rw [← hθd, ← map_mul, hr]⟩)⟩ + map_one' := Subtype.ext hθd + map_mul' x y := Subtype.ext (map_mul θ x.val y.val) + +theorem cornerMap_surjective {A M : Type*} [Semigroup A] [Semigroup M] + (θ : A →ₙ* M) (hθ : Function.Surjective θ) {d : A} {q : M} + (hd : IsIdempotentElem d) (hq : IsIdempotentElem q) (hθd : θ d = q) : + Function.Surjective (cornerMap θ hd hq hθd) := by + intro y + obtain ⟨x, hx⟩ := hθ y.val + refine ⟨⟨d * x * d, x, rfl⟩, ?_⟩ + apply Subtype.ext + change θ (d * x * d) = y.val + obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hq).mp y.property + rw [map_mul, map_mul, hθd, hx, hl, hr] + +/-- At a minimal idempotent lift, every element with unit image is a unit. -/ +theorem minimal_corner_reflects_units {A M : Type*} [Semigroup A] [Finite A] [Semigroup M] + (θ : A →ₙ* M) {d : A} {q : M} (hd : IsIdempotentElem d) (hq : IsIdempotentElem q) + (hθd : θ d = q) + (hminimal : ∀ e, IsIdempotentElem e → θ e = q → e * d = e → d * e = e → e = d) + (w : hd.Corner) (hw : IsUnit (cornerMap θ hd hq hθd w)) : IsUnit w := by + obtain ⟨k, hk, hpow⟩ := finite_idempotent_pow w + have hmapIdem : IsIdempotentElem (cornerMap θ hd hq hθd (w ^ k)) := by + change _ * _ = _ + rw [← map_mul, hpow.eq] + have hmapUnit : IsUnit (cornerMap θ hd hq hθd (w ^ k)) := by + rw [map_pow] + exact hw.pow k + have hmapOne := (IsIdempotentElem.iff_eq_one_of_isUnit hmapUnit).mp hmapIdem + have hθpow : θ (w ^ k).val = q := congrArg Subtype.val hmapOne + obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hd).mp (w ^ k).property + have hwd : (w ^ k).val = d := hminimal _ (congrArg Subtype.val hpow.eq) hθpow hr hl + exact IsUnit.of_pow_eq_one (Subtype.ext hwd) hk.ne' + +/-- Taking a corner inside an absorbing corner gives the same smaller corner. -/ +def cornerFlatten {M : Type*} [Semigroup M] {p : M} (hp : IsIdempotentElem p) + (q : hp.Corner) (hq : IsIdempotentElem q) : + hq.Corner ≃* (show IsIdempotentElem q.val from congrArg Subtype.val hq.eq).Corner := by + let hQ : IsIdempotentElem q.val := congrArg Subtype.val hq.eq + have hqprop := (Subsemigroup.mem_corner_iff hp).mp q.property + refine + { toFun := fun x => ⟨x.val.val, (Subsemigroup.mem_corner_iff hQ).mpr ?_⟩ + invFun := fun y => ⟨⟨y.val, (Subsemigroup.mem_corner_iff hp).mpr ?_⟩, + (Subsemigroup.mem_corner_iff hq).mpr ?_⟩ + left_inv := fun _ => rfl + right_inv := fun _ => rfl + map_mul' := fun _ _ => rfl } + · obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hq).mp x.property + exact ⟨congrArg Subtype.val hl, congrArg Subtype.val hr⟩ + · obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hQ).mp y.property + constructor + · calc + p * y.val = p * (q.val * y.val) := by rw [hl] + _ = y.val := by rw [← mul_assoc, hqprop.1, hl] + · calc + y.val * p = (y.val * q.val) * p := by rw [hr] + _ = y.val := by rw [mul_assoc, hqprop.2, hr] + · obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hQ).mp y.property + exact ⟨Subtype.ext hl, Subtype.ext hr⟩ + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicBound.lean b/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicBound.lean new file mode 100644 index 0000000000000000000000000000000000000000..4d99245644c0dcbb7e5bfa3a30623d4240b86c64 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicBound.lean @@ -0,0 +1,67 @@ +import OAI.Combinatorics.TwoWayAutomata.DeterministicTransport +import OAI.Combinatorics.TwoWayAutomata.RelationBridge +import OAI.Combinatorics.TwoWayAutomata.TransitionBridge +import OAI.Combinatorics.Automata.Main + +namespace OAI + +namespace TwoWayComplementation + +namespace CompanionBridge + +/-- A state lower bound for every deterministic recognizer of relation liveness. -/ +theorem deterministic_bound (h s : ℕ) (hh : 2 ≤ h) + (D : TwoNFA (SetRel (Fin h) (Fin h)) (Fin s)) + (hdet : D.Deterministic) (hD : D.language = sourceLanguage (Fin h)) : + 2 ^ ((h - 2) / 31) ≤ 4 * (s + 1) ^ 2 := by + let A := D.pullAlphabet (relationEquiv (Fin h)) + have ha : A.Deterministic := hdet.pullAlphabet _ + apply (OneWayLiveness.main_theorem h hh).2 false s (toDMachine A ha) + intro w + rw [toDMachine_accepts] + change w ∈ (D.pullAlphabet (relationEquiv (Fin h))).language ↔ _ + rw [D.pullAlphabet_language, hD] + exact sourceLanguage_map_iff w + +end CompanionBridge + +/-- Arithmetic specialization for the relation alphabet on n - 2 vertices. -/ +theorem deterministic_rate (n s : ℕ) (hn : 131 ≤ n) + (hb : 2 ^ ((n - 4) / 31) ≤ 4 * (s + 1) ^ 2) : + (1 / 2 : ℝ) * 2 ^ ((n - 4) / 127) ≤ (s : ℝ) := by + let k := (n - 4) / 127 + have hk : 1 ≤ k := by dsimp [k]; omega + have hrate : 4 * k ≤ (n - 4) / 31 := by dsimp [k]; omega + have hbR : (2 : ℝ) ^ ((n - 4) / 31) ≤ 4 * ((s : ℝ) + 1) ^ 2 := by + exact_mod_cast hb + have hp : (2 : ℝ) ^ (4 * k) ≤ 4 * ((s : ℝ) + 1) ^ 2 := + (pow_le_pow_right₀ (by norm_num : (1 : ℝ) ≤ 2) hrate).trans hbR + have he : (2 : ℝ) ^ (4 * k) = ((2 : ℝ) ^ k) ^ 4 := by + rw [← pow_mul, Nat.mul_comm] + rw [he] at hp + have ht : (2 : ℝ) ≤ (2 : ℝ) ^ k := by + simpa only [pow_one] using + (pow_le_pow_right₀ (by norm_num : (1 : ℝ) ≤ 2) hk) + have hsq : (((2 : ℝ) ^ k) ^ 2) ^ 2 ≤ (2 * ((s : ℝ) + 1)) ^ 2 := by + nlinarith only [hp] + have hs : ((2 : ℝ) ^ k) ^ 2 ≤ 2 * ((s : ℝ) + 1) := + (sq_le_sq₀ (sq_nonneg _) (by positivity)).mp hsq + change (1 / 2 : ℝ) * 2 ^ k ≤ (s : ℝ) + nlinarith [mul_nonneg (sub_nonneg.mpr ht) + (show 0 ≤ (2 : ℝ) ^ k + 1 by positivity)] + +/-- Deterministic lower bound with arbitrary finite state types. -/ +theorem deterministic_liveness_lower_bound (n : ℕ) (hn : 131 ≤ n) + (Q : Type*) [Fintype Q] + (D : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) Q) + (hdet : D.Deterministic) (hD : D.language = sourceLanguage (Fin (n - 2))) : + (1 / 2 : ℝ) * 2 ^ ((n - 4) / 127) ≤ (Fintype.card Q : ℝ) := by + have hb := CompanionBridge.deterministic_bound (n - 2) (Fintype.card Q) (by omega) + (D.reindex (Fintype.equivFin Q)) (hdet.reindex _) (by + rw [TwoNFA.reindex_language, hD]) + apply deterministic_rate n (Fintype.card Q) hn + simpa only [Nat.sub_sub, Nat.reduceAdd] using hb + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicCore.lean b/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicCore.lean new file mode 100644 index 0000000000000000000000000000000000000000..bfbbdeabc3f9a9cc57e62e8c68700208493249c7 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicCore.lean @@ -0,0 +1,13 @@ +import OAI.Combinatorics.TwoWayAutomata.Model + +namespace OAI + +namespace TwoWayComplementation + +/-- At most one next-state/move pair at every state and scanned symbol. -/ +def TwoNFA.Deterministic {Sigma Q : Type*} (A : TwoNFA Sigma Q) : Prop := + ∀ q symbol, (A.transition q symbol).Subsingleton + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicTransport.lean b/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicTransport.lean new file mode 100644 index 0000000000000000000000000000000000000000..6844beb6557e1b9cda877f668dc71e7e3ff08d2f --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/DeterministicTransport.lean @@ -0,0 +1,23 @@ +import OAI.Combinatorics.TwoWayAutomata.DeterministicCore +import OAI.Combinatorics.TwoWayAutomata.Main + +namespace OAI + +namespace TwoWayComplementation + +theorem TwoNFA.Deterministic.reindex {Sigma Q Q' : Type*} {A : TwoNFA Sigma Q} + (hA : A.Deterministic) (e : Q ≃ Q') : (A.reindex e).Deterministic := by + intro q symbol a ha b hb + have h := hA (e.symm q) symbol ha hb + apply Prod.ext + · exact e.symm.injective (congrArg (fun t : Q × Fin 3 => t.1) h) + · exact congrArg (fun t : Q × Fin 3 => t.2) h + +theorem TwoNFA.Deterministic.pullAlphabet {Sigma Gamma Q : Type*} {A : TwoNFA Gamma Q} + (hA : A.Deterministic) (f : Sigma → Gamma) : (A.pullAlphabet f).Deterministic := by + intro q symbol + exact hA q (Sum.map id f symbol) + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Diagram.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Diagram.lean new file mode 100644 index 0000000000000000000000000000000000000000..8107e9a40a0dc66cd7edce5ea3fe7eeb6175b6e7 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Diagram.lean @@ -0,0 +1,338 @@ +import OAI.Combinatorics.TwoWayAutomata.Relations + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Four boundary relations; the direction distinguishes the two copies of `Fin m`. -/ +@[ext] +structure Diagram (m : ℕ) where + /-- Enter on the left and exit on the right. -/ + forward : SetRel (Fin m) (Fin m) + /-- Enter on the right and exit on the left. -/ + backward : SetRel (Fin m) (Fin m) + /-- Enter and exit on the left. -/ + left : SetRel (Fin m) (Fin m) + /-- Enter and exit on the right. -/ + right : SetRel (Fin m) (Fin m) + +namespace Diagram + +/-- Composition of boundary diagrams in path order. -/ +def comp {m : ℕ} (a b : Diagram m) : Diagram m where + forward := a.forward ○ relStar (b.left ○ a.right) ○ b.forward + backward := b.backward ○ relStar (a.right ○ b.left) ○ a.backward + left := a.left ∪ (a.forward ○ relStar (b.left ○ a.right) ○ b.left ○ a.backward) + right := b.right ∪ (b.backward ○ a.right ○ relStar (b.left ○ a.right) ○ b.forward) + +/-- Identity boundary diagram. -/ +def identity (m : ℕ) : Diagram m := ⟨SetRel.id, SetRel.id, ∅, ∅⟩ + +/-- Componentwise edge inclusion, as a relation on diagrams. -/ +def inclusion (m : ℕ) : SetRel (Diagram m) (Diagram m) := + {pair | pair.1.forward ⊆ pair.2.forward ∧ pair.1.backward ⊆ pair.2.backward ∧ + pair.1.left ⊆ pair.2.left ∧ pair.1.right ⊆ pair.2.right} + +theorem inclusion_refl {m : ℕ} (a : Diagram m) : (a, a) ∈ inclusion m := + ⟨Set.Subset.rfl, Set.Subset.rfl, Set.Subset.rfl, Set.Subset.rfl⟩ + +/-- Reflection reverses the order of multiplication. -/ +def reflect {m : ℕ} (a : Diagram m) : Diagram m := + ⟨a.backward, a.forward, a.right, a.left⟩ + +/-- `true` denotes the positive sign; `false` denotes the negative sign. -/ +def through {m : ℕ} (a : Diagram m) (positive : Bool) : SetRel (Fin m) (Fin m) := + if positive then a.forward else a.backward + +/-- Closure of alternating returns at a boundary. -/ +def loopClosure {m : ℕ} (e : Diagram m) (positive : Bool) : SetRel (Fin m) (Fin m) := + relStar (if positive then e.left ○ e.right else e.right ○ e.left) + +/-- The recurrent relation obtained by closing through traversals. -/ +def recurrence {m : ℕ} (e : Diagram m) (positive : Bool) : SetRel (Fin m) (Fin m) := + e.loopClosure positive ○ e.through positive ○ e.loopClosure positive + +/-- Signed recurrent equivalence classes of an idempotent diagram. -/ +def classes {m : ℕ} (e : Diagram m) : Set (Bool × Set (Fin m)) := + {c | ∃ x, (x, x) ∈ e.recurrence c.1 ∧ + c.2 = {y | (x, y) ∈ e.recurrence c.1 ∧ (y, x) ∈ e.recurrence c.1}} + +/-- The corner membership equations for an idempotent `e`. -/ +def corner {m : ℕ} (e : Diagram m) : Set (Diagram m) := + {z | comp e z = z ∧ comp z e = z} + +/-- Nesting of idempotents; distinct from edge inclusion. -/ +def nesting (m : ℕ) : SetRel (Diagram m) (Diagram m) := + {(d, e) | comp d d = d ∧ comp e e = e ∧ comp d e = d ∧ comp e d = d} + +/-- The surviving through relation `U` relative to `e`. -/ +def surviving {m : ℕ} (e z : Diagram m) (positive : Bool) : SetRel (Fin m) (Fin m) := + e.loopClosure positive ○ z.through positive ○ e.loopClosure positive + +/-- Classes whose recurrent loops are absent after replacement by `z`. -/ +def missing {m : ℕ} (e z : Diagram m) : Set (Bool × Set (Fin m)) := + {c | c ∈ e.classes ∧ ∀ x ∈ c.2, (x, x) ∉ e.surviving z c.1} + +/-- Classes containing none of a fixed set of signed protected labels. -/ +def unprotected {m : ℕ} (e : Diagram m) (P : Set (Bool × Fin m)) : Set (Bool × Set (Fin m)) := + {c | c ∈ e.classes ∧ ∀ x ∈ c.2, (c.1, x) ∉ P} + +/-- Rectangle associated with a recurrent representative. -/ +def rectangle {m : ℕ} (e : Diagram m) (positive : Bool) (p : Fin m) : + SetRel (Fin m) (Fin m) := + {u | (u, p) ∈ e.through positive ○ e.loopClosure positive} ×ˢ + {v | (p, v) ∈ e.loopClosure positive ○ e.through positive} + +/-- Boundary ports: `false` is left and `true` is right. -/ +def portRelation {m : ℕ} (e : Diagram m) : SetRel (Bool × Fin m) (Bool × Fin m) := + {pair | (pair.1.2, pair.2.2) ∈ match pair.1.1, pair.2.1 with + | false, false => e.left + | false, true => e.forward + | true, false => e.backward + | true, true => e.right} + +/-- Returns from an exit port through the surrounding left or right context. -/ +def bounceRelation {m : ℕ} (R L : SetRel (Fin m) (Fin m)) : + SetRel (Bool × Fin m) (Bool × Fin m) := + {pair | pair.1.1 = pair.2.1 ∧ (pair.1.2, pair.2.2) ∈ if pair.1.1 then L else R} + +/-- A middle traversal followed by any finite number of context returns and traversals. -/ +def feedback {m : ℕ} (e : Diagram m) (R L : SetRel (Fin m) (Fin m)) : + SetRel (Bool × Fin m) (Bool × Fin m) := + e.portRelation ○ relStar (bounceRelation R L ○ e.portRelation) + +theorem mem_port_through {m : ℕ} (e : Diagram m) (positive : Bool) (p q : Fin m) : + ((!positive, p), (positive, q)) ∈ e.portRelation ↔ (p, q) ∈ e.through positive := by + cases positive <;> rfl + +/-- The rectangular port edges belonging to a signed recurrent class. -/ +def classRectangle {m : ℕ} (e : Diagram m) (C : Bool × Set (Fin m)) : + SetRel (Bool × Fin m) (Bool × Fin m) := + {pair | pair.1.1 = !C.1 ∧ pair.2.1 = C.1 ∧ + ∃ p ∈ C.2, (pair.1.2, pair.2.2) ∈ e.rectangle C.1 p} + +/-- Initial traversal of the outer context, including recurrence closure. -/ +def contextEntry {m : ℕ} (d u v : Diagram m) (positive : Bool) : + SetRel (Fin m) (Bool × Fin m) := + {pair | pair.2.1 = !positive ∧ + (pair.1, pair.2.2) ∈ d.loopClosure positive ○ + (if positive then u.forward else v.backward)} + +/-- Final traversal of the outer context, including recurrence closure. -/ +def contextExit {m : ℕ} (d u v : Diagram m) (positive : Bool) : + SetRel (Bool × Fin m) (Fin m) := + {pair | pair.1.1 = positive ∧ + (pair.1.2, pair.2) ∈ (if positive then v.forward else u.backward) ○ + d.loopClosure positive} + +/-- Traversals with one designated occurrence of a recurrent rectangle in the middle factor. -/ +def classInfluence {m : ℕ} (e d u v : Diagram m) (positive : Bool) + (C : Bool × Set (Fin m)) : SetRel (Fin m) (Fin m) := + contextEntry d u v positive ○ relStar (e.portRelation ○ bounceRelation u.right v.left) ○ + e.classRectangle C ○ relStar (bounceRelation u.right v.left ○ e.portRelation) ○ + contextExit d u v positive + +/-- Target classes whose original traversals can use a rectangle from the fixed set `J`. -/ +def transportMarked {m : ℕ} (e d u v : Diagram m) (J : Set (Bool × Set (Fin m))) : + Set (Bool × Set (Fin m)) := + {D | D ∈ d.classes ∧ ∃ C ∈ J, ∃ x ∈ D.2, (x, x) ∈ e.classInfluence d u v D.1 C} + +theorem feedback_forward {m : ℕ} (e : Diagram m) (R L : SetRel (Fin m) (Fin m)) + {p q : Fin m} : + ((false, p), (true, q)) ∈ e.feedback R L ↔ + (p, q) ∈ relStar (e.left ○ R) ○ e.forward ○ + relStar (L ○ (e.right ∪ (e.backward ○ R ○ relStar (e.left ○ R) ○ e.forward))) := by + let S := relStar (e.left ○ R) + let P := e.right ∪ (e.backward ○ R ○ S ○ e.forward) + let V := relStar (L ○ P) + let E := e.portRelation + let B := bounceRelation R L + have hbound {a b : Bool × Fin m} (hab : (a, b) ∈ relStar (B ○ E)) (hb : b.1 = true) : + (a.2, b.2) ∈ if a.1 then V else R ○ S ○ e.forward ○ V := by + induction hab using Relation.ReflTransGen.head_induction_on with + | refl => simp only [hb, ite_true]; exact Relation.ReflTransGen.refl + | @head a c hac _ ih => + obtain ⟨mid, hB, hE⟩ := hac + obtain ⟨sa, x⟩ := a + obtain ⟨sc, y⟩ := c + obtain ⟨sm, z⟩ := mid + obtain ⟨hside, hreturn⟩ := hB + change sa = sm at hside + subst sm + cases sa <;> cases sc + · obtain ⟨t, ⟨u, ⟨v, hyv, hvu⟩, hut⟩, htb⟩ := ih + exact ⟨t, ⟨u, ⟨z, hreturn, hvu.head ⟨y, hE, hyv⟩⟩, hut⟩, htb⟩ + · exact ⟨y, ⟨z, ⟨z, hreturn, .refl⟩, hE⟩, ih⟩ + · obtain ⟨t, ⟨u, ⟨v, hyv, hvu⟩, hut⟩, htb⟩ := ih + exact htb.head ⟨z, hreturn, Or.inr ⟨u, ⟨v, ⟨y, hE, hyv⟩, hvu⟩, hut⟩⟩ + · exact ih.head ⟨z, hreturn, Or.inl hE⟩ + constructor + · rintro ⟨⟨side, x⟩, hfirst, hrest⟩ + have h := hbound hrest rfl + cases side + · obtain ⟨t, ⟨u, ⟨v, hxv, hvu⟩, hut⟩, htq⟩ := h + exact ⟨t, ⟨u, hvu.head ⟨x, hfirst, hxv⟩, hut⟩, htq⟩ + · exact ⟨x, ⟨p, .refl, hfirst⟩, h⟩ + · intro hpq + have hS {x y : Fin m} (hxy : (x, y) ∈ S) : + ((false, x), (false, y)) ∈ relStar (E ○ B) := by + exact hxy.lift (fun a => (false, a)) (fun _ _ ⟨z, hxz, hzy⟩ => + ⟨(false, z), hxz, rfl, hzy⟩) + have hV {x y : Fin m} (hxy : (x, y) ∈ V) : + ((true, x), (true, y)) ∈ relStar (B ○ E) := by + apply hxy.lift' (fun a => (true, a)) + rintro a b ⟨z, haz, hzb⟩ + rcases hzb with hright | ⟨u, ⟨v, ⟨w, hzw, hwv⟩, hvu⟩, hub⟩ + · exact Relation.ReflTransGen.single ⟨(true, z), ⟨rfl, haz⟩, hright⟩ + · have hmid : ((false, w), (false, u)) ∈ B ○ relStar (E ○ B) := + ⟨(false, v), ⟨rfl, hwv⟩, hS hvu⟩ + rw [comp_relStar_rotate] at hmid + obtain ⟨c, hwc, hcu⟩ := hmid + have htail : ((false, w), (true, b)) ∈ relStar (B ○ E) := + hwc.tail ⟨(false, u), hcu, hub⟩ + have hstart : ((true, a), (false, w)) ∈ B ○ E := + ⟨(true, z), ⟨rfl, haz⟩, hzw⟩ + exact htail.head hstart + obtain ⟨b, ⟨a, hpa, hab⟩, hbq⟩ := hpq + have h : ((false, p), (true, q)) ∈ relStar (E ○ B) ○ E ○ relStar (B ○ E) := + ⟨(true, b), ⟨(false, a), hS hpa, hab⟩, hV hbq⟩ + rwa [feedback_insert] at h + +@[simp] +theorem identity_comp {m : ℕ} (a : Diagram m) : comp (identity m) a = a := by + cases a + simp [comp, identity] + +@[simp] +theorem comp_identity {m : ℕ} (a : Diagram m) : comp a (identity m) = a := by + cases a + simp [comp, identity] + +theorem reflect_comp {m : ℕ} (a b : Diagram m) : + reflect (comp a b) = comp (reflect b) (reflect a) := by + apply Diagram.ext + · rfl + · rfl + · simpa only [reflect, comp, SetRel.comp_assoc] using + congrArg (fun R => b.right ∪ (b.backward ○ R ○ b.forward)) + (comp_relStar_rotate a.right b.left) + · simpa only [reflect, comp, SetRel.comp_assoc] using + congrArg (fun R => a.left ∪ (a.forward ○ R ○ a.backward)) + (comp_relStar_rotate b.left a.right).symm + +theorem feedback_reflect_map {m : ℕ} (e : Diagram m) (R L : SetRel (Fin m) (Fin m)) + {s t : Bool} {p q : Fin m} (h : ((s, p), (t, q)) ∈ e.feedback R L) : + ((!s, p), (!t, q)) ∈ (reflect e).feedback L R := by + have hE {a b : Bool × Fin m} (hab : (a, b) ∈ e.portRelation) : + ((!a.1, a.2), (!b.1, b.2)) ∈ (reflect e).portRelation := by + obtain ⟨sa, a⟩ := a + obtain ⟨sb, b⟩ := b + cases sa <;> cases sb <;> exact hab + have hB {a b : Bool × Fin m} (hab : (a, b) ∈ bounceRelation R L) : + ((!a.1, a.2), (!b.1, b.2)) ∈ bounceRelation L R := by + obtain ⟨sa, x⟩ := a + obtain ⟨sb, y⟩ := b + obtain ⟨hside, hreturn⟩ := hab + refine ⟨congrArg Bool.not hside, ?_⟩ + cases sa <;> exact hreturn + obtain ⟨a, hfirst, hrest⟩ := h + refine ⟨(!a.1, a.2), hE hfirst, ?_⟩ + exact hrest.lift (fun a => (!a.1, a.2)) + (p := fun a b => (a, b) ∈ bounceRelation L R ○ (reflect e).portRelation) + (fun _ _ ⟨b, hab, hbc⟩ => ⟨(!b.1, b.2), hB hab, hE hbc⟩) + +theorem mem_feedback_reflect {m : ℕ} (e : Diagram m) (R L : SetRel (Fin m) (Fin m)) + {s t : Bool} {p q : Fin m} : + ((s, p), (t, q)) ∈ (reflect e).feedback L R ↔ + ((!s, p), (!t, q)) ∈ e.feedback R L := by + constructor + · exact feedback_reflect_map (reflect e) L R + · intro h + simpa only [Bool.not_not] using feedback_reflect_map e R L h + +theorem comp_assoc_forward {m : ℕ} (a b c : Diagram m) : + (comp (comp a b) c).forward = (comp a (comp b c)).forward := by + simpa only [comp, relComp_union, union_relComp, SetRel.comp_assoc] using + congrArg (fun R => a.forward ○ R ○ c.forward) + (relStar_block_forward (b.left ○ a.right) b.forward + (c.left ○ b.backward ○ a.right) (c.left ○ b.right)).symm + +theorem comp_assoc_left {m : ℕ} (a b c : Diagram m) : + (comp (comp a b) c).left = (comp a (comp b c)).left := by + simpa only [comp, relComp_union, union_relComp, SetRel.comp_assoc, Set.union_assoc] using + congrArg (fun R => a.left ∪ (a.forward ○ R ○ a.backward)) + (relStar_block_exit b.left a.right b.forward (c.left ○ b.backward) + (c.left ○ b.right)).symm + +/-- The product of boundary diagrams is associative. -/ +theorem comp_assoc {m : ℕ} (a b c : Diagram m) : + comp (comp a b) c = comp a (comp b c) := by + apply Diagram.ext + · exact comp_assoc_forward a b c + · have h := comp_assoc_forward (reflect c) (reflect b) (reflect a) + simp only [← reflect_comp] at h + exact h.symm + · exact comp_assoc_left a b c + · have h := comp_assoc_left (reflect c) (reflect b) (reflect a) + simp only [← reflect_comp] at h + exact h.symm + +theorem context_forward_iff {m : ℕ} (u e v : Diagram m) {p q : Fin m} : + (p, q) ∈ (comp (comp u e) v).forward ↔ + ∃ a b, (p, a) ∈ u.forward ∧ + ((false, a), (true, b)) ∈ e.feedback u.right v.left ∧ (b, q) ∈ v.forward := by + simp only [feedback_forward, comp, SetRel.comp_assoc, SetRel.mem_comp] + aesop + +theorem context_through_iff {m : ℕ} (u e v : Diagram m) (positive : Bool) {p q : Fin m} : + (p, q) ∈ (comp (comp u e) v).through positive ↔ + ∃ a b, (p, a) ∈ (if positive then u.forward else v.backward) ∧ + ((!positive, a), (positive, b)) ∈ e.feedback u.right v.left ∧ + (b, q) ∈ (if positive then v.forward else u.backward) := by + cases positive + · have h := context_forward_iff (reflect v) (reflect e) (reflect u) (p := p) (q := q) + have hd : comp (comp (reflect v) (reflect e)) (reflect u) = + reflect (comp (comp u e) v) := by + rw [comp_assoc, ← reflect_comp, ← reflect_comp] + rw [hd] at h + simp only [mem_feedback_reflect] at h + exact h + · exact context_forward_iff u e v + +theorem context_surviving {m : ℕ} (d u e v : Diagram m) (positive : Bool) : + d.surviving (comp (comp u e) v) positive = + contextEntry d u v positive ○ e.feedback u.right v.left ○ contextExit d u v positive := by + ext ⟨p, q⟩ + constructor + · rintro ⟨b, ⟨a, hpa, hab⟩, hbq⟩ + obtain ⟨c, f, hac, hcf, hfb⟩ := (context_through_iff u e v positive).mp hab + exact ⟨(positive, f), ⟨(!positive, c), ⟨rfl, a, hpa, hac⟩, hcf⟩, + ⟨rfl, b, hfb, hbq⟩⟩ + · rintro ⟨⟨t, b⟩, ⟨⟨s, a⟩, ⟨hs, c, hpc, hca⟩, hab⟩, ⟨ht, f, hbf, hfq⟩⟩ + change s = !positive at hs + change t = positive at ht + subst s + subst t + exact ⟨f, ⟨c, hpc, (context_through_iff u e v positive).mpr ⟨a, b, hca, hab, hbf⟩⟩, hfq⟩ + +instance {m : ℕ} : Monoid (Diagram m) where + mul := comp + one := identity m + mul_assoc := comp_assoc + one_mul := identity_comp + mul_one := comp_identity + +@[instance] +theorem finite_diagram {m : ℕ} : Finite (Diagram m) := + Finite.of_injective (fun a : Diagram m => (a.forward, a.backward, a.left, a.right)) (by + intro a b h + exact Diagram.ext (congrArg (·.1) h) (congrArg (·.2.1) h) + (congrArg (·.2.2.1) h) (congrArg (·.2.2.2) h)) + +end Diagram + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/ExplicitFamily.lean b/lean/OAI/Combinatorics/TwoWayAutomata/ExplicitFamily.lean new file mode 100644 index 0000000000000000000000000000000000000000..89d9abada59ed8333aac1e8132bc39db08afcd2c --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/ExplicitFamily.lean @@ -0,0 +1,54 @@ +import OAI.Combinatorics.TwoWayAutomata.DeterministicBound + +namespace OAI + +namespace TwoWayComplementation + +/-- An explicit liveness witness and its complementation lower bound. -/ +theorem explicit_complementation_family (n : ℕ) (hn : 4 ≤ n) : + ∃ A : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) (Fin n), + A.language = sourceLanguage (Fin (n - 2)) ∧ + (∀ (Q : Type*) [Fintype Q], + ∀ B : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) Q, + B.language = A.languageᶜ → + (1 / 2 : ℝ) * 2 ^ ((n - 4) / 127) - 1 ≤ (Fintype.card Q : ℝ)) := by + obtain ⟨A, hA⟩ := sourceLanguage_state_bound (Fin (n - 2)) + have hsize : Fintype.card (Fin (n - 2)) + 2 = n := by simp; omega + let A' := A.reindex (finCongr hsize) + have hAlang : A'.language = sourceLanguage (Fin (n - 2)) := by + rw [TwoNFA.reindex_language, hA] + refine ⟨A', hAlang, ?_⟩ + intro Q _ B hB + let B' := B.reindex (Fintype.equivFin Q) + obtain ⟨τ, l, r, a, b, hzero, hmul, hrecognition⟩ := B'.diagram_representation + have hbound := diagram_recognition_lower_bound (n - 2) (Fintype.card Q + 1) + (by omega) (by omega) τ hzero hmul l r a b (by + intro w + rw [← hrecognition w, TwoNFA.reindex_language, hB, hAlang] + rfl) + have hnat : 2 ^ ((n - 4) / 127) ≤ 2 * (Fintype.card Q + 1) := by + simpa only [lossBound, Nat.sub_sub, Nat.reduceAdd] using hbound + have hreal : (2 : ℝ) ^ ((n - 4) / 127) ≤ 2 * ((Fintype.card Q : ℝ) + 1) := by + exact_mod_cast hnat + linarith + +/-- Both lower bounds hold for the same n-state liveness witness. -/ +theorem explicit_family_main (n : ℕ) (hn : 4 ≤ n) : + ∃ A : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) (Fin n), + A.language = sourceLanguage (Fin (n - 2)) ∧ + (∀ (Q : Type*) [Fintype Q], + ∀ B : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) Q, + B.language = A.languageᶜ → + (1 / 2 : ℝ) * 2 ^ ((n - 4) / 127) - 1 ≤ (Fintype.card Q : ℝ)) ∧ + (131 ≤ n → ∀ (Q : Type*) [Fintype Q], + ∀ D : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) Q, + D.Deterministic → D.language = A.language → + (1 / 2 : ℝ) * 2 ^ ((n - 4) / 127) ≤ (Fintype.card Q : ℝ)) := by + obtain ⟨A, hA, hcomp⟩ := explicit_complementation_family n hn + refine ⟨A, hA, hcomp, ?_⟩ + intro hn Q _ D hdet hD + exact deterministic_liveness_lower_bound n hn Q D hdet (hD.trans hA) + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/ImageBound.lean b/lean/OAI/Combinatorics/TwoWayAutomata/ImageBound.lean new file mode 100644 index 0000000000000000000000000000000000000000..260225588cbb4e6a38ab0e7802de791d7f2db5b5 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/ImageBound.lean @@ -0,0 +1,282 @@ +import OAI.Combinatorics.TwoWayAutomata.Retained + +namespace OAI + +namespace TwoWayComplementation + +universe u v + +open scoped SetRel + +/-- Exponential cost of a single edge for every finite source monoid and label type. -/ +theorem single_edge_cost (h : ℕ) : + ∀ {A : Type u} {H : Type v} [Monoid A] [Finite A] [Finite H] {m : ℕ}, + Nat.card H = h → 2 ≤ h → + ∀ (ι : A →ₙ* Diagram m) (φ : A →* RelationMonoid H), Function.Surjective φ → + (∀ a b, (ι a, ι b) ∈ Diagram.inclusion m → (φ b).toRel ⊆ (φ a).toRel) → + (∀ σ : Equiv.Perm H, ∃ p : Aˣ, φ p = (permutationUnit σ).val) → + ∀ a, IsIdempotentElem a → ∀ x y : H, x ≠ y → + (φ a).toRel = SetRel.id ∪ {(x, y)} → + lossBound h ≤ ((ι 1).missing (ι a)).ncard := by + classical + induction h using Nat.strong_induction_on with + | h h ih => + intro A H _ _ _ m hH hh ι φ hφ hreverse hlift a ha x y hxy himage + by_cases hsmall : h ≤ 128 + · simpa only [hH] using single_edge_base ι φ hreverse a x y hxy himage (by omega) + have hlarge : 129 ≤ h := by omega + obtain ⟨u, v, z, huv, huz, hvz⟩ := exists_hub_families (H := H) (by omega) + choose s hs hsimage hscost using fun i => + pair_conjugate ι φ hlift a ha x y hxy himage (u i) z (huz i) + choose s' hs' hsimage' hscost' using fun j => + pair_conjugate ι φ hlift a ha x y hxy himage z (v j) (hvz j).symm + let f : Bool × Fin 64 → A := fun p => if p.1 then s' p.2 else s p.2 + let c := ((ι 1).missing (ι a)).ncard + have hcost (p : Bool × Fin 64) : ((ι 1).missing (ι (f p))).ncard ≤ 2 * c := by + obtain ⟨b, i⟩ := p + cases b + · exact hscost i + · exact hscost' i + let T : Set H := {w | w ≠ z} + let P0 := restrictedIdentity T + have hP0 : IsIdempotentElem (RelationMonoid.ofRel P0) := + RelationMonoid.ext (restrictedIdentity_comp_self T) + obtain ⟨b0, hb0, hb0image, _⟩ := minimal_idempotent_lift φ.toMulHom hφ + (RelationMonoid.ofRel P0) hP0 + have hφb0 : (φ b0).toRel = P0 := congrArg RelationMonoid.toRel hb0image + obtain ⟨J0, _, hJcard, hJ⟩ := common_pair_budget ι f b0 hb0 hcost + let index : Fin 4096 ≃ Fin 64 × Fin 64 := (finProdFinEquiv (m := 64) (n := 64)).symm + let edge : Fin 4096 → H × H := fun r => (u (index r).1, v (index r).2) + have hedgeinj : Function.Injective edge := by + intro r s h + apply index.injective + exact Prod.ext (u.injective (congrArg Prod.fst h)) (v.injective (congrArg Prod.snd h)) + let E (j : ℕ) : SetRel H H := edge '' {r : Fin 4096 | r.val < j} + let P (j : ℕ) := P0 ∪ E j + let U := Set.range u + let V := Set.range v + have hUV : Disjoint U V := by + apply Set.disjoint_left.mpr + rintro w ⟨i, rfl⟩ ⟨j, heq⟩ + exact huv i j heq.symm + have hUT : U ⊆ T := by rintro w ⟨i, rfl⟩; exact huz i + have hVT : V ⊆ T := by rintro w ⟨j, rfl⟩; exact hvz j + have hE (j : ℕ) : E j ⊆ U ×ˢ V := by + rintro _ ⟨r, _, rfl⟩ + exact ⟨⟨(index r).1, rfl⟩, ⟨(index r).2, rfl⟩⟩ + have he (r : Fin 4096) : {edge r} ⊆ U ×ˢ V := by + rintro w rfl + exact ⟨⟨(index r).1, rfl⟩, ⟨(index r).2, rfl⟩⟩ + have hnew (r : Fin 4096) : edge r ∉ E r.val := by + rintro ⟨s, hs, heq⟩ + change s.val < r.val at hs + exact (hedgeinj heq ▸ hs).false + have hEsucc (r : Fin 4096) : E (r.val + 1) = E r.val ∪ {edge r} := by + have hidx : {s : Fin 4096 | s.val < r.val + 1} = + {s : Fin 4096 | s.val < r.val} ∪ {r} := by + ext s + simp only [Set.mem_ofPred_eq, Set.mem_union, Set.mem_singleton_iff] + constructor + · intro hs + by_cases hlt : s.val < r.val + · exact Or.inl hlt + · exact Or.inr (Fin.ext (by omega)) + · rintro (hlt | rfl) <;> omega + change edge '' _ = _ + rw [hidx, Set.image_union, Set.image_singleton] + have hPidemp (j : ℕ) : P j ○ P j = P j := by + simpa only [P, Set.union_self] using + bipartite_relation_comp T U V hUT hVT hUV (E j) (E j) (hE j) (hE j) + let g (j : ℕ) : A := if hj : j < 4096 then + b0 * s (index ⟨j, hj⟩).1 * s' (index ⟨j, hj⟩).2 * b0 else b0 + have hgimage (r : Fin 4096) : (φ (g r.val)).toRel = P0 ∪ {edge r} := by + simp only [g, dite_eq_left r.isLt, map_mul, RelationMonoid.toRel_mul, + hφb0, hsimage, hsimage'] + exact hub_pair_relation _ _ z (huz _) (hvz _) + have hgbudget : ∀ j < 4096, + ι (g j) ∈ (ι b0).corner ∧ (ι b0).missing (ι (g j)) ⊆ J0 := by + intro j hj + simp only [g, dite_eq_left hj] + constructor + · constructor + · change ι b0 * ι _ = _ + rw [← map_mul, ← mul_assoc, ← mul_assoc, ← mul_assoc, hb0.eq] + · change ι _ * ι b0 = _ + rw [← map_mul, mul_assoc, hb0.eq] + · exact hJ _ _ + obtain ⟨b, hbzero, hbidemp, hbpower, _, _, htotal⟩ := + exists_nested_chain ι b0 hb0 g J0 hgbudget + have hbimage : ∀ j ≤ 4096, (φ (b j)).toRel = P j := by + intro j + induction j with + | zero => intro _; simp [hbzero, hφb0, P, E] + | succ j hj => + intro hjle + let r : Fin 4096 := ⟨j, by omega⟩ + have hprev := hj (by omega) + have hnext : φ (b j * g j * b j) = RelationMonoid.ofRel (P (j + 1)) := by + apply RelationMonoid.ext + rw [map_mul, map_mul, RelationMonoid.toRel_mul, RelationMonoid.toRel_mul, + hprev, hgimage r, RelationMonoid.toRel_ofRel] + change (P0 ∪ E j) ○ (P0 ∪ {edge r}) ○ (P0 ∪ E j) = P0 ∪ E (j + 1) + rw [bipartite_relation_comp T U V hUT hVT hUV _ _ (hE j) (he r)] + rw [bipartite_relation_comp T U V hUT hVT hUV _ _ + (Set.union_subset (hE j) (he r)) (hE j)] + rw [show E (j + 1) = E j ∪ {edge r} from hEsucc r] + rw [Set.union_right_comm (E j) {edge r} (E j), Set.union_self] + obtain ⟨n, hn⟩ := hbpower j + rw [hn, map_pow, hnext] + have hi : IsIdempotentElem (RelationMonoid.ofRel (P (j + 1))) := + RelationMonoid.ext (hPidemp (j + 1)) + rw [hi.pow_succ_eq, RelationMonoid.toRel_ofRel] + let δ (r : Fin 4096) := ((ι (b r.val)).missing (ι (b (r.val + 1)))).ncard + have hsum : ∑ r, δ r ≤ 1024 * c := by + have heq : ∑ r, δ r = + ∑ j ∈ Finset.range 4096, ((ι (b j)).missing (ι (b (j + 1)))).ncard := by + exact Fin.sum_univ_eq_sum_range + (fun j => ((ι (b j)).missing (ι (b (j + 1)))).ncard) 4096 + rw [heq] + omega + apply amplification_arithmetic hlarge δ hsum + intro r + obtain ⟨F, hFcard, hxF, hyF, hzF, hUF, hVF⟩ := retained_pair_set U V + (by simpa [U] using Set.ncard_range_of_injective u.injective) + (by simpa [V] using Set.ncard_range_of_injective v.injective) hUV z + (edge r).1 (edge r).2 + (by rintro ⟨i, hi⟩; exact huz i hi) + (by rintro ⟨j, hj⟩; exact hvz j hj) + ⟨(index r).1, rfl⟩ ⟨(index r).2, rfl⟩ + have hrestrict : restrictedIdentity F ○ (φ (b r.val)).toRel ○ restrictedIdentity F = + restrictedIdentity F := by + rw [hbimage _ r.isLt.le] + exact retained_relation_identity U V F z _ _ hzF hUF hVF (E r.val) (hE r.val) (hnew r) + have hrestrict' : restrictedIdentity F ○ (φ (b (r.val + 1))).toRel ○ + restrictedIdentity F = restrictedIdentity F ∪ {edge r} := by + rw [hbimage _ r.isLt, show P (r.val + 1) = P r.val ∪ {edge r} from + (congrArg (P0 ∪ ·) (hEsucc r)).trans (Set.union_assoc _ _ _).symm] + have hp : restrictedIdentity F ○ P r.val ○ restrictedIdentity F = restrictedIdentity F := by + rwa [hbimage _ r.isLt.le] at hrestrict + exact retained_relation_pair (P r.val) F hp _ _ hxF hyF + obtain ⟨n, hn⟩ := hbpower r.val + have habsorb := sandwich_power_absorption (b r.val) (g r.val) (hbidemp r.val) n + rw [← hn] at habsorb + obtain ⟨d, hd, ψ, hψ, hliftψ, hψrel, w, hw, hwimage, hwcost⟩ := + smaller_relation_instance ι φ hφ (b r.val) (b (r.val + 1)) (hbidemp r.val) + habsorb.1 habsorb.2 F hrestrict ⟨(edge r).1, hxF⟩ ⟨(edge r).2, hyF⟩ hrestrict' + let ιb : (hbidemp r.val).Corner →ₙ* Diagram m := ι.comp (MulMemClass.subtype _) + let ιd : hd.Corner →ₙ* Diagram m := ιb.comp (MulMemClass.subtype _) + have hreverseψ : ∀ s t, (ιd s, ιd t) ∈ Diagram.inclusion m → + (ψ t).toRel ⊆ (ψ s).toRel := by + intro s t hst + rw [hψrel, hψrel] + exact fun _ hp => hreverse s.val.val t.val.val hst hp + have hlow := ih (h - 127) (by omega) (A := hd.Corner) (H := F) + (by omega) (by omega) ιd ψ hψ hreverseψ hliftψ w hw + ⟨(edge r).1, hxF⟩ ⟨(edge r).2, hyF⟩ + (fun heq => huv (index r).1 (index r).2 (congrArg Subtype.val heq)) hwimage + exact hlow.trans hwcost + +/-- A surjection has a unital identity corner with unit lifts for every target unit. -/ +theorem minimal_unital_corner {A M : Type*} [Semigroup A] [Finite A] [Monoid M] + (θ : A →ₙ* M) (hθ : Function.Surjective θ) : + ∃ d : A, ∃ hd : IsIdempotentElem d, ∃ ψ : hd.Corner →* M, + Function.Surjective ψ ∧ (∀ w, ψ w = θ w.val) ∧ + ∀ u : Mˣ, ∃ p : hd.Cornerˣ, ψ p = u.val := by + have h1 : IsIdempotentElem (1 : M) := by simp [IsIdempotentElem] + obtain ⟨d, hd, hθd, hminimal⟩ := minimal_idempotent_lift θ hθ 1 h1 + let ε : h1.Corner ≃* M := + { toFun := Subtype.val + invFun := fun x => ⟨x, (Subsemigroup.mem_corner_iff h1).mpr ⟨one_mul x, mul_one x⟩⟩ + left_inv := fun _ => rfl + right_inv := fun _ => rfl + map_mul' := fun _ _ => rfl } + let γ := cornerMap θ hd h1 hθd + let ψ := ε.toMonoidHom.comp γ + have hψ : Function.Surjective ψ := + ε.surjective.comp (cornerMap_surjective θ hθ hd h1 hθd) + refine ⟨d, hd, ψ, hψ, fun _ => rfl, ?_⟩ + intro u + obtain ⟨w, hw⟩ := hψ u.val + have hu : IsUnit (ψ w) := hw.symm ▸ u.isUnit + have hγ : IsUnit (γ w) := by + have hu' := hu.map ε.symm.toMonoidHom + simpa only [ψ, MonoidHom.comp_apply, MulEquiv.coe_toMonoidHom, + MulEquiv.symm_apply_apply] using hu' + obtain ⟨p, hp⟩ := minimal_corner_reflects_units θ hd h1 hθd hminimal w hγ + exact ⟨p, hp ▸ hw⟩ + +/-- Identity plus a single pair is always an idempotent relation. -/ +theorem identity_pair_idempotent {H : Type*} (x y : H) : + IsIdempotentElem (RelationMonoid.ofRel (SetRel.id ∪ {(x, y)})) := by + apply RelationMonoid.ext + change (SetRel.id ∪ {(x, y)}) ○ (SetRel.id ∪ {(x, y)}) = SetRel.id ∪ {(x, y)} + ext ⟨r, s⟩ + simp only [SetRel.comp, Set.mem_ofPred_eq, Set.mem_union, Set.mem_singleton_iff, + SetRel.id, Prod.mk.injEq] + aesop + +/-- A surjective order-reversing multiplicative image has exponential cost. -/ +theorem order_reversing_image_bound_finite {H : Type*} [Finite H] + (m : ℕ) (_hm : 1 ≤ m) (hh : 2 ≤ Nat.card H) + (S : Set (Diagram m)) (hidentity : Diagram.identity m ∈ S) + (hclosed : ∀ ⦃a b⦄, a ∈ S → b ∈ S → Diagram.comp a b ∈ S) + (φ : S → SetRel H H) (hsurj : Function.Surjective φ) + (hmul : ∀ a b : S, + φ ⟨Diagram.comp a.val b.val, hclosed a.property b.property⟩ = φ a ○ φ b) + (hreverse : ∀ a b : S, (a.val, b.val) ∈ Diagram.inclusion m → φ b ⊆ φ a) : + lossBound (Nat.card H) ≤ 2 * m := by + classical + let S0 : Submonoid (Diagram m) := + { carrier := S + one_mem' := hidentity + mul_mem' := by + intro a b ha hb + change Diagram.comp a b ∈ S + exact hclosed ha hb } + let θ : S0 →ₙ* RelationMonoid H := + { toFun := fun x => .ofRel (φ ⟨x.val, x.property⟩) + map_mul' := fun a b => RelationMonoid.ext (hmul ⟨a.val, a.property⟩ ⟨b.val, b.property⟩) } + have hθ : Function.Surjective θ := by + intro R + obtain ⟨x, hx⟩ := hsurj R.toRel + exact ⟨⟨x.val, x.property⟩, RelationMonoid.ext hx⟩ + obtain ⟨e, he, ψ, hψ, hψval, hunit⟩ := minimal_unital_corner θ hθ + let ι : he.Corner →ₙ* Diagram m := + { toFun := fun w => w.val.val + map_mul' := fun _ _ => rfl } + have hrev : ∀ a b, (ι a, ι b) ∈ Diagram.inclusion m → (ψ b).toRel ⊆ (ψ a).toRel := by + intro a b hab + rw [hψval, hψval] + exact hreverse ⟨a.val.val, a.val.property⟩ ⟨b.val.val, b.val.property⟩ hab + let := Fintype.ofFinite H + obtain ⟨pair⟩ : Nonempty (Fin 2 ↪ H) := + Function.Embedding.nonempty_of_card_le (by simpa using hh) + let x := pair 0 + let y := pair 1 + have hxy : x ≠ y := fun heq => by + have := congrArg Fin.val (pair.injective heq) + norm_num at this + obtain ⟨a, ha, haimage, _⟩ := minimal_idempotent_lift ψ.toMulHom hψ + (RelationMonoid.ofRel (SetRel.id ∪ {(x, y)})) (identity_pair_idempotent x y) + have hcost := single_edge_cost (Nat.card H) rfl hh ι ψ hψ hrev + (fun σ => hunit (permutationUnit σ)) a ha x y hxy + (congrArg RelationMonoid.toRel haimage) + exact hcost.trans ((Set.ncard_le_ncard (show (ι 1).missing (ι a) ⊆ (ι 1).classes from + fun _ h => h.1)).trans (Diagram.class_count (ι 1))) + +/-- The order-reversing image bound for a finite label domain. -/ +theorem order_reversing_image_bound + (m h : ℕ) (_hm : 1 ≤ m) (hh : 2 ≤ h) + (S : Set (Diagram m)) (hidentity : Diagram.identity m ∈ S) + (hclosed : ∀ ⦃a b⦄, a ∈ S → b ∈ S → Diagram.comp a b ∈ S) + (φ : S → SetRel (Fin h) (Fin h)) (hsurj : Function.Surjective φ) + (hmul : ∀ a b : S, + φ ⟨Diagram.comp a.val b.val, hclosed a.property b.property⟩ = φ a ○ φ b) + (hreverse : ∀ a b : S, (a.val, b.val) ∈ Diagram.inclusion m → φ b ⊆ φ a) : + lossBound h ≤ 2 * m := by + simpa only [Nat.card_fin] using order_reversing_image_bound_finite (H := Fin h) m _hm + (by simpa using hh) S hidentity hclosed φ hsurj hmul hreverse + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Main.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Main.lean new file mode 100644 index 0000000000000000000000000000000000000000..39e982dc1acba2a325614b46a90922a4d59c2643 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Main.lean @@ -0,0 +1,139 @@ +import OAI.Combinatorics.TwoWayAutomata.Recognition +import OAI.Combinatorics.TwoWayAutomata.Representation + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- An n-state source language requires exponentially many states for any 2NFA complement. -/ +theorem complementation_lower_bound (n : ℕ) (hn : 4 ≤ n) : + ∃ A : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) (Fin n), + ∀ s : ℕ, ∀ B : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) (Fin s), + B.language = A.languageᶜ → 2 ^ ((n - 4) / 127) ≤ 2 * (s + 1) := by + obtain ⟨A, hA⟩ := sourceLanguage_state_bound (Fin (n - 2)) + have hsize : Fintype.card (Fin (n - 2)) + 2 = n := by simp; omega + let A' := A.reindex (finCongr hsize) + refine ⟨A', ?_⟩ + intro s B hB + have hAlang : A'.language = sourceLanguage (Fin (n - 2)) := by + rw [TwoNFA.reindex_language, hA] + obtain ⟨τ, l, r, a, b, hzero, hmul, hrecognition⟩ := B.diagram_representation + have hbound := diagram_recognition_lower_bound (n - 2) (s + 1) (by omega) (by omega) + τ hzero hmul l r a b (by + intro w + rw [← hrecognition w, hB, hAlang] + rfl) + simpa only [lossBound, Nat.sub_sub, Nat.reduceAdd] using hbound + +/-- The real-valued lower bound for arbitrary finite target state sets. -/ +theorem complementation_lower_bound_finite (n : ℕ) (hn : 4 ≤ n) : + ∃ A : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) (Fin n), + ∀ (Q : Type*) [Fintype Q], ∀ B : TwoNFA (SetRel (Fin (n - 2)) (Fin (n - 2))) Q, + B.language = A.languageᶜ → + (1 / 2 : ℝ) * 2 ^ ((n - 4) / 127) - 1 ≤ (Fintype.card Q : ℝ) := by + obtain ⟨A, hA⟩ := complementation_lower_bound n hn + refine ⟨A, ?_⟩ + intro Q _ B hB + have hbound := hA (Fintype.card Q) (B.reindex (Fintype.equivFin Q)) (by + rw [TwoNFA.reindex_language, hB]) + have hreal : (2 : ℝ) ^ ((n - 4) / 127) ≤ 2 * ((Fintype.card Q : ℝ) + 1) := by + exact_mod_cast hbound + linarith + +/-- Letter maps commute with tape lookup and preserve the two endmarkers. -/ +theorem tapeSymbol_map {Sigma Gamma : Type*} (f : Sigma → Gamma) (w : List Sigma) + (p : Fin (w.length + 2)) : + tapeSymbol (w.map f) ⟨p.val, by simp⟩ = + Sum.map id f (tapeSymbol w p) := by + simp only [tapeSymbol, List.length_map, List.getElem_map] + split_ifs <;> rfl + +/-- Pull an automaton back along a letter map, preserving its state set. -/ +def TwoNFA.pullAlphabet {Sigma Gamma Q : Type*} (A : TwoNFA Gamma Q) (f : Sigma → Gamma) : + TwoNFA Sigma Q where + initial := A.initial + accepting := A.accepting + transition q symbol := A.transition q (Sum.map id f symbol) + +/-- Letter pullback accepts exactly the words whose images the original machine accepts. -/ +theorem TwoNFA.pullAlphabet_language {Sigma Gamma Q : Type*} (A : TwoNFA Gamma Q) + (f : Sigma → Gamma) (w : List Sigma) : + w ∈ (A.pullAlphabet f).language ↔ w.map f ∈ A.language := by + let e : (Q × Fin (w.length + 2)) ≃ (Q × Fin ((w.map f).length + 2)) := + Equiv.prodCongr (Equiv.refl Q) (finCongr (by simp)) + have hstep (c d : Q × Fin (w.length + 2)) : + (e c, e d) ∈ A.step (w.map f) ↔ (c, d) ∈ (A.pullAlphabet f).step w := by + change (∃ move : Fin 3, + (d.1, move) ∈ A.transition c.1 + (tapeSymbol (w.map f) ⟨c.2.val, by simp⟩) ∧ + (d.2.val : ℤ) = (c.2.val : ℤ) + (move.val : ℤ) - 1) ↔ _ + rw [tapeSymbol_map] + rfl + constructor + · rintro ⟨c, hc, hrun⟩ + refine ⟨e c, hc, ?_⟩ + exact (relStar_equiv_iff e _ _ hstep (A.initial, ⟨0, by omega⟩) c).mpr hrun + · rintro ⟨d, hd, hrun⟩ + refine ⟨e.symm d, hd, ?_⟩ + apply (relStar_equiv_iff e _ _ hstep (A.initial, ⟨0, by omega⟩) (e.symm d)).mp + rw [Equiv.apply_symm_apply] + exact hrun + +/-- No real polynomial bounds the powers of two at all natural inputs. -/ +theorem polynomial_not_bound_two_pow (p : Polynomial ℝ) : + ¬ ∀ n : ℕ, (2 : ℝ) ^ n ≤ p.eval (n : ℝ) := by + have hlittle (q : Polynomial ℝ) : + Asymptotics.IsLittleO Filter.atTop (fun n : ℕ => q.eval (n : ℝ)) + (fun n : ℕ => (2 : ℝ) ^ n) := by + induction q using Polynomial.induction_on' with + | add q r hq hr => simpa only [Polynomial.eval_add] using hq.add hr + | monomial k a => + simpa only [Polynomial.eval_monomial] using + (isLittleO_pow_const_const_pow_of_one_lt (R := ℝ) k + (by norm_num : (1 : ℝ) < 2)).const_mul_left a + intro hbound + obtain ⟨n, hn⟩ := (hlittle p |>.bound (by norm_num : (0 : ℝ) < 1 / 2)).exists + have hpos : 0 < (2 : ℝ) ^ n := pow_pos (by norm_num) n + have habs := le_abs_self (p.eval (n : ℝ)) + have hle := hbound n + simp only [Real.norm_eq_abs, abs_of_pos hpos] at hn + linarith + +/-- There is no polynomial state bound for complementation over all finite alphabets. -/ +theorem no_polynomial_complementation : + ¬ ∃ p : Polynomial ℝ, ∀ n k : ℕ, ∀ A : TwoNFA (Fin k) (Fin n), + ∃ s : ℕ, ∃ B : TwoNFA (Fin k) (Fin s), + B.language = A.languageᶜ ∧ (s : ℝ) ≤ p.eval (n : ℝ) := by + classical + rintro ⟨p, hp⟩ + let q : Polynomial ℝ := Polynomial.C 2 * + (p.comp (Polynomial.C 127 * Polynomial.X + Polynomial.C 4) + Polynomial.C 1) + apply polynomial_not_bound_two_pow q + intro t + let n := 127 * t + 4 + obtain ⟨A, hA⟩ := complementation_lower_bound n (by dsimp [n]; omega) + let Sigma := SetRel (Fin (n - 2)) (Fin (n - 2)) + let e : Sigma ≃ Fin (Fintype.card Sigma) := Fintype.equivFin Sigma + obtain ⟨s, B, hB, hs⟩ := hp n (Fintype.card Sigma) (A.pullAlphabet e.symm) + have hcomplement : (B.pullAlphabet e).language = A.languageᶜ := by + ext w + rw [B.pullAlphabet_language, hB] + change ¬ w.map e ∈ (A.pullAlphabet e.symm).language ↔ ¬ w ∈ A.language + rw [A.pullAlphabet_language] + simp only [List.map_map, Equiv.symm_comp_self, List.map_id] + have hbound := hA s (B.pullAlphabet e) hcomplement + have hexponent : (n - 4) / 127 = t := by dsimp [n]; omega + rw [hexponent] at hbound + have hreal : (2 : ℝ) ^ t ≤ 2 * ((s : ℝ) + 1) := by exact_mod_cast hbound + change (2 : ℝ) ^ t ≤ q.eval (t : ℝ) + simp only [q, Polynomial.eval_mul, Polynomial.eval_add, Polynomial.eval_comp, + Polynomial.eval_C, Polynomial.eval_X] + have hn : (n : ℝ) = 127 * (t : ℝ) + 4 := by simp [n] + rw [hn] at hs + linarith + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Model.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Model.lean new file mode 100644 index 0000000000000000000000000000000000000000..e18f184b843e40c4840c4397f50adaa7ffbea5de --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Model.lean @@ -0,0 +1,47 @@ +import Mathlib + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Reflexive transitive closure, including the empty path. -/ +def relStar {α : Type*} (R : SetRel α α) : SetRel α α := + {(x, y) | Relation.ReflTransGen (fun a b => (a, b) ∈ R) x y} + +/-- A read-only 2NFA. The two endmarkers are `Sum.inl 0` and `Sum.inl 1`. + Move codes 0, 1, 2 mean left, stay, right. -/ +structure TwoNFA (Sigma Q : Type*) where + /-- The state in which the machine starts on the left endmarker. -/ + initial : Q + /-- States whose occurrence on a finite run constitutes acceptance. -/ + accepting : Set Q + /-- Allowed next-state and move pairs, determined by the state and scanned symbol. -/ + transition : Q → (Fin 2 ⊕ Sigma) → Set (Q × Fin 3) + +/-- Tape lookup includes two distinct endmarkers, even on empty input. -/ +def tapeSymbol {Sigma : Type*} (w : List Sigma) (p : Fin (w.length + 2)) : Fin 2 ⊕ Sigma := + if hleft : p.val = 0 then .inl 0 + else if hright : p.val = w.length + 1 then .inl 1 + else .inr (w[p.val - 1]'(by omega)) + +namespace TwoNFA + +/-- One legal step: both head positions stay within the marked input. -/ +def step {Sigma Q : Type*} (A : TwoNFA Sigma Q) (w : List Sigma) : + SetRel (Q × Fin (w.length + 2)) (Q × Fin (w.length + 2)) := + {(c, d) | ∃ move : Fin 3, + (d.1, move) ∈ A.transition c.1 (tapeSymbol w c.2) ∧ + (d.2.val : ℤ) = (c.2.val : ℤ) + (move.val : ℤ) - 1} + +/-- Existential finite-run acceptance, allowing a run of length zero. -/ +def language {Sigma Q : Type*} (A : TwoNFA Sigma Q) : Set (List Sigma) := + {w | ∃ c, c.1 ∈ A.accepting ∧ + ((A.initial, ⟨0, by omega⟩), c) ∈ relStar (A.step w)} + +end TwoNFA + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Nesting.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Nesting.lean new file mode 100644 index 0000000000000000000000000000000000000000..3dca5e6af4c06d369f87123ee2d2f767bc63269c --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Nesting.lean @@ -0,0 +1,352 @@ +import OAI.Combinatorics.TwoWayAutomata.Transport + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +namespace Diagram + +theorem loopClosure_subset_of_corner {m : ℕ} (e d : Diagram m) (hd : d ∈ e.corner) + (positive : Bool) : e.loopClosure positive ⊆ d.loopClosure positive := by + obtain ⟨hL, hR⟩ := corner_returns e d hd + cases positive + · exact relStar_mono (SetRel.comp_subset_comp hR hL) + · exact relStar_mono (SetRel.comp_subset_comp hL hR) + +/-- Two finite-path rearrangements for absorption under nesting. -/ +theorem nested_forward_loop_absorption {m : ℕ} (e d : Diagram m) (hd : d ∈ e.corner) : + e.forward ○ d.loopClosure true ○ d.forward ⊆ d.loopClosure true ○ d.forward ∧ + d.forward ○ d.loopClosure true ○ e.forward ⊆ d.forward ○ d.loopClosure true := by + let S := relStar (d.left ○ e.right) + let T := relStar (e.left ○ d.right) + obtain ⟨hL, hR⟩ := corner_returns e d hd + have hS : S ⊆ d.loopClosure true := relStar_mono (SetRel.comp_subset_comp_right hR) + have hT : T ⊆ d.loopClosure true := relStar_mono (SetRel.comp_subset_comp_left hL) + have hF : e.forward ○ S ○ d.forward = d.forward := congrArg forward hd.1 + have hF' : d.forward ○ T ○ e.forward = d.forward := congrArg forward hd.2 + have hLd : e.left ∪ (e.forward ○ S ○ d.left ○ e.backward) = d.left := + congrArg left hd.1 + have hRd : e.right ∪ (e.backward ○ d.right ○ T ○ e.forward) = d.right := + congrArg right hd.2 + have hLpart : e.forward ○ S ○ d.left ○ e.backward ⊆ d.left := + Set.subset_union_right.trans hLd.le + have hRpart : e.backward ○ d.right ○ T ○ e.forward ⊆ d.right := + Set.subset_union_right.trans hRd.le + have hKleft : d.loopClosure true = + S ○ relStar (d.left ○ e.backward ○ d.right ○ T ○ e.forward ○ S) := by + change relStar (d.left ○ d.right) = _ + conv_lhs => rw [← hRd] + simp only [S, relComp_union, relStar_union, SetRel.comp_assoc] + have hKright : d.loopClosure true = + T ○ relStar (e.forward ○ S ○ d.left ○ e.backward ○ d.right ○ T) := by + change relStar (d.left ○ d.right) = _ + conv_lhs => rw [← hLd] + simp only [T, union_relComp, relStar_union, SetRel.comp_assoc] + have hleft : e.forward ○ S ○ d.left ○ e.backward ○ d.right ○ T ⊆ + d.loopClosure true := by + calc + _ ⊆ d.left ○ d.right ○ T := by gcongr + _ ⊆ d.loopClosure true := by + rintro ⟨x, y⟩ ⟨z, hxz, hzy⟩ + exact (hT hzy).head hxz + have hright : S ○ d.left ○ e.backward ○ d.right ○ T ○ e.forward ⊆ + d.loopClosure true := by + calc + _ = S ○ d.left ○ (e.backward ○ d.right ○ T ○ e.forward) := by + simp only [SetRel.comp_assoc] + _ ⊆ S ○ d.left ○ d.right := by gcongr + _ ⊆ d.loopClosure true := by + rintro ⟨x, y⟩ ⟨z, ⟨p, hxp, hpz⟩, hzy⟩ + exact (hS hxp).tail ⟨z, hpz, hzy⟩ + have hleftStar : relStar (e.forward ○ S ○ d.left ○ e.backward ○ d.right ○ T) ⊆ + d.loopClosure true := fun _ h => + Relation.reflTransGen_closed (fun _ _ hab => hleft hab) _ _ h + have hrightStar : relStar (S ○ d.left ○ e.backward ○ d.right ○ T ○ e.forward) ⊆ + d.loopClosure true := fun _ h => + Relation.reflTransGen_closed (fun _ _ hab => hright hab) _ _ h + constructor + · calc + _ = relStar (e.forward ○ S ○ d.left ○ e.backward ○ d.right ○ T) ○ + (e.forward ○ S ○ d.forward) := by + rw [hKleft] + simpa only [SetRel.comp_assoc] using + congrArg (· ○ d.forward) + (comp_relStar_rotate (e.forward ○ S) (d.left ○ e.backward ○ d.right ○ T)) + _ ⊆ d.loopClosure true ○ d.forward := by rw [hF]; gcongr + · calc + _ = (d.forward ○ T ○ e.forward) ○ + relStar (S ○ d.left ○ e.backward ○ d.right ○ T ○ e.forward) := by + rw [hKright] + simpa only [SetRel.comp_assoc] using + congrArg (d.forward ○ T ○ ·) + (comp_relStar_rotate e.forward (S ○ d.left ○ e.backward ○ d.right ○ T)).symm + _ ⊆ d.forward ○ d.loopClosure true := by rw [hF']; gcongr + +theorem nested_through_loop_absorption {m : ℕ} (e d : Diagram m) (hd : d ∈ e.corner) + (positive : Bool) : + e.through positive ○ d.loopClosure positive ○ d.through positive ⊆ + d.loopClosure positive ○ d.through positive ∧ + d.through positive ○ d.loopClosure positive ○ e.through positive ⊆ + d.through positive ○ d.loopClosure positive := by + cases positive + · have hreflect : reflect d ∈ (reflect e).corner := + ⟨by rw [← reflect_comp, hd.2], by rw [← reflect_comp, hd.1]⟩ + exact nested_forward_loop_absorption (reflect e) (reflect d) hreflect + · exact nested_forward_loop_absorption e d hd + +theorem nested_through_absorption {m : ℕ} (e d : Diagram m) (hd : d ∈ e.corner) + (positive : Bool) : + e.through positive ○ d.recurrence positive ⊆ d.recurrence positive ∧ + d.recurrence positive ○ e.through positive ⊆ d.recurrence positive := by + obtain ⟨hl, hr⟩ := nested_through_loop_absorption e d hd positive + constructor + · simpa only [recurrence, SetRel.comp_assoc] using + SetRel.comp_subset_comp_left (S := d.loopClosure positive) hl + · simpa only [recurrence, SetRel.comp_assoc] using + SetRel.comp_subset_comp_right (R := d.loopClosure positive) hr + +/-- Mixed recurrent relations are absorbed under nesting. -/ +theorem nested_recurrence_absorption {m : ℕ} (e d : Diagram m) (hd : d ∈ e.corner) + (positive : Bool) : + e.recurrence positive ○ d.recurrence positive ⊆ d.recurrence positive ∧ + d.recurrence positive ○ e.recurrence positive ⊆ d.recurrence positive := by + have hK := loopClosure_subset_of_corner e d hd positive + obtain ⟨hl, hr⟩ := nested_through_absorption e d hd positive + have hKP : d.loopClosure positive ○ d.recurrence positive = d.recurrence positive := by + simp only [recurrence, SetRel.comp_assoc, loopClosure_comp] + have hPK : d.recurrence positive ○ d.loopClosure positive = d.recurrence positive := by + simp only [recurrence, SetRel.comp_assoc, loopClosure_comp_self] + constructor + · calc + _ ⊆ d.loopClosure positive ○ e.through positive ○ d.loopClosure positive ○ + d.recurrence positive := by unfold recurrence; gcongr + _ ⊆ d.recurrence positive := by + rw [SetRel.comp_assoc, SetRel.comp_assoc, hKP] + exact (SetRel.comp_subset_comp_right hl).trans hKP.le + · calc + _ ⊆ d.recurrence positive ○ d.loopClosure positive ○ e.through positive ○ + d.loopClosure positive := by unfold recurrence; simp only [SetRel.comp_assoc]; gcongr + _ ⊆ d.recurrence positive := by + rw [hPK] + exact (SetRel.comp_subset_comp_left hr).trans hPK.le + +theorem corner_forward_factor {m : ℕ} (e d : Diagram m) (he : comp e e = e) + (hd : d ∈ e.corner) {u v : Fin m} (huv : (u, v) ∈ d.forward) : + ∃ p q, (p, p) ∈ e.recurrence true ∧ (q, q) ∈ e.recurrence true ∧ + (u, p) ∈ e.forward ○ e.loopClosure true ∧ (p, q) ∈ e.surviving d true ∧ + (q, v) ∈ e.loopClosure true ○ e.forward := by + let S := relStar (d.left ○ e.right) + let T := relStar (e.left ○ d.right) + have hF : e.forward ○ S ○ d.forward = d.forward := congrArg forward hd.1 + have hF' : d.forward ○ T ○ e.forward = d.forward := congrArg forward hd.2 + have hsplit : e.forward ○ S ○ d.forward ○ T ○ e.forward = d.forward := by rw [hF, hF'] + have hw : (u, v) ∈ e.forward ○ S ○ d.forward ○ T ○ e.forward := by rw [hsplit]; exact huv + obtain ⟨a, ⟨b, ⟨c, ⟨f, huf, hfc⟩, hcb⟩, hba⟩, hav⟩ := hw + obtain ⟨p, hpp, ⟨hup, hpf⟩⟩ := rectangle_cover e he (positive := true) huf + obtain ⟨q, hqq, ⟨haq, hqv⟩⟩ := rectangle_cover e he (positive := true) hav + refine ⟨p, q, hpp, hqq, hup, ?_, hqv⟩ + obtain ⟨r, hpr, hrf⟩ := hpf + obtain ⟨s, has, hsq⟩ := haq + change (p, q) ∈ e.loopClosure true ○ d.forward ○ e.loopClosure true + rw [← hsplit] + exact ⟨s, ⟨r, hpr, ⟨a, ⟨b, ⟨c, ⟨f, hrf, hfc⟩, hcb⟩, hba⟩, has⟩⟩, hsq⟩ + +theorem corner_through_factor {m : ℕ} (e d : Diagram m) (he : comp e e = e) + (hd : d ∈ e.corner) {positive : Bool} {u v : Fin m} + (huv : (u, v) ∈ d.through positive) : + ∃ p q, (p, p) ∈ e.recurrence positive ∧ (q, q) ∈ e.recurrence positive ∧ + (u, p) ∈ e.through positive ○ e.loopClosure positive ∧ + (p, q) ∈ e.surviving d positive ∧ + (q, v) ∈ e.loopClosure positive ○ e.through positive := by + cases positive + · exact corner_forward_factor (reflect e) (reflect d) + (by rw [← reflect_comp, he]) + ⟨by rw [← reflect_comp, hd.2], by rw [← reflect_comp, hd.1]⟩ huv + · exact corner_forward_factor e d he hd huv + +/-- A recurrent loop in a nested corner contains two recurrent points of its parent. -/ +theorem nested_recurrent_witness {m : ℕ} (e d : Diagram m) (he : comp e e = e) + (hd : d ∈ e.corner) {positive : Bool} {x : Fin m} + (hxx : (x, x) ∈ d.recurrence positive) : + ∃ p q, (p, p) ∈ e.recurrence positive ∧ (q, q) ∈ e.recurrence positive ∧ + (x, p) ∈ d.recurrence positive ∧ (p, x) ∈ d.recurrence positive ∧ + (x, q) ∈ d.recurrence positive ∧ (q, x) ∈ d.recurrence positive ∧ + (p, q) ∈ e.surviving d positive := by + obtain ⟨v, ⟨u, hxu, huv⟩, hvx⟩ := hxx + obtain ⟨p, q, hpp, hqq, ⟨a, hua, hap⟩, hpq, ⟨b, hqb, hbv⟩⟩ := + corner_through_factor e d he hd huv + have hK := loopClosure_subset_of_corner e d hd positive + obtain ⟨hFP, hPF⟩ := nested_through_absorption e d hd positive + have hKP : d.loopClosure positive ○ d.recurrence positive ⊆ d.recurrence positive := by + simp only [recurrence, SetRel.comp_assoc, loopClosure_comp, Set.Subset.rfl] + have hPK : d.recurrence positive ○ d.loopClosure positive ⊆ d.recurrence positive := by + simp only [recurrence, SetRel.comp_assoc, loopClosure_comp_self, Set.Subset.rfl] + have hpqd : (p, q) ∈ d.recurrence positive := + (show e.surviving d positive ⊆ d.recurrence positive by + unfold surviving recurrence; gcongr) hpq + have hxpd : (x, p) ∈ d.recurrence positive := + hPK ⟨a, hPF ⟨u, hPK ⟨x, ⟨v, ⟨u, hxu, huv⟩, hvx⟩, hxu⟩, hua⟩, hK hap⟩ + have hpxd : (p, x) ∈ d.recurrence positive := + hPK ⟨v, hPF ⟨b, hPK ⟨q, hpqd, hK hqb⟩, hbv⟩, hvx⟩ + have hxqd : (x, q) ∈ d.recurrence positive := + hKP ⟨u, hxu, hFP ⟨a, hua, hKP ⟨p, hK hap, hpqd⟩⟩⟩ + have hqxd : (q, x) ∈ d.recurrence positive := + hKP ⟨b, hK hqb, hFP ⟨v, hbv, hKP ⟨x, hvx, ⟨v, ⟨u, hxu, huv⟩, hvx⟩⟩⟩⟩ + exact ⟨p, q, hpp, hqq, hxpd, hpxd, hxqd, hqxd, hpq⟩ + +/-- A new class contains a surviving old class or two distinct old classes. -/ +theorem nested_class_containment {m : ℕ} (e d : Diagram m) + (he : comp e e = e) (hd : comp d d = d) (hde : d ∈ e.corner) + {c : Bool × Set (Fin m)} (hc : c ∈ d.classes) : + (∃ C ∈ e.classes, C.1 = c.1 ∧ C.2 ⊆ c.2 ∧ C ∉ e.missing d) ∨ + ∃ C ∈ e.classes, ∃ D ∈ e.classes, + C ≠ D ∧ C.1 = c.1 ∧ D.1 = c.1 ∧ C.2 ⊆ c.2 ∧ D.2 ⊆ c.2 := by + classical + obtain ⟨x, hx⟩ := class_nonempty hc + have hxx := (class_related d hd hc hx hx).1 + obtain ⟨p, q, hpp, hqq, hxp, hpx, hxq, hqx, hpq⟩ := + nested_recurrent_witness e d he hde hxx + let C : Bool × Set (Fin m) := + (c.1, {y | (p, y) ∈ e.recurrence c.1 ∧ (y, p) ∈ e.recurrence c.1}) + let D : Bool × Set (Fin m) := + (c.1, {y | (q, y) ∈ e.recurrence c.1 ∧ (y, q) ∈ e.recurrence c.1}) + have hC : C ∈ e.classes := ⟨p, hpp, rfl⟩ + have hD : D ∈ e.classes := ⟨q, hqq, rfl⟩ + obtain ⟨hPePd, hPdPe⟩ := nested_recurrence_absorption e d hde c.1 + have hCsub : C.2 ⊆ c.2 := by + intro r hr + rw [class_eq_of_mem d hd hc hx] + exact ⟨hPdPe ⟨p, hxp, hr.1⟩, hPePd ⟨p, hr.2, hpx⟩⟩ + have hDsub : D.2 ⊆ c.2 := by + intro r hr + rw [class_eq_of_mem d hd hc hx] + exact ⟨hPdPe ⟨q, hxq, hr.1⟩, hPePd ⟨q, hr.2, hqx⟩⟩ + by_cases hCD : C = D + · left + refine ⟨C, hC, rfl, hCsub, ?_⟩ + have hqC : q ∈ C.2 := by rw [hCD]; exact ⟨hqq, hqq⟩ + apply (not_mem_missing_iff e d he hde hC ⟨hpp, hpp⟩).mpr + exact (surviving_absorption e d hde c.1).2 ⟨q, hpq, hqC.2⟩ + · exact Or.inr ⟨C, hC, D, hD, hCD, rfl, rfl, hCsub, hDsub⟩ + +theorem nested_unprotected_loss {m : ℕ} (e d : Diagram m) + (he : comp e e = e) (hd : comp d d = d) (hde : d ∈ e.corner) + (P : Set (Bool × Fin m)) (hmissing : e.missing d ⊆ e.unprotected P) : + 2 * (d.unprotected P).ncard + (e.missing d).ncard ≤ 2 * (e.unprotected P).ncard := by + classical + rw [Set.ncard_eq_toFinset_card, Set.ncard_eq_toFinset_card, Set.ncard_eq_toFinset_card] + apply weighted_class_bound _ _ _ + (fun c C : Bool × Set (Fin m) => C.1 = c.1 ∧ C.2 ⊆ c.2) + · simpa only [Set.Finite.toFinset_subset_toFinset] using hmissing + · intro c hc d' hd' C hC hCc hCd + obtain ⟨hc, _⟩ := (Set.Finite.mem_toFinset _).mp hc + obtain ⟨hd', _⟩ := (Set.Finite.mem_toFinset _).mp hd' + obtain ⟨hC, _⟩ := (Set.Finite.mem_toFinset _).mp hC + obtain ⟨x, hx⟩ := class_nonempty hC + exact class_eq_of_common d hd hc hd' (hCc.1.symm.trans hCd.1) (hCc.2 hx) (hCd.2 hx) + · intro c hc + obtain ⟨hc, hprotected⟩ := (Set.Finite.mem_toFinset _).mp hc + have hcontained {C : Bool × Set (Fin m)} (hC : C ∈ e.classes) + (hsign : C.1 = c.1) (hsub : C.2 ⊆ c.2) : C ∈ e.unprotected P := by + refine ⟨hC, ?_⟩ + intro x hx + rw [hsign] + exact hprotected x (hsub hx) + rcases nested_class_containment e d he hd hde hc with + ⟨C, hC, hsign, hsub, hnC⟩ | ⟨C, hC, D, hD, hCD, hsignC, hsignD, hsubC, hsubD⟩ + · exact Or.inl ⟨C, (Set.Finite.mem_toFinset _).mpr (hcontained hC hsign hsub), + by simpa only [Set.Finite.mem_toFinset] using hnC, hsign, hsub⟩ + · exact Or.inr ⟨C, (Set.Finite.mem_toFinset _).mpr (hcontained hC hsignC hsubC), + D, (Set.Finite.mem_toFinset _).mpr (hcontained hD hsignD hsubD), + hCD, ⟨hsignC, hsubC⟩, ⟨hsignD, hsubD⟩⟩ + +/-- A common initial missing set pays for all later losses. -/ +theorem nested_chain_bound {m k : ℕ} (b : ℕ → Diagram m) + (hidemp : ∀ j ≤ k, comp (b j) (b j) = b j) + (hnest : ∀ j < k, (b (j + 1), b j) ∈ nesting m) + (J : Set (Bool × Set (Fin m))) + (hJ : ∀ j ≤ k, (b 0).missing (b j) ⊆ J) : + ∑ j ∈ Finset.range k, ((b j).missing (b (j + 1))).ncard ≤ 2 * J.ncard := by + classical + have hbase : ∀ j ≤ k, (b j, b 0) ∈ nesting m := by + intro j + induction j with + | zero => intro _; exact nesting_refl (hidemp 0 (Nat.zero_le _)) + | succ j ih => + intro hj + exact nesting_trans (hnest j (by omega)) (ih (by omega)) + have hchoose : ∀ C : ↥((b 0).classes \ J), ∃ x, x ∈ C.val.2 := + fun C => class_nonempty C.property.1 + choose pick hpick using hchoose + let P : Set (Bool × Fin m) := Set.range (fun C : ↥((b 0).classes \ J) => (C.val.1, pick C)) + have hinit : (b 0).unprotected P = J ∩ (b 0).classes := by + ext C + constructor + · rintro ⟨hC, hCP⟩ + refine ⟨?_, hC⟩ + by_contra hnC + let C' : ↥((b 0).classes \ J) := ⟨C, hC, hnC⟩ + exact hCP (pick C') (hpick C') ⟨C', rfl⟩ + · rintro ⟨hCJ, hC⟩ + refine ⟨hC, ?_⟩ + intro x hx hxP + obtain ⟨D, hD⟩ := hxP + have hsign : C.1 = D.val.1 := (congrArg (fun sx : Bool × Fin m => sx.1) hD).symm + have hlabel : pick D = x := congrArg (fun sx : Bool × Fin m => sx.2) hD + have heq : C = D.val := class_eq_of_common (b 0) (hidemp 0 (Nat.zero_le _)) + hC D.property.1 hsign hx (hlabel ▸ hpick D) + exact D.property.2 (heq ▸ hCJ) + have hprotected (j : ℕ) (hj : j ≤ k) (sx : Bool × Fin m) (hxP : sx ∈ P) : + (sx.2, sx.2) ∈ (b 0).surviving (b j) sx.1 ∧ + (sx.2, sx.2) ∈ (b j).recurrence sx.1 := by + obtain ⟨C, rfl⟩ := hxP + have hcorner : b j ∈ (b 0).corner := ⟨(hbase j hj).2.2.2, (hbase j hj).2.2.1⟩ + have hnotmissing : C.val ∉ (b 0).missing (b j) := fun h => C.property.2 (hJ j hj h) + have hloop := (not_mem_missing_iff (b 0) (b j) (hidemp 0 (Nat.zero_le _)) + hcorner C.property.1 (hpick C)).mp hnotmissing + have hK := loopClosure_subset_of_corner (b 0) (b j) hcorner C.val.1 + refine ⟨hloop, ?_⟩ + exact (show (b 0).surviving (b j) C.val.1 ⊆ (b j).recurrence C.val.1 by + unfold surviving recurrence; gcongr) hloop + have hstep (j : ℕ) (hj : j < k) : + 2 * ((b (j + 1)).unprotected P).ncard + ((b j).missing (b (j + 1))).ncard ≤ + 2 * ((b j).unprotected P).ncard := by + have hjk : j ≤ k := hj.le + have hj1k : j + 1 ≤ k := hj + obtain ⟨_, _, hnextprev, hprevnext⟩ := hnest j hj + apply nested_unprotected_loss (b j) (b (j + 1)) (hidemp j hjk) + (hidemp (j + 1) hj1k) ⟨hprevnext, hnextprev⟩ P + intro C hC + refine ⟨hC.1, ?_⟩ + intro x hx hxP + have hloop := (hprotected (j + 1) hj1k (C.1, x) hxP).1 + have hK := loopClosure_subset_of_corner (b 0) (b j) + ⟨(hbase j hjk).2.2.2, (hbase j hjk).2.2.1⟩ C.1 + apply hC.2 x hx + exact (show (b 0).surviving (b (j + 1)) C.1 ⊆ (b j).surviving (b (j + 1)) C.1 by + unfold surviving; gcongr) hloop + have hbudget : ∀ n ≤ k, + (∑ j ∈ Finset.range n, ((b j).missing (b (j + 1))).ncard) + + 2 * ((b n).unprotected P).ncard ≤ 2 * ((b 0).unprotected P).ncard := by + intro n + induction n with + | zero => simp + | succ n ih => + intro hn + have hi := ih (by omega) + have hs := hstep n (by omega) + rw [Finset.sum_range_succ] + omega + have hi : ((b 0).unprotected P).ncard ≤ J.ncard := by + rw [hinit] + exact Set.ncard_le_ncard Set.inter_subset_left + have hb := hbudget k le_rfl + omega + +end Diagram + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Network.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Network.lean new file mode 100644 index 0000000000000000000000000000000000000000..97a7393f6404937765debeaee53eddaae811e5d2 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Network.lean @@ -0,0 +1,189 @@ +import OAI.Combinatorics.TwoWayAutomata.Diagram + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Single-cell diagram from stay, rightward, and leftward augmented transitions. -/ +def cellDiagram (m : ℕ) (stay right left : SetRel (Fin m) (Fin m)) : Diagram m := + ⟨relStar stay ○ right, relStar stay ○ left, relStar stay ○ left, relStar stay ○ right⟩ + +/-- Multiply the single-letter diagrams in input order, using identity for the empty word. -/ +def wordDiagram {Sigma : Type*} {m : ℕ} (d : Sigma → Diagram m) (w : List Sigma) : Diagram m := + (w.map d).prod + +@[simp] +theorem wordDiagram_nil {Sigma : Type*} {m : ℕ} (d : Sigma → Diagram m) : + wordDiagram d [] = Diagram.identity m := rfl + +theorem wordDiagram_append {Sigma : Type*} {m : ℕ} (d : Sigma → Diagram m) + (u v : List Sigma) : + wordDiagram d (u ++ v) = Diagram.comp (wordDiagram d u) (wordDiagram d v) := by + change ((u ++ v).map d).prod = (u.map d).prod * (v.map d).prod + rw [List.map_append, List.prod_append] + +/-- Swap the two vertex blocks without changing the finite-path semantics. -/ +theorem blockRelation_star_swap {α β : Type*} (E : SetRel α α) (F : SetRel α β) + (G : SetRel β α) (H : SetRel β β) (x y : α ⊕ β) : + (x.swap, y.swap) ∈ relStar (blockRelation H G F E) ↔ + (x, y) ∈ relStar (blockRelation E F G H) := by + have hs {a b : α ⊕ β} : + (a.swap, b.swap) ∈ blockRelation H G F E ↔ (a, b) ∈ blockRelation E F G H := by + cases a <;> cases b <;> rfl + constructor + · intro h + have h' := h.lift Sum.swap (p := fun a b => (a, b) ∈ blockRelation E F G H) + (fun a b hab => by cases a <;> cases b <;> exact hab) + change Relation.ReflTransGen (fun a b => (a, b) ∈ blockRelation E F G H) x y + simpa only [Function.onFun, Sum.swap_swap] using h' + · exact fun h => h.lift Sum.swap (fun _ _ hab => hs.mpr hab) + +/-- The left-left version of block elimination. -/ +theorem blockRelation_star_left {α β : Type*} (E : SetRel α α) (F : SetRel α β) + (G : SetRel β α) (H : SetRel β β) (x y : α) : + (Sum.inl x, Sum.inl y) ∈ relStar (blockRelation E F G H) ↔ + (x, y) ∈ relStar (E ∪ (F ○ relStar H ○ G)) := by + rw [← blockRelation_star_swap] + exact blockRelation_star_right H G F E x y + +/-- The right-left version of block elimination. -/ +theorem blockRelation_star_back {α β : Type*} (E : SetRel α α) (F : SetRel α β) + (G : SetRel β α) (H : SetRel β β) (x : β) (y : α) : + (Sum.inr x, Sum.inl y) ∈ relStar (blockRelation E F G H) ↔ + (x, y) ∈ relStar H ○ G ○ relStar (E ∪ (F ○ relStar H ○ G)) := by + rw [← blockRelation_star_swap] + exact blockRelation_star_cross H G F E x y + +/-- Eliminate an internal graph while retaining arbitrary repeated feedback at its entry. -/ +theorem relStar_entry_feedback {Q V : Type*} (I : SetRel Q V) (E : SetRel V V) + (O R : SetRel V Q) (X : SetRel Q Q) : + I ○ relStar (E ∪ (R ○ X ○ I)) ○ O = + relStar (I ○ relStar E ○ R ○ X) ○ (I ○ relStar E ○ O) := by + have hrotate := comp_relStar_rotate (I ○ relStar E) (R ○ X) + simpa only [relStar_union, SetRel.comp_assoc] using congrArg (· ○ O) hrotate + +/-- Internal paths either avoid the feedback edge or use it after a first internal segment. -/ +theorem relStar_general_feedback {Q V : Type*} (I J : SetRel Q V) (E : SetRel V V) + (O R : SetRel V Q) (X : SetRel Q Q) : + I ○ relStar (E ∪ (R ○ X ○ J)) ○ O = + (I ○ relStar E ○ O) ∪ + (I ○ relStar E ○ R ○ relStar (X ○ J ○ relStar E ○ R) ○ X ○ J ○ relStar E ○ O) := by + have hu := relStar_comp_unfold (R : SetRel V Q) (X ○ J ○ relStar E) + have hexpand := congrArg (fun K => I ○ relStar E ○ K ○ O) hu + simpa only [relStar_union, relComp_union, union_relComp, SetRel.comp_id, + SetRel.comp_assoc] using hexpand + +/-- A finite-path network with labelled entrances and exits at both boundaries. -/ +structure Network (m : ℕ) (V : Type*) where + /-- Internal transitions. -/ + edge : SetRel V V + /-- Left boundary entrance. -/ + leftIn : SetRel (Fin m) V + /-- Right boundary entrance. -/ + rightIn : SetRel (Fin m) V + /-- Left boundary exit. -/ + leftOut : SetRel V (Fin m) + /-- Right boundary exit. -/ + rightOut : SetRel V (Fin m) + +namespace Network + +/-- Summarize all finite internal paths between the four boundary pairs. -/ +def summary {m : ℕ} {V : Type*} (N : Network m V) : Diagram m where + forward := N.leftIn ○ relStar N.edge ○ N.rightOut + backward := N.rightIn ○ relStar N.edge ○ N.leftOut + left := N.leftIn ○ relStar N.edge ○ N.leftOut + right := N.rightIn ○ relStar N.edge ○ N.rightOut + +/-- Join two networks, wiring the adjacent exits to entrances in both directions. -/ +def join {m : ℕ} {V W : Type*} (N : Network m V) (M : Network m W) : Network m (V ⊕ W) where + edge := blockRelation N.edge (N.rightOut ○ M.leftIn) (M.leftOut ○ N.rightIn) M.edge + leftIn := {xy | ∃ v, xy.2 = Sum.inl v ∧ (xy.1, v) ∈ N.leftIn} + rightIn := {xy | ∃ w, xy.2 = Sum.inr w ∧ (xy.1, w) ∈ M.rightIn} + leftOut := {xy | ∃ v, xy.1 = Sum.inl v ∧ (v, xy.2) ∈ N.leftOut} + rightOut := {xy | ∃ w, xy.1 = Sum.inr w ∧ (w, xy.2) ∈ M.rightOut} + +/-- Joining path networks implements exactly the four diagram multiplication formulas. -/ +theorem summary_join {m : ℕ} {V W : Type*} (N : Network m V) (M : Network m W) : + (N.join M).summary = Diagram.comp N.summary M.summary := by + have hF : (N.join M).summary.forward = + N.leftIn ○ (relStar N.edge ○ (N.rightOut ○ M.leftIn) ○ + relStar (M.edge ∪ ((M.leftOut ○ N.rightIn) ○ relStar N.edge ○ + (N.rightOut ○ M.leftIn)))) ○ M.rightOut := by + ext ⟨x, y⟩ + constructor + · rintro ⟨v, ⟨u, ⟨a, rfl, hxa⟩, huv⟩, ⟨b, rfl, hby⟩⟩ + exact ⟨b, ⟨a, hxa, (blockRelation_star_cross _ _ _ _ a b).mp huv⟩, hby⟩ + · rintro ⟨b, hab, hby⟩ + obtain ⟨a, hxa, hab⟩ := hab + exact ⟨Sum.inr b, ⟨Sum.inl a, ⟨a, rfl, hxa⟩, + (blockRelation_star_cross _ _ _ _ a b).mpr hab⟩, ⟨b, rfl, hby⟩⟩ + have hB : (N.join M).summary.backward = + M.rightIn ○ (relStar M.edge ○ (M.leftOut ○ N.rightIn) ○ + relStar (N.edge ∪ ((N.rightOut ○ M.leftIn) ○ relStar M.edge ○ + (M.leftOut ○ N.rightIn)))) ○ N.leftOut := by + ext ⟨x, y⟩ + constructor + · rintro ⟨v, ⟨u, ⟨a, rfl, hxa⟩, huv⟩, ⟨b, rfl, hby⟩⟩ + exact ⟨b, ⟨a, hxa, (blockRelation_star_back _ _ _ _ a b).mp huv⟩, hby⟩ + · rintro ⟨b, hab, hby⟩ + obtain ⟨a, hxa, hab⟩ := hab + exact ⟨Sum.inl b, ⟨Sum.inr a, ⟨a, rfl, hxa⟩, + (blockRelation_star_back _ _ _ _ a b).mpr hab⟩, ⟨b, rfl, hby⟩⟩ + have hL : (N.join M).summary.left = + N.leftIn ○ relStar (N.edge ∪ ((N.rightOut ○ M.leftIn) ○ relStar M.edge ○ + (M.leftOut ○ N.rightIn))) ○ N.leftOut := by + ext ⟨x, y⟩ + constructor + · rintro ⟨v, ⟨u, ⟨a, rfl, hxa⟩, huv⟩, ⟨b, rfl, hby⟩⟩ + exact ⟨b, ⟨a, hxa, (blockRelation_star_left _ _ _ _ a b).mp huv⟩, hby⟩ + · rintro ⟨b, ⟨a, hxa, hab⟩, hby⟩ + exact ⟨Sum.inl b, ⟨Sum.inl a, ⟨a, rfl, hxa⟩, + (blockRelation_star_left _ _ _ _ a b).mpr hab⟩, ⟨b, rfl, hby⟩⟩ + have hR : (N.join M).summary.right = + M.rightIn ○ relStar (M.edge ∪ ((M.leftOut ○ N.rightIn) ○ relStar N.edge ○ + (N.rightOut ○ M.leftIn))) ○ M.rightOut := by + ext ⟨x, y⟩ + constructor + · rintro ⟨v, ⟨u, ⟨a, rfl, hxa⟩, huv⟩, ⟨b, rfl, hby⟩⟩ + exact ⟨b, ⟨a, hxa, (blockRelation_star_right _ _ _ _ a b).mp huv⟩, hby⟩ + · rintro ⟨b, ⟨a, hxa, hab⟩, hby⟩ + exact ⟨Sum.inr b, ⟨Sum.inr a, ⟨a, rfl, hxa⟩, + (blockRelation_star_right _ _ _ _ a b).mpr hab⟩, ⟨b, rfl, hby⟩⟩ + apply Diagram.ext + · rw [hF] + have hf := relStar_entry_feedback M.leftIn M.edge M.rightOut M.leftOut + (N.rightIn ○ relStar N.edge ○ N.rightOut) + simpa only [summary, Diagram.comp, SetRel.comp_assoc] using + congrArg ((N.leftIn ○ relStar N.edge ○ N.rightOut) ○ ·) hf + · rw [hB] + have hb := relStar_entry_feedback N.rightIn N.edge N.leftOut N.rightOut + (M.leftIn ○ relStar M.edge ○ M.leftOut) + simpa only [summary, Diagram.comp, SetRel.comp_assoc] using + congrArg ((M.rightIn ○ relStar M.edge ○ M.leftOut) ○ ·) hb + · rw [hL] + simpa only [summary, Diagram.comp, SetRel.comp_assoc] using + relStar_general_feedback N.leftIn N.rightIn N.edge N.leftOut N.rightOut + (M.leftIn ○ relStar M.edge ○ M.leftOut) + · rw [hR] + have hr := relStar_general_feedback M.rightIn M.leftIn M.edge M.rightOut M.leftOut + (N.rightIn ○ relStar N.edge ○ N.rightOut) + have hrot := comp_relStar_rotate (N.rightIn ○ relStar N.edge ○ N.rightOut) + (M.leftIn ○ relStar M.edge ○ M.leftOut) + calc + _ = M.summary.right ∪ (M.summary.backward ○ + relStar (N.summary.right ○ M.summary.left) ○ N.summary.right ○ + M.summary.forward) := by + simpa only [summary, SetRel.comp_assoc] using hr + _ = _ := by + simpa only [summary, Diagram.comp, SetRel.comp_assoc] using + congrArg (fun K => M.summary.right ∪ (M.summary.backward ○ K ○ + M.summary.forward)) hrot.symm + +end Network + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Normalization.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Normalization.lean new file mode 100644 index 0000000000000000000000000000000000000000..8ee7c0bdbf24b544e5ee15df74355cbd6b187d9a --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Normalization.lean @@ -0,0 +1,207 @@ +import OAI.Combinatorics.TwoWayAutomata.Model + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Only the first tape position carries the left endmarker. -/ +theorem tapeSymbol_left_iff {Sigma : Type*} (w : List Sigma) (p : Fin (w.length + 2)) : + tapeSymbol w p = Sum.inl 0 ↔ p.val = 0 := by + unfold tapeSymbol + split_ifs <;> simp_all + +/-- Only the last original tape position carries the right endmarker. -/ +theorem tapeSymbol_right_iff {Sigma : Type*} (w : List Sigma) (p : Fin (w.length + 2)) : + tapeSymbol w p = Sum.inl 1 ↔ p.val = w.length + 1 := by + unfold tapeSymbol + split_ifs <;> simp_all + +namespace TwoNFA + +/-- Rename states along an equivalence, retaining all transitions and moves. -/ +def reindex {Sigma Q Q' : Type*} (A : TwoNFA Sigma Q) (e : Q ≃ Q') : TwoNFA Sigma Q' where + initial := e A.initial + accepting := e.symm ⁻¹' A.accepting + transition q s := {target | (e.symm target.1, target.2) ∈ A.transition (e.symm q) s} + +theorem reindex_language {Sigma Q Q' : Type*} (A : TwoNFA Sigma Q) (e : Q ≃ Q') : + (A.reindex e).language = A.language := by + ext w + constructor + · rintro ⟨⟨q, p⟩, haccept, hrun⟩ + refine ⟨(e.symm q, p), haccept, ?_⟩ + have h := hrun.lift (fun c : Q' × Fin (w.length + 2) => (e.symm c.1, c.2)) + (p := fun c d => (c, d) ∈ A.step w) (fun _ _ h => h) + simpa only [Function.onFun, reindex, relStar, Set.mem_ofPred_eq, Equiv.symm_apply_apply] using h + · rintro ⟨⟨q, p⟩, haccept, hrun⟩ + refine ⟨(e q, p), ?_, ?_⟩ + · simpa only [reindex, Set.mem_preimage, Equiv.symm_apply_apply] using haccept + · exact hrun.lift (fun c : Q × Fin (w.length + 2) => (e c.1, c.2)) + (fun c d h => by + simpa only [Function.onFun, step, reindex, Set.mem_ofPred_eq, + Equiv.symm_apply_apply] using h) + +/-- Embed an original configuration while reserving one state and one exit position. -/ +def augmentConfig {Q : Type*} {n : ℕ} (c : Q × Fin n) : Option Q × Fin (n + 1) := + (some c.1, c.2.castSucc) + +theorem augmentConfig_injective {Q : Type*} {n : ℕ} : + Function.Injective (augmentConfig : Q × Fin n → Option Q × Fin (n + 1)) := by + intro a b hab + have hstate : some a.1 = some b.1 := congrArg Prod.fst hab + have hpos : a.2.val = b.2.val := + congrArg (fun p : Option Q × Fin (n + 1) => p.2.val) hab + exact Prod.ext (Option.some.inj hstate) (Fin.ext hpos) + +/-- Original moves, an accepting stay into the fresh state, and its rightward sweep. -/ +def augmentedStep {Sigma Q : Type*} (A : TwoNFA Sigma Q) (w : List Sigma) : + SetRel (Option Q × Fin (w.length + 3)) (Option Q × Fin (w.length + 3)) := + {(c, d) | (∃ x y : Q × Fin (w.length + 2), + c = augmentConfig x ∧ d = augmentConfig y ∧ (x, y) ∈ A.step w) ∨ + (∃ q : Q, ∃ p : Fin (w.length + 2), + c = augmentConfig (q, p) ∧ d = (none, p.castSucc) ∧ q ∈ A.accepting) ∨ + (c.1 = none ∧ d.1 = none ∧ d.2.val = c.2.val + 1)} + +/-- Before entering the fresh state, every augmented finite path is an original run. -/ +theorem augmented_run_cases {Sigma Q : Type*} (A : TwoNFA Sigma Q) (w : List Sigma) + {c : Option Q × Fin (w.length + 3)} + (hrun : (augmentConfig (A.initial, ⟨0, by omega⟩), c) ∈ relStar (A.augmentedStep w)) : + w ∈ A.language ∨ ∃ x : Q × Fin (w.length + 2), + c = augmentConfig x ∧ ((A.initial, ⟨0, by omega⟩), x) ∈ relStar (A.step w) := by + induction hrun with + | refl => exact Or.inr ⟨(A.initial, ⟨0, by omega⟩), rfl, .refl⟩ + | @tail c d _ hstep ih => + rcases ih with haccept | ⟨x, rfl, hxrun⟩ + · exact Or.inl haccept + rcases hstep with ⟨y, z, hxy, rfl, hyz⟩ | ⟨q, p, hxq, _, hq⟩ | ⟨hnone, _, _⟩ + · have hxy' : x = y := augmentConfig_injective hxy + exact Or.inr ⟨z, rfl, (hxy' ▸ hxrun).tail hyz⟩ + · have hxq' : x = (q, p) := augmentConfig_injective hxq + exact Or.inl ⟨(q, p), hq, hxq' ▸ hxrun⟩ + · exact (Option.some_ne_none _ hnone).elim + +/-- The fresh state can sweep from any position to the designated right exit. -/ +theorem augmented_sink_exit {Sigma Q : Type*} (A : TwoNFA Sigma Q) (w : List Sigma) + (p : Fin (w.length + 3)) : + ((none, p), (none, ⟨w.length + 2, by omega⟩)) ∈ relStar (A.augmentedStep w) := by + have hsweep : ∀ k, ∀ p : Fin (w.length + 3), p.val + k = w.length + 2 → + ((none, p), (none, ⟨w.length + 2, by omega⟩)) ∈ relStar (A.augmentedStep w) := by + intro k + induction k with + | zero => + intro p hp + have hlast : p = ⟨w.length + 2, by omega⟩ := Fin.ext (by omega) + rw [hlast] + exact .refl + | succ k ih => + intro p hp + let next : Fin (w.length + 3) := ⟨p.val + 1, by omega⟩ + have hnext : next.val + k = w.length + 2 := by dsimp [next]; omega + exact (ih next hnext).head (Or.inr (Or.inr ⟨rfl, rfl, rfl⟩)) + exact hsweep (w.length + 2 - p.val) p (by omega) + +/-- Finite acceptance is equivalent to reaching the designated augmented exit. -/ +theorem augmented_acceptance {Sigma Q : Type*} (A : TwoNFA Sigma Q) (w : List Sigma) : + w ∈ A.language ↔ + (augmentConfig (A.initial, ⟨0, by omega⟩), (none, ⟨w.length + 2, by omega⟩)) ∈ + relStar (A.augmentedStep w) := by + constructor + · rintro ⟨c, hc, hrun⟩ + have hlift := hrun.lift augmentConfig + (p := fun c d => (c, d) ∈ A.augmentedStep w) + (fun x y h => Or.inl ⟨x, y, rfl, rfl, h⟩) + have hjump : (augmentConfig c, (none, c.2.castSucc)) ∈ A.augmentedStep w := + Or.inr (Or.inl ⟨c.1, c.2, rfl, rfl, hc⟩) + exact (hlift.tail hjump).trans (A.augmented_sink_exit w c.2.castSucc) + · intro hrun + rcases A.augmented_run_cases w hrun with h | ⟨c, heq, _⟩ + · exact h + · have hnone : (none : Option Q) = some c.1 := + congrArg (fun p : Option Q × Fin (w.length + 3) => p.1) heq + cases hnone + +/-- Augmented moves at one symbol, including the original endmarker restrictions. -/ +def augmentedMove {Sigma Q : Type*} (A : TwoNFA Sigma Q) (symbol : Fin 2 ⊕ Sigma) + (move : Fin 3) : SetRel (Option Q) (Option Q) := + {pq | match pq.1, pq.2 with + | some q, some r => (r, move) ∈ A.transition q symbol ∧ + (symbol = Sum.inl 0 → move ≠ 0) ∧ (symbol = Sum.inl 1 → move ≠ 2) + | some q, none => q ∈ A.accepting ∧ move = 1 + | none, none => move = 2 + | none, some _ => False} + +/-- The global augmented graph is exactly the graph of the local moves and head displacement. -/ +theorem augmentedStep_local_iff {Sigma Q : Type*} (A : TwoNFA Sigma Q) (w : List Sigma) + (c d : Option Q × Fin (w.length + 3)) : + (c, d) ∈ A.augmentedStep w ↔ + ∃ p : Fin (w.length + 2), c.2 = p.castSucc ∧ ∃ move : Fin 3, + (c.1, d.1) ∈ A.augmentedMove (tapeSymbol w p) move ∧ + (d.2.val : ℤ) = (p.val : ℤ) + (move.val : ℤ) - 1 := by + constructor + · rintro (⟨⟨q, p⟩, ⟨r, t⟩, rfl, rfl, hstep⟩ | + ⟨q, p, rfl, rfl, hq⟩ | ⟨hc, hd, hpos⟩) + · obtain ⟨move, htrans, hpos⟩ := hstep + change (t.val : ℤ) = (p.val : ℤ) + (move.val : ℤ) - 1 at hpos + refine ⟨p, rfl, move, ⟨htrans, ?_, ?_⟩, hpos⟩ + · intro hleft hmove + have hp := (tapeSymbol_left_iff w p).mp hleft + have hm := congrArg Fin.val hmove + simp only [Fin.val_zero] at hm + omega + · intro hright hmove + have hp := (tapeSymbol_right_iff w p).mp hright + have hm := congrArg Fin.val hmove + norm_num at hm + omega + · exact ⟨p, rfl, 1, ⟨hq, rfl⟩, by simp⟩ + · let p : Fin (w.length + 2) := ⟨c.2.val, by have := d.2.isLt; omega⟩ + refine ⟨p, Fin.ext rfl, 2, ?_, ?_⟩ + · rw [hc, hd] + exact rfl + · dsimp [p] + omega + · rintro ⟨p, hpos, move, hlocal, hdisp⟩ + obtain ⟨cq, cp⟩ := c + obtain ⟨dq, dp⟩ := d + change cp = p.castSucc at hpos + subst cp + change (dp.val : ℤ) = (p.val : ℤ) + (move.val : ℤ) - 1 at hdisp + cases cq with + | none => + cases dq with + | none => + change move = 2 at hlocal + subst move + exact Or.inr (Or.inr ⟨rfl, rfl, by simp only [Fin.val_castSucc]; norm_num at hdisp; omega⟩) + | some r => exact hlocal.elim + | some q => + cases dq with + | none => + change q ∈ A.accepting ∧ move = 1 at hlocal + obtain ⟨hq, rfl⟩ := hlocal + refine Or.inr (Or.inl ⟨q, p, rfl, ?_, hq⟩) + congr 1 + apply Fin.ext + change dp.val = p.val + norm_num at hdisp + omega + | some r => + change (r, move) ∈ A.transition q (tapeSymbol w p) ∧ + (tapeSymbol w p = Sum.inl 0 → move ≠ 0) ∧ + (tapeSymbol w p = Sum.inl 1 → move ≠ 2) at hlocal + have hdp : dp.val < w.length + 2 := by + by_contra hn + have hdpval : dp.val = w.length + 2 := by omega + have hpval : p.val = w.length + 1 := by have := move.isLt; omega + have hm : move = 2 := Fin.ext (by have := move.isLt; omega) + exact hlocal.2.2 ((tapeSymbol_right_iff w p).mpr hpval) hm + refine Or.inl ⟨(q, p), (r, ⟨dp.val, hdp⟩), rfl, ?_, move, hlocal.1, hdisp⟩ + exact Prod.ext rfl (Fin.ext rfl) + +end TwoNFA + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/PolynomialBound.lean b/lean/OAI/Combinatorics/TwoWayAutomata/PolynomialBound.lean new file mode 100644 index 0000000000000000000000000000000000000000..4a674044610a4d5fd497157a2312e3414be8ba34 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/PolynomialBound.lean @@ -0,0 +1,42 @@ +import OAI.Combinatorics.TwoWayAutomata.ExplicitFamily + +namespace OAI + +namespace TwoWayComplementation + +/-- No alphabet-uniform polynomial state bound determinizes all 2NFAs. -/ +theorem no_polynomial_determinization : + ¬ ∃ p : Polynomial ℝ, ∀ n k : ℕ, ∀ A : TwoNFA (Fin k) (Fin n), + ∃ s : ℕ, ∃ D : TwoNFA (Fin k) (Fin s), + D.Deterministic ∧ D.language = A.language ∧ + (s : ℝ) ≤ p.eval (n : ℝ) := by + classical + rintro ⟨p, hp⟩ + let q : Polynomial ℝ := p.comp (Polynomial.C 127 * Polynomial.X + Polynomial.C 131) + apply polynomial_not_bound_two_pow q + intro t + let n := 127 * t + 131 + obtain ⟨A, _hA, _hcomp, hdet⟩ := explicit_family_main.{0, 0} n (by dsimp [n]; omega) + let Sigma := SetRel (Fin (n - 2)) (Fin (n - 2)) + let e : Sigma ≃ Fin (Fintype.card Sigma) := Fintype.equivFin Sigma + obtain ⟨s, D, hDdet, hD, hs⟩ := hp n (Fintype.card Sigma) (A.pullAlphabet e.symm) + have hrecognizes : (D.pullAlphabet e).language = A.language := by + ext w + rw [D.pullAlphabet_language, hD] + change w.map e ∈ (A.pullAlphabet e.symm).language ↔ w ∈ A.language + rw [A.pullAlphabet_language] + simp only [List.map_map, Equiv.symm_comp_self, List.map_id] + have hbound := hdet (by dsimp [n]; omega) (Fin s) (D.pullAlphabet e) + (hDdet.pullAlphabet e) hrecognizes + have hexponent : (n - 4) / 127 = t + 1 := by dsimp [n]; omega + rw [hexponent, Fintype.card_fin, pow_succ] at hbound + change (2 : ℝ) ^ t ≤ q.eval (t : ℝ) + simp only [q, Polynomial.eval_comp, Polynomial.eval_add, Polynomial.eval_mul, + Polynomial.eval_C, Polynomial.eval_X] + have hn : (n : ℝ) = 127 * (t : ℝ) + 131 := by simp [n] + rw [hn] at hs + linarith + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Recognition.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Recognition.lean new file mode 100644 index 0000000000000000000000000000000000000000..eddc37971e71a324a2053de33b71bd7198b0e824 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Recognition.lean @@ -0,0 +1,114 @@ +import OAI.Combinatorics.TwoWayAutomata.ImageBound +import OAI.Combinatorics.TwoWayAutomata.Source + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Singleton contexts force reversal of relation inclusion. -/ +theorem word_diagram_order_reversal {H : Type*} {m : ℕ} + (τ : List (SetRel H H) → Diagram m) + (hmul : ∀ u v, τ (u ++ v) = Diagram.comp (τ u) (τ v)) + (l r : Diagram m) (a b : Fin m) + (hlanguage : ∀ w, (a, b) ∈ (Diagram.comp (Diagram.comp l (τ w)) r).forward ↔ + ¬ (relationProduct w).Nonempty) + {u v : List (SetRel H H)} (huv : (τ u, τ v) ∈ Diagram.inclusion m) : + relationProduct v ⊆ relationProduct u := by + classical + rintro ⟨p, q⟩ hpq + by_contra hn + let left := [restrictedIdentity {p}] + let right := [restrictedIdentity {q}] + have hU : ¬ (relationProduct (left ++ u ++ right)).Nonempty := + fun h => hn ((singleton_word_context u p q).mp h) + have hV : (relationProduct (left ++ v ++ right)).Nonempty := + (singleton_word_context v p q).mpr hpq + have hcontext : (τ (left ++ u ++ right), τ (left ++ v ++ right)) ∈ Diagram.inclusion m := by + simp only [hmul] + exact Diagram.comp_mono (Diagram.comp_mono (Diagram.inclusion_refl _) huv) + (Diagram.inclusion_refl _) + have hmarked := Diagram.comp_mono + (Diagram.comp_mono (Diagram.inclusion_refl l) hcontext) (Diagram.inclusion_refl r) + exact (hlanguage _).mp (hmarked.1 ((hlanguage _).mpr hU)) hV + +/-- The relation product descends to the diagram image by singleton-context tests. -/ +theorem diagram_relation_image {H : Type*} {m : ℕ} + (τ : List (SetRel H H) → Diagram m) + (hzero : τ [] = Diagram.identity m) + (hmul : ∀ u v, τ (u ++ v) = Diagram.comp (τ u) (τ v)) + (l r : Diagram m) (a b : Fin m) + (hlanguage : ∀ w, (a, b) ∈ (Diagram.comp (Diagram.comp l (τ w)) r).forward ↔ + ¬ (relationProduct w).Nonempty) : + ∃ S : Submonoid (Diagram m), ∃ φ : S →* RelationMonoid H, + Function.Surjective φ ∧ + (∀ z w : S, (z.val, w.val) ∈ Diagram.inclusion m → (φ w).toRel ⊆ (φ z).toRel) := by + classical + let S : Submonoid (Diagram m) := + { carrier := Set.range τ + one_mem' := ⟨[], hzero⟩ + mul_mem' := by + rintro _ _ ⟨u, rfl⟩ ⟨v, rfl⟩ + exact ⟨u ++ v, hmul u v⟩ } + have hword (z : S) : ∃ w, τ w = z.val := z.property + choose word hwordeq using hword + have hwell {u v : List (SetRel H H)} (huv : τ u = τ v) : + relationProduct u = relationProduct v := by + apply Set.Subset.antisymm + · apply word_diagram_order_reversal τ hmul l r a b hlanguage + rw [huv] + exact Diagram.inclusion_refl _ + · apply word_diagram_order_reversal τ hmul l r a b hlanguage + rw [huv] + exact Diagram.inclusion_refl _ + have hvalue (z : S) (w : List (SetRel H H)) (hw : τ w = z.val) : + relationProduct (word z) = relationProduct w := hwell ((hwordeq z).trans hw.symm) + let φ : S →* RelationMonoid H := + { toFun := fun z => .ofRel (relationProduct (word z)) + map_one' := by + apply RelationMonoid.ext + exact hvalue 1 [] hzero + map_mul' := by + intro z w + apply RelationMonoid.ext + change relationProduct (word (z * w)) = relationProduct (word z) ○ relationProduct (word w) + rw [hvalue (z * w) (word z ++ word w) (by rw [hmul, hwordeq, hwordeq]; rfl)] + exact relationProduct_append _ _ } + refine ⟨S, φ, ?_, ?_⟩ + · intro R + refine ⟨⟨τ [R.toRel], [R.toRel], rfl⟩, ?_⟩ + apply RelationMonoid.ext + change relationProduct (word _) = R.toRel + rw [hvalue _ [R.toRel] rfl] + simp [relationProduct] + · intro z w hzw + change relationProduct (word w) ⊆ relationProduct (word z) + apply word_diagram_order_reversal τ hmul l r a b hlanguage + rwa [hwordeq, hwordeq] + +/-- Recognition of relation-product emptiness forces the image theorem's state bound. -/ +theorem diagram_recognition_lower_bound (h m : ℕ) (hh : 2 ≤ h) (hm : 1 ≤ m) + (τ : List (SetRel (Fin h) (Fin h)) → Diagram m) + (hzero : τ [] = Diagram.identity m) + (hmul : ∀ u v, τ (u ++ v) = Diagram.comp (τ u) (τ v)) + (l r : Diagram m) (a b : Fin m) + (hlanguage : ∀ w, (a, b) ∈ (Diagram.comp (Diagram.comp l (τ w)) r).forward ↔ + ¬ (relationProduct w).Nonempty) : lossBound h ≤ 2 * m := by + obtain ⟨S, φ, hsurj, hreverse⟩ := diagram_relation_image τ hzero hmul l r a b hlanguage + apply order_reversing_image_bound m h hm hh S S.one_mem (by + intro z w hz hw + change z * w ∈ S + exact S.mul_mem hz hw) + (fun z => (φ z).toRel) + · intro R + obtain ⟨z, hz⟩ := hsurj (RelationMonoid.ofRel R) + exact ⟨z, congrArg RelationMonoid.toRel hz⟩ + · intro z w + change (φ (z * w)).toRel = (φ z).toRel ○ (φ w).toRel + rw [map_mul, RelationMonoid.toRel_mul] + · exact hreverse + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/RelationBridge.lean b/lean/OAI/Combinatorics/TwoWayAutomata/RelationBridge.lean new file mode 100644 index 0000000000000000000000000000000000000000..83e2170d91f34b94f0380a51335e1698e90dc7ce --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/RelationBridge.lean @@ -0,0 +1,37 @@ +import OAI.Combinatorics.TwoWayAutomata.Source +import OAI.Combinatorics.Automata.Model + +namespace OAI + +namespace TwoWayComplementation.CompanionBridge + +open OneWayLiveness +open scoped SetRel + +/-- Exact currying/uncurrying equivalence of the two full relation alphabets. -/ +def relationEquiv (H : Type*) : BRel H ≃ SetRel H H where + toFun R := {xy | R.holds xy.1 xy.2} + invFun R := ⟨fun x y => (x, y) ∈ R⟩ + left_inv _R := rfl + right_inv _R := rfl + +/-- Both products compose in path order, including the identity for the empty word. -/ +theorem relationProduct_map {H : Type*} (w : List (BRel H)) : + relationProduct (w.map (relationEquiv H)) = relationEquiv H w.prod := by + induction w with + | nil => rfl + | cons R w ih => + change relationEquiv H R ○ relationProduct (w.map (relationEquiv H)) = + relationEquiv H (R * w.prod) + rw [ih] + rfl + +theorem sourceLanguage_map_iff {H : Type*} (w : List (BRel H)) : + w.map (relationEquiv H) ∈ sourceLanguage H ↔ BRel.live w := by + change (relationProduct (w.map (relationEquiv H))).Nonempty ↔ _ + rw [relationProduct_map] + exact ⟨fun ⟨⟨x, y⟩, h⟩ => ⟨x, y, h⟩, fun ⟨x, y, h⟩ => ⟨(x, y), h⟩⟩ + +end TwoWayComplementation.CompanionBridge + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/RelationMonoid.lean b/lean/OAI/Combinatorics/TwoWayAutomata/RelationMonoid.lean new file mode 100644 index 0000000000000000000000000000000000000000..db95e3c2b940c237a8f8422138d7480c73409de6 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/RelationMonoid.lean @@ -0,0 +1,230 @@ +import OAI.Combinatorics.TwoWayAutomata.Corners + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Identity restricted to the retained points. -/ +def restrictedIdentity {α : Type*} (F : Set α) : SetRel α α := + {(x, y) | x = y ∧ x ∈ F} + +@[simp] +theorem restrictedIdentity_comp_self {α : Type*} (F : Set α) : + restrictedIdentity F ○ restrictedIdentity F = restrictedIdentity F := by + ext ⟨x, y⟩ + simp [SetRel.comp, restrictedIdentity] + +/-- Singleton contexts test an individual relation edge. -/ +theorem singleton_context {α : Type*} (R : SetRel α α) (p q : α) : + restrictedIdentity {p} ○ R ○ restrictedIdentity {q} = + {(x, y) | x = p ∧ y = q ∧ (p, q) ∈ R} := by + ext ⟨x, y⟩ + simp [SetRel.comp, restrictedIdentity] + aesop + +/-- Restriction-corner identities. + The supporting relation `i` can be any idempotent with `iPi = i`. -/ +theorem relation_corner_identities {α : Type*} (P i : SetRel α α) + (hP : P ○ P = P) (hiPi : i ○ P ○ i = i) : + let Q := P ○ i ○ P + Q ○ Q = Q ∧ P ○ Q = Q ∧ Q ○ P = Q ∧ + Q ○ i = P ○ i ∧ i ○ Q = i ○ P ∧ i ○ Q ○ i = i := by + have hPP (R : SetRel α α) : P ○ (P ○ R) = P ○ R := by + rw [← SetRel.comp_assoc, hP] + have hiPiR (R : SetRel α α) : i ○ (P ○ (i ○ R)) = i ○ R := by + rw [← SetRel.comp_assoc i P, ← SetRel.comp_assoc, hiPi] + dsimp + simp [SetRel.comp_assoc, hPP, hiPiR, hP, hiPi] + +/-- Restriction and extension are inverse on the two supported corners. -/ +theorem relation_corner_inverse {α : Type*} (P i : SetRel α α) + (hP : P ○ P = P) (hiPi : i ○ P ○ i = i) : + (∀ Z, (P ○ i ○ P) ○ Z ○ (P ○ i ○ P) = Z → P ○ (i ○ Z ○ i) ○ P = Z) ∧ + (∀ A, i ○ A ○ i = A → + (P ○ i ○ P) ○ (P ○ A ○ P) ○ (P ○ i ○ P) = P ○ A ○ P ∧ + i ○ (P ○ A ○ P) ○ i = A) := by + have hPP (R : SetRel α α) : P ○ (P ○ R) = P ○ R := by + rw [← SetRel.comp_assoc, hP] + have hiPiR (R : SetRel α α) : i ○ (P ○ (i ○ R)) = i ○ R := by + rw [← SetRel.comp_assoc i P, ← SetRel.comp_assoc, hiPi] + constructor + · intro Z hZ + have hPZP : P ○ Z ○ P = Z := by + conv_lhs => rw [← hZ] + simpa only [SetRel.comp_assoc, hPP, hP] using hZ + calc + P ○ (i ○ Z ○ i) ○ P = P ○ (i ○ (P ○ Z ○ P) ○ i) ○ P := by rw [hPZP] + _ = Z := by simpa only [SetRel.comp_assoc] using hZ + · intro A hA + constructor + · calc + (P ○ i ○ P) ○ (P ○ A ○ P) ○ (P ○ i ○ P) = + P ○ (i ○ A ○ i) ○ P := by + conv_lhs => rw [← hA] + simp only [SetRel.comp_assoc, hPP, hiPiR] + _ = P ○ A ○ P := by rw [hA] + · conv_lhs => rw [← hA] + simpa only [SetRel.comp_assoc, hiPiR, hiPi] using hA + +/-- Extension preserves products on the supported relation corner. -/ +theorem relation_corner_comp {α : Type*} (P i A B : SetRel α α) + (hP : P ○ P = P) (hi : i ○ i = i) (hiPi : i ○ P ○ i = i) + (hA : i ○ A ○ i = A) (hB : i ○ B ○ i = B) : + (P ○ A ○ P) ○ (P ○ B ○ P) = P ○ (A ○ B) ○ P := by + have hPP (R : SetRel α α) : P ○ (P ○ R) = P ○ R := by + rw [← SetRel.comp_assoc, hP] + have hiPiR (R : SetRel α α) : i ○ (P ○ (i ○ R)) = i ○ R := by + rw [← SetRel.comp_assoc i P, ← SetRel.comp_assoc, hiPi] + have hii (R : SetRel α α) : i ○ (i ○ R) = i ○ R := by + rw [← SetRel.comp_assoc, hi] + conv_lhs => rw [← hA, ← hB] + simpa only [SetRel.comp_assoc, hPP, hiPiR, hii] using + congrArg (fun R => P ○ R ○ P) (congrArg₂ SetRel.comp hA hB) + +/-- Relations in path order, using the opposite of Mathlib's endomorphism monoid. -/ +abbrev RelationMonoid (H : Type*) := + (CategoryTheory.End (show CategoryTheory.RelCat from H))ᵐᵒᵖ + +namespace RelationMonoid + +/-- Regard a binary relation as a path-ordered endomorphism. -/ +def ofRel {H : Type*} (R : SetRel H H) : RelationMonoid H := + MulOpposite.op (.ofRel R) + +/-- The underlying binary relation. -/ +def toRel {H : Type*} (R : RelationMonoid H) : SetRel H H := R.unop.rel + +@[ext] +theorem ext {H : Type*} {R S : RelationMonoid H} (h : R.toRel = S.toRel) : R = S := + MulOpposite.unop_injective (CategoryTheory.RelCat.Hom.ext _ _ h) + +@[simp] +theorem toRel_ofRel {H : Type*} (R : SetRel H H) : (ofRel R).toRel = R := rfl + +@[simp] +theorem toRel_mul {H : Type*} (R S : RelationMonoid H) : + (R * S).toRel = R.toRel ○ S.toRel := rfl + +@[simp] +theorem toRel_one {H : Type*} : (1 : RelationMonoid H).toRel = SetRel.id := rfl + +end RelationMonoid + +/-- Extending a relation on a subset and restricting it again loses no pairs. -/ +theorem relation_extend_restrict {H : Type*} (F : Set H) (R : SetRel F F) : + {xy : F × F | (xy.1.val, xy.2.val) ∈ (Prod.map Subtype.val Subtype.val) '' R} = R := by + ext ⟨x, y⟩ + simp only [Set.mem_ofPred_eq, Set.mem_image, Prod.exists, Prod.map_apply, Prod.mk.injEq] + constructor + · rintro ⟨a, b, hab, hax, hby⟩ + exact (Subtype.ext hax ▸ Subtype.ext hby ▸ hab) + · intro hxy + exact ⟨x, y, hxy, rfl, rfl⟩ + +/-- Extension of restriction is precisely the identity sandwich on the subset. -/ +theorem relation_restrict_extend {H : Type*} (F : Set H) (R : SetRel H H) : + (Prod.map Subtype.val Subtype.val) '' {xy : F × F | (xy.1.val, xy.2.val) ∈ R} = + restrictedIdentity F ○ R ○ restrictedIdentity F := by + ext ⟨x, y⟩ + simp only [Set.mem_image, Prod.exists, Set.mem_ofPred_eq, Prod.map_apply, Prod.mk.injEq, + SetRel.comp, restrictedIdentity] + constructor + · rintro ⟨a, b, hab, rfl, rfl⟩ + exact ⟨b.val, ⟨a.val, ⟨rfl, a.property⟩, hab⟩, rfl, b.property⟩ + · rintro ⟨b, ⟨a, ⟨rfl, hx⟩, hab⟩, rfl, hy⟩ + exact ⟨⟨x, hx⟩, ⟨b, hy⟩, hab, rfl, rfl⟩ + +/-- Extension of relations on a subset preserves path composition. -/ +theorem relation_extend_comp {H : Type*} (F : Set H) (A B : SetRel F F) : + (Prod.map Subtype.val Subtype.val) '' (A ○ B) = + ((Prod.map Subtype.val Subtype.val) '' A) ○ + ((Prod.map Subtype.val Subtype.val) '' B) := by + ext ⟨x, y⟩ + constructor + · rintro ⟨⟨a, b⟩, ⟨c, hac, hcb⟩, hxy⟩ + cases hxy + exact ⟨c.val, ⟨(a, c), hac, rfl⟩, (c, b), hcb, rfl⟩ + · rintro ⟨z, ⟨⟨a, b⟩, hab, habxy⟩, ⟨⟨c, d⟩, hcd, hcdxy⟩⟩ + have hbc : b = c := Subtype.ext ((congrArg Prod.snd habxy).trans + (congrArg Prod.fst hcdxy).symm) + subst c + have hax : a.val = x := congrArg Prod.fst habxy + have hdy : d.val = y := congrArg Prod.snd hcdxy + exact ⟨(a, d), ⟨b, hab, hcd⟩, Prod.ext hax hdy⟩ + +/-- The full relation monoid on a subset is the compressed relation corner. -/ +theorem relation_corner_equiv {H : Type*} (P : SetRel H H) (F : Set H) + (hP : P ○ P = P) (hiPi : restrictedIdentity F ○ P ○ restrictedIdentity F = + restrictedIdentity F) : + ∃ hQ : IsIdempotentElem (RelationMonoid.ofRel (P ○ restrictedIdentity F ○ P)), + ∃ ρ : hQ.Corner ≃* RelationMonoid F, + ∀ Z, (ρ Z).toRel = {xy : F × F | (xy.1.val, xy.2.val) ∈ Z.val.toRel} := by + let i := restrictedIdentity F + let Q := P ○ i ○ P + have hQ : IsIdempotentElem (RelationMonoid.ofRel Q) := + RelationMonoid.ext (relation_corner_identities P i hP hiPi).1 + have hZ (Z : hQ.Corner) : Q ○ Z.val.toRel ○ Q = Z.val.toRel := by + obtain ⟨hl, hr⟩ := (Subsemigroup.mem_corner_iff hQ).mp Z.property + exact congrArg RelationMonoid.toRel ((congrArg (· * RelationMonoid.ofRel Q) hl).trans hr) + let extend (A : RelationMonoid F) : SetRel H H := + (Prod.map Subtype.val Subtype.val) '' A.toRel + have hsupp (A : RelationMonoid F) : i ○ extend A ○ i = extend A := by + rw [← relation_restrict_extend F, relation_extend_restrict] + let f (Z : hQ.Corner) : RelationMonoid F := + .ofRel {xy : F × F | (xy.1.val, xy.2.val) ∈ Z.val.toRel} + let g (A : RelationMonoid F) : hQ.Corner := + ⟨.ofRel (P ○ extend A ○ P), (Subsemigroup.mem_corner_iff hQ).mpr (by + have hs := (relation_corner_inverse P i hP hiPi).2 (extend A) (hsupp A) |>.1 + change Q ○ (P ○ extend A ○ P) ○ Q = P ○ extend A ○ P at hs + have hQQ : Q ○ Q = Q := (relation_corner_identities P i hP hiPi).1 + constructor <;> apply RelationMonoid.ext + · change Q ○ (P ○ extend A ○ P) = P ○ extend A ○ P + conv_lhs => rw [← hs] + simpa only [← SetRel.comp_assoc, hQQ] using hs + · change (P ○ extend A ○ P) ○ Q = P ○ extend A ○ P + conv_lhs => rw [← hs] + simpa only [SetRel.comp_assoc, hQQ] using hs)⟩ + have hleft (Z : hQ.Corner) : g (f Z) = Z := by + apply Subtype.ext + apply RelationMonoid.ext + change P ○ ((Prod.map Subtype.val Subtype.val) '' + {xy : F × F | (xy.1.val, xy.2.val) ∈ Z.val.toRel}) ○ P = Z.val.toRel + rw [relation_restrict_extend] + exact (relation_corner_inverse P i hP hiPi).1 _ (hZ Z) + have hright (A : RelationMonoid F) : f (g A) = A := by + apply RelationMonoid.ext + have hext : extend (f (g A)) = extend A := by + change (Prod.map Subtype.val Subtype.val) '' {xy : F × F | + (xy.1.val, xy.2.val) ∈ P ○ extend A ○ P} = extend A + rw [relation_restrict_extend] + exact (relation_corner_inverse P i hP hiPi).2 (extend A) (hsupp A) |>.2 + have hinj : Function.Injective + (Prod.map (Subtype.val : F → H) (Subtype.val : F → H)) := + fun _ _ h => Prod.ext (Subtype.ext (congrArg Prod.fst h)) + (Subtype.ext (congrArg Prod.snd h)) + exact Set.image_injective.mpr hinj hext + have hgmul (A B : RelationMonoid F) : g (A * B) = g A * g B := by + apply Subtype.ext + apply RelationMonoid.ext + change P ○ extend (A * B) ○ P = (P ○ extend A ○ P) ○ (P ○ extend B ○ P) + have hext : extend (A * B) = extend A ○ extend B := relation_extend_comp F _ _ + rw [hext] + exact (relation_corner_comp P i _ _ hP (restrictedIdentity_comp_self F) + hiPi (hsupp A) (hsupp B)).symm + let ρ : hQ.Corner ≃* RelationMonoid F := + { toFun := f + invFun := g + left_inv := hleft + right_inv := hright + map_mul' := by + intro Z W + apply_fun g + · rw [hgmul, hleft, hleft, hleft] + · exact Function.LeftInverse.injective hright } + exact ⟨hQ, ρ, fun _ => rfl⟩ + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Relations.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Relations.lean new file mode 100644 index 0000000000000000000000000000000000000000..f7408e5fce1ee9892ead93e8a50f16ed00bdc9f0 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Relations.lean @@ -0,0 +1,351 @@ +import OAI.Combinatorics.TwoWayAutomata.Model + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +@[gcongr] +theorem relStar_mono {α : Type*} {R S : SetRel α α} (h : R ⊆ S) : + relStar R ⊆ relStar S := + fun _ hab => hab.mono (fun _ _ hxy => h hxy) _ _ + +@[simp] +theorem relStar_empty {α : Type*} : relStar (∅ : SetRel α α) = SetRel.id := by + ext ⟨a, b⟩ + constructor + · intro h + cases h with + | refl => rfl + | tail _ h => exact h.elim + · rintro rfl + exact Relation.ReflTransGen.refl + +@[simp] +theorem relStar_comp_self {α : Type*} (R : SetRel α α) : + relStar R ○ relStar R = relStar R := by + ext ⟨a, b⟩ + exact ⟨fun ⟨_, hac, hcb⟩ => hac.trans hcb, + fun hab => ⟨b, hab, Relation.ReflTransGen.refl⟩⟩ + +/-- A directed graph partitioned into two kinds of vertices. -/ +def blockRelation {α β : Type*} (E : SetRel α α) (F : SetRel α β) + (G : SetRel β α) (H : SetRel β β) : SetRel (α ⊕ β) (α ⊕ β) := + {xy | match xy.1, xy.2 with + | .inl a, .inl b => (a, b) ∈ E + | .inl a, .inr b => (a, b) ∈ F + | .inr a, .inl b => (a, b) ∈ G + | .inr a, .inr b => (a, b) ∈ H} + +/-- Eliminate the left block by replacing each finite excursion with one edge. -/ +theorem blockRelation_star_right {α β : Type*} (E : SetRel α α) (F : SetRel α β) + (G : SetRel β α) (H : SetRel β β) (x y : β) : + ((Sum.inr x), (Sum.inr y)) ∈ relStar (blockRelation E F G H) ↔ + (x, y) ∈ relStar (H ∪ (G ○ relStar E ○ F)) := by + let R := blockRelation E F G H + let K := H ∪ (G ○ relStar E ○ F) + have hsound {c : α ⊕ β} (hrun : (Sum.inr x, c) ∈ relStar R) : + match c with + | .inl a => (x, a) ∈ relStar K ○ G ○ relStar E + | .inr b => (x, b) ∈ relStar K := by + induction hrun with + | refl => exact .refl + | @tail c d _ hcd ih => + cases c with + | inl a => + obtain ⟨p, ⟨q, hxq, hqp⟩, hpa⟩ := ih + cases d with + | inl b => exact ⟨p, ⟨q, hxq, hqp⟩, hpa.tail hcd⟩ + | inr b => exact hxq.tail (Or.inr ⟨a, ⟨p, hqp, hpa⟩, hcd⟩) + | inr a => + cases d with + | inl b => exact ⟨b, ⟨a, ih, hcd⟩, .refl⟩ + | inr b => exact ih.tail (Or.inl hcd) + constructor + · exact hsound + · intro hxy + exact hxy.lift' (Sum.inr : β → α ⊕ β) + (p := fun a b => (a, b) ∈ R) (by + intro a b hab + rcases hab with hH | ⟨q, ⟨p, hag, hpq⟩, hfb⟩ + · exact .single hH + · have hmid := hpq.lift (Sum.inl : α → α ⊕ β) + (p := fun a b => (a, b) ∈ R) (fun _ _ h => h) + exact (hmid.head hag).tail hfb) + +/-- A path from the left block first leaves it once, then makes arbitrary finite excursions. -/ +theorem blockRelation_star_cross {α β : Type*} (E : SetRel α α) (F : SetRel α β) + (G : SetRel β α) (H : SetRel β β) (x : α) (y : β) : + ((Sum.inl x), (Sum.inr y)) ∈ relStar (blockRelation E F G H) ↔ + (x, y) ∈ relStar E ○ F ○ relStar (H ∪ (G ○ relStar E ○ F)) := by + let R := blockRelation E F G H + let K := H ∪ (G ○ relStar E ○ F) + have hsound {c : α ⊕ β} (hrun : (c, Sum.inr y) ∈ relStar R) : + ∀ x, c = Sum.inl x → (x, y) ∈ relStar E ○ F ○ relStar K := by + induction hrun using Relation.ReflTransGen.head_induction_on with + | refl => intro _ heq; cases heq + | @head c d hcd hdy ih => + intro x hcx + subst c + cases d with + | inl a => + obtain ⟨u, ⟨v, hxv, hvu⟩, huy⟩ := ih a rfl + exact ⟨u, ⟨v, hxv.head hcd, hvu⟩, huy⟩ + | inr b => + exact ⟨b, ⟨x, .refl, hcd⟩, (blockRelation_star_right E F G H b y).mp hdy⟩ + constructor + · intro hxy + exact hsound hxy x rfl + · rintro ⟨v, ⟨u, hxu, huv⟩, hvy⟩ + have hleft := hxu.lift (Sum.inl : α → α ⊕ β) + (p := fun a b => (a, b) ∈ R) (fun _ _ h => h) + have hcross : (Sum.inl x, Sum.inr v) ∈ relStar R := hleft.tail huv + exact hcross.trans ((blockRelation_star_right E F G H v y).mpr hvy) + +/-- Closure of a union factors into alternating finite paths. -/ +theorem relStar_union {α : Type*} (A B : SetRel α α) : + relStar (A ∪ B) = relStar A ○ relStar (B ○ relStar A) := by + ext ⟨a, b⟩ + constructor + · intro h + induction h using Relation.ReflTransGen.head_induction_on with + | refl => exact ⟨b, .refl, .refl⟩ + | @head a c hstep _ ih => + obtain ⟨d, hcd, hdb⟩ := ih + rcases hstep with hA | hB + · exact ⟨d, hcd.head hA, hdb⟩ + · exact ⟨a, .refl, hdb.head ⟨c, hB, hcd⟩⟩ + · rintro ⟨c, hac, hcb⟩ + have hA := relStar_mono (show A ⊆ A ∪ B from Set.subset_union_left) hac + refine hA.trans (Relation.reflTransGen_closed ?_ _ _ hcb) + rintro x y ⟨z, hxz, hzy⟩ + exact (relStar_mono Set.subset_union_left hzy).head (Or.inr hxz) + +/-- A finite alternating path can be rotated between its endpoint types. -/ +theorem comp_relStar_rotate {α β : Type*} (A : SetRel α β) (B : SetRel β α) : + A ○ relStar (B ○ A) = relStar (A ○ B) ○ A := by + ext ⟨a, b⟩ + constructor + · rintro ⟨c, hac, hcb⟩ + induction hcb with + | refl => exact ⟨a, .refl, hac⟩ + | @tail x y _ hxy ih => + obtain ⟨p, hap, hpx⟩ := ih + obtain ⟨q, hxq, hqy⟩ := hxy + exact ⟨q, hap.tail ⟨x, hpx, hxq⟩, hqy⟩ + · rintro ⟨c, hac, hcb⟩ + induction hac generalizing b with + | refl => exact ⟨b, hcb, .refl⟩ + | @tail x y _ hxy ih => + obtain ⟨p, hxp, hpy⟩ := hxy + obtain ⟨q, haq, hqp⟩ := ih p hxp + exact ⟨q, haq, hqp.tail ⟨y, hpy, hcb⟩⟩ + +theorem relComp_union {α β γ : Type*} (A : SetRel α β) (B C : SetRel β γ) : + A ○ (B ∪ C) = (A ○ B) ∪ (A ○ C) := by + ext ⟨x, y⟩ + simp only [SetRel.comp, Set.mem_ofPred_eq, Set.mem_union] + aesop + +theorem union_relComp {α β γ : Type*} (A B : SetRel α β) (C : SetRel β γ) : + (A ∪ B) ○ C = (A ○ C) ∪ (B ○ C) := by + ext ⟨x, y⟩ + simp only [SetRel.comp, Set.mem_ofPred_eq, Set.mem_union] + aesop + +theorem relStar_unfold {α : Type*} (R : SetRel α α) : + relStar R = SetRel.id ∪ (relStar R ○ R) := by + ext ⟨x, y⟩ + constructor + · intro h + cases h with + | refl => exact Or.inl rfl + | @tail z _ hxz hzy => exact Or.inr ⟨z, hxz, hzy⟩ + · rintro (h | ⟨z, hxz, hzy⟩) + · cases h + exact Relation.ReflTransGen.refl + · exact hxz.tail hzy + +/-- Eliminating two internal boundaries in either order gives the same through relation. -/ +theorem relStar_block_forward {α : Type*} (A B C D : SetRel α α) : + relStar (A ∪ (B ○ relStar D ○ C)) ○ B ○ relStar D = + relStar A ○ B ○ relStar (D ∪ (C ○ relStar A ○ B)) := by + simpa only [relStar_union, SetRel.comp_assoc] using + congrArg (relStar A ○ ·) + (comp_relStar_rotate (B ○ relStar D) (C ○ relStar A)).symm + +/-- The corresponding return relation when eliminating two internal boundaries. -/ +theorem relStar_block_return {α : Type*} (A B C D : SetRel α α) : + relStar (A ∪ (B ○ relStar D ○ C)) = + relStar A ∪ (relStar A ○ B ○ relStar (D ∪ (C ○ relStar A ○ B)) ○ + C ○ relStar A) := by + have hunfold := congrArg (relStar A ○ ·) + (relStar_unfold (B ○ relStar D ○ C ○ relStar A)) + have hreturn : relStar (A ∪ (B ○ relStar D ○ C)) = + relStar A ∪ (relStar (A ∪ (B ○ relStar D ○ C)) ○ B ○ relStar D ○ + C ○ relStar A) := by + simpa only [relStar_union, relComp_union, SetRel.comp_id, SetRel.comp_assoc] using hunfold + rw [hreturn, relStar_block_forward] + +theorem relStar_comp_unfold {α β : Type*} (R : SetRel α β) (L : SetRel β α) : + relStar (R ○ L) = SetRel.id ∪ (R ○ relStar (L ○ R) ○ L) := by + calc + _ = SetRel.id ∪ (relStar (R ○ L) ○ (R ○ L)) := relStar_unfold _ + _ = _ := by rw [comp_relStar_rotate, SetRel.comp_assoc] + +theorem relStar_block_exit {α : Type*} (L R B E D : SetRel α α) : + relStar ((L ○ R) ∪ (B ○ relStar D ○ E ○ R)) ○ (L ∪ (B ○ relStar D ○ E)) = + (relStar (L ○ R) ○ L) ∪ + (relStar (L ○ R) ○ B ○ relStar (D ∪ (E ○ R ○ relStar (L ○ R) ○ B)) ○ + E ○ relStar (R ○ L)) := by + have hF := relStar_block_forward (L ○ R) B (E ○ R) D + have hW := relStar_block_return (L ○ R) B (E ○ R) D + simp only [SetRel.comp_assoc] at hF hW + calc + _ = (relStar ((L ○ R) ∪ (B ○ relStar D ○ E ○ R)) ○ L) ∪ + ((relStar ((L ○ R) ∪ (B ○ relStar D ○ E ○ R)) ○ B ○ relStar D) ○ E) := by + simp only [relComp_union, SetRel.comp_assoc] + _ = ((relStar (L ○ R) ∪ + (relStar (L ○ R) ○ B ○ relStar (D ∪ (E ○ R ○ relStar (L ○ R) ○ B)) ○ + E ○ R ○ relStar (L ○ R))) ○ L) ∪ + ((relStar (L ○ R) ○ B ○ relStar (D ∪ (E ○ R ○ relStar (L ○ R) ○ B))) ○ E) := by + simpa only [SetRel.comp_assoc] using + congrArg₂ (· ∪ ·) (congrArg (· ○ L) hW) (congrArg (· ○ E) hF) + _ = _ := by + rw [relStar_comp_unfold R L] + simp only [union_relComp, relComp_union, SetRel.comp_id, SetRel.comp_assoc] + ac_rfl + +/-- A path lost after an edge replacement contains a lost edge between old path segments. -/ +theorem relStar_lost_edge {α : Type*} (R S : SetRel α α) {x y : α} + (hxy : (x, y) ∈ relStar R) (hnot : (x, y) ∉ relStar S) : + ∃ a b, (x, a) ∈ relStar R ∧ (a, b) ∈ R ∧ (a, b) ∉ S ∧ (b, y) ∈ relStar R := by + classical + induction hxy with + | refl => exact (hnot Relation.ReflTransGen.refl).elim + | @tail a b hxa hab ih => + by_cases hgood : (x, a) ∈ relStar S + · refine ⟨a, b, hxa, hab, ?_, .refl⟩ + exact fun habS => hnot (hgood.tail habS) + · obtain ⟨u, v, hxu, huv, hnuv, hva⟩ := ih hgood + exact ⟨u, v, hxu, huv, hnuv, hva.tail hab⟩ + +/-- A lost alternating traversal has a lost edge, allowing earlier and later bounces. -/ +theorem feedback_lost_edge {α : Type*} (E E' B : SetRel α α) {x y : α} + (hxy : (x, y) ∈ E ○ relStar (B ○ E)) + (hnot : (x, y) ∉ E' ○ relStar (B ○ E')) : + ∃ a b, (x, a) ∈ relStar (E ○ B) ∧ (a, b) ∈ E ∧ + (a, b) ∉ E' ∧ (b, y) ∈ relStar (B ○ E) := by + classical + obtain ⟨z, hxz, hzy⟩ := hxy + by_cases hfirst : (x, z) ∈ E' + · have hnzy : (z, y) ∉ relStar (B ○ E') := fun h => hnot ⟨z, hfirst, h⟩ + obtain ⟨a, b, hza, ⟨c, hac, hcb⟩, hnab, hby⟩ := + relStar_lost_edge (B ○ E) (B ○ E') hzy hnzy + have hprefix : (x, a) ∈ relStar (E ○ B) ○ E := by + rw [← comp_relStar_rotate] + exact ⟨z, hxz, hza⟩ + obtain ⟨d, hxd, hda⟩ := hprefix + exact ⟨c, b, hxd.tail ⟨a, hda, hac⟩, hcb, fun h => hnab ⟨c, hac, h⟩, hby⟩ + · exact ⟨x, z, .refl, hxz, hfirst, hzy⟩ + +theorem feedback_insert {α : Type*} (E B : SetRel α α) : + relStar (E ○ B) ○ E ○ relStar (B ○ E) = E ○ relStar (B ○ E) := by + rw [← comp_relStar_rotate, SetRel.comp_assoc, relStar_comp_self] + +/-- A bound for weighted classes with integer weights one and two. -/ +theorem weighted_class_bound {α β : Type*} (X M : Finset α) (Y : Finset β) + (R : β → α → Prop) (hM : M ⊆ X) + (hunique : ∀ y ∈ Y, ∀ z ∈ Y, ∀ x ∈ X, R y x → R z x → y = z) + (hcover : ∀ y ∈ Y, + (∃ x ∈ X, x ∉ M ∧ R y x) ∨ + ∃ x ∈ X, ∃ z ∈ X, x ≠ z ∧ R y x ∧ R y z) : + 2 * Y.card + M.card ≤ 2 * X.card := by + classical + let F : β → Finset α := fun y => X.filter (R y) + let w : α → ℕ := fun x => if x ∈ M then 1 else 2 + have hweight : ∀ y ∈ Y, 2 ≤ ∑ x ∈ F y, w x := by + intro y hy + rcases hcover y hy with ⟨x, hx, hxm, hxy⟩ | ⟨x, hx, z, hz, hxz, hxy, hzy⟩ + · have h := Finset.single_le_sum (f := w) (fun _ _ => Nat.zero_le _) + (show x ∈ F y from Finset.mem_filter.mpr ⟨hx, hxy⟩) + simpa [w, hxm] using h + · have hsub : ({x, z} : Finset α) ⊆ F y := by + intro a ha + rcases Finset.mem_insert.mp ha with rfl | ha + · exact Finset.mem_filter.mpr ⟨hx, hxy⟩ + · have haeq := Finset.mem_singleton.mp ha + subst a + exact Finset.mem_filter.mpr ⟨hz, hzy⟩ + have h := Finset.sum_le_sum_of_subset hsub (f := w) + have hxw : 1 ≤ w x := by unfold w; split_ifs <;> omega + have hzw : 1 ≤ w z := by unfold w; split_ifs <;> omega + simp only [Finset.sum_pair hxz] at h + omega + have hdisjoint : Set.PairwiseDisjoint (↑Y) F := by + intro y hy z hz hne + apply Finset.disjoint_left.mpr + intro x hxy hxz + obtain ⟨hx, hxy⟩ := Finset.mem_filter.mp hxy + obtain ⟨_, hxz⟩ := Finset.mem_filter.mp hxz + exact hne (hunique y hy z hz x hx hxy hxz) + have hsum : 2 * Y.card ≤ ∑ x ∈ X, w x := by + calc + _ = ∑ _ ∈ Y, 2 := by simp [Nat.mul_comm] + _ ≤ ∑ y ∈ Y, ∑ x ∈ F y, w x := Finset.sum_le_sum hweight + _ = ∑ x ∈ Y.biUnion F, w x := (Finset.sum_biUnion hdisjoint).symm + _ ≤ ∑ x ∈ X, w x := Finset.sum_le_sum_of_subset (by + intro x hx + obtain ⟨y, _, hxy⟩ := Finset.mem_biUnion.mp hx + exact (Finset.mem_filter.mp hxy).1) + have hfilter : X.filter (· ∈ M) = M := by + ext x + simp only [Finset.mem_filter] + exact ⟨And.right, fun hx => ⟨hM hx, hx⟩⟩ + have hindicator : (∑ x ∈ X, if x ∈ M then 1 else 0) = M.card := by + rw [← Finset.sum_filter, hfilter] + simp + have htotal : (∑ x ∈ X, w x) + M.card = 2 * X.card := by + rw [← hindicator, ← Finset.sum_add_distrib] + calc + _ = ∑ _ ∈ X, 2 := Finset.sum_congr rfl (fun x _ => by + unfold w; split_ifs <;> omega) + _ = _ := by simp [Nat.mul_comm] + omega + +/-- Every edge of a finite idempotent relation factors through a recurrent point. -/ +theorem idempotent_relation_factor {α : Type*} [Finite α] (P : SetRel α α) + (hP : P ○ P = P) {u v : α} (huv : (u, v) ∈ P) : + ∃ p, (u, p) ∈ P ∧ (p, p) ∈ P ∧ (p, v) ∈ P := by + classical + have htrans {a b c : α} (hab : (a, b) ∈ P) (hbc : (b, c) ∈ P) : + (a, c) ∈ P := hP ▸ (show (a, c) ∈ P ○ P from ⟨b, hab, hbc⟩) + let T := {x : α // (x, v) ∈ P} + have hnext : ∀ x : T, ∃ y : T, (x.val, y.val) ∈ P := by + intro x + have hx : (x.val, v) ∈ P ○ P := hP.symm ▸ x.property + obtain ⟨y, hxy, hyv⟩ := hx + exact ⟨⟨y, hyv⟩, hxy⟩ + choose f hf using hnext + have hpath (x : T) (n : ℕ) : (x.val, (f^[n + 1] x).val) ∈ P := by + induction n with + | zero => exact hf x + | succ n ih => + rw [Function.iterate_succ_apply'] + exact htrans ih (hf _) + let x : T := ⟨u, huv⟩ + obtain ⟨i, j, hij, heq⟩ := Set.finite_univ.exists_lt_map_eq_of_forall_mem + (fun n : ℕ => Set.mem_univ (f^[n] x)) + obtain ⟨k, rfl⟩ := Nat.exists_eq_add_of_lt hij + have hloop := hpath (f^[i] x) k + rw [← Function.iterate_add_apply, Nat.add_comm] at hloop + rw [← Nat.add_assoc, ← heq] at hloop + refine ⟨(f^[i] x).val, ?_, hloop, (f^[i] x).property⟩ + cases i with + | zero => exact hloop + | succ i => exact hpath x i + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Representation.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Representation.lean new file mode 100644 index 0000000000000000000000000000000000000000..febd17a24a3621ccde160e8c7671ddfa2cfb95b0 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Representation.lean @@ -0,0 +1,83 @@ +import OAI.Combinatorics.TwoWayAutomata.Tape + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Every 2NFA has an exact diagram representation with one additional state label. -/ +theorem TwoNFA.diagram_representation {Sigma : Type*} {s : ℕ} (A : TwoNFA Sigma (Fin s)) : + ∃ τ : List Sigma → Diagram (s + 1), ∃ l r : Diagram (s + 1), ∃ a b : Fin (s + 1), + τ [] = Diagram.identity (s + 1) ∧ + (∀ u v, τ (u ++ v) = Diagram.comp (τ u) (τ v)) ∧ + (∀ w, w ∈ A.language ↔ (a, b) ∈ (Diagram.comp (Diagram.comp l (τ w)) r).forward) := by + let e := finSuccEquiv s + let moves (symbol : Fin 2 ⊕ Sigma) (move : Fin 3) : SetRel (Fin (s + 1)) (Fin (s + 1)) := + {qr | (e qr.1, e qr.2) ∈ A.augmentedMove symbol move} + let d (symbol : Fin 2 ⊕ Sigma) := cellDiagram (s + 1) + (moves symbol 1) (moves symbol 2) (moves symbol 0) + let a := e.symm (some A.initial) + let b := e.symm none + refine ⟨wordDiagram (fun symbol => d (Sum.inr symbol)), d (Sum.inl 0), d (Sum.inl 1), + a, b, wordDiagram_nil _, wordDiagram_append _, ?_⟩ + intro w + let cells : List (Fin 2 ⊕ Sigma) := Sum.inl 0 :: w.map Sum.inr + let ec : (Fin (s + 1) × Fin (cells.length + 2)) ≃ + (Option (Fin s) × Fin (w.length + 3)) := + Equiv.prodCongr e (finCongr (by simp [cells])) + have hread (c : ChainNode (s + 1) cells.length) (p : Fin (w.length + 2)) + (hp : c.position = p.val) : chainSymbol cells (Sum.inl 1) c = tapeSymbol w p := by + rw [chainSymbol_getElem] + simpa only [cells, List.cons_append, hp] using marked_getElem w p + have hstep (x y : Fin (s + 1) × Fin (cells.length + 2)) : + (ec x, ec y) ∈ A.augmentedStep w ↔ (x, y) ∈ chainStep moves cells (Sum.inl 1) := by + rw [A.augmentedStep_local_iff] + constructor + · rintro ⟨p, hp, move, hmove, hdisp⟩ + have hpx : p.val = x.2.val := (congrArg Fin.val hp).symm + have hbound : p.val ≤ cells.length := by simp [cells]; omega + obtain ⟨c, _, hc⟩ := ChainNode.coordinates_surjective x.1 p.val hbound + refine ⟨c, hpx.symm.trans hc.symm, move, ?_, ?_⟩ + · change (e x.1, e y.1) ∈ A.augmentedMove (chainSymbol cells (Sum.inl 1) c) move + rw [hread c p hc] + exact hmove + · change (y.2.val : ℤ) = (p.val : ℤ) + (move.val : ℤ) - 1 at hdisp + rwa [hpx] at hdisp + · rintro ⟨c, hpos, move, hmove, hdisp⟩ + have hc : c.position < w.length + 2 := by + have hbound := c.position_le + simp only [cells, List.length_cons, List.length_map] at hbound + omega + let p : Fin (w.length + 2) := ⟨c.position, hc⟩ + refine ⟨p, Fin.ext hpos, move, ?_, ?_⟩ + · change (e x.1, e y.1) ∈ A.augmentedMove (tapeSymbol w p) move + rw [← hread c p rfl] + exact hmove + · change (y.2.val : ℤ) = (c.position : ℤ) + (move.val : ℤ) - 1 + rwa [hpos] at hdisp + have hstart : ec (a, ⟨0, by omega⟩) = + TwoNFA.augmentConfig (A.initial, ⟨0, by omega⟩) := + Prod.ext (e.apply_symm_apply _) (Fin.ext rfl) + have hend : ec (b, ⟨cells.length + 1, by omega⟩) = + (none, ⟨w.length + 2, by omega⟩) := by + apply Prod.ext (e.apply_symm_apply _) + apply Fin.ext + change cells.length + 1 = w.length + 2 + simp [cells] + have hpaths := relStar_equiv_iff ec (chainStep moves cells (Sum.inl 1)) + (A.augmentedStep w) hstep (a, ⟨0, by omega⟩) (b, ⟨cells.length + 1, by omega⟩) + rw [hstart, hend, ← A.augmented_acceptance w] at hpaths + rw [hpaths, chainStep_recognition, chainNetwork_summary] + have hprod : wordDiagram d (cells ++ [Sum.inl 1]) = + Diagram.comp (Diagram.comp (d (Sum.inl 0)) + (wordDiagram (fun symbol => d (Sum.inr symbol)) w)) (d (Sum.inl 1)) := by + simp only [wordDiagram, cells, List.cons_append, List.map_cons, List.map_append, + List.prod_cons, List.prod_append, List.map_map, List.map_nil, List.prod_nil, mul_one] + exact (mul_assoc _ _ _).symm + change (a, b) ∈ (wordDiagram d (cells ++ [Sum.inl 1])).forward ↔ _ + rw [hprod] + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Retained.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Retained.lean new file mode 100644 index 0000000000000000000000000000000000000000..1639490fa603240d3fc808ed66f81007298555d6 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Retained.lean @@ -0,0 +1,165 @@ +import OAI.Combinatorics.TwoWayAutomata.Amplification + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Select the two 64-point families and a distinct hub. -/ +theorem exists_hub_families {H : Type*} [Finite H] (hh : 129 ≤ Nat.card H) : + ∃ u v : Fin 64 ↪ H, ∃ z : H, + (∀ i j, u i ≠ v j) ∧ (∀ i, u i ≠ z) ∧ (∀ j, v j ≠ z) := by + classical + let := Fintype.ofFinite H + obtain ⟨e⟩ : Nonempty (Fin 129 ↪ H) := Function.Embedding.nonempty_of_card_le (by simpa using hh) + let u : Fin 64 ↪ H := + ⟨fun i => e ⟨i.val, by omega⟩, + fun a b h => Fin.ext (congrArg (fun k : Fin 129 => k.val) (e.injective h))⟩ + let v : Fin 64 ↪ H := ⟨fun i => e ⟨64 + i.val, by omega⟩, by + intro a b h + have he := congrArg (fun k : Fin 129 => k.val) (e.injective h) + apply Fin.ext + change 64 + a.val = 64 + b.val at he + omega⟩ + refine ⟨u, v, e 128, ?_, ?_, ?_⟩ + · intro i j h + have he := congrArg (fun k : Fin 129 => k.val) (e.injective h) + change i.val = 64 + j.val at he + omega + · intro i h + have he := congrArg (fun k : Fin 129 => k.val) (e.injective h) + change i.val = 128 at he + omega + · intro j h + have he := congrArg (fun k : Fin 129 => k.val) (e.injective h) + change 64 + j.val = 128 at he + omega + +/-- Retaining a new pair and all external points removes exactly 127 points. -/ +theorem retained_pair_set {H : Type*} [Finite H] (U V : Set H) + (hU : U.ncard = 64) (hV : V.ncard = 64) (hUV : Disjoint U V) + (z x y : H) (hzU : z ∉ U) (hzV : z ∉ V) (hx : x ∈ U) (hy : y ∈ V) : + ∃ F : Set H, Nat.card F = Nat.card H - 127 ∧ x ∈ F ∧ y ∈ F ∧ z ∉ F ∧ + (∀ a ∈ U, a ∈ F → a = x) ∧ (∀ b ∈ V, b ∈ F → b = y) := by + classical + let S := insert z (U ∪ V) + let F := ({x, y} : Set H) ∪ Sᶜ + have hxy : x ≠ y := fun h => Set.disjoint_left.mp hUV (h ▸ hx) hy + have hS : S.ncard = 129 := by + rw [Set.ncard_insert_of_notMem (by simpa using And.intro hzU hzV), + Set.ncard_union_eq hUV, hU, hV] + have hpair : ({x, y} : Set H).ncard = 2 := by simp [hxy] + have hpS : ({x, y} : Set H) ⊆ S := by + rintro a (rfl | rfl) <;> simp [S, hx, hy] + have hdisj : Disjoint ({x, y} : Set H) Sᶜ := Set.disjoint_left.mpr (fun _ ha hn => hn (hpS ha)) + have hF : F.ncard = Nat.card H - 127 := by + rw [Set.ncard_union_eq hdisj, hpair, Set.ncard_compl, hS] + have hh : 129 ≤ Nat.card H := by simpa only [hS] using Set.ncard_le_card S + omega + refine ⟨F, by simpa only [Nat.card_coe_set_eq] using hF, Or.inl (Or.inl rfl), + Or.inl (Or.inr rfl), ?_, ?_, ?_⟩ + · rintro (h | h) + · rcases h with rfl | rfl + · exact hzU hx + · exact hzV hy + · exact h (by simp [S]) + · intro a ha hF + rcases hF with (rfl | rfl) | hout + · rfl + · exact (Set.disjoint_left.mp hUV ha hy).elim + · exact (hout (by simp [S, ha])).elim + · intro b hb hF + rcases hF with (rfl | rfl) | hout + · exact (Set.disjoint_left.mp hUV hx hb).elim + · rfl + · exact (hout (by simp [S, hb])).elim + +/-- Identity sandwiches restrict exactly the two endpoints. -/ +theorem mem_restricted_sandwich {H : Type*} (F : Set H) (R : SetRel H H) (a b : H) : + (a, b) ∈ restrictedIdentity F ○ R ○ restrictedIdentity F ↔ + a ∈ F ∧ b ∈ F ∧ (a, b) ∈ R := by + simp only [SetRel.comp, restrictedIdentity, Set.mem_ofPred_eq] + aesop + +/-- Before its turn, the selected pair is absent from the retained restriction. -/ +theorem retained_relation_identity {H : Type*} (U V F : Set H) (z x y : H) + (hz : z ∉ F) (hx : ∀ a ∈ U, a ∈ F → a = x) (hy : ∀ b ∈ V, b ∈ F → b = y) + (E : SetRel H H) (hE : E ⊆ U ×ˢ V) (hnew : (x, y) ∉ E) : + restrictedIdentity F ○ (restrictedIdentity {a | a ≠ z} ∪ E) ○ restrictedIdentity F = + restrictedIdentity F := by + ext ⟨a, b⟩ + rw [mem_restricted_sandwich] + constructor + · rintro ⟨ha, hb, hid | hedge⟩ + · exact ⟨hid.1, ha⟩ + · exact (hnew (by simpa only [hx a (hE hedge).1 ha, hy b (hE hedge).2 hb] using hedge)).elim + · rintro ⟨rfl, ha⟩ + exact ⟨ha, ha, Or.inl ⟨rfl, fun h => hz (h ▸ ha)⟩⟩ + +/-- At its turn, restriction adds precisely the selected pair to the retained identity. -/ +theorem retained_relation_pair {H : Type*} (P : SetRel H H) (F : Set H) + (hP : restrictedIdentity F ○ P ○ restrictedIdentity F = restrictedIdentity F) + (x y : H) (hx : x ∈ F) (hy : y ∈ F) : + restrictedIdentity F ○ (P ∪ {(x, y)}) ○ restrictedIdentity F = + restrictedIdentity F ∪ {(x, y)} := by + rw [relComp_union, union_relComp, hP] + congr 1 + ext ⟨a, b⟩ + rw [mem_restricted_sandwich] + simp only [Set.mem_singleton_iff, Prod.mk.injEq] + aesop + +/-- The target doubles whenever the induction removes 127 points. -/ +theorem lossBound_step {h : ℕ} (hh : 129 ≤ h) : lossBound h = 2 * lossBound (h - 127) := by + have hexp : (h - 2) / 127 = (h - 127 - 2) / 127 + 1 := by omega + simp only [lossBound, hexp, pow_succ, Nat.mul_comm] + +/-- The numerical conclusion of summing the 4096 inductive lower bounds. -/ +theorem amplification_arithmetic {h c : ℕ} (hh : 129 ≤ h) (δ : Fin 4096 → ℕ) + (htotal : ∑ j, δ j ≤ 1024 * c) + (hstep : ∀ j, lossBound (h - 127) ≤ 2 * δ j) : lossBound h ≤ c := by + have hsum := Finset.sum_le_sum (fun j (_ : j ∈ (Finset.univ : Finset (Fin 4096))) => hstep j) + have hbound : 4096 * lossBound (h - 127) ≤ 2 * ∑ j, δ j := by + simpa only [Finset.sum_const, Finset.card_univ, Fintype.card_fin, smul_eq_mul, + ← Finset.mul_sum] using hsum + rw [lossBound_step hh] + omega + +/-- The base case detects the off-diagonal pair using rectangle coverage and order reversal. -/ +theorem single_edge_base {A H : Type*} [Monoid A] {m : ℕ} + (ι : A →ₙ* Diagram m) (φ : A →* RelationMonoid H) + (hreverse : ∀ a b, (ι a, ι b) ∈ Diagram.inclusion m → (φ b).toRel ⊆ (φ a).toRel) + (a : A) (x y : H) (hxy : x ≠ y) + (himage : (φ a).toRel = SetRel.id ∪ {(x, y)}) (hh : Nat.card H ≤ 128) : + lossBound (Nat.card H) ≤ ((ι 1).missing (ι a)).ncard := by + have hsmall : lossBound (Nat.card H) = 1 := by + have he : (Nat.card H - 2) / 127 = 0 := by omega + simp [lossBound, he] + rw [hsmall] + by_contra h + have hempty : (ι 1).missing (ι a) = ∅ := (Set.ncard_eq_zero).mp (by omega) + have hinclusion := Diagram.inclusion_of_missing_empty (ι 1) (ι a) + (map_identity_corner ι 1).1 (map_identity_corner ι a) hempty + have hedge : (x, y) ∈ (φ a).toRel := by rw [himage]; exact Or.inr rfl + have hid := hreverse 1 a hinclusion hedge + rw [map_one, RelationMonoid.toRel_one] at hid + exact hxy hid + +/-- Every positive power of a sandwich is absorbed by its idempotent outer factor. -/ +theorem sandwich_power_absorption {A : Type*} [Monoid A] (b a : A) + (hb : IsIdempotentElem b) (k : ℕ) : + b * (b * a * b) ^ (k + 1) = (b * a * b) ^ (k + 1) ∧ + (b * a * b) ^ (k + 1) * b = (b * a * b) ^ (k + 1) := by + have hl : b * (b * a * b) = b * a * b := by rw [← mul_assoc, ← mul_assoc, hb.eq] + have hr : b * a * b * b = b * a * b := by rw [mul_assoc, hb.eq] + induction k with + | zero => simpa using And.intro hl hr + | succ k ih => + constructor + · rw [pow_succ, ← mul_assoc, ih.1] + · rw [pow_succ, mul_assoc, hr] + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Source.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Source.lean new file mode 100644 index 0000000000000000000000000000000000000000..4fb472915701c2e78aa3d20d2e8ab1271c2380da --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Source.lean @@ -0,0 +1,178 @@ +import OAI.Combinatorics.TwoWayAutomata.Normalization +import OAI.Combinatorics.TwoWayAutomata.RelationMonoid + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Relation product for an input word; the empty product is the identity. -/ +def relationProduct {H : Type*} (w : List (SetRel H H)) : SetRel H H := + w.foldr SetRel.comp SetRel.id + +theorem relationProduct_append {H : Type*} (u v : List (SetRel H H)) : + relationProduct (u ++ v) = relationProduct u ○ relationProduct v := by + induction u with + | nil => simp [relationProduct] + | cons R u ih => simpa [relationProduct, SetRel.comp_assoc] using congrArg (R ○ ·) ih + +/-- The source language consists of the words with nonempty relation product. -/ +def sourceLanguage (H : Type*) : Set (List (SetRel H H)) := + {w | (relationProduct w).Nonempty} + +/-- The explicit source machine: `none` starts, `some none` accepts, and + `some (some h)` is the state labelled by `h`. -/ +def sourceAutomaton (H : Type*) : TwoNFA (SetRel H H) (Option (Option H)) where + initial := none + accepting := {some none} + transition state symbol := match state, symbol with + | none, .inl marker => + if marker = 0 then {target | ∃ h, target = (some (some h), 2)} else ∅ + | some (some p), .inr R => + {target | ∃ q, (p, q) ∈ R ∧ target = (some (some q), 2)} + | some (some _), .inl marker => + if marker = 1 then {(some none, 1)} else ∅ + | _, _ => ∅ + +/-- Configurations from which the explicit source machine has a suffix witness. -/ +def sourceProductive {H : Type*} (w : List (SetRel H H)) : + Set (Option (Option H) × Fin (w.length + 2)) := + {c | match c.1 with + | none => c.2.val = 0 ∧ (relationProduct w).Nonempty + | some none => True + | some (some p) => 1 ≤ c.2.val ∧ ∃ q, (p, q) ∈ relationProduct (w.drop (c.2.val - 1))} + +theorem sourceProductive_of_step {H : Type*} (w : List (SetRel H H)) + {c d : Option (Option H) × Fin (w.length + 2)} + (hstep : (c, d) ∈ (sourceAutomaton H).step w) (hd : d ∈ sourceProductive w) : + c ∈ sourceProductive w := by + obtain ⟨cs, ci⟩ := c + obtain ⟨ds, di⟩ := d + obtain ⟨move, htransition, hpos⟩ := hstep + change (ds, move) ∈ (sourceAutomaton H).transition cs (tapeSymbol w ci) at htransition + change (di.val : ℤ) = (ci.val : ℤ) + (move.val : ℤ) - 1 at hpos + cases cs with + | none => + by_cases hleft : ci.val = 0 + · have hex : ∃ p, ds = some (some p) ∧ move = 2 := by + simpa [sourceAutomaton, tapeSymbol, hleft] using htransition + obtain ⟨p, hds, hmove⟩ := hex + subst ds + subst move + have hdi : di.val = 1 := by norm_num at hpos; omega + obtain ⟨_, q, hpq⟩ := hd + exact ⟨hleft, (p, q), by simpa [hdi] using hpq⟩ + · by_cases hright : ci.val = w.length + 1 <;> + simp [sourceAutomaton, tapeSymbol, hleft, hright] at htransition + | some state => + cases state with + | none => + change (ds, move) ∈ (∅ : Set (Option (Option H) × Fin 3)) at htransition + exact htransition.elim + | some p => + by_cases hleft : ci.val = 0 + · simp [sourceAutomaton, tapeSymbol, hleft] at htransition + · by_cases hright : ci.val = w.length + 1 + · change 1 ≤ ci.val ∧ ∃ q, (p, q) ∈ relationProduct (w.drop (ci.val - 1)) + refine ⟨by omega, p, ?_⟩ + simp [hright, relationProduct] + · have hex : ∃ q, (p, q) ∈ w[ci.val - 1]'(by omega) ∧ + ds = some (some q) ∧ move = 2 := by + simpa [sourceAutomaton, tapeSymbol, hleft, hright] using htransition + obtain ⟨q, hpq, hds, hmove⟩ := hex + subst ds + subst move + have hdi : di.val = ci.val + 1 := by norm_num at hpos; omega + obtain ⟨_, r, hqr⟩ := hd + change 1 ≤ ci.val ∧ ∃ r, (p, r) ∈ relationProduct (w.drop (ci.val - 1)) + refine ⟨by omega, r, ?_⟩ + rw [List.drop_eq_getElem_cons (show ci.val - 1 < w.length from by omega)] + change (p, r) ∈ w[ci.val - 1] ○ relationProduct (w.drop (ci.val - 1 + 1)) + refine ⟨q, hpq, ?_⟩ + simpa only [hdi, Nat.add_sub_cancel, + Nat.sub_add_cancel (show 1 ≤ ci.val by omega)] using hqr + +theorem sourceAutomaton_sound {H : Type*} (w : List (SetRel H H)) + (hw : w ∈ (sourceAutomaton H).language) : (relationProduct w).Nonempty := by + obtain ⟨c, hc, hrun⟩ := hw + have hproductive : c ∈ sourceProductive w := by + change c.1 ∈ ({some none} : Set (Option (Option H))) at hc + have hstate : c.1 = some none := hc + simp only [sourceProductive, Set.mem_ofPred_eq, hstate] + have hback : ∀ {c d}, (c, d) ∈ relStar ((sourceAutomaton H).step w) → + d ∈ sourceProductive w → c ∈ sourceProductive w := by + intro c d h + induction h with + | refl => exact id + | tail _ hstep ih => exact fun hd => ih (sourceProductive_of_step w hstep hd) + exact (hback hrun hproductive).2 + +theorem sourceAutomaton_suffix_run {H : Type*} (w : List (SetRel H H)) + (u v : List (SetRel H H)) (hw : w = u ++ v) {p q : H} + (hpq : (p, q) ∈ relationProduct v) : + ((some (some p), ⟨u.length + 1, by simp [hw]⟩), + (some none, ⟨w.length + 1, by omega⟩)) ∈ relStar ((sourceAutomaton H).step w) := by + induction v generalizing u p with + | nil => + have hlen : w.length = u.length := by simp [hw] + apply Relation.ReflTransGen.single + refine ⟨1, ?_, ?_⟩ + · simp [sourceAutomaton, tapeSymbol, hlen] + · simp [hlen] + | cons R v ih => + obtain ⟨r, hpr, hrq⟩ := hpq + have hlen : w.length = u.length + (v.length + 1) := by simp [hw] + have hw' : w = (u ++ [R]) ++ v := by + simpa only [List.append_assoc, List.singleton_append] using hw + have htail := ih (u ++ [R]) hw' hrq + apply htail.head + refine ⟨2, ?_, ?_⟩ + · have hsymbol : tapeSymbol w ⟨u.length + 1, by omega⟩ = Sum.inr R := by + simp [tapeSymbol, hw, List.getElem_append_right] + change (some (some r), (2 : Fin 3)) ∈ + (sourceAutomaton H).transition (some (some p)) (tapeSymbol w _) + rw [hsymbol] + exact ⟨r, hpr, rfl⟩ + · simp [List.length_append] + omega + +/-- The explicit machine recognizes nonempty relation products. -/ +theorem sourceAutomaton_language (H : Type*) : + (sourceAutomaton H).language = sourceLanguage H := by + ext w + constructor + · exact sourceAutomaton_sound w + · rintro ⟨⟨p, q⟩, hpq⟩ + refine ⟨(some none, ⟨w.length + 1, by omega⟩), rfl, ?_⟩ + have hrun := sourceAutomaton_suffix_run w [] w (by simp) hpq + apply hrun.head + refine ⟨2, ?_, ?_⟩ + · exact ⟨p, rfl⟩ + · norm_num + +/-- The source language on `h` labels is recognized using exactly `h + 2` states. -/ +theorem sourceLanguage_state_bound (H : Type*) [Fintype H] : + ∃ A : TwoNFA (SetRel H H) (Fin (Fintype.card H + 2)), A.language = sourceLanguage H := by + classical + let e : Option (Option H) ≃ Fin (Fintype.card H + 2) := + (Fintype.equivFin _).trans (finCongr (by simp)) + refine ⟨(sourceAutomaton H).reindex e, ?_⟩ + rw [TwoNFA.reindex_language, sourceAutomaton_language] + +/-- Singleton letters on the two sides test one edge of a word's relation product. -/ +theorem singleton_word_context {H : Type*} (w : List (SetRel H H)) (p q : H) : + (relationProduct ([restrictedIdentity {p}] ++ w ++ [restrictedIdentity {q}])).Nonempty ↔ + (p, q) ∈ relationProduct w := by + rw [relationProduct_append, relationProduct_append] + simp only [relationProduct, List.foldr_cons, List.foldr_nil, SetRel.comp_id] + rw [singleton_context] + constructor + · rintro ⟨⟨x, y⟩, _, _, hpq⟩ + exact hpq + · intro hpq + exact ⟨(p, q), rfl, rfl, hpq⟩ + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Tape.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Tape.lean new file mode 100644 index 0000000000000000000000000000000000000000..13ee6b7f53b7b75a5f8dda965eb65f38d2cfe2d1 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Tape.lean @@ -0,0 +1,173 @@ +import OAI.Combinatorics.TwoWayAutomata.Chain +import OAI.Combinatorics.TwoWayAutomata.Normalization + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +/-- Chain symbols agree with ordinary zero-based lookup of the nonempty symbol list. -/ +theorem chainSymbol_getElem {Sigma : Type*} {m : ℕ} (w : List Sigma) (last : Sigma) + (c : ChainNode m w.length) : + chainSymbol w last c = (w ++ [last])[c.position]'(by + have := c.position_le + simp only [List.length_append, List.length_singleton] + omega) := by + induction w with + | nil => rfl + | cons a w ih => + cases c with + | inl q => rfl + | inr c => simpa only [chainSymbol, ChainNode.position, List.cons_append, + List.getElem_cons_succ] using ih c + +/-- Lookup of the explicitly marked list agrees with the automaton's tape lookup. -/ +theorem marked_getElem {Sigma : Type*} (w : List Sigma) (p : Fin (w.length + 2)) : + ((Sum.inl 0 : Fin 2 ⊕ Sigma) :: (w.map Sum.inr ++ [Sum.inl 1]))[p.val]'(by simp; omega) = + tapeSymbol w p := by + obtain ⟨p, hp⟩ := p + cases p with + | zero => rfl + | succ p => + by_cases h : p = w.length + · subst p + simp [tapeSymbol] + · have hlt : p < w.length := by omega + rw [List.getElem_cons_succ, List.getElem_append_left (by simpa using hlt), + List.getElem_map] + simp [tapeSymbol, h] + +/-- Convert the recursively joined cells and one terminal exit into tape coordinates. -/ +noncomputable def ChainNode.exitEquiv (m n : ℕ) : + (ChainNode m n ⊕ Fin m) ≃ (Fin m × Fin (n + 2)) := + Equiv.ofBijective (fun c => match c with + | .inl v => (v.label, ⟨v.position, by have := v.position_le; omega⟩) + | .inr q => (q, ⟨n + 1, by omega⟩)) (by + constructor + · intro c d h + have hq := congrArg (fun p : Fin m × Fin (n + 2) => p.1) h + have hp := congrArg (fun p : Fin m × Fin (n + 2) => p.2.val) h + cases c with + | inl c => + cases d with + | inl d => exact congrArg Sum.inl (c.coordinates_injective d hp hq) + | inr q => have := c.position_le; dsimp at hp; omega + | inr q => + cases d with + | inl d => have := d.position_le; dsimp at hp; omega + | inr r => exact congrArg Sum.inr hq + · rintro ⟨q, p⟩ + by_cases hp : p.val ≤ n + · obtain ⟨c, hq, hc⟩ := ChainNode.coordinates_surjective q p.val hp + exact ⟨Sum.inl c, Prod.ext hq (Fin.ext hc)⟩ + · exact ⟨Sum.inr q, Prod.ext rfl (Fin.ext (by change n + 1 = p.val; omega))⟩) + +/-- A graph equivalence transports finite paths in both directions. -/ +theorem relStar_equiv_iff {V W : Type*} (e : V ≃ W) (R : SetRel V V) (S : SetRel W W) + (h : ∀ x y, (e x, e y) ∈ S ↔ (x, y) ∈ R) (x y : V) : + (e x, e y) ∈ relStar S ↔ (x, y) ∈ relStar R := by + constructor + · intro hrun + have hlift := hrun.lift e.symm (p := fun a b => (a, b) ∈ R) (by + intro a b hab + apply (h (e.symm a) (e.symm b)).mp + simpa only [Equiv.apply_symm_apply] using hab) + change Relation.ReflTransGen (fun a b => (a, b) ∈ R) x y + simpa only [Function.onFun, Equiv.symm_apply_apply] using hlift + · exact fun hrun => hrun.lift e (fun a b hab => (h a b).mpr hab) + +/-- A terminal block with no outgoing edges is reached by an internal path and one exit. -/ +theorem blockRelation_terminal {V Q : Type*} (E : SetRel V V) (O : SetRel V Q) + (c : V) (q : Q) : + (Sum.inl c, Sum.inr q) ∈ relStar (blockRelation E O ∅ ∅) ↔ + (c, q) ∈ relStar E ○ O := by + rw [blockRelation_star_cross] + simp only [SetRel.empty_comp, Set.empty_union, relStar_empty, SetRel.comp_id] + +/-- A cell's scanned symbol depends only on its position. -/ +theorem chainSymbol_position {Sigma : Type*} {m : ℕ} (w : List Sigma) (last : Sigma) + (c d : ChainNode m w.length) (hp : c.position = d.position) : + chainSymbol w last c = chainSymbol w last d := by + rw [chainSymbol_getElem, chainSymbol_getElem] + simp only [hp] + +/-- Local moves on the bounded tape, with a final terminal position beyond the last cell. -/ +def chainStep {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) : + SetRel (Fin m × Fin (w.length + 2)) (Fin m × Fin (w.length + 2)) := + {cd | ∃ c : ChainNode m w.length, cd.1.2.val = c.position ∧ ∃ move : Fin 3, + (cd.1.1, cd.2.1) ∈ moves (chainSymbol w last c) move ∧ + (cd.2.2.val : ℤ) = (cd.1.2.val : ℤ) + (move.val : ℤ) - 1} + +/-- The joined network with terminal exits has exactly the bounded tape's local edges. -/ +theorem chainStep_exitEquiv {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) + (x y : ChainNode m w.length ⊕ Fin m) : + (ChainNode.exitEquiv m w.length x, ChainNode.exitEquiv m w.length y) ∈ + chainStep moves w last ↔ + (x, y) ∈ blockRelation (chainNetwork moves w last).edge + (chainNetwork moves w last).rightOut ∅ ∅ := by + cases x with + | inl c => + cases y with + | inl d => + change (∃ a : ChainNode m w.length, c.position = a.position ∧ ∃ move : Fin 3, + (c.label, d.label) ∈ moves (chainSymbol w last a) move ∧ + (d.position : ℤ) = (c.position : ℤ) + (move.val : ℤ) - 1) ↔ + (c, d) ∈ (chainNetwork moves w last).edge + rw [chainNetwork_edges] + constructor + · rintro ⟨a, hp, move, h, hd⟩ + exact ⟨move, (chainSymbol_position w last a c hp.symm) ▸ h, hd⟩ + · rintro ⟨move, h, hd⟩ + exact ⟨c, rfl, move, h, hd⟩ + | inr q => + change (∃ a : ChainNode m w.length, c.position = a.position ∧ ∃ move : Fin 3, + (c.label, q) ∈ moves (chainSymbol w last a) move ∧ + ((w.length + 1 : ℕ) : ℤ) = (c.position : ℤ) + (move.val : ℤ) - 1) ↔ + (c, q) ∈ (chainNetwork moves w last).rightOut + rw [(chainNetwork_exits moves w last c q).2] + constructor + · rintro ⟨a, hp, move, h, hd⟩ + have hc := c.position_le + have hm : move = 2 := Fin.ext (by have := move.isLt; omega) + refine ⟨by have := move.isLt; omega, ?_⟩ + simpa only [← chainSymbol_position w last c a hp, hm] using h + · rintro ⟨hp, h⟩ + exact ⟨c, rfl, 2, h, by rw [hp]; push_cast; omega⟩ + | inr q => + change (∃ a : ChainNode m w.length, w.length + 1 = a.position ∧ _) ↔ _ + constructor + · rintro ⟨a, hp, _⟩ + have := a.position_le + omega + · intro h + cases y <;> exact h.elim + +/-- The designated forward edge of a diagram product is exactly a successful tape run. -/ +theorem chainStep_recognition {Sigma : Type*} {m : ℕ} + (moves : Sigma → Fin 3 → SetRel (Fin m) (Fin m)) (w : List Sigma) (last : Sigma) + (q r : Fin m) : + ((q, ⟨0, by omega⟩), (r, ⟨w.length + 1, by omega⟩)) ∈ relStar (chainStep moves w last) ↔ + (q, r) ∈ (chainNetwork moves w last).summary.forward := by + obtain ⟨c, hq, hp⟩ := ChainNode.coordinates_surjective (n := w.length) q 0 (Nat.zero_le _) + have hstart : ChainNode.exitEquiv m w.length (Sum.inl c) = (q, ⟨0, by omega⟩) := + Prod.ext hq (Fin.ext hp) + have hend : ChainNode.exitEquiv m w.length (Sum.inr r) = + (r, ⟨w.length + 1, by omega⟩) := rfl + rw [← hstart, ← hend, relStar_equiv_iff (ChainNode.exitEquiv m w.length) + (blockRelation (chainNetwork moves w last).edge (chainNetwork moves w last).rightOut ∅ ∅) + (chainStep moves w last) (chainStep_exitEquiv moves w last), blockRelation_terminal] + constructor + · rintro ⟨d, hcd, hdr⟩ + exact ⟨d, ⟨c, (chainNetwork_entrances moves w last q c).1.mpr ⟨hp, hq.symm⟩, + hcd⟩, hdr⟩ + · rintro ⟨d, ⟨c', hqc, hcd⟩, hdr⟩ + obtain ⟨hp', hq'⟩ := (chainNetwork_entrances moves w last q c').1.mp hqc + have hc : c' = c := c'.coordinates_injective c (hp'.trans hp.symm) (hq'.symm.trans hq.symm) + exact ⟨d, hc ▸ hcd, hdr⟩ + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/TransitionBridge.lean b/lean/OAI/Combinatorics/TwoWayAutomata/TransitionBridge.lean new file mode 100644 index 0000000000000000000000000000000000000000..958f57dec4bfe3eb787c4a2645543e4fde7a07d2 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/TransitionBridge.lean @@ -0,0 +1,141 @@ +import OAI.Combinatorics.TwoWayAutomata.DeterministicCore +import OAI.Combinatorics.TwoWayAutomata.Normalization +import OAI.Combinatorics.Automata.Model + +namespace OAI + +namespace TwoWayComplementation.CompanionBridge + +open OneWayLiveness + +/-- The encodings have the same two distinct endmarkers. -/ +def encodeSymbol {Alpha : Type*} : Symbol Alpha → Fin 2 ⊕ Alpha + | .left => .inl 0 + | .letter a => .inr a + | .right => .inl 1 + +theorem encode_scanned {Alpha : Type*} (w : List Alpha) (p : Fin (w.length + 2)) : + encodeSymbol (scanned w p) = tapeSymbol w p := by + unfold scanned tapeSymbol + split_ifs <;> simp_all [encodeSymbol] <;> omega + +def encodeMove : Move → Fin 3 + | .left => 0 + | .stay => 1 + | .right => 2 + +def decodeMove (m : Fin 3) : Move := + if m = 0 then .left else if m = 1 then .stay else .right + +@[simp] theorem encode_decode (m : Fin 3) : encodeMove (decodeMove m) = m := by + fin_cases m <;> rfl + +theorem encodeMove_injective : Function.Injective encodeMove := by + intro a b h + cases a <;> cases b <;> simp_all [encodeMove] + +theorem move_rel_iff (m : Move) (i j : ℕ) : + m.Rel i j ↔ (j : ℤ) = (i : ℤ) + ((encodeMove m).val : ℤ) - 1 := by + cases m <;> simp [Move.Rel, encodeMove] <;> omega + +/-- A marker-outward transition is never usable on this bounded tape. -/ +def Guard {Alpha : Type*} (a : Symbol Alpha) (m : Move) : Prop := + (a = .left → m ≠ .left) ∧ (a = .right → m ≠ .right) + +theorem guard_of_legal_move {Alpha : Type*} (w : List Alpha) + (p r : Fin (w.length + 2)) (m : Move) (h : m.Rel p.val r.val) : + Guard (scanned w p) m := by + constructor + · intro hs hm + subst m + have hp : p.val = 0 := (tapeSymbol_left_iff w p).mp (by + rw [← encode_scanned, hs]; rfl) + simp only [Move.Rel] at h + omega + · intro hs hm + subst m + have hp : p.val = w.length + 1 := (tapeSymbol_right_iff w p).mp (by + rw [← encode_scanned, hs]; rfl) + simp only [Move.Rel] at h + omega + +/-- Option-valued form of a partial transition. Only the single allowed element is selected. -/ +noncomputable def select {X : Type*} (S : Set X) : Option X := by + classical + exact if h : S.Nonempty then some h.choose else none + +theorem select_eq_some_iff {X : Type*} (S : Set X) (hS : S.Subsingleton) (x : X) : + select S = some x ↔ x ∈ S := by + classical + unfold select + split_ifs with h + · simp only [Option.some.injEq] + exact ⟨fun hx => hx ▸ h.choose_spec, fun hx => hS h.choose_spec hx⟩ + · simp only [false_iff] + exact fun hx => h ⟨x, hx⟩ + +/-- Recode moves and remove only impossible outward marker transitions. -/ +def convertedTransitions {Alpha : Type*} {s : ℕ} (A : TwoNFA Alpha (Fin s)) + (q : Fin s) (a : Symbol Alpha) : Set (Fin s × Move) := + {t | (t.1, encodeMove t.2) ∈ A.transition q (encodeSymbol a) ∧ Guard a t.2} + +theorem convertedTransitions_subsingleton {Alpha : Type*} {s : ℕ} + (A : TwoNFA Alpha (Fin s)) (hA : A.Deterministic) (q : Fin s) (a : Symbol Alpha) : + (convertedTransitions A q a).Subsingleton := by + intro x hx y hy + have h := hA q (encodeSymbol a) hx.1 hy.1 + apply Prod.ext + · exact congrArg (fun t : Fin s × Fin 3 => t.1) h + · exact encodeMove_injective (congrArg (fun t : Fin s × Fin 3 => t.2) h) + +/-- A deterministic machine with exactly the same states and finite accepting runs. +No halting or totality condition is introduced. -/ +noncomputable def toDMachine {Alpha : Type*} {s : ℕ} + (A : TwoNFA Alpha (Fin s)) (hA : A.Deterministic) : DMachine Alpha s where + initial := A.initial + accepting := A.accepting + transition q a := select (convertedTransitions A q a) + left_boundary q q' he := by + have h := (select_eq_some_iff _ (convertedTransitions_subsingleton A hA q .left) _).mp he + exact h.2.1 rfl rfl + right_boundary q q' he := by + have h := (select_eq_some_iff _ (convertedTransitions_subsingleton A hA q .right) _).mp he + exact h.2.2 rfl rfl + +theorem toDMachine_step {Alpha : Type*} {s : ℕ} + (A : TwoNFA Alpha (Fin s)) (hA : A.Deterministic) (w : List Alpha) + (c d : Fin s × Fin (w.length + 2)) : + (toDMachine A hA).Step w (c.2, c.1) (d.2, d.1) ↔ (c, d) ∈ A.step w := by + change (∃ m, select (convertedTransitions A c.1 (scanned w c.2)) = some (d.1, m) ∧ + m.Rel c.2.val d.2.val) ↔ _ + simp only [select_eq_some_iff _ (convertedTransitions_subsingleton A hA _ _)] + constructor + · rintro ⟨m, hm, hrel⟩ + refine ⟨encodeMove m, ?_, (move_rel_iff m _ _).mp hrel⟩ + simpa only [encode_scanned] using hm.1 + · rintro ⟨m, hm, hrel⟩ + have hr : (decodeMove m).Rel c.2.val d.2.val := by + rw [move_rel_iff, encode_decode] + exact hrel + refine ⟨decodeMove m, ⟨?_, guard_of_legal_move w c.2 d.2 _ hr⟩, hr⟩ + simpa only [encode_decode, encode_scanned] using hm + +theorem toDMachine_accepts {Alpha : Type*} {s : ℕ} + (A : TwoNFA Alpha (Fin s)) (hA : A.Deterministic) (w : List Alpha) : + (toDMachine A hA).Accepts false w ↔ w ∈ A.language := by + constructor + · rintro ⟨⟨p, q⟩, hr, hq⟩ + refine ⟨(q, p), hq, ?_⟩ + change Relation.ReflTransGen ((toDMachine A hA).Step w) _ _ at hr + exact hr.lift (fun c => (c.2, c.1)) + (p := fun c d => (c, d) ∈ A.step w) (fun c d h => + (toDMachine_step A hA w (c.2, c.1) (d.2, d.1)).mp h) + · rintro ⟨⟨q, p⟩, hq, hr⟩ + refine ⟨(p, q), ?_, hq⟩ + exact hr.lift (fun c => (c.2, c.1)) + (p := (toDMachine A hA).Step w) (fun c d h => + (toDMachine_step A hA w c d).mpr h) + +end TwoWayComplementation.CompanionBridge + +end OAI diff --git a/lean/OAI/Combinatorics/TwoWayAutomata/Transport.lean b/lean/OAI/Combinatorics/TwoWayAutomata/Transport.lean new file mode 100644 index 0000000000000000000000000000000000000000..ca96a6812dfe2c4b1a9b4d83018fd484220424e6 --- /dev/null +++ b/lean/OAI/Combinatorics/TwoWayAutomata/Transport.lean @@ -0,0 +1,173 @@ +import OAI.Combinatorics.TwoWayAutomata.Classes + +namespace OAI + +namespace TwoWayComplementation + +open scoped SetRel + +namespace Diagram + +/-- Losing no recurrent class forces componentwise inclusion. -/ +theorem inclusion_of_missing_empty {m : ℕ} (e z : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) (hmissing : e.missing z = ∅) : (e, z) ∈ inclusion m := by + have hthrough (positive : Bool) : e.through positive ⊆ z.through positive := by + rintro ⟨u, v⟩ huv + obtain ⟨p, hpp, hp⟩ := rectangle_cover e he huv + let C : Bool × Set (Fin m) := + (positive, {y | (p, y) ∈ e.recurrence positive ∧ (y, p) ∈ e.recurrence positive}) + have hC : C ∈ e.classes := ⟨p, hpp, rfl⟩ + have hnC : C ∉ e.missing z := by rw [hmissing]; exact Set.notMem_empty _ + have hloop := (not_mem_missing_iff e z he hz hC ⟨hpp, hpp⟩).mp hnC + exact rectangle_survives e z hz hloop hp + exact ⟨hthrough true, hthrough false, corner_returns e z hz⟩ + +theorem classRectangle_subset {m : ℕ} (e : Diagram m) (he : comp e e = e) + (C : Bool × Set (Fin m)) : e.classRectangle C ⊆ e.portRelation := by + rintro ⟨⟨s, a⟩, ⟨t, b⟩⟩ ⟨hs, ht, p, _, hab⟩ + change s = !C.1 at hs + change t = C.1 at ht + subst s + subst t + exact (mem_port_through e C.1 a b).mpr (rectangle_subset e he C.1 p hab) + +theorem classRectangle_cross {m : ℕ} (e : Diagram m) (he : comp e e = e) + {C : Bool × Set (Fin m)} (hC : C ∈ e.classes) {a b c d : Bool × Fin m} + (hab : (a, b) ∈ e.classRectangle C) (hcd : (c, d) ∈ e.classRectangle C) : + (a, d) ∈ e.classRectangle C := by + obtain ⟨ha, _, p, hp, hab⟩ := hab + obtain ⟨_, hd, q, hq, hcd⟩ := hcd + obtain ⟨hpq, hqp⟩ := class_related e he hC hp hq + rw [← rectangle_eq_of_related e he hpq hqp] at hcd + exact ⟨ha, hd, p, hp, hab.1, hcd.2⟩ + +theorem classRectangle_survives {m : ℕ} (e z : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) {C : Bool × Set (Fin m)} (hC : C ∈ e.classes) + (hmissing : C ∉ e.missing z) : e.classRectangle C ⊆ z.portRelation := by + rintro ⟨⟨s, a⟩, ⟨t, b⟩⟩ ⟨hs, ht, p, hp, hab⟩ + change s = !C.1 at hs + change t = C.1 at ht + subst s + subst t + have hloop := (not_mem_missing_iff e z he hz hC hp).mp hmissing + exact (mem_port_through z C.1 a b).mpr (rectangle_survives e z hz hloop hab) + +theorem portRelation_covered {m : ℕ} (e z : Diagram m) (he : comp e e = e) + (hz : z ∈ e.corner) {a b : Bool × Fin m} (hab : (a, b) ∈ e.portRelation) : + (a, b) ∈ z.portRelation ∨ ∃ C ∈ e.missing z, (a, b) ∈ e.classRectangle C := by + classical + have hthrough (positive : Bool) {p q : Fin m} (hpq : (p, q) ∈ e.through positive) : + ((!positive, p), (positive, q)) ∈ z.portRelation ∨ + ∃ C ∈ e.missing z, ((!positive, p), (positive, q)) ∈ e.classRectangle C := by + obtain ⟨r, hrr, hrectangle⟩ := rectangle_cover e he hpq + let C : Bool × Set (Fin m) := + (positive, {x | (r, x) ∈ e.recurrence positive ∧ (x, r) ∈ e.recurrence positive}) + have hC : C ∈ e.classes := ⟨r, hrr, rfl⟩ + have hrect : ((!positive, p), (positive, q)) ∈ e.classRectangle C := + ⟨rfl, rfl, r, ⟨hrr, hrr⟩, hrectangle⟩ + by_cases hmissing : C ∈ e.missing z + · exact Or.inr ⟨C, hmissing, hrect⟩ + · exact Or.inl (classRectangle_survives e z he hz hC hmissing hrect) + obtain ⟨s, p⟩ := a + obtain ⟨t, q⟩ := b + cases s <;> cases t + · exact Or.inl ((corner_returns e z hz).1 hab) + · exact hthrough true hab + · exact hthrough false hab + · exact Or.inl ((corner_returns e z hz).2 hab) + +theorem classInfluence_cross {m : ℕ} (e d u v : Diagram m) (he : comp e e = e) + (positive : Bool) {C : Bool × Set (Fin m)} (hC : C ∈ e.classes) {a b c f : Fin m} + (hab : (a, b) ∈ e.classInfluence d u v positive C) + (hcf : (c, f) ∈ e.classInfluence d u v positive C) : + (a, f) ∈ e.classInfluence d u v positive C := by + obtain ⟨_, ⟨y, ⟨x, ⟨p, hap, hpx⟩, hxy⟩, _⟩, _⟩ := hab + obtain ⟨q, ⟨y', ⟨_, ⟨_, _, _⟩, hx'y'⟩, hy'q⟩, hqf⟩ := hcf + exact ⟨q, ⟨y', ⟨x, ⟨p, hap, hpx⟩, classRectangle_cross e he hC hxy hx'y'⟩, hy'q⟩, hqf⟩ + +theorem classInfluence_subset {m : ℕ} (e d u v : Diagram m) (he : comp e e = e) + (hcontext : comp (comp u e) v = d) (positive : Bool) (C : Bool × Set (Fin m)) : + e.classInfluence d u v positive C ⊆ d.recurrence positive := by + have hC := classRectangle_subset e he C + unfold classInfluence + calc + _ ⊆ contextEntry d u v positive ○ relStar (e.portRelation ○ bounceRelation u.right v.left) ○ + e.portRelation ○ relStar (bounceRelation u.right v.left ○ e.portRelation) ○ + contextExit d u v positive := by gcongr + _ = contextEntry d u v positive ○ e.feedback u.right v.left ○ contextExit d u v positive := by + simpa only [feedback, SetRel.comp_assoc] using + congrArg (fun R => contextEntry d u v positive ○ R ○ contextExit d u v positive) + (feedback_insert e.portRelation (bounceRelation u.right v.left)) + _ = d.recurrence positive := by rw [← context_surviving, hcontext]; rfl + +theorem transportMarked_card {m : ℕ} (e d u v : Diagram m) + (he : comp e e = e) (hd : comp d d = d) (hcontext : comp (comp u e) v = d) + (J : Set (Bool × Set (Fin m))) (hJ : J ⊆ e.classes) : + (e.transportMarked d u v J).ncard ≤ 2 * J.ncard := by + classical + have hwitness : ∀ D : e.transportMarked d u v J, ∃ C : J, + ∃ x ∈ D.val.2, (x, x) ∈ e.classInfluence d u v D.val.1 C.val := by + intro D + obtain ⟨C, hCJ, x, hx, hloop⟩ := D.property.2 + exact ⟨⟨C, hCJ⟩, x, hx, hloop⟩ + choose color hcolor using hwitness + let f : e.transportMarked d u v J → Bool × J := fun D => (D.val.1, color D) + have hinj : Function.Injective f := by + intro C D hCD + have hsign : C.val.1 = D.val.1 := congrArg (fun p : Bool × J => p.1) hCD + have hsame : color C = color D := congrArg Prod.snd hCD + obtain ⟨x, hx, hxx⟩ := hcolor C + obtain ⟨y, hy, hyy⟩ := hcolor D + rw [← hsame, ← hsign] at hyy + have hsub := classInfluence_subset e d u v he hcontext C.val.1 (color C).val + have hxy := hsub (classInfluence_cross e d u v he C.val.1 (hJ (color C).property) hxx hyy) + have hyx := hsub (classInfluence_cross e d u v he C.val.1 (hJ (color C).property) hyy hxx) + have hyC : y ∈ C.val.2 := by + rw [class_eq_of_mem d hd C.property.1 hx] + exact ⟨hxy, hyx⟩ + exact Subtype.ext (class_eq_of_common d hd C.property.1 D.property.1 hsign hyC hy) + have hBool : Nat.card Bool = 2 := by simp + simpa only [Nat.card_prod, hBool, Nat.card_coe_set_eq] using + Nat.card_le_card_of_injective f hinj + +theorem missing_context_subset_marked {m : ℕ} (e d u v z : Diagram m) + (he : comp e e = e) (hd : comp d d = d) (hcontext : comp (comp u e) v = d) + (hz : z ∈ e.corner) (J : Set (Bool × Set (Fin m))) (hJz : e.missing z ⊆ J) : + d.missing (comp (comp u z) v) ⊆ e.transportMarked d u v J := by + intro D hD + obtain ⟨x, hx⟩ := class_nonempty hD.1 + have hxx := (class_related d hd hD.1 hx hx).1 + have hloop : (x, x) ∈ d.surviving (comp (comp u e) v) D.1 := by + rw [hcontext] + exact hxx + rw [context_surviving] at hloop + obtain ⟨q, ⟨p, hentry, hfeedback⟩, hexit⟩ := hloop + have hnot : (p, q) ∉ z.feedback u.right v.left := by + intro hnew + apply hD.2 x hx + rw [context_surviving] + exact ⟨q, ⟨p, hentry, hnew⟩, hexit⟩ + obtain ⟨a, b, hpa, hab, hnotab, hbq⟩ := + feedback_lost_edge e.portRelation z.portRelation (bounceRelation u.right v.left) hfeedback hnot + obtain hnew | ⟨C, hC, hrect⟩ := portRelation_covered e z he hz hab + · exact (hnotab hnew).elim + · exact ⟨hD.1, C, hJz hC, x, hx, + q, ⟨b, ⟨a, ⟨p, hentry, hpa⟩, hrect⟩, hbq⟩, hexit⟩ + +/-- One fixed target set covers every admissible replacement. -/ +theorem transport {m : ℕ} (e d u v : Diagram m) + (he : comp e e = e) (hd : comp d d = d) (hcontext : comp (comp u e) v = d) + (J : Set (Bool × Set (Fin m))) (hJ : J ⊆ e.classes) : + ∃ J' ⊆ d.classes, J'.ncard ≤ 2 * J.ncard ∧ + ∀ z ∈ e.corner, e.missing z ⊆ J → comp (comp u z) v ∈ d.corner → + d.missing (comp (comp u z) v) ⊆ J' := by + refine ⟨e.transportMarked d u v J, fun _ h => h.1, + transportMarked_card e d u v he hd hcontext J hJ, ?_⟩ + intro z hz hJz _ + exact missing_context_subset_marked e d u v z he hd hcontext hz J hJz + +end Diagram + +end TwoWayComplementation + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualAllocationBudget.lean b/lean/OAI/Combinatorics/UniformKServer/ActualAllocationBudget.lean new file mode 100644 index 0000000000000000000000000000000000000000..2cf867a0416b105e545493fab7612ceff3e49067 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualAllocationBudget.lean @@ -0,0 +1,60 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.ActualCoordinateBudget + +namespace OAI + +noncomputable section + +/-! The concrete common-tree allocator paid by true metric movement. -/ +namespace UniformKServer.PartitionTree +open Finset FiniteProbability PilotEdits TreeAllocationMovement +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} +local instance ixAB (m : ℕ) : DecidableEq (Fin m) := fun a b=>Classical.propDecidable (a=b) +local instance pairAB : DecidableEq (X × X) := fun a b=>Classical.propDecidable (a=b) + +theorem expect_average {Z : Type*} [Fintype Z] (P : Law Z) (w : Ω→ℝ) (f : Z→Ω→ℝ) : + P.expect (fun z=>average w (f z))=average w (fun ω=>P.expect (fun z=>f z ω)) := by + unfold Law.expect average + simp only [mul_sum] + rw [sum_comm] + apply sum_congr rfl + intro ω _ + apply sum_congr rfl + intro z _ + ring + +theorem allocation_finance (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) : + (tapeLaw A N k J).expect (fun z=>movement (TreeCountData.data D (map A D hk z)) + (by omega) N (weight A))≤ + (8*(10:ℝ)^63/A.q)*(A.editBound+separationRate A)*EpochAlpha.ell k*(1+Real.log (k+1))* + (∑ t∈range N,average D.weight (moverCost (HiddenFlow.flow D) t))+ + (8*(10:ℝ)^63/A.q)*EpochAlpha.ell k*A.editEndpoint k J+ + 2*(10:ℝ)^63*EpochAlpha.ell k*allowance (shape A k J) k (weight A) := by + have h := (tapeLaw A N k J).expect_mono _ _ (fun z=>actual_movement A D hk z N) + rw [Law.expect_add,Law.expect_const,Law.expect_mul] at h + have he : (tapeLaw A N k J).expect + (fun z=>integral (TreeCountData.data D (map A D hk z)) N (coordinateCost A D hk z))= + totalCoordinate (N:=N) (J:=J) A D hk := by + change (tapeLaw A N k J).expect (fun z=>∑ t∈range N,average D.weight (coordinateCost A D hk z t))=_ + rw [Law.expect_sum] + exact sum_congr rfl (fun t _=>expect_average _ _ _) + rw [he] at h + have hc := coordinate_finance (N:=N) (J:=J) A D hk + have hq := A.q_pos + have hx : 0≤8*(10:ℝ)^63/A.q*EpochAlpha.ell k := by + have := EpochAlpha.ell_one k + positivity + have hm := mul_le_mul_of_nonneg_left hc hx + have hf : (8*(10:ℝ)^63/A.q*EpochAlpha.ell k)* + (A.q*totalCoordinate (N:=N) (J:=J) A D hk)= + 8*(10:ℝ)^63*EpochAlpha.ell k*totalCoordinate (N:=N) (J:=J) A D hk := by + field_simp [ne_of_gt hq] + rw [hf] at hm + nlinarith only [h,hm] + +end UniformKServer.PartitionTree + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualAnchorDrift.lean b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorDrift.lean new file mode 100644 index 0000000000000000000000000000000000000000..e4f29e3f47f280b52eaabbc8f72059f221c6a1b0 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorDrift.lean @@ -0,0 +1,120 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.AnchorScalar +import OAI.Combinatorics.UniformKServer.SelectedParkCoefficients +import OAI.Combinatorics.UniformKServer.PrefixMovement + +namespace OAI + + +section + +/-! Height-free hidden-motion drift of the actual parked-heavy potential. +The coefficient is evaluated in the old completed state at the true old +member, with no event-conditioned filtering assumption. -/ +noncomputable section +namespace UniformKServer.PartitionTree +open Finset TreeRounding TreeAncestry +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} +local instance labelDecEq (C : ℝ) (m : ℕ) : DecidableEq (Label X C m) := fun a b=>Classical.propDecidable (a=b) + +def heavyCoefficient (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (j : Fin J) (t : ℕ) (ω : Ω) (l : LevelMap.HeavySlot X) : ℝ := + labelPark A D hk z j (Sum.inl l) t ω/KeySizeTracker.held (labelTracker A D hk z j (Sum.inl l)) t ω + +def heavySelection (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (y : X) (j : Fin J) : Option (LevelMap.HeavySlot X) := + match word A D hk z t ω y j with + | Sum.inl l => if dist ((heavyState A D hk z j t ω).center l) y<7*GeometricMass.radius A.R A.q j.val then some l else none + | Sum.inr _ => none + +theorem heavySelection_near (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (y : X) (j : Fin J) (l : LevelMap.HeavySlot X) + (hl : heavySelection A D hk z t ω y j=some l) : nearHeavy A D hk z j t ω y l := by + unfold heavySelection at hl + cases he : word A D hk z t ω y j with + | inr a => simp only [he] at hl; cases hl + | inl a => + simp only [he] at hl + split_ifs at hl with hd + · have hal : a=l := Option.some.inj hl + subst a + have hc := (LevelMap.Data.key_heavy_iff ((A.input (N:=N) (J:=J) D hk ω).level j.val).data + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order (z j) t y l).mp he + exact ⟨hc.1,by simpa only [dist_comm] using hd.le⟩ + +def heavyNear (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (y : X) (j : Fin J) : ℝ := + AnchorScalar.near (GeometricMass.radius A.R A.q j.val) (heavyCoefficient A D hk z j t ω) + (heavyState A D hk z j t ω).center (word A D hk z t ω y j) y + +theorem near_sum (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (y : X) (hdiam : ∀ p q : X,dist p q≤40*A.R) : + (∑ j : Fin J,heavyNear A D hk z t ω y j)≤(6/5)*132*(2+7/Real.log 2)*Real.log (k+1) := by + have h := selected_coefficients A D hk z (heavySelection A D hk z t ω y) t ω y + (fun j l hl=>heavySelection_near A D hk z t ω y j l hl) hdiam + rw [←Fin.sum_univ_eq_sum_range] at h + convert h using 1 + apply sum_congr rfl + intro j _ + simp only [selectedVertices,selectedDen,dite_eq_left j.isLt,heavySelection,heavyNear,AnchorScalar.near] + cases he : word A D hk z t ω y j with + | inr a => simp only [Option.map_none,Option.getD_none,sum_empty,zero_div] + | inl a => + dsimp only + split_ifs <;> simp only [Option.map_some,Option.getD_some,Option.map_none,Option.getD_none,sum_empty,zero_div] + rfl + +def heavyValue (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (j : Fin J) (t : ℕ) (ω : Ω) (l : Label X A.C k) (p : X) : ℝ := + AnchorScalar.value (GeometricMass.radius A.R A.q j.val) (heavyCoefficient A D hk z j t ω) + (heavyState A D hk z j t ω).center l p + +def memberDrift (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (a : Fin k) : ℝ := + ∑ j : Fin J,GeometricMass.radius A.R A.q j.val* + (heavyValue A D hk z j t ω (word A D hk z (t+1) ω (D.position (t+1) ω a) j) (D.position (t+1) ω a)- + heavyValue A D hk z j t ω (word A D hk z t ω (D.position t ω a) j) (D.position t ω a)) + +theorem member_drift (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (a : Fin k) (hdiam : ∀ p q : X,dist p q≤40*A.R) : + memberDrift A D hk z t ω a≤ + (6/5)*132*(∑ j : Fin J,GeometricMass.radius A.R A.q j.val* + PrefixMovement.diff (word A D hk z (t+1) ω (D.position (t+1) ω a) j) + (word A D hk z t ω (D.position t ω a) j))+ + dist (D.position t ω a) (D.position (t+1) ω a)/3* + ((6/5)*132*(2+7/Real.log 2)*Real.log (k+1)) := by + have hc (j : Fin J) (l : LevelMap.HeavySlot X) : 0≤heavyCoefficient A D hk z j t ω l := + div_nonneg (labelPark_nonneg A D hk z j (Sum.inl l) t ω) + (by have := (KeySizeTracker.held_range (labelTracker A D hk z j (Sum.inl l)) t ω).1; linarith) + have h (j : Fin J) := AnchorScalar.member (GeometricMass.radius A.R A.q j.val) ((6/5)*132) + (GeometricMass.radius_pos _ _ A.R_pos A.q_pos _) (heavyCoefficient A D hk z j t ω) + (heavyState A D hk z j t ω).center (hc j) (by norm_num) + (fun l=>label_individual A D hk z j (Sum.inl l) t ω hdiam) + (word A D hk z t ω (D.position t ω a) j) + (word A D hk z (t+1) ω (D.position (t+1) ω a) j) (D.position t ω a) (D.position (t+1) ω a) + have hs := sum_le_sum (s:=univ) (fun j (_ : j∈(univ : Finset (Fin J)))=>h j) + have hn := mul_le_mul_of_nonneg_left (near_sum A D hk z t ω (D.position t ω a) hdiam) + (show 0≤dist (D.position t ω a) (D.position (t+1) ω a)/3 by positivity) + simp only [sum_add_distrib,←mul_sum] at hs + change memberDrift A D hk z t ω a≤_ at hs + refine hs.trans ?_ + convert add_le_add (le_refl ((6/5)*132*(∑ j : Fin J,GeometricMass.radius A.R A.q j.val* + PrefixMovement.diff (word A D hk z (t+1) ω (D.position (t+1) ω a) j) (word A D hk z t ω (D.position t ω a) j)))) hn using 1 + congr 1 + rw [mul_sum] + apply sum_congr rfl + intro j _ + unfold PrefixMovement.diff + by_cases he : word A D hk z t ω (D.position t ω a) j=word A D hk z (t+1) ω (D.position (t+1) ω a) j + · simp only [he,ite_true,mul_zero] + · simp only [ite_eq_right he,ite_eq_right (Ne.symm he),mul_one] + +end UniformKServer.PartitionTree + +end + + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualAnchorDriftSum.lean b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorDriftSum.lean new file mode 100644 index 0000000000000000000000000000000000000000..398b5a2b7476f9bd76ec3a88ebee0d326650672e --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorDriftSum.lean @@ -0,0 +1,49 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.PartitionMovement +import OAI.Combinatorics.UniformKServer.CurrentTesting +import OAI.Combinatorics.UniformKServer.ActualAnchorDrift + +namespace OAI + + +section + +/-! The individual retained-anchor hidden drift summed over exactly the +chosen mover and all stationary members. -/ +noncomputable section +namespace UniformKServer.PartitionTree +open Finset TreeRounding TreeAncestry +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} + +def driftLog (k : ℕ) : ℝ := (6/5)*132*(2+7/Real.log 2)*Real.log (k+1) + +theorem members_drift (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) (hdiam : ∀ p q : X,dist p q≤40*A.R) : + (∑ a : Fin k,memberDrift A D hk z t ω a)≤ + (6/5)*132*A.q*coordinateCost A D hk z t ω+ + (driftLog k/3)*dist (D.position t ω (D.chosen t ω)) (D.request t ω) := by + have hs := sum_le_sum (s:=univ) (fun a (_ : a∈(univ : Finset (Fin k)))=>member_drift A D hk z t ω a hdiam) + have he : (∑ a : Fin k,∑ j : Fin J,GeometricMass.radius A.R A.q j.val* + PrefixMovement.diff (word A D hk z (t+1) ω (D.position (t+1) ω a) j) + (word A D hk z t ω (D.position t ω a) j))=A.q*coordinateCost A D hk z t ω := by + simp only [coordinateCost,mul_sum] + apply sum_congr rfl + intro a _ + apply sum_congr rfl + intro j _ + simp only [GeometricMass.radius,radius,pow_succ] + ring + simp only [sum_add_distrib,←mul_sum,←sum_mul,←sum_div] at hs + rw [he,HiddenFlow.member_distance] at hs + dsimp only [driftLog] + nlinarith only [hs] + +end UniformKServer.PartitionTree + +end + + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualAnchorFiltering.lean b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorFiltering.lean new file mode 100644 index 0000000000000000000000000000000000000000..3d0a7419476db05865359ac25f16c57a02fb3476 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorFiltering.lean @@ -0,0 +1,102 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.ActualAnchorDriftSum +import OAI.Combinatorics.UniformKServer.ActualAnchorPotential + +namespace OAI + + +section + +/-! The five-substep potential's filtering and hidden-motion identity is an +unconditional finite-law identity at every step. -/ +noncomputable section +namespace UniformKServer.PartitionTree +open Finset TreeRounding TreeAncestry PilotEdits +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} + +def frozenPotential (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (t s : ℕ) (ω : Ω) : ℝ := + ∑ j : Fin J,∑ l : LevelMap.HeavySlot X,anchorTerm A D hk z j l t t s t ω + +theorem frozen_regroup (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (t s : ℕ) (ω : Ω) : + frozenPotential A D hk z t s ω=∑ p,HiddenFlow.current D s ω p* + ∑ j : Fin J,GeometricMass.radius A.R A.q j.val*heavyValue A D hk z j t ω (word A D hk z s ω p j) p := by + have hj (j : Fin J) : (∑ l : LevelMap.HeavySlot X,anchorTerm A D hk z j l t t s t ω)= + GeometricMass.radius A.R A.q j.val*(∑ p,HiddenFlow.current D s ω p* + heavyValue A D hk z j t ω (word A D hk z s ω p j) p) := by + have hm := AnchorPotential.regroup (GeometricMass.radius A.R A.q j.val) (HiddenFlow.current D s ω) + (heavyCoefficient A D hk z j t ω) (heavyState A D hk z j t ω).center (fun p=>word A D hk z s ω p j) + dsimp only [heavyValue] + rw [←hm,mul_sum] + apply sum_congr rfl + intro l _ + dsimp only [anchorTerm,anchorIntegral,heavyCoefficient,keyRegion] + ring_nf + congr 2 + ext p + simp only [mem_filter,mem_univ,true_and] + simp only [frozenPotential,hj,mul_sum] + rw [sum_comm] + apply sum_congr rfl + intro p _ + apply sum_congr rfl + intro j _ + ring + +theorem heavyValue_measurable (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (t : ℕ) (ω v : Ω) (h : (D.filtration t).r ω v) + (l : Label X A.C k) (p : X) : heavyValue A D hk z j t ω l p=heavyValue A D hk z j t v l p := by + cases l with + | inr a => rfl + | inl l => + dsimp only [heavyValue,AnchorScalar.value,heavyCoefficient] + have hp := labelPark_measurable A D hk z j (Sum.inl l) t ω v h + have hu := KeySizeTracker.held_measurable (labelTracker A D hk z j (Sum.inl l)) t ω v h + have ha := A.anchor_measurable (N:=N) (J:=J) D hk ω v t j.val h (z j) (Sum.inl l) + change (heavyState A D hk z j t ω).center l=(heavyState A D hk z j t v).center l at ha + rw [hp,hu,ha] + +theorem frozen_testing (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (t s : ℕ) (hts : t ≤ s) : + average D.weight (frozenPotential A D hk z t s)= + average D.weight (fun ω=>∑ a : Fin k,∑ j : Fin J,GeometricMass.radius A.R A.q j.val* + heavyValue A D hk z j t ω (word A D hk z s ω (D.position s ω a) j) (D.position s ω a)) := by + change average D.weight (fun ω=>frozenPotential A D hk z t s ω)=_ + simp_rw [frozen_regroup] + apply HiddenFlow.current_test + intro ω v h + funext p + apply sum_congr rfl + intro j _ + have hk' := A.key_measurable (N:=N) (J:=J) D hk ω v s j.val h (z j) p + change word A D hk z s ω p j=word A D hk z s v p j at hk' + rw [hk'] + exact congrArg (fun x : ℝ=>GeometricMass.radius A.R A.q j.val*x) + (heavyValue_measurable A D hk z j t ω v (HiddenFlow.refines_le D t s hts ω v h) _ p) + +theorem expected_drift (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (t : ℕ) (hdiam : ∀ p q : X,dist p q ≤ 40*A.R) : + average D.weight (fun ω=>frozenPotential A D hk z t (t+1) ω-frozenPotential A D hk z t t ω) ≤ + average D.weight (fun ω=>(6/5)*132*A.q*coordinateCost A D hk z t ω+ + (driftLog k/3)*dist (D.position t ω (D.chosen t ω)) (D.request t ω)) := by + have he : average D.weight (fun ω=>frozenPotential A D hk z t (t+1) ω-frozenPotential A D hk z t t ω)= + average D.weight (fun ω=>∑ a : Fin k,memberDrift A D hk z t ω a) := by + have h1 := frozen_testing A D hk z t (t+1) (by omega) + have h0 := frozen_testing A D hk z t t le_rfl + simp only [average,mul_sub,sum_sub_distrib] at h1 h0 ⊢ + rw [h1,h0] + simp only [memberDrift,mul_sub,sum_sub_distrib] + rw [he] + exact PilotEdits.average_mono D.weight _ _ (fun ω=>(D.positive ω).le) + (fun ω=>members_drift A D hk z t ω hdiam) + +end UniformKServer.PartitionTree + +end + + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualAnchorPotential.lean b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorPotential.lean new file mode 100644 index 0000000000000000000000000000000000000000..617e2a40bf0cd10d3ca1560e86d97b7206264194 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualAnchorPotential.lean @@ -0,0 +1,113 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.HeavyLabelParks +import OAI.Combinatorics.UniformKServer.AnchorPotential + +namespace OAI + + +section + +/-! The actual four parameter substeps after the unconditional hidden drift. -/ +noncomputable section +namespace UniformKServer.PartitionTree +open Finset TreeRounding TreeAncestry +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} + +def anchorIntegral (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (m a : ℕ) (ω : Ω) : ℝ := + AnchorPotential.integral (GeometricMass.radius A.R A.q j.val) (HiddenFlow.current D m ω) + (keyRegion A D hk z j (Sum.inl l) m ω) ((heavyState A D hk z j a ω).center l) + +def anchorTerm (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (b u m a : ℕ) (ω : Ω) : ℝ := + GeometricMass.radius A.R A.q j.val*labelPark A D hk z j (Sum.inl l) b ω/ + KeySizeTracker.held (labelTracker A D hk z j (Sum.inl l)) u ω*anchorIntegral A D hk z j l m a ω + +theorem anchorIntegral_nonneg (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (m a : ℕ) (ω : Ω) : + 0 ≤ anchorIntegral A D hk z j l m a ω := + AnchorPotential.integral_nonneg _ _ ((HiddenFlow.flow D).post_nonneg m ω) _ _ + +theorem anchorIntegral_held (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (m a : ℕ) (ω : Ω) : + anchorIntegral A D hk z j l m a ω ≤ (6/5)*KeySizeTracker.held (labelTracker A D hk z j (Sum.inl l)) m ω := by + have hi := AnchorPotential.integral_le (GeometricMass.radius A.R A.q j.val) _ + ((HiddenFlow.flow D).post_nonneg m ω) (keyRegion A D hk z j (Sum.inl l) m ω) ((heavyState A D hk z j a ω).center l) + have hc := (KeySizeTracker.comparisons (labelTracker A D hk z j (Sum.inl l)) m ω).1 + rw [labelTracker_size] at hc + exact hi.trans (by + change (∑ p∈keyRegion A D hk z j (Sum.inl l) m ω,HiddenFlow.current D m ω p) ≤ _ + dsimp only [ParkCapacity.mass] at hc + linarith) + +theorem anchorTerm_nonneg (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (b u m a : ℕ) (ω : Ω) : + 0 ≤ anchorTerm A D hk z j l b u m a ω := by + unfold anchorTerm + exact mul_nonneg (div_nonneg (mul_nonneg (GeometricMass.radius_pos _ _ A.R_pos A.q_pos j.val).le + (labelPark_nonneg A D hk z j (Sum.inl l) b ω)) (by have := (KeySizeTracker.held_range (labelTracker A D hk z j (Sum.inl l)) u ω).1; linarith)) + (anchorIntegral_nonneg A D hk z j l m a ω) + +theorem absent_park (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (t : ℕ) (ω : Ω) + (hdiam : ∀ p q : X,dist p q ≤ 40*A.R) (hl : l∉(heavyState A D hk z j t ω).present) : + labelPark A D hk z j (Sum.inl l) t ω=0 := by + have he : keyRegion A D hk z j (Sum.inl l) t ω=∅ := by + apply eq_empty_iff_forall_notMem.mpr + intro p hp + have hc := (LevelMap.Data.key_heavy_iff ((A.input (N:=N) (J:=J) D hk ω).level j.val).data + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order (z j) t p l).mp ((mem_filter.mp hp).2) + exact hl hc.1 + have hd := label_domination A D hk z j (Sum.inl l) t ω hdiam + rw [he,ParkCapacity.mass,sum_empty,mul_zero] at hd + exact le_antisymm hd (labelPark_nonneg A D hk z j (Sum.inl l) t ω) + +theorem anchorTerm_bound (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2 ≤ k) + (z : Tape A k N J) (j : Fin J) (l : LevelMap.HeavySlot X) (b m a : ℕ) (ω : Ω) : + anchorTerm A D hk z j l b m m a ω ≤ + (6/5)*GeometricMass.radius A.R A.q j.val*labelPark A D hk z j (Sum.inl l) b ω := by + have hu : 0PilotEdits.average_mono D.weight _ _ + (fun ω=>(D.positive ω).le) (anchorTravel_le A D hk z t)) + simpa only [average_smul,←mul_sum,totalAnchorTravel,payments] using h + +end UniformKServer.PartitionTree + +end + + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualAuxiliary.lean b/lean/OAI/Combinatorics/UniformKServer/ActualAuxiliary.lean new file mode 100644 index 0000000000000000000000000000000000000000..0c5ffd0755dbd5b6d62897fce63ec5117c3e5391 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualAuxiliary.lean @@ -0,0 +1,63 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.ActualPartitions +import OAI.Combinatorics.UniformKServer.PilotTemplates +import OAI.Combinatorics.UniformKServer.CausalLevelAux +import OAI.Combinatorics.UniformKServer.FlowAuxiliary + +namespace OAI + +noncomputable section + +/-! Adapted linear auxiliary family for the literal multiscale partition. -/ +namespace UniformKServer.ActualPartitions.Config +open Finset PilotEdits +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} + +def auxiliary (A : Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (t : ℕ) (ω : Ω) (p : X) : ℝ := + ∑ j∈range J,((A.input (N:=N) (J:=J) D hk ω).level j).data.auxiliary + ((A.input (N:=N) (J:=J) D hk ω).level j).order t p + +def auxiliaryCap (A : Config X) (k J : ℕ) : ℝ := + (1+((DyadicTiers.last (Real.log k)+1:ℕ):ℝ))*(∑ j∈range J,GeometricMass.radius A.R A.q j) + +theorem level_auxiliary_measurable (A : Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (ω v : Ω) (t j : ℕ) (he : (D.filtration t).r ω v) (p : X) : + ((A.input (N:=N) (J:=J) D hk ω).level j).data.auxiliary ((A.input (N:=N) (J:=J) D hk ω).level j).order t p= + ((A.input (N:=N) (J:=J) D hk v).level j).data.auxiliary ((A.input (N:=N) (J:=J) D hk v).level j).order t p := by + let I := ((A.input (N:=N) (J:=J) D hk ω).level j).data + let V := ((A.input (N:=N) (J:=J) D hk v).level j).data + exact LevelMap.Data.auxiliary_inputs I I.center V.center I.heavy V.heavy I.qualify V.qualify t + (A.past_centers (J:=J) D hk ω v t he) (A.past_heavy (J:=J) D hk ω v t j he) + (A.past_qualify (J:=J) D hk ω v t j he) _ p + +theorem auxiliary_measurable (A : Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (t : ℕ) (ω v : Ω) (he : (D.filtration t).r ω v) : + A.auxiliary (N:=N) (J:=J) D hk t ω=A.auxiliary (N:=N) (J:=J) D hk t v := by + funext p + apply sum_congr rfl + intro j _ + exact A.level_auxiliary_measurable D hk ω v t j he p + +theorem auxiliary_bounds (A : Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (t : ℕ) (ω : Ω) (p : X) : + 0≤A.auxiliary (N:=N) (J:=J) D hk t ω p ∧ + A.auxiliary (N:=N) (J:=J) D hk t ω p≤A.auxiliaryCap k J := by + let I := A.input (N:=N) (J:=J) D hk ω + have hb (j : ℕ) := (I.level j).data.auxiliary_bounds (I.level j).order t p + constructor + · exact sum_nonneg (fun j _=>(hb j).1) + · unfold auxiliary auxiliaryCap + have h := sum_le_sum (s:=range J) (fun j _=>(hb j).2) + convert h using 1 + dsimp only [I] at * + simp only [PartitionLevel.Input.order,List.length_ofFn,PartitionLevel.Input.height, + PartitionScales.Input.level,ActualPartitions.Config.input,PartitionLevel.Input.data,←sum_mul] + ring + +end UniformKServer.ActualPartitions.Config + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualCoordinate.lean b/lean/OAI/Combinatorics/UniformKServer/ActualCoordinate.lean new file mode 100644 index 0000000000000000000000000000000000000000..441ac276e927261571a35556fdf278cf838973e1 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualCoordinate.lean @@ -0,0 +1,98 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.PilotTemplates +import OAI.Combinatorics.UniformKServer.PartitionMovement +import OAI.Combinatorics.UniformKServer.PartitionLaw +import OAI.Combinatorics.UniformKServer.FlowAuxiliary + +namespace OAI + +noncomputable section + +/-! Literal coordinate-key motion split into actual edits and the one mover. -/ +namespace UniformKServer.PartitionTree +open Finset FiniteProbability PilotEdits +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} +local instance ixAC (m : ℕ) : DecidableEq (Fin m) := fun a b=>Classical.propDecidable (a=b) +local instance pairAC : DecidableEq (X × X) := fun a b=>Classical.propDecidable (a=b) + +theorem diff_triangle {B : Type*} (a b c : B) : + PrefixMovement.diff a c≤PrefixMovement.diff a b+PrefixMovement.diff b c := by + unfold PrefixMovement.diff + split_ifs <;> norm_num + aesop + +def pathEdits (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) : ℝ := + ∑ a : Fin k,∑ j : Fin J,GeometricMass.radius A.R A.q j.val*PrefixMovement.diff + (word A D hk z (t+1) ω (D.position t ω a) j) (word A D hk z t ω (D.position t ω a) j) + +def pathMove (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) : ℝ := + ∑ j : Fin J,GeometricMass.radius A.R A.q j.val*PrefixMovement.diff + (word A D hk z (t+1) ω (D.request t ω) j) + (word A D hk z (t+1) ω (D.position t ω (D.chosen t ω)) j) + +theorem coordinate_split (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (z : Tape A k N J) (t : ℕ) (ω : Ω) : + A.q*coordinateCost A D hk z t ω≤pathEdits A D hk z t ω+pathMove A D hk z t ω := by + have hr (j : Fin J) : A.q*radius A j.val=GeometricMass.radius A.R A.q j.val := by + unfold radius GeometricMass.radius + rw [pow_succ] + ring + have h1 := sum_le_sum (fun a (_ : a∈(univ : Finset (Fin k)))=> + sum_le_sum (fun j (_ : j∈(univ : Finset (Fin J)))=> + mul_le_mul_of_nonneg_left (diff_triangle + (word A D hk z (t+1) ω (D.position (t+1) ω a) j) + (word A D hk z (t+1) ω (D.position t ω a) j) + (word A D hk z t ω (D.position t ω a) j)) + (GeometricMass.radius_pos A.R A.q A.R_pos A.q_pos j.val).le)) + have he (a : Fin k) : (∑ j : Fin J,GeometricMass.radius A.R A.q j.val*PrefixMovement.diff + (word A D hk z (t+1) ω (D.position (t+1) ω a) j) + (word A D hk z (t+1) ω (D.position t ω a) j))= + if a=D.chosen t ω then pathMove A D hk z t ω else 0 := by + rw [D.update] + split_ifs with ha + · subst a; rfl + · simp [PrefixMovement.diff] + simp only [mul_add,sum_add_distrib,he,sum_ite_eq',mem_univ,ite_true] at h1 + unfold coordinateCost + simp only [mul_sum,←mul_assoc,hr] + dsimp only [pathEdits] at * + linarith only [h1] + +theorem move_expect (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (ω : Ω) (n : Fin N) : + (tapeLaw A N k J).expect (fun z=>pathMove A D hk z n.val ω)≤ + (A.input (N:=N) (J:=J) D hk ω).coefficient*(1+Real.log k)* + dist (D.request n.val ω) (D.position n.val ω (D.chosen n.val ω)) := by + unfold pathMove + rw [Law.expect_sum] + simp only [Law.expect_mul] + have he (j : Fin J) : + (tapeLaw A N k J).expect (fun z=>PrefixMovement.diff + (word A D hk z (n.val+1) ω (D.request n.val ω) j) + (word A D hk z (n.val+1) ω (D.position n.val ω (D.chosen n.val ω)) j))= + ((A.input (N:=N) (J:=J) D hk ω).level j.val).separation n + (D.position n.val ω (D.chosen n.val ω)) := by + change (tapeLaw A N k J).expect (fun z=>FirstStructure.indicator + (((A.input (N:=N) (J:=J) D hk ω).level j.val).data.key + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order (z j) (n.val+1) (D.request n.val ω)) + (((A.input (N:=N) (J:=J) D hk ω).level j.val).data.key + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order (z j) (n.val+1) + (D.position n.val ω (D.chosen n.val ω))))=_ + unfold PartitionLevel.Input.separation + exact coordinate_expect (N:=N) A D hk ω j (fun zz=>FirstStructure.indicator + (((A.input (N:=N) (J:=J) D hk ω).level j.val).data.key + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order zz (n.val+1) (D.request n.val ω)) + (((A.input (N:=N) (J:=J) D hk ω).level j.val).data.key + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order zz (n.val+1) + (D.position n.val ω (D.chosen n.val ω)))) + simp_rw [he] + simpa only [←Fin.sum_univ_eq_sum_range,PartitionScales.Input.level,ActualPartitions.Config.input] using A.separation (J:=J) D hk ω n (D.position n.val ω (D.chosen n.val ω)) + +end UniformKServer.PartitionTree + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualCoordinateBudget.lean b/lean/OAI/Combinatorics/UniformKServer/ActualCoordinateBudget.lean new file mode 100644 index 0000000000000000000000000000000000000000..af5e790e82294eaa941fea590cd939c0bfc74354 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualCoordinateBudget.lean @@ -0,0 +1,82 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.ActualEditIntegration +import OAI.Combinatorics.UniformKServer.ActualEditFinance + +namespace OAI + +noncomputable section + +/-! The actual multiscale coordinate motion is height-free after integration. -/ +namespace UniformKServer.PartitionTree +open Finset FiniteProbability PilotEdits +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} +local instance ixCB (m : ℕ) : DecidableEq (Fin m) := fun a b=>Classical.propDecidable (a=b) +local instance pairCB : DecidableEq (X × X) := fun a b=>Classical.propDecidable (a=b) + +def separationRate (A : ActualPartitions.Config X) : ℝ := + 8+(16*A.C+33)*(1+1/Real.log (1+A.P.deltaH)) + +omit [Fintype X] [MetricSpace X] in +theorem separationRate_nonneg (A : ActualPartitions.Config X) : 0 ≤ separationRate A := by + have hδ : 0(tapeLaw A N k J).expect (fun z=>coordinateCost A D hk z t ω)) + +theorem average_move (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (n : Fin N) : + average D.weight (fun ω=>(tapeLaw A N k J).expect (fun z=>pathMove A D hk z n.val ω))≤ + separationRate A*(1+Real.log k)*average D.weight (moverCost (HiddenFlow.flow D) n.val) := by + have h := average_mono D.weight _ _ (fun ω=>(D.positive ω).le) (fun ω=>move_expect (J:=J) A D hk ω n) + change _≤average D.weight (fun ω=>separationRate A*(1+Real.log k)* + dist (D.request n.val ω) (D.position n.val ω (D.chosen n.val ω))) at h + rw [average_smul] at h + change _≤_*_*(∑ ω,D.weight ω*_) at h + rw [←HiddenFlow.expected_mover_cost D n.val] at h + exact h + +theorem coordinate_step_budget (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (n : Fin N) : + A.q*average D.weight (fun ω=>(tapeLaw A N k J).expect (fun z=>coordinateCost A D hk z n.val ω))≤ + average D.weight (A.keyEditCost (N:=N) (J:=J) D hk n.val)+ + separationRate A*(1+Real.log k)*average D.weight (moverCost (HiddenFlow.flow D) n.val) := by + have h := average_mono D.weight _ _ (fun ω=>(D.positive ω).le) (fun ω=> + (tapeLaw A N k J).expect_mono _ _ (fun z=>coordinate_split A D hk z n.val ω)) + simp only [Law.expect_mul,Law.expect_add,average_smul,average_add] at h + rw [edits_expect] at h + exact h.trans (add_le_add le_rfl (average_move A D hk n)) + +theorem coordinate_finance (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) : + A.q*totalCoordinate (N:=N) (J:=J) A D hk≤ + (A.editBound+separationRate A)*(1+Real.log (k+1))* + (∑ t∈range N,average D.weight (moverCost (HiddenFlow.flow D) t))+A.editEndpoint k J := by + let Q := ∑ t∈range N,average D.weight (moverCost (HiddenFlow.flow D) t) + have hQ : 0≤Q := sum_nonneg (fun t _=>sum_nonneg (fun ω _=>mul_nonneg (D.positive ω).le + (sum_nonneg (fun p _=>mul_nonneg ((HiddenFlow.flow D).mover_nonneg t ω p) dist_nonneg)))) + have h : (∑ t∈range N, A.q*average D.weight (fun ω=>(tapeLaw A N k J).expect + (fun z=>coordinateCost A D hk z t ω)))≤ + ∑ t∈range N,(average D.weight (A.keyEditCost (N:=N) (J:=J) D hk t)+ + separationRate A*(1+Real.log k)*average D.weight (moverCost (HiddenFlow.flow D) t)) := + sum_le_sum (fun t ht=>coordinate_step_budget (J:=J) A D hk ⟨t,mem_range.mp ht⟩) + simp only [sum_add_distrib,←mul_sum] at h + change A.q*totalCoordinate (N:=N) (J:=J) A D hk≤ + (∑ t∈range N,average D.weight (A.keyEditCost (N:=N) (J:=J) D hk t))+ + separationRate A*(1+Real.log k)*Q at h + have hf := A.edit_finance (N:=N) (J:=J) D hk + have hlog : 1+Real.log k≤1+Real.log (k+1) := by + have hk' : (0:ℝ)F.filtered n.val ω p-F.mover n.val ω p + have hs (p : X) : 0≤ s p := sub_nonneg.mpr (F.mover_le n.val ω p) + have hs' (p : X) : s p≤I.μ n p := by + have he := F.update n.val ω p + change I.μ n p= s p + (if p=I.center n then 1 else 0) at he + rw [he] + split_ifs <;> linarith + have h := I.data.mass_ledger n I.order A.P.gammaH (by linarith [A.P.gammaH_small]) + s (F.mover n.val ω) hs (F.mover_nonneg n.val ω) (F.mover_total n.val ω) + have hstat := I.stationary_allowance n s hs hs' + have hdecomp : (fun p=>s p+F.mover n.val ω p)=F.filtered n.val ω := by funext p; dsimp [s]; ring + have hpost (b : X→ℝ) : (∑ p,s p*b p)+b (I.data.center n)=linearPotential (F.post (n.val+1) ω) b := by + unfold linearPotential + simp only [F.update,add_mul,sum_add_distrib,ite_mul,one_mul,zero_mul,sum_ite_eq',mem_univ,ite_true] + rfl + simp only [congrFun hdecomp] at h + rw [hpost] at h + dsimp only [linearPotential] at h ⊢ + change _≤98*I.charges n+_ + change _≤98*I.charges n at hstat + have hstat' : (∑ p,s p*I.data.stationaryAllowance I.order A.P.gammaH n p)≤98*I.charges n := hstat + exact h.trans (add_le_add hstat' le_rfl) + +theorem key_mass_step (A : Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (ω : Ω) (n : Fin N) : + A.keyEditCost (N:=N) (J:=J) D hk n.val ω+ + linearPotential (HiddenFlow.current D (n.val+1) ω) (A.auxiliary (N:=N) (J:=J) D hk (n.val+1) ω)- + linearPotential (HiddenFlow.filtered D n.val ω) (A.auxiliary (N:=N) (J:=J) D hk n.val ω)≤ + 147*A.totalPilotCharge (N:=N) (J:=J) D hk n.val ω+ + A.editRate*(1+Real.log k)*moverCost (HiddenFlow.flow D) n.val ω := by + let I := A.input (N:=N) (J:=J) D hk ω + let F := HiddenFlow.flow D + have hm := sum_le_sum (fun p (_ : p∈(univ : Finset X))=> + mul_le_mul_of_nonneg_left (I.mover_allowance_sum n p) (F.mover_nonneg n.val ω p)) + have hme : (∑ p,F.mover n.val ω p*(49*(∑ j∈range J,(I.level j).charges n)+ + I.editCoefficient*(1+Real.log I.k)*dist (I.center n) p))= + 49*(∑ j∈range J,(I.level j).charges n)+A.editRate*(1+Real.log k)*moverCost F n.val ω := by + simp only [mul_add,sum_add_distrib,←sum_mul,F.mover_total,one_mul] + congr 1 + unfold moverCost + rw [mul_sum] + apply sum_congr rfl + intro p _ + change F.mover n.val ω p*((A.editRate*1+A.editRate*Real.log k)*dist (I.center n) p)= + (A.editRate*1+A.editRate*Real.log k)*(F.mover n.val ω p*dist (I.center n) p) + ring + rw [hme] at hm + have h := sum_le_sum (s:=range J) (fun j _=>A.level_mass_step (J:=J) D hk ω n j) + change (∑ j∈range J,((∑ p,F.filtered n.val ω p*(I.level j).data.editCost n (I.level j).order p)+ + linearPotential (F.post (n.val+1) ω) ((I.level j).data.auxiliary (I.level j).order (n.val+1))- + linearPotential (F.filtered n.val ω) ((I.level j).data.auxiliary (I.level j).order n.val)))≤ + ∑ j∈range J,(98*(I.level j).charges n+∑ p,F.mover n.val ω p* + (I.level j).data.moverAllowance (I.level j).order A.P.gammaH n p) at h + rw [sum_sub_distrib,sum_add_distrib,sum_add_distrib,←mul_sum] at h + have hlin (μ : X→ℝ) (t : ℕ) : + (∑ j∈range J,linearPotential μ ((I.level j).data.auxiliary (I.level j).order t))= + linearPotential μ (A.auxiliary (N:=N) (J:=J) D hk t ω) := by + unfold linearPotential auxiliary + rw [sum_comm] + simp only [mul_sum] + rfl + rw [hlin,hlin] at h + have hm2 : (∑ j∈range J,∑ p,F.mover n.val ω p*(I.level j).data.moverAllowance (I.level j).order A.P.gammaH n p)= + ∑ p,F.mover n.val ω p*∑ j∈range J,(I.level j).data.moverAllowance (I.level j).order A.P.gammaH n p := by + rw [sum_comm] + simp only [mul_sum] + rw [hm2] at h + change (∑ p,F.mover n.val ω p*∑ j∈range J,(I.level j).data.moverAllowance (I.level j).order A.P.gammaH n p)≤_ at hm + have hc : (∑ j∈range J,(I.level j).charges n)=A.totalPilotCharge (N:=N) (J:=J) D hk n.val ω := A.charges_identity D hk ω n + rw [hc] at h hm + rw [keyEditCost,dite_eq_left n.isLt] + change (∑ j∈range J,∑ p,F.filtered n.val ω p*(I.level j).data.editCost n (I.level j).order p)+ + linearPotential (F.post (n.val+1) ω) (A.auxiliary (N:=N) (J:=J) D hk (n.val+1) ω)- + linearPotential (F.filtered n.val ω) (A.auxiliary (N:=N) (J:=J) D hk n.val ω)≤_ + linarith only [h,hm] + +end UniformKServer.ActualPartitions.Config + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualEditFinance.lean b/lean/OAI/Combinatorics/UniformKServer/ActualEditFinance.lean new file mode 100644 index 0000000000000000000000000000000000000000..2a3efb7d2c472d74f671705e2a53b2dd3aff1fef --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualEditFinance.lean @@ -0,0 +1,67 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.ActualEditCosts + +namespace OAI + +noncomputable section + +/-! Integrated height-free key edits, financed by actual posterior pilots. -/ +namespace UniformKServer.ActualPartitions.Config +open Finset PilotEdits +open scoped Classical +variable {X Ω : Type} [Fintype X] [MetricSpace X] [Fintype Ω] {k N J : ℕ} + +omit [Fintype X] [MetricSpace X] in +theorem editRate_nonneg (A : Config X) : 0≤A.editRate := by + have hγ := A.P.gammaH_pos + have hδ : 0sum_nonneg (fun ω _=>mul_nonneg (D.positive ω).le + (sum_nonneg (fun p _=>mul_nonneg (F.mover_nonneg t ω p) dist_nonneg)))) + have he := linear_edits F N (A.auxiliary (N:=N) (J:=J) D hk) (A.auxiliaryCap k J) + (A.auxiliary_bounds D hk) (A.auxiliary_measurable D hk) + (A.keyEditCost (N:=N) (J:=J) D hk) + (fun t ω=>147*A.totalPilotCharge (N:=N) (J:=J) D hk t ω+A.editRate*(1+Real.log k)*moverCost F t ω) + (fun t ht ω=>A.key_mass_step D hk ω ⟨t,ht⟩) + simp only [average_add,average_smul,sum_add_distrib,←mul_sum] at he + have hp := A.pilot_finance (N:=N) (J:=J) D hk N + have hl : 0≤1+Real.log (k+1) := by + have h : (1:ℝ)≤k+1 := by have := Nat.cast_nonneg (α:=ℝ) k; linarith + linarith [Real.log_nonneg h] + have hr := mul_le_mul_of_nonneg_right (PilotFamily.rate_bound A.P k) (mul_nonneg hl hQ) + have hm := mul_le_mul_of_nonneg_left (PilotFamily.weight_sum k) + (show 0≤(2:ℝ)*k*(∑ j∈range J,GeometricMass.radius A.R A.q j) by + exact mul_nonneg (mul_nonneg (by norm_num) (Nat.cast_nonneg _)) + (sum_nonneg (fun j _=> (GeometricMass.radius_pos A.R A.q A.R_pos A.q_pos j).le))) + have hlog : 1+Real.log k≤1+Real.log (k+1) := by + have hk' : (0:ℝ)Classical.propDecidable (a=b) +local instance pairEI : DecidableEq (X × X) := fun a b=>Classical.propDecidable (a=b) + +theorem diff_indicator {B : Type*} (a b : B) : PrefixMovement.diff a b=FirstStructure.indicator b a := by + unfold PrefixMovement.diff FirstStructure.indicator + simp only [eq_comm] + +theorem integrated_edit (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (ω : Ω) (n : Fin N) (j : Fin J) (p : X) : + (tapeLaw A N k J).expect (fun z=>GeometricMass.radius A.R A.q j.val*PrefixMovement.diff + (word A D hk z (n.val+1) ω p j) (word A D hk z n.val ω p j))= + ((A.input (N:=N) (J:=J) D hk ω).level j.val).data.editCost n + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order p := by + rw [Law.expect_mul] + simp_rw [diff_indicator] + exact congrArg (fun x=>GeometricMass.radius A.R A.q j.val*x) (coordinate_expect (N:=N) A D hk ω j + (fun zz=>FirstStructure.indicator + (((A.input (N:=N) (J:=J) D hk ω).level j.val).data.key + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order zz n.val p) + (((A.input (N:=N) (J:=J) D hk ω).level j.val).data.key + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order zz (n.val+1) p))) + +theorem edit_measurable (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (n : Fin N) (j : Fin J) (ω v : Ω) (h : (D.filtration (n.val+1)).r ω v) : + ((A.input (N:=N) (J:=J) D hk ω).level j.val).data.editCost n + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order = + ((A.input (N:=N) (J:=J) D hk v).level j.val).data.editCost n + ((A.input (N:=N) (J:=J) D hk v).level j.val).order := by + funext p + rw [←integrated_edit A D hk ω n j p,←integrated_edit A D hk v n j p] + congr 1 + funext z + apply congrArg (fun x=>GeometricMass.radius A.R A.q j.val*x) + exact congrArg₂ PrefixMovement.diff + (A.key_measurable (N:=N) (J:=J) D hk ω v (n.val+1) j.val h (z j) p) + (A.key_measurable (N:=N) (J:=J) D hk ω v n.val j.val (D.refines n.val ω v h) (z j) p) + +theorem edits_expect (A : ActualPartitions.Config X) (D : HiddenFlow.Data X Ω k) (hk : 2≤k) + (n : Fin N) : + average D.weight (fun ω=>(tapeLaw A N k J).expect (fun z=>pathEdits A D hk z n.val ω))= + average D.weight (A.keyEditCost (N:=N) (J:=J) D hk n.val) := by + have hp (ω : Ω) : (tapeLaw A N k J).expect (fun z=>pathEdits A D hk z n.val ω)= + ∑ j : Fin J,∑ a : Fin k, + ((A.input (N:=N) (J:=J) D hk ω).level j.val).data.editCost n + ((A.input (N:=N) (J:=J) D hk ω).level j.val).order (D.position n.val ω a) := by + unfold pathEdits + simp_rw [Law.expect_sum] + rw [sum_comm] + exact sum_congr rfl (fun j _=>sum_congr rfl (fun a _=>integrated_edit A D hk ω n j _)) + simp_rw [hp] + unfold ActualPartitions.Config.keyEditCost + simp only [dite_eq_left n.isLt] + simp only [←Fin.sum_univ_eq_sum_range] + have hs (f : Fin J→Ω→ℝ) : average D.weight (fun ω=>∑ j,f j ω)=∑ j,average D.weight (f j) := by + unfold average + simp only [mul_sum] + rw [sum_comm] + rw [hs,hs] + apply sum_congr rfl + intro j _ + exact (HiddenFlow.filtered_test D n.val _ (edit_measurable A D hk n j)).symm + +end UniformKServer.PartitionTree + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualFineCaps.lean b/lean/OAI/Combinatorics/UniformKServer/ActualFineCaps.lean new file mode 100644 index 0000000000000000000000000000000000000000..f62129a1d18da4d1770d2494d95975d9844122f3 --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualFineCaps.lean @@ -0,0 +1,129 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.FineVariation +import OAI.Combinatorics.UniformKServer.StarParameters + +namespace OAI + + +section + +/-! Fine caps of the actual persistent rank trackers. No cap feasibility +inequality is assumed; it follows from endpoint errors and the size-log gap. -/ +noncomputable section +namespace UniformKServer.ActualFineCaps +open Finset RankFunctions RankData RankTracking CoarseData FlexEstimates +open scoped Classical +variable {Ω R ι : Type*} [Fintype Ω] [Fintype R] [Fintype ι] {k : ℕ} + +theorem cap_nonneg (I : R → Input Ω) {β factor : ℝ} (hβ : allowed β) + (hf : 0 ≤ factor) (ξ : ℝ) (t : ℕ) (ω : Ω) : + 0 ≤ FineVariation.cap I β ξ factor t ω := by + apply add_nonneg + · exact sum_nonneg fun r _ => by split_ifs <;> norm_num + · apply mul_nonneg hf + exact sum_nonneg fun r _ => by split_ifs <;> first | exact le_rfl | exact (term_range _ hβ _ _ _).1 + +theorem cap_bound (I : R → Input Ω) {β ξ factor : ℝ} (hβ : allowed β) + (hξ : 0 < ξ) (hξ' : ξ ≤ 1/2) (hf : factor ∈ Set.Icc (0:ℝ) 2) (t : ℕ) (ω : Ω) : + FineVariation.cap I β ξ factor t ω ≤ 120*heldSize I t ω := by + have ht := flex_nonneg I hβ t ω + have hn : 0 ≤ coreCount (fun r => core (I r) t ω) := + sum_nonneg fun r _ => by split_ifs <;> norm_num + have he := (flex_comparison I hβ hξ t ω).2 + have he0 : 0 ≤ estimateFlex I β ξ t ω := + sum_nonneg fun r _ => by split_ifs <;> first | exact le_rfl | exact (term_range _ hβ _ _ _).1 + have hfac : factor*estimateFlex I β ξ t ω ≤ 3*trueFlex I β t ω := by + have h1 := mul_le_mul_of_nonneg_right hf.2 he0 + have h2 := mul_le_mul_of_nonneg_right hξ' ht + nlinarith only [he,h1,h2] + cases hb : activeState I t ω with + | none => + have hz := inactive_zero I t ω β hb + have hz' : trueFlex I β t ω=0 := hz.2.2 + unfold FineVariation.cap + rw [hz.1,CoarseBridge.held_none I t ω hb] + linarith + | some b => + have ha := (active_accuracy I t ω hb).1 + have hd := core_flex_domination (fun r => core (I r) t ω) β + (fun r => (I r).posterior t ω) hβ (fun r => ((I r).posterior_range t ω).1) + (fun r hr => by + have hp := core_small (I r) t ω hr + norm_num [pstar] at hp ⊢ + linarith) + change coreCount (fun r => core (I r) t ω)+trueFlex I β t ω ≤ _ at hd + unfold FineVariation.cap + rw [CoarseBridge.held_some I t ω hb] + linarith + +theorem coarse_comparison (I : R → Input Ω) {β : ℝ} (hβ : allowed β) (t : ℕ) (ω : Ω) : + trueFlex I β t ω/2 ≤ estimateFlex I 3 sizeTolerance t ω ∧ + estimateFlex I 3 sizeTolerance t ω ≤ 2*trueFlex I β t ω := by + have ht := flex_nonneg I hβ t ω + have hg := gap_bounds I hβ t ω + have hg0 : 0 ≤ flexGap I t ω := le_trans (div_nonneg ht (by norm_num)) hg.1 + have hsmall : 0 ≤ β-3 ∧ β-3 ≤ 1/100 := by + norm_num [allowed] at hβ + constructor <;> linarith [hβ.1,hβ.2] + have he := flex_affine I β 3 t ω + have hm := mul_nonneg hsmall.1 hg0 + have hm' := mul_le_mul hsmall.2 hg.2 hg0 (by norm_num : (0:ℝ) ≤ 1/100) + have hlo : trueFlex I β t ω ≤ trueFlex I 3 t ω := by nlinarith only [he,hm] + have hhi : trueFlex I 3 t ω ≤ (101/100)*trueFlex I β t ω := by nlinarith only [he,hm'] + have hc := flex_comparison I (show allowed 3 by norm_num [allowed]) + (show 0 < sizeTolerance by norm_num [sizeTolerance]) t ω + norm_num [sizeTolerance] at hc ⊢ + constructor <;> linarith [hc.1,hc.2] + +theorem held_error (d : StarRanks.Data Ω ι k) (hk : 1 ≤ k) {δ ρ : ℝ} + (hδ : 0 < δ) (hρ : 0 < ρ) (t : ℕ) (ω : Ω) (i : ι) : + |estimateFlex (StarRanks.input d i) (ParentBeta.heldParam (StarRanks.parentInput d) δ k t ω) + (ρ/EpochAlpha.ell k) t ω- + trueFlex (StarRanks.input d i) (ParentBeta.trueParam (StarRanks.parentInput d) k t ω) t ω| ≤ + ((ρ+4*ParentBeta.cBeta*δ)/EpochAlpha.ell k)* + trueFlex (StarRanks.input d i) (ParentBeta.trueParam (StarRanks.parentInput d) k t ω) t ω := by + have hb := ParentBeta.true_allowed (StarRanks.parentInput d) hk t ω (StarRanks.parent_size_bound d t ω) + have hh := ParentBeta.held_allowed (StarRanks.parentInput d) hδ hk t ω (StarRanks.parent_size_bound d t ω) + have hp := ParentBeta.parameter_accuracy (StarRanks.parentInput d) hδ hk t ω + have hell : 0 < EpochAlpha.ell k := lt_of_lt_of_le (by norm_num) (EpochAlpha.ell_one k) + have he := parameter_error (StarRanks.input d i) hb hh (div_pos hρ hell) hp.1 hp.2 t ω + convert he using 1 + ring + +theorem regular_comparison (d : StarRanks.Data Ω ι k) (hk : 1 ≤ k) {δ ρ lam ε : ℝ} + (hδ : 0 < δ) (hρ : 0 < ρ) (hlam : 0 ≤ lam) (hlam' : lam ≤ 1/4) + (he : ρ+4*ParentBeta.cBeta*δ ≤ lam/4) + (hlg : 4*lam ≤ (ParentBeta.cBeta/50)/96) (hle : 4*lam ≤ ε) + (t : ℕ) (ω : Ω) (s : EpochGeometry.State ι) (hs : EpochGeometry.valid (StarRanks.held d t ω) s) + (i : ι) (ha : 0 < StarRanks.held d t ω i) (hi : EpochGeometry.dominant s ≠ some i) : + trueFlex (StarRanks.input d i) (ParentBeta.trueParam (StarRanks.input d i) k t ω) t ω ≤ + (1-lam/EpochAlpha.ell k)*estimateFlex (StarRanks.input d i) + (ParentBeta.heldParam (StarRanks.parentInput d) δ k t ω) (ρ/EpochAlpha.ell k) t ω ∧ + (1-lam/EpochAlpha.ell k)*estimateFlex (StarRanks.input d i) + (ParentBeta.heldParam (StarRanks.parentInput d) δ k t ω) (ρ/EpochAlpha.ell k) t ω ≤ + trueFlex (StarRanks.input d i) (ParentBeta.trueParam (StarRanks.parentInput d) k t ω) t ω := by + have hb := ParentBeta.true_allowed (StarRanks.parentInput d) hk t ω (StarRanks.parent_size_bound d t ω) + have hell : 0 < EpochAlpha.ell k := lt_of_lt_of_le (by norm_num) (EpochAlpha.ell_one k) + have hg := StarParameters.regular_gap d hk t ω s hs i ha hi + have hg0 : 0 ≤ (ParentBeta.cBeta/50)/EpochAlpha.ell k := by + exact div_nonneg (by norm_num [ParentBeta.cBeta]) hell.le + have hheight := EpochParameters.regular_height hs ha hi + have hgap : (ParentBeta.cBeta/50)/EpochAlpha.ell k ≤ + ParentBeta.trueParam (StarRanks.input d i) k t ω-ParentBeta.trueParam (StarRanks.parentInput d) k t ω := + le_trans (by simpa using mul_le_mul_of_nonneg_left hheight hg0) hg + have hf := child_slack (StarRanks.input d i) hb hgap + (show 0 ≤ ParentBeta.cBeta/50 by norm_num [ParentBeta.cBeta]) hell t ω + have he0 : 0 ≤ ρ+4*ParentBeta.cBeta*δ := by + have hcb : 0 ≤ 4*ParentBeta.cBeta := by norm_num [ParentBeta.cBeta] + exact add_nonneg hρ.le (mul_nonneg hcb hδ.le) + exact (FineCaps.caps (EpochAlpha.ell_one k) hlam hlam' he0 he hlg hle + (flex_nonneg _ hb t ω) (held_error d hk hδ hρ t ω i) hf).1 + +end UniformKServer.ActualFineCaps + +end + + +end + +end OAI diff --git a/lean/OAI/Combinatorics/UniformKServer/ActualLevelPilot.lean b/lean/OAI/Combinatorics/UniformKServer/ActualLevelPilot.lean new file mode 100644 index 0000000000000000000000000000000000000000..84e6f6760b429ed48c7243531980e36b1705ba0f --- /dev/null +++ b/lean/OAI/Combinatorics/UniformKServer/ActualLevelPilot.lean @@ -0,0 +1,150 @@ +import Mathlib +import OAI.Combinatorics.UniformKServer.PartitionLevel +import OAI.Combinatorics.UniformKServer.TierShortSchedule + +namespace OAI + + +section + + + +noncomputable section +namespace UniformKServer.PartitionLevel.Input +open Finset PilotCompact +open scoped Classical +variable {X : Type} [Fintype X] [MetricSpace X] {N : ℕ} + +def longParameter (I : PartitionLevel.Input X N) (t : ℕ) : X→ℝ := + HeavyPilot.parameter I.r (HeavySchedule.centers I.r I.data.heavyFlag I.data.point t) + +theorem long_bounds (I : PartitionLevel.Input X N) (t : ℕ) (p : X) : + I.longParameter t p∈Set.Icc (0:ℝ) 1 := HeavyPilot.parameter_bounds _ _ _ + +theorem long_lip (I : PartitionLevel.Input X N) (t : ℕ) (p q : X) : + |I.longParameter t p-I.longParameter t q|≤dist p q/I.r := + HeavyPilot.parameter_lip I.r I.r_pos _ p q + +def shortProb (I : PartitionLevel.Input X N) (i : Fin I.height) : ℝ := + 1/Real.sqrt (I.data.K i:ℝ) + +theorem shortProb_bounds (I : PartitionLevel.Input X N) (i : Fin I.height) : + I.shortProb i∈Set.Icc (0:ℝ) 1 := by + have hk : (1:ℝ)≤I.data.K i := by exact_mod_cast (show 1≤I.data.K i by have := I.data.two i; omega) + have hs : 1≤Real.sqrt (I.data.K i:ℝ) := (Real.le_sqrt (by norm_num) (by positivity)).mpr (by simpa using hk) + constructor + · unfold shortProb; positivity + · exact (div_le_one (by linarith)).mpr hs + +def shortParameter (I : PartitionLevel.Input X N) (i : Fin I.height) (t : ℕ) : X→ℝ := + TierShortSchedule.parameter I.r (I.data.K i) (I.shortProb i) (I.shortProb_bounds i) + (I.data.qualify i) I.center t + +theorem short_bounds (I : PartitionLevel.Input X N) (i : Fin I.height) (t : ℕ) (p : X) : + I.shortParameter i t p∈Set.Icc (0:ℝ) 1 := ShortRoster.parameter_bounds _ _ _ _ + +theorem short_lip (I : PartitionLevel.Input X N) (i : Fin I.height) (t : ℕ) (p q : X) : + |I.shortParameter i t p-I.shortParameter i t q|≤8*(dist p q/I.r) := by + simpa only [shortParameter,TierShortSchedule.parameter,mul_div_assoc] using ShortRoster.parameter_lip + (TierShortSchedule.run I.r (I.data.K i) (I.shortProb i) (I.shortProb_bounds i) + (I.data.qualify i) I.center t) I.center I.r I.r_pos p q + +def longCharge (I : PartitionLevel.Input X N) (t : ℕ) : ℝ := + if ht : t [exact I.μ_nonneg ⟨t,ht⟩ p; exact le_rfl] + all_goals exact le_rfl + +theorem short_charge_nonneg (I : PartitionLevel.Input X N) (i : Fin I.height) (t : ℕ) : + 0≤I.shortCharge i t := by + unfold shortCharge + split_ifs with ht hn + · exact div_nonneg (mul_nonneg I.r_pos.le + (TierPilot.insertionMass_bounds I.P I.r I.r_pos.le _ (I.μ_nonneg ⟨t,ht⟩) _).1) (Nat.cast_nonneg _) + all_goals exact le_rfl + +theorem long_payment (I : PartitionLevel.Input X N) (t : ℕ) (μ : X→ℝ) + (hμ : ∀ ht : t hf h.1 + simp only [longCharge,dite_eq_right ht,longParameter,HeavySchedule.centers,HeavySchedule.step,ite_eq_right hn,sub_self,le_refl] + +theorem short_payment (I : PartitionLevel.Input X N) (i : Fin I.height) + (hlarge : Real.exp (TierInsertion.lifetimeExponent I.P*DyadicTiers.value i.val)≤I.data.K i) + (t : ℕ) (μ : X→ℝ) (hμ : ∀ ht : t