Download data/lean/OAI/Computability/RationalHitting/EncodingBounds.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 14.9 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/RationalHitting/EncodingBounds.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Computability/RationalHitting/EncodingBounds.lean
-
curl -L -o EncodingBounds.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Computability/RationalHitting/EncodingBounds.lean
14.9 kB
| import OAI.Computability.RationalHitting.IntegralLift | |
| namespace OAI | |
| namespace RationalHitting.EncodingBounds | |
| open Matrix | |
| open scoped BigOperators | |
| lemma encodeNat_length (a : β) : (encodeNat a).length = 2*(Nat.digits 2 a).length+1 := by | |
| simp [encodeNat] | |
| omega | |
| lemma encodeNat_le (a L : β) (ha : a < 2^L) : (encodeNat a).length β€ 2*L+1 := by | |
| rw [encodeNat_length] | |
| exact Nat.add_le_add_right (Nat.mul_le_mul_left 2 ((Nat.digits_length_le_iff (by omega) a).mpr ha)) 1 | |
| lemma ratio_num_le (a b : β€) (hb : b β 0) : (Rat.divInt a b).num.natAbs β€ a.natAbs := by | |
| by_cases ha : a = 0 | |
| Β· simp [ha] | |
| Β· exact Int.natAbs_le_of_dvd_ne_zero (Rat.num_dvd a hb) ha | |
| lemma ratio_den_le (a b : β€) (hb : b β 0) : (Rat.divInt a b).den β€ b.natAbs := by | |
| simpa only [Int.natAbs_natCast] using Int.natAbs_le_of_dvd_ne_zero (Rat.den_dvd a b) hb | |
| lemma encodeRat_ratio_le (a b : β€) (hb : b β 0) (L : β) | |
| (ha : a.natAbs < 2^L) (hb' : b.natAbs < 2^L) : | |
| (encodeRat (Rat.divInt a b)).length β€ 4*L+3 := by | |
| have hn := encodeNat_le _ L (lt_of_le_of_lt (ratio_num_le a b hb) ha) | |
| have hd := encodeNat_le _ L (lt_of_le_of_lt (ratio_den_le a b hb) hb') | |
| simp only [encodeRat,List.length_append,List.length_singleton] | |
| omega | |
| lemma determinant_bound {d : β} (A : Matrix (Fin d) (Fin d) β€) (B : β) | |
| (hA : β i j, |A i j| β€ B) : |A.det| β€ (d.factorial : β€)*(B : β€)^d := by | |
| rw [Matrix.det_apply] | |
| apply (Finset.abs_sum_le_sum_abs _ _).trans | |
| calc β Ο : Equiv.Perm (Fin d), |Equiv.Perm.sign Ο β’ β i, A (Ο i) i| β€ | |
| β _Ο : Equiv.Perm (Fin d), (B : β€)^d := by | |
| apply Finset.sum_le_sum | |
| intro Ο _ | |
| rw [Units.smul_def, zsmul_eq_mul, abs_mul] | |
| have hsign : |((Equiv.Perm.sign Ο : β€Λ£) : β€)| = 1 := by | |
| rcases Int.units_eq_one_or (Equiv.Perm.sign Ο) with h | h <;> rw [h] <;> norm_num | |
| change |((Equiv.Perm.sign Ο : β€Λ£) : β€)| * |β i, A (Ο i) i| β€ (B : β€)^d | |
| rw [hsign] | |
| rw [one_mul,Finset.abs_prod] | |
| calc β i, |A (Ο i) i| β€ β _i : Fin d, (B : β€) := | |
| Finset.prod_le_prodβ (fun _ _ => abs_nonneg _) (fun _ _ => hA _ _) | |
| _ = _ := by simp | |
| _ = _ := by simp [Fintype.card_perm] | |
| /-- Cramer's rule used by the generator, without any assumption on arithmetic algorithms. -/ | |
| lemma normalized_entry_ratio {d : β} (A B : Matrix (Fin d) (Fin d) β€) (hA : A.det β 0) | |
| (i j : Fin d) : | |
| ((A.map (Int.castRingHom β))β»ΒΉ * B.map (Int.castRingHom β)) i j = | |
| Rat.divInt ((A.updateCol i (fun k => B k j)).det) A.det := by | |
| let AQ := A.map (Int.castRingHom β) | |
| have hdet : AQ.det β 0 := by | |
| change (A.map (fun z : β€ => (z : β))).det β 0 | |
| rw [β Int.cast_det] | |
| exact_mod_cast hA | |
| have h := Matrix.det_smul_inv_mulVec_eq_cramer AQ ((B.map (Int.castRingHom β)).col j) | |
| (isUnit_iff_ne_zero.mpr hdet) | |
| have hi := congrFun h i | |
| simp only [Pi.smul_apply,smul_eq_mul,Matrix.cramer_apply] at hi | |
| have hcol : AQ.updateCol i ((B.map (Int.castRingHom β)).col j) = | |
| (A.updateCol i (fun k => B k j)).map (Int.castRingHom β) := by | |
| ext a b | |
| simp [AQ,Matrix.updateCol_apply,Matrix.col,Matrix.map_apply] | |
| rw [hcol] at hi | |
| change AQ.det * (AQβ»ΒΉ *α΅₯ (B.map (Int.castRingHom β)).col j) i = | |
| ((A.updateCol i (fun k => B k j)).map (fun z : β€ => (z : β))).det at hi | |
| rw [β Int.cast_det] at hi | |
| change AQ.det * (AQβ»ΒΉ * B.map (Int.castRingHom β)) i j = _ at hi | |
| rw [Rat.divInt_eq_div] | |
| apply (eq_div_iff (show (A.det : β) β 0 from by exact_mod_cast hA)).mpr | |
| rw [mul_comm] | |
| have he : AQ.det = (A.det : β) := (Int.cast_det A).symm | |
| rw [he] at hi | |
| exact hi | |
| lemma nat_le_two_pow (n : β) : n β€ 2^n := by | |
| induction n with | |
| | zero => norm_num | |
| | succ n ih => | |
| rw [pow_succ] | |
| have : 0 < 2^n := by positivity | |
| omega | |
| lemma determinant_pow_bound {d b : β} (A : Matrix (Fin d) (Fin d) β€) | |
| (hA : β i j, |A i j| β€ (2 : β€)^b) : |A.det| < (2 : β€)^(d*d+d*b+1) := by | |
| have hb := determinant_bound A (2^b) (by simpa using hA) | |
| have hf : d.factorial β€ (2^d)^d := | |
| (Nat.factorial_le_pow d).trans (Nat.pow_le_pow_left ((nat_le_two_pow d)) d) | |
| have hfc : (d.factorial : β€) β€ ((2 : β€)^d)^d := by exact_mod_cast hf | |
| have hn : |A.det| β€ (2 : β€)^(d*d+d*b) := by | |
| calc |A.det| β€ (d.factorial : β€)*((2 : β€)^b)^d := by simpa using hb | |
| _ β€ ((2 : β€)^d)^d*((2 : β€)^b)^d := mul_le_mul_of_nonneg_right hfc (by positivity) | |
| _ = _ := by rw [β pow_mul,β pow_mul,β pow_add]; congr 1; ring | |
| apply hn.trans_lt | |
| rw [pow_succ] | |
| have : (0 : β€) < 2^(d*d+d*b) := by positivity | |
| omega | |
| lemma normalization_bit_bound {d b : β} (A B : Matrix (Fin d) (Fin d) β€) (hA : A.det β 0) | |
| (hAb : β i j, |A i j| β€ (2 : β€)^b) (hBb : β i j, |B i j| β€ (2 : β€)^b) (i j : Fin d) : | |
| (encodeRat (((A.map (Int.castRingHom β))β»ΒΉ * B.map (Int.castRingHom β)) i j)).length β€ | |
| 4*(d*d+d*b+1)+3 := by | |
| rw [normalized_entry_ratio A B hA i j] | |
| apply encodeRat_ratio_le _ _ hA | |
| Β· have h := determinant_pow_bound (A.updateCol i (fun k => B k j)) (b := b) (by | |
| intro a c | |
| by_cases hc : c = i | |
| Β· simpa [Matrix.updateCol_apply,hc] using hBb a j | |
| Β· simpa [Matrix.updateCol_apply,hc] using hAb a c) | |
| rw [β Int.natCast_natAbs] at h | |
| exact_mod_cast h | |
| Β· have h := determinant_pow_bound A hAb | |
| rw [β Int.natCast_natAbs] at h | |
| exact_mod_cast h | |
| end RationalHitting.EncodingBounds | |
| namespace RationalHitting.EncodingBounds | |
| lemma flatMap_length_le {Ξ± Ξ² : Type*} (l : List Ξ±) (f : Ξ± β List Ξ²) (B : β) | |
| (h : β a β l, (f a).length β€ B) : (l.flatMap f).length β€ l.length*B := by | |
| induction l with | |
| | nil => simp | |
| | cons a l ih => | |
| simp only [List.flatMap_cons,List.length_append,List.length_cons] | |
| have ha := h a (by simp) | |
| have hl := ih (fun b hb => h b (by simp [hb])) | |
| simp only [Nat.add_mul,Nat.one_mul] | |
| omega | |
| lemma tuple_length_le {n d B : β} (X : Tuple n d) | |
| (h : β a i j, (encodeRat (X a i j)).length β€ B) : | |
| (encodeTuple X).length β€ n*d*d*B := by | |
| unfold encodeTuple | |
| calc _ β€ (List.finRange n).length*(d*(d*B)) := by | |
| apply flatMap_length_le | |
| intro a _ | |
| calc _ β€ (List.finRange d).length*(d*B) := by | |
| apply flatMap_length_le | |
| intro i _ | |
| calc _ β€ (List.finRange d).length*B := flatMap_length_le _ _ B (fun j _ => h a i j) | |
| _ = _ := by simp | |
| _ = _ := by simp | |
| _ = _ := by simp [Nat.mul_assoc] | |
| lemma encodeNat_linear (a : β) : (encodeNat a).length β€ 2*(a+1)+1 := by | |
| apply encodeNat_le | |
| have h := nat_le_two_pow a | |
| have hp : 0 < 2^a := by positivity | |
| rw [pow_succ] | |
| omega | |
| lemma output_length_le {n B : β} (H : Output n) | |
| (hb : β X β H.tuples, β a i j, (encodeRat (X a i j)).length β€ B) : | |
| (encodeOutput H).length β€ 2*(H.dimension+1)+1 + 2*(H.tuples.length+1)+1 + | |
| H.tuples.length*n*H.dimension*H.dimension*B := by | |
| have hd := encodeNat_linear H.dimension | |
| have hc := encodeNat_linear H.tuples.length | |
| have ht := flatMap_length_le H.tuples encodeTuple (n*H.dimension*H.dimension*B) | |
| (fun X hX => tuple_length_le X (hb X hX)) | |
| simp only [encodeOutput,List.length_append] at * | |
| rw [β Nat.mul_assoc,β Nat.mul_assoc,β Nat.mul_assoc] at ht | |
| omega | |
| end RationalHitting.EncodingBounds | |
| namespace RationalHitting.PolynomialBounds | |
| /-- Coefficient/exponent bookkeeping, with no symbolic expansion of large powers. -/ | |
| lemma mul_monomials (a b m i j : β) : (a*m^i)*(b*m^j) = (a*b)*m^(i+j) := by | |
| rw [pow_add] | |
| ac_rfl | |
| lemma lift_power (c m i j : β) (hm : 1 β€ m) (hij : i β€ j) : c*m^i β€ c*m^j := | |
| Nat.mul_le_mul_left c (Nat.pow_le_pow_right hm hij) | |
| lemma one_le_pow (m k : β) (hm : 1 β€ m) : 1 β€ m^k := | |
| Nat.one_le_pow k m hm | |
| /-- Exponent bounding all denominator-cleared matrix entries by a power of 2. -/ | |
| def logarithmic (n s : β) : β := | |
| 4*Parameters.M n s + 4*Parameters.M n s*(n+1) + 3*Parameters.M n s*Parameters.H n s | |
| def logConstant : β := 64+64+3*16*24577 | |
| theorem logarithmic_bound (n s : β) (hn : 1 β€ n) : | |
| logarithmic n s β€ logConstant*(n+s+1)^22 := by | |
| let m := n+s+1 | |
| have hm : 1 β€ m := by dsimp [m]; omega | |
| have hM := Parameters.polynomial_M n s | |
| have hH := Parameters.polynomial_H n s hn | |
| have hβ : 4*Parameters.M n s β€ 64*m^22 := by | |
| calc _ β€ 4*(16*m^5) := Nat.mul_le_mul_left 4 hM | |
| _ = 64*m^5 := by rw [β Nat.mul_assoc] | |
| _ β€ _ := lift_power _ _ _ _ hm (by omega) | |
| have hβ : 4*Parameters.M n s*(n+1) β€ 64*m^22 := by | |
| calc _ β€ (64*m^5)*m := by | |
| apply Nat.mul_le_mul | |
| Β· calc _ β€ 4*(16*m^5) := Nat.mul_le_mul_left 4 hM | |
| _ = _ := by rw [β Nat.mul_assoc] | |
| Β· dsimp [m]; omega | |
| _ = 64*m^6 := by rw [show m^6 = m^5*m from pow_succ m 5,Nat.mul_assoc] | |
| _ β€ _ := lift_power _ _ _ _ hm (by omega) | |
| have hβ : 3*Parameters.M n s*Parameters.H n s β€ (3*16*24577)*m^22 := by | |
| calc _ β€ (3*(16*m^5))*(24577*m^17) := Nat.mul_le_mul (Nat.mul_le_mul_left 3 hM) hH | |
| _ = (48*m^5)*(24577*m^17) := by rw [β Nat.mul_assoc 3 16] | |
| _ = _ := mul_monomials 48 24577 m 5 17 | |
| unfold logarithmic logConstant | |
| change _ β€ (64+64+3*16*24577)*m^22 | |
| rw [Nat.add_mul,Nat.add_mul] | |
| exact Nat.add_le_add (Nat.add_le_add hβ hβ) hβ | |
| def bitConstant : β := 4*(256*256+256*logConstant+1)+3 | |
| theorem normalized_bits_bound (m D b : β) (hm : 1 β€ m) | |
| (hD : D β€ 256*m^10) (hb : b β€ logConstant*m^22) : | |
| 4*(D*D+D*b+1)+3 β€ bitConstant*m^32 := by | |
| have hβ : D*D β€ (256*256)*m^32 := by | |
| calc _ β€ (256*m^10)*(256*m^10) := Nat.mul_le_mul hD hD | |
| _ = (256*256)*m^20 := mul_monomials .. | |
| _ β€ _ := lift_power _ _ _ _ hm (by omega) | |
| have hβ : D*b β€ (256*logConstant)*m^32 := by | |
| calc _ β€ (256*m^10)*(logConstant*m^22) := Nat.mul_le_mul hD hb | |
| _ = _ := mul_monomials .. | |
| have hp := one_le_pow m 32 hm | |
| unfold bitConstant | |
| rw [Nat.add_mul,Nat.mul_assoc,Nat.add_mul,Nat.add_mul,one_mul] | |
| omega | |
| def outputConstant : β := (2*256+3) + (2*24577^3+3) + 24577^3*256*256*bitConstant | |
| theorem output_bits_bound (n m D L B : β) (hm : 1 β€ m) (hn : n β€ m) | |
| (hD : D β€ 256*m^10) (hL : L β€ 24577^3*m^51) (hB : B β€ bitConstant*m^32) : | |
| 2*(D+1)+1 + 2*(L+1)+1 + L*n*D*D*B β€ outputConstant*m^104 := by | |
| have hp := one_le_pow m 104 hm | |
| have hd : D β€ 256*m^104 := hD.trans (lift_power _ _ _ _ hm (by omega)) | |
| have hl : L β€ 24577^3*m^104 := hL.trans (lift_power _ _ _ _ hm (by omega)) | |
| have hproduct : L*n*D*D*B β€ (24577^3*256*256*bitConstant)*m^104 := by | |
| calc _ β€ (24577^3*m^51)*m*(256*m^10)*(256*m^10)*(bitConstant*m^32) := by | |
| gcongr | |
| _ = _ := by | |
| have hh := mul_monomials (24577^3*256*256) bitConstant m 72 32 | |
| calc _ = ((24577^3*256*256)*m^72)*(bitConstant*m^32) := by | |
| congr 1 | |
| calc _ = ((24577^3*256)*m^62)*(256*m^10) := by | |
| congr 1 | |
| calc _ = (24577^3*m^52)*(256*m^10) := by | |
| congr 1 | |
| rw [show m^52=m^51*m from pow_succ m 51,Nat.mul_assoc] | |
| _ = _ := mul_monomials _ _ _ _ _ | |
| _ = _ := mul_monomials _ _ _ _ _ | |
| _ = _ := hh | |
| unfold outputConstant | |
| rw [Nat.add_mul,Nat.add_mul,Nat.add_mul,Nat.add_mul] | |
| omega | |
| end RationalHitting.PolynomialBounds | |
| namespace RationalHitting.Generator | |
| open Matrix EncodingBounds | |
| open scoped BigOperators | |
| noncomputable section | |
| lemma U_power_bound (n N e H : β) (x : Fin 3 β β) (hH : 1 β€ H) (hx : β l, x l β€ H) | |
| (a : Fin (n+1)) (i j : Fin (D e)) : | |
| |U n N e x a i j| β€ (2 : β€)^(4*CyclotomicRing.dimension e + | |
| 4*CyclotomicRing.dimension e*(n+1)+3*CyclotomicRing.dimension e*H) := by | |
| let M := CyclotomicRing.dimension e | |
| have hM : (M : β€) β€ 2^M := by exact_mod_cast nat_le_two_pow M | |
| have hn : (2*(n+1) : β€) β€ 2^(2*(n+1)) := by exact_mod_cast nat_le_two_pow (2*(n+1)) | |
| have hh : (H : β€) β€ 2^H := by exact_mod_cast nat_le_two_pow H | |
| calc _ β€ (M : β€)^4*(2*(n+1) : β€)^(2*M)*(H : β€)^(3*M) := U_bound n N e H x hH hx a i j | |
| _ β€ ((2 : β€)^M)^4*((2 : β€)^(2*(n+1)))^(2*M)*((2 : β€)^H)^(3*M) := by | |
| gcongr | |
| _ = _ := by | |
| simp only [β pow_mul,β pow_add] | |
| congr 1 | |
| dsimp [M] | |
| ring | |
| lemma tupleAt_bit_bound (n N e H : β) (x : Fin 3 β β) (hH : 1 β€ H) (hx : β l, x l β€ H) | |
| (hz : (Z n N e x 0).det β 0) (a : Fin n) (i j : Fin (D e)) : | |
| (encodeRat (tupleAt n N e x a i j)).length β€ | |
| 4*(D e*D e+D e*(4*CyclotomicRing.dimension e + | |
| 4*CyclotomicRing.dimension e*(n+1)+3*CyclotomicRing.dimension e*H)+1)+3 := by | |
| rw [tupleAt_integer n N e x hz a] | |
| exact normalization_bit_bound _ _ (U_det_ne_zero n N e x hz) | |
| (U_power_bound n N e H x hH hx 0) (U_power_bound n N e H x hH hx a.succ) i j | |
| lemma output_entry_bit_bound (n N e H : β) (hH : 1 β€ H) | |
| (X : Tuple n (D e)) (hX : X β (output n N e H).tuples) | |
| (a : Fin n) (i j : Fin (D e)) : | |
| (encodeRat (X a i j)).length β€ | |
| 4*(D e*D e+D e*(4*CyclotomicRing.dimension e + | |
| 4*CyclotomicRing.dimension e*(n+1)+3*CyclotomicRing.dimension e*H)+1)+3 := by | |
| obtain β¨x,hx,heβ© := List.mem_filterMap.mp hX | |
| by_cases hz : (Z n N e (fun l => (x l).val+1) 0).det = 0 | |
| Β· simp [atPoint,hz] at he | |
| Β· simp only [atPoint,ite_eq_right hz,Option.some.injEq] at he | |
| rw [β he] | |
| exact tupleAt_bit_bound n N e H _ hH (fun l => by omega) hz a i j | |
| lemma list_output_bound (n s : β) (hn : 1 β€ n) : | |
| (encodeOutput (list n s)).length β€ PolynomialBounds.outputConstant*(n+s+1)^104 := by | |
| have hH : 1 β€ Parameters.H n s := by unfold Parameters.H; omega | |
| have hb : β X β (list n s).tuples, β a i j, | |
| (encodeRat (X a i j)).length β€ PolynomialBounds.bitConstant*(n+s+1)^32 := by | |
| intro X hX a i j | |
| apply (output_entry_bit_bound n (Parameters.N n s) (Parameters.exponent n s) | |
| (Parameters.H n s) hH X hX a i j).trans | |
| apply PolynomialBounds.normalized_bits_bound (hm := by omega) | |
| Β· exact list_dimension_bound n s | |
| Β· exact PolynomialBounds.logarithmic_bound n s hn | |
| apply (output_length_le (list n s) hb).trans | |
| apply PolynomialBounds.output_bits_bound (hm := by omega) (hn := by omega) | |
| Β· exact list_dimension_bound n s | |
| Β· exact list_length_bound n s hn | |
| Β· exact Nat.le_refl _ | |
| end | |
| end RationalHitting.Generator | |
| /-! A staged division-free determinant computation for integer matrices. | |
| Explicit dense tables prevent recursive matrix-entry recomputation. -/ | |
| end OAI | |