src/HOL/Analysis/Product_Vector.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (2 months ago)
changeset 69981 3dced198b9ec
parent 69541 d466e0a639e4
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
     1 (*  Title:      HOL/Analysis/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
     9     Complex_Main
    10     "HOL-Library.Product_Plus"
    11 begin
    12 
    13 lemma Times_eq_image_sum:
    14   fixes S :: "'a :: comm_monoid_add set" and T :: "'b :: comm_monoid_add set"
    15   shows "S \<times> T = {u + v |u v. u \<in> (\<lambda>x. (x, 0)) ` S \<and> v \<in> Pair 0 ` T}"
    16   by force
    17 
    18 
    19 subsection \<open>Product is a Module\<close>
    20 
    21 locale module_prod = module_pair begin
    22 
    23 definition scale :: "'a \<Rightarrow> 'b \<times> 'c \<Rightarrow> 'b \<times> 'c"
    24   where "scale a v = (s1 a (fst v), s2 a (snd v))"
    25 
    26 lemma%important scale_prod: "scale x (a, b) = (s1 x a, s2 x b)"
    27   by (auto simp: scale_def)
    28 
    29 sublocale%important p: module scale
    30 proof qed (simp_all add: scale_def
    31   m1.scale_left_distrib m1.scale_right_distrib m2.scale_left_distrib m2.scale_right_distrib)
    32 
    33 lemma subspace_Times: "m1.subspace A \<Longrightarrow> m2.subspace B \<Longrightarrow> p.subspace (A \<times> B)"
    34   unfolding m1.subspace_def m2.subspace_def p.subspace_def
    35   by (auto simp: zero_prod_def scale_def)
    36 
    37 lemma module_hom_fst: "module_hom scale s1 fst"
    38   by unfold_locales (auto simp: scale_def)
    39 
    40 lemma module_hom_snd: "module_hom scale s2 snd"
    41   by unfold_locales (auto simp: scale_def)
    42 
    43 end
    44 
    45 locale vector_space_prod = vector_space_pair begin
    46 
    47 sublocale module_prod s1 s2
    48   rewrites "module_hom = Vector_Spaces.linear"
    49   by unfold_locales (fact module_hom_eq_linear)
    50 
    51 sublocale p: vector_space scale by unfold_locales (auto simp: algebra_simps)
    52 
    53 lemmas linear_fst = module_hom_fst
    54   and linear_snd = module_hom_snd
    55 
    56 end
    57 
    58 
    59 subsection \<open>Product is a Real Vector Space\<close>
    60 
    61 instantiation prod :: (real_vector, real_vector) real_vector
    62 begin
    63 
    64 definition scaleR_prod_def:
    65   "scaleR r A = (scaleR r (fst A), scaleR r (snd A))"
    66 
    67 lemma fst_scaleR [simp]: "fst (scaleR r A) = scaleR r (fst A)"
    68   unfolding scaleR_prod_def by simp
    69 
    70 lemma snd_scaleR [simp]: "snd (scaleR r A) = scaleR r (snd A)"
    71   unfolding scaleR_prod_def by simp
    72 
    73 proposition scaleR_Pair [simp]: "scaleR r (a, b) = (scaleR r a, scaleR r b)"
    74   unfolding scaleR_prod_def by simp
    75 
    76 instance
    77 proof
    78   fix a b :: real and x y :: "'a \<times> 'b"
    79   show "scaleR a (x + y) = scaleR a x + scaleR a y"
    80     by (simp add: prod_eq_iff scaleR_right_distrib)
    81   show "scaleR (a + b) x = scaleR a x + scaleR b x"
    82     by (simp add: prod_eq_iff scaleR_left_distrib)
    83   show "scaleR a (scaleR b x) = scaleR (a * b) x"
    84     by (simp add: prod_eq_iff)
    85   show "scaleR 1 x = x"
    86     by (simp add: prod_eq_iff)
    87 qed
    88 
    89 end
    90 
    91 lemma module_prod_scale_eq_scaleR: "module_prod.scale (*\<^sub>R) (*\<^sub>R) = scaleR"
    92   apply (rule ext) apply (rule ext)
    93   apply (subst module_prod.scale_def)
    94   subgoal by unfold_locales
    95   by (simp add: scaleR_prod_def)
    96 
    97 interpretation real_vector?: vector_space_prod "scaleR::_\<Rightarrow>_\<Rightarrow>'a::real_vector" "scaleR::_\<Rightarrow>_\<Rightarrow>'b::real_vector"
    98   rewrites "scale = ((*\<^sub>R)::_\<Rightarrow>_\<Rightarrow>('a \<times> 'b))"
    99     and "module.dependent (*\<^sub>R) = dependent"
   100     and "module.representation (*\<^sub>R) = representation"
   101     and "module.subspace (*\<^sub>R) = subspace"
   102     and "module.span (*\<^sub>R) = span"
   103     and "vector_space.extend_basis (*\<^sub>R) = extend_basis"
   104     and "vector_space.dim (*\<^sub>R) = dim"
   105     and "Vector_Spaces.linear (*\<^sub>R) (*\<^sub>R) = linear"
   106   subgoal by unfold_locales
   107   subgoal by (fact module_prod_scale_eq_scaleR)
   108   unfolding dependent_raw_def representation_raw_def subspace_raw_def span_raw_def
   109     extend_basis_raw_def dim_raw_def linear_def
   110   by (rule refl)+
   111 
   112 subsection \<open>Product is a Metric Space\<close>
   113 
   114 (* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
   115 
   116 instantiation%unimportant prod :: (metric_space, metric_space) dist
   117 begin
   118 
   119 definition dist_prod_def[code del]:
   120   "dist x y = sqrt ((dist (fst x) (fst y))\<^sup>2 + (dist (snd x) (snd y))\<^sup>2)"
   121 
   122 instance ..
   123 end
   124 
   125 instantiation%unimportant prod :: (metric_space, metric_space) uniformity_dist
   126 begin
   127 
   128 definition [code del]:
   129   "(uniformity :: (('a \<times> 'b) \<times> ('a \<times> 'b)) filter) =
   130     (INF e\<in>{0 <..}. principal {(x, y). dist x y < e})"
   131 
   132 instance
   133   by standard (rule uniformity_prod_def)
   134 end
   135 
   136 declare uniformity_Abort[where 'a="'a :: metric_space \<times> 'b :: metric_space", code]
   137 
   138 instantiation prod :: (metric_space, metric_space) metric_space
   139 begin
   140 
   141 proposition dist_Pair_Pair: "dist (a, b) (c, d) = sqrt ((dist a c)\<^sup>2 + (dist b d)\<^sup>2)"
   142   unfolding dist_prod_def by simp
   143 
   144 lemma dist_fst_le: "dist (fst x) (fst y) \<le> dist x y"
   145   unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge1)
   146 
   147 lemma dist_snd_le: "dist (snd x) (snd y) \<le> dist x y"
   148   unfolding dist_prod_def by (rule real_sqrt_sum_squares_ge2)
   149 
   150 instance
   151 proof
   152   fix x y :: "'a \<times> 'b"
   153   show "dist x y = 0 \<longleftrightarrow> x = y"
   154     unfolding dist_prod_def prod_eq_iff by simp
   155 next
   156   fix x y z :: "'a \<times> 'b"
   157   show "dist x y \<le> dist x z + dist y z"
   158     unfolding dist_prod_def
   159     by (intro order_trans [OF _ real_sqrt_sum_squares_triangle_ineq]
   160         real_sqrt_le_mono add_mono power_mono dist_triangle2 zero_le_dist)
   161 next
   162   fix S :: "('a \<times> 'b) set"
   163   have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   164   proof
   165     assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   166     proof
   167       fix x assume "x \<in> S"
   168       obtain A B where "open A" "open B" "x \<in> A \<times> B" "A \<times> B \<subseteq> S"
   169         using \<open>open S\<close> and \<open>x \<in> S\<close> by (rule open_prod_elim)
   170       obtain r where r: "0 < r" "\<forall>y. dist y (fst x) < r \<longrightarrow> y \<in> A"
   171         using \<open>open A\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
   172       obtain s where s: "0 < s" "\<forall>y. dist y (snd x) < s \<longrightarrow> y \<in> B"
   173         using \<open>open B\<close> and \<open>x \<in> A \<times> B\<close> unfolding open_dist by auto
   174       let ?e = "min r s"
   175       have "0 < ?e \<and> (\<forall>y. dist y x < ?e \<longrightarrow> y \<in> S)"
   176       proof (intro allI impI conjI)
   177         show "0 < min r s" by (simp add: r(1) s(1))
   178       next
   179         fix y assume "dist y x < min r s"
   180         hence "dist y x < r" and "dist y x < s"
   181           by simp_all
   182         hence "dist (fst y) (fst x) < r" and "dist (snd y) (snd x) < s"
   183           by (auto intro: le_less_trans dist_fst_le dist_snd_le)
   184         hence "fst y \<in> A" and "snd y \<in> B"
   185           by (simp_all add: r(2) s(2))
   186         hence "y \<in> A \<times> B" by (induct y, simp)
   187         with \<open>A \<times> B \<subseteq> S\<close> show "y \<in> S" ..
   188       qed
   189       thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
   190     qed
   191   next
   192     assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
   193     proof (rule open_prod_intro)
   194       fix x assume "x \<in> S"
   195       then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   196         using * by fast
   197       define r where "r = e / sqrt 2"
   198       define s where "s = e / sqrt 2"
   199       from \<open>0 < e\<close> have "0 < r" and "0 < s"
   200         unfolding r_def s_def by simp_all
   201       from \<open>0 < e\<close> have "e = sqrt (r\<^sup>2 + s\<^sup>2)"
   202         unfolding r_def s_def by (simp add: power_divide)
   203       define A where "A = {y. dist (fst x) y < r}"
   204       define B where "B = {y. dist (snd x) y < s}"
   205       have "open A" and "open B"
   206         unfolding A_def B_def by (simp_all add: open_ball)
   207       moreover have "x \<in> A \<times> B"
   208         unfolding A_def B_def mem_Times_iff
   209         using \<open>0 < r\<close> and \<open>0 < s\<close> by simp
   210       moreover have "A \<times> B \<subseteq> S"
   211       proof (clarify)
   212         fix a b assume "a \<in> A" and "b \<in> B"
   213         hence "dist a (fst x) < r" and "dist b (snd x) < s"
   214           unfolding A_def B_def by (simp_all add: dist_commute)
   215         hence "dist (a, b) x < e"
   216           unfolding dist_prod_def \<open>e = sqrt (r\<^sup>2 + s\<^sup>2)\<close>
   217           by (simp add: add_strict_mono power_strict_mono)
   218         thus "(a, b) \<in> S"
   219           by (simp add: S)
   220       qed
   221       ultimately show "\<exists>A B. open A \<and> open B \<and> x \<in> A \<times> B \<and> A \<times> B \<subseteq> S" by fast
   222     qed
   223   qed
   224   show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   225     unfolding * eventually_uniformity_metric
   226     by (simp del: split_paired_All add: dist_prod_def dist_commute)
   227 qed
   228 
   229 end
   230 
   231 declare [[code abort: "dist::('a::metric_space*'b::metric_space)\<Rightarrow>('a*'b) \<Rightarrow> real"]]
   232 
   233 lemma Cauchy_fst: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. fst (X n))"
   234   unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_fst_le])
   235 
   236 lemma Cauchy_snd: "Cauchy X \<Longrightarrow> Cauchy (\<lambda>n. snd (X n))"
   237   unfolding Cauchy_def by (fast elim: le_less_trans [OF dist_snd_le])
   238 
   239 lemma Cauchy_Pair:
   240   assumes "Cauchy X" and "Cauchy Y"
   241   shows "Cauchy (\<lambda>n. (X n, Y n))"
   242 proof (rule metric_CauchyI)
   243   fix r :: real assume "0 < r"
   244   hence "0 < r / sqrt 2" (is "0 < ?s") by simp
   245   obtain M where M: "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < ?s"
   246     using metric_CauchyD [OF \<open>Cauchy X\<close> \<open>0 < ?s\<close>] ..
   247   obtain N where N: "\<forall>m\<ge>N. \<forall>n\<ge>N. dist (Y m) (Y n) < ?s"
   248     using metric_CauchyD [OF \<open>Cauchy Y\<close> \<open>0 < ?s\<close>] ..
   249   have "\<forall>m\<ge>max M N. \<forall>n\<ge>max M N. dist (X m, Y m) (X n, Y n) < r"
   250     using M N by (simp add: real_sqrt_sum_squares_less dist_Pair_Pair)
   251   then show "\<exists>n0. \<forall>m\<ge>n0. \<forall>n\<ge>n0. dist (X m, Y m) (X n, Y n) < r" ..
   252 qed
   253 
   254 subsection \<open>Product is a Complete Metric Space\<close>
   255 
   256 instance%important prod :: (complete_space, complete_space) complete_space
   257 proof%unimportant
   258   fix X :: "nat \<Rightarrow> 'a \<times> 'b" assume "Cauchy X"
   259   have 1: "(\<lambda>n. fst (X n)) \<longlonglongrightarrow> lim (\<lambda>n. fst (X n))"
   260     using Cauchy_fst [OF \<open>Cauchy X\<close>]
   261     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   262   have 2: "(\<lambda>n. snd (X n)) \<longlonglongrightarrow> lim (\<lambda>n. snd (X n))"
   263     using Cauchy_snd [OF \<open>Cauchy X\<close>]
   264     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   265   have "X \<longlonglongrightarrow> (lim (\<lambda>n. fst (X n)), lim (\<lambda>n. snd (X n)))"
   266     using tendsto_Pair [OF 1 2] by simp
   267   then show "convergent X"
   268     by (rule convergentI)
   269 qed
   270 
   271 subsection \<open>Product is a Normed Vector Space\<close>
   272 
   273 instantiation prod :: (real_normed_vector, real_normed_vector) real_normed_vector
   274 begin
   275 
   276 definition norm_prod_def[code del]:
   277   "norm x = sqrt ((norm (fst x))\<^sup>2 + (norm (snd x))\<^sup>2)"
   278 
   279 definition sgn_prod_def:
   280   "sgn (x::'a \<times> 'b) = scaleR (inverse (norm x)) x"
   281 
   282 proposition norm_Pair: "norm (a, b) = sqrt ((norm a)\<^sup>2 + (norm b)\<^sup>2)"
   283   unfolding norm_prod_def by simp
   284 
   285 instance
   286 proof
   287   fix r :: real and x y :: "'a \<times> 'b"
   288   show "norm x = 0 \<longleftrightarrow> x = 0"
   289     unfolding norm_prod_def
   290     by (simp add: prod_eq_iff)
   291   show "norm (x + y) \<le> norm x + norm y"
   292     unfolding norm_prod_def
   293     apply (rule order_trans [OF _ real_sqrt_sum_squares_triangle_ineq])
   294     apply (simp add: add_mono power_mono norm_triangle_ineq)
   295     done
   296   show "norm (scaleR r x) = \<bar>r\<bar> * norm x"
   297     unfolding norm_prod_def
   298     apply (simp add: power_mult_distrib)
   299     apply (simp add: distrib_left [symmetric])
   300     apply (simp add: real_sqrt_mult)
   301     done
   302   show "sgn x = scaleR (inverse (norm x)) x"
   303     by (rule sgn_prod_def)
   304   show "dist x y = norm (x - y)"
   305     unfolding dist_prod_def norm_prod_def
   306     by (simp add: dist_norm)
   307 qed
   308 
   309 end
   310 
   311 declare [[code abort: "norm::('a::real_normed_vector*'b::real_normed_vector) \<Rightarrow> real"]]
   312 
   313 instance%important prod :: (banach, banach) banach ..
   314 
   315 subsubsection%unimportant \<open>Pair operations are linear\<close>
   316 
   317 lemma bounded_linear_fst: "bounded_linear fst"
   318   using fst_add fst_scaleR
   319   by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
   320 
   321 lemma bounded_linear_snd: "bounded_linear snd"
   322   using snd_add snd_scaleR
   323   by (rule bounded_linear_intro [where K=1], simp add: norm_prod_def)
   324 
   325 lemmas bounded_linear_fst_comp = bounded_linear_fst[THEN bounded_linear_compose]
   326 
   327 lemmas bounded_linear_snd_comp = bounded_linear_snd[THEN bounded_linear_compose]
   328 
   329 lemma bounded_linear_Pair:
   330   assumes f: "bounded_linear f"
   331   assumes g: "bounded_linear g"
   332   shows "bounded_linear (\<lambda>x. (f x, g x))"
   333 proof
   334   interpret f: bounded_linear f by fact
   335   interpret g: bounded_linear g by fact
   336   fix x y and r :: real
   337   show "(f (x + y), g (x + y)) = (f x, g x) + (f y, g y)"
   338     by (simp add: f.add g.add)
   339   show "(f (r *\<^sub>R x), g (r *\<^sub>R x)) = r *\<^sub>R (f x, g x)"
   340     by (simp add: f.scale g.scale)
   341   obtain Kf where "0 < Kf" and norm_f: "\<And>x. norm (f x) \<le> norm x * Kf"
   342     using f.pos_bounded by fast
   343   obtain Kg where "0 < Kg" and norm_g: "\<And>x. norm (g x) \<le> norm x * Kg"
   344     using g.pos_bounded by fast
   345   have "\<forall>x. norm (f x, g x) \<le> norm x * (Kf + Kg)"
   346     apply (rule allI)
   347     apply (simp add: norm_Pair)
   348     apply (rule order_trans [OF sqrt_add_le_add_sqrt], simp, simp)
   349     apply (simp add: distrib_left)
   350     apply (rule add_mono [OF norm_f norm_g])
   351     done
   352   then show "\<exists>K. \<forall>x. norm (f x, g x) \<le> norm x * K" ..
   353 qed
   354 
   355 subsubsection%unimportant \<open>Frechet derivatives involving pairs\<close>
   356 
   357 proposition has_derivative_Pair [derivative_intros]:
   358   assumes f: "(f has_derivative f') (at x within s)"
   359     and g: "(g has_derivative g') (at x within s)"
   360   shows "((\<lambda>x. (f x, g x)) has_derivative (\<lambda>h. (f' h, g' h))) (at x within s)"
   361 proof (rule has_derivativeI_sandwich[of 1])
   362   show "bounded_linear (\<lambda>h. (f' h, g' h))"
   363     using f g by (intro bounded_linear_Pair has_derivative_bounded_linear)
   364   let ?Rf = "\<lambda>y. f y - f x - f' (y - x)"
   365   let ?Rg = "\<lambda>y. g y - g x - g' (y - x)"
   366   let ?R = "\<lambda>y. ((f y, g y) - (f x, g x) - (f' (y - x), g' (y - x)))"
   367 
   368   show "((\<lambda>y. norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)) \<longlongrightarrow> 0) (at x within s)"
   369     using f g by (intro tendsto_add_zero) (auto simp: has_derivative_iff_norm)
   370 
   371   fix y :: 'a assume "y \<noteq> x"
   372   show "norm (?R y) / norm (y - x) \<le> norm (?Rf y) / norm (y - x) + norm (?Rg y) / norm (y - x)"
   373     unfolding add_divide_distrib [symmetric]
   374     by (simp add: norm_Pair divide_right_mono order_trans [OF sqrt_add_le_add_sqrt])
   375 qed simp
   376 
   377 lemma differentiable_Pair [simp, derivative_intros]:
   378   "f differentiable at x within s \<Longrightarrow> g differentiable at x within s \<Longrightarrow>
   379     (\<lambda>x. (f x, g x)) differentiable at x within s"
   380   unfolding differentiable_def by (blast intro: has_derivative_Pair)
   381 
   382 lemmas has_derivative_fst [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_fst]
   383 lemmas has_derivative_snd [derivative_intros] = bounded_linear.has_derivative [OF bounded_linear_snd]
   384 
   385 lemma has_derivative_split [derivative_intros]:
   386   "((\<lambda>p. f (fst p) (snd p)) has_derivative f') F \<Longrightarrow> ((\<lambda>(a, b). f a b) has_derivative f') F"
   387   unfolding split_beta' .
   388 
   389 
   390 subsubsection%unimportant \<open>Vector derivatives involving pairs\<close>
   391 
   392 lemma has_vector_derivative_Pair[derivative_intros]:
   393   assumes "(f has_vector_derivative f') (at x within s)"
   394     "(g has_vector_derivative g') (at x within s)"
   395   shows "((\<lambda>x. (f x, g x)) has_vector_derivative (f', g')) (at x within s)"
   396   using assms
   397   by (auto simp: has_vector_derivative_def intro!: derivative_eq_intros)
   398 
   399 lemma
   400   fixes x :: "'a::real_normed_vector"
   401   shows norm_Pair1 [simp]: "norm (0,x) = norm x"
   402     and norm_Pair2 [simp]: "norm (x,0) = norm x"
   403 by (auto simp: norm_Pair)
   404 
   405 lemma norm_commute: "norm (x,y) = norm (y,x)"
   406   by (simp add: norm_Pair)
   407 
   408 lemma norm_fst_le: "norm x \<le> norm (x,y)"
   409   by (metis dist_fst_le fst_conv fst_zero norm_conv_dist)
   410 
   411 lemma norm_snd_le: "norm y \<le> norm (x,y)"
   412   by (metis dist_snd_le snd_conv snd_zero norm_conv_dist)
   413 
   414 lemma norm_Pair_le:
   415   shows "norm (x, y) \<le> norm x + norm y"
   416   unfolding norm_Pair
   417   by (metis norm_ge_zero sqrt_sum_squares_le_sum)
   418 
   419 lemma (in vector_space_prod) span_Times_sing1: "p.span ({0} \<times> B) = {0} \<times> vs2.span B"
   420   apply (rule p.span_unique)
   421   subgoal by (auto intro!: vs1.span_base vs2.span_base)
   422   subgoal using vs1.subspace_single_0 vs2.subspace_span by (rule subspace_Times)
   423   subgoal for T
   424   proof safe
   425     fix b
   426     assume subset_T: "{0} \<times> B \<subseteq> T" and subspace: "p.subspace T" and b_span: "b \<in> vs2.span B"
   427     then obtain t r where b: "b = (\<Sum>a\<in>t. r a *b a)" and t: "finite t" "t \<subseteq> B"
   428       by (auto simp: vs2.span_explicit)
   429     have "(0, b) = (\<Sum>b\<in>t. scale (r b) (0, b))"
   430       unfolding b scale_prod sum_prod
   431       by simp
   432     also have "\<dots> \<in> T"
   433       using \<open>t \<subseteq> B\<close> subset_T
   434       by (auto intro!: p.subspace_sum p.subspace_scale subspace)
   435     finally show "(0, b) \<in> T" .
   436   qed
   437   done
   438 
   439 lemma (in vector_space_prod) span_Times_sing2: "p.span (A \<times> {0}) = vs1.span A \<times> {0}"
   440   apply (rule p.span_unique)
   441   subgoal by (auto intro!: vs1.span_base vs2.span_base)
   442   subgoal using vs1.subspace_span vs2.subspace_single_0 by (rule subspace_Times)
   443   subgoal for T
   444   proof safe
   445     fix a
   446     assume subset_T: "A \<times> {0} \<subseteq> T" and subspace: "p.subspace T" and a_span: "a \<in> vs1.span A"
   447     then obtain t r where a: "a = (\<Sum>a\<in>t. r a *a a)" and t: "finite t" "t \<subseteq> A"
   448       by (auto simp: vs1.span_explicit)
   449     have "(a, 0) = (\<Sum>a\<in>t. scale (r a) (a, 0))"
   450       unfolding a scale_prod sum_prod
   451       by simp
   452     also have "\<dots> \<in> T"
   453       using \<open>t \<subseteq> A\<close> subset_T
   454       by (auto intro!: p.subspace_sum p.subspace_scale subspace)
   455     finally show "(a, 0) \<in> T" .
   456   qed
   457   done
   458 
   459 subsection \<open>Product is Finite Dimensional\<close>
   460 
   461 lemma (in finite_dimensional_vector_space) zero_not_in_Basis[simp]: "0 \<notin> Basis"
   462   using dependent_zero local.independent_Basis by blast
   463 
   464 locale finite_dimensional_vector_space_prod = vector_space_prod + finite_dimensional_vector_space_pair begin
   465 
   466 definition "Basis_pair = B1 \<times> {0} \<union> {0} \<times> B2"
   467 
   468 sublocale p: finite_dimensional_vector_space scale Basis_pair
   469 proof unfold_locales
   470   show "finite Basis_pair"
   471     by (auto intro!: finite_cartesian_product vs1.finite_Basis vs2.finite_Basis simp: Basis_pair_def)
   472   show "p.independent Basis_pair"
   473     unfolding p.dependent_def Basis_pair_def
   474   proof safe
   475     fix a
   476     assume a: "a \<in> B1"
   477     assume "(a, 0) \<in> p.span (B1 \<times> {0} \<union> {0} \<times> B2 - {(a, 0)})"
   478     also have "B1 \<times> {0} \<union> {0} \<times> B2 - {(a, 0)} = (B1 - {a}) \<times> {0} \<union> {0} \<times> B2"
   479       by auto
   480     finally show False
   481       using a vs1.dependent_def vs1.independent_Basis
   482       by (auto simp: p.span_Un span_Times_sing1 span_Times_sing2)
   483   next
   484     fix b
   485     assume b: "b \<in> B2"
   486     assume "(0, b) \<in> p.span (B1 \<times> {0} \<union> {0} \<times> B2 - {(0, b)})"
   487     also have "(B1 \<times> {0} \<union> {0} \<times> B2 - {(0, b)}) = B1 \<times> {0} \<union> {0} \<times> (B2 - {b})"
   488       by auto
   489     finally show False
   490       using b vs2.dependent_def vs2.independent_Basis
   491       by (auto simp: p.span_Un span_Times_sing1 span_Times_sing2)
   492   qed
   493   show "p.span Basis_pair = UNIV"
   494     by (auto simp: p.span_Un span_Times_sing2 span_Times_sing1 vs1.span_Basis vs2.span_Basis
   495         Basis_pair_def)
   496 qed
   497 
   498 proposition dim_Times:
   499   assumes "vs1.subspace S" "vs2.subspace T"
   500   shows "p.dim(S \<times> T) = vs1.dim S + vs2.dim T"
   501 proof -
   502   interpret p1: Vector_Spaces.linear s1 scale "(\<lambda>x. (x, 0))"
   503     by unfold_locales (auto simp: scale_def)
   504   interpret pair1: finite_dimensional_vector_space_pair "(*a)" B1 scale Basis_pair
   505     by unfold_locales
   506   interpret p2: Vector_Spaces.linear s2 scale "(\<lambda>x. (0, x))"
   507     by unfold_locales (auto simp: scale_def)
   508   interpret pair2: finite_dimensional_vector_space_pair "(*b)" B2 scale Basis_pair
   509     by unfold_locales
   510   have ss: "p.subspace ((\<lambda>x. (x, 0)) ` S)" "p.subspace (Pair 0 ` T)"
   511     by (rule p1.subspace_image p2.subspace_image assms)+
   512   have "p.dim(S \<times> T) = p.dim({u + v |u v. u \<in> (\<lambda>x. (x, 0)) ` S \<and> v \<in> Pair 0 ` T})"
   513     by (simp add: Times_eq_image_sum)
   514   moreover have "p.dim ((\<lambda>x. (x, 0::'c)) ` S) = vs1.dim S" "p.dim (Pair (0::'b) ` T) = vs2.dim T"
   515      by (simp_all add: inj_on_def p1.linear_axioms pair1.dim_image_eq p2.linear_axioms pair2.dim_image_eq)
   516   moreover have "p.dim ((\<lambda>x. (x, 0)) ` S \<inter> Pair 0 ` T) = 0"
   517     by (subst p.dim_eq_0) auto
   518   ultimately show ?thesis
   519     using p.dim_sums_Int [OF ss] by linarith
   520 qed
   521 
   522 lemma dimension_pair: "p.dimension = vs1.dimension + vs2.dimension"
   523   using dim_Times[OF vs1.subspace_UNIV vs2.subspace_UNIV]
   524   by (auto simp: p.dim_UNIV vs1.dim_UNIV vs2.dim_UNIV
   525       p.dimension_def vs1.dimension_def vs2.dimension_def)
   526 
   527 end
   528 
   529 end