Eine aufbereitete Darstellung der Quelle

 
     
 
 
 
Anforderungen  |   Konzepte  |   Entwurf  |   Entwicklung  |   Qualitätssicherung  |   Lebenszyklus  |   Steuerung


Quelle  Basic.lean   Sprache: Lean

 

/-
Copyright (c) 2020 Sébastien Gouëzel. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Sébastien Gouëzel, Floris van Doorn
-/

module

public import Mathlib.Analysis.Calculus.TangentCone.Prod
public import Mathlib.Geometry.Manifold.ContMDiff.Defs
public import Mathlib.Geometry.Manifold.MFDeriv.Defs

import Mathlib.Geometry.Manifold.Notation

/-!
# Basic properties of the manifold Fréchet derivative

In this file, we show various properties of the manifold Fréchet derivative,
mimicking the API for Fréchet derivatives.
- basic properties of unique differentiability sets
- various general lemmas about the manifold Fréchet derivative
- deducing differentiability from smoothness,
- deriving continuity from differentiability on manifolds,
- congruence lemmas for derivatives on manifolds
- composition lemmas and the chain rule
-/


public section

assert_not_exists tangentBundleCore

open scoped Topology Manifold
open Function Set Bundle ChartedSpace

/-! ### Unique differentiability sets in manifolds -/

variable
  {�� : Type*} [NontriviallyNormedField ��]
  {E : Type*} [NormedAddCommGroup E] [NormedSpace �� E]
  {H : Type*} [TopologicalSpace H] (I : ModelWithCorners �� E H)
  {M : Type*} [TopologicalSpace M] [ChartedSpace H M]
  {E' : Type*} [NormedAddCommGroup E'] [NormedSpace �� E']
  {H' : Type*} [TopologicalSpace H'] {I' : ModelWithCorners �� E' H'}
  {M' : Type*} [TopologicalSpace M'] [ChartedSpace H' M']
  {E'' : Type*} [NormedAddCommGroup E''] [NormedSpace �� E'']
  {H'' : Type*} [TopologicalSpace H''] {I'' : ModelWithCorners �� E'' H''}
  {M'' : Type*} [TopologicalSpace M''] [ChartedSpace H'' M'']
  {f f₁ : M → M'} {x : M} {s t : Set M} {g : M' → M''} {u : Set M'}

theorem uniqueMDiffWithinAt_univ : UniqueMDiffAt[(univ : Set M)] x := by
  unfold UniqueMDiffWithinAt
  simp only [preimage_univ, univ_inter]
  exact I.uniqueDiffOn _ (mem_range_self _)

variable {I}

theorem uniqueMDiffWithinAt_iff_inter_range {s : Set M} {x : M} :
    UniqueMDiffAt[s] x ↔
      UniqueDiffWithinAt �� ((extChartAt I x).symm ⁻¹' s ∩ range I)
        ((extChartAt I x) x) := Iff.rfl

theorem uniqueMDiffWithinAt_iff {s : Set M} {x : M} :
    UniqueMDiffAt[s] x ↔
      UniqueDiffWithinAt �� ((extChartAt I x).symm ⁻¹' s ∩ (extChartAt I x).target)
        ((extChartAt I x) x) := by
  apply uniqueDiffWithinAt_congr
  rw [nhdsWithin_inter, nhdsWithin_inter, nhdsWithin_extChartAt_target_eq]

nonrec theorem UniqueMDiffWithinAt.mono_nhds {s t : Set M} {x : M} (hs : UniqueMDiffAt[s] x)
    (ht : ��[s] x ≤ ��[t] x) : UniqueMDiffAt[t] x :=
  hs.mono_nhds <| by simpa only [← map_extChartAt_nhdsWithin] using Filter.map_mono ht

theorem UniqueMDiffWithinAt.mono_of_mem_nhdsWithin {s t : Set M} {x : M}
    (hs : UniqueMDiffAt[s] x) (ht : t ∈ ��[s] x) : UniqueMDiffAt[t] x :=
  hs.mono_nhds (nhdsWithin_le_iff.2 ht)

theorem UniqueMDiffWithinAt.mono (h : UniqueMDiffAt[s] x) (st : s ⊆ t) :
    UniqueMDiffAt[t] x :=
  UniqueDiffWithinAt.mono h <| inter_subset_inter (preimage_mono st) (Subset.refl _)

theorem UniqueMDiffWithinAt.inter' (hs : UniqueMDiffAt[s] x) (ht : t ∈ ��[s] x) :
    UniqueMDiffAt[s ∩ t] x :=
  hs.mono_of_mem_nhdsWithin (Filter.inter_mem self_mem_nhdsWithin ht)

theorem UniqueMDiffWithinAt.inter (hs : UniqueMDiffAt[s] x) (ht : t ∈ �� x) :
    UniqueMDiffAt[s ∩ t] x :=
  hs.inter' (nhdsWithin_le_nhds ht)

theorem IsOpen.uniqueMDiffWithinAt (hs : IsOpen s) (xs : x ∈ s) : UniqueMDiffAt[s] x :=
  (uniqueMDiffWithinAt_univ I).mono_of_mem_nhdsWithin <| nhdsWithin_le_nhds <| hs.mem_nhds xs

theorem UniqueMDiffOn.inter (hs : UniqueMDiff[s]) (ht : IsOpen t) : UniqueMDiff[s ∩ t] :=
  fun _x hx => UniqueMDiffWithinAt.inter (hs _ hx.1) (ht.mem_nhds hx.2)

theorem IsOpen.uniqueMDiffOn (hs : IsOpen s) : UniqueMDiff[s] :=
  fun _x hx => hs.uniqueMDiffWithinAt hx

theorem uniqueMDiffOn_univ : UniqueMDiff[(univ : Set M)] :=
  isOpen_univ.uniqueMDiffOn

nonrec theorem UniqueMDiffWithinAt.prod {x : M} {y : M'} {s : Set M} {t : Set M'}
    (hs : UniqueMDiffAt[s] x) (ht : UniqueMDiffAt[t] y) : UniqueMDiffAt[s ×ˢ t] (x, y) := by
  refine (hs.prod ht).mono ?_
  rw [ModelWithCorners.range_prod, ← prod_inter_prod]
  rfl

theorem UniqueMDiffOn.prod {s : Set M} {t : Set M'} (hs : UniqueMDiff[s])
    (ht : UniqueMDiff[t]) : UniqueMDiff[s ×ˢ t] := fun x h ↦
  (hs x.1 h.1).prod (ht x.2 h.2)

theorem MDifferentiableWithinAt.mono (hst : s ⊆ t) (h : MDiffAt[t] f x) : MDiffAt[s] f x :=
  ⟨ContinuousWithinAt.mono h.1 hst, DifferentiableWithinAt.mono
    h.differentiableWithinAt_writtenInExtChartAt
    (inter_subset_inter_left _ (preimage_mono hst))⟩

theorem mdifferentiableWithinAt_univ : MDiffAt[univ] f x ↔ MDiffAt f x := by
  simp_rw [MDifferentiableWithinAt, MDifferentiableAt, ChartedSpace.LiftPropAt]

theorem mdifferentiableWithinAt_inter (ht : t ∈ �� x) : MDiffAt[s ∩ t] f x ↔ MDiffAt[s] f x := by
  rw [MDifferentiableWithinAt, MDifferentiableWithinAt,
    differentiableWithinAt_localInvariantProp.liftPropWithinAt_inter ht]

theorem mdifferentiableWithinAt_inter' (ht : t ∈ ��[s] x) : MDiffAt[s ∩ t] f x ↔ MDiffAt[s] f x := by
  rw [MDifferentiableWithinAt, MDifferentiableWithinAt,
    differentiableWithinAt_localInvariantProp.liftPropWithinAt_inter' ht]

theorem MDifferentiableAt.mdifferentiableWithinAt (h : MDiffAt f x) : MDiffAt[s] f x :=
  MDifferentiableWithinAt.mono (subset_univ _) (mdifferentiableWithinAt_univ.2 h)

theorem MDifferentiableWithinAt.mdifferentiableAt (h : MDiffAt[s] f x) (hs : s ∈ �� x) :
    MDiffAt f x := by
  have : s = univ ∩ s := by rw [univ_inter]
  rwa [this, mdifferentiableWithinAt_inter hs, mdifferentiableWithinAt_univ] at h

theorem MDifferentiableOn.mono (h : MDiff[t] f) (st : s ⊆ t) : MDiff[s] f :=
  fun x hx => (h x (st hx)).mono st

@[simp]
theorem mdifferentiableOn_empty : MDiff[∅] f := fun _x hx ↦ hx.elim

theorem mdifferentiableOn_univ : MDiff[univ] f ↔ MDiff f := by
  simp only [MDifferentiableOn, mdifferentiableWithinAt_univ, mfld_simps]; rfl

theorem MDifferentiableOn.mdifferentiableAt (h : MDiff[s] f) (hx : s ∈ �� x) : MDiffAt f x :=
  (h x (mem_of_mem_nhds hx)).mdifferentiableAt hx

theorem MDifferentiable.mdifferentiableOn (h : MDiff f) : MDiff[s] f :=
  (mdifferentiableOn_univ.2 h).mono (subset_univ _)

theorem mdifferentiableOn_of_locally_mdifferentiableOn
    (h : ∀ x ∈ s, ∃ u, IsOpen u ∧ x ∈ u ∧ MDiff[s ∩ u] f) : MDiff[s] f := by
  intro x xs
  rcases h x xs with ⟨t, t_open, xt, ht⟩
  exact (mdifferentiableWithinAt_inter (t_open.mem_nhds xt)).1 (ht x ⟨xs, xt⟩)

theorem MDifferentiable.mdifferentiableAt (hf : MDiff f) : MDiffAt f x :=
  hf x

/-!
### Relating differentiability in a manifold and differentiability in the model space
through extended charts
-/


theorem mdifferentiableWithinAt_iff_target_inter {f : M → M'} {s : Set M} {x : M} :
    MDiffAt[s] f x ↔
      ContinuousWithinAt f s x ∧
        DifferentiableWithinAt �� (writtenInExtChartAt I I' x f)
          ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' s) ((extChartAt I x) x) := by
  rw [mdifferentiableWithinAt_iff']
  refine and_congr Iff.rfl (exists_congr fun f' => ?_)
  rw [inter_comm]
  simp only [HasFDerivWithinAt, nhdsWithin_inter, nhdsWithin_extChartAt_target_eq]

/-- One can reformulate smoothness within a set at a point as continuity within this set at this
point, and smoothness in the corresponding extended chart. -/

theorem mdifferentiableWithinAt_iff :
    MDiffAt[s] f x ↔
      ContinuousWithinAt f s x ∧
        DifferentiableWithinAt �� (extChartAt I' (f x) ∘ f ∘ (extChartAt I x).symm)
          ((extChartAt I x).symm ⁻¹' s ∩ range I) (extChartAt I x x) := by
  simp_rw [MDifferentiableWithinAt, ChartedSpace.liftPropWithinAt_iff']; rfl

/-- One can reformulate smoothness within a set at a point as continuity within this set at this
point, and smoothness in the corresponding extended chart. This form states smoothness of `f`
written in such a way that the set is restricted to lie within the domain/codomain of the
corresponding charts.
Even though this expression is more complicated than the one in `mdifferentiableWithinAt_iff`, it is
a smaller set, but their germs at `extChartAt I x x` are equal. It is sometimes useful to rewrite
using this in the goal.
-/

theorem mdifferentiableWithinAt_iff_target_inter' :
    MDiffAt[s] f x ↔
      ContinuousWithinAt f s x ∧
        DifferentiableWithinAt �� (extChartAt I' (f x) ∘ f ∘ (extChartAt I x).symm)
          ((extChartAt I x).target ∩
            (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' (f x)).source))
          (extChartAt I x x) := by
  simp only [MDifferentiableWithinAt, liftPropWithinAt_iff']
  exact and_congr_right fun hc => differentiableWithinAt_congr_nhds
    hc.nhdsWithin_extChartAt_symm_preimage_inter_range

/-- One can reformulate smoothness within a set at a point as continuity within this set at this
point, and smoothness in the corresponding extended chart in the target. -/

theorem mdifferentiableWithinAt_iff_target :
    MDiffAt[s] f x ↔ ContinuousWithinAt f s x ∧ MDiffAt[s] (extChartAt I' (f x) ∘ f) x := by
  simp_rw [MDifferentiableWithinAt, liftPropWithinAt_iff', ← and_assoc]
  have cont :
    ContinuousWithinAt f s x ∧ ContinuousWithinAt (extChartAt I' (f x) ∘ f) s x ↔
        ContinuousWithinAt f s x :=
      and_iff_left_of_imp (continuousAt_extChartAt _).comp_continuousWithinAt
  simp_rw [cont, DifferentiableWithinAtProp, extChartAt, OpenPartialHomeomorph.extend,
    PartialEquiv.coe_trans, ModelWithCorners.toPartialEquiv_coe,
    OpenPartialHomeomorph.coe_toPartialEquiv, modelWithCornersSelf_coe, chartAt_self_eq,
    OpenPartialHomeomorph.refl_apply]
  rfl

theorem mdifferentiableAt_iff_target {x : M} :
    MDiffAt f x ↔ ContinuousAt f x ∧ MDiffAt (extChartAt I' (f x) ∘ f) x := by
  rw [← mdifferentiableWithinAt_univ, ← mdifferentiableWithinAt_univ,
    mdifferentiableWithinAt_iff_target, continuousWithinAt_univ]

section IsManifold

variable {e : OpenPartialHomeomorph M H} {e' : OpenPartialHomeomorph M' H'}

open IsManifold

theorem mdifferentiableWithinAt_iff_source_of_mem_maximalAtlas (he : e ∈ maximalAtlas I 1 M)
    (hx : x ∈ e.source) :
    MDiffAt[s] f x ↔
      MDiffAt[(e.extend I).symm ⁻¹' s ∩ range I] (f ∘ (e.extend I).symm) (e.extend I x) := by
  have h2x := hx; rw [← e.extend_source (I := I)] at h2x
  simp_rw [MDifferentiableWithinAt,
    differentiableWithinAt_localInvariantProp.liftPropWithinAt_indep_chart_source he hx,
    StructureGroupoid.liftPropWithinAt_self_source,
    e.extend_symm_continuousWithinAt_comp_right_iff, differentiableWithinAtProp_self_source,
    DifferentiableWithinAtProp, Function.comp, e.left_inv hx, (e.extend I).left_inv h2x]
  rfl

theorem mdifferentiableWithinAt_iff_source_of_mem_source
    [IsManifold I 1 M] {x' : M} (hx' : x' ∈ (chartAt H x).source) :
    MDiffAt[s] f x' ↔
      MDiffAt[(extChartAt I x).symm ⁻¹' s ∩ range I] (f ∘ (extChartAt I x).symm)
        (extChartAt I x x') :=
  mdifferentiableWithinAt_iff_source_of_mem_maximalAtlas (chart_mem_maximalAtlas x) hx'

theorem mdifferentiableAt_iff_source_of_mem_source
    [IsManifold I 1 M] {x' : M} (hx' : x' ∈ (chartAt H x).source) :
    MDiffAt f x' ↔ MDiffAt[range I] (f ∘ (extChartAt I x).symm) (extChartAt I x x') := by
  simp_rw [← mdifferentiableWithinAt_univ, mdifferentiableWithinAt_iff_source_of_mem_source hx',
    preimage_univ, univ_inter]

theorem mdifferentiableWithinAt_iff_target_of_mem_source
    [IsManifold I' 1 M'] {x : M} {y : M'} (hy : f x ∈ (chartAt H' y).source) :
    MDiffAt[s] f x ↔ ContinuousWithinAt f s x ∧ MDiffAt[s] (extChartAt I' y ∘ f) x := by
  simp_rw [MDifferentiableWithinAt]
  rw [differentiableWithinAt_localInvariantProp.liftPropWithinAt_indep_chart_target
      (chart_mem_maximalAtlas y) hy, and_congr_right]
  intro hf
  simp_rw [StructureGroupoid.liftPropWithinAt_self_target]
  simp_rw [((chartAt H' y).continuousAt hy).comp_continuousWithinAt hf]
  rw [← extChartAt_source I'] at hy
  simp_rw [(continuousAt_extChartAt' hy).comp_continuousWithinAt hf]
  rfl

theorem mdifferentiableAt_iff_target_of_mem_source
    [IsManifold I' 1 M'] {x : M} {y : M'} (hy : f x ∈ (chartAt H' y).source) :
    MDiffAt f x ↔ ContinuousAt f x ∧ MDiffAt (extChartAt I' y ∘ f) x := by
  rw [← mdifferentiableWithinAt_univ, mdifferentiableWithinAt_iff_target_of_mem_source hy,
    continuousWithinAt_univ, ← mdifferentiableWithinAt_univ]

theorem mdifferentiableWithinAt_iff_of_mem_maximalAtlas {x : M} (he : e ∈ maximalAtlas I 1 M)
    (he' : e' ∈ maximalAtlas I' 1 M') (hx : x ∈ e.source) (hy : f x ∈ e'.source) :
    MDiffAt[s] f x ↔
      ContinuousWithinAt f s x ∧
        DifferentiableWithinAt �� (e'.extend I' ∘ f ∘ (e.extend I).symm)
          ((e.extend I).symm ⁻¹' s ∩ range I) (e.extend I x) :=
  differentiableWithinAt_localInvariantProp.liftPropWithinAt_indep_chart he hx he' hy

/-- An alternative formulation of `mdifferentiableWithinAt_iff_of_mem_maximalAtlas`
if the set if `s` lies in `e.source`. -/

theorem mdifferentiableWithinAt_iff_image {x : M} (he : e ∈ maximalAtlas I 1 M)
    (he' : e' ∈ maximalAtlas I' 1 M') (hs : s ⊆ e.source) (hx : x ∈ e.source)
    (hy : f x ∈ e'.source) :
    MDiffAt[s] f x ↔
      ContinuousWithinAt f s x ∧
        DifferentiableWithinAt �� (e'.extend I' ∘ f ∘ (e.extend I).symm) (e.extend I '' s)
          (e.extend I x) := by
  rw [mdifferentiableWithinAt_iff_of_mem_maximalAtlas he he' hx hy, and_congr_right_iff]
  refine fun _ => differentiableWithinAt_congr_nhds ?_
  simp_rw [nhdsWithin_eq_iff_eventuallyEqSet,
    e.extend_symm_preimage_inter_range_eventuallyEqSet hs hx]

/-- One can reformulate smoothness within a set at a point as continuity within this set at this
point, and smoothness in any chart containing that point. -/

theorem mdifferentiableWithinAt_iff_of_mem_source [IsManifold I 1 M] [IsManifold I' 1 M']
    {x' : M} {y : M'} (hx : x' ∈ (chartAt H x).source) (hy : f x' ∈ (chartAt H' y).source) :
    MDiffAt[s] f x' ↔
      ContinuousWithinAt f s x' ∧
        DifferentiableWithinAt �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm)
          ((extChartAt I x).symm ⁻¹' s ∩ range I) (extChartAt I x x') :=
  mdifferentiableWithinAt_iff_of_mem_maximalAtlas (chart_mem_maximalAtlas x)
    (chart_mem_maximalAtlas y) hx hy

/-- One can reformulate smoothness within a set at a point as continuity within this set at this
point, and smoothness in any chart containing that point. Version requiring differentiability
in the target instead of `range I`. -/

theorem mdifferentiableWithinAt_iff_of_mem_source' [IsManifold I 1 M] [IsManifold I' 1 M']
    {x' : M} {y : M'} (hx : x' ∈ (chartAt H x).source) (hy : f x' ∈ (chartAt H' y).source) :
    MDiffAt[s] f x' ↔
      ContinuousWithinAt f s x' ∧
        DifferentiableWithinAt �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm)
          ((extChartAt I x).target ∩ (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' y).source))
          (extChartAt I x x') := by
  refine (mdifferentiableWithinAt_iff_of_mem_source hx hy).trans ?_
  rw [← extChartAt_source I] at hx
  rw [← extChartAt_source I'] at hy
  rw [and_congr_right_iff]
  set e := extChartAt I x; set e' := extChartAt I' (f x)
  refine fun hc => differentiableWithinAt_congr_nhds ?_
  rw [← e.image_source_inter_eq', ← map_extChartAt_nhdsWithin_eq_image' hx,
    ← map_extChartAt_nhdsWithin' hx, inter_comm, nhdsWithin_inter_of_mem]
  exact hc (extChartAt_source_mem_nhds' hy)

theorem mdifferentiableAt_iff_of_mem_source [IsManifold I 1 M] [IsManifold I' 1 M']
    {x' : M} {y : M'} (hx : x' ∈ (chartAt H x).source) (hy : f x' ∈ (chartAt H' y).source) :
    MDiffAt f x' ↔
      ContinuousAt f x' ∧
        DifferentiableWithinAt �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) (range I)
          (extChartAt I x x') :=
  (mdifferentiableWithinAt_iff_of_mem_source hx hy).trans <| by
    rw [continuousWithinAt_univ, preimage_univ, univ_inter]

theorem mdifferentiableOn_iff_of_mem_maximalAtlas (he : e ∈ maximalAtlas I 1 M)
    (he' : e' ∈ maximalAtlas I' 1 M') (hs : s ⊆ e.source) (h2s : MapsTo f s e'.source) :
    MDiff[s] f ↔
      ContinuousOn f s ∧
        DifferentiableOn �� (e'.extend I' ∘ f ∘ (e.extend I).symm) (e.extend I '' s) := by
  simp_rw [ContinuousOn, DifferentiableOn, Set.forall_mem_image, ← forall_and, MDifferentiableOn]
  exact forall₂_congr fun x hx => mdifferentiableWithinAt_iff_image he he' hs (hs hx) (h2s hx)

/-- Differentiability on a set is equivalent to differentiability in the extended charts. -/
theorem mdifferentiableOn_iff_of_mem_maximalAtlas' (he : e ∈ maximalAtlas I 1 M)
    (he' : e' ∈ maximalAtlas I' 1 M') (hs : s ⊆ e.source) (h2s : MapsTo f s e'.source) :
    MDiff[s] f ↔
      DifferentiableOn �� (e'.extend I' ∘ f ∘ (e.extend I).symm) (e.extend I '' s) :=
  (mdifferentiableOn_iff_of_mem_maximalAtlas he he' hs h2s).trans <| and_iff_right_of_imp fun h ↦
    (e.continuousOn_writtenInExtend_iff hs h2s).1 h.continuousOn

variable [IsManifold I 1 M] [IsManifold I' 1 M']

/-- If the set where you want `f` to be smooth lies entirely in a single chart, and `f` maps it
into a single chart, the smoothness of `f` on that set can be expressed by purely looking in
these charts.
Note: this lemma uses `extChartAt I x '' s` instead of `(extChartAt I x).symm ⁻¹' s` to ensure
that this set lies in `(extChartAt I x).target`. -/

theorem mdifferentiableOn_iff_of_subset_source
    {x : M} {y : M'} (hs : s ⊆ (chartAt H x).source) (h2s : MapsTo f s (chartAt H' y).source) :
    MDiff[s] f ↔
      ContinuousOn f s ∧
        DifferentiableOn �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) (extChartAt I x '' s) :=
  mdifferentiableOn_iff_of_mem_maximalAtlas (chart_mem_maximalAtlas x)
    (chart_mem_maximalAtlas y) hs h2s

/-- If the set where you want `f` to be smooth lies entirely in a single chart, and `f` maps it
into a single chart, the smoothness of `f` on that set can be expressed by purely looking in
these charts.
Note: this lemma uses `extChartAt I x '' s` instead of `(extChartAt I x).symm ⁻¹' s` to ensure
that this set lies in `(extChartAt I x).target`. -/

theorem mdifferentiableOn_iff_of_subset_source'
    {x : M} {y : M'} (hs : s ⊆ (extChartAt I x).source)
    (h2s : MapsTo f s (extChartAt I' y).source) :
    MDiff[s] f ↔
        DifferentiableOn �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm) (extChartAt I x '' s) := by
  rw [extChartAt_source] at hs h2s
  exact mdifferentiableOn_iff_of_mem_maximalAtlas' (chart_mem_maximalAtlas x)
    (chart_mem_maximalAtlas y) hs h2s

/-- One can reformulate smoothness on a set as continuity on this set, and smoothness in any
extended chart. -/

theorem mdifferentiableOn_iff :
    MDiff[s] f ↔
      ContinuousOn f s ∧
        ∀ (x : M) (y : M'),
          DifferentiableOn �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm)
            ((extChartAt I x).target ∩
              (extChartAt I x).symm ⁻¹' (s ∩ f ⁻¹' (extChartAt I' y).source)) := by
  constructor
  · intro h
    refine ⟨fun x hx => (h x hx).1, fun x y z hz => ?_⟩
    simp only [mfld_simps] at hz
    let w := (extChartAt I x).symm z
    have : w ∈ s := by simp only [w, hz, mfld_simps]
    specialize h w this
    have w1 : w ∈ (chartAt H x).source := by simp only [w, hz, mfld_simps]
    have w2 : f w ∈ (chartAt H' y).source := by simp only [w, hz, mfld_simps]
    convert ((mdifferentiableWithinAt_iff_of_mem_source w1 w2).mp h).2.mono _
    · simp only [w, hz, mfld_simps]
    · mfld_set_tac
  · rintro ⟨hcont, hdiff⟩ x hx
    refine differentiableWithinAt_localInvariantProp.liftPropWithinAt_iff.mpr ?_
    refine ⟨hcont x hx, ?_⟩
    dsimp [DifferentiableWithinAtProp]
    convert! hdiff x (f x) (extChartAt I x x) (by simp only [hx, mfld_simps]) using 1
    mfld_set_tac

/-- One can reformulate smoothness on a set as continuity on this set, and smoothness in any
extended chart in the target. -/

theorem mdifferentiableOn_iff_target :
    MDiff[s] f ↔
      ContinuousOn f s ∧
        ∀ y : M', MDiff[s ∩ f ⁻¹' (extChartAt I' y).source] (extChartAt I' y ∘ f) := by
  simp only [mdifferentiableOn_iff, ModelWithCorners.source_eq, chartAt_self_eq,
    OpenPartialHomeomorph.refl_partialEquiv, PartialEquiv.refl_trans, extChartAt,
    OpenPartialHomeomorph.extend, Set.preimage_univ, Set.inter_univ, and_congr_right_iff]
  intro h
  constructor
  · refine fun h' y => ⟨?_, fun x _ => h' x y⟩
    have h'' : ContinuousOn _ univ := (ModelWithCorners.continuous I').continuousOn
    convert! (h''.comp_inter (chartAt H' y).continuousOn_toFun).comp_inter h
    simp
  · exact fun h' x y => (h' y).2 x 0

/-- One can reformulate smoothness as continuity and smoothness in any extended chart. -/
theorem mdifferentiable_iff :
    MDiff f ↔
      Continuous f ∧
        ∀ (x : M) (y : M'),
          DifferentiableOn �� (extChartAt I' y ∘ f ∘ (extChartAt I x).symm)
            ((extChartAt I x).target ∩
              (extChartAt I x).symm ⁻¹' f ⁻¹' (extChartAt I' y).source) := by
  simp [← mdifferentiableOn_univ, mdifferentiableOn_iff, continuousOn_univ]

/-- One can reformulate smoothness as continuity and smoothness in any extended chart in the
target. -/

theorem mdifferentiable_iff_target :
    MDiff f ↔
      Continuous f ∧ ∀ y : M',
        MDiff[f ⁻¹' (extChartAt I' y).source] (extChartAt I' y ∘ f) := by
  rw [← mdifferentiableOn_univ, mdifferentiableOn_iff_target]
  simp [continuousOn_univ]

end IsManifold

/-! ### Deducing differentiability from smoothness -/

variable {n : WithTop ℕ∞}

theorem ContMDiffWithinAt.mdifferentiableWithinAt (hf : CMDiffAt[s] n f x) (hn : n ≠ 0) :
    MDiffAt[s] f x := by
  suffices h : MDiffAt[s ∩ f ⁻¹' (extChartAt I' (f x)).source] f x by
    rwa [mdifferentiableWithinAt_inter'] at h
    apply hf.1.preimage_mem_nhdsWithin
    exact extChartAt_source_mem_nhds (f x)
  rw [mdifferentiableWithinAt_iff]
  exact ⟨hf.1.mono inter_subset_left, (hf.2.differentiableWithinAt hn).mono (by mfld_set_tac)⟩

theorem ContMDiffAt.mdifferentiableAt (hf : CMDiffAt n f x) (hn : n ≠ 0) : MDiffAt f x :=
  mdifferentiableWithinAt_univ.1 <| ContMDiffWithinAt.mdifferentiableWithinAt hf hn

theorem ContMDiff.mdifferentiableAt (hf : CMDiff n f) (hn : n ≠ 0) : MDiffAt f x :=
  hf.contMDiffAt.mdifferentiableAt hn

theorem ContMDiff.mdifferentiableWithinAt (hf : CMDiff n f) (hn : n ≠ 0) : MDiffAt[s] f x :=
  (hf.contMDiffAt.mdifferentiableAt hn).mdifferentiableWithinAt

theorem ContMDiffOn.mdifferentiableOn (hf : CMDiff[s] n f) (hn : n ≠ 0) : MDiff[s] f :=
  fun x hx => (hf x hx).mdifferentiableWithinAt hn

theorem ContMDiff.mdifferentiable (hf : CMDiff n f) (hn : n ≠ 0) : MDiff f :=
  fun x => (hf x).mdifferentiableAt hn

/-! ### Deriving continuity from differentiability on manifolds -/

@[fun_prop]
theorem MDifferentiableOn.continuousOn (h : MDiff[s] f) : ContinuousOn f s :=
  fun x hx => (h x hx).continuousWithinAt

@[fun_prop]
theorem MDifferentiable.continuous (h : MDiff f) : Continuous f :=
  continuous_iff_continuousAt.2 fun x => (h x).continuousAt

theorem writtenInExtChartAt_comp (h : ContinuousWithinAt f s x) :
    writtenInExtChartAt I I'' x (g ∘ f)
      =ᶠ[��[(extChartAt I x).symm ⁻¹' s ∩ range I] (extChartAt I x x)]
        (writtenInExtChartAt I' I'' (f x) g ∘ writtenInExtChartAt I I' x f) := by
  apply
    @Filter.mem_of_superset _ _ (f ∘ (extChartAt I x).symm ⁻¹' (extChartAt I' (f x)).source) _
      (extChartAt_preimage_mem_nhdsWithin
        (h.preimage_mem_nhdsWithin (extChartAt_source_mem_nhds _)))
  mfld_set_tac

variable {f' f₀' f₁' : TangentSpace% x →L[��] TangentSpace% (f x)}
  {g' : TangentSpace% (f x) →L[��] TangentSpace% (g (f x))}

/-- `UniqueMDiffWithinAt` achieves its goal: it implies the uniqueness of the derivative. -/
protected nonrec theorem UniqueMDiffWithinAt.eq (U : UniqueMDiffAt[s] x)
    (h : HasMFDerivAt[s] f x f') (h₁ : HasMFDerivAt[s] f x f₁') : f' = f₁' :=
  U.eq (E := E) (F := E') h.2 h₁.2

protected theorem UniqueMDiffOn.eq (U : UniqueMDiff[s]) (hx : x ∈ s)
    (h : HasMFDerivAt[s] f x f') (h₁ : HasMFDerivAt[s] f x f₁') : f' = f₁' :=
  UniqueMDiffWithinAt.eq (U _ hx) h h₁

/-!
### General lemmas on derivatives of functions between manifolds

We mimic the API for functions between vector spaces
-/


@[simp, mfld_simps]
theorem mfderivWithin_univ : mfderiv[univ] f = mfderiv% f := by
  ext x : 1
  simp only [mfderivWithin, mfderiv, mfld_simps]
  rw [mdifferentiableWithinAt_univ]

theorem mfderivWithin_zero_of_not_mdifferentiableWithinAt (h : ¬MDiffAt[s] f x) :
    mfderiv[s] f x = 0 := by
  simp only [mfderivWithin, h, ite_eq_right, not_false_iff]

theorem mfderiv_zero_of_not_mdifferentiableAt (h : ¬MDiffAt f x) :
    mfderiv% f x = 0 := by simp only [mfderiv, h, ite_eq_right, not_false_iff]

@[nontriviality]
theorem mdifferentiable_of_subsingleton [Subsingleton E] : MDiff f := by
  intro x
  have : Subsingleton H := I.injective.subsingleton
  have : DiscreteTopology M := discreteTopology H M
  simp only [mdifferentiableAt_iff, continuous_of_discreteTopology.continuousAt, true_and]
  exact (hasFDerivAt_of_subsingleton _ _).differentiableAt.differentiableWithinAt

@[nontriviality]
theorem mdifferentiableWithinAt_of_subsingleton [Subsingleton E] : MDiffAt[s] f x :=
  (mdifferentiable_of_subsingleton x).mdifferentiableWithinAt

/-- If `f : M → M'` has injective differential at `x` within `s`,
it is `MDifferentiable` at `x` within `s`. -/

lemma mdifferentiableWithinAt_of_mfderivWithin_injective (hf : Injective (mfderiv[s] f x)) :
    MDiffAt[s] f x := by
  nontriviality E
  have : Nontrivial (TangentSpace% x) := inferInstanceAs (Nontrivial E)
  contrapose hf
  rw [mfderivWithin_zero_of_not_mdifferentiableWithinAt hf]
  exact not_injective_const

/-- If `f : M → M'` has injective differential at `x`, it is `MDifferentiable` at `x`. -/
lemma mdifferentiableAt_of_mfderiv_injective {f : M → M'} (hf : Injective (mfderiv% f x)) :
    MDiffAt f x := by
  simp only [← mdifferentiableWithinAt_univ, ← mfderivWithin_univ] at hf ⊢
  exact mdifferentiableWithinAt_of_mfderivWithin_injective hf

theorem mdifferentiableWithinAt_of_isInvertible_mfderivWithin (hf : (mfderiv[s] f x).IsInvertible) :
    MDiffAt[s] f x :=
  mdifferentiableWithinAt_of_mfderivWithin_injective hf.injective

theorem mdifferentiableAt_of_isInvertible_mfderiv (hf : (mfderiv% f x).IsInvertible) :
    MDiffAt f x :=
  mdifferentiableAt_of_mfderiv_injective hf.injective

theorem HasMFDerivWithinAt.mono (h : HasMFDerivAt[t] f x f') (hst : s ⊆ t) :
    HasMFDerivAt[s] f x f' :=
  ⟨ContinuousWithinAt.mono h.1 hst,
    HasFDerivWithinAt.mono h.2 (inter_subset_inter (preimage_mono hst) (Subset.refl _))⟩

theorem HasMFDerivAt.hasMFDerivWithinAt (h : HasMFDerivAt% f x f') : HasMFDerivAt[s] f x f' :=
  ⟨ContinuousAt.continuousWithinAt h.1, HasFDerivWithinAt.mono h.2 inter_subset_right⟩

theorem HasMFDerivWithinAt.mdifferentiableWithinAt (h : HasMFDerivAt[s] f x f') : MDiffAt[s] f x :=
  ⟨h.1, ⟨f', h.2⟩⟩

theorem HasMFDerivAt.mdifferentiableAt (h : HasMFDerivAt% f x f') : MDiffAt f x := by
  rw [mdifferentiableAt_iff]
  exact ⟨h.1, ⟨f', h.2⟩⟩

@[simp, mfld_simps]
theorem hasMFDerivWithinAt_univ : HasMFDerivAt[univ] f x f' ↔ HasMFDerivAt% f x f' := by
  simp only [HasMFDerivWithinAt, HasMFDerivAt, continuousWithinAt_univ, mfld_simps]

theorem hasMFDerivAt_unique (h₀ : HasMFDerivAt% f x f₀') (h₁ : HasMFDerivAt% f x f₁') :
    f₀' = f₁' := by
  rw [← hasMFDerivWithinAt_univ] at h₀ h₁
  exact (uniqueMDiffWithinAt_univ I).eq h₀ h₁

theorem hasMFDerivWithinAt_inter' (h : t ∈ ��[s] x) :
    HasMFDerivAt[s ∩ t] f x f' ↔ HasMFDerivAt[s] f x f' := by
  rw [HasMFDerivWithinAt, HasMFDerivWithinAt, extChartAt_preimage_inter_eq,
    hasFDerivWithinAt_inter', continuousWithinAt_inter' h]
  exact extChartAt_preimage_mem_nhdsWithin h

theorem hasMFDerivWithinAt_inter (h : t ∈ �� x) :
    HasMFDerivAt[s ∩ t] f x f' ↔ HasMFDerivAt[s] f x f' := by
  rw [HasMFDerivWithinAt, HasMFDerivWithinAt, extChartAt_preimage_inter_eq, hasFDerivWithinAt_inter,
    continuousWithinAt_inter h]
  exact extChartAt_preimage_mem_nhds h

theorem HasMFDerivWithinAt.union (hs : HasMFDerivAt[s] f x f') (ht : HasMFDerivAt[t] f x f') :
    HasMFDerivAt[s ∪ t] f x f' := by
  constructor
  · exact ContinuousWithinAt.union hs.1 ht.1
  · convert HasFDerivWithinAt.union hs.2 ht.2 using 1
    simp only [union_inter_distrib_right, preimage_union]

theorem HasMFDerivWithinAt.mono_of_mem_nhdsWithin (h : HasMFDerivAt[s] f x f') (ht : s ∈ ��[t] x) :
    HasMFDerivAt[t] f x f' :=
  (hasMFDerivWithinAt_inter' ht).1 (h.mono inter_subset_right)

theorem HasMFDerivWithinAt.hasMFDerivAt (h : HasMFDerivAt[s] f x f') (hs : s ∈ �� x) :
    HasMFDerivAt% f x f' := by
  rwa [← univ_inter s, hasMFDerivWithinAt_inter hs, hasMFDerivWithinAt_univ] at h

theorem MDifferentiableWithinAt.hasMFDerivWithinAt (h : MDiffAt[s] f x) :
    HasMFDerivAt[s] f x (mfderiv[s] f x) := by
  refine ⟨h.1, ?_⟩
  simp only [mfderivWithin, h, mfld_simps]
  exact DifferentiableWithinAt.hasFDerivWithinAt h.2

theorem mdifferentiableWithinAt_iff_exists_hasMFDerivWithinAt :
    MDiffAt[s] f x ↔ ∃ f', HasMFDerivWithinAt I I' f s x f' := by
  refine ⟨fun h ↦ ⟨mfderiv[s] f x, h.hasMFDerivWithinAt⟩, ?_⟩
  rintro ⟨f', hf'⟩
  exact hf'.mdifferentiableWithinAt

theorem MDifferentiableWithinAt.mono_of_mem_nhdsWithin (h : MDiffAt[s] f x) {t : Set M}
    (hst : s ∈ ��[t] x) : MDiffAt[t] f x :=
  (h.hasMFDerivWithinAt.mono_of_mem_nhdsWithin hst).mdifferentiableWithinAt

theorem MDifferentiableWithinAt.congr_nhds (h : MDiffAt[s] f x) {t : Set M}
    (hst : ��[s] x = ��[t] x) : MDiffAt[t] f x :=
  h.mono_of_mem_nhdsWithin <| hst ▸ self_mem_nhdsWithin

theorem mdifferentiableWithinAt_congr_nhds {t : Set M} (hst : ��[s] x = ��[t] x) :
    MDiffAt[s] f x ↔ MDiffAt[t] f x :=
  ⟨fun h => h.congr_nhds hst, fun h => h.congr_nhds hst.symm⟩

protected theorem MDifferentiableWithinAt.mfderivWithin (h : MDiffAt[s] f x) :
    mfderiv[s] f x =
      ((tangentSpaceCastModel I' (f x)).symm) ∘L
      (fderivWithin �� (writtenInExtChartAt I I' x f) ((extChartAt I x).symm ⁻¹' s ∩ range I)
        ((extChartAt I x) x)) ∘L (tangentSpaceCastModel I x) := by
  simp only [mfderivWithin, h, ite_eq_left]

/-- Formula for the manifold derivative of a map within a set, abusing the defeq between the tangent
space and the model space. Use instead `MDifferentiableWithinAt.mfderivWithin`, unless you have
good reasons not to do so. -/

protected theorem MDifferentiableWithinAt.mfderivWithin_abuse (h : MDiffAt[s] f x) :
    mfderiv[s] f x =
      fderivWithin �� (writtenInExtChartAt I I' x f) ((extChartAt I x).symm ⁻¹' s ∩ range I)
        ((extChartAt I x) x) :=
  h.mfderivWithin

theorem MDifferentiableAt.hasMFDerivAt (h : MDiffAt f x) : HasMFDerivAt% f x (mfderiv% f x) := by
  refine ⟨h.continuousAt, ?_⟩
  simp only [mfderiv, h, mfld_simps]
  exact DifferentiableWithinAt.hasFDerivWithinAt h.differentiableWithinAt_writtenInExtChartAt

protected theorem MDifferentiableAt.mfderiv (h : MDiffAt f x) :
    mfderiv% f x =
      ((tangentSpaceCastModel I' (f x)).symm) ∘L
      fderivWithin �� (writtenInExtChartAt I I' x f) (range I) ((extChartAt I x) x) ∘L
      (tangentSpaceCastModel I x) := by
  simp only [mfderiv, h, ite_eq_left]

/-- Formula for the manifold derivative of a map, abusing the defeq between the tangent space
and the model space. Use instead `MDifferentiableAt.mfderiv`, unless you have good reasons not
to do so. -/

protected theorem MDifferentiableAt.mfderiv_abuse (h : MDiffAt f x) :
    mfderiv% f x =
      fderivWithin �� (writtenInExtChartAt I I' x f) (range I) ((extChartAt I x) x) :=
  h.mfderiv

protected theorem HasMFDerivAt.mfderiv (h : HasMFDerivAt% f x f') : mfderiv% f x = f' :=
  (hasMFDerivAt_unique h h.mdifferentiableAt.hasMFDerivAt).symm

protected theorem HasMFDerivWithinAt.mfderivWithin (h : HasMFDerivAt[s] f x f')
    (hxs : UniqueMDiffAt[s] x) : mfderiv[s] f x = f' := by
  ext
  rw [hxs.eq h h.mdifferentiableWithinAt.hasMFDerivWithinAt]

theorem HasMFDerivWithinAt.mfderivWithin_eq_zero (h : HasMFDerivWithinAt I I' f s x 0) :
    mfderiv[s] f x = 0 := by
  simp only [mfld_simps, mfderivWithin, h.mdifferentiableWithinAt, ↓reduceIte]
  simp only [HasMFDerivWithinAt, mfld_simps] at h
  rw [fderivWithin_def, ite_eq_left (by exact h.2)]
  simp

protected theorem MDifferentiable.mfderivWithin (h : MDiffAt f x) (hxs : UniqueMDiffAt[s] x) :
    mfderiv[s] f x = mfderiv% f x := by
  apply HasMFDerivWithinAt.mfderivWithin _ hxs
  exact h.hasMFDerivAt.hasMFDerivWithinAt

theorem mfderivWithin_subset (st : s ⊆ t) (hs : UniqueMDiffAt[s] x) (h : MDiffAt[t] f x) :
    mfderiv[s] f x = mfderiv[t] f x :=
  ((MDifferentiableWithinAt.hasMFDerivWithinAt h).mono st).mfderivWithin hs

theorem mfderivWithin_inter (ht : t ∈ �� x) : mfderiv[s ∩ t] f x = mfderiv[s] f x := by
  rw [mfderivWithin, mfderivWithin, extChartAt_preimage_inter_eq, mdifferentiableWithinAt_inter ht,
    fderivWithin_inter (extChartAt_preimage_mem_nhds ht)]

theorem mfderivWithin_of_mem_nhds (h : s ∈ �� x) : mfderiv[s] f x = mfderiv% f x := by
  rw [← mfderivWithin_univ, ← univ_inter s, mfderivWithin_inter h]

lemma mfderivWithin_of_isOpen (hs : IsOpen s) (hx : x ∈ s) : mfderiv[s] f x = mfderiv% f x :=
  mfderivWithin_of_mem_nhds (hs.mem_nhds hx)

theorem hasMFDerivWithinAt_insert {y : M} :
    HasMFDerivAt[insert y s] f x f' ↔ HasMFDerivAt[s] f x f' := by
  have : T1Space M := I.t1Space M
  refine ⟨fun h => h.mono <| subset_insert y s, fun hf ↦ ?_⟩
  rcases eq_or_ne x y with rfl | h
  · rw [HasMFDerivWithinAt] at hf ⊢
    refine ⟨hf.1.insert, ?_⟩
    have : (extChartAt I x).target ∈
        ��[(extChartAt I x).symm ⁻¹' insert x s ∩ range I] (extChartAt I x) x :=
      nhdsWithin_mono _ inter_subset_right (extChartAt_target_mem_nhdsWithin x)
    rw [← hasFDerivWithinAt_inter' this]
    apply hf.2.insert.mono
    rintro z ⟨⟨hz, h2z⟩, h'z⟩
    simp only [mem_inter_iff, mem_preimage, mem_insert_iff, mem_range] at hz h2z ⊢
    rcases hz with xz | h'z
    · left
      have : x ∈ (extChartAt I x).source := mem_extChartAt_source x
      exact (((extChartAt I x).eq_symm_apply this h'z).1 xz.symm).symm
    · exact Or.inr ⟨h'z, h2z⟩
  · apply hf.mono_of_mem_nhdsWithin ?_
    simp_rw [nhdsWithin_insert_of_ne h, self_mem_nhdsWithin]

alias ⟨HasMFDerivWithinAt.of_insert, HasMFDerivWithinAt.insert'⟩ := hasMFDerivWithinAt_insert

protected theorem HasMFDerivWithinAt.insert (h : HasMFDerivAt[s] f x f') :
    HasMFDerivAt[insert x s] f x f' :=
  h.insert'

theorem hasMFDerivWithinAt_sdiff_singleton (y : M) :
    HasMFDerivAt[s \ {y}] f x f' ↔ HasMFDerivAt[s] f x f' := by
  rw [← hasMFDerivWithinAt_insert, insert_sdiff_singleton, hasMFDerivWithinAt_insert]

@[deprecated (since := "2026-06-03")]
alias hasMFDerivWithinAt_diff_singleton := hasMFDerivWithinAt_sdiff_singleton

theorem mfderivWithin_eq_mfderiv (hs : UniqueMDiffAt[s] x) (h : MDiffAt f x) :
    mfderiv[s] f x = mfderiv% f x := by
  rw [← mfderivWithin_univ]
  exact mfderivWithin_subset (subset_univ _) hs h.mdifferentiableWithinAt

theorem mdifferentiableWithinAt_insert_self : MDiffAt[insert x s] f x ↔ MDiffAt[s] f x :=
  ⟨fun h ↦ h.mono (subset_insert x s), fun h ↦ h.hasMFDerivWithinAt.insert.mdifferentiableWithinAt⟩

theorem mdifferentiableWithinAt_insert {y : M} : MDiffAt[insert y s] f x ↔ MDiffAt[s] f x := by
  rcases eq_or_ne x y with (rfl | h)
  · exact mdifferentiableWithinAt_insert_self
  have : T1Space M := I.t1Space M
  apply mdifferentiableWithinAt_congr_nhds
  exact nhdsWithin_insert_of_ne h

alias ⟨MDifferentiableWithinAt.of_insert, MDifferentiableWithinAt.insert'⟩ :=
mdifferentiableWithinAt_insert

protected theorem MDifferentiableWithinAt.insert (h : MDiffAt[s] f x) : MDiffAt[insert x s] f x :=
  h.insert'

/-! ### Being differentiable on a union of open sets can be tested on each set -/

section mdifferentiableOn_union

/-- If a function is differentiable on two open sets, it is also differentiable on their union. -/
lemma MDifferentiableOn.union_of_isOpen
    (hf : MDiff[s] f) (hf' : MDiff[t] f) (hs : IsOpen s) (ht : IsOpen t) : MDiff[s ∪ t] f := by
  intro x hx
  obtain (hx | hx) := hx
  · exact (hf x hx).mdifferentiableAt (hs.mem_nhds hx) |>.mdifferentiableWithinAt
  · exact (hf' x hx).mdifferentiableAt (ht.mem_nhds hx) |>.mdifferentiableWithinAt

/-- A function is differentiable on two open sets iff it is differentiable on their union. -/
lemma mdifferentiableOn_union_iff_of_isOpen (hs : IsOpen s) (ht : IsOpen t) :
    MDiff[s ∪ t] f ↔ MDiff[s] f ∧ MDiff[t] f :=
  ⟨fun h ↦ ⟨h.mono subset_union_left, h.mono subset_union_right⟩,
    fun ⟨hfs, hft⟩ ↦ MDifferentiableOn.union_of_isOpen hfs hft hs ht⟩

lemma mdifferentiable_of_mdifferentiableOn_union_of_isOpen (hf : MDiff[s] f) (hf' : MDiff[t] f)
    (hst : s ∪ t = univ) (hs : IsOpen s) (ht : IsOpen t) : MDiff f := by
  rw [← mdifferentiableOn_univ, ← hst]
  exact hf.union_of_isOpen hf' hs ht

/-- If a function is differentiable on open sets `s i`, it is differentiable on their union. -/
lemma MDifferentiableOn.iUnion_of_isOpen
    {ι : Type*} {s : ι → Set M} (hf : ∀ i : ι, MDiff[s i] f) (hs : ∀ i, IsOpen (s i)) :
    MDiff[⋃ i, s i] f := by
  rintro x ⟨si, ⟨i, rfl⟩, hxsi⟩
  exact (hf i).mdifferentiableAt ((hs i).mem_nhds hxsi) |>.mdifferentiableWithinAt

/-- A function is differentiable on a union of open sets `s i`
iff it is differentiable on each `s i`. -/

lemma mdifferentiableOn_iUnion_iff_of_isOpen {ι : Type*} {s : ι → Set M} (hs : ∀ i, IsOpen (s i)) :
    MDiff[⋃ i, s i] f ↔ ∀ i : ι, MDiff[s i] f :=
  ⟨fun h i ↦ h.mono <| subset_iUnion_of_subset i fun _ a ↦ a,
   fun h ↦ MDifferentiableOn.iUnion_of_isOpen h hs⟩

lemma mdifferentiable_of_mdifferentiableOn_iUnion_of_isOpen {ι : Type*} {s : ι → Set M}
    (hf : ∀ i : ι, MDiff[s i] f) (hs : ∀ i, IsOpen (s i)) (hs' : ⋃ i, s i = univ) : MDiff f := by
  rw [← mdifferentiableOn_univ, ← hs']
  exact MDifferentiableOn.iUnion_of_isOpen hf hs

end mdifferentiableOn_union

lemma injective_fderivWithin_writtenInExtChartAt {f : M → M'} {s : Set M} {p : M}
    (hf : Injective (mfderiv[s] f p)) :
    Injective (fderivWithin �� (writtenInExtChartAt I I' p f)
      ((extChartAt I p).symm ⁻¹' s ∩ range I) (extChartAt I p p)) := by
  rw [mfderivWithin, ite_eq_left (mdifferentiableWithinAt_of_mfderivWithin_injective hf)] at hf
  exact hf

lemma isInvertible_fderivWithin_writtenInExtChartAt {f : M → M'} {p : M}
    (hf : (mfderiv% f p).IsInvertible) :
    (fderivWithin �� (writtenInExtChartAt I I' p f) (range I) (extChartAt I p p)).IsInvertible := by
  rw [mfderiv, ite_eq_left (mdifferentiableAt_of_mfderiv_injective hf.injective)] at hf
  exact hf

/-! ### Deriving continuity from differentiability on manifolds -/

theorem HasMFDerivWithinAt.continuousWithinAt (h : HasMFDerivAt[s] f x f') :
    ContinuousWithinAt f s x :=
  h.1

theorem HasMFDerivAt.continuousAt (h : HasMFDerivAt% f x f') : ContinuousAt f x :=
  h.1

theorem tangentMapWithin_subset
    {p : TangentBundle I M} (st : s ⊆ t) (hs : UniqueMDiffAt[s] p.1) (h : MDiffAt[t] f p.1) :
    tangentMap[s] f p = tangentMap[t] f p := by
  simp only [tangentMapWithin, mfld_simps]
  rw [mfderivWithin_subset st hs h]

theorem tangentMapWithin_univ : tangentMap[(univ : Set M)] f = tangentMap% f := by
  ext p : 1
  simp only [tangentMapWithin, tangentMap, mfld_simps]

theorem tangentMapWithin_eq_tangentMap {p : TangentBundle I M} (hs : UniqueMDiffAt[s] p.1)
    (h : MDiffAt f p.1) : tangentMap[s] f p = tangentMap% f p := by
  rw [← mdifferentiableWithinAt_univ] at h
  rw [← tangentMapWithin_univ]
  exact tangentMapWithin_subset (subset_univ _) hs h

@[simp, mfld_simps]
theorem tangentMapWithin_proj {p : TangentBundle I M} : (tangentMap[s] f p).proj = f p.proj := rfl

@[simp, mfld_simps]
lemma tangentMapWithin_snd {X : TangentSpace% x} : (tangentMap[s] f X).2 = (mfderiv[s] f x) X := rfl

@[simp, mfld_simps]
theorem tangentMap_proj {p : TangentBundle I M} : (tangentMap% f p).proj = f p.proj := rfl

@[simp, mfld_simps]
lemma tangentMap_snd {X : TangentSpace% x} : (tangentMap% f X).2 = (mfderiv% f x) X := rfl

/-- If two sets coincide locally around `x`, except maybe at a point `y`, then their
preimage under `extChartAt x` coincide locally, except maybe at `extChartAt I x x`. -/

theorem preimage_extChartAt_eventuallyEqSet_compl_singleton (y : M) (h : s =ᶠ[��[{y}ᶜ] x] t) :
    (extChartAt I x).symm ⁻¹' s ∩ range I =ᶠ[��[{extChartAt I x x}ᶜ] (extChartAt I x x)]
    ((extChartAt I x).symm ⁻¹' t ∩ range I : Set E) := by
  have : T1Space M := I.t1Space M
  obtain ⟨u, u_mem, hu⟩ : ∃ u ∈ �� x, u ∩ {x}ᶜ ⊆ {y | y ∈ s ↔ y ∈ t} :=
    mem_nhdsWithin_iff_exists_mem_nhds_inter.1 (nhdsWithin_compl_singleton_le x y h)
  rw [← extChartAt_to_inv (I := I) x] at u_mem
  have B : (extChartAt I x).target ∪ (range I)ᶜ ∈ �� (extChartAt I x x) := by
    rw [← nhdsWithin_univ, ← union_compl_self (range I), nhdsWithin_union]
    apply Filter.union_mem_sup (extChartAt_target_mem_nhdsWithin x) self_mem_nhdsWithin
  apply mem_nhdsWithin_iff_exists_mem_nhds_inter.2
    ⟨_, Filter.inter_mem ((continuousAt_extChartAt_symm x).preimage_mem_nhds u_mem) B, ?_⟩
  rintro z ⟨hz, h'z⟩
  simp only [mem_ofPred_eq]
  by_cases hIz : z ∈ range I
  · simp only [mem_inter_iff, mem_preimage, mem_union, mem_compl_iff, hIz, not_true_eq_false,
      or_false, and_true] at hz ⊢
    apply hu ⟨hz.1, ?_⟩
    push _ ∈ _ at h'z ⊢
    rw [eq_comm, (extChartAt I x).eq_symm_apply (by simp) hz.2]
    exact Ne.symm h'z
  · simp [hIz]

@[deprecated (since := "2026-08-14")]
alias preimage_extChartAt_eventuallyEq_compl_singleton :=
  preimage_extChartAt_eventuallyEqSet_compl_singleton

/-! ### Congruence lemmas for derivatives on manifolds -/

/-- If two sets coincide locally, except maybe at a point, then it is equivalent to have a manifold
derivative within one or the other. -/

theorem hasMFDerivWithinAt_congr_set' (y : M) (h : s =ᶠ[��[{y}ᶜ] x] t) :
    HasMFDerivAt[s] f x f' ↔ HasMFDerivAt[t] f x f' := by
  have : T1Space M := I.t1Space M
  simp only [HasMFDerivWithinAt]
  refine and_congr ?_ ?_
  · exact continuousWithinAt_congr_set' _ h
  · apply hasFDerivWithinAt_congr_set' (extChartAt I x x)
    exact preimage_extChartAt_eventuallyEqSet_compl_singleton y h

theorem hasMFDerivWithinAt_congr_set (h : s =ᶠ[�� x] t) :
    HasMFDerivAt[s] f x f' ↔ HasMFDerivAt[t] f x f' :=
  hasMFDerivWithinAt_congr_set' x <| h.filter_mono inf_le_left

/-- If two sets coincide around a point (except possibly at a single point `y`), then it is
equivalent to be differentiable within one or the other set. -/

theorem mdifferentiableWithinAt_congr_set' (y : M) (h : s =ᶠ[��[{y}ᶜ] x] t) :
    MDiffAt[s] f x ↔ MDiffAt[t] f x := by
  simp only [mdifferentiableWithinAt_iff_exists_hasMFDerivWithinAt]
  exact exists_congr fun _ => hasMFDerivWithinAt_congr_set' _ h

theorem mdifferentiableWithinAt_congr_set (h : s =ᶠ[�� x] t) : MDiffAt[s] f x ↔ MDiffAt[t] f x := by
  simp only [mdifferentiableWithinAt_iff_exists_hasMFDerivWithinAt]
  exact exists_congr fun _ => hasMFDerivWithinAt_congr_set h

/-- If two sets coincide locally, except maybe at a point, then derivatives within these sets
are the same. -/

theorem mfderivWithin_congr_set' (y : M) (h : s =ᶠ[��[{y}ᶜ] x] t) :
    mfderiv[s] f x = mfderiv[t] f x := by
  by_cases hx : MDiffAt[s] f x
  · simp only [mfderivWithin, hx, (mdifferentiableWithinAt_congr_set' y h).1 hx, ↓reduceIte]
    apply fderivWithin_congr_set' (extChartAt I x x)
    exact preimage_extChartAt_eventuallyEqSet_compl_singleton y h
  · simp [mfderivWithin, hx, ← mdifferentiableWithinAt_congr_set' y h]

/-- If two sets coincide locally, then derivatives within these sets
are the same. -/

theorem mfderivWithin_congr_set (h : s =ᶠ[�� x] t) : mfderiv[s] f x = mfderiv[t] f x :=
  mfderivWithin_congr_set' x <| h.filter_mono inf_le_left

/-- If two sets coincide locally, except maybe at a point, then derivatives within these sets
coincide locally. -/

theorem mfderivWithin_eventually_congr_set' (y : M) (h : s =ᶠ[��[{y}ᶜ] x] t) :
    ∀ᶠ y in �� x, mfderiv[s] f y = mfderiv[t] f y :=
  (eventually_nhds_nhdsWithin.2 h).mono fun _ => mfderivWithin_congr_set' y

/-- If two sets coincide locally, then derivatives within these sets coincide locally. -/
theorem mfderivWithin_eventually_congr_set (h : s =ᶠ[�� x] t) :
    ∀ᶠ y in �� x, mfderiv[s] f y = mfderiv[t] f y :=
  mfderivWithin_eventually_congr_set' x <| h.filter_mono inf_le_left

theorem HasMFDerivAt.congr_mfderiv (h : HasMFDerivAt% f x f') (h' : f' = f₁') :
    HasMFDerivAt% f x f₁' :=
  h' ▸ h

theorem HasMFDerivWithinAt.congr_mfderiv (h : HasMFDerivAt[s] f x f') (h' : f' = f₁') :
    HasMFDerivAt[s] f x f₁' :=
  h' ▸ h

theorem HasMFDerivWithinAt.congr_of_eventuallyEq
    (h : HasMFDerivAt[s] f x f') (h₁ : f₁ =ᶠ[��[s] x] f) (hx : f₁ x = f x) :
    HasMFDerivAt[s] f₁ x
      ((tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L f') := by
  refine ⟨ContinuousWithinAt.congr_of_eventuallyEq h.1 h₁ hx, ?_⟩
  apply HasFDerivWithinAt.congr_of_eventuallyEq h.2
  · have :
      (extChartAt I x).symm ⁻¹' {y | f₁ y = f y} ∈
        ��[(extChartAt I x).symm ⁻¹' s ∩ range I] (extChartAt I x) x :=
      extChartAt_preimage_mem_nhdsWithin h₁
    apply Filter.mem_of_superset this fun y => _
    simp +contextual only [hx, mfld_simps]
  · simp only [hx, mfld_simps]

theorem HasMFDerivWithinAt.congr_mono
    (h : HasMFDerivAt[s] f x f') (ht : ∀ x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
    HasMFDerivAt[t] f₁ x
    ((tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L f') :=
  (h.mono h₁).congr_of_eventuallyEq (Filter.mem_inf_of_right ht) hx

theorem HasMFDerivAt.congr_of_eventuallyEq (h : HasMFDerivAt% f x f') (h₁ : f₁ =ᶠ[�� x] f) :
    HasMFDerivAt% f₁ x
    ((tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L f') := by
  rw [← hasMFDerivWithinAt_univ] at h ⊢
  apply h.congr_of_eventuallyEq _ (mem_of_mem_nhds h₁ :)
  rwa [nhdsWithin_univ]

theorem HasMFDerivAt.congr_of_eventuallyEq_abuse (h : HasMFDerivAt% f x f') (h₁ : f₁ =ᶠ[�� x] f) :
    HasMFDerivAt% f₁ x f' :=
  HasMFDerivAt.congr_of_eventuallyEq h h₁

theorem mdifferentiableWithinAt_congr (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : f₁ x = f x) :
    MDiffAt[s] f₁ x ↔ MDiffAt[s] f x :=
  differentiableWithinAt_localInvariantProp.liftPropWithinAt_congr_iff h₁ hx

theorem MDifferentiableWithinAt.congr_of_mem (h : MDiffAt[s] f x) (h₁ : ∀ y ∈ s, f₁ y = f y)
    (hx : x ∈ s) : MDiffAt[s] f₁ x :=
  differentiableWithinAt_localInvariantProp.liftPropWithinAt_congr_of_mem h h₁ hx

theorem mdifferentiableWithinAt_congr_of_mem (h₁ : ∀ y ∈ s, f₁ y = f y) (hx : x ∈ s) :
    MDiffAt[s] f₁ x ↔ MDiffAt[s] f x :=
  differentiableWithinAt_localInvariantProp.liftPropWithinAt_congr_iff_of_mem h₁ hx

theorem Filter.EventuallyEq.mdifferentiablefWithinAt_iff (h₁ : f₁ =ᶠ[��[s] x] f) (hx : f₁ x = f x) :
    MDiffAt[s] f₁ x ↔ MDiffAt[s] f x :=
  differentiableWithinAt_localInvariantProp.liftPropWithinAt_congr_iff_of_eventuallyEq h₁ hx

theorem MDifferentiableWithinAt.congr_of_eventuallyEq (h : MDiffAt[s] f x) (h₁ : f₁ =ᶠ[��[s] x] f)
    (hx : f₁ x = f x) : MDiffAt[s] f₁ x :=
  (h.hasMFDerivWithinAt.congr_of_eventuallyEq h₁ hx).mdifferentiableWithinAt

theorem MDifferentiableWithinAt.congr_of_eventuallyEq_of_mem
    (h : MDiffAt[s] f x) (h₁ : f₁ =ᶠ[��[s] x] f) (hx : x ∈ s) : MDiffAt[s] f₁ x :=
  h.congr_of_eventuallyEq h₁ (mem_of_mem_nhdsWithin hx h₁ :)

theorem MDifferentiableWithinAt.congr_of_eventuallyEq_insert
    (h : MDiffAt[s] f x) (h₁ : f₁ =ᶠ[��[insert x s] x] f) : MDiffAt[s] f₁ x :=
  (h.insert.congr_of_eventuallyEq_of_mem h₁ (mem_insert x s)).of_insert

theorem Filter.EventuallyEq.mdifferentiableWithinAt_iff (h₁ : f₁ =ᶠ[��[s] x] f) (hx : f₁ x = f x) :
    MDiffAt[s] f x ↔ MDiffAt[s] f₁ x :=
  mdifferentiablefWithinAt_iff h₁.symm hx.symm

theorem MDifferentiableWithinAt.congr_mono
    (h : MDiffAt[s] f x) (ht : ∀ x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t ⊆ s) :
    MDiffAt[t] f₁ x :=
  (HasMFDerivWithinAt.congr_mono h.hasMFDerivWithinAt ht hx h₁).mdifferentiableWithinAt

theorem MDifferentiableWithinAt.congr
    (h : MDiffAt[s] f x) (ht : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) :
    MDiffAt[s] f₁ x :=
  (HasMFDerivWithinAt.congr_mono h.hasMFDerivWithinAt ht hx (Subset.refl _)).mdifferentiableWithinAt

/-- Version of `MDifferentiableWithinAt.congr` where `x` need not be contained in `s`,
but `f` and `f₁` are equal on a set containing both. -/

theorem MDifferentiableWithinAt.congr'
    (h : MDiffAt[s] f x) (ht : ∀ x ∈ t, f₁ x = f x) (hst : s ⊆ t) (hxt : x ∈ t) : MDiffAt[s] f₁ x :=
  h.congr (fun _y hy ↦ ht _y (hst hy)) (ht x hxt)

theorem Filter.EventuallyEq.mdifferentiableAt_iff (h₁ : f₁ =ᶠ[�� x] f) :
    MDiffAt f₁ x ↔ MDiffAt f x :=
  differentiableWithinAt_localInvariantProp.liftPropAt_congr_iff_of_eventuallyEq h₁

theorem MDifferentiableOn.congr (h : MDiff[s] f) (h₁ : ∀ y ∈ s, f₁ y = f y) : MDiff[s] f₁ :=
  differentiableWithinAt_localInvariantProp.liftPropOn_congr h h₁

theorem mdifferentiableOn_congr (h₁ : ∀ y ∈ s, f₁ y = f y) : MDiff[s] f₁ ↔ MDiff[s] f :=
  differentiableWithinAt_localInvariantProp.liftPropOn_congr_iff h₁

theorem MDifferentiableOn.congr_mono (h : MDiff[s] f) (h' : ∀ x ∈ t, f₁ x = f x) (h₁ : t ⊆ s) :
    MDiff[t] f₁ := fun x hx =>
  (h x (h₁ hx)).congr_mono h' (h' x hx) h₁

theorem MDifferentiableAt.congr_of_eventuallyEq (h : MDiffAt f x) (hL : f₁ =ᶠ[�� x] f) :
    MDiffAt f₁ x :=
  (h.hasMFDerivAt.congr_of_eventuallyEq hL).mdifferentiableAt

theorem MDifferentiableWithinAt.mfderivWithin_congr_mono (h : MDiffAt[s] f x)
    (hs : ∀ x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : UniqueMDiffAt[t] x) (h₁ : t ⊆ s) :
    mfderiv[t] f₁ x =
      (tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L mfderiv[s] f x :=
  (HasMFDerivWithinAt.congr_mono h.hasMFDerivWithinAt hs hx h₁).mfderivWithin hxt

theorem MDifferentiableWithinAt.mfderivWithin_mono
    (h : MDiffAt[s] f x) (hxt : UniqueMDiffAt[t] x) (h₁ : t ⊆ s) :
    mfderiv[t] f x = mfderiv[s] f x :=
  h.mfderivWithin_congr_mono (fun _ _ ↦ rfl) rfl hxt h₁

theorem MDifferentiableWithinAt.mfderivWithin_mono_of_mem_nhdsWithin
    (h : MDiffAt[s] f x) (hxt : UniqueMDiffAt[t] x) (h₁ : s ∈ ��[t] x) :
    mfderiv[t] f x = mfderiv[s] f x :=
  (HasMFDerivWithinAt.mono_of_mem_nhdsWithin h.hasMFDerivWithinAt h₁).mfderivWithin hxt

theorem Filter.EventuallyEq.mfderivWithin_eq (hL : f₁ =ᶠ[��[s] x] f) (hx : f₁ x = f x) :
    mfderiv[s] f₁ x =
      (tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L mfderiv[s] f x := by
  by_cases h : MDiffAt[s] f x
  · unfold mfderivWithin
    simp only [h, (hL.mdifferentiableWithinAt_iff hx).1 h, ↓reduceIte, writtenInExtChartAt]
    rw [hx]
    congr 2
    apply Filter.EventuallyEq.fderivWithin_eq; swap
    · simp [hx]
    filter_upwards [extChartAt_preimage_mem_nhdsWithin (I := I) hL] with y hy
    simp only [preimage_ofPred_eq, mem_ofPred_eq] at hy
    simp [-extChartAt, hy]
  · unfold mfderivWithin
    rw [ite_eq_right h, ite_eq_right]
    · simp
    · rwa [← hL.mdifferentiableWithinAt_iff hx]

theorem Filter.EventuallyEq.mfderivWithin_eq_of_mem (hL : f₁ =ᶠ[��[s] x] f) (hx : x ∈ s) :
    mfderiv[s] f₁ x =
      (tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L mfderiv[s] f x :=
  hL.mfderivWithin_eq (mem_of_mem_nhdsWithin hx hL :)

theorem mfderivWithin_congr (hL : ∀ x ∈ s, f₁ x = f x) (hx : f₁ x = f x) :
    mfderiv[s] f₁ x =
      (tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L mfderiv[s] f x :=
  Filter.EventuallyEq.mfderivWithin_eq (Filter.eventuallyEq_of_mem self_mem_nhdsWithin hL) hx

theorem mfderivWithin_congr_of_mem (hL : ∀ x ∈ s, f₁ x = f x) (hx : x ∈ s) :
    mfderiv[s] f₁ x =
      (tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L mfderiv[s] f x :=
  Filter.EventuallyEq.mfderivWithin_eq_of_mem (Filter.eventuallyEq_of_mem self_mem_nhdsWithin hL) hx

theorem tangentMapWithin_congr (h : ∀ x ∈ s, f x = f₁ x) (p : TangentBundle I M) (hp : p.1 ∈ s) :
    tangentMap[s] f p = tangentMap[s] f₁ p := by
  refine TotalSpace.ext (h p.1 hp) ?_
  rw [tangentMapWithin, h p.1 hp, tangentMapWithin, mfderivWithin_congr h (h _ hp)]
  rfl

theorem Filter.EventuallyEq.mfderiv_eq (hL : f₁ =ᶠ[�� x] f) :
    mfderiv% f₁ x =
      (tangentSpaceCast I' (f x) (f₁ x) : TangentSpace I' (f x) →L[��] TangentSpace I' (f₁ x))
        ∘L mfderiv% f x := by
  have A : f₁ x = f x := (mem_of_mem_nhds hL :)
  rw [← mfderivWithin_univ, ← mfderivWithin_univ]
  rw [← nhdsWithin_univ] at hL
  exact hL.mfderivWithin_eq A

/-- A congruence lemma for `mfderiv`, (ab)using the fact that `TangentSpace I' (f x)` is
definitionally equal to `E'`. -/

theorem mfderiv_congr_point {x' : M} (h : x = x') :
    @Eq (E →L[��] E') (mfderiv% f x) (mfderiv% f x') := by subst h; rfl

/-- A congruence lemma for `mfderiv`, (ab)using the fact that `TangentSpace I' (f x)` is
definitionally equal to `E'`. -/

theorem mfderiv_congr {f' : M → M'} (h : f = f') :
    @Eq (E →L[��] E') (mfderiv% f x) (mfderiv% f' x) := by subst h; rfl

/-! ### Composition lemmas -/

variable (x)

theorem HasMFDerivWithinAt.comp (hg : HasMFDerivAt[u] g (f x) g')
    (hf : HasMFDerivAt[s] f x f') (hst : s ⊆ f ⁻¹' u) :
    HasMFDerivAt[s] (g ∘ f) x (g'.comp f') := by
  refine ⟨ContinuousWithinAt.comp hg.1 hf.1 hst, ?_⟩
  have A :
    HasFDerivWithinAt (writtenInExtChartAt I' I'' (f x) g ∘ writtenInExtChartAt I I' x f)
      (ContinuousLinearMap.comp g' f' : E →L[��] E'') ((extChartAt I x).symm ⁻¹' s ∩ range I)
      ((extChartAt I x) x) := by
    have :
      (extChartAt I x).symm ⁻¹' f ⁻¹' (extChartAt I' (f x)).source ∈
        ��[(extChartAt I x).symm ⁻¹' s ∩ range I] (extChartAt I x) x :=
      extChartAt_preimage_mem_nhdsWithin
        (hf.1.preimage_mem_nhdsWithin (extChartAt_source_mem_nhds _))
    unfold HasMFDerivWithinAt at *
    rw [← hasFDerivWithinAt_inter' this, ← extChartAt_preimage_inter_eq] at hf ⊢
    rw [← writtenInExtChartAt_apply_extChartAt (I := I) (mem_extChartAt_source x)] at hg
    apply HasFDerivWithinAt.comp ((extChartAt I x) x) hg.2 hf.2 _
    intro y hy
    simp only [mfld_simps] at hy
    have : f (((chartAt H x).symm : H → M) (I.symm y)) ∈ u := hst hy.1.1
    simp only [hy, this, mfld_simps]
  apply A.congr_of_eventuallyEq (writtenInExtChartAt_comp hf.1)
  simp only [mfld_simps]

/-- The **chain rule for manifolds**. -/
theorem HasMFDerivAt.comp (hg : HasMFDerivAt% g (f x) g') (hf : HasMFDerivAt% f x f') :
    HasMFDerivAt% (g ∘ f) x (g'.comp f') := by
  rw [← hasMFDerivWithinAt_univ] at *
  exact HasMFDerivWithinAt.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ

theorem HasMFDerivAt.comp_hasMFDerivWithinAt (hg : HasMFDerivAt% g (f x) g')
    (hf : HasMFDerivAt[s] f x f') : HasMFDerivAt[s] (g ∘ f) x (g'.comp f') := by
  rw [← hasMFDerivWithinAt_univ] at *
  exact HasMFDerivWithinAt.comp x (hg.mono (subset_univ _)) hf subset_preimage_univ

theorem MDifferentiableWithinAt.comp (hg : MDiffAt[u] g (f x)) (hf : MDiffAt[s] f x)
    (h : s ⊆ f ⁻¹' u) : MDifferentiableWithinAt I I'' (g ∘ f) s x := by
  rcases hf.2 with ⟨f', hf'⟩
  have F : HasMFDerivAt[s] f x f' := ⟨hf.1, hf'⟩
  rcases hg.2 with ⟨g', hg'⟩
  have G : HasMFDerivAt[u] g (f x) g' := ⟨hg.1, hg'⟩
  exact (HasMFDerivWithinAt.comp x G F h).mdifferentiableWithinAt

theorem MDifferentiableWithinAt.comp_of_eq
    {y : M'} (hg : MDiffAt[u] g y) (hf : MDiffAt[s] f x) (h : s ⊆ f ⁻¹' u) (hy : f x = y) :
    MDiffAt[s] (g ∘ f) x := by
  subst hy; exact hg.comp _ hf h

theorem MDifferentiableWithinAt.comp_of_preimage_mem_nhdsWithin
    (hg : MDiffAt[u] g (f x)) (hf : MDiffAt[s] f x) (h : f ⁻¹' u ∈ ��[s] x) : MDiffAt[s] (g ∘ f) x :=
  (hg.comp _ (hf.mono inter_subset_right) inter_subset_left).mono_of_mem_nhdsWithin
    (Filter.inter_mem h self_mem_nhdsWithin)

theorem MDifferentiableWithinAt.comp_of_preimage_mem_nhdsWithin_of_eq
    {y : M'} (hg : MDiffAt[u] g y) (hf : MDiffAt[s] f x) (h : f ⁻¹' u ∈ ��[s] x) (hy : f x = y) :
    MDifferentiableWithinAt I I'' (g ∘ f) s x := by
  subst hy; exact MDifferentiableWithinAt.comp_of_preimage_mem_nhdsWithin _ hg hf h

theorem MDifferentiableAt.comp (hg : MDiffAt g (f x)) (hf : MDiffAt f x) : MDiffAt (g ∘ f) x :=
  (hg.hasMFDerivAt.comp x hf.hasMFDerivAt).mdifferentiableAt

theorem MDifferentiableAt.comp_of_eq {y : M'} (hg : MDiffAt g y) (hf : MDiffAt f x) (hy : f x = y) :
    MDiffAt (g ∘ f) x := by
  subst hy; exact hg.comp _ hf

theorem MDifferentiableAt.comp_mdifferentiableWithinAt
    (hg : MDiffAt g (f x)) (hf : MDiffAt[s] f x) : MDiffAt[s] (g ∘ f) x := by
  rw [← mdifferentiableWithinAt_univ] at hg
  exact hg.comp _ hf (by simp)

theorem MDifferentiableAt.comp_mdifferentiableWithinAt_of_eq
    {y : M'} (hg : MDiffAt g y) (hf : MDiffAt[s] f x) (hy : f x = y) : MDiffAt[s] (g ∘ f) x := by
  subst hy; exact hg.comp_mdifferentiableWithinAt _ hf

theorem mfderivWithin_comp
    (hg : MDiffAt[u] g (f x)) (hf : MDiffAt[s] f x) (h : s ⊆ f ⁻¹' u) (hxs : UniqueMDiffAt[s] x) :
    mfderiv[s] (g ∘ f) x = (mfderiv[u] g (f x)).comp (mfderiv[s] f x) := by
  apply HasMFDerivWithinAt.mfderivWithin _ hxs
  exact HasMFDerivWithinAt.comp x hg.hasMFDerivWithinAt hf.hasMFDerivWithinAt h

theorem mfderivWithin_comp_of_eq {x : M} {y : M'} (hg : MDiffAt[u] g y)
    (hf : MDiffAt[s] f x) (h : s ⊆ f ⁻¹' u) (hxs : UniqueMDiffAt[s] x) (hy : f x = y) :
    mfderiv[s] (g ∘ f) x = (mfderiv[u] g y).comp (mfderiv[s] f x) := by
  subst hy; exact mfderivWithin_comp x hg hf h hxs

theorem mfderivWithin_comp_of_preimage_mem_nhdsWithin (hg : MDiffAt[u] g (f x))
    (hf : MDiffAt[s] f x) (h : f ⁻¹' u ∈ ��[s] x) (hxs : UniqueMDiffAt[s] x) :
    mfderiv[s] (g ∘ f) x = (mfderiv[u] g (f x)).comp (mfderiv[s] f x) := by
  have A : s ∩ f ⁻¹' u ∈ ��[s] x := Filter.inter_mem self_mem_nhdsWithin h
  have B : mfderiv[s] (g ∘ f) x = mfderiv[s ∩ f ⁻¹' u] (g ∘ f) x := by
    apply MDifferentiableWithinAt.mfderivWithin_mono_of_mem_nhdsWithin _ hxs A
    exact hg.comp _ (hf.mono inter_subset_left) inter_subset_right
  have C : mfderiv[s] f x = mfderiv[s ∩ f ⁻¹' u] f x :=
    MDifferentiableWithinAt.mfderivWithin_mono_of_mem_nhdsWithin (hf.mono inter_subset_left) hxs A
  rw [B, C]
  exact mfderivWithin_comp _ hg (hf.mono inter_subset_left) inter_subset_right (hxs.inter' h)

theorem mfderivWithin_comp_of_preimage_mem_nhdsWithin_of_eq {y : M'}
    (hg : MDiffAt[u] g y) (hf : MDiffAt[s] f x) (h : f ⁻¹' u ∈ ��[s] x) (hxs : UniqueMDiffAt[s] x)
    (hy : f x = y) : mfderiv[s] (g ∘ f) x = (mfderiv[u] g y).comp (mfderiv[s] f x) := by
  subst hy; exact mfderivWithin_comp_of_preimage_mem_nhdsWithin _ hg hf h hxs

theorem mfderiv_comp_mfderivWithin (hg : MDiffAt g (f x)) (hf : MDiffAt[s] f x)
    (hxs : UniqueMDiffAt[s] x) :
    mfderiv[s] (g ∘ f) x = (mfderiv% g (f x)).comp (mfderiv[s] f x) := by
  rw [← mfderivWithin_univ]
  exact mfderivWithin_comp _ hg.mdifferentiableWithinAt hf (by simp) hxs

theorem mfderiv_comp_mfderivWithin_of_eq {x : M} {y : M'} (hg : MDiffAt g y)
    (hf : MDiffAt[s] f x) (hxs : UniqueMDiffAt[s] x) (hy : f x = y) :
    mfderiv[s] (g ∘ f) x = (mfderiv% g y).comp (mfderiv[s] f x) := by
  subst hy; exact mfderiv_comp_mfderivWithin x hg hf hxs

theorem mfderiv_comp (hg : MDiffAt g (f x)) (hf : MDiffAt f x) :
    mfderiv% (g ∘ f) x = (mfderiv% g (f x)).comp (mfderiv% f x) := by
  apply HasMFDerivAt.mfderiv
  exact HasMFDerivAt.comp x hg.hasMFDerivAt hf.hasMFDerivAt

theorem mfderiv_comp_of_eq {x : M} {y : M'} (hg : MDiffAt g y) (hf : MDiffAt f x) (hy : f x = y) :
    mfderiv% (g ∘ f) x = (mfderiv% g (f x)).comp (mfderiv% f x) := by
  subst hy; exact mfderiv_comp x hg hf

theorem mfderiv_comp_apply (hg : MDiffAt g (f x)) (hf : MDiffAt f x) (v : TangentSpace% x) :
    mfderiv% (g ∘ f) x v = (mfderiv% g (f x)) ((mfderiv% f x) v) := by
  rw [mfderiv_comp _ hg hf]
  rfl

theorem mfderiv_comp_apply_of_eq
    {y : M'} (hg : MDiffAt g y) (hf : MDiffAt f x) (hy : f x = y) (v : TangentSpace% x) :
    mfderiv% (g ∘ f) x v = (mfderiv% g y) ((mfderiv% f x) v) := by
  subst hy; exact mfderiv_comp_apply _ hg hf v

theorem MDifferentiableOn.comp (hg : MDiff[u] g) (hf : MDiff[s] f) (st : s ⊆ f ⁻¹' u) :
    MDiff[s] (g ∘ f) := fun x hx =>
  MDifferentiableWithinAt.comp x (hg (f x) (st hx)) (hf x hx) st

theorem MDifferentiable.comp_mdifferentiableOn (hg : MDiff g) (hf : MDiff[s] f) :
    MDiff[s] (g ∘ f) := by
  rw [← mdifferentiableOn_univ] at hg
  exact hg.comp hf (by simp)

theorem MDifferentiable.comp (hg : MDiff g) (hf : MDiff f) : MDiff (g ∘ f) :=
  fun x => MDifferentiableAt.comp x (hg (f x)) (hf x)

theorem tangentMapWithin_comp_at (p : TangentBundle I M) (hg : MDiffAt[u] g (f p.1))
    (hf : MDiffAt[s] f p.1) (h : s ⊆ f ⁻¹' u) (hps : UniqueMDiffAt[s] p.1) :
    tangentMap[s] (g ∘ f) p = tangentMap[u] g (tangentMap[s] f p) := by
  simp only [tangentMapWithin, mfld_simps]
  rw [mfderivWithin_comp p.1 hg hf h hps]
  rfl

theorem tangentMap_comp_at (p : TangentBundle I M) (hg : MDiffAt g (f p.1)) (hf : MDiffAt f p.1) :
    tangentMap% (g ∘ f) p = tangentMap% g (tangentMap% f p) := by
  simp only [tangentMap, mfld_simps]
  rw [mfderiv_comp p.1 hg hf]
  rfl

theorem tangentMap_comp (hg : MDiff g) (hf : MDiff f) :
    tangentMap% (g ∘ f) = tangentMap% g ∘ tangentMap% f := by
  ext p : 1; exact tangentMap_comp_at _ (hg _) (hf _)

Messung
CHZG
8610010095

¤ Dauer der Verarbeitung: 0.12 Sekunden  (vorverarbeitet am  2026-11-24) ¤

*© Formatika GbR, Deutschland







                                                                                                                                                                                                                                                                                                                                                                                                     

 

Neuigkeiten

Aktuelles
Motto des Tages

Open Source Software


Gedichte
Musik
Bilder

Jenseits des Üblichen

Besucherstatistik

Besucherstatistik

Statistik
#Sources=1,188,151
#Domains=932,587