CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download

Try doing some basic maths questions in the Lean Theorem Prover. Functions, real numbers, equivalence relations and groups. Click on README.md and then on "Open in CoCalc with one click".

Project: Xena
Views: 18536
License: APACHE
/-
Copyright (c) 2019 Jeremy Avigad. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Jeremy Avigad, SΓ©bastien GouΓ«zel, Yury Kudryashov
-/

import analysis.asymptotics analysis.calculus.tangent_cone

/-!
# The FrΓ©chet derivative

Let `E` and `F` be normed spaces, `f : E β†’ F`, and `f' : E β†’L[π•œ] F` a
continuous π•œ-linear map, where `π•œ` is a non-discrete normed field. Then

  `has_fderiv_within_at f f' s x`

says that `f` has derivative `f'` at `x`, where the domain of interest
is restricted to `s`. We also have

  `has_fderiv_at f f' x := has_fderiv_within_at f f' x univ`

## Main results

In addition to the definition and basic properties of the derivative, this file contains the
usual formulas (and existence assertions) for the derivative of
* constants
* the identity
* bounded linear maps
* bounded bilinear maps
* sum of two functions
* multiplication of a function by a scalar constant
* negative of a function
* subtraction of two functions
* multiplication of a function by a scalar function
* multiplication of two scalar functions
* composition of functions (the chain rule)

For most binary operations we also define `const_op` and `op_const` theorems for the cases when
the first or second argument is a constant. This makes writing chains of `has_deriv_at`'s easier,
and they more frequently lead to the desired result.

One can also interpret the derivative of a function `f : π•œ β†’ E` as an element of `E` (by identifying
a linear function from `π•œ` to `E` with its value at `1`). Results on the FrΓ©chet derivative are
translated to this more elementary point of view on the derivative in the file `deriv.lean`. The
derivative of polynomials is handled there, as it is naturally one-dimensional.

## Implementation details

The derivative is defined in terms of the `is_o` relation, but also
characterized in terms of the `tendsto` relation.

We also introduce predicates `differentiable_within_at π•œ f s x` (where `π•œ` is the base field,
`f` the function to be differentiated, `x` the point at which the derivative is asserted to exist,
and `s` the set along which the derivative is defined), as well as `differentiable_at π•œ f x`,
`differentiable_on π•œ f s` and `differentiable π•œ f` to express the existence of a derivative.

To be able to compute with derivatives, we write `fderiv_within π•œ f s x` and `fderiv π•œ f x`
for some choice of a derivative if it exists, and the zero function otherwise. This choice only
behaves well along sets for which the derivative is unique, i.e., those for which the tangent
directions span a dense subset of the whole space. The predicates `unique_diff_within_at s x` and
`unique_diff_on s`, defined in `tangent_cone.lean` express this property. We prove that indeed
they imply the uniqueness of the derivative. This is satisfied for open subsets, and in particular
for `univ`. This uniqueness only holds when the field is non-discrete, which we request at the very
beginning: otherwise, a derivative can be defined, but it has no interesting properties whatsoever.

## Tags

derivative, differentiable, FrΓ©chet, calculus

-/

open filter asymptotics continuous_linear_map set
open_locale topological_space classical

noncomputable theory

set_option class.instance_max_depth 90

section

variables {π•œ : Type*} [nondiscrete_normed_field π•œ]
variables {E : Type*} [normed_group E] [normed_space π•œ E]
variables {F : Type*} [normed_group F] [normed_space π•œ F]
variables {G : Type*} [normed_group G] [normed_space π•œ G]

/-- A function `f` has the continuous linear map `f'` as derivative along the filter `L` if
`f x' = f x + f' (x' - x) + o (x' - x)` when `x'` converges along the filter `L`. This definition
is designed to be specialized for `L = 𝓝 x` (in `has_fderiv_at`), giving rise to the usual notion
of FrΓ©chet derivative, and for `L = nhds_within x s` (in `has_fderiv_within_at`), giving rise to
the notion of FrΓ©chet derivative along the set `s`. -/
def has_fderiv_at_filter (f : E β†’ F) (f' : E β†’L[π•œ] F) (x : E) (L : filter E) :=
is_o (Ξ» x', f x' - f x - f' (x' - x)) (Ξ» x', x' - x) L

/-- A function `f` has the continuous linear map `f'` as derivative at `x` within a set `s` if
`f x' = f x + f' (x' - x) + o (x' - x)` when `x'` tends to `x` inside `s`. -/
def has_fderiv_within_at (f : E β†’ F) (f' : E β†’L[π•œ] F) (s : set E) (x : E) :=
has_fderiv_at_filter f f' x (nhds_within x s)

/-- A function `f` has the continuous linear map `f'` as derivative at `x` if
`f x' = f x + f' (x' - x) + o (x' - x)` when `x'` tends to `x`. -/
def has_fderiv_at (f : E β†’ F) (f' : E β†’L[π•œ] F) (x : E) :=
has_fderiv_at_filter f f' x (𝓝 x)

variables (π•œ)

/-- A function `f` is differentiable at a point `x` within a set `s` if it admits a derivative
there (possibly non-unique). -/
def differentiable_within_at (f : E β†’ F) (s : set E) (x : E) :=
βˆƒf' : E β†’L[π•œ] F, has_fderiv_within_at f f' s x

/-- A function `f` is differentiable at a point `x` if it admits a derivative there (possibly
non-unique). -/
def differentiable_at (f : E β†’ F) (x : E) :=
βˆƒf' : E β†’L[π•œ] F, has_fderiv_at f f' x

/-- If `f` has a derivative at `x` within `s`, then `fderiv_within π•œ f s x` is such a derivative.
Otherwise, it is set to `0`. -/
def fderiv_within (f : E β†’ F) (s : set E) (x : E) : E β†’L[π•œ] F :=
if h : βˆƒf', has_fderiv_within_at f f' s x then classical.some h else 0

/-- If `f` has a derivative at `x`, then `fderiv π•œ f x` is such a derivative. Otherwise, it is
set to `0`. -/
def fderiv (f : E β†’ F) (x : E) : E β†’L[π•œ] F :=
if h : βˆƒf', has_fderiv_at f f' x then classical.some h else 0

/-- `differentiable_on π•œ f s` means that `f` is differentiable within `s` at any point of `s`. -/
def differentiable_on (f : E β†’ F) (s : set E) :=
βˆ€x ∈ s, differentiable_within_at π•œ f s x

/-- `differentiable π•œ f` means that `f` is differentiable at any point. -/
def differentiable (f : E β†’ F) :=
βˆ€x, differentiable_at π•œ f x

variables {π•œ}
variables {f fβ‚€ f₁ g : E β†’ F}
variables {f' fβ‚€' f₁' g' : E β†’L[π•œ] F}
variables (e : E β†’L[π•œ] F)
variables {x : E}
variables {s t : set E}
variables {L L₁ Lβ‚‚ : filter E}

lemma fderiv_within_zero_of_not_differentiable_within_at
  (h : Β¬ differentiable_within_at π•œ f s x) : fderiv_within π•œ f s x = 0 :=
have Β¬ βˆƒ f', has_fderiv_within_at f f' s x, from h,
by simp [fderiv_within, this]

lemma fderiv_zero_of_not_differentiable_at (h : Β¬ differentiable_at π•œ f x) : fderiv π•œ f x = 0 :=
have Β¬ βˆƒ f', has_fderiv_at f f' x, from h,
by simp [fderiv, this]

section derivative_uniqueness
/- In this section, we discuss the uniqueness of the derivative.
We prove that the definitions `unique_diff_within_at` and `unique_diff_on` indeed imply the
uniqueness of the derivative. -/

/-- If a function f has a derivative f' at x, a rescaled version of f around x converges to f', i.e.,
`n (f (x + (1/n) v) - f x)` converges to `f' v`. More generally, if `c n` tends to infinity and
`c n * d n` tends to `v`, then `c n * (f (x + d n) - f x)` tends to `f' v`. This lemma expresses
this fact, for functions having a derivative within a set. Its specific formulation is useful for
tangent cone related discussions. -/
theorem has_fderiv_within_at.lim (h : has_fderiv_within_at f f' s x) {Ξ± : Type*} (l : filter Ξ±)
  {c : Ξ± β†’ π•œ} {d : Ξ± β†’ E} {v : E} (dtop : βˆ€αΆ  n in l, x + d n ∈ s)
  (clim : tendsto (Ξ» n, βˆ₯c nβˆ₯) l at_top)
  (cdlim : tendsto (Ξ» n, c n β€’ d n) l (𝓝 v)) :
  tendsto (Ξ»n, c n β€’ (f (x + d n) - f x)) l (𝓝 (f' v)) :=
begin
  have tendsto_arg : tendsto (Ξ» n, x + d n) l (nhds_within x s),
  { conv in (nhds_within x s) { rw ← add_zero x },
    rw [nhds_within, tendsto_inf],
    split,
    { apply tendsto_const_nhds.add (tangent_cone_at.lim_zero l clim cdlim) },
    { rwa tendsto_principal } },
  have : is_o (Ξ» y, f y - f x - f' (y - x)) (Ξ» y, y - x) (nhds_within x s) := h,
  have : is_o (Ξ» n, f (x + d n) - f x - f' ((x + d n) - x)) (Ξ» n, (x + d n)  - x) l :=
    this.comp_tendsto tendsto_arg,
  have : is_o (Ξ» n, f (x + d n) - f x - f' (d n)) d l := by simpa only [add_sub_cancel'],
  have : is_o (Ξ»n, c n β€’ (f (x + d n) - f x - f' (d n))) (Ξ»n, c n β€’ d n) l :=
    (is_O_refl c l).smul_is_o this,
  have : is_o (Ξ»n, c n β€’ (f (x + d n) - f x - f' (d n))) (Ξ»n, (1:ℝ)) l :=
    this.trans_is_O (is_O_one_of_tendsto ℝ cdlim),
  have L1 : tendsto (Ξ»n, c n β€’ (f (x + d n) - f x - f' (d n))) l (𝓝 0) :=
    (is_o_one_iff ℝ).1 this,
  have L2 : tendsto (Ξ»n, f' (c n β€’ d n)) l (𝓝 (f' v)) :=
    tendsto.comp f'.cont.continuous_at cdlim,
  have L3 : tendsto (Ξ»n, (c n β€’ (f (x + d n) - f x - f' (d n)) +  f' (c n β€’ d n)))
            l (𝓝 (0 + f' v)) :=
    L1.add L2,
  have : (Ξ»n, (c n β€’ (f (x + d n) - f x - f' (d n)) +  f' (c n β€’ d n)))
          = (Ξ»n, c n β€’ (f (x + d n) - f x)),
    by { ext n, simp [smul_add] },
  rwa [this, zero_add] at L3
end

/-- `unique_diff_within_at` achieves its goal: it implies the uniqueness of the derivative. -/
theorem unique_diff_within_at.eq (H : unique_diff_within_at π•œ s x)
  (h : has_fderiv_within_at f f' s x) (h₁ : has_fderiv_within_at f f₁' s x) : f' = f₁' :=
begin
  have A : βˆ€y ∈ tangent_cone_at π•œ s x, f' y = f₁' y,
  { rintros y ⟨c, d, dtop, clim, cdlim⟩,
    exact tendsto_nhds_unique (by simp) (h.lim at_top dtop clim cdlim) (h₁.lim at_top dtop clim cdlim) },
  have B : βˆ€y ∈ submodule.span π•œ (tangent_cone_at π•œ s x), f' y = f₁' y,
  { assume y hy,
    apply submodule.span_induction hy,
    { exact Ξ»y hy, A y hy },
    { simp only [continuous_linear_map.map_zero] },
    { simp {contextual := tt} },
    { simp {contextual := tt} } },
  have C : βˆ€y ∈ closure ((submodule.span π•œ (tangent_cone_at π•œ s x)) : set E), f' y = f₁' y,
  { assume y hy,
    let K := {y | f' y = f₁' y},
    have : (submodule.span π•œ (tangent_cone_at π•œ s x) : set E) βŠ† K := B,
    have : closure (submodule.span π•œ (tangent_cone_at π•œ s x) : set E) βŠ† closure K :=
      closure_mono this,
    have : y ∈ closure K := this hy,
    rwa closure_eq_of_is_closed (is_closed_eq f'.continuous f₁'.continuous) at this },
  rw H.1 at C,
  ext y,
  exact C y (mem_univ _)
end

theorem unique_diff_on.eq (H : unique_diff_on π•œ s) (hx : x ∈ s)
  (h : has_fderiv_within_at f f' s x) (h₁ : has_fderiv_within_at f f₁' s x) : f' = f₁' :=
unique_diff_within_at.eq (H x hx) h h₁

end derivative_uniqueness

section fderiv_properties
/-! ### Basic properties of the derivative -/

theorem has_fderiv_at_filter_iff_tendsto :
  has_fderiv_at_filter f f' x L ↔
  tendsto (Ξ» x', βˆ₯x' - xβˆ₯⁻¹ * βˆ₯f x' - f x - f' (x' - x)βˆ₯) L (𝓝 0) :=
have h : βˆ€ x', βˆ₯x' - xβˆ₯ = 0 β†’ βˆ₯f x' - f x - f' (x' - x)βˆ₯ = 0, from Ξ» x' hx',
  by { rw [sub_eq_zero.1 ((norm_eq_zero (x' - x)).1 hx')], simp },
begin
  unfold has_fderiv_at_filter,
  rw [←is_o_norm_left, ←is_o_norm_right, is_o_iff_tendsto h],
  exact tendsto_congr (Ξ» _, div_eq_inv_mul),
end

theorem has_fderiv_within_at_iff_tendsto : has_fderiv_within_at f f' s x ↔
  tendsto (Ξ» x', βˆ₯x' - xβˆ₯⁻¹ * βˆ₯f x' - f x - f' (x' - x)βˆ₯) (nhds_within x s) (𝓝 0) :=
has_fderiv_at_filter_iff_tendsto

theorem has_fderiv_at_iff_tendsto : has_fderiv_at f f' x ↔
  tendsto (Ξ» x', βˆ₯x' - xβˆ₯⁻¹ * βˆ₯f x' - f x - f' (x' - x)βˆ₯) (𝓝 x) (𝓝 0) :=
has_fderiv_at_filter_iff_tendsto

theorem has_fderiv_at_iff_is_o_nhds_zero : has_fderiv_at f f' x ↔
  is_o (Ξ»h, f (x + h) - f x - f' h) (Ξ»h, h) (𝓝 0) :=
begin
  split,
  { assume H,
    have : tendsto (Ξ» (z : E), z + x) (𝓝 0) (𝓝 (0 + x)),
      from tendsto_id.add tendsto_const_nhds,
    rw [zero_add] at this,
    refine (H.comp_tendsto this).congr _ _;
      intro z; simp only [function.comp, add_sub_cancel', add_comm z] },
  { assume H,
    have : tendsto (Ξ» (z : E), z - x) (𝓝 x) (𝓝 (x - x)),
      from tendsto_id.sub tendsto_const_nhds,
    rw [sub_self] at this,
    refine (H.comp_tendsto this).congr _ _;
      intro z; simp only [function.comp, add_sub_cancel'_right] }
end

theorem has_fderiv_at_filter.mono (h : has_fderiv_at_filter f f' x Lβ‚‚) (hst : L₁ ≀ Lβ‚‚) :
  has_fderiv_at_filter f f' x L₁ :=
h.mono hst

theorem has_fderiv_within_at.mono (h : has_fderiv_within_at f f' t x) (hst : s βŠ† t) :
  has_fderiv_within_at f f' s x :=
h.mono (nhds_within_mono _ hst)

theorem has_fderiv_at.has_fderiv_at_filter (h : has_fderiv_at f f' x) (hL : L ≀ 𝓝 x) :
  has_fderiv_at_filter f f' x L :=
h.mono hL

theorem has_fderiv_at.has_fderiv_within_at
  (h : has_fderiv_at f f' x) : has_fderiv_within_at f f' s x :=
h.has_fderiv_at_filter lattice.inf_le_left

lemma has_fderiv_within_at.differentiable_within_at (h : has_fderiv_within_at f f' s x) :
  differentiable_within_at π•œ f s x :=
⟨f', h⟩

lemma has_fderiv_at.differentiable_at (h : has_fderiv_at f f' x) : differentiable_at π•œ f x :=
⟨f', h⟩

@[simp] lemma has_fderiv_within_at_univ :
  has_fderiv_within_at f f' univ x ↔ has_fderiv_at f f' x :=
by { simp only [has_fderiv_within_at, nhds_within_univ], refl }

/-- Directional derivative agrees with `has_fderiv`. -/
lemma has_fderiv_at.lim (hf : has_fderiv_at f f' x) (v : E) {Ξ± : Type*} {c : Ξ± β†’ π•œ}
  {l : filter Ξ±} (hc : tendsto (Ξ» n, βˆ₯c nβˆ₯) l at_top) :
  tendsto (Ξ» n, (c n) β€’ (f (x + (c n)⁻¹ β€’ v) - f x)) l (𝓝 (f' v)) :=
begin
  refine (has_fderiv_within_at_univ.2 hf).lim _ (univ_mem_sets' (Ξ» _, trivial)) hc _,
  assume U hU,
  apply mem_sets_of_superset (ne_mem_of_tendsto_norm_at_top hc (0:π•œ)) _,
  assume y hy,
  rw [mem_preimage],
  convert mem_of_nhds hU,
  rw [← mul_smul, mul_inv_cancel hy, one_smul]
end

theorem has_fderiv_at_unique
  (hβ‚€ : has_fderiv_at f fβ‚€' x) (h₁ : has_fderiv_at f f₁' x) : fβ‚€' = f₁' :=
begin
  rw ← has_fderiv_within_at_univ at hβ‚€ h₁,
  exact unique_diff_within_at_univ.eq hβ‚€ h₁
end

lemma has_fderiv_within_at_inter' (h : t ∈ nhds_within x s) :
  has_fderiv_within_at f f' (s ∩ t) x ↔ has_fderiv_within_at f f' s x :=
by simp [has_fderiv_within_at, nhds_within_restrict'' s h]

lemma has_fderiv_within_at_inter (h : t ∈ 𝓝 x) :
  has_fderiv_within_at f f' (s ∩ t) x ↔ has_fderiv_within_at f f' s x :=
by simp [has_fderiv_within_at, nhds_within_restrict' s h]

lemma has_fderiv_within_at.union (hs : has_fderiv_within_at f f' s x) (ht : has_fderiv_within_at f f' t x) :
  has_fderiv_within_at f f' (s βˆͺ t) x :=
begin
  simp only [has_fderiv_within_at, nhds_within_union],
  exact hs.join ht,
end

lemma has_fderiv_within_at.nhds_within (h : has_fderiv_within_at f f' s x)
  (ht : s ∈ nhds_within x t) : has_fderiv_within_at f f' t x :=
(has_fderiv_within_at_inter' ht).1 (h.mono (inter_subset_right _ _))

lemma has_fderiv_within_at.has_fderiv_at (h : has_fderiv_within_at f f' s x) (hs : s ∈ 𝓝 x) :
  has_fderiv_at f f' x :=
by rwa [← univ_inter s, has_fderiv_within_at_inter hs, has_fderiv_within_at_univ] at h

lemma differentiable_within_at.has_fderiv_within_at (h : differentiable_within_at π•œ f s x) :
  has_fderiv_within_at f (fderiv_within π•œ f s x) s x :=
begin
  dunfold fderiv_within,
  dunfold differentiable_within_at at h,
  rw dif_pos h,
  exact classical.some_spec h
end

lemma differentiable_at.has_fderiv_at (h : differentiable_at π•œ f x) :
  has_fderiv_at f (fderiv π•œ f x) x :=
begin
  dunfold fderiv,
  dunfold differentiable_at at h,
  rw dif_pos h,
  exact classical.some_spec h
end

lemma has_fderiv_at.fderiv (h : has_fderiv_at f f' x) : fderiv π•œ f x = f' :=
by { ext, rw has_fderiv_at_unique h h.differentiable_at.has_fderiv_at }

lemma has_fderiv_within_at.fderiv_within
  (h : has_fderiv_within_at f f' s x) (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ f s x = f' :=
by { ext, rw hxs.eq h h.differentiable_within_at.has_fderiv_within_at }

/-- If `x` is not in the closure of `s`, then `f` has any derivative at `x` within `s`,
as this statement is empty. -/
lemma has_fderiv_within_at_of_not_mem_closure (h : x βˆ‰ closure s) :
  has_fderiv_within_at f f' s x :=
begin
  simp [mem_closure_iff_nhds_within_ne_bot] at h,
  simp [has_fderiv_within_at, has_fderiv_at_filter, h, is_o, is_O_with],
end

lemma differentiable_within_at.mono (h : differentiable_within_at π•œ f t x) (st : s βŠ† t) :
  differentiable_within_at π•œ f s x :=
begin
  rcases h with ⟨f', hf'⟩,
  exact ⟨f', hf'.mono st⟩
end

lemma differentiable_within_at_univ :
  differentiable_within_at π•œ f univ x ↔ differentiable_at π•œ f x :=
by simp only [differentiable_within_at, has_fderiv_within_at_univ, differentiable_at]

lemma differentiable_within_at_inter (ht : t ∈ 𝓝 x) :
  differentiable_within_at π•œ f (s ∩ t) x ↔ differentiable_within_at π•œ f s x :=
by simp only [differentiable_within_at, has_fderiv_within_at, has_fderiv_at_filter,
    nhds_within_restrict' s ht]

lemma differentiable_within_at_inter' (ht : t ∈ nhds_within x s) :
  differentiable_within_at π•œ f (s ∩ t) x ↔ differentiable_within_at π•œ f s x :=
by simp only [differentiable_within_at, has_fderiv_within_at, has_fderiv_at_filter,
    nhds_within_restrict'' s ht]

lemma differentiable_at.differentiable_within_at
  (h : differentiable_at π•œ f x) : differentiable_within_at π•œ f s x :=
(differentiable_within_at_univ.2 h).mono (subset_univ _)

lemma differentiable_within_at.differentiable_at
  (h : differentiable_within_at π•œ f s x) (hs : s ∈ 𝓝 x) : differentiable_at π•œ f x :=
h.imp (Ξ» f' hf', hf'.has_fderiv_at hs)

lemma differentiable_at.fderiv_within
  (h : differentiable_at π•œ f x) (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ f s x = fderiv π•œ f x :=
begin
  apply has_fderiv_within_at.fderiv_within _ hxs,
  exact h.has_fderiv_at.has_fderiv_within_at
end

lemma differentiable_on.mono (h : differentiable_on π•œ f t) (st : s βŠ† t) :
  differentiable_on π•œ f s :=
Ξ»x hx, (h x (st hx)).mono st

lemma differentiable_on_univ :
  differentiable_on π•œ f univ ↔ differentiable π•œ f :=
by { simp [differentiable_on, differentiable_within_at_univ], refl }

lemma differentiable.differentiable_on (h : differentiable π•œ f) : differentiable_on π•œ f s :=
(differentiable_on_univ.2 h).mono (subset_univ _)

lemma differentiable_on_of_locally_differentiable_on
  (h : βˆ€x∈s, βˆƒu, is_open u ∧ x ∈ u ∧ differentiable_on π•œ f (s ∩ u)) : differentiable_on π•œ f s :=
begin
  assume x xs,
  rcases h x xs with ⟨t, t_open, xt, ht⟩,
  exact (differentiable_within_at_inter (mem_nhds_sets t_open xt)).1 (ht x ⟨xs, xt⟩)
end

lemma fderiv_within_subset (st : s βŠ† t) (ht : unique_diff_within_at π•œ s x)
  (h : differentiable_within_at π•œ f t x) :
  fderiv_within π•œ f s x = fderiv_within π•œ f t x :=
((differentiable_within_at.has_fderiv_within_at h).mono st).fderiv_within ht

@[simp] lemma fderiv_within_univ : fderiv_within π•œ f univ = fderiv π•œ f :=
begin
  ext x : 1,
  by_cases h : differentiable_at π•œ f x,
  { apply has_fderiv_within_at.fderiv_within _ (is_open_univ.unique_diff_within_at (mem_univ _)),
    rw has_fderiv_within_at_univ,
    apply h.has_fderiv_at },
  { have : Β¬ differentiable_within_at π•œ f univ x,
      by contrapose! h; rwa ← differentiable_within_at_univ,
    rw [fderiv_zero_of_not_differentiable_at h,
        fderiv_within_zero_of_not_differentiable_within_at this] }
end

lemma fderiv_within_inter (ht : t ∈ 𝓝 x) (hs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ f (s ∩ t) x = fderiv_within π•œ f s x :=
begin
  by_cases h : differentiable_within_at π•œ f (s ∩ t) x,
  { apply fderiv_within_subset (inter_subset_left _ _) _ ((differentiable_within_at_inter ht).1 h),
    apply hs.inter ht },
  { have : Β¬ differentiable_within_at π•œ f s x,
      by contrapose! h; rw differentiable_within_at_inter; assumption,
    rw [fderiv_within_zero_of_not_differentiable_within_at h,
        fderiv_within_zero_of_not_differentiable_within_at this] }
end

end fderiv_properties

section congr
/-! ### congr properties of the derivative -/

theorem has_fderiv_at_filter_congr_of_mem_sets
  (hx : fβ‚€ x = f₁ x) (hβ‚€ : βˆ€αΆ  x in L, fβ‚€ x = f₁ x) (h₁ : βˆ€ x, fβ‚€' x = f₁' x) :
  has_fderiv_at_filter fβ‚€ fβ‚€' x L ↔ has_fderiv_at_filter f₁ f₁' x L :=
by { rw (ext h₁), exact is_o_congr
  (by filter_upwards [hβ‚€] Ξ» x (h : _ = _), by simp [h, hx])
  (univ_mem_sets' $ Ξ» _, rfl) }

lemma has_fderiv_at_filter.congr_of_mem_sets (h : has_fderiv_at_filter f f' x L)
  (hL : βˆ€αΆ  x in L, f₁ x = f x) (hx : f₁ x = f x) : has_fderiv_at_filter f₁ f' x L :=
begin
  apply (has_fderiv_at_filter_congr_of_mem_sets hx hL _).2 h,
  exact Ξ»x, rfl
end

lemma has_fderiv_within_at.congr_mono (h : has_fderiv_within_at f f' s x) (ht : βˆ€x ∈ t, f₁ x = f x)
  (hx : f₁ x = f x) (h₁ : t βŠ† s) : has_fderiv_within_at f₁ f' t x :=
has_fderiv_at_filter.congr_of_mem_sets (h.mono h₁) (filter.mem_inf_sets_of_right ht) hx

lemma has_fderiv_within_at.congr (h : has_fderiv_within_at f f' s x) (hs : βˆ€x ∈ s, f₁ x = f x)
  (hx : f₁ x = f x) : has_fderiv_within_at f₁ f' s x :=
h.congr_mono hs hx (subset.refl _)

lemma has_fderiv_within_at.congr_of_mem_nhds_within (h : has_fderiv_within_at f f' s x)
  (h₁ : βˆ€αΆ  y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) : has_fderiv_within_at f₁ f' s x :=
has_fderiv_at_filter.congr_of_mem_sets h h₁ hx

lemma has_fderiv_at.congr_of_mem_nhds (h : has_fderiv_at f f' x)
  (h₁ : βˆ€αΆ  y in 𝓝 x, f₁ y = f y) : has_fderiv_at f₁ f' x :=
has_fderiv_at_filter.congr_of_mem_sets h h₁ (mem_of_nhds h₁ : _)

lemma differentiable_within_at.congr_mono (h : differentiable_within_at π•œ f s x)
  (ht : βˆ€x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (h₁ : t βŠ† s) : differentiable_within_at π•œ f₁ t x :=
(has_fderiv_within_at.congr_mono h.has_fderiv_within_at ht hx h₁).differentiable_within_at

lemma differentiable_within_at.congr (h : differentiable_within_at π•œ f s x)
  (ht : βˆ€x ∈ s, f₁ x = f x) (hx : f₁ x = f x) : differentiable_within_at π•œ f₁ s x :=
differentiable_within_at.congr_mono h ht hx (subset.refl _)

lemma differentiable_within_at.congr_of_mem_nhds_within
  (h : differentiable_within_at π•œ f s x) (h₁ : βˆ€αΆ  y in nhds_within x s, f₁ y = f y)
  (hx : f₁ x = f x) : differentiable_within_at π•œ f₁ s x :=
(h.has_fderiv_within_at.congr_of_mem_nhds_within h₁ hx).differentiable_within_at

lemma differentiable_on.congr_mono (h : differentiable_on π•œ f s) (h' : βˆ€x ∈ t, f₁ x = f x)
  (h₁ : t βŠ† s) : differentiable_on π•œ f₁ t :=
Ξ» x hx, (h x (h₁ hx)).congr_mono h' (h' x hx) h₁

lemma differentiable_on.congr (h : differentiable_on π•œ f s) (h' : βˆ€x ∈ s, f₁ x = f x) :
  differentiable_on π•œ f₁ s :=
Ξ» x hx, (h x hx).congr h' (h' x hx)

lemma differentiable_at.congr_of_mem_nhds (h : differentiable_at π•œ f x)
  (hL : βˆ€αΆ  y in 𝓝 x, f₁ y = f y) : differentiable_at π•œ f₁ x :=
has_fderiv_at.differentiable_at (has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_at hL (mem_of_nhds hL : _))

lemma differentiable_within_at.fderiv_within_congr_mono (h : differentiable_within_at π•œ f s x)
  (hs : βˆ€x ∈ t, f₁ x = f x) (hx : f₁ x = f x) (hxt : unique_diff_within_at π•œ t x) (h₁ : t βŠ† s) :
  fderiv_within π•œ f₁ t x = fderiv_within π•œ f s x :=
(has_fderiv_within_at.congr_mono h.has_fderiv_within_at hs hx h₁).fderiv_within hxt

lemma fderiv_within_congr_of_mem_nhds_within (hs : unique_diff_within_at π•œ s x)
  (hL : βˆ€αΆ  y in nhds_within x s, f₁ y = f y) (hx : f₁ x = f x) :
  fderiv_within π•œ f₁ s x = fderiv_within π•œ f s x :=
begin
  by_cases h : differentiable_within_at π•œ f s x ∨ differentiable_within_at π•œ f₁ s x,
  { cases h,
    { apply has_fderiv_within_at.fderiv_within _ hs,
      exact has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_within_at hL hx },
    { symmetry,
      apply has_fderiv_within_at.fderiv_within _ hs,
      apply has_fderiv_at_filter.congr_of_mem_sets h.has_fderiv_within_at _ hx.symm,
      convert hL,
      ext y,
      exact eq_comm } },
  { push_neg at h,
    have A : fderiv_within π•œ f s x = 0,
      by { unfold differentiable_within_at at h, simp [fderiv_within, h] },
    have A₁ : fderiv_within π•œ f₁ s x = 0,
      by { unfold differentiable_within_at at h, simp [fderiv_within, h] },
    rw [A, A₁] }
end

lemma fderiv_within_congr (hs : unique_diff_within_at π•œ s x)
  (hL : βˆ€y∈s, f₁ y = f y) (hx : f₁ x = f x) :
  fderiv_within π•œ f₁ s x = fderiv_within π•œ f s x :=
begin
  apply fderiv_within_congr_of_mem_nhds_within hs _ hx,
  apply mem_sets_of_superset self_mem_nhds_within,
  exact hL
end

lemma fderiv_congr_of_mem_nhds (hL : βˆ€αΆ  y in 𝓝 x, f₁ y = f y) :
  fderiv π•œ f₁ x = fderiv π•œ f x :=
begin
  have A : f₁ x = f x := (mem_of_nhds hL : _),
  rw [← fderiv_within_univ, ← fderiv_within_univ],
  rw ← nhds_within_univ at hL,
  exact fderiv_within_congr_of_mem_nhds_within unique_diff_within_at_univ hL A
end

end congr

section id
/-! ### Derivative of the identity -/

theorem has_fderiv_at_filter_id (x : E) (L : filter E) :
  has_fderiv_at_filter id (id : E β†’L[π•œ] E) x L :=
(is_o_zero _ _).congr_left $ by simp

theorem has_fderiv_within_at_id (x : E) (s : set E) :
  has_fderiv_within_at id (id : E β†’L[π•œ] E) s x :=
has_fderiv_at_filter_id _ _

theorem has_fderiv_at_id (x : E) : has_fderiv_at id (id : E β†’L[π•œ] E) x :=
has_fderiv_at_filter_id _ _

lemma differentiable_at_id : differentiable_at π•œ id x :=
(has_fderiv_at_id x).differentiable_at

lemma differentiable_within_at_id : differentiable_within_at π•œ id s x :=
differentiable_at_id.differentiable_within_at

lemma differentiable_id : differentiable π•œ (id : E β†’ E) :=
Ξ»x, differentiable_at_id

lemma differentiable_on_id : differentiable_on π•œ id s :=
differentiable_id.differentiable_on

lemma fderiv_id : fderiv π•œ id x = id :=
has_fderiv_at.fderiv (has_fderiv_at_id x)

lemma fderiv_within_id (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ id s x = id :=
begin
  rw differentiable_at.fderiv_within (differentiable_at_id) hxs,
  exact fderiv_id
end

end id

section const
/-! ### derivative of a constant function -/

theorem has_fderiv_at_filter_const (c : F) (x : E) (L : filter E) :
  has_fderiv_at_filter (Ξ» x, c) (0 : E β†’L[π•œ] F) x L :=
(is_o_zero _ _).congr_left $ Ξ» _, by simp only [zero_apply, sub_self]

theorem has_fderiv_within_at_const (c : F) (x : E) (s : set E) :
  has_fderiv_within_at (Ξ» x, c) (0 : E β†’L[π•œ] F) s x :=
has_fderiv_at_filter_const _ _ _

theorem has_fderiv_at_const (c : F) (x : E) :
  has_fderiv_at (Ξ» x, c) (0 : E β†’L[π•œ] F) x :=
has_fderiv_at_filter_const _ _ _

lemma differentiable_at_const (c : F) : differentiable_at π•œ (Ξ»x, c) x :=
⟨0, has_fderiv_at_const c x⟩

lemma differentiable_within_at_const (c : F) : differentiable_within_at π•œ (Ξ»x, c) s x :=
differentiable_at.differentiable_within_at (differentiable_at_const _)

lemma fderiv_const (c : F) : fderiv π•œ (Ξ»y, c) x = 0 :=
has_fderiv_at.fderiv (has_fderiv_at_const c x)

lemma fderiv_within_const (c : F) (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ (Ξ»y, c) s x = 0 :=
begin
  rw differentiable_at.fderiv_within (differentiable_at_const _) hxs,
  exact fderiv_const _
end

lemma differentiable_const (c : F) : differentiable π•œ (Ξ»x : E, c) :=
Ξ»x, differentiable_at_const _

lemma differentiable_on_const (c : F) : differentiable_on π•œ (Ξ»x, c) s :=
(differentiable_const _).differentiable_on

end const

section continuous_linear_map
/-! ### Continuous linear maps

There are currently two variants of these in mathlib, the bundled version
(named `continuous_linear_map`, and denoted `E β†’L[π•œ] F`), and the unbundled version (with a
predicate `is_bounded_linear_map`). We give statements for both versions. -/

lemma is_bounded_linear_map.has_fderiv_at_filter (h : is_bounded_linear_map π•œ f) :
  has_fderiv_at_filter f h.to_continuous_linear_map x L :=
begin
  have : (Ξ» (x' : E), f x' - f x - h.to_continuous_linear_map (x' - x)) = Ξ»x', 0,
  { ext,
    have : βˆ€a, h.to_continuous_linear_map a = f a := Ξ»a, rfl,
    simp,
    simp [this] },
  rw [has_fderiv_at_filter, this],
  exact asymptotics.is_o_zero _ _
end

lemma is_bounded_linear_map.has_fderiv_within_at (h : is_bounded_linear_map π•œ f) :
  has_fderiv_within_at f h.to_continuous_linear_map s x :=
h.has_fderiv_at_filter

lemma is_bounded_linear_map.has_fderiv_at (h : is_bounded_linear_map π•œ f) :
  has_fderiv_at f h.to_continuous_linear_map x  :=
h.has_fderiv_at_filter

lemma is_bounded_linear_map.differentiable_at (h : is_bounded_linear_map π•œ f) :
  differentiable_at π•œ f x :=
h.has_fderiv_at.differentiable_at

lemma is_bounded_linear_map.differentiable_within_at (h : is_bounded_linear_map π•œ f) :
  differentiable_within_at π•œ f s x :=
h.differentiable_at.differentiable_within_at

lemma is_bounded_linear_map.fderiv (h : is_bounded_linear_map π•œ f) :
  fderiv π•œ f x = h.to_continuous_linear_map :=
has_fderiv_at.fderiv (h.has_fderiv_at)

lemma is_bounded_linear_map.fderiv_within (h : is_bounded_linear_map π•œ f)
  (hxs : unique_diff_within_at π•œ s x) : fderiv_within π•œ f s x = h.to_continuous_linear_map :=
begin
  rw differentiable_at.fderiv_within h.differentiable_at hxs,
  exact h.fderiv
end

lemma is_bounded_linear_map.differentiable (h : is_bounded_linear_map π•œ f) :
  differentiable π•œ f :=
Ξ»x, h.differentiable_at

lemma is_bounded_linear_map.differentiable_on (h : is_bounded_linear_map π•œ f) :
  differentiable_on π•œ f s :=
h.differentiable.differentiable_on

lemma continuous_linear_map.has_fderiv_at_filter :
  has_fderiv_at_filter e e x L :=
begin
  have : (Ξ» (x' : E), e x' - e x - e (x' - x)) = Ξ»x', 0, by { ext, simp },
  rw [has_fderiv_at_filter, this],
  exact asymptotics.is_o_zero _ _
end

protected lemma continuous_linear_map.has_fderiv_within_at : has_fderiv_within_at e e s x :=
e.has_fderiv_at_filter

protected lemma continuous_linear_map.has_fderiv_at : has_fderiv_at e e x  :=
e.has_fderiv_at_filter

protected lemma continuous_linear_map.differentiable_at : differentiable_at π•œ e x :=
e.has_fderiv_at.differentiable_at

protected lemma continuous_linear_map.differentiable_within_at : differentiable_within_at π•œ e s x :=
e.differentiable_at.differentiable_within_at

protected lemma continuous_linear_map.fderiv : fderiv π•œ e x = e :=
e.has_fderiv_at.fderiv

protected lemma continuous_linear_map.fderiv_within (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ e s x = e :=
begin
  rw differentiable_at.fderiv_within e.differentiable_at hxs,
  exact e.fderiv
end

protected lemma continuous_linear_map.differentiable : differentiable π•œ e :=
Ξ»x, e.differentiable_at

protected lemma continuous_linear_map.differentiable_on : differentiable_on π•œ e s :=
e.differentiable.differentiable_on

end continuous_linear_map

section const_smul
/-! ### Derivative of a function multiplied by a constant -/
theorem has_fderiv_at_filter.const_smul (h : has_fderiv_at_filter f f' x L) (c : π•œ) :
  has_fderiv_at_filter (Ξ» x, c β€’ f x) (c β€’ f') x L :=
(is_o_const_smul_left h c).congr_left $ Ξ» x, by simp [smul_neg, smul_add]

theorem has_fderiv_within_at.const_smul (h : has_fderiv_within_at f f' s x) (c : π•œ) :
  has_fderiv_within_at (Ξ» x, c β€’ f x) (c β€’ f') s x :=
h.const_smul c

theorem has_fderiv_at.const_smul (h : has_fderiv_at f f' x) (c : π•œ) :
  has_fderiv_at (Ξ» x, c β€’ f x) (c β€’ f') x :=
h.const_smul c

lemma differentiable_within_at.const_smul (h : differentiable_within_at π•œ f s x) (c : π•œ) :
  differentiable_within_at π•œ (Ξ»y, c β€’ f y) s x :=
(h.has_fderiv_within_at.const_smul c).differentiable_within_at

lemma differentiable_at.const_smul (h : differentiable_at π•œ f x) (c : π•œ) :
  differentiable_at π•œ (Ξ»y, c β€’ f y) x :=
(h.has_fderiv_at.const_smul c).differentiable_at

lemma differentiable_on.const_smul (h : differentiable_on π•œ f s) (c : π•œ) :
  differentiable_on π•œ (Ξ»y, c β€’ f y) s :=
Ξ»x hx, (h x hx).const_smul c

lemma differentiable.const_smul (h : differentiable π•œ f) (c : π•œ) :
  differentiable π•œ (Ξ»y, c β€’ f y) :=
Ξ»x, (h x).const_smul c

lemma fderiv_within_const_smul (hxs : unique_diff_within_at π•œ s x)
  (h : differentiable_within_at π•œ f s x) (c : π•œ) :
  fderiv_within π•œ (Ξ»y, c β€’ f y) s x = c β€’ fderiv_within π•œ f s x :=
(h.has_fderiv_within_at.const_smul c).fderiv_within hxs

lemma fderiv_const_smul (h : differentiable_at π•œ f x) (c : π•œ) :
  fderiv π•œ (Ξ»y, c β€’ f y) x = c β€’ fderiv π•œ f x :=
(h.has_fderiv_at.const_smul c).fderiv

end const_smul

section add
/-! ### Derivative of the sum of two functions -/

theorem has_fderiv_at_filter.add
  (hf : has_fderiv_at_filter f f' x L) (hg : has_fderiv_at_filter g g' x L) :
  has_fderiv_at_filter (Ξ» y, f y + g y) (f' + g') x L :=
(hf.add hg).congr_left $ Ξ» _, by simp

theorem has_fderiv_within_at.add
  (hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) :
  has_fderiv_within_at (Ξ» y, f y + g y) (f' + g') s x :=
hf.add hg

theorem has_fderiv_at.add
  (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) :
  has_fderiv_at (Ξ» x, f x + g x) (f' + g') x :=
hf.add hg

lemma differentiable_within_at.add
  (hf : differentiable_within_at π•œ f s x) (hg : differentiable_within_at π•œ g s x) :
  differentiable_within_at π•œ (Ξ» y, f y + g y) s x :=
(hf.has_fderiv_within_at.add hg.has_fderiv_within_at).differentiable_within_at

lemma differentiable_at.add
  (hf : differentiable_at π•œ f x) (hg : differentiable_at π•œ g x) :
  differentiable_at π•œ (Ξ» y, f y + g y) x :=
(hf.has_fderiv_at.add hg.has_fderiv_at).differentiable_at

lemma differentiable_on.add
  (hf : differentiable_on π•œ f s) (hg : differentiable_on π•œ g s) :
  differentiable_on π•œ (Ξ»y, f y + g y) s :=
Ξ»x hx, (hf x hx).add (hg x hx)

lemma differentiable.add
  (hf : differentiable π•œ f) (hg : differentiable π•œ g) :
  differentiable π•œ (Ξ»y, f y + g y) :=
Ξ»x, (hf x).add (hg x)

lemma fderiv_within_add (hxs : unique_diff_within_at π•œ s x)
  (hf : differentiable_within_at π•œ f s x) (hg : differentiable_within_at π•œ g s x) :
  fderiv_within π•œ (Ξ»y, f y + g y) s x = fderiv_within π•œ f s x + fderiv_within π•œ g s x :=
(hf.has_fderiv_within_at.add hg.has_fderiv_within_at).fderiv_within hxs

lemma fderiv_add
  (hf : differentiable_at π•œ f x) (hg : differentiable_at π•œ g x) :
  fderiv π•œ (Ξ»y, f y + g y) x = fderiv π•œ f x + fderiv π•œ g x :=
(hf.has_fderiv_at.add hg.has_fderiv_at).fderiv

theorem has_fderiv_at_filter.add_const
  (hf : has_fderiv_at_filter f f' x L) (c : F) :
  has_fderiv_at_filter (Ξ» y, f y + c) f' x L :=
add_zero f' β–Έ hf.add (has_fderiv_at_filter_const _ _ _)

theorem has_fderiv_within_at.add_const
  (hf : has_fderiv_within_at f f' s x) (c : F) :
  has_fderiv_within_at (Ξ» y, f y + c) f' s x :=
hf.add_const c

theorem has_fderiv_at.add_const
  (hf : has_fderiv_at f f' x) (c : F):
  has_fderiv_at (Ξ» x, f x + c) f' x :=
hf.add_const c

lemma differentiable_within_at.add_const
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  differentiable_within_at π•œ (Ξ» y, f y + c) s x :=
(hf.has_fderiv_within_at.add_const c).differentiable_within_at

lemma differentiable_at.add_const
  (hf : differentiable_at π•œ f x) (c : F) :
  differentiable_at π•œ (Ξ» y, f y + c) x :=
(hf.has_fderiv_at.add_const c).differentiable_at

lemma differentiable_on.add_const
  (hf : differentiable_on π•œ f s) (c : F) :
  differentiable_on π•œ (Ξ»y, f y + c) s :=
Ξ»x hx, (hf x hx).add_const c

lemma differentiable.add_const
  (hf : differentiable π•œ f) (c : F) :
  differentiable π•œ (Ξ»y, f y + c) :=
Ξ»x, (hf x).add_const c

lemma fderiv_within_add_const (hxs : unique_diff_within_at π•œ s x)
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  fderiv_within π•œ (Ξ»y, f y + c) s x = fderiv_within π•œ f s x :=
(hf.has_fderiv_within_at.add_const c).fderiv_within hxs

lemma fderiv_add_const
  (hf : differentiable_at π•œ f x) (c : F) :
  fderiv π•œ (Ξ»y, f y + c) x = fderiv π•œ f x :=
(hf.has_fderiv_at.add_const c).fderiv

theorem has_fderiv_at_filter.const_add
  (hf : has_fderiv_at_filter f f' x L) (c : F) :
  has_fderiv_at_filter (Ξ» y, c + f y) f' x L :=
zero_add f' β–Έ (has_fderiv_at_filter_const _ _ _).add hf

theorem has_fderiv_within_at.const_add
  (hf : has_fderiv_within_at f f' s x) (c : F) :
  has_fderiv_within_at (Ξ» y, c + f y) f' s x :=
hf.const_add c

theorem has_fderiv_at.const_add
  (hf : has_fderiv_at f f' x) (c : F):
  has_fderiv_at (Ξ» x, c + f x) f' x :=
hf.const_add c

lemma differentiable_within_at.const_add
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  differentiable_within_at π•œ (Ξ» y, c + f y) s x :=
(hf.has_fderiv_within_at.const_add c).differentiable_within_at

lemma differentiable_at.const_add
  (hf : differentiable_at π•œ f x) (c : F) :
  differentiable_at π•œ (Ξ» y, c + f y) x :=
(hf.has_fderiv_at.const_add c).differentiable_at

lemma differentiable_on.const_add
  (hf : differentiable_on π•œ f s) (c : F) :
  differentiable_on π•œ (Ξ»y, c + f y) s :=
Ξ»x hx, (hf x hx).const_add c

lemma differentiable.const_add
  (hf : differentiable π•œ f) (c : F) :
  differentiable π•œ (Ξ»y, c + f y) :=
Ξ»x, (hf x).const_add c

lemma fderiv_within_const_add (hxs : unique_diff_within_at π•œ s x)
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  fderiv_within π•œ (Ξ»y, c + f y) s x = fderiv_within π•œ f s x :=
(hf.has_fderiv_within_at.const_add c).fderiv_within hxs

lemma fderiv_const_add
  (hf : differentiable_at π•œ f x) (c : F) :
  fderiv π•œ (Ξ»y, c + f y) x = fderiv π•œ f x :=
(hf.has_fderiv_at.const_add c).fderiv

end add

section neg
/-! ### Derivative of the negative of a function -/

theorem has_fderiv_at_filter.neg (h : has_fderiv_at_filter f f' x L) :
  has_fderiv_at_filter (Ξ» x, -f x) (-f') x L :=
(h.const_smul (-1:π•œ)).congr (by simp) (by simp)

theorem has_fderiv_within_at.neg (h : has_fderiv_within_at f f' s x) :
  has_fderiv_within_at (Ξ» x, -f x) (-f') s x :=
h.neg

theorem has_fderiv_at.neg (h : has_fderiv_at f f' x) :
  has_fderiv_at (Ξ» x, -f x) (-f') x :=
h.neg

lemma differentiable_within_at.neg (h : differentiable_within_at π•œ f s x) :
  differentiable_within_at π•œ (Ξ»y, -f y) s x :=
h.has_fderiv_within_at.neg.differentiable_within_at

lemma differentiable_at.neg (h : differentiable_at π•œ f x) :
  differentiable_at π•œ (Ξ»y, -f y) x :=
h.has_fderiv_at.neg.differentiable_at

lemma differentiable_on.neg (h : differentiable_on π•œ f s) :
  differentiable_on π•œ (Ξ»y, -f y) s :=
Ξ»x hx, (h x hx).neg

lemma differentiable.neg (h : differentiable π•œ f) :
  differentiable π•œ (Ξ»y, -f y) :=
Ξ»x, (h x).neg

lemma fderiv_within_neg (hxs : unique_diff_within_at π•œ s x)
  (h : differentiable_within_at π•œ f s x) :
  fderiv_within π•œ (Ξ»y, -f y) s x = - fderiv_within π•œ f s x :=
h.has_fderiv_within_at.neg.fderiv_within hxs

lemma fderiv_neg (h : differentiable_at π•œ f x) :
  fderiv π•œ (Ξ»y, -f y) x = - fderiv π•œ f x :=
h.has_fderiv_at.neg.fderiv

end neg

section sub
/-! ### Derivative of the difference of two functions -/

theorem has_fderiv_at_filter.sub
  (hf : has_fderiv_at_filter f f' x L) (hg : has_fderiv_at_filter g g' x L) :
  has_fderiv_at_filter (Ξ» x, f x - g x) (f' - g') x L :=
hf.add hg.neg

theorem has_fderiv_within_at.sub
  (hf : has_fderiv_within_at f f' s x) (hg : has_fderiv_within_at g g' s x) :
  has_fderiv_within_at (Ξ» x, f x - g x) (f' - g') s x :=
hf.sub hg

theorem has_fderiv_at.sub
  (hf : has_fderiv_at f f' x) (hg : has_fderiv_at g g' x) :
  has_fderiv_at (Ξ» x, f x - g x) (f' - g') x :=
hf.sub hg

lemma differentiable_within_at.sub
  (hf : differentiable_within_at π•œ f s x) (hg : differentiable_within_at π•œ g s x) :
  differentiable_within_at π•œ (Ξ» y, f y - g y) s x :=
(hf.has_fderiv_within_at.sub hg.has_fderiv_within_at).differentiable_within_at

lemma differentiable_at.sub
  (hf : differentiable_at π•œ f x) (hg : differentiable_at π•œ g x) :
  differentiable_at π•œ (Ξ» y, f y - g y) x :=
(hf.has_fderiv_at.sub hg.has_fderiv_at).differentiable_at

lemma differentiable_on.sub
  (hf : differentiable_on π•œ f s) (hg : differentiable_on π•œ g s) :
  differentiable_on π•œ (Ξ»y, f y - g y) s :=
Ξ»x hx, (hf x hx).sub (hg x hx)

lemma differentiable.sub
  (hf : differentiable π•œ f) (hg : differentiable π•œ g) :
  differentiable π•œ (Ξ»y, f y - g y) :=
Ξ»x, (hf x).sub (hg x)

lemma fderiv_within_sub (hxs : unique_diff_within_at π•œ s x)
  (hf : differentiable_within_at π•œ f s x) (hg : differentiable_within_at π•œ g s x) :
  fderiv_within π•œ (Ξ»y, f y - g y) s x = fderiv_within π•œ f s x - fderiv_within π•œ g s x :=
(hf.has_fderiv_within_at.sub hg.has_fderiv_within_at).fderiv_within hxs

lemma fderiv_sub
  (hf : differentiable_at π•œ f x) (hg : differentiable_at π•œ g x) :
  fderiv π•œ (Ξ»y, f y - g y) x = fderiv π•œ f x - fderiv π•œ g x :=
(hf.has_fderiv_at.sub hg.has_fderiv_at).fderiv

theorem has_fderiv_at_filter.is_O_sub (h : has_fderiv_at_filter f f' x L) :
is_O (Ξ» x', f x' - f x) (Ξ» x', x' - x) L :=
h.is_O.congr_of_sub.2 (f'.is_O_sub _ _)

theorem has_fderiv_at_filter.sub_const
  (hf : has_fderiv_at_filter f f' x L) (c : F) :
  has_fderiv_at_filter (Ξ» x, f x - c) f' x L :=
hf.add_const (-c)

theorem has_fderiv_within_at.sub_const
  (hf : has_fderiv_within_at f f' s x) (c : F) :
  has_fderiv_within_at (Ξ» x, f x - c) f' s x :=
hf.sub_const c

theorem has_fderiv_at.sub_const
  (hf : has_fderiv_at f f' x) (c : F) :
  has_fderiv_at (Ξ» x, f x - c) f' x :=
hf.sub_const c

lemma differentiable_within_at.sub_const
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  differentiable_within_at π•œ (Ξ» y, f y - c) s x :=
(hf.has_fderiv_within_at.sub_const c).differentiable_within_at

lemma differentiable_at.sub_const
  (hf : differentiable_at π•œ f x) (c : F) :
  differentiable_at π•œ (Ξ» y, f y - c) x :=
(hf.has_fderiv_at.sub_const c).differentiable_at

lemma differentiable_on.sub_const
  (hf : differentiable_on π•œ f s) (c : F) :
  differentiable_on π•œ (Ξ»y, f y - c) s :=
Ξ»x hx, (hf x hx).sub_const c

lemma differentiable.sub_const
  (hf : differentiable π•œ f) (c : F) :
  differentiable π•œ (Ξ»y, f y - c) :=
Ξ»x, (hf x).sub_const c

lemma fderiv_within_sub_const (hxs : unique_diff_within_at π•œ s x)
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  fderiv_within π•œ (Ξ»y, f y - c) s x = fderiv_within π•œ f s x :=
(hf.has_fderiv_within_at.sub_const c).fderiv_within hxs

lemma fderiv_sub_const
  (hf : differentiable_at π•œ f x) (c : F) :
  fderiv π•œ (Ξ»y, f y - c) x = fderiv π•œ f x :=
(hf.has_fderiv_at.sub_const c).fderiv

theorem has_fderiv_at_filter.const_sub
  (hf : has_fderiv_at_filter f f' x L) (c : F) :
  has_fderiv_at_filter (Ξ» x, c - f x) (-f') x L :=
hf.neg.const_add c

theorem has_fderiv_within_at.const_sub
  (hf : has_fderiv_within_at f f' s x) (c : F) :
  has_fderiv_within_at (Ξ» x, c - f x) (-f') s x :=
hf.const_sub c

theorem has_fderiv_at.const_sub
  (hf : has_fderiv_at f f' x) (c : F) :
  has_fderiv_at (Ξ» x, c - f x) (-f') x :=
hf.const_sub c

lemma differentiable_within_at.const_sub
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  differentiable_within_at π•œ (Ξ» y, c - f y) s x :=
(hf.has_fderiv_within_at.const_sub c).differentiable_within_at

lemma differentiable_at.const_sub
  (hf : differentiable_at π•œ f x) (c : F) :
  differentiable_at π•œ (Ξ» y, c - f y) x :=
(hf.has_fderiv_at.const_sub c).differentiable_at

lemma differentiable_on.const_sub
  (hf : differentiable_on π•œ f s) (c : F) :
  differentiable_on π•œ (Ξ»y, c - f y) s :=
Ξ»x hx, (hf x hx).const_sub c

lemma differentiable.const_sub
  (hf : differentiable π•œ f) (c : F) :
  differentiable π•œ (Ξ»y, c - f y) :=
Ξ»x, (hf x).const_sub c

lemma fderiv_within_const_sub (hxs : unique_diff_within_at π•œ s x)
  (hf : differentiable_within_at π•œ f s x) (c : F) :
  fderiv_within π•œ (Ξ»y, c - f y) s x = -fderiv_within π•œ f s x :=
(hf.has_fderiv_within_at.const_sub c).fderiv_within hxs

lemma fderiv_const_sub
  (hf : differentiable_at π•œ f x) (c : F) :
  fderiv π•œ (Ξ»y, c - f y) x = -fderiv π•œ f x :=
(hf.has_fderiv_at.const_sub c).fderiv

end sub

section continuous
/-! ### Deducing continuity from differentiability -/

theorem has_fderiv_at_filter.tendsto_nhds
  (hL : L ≀ 𝓝 x) (h : has_fderiv_at_filter f f' x L) :
  tendsto f L (𝓝 (f x)) :=
begin
  have : tendsto (Ξ» x', f x' - f x) L (𝓝 0),
  { refine h.is_O_sub.trans_tendsto (tendsto_le_left hL _),
    rw ← sub_self x, exact tendsto_id.sub tendsto_const_nhds },
  have := tendsto.add this tendsto_const_nhds,
  rw zero_add (f x) at this,
  exact this.congr (by simp)
end

theorem has_fderiv_within_at.continuous_within_at
  (h : has_fderiv_within_at f f' s x) : continuous_within_at f s x :=
has_fderiv_at_filter.tendsto_nhds lattice.inf_le_left h

theorem has_fderiv_at.continuous_at (h : has_fderiv_at f f' x) :
  continuous_at f x :=
has_fderiv_at_filter.tendsto_nhds (le_refl _) h

lemma differentiable_within_at.continuous_within_at (h : differentiable_within_at π•œ f s x) :
  continuous_within_at f s x :=
let ⟨f', hf'⟩ := h in hf'.continuous_within_at

lemma differentiable_at.continuous_at (h : differentiable_at π•œ f x) : continuous_at f x :=
let ⟨f', hf'⟩ := h in hf'.continuous_at

lemma differentiable_on.continuous_on (h : differentiable_on π•œ f s) : continuous_on f s :=
Ξ»x hx, (h x hx).continuous_within_at

lemma differentiable.continuous (h : differentiable π•œ f) : continuous f :=
continuous_iff_continuous_at.2 $ Ξ»x, (h x).continuous_at

end continuous

section bilinear_map
/-! ### Derivative of a bounded bilinear map -/

variables {b : E Γ— F β†’ G} {u : set (E Γ— F) }

open normed_field

lemma is_bounded_bilinear_map.has_fderiv_at (h : is_bounded_bilinear_map π•œ b) (p : E Γ— F) :
  has_fderiv_at b (h.deriv p) p :=
begin
  have : (Ξ» (x : E Γ— F), b x - b p - (h.deriv p) (x - p)) = (Ξ»x, b (x.1 - p.1, x.2 - p.2)),
  { ext x,
    delta is_bounded_bilinear_map.deriv,
    change b x - b p - (b (p.1, x.2-p.2) + b (x.1-p.1, p.2))
           = b (x.1 - p.1, x.2 - p.2),
    have : b x = b (x.1, x.2), by { cases x, refl },
    rw this,
    have : b p = b (p.1, p.2), by { cases p, refl },
    rw this,
    simp only [h.map_sub_left, h.map_sub_right],
    abel },
  rw [has_fderiv_at, has_fderiv_at_filter, this],
  rcases h.bound with ⟨C, Cpos, hC⟩,
  have A : asymptotics.is_O (Ξ»x : E Γ— F, b (x.1 - p.1, x.2 - p.2))
    (Ξ»x, βˆ₯x - pβˆ₯ * βˆ₯x - pβˆ₯) (𝓝 p) :=
  ⟨C, filter.univ_mem_sets' (λx, begin
    simp only [mem_set_of_eq, norm_mul, norm_norm],
    calc βˆ₯b (x.1 - p.1, x.2 - p.2)βˆ₯ ≀ C * βˆ₯x.1 - p.1βˆ₯ * βˆ₯x.2 - p.2βˆ₯ : hC _ _
    ... ≀ C * βˆ₯x-pβˆ₯ * βˆ₯x-pβˆ₯ : by apply_rules [mul_le_mul, le_max_left, le_max_right, norm_nonneg,
      le_of_lt Cpos, le_refl, mul_nonneg, norm_nonneg, norm_nonneg]
    ... = C * (βˆ₯x-pβˆ₯ * βˆ₯x-pβˆ₯) : mul_assoc _ _ _ end)⟩,
  have B : asymptotics.is_o (Ξ» (x : E Γ— F), βˆ₯x - pβˆ₯ * βˆ₯x - pβˆ₯)
    (Ξ»x, 1 * βˆ₯x - pβˆ₯) (𝓝 p),
  { refine asymptotics.is_o.mul_is_O (asymptotics.is_o.norm_left _) (asymptotics.is_O_refl _ _),
    apply (asymptotics.is_o_one_iff ℝ).2,
    rw [← sub_self p],
    exact tendsto_id.sub tendsto_const_nhds },
  simp only [one_mul, asymptotics.is_o_norm_right] at B,
  exact A.trans_is_o B
end

lemma is_bounded_bilinear_map.has_fderiv_within_at (h : is_bounded_bilinear_map π•œ b) (p : E Γ— F) :
  has_fderiv_within_at b (h.deriv p) u p :=
(h.has_fderiv_at p).has_fderiv_within_at

lemma is_bounded_bilinear_map.differentiable_at (h : is_bounded_bilinear_map π•œ b) (p : E Γ— F) :
  differentiable_at π•œ b p :=
(h.has_fderiv_at p).differentiable_at

lemma is_bounded_bilinear_map.differentiable_within_at (h : is_bounded_bilinear_map π•œ b) (p : E Γ— F) :
  differentiable_within_at π•œ b u p :=
(h.differentiable_at p).differentiable_within_at

lemma is_bounded_bilinear_map.fderiv (h : is_bounded_bilinear_map π•œ b) (p : E Γ— F) :
  fderiv π•œ b p = h.deriv p :=
has_fderiv_at.fderiv (h.has_fderiv_at p)

lemma is_bounded_bilinear_map.fderiv_within (h : is_bounded_bilinear_map π•œ b) (p : E Γ— F)
  (hxs : unique_diff_within_at π•œ u p) : fderiv_within π•œ b u p = h.deriv p :=
begin
  rw differentiable_at.fderiv_within (h.differentiable_at p) hxs,
  exact h.fderiv p
end

lemma is_bounded_bilinear_map.differentiable (h : is_bounded_bilinear_map π•œ b) :
  differentiable π•œ b :=
Ξ»x, h.differentiable_at x

lemma is_bounded_bilinear_map.differentiable_on (h : is_bounded_bilinear_map π•œ b) :
  differentiable_on π•œ b u :=
h.differentiable.differentiable_on

lemma is_bounded_bilinear_map.continuous (h : is_bounded_bilinear_map π•œ b) :
  continuous b :=
h.differentiable.continuous

lemma is_bounded_bilinear_map.continuous_left (h : is_bounded_bilinear_map π•œ b) {f : F} :
  continuous (Ξ»e, b (e, f)) :=
h.continuous.comp (continuous_id.prod_mk continuous_const)

lemma is_bounded_bilinear_map.continuous_right (h : is_bounded_bilinear_map π•œ b) {e : E} :
  continuous (Ξ»f, b (e, f)) :=
h.continuous.comp (continuous_const.prod_mk continuous_id)

end bilinear_map

section cartesian_product
/-! ### Derivative of the cartesian product of two functions -/

variables {fβ‚‚ : E β†’ G} {fβ‚‚' : E β†’L[π•œ] G}

lemma has_fderiv_at_filter.prod
  (hf₁ : has_fderiv_at_filter f₁ f₁' x L) (hfβ‚‚ : has_fderiv_at_filter fβ‚‚ fβ‚‚' x L) :
  has_fderiv_at_filter (Ξ»x, (f₁ x, fβ‚‚ x)) (continuous_linear_map.prod f₁' fβ‚‚') x L :=
begin
  have : (Ξ» (x' : E), (f₁ x', fβ‚‚ x') - (f₁ x, fβ‚‚ x) - (continuous_linear_map.prod f₁' fβ‚‚') (x' -x)) =
           (Ξ» (x' : E), (f₁ x' - f₁ x - f₁' (x' - x), fβ‚‚ x' - fβ‚‚ x - fβ‚‚' (x' - x))) := rfl,
  rw [has_fderiv_at_filter, this],
  rw [asymptotics.is_o_prod_left],
  exact ⟨hf₁, hfβ‚‚βŸ©
end

lemma has_fderiv_within_at.prod
  (hf₁ : has_fderiv_within_at f₁ f₁' s x) (hfβ‚‚ : has_fderiv_within_at fβ‚‚ fβ‚‚' s x) :
  has_fderiv_within_at (Ξ»x, (f₁ x, fβ‚‚ x)) (continuous_linear_map.prod f₁' fβ‚‚') s x :=
hf₁.prod hfβ‚‚

lemma has_fderiv_at.prod (hf₁ : has_fderiv_at f₁ f₁' x) (hfβ‚‚ : has_fderiv_at fβ‚‚ fβ‚‚' x) :
  has_fderiv_at (Ξ»x, (f₁ x, fβ‚‚ x)) (continuous_linear_map.prod f₁' fβ‚‚') x :=
hf₁.prod hfβ‚‚

lemma differentiable_within_at.prod
  (hf₁ : differentiable_within_at π•œ f₁ s x) (hfβ‚‚ : differentiable_within_at π•œ fβ‚‚ s x) :
  differentiable_within_at π•œ (Ξ»x:E, (f₁ x, fβ‚‚ x)) s x :=
(hf₁.has_fderiv_within_at.prod hfβ‚‚.has_fderiv_within_at).differentiable_within_at

lemma differentiable_at.prod (hf₁ : differentiable_at π•œ f₁ x) (hfβ‚‚ : differentiable_at π•œ fβ‚‚ x) :
  differentiable_at π•œ (Ξ»x:E, (f₁ x, fβ‚‚ x)) x :=
(hf₁.has_fderiv_at.prod hfβ‚‚.has_fderiv_at).differentiable_at

lemma differentiable_on.prod (hf₁ : differentiable_on π•œ f₁ s) (hfβ‚‚ : differentiable_on π•œ fβ‚‚ s) :
  differentiable_on π•œ (Ξ»x:E, (f₁ x, fβ‚‚ x)) s :=
Ξ»x hx, differentiable_within_at.prod (hf₁ x hx) (hfβ‚‚ x hx)

lemma differentiable.prod (hf₁ : differentiable π•œ f₁) (hfβ‚‚ : differentiable π•œ fβ‚‚) :
  differentiable π•œ (Ξ»x:E, (f₁ x, fβ‚‚ x)) :=
Ξ» x, differentiable_at.prod (hf₁ x) (hfβ‚‚ x)

lemma differentiable_at.fderiv_prod
  (hf₁ : differentiable_at π•œ f₁ x) (hfβ‚‚ : differentiable_at π•œ fβ‚‚ x) :
  fderiv π•œ (Ξ»x:E, (f₁ x, fβ‚‚ x)) x =
    continuous_linear_map.prod (fderiv π•œ f₁ x) (fderiv π•œ fβ‚‚ x) :=
has_fderiv_at.fderiv (has_fderiv_at.prod hf₁.has_fderiv_at hfβ‚‚.has_fderiv_at)

lemma differentiable_at.fderiv_within_prod
  (hf₁ : differentiable_within_at π•œ f₁ s x) (hfβ‚‚ : differentiable_within_at π•œ fβ‚‚ s x)
  (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ (Ξ»x:E, (f₁ x, fβ‚‚ x)) s x =
    continuous_linear_map.prod (fderiv_within π•œ f₁ s x) (fderiv_within π•œ fβ‚‚ s x) :=
begin
  apply has_fderiv_within_at.fderiv_within _ hxs,
  exact has_fderiv_within_at.prod hf₁.has_fderiv_within_at hfβ‚‚.has_fderiv_within_at
end

end cartesian_product

section composition
/-! ###
Derivative of the composition of two functions

For composition lemmas, we put x explicit to help the elaborator, as otherwise Lean tends to
get confused since there are too many possibilities for composition -/

variable (x)

theorem has_fderiv_at_filter.comp {g : F β†’ G} {g' : F β†’L[π•œ] G}
  (hg : has_fderiv_at_filter g g' (f x) (L.map f))
  (hf : has_fderiv_at_filter f f' x L) :
  has_fderiv_at_filter (g ∘ f) (g'.comp f') x L :=
let eq₁ := (g'.is_O_comp _ _).trans_is_o hf in
let eqβ‚‚ := (hg.comp_tendsto tendsto_map).trans_is_O hf.is_O_sub in
by { refine eqβ‚‚.triangle (eq₁.congr_left (Ξ» x', _)), simp }

/- A readable version of the previous theorem,
   a general form of the chain rule. -/

example {g : F β†’ G} {g' : F β†’L[π•œ] G}
  (hg : has_fderiv_at_filter g g' (f x) (L.map f))
  (hf : has_fderiv_at_filter f f' x L) :
  has_fderiv_at_filter (g ∘ f) (g'.comp f') x L :=
begin
  unfold has_fderiv_at_filter at hg,
  have : is_o (Ξ» x', g (f x') - g (f x) - g' (f x' - f x)) (Ξ» x', f x' - f x) L,
    from hg.comp_tendsto (le_refl _),
  have eq₁ : is_o (Ξ» x', g (f x') - g (f x) - g' (f x' - f x)) (Ξ» x', x' - x) L,
    from this.trans_is_O hf.is_O_sub,
  have eqβ‚‚ : is_o (Ξ» x', f x' - f x - f' (x' - x)) (Ξ» x', x' - x) L,
    from hf,
  have : is_O
    (Ξ» x', g' (f x' - f x - f' (x' - x))) (Ξ» x', f x' - f x - f' (x' - x)) L,
    from g'.is_O_comp _ _,
  have : is_o (Ξ» x', g' (f x' - f x - f' (x' - x))) (Ξ» x', x' - x) L,
    from this.trans_is_o eqβ‚‚,
  have eq₃ : is_o (Ξ» x', g' (f x' - f x) - (g' (f' (x' - x)))) (Ξ» x', x' - x) L,
    by { refine this.congr_left _, simp},
  exact eq₁.triangle eq₃
end

theorem has_fderiv_within_at.comp {g : F β†’ G} {g' : F β†’L[π•œ] G} {t : set F}
  (hg : has_fderiv_within_at g g' t (f x)) (hf : has_fderiv_within_at f f' s x) (hst : s βŠ† f ⁻¹' t) :
  has_fderiv_within_at (g ∘ f) (g'.comp f') s x :=
begin
  apply has_fderiv_at_filter.comp _ (has_fderiv_at_filter.mono hg _) hf,
  calc map f (nhds_within x s)
      ≀ nhds_within (f x) (f '' s) : hf.continuous_within_at.tendsto_nhds_within_image
  ... ≀ nhds_within (f x) t        : nhds_within_mono _ (image_subset_iff.mpr hst)
end

/-- The chain rule. -/
theorem has_fderiv_at.comp {g : F β†’ G} {g' : F β†’L[π•œ] G}
  (hg : has_fderiv_at g g' (f x)) (hf : has_fderiv_at f f' x) :
  has_fderiv_at (g ∘ f) (g'.comp f') x :=
(hg.mono hf.continuous_at).comp x hf

theorem has_fderiv_at.comp_has_fderiv_within_at {g : F β†’ G} {g' : F β†’L[π•œ] G}
  (hg : has_fderiv_at g g' (f x)) (hf : has_fderiv_within_at f f' s x) :
  has_fderiv_within_at (g ∘ f) (g'.comp f') s x :=
begin
  rw ← has_fderiv_within_at_univ at hg,
  exact has_fderiv_within_at.comp x hg hf subset_preimage_univ
end

lemma differentiable_within_at.comp {g : F β†’ G} {t : set F}
  (hg : differentiable_within_at π•œ g t (f x)) (hf : differentiable_within_at π•œ f s x)
  (h : s βŠ† f ⁻¹' t) : differentiable_within_at π•œ (g ∘ f) s x :=
begin
  rcases hf with ⟨f', hf'⟩,
  rcases hg with ⟨g', hg'⟩,
  exact ⟨continuous_linear_map.comp g' f', hg'.comp x hf' h⟩
end

lemma differentiable_at.comp {g : F β†’ G}
  (hg : differentiable_at π•œ g (f x)) (hf : differentiable_at π•œ f x) :
  differentiable_at π•œ (g ∘ f) x :=
(hg.has_fderiv_at.comp x hf.has_fderiv_at).differentiable_at

lemma fderiv_within.comp {g : F β†’ G} {t : set F}
  (hg : differentiable_within_at π•œ g t (f x)) (hf : differentiable_within_at π•œ f s x)
  (h : s βŠ† f ⁻¹' t) (hxs : unique_diff_within_at π•œ s x) :
  fderiv_within π•œ (g ∘ f) s x =
    continuous_linear_map.comp (fderiv_within π•œ g t (f x)) (fderiv_within π•œ f s x) :=
begin
  apply has_fderiv_within_at.fderiv_within _ hxs,
  exact has_fderiv_within_at.comp x (hg.has_fderiv_within_at) (hf.has_fderiv_within_at) h
end

lemma fderiv.comp {g : F β†’ G}
  (hg : differentiable_at π•œ g (f x)) (hf : differentiable_at π•œ f x) :
  fderiv π•œ (g ∘ f) x = continuous_linear_map.comp (fderiv π•œ g (f x)) (fderiv π•œ f x) :=
begin
  apply has_fderiv_at.fderiv,
  exact has_fderiv_at.comp x hg.has_fderiv_at hf.has_fderiv_at
end

lemma differentiable_on.comp {g : F β†’ G} {t : set F}
  (hg : differentiable_on π•œ g t) (hf : differentiable_on π•œ f s) (st : s βŠ† f ⁻¹' t) :
  differentiable_on π•œ (g ∘ f) s :=
Ξ»x hx, differentiable_within_at.comp x (hg (f x) (st hx)) (hf x hx) st

lemma differentiable.comp {g : F β†’ G} (hg : differentiable π•œ g) (hf : differentiable π•œ f) :
  differentiable π•œ (g ∘ f) :=
Ξ»x, differentiable_at.comp x (hg (f x)) (hf x)

end composition

section smul
/-! ### Derivative of the product of a scalar-valued function and a vector-valued function -/

variables {c : E β†’ π•œ} {c' : E β†’L[π•œ] π•œ}

theorem has_fderiv_within_at.smul
  (hc : has_fderiv_within_at c c' s x) (hf : has_fderiv_within_at f f' s x) :
  has_fderiv_within_at (Ξ» y, c y β€’ f y) (c x β€’ f' + c'.smul_right (f x)) s x :=
begin
  have : is_bounded_bilinear_map π•œ (Ξ» (p : π•œ Γ— F), p.1 β€’ p.2) := is_bounded_bilinear_map_smul,
  exact has_fderiv_at.comp_has_fderiv_within_at x (this.has_fderiv_at (c x, f x)) (hc.prod hf)
end

theorem has_fderiv_at.smul (hc : has_fderiv_at c c' x) (hf : has_fderiv_at f f' x) :
  has_fderiv_at (Ξ» y, c y β€’ f y) (c x β€’ f' + c'.smul_right (f x)) x :=
begin
  have : is_bounded_bilinear_map π•œ (Ξ» (p : π•œ Γ— F), p.1 β€’ p.2) := is_bounded_bilinear_map_smul,
  exact has_fderiv_at.comp x (this.has_fderiv_at (c x, f x)) (hc.prod hf)
end

lemma differentiable_within_at.smul
  (hc : differentiable_within_at π•œ c s x) (hf : differentiable_within_at π•œ f s x) :
  differentiable_within_at π•œ (Ξ» y, c y β€’ f y) s x :=
(hc.has_fderiv_within_at.smul hf.has_fderiv_within_at).differentiable_within_at

lemma differentiable_at.smul (hc : differentiable_at π•œ c x) (hf : differentiable_at π•œ f x) :
  differentiable_at π•œ (Ξ» y, c y β€’ f y) x :=
(hc.has_fderiv_at.smul hf.has_fderiv_at).differentiable_at

lemma differentiable_on.smul (hc : differentiable_on π•œ c s) (hf : differentiable_on π•œ f s) :
  differentiable_on π•œ (Ξ» y, c y β€’ f y) s :=
Ξ»x hx, (hc x hx).smul (hf x hx)

lemma differentiable.smul (hc : differentiable π•œ c) (hf : differentiable π•œ f) :
  differentiable π•œ (Ξ» y, c y β€’ f y) :=
Ξ»x, (hc x).smul (hf x)

lemma fderiv_within_smul (hxs : unique_diff_within_at π•œ s x)
  (hc : differentiable_within_at π•œ c s x) (hf : differentiable_within_at π•œ f s x) :
  fderiv_within π•œ (Ξ» y, c y β€’ f y) s x =
    c x β€’ fderiv_within π•œ f s x + (fderiv_within π•œ c s x).smul_right (f x) :=
(hc.has_fderiv_within_at.smul hf.has_fderiv_within_at).fderiv_within hxs

lemma fderiv_smul (hc : differentiable_at π•œ c x) (hf : differentiable_at π•œ f x) :
  fderiv π•œ (Ξ» y, c y β€’ f y) x =
    c x β€’ fderiv π•œ f x + (fderiv π•œ c x).smul_right (f x) :=
(hc.has_fderiv_at.smul hf.has_fderiv_at).fderiv

theorem has_fderiv_within_at.smul_const (hc : has_fderiv_within_at c c' s x) (f : F) :
  has_fderiv_within_at (Ξ» y, c y β€’ f) (c'.smul_right f) s x :=
begin
  convert hc.smul (has_fderiv_within_at_const f x s),
  -- Help Lean find an instance
  letI : distrib_mul_action π•œ (E β†’L[π•œ] F) :=
    continuous_linear_map.module.to_distrib_mul_action,
  rw [smul_zero, zero_add]
end

theorem has_fderiv_at.smul_const (hc : has_fderiv_at c c' x) (f : F) :
  has_fderiv_at (Ξ» y, c y β€’ f) (c'.smul_right f) x :=
begin
  rw [← has_fderiv_within_at_univ] at *,
  exact hc.smul_const f
end

lemma differentiable_within_at.smul_const
  (hc : differentiable_within_at π•œ c s x) (f : F) :
  differentiable_within_at π•œ (Ξ» y, c y β€’ f) s x :=
(hc.has_fderiv_within_at.smul_const f).differentiable_within_at

lemma differentiable_at.smul_const (hc : differentiable_at π•œ c x) (f : F) :
  differentiable_at π•œ (Ξ» y, c y β€’ f) x :=
(hc.has_fderiv_at.smul_const f).differentiable_at

lemma differentiable_on.smul_const (hc : differentiable_on π•œ c s) (f : F) :
  differentiable_on π•œ (Ξ» y, c y β€’ f) s :=
Ξ»x hx, (hc x hx).smul_const f

lemma differentiable.smul_const (hc : differentiable π•œ c) (f : F) :
  differentiable π•œ (Ξ» y, c y β€’ f) :=
Ξ»x, (hc x).smul_const f

lemma fderiv_within_smul_const (hxs : unique_diff_within_at π•œ s x)
  (hc : differentiable_within_at π•œ c s x) (f : F) :
  fderiv_within π•œ (Ξ» y, c y β€’ f) s x =
    (fderiv_within π•œ c s x).smul_right f :=
(hc.has_fderiv_within_at.smul_const f).fderiv_within hxs

lemma fderiv_smul_const (hc : differentiable_at π•œ c x) (f : F) :
  fderiv π•œ (Ξ» y, c y β€’ f) x = (fderiv π•œ c x).smul_right f :=
(hc.has_fderiv_at.smul_const f).fderiv

end smul

section mul
/-! ### Derivative of the product of two scalar-valued functions -/

set_option class.instance_max_depth 120
variables {c d : E β†’ π•œ} {c' d' : E β†’L[π•œ] π•œ}

theorem has_fderiv_within_at.mul
  (hc : has_fderiv_within_at c c' s x) (hd : has_fderiv_within_at d d' s x) :
  has_fderiv_within_at (Ξ» y, c y * d y) (c x β€’ d' + d x β€’ c') s x :=
begin
  have : is_bounded_bilinear_map π•œ (Ξ» (p : π•œ Γ— π•œ), p.1 * p.2) := is_bounded_bilinear_map_mul,
  convert has_fderiv_at.comp_has_fderiv_within_at x (this.has_fderiv_at (c x, d x)) (hc.prod hd),
  ext z,
  change c x * d' z + d x * c' z = c x * d' z + c' z * d x,
  ring
end

theorem has_fderiv_at.mul (hc : has_fderiv_at c c' x) (hd : has_fderiv_at d d' x) :
  has_fderiv_at (Ξ» y, c y * d y) (c x β€’ d' + d x β€’ c') x :=
begin
  have : is_bounded_bilinear_map π•œ (Ξ» (p : π•œ Γ— π•œ), p.1 * p.2) := is_bounded_bilinear_map_mul,
  convert has_fderiv_at.comp x (this.has_fderiv_at (c x, d x)) (hc.prod hd),
  ext z,
  change c x * d' z + d x * c' z = c x * d' z + c' z * d x,
  ring
end

lemma differentiable_within_at.mul
  (hc : differentiable_within_at π•œ c s x) (hd : differentiable_within_at π•œ d s x) :
  differentiable_within_at π•œ (Ξ» y, c y * d y) s x :=
(hc.has_fderiv_within_at.mul hd.has_fderiv_within_at).differentiable_within_at

lemma differentiable_at.mul (hc : differentiable_at π•œ c x) (hd : differentiable_at π•œ d x) :
  differentiable_at π•œ (Ξ» y, c y * d y) x :=
(hc.has_fderiv_at.mul hd.has_fderiv_at).differentiable_at

lemma differentiable_on.mul (hc : differentiable_on π•œ c s) (hd : differentiable_on π•œ d s) :
  differentiable_on π•œ (Ξ» y, c y * d y) s :=
Ξ»x hx, (hc x hx).mul (hd x hx)

lemma differentiable.mul (hc : differentiable π•œ c) (hd : differentiable π•œ d) :
  differentiable π•œ (Ξ» y, c y * d y) :=
Ξ»x, (hc x).mul (hd x)

lemma fderiv_within_mul (hxs : unique_diff_within_at π•œ s x)
  (hc : differentiable_within_at π•œ c s x) (hd : differentiable_within_at π•œ d s x) :
  fderiv_within π•œ (Ξ» y, c y * d y) s x =
    c x β€’ fderiv_within π•œ d s x + d x β€’ fderiv_within π•œ c s x :=
(hc.has_fderiv_within_at.mul hd.has_fderiv_within_at).fderiv_within hxs

lemma fderiv_mul (hc : differentiable_at π•œ c x) (hd : differentiable_at π•œ d x) :
  fderiv π•œ (Ξ» y, c y * d y) x =
    c x β€’ fderiv π•œ d x + d x β€’ fderiv π•œ c x :=
(hc.has_fderiv_at.mul hd.has_fderiv_at).fderiv

theorem has_fderiv_within_at.mul_const
  (hc : has_fderiv_within_at c c' s x) (d : π•œ) :
  has_fderiv_within_at (Ξ» y, c y * d) (d β€’ c') s x :=
begin
  have := hc.mul (has_fderiv_within_at_const d x s),
  letI : distrib_mul_action π•œ (E β†’L[π•œ] π•œ) := continuous_linear_map.module.to_distrib_mul_action,
  rwa [smul_zero, zero_add] at this
end

theorem has_fderiv_at.mul_const (hc : has_fderiv_at c c' x) (d : π•œ) :
  has_fderiv_at (Ξ» y, c y * d) (d β€’ c') x :=
begin
  rw [← has_fderiv_within_at_univ] at *,
  exact hc.mul_const d
end

lemma differentiable_within_at.mul_const
  (hc : differentiable_within_at π•œ c s x) (d : π•œ) :
  differentiable_within_at π•œ (Ξ» y, c y * d) s x :=
(hc.has_fderiv_within_at.mul_const d).differentiable_within_at

lemma differentiable_at.mul_const (hc : differentiable_at π•œ c x) (d : π•œ) :
  differentiable_at π•œ (Ξ» y, c y * d) x :=
(hc.has_fderiv_at.mul_const d).differentiable_at

lemma differentiable_on.mul_const (hc : differentiable_on π•œ c s) (d : π•œ) :
  differentiable_on π•œ (Ξ» y, c y * d) s :=
Ξ»x hx, (hc x hx).mul_const d

lemma differentiable.mul_const (hc : differentiable π•œ c) (d : π•œ) :
  differentiable π•œ (Ξ» y, c y * d) :=
Ξ»x, (hc x).mul_const d

lemma fderiv_within_mul_const (hxs : unique_diff_within_at π•œ s x)
  (hc : differentiable_within_at π•œ c s x) (d : π•œ) :
  fderiv_within π•œ (Ξ» y, c y * d) s x = d β€’ fderiv_within π•œ c s x :=
(hc.has_fderiv_within_at.mul_const d).fderiv_within hxs

lemma fderiv_mul_const (hc : differentiable_at π•œ c x) (d : π•œ) :
  fderiv π•œ (Ξ» y, c y * d) x = d β€’ fderiv π•œ c x :=
(hc.has_fderiv_at.mul_const d).fderiv

theorem has_fderiv_within_at.const_mul
  (hc : has_fderiv_within_at c c' s x) (d : π•œ) :
  has_fderiv_within_at (Ξ» y, d * c y) (d β€’ c') s x :=
begin
  simp only [mul_comm d],
  exact hc.mul_const d,
end

theorem has_fderiv_at.const_mul (hc : has_fderiv_at c c' x) (d : π•œ) :
  has_fderiv_at (Ξ» y, d * c y) (d β€’ c') x :=
begin
  simp only [mul_comm d],
  exact hc.mul_const d,
end

lemma differentiable_within_at.const_mul
  (hc : differentiable_within_at π•œ c s x) (d : π•œ) :
  differentiable_within_at π•œ (Ξ» y, d * c y) s x :=
(hc.has_fderiv_within_at.const_mul d).differentiable_within_at

lemma differentiable_at.const_mul (hc : differentiable_at π•œ c x) (d : π•œ) :
  differentiable_at π•œ (Ξ» y, d * c y) x :=
(hc.has_fderiv_at.const_mul d).differentiable_at

lemma differentiable_on.const_mul (hc : differentiable_on π•œ c s) (d : π•œ) :
  differentiable_on π•œ (Ξ» y, d * c y) s :=
Ξ»x hx, (hc x hx).const_mul d

lemma differentiable.const_mul (hc : differentiable π•œ c) (d : π•œ) :
  differentiable π•œ (Ξ» y, d * c y) :=
Ξ»x, (hc x).const_mul d

lemma fderiv_within_const_mul (hxs : unique_diff_within_at π•œ s x)
  (hc : differentiable_within_at π•œ c s x) (d : π•œ) :
  fderiv_within π•œ (Ξ» y, d * c y) s x = d β€’ fderiv_within π•œ c s x :=
(hc.has_fderiv_within_at.const_mul d).fderiv_within hxs

lemma fderiv_const_mul (hc : differentiable_at π•œ c x) (d : π•œ) :
  fderiv π•œ (Ξ» y, d * c y) x = d β€’ fderiv π•œ c x :=
(hc.has_fderiv_at.const_mul d).fderiv

end mul

end

section
/-
  In the special case of a normed space over the reals,
  we can use  scalar multiplication in the `tendsto` characterization
  of the FrΓ©chet derivative.
-/


variables {E : Type*} [normed_group E] [normed_space ℝ E]
variables {F : Type*} [normed_group F] [normed_space ℝ F]
variables {f : E β†’ F} {f' : E β†’L[ℝ] F} {x : E}

theorem has_fderiv_at_filter_real_equiv {L : filter E} :
  tendsto (Ξ» x' : E, βˆ₯x' - xβˆ₯⁻¹ * βˆ₯f x' - f x - f' (x' - x)βˆ₯) L (𝓝 0) ↔
  tendsto (Ξ» x' : E, βˆ₯x' - xβˆ₯⁻¹ β€’ (f x' - f x - f' (x' - x))) L (𝓝 0) :=
begin
  symmetry,
  rw [tendsto_iff_norm_tendsto_zero], refine tendsto_congr (Ξ» x', _),
  have : βˆ₯x' + -xβˆ₯⁻¹ β‰₯ 0, from inv_nonneg.mpr (norm_nonneg _),
  simp [norm_smul, real.norm_eq_abs, abs_of_nonneg this]
end

lemma has_fderiv_at.lim_real (hf : has_fderiv_at f f' x) (v : E) :
  tendsto (Ξ» (c:ℝ), c β€’ (f (x + c⁻¹ β€’ v) - f x)) at_top (𝓝 (f' v)) :=
begin
  apply hf.lim v,
  rw tendsto_at_top_at_top,
  exact λ b, ⟨b, λ a ha, le_trans ha (le_abs_self _)⟩
end

end

section tangent_cone

variables {π•œ : Type*} [nondiscrete_normed_field π•œ]
{E : Type*} [normed_group E] [normed_space π•œ E]
{F : Type*} [normed_group F] [normed_space π•œ F]
{f : E β†’ F} {s : set E} {f' : E β†’L[π•œ] F}

/-- The image of a tangent cone under the differential of a map is included in the tangent cone to
the image. -/
lemma has_fderiv_within_at.image_tangent_cone_subset {x : E} (h : has_fderiv_within_at f f' s x) :
  f' '' (tangent_cone_at π•œ s x) βŠ† tangent_cone_at π•œ (f '' s) (f x) :=
begin
  rw image_subset_iff,
  rintros v ⟨c, d, dtop, clim, cdlim⟩,
  refine ⟨c, (λn, f (x + d n) - f x), mem_sets_of_superset dtop _, clim, h.lim at_top dtop clim cdlim⟩,
  simp [-mem_image, mem_image_of_mem] {contextual := tt}
end

/-- If a set has the unique differentiability property at a point x, then the image of this set
under a map with onto derivative has also the unique differentiability property at the image point.
-/
lemma has_fderiv_within_at.unique_diff_within_at {x : E} (h : has_fderiv_within_at f f' s x)
  (hs : unique_diff_within_at π•œ s x) (h' : closure (range f') = univ) :
  unique_diff_within_at π•œ (f '' s) (f x) :=
begin
  have A : βˆ€v ∈ tangent_cone_at π•œ s x, f' v ∈ tangent_cone_at π•œ (f '' s) (f x),
  { assume v hv,
    have := h.image_tangent_cone_subset,
    rw image_subset_iff at this,
    exact this hv },
  have B : βˆ€v ∈ (submodule.span π•œ (tangent_cone_at π•œ s x) : set E),
    f' v ∈ (submodule.span π•œ (tangent_cone_at π•œ (f '' s) (f x)) : set F),
  { assume v hv,
    apply submodule.span_induction hv,
    { exact Ξ» w hw, submodule.subset_span (A w hw) },
    { simp },
    { assume w₁ wβ‚‚ hw₁ hwβ‚‚,
      rw continuous_linear_map.map_add,
      exact submodule.add_mem (submodule.span π•œ (tangent_cone_at π•œ (f '' s) (f x))) hw₁ hwβ‚‚ },
    { assume a w hw,
      rw continuous_linear_map.map_smul,
      exact submodule.smul_mem (submodule.span π•œ (tangent_cone_at π•œ (f '' s) (f x))) _ hw } },
  rw [unique_diff_within_at, ← univ_subset_iff],
  split,
  show f x ∈ closure (f '' s), from h.continuous_within_at.mem_closure_image hs.2,
  show univ βŠ† closure ↑(submodule.span π•œ (tangent_cone_at π•œ (f '' s) (f x))), from calc
    univ βŠ† closure (range f') : univ_subset_iff.2 h'
    ... = closure (f' '' univ) : by rw image_univ
    ... = closure (f' '' (closure (submodule.span π•œ (tangent_cone_at π•œ s x) : set E))) : by rw hs.1
    ... βŠ† closure (closure (f' '' (submodule.span π•œ (tangent_cone_at π•œ s x) : set E))) :
      closure_mono (image_closure_subset_closure_image f'.cont)
    ... = closure (f' '' (submodule.span π•œ (tangent_cone_at π•œ s x) : set E)) : closure_closure
    ... βŠ† closure (submodule.span π•œ (tangent_cone_at π•œ (f '' s) (f x)) : set F) :
      closure_mono (image_subset_iff.mpr B)
end

lemma has_fderiv_within_at.unique_diff_within_at_of_continuous_linear_equiv
  {x : E} (e' : E ≃L[π•œ] F) (h : has_fderiv_within_at f (e' : E β†’L[π•œ] F) s x)
  (hs : unique_diff_within_at π•œ s x) :
  unique_diff_within_at π•œ (f '' s) (f x) :=
begin
  apply h.unique_diff_within_at hs,
  have : range (e' : E β†’L[π•œ] F) = univ := e'.to_linear_equiv.to_equiv.range_eq_univ,
  rw [this, closure_univ]
end

end tangent_cone

section restrict_scalars
/-! ### Restricting from `β„‚` to `ℝ`, or generally from `π•œ'` to `π•œ`

If a function is differentiable over `β„‚`, then it is differentiable over `ℝ`. In this paragraph,
we give variants of this statement, in the general situation where `β„‚` and `ℝ` are replaced
respectively by `π•œ'` and `π•œ` where `π•œ'` is a normed algebra over `π•œ`. -/

variables (π•œ : Type*) [nondiscrete_normed_field π•œ]
{π•œ' : Type*} [nondiscrete_normed_field π•œ'] [normed_algebra π•œ π•œ']
{E : Type*} [normed_group E] [normed_space π•œ' E]
{F : Type*} [normed_group F] [normed_space π•œ' F]
{f : E β†’ F} {f' : E β†’L[π•œ'] F} {s : set E} {x : E}

local attribute [instance] normed_space.restrict_scalars

lemma has_fderiv_at.restrict_scalars (h : has_fderiv_at f f' x) :
  has_fderiv_at f (f'.restrict_scalars π•œ) x := h

lemma has_fderiv_within_at.restrict_scalars (h : has_fderiv_within_at f f' s x) :
  has_fderiv_within_at f (f'.restrict_scalars π•œ) s x := h

lemma differentiable_at.restrict_scalars (h : differentiable_at π•œ' f x) :
  differentiable_at π•œ f x :=
(h.has_fderiv_at.restrict_scalars π•œ).differentiable_at

lemma differentiable_within_at.restrict_scalars (h : differentiable_within_at π•œ' f s x) :
  differentiable_within_at π•œ f s x :=
(h.has_fderiv_within_at.restrict_scalars π•œ).differentiable_within_at

lemma differentiable_on.restrict_scalars (h : differentiable_on π•œ' f s) :
  differentiable_on π•œ f s :=
Ξ»x hx, (h x hx).restrict_scalars π•œ

lemma differentiable.restrict_scalars (h : differentiable π•œ' f) :
  differentiable π•œ f :=
Ξ»x, (h x).restrict_scalars π•œ

end restrict_scalars