File size: 18,598 Bytes
caec230
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import OAI.MathematicalPhysics.Transonic.Core

namespace OAI

section

/-! Exact integer polynomial evaluation and interval control for the regular shooting ODE. -/
namespace SepticProfile.IntegerPolynomial
open Set
abbrev Poly := List ℤ

def add : Poly → Poly → Poly
  | [],q => q
  | p,[] => p
  | a::p,b::q => (a+b)::add p q

def scale (a : ℤ) : Poly → Poly
  | [] => []
  | b::p => (a*b)::scale a p
def sub (p q : Poly) : Poly := add p (scale (-1) q)
def mul : Poly → Poly → Poly
  | [],_ => []
  | a::p,q => add (scale a q) (0::mul p q)
def der : Poly → Poly
  | [] => []
  | _::p => add p (0::der p)
def sq (p : Poly) : Poly := mul p p

def eval : Poly → ℝ → ℝ
  | [],_ => 0
  | a::p,x => a+x*eval p x

def norm : Poly → ℤ
  | [] => 0
  | a::p => |a|+norm p

def head (p : Poly) : ℤ := p.headD 0

def tailNorm (p : Poly) : ℤ := norm p.tail

def Positive (p : Poly) : Prop := tailNorm p<head p
instance (p : Poly) : Decidable (Positive p) := inferInstanceAs (Decidable (tailNorm p<head p))

lemma eval_add (p q : Poly) (x : ℝ) : eval (add p q) x=eval p x+eval q x := by
  induction p generalizing q with
  | nil => simp [add,eval]
  | cons a p ih =>
    cases q with
    | nil => simp [add,eval]
    | cons b q => simp [add,eval,ih]; ring

lemma eval_scale (a : ℤ) (p : Poly) (x : ℝ) : eval (scale a p) x=a*eval p x := by
  induction p with
  | nil => simp [scale,eval]
  | cons b p ih => simp only [scale,eval,Int.cast_mul,ih]; ring

lemma eval_sub (p q : Poly) (x : ℝ) : eval (sub p q) x=eval p x-eval q x := by
  simp [sub,eval_add,eval_scale,sub_eq_add_neg]

lemma eval_mul (p q : Poly) (x : ℝ) : eval (mul p q) x=eval p x*eval q x := by
  induction p with
  | nil => simp [mul,eval]
  | cons a p ih => rw [mul,eval_add,eval_scale,eval,ih,eval]; simp only [Int.cast_zero]; ring

lemma eval_sq (p : Poly) (x : ℝ) : eval (sq p) x=(eval p x)^2 := by
  rw [sq,eval_mul,pow_two]

lemma hasDerivAt_eval (p : Poly) (x : ℝ) : HasDerivAt (eval p) (eval (der p) x) x := by
  induction p with
  | nil => simpa only [eval,der] using hasDerivAt_const x (0:ℝ)
  | cons a p ih =>
    have hh := (hasDerivAt_const x (a : ℝ)).add ((hasDerivAt_id x).mul ih)
    change HasDerivAt (fun y => (a : ℝ)+y*eval p y)
      (0+(1*eval p x+x*eval (der p) x)) x at hh
    change HasDerivAt (fun y => (a : ℝ)+y*eval p y)
      (eval (add p (0::der p)) x) x
    simpa only [zero_add,one_mul,eval_add,eval,Int.cast_zero] using hh

lemma contDiff_eval (p : Poly) {n : WithTop ℕ∞} : ContDiff ℝ n (eval p) := by
  induction p with
  | nil => exact contDiff_const
  | cons a p ih => exact contDiff_const.add (contDiff_id.mul ih)

lemma norm_nonneg (p : Poly) : 0≤norm p := by
  induction p <;> simp_all [norm]; positivity

lemma eval_abs_le (p : Poly) {x : ℝ} (hx : x ∈ Icc 0 1) : |eval p x|≤norm p := by
  induction p with
  | nil => simp [eval,norm]
  | cons a p ih =>
    rw [eval,norm,Int.cast_add,Int.cast_abs]
    calc
      _ ≤ |(a : ℝ)|+|x*eval p x| := abs_add_le _ _
      _ = |(a : ℝ)|+x*|eval p x| := by rw [abs_mul,abs_of_nonneg hx.1]
      _ ≤ |(a : ℝ)|+|eval p x| := add_le_add_right (mul_le_of_le_one_left (abs_nonneg _) hx.2) _
      _ ≤ _ := add_le_add_right ih _

lemma eval_deviation (p : Poly) {x : ℝ} (hx : x ∈ Icc 0 1) :
    |eval p x-head p|≤tailNorm p := by
  cases p with
  | nil => simp [eval,head,tailNorm,norm]
  | cons a p =>
    simp only [head,List.headD_cons,eval,add_sub_cancel_left,tailNorm,List.tail_cons]
    rw [abs_mul,abs_of_nonneg hx.1]
    have h := eval_abs_le p hx
    exact (mul_le_of_le_one_left (abs_nonneg _) hx.2).trans h

lemma eval_pos {p : Poly} (hp : Positive p) {x : ℝ} (hx : x ∈ Icc 0 1) : 0<eval p x := by
  have hd := (abs_le.mp (eval_deviation p hx)).1
  have hp' : (tailNorm p : ℝ)<head p := by exact_mod_cast hp
  linarith

lemma eval_bounds (p : Poly) {x : ℝ} (hx : x ∈ Icc 0 1) :
    ((head p-tailNorm p : ℤ) : ℝ)≤eval p x ∧ eval p x≤((head p+tailNorm p : ℤ) : ℝ) := by
  have hh := abs_le.mp (eval_deviation p hx)
  push_cast
  constructor <;> linarith

end SepticProfile.IntegerPolynomial

namespace SepticProfile.IntegerPolynomial
open Set

/-- Numerator after substituting `z=(Z-Hs)/R`, `u=P(s)/Q` in the
exact normalized source ODE. All arithmetic in certificates is integral. -/
def residual (S SN K KN R Q Z H : ℤ) (p : Poly) : Poly :=
  let z : Poly := [Z,-H]
  let v := sub [Q^2] (sq p)
  add (scale (5*K*Q) (mul (mul (mul z (sub [S*R^2] (scale SN (sq z)))) v) (der p)))
    (scale (H*S*R) (mul (sub [5*Q^2] (scale 3 (sq p)))
      (add (scale KN (mul z v)) (scale (3*K) (sub (scale (Q^2) z) (scale (R*Q) p))))))

def denom (sigma z u : ℝ) : ℝ := z*(1-sigma*z^2)*(1-u^2)
noncomputable def numer (kappa z u : ℝ) : ℝ := (1-3*u^2/5)*(kappa*z*(1-u^2)+3*(z-u))

noncomputable def coordinate (R Z H : ℤ) (s : ℝ) : ℝ := (Z-H*s)/R

noncomputable def velocity (Q : ℤ) (p : Poly) (s : ℝ) : ℝ := eval p s/Q

lemma eval_singleton (a : ℤ) (x : ℝ) : eval [a] x=a := by simp [eval]
lemma eval_linear (a b : ℤ) (x : ℝ) : eval [a,b] x=a+b*x := by simp [eval]; ring

lemma eval_residual (S SN K KN R Q Z H : ℤ) (p : Poly) (s : ℝ)
    (hS : S≠0) (hK : K≠0) (hR : R≠0) (hQ : Q≠0) :
    eval (residual S SN K KN R Q Z H p) s =
      5*K*S*R^3*Q^4 *
       (denom (SN/S) (coordinate R Z H s) (velocity Q p s)*(eval (der p) s/Q)
        +(H/R)*numer (KN/K) (coordinate R Z H s) (velocity Q p s)) := by
  have hs : (S : ℝ)≠0 := by exact_mod_cast hS
  have hk : (K : ℝ)≠0 := by exact_mod_cast hK
  have hr : (R : ℝ)≠0 := by exact_mod_cast hR
  have hq : (Q : ℝ)≠0 := by exact_mod_cast hQ
  simp only [residual,eval_add,eval_sub,eval_scale,eval_mul,eval_sq,
    eval_singleton,eval_linear,Int.cast_mul,Int.cast_pow,Int.cast_ofNat,Int.cast_neg,
    denom,numer,coordinate,velocity]
  field_simp
  ring

lemma hasDerivAt_velocity (Q : ℤ) (p : Poly) (s : ℝ) :
    HasDerivAt (velocity Q p) (eval (der p) s/Q) s :=
  (hasDerivAt_eval p s).div_const Q

lemma residual_strict {S SN K KN R Q Z H : ℤ} (hS : 0<S) (hK : 0<K)
    (hR : 0<R) (hQ : 0<Q) {p : Poly} (hp : Positive (residual S SN K KN R Q Z H p))
    {s : ℝ} (hs : s ∈ Icc 0 1) :
    0 < denom (SN/S) (coordinate R Z H s) (velocity Q p s)*(eval (der p) s/Q)
      +(H/R)*numer (KN/K) (coordinate R Z H s) (velocity Q p s) := by
  have h := eval_pos hp hs
  rw [eval_residual S SN K KN R Q Z H p s (ne_of_gt hS) (ne_of_gt hK)
    (ne_of_gt hR) (ne_of_gt hQ)] at h
  have hs' : (0:ℝ)<S := by exact_mod_cast hS
  have hk' : (0:ℝ)<K := by exact_mod_cast hK
  have hr' : (0:ℝ)<R := by exact_mod_cast hR
  have hq' : (0:ℝ)<Q := by exact_mod_cast hQ
  exact (mul_pos_iff_of_pos_left (by positivity)).mp h

lemma residual_negative {S SN K KN R Q Z H : ℤ} (hS : 0<S) (hK : 0<K)
    (hR : 0<R) (hQ : 0<Q) {p : Poly}
    (hp : Positive (scale (-1) (residual S SN K KN R Q Z H p)))
    {s : ℝ} (hs : s ∈ Icc 0 1) :
    denom (SN/S) (coordinate R Z H s) (velocity Q p s)*(eval (der p) s/Q)
      +(H/R)*numer (KN/K) (coordinate R Z H s) (velocity Q p s) < 0 := by
  have h := eval_pos hp hs
  rw [eval_scale,eval_residual S SN K KN R Q Z H p s (ne_of_gt hS)
    (ne_of_gt hK) (ne_of_gt hR) (ne_of_gt hQ)] at h
  have hs' : (0:ℝ)<S := by exact_mod_cast hS
  have hk' : (0:ℝ)<K := by exact_mod_cast hK
  have hr' : (0:ℝ)<R := by exact_mod_cast hR
  have hq' : (0:ℝ)<Q := by exact_mod_cast hQ
  have hn : ((5:ℝ)*K*S*R^3*Q^4) *
        (denom (SN/S) (coordinate R Z H s) (velocity Q p s)*(eval (der p) s/Q)
        +(H/R)*numer (KN/K) (coordinate R Z H s) (velocity Q p s))<0 := by
    norm_num only [Int.cast_neg,Int.cast_one,neg_one_mul] at h
    linarith
  exact neg_pos.mp ((mul_pos_iff_of_pos_left (show (0:ℝ)<5*K*S*R^3*Q^4 by positivity)).mp (by nlinarith : 0<(5:ℝ)*K*S*R^3*Q^4 * -_))
end SepticProfile.IntegerPolynomial

namespace SepticProfile.IntegerPolynomial

@[simp] lemma eval_zero (p : Poly) : eval p 0=head p := by
  cases p <;> simp [eval,head]

@[simp] lemma eval_one (p : Poly) : eval p 1=(p.sum : ℤ) := by
  induction p <;> simp_all [eval]

structure Step where
  Z : ℤ
  H : ℤ
  lo : Poly
  hi : Poly

def Range (Q : ℤ) (p : Poly) : Prop :=
  0<head p-tailNorm p ∧ 1000*(head p+tailNorm p)<999*Q
instance (Q : ℤ) (p : Poly) : Decidable (Range Q p) :=
  inferInstanceAs (Decidable (_ ∧ _))

def Valid (S SN K KN R Q : ℤ) (a : Step) : Prop :=
  0<a.H ∧ 9*R≤10*(a.Z-a.H) ∧ 100*a.Z≤99*R ∧
  Range Q a.lo ∧ Range Q a.hi ∧ head a.lo≤head a.hi ∧
  Positive (scale (-1) (residual S SN K KN R Q a.Z a.H a.lo)) ∧
  Positive (residual S SN K KN R Q a.Z a.H a.hi)
instance (S SN K KN R Q : ℤ) (a : Step) : Decidable (Valid S SN K KN R Q a) :=
  inferInstanceAs (Decidable (_ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _ ∧ _))

lemma range_velocity {Q : ℤ} (hq : 0<Q) {p : Poly} (hp : Range Q p) {s : ℝ}
    (hs : s ∈ Set.Icc 0 1) : velocity Q p s ∈ Set.Icc (0:ℝ) (999/1000) := by
  have hq' : (0:ℝ)<Q := by exact_mod_cast hq
  have hr := eval_bounds p hs
  have hlo : (0:ℝ)<((head p-tailNorm p : ℤ) : ℝ) := by exact_mod_cast hp.1
  have hhi : (1000:ℝ)*((head p+tailNorm p : ℤ) : ℝ)<999*Q := by exact_mod_cast hp.2
  constructor
  · exact div_nonneg (le_of_lt (hlo.trans_le hr.1)) hq'.le
  · apply (div_le_iff₀ hq').mpr
    nlinarith

lemma coordinate_bounds {R Z H : ℤ} (hr : 0<R) (hh : 0<H)
    (hstart : 100*Z≤99*R) (hend : 9*R≤10*(Z-H)) {s : ℝ}
    (hs : s ∈ Set.Icc 0 1) : coordinate R Z H s ∈ Set.Icc (9/10:ℝ) (99/100) := by
  have hr' : (0:ℝ)<R := by exact_mod_cast hr
  have hh' : (0:ℝ)<H := by exact_mod_cast hh
  have ha : (100:ℝ)*Z≤99*R := by exact_mod_cast hstart
  have hb : (9:ℝ)*R≤10*(Z-H) := by exact_mod_cast hend
  unfold coordinate
  constructor
  · apply (le_div_iff₀ hr').mpr
    nlinarith [mul_le_of_le_one_right hh'.le hs.2]
  · apply (div_le_iff₀ hr').mpr
    nlinarith [mul_nonneg hh'.le hs.1]

lemma denom_pos {sigma z u : ℝ} (hsigma : sigma ∈ Set.Icc (0:ℝ) 1)
    (hz : z ∈ Set.Icc (9/10:ℝ) (99/100)) (hu : u ∈ Set.Icc (0:ℝ) (999/1000)) :
    0<denom sigma z u := by
  have hz0 : 0<z := by linarith [hz.1]
  have hz1 : z^2<1 := by nlinarith [hz.2]
  have hu1 : u^2<1 := by nlinarith [hu.1,hu.2]
  have hsz : sigma*z^2≤z^2 := mul_le_of_le_one_left (sq_nonneg _) hsigma.2
  exact mul_pos (mul_pos hz0 (by linarith)) (by linarith)
end SepticProfile.IntegerPolynomial


namespace SepticProfile.IntegerPolynomial
open Set
open RegularContinuation

noncomputable def stepField (sigma kappa : ℝ) (R Z H : ℤ) (s w : ℝ) : ℝ :=
  -(H/R)*numer kappa (coordinate R Z H s) (clamp 0 (999/1000) w)/
    denom sigma (coordinate R Z H s) (clamp 0 (999/1000) w)

/-- Exact signed polynomial residuals control the actual clamped ODE, not an
approximate trajectory. The comparison proves the clamp inactive on this step. -/
theorem step_control {S SN K KN R Q : ℤ} (hs : 0<S) (hk : 0<K) (hr : 0<R)
    (hq : 0<Q) (hsigma : ((SN:ℝ)/S) ∈ Icc (0:ℝ) 1) {a : Step}
    (ha : Valid S SN K KN R Q a) {w : ℝ → ℝ}
    (hwc : ContinuousOn w (Icc 0 1))
    (hwd : ∀ s ∈ Icc (0:ℝ) 1, HasDerivWithinAt w
      (stepField (SN/S) (KN/K) R a.Z a.H s (w s)) (Icc 0 1) s)
    (hw0 : w 0 ∈ Icc ((head a.lo:ℝ)/Q) ((head a.hi:ℝ)/Q)) :
    ∀ s ∈ Icc (0:ℝ) 1, w s ∈ Icc (velocity Q a.lo s) (velocity Q a.hi s) := by
  obtain ⟨hh,hend,hstart,hlo,hhi,_,hlor,_hhir⟩ := ha
  have hright (s : ℝ) (ht : s ∈ Ico (0:ℝ) 1) : HasDerivWithinAt w
      (stepField (SN/S) (KN/K) R a.Z a.H s (w s)) (Ici s) s := by
    apply (hwd s ⟨ht.1,ht.2.le⟩).mono_of_mem_nhdsWithin
    exact Filter.mem_of_superset (Icc_mem_nhdsGE ht.2) (Icc_subset_Icc_left ht.1)
  have hupper : ∀ s ∈ Icc (0:ℝ) 1, w s≤velocity Q a.hi s := by
    apply image_le_of_deriv_right_lt_deriv_boundary' hwc hright
      (B' := fun s => eval (der a.hi) s/Q) (by simpa [velocity] using hw0.2)
      (fun s _ => (hasDerivAt_velocity Q a.hi s).continuousAt.continuousWithinAt)
      (fun s _ => (hasDerivAt_velocity Q a.hi s).hasDerivWithinAt)
    intro s ht he
    have he2 : s ∈ Icc (0:ℝ) 1 := ⟨ht.1,ht.2.le⟩
    have hrange := range_velocity hq hhi he2
    have hz := coordinate_bounds hr hh hstart hend he2
    have hd := denom_pos hsigma hz hrange
    have heq := clamp_eq hrange
    have hhres := residual_strict hs hk hr hq _hhir he2
    dsimp only [stepField]
    rw [he,heq]
    apply (div_lt_iff₀ hd).mpr
    nlinarith
  have hlower : ∀ s ∈ Icc (0:ℝ) 1, velocity Q a.lo s≤w s := by
    have hhcmp := image_le_of_deriv_right_lt_deriv_boundary' hwc.neg
      (fun s hs => (hright s hs).neg) (B := fun s => -velocity Q a.lo s)
      (B' := fun s => -(eval (der a.lo) s/Q))
      (by simpa [velocity] using neg_le_neg hw0.1)
      (fun s _ => (hasDerivAt_velocity Q a.lo s).neg.continuousAt.continuousWithinAt)
      (fun s _ => (hasDerivAt_velocity Q a.lo s).neg.hasDerivWithinAt)
    have hhcmp' : ∀ s ∈ Icc (0:ℝ) 1, -w s≤ -velocity Q a.lo s := by
      apply hhcmp
      intro s ht he
      have he' : w s=velocity Q a.lo s := neg_inj.mp he
      have he2 : s ∈ Icc (0:ℝ) 1 := ⟨ht.1,ht.2.le⟩
      have hrange := range_velocity hq hlo he2
      have hz := coordinate_bounds hr hh hstart hend he2
      have hd := denom_pos hsigma hz hrange
      have heq := clamp_eq hrange
      have hlres := residual_negative hs hk hr hq hlor he2
      apply neg_lt_neg
      dsimp only [stepField]
      rw [he',heq]
      apply (lt_div_iff₀ hd).mpr
      nlinarith
    intro s hs
    linarith [hhcmp' s hs]
  intro s hs
  exact ⟨hlower s hs,hupper s hs⟩

end SepticProfile.IntegerPolynomial

namespace SepticProfile.IntegerPolynomial
open Set RegularContinuation

noncomputable def globalField (sigma kappa : ℝ) (t w : ℝ) : ℝ :=
  -numer kappa (99/100-t) (clamp 0 (999/1000) w)/
    denom sigma (99/100-t) (clamp 0 (999/1000) w)
noncomputable def stepTime (R Z H : ℤ) (s : ℝ) : ℝ := 99/100-coordinate R Z H s
noncomputable def startTime (R : ℤ) (a : Step) : ℝ := 99/100-a.Z/R
noncomputable def stopTime (R : ℤ) (a : Step) : ℝ := 99/100-((a.Z-a.H : ℤ):ℝ)/R

lemma hasDerivAt_stepTime (R Z H : ℤ) (s : ℝ) : HasDerivAt (stepTime R Z H) (H/R) s := by
  have hh := (hasDerivAt_const s (99/100:ℝ)).sub
    (((hasDerivAt_const s (Z:ℝ)).sub ((hasDerivAt_id s).const_mul (H:ℝ))).div_const R)
  change HasDerivAt (stepTime R Z H) (0-(0-(H:ℝ)*1)/R) s at hh
  simpa only [mul_one,zero_sub,neg_div,neg_neg] using hh

lemma stepTime_mem {R Z H : ℤ} (hr : 0<R) (hh : 0<H)
    (hstart : 100*Z≤99*R) (hend : 9*R≤10*(Z-H)) {s : ℝ}
    (hs : s ∈ Icc (0:ℝ) 1) : stepTime R Z H s ∈ Icc (0:ℝ) (9/100) := by
  have hz := coordinate_bounds hr hh hstart hend hs
  unfold stepTime
  constructor <;> linarith [hz.1,hz.2]

lemma localized_derivative {sigma kappa : ℝ} {R Z H : ℤ} (hr : 0<R) (hh : 0<H)
    (hstart : 100*Z≤99*R) (hend : 9*R≤10*(Z-H)) {u : ℝ → ℝ}
    (hud : ∀ t ∈ Icc (0:ℝ) (9/100), HasDerivWithinAt u (globalField sigma kappa t (u t))
      (Icc 0 (9/100)) t) (s : ℝ) (hs : s ∈ Icc (0:ℝ) 1) :
    HasDerivWithinAt (u ∘ stepTime R Z H)
      (stepField sigma kappa R Z H s ((u ∘ stepTime R Z H) s)) (Icc 0 1) s := by
  have hm := stepTime_mem hr hh hstart hend hs
  have hd := (hud _ hm).comp s (hasDerivAt_stepTime R Z H s).hasDerivWithinAt
    (fun t ht => stepTime_mem hr hh hstart hend ht)
  have he : globalField sigma kappa (stepTime R Z H s) (u (stepTime R Z H s)) * (H/R) =
      stepField sigma kappa R Z H s ((u ∘ stepTime R Z H) s) := by
    dsimp [globalField,stepField,stepTime]
    simp only [sub_sub_cancel]
    ring
  rw [← he]
  exact hd

/-- Each integer certificate propagates a rigorous interval for the same
single globally constructed clamped solution. No approximate flow is used. -/
theorem global_step_endpoint {S SN K KN R Q : ℤ} (hs : 0<S) (hk : 0<K) (hr : 0<R)
    (hq : 0<Q) (hsigma : ((SN:ℝ)/S) ∈ Icc (0:ℝ) 1) {a : Step}
    (ha : Valid S SN K KN R Q a) {u : ℝ → ℝ}
    (hud : ∀ t ∈ Icc (0:ℝ) (9/100), HasDerivWithinAt u (globalField (SN/S) (KN/K) t (u t))
      (Icc 0 (9/100)) t)
    (hu0 : u (startTime R a) ∈ Icc ((head a.lo:ℝ)/Q) ((head a.hi:ℝ)/Q)) :
    u (stopTime R a) ∈ Icc ((a.lo.sum:ℝ)/Q) ((a.hi.sum:ℝ)/Q) := by
  have hd := localized_derivative hr ha.1 ha.2.2.1 ha.2.1 hud
  have hc : ContinuousOn (u ∘ stepTime R a.Z a.H) (Icc 0 1) :=
    fun s hs => (hd s hs).continuousWithinAt
  have hzero : (u ∘ stepTime R a.Z a.H) 0=u (startTime R a) := by
    simp [stepTime,coordinate,startTime]
  have hb := step_control hs hk hr hq hsigma ha hc hd (hzero.symm ▸ hu0) 1 (by norm_num)
  simpa [stepTime,coordinate,stopTime,velocity] using hb

lemma join_bound {R Q : ℤ} (hq : 0<Q) {a b : Step} {u : ℝ → ℝ}
    (hjoin : a.Z-a.H=b.Z ∧ head b.lo≤a.lo.sum ∧ a.hi.sum≤head b.hi)
    (hb : u (stopTime R a) ∈ Icc ((a.lo.sum:ℝ)/Q) ((a.hi.sum:ℝ)/Q)) :
    u (startTime R b) ∈ Icc ((head b.lo:ℝ)/Q) ((head b.hi:ℝ)/Q) := by
  have he : stopTime R a=startTime R b := by rw [stopTime,startTime,hjoin.1]
  rw [he] at hb
  have hq' : (0:ℝ)≤Q := le_of_lt (by exact_mod_cast hq)
  constructor
  · exact (div_le_div_of_nonneg_right (by exact_mod_cast hjoin.2.1) hq').trans hb.1
  · exact hb.2.trans (div_le_div_of_nonneg_right (by exact_mod_cast hjoin.2.2) hq')
end SepticProfile.IntegerPolynomial

namespace SepticProfile.IntegerPolynomial
open Set RegularContinuation

lemma regular_field_contDiffOn (sigma kappa : ℝ) (hs : sigma ∈ Icc (0:ℝ) 1) :
    ContDiffOn ℝ 1 (fun p : ℝ×ℝ => -numer kappa (99/100-p.1) p.2 /
      denom sigma (99/100-p.1) p.2) (Icc (0:ℝ) (9/100) ×ˢ Icc (0:ℝ) (999/1000)) := by
  apply ContDiffOn.div
  · unfold numer
    fun_prop
  · unfold denom
    fun_prop
  · intro p hp
    apply ne_of_gt (denom_pos hs _ hp.2)
    constructor <;> linarith [hp.1.1,hp.1.2]

/-- A single exact clamped trajectory exists on the entire regular shooting
interval. Step certificates will certify its value, rather than assume a flow. -/
theorem exists_global_clamped_solution (sigma kappa : ℝ) (hs : sigma ∈ Icc (0:ℝ) 1)
    (u0 : ℝ) : ∃ u : ℝ → ℝ, u 0=u0 ∧ ContinuousOn u (Icc (0:ℝ) (9/100)) ∧
      ∀ t ∈ Icc (0:ℝ) (9/100), HasDerivWithinAt u (globalField sigma kappa t (u t))
        (Icc 0 (9/100)) t := by
  exact exists_clamped_solution (by norm_num) (by norm_num)
    (regular_field_contDiffOn sigma kappa hs) u0
end SepticProfile.IntegerPolynomial


end

end OAI