| 1 | module |
| 2 | |
| 3 | public import E424.ToolDefs |
| 4 | |
| 5 | @[expose] public section |
| 6 | |
| 7 | /-! |
| 8 | # Erdős 424: helpers for tool T9 (trapezoid error on a unit interval). |
| 9 | -/ |
| 10 | |
| 11 | set_option linter.style.header false |
| 12 | |
| 13 | namespace E424 |
| 14 | |
| 15 | open MeasureTheory intervalIntegral |
| 16 | |
| 17 | /-- Weight of the trapezoid error on `[a, a+1]`; it vanishes at both ends and lies in `[0, 1/8]`. -/ |
| 18 | noncomputable def trapW (a x : ℝ) : ℝ := (x - a) * (a + 1 - x) / 2 |
| 19 | |
| 20 | /-- Trapezoid identity on a unit interval, by integrating by parts twice. -/ |
| 21 | theorem trap_unit_identity (f : ℝ → ℂ) (hf : ContDiff ℝ 2 f) (a : ℝ) : |
| 22 | (f a + f (a + 1)) / 2 - ∫ x in a..(a + 1), f x = |
| 23 | ∫ x in a..(a + 1), (trapW a x : ℂ) * deriv (deriv f) x := by |
| 24 | have h1 : ContDiff ℝ 1 (deriv f) := (contDiff_succ_iff_deriv.mp (show ContDiff ℝ (1 + 1) f from hf)).2.2 |
| 25 | have hdf : Differentiable ℝ f := hf.differentiable (by norm_num) |
| 26 | have hddf : Differentiable ℝ (deriv f) := h1.differentiable (by norm_num) |
| 27 | have hc : Continuous (deriv (deriv f)) := h1.continuous_deriv (by norm_num) |
| 28 | have hcf : Continuous f := hf.continuous |
| 29 | have hK : ∀ x, HasDerivAt (fun y => (trapW a y : ℂ)) (((2 * a + 1 - 2 * x) / 2 : ℝ) : ℂ) x := by |
| 30 | intro x |
| 31 | have : HasDerivAt (fun y => trapW a y) ((2 * a + 1 - 2 * x) / 2) x := by |
| 32 | unfold trapW |
| 33 | refine ((((hasDerivAt_id x).sub_const a).mul ((hasDerivAt_id x).const_sub (a + 1))).div_const 2).congr_deriv ?_ |
| 34 | simp only [id] |
| 35 | ring |
| 36 | exact this.ofReal_comp |
| 37 | have hK' : ∀ x, HasDerivAt (fun y : ℝ => (((2 * a + 1 - 2 * y) / 2 : ℝ) : ℂ)) ((-1 : ℝ) : ℂ) x := by |
| 38 | intro x |
| 39 | have : HasDerivAt (fun y : ℝ => (2 * a + 1 - 2 * y) / 2) (-1 : ℝ) x := by |
| 40 | refine ((((hasDerivAt_id x).const_mul 2).const_sub (2 * a + 1)).div_const 2).congr_deriv ?_ |
| 41 | ring |
| 42 | exact this.ofReal_comp |
| 43 | have hf' : ∀ x, HasDerivAt f (deriv f x) x := fun x => (hdf x).hasDerivAt |
| 44 | have hdf' : ∀ x, HasDerivAt (deriv f) (deriv (deriv f) x) x := fun x => (hddf x).hasDerivAt |
| 45 | have hint : ∀ g : ℝ → ℂ, Continuous g → IntervalIntegrable g volume a (a + 1) := |
| 46 | fun g hg => hg.intervalIntegrable _ _ |
| 47 | -- first integration by parts: ∫ K f'' = - ∫ K' f' |
| 48 | have step1 := integral_mul_deriv_eq_deriv_mul (a := a) (b := a + 1) |
| 49 | (u := fun y => (trapW a y : ℂ)) (v := deriv f) |
| 50 | (u' := fun y => (((2 * a + 1 - 2 * y) / 2 : ℝ) : ℂ)) (v' := deriv (deriv f)) |
| 51 | (fun x _ => hK x) (fun x _ => hdf' x) |
| 52 | (hint _ (by fun_prop)) (hint _ hc) |
| 53 | -- second: ∫ K' f' = K'(a+1) f(a+1) - K'(a) f(a) - ∫ f |
| 54 | have step2 := integral_mul_deriv_eq_deriv_mul (a := a) (b := a + 1) |
| 55 | (u := fun y => (((2 * a + 1 - 2 * y) / 2 : ℝ) : ℂ)) (v := f) (u' := fun _ => ((-1 : ℝ) : ℂ)) |
| 56 | (v' := deriv f) |
| 57 | (fun x _ => hK' x) (fun x _ => hf' x) |
| 58 | (hint _ continuous_const) (hint _ (hddf.continuous)) |
| 59 | have e0 : trapW a a = 0 := by unfold trapW; ring |
| 60 | have e1 : trapW a (a + 1) = 0 := by unfold trapW; ring |
| 61 | have k0 : (2 * a + 1 - 2 * a) / 2 = (1 / 2 : ℝ) := by ring |
| 62 | have k1 : (2 * a + 1 - 2 * (a + 1)) / 2 = (-1 / 2 : ℝ) := by ring |
| 63 | have hneg : ∫ x in a..(a + 1), ((-1 : ℝ) : ℂ) * f x = -∫ x in a..(a + 1), f x := by |
| 64 | simp |
| 65 | simp only [e0, e1, k0, k1, Complex.ofReal_zero, zero_mul, sub_zero, zero_sub, hneg] at step1 step2 |
| 66 | rw [step1, step2] |
| 67 | push_cast |
| 68 | ring |
| 69 | |
| 70 | /-- The trapezoid error on `[a, a+1]` is at most the integral of `‖f''‖` there (weight `≤ 1/8 ≤ 1`). -/ |
| 71 | theorem trap_unit_bound (f : ℝ → ℂ) (hf : ContDiff ℝ 2 f) (a : ℝ) : |
| 72 | ‖(f a + f (a + 1)) / 2 - ∫ x in a..(a + 1), f x‖ ≤ ∫ x in a..(a + 1), ‖deriv (deriv f) x‖ := by |
| 73 | rw [trap_unit_identity f hf a] |
| 74 | have hc : Continuous (deriv (deriv f)) := |
| 75 | (contDiff_succ_iff_deriv.mp (show ContDiff ℝ (1 + 1) f from hf)).2.2.continuous_deriv (by norm_num) |
| 76 | refine (intervalIntegral.norm_integral_le_integral_norm (by linarith)).trans ?_ |
| 77 | refine intervalIntegral.integral_mono_on (by linarith) ?_ ?_ fun x hx => ?_ |
| 78 | · have hK : Continuous fun x => (trapW a x : ℂ) := Complex.continuous_ofReal.comp (by unfold trapW; fun_prop) |
| 79 | exact ((hK.mul hc).norm).intervalIntegrable _ _ |
| 80 | · exact hc.norm.intervalIntegrable _ _ |
| 81 | · have h0 : 0 ≤ trapW a x := by |
| 82 | unfold trapW; nlinarith [hx.1, hx.2] |
| 83 | have h1 : trapW a x ≤ 1 := by |
| 84 | unfold trapW; nlinarith [hx.1, hx.2, sq_nonneg (x - a - 1 / 2)] |
| 85 | rw [norm_mul, Complex.norm_real, Real.norm_of_nonneg h0] |
| 86 | exact mul_le_of_le_one_left (norm_nonneg _) h1 |
| 87 | |
| 88 | end E424 |
| 89 | |