Download data/lean/OAI/Combinatorics/EditApproximation/Numerics/NumericalWork.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 15 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/EditApproximation/Numerics/NumericalWork.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/Combinatorics/EditApproximation/Numerics/NumericalWork.lean
-
curl -L -o NumericalWork.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/Combinatorics/EditApproximation/Numerics/NumericalWork.lean
15 kB
| import OAI.Combinatorics.EditApproximation.Numerics.Numerics | |
| namespace OAI | |
| /-! | |
| # Concrete numerical representation and arithmetic work | |
| These are integer bit-length and exact rational-operation bounds for the | |
| finite optimizer. They are not a RAM theorem: the costed evaluator below | |
| counts rational arithmetic operations, while bit-operation costs also | |
| require the charged evaluation model used by the surrounding algorithm. | |
| -/ | |
| namespace EditApproximation | |
| open Finset | |
| /-- Signed numerator plus positive denominator encoding length. -/ | |
| def rationalEncodingBits (r : β) : β := Nat.size r.num.natAbs + Nat.size r.den + 1 | |
| theorem natural_product_bit_length (a b : β) : | |
| Nat.size (a * b) β€ Nat.size a + Nat.size b := by | |
| apply Nat.size_le.mpr | |
| calc | |
| a * b β€ a * 2 ^ Nat.size b := Nat.mul_le_mul_left a (Nat.lt_size_self b).le | |
| _ < 2 ^ Nat.size a * 2 ^ Nat.size b := | |
| Nat.mul_lt_mul_of_pos_right (Nat.lt_size_self a) (by positivity) | |
| _ = 2 ^ (Nat.size a + Nat.size b) := (pow_add _ _ _).symm | |
| theorem natural_sum_bit_length (a b : β) : | |
| Nat.size (a + b) β€ Nat.size a + Nat.size b + 1 := by | |
| apply Nat.size_le.mpr | |
| have ha : a < 2 ^ (Nat.size a + Nat.size b) := | |
| (Nat.lt_size_self a).trans_le (Nat.pow_le_pow_right (by norm_num) (by omega)) | |
| have hb : b < 2 ^ (Nat.size a + Nat.size b) := | |
| (Nat.lt_size_self b).trans_le (Nat.pow_le_pow_right (by norm_num) (by omega)) | |
| rw [pow_succ] | |
| omega | |
| theorem natural_power_bit_length (a n : β) : | |
| Nat.size (a ^ n) β€ n * Nat.size a + 1 := by | |
| induction n with | |
| | zero => simp | |
| | succ n ih => | |
| rw [pow_succ] | |
| have hmul := natural_product_bit_length (a ^ n) a | |
| nlinarith | |
| theorem natural_finite_product_bit_length {ΞΉ : Type*} (s : Finset ΞΉ) (f : ΞΉ β β) : | |
| Nat.size (β i β s, f i) β€ (β i β s, Nat.size (f i)) + 1 := by | |
| classical | |
| induction s using Finset.induction_on with | |
| | empty => simp | |
| | @insert i s hi ih => | |
| rw [prod_insert hi, sum_insert hi] | |
| have hmul := natural_product_bit_length (f i) (β j β s, f j) | |
| omega | |
| /-- Denominator of a finite rational sum divides the product of the | |
| individual denominators. -/ | |
| theorem rational_finite_sum_denominator {ΞΉ : Type*} (s : Finset ΞΉ) (values : ΞΉ β β) : | |
| (β i β s, values i).den β£ β i β s, (values i).den := by | |
| classical | |
| induction s using Finset.induction_on with | |
| | empty => simp | |
| | @insert i s hi ih => | |
| rw [sum_insert hi, prod_insert hi] | |
| exact (Rat.add_den_dvd _ _).trans (Nat.mul_dvd_mul (dvd_refl _) ih) | |
| /-- The symmetric logarithm parameter has a denominator bounded by | |
| the input's numerator/denominator height. -/ | |
| theorem logarithm_parameter_denominator (r : β) : | |
| ((r - 1) / (r + 1)).den β€ r.num.natAbs + r.den := by | |
| have hden : (r.den : β) β 0 := by exact_mod_cast r.den_ne_zero | |
| have hparameter : (r - 1) / (r + 1) = | |
| Rat.divInt (r.num - (r.den : β€)) (r.num + (r.den : β€)) := by | |
| rw [Rat.divInt_eq_div] | |
| push_cast | |
| conv_lhs => rw [β r.num_div_den] | |
| rw [div_sub_one hden, div_add_one hden, div_div_div_cancel_rightβ hden] | |
| rw [hparameter] | |
| by_cases hzero : r.num + (r.den : β€) = 0 | |
| Β· simp only [hzero, Rat.divInt_zero, Rat.den_zero] | |
| have hpositive := r.den_pos | |
| omega | |
| Β· have hdivides := Rat.den_dvd (r.num - (r.den : β€)) (r.num + (r.den : β€)) | |
| have hdividesNat : (Rat.divInt (r.num - (r.den : β€)) (r.num + (r.den : β€))).den β£ | |
| (r.num + (r.den : β€)).natAbs := by | |
| simpa only [Int.natAbs_natCast] using Int.natAbs_dvd_natAbs.mpr hdivides | |
| exact (Nat.le_of_dvd (Int.natAbs_pos.mpr hzero) hdividesNat).trans (by | |
| simpa only [Int.natAbs_natCast] using Int.natAbs_add_le r.num (r.den : β€)) | |
| theorem logarithm_parameter_bit_length (r : β) : | |
| Nat.size ((r - 1) / (r + 1)).den β€ rationalEncodingBits r := by | |
| exact (Nat.size_le_size (logarithm_parameter_denominator r)).trans | |
| (natural_sum_bit_length r.num.natAbs r.den) | |
| def logarithmDenominatorBudget (r : β) (K : β) : β := | |
| ((r - 1) / (r + 1)).den ^ (K * K) * β k β range K, (2 * k + 1) | |
| theorem sum_odd_logarithm_exponents (K : β) : | |
| (β k β range K, (2 * k + 1)) = K * K := by | |
| induction K with | |
| | zero => simp | |
| | succ K ih => rw [sum_range_succ, ih]; ring | |
| /-- A concrete unreduced denominator for every finite logarithm sum. | |
| The exponent `KΒ²` is deliberately loose and keeps the proof elementary. -/ | |
| theorem rationalLogApproximation_denominator (r : β) (K : β) : | |
| (rationalLogApproximation r K).den β£ logarithmDenominatorBudget r K := by | |
| let b := (r - 1) / (r + 1) | |
| have hterm k : (b ^ (2 * k + 1) / (2 * k + 1 : β)).den β£ | |
| b.den ^ (2 * k + 1) * (2 * k + 1) := by | |
| have hmul := Rat.mul_den_dvd (b ^ (2 * k + 1)) (((2 * k + 1 : β) : β)β»ΒΉ) | |
| rw [Rat.den_pow, Rat.inv_natCast_den_of_pos (by omega : 0 < 2 * k + 1)] at hmul | |
| simpa only [div_eq_mul_inv] using hmul | |
| have hsum := rational_finite_sum_denominator (range K) | |
| (fun k => b ^ (2 * k + 1) / (2 * k + 1 : β)) | |
| have hproducts := Finset.prod_dvd_prod_of_dvd (s := range K) | |
| (fun k => (b ^ (2 * k + 1) / (2 * k + 1 : β)).den) | |
| (fun k => b.den ^ (2 * k + 1) * (2 * k + 1)) (fun k _ => hterm k) | |
| have hproduct : (β k β range K, b.den ^ (2 * k + 1) * (2 * k + 1)) = | |
| logarithmDenominatorBudget r K := by | |
| rw [prod_mul_distrib, prod_pow_eq_pow_sum, sum_odd_logarithm_exponents] | |
| rfl | |
| have htwice := Rat.mul_den_dvd (2 : β) | |
| (β k β range K, b ^ (2 * k + 1) / (2 * k + 1 : β)) | |
| norm_num only [Rat.den_ofNat, one_mul] at htwice | |
| simpa only [rationalLogApproximation, b, Nat.cast_add, Nat.cast_mul, Nat.cast_one, Nat.cast_ofNat] using | |
| htwice.trans (hsum.trans (hproduct βΈ hproducts)) | |
| theorem logarithmDenominatorBudget_positive (r : β) (K : β) : | |
| 0 < logarithmDenominatorBudget r K := by | |
| unfold logarithmDenominatorBudget | |
| exact Nat.mul_pos (pow_pos (Rat.den_pos _) _) (prod_pos (fun k _ => by omega)) | |
| /-- Polynomial bit length of the actual logarithm denominator in the | |
| input encoding length and truncation count. -/ | |
| theorem rationalLogApproximation_denominator_bit_length (r : β) (K : β) : | |
| Nat.size (rationalLogApproximation r K).den β€ | |
| K * K * rationalEncodingBits r + K * Nat.size (2 * K + 1) + 2 := by | |
| have hden := Nat.le_of_dvd (logarithmDenominatorBudget_positive r K) | |
| (rationalLogApproximation_denominator r K) | |
| have hmono := Nat.size_le_size hden | |
| have hmul := natural_product_bit_length (((r - 1) / (r + 1)).den ^ (K * K)) | |
| (β k β range K, (2 * k + 1)) | |
| have hpower := natural_power_bit_length ((r - 1) / (r + 1)).den (K * K) | |
| have hproduct := natural_finite_product_bit_length (range K) (fun k => 2 * k + 1) | |
| have hsum : (β k β range K, Nat.size (2 * k + 1)) β€ K * Nat.size (2 * K + 1) := by | |
| calc | |
| _ β€ β _ β range K, Nat.size (2 * K + 1) := sum_le_sum (fun k hk => | |
| Nat.size_le_size (by have hkK := mem_range.mp hk; omega)) | |
| _ = _ := by simp | |
| have hinput := Nat.mul_le_mul_left (K * K) (logarithm_parameter_bit_length r) | |
| dsimp only [logarithmDenominatorBudget] at hmono | |
| omega | |
| /-- Costed repeated multiplication, counting one rational | |
| multiplication at each successor. -/ | |
| def rationalPowerWithWork (r : β) : β β β Γ β | |
| | 0 => (1, 0) | |
| | n + 1 => let previous := rationalPowerWithWork r n | |
| (previous.1 * r, previous.2 + 1) | |
| theorem rationalPowerWithWork_spec (r : β) (n : β) : | |
| rationalPowerWithWork r n = (r ^ n, n) := by | |
| induction n with | |
| | zero => simp [rationalPowerWithWork] | |
| | succ n ih => simp [rationalPowerWithWork, ih, pow_succ] | |
| /-- The direct finite logarithm summation, with every power, | |
| division and addition counted. -/ | |
| def rationalLogPartialWithWork (b : β) : β β β Γ β | |
| | 0 => (0, 0) | |
| | K + 1 => | |
| let previous := rationalLogPartialWithWork b K | |
| let power := rationalPowerWithWork b (2 * K + 1) | |
| (previous.1 + power.1 / (2 * K + 1 : β), previous.2 + power.2 + 2) | |
| theorem rationalLogPartialWithWork_spec (b : β) (K : β) : | |
| rationalLogPartialWithWork b K = | |
| (β k β range K, b ^ (2 * k + 1) / (2 * k + 1 : β), K * K + 2 * K) := by | |
| induction K with | |
| | zero => simp [rationalLogPartialWithWork] | |
| | succ K ih => | |
| simp only [rationalLogPartialWithWork, ih, rationalPowerWithWork_spec, sum_range_succ] | |
| congr 1 | |
| ring | |
| /-- Three operations form the logarithm parameter, and one final | |
| multiplication doubles the partial sum. -/ | |
| def rationalLogApproximationWithWork (r : β) (K : β) : β Γ β := | |
| let seriesResult := rationalLogPartialWithWork ((r - 1) / (r + 1)) K | |
| (2 * seriesResult.1, seriesResult.2 + 4) | |
| /-- The actual logarithm evaluator uses exactly a quadratic number of | |
| rational arithmetic operations and returns the already verified value. -/ | |
| theorem rationalLogApproximationWithWork_spec (r : β) (K : β) : | |
| rationalLogApproximationWithWork r K = | |
| (rationalLogApproximation r K, K * K + 2 * K + 4) := by | |
| simp [rationalLogApproximationWithWork, rationalLogPartialWithWork_spec, rationalLogApproximation] | |
| /-- Every logarithm series term is bounded on nonnegative inputs; | |
| this controls the returned numerator without transcendental arithmetic. -/ | |
| theorem rationalLogApproximation_abs_bound (r : β) (K : β) (hr : 0 β€ r) : | |
| |rationalLogApproximation r K| β€ 2 * K := by | |
| let b := (r - 1) / (r + 1) | |
| have hrden : 0 < r + 1 := by linarith | |
| have hb : |b| β€ 1 := by | |
| dsimp only [b] | |
| rw [abs_div, abs_of_pos hrden] | |
| apply (div_le_oneβ hrden).mpr | |
| exact abs_le.mpr β¨by linarith, by linarithβ© | |
| have hterm (k : β) : |b ^ (2 * k + 1) / (2 * k + 1 : β)| β€ 1 := by | |
| have hden : (1 : β) β€ (2 * k + 1 : β) := by norm_cast; omega | |
| have hdenpos : (0 : β) < (2 * k + 1 : β) := by positivity | |
| rw [abs_div, abs_pow, abs_of_pos hdenpos] | |
| calc | |
| _ β€ (1 : β) / (2 * k + 1 : β) := div_le_div_of_nonneg_right | |
| (pow_le_oneβ (abs_nonneg b) hb) hdenpos.le | |
| _ β€ 1 := (div_le_oneβ hdenpos).mpr hden | |
| have hsum : |β k β range K, b ^ (2 * k + 1) / (2 * k + 1 : β)| β€ K := by | |
| calc | |
| _ β€ β k β range K, |b ^ (2 * k + 1) / (2 * k + 1 : β)| := abs_sum_le_sum_abs _ _ | |
| _ β€ β _ β range K, (1 : β) := sum_le_sum (fun k _ => hterm k) | |
| _ = K := by simp | |
| unfold rationalLogApproximation | |
| push_cast at hsum | |
| dsimp only [b] at hsum | |
| rw [abs_mul, abs_of_nonneg (by norm_num : (0 : β) β€ 2)] | |
| linarith | |
| theorem rational_numerator_bound (r : β) (M : β) (hM : |r| β€ M) : | |
| r.num.natAbs β€ M * r.den := by | |
| have hscaled := mul_le_mul_of_nonneg_right hM (show (0 : β) β€ r.den by positivity) | |
| have hnum : |r| * (r.den : β) = (r.num.natAbs : β) := by | |
| calc | |
| _ = |r * (r.den : β)| := by rw [abs_mul, abs_of_nonneg (by positivity : (0 : β) β€ r.den)] | |
| _ = |(r.num : β)| := by rw [Rat.mul_den_eq_num] | |
| _ = (r.num.natAbs : β) := by rw [Nat.cast_natAbs, Int.cast_abs] | |
| rw [hnum] at hscaled | |
| exact_mod_cast hscaled | |
| /-- Polynomial bit-length bound for both numerator and denominator of | |
| all actually returned logarithm approximations. -/ | |
| theorem rationalLogApproximation_encoding_bit_length (r : β) (K : β) (hr : 0 β€ r) : | |
| rationalEncodingBits (rationalLogApproximation r K) β€ | |
| 2 * (K * K * rationalEncodingBits r + K * Nat.size (2 * K + 1) + 2) + | |
| Nat.size (2 * K) + 1 := by | |
| have habsolute := rationalLogApproximation_abs_bound r K hr | |
| have habsolute' : |rationalLogApproximation r K| β€ ((2 * K : β) : β) := by | |
| simpa only [Nat.cast_mul, Nat.cast_ofNat] using habsolute | |
| have hnum := Nat.size_le_size (rational_numerator_bound _ (2 * K) habsolute') | |
| have hmul := natural_product_bit_length (2 * K) (rationalLogApproximation r K).den | |
| have hden := rationalLogApproximation_denominator_bit_length r K | |
| dsimp only [rationalEncodingBits] at hden β’ | |
| omega | |
| /-- Stored mixture coefficients use a denominator dividing `K(K+1)`; | |
| there is no growth with the number of earlier optimizer updates. -/ | |
| theorem optimizerMixtureWeight_denominator (K j : β) (hK : 0 < K) : | |
| (optimizerMixtureWeight K j).den β£ K * (K + 1) := by | |
| have hrepresentation : optimizerMixtureWeight K j = | |
| ((2 * (j + 1) : β) : β) * (((K * (K + 1) : β) : β))β»ΒΉ := by | |
| unfold optimizerMixtureWeight | |
| push_cast | |
| ring | |
| rw [hrepresentation] | |
| have hpositive : 0 < K * (K + 1) := Nat.mul_pos hK (by omega) | |
| simpa only [Rat.den_natCast, Rat.inv_natCast_den_of_pos hpositive, one_mul] using | |
| Rat.mul_den_dvd (((2 * (j + 1) : β) : β)) (((K * (K + 1) : β) : β))β»ΒΉ | |
| theorem optimizerMixtureWeight_bit_length (K j : β) (hK : 0 < K) : | |
| Nat.size (optimizerMixtureWeight K j).den β€ 2 * Nat.size K + 2 := by | |
| have hpositive : 0 < K * (K + 1) := Nat.mul_pos hK (by omega) | |
| have hden := Nat.size_le_size (Nat.le_of_dvd hpositive (optimizerMixtureWeight_denominator K j hK)) | |
| have hmul := natural_product_bit_length K (K + 1) | |
| have hadd := natural_sum_bit_length K 1 | |
| norm_num only [Nat.size_one] at hadd | |
| omega | |
| /-- Every actual rational iterate has a polynomial denominator bit | |
| length in its trace length and the listed vertices' input bit bound. -/ | |
| theorem rationalOptimizerIterate_denominator_bit_length {ΞΉ : Type*} | |
| (initial : ΞΉ β β) (selected : β β ΞΉ β β) (K B : β) (hK : 0 < K) (i : ΞΉ) | |
| (hinput : β j β range K, Nat.size (selected j i).den β€ B) : | |
| Nat.size (rationalOptimizerIterate initial selected K i).den β€ | |
| K * (2 * Nat.size K + 2 + B) + 1 := by | |
| have hmixture := rationalOptimizerIterate_eq_mixture initial selected (K - 1) i | |
| rw [Nat.sub_add_cancel (show 1 β€ K by omega)] at hmixture | |
| rw [hmixture] | |
| have hdivides := rational_finite_sum_denominator (range K) | |
| (fun j => optimizerMixtureWeight K j * selected j i) | |
| have hpositive : 0 < β j β range K, (optimizerMixtureWeight K j * selected j i).den := | |
| prod_pos (fun _ _ => Rat.den_pos _) | |
| have hden := Nat.size_le_size (Nat.le_of_dvd hpositive hdivides) | |
| have hproduct := natural_finite_product_bit_length (range K) | |
| (fun j => (optimizerMixtureWeight K j * selected j i).den) | |
| have hterm j (hj : j β range K) : Nat.size (optimizerMixtureWeight K j * selected j i).den β€ | |
| 2 * Nat.size K + 2 + B := by | |
| have hmulden := Nat.size_le_size (Nat.le_of_dvd | |
| (Nat.mul_pos (Rat.den_pos (optimizerMixtureWeight K j)) (Rat.den_pos (selected j i))) | |
| (Rat.mul_den_dvd (optimizerMixtureWeight K j) (selected j i))) | |
| have hmul := natural_product_bit_length (optimizerMixtureWeight K j).den (selected j i).den | |
| have hweight := optimizerMixtureWeight_bit_length K j hK | |
| have hselected := hinput j hj | |
| omega | |
| have hsum : (β j β range K, Nat.size (optimizerMixtureWeight K j * selected j i).den) β€ | |
| K * (2 * Nat.size K + 2 + B) := by | |
| calc | |
| _ β€ β _ β range K, (2 * Nat.size K + 2 + B) := sum_le_sum hterm | |
| _ = _ := by simp | |
| omega | |
| end EditApproximation | |
| end OAI | |