roseking's picture
Add lean source and binary files (part 41)
97c3405 verified
Raw History Blame Contribute Delete
4.79 kB
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<q := by
simpa only [listExchange,List.length_map,List.length_range,List.length_ofFn] using hj
simp only [listExchange,List.getElem_map,List.getElem_range,List.getElem_ofFn,
exchangeEntry,List.getD_eq_getElem?_getD,List.getElem?_ofFn,hjq,dite_true,
Option.getD_some,ite_eq_right (Nat.ne_of_lt hjq),Nat.sub_zero]
by_cases hji : (⟨j,hjq⟩ : Fin q)=i
· subst i
simp only [Function.update_self,ite_true]
· have hne : j≠i.val := fun h => 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<q := by
simpa only [listExchange,List.length_map,List.length_range,List.length_ofFn] using hj
simp only [listExchange,List.getElem_map,List.getElem_range,List.getElem_ofFn,
exchangeEntry,List.getD_eq_getElem?_getD,List.getElem?_ofFn,hjq,dite_true,
Option.getD_some,ite_eq_right (Nat.ne_of_lt hjq),Nat.add_zero]
by_cases hji : (⟨j,hjq⟩ : Fin q)=i
· subst i
simp only [Function.update_self,ite_true]
· have hne : j≠i.val := fun h => 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<K := 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,by omega,h⟩
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_increment (fun j => (corner a j).toNat) i).trans hc.symm)
· have hn : ¬(corner a i).toNat+1<K := by omega
rw [ite_eq_right hn,dite_eq_right h,Option.map_none]
end ContingencyTables.DensePrograms
end OAI