File size: 20,076 Bytes
7d9f027
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Mathlib
import OAI.Probability.SKGap.Localization.PreparationGrowth

namespace OAI

section
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators
open scoped BigOperators NNReal
open MeasureTheory ProbabilityTheory
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal
open MeasureTheory ProbabilityTheory
open scoped BigOperators NNReal ENNReal
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal
open MeasureTheory ProbabilityTheory
open scoped BigOperators Matrix Matrix.Norms.Elementwise
open scoped BigOperators
open MeasureTheory ProbabilityTheory
open scoped BigOperators Matrix Matrix.Norms.Elementwise
open scoped BigOperators
open scoped BigOperators NNReal ENNReal
open MeasureTheory Metric Set
open scoped BigOperators NNReal ENNReal
open MeasureTheory ProbabilityTheory Filter Set
open scoped BigOperators NNReal ENNReal Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Set
open scoped BigOperators Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Set
open scoped BigOperators Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Set
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators ENNReal NNReal
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Matrix
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators ENNReal NNReal
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal
open scoped BigOperators
open MeasureTheory ProbabilityTheory
open scoped BigOperators Matrix Matrix.Norms.Elementwise NNReal ENNReal
open scoped BigOperators
open Filter Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped BigOperators NNReal ENNReal Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal Topology
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter
open scoped ENNReal NNReal Topology BigOperators
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Set Matrix
open scoped BigOperators NNReal ENNReal Matrix.Norms.L2Operator
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open MeasureTheory ProbabilityTheory Filter Matrix
open scoped NNReal ENNReal BigOperators Topology Matrix Matrix.Norms.Elementwise
open Filter MeasureTheory ProbabilityTheory
open scoped Topology NNReal ENNReal
open Filter MeasureTheory ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter
open scoped Topology
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter ProbabilityTheory ContinuousLinearMap
open scoped Topology NNReal ENNReal
open Filter MeasureTheory ProbabilityTheory
open scoped Topology NNReal ENNReal
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open MeasureTheory Filter
open scoped BigOperators Topology
open Filter Set Metric
open scoped Topology RealInnerProductSpace
open scoped BigOperators
open ContinuousLinearMap
open scoped BigOperators
open ContinuousLinearMap
namespace SKGapCutoff
open RandomMatrix

lemma halfDiff_field {n : ℕ} (J : Interaction n) (x : Spin n) (i j : Fin n) :
    halfDiff i (fun y => field J y j) x = J j i := by
  simp only [field, halfDiff_sum, halfDiff_mul, halfDiff_const, halfDiff_spin,
    add_zero, mul_ite, mul_one, mul_zero]
  simp

lemma halfDiff_field_add {n : ℕ} (J : Interaction n) (g : VectorFields n)
    (x : Spin n) (i j : Fin n) :
    halfDiff i (fun y => field J y j+g y j) x = J j i+halfDiff i (fun y => g y j) x := by
  rw [← halfDiff_field J x i j]
  simp only [halfDiff]
  ring

lemma refreshSemigroup_eq_preparationFlow {n : ℕ} (J : Interaction n)
    (g : VectorFields n) (t : ℝ) :
    refreshSemigroup (fun x i => Real.tanh (field J x i+g x i)) t = preparationFlow J g t := by
  rw [refreshSemigroup, refreshGeneratorCLM_eq, smul_smul, mul_comm t]
  rfl

theorem preparation_poincare {n : ℕ} (J : Interaction n)
    (γ : ℝ) (hγ : 0 < γ) (hg : HasGap J γ)
    (g : VectorFields n) (C : ℝ) (hC : 0 ≤ C)
    (hJC : ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ) J‖ ≤ C)
    (hgc : ∀ x, ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ)
      (fun j i => halfDiff i (fun y => g y j) x)‖ ≤ 1)
    (f : Observables n) (t : ℝ) (ht : 0 ≤ t) :
    gibbsExpectation J (preparationFlow J g t (fun y => f y^2)) -
      (gibbsExpectation J (preparationFlow J g t f))^2 ≤
      (γ⁻¹*Real.exp (preparationGrowth (C+1)*t)+
        4*((Real.exp (preparationGrowth (C+1)*t)-1)/preparationGrowth (C+1)))*
        gibbsExpectation J (preparationFlow J g t (fun y => ∑ i, (halfDiff i f y)^2)) := by
  have hHC (x : Spin n) : ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ)
      (fun j i => halfDiff i (fun y => field J y j+g y j) x)‖ ≤ C+1 := by
    have he : Matrix.of (fun j i => halfDiff i (fun y => field J y j+g y j) x) =
        J+Matrix.of (fun j i => halfDiff i (fun y => g y j) x) := by
      ext j i
      exact halfDiff_field_add J g x i j
    change ‖Matrix.toEuclideanCLM (n := Fin n) (𝕜 := ℝ)
      (Matrix.of (fun j i => halfDiff i (fun y => field J y j+g y j) x))‖ ≤ C+1
    rw [he, map_add]
    exact (norm_add_le _ _).trans (add_le_add hJC (hgc x))
  have hh := preparation_poincare_field J γ hγ hg (fun x i => field J x i+g x i)
    (C+1) (by linarith) hHC f t ht
  simpa only [refreshSemigroup_eq_preparationFlow] using hh

end SKGapCutoff

open scoped Topology Interval
open MeasureTheory
namespace SKGapCutoff

lemma interval_hasFDerivAt_of_continuous
    {H E : Type*} [NormedAddCommGroup H] [NormedSpace ℝ H] [ProperSpace H]
    [NormedAddCommGroup E] [NormedSpace ℝ E] [CompleteSpace E]
    (F : H → ℝ → E) (F' : H → ℝ → H →L[ℝ] E)
    (hF : Continuous (Function.uncurry F)) (hF' : Continuous (Function.uncurry F'))
    (hd : ∀ x t, HasFDerivAt (fun y => F y t) (F' x t) x)
    (a b : ℝ) (x : H) :
    HasFDerivAt (fun y => ∫ t in a..b, F y t) (∫ t in a..b, F' x t) x := by
  obtain ⟨C,hC⟩ := ((isCompact_closedBall x 1).prod isCompact_uIcc).exists_bound_of_continuousOn
    hF'.continuousOn
  apply intervalIntegral.hasFDerivAt_integral_of_dominated_of_fderiv_le
    (s := Metric.closedBall x 1) (bound := fun _ => C) (Metric.closedBall_mem_nhds x (by norm_num))
  · filter_upwards [] with y
    exact (hF.comp (continuous_const.prodMk continuous_id)).aestronglyMeasurable
  · exact (hF.comp (continuous_const.prodMk continuous_id)).intervalIntegrable _ _
  · exact (hF'.comp (continuous_const.prodMk continuous_id)).aestronglyMeasurable
  · filter_upwards [] with t ht y hy
    exact hC (y,t) ⟨hy, Set.uIoc_subset_uIcc ht⟩
  · exact intervalIntegrable_const
  · filter_upwards [] with t _ y _
    exact hd y t

lemma algebra_exp_contDiff {A : Type*} [NormedRing A] [NormedAlgebra ℝ A]
    [CompleteSpace A] : ContDiff ℝ ⊤ (NormedSpace.exp : A → A) := by
  exact contDiff_iff_contDiffAt.mpr (fun x => (NormedSpace.exp_analytic (𝕂 := ℝ) x).contDiffAt)

lemma algebra_duhamel {A : Type*} [NormedRing A] [NormedAlgebra ℝ A]
    [CompleteSpace A] (X Y : A) (t : ℝ) :
    NormedSpace.exp (t • Y)-NormedSpace.exp (t • X) =
      ∫ s in 0..t, NormedSpace.exp (s • X)*(Y-X)*NormedSpace.exp ((t-s) • Y) := by
  let : NormedAlgebra ℚ A := NormedAlgebra.restrictScalars ℚ ℝ A
  have hc : Continuous (fun s : ℝ => NormedSpace.exp (s • X)*(Y-X)*NormedSpace.exp ((t-s) • Y)) := by
    fun_prop
  have hd (s : ℝ) : HasDerivAt
      (fun u : ℝ => NormedSpace.exp (u • X)*NormedSpace.exp ((t-u) • Y))
      (-(NormedSpace.exp (s • X)*(Y-X)*NormedSpace.exp ((t-s) • Y))) s := by
    have h1 := hasDerivAt_exp_smul_const X s
    have h2 := (hasDerivAt_exp_smul_const' Y (t-s)).scomp s
      ((hasDerivAt_id s).const_sub t)
    convert! h1.mul h2 using 1
    simp only [one_smul, neg_smul]
    noncomm_ring
  have hi := intervalIntegral.integral_eq_sub_of_hasDerivAt (fun s _ => hd s)
    (hc.neg.intervalIntegrable 0 t)
  simp only [intervalIntegral.integral_neg, sub_self, zero_smul, NormedSpace.exp_zero,
    mul_one, one_mul, sub_zero] at hi
  simpa only [neg_sub, neg_neg] using (congrArg Neg.neg hi).symm

lemma algebra_exp_smul_hasFDerivAt {A : Type*} [NormedRing A] [NormedAlgebra ℝ A]
    [CompleteSpace A] [ProperSpace A] (X : A) (t : ℝ) :
    HasFDerivAt (fun Y : A => NormedSpace.exp (t • Y))
      (∫ s in 0..t, ContinuousLinearMap.mulLeftRight ℝ A
        (NormedSpace.exp (s • X)) (NormedSpace.exp ((t-s) • X))) X := by
  let F : A → ℝ → A := fun Y s =>
    NormedSpace.exp (s • X)*(Y-X)*NormedSpace.exp ((t-s) • Y)
  have ce : ContDiff ℝ ⊤ (NormedSpace.exp : A → A) := algebra_exp_contDiff
  have hF : ContDiff ℝ ⊤ (Function.uncurry F) := by
    exact ((ce.comp (contDiff_snd.smul_const X)).mul (contDiff_fst.sub contDiff_const)).mul
      (ce.comp ((contDiff_const.sub contDiff_snd).smul contDiff_fst))
  have hd (Y : A) (s : ℝ) : DifferentiableAt ℝ (fun Z => F Z s) Y := by
    exact (hF.comp (contDiff_id.prodMk contDiff_const)).differentiable (by simp) |>.differentiableAt
  have hc' : Continuous (fun p : A × ℝ => fderiv ℝ (fun Y => F Y p.2) p.1) := by
    have hFF : ContDiff ℝ ⊤ (fun p : (A × ℝ) × A => F p.2 p.1.2) :=
      hF.comp (contDiff_snd.prodMk contDiff_fst.snd)
    exact Continuous.fderiv (f := fun p : A × ℝ => fun Y => F Y p.2)
      (g := fun p => p.1) hFF continuous_fst le_top
  have hint := interval_hasFDerivAt_of_continuous F
    (fun Y s => fderiv ℝ (fun Z => F Z s) Y) hF.continuous hc'
    (fun Y s => (hd Y s).hasFDerivAt) 0 t X
  have he (s : ℝ) : fderiv ℝ (fun Y => F Y s) X =
      ContinuousLinearMap.mulLeftRight ℝ A (NormedSpace.exp (s • X))
        (NormedSpace.exp ((t-s) • X)) := by
    have hdR : DifferentiableAt ℝ (fun Y : A => NormedSpace.exp ((t-s) • Y)) X :=
      (ce.comp (contDiff_id.const_smul (t-s))).differentiable (by simp) |>.differentiableAt
    have hh := (((hasFDerivAt_id X).sub_const X).mul' hdR.hasFDerivAt).const_mul
      (NormedSpace.exp (s • X))
    have hfun : (fun Y => NormedSpace.exp (s • X)*((Y-X)*NormedSpace.exp ((t-s) • Y))) =
        (fun Y => F Y s) := by funext Y; exact (mul_assoc _ _ _).symm
    simp only [Pi.mul_apply, id_eq] at hh
    rw [hfun] at hh
    rw [hh.fderiv]
    ext Z
    simp [ContinuousLinearMap.mulLeftRight_apply, mul_assoc]
  simp_rw [he] at hint
  have hfinal := hint.add_const (NormedSpace.exp (t • X))
  convert! hfinal using 1
  funext Y
  rw [← algebra_duhamel X Y t]
  exact (sub_add_cancel _ _).symm

lemma algebra_exp_curve_hasDerivAt {A : Type*} [NormedRing A] [NormedAlgebra ℝ A]
    [CompleteSpace A] [ProperSpace A] {L : ℝ → A} {L' : A} {θ : ℝ}
    (hL : HasDerivAt L L' θ) (t : ℝ) :
    HasDerivAt (fun u => NormedSpace.exp (t • L u))
      (∫ s in 0..t, NormedSpace.exp (s • L θ)*L'*NormedSpace.exp ((t-s) • L θ)) θ := by
  have hc : Continuous (fun s : ℝ => ContinuousLinearMap.mulLeftRight ℝ A
      (NormedSpace.exp (s • L θ)) (NormedSpace.exp ((t-s) • L θ))) := by
    have ce : Continuous (NormedSpace.exp : A → A) := algebra_exp_contDiff.continuous
    exact ((ContinuousLinearMap.mulLeftRight ℝ A).continuous.comp
      (ce.comp (continuous_id.smul continuous_const))).clm_apply
        (ce.comp ((continuous_const.sub continuous_id).smul continuous_const))
  have hh := (algebra_exp_smul_hasFDerivAt (L θ) t).comp_hasDerivAt θ hL
  rw [ContinuousLinearMap.intervalIntegral_apply (hc.intervalIntegrable _ _) L'] at hh
  exact hh

noncomputable def meanGeneratorCLM {n : ℕ} (m : VectorFields n) :
    Observables n →L[ℝ] Observables n := refreshGeneratorCLM m-refreshGeneratorCLM 0

lemma meanGeneratorCLM_apply {n : ℕ} (m : VectorFields n) (f : Observables n) (x : Spin n) :
    meanGeneratorCLM m f x = ∑ i, m x i*halfDiff i f x := by
  change (∑ i, (m x i-spin x i)*halfDiff i f x)-
    (∑ i, (0-spin x i)*halfDiff i f x) = _
  rw [← Finset.sum_sub_distrib]
  apply Finset.sum_congr rfl
  intro i _
  ring

noncomputable def meanGeneratorMapLM (n : ℕ) :
    VectorFields n →ₗ[ℝ] (Observables n →L[ℝ] Observables n) where
  toFun := meanGeneratorCLM
  map_add' p q := by
    ext f x
    simp [meanGeneratorCLM_apply, add_mul, Finset.sum_add_distrib]
  map_smul' a p := by
    ext f x
    simp [meanGeneratorCLM_apply, mul_assoc, Finset.mul_sum]

noncomputable def meanGeneratorMap (n : ℕ) :
    VectorFields n →L[ℝ] (Observables n →L[ℝ] Observables n) :=
  (meanGeneratorMapLM n).toContinuousLinearMap

lemma meanGeneratorMap_apply {n : ℕ} (m : VectorFields n) :
    meanGeneratorMap n m = meanGeneratorCLM m := rfl

lemma refreshGeneratorCLM_eq_mean {n : ℕ} (m : VectorFields n) :
    refreshGeneratorCLM m = meanGeneratorMap n m + refreshGeneratorCLM 0 := by
  simp [meanGeneratorMap_apply, meanGeneratorCLM]

lemma preparation_mean_hasDerivAt {n : ℕ} (H v : VectorFields n) (θ : ℝ) :
    HasDerivAt (fun u : ℝ => (fun x i => Real.tanh (H x i+u*v x i) : VectorFields n))
      (fun x i => scalarVariance (H x i+θ*v x i)*v x i) θ := by
  apply hasDerivAt_pi.mpr
  intro x
  apply hasDerivAt_pi.mpr
  intro i
  convert! (tanh_hasDerivAt (H x i+θ*v x i)).comp θ
    (((hasDerivAt_id θ).mul_const (v x i)).const_add (H x i)) using 1
  simp

lemma preparation_generator_hasDerivAt {n : ℕ} (H v : VectorFields n) (θ : ℝ) :
    HasDerivAt (fun u : ℝ => refreshGeneratorCLM (fun x i => Real.tanh (H x i+u*v x i)))
      (meanGeneratorCLM (fun x i => scalarVariance (H x i+θ*v x i)*v x i)) θ := by
  have hh := ((meanGeneratorMap n).hasFDerivAt.comp_hasDerivAt θ
    (preparation_mean_hasDerivAt H v θ)).add_const (refreshGeneratorCLM 0)
  simp only [meanGeneratorMap_apply] at hh
  convert! hh using 1
  funext u
  exact refreshGeneratorCLM_eq_mean _

lemma preparation_semigroup_hasDerivAt {n : ℕ} (H v : VectorFields n) (θ h : ℝ) :
    HasDerivAt (fun u : ℝ => refreshSemigroup (fun x i => Real.tanh (H x i+u*v x i)) h)
      (∫ s in 0..h,
        refreshSemigroup (fun x i => Real.tanh (H x i+θ*v x i)) s *
        meanGeneratorCLM (fun x i => scalarVariance (H x i+θ*v x i)*v x i) *
        refreshSemigroup (fun x i => Real.tanh (H x i+θ*v x i)) (h-s)) θ := by
  exact algebra_exp_curve_hasDerivAt (preparation_generator_hasDerivAt H v θ) h

theorem preparation_expectation_hasDerivAt {n : ℕ} (J : Interaction n)
    (g v : VectorFields n) (θ h : ℝ) (f : Observables n) :
    HasDerivAt (fun u : ℝ => gibbsExpectation J
      (preparationFlow J (fun x i => g x i+u*v x i) h f))
      (∫ s in 0..h, gibbsExpectation J
        (preparationFlow J (fun x i => g x i+θ*v x i) s
          (fun x => ∑ i, scalarVariance (field J x i+g x i+θ*v x i)*v x i*
            halfDiff i (preparationFlow J (fun x i => g x i+θ*v x i) (h-s) f) x))) θ := by
  let m : VectorFields n := fun x i => Real.tanh (field J x i+g x i+θ*v x i)
  let B := meanGeneratorCLM (fun x i => scalarVariance (field J x i+g x i+θ*v x i)*v x i)
  have hc : Continuous (fun s : ℝ => refreshSemigroup m s*B*refreshSemigroup m (h-s)) := by
    exact ((refreshSemigroup_continuous m).mul continuous_const).mul
      ((refreshSemigroup_continuous m).comp (continuous_const.sub continuous_id))
  have hd := (preparation_semigroup_hasDerivAt (fun x i => field J x i+g x i) v θ h).clm_apply
    (hasDerivAt_const θ f)
  have hh := (expectationCLM J).hasFDerivAt.comp_hasDerivAt θ hd
  simp only [map_zero, add_zero] at hh
  change HasDerivAt _ (expectationCLM J ((∫ s in 0..h,
    refreshSemigroup m s*B*refreshSemigroup m (h-s)) f)) θ at hh
  rw [ContinuousLinearMap.intervalIntegral_apply (hc.intervalIntegrable _ _) f,
    ← (expectationCLM J).intervalIntegral_comp_comm
      ((hc.clm_apply continuous_const).intervalIntegrable _ _)] at hh
  have he (u k : ℝ) : refreshSemigroup
      (fun x i => Real.tanh (field J x i+g x i+u*v x i)) k =
      preparationFlow J (fun x i => g x i+u*v x i) k := by
    convert! refreshSemigroup_eq_preparationFlow J (fun x i => g x i+u*v x i) k using 1
    congr 1
    funext x i
    congr 1
    ring
  simp only [mul_apply_eq_comp, m, B, he] at hh
  have hB (q : Observables n) :
      meanGeneratorCLM (fun x i => scalarVariance (field J x i+g x i+θ*v x i)*v x i) q =
        fun x => ∑ i, scalarVariance (field J x i+g x i+θ*v x i)*v x i*halfDiff i q x := by
    funext x
    exact meanGeneratorCLM_apply _ q x
  simp_rw [hB] at hh
  exact hh

end SKGapCutoff

end

end OAI