openAI-math-dataset / lean /OAI /Analysis /Graph /Gradients.lean
roseking's picture
Add lean source and binary files (part 18)
67aee1d verified
Raw History Blame
8.25 kB
13
structure OrientedGraph (V E : Type*) where
body · 2 lines
  tail : E → V
  head : E → V
21
inductive Walk : V → V → Type _
body · 3 lines
  | nil (v : V) : Walk v v
  | forward {v : V} (e : E) (p : Walk v (G.tail e)) : Walk v (G.head e)
  | backward {v : V} (e : E) (p : Walk v (G.head e)) : Walk v (G.tail e)
26
def Connected : Prop := ∀ v w : V, Nonempty (G.Walk v w)
28
def gradients : Submodule ℝ (ℓ²(E, ℝ)) where
body · 16 lines
  carrier := {c | ∃ f : V → ℝ, ∀ e, c e = f (G.head e) - f (G.tail e)}
  zero_mem' := ⟨0, by intro e; simp⟩
  add_mem' := by
    rintro c d ⟨f, hf⟩ ⟨g, hg⟩
    refine ⟨f + g, ?_⟩
    intro e
    change c e + d e = (f (G.head e) + g (G.head e)) -
      (f (G.tail e) + g (G.tail e))
    rw [hf e, hg e]
    ring
  smul_mem' := by
    rintro r c ⟨f, hf⟩
    refine ⟨r • f, ?_⟩
    intro e
    change r * c e = r * f (G.head e) - r * f (G.tail e)
    rw [hf e, mul_sub]
47
theorem mem_gradients (c : ℓ²(E, ℝ)) :
    c ∈ G.gradients ↔ ∃ f : V → ℝ, ∀ e, c e = f (G.head e) - f (G.tail e) := Iff.rfl
50
def Walk.integral {G : OrientedGraph V E} {v w : V} : G.Walk v w → (ℓ²(E, ℝ) →L[ℝ] ℝ)
body · 3 lines
  | .nil _ => 0
  | .forward e p => p.integral + lp.evalCLM ℝ (fun _ : E => ℝ) 2 e
  | .backward e p => p.integral - lp.evalCLM ℝ (fun _ : E => ℝ) 2 e
55
theorem Walk.integral_gradient {G : OrientedGraph V E} {c : ℓ²(E, ℝ)} {f : V → ℝ}
    (hf : ∀ e, c e = f (G.head e) - f (G.tail e)) {v w : V}
    (p : G.Walk v w) : p.integral c = f w - f v := by
proof · 10 lines
  induction p with
  | nil => simp [Walk.integral]
  | forward e p ih =>
    change p.integral c + c e = f (G.head e) - f v
    rw [ih, hf e]
    ring
  | backward e p ih =>
    change p.integral c - c e = f (G.tail e) - f v
    rw [ih, hf e]
    ring
69
theorem eq_of_zero_differences (hG : G.Connected) {f : V → ℝ}
    (hf : ∀ e, f (G.head e) = f (G.tail e)) (v w : V) : f v = f w := by
proof · 5 lines
  have hgrad : ∀ e, (0 : ℓ²(E, ℝ)) e = f (G.head e) - f (G.tail e) := by
    intro e
    simp [hf e]
  have h := (Classical.choice (hG v w)).integral_gradient hgrad
  simpa only [map_zero, eq_comm, sub_eq_zero] using h.symm
77
theorem potential_unique (hG : G.Connected) (o : V) {f g : V → ℝ}
    (hedges : ∀ e, f (G.head e) - f (G.tail e) = g (G.head e) - g (G.tail e))
    (ho : f o = g o) : f = g := by
proof · 9 lines
  funext v
  have hzero : ∀ e, (f - g) (G.head e) = (f - g) (G.tail e) := by
    intro e
    have := hedges e
    dsimp
    linarith
  have h := G.eq_of_zero_differences hG hzero o v
  dsimp at h
  linarith
90
theorem l2_eq_zero_of_constant [Infinite V] (f : ℓ²(V, ℝ)) {r : ℝ}
    (hf : ∀ v, f v = r) : f = 0 := by
proof · 9 lines
  have hs : Summable (fun v : V => ‖r‖ ^ (2 : ℕ)) := by
    simpa only [hf, ENNReal.toReal_ofNat, Real.rpow_two] using
      (lp.memℓp f).summable (by norm_num : 0 < (2 : ENNReal).toReal)
  have hr : r = 0 := by
    have h := (summable_const_iff (β := V) (‖r‖ ^ (2 : ℕ))).mp hs
    simpa using h
  apply lp.ext
  funext v
  simpa only [lp.coeFn_zero, Pi.zero_apply, hr] using hf v
102
theorem l2_eq_zero_of_zero_differences [Infinite V] (hG : G.Connected)
    (f : ℓ²(V, ℝ)) (hf : ∀ e, f (G.head e) = f (G.tail e)) : f = 0 := by
proof · 2 lines
  let o : V := Classical.choice (inferInstance : Nonempty V)
  exact l2_eq_zero_of_constant f (fun v => G.eq_of_zero_differences hG hf v o)
107
noncomputable def normalizedPotential (hG : G.Connected) (o v : V) :
    ℓ²(E, ℝ) →L[ℝ] ℝ :=
body · 1 line
  (Classical.choice (hG o v)).integral
111
theorem normalizedPotential_gradient (hG : G.Connected) (o v : V)
    {c : ℓ²(E, ℝ)} {f : V → ℝ}
    (hf : ∀ e, c e = f (G.head e) - f (G.tail e)) :
    G.normalizedPotential hG o v c = f v - f o :=
proof · 1 line
  Walk.integral_gradient hf _
117
theorem mem_gradients_iff_normalized (hG : G.Connected) (o : V) (c : ℓ²(E, ℝ)) :
    c ∈ G.gradients ↔ ∀ e,
      c e = G.normalizedPotential hG o (G.head e) c -
        G.normalizedPotential hG o (G.tail e) c := by
proof · 7 lines
  constructor
  · rintro ⟨f, hf⟩ e
    rw [G.normalizedPotential_gradient hG o (G.head e) hf,
      G.normalizedPotential_gradient hG o (G.tail e) hf, hf e]
    ring
  · intro hc
    exact ⟨fun v => G.normalizedPotential hG o v c, hc⟩
129
theorem isClosed_gradients (hG : G.Connected) (o : V) :
    IsClosed (G.gradients : Set (ℓ²(E, ℝ))) := by
proof · 13 lines
  have hset : (G.gradients : Set (ℓ²(E, ℝ))) =
      ⋂ e : E, {c : ℓ²(E, ℝ) | c e =
        G.normalizedPotential hG o (G.head e) c -
          G.normalizedPotential hG o (G.tail e) c} := by
    ext c
    simp only [SetLike.mem_coe, Set.mem_iInter, Set.mem_ofPred,
      G.mem_gradients_iff_normalized hG o c]
  rw [hset]
  apply isClosed_iInter
  intro e
  exact isClosed_eq (lp.evalCLM ℝ (fun _ : E => ℝ) 2 e).continuous
    ((G.normalizedPotential hG o (G.head e)).continuous.sub
      (G.normalizedPotential hG o (G.tail e)).continuous)
145
theorem completeSpace_gradients (hG : G.Connected) (o : V) :
    CompleteSpace G.gradients :=
proof · 1 line
  (G.isClosed_gradients hG o).completeSpace_coe
149
def finiteSupportGradients : Submodule ℝ (ℓ²(E, ℝ)) where
body · 16 lines
  carrier := {c | ∃ f : V →₀ ℝ, ∀ e, c e = f (G.head e) - f (G.tail e)}
  zero_mem' := ⟨0, by intro e; simp⟩
  add_mem' := by
    rintro c d ⟨f, hf⟩ ⟨g, hg⟩
    refine ⟨f + g, ?_⟩
    intro e
    change c e + d e = (f (G.head e) + g (G.head e)) -
      (f (G.tail e) + g (G.tail e))
    rw [hf e, hg e]
    ring
  smul_mem' := by
    rintro r c ⟨f, hf⟩
    refine ⟨r • f, ?_⟩
    intro e
    change r * c e = r * f (G.head e) - r * f (G.tail e)
    rw [hf e, mul_sub]
167
def LocallyFinite : Prop :=
body · 1 line
  ∀ v : V, Set.Finite {e : E | G.tail e = v ∨ G.head e = v}
170
theorem hasFiniteSupport_differential (hG : G.LocallyFinite) (f : V →₀ ℝ) :
    (Function.support (fun e => f (G.head e) - f (G.tail e))).Finite := by
proof · 14 lines
  classical
  have hfinite : (⋃ v ∈ (f.support : Set V),
      {e : E | G.tail e = v ∨ G.head e = v}).Finite :=
    f.support.finite_toSet.biUnion (fun v _ => hG v)
  apply hfinite.subset
  intro e he
  change f (G.head e) - f (G.tail e) ≠ 0 at he
  have hside : G.head e ∈ f.support ∨ G.tail e ∈ f.support := by
    by_contra h
    push Not at h
    simp [Finsupp.notMem_support_iff.mp h.1, Finsupp.notMem_support_iff.mp h.2] at he
  rcases hside with hh | ht
  · exact Set.mem_iUnion₂.mpr ⟨G.head e, hh, Or.inr rfl⟩
  · exact Set.mem_iUnion₂.mpr ⟨G.tail e, ht, Or.inl rfl⟩
187
def finiteSupportDifferential (hG : G.LocallyFinite) :
    (V →₀ ℝ) →ₗ[ℝ] ℓ²(E, ℝ) where
body · 12 lines
  toFun f := ⟨fun e => f (G.head e) - f (G.tail e),
    (memℓp_zero (G.hasFiniteSupport_differential hG f)).of_exponent_ge (by positivity)⟩
  map_add' f g := by
    apply lp.ext
    funext e
    change f (G.head e) + g (G.head e) - (f (G.tail e) + g (G.tail e)) =
      (f (G.head e) - f (G.tail e)) + (g (G.head e) - g (G.tail e))
    ring
  map_smul' r f := by
    apply lp.ext
    funext e
    exact mul_sub r (f (G.head e)) (f (G.tail e)) |>.symm
203
theorem finiteSupportDifferential_apply (hG : G.LocallyFinite) (f : V →₀ ℝ) (e : E) :
    G.finiteSupportDifferential hG f e = f (G.head e) - f (G.tail e) := rfl
206
theorem range_finiteSupportDifferential (hG : G.LocallyFinite) :
    LinearMap.range (G.finiteSupportDifferential hG) = G.finiteSupportGradients := by
proof · 9 lines
  ext c
  constructor
  · rintro ⟨f, rfl⟩
    exact ⟨f, fun e => rfl⟩
  · rintro ⟨f, hf⟩
    refine ⟨f, ?_⟩
    apply lp.ext
    funext e
    exact (hf e).symm
218
def zeroBoundaryGradients : Submodule ℝ (ℓ²(E, ℝ)) :=
body · 1 line
  G.finiteSupportGradients.topologicalClosure
221
theorem finiteSupportGradients_le : G.finiteSupportGradients ≤ G.gradients := by
proof · 2 lines
  rintro c ⟨f, hf⟩
  exact ⟨f, hf⟩
225
theorem zeroBoundaryGradients_le (hG : G.Connected) (o : V) :
    G.zeroBoundaryGradients ≤ G.gradients :=
proof · 2 lines
  G.finiteSupportGradients.topologicalClosure_minimal G.finiteSupportGradients_le
    (G.isClosed_gradients hG o)