import OAI.Combinatorics.ContingencyTables.Dense.DenseAxisProgram namespace OAI /-! # Natural coordinate encodings of dense axis moves -/ namespace ContingencyTables.DensePrograms open ProfilePrograms GridBoundary open scoped Classical lemma listExchange_increment {q : ℕ} (v : Fin q → ℕ) (i : Fin q) : listExchange (List.ofFn v,(i.val,q))= List.ofFn (Function.update v i (v i+1)) := by apply List.ext_getElem · simp only [listExchange,List.length_map,List.length_range,List.length_ofFn] · intro j hj _ have hjq : j hji (Fin.ext h) simp only [hne,ite_false,Nat.add_zero,Function.update_of_ne hji] lemma listExchange_decrement {q : ℕ} (v : Fin q → ℕ) (i : Fin q) : listExchange (List.ofFn v,(q,i.val))= List.ofFn (Function.update v i (v i-1)) := by apply List.ext_getElem · simp only [listExchange,List.length_map,List.length_range,List.length_ofFn] · intro j hj _ have hjq : j hji (Fin.ext h) simp only [hne,ite_false,Nat.sub_zero,Function.update_of_ne hji] def gridNatCode {q K : ℕ} (a : Grid (I := Fin q) K) : List ℕ := List.ofFn (fun i => (corner a i).toNat) lemma gridNatCode_getD {q K : ℕ} (a : Grid (I := Fin q) K) (i : Fin q) : (gridNatCode a).getD i.val 0=(corner a i).toNat := by simp only [gridNatCode,List.getD_eq_getElem?_getD,List.getElem?_ofFn,i.isLt, dite_true,Option.getD_some,Fin.eta] lemma gridNatCode_update {q K : ℕ} (a : Grid (I := Fin q) K) (i : Fin q) (v : Set.Icc (0 : ℤ) ((K : ℤ)-1)) : gridNatCode (Function.update a i v)= List.ofFn (Function.update (fun j => (corner a j).toNat) i v.val.toNat) := by apply congrArg List.ofFn funext j by_cases hji : j=i · subst j simp only [Function.update_self,corner] · simp only [Function.update_of_ne hji,corner] /-- The bounded list operation implements exactly the signed geometric axis proposal, including both boundary rejections. -/ theorem axisListStep_eq {q K : ℕ} (a : Grid (I := Fin q) K) (i : Fin q) (sign : Bool) : axisListStep (((K,gridNatCode a),i.val),sign)= (axisStep a i sign).map gridNatCode := by have hlen : (gridNatCode a).length=q := by simp only [gridNatCode,List.length_ofFn] have hnonneg : 0≤corner a i := (a i).property.1 have hupper : corner a i≤(K : ℤ)-1 := (a i).property.2 have hcast : ((corner a i).toNat : ℤ)=corner a i := Int.toNat_of_nonneg hnonneg cases sign with | false => simp only [axisListStep,hlen,i.isLt,ite_true,Bool.false_eq_true,ite_false, gridNatCode_getD,axisStep] by_cases h : 0≤corner a i-1 · have hn : 0<(corner a i).toNat := by omega rw [ite_eq_left hn,dite_eq_left h,Option.map_some] let v : Set.Icc (0 : ℤ) ((K : ℤ)-1) := ⟨corner a i-1,h,by omega⟩ have hc := gridNatCode_update a i v have he : v.val.toNat=(corner a i).toNat-1 := by dsimp only [v]; omega rw [he] at hc exact congrArg some ((listExchange_decrement (fun j => (corner a j).toNat) i).trans hc.symm) · have hn : ¬0<(corner a i).toNat := by omega rw [ite_eq_right hn,dite_eq_right h,Option.map_none] | true => simp only [axisListStep,hlen,i.isLt,ite_true,gridNatCode_getD,axisStep] by_cases h : corner a i+1≤(K : ℤ)-1 · have hn : (corner a i).toNat+1 (corner a j).toNat) i).trans hc.symm) · have hn : ¬(corner a i).toNat+1