Theory Mixed_Partials

theory Mixed_Partials
  imports "Smooth_Manifolds.Smooth"
    "Ordinary_Differential_Equations.Multivariate_Taylor"
begin

no_notation blinfun_apply (infixl ‹$› 999)
notation vec_nth (infixl ‹$› 90)

section ‹Symmetry of second derivatives›

text ‹We specialize the symmetry theorem from the AFP ordinary differential
  equations entry to scalar functions, then lift through inner products. This
  preserves the real-inner-space codomain without requiring completeness.›

theorem c2_scalar_second_derivative_symmetric:
  fixes f :: "'a::real_normed_vector ⇒ real"
  assumes op: "open U" and sm: "2-smooth_on U f" and p: "p ∈ U"
  shows "frechet_derivative (λq. frechet_derivative f (at q) v) (at p) w =
    frechet_derivative (λq. frechet_derivative f (at q) w) (at p) v"
proof -
  have df: "f differentiable (at q)" if "q ∈ U" for q
    using smooth_on_imp_differentiable_on[OF sm] op that
    by (simp add: differentiable_on_eq_differentiable_at)
  have ds: "1-smooth_on U (λq. frechet_derivative f (at q) t)" for t
    using derivative_is_smooth'[where k=1 and S=U and f=f and v=t] sm by simp
  have ddf: "(λq. frechet_derivative f (at q) t) differentiable (at p)" for t
    using smooth_on_imp_differentiable_on[OF ds[of t]] op p
    by (simp add: differentiable_on_eq_differentiable_at)
  interpret second_derivative_on_open f "λq. frechet_derivative f (at q)"
    "λt. frechet_derivative (λq. frechet_derivative f (at q) t) (at p)" p U
    by unfold_locales
      (auto intro: frechet_derivative_worksI df ddf simp: op p)
  show ?thesis by (rule symmetric_second_derivative[OF p])
qed

lemma frechet_inner_fixed:
  fixes f :: "'a::real_normed_vector ⇒ 'b::real_inner"
  assumes df: "f differentiable (at p)"
  shows "frechet_derivative (λq. inner (f q) z) (at p) v =
    inner (frechet_derivative f (at p) v) z"
  using bounded_bilinear.frechet_derivative[where y="λ_. z",
    OF bounded_bilinear_inner df differentiable_const]
  by (simp add: fun_eq_iff)

theorem c2_second_derivative_symmetric:
  fixes f :: "'a::real_normed_vector ⇒ 'b::real_inner"
  assumes op: "open U" and sm: "2-smooth_on U f" and p: "p ∈ U"
  shows "frechet_derivative (λq. frechet_derivative f (at q) v) (at p) w =
    frechet_derivative (λq. frechet_derivative f (at q) w) (at p) v"
proof -
  let ?D = "λv q. frechet_derivative f (at q) v"
  have df: "f differentiable (at q)" if "q ∈ U" for q
    using smooth_on_imp_differentiable_on[OF sm] op that
    by (simp add: differentiable_on_eq_differentiable_at)
  have ds: "1-smooth_on U (?D t)" for t
    using derivative_is_smooth'[where k=1 and S=U and f=f and v=t] sm by simp
  have ddf: "?D t differentiable (at q)" if "q ∈ U" for t q
    using smooth_on_imp_differentiable_on[OF ds[of t]] op that
    by (simp add: differentiable_on_eq_differentiable_at)
  have projection: "frechet_derivative (λq. frechet_derivative
      (λx. inner (f x) z) (at q) t) (at p) s =
    inner (frechet_derivative (?D t) (at p) s) z" for z t s
  proof -
    have contdiff: "(λq. inner (?D t q) z) differentiable (at p)"
      by (intro differentiable_inner ddf[OF p] differentiable_const)
    have eq: "frechet_derivative (λq. inner (?D t q) z) (at p) =
      frechet_derivative (λq. frechet_derivative (λx. inner (f x) z) (at q) t) (at p)"
      using frechet_derivative_transform_within_open[OF op p] frechet_inner_fixed[OF df]
      by (metis (mono_tags, lifting))
    show ?thesis using fun_cong[OF eq, where x=s]
      frechet_inner_fixed[where f="?D t" and z=z and v=s, OF ddf[OF p]]
      by simp
  qed
  have inner_eq: "inner (frechet_derivative (?D v) (at p) w) z =
    inner (frechet_derivative (?D w) (at p) v) z" for z
  proof -
    have scalar: "2-smooth_on U (λq. inner (f q) z)"
      by (intro smooth_on_inner sm smooth_on_const op)
    show ?thesis
      using c2_scalar_second_derivative_symmetric[OF op scalar p, of v w]
      by (simp add: projection)
  qed
  let ?d = "frechet_derivative (?D v) (at p) w - frechet_derivative (?D w) (at p) v"
  have "inner ?d ?d = 0" using inner_eq[of ?d] by (simp add: inner_diff_left)
  thus ?thesis by simp
qed

end