Theory Surface_Reparametrization

theory Surface_Reparametrization
  imports Gauss_Equation
begin

section ‹Coordinate changes of actual immersions›

definition parameter_jacobian where
  "parameter_jacobian (f :: surface_parameter ⇒ surface_parameter) p =
    (frechet_derivative f (at p) (axis 1 1))$1 *
      (frechet_derivative f (at p) (axis 2 1))$2 -
    (frechet_derivative f (at p) (axis 1 1))$2 *
      (frechet_derivative f (at p) (axis 2 1))$1"

locale surface_coordinate_change = Y: regular_surface_patch V Y
  for V Y +
  fixes U :: "surface_parameter set"
    and f :: "surface_parameter ⇒ surface_parameter"
  assumes open_source: "open U"
    and smooth_change: "3-smooth_on U f"
    and maps_into: "f ` U ⊆ V"
    and nonsingular: "p ∈ U ⟹ parameter_jacobian f p ≠ 0"
begin

lemma change_differentiable:
  assumes "p ∈ U"
  shows "f differentiable at p"
  using smooth_on_imp_differentiable_on[OF smooth_change] assms open_source
  by (simp add: differentiable_on_eq_differentiable_at)

lemma target_point:
  "p ∈ U ⟹ f p ∈ V"
  using maps_into by auto

lemma derivative_pullback:
  assumes p: "p ∈ U"
  shows "patch_derivative (Y ∘ f) p a =
    patch_derivative Y (f p) (frechet_derivative f (at p) a)"
  unfolding patch_derivative_def
  by (simp add: frechet_derivative_compose[OF change_differentiable[OF p]
    Y.immersion_differentiable[OF target_point[OF p]]])

lemma metric_pullback:
  assumes p: "p ∈ U"
  shows "first_fundamental_form (Y ∘ f) p a b =
    first_fundamental_form Y (f p)
      (frechet_derivative f (at p) a) (frechet_derivative f (at p) b)"
  unfolding first_fundamental_form_def derivative_pullback[OF p] by rule

lemma normal_pullback:
  assumes p: "p ∈ U"
  shows "patch_normal (Y ∘ f) p = parameter_jacobian f p *R patch_normal Y (f p)"
  unfolding patch_normal_def patch_u_def patch_v_def derivative_pullback[OF p]
  unfolding Y.patch_derivative_coordinates[OF target_point[OF p]]
  by (simp add: parameter_jacobian_def cross3_simps forall_3 axis_def algebra_simps)

lemma composite_regular:
  "regular_surface_patch U (Y ∘ f)"
proof
  show "open U" by (rule open_source)
  show "3-smooth_on U (Y ∘ f)"
    by (rule smooth_on_compose[OF Y.c3 smooth_change open_source Y.open_domain maps_into])
  show "⋀p. p ∈ U ⟹ patch_normal (Y ∘ f) p ≠ 0"
    by (simp add: normal_pullback nonsingular Y.regular target_point)
qed

lemma gauss_map_pullback:
  assumes p: "p ∈ U"
  shows "gauss_map (Y ∘ f) p =
    (parameter_jacobian f p / abs (parameter_jacobian f p)) *R gauss_map Y (f p)"
  unfolding gauss_map_def normal_pullback[OF p]
  by (simp add: norm_scaleR scaleR_scaleR inverse_mult_distrib divide_inverse
    mult.commute mult.left_commute)

lemma change_direction_differentiable:
  assumes "p ∈ U"
  shows "(λq. frechet_derivative f (at q) b) differentiable at p"
proof -
  have smooth: "2-smooth_on U (λq. frechet_derivative f (at q) b)"
    using derivative_is_smooth'[where k=2 and S=U and f=f and v=b] smooth_change by simp
  show ?thesis using smooth_on_imp_differentiable_on[OF smooth] assms open_source
    by (simp add: differentiable_on_eq_differentiable_at)
qed

subsection ‹The second derivative and fundamental form›

text ‹The second derivative of the parameter map contributes a tangent
  term. Its inner product with the target normal vanishes, leaving the
  second-form pullback multiplied by the sign of the Jacobian.›

lemma second_derivative_pullback:
  assumes p: "p ∈ U"
  shows "patch_second_derivative (Y ∘ f) p a b =
    patch_second_derivative Y (f p)
      (frechet_derivative f (at p) a) (frechet_derivative f (at p) b) +
    patch_derivative Y (f p)
      (frechet_derivative (λq. frechet_derivative f (at q) b) (at p) a)"
proof -
  let ?F = "frechet_derivative f (at p)"
  let ?H = "frechet_derivative (λq. frechet_derivative f (at q) b) (at p)"
  let ?term = "λi q. (frechet_derivative f (at q) b)$i *R
    patch_derivative Y (f q) (axis i 1)"
  have df: "(f has_derivative ?F) (at p)"
    using change_differentiable[OF p] unfolding frechet_derivative_works .
  have dh: "((λq. frechet_derivative f (at q) b) has_derivative ?H) (at p)"
    using change_direction_differentiable[OF p] unfolding frechet_derivative_works .
  have term_deriv: "(?term i has_derivative (λh.
    (?F b)$i *R patch_second_derivative Y (f p) (?F h) (axis i 1) +
    (?H h)$i *R patch_derivative Y (f p) (axis i 1))) (at p)" for i
  proof -
    have coeff: "((λq. (frechet_derivative f (at q) b)$i) has_derivative
      (λh. (?H h)$i)) (at p)"
      by (rule has_derivative_compose[OF dh],
        rule bounded_linear.has_derivative[OF bounded_linear_vec_nth], simp)
    have dy: "((λq. patch_derivative Y q (axis i 1)) has_derivative
      (λh. patch_second_derivative Y (f p) h (axis i 1))) (at (f p))"
      using Y.patch_direction_differentiable[OF target_point[OF p], of "axis i 1"]
      unfolding patch_second_derivative_def frechet_derivative_works .
    have field: "((λq. patch_derivative Y (f q) (axis i 1)) has_derivative
      (λh. patch_second_derivative Y (f p) (?F h) (axis i 1))) (at p)"
      using diff_chain_at[OF df dy] by (simp add: o_def)
    show ?thesis by (rule bounded_bilinear.FDERIV[OF bounded_bilinear_scaleR coeff field])
  qed
  have expansion: "patch_second_derivative Y (f p) (?F h) (?F b) +
      patch_derivative Y (f p) (?H h) =
    ((?F b)$1 *R patch_second_derivative Y (f p) (?F h) (axis 1 1) +
      (?H h)$1 *R patch_derivative Y (f p) (axis 1 1)) +
    ((?F b)$2 *R patch_second_derivative Y (f p) (?F h) (axis 2 1) +
      (?H h)$2 *R patch_derivative Y (f p) (axis 2 1))" for h
    by (subst Y.second_derivative_coordinates_right[OF target_point[OF p]],
      subst Y.patch_derivative_coordinates[OF target_point[OF p]],
      simp add: patch_u_def patch_v_def add_ac)
  have sum: "((λq. ?term 1 q + ?term 2 q) has_derivative
    (λh. patch_second_derivative Y (f p) (?F h) (?F b) +
      patch_derivative Y (f p) (?H h))) (at p)"
    using has_derivative_add[OF term_deriv[of 1] term_deriv[of 2]]
    unfolding expansion by simp
  have eq: "patch_derivative (Y ∘ f) q b = ?term 1 q + ?term 2 q" if "q ∈ U" for q
    unfolding derivative_pullback[OF that]
    by (rule Y.patch_derivative_coordinates[OF target_point[OF that], unfolded patch_u_def patch_v_def])
  have actual: "((λq. patch_derivative (Y ∘ f) q b) has_derivative
    (λh. patch_second_derivative Y (f p) (?F h) (?F b) +
      patch_derivative Y (f p) (?H h))) (at p)"
    by (rule has_derivative_transform_within_open[OF sum open_source p], simp add: eq)
  show ?thesis unfolding patch_second_derivative_def
    using fun_cong[OF frechet_derivative_at[OF actual], where x=a]
    unfolding patch_second_derivative_def by simp
qed

lemma metric_determinant_pullback:
  assumes p: "p ∈ U"
  shows "norm (patch_normal (Y ∘ f) p) ^ 2 =
    parameter_jacobian f p ^ 2 * norm (patch_normal Y (f p)) ^ 2"
  by (simp add: normal_pullback[OF p] norm_scaleR power_mult_distrib)

lemma second_form_pullback:
  assumes p: "p ∈ U"
  shows "second_fundamental_form (Y ∘ f) p a b =
    (parameter_jacobian f p / abs (parameter_jacobian f p)) *
    second_fundamental_form Y (f p)
      (frechet_derivative f (at p) a) (frechet_derivative f (at p) b)"
  unfolding second_fundamental_form_def[folded patch_second_derivative_def]
    second_derivative_pullback[OF p] gauss_map_pullback[OF p]
  by (simp add: inner_add_left inner_add_right inner_commute
    Y.gauss_map_orthogonal_derivative[OF target_point[OF p]])

lemma normal_orientation_unit:
  assumes p: "p ∈ U"
  shows "(parameter_jacobian f p / abs (parameter_jacobian f p)) ^ 2 = 1"
  using nonsingular[OF p] by (simp add: power_divide)

subsection ‹Curvature under changes of parameters›

text ‹The two determinants acquire the same squared Jacobian factor.
  Its cancellation works for either orientation.›

theorem curvature_reparametrization:
  assumes p: "p ∈ U"
  shows "gaussian_curvature (Y ∘ f) p = gaussian_curvature Y (f p)"
proof -
  let ?B = "λZ q. second_fundamental_form Z q (axis 1 1) (axis 1 1) *
    second_fundamental_form Z q (axis 2 1) (axis 2 1) -
    second_fundamental_form Z q (axis 1 1) (axis 2 1) *
    second_fundamental_form Z q (axis 2 1) (axis 1 1)"
  let ?j = "parameter_jacobian f p"
  let ?s = "?j / abs ?j"
  let ?u = "frechet_derivative f (at p) (axis 1 1)"
  let ?v = "frechet_derivative f (at p) (axis 2 1)"
  have numerator: "?B (Y ∘ f) p = ?j^2 * ?B Y (f p)"
  proof -
    have "?B (Y ∘ f) p = ?s^2 *
      (second_fundamental_form Y (f p) ?u ?u * second_fundamental_form Y (f p) ?v ?v -
       second_fundamental_form Y (f p) ?u ?v * second_fundamental_form Y (f p) ?v ?u)"
      unfolding second_form_pullback[OF p]
      by (simp add: algebra_simps power2_eq_square)
    also have "... = ?j^2 * ?B Y (f p)"
      unfolding Y.second_form_gram_determinant[OF target_point[OF p], where u="?u" and v="?v"]
        normal_orientation_unit[OF p]
      by (simp add: parameter_jacobian_def)
    finally show ?thesis .
  qed
  show ?thesis
    unfolding gaussian_curvature_def metric_determinant_normal numerator
      metric_determinant_pullback[OF p]
    using nonsingular[OF p] by simp
qed

end

section ‹Theorema Egregium›

text ‹Metric preservation already forces the parameter derivative to be
injective. Thus nonsingularity is a consequence, not an extra isometry premise.›

locale surface_local_isometry = X: regular_surface_patch U X +
  Y: regular_surface_patch V Y
  for U X V Y +
  fixes f :: "surface_parameter ⇒ surface_parameter"
  assumes smooth_map: "3-smooth_on U f"
    and maps_into: "f ` U ⊆ V"
    and preserves_metric: "⋀p a b. p ∈ U ⟹
    first_fundamental_form X p a b = first_fundamental_form Y (f p)
      (frechet_derivative f (at p) a) (frechet_derivative f (at p) b)"
begin

lemma map_differentiable:
  "p ∈ U ⟹ f differentiable at p"
  using smooth_on_imp_differentiable_on[OF smooth_map] X.open_domain
  by (simp add: differentiable_on_eq_differentiable_at)

lemma map_derivative_injective:
  assumes p: "p ∈ U"
  shows "inj (frechet_derivative f (at p))"
proof (rule injI)
  fix a b
  assume eq: "frechet_derivative f (at p) a = frechet_derivative f (at p) b"
  interpret D: bounded_linear "frechet_derivative f (at p)"
    using map_differentiable[OF p] unfolding frechet_derivative_works
    by (rule has_derivative_bounded_linear)
  have zero: "frechet_derivative f (at p) (a - b) = 0"
    by (simp add: D.diff eq)
  have fp: "f p ∈ V" using maps_into p by auto
  have "first_fundamental_form X p (a - b) (a - b) = 0"
    using preserves_metric[OF p, of "a-b" "a-b"]
    by (simp add: zero first_fundamental_form_def linear_0[OF Y.patch_derivative_linear[OF fp]])
  then show "a = b" using X.metric_positive_definite[OF p, of "a-b"] by auto
qed

lemma map_nonsingular:
  assumes p: "p ∈ U"
  shows "parameter_jacobian f p ≠ 0"
proof -
  have lin: "linear (frechet_derivative f (at p))"
    using map_differentiable[OF p] unfolding frechet_derivative_works
    by (meson bounded_linear.linear has_derivative_bounded_linear)
  have "matrix_det (matrix (frechet_derivative f (at p))) ≠ 0"
    using det_nz_iff_inj[OF lin] map_derivative_injective[OF p] by blast
  then show ?thesis by (simp add: parameter_jacobian_def det_2 matrix_def mult.commute)
qed

sublocale change: surface_coordinate_change V Y U f
  by unfold_locales (rule X.open_domain, rule smooth_map, rule maps_into, rule map_nonsingular)

text ‹Pull the target immersion back to the source parameter domain.
  Metric preservation identifies its metric with the source metric, so the
  metric-determination theorem applies. Curvature reparametrization then
  compares the source point with its image under the isometry.›

theorem theorema_egregium:
  assumes p: "p ∈ U"
  shows "gaussian_curvature X p = gaussian_curvature Y (f p)"
proof -
  interpret equal: same_surface_metric U X "Y ∘ f"
    using X.regular_surface_patch_axioms change.composite_regular
      preserves_metric change.metric_pullback
    by (unfold_locales, auto simp: regular_surface_patch_def)
  show ?thesis using equal.gaussian_curvature_equal[OF p]
    change.curvature_reparametrization[OF p] by simp
qed

end
end