import OAI.Combinatorics.ContingencyTables.Machines.BinaryExchangeProgram import OAI.Combinatorics.ContingencyTables.Dense.DenseIntegerProgram import OAI.Combinatorics.ContingencyTables.Counting.CompanionFactorialApplication namespace OAI /-! # Computing the companion's retained small-cell capacities The program scans the rectangular cell list determined by the two margin-list lengths, then tests the binary cell bound against W. It does not enumerate up to the numeric value of a capacity. -/ namespace ContingencyTables.CompanionCellCatalog open MatchingFPRAS.TreeTyped ProfilePrograms DensePrograms abbrev Input := ((List ℕ×List ℕ)×MatrixCode)×ℕ def entry (z : Input×(ℕ×ℕ)) : ℕ := matrixEntry (z.1.1.2,z.2) def entryRealizer : Realizer entry := composition (pair (composition (composition first first) second) second) matrixEntryRealizer lemma polynomial_entry : PolynomialTime entryRealizer := polynomial_composition (polynomial_pair (polynomial_composition (polynomial_composition polynomial_first polynomial_first) polynomial_second) polynomial_second) polynomial_matrixEntry def retainedTest (z : Input×(ℕ×ℕ)) : Bool := decide (entry z retainedTest (z,a)) def catalogRealizer : Realizer catalog := composition (pair identity (composition (composition first first) cellPairsRealizer)) (filterWith retainedTestRealizer) theorem polynomial_catalog : PolynomialTime catalogRealizer := polynomial_composition (polynomial_pair polynomial_identity (polynomial_composition (polynomial_composition polynomial_first polynomial_first) polynomial_cellPairs)) (polynomial_filterWith polynomial_retainedTest) def capacities (z : Input) : List ℕ := (catalog z).map (fun a => entry (z,a)) def capacitiesRealizer : Realizer capacities := composition (pair identity catalogRealizer) (mapWith entryRealizer) theorem polynomial_capacities : PolynomialTime capacitiesRealizer := polynomial_composition (polynomial_pair polynomial_identity polynomial_catalog) (polynomial_mapWith polynomial_entry) lemma catalog_nodup (z : Input) : (catalog z).Nodup := (cellPairs_nodup z.1.1.1 z.1.1.2).filter _ noncomputable section open CompanionFactorialApplication open scoped Classical variable {m n : ℕ} def input (A : Instance (Fin m) (Fin n)) : Input := (((List.ofFn (fun i => (A.r i).toNat),List.ofFn (fun j => (A.c j).toNat)), matrixCode A.b),A.W) lemma small_iff_bound (A : Instance (Fin m) (Fin n)) (i : Fin m) (j : Fin n) : A.small i j=true ↔ A.b i j (A.r i).toNat), List.ofFn (fun j => (A.c j).toNat)) ∧ retainedTest (input A,ij)=true) ↔ _ rw [mem_cellPairs] simp only [List.length_ofFn] constructor · rintro ⟨⟨hi,hj⟩,hb⟩ let i : Fin m := ⟨ij.1,hi⟩ let j : Fin n := ⟨ij.2,hj⟩ have he : A.b i j (a.val.1.val,a.val.2.val)) h rw [catalogCell_coordinates,catalogCell_coordinates] at he exact Fin.ext ((catalog_nodup (input A)).getElem_inj_iff.mp he) · intro a have hm : (a.val.1.val,a.val.2.val)∈catalog (input A) := (mem_catalog_input A _).mpr ⟨a,rfl⟩ obtain ⟨i,hi,hval⟩ := List.mem_iff_getElem.mp hm refine ⟨⟨i,hi⟩,?_⟩ have he := (catalogCell_coordinates A ⟨i,hi⟩).trans hval apply Subtype.ext exact Prod.ext (Fin.ext (congrArg Prod.fst he)) (Fin.ext (congrArg Prod.snd he)) def cellEquiv (A : Instance (Fin m) (Fin n)) : Fin (catalog (input A)).length ≃ SmallCell A := Equiv.ofBijective (catalogCell A) (catalogCell_bijective A) lemma capacities_eq (A : Instance (Fin m) (Fin n)) : capacities (input A)=List.ofFn (fun i => A.b ((cellEquiv A) i).val.1 ((cellEquiv A) i).val.2) := by apply List.ext_getElem · simp only [capacities,List.length_map,List.length_ofFn] · intro i hi hj have hi' : i<(catalog (input A)).length := by simpa only [List.length_ofFn] using hj simp only [capacities,List.getElem_map,List.getElem_ofFn] have he := catalogCell_coordinates A ⟨i,hi'⟩ change entry (input A,(catalog (input A))[i]'hi')= A.b (catalogCell A ⟨i,hi'⟩).val.1 (catalogCell A ⟨i,hi'⟩).val.2 rw [←he] exact entry_input A _ _ end end ContingencyTables.CompanionCellCatalog end OAI