math / data /lean /OAI /LinearAlgebra /MatrixMultiplication /Numerical /ComplexRationalLogCertificate.lean
Download data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/ComplexRationalLogCertificate.lean from introvoyz045/math: direct link, hf CLI and curl.
- Browser
- Download file 8.42 kB
-
https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/ComplexRationalLogCertificate.lean
- Command line
-
hf download hf://datasets/introvoyz045/math/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/ComplexRationalLogCertificate.lean
-
curl -L -o ComplexRationalLogCertificate.lean https://huggingface.co/datasets/introvoyz045/math/resolve/main/data/lean/OAI/LinearAlgebra/MatrixMultiplication/Numerical/ComplexRationalLogCertificate.lean
8.42 kB
| import Mathlib.Analysis.SpecialFunctions.Log.Deriv | |
| import Mathlib.Tactic.FieldSimp | |
| import Mathlib.Tactic.GCongr | |
| import Mathlib.Tactic.Linarith | |
| import Mathlib.Tactic.NormNum | |
| import Mathlib.Tactic.Ring | |
| namespace OAI | |
| /-! Exact rational intervals, logarithm bounds and arithmetic circuit soundness. -/ | |
| namespace MatrixMultiplication.Foundation.RationalLogCertificate | |
| open scoped BigOperators | |
| noncomputable def sumPower (t : β) (n : β) : β := | |
| β i β Finset.range n, t ^ (i + 1) / (i + 1) | |
| def rationalSumPower (t : β) (n : β) : β := | |
| β i β Finset.range n, t ^ (i + 1) / (i + 1) | |
| noncomputable def realApprox (n : β) (x : β) : β := | |
| sumPower ((x - 1) / (x + 1)) n - sumPower (-((x - 1) / (x + 1))) n | |
| def rationalApprox (n : β) (x : β) : β := | |
| rationalSumPower ((x - 1) / (x + 1)) n - | |
| rationalSumPower (-((x - 1) / (x + 1))) n | |
| @[simp] theorem rationalSumPower_cast (t : β) (n : β) : | |
| (rationalSumPower t n : β) = sumPower (t : β) n := by | |
| simp [rationalSumPower, sumPower] | |
| @[simp] theorem rationalApprox_cast (n : β) (x : β) : | |
| (rationalApprox n x : β) = realApprox n (x : β) := by | |
| simp [rationalApprox, realApprox] | |
| theorem symmetric_expansion_error (t : β) (ht : |t| < 1) (n : β) : | |
| |(Real.log (1 + t) - Real.log (1 - t)) - | |
| (sumPower t n - sumPower (-t) n)| β€ | |
| 2 * (|t| ^ (n + 1) / (1 - |t|)) := by | |
| have hminus : |sumPower t n + Real.log (1 - t)| β€ | |
| |t| ^ (n + 1) / (1 - |t|) := | |
| Real.abs_log_sub_add_sum_range_le ht n | |
| have hplus : |sumPower (-t) n + Real.log (1 + t)| β€ | |
| |t| ^ (n + 1) / (1 - |t|) := by | |
| simpa only [sumPower, abs_neg, sub_neg_eq_add] using | |
| Real.abs_log_sub_add_sum_range_le (by simpa using ht : |-t| < 1) n | |
| calc | |
| _ = |(sumPower (-t) n + Real.log (1 + t)) - | |
| (sumPower t n + Real.log (1 - t))| := by congr 1; ring | |
| _ β€ |sumPower (-t) n + Real.log (1 + t)| + | |
| |sumPower t n + Real.log (1 - t)| := abs_sub _ _ | |
| _ β€ 2 * (|t| ^ (n + 1) / (1 - |t|)) := by linarith | |
| theorem normalized_variable_bounds (x : β) (hlo : 1 β€ x) (hhi : x β€ 2) : | |
| 0 β€ (x - 1) / (x + 1) β§ (x - 1) / (x + 1) β€ 1 / 3 := by | |
| have hden : 0 < x + 1 := by linarith | |
| constructor | |
| Β· exact div_nonneg (sub_nonneg.mpr hlo) hden.le | |
| Β· apply (div_le_iffβ hden).2 | |
| linarith | |
| theorem normalized_log_error (x : β) (hlo : 1 β€ x) (hhi : x β€ 2) (n : β) : | |
| |Real.log x - realApprox n x| β€ | |
| 2 * (((x - 1) / (x + 1)) ^ (n + 1) / (1 - (x - 1) / (x + 1))) := by | |
| let t := (x - 1) / (x + 1) | |
| obtain β¨ht0, ht3β© := normalized_variable_bounds x hlo hhi | |
| have ht1 : t < 1 := by dsimp [t]; linarith | |
| have hden : x + 1 β 0 := by linarith | |
| have hminus : 1 - t β 0 := by linarith | |
| have hplus : 1 + t β 0 := by dsimp [t]; linarith | |
| have hratio : (1 + t) / (1 - t) = x := by | |
| dsimp [t] | |
| field_simp [hden] | |
| ring | |
| have hlog : Real.log x = Real.log (1 + t) - Real.log (1 - t) := by | |
| rw [β hratio] | |
| exact Real.log_div hplus hminus | |
| have ht0' : 0 β€ t := ht0 | |
| have herror := symmetric_expansion_error t (by rwa [abs_of_nonneg ht0']) n | |
| rw [abs_of_nonneg ht0'] at herror | |
| simpa only [realApprox, β hlog, t] using herror | |
| theorem normalized_log_error_80 (x : β) (hlo : 1 β€ x) (hhi : x β€ 2) : | |
| |Real.log x - realApprox 80 x| < 1 / (2 : β) ^ 120 := by | |
| obtain β¨ht0, ht3β© := normalized_variable_bounds x hlo hhi | |
| apply lt_of_le_of_lt (normalized_log_error x hlo hhi 80) | |
| calc | |
| _ β€ 2 * (((1 : β) / 3) ^ 81 / (1 - 1 / 3)) := by gcongr | |
| _ < 1 / (2 : β) ^ 120 := by norm_num | |
| theorem rational_log_error_80 (x : β) (hlo : 1 β€ x) (hhi : x β€ 2) : | |
| |Real.log (x : β) - (rationalApprox 80 x : β)| < 1 / (2 : β) ^ 120 := by | |
| rw [rationalApprox_cast] | |
| exact normalized_log_error_80 (x : β) (by exact_mod_cast hlo) (by exact_mod_cast hhi) | |
| structure ReducedArgument (x : β) where | |
| exponent : β€ | |
| mantissa : β | |
| lower : 1 β€ mantissa | |
| upper : mantissa β€ 2 | |
| value : x = mantissa * (2 : β) ^ exponent | |
| def reducedApprox {x : β} (d : ReducedArgument x) : β := | |
| rationalApprox 80 d.mantissa + (d.exponent : β) * rationalApprox 80 2 | |
| theorem ReducedArgument.positive {x : β} (d : ReducedArgument x) : 0 < x := by | |
| rw [d.value] | |
| exact mul_pos (lt_of_lt_of_le (by norm_num) d.lower) (zpow_pos (by norm_num) _) | |
| theorem reduced_log_error {x : β} (d : ReducedArgument x) : | |
| |Real.log (x : β) - (reducedApprox d : β)| β€ | |
| (1 + |(d.exponent : β)|) / (2 : β) ^ 120 := by | |
| have hm : (0 : β) < d.mantissa := by | |
| have hlo : (1 : β) β€ d.mantissa := by exact_mod_cast d.lower | |
| linarith | |
| have hvalue : (x : β) = (d.mantissa : β) * (2 : β) ^ d.exponent := by | |
| simpa only [Rat.cast_mul, Rat.cast_zpow, Rat.cast_ofNat] using | |
| congrArg (fun q : β => (q : β)) d.value | |
| have hlog : Real.log (x : β) = Real.log (d.mantissa : β) + | |
| (d.exponent : β) * Real.log 2 := by | |
| rw [hvalue, Real.log_mul (ne_of_gt hm) (zpow_ne_zero _ (by norm_num)), | |
| Real.log_zpow] | |
| have hmerr := (rational_log_error_80 d.mantissa d.lower d.upper).le | |
| have htwoerr := (rational_log_error_80 2 (by norm_num) (by norm_num)).le | |
| have hcast : (reducedApprox d : β) = (rationalApprox 80 d.mantissa : β) + | |
| (d.exponent : β) * (rationalApprox 80 2 : β) := by | |
| simp [reducedApprox] | |
| rw [hlog, hcast] | |
| calc | |
| _ = |(Real.log (d.mantissa : β) - (rationalApprox 80 d.mantissa : β)) + | |
| (d.exponent : β) * (Real.log 2 - (rationalApprox 80 2 : β))| := by | |
| congr 1 | |
| ring | |
| _ β€ |Real.log (d.mantissa : β) - (rationalApprox 80 d.mantissa : β)| + | |
| |(d.exponent : β)| * |Real.log 2 - (rationalApprox 80 2 : β)| := by | |
| simpa only [abs_mul] using abs_add_le | |
| (Real.log (d.mantissa : β) - (rationalApprox 80 d.mantissa : β)) | |
| ((d.exponent : β) * (Real.log 2 - (rationalApprox 80 2 : β))) | |
| _ β€ 1 / (2 : β) ^ 120 + |(d.exponent : β)| * (1 / (2 : β) ^ 120) := by | |
| exact add_le_add hmerr (mul_le_mul_of_nonneg_left htwoerr (abs_nonneg _)) | |
| _ = (1 + |(d.exponent : β)|) / (2 : β) ^ 120 := by ring | |
| theorem reduced_entropy_error {x : β} (d : ReducedArgument x) : | |
| |(-(x : β) * Real.log (x : β)) - (-x * reducedApprox d : β)| β€ | |
| (x : β) * ((1 + |(d.exponent : β)|) / (2 : β) ^ 120) := by | |
| have hx : (0 : β) β€ x := by exact_mod_cast d.positive.le | |
| have hcast : ((-x * reducedApprox d : β) : β) = -(x : β) * (reducedApprox d : β) := by | |
| simp | |
| rw [hcast, β mul_sub, abs_mul, abs_neg, abs_of_nonneg hx] | |
| exact mul_le_mul_of_nonneg_left (reduced_log_error d) hx | |
| def finiteEntropyApprox {ΞΉ : Type*} [Fintype ΞΉ] (p : ΞΉ β β) | |
| (d : β i, ReducedArgument (p i)) : β := | |
| β i, -p i * reducedApprox (d i) | |
| theorem finite_entropy_error {ΞΉ : Type*} [Fintype ΞΉ] (p : ΞΉ β β) | |
| (d : β i, ReducedArgument (p i)) : | |
| |(β i, -(p i : β) * Real.log (p i : β)) - (finiteEntropyApprox p d : β)| β€ | |
| β i, (p i : β) * ((1 + |((d i).exponent : β)|) / (2 : β) ^ 120) := by | |
| have hcast : (finiteEntropyApprox p d : β) = | |
| β i, ((-p i * reducedApprox (d i) : β) : β) := by | |
| simp [finiteEntropyApprox] | |
| rw [hcast, β Finset.sum_sub_distrib] | |
| calc | |
| _ β€ β i, |(-(p i : β) * Real.log (p i : β)) - | |
| ((-p i * reducedApprox (d i) : β) : β)| := | |
| Finset.abs_sum_le_sum_abs _ _ | |
| _ β€ _ := Finset.sum_le_sum (fun i _ => reduced_entropy_error (d i)) | |
| theorem finite_entropy_error_of_exponent_bound {ΞΉ : Type*} [Fintype ΞΉ] | |
| (p : ΞΉ β β) (d : β i, ReducedArgument (p i)) | |
| (hnorm : β i, p i = 1) (K : β) | |
| (hK : β i, |((d i).exponent : β)| β€ K) : | |
| |(β i, -(p i : β) * Real.log (p i : β)) - (finiteEntropyApprox p d : β)| β€ | |
| (1 + K) / (2 : β) ^ 120 := by | |
| apply (finite_entropy_error p d).trans | |
| have hnorm' : β i, (p i : β) = 1 := by exact_mod_cast hnorm | |
| calc | |
| _ β€ β i, (p i : β) * ((1 + K) / (2 : β) ^ 120) := by | |
| apply Finset.sum_le_sum | |
| intro i _ | |
| have hp : (0 : β) β€ p i := by exact_mod_cast (d i).positive.le | |
| apply mul_le_mul_of_nonneg_left _ hp | |
| exact div_le_div_of_nonneg_right (add_le_add_right (hK i) 1) (by positivity) | |
| _ = (1 + K) / (2 : β) ^ 120 := by rw [β Finset.sum_mul, hnorm', one_mul] | |
| end MatrixMultiplication.Foundation.RationalLogCertificate | |
| end OAI | |