Download data/lean/OAI/Probability/Contingency/DirectCases.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 21.8 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/Contingency/DirectCases.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Probability/Contingency/DirectCases.lean
-
curl -L -o DirectCases.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Probability/Contingency/DirectCases.lean
21.8 kB
| import Mathlib | |
| import OAI.Probability.Contingency.GreedyTable | |
| namespace OAI | |
| /-! Direct Cases. -/ | |
| noncomputable section | |
| open scoped BigOperators | |
| open scoped BigOperators | |
| namespace Contingency | |
| noncomputable section | |
| variable {m n : β} (r : Fin m β β) (c : Fin n β β) | |
| def DirectCase (_c : Fin n β β) : Prop := m=0 β¨ n=0 β¨ (β i,r i)=0 β¨ m=1 β¨ n=1 | |
| theorem table_subsingleton_of_direct (h : DirectCase r c) : Subsingleton (Tables r c) := by | |
| constructor | |
| intro X Y | |
| apply Subtype.ext | |
| funext i j | |
| rcases h with hm | hn | hN | hm | hn | |
| Β· exact Fin.elim0 (hm βΈ i) | |
| Β· exact Fin.elim0 (hn βΈ j) | |
| Β· have hi : r i=0 := by | |
| have hh := Finset.single_le_sum (fun i _ => Nat.zero_le (r i)) (Finset.mem_univ i) | |
| omega | |
| have hx := Finset.single_le_sum (fun j _ => Nat.zero_le (X.val i j)) (Finset.mem_univ j) | |
| have hy := Finset.single_le_sum (fun j _ => Nat.zero_le (Y.val i j)) (Finset.mem_univ j) | |
| rw [X.property.1 i,hi] at hx | |
| rw [Y.property.1 i,hi] at hy | |
| omega | |
| Β· subst m | |
| have hx := X.property.2 j | |
| have hy := Y.property.2 j | |
| simp only [Fin.sum_univ_one] at hx hy | |
| have hi : i=0 := Fin.eq_zero i | |
| simp only [hi,hx,hy] | |
| Β· subst n | |
| have hx := X.property.1 i | |
| have hy := Y.property.1 i | |
| simp only [Fin.sum_univ_one] at hx hy | |
| have hj : j=0 := Fin.eq_zero j | |
| simp only [hj,hx,hy] | |
| namespace Law | |
| variable {Ξ± : Type*} [Fintype Ξ±] [Nonempty Ξ±] [Subsingleton Ξ±] | |
| theorem uniform_subsingleton (x : Ξ±) : uniform Ξ±=pure x := by | |
| classical | |
| apply Law.ext | |
| funext a | |
| have hcard : Fintype.card Ξ±=1 := by rw [β Nat.card_eq_fintype_card]; exact Nat.card_unique | |
| simp [uniform,pure,hcard,Subsingleton.elim a x] | |
| end Law | |
| local instance directCasesTablesFintype : Fintype (Tables r c) := Fintype.ofFinite _ | |
| def arbitraryDraw (hrc : (β i,r i)=β j,c j) (k : β) : DyadicLaw (Tables r c) := by | |
| classical | |
| by_cases h : DirectCase r c | |
| Β· exact DyadicLaw.pure (greedyTable r c hrc) 0 | |
| Β· have hm : 0 < m := by by_contra hh; exact h (Or.inl (by omega)) | |
| have hn : 0 < n := by by_contra hh; exact h (Or.inr (Or.inl (by omega))) | |
| exact PaddedGraph.canonicalDraw r c hm hn hrc k | |
| theorem arbitraryDraw_bits (hrc : (β i,r i)=β j,c j) (k : β) : | |
| (arbitraryDraw r c hrc k).bitsβ€Schedule.R (Parameters.dim m n) (Parameters.bits m n (β i,r i)) k := by | |
| classical | |
| unfold arbitraryDraw | |
| split_ifs | |
| Β· exact Nat.zero_le _ | |
| Β· exact (PaddedGraph.canonicalDraw_bits _ _ _ _ _ _).le | |
| theorem arbitraryDraw_tv (hrc : (β i,r i)=β j,c j) (k : β) (hk : 1β€k) : | |
| letI := tables_nonempty r c hrc | |
| (arbitraryDraw r c hrc k).toLaw.tv (Law.uniform (Tables r c))β€(2:β)^(-(k:β€)) := by | |
| classical | |
| let := tables_nonempty r c hrc | |
| unfold arbitraryDraw | |
| split_ifs with h | |
| Β· have huni : Law.uniform (Tables r c)=Law.pure (greedyTable r c hrc) := by | |
| let := table_subsingleton_of_direct r c h | |
| exact Law.uniform_subsingleton _ | |
| rw [DyadicLaw.toLaw_pure,huni,Law.tv_self] | |
| positivity | |
| Β· exact PaddedGraph.canonicalDraw_tv _ _ _ _ _ _ hk | |
| end | |
| end Contingency | |
| open scoped BigOperators | |
| namespace Contingency.IntegerTabulation | |
| def step {s : β} (A : Fin s β Fin s β β) (v : Fin s β β) (j : Fin s) : β := | |
| β i,v i*A i j | |
| def propagate {s : β} (A : Fin s β Fin s β β) (v : Fin s β β) : β β (Fin s β β) | |
| | 0 => v | |
| | t+1 => step A (propagate A v t) | |
| def propagationOps (s : β) : β β β | |
| | 0 => 0 | |
| | t+1 => propagationOps s t+2*s*s | |
| theorem propagationOps_eq (s t : β) : propagationOps s t=2*t*s^2 := by | |
| induction t with | |
| | zero => simp [propagationOps] | |
| | succ t ih => rw [propagationOps,ih]; ring | |
| theorem step_total {s : β} (A : Fin s β Fin s β β) (v : Fin s β β) (Q : β) | |
| (hA : β i,β j,A i j=2^Q) : (β j,step A v j)=(β i,v i)*2^Q := by | |
| unfold step | |
| rw [Finset.sum_comm] | |
| simp_rw [β Finset.mul_sum,hA] | |
| rw [Finset.sum_mul] | |
| theorem propagate_total {s : β} (A : Fin s β Fin s β β) (v : Fin s β β) (Q R : β) | |
| (hA : β i,β j,A i j=2^Q) (hv : β i,v i=2^R) (t : β) : | |
| (β i,propagate A v t i)=2^(R+t*Q) := by | |
| induction t with | |
| | zero => simpa [propagate] using hv | |
| | succ t ih => rw [propagate,step_total _ _ Q hA,ih,β pow_add]; congr 1; ring | |
| theorem propagate_entry_bound {s : β} (A : Fin s β Fin s β β) (v : Fin s β β) (Q R : β) | |
| (hA : β i,β j,A i j=2^Q) (hv : β i,v i=2^R) (t : β) (i : Fin s) : | |
| propagate A v t iβ€2^(R+t*Q) := by | |
| rw [β propagate_total A v Q R hA hv t] | |
| exact Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ i) | |
| theorem partial_step_bound {s : β} (A : Fin s β Fin s β β) (v : Fin s β β) (Q R : β) | |
| (hA : β i,β j,A i j=2^Q) (hv : β i,v i=2^R) (t : β) (j : Fin s) (I : Finset (Fin s)) : | |
| (β iβI,propagate A v t i*A i j)β€2^(R+(t+1)*Q) := by | |
| apply (Finset.sum_le_sum_of_subset_of_nonneg (Finset.subset_univ I) (fun _ _ _ => Nat.zero_le _)).trans | |
| exact propagate_entry_bound A v Q R hA hv (t+1) j | |
| theorem propagate_walk {s : β} (q : Fin s β DyadicLaw (Fin s)) (Q : β) | |
| (hq : β i,(q i).bits=Q) (iβ : Fin s) (t : β) : | |
| propagate (fun i j => (q i).count j) (fun i => if i=iβ then 1 else 0) t= | |
| (DyadicLaw.walk q Q hq iβ t).count := by | |
| induction t with | |
| | zero => rfl | |
| | succ t ih => | |
| funext j | |
| simp only [propagate,step,DyadicLaw.walk,DyadicLaw.bind,ih] | |
| def stoppedVector {s : β} (R Ο : β) (a : Fin s β β) (zβ : Fin s) : β β (Fin s β β) | |
| | 0 => fun z => if z=zβ then 1 else 0 | |
| | j+1 => fun z => a z*2^(R*j)+Ο*stoppedVector R Ο a zβ j z | |
| theorem stoppedVector_eq {s : β} (R Ο : β) (a : Fin s β β) (zβ : Fin s) (J : β) (z : Fin s) : | |
| stoppedVector R Ο a zβ J z=stoppedNumerator R Ο a zβ z J := by | |
| induction J with | |
| | zero => rfl | |
| | succ J ih => simp only [stoppedVector,stoppedNumerator,ih] | |
| theorem stoppedVector_total {s : β} (R Ο : β) (a : Fin s β β) (zβ : Fin s) | |
| (ht : (β z,a z)+Ο=2^R) (J : β) : (β z,stoppedVector R Ο a zβ J z)=2^(R*J) := by | |
| simp_rw [stoppedVector_eq] | |
| exact stoppedNumerator_total R Ο a zβ ht J | |
| theorem stoppedVector_bound {s : β} (R Ο : β) (a : Fin s β β) (zβ : Fin s) | |
| (ht : (β z,a z)+Ο=2^R) (J : β) (z : Fin s) : stoppedVector R Ο a zβ J zβ€2^(R*J) := by | |
| rw [β stoppedVector_total R Ο a zβ ht J] | |
| exact Finset.single_le_sum (fun _ _ => Nat.zero_le _) (Finset.mem_univ z) | |
| end Contingency.IntegerTabulation | |
| open scoped BigOperators | |
| namespace Contingency.PaddedGraph | |
| noncomputable section | |
| variable {m n : β} (U L : β) (r : Fin m β β) (c : Fin n β β) | |
| def graphArray (X : GraphState U r c) : BoolΓCell m n β Fin (U+1) := | |
| fun w => β¨(if w.1 then (graphUnderlying U r c X).x w.2 else (graphUnderlying U r c X).q w.2).toNat,by | |
| have h := (graph_feasible U r c X).1 w.2 | |
| cases w.1 <;> simp only [Bool.false_eq_true,ite_false,ite_true] <;> omegaβ© | |
| theorem graphArray_injective : Function.Injective (graphArray U r c) := by | |
| intro X Y h | |
| apply graphUnderlying_injective U r c | |
| have hx : (graphUnderlying U r c X).x=(graphUnderlying U r c Y).x := by | |
| funext w | |
| have hh := congrArg Fin.val (congrFun h (true,w)) | |
| dsimp [graphArray] at hh | |
| have hX := ((graph_feasible U r c X).1 w).1 | |
| have hY := ((graph_feasible U r c Y).1 w).1 | |
| omega | |
| have hq : (graphUnderlying U r c X).q=(graphUnderlying U r c Y).q := by | |
| funext w | |
| have hh := congrArg Fin.val (congrFun h (false,w)) | |
| dsimp [graphArray] at hh | |
| have hX := ((graph_feasible U r c X).1 w).2.2.1 | |
| have hY := ((graph_feasible U r c Y).1 w).2.2.1 | |
| omega | |
| exact congrArgβ State.mk hx hq | |
| theorem graph_card_array : Nat.card (GraphState U r c)β€(U+1)^(2*(m*n)) := by | |
| rw [Nat.card_eq_fintype_card] | |
| simpa [Cell] using Fintype.card_le_of_injective (graphArray U r c) (graphArray_injective U r c) | |
| theorem graph_card_dyadic (d b C : β) (hmn : m*nβ€d) (hU : Uβ€C) (hCb : C+1β€2^b) : | |
| Nat.card (GraphState U r c)β€2^(2*d*b) := by | |
| calc | |
| _ β€ (U+1)^(2*(m*n)) := graph_card_array U r c | |
| _ β€ (C+1)^(2*(m*n)) := Nat.pow_le_pow_left (by omega) _ | |
| _ β€ (C+1)^(2*d) := Nat.pow_le_pow_right (by omega) (by omega) | |
| _ β€ (2^b)^(2*d) := Nat.pow_le_pow_left hCb _ | |
| _ = _ := by rw [β pow_mul]; congr 1; ring | |
| def completionArray (C : β) (hC : β i,paddedRow U L r c iβ€C) | |
| (X : GraphState U r c) (A : Completion U L r c (graphUnderlying U r c X)) : | |
| Cell m n β Fin (C+1) := fun w => β¨(A.val w).toNat,by | |
| have hlo := A.property.1 w | |
| have hhi := (completion_cell_le U L r c _ (graph_feasible U r c X) A w).trans (hC w.1) | |
| omegaβ© | |
| theorem completion_card_cap (C : β) (hC : β i,paddedRow U L r c iβ€C) (X : GraphState U r c) : | |
| Nat.card (Completion U L r c (graphUnderlying U r c X))β€(C+1)^(m*n) := by | |
| let : Fintype (Completion U L r c (graphUnderlying U r c X)) := Fintype.ofFinite _ | |
| have hinj : Function.Injective (completionArray U L r c C hC X) := by | |
| intro A B h | |
| apply Subtype.ext | |
| funext w | |
| have hh := congrArg Fin.val (congrFun h w) | |
| dsimp [completionArray] at hh | |
| have ha := A.property.1 w | |
| have hb := B.property.1 w | |
| omega | |
| rw [Nat.card_eq_fintype_card] | |
| simpa [Cell] using Fintype.card_le_of_injective _ hinj | |
| theorem completion_card_dyadic (d b C : β) (hmn : m*nβ€d) | |
| (hC : β i,paddedRow U L r c iβ€C) (hCb : C+1β€2^b) (X : GraphState U r c) : | |
| Nat.card (Completion U L r c (graphUnderlying U r c X))β€2^(d*b) := by | |
| calc | |
| _ β€ (C+1)^(m*n) := completion_card_cap U L r c C hC X | |
| _ β€ (C+1)^d := Nat.pow_le_pow_right (by omega) hmn | |
| _ β€ (2^b)^d := Nat.pow_le_pow_left hCb _ | |
| _ = _ := by rw [β pow_mul]; congr 1; ring | |
| end | |
| end Contingency.PaddedGraph | |
| namespace Contingency.BinaryArithmetic | |
| open PosNum | |
| def succCost : PosNum β β | |
| | .one => 1 | |
| | .bit0 _ => 1 | |
| | .bit1 a => succCost a+1 | |
| theorem succCost_le (a : PosNum) : succCost aβ€a.natSize := by | |
| induction a <;> simp_all [succCost,PosNum.natSize] | |
| theorem succ_size_le (a : PosNum) : a.succ.natSizeβ€a.natSize+1 := by | |
| induction a <;> simp_all [PosNum.succ,PosNum.natSize] | |
| theorem add_size_le (a b : PosNum) : (a+b).natSizeβ€a.natSize+b.natSize := by | |
| induction a generalizing b with | |
| | one => | |
| change (PosNum.succ b).natSizeβ€1+b.natSize | |
| simpa [Nat.add_comm] using succ_size_le b | |
| | bit0 a ih => | |
| cases b with | |
| | one => exact (succ_size_le a.bit0) | |
| | bit0 b => change (a+b).natSize+1β€(a.natSize+1)+(b.natSize+1); have := ih b; omega | |
| | bit1 b => change (a+b).natSize+1β€(a.natSize+1)+(b.natSize+1); have := ih b; omega | |
| | bit1 a ih => | |
| cases b with | |
| | one => exact (succ_size_le a.bit1) | |
| | bit0 b => change (a+b).natSize+1β€(a.natSize+1)+(b.natSize+1); have := ih b; omega | |
| | bit1 b => | |
| change (a+b).succ.natSize+1β€(a.natSize+1)+(b.natSize+1) | |
| have := succ_size_le (a+b) | |
| have := ih b | |
| omega | |
| def addCost : PosNum β PosNum β β | |
| | .one,b => succCost b+1 | |
| | a,.one => succCost a+1 | |
| | .bit0 a,.bit0 b => addCost a b+1 | |
| | .bit1 a,.bit1 b => addCost a b+succCost (a+b)+2 | |
| | .bit0 a,.bit1 b => addCost a b+1 | |
| | .bit1 a,.bit0 b => addCost a b+1 | |
| theorem addCost_le (a b : PosNum) : addCost a bβ€(a.natSize+b.natSize+1)^2 := by | |
| induction a generalizing b with | |
| | one => | |
| have := succCost_le b | |
| simp only [addCost,PosNum.natSize] | |
| nlinarith | |
| | bit0 a ih => | |
| cases b with | |
| | one => have := succCost_le a.bit0; simp only [addCost,PosNum.natSize,Nat.succ_eq_add_one] at *; nlinarith | |
| | bit0 b => have := ih b; simp only [addCost,PosNum.natSize,Nat.succ_eq_add_one]; nlinarith [Nat.zero_le a.natSize,Nat.zero_le b.natSize] | |
| | bit1 b => have := ih b; simp only [addCost,PosNum.natSize,Nat.succ_eq_add_one]; nlinarith [Nat.zero_le a.natSize,Nat.zero_le b.natSize] | |
| | bit1 a ih => | |
| cases b with | |
| | one => have := succCost_le a.bit1; simp only [addCost,PosNum.natSize,Nat.succ_eq_add_one] at *; nlinarith | |
| | bit0 b => have := ih b; simp only [addCost,PosNum.natSize,Nat.succ_eq_add_one]; nlinarith [Nat.zero_le a.natSize,Nat.zero_le b.natSize] | |
| | bit1 b => | |
| have h := ih b | |
| have hs := (succCost_le (a+b)).trans (add_size_le a b) | |
| simp only [addCost,PosNum.natSize,Nat.succ_eq_add_one] | |
| nlinarith | |
| def mulCost (a : PosNum) : PosNum β β | |
| | .one => a.natSize+1 | |
| | .bit0 b => mulCost a b+1 | |
| | .bit1 b => mulCost a b+addCost ((a*b).bit0) a+2 | |
| theorem mul_size_le (a b : PosNum) : (a*b).natSizeβ€a.natSize+b.natSize := by | |
| have ha : (a:β)<2^a.natSize := by rw [PosNum.natSize_to_nat]; exact Nat.size_le.mp le_rfl | |
| have hb : (b:β)<2^b.natSize := by rw [PosNum.natSize_to_nat]; exact Nat.size_le.mp le_rfl | |
| rw [PosNum.natSize_to_nat,PosNum.mul_to_nat,Nat.size_le,pow_add] | |
| exact Nat.mul_lt_mul_of_lt_of_le ha hb.le (by positivity) | |
| theorem mulCost_le (a b : PosNum) : | |
| mulCost a bβ€4*(a.natSize+b.natSize+1)^3 := by | |
| induction b with | |
| | one => simp only [mulCost,PosNum.natSize]; nlinarith [Nat.zero_le (a.natSize^3)] | |
| | bit0 b ih => simp only [mulCost,PosNum.natSize,Nat.succ_eq_add_one]; nlinarith [Nat.zero_le a.natSize,Nat.zero_le b.natSize,Nat.zero_le (a.natSize*b.natSize)] | |
| | bit1 b ih => | |
| have ha := addCost_le ((a*b).bit0) a | |
| have hs := mul_size_le a b | |
| simp only [PosNum.natSize] at ha | |
| have hh : ((a*b).natSize+1+a.natSize+1)^2β€(2*(a.natSize+b.natSize+1))^2 := by | |
| apply Nat.pow_le_pow_left | |
| omega | |
| have ha' := ha.trans hh | |
| simp only [mulCost,PosNum.natSize,Nat.succ_eq_add_one] | |
| nlinarith | |
| theorem add_certificate (a b : PosNum) : | |
| ((a+b:PosNum):β)=(a:β)+(b:β) β§ addCost a bβ€(a.natSize+b.natSize+1)^2 := | |
| β¨PosNum.add_to_nat a b,addCost_le a bβ© | |
| theorem mul_certificate (a b : PosNum) : | |
| ((a*b:PosNum):β)=(a:β)*(b:β) β§ mulCost a bβ€4*(a.natSize+b.natSize+1)^3 := | |
| β¨PosNum.mul_to_nat a b,mulCost_le a bβ© | |
| end Contingency.BinaryArithmetic | |
| open scoped BigOperators | |
| namespace Contingency.BinaryArithmetic | |
| def numAddCost : Num β Num β β | |
| | .zero, b => b.natSize+1 | |
| | a, .zero => a.natSize+1 | |
| | .pos a, .pos b => addCost a b+1 | |
| def numMulCost : Num β Num β β | |
| | .zero, _ => 1 | |
| | _, .zero => 1 | |
| | .pos a, .pos b => mulCost a b+1 | |
| theorem numAddCost_le (a b : Num) : numAddCost a bβ€(a.natSize+b.natSize+2)^2 := by | |
| cases a with | |
| | zero => simp only [numAddCost,Num.natSize,zero_add]; nlinarith [Nat.zero_le b.natSize] | |
| | pos a => | |
| cases b with | |
| | zero => simp only [numAddCost,Num.natSize,add_zero]; nlinarith [Nat.zero_le a.natSize] | |
| | pos b => | |
| have h := addCost_le a b | |
| simp only [numAddCost,Num.natSize] | |
| nlinarith [Nat.zero_le a.natSize,Nat.zero_le b.natSize] | |
| theorem numMulCost_le (a b : Num) : numMulCost a bβ€5*(a.natSize+b.natSize+2)^3 := by | |
| cases a with | |
| | zero => | |
| have h := Nat.one_le_pow 3 (b.natSize+2) (by omega) | |
| have hh : 1β€5*(b.natSize+2)^3 := by omega | |
| simpa only [numMulCost,Num.natSize,zero_add] using hh | |
| | pos a => | |
| cases b with | |
| | zero => | |
| change 1β€5*(a.natSize+2)^3 | |
| have h := Nat.one_le_pow 3 (a.natSize+2) (by omega) | |
| omega | |
| | pos b => | |
| have h := mulCost_le a b | |
| simp only [numMulCost,Num.natSize] | |
| nlinarith [Nat.zero_le ((a.natSize+b.natSize)^2),Nat.zero_le ((a.natSize+b.natSize)^3),Nat.zero_le a.natSize,Nat.zero_le b.natSize] | |
| theorem numSize_le_of_lt (a : Num) (L : β) (h : (a:β)<2^L) : a.natSizeβ€L := by | |
| cases a with | |
| | zero => exact Nat.zero_le _ | |
| | pos a => | |
| change a.natSizeβ€L | |
| rw [PosNum.natSize_to_nat,Nat.size_le] | |
| exact h | |
| def binaryDot : List (NumΓNum) β NumΓβ | |
| | [] => (.zero,1) | |
| | (a,b)::xs => | |
| let t := binaryDot xs | |
| let p := a*b | |
| (p+t.1,t.2+numMulCost a b+numAddCost p t.1+3) | |
| def dotValue (xs : List (NumΓNum)) : β := (xs.map (fun ab => (ab.1:β)*(ab.2:β))).sum | |
| theorem binaryDot_value (xs : List (NumΓNum)) : ((binaryDot xs).1:β)=dotValue xs := by | |
| induction xs with | |
| | nil => rfl | |
| | cons ab xs ih => | |
| rcases ab with β¨a,bβ© | |
| simp only [binaryDot,Num.add_to_nat,Num.mul_to_nat,dotValue,List.map_cons,List.sum_cons] at * | |
| rw [ih] | |
| theorem binaryDot_cost (xs : List (NumΓNum)) (L : β) | |
| (hx : β abβxs,ab.1.natSizeβ€L β§ ab.2.natSizeβ€L) | |
| (hv : dotValue xs<2^L) : | |
| (binaryDot xs).2β€1+xs.length*(10*(2*L+2)^3) := by | |
| induction xs with | |
| | nil => simp [binaryDot] | |
| | cons ab xs ih => | |
| rcases ab with β¨a,bβ© | |
| have hv' : (a:β)*(b:β)+dotValue xs<2^L := hv | |
| have hp : (a:β)*(b:β)<2^L := (Nat.le_add_right _ _).trans_lt hv' | |
| have ht : dotValue xs<2^L := (Nat.le_add_left _ _).trans_lt hv' | |
| have hab := hx (a,b) (by simp) | |
| dsimp only at hab | |
| have hc := ih (fun ab h => hx ab (by simp [h])) ht | |
| have hs := numSize_le_of_lt (a*b) L (by simpa only [Num.mul_to_nat] using hp) | |
| have hst := numSize_le_of_lt (binaryDot xs).1 L (by simpa only [binaryDot_value] using ht) | |
| have hm := (numMulCost_le a b).trans (Nat.mul_le_mul_left 5 (Nat.pow_le_pow_left (by omega : a.natSize+b.natSize+2β€2*L+2) 3)) | |
| have ha := (numAddCost_le (a*b) (binaryDot xs).1).trans | |
| (Nat.pow_le_pow_left (by omega : (a*b).natSize+(binaryDot xs).1.natSize+2β€2*L+2) 2) | |
| have he : (2*L+2)^2β€(2*L+2)^3 := Nat.pow_le_pow_right (by omega) (by omega) | |
| have hu : 1β€(2*L+2)^3 := Nat.one_le_pow _ _ (by omega) | |
| simp only [binaryDot,List.length_cons] | |
| nlinarith only [hc,hm,ha,he,hu] | |
| def binaryColumn {s : β} (A : Fin s β Fin s β Num) (v : Fin s β Num) (j : Fin s) : NumΓβ := | |
| binaryDot (List.ofFn (fun i => (v i,A i j))) | |
| theorem binaryColumn_value {s : β} (A : Fin s β Fin s β Num) (v : Fin s β Num) (j : Fin s) : | |
| ((binaryColumn A v j).1:β)=IntegerTabulation.step (fun i j => (A i j:β)) (fun i => (v i:β)) j := by | |
| rw [binaryColumn,binaryDot_value] | |
| simp [dotValue,IntegerTabulation.step,List.map_ofFn,List.sum_ofFn] | |
| def binaryPropagate {s : β} (A : Fin s β Fin s β Num) (v : Fin s β Num) : β β (Fin s β Num) | |
| | 0 => v | |
| | t+1 => | |
| let previous := binaryPropagate A v t | |
| fun j => (binaryColumn A previous j).1 | |
| theorem binaryPropagate_value {s : β} (A : Fin s β Fin s β Num) (v : Fin s β Num) (t : β) : | |
| (fun i => (binaryPropagate A v t i:β))= | |
| IntegerTabulation.propagate (fun i j => (A i j:β)) (fun i => (v i:β)) t := by | |
| induction t with | |
| | zero => rfl | |
| | succ t ih => | |
| funext j | |
| simp only [binaryPropagate,binaryColumn_value,IntegerTabulation.propagate,ih] | |
| end Contingency.BinaryArithmetic | |
| open scoped BigOperators | |
| namespace Contingency.BinaryArithmetic | |
| variable {s : β} (A : Fin s β Fin s β Num) (v : Fin s β Num) (Q R : β) | |
| (hA : β i,β j,(A i j:β)=2^Q) (hv : β i,(v i:β)=2^R) | |
| theorem denominator_size (x B : β) (hx : xβ€2^B) : x<2^(B+1) := by | |
| exact hx.trans_lt (Nat.pow_lt_pow_right (by norm_num) (Nat.lt_succ_self B)) | |
| include hA hv in | |
| theorem binaryColumn_cost (t : β) (j : Fin s) : | |
| (binaryColumn A (binaryPropagate A v t) j).2β€ | |
| 1+s*(10*(2*(R+(t+1)*Q+1)+2)^3) := by | |
| let L := R+(t+1)*Q+1 | |
| have hval := binaryPropagate_value A v t | |
| have hvec (i : Fin s) : (binaryPropagate A v t i:β)β€2^(R+t*Q) := by | |
| rw [show (binaryPropagate A v t i:β)= | |
| IntegerTabulation.propagate (fun i j => (A i j:β)) (fun i => (v i:β)) t i from congrFun hval i] | |
| exact IntegerTabulation.propagate_entry_bound _ _ Q R hA hv t i | |
| have hentry (i l : Fin s) : (A i l:β)β€2^Q := by | |
| rw [β hA i] | |
| exact Finset.single_le_sum (fun l _ => Nat.zero_le (A i l:β)) (Finset.mem_univ l) | |
| have hi : Qβ€R+(t+1)*Q := by nlinarith only [Nat.zero_le R,Nat.zero_le (t*Q)] | |
| unfold binaryColumn | |
| have hh := binaryDot_cost (List.ofFn (fun i => (binaryPropagate A v t i,A i j))) L | |
| rw [List.length_ofFn] at hh | |
| apply hh | |
| Β· intro ab hab | |
| rcases List.mem_ofFn.mp hab with β¨i,rflβ© | |
| constructor | |
| Β· apply numSize_le_of_lt | |
| change (binaryPropagate A v t i:β)<2^(R+(t+1)*Q+1) | |
| apply denominator_size | |
| exact (hvec i).trans (Nat.pow_le_pow_right (by norm_num) (by gcongr; omega)) | |
| Β· apply numSize_le_of_lt | |
| change (A i j:β)<2^(R+(t+1)*Q+1) | |
| exact denominator_size _ _ ((hentry i j).trans (Nat.pow_le_pow_right (by norm_num) hi)) | |
| Β· change _<2^(R+(t+1)*Q+1) | |
| apply denominator_size | |
| rw [β binaryDot_value] | |
| change ((binaryColumn A (binaryPropagate A v t) j).1:β)β€_ | |
| rw [binaryColumn_value,hval] | |
| exact IntegerTabulation.propagate_entry_bound _ _ Q R hA hv (t+1) j | |
| def binaryPropagationCost : β β β | |
| | 0 => 0 | |
| | t+1 => binaryPropagationCost t+β j,(binaryColumn A (binaryPropagate A v t) j).2 | |
| include hA hv in | |
| theorem binaryPropagationCost_le_aux (T : β) (t : β) (ht : tβ€T) : | |
| binaryPropagationCost A v tβ€t*s*(1+s*(10*(2*(R+T*Q+1)+2)^3)) := by | |
| induction t with | |
| | zero => simp [binaryPropagationCost] | |
| | succ t ih => | |
| have hi := ih (by omega) | |
| have hcol (j : Fin s) := binaryColumn_cost A v Q R hA hv t j | |
| have hb : R+(t+1)*Q+1β€R+T*Q+1 := by gcongr | |
| have hm : (β j,(binaryColumn A (binaryPropagate A v t) j).2)β€ | |
| s*(1+s*(10*(2*(R+T*Q+1)+2)^3)) := by | |
| calc | |
| _ β€ β _ : Fin s,(1+s*(10*(2*(R+T*Q+1)+2)^3)) := by | |
| apply Finset.sum_le_sum | |
| intro j _ | |
| exact (hcol j).trans (by gcongr) | |
| _ = _ := by simp | |
| rw [binaryPropagationCost] | |
| nlinarith only [hi,hm] | |
| include hA hv in | |
| theorem binaryPropagationCost_le (T : β) : | |
| binaryPropagationCost A v Tβ€T*s*(1+s*(10*(2*(R+T*Q+1)+2)^3)) := | |
| binaryPropagationCost_le_aux A v Q R hA hv T T le_rfl | |
| end Contingency.BinaryArithmetic | |
| end | |
| end OAI | |