roseking's picture
Add lean source and binary files (part 91)
caec230 verified
Raw History Blame Contribute Delete
7.63 kB
15
abbrev RationalSpaceTime := ℚ × (Fin 3 → ℚ)
16
abbrev RationalVector := Fin 3 → ℚ
18
def rationalVector (a : RationalVector) : Space := fun k => (a k : ℝ)
20
def IsFastName (a : ℕ → RationalSpaceTime) (y : SpaceTime) : Prop :=
body · 1 line
  ∀ n, ‖y - rationalPoint (a n)‖ ≤ errorTolerance n
23
theorem timeSpaceCoord_norm (j : Fin 4) (v : SpaceTime) :
    ‖timeSpaceCoord j v‖ ≤ ‖v‖ := by
proof · 3 lines
  refine Fin.cases ?_ (fun k => ?_) j
  · exact norm_fst_le v
  · exact (norm_le_pi_norm v.2 k).trans (norm_snd_le v)
29
theorem spaceTimeDirections_sum (v : SpaceTime) :
    (∑ j : Fin 4, timeSpaceCoord j v • spaceTimeDirection j) = v := by
proof · 4 lines
  apply Prod.ext
  · simp [Fin.sum_univ_succ, timeSpaceCoord, spaceTimeDirection]
  · ext k
    fin_cases k <;> simp [Fin.sum_univ_succ, timeSpaceCoord, spaceTimeDirection, basis]
38
def modulusBound (c : VelocityExpr) (α : List (Fin 4)) : ℕ :=
body · 1 line
  ∑ j : Fin 4, c.bound (j :: α)
41
theorem fderiv_bound {c : VelocityExpr} (hc : c.Valid) (α : List (Fin 4)) (y : SpaceTime) :
    ‖fderiv ℝ (mixedDerivative c.val α) y‖ ≤ (c.modulusBound α : ℝ) := by
proof · 15 lines
  apply ContinuousLinearMap.opNorm_le_bound _ (Nat.cast_nonneg _)
  intro v
  conv_lhs => rw [← spaceTimeDirections_sum v]
  rw [map_sum]
  apply (norm_sum_le _ _).trans
  calc
    (∑ j : Fin 4, ‖(fderiv ℝ (mixedDerivative c.val α) y)
        (timeSpaceCoord j v • spaceTimeDirection j)‖) ≤
        ∑ j : Fin 4, ‖v‖ * (c.bound (j :: α) : ℝ) := by
      apply Finset.sum_le_sum
      intro j _
      rw [map_smul, norm_smul]
      exact mul_le_mul (timeSpaceCoord_norm j v) (c.val_bound hc (j :: α) y)
        (norm_nonneg _) (norm_nonneg _)
    _ = (c.modulusBound α : ℝ) * ‖v‖ := by simp [modulusBound, Finset.sum_mul, mul_comm]
59
theorem lipschitz_bound {c : VelocityExpr} (hc : c.Valid) (α : List (Fin 4)) (x y : SpaceTime) :
    ‖mixedDerivative c.val α x - mixedDerivative c.val α y‖ ≤
      (c.modulusBound α : ℝ) * ‖x-y‖ := by
proof · 4 lines
  exact Convex.norm_image_sub_le_of_norm_fderiv_le
    (fun z (_ : z ∈ (univ : Set SpaceTime)) =>
      (mixedDerivative_smooth (smooth hc) α).differentiable (by simp) z)
    (fun z _ => fderiv_bound hc α z) (convex_univ) (mem_univ y) (mem_univ x)
67
def inputPrecision (c : VelocityExpr) (α : List (Fin 4)) (ε : ℚ) : ℕ :=
body · 1 line
  ⌈2 * (c.modulusBound α : ℚ) / ε⌉₊ + 1
70
theorem inputPrecision_spec (c : VelocityExpr) (α : List (Fin 4)) {ε : ℚ} (hε : 0 < ε) :
    (c.modulusBound α : ℝ) * errorTolerance (c.inputPrecision α ε) ≤ (ε : ℝ)/2 := by
proof · 13 lines
  have he : (0 : ℝ) < ε := by exact_mod_cast hε
  have hn : (2 * (c.modulusBound α : ℝ) / ε) ≤ (c.inputPrecision α ε : ℝ) := by
    have hh : 2 * (c.modulusBound α : ℚ) / ε ≤ (c.inputPrecision α ε : ℚ) :=
      (Nat.le_ceil _).trans (by simp [inputPrecision])
    exact_mod_cast hh
  have hp : (c.inputPrecision α ε : ℝ) ≤ (2 : ℝ)^(c.inputPrecision α ε) := by
    exact_mod_cast (Nat.lt_two_pow_self (n := c.inputPrecision α ε)).le
  have hle := hn.trans hp
  have h2 : (0 : ℝ) < 2^(c.inputPrecision α ε) := by positivity
  rw [errorTolerance, ← div_eq_mul_inv]
  apply (div_le_iff₀ h2).2
  have hh := (div_le_iff₀ he).1 hle
  linarith
86
def Ready (c : VelocityExpr) (α : List (Fin 4)) (q : RationalSpaceTime) (ε : ℚ) (n : ℕ) : Prop :=
body · 1 line
  ∀ k : Fin 3, (((c k).diffWord α).enclose q n).radius ≤ ε
89
instance (c : VelocityExpr) (α : List (Fin 4)) (q : RationalSpaceTime) (ε : ℚ) (n : ℕ) :
    Decidable (c.Ready α q ε n) := inferInstanceAs (Decidable (∀ _ : Fin 3, _ ≤ _))
92
theorem ready_exists {c : VelocityExpr} (hc : c.Valid) (α : List (Fin 4))
    (q : RationalSpaceTime) {ε : ℚ} (hε : 0 < ε) : ∃ n, c.Ready α q ε n := by
proof · 7 lines
  have he : (0 : ℝ) < ε := by exact_mod_cast hε
  have hh (k : Fin 3) : ∀ᶠ n in atTop,
      (((c k).diffWord α).enclose q n).radius ≤ ε := by
    have ht := (FieldExpr.enclose_converges (FieldExpr.valid_diffWord (hc k) α) q).2
    filter_upwards [ht.eventually_lt_const he] with n hn
    exact_mod_cast hn.le
  exact ((Filter.eventually_all).2 hh).exists
102
def evaluateRational (c : VelocityExpr) (hc : c.Valid) (α : List (Fin 4))
    (q : RationalSpaceTime) (ε : ℚ) (hε : 0 < ε) : RationalVector :=
body · 2 lines
  let n := Nat.find (ready_exists hc α q hε)
  fun k => (((c k).diffWord α).enclose q n).center
107
theorem evaluateRational_spec {c : VelocityExpr} (hc : c.Valid) (α : List (Fin 4))
    (q : RationalSpaceTime) (ε : ℚ) (hε : 0 < ε) :
    ‖mixedDerivative c.val α (rationalPoint q) -
      rationalVector (c.evaluateRational hc α q ε hε)‖ ≤ (ε : ℝ) := by
proof · 7 lines
  rw [← val_diffWord hc]
  apply (pi_norm_le_iff_of_nonneg (Rat.cast_nonneg.mpr hε.le)).2
  intro k
  have hb := FieldExpr.enclose_contains (FieldExpr.valid_diffWord (hc k) α) q
    (Nat.find (ready_exists hc α q hε))
  have hn := Nat.find_spec (ready_exists hc α q hε) k
  exact hb.trans (by exact_mod_cast hn)
119
def evaluate (c : VelocityExpr) (hc : c.Valid) (α : List (Fin 4))
    (a : ℕ → RationalSpaceTime) (ε : ℚ) (hε : 0 < ε) : RationalVector :=
body · 1 line
  c.evaluateRational hc α (a (c.inputPrecision α ε)) (ε/2) (by positivity)
123
theorem evaluate_spec {c : VelocityExpr} (hc : c.Valid) (α : List (Fin 4))
    (a : ℕ → RationalSpaceTime) {y : SpaceTime} (ha : IsFastName a y)
    (ε : ℚ) (hε : 0 < ε) :
    ‖mixedDerivative c.val α y - rationalVector (c.evaluate hc α a ε hε)‖ ≤ (ε : ℝ) := by
proof · 12 lines
  let q := a (c.inputPrecision α ε)
  calc
    _ ≤ ‖mixedDerivative c.val α y - mixedDerivative c.val α (rationalPoint q)‖ +
        ‖mixedDerivative c.val α (rationalPoint q) -
          rationalVector (c.evaluate hc α a ε hε)‖ := norm_sub_le_norm_sub_add_norm_sub _ _ _
    _ ≤ (ε : ℝ)/2 + (ε : ℝ)/2 := by
      apply add_le_add
      · exact (lipschitz_bound hc α y (rationalPoint q)).trans
          ((mul_le_mul_of_nonneg_left (ha _) (Nat.cast_nonneg _)).trans (inputPrecision_spec c α hε))
      · simpa only [evaluate, Rat.cast_div, Rat.cast_ofNat] using
          evaluateRational_spec hc α q (ε/2) (by positivity)
    _ = _ := by ring
142
structure FinitePrescription where
body · 2 lines
  code : VelocityExpr
  radius : ℚ
146
def compileShears (d : Input) : FinitePrescription := ⟨d.velocityExpr, d.tubeRadius⟩
148
structure HasEffectiveDerivatives (V : Velocity) (c : VelocityExpr) : Prop where
body · 6 lines
  valid : c.Valid
  evaluation : ∀ (α : List (Fin 4)) (a : ℕ → RationalSpaceTime) (y : SpaceTime),
    IsFastName a y → ∀ (ε : ℚ) (hε : 0 < ε),
      ‖mixedDerivative V α y - rationalVector (c.evaluate valid α a ε hε)‖ ≤ (ε : ℝ)
  bound : ∀ (α : List (Fin 4)) (y : SpaceTime),
    ‖mixedDerivative V α y‖ ≤ (c.bound α : ℝ)
156
theorem compileShears_effective {d : Input} (hd : ValidInput d) :
    HasEffectiveDerivatives d.realizingVelocity (compileShears d).code := by
proof · 8 lines
  have hc := velocityExpr_valid hd
  refine ⟨hc, ?_, ?_⟩
  · intro α a y hy ε hε
    simpa only [compileShears, ← velocityExpr_val hd] using
      VelocityExpr.evaluate_spec hc α a hy ε hε
  · intro α y
    simpa only [compileShears, ← velocityExpr_val hd] using
      VelocityExpr.val_bound hc α y