cfahlgren1's picture
download
raw
4.12 kB
import Mathlib
namespace OAI
open Matrix
namespace CurrentKS.RationalRoot
def squareInterval (q : ℚ) : ℕ → ℚ × ℚ
| 0 => (0,1)
| k+1 =>
let s := squareInterval q k
let m := (s.1+s.2)/2
if m^2 ≤ q then (m,s.2) else (s.1,m)
lemma squareInterval_spec (q : ℚ) (r : ℝ) (hr : 0 ≤ r) (hr1 : r ≤ 1)
(hq : (q:ℝ)=r^2) (k : ℕ) :
0 ≤ (squareInterval q k).1 ∧ (squareInterval q k).2 ≤ 1 ∧
((squareInterval q k).1:ℝ) ≤ r ∧ r ≤ ((squareInterval q k).2:ℝ) ∧
(squareInterval q k).2-(squareInterval q k).1=(1/2:ℚ)^k := by
induction k with
| zero => simpa [squareInterval] using And.intro hr hr1
| succ k ih =>
have horder : (squareInterval q k).1 ≤ (squareInterval q k).2 := by
exact_mod_cast ih.2.2.1.trans ih.2.2.2.1
let m : ℚ := ((squareInterval q k).1+(squareInterval q k).2)/2
have hml : (squareInterval q k).1 ≤ m := by dsimp [m]; linarith
have hmu : m ≤ (squareInterval q k).2 := by dsimp [m]; linarith
have hm : 0 ≤ m := ih.1.trans hml
have hmr : (0:ℝ) ≤ m := by exact_mod_cast hm
have he : squareInterval q (k+1) = if m^2 ≤ q then (m,(squareInterval q k).2) else ((squareInterval q k).1,m) := rfl
rw [he]
split
· rename_i h
have hc : (m:ℝ)^2 ≤ r^2 := by rw [← hq]; exact_mod_cast h
have hle := (pow_le_pow_iff_left₀ hmr hr (by decide : 2≠0)).mp hc
refine ⟨hm,ih.2.1,hle,ih.2.2.2.1,?_⟩
change (squareInterval q k).2-m=(1/2:ℚ)^(k+1)
rw [pow_succ,← ih.2.2.2.2]
dsimp [m]
ring
· rename_i h
have hc : r^2 ≤ (m:ℝ)^2 := by rw [← hq]; exact_mod_cast (le_of_lt (lt_of_not_ge h))
have hle := (pow_le_pow_iff_left₀ hr hmr (by decide : 2≠0)).mp hc
refine ⟨ih.1,hmu.trans ih.2.1,ih.2.2.1,hle,?_⟩
change m-(squareInterval q k).1=(1/2:ℚ)^(k+1)
rw [pow_succ,← ih.2.2.2.2]
dsimp [m]
ring
lemma squareInterval_upper_error (q : ℚ) (r : ℝ) (hr : 0 ≤ r) (hr1 : r ≤ 1)
(hq : (q:ℝ)=r^2) (k : ℕ) :
|((squareInterval q k).2:ℝ)-r| ≤ (1/2:ℝ)^k := by
obtain ⟨_,_,hl,hu,hw⟩ := squareInterval_spec q r hr hr1 hq k
have hwr : ((squareInterval q k).2:ℝ)-(squareInterval q k).1=(1/2:ℝ)^k := by
have hc := congrArg (fun z : ℚ => (z : ℝ)) hw
norm_num only [Rat.cast_sub,Rat.cast_pow,Rat.cast_div,Rat.cast_one,Rat.cast_ofNat] at hc
exact hc
rw [abs_of_nonneg (sub_nonneg.mpr hu)]
linarith
lemma inverse_pow_size_le {δ : ℚ} (hδ : 0 < δ) : (1/2:ℚ)^δ.den.size ≤ δ := by
have hd : (0:ℚ) < δ.den := by exact_mod_cast δ.den_pos
have hp : (0:ℚ) < 2^δ.den.size := by positivity
have hnum : (1:ℚ) ≤ δ.num := by exact_mod_cast (Int.add_one_le_iff.mpr (Rat.num_pos.mpr hδ))
have hden : (δ.den:ℚ) ≤ 2^δ.den.size := by exact_mod_cast (Nat.lt_size_self δ.den).le
rw [_root_.one_div_pow,one_div]
calc
(2^δ.den.size:ℚ)⁻¹ ≤ (δ.den:ℚ)⁻¹ := inv_anti₀ hd hden
_ ≤ (δ.num:ℚ)/(δ.den:ℚ) := by rw [inv_eq_one_div]; exact div_le_div_of_nonneg_right hnum hd.le
_ = δ := Rat.num_div_den δ
def upperSqrt (q δ : ℚ) : ℚ := (squareInterval q δ.den.size).2
theorem upperSqrt_spec (q δ : ℚ) (hδ : 0 < δ) (hq : 0 ≤ q) (hq1 : q ≤ 1) :
Real.sqrt (q:ℝ) ≤ (upperSqrt q δ:ℝ) ∧
(upperSqrt q δ:ℝ) ≤ Real.sqrt (q:ℝ)+(δ:ℝ) := by
have hr := Real.sqrt_nonneg (q:ℝ)
have hr1 : Real.sqrt (q:ℝ) ≤ 1 := by
exact (Real.sqrt_le_one).mpr (by exact_mod_cast hq1)
have he : (q:ℝ) = Real.sqrt (q:ℝ)^2 := (Real.sq_sqrt (by exact_mod_cast hq)).symm
have hs := squareInterval_spec q (Real.sqrt q) hr hr1 he δ.den.size
have herror := squareInterval_upper_error q (Real.sqrt q) hr hr1 he δ.den.size
have hh : (1/2:ℝ)^δ.den.size ≤ (δ:ℝ) := by
have h := Rat.cast_le (K := ℝ).mpr (inverse_pow_size_le hδ)
norm_num only [Rat.cast_pow,Rat.cast_div,Rat.cast_one,Rat.cast_ofNat] at h
exact h
refine ⟨hs.2.2.2.1,?_⟩
have hm := (abs_le.mp herror).2
dsimp [upperSqrt]
linarith
end CurrentKS.RationalRoot
end OAI

Xet Storage Details

Size:
4.12 kB
·
Xet hash:
024c9c8c364a0f4e6333fced317cb5755dfa1f9a7db206f2947cea4802bd7b50

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.