import OAI.Combinatorics.ContingencyTables.Counting.CompanionOracleRational import OAI.Combinatorics.ContingencyTables.Machines.RationalIntervalProgram namespace OAI /-! # Integral numerators for the literal forest penalty Cut signs and affine completion coefficients are integers in {-1,0,1}. At an integral bin corner the tree value has the single denominator B times its capacity. This identifies the integer arithmetic consumed by the executable interval-penalty program. -/ namespace ContingencyTables.ForestIntegerEvaluation open CompanionFactorialApplication CompanionOracleDensity ForestAffineCoefficients open ForestAssignmentSplit GraphCompletionCounts open scoped BigOperators Classical noncomputable section variable {I J : Type*} [Fintype I] [Fintype J] def indicator (b : Bool) : ℤ := if b then 1 else 0 def sign (A : Instance I J) (t : TreeCell A.T) (a : I×J) : ℤ := indicator (cutSide A t (.inl a.1))-indicator (cutSide A t (.inr a.2)) lemma sign_cast (A : Instance I J) (t : TreeCell A.T) (a : I×J) : (sign A t a:ℚ)=rationalSign A t a := by cases hr : cutSide A t (.inl a.1) <;> cases hc : cutSide A t (.inr a.2) <;> norm_num [sign,indicator,rationalSign,rationalIndicator,hr,hc] lemma selfSign_square (A : Instance I J) (t : TreeCell A.T) : (rationalSign A t t.val)^2=1 := by have hn := (cut_properties A t).2.2 cases hr : cutSide A t (.inl t.val.1) <;> cases hc : cutSide A t (.inr t.val.2) <;> simp_all [rationalSign,rationalIndicator] def coefficient (A : Instance I J) (t : TreeCell A.T) (a : I×J) : ℤ := if A.small a.1 a.2=false ∧ ¬A.T.Adj (.inl a.1) (.inr a.2) then -(sign A t a)*sign A t t.val else 0 lemma coefficient_cast (A : Instance I J) (t : TreeCell A.T) (a : I×J) : (coefficient A t a:ℚ)=rationalCoefficient A t a := by have hs := selfSign_square A t have hn : rationalSign A t t.val≠0 := by intro h; rw [h] at hs; norm_num at hs unfold coefficient rationalCoefficient split_ifs · simp only [Int.cast_mul,Int.cast_neg,sign_cast] apply (eq_div_iff hn).mpr calc (-(rationalSign A t a)*rationalSign A t t.val)*rationalSign A t t.val= -(rationalSign A t a)*(rationalSign A t t.val)^2 := by ring _ = _ := by rw [hs,mul_one] · exact Int.cast_zero def smallContribution (A : Instance I J) (t : TreeCell A.T) (x q : I → J → ℤ) : ℤ := ∑ a : I×J,if A.small a.1 a.2 then indicator (cutSide A t (.inl a.1))*(x a.1 a.2-A.X a.1 a.2)- indicator (cutSide A t (.inr a.2))*(q a.1 a.2-A.X a.1 a.2) else 0 lemma smallContribution_cast (A : Instance I J) (t : TreeCell A.T) (x q : I → J → ℤ) : (smallContribution A t x q:ℚ)=rationalSmallContribution A t x q := by unfold smallContribution rationalSmallContribution rw [Int.cast_sum] apply Finset.sum_congr rfl intro a _ split_ifs · cases hr : cutSide A t (.inl a.1) <;> cases hc : cutSide A t (.inr a.2) <;> simp_all [indicator,rationalIndicator] · exact Int.cast_zero def offset (A : Instance I J) (t : TreeCell A.T) (x q : I → J → ℤ) : ℤ := A.X t.val.1 t.val.2-smallContribution A t x q*sign A t t.val- ∑ a : I×J,coefficient A t a*A.X a.1 a.2 lemma offset_cast (A : Instance I J) (t : TreeCell A.T) (x q : I → J → ℤ) : (offset A t x q:ℚ)=rationalOffset A t x q := by have hs := selfSign_square A t have hn : rationalSign A t t.val≠0 := by intro h; rw [h] at hs; norm_num at hs have he : rationalSmallContribution A t x q*rationalSign A t t.val= rationalSmallContribution A t x q/rationalSign A t t.val := by apply (eq_div_iff hn).mpr rw [mul_assoc,←pow_two,hs,mul_one] simp only [offset,rationalOffset,Int.cast_sub,Int.cast_mul,Int.cast_sum, smallContribution_cast,sign_cast,coefficient_cast,he] def numerator (A : Instance I J) (C : ℕ) (t : TreeCell A.T) (x q : I → J → ℤ) (v : FreeCell A.small A.T → ℕ) : ℤ := (rationalBinWidth A C:ℤ)*offset A t x q+ ∑ f : FreeCell A.small A.T, coefficient A t f.val*((A.b f.val.1 f.val.2:ℤ)+1)*(v f:ℤ) /-- The exact affine tree value is a signed integral numerator divided by the explicit natural denominator, even when the original margins have very large binary representations. -/ theorem numerator_div (A : Instance I J) (C : ℕ) (hC : 0 (v f:ℚ)) t := by have hd : (0:ℚ) (v f:ℚ)) t) (rationalScaledTree A C x q (fun f => (v f:ℚ)) t-1)) := by have hB : 0