math / data /lean /OAI /Combinatorics /EditDistance /DirectSums.lean
introvoyz045's picture
Migrated from GitHub
4a56cd6 verified
Raw History Blame Contribute Delete
5.32 kB
import OAI.Combinatorics.EditDistance.PrimeDisplacement
namespace OAI
/-!
# Weighted direct sums and normalized slice maps
-/
namespace EditL1
open scoped BigOperators
abbrev L1 := lp (fun _ : β„• => ℝ) 1
abbrev L1On (I : Type*) := lp (fun _ : I => ℝ) 1
lemma norm_eq_tsum {I : Type*} (f : L1On I) : β€–fβ€– = βˆ‘' i, |f i| := by
simpa using lp.norm_eq_tsum_rpow (by norm_num : 0 < (1 : ENNReal).toReal) f
lemma summable_abs {I : Type*} (f : L1On I) : Summable (fun i => |f i|) := by
simpa using (lp.memβ„“p f).summable (by norm_num : 0 < (1 : ENNReal).toReal)
noncomputable def reindex {I J : Type*} (e : I ≃ J) (f : L1On I) : L1On J :=
⟨fun j => f (e.symm j), memβ„“p_gen (by
simpa only [ENNReal.toReal_one, Real.rpow_one, Real.norm_eq_abs, Function.comp_def] using!
(e.symm.summable_iff).mpr (summable_abs f))⟩
lemma reindex_apply {I J : Type*} (e : I ≃ J) (f : L1On I) (j : J) :
reindex e f j = f (e.symm j) := rfl
lemma reindex_norm {I J : Type*} (e : I ≃ J) (f : L1On I) :
β€–reindex e fβ€– = β€–fβ€– := by
simp_rw [norm_eq_tsum, reindex_apply]
exact e.symm.tsum_eq (fun i => |f i|)
lemma reindex_sub {I J : Type*} (e : I ≃ J) (f g : L1On I) :
reindex e (f - g) = reindex e f - reindex e g := by
ext j
rfl
section FiniteSum
variable {I : Type*} [Fintype I]
lemma family_summable (f : I β†’ L1) : Summable (fun a : I Γ— β„• => |f a.1 a.2|) :=
(summable_prod_of_nonneg (fun _ => abs_nonneg _)).mpr
⟨fun i => summable_abs (f i), (hasSum_fintype _).summable⟩
noncomputable def familyLp (f : I β†’ L1) : L1On (I Γ— β„•) :=
⟨fun a => f a.1 a.2, memβ„“p_gen (by simpa using family_summable f)⟩
lemma familyLp_apply (f : I β†’ L1) (i : I) (n : β„•) : familyLp f (i, n) = f i n := rfl
lemma familyLp_norm (f : I β†’ L1) : β€–familyLp fβ€– = βˆ‘ i, β€–f iβ€– := by
rw [norm_eq_tsum]
change (βˆ‘' a : I Γ— β„•, |f a.1 a.2|) = _
rw [(family_summable f).tsum_prod, tsum_fintype]
exact Finset.sum_congr rfl (fun i _ => (norm_eq_tsum (f i)).symm)
lemma familyLp_sub (f g : I β†’ L1) : familyLp (f - g) = familyLp f - familyLp g := by
ext ⟨i, n⟩
rfl
variable [Nonempty I]
noncomputable def indexEquiv : (I Γ— β„•) ≃ β„• := Classical.choice inferInstance
noncomputable def pack (f : I β†’ L1) : L1 := reindex indexEquiv (familyLp f)
lemma pack_norm (f : I β†’ L1) : β€–pack fβ€– = βˆ‘ i, β€–f iβ€– := by
rw [pack, reindex_norm, familyLp_norm]
lemma pack_sub (f g : I β†’ L1) : pack (f - g) = pack f - pack g := by
rw [pack, familyLp_sub, reindex_sub]
rfl
lemma pack_distance (f g : I β†’ L1) : β€–pack f - pack gβ€– = βˆ‘ i, β€–f i - g iβ€– := by
rw [← pack_sub, pack_norm]
rfl
lemma weighted_pack_distance (w : I β†’ ℝ) (hw : βˆ€ i, 0 ≀ w i) (f g : I β†’ L1) :
β€–pack (fun i => w i β€’ f i) - pack (fun i => w i β€’ g i)β€– =
βˆ‘ i, w i * β€–f i - g iβ€– := by
rw [pack_distance]
apply Finset.sum_congr rfl
intro i _
rw [← smul_sub, norm_smul, Real.norm_eq_abs, abs_of_nonneg (hw i)]
end FiniteSum
end EditL1
namespace EditAverage
lemma avg_prod {X Y : Type*} [Fintype X] [Fintype Y] (f : X β†’ Y β†’ ℝ) :
avg (fun z : X Γ— Y => f z.1 z.2) = avg (fun x => avg (f x)) := by
simp only [avg, Fintype.card_prod, Nat.cast_mul, Fintype.sum_prod_type, ← Finset.sum_div,
div_div]
rw [mul_comm]
end EditAverage
namespace EditSlices
open scoped BigOperators
open EditAverage EditFourier
variable {J : Type*} [Fintype J] [DecidableEq J]
variable (G : J β†’ Type*) [βˆ€ j, AddCommGroup (G j)] [βˆ€ j, Fintype (G j)]
omit [Fintype J] [βˆ€ j, Fintype (G j)] in
lemma split_add_single (i : J) (x u : G i) (y : βˆ€ j : {j // j β‰  i}, G j) :
(Equiv.piSplitAt i G).symm (x + u, y) =
(Equiv.piSplitAt i G).symm (x, y) + Pi.single i u := by
ext j
by_cases h : j = i
Β· subst j
simp [Equiv.piSplitAt]
Β· simp [Equiv.piSplitAt, h]
noncomputable def slice (H : (βˆ€ j, G j) β†’ EditL1.L1) (i : J) (Ο‰ : ℝ) : G i β†’ EditL1.L1 :=
fun x => EditL1.pack (fun y : (βˆ€ j : {j // j β‰  i}, G j) =>
(1 / (Ο‰ * Fintype.card (βˆ€ j : {j // j β‰  i}, G j))) β€’
H ((Equiv.piSplitAt i G).symm (x,y)))
lemma variation_slice (H : (βˆ€ j, G j) β†’ EditL1.L1) (i : J) (Ο‰ : ℝ) (hΟ‰ : 0 < Ο‰)
(u : G i) : variation (slice G H i Ο‰) u = variation H (Pi.single i u) / Ο‰ := by
let Y := βˆ€ j : {j // j β‰  i}, G j
let E := Equiv.piSplitAt i G
have hY : 0 < (Fintype.card Y : ℝ) := Nat.cast_pos.mpr Fintype.card_pos
have hweight : 0 ≀ 1 / (Ο‰ * (Fintype.card Y : ℝ)) := by positivity
have hd (x : G i) :
β€–slice G H i Ο‰ (x + u) - slice G H i Ο‰ xβ€– =
(1 / Ο‰) * avg (fun y : Y => β€–H (E.symm (x,y) + Pi.single i u) - H (E.symm (x,y))β€–) := by
rw [slice, slice, EditL1.weighted_pack_distance (fun _ => _) (fun _ => hweight)]
simp_rw [split_add_single]
rw [← Finset.mul_sum, avg]
dsimp [E, Y]
ring
have he : variation H (Pi.single i u) =
avg (fun x : G i => avg (fun y : Y =>
β€–H (E.symm (x,y) + Pi.single i u) - H (E.symm (x,y))β€–)) := by
rw [← avg_prod]
exact (avg_equiv E.symm (fun z => β€–H (z + Pi.single i u) - H zβ€–)).symm
change avg (fun x => β€–slice G H i Ο‰ (x + u) - slice G H i Ο‰ xβ€–) = _
simp_rw [hd]
rw [avg_mul, ← he]
ring
end EditSlices
end OAI