Download data/lean/OAI/Combinatorics/MatchingCount/Sampling/Components.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 15.7 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/MatchingCount/Sampling/Components.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/MatchingCount/Sampling/Components.lean
-
curl -L -o Components.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/MatchingCount/Sampling/Components.lean
15.7 kB
| import Mathlib | |
| import OAI.Combinatorics.MatchingCount.Sampling.IntervalPattern | |
| namespace OAI | |
| namespace MatchingFPRAS.Transport | |
| section | |
| variable {V E : Type*} [DecidableEq E] | |
| variable {inc : E β V β Prop} | |
| noncomputable def components (U : Finset E) : Finset (Finset E) := by | |
| classical | |
| exact U.image (componentEdges (inc := inc) U) | |
| lemma mem_components {U S : Finset E} : | |
| S β components (inc := inc) U β β a β U, componentEdges (inc := inc) U a = S := by | |
| classical | |
| simp only [components, Finset.mem_image] | |
| omit [DecidableEq E] in | |
| lemma componentEdges_subset (U : Finset E) (a : E) : componentEdges (inc := inc) U a β U := by | |
| intro e he | |
| exact ((mem_componentEdges U a e).mp he).1 | |
| omit [DecidableEq E] in | |
| lemma componentEdges_self {U : Finset E} {a : E} (ha : a β U) : | |
| a β componentEdges (inc := inc) U a := | |
| (mem_componentEdges U a a).mpr β¨ha,SimpleGraph.Reachable.refl aβ© | |
| omit [DecidableEq E] in | |
| lemma componentEdges_eq {U : Finset E} {a b : E} | |
| (hab : (overlayLineGraph inc U).Reachable a b) : | |
| componentEdges (inc := inc) U a = componentEdges (inc := inc) U b := by | |
| ext e | |
| simp only [mem_componentEdges] | |
| exact and_congr_right (fun _ => β¨fun h => hab.symm.trans h, fun h => hab.trans hβ©) | |
| lemma component_nonempty {U S : Finset E} (hS : S β components (inc := inc) U) : S.Nonempty := by | |
| obtain β¨a,ha,rflβ© := mem_components.mp hS | |
| exact β¨a,componentEdges_self haβ© | |
| lemma component_subset {U S : Finset E} (hS : S β components (inc := inc) U) : S β U := by | |
| obtain β¨a,ha,rflβ© := mem_components.mp hS | |
| exact componentEdges_subset U a | |
| lemma component_eq_of_shares {U S T : Finset E} | |
| (hS : S β components (inc := inc) U) (hT : T β components (inc := inc) U) | |
| {e f : E} (he : e β S) (hf : f β T) {v : V} (hev : inc e v) (hfv : inc f v) : S=T := by | |
| obtain β¨a,ha,rflβ© := mem_components.mp hS | |
| obtain β¨b,hb,rflβ© := mem_components.mp hT | |
| obtain β¨heu,hraβ© := (mem_componentEdges U a e).mp he | |
| obtain β¨hfu,hrbβ© := (mem_componentEdges U b f).mp hf | |
| exact componentEdges_eq ((hra.trans (shares_reachable heu hfu hev hfv)).trans hrb.symm) | |
| lemma components_pairwise_disjoint (U : Finset E) : | |
| (components (inc := inc) U : Set (Finset E)).PairwiseDisjoint id := by | |
| intro S hS T hT hne | |
| apply Finset.disjoint_left.mpr | |
| intro e he hf | |
| apply hne | |
| obtain β¨a,ha,rflβ© := mem_components.mp hS | |
| obtain β¨b,hb,rflβ© := mem_components.mp hT | |
| obtain β¨_,hraβ© := (mem_componentEdges U a e).mp he | |
| obtain β¨_,hrbβ© := (mem_componentEdges U b e).mp hf | |
| exact componentEdges_eq (hra.trans hrb.symm) | |
| lemma components_cover (U : Finset E) : (components (inc := inc) U).biUnion id = U := by | |
| classical | |
| ext e | |
| simp only [Finset.mem_biUnion, id_eq] | |
| constructor | |
| Β· rintro β¨S,hS,heβ© | |
| exact component_subset hS he | |
| Β· intro he | |
| exact β¨componentEdges (inc := inc) U e, mem_components.mpr β¨e,he,rflβ©, | |
| componentEdges_self heβ© | |
| lemma component_exists_vertex (hinc : β e, β v, inc e v) | |
| {U S : Finset E} (hS : S β components (inc := inc) U) : | |
| β v, β e β S, inc e v := by | |
| obtain β¨e,heβ© := component_nonempty hS | |
| obtain β¨v,hvβ© := hinc e | |
| exact β¨v,e,he,hvβ© | |
| noncomputable def componentVertex (hinc : β e, β v, inc e v) | |
| {U : Finset E} (S : β₯(components (inc := inc) U)) : V := | |
| (component_exists_vertex hinc S.property).choose | |
| lemma componentVertex_injective (hinc : β e, β v, inc e v) (U : Finset E) : | |
| Function.Injective (componentVertex hinc (U := U)) := by | |
| intro S T h | |
| obtain β¨e,he,hevβ© := (component_exists_vertex hinc S.property).choose_spec | |
| obtain β¨f,hf,hfvβ© := (component_exists_vertex hinc T.property).choose_spec | |
| apply Subtype.ext | |
| apply component_eq_of_shares S.property T.property he hf hev | |
| change inc f (componentVertex hinc T) at hfv | |
| change inc f (componentVertex hinc S) | |
| rwa [h] | |
| lemma components_card_le [Fintype V] (hinc : β e, β v, inc e v) (U : Finset E) : | |
| (components (inc := inc) U).card β€ Fintype.card V := by | |
| simpa only [Fintype.card_coe] using | |
| Fintype.card_le_of_injective (componentVertex hinc (U := U)) (componentVertex_injective hinc U) | |
| lemma component_exchange_compatible {M N S : Finset E} | |
| (hS : S β components (inc := inc) (M βͺ N)) : ExchangeCompatible (inc := inc) M N S := by | |
| obtain β¨a,_,rflβ© := mem_components.mp hS | |
| exact component_compatible M N a | |
| namespace IndexedCycle | |
| variable (c : IndexedCycle inc) | |
| noncomputable def edgeSet : Finset E := Finset.univ.image c.edge | |
| lemma mem_edgeSet (e : E) : e β c.edgeSet β β i, i < c.size β§ c.edgeAt i = e := by | |
| simp only [edgeSet, Finset.mem_image, Finset.mem_univ, true_and] | |
| constructor | |
| Β· rintro β¨i,rflβ© | |
| exact β¨i.val,i.isLt,by simp [edgeAt,index,Nat.mod_eq_of_lt i.isLt]β© | |
| Β· rintro β¨i,hi,rflβ© | |
| exact β¨c.index i,rflβ© | |
| lemma edgeAt_mem_edgeSet (i : β) : c.edgeAt i β c.edgeSet := by | |
| apply (c.mem_edgeSet _).mpr | |
| exact β¨i % c.size,Nat.mod_lt _ (by have := c.four_le; omega), c.edgeAt_mod iβ© | |
| lemma edgeSet_subset {U : Finset E} (hU : β i, c.edgeAt i β U) : c.edgeSet β U := by | |
| intro e he | |
| obtain β¨i,_,rflβ© := (c.mem_edgeSet e).mp he | |
| exact hU i | |
| omit [DecidableEq E] in | |
| lemma previous_indices (i : β) (hi : i < c.size) : | |
| let j := (i+c.size-1) % c.size | |
| c.index (j+1) = c.index i := by | |
| dsimp only | |
| apply Fin.ext | |
| dsimp only [index] | |
| rw [Nat.add_mod, Nat.mod_mod, β Nat.add_mod] | |
| have he : i + c.size - 1 + 1 = i + c.size := by have := c.four_le; omega | |
| rw [he, Nat.add_mod, Nat.mod_self, add_zero, Nat.mod_mod] | |
| variable [Fintype V] | |
| lemma edge_incident_cycle_mem {M N : Finset E} | |
| (hM : MatchingOn inc M Finset.univ) (hN : MatchingOn inc N Finset.univ) | |
| (hU : β i, c.edgeAt i β M βͺ N) {e : E} (he : e β M βͺ N) | |
| (i : β) (hi : i < c.size) (hev : inc e (c.vertexAt i)) : e β c.edgeSet := by | |
| let j := (i+c.size-1) % c.size | |
| have hj : j < c.size := Nat.mod_lt _ (by have := c.four_le; omega) | |
| have heq : c.edgeAt (j+1) = c.edgeAt i := congrArg c.edge (c.previous_indices i hi) | |
| have hvq : c.vertexAt (j+1) = c.vertexAt i := congrArg c.vertex (c.previous_indices i hi) | |
| have hji : c.edgeAt j β c.edgeAt i := by rw [β heq]; exact c.consecutive_ne j hj | |
| have hiinc := (c.edgeAt_incidence i (c.vertexAt i)).mpr (Or.inl rfl) | |
| have hjinc : inc (c.edgeAt j) (c.vertexAt i) := | |
| (c.edgeAt_incidence j _).mpr (Or.inr hvq.symm) | |
| have hcomp : c.edgeAt i β M β c.edgeAt j β M := by | |
| rw [β heq]; exact c.adjacent_mem_complement hM hN hU j hj | |
| by_cases hjM : c.edgeAt j β M | |
| Β· have hiN : c.edgeAt i β N := | |
| (Finset.mem_union.mp (hU i)).resolve_left (fun hiM => (hcomp.mp hiM) hjM) | |
| rcases Finset.mem_union.mp he with heM | heN | |
| Β· rw [hM.unique (Finset.mem_univ _) heM hev hjM hjinc] | |
| exact c.edgeAt_mem_edgeSet j | |
| Β· rw [hN.unique (Finset.mem_univ _) heN hev hiN hiinc] | |
| exact c.edgeAt_mem_edgeSet i | |
| Β· have hjN := (Finset.mem_union.mp (hU j)).resolve_left hjM | |
| have hiM := hcomp.mpr hjM | |
| rcases Finset.mem_union.mp he with heM | heN | |
| Β· rw [hM.unique (Finset.mem_univ _) heM hev hiM hiinc] | |
| exact c.edgeAt_mem_edgeSet i | |
| Β· rw [hN.unique (Finset.mem_univ _) heN hev hjN hjinc] | |
| exact c.edgeAt_mem_edgeSet j | |
| lemma edgeSet_closed {M N : Finset E} | |
| (hM : MatchingOn inc M Finset.univ) (hN : MatchingOn inc N Finset.univ) | |
| (hU : β i, c.edgeAt i β M βͺ N) {e f : E} (he : e β c.edgeSet) | |
| (hef : (overlayLineGraph inc (M βͺ N)).Adj e f) : f β c.edgeSet := by | |
| obtain β¨i,hi,rflβ© := (c.mem_edgeSet e).mp he | |
| obtain β¨_,_,hf,v,hev,hfvβ© := hef | |
| rcases (c.edgeAt_incidence i v).mp hev with rfl | rfl | |
| Β· exact c.edge_incident_cycle_mem hM hN hU hf i hi hfv | |
| Β· have hi' : (i+1) % c.size < c.size := Nat.mod_lt _ (by have := c.four_le; omega) | |
| apply c.edge_incident_cycle_mem hM hN hU hf ((i+1)%c.size) hi' | |
| simpa only [c.vertexAt_mod] using hfv | |
| lemma edgeSet_eq_component {M N : Finset E} | |
| (hM : MatchingOn inc M Finset.univ) (hN : MatchingOn inc N Finset.univ) | |
| (hU : β i, c.edgeAt i β M βͺ N) : | |
| c.edgeSet = componentEdges (inc := inc) (M βͺ N) (c.edgeAt 0) := by | |
| ext e | |
| constructor | |
| Β· intro he | |
| obtain β¨i,hi,rflβ© := (c.mem_edgeSet e).mp he | |
| exact (mem_componentEdges _ _ _).mpr β¨hU i,c.edges_reachable _ hU iβ© | |
| Β· intro he | |
| obtain β¨heu,hrβ© := (mem_componentEdges _ _ _).mp he | |
| have hrel := (SimpleGraph.reachable_iff_reflTransGen _ _).mp hr | |
| clear he heu hr | |
| induction hrel with | |
| | refl => exact c.edgeAt_mem_edgeSet 0 | |
| | tail hpath hadj ih => exact c.edgeSet_closed hM hN hU ih hadj | |
| lemma edgeSet_mem_components {M N : Finset E} | |
| (hM : MatchingOn inc M Finset.univ) (hN : MatchingOn inc N Finset.univ) | |
| (hU : β i, c.edgeAt i β M βͺ N) : c.edgeSet β components (inc := inc) (M βͺ N) := by | |
| rw [c.edgeSet_eq_component hM hN hU] | |
| exact mem_components.mpr β¨c.edgeAt 0,hU 0,rflβ© | |
| lemma inter_edgeSet_parity {M N : Finset E} | |
| (hM : MatchingOn inc M Finset.univ) (hN : MatchingOn inc N Finset.univ) | |
| (hU : β i, c.edgeAt i β M βͺ N) : | |
| M β© c.edgeSet = c.parityPattern (if c.edgeAt 0 β M then 0 else 1) := by | |
| classical | |
| ext e | |
| simp only [Finset.mem_inter, c.mem_edgeSet, parityPattern, mem_intervalPattern] | |
| constructor | |
| Β· rintro β¨heM,i,hi,rflβ© | |
| refine β¨i, Nat.zero_le _,hi,?_,rflβ© | |
| have hp := (c.cycle_edge_mem_parity hM hN hU i).mp heM | |
| split_ifs at hp β’ <;> first | omega | tauto | |
| Β· rintro β¨i,_,hi,hpar,rflβ© | |
| refine β¨?_, i,hi,rflβ© | |
| apply (c.cycle_edge_mem_parity hM hN hU i).mpr | |
| split_ifs at hpar β’ <;> omega | |
| end IndexedCycle | |
| end | |
| variable {V E : Type*} [DecidableEq V] [DecidableEq E] | |
| variable {inc : E β V β Prop} | |
| namespace CellPaths | |
| variable (c : CellPaths inc) | |
| lemma mem_assemble_iff (M : Fin 4 β Finset E) (e : E) : | |
| e β assemble M β β i, e β M i := by | |
| simp only [assemble, Finset.mem_union] | |
| constructor | |
| Β· rintro ((h | h) | (h | h)) | |
| Β· exact β¨0,hβ© | |
| Β· exact β¨2,hβ© | |
| Β· exact β¨1,hβ© | |
| Β· exact β¨3,hβ© | |
| Β· rintro β¨i,hiβ© | |
| fin_cases i <;> tauto | |
| lemma assemble_update (M : Fin 4 β Finset E) | |
| (hd : Pairwise fun i j => Disjoint (M i) (M j)) (i : Fin 4) (Q : Finset E) : | |
| assemble (Function.update M i Q) = (assemble M \ M i) βͺ Q := by | |
| ext e | |
| simp only [mem_assemble_iff, Finset.mem_union, Finset.mem_sdiff] | |
| constructor | |
| Β· rintro β¨j,hjβ© | |
| by_cases hji : j = i | |
| Β· subst j | |
| exact Or.inr (by simpa using hj) | |
| Β· have hj' : e β M j := by simpa [Function.update_of_ne hji] using hj | |
| exact Or.inl β¨β¨j,hj'β©, fun hi => Finset.disjoint_left.mp (hd hji) hj' hiβ© | |
| Β· rintro (β¨β¨j,hjβ©,hiβ© | hq) | |
| Β· have hji : j β i := by rintro rfl; exact hi hj | |
| exact β¨j, by simpa [Function.update_of_ne hji] using hjβ© | |
| Β· exact β¨i, by simpa using hqβ© | |
| def conversionPieces (b : Bool) (S : Finset (Fin 4)) (i : Fin 4) : Finset E := | |
| if i β S then (c.side i).closed (c.chord i) else c.orientationPieces b i | |
| def converted (b : Bool) (S : Finset (Fin 4)) : Finset E := | |
| assemble (c.conversionPieces b S) | |
| lemma conversion_pieces_matching (b : Bool) (S : Finset (Fin 4)) | |
| (hS : β i β S, sideBit i = b) (i : Fin 4) : | |
| MatchingOn inc (c.conversionPieces b S i) (c.blocks b i) := by | |
| by_cases hi : i β S | |
| Β· simp only [conversionPieces, ite_eq_left hi, blocks, ite_eq_left (hS i hi)] | |
| exact (c.side i).closed_matching _ (c.chord_incidence i) | |
| Β· simp only [conversionPieces, ite_eq_right hi] | |
| exact c.orientation_pieces_matching b i | |
| lemma converted_matching (b : Bool) (S : Finset (Fin 4)) | |
| (hS : β i β S, sideBit i = b) : | |
| MatchingOn inc (c.converted b S) c.vertices := | |
| c.assembled_matching b _ (c.conversion_pieces_matching b S hS) | |
| lemma converted_empty (b : Bool) : c.converted b β = c.orientation b := by | |
| unfold converted orientation | |
| congr 1 | |
| lemma conversion_pieces_insert (b : Bool) (S : Finset (Fin 4)) (i : Fin 4) : | |
| c.conversionPieces b (insert i S) = | |
| Function.update (c.conversionPieces b S) i ((c.side i).closed (c.chord i)) := by | |
| funext j | |
| by_cases hji : j = i | |
| Β· subst j; simp [conversionPieces] | |
| Β· simp [conversionPieces, hji] | |
| lemma converted_insert (b : Bool) (S : Finset (Fin 4)) | |
| (hS : β j β S, sideBit j = b) (i : Fin 4) (hi : sideBit i = b) | |
| (hni : i β S) (hinc : β e, β v, inc e v) : | |
| c.converted b (insert i S) = | |
| (c.converted b S \ (c.side i).through) βͺ (c.side i).closed (c.chord i) := by | |
| rw [converted, c.conversion_pieces_insert] | |
| rw [assemble_update] | |
| Β· simp only [conversionPieces, ite_eq_right hni, orientationPieces, pieces, ite_eq_left hi, converted] | |
| Β· intro j k hjk | |
| exact (c.conversion_pieces_matching b S hS j).disjoint_edges | |
| (c.conversion_pieces_matching b S hS k) hinc (c.blocks_disjoint b hjk) | |
| lemma converted_singleton (b : Bool) (i : Fin 4) (hi : sideBit i = b) | |
| (hinc : β e, β v, inc e v) : | |
| c.converted b {i} = c.contextOne b i := by | |
| have h := c.converted_insert b β (by simp) i hi (by simp) hinc | |
| simpa only [converted_empty, Finset.insert_empty, contextOne] using h | |
| lemma converted_pair (b : Bool) (X Y : Fin 4) | |
| (hX : sideBit X = b) (hY : sideBit Y = b) (hne : X β Y) : | |
| c.converted b {X,Y} = c.switched b := by | |
| unfold converted switched | |
| congr 1 | |
| funext i | |
| have hh : i β ({X,Y} : Finset (Fin 4)) β sideBit i = b := by | |
| fin_cases X <;> fin_cases Y <;> fin_cases i <;> cases b <;> | |
| simp_all [sideBit] | |
| simp only [conversionPieces, hh, switchPieces, pieces, orientationPieces] | |
| split_ifs <;> rfl | |
| lemma double_conversion (b : Bool) (X Y : Fin 4) | |
| (hX : sideBit X = b) (hY : sideBit Y = b) (hne : X β Y) | |
| (hinc : β e, β v, inc e v) : | |
| (c.contextOne b Y \ (c.side X).through) βͺ (c.side X).closed (c.chord X) = | |
| c.switched b := by | |
| rw [β c.converted_singleton b Y hY hinc, β c.converted_insert b {Y} | |
| (by intro i hi; have hi := Finset.mem_singleton.mp hi; subst i; exact hY) X hX | |
| (by simpa only [Finset.mem_singleton] using hne) hinc] | |
| exact c.converted_pair b X Y hX hY hne | |
| def canonicalGain (R : Finset E) (f : Finset E β β) (b : Bool) (i : Fin 4) : β := | |
| f (R βͺ c.orientation b) - f (R βͺ c.contextOne b i) | |
| def contextError (R : Finset E) (f : Finset E β β) (b : Bool) (X Y : Fin 4) : β := | |
| (f (R βͺ c.contextOne b Y) - | |
| f (R βͺ ((c.contextOne b Y \ (c.side X).through) βͺ (c.side X).closed (c.chord X)))) - | |
| (f (R βͺ c.orientation b) - f (R βͺ c.contextOne b X)) | |
| lemma opposite_pair_identity (R : Finset E) (f : Finset E β β) (b : Bool) | |
| (X Y : Fin 4) (hX : sideBit X = b) (hY : sideBit Y = b) (hne : X β Y) | |
| (hinc : β e, β v, inc e v) : | |
| f (R βͺ c.orientation b) - f (R βͺ c.switched b) = | |
| c.canonicalGain R f b Y + c.canonicalGain R f b X + c.contextError R f b X Y := by | |
| rw [contextError, c.double_conversion b X Y hX hY hne hinc] | |
| unfold canonicalGain | |
| ring | |
| theorem single_cell_identity (R : Finset E) (f : Finset E β β) | |
| (hinc : β e, β v, inc e v) : | |
| f (R βͺ c.orientation true) - f (R βͺ c.orientation false) = | |
| (f (R βͺ c.switched true) - f (R βͺ c.switched false)) + | |
| (c.canonicalGain R f true 0 + c.canonicalGain R f true 2) - | |
| (c.canonicalGain R f false 1 + c.canonicalGain R f false 3) + | |
| c.contextError R f true 2 0 - c.contextError R f false 3 1 := by | |
| have hβ := c.opposite_pair_identity R f true 2 0 (by decide) (by decide) (by decide) hinc | |
| have hβ := c.opposite_pair_identity R f false 3 1 (by decide) (by decide) (by decide) hinc | |
| linarith | |
| end CellPaths | |
| end MatchingFPRAS.Transport | |
| end OAI | |