src/HOL/Analysis/Finite_Cartesian_Product.thy
author wenzelm
Mon Mar 25 17:21:26 2019 +0100 (4 weeks ago)
changeset 69981 3dced198b9ec
parent 69720 be6634e99e09
child 70136 f03a01a18c6e
permissions -rw-r--r--
more strict AFP properties;
     1 (*  Title:      HOL/Analysis/Finite_Cartesian_Product.thy
     2     Author:     Amine Chaieb, University of Cambridge
     3 *)
     4 
     5 section \<open>Definition of Finite Cartesian Product Type\<close>
     6 
     7 theory Finite_Cartesian_Product
     8 imports
     9   Euclidean_Space
    10   L2_Norm
    11   "HOL-Library.Numeral_Type"
    12   "HOL-Library.Countable_Set"
    13   "HOL-Library.FuncSet"
    14 begin
    15 
    16 subsection%unimportant \<open>Finite Cartesian products, with indexing and lambdas\<close>
    17 
    18 typedef ('a, 'b) vec = "UNIV :: ('b::finite \<Rightarrow> 'a) set"
    19   morphisms vec_nth vec_lambda ..
    20 
    21 declare vec_lambda_inject [simplified, simp]
    22 
    23 bundle vec_syntax begin
    24 notation
    25   vec_nth (infixl "$" 90) and
    26   vec_lambda (binder "\<chi>" 10)
    27 end
    28 
    29 bundle no_vec_syntax begin
    30 no_notation
    31   vec_nth (infixl "$" 90) and
    32   vec_lambda (binder "\<chi>" 10)
    33 end
    34 
    35 unbundle vec_syntax
    36 
    37 text \<open>
    38   Concrete syntax for \<open>('a, 'b) vec\<close>:
    39     \<^item> \<open>'a^'b\<close> becomes \<open>('a, 'b::finite) vec\<close>
    40     \<^item> \<open>'a^'b::_\<close> becomes \<open>('a, 'b) vec\<close> without extra sort-constraint
    41 \<close>
    42 syntax "_vec_type" :: "type \<Rightarrow> type \<Rightarrow> type" (infixl "^" 15)
    43 parse_translation \<open>
    44   let
    45     fun vec t u = Syntax.const \<^type_syntax>\<open>vec\<close> $ t $ u;
    46     fun finite_vec_tr [t, u] =
    47       (case Term_Position.strip_positions u of
    48         v as Free (x, _) =>
    49           if Lexicon.is_tid x then
    50             vec t (Syntax.const \<^syntax_const>\<open>_ofsort\<close> $ v $
    51               Syntax.const \<^class_syntax>\<open>finite\<close>)
    52           else vec t u
    53       | _ => vec t u)
    54   in
    55     [(\<^syntax_const>\<open>_vec_type\<close>, K finite_vec_tr)]
    56   end
    57 \<close>
    58 
    59 lemma vec_eq_iff: "(x = y) \<longleftrightarrow> (\<forall>i. x$i = y$i)"
    60   by (simp add: vec_nth_inject [symmetric] fun_eq_iff)
    61 
    62 lemma vec_lambda_beta [simp]: "vec_lambda g $ i = g i"
    63   by (simp add: vec_lambda_inverse)
    64 
    65 lemma vec_lambda_unique: "(\<forall>i. f$i = g i) \<longleftrightarrow> vec_lambda g = f"
    66   by (auto simp add: vec_eq_iff)
    67 
    68 lemma vec_lambda_eta [simp]: "(\<chi> i. (g$i)) = g"
    69   by (simp add: vec_eq_iff)
    70 
    71 subsection \<open>Cardinality of vectors\<close>
    72 
    73 instance vec :: (finite, finite) finite
    74 proof
    75   show "finite (UNIV :: ('a, 'b) vec set)"
    76   proof (subst bij_betw_finite)
    77     show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
    78       by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
    79     have "finite (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
    80       by (intro finite_PiE) auto
    81     also have "(PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set)) = Pi UNIV (\<lambda>_. UNIV)"
    82       by auto
    83     finally show "finite \<dots>" .
    84   qed
    85 qed
    86 
    87 lemma countable_PiE:
    88   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> countable (F i)) \<Longrightarrow> countable (Pi\<^sub>E I F)"
    89   by (induct I arbitrary: F rule: finite_induct) (auto simp: PiE_insert_eq)
    90 
    91 instance vec :: (countable, finite) countable
    92 proof
    93   have "countable (UNIV :: ('a, 'b) vec set)"
    94   proof (rule countableI_bij2)
    95     show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
    96       by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
    97     have "countable (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
    98       by (intro countable_PiE) auto
    99     also have "(PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set)) = Pi UNIV (\<lambda>_. UNIV)"
   100       by auto
   101     finally show "countable \<dots>" .
   102   qed
   103   thus "\<exists>t::('a, 'b) vec \<Rightarrow> nat. inj t"
   104     by (auto elim!: countableE)
   105 qed
   106 
   107 lemma infinite_UNIV_vec:
   108   assumes "infinite (UNIV :: 'a set)"
   109   shows   "infinite (UNIV :: ('a^'b) set)"
   110 proof (subst bij_betw_finite)
   111   show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
   112     by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
   113   have "infinite (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))" (is "infinite ?A")
   114   proof
   115     assume "finite ?A"
   116     hence "finite ((\<lambda>f. f undefined) ` ?A)"
   117       by (rule finite_imageI)
   118     also have "(\<lambda>f. f undefined) ` ?A = UNIV"
   119       by auto
   120     finally show False 
   121       using \<open>infinite (UNIV :: 'a set)\<close> by contradiction
   122   qed
   123   also have "?A = Pi UNIV (\<lambda>_. UNIV)" 
   124     by auto
   125   finally show "infinite (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))" .
   126 qed
   127 
   128 proposition CARD_vec [simp]:
   129   "CARD('a^'b) = CARD('a) ^ CARD('b)"
   130 proof (cases "finite (UNIV :: 'a set)")
   131   case True
   132   show ?thesis
   133   proof (subst bij_betw_same_card)
   134     show "bij_betw vec_nth UNIV (Pi (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
   135       by (intro bij_betwI[of _ _ _ vec_lambda]) (auto simp: vec_eq_iff)
   136     have "CARD('a) ^ CARD('b) = card (PiE (UNIV :: 'b set) (\<lambda>_. UNIV :: 'a set))"
   137       (is "_ = card ?A")
   138       by (subst card_PiE) (auto simp: prod_constant)
   139     
   140     also have "?A = Pi UNIV (\<lambda>_. UNIV)" 
   141       by auto
   142     finally show "card \<dots> = CARD('a) ^ CARD('b)" ..
   143   qed
   144 qed (simp_all add: infinite_UNIV_vec)
   145 
   146 lemma countable_vector:
   147   fixes B:: "'n::finite \<Rightarrow> 'a set"
   148   assumes "\<And>i. countable (B i)"
   149   shows "countable {V. \<forall>i::'n::finite. V $ i \<in> B i}"
   150 proof -
   151   have "f \<in> ($) ` {V. \<forall>i. V $ i \<in> B i}" if "f \<in> Pi\<^sub>E UNIV B" for f
   152   proof -
   153     have "\<exists>W. (\<forall>i. W $ i \<in> B i) \<and> ($) W = f"
   154       by (metis that PiE_iff UNIV_I vec_lambda_inverse)
   155     then show "f \<in> ($) ` {v. \<forall>i. v $ i \<in> B i}"
   156       by blast
   157   qed
   158   then have "Pi\<^sub>E UNIV B = vec_nth ` {V. \<forall>i::'n. V $ i \<in> B i}"
   159     by blast
   160   then have "countable (vec_nth ` {V. \<forall>i. V $ i \<in> B i})"
   161     by (metis finite_class.finite_UNIV countable_PiE assms)
   162   then have "countable (vec_lambda ` vec_nth ` {V. \<forall>i. V $ i \<in> B i})"
   163     by auto
   164   then show ?thesis
   165     by (simp add: image_comp o_def vec_nth_inverse)
   166 qed
   167 
   168 subsection%unimportant \<open>Group operations and class instances\<close>
   169 
   170 instantiation vec :: (zero, finite) zero
   171 begin
   172   definition "0 \<equiv> (\<chi> i. 0)"
   173   instance ..
   174 end
   175 
   176 instantiation vec :: (plus, finite) plus
   177 begin
   178   definition "(+) \<equiv> (\<lambda> x y. (\<chi> i. x$i + y$i))"
   179   instance ..
   180 end
   181 
   182 instantiation vec :: (minus, finite) minus
   183 begin
   184   definition "(-) \<equiv> (\<lambda> x y. (\<chi> i. x$i - y$i))"
   185   instance ..
   186 end
   187 
   188 instantiation vec :: (uminus, finite) uminus
   189 begin
   190   definition "uminus \<equiv> (\<lambda> x. (\<chi> i. - (x$i)))"
   191   instance ..
   192 end
   193 
   194 lemma zero_index [simp]: "0 $ i = 0"
   195   unfolding zero_vec_def by simp
   196 
   197 lemma vector_add_component [simp]: "(x + y)$i = x$i + y$i"
   198   unfolding plus_vec_def by simp
   199 
   200 lemma vector_minus_component [simp]: "(x - y)$i = x$i - y$i"
   201   unfolding minus_vec_def by simp
   202 
   203 lemma vector_uminus_component [simp]: "(- x)$i = - (x$i)"
   204   unfolding uminus_vec_def by simp
   205 
   206 instance vec :: (semigroup_add, finite) semigroup_add
   207   by standard (simp add: vec_eq_iff add.assoc)
   208 
   209 instance vec :: (ab_semigroup_add, finite) ab_semigroup_add
   210   by standard (simp add: vec_eq_iff add.commute)
   211 
   212 instance vec :: (monoid_add, finite) monoid_add
   213   by standard (simp_all add: vec_eq_iff)
   214 
   215 instance vec :: (comm_monoid_add, finite) comm_monoid_add
   216   by standard (simp add: vec_eq_iff)
   217 
   218 instance vec :: (cancel_semigroup_add, finite) cancel_semigroup_add
   219   by standard (simp_all add: vec_eq_iff)
   220 
   221 instance vec :: (cancel_ab_semigroup_add, finite) cancel_ab_semigroup_add
   222   by standard (simp_all add: vec_eq_iff diff_diff_eq)
   223 
   224 instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   225 
   226 instance vec :: (group_add, finite) group_add
   227   by standard (simp_all add: vec_eq_iff)
   228 
   229 instance vec :: (ab_group_add, finite) ab_group_add
   230   by standard (simp_all add: vec_eq_iff)
   231 
   232 
   233 subsection%unimportant\<open>Basic componentwise operations on vectors\<close>
   234 
   235 instantiation vec :: (times, finite) times
   236 begin
   237 
   238 definition "(*) \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))"
   239 instance ..
   240 
   241 end
   242 
   243 instantiation vec :: (one, finite) one
   244 begin
   245 
   246 definition "1 \<equiv> (\<chi> i. 1)"
   247 instance ..
   248 
   249 end
   250 
   251 instantiation vec :: (ord, finite) ord
   252 begin
   253 
   254 definition "x \<le> y \<longleftrightarrow> (\<forall>i. x$i \<le> y$i)"
   255 definition "x < (y::'a^'b) \<longleftrightarrow> x \<le> y \<and> \<not> y \<le> x"
   256 instance ..
   257 
   258 end
   259 
   260 text\<open>The ordering on one-dimensional vectors is linear.\<close>
   261 
   262 instance vec:: (order, finite) order
   263   by standard (auto simp: less_eq_vec_def less_vec_def vec_eq_iff
   264       intro: order.trans order.antisym order.strict_implies_order)
   265 
   266 instance vec :: (linorder, CARD_1) linorder
   267 proof
   268   obtain a :: 'b where all: "\<And>P. (\<forall>i. P i) \<longleftrightarrow> P a"
   269   proof -
   270     have "CARD ('b) = 1" by (rule CARD_1)
   271     then obtain b :: 'b where "UNIV = {b}" by (auto iff: card_Suc_eq)
   272     then have "\<And>P. (\<forall>i\<in>UNIV. P i) \<longleftrightarrow> P b" by auto
   273     then show thesis by (auto intro: that)
   274   qed
   275   fix x y :: "'a^'b::CARD_1"
   276   note [simp] = less_eq_vec_def less_vec_def all vec_eq_iff field_simps
   277   show "x \<le> y \<or> y \<le> x" by auto
   278 qed
   279 
   280 text\<open>Constant Vectors\<close>
   281 
   282 definition "vec x = (\<chi> i. x)"
   283 
   284 text\<open>Also the scalar-vector multiplication.\<close>
   285 
   286 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
   287   where "c *s x = (\<chi> i. c * (x$i))"
   288 
   289 text \<open>scalar product\<close>
   290 
   291 definition scalar_product :: "'a :: semiring_1 ^ 'n \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a" where
   292   "scalar_product v w = (\<Sum> i \<in> UNIV. v $ i * w $ i)"
   293 
   294 
   295 subsection \<open>Real vector space\<close>
   296 
   297 instantiation%unimportant vec :: (real_vector, finite) real_vector
   298 begin
   299 
   300 definition%important "scaleR \<equiv> (\<lambda> r x. (\<chi> i. scaleR r (x$i)))"
   301 
   302 lemma vector_scaleR_component [simp]: "(scaleR r x)$i = scaleR r (x$i)"
   303   unfolding scaleR_vec_def by simp
   304 
   305 instance%unimportant
   306   by standard (simp_all add: vec_eq_iff scaleR_left_distrib scaleR_right_distrib)
   307 
   308 end
   309 
   310 
   311 subsection \<open>Topological space\<close>
   312 
   313 instantiation%unimportant vec :: (topological_space, finite) topological_space
   314 begin
   315 
   316 definition%important [code del]:
   317   "open (S :: ('a ^ 'b) set) \<longleftrightarrow>
   318     (\<forall>x\<in>S. \<exists>A. (\<forall>i. open (A i) \<and> x$i \<in> A i) \<and>
   319       (\<forall>y. (\<forall>i. y$i \<in> A i) \<longrightarrow> y \<in> S))"
   320 
   321 instance%unimportant proof
   322   show "open (UNIV :: ('a ^ 'b) set)"
   323     unfolding open_vec_def by auto
   324 next
   325   fix S T :: "('a ^ 'b) set"
   326   assume "open S" "open T" thus "open (S \<inter> T)"
   327     unfolding open_vec_def
   328     apply clarify
   329     apply (drule (1) bspec)+
   330     apply (clarify, rename_tac Sa Ta)
   331     apply (rule_tac x="\<lambda>i. Sa i \<inter> Ta i" in exI)
   332     apply (simp add: open_Int)
   333     done
   334 next
   335   fix K :: "('a ^ 'b) set set"
   336   assume "\<forall>S\<in>K. open S" thus "open (\<Union>K)"
   337     unfolding open_vec_def
   338     apply clarify
   339     apply (drule (1) bspec)
   340     apply (drule (1) bspec)
   341     apply clarify
   342     apply (rule_tac x=A in exI)
   343     apply fast
   344     done
   345 qed
   346 
   347 end
   348 
   349 lemma open_vector_box: "\<forall>i. open (S i) \<Longrightarrow> open {x. \<forall>i. x $ i \<in> S i}"
   350   unfolding open_vec_def by auto
   351 
   352 lemma open_vimage_vec_nth: "open S \<Longrightarrow> open ((\<lambda>x. x $ i) -` S)"
   353   unfolding open_vec_def
   354   apply clarify
   355   apply (rule_tac x="\<lambda>k. if k = i then S else UNIV" in exI, simp)
   356   done
   357 
   358 lemma closed_vimage_vec_nth: "closed S \<Longrightarrow> closed ((\<lambda>x. x $ i) -` S)"
   359   unfolding closed_open vimage_Compl [symmetric]
   360   by (rule open_vimage_vec_nth)
   361 
   362 lemma closed_vector_box: "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
   363 proof -
   364   have "{x. \<forall>i. x $ i \<in> S i} = (\<Inter>i. (\<lambda>x. x $ i) -` S i)" by auto
   365   thus "\<forall>i. closed (S i) \<Longrightarrow> closed {x. \<forall>i. x $ i \<in> S i}"
   366     by (simp add: closed_INT closed_vimage_vec_nth)
   367 qed
   368 
   369 lemma tendsto_vec_nth [tendsto_intros]:
   370   assumes "((\<lambda>x. f x) \<longlongrightarrow> a) net"
   371   shows "((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
   372 proof (rule topological_tendstoI)
   373   fix S assume "open S" "a $ i \<in> S"
   374   then have "open ((\<lambda>y. y $ i) -` S)" "a \<in> ((\<lambda>y. y $ i) -` S)"
   375     by (simp_all add: open_vimage_vec_nth)
   376   with assms have "eventually (\<lambda>x. f x \<in> (\<lambda>y. y $ i) -` S) net"
   377     by (rule topological_tendstoD)
   378   then show "eventually (\<lambda>x. f x $ i \<in> S) net"
   379     by simp
   380 qed
   381 
   382 lemma isCont_vec_nth [simp]: "isCont f a \<Longrightarrow> isCont (\<lambda>x. f x $ i) a"
   383   unfolding isCont_def by (rule tendsto_vec_nth)
   384 
   385 lemma vec_tendstoI:
   386   assumes "\<And>i. ((\<lambda>x. f x $ i) \<longlongrightarrow> a $ i) net"
   387   shows "((\<lambda>x. f x) \<longlongrightarrow> a) net"
   388 proof (rule topological_tendstoI)
   389   fix S assume "open S" and "a \<in> S"
   390   then obtain A where A: "\<And>i. open (A i)" "\<And>i. a $ i \<in> A i"
   391     and S: "\<And>y. \<forall>i. y $ i \<in> A i \<Longrightarrow> y \<in> S"
   392     unfolding open_vec_def by metis
   393   have "\<And>i. eventually (\<lambda>x. f x $ i \<in> A i) net"
   394     using assms A by (rule topological_tendstoD)
   395   hence "eventually (\<lambda>x. \<forall>i. f x $ i \<in> A i) net"
   396     by (rule eventually_all_finite)
   397   thus "eventually (\<lambda>x. f x \<in> S) net"
   398     by (rule eventually_mono, simp add: S)
   399 qed
   400 
   401 lemma tendsto_vec_lambda [tendsto_intros]:
   402   assumes "\<And>i. ((\<lambda>x. f x i) \<longlongrightarrow> a i) net"
   403   shows "((\<lambda>x. \<chi> i. f x i) \<longlongrightarrow> (\<chi> i. a i)) net"
   404   using assms by (simp add: vec_tendstoI)
   405 
   406 lemma open_image_vec_nth: assumes "open S" shows "open ((\<lambda>x. x $ i) ` S)"
   407 proof (rule openI)
   408   fix a assume "a \<in> (\<lambda>x. x $ i) ` S"
   409   then obtain z where "a = z $ i" and "z \<in> S" ..
   410   then obtain A where A: "\<forall>i. open (A i) \<and> z $ i \<in> A i"
   411     and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
   412     using \<open>open S\<close> unfolding open_vec_def by auto
   413   hence "A i \<subseteq> (\<lambda>x. x $ i) ` S"
   414     by (clarsimp, rule_tac x="\<chi> j. if j = i then x else z $ j" in image_eqI,
   415       simp_all)
   416   hence "open (A i) \<and> a \<in> A i \<and> A i \<subseteq> (\<lambda>x. x $ i) ` S"
   417     using A \<open>a = z $ i\<close> by simp
   418   then show "\<exists>T. open T \<and> a \<in> T \<and> T \<subseteq> (\<lambda>x. x $ i) ` S" by - (rule exI)
   419 qed
   420 
   421 instance%unimportant vec :: (perfect_space, finite) perfect_space
   422 proof
   423   fix x :: "'a ^ 'b" show "\<not> open {x}"
   424   proof
   425     assume "open {x}"
   426     hence "\<forall>i. open ((\<lambda>x. x $ i) ` {x})" by (fast intro: open_image_vec_nth)
   427     hence "\<forall>i. open {x $ i}" by simp
   428     thus "False" by (simp add: not_open_singleton)
   429   qed
   430 qed
   431 
   432 
   433 subsection \<open>Metric space\<close>
   434 (* TODO: Product of uniform spaces and compatibility with metric_spaces! *)
   435 
   436 instantiation%unimportant vec :: (metric_space, finite) dist
   437 begin
   438 
   439 definition%important
   440   "dist x y = L2_set (\<lambda>i. dist (x$i) (y$i)) UNIV"
   441 
   442 instance ..
   443 end
   444 
   445 instantiation%unimportant vec :: (metric_space, finite) uniformity_dist
   446 begin
   447 
   448 definition%important [code del]:
   449   "(uniformity :: (('a^'b::_) \<times> ('a^'b::_)) filter) =
   450     (INF e\<in>{0 <..}. principal {(x, y). dist x y < e})"
   451 
   452 instance%unimportant
   453   by standard (rule uniformity_vec_def)
   454 end
   455 
   456 declare uniformity_Abort[where 'a="'a :: metric_space ^ 'b :: finite", code]
   457 
   458 instantiation%unimportant vec :: (metric_space, finite) metric_space
   459 begin
   460 
   461 proposition dist_vec_nth_le: "dist (x $ i) (y $ i) \<le> dist x y"
   462   unfolding dist_vec_def by (rule member_le_L2_set) simp_all
   463 
   464 instance proof
   465   fix x y :: "'a ^ 'b"
   466   show "dist x y = 0 \<longleftrightarrow> x = y"
   467     unfolding dist_vec_def
   468     by (simp add: L2_set_eq_0_iff vec_eq_iff)
   469 next
   470   fix x y z :: "'a ^ 'b"
   471   show "dist x y \<le> dist x z + dist y z"
   472     unfolding dist_vec_def
   473     apply (rule order_trans [OF _ L2_set_triangle_ineq])
   474     apply (simp add: L2_set_mono dist_triangle2)
   475     done
   476 next
   477   fix S :: "('a ^ 'b) set"
   478   have *: "open S \<longleftrightarrow> (\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S)"
   479   proof
   480     assume "open S" show "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   481     proof
   482       fix x assume "x \<in> S"
   483       obtain A where A: "\<forall>i. open (A i)" "\<forall>i. x $ i \<in> A i"
   484         and S: "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
   485         using \<open>open S\<close> and \<open>x \<in> S\<close> unfolding open_vec_def by metis
   486       have "\<forall>i\<in>UNIV. \<exists>r>0. \<forall>y. dist y (x $ i) < r \<longrightarrow> y \<in> A i"
   487         using A unfolding open_dist by simp
   488       hence "\<exists>r. \<forall>i\<in>UNIV. 0 < r i \<and> (\<forall>y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i)"
   489         by (rule finite_set_choice [OF finite])
   490       then obtain r where r1: "\<forall>i. 0 < r i"
   491         and r2: "\<forall>i y. dist y (x $ i) < r i \<longrightarrow> y \<in> A i" by fast
   492       have "0 < Min (range r) \<and> (\<forall>y. dist y x < Min (range r) \<longrightarrow> y \<in> S)"
   493         by (simp add: r1 r2 S le_less_trans [OF dist_vec_nth_le])
   494       thus "\<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" ..
   495     qed
   496   next
   497     assume *: "\<forall>x\<in>S. \<exists>e>0. \<forall>y. dist y x < e \<longrightarrow> y \<in> S" show "open S"
   498     proof (unfold open_vec_def, rule)
   499       fix x assume "x \<in> S"
   500       then obtain e where "0 < e" and S: "\<forall>y. dist y x < e \<longrightarrow> y \<in> S"
   501         using * by fast
   502       define r where [abs_def]: "r i = e / sqrt (of_nat CARD('b))" for i :: 'b
   503       from \<open>0 < e\<close> have r: "\<forall>i. 0 < r i"
   504         unfolding r_def by simp_all
   505       from \<open>0 < e\<close> have e: "e = L2_set r UNIV"
   506         unfolding r_def by (simp add: L2_set_constant)
   507       define A where "A i = {y. dist (x $ i) y < r i}" for i
   508       have "\<forall>i. open (A i) \<and> x $ i \<in> A i"
   509         unfolding A_def by (simp add: open_ball r)
   510       moreover have "\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S"
   511         by (simp add: A_def S dist_vec_def e L2_set_strict_mono dist_commute)
   512       ultimately show "\<exists>A. (\<forall>i. open (A i) \<and> x $ i \<in> A i) \<and>
   513         (\<forall>y. (\<forall>i. y $ i \<in> A i) \<longrightarrow> y \<in> S)" by metis
   514     qed
   515   qed
   516   show "open S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
   517     unfolding * eventually_uniformity_metric
   518     by (simp del: split_paired_All add: dist_vec_def dist_commute)
   519 qed
   520 
   521 end
   522 
   523 lemma Cauchy_vec_nth:
   524   "Cauchy (\<lambda>n. X n) \<Longrightarrow> Cauchy (\<lambda>n. X n $ i)"
   525   unfolding Cauchy_def by (fast intro: le_less_trans [OF dist_vec_nth_le])
   526 
   527 lemma vec_CauchyI:
   528   fixes X :: "nat \<Rightarrow> 'a::metric_space ^ 'n"
   529   assumes X: "\<And>i. Cauchy (\<lambda>n. X n $ i)"
   530   shows "Cauchy (\<lambda>n. X n)"
   531 proof (rule metric_CauchyI)
   532   fix r :: real assume "0 < r"
   533   hence "0 < r / of_nat CARD('n)" (is "0 < ?s") by simp
   534   define N where "N i = (LEAST N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s)" for i
   535   define M where "M = Max (range N)"
   536   have "\<And>i. \<exists>N. \<forall>m\<ge>N. \<forall>n\<ge>N. dist (X m $ i) (X n $ i) < ?s"
   537     using X \<open>0 < ?s\<close> by (rule metric_CauchyD)
   538   hence "\<And>i. \<forall>m\<ge>N i. \<forall>n\<ge>N i. dist (X m $ i) (X n $ i) < ?s"
   539     unfolding N_def by (rule LeastI_ex)
   540   hence M: "\<And>i. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m $ i) (X n $ i) < ?s"
   541     unfolding M_def by simp
   542   {
   543     fix m n :: nat
   544     assume "M \<le> m" "M \<le> n"
   545     have "dist (X m) (X n) = L2_set (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   546       unfolding dist_vec_def ..
   547     also have "\<dots> \<le> sum (\<lambda>i. dist (X m $ i) (X n $ i)) UNIV"
   548       by (rule L2_set_le_sum [OF zero_le_dist])
   549     also have "\<dots> < sum (\<lambda>i::'n. ?s) UNIV"
   550       by (rule sum_strict_mono, simp_all add: M \<open>M \<le> m\<close> \<open>M \<le> n\<close>)
   551     also have "\<dots> = r"
   552       by simp
   553     finally have "dist (X m) (X n) < r" .
   554   }
   555   hence "\<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r"
   556     by simp
   557   then show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (X m) (X n) < r" ..
   558 qed
   559 
   560 instance%unimportant vec :: (complete_space, finite) complete_space
   561 proof
   562   fix X :: "nat \<Rightarrow> 'a ^ 'b" assume "Cauchy X"
   563   have "\<And>i. (\<lambda>n. X n $ i) \<longlonglongrightarrow> lim (\<lambda>n. X n $ i)"
   564     using Cauchy_vec_nth [OF \<open>Cauchy X\<close>]
   565     by (simp add: Cauchy_convergent_iff convergent_LIMSEQ_iff)
   566   hence "X \<longlonglongrightarrow> vec_lambda (\<lambda>i. lim (\<lambda>n. X n $ i))"
   567     by (simp add: vec_tendstoI)
   568   then show "convergent X"
   569     by (rule convergentI)
   570 qed
   571 
   572 
   573 subsection \<open>Normed vector space\<close>
   574 
   575 instantiation%unimportant vec :: (real_normed_vector, finite) real_normed_vector
   576 begin
   577 
   578 definition%important "norm x = L2_set (\<lambda>i. norm (x$i)) UNIV"
   579 
   580 definition%important "sgn (x::'a^'b) = scaleR (inverse (norm x)) x"
   581 
   582 instance%unimportant proof
   583   fix a :: real and x y :: "'a ^ 'b"
   584   show "norm x = 0 \<longleftrightarrow> x = 0"
   585     unfolding norm_vec_def
   586     by (simp add: L2_set_eq_0_iff vec_eq_iff)
   587   show "norm (x + y) \<le> norm x + norm y"
   588     unfolding norm_vec_def
   589     apply (rule order_trans [OF _ L2_set_triangle_ineq])
   590     apply (simp add: L2_set_mono norm_triangle_ineq)
   591     done
   592   show "norm (scaleR a x) = \<bar>a\<bar> * norm x"
   593     unfolding norm_vec_def
   594     by (simp add: L2_set_right_distrib)
   595   show "sgn x = scaleR (inverse (norm x)) x"
   596     by (rule sgn_vec_def)
   597   show "dist x y = norm (x - y)"
   598     unfolding dist_vec_def norm_vec_def
   599     by (simp add: dist_norm)
   600 qed
   601 
   602 end
   603 
   604 lemma norm_nth_le: "norm (x $ i) \<le> norm x"
   605 unfolding norm_vec_def
   606 by (rule member_le_L2_set) simp_all
   607 
   608 lemma norm_le_componentwise_cart:
   609   fixes x :: "'a::real_normed_vector^'n"
   610   assumes "\<And>i. norm(x$i) \<le> norm(y$i)"
   611   shows "norm x \<le> norm y"
   612   unfolding%unimportant norm_vec_def
   613   by%unimportant (rule L2_set_mono) (auto simp: assms)
   614 
   615 lemma component_le_norm_cart: "\<bar>x$i\<bar> \<le> norm x"
   616   apply (simp add: norm_vec_def)
   617   apply (rule member_le_L2_set, simp_all)
   618   done
   619 
   620 lemma norm_bound_component_le_cart: "norm x \<le> e ==> \<bar>x$i\<bar> \<le> e"
   621   by (metis component_le_norm_cart order_trans)
   622 
   623 lemma norm_bound_component_lt_cart: "norm x < e ==> \<bar>x$i\<bar> < e"
   624   by (metis component_le_norm_cart le_less_trans)
   625 
   626 lemma norm_le_l1_cart: "norm x \<le> sum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
   627   by (simp add: norm_vec_def L2_set_le_sum)
   628 
   629 lemma bounded_linear_vec_nth[intro]: "bounded_linear (\<lambda>x. x $ i)"
   630 apply standard
   631 apply (rule vector_add_component)
   632 apply (rule vector_scaleR_component)
   633 apply (rule_tac x="1" in exI, simp add: norm_nth_le)
   634 done
   635 
   636 instance vec :: (banach, finite) banach ..
   637 
   638 
   639 subsection \<open>Inner product space\<close>
   640 
   641 instantiation%unimportant vec :: (real_inner, finite) real_inner
   642 begin
   643 
   644 definition%important "inner x y = sum (\<lambda>i. inner (x$i) (y$i)) UNIV"
   645 
   646 instance%unimportant proof
   647   fix r :: real and x y z :: "'a ^ 'b"
   648   show "inner x y = inner y x"
   649     unfolding inner_vec_def
   650     by (simp add: inner_commute)
   651   show "inner (x + y) z = inner x z + inner y z"
   652     unfolding inner_vec_def
   653     by (simp add: inner_add_left sum.distrib)
   654   show "inner (scaleR r x) y = r * inner x y"
   655     unfolding inner_vec_def
   656     by (simp add: sum_distrib_left)
   657   show "0 \<le> inner x x"
   658     unfolding inner_vec_def
   659     by (simp add: sum_nonneg)
   660   show "inner x x = 0 \<longleftrightarrow> x = 0"
   661     unfolding inner_vec_def
   662     by (simp add: vec_eq_iff sum_nonneg_eq_0_iff)
   663   show "norm x = sqrt (inner x x)"
   664     unfolding inner_vec_def norm_vec_def L2_set_def
   665     by (simp add: power2_norm_eq_inner)
   666 qed
   667 
   668 end
   669 
   670 
   671 subsection \<open>Euclidean space\<close>
   672 
   673 text \<open>Vectors pointing along a single axis.\<close>
   674 
   675 definition%important "axis k x = (\<chi> i. if i = k then x else 0)"
   676 
   677 lemma axis_nth [simp]: "axis i x $ i = x"
   678   unfolding axis_def by simp
   679 
   680 lemma axis_eq_axis: "axis i x = axis j y \<longleftrightarrow> x = y \<and> i = j \<or> x = 0 \<and> y = 0"
   681   unfolding axis_def vec_eq_iff by auto
   682 
   683 lemma inner_axis_axis:
   684   "inner (axis i x) (axis j y) = (if i = j then inner x y else 0)"
   685   unfolding inner_vec_def
   686   apply (cases "i = j")
   687   apply clarsimp
   688   apply (subst sum.remove [of _ j], simp_all)
   689   apply (rule sum.neutral, simp add: axis_def)
   690   apply (rule sum.neutral, simp add: axis_def)
   691   done
   692 
   693 lemma inner_axis: "inner x (axis i y) = inner (x $ i) y"
   694   by (simp add: inner_vec_def axis_def sum.remove [where x=i])
   695 
   696 lemma inner_axis': "inner(axis i y) x = inner y (x $ i)"
   697   by (simp add: inner_axis inner_commute)
   698 
   699 instantiation%unimportant vec :: (euclidean_space, finite) euclidean_space
   700 begin
   701 
   702 definition%important "Basis = (\<Union>i. \<Union>u\<in>Basis. {axis i u})"
   703 
   704 instance%unimportant proof
   705   show "(Basis :: ('a ^ 'b) set) \<noteq> {}"
   706     unfolding Basis_vec_def by simp
   707 next
   708   show "finite (Basis :: ('a ^ 'b) set)"
   709     unfolding Basis_vec_def by simp
   710 next
   711   fix u v :: "'a ^ 'b"
   712   assume "u \<in> Basis" and "v \<in> Basis"
   713   thus "inner u v = (if u = v then 1 else 0)"
   714     unfolding Basis_vec_def
   715     by (auto simp add: inner_axis_axis axis_eq_axis inner_Basis)
   716 next
   717   fix x :: "'a ^ 'b"
   718   show "(\<forall>u\<in>Basis. inner x u = 0) \<longleftrightarrow> x = 0"
   719     unfolding Basis_vec_def
   720     by (simp add: inner_axis euclidean_all_zero_iff vec_eq_iff)
   721 qed
   722 
   723 proposition DIM_cart [simp]: "DIM('a^'b) = CARD('b) * DIM('a)"
   724 proof -
   725   have "card (\<Union>i::'b. \<Union>u::'a\<in>Basis. {axis i u}) = (\<Sum>i::'b\<in>UNIV. card (\<Union>u::'a\<in>Basis. {axis i u}))"
   726     by (rule card_UN_disjoint) (auto simp: axis_eq_axis) 
   727   also have "... = CARD('b) * DIM('a)"
   728     by (subst card_UN_disjoint) (auto simp: axis_eq_axis)
   729   finally show ?thesis
   730     by (simp add: Basis_vec_def)
   731 qed
   732 
   733 end
   734 
   735 lemma norm_axis_1 [simp]: "norm (axis m (1::real)) = 1"
   736   by (simp add: inner_axis' norm_eq_1)
   737 
   738 lemma sum_norm_allsubsets_bound_cart:
   739   fixes f:: "'a \<Rightarrow> real ^'n"
   740   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (sum f Q) \<le> e"
   741   shows "sum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
   742   using sum_norm_allsubsets_bound[OF assms]
   743   by simp
   744 
   745 lemma cart_eq_inner_axis: "a $ i = inner a (axis i 1)"
   746   by (simp add: inner_axis)
   747 
   748 lemma axis_eq_0_iff [simp]:
   749   shows "axis m x = 0 \<longleftrightarrow> x = 0"
   750   by (simp add: axis_def vec_eq_iff)
   751 
   752 lemma axis_in_Basis_iff [simp]: "axis i a \<in> Basis \<longleftrightarrow> a \<in> Basis"
   753   by (auto simp: Basis_vec_def axis_eq_axis)
   754 
   755 text\<open>Mapping each basis element to the corresponding finite index\<close>
   756 definition axis_index :: "('a::comm_ring_1)^'n \<Rightarrow> 'n" where "axis_index v \<equiv> SOME i. v = axis i 1"
   757 
   758 lemma axis_inverse:
   759   fixes v :: "real^'n"
   760   assumes "v \<in> Basis"
   761   shows "\<exists>i. v = axis i 1"
   762 proof -
   763   have "v \<in> (\<Union>n. \<Union>r\<in>Basis. {axis n r})"
   764     using assms Basis_vec_def by blast
   765   then show ?thesis
   766     by (force simp add: vec_eq_iff)
   767 qed
   768 
   769 lemma axis_index:
   770   fixes v :: "real^'n"
   771   assumes "v \<in> Basis"
   772   shows "v = axis (axis_index v) 1"
   773   by (metis (mono_tags) assms axis_inverse axis_index_def someI_ex)
   774 
   775 lemma axis_index_axis [simp]:
   776   fixes UU :: "real^'n"
   777   shows "(axis_index (axis u 1 :: real^'n)) = (u::'n)"
   778   by (simp add: axis_eq_axis axis_index_def)
   779 
   780 subsection%unimportant \<open>A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space\<close>
   781 
   782 lemma sum_cong_aux:
   783   "(\<And>x. x \<in> A \<Longrightarrow> f x = g x) \<Longrightarrow> sum f A = sum g A"
   784   by (auto intro: sum.cong)
   785 
   786 hide_fact (open) sum_cong_aux
   787 
   788 method_setup vector = \<open>
   789 let
   790   val ss1 =
   791     simpset_of (put_simpset HOL_basic_ss \<^context>
   792       addsimps [@{thm sum.distrib} RS sym,
   793       @{thm sum_subtractf} RS sym, @{thm sum_distrib_left},
   794       @{thm sum_distrib_right}, @{thm sum_negf} RS sym])
   795   val ss2 =
   796     simpset_of (\<^context> addsimps
   797              [@{thm plus_vec_def}, @{thm times_vec_def},
   798               @{thm minus_vec_def}, @{thm uminus_vec_def},
   799               @{thm one_vec_def}, @{thm zero_vec_def}, @{thm vec_def},
   800               @{thm scaleR_vec_def},
   801               @{thm vec_lambda_beta}, @{thm vector_scalar_mult_def}])
   802   fun vector_arith_tac ctxt ths =
   803     simp_tac (put_simpset ss1 ctxt)
   804     THEN' (fn i => resolve_tac ctxt @{thms Finite_Cartesian_Product.sum_cong_aux} i
   805          ORELSE resolve_tac ctxt @{thms sum.neutral} i
   806          ORELSE simp_tac (put_simpset HOL_basic_ss ctxt addsimps [@{thm vec_eq_iff}]) i)
   807     (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
   808     THEN' asm_full_simp_tac (put_simpset ss2 ctxt addsimps ths)
   809 in
   810   Attrib.thms >> (fn ths => fn ctxt => SIMPLE_METHOD' (vector_arith_tac ctxt ths))
   811 end
   812 \<close> "lift trivial vector statements to real arith statements"
   813 
   814 lemma vec_0[simp]: "vec 0 = 0" by vector
   815 lemma vec_1[simp]: "vec 1 = 1" by vector
   816 
   817 lemma vec_inj[simp]: "vec x = vec y \<longleftrightarrow> x = y" by vector
   818 
   819 lemma vec_in_image_vec: "vec x \<in> (vec ` S) \<longleftrightarrow> x \<in> S" by auto
   820 
   821 lemma vec_add: "vec(x + y) = vec x + vec y"  by vector
   822 lemma vec_sub: "vec(x - y) = vec x - vec y" by vector
   823 lemma vec_cmul: "vec(c * x) = c *s vec x " by vector
   824 lemma vec_neg: "vec(- x) = - vec x " by vector
   825 
   826 lemma vec_scaleR: "vec(c * x) = c *\<^sub>R vec x"
   827   by vector
   828 
   829 lemma vec_sum:
   830   assumes "finite S"
   831   shows "vec(sum f S) = sum (vec \<circ> f) S"
   832   using assms
   833 proof induct
   834   case empty
   835   then show ?case by simp
   836 next
   837   case insert
   838   then show ?case by (auto simp add: vec_add)
   839 qed
   840 
   841 text\<open>Obvious "component-pushing".\<close>
   842 
   843 lemma vec_component [simp]: "vec x $ i = x"
   844   by vector
   845 
   846 lemma vector_mult_component [simp]: "(x * y)$i = x$i * y$i"
   847   by vector
   848 
   849 lemma vector_smult_component [simp]: "(c *s y)$i = c * (y$i)"
   850   by vector
   851 
   852 lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
   853 
   854 lemmas%unimportant vector_component =
   855   vec_component vector_add_component vector_mult_component
   856   vector_smult_component vector_minus_component vector_uminus_component
   857   vector_scaleR_component cond_component
   858 
   859 
   860 subsection%unimportant \<open>Some frequently useful arithmetic lemmas over vectors\<close>
   861 
   862 instance vec :: (semigroup_mult, finite) semigroup_mult
   863   by standard (vector mult.assoc)
   864 
   865 instance vec :: (monoid_mult, finite) monoid_mult
   866   by standard vector+
   867 
   868 instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult
   869   by standard (vector mult.commute)
   870 
   871 instance vec :: (comm_monoid_mult, finite) comm_monoid_mult
   872   by standard vector
   873 
   874 instance vec :: (semiring, finite) semiring
   875   by standard (vector field_simps)+
   876 
   877 instance vec :: (semiring_0, finite) semiring_0
   878   by standard (vector field_simps)+
   879 instance vec :: (semiring_1, finite) semiring_1
   880   by standard vector
   881 instance vec :: (comm_semiring, finite) comm_semiring
   882   by standard (vector field_simps)+
   883 
   884 instance vec :: (comm_semiring_0, finite) comm_semiring_0 ..
   885 instance vec :: (semiring_0_cancel, finite) semiring_0_cancel ..
   886 instance vec :: (comm_semiring_0_cancel, finite) comm_semiring_0_cancel ..
   887 instance vec :: (ring, finite) ring ..
   888 instance vec :: (semiring_1_cancel, finite) semiring_1_cancel ..
   889 instance vec :: (comm_semiring_1, finite) comm_semiring_1 ..
   890 
   891 instance vec :: (ring_1, finite) ring_1 ..
   892 
   893 instance vec :: (real_algebra, finite) real_algebra
   894   by standard (simp_all add: vec_eq_iff)
   895 
   896 instance vec :: (real_algebra_1, finite) real_algebra_1 ..
   897 
   898 lemma of_nat_index: "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   899 proof (induct n)
   900   case 0
   901   then show ?case by vector
   902 next
   903   case Suc
   904   then show ?case by vector
   905 qed
   906 
   907 lemma one_index [simp]: "(1 :: 'a :: one ^ 'n) $ i = 1"
   908   by vector
   909 
   910 lemma neg_one_index [simp]: "(- 1 :: 'a :: {one, uminus} ^ 'n) $ i = - 1"
   911   by vector
   912 
   913 instance vec :: (semiring_char_0, finite) semiring_char_0
   914 proof
   915   fix m n :: nat
   916   show "inj (of_nat :: nat \<Rightarrow> 'a ^ 'b)"
   917     by (auto intro!: injI simp add: vec_eq_iff of_nat_index)
   918 qed
   919 
   920 instance vec :: (numeral, finite) numeral ..
   921 instance vec :: (semiring_numeral, finite) semiring_numeral ..
   922 
   923 lemma numeral_index [simp]: "numeral w $ i = numeral w"
   924   by (induct w) (simp_all only: numeral.simps vector_add_component one_index)
   925 
   926 lemma neg_numeral_index [simp]: "- numeral w $ i = - numeral w"
   927   by (simp only: vector_uminus_component numeral_index)
   928 
   929 instance vec :: (comm_ring_1, finite) comm_ring_1 ..
   930 instance vec :: (ring_char_0, finite) ring_char_0 ..
   931 
   932 lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
   933   by (vector mult.assoc)
   934 lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
   935   by (vector field_simps)
   936 lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
   937   by (vector field_simps)
   938 lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
   939 lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
   940 lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
   941   by (vector field_simps)
   942 lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
   943 lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
   944 lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector
   945 lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
   946 lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
   947   by (vector field_simps)
   948 
   949 lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
   950   by (simp add: vec_eq_iff)
   951 
   952 lemma Vector_Spaces_linear_vec [simp]: "Vector_Spaces.linear (*) vector_scalar_mult vec"
   953   by unfold_locales (vector algebra_simps)+
   954 
   955 lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
   956   by vector
   957 
   958 lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::'a::field) \<or> x = y"
   959   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
   960 
   961 lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::'a::field) = b \<or> x = 0"
   962   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
   963 
   964 lemma scalar_mult_eq_scaleR [abs_def]: "c *s x = c *\<^sub>R x"
   965   unfolding scaleR_vec_def vector_scalar_mult_def by simp
   966 
   967 lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
   968   unfolding dist_norm scalar_mult_eq_scaleR
   969   unfolding scaleR_right_diff_distrib[symmetric] by simp
   970 
   971 lemma sum_component [simp]:
   972   fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
   973   shows "(sum f S)$i = sum (\<lambda>x. (f x)$i) S"
   974 proof (cases "finite S")
   975   case True
   976   then show ?thesis by induct simp_all
   977 next
   978   case False
   979   then show ?thesis by simp
   980 qed
   981 
   982 lemma sum_eq: "sum f S = (\<chi> i. sum (\<lambda>x. (f x)$i ) S)"
   983   by (simp add: vec_eq_iff)
   984 
   985 lemma sum_cmul:
   986   fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
   987   shows "sum (\<lambda>x. c *s f x) S = c *s sum f S"
   988   by (simp add: vec_eq_iff sum_distrib_left)
   989 
   990 lemma linear_vec [simp]: "linear vec"
   991   using Vector_Spaces_linear_vec
   992   apply (auto )
   993   by unfold_locales (vector algebra_simps)+
   994 
   995 subsection \<open>Matrix operations\<close>
   996 
   997 text\<open>Matrix notation. NB: an MxN matrix is of type \<^typ>\<open>'a^'n^'m\<close>, not \<^typ>\<open>'a^'m^'n\<close>\<close>
   998 
   999 definition%important map_matrix::"('a \<Rightarrow> 'b) \<Rightarrow> (('a, 'i::finite)vec, 'j::finite) vec \<Rightarrow> (('b, 'i)vec, 'j) vec" where
  1000   "map_matrix f x = (\<chi> i j. f (x $ i $ j))"
  1001 
  1002 lemma nth_map_matrix[simp]: "map_matrix f x $ i $ j = f (x $ i $ j)"
  1003   by (simp add: map_matrix_def)
  1004 
  1005 definition%important matrix_matrix_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"
  1006     (infixl "**" 70)
  1007   where "m ** m' == (\<chi> i j. sum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
  1008 
  1009 definition%important matrix_vector_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"
  1010     (infixl "*v" 70)
  1011   where "m *v x \<equiv> (\<chi> i. sum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
  1012 
  1013 definition%important vector_matrix_mult :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "
  1014     (infixl "v*" 70)
  1015   where "v v* m == (\<chi> j. sum (\<lambda>i. ((m$i)$j) * (v$i)) (UNIV :: 'm set)) :: 'a^'n"
  1016 
  1017 definition%unimportant "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
  1018 definition%unimportant transpose where
  1019   "(transpose::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
  1020 definition%unimportant "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
  1021 definition%unimportant "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
  1022 definition%unimportant "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
  1023 definition%unimportant "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
  1024 
  1025 lemma times0_left [simp]: "(0::'a::semiring_1^'n^'m) ** (A::'a ^'p^'n) = 0" 
  1026   by (simp add: matrix_matrix_mult_def zero_vec_def)
  1027 
  1028 lemma times0_right [simp]: "(A::'a::semiring_1^'n^'m) ** (0::'a ^'p^'n) = 0" 
  1029   by (simp add: matrix_matrix_mult_def zero_vec_def)
  1030 
  1031 lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
  1032 lemma matrix_add_ldistrib: "(A ** (B + C)) = (A ** B) + (A ** C)"
  1033   by (vector matrix_matrix_mult_def sum.distrib[symmetric] field_simps)
  1034 
  1035 lemma matrix_mul_lid [simp]:
  1036   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
  1037   shows "mat 1 ** A = A"
  1038   apply (simp add: matrix_matrix_mult_def mat_def)
  1039   apply vector
  1040   apply (auto simp only: if_distrib if_distribR sum.delta'[OF finite]
  1041     mult_1_left mult_zero_left if_True UNIV_I)
  1042   done
  1043 
  1044 lemma matrix_mul_rid [simp]:
  1045   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
  1046   shows "A ** mat 1 = A"
  1047   apply (simp add: matrix_matrix_mult_def mat_def)
  1048   apply vector
  1049   apply (auto simp only: if_distrib if_distribR sum.delta[OF finite]
  1050     mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
  1051   done
  1052 
  1053 proposition matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
  1054   apply (vector matrix_matrix_mult_def sum_distrib_left sum_distrib_right mult.assoc)
  1055   apply (subst sum.swap)
  1056   apply simp
  1057   done
  1058 
  1059 proposition matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
  1060   apply (vector matrix_matrix_mult_def matrix_vector_mult_def
  1061     sum_distrib_left sum_distrib_right mult.assoc)
  1062   apply (subst sum.swap)
  1063   apply simp
  1064   done
  1065 
  1066 proposition scalar_matrix_assoc:
  1067   fixes A :: "('a::real_algebra_1)^'m^'n"
  1068   shows "k *\<^sub>R (A ** B) = (k *\<^sub>R A) ** B"
  1069   by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff scaleR_sum_right)
  1070 
  1071 proposition matrix_scalar_ac:
  1072   fixes A :: "('a::real_algebra_1)^'m^'n"
  1073   shows "A ** (k *\<^sub>R B) = k *\<^sub>R A ** B"
  1074   by (simp add: matrix_matrix_mult_def sum_distrib_left mult_ac vec_eq_iff)
  1075 
  1076 lemma matrix_vector_mul_lid [simp]: "mat 1 *v x = (x::'a::semiring_1 ^ 'n)"
  1077   apply (vector matrix_vector_mult_def mat_def)
  1078   apply (simp add: if_distrib if_distribR sum.delta' cong del: if_weak_cong)
  1079   done
  1080 
  1081 lemma matrix_transpose_mul:
  1082     "transpose(A ** B) = transpose B ** transpose (A::'a::comm_semiring_1^_^_)"
  1083   by (simp add: matrix_matrix_mult_def transpose_def vec_eq_iff mult.commute)
  1084 
  1085 lemma matrix_mult_transpose_dot_column:
  1086   shows "transpose A ** A = (\<chi> i j. inner (column i A) (column j A))"
  1087   by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def column_def inner_vec_def)
  1088 
  1089 lemma matrix_mult_transpose_dot_row:
  1090   shows "A ** transpose A = (\<chi> i j. inner (row i A) (row j A))"
  1091   by (simp add: matrix_matrix_mult_def vec_eq_iff transpose_def row_def inner_vec_def)
  1092 
  1093 lemma matrix_eq:
  1094   fixes A B :: "'a::semiring_1 ^ 'n ^ 'm"
  1095   shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
  1096   apply auto
  1097   apply (subst vec_eq_iff)
  1098   apply clarify
  1099   apply (clarsimp simp add: matrix_vector_mult_def if_distrib if_distribR vec_eq_iff cong del: if_weak_cong)
  1100   apply (erule_tac x="axis ia 1" in allE)
  1101   apply (erule_tac x="i" in allE)
  1102   apply (auto simp add: if_distrib if_distribR axis_def
  1103     sum.delta[OF finite] cong del: if_weak_cong)
  1104   done
  1105 
  1106 lemma matrix_vector_mul_component: "(A *v x)$k = inner (A$k) x"
  1107   by (simp add: matrix_vector_mult_def inner_vec_def)
  1108 
  1109 lemma dot_lmul_matrix: "inner ((x::real ^_) v* A) y = inner x (A *v y)"
  1110   apply (simp add: inner_vec_def matrix_vector_mult_def vector_matrix_mult_def sum_distrib_right sum_distrib_left ac_simps)
  1111   apply (subst sum.swap)
  1112   apply simp
  1113   done
  1114 
  1115 lemma transpose_mat [simp]: "transpose (mat n) = mat n"
  1116   by (vector transpose_def mat_def)
  1117 
  1118 lemma transpose_transpose [simp]: "transpose(transpose A) = A"
  1119   by (vector transpose_def)
  1120 
  1121 lemma row_transpose [simp]: "row i (transpose A) = column i A"
  1122   by (simp add: row_def column_def transpose_def vec_eq_iff)
  1123 
  1124 lemma column_transpose [simp]: "column i (transpose A) = row i A"
  1125   by (simp add: row_def column_def transpose_def vec_eq_iff)
  1126 
  1127 lemma rows_transpose [simp]: "rows(transpose A) = columns A"
  1128   by (auto simp add: rows_def columns_def intro: set_eqI)
  1129 
  1130 lemma columns_transpose [simp]: "columns(transpose A) = rows A"
  1131   by (metis transpose_transpose rows_transpose)
  1132 
  1133 lemma transpose_scalar: "transpose (k *\<^sub>R A) = k *\<^sub>R transpose A"
  1134   unfolding transpose_def
  1135   by (simp add: vec_eq_iff)
  1136 
  1137 lemma transpose_iff [iff]: "transpose A = transpose B \<longleftrightarrow> A = B"
  1138   by (metis transpose_transpose)
  1139 
  1140 lemma matrix_mult_sum:
  1141   "(A::'a::comm_semiring_1^'n^'m) *v x = sum (\<lambda>i. (x$i) *s column i A) (UNIV:: 'n set)"
  1142   by (simp add: matrix_vector_mult_def vec_eq_iff column_def mult.commute)
  1143 
  1144 lemma vector_componentwise:
  1145   "(x::'a::ring_1^'n) = (\<chi> j. \<Sum>i\<in>UNIV. (x$i) * (axis i 1 :: 'a^'n) $ j)"
  1146   by (simp add: axis_def if_distrib sum.If_cases vec_eq_iff)
  1147 
  1148 lemma basis_expansion: "sum (\<lambda>i. (x$i) *s axis i 1) UNIV = (x::('a::ring_1) ^'n)"
  1149   by (auto simp add: axis_def vec_eq_iff if_distrib sum.If_cases cong del: if_weak_cong)
  1150 
  1151 
  1152 text\<open>Correspondence between matrices and linear operators.\<close>
  1153 
  1154 definition%important matrix :: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
  1155   where "matrix f = (\<chi> i j. (f(axis j 1))$i)"
  1156 
  1157 lemma matrix_id_mat_1: "matrix id = mat 1"
  1158   by (simp add: mat_def matrix_def axis_def)
  1159 
  1160 lemma matrix_scaleR: "(matrix ((*\<^sub>R) r)) = mat r"
  1161   by (simp add: mat_def matrix_def axis_def if_distrib cong: if_cong)
  1162 
  1163 lemma matrix_vector_mul_linear[intro, simp]: "linear (\<lambda>x. A *v (x::'a::real_algebra_1 ^ _))"
  1164   by (simp add: linear_iff matrix_vector_mult_def vec_eq_iff field_simps sum_distrib_left
  1165       sum.distrib scaleR_right.sum)
  1166 
  1167 lemma vector_matrix_left_distrib [algebra_simps]:
  1168   shows "(x + y) v* A = x v* A + y v* A"
  1169   unfolding vector_matrix_mult_def
  1170   by (simp add: algebra_simps sum.distrib vec_eq_iff)
  1171 
  1172 lemma matrix_vector_right_distrib [algebra_simps]:
  1173   "A *v (x + y) = A *v x + A *v y"
  1174   by (vector matrix_vector_mult_def sum.distrib distrib_left)
  1175 
  1176 lemma matrix_vector_mult_diff_distrib [algebra_simps]:
  1177   fixes A :: "'a::ring_1^'n^'m"
  1178   shows "A *v (x - y) = A *v x - A *v y"
  1179   by (vector matrix_vector_mult_def sum_subtractf right_diff_distrib)
  1180 
  1181 lemma matrix_vector_mult_scaleR[algebra_simps]:
  1182   fixes A :: "real^'n^'m"
  1183   shows "A *v (c *\<^sub>R x) = c *\<^sub>R (A *v x)"
  1184   using linear_iff matrix_vector_mul_linear by blast
  1185 
  1186 lemma matrix_vector_mult_0_right [simp]: "A *v 0 = 0"
  1187   by (simp add: matrix_vector_mult_def vec_eq_iff)
  1188 
  1189 lemma matrix_vector_mult_0 [simp]: "0 *v w = 0"
  1190   by (simp add: matrix_vector_mult_def vec_eq_iff)
  1191 
  1192 lemma matrix_vector_mult_add_rdistrib [algebra_simps]:
  1193   "(A + B) *v x = (A *v x) + (B *v x)"
  1194   by (vector matrix_vector_mult_def sum.distrib distrib_right)
  1195 
  1196 lemma matrix_vector_mult_diff_rdistrib [algebra_simps]:
  1197   fixes A :: "'a :: ring_1^'n^'m"
  1198   shows "(A - B) *v x = (A *v x) - (B *v x)"
  1199   by (vector matrix_vector_mult_def sum_subtractf left_diff_distrib)
  1200 
  1201 lemma matrix_vector_column:
  1202   "(A::'a::comm_semiring_1^'n^_) *v x = sum (\<lambda>i. (x$i) *s ((transpose A)$i)) (UNIV:: 'n set)"
  1203   by (simp add: matrix_vector_mult_def transpose_def vec_eq_iff mult.commute)
  1204 
  1205 subsection\<open>Inverse matrices  (not necessarily square)\<close>
  1206 
  1207 definition%important
  1208   "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
  1209 
  1210 definition%important
  1211   "matrix_inv(A:: 'a::semiring_1^'n^'m) =
  1212     (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
  1213 
  1214 lemma inj_matrix_vector_mult:
  1215   fixes A::"'a::field^'n^'m"
  1216   assumes "invertible A"
  1217   shows "inj ((*v) A)"
  1218   by (metis assms inj_on_inverseI invertible_def matrix_vector_mul_assoc matrix_vector_mul_lid)
  1219 
  1220 lemma scalar_invertible:
  1221   fixes A :: "('a::real_algebra_1)^'m^'n"
  1222   assumes "k \<noteq> 0" and "invertible A"
  1223   shows "invertible (k *\<^sub>R A)"
  1224 proof -
  1225   obtain A' where "A ** A' = mat 1" and "A' ** A = mat 1"
  1226     using assms unfolding invertible_def by auto
  1227   with \<open>k \<noteq> 0\<close>
  1228   have "(k *\<^sub>R A) ** ((1/k) *\<^sub>R A') = mat 1" "((1/k) *\<^sub>R A') ** (k *\<^sub>R A) = mat 1"
  1229     by (simp_all add: assms matrix_scalar_ac)
  1230   thus "invertible (k *\<^sub>R A)"
  1231     unfolding invertible_def by auto
  1232 qed
  1233 
  1234 proposition scalar_invertible_iff:
  1235   fixes A :: "('a::real_algebra_1)^'m^'n"
  1236   assumes "k \<noteq> 0" and "invertible A"
  1237   shows "invertible (k *\<^sub>R A) \<longleftrightarrow> k \<noteq> 0 \<and> invertible A"
  1238   by (simp add: assms scalar_invertible)
  1239 
  1240 lemma vector_transpose_matrix [simp]: "x v* transpose A = A *v x"
  1241   unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def
  1242   by simp
  1243 
  1244 lemma transpose_matrix_vector [simp]: "transpose A *v x = x v* A"
  1245   unfolding transpose_def vector_matrix_mult_def matrix_vector_mult_def
  1246   by simp
  1247 
  1248 lemma vector_scalar_commute:
  1249   fixes A :: "'a::{field}^'m^'n"
  1250   shows "A *v (c *s x) = c *s (A *v x)"
  1251   by (simp add: vector_scalar_mult_def matrix_vector_mult_def mult_ac sum_distrib_left)
  1252 
  1253 lemma scalar_vector_matrix_assoc:
  1254   fixes k :: "'a::{field}" and x :: "'a::{field}^'n" and A :: "'a^'m^'n"
  1255   shows "(k *s x) v* A = k *s (x v* A)"
  1256   by (metis transpose_matrix_vector vector_scalar_commute)
  1257  
  1258 lemma vector_matrix_mult_0 [simp]: "0 v* A = 0"
  1259   unfolding vector_matrix_mult_def by (simp add: zero_vec_def)
  1260 
  1261 lemma vector_matrix_mult_0_right [simp]: "x v* 0 = 0"
  1262   unfolding vector_matrix_mult_def by (simp add: zero_vec_def)
  1263 
  1264 lemma vector_matrix_mul_rid [simp]:
  1265   fixes v :: "('a::semiring_1)^'n"
  1266   shows "v v* mat 1 = v"
  1267   by (metis matrix_vector_mul_lid transpose_mat vector_transpose_matrix)
  1268 
  1269 lemma scaleR_vector_matrix_assoc:
  1270   fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
  1271   shows "(k *\<^sub>R x) v* A = k *\<^sub>R (x v* A)"
  1272   by (metis matrix_vector_mult_scaleR transpose_matrix_vector)
  1273 
  1274 proposition vector_scaleR_matrix_ac:
  1275   fixes k :: real and x :: "real^'n" and A :: "real^'m^'n"
  1276   shows "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)"
  1277 proof -
  1278   have "x v* (k *\<^sub>R A) = (k *\<^sub>R x) v* A"
  1279     unfolding vector_matrix_mult_def
  1280     by (simp add: algebra_simps)
  1281   with scaleR_vector_matrix_assoc
  1282   show "x v* (k *\<^sub>R A) = k *\<^sub>R (x v* A)"
  1283     by auto
  1284 qed
  1285 
  1286 end