File size: 20,508 Bytes
2fca478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
428
429
430
import OAI.Geometry.ProjectionBodies.SupportMeasures

namespace OAI

noncomputable section
open Set Filter Topology
namespace PettyProjection.Spherical

def softAbs (ε t : ℝ) : ℝ := Real.sqrt (t^2+ε)
def softSign (ε t : ℝ) : ℝ := t/softAbs ε t
def softSlope (ε t : ℝ) : ℝ := ε/(softAbs ε t)^3

lemma softAbs_pos {ε : ℝ} (hε : 0 < ε) (t : ℝ) : 0 < softAbs ε t :=
  Real.sqrt_pos.mpr (by positivity)

lemma softAbs_sq {ε : ℝ} (hε : 0 < ε) (t : ℝ) : (softAbs ε t)^2 = t^2+ε :=
  Real.sq_sqrt (by positivity)

lemma softAbs_contDiff {ε : ℝ} (hε : 0 < ε) : ContDiff ℝ ⊤ (softAbs ε) := by
  exact ((contDiff_id.pow 2).add contDiff_const).sqrt (fun t => ne_of_gt (by positivity))

lemma softSign_contDiff {ε : ℝ} (hε : 0 < ε) : ContDiff ℝ ⊤ (softSign ε) :=
  contDiff_id.div (softAbs_contDiff hε) (fun t => (softAbs_pos hε t).ne')

lemma softAbs_hasDerivAt {ε : ℝ} (hε : 0 < ε) (t : ℝ) :
    HasDerivAt (softAbs ε) (softSign ε t) t := by
  have h := ((hasDerivAt_pow 2 t).add_const ε).sqrt (by positivity : t^2+ε ≠ 0)
  convert! h using 1
  simp only [softSign, softAbs, Nat.cast_ofNat]
  ring

lemma softSign_hasDerivAt {ε : ℝ} (hε : 0 < ε) (t : ℝ) :
    HasDerivAt (softSign ε) (softSlope ε t) t := by
  have h := (hasDerivAt_id t).div (softAbs_hasDerivAt hε t) (softAbs_pos hε t).ne'
  convert! h using 1
  dsimp [softSlope, softSign]
  field_simp [(softAbs_pos hε t).ne']
  nlinarith [softAbs_sq hε t]

lemma abs_le_softAbs {ε : ℝ} (hε : 0 < ε) (t : ℝ) : |t| ≤ softAbs ε t := by
  have hs := softAbs_sq hε t
  have hp := (softAbs_pos hε t).le
  nlinarith [sq_abs t, abs_nonneg t]

lemma softAbs_le {ε : ℝ} (hε : 0 < ε) (t : ℝ) : softAbs ε t ≤ |t|+Real.sqrt ε := by
  have hs := softAbs_sq hε t
  have hp := (softAbs_pos hε t).le
  have he := Real.sq_sqrt hε.le
  have hn := mul_nonneg (abs_nonneg t) (Real.sqrt_nonneg ε)
  nlinarith [sq_abs t, abs_nonneg t, Real.sqrt_nonneg ε]

lemma abs_softSign_le {ε : ℝ} (hε : 0 < ε) (t : ℝ) : |softSign ε t| ≤ 1 := by
  rw [softSign, abs_div, abs_of_pos (softAbs_pos hε t), div_le_one (softAbs_pos hε t)]
  exact abs_le_softAbs hε t

lemma abs_softCorrection_le {ε : ℝ} (hε : 0 < ε) (t : ℝ) : |t*softSlope ε t| ≤ 1 := by
  have hp := softAbs_pos hε t
  have hs : ε ≤ (softAbs ε t)^2 := by nlinarith [softAbs_sq hε t, sq_nonneg t]
  have hm := mul_le_mul (abs_le_softAbs hε t) hs hε.le hp.le
  rw [softSlope, ← mul_div_assoc, abs_div, abs_of_pos (pow_pos hp _),
    abs_mul, abs_of_pos hε, div_le_one (pow_pos hp _)]
  nlinarith

lemma real_sign_eq (t : ℝ) : Real.sign t = t/|t| := by
  rcases lt_trichotomy t 0 with ht | rfl | ht
  · rw [Real.sign_of_neg ht, abs_of_neg ht]
    field_simp [ht.ne]
  · simp
  · rw [Real.sign_of_pos ht, abs_of_pos ht, div_self ht.ne']

lemma softAbs_tendsto (t : ℝ) :
    Tendsto (fun m : ℕ => softAbs (1/((m : ℝ)+1)) t) atTop (𝓝 |t|) := by
  have h : Tendsto (fun m : ℕ => t^2 + 1/((m : ℝ)+1)) atTop (𝓝 (t^2+0)) :=
    tendsto_const_nhds.add tendsto_one_div_add_atTop_nhds_zero_nat
  simp only [add_zero] at h
  simpa only [softAbs, Function.comp_def, Real.sqrt_sq_eq_abs] using
    (Real.continuous_sqrt.tendsto (t^2)).comp h

lemma softSign_tendsto (t : ℝ) :
    Tendsto (fun m : ℕ => softSign (1/((m : ℝ)+1)) t) atTop (𝓝 (Real.sign t)) := by
  by_cases ht : t = 0
  · subst t
    simpa only [softSign, zero_div, Real.sign_zero] using tendsto_const_nhds
  · rw [real_sign_eq]
    exact tendsto_const_nhds.div (softAbs_tendsto t) (abs_ne_zero.mpr ht)

lemma softCorrection_tendsto (t : ℝ) :
    Tendsto (fun m : ℕ => t*softSlope (1/((m : ℝ)+1)) t) atTop (𝓝 0) := by
  by_cases ht : t = 0
  · subst t
    simp only [zero_mul]
    exact tendsto_const_nhds
  · have h := (tendsto_one_div_add_atTop_nhds_zero_nat (𝕜 := ℝ)).div
      ((softAbs_tendsto t).pow 3) (pow_ne_zero 3 (abs_ne_zero.mpr ht))
    simpa only [softSlope, Pi.div_apply, zero_div, mul_zero] using h.const_mul t

end PettyProjection.Spherical
end

noncomputable section
open Set MeasureTheory Metric Filter Topology Function
open scoped ENNReal RealInnerProductSpace Gradient
namespace PettyProjection.Spherical

lemma casimir_linear {n : ℕ} (w x : Space n) :
    casimir (fun y => ⟪w,y⟫) x = -((n : ℝ)-1)*⟪w,x⟫ := by
  simpa only [id_eq, zero_mul, mul_one, zero_sub, neg_mul] using
    casimir_scalar_inner w x (fun t => hasDerivAt_id t)
      (fun t => hasDerivAt_const t (1 : ℝ))

lemma sphereGradient_linear_inner {n : ℕ} (w : Space n) (x : Sphere n) (v : Space n) :
    ⟪sphereGradient (fun y => ⟪w,y⟫) x,v⟫ = ⟪w,v⟫-⟪w,(x : Space n)⟫*⟪(x : Space n),v⟫ := by
  rw [sphereGradient_inner]
  change (fderiv ℝ (innerSL ℝ w) x) v - (fderiv ℝ (innerSL ℝ w) x) x * _ = _
  rw [(innerSL ℝ w).fderiv]
  rfl

lemma mean_sphereGradient_inner {n : ℕ} [NeZero n] {f : Space n → ℝ}
    (hf : ContDiff ℝ 1 f) (w : Space n) :
    mean (fun x : Sphere n => ⟪sphereGradient f x,w⟫) =
      ((n : ℝ)-1)*mean (fun x : Sphere n => f x*⟪(x : Space n),w⟫) := by
  have h := mean_gradient_casimir hf ((innerSL ℝ w).contDiff (n := 2))
  change mean (fun x : Sphere n => ⟪sphereGradient f x,sphereGradient (fun y => ⟪w,y⟫) x⟫) =
    -mean (fun x : Sphere n => f x*casimir (fun y => ⟪w,y⟫) x) at h
  have hg (x : Sphere n) : ⟪sphereGradient f x,sphereGradient (fun y => ⟪w,y⟫) x⟫ =
      ⟪sphereGradient f x,w⟫ := by
    have ht : ⟪(x : Space n),sphereGradient f x⟫ = 0 := by
      rw [real_inner_comm, sphereGradient_tangent]
    rw [real_inner_comm, sphereGradient_linear_inner, ht, mul_zero, sub_zero, real_inner_comm]
  simp_rw [hg, casimir_linear] at h
  rw [h]
  simp_rw [real_inner_comm w]
  rw [mean, mean, ← integral_const_mul, ← integral_neg]
  congr 1
  funext x
  ring

lemma sphereGradient_product_scalar_inner {n : ℕ} {f : Space n → ℝ}
    (hf : ContDiff ℝ 1 f) {ρ ρ' : ℝ → ℝ}
    (hρ : ∀ t, HasDerivAt ρ (ρ' t) t) (v x w : Space n) :
    ⟪sphereGradient (fun y => f y*ρ ⟪v,y⟫) x,w⟫ =
      ρ ⟪v,x⟫*⟪sphereGradient f x,w⟫+
      f x*ρ' ⟪v,x⟫*(⟪v,w⟫-⟪v,x⟫*⟪x,w⟫) := by
  rw [sphereGradient_inner, sphereGradient_inner]
  have hd := (hf.differentiable (by norm_num) x).hasFDerivAt.mul
    ((hρ ⟪v,x⟫).comp_hasFDerivAt x (innerSL ℝ v).hasFDerivAt)
  change HasFDerivAt (fun y => f y*ρ ⟪v,y⟫) _ x at hd
  rw [hd.fderiv]
  simp only [add_apply, smul_apply, smul_eq_mul, innerSL_apply_apply, comp_apply]
  ring

lemma mean_weighted_gradient {n : ℕ} [NeZero n] {f : Space n → ℝ}
    (hf : ContDiff ℝ 1 f) {ρ ρ' : ℝ → ℝ}
    (hρ : ContDiff ℝ 1 ρ) (hρ' : Continuous ρ') (hd : ∀ t, HasDerivAt ρ (ρ' t) t)
    (v w : Space n) :
    mean (fun x : Sphere n => ρ ⟪v,(x : Space n)⟫*⟪sphereGradient f x,w⟫) =
      ((n : ℝ)-1)*mean (fun x : Sphere n => ρ ⟪v,(x : Space n)⟫*f x*⟪(x : Space n),w⟫) -
      mean (fun x : Sphere n => f x*ρ' ⟪v,(x : Space n)⟫*
        (⟪v,w⟫-⟪v,(x : Space n)⟫*⟪(x : Space n),w⟫)) := by
  have h := mean_sphereGradient_inner (hf.mul (hρ.comp (innerSL ℝ v).contDiff)) w
  change mean (fun x : Sphere n => ⟪sphereGradient (fun y => f y*ρ ⟪v,y⟫) x,w⟫) =
    ((n : ℝ)-1)*mean (fun x : Sphere n => (f x*ρ ⟪v,(x : Space n)⟫)*⟪(x : Space n),w⟫) at h
  simp_rw [sphereGradient_product_scalar_inner hf hd] at h
  have h₁ : Continuous (fun x : Sphere n => ρ ⟪v,(x : Space n)⟫*⟪sphereGradient f x,w⟫) :=
    (hρ.continuous.comp (continuous_const.inner continuous_subtype_val)).mul
      (((sphereGradient_continuous hf).comp continuous_subtype_val).inner continuous_const)
  have h₂ : Continuous (fun x : Sphere n => f x*ρ' ⟪v,(x : Space n)⟫*
      (⟪v,w⟫-⟪v,(x : Space n)⟫*⟪(x : Space n),w⟫)) := by fun_prop
  rw [mean, integral_add (continuous_integrable h₁) (continuous_integrable h₂)] at h
  change mean _ + mean _ = _ at h
  have he : (fun x : Sphere n => (f x*ρ ⟪v,(x : Space n)⟫)*⟪(x : Space n),w⟫) =
      (fun x : Sphere n => ρ ⟪v,(x : Space n)⟫*f x*⟪(x : Space n),w⟫) := by
    funext x; ring
  rw [he] at h
  linarith

end PettyProjection.Spherical
end

noncomputable section
open Set MeasureTheory Metric Filter Topology Function
open scoped ENNReal RealInnerProductSpace Gradient
namespace PettyProjection.Spherical

lemma sign_measurable : Measurable Real.sign := by
  have he : Real.sign = fun t : ℝ => t/|t| := funext real_sign_eq
  rw [he]
  exact measurable_id.div measurable_id.abs

lemma abs_sign_le (t : ℝ) : |Real.sign t| ≤ 1 := by
  rw [real_sign_eq, abs_div, abs_abs]
  by_cases ht : t = 0
  · simp [ht]
  · rw [div_self (abs_ne_zero.mpr ht)]

lemma sign_mul_self (t : ℝ) : Real.sign t*t = |t| := by
  rw [real_sign_eq]
  by_cases ht : t = 0
  · simp [ht]
  · field_simp [abs_ne_zero.mpr ht]
    exact (sq_abs t).symm

lemma sign_weight_integrable {n : ℕ} [NeZero n] (t f : Sphere n → ℝ)
    (ht : Continuous t) (hf : Continuous f) :
    Integrable (fun x => Real.sign (t x)*f x) (sigma n) := by
  apply (continuous_integrable hf).bdd_mul (sign_measurable.comp ht.measurable).aestronglyMeasurable
  filter_upwards with x
  exact abs_sign_le (t x)

lemma mean_softSign_tendsto {n : ℕ} [NeZero n] (t f : Sphere n → ℝ)
    (ht : Continuous t) (hf : Continuous f) :
    Tendsto (fun m : ℕ => mean (fun x => softSign (1/((m : ℝ)+1)) (t x)*f x)) atTop
      (𝓝 (mean (fun x => Real.sign (t x)*f x))) := by
  apply tendsto_integral_of_dominated_convergence (fun x => |f x|)
  · intro m
    exact (((softSign_contDiff (by positivity : 0 < 1/((m : ℝ)+1))).continuous.comp ht).mul hf).aestronglyMeasurable
  · exact continuous_integrable hf.abs
  · intro m
    filter_upwards with x
    rw [Real.norm_eq_abs, abs_mul]
    simpa only [one_mul] using mul_le_mul_of_nonneg_right
      (abs_softSign_le (by positivity : 0 < 1/((m : ℝ)+1)) (t x)) (abs_nonneg (f x))
  · filter_upwards with x
    exact (softSign_tendsto (t x)).mul_const (f x)

lemma mean_softCorrection_tendsto {n : ℕ} [NeZero n] (t f : Sphere n → ℝ)
    (ht : Continuous t) (hf : Continuous f) :
    Tendsto (fun m : ℕ => mean (fun x => (t x*softSlope (1/((m : ℝ)+1)) (t x))*f x)) atTop
      (𝓝 0) := by
  have h : Tendsto (fun m : ℕ => mean (fun x => (t x*softSlope (1/((m : ℝ)+1)) (t x))*f x)) atTop
      (𝓝 (mean (fun _ : Sphere n => 0))) := by
    apply tendsto_integral_of_dominated_convergence (fun x => |f x|)
    · intro m
      have hc : Continuous (softSlope (1/((m : ℝ)+1))) :=
        continuous_const.div ((softAbs_contDiff (by positivity)).continuous.pow 3)
          (fun y => pow_ne_zero _ (softAbs_pos (by positivity) y).ne')
      exact ((ht.mul (hc.comp ht)).mul hf).aestronglyMeasurable
    · exact continuous_integrable hf.abs
    · intro m
      filter_upwards with x
      rw [Real.norm_eq_abs, abs_mul]
      simpa only [one_mul] using mul_le_mul_of_nonneg_right
        (abs_softCorrection_le (by positivity : 0 < 1/((m : ℝ)+1)) (t x)) (abs_nonneg (f x))
    · filter_upwards with x
      simpa only [zero_mul] using (softCorrection_tendsto (t x)).mul_const (f x)
  simpa only [mean, integral_zero] using h

lemma mean_softAbs_tendsto {n : ℕ} [NeZero n] (t f : Sphere n → ℝ)
    (ht : Continuous t) (hf : Continuous f) :
    Tendsto (fun m : ℕ => mean (fun x => softAbs (1/((m : ℝ)+1)) (t x)*f x)) atTop
      (𝓝 (mean (fun x => |t x| *f x))) := by
  apply tendsto_integral_of_dominated_convergence (fun x => (|t x|+1)*|f x|)
  · intro m
    exact (((softAbs_contDiff (by positivity : 0 < 1/((m : ℝ)+1))).continuous.comp ht).mul hf).aestronglyMeasurable
  · exact continuous_integrable ((ht.abs.add continuous_const).mul hf.abs)
  · intro m
    filter_upwards with x
    have hε : Real.sqrt (1/((m : ℝ)+1)) ≤ 1 := by
      apply (Real.sqrt_le_one).mpr
      apply (div_le_one (by positivity : 0 < (m : ℝ)+1)).mpr
      linarith [Nat.cast_nonneg (α := ℝ) m]
    rw [Real.norm_eq_abs, abs_mul, abs_of_pos (softAbs_pos (by positivity) (t x))]
    exact mul_le_mul_of_nonneg_right ((softAbs_le (by positivity) (t x)).trans (by linarith)) (abs_nonneg (f x))
  · filter_upwards with x
    exact (softAbs_tendsto (t x)).mul_const (f x)

end PettyProjection.Spherical
end

noncomputable section
open Set MeasureTheory Metric Filter Topology Function
open scoped ENNReal RealInnerProductSpace Gradient
namespace PettyProjection.Spherical

lemma mean_sign_gradient_tangent {n : ℕ} [NeZero n] {f : Space n → ℝ}
    (hf : ContDiff ℝ 1 f) (v w : Space n) (hvw : ⟪v,w⟫ = 0) :
    mean (fun x : Sphere n => Real.sign ⟪v,(x : Space n)⟫*⟪sphereGradient f x,w⟫) =
      ((n : ℝ)-1)*mean (fun x : Sphere n => Real.sign ⟪v,(x : Space n)⟫*f x*⟪(x : Space n),w⟫) := by
  let t : Sphere n → ℝ := fun x => ⟪v,(x : Space n)⟫
  have ht : Continuous t := continuous_const.inner continuous_subtype_val
  have hF : Continuous (fun x : Sphere n => ⟪sphereGradient f x,w⟫) :=
    ((sphereGradient_continuous hf).comp continuous_subtype_val).inner continuous_const
  have hG : Continuous (fun x : Sphere n => f x*⟪(x : Space n),w⟫) := by fun_prop
  have he (m : ℕ) :
      mean (fun x : Sphere n => softSign (1/((m : ℝ)+1)) (t x)*⟪sphereGradient f x,w⟫) =
      ((n : ℝ)-1)*mean (fun x : Sphere n => softSign (1/((m : ℝ)+1)) (t x)*(f x*⟪(x : Space n),w⟫)) +
        mean (fun x : Sphere n => (t x*softSlope (1/((m : ℝ)+1)) (t x))*(f x*⟪(x : Space n),w⟫)) := by
    have hp : 0 < 1/((m : ℝ)+1) := by positivity
    have hc : Continuous (softSlope (1/((m : ℝ)+1))) :=
      continuous_const.div ((softAbs_contDiff hp).continuous.pow 3)
        (fun y => pow_ne_zero _ (softAbs_pos hp y).ne')
    have h := mean_weighted_gradient hf ((softSign_contDiff hp).of_le (by norm_num)) hc
      (softSign_hasDerivAt hp) v w
    rw [h]
    simp only [hvw, zero_sub]
    rw [mean, mean, mean, mean, sub_eq_add_neg, ← integral_neg]
    congr 1
    · congr 1
      apply integral_congr_ae
      filter_upwards with x
      dsimp [t]; ring
    · apply integral_congr_ae
      filter_upwards with x
      dsimp [t]; ring
  have h₁ := mean_softSign_tendsto t _ ht hF
  have h₂ := ((mean_softSign_tendsto t _ ht hG).const_mul ((n : ℝ)-1)).add
    (mean_softCorrection_tendsto t _ ht hG)
  have heq := tendsto_nhds_unique h₁ (h₂.congr' (Filter.Eventually.of_forall (fun m => (he m).symm)))
  simpa only [add_zero, t, mul_assoc] using heq

lemma sphereGradient_scalar_inner {n : ℕ} {ρ ρ' : ℝ → ℝ}
    (hρ : ∀ t, HasDerivAt ρ (ρ' t) t) (v x w : Space n) :
    ⟪sphereGradient (fun y => ρ ⟪v,y⟫) x,w⟫ =
      ρ' ⟪v,x⟫*(⟪v,w⟫-⟪v,x⟫*⟪x,w⟫) := by
  have hd := (hρ ⟪v,x⟫).comp_hasFDerivAt x (innerSL ℝ v).hasFDerivAt
  change HasFDerivAt (fun y => ρ ⟪v,y⟫) _ x at hd
  rw [sphereGradient_inner, hd.fderiv]
  simp only [smul_apply, smul_eq_mul, innerSL_apply_apply]
  ring

lemma mean_sign_gradient_radial {n : ℕ} [NeZero n] {f : Space n → ℝ}
    (hf : ContDiff ℝ 2 f) (v : Space n) :
    mean (fun x : Sphere n => Real.sign ⟪v,(x : Space n)⟫*⟪sphereGradient f x,v⟫) =
      -mean (fun x : Sphere n => |⟪v,(x : Space n)⟫| * casimir f x) := by
  let t : Sphere n → ℝ := fun x => ⟪v,(x : Space n)⟫
  have ht : Continuous t := continuous_const.inner continuous_subtype_val
  have hF : Continuous (fun x : Sphere n => ⟪sphereGradient f x,v⟫) :=
    ((sphereGradient_continuous (hf.of_le (by norm_num))).comp continuous_subtype_val).inner continuous_const
  have hG : Continuous (fun x : Sphere n => casimir f x) :=
    by
      unfold casimir
      exact continuous_const.mul (continuous_finsetSum _ fun i _ =>
        continuous_finsetSum _ fun j _ =>
          (continuous_rotDeriv _ (contDiff_rotDeriv _ hf)).comp continuous_subtype_val)
  have he (m : ℕ) :
      mean (fun x : Sphere n => softSign (1/((m : ℝ)+1)) (t x)*⟪sphereGradient f x,v⟫) =
      -mean (fun x : Sphere n => softAbs (1/((m : ℝ)+1)) (t x)*casimir f x) := by
    have hp : 0 < 1/((m : ℝ)+1) := by positivity
    have h := mean_gradient_casimir
      ((show ContDiff ℝ 1 (softAbs (1/((m : ℝ)+1))) from (softAbs_contDiff hp).of_le le_top).comp (innerSL ℝ v).contDiff) hf
    change mean (fun x : Sphere n => ⟪sphereGradient (fun y => softAbs (1/((m : ℝ)+1)) ⟪v,y⟫) x,sphereGradient f x⟫) = _ at h
    have hg (x : Sphere n) :
        ⟪sphereGradient (fun y => softAbs (1/((m : ℝ)+1)) ⟪v,y⟫) x,sphereGradient f x⟫ =
          softSign (1/((m : ℝ)+1)) (t x)*⟪sphereGradient f x,v⟫ := by
      rw [sphereGradient_scalar_inner (softAbs_hasDerivAt hp)]
      have hx : ⟪(x : Space n),sphereGradient f x⟫ = 0 := by rw [real_inner_comm, sphereGradient_tangent]
      rw [hx, mul_zero, sub_zero, real_inner_comm v]
    simp_rw [hg] at h
    exact h
  have h₁ := mean_softSign_tendsto t _ ht hF
  have h₂ := (mean_softAbs_tendsto t _ ht hG).neg
  exact tendsto_nhds_unique h₁ (h₂.congr' (Filter.Eventually.of_forall (fun m => (he m).symm)))

end PettyProjection.Spherical
end

noncomputable section
open Set MeasureTheory Metric Filter Topology Function
open scoped ENNReal RealInnerProductSpace Gradient
namespace PettyProjection.Spherical

def signedPair {n : ℕ} (F G : Sphere n → Space n) : ℝ :=
  ∫ z : Sphere n × Sphere n, Real.sign ⟪(z.1 : Space n),(z.2 : Space n)⟫ *
    ⟪F z.1,G z.2⟫ ∂(sigma n).prod (sigma n)

lemma signedPair_integrable {n : ℕ} [NeZero n] {F G : Sphere n → Space n}
    (hF : Continuous F) (hG : Continuous G) :
    Integrable (fun z : Sphere n × Sphere n => Real.sign ⟪(z.1 : Space n),(z.2 : Space n)⟫ *
      ⟪F z.1,G z.2⟫) ((sigma n).prod (sigma n)) := by
  have hi : Integrable (fun z : Sphere n × Sphere n => ⟪F z.1,G z.2⟫)
      ((sigma n).prod (sigma n)) :=
    (by fun_prop : Continuous _).integrable_of_hasCompactSupport (HasCompactSupport.of_compactSpace _)
  apply hi.bdd_mul (sign_measurable.comp (by fun_prop : Continuous
    (fun z : Sphere n × Sphere n => ⟪(z.1 : Space n),(z.2 : Space n)⟫)).measurable).aestronglyMeasurable
  filter_upwards with z
  exact abs_sign_le _

lemma signedPair_eq {n : ℕ} [NeZero n] {F G : Sphere n → Space n}
    (hF : Continuous F) (hG : Continuous G) :
    signedPair F G = mean (fun u => mean (fun v =>
      Real.sign ⟪(u : Space n),(v : Space n)⟫ * ⟪F u,G v⟫)) :=
  integral_prod _ (signedPair_integrable hF hG)

lemma signedPair_eq_swap {n : ℕ} [NeZero n] {F G : Sphere n → Space n}
    (hF : Continuous F) (hG : Continuous G) :
    signedPair F G = mean (fun v => mean (fun u =>
      Real.sign ⟪(u : Space n),(v : Space n)⟫ * ⟪F u,G v⟫)) := by
  rw [signedPair_eq hF hG]
  exact integral_integral_swap (signedPair_integrable hF hG)

lemma signedPair_symm {n : ℕ} [NeZero n] {F G : Sphere n → Space n}
    (hF : Continuous F) (hG : Continuous G) : signedPair F G = signedPair G F := by
  rw [signedPair_eq hF hG, signedPair_eq_swap hG hF]
  congr 1
  funext u
  congr 1
  funext v
  rw [real_inner_comm (u : Space n) (v : Space n), real_inner_comm (F u) (G v)]

lemma signedPair_add_left {n : ℕ} [NeZero n] {F G J : Sphere n → Space n}
    (hF : Continuous F) (hG : Continuous G) (hJ : Continuous J) :
    signedPair (fun u => F u+G u) J = signedPair F J+signedPair G J := by
  unfold signedPair
  simp_rw [inner_add_left, mul_add]
  exact integral_add (signedPair_integrable hF hJ) (signedPair_integrable hG hJ)

lemma signedPair_add_right {n : ℕ} [NeZero n] {F G J : Sphere n → Space n}
    (hF : Continuous F) (hG : Continuous G) (hJ : Continuous J) :
    signedPair F (fun u => G u+J u) = signedPair F G+signedPair F J := by
  unfold signedPair
  simp_rw [inner_add_right, mul_add]
  exact integral_add (signedPair_integrable hF hG) (signedPair_integrable hF hJ)

lemma signedPair_smul_left {n : ℕ} (F G : Sphere n → Space n) (c : ℝ) :
    signedPair (fun u => c • F u) G = c*signedPair F G := by
  unfold signedPair
  simp_rw [real_inner_smul_left, mul_left_comm _ c]
  exact integral_const_mul c _

lemma signedPair_smul_right {n : ℕ} (F G : Sphere n → Space n) (c : ℝ) :
    signedPair F (fun u => c • G u) = c*signedPair F G := by
  unfold signedPair
  simp_rw [real_inner_smul_right, mul_left_comm _ c]
  exact integral_const_mul c _

end PettyProjection.Spherical
end

end OAI