src/HOL/Library/Product_Vector.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 62367 d2bc8a7e5fec
child 63040 eb4ddd18d635
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
     1 (*  Title:      HOL/Library/Product_Vector.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 section \<open>Cartesian Products as Vector Spaces\<close>
     6 
     7 theory Product_Vector
     8 imports Inner_Product Product_plus
     9 begin
    10 
    11 subsection \<open>Product is a real vector space\<close>
    12 
    13 instantiation prod :: (real_vector, real_vector) real_vector
    14 begin
    15 
    16 definition scaleR_prod_def:
    17   "scaleR r A = (scaleR r (fst A), scaleR r (snd A))"
    18 
    19 lemma fst_scaleR [simp]: "fst (scaleR r A) = scaleR r (fst A)"
    20   unfolding scaleR_prod_def by simp
    21 
    22 lemma snd_scaleR [simp]: "snd (scaleR r A) = scaleR r (snd A)"
    23   unfolding scaleR_prod_def by simp
    24 
    25 lemma scaleR_Pair [simp]: "scaleR r (a, b) = (scaleR r a, scaleR r b)"
    26   unfolding scaleR_prod_def by simp
    27 
    28 instance
    29 proof
    30   fix a b :: real and x y :: "'a \<times> 'b"
    31   show "scaleR a (x + y) = scaleR a x + scaleR a y"
    32     by (simp add: prod_eq_iff scaleR_right_distrib)
    33   show "scaleR (a + b) x = scaleR a x + scaleR b x"
    34     by (simp add: prod_eq_iff scaleR_left_distrib)
    35   show "scaleR a (scaleR b x) = scaleR (a * b) x"
    36     by (simp add: prod_eq_iff)
    37   show "scaleR 1 x = x"
    38     by (simp add: prod_eq_iff)
    39 qed
    40 
    41 end
    42 
    43 subsection \<open>Product is a metric space\<close>
    44 
    45 (* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
    46 
    47 instantiation prod :: (metric_space, metric_space) dist
    48 begin
    49 
    50 definition dist_prod_def[code del]:
    51   "dist x y = sqrt ((dist (fst x) (fst y))\<^sup>2 + (dist (snd x) (snd y))\<^sup>2)"
    52 
    53 instance ..
    54 end
    55 
    56 instantiation prod :: (metric_space, metric_space) uniformity_dist
    57 begin
    58 
    59 definition [code del]:
    60   "(uniformity :: (('a \<times> 'b) \<times> ('a \<times> 'b)) filter) =
    61     (INF e:{0 <..}. principal {(x, y). dist x y < e})"
    62 
    63 instance
    64   by standard (rule uniformity_prod_def)
    65 end
    66 
    67 declare uniformity_Abort[where 'a="'a :: metric_space \<times> 'b :: metric_space", code]
    68 
    69 instantiation prod :: (metric_space, metric_space) metric_space
    70 begin
    71 
    72 lemma dist_Pair_Pair: "dist (a, b) (c, d) = sqrt ((dist a c)\<^sup>2 + (dist b d)\<^sup>2)"
    73   unfolding dist_prod_def by simp
    74 
    75 lemma dist_fst_le: "dist (fst x) (fst y) \<le> dist x y"
    76   unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge1)
    77 
    78 lemma dist_snd_le: "dist (snd x) (snd y) \<le> dist x y"
    79   unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge2)
    80 
    81 instance
    82 proof
    83   fix x y :: "'a \<times> 'b"
    84   show "dist x y = 0 \<longleftrightarrow> x = y"
    85     unfolding dist_prod_def prod_eq_iff by simp
    86 next
    87   fix x y z :: "'a \<times> 'b"
    88   show "dist x y \<le> dist x z + dist y z"
    89     unfolding dist_prod_def
    90     by (intro order_trans [OF _ real_sqrt_sum_squares_triangle_ineq]
    91         real_sqrt_le_mono add_mono power_mono dist_triangle2 zero_le_dist)
    92 next
    93   fix S :: "('a \<times> 'b) set"
    94   have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
    95   proof
    96     assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
    97     proof
    98       fix x assume "x \<in> S"
    99       obtain A B where "open A" "open B" "x \<in> A \<times> B" "A \<times> B \<subseteq> S"
   100         using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
   101       obtain r where r: "0 < r" "\<forall>y. dist y (fst x) < r \<longrightarrow> y \<in> A"
   102         using \<open>open A\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
   103       obtain s where s: "0 < s" "\<forall>y. dist y (snd x) < s \<longrightarrow> y \<in> B"
   104         using \<open>open B\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
   105       let ?e = "min r s"
   106       have "0 < ?e \<and> (\<forall>y. dist y x < ?e \<longrightarrow> y \<in> S)"
   107       proof (intro allI impI conjI)
   108         show "0 < min r s" by (simp add: r(1) s(1))
   109       next
   110         fix y assume "dist y x < min r s"
   111         hence "dist y x < r" and "dist y x < s"
   112           by simp_all
   113         hence "dist (fst y) (fst x) < r" and "dist (snd y) (snd x) < s"
   114           by (auto intro: le_less_trans dist_fst_le dist_snd_le)
   115         hence "fst y \<in> A" and "snd y \<in> B"
   116           by (simp_all add: r(2) s(2))
   117         hence "y \<in> A \<times> B" by (induct y, simp)
   118         with \<open>A \<times> B \<subseteq> S\<close> show "y \<in> S" ..
   119       qed
   120       thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
   121     qed
   122   next
   123     assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
   124     proof (rule open_prod_intro)
   125       fix x assume "x \<in> S"
   126       then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   127         using * by fast
   128       def r \<equiv> "e / sqrt 2" and s \<equiv> "e / sqrt 2"
   129       from \<open>0 < e\<close> have "0 < r" and "0 < s"
   130         unfolding r_def s_def by simp_all
   131       from \<open>0 < e\<close> have "e = sqrt (r\<^sup>2 + s\<^sup>2)"
   132         unfolding r_def s_def by (simp add: power_divide)
   133       def A \<equiv> "{y. dist (fst x) y < r}" and B \<equiv> "{y. dist (snd x) y < s}"
   134       have "open A" and "open B"
   135         unfolding A_def B_def by (simp_all add: open_ball)
   136       moreover have "x \<in> A \<times> B"
   137         unfolding A_def B_def mem_Times_iff
   138         using \<open>0 < r\<close> and \<open>0 < s\<close> by simp
   139       moreover have "A \<times> B \<subseteq> S"
   140       proof (clarify)
   141         fix a b assume "a \<in> A" and "b \<in> B"
   142         hence "dist a (fst x) < r" and "dist b (snd x) < s"
   143           unfolding A_def B_def by (simp_all add: dist_commute)
   144         hence "dist (a, b) x < e"
   145           unfolding dist_prod_def \<open>e = sqrt (r\<^sup>2 + s\<^sup>2)\<close>
   146           by (simp add: add_strict_mono power_strict_mono)
   147         thus "(a, b) \<in> S"
   148           by (simp add: S)
   149       qed
   150       ultimately show "\<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S" by fast
   151     qed
   152   qed
   153   show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   154     unfolding * eventually_uniformity_metric
   155     by (simp del: split_paired_All add: dist_prod_def dist_commute)
   156 qed
   157 
   158 end
   159 
   160 declare [[code abort: "dist::('a::metric_space*'b::metric_space)\<Rightarrow>('a*'b) \<Rightarrow> real"]]
   161 
   162 lemma Cauchy_fst: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. fst (X n))"
   163   unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_fst_le])
   164 
   165 lemma Cauchy_snd: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. snd (X n))"
   166   unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_snd_le])
   167 
   168 lemma Cauchy_Pair:
   169   assumes "Cauchy X" and "Cauchy Y"
   170   shows "Cauchy (\<lambda>n. (X n, Y n))"
   171 proof (rule metric_CauchyI)
   172   fix r :: real assume "0 < r"
   173   hence "0 < r / sqrt 2" (is "0 < ?s") by simp
   174   obtain M where M: "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < ?s"
   175     using metric_CauchyD [OF \<open>Cauchy X\<close> \<open>0 < ?s\<close>] ..
   176   obtain N where N: "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (Y m) (Y n) < ?s"
   177     using metric_CauchyD [OF \<open>Cauchy Y\<close> \<open>0 < ?s\<close>] ..
   178   have "\<forall>m\<ge>max M N. \<forall>n\<ge>max M N. dist (X m, Y m) (X n, Y n) < r"
   179     using M N by (simp add: real_sqrt_sum_squares_less dist_Pair_Pair)
   180   then show "\<exists>n0. \<forall>m\<ge>n0. \<forall>n\<ge>n0. dist (X m, Y m) (X n, Y n) < r" ..
   181 qed
   182 
   183 subsection \<open>Product is a complete metric space\<close>
   184 
   185 instance prod :: (complete_space, complete_space) complete_space
   186 proof
   187   fix X :: "nat \<Rightarrow> 'a \<times> 'b" assume "Cauchy X"
   188   have 1: "(\<lambda>n. fst (X n)) \<longlonglongrightarrow> lim (\<lambda>n. fst (X n))"
   189     using Cauchy_fst [OF \<open>Cauchy X\<close>]
   190     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   191   have 2: "(\<lambda>n. snd (X n)) \<longlonglongrightarrow> lim (\<lambda>n. snd (X n))"
   192     using Cauchy_snd [OF \<open>Cauchy X\<close>]
   193     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   194   have "X \<longlonglongrightarrow> (lim (\<lambda>n. fst (X n)), lim (\<lambda>n. snd (X n)))"
   195     using tendsto_Pair [OF 1 2] by simp
   196   then show "convergent X"
   197     by (rule convergentI)
   198 qed
   199 
   200 subsection \<open>Product is a normed vector space\<close>
   201 
   202 instantiation prod :: (real_normed_vector, real_normed_vector) real_normed_vector
   203 begin
   204 
   205 definition norm_prod_def[code del]:
   206   "norm x = sqrt ((norm (fst x))\<^sup>2 + (norm (snd x))\<^sup>2)"
   207 
   208 definition sgn_prod_def:
   209   "sgn (x::'a \<times> 'b) = scaleR (inverse (norm x)) x"
   210 
   211 lemma norm_Pair: "norm (a, b) = sqrt ((norm a)\<^sup>2 + (norm b)\<^sup>2)"
   212   unfolding norm_prod_def by simp
   213 
   214 instance
   215 proof
   216   fix r :: real and x y :: "'a \<times> 'b"
   217   show "norm x = 0 \<longleftrightarrow> x = 0"
   218     unfolding norm_prod_def
   219     by (simp add: prod_eq_iff)
   220   show "norm (x + y) \<le> norm x + norm y"
   221     unfolding norm_prod_def
   222     apply (rule order_trans [OF _ real_sqrt_sum_squares_triangle_ineq])
   223     apply (simp add: add_mono power_mono norm_triangle_ineq)
   224     done
   225   show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
   226     unfolding norm_prod_def
   227     apply (simp add: power_mult_distrib)
   228     apply (simp add: distrib_left [symmetric])
   229     apply (simp add: real_sqrt_mult_distrib)
   230     done
   231   show "sgn x = scaleR (inverse (norm x)) x"
   232     by (rule sgn_prod_def)
   233   show "dist x y = norm (x - y)"
   234     unfolding dist_prod_def norm_prod_def
   235     by (simp add: dist_norm)
   236 qed
   237 
   238 end
   239 
   240 declare [[code abort: "norm::('a::real_normed_vector*'b::real_normed_vector) \<Rightarrow> real"]]
   241 
   242 instance prod :: (banach, banach) banach ..
   243 
   244 subsubsection \<open>Pair operations are linear\<close>
   245 
   246 lemma bounded_linear_fst: "bounded_linear fst"
   247   using fst_add fst_scaleR
   248   by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
   249 
   250 lemma bounded_linear_snd: "bounded_linear snd"
   251   using snd_add snd_scaleR
   252   by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
   253 
   254 lemmas bounded_linear_fst_comp = bounded_linear_fst[THEN bounded_linear_compose]
   255 
   256 lemmas bounded_linear_snd_comp = bounded_linear_snd[THEN bounded_linear_compose]
   257 
   258 lemma bounded_linear_Pair:
   259   assumes f: "bounded_linear f"
   260   assumes g: "bounded_linear g"
   261   shows "bounded_linear (\<lambda>x. (f x, g x))"
   262 proof
   263   interpret f: bounded_linear f by fact
   264   interpret g: bounded_linear g by fact
   265   fix x y and r :: real
   266   show "(f (x + y), g (x + y)) = (f x, g x) + (f y, g y)"
   267     by (simp add: f.add g.add)
   268   show "(f (r *\<^sub>R x), g (r *\<^sub>R x)) = r *\<^sub>R (f x, g x)"
   269     by (simp add: f.scaleR g.scaleR)
   270   obtain Kf where "0 < Kf" and norm_f: "\<And>x. norm (f x) \<le> norm x * Kf"
   271     using f.pos_bounded by fast
   272   obtain Kg where "0 < Kg" and norm_g: "\<And>x. norm (g x) \<le> norm x * Kg"
   273     using g.pos_bounded by fast
   274   have "\<forall>x. norm (f x, g x) \<le> norm x * (Kf + Kg)"
   275     apply (rule allI)
   276     apply (simp add: norm_Pair)
   277     apply (rule order_trans [OF sqrt_add_le_add_sqrt], simp, simp)
   278     apply (simp add: distrib_left)
   279     apply (rule add_mono [OF norm_f norm_g])
   280     done
   281   then show "\<exists>K. \<forall>x. norm (f x, g x) \<le> norm x * K" ..
   282 qed
   283 
   284 subsubsection \<open>Frechet derivatives involving pairs\<close>
   285 
   286 lemma has_derivative_Pair [derivative_intros]:
   287   assumes f: "(f has_derivative f') (at x within s)" and g: "(g has_derivative g') (at x within s)"
   288   shows "((\<lambda>x. (f x, g x)) has_derivative (\<lambda>h. (f' h, g' h))) (at x within s)"
   289 proof (rule has_derivativeI_sandwich[of 1])
   290   show "bounded_linear (\<lambda>h. (f' h, g' h))"
   291     using f g by (intro bounded_linear_Pair has_derivative_bounded_linear)
   292   let ?Rf = "\<lambda>y. f y - f x - f' (y - x)"
   293   let ?Rg = "\<lambda>y. g y - g x - g' (y - x)"
   294   let ?R = "\<lambda>y. ((f y, g y) - (f x, g x) - (f' (y - x), g' (y - x)))"
   295 
   296   show "((\<lambda>y. norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)) \<longlongrightarrow> 0) (at x within s)"
   297     using f g by (intro tendsto_add_zero) (auto simp: has_derivative_iff_norm)
   298 
   299   fix y :: 'a assume "y \<noteq> x"
   300   show "norm (?R y) / norm (y - x) \<le> norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)"
   301     unfolding add_divide_distrib [symmetric]
   302     by (simp add: norm_Pair divide_right_mono order_trans [OF sqrt_add_le_add_sqrt])
   303 qed simp
   304 
   305 lemmas has_derivative_fst [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_fst]
   306 lemmas has_derivative_snd [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_snd]
   307 
   308 lemma has_derivative_split [derivative_intros]:
   309   "((\<lambda>p. f (fst p) (snd p)) has_derivative f') F \<Longrightarrow> ((\<lambda>(a, b). f a b) has_derivative f') F"
   310   unfolding split_beta' .
   311 
   312 subsection \<open>Product is an inner product space\<close>
   313 
   314 instantiation prod :: (real_inner, real_inner) real_inner
   315 begin
   316 
   317 definition inner_prod_def:
   318   "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
   319 
   320 lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
   321   unfolding inner_prod_def by simp
   322 
   323 instance
   324 proof
   325   fix r :: real
   326   fix x y z :: "'a::real_inner \<times> 'b::real_inner"
   327   show "inner x y = inner y x"
   328     unfolding inner_prod_def
   329     by (simp add: inner_commute)
   330   show "inner (x + y) z = inner x z + inner y z"
   331     unfolding inner_prod_def
   332     by (simp add: inner_add_left)
   333   show "inner (scaleR r x) y = r * inner x y"
   334     unfolding inner_prod_def
   335     by (simp add: distrib_left)
   336   show "0 \<le> inner x x"
   337     unfolding inner_prod_def
   338     by (intro add_nonneg_nonneg inner_ge_zero)
   339   show "inner x x = 0 \<longleftrightarrow> x = 0"
   340     unfolding inner_prod_def prod_eq_iff
   341     by (simp add: add_nonneg_eq_0_iff)
   342   show "norm x = sqrt (inner x x)"
   343     unfolding norm_prod_def inner_prod_def
   344     by (simp add: power2_norm_eq_inner)
   345 qed
   346 
   347 end
   348 
   349 lemma inner_Pair_0: "inner x (0, b) = inner (snd x) b" "inner x (a, 0) = inner (fst x) a"
   350     by (cases x, simp)+
   351 
   352 lemma
   353   fixes x :: "'a::real_normed_vector"
   354   shows norm_Pair1 [simp]: "norm (0,x) = norm x"
   355     and norm_Pair2 [simp]: "norm (x,0) = norm x"
   356 by (auto simp: norm_Pair)
   357 
   358 lemma norm_commute: "norm (x,y) = norm (y,x)"
   359   by (simp add: norm_Pair)
   360 
   361 lemma norm_fst_le: "norm x \<le> norm (x,y)"
   362   by (metis dist_fst_le fst_conv fst_zero norm_conv_dist)
   363 
   364 lemma norm_snd_le: "norm y \<le> norm (x,y)"
   365   by (metis dist_snd_le snd_conv snd_zero norm_conv_dist)
   366 
   367 end