src/HOL/Library/Product_Vector.thy
author huffman
Thu Jun 11 09:03:24 2009 -0700 (2009-06-11)
changeset 31563 ded2364d14d4
parent 31562 10d0fb526643
child 31565 da5a5589418e
permissions -rw-r--r--
cleaned up some proofs
     1 (*  Title:      HOL/Library/Product_Vector.thy
     2     Author:     Brian Huffman
     3 *)
     4 
     5 header {* Cartesian Products as Vector Spaces *}
     6 
     7 theory Product_Vector
     8 imports Inner_Product Product_plus
     9 begin
    10 
    11 subsection {* Product is a real vector space *}
    12 
    13 instantiation "*" :: (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 proof
    29   fix a b :: real and x y :: "'a \<times> 'b"
    30   show "scaleR a (x + y) = scaleR a x + scaleR a y"
    31     by (simp add: expand_prod_eq scaleR_right_distrib)
    32   show "scaleR (a + b) x = scaleR a x + scaleR b x"
    33     by (simp add: expand_prod_eq scaleR_left_distrib)
    34   show "scaleR a (scaleR b x) = scaleR (a * b) x"
    35     by (simp add: expand_prod_eq)
    36   show "scaleR 1 x = x"
    37     by (simp add: expand_prod_eq)
    38 qed
    39 
    40 end
    41 
    42 subsection {* Product is a topological space *}
    43 
    44 instantiation
    45   "*" :: (topological_space, topological_space) topological_space
    46 begin
    47 
    48 definition open_prod_def:
    49   "open (S :: ('a \<times> 'b) set) \<longleftrightarrow>
    50     (\<forall>x\<in>S. \<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S)"
    51 
    52 instance proof
    53   show "open (UNIV :: ('a \<times> 'b) set)"
    54     unfolding open_prod_def by auto
    55 next
    56   fix S T :: "('a \<times> 'b) set"
    57   assume "open S" "open T" thus "open (S \<inter> T)"
    58     unfolding open_prod_def
    59     apply clarify
    60     apply (drule (1) bspec)+
    61     apply (clarify, rename_tac Sa Ta Sb Tb)
    62     apply (rule_tac x="Sa \<inter> Ta" in exI)
    63     apply (rule_tac x="Sb \<inter> Tb" in exI)
    64     apply (simp add: open_Int)
    65     apply fast
    66     done
    67 next
    68   fix K :: "('a \<times> 'b) set set"
    69   assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
    70     unfolding open_prod_def by fast
    71 qed
    72 
    73 end
    74 
    75 lemma open_Times: "open S \<Longrightarrow> open T \<Longrightarrow> open (S \<times> T)"
    76 unfolding open_prod_def by auto
    77 
    78 lemma fst_vimage_eq_Times: "fst -` S = S \<times> UNIV"
    79 by auto
    80 
    81 lemma snd_vimage_eq_Times: "snd -` S = UNIV \<times> S"
    82 by auto
    83 
    84 lemma open_vimage_fst: "open S \<Longrightarrow> open (fst -` S)"
    85 by (simp add: fst_vimage_eq_Times open_Times)
    86 
    87 lemma open_vimage_snd: "open S \<Longrightarrow> open (snd -` S)"
    88 by (simp add: snd_vimage_eq_Times open_Times)
    89 
    90 subsection {* Product is a metric space *}
    91 
    92 instantiation
    93   "*" :: (metric_space, metric_space) metric_space
    94 begin
    95 
    96 definition dist_prod_def:
    97   "dist (x::'a \<times> 'b) y = sqrt ((dist (fst x) (fst y))\<twosuperior> + (dist (snd x) (snd y))\<twosuperior>)"
    98 
    99 lemma dist_Pair_Pair: "dist (a, b) (c, d) = sqrt ((dist a c)\<twosuperior> + (dist b d)\<twosuperior>)"
   100   unfolding dist_prod_def by simp
   101 
   102 instance proof
   103   fix x y :: "'a \<times> 'b"
   104   show "dist x y = 0 \<longleftrightarrow> x = y"
   105     unfolding dist_prod_def expand_prod_eq by simp
   106 next
   107   fix x y z :: "'a \<times> 'b"
   108   show "dist x y \<le> dist x z + dist y z"
   109     unfolding dist_prod_def
   110     by (intro order_trans [OF _ real_sqrt_sum_squares_triangle_ineq]
   111         real_sqrt_le_mono add_mono power_mono dist_triangle2 zero_le_dist)
   112 next
   113   (* FIXME: long proof! *)
   114   (* Maybe it would be easier to define topological spaces *)
   115   (* in terms of neighborhoods instead of open sets? *)
   116   fix S :: "('a \<times> 'b) set"
   117   show "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   118   proof
   119     assume "open S" thus "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   120     unfolding open_prod_def open_dist
   121     apply safe
   122     apply (drule (1) bspec)
   123     apply clarify
   124     apply (drule (1) bspec)+
   125     apply (clarify, rename_tac r s)
   126     apply (rule_tac x="min r s" in exI, simp)
   127     apply (clarify, rename_tac c d)
   128     apply (erule subsetD)
   129     apply (simp add: dist_Pair_Pair)
   130     apply (rule conjI)
   131     apply (drule spec, erule mp)
   132     apply (erule le_less_trans [OF real_sqrt_sum_squares_ge1])
   133     apply (drule spec, erule mp)
   134     apply (erule le_less_trans [OF real_sqrt_sum_squares_ge2])
   135     done
   136   next
   137     assume "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" thus "open S"
   138     unfolding open_prod_def open_dist
   139     apply safe
   140     apply (drule (1) bspec)
   141     apply clarify
   142     apply (subgoal_tac "\<exists>r>0. \<exists>s>0. e = sqrt (r\<twosuperior> + s\<twosuperior>)")
   143     apply clarify
   144     apply (rule_tac x="{y. dist y a < r}" in exI)
   145     apply (rule_tac x="{y. dist y b < s}" in exI)
   146     apply (rule conjI)
   147     apply clarify
   148     apply (rule_tac x="r - dist x a" in exI, rule conjI, simp)
   149     apply clarify
   150     apply (simp add: less_diff_eq)
   151     apply (erule le_less_trans [OF dist_triangle])
   152     apply (rule conjI)
   153     apply clarify
   154     apply (rule_tac x="s - dist x b" in exI, rule conjI, simp)
   155     apply clarify
   156     apply (simp add: less_diff_eq)
   157     apply (erule le_less_trans [OF dist_triangle])
   158     apply (rule conjI)
   159     apply simp
   160     apply (clarify, rename_tac c d)
   161     apply (drule spec, erule mp)
   162     apply (simp add: dist_Pair_Pair add_strict_mono power_strict_mono)
   163     apply (rule_tac x="e / sqrt 2" in exI, simp add: divide_pos_pos)
   164     apply (rule_tac x="e / sqrt 2" in exI, simp add: divide_pos_pos)
   165     apply (simp add: power_divide)
   166     done
   167   qed
   168 qed
   169 
   170 end
   171 
   172 subsection {* Continuity of operations *}
   173 
   174 lemma dist_fst_le: "dist (fst x) (fst y) \<le> dist x y"
   175 unfolding dist_prod_def by simp
   176 
   177 lemma dist_snd_le: "dist (snd x) (snd y) \<le> dist x y"
   178 unfolding dist_prod_def by simp
   179 
   180 lemma tendsto_fst:
   181   assumes "(f ---> a) net"
   182   shows "((\<lambda>x. fst (f x)) ---> fst a) net"
   183 proof (rule topological_tendstoI)
   184   fix S assume "open S" "fst a \<in> S"
   185   then have "open (fst -` S)" "a \<in> fst -` S"
   186     unfolding open_prod_def
   187     apply simp_all
   188     apply clarify
   189     apply (rule exI, erule conjI)
   190     apply (rule exI, rule conjI [OF open_UNIV])
   191     apply auto
   192     done
   193   with assms have "eventually (\<lambda>x. f x \<in> fst -` S) net"
   194     by (rule topological_tendstoD)
   195   then show "eventually (\<lambda>x. fst (f x) \<in> S) net"
   196     by simp
   197 qed
   198 
   199 lemma tendsto_snd:
   200   assumes "(f ---> a) net"
   201   shows "((\<lambda>x. snd (f x)) ---> snd a) net"
   202 proof (rule topological_tendstoI)
   203   fix S assume "open S" "snd a \<in> S"
   204   then have "open (snd -` S)" "a \<in> snd -` S"
   205     unfolding open_prod_def
   206     apply simp_all
   207     apply clarify
   208     apply (rule exI, rule conjI [OF open_UNIV])
   209     apply (rule exI, erule conjI)
   210     apply auto
   211     done
   212   with assms have "eventually (\<lambda>x. f x \<in> snd -` S) net"
   213     by (rule topological_tendstoD)
   214   then show "eventually (\<lambda>x. snd (f x) \<in> S) net"
   215     by simp
   216 qed
   217 
   218 lemma tendsto_Pair:
   219   assumes "(f ---> a) net" and "(g ---> b) net"
   220   shows "((\<lambda>x. (f x, g x)) ---> (a, b)) net"
   221 proof (rule topological_tendstoI)
   222   fix S assume "open S" "(a, b) \<in> S"
   223   then obtain A B where "open A" "open B" "a \<in> A" "b \<in> B" "A \<times> B \<subseteq> S"
   224     unfolding open_prod_def by auto
   225   have "eventually (\<lambda>x. f x \<in> A) net"
   226     using `(f ---> a) net` `open A` `a \<in> A`
   227     by (rule topological_tendstoD)
   228   moreover
   229   have "eventually (\<lambda>x. g x \<in> B) net"
   230     using `(g ---> b) net` `open B` `b \<in> B`
   231     by (rule topological_tendstoD)
   232   ultimately
   233   show "eventually (\<lambda>x. (f x, g x) \<in> S) net"
   234     by (rule eventually_elim2)
   235        (simp add: subsetD [OF `A \<times> B \<subseteq> S`])
   236 qed
   237 
   238 lemma LIMSEQ_fst: "(X ----> a) \<Longrightarrow> (\<lambda>n. fst (X n)) ----> fst a"
   239 unfolding LIMSEQ_conv_tendsto by (rule tendsto_fst)
   240 
   241 lemma LIMSEQ_snd: "(X ----> a) \<Longrightarrow> (\<lambda>n. snd (X n)) ----> snd a"
   242 unfolding LIMSEQ_conv_tendsto by (rule tendsto_snd)
   243 
   244 lemma LIMSEQ_Pair:
   245   assumes "X ----> a" and "Y ----> b"
   246   shows "(\<lambda>n. (X n, Y n)) ----> (a, b)"
   247 using assms unfolding LIMSEQ_conv_tendsto
   248 by (rule tendsto_Pair)
   249 
   250 lemma LIM_fst: "f -- x --> a \<Longrightarrow> (\<lambda>x. fst (f x)) -- x --> fst a"
   251 unfolding LIM_conv_tendsto by (rule tendsto_fst)
   252 
   253 lemma LIM_snd: "f -- x --> a \<Longrightarrow> (\<lambda>x. snd (f x)) -- x --> snd a"
   254 unfolding LIM_conv_tendsto by (rule tendsto_snd)
   255 
   256 lemma LIM_Pair:
   257   assumes "f -- x --> a" and "g -- x --> b"
   258   shows "(\<lambda>x. (f x, g x)) -- x --> (a, b)"
   259 using assms unfolding LIM_conv_tendsto
   260 by (rule tendsto_Pair)
   261 
   262 lemma Cauchy_fst: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. fst (X n))"
   263 unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_fst_le])
   264 
   265 lemma Cauchy_snd: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. snd (X n))"
   266 unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_snd_le])
   267 
   268 lemma Cauchy_Pair:
   269   assumes "Cauchy X" and "Cauchy Y"
   270   shows "Cauchy (\<lambda>n. (X n, Y n))"
   271 proof (rule metric_CauchyI)
   272   fix r :: real assume "0 < r"
   273   then have "0 < r / sqrt 2" (is "0 < ?s")
   274     by (simp add: divide_pos_pos)
   275   obtain M where M: "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < ?s"
   276     using metric_CauchyD [OF `Cauchy X` `0 < ?s`] ..
   277   obtain N where N: "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (Y m) (Y n) < ?s"
   278     using metric_CauchyD [OF `Cauchy Y` `0 < ?s`] ..
   279   have "\<forall>m\<ge>max M N. \<forall>n\<ge>max M N. dist (X m, Y m) (X n, Y n) < r"
   280     using M N by (simp add: real_sqrt_sum_squares_less dist_Pair_Pair)
   281   then show "\<exists>n0. \<forall>m\<ge>n0. \<forall>n\<ge>n0. dist (X m, Y m) (X n, Y n) < r" ..
   282 qed
   283 
   284 lemma isCont_Pair [simp]:
   285   "\<lbrakk>isCont f x; isCont g x\<rbrakk> \<Longrightarrow> isCont (\<lambda>x. (f x, g x)) x"
   286   unfolding isCont_def by (rule LIM_Pair)
   287 
   288 subsection {* Product is a complete metric space *}
   289 
   290 instance "*" :: (complete_space, complete_space) complete_space
   291 proof
   292   fix X :: "nat \<Rightarrow> 'a \<times> 'b" assume "Cauchy X"
   293   have 1: "(\<lambda>n. fst (X n)) ----> lim (\<lambda>n. fst (X n))"
   294     using Cauchy_fst [OF `Cauchy X`]
   295     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   296   have 2: "(\<lambda>n. snd (X n)) ----> lim (\<lambda>n. snd (X n))"
   297     using Cauchy_snd [OF `Cauchy X`]
   298     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   299   have "X ----> (lim (\<lambda>n. fst (X n)), lim (\<lambda>n. snd (X n)))"
   300     using LIMSEQ_Pair [OF 1 2] by simp
   301   then show "convergent X"
   302     by (rule convergentI)
   303 qed
   304 
   305 subsection {* Product is a normed vector space *}
   306 
   307 instantiation
   308   "*" :: (real_normed_vector, real_normed_vector) real_normed_vector
   309 begin
   310 
   311 definition norm_prod_def:
   312   "norm x = sqrt ((norm (fst x))\<twosuperior> + (norm (snd x))\<twosuperior>)"
   313 
   314 definition sgn_prod_def:
   315   "sgn (x::'a \<times> 'b) = scaleR (inverse (norm x)) x"
   316 
   317 lemma norm_Pair: "norm (a, b) = sqrt ((norm a)\<twosuperior> + (norm b)\<twosuperior>)"
   318   unfolding norm_prod_def by simp
   319 
   320 instance proof
   321   fix r :: real and x y :: "'a \<times> 'b"
   322   show "0 \<le> norm x"
   323     unfolding norm_prod_def by simp
   324   show "norm x = 0 \<longleftrightarrow> x = 0"
   325     unfolding norm_prod_def
   326     by (simp add: expand_prod_eq)
   327   show "norm (x + y) \<le> norm x + norm y"
   328     unfolding norm_prod_def
   329     apply (rule order_trans [OF _ real_sqrt_sum_squares_triangle_ineq])
   330     apply (simp add: add_mono power_mono norm_triangle_ineq)
   331     done
   332   show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
   333     unfolding norm_prod_def
   334     apply (simp add: norm_scaleR power_mult_distrib)
   335     apply (simp add: right_distrib [symmetric])
   336     apply (simp add: real_sqrt_mult_distrib)
   337     done
   338   show "sgn x = scaleR (inverse (norm x)) x"
   339     by (rule sgn_prod_def)
   340   show "dist x y = norm (x - y)"
   341     unfolding dist_prod_def norm_prod_def
   342     by (simp add: dist_norm)
   343 qed
   344 
   345 end
   346 
   347 instance "*" :: (banach, banach) banach ..
   348 
   349 subsection {* Product is an inner product space *}
   350 
   351 instantiation "*" :: (real_inner, real_inner) real_inner
   352 begin
   353 
   354 definition inner_prod_def:
   355   "inner x y = inner (fst x) (fst y) + inner (snd x) (snd y)"
   356 
   357 lemma inner_Pair [simp]: "inner (a, b) (c, d) = inner a c + inner b d"
   358   unfolding inner_prod_def by simp
   359 
   360 instance proof
   361   fix r :: real
   362   fix x y z :: "'a::real_inner * 'b::real_inner"
   363   show "inner x y = inner y x"
   364     unfolding inner_prod_def
   365     by (simp add: inner_commute)
   366   show "inner (x + y) z = inner x z + inner y z"
   367     unfolding inner_prod_def
   368     by (simp add: inner_left_distrib)
   369   show "inner (scaleR r x) y = r * inner x y"
   370     unfolding inner_prod_def
   371     by (simp add: inner_scaleR_left right_distrib)
   372   show "0 \<le> inner x x"
   373     unfolding inner_prod_def
   374     by (intro add_nonneg_nonneg inner_ge_zero)
   375   show "inner x x = 0 \<longleftrightarrow> x = 0"
   376     unfolding inner_prod_def expand_prod_eq
   377     by (simp add: add_nonneg_eq_0_iff)
   378   show "norm x = sqrt (inner x x)"
   379     unfolding norm_prod_def inner_prod_def
   380     by (simp add: power2_norm_eq_inner)
   381 qed
   382 
   383 end
   384 
   385 subsection {* Pair operations are linear *}
   386 
   387 interpretation fst: bounded_linear fst
   388   apply (unfold_locales)
   389   apply (rule fst_add)
   390   apply (rule fst_scaleR)
   391   apply (rule_tac x="1" in exI, simp add: norm_Pair)
   392   done
   393 
   394 interpretation snd: bounded_linear snd
   395   apply (unfold_locales)
   396   apply (rule snd_add)
   397   apply (rule snd_scaleR)
   398   apply (rule_tac x="1" in exI, simp add: norm_Pair)
   399   done
   400 
   401 text {* TODO: move to NthRoot *}
   402 lemma sqrt_add_le_add_sqrt:
   403   assumes x: "0 \<le> x" and y: "0 \<le> y"
   404   shows "sqrt (x + y) \<le> sqrt x + sqrt y"
   405 apply (rule power2_le_imp_le)
   406 apply (simp add: real_sum_squared_expand add_nonneg_nonneg x y)
   407 apply (simp add: mult_nonneg_nonneg x y)
   408 apply (simp add: add_nonneg_nonneg x y)
   409 done
   410 
   411 lemma bounded_linear_Pair:
   412   assumes f: "bounded_linear f"
   413   assumes g: "bounded_linear g"
   414   shows "bounded_linear (\<lambda>x. (f x, g x))"
   415 proof
   416   interpret f: bounded_linear f by fact
   417   interpret g: bounded_linear g by fact
   418   fix x y and r :: real
   419   show "(f (x + y), g (x + y)) = (f x, g x) + (f y, g y)"
   420     by (simp add: f.add g.add)
   421   show "(f (r *\<^sub>R x), g (r *\<^sub>R x)) = r *\<^sub>R (f x, g x)"
   422     by (simp add: f.scaleR g.scaleR)
   423   obtain Kf where "0 < Kf" and norm_f: "\<And>x. norm (f x) \<le> norm x * Kf"
   424     using f.pos_bounded by fast
   425   obtain Kg where "0 < Kg" and norm_g: "\<And>x. norm (g x) \<le> norm x * Kg"
   426     using g.pos_bounded by fast
   427   have "\<forall>x. norm (f x, g x) \<le> norm x * (Kf + Kg)"
   428     apply (rule allI)
   429     apply (simp add: norm_Pair)
   430     apply (rule order_trans [OF sqrt_add_le_add_sqrt], simp, simp)
   431     apply (simp add: right_distrib)
   432     apply (rule add_mono [OF norm_f norm_g])
   433     done
   434   then show "\<exists>K. \<forall>x. norm (f x, g x) \<le> norm x * K" ..
   435 qed
   436 
   437 subsection {* Frechet derivatives involving pairs *}
   438 
   439 lemma FDERIV_Pair:
   440   assumes f: "FDERIV f x :> f'" and g: "FDERIV g x :> g'"
   441   shows "FDERIV (\<lambda>x. (f x, g x)) x :> (\<lambda>h. (f' h, g' h))"
   442 apply (rule FDERIV_I)
   443 apply (rule bounded_linear_Pair)
   444 apply (rule FDERIV_bounded_linear [OF f])
   445 apply (rule FDERIV_bounded_linear [OF g])
   446 apply (simp add: norm_Pair)
   447 apply (rule real_LIM_sandwich_zero)
   448 apply (rule LIM_add_zero)
   449 apply (rule FDERIV_D [OF f])
   450 apply (rule FDERIV_D [OF g])
   451 apply (rename_tac h)
   452 apply (simp add: divide_nonneg_pos)
   453 apply (rename_tac h)
   454 apply (subst add_divide_distrib [symmetric])
   455 apply (rule divide_right_mono [OF _ norm_ge_zero])
   456 apply (rule order_trans [OF sqrt_add_le_add_sqrt])
   457 apply simp
   458 apply simp
   459 apply simp
   460 done
   461 
   462 end