math / data /lean /OAI /Combinatorics /EditApproximation /Binary /BinaryBandInputs.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
7.97 kB
import OAI.Combinatorics.EditApproximation.Binary.BinaryOptimizerWork
import OAI.Combinatorics.EditApproximation.Geometry.BandNumericalBits
import OAI.Combinatorics.EditApproximation.Definitions
namespace OAI
/-! Actual Boolean construction of the normalized band vertices.
The input masses are stored fractions; their sum and divisions use the
counted routines and no reduced-rational conversion is performed. -/
namespace EditApproximation
namespace BinaryFraction
open Finset
theorem value_abs_le_pow_width (a : BinaryFraction) : |a.value| ≀ (2 : β„š) ^ a.width := by
have hn := (bitWordValue_lt_pow_length a.numerator.bits).le
have hw : a.numerator.bits.length ≀ a.width := le_max_left _ _
have hn' : bitWordValue a.numerator.bits ≀ 2 ^ a.width :=
hn.trans (Nat.pow_le_pow_right (by decide) hw)
have hden : (1 : β„š) ≀ bitWordValue a.denominator := by exact_mod_cast a.denominator_pos
have habs : |(a.numerator.value : β„š)| = bitWordValue a.numerator.bits := by
cases h : a.numerator.negative <;> simp [SignedBinary.value, signedMagnitude, h]
unfold value
rw [abs_div, habs, abs_of_pos (lt_of_lt_of_le (by norm_num : (0 : β„š) < 1) hden)]
apply (div_le_iffβ‚€ (lt_of_lt_of_le (by norm_num : (0 : β„š) < 1) hden)).mpr
have hnQ : (bitWordValue a.numerator.bits : β„š) ≀ (2 : β„š) ^ a.width := by exact_mod_cast hn'
exact hnQ.trans (le_mul_of_one_le_right (by positivity) hden)
theorem bandMassTotalWithWork_value {M : β„•} (mass : Vector BinaryFraction M) :
(bandMassTotalWithWork mass).1.value = βˆ‘ i, (mass.get i).value := by
rw [bandMassTotalWithWork, ← representation_value, dotWithWork_representation,
UnreducedRational.dot_value]
simp [one_representation]
def bandMassTotalStorageBudget (M B : β„•) : β„• :=
Nat.size M + B + 2 * (1 + M * (B + 1)) + 2
theorem bandMassTotalWithWork_storage {M : β„•} (mass : Vector BinaryFraction M)
(B : β„•) (hmass : βˆ€ i, (mass.get i).width ≀ B) :
(bandMassTotalWithWork mass).1.representation.storageBits ≀ bandMassTotalStorageBudget M B := by
have hvalue (i : Fin M) : |(mass.get i).value| ≀ ((2 ^ B : β„•) : β„š) := by
have h := (value_abs_le_pow_width (mass.get i)).trans
(pow_le_pow_rightβ‚€ (by norm_num : (1 : β„š) ≀ 2) (hmass i))
simpa only [Nat.cast_pow, Nat.cast_ofNat] using h
have h := UnreducedRational.dotList_storageBits
(mass.map representation) ((Vector.replicate M one).map representation)
(List.finRange M) B 1 (2 ^ B) 1
(by intro i _; simpa only [vector_map_get] using (representation_den_bits_le_width (mass.get i)).trans (hmass i))
(by intro i _; simp [one_representation, UnreducedRational.one, UnreducedRational.ofRat])
(by intro i _; simpa only [vector_map_get, representation_value] using hvalue i)
(by intro i _; simp [one_representation])
simp only [List.length_finRange, Nat.mul_one] at h
have hsize := (natural_product_bit_length M (2 ^ B)).trans
(Nat.add_le_add_left (by rw [Nat.size_pow] : Nat.size (2 ^ B) ≀ B + 1) M.size)
rw [bandMassTotalWithWork, dotWithWork_representation]
change (UnreducedRational.dotList (mass.map representation)
((Vector.replicate M one).map representation) (List.finRange M)).storageBits ≀ _
unfold bandMassTotalStorageBudget
omega
def bandVertexWordBudget (M B : β„•) : β„• := 2 * B + bandMassTotalStorageBudget M B + 3
def bandCoordinateWithWork {M : β„•} {Οƒ : Type*} [DecidableEq Οƒ]
(mass : Vector BinaryFraction M) (total : BinaryFraction)
(action : Fin M β†’ Οƒ) (coordinate : Fin M Γ— Οƒ) : BinaryFraction Γ— β„• :=
if coordinate.2 = action coordinate.1 then divWithWork (mass.get coordinate.1) total else (zero, 1)
theorem bandCoordinateWithWork_value {M : β„•} {Οƒ : Type*} [DecidableEq Οƒ]
(mass : Vector BinaryFraction M) (action : Fin M β†’ Οƒ) (coordinate : Fin M Γ— Οƒ) :
(bandCoordinateWithWork mass (bandMassTotalWithWork mass).1 action coordinate).1.value =
actionVector (fun i => (mass.get i).value) action coordinate := by
unfold bandCoordinateWithWork actionVector
split_ifs
Β· rw [← representation_value, divWithWork_representation,
UnreducedRational.value_div, representation_value, representation_value,
bandMassTotalWithWork_value]
Β· simp [zero, value, SignedBinary.value, signedMagnitude, bitWordValue]
theorem bandCoordinateWithWork_bounds {M : β„•} {Οƒ : Type*} [DecidableEq Οƒ]
(mass : Vector BinaryFraction M) (action : Fin M β†’ Οƒ) (coordinate : Fin M Γ— Οƒ)
(B : β„•) (hmass : βˆ€ i, (mass.get i).width ≀ B) (hpos : βˆ€ i, 0 < (mass.get i).value) :
let output := (bandCoordinateWithWork mass (bandMassTotalWithWork mass).1 action coordinate).1
output.width ≀ output.representation.storageBits ∧
(0 ≀ output.value ∧ output.value ≀ 1) ∧
output.representation.storageBits ≀ bandVertexWordBudget M B := by
dsimp only
have hvalues := actionVector_zero_one (fun i => (mass.get i).value) action coordinate hpos
rw [← bandCoordinateWithWork_value mass action coordinate] at hvalues
refine ⟨?_, hvalues, ?_⟩
Β· unfold bandCoordinateWithWork
split_ifs
Β· exact divWithWork_self_width _ _
Β· exact zero_width_le_storageBits
Β· have ht := bandMassTotalWithWork_storage mass B hmass
have hm := representation_storageBits_le (mass.get coordinate.1)
have hi := hmass coordinate.1
unfold bandCoordinateWithWork
split_ifs
Β· rw [divWithWork_representation]
have hd := UnreducedRational.storageBits_div (mass.get coordinate.1).representation
(bandMassTotalWithWork mass).1.representation
unfold bandVertexWordBudget
omega
Β· change UnreducedRational.zero.storageBits ≀ _
rw [UnreducedRational.storageBits_zero]
unfold bandVertexWordBudget
omega
def bandVerticesWithWork {M d : β„•} {Οƒ : Type*} [DecidableEq Οƒ]
(coordinates : Vector (Fin M Γ— Οƒ) d) (mass : Vector BinaryFraction M)
(band : List (Fin M β†’ Οƒ)) : List (Vector BinaryFraction d) Γ— β„• :=
let total := bandMassTotalWithWork mass
let vertices := band.map fun action => vectorMapWithWork d fun i =>
bandCoordinateWithWork mass total.1 action (coordinates.get i)
(vertices.map Prod.fst, total.2 + (vertices.map Prod.snd).sum + 6 * band.length)
theorem bandVerticesWithWork_values {M d : β„•} {Οƒ : Type*} [DecidableEq Οƒ]
(coordinates : Vector (Fin M Γ— Οƒ) d) (mass : Vector BinaryFraction M)
(band : List (Fin M β†’ Οƒ)) :
(bandVerticesWithWork coordinates mass band).1.map (fun v i => (v.get i).value) =
band.map (fun action i => actionVector (fun i => (mass.get i).value) action (coordinates.get i)) := by
simp only [bandVerticesWithWork, List.map_map, Function.comp_def]
apply List.map_congr_left
intro action _
funext i
simp only [vectorMapWithWork_value, bandCoordinateWithWork_value]
theorem bandVerticesWithWork_input_bounds {M d : β„•} {Οƒ : Type*} [DecidableEq Οƒ]
(coordinates : Vector (Fin M Γ— Οƒ) d) (mass : Vector BinaryFraction M)
(band : List (Fin M β†’ Οƒ)) (B : β„•)
(hmass : βˆ€ i, (mass.get i).width ≀ B) (hpos : βˆ€ i, 0 < (mass.get i).value)
(v : Vector BinaryFraction d) (hv : v ∈ (bandVerticesWithWork coordinates mass band).1)
(i : Fin d) :
(v.get i).width ≀ (v.get i).representation.storageBits ∧
(0 ≀ (v.get i).value ∧ (v.get i).value ≀ 1) ∧
Nat.size (v.get i).representation.den ≀ bandVertexWordBudget M B ∧
(v.get i).width ≀ bandVertexWordBudget M B := by
simp only [bandVerticesWithWork, List.map_map, Function.comp_def] at hv
obtain ⟨action, _, rfl⟩ := List.mem_map.mp hv
simp only [vectorMapWithWork_value]
have h := bandCoordinateWithWork_bounds mass action (coordinates.get i) B hmass hpos
dsimp only at h
refine ⟨h.1, h.2.1, ?_, h.1.trans h.2.2⟩
apply le_trans ?_ h.2.2
unfold UnreducedRational.storageBits
omega
end BinaryFraction
end EditApproximation
end OAI