src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy
author hoelzl
Thu Jan 31 11:31:22 2013 +0100 (2013-01-31)
changeset 50998 501200635659
parent 50526 899c9c4e4a4c
child 51489 f738e6dbd844
permissions -rw-r--r--
simplify heine_borel type class
     1 header {*Instanciates the finite cartesian product of euclidean spaces as a euclidean space.*}
     2 
     3 theory Cartesian_Euclidean_Space
     4 imports Finite_Cartesian_Product Integration
     5 begin
     6 
     7 lemma delta_mult_idempotent:
     8   "(if k=a then 1 else (0::'a::semiring_1)) * (if k=a then 1 else 0) = (if k=a then 1 else 0)"
     9   by (cases "k=a") auto
    10 
    11 lemma setsum_Plus:
    12   "\<lbrakk>finite A; finite B\<rbrakk> \<Longrightarrow>
    13     (\<Sum>x\<in>A <+> B. g x) = (\<Sum>x\<in>A. g (Inl x)) + (\<Sum>x\<in>B. g (Inr x))"
    14   unfolding Plus_def
    15   by (subst setsum_Un_disjoint, auto simp add: setsum_reindex)
    16 
    17 lemma setsum_UNIV_sum:
    18   fixes g :: "'a::finite + 'b::finite \<Rightarrow> _"
    19   shows "(\<Sum>x\<in>UNIV. g x) = (\<Sum>x\<in>UNIV. g (Inl x)) + (\<Sum>x\<in>UNIV. g (Inr x))"
    20   apply (subst UNIV_Plus_UNIV [symmetric])
    21   apply (rule setsum_Plus [OF finite finite])
    22   done
    23 
    24 lemma setsum_mult_product:
    25   "setsum h {..<A * B :: nat} = (\<Sum>i\<in>{..<A}. \<Sum>j\<in>{..<B}. h (j + i * B))"
    26   unfolding sumr_group[of h B A, unfolded atLeast0LessThan, symmetric]
    27 proof (rule setsum_cong, simp, rule setsum_reindex_cong)
    28   fix i
    29   show "inj_on (\<lambda>j. j + i * B) {..<B}" by (auto intro!: inj_onI)
    30   show "{i * B..<i * B + B} = (\<lambda>j. j + i * B) ` {..<B}"
    31   proof safe
    32     fix j assume "j \<in> {i * B..<i * B + B}"
    33     then show "j \<in> (\<lambda>j. j + i * B) ` {..<B}"
    34       by (auto intro!: image_eqI[of _ _ "j - i * B"])
    35   qed simp
    36 qed simp
    37 
    38 
    39 subsection{* Basic componentwise operations on vectors. *}
    40 
    41 instantiation vec :: (times, finite) times
    42 begin
    43 
    44 definition "op * \<equiv> (\<lambda> x y.  (\<chi> i. (x$i) * (y$i)))"
    45 instance ..
    46 
    47 end
    48 
    49 instantiation vec :: (one, finite) one
    50 begin
    51 
    52 definition "1 \<equiv> (\<chi> i. 1)"
    53 instance ..
    54 
    55 end
    56 
    57 instantiation vec :: (ord, finite) ord
    58 begin
    59 
    60 definition "x \<le> y \<longleftrightarrow> (\<forall>i. x$i \<le> y$i)"
    61 definition "x < y \<longleftrightarrow> (\<forall>i. x$i < y$i)"
    62 instance ..
    63 
    64 end
    65 
    66 text{* The ordering on one-dimensional vectors is linear. *}
    67 
    68 class cart_one =
    69   assumes UNIV_one: "card (UNIV \<Colon> 'a set) = Suc 0"
    70 begin
    71 
    72 subclass finite
    73 proof
    74   from UNIV_one show "finite (UNIV :: 'a set)"
    75     by (auto intro!: card_ge_0_finite)
    76 qed
    77 
    78 end
    79 
    80 instantiation vec :: (linorder, cart_one) linorder
    81 begin
    82 
    83 instance
    84 proof
    85   obtain a :: 'b where all: "\<And>P. (\<forall>i. P i) \<longleftrightarrow> P a"
    86   proof -
    87     have "card (UNIV :: 'b set) = Suc 0" by (rule UNIV_one)
    88     then obtain b :: 'b where "UNIV = {b}" by (auto iff: card_Suc_eq)
    89     then have "\<And>P. (\<forall>i\<in>UNIV. P i) \<longleftrightarrow> P b" by auto
    90     then show thesis by (auto intro: that)
    91   qed
    92 
    93   note [simp] = less_eq_vec_def less_vec_def all vec_eq_iff field_simps
    94   fix x y z :: "'a^'b::cart_one"
    95   show "x \<le> x" "(x < y) = (x \<le> y \<and> \<not> y \<le> x)" "x \<le> y \<or> y \<le> x" by auto
    96   { assume "x\<le>y" "y\<le>z" then show "x\<le>z" by auto }
    97   { assume "x\<le>y" "y\<le>x" then show "x=y" by auto }
    98 qed
    99 
   100 end
   101 
   102 text{* Constant Vectors *} 
   103 
   104 definition "vec x = (\<chi> i. x)"
   105 
   106 text{* Also the scalar-vector multiplication. *}
   107 
   108 definition vector_scalar_mult:: "'a::times \<Rightarrow> 'a ^ 'n \<Rightarrow> 'a ^ 'n" (infixl "*s" 70)
   109   where "c *s x = (\<chi> i. c * (x$i))"
   110 
   111 
   112 subsection {* A naive proof procedure to lift really trivial arithmetic stuff from the basis of the vector space. *}
   113 
   114 method_setup vector = {*
   115 let
   116   val ss1 = HOL_basic_ss addsimps [@{thm setsum_addf} RS sym,
   117     @{thm setsum_subtractf} RS sym, @{thm setsum_right_distrib},
   118     @{thm setsum_left_distrib}, @{thm setsum_negf} RS sym]
   119   val ss2 = @{simpset} addsimps
   120              [@{thm plus_vec_def}, @{thm times_vec_def},
   121               @{thm minus_vec_def}, @{thm uminus_vec_def},
   122               @{thm one_vec_def}, @{thm zero_vec_def}, @{thm vec_def},
   123               @{thm scaleR_vec_def},
   124               @{thm vec_lambda_beta}, @{thm vector_scalar_mult_def}]
   125   fun vector_arith_tac ths =
   126     simp_tac ss1
   127     THEN' (fn i => rtac @{thm setsum_cong2} i
   128          ORELSE rtac @{thm setsum_0'} i
   129          ORELSE simp_tac (HOL_basic_ss addsimps [@{thm vec_eq_iff}]) i)
   130     (* THEN' TRY o clarify_tac HOL_cs  THEN' (TRY o rtac @{thm iffI}) *)
   131     THEN' asm_full_simp_tac (ss2 addsimps ths)
   132 in
   133   Attrib.thms >> (fn ths => K (SIMPLE_METHOD' (vector_arith_tac ths)))
   134 end
   135 *} "lift trivial vector statements to real arith statements"
   136 
   137 lemma vec_0[simp]: "vec 0 = 0" by (vector zero_vec_def)
   138 lemma vec_1[simp]: "vec 1 = 1" by (vector one_vec_def)
   139 
   140 lemma vec_inj[simp]: "vec x = vec y \<longleftrightarrow> x = y" by vector
   141 
   142 lemma vec_in_image_vec: "vec x \<in> (vec ` S) \<longleftrightarrow> x \<in> S" by auto
   143 
   144 lemma vec_add: "vec(x + y) = vec x + vec y"  by (vector vec_def)
   145 lemma vec_sub: "vec(x - y) = vec x - vec y" by (vector vec_def)
   146 lemma vec_cmul: "vec(c * x) = c *s vec x " by (vector vec_def)
   147 lemma vec_neg: "vec(- x) = - vec x " by (vector vec_def)
   148 
   149 lemma vec_setsum:
   150   assumes "finite S"
   151   shows "vec(setsum f S) = setsum (vec o f) S"
   152   using assms
   153 proof induct
   154   case empty
   155   then show ?case by simp
   156 next
   157   case insert
   158   then show ?case by (auto simp add: vec_add)
   159 qed
   160 
   161 text{* Obvious "component-pushing". *}
   162 
   163 lemma vec_component [simp]: "vec x $ i = x"
   164   by (vector vec_def)
   165 
   166 lemma vector_mult_component [simp]: "(x * y)$i = x$i * y$i"
   167   by vector
   168 
   169 lemma vector_smult_component [simp]: "(c *s y)$i = c * (y$i)"
   170   by vector
   171 
   172 lemma cond_component: "(if b then x else y)$i = (if b then x$i else y$i)" by vector
   173 
   174 lemmas vector_component =
   175   vec_component vector_add_component vector_mult_component
   176   vector_smult_component vector_minus_component vector_uminus_component
   177   vector_scaleR_component cond_component
   178 
   179 
   180 subsection {* Some frequently useful arithmetic lemmas over vectors. *}
   181 
   182 instance vec :: (semigroup_mult, finite) semigroup_mult
   183   by default (vector mult_assoc)
   184 
   185 instance vec :: (monoid_mult, finite) monoid_mult
   186   by default vector+
   187 
   188 instance vec :: (ab_semigroup_mult, finite) ab_semigroup_mult
   189   by default (vector mult_commute)
   190 
   191 instance vec :: (ab_semigroup_idem_mult, finite) ab_semigroup_idem_mult
   192   by default (vector mult_idem)
   193 
   194 instance vec :: (comm_monoid_mult, finite) comm_monoid_mult
   195   by default vector
   196 
   197 instance vec :: (semiring, finite) semiring
   198   by default (vector field_simps)+
   199 
   200 instance vec :: (semiring_0, finite) semiring_0
   201   by default (vector field_simps)+
   202 instance vec :: (semiring_1, finite) semiring_1
   203   by default vector
   204 instance vec :: (comm_semiring, finite) comm_semiring
   205   by default (vector field_simps)+
   206 
   207 instance vec :: (comm_semiring_0, finite) comm_semiring_0 ..
   208 instance vec :: (cancel_comm_monoid_add, finite) cancel_comm_monoid_add ..
   209 instance vec :: (semiring_0_cancel, finite) semiring_0_cancel ..
   210 instance vec :: (comm_semiring_0_cancel, finite) comm_semiring_0_cancel ..
   211 instance vec :: (ring, finite) ring ..
   212 instance vec :: (semiring_1_cancel, finite) semiring_1_cancel ..
   213 instance vec :: (comm_semiring_1, finite) comm_semiring_1 ..
   214 
   215 instance vec :: (ring_1, finite) ring_1 ..
   216 
   217 instance vec :: (real_algebra, finite) real_algebra
   218   by default (simp_all add: vec_eq_iff)
   219 
   220 instance vec :: (real_algebra_1, finite) real_algebra_1 ..
   221 
   222 lemma of_nat_index: "(of_nat n :: 'a::semiring_1 ^'n)$i = of_nat n"
   223 proof (induct n)
   224   case 0
   225   then show ?case by vector
   226 next
   227   case Suc
   228   then show ?case by vector
   229 qed
   230 
   231 lemma one_index[simp]: "(1 :: 'a::one ^'n)$i = 1"
   232   by vector
   233 
   234 instance vec :: (semiring_char_0, finite) semiring_char_0
   235 proof
   236   fix m n :: nat
   237   show "inj (of_nat :: nat \<Rightarrow> 'a ^ 'b)"
   238     by (auto intro!: injI simp add: vec_eq_iff of_nat_index)
   239 qed
   240 
   241 instance vec :: (numeral, finite) numeral ..
   242 instance vec :: (semiring_numeral, finite) semiring_numeral ..
   243 
   244 lemma numeral_index [simp]: "numeral w $ i = numeral w"
   245   by (induct w) (simp_all only: numeral.simps vector_add_component one_index)
   246 
   247 lemma neg_numeral_index [simp]: "neg_numeral w $ i = neg_numeral w"
   248   by (simp only: neg_numeral_def vector_uminus_component numeral_index)
   249 
   250 instance vec :: (comm_ring_1, finite) comm_ring_1 ..
   251 instance vec :: (ring_char_0, finite) ring_char_0 ..
   252 
   253 lemma vector_smult_assoc: "a *s (b *s x) = ((a::'a::semigroup_mult) * b) *s x"
   254   by (vector mult_assoc)
   255 lemma vector_sadd_rdistrib: "((a::'a::semiring) + b) *s x = a *s x + b *s x"
   256   by (vector field_simps)
   257 lemma vector_add_ldistrib: "(c::'a::semiring) *s (x + y) = c *s x + c *s y"
   258   by (vector field_simps)
   259 lemma vector_smult_lzero[simp]: "(0::'a::mult_zero) *s x = 0" by vector
   260 lemma vector_smult_lid[simp]: "(1::'a::monoid_mult) *s x = x" by vector
   261 lemma vector_ssub_ldistrib: "(c::'a::ring) *s (x - y) = c *s x - c *s y"
   262   by (vector field_simps)
   263 lemma vector_smult_rneg: "(c::'a::ring) *s -x = -(c *s x)" by vector
   264 lemma vector_smult_lneg: "- (c::'a::ring) *s x = -(c *s x)" by vector
   265 lemma vector_sneg_minus1: "-x = (-1::'a::ring_1) *s x" by vector
   266 lemma vector_smult_rzero[simp]: "c *s 0 = (0::'a::mult_zero ^ 'n)" by vector
   267 lemma vector_sub_rdistrib: "((a::'a::ring) - b) *s x = a *s x - b *s x"
   268   by (vector field_simps)
   269 
   270 lemma vec_eq[simp]: "(vec m = vec n) \<longleftrightarrow> (m = n)"
   271   by (simp add: vec_eq_iff)
   272 
   273 lemma norm_eq_0_imp: "norm x = 0 ==> x = (0::real ^'n)" by (metis norm_eq_zero)
   274 lemma vector_mul_eq_0[simp]: "(a *s x = 0) \<longleftrightarrow> a = (0::'a::idom) \<or> x = 0"
   275   by vector
   276 lemma vector_mul_lcancel[simp]: "a *s x = a *s y \<longleftrightarrow> a = (0::real) \<or> x = y"
   277   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_ssub_ldistrib)
   278 lemma vector_mul_rcancel[simp]: "a *s x = b *s x \<longleftrightarrow> (a::real) = b \<or> x = 0"
   279   by (metis eq_iff_diff_eq_0 vector_mul_eq_0 vector_sub_rdistrib)
   280 lemma vector_mul_lcancel_imp: "a \<noteq> (0::real) ==>  a *s x = a *s y ==> (x = y)"
   281   by (metis vector_mul_lcancel)
   282 lemma vector_mul_rcancel_imp: "x \<noteq> 0 \<Longrightarrow> (a::real) *s x = b *s x ==> a = b"
   283   by (metis vector_mul_rcancel)
   284 
   285 lemma component_le_norm_cart: "\<bar>x$i\<bar> <= norm x"
   286   apply (simp add: norm_vec_def)
   287   apply (rule member_le_setL2, simp_all)
   288   done
   289 
   290 lemma norm_bound_component_le_cart: "norm x <= e ==> \<bar>x$i\<bar> <= e"
   291   by (metis component_le_norm_cart order_trans)
   292 
   293 lemma norm_bound_component_lt_cart: "norm x < e ==> \<bar>x$i\<bar> < e"
   294   by (metis component_le_norm_cart basic_trans_rules(21))
   295 
   296 lemma norm_le_l1_cart: "norm x <= setsum(\<lambda>i. \<bar>x$i\<bar>) UNIV"
   297   by (simp add: norm_vec_def setL2_le_setsum)
   298 
   299 lemma scalar_mult_eq_scaleR: "c *s x = c *\<^sub>R x"
   300   unfolding scaleR_vec_def vector_scalar_mult_def by simp
   301 
   302 lemma dist_mul[simp]: "dist (c *s x) (c *s y) = \<bar>c\<bar> * dist x y"
   303   unfolding dist_norm scalar_mult_eq_scaleR
   304   unfolding scaleR_right_diff_distrib[symmetric] by simp
   305 
   306 lemma setsum_component [simp]:
   307   fixes f:: " 'a \<Rightarrow> ('b::comm_monoid_add) ^'n"
   308   shows "(setsum f S)$i = setsum (\<lambda>x. (f x)$i) S"
   309 proof (cases "finite S")
   310   case True
   311   then show ?thesis by induct simp_all
   312 next
   313   case False
   314   then show ?thesis by simp
   315 qed
   316 
   317 lemma setsum_eq: "setsum f S = (\<chi> i. setsum (\<lambda>x. (f x)$i ) S)"
   318   by (simp add: vec_eq_iff)
   319 
   320 lemma setsum_cmul:
   321   fixes f:: "'c \<Rightarrow> ('a::semiring_1)^'n"
   322   shows "setsum (\<lambda>x. c *s f x) S = c *s setsum f S"
   323   by (simp add: vec_eq_iff setsum_right_distrib)
   324 
   325 (* TODO: use setsum_norm_allsubsets_bound *)
   326 lemma setsum_norm_allsubsets_bound_cart:
   327   fixes f:: "'a \<Rightarrow> real ^'n"
   328   assumes fP: "finite P" and fPs: "\<And>Q. Q \<subseteq> P \<Longrightarrow> norm (setsum f Q) \<le> e"
   329   shows "setsum (\<lambda>x. norm (f x)) P \<le> 2 * real CARD('n) *  e"
   330   using setsum_norm_allsubsets_bound[OF assms]
   331   by (simp add: DIM_cart Basis_real_def)
   332 
   333 instance vec :: (ordered_euclidean_space, finite) ordered_euclidean_space
   334 proof
   335   fix x y::"'a^'b"
   336   show "(x \<le> y) = (\<forall>i\<in>Basis. x \<bullet> i \<le> y \<bullet> i)"
   337     unfolding less_eq_vec_def apply(subst eucl_le) by (simp add: Basis_vec_def inner_axis)
   338   show"(x < y) = (\<forall>i\<in>Basis. x \<bullet> i < y \<bullet> i)"
   339     unfolding less_vec_def apply(subst eucl_less) by (simp add: Basis_vec_def inner_axis)
   340 qed
   341 
   342 subsection {* Matrix operations *}
   343 
   344 text{* Matrix notation. NB: an MxN matrix is of type @{typ "'a^'n^'m"}, not @{typ "'a^'m^'n"} *}
   345 
   346 definition matrix_matrix_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'p^'n \<Rightarrow> 'a ^ 'p ^'m"
   347     (infixl "**" 70)
   348   where "m ** m' == (\<chi> i j. setsum (\<lambda>k. ((m$i)$k) * ((m'$k)$j)) (UNIV :: 'n set)) ::'a ^ 'p ^'m"
   349 
   350 definition matrix_vector_mult :: "('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n \<Rightarrow> 'a ^ 'm"
   351     (infixl "*v" 70)
   352   where "m *v x \<equiv> (\<chi> i. setsum (\<lambda>j. ((m$i)$j) * (x$j)) (UNIV ::'n set)) :: 'a^'m"
   353 
   354 definition vector_matrix_mult :: "'a ^ 'm \<Rightarrow> ('a::semiring_1) ^'n^'m \<Rightarrow> 'a ^'n "
   355     (infixl "v*" 70)
   356   where "v v* m == (\<chi> j. setsum (\<lambda>i. ((m$i)$j) * (v$i)) (UNIV :: 'm set)) :: 'a^'n"
   357 
   358 definition "(mat::'a::zero => 'a ^'n^'n) k = (\<chi> i j. if i = j then k else 0)"
   359 definition transpose where 
   360   "(transpose::'a^'n^'m \<Rightarrow> 'a^'m^'n) A = (\<chi> i j. ((A$j)$i))"
   361 definition "(row::'m => 'a ^'n^'m \<Rightarrow> 'a ^'n) i A = (\<chi> j. ((A$i)$j))"
   362 definition "(column::'n =>'a^'n^'m =>'a^'m) j A = (\<chi> i. ((A$i)$j))"
   363 definition "rows(A::'a^'n^'m) = { row i A | i. i \<in> (UNIV :: 'm set)}"
   364 definition "columns(A::'a^'n^'m) = { column i A | i. i \<in> (UNIV :: 'n set)}"
   365 
   366 lemma mat_0[simp]: "mat 0 = 0" by (vector mat_def)
   367 lemma matrix_add_ldistrib: "(A ** (B + C)) = (A ** B) + (A ** C)"
   368   by (vector matrix_matrix_mult_def setsum_addf[symmetric] field_simps)
   369 
   370 lemma matrix_mul_lid:
   371   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
   372   shows "mat 1 ** A = A"
   373   apply (simp add: matrix_matrix_mult_def mat_def)
   374   apply vector
   375   apply (auto simp only: if_distrib cond_application_beta setsum_delta'[OF finite]
   376     mult_1_left mult_zero_left if_True UNIV_I)
   377   done
   378 
   379 
   380 lemma matrix_mul_rid:
   381   fixes A :: "'a::semiring_1 ^ 'm ^ 'n"
   382   shows "A ** mat 1 = A"
   383   apply (simp add: matrix_matrix_mult_def mat_def)
   384   apply vector
   385   apply (auto simp only: if_distrib cond_application_beta setsum_delta[OF finite]
   386     mult_1_right mult_zero_right if_True UNIV_I cong: if_cong)
   387   done
   388 
   389 lemma matrix_mul_assoc: "A ** (B ** C) = (A ** B) ** C"
   390   apply (vector matrix_matrix_mult_def setsum_right_distrib setsum_left_distrib mult_assoc)
   391   apply (subst setsum_commute)
   392   apply simp
   393   done
   394 
   395 lemma matrix_vector_mul_assoc: "A *v (B *v x) = (A ** B) *v x"
   396   apply (vector matrix_matrix_mult_def matrix_vector_mult_def
   397     setsum_right_distrib setsum_left_distrib mult_assoc)
   398   apply (subst setsum_commute)
   399   apply simp
   400   done
   401 
   402 lemma matrix_vector_mul_lid: "mat 1 *v x = (x::'a::semiring_1 ^ 'n)"
   403   apply (vector matrix_vector_mult_def mat_def)
   404   apply (simp add: if_distrib cond_application_beta setsum_delta' cong del: if_weak_cong)
   405   done
   406 
   407 lemma matrix_transpose_mul:
   408     "transpose(A ** B) = transpose B ** transpose (A::'a::comm_semiring_1^_^_)"
   409   by (simp add: matrix_matrix_mult_def transpose_def vec_eq_iff mult_commute)
   410 
   411 lemma matrix_eq:
   412   fixes A B :: "'a::semiring_1 ^ 'n ^ 'm"
   413   shows "A = B \<longleftrightarrow>  (\<forall>x. A *v x = B *v x)" (is "?lhs \<longleftrightarrow> ?rhs")
   414   apply auto
   415   apply (subst vec_eq_iff)
   416   apply clarify
   417   apply (clarsimp simp add: matrix_vector_mult_def if_distrib cond_application_beta vec_eq_iff cong del: if_weak_cong)
   418   apply (erule_tac x="axis ia 1" in allE)
   419   apply (erule_tac x="i" in allE)
   420   apply (auto simp add: if_distrib cond_application_beta axis_def
   421     setsum_delta[OF finite] cong del: if_weak_cong)
   422   done
   423 
   424 lemma matrix_vector_mul_component: "((A::real^_^_) *v x)$k = (A$k) \<bullet> x"
   425   by (simp add: matrix_vector_mult_def inner_vec_def)
   426 
   427 lemma dot_lmul_matrix: "((x::real ^_) v* A) \<bullet> y = x \<bullet> (A *v y)"
   428   apply (simp add: inner_vec_def matrix_vector_mult_def vector_matrix_mult_def setsum_left_distrib setsum_right_distrib mult_ac)
   429   apply (subst setsum_commute)
   430   apply simp
   431   done
   432 
   433 lemma transpose_mat: "transpose (mat n) = mat n"
   434   by (vector transpose_def mat_def)
   435 
   436 lemma transpose_transpose: "transpose(transpose A) = A"
   437   by (vector transpose_def)
   438 
   439 lemma row_transpose:
   440   fixes A:: "'a::semiring_1^_^_"
   441   shows "row i (transpose A) = column i A"
   442   by (simp add: row_def column_def transpose_def vec_eq_iff)
   443 
   444 lemma column_transpose:
   445   fixes A:: "'a::semiring_1^_^_"
   446   shows "column i (transpose A) = row i A"
   447   by (simp add: row_def column_def transpose_def vec_eq_iff)
   448 
   449 lemma rows_transpose: "rows(transpose (A::'a::semiring_1^_^_)) = columns A"
   450   by (auto simp add: rows_def columns_def row_transpose intro: set_eqI)
   451 
   452 lemma columns_transpose: "columns(transpose (A::'a::semiring_1^_^_)) = rows A"
   453   by (metis transpose_transpose rows_transpose)
   454 
   455 text{* Two sometimes fruitful ways of looking at matrix-vector multiplication. *}
   456 
   457 lemma matrix_mult_dot: "A *v x = (\<chi> i. A$i \<bullet> x)"
   458   by (simp add: matrix_vector_mult_def inner_vec_def)
   459 
   460 lemma matrix_mult_vsum:
   461   "(A::'a::comm_semiring_1^'n^'m) *v x = setsum (\<lambda>i. (x$i) *s column i A) (UNIV:: 'n set)"
   462   by (simp add: matrix_vector_mult_def vec_eq_iff column_def mult_commute)
   463 
   464 lemma vector_componentwise:
   465   "(x::'a::ring_1^'n) = (\<chi> j. \<Sum>i\<in>UNIV. (x$i) * (axis i 1 :: 'a^'n) $ j)"
   466   by (simp add: axis_def if_distrib setsum_cases vec_eq_iff)
   467 
   468 lemma basis_expansion: "setsum (\<lambda>i. (x$i) *s axis i 1) UNIV = (x::('a::ring_1) ^'n)"
   469   by (auto simp add: axis_def vec_eq_iff if_distrib setsum_cases cong del: if_weak_cong)
   470 
   471 lemma linear_componentwise:
   472   fixes f:: "real ^'m \<Rightarrow> real ^ _"
   473   assumes lf: "linear f"
   474   shows "(f x)$j = setsum (\<lambda>i. (x$i) * (f (axis i 1)$j)) (UNIV :: 'm set)" (is "?lhs = ?rhs")
   475 proof -
   476   let ?M = "(UNIV :: 'm set)"
   477   let ?N = "(UNIV :: 'n set)"
   478   have fM: "finite ?M" by simp
   479   have "?rhs = (setsum (\<lambda>i.(x$i) *\<^sub>R f (axis i 1) ) ?M)$j"
   480     unfolding setsum_component by simp
   481   then show ?thesis
   482     unfolding linear_setsum_mul[OF lf fM, symmetric]
   483     unfolding scalar_mult_eq_scaleR[symmetric]
   484     unfolding basis_expansion
   485     by simp
   486 qed
   487 
   488 text{* Inverse matrices  (not necessarily square) *}
   489 
   490 definition
   491   "invertible(A::'a::semiring_1^'n^'m) \<longleftrightarrow> (\<exists>A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
   492 
   493 definition
   494   "matrix_inv(A:: 'a::semiring_1^'n^'m) =
   495     (SOME A'::'a^'m^'n. A ** A' = mat 1 \<and> A' ** A = mat 1)"
   496 
   497 text{* Correspondence between matrices and linear operators. *}
   498 
   499 definition matrix :: "('a::{plus,times, one, zero}^'m \<Rightarrow> 'a ^ 'n) \<Rightarrow> 'a^'m^'n"
   500   where "matrix f = (\<chi> i j. (f(axis j 1))$i)"
   501 
   502 lemma matrix_vector_mul_linear: "linear(\<lambda>x. A *v (x::real ^ _))"
   503   by (simp add: linear_def matrix_vector_mult_def vec_eq_iff
   504       field_simps setsum_right_distrib setsum_addf)
   505 
   506 lemma matrix_works:
   507   assumes lf: "linear f"
   508   shows "matrix f *v x = f (x::real ^ 'n)"
   509   apply (simp add: matrix_def matrix_vector_mult_def vec_eq_iff mult_commute)
   510   apply clarify
   511   apply (rule linear_componentwise[OF lf, symmetric])
   512   done
   513 
   514 lemma matrix_vector_mul: "linear f ==> f = (\<lambda>x. matrix f *v (x::real ^ 'n))"
   515   by (simp add: ext matrix_works)
   516 
   517 lemma matrix_of_matrix_vector_mul: "matrix(\<lambda>x. A *v (x :: real ^ 'n)) = A"
   518   by (simp add: matrix_eq matrix_vector_mul_linear matrix_works)
   519 
   520 lemma matrix_compose:
   521   assumes lf: "linear (f::real^'n \<Rightarrow> real^'m)"
   522     and lg: "linear (g::real^'m \<Rightarrow> real^_)"
   523   shows "matrix (g o f) = matrix g ** matrix f"
   524   using lf lg linear_compose[OF lf lg] matrix_works[OF linear_compose[OF lf lg]]
   525   by (simp add: matrix_eq matrix_works matrix_vector_mul_assoc[symmetric] o_def)
   526 
   527 lemma matrix_vector_column:
   528   "(A::'a::comm_semiring_1^'n^_) *v x = setsum (\<lambda>i. (x$i) *s ((transpose A)$i)) (UNIV:: 'n set)"
   529   by (simp add: matrix_vector_mult_def transpose_def vec_eq_iff mult_commute)
   530 
   531 lemma adjoint_matrix: "adjoint(\<lambda>x. (A::real^'n^'m) *v x) = (\<lambda>x. transpose A *v x)"
   532   apply (rule adjoint_unique)
   533   apply (simp add: transpose_def inner_vec_def matrix_vector_mult_def
   534     setsum_left_distrib setsum_right_distrib)
   535   apply (subst setsum_commute)
   536   apply (auto simp add: mult_ac)
   537   done
   538 
   539 lemma matrix_adjoint: assumes lf: "linear (f :: real^'n \<Rightarrow> real ^'m)"
   540   shows "matrix(adjoint f) = transpose(matrix f)"
   541   apply (subst matrix_vector_mul[OF lf])
   542   unfolding adjoint_matrix matrix_of_matrix_vector_mul
   543   apply rule
   544   done
   545 
   546 
   547 subsection {* lambda skolemization on cartesian products *}
   548 
   549 (* FIXME: rename do choice_cart *)
   550 
   551 lemma lambda_skolem: "(\<forall>i. \<exists>x. P i x) \<longleftrightarrow>
   552    (\<exists>x::'a ^ 'n. \<forall>i. P i (x $ i))" (is "?lhs \<longleftrightarrow> ?rhs")
   553 proof -
   554   let ?S = "(UNIV :: 'n set)"
   555   { assume H: "?rhs"
   556     then have ?lhs by auto }
   557   moreover
   558   { assume H: "?lhs"
   559     then obtain f where f:"\<forall>i. P i (f i)" unfolding choice_iff by metis
   560     let ?x = "(\<chi> i. (f i)) :: 'a ^ 'n"
   561     { fix i
   562       from f have "P i (f i)" by metis
   563       then have "P i (?x $ i)" by auto
   564     }
   565     hence "\<forall>i. P i (?x$i)" by metis
   566     hence ?rhs by metis }
   567   ultimately show ?thesis by metis
   568 qed
   569 
   570 lemma vector_sub_project_orthogonal_cart: "(b::real^'n) \<bullet> (x - ((b \<bullet> x) / (b \<bullet> b)) *s b) = 0"
   571   unfolding inner_simps scalar_mult_eq_scaleR by auto
   572 
   573 lemma left_invertible_transpose:
   574   "(\<exists>(B). B ** transpose (A) = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B). A ** B = mat 1)"
   575   by (metis matrix_transpose_mul transpose_mat transpose_transpose)
   576 
   577 lemma right_invertible_transpose:
   578   "(\<exists>(B). transpose (A) ** B = mat (1::'a::comm_semiring_1)) \<longleftrightarrow> (\<exists>(B). B ** A = mat 1)"
   579   by (metis matrix_transpose_mul transpose_mat transpose_transpose)
   580 
   581 lemma matrix_left_invertible_injective:
   582   "(\<exists>B. (B::real^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x y. A *v x = A *v y \<longrightarrow> x = y)"
   583 proof -
   584   { fix B:: "real^'m^'n" and x y assume B: "B ** A = mat 1" and xy: "A *v x = A*v y"
   585     from xy have "B*v (A *v x) = B *v (A*v y)" by simp
   586     hence "x = y"
   587       unfolding matrix_vector_mul_assoc B matrix_vector_mul_lid . }
   588   moreover
   589   { assume A: "\<forall>x y. A *v x = A *v y \<longrightarrow> x = y"
   590     hence i: "inj (op *v A)" unfolding inj_on_def by auto
   591     from linear_injective_left_inverse[OF matrix_vector_mul_linear i]
   592     obtain g where g: "linear g" "g o op *v A = id" by blast
   593     have "matrix g ** A = mat 1"
   594       unfolding matrix_eq matrix_vector_mul_lid matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
   595       using g(2) by (simp add: fun_eq_iff)
   596     then have "\<exists>B. (B::real ^'m^'n) ** A = mat 1" by blast }
   597   ultimately show ?thesis by blast
   598 qed
   599 
   600 lemma matrix_left_invertible_ker:
   601   "(\<exists>B. (B::real ^'m^'n) ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> (\<forall>x. A *v x = 0 \<longrightarrow> x = 0)"
   602   unfolding matrix_left_invertible_injective
   603   using linear_injective_0[OF matrix_vector_mul_linear, of A]
   604   by (simp add: inj_on_def)
   605 
   606 lemma matrix_right_invertible_surjective:
   607   "(\<exists>B. (A::real^'n^'m) ** (B::real^'m^'n) = mat 1) \<longleftrightarrow> surj (\<lambda>x. A *v x)"
   608 proof -
   609   { fix B :: "real ^'m^'n"
   610     assume AB: "A ** B = mat 1"
   611     { fix x :: "real ^ 'm"
   612       have "A *v (B *v x) = x"
   613         by (simp add: matrix_vector_mul_lid matrix_vector_mul_assoc AB) }
   614     hence "surj (op *v A)" unfolding surj_def by metis }
   615   moreover
   616   { assume sf: "surj (op *v A)"
   617     from linear_surjective_right_inverse[OF matrix_vector_mul_linear sf]
   618     obtain g:: "real ^'m \<Rightarrow> real ^'n" where g: "linear g" "op *v A o g = id"
   619       by blast
   620 
   621     have "A ** (matrix g) = mat 1"
   622       unfolding matrix_eq  matrix_vector_mul_lid
   623         matrix_vector_mul_assoc[symmetric] matrix_works[OF g(1)]
   624       using g(2) unfolding o_def fun_eq_iff id_def
   625       .
   626     hence "\<exists>B. A ** (B::real^'m^'n) = mat 1" by blast
   627   }
   628   ultimately show ?thesis unfolding surj_def by blast
   629 qed
   630 
   631 lemma matrix_left_invertible_independent_columns:
   632   fixes A :: "real^'n^'m"
   633   shows "(\<exists>(B::real ^'m^'n). B ** A = mat 1) \<longleftrightarrow>
   634       (\<forall>c. setsum (\<lambda>i. c i *s column i A) (UNIV :: 'n set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
   635     (is "?lhs \<longleftrightarrow> ?rhs")
   636 proof -
   637   let ?U = "UNIV :: 'n set"
   638   { assume k: "\<forall>x. A *v x = 0 \<longrightarrow> x = 0"
   639     { fix c i
   640       assume c: "setsum (\<lambda>i. c i *s column i A) ?U = 0" and i: "i \<in> ?U"
   641       let ?x = "\<chi> i. c i"
   642       have th0:"A *v ?x = 0"
   643         using c
   644         unfolding matrix_mult_vsum vec_eq_iff
   645         by auto
   646       from k[rule_format, OF th0] i
   647       have "c i = 0" by (vector vec_eq_iff)}
   648     hence ?rhs by blast }
   649   moreover
   650   { assume H: ?rhs
   651     { fix x assume x: "A *v x = 0"
   652       let ?c = "\<lambda>i. ((x$i ):: real)"
   653       from H[rule_format, of ?c, unfolded matrix_mult_vsum[symmetric], OF x]
   654       have "x = 0" by vector }
   655   }
   656   ultimately show ?thesis unfolding matrix_left_invertible_ker by blast
   657 qed
   658 
   659 lemma matrix_right_invertible_independent_rows:
   660   fixes A :: "real^'n^'m"
   661   shows "(\<exists>(B::real^'m^'n). A ** B = mat 1) \<longleftrightarrow>
   662     (\<forall>c. setsum (\<lambda>i. c i *s row i A) (UNIV :: 'm set) = 0 \<longrightarrow> (\<forall>i. c i = 0))"
   663   unfolding left_invertible_transpose[symmetric]
   664     matrix_left_invertible_independent_columns
   665   by (simp add: column_transpose)
   666 
   667 lemma matrix_right_invertible_span_columns:
   668   "(\<exists>(B::real ^'n^'m). (A::real ^'m^'n) ** B = mat 1) \<longleftrightarrow>
   669     span (columns A) = UNIV" (is "?lhs = ?rhs")
   670 proof -
   671   let ?U = "UNIV :: 'm set"
   672   have fU: "finite ?U" by simp
   673   have lhseq: "?lhs \<longleftrightarrow> (\<forall>y. \<exists>(x::real^'m). setsum (\<lambda>i. (x$i) *s column i A) ?U = y)"
   674     unfolding matrix_right_invertible_surjective matrix_mult_vsum surj_def
   675     apply (subst eq_commute)
   676     apply rule
   677     done
   678   have rhseq: "?rhs \<longleftrightarrow> (\<forall>x. x \<in> span (columns A))" by blast
   679   { assume h: ?lhs
   680     { fix x:: "real ^'n"
   681       from h[unfolded lhseq, rule_format, of x] obtain y :: "real ^'m"
   682         where y: "setsum (\<lambda>i. (y$i) *s column i A) ?U = x" by blast
   683       have "x \<in> span (columns A)"
   684         unfolding y[symmetric]
   685         apply (rule span_setsum[OF fU])
   686         apply clarify
   687         unfolding scalar_mult_eq_scaleR
   688         apply (rule span_mul)
   689         apply (rule span_superset)
   690         unfolding columns_def
   691         apply blast
   692         done
   693     }
   694     then have ?rhs unfolding rhseq by blast }
   695   moreover
   696   { assume h:?rhs
   697     let ?P = "\<lambda>(y::real ^'n). \<exists>(x::real^'m). setsum (\<lambda>i. (x$i) *s column i A) ?U = y"
   698     { fix y
   699       have "?P y"
   700       proof (rule span_induct_alt[of ?P "columns A", folded scalar_mult_eq_scaleR])
   701         show "\<exists>x\<Colon>real ^ 'm. setsum (\<lambda>i. (x$i) *s column i A) ?U = 0"
   702           by (rule exI[where x=0], simp)
   703       next
   704         fix c y1 y2
   705         assume y1: "y1 \<in> columns A" and y2: "?P y2"
   706         from y1 obtain i where i: "i \<in> ?U" "y1 = column i A"
   707           unfolding columns_def by blast
   708         from y2 obtain x:: "real ^'m" where
   709           x: "setsum (\<lambda>i. (x$i) *s column i A) ?U = y2" by blast
   710         let ?x = "(\<chi> j. if j = i then c + (x$i) else (x$j))::real^'m"
   711         show "?P (c*s y1 + y2)"
   712         proof (rule exI[where x= "?x"], vector, auto simp add: i x[symmetric] if_distrib distrib_left cond_application_beta cong del: if_weak_cong)
   713           fix j
   714           have th: "\<forall>xa \<in> ?U. (if xa = i then (c + (x$i)) * ((column xa A)$j)
   715               else (x$xa) * ((column xa A$j))) = (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))"
   716             using i(1) by (simp add: field_simps)
   717           have "setsum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
   718               else (x$xa) * ((column xa A$j))) ?U = setsum (\<lambda>xa. (if xa = i then c * ((column i A)$j) else 0) + ((x$xa) * ((column xa A)$j))) ?U"
   719             apply (rule setsum_cong[OF refl])
   720             using th apply blast
   721             done
   722           also have "\<dots> = setsum (\<lambda>xa. if xa = i then c * ((column i A)$j) else 0) ?U + setsum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U"
   723             by (simp add: setsum_addf)
   724           also have "\<dots> = c * ((column i A)$j) + setsum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U"
   725             unfolding setsum_delta[OF fU]
   726             using i(1) by simp
   727           finally show "setsum (\<lambda>xa. if xa = i then (c + (x$i)) * ((column xa A)$j)
   728             else (x$xa) * ((column xa A$j))) ?U = c * ((column i A)$j) + setsum (\<lambda>xa. ((x$xa) * ((column xa A)$j))) ?U" .
   729         qed
   730       next
   731         show "y \<in> span (columns A)"
   732           unfolding h by blast
   733       qed
   734     }
   735     then have ?lhs unfolding lhseq ..
   736   }
   737   ultimately show ?thesis by blast
   738 qed
   739 
   740 lemma matrix_left_invertible_span_rows:
   741   "(\<exists>(B::real^'m^'n). B ** (A::real^'n^'m) = mat 1) \<longleftrightarrow> span (rows A) = UNIV"
   742   unfolding right_invertible_transpose[symmetric]
   743   unfolding columns_transpose[symmetric]
   744   unfolding matrix_right_invertible_span_columns
   745   ..
   746 
   747 text {* The same result in terms of square matrices. *}
   748 
   749 lemma matrix_left_right_inverse:
   750   fixes A A' :: "real ^'n^'n"
   751   shows "A ** A' = mat 1 \<longleftrightarrow> A' ** A = mat 1"
   752 proof -
   753   { fix A A' :: "real ^'n^'n"
   754     assume AA': "A ** A' = mat 1"
   755     have sA: "surj (op *v A)"
   756       unfolding surj_def
   757       apply clarify
   758       apply (rule_tac x="(A' *v y)" in exI)
   759       apply (simp add: matrix_vector_mul_assoc AA' matrix_vector_mul_lid)
   760       done
   761     from linear_surjective_isomorphism[OF matrix_vector_mul_linear sA]
   762     obtain f' :: "real ^'n \<Rightarrow> real ^'n"
   763       where f': "linear f'" "\<forall>x. f' (A *v x) = x" "\<forall>x. A *v f' x = x" by blast
   764     have th: "matrix f' ** A = mat 1"
   765       by (simp add: matrix_eq matrix_works[OF f'(1)]
   766           matrix_vector_mul_assoc[symmetric] matrix_vector_mul_lid f'(2)[rule_format])
   767     hence "(matrix f' ** A) ** A' = mat 1 ** A'" by simp
   768     hence "matrix f' = A'"
   769       by (simp add: matrix_mul_assoc[symmetric] AA' matrix_mul_rid matrix_mul_lid)
   770     hence "matrix f' ** A = A' ** A" by simp
   771     hence "A' ** A = mat 1" by (simp add: th)
   772   }
   773   then show ?thesis by blast
   774 qed
   775 
   776 text {* Considering an n-element vector as an n-by-1 or 1-by-n matrix. *}
   777 
   778 definition "rowvector v = (\<chi> i j. (v$j))"
   779 
   780 definition "columnvector v = (\<chi> i j. (v$i))"
   781 
   782 lemma transpose_columnvector: "transpose(columnvector v) = rowvector v"
   783   by (simp add: transpose_def rowvector_def columnvector_def vec_eq_iff)
   784 
   785 lemma transpose_rowvector: "transpose(rowvector v) = columnvector v"
   786   by (simp add: transpose_def columnvector_def rowvector_def vec_eq_iff)
   787 
   788 lemma dot_rowvector_columnvector: "columnvector (A *v v) = A ** columnvector v"
   789   by (vector columnvector_def matrix_matrix_mult_def matrix_vector_mult_def)
   790 
   791 lemma dot_matrix_product:
   792   "(x::real^'n) \<bullet> y = (((rowvector x ::real^'n^1) ** (columnvector y :: real^1^'n))$1)$1"
   793   by (vector matrix_matrix_mult_def rowvector_def columnvector_def inner_vec_def)
   794 
   795 lemma dot_matrix_vector_mul:
   796   fixes A B :: "real ^'n ^'n" and x y :: "real ^'n"
   797   shows "(A *v x) \<bullet> (B *v y) =
   798       (((rowvector x :: real^'n^1) ** ((transpose A ** B) ** (columnvector y :: real ^1^'n)))$1)$1"
   799   unfolding dot_matrix_product transpose_columnvector[symmetric]
   800     dot_rowvector_columnvector matrix_transpose_mul matrix_mul_assoc ..
   801 
   802 
   803 lemma infnorm_cart:"infnorm (x::real^'n) = Sup {abs(x$i) |i. i\<in>UNIV}"
   804   by (simp add: infnorm_def inner_axis Basis_vec_def) (metis (lifting) inner_axis real_inner_1_right)
   805 
   806 lemma component_le_infnorm_cart: "\<bar>x$i\<bar> \<le> infnorm (x::real^'n)"
   807   using Basis_le_infnorm[of "axis i 1" x]
   808   by (simp add: Basis_vec_def axis_eq_axis inner_axis)
   809 
   810 lemma continuous_component: "continuous F f \<Longrightarrow> continuous F (\<lambda>x. f x $ i)"
   811   unfolding continuous_def by (rule tendsto_vec_nth)
   812 
   813 lemma continuous_on_component: "continuous_on s f \<Longrightarrow> continuous_on s (\<lambda>x. f x $ i)"
   814   unfolding continuous_on_def by (fast intro: tendsto_vec_nth)
   815 
   816 lemma closed_positive_orthant: "closed {x::real^'n. \<forall>i. 0 \<le>x$i}"
   817   by (simp add: Collect_all_eq closed_INT closed_Collect_le)
   818 
   819 lemma bounded_component_cart: "bounded s \<Longrightarrow> bounded ((\<lambda>x. x $ i) ` s)"
   820   unfolding bounded_def
   821   apply clarify
   822   apply (rule_tac x="x $ i" in exI)
   823   apply (rule_tac x="e" in exI)
   824   apply clarify
   825   apply (rule order_trans [OF dist_vec_nth_le], simp)
   826   done
   827 
   828 lemma compact_lemma_cart:
   829   fixes f :: "nat \<Rightarrow> 'a::heine_borel ^ 'n"
   830   assumes f: "bounded (range f)"
   831   shows "\<forall>d.
   832         \<exists>l r. subseq r \<and>
   833         (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
   834 proof
   835   fix d :: "'n set"
   836   have "finite d" by simp
   837   thus "\<exists>l::'a ^ 'n. \<exists>r. subseq r \<and>
   838       (\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r n) $ i) (l $ i) < e) sequentially)"
   839   proof (induct d)
   840     case empty
   841     thus ?case unfolding subseq_def by auto
   842   next
   843     case (insert k d)
   844     obtain l1::"'a^'n" and r1 where r1:"subseq r1"
   845       and lr1:"\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $ i) (l1 $ i) < e) sequentially"
   846       using insert(3) by auto
   847     have s': "bounded ((\<lambda>x. x $ k) ` range f)" using `bounded (range f)`
   848       by (auto intro!: bounded_component_cart)
   849     have f': "\<forall>n. f (r1 n) $ k \<in> (\<lambda>x. x $ k) ` range f" by simp
   850     have "bounded (range (\<lambda>i. f (r1 i) $ k))"
   851       by (metis (lifting) bounded_subset image_subsetI f' s')
   852     then obtain l2 r2 where r2: "subseq r2"
   853       and lr2: "((\<lambda>i. f (r1 (r2 i)) $ k) ---> l2) sequentially"
   854       using bounded_imp_convergent_subsequence[of "\<lambda>i. f (r1 i) $ k"] by (auto simp: o_def)
   855     def r \<equiv> "r1 \<circ> r2"
   856     have r: "subseq r"
   857       using r1 and r2 unfolding r_def o_def subseq_def by auto
   858     moreover
   859     def l \<equiv> "(\<chi> i. if i = k then l2 else l1$i)::'a^'n"
   860     { fix e :: real assume "e > 0"
   861       from lr1 `e>0` have N1:"eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 n) $ i) (l1 $ i) < e) sequentially"
   862         by blast
   863       from lr2 `e>0` have N2:"eventually (\<lambda>n. dist (f (r1 (r2 n)) $ k) l2 < e) sequentially"
   864         by (rule tendstoD)
   865       from r2 N1 have N1': "eventually (\<lambda>n. \<forall>i\<in>d. dist (f (r1 (r2 n)) $ i) (l1 $ i) < e) sequentially"
   866         by (rule eventually_subseq)
   867       have "eventually (\<lambda>n. \<forall>i\<in>(insert k d). dist (f (r n) $ i) (l $ i) < e) sequentially"
   868         using N1' N2 by (rule eventually_elim2, simp add: l_def r_def)
   869     }
   870     ultimately show ?case by auto
   871   qed
   872 qed
   873 
   874 instance vec :: (heine_borel, finite) heine_borel
   875 proof
   876   fix f :: "nat \<Rightarrow> 'a ^ 'b"
   877   assume f: "bounded (range f)"
   878   then obtain l r where r: "subseq r"
   879       and l: "\<forall>e>0. eventually (\<lambda>n. \<forall>i\<in>UNIV. dist (f (r n) $ i) (l $ i) < e) sequentially"
   880     using compact_lemma_cart [OF f] by blast
   881   let ?d = "UNIV::'b set"
   882   { fix e::real assume "e>0"
   883     hence "0 < e / (real_of_nat (card ?d))"
   884       using zero_less_card_finite divide_pos_pos[of e, of "real_of_nat (card ?d)"] by auto
   885     with l have "eventually (\<lambda>n. \<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))) sequentially"
   886       by simp
   887     moreover
   888     { fix n
   889       assume n: "\<forall>i. dist (f (r n) $ i) (l $ i) < e / (real_of_nat (card ?d))"
   890       have "dist (f (r n)) l \<le> (\<Sum>i\<in>?d. dist (f (r n) $ i) (l $ i))"
   891         unfolding dist_vec_def using zero_le_dist by (rule setL2_le_setsum)
   892       also have "\<dots> < (\<Sum>i\<in>?d. e / (real_of_nat (card ?d)))"
   893         by (rule setsum_strict_mono) (simp_all add: n)
   894       finally have "dist (f (r n)) l < e" by simp
   895     }
   896     ultimately have "eventually (\<lambda>n. dist (f (r n)) l < e) sequentially"
   897       by (rule eventually_elim1)
   898   }
   899   hence "((f \<circ> r) ---> l) sequentially" unfolding o_def tendsto_iff by simp
   900   with r show "\<exists>l r. subseq r \<and> ((f \<circ> r) ---> l) sequentially" by auto
   901 qed
   902 
   903 lemma interval_cart:
   904   fixes a :: "'a::ord^'n"
   905   shows "{a <..< b} = {x::'a^'n. \<forall>i. a$i < x$i \<and> x$i < b$i}"
   906     and "{a .. b} = {x::'a^'n. \<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i}"
   907   by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def)
   908 
   909 lemma mem_interval_cart:
   910   fixes a :: "'a::ord^'n"
   911   shows "x \<in> {a<..<b} \<longleftrightarrow> (\<forall>i. a$i < x$i \<and> x$i < b$i)"
   912     and "x \<in> {a .. b} \<longleftrightarrow> (\<forall>i. a$i \<le> x$i \<and> x$i \<le> b$i)"
   913   using interval_cart[of a b] by (auto simp add: set_eq_iff less_vec_def less_eq_vec_def)
   914 
   915 lemma interval_eq_empty_cart:
   916   fixes a :: "real^'n"
   917   shows "({a <..< b} = {} \<longleftrightarrow> (\<exists>i. b$i \<le> a$i))" (is ?th1)
   918     and "({a  ..  b} = {} \<longleftrightarrow> (\<exists>i. b$i < a$i))" (is ?th2)
   919 proof -
   920   { fix i x assume as:"b$i \<le> a$i" and x:"x\<in>{a <..< b}"
   921     hence "a $ i < x $ i \<and> x $ i < b $ i" unfolding mem_interval_cart by auto
   922     hence "a$i < b$i" by auto
   923     hence False using as by auto }
   924   moreover
   925   { assume as:"\<forall>i. \<not> (b$i \<le> a$i)"
   926     let ?x = "(1/2) *\<^sub>R (a + b)"
   927     { fix i
   928       have "a$i < b$i" using as[THEN spec[where x=i]] by auto
   929       hence "a$i < ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i < b$i"
   930         unfolding vector_smult_component and vector_add_component
   931         by auto }
   932     hence "{a <..< b} \<noteq> {}" using mem_interval_cart(1)[of "?x" a b] by auto }
   933   ultimately show ?th1 by blast
   934 
   935   { fix i x assume as:"b$i < a$i" and x:"x\<in>{a .. b}"
   936     hence "a $ i \<le> x $ i \<and> x $ i \<le> b $ i" unfolding mem_interval_cart by auto
   937     hence "a$i \<le> b$i" by auto
   938     hence False using as by auto }
   939   moreover
   940   { assume as:"\<forall>i. \<not> (b$i < a$i)"
   941     let ?x = "(1/2) *\<^sub>R (a + b)"
   942     { fix i
   943       have "a$i \<le> b$i" using as[THEN spec[where x=i]] by auto
   944       hence "a$i \<le> ((1/2) *\<^sub>R (a+b)) $ i" "((1/2) *\<^sub>R (a+b)) $ i \<le> b$i"
   945         unfolding vector_smult_component and vector_add_component
   946         by auto }
   947     hence "{a .. b} \<noteq> {}" using mem_interval_cart(2)[of "?x" a b] by auto  }
   948   ultimately show ?th2 by blast
   949 qed
   950 
   951 lemma interval_ne_empty_cart:
   952   fixes a :: "real^'n"
   953   shows "{a  ..  b} \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i \<le> b$i)"
   954     and "{a <..< b} \<noteq> {} \<longleftrightarrow> (\<forall>i. a$i < b$i)"
   955   unfolding interval_eq_empty_cart[of a b] by (auto simp add: not_less not_le)
   956     (* BH: Why doesn't just "auto" work here? *)
   957 
   958 lemma subset_interval_imp_cart:
   959   fixes a :: "real^'n"
   960   shows "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c .. d} \<subseteq> {a .. b}"
   961     and "(\<forall>i. a$i < c$i \<and> d$i < b$i) \<Longrightarrow> {c .. d} \<subseteq> {a<..<b}"
   962     and "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c<..<d} \<subseteq> {a .. b}"
   963     and "(\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i) \<Longrightarrow> {c<..<d} \<subseteq> {a<..<b}"
   964   unfolding subset_eq[unfolded Ball_def] unfolding mem_interval_cart
   965   by (auto intro: order_trans less_le_trans le_less_trans less_imp_le) (* BH: Why doesn't just "auto" work here? *)
   966 
   967 lemma interval_sing:
   968   fixes a :: "'a::linorder^'n"
   969   shows "{a .. a} = {a} \<and> {a<..<a} = {}"
   970   apply (auto simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
   971   apply (simp add: order_eq_iff)
   972   apply (auto simp add: not_less less_imp_le)
   973   done
   974 
   975 lemma interval_open_subset_closed_cart:
   976   fixes a :: "'a::preorder^'n"
   977   shows "{a<..<b} \<subseteq> {a .. b}"
   978 proof (simp add: subset_eq, rule)
   979   fix x
   980   assume x: "x \<in>{a<..<b}"
   981   { fix i
   982     have "a $ i \<le> x $ i"
   983       using x order_less_imp_le[of "a$i" "x$i"]
   984       by(simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
   985   }
   986   moreover
   987   { fix i
   988     have "x $ i \<le> b $ i"
   989       using x order_less_imp_le[of "x$i" "b$i"]
   990       by(simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
   991   }
   992   ultimately
   993   show "a \<le> x \<and> x \<le> b"
   994     by(simp add: set_eq_iff less_vec_def less_eq_vec_def vec_eq_iff)
   995 qed
   996 
   997 lemma subset_interval_cart:
   998   fixes a :: "real^'n"
   999   shows "{c .. d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th1)
  1000     and "{c .. d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i. c$i \<le> d$i) --> (\<forall>i. a$i < c$i \<and> d$i < b$i)" (is ?th2)
  1001     and "{c<..<d} \<subseteq> {a .. b} \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th3)
  1002     and "{c<..<d} \<subseteq> {a<..<b} \<longleftrightarrow> (\<forall>i. c$i < d$i) --> (\<forall>i. a$i \<le> c$i \<and> d$i \<le> b$i)" (is ?th4)
  1003   using subset_interval[of c d a b] by (simp_all add: Basis_vec_def inner_axis)
  1004 
  1005 lemma disjoint_interval_cart:
  1006   fixes a::"real^'n"
  1007   shows "{a .. b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i. (b$i < a$i \<or> d$i < c$i \<or> b$i < c$i \<or> d$i < a$i))" (is ?th1)
  1008     and "{a .. b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i. (b$i < a$i \<or> d$i \<le> c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th2)
  1009     and "{a<..<b} \<inter> {c .. d} = {} \<longleftrightarrow> (\<exists>i. (b$i \<le> a$i \<or> d$i < c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th3)
  1010     and "{a<..<b} \<inter> {c<..<d} = {} \<longleftrightarrow> (\<exists>i. (b$i \<le> a$i \<or> d$i \<le> c$i \<or> b$i \<le> c$i \<or> d$i \<le> a$i))" (is ?th4)
  1011   using disjoint_interval[of a b c d] by (simp_all add: Basis_vec_def inner_axis)
  1012 
  1013 lemma inter_interval_cart:
  1014   fixes a :: "'a::linorder^'n"
  1015   shows "{a .. b} \<inter> {c .. d} =  {(\<chi> i. max (a$i) (c$i)) .. (\<chi> i. min (b$i) (d$i))}"
  1016   unfolding set_eq_iff and Int_iff and mem_interval_cart
  1017   by auto
  1018 
  1019 lemma closed_interval_left_cart:
  1020   fixes b :: "real^'n"
  1021   shows "closed {x::real^'n. \<forall>i. x$i \<le> b$i}"
  1022   by (simp add: Collect_all_eq closed_INT closed_Collect_le)
  1023 
  1024 lemma closed_interval_right_cart:
  1025   fixes a::"real^'n"
  1026   shows "closed {x::real^'n. \<forall>i. a$i \<le> x$i}"
  1027   by (simp add: Collect_all_eq closed_INT closed_Collect_le)
  1028 
  1029 lemma is_interval_cart:
  1030   "is_interval (s::(real^'n) set) \<longleftrightarrow>
  1031     (\<forall>a\<in>s. \<forall>b\<in>s. \<forall>x. (\<forall>i. ((a$i \<le> x$i \<and> x$i \<le> b$i) \<or> (b$i \<le> x$i \<and> x$i \<le> a$i))) \<longrightarrow> x \<in> s)"
  1032   by (simp add: is_interval_def Ball_def Basis_vec_def inner_axis imp_ex)
  1033 
  1034 lemma closed_halfspace_component_le_cart: "closed {x::real^'n. x$i \<le> a}"
  1035   by (simp add: closed_Collect_le)
  1036 
  1037 lemma closed_halfspace_component_ge_cart: "closed {x::real^'n. x$i \<ge> a}"
  1038   by (simp add: closed_Collect_le)
  1039 
  1040 lemma open_halfspace_component_lt_cart: "open {x::real^'n. x$i < a}"
  1041   by (simp add: open_Collect_less)
  1042 
  1043 lemma open_halfspace_component_gt_cart: "open {x::real^'n. x$i  > a}"
  1044   by (simp add: open_Collect_less)
  1045 
  1046 lemma Lim_component_le_cart:
  1047   fixes f :: "'a \<Rightarrow> real^'n"
  1048   assumes "(f ---> l) net" "\<not> (trivial_limit net)"  "eventually (\<lambda>x. f x $i \<le> b) net"
  1049   shows "l$i \<le> b"
  1050   by (rule tendsto_le[OF assms(2) tendsto_const tendsto_vec_nth, OF assms(1, 3)])
  1051 
  1052 lemma Lim_component_ge_cart:
  1053   fixes f :: "'a \<Rightarrow> real^'n"
  1054   assumes "(f ---> l) net"  "\<not> (trivial_limit net)"  "eventually (\<lambda>x. b \<le> (f x)$i) net"
  1055   shows "b \<le> l$i"
  1056   by (rule tendsto_le[OF assms(2) tendsto_vec_nth tendsto_const, OF assms(1, 3)])
  1057 
  1058 lemma Lim_component_eq_cart:
  1059   fixes f :: "'a \<Rightarrow> real^'n"
  1060   assumes net: "(f ---> l) net" "~(trivial_limit net)" and ev:"eventually (\<lambda>x. f(x)$i = b) net"
  1061   shows "l$i = b"
  1062   using ev[unfolded order_eq_iff eventually_conj_iff] and
  1063     Lim_component_ge_cart[OF net, of b i] and
  1064     Lim_component_le_cart[OF net, of i b] by auto
  1065 
  1066 lemma connected_ivt_component_cart:
  1067   fixes x :: "real^'n"
  1068   shows "connected s \<Longrightarrow> x \<in> s \<Longrightarrow> y \<in> s \<Longrightarrow> x$k \<le> a \<Longrightarrow> a \<le> y$k \<Longrightarrow> (\<exists>z\<in>s.  z$k = a)"
  1069   using connected_ivt_hyperplane[of s x y "axis k 1" a]
  1070   by (auto simp add: inner_axis inner_commute)
  1071 
  1072 lemma subspace_substandard_cart: "subspace {x::real^_. (\<forall>i. P i \<longrightarrow> x$i = 0)}"
  1073   unfolding subspace_def by auto
  1074 
  1075 lemma closed_substandard_cart:
  1076   "closed {x::'a::real_normed_vector ^ 'n. \<forall>i. P i \<longrightarrow> x$i = 0}"
  1077 proof -
  1078   { fix i::'n
  1079     have "closed {x::'a ^ 'n. P i \<longrightarrow> x$i = 0}"
  1080       by (cases "P i") (simp_all add: closed_Collect_eq) }
  1081   thus ?thesis
  1082     unfolding Collect_all_eq by (simp add: closed_INT)
  1083 qed
  1084 
  1085 lemma dim_substandard_cart: "dim {x::real^'n. \<forall>i. i \<notin> d \<longrightarrow> x$i = 0} = card d"
  1086   (is "dim ?A = _")
  1087 proof -
  1088   let ?a = "\<lambda>x. axis x 1 :: real^'n"
  1089   have *: "{x. \<forall>i\<in>Basis. i \<notin> ?a ` d \<longrightarrow> x \<bullet> i = 0} = ?A"
  1090     by (auto simp: image_iff Basis_vec_def axis_eq_axis inner_axis)
  1091   have "?a ` d \<subseteq> Basis"
  1092     by (auto simp: Basis_vec_def)
  1093   thus ?thesis
  1094     using dim_substandard[of "?a ` d"] card_image[of ?a d]
  1095     by (auto simp: axis_eq_axis inj_on_def *)
  1096 qed
  1097 
  1098 lemma affinity_inverses:
  1099   assumes m0: "m \<noteq> (0::'a::field)"
  1100   shows "(\<lambda>x. m *s x + c) o (\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) = id"
  1101   "(\<lambda>x. inverse(m) *s x + (-(inverse(m) *s c))) o (\<lambda>x. m *s x + c) = id"
  1102   using m0
  1103   apply (auto simp add: fun_eq_iff vector_add_ldistrib)
  1104   apply (simp_all add: vector_smult_lneg[symmetric] vector_smult_assoc vector_sneg_minus1[symmetric])
  1105   done
  1106 
  1107 lemma vector_affinity_eq:
  1108   assumes m0: "(m::'a::field) \<noteq> 0"
  1109   shows "m *s x + c = y \<longleftrightarrow> x = inverse m *s y + -(inverse m *s c)"
  1110 proof
  1111   assume h: "m *s x + c = y"
  1112   hence "m *s x = y - c" by (simp add: field_simps)
  1113   hence "inverse m *s (m *s x) = inverse m *s (y - c)" by simp
  1114   then show "x = inverse m *s y + - (inverse m *s c)"
  1115     using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1116 next
  1117   assume h: "x = inverse m *s y + - (inverse m *s c)"
  1118   show "m *s x + c = y" unfolding h diff_minus[symmetric]
  1119     using m0 by (simp add: vector_smult_assoc vector_ssub_ldistrib)
  1120 qed
  1121 
  1122 lemma vector_eq_affinity:
  1123     "(m::'a::field) \<noteq> 0 ==> (y = m *s x + c \<longleftrightarrow> inverse(m) *s y + -(inverse(m) *s c) = x)"
  1124   using vector_affinity_eq[where m=m and x=x and y=y and c=c]
  1125   by metis
  1126 
  1127 lemma vector_cart:
  1128   fixes f :: "real^'n \<Rightarrow> real"
  1129   shows "(\<chi> i. f (axis i 1)) = (\<Sum>i\<in>Basis. f i *\<^sub>R i)"
  1130   unfolding euclidean_eq_iff[where 'a="real^'n"]
  1131   by simp (simp add: Basis_vec_def inner_axis)
  1132   
  1133 lemma const_vector_cart:"((\<chi> i. d)::real^'n) = (\<Sum>i\<in>Basis. d *\<^sub>R i)"
  1134   by (rule vector_cart)
  1135 
  1136 subsection "Convex Euclidean Space"
  1137 
  1138 lemma Cart_1:"(1::real^'n) = \<Sum>Basis"
  1139   using const_vector_cart[of 1] by (simp add: one_vec_def)
  1140 
  1141 declare vector_add_ldistrib[simp] vector_ssub_ldistrib[simp] vector_smult_assoc[simp] vector_smult_rneg[simp]
  1142 declare vector_sadd_rdistrib[simp] vector_sub_rdistrib[simp]
  1143 
  1144 lemmas vector_component_simps = vector_minus_component vector_smult_component vector_add_component less_eq_vec_def vec_lambda_beta vector_uminus_component
  1145 
  1146 lemma convex_box_cart:
  1147   assumes "\<And>i. convex {x. P i x}"
  1148   shows "convex {x. \<forall>i. P i (x$i)}"
  1149   using assms unfolding convex_def by auto
  1150 
  1151 lemma convex_positive_orthant_cart: "convex {x::real^'n. (\<forall>i. 0 \<le> x$i)}"
  1152   by (rule convex_box_cart) (simp add: atLeast_def[symmetric] convex_real_interval)
  1153 
  1154 lemma unit_interval_convex_hull_cart:
  1155   "{0::real^'n .. 1} = convex hull {x. \<forall>i. (x$i = 0) \<or> (x$i = 1)}" (is "?int = convex hull ?points")
  1156   unfolding Cart_1 unit_interval_convex_hull[where 'a="real^'n"]
  1157   by (rule arg_cong[where f="\<lambda>x. convex hull x"]) (simp add: Basis_vec_def inner_axis)
  1158 
  1159 lemma cube_convex_hull_cart:
  1160   assumes "0 < d"
  1161   obtains s::"(real^'n) set"
  1162     where "finite s" "{x - (\<chi> i. d) .. x + (\<chi> i. d)} = convex hull s"
  1163 proof -
  1164   from cube_convex_hull [OF assms, of x] guess s .
  1165   with that[of s] show thesis by (simp add: const_vector_cart)
  1166 qed
  1167 
  1168 
  1169 subsection "Derivative"
  1170 
  1171 lemma differentiable_at_imp_differentiable_on:
  1172   "(\<forall>x\<in>(s::(real^'n) set). f differentiable at x) \<Longrightarrow> f differentiable_on s"
  1173   unfolding differentiable_on_def by(auto intro!: differentiable_at_withinI)
  1174 
  1175 definition "jacobian f net = matrix(frechet_derivative f net)"
  1176 
  1177 lemma jacobian_works:
  1178   "(f::(real^'a) \<Rightarrow> (real^'b)) differentiable net \<longleftrightarrow>
  1179     (f has_derivative (\<lambda>h. (jacobian f net) *v h)) net"
  1180   apply rule
  1181   unfolding jacobian_def
  1182   apply (simp only: matrix_works[OF linear_frechet_derivative]) defer
  1183   apply (rule differentiableI)
  1184   apply assumption
  1185   unfolding frechet_derivative_works
  1186   apply assumption
  1187   done
  1188 
  1189 
  1190 subsection {* Component of the differential must be zero if it exists at a local
  1191   maximum or minimum for that corresponding component. *}
  1192 
  1193 lemma differential_zero_maxmin_cart:
  1194   fixes f::"real^'a \<Rightarrow> real^'b"
  1195   assumes "0 < e" "((\<forall>y \<in> ball x e. (f y)$k \<le> (f x)$k) \<or> (\<forall>y\<in>ball x e. (f x)$k \<le> (f y)$k))"
  1196     "f differentiable (at x)"
  1197   shows "jacobian f (at x) $ k = 0"
  1198   using differential_zero_maxmin_component[of "axis k 1" e x f] assms
  1199     vector_cart[of "\<lambda>j. frechet_derivative f (at x) j $ k"]
  1200   by (simp add: Basis_vec_def axis_eq_axis inner_axis jacobian_def matrix_def)
  1201 
  1202 subsection {* Lemmas for working on @{typ "real^1"} *}
  1203 
  1204 lemma forall_1[simp]: "(\<forall>i::1. P i) \<longleftrightarrow> P 1"
  1205   by (metis (full_types) num1_eq_iff)
  1206 
  1207 lemma ex_1[simp]: "(\<exists>x::1. P x) \<longleftrightarrow> P 1"
  1208   by auto (metis (full_types) num1_eq_iff)
  1209 
  1210 lemma exhaust_2:
  1211   fixes x :: 2
  1212   shows "x = 1 \<or> x = 2"
  1213 proof (induct x)
  1214   case (of_int z)
  1215   then have "0 <= z" and "z < 2" by simp_all
  1216   then have "z = 0 | z = 1" by arith
  1217   then show ?case by auto
  1218 qed
  1219 
  1220 lemma forall_2: "(\<forall>i::2. P i) \<longleftrightarrow> P 1 \<and> P 2"
  1221   by (metis exhaust_2)
  1222 
  1223 lemma exhaust_3:
  1224   fixes x :: 3
  1225   shows "x = 1 \<or> x = 2 \<or> x = 3"
  1226 proof (induct x)
  1227   case (of_int z)
  1228   then have "0 <= z" and "z < 3" by simp_all
  1229   then have "z = 0 \<or> z = 1 \<or> z = 2" by arith
  1230   then show ?case by auto
  1231 qed
  1232 
  1233 lemma forall_3: "(\<forall>i::3. P i) \<longleftrightarrow> P 1 \<and> P 2 \<and> P 3"
  1234   by (metis exhaust_3)
  1235 
  1236 lemma UNIV_1 [simp]: "UNIV = {1::1}"
  1237   by (auto simp add: num1_eq_iff)
  1238 
  1239 lemma UNIV_2: "UNIV = {1::2, 2::2}"
  1240   using exhaust_2 by auto
  1241 
  1242 lemma UNIV_3: "UNIV = {1::3, 2::3, 3::3}"
  1243   using exhaust_3 by auto
  1244 
  1245 lemma setsum_1: "setsum f (UNIV::1 set) = f 1"
  1246   unfolding UNIV_1 by simp
  1247 
  1248 lemma setsum_2: "setsum f (UNIV::2 set) = f 1 + f 2"
  1249   unfolding UNIV_2 by simp
  1250 
  1251 lemma setsum_3: "setsum f (UNIV::3 set) = f 1 + f 2 + f 3"
  1252   unfolding UNIV_3 by (simp add: add_ac)
  1253 
  1254 instantiation num1 :: cart_one
  1255 begin
  1256 
  1257 instance
  1258 proof
  1259   show "CARD(1) = Suc 0" by auto
  1260 qed
  1261 
  1262 end
  1263 
  1264 subsection{* The collapse of the general concepts to dimension one. *}
  1265 
  1266 lemma vector_one: "(x::'a ^1) = (\<chi> i. (x$1))"
  1267   by (simp add: vec_eq_iff)
  1268 
  1269 lemma forall_one: "(\<forall>(x::'a ^1). P x) \<longleftrightarrow> (\<forall>x. P(\<chi> i. x))"
  1270   apply auto
  1271   apply (erule_tac x= "x$1" in allE)
  1272   apply (simp only: vector_one[symmetric])
  1273   done
  1274 
  1275 lemma norm_vector_1: "norm (x :: _^1) = norm (x$1)"
  1276   by (simp add: norm_vec_def)
  1277 
  1278 lemma norm_real: "norm(x::real ^ 1) = abs(x$1)"
  1279   by (simp add: norm_vector_1)
  1280 
  1281 lemma dist_real: "dist(x::real ^ 1) y = abs((x$1) - (y$1))"
  1282   by (auto simp add: norm_real dist_norm)
  1283 
  1284 
  1285 subsection{* Explicit vector construction from lists. *}
  1286 
  1287 definition "vector l = (\<chi> i. foldr (\<lambda>x f n. fun_upd (f (n+1)) n x) l (\<lambda>n x. 0) 1 i)"
  1288 
  1289 lemma vector_1: "(vector[x]) $1 = x"
  1290   unfolding vector_def by simp
  1291 
  1292 lemma vector_2:
  1293  "(vector[x,y]) $1 = x"
  1294  "(vector[x,y] :: 'a^2)$2 = (y::'a::zero)"
  1295   unfolding vector_def by simp_all
  1296 
  1297 lemma vector_3:
  1298  "(vector [x,y,z] ::('a::zero)^3)$1 = x"
  1299  "(vector [x,y,z] ::('a::zero)^3)$2 = y"
  1300  "(vector [x,y,z] ::('a::zero)^3)$3 = z"
  1301   unfolding vector_def by simp_all
  1302 
  1303 lemma forall_vector_1: "(\<forall>v::'a::zero^1. P v) \<longleftrightarrow> (\<forall>x. P(vector[x]))"
  1304   apply auto
  1305   apply (erule_tac x="v$1" in allE)
  1306   apply (subgoal_tac "vector [v$1] = v")
  1307   apply simp
  1308   apply (vector vector_def)
  1309   apply simp
  1310   done
  1311 
  1312 lemma forall_vector_2: "(\<forall>v::'a::zero^2. P v) \<longleftrightarrow> (\<forall>x y. P(vector[x, y]))"
  1313   apply auto
  1314   apply (erule_tac x="v$1" in allE)
  1315   apply (erule_tac x="v$2" in allE)
  1316   apply (subgoal_tac "vector [v$1, v$2] = v")
  1317   apply simp
  1318   apply (vector vector_def)
  1319   apply (simp add: forall_2)
  1320   done
  1321 
  1322 lemma forall_vector_3: "(\<forall>v::'a::zero^3. P v) \<longleftrightarrow> (\<forall>x y z. P(vector[x, y, z]))"
  1323   apply auto
  1324   apply (erule_tac x="v$1" in allE)
  1325   apply (erule_tac x="v$2" in allE)
  1326   apply (erule_tac x="v$3" in allE)
  1327   apply (subgoal_tac "vector [v$1, v$2, v$3] = v")
  1328   apply simp
  1329   apply (vector vector_def)
  1330   apply (simp add: forall_3)
  1331   done
  1332 
  1333 lemma bounded_linear_component_cart[intro]: "bounded_linear (\<lambda>x::real^'n. x $ k)"
  1334   apply (rule bounded_linearI[where K=1])
  1335   using component_le_norm_cart[of _ k] unfolding real_norm_def by auto
  1336 
  1337 lemma integral_component_eq_cart[simp]:
  1338   fixes f :: "'n::ordered_euclidean_space \<Rightarrow> real^'m"
  1339   assumes "f integrable_on s"
  1340   shows "integral s (\<lambda>x. f x $ k) = integral s f $ k"
  1341   using integral_linear[OF assms(1) bounded_linear_component_cart,unfolded o_def] .
  1342 
  1343 lemma interval_split_cart:
  1344   "{a..b::real^'n} \<inter> {x. x$k \<le> c} = {a .. (\<chi> i. if i = k then min (b$k) c else b$i)}"
  1345   "{a..b} \<inter> {x. x$k \<ge> c} = {(\<chi> i. if i = k then max (a$k) c else a$i) .. b}"
  1346   apply (rule_tac[!] set_eqI)
  1347   unfolding Int_iff mem_interval_cart mem_Collect_eq
  1348   unfolding vec_lambda_beta
  1349   by auto
  1350 
  1351 lemma interval_bij_bij_cart: fixes x::"real^'n" assumes "\<forall>i. a$i < b$i \<and> u$i < v$i" 
  1352   shows "interval_bij (a,b) (u,v) (interval_bij (u,v) (a,b) x) = x"
  1353   using assms by (intro interval_bij_bij) (auto simp: Basis_vec_def inner_axis)
  1354 
  1355 end