Theory Regular_Surface_Patch

theory Regular_Surface_Patch
  imports Mixed_Partials "HOL-Analysis.Cross3"
begin

section ‹Derivative-based regular surface patches›

text ‹All geometric objects below are built from the immersion's actual
  derivatives. The regular-patch locale supplies precisely an open parameter
  domain, three continuous derivatives, and a nonzero cross product.›

subsection ‹Geometric definitions›

type_synonym surface_parameter = "real^2"
type_synonym surface_vector = "real^3"

definition patch_derivative ::
  "(surface_parameter ⇒ surface_vector) ⇒ surface_parameter ⇒
    surface_parameter ⇒ surface_vector" where
  "patch_derivative X p = frechet_derivative X (at p)"

definition patch_u where
  "patch_u X p = patch_derivative X p (axis 1 1)"

definition patch_v where
  "patch_v X p = patch_derivative X p (axis 2 1)"

definition patch_normal where
  "patch_normal X p = cross3 (patch_u X p) (patch_v X p)"

definition gauss_map where
  "gauss_map X p = inverse (norm (patch_normal X p)) *R patch_normal X p"

definition tangent_plane where
  "tangent_plane X p = range (patch_derivative X p)"

definition tangent_coordinates where
  "tangent_coordinates X p = inv (patch_derivative X p)"

definition shape_operator where
  "shape_operator X p w =
    - frechet_derivative (gauss_map X) (at p) (tangent_coordinates X p w)"

definition first_fundamental_form where
  "first_fundamental_form X p a b = inner (patch_derivative X p a) (patch_derivative X p b)"

definition second_fundamental_form where
  "second_fundamental_form X p a b =
    inner (frechet_derivative (λq. patch_derivative X q b) (at p) a) (gauss_map X p)"

definition patch_second_derivative where
  "patch_second_derivative X p a b =
    frechet_derivative (λq. patch_derivative X q b) (at p) a"

definition patch_connection where
  "patch_connection X p a b = tangent_coordinates X p
    (patch_second_derivative X p a b -
     second_fundamental_form X p a b *R gauss_map X p)"

definition shape_coordinates where
  "shape_coordinates X p a = tangent_coordinates X p
    (shape_operator X p (patch_derivative X p a))"

definition shape_matrix ::
  "(surface_parameter ⇒ surface_vector) ⇒ surface_parameter ⇒ real^2^2" where
  "shape_matrix X p = (χ i j. (shape_coordinates X p (axis j 1))$i)"

definition gaussian_curvature where
  "gaussian_curvature X p =
    (second_fundamental_form X p (axis 1 1) (axis 1 1) *
       second_fundamental_form X p (axis 2 1) (axis 2 1) -
     second_fundamental_form X p (axis 1 1) (axis 2 1) *
       second_fundamental_form X p (axis 2 1) (axis 1 1)) /
    (first_fundamental_form X p (axis 1 1) (axis 1 1) *
       first_fundamental_form X p (axis 2 1) (axis 2 1) -
     first_fundamental_form X p (axis 1 1) (axis 2 1) ^ 2)"

lemma metric_determinant_normal:
  "first_fundamental_form X p (axis 1 1) (axis 1 1) *
     first_fundamental_form X p (axis 2 1) (axis 2 1) -
   first_fundamental_form X p (axis 1 1) (axis 2 1) ^ 2 =
   norm (patch_normal X p) ^ 2"
  using norm_cross_dot[of "patch_u X p" "patch_v X p"]
  unfolding first_fundamental_form_def patch_normal_def
    patch_u_def patch_v_def power_mult_distrib power2_norm_eq_inner
  by linarith

lemma gram_normal_decomposition:
  fixes u v w :: surface_vector
  shows "(inner u u * inner v v - inner u v ^ 2) *R w =
    (inner w u * inner v v - inner w v * inner u v) *R u +
    (inner w v * inner u u - inner w u * inner u v) *R v +
    inner w (cross3 u v) *R cross3 u v"
  by (simp add: cross3_simps forall_3 power2_eq_square)

subsection ‹Regularity and derivative symmetry›

text ‹The smoothness hypothesis supplies the mixed second and third derivative
  identities used later in the Gauss equation.›

locale regular_surface_patch =
  fixes U :: "surface_parameter set"
    and X :: "surface_parameter ⇒ surface_vector"
  assumes open_domain: "open U"
    and c3: "3-smooth_on U X"
    and regular: "p ∈ U ⟹ patch_normal X p ≠ 0"
begin

lemma restrict_patch:
  assumes "open V" "V ⊆ U"
  shows "regular_surface_patch V X"
  by (unfold_locales, use assms open_domain c3 regular in
    ‹auto intro: smooth_on_subset›)

lemma immersion_differentiable:
  assumes "p ∈ U"
  shows "X differentiable (at p)"
  using smooth_on_imp_differentiable_on[OF c3] assms open_domain
  by (simp add: differentiable_on_eq_differentiable_at)

lemma immersion_c2: "2-smooth_on U X"
proof -
  have le: "(2::enat) ≤ 3" by simp
  show ?thesis using c3 le unfolding smooth_on_def by (meson order_trans)
qed

lemma patch_second_derivative_symmetric:
  assumes "p ∈ U"
  shows "patch_second_derivative X p a b = patch_second_derivative X p b a"
  unfolding patch_second_derivative_def patch_derivative_def
  by (rule c2_second_derivative_symmetric[OF open_domain immersion_c2 assms])

lemma second_fundamental_form_symmetric:
  assumes "p ∈ U"
  shows "second_fundamental_form X p a b = second_fundamental_form X p b a"
  unfolding second_fundamental_form_def patch_second_derivative_def[symmetric]
  by (simp add: patch_second_derivative_symmetric[OF assms])

lemma patch_connection_symmetric:
  assumes "p ∈ U"
  shows "patch_connection X p a b = patch_connection X p b a"
  unfolding patch_connection_def
  by (simp add: patch_second_derivative_symmetric[OF assms]
    second_fundamental_form_symmetric[OF assms])

lemma patch_derivative_linear:
  "p ∈ U ⟹ linear (patch_derivative X p)"
  unfolding patch_derivative_def
  by (rule linear_frechet_derivative, rule immersion_differentiable)

lemma patch_derivative_c2:
  "2-smooth_on U (λp. patch_derivative X p a)"
  unfolding patch_derivative_def
  using derivative_is_smooth'[where k=2 and S=U and f=X and v=a] c3 by simp

lemma patch_second_derivative_c1:
  "1-smooth_on U (λp. frechet_derivative (λq. patch_derivative X q b) (at p) a)"
  using derivative_is_smooth'[where k=1 and S=U and
    f="λq. patch_derivative X q b" and v=a] patch_derivative_c2[of b] by simp

lemma patch_third_derivative_commute:
  assumes "p ∈ U"
  shows "frechet_derivative (λq. patch_second_derivative X q a b) (at p) c =
    frechet_derivative (λq. patch_second_derivative X q c b) (at p) a"
  unfolding patch_second_derivative_def
  by (rule c2_second_derivative_symmetric[OF open_domain patch_derivative_c2 assms])

lemma patch_direction_differentiable:
  assumes "p ∈ U"
  shows "(λq. patch_derivative X q a) differentiable (at p)"
  using smooth_on_imp_differentiable_on[OF patch_derivative_c2[of a]] assms open_domain
  by (simp add: differentiable_on_eq_differentiable_at)

lemma patch_normal_differentiable:
  assumes p: "p ∈ U"
  shows "patch_normal X differentiable (at p)"
proof -
  have bounded: "bounded_bilinear cross3"
    using bilinear_cross by (simp add: bilinear_conv_bounded_bilinear)
  have du: "patch_u X differentiable (at p)"
    unfolding patch_u_def using patch_direction_differentiable[OF p] .
  have dv: "patch_v X differentiable (at p)"
    unfolding patch_v_def using patch_direction_differentiable[OF p] .
  show ?thesis unfolding patch_normal_def
    using bounded_bilinear.FDERIV[OF bounded
      du[unfolded frechet_derivative_works] dv[unfolded frechet_derivative_works]]
    by (auto intro: differentiableI)
qed

lemma gauss_map_differentiable:
  assumes p: "p ∈ U"
  shows "gauss_map X differentiable (at p)"
proof -
  have dn: "patch_normal X differentiable (at p)"
    by (rule patch_normal_differentiable[OF p])
  have nn: "(λq. norm (patch_normal X q)) differentiable (at p)"
    by (rule differentiable_compose[OF differentiable_norm_at[OF regular[OF p]] dn])
  show ?thesis unfolding gauss_map_def
    by (intro differentiable_scaleR differentiable_inverse nn dn, simp add: regular[OF p])
qed

subsection ‹Immersion, metric, and tangent plane›

text ‹Nonvanishing of the cross product makes the immersion derivative injective
  and the metric positive definite. Tangent coordinates are used only on the
  range of that derivative.›

lemma patch_derivative_coordinates:
  assumes "p ∈ U"
  shows "patch_derivative X p a = a$1 *R patch_u X p + a$2 *R patch_v X p"
proof -
  have coordinates: "a = a$1 *R axis 1 1 + a$2 *R axis 2 1"
    by (simp add: vec_eq_iff forall_2 axis_def)
  interpret D: linear "patch_derivative X p"
    by (rule patch_derivative_linear[OF assms])
  show ?thesis
    by (subst coordinates, simp add: D.add D.scaleR patch_u_def patch_v_def)
qed

lemma metric_determinant_positive:
  assumes "p ∈ U"
  shows "0 < first_fundamental_form X p (axis 1 1) (axis 1 1) *
     first_fundamental_form X p (axis 2 1) (axis 2 1) -
   first_fundamental_form X p (axis 1 1) (axis 2 1) ^ 2"
  unfolding metric_determinant_normal
  using regular[OF assms] by simp

lemma patch_derivative_kernel:
  assumes p: "p ∈ U" and zero: "patch_derivative X p a = 0"
  shows "a = 0"
proof -
  have coordinates: "a$1 *R patch_u X p + a$2 *R patch_v X p = 0"
    using zero patch_derivative_coordinates[OF p, of a] by simp
  have one: "a$1 = 0"
    using arg_cong[OF coordinates, of "λw. cross3 w (patch_v X p)"] regular[OF p]
    by (simp add: cross_add_left cross_mult_left patch_normal_def)
  have two: "a$2 = 0"
    using arg_cong[OF coordinates, of "λw. cross3 (patch_u X p) w"] regular[OF p]
    by (simp add: cross_add_right cross_mult_right patch_normal_def)
  show ?thesis using one two by (simp add: vec_eq_iff forall_2)
qed

lemma patch_derivative_injective:
  assumes p: "p ∈ U"
  shows "inj (patch_derivative X p)"
proof (rule injI)
  fix a b
  assume eq: "patch_derivative X p a = patch_derivative X p b"
  interpret D: linear "patch_derivative X p"
    by (rule patch_derivative_linear[OF p])
  have "patch_derivative X p (a - b) = 0" using eq by (simp add: D.diff)
  hence "a - b = 0" by (rule patch_derivative_kernel[OF p])
  thus "a = b" by simp
qed

lemma metric_positive_definite:
  assumes "p ∈ U" "a ≠ 0"
  shows "0 < first_fundamental_form X p a a"
  using patch_derivative_kernel[OF assms(1)] assms(2)
  by (auto simp: first_fundamental_form_def inner_gt_zero_iff)

lemma tangent_plane_coordinates:
  assumes "p ∈ U"
  shows "tangent_plane X p = {s *R patch_u X p + t *R patch_v X p |s t. True}"
proof (rule set_eqI)
  fix w
  show "w ∈ tangent_plane X p ⟷
    w ∈ {s *R patch_u X p + t *R patch_v X p |s t. True}"
  proof
    assume "w ∈ tangent_plane X p"
    then obtain a where "w = patch_derivative X p a"
      unfolding tangent_plane_def by blast
    thus "w ∈ {s *R patch_u X p + t *R patch_v X p |s t. True}"
      using patch_derivative_coordinates[OF assms] by blast
  next
    assume "w ∈ {s *R patch_u X p + t *R patch_v X p |s t. True}"
    then obtain s t where w: "w = s *R patch_u X p + t *R patch_v X p" by blast
    have "patch_derivative X p (vector [s,t]) = w"
      by (simp add: patch_derivative_coordinates[OF assms] vector_def w)
    thus "w ∈ tangent_plane X p" unfolding tangent_plane_def by blast
  qed
qed

lemma gauss_map_unit:
  assumes "p ∈ U"
  shows "norm (gauss_map X p) = 1"
  using regular[OF assms]
  by (simp add: gauss_map_def norm_scaleR)

lemma gauss_map_orthogonal_u:
  "inner (gauss_map X p) (patch_u X p) = 0"
  by (simp add: gauss_map_def patch_normal_def inner_scaleR_left inner_commute dot_cross_self)

lemma gauss_map_orthogonal_v:
  "inner (gauss_map X p) (patch_v X p) = 0"
  by (simp add: gauss_map_def patch_normal_def inner_scaleR_left dot_cross_self)

lemma gauss_map_orthogonal_derivative:
  assumes "p ∈ U"
  shows "inner (gauss_map X p) (patch_derivative X p a) = 0"
  by (simp add: patch_derivative_coordinates[OF assms] inner_add_right
    gauss_map_orthogonal_u gauss_map_orthogonal_v)

lemma gauss_map_orthogonal_tangent:
  assumes "p ∈ U" "w ∈ tangent_plane X p"
  shows "inner (gauss_map X p) w = 0"
  using assms gauss_map_orthogonal_derivative unfolding tangent_plane_def by blast

lemma tangent_coordinates_derivative:
  assumes "p ∈ U"
  shows "tangent_coordinates X p (patch_derivative X p a) = a"
  unfolding tangent_coordinates_def
  by (rule inv_f_f[OF patch_derivative_injective[OF assms]])

lemma tangent_plane_normal_kernel:
  assumes p: "p ∈ U"
  shows "w ∈ tangent_plane X p ⟷ inner (gauss_map X p) w = 0"
proof
  show "w ∈ tangent_plane X p ⟹ inner (gauss_map X p) w = 0"
    by (rule gauss_map_orthogonal_tangent[OF p])
next
  assume orth: "inner (gauss_map X p) w = 0"
  let ?u = "patch_u X p"
  let ?v = "patch_v X p"
  let ?d = "inner ?u ?u * inner ?v ?v - inner ?u ?v ^ 2"
  let ?s = "inner w ?u * inner ?v ?v - inner w ?v * inner ?u ?v"
  let ?t = "inner w ?v * inner ?u ?u - inner w ?u * inner ?u ?v"
  have nonzero: "?d ≠ 0"
    using metric_determinant_positive[OF p]
    unfolding first_fundamental_form_def patch_u_def patch_v_def by linarith
  have normal_zero: "inner w (cross3 ?u ?v) = 0"
    using orth regular[OF p]
    by (simp add: gauss_map_def patch_normal_def inner_commute)
  have decomp: "?d *R w = ?s *R ?u + ?t *R ?v"
    using gram_normal_decomposition[of ?u ?v w] normal_zero by simp
  have "w = (?s / ?d) *R ?u + (?t / ?d) *R ?v"
    using arg_cong[OF decomp, of "λz. inverse ?d *R z"] nonzero
    by (simp add: scaleR_add_right scaleR_scaleR divide_inverse mult.commute)
  thus "w ∈ tangent_plane X p"
    unfolding tangent_plane_coordinates[OF p] by blast
qed

lemma derivative_tangent_coordinates:
  assumes "p ∈ U" "w ∈ tangent_plane X p"
  shows "patch_derivative X p (tangent_coordinates X p w) = w"
  using assms tangent_coordinates_derivative unfolding tangent_plane_def by auto

subsection ‹The shape operator and Weingarten identity›

text ‹Differentiating normal orthogonality identifies the shape operator with
  the second fundamental form. Its linearity is asserted on tangent vectors.›

lemma shape_operator_on_derivative:
  assumes "p ∈ U"
  shows "shape_operator X p (patch_derivative X p a) =
    - frechet_derivative (gauss_map X) (at p) a"
  by (simp add: shape_operator_def tangent_coordinates_derivative[OF assms])

lemma tangent_coordinates_add:
  assumes p: "p ∈ U" and w: "w ∈ tangent_plane X p" and z: "z ∈ tangent_plane X p"
  shows "tangent_coordinates X p (w + z) = tangent_coordinates X p w + tangent_coordinates X p z"
proof -
  interpret D: linear "patch_derivative X p"
    by (rule patch_derivative_linear[OF p])
  have "w + z = patch_derivative X p (tangent_coordinates X p w + tangent_coordinates X p z)"
    by (simp add: D.add derivative_tangent_coordinates[OF p w] derivative_tangent_coordinates[OF p z])
  thus ?thesis by (simp add: tangent_coordinates_derivative[OF p])
qed

lemma tangent_coordinates_scale:
  assumes p: "p ∈ U" and w: "w ∈ tangent_plane X p"
  shows "tangent_coordinates X p (r *R w) = r *R tangent_coordinates X p w"
proof -
  interpret D: linear "patch_derivative X p"
    by (rule patch_derivative_linear[OF p])
  have "r *R w = patch_derivative X p (r *R tangent_coordinates X p w)"
    by (simp add: D.scaleR derivative_tangent_coordinates[OF p w])
  thus ?thesis by (simp add: tangent_coordinates_derivative[OF p])
qed

lemma shape_operator_add:
  assumes p: "p ∈ U" and w: "w ∈ tangent_plane X p" and z: "z ∈ tangent_plane X p"
  shows "shape_operator X p (w + z) = shape_operator X p w + shape_operator X p z"
proof -
  interpret N: linear "frechet_derivative (gauss_map X) (at p)"
    by (rule linear_frechet_derivative[OF gauss_map_differentiable[OF p]])
  show ?thesis by (simp add: shape_operator_def tangent_coordinates_add[OF p w z] N.add)
qed

lemma shape_operator_scale:
  assumes p: "p ∈ U" and w: "w ∈ tangent_plane X p"
  shows "shape_operator X p (r *R w) = r *R shape_operator X p w"
proof -
  interpret N: linear "frechet_derivative (gauss_map X) (at p)"
    by (rule linear_frechet_derivative[OF gauss_map_differentiable[OF p]])
  show ?thesis by (simp add: shape_operator_def tangent_coordinates_scale[OF p w] N.scaleR)
qed

lemma weingarten_identity:
  assumes p: "p ∈ U"
  shows "inner (frechet_derivative (gauss_map X) (at p) a) (patch_derivative X p b) =
    - second_fundamental_form X p a b"
proof -
  let ?N = "gauss_map X"
  let ?V = "λq. patch_derivative X q b"
  have dn: "(?N has_derivative frechet_derivative ?N (at p)) (at p)"
    using gauss_map_differentiable[OF p] unfolding frechet_derivative_works .
  have dv: "(?V has_derivative frechet_derivative ?V (at p)) (at p)"
    using patch_direction_differentiable[OF p, of b] unfolding frechet_derivative_works .
  have product: "((λq. inner (?N q) (?V q)) has_derivative
      (λh. inner (?N p) (frechet_derivative ?V (at p) h) +
        inner (frechet_derivative ?N (at p) h) (?V p))) (at p)"
    by (rule bounded_bilinear.FDERIV[OF bounded_bilinear_inner dn dv])
  have zero: "((λq. inner (?N q) (?V q)) has_derivative (λh. 0)) (at p)"
    by (rule has_derivative_transform_within_open[OF has_derivative_const open_domain p],
        simp add: gauss_map_orthogonal_derivative)
  have "(λh. inner (?N p) (frechet_derivative ?V (at p) h) +
        inner (frechet_derivative ?N (at p) h) (?V p)) = (λh. 0)"
    by (rule has_derivative_unique[OF product zero])
  hence "inner (?N p) (frechet_derivative ?V (at p) a) +
        inner (frechet_derivative ?N (at p) a) (?V p) = 0"
    by (rule fun_cong)
  thus ?thesis unfolding second_fundamental_form_def
    by (metis add_eq_0_iff inner_commute)
qed

lemma shape_operator_second_form:
  assumes "p ∈ U"
  shows "inner (shape_operator X p (patch_derivative X p a)) (patch_derivative X p b) =
    second_fundamental_form X p a b"
  by (simp add: shape_operator_on_derivative[OF assms] weingarten_identity[OF assms])

lemma shape_operator_self_adjoint:
  assumes p: "p ∈ U" and w: "w ∈ tangent_plane X p" and z: "z ∈ tangent_plane X p"
  shows "inner (shape_operator X p w) z = inner w (shape_operator X p z)"
proof -
  obtain a where a: "w = patch_derivative X p a" using w unfolding tangent_plane_def by blast
  obtain b where b: "z = patch_derivative X p b" using z unfolding tangent_plane_def by blast
  show ?thesis unfolding a b
    by (metis shape_operator_second_form[OF p] second_fundamental_form_symmetric[OF p] inner_commute)
qed

subsection ‹Tangency of the Gauss-map derivative›

text ‹Differentiating the unit-length identity shows that the normal derivative
  is tangent. This also justifies the coordinate representation of the shape
  operator.›

lemma gauss_derivative_orthogonal:
  assumes p: "p ∈ U"
  shows "inner (gauss_map X p) (frechet_derivative (gauss_map X) (at p) a) = 0"
proof -
  let ?N = "gauss_map X"
  have dn: "(?N has_derivative frechet_derivative ?N (at p)) (at p)"
    using gauss_map_differentiable[OF p] unfolding frechet_derivative_works .
  have product: "((λq. inner (?N q) (?N q)) has_derivative
      (λh. inner (?N p) (frechet_derivative ?N (at p) h) +
        inner (frechet_derivative ?N (at p) h) (?N p))) (at p)"
    by (rule bounded_bilinear.FDERIV[OF bounded_bilinear_inner dn dn])
  have const_deriv: "((λq. inner (?N q) (?N q)) has_derivative (λh. 0)) (at p)"
  proof (rule has_derivative_transform_within_open[where f="λq. 1",
      OF has_derivative_const open_domain p])
    fix q assume "q ∈ U"
    thus "1 = inner (?N q) (?N q)"
      using gauss_map_unit[of q] by (simp add: dot_square_norm)
  qed
  have "(λh. inner (?N p) (frechet_derivative ?N (at p) h) +
        inner (frechet_derivative ?N (at p) h) (?N p)) = (λh. 0)"
    by (rule has_derivative_unique[OF product const_deriv])
  hence "inner (?N p) (frechet_derivative ?N (at p) a) +
        inner (frechet_derivative ?N (at p) a) (?N p) = 0"
    by (rule fun_cong)
  thus ?thesis by (simp add: inner_commute)
qed

lemma gauss_derivative_tangent:
  assumes "p ∈ U"
  shows "frechet_derivative (gauss_map X) (at p) a ∈ tangent_plane X p"
  using gauss_derivative_orthogonal[OF assms]
  by (simp add: tangent_plane_normal_kernel[OF assms])

lemma shape_operator_tangent:
  assumes "p ∈ U"
  shows "shape_operator X p w ∈ tangent_plane X p"
  by (simp add: tangent_plane_normal_kernel[OF assms] shape_operator_def
    gauss_derivative_orthogonal[OF assms])

lemma second_derivative_tangential_part:
  assumes p: "p ∈ U"
  shows "patch_second_derivative X p a b -
    second_fundamental_form X p a b *R gauss_map X p ∈ tangent_plane X p"
proof -
  have unit: "inner (gauss_map X p) (gauss_map X p) = 1"
    using gauss_map_unit[OF p] by (simp add: dot_square_norm)
  show ?thesis
    by (simp add: tangent_plane_normal_kernel[OF p] inner_diff_right unit
      second_fundamental_form_def patch_second_derivative_def inner_commute)
qed

subsection ‹Gaussian curvature as a determinant›

text ‹The second-form quotient agrees with the determinant of the coordinate
  matrix of the shape operator. Positivity of the metric determinant
  justifies division.›

lemma shape_coordinates_representation:
  assumes p: "p ∈ U"
  shows "patch_derivative X p (shape_coordinates X p a) =
    shape_operator X p (patch_derivative X p a)"
  unfolding shape_coordinates_def
  by (rule derivative_tangent_coordinates[OF p shape_operator_tangent[OF p]])

lemma second_form_shape_coordinates:
  assumes p: "p ∈ U"
  shows "second_fundamental_form X p a b =
    first_fundamental_form X p (shape_coordinates X p a) b"
  unfolding first_fundamental_form_def
  by (simp add: shape_coordinates_representation[OF p] shape_operator_second_form[OF p])

lemma shape_determinant_curvature:
  assumes p: "p ∈ U"
  shows "matrix_det (shape_matrix X p) = gaussian_curvature X p"
proof -
  let ?e = "axis 1 1 :: surface_parameter"
  let ?f = "axis 2 1 :: surface_parameter"
  let ?I = "first_fundamental_form X p"
  let ?II = "second_fundamental_form X p"
  have nonzero: "?I ?e ?e * ?I ?f ?f - ?I ?e ?f ^ 2 ≠ 0"
    using metric_determinant_positive[OF p] by linarith
  have identity: "matrix_det (shape_matrix X p) * (?I ?e ?e * ?I ?f ?f - ?I ?e ?f ^ 2) =
    ?II ?e ?e * ?II ?f ?f - ?II ?e ?f * ?II ?f ?e"
    unfolding second_form_shape_coordinates[OF p]
    unfolding first_fundamental_form_def shape_matrix_def det_2
    by (simp add: patch_derivative_coordinates[OF p] axis_def inner_commute power2_eq_square algebra_simps)
  show ?thesis unfolding gaussian_curvature_def
    using identity nonzero by (simp add: field_simps)
qed

lemma shape_coordinates_linear:
  assumes p: "p ∈ U"
  shows "linear (shape_coordinates X p)"
proof
  fix a b
  have tangent: "patch_derivative X p c ∈ tangent_plane X p" for c
    unfolding tangent_plane_def by simp
  interpret D: linear "patch_derivative X p" by (rule patch_derivative_linear[OF p])
  show "shape_coordinates X p (a + b) = shape_coordinates X p a + shape_coordinates X p b"
    unfolding shape_coordinates_def D.add
    by (simp add: shape_operator_add[OF p tangent tangent]
      tangent_coordinates_add[OF p shape_operator_tangent[OF p] shape_operator_tangent[OF p]])
next
  fix r a
  have tangent: "patch_derivative X p a ∈ tangent_plane X p"
    unfolding tangent_plane_def by simp
  interpret D: linear "patch_derivative X p" by (rule patch_derivative_linear[OF p])
  show "shape_coordinates X p (r *R a) = r *R shape_coordinates X p a"
    unfolding shape_coordinates_def D.scaleR
    by (simp add: shape_operator_scale[OF p tangent]
      tangent_coordinates_scale[OF p shape_operator_tangent[OF p]])
qed

lemma shape_matrix_action:
  assumes p: "p ∈ U"
  shows "shape_matrix X p *v a = shape_coordinates X p a"
proof -
  interpret S: linear "shape_coordinates X p" by (rule shape_coordinates_linear[OF p])
  have coordinates: "a = a$1 *R axis 1 1 + a$2 *R axis 2 1"
    by (simp add: vec_eq_iff forall_2 axis_def)
  have "shape_coordinates X p a = a$1 *R shape_coordinates X p (axis 1 1) +
      a$2 *R shape_coordinates X p (axis 2 1)"
    by (subst coordinates, simp add: S.add S.scaleR)
  thus ?thesis by (simp add: shape_matrix_def matrix_vector_mult_def sum_2 vec_eq_iff mult.commute)
qed

subsection ‹The induced connection and metric compatibility›

text ‹The Gauss formula separates the second derivative into tangent and normal
  parts. Metric compatibility and symmetry then determine the tangent
  connection from derivatives of the first fundamental form.›

lemma gauss_formula:
  assumes p: "p ∈ U"
  shows "patch_second_derivative X p a b =
    patch_derivative X p (patch_connection X p a b) +
    second_fundamental_form X p a b *R gauss_map X p"
  unfolding patch_connection_def
  using derivative_tangent_coordinates[OF p second_derivative_tangential_part[OF p, of a b]]
  by simp

lemma metric_derivative:
  assumes p: "p ∈ U"
  shows "frechet_derivative (λq. first_fundamental_form X q b c) (at p) a =
    inner (patch_second_derivative X p a b) (patch_derivative X p c) +
    inner (patch_derivative X p b) (patch_second_derivative X p a c)"
proof -
  have db: "((λq. patch_derivative X q b) has_derivative
    frechet_derivative (λq. patch_derivative X q b) (at p)) (at p)"
    using patch_direction_differentiable[OF p, of b] unfolding frechet_derivative_works .
  have dc: "((λq. patch_derivative X q c) has_derivative
    frechet_derivative (λq. patch_derivative X q c) (at p)) (at p)"
    using patch_direction_differentiable[OF p, of c] unfolding frechet_derivative_works .
  have product: "((λq. first_fundamental_form X q b c) has_derivative
      (λh. inner (patch_derivative X p b) (patch_second_derivative X p h c) +
        inner (patch_second_derivative X p h b) (patch_derivative X p c))) (at p)"
    unfolding first_fundamental_form_def patch_second_derivative_def
    by (rule bounded_bilinear.FDERIV[OF bounded_bilinear_inner db dc])
  show ?thesis using fun_cong[OF frechet_derivative_at[OF product], of a]
    by (simp add: add.commute)
qed

lemma metric_compatibility:
  assumes p: "p ∈ U"
  shows "frechet_derivative (λq. first_fundamental_form X q b c) (at p) a =
    first_fundamental_form X p (patch_connection X p a b) c +
    first_fundamental_form X p b (patch_connection X p a c)"
  unfolding metric_derivative[OF p]
  by (subst (1 2) gauss_formula[OF p],
      simp add: first_fundamental_form_def inner_add_left inner_add_right
        gauss_map_orthogonal_derivative[OF p] inner_commute)

lemma connection_metric_formula:
  assumes p: "p ∈ U"
  shows "2 * first_fundamental_form X p (patch_connection X p a b) c =
    frechet_derivative (λq. first_fundamental_form X q b c) (at p) a +
    frechet_derivative (λq. first_fundamental_form X q a c) (at p) b -
    frechet_derivative (λq. first_fundamental_form X q a b) (at p) c"
  unfolding metric_compatibility[OF p]
  by (simp add: patch_connection_symmetric[OF p]
    first_fundamental_form_def inner_commute algebra_simps)

end

lemma first_fundamental_form_symmetric:
  "first_fundamental_form X p a b = first_fundamental_form X p b a"
  by (simp add: first_fundamental_form_def inner_commute)

lemma first_fundamental_form_nonnegative:
  "0 ≤ first_fundamental_form X p a a"
  by (simp add: first_fundamental_form_def)

end