math / data /lean /OAI /Combinatorics /EditApproximation /Geometry /BandEnvelopeBits.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
5.26 kB
import OAI.Combinatorics.EditApproximation.Geometry.BandNumericalBits
import OAI.Combinatorics.EditApproximation.Geometry.BandEnvelopes
namespace OAI
/-! Encoding lengths of the actual finite maxima and dyadic envelopes.
These estimates include the scales below one and require no good seed. -/
namespace EditApproximation
theorem positive_rational_binary_bounds (r : β„š) (B : β„•)
(hr : 0 < r) (hbits : rationalEncodingBits r ≀ B) :
(2 : β„š) ^ (-(B : β„€)) ≀ r ∧ r ≀ (2 : β„š) ^ (B : β„€) := by
have hnumSize : Nat.size r.num.natAbs ≀ B := by unfold rationalEncodingBits at hbits; omega
have hdenSize : Nat.size r.den ≀ B := by unfold rationalEncodingBits at hbits; omega
have hnum : r.num.natAbs ≀ 2 ^ B :=
(Nat.lt_size_self _).le.trans (Nat.pow_le_pow_right (by decide) hnumSize)
have hden : r.den ≀ 2 ^ B :=
(Nat.lt_size_self _).le.trans (Nat.pow_le_pow_right (by decide) hdenSize)
have hnumPos : 0 < r.num := Rat.num_pos.mpr hr
have hnumOne : 1 ≀ r.num.natAbs := Int.natAbs_pos.mpr (ne_of_gt hnumPos)
have hrep : r = (r.num.natAbs : β„š) / r.den := by
rw [← Int.cast_natCast, Int.natCast_natAbs, abs_of_pos hnumPos]
exact r.num_div_den.symm
have hd : (0 : β„š) < r.den := by exact_mod_cast r.den_pos
have hp : (0 : β„š) < (2 : β„š) ^ B := pow_pos (by norm_num) _
simp only [zpow_neg, zpow_natCast]
rw [hrep]
constructor
Β· rw [inv_eq_one_div, div_le_div_iffβ‚€ hp hd, one_mul]
exact_mod_cast (hden.trans (Nat.le_mul_of_pos_left _ (by omega : 0 < r.num.natAbs)))
Β· apply (div_le_iffβ‚€ hd).mpr
exact_mod_cast (hnum.trans (Nat.le_mul_of_pos_right _ r.den_pos))
theorem rationalDyadicCeiling_encoding (r : β„š) (B : β„•)
(hr : 0 < r) (hbits : rationalEncodingBits r ≀ B) :
rationalEncodingBits (rationalDyadicCeiling r) ≀ 5 * B + 5 := by
have hrange := positive_rational_binary_bounds r B hr hbits
have hlo := Int.clog_mono_right (b := 2) (zpow_pos (by norm_num : (0 : β„š) < 2) (-(B : β„€))) hrange.1
have hhi := Int.clog_mono_right (b := 2) hr hrange.2
have hclog (z : β„€) : Int.clog 2 ((2 : β„š) ^ z) = z := by
simpa only [Nat.cast_ofNat] using (Int.clog_zpow (R := β„š) (b := 2) (by decide) z)
rw [hclog] at hlo hhi
have htwo : rationalEncodingBits (2 : β„š) = 4 := by
rw [show (2 : β„š) = (2 : β„•) by norm_num, rationalEncodingBits_natCast]
rfl
unfold rationalDyadicCeiling
cases he : Int.clog 2 r with
| ofNat n =>
rw [he, Int.ofNat_eq_natCast] at hlo hhi
have hn : n ≀ B := by omega
rw [Int.ofNat_eq_natCast, zpow_natCast]
have h := rationalEncodingBits_pow (2 : β„š) n
rw [htwo] at h
omega
| negSucc n =>
rw [he, Int.negSucc_eq] at hlo hhi
have hn : n + 1 ≀ B := by omega
rw [zpow_negSucc]
have hp := rationalEncodingBits_pow (2 : β„š) (n + 1)
have hi := rationalEncodingBits_inv ((2 : β„š) ^ (n + 1))
rw [htwo] at hp
omega
theorem rationalMaximum_encoding (fallback : β„š) (values : List β„š) (B : β„•)
(hfallback : rationalEncodingBits fallback ≀ B)
(hvalues : βˆ€ value ∈ values, rationalEncodingBits value ≀ B) :
rationalEncodingBits (rationalMaximum fallback values) ≀ B := by
induction values with
| nil => exact hfallback
| cons value rest ih =>
have hv := hvalues value List.mem_cons_self
have ht := ih (fun q hq => hvalues q (List.mem_cons_of_mem _ hq))
change rationalEncodingBits (max value (rationalMaximum fallback rest)) ≀ B
rcases le_total value (rationalMaximum fallback rest) with h | h
Β· simpa only [max_eq_right h] using ht
Β· simpa only [max_eq_left h] using hv
def bandEnvelopeEncodingBudget (M b initialBits : β„•) : β„• :=
5 * max (Nat.size b + Nat.size M + 7) (initialBits + 2) + 5
theorem bandChildEnvelope_encoding {ny M : β„•} (b : β„•)
(band : List (Fin M β†’ TargetInterval ny))
(initial : Fin M β†’ TargetInterval ny β†’ β„š) (B : β„•) (hb : 0 < b) (hM : 0 < M)
(hinitial : βˆ€ action ∈ band, βˆ€ i, rationalEncodingBits (initial i (action i)) ≀ B)
(i : Fin M) :
rationalEncodingBits (bandChildEnvelope b band initial i) ≀ bandEnvelopeEncodingBudget M b B := by
have hmax := rationalMaximum_encoding 0 (band.map fun action => initial i (action i)) (B + 2)
(by norm_num [rationalEncodingBits]) (by
intro q hq
obtain ⟨action, ha, rfl⟩ := List.mem_map.mp hq
exact (hinitial action ha i).trans (by omega))
have hfloor := rationalEncodingBits_div (b : β„š) (M : β„š)
rw [rationalEncodingBits_natCast, rationalEncodingBits_natCast] at hfloor
have hbits : rationalEncodingBits (max ((b : β„š) / M)
(rationalMaximum 0 (band.map fun action => initial i (action i)))) ≀
max (Nat.size b + Nat.size M + 7) (B + 2) := by
rcases le_total ((b : β„š) / M) (rationalMaximum 0 (band.map fun action => initial i (action i))) with h | h
Β· rw [max_eq_right h]
exact hmax.trans (Nat.le_max_right _ _)
Β· rw [max_eq_left h]
exact hfloor.trans (by have h := Nat.le_max_left (Nat.size b + Nat.size M + 7) (B + 2); omega)
exact rationalDyadicCeiling_encoding _ _
((div_pos (by exact_mod_cast hb) (by exact_mod_cast hM)).trans_le (le_max_left _ _)) hbits
end EditApproximation
end OAI