Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/AllFieldCertificates.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 10.1 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/AllFieldCertificates.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/AllFieldCertificates.lean
-
curl -L -o AllFieldCertificates.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/AllFieldCertificates.lean
10.1 kB
| import OAI.LinearAlgebra.MatrixMultiplication.Numerical.ComplexRationalLogCertificate | |
| namespace OAI | |
| /-! Exact rational intervals, logarithm bounds and arithmetic circuit soundness. -/ | |
| namespace MatrixMultiplication.AllFieldCertificates | |
| open MatrixMultiplication.Foundation.RationalLogCertificate | |
| structure Ball where | |
| center : β | |
| radius : β | |
| deriving DecidableEq | |
| namespace Ball | |
| def Encloses (b : Ball) (x : β) : Prop := |x - (b.center : β)| β€ (b.radius : β) | |
| def exact (q : β) : Ball := β¨q, 0β© | |
| def add (a b : Ball) : Ball := β¨a.center + b.center, a.radius + b.radiusβ© | |
| def neg (a : Ball) : Ball := β¨-a.center, a.radiusβ© | |
| def mul (a b : Ball) : Ball := | |
| β¨a.center * b.center, | |
| |a.center| * b.radius + |b.center| * a.radius + a.radius * b.radiusβ© | |
| def lower (b : Ball) : β := b.center - b.radius | |
| def upper (b : Ball) : β := b.center + b.radius | |
| def ofBounds (lo hi : β) : Ball := β¨(lo + hi) / 2, (hi - lo) / 2β© | |
| theorem nonnegative_radius {b : Ball} {x : β} (h : b.Encloses x) : 0 β€ b.radius := by | |
| have : (0 : β) β€ b.radius := (abs_nonneg _).trans h | |
| exact_mod_cast this | |
| theorem bounds {b : Ball} {x : β} (h : b.Encloses x) : | |
| (b.lower : β) β€ x β§ x β€ (b.upper : β) := by | |
| obtain β¨hl, huβ© := abs_le.mp h | |
| simp only [lower, upper, Rat.cast_sub, Rat.cast_add] | |
| constructor <;> linarith | |
| theorem ofBounds_sound {lo hi : β} {x : β} | |
| (hlo : (lo : β) β€ x) (hhi : x β€ (hi : β)) : | |
| (ofBounds lo hi).Encloses x := by | |
| simp only [Encloses, ofBounds, Rat.cast_add, Rat.cast_div, Rat.cast_ofNat, | |
| Rat.cast_sub] | |
| exact abs_le.mpr β¨by linarith, by linarithβ© | |
| theorem exact_sound (q : β) : (exact q).Encloses (q : β) := by | |
| simp [Encloses, exact] | |
| theorem add_sound {a b : Ball} {x y : β} (hx : a.Encloses x) (hy : b.Encloses y) : | |
| (add a b).Encloses (x + y) := by | |
| unfold Encloses add | |
| simp only [Rat.cast_add] | |
| calc | |
| _ = |(x - (a.center : β)) + (y - (b.center : β))| := by congr 1; ring | |
| _ β€ |x - (a.center : β)| + |y - (b.center : β)| := abs_add_le _ _ | |
| _ β€ _ := add_le_add hx hy | |
| theorem neg_sound {a : Ball} {x : β} (hx : a.Encloses x) : | |
| (neg a).Encloses (-x) := by | |
| simpa only [Encloses, neg, Rat.cast_neg, neg_sub_neg, abs_sub_comm] using hx | |
| theorem mul_sound {a b : Ball} {x y : β} (hx : a.Encloses x) (hy : b.Encloses y) : | |
| (mul a b).Encloses (x * y) := by | |
| have ha : (0 : β) β€ a.radius := by exact_mod_cast nonnegative_radius hx | |
| have hb : (0 : β) β€ b.radius := by exact_mod_cast nonnegative_radius hy | |
| unfold Encloses mul | |
| simp only [Rat.cast_mul, Rat.cast_add, Rat.cast_abs] | |
| calc | |
| _ = |(a.center : β) * (y - (b.center : β)) + | |
| (b.center : β) * (x - (a.center : β)) + | |
| (x - (a.center : β)) * (y - (b.center : β))| := by congr 1; ring | |
| _ β€ |(a.center : β) * (y - (b.center : β))| + | |
| |(b.center : β) * (x - (a.center : β))| + | |
| |(x - (a.center : β)) * (y - (b.center : β))| := | |
| (abs_add_le _ _).trans (add_le_add_left (abs_add_le _ _) _) | |
| _ = |(a.center : β)| * |y - (b.center : β)| + | |
| |(b.center : β)| * |x - (a.center : β)| + | |
| |x - (a.center : β)| * |y - (b.center : β)| := by simp only [abs_mul] | |
| _ β€ _ := add_le_add | |
| (add_le_add (mul_le_mul_of_nonneg_left hy (abs_nonneg _)) | |
| (mul_le_mul_of_nonneg_left hx (abs_nonneg _))) | |
| (mul_le_mul hx hy (abs_nonneg _) ha) | |
| theorem widen_sound {a b : Ball} {x : β} (hx : a.Encloses x) | |
| (h : |a.center - b.center| + a.radius β€ b.radius) : b.Encloses x := by | |
| have hc : |(a.center : β) - (b.center : β)| + (a.radius : β) β€ (b.radius : β) := by | |
| exact_mod_cast h | |
| unfold Encloses | |
| calc | |
| _ = |(x - (a.center : β)) + ((a.center : β) - (b.center : β))| := by congr 1; ring | |
| _ β€ |x - (a.center : β)| + |(a.center : β) - (b.center : β)| := abs_add_le _ _ | |
| _ β€ (a.radius : β) + |(a.center : β) - (b.center : β)| := add_le_add_left hx _ | |
| _ β€ _ := by linarith | |
| def inv (a : Ball) : Ball := ofBounds (1 / a.upper) (1 / a.lower) | |
| theorem inv_sound {a : Ball} {x : β} (hx : a.Encloses x) | |
| (hpos : 0 < a.lower) : (inv a).Encloses xβ»ΒΉ := by | |
| have hlo : (0 : β) < a.lower := by exact_mod_cast hpos | |
| obtain β¨hl, huβ© := bounds hx | |
| have hxp : 0 < x := lt_of_lt_of_le hlo hl | |
| apply ofBounds_sound | |
| Β· simpa only [Rat.cast_div, Rat.cast_one, Rat.cast_inv, one_div] using | |
| one_div_le_one_div_of_le hxp hu | |
| Β· simpa only [Rat.cast_div, Rat.cast_one, Rat.cast_inv, one_div] using | |
| one_div_le_one_div_of_le hlo hl | |
| end Ball | |
| def reductionError {x : β} (d : ReducedArgument x) : β := | |
| (1 + |(d.exponent : β)|) / 2 ^ 120 | |
| theorem reduction_error_cast {x : β} (d : ReducedArgument x) : | |
| (reductionError d : β) = (1 + |(d.exponent : β)|) / 2 ^ 120 := by | |
| simp [reductionError] | |
| def logBall (b : Ball) (dl : ReducedArgument b.lower) (du : ReducedArgument b.upper) : Ball := | |
| Ball.ofBounds (reducedApprox dl - reductionError dl) (reducedApprox du + reductionError du) | |
| theorem logBall_sound {b : Ball} {x : β} (hx : b.Encloses x) | |
| (dl : ReducedArgument b.lower) (du : ReducedArgument b.upper) : | |
| (logBall b dl du).Encloses (Real.log x) := by | |
| have hlo : (0 : β) < b.lower := by exact_mod_cast dl.positive | |
| obtain β¨hl, huβ© := Ball.bounds hx | |
| have hxp : 0 < x := lt_of_lt_of_le hlo hl | |
| have el := abs_le.mp (reduced_log_error dl) | |
| have eu := abs_le.mp (reduced_log_error du) | |
| apply Ball.ofBounds_sound | |
| Β· have hm := Real.log_le_log hlo hl | |
| simp only [Rat.cast_sub, reduction_error_cast] | |
| linarith [el.1] | |
| Β· have hm := Real.log_le_log hxp hu | |
| simp only [Rat.cast_add, reduction_error_cast] | |
| linarith [eu.2] | |
| theorem log_center_lipschitz {b : Ball} {x : β} (hx : b.Encloses x) | |
| (hpos : 0 < b.lower) : | |
| |Real.log x - Real.log (b.center : β)| β€ (b.radius / b.lower : β) := by | |
| have hr : (0 : β) β€ b.radius := by exact_mod_cast Ball.nonnegative_radius hx | |
| have hl : (0 : β) < b.lower := by exact_mod_cast hpos | |
| have hcl : (b.lower : β) β€ b.center := by | |
| simp only [Ball.lower, Rat.cast_sub] | |
| linarith | |
| have hc : (0 : β) < b.center := hl.trans_le hcl | |
| obtain β¨hxl, _β© := Ball.bounds hx | |
| have hxp : 0 < x := hl.trans_le hxl | |
| have logs (a c : β) (ha : 0 < a) (hc : 0 < c) : | |
| Real.log a - Real.log c β€ (a - c) / c := by | |
| rw [β Real.log_div ha.ne' hc.ne'] | |
| have h := Real.log_le_sub_one_of_pos (div_pos ha hc) | |
| have he : a / c - 1 = (a - c) / c := by field_simp | |
| rwa [he] at h | |
| have hup : Real.log x - Real.log (b.center : β) β€ (b.radius : β) / (b.lower : β) := by | |
| calc | |
| _ β€ (x - (b.center : β)) / (b.center : β) := logs _ _ hxp hc | |
| _ β€ (b.radius : β) / (b.center : β) := | |
| div_le_div_of_nonneg_right (abs_le.mp hx).2 hc.le | |
| _ β€ _ := div_le_div_of_nonneg_left hr hl hcl | |
| have hdown : Real.log (b.center : β) - Real.log x β€ | |
| (b.radius : β) / (b.lower : β) := by | |
| calc | |
| _ β€ ((b.center : β) - x) / x := logs _ _ hc hxp | |
| _ β€ (b.radius : β) / x := | |
| div_le_div_of_nonneg_right (by linarith [(abs_le.mp hx).1]) hxp.le | |
| _ β€ _ := div_le_div_of_nonneg_left hr hl hxl | |
| rw [Rat.cast_div] | |
| exact abs_le.mpr β¨by linarith, hupβ© | |
| inductive Expression where | |
| | constant : β β Expression | |
| | add : Expression β Expression β Expression | |
| | neg : Expression β Expression | |
| | mul : Expression β Expression β Expression | |
| | inv : Expression β Expression | |
| | log : Expression β Expression | |
| noncomputable def Expression.value : Expression β β | |
| | .constant q => q | |
| | .add a b => a.value + b.value | |
| | .neg a => -a.value | |
| | .mul a b => a.value * b.value | |
| | .inv a => a.valueβ»ΒΉ | |
| | .log a => Real.log a.value | |
| inductive Certificate : Expression β Ball β Prop where | |
| | constant (q : β) : Certificate (.constant q) (.exact q) | |
| | add {e f a b} : Certificate e a β Certificate f b β Certificate (.add e f) (.add a b) | |
| | neg {e a} : Certificate e a β Certificate (.neg e) (.neg a) | |
| | mul {e f a b} : Certificate e a β Certificate f b β Certificate (.mul e f) (.mul a b) | |
| | inv {e a} : Certificate e a β 0 < a.lower β Certificate (.inv e) (.inv a) | |
| | log {e a} (dl : ReducedArgument a.lower) (du : ReducedArgument a.upper) : | |
| Certificate e a β Certificate (.log e) (logBall a dl du) | |
| | widen {e a b} : Certificate e a β |a.center - b.center| + a.radius β€ b.radius β | |
| Certificate e b | |
| theorem Certificate.sound {e : Expression} {b : Ball} (h : Certificate e b) : | |
| b.Encloses e.value := by | |
| induction h with | |
| | constant q => exact Ball.exact_sound q | |
| | add _ _ ihβ ihβ => exact Ball.add_sound ihβ ihβ | |
| | neg _ ih => exact Ball.neg_sound ih | |
| | mul _ _ ihβ ihβ => exact Ball.mul_sound ihβ ihβ | |
| | inv _ hp ih => exact Ball.inv_sound ih hp | |
| | log dl du _ ih => exact logBall_sound ih dl du | |
| | widen _ hw ih => exact Ball.widen_sound ih hw | |
| theorem published_endpoint_below_target : | |
| (2371054886006745685 : β) / 10 ^ 18 < (2371054887 : β) / 10 ^ 9 := by | |
| decide +kernel | |
| theorem value_below_target_of_certificate {e : Expression} {b : Ball} | |
| (hc : Certificate e b) | |
| (hu : b.upper β€ (2371054886006745685 : β) / 10 ^ 18) : | |
| e.value < (2371054887 : β) / 10 ^ 9 := by | |
| have hbound := (Ball.bounds hc.sound).2 | |
| have hu' : (b.upper : β) β€ (2371054886006745685 : β) / 10 ^ 18 := by | |
| have hcast : (b.upper : β) β€ ((2371054886006745685 / 10 ^ 18 : β) : β) := | |
| Rat.cast_le.mpr hu | |
| simpa only [Rat.cast_div, Rat.cast_pow, Rat.cast_ofNat] using hcast | |
| have htarget : (2371054886006745685 : β) / 10 ^ 18 < | |
| (2371054887 : β) / 10 ^ 9 := by | |
| have hcast : ((2371054886006745685 / 10 ^ 18 : β) : β) < | |
| ((2371054887 / 10 ^ 9 : β) : β) := Rat.cast_lt.mpr published_endpoint_below_target | |
| simpa only [Rat.cast_div, Rat.cast_pow, Rat.cast_ofNat] using hcast | |
| exact lt_of_le_of_lt (hbound.trans hu') htarget | |
| end MatrixMultiplication.AllFieldCertificates | |
| end OAI | |