import Mathlib /-! # Exact Bottleneck Distance, Algebraic Interleaving Stability & Isometry Theorem (`d_B = d_I`) This standalone module formalizes `ℝ`-indexed persistence modules over an arbitrary field `K`, finite persistence diagrams / barcodes, partial matchings to the diagonal, exact bottleneck distance `d_B`, and `ε`-interleavings / interleaving distance `d_I`, and proves: 1. **Exact Attainment & Metric Properties of Bottleneck Distance**: Since `PartialMatching D₁ D₂` is finite and nonempty, `d_B(D₁, D₂)` is attained by an optimal partial matching, so `d_B(D₁, D₂) ≤ ε ↔ ∃ M, M.HasCostLe ε` for every `ε ≥ 0`. 2. **Converse Stability (`d_I ≤ d_B`)**: Every partial `ε`-matching constructs an explicit `ε`-interleaving between the corresponding diagram persistence modules. 3. **Algebraic Stability Theorem (`d_B ≤ d_I`) via Bauer–Lesnick Induced Matchings**: For any `ε`-interleaving `(φ, ψ)` between finite-barcode persistence modules `U, V`: - `im(φ)` is piecewise-constant on the finite critical-value grid and decomposes into a finite barcode `D_im` via finite-poset interval decomposition; - Exact quotient-dimension bounds for epimorphisms (`U ↠ im(φ) ↠ im(σ_{U, 2ε})`) and monomorphisms (`im(σ_{V[-ε], 2ε}) ↪ im(φ) ↪ V[ε]`) plus order-statistics matching produce an explicit partial `ε`-matching between `D₁` and `D₂`. 4. **Exact Isometry Theorem (`d_I = d_B`)**: `interleavingDist (diagramModule K D₁) (diagramModule K D₂) = bottleneckDist D₁ D₂` and `Nonempty (Interleaving (diagramModule K D₁) (diagramModule K D₂) ε) ↔ bottleneckDist D₁ D₂ ≤ ε`. 5. **Sublevel-Set Filtration Stability & Exact Sharpness Witness**: For any finite graph `G` and vertex functions `f, g : V → ℝ`, reduced `0`-th persistent homology modules `sublevelH0 K G f` and `sublevelH0 K G g` are `‖f - g‖_∞`-interleaved, yielding `d_B(Dgm(f), Dgm(g)) ≤ ‖f - g‖_∞`, with an exact sharpness family on `P₃` attaining `d_B(Dgm(f_c), Dgm(g_c)) = ‖f_c - g_c‖_∞ = c` for every `c > 0`. -/ open scoped BigOperators namespace OpenTDAPersistence.BottleneckInterleaving universe u /-! ### Part 1: `ℝ`-Indexed Persistence Modules, Diagrams, and `ε`-Interleavings -/ /-- A persistence module over a field `K` indexed by `(ℝ, ≤)`. -/ structure RPersistenceModule (K : Type u) [Field K] where obj : ℝ → Type u addCommGroup : ∀ t, AddCommGroup (obj t) module : ∀ t, Module K (obj t) map : ∀ {s t : ℝ}, s ≤ t → obj s →ₗ[K] obj t map_id : ∀ t, map (le_refl t) = LinearMap.id map_comp : ∀ {r s t : ℝ} (hrs : r ≤ s) (hst : s ≤ t), (map hst).comp (map hrs) = map (le_trans hrs hst) attribute [instance] RPersistenceModule.addCommGroup attribute [instance] RPersistenceModule.module namespace RPersistenceModule variable {K : Type u} [Field K] lemma map_id_apply (U : RPersistenceModule K) (t : ℝ) (x : U.obj t) : U.map (le_refl t) x = x := by simpa using LinearMap.congr_fun (U.map_id t) x lemma map_comp_apply (U : RPersistenceModule K) {r s t : ℝ} (hrs : r ≤ s) (hst : s ≤ t) (x : U.obj r) : U.map hst (U.map hrs x) = U.map (le_trans hrs hst) x := by simpa using LinearMap.congr_fun (U.map_comp hrs hst) x /-- A morphism of `ℝ`-indexed persistence modules. -/ structure Hom (U V : RPersistenceModule K) where toFun : ∀ t : ℝ, U.obj t →ₗ[K] V.obj t comm : ∀ {s t : ℝ} (hst : s ≤ t), (toFun t).comp (U.map hst) = (V.map hst).comp (toFun s) /-- An isomorphism of `ℝ`-indexed persistence modules. -/ structure Iso (U V : RPersistenceModule K) where equiv : ∀ t : ℝ, U.obj t ≃ₗ[K] V.obj t comm : ∀ {s t : ℝ} (hst : s ≤ t), (equiv t : U.obj t →ₗ[K] V.obj t).comp (U.map hst) = (V.map hst).comp (equiv s : U.obj s →ₗ[K] V.obj s) def Iso.toHom {U V : RPersistenceModule K} (e : Iso U V) : Hom U V where toFun t := (e.equiv t : U.obj t →ₗ[K] V.obj t) comm := e.comm def Iso.symm {U V : RPersistenceModule K} (e : Iso U V) : Iso V U where equiv t := (e.equiv t).symm comm {s t} hst := by ext y apply (e.equiv t).injective have hc := LinearMap.congr_fun (e.comm hst) ((e.equiv s).symm y) simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, LinearEquiv.apply_symm_apply] at hc ⊢ exact hc.symm def Iso.trans {U V W : RPersistenceModule K} (e₁ : Iso U V) (e₂ : Iso V W) : Iso U W where equiv t := (e₁.equiv t).trans (e₂.equiv t) comm {s t} hst := by ext x have h1 := LinearMap.congr_fun (e₁.comm hst) x have h2 := LinearMap.congr_fun (e₂.comm hst) (e₁.equiv s x) simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, LinearEquiv.trans_apply] at h1 h2 ⊢ rw [h1, h2] def Hom.comp {U V W : RPersistenceModule K} (g : Hom V W) (f : Hom U V) : Hom U W where toFun t := (g.toFun t).comp (f.toFun t) comm {s t} hst := by ext x have hf := LinearMap.congr_fun (f.comm hst) x have hg := LinearMap.congr_fun (g.comm hst) (f.toFun s x) simp only [LinearMap.comp_apply] at hf hg ⊢ rw [hf, hg] /-- The `ε`-shifted persistence module `U[ε]` with `(U[ε])_t = U_{t + ε}`. -/ def shift (U : RPersistenceModule K) (ε : ℝ) : RPersistenceModule K where obj t := U.obj (t + ε) addCommGroup t := U.addCommGroup (t + ε) module t := U.module (t + ε) map hst := U.map (by linarith) map_id t := U.map_id (t + ε) map_comp hrs hst := U.map_comp (by linarith) (by linarith) /-- Shift of a morphism `f : U → V` by `ε`. -/ def Hom.shift {U V : RPersistenceModule K} (f : Hom U V) (ε : ℝ) : Hom (U.shift ε) (V.shift ε) where toFun t := f.toFun (t + ε) comm hst := f.comm (by linarith) /-- Shift of an isomorphism `e : U ≅ V` by `ε`. -/ def Iso.shift {U V : RPersistenceModule K} (e : Iso U V) (ε : ℝ) : Iso (U.shift ε) (V.shift ε) where equiv t := e.equiv (t + ε) comm hst := e.comm (by linarith) /-- An `ε`-interleaving `(φ, ψ)` between `U` and `V` (`ε ≥ 0`). -/ structure Interleaving (U V : RPersistenceModule K) (ε : ℝ) where hε : 0 ≤ ε φ : Hom U (V.shift ε) ψ : Hom V (U.shift ε) left_comp : ∀ t : ℝ, (ψ.toFun (t + ε)).comp (φ.toFun t) = U.map (by linarith : t ≤ t + ε + ε) right_comp : ∀ t : ℝ, (φ.toFun (t + ε)).comp (ψ.toFun t) = V.map (by linarith : t ≤ t + ε + ε) /-- Transport an `ε`-interleaving across isomorphisms `U ≅ U'` and `V ≅ V'`. -/ def Interleaving.transport {U U' V V' : RPersistenceModule K} {ε : ℝ} (I : Interleaving U V ε) (eU : Iso U U') (eV : Iso V V') : Interleaving U' V' ε where hε := I.hε φ := (eV.shift ε).toHom.comp (I.φ.comp eU.symm.toHom) ψ := (eU.shift ε).toHom.comp (I.ψ.comp eV.symm.toHom) left_comp t := by apply LinearMap.ext; intro x let y : V.obj (t + ε) := I.φ.toFun t ((eU.equiv t).symm x) change eU.equiv (t + ε + ε) (I.ψ.toFun (t + ε) ((eV.equiv (t + ε)).symm (eV.equiv (t + ε) y))) = _ rw [LinearEquiv.symm_apply_apply] have hl : I.ψ.toFun (t + ε) y = U.map (by linarith [I.hε] : t ≤ t + ε + ε) ((eU.equiv t).symm x) := LinearMap.congr_fun (I.left_comp t) ((eU.equiv t).symm x) change eU.equiv (t + ε + ε) (I.ψ.toFun (t + ε) y) = _ rw [hl] have hc := LinearMap.congr_fun (eU.comm (by linarith [I.hε] : t ≤ t + ε + ε)) ((eU.equiv t).symm x) simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, LinearEquiv.apply_symm_apply] at hc exact hc right_comp t := by apply LinearMap.ext; intro y let x : U.obj (t + ε) := I.ψ.toFun t ((eV.equiv t).symm y) change eV.equiv (t + ε + ε) (I.φ.toFun (t + ε) ((eU.equiv (t + ε)).symm (eU.equiv (t + ε) x))) = _ rw [LinearEquiv.symm_apply_apply] have hr : I.φ.toFun (t + ε) x = V.map (by linarith [I.hε] : t ≤ t + ε + ε) ((eV.equiv t).symm y) := LinearMap.congr_fun (I.right_comp t) ((eV.equiv t).symm y) change eV.equiv (t + ε + ε) (I.φ.toFun (t + ε) x) = _ rw [hr] have hc := LinearMap.congr_fun (eV.comm (by linarith [I.hε] : t ≤ t + ε + ε)) ((eV.equiv t).symm y) simp only [LinearMap.comp_apply, LinearEquiv.coe_coe, LinearEquiv.apply_symm_apply] at hc exact hc /-- Exact interleaving distance `d_I(U, V) = inf {ε ≥ 0 | U and V are ε-interleaved}`. -/ noncomputable def interleavingDist (U V : RPersistenceModule K) : ℝ := sInf {ε : ℝ | Nonempty (Interleaving U V ε)} /-- Pointwise image persistence module `im(f)` of a morphism `f : U → V`. -/ def imageMod {U V : RPersistenceModule K} (f : Hom U V) : RPersistenceModule K where obj t := LinearMap.range (f.toFun t) addCommGroup _ := inferInstance module _ := inferInstance map {s t} hst := { toFun := fun ⟨y, hy⟩ => ⟨V.map hst y, by rcases hy with ⟨x, rfl⟩ refine ⟨U.map hst x, ?_⟩ exact LinearMap.congr_fun (f.comm hst) x⟩ map_add' := fun _ _ => Subtype.ext (map_add _ _ _) map_smul' := fun _ _ => Subtype.ext (map_smul _ _ _) } map_id t := by apply LinearMap.ext; rintro ⟨y, hy⟩ exact Subtype.ext (V.map_id_apply t y) map_comp hrs hst := by apply LinearMap.ext; rintro ⟨y, hy⟩ exact Subtype.ext (V.map_comp_apply hrs hst y) /-- Canonical surjection `U ↠ im(f)`. -/ def imageProj {U V : RPersistenceModule K} (f : Hom U V) : Hom U (imageMod f) where toFun t := { toFun := fun x => ⟨f.toFun t x, ⟨x, rfl⟩⟩ map_add' := fun _ _ => Subtype.ext (map_add _ _ _) map_smul' := fun _ _ => Subtype.ext (map_smul _ _ _) } comm hst := by ext x exact Subtype.ext (LinearMap.congr_fun (f.comm hst) x) /-- Canonical injection `im(f) ↪ V`. -/ def imageIncl {U V : RPersistenceModule K} (f : Hom U V) : Hom (imageMod f) V where toFun t := (LinearMap.range (f.toFun t)).subtype comm _ := by ext ⟨y, _⟩; rfl lemma imageProj_surjective {U V : RPersistenceModule K} (f : Hom U V) (t : ℝ) : Function.Surjective ((imageProj f).toFun t) := by rintro ⟨y, x, rfl⟩ exact ⟨x, rfl⟩ lemma imageIncl_injective {U V : RPersistenceModule K} (f : Hom U V) (t : ℝ) : Function.Injective ((imageIncl f).toFun t) := Subtype.val_injective end RPersistenceModule /-! ### Part 2: Finite Persistence Diagrams, Interval Modules & Partial Matchings -/ /-- A finite half-open persistence bar `[b, d)` with `b < d`. -/ structure Bar where b : ℝ d : ℝ hlt : b < d /-- A finite persistence diagram (indexed family of bars). -/ structure Diagram where n : ℕ bar : Fin n → Bar /-- The empty persistence diagram. -/ def Diagram.empty : Diagram where n := 0 bar := Fin.elim0 /-- Underlying space of the half-open interval module `𝕀[b, d)` at `t ∈ ℝ`. -/ abbrev RIntervalObj (K : Type u) (b d t : ℝ) : Type u := PLift (b ≤ t ∧ t < d) → K noncomputable def rIntervalObjEquivK (K : Type u) [Field K] {b d t : ℝ} (h : b ≤ t ∧ t < d) : RIntervalObj K b d t ≃ₗ[K] K where toFun f := f ⟨h⟩ map_add' _ _ := rfl map_smul' _ _ := rfl invFun c _ := c left_inv f := funext fun ⟨h'⟩ => congrArg (fun p => f ⟨p⟩) (Subsingleton.elim h' h) right_inv _ := rfl noncomputable def rIntervalObjEquivZero (K : Type u) [Field K] {b d t : ℝ} (h : ¬ (b ≤ t ∧ t < d)) : RIntervalObj K b d t ≃ₗ[K] (Fin 0 → K) where toFun _ k := Fin.elim0 k map_add' _ _ := funext fun k => Fin.elim0 k map_smul' _ _ := funext fun k => Fin.elim0 k invFun _ ht := absurd ht.down h left_inv _f := funext fun ht => absurd ht.down h right_inv _g := funext fun k => Fin.elim0 k lemma finiteDimensional_rIntervalObj (K : Type u) [Field K] (b d t : ℝ) : FiniteDimensional K (RIntervalObj K b d t) := by by_cases h : b ≤ t ∧ t < d · exact Module.Finite.equiv (rIntervalObjEquivK K h).symm · exact Module.Finite.equiv (rIntervalObjEquivZero K h).symm noncomputable def rIntervalMap (K : Type u) [Field K] {b d s t : ℝ} (_hst : s ≤ t) : RIntervalObj K b d s →ₗ[K] RIntervalObj K b d t where toFun f _ := if hs : b ≤ s ∧ s < d then f ⟨hs⟩ else 0 map_add' f g := by funext ht by_cases hs : b ≤ s ∧ s < d <;> simp [hs] map_smul' c f := by funext ht by_cases hs : b ≤ s ∧ s < d <;> simp [hs] /-- The `ℝ`-indexed persistence module `⨁_{i} 𝕀[b_i, d_i)` associated to a finite diagram `D`. -/ noncomputable def diagramModule (K : Type u) [Field K] (D : Diagram) : RPersistenceModule K where obj t := ∀ i : Fin D.n, RIntervalObj K (D.bar i).b (D.bar i).d t addCommGroup _ := inferInstance module _ := inferInstance map hst := { toFun := fun x i => rIntervalMap K hst (x i) map_add' := fun x y => by funext i; exact (rIntervalMap K hst).map_add _ _ map_smul' := fun c x => by funext i; exact (rIntervalMap K hst).map_smul _ _ } map_id t := by apply LinearMap.ext; intro x; funext i ht simp [rIntervalMap, ht.down] map_comp {r s t} hrs hst := by apply LinearMap.ext; intro x; funext i ht simp only [LinearMap.comp_apply, rIntervalMap, LinearMap.coe_mk, AddHom.coe_mk] by_cases hr : (D.bar i).b ≤ r ∧ r < (D.bar i).d · have hs : (D.bar i).b ≤ s ∧ s < (D.bar i).d := ⟨hr.1.trans hrs, lt_of_le_of_lt hst ht.down.2⟩ simp [hr, hs] · by_cases hs : (D.bar i).b ≤ s ∧ s < (D.bar i).d <;> simp [hr, hs] lemma finiteDimensional_diagramModule (K : Type u) [Field K] (D : Diagram) (t : ℝ) : FiniteDimensional K ((diagramModule K D).obj t) := by have : ∀ i : Fin D.n, FiniteDimensional K (RIntervalObj K (D.bar i).b (D.bar i).d t) := fun i => finiteDimensional_rIntervalObj K (D.bar i).b (D.bar i).d t change FiniteDimensional K (∀ i : Fin D.n, RIntervalObj K (D.bar i).b (D.bar i).d t) infer_instance /-- Shifted diagram `D[ε]` with bars `[b_i - ε, d_i - ε)`. -/ def Diagram.shift (D : Diagram) (ε : ℝ) : Diagram where n := D.n bar i := ⟨(D.bar i).b - ε, (D.bar i).d - ε, by linarith [(D.bar i).hlt]⟩ /-- Canonical isomorphism `(diagramModule K D).shift ε ≅ diagramModule K (D.shift ε)`. -/ noncomputable def diagramModuleShiftIso (K : Type u) [Field K] (D : Diagram) (ε : ℝ) : RPersistenceModule.Iso ((diagramModule K D).shift ε) (diagramModule K (D.shift ε)) where equiv t := { toFun := fun x i ⟨h⟩ => x i ⟨⟨by have h1 := h.1; simp only [Diagram.shift] at h1; linarith, by have h2 := h.2; simp only [Diagram.shift] at h2; linarith⟩⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun y i ⟨h⟩ => y i ⟨⟨by simp only [Diagram.shift]; linarith [h.1], by simp only [Diagram.shift]; linarith [h.2]⟩⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } comm {s t} hst := by apply LinearMap.ext; intro x; funext i ⟨ht⟩ change (if hs : (D.bar i).b ≤ s + ε ∧ s + ε < (D.bar i).d then x i ⟨hs⟩ else 0) = if hs : (D.bar i).b - ε ≤ s ∧ s < (D.bar i).d - ε then x i ⟨⟨by linarith [hs.1], by linarith [hs.2]⟩⟩ else 0 have hiff : ((D.bar i).b ≤ s + ε ∧ s + ε < (D.bar i).d) ↔ ((D.bar i).b - ε ≤ s ∧ s < (D.bar i).d - ε) := by constructor <;> rintro ⟨h1, h2⟩ <;> exact ⟨by linarith, by linarith⟩ by_cases hs : (D.bar i).b ≤ s + ε ∧ s + ε < (D.bar i).d · rw [dif_pos hs, dif_pos (hiff.mp hs)] · rw [dif_neg hs, dif_neg (mt hiff.mpr hs)] /-- `ℓ_∞` distance between two bars. -/ def barDist (p q : Bar) : ℝ := max |p.b - q.b| |p.d - q.d| /-- `ℓ_∞` distance from a bar `[b, d)` to the diagonal `Δ`. -/ noncomputable def diagDist (p : Bar) : ℝ := (p.d - p.b) / 2 lemma barDist_nonneg (p q : Bar) : 0 ≤ barDist p q := le_max_of_le_left (abs_nonneg _) lemma diagDist_pos (p : Bar) : 0 < diagDist p := by unfold diagDist; linarith [p.hlt] lemma diagDist_nonneg (p : Bar) : 0 ≤ diagDist p := (diagDist_pos p).le /-- A partial matching between finite persistence diagrams `D₁` and `D₂`. -/ structure PartialMatching (D₁ D₂ : Diagram) where toFun : Fin D₁.n → Option (Fin D₂.n) inj : ∀ {i₁ i₂ : Fin D₁.n} {j : Fin D₂.n}, toFun i₁ = some j → toFun i₂ = some j → i₁ = i₂ namespace PartialMatching /-- The empty partial matching (matches every bar to the diagonal). -/ def empty (D₁ D₂ : Diagram) : PartialMatching D₁ D₂ where toFun _ := none inj h := by cases h /-- Predicate that partial matching `M` has bottleneck cost `≤ ε`. -/ def HasCostLe {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (ε : ℝ) : Prop := 0 ≤ ε ∧ (∀ i j, M.toFun i = some j → barDist (D₁.bar i) (D₂.bar j) ≤ ε) ∧ (∀ i, M.toFun i = none → diagDist (D₁.bar i) ≤ ε) ∧ (∀ j, (∀ i, M.toFun i ≠ some j) → diagDist (D₂.bar j) ≤ ε) /-- Exact bottleneck cost of a partial matching `M`. -/ noncomputable def cost {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : ℝ := let c1 : ℝ := Finset.univ.fold max 0 fun i : Fin D₁.n => match M.toFun i with | some j => barDist (D₁.bar i) (D₂.bar j) | none => diagDist (D₁.bar i) let c2 : ℝ := Finset.univ.fold max 0 fun j : Fin D₂.n => if ∀ i : Fin D₁.n, M.toFun i ≠ some j then diagDist (D₂.bar j) else 0 max c1 c2 lemma fold_max_le {α : Type*} [DecidableEq α] (s : Finset α) (f : α → ℝ) (z ε : ℝ) (hz : z ≤ ε) (hf : ∀ a ∈ s, f a ≤ ε) : s.fold max z f ≤ ε := by induction s using Finset.induction_on with | empty => simpa using hz | @insert a s ha ih => rw [Finset.fold_insert ha] exact max_le (hf a (Finset.mem_insert_self a s)) (ih (fun b hb => hf b (Finset.mem_insert_of_mem hb))) lemma le_fold_max_init {α : Type*} [DecidableEq α] (s : Finset α) (f : α → ℝ) (z : ℝ) : z ≤ s.fold max z f := by induction s using Finset.induction_on with | empty => simp | @insert a s ha ih => rw [Finset.fold_insert ha] exact le_trans ih (le_max_right _ _) lemma le_fold_max_of_mem {α : Type*} [DecidableEq α] (s : Finset α) (f : α → ℝ) (z : ℝ) {a : α} (ha : a ∈ s) : f a ≤ s.fold max z f := by induction s using Finset.induction_on with | empty => exact absurd ha (Finset.notMem_empty a) | @insert b s hb ih => rw [Finset.fold_insert hb] rcases Finset.mem_insert.mp ha with rfl | hmem · exact le_max_left _ _ · exact le_trans (ih hmem) (le_max_right _ _) lemma cost_nonneg {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : 0 ≤ M.cost := by unfold cost exact le_trans (le_fold_max_init Finset.univ _ 0) (le_max_left _ _) lemma hasCostLe_cost {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) : M.HasCostLe M.cost := by refine ⟨M.cost_nonneg, ?_, ?_, ?_⟩ · intro i j hij have h1 := le_fold_max_of_mem Finset.univ (fun i' : Fin D₁.n => match M.toFun i' with | some j' => barDist (D₁.bar i') (D₂.bar j') | none => diagDist (D₁.bar i')) 0 (Finset.mem_univ i) rw [hij] at h1 exact le_trans h1 (le_max_left _ _) · intro i hi have h1 := le_fold_max_of_mem Finset.univ (fun i' : Fin D₁.n => match M.toFun i' with | some j' => barDist (D₁.bar i') (D₂.bar j') | none => diagDist (D₁.bar i')) 0 (Finset.mem_univ i) rw [hi] at h1 exact le_trans h1 (le_max_left _ _) · intro j hj have h2 := le_fold_max_of_mem Finset.univ (fun j' : Fin D₂.n => if ∀ i : Fin D₁.n, M.toFun i ≠ some j' then diagDist (D₂.bar j') else 0) 0 (Finset.mem_univ j) rw [if_pos hj] at h2 exact le_trans h2 (le_max_right _ _) lemma cost_le_of_hasCostLe {D₁ D₂ : Diagram} {M : PartialMatching D₁ D₂} {ε : ℝ} (h : M.HasCostLe ε) : M.cost ≤ ε := by rcases h with ⟨hε, hmatch, hun1, hun2⟩ unfold cost apply max_le · apply fold_max_le _ _ _ _ hε intro i _ cases hmi : M.toFun i with | none => exact hun1 i hmi | some j => exact hmatch i j hmi · apply fold_max_le _ _ _ _ hε intro j _ split_ifs with hj · exact hun2 j hj · exact hε lemma cost_le_iff {D₁ D₂ : Diagram} (M : PartialMatching D₁ D₂) (ε : ℝ) : M.cost ≤ ε ↔ M.HasCostLe ε := ⟨fun h => have hc := M.hasCostLe_cost ⟨le_trans hc.1 h, fun i j hij => le_trans (hc.2.1 i j hij) h, fun i hi => le_trans (hc.2.2.1 i hi) h, fun j hj => le_trans (hc.2.2.2 j hj) h⟩, cost_le_of_hasCostLe⟩ instance instFinite (D₁ D₂ : Diagram) : Finite (PartialMatching D₁ D₂) := by refine Finite.of_injective (fun M : PartialMatching D₁ D₂ => M.toFun) ?_ intro M₁ M₂ h cases M₁; cases M₂; simp only at h; subst h; rfl end PartialMatching /-- Exact bottleneck distance `d_B(D₁, D₂)` between finite persistence diagrams. -/ noncomputable def bottleneckDist (D₁ D₂ : Diagram) : ℝ := sInf {ε : ℝ | ∃ M : PartialMatching D₁ D₂, M.HasCostLe ε} /-- Attainment of bottleneck distance by an optimal finite partial matching. -/ theorem exists_optimal_bottleneck_matching (D₁ D₂ : Diagram) : ∃ M : PartialMatching D₁ D₂, M.cost = bottleneckDist D₁ D₂ := by classical let _ : Fintype (PartialMatching D₁ D₂) := Fintype.ofFinite _ have hne : (Finset.univ : Finset (PartialMatching D₁ D₂)).Nonempty := ⟨PartialMatching.empty D₁ D₂, Finset.mem_univ _⟩ obtain ⟨Mstar, _, hmin⟩ := Finset.exists_min_image Finset.univ PartialMatching.cost hne refine ⟨Mstar, le_antisymm ?_ ?_⟩ · apply le_csInf · exact ⟨Mstar.cost, Mstar, Mstar.hasCostLe_cost⟩ · rintro ε ⟨M, hM⟩ exact le_trans (hmin M (Finset.mem_univ M)) (PartialMatching.cost_le_of_hasCostLe hM) · apply csInf_le · refine ⟨0, ?_⟩ rintro ε ⟨_, hε, _⟩ exact hε · exact ⟨Mstar, Mstar.hasCostLe_cost⟩ lemma bottleneckDist_nonneg (D₁ D₂ : Diagram) : 0 ≤ bottleneckDist D₁ D₂ := by obtain ⟨M, hM⟩ := exists_optimal_bottleneck_matching D₁ D₂ rw [← hM] exact M.cost_nonneg lemma bottleneckDist_le_iff (D₁ D₂ : Diagram) (ε : ℝ) : bottleneckDist D₁ D₂ ≤ ε ↔ ∃ M : PartialMatching D₁ D₂, M.HasCostLe ε := by obtain ⟨Mstar, hMstar⟩ := exists_optimal_bottleneck_matching D₁ D₂ constructor · intro hle exact ⟨Mstar, (Mstar.cost_le_iff ε).mp (hMstar.le.trans hle)⟩ · rintro ⟨M, hM⟩ apply csInf_le · refine ⟨0, ?_⟩ rintro c ⟨_, hc, _⟩ exact hc · exact ⟨M, hM⟩ /-! ### Part 3: Explicit `ε`-Interleaving from a Partial `ε`-Matching (`d_I ≤ d_B`) -/ /-- Construct an explicit `ε`-interleaving between `diagramModule K D₁` and `diagramModule K D₂` from any partial matching `M` with `M.HasCostLe ε`. -/ noncomputable def interleavingOfMatching (K : Type u) [Field K] {D₁ D₂ : Diagram} {ε : ℝ} (M : PartialMatching D₁ D₂) (hM : M.HasCostLe ε) : RPersistenceModule.Interleaving (diagramModule K D₁) (diagramModule K D₂) ε := by classical rcases hM with ⟨hε, hmatch, hun1, hun2⟩ refine { hε := hε φ := { toFun := fun t => { toFun := fun x j ht => if hj : ∃ i : Fin D₁.n, M.toFun i = some j then let i := Classical.choose hj if hi : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d then x i ⟨hi⟩ else 0 else 0 map_add' := fun x y => by funext j ht change (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if hi : (D₁.bar (Classical.choose hj)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj)).d then x (Classical.choose hj) ⟨hi⟩ + y (Classical.choose hj) ⟨hi⟩ else 0 else 0) = (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if hi : (D₁.bar (Classical.choose hj)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj)).d then x (Classical.choose hj) ⟨hi⟩ else 0 else 0) + (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if hi : (D₁.bar (Classical.choose hj)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj)).d then y (Classical.choose hj) ⟨hi⟩ else 0 else 0) split_ifs <;> simp map_smul' := fun c x => by funext j ht change (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if hi : (D₁.bar (Classical.choose hj)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj)).d then c • x (Classical.choose hj) ⟨hi⟩ else 0 else 0) = c • (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if hi : (D₁.bar (Classical.choose hj)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj)).d then x (Classical.choose hj) ⟨hi⟩ else 0 else 0) split_ifs <;> simp } comm := fun {s t} hst => by apply LinearMap.ext; intro x; funext j ht change (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if ht_i : (D₁.bar (Classical.choose hj)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj)).d then (if hs_i : (D₁.bar (Classical.choose hj)).b ≤ s ∧ s < (D₁.bar (Classical.choose hj)).d then x (Classical.choose hj) ⟨hs_i⟩ else 0) else 0 else 0) = (if hs_j : (D₂.bar j).b ≤ s + ε ∧ s + ε < (D₂.bar j).d then (if hj : ∃ i : Fin D₁.n, M.toFun i = some j then if hs_i : (D₁.bar (Classical.choose hj)).b ≤ s ∧ s < (D₁.bar (Classical.choose hj)).d then x (Classical.choose hj) ⟨hs_i⟩ else 0 else 0) else 0) by_cases hj : ∃ i : Fin D₁.n, M.toFun i = some j · rw [dif_pos hj] set i := Classical.choose hj have hmi : M.toFun i = some j := Classical.choose_spec hj have hdist := hmatch i j hmi have hb_le : |(D₁.bar i).b - (D₂.bar j).b| ≤ ε := le_trans (le_max_left _ _) hdist have hd_le : |(D₁.bar i).d - (D₂.bar j).d| ≤ ε := le_trans (le_max_right _ _) hdist rw [abs_le] at hb_le hd_le have ht_j : (D₂.bar j).b ≤ t + ε ∧ t + ε < (D₂.bar j).d := ht.down by_cases hs_i : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d · have ht_i : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d := ⟨hs_i.1.trans hst, by linarith [ht_j.2, hd_le.1]⟩ have hs_j : (D₂.bar j).b ≤ s + ε ∧ s + ε < (D₂.bar j).d := ⟨by linarith [hs_i.1, hb_le.2], by linarith [hst, ht_j.2]⟩ rw [dif_pos ht_i, dif_pos hs_i, dif_pos hs_j, dif_pos hj, dif_pos hs_i] · by_cases ht_i : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d · rw [dif_pos ht_i, dif_neg hs_i] by_cases hs_j : (D₂.bar j).b ≤ s + ε ∧ s + ε < (D₂.bar j).d · rw [dif_pos hs_j, dif_pos hj, dif_neg hs_i] · rw [dif_neg hs_j] · rw [dif_neg ht_i] by_cases hs_j : (D₂.bar j).b ≤ s + ε ∧ s + ε < (D₂.bar j).d · rw [dif_pos hs_j, dif_pos hj, dif_neg hs_i] · rw [dif_neg hs_j] · simp [hj] } ψ := { toFun := fun t => { toFun := fun y i ht => match M.toFun i with | some j => if hj : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then y j ⟨hj⟩ else 0 | none => 0 map_add' := fun x y => by funext i ht change (match M.toFun i with | some j => if hj : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then x j ⟨hj⟩ + y j ⟨hj⟩ else 0 | none => 0) = (match M.toFun i with | some j => if hj : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then x j ⟨hj⟩ else 0 | none => 0) + (match M.toFun i with | some j => if hj : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then y j ⟨hj⟩ else 0 | none => 0) cases M.toFun i with | none => simp | some j => dsimp only; split_ifs <;> simp map_smul' := fun c x => by funext i ht change (match M.toFun i with | some j => if hj : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then c • x j ⟨hj⟩ else 0 | none => 0) = c • (match M.toFun i with | some j => if hj : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then x j ⟨hj⟩ else 0 | none => 0) cases M.toFun i with | none => simp | some j => dsimp only; split_ifs <;> simp } comm := fun {s t} hst => by apply LinearMap.ext; intro y; funext i ht change (match M.toFun i with | some j => if ht_j : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then (if hs_j : (D₂.bar j).b ≤ s ∧ s < (D₂.bar j).d then y j ⟨hs_j⟩ else 0) else 0 | none => 0) = (if hs_i : (D₁.bar i).b ≤ s + ε ∧ s + ε < (D₁.bar i).d then (match M.toFun i with | some j => if hs_j : (D₂.bar j).b ≤ s ∧ s < (D₂.bar j).d then y j ⟨hs_j⟩ else 0 | none => 0) else 0) cases hmi : M.toFun i with | none => simp | some j => dsimp only have hdist := hmatch i j hmi have hb_le : |(D₁.bar i).b - (D₂.bar j).b| ≤ ε := le_trans (le_max_left _ _) hdist have hd_le : |(D₁.bar i).d - (D₂.bar j).d| ≤ ε := le_trans (le_max_right _ _) hdist rw [abs_le] at hb_le hd_le have ht_i : (D₁.bar i).b ≤ t + ε ∧ t + ε < (D₁.bar i).d := ht.down by_cases hs_j : (D₂.bar j).b ≤ s ∧ s < (D₂.bar j).d · have ht_j : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d := ⟨hs_j.1.trans hst, by linarith [ht_i.2, hd_le.2]⟩ have hs_i : (D₁.bar i).b ≤ s + ε ∧ s + ε < (D₁.bar i).d := ⟨by linarith [hs_j.1, hb_le.1], by linarith [hst, ht_i.2]⟩ rw [dif_pos ht_j, dif_pos hs_j, dif_pos hs_i] · by_cases ht_j : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d <;> by_cases hs_i : (D₁.bar i).b ≤ s + ε ∧ s + ε < (D₁.bar i).d <;> simp [hs_j, ht_j, hs_i] } left_comp := fun t => by apply LinearMap.ext; intro x; funext i ht change (match M.toFun i with | some j => if hmid : (D₂.bar j).b ≤ t + ε ∧ t + ε < (D₂.bar j).d then (if hj_ex : ∃ i' : Fin D₁.n, M.toFun i' = some j then if hs : (D₁.bar (Classical.choose hj_ex)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj_ex)).d then x (Classical.choose hj_ex) ⟨hs⟩ else 0 else 0) else 0 | none => 0) = (if hs : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d then x i ⟨hs⟩ else 0) have ht2 : (D₁.bar i).b ≤ t + ε + ε ∧ t + ε + ε < (D₁.bar i).d := ht.down cases hmi : M.toFun i with | none => have hshort : (D₁.bar i).d - (D₁.bar i).b ≤ 2 * ε := by have := hun1 i hmi; unfold diagDist at this; linarith by_cases hs : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d · exfalso; linarith [hs.1, ht2.2, hshort] · simp [hs] | some j => dsimp only have hj_ex : ∃ i' : Fin D₁.n, M.toFun i' = some j := ⟨i, hmi⟩ have hch : Classical.choose hj_ex = i := M.inj (Classical.choose_spec hj_ex) hmi have hdist := hmatch i j hmi have hb_le : |(D₁.bar i).b - (D₂.bar j).b| ≤ ε := le_trans (le_max_left _ _) hdist have hd_le : |(D₁.bar i).d - (D₂.bar j).d| ≤ ε := le_trans (le_max_right _ _) hdist rw [abs_le] at hb_le hd_le by_cases hs : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d · have hmid : (D₂.bar j).b ≤ t + ε ∧ t + ε < (D₂.bar j).d := ⟨by linarith [hs.1, hb_le.2], by linarith [ht2.2, hd_le.1]⟩ have hs' : (D₁.bar (Classical.choose hj_ex)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj_ex)).d := by rwa [hch] rw [dif_pos hmid, dif_pos hj_ex, dif_pos hs', dif_pos hs] have hcast : ∀ (i' : Fin D₁.n) (h' : (D₁.bar i').b ≤ t ∧ t < (D₁.bar i').d), i' = i → x i' ⟨h'⟩ = x i ⟨hs⟩ := by rintro _ _ rfl; rfl exact hcast _ _ hch · rw [dif_neg hs] by_cases hmid : (D₂.bar j).b ≤ t + ε ∧ t + ε < (D₂.bar j).d · rw [dif_pos hmid, dif_pos hj_ex] have hs' : ¬ ((D₁.bar (Classical.choose hj_ex)).b ≤ t ∧ t < (D₁.bar (Classical.choose hj_ex)).d) := by rwa [hch] rw [dif_neg hs'] · rw [dif_neg hmid] right_comp := fun t => by apply LinearMap.ext; intro y; funext j ht change (if hj_ex : ∃ i : Fin D₁.n, M.toFun i = some j then if hmid : (D₁.bar (Classical.choose hj_ex)).b ≤ t + ε ∧ t + ε < (D₁.bar (Classical.choose hj_ex)).d then (match M.toFun (Classical.choose hj_ex) with | some j' => if hs : (D₂.bar j').b ≤ t ∧ t < (D₂.bar j').d then y j' ⟨hs⟩ else 0 | none => 0) else 0 else 0) = (if hs : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d then y j ⟨hs⟩ else 0) have ht2 : (D₂.bar j).b ≤ t + ε + ε ∧ t + ε + ε < (D₂.bar j).d := ht.down by_cases hj_ex : ∃ i : Fin D₁.n, M.toFun i = some j · rw [dif_pos hj_ex] set i := Classical.choose hj_ex have hmi : M.toFun i = some j := Classical.choose_spec hj_ex have hdist := hmatch i j hmi have hb_le : |(D₁.bar i).b - (D₂.bar j).b| ≤ ε := le_trans (le_max_left _ _) hdist have hd_le : |(D₁.bar i).d - (D₂.bar j).d| ≤ ε := le_trans (le_max_right _ _) hdist rw [abs_le] at hb_le hd_le by_cases hs : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d · have hmid : (D₁.bar i).b ≤ t + ε ∧ t + ε < (D₁.bar i).d := ⟨by linarith [hs.1, hb_le.1], by linarith [ht2.2, hd_le.2]⟩ rw [dif_pos hmid, hmi] · rw [dif_neg hs] by_cases hmid : (D₁.bar i).b ≤ t + ε ∧ t + ε < (D₁.bar i).d · rw [dif_pos hmid, hmi]; simp [hs] · rw [dif_neg hmid] · rw [dif_neg hj_ex] have hun : ∀ i : Fin D₁.n, M.toFun i ≠ some j := by intro i h; exact hj_ex ⟨i, h⟩ have hshort : (D₂.bar j).d - (D₂.bar j).b ≤ 2 * ε := by have := hun2 j hun; unfold diagDist at this; linarith by_cases hs : (D₂.bar j).b ≤ t ∧ t < (D₂.bar j).d · exfalso; linarith [hs.1, ht2.2, hshort] · rw [dif_neg hs] } /-- **Converse Isometry (`d_I ≤ d_B`)**: The interleaving distance between diagram persistence modules is bounded above by their bottleneck distance. -/ theorem interleavingDist_le_bottleneckDist (K : Type u) [Field K] (D₁ D₂ : Diagram) : RPersistenceModule.interleavingDist (diagramModule K D₁) (diagramModule K D₂) ≤ bottleneckDist D₁ D₂ := by obtain ⟨M, hM⟩ := exists_optimal_bottleneck_matching D₁ D₂ rw [← hM] apply csInf_le · refine ⟨0, ?_⟩ rintro ε ⟨I⟩ exact I.hε · exact ⟨interleavingOfMatching K M M.hasCostLe_cost⟩ /-! ### Part 4: Finite-Grid Interval Decomposition for Piecewise-Constant Images -/ section FiniteGridDecomp variable {K : Type u} [Field K] /-- Compact finite-poset persistence module for decomposing `imageMod φ` on the finite critical grid. -/ private structure GridMod (K : Type u) [Field K] (m : ℕ) where obj : Fin m → Type u acg : ∀ i, AddCommGroup (obj i) mod : ∀ i, Module K (obj i) fd : ∀ i, FiniteDimensional K (obj i) map : ∀ {i j : Fin m}, i ≤ j → obj i →ₗ[K] obj j map_id : ∀ i, map (le_refl i) = LinearMap.id map_comp : ∀ {i j k : Fin m} (hij : i ≤ j) (hjk : j ≤ k), (map hjk).comp (map hij) = map (hij.trans hjk) attribute [local instance] GridMod.acg GridMod.mod GridMod.fd private abbrev GridPair (m : ℕ) := {p : Fin m × Fin m // p.1 ≤ p.2} private lemma grid_modular {M : Type*} [AddCommGroup M] [Module K M] {A A' B B' : Submodule K M} (hA : A' ≤ A) (hB : B' ≤ B) : (A ⊓ B) ⊓ (A' ⊔ B') = (A' ⊓ B) ⊔ (A ⊓ B') := by apply le_antisymm · intro x ⟨⟨hxA, hxB⟩, hxAB'⟩ rcases Submodule.mem_sup.mp hxAB' with ⟨a', ha', b', hb', rfl⟩ have hb'A : b' ∈ A := by simpa using A.sub_mem hxA (hA ha') have ha'B : a' ∈ B := by simpa using B.sub_mem hxB (hB hb') exact Submodule.mem_sup.mpr ⟨a', ⟨ha', ha'B⟩, b', ⟨hb'A, hb'⟩, rfl⟩ · exact sup_le (le_inf (inf_le_inf_right B hA) (inf_le_left.trans le_sup_left)) (le_inf (inf_le_inf_left A hB) (inf_le_right.trans le_sup_right)) private def GridMod.bSub {m : ℕ} (V : GridMod K m) (b i : Fin m) (hbi : b ≤ i) : Submodule K (V.obj i) := LinearMap.range (V.map hbi) private def GridMod.bSubP {m : ℕ} (V : GridMod K m) (b i : Fin m) (hbi : b ≤ i) : Submodule K (V.obj i) := if h : 0 < b.val then LinearMap.range (V.map (show (⟨b.val - 1, by omega⟩ : Fin m) ≤ i by simp only [Fin.le_def]; have := hbi; simp only [Fin.le_def] at this; omega)) else ⊥ private def GridMod.dSub {m : ℕ} (V : GridMod K m) (i d : Fin m) (hid : i ≤ d) : Submodule K (V.obj i) := if h : d.val + 1 < m then LinearMap.ker (V.map (show i ≤ (⟨d.val + 1, h⟩ : Fin m) by simp only [Fin.le_def]; have := hid; simp only [Fin.le_def] at this; omega)) else ⊤ private def GridMod.dSubP {m : ℕ} (V : GridMod K m) (i d : Fin m) (hid : i ≤ d) : Submodule K (V.obj i) := if i < d then LinearMap.ker (V.map hid) else ⊥ private def GridMod.fSp {m : ℕ} (V : GridMod K m) (b i d : Fin m) (hbi : b ≤ i) (hid : i ≤ d) : Submodule K (V.obj i) := V.bSub b i hbi ⊓ V.dSub i d hid private def GridMod.fSpP {m : ℕ} (V : GridMod K m) (b i d : Fin m) (hbi : b ≤ i) (hid : i ≤ d) : Submodule K (V.obj i) := (V.bSubP b i hbi ⊓ V.dSub i d hid) ⊔ (V.bSub b i hbi ⊓ V.dSubP i d hid) private lemma GridMod.map_comp_apply {m : ℕ} (V : GridMod K m) {i j k : Fin m} (hij : i ≤ j) (hjk : j ≤ k) (x : V.obj i) : V.map hjk (V.map hij x) = V.map (hij.trans hjk) x := by simpa using LinearMap.congr_fun (V.map_comp hij hjk) x private lemma GridMod.map_id_apply {m : ℕ} (V : GridMod K m) (i : Fin m) (x : V.obj i) : V.map (le_refl i) x = x := by simpa using LinearMap.congr_fun (V.map_id i) x private lemma GridMod.bSubP_le {m : ℕ} (V : GridMod K m) (b i : Fin m) (hbi : b ≤ i) : V.bSubP b i hbi ≤ V.bSub b i hbi := by unfold bSubP bSub split_ifs with h · have hle : (⟨b.val - 1, by omega⟩ : Fin m) ≤ b := by simp [Fin.le_def] rintro y ⟨x, rfl⟩; exact ⟨V.map hle x, V.map_comp_apply hle hbi x⟩ · exact bot_le private lemma GridMod.dSubP_le {m : ℕ} (V : GridMod K m) (i d : Fin m) (hid : i ≤ d) : V.dSubP i d hid ≤ V.dSub i d hid := by unfold dSubP dSub split_ifs with hlt hd1 · intro x hx simp only [LinearMap.mem_ker] at hx ⊢ have hstep : d ≤ ⟨d.val + 1, hd1⟩ := by simp [Fin.le_def] have := congrArg (V.map hstep) hx rw [V.map_comp_apply, map_zero] at this; exact this · exact le_top · exact bot_le · exact bot_le private lemma GridMod.map_mem_dSub_iff {m : ℕ} (V : GridMod K m) {i j d : Fin m} (hij : i ≤ j) (hjd : j ≤ d) (x : V.obj i) : V.map hij x ∈ V.dSub j d hjd ↔ x ∈ V.dSub i d (hij.trans hjd) := by unfold dSub; split_ifs with hd <;> simp [LinearMap.mem_ker, V.map_comp_apply] private lemma GridMod.fSpP_map {m : ℕ} (V : GridMod K m) {b i d : Fin m} (hbi : b ≤ i) (hid : i ≤ d) {x : V.obj b} (hx : x ∈ V.fSpP b b d le_rfl (hbi.trans hid)) : V.map hbi x ∈ V.fSpP b i d hbi hid := by rcases Submodule.mem_sup.mp hx with ⟨x₁, hx₁, x₂, hx₂, rfl⟩ rw [map_add] apply Submodule.add_mem · apply Submodule.mem_sup_left refine ⟨?_, (V.map_mem_dSub_iff hbi hid x₁).mpr hx₁.2⟩ rcases hx₁ with ⟨hx₁b, _⟩; unfold bSubP at hx₁b ⊢ by_cases hpos : 0 < b.val · rw [dif_pos hpos] at hx₁b ⊢ rcases hx₁b with ⟨z, rfl⟩; exact ⟨z, (V.map_comp_apply _ hbi z).symm⟩ · rw [dif_neg hpos] at hx₁b ⊢ have h0 : x₁ = 0 := by simpa using hx₁b rw [h0, map_zero]; exact Submodule.zero_mem _ · apply Submodule.mem_sup_right refine ⟨⟨x₂, rfl⟩, ?_⟩ rcases hx₂ with ⟨_, hx₂d⟩; unfold dSubP at hx₂d ⊢ by_cases hbd : b < d · rw [if_pos hbd] at hx₂d change x₂ ∈ LinearMap.ker (V.map (hbi.trans hid)) at hx₂d rw [LinearMap.mem_ker] at hx₂d by_cases hid' : i < d · rw [if_pos hid'] change V.map hbi x₂ ∈ LinearMap.ker (V.map hid) rw [LinearMap.mem_ker, V.map_comp_apply]; exact hx₂d · rw [if_neg hid']; have heq : i = d := le_antisymm hid (not_lt.mp hid'); subst heq change V.map hbi x₂ = 0; exact hx₂d · rw [if_neg hbd] at hx₂d have h0 : x₂ = 0 := by simpa using hx₂d rw [h0, map_zero]; exact Submodule.zero_mem _ private lemma GridMod.fSp_quot_inj {m : ℕ} (V : GridMod K m) {b i d : Fin m} (hbi : b ≤ i) (hid : i ≤ d) {x : V.obj b} (himg : V.map hbi x ∈ V.fSpP b i d hbi hid) : x ∈ V.fSpP b b d le_rfl (hbi.trans hid) := by rcases Submodule.mem_sup.mp himg with ⟨y₁, hy₁, y₂, hy₂, hsum⟩ have hex1 : ∃ x₁ ∈ V.bSubP b b le_rfl ⊓ V.dSub b d (hbi.trans hid), V.map hbi x₁ = y₁ := by rcases hy₁ with ⟨hy₁b, hy₁d⟩; unfold bSubP at hy₁b ⊢ by_cases hpos : 0 < b.val · rw [dif_pos hpos] at hy₁b ⊢; rcases hy₁b with ⟨z, rfl⟩ have hle : (⟨b.val - 1, by omega⟩ : Fin m) ≤ b := by simp [Fin.le_def] refine ⟨V.map hle z, ⟨⟨z, rfl⟩, ?_⟩, V.map_comp_apply hle hbi z⟩ change V.map hle z ∈ V.dSub b d (hbi.trans hid) rw [← V.map_mem_dSub_iff hbi hid, V.map_comp_apply hle hbi z]; exact hy₁d · rw [dif_neg hpos] at hy₁b ⊢ have h0 : y₁ = 0 := by simpa using hy₁b rw [h0] exact ⟨0, ⟨Submodule.zero_mem _, Submodule.zero_mem _⟩, map_zero _⟩ rcases hex1 with ⟨x₁, hx₁, hx₁_map⟩ have hx₂ : x - x₁ ∈ V.bSub b b le_rfl ⊓ V.dSubP b d (hbi.trans hid) := by refine ⟨⟨x - x₁, V.map_id_apply b _⟩, ?_⟩ have hmap_sub : V.map hbi (x - x₁) = y₂ := by rw [map_sub, hx₁_map, ← hsum]; abel rcases hy₂ with ⟨_, hy₂d⟩; unfold dSubP at hy₂d ⊢ by_cases hbd : b < d · rw [if_pos hbd] change x - x₁ ∈ LinearMap.ker (V.map (hbi.trans hid)) rw [LinearMap.mem_ker] by_cases hid' : i < d · rw [if_pos hid'] at hy₂d change y₂ ∈ LinearMap.ker (V.map hid) at hy₂d rw [LinearMap.mem_ker] at hy₂d rw [← V.map_comp_apply hbi hid, hmap_sub]; exact hy₂d · rw [if_neg hid'] at hy₂d; have heq : i = d := le_antisymm hid (not_lt.mp hid'); subst heq have h0 : y₂ = 0 := by simpa using hy₂d rw [hmap_sub, h0] · have hbi_eq : b = i := le_antisymm hbi (hid.trans (le_antisymm (hbi.trans hid) (not_lt.mp hbd)).symm.le) have hid_eq : i = d := le_antisymm hid (not_lt.mp (fun hlt => hbd (lt_of_le_of_lt hbi hlt))) subst hbi_eq; subst hid_eq; rw [if_neg (lt_irrefl b)] at hy₂d ⊢ rw [V.map_id_apply] at hmap_sub; rw [hmap_sub]; exact hy₂d have hdec : x = x₁ + (x - x₁) := by abel rw [hdec]; exact Submodule.add_mem _ (Submodule.mem_sup_left hx₁) (Submodule.mem_sup_right hx₂) private noncomputable def GridMod.cSub {m : ℕ} (V : GridMod K m) (p : GridPair m) : Submodule K (V.obj p.1.1) := by let F := V.fSp p.1.1 p.1.1 p.1.2 le_rfl p.2 let Fm := V.fSpP p.1.1 p.1.1 p.1.2 le_rfl p.2 exact (Classical.choose (Submodule.exists_isCompl (Fm.comap F.subtype))).map F.subtype private lemma GridMod.cSub_le {m : ℕ} (V : GridMod K m) (p : GridPair m) : V.cSub p ≤ V.fSp p.1.1 p.1.1 p.1.2 le_rfl p.2 := by rintro x ⟨y, _, rfl⟩; exact y.2 private lemma GridMod.cSub_disjoint {m : ℕ} (V : GridMod K m) (p : GridPair m) : Disjoint (V.fSpP p.1.1 p.1.1 p.1.2 le_rfl p.2) (V.cSub p) := by unfold cSub set F := V.fSp p.1.1 p.1.1 p.1.2 le_rfl p.2 set Fm := V.fSpP p.1.1 p.1.1 p.1.2 le_rfl p.2 have hc := Classical.choose_spec (Submodule.exists_isCompl (Fm.comap F.subtype)) rw [Submodule.disjoint_def]; intro x hxFm ⟨y, hyW, hyx⟩ subst hyx have hy0 : y = 0 := Submodule.disjoint_def.mp hc.disjoint y hxFm hyW simp [hy0] private lemma GridMod.cSub_sup {m : ℕ} (V : GridMod K m) (p : GridPair m) : V.fSpP p.1.1 p.1.1 p.1.2 le_rfl p.2 ⊔ V.cSub p = V.fSp p.1.1 p.1.1 p.1.2 le_rfl p.2 := by unfold cSub set F := V.fSp p.1.1 p.1.1 p.1.2 le_rfl p.2 set Fm := V.fSpP p.1.1 p.1.1 p.1.2 le_rfl p.2 have hc := Classical.choose_spec (Submodule.exists_isCompl (Fm.comap F.subtype)) apply le_antisymm · refine sup_le (sup_le (inf_le_inf_right _ (V.bSubP_le _ _ _)) (inf_le_inf_left _ (V.dSubP_le _ _ _))) ?_ rintro x ⟨y, _, rfl⟩; exact y.2 · intro x hxF have ht : (⟨x, hxF⟩ : F) ∈ Fm.comap F.subtype ⊔ Classical.choose (Submodule.exists_isCompl (Fm.comap F.subtype)) := by rw [hc.sup_eq_top]; trivial rcases Submodule.mem_sup.mp ht with ⟨u, hu, w, hw, huw⟩ have hx_eq : x = u.1 + w.1 := (congrArg Subtype.val huw).symm rw [hx_eq] exact Submodule.add_mem _ (Submodule.mem_sup_left hu) (Submodule.mem_sup_right ⟨w, hw, rfl⟩) /-- Every `GridMod K m` decomposes into a finite family of intervals `[a_k, c_k]` on `Fin m`. -/ private theorem gridMod_exists_decomp {m : ℕ} (V : GridMod K m) : ∃ (N : ℕ) (a c : Fin N → Fin m) (_hac : ∀ k, a k ≤ c k) (e : ∀ i : Fin m, (∀ k : Fin N, PLift (a k ≤ i ∧ i ≤ c k) → K) ≃ₗ[K] V.obj i), ∀ {i j : Fin m} (hij : i ≤ j) (x : ∀ k : Fin N, PLift (a k ≤ i ∧ i ≤ c k) → K), e j (fun k _ => if hi : a k ≤ i ∧ i ≤ c k then x k ⟨hi⟩ else 0) = V.map hij (e i x) := by classical let idxType := Σ p : GridPair m, Fin (Module.finrank K (V.cSub p)) let N := Fintype.card idxType let eqv : Fin N ≃ idxType := (Fintype.equivFin idxType).symm let a : Fin N → Fin m := fun k => (eqv k).1.1.1 let c : Fin N → Fin m := fun k => (eqv k).1.1.2 have hac : ∀ k, a k ≤ c k := fun k => (eqv k).1.2 let basisOf : ∀ p : GridPair m, (Fin (Module.finrank K (V.cSub p)) → K) ≃ₗ[K] V.cSub p := fun p => (Module.finBasis K (V.cSub p)).equivFun.symm let Φ : ∀ i : Fin m, (∀ k : Fin N, PLift (a k ≤ i ∧ i ≤ c k) → K) →ₗ[K] V.obj i := fun i => { toFun := fun x => ∑ p : GridPair m, if h : p.1.1 ≤ i ∧ i ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩)).1) else 0 map_add' := fun x y => by rw [← Finset.sum_add_distrib]; apply Finset.sum_congr rfl; intro p _ split_ifs with h · have ha : (fun r => (x + y) (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩) = (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩) + (fun r => y (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩) := rfl rw [ha, map_add, Submodule.coe_add, map_add] · simp map_smul' := fun s x => by simp only [RingHom.id_apply, Finset.smul_sum]; apply Finset.sum_congr rfl; intro p _ split_ifs with h · have hs : (fun r => (s • x) (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩) = s • (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩) := rfl rw [hs, map_smul, SetLike.val_smul, map_smul] · simp } have hsingle : ∀ (i : Fin m) (p0 : GridPair m) (hi : p0.1.1 ≤ i ∧ i ≤ p0.1.2) (w : V.cSub p0), ∃ x, Φ i x = V.map hi.1 w.1 := by intro i p0 hi w let coords := (basisOf p0).symm w refine ⟨fun k _ => if h : (eqv k).1 = p0 then coords (Fin.cast (by rw [h]) (eqv k).2) else 0, ?_⟩ simp only [Φ, LinearMap.coe_mk, AddHom.coe_mk] rw [Finset.sum_eq_single p0] · rw [dif_pos hi] have hf : (fun r => if h : (eqv (eqv.symm ⟨p0, r⟩)).1 = p0 then coords (Fin.cast (by rw [h]) (eqv (eqv.symm ⟨p0, r⟩)).2) else 0) = coords := by funext r have heq : eqv (eqv.symm ⟨p0, r⟩) = ⟨p0, r⟩ := eqv.apply_symm_apply ⟨p0, r⟩ generalize hz : eqv (eqv.symm ⟨p0, r⟩) = z at heq ⊢ subst heq; simp rw [hf, LinearEquiv.apply_symm_apply] · intro p _ hp split_ifs with hpi · have hf : (fun r : Fin (Module.finrank K (V.cSub p)) => if h : (eqv (eqv.symm ⟨p, r⟩)).1 = p0 then coords (Fin.cast (by rw [h]) (eqv (eqv.symm ⟨p, r⟩)).2) else 0) = 0 := by funext r; simp [hp] rw [hf, map_zero, Submodule.coe_zero, map_zero] · rfl · intro h; exact absurd (Finset.mem_univ p0) h have hinj : ∀ i : Fin m, Function.Injective (Φ i) := by intro i rw [injective_iff_map_eq_zero] intro x hx by_contra hne let S : Finset (GridPair m) := Finset.univ.filter fun p => ∃ h : p.1.1 ≤ i ∧ i ≤ p.1.2, (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩) ≠ 0 have hSne : S.Nonempty := by by_contra hemp; rw [Finset.not_nonempty_iff_eq_empty] at hemp apply hne; funext k ⟨hk⟩ change x k ⟨hk⟩ = 0 set pr := eqv k with hpr have hp : pr.1 ∉ S := by rw [hemp]; exact Finset.notMem_empty _ simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, not_exists] at hp have hk' : pr.1.1.1 ≤ i ∧ i ≤ pr.1.1.2 := hk have h0 := not_ne_iff.mp (hp hk') have h0r : x (eqv.symm ⟨pr.1, pr.2⟩) ⟨by simpa [a, c] using hk'⟩ = 0 := congrFun h0 pr.2 have hkeq : eqv.symm ⟨pr.1, pr.2⟩ = k := by rw [Sigma.eta, hpr, Equiv.symm_apply_apply] have hsub : ∀ (k' : Fin N) (hk'' : a k' ≤ i ∧ i ≤ c k'), k' = k → x k' ⟨hk''⟩ = 0 → x k ⟨hk⟩ = 0 := by rintro _ _ rfl h; exact h exact hsub _ _ hkeq h0r let key : GridPair m → ℕ := fun p => p.1.2.val * m + p.1.1.val obtain ⟨pstar, hpstar_mem, hpstar_max⟩ := Finset.exists_max_image S key hSne simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hpstar_mem rcases hpstar_mem with ⟨hstar, hstar_ne⟩ let wstar : V.cSub pstar := basisOf pstar (fun r => x (eqv.symm ⟨pstar, r⟩) ⟨by simpa [a, c] using hstar⟩) have hwstar_ne : wstar.1 ≠ 0 := by intro h0; apply hstar_ne exact (basisOf pstar).injective (by simpa [wstar] using Subtype.ext (show wstar.1 = (0 : V.cSub pstar).1 from h0)) have hx_sum : ∑ p : GridPair m, (if h : p.1.1 ≤ i ∧ i ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩)).1) else 0) = 0 := hx have hterm : V.map hstar.1 wstar.1 = -(∑ p ∈ Finset.univ.erase pstar, if h : p.1.1 ≤ i ∧ i ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩)).1) else 0) := by have hsp := Finset.add_sum_erase Finset.univ (fun p : GridPair m => if h : p.1.1 ≤ i ∧ i ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩)).1) else 0) (Finset.mem_univ pstar) rw [dif_pos hstar] at hsp; rw [hx_sum] at hsp exact eq_neg_of_add_eq_zero_left hsp have hother : (∑ p ∈ Finset.univ.erase pstar, if h : p.1.1 ≤ i ∧ i ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩)).1) else 0) ∈ V.bSubP pstar.1.1 i hstar.1 ⊔ V.dSubP i pstar.1.2 hstar.2 := by apply Submodule.sum_mem; intro p hp_er split_ifs with hp · by_cases hp0 : (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using hp⟩) = 0 · rw [hp0, map_zero, Submodule.coe_zero, map_zero]; exact Submodule.zero_mem _ · have hpS : p ∈ S := by simp only [S, Finset.mem_filter, Finset.mem_univ, true_and]; exact ⟨hp, hp0⟩ have hle := hpstar_max p hpS have hpne := Finset.ne_of_mem_erase hp_er have hF : V.map hp.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using hp⟩)).1) ∈ V.fSp p.1.1 i p.1.2 hp.1 hp.2 := ⟨⟨_, rfl⟩, (V.map_mem_dSub_iff hp.1 hp.2 _).mpr (V.cSub_le p (basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using hp⟩)).2).2⟩ have hlt : p.1.2 < pstar.1.2 ∨ (p.1.2 = pstar.1.2 ∧ p.1.1 < pstar.1.1) := by have := p.1.1.isLt; have := pstar.1.1.isLt by_cases hd : p.1.2.val < pstar.1.2.val · exact Or.inl hd · have hdeq : p.1.2.val = pstar.1.2.val := by have : p.1.2.val * m + p.1.1.val ≤ pstar.1.2.val * m + pstar.1.1.val := hle nlinarith have hblt : p.1.1.val < pstar.1.1.val := by have : p.1.1.val ≤ pstar.1.1.val := by have : p.1.2.val * m + p.1.1.val ≤ pstar.1.2.val * m + pstar.1.1.val := hle nlinarith by_contra hge apply hpne exact Subtype.ext (Prod.ext (Fin.ext (by omega)) (Fin.ext hdeq)) exact Or.inr ⟨Fin.ext hdeq, hblt⟩ rcases hlt with hdlt | ⟨_, hblt⟩ · apply Submodule.mem_sup_right have hd1 : p.1.2.val + 1 < m := by have : p.1.2.val < pstar.1.2.val := hdlt; have := pstar.1.2.isLt; omega have hstep : (⟨p.1.2.val + 1, hd1⟩ : Fin m) ≤ pstar.1.2 := by simp only [Fin.le_def]; have : p.1.2.val < pstar.1.2.val := hdlt; omega have hker := hF.2; unfold GridMod.dSub at hker; rw [dif_pos hd1] at hker unfold GridMod.dSubP; rw [if_pos (lt_of_le_of_lt hp.2 hdlt)] simp only [LinearMap.mem_ker] at hker ⊢ have := congrArg (V.map hstep) hker; rw [V.map_comp_apply, map_zero] at this; exact this · apply Submodule.mem_sup_left have hpos : 0 < pstar.1.1.val := by have : p.1.1.val < pstar.1.1.val := hblt; omega have hstep : p.1.1 ≤ ⟨pstar.1.1.val - 1, by omega⟩ := by simp only [Fin.le_def]; have : p.1.1.val < pstar.1.1.val := hblt; omega unfold GridMod.bSubP; rw [dif_pos hpos] rcases hF.1 with ⟨z, hz⟩ exact ⟨V.map hstep z, by rw [V.map_comp_apply]; exact hz⟩ · exact Submodule.zero_mem _ have himgF : V.map hstar.1 wstar.1 ∈ V.fSp pstar.1.1 i pstar.1.2 hstar.1 hstar.2 := ⟨⟨_, rfl⟩, (V.map_mem_dSub_iff hstar.1 hstar.2 _).mpr (V.cSub_le pstar wstar.2).2⟩ have himgFm : V.map hstar.1 wstar.1 ∈ V.fSpP pstar.1.1 i pstar.1.2 hstar.1 hstar.2 := by have hg := grid_modular (K := K) (V.bSubP_le pstar.1.1 i hstar.1) (V.dSubP_le i pstar.1.2 hstar.2) change V.map hstar.1 wstar.1 ∈ (V.bSubP pstar.1.1 i hstar.1 ⊓ V.dSub i pstar.1.2 hstar.2) ⊔ (V.bSub pstar.1.1 i hstar.1 ⊓ V.dSubP i pstar.1.2 hstar.2) rw [← hg]; exact ⟨himgF, by rw [hterm]; exact Submodule.neg_mem _ hother⟩ have hwFm := V.fSp_quot_inj hstar.1 hstar.2 himgFm exact hwstar_ne (Submodule.disjoint_def.mp (V.cSub_disjoint pstar) wstar.1 hwFm wstar.2) have hsurj : ∀ i : Fin m, Function.Surjective (Φ i) := by intro i have hfilt : ∀ (dV : ℕ) (hd : dV < m) (hid : i ≤ ⟨dV, hd⟩) (bV : ℕ) (hb : bV < m) (hbi : ⟨bV, hb⟩ ≤ i), V.fSp ⟨bV, hb⟩ i ⟨dV, hd⟩ hbi hid ≤ LinearMap.range (Φ i) := by intro dV induction dV with | zero => intro hd hid bV induction bV with | zero => intro hb hbi y hy rcases hy.1 with ⟨x, rfl⟩ let p0 : GridPair m := ⟨(⟨0, hb⟩, ⟨0, hd⟩), hbi.trans hid⟩ have hxF : x ∈ V.fSp p0.1.1 p0.1.1 p0.1.2 le_rfl p0.2 := ⟨⟨x, V.map_id_apply _ x⟩, (V.map_mem_dSub_iff hbi hid x).mp hy.2⟩ have hbot : V.fSpP p0.1.1 p0.1.1 p0.1.2 le_rfl p0.2 = ⊥ := by unfold GridMod.fSpP GridMod.bSubP GridMod.dSubP; simp [p0] have hxW : x ∈ V.cSub p0 := by have hs := V.cSub_sup p0; rw [hbot, bot_sup_eq] at hs; rw [hs]; exact hxF exact hsingle i p0 ⟨hbi, hid⟩ ⟨x, hxW⟩ | succ bV _ => intro hb hbi; exfalso; have : bV + 1 ≤ i.val := hbi; have : i.val ≤ 0 := hid; omega | succ dV ih_d => intro hd hid bV induction bV with | zero => intro hb hbi y hy rcases hy.1 with ⟨x, rfl⟩ let p0 : GridPair m := ⟨(⟨0, hb⟩, ⟨dV + 1, hd⟩), hbi.trans hid⟩ have hxF : x ∈ V.fSp p0.1.1 p0.1.1 p0.1.2 le_rfl p0.2 := ⟨⟨x, V.map_id_apply _ x⟩, (V.map_mem_dSub_iff hbi hid x).mp hy.2⟩ have hx_sup : x ∈ V.fSpP p0.1.1 p0.1.1 p0.1.2 le_rfl p0.2 ⊔ V.cSub p0 := by rw [V.cSub_sup p0]; exact hxF rcases Submodule.mem_sup.mp hx_sup with ⟨x₁, hx₁, x₂, hx₂, rfl⟩ rw [map_add]; apply Submodule.add_mem · rcases Submodule.mem_sup.mp (V.fSpP_map hbi hid hx₁) with ⟨u₁, hu₁, u₂, hu₂, hueq⟩ rw [← hueq]; apply Submodule.add_mem · have : u₁ = 0 := by have h := hu₁.1; unfold GridMod.bSubP at h; simpa using h rw [this]; exact Submodule.zero_mem _ · rcases hu₂ with ⟨hu₂b, hu₂d⟩; unfold GridMod.dSubP at hu₂d by_cases hilt : i < ⟨dV + 1, hd⟩ · rw [if_pos hilt] at hu₂d have hid' : i ≤ ⟨dV, by omega⟩ := by simp only [Fin.le_def]; have : i.val < dV + 1 := hilt; omega exact ih_d (by omega) hid' 0 hb hbi ⟨hu₂b, by unfold GridMod.dSub; rw [dif_pos hd]; exact hu₂d⟩ · rw [if_neg hilt] at hu₂d have h0 : u₂ = 0 := by simpa using hu₂d rw [h0]; exact Submodule.zero_mem _ · exact hsingle i p0 ⟨hbi, hid⟩ ⟨x₂, hx₂⟩ | succ bV ih_b => intro hb hbi y hy rcases hy.1 with ⟨x, rfl⟩ let p0 : GridPair m := ⟨(⟨bV + 1, hb⟩, ⟨dV + 1, hd⟩), hbi.trans hid⟩ have hxF : x ∈ V.fSp p0.1.1 p0.1.1 p0.1.2 le_rfl p0.2 := ⟨⟨x, V.map_id_apply _ x⟩, (V.map_mem_dSub_iff hbi hid x).mp hy.2⟩ have hx_sup : x ∈ V.fSpP p0.1.1 p0.1.1 p0.1.2 le_rfl p0.2 ⊔ V.cSub p0 := by rw [V.cSub_sup p0]; exact hxF rcases Submodule.mem_sup.mp hx_sup with ⟨x₁, hx₁, x₂, hx₂, rfl⟩ rw [map_add]; apply Submodule.add_mem · rcases Submodule.mem_sup.mp (V.fSpP_map hbi hid hx₁) with ⟨u₁, hu₁, u₂, hu₂, hueq⟩ rw [← hueq]; apply Submodule.add_mem · have hbi' : ⟨bV, by omega⟩ ≤ i := by simp only [Fin.le_def]; have : bV + 1 ≤ i.val := hbi; omega have h1 := hu₁.1; unfold GridMod.bSubP at h1; rw [dif_pos (Nat.succ_pos bV)] at h1 exact ih_b (by omega) hbi' ⟨h1, hu₁.2⟩ · rcases hu₂ with ⟨hu₂b, hu₂d⟩; unfold GridMod.dSubP at hu₂d by_cases hilt : i < ⟨dV + 1, hd⟩ · rw [if_pos hilt] at hu₂d have hid' : i ≤ ⟨dV, by omega⟩ := by simp only [Fin.le_def]; have : i.val < dV + 1 := hilt; omega exact ih_d (by omega) hid' (bV + 1) hb hbi ⟨hu₂b, by unfold GridMod.dSub; rw [dif_pos hd]; exact hu₂d⟩ · rw [if_neg hilt] at hu₂d have h0 : u₂ = 0 := by simpa using hu₂d rw [h0]; exact Submodule.zero_mem _ · exact hsingle i p0 ⟨hbi, hid⟩ ⟨x₂, hx₂⟩ intro y have hm : 0 < m := Nat.lt_of_le_of_lt (Nat.zero_le i.val) i.isLt have hid : i ≤ ⟨m - 1, by omega⟩ := by simp [Fin.le_def]; have := i.isLt; omega have hyF : y ∈ V.fSp i i ⟨m - 1, by omega⟩ le_rfl hid := by refine ⟨⟨y, V.map_id_apply i y⟩, ?_⟩ unfold GridMod.dSub have hnot : ¬ (m - 1 + 1 < m) := by omega rw [dif_neg hnot]; trivial exact hfilt (m - 1) (by omega) hid i.val i.isLt le_rfl hyF refine ⟨N, a, c, hac, fun i => LinearEquiv.ofBijective (Φ i) ⟨hinj i, hsurj i⟩, ?_⟩ intro i j hij x change ∑ p : GridPair m, (if h : p.1.1 ≤ j ∧ j ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => if hi' : a (eqv.symm ⟨p, r⟩) ≤ i ∧ i ≤ c (eqv.symm ⟨p, r⟩) then x (eqv.symm ⟨p, r⟩) ⟨hi'⟩ else 0)).1) else 0) = V.map hij (∑ p : GridPair m, if h : p.1.1 ≤ i ∧ i ≤ p.1.2 then V.map h.1 ((basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using h⟩)).1) else 0) rw [map_sum] apply Finset.sum_congr rfl; intro p _ by_cases hi : p.1.1 ≤ i ∧ i ≤ p.1.2 · by_cases hj : p.1.1 ≤ j ∧ j ≤ p.1.2 · rw [dif_pos hj, dif_pos hi, V.map_comp_apply hi.1 hij] have harg : (fun r => if hi' : a (eqv.symm ⟨p, r⟩) ≤ i ∧ i ≤ c (eqv.symm ⟨p, r⟩) then x (eqv.symm ⟨p, r⟩) ⟨hi'⟩ else 0) = (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using hi⟩) := by funext r; have : a (eqv.symm ⟨p, r⟩) ≤ i ∧ i ≤ c (eqv.symm ⟨p, r⟩) := by simpa [a, c] using hi simp [this] rw [harg] · rw [dif_neg hj, dif_pos hi, V.map_comp_apply hi.1 hij] have hdj : p.1.2 < j := by by_contra hle; exact hj ⟨hi.1.trans hij, not_lt.mp hle⟩ let w : V.cSub p := basisOf p (fun r => x (eqv.symm ⟨p, r⟩) ⟨by simpa [a, c] using hi⟩) have hxD := (V.cSub_le p w.2).2 unfold GridMod.dSub at hxD have hd1 : p.1.2.val + 1 < m := by have : p.1.2.val < j.val := hdj; have := j.isLt; omega rw [dif_pos hd1] at hxD have hxKer : V.map (show p.1.1 ≤ ⟨p.1.2.val + 1, hd1⟩ by simp only [Fin.le_def]; have : p.1.1.val ≤ p.1.2.val := p.2; omega) w.1 = 0 := LinearMap.mem_ker.mp hxD have hstep : (⟨p.1.2.val + 1, hd1⟩ : Fin m) ≤ j := by simp only [Fin.le_def]; have : p.1.2.val < j.val := hdj; omega have := congrArg (V.map hstep) hxKer; rw [V.map_comp_apply, map_zero] at this exact this.symm · rw [dif_neg hi, map_zero] split_ifs with hj · have harg : (fun r => if hi' : a (eqv.symm ⟨p, r⟩) ≤ i ∧ i ≤ c (eqv.symm ⟨p, r⟩) then x (eqv.symm ⟨p, r⟩) ⟨hi'⟩ else 0) = 0 := by funext r; have : ¬ (a (eqv.symm ⟨p, r⟩) ≤ i ∧ i ≤ c (eqv.symm ⟨p, r⟩)) := by simpa [a, c] using hi simp [this] rw [harg, map_zero, Submodule.coe_zero, map_zero] · rfl /-- Any morphism `φ : diagramModule K D₁ → (diagramModule K D₂).shift ε` has a finite-barcode image module `im(φ) ≅ diagramModule K D_im`. -/ lemma exists_diagram_iso_imageMod {D₁ D₂ : Diagram} {ε : ℝ} (φ : RPersistenceModule.Hom (diagramModule K D₁) ((diagramModule K D₂).shift ε)) : ∃ D_im : Diagram, Nonempty (RPersistenceModule.Iso (diagramModule K D_im) (RPersistenceModule.imageMod φ)) := by classical -- Special case: if D₁ is empty, im(φ) is zero everywhere by_cases hD1 : D₁.n = 0 · refine ⟨Diagram.empty, ⟨?_⟩⟩ refine { equiv := fun t => { toFun := fun _ => 0 map_add' := fun _ _ => (add_zero 0).symm map_smul' := fun _ _ => (smul_zero _).symm invFun := fun _ k => Fin.elim0 k left_inv := fun x => funext fun k => Fin.elim0 k right_inv := fun ⟨y, ⟨x, hxy⟩⟩ => by have hx0 : x = 0 := funext fun i => Fin.elim0 (Fin.cast hD1 i) apply Subtype.ext change (0 : _) = y rw [← hxy, hx0, map_zero] } comm := fun _ => by apply LinearMap.ext; intro x; apply Subtype.ext change (0 : _) = ((diagramModule K D₂).shift ε).map _ 0 rw [map_zero] } -- Collect all endpoints of D₁ and D₂.shift ε into a finite sorted grid `t : Fin m → ℝ` let S : Finset ℝ := (Finset.univ.image fun i : Fin D₁.n => (D₁.bar i).b) ∪ (Finset.univ.image fun i : Fin D₁.n => (D₁.bar i).d) ∪ (Finset.univ.image fun j : Fin D₂.n => (D₂.bar j).b - ε) ∪ (Finset.univ.image fun j : Fin D₂.n => (D₂.bar j).d - ε) have hm2 : 2 ≤ S.card := by let i0 : Fin D₁.n := ⟨0, Nat.pos_of_ne_zero hD1⟩ have hb : (D₁.bar i0).b ∈ S := Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ i0)))) have hd : (D₁.bar i0).d ∈ S := Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ i0)))) have hne : (D₁.bar i0).b ≠ (D₁.bar i0).d := ne_of_lt (D₁.bar i0).hlt have hsub : ({(D₁.bar i0).b, (D₁.bar i0).d} : Finset ℝ) ⊆ S := by intro x hx; rcases Finset.mem_insert.mp hx with rfl | hx' · exact hb · rw [Finset.mem_singleton.mp hx']; exact hd calc 2 = ({(D₁.bar i0).b, (D₁.bar i0).d} : Finset ℝ).card := (Finset.card_pair hne).symm _ ≤ S.card := Finset.card_le_card hsub let m := S.card let tIso : Fin m ≃o S := S.orderIsoOfFin rfl let t : Fin m → ℝ := fun k => (tIso k).1 have ht_strict : StrictMono t := fun k₁ k₂ h => tIso.strictMono h have ht_mem : ∀ k, t k ∈ S := fun k => (tIso k).2 have hS_range : ∀ x ∈ S, ∃ k : Fin m, t k = x := fun x hx => ⟨tIso.symm ⟨x, hx⟩, congrArg Subtype.val (tIso.apply_symm_apply ⟨x, hx⟩)⟩ -- Key property: no point of S lies strictly between t k and t (k + 1) have hno_between : ∀ (k : Fin (m - 1)) (x : ℝ), x ∈ S → x ≤ t ⟨k.val, by omega⟩ ∨ t ⟨k.val + 1, by omega⟩ ≤ x := by intro k x hx obtain ⟨r, rfl⟩ := hS_range x hx by_cases hr : r.val ≤ k.val · exact Or.inl (ht_strict.monotone (show r ≤ ⟨k.val, by omega⟩ from hr)) · exact Or.inr (ht_strict.monotone (show (⟨k.val + 1, by omega⟩ : Fin m) ≤ r by simp only [Fin.le_def]; omega)) -- On every cell [t k, t (k+1)), the transition map of im(φ) is a bijection have hcell_bij : ∀ (k : Fin (m - 1)) (s : ℝ) (hs1 : t ⟨k.val, by omega⟩ ≤ s) (hs2 : s < t ⟨k.val + 1, by omega⟩), Function.Bijective ((RPersistenceModule.imageMod φ).map hs1) := by intro k s hs1 hs2 -- Both U and V[ε] have bijective transition maps from t k to s have hU_iff : ∀ i : Fin D₁.n, ((D₁.bar i).b ≤ t ⟨k.val, by omega⟩ ∧ t ⟨k.val, by omega⟩ < (D₁.bar i).d) ↔ ((D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d) := by intro i have hbS : (D₁.bar i).b ∈ S := Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ i)))) have hdS : (D₁.bar i).d ∈ S := Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ i)))) have hbc := hno_between k _ hbS have hdc := hno_between k _ hdS constructor <;> rintro ⟨h1, h2⟩ <;> rcases hbc with hbc | hbc <;> rcases hdc with hdc | hdc <;> constructor <;> linarith have hV_iff : ∀ j : Fin D₂.n, ((D₂.bar j).b ≤ t ⟨k.val, by omega⟩ + ε ∧ t ⟨k.val, by omega⟩ + ε < (D₂.bar j).d) ↔ ((D₂.bar j).b ≤ s + ε ∧ s + ε < (D₂.bar j).d) := by intro j have hbS : (D₂.bar j).b - ε ∈ S := Finset.mem_union_left _ (Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ j))) have hdS : (D₂.bar j).d - ε ∈ S := Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ j)) have hbc := hno_between k _ hbS have hdc := hno_between k _ hdS constructor <;> rintro ⟨h1, h2⟩ <;> rcases hbc with hbc | hbc <;> rcases hdc with hdc | hdc <;> constructor <;> linarith have hU_surj : Function.Surjective ((diagramModule K D₁).map hs1) := by intro x refine ⟨fun i ⟨hi⟩ => x i ⟨(hU_iff i).mp hi⟩, ?_⟩ funext i ⟨hs_i⟩ change (if hs : (D₁.bar i).b ≤ t ⟨k.val, by omega⟩ ∧ t ⟨k.val, by omega⟩ < (D₁.bar i).d then x i ⟨(hU_iff i).mp hs⟩ else 0) = x i ⟨hs_i⟩ simp [(hU_iff i).mpr hs_i] have hV_inj : Function.Injective (((diagramModule K D₂).shift ε).map hs1) := by intro y₁ y₂ heq funext j ⟨hj⟩ have hj' := (hV_iff j).mp hj have h := congrFun (congrFun heq j) ⟨hj'⟩ change (if hs : (D₂.bar j).b ≤ t ⟨k.val, by omega⟩ + ε ∧ t ⟨k.val, by omega⟩ + ε < (D₂.bar j).d then y₁ j ⟨hs⟩ else 0) = (if hs : (D₂.bar j).b ≤ t ⟨k.val, by omega⟩ + ε ∧ t ⟨k.val, by omega⟩ + ε < (D₂.bar j).d then y₂ j ⟨hs⟩ else 0) at h rwa [dif_pos hj, dif_pos hj] at h constructor · intro ⟨y₁, hy₁⟩ ⟨y₂, hy₂⟩ heq apply Subtype.ext exact hV_inj (congrArg Subtype.val heq) · rintro ⟨y, ⟨x, hxy⟩⟩ subst hxy obtain ⟨x₀, rfl⟩ := hU_surj x refine ⟨⟨φ.toFun (t ⟨k.val, by omega⟩) x₀, ⟨x₀, rfl⟩⟩, ?_⟩ apply Subtype.ext exact (LinearMap.congr_fun (φ.comm hs1) x₀).symm -- Outside [t 0, t (m - 1)), im(φ) is zero have hout_zero : ∀ (s : ℝ), s < t ⟨0, by omega⟩ ∨ t ⟨m - 1, by omega⟩ ≤ s → ∀ y : (RPersistenceModule.imageMod φ).obj s, y = 0 := by intro s hs ⟨y, ⟨x, hxy⟩⟩ subst hxy have hx0 : x = 0 := by funext i ⟨hi⟩ exfalso have hbS : (D₁.bar i).b ∈ S := Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ i)))) have hdS : (D₁.bar i).d ∈ S := Finset.mem_union_left _ (Finset.mem_union_left _ (Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ i)))) obtain ⟨kb, hkb⟩ := hS_range _ hbS obtain ⟨kd, hkd⟩ := hS_range _ hdS rcases hs with hs_lt | hs_ge · have : t ⟨0, by omega⟩ ≤ t kb := ht_strict.monotone (show (⟨0, by omega⟩ : Fin m) ≤ kb by simp [Fin.le_def]) linarith [hi.1, hkb] · have : t kd ≤ t ⟨m - 1, by omega⟩ := ht_strict.monotone (show kd ≤ ⟨m - 1, by omega⟩ by simp [Fin.le_def]; omega) linarith [hi.2, hkd] apply Subtype.ext; change φ.toFun s x = 0; rw [hx0, map_zero] -- Build the finite GridMod on `Fin (m - 1)` let G : GridMod K (m - 1) := { obj := fun k => (RPersistenceModule.imageMod φ).obj (t ⟨k.val, by omega⟩) acg := fun _ => inferInstance mod := fun _ => inferInstance fd := fun k => by have : FiniteDimensional K (((diagramModule K D₂).shift ε).obj (t ⟨k.val, by omega⟩)) := finiteDimensional_diagramModule K D₂ (t ⟨k.val, by omega⟩ + ε) change FiniteDimensional K (LinearMap.range (φ.toFun (t ⟨k.val, by omega⟩))) infer_instance map := fun {i j} hij => (RPersistenceModule.imageMod φ).map (ht_strict.monotone (show (⟨i.val, by omega⟩ : Fin m) ≤ ⟨j.val, by omega⟩ from hij)) map_id := fun k => (RPersistenceModule.imageMod φ).map_id _ map_comp := fun hij hjk => (RPersistenceModule.imageMod φ).map_comp _ _ } obtain ⟨N, a, c, hac, eG, heG_comm⟩ := gridMod_exists_decomp G -- Construct real diagram D_im with bars [t (a k), t (c k + 1)) let D_im : Diagram := { n := N bar := fun k => ⟨t ⟨(a k).val, by omega⟩, t ⟨(c k).val + 1, by omega⟩, ht_strict (show (⟨(a k).val, by omega⟩ : Fin m) < ⟨(c k).val + 1, by omega⟩ by simp only [Fin.lt_def]; have : (a k).val ≤ (c k).val := hac k; omega)⟩ } -- Helper: locate any s ∈ [t 0, t (m - 1)) in its unique cell k : Fin (m - 1) have hlocate : ∀ s : ℝ, t ⟨0, by omega⟩ ≤ s → s < t ⟨m - 1, by omega⟩ → ∃ k : Fin (m - 1), t ⟨k.val, by omega⟩ ≤ s ∧ s < t ⟨k.val + 1, by omega⟩ := by intro s hs0 hsm let P : Finset (Fin m) := Finset.univ.filter fun r => t r ≤ s have hPne : P.Nonempty := ⟨⟨0, by omega⟩, by simp [P, hs0]⟩ obtain ⟨r, hr_mem, hr_max⟩ := Finset.exists_max_image P Fin.val hPne simp only [P, Finset.mem_filter, Finset.mem_univ, true_and] at hr_mem have hr_lt : r.val < m - 1 := by by_contra hge have hrlt := r.isLt have hreq : r = ⟨m - 1, by omega⟩ := Fin.ext (show r.val = m - 1 by omega) rw [hreq] at hr_mem; linarith refine ⟨⟨r.val, hr_lt⟩, hr_mem, ?_⟩ by_contra hge; push Not at hge have hr1 : (⟨r.val + 1, by omega⟩ : Fin m) ∈ P := by simp [P, hge] have := hr_max ⟨r.val + 1, by omega⟩ hr1 simp at this have hcell_bar_iff : ∀ (k : Fin (m - 1)) (s : ℝ) (hs1 : t ⟨k.val, by omega⟩ ≤ s) (hs2 : s < t ⟨k.val + 1, by omega⟩) (r : Fin N), (a r ≤ k ∧ k ≤ c r) ↔ ((D_im.bar r).b ≤ s ∧ s < (D_im.bar r).d) := by intro k s hs1 hs2 r constructor · rintro ⟨h1, h2⟩ have hb : t ⟨(a r).val, by omega⟩ ≤ t ⟨k.val, by omega⟩ := ht_strict.monotone (show (⟨(a r).val, by omega⟩ : Fin m) ≤ ⟨k.val, by omega⟩ from h1) have hd : t ⟨k.val + 1, by omega⟩ ≤ t ⟨(c r).val + 1, by omega⟩ := ht_strict.monotone (show (⟨k.val + 1, by omega⟩ : Fin m) ≤ ⟨(c r).val + 1, by omega⟩ by simp only [Fin.le_def]; have : k.val ≤ (c r).val := h2; omega) exact ⟨hb.trans hs1, lt_of_lt_of_le hs2 hd⟩ · rintro ⟨h1, h2⟩ have ha_le : a r ≤ k := by by_contra hgt; push Not at hgt have hstep : t ⟨k.val + 1, by omega⟩ ≤ t ⟨(a r).val, by omega⟩ := ht_strict.monotone (show (⟨k.val + 1, by omega⟩ : Fin m) ≤ ⟨(a r).val, by omega⟩ by simp only [Fin.le_def]; have : k.val < (a r).val := hgt; omega) linarith have hc_ge : k ≤ c r := by by_contra hgt; push Not at hgt have hstep : t ⟨(c r).val + 1, by omega⟩ ≤ t ⟨k.val, by omega⟩ := ht_strict.monotone (show (⟨(c r).val + 1, by omega⟩ : Fin m) ≤ ⟨k.val, by omega⟩ by simp only [Fin.le_def]; have : (c r).val < k.val := hgt; omega) linarith exact ⟨ha_le, hc_ge⟩ -- Build the real isomorphism `diagramModule K D_im ≅ imageMod φ` refine ⟨D_im, ⟨?_⟩⟩ let eqvAt : ∀ s : ℝ, (diagramModule K D_im).obj s ≃ₗ[K] (RPersistenceModule.imageMod φ).obj s := fun s => if hs : t ⟨0, by omega⟩ ≤ s ∧ s < t ⟨m - 1, by omega⟩ then let k := Classical.choose (hlocate s hs.1 hs.2) have hk := Classical.choose_spec (hlocate s hs.1 hs.2) let e1 : (diagramModule K D_im).obj s ≃ₗ[K] (∀ r : Fin N, PLift (a r ≤ k ∧ k ≤ c r) → K) := { toFun := fun x r ⟨hr⟩ => x r ⟨(hcell_bar_iff k s hk.1 hk.2 r).mp hr⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun y r ⟨hr⟩ => y r ⟨(hcell_bar_iff k s hk.1 hk.2 r).mpr hr⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } let e2 := eG k let e3 := LinearEquiv.ofBijective ((RPersistenceModule.imageMod φ).map hk.1) (hcell_bij k s hk.1 hk.2) (e1.trans e2).trans e3 else { toFun := fun _ => 0 map_add' := fun _ _ => (add_zero 0).symm map_smul' := fun _ _ => (smul_zero _).symm invFun := fun _ r ⟨hr⟩ => by exfalso have h0 : t ⟨0, by omega⟩ ≤ (D_im.bar r).b := ht_strict.monotone (show (⟨0, by omega⟩ : Fin m) ≤ _ by simp [Fin.le_def]) have hm1 : (D_im.bar r).d ≤ t ⟨m - 1, by omega⟩ := ht_strict.monotone (show (⟨(c r).val + 1, by omega⟩ : Fin m) ≤ ⟨m - 1, by omega⟩ from Nat.succ_le_of_lt (c r).isLt) exact hs ⟨h0.trans hr.1, lt_of_lt_of_le hr.2 hm1⟩ left_inv := fun _ => by funext r ⟨hr⟩; exfalso have h0 : t ⟨0, by omega⟩ ≤ (D_im.bar r).b := ht_strict.monotone (show (⟨0, by omega⟩ : Fin m) ≤ _ by simp [Fin.le_def]) have hm1 : (D_im.bar r).d ≤ t ⟨m - 1, by omega⟩ := ht_strict.monotone (show (⟨(c r).val + 1, by omega⟩ : Fin m) ≤ ⟨m - 1, by omega⟩ from Nat.succ_le_of_lt (c r).isLt) exact hs ⟨h0.trans hr.1, lt_of_lt_of_le hr.2 hm1⟩ right_inv := fun y => by have hout : s < t ⟨0, by omega⟩ ∨ t ⟨m - 1, by omega⟩ ≤ s := by by_cases h1 : t ⟨0, by omega⟩ ≤ s · exact Or.inr (not_lt.mp fun h2 => hs ⟨h1, h2⟩) · exact Or.inl (lt_of_not_ge h1) exact (hout_zero s hout y).symm } refine { equiv := eqvAt, comm := ?_ } intro s₁ s₂ h12 apply LinearMap.ext; intro x change eqvAt s₂ ((diagramModule K D_im).map h12 x) = (RPersistenceModule.imageMod φ).map h12 (eqvAt s₁ x) dsimp only [eqvAt] by_cases hs₂ : t ⟨0, by omega⟩ ≤ s₂ ∧ s₂ < t ⟨m - 1, by omega⟩ · by_cases hs₁ : t ⟨0, by omega⟩ ≤ s₁ ∧ s₁ < t ⟨m - 1, by omega⟩ · rw [dif_pos hs₂, dif_pos hs₁] set k₁ := Classical.choose (hlocate s₁ hs₁.1 hs₁.2) have hk₁ := Classical.choose_spec (hlocate s₁ hs₁.1 hs₁.2) set k₂ := Classical.choose (hlocate s₂ hs₂.1 hs₂.2) have hk₂ := Classical.choose_spec (hlocate s₂ hs₂.1 hs₂.2) have hk12 : k₁ ≤ k₂ := by by_contra hgt; push Not at hgt have hstep : t ⟨k₂.val + 1, by omega⟩ ≤ t ⟨k₁.val, by omega⟩ := ht_strict.monotone (show (⟨k₂.val + 1, by omega⟩ : Fin m) ≤ ⟨k₁.val, by omega⟩ by simp only [Fin.le_def]; have : k₂.val < k₁.val := hgt; omega) linarith [hk₁.1, hk₂.2, h12] change (RPersistenceModule.imageMod φ).map hk₂.1 (eG k₂ (fun r ⟨hr⟩ => (diagramModule K D_im).map h12 x r ⟨(hcell_bar_iff k₂ s₂ hk₂.1 hk₂.2 r).mp hr⟩)) = (RPersistenceModule.imageMod φ).map h12 ((RPersistenceModule.imageMod φ).map hk₁.1 (eG k₁ (fun r ⟨hr⟩ => x r ⟨(hcell_bar_iff k₁ s₁ hk₁.1 hk₁.2 r).mp hr⟩))) rw [(RPersistenceModule.imageMod φ).map_comp_apply hk₁.1 h12] let x1 : ∀ r : Fin N, PLift (a r ≤ k₁ ∧ k₁ ≤ c r) → K := fun r hr => x r ⟨(hcell_bar_iff k₁ s₁ hk₁.1 hk₁.2 r).mp hr.down⟩ have harg : (fun (r : Fin N) (hr : PLift (a r ≤ k₂ ∧ k₂ ≤ c r)) => (diagramModule K D_im).map h12 x r ⟨(hcell_bar_iff k₂ s₂ hk₂.1 hk₂.2 r).mp hr.down⟩) = (fun (r : Fin N) (_ : PLift (a r ≤ k₂ ∧ k₂ ≤ c r)) => if hi : a r ≤ k₁ ∧ k₁ ≤ c r then x1 r ⟨hi⟩ else 0) := by funext r ⟨hr⟩ change (if hs : (D_im.bar r).b ≤ s₁ ∧ s₁ < (D_im.bar r).d then x r ⟨hs⟩ else 0) = _ by_cases hi : a r ≤ k₁ ∧ k₁ ≤ c r · rw [dif_pos ((hcell_bar_iff k₁ s₁ hk₁.1 hk₁.2 r).mp hi), dif_pos hi] · rw [dif_neg (mt (hcell_bar_iff k₁ s₁ hk₁.1 hk₁.2 r).mpr hi), dif_neg hi] rw [harg, heG_comm hk12 x1] change (RPersistenceModule.imageMod φ).map hk₂.1 ((RPersistenceModule.imageMod φ).map _ (eG k₁ x1)) = (RPersistenceModule.imageMod φ).map _ (eG k₁ x1) rw [(RPersistenceModule.imageMod φ).map_comp_apply] · rw [dif_pos hs₂, dif_neg hs₁] have hzero : (diagramModule K D_im).map h12 x = 0 := by funext r ⟨hr⟩ change (if hs : (D_im.bar r).b ≤ s₁ ∧ s₁ < (D_im.bar r).d then x r ⟨hs⟩ else 0) = 0 have hs₁_lt : s₁ < t ⟨0, by omega⟩ := by by_contra hge; exact hs₁ ⟨not_lt.mp hge, lt_of_le_of_lt h12 hs₂.2⟩ have hnot : ¬ ((D_im.bar r).b ≤ s₁ ∧ s₁ < (D_im.bar r).d) := by intro h have h0 : t ⟨0, by omega⟩ ≤ (D_im.bar r).b := ht_strict.monotone (show (⟨0, by omega⟩ : Fin m) ≤ _ by simp [Fin.le_def]) linarith [h.1] rw [dif_neg hnot] rw [hzero, map_zero] change (0 : _) = (RPersistenceModule.imageMod φ).map h12 0 rw [map_zero] · have hout₂ : s₂ < t ⟨0, by omega⟩ ∨ t ⟨m - 1, by omega⟩ ≤ s₂ := by by_cases h1 : t ⟨0, by omega⟩ ≤ s₂ · exact Or.inr (not_lt.mp fun h2 => hs₂ ⟨h1, h2⟩) · exact Or.inl (lt_of_not_ge h1) rw [dif_neg hs₂] exact (hout_zero s₂ hout₂ _).symm end FiniteGridDecomp /-! ### Part 5: Bauer–Lesnick Induced Matchings & Algebraic Stability (`d_B ≤ d_I`) -/ section BauerLesnick variable {K : Type u} [Field K] /-- Ascending order-statistics dominance: if `f : Fin k → ℝ` and `g : Fin m → ℝ` are monotone and `#{i | f i ≤ x} ≤ #{j | g j ≤ x}` for all `x`, then `k ≤ m` and `g r ≤ f r` for all `r`. -/ lemma sorted_mono_le_of_count_le {k m : ℕ} {f : Fin k → ℝ} {g : Fin m → ℝ} (hf : Monotone f) (hg : Monotone g) (hcnt : ∀ x : ℝ, (Finset.univ.filter fun i : Fin k => f i ≤ x).card ≤ (Finset.univ.filter fun j : Fin m => g j ≤ x).card) : ∃ hkm : k ≤ m, ∀ r : Fin k, g ⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ ≤ f r := by have hkm : k ≤ m := by by_cases hk : k = 0 · omega · let rmax : Fin k := ⟨k - 1, by omega⟩ have h1 : k = (Finset.univ.filter fun i : Fin k => f i ≤ f rmax).card := by have hall : (Finset.univ.filter fun i : Fin k => f i ≤ f rmax) = Finset.univ := by ext i; simp only [Finset.mem_filter, Finset.mem_univ, true_and, iff_true] exact hf (show i ≤ rmax by simp [Fin.le_def, rmax]; omega) rw [hall, Finset.card_univ, Fintype.card_fin] calc k = (Finset.univ.filter fun i : Fin k => f i ≤ f rmax).card := h1 _ ≤ (Finset.univ.filter fun j : Fin m => g j ≤ f rmax).card := hcnt (f rmax) _ ≤ (Finset.univ : Finset (Fin m)).card := Finset.card_filter_le _ _ _ = m := by simp refine ⟨hkm, fun r => ?_⟩ by_contra hgt; push Not at hgt have hlow : r.val + 1 ≤ (Finset.univ.filter fun i : Fin k => f i ≤ f r).card := by let S : Finset (Fin k) := Finset.univ.filter fun i => i.val ≤ r.val have hsub : S ⊆ Finset.univ.filter fun i : Fin k => f i ≤ f r := by intro i hi; simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hi ⊢ exact hf hi have hcard : S.card = r.val + 1 := by have himg : S = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin k)) := by ext i; simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) rw [← hcard]; exact Finset.card_le_card hsub have hhigh : (Finset.univ.filter fun j : Fin m => g j ≤ f r).card ≤ r.val := by let T : Finset (Fin m) := Finset.univ.filter fun j => j.val < r.val have hsub : (Finset.univ.filter fun j : Fin m => g j ≤ f r) ⊆ T := by intro j hj; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ by_contra hge; push Not at hge have hgj : g ⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ ≤ g j := hg (show (⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ : Fin m) ≤ j from hge) linarith have hcard : T.card = r.val := by have himg : T = (Finset.range r.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin m)) := by ext j; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hj; exact ⟨j.val, hj, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) rw [← hcard]; exact Finset.card_le_card hsub have := hcnt (f r) omega /-- Descending order-statistics dominance: if `f : Fin k → ℝ` and `g : Fin m → ℝ` are antitone and `#{i | y < f i} ≤ #{j | y < g j}` for all `y`, then `k ≤ m` and `f r ≤ g r` for all `r`. -/ lemma sorted_anti_le_of_count_le {k m : ℕ} {f : Fin k → ℝ} {g : Fin m → ℝ} (hf : Antitone f) (hg : Antitone g) (hcnt : ∀ y : ℝ, (Finset.univ.filter fun i : Fin k => y < f i).card ≤ (Finset.univ.filter fun j : Fin m => y < g j).card) : ∃ hkm : k ≤ m, ∀ r : Fin k, f r ≤ g ⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ := by have hkm : k ≤ m := by by_cases hk : k = 0 · omega · let rmax : Fin k := ⟨k - 1, by omega⟩ let y0 : ℝ := f rmax - 1 have h1 : k = (Finset.univ.filter fun i : Fin k => y0 < f i).card := by have hall : (Finset.univ.filter fun i : Fin k => y0 < f i) = Finset.univ := by ext i; simp only [Finset.mem_filter, Finset.mem_univ, true_and, iff_true] have := hf (show i ≤ rmax by simp [Fin.le_def, rmax]; omega) linarith rw [hall, Finset.card_univ, Fintype.card_fin] calc k = (Finset.univ.filter fun i : Fin k => y0 < f i).card := h1 _ ≤ (Finset.univ.filter fun j : Fin m => y0 < g j).card := hcnt y0 _ ≤ (Finset.univ : Finset (Fin m)).card := Finset.card_filter_le _ _ _ = m := by simp refine ⟨hkm, fun r => ?_⟩ by_contra hgt; push Not at hgt let y0 : ℝ := g ⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ have hlow : r.val + 1 ≤ (Finset.univ.filter fun i : Fin k => y0 < f i).card := by let S : Finset (Fin k) := Finset.univ.filter fun i => i.val ≤ r.val have hsub : S ⊆ Finset.univ.filter fun i : Fin k => y0 < f i := by intro i hi; simp only [S, Finset.mem_filter, Finset.mem_univ, true_and] at hi ⊢ exact lt_of_lt_of_le hgt (hf hi) have hcard : S.card = r.val + 1 := by have himg : S = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin k)) := by ext i; simp only [S, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) rw [← hcard]; exact Finset.card_le_card hsub have hhigh : (Finset.univ.filter fun j : Fin m => y0 < g j).card ≤ r.val := by let T : Finset (Fin m) := Finset.univ.filter fun j => j.val < r.val have hsub : (Finset.univ.filter fun j : Fin m => y0 < g j) ⊆ T := by intro j hj; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hj ⊢ by_contra hge; push Not at hge have hgj : g j ≤ g ⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ := hg (show (⟨r.val, lt_of_lt_of_le r.isLt hkm⟩ : Fin m) ≤ j from hge) linarith have hcard : T.card = r.val := by have himg : T = (Finset.range r.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin m)) := by ext j; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hj; exact ⟨j.val, hj, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) rw [← hcard]; exact Finset.card_le_card hsub have := hcnt y0 omega /-- Exact dimension of `ker((diagramModule K D).map hxt)` for `x ≤ t`. -/ lemma finrank_ker_diagramMap (D : Diagram) {x t : ℝ} (hxt : x ≤ t) : Module.finrank K (LinearMap.ker ((diagramModule K D).map hxt)) = (Finset.univ.filter fun i : Fin D.n => (D.bar i).b ≤ x ∧ x < (D.bar i).d ∧ (D.bar i).d ≤ t).card := by classical have : FiniteDimensional K ((diagramModule K D).obj x) := finiteDimensional_diagramModule K D x let S : Finset (Fin D.n) := Finset.univ.filter fun i : Fin D.n => (D.bar i).b ≤ x ∧ x < (D.bar i).d ∧ (D.bar i).d ≤ t let e : LinearMap.ker ((diagramModule K D).map hxt) ≃ₗ[K] (S → K) := { toFun := fun ⟨v, _⟩ i => have hi := (Finset.mem_filter.mp i.2).2 v i.1 ⟨⟨hi.1, hi.2.1⟩⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun f => ⟨fun i ⟨hi⟩ => if h : i ∈ S then f ⟨i, h⟩ else 0, by change ((diagramModule K D).map hxt) (fun i ⟨_⟩ => if h : i ∈ S then f ⟨i, h⟩ else 0) = 0 funext i ⟨ht⟩ change (if hx : (D.bar i).b ≤ x ∧ x < (D.bar i).d then if h : i ∈ S then f ⟨i, h⟩ else 0 else 0) = 0 split_ifs with hx hiS · exfalso have hS := (Finset.mem_filter.mp hiS).2 linarith [ht.2, hS.2.2] · rfl · rfl⟩ left_inv := fun ⟨v, hv⟩ => by apply Subtype.ext; funext i ⟨hi⟩ rw [LinearMap.mem_ker] at hv change (if h : i ∈ S then v i ⟨_⟩ else 0) = v i ⟨hi⟩ by_cases hS : i ∈ S · rw [dif_pos hS] · rw [dif_neg hS] have htd : t < (D.bar i).d := by by_contra hle; push Not at hle exact hS (Finset.mem_filter.mpr ⟨Finset.mem_univ i, hi.1, hi.2, hle⟩) have hti : (D.bar i).b ≤ t ∧ t < (D.bar i).d := ⟨hi.1.trans hxt, htd⟩ have hv0 := congrFun (congrFun hv i) ⟨hti⟩ change (if hs : (D.bar i).b ≤ x ∧ x < (D.bar i).d then v i ⟨hs⟩ else 0) = 0 at hv0 rw [dif_pos hi] at hv0 exact hv0.symm right_inv := fun f => by funext ⟨i, hi⟩; simp [hi] } rw [LinearEquiv.finrank_eq e, Module.finrank_pi_fintype] simp [S] /-- Exact dimension of `range((diagramModule K D).map hxy)` for `x ≤ y`. -/ lemma finrank_range_diagramMap (D : Diagram) {x y : ℝ} (hxy : x ≤ y) : Module.finrank K (LinearMap.range ((diagramModule K D).map hxy)) = (Finset.univ.filter fun i : Fin D.n => (D.bar i).b ≤ x ∧ y < (D.bar i).d).card := by classical have : FiniteDimensional K ((diagramModule K D).obj y) := finiteDimensional_diagramModule K D y let S : Finset (Fin D.n) := Finset.univ.filter fun i : Fin D.n => (D.bar i).b ≤ x ∧ y < (D.bar i).d let e : LinearMap.range ((diagramModule K D).map hxy) ≃ₗ[K] (S → K) := { toFun := fun ⟨v, _⟩ i => have hi := (Finset.mem_filter.mp i.2).2 v i.1 ⟨⟨hi.1.trans hxy, hi.2⟩⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun f => ⟨fun i ⟨hi⟩ => if h : i ∈ S then f ⟨i, h⟩ else 0, by refine ⟨fun i ⟨_⟩ => if h : i ∈ S then f ⟨i, h⟩ else 0, ?_⟩ funext i ⟨hy⟩ change (if hx : (D.bar i).b ≤ x ∧ x < (D.bar i).d then if h : i ∈ S then f ⟨i, h⟩ else 0 else 0) = if h : i ∈ S then f ⟨i, h⟩ else 0 by_cases hS : i ∈ S · have hbi := (Finset.mem_filter.mp hS).2 have hx : (D.bar i).b ≤ x ∧ x < (D.bar i).d := ⟨hbi.1, lt_of_le_of_lt hxy hbi.2⟩ rw [dif_pos hx, dif_pos hS] · rw [dif_neg hS]; split_ifs <;> rfl⟩ left_inv := fun ⟨v, u, huv⟩ => by apply Subtype.ext; funext i ⟨hy⟩ change (if h : i ∈ S then v i ⟨_⟩ else 0) = v i ⟨hy⟩ by_cases hS : i ∈ S · rw [dif_pos hS] · rw [dif_neg hS] have hxnot : ¬ ((D.bar i).b ≤ x ∧ x < (D.bar i).d) := by intro hx; exact hS (Finset.mem_filter.mpr ⟨Finset.mem_univ i, hx.1, hy.2⟩) have h0 := congrFun (congrFun huv i) ⟨hy⟩ change (if hs : (D.bar i).b ≤ x ∧ x < (D.bar i).d then u i ⟨hs⟩ else 0) = v i ⟨hy⟩ at h0 rwa [dif_neg hxnot] at h0 right_inv := fun f => by funext ⟨i, hi⟩; simp [hi] } rw [LinearEquiv.finrank_eq e, Module.finrank_pi_fintype] simp [S] /-- **Bauer–Lesnick Lemma A1 (Monomorphism Birth-Count Inequality)**: If `ι : diagramModule K A ↪ diagramModule K B` is pointwise injective, then for every `d` and `x < d`, `#{i ∈ A | d_i = d ∧ b_i ≤ x} ≤ #{j ∈ B | d_j = d ∧ b_j ≤ x}`. -/ lemma mono_birth_count_le {A B : Diagram} (ι : RPersistenceModule.Hom (diagramModule K A) (diagramModule K B)) (hinj : ∀ t : ℝ, Function.Injective (ι.toFun t)) (d x : ℝ) (hxd : x < d) : (Finset.univ.filter fun i : Fin A.n => (A.bar i).d = d ∧ (A.bar i).b ≤ x).card ≤ (Finset.univ.filter fun j : Fin B.n => (B.bar j).d = d ∧ (B.bar j).b ≤ x).card := by classical -- Choose t0 ∈ [x, d) strictly larger than every bar death < d in A and B let deathsLt : Finset ℝ := ((Finset.univ.image fun i : Fin A.n => (A.bar i).d) ∪ (Finset.univ.image fun j : Fin B.n => (B.bar j).d)).filter (· < d) let m0 : ℝ := deathsLt.fold max x id have hm0_ge : x ≤ m0 := PartialMatching.le_fold_max_init deathsLt id x have hm0_lt : m0 < d := by have hle : ∀ s : Finset ℝ, (∀ y ∈ s, y < d) → s.fold max x id < d := by intro s hs induction s using Finset.induction_on with | empty => simpa using hxd | @insert a s ha ih => rw [Finset.fold_insert ha] exact max_lt (hs a (Finset.mem_insert_self a s)) (ih fun y hy => hs y (Finset.mem_insert_of_mem hy)) exact hle deathsLt fun y hy => (Finset.mem_filter.mp hy).2 let t0 : ℝ := (m0 + d) / 2 have ht0_def : t0 = (m0 + d) / 2 := rfl have hxt0 : x ≤ t0 := by linarith have ht0d : t0 ≤ d := by linarith have ht0_gt_A : ∀ i : Fin A.n, (A.bar i).d < d → (A.bar i).d < t0 := by intro i hi have hmem : (A.bar i).d ∈ deathsLt := Finset.mem_filter.mpr ⟨Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ i)), hi⟩ have hle : (A.bar i).d ≤ m0 := PartialMatching.le_fold_max_of_mem deathsLt id x hmem linarith have ht0_gt_B : ∀ j : Fin B.n, (B.bar j).d < d → (B.bar j).d < t0 := by intro j hj have hmem : (B.bar j).d ∈ deathsLt := Finset.mem_filter.mpr ⟨Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ j)), hj⟩ have hle : (B.bar j).d ≤ m0 := PartialMatching.le_fold_max_of_mem deathsLt id x hmem linarith -- Submodules K1 = ker(map x t0) ≤ K2 = ker(map x d) in A and B let KA1 := LinearMap.ker ((diagramModule K A).map hxt0) let KA2 := LinearMap.ker ((diagramModule K A).map hxd.le) let KB1 := LinearMap.ker ((diagramModule K B).map hxt0) let KB2 := LinearMap.ker ((diagramModule K B).map hxd.le) have hKA_le : KA1 ≤ KA2 := by intro u hu; simp only [KA1, KA2, LinearMap.mem_ker] at hu ⊢ rw [← (diagramModule K A).map_comp_apply hxt0 ht0d u, hu, map_zero] have hKB_le : KB1 ≤ KB2 := by intro u hu; simp only [KB1, KB2, LinearMap.mem_ker] at hu ⊢ rw [← (diagramModule K B).map_comp_apply hxt0 ht0d u, hu, map_zero] -- Induced injective linear map KA2 / KA1 ↪ KB2 / KB1 let fMap : KA2 →ₗ[K] KB2 := { toFun := fun u => ⟨ι.toFun x u.1, by simp only [KB2, LinearMap.mem_ker] have hc := LinearMap.congr_fun (ι.comm hxd.le) u.1 simp only [LinearMap.comp_apply] at hc rw [← hc, LinearMap.mem_ker.mp u.2, map_zero]⟩ map_add' := fun u v => Subtype.ext (LinearMap.map_add (ι.toFun x) u.1 v.1) map_smul' := fun c u => Subtype.ext (LinearMap.map_smul (ι.toFun x) c u.1) } let f : KA2 →ₗ[K] KB2 ⧸ (KB1.comap KB2.subtype) := (Submodule.mkQ (KB1.comap KB2.subtype)).comp fMap have hker_f : LinearMap.ker f = KA1.comap KA2.subtype := by ext ⟨u, hu⟩ simp only [f, fMap, LinearMap.mem_ker, LinearMap.comp_apply, Submodule.mkQ_apply, Submodule.Quotient.mk_eq_zero, Submodule.mem_comap, Submodule.coe_subtype, KB1, KA1, LinearMap.coe_mk, AddHom.coe_mk] have hc := LinearMap.congr_fun (ι.comm hxt0) u simp only [LinearMap.comp_apply] at hc rw [← hc] exact ⟨fun h => hinj t0 (by rw [h, map_zero]), fun h => by rw [h, map_zero]⟩ have := finiteDimensional_diagramModule K A x have := finiteDimensional_diagramModule K B x -- Compare finrank of quotients have hq_le : Module.finrank K (KA2 ⧸ (KA1.comap KA2.subtype)) ≤ Module.finrank K (KB2 ⧸ (KB1.comap KB2.subtype)) := by rw [← hker_f] have hrank := LinearMap.finrank_range_add_finrank_ker f have hquot := Submodule.finrank_quotient_add_finrank (LinearMap.ker f) have hrange_le : Module.finrank K (LinearMap.range f) ≤ Module.finrank K (KB2 ⧸ (KB1.comap KB2.subtype)) := Submodule.finrank_le _ omega -- Compute finrank(KA2 / KA1) = finrank KA2 - finrank KA1 have hfin_comap_A : Module.finrank K (KA1.comap KA2.subtype) = Module.finrank K KA1 := by let e : (KA1.comap KA2.subtype) ≃ₗ[K] KA1 := { toFun := fun ⟨⟨u, _⟩, hu⟩ => ⟨u, hu⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun ⟨u, hu⟩ => ⟨⟨u, hKA_le hu⟩, hu⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } exact LinearEquiv.finrank_eq e have hfin_comap_B : Module.finrank K (KB1.comap KB2.subtype) = Module.finrank K KB1 := by let e : (KB1.comap KB2.subtype) ≃ₗ[K] KB1 := { toFun := fun ⟨⟨u, _⟩, hu⟩ => ⟨u, hu⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun ⟨u, hu⟩ => ⟨⟨u, hKB_le hu⟩, hu⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } exact LinearEquiv.finrank_eq e have hqA := Submodule.finrank_quotient_add_finrank (KA1.comap KA2.subtype) have hqB := Submodule.finrank_quotient_add_finrank (KB1.comap KB2.subtype) rw [hfin_comap_A] at hqA; rw [hfin_comap_B] at hqB -- Identify finrank KA2 - finrank KA1 with #{i ∈ A | d_i = d ∧ b_i ≤ x} have hcardA : Module.finrank K KA2 = Module.finrank K KA1 + (Finset.univ.filter fun i : Fin A.n => (A.bar i).d = d ∧ (A.bar i).b ≤ x).card := by simp only [KA1, KA2, finrank_ker_diagramMap] have hdisj : Disjoint (Finset.univ.filter fun i : Fin A.n => (A.bar i).b ≤ x ∧ x < (A.bar i).d ∧ (A.bar i).d ≤ t0) (Finset.univ.filter fun i : Fin A.n => (A.bar i).d = d ∧ (A.bar i).b ≤ x) := by rw [Finset.disjoint_filter]; intro i _ h1 h2; linarith [h1.2.2, h2.1] rw [← Finset.card_union_of_disjoint hdisj] congr 1; ext i; simp only [Finset.mem_filter, Finset.mem_union, Finset.mem_univ, true_and] constructor · rintro ⟨h1, h2, h3⟩ rcases lt_or_eq_of_le h3 with hlt | heq · exact Or.inl ⟨h1, h2, (ht0_gt_A i hlt).le⟩ · exact Or.inr ⟨heq, h1⟩ · rintro (⟨h1, h2, h3⟩ | ⟨heq, h1⟩) · exact ⟨h1, h2, h3.trans ht0d⟩ · exact ⟨h1, by linarith, heq.le⟩ have hcardB : Module.finrank K KB2 = Module.finrank K KB1 + (Finset.univ.filter fun j : Fin B.n => (B.bar j).d = d ∧ (B.bar j).b ≤ x).card := by simp only [KB1, KB2, finrank_ker_diagramMap] have hdisj : Disjoint (Finset.univ.filter fun j : Fin B.n => (B.bar j).b ≤ x ∧ x < (B.bar j).d ∧ (B.bar j).d ≤ t0) (Finset.univ.filter fun j : Fin B.n => (B.bar j).d = d ∧ (B.bar j).b ≤ x) := by rw [Finset.disjoint_filter]; intro j _ h1 h2; linarith [h1.2.2, h2.1] rw [← Finset.card_union_of_disjoint hdisj] congr 1; ext j; simp only [Finset.mem_filter, Finset.mem_union, Finset.mem_univ, true_and] constructor · rintro ⟨h1, h2, h3⟩ rcases lt_or_eq_of_le h3 with hlt | heq · exact Or.inl ⟨h1, h2, (ht0_gt_B j hlt).le⟩ · exact Or.inr ⟨heq, h1⟩ · rintro (⟨h1, h2, h3⟩ | ⟨heq, h1⟩) · exact ⟨h1, h2, h3.trans ht0d⟩ · exact ⟨h1, by linarith, heq.le⟩ omega /-- **Bauer–Lesnick Lemma A2 (Epimorphism Death-Count Inequality)**: If `π : diagramModule K A ↠ diagramModule K B` is pointwise surjective, then for every `b` and `y ≥ b`, `#{j ∈ B | b_j = b ∧ y < d_j} ≤ #{i ∈ A | b_i = b ∧ y < d_i}`. -/ lemma epi_death_count_le {A B : Diagram} (π : RPersistenceModule.Hom (diagramModule K A) (diagramModule K B)) (hsurj : ∀ t : ℝ, Function.Surjective (π.toFun t)) (b y : ℝ) (hby : b ≤ y) : (Finset.univ.filter fun j : Fin B.n => (B.bar j).b = b ∧ y < (B.bar j).d).card ≤ (Finset.univ.filter fun i : Fin A.n => (A.bar i).b = b ∧ y < (A.bar i).d).card := by classical -- Choose s0 < b strictly larger than every bar birth < b in A and B let birthsLt : Finset ℝ := ((Finset.univ.image fun i : Fin A.n => (A.bar i).b) ∪ (Finset.univ.image fun j : Fin B.n => (B.bar j).b)).filter (· < b) let m0 : ℝ := birthsLt.fold max (b - 1) id have hm0_lt : m0 < b := by have hle : ∀ s : Finset ℝ, (∀ x ∈ s, x < b) → s.fold max (b - 1) id < b := by intro s hs induction s using Finset.induction_on with | empty => simp | @insert a s ha ih => rw [Finset.fold_insert ha] exact max_lt (hs a (Finset.mem_insert_self a s)) (ih fun x hx => hs x (Finset.mem_insert_of_mem hx)) exact hle birthsLt fun x hx => (Finset.mem_filter.mp hx).2 let s0 : ℝ := (m0 + b) / 2 have hs0_def : s0 = (m0 + b) / 2 := rfl have hs0b : s0 ≤ b := by linarith have hs0y : s0 ≤ y := hs0b.trans hby have hs0_gt_A : ∀ i : Fin A.n, (A.bar i).b < b → (A.bar i).b < s0 := by intro i hi have hmem : (A.bar i).b ∈ birthsLt := Finset.mem_filter.mpr ⟨Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ i)), hi⟩ have hle : (A.bar i).b ≤ m0 := PartialMatching.le_fold_max_of_mem birthsLt id (b - 1) hmem linarith have hs0_gt_B : ∀ j : Fin B.n, (B.bar j).b < b → (B.bar j).b < s0 := by intro j hj have hmem : (B.bar j).b ∈ birthsLt := Finset.mem_filter.mpr ⟨Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ j)), hj⟩ have hle : (B.bar j).b ≤ m0 := PartialMatching.le_fold_max_of_mem birthsLt id (b - 1) hmem linarith -- Submodules IA1 = range(map s0 y) ≤ IA2 = range(map b y) in A and B let IA1 := LinearMap.range ((diagramModule K A).map hs0y) let IA2 := LinearMap.range ((diagramModule K A).map hby) let IB1 := LinearMap.range ((diagramModule K B).map hs0y) let IB2 := LinearMap.range ((diagramModule K B).map hby) have hIA_le : IA1 ≤ IA2 := by rintro z ⟨x, rfl⟩ exact ⟨(diagramModule K A).map hs0b x, (diagramModule K A).map_comp_apply hs0b hby x⟩ have hIB_le : IB1 ≤ IB2 := by rintro z ⟨x, rfl⟩ exact ⟨(diagramModule K B).map hs0b x, (diagramModule K B).map_comp_apply hs0b hby x⟩ -- Induced surjective linear map IA2 / IA1 ↠ IB2 / IB1 let gMap : IA2 →ₗ[K] IB2 := { toFun := fun u => ⟨π.toFun y u.1, by rcases u.2 with ⟨x, hx⟩ refine ⟨π.toFun b x, ?_⟩ have hc := LinearMap.congr_fun (π.comm hby) x simp only [LinearMap.comp_apply] at hc rw [← hc, hx]⟩ map_add' := fun u v => Subtype.ext (LinearMap.map_add (π.toFun y) u.1 v.1) map_smul' := fun c u => Subtype.ext (LinearMap.map_smul (π.toFun y) c u.1) } let g : IA2 →ₗ[K] IB2 ⧸ (IB1.comap IB2.subtype) := (Submodule.mkQ (IB1.comap IB2.subtype)).comp gMap have hker_sub : IA1.comap IA2.subtype ≤ LinearMap.ker g := by rintro ⟨u, hu⟩ hu1 simp only [g, gMap, LinearMap.mem_ker, LinearMap.comp_apply, Submodule.mkQ_apply, Submodule.Quotient.mk_eq_zero, Submodule.mem_comap, Submodule.coe_subtype, LinearMap.coe_mk, AddHom.coe_mk] rcases hu1 with ⟨x, rfl⟩ refine ⟨π.toFun s0 x, ?_⟩ have hc := LinearMap.congr_fun (π.comm hs0y) x simp only [LinearMap.comp_apply] at hc exact hc.symm have hg_surj : Function.Surjective g := by intro q obtain ⟨⟨z, xB, rfl⟩, rfl⟩ := Submodule.mkQ_surjective _ q obtain ⟨xA, rfl⟩ := hsurj b xB refine ⟨⟨(diagramModule K A).map hby xA, ⟨xA, rfl⟩⟩, ?_⟩ simp only [g, gMap, LinearMap.comp_apply, Submodule.mkQ_apply, LinearMap.coe_mk, AddHom.coe_mk] congr 1; apply Subtype.ext exact LinearMap.congr_fun (π.comm hby) xA have := finiteDimensional_diagramModule K A y have := finiteDimensional_diagramModule K B y have hq_le : Module.finrank K (IB2 ⧸ (IB1.comap IB2.subtype)) ≤ Module.finrank K (IA2 ⧸ (IA1.comap IA2.subtype)) := by have hrank := LinearMap.finrank_range_add_finrank_ker g rw [LinearMap.range_eq_top.mpr hg_surj, finrank_top] at hrank have hquot := Submodule.finrank_quotient_add_finrank (IA1.comap IA2.subtype) have hkle := Submodule.finrank_mono hker_sub omega have hfin_comap_A : Module.finrank K (IA1.comap IA2.subtype) = Module.finrank K IA1 := LinearEquiv.finrank_eq { toFun := fun ⟨⟨u, _⟩, hu⟩ => ⟨u, hu⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun ⟨u, hu⟩ => ⟨⟨u, hIA_le hu⟩, hu⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } have hfin_comap_B : Module.finrank K (IB1.comap IB2.subtype) = Module.finrank K IB1 := LinearEquiv.finrank_eq { toFun := fun ⟨⟨u, _⟩, hu⟩ => ⟨u, hu⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl invFun := fun ⟨u, hu⟩ => ⟨⟨u, hIB_le hu⟩, hu⟩ left_inv := fun _ => rfl right_inv := fun _ => rfl } have hqA := Submodule.finrank_quotient_add_finrank (IA1.comap IA2.subtype) have hqB := Submodule.finrank_quotient_add_finrank (IB1.comap IB2.subtype) rw [hfin_comap_A] at hqA; rw [hfin_comap_B] at hqB have hcardA : Module.finrank K IA2 = Module.finrank K IA1 + (Finset.univ.filter fun i : Fin A.n => (A.bar i).b = b ∧ y < (A.bar i).d).card := by simp only [IA1, IA2, finrank_range_diagramMap] have hdisj : Disjoint (Finset.univ.filter fun i : Fin A.n => (A.bar i).b ≤ s0 ∧ y < (A.bar i).d) (Finset.univ.filter fun i : Fin A.n => (A.bar i).b = b ∧ y < (A.bar i).d) := by rw [Finset.disjoint_filter]; intro i _ h1 h2; linarith [h1.1, h2.1] rw [← Finset.card_union_of_disjoint hdisj] congr 1; ext i; simp only [Finset.mem_filter, Finset.mem_union, Finset.mem_univ, true_and] constructor · rintro ⟨h1, h2⟩ rcases lt_or_eq_of_le h1 with hlt | heq · exact Or.inl ⟨(hs0_gt_A i hlt).le, h2⟩ · exact Or.inr ⟨heq, h2⟩ · rintro (⟨h1, h2⟩ | ⟨heq, h2⟩) · exact ⟨h1.trans hs0b, h2⟩ · exact ⟨heq.le, h2⟩ have hcardB : Module.finrank K IB2 = Module.finrank K IB1 + (Finset.univ.filter fun j : Fin B.n => (B.bar j).b = b ∧ y < (B.bar j).d).card := by simp only [IB1, IB2, finrank_range_diagramMap] have hdisj : Disjoint (Finset.univ.filter fun j : Fin B.n => (B.bar j).b ≤ s0 ∧ y < (B.bar j).d) (Finset.univ.filter fun j : Fin B.n => (B.bar j).b = b ∧ y < (B.bar j).d) := by rw [Finset.disjoint_filter]; intro j _ h1 h2; linarith [h1.1, h2.1] rw [← Finset.card_union_of_disjoint hdisj] congr 1; ext j; simp only [Finset.mem_filter, Finset.mem_union, Finset.mem_univ, true_and] constructor · rintro ⟨h1, h2⟩ rcases lt_or_eq_of_le h1 with hlt | heq · exact Or.inl ⟨(hs0_gt_B j hlt).le, h2⟩ · exact Or.inr ⟨heq, h2⟩ · rintro (⟨h1, h2⟩ | ⟨heq, h2⟩) · exact ⟨h1.trans hs0b, h2⟩ · exact ⟨heq.le, h2⟩ omega /-- Filter of bars of `D` surviving a `δ`-shrink. -/ noncomputable def shrinkSet (D : Diagram) (δ : ℝ) : Finset (Fin D.n) := by classical exact Finset.univ.filter fun i => (D.bar i).b < (D.bar i).d - δ /-- Shrunk diagram `D` keeping bars `[b, d - δ)` with `b < d - δ`. -/ noncomputable def shrinkDiagram (D : Diagram) (δ : ℝ) : Diagram := by classical let eqv := (shrinkSet D δ).equivFin exact { n := (shrinkSet D δ).card bar := fun r => let i := (eqv.symm r).1 ⟨(D.bar i).b, (D.bar i).d - δ, (Finset.mem_filter.mp (eqv.symm r).2).2⟩ } /-- Filter of bars of `D` surviving a symmetric `ε`-shrink. -/ noncomputable def shiftShrinkSet (D : Diagram) (ε : ℝ) : Finset (Fin D.n) := by classical exact Finset.univ.filter fun i => (D.bar i).b + ε < (D.bar i).d - ε /-- Shifted-and-shrunk diagram `D` keeping bars `[b + ε, d - ε)` with `b + ε < d - ε`. -/ noncomputable def shiftShrinkDiagram (D : Diagram) (ε : ℝ) : Diagram := by classical let eqv := (shiftShrinkSet D ε).equivFin exact { n := (shiftShrinkSet D ε).card bar := fun r => let i := (eqv.symm r).1 ⟨(D.bar i).b + ε, (D.bar i).d - ε, (Finset.mem_filter.mp (eqv.symm r).2).2⟩ } /-- Given a finite type `α` and real score `f : α → ℝ`, sort `α` in nondecreasing order of `f`. -/ private noncomputable def sortAsc {α : Type*} [Fintype α] (f : α → ℝ) : Fin (Fintype.card α) ≃ α := by classical let leB : α → α → Bool := fun a b => decide (f a ≤ f b) let l : List α := (Finset.univ : Finset α).toList.mergeSort leB have hperm : l.Perm (Finset.univ : Finset α).toList := List.mergeSort_perm _ _ have hnodup : l.Nodup := hperm.nodup_iff.mpr (Finset.nodup_toList _) have hlen : l.length = Fintype.card α := by rw [hperm.length_eq, Finset.length_toList, Finset.card_univ] let toF : Fin (Fintype.card α) → α := fun k => l[k.val]'(by omega) have hinj : Function.Injective toF := by intro i j hij have := (List.Nodup.getElem_inj_iff hnodup).mp hij exact Fin.ext this exact Equiv.ofBijective toF ((Fintype.bijective_iff_injective_and_card toF).mpr ⟨hinj, by simp⟩) private lemma sortAsc_mono {α : Type*} [Fintype α] (f : α → ℝ) : Monotone (fun k => f (sortAsc f k)) := by classical intro i j hij rcases lt_or_eq_of_le hij with hlt | rfl · let leB : α → α → Bool := fun a b => decide (f a ≤ f b) have hpair : ((Finset.univ : Finset α).toList.mergeSort leB).Pairwise (fun a b => leB a b = true) := List.pairwise_mergeSort (fun a b c hab hbc => decide_eq_true ((of_decide_eq_true hab).trans (of_decide_eq_true hbc))) (fun a b => by by_cases h : f a ≤ f b <;> simp [h, le_of_not_ge]) _ have hlen : ((Finset.univ : Finset α).toList.mergeSort leB).length = Fintype.card α := by rw [(List.mergeSort_perm _ _).length_eq, Finset.length_toList, Finset.card_univ] have hget := List.pairwise_iff_getElem.mp hpair i.val j.val (by omega) (by omega) hlt exact of_decide_eq_true hget · exact le_rfl /-- Given a finite type `α` and real score `f : α → ℝ`, sort `α` in nonincreasing order of `f`. -/ private noncomputable def sortDesc {α : Type*} [Fintype α] (f : α → ℝ) : Fin (Fintype.card α) ≃ α := sortAsc (fun a => -f a) private lemma sortDesc_anti {α : Type*} [Fintype α] (f : α → ℝ) : Antitone (fun k => f (sortDesc f k)) := by intro i j hij have h : -f (sortAsc (fun a => -f a) i) ≤ -f (sortAsc (fun a => -f a) j) := sortAsc_mono (fun a => -f a) hij change f (sortAsc (fun a => -f a) j) ≤ f (sortAsc (fun a => -f a) i) linarith /-- Helper 1 (Bauer–Lesnick epimorphism chain): given pointwise surjections `D₁ ↠ D_im ↠ shrinkDiagram D₁ (2ε)`, construct the induced injection `χ₁ : Fin D_im.n ↪ Fin D₁.n`. -/ private lemma induced_matching_epi_chain {D₁ D_im : Diagram} {ε : ℝ} (π₁ : RPersistenceModule.Hom (diagramModule K D₁) (diagramModule K D_im)) (hπ₁_surj : ∀ t, Function.Surjective (π₁.toFun t)) (π₂ : RPersistenceModule.Hom (diagramModule K D_im) (diagramModule K (shrinkDiagram D₁ (2 * ε)))) (hπ₂_surj : ∀ t, Function.Surjective (π₂.toFun t)) : ∃ χ₁ : Fin D_im.n ↪ Fin D₁.n, (∀ k, (D₁.bar (χ₁ k)).b = (D_im.bar k).b ∧ (D₁.bar (χ₁ k)).d - 2 * ε ≤ (D_im.bar k).d ∧ (D_im.bar k).d ≤ (D₁.bar (χ₁ k)).d) ∧ (∀ i, (∀ k, χ₁ k ≠ i) → (D₁.bar i).d - (D₁.bar i).b ≤ 2 * ε) := by classical let S₁ : Finset (Fin D₁.n) := shrinkSet D₁ (2 * ε) let grpIm (b : ℝ) := {k : Fin D_im.n // (D_im.bar k).b = b} let grp1 (b : ℝ) := {i : Fin D₁.n // (D₁.bar i).b = b} let grpShr (b : ℝ) := {r : Fin (shrinkDiagram D₁ (2 * ε)).n // ((shrinkDiagram D₁ (2 * ε)).bar r).b = b} have hpair1 : ∀ b : ℝ, ∃ hle : Fintype.card (grpIm b) ≤ Fintype.card (grp1 b), ∀ r : Fin (Fintype.card (grpIm b)), (D_im.bar (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r).1).d ≤ (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r.val, lt_of_lt_of_le r.isLt hle⟩).1).d := by intro b apply sorted_anti_le_of_count_le (sortDesc_anti (fun x : grpIm b => (D_im.bar x.1).d)) (sortDesc_anti (fun x : grp1 b => (D₁.bar x.1).d)) intro y let y' := max b y have hcnt := epi_death_count_le π₁ hπ₁_surj b y' (le_max_left b y) have hl : (Finset.univ.filter fun r : Fin (Fintype.card (grpIm b)) => y < (D_im.bar (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r).1).d).card = (Finset.univ.filter fun k : Fin D_im.n => (D_im.bar k).b = b ∧ y' < (D_im.bar k).d).card := by apply Finset.card_bij (fun r _ => (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r).1) · intro r hr simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ set x := sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r have hx2 : (D_im.bar x.1).b = b := x.2 exact ⟨hx2, max_lt (by linarith [(D_im.bar x.1).hlt]) hr⟩ · intro r₁ _ r₂ _ heq exact (sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).injective (Subtype.ext heq) · intro k hk simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact lt_of_le_of_lt (le_max_right b y) hk.2 have hr : (Finset.univ.filter fun r : Fin (Fintype.card (grp1 b)) => y < (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r).1).d).card = (Finset.univ.filter fun i : Fin D₁.n => (D₁.bar i).b = b ∧ y' < (D₁.bar i).d).card := by apply Finset.card_bij (fun r _ => (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r).1) · intro r hr simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ set x := sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r have hx2 : (D₁.bar x.1).b = b := x.2 exact ⟨hx2, max_lt (by linarith [(D₁.bar x.1).hlt]) hr⟩ · intro r₁ _ r₂ _ heq exact (sortDesc (fun x : grp1 b => (D₁.bar x.1).d)).injective (Subtype.ext heq) · intro i hi simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hi refine ⟨(sortDesc (fun x : grp1 b => (D₁.bar x.1).d)).symm ⟨i, hi.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact lt_of_le_of_lt (le_max_right b y) hi.2 rw [hl, hr]; exact hcnt have hpair2 : ∀ b : ℝ, ∃ hle : Fintype.card (grpShr b) ≤ Fintype.card (grpIm b), ∀ r : Fin (Fintype.card (grpShr b)), ((shrinkDiagram D₁ (2 * ε)).bar (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) r).1).d ≤ (D_im.bar (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) ⟨r.val, lt_of_lt_of_le r.isLt hle⟩).1).d := by intro b apply sorted_anti_le_of_count_le (sortDesc_anti (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d)) (sortDesc_anti (fun x : grpIm b => (D_im.bar x.1).d)) intro y let y' := max b y have hcnt := epi_death_count_le π₂ hπ₂_surj b y' (le_max_left b y) have hl : (Finset.univ.filter fun r : Fin (Fintype.card (grpShr b)) => y < ((shrinkDiagram D₁ (2 * ε)).bar (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) r).1).d).card = (Finset.univ.filter fun r : Fin (shrinkDiagram D₁ (2 * ε)).n => ((shrinkDiagram D₁ (2 * ε)).bar r).b = b ∧ y' < ((shrinkDiagram D₁ (2 * ε)).bar r).d).card := by apply Finset.card_bij (fun r _ => (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) r).1) · intro r hr simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ set x := sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) r have hx2 : ((shrinkDiagram D₁ (2 * ε)).bar x.1).b = b := x.2 exact ⟨hx2, max_lt (by linarith [((shrinkDiagram D₁ (2 * ε)).bar x.1).hlt]) hr⟩ · intro r₁ _ r₂ _ heq exact (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d)).injective (Subtype.ext heq) · intro k hk simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact lt_of_le_of_lt (le_max_right b y) hk.2 have hr : (Finset.univ.filter fun r : Fin (Fintype.card (grpIm b)) => y < (D_im.bar (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r).1).d).card = (Finset.univ.filter fun k : Fin D_im.n => (D_im.bar k).b = b ∧ y' < (D_im.bar k).d).card := by apply Finset.card_bij (fun r _ => (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r).1) · intro r hr simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ set x := sortDesc (fun x : grpIm b => (D_im.bar x.1).d) r have hx2 : (D_im.bar x.1).b = b := x.2 exact ⟨hx2, max_lt (by linarith [(D_im.bar x.1).hlt]) hr⟩ · intro r₁ _ r₂ _ heq exact (sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).injective (Subtype.ext heq) · intro k hk simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact lt_of_le_of_lt (le_max_right b y) hk.2 rw [hl, hr]; exact hcnt have hprefix : ∀ b : ℝ, ∀ r : Fin (Fintype.card (grp1 b)), b < (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r).1).d - 2 * ε → ∃ hr : r.val < Fintype.card (grpShr b), ((shrinkDiagram D₁ (2 * ε)).bar (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) ⟨r.val, hr⟩).1).d = (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r).1).d - 2 * ε := by intro b r hlong have hcnt_eq : ∀ y : ℝ, b < y - 2 * ε → (Finset.univ.filter fun s : Fin (Fintype.card (grpShr b)) => y - 2 * ε < ((shrinkDiagram D₁ (2 * ε)).bar (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s).1).d).card = (Finset.univ.filter fun s : Fin (Fintype.card (grp1 b)) => y < (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) s).1).d).card := by intro y hy apply Finset.card_bij (fun s _ => let x := sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s (sortDesc (fun x : grp1 b => (D₁.bar x.1).d)).symm ⟨(S₁.equivFin.symm x.1).1, x.2⟩) · intro s hs simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] at hs ⊢ have : ((shrinkDiagram D₁ (2 * ε)).bar (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s).1).d = (D₁.bar (S₁.equivFin.symm (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s).1).1).d - 2 * ε := rfl linarith · intro s₁ _ s₂ _ heq have h1 := (sortDesc (fun x : grp1 b => (D₁.bar x.1).d)).symm.injective heq have h2 : (S₁.equivFin.symm (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s₁).1).1 = (S₁.equivFin.symm (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s₂).1).1 := congrArg (fun z : grp1 b => z.1) h1 have h3 : (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s₁).1 = (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s₂).1 := S₁.equivFin.symm.injective (Subtype.ext h2) exact (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d)).injective (Subtype.ext h3) · intro s hs simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hs set x := sortDesc (fun x : grp1 b => (D₁.bar x.1).d) s have hxS : x.1 ∈ S₁ := Finset.mem_filter.mpr ⟨Finset.mem_univ x.1, by have hx2 := x.2; linarith⟩ let rShr : grpShr b := ⟨S₁.equivFin ⟨x.1, hxS⟩, by change (D₁.bar (S₁.equivFin.symm (S₁.equivFin ⟨x.1, hxS⟩)).1).b = b simp only [Equiv.symm_apply_apply]; exact x.2⟩ refine ⟨(sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d)).symm rShr, ?_, ?_⟩ · simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] change y - 2 * ε < (D₁.bar (S₁.equivFin.symm (S₁.equivFin ⟨x.1, hxS⟩)).1).d - 2 * ε simp only [Equiv.symm_apply_apply]; linarith · simp only [Equiv.apply_symm_apply] have hsub : (⟨(S₁.equivFin.symm (S₁.equivFin ⟨x.1, hxS⟩)).1, rShr.2⟩ : grp1 b) = x := by apply Subtype.ext; simp only [Equiv.symm_apply_apply] rw [hsub, Equiv.symm_apply_apply] let f1 := fun s : Fin (Fintype.card (grp1 b)) => (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) s).1).d let fS := fun s : Fin (Fintype.card (grpShr b)) => ((shrinkDiagram D₁ (2 * ε)).bar (sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) s).1).d have hf1 : Antitone f1 := sortDesc_anti (fun x : grp1 b => (D₁.bar x.1).d) have hfS : Antitone fS := sortDesc_anti (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) let y0 : ℝ := (b + 2 * ε + f1 r) / 2 have hy0_def : y0 = (b + 2 * ε + f1 r) / 2 := rfl have hy0_lt : y0 < f1 r := by have hlong' : b < f1 r - 2 * ε := hlong; linarith have hy0_gt : b < y0 - 2 * ε := by have hlong' : b < f1 r - 2 * ε := hlong; linarith have hge_r : r.val + 1 ≤ (Finset.univ.filter fun s => y0 < f1 s).card := by let P : Finset (Fin (Fintype.card (grp1 b))) := Finset.univ.filter fun s => s.val ≤ r.val have hsub : P ⊆ Finset.univ.filter fun s => y0 < f1 s := by intro s hs; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ exact lt_of_lt_of_le hy0_lt (hf1 hs) have hcard : P.card = r.val + 1 := by have himg : P = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact hcard ▸ Finset.card_le_card hsub have hr_lt : r.val < Fintype.card (grpShr b) := by have h1 : (Finset.univ.filter fun s => y0 - 2 * ε < fS s).card = (Finset.univ.filter fun s => y0 < f1 s).card := hcnt_eq y0 hy0_gt have h2 : (Finset.univ.filter fun s => y0 - 2 * ε < fS s).card ≤ Fintype.card (grpShr b) := by calc _ ≤ (Finset.univ : Finset (Fin (Fintype.card (grpShr b)))).card := Finset.card_filter_le _ _ _ = Fintype.card (grpShr b) := by simp omega refine ⟨hr_lt, le_antisymm ?_ ?_⟩ · by_contra hgt; push Not at hgt have h1 : (Finset.univ.filter fun s => f1 r - 2 * ε < fS s).card = (Finset.univ.filter fun s => f1 r < f1 s).card := hcnt_eq (f1 r) hlong have hl : r.val + 1 ≤ (Finset.univ.filter fun s => f1 r - 2 * ε < fS s).card := by let P : Finset (Fin (Fintype.card (grpShr b))) := Finset.univ.filter fun s => s.val ≤ r.val have hsub : P ⊆ Finset.univ.filter fun s => f1 r - 2 * ε < fS s := by intro s hs; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ exact lt_of_lt_of_le hgt (hfS (show s ≤ ⟨r.val, hr_lt⟩ from hs)) have hcard : P.card = r.val + 1 := by have himg : P = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact hcard ▸ Finset.card_le_card hsub have hR : (Finset.univ.filter fun s => f1 r < f1 s).card ≤ r.val := by let T : Finset (Fin (Fintype.card (grp1 b))) := Finset.univ.filter fun s => s.val < r.val have hsub : (Finset.univ.filter fun s => f1 r < f1 s) ⊆ T := by intro s hs; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ by_contra hge; push Not at hge; linarith [hf1 (show r ≤ s from hge)] have hcard : T.card = r.val := by have himg : T = (Finset.range r.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, hi, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact (Finset.card_le_card hsub).trans hcard.le omega · by_contra hlt; push Not at hlt have hb_lt_fS : b < fS ⟨r.val, hr_lt⟩ := by set x := sortDesc (fun x : grpShr b => ((shrinkDiagram D₁ (2 * ε)).bar x.1).d) ⟨r.val, hr_lt⟩ have hx2 := x.2 linarith [((shrinkDiagram D₁ (2 * ε)).bar x.1).hlt] have h1 : (Finset.univ.filter fun s => fS ⟨r.val, hr_lt⟩ + 2 * ε - 2 * ε < fS s).card = (Finset.univ.filter fun s => fS ⟨r.val, hr_lt⟩ + 2 * ε < f1 s).card := hcnt_eq (fS ⟨r.val, hr_lt⟩ + 2 * ε) (by linarith) have hR : r.val + 1 ≤ (Finset.univ.filter fun s => fS ⟨r.val, hr_lt⟩ + 2 * ε < f1 s).card := by let P : Finset (Fin (Fintype.card (grp1 b))) := Finset.univ.filter fun s => s.val ≤ r.val have hsub : P ⊆ Finset.univ.filter fun s => fS ⟨r.val, hr_lt⟩ + 2 * ε < f1 s := by intro s hs; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ have : fS ⟨r.val, hr_lt⟩ + 2 * ε < f1 r := by linarith exact lt_of_lt_of_le this (hf1 hs) have hcard : P.card = r.val + 1 := by have himg : P = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact hcard ▸ Finset.card_le_card hsub have hL : (Finset.univ.filter fun s => fS ⟨r.val, hr_lt⟩ + 2 * ε - 2 * ε < fS s).card ≤ r.val := by let T : Finset (Fin (Fintype.card (grpShr b))) := Finset.univ.filter fun s => s.val < r.val have hsub : (Finset.univ.filter fun s => fS ⟨r.val, hr_lt⟩ + 2 * ε - 2 * ε < fS s) ⊆ T := by intro s hs; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ by_contra hge; push Not at hge have := hfS (show (⟨r.val, hr_lt⟩ : Fin _) ≤ s from hge) linarith have hcard : T.card = r.val := by have himg : T = (Finset.range r.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, hi, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact (Finset.card_le_card hsub).trans hcard.le omega let χ₁Fun : Fin D_im.n → Fin D₁.n := fun k => let b := (D_im.bar k).b let r := (sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k, rfl⟩ let hle := Classical.choose (hpair1 b) (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r.val, lt_of_lt_of_le r.isLt hle⟩).1 have hχ₁_inj : Function.Injective χ₁Fun := by intro k₁ k₂ heq have hb1 : (D₁.bar (χ₁Fun k₁)).b = (D_im.bar k₁).b := (sortDesc (fun x : grp1 (D_im.bar k₁).b => (D₁.bar x.1).d) _).2 have hb2 : (D₁.bar (χ₁Fun k₂)).b = (D_im.bar k₂).b := (sortDesc (fun x : grp1 (D_im.bar k₂).b => (D₁.bar x.1).d) _).2 have hbeq : (D_im.bar k₁).b = (D_im.bar k₂).b := by rw [← hb1, heq, hb2] set b := (D_im.bar k₁).b let r1 := (sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k₁, rfl⟩ let r2 := (sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k₂, hbeq.symm⟩ have hle_b := Classical.choose (hpair1 b) have hsub_eq : (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r1.val, lt_of_lt_of_le r1.isLt hle_b⟩) = (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r2.val, lt_of_lt_of_le r2.isLt hle_b⟩) := by apply Subtype.ext have hcast : ∀ (b' : ℝ) (hb' : b' = b) (hk' : (D_im.bar k₂).b = b'), (sortDesc (fun x : grp1 b' => (D₁.bar x.1).d) ⟨((sortDesc (fun x : grpIm b' => (D_im.bar x.1).d)).symm ⟨k₂, hk'⟩).val, lt_of_lt_of_le ((sortDesc (fun x : grpIm b' => (D_im.bar x.1).d)).symm ⟨k₂, hk'⟩).isLt (Classical.choose (hpair1 b'))⟩).1 = (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r2.val, lt_of_lt_of_le r2.isLt hle_b⟩).1 := by rintro _ rfl hk'; rfl have h2 := hcast (D_im.bar k₂).b hbeq.symm rfl change χ₁Fun k₁ = _ rw [heq]; exact h2 have hr_eq := (sortDesc (fun x : grp1 b => (D₁.bar x.1).d)).injective hsub_eq have hval_eq : r1 = r2 := Fin.ext (congrArg (fun z : Fin (Fintype.card (grp1 b)) => z.val) hr_eq) exact congrArg Subtype.val ((sortDesc _).symm.injective hval_eq) refine ⟨⟨χ₁Fun, hχ₁_inj⟩, ?_, ?_⟩ · intro k set b := (D_im.bar k).b let r := (sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k, rfl⟩ have hle1 := Classical.choose (hpair1 b) have hspec1 := Classical.choose_spec (hpair1 b) let r1 : Fin (Fintype.card (grp1 b)) := ⟨r.val, lt_of_lt_of_le r.isLt hle1⟩ have hb_eq : (D₁.bar (χ₁Fun k)).b = b := (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r1).2 have hd_upper : (D_im.bar k).d ≤ (D₁.bar (χ₁Fun k)).d := by have := hspec1 r simpa [r, χ₁Fun, b, r1] using this have hd_lower : (D₁.bar (χ₁Fun k)).d - 2 * ε ≤ (D_im.bar k).d := by by_cases hlong : b < (D₁.bar (χ₁Fun k)).d - 2 * ε · obtain ⟨hrShr, hshreq⟩ := hprefix b r1 hlong have hspec2 := Classical.choose_spec (hpair2 b) have hle_im := hspec2 ⟨r.val, hrShr⟩ rw [hshreq] at hle_im have hr_back : (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) ⟨r.val, lt_of_lt_of_le hrShr (Classical.choose (hpair2 b))⟩).1 = k := by have heq : (⟨r.val, lt_of_lt_of_le hrShr (Classical.choose (hpair2 b))⟩ : Fin (Fintype.card (grpIm b))) = r := Fin.ext rfl rw [heq]; simp [r] rw [hr_back] at hle_im exact hle_im · linarith [(D_im.bar k).hlt] exact ⟨hb_eq, hd_lower, hd_upper⟩ · intro i hun set b := (D₁.bar i).b let r1 := (sortDesc (fun x : grp1 b => (D₁.bar x.1).d)).symm ⟨i, rfl⟩ by_contra hgt; push Not at hgt have hlong : b < (D₁.bar (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) r1).1).d - 2 * ε := by simp [r1]; linarith obtain ⟨hrShr, _⟩ := hprefix b r1 hlong have hle2 := Classical.choose (hpair2 b) have hrIm : r1.val < Fintype.card (grpIm b) := lt_of_lt_of_le hrShr hle2 let k := (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) ⟨r1.val, hrIm⟩).1 have hkb : (D_im.bar k).b = b := (sortDesc (fun x : grpIm b => (D_im.bar x.1).d) ⟨r1.val, hrIm⟩).2 apply hun k have hr_back : ((sortDesc (fun x : grpIm b => (D_im.bar x.1).d)).symm ⟨k, hkb⟩).val = r1.val := by have hsub : (⟨k, hkb⟩ : grpIm b) = sortDesc _ ⟨r1.val, hrIm⟩ := rfl rw [hsub, Equiv.symm_apply_apply] have h1 : χ₁Fun k = (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r1.val, r1.isLt⟩).1 := by dsimp only [χ₁Fun] have hcast : ∀ (b' : ℝ) (hb' : b' = b) (hk' : (D_im.bar k).b = b'), (sortDesc (fun x : grp1 b' => (D₁.bar x.1).d) ⟨((sortDesc (fun x : grpIm b' => (D_im.bar x.1).d)).symm ⟨k, hk'⟩).val, lt_of_lt_of_le ((sortDesc (fun x : grpIm b' => (D_im.bar x.1).d)).symm ⟨k, hk'⟩).isLt (Classical.choose (hpair1 b'))⟩).1 = (sortDesc (fun x : grp1 b => (D₁.bar x.1).d) ⟨r1.val, r1.isLt⟩).1 := by rintro _ rfl hk' congr 1; apply congrArg (sortDesc (fun x : grp1 b => (D₁.bar x.1).d)) exact Fin.ext hr_back exact hcast (D_im.bar k).b hkb rfl change χ₁Fun k = i rw [h1] have : (⟨r1.val, r1.isLt⟩ : Fin (Fintype.card (grp1 b))) = r1 := Fin.ext rfl rw [this]; simp [r1] /-- Helper 2 (Bauer–Lesnick monomorphism chain): given pointwise injections `shiftShrinkDiagram D₂ ε ↪ D_im ↪ D₂.shift ε`, construct the induced injection `χ₂ : Fin D_im.n ↪ Fin D₂.n`. -/ private lemma induced_matching_mono_chain {D_im D₂ : Diagram} {ε : ℝ} (ι₁ : RPersistenceModule.Hom (diagramModule K (shiftShrinkDiagram D₂ ε)) (diagramModule K D_im)) (hι₁_inj : ∀ t, Function.Injective (ι₁.toFun t)) (ι₂ : RPersistenceModule.Hom (diagramModule K D_im) (diagramModule K (D₂.shift ε))) (hι₂_inj : ∀ t, Function.Injective (ι₂.toFun t)) : ∃ χ₂ : Fin D_im.n ↪ Fin D₂.n, (∀ k, (D_im.bar k).d = (D₂.bar (χ₂ k)).d - ε ∧ (D₂.bar (χ₂ k)).b - ε ≤ (D_im.bar k).b ∧ (D_im.bar k).b ≤ (D₂.bar (χ₂ k)).b + ε) ∧ (∀ j, (∀ k, χ₂ k ≠ j) → (D₂.bar j).d - (D₂.bar j).b ≤ 2 * ε) := by classical let S₂ : Finset (Fin D₂.n) := shiftShrinkSet D₂ ε let grpIm (d0 : ℝ) := {k : Fin D_im.n // (D_im.bar k).d = d0} let grp2 (d0 : ℝ) := {j : Fin D₂.n // (D₂.bar j).d - ε = d0} let grpShr (d0 : ℝ) := {r : Fin (shiftShrinkDiagram D₂ ε).n // ((shiftShrinkDiagram D₂ ε).bar r).d = d0} have hpair1 : ∀ d0 : ℝ, ∃ hle : Fintype.card (grpIm d0) ≤ Fintype.card (grp2 d0), ∀ r : Fin (Fintype.card (grpIm d0)), (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r.val, lt_of_lt_of_le r.isLt hle⟩).1).b - ε ≤ (D_im.bar (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1).b := by intro d0 apply sorted_mono_le_of_count_le (sortAsc_mono (fun x : grpIm d0 => (D_im.bar x.1).b)) (sortAsc_mono (fun x : grp2 d0 => (D₂.bar x.1).b - ε)) intro x by_cases hxd0 : x < d0 · have hcnt := mono_birth_count_le ι₂ hι₂_inj d0 x hxd0 have hl : (Finset.univ.filter fun r : Fin (Fintype.card (grpIm d0)) => (D_im.bar (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1).b ≤ x).card = (Finset.univ.filter fun k : Fin D_im.n => (D_im.bar k).d = d0 ∧ (D_im.bar k).b ≤ x).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1) · intro r hr; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ exact ⟨(sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).2, hr⟩ · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).injective (Subtype.ext heq) · intro k hk; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact hk.2 have hr : (Finset.univ.filter fun r : Fin (Fintype.card (grp2 d0)) => (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).1).b - ε ≤ x).card = (Finset.univ.filter fun j : Fin (D₂.shift ε).n => ((D₂.shift ε).bar j).d = d0 ∧ ((D₂.shift ε).bar j).b ≤ x).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).1) · intro r hr have hr' := (Finset.mem_filter.mp hr).2 exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).2, hr'⟩ · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).injective (Subtype.ext heq) · intro j hj have hj' := (Finset.mem_filter.mp hj).2 refine ⟨(sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).symm ⟨j, hj'.1⟩, ?_, by simp⟩ exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simpa only [Equiv.apply_symm_apply, Diagram.shift] using hj'.2⟩ rw [hl, hr]; exact hcnt · push Not at hxd0 let birthsAll : Finset ℝ := (Finset.univ.image fun k : grpIm d0 => (D_im.bar k.1).b) ∪ (Finset.univ.image fun j : grp2 d0 => (D₂.bar j.1).b - ε) let m0 : ℝ := birthsAll.fold max (d0 - 1) id have hm0_lt : m0 < d0 := by have hle : ∀ s : Finset ℝ, (∀ y ∈ s, y < d0) → s.fold max (d0 - 1) id < d0 := by intro s hs induction s using Finset.induction_on with | empty => simp | @insert a s ha ih => rw [Finset.fold_insert ha] exact max_lt (hs a (Finset.mem_insert_self a s)) (ih fun y hy => hs y (Finset.mem_insert_of_mem hy)) apply hle; intro y hy rcases Finset.mem_union.mp hy with h1 | h2 · rcases Finset.mem_image.mp h1 with ⟨k, _, rfl⟩ have hk2 := k.2; linarith [(D_im.bar k.1).hlt] · rcases Finset.mem_image.mp h2 with ⟨j, _, rfl⟩ have hj2 := j.2; linarith [(D₂.bar j.1).hlt] have hcnt := mono_birth_count_le ι₂ hι₂_inj d0 m0 hm0_lt have hl : (Finset.univ.filter fun r : Fin (Fintype.card (grpIm d0)) => (D_im.bar (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1).b ≤ x).card = (Finset.univ.filter fun k : Fin D_im.n => (D_im.bar k).d = d0 ∧ (D_im.bar k).b ≤ m0).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1) · intro r _ set z := sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, z.2, PartialMatching.le_fold_max_of_mem birthsAll id (d0 - 1) (Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ z)))⟩ · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).injective (Subtype.ext heq) · intro k hk have hk' := (Finset.mem_filter.mp hk).2 refine ⟨(sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, hk'.1⟩, ?_, by simp⟩ exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simpa only [Equiv.apply_symm_apply] using le_trans hk'.2 (hm0_lt.le.trans hxd0)⟩ have hr : (Finset.univ.filter fun r : Fin (Fintype.card (grp2 d0)) => (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).1).b - ε ≤ x).card = (Finset.univ.filter fun j : Fin (D₂.shift ε).n => ((D₂.shift ε).bar j).d = d0 ∧ ((D₂.shift ε).bar j).b ≤ m0).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).1) · intro r _ set z := sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, z.2, PartialMatching.le_fold_max_of_mem birthsAll id (d0 - 1) (Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ z)))⟩ · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).injective (Subtype.ext heq) · intro j hj have hj' := (Finset.mem_filter.mp hj).2 refine ⟨(sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).symm ⟨j, hj'.1⟩, ?_, by simp⟩ exact Finset.mem_filter.mpr ⟨Finset.mem_univ _, by simpa only [Equiv.apply_symm_apply, Diagram.shift] using le_trans hj'.2 (hm0_lt.le.trans hxd0)⟩ rw [hl, hr]; exact hcnt have hpair2 : ∀ d0 : ℝ, ∃ hle : Fintype.card (grpShr d0) ≤ Fintype.card (grpIm d0), ∀ r : Fin (Fintype.card (grpShr d0)), (D_im.bar (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) ⟨r.val, lt_of_lt_of_le r.isLt hle⟩).1).b ≤ ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r).1).b := by intro d0 apply sorted_mono_le_of_count_le (sortAsc_mono (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)) (sortAsc_mono (fun x : grpIm d0 => (D_im.bar x.1).b)) intro x by_cases hxd0 : x < d0 · have hcnt := mono_birth_count_le ι₁ hι₁_inj d0 x hxd0 have hl : (Finset.univ.filter fun r : Fin (Fintype.card (grpShr d0)) => ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r).1).b ≤ x).card = (Finset.univ.filter fun r : Fin (shiftShrinkDiagram D₂ ε).n => ((shiftShrinkDiagram D₂ ε).bar r).d = d0 ∧ ((shiftShrinkDiagram D₂ ε).bar r).b ≤ x).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r).1) · intro r hr; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ exact ⟨(sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r).2, hr⟩ · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)).injective (Subtype.ext heq) · intro k hk; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact hk.2 have hr : (Finset.univ.filter fun r : Fin (Fintype.card (grpIm d0)) => (D_im.bar (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1).b ≤ x).card = (Finset.univ.filter fun k : Fin D_im.n => (D_im.bar k).d = d0 ∧ (D_im.bar k).b ≤ x).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1) · intro r hr; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hr ⊢ exact ⟨(sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).2, hr⟩ · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).injective (Subtype.ext heq) · intro k hk; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact hk.2 rw [hl, hr]; exact hcnt · push Not at hxd0 let birthsAll : Finset ℝ := (Finset.univ.image fun k : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar k.1).b) ∪ (Finset.univ.image fun k : grpIm d0 => (D_im.bar k.1).b) let m0 : ℝ := birthsAll.fold max (d0 - 1) id have hm0_lt : m0 < d0 := by have hle : ∀ s : Finset ℝ, (∀ y ∈ s, y < d0) → s.fold max (d0 - 1) id < d0 := by intro s hs induction s using Finset.induction_on with | empty => simp | @insert a s ha ih => rw [Finset.fold_insert ha] exact max_lt (hs a (Finset.mem_insert_self a s)) (ih fun y hy => hs y (Finset.mem_insert_of_mem hy)) apply hle; intro y hy rcases Finset.mem_union.mp hy with h1 | h2 · rcases Finset.mem_image.mp h1 with ⟨k, _, rfl⟩ have hk2 := k.2; linarith [((shiftShrinkDiagram D₂ ε).bar k.1).hlt] · rcases Finset.mem_image.mp h2 with ⟨k, _, rfl⟩ have hk2 := k.2; linarith [(D_im.bar k.1).hlt] have hcnt := mono_birth_count_le ι₁ hι₁_inj d0 m0 hm0_lt have hl : (Finset.univ.filter fun r : Fin (Fintype.card (grpShr d0)) => ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r).1).b ≤ x).card = (Finset.univ.filter fun r : Fin (shiftShrinkDiagram D₂ ε).n => ((shiftShrinkDiagram D₂ ε).bar r).d = d0 ∧ ((shiftShrinkDiagram D₂ ε).bar r).b ≤ m0).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r).1) · intro r _ simp only [Finset.mem_filter, Finset.mem_univ, true_and] set z := sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) r refine ⟨z.2, PartialMatching.le_fold_max_of_mem birthsAll id (d0 - 1) ?_⟩ exact Finset.mem_union_left _ (Finset.mem_image_of_mem _ (Finset.mem_univ z)) · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)).injective (Subtype.ext heq) · intro k hk; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact le_trans hk.2 (hm0_lt.le.trans hxd0) have hr : (Finset.univ.filter fun r : Fin (Fintype.card (grpIm d0)) => (D_im.bar (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1).b ≤ x).card = (Finset.univ.filter fun k : Fin D_im.n => (D_im.bar k).d = d0 ∧ (D_im.bar k).b ≤ m0).card := by apply Finset.card_bij (fun r _ => (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r).1) · intro r _ simp only [Finset.mem_filter, Finset.mem_univ, true_and] set z := sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) r refine ⟨z.2, PartialMatching.le_fold_max_of_mem birthsAll id (d0 - 1) ?_⟩ exact Finset.mem_union_right _ (Finset.mem_image_of_mem _ (Finset.mem_univ z)) · intro r₁ _ r₂ _ heq exact (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).injective (Subtype.ext heq) · intro k hk; simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hk refine ⟨(sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, hk.1⟩, ?_, by simp⟩ simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] exact le_trans hk.2 (hm0_lt.le.trans hxd0) rw [hl, hr]; exact hcnt have hprefix : ∀ d0 : ℝ, ∀ r : Fin (Fintype.card (grp2 d0)), (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).1).b + ε < d0 → ∃ hr : r.val < Fintype.card (grpShr d0), ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) ⟨r.val, hr⟩).1).b = (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r).1).b + ε := by intro d0 r hlong have hcnt_eq : ∀ x : ℝ, x + 2 * ε < d0 → (Finset.univ.filter fun s : Fin (Fintype.card (grpShr d0)) => ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s).1).b ≤ x + 2 * ε).card = (Finset.univ.filter fun s : Fin (Fintype.card (grp2 d0)) => (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) s).1).b - ε ≤ x).card := by intro x hx apply Finset.card_bij (fun s _ => let z := sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).symm ⟨(S₂.equivFin.symm z.1).1, z.2⟩) · intro s hs simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] at hs ⊢ have : ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s).1).b = (D₂.bar (S₂.equivFin.symm (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s).1).1).b + ε := rfl linarith · intro s₁ _ s₂ _ heq have h1 := (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).symm.injective heq have h2 : (S₂.equivFin.symm (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s₁).1).1 = (S₂.equivFin.symm (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s₂).1).1 := congrArg (fun z : grp2 d0 => z.1) h1 have h3 : (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s₁).1 = (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s₂).1 := S₂.equivFin.symm.injective (Subtype.ext h2) exact (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)).injective (Subtype.ext h3) · intro s hs simp only [Finset.mem_filter, Finset.mem_univ, true_and] at hs set z := sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) s have hzS : z.1 ∈ S₂ := Finset.mem_filter.mpr ⟨Finset.mem_univ z.1, by have hz2 := z.2; linarith⟩ let rShr : grpShr d0 := ⟨S₂.equivFin ⟨z.1, hzS⟩, by change (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨z.1, hzS⟩)).1).d - ε = d0 simp only [Equiv.symm_apply_apply]; exact z.2⟩ refine ⟨(sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b)).symm rShr, ?_, ?_⟩ · simp only [Finset.mem_filter, Finset.mem_univ, true_and, Equiv.apply_symm_apply] change (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨z.1, hzS⟩)).1).b + ε ≤ x + 2 * ε simp only [Equiv.symm_apply_apply]; linarith · simp only [Equiv.apply_symm_apply] have hsub : (⟨(S₂.equivFin.symm (S₂.equivFin ⟨z.1, hzS⟩)).1, rShr.2⟩ : grp2 d0) = z := by apply Subtype.ext; simp only [Equiv.symm_apply_apply] rw [hsub, Equiv.symm_apply_apply] let f2 := fun s : Fin (Fintype.card (grp2 d0)) => (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) s).1).b - ε let fS := fun s : Fin (Fintype.card (grpShr d0)) => ((shiftShrinkDiagram D₂ ε).bar (sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) s).1).b have hf2 : Monotone f2 := sortAsc_mono (fun x : grp2 d0 => (D₂.bar x.1).b - ε) have hfS : Monotone fS := sortAsc_mono (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) have hge_r : r.val + 1 ≤ (Finset.univ.filter fun s => f2 s ≤ f2 r).card := by let P : Finset (Fin (Fintype.card (grp2 d0))) := Finset.univ.filter fun s => s.val ≤ r.val have hsub : P ⊆ Finset.univ.filter fun s => f2 s ≤ f2 r := by intro s hs; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ exact hf2 hs have hcard : P.card = r.val + 1 := by have himg : P = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact hcard ▸ Finset.card_le_card hsub have hr_lt : r.val < Fintype.card (grpShr d0) := by have h1 : (Finset.univ.filter fun s => fS s ≤ f2 r + 2 * ε).card = (Finset.univ.filter fun s => f2 s ≤ f2 r).card := hcnt_eq (f2 r) (by linarith) have h2 : (Finset.univ.filter fun s => fS s ≤ f2 r + 2 * ε).card ≤ Fintype.card (grpShr d0) := by calc _ ≤ (Finset.univ : Finset (Fin (Fintype.card (grpShr d0)))).card := Finset.card_filter_le _ _ _ = Fintype.card (grpShr d0) := by simp omega refine ⟨hr_lt, le_antisymm ?_ ?_⟩ · by_contra hgt; push Not at hgt have h1 : (Finset.univ.filter fun s => fS s ≤ f2 r + 2 * ε).card = (Finset.univ.filter fun s => f2 s ≤ f2 r).card := hcnt_eq (f2 r) (by linarith) have hL : (Finset.univ.filter fun s => fS s ≤ f2 r + 2 * ε).card ≤ r.val := by let T : Finset (Fin (Fintype.card (grpShr d0))) := Finset.univ.filter fun s => s.val < r.val have hsub : (Finset.univ.filter fun s => fS s ≤ f2 r + 2 * ε) ⊆ T := by intro s hs; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ by_contra hge; push Not at hge have := hfS (show (⟨r.val, hr_lt⟩ : Fin _) ≤ s from hge); linarith have hcard : T.card = r.val := by have himg : T = (Finset.range r.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, hi, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact (Finset.card_le_card hsub).trans hcard.le omega · by_contra hlt; push Not at hlt have hfS_lt : fS ⟨r.val, hr_lt⟩ < d0 := by set z := sortAsc (fun x : grpShr d0 => ((shiftShrinkDiagram D₂ ε).bar x.1).b) ⟨r.val, hr_lt⟩ have hz2 := z.2 linarith [((shiftShrinkDiagram D₂ ε).bar z.1).hlt] have h1 : (Finset.univ.filter fun s => fS s ≤ fS ⟨r.val, hr_lt⟩ - 2 * ε + 2 * ε).card = (Finset.univ.filter fun s => f2 s ≤ fS ⟨r.val, hr_lt⟩ - 2 * ε).card := hcnt_eq (fS ⟨r.val, hr_lt⟩ - 2 * ε) (by linarith) have hL : r.val + 1 ≤ (Finset.univ.filter fun s => fS s ≤ fS ⟨r.val, hr_lt⟩ - 2 * ε + 2 * ε).card := by let P : Finset (Fin (Fintype.card (grpShr d0))) := Finset.univ.filter fun s => s.val ≤ r.val have hsub : P ⊆ Finset.univ.filter fun s => fS s ≤ fS ⟨r.val, hr_lt⟩ - 2 * ε + 2 * ε := by intro s hs; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ have := hfS (show s ≤ ⟨r.val, hr_lt⟩ from hs); linarith have hcard : P.card = r.val + 1 := by have himg : P = (Finset.range (r.val + 1)).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [P, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, by omega, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact hcard ▸ Finset.card_le_card hsub have hR : (Finset.univ.filter fun s => f2 s ≤ fS ⟨r.val, hr_lt⟩ - 2 * ε).card ≤ r.val := by let T : Finset (Fin (Fintype.card (grp2 d0))) := Finset.univ.filter fun s => s.val < r.val have hsub : (Finset.univ.filter fun s => f2 s ≤ fS ⟨r.val, hr_lt⟩ - 2 * ε) ⊆ T := by intro s hs; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and] at hs ⊢ by_contra hge; push Not at hge have := hf2 (show r ≤ s from hge); linarith have hcard : T.card = r.val := by have himg : T = (Finset.range r.val).attach.image (fun x => (⟨x.1, by have := Finset.mem_range.mp x.2; omega⟩ : Fin _)) := by ext i; simp only [T, Finset.mem_filter, Finset.mem_univ, true_and, Finset.mem_image, Finset.mem_attach, Subtype.exists, Finset.mem_range] constructor · intro hi; exact ⟨i.val, hi, rfl⟩ · rintro ⟨v, hv, hveq⟩; have := congrArg Fin.val hveq; simp at this; omega rw [himg, Finset.card_image_of_injective] · simp · intro a b hab; exact Subtype.ext (congrArg Fin.val hab) exact (Finset.card_le_card hsub).trans hcard.le omega let χ₂Fun : Fin D_im.n → Fin D₂.n := fun k => let d0 := (D_im.bar k).d let r := (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, rfl⟩ let hle := Classical.choose (hpair1 d0) (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r.val, lt_of_lt_of_le r.isLt hle⟩).1 have hχ₂_inj : Function.Injective χ₂Fun := by intro k₁ k₂ heq have hd1 : (D₂.bar (χ₂Fun k₁)).d - ε = (D_im.bar k₁).d := (sortAsc (fun x : grp2 (D_im.bar k₁).d => (D₂.bar x.1).b - ε) _).2 have hd2 : (D₂.bar (χ₂Fun k₂)).d - ε = (D_im.bar k₂).d := (sortAsc (fun x : grp2 (D_im.bar k₂).d => (D₂.bar x.1).b - ε) _).2 have hdeq : (D_im.bar k₁).d = (D_im.bar k₂).d := by rw [← hd1, heq, hd2] set d0 := (D_im.bar k₁).d let r1 := (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k₁, rfl⟩ let r2 := (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k₂, hdeq.symm⟩ have hle_d := Classical.choose (hpair1 d0) have hsub_eq : (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r1.val, lt_of_lt_of_le r1.isLt hle_d⟩) = (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r2.val, lt_of_lt_of_le r2.isLt hle_d⟩) := by apply Subtype.ext have hcast : ∀ (d' : ℝ) (hd' : d' = d0) (hk' : (D_im.bar k₂).d = d'), (sortAsc (fun x : grp2 d' => (D₂.bar x.1).b - ε) ⟨((sortAsc (fun x : grpIm d' => (D_im.bar x.1).b)).symm ⟨k₂, hk'⟩).val, lt_of_lt_of_le ((sortAsc (fun x : grpIm d' => (D_im.bar x.1).b)).symm ⟨k₂, hk'⟩).isLt (Classical.choose (hpair1 d'))⟩).1 = (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r2.val, lt_of_lt_of_le r2.isLt hle_d⟩).1 := by rintro _ rfl hk'; rfl have h2 := hcast (D_im.bar k₂).d hdeq.symm rfl change χ₂Fun k₁ = _ rw [heq]; exact h2 have hr_eq := (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).injective hsub_eq have hval_eq : r1 = r2 := Fin.ext (congrArg (fun z : Fin (Fintype.card (grp2 d0)) => z.val) hr_eq) exact congrArg Subtype.val ((sortAsc _).symm.injective hval_eq) refine ⟨⟨χ₂Fun, hχ₂_inj⟩, ?_, ?_⟩ · intro k set d0 := (D_im.bar k).d let r := (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, rfl⟩ have hle1 := Classical.choose (hpair1 d0) have hspec1 := Classical.choose_spec (hpair1 d0) let r2 : Fin (Fintype.card (grp2 d0)) := ⟨r.val, lt_of_lt_of_le r.isLt hle1⟩ have hd_eq : (D₂.bar (χ₂Fun k)).d - ε = d0 := (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r2).2 have hb_lower : (D₂.bar (χ₂Fun k)).b - ε ≤ (D_im.bar k).b := by have := hspec1 r simpa [r, χ₂Fun, d0, r2] using this have hb_upper : (D_im.bar k).b ≤ (D₂.bar (χ₂Fun k)).b + ε := by by_cases hlong : (D₂.bar (χ₂Fun k)).b + ε < d0 · obtain ⟨hrShr, hshreq⟩ := hprefix d0 r2 hlong have hspec2 := Classical.choose_spec (hpair2 d0) have hle_im := hspec2 ⟨r.val, hrShr⟩ rw [hshreq] at hle_im have hr_back : (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) ⟨r.val, lt_of_lt_of_le hrShr (Classical.choose (hpair2 d0))⟩).1 = k := by have heq : (⟨r.val, lt_of_lt_of_le hrShr (Classical.choose (hpair2 d0))⟩ : Fin (Fintype.card (grpIm d0))) = r := Fin.ext rfl rw [heq]; simp [r] rw [hr_back] at hle_im exact hle_im · linarith [(D_im.bar k).hlt] exact ⟨hd_eq.symm, hb_lower, hb_upper⟩ · intro j hun set d0 := (D₂.bar j).d - ε let r2 := (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)).symm ⟨j, rfl⟩ by_contra hgt; push Not at hgt have hlong : (D₂.bar (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) r2).1).b + ε < d0 := by simp [r2]; linarith obtain ⟨hrShr, _⟩ := hprefix d0 r2 hlong have hle2 := Classical.choose (hpair2 d0) have hrIm : r2.val < Fintype.card (grpIm d0) := lt_of_lt_of_le hrShr hle2 let k := (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) ⟨r2.val, hrIm⟩).1 have hkd : (D_im.bar k).d = d0 := (sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b) ⟨r2.val, hrIm⟩).2 apply hun k have hr_back : ((sortAsc (fun x : grpIm d0 => (D_im.bar x.1).b)).symm ⟨k, hkd⟩).val = r2.val := by have hsub : (⟨k, hkd⟩ : grpIm d0) = sortAsc _ ⟨r2.val, hrIm⟩ := rfl rw [hsub, Equiv.symm_apply_apply] have h1 : χ₂Fun k = (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r2.val, r2.isLt⟩).1 := by dsimp only [χ₂Fun] have hcast : ∀ (d' : ℝ) (hd' : d' = d0) (hk' : (D_im.bar k).d = d'), (sortAsc (fun x : grp2 d' => (D₂.bar x.1).b - ε) ⟨((sortAsc (fun x : grpIm d' => (D_im.bar x.1).b)).symm ⟨k, hk'⟩).val, lt_of_lt_of_le ((sortAsc (fun x : grpIm d' => (D_im.bar x.1).b)).symm ⟨k, hk'⟩).isLt (Classical.choose (hpair1 d'))⟩).1 = (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε) ⟨r2.val, r2.isLt⟩).1 := by rintro _ rfl hk' congr 1; apply congrArg (sortAsc (fun x : grp2 d0 => (D₂.bar x.1).b - ε)) exact Fin.ext hr_back exact hcast (D_im.bar k).d hkd rfl change χ₂Fun k = j rw [h1] have : (⟨r2.val, r2.isLt⟩ : Fin (Fintype.card (grp2 d0))) = r2 := Fin.ext rfl rw [this]; simp [r2] /-- **Algebraic Stability Theorem (`d_B ≤ d_I`)**: If `diagramModule K D₁` and `diagramModule K D₂` are `ε`-interleaved, then `d_B(D₁, D₂) ≤ ε`. -/ theorem algebraic_stability_theorem {D₁ D₂ : Diagram} {ε : ℝ} (I : RPersistenceModule.Interleaving (diagramModule K D₁) (diagramModule K D₂) ε) : bottleneckDist D₁ D₂ ≤ ε := by classical rw [bottleneckDist_le_iff] have hε : 0 ≤ ε := I.hε -- Step 1: Decompose im(φ) into a finite diagram D_im obtain ⟨D_im, ⟨eIm⟩⟩ := exists_diagram_iso_imageMod I.φ -- Step 2: Surjections U ↠ D_im and D_im ↠ shrinkDiagram D₁ (2 * ε) let π₁ : RPersistenceModule.Hom (diagramModule K D₁) (diagramModule K D_im) := eIm.symm.toHom.comp (RPersistenceModule.imageProj I.φ) have hπ₁_surj : ∀ t, Function.Surjective (π₁.toFun t) := fun t => (eIm.equiv t).symm.surjective.comp (RPersistenceModule.imageProj_surjective I.φ t) -- Build surjection `π₂ : diagramModule K D_im ↠ diagramModule K (shrinkDiagram D₁ (2*ε))` let S₁ : Finset (Fin D₁.n) := shrinkSet D₁ (2 * ε) let π₂_raw : RPersistenceModule.Hom (RPersistenceModule.imageMod I.φ) (diagramModule K (shrinkDiagram D₁ (2 * ε))) := { toFun := fun t => { toFun := fun y r ⟨hr⟩ => let i := (S₁.equivFin.symm r).1 have hr1 : (D₁.bar i).b ≤ t := hr.1 have hr2 : t < (D₁.bar i).d - 2 * ε := hr.2 have hbi : (D₁.bar i).b ≤ t + ε + ε ∧ t + ε + ε < (D₁.bar i).d := ⟨by linarith, by linarith⟩ (I.ψ.toFun (t + ε) y.1) i ⟨hbi⟩ map_add' := fun y₁ y₂ => by funext r ⟨hr⟩ let a₁ : (diagramModule K D₂).obj (t + ε) := y₁.1 let a₂ : (diagramModule K D₂).obj (t + ε) := y₂.1 change (I.ψ.toFun (t + ε) (a₁ + a₂)) _ _ = (I.ψ.toFun (t + ε) a₁) _ _ + (I.ψ.toFun (t + ε) a₂) _ _ rw [LinearMap.map_add]; rfl map_smul' := fun c y => by funext r ⟨hr⟩ let a : (diagramModule K D₂).obj (t + ε) := y.1 change (I.ψ.toFun (t + ε) (c • a)) _ _ = c • (I.ψ.toFun (t + ε) a) _ _ rw [LinearMap.map_smul]; rfl } comm := fun {s t} hst => by apply LinearMap.ext; rintro ⟨y, ⟨x, rfl⟩⟩; funext r ⟨hr⟩ set i := (S₁.equivFin.symm r).1 have hr1 : (D₁.bar i).b ≤ t := hr.1 have hr2 : t < (D₁.bar i).d - 2 * ε := hr.2 have hcomp_s : I.ψ.toFun (s + ε) (I.φ.toFun s x) = (diagramModule K D₁).map (by linarith : s ≤ s + ε + ε) x := LinearMap.congr_fun (I.left_comp s) x have hcomp_t : I.ψ.toFun (t + ε) (I.φ.toFun t ((diagramModule K D₁).map hst x)) = (diagramModule K D₁).map (by linarith : t ≤ t + ε + ε) ((diagramModule K D₁).map hst x) := LinearMap.congr_fun (I.left_comp t) ((diagramModule K D₁).map hst x) have hφ : I.φ.toFun t ((diagramModule K D₁).map hst x) = ((diagramModule K D₂).shift ε).map hst (I.φ.toFun s x) := LinearMap.congr_fun (I.φ.comm hst) x change (I.ψ.toFun (t + ε) (((diagramModule K D₂).shift ε).map hst (I.φ.toFun s x))) i _ = if hs : ((shrinkDiagram D₁ (2 * ε)).bar r).b ≤ s ∧ s < ((shrinkDiagram D₁ (2 * ε)).bar r).d then (I.ψ.toFun (s + ε) (I.φ.toFun s x)) i _ else 0 rw [← hφ, hcomp_t, hcomp_s] change (if hti : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d then (if hsi : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d then x i ⟨hsi⟩ else 0) else 0) = if hs : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d - 2 * ε then (if hsi : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d then x i ⟨hsi⟩ else 0) else 0 by_cases hs : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d - 2 * ε · have hsi : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d := ⟨hs.1, by linarith [hs.2]⟩ have hti : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d := ⟨hs.1.trans hst, by linarith [hr2]⟩ rw [dif_pos hti, dif_pos hsi, dif_pos hs] · rw [dif_neg hs] by_cases hti : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d · rw [dif_pos hti] by_cases hsi : (D₁.bar i).b ≤ s ∧ s < (D₁.bar i).d · exfalso; exact hs ⟨hsi.1, by linarith [hst, hr2]⟩ · rw [dif_neg hsi] · rw [dif_neg hti] } let π₂ : RPersistenceModule.Hom (diagramModule K D_im) (diagramModule K (shrinkDiagram D₁ (2 * ε))) := π₂_raw.comp eIm.toHom have hπ₂_surj : ∀ t, Function.Surjective (π₂.toFun t) := by intro t z let x : (diagramModule K D₁).obj t := fun i ⟨hi⟩ => if hS : i ∈ S₁ then if hlt : t < (D₁.bar i).d - 2 * ε then z (S₁.equivFin ⟨i, hS⟩) ⟨⟨by show (D₁.bar (S₁.equivFin.symm (S₁.equivFin ⟨i, hS⟩)).1).b ≤ t simp only [Equiv.symm_apply_apply]; exact hi.1, by show t < (D₁.bar (S₁.equivFin.symm (S₁.equivFin ⟨i, hS⟩)).1).d - 2 * ε simp only [Equiv.symm_apply_apply]; exact hlt⟩⟩ else 0 else 0 let yIm : (RPersistenceModule.imageMod I.φ).obj t := ⟨I.φ.toFun t x, ⟨x, rfl⟩⟩ refine ⟨(eIm.equiv t).symm yIm, ?_⟩ funext r ⟨hr⟩ have hre : (eIm.equiv t) ((eIm.equiv t).symm yIm) = yIm := LinearEquiv.apply_symm_apply _ _ change (I.ψ.toFun (t + ε) ((eIm.equiv t ((eIm.equiv t).symm yIm)).1)) _ _ = z r ⟨hr⟩ rw [hre] have hl : I.ψ.toFun (t + ε) (I.φ.toFun t x) = (diagramModule K D₁).map (by linarith : t ≤ t + ε + ε) x := LinearMap.congr_fun (I.left_comp t) x change (I.ψ.toFun (t + ε) (I.φ.toFun t x)) _ _ = z r ⟨hr⟩ rw [hl] set i := (S₁.equivFin.symm r).1 have hiS : i ∈ S₁ := (S₁.equivFin.symm r).2 have hr1 : (D₁.bar i).b ≤ t := hr.1 have hr2 : t < (D₁.bar i).d - 2 * ε := hr.2 have hti : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d := ⟨hr1, by linarith⟩ change (if hs : (D₁.bar i).b ≤ t ∧ t < (D₁.bar i).d then x i ⟨hs⟩ else 0) = z r ⟨hr⟩ rw [dif_pos hti] simp only [x, hiS, hr2, ↓reduceDIte] have hreq : S₁.equivFin ⟨i, hiS⟩ = r := S₁.equivFin.apply_symm_apply r have hsub : ∀ (r' : Fin (shrinkDiagram D₁ (2 * ε)).n) (h' : _), r' = r → z r' ⟨h'⟩ = z r ⟨hr⟩ := by rintro _ _ rfl; rfl exact hsub _ _ hreq -- Step 3: Injections `D_im ↪ D₂[ε]` and `shiftShrinkDiagram D₂ ε ↪ D_im` let ι₂ : RPersistenceModule.Hom (diagramModule K D_im) (diagramModule K (D₂.shift ε)) := (diagramModuleShiftIso K D₂ ε).toHom.comp ((RPersistenceModule.imageIncl I.φ).comp eIm.toHom) have hι₂_inj : ∀ t, Function.Injective (ι₂.toFun t) := fun t => ((diagramModuleShiftIso K D₂ ε).equiv t).injective.comp ((RPersistenceModule.imageIncl_injective I.φ t).comp (eIm.equiv t).injective) let S₂ : Finset (Fin D₂.n) := shiftShrinkSet D₂ ε -- Helper casting `((diagramModule K D₁).shift ε).obj (t - ε)` to `(diagramModule K D₁).obj t` let castObj : ∀ t : ℝ, ((diagramModule K D₁).shift ε).obj (t - ε) →ₗ[K] (diagramModule K D₁).obj t := fun t => { toFun := fun u i ⟨hi⟩ => u i ⟨⟨by linarith [hi.1], by linarith [hi.2]⟩⟩ map_add' := fun _ _ => rfl map_smul' := fun _ _ => rfl } have hφ_cast : ∀ (u : ℝ) (y : (diagramModule K D₂).obj (u - ε)), (I.φ.toFun u (castObj u (I.ψ.toFun (u - ε) y)) : (diagramModule K D₂).obj (u + ε)) = (diagramModule K D₂).map (by linarith : u - ε ≤ u + ε) y := by intro u y have hrc : (I.φ.toFun (u - ε + ε) (I.ψ.toFun (u - ε) y) : (diagramModule K D₂).obj (u - ε + ε + ε)) = (diagramModule K D₂).map (by linarith : u - ε ≤ u - ε + ε + ε) y := LinearMap.congr_fun (I.right_comp (u - ε)) y have heq : u - ε + ε = u := sub_add_cancel u ε have h1 : ∀ (a b : ℝ) (hab : a = b) (v : (diagramModule K D₁).obj a), (I.φ.toFun b (fun i ⟨hi⟩ => v i ⟨by rwa [hab]⟩) : (diagramModule K D₂).obj (b + ε)) = (fun j ⟨hj⟩ => I.φ.toFun a v j ⟨by rwa [hab]⟩) := by rintro a _ rfl v; rfl rw [show castObj u (I.ψ.toFun (u - ε) y) = (fun i ⟨hi⟩ => I.ψ.toFun (u - ε) y i ⟨by rwa [heq]⟩) from rfl] rw [h1 (u - ε + ε) u heq (I.ψ.toFun (u - ε) y), hrc] funext j ⟨hj⟩ change (if hs : (D₂.bar j).b ≤ u - ε ∧ u - ε < (D₂.bar j).d then y j ⟨hs⟩ else 0) = (if hs : (D₂.bar j).b ≤ u - ε ∧ u - ε < (D₂.bar j).d then y j ⟨hs⟩ else 0) rfl let mkY : ∀ (t : ℝ), (diagramModule K (shiftShrinkDiagram D₂ ε)).obj t →ₗ[K] (diagramModule K D₂).obj (t - ε) := fun t => { toFun := fun z j ⟨hj⟩ => if hS : j ∈ S₂ then if hlt : t < (D₂.bar j).d - ε then z (S₂.equivFin ⟨j, hS⟩) ⟨⟨by show (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨j, hS⟩)).1).b + ε ≤ t simp only [Equiv.symm_apply_apply]; linarith [hj.1], by show t < (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨j, hS⟩)).1).d - ε simp only [Equiv.symm_apply_apply]; exact hlt⟩⟩ else 0 else 0 map_add' := fun z₁ z₂ => by funext j ⟨hj⟩ change (if hS : j ∈ S₂ then if hlt : t < (D₂.bar j).d - ε then z₁ _ _ + z₂ _ _ else 0 else 0) = (if hS : j ∈ S₂ then if hlt : t < (D₂.bar j).d - ε then z₁ _ _ else 0 else 0) + (if hS : j ∈ S₂ then if hlt : t < (D₂.bar j).d - ε then z₂ _ _ else 0 else 0) split_ifs <;> simp map_smul' := fun c z => by funext j ⟨hj⟩ change (if hS : j ∈ S₂ then if hlt : t < (D₂.bar j).d - ε then c • z _ _ else 0 else 0) = c • (if hS : j ∈ S₂ then if hlt : t < (D₂.bar j).d - ε then z _ _ else 0 else 0) split_ifs <;> simp } let ι₁_raw : RPersistenceModule.Hom (diagramModule K (shiftShrinkDiagram D₂ ε)) (RPersistenceModule.imageMod I.φ) := { toFun := fun t => { toFun := fun z => ⟨I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t z))), ⟨castObj t (I.ψ.toFun (t - ε) (mkY t z)), rfl⟩⟩ map_add' := fun z₁ z₂ => by apply Subtype.ext change I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t (z₁ + z₂)))) = I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t z₁))) + I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t z₂))) rw [map_add, map_add, map_add, map_add] map_smul' := fun c z => by apply Subtype.ext change I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t (c • z)))) = c • I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t z))) rw [map_smul, map_smul, map_smul, map_smul] } comm := fun {s t} hst => by apply LinearMap.ext; intro z; apply Subtype.ext change I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t ((diagramModule K (shiftShrinkDiagram D₂ ε)).map hst z)))) = ((diagramModule K D₂).shift ε).map hst (I.φ.toFun s (castObj s (I.ψ.toFun (s - ε) (mkY s z)))) rw [hφ_cast t, hφ_cast s] change (diagramModule K D₂).map (by linarith : t - ε ≤ t + ε) _ = (diagramModule K D₂).map (by linarith : s + ε ≤ t + ε) ((diagramModule K D₂).map (by linarith : s - ε ≤ s + ε) _) rw [(diagramModule K D₂).map_comp_apply] funext j ⟨hj⟩ change (if htt_j : (D₂.bar j).b ≤ t - ε ∧ t - ε < (D₂.bar j).d then mkY t ((diagramModule K (shiftShrinkDiagram D₂ ε)).map hst z) j ⟨htt_j⟩ else 0) = (if hst_j : (D₂.bar j).b ≤ s - ε ∧ s - ε < (D₂.bar j).d then mkY s z j ⟨hst_j⟩ else 0) by_cases hS : j ∈ S₂ · have hj_lt_t : t < (D₂.bar j).d - ε := by linarith [hj.2] have hj_lt_s : s < (D₂.bar j).d - ε := by linarith [hst, hj.2] by_cases hst_j : (D₂.bar j).b ≤ s - ε ∧ s - ε < (D₂.bar j).d · have htt_j : (D₂.bar j).b ≤ t - ε ∧ t - ε < (D₂.bar j).d := ⟨by linarith [hst_j.1, hst], by linarith [hj.2]⟩ rw [dif_pos htt_j, dif_pos hst_j] have hsb : ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS⟩)).b ≤ s ∧ s < ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS⟩)).d := by constructor · show (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨j, hS⟩)).1).b + ε ≤ s simp only [Equiv.symm_apply_apply]; linarith [hst_j.1] · show s < (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨j, hS⟩)).1).d - ε simp only [Equiv.symm_apply_apply]; exact hj_lt_s change (if hS' : j ∈ S₂ then if hlt' : t < (D₂.bar j).d - ε then (if hs : ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS'⟩)).b ≤ s ∧ s < ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS'⟩)).d then z (S₂.equivFin ⟨j, hS'⟩) ⟨hs⟩ else 0) else 0 else 0) = (if hS' : j ∈ S₂ then if hlt' : s < (D₂.bar j).d - ε then z (S₂.equivFin ⟨j, hS'⟩) ⟨_⟩ else 0 else 0) rw [dif_pos hS, dif_pos hj_lt_t, dif_pos hsb, dif_pos hS, dif_pos hj_lt_s] · rw [dif_neg hst_j] by_cases htt_j : (D₂.bar j).b ≤ t - ε ∧ t - ε < (D₂.bar j).d · rw [dif_pos htt_j] have hsb_not : ¬ (((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS⟩)).b ≤ s ∧ s < ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS⟩)).d) := by intro h have h1 : (D₂.bar j).b + ε ≤ s := by have := h.1 change (D₂.bar (S₂.equivFin.symm (S₂.equivFin ⟨j, hS⟩)).1).b + ε ≤ s at this simpa only [Equiv.symm_apply_apply] using this exact hst_j ⟨by linarith, by linarith [hj_lt_s]⟩ change (if hS' : j ∈ S₂ then if hlt' : t < (D₂.bar j).d - ε then (if hs : ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS'⟩)).b ≤ s ∧ s < ((shiftShrinkDiagram D₂ ε).bar (S₂.equivFin ⟨j, hS'⟩)).d then z (S₂.equivFin ⟨j, hS'⟩) ⟨hs⟩ else 0) else 0 else 0) = 0 rw [dif_pos hS, dif_pos hj_lt_t, dif_neg hsb_not] · rw [dif_neg htt_j] · have hmk0 : ∀ (u : ℝ) (w : (diagramModule K (shiftShrinkDiagram D₂ ε)).obj u) (hu : (D₂.bar j).b ≤ u - ε ∧ u - ε < (D₂.bar j).d), mkY u w j ⟨hu⟩ = 0 := by intro u w hu change (if hS' : j ∈ S₂ then _ else 0) = 0 rw [dif_neg hS] split_ifs with h1 h2 h2 <;> simp [hmk0] } let ι₁ : RPersistenceModule.Hom (diagramModule K (shiftShrinkDiagram D₂ ε)) (diagramModule K D_im) := eIm.symm.toHom.comp ι₁_raw have hι₁_inj : ∀ t, Function.Injective (ι₁.toFun t) := by intro t z₁ z₂ heq have hval := congrArg Subtype.val ((eIm.equiv t).symm.injective heq) change (I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t z₁))) : (diagramModule K D₂).obj (t + ε)) = I.φ.toFun t (castObj t (I.ψ.toFun (t - ε) (mkY t z₂))) at hval rw [hφ_cast t, hφ_cast t] at hval funext r ⟨hr⟩ set j := (S₂.equivFin.symm r).1 have hjS : j ∈ S₂ := (S₂.equivFin.symm r).2 have hr1 : (D₂.bar j).b + ε ≤ t := hr.1 have hr2 : t < (D₂.bar j).d - ε := hr.2 have hte : (D₂.bar j).b ≤ t + ε ∧ t + ε < (D₂.bar j).d := ⟨by linarith, by linarith⟩ have htme : (D₂.bar j).b ≤ t - ε ∧ t - ε < (D₂.bar j).d := ⟨by linarith, by linarith⟩ have hpt := congrFun (congrFun hval j) ⟨hte⟩ change (if hs : (D₂.bar j).b ≤ t - ε ∧ t - ε < (D₂.bar j).d then mkY t z₁ j ⟨hs⟩ else 0) = (if hs : (D₂.bar j).b ≤ t - ε ∧ t - ε < (D₂.bar j).d then mkY t z₂ j ⟨hs⟩ else 0) at hpt rw [dif_pos htme, dif_pos htme] at hpt change (if hS' : j ∈ S₂ then if hlt' : t < (D₂.bar j).d - ε then z₁ (S₂.equivFin ⟨j, hS'⟩) ⟨_⟩ else 0 else 0) = (if hS' : j ∈ S₂ then if hlt' : t < (D₂.bar j).d - ε then z₂ (S₂.equivFin ⟨j, hS'⟩) ⟨_⟩ else 0 else 0) at hpt rw [dif_pos hjS, dif_pos hr2, dif_pos hjS, dif_pos hr2] at hpt have hreq : S₂.equivFin ⟨j, hjS⟩ = r := S₂.equivFin.apply_symm_apply r have hsub : ∀ (r' : Fin (shiftShrinkDiagram D₂ ε).n) (h1 h2 : _), r' = r → z₁ r' ⟨h1⟩ = z₂ r' ⟨h2⟩ → z₁ r ⟨hr⟩ = z₂ r ⟨hr⟩ := by rintro _ _ _ rfl h; exact h exact hsub _ _ _ hreq hpt -- Steps 4 & 5: Construct induced injections χ₁ : D_im ↪ D₁ and χ₂ : D_im ↪ D₂ have hχ₁_ex := induced_matching_epi_chain π₁ hπ₁_surj π₂ hπ₂_surj have hχ₂_ex := induced_matching_mono_chain ι₁ hι₁_inj ι₂ hι₂_inj -- Step 6: Compose χ₁ and χ₂ into a partial ε-matching M : PartialMatching D₁ D₂ rcases hχ₁_ex with ⟨χ₁, hχ₁_match, hχ₁_un⟩ rcases hχ₂_ex with ⟨χ₂, hχ₂_match, hχ₂_un⟩ let M : PartialMatching D₁ D₂ := { toFun := fun i => if h : ∃ k : Fin D_im.n, χ₁ k = i then some (χ₂ (Classical.choose h)) else none inj := fun {i₁ i₂ j} h1 h2 => by by_cases hex1 : ∃ k : Fin D_im.n, χ₁ k = i₁ · rw [dif_pos hex1] at h1 by_cases hex2 : ∃ k : Fin D_im.n, χ₁ k = i₂ · rw [dif_pos hex2] at h2 have hjeq : χ₂ (Classical.choose hex1) = χ₂ (Classical.choose hex2) := Option.some.inj (h1.trans h2.symm) have hkeq : Classical.choose hex1 = Classical.choose hex2 := χ₂.injective hjeq rw [← Classical.choose_spec hex1, ← Classical.choose_spec hex2, hkeq] · rw [dif_neg hex2] at h2; cases h2 · rw [dif_neg hex1] at h1; cases h1 } refine ⟨M, hε, ?_, ?_, ?_⟩ · intro i j hij dsimp only [M] at hij by_cases hex : ∃ k : Fin D_im.n, χ₁ k = i · rw [dif_pos hex] at hij set k := Classical.choose hex have hki : χ₁ k = i := Classical.choose_spec hex have hkj : χ₂ k = j := Option.some.inj hij have h1 := hχ₁_match k have h2 := hχ₂_match k rw [hki] at h1; rw [hkj] at h2 unfold barDist exact max_le (abs_le.mpr ⟨by linarith [h1.1, h2.2.1], by linarith [h1.1, h2.2.2]⟩) (abs_le.mpr ⟨by linarith [h1.2.2, h2.1], by linarith [h1.2.1, h2.1]⟩) · rw [dif_neg hex] at hij; cases hij · intro i hi dsimp only [M] at hi have hnot : ∀ k : Fin D_im.n, χ₁ k ≠ i := by intro k hk have hex : ∃ k' : Fin D_im.n, χ₁ k' = i := ⟨k, hk⟩ rw [dif_pos hex] at hi; cases hi unfold diagDist; linarith [hχ₁_un i hnot] · intro j hj have hnot : ∀ k : Fin D_im.n, χ₂ k ≠ j := by intro k hk have hex : ∃ k' : Fin D_im.n, χ₁ k' = χ₁ k := ⟨k, rfl⟩ have hch : Classical.choose hex = k := χ₁.injective (Classical.choose_spec hex) have hMj : M.toFun (χ₁ k) = some j := by show (if h : ∃ k' : Fin D_im.n, χ₁ k' = χ₁ k then some (χ₂ (Classical.choose h)) else none) = some j rw [dif_pos hex, hch, hk] exact hj (χ₁ k) hMj unfold diagDist; linarith [hχ₂_un j hnot] /-- **Bottleneck ≤ Interleaving (`d_B ≤ d_I`)**: Taking the infimum over all `ε`-interleavings yields `d_B(D₁, D₂) ≤ d_I(U, V)`. -/ theorem bottleneckDist_le_interleavingDist (D₁ D₂ : Diagram) : bottleneckDist D₁ D₂ ≤ RPersistenceModule.interleavingDist (diagramModule K D₁) (diagramModule K D₂) := by apply le_csInf · obtain ⟨M, _⟩ := exists_optimal_bottleneck_matching D₁ D₂ exact ⟨M.cost, ⟨interleavingOfMatching K M M.hasCostLe_cost⟩⟩ · rintro ε ⟨I⟩ exact algebraic_stability_theorem I /-- **Exact Isometry Theorem (`d_I = d_B`)**: For any finite persistence diagrams `D₁, D₂` over an arbitrary field `K`, `d_I(diagramModule K D₁, diagramModule K D₂) = d_B(D₁, D₂)`. -/ theorem isometry_theorem (D₁ D₂ : Diagram) : RPersistenceModule.interleavingDist (diagramModule K D₁) (diagramModule K D₂) = bottleneckDist D₁ D₂ := le_antisymm (interleavingDist_le_bottleneckDist K D₁ D₂) (bottleneckDist_le_interleavingDist D₁ D₂) /-- **Exact `ε`-Interleaving ↔ `ε`-Matching Equivalence**: Two finite-barcode persistence modules are `ε`-interleaved (`ε ≥ 0`) if and only if their bottleneck distance is `≤ ε`. -/ theorem interleaved_iff_bottleneckDist_le (D₁ D₂ : Diagram) {ε : ℝ} : Nonempty (RPersistenceModule.Interleaving (diagramModule K D₁) (diagramModule K D₂) ε) ↔ bottleneckDist D₁ D₂ ≤ ε := by constructor · rintro ⟨I⟩ exact algebraic_stability_theorem I · intro hle obtain ⟨M, hM⟩ := (bottleneckDist_le_iff D₁ D₂ ε).mp hle exact ⟨interleavingOfMatching K M hM⟩ end BauerLesnick /-! ### Part 6: Sublevel-Set Filtration Stability & Exact Sharpness Witness -/ section SublevelStability variable {K : Type u} [Field K] /-- Sublevel-set `0`-th homology of a finite graph `(V, G)` with vertex filtration `f : V → ℝ`. At level `t`, vertices are `{v | f v ≤ t}` and edges are `{u ~ v | max(f u, f v) ≤ t}`. We model `H_0(G_{\le t}; K)` via connected-component equivalence classes of active vertices, and take the reduced kernel `H̃_0(G_{\le t}; K) = {c : C_0 → K | ∑ c = 0}` (which has only finite bars on every connected graph). -/ def SublevelAdj {V : Type*} (G : SimpleGraph V) (f : V → ℝ) (t : ℝ) (u v : {x : V // f x ≤ t}) : Prop := G.Adj u.1 v.1 abbrev SublevelComp {V : Type*} (G : SimpleGraph V) (f : V → ℝ) (t : ℝ) : Type _ := Quot (Relation.ReflTransGen (SublevelAdj G f t)) /-- Inclusion of connected components from sublevel `s` to sublevel `t` (`s ≤ t`), or from `f` at `s` to `g` at `t` whenever `f v ≤ s → g v ≤ t`. -/ def sublevelCompMap {V : Type*} (G : SimpleGraph V) {f g : V → ℝ} {s t : ℝ} (hfg : ∀ v : V, f v ≤ s → g v ≤ t) : SublevelComp G f s → SublevelComp G g t := Quot.lift (fun u : {x : V // f x ≤ s} => Quot.mk _ ⟨u.1, hfg u.1 u.2⟩) (fun a b hab => by apply Quot.sound induction hab with | refl => exact Relation.ReflTransGen.refl | tail _ hstep ih => exact Relation.ReflTransGen.tail ih hstep) /-- For `V : Type u` (same universe `u` as `K`), `sublevelH0Module` is the reduced `0`-th persistent homology module `H̃_0(G_{\le t}; K) = ker(augmentation : (Comp →₀ K) →ₗ[K] K)`. -/ noncomputable def sublevelH0Module (K : Type u) [Field K] {V : Type u} (G : SimpleGraph V) (f : V → ℝ) : RPersistenceModule K where obj t := LinearMap.ker (Finsupp.linearCombination K (fun _ : SublevelComp G f t => (1 : K))) addCommGroup _ := inferInstance module _ := inferInstance map {s t} hst := { toFun := fun ⟨c, hc⟩ => ⟨Finsupp.mapDomain (sublevelCompMap G (fun v hv => hv.trans hst)) c, by simp only [LinearMap.mem_ker] at hc ⊢ rw [Finsupp.linearCombination_apply, Finsupp.sum_mapDomain_index (fun _ => zero_smul K (1 : K)) (fun _ _ _ => add_smul _ _ (1 : K))] simpa [Finsupp.linearCombination_apply] using hc⟩ map_add' := fun _ _ => Subtype.ext Finsupp.mapDomain_add map_smul' := fun _ _ => Subtype.ext (Finsupp.mapDomain_smul _ _) } map_id t := by apply LinearMap.ext; rintro ⟨c, hc⟩ apply Subtype.ext have hid : sublevelCompMap G (fun v (hv : f v ≤ t) => hv.trans (le_refl t)) = id := by funext q; refine Quot.inductionOn q fun ⟨v, hv⟩ => rfl change Finsupp.mapDomain _ c = c rw [hid, Finsupp.mapDomain_id] map_comp {r s t} hrs hst := by apply LinearMap.ext; rintro ⟨c, hc⟩ apply Subtype.ext have hcomp : sublevelCompMap G (fun v (hv : f v ≤ s) => hv.trans hst) ∘ sublevelCompMap G (fun v (hv : f v ≤ r) => hv.trans hrs) = sublevelCompMap G (fun v (hv : f v ≤ r) => hv.trans (hrs.trans hst)) := by funext q; refine Quot.inductionOn q fun ⟨v, hv⟩ => rfl change Finsupp.mapDomain _ (Finsupp.mapDomain _ c) = Finsupp.mapDomain _ c rw [← Finsupp.mapDomain_comp, hcomp] /-- Uniform `L_∞` distance between functions on a finite type `V`. -/ noncomputable def supDist {V : Type*} [Fintype V] (f g : V → ℝ) : ℝ := by classical exact Finset.univ.fold max 0 fun v : V => |f v - g v| lemma supDist_nonneg {V : Type*} [Fintype V] (f g : V → ℝ) : 0 ≤ supDist f g := by classical unfold supDist exact PartialMatching.le_fold_max_init Finset.univ _ 0 lemma abs_sub_le_supDist {V : Type*} [Fintype V] (f g : V → ℝ) (v : V) : |f v - g v| ≤ supDist f g := by classical unfold supDist exact PartialMatching.le_fold_max_of_mem Finset.univ (fun x => |f x - g x|) 0 (Finset.mem_univ v) /-- **Sublevel-Set Interleaving Theorem**: If `∀ v, |f v - g v| ≤ ε` (`ε ≥ 0`), then `sublevelH0Module K G f` and `sublevelH0Module K G g` are explicitly `ε`-interleaved. -/ noncomputable def sublevelH0Interleaving (K : Type u) [Field K] {V : Type u} (G : SimpleGraph V) (f g : V → ℝ) {ε : ℝ} (hε : 0 ≤ ε) (hbound : ∀ v : V, |f v - g v| ≤ ε) : RPersistenceModule.Interleaving (sublevelH0Module K G f) (sublevelH0Module K G g) ε where hε := hε φ := { toFun := fun t => { toFun := fun ⟨c, hc⟩ => ⟨Finsupp.mapDomain (sublevelCompMap G (fun v hv => by have := (abs_le.mp (hbound v)).1; linarith)) c, by simp only [LinearMap.mem_ker] at hc ⊢ rw [Finsupp.linearCombination_apply, Finsupp.sum_mapDomain_index (fun _ => zero_smul K (1 : K)) (fun _ _ _ => add_smul _ _ (1 : K))] simpa [Finsupp.linearCombination_apply] using hc⟩ map_add' := fun _ _ => Subtype.ext Finsupp.mapDomain_add map_smul' := fun _ _ => Subtype.ext (Finsupp.mapDomain_smul _ _) } comm := fun {s t} hst => by apply LinearMap.ext; rintro ⟨c, hc⟩; apply Subtype.ext change Finsupp.mapDomain _ (Finsupp.mapDomain _ c) = Finsupp.mapDomain _ (Finsupp.mapDomain _ c) rw [← Finsupp.mapDomain_comp, ← Finsupp.mapDomain_comp] congr 1; funext q; refine Quot.inductionOn q fun ⟨v, hv⟩ => rfl } ψ := { toFun := fun t => { toFun := fun ⟨c, hc⟩ => ⟨Finsupp.mapDomain (sublevelCompMap G (fun v hv => by have := (abs_le.mp (hbound v)).2; linarith)) c, by simp only [LinearMap.mem_ker] at hc ⊢ rw [Finsupp.linearCombination_apply, Finsupp.sum_mapDomain_index (fun _ => zero_smul K (1 : K)) (fun _ _ _ => add_smul _ _ (1 : K))] simpa [Finsupp.linearCombination_apply] using hc⟩ map_add' := fun _ _ => Subtype.ext Finsupp.mapDomain_add map_smul' := fun _ _ => Subtype.ext (Finsupp.mapDomain_smul _ _) } comm := fun {s t} hst => by apply LinearMap.ext; rintro ⟨c, hc⟩; apply Subtype.ext change Finsupp.mapDomain _ (Finsupp.mapDomain _ c) = Finsupp.mapDomain _ (Finsupp.mapDomain _ c) rw [← Finsupp.mapDomain_comp, ← Finsupp.mapDomain_comp] congr 1; funext q; refine Quot.inductionOn q fun ⟨v, hv⟩ => rfl } left_comp := fun t => by apply LinearMap.ext; rintro ⟨c, hc⟩; apply Subtype.ext change Finsupp.mapDomain _ (Finsupp.mapDomain _ c) = Finsupp.mapDomain _ c rw [← Finsupp.mapDomain_comp] congr 1; funext q; refine Quot.inductionOn q fun ⟨v, hv⟩ => rfl right_comp := fun t => by apply LinearMap.ext; rintro ⟨c, hc⟩; apply Subtype.ext change Finsupp.mapDomain _ (Finsupp.mapDomain _ c) = Finsupp.mapDomain _ c rw [← Finsupp.mapDomain_comp] congr 1; funext q; refine Quot.inductionOn q fun ⟨v, hv⟩ => rfl /-- **Sublevel-Set Filtration Bottleneck Stability Theorem**: For any finite graph `G` on `V`, functions `f, g : V → ℝ`, and finite persistence diagrams `D_f, D_g` realizing `sublevelH0Module K G f` and `sublevelH0Module K G g`, `d_B(D_f, D_g) ≤ ‖f - g‖_∞`. -/ theorem sublevel_filtration_stability {V : Type u} [Fintype V] (G : SimpleGraph V) (f g : V → ℝ) {D_f D_g : Diagram} (ef : RPersistenceModule.Iso (sublevelH0Module K G f) (diagramModule K D_f)) (eg : RPersistenceModule.Iso (sublevelH0Module K G g) (diagramModule K D_g)) : bottleneckDist D_f D_g ≤ supDist f g := by have hI := (sublevelH0Interleaving K G f g (supDist_nonneg f g) (abs_sub_le_supDist f g)).transport ef eg exact algebraic_stability_theorem hI /-! ### Exact Sharpness Witness on the 3-Vertex Path `P₃` -/ /-- The 3-vertex path graph `P₃` on `ULift (Fin 3)` with edges `0 — 1 — 2`. -/ def path3Graph : SimpleGraph (ULift.{u} (Fin 3)) where Adj u v := (u.down = 0 ∧ v.down = 1) ∨ (u.down = 1 ∧ v.down = 0) ∨ (u.down = 1 ∧ v.down = 2) ∨ (u.down = 2 ∧ v.down = 1) symm := ⟨fun _ _ h => by rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ <;> tauto⟩ loopless := ⟨fun u h => by rcases h with ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ | ⟨h1, h2⟩ <;> omega⟩ /-- Peak-valley function `f_c = (0, 2c, 0)` on `P₃`. -/ abbrev path3F (c : ℝ) : ULift.{u} (Fin 3) → ℝ | ⟨0⟩ => 0 | ⟨1⟩ => 2 * c | ⟨2⟩ => 0 /-- Constant function `g_c = (c, c, c)` on `P₃`. -/ def path3G (c : ℝ) : ULift.{u} (Fin 3) → ℝ := fun _ => c /-- Diagram `{[0, 2c)}` for `f_c` on `P₃` (`c > 0`). -/ abbrev path3DiagF {c : ℝ} (hc : 0 < c) : Diagram where n := 1 bar _ := ⟨0, 2 * c, by linarith⟩ lemma path3_supDist {c : ℝ} (hc : 0 < c) : supDist (path3F.{u} c) (path3G.{u} c) = c := by classical apply le_antisymm · unfold supDist apply PartialMatching.fold_max_le _ _ _ _ hc.le intro ⟨v⟩ _ fin_cases v · simp [path3F, path3G, abs_of_neg (neg_lt_zero.mpr hc)] · simp only [path3F, path3G] rw [show 2 * c - c = c by ring, abs_of_pos hc] · simp [path3F, path3G, abs_of_neg (neg_lt_zero.mpr hc)] · have h0 := abs_sub_le_supDist (path3F.{u} c) (path3G.{u} c) ⟨0⟩ simpa [path3F, path3G, abs_of_neg (neg_lt_zero.mpr hc)] using h0 /-- Exact isomorphism `sublevelH0Module K path3Graph (path3G c) ≅ diagramModule K Diagram.empty`. -/ noncomputable def path3GIso (K : Type u) [Field K] (c : ℝ) : RPersistenceModule.Iso (sublevelH0Module K path3Graph (path3G c)) (diagramModule K Diagram.empty) := by refine { equiv := fun t => { toFun := fun _ k => Fin.elim0 k map_add' := fun _ _ => funext fun k => Fin.elim0 k map_smul' := fun _ _ => funext fun k => Fin.elim0 k invFun := fun _ => 0 left_inv := fun ⟨x, hx⟩ => by apply Subtype.ext by_cases ht : c ≤ t · have hsub : Subsingleton (SublevelComp path3Graph (path3G c) t) := by refine ⟨fun q₁ q₂ => Quot.inductionOn q₁ fun ⟨⟨u⟩, hu⟩ => Quot.inductionOn q₂ fun ⟨⟨v⟩, hv⟩ => ?_⟩ have h01 : Quot.mk (Relation.ReflTransGen (SublevelAdj path3Graph (path3G c) t)) ⟨⟨0⟩, ht⟩ = Quot.mk _ ⟨⟨1⟩, ht⟩ := Quot.sound (Relation.ReflTransGen.single (Or.inl ⟨rfl, rfl⟩)) have h12 : Quot.mk (Relation.ReflTransGen (SublevelAdj path3Graph (path3G c) t)) ⟨⟨1⟩, ht⟩ = Quot.mk _ ⟨⟨2⟩, ht⟩ := Quot.sound (Relation.ReflTransGen.single (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)))) fin_cases u <;> fin_cases v · rfl · exact h01 · exact h01.trans h12 · exact h01.symm · rfl · exact h12 · exact (h01.trans h12).symm · exact h12.symm · rfl let q0 : SublevelComp path3Graph (path3G c) t := Quot.mk _ ⟨⟨0⟩, ht⟩ ext q have hqq0 : q = q0 := Subsingleton.elim q q0 subst hqq0 simp only [LinearMap.mem_ker, Finsupp.linearCombination_apply] at hx have hsum : x.sum (fun _ a => a • (1 : K)) = x q0 := by rw [Finsupp.sum_eq_single q0] · simp · intro b _ hb; exact absurd (Subsingleton.elim b q0) hb · simp rw [hsum] at hx exact hx.symm · ext q refine Quot.inductionOn q fun ⟨_, hv⟩ => absurd hv ht right_inv := fun y => funext fun k => Fin.elim0 k } comm := fun _ => by apply LinearMap.ext; intro x; funext k; exact Fin.elim0 k } /-- Exact isomorphism `sublevelH0Module K path3Graph (path3F c) ≅ diagramModule K (path3DiagF hc)`. -/ noncomputable def path3FIso (K : Type u) [Field K] {c : ℝ} (hc : 0 < c) : RPersistenceModule.Iso (sublevelH0Module K path3Graph (path3F c)) (diagramModule K (path3DiagF hc)) := by classical have hneq_02 : ∀ (t : ℝ) (ht0 : 0 ≤ t), t < 2 * c → (Quot.mk (Relation.ReflTransGen (SublevelAdj path3Graph (path3F c) t)) ⟨⟨0⟩, by simpa [path3F] using ht0⟩ : SublevelComp path3Graph (path3F c) t) ≠ Quot.mk _ ⟨⟨2⟩, by simpa [path3F] using ht0⟩ := by intro t ht0 ht2 heq let inv : SublevelComp path3Graph (path3F c) t → Bool := Quot.lift (fun u => decide (u.1.down = 0)) fun a b hab => by induction hab with | refl => rfl | @tail x y _ hstep _ => exfalso rcases x with ⟨⟨vx⟩, hvx⟩ rcases y with ⟨⟨vy⟩, hvy⟩ unfold SublevelAdj path3Graph at hstep; dsimp only at hstep rcases hstep with ⟨_, h1⟩ | ⟨h1, _⟩ | ⟨h1, _⟩ | ⟨_, h1⟩ · rw [h1] at hvy; simp [path3F] at hvy; linarith · rw [h1] at hvx; simp [path3F] at hvx; linarith · rw [h1] at hvx; simp [path3F] at hvx; linarith · rw [h1] at hvy; simp [path3F] at hvy; linarith have hbool := congrArg inv heq simp [inv] at hbool let i0 : Fin (path3DiagF hc).n := ⟨0, Nat.zero_lt_one⟩ let invF (t : ℝ) (y : (diagramModule K (path3DiagF hc)).obj t) : (sublevelH0Module K path3Graph (path3F c)).obj t := if ht : 0 ≤ t ∧ t < 2 * c then let q0 : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨0⟩, ht.1⟩ let q2 : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨2⟩, ht.1⟩ let a : K := y i0 ⟨ht⟩ ⟨Finsupp.single q0 a + Finsupp.single q2 (-a), by rw [LinearMap.mem_ker, map_add, Finsupp.linearCombination_single, Finsupp.linearCombination_single] simp⟩ else 0 have hinv_pos : ∀ (t : ℝ) (ht : 0 ≤ t ∧ t < 2 * c) (y : (diagramModule K (path3DiagF hc)).obj t), (invF t y).1 = Finsupp.single (Quot.mk _ ⟨⟨0⟩, ht.1⟩ : SublevelComp path3Graph (path3F c) t) (y i0 ⟨ht⟩) + Finsupp.single (Quot.mk _ ⟨⟨2⟩, ht.1⟩ : SublevelComp path3Graph (path3F c) t) (-(y i0 ⟨ht⟩)) := by intro t ht y exact congrArg Subtype.val (dif_pos (α := (sublevelH0Module K path3Graph (path3F c)).obj t) ht) have hinv_neg : ∀ (t : ℝ) (ht : ¬ (0 ≤ t ∧ t < 2 * c)) (y : (diagramModule K (path3DiagF hc)).obj t), (invF t y).1 = 0 := by intro t ht y exact congrArg Subtype.val (dif_neg (α := (sublevelH0Module K path3Graph (path3F c)).obj t) ht) let toF (t : ℝ) (u : (sublevelH0Module K path3Graph (path3F c)).obj t) : (diagramModule K (path3DiagF hc)).obj t := fun _ ⟨ht⟩ => u.1 (Quot.mk _ ⟨⟨0⟩, ht.1⟩) refine { equiv := fun t => { toFun := toF t map_add' := fun _ _ => funext fun _ => funext fun _ => rfl map_smul' := fun _ _ => funext fun _ => funext fun _ => rfl invFun := invF t left_inv := fun u => by apply Subtype.ext by_cases ht : 0 ≤ t ∧ t < 2 * c · rw [hinv_pos t ht (toF t u)] rcases u with ⟨x, hx⟩ let q0 : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨0⟩, ht.1⟩ let q2 : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨2⟩, ht.1⟩ have hne : q0 ≠ q2 := hneq_02 t ht.1 ht.2 have hall : ∀ q : SublevelComp path3Graph (path3F c) t, q = q0 ∨ q = q2 := by intro q; refine Quot.inductionOn q fun ⟨⟨v⟩, hv⟩ => ?_ fin_cases v · exact Or.inl rfl · simp [path3F] at hv; linarith [ht.2] · exact Or.inr rfl have hsum0 : x q0 + x q2 = 0 := by simp only [LinearMap.mem_ker, Finsupp.linearCombination_apply] at hx have hsupp : x.support ⊆ {q0, q2} := by intro q _; rcases hall q with rfl | rfl <;> simp have hs : x.sum (fun _ a => a • (1 : K)) = x q0 + x q2 := by rw [Finsupp.sum_of_support_subset _ hsupp (fun _ a => a • (1 : K)) (by simp)] rw [Finset.sum_pair hne]; simp rw [hs] at hx; exact hx ext q rcases hall q with rfl | rfl · change (Finsupp.single q0 (x q0) + Finsupp.single q2 (-(x q0))) q0 = x q0 rw [Finsupp.add_apply, Finsupp.single_eq_same, Finsupp.single_eq_of_ne hne, add_zero] · change (Finsupp.single q0 (x q0) + Finsupp.single q2 (-(x q0))) q2 = x q2 rw [Finsupp.add_apply, Finsupp.single_eq_of_ne hne.symm, Finsupp.single_eq_same, zero_add] exact (eq_neg_of_add_eq_zero_right hsum0).symm · rw [hinv_neg t ht (toF t u)] rcases u with ⟨x, hx⟩ by_cases ht0 : 0 ≤ t · have ht2 : 2 * c ≤ t := not_lt.mp fun h2 => ht ⟨ht0, h2⟩ have hsub : Subsingleton (SublevelComp path3Graph (path3F c) t) := by refine ⟨fun q₁ q₂ => Quot.inductionOn q₁ fun ⟨⟨u⟩, hu⟩ => Quot.inductionOn q₂ fun ⟨⟨v⟩, hv⟩ => ?_⟩ have h0 : path3F.{u} c ⟨0⟩ ≤ t := by simpa [path3F] using ht0 have h1 : path3F.{u} c ⟨1⟩ ≤ t := by simpa [path3F] using ht2 have h2 : path3F.{u} c ⟨2⟩ ≤ t := by simpa [path3F] using ht0 have h01 : Quot.mk (Relation.ReflTransGen (SublevelAdj path3Graph (path3F c) t)) ⟨⟨0⟩, h0⟩ = Quot.mk _ ⟨⟨1⟩, h1⟩ := Quot.sound (Relation.ReflTransGen.single (Or.inl ⟨rfl, rfl⟩)) have h12 : Quot.mk (Relation.ReflTransGen (SublevelAdj path3Graph (path3F c) t)) ⟨⟨1⟩, h1⟩ = Quot.mk _ ⟨⟨2⟩, h2⟩ := Quot.sound (Relation.ReflTransGen.single (Or.inr (Or.inr (Or.inl ⟨rfl, rfl⟩)))) fin_cases u <;> fin_cases v · rfl · exact h01 · exact h01.trans h12 · exact h01.symm · rfl · exact h12 · exact (h01.trans h12).symm · exact h12.symm · rfl let q0 : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨0⟩, by simpa [path3F] using ht0⟩ ext q have hqq0 : q = q0 := Subsingleton.elim q q0 subst hqq0 simp only [LinearMap.mem_ker, Finsupp.linearCombination_apply] at hx have hsum : x.sum (fun _ a => a • (1 : K)) = x q0 := by rw [Finsupp.sum_eq_single q0] · simp · intro b _ hb; exact absurd (Subsingleton.elim b q0) hb · simp rw [hsum] at hx; exact hx.symm · ext q refine Quot.inductionOn q fun ⟨⟨v⟩, hv⟩ => ?_ fin_cases v <;> simp [path3F] at hv <;> linarith right_inv := fun y => by funext i ⟨ht⟩ have hi0 : i = i0 := Fin.ext (by have := i.isLt; dsimp [path3DiagF] at this; change i.val = 0; omega) subst hi0 have hht : 0 ≤ t ∧ t < 2 * c := ht have hne := hneq_02 t hht.1 hht.2 change (invF t y).1 (Quot.mk _ ⟨⟨0⟩, hht.1⟩) = y i0 ⟨hht⟩ rw [hinv_pos t hht y, Finsupp.add_apply, Finsupp.single_eq_same, Finsupp.single_eq_of_ne hne, add_zero] } comm := fun {s t} hst => by apply LinearMap.ext; rintro ⟨x, hx⟩; funext i ⟨ht⟩ have hht : 0 ≤ t ∧ t < 2 * c := ht change (Finsupp.mapDomain (sublevelCompMap path3Graph (fun v (hv : path3F c v ≤ s) => hv.trans hst)) x) (Quot.mk _ ⟨⟨0⟩, by simpa [path3F] using hht.1⟩) = if hs : 0 ≤ s ∧ s < 2 * c then x (Quot.mk _ ⟨⟨0⟩, by simpa [path3F] using hs.1⟩) else 0 by_cases hs : 0 ≤ s ∧ s < 2 * c · rw [dif_pos hs] let q0s : SublevelComp path3Graph (path3F c) s := Quot.mk _ ⟨⟨0⟩, by simpa [path3F] using hs.1⟩ let q2s : SublevelComp path3Graph (path3F c) s := Quot.mk _ ⟨⟨2⟩, by simpa [path3F] using hs.1⟩ let q0t : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨0⟩, by simpa [path3F] using hht.1⟩ let q2t : SublevelComp path3Graph (path3F c) t := Quot.mk _ ⟨⟨2⟩, by simpa [path3F] using hht.1⟩ have hne_t : q0t ≠ q2t := hneq_02 t hht.1 hht.2 have hall_s : ∀ q : SublevelComp path3Graph (path3F c) s, q = q0s ∨ q = q2s := by intro q; refine Quot.inductionOn q fun ⟨⟨v⟩, hv⟩ => ?_ fin_cases v · exact Or.inl rfl · simp [path3F] at hv; linarith [hs.2] · exact Or.inr rfl have hfinj : Function.Injective (sublevelCompMap path3Graph (fun v (hv : path3F c v ≤ s) => hv.trans hst)) := by intro a b hab rcases hall_s a with rfl | rfl <;> rcases hall_s b with rfl | rfl · rfl · exact absurd hab hne_t · exact absurd hab hne_t.symm · rfl change Finsupp.mapDomain (sublevelCompMap path3Graph (fun v (hv : path3F c v ≤ s) => hv.trans hst)) x (sublevelCompMap path3Graph (fun v (hv : path3F c v ≤ s) => hv.trans hst) q0s) = x q0s exact Finsupp.mapDomain_apply hfinj x q0s · rw [dif_neg hs] have hs0 : s < 0 := by by_contra hge; exact hs ⟨not_lt.mp hge, lt_of_le_of_lt hst hht.2⟩ have hx0 : x = 0 := by ext q; refine Quot.inductionOn q fun ⟨⟨v⟩, hv⟩ => ?_ fin_cases v <;> simp [path3F] at hv <;> linarith rw [hx0, Finsupp.mapDomain_zero]; rfl } /-- **Exact Sharpness Witness for Sublevel-Set Bottleneck Stability**: For every `c > 0`, on the 3-vertex path `P₃`, `f_c = (0, 2c, 0)` and `g_c = (c, c, c)` achieve `d_B(Dgm(f_c), Dgm(g_c)) = d_I(H̃_0(f_c), H̃_0(g_c)) = ‖f_c - g_c‖_∞ = c` with equality. -/ theorem sublevel_stability_exact_sharpness {c : ℝ} (hc : 0 < c) : supDist (path3F.{u} c) (path3G.{u} c) = c ∧ bottleneckDist (path3DiagF hc) Diagram.empty = c ∧ RPersistenceModule.interleavingDist (sublevelH0Module K path3Graph (path3F c)) (sublevelH0Module K path3Graph (path3G c)) = c := by have hsup : supDist (path3F.{u} c) (path3G.{u} c) = c := path3_supDist hc have hdb : bottleneckDist (path3DiagF hc) Diagram.empty = c := by apply le_antisymm · rw [bottleneckDist_le_iff] refine ⟨PartialMatching.empty _ _, hc.le, ?_, ?_, ?_⟩ · intro _ _ h; cases h · intro i _; unfold diagDist path3DiagF; dsimp; linarith · intro j; exact Fin.elim0 j · obtain ⟨M, hM⟩ := exists_optimal_bottleneck_matching (path3DiagF hc) Diagram.empty rw [← hM] let i0 : Fin (path3DiagF hc).n := ⟨0, Nat.zero_lt_one⟩ have hnone : M.toFun i0 = none := by cases h0 : M.toFun i0 with | none => rfl | some j => exact Fin.elim0 j have hc0 := M.hasCostLe_cost.2.2.1 i0 hnone unfold diagDist path3DiagF at hc0; dsimp at hc0 have hhalf : (2 * c - 0) / 2 = c := by ring rwa [hhalf] at hc0 have hdi : RPersistenceModule.interleavingDist (sublevelH0Module K path3Graph (path3F c)) (sublevelH0Module K path3Graph (path3G c)) = c := by have ef := path3FIso K hc have eg := path3GIso K c have heq_set : {ε : ℝ | Nonempty (RPersistenceModule.Interleaving (sublevelH0Module K path3Graph (path3F c)) (sublevelH0Module K path3Graph (path3G c)) ε)} = {ε : ℝ | Nonempty (RPersistenceModule.Interleaving (diagramModule K (path3DiagF hc)) (diagramModule K Diagram.empty) ε)} := by ext ε; constructor · rintro ⟨I⟩; exact ⟨I.transport ef eg⟩ · rintro ⟨I⟩; exact ⟨I.transport ef.symm eg.symm⟩ unfold RPersistenceModule.interleavingDist rw [heq_set] change RPersistenceModule.interleavingDist (diagramModule K (path3DiagF hc)) (diagramModule K Diagram.empty) = c rw [isometry_theorem, hdb] exact ⟨hsup, hdb, hdi⟩ end SublevelStability end OpenTDAPersistence.BottleneckInterleaving #print axioms OpenTDAPersistence.BottleneckInterleaving.exists_optimal_bottleneck_matching #print axioms OpenTDAPersistence.BottleneckInterleaving.interleavingDist_le_bottleneckDist #print axioms OpenTDAPersistence.BottleneckInterleaving.algebraic_stability_theorem #print axioms OpenTDAPersistence.BottleneckInterleaving.bottleneckDist_le_interleavingDist #print axioms OpenTDAPersistence.BottleneckInterleaving.isometry_theorem #print axioms OpenTDAPersistence.BottleneckInterleaving.interleaved_iff_bottleneckDist_le #print axioms OpenTDAPersistence.BottleneckInterleaving.sublevel_filtration_stability #print axioms OpenTDAPersistence.BottleneckInterleaving.sublevel_stability_exact_sharpness